Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add more test case for field flattening

Add three success test cases to test the flattening of array of nested
struct. For these three tests, the number of special fields in map is
BTF_FIELDS_MAX, but the array is defined in structs with different
nested level.

Add one failure test case for the flattening as well. In the test case,
the number of special fields in map is BTF_FIELDS_MAX + 1. It will make
btf_parse_fields() in map_create() return -E2BIG, the creation of map
will succeed, but the load of program will fail because the btf_record
is invalid for the map.

Signed-off-by: Hou Tao <houtao1@huawei.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20241008071114.3718177-3-houtao@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Hou Tao and committed by
Alexei Starovoitov
c456f080 797d73ee

+117 -2
+1
tools/testing/selftests/bpf/prog_tests/cpumask.c
··· 23 23 "test_global_mask_array_l2_rcu", 24 24 "test_global_mask_nested_rcu", 25 25 "test_global_mask_nested_deep_rcu", 26 + "test_global_mask_nested_deep_array_rcu", 26 27 "test_cpumask_weight", 27 28 }; 28 29
+5
tools/testing/selftests/bpf/progs/cpumask_common.h
··· 7 7 #include "errno.h" 8 8 #include <stdbool.h> 9 9 10 + /* Should use BTF_FIELDS_MAX, but it is not always available in vmlinux.h, 11 + * so use the hard-coded number as a workaround. 12 + */ 13 + #define CPUMASK_KPTR_FIELDS_MAX 11 14 + 10 15 int err; 11 16 12 17 #define private(name) SEC(".bss." #name) __attribute__((aligned(8)))
+35
tools/testing/selftests/bpf/progs/cpumask_failure.c
··· 10 10 11 11 char _license[] SEC("license") = "GPL"; 12 12 13 + struct kptr_nested_array_2 { 14 + struct bpf_cpumask __kptr * mask; 15 + }; 16 + 17 + struct kptr_nested_array_1 { 18 + /* Make btf_parse_fields() in map_create() return -E2BIG */ 19 + struct kptr_nested_array_2 d_2[CPUMASK_KPTR_FIELDS_MAX + 1]; 20 + }; 21 + 22 + struct kptr_nested_array { 23 + struct kptr_nested_array_1 d_1; 24 + }; 25 + 26 + private(MASK_NESTED) static struct kptr_nested_array global_mask_nested_arr; 27 + 13 28 /* Prototype for all of the program trace events below: 14 29 * 15 30 * TRACE_EVENT(task_newtask, ··· 199 184 bpf_rcu_read_unlock(); 200 185 if (prev) 201 186 bpf_cpumask_release(prev); 187 + 188 + return 0; 189 + } 190 + 191 + SEC("tp_btf/task_newtask") 192 + __failure __msg("has no valid kptr") 193 + int BPF_PROG(test_invalid_nested_array, struct task_struct *task, u64 clone_flags) 194 + { 195 + struct bpf_cpumask *local, *prev; 196 + 197 + local = create_cpumask(); 198 + if (!local) 199 + return 0; 200 + 201 + prev = bpf_kptr_xchg(&global_mask_nested_arr.d_1.d_2[CPUMASK_KPTR_FIELDS_MAX].mask, local); 202 + if (prev) { 203 + bpf_cpumask_release(prev); 204 + err = 3; 205 + return 0; 206 + } 202 207 203 208 return 0; 204 209 }
+76 -2
tools/testing/selftests/bpf/progs/cpumask_success.c
··· 31 31 struct kptr_nested_pair ptr_pairs[3]; 32 32 }; 33 33 34 + struct kptr_nested_deep_array_1_2 { 35 + int dummy; 36 + struct bpf_cpumask __kptr * mask[CPUMASK_KPTR_FIELDS_MAX]; 37 + }; 38 + 39 + struct kptr_nested_deep_array_1_1 { 40 + int dummy; 41 + struct kptr_nested_deep_array_1_2 d_2; 42 + }; 43 + 44 + struct kptr_nested_deep_array_1 { 45 + long dummy; 46 + struct kptr_nested_deep_array_1_1 d_1; 47 + }; 48 + 49 + struct kptr_nested_deep_array_2_2 { 50 + long dummy[2]; 51 + struct bpf_cpumask __kptr * mask; 52 + }; 53 + 54 + struct kptr_nested_deep_array_2_1 { 55 + int dummy; 56 + struct kptr_nested_deep_array_2_2 d_2[CPUMASK_KPTR_FIELDS_MAX]; 57 + }; 58 + 59 + struct kptr_nested_deep_array_2 { 60 + long dummy; 61 + struct kptr_nested_deep_array_2_1 d_1; 62 + }; 63 + 64 + struct kptr_nested_deep_array_3_2 { 65 + long dummy[2]; 66 + struct bpf_cpumask __kptr * mask; 67 + }; 68 + 69 + struct kptr_nested_deep_array_3_1 { 70 + int dummy; 71 + struct kptr_nested_deep_array_3_2 d_2; 72 + }; 73 + 74 + struct kptr_nested_deep_array_3 { 75 + long dummy; 76 + struct kptr_nested_deep_array_3_1 d_1[CPUMASK_KPTR_FIELDS_MAX]; 77 + }; 78 + 34 79 private(MASK) static struct bpf_cpumask __kptr * global_mask_array[2]; 35 80 private(MASK) static struct bpf_cpumask __kptr * global_mask_array_l2[2][1]; 36 81 private(MASK) static struct bpf_cpumask __kptr * global_mask_array_one[1]; 37 82 private(MASK) static struct kptr_nested global_mask_nested[2]; 38 83 private(MASK_DEEP) static struct kptr_nested_deep global_mask_nested_deep; 84 + private(MASK_1) static struct kptr_nested_deep_array_1 global_mask_nested_deep_array_1; 85 + private(MASK_2) static struct kptr_nested_deep_array_2 global_mask_nested_deep_array_2; 86 + private(MASK_3) static struct kptr_nested_deep_array_3 global_mask_nested_deep_array_3; 39 87 40 88 static bool is_test_task(void) 41 89 { ··· 591 543 goto err_exit; 592 544 } 593 545 594 - /* [<mask 0>, NULL] */ 595 - if (!*mask0 || *mask1) { 546 + /* [<mask 0>, *] */ 547 + if (!*mask0) { 596 548 err = 2; 549 + goto err_exit; 550 + } 551 + 552 + if (!mask1) 553 + goto err_exit; 554 + 555 + /* [*, NULL] */ 556 + if (*mask1) { 557 + err = 3; 597 558 goto err_exit; 598 559 } 599 560 ··· 685 628 if (r) 686 629 return r; 687 630 } 631 + return 0; 632 + } 633 + 634 + SEC("tp_btf/task_newtask") 635 + int BPF_PROG(test_global_mask_nested_deep_array_rcu, struct task_struct *task, u64 clone_flags) 636 + { 637 + int i; 638 + 639 + for (i = 0; i < CPUMASK_KPTR_FIELDS_MAX; i++) 640 + _global_mask_array_rcu(&global_mask_nested_deep_array_1.d_1.d_2.mask[i], NULL); 641 + 642 + for (i = 0; i < CPUMASK_KPTR_FIELDS_MAX; i++) 643 + _global_mask_array_rcu(&global_mask_nested_deep_array_2.d_1.d_2[i].mask, NULL); 644 + 645 + for (i = 0; i < CPUMASK_KPTR_FIELDS_MAX; i++) 646 + _global_mask_array_rcu(&global_mask_nested_deep_array_3.d_1[i].d_2.mask, NULL); 647 + 688 648 return 0; 689 649 } 690 650