From patchwork Tue Sep 7 21:19:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Faust X-Patchwork-Id: 1525495 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=BDy1hL5l; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4H3yrk3dL9z9svs for ; Wed, 8 Sep 2021 07:21:57 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5E0F0385F02E for ; Tue, 7 Sep 2021 21:21:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5E0F0385F02E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1631049715; bh=BeFXkK40HAyzW502TyST3nnfq70kOjaBi7UL8js6ULs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=BDy1hL5ly6KYLoeKnoyGYEAMY+tDrYIuks4GEtbHHRKTkZozY5xPjKd5ncidWVTEN Ck0St13ym68CWft83L/9nN2JaLNgPyQzJHqWHX1U3QYeNxxubJ+xHTfJkl1RzBstVx whDBzZ5bbi9fU53eLidS1YQRlVsttovBwRi458T0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id 7B5C9385F02A for ; Tue, 7 Sep 2021 21:21:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7B5C9385F02A Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 187KxHDu029036 for ; Tue, 7 Sep 2021 21:21:05 GMT Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by mx0b-00069f02.pphosted.com with ESMTP id 3axcw68gt1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 07 Sep 2021 21:21:04 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 187LElYG157124 for ; Tue, 7 Sep 2021 21:21:04 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2103.outbound.protection.outlook.com [104.47.70.103]) by userp3030.oracle.com with ESMTP id 3axcq09rvb-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 07 Sep 2021 21:21:03 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VWmuotg7Zt7I0uQn5AM8yvgOE7zJKehVDOduYmlP8mwjlyuSw4tAPfF6PULOX9g/6CEoN6yme6CtsjeaynliuphMhqJdMZNZ48jX7nhdBy7eGLTX9EhV3reKbTVF7/w7Glhcxb0ixQDQP/QkytDc1eJj4uQo8CS+iGDN8fUqpivdvePAUhOwkcnn017b1nm4roIKN7IwcXnUooawFqZ5P+MuGR1epMnxCktGtZtyEEXS4r0rXl6SbHwXJn+g6CeL6qeQbUGtDyiTKzdGzFLpzhLpugx5MmXNyOG+OO4Vqu3vkY/6FpupMFQsMaZTF1LpAJJZTo04idzOpQNV87/VYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=BeFXkK40HAyzW502TyST3nnfq70kOjaBi7UL8js6ULs=; b=VXjtupPT/UfdOQDmmCdkpthXoI/qdKuJc2bdx60gsFAxZSvD+C+mFhGOP6EAHTWHQ8wZQ3qqDv0cgNxFUwglEmx6Up7TDPcbmsq6bzFCX20NgSD6kFXuZB2uTi3E3KFkAC4GoqAP+Rblv7q98gDypD0q/G8Wz1eN9M/C/m5lLmM33RuD45ti2cuO6TN7aXvljmo15+/V0apGfizH9t9kui838WdCN1ZW3wkNIdrBq/K5+XYvi7Gl1YIaMkZeqB/fu5a3DQiJiJd2oRcJQpgIlt00TIQcC7NPJrXQ8i1NeIKSggNOva6Uiblll2TMilhL7m5pxLWqKQCeW3cP8GW2jQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none Received: from SA2PR10MB4458.namprd10.prod.outlook.com (2603:10b6:806:f8::24) by SA2PR10MB4476.namprd10.prod.outlook.com (2603:10b6:806:f9::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4500.14; Tue, 7 Sep 2021 21:21:00 +0000 Received: from SA2PR10MB4458.namprd10.prod.outlook.com ([fe80::1493:70de:28e4:3d3e]) by SA2PR10MB4458.namprd10.prod.outlook.com ([fe80::1493:70de:28e4:3d3e%4]) with mapi id 15.20.4500.014; Tue, 7 Sep 2021 21:21:00 +0000 To: gcc-patches@gcc.gnu.org Subject: [COMMITTED V2 0/7] BPF CO-RE Support Date: Tue, 7 Sep 2021 14:19:42 -0700 Message-Id: <20210907211949.8436-1-david.faust@oracle.com> X-Mailer: git-send-email 2.31.1 X-ClientProxiedBy: SN4PR0201CA0049.namprd02.prod.outlook.com (2603:10b6:803:20::11) To SA2PR10MB4458.namprd10.prod.outlook.com (2603:10b6:806:f8::24) MIME-Version: 1.0 Received: from sequence.us.oracle.com (2606:b400:8301:1041::10) by SN4PR0201CA0049.namprd02.prod.outlook.com (2603:10b6:803:20::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4500.14 via Frontend Transport; Tue, 7 Sep 2021 21:20:59 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 2a83d9d1-46c4-4a82-52a9-08d9724563c9 X-MS-TrafficTypeDiagnostic: SA2PR10MB4476: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3vp4TvVWsm4kLLCaQ+0xVnZV7bVXGierjO+s7Dlww/atULPA4hcPtgWje/+jblEQqgDL7XHqR1Me3Y8235Gu/FSwz/ozcGLDvt6aQrgH+EKk6z0o9H1y3IIztWtMp7hM62P5RIqOCt6iMBCsqBSsmvWK7dom2vX4scH69XyaxrdnL3LSnqFaZDbfbBc2Mb040qbpLyuHHcNcaJ8TGulCwCDkcjQqAX00BSrIv6I8rkU1wE71/P4L9oWVVJbnXlRc1CPmB8+rZ5nNSFZBpU9FSpcuiiSc71xvxCKMdR+5WBpMmC0pwK8xzYktOWicC0Ax1m2PJPnO4Jwynm36HVQOL+wvkiKQhFmRezQSJQFMWkoSgQ28fGDoqIMX2I1kguunMkerjBKO8ufE5SyJtMkTIg30J+4i1ZFINOFxa3E95sY9NoutswXJgD+bSE6e57c6f2CAyAg2tdzR6G0uy4L2c9DaOtNa8kJJW4c021Fd+l1PfoZ1kELjIISQuf63/damGAp1rJnbUSbGa6wPJI3sg8HA1ZKpPdKvLnEpAefu5PQmLTuiLaKaEV2wMiV4KGoqp1MA7DRAHFqeu5bQy21MKCOyGOVh/VtuH1quv3KxXvsvX1EMMwybAK/e/czgnvk2Epb1HU2G8LSqW1a0b/aPuw== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SA2PR10MB4458.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(366004)(7696005)(52116002)(6916009)(38100700002)(6666004)(2616005)(5660300002)(1076003)(66476007)(66556008)(8676002)(186003)(8936002)(66946007)(2906002)(44832011)(316002)(86362001)(83380400001)(6486002)(36756003)(508600001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: DMj2yElJ5roEYz41Y51K5bj6a9eXR6qezRNMjkTmMWnVtH4FGCN079/godMN3i2SPYJ0vTWz330egeY9p4FsoIJhBS3RwNN4ri3vFC5Y5Fjfvpx5wTsLMBF5sQCQThrpxbOHOl6T17qwhAexiLO+4Avw1R+EPGr6uTH7TRTwlBVR9LA70fgSVXALUZxiq77/LtY10usL218p4jcWAOKLhoZ5cMXBU3AhEaxIX39sKujhiHg68q6qIq9xxcyZNHB3fJJxnOAuHX8uIcPWNXqdgCdN+8bshqM5+M7fWD5JSLYScUPyYkQXolg/2EpqOi3vowU8kQkKqMgeIMa29JcbwV18sNu0/xqh5WTTVpkOdpPxVIRX7rPqWP2qAYGYhw/ZAqlZtSiJOPrdra8fpRVjSaTsmOPkiqju7HJxnpzL1D9qCrYd9v8MRurKKmkdXE9XJlw6TcGiR3lcCr8n5PvWwwvcgGJpYmpGH31GU+5g2ZUNeK61IWMh+df3B3N0WLhrHscv+V5MECr5Li6hVM1j/uzqS8Tdy9E1mRO98BN4qvXqGrwsB3nvARyd5oSK4e5tX6E18zU2MJAiEy+irC7NjZG7pXsWia2DjtvA3cB3Ngvyhko1jeJNS3Gm8lYDGbpr0flqv0lyYQCPo6+qHjNpTm+mOQ+GCzNOniIQmVJYdG6rjn+6sYglhtRY3cqkJw2gFrib2RWiwAlIHJeUPAKz7NFi6x18b4ViyHfyGR9FJTDbHxMe+oMXO7rFe0DK8u7U1K1cDKoNzWNXzbjB+VmaiYcxuNbX26gTccKQ4hhOf0klopq3BQpbrTtJ1u1U6tNTRfeejxX2yhlyHAtnq9cPKKv1r6TVFlY0FdMEgVunH+Ox7tXH79K2REJInYzlsWnoW8pdKB6H3Vo1N5Ln6iJFUOsVN6tDLBa4y63Gk/npmVrI2MfwUOikI2z14RP/m7x41rp1imOCI+nD91k3kduq5w64jn62+79zA9SXlfs14NTh9qPGDHjyTjO1G0LxIoHqIh8N9snlNdA0v0t8PA+3ok/fPYPURwXnQ9i1XtdGTjBBBoAW5cKo58JkpqjLSLB9utozDvcDCo8cqdry3Ac7XhZL7IrCAuhrYdCo0TOe4RZWWd/e4pf3igY6LenIfw7UePs5sS08ZURIevp/SIBpcIe9Asbu6/Q1ncu3H+a+Y3QBDg49N5pXgVy2Hmih/seTbMfJvtFgMs9+tlXZcCTblk/8e9HrkYfOCgKA3lcQJtcZildl7a6c2paHvg3xFW0j8T+omQzvCxza5HSyZVB8IbAPT7QJiKJOrOJu7daQwYqYILqwDm6JknwmeF75q9+3cGOj+KQzE0QOpzx/5lhfoQ== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2a83d9d1-46c4-4a82-52a9-08d9724563c9 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4458.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Sep 2021 21:21:00.3079 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: oKfAB9w6AV+oriK69B/0IkFvuTpW+PgvMCNSR2ctIDG1ku7ixdJMRNwn/w1gAf25ZQwQ6vbBceXO9b2PkWntEA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4476 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10100 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 adultscore=0 malwarescore=0 mlxscore=0 spamscore=0 bulkscore=0 mlxlogscore=999 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2109030001 definitions=main-2109070134 X-Proofpoint-GUID: naS4FNTliybHaWDkAXq9hTKHI9v8fUyE X-Proofpoint-ORIG-GUID: naS4FNTliybHaWDkAXq9hTKHI9v8fUyE X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, MSGID_FROM_MTA_HEADER, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: David Faust via Gcc-patches From: David Faust Reply-To: David Faust Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" [ Changes from V1: All patches have been OK'd, but the prerequisite series "Allow means for late BTF generation for BPF CO-RE" had not been accepted. Now that that series has been applied, this can be pushed with some very minor tweaks: - Accomodate rename of option '-mco-re' (was -mcore) in option handling, tests and documentation. - Set BTF_WITH_CORE_DEBUG write symbols flag where needed. ] Hello, This patch series adds support for the BPF Compile Once - Run Everywhere (BPF CO-RE) mechanism in GCC. A BPF program is some user code which is injected (via a verifier and loader) into a running kernel, and executed in kernel context. To do useful work, a BPF program generally must interact with kernel data structures in some way. Therefore, BPF programs written in C usually include kernel headers. This introduces two major portability issues when compiling BPF programs: 1. Kernel data structures regularly change, with fields added, moved or deleted between versions. An eBPF program cannot in general be expected to run on any systems which does not share an identical kernel version to the system on which it was compiled. 2. Included kernel headers (and used data structures) may be internal, not exposed in an userspace API, and therefore target-specific. An eBPF program compiled on an x86_64 machine will include x86_64 kernel headers. The resulting program may not run well (or at all) in machines of another architecture. BPF CO-RE is designed to solve the first issue by leveraging the BPF loader to adjust references to kernel data structures made by the program as-needed according to versions of structures actually present on the host kernel. To achieve this, additional information is placed in a ".BTF.ext" section. This information tells the loader which references will require adjusting, and how to perform each necessary adjustment. For any access to a data structure which may require load-time adjustment, the following information is recorded (making up a CO-RE relocation record): - The BTF type ID of the outermost structure which is accessed. - An access string encoding the accessed member via a series of member and array indexes. These indexes are used to look up detailed BTF information about the member. - The offset of the appropriate instruction to patch in the BPF program. - An integer specifying what kind of relocation to perform. A CO-RE-capable BPF loader reads this information together with the BTF information of the program, compares it against BTF information of the host kernel, and determines the appropriate way to patch the specified instruction. Once all CO-RE relocations are resolved, the program is loaded and verified as usual. The process can be summarized with the following diagram: +------------+ | C compiler | +-----+------+ | BPF + BTF + CO-RE relocations v +------------+ +--->| BPF loader | | +-----+------+ | | BPF (adapted) BTF | v | +------------+ +----+ Kernel | +------------+ Note that a single ELF object may contain multiple eBPF programs. As a result, a single .BTF.ext section can contain CO-RE relocations for multiple programs in distinct sections. Many data structure accesses (e.g., those described in the program itself) do not need to be patched. So, GCC only generates CO-RE information for accesses marked as being "of interest." To be compatible with LLVM a new BPF target builtin, __builtin_preserve_access_index, is implemented. Any accesses to aggregate data structures (structs, unions, arrays) in the argument will have appropriate CO-RE information generated and output. This builtin is otherwise transparent - it does not alter the program's functionality in any way. In addition, a new BPF target attribute preserve_access_index is added. This attribute may annotate struct and union type definitions. Any access to a type with this attribute is automatically "of interest," and will have CO-RE information generated accordingly. Finally, generation of BPF CO-RE information is gated behind a new BPF option, -mcore (and its negative, -mno-core). Because CO-RE support is intimately tied to BTF debug information, -gbtf for BPF target implies -mcore, and -mcore requires BTF generation. For cases where BTF information is desired but CO-RE is not important, it can be disabled with -mno-core. David Faust (7): dwarf: externalize lookup_type_die ctfc: externalize ctf_dtd_lookup ctfc: add function to lookup CTF ID of a TREE type btf: expose get_btf_id bpf: BPF CO-RE support bpf testsuite: Add BPF CO-RE tests doc: BPF CO-RE documentation gcc/btfout.c | 2 +- gcc/config.gcc | 3 + gcc/config/bpf/bpf-passes.def | 20 + gcc/config/bpf/bpf-protos.h | 2 + gcc/config/bpf/bpf.c | 591 ++++++++++++++++++ gcc/config/bpf/coreout.c | 356 +++++++++++ gcc/config/bpf/coreout.h | 114 ++++ gcc/config/bpf/t-bpf | 8 + gcc/ctfc.c | 18 +- gcc/ctfc.h | 8 +- gcc/doc/extend.texi | 16 + gcc/doc/invoke.texi | 13 +- gcc/dwarf2out.c | 3 +- gcc/dwarf2out.h | 1 + gcc/testsuite/gcc.target/bpf/core-attr-1.c | 23 + gcc/testsuite/gcc.target/bpf/core-attr-2.c | 21 + gcc/testsuite/gcc.target/bpf/core-attr-3.c | 41 ++ gcc/testsuite/gcc.target/bpf/core-attr-4.c | 35 ++ gcc/testsuite/gcc.target/bpf/core-builtin-1.c | 64 ++ gcc/testsuite/gcc.target/bpf/core-builtin-2.c | 26 + gcc/testsuite/gcc.target/bpf/core-builtin-3.c | 26 + gcc/testsuite/gcc.target/bpf/core-section-1.c | 38 ++ 22 files changed, 1423 insertions(+), 6 deletions(-) create mode 100644 gcc/config/bpf/bpf-passes.def create mode 100644 gcc/config/bpf/coreout.c create mode 100644 gcc/config/bpf/coreout.h create mode 100644 gcc/testsuite/gcc.target/bpf/core-attr-1.c create mode 100644 gcc/testsuite/gcc.target/bpf/core-attr-2.c create mode 100644 gcc/testsuite/gcc.target/bpf/core-attr-3.c create mode 100644 gcc/testsuite/gcc.target/bpf/core-attr-4.c create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-1.c create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-2.c create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-3.c create mode 100644 gcc/testsuite/gcc.target/bpf/core-section-1.c