From patchwork Tue Aug 4 18:24:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 262725 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF11FC433DF for ; Tue, 4 Aug 2020 18:24:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9CB6022B45 for ; Tue, 4 Aug 2020 18:24:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="kltBEm7x" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726622AbgHDSYX (ORCPT ); Tue, 4 Aug 2020 14:24:23 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:1612 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726542AbgHDSYW (ORCPT ); Tue, 4 Aug 2020 14:24:22 -0400 Received: from pps.filterd (m0044010.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 074IAGEc006286 for ; Tue, 4 Aug 2020 11:24:21 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=facebook; bh=6fndHO52AYDmg/fBFhWy49SBXHBSgUb1R1ovH26t910=; b=kltBEm7xabLkCKWj+2WbvhZr4C32Ymy+rkhGzBqwx3E7SgF2P5IvaLTlcLxjt6nEfsA1 zop9fC9KF/bakxr4kRzBzCFVAXR7YwPbXX6EIlExWChHabTXFngUwBGBtIcS8ephCDp7 LnF2FqVCHsrHIIpGlZesVpldQnwPIngID5U= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 32nr82knvb-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 04 Aug 2020 11:24:21 -0700 Received: from intmgw001.03.ash8.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::d) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Tue, 4 Aug 2020 11:24:18 -0700 Received: by devbig012.ftw2.facebook.com (Postfix, from userid 137359) id BFAB92EC52E0; Tue, 4 Aug 2020 11:24:15 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Andrii Nakryiko Smtp-Origin-Hostname: devbig012.ftw2.facebook.com To: , , , CC: , , Andrii Nakryiko Smtp-Origin-Cluster: ftw2c04 Subject: [RFC PATCH bpf-next 0/9] Add support for type-based and enum value-based CO-RE relocations Date: Tue, 4 Aug 2020 11:24:00 -0700 Message-ID: <20200804182409.1512434-1-andriin@fb.com> X-Mailer: git-send-email 2.24.1 MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235, 18.0.687 definitions=2020-08-04_04:2020-08-03,2020-08-04 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 adultscore=0 impostorscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 malwarescore=0 suspectscore=0 phishscore=0 priorityscore=1501 bulkscore=0 mlxscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2008040132 X-FB-Internal: deliver Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org N.B. Posting this patch set as an RFC to raise awareness and let people think about other ways to apply these in practical applications. This patch set adds libbpf support to two new classes of CO-RE relocations: type-based (TYPE_EXISTS/TYPE_SIZE/TYPE_ID_LOCAL/TYPE_ID_TARGET) and enum value-vased (ENUMVAL_EXISTS/ENUMVAL_VALUE): - TYPE_EXISTS allows to detect presence in kernel BTF of a locally-recorded BTF type. Useful for feature detection (new functionality often comes with new internal kernel types), as well as handling type renames and bigger refactorings. - TYPE_SIZE allows to get the real size (in bytes) of a specified kernel type. Useful for dumping internal structure as-is through perfbuf or ringbuf. - TYPE_ID_LOCAL/TYPE_ID_TARGET allow to capture BTF type ID of a BTF type in program's BTF or kernel BTF, respectively. These could be used for high-performance and space-efficient generic data dumping/logging by relying on small and cheap BTF type ID as a data layout descriptor, for post-processing on user-space side. - ENUMVAL_EXISTS can be used for detecting the presence of enumerator value in kernel's enum type. Most direct application is to detect BPF helper support in kernel. - ENUMVAL_VALUE allows to relocate real integer value of kernel enumerator value, which is subject to change (e.g., always a potential issue for internal, non-UAPI, kernel enums). I've indicated potential applications for these relocations, but relocations themselves are generic and unassuming and are designed to work correctly even in unintended applications. Furthermore, relocated values become constants, known to the verifier and could and would be used for dead branch code detection and elimination. This makes them ideal to do all sorts of feature detection and guarding functionality that's not available on some older (but still supported by BPF program) kernels, while having to compile and maintain one unified source code. As part of this patch set, one potential issue with ambiguous CO-RE relocations was solved (see patch #3). There are also some improvements to the way debug relocation logs are emitted, helping to get a high-level idea of what's going on for users that are willing to dive deeper into the internals of libbpf (or libbpf contributors, of course). Selftests are added for all the new features and relocation ambiguity issue is excercised as well. LLVM patches adding these relocation in Clang: - __builtin_btf_type_id() ([0], [1], [2]); - __builtin_preserve_type_info(), __builtin_preserve_enum_value() ([3], [4]). [0] https://reviews.llvm.org/D74572 [1] https://reviews.llvm.org/D74668 [2] https://reviews.llvm.org/D85174 [3] https://reviews.llvm.org/D83878 [4] https://reviews.llvm.org/D83242 Andrii Nakryiko (9): libbpf: improve error logging for mismatched BTF kind cases libbpf: clean up and improve CO-RE reloc logging libbpf: improve relocation ambiguity detection selftests/bpf: add test validating failure on ambiguous relocation value libbpf: implement type-based CO-RE relocations support selftests/bpf: test TYPE_EXISTS and TYPE_SIZE CO-RE relocations selftests/bpf: add CO-RE relo test for TYPE_ID_LOCAL/TYPE_ID_TARGET libbpf: implement enum value-based CO-RE relocations selftests/bpf: add tests for ENUMVAL_EXISTS/ENUMVAL_VALUE relocations tools/lib/bpf/Makefile | 2 +- tools/lib/bpf/bpf_core_read.h | 80 +- tools/lib/bpf/btf.c | 17 +- tools/lib/bpf/btf.h | 38 - tools/lib/bpf/libbpf.c | 754 ++++++++++++++---- tools/lib/bpf/libbpf_internal.h | 84 +- .../selftests/bpf/prog_tests/core_reloc.c | 328 +++++++- .../bpf/progs/btf__core_reloc_enumval.c | 3 + .../progs/btf__core_reloc_enumval___diff.c | 3 + .../btf__core_reloc_enumval___err_missing.c | 3 + .../btf__core_reloc_enumval___val3_missing.c | 3 + .../btf__core_reloc_size___err_ambiguous.c | 4 + .../bpf/progs/btf__core_reloc_type_based.c | 3 + ...btf__core_reloc_type_based___all_missing.c | 3 + .../btf__core_reloc_type_based___diff_sz.c | 3 + ...f__core_reloc_type_based___fn_wrong_args.c | 3 + .../btf__core_reloc_type_based___incompat.c | 3 + .../bpf/progs/btf__core_reloc_type_id.c | 3 + ...tf__core_reloc_type_id___missing_targets.c | 3 + .../selftests/bpf/progs/core_reloc_types.h | 352 +++++++- .../bpf/progs/test_core_reloc_enumval.c | 69 ++ .../bpf/progs/test_core_reloc_type_based.c | 107 +++ .../bpf/progs/test_core_reloc_type_id.c | 94 +++ 23 files changed, 1728 insertions(+), 234 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval.c create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___diff.c create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_missing.c create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___val3_missing.c create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_size___err_ambiguous.c create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_type_based.c create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_type_based___all_missing.c create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_type_based___diff_sz.c create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_type_based___fn_wrong_args.c create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_type_based___incompat.c create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_type_id.c create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_type_id___missing_targets.c create mode 100644 tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c create mode 100644 tools/testing/selftests/bpf/progs/test_core_reloc_type_based.c create mode 100644 tools/testing/selftests/bpf/progs/test_core_reloc_type_id.c