From patchwork Wed Feb 1 17:30:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 51492 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp411228wrn; Wed, 1 Feb 2023 09:37:13 -0800 (PST) X-Google-Smtp-Source: AK7set/SY4sk2Jx6q9EWMyONjHOQDbMIY1uQsZAxwkQZP+tT5HqhW+06wLvnOycox7NKxfQvgbO6 X-Received: by 2002:a05:6a21:900d:b0:bf:110f:c6c6 with SMTP id tq13-20020a056a21900d00b000bf110fc6c6mr2962061pzb.49.1675273032873; Wed, 01 Feb 2023 09:37:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675273032; cv=none; d=google.com; s=arc-20160816; b=n1fSRxqrawZU4H2cibxZkBNcUVAcuB5XLYsR6NQv1PP/TtuUoSkb6Aup+sqA7xK7Oc AYlxNtPBtC/jrktWIrQXalLblrguptNcXmHiDyTrOMnwci8gjO7u+APbjLdTLCzQYNbD 452QekZfqMDr86KyqEPSOGV5tS7lMX3T2Q+GhNAJUhAhnubJh65iJcSgBaGJrIGptdyL 4uUwPL04NlqUM5rGqrf5mslMgwO2c9wEdtXcCEqOH7RerKj1V6FgtZJH9DHZ4RYvkufr 4VtozsMurOu61g5reiWcKNtRdFUmrYxMTZCNeoZmyMg8xgH6CAGltzrU6Hfpwx4e10L4 INzg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=GMUIMRDtOkpySHrb7MIScjLqI4hTvf6fpXHhnvoXlBE=; b=TjmM0VdWGBWxyqdK7es6UXOneMAcOsZGiBFyGmzAWBvDYLeX5dtZt3vBJS92u4yOkM B2KaBhfyx88airRpZ25mfVxN5RC2/554dfTaR6jUN6qu316Ry4S55f9xGPYzWMqiqtJ/ AhfVemSj5r7MgEN5fErSEle58pKOTKy918aVb8vQ5APgLDeHGdzTGtsXFtMJdE8Hm6PZ nK1uYQAarMRZjf8UqYar567zmwEFrxmWVLb6NANN9z3m1qQa8XL9WUGPA7TpI1OCyLeU xonA8En2nTDKnavCeTfNlro8ZigF2UjaszBPtfMLDlaFflmNgDT42NtvLVmQObAxYmdO 9zmA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k12-20020a63ab4c000000b004bcbc0feb02si20030070pgp.650.2023.02.01.09.37.00; Wed, 01 Feb 2023 09:37:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231717AbjBARbW (ORCPT + 99 others); Wed, 1 Feb 2023 12:31:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231733AbjBARaw (ORCPT ); Wed, 1 Feb 2023 12:30:52 -0500 Received: from mail-qt1-f174.google.com (mail-qt1-f174.google.com [209.85.160.174]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0088977DFB; Wed, 1 Feb 2023 09:30:23 -0800 (PST) Received: by mail-qt1-f174.google.com with SMTP id v17so7838807qto.3; Wed, 01 Feb 2023 09:30:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GMUIMRDtOkpySHrb7MIScjLqI4hTvf6fpXHhnvoXlBE=; b=MYAvEWUZkns0jkI7JFb8JHm9eUtgrNYCNMZ9nSA2pVNO3XXxmDrOze2o6KQmIXvRYC HoeSWJfJTCJL0BFDhnks2dEXdeSQP5AUrNu0M+QUKNcHr1A8mKVVhJLti0vBBErcKhFD su+ibMK/uG+wq1APIJWp3CowiKlfKFyop53wiHtP3dSiJbeSmVZ23Ra0+BTzHkw2zk1W 8lD42FQgq9bRYp924ddBQFRHJwL/LK8QWvGOxzqysknm2rn4a7c0d/FJIyJ7HYpCRbKY C2aLfiacPMx0rEwEzpvlQErT1gfQzx3SPe8gb7wBR3y+ETenBiTxteA0c4pzovEOXyZy qc8Q== X-Gm-Message-State: AO0yUKUxmQGoAx6TyUQe1sIVnUFCYLtEUKzM0+eYaN8HEDM05EsVzQBV FBOiMsoyRGMKwIlvdydwma/WtpqZWFmcnNQj X-Received: by 2002:a05:622a:607:b0:3b8:330c:c966 with SMTP id z7-20020a05622a060700b003b8330cc966mr4541813qta.51.1675272622538; Wed, 01 Feb 2023 09:30:22 -0800 (PST) Received: from localhost ([2620:10d:c091:480::1:1971]) by smtp.gmail.com with ESMTPSA id hh5-20020a05622a618500b0039cc0fbdb61sm12002744qtb.53.2023.02.01.09.30.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 09:30:22 -0800 (PST) From: David Vernet To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@meta.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, corbet@lwn.net, hch@infradead.org, acme@kernel.org, alan.maguire@oracle.com Subject: [PATCH bpf-next v3 1/4] bpf: Add __bpf_kfunc tag for marking kernel functions as kfuncs Date: Wed, 1 Feb 2023 11:30:13 -0600 Message-Id: <20230201173016.342758-2-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230201173016.342758-1-void@manifault.com> References: <20230201173016.342758-1-void@manifault.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756651095733563816?= X-GMAIL-MSGID: =?utf-8?q?1756651095733563816?= kfuncs are functions defined in the kernel, which may be invoked by BPF programs. They may or may not also be used as regular kernel functions, implying that they may be static (in which case the compiler could e.g. inline it away, or elide one or more arguments), or it could have external linkage, but potentially be elided in an LTO build if a function is observed to never be used, and is stripped from the final kernel binary. This has already resulted in some issues, such as those discussed in [0] wherein changes in DWARF that identify when a parameter has been optimized out can break BTF encodings (and in general break the kfunc). [0]: https://lore.kernel.org/all/1675088985-20300-2-git-send-email-alan.maguire@oracle.com/ We therefore require some convenience macro that kfunc developers can use just add to their kfuncs, and which will prevent all of the above issues from happening. This is in contrast with what we have today, where some kfunc definitions have "noinline", some have "__used", and others are static and have neither. Note that longer term, this mechanism may be replaced by a macro that more closely resembles EXPORT_SYMBOL_GPL(), as described in [1]. For now, we're going with this shorter-term approach to fix existing issues in kfuncs. [1]: https://lore.kernel.org/lkml/Y9AFT4pTydKh+PD3@maniforge.lan/ Note as well that checkpatch complains about this patch with the following: ERROR: Macros with complex values should be enclosed in parentheses +#define __bpf_kfunc __used noinline There seems to be a precedent for using this pattern in other places such as compiler_types.h (see e.g. __randomize_layout and noinstr), so it seems appropriate. Acked-by: Stanislav Fomichev Signed-off-by: David Vernet --- include/linux/btf.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/linux/btf.h b/include/linux/btf.h index e9b90d9c3569..49e0fe6d8274 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -72,6 +72,14 @@ #define KF_DESTRUCTIVE (1 << 6) /* kfunc performs destructive actions */ #define KF_RCU (1 << 7) /* kfunc only takes rcu pointer arguments */ +/* + * Tag marking a kernel function as a kfunc. This is meant to minimize the + * amount of copy-paste that kfunc authors have to include for correctness so + * as to avoid issues such as the compiler inlining or eliding either a static + * kfunc, or a global kfunc in an LTO build. + */ +#define __bpf_kfunc __used noinline + /* * Return the name of the passed struct, if exists, or halt the build if for * example the structure gets renamed. In this way, developers have to revisit