compiler attribute: define __user as __attribute__((btf_type_tag("user")))
Subsystems (6)
cover compiler attribute: define __user as __attribute__((btf_type_tag("user")))
Latest clang compiler supports a type attribute like
__attribute__((btf_type_tag("<arbitrary string>")))
which can be used in places like __user/__rcu.
With the above attribute, clang was able to preserve the "<arbitrary string>" info in dwarf and subsequently BTF. For example __user/__rcu attribute can be preserved in BTF, which currently is loaded inside the kernel. Such information can then be used by verifier to check bpf program memory access conforms to the access attribute. Please see Patches 1 and 2 for details.
This is a RFC patch as it depends on pahole patch and a new pahole version. The following is pahole patch link:
https://lore.kernel.org/bpf/[email protected]/
Second, for bpf verifier use of this new __user tag information, I only implemented support to check function parameter dereference. More work will be needed to check other non function parameter memory accesses.
Yonghong Song (3):
compiler_types: define __user as __attribute__((btf_type_tag("user")))
bpf: reject program if a __user tagged memory accessed in kernel way selftests/bpf: add a selftest with __user tag
include/linux/bpf.h | 1 + include/linux/bpf_verifier.h | 1 + include/linux/btf.h | 5 ++++ include/linux/compiler_types.h | 2 ++ kernel/bpf/btf.c | 15 +++++++++--- kernel/bpf/verifier.c | 16 ++++++++++--- lib/Kconfig.debug | 5 ++++ .../selftests/bpf/bpf_testmod/bpf_testmod.c | 9 +++++++ .../selftests/bpf/prog_tests/btf_tag.c | 23 ++++++++++++++++++ .../selftests/bpf/progs/btf_type_tag_user.c | 24 +++++++++++++++++++ 10 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/btf_type_tag_user.c
Signature · 1 line
2.30.2
RFC PATCH 1/3 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
--- include/linux/compiler_types.h | 2 ++ lib/Kconfig.debug | 5 +++++ 2 files changed, 7 insertions(+)
include/linux/compiler_types.h +2 -0
@@ -31,6 +31,8 @@ static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }# define __kernel# ifdef STRUCTLEAK_PLUGIN # define __user __attribute__((user)) +# elif defined(CONFIG_PAHOLE_HAS_BTF_TAG ) &&__has_attribute (btf_type_tag) +# define __user __attribute__((btf_type_tag("user")))# else # define __user# endif
lib/Kconfig.debug +5 -0
@@ -324,6 +324,11 @@ config DEBUG_INFO_BTF config PAHOLE_HAS_SPLIT_BTF def_bool $(success, test `$(PAHOLE) --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/'` -ge "119") +config PAHOLE_HAS_BTF_TAG + depends on DEBUG_INFO_BTF + depends on CC_IS_CLANG + def_bool $(success, test `$(PAHOLE) --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/'` -ge "122") + config DEBUG_INFO_BTF_MODULES def_bool y depends on DEBUG_INFO_BTF && MODULES && PAHOLE_HAS_SPLIT_BTF
Signature · 1 line
2.30.2
RFC PATCH 2/3 bpf: reject program if a __user tagged memory accessed in kernel way
In reply to Yonghong SongBPF verifier supports direct access, e.g., a->b. If "a" is a pointer pointing to kernel memory, bpf verifier will allow user to write code in C like a->b and bpf verifier will translate it to a kernel load properly. If "a" is a pointer to user memory, it is expected that bpf developer should be bpf_probe_read_user() helper to get the value a->b. In the current mechanism, if "a" is a user pointer, a->b access may trigger a page fault and the verifier generated code will simulate bpf_probe_read() and return 0 for a->b, which may not be correct value.
Now BTF contains __user information, it can check whether the pointer points to a user memory or not. If it is, the verifier can reject the program and force users to use bpf_probe_read_user() helper explicitly.
--- include/linux/bpf.h | 1 + include/linux/bpf_verifier.h | 1 + include/linux/btf.h | 5 +++++ kernel/bpf/btf.c | 15 ++++++++++++--- kernel/bpf/verifier.c | 16 +++++++++++++--- 5 files changed, 32 insertions(+), 6 deletions(-)
include/linux/bpf.h +1 -0
@@ -473,6 +473,7 @@ struct bpf_insn_access_aux {struct {struct btf *btf;u32 btf_id; +bool is_user; }; };struct bpf_verifier_log *log;/* for verbose logs */
include/linux/bpf_verifier.h +1 -0
@@ -66,6 +66,7 @@ struct bpf_reg_state {struct {struct btf *btf;u32 btf_id; +bool is_user; };u32 mem_size;/* for PTR_TO_MEM | PTR_TO_MEM_OR_NULL */
include/linux/btf.h +5 -0
@@ -169,6 +169,11 @@ static inline bool btf_type_is_var(const struct btf_type *t)return BTF_INFO_KIND (t->info) ==BTF_KIND_VAR ; } +static inline bool btf_type_is_type_tag (const struct btf_type *t) +{ +return BTF_INFO_KIND (t->info) ==BTF_KIND_TYPE_TAG ; +} +/* union is only a special case of struct: * all its offsetof(member) == 0 */
kernel/bpf/btf.c +12 -3
@@ -4999,6 +4999,14 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, info->btf = btf; info->btf_id = t->type; t =btf_type_by_id (btf, t->type); + +if (btf_type_is_type_tag (t)) { +const char *tag_value =__btf_name_by_offset (btf, t->name_off); + +if (strcmp (tag_value,"user" ) ==0 ) + info->is_user = true; + } +/* skip modifiers */ while (btf_type_is_modifier (t)) { info->btf_id = t->type;
@@ -5010,8 +5018,9 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, tname, arg, btf_kind_str[BTF_INFO_KIND(t->info)]);return false; } -bpf_log (log,"func '%s' arg%d has btf_id %d type %s '%s'\n" , - tname, arg, info->btf_id, btf_kind_str[BTF_INFO_KIND (t->info)], +bpf_log (log,"func '%s' arg%d has btf_id %d is_user %d type %s '%s'\n" , + tname, arg, info->btf_id, info->is_user, + btf_kind_str[BTF_INFO_KIND (t->info)],__btf_name_by_offset (btf, t->name_off));return true; }
@@ -5030,7 +5039,7 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,u32 i, moff, mtrue_end, msize =0 , total_nelems =0 ;const struct btf_type *mtype, *elem_type =NULL ;const struct btf_member *member; -const char *tname, *mname; +const char *tname, *mname, *tag_value;u32 vlen, elem_id, mid; again:
kernel/bpf/verifier.c +13 -3
@@ -647,7 +647,8 @@ static void print_verifier_state(struct bpf_verifier_env *env,if (t ==PTR_TO_BTF_ID || t ==PTR_TO_BTF_ID_OR_NULL || t ==PTR_TO_PERCPU_BTF_ID ) -verbose (env,"%s" ,kernel_type_name (reg->btf, reg->btf_id)); +verbose (env,"%s,is_user=%d" ,kernel_type_name (reg->btf, reg->btf_id), + reg->is_user);verbose (env,"(id=%d" , reg->id);if (reg_type_may_be_refcounted_or_null (t))verbose (env,",ref_obj_id=%d" , reg->ref_obj_id);
@@ -3551,7 +3552,7 @@ static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,/* check access to 'struct bpf_context' fields. Supports fixed offsets only */ static int check_ctx_access (struct bpf_verifier_env *env,int insn_idx,int off,int size,enum bpf_access_type t,enum bpf_reg_type *reg_type, -struct btf **btf,u32 *btf_id) +struct btf **btf,u32 *btf_id,bool *is_user) {struct bpf_insn_access_aux info = { .reg_type = *reg_type,
@@ -3572,6 +3573,7 @@ static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off,if (*reg_type ==PTR_TO_BTF_ID || *reg_type ==PTR_TO_BTF_ID_OR_NULL ) { *btf = info.btf; *btf_id = info.btf_id; + *is_user = info.is_user; }else { env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; }
@@ -4116,6 +4118,13 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,return -EACCES ; } +if (reg->is_user) { +verbose (env, +"R%d is ptr_%s access user memory: off=%d\n" , + regno, tname, off); +return -EACCES ; + } +if (env->ops->btf_struct_access) { ret = env->ops->btf_struct_access (&env->log, reg->btf, t, off, size, atype, &btf_id);
@@ -4374,7 +4383,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnif (err <0 )return err; - err =check_ctx_access (env, insn_idx, off, size, t, ®_type, &btf, &btf_id); + err =check_ctx_access (env, insn_idx, off, size, t, ®_type, &btf, &btf_id, &is_user);if (err)verbose_linfo (env, insn_idx,"; " );if (!err && t ==BPF_READ && value_regno >=0 ) {
@@ -4399,6 +4408,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn reg_type == PTR_TO_BTF_ID_OR_NULL) { regs[value_regno].btf = btf; regs[value_regno].btf_id = btf_id; + regs[value_regno].is_user = is_user; } } regs[value_regno].type = reg_type;
Signature · 1 line
2.30.2
kernel test robot
build botRe: [RFC PATCH bpf-next 2/3] bpf: reject program if a __user tagged memory accessed in kernel way
reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross #…
Hi Yonghong,
[FYI, it's a private test report for your RFC patch.] [auto build test ERROR on bpf-next/master]
url: https://github.com/0day-ci/linux/commits/Yonghong-Song/compiler-attribute-define-__user-as-__attribute__-btf_type_tag-user/20211118-044124 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: arc-randconfig-r043-20211117 (attached as .config) compiler: arceb-elf-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/07b1ca82a3a2c8779b7451e66b9cd37eb2886c48
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Yonghong-Song/compiler-attribute-define-__user-as-__attribute__-btf_type_tag-user/20211118-044124 git checkout 07b1ca82a3a2c8779b7451e66b9cd37eb2886c48
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=arc SHELL=/bin/bash kernel/bpf/
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
kernel/bpf/verifier.c: In function 'check_mem_access':
>> kernel/bpf/verifier.c:4386:96: error: 'is_user' undeclared (first use in this function); did you mean 'in_userns'?
4386 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, &btf_id, &is_user);
| ^~~~~~~
| in_userns
kernel/bpf/verifier.c:4386:96: note: each undeclared identifier is reported only once for each function it appears in
In file included from include/linux/perf_event.h:25,
from kernel/bpf/verifier.c:20:
At top level:
arch/arc/include/asm/perf_event.h:126:27: warning: 'arc_pmu_cache_map' defined but not used [-Wunused-const-variable=]
126 | static const unsigned int arc_pmu_cache_map[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
| ^~~~~~~~~~~~~~~~~
arch/arc/include/asm/perf_event.h:91:27: warning: 'arc_pmu_ev_hw_map' defined but not used [-Wunused-const-variable=]
91 | static const char * const arc_pmu_ev_hw_map[] = {
| ^~~~~~~~~~~~~~~~~vim +4386 kernel/bpf/verifier.c
4290
4291 /* check whether memory at (regno + off) is accessible for t = (read | write)
4292 * if t==write, value_regno is a register which value is stored into memory
4293 * if t==read, value_regno is a register which will receive the value from memory
4294 * if t==write && value_regno==-1, some unknown value is stored into memory
4295 * if t==read && value_regno==-1, don't care what we read from memory
4296 */
4297 static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
4298 int off, int bpf_size, enum bpf_access_type t,
4299 int value_regno, bool strict_alignment_once)
4300 {
4301 struct bpf_reg_state *regs = cur_regs(env);
4302 struct bpf_reg_state *reg = regs + regno;
4303 struct bpf_func_state *state;
4304 int size, err = 0;
4305
4306 size = bpf_size_to_bytes(bpf_size);
4307 if (size < 0)
4308 return size;
4309
4310 /* alignment checks will add in reg->off themselves */
4311 err = check_ptr_alignment(env, reg, off, size, strict_alignment_once);
4312 if (err)
4313 return err;
4314
4315 /* for access checks, reg->off is just part of off */
4316 off += reg->off;
4317
4318 if (reg->type == PTR_TO_MAP_KEY) {
4319 if (t == BPF_WRITE) {
4320 verbose(env, "write to change key R%d not allowed\n", regno);
4321 return -EACCES;
4322 }
4323
4324 err = check_mem_region_access(env, regno, off, size,
4325 reg->map_ptr->key_size, false);
4326 if (err)
4327 return err;
4328 if (value_regno >= 0)
4329 mark_reg_unknown(env, regs, value_regno);
4330 } else if (reg->type == PTR_TO_MAP_VALUE) {
4331 if (t == BPF_WRITE && value_regno >= 0 &&
4332 is_pointer_value(env, value_regno)) {
4333 verbose(env, "R%d leaks addr into map\n", value_regno);
4334 return -EACCES;
4335 }
4336 err = check_map_access_type(env, regno, off, size, t);
4337 if (err)
4338 return err;
4339 err = check_map_access(env, regno, off, size, false);
4340 if (!err && t == BPF_READ && value_regno >= 0) {
4341 struct bpf_map *map = reg->map_ptr;
4342
4343 /* if map is read-only, track its contents as scalars */
4344 if (tnum_is_const(reg->var_off) &&
4345 bpf_map_is_rdonly(map) &&
4346 map->ops->map_direct_value_addr) {
4347 int map_off = off + reg->var_off.value;
4348 u64 val = 0;
4349
4350 err = bpf_map_direct_read(map, map_off, size,
4351 &val);
4352 if (err)
4353 return err;
4354
4355 regs[value_regno].type = SCALAR_VALUE;
4356 __mark_reg_known(®s[value_regno], val);
4357 } else {
4358 mark_reg_unknown(env, regs, value_regno);
4359 }
4360 }
4361 } else if (reg->type == PTR_TO_MEM) {
4362 if (t == BPF_WRITE && value_regno >= 0 &&
4363 is_pointer_value(env, value_regno)) {
4364 verbose(env, "R%d leaks addr into mem\n", value_regno);
4365 return -EACCES;
4366 }
4367 err = check_mem_region_access(env, regno, off, size,
4368 reg->mem_size, false);
4369 if (!err && t == BPF_READ && value_regno >= 0)
4370 mark_reg_unknown(env, regs, value_regno);
4371 } else if (reg->type == PTR_TO_CTX) {
4372 enum bpf_reg_type reg_type = SCALAR_VALUE;
4373 struct btf *btf = NULL;
4374 u32 btf_id = 0;
4375
4376 if (t == BPF_WRITE && value_regno >= 0 &&
4377 is_pointer_value(env, value_regno)) {
4378 verbose(env, "R%d leaks addr into ctx\n", value_regno);
4379 return -EACCES;
4380 }
4381
4382 err = check_ctx_reg(env, reg, regno);
4383 if (err < 0)
4384 return err;
4385
> 4386 err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, &btf_id, &is_user);
4387 if (err)
4388 verbose_linfo(env, insn_idx, "; ");
4389 if (!err && t == BPF_READ && value_regno >= 0) {
4390 /* ctx access returns either a scalar, or a
4391 * PTR_TO_PACKET[_META,_END]. In the latter
4392 * case, we know the offset is zero.
4393 */
4394 if (reg_type == SCALAR_VALUE) {
4395 mark_reg_unknown(env, regs, value_regno);
4396 } else {
4397 mark_reg_known_zero(env, regs,
4398 value_regno);
4399 if (reg_type_may_be_null(reg_type))
4400 regs[value_regno].id = ++env->id_gen;
4401 /* A load of ctx field could have different
4402 * actual load size with the one encoded in the
4403 * insn. When the dst is PTR, it is for sure not
4404 * a sub-register.
4405 */
4406 regs[value_regno].subreg_def = DEF_NOT_SUBREG;
4407 if (reg_type == PTR_TO_BTF_ID ||
4408 reg_type == PTR_TO_BTF_ID_OR_NULL) {
4409 regs[value_regno].btf = btf;
4410 regs[value_regno].btf_id = btf_id;
4411 regs[value_regno].is_user = is_user;
4412 }
4413 }
4414 regs[value_regno].type = reg_type;
4415 }
4416
4417 } else if (reg->type == PTR_TO_STACK) {
4418 /* Basic bounds checks. */
4419 err = check_stack_access_within_bounds(env, regno, off, size, ACCESS_DIRECT, t);
4420 if (err)
4421 return err;
4422
4423 state = func(env, reg);
4424 err = update_stack_depth(env, state, off);
4425 if (err)
4426 return err;
4427
4428 if (t == BPF_READ)
4429 err = check_stack_read(env, regno, off, size,
4430 value_regno);
4431 else
4432 err = check_stack_write(env, regno, off, size,
4433 value_regno, insn_idx);
4434 } else if (reg_is_pkt_pointer(reg)) {
4435 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
4436 verbose(env, "cannot write into packet\n");
4437 return -EACCES;
4438 }
4439 if (t == BPF_WRITE && value_regno >= 0 &&
4440 is_pointer_value(env, value_regno)) {
4441 verbose(env, "R%d leaks addr into packet\n",
4442 value_regno);
4443 return -EACCES;
4444 }
4445 err = check_packet_access(env, regno, off, size, false);
4446 if (!err && t == BPF_READ && value_regno >= 0)
4447 mark_reg_unknown(env, regs, value_regno);
4448 } else if (reg->type == PTR_TO_FLOW_KEYS) {
4449 if (t == BPF_WRITE && value_regno >= 0 &&
4450 is_pointer_value(env, value_regno)) {
4451 verbose(env, "R%d leaks addr into flow keys\n",
4452 value_regno);
4453 return -EACCES;
4454 }
4455
4456 err = check_flow_keys_access(env, off, size);
4457 if (!err && t == BPF_READ && value_regno >= 0)
4458 mark_reg_unknown(env, regs, value_regno);
4459 } else if (type_is_sk_pointer(reg->type)) {
4460 if (t == BPF_WRITE) {
4461 verbose(env, "R%d cannot write into %s\n",
4462 regno, reg_type_str[reg->type]);
4463 return -EACCES;
4464 }
4465 err = check_sock_access(env, insn_idx, regno, off, size, t);
4466 if (!err && value_regno >= 0)
4467 mark_reg_unknown(env, regs, value_regno);
4468 } else if (reg->type == PTR_TO_TP_BUFFER) {
4469 err = check_tp_buffer_access(env, reg, regno, off, size);
4470 if (!err && t == BPF_READ && value_regno >= 0)
4471 mark_reg_unknown(env, regs, value_regno);
4472 } else if (reg->type == PTR_TO_BTF_ID) {
4473 err = check_ptr_to_btf_access(env, regs, regno, off, size, t,
4474 value_regno);
4475 } else if (reg->type == CONST_PTR_TO_MAP) {
4476 err = check_ptr_to_map_access(env, regs, regno, off, size, t,
4477 value_regno);
4478 } else if (reg->type == PTR_TO_RDONLY_BUF) {
4479 if (t == BPF_WRITE) {
4480 verbose(env, "R%d cannot write into %s\n",
4481 regno, reg_type_str[reg->type]);
4482 return -EACCES;
4483 }
4484 err = check_buffer_access(env, reg, regno, off, size, false,
4485 "rdonly",
4486 &env->prog->aux->max_rdonly_access);
4487 if (!err && value_regno >= 0)
4488 mark_reg_unknown(env, regs, value_regno);
4489 } else if (reg->type == PTR_TO_RDWR_BUF) {
4490 err = check_buffer_access(env, reg, regno, off, size, false,
4491 "rdwr",
4492 &env->prog->aux->max_rdwr_access);
4493 if (!err && t == BPF_READ && value_regno >= 0)
4494 mark_reg_unknown(env, regs, value_regno);
4495 } else {
4496 verbose(env, "R%d invalid mem access '%s'\n", regno,
4497 reg_type_str[reg->type]);
4498 return -EACCES;
4499 }
4500
4501 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
4502 regs[value_regno].type == SCALAR_VALUE) {
4503 /* b/h/w load zero-extends, mark upper bits as known 0 */
4504 coerce_reg_to_size(®s[value_regno], size);
4505 }
4506 return err;
4507 }
4508
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
RFC PATCH 3/3 selftests/bpf: add a selftest with __user tag
In reply to Yonghong SongAdded a selftest where the argument is a pointer with __user tag. Directly accessing its field without helper will result verification failure.
$ ./test_progs -v -n 21/3 ... Successfully loaded bpf_testmod.ko. test_btf_type_tag_user:PASS:btf_type_tag_user 0 nsec libbpf: load bpf program failed: Permission denied libbpf: -- BEGIN DUMP LOG --- libbpf: R1 type=ctx expected=fp ; int BPF_PROG(sub, struct bpf_testmod_btf_type_tag *arg) 0: (79) r1 = *(u64 *)(r1 +0) func 'bpf_testmod_test_btf_type_tag_user' arg0 accesses user memory invalid bpf_context access off=0 size=8 processed 1 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 ... test_btf_type_tag_user:PASS:btf_type_tag_user 0 nsec #21/3 btf_tag/btf_type_tag_user:OK #21 btf_tag:OK Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED Successfully unloaded bpf_testmod.ko.
--- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 9 +++++++ .../selftests/bpf/prog_tests/btf_tag.c | 23 ++++++++++++++++++ .../selftests/bpf/progs/btf_type_tag_user.c | 24 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/btf_type_tag_user.c
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +9 -0
@@ -21,6 +21,15 @@ bpf_testmod_test_mod_kfunc(int i) *(int *)this_cpu_ptr (&bpf_testmod_ksym_percpu) = i; } +struct bpf_testmod_btf_type_tag { +int a; +}; + +noinline int +bpf_testmod_test_btf_type_tag_user (struct bpf_testmod_btf_type_tag __user *arg) { +return arg->a; +} + noinline int bpf_testmod_loop_test(int n) {int i, sum =0 ;
tools/testing/selftests/bpf/prog_tests/btf_tag.c +23 -0
@@ -8,6 +8,7 @@ struct btf_type_tag_test {int **p; };#include "btf_type_tag.skel.h" +#include "btf_type_tag_user.skel.h" static void test_btf_decl_tag (void ) {
@@ -41,10 +42,32 @@ static void test_btf_type_tag(void)btf_type_tag__destroy (skel); } +static void test_btf_type_tag_user (void ) +{ +struct btf_type_tag_user *skel; +int err; + + skel =btf_type_tag_user__open (); +if (!ASSERT_OK_PTR (skel,"btf_type_tag_user" )) +return ; + +if (skel->rodata->skip_tests) { +printf ("%s:SKIP: btf_type_tag attribute not supported" , __func__); +test__skip (); + }else { + err =btf_type_tag_user__load (skel); +ASSERT_ERR (err,"btf_type_tag_user" ); + } + +btf_type_tag_user__destroy (skel); +} +void test_btf_tag (void ) {if (test__start_subtest ("btf_decl_tag" ))test_btf_decl_tag ();if (test__start_subtest ("btf_type_tag" ))test_btf_type_tag (); +if (test__start_subtest ("btf_type_tag_user" )) +test_btf_type_tag_user (); }
tools/testing/selftests/bpf/progs/btf_type_tag_user.c new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +#if __has_attribute (btf_type_tag) +volatile const bool skip_tests = false; +#else +volatile const bool skip_tests = true; +#endif + +struct bpf_testmod_btf_type_tag { +int a; +}; + +int g; + +SEC("fentry/bpf_testmod_test_btf_type_tag_user") +int BPF_PROG (sub ,struct bpf_testmod_btf_type_tag *arg) +{ + g = arg->a; +return 0 ; +}
Signature · 1 line
2.30.2