Skip to content

Commit f1d79a9

Browse files
yonghong-songNobody
authored and
Nobody
committed
compiler_types: define __user as __attribute__((btf_type_tag("user")))
If pahole and compiler supports btf_type_tag attributes, during kernel build, we can define __user as __attribute__((btf_type_tag("user"))). This will encode __user information in BTF. Such information, encoded in BTF as BTF_KIND_TYPE_TAG, can help bpf verifier to ensure proper memory dereference mechanism depending on user memory or kernel memory. The encoded __user info is also useful for other tracing facility where instead of to require user to specify kernel/user address type, the kernel can detect it by itself with btf. The following is an example with latest upstream clang (clang14, [1]) and latest pahole: [$ ~] cat test.c #define __tag1 __attribute__((btf_type_tag("tag1"))) int foo(int __tag1 *arg) { return *arg; } [$ ~] clang -O2 -g -c test.c [$ ~] pahole -JV test.o ... [1] INT int size=4 nr_bits=32 encoding=SIGNED [2] TYPE_TAG tag1 type_id=1 [3] PTR (anon) type_id=2 [4] FUNC_PROTO (anon) return=1 args=(3 arg) [5] FUNC foo type_id=4 [$ ~] You can see for the function argument "int __tag1 *arg", its type is described as PTR -> TYPE_TAG(tag1) -> INT The kernel can take advantage of this information to bpf verification or other use cases. Current btf_type_tag is only supported in clang (>= clang14). gcc support is also proposed and under development ([2]). [1] https://reviews.llvm.org/D111199 [2] https://www.spinics.net/lists/bpf/msg45773.html Signed-off-by: Yonghong Song <[email protected]>
1 parent 91840df commit f1d79a9

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

include/linux/compiler_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
3131
# define __kernel
3232
# ifdef STRUCTLEAK_PLUGIN
3333
# define __user __attribute__((user))
34+
# elif defined(CONFIG_PAHOLE_HAS_BTF_TAG) && __has_attribute(btf_type_tag)
35+
# define __user __attribute__((btf_type_tag("user")))
3436
# else
3537
# define __user
3638
# endif

lib/Kconfig.debug

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ config DEBUG_INFO_BTF
324324
config PAHOLE_HAS_SPLIT_BTF
325325
def_bool $(success, test `$(PAHOLE) --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/'` -ge "119")
326326

327+
config PAHOLE_HAS_BTF_TAG
328+
depends on DEBUG_INFO_BTF
329+
depends on CC_IS_CLANG
330+
def_bool $(success, test `$(PAHOLE) --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/'` -ge "122")
331+
327332
config DEBUG_INFO_BTF_MODULES
328333
def_bool y
329334
depends on DEBUG_INFO_BTF && MODULES && PAHOLE_HAS_SPLIT_BTF

0 commit comments

Comments
 (0)