[PATCH bpf-next v2 1/3] bpf: Add BPF_KFUNC macro for defining kfuncs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 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.

We therefore require some convenience macro that kfunc developers can
use when defining a kfunc, 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.

In addition to providing the obvious correctness benefits, having such a
macro also provides an easy way to query for if kfuncs, as Christoph
suggested at the kernel maintainers summit
(https://lwn.net/Articles/908464/). This is currently possible by
grepping for BTF_ID_FLAGS(func, but having something more self
describing is useful.

Note that checkpatch complains about this patch with the following:

ERROR: Macros with multiple statements should be enclosed in a do - while loop
+#define BPF_KFUNC(proto)       \
+       proto;                  \
+       __used noinline proto

Given that we're wrapping the function definition and also including its
prototype, using do { } while(0) isn't an option. Hopefully it's
therefore inappropriate to ignore the warning.

Signed-off-by: David Vernet <void@xxxxxxxxxxxxx>
---
 include/linux/btf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/include/linux/btf.h b/include/linux/btf.h
index 5f628f323442..593a1c2615e7 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -72,6 +72,16 @@
 #define KF_DESTRUCTIVE  (1 << 6) /* kfunc performs destructive actions */
 #define KF_RCU          (1 << 7) /* kfunc only takes rcu pointer arguments */
 
+/*
+ * Macro for defining 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(proto)	\
+	proto;			\
+	__used noinline proto
+
 /*
  * 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
-- 
2.39.0




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux