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

selftests/bpf: Test bpf_kptr_xchg stashing of bpf_rb_root

There was some confusion amongst Meta sched_ext folks regarding whether
stashing bpf_rb_root - the tree itself, rather than a single node - was
supported. This patch adds a small test which demonstrates this
functionality: a local kptr with rb_root is created, a node is created
and added to the tree, then the tree is kptr_xchg'd into a mapval.

Signed-off-by: Dave Marchevsky <davemarchevsky@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/bpf/20231204211722.571346-1-davemarchevsky@fb.com

authored by

Dave Marchevsky and committed by
Daniel Borkmann
1b4c7e20 ce3c49da

+76
+23
tools/testing/selftests/bpf/prog_tests/local_kptr_stash.c
··· 48 48 local_kptr_stash__destroy(skel); 49 49 } 50 50 51 + static void test_local_kptr_stash_local_with_root(void) 52 + { 53 + LIBBPF_OPTS(bpf_test_run_opts, opts, 54 + .data_in = &pkt_v4, 55 + .data_size_in = sizeof(pkt_v4), 56 + .repeat = 1, 57 + ); 58 + struct local_kptr_stash *skel; 59 + int ret; 60 + 61 + skel = local_kptr_stash__open_and_load(); 62 + if (!ASSERT_OK_PTR(skel, "local_kptr_stash__open_and_load")) 63 + return; 64 + 65 + ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.stash_local_with_root), &opts); 66 + ASSERT_OK(ret, "local_kptr_stash_add_local_with_root run"); 67 + ASSERT_OK(opts.retval, "local_kptr_stash_add_local_with_root retval"); 68 + 69 + local_kptr_stash__destroy(skel); 70 + } 71 + 51 72 static void test_local_kptr_stash_unstash(void) 52 73 { 53 74 LIBBPF_OPTS(bpf_test_run_opts, opts, ··· 136 115 test_local_kptr_stash_simple(); 137 116 if (test__start_subtest("local_kptr_stash_plain")) 138 117 test_local_kptr_stash_plain(); 118 + if (test__start_subtest("local_kptr_stash_local_with_root")) 119 + test_local_kptr_stash_local_with_root(); 139 120 if (test__start_subtest("local_kptr_stash_unstash")) 140 121 test_local_kptr_stash_unstash(); 141 122 if (test__start_subtest("refcount_acquire_without_unstash"))
+53
tools/testing/selftests/bpf/progs/local_kptr_stash.c
··· 37 37 long data; 38 38 }; 39 39 40 + struct local_with_root { 41 + long key; 42 + struct bpf_spin_lock l; 43 + struct bpf_rb_root r __contains(node_data, node); 44 + }; 45 + 40 46 struct map_value { 41 47 struct prog_test_ref_kfunc *not_kptr; 42 48 struct prog_test_ref_kfunc __kptr *val; 43 49 struct node_data __kptr *node; 44 50 struct plain_local __kptr *plain; 51 + struct local_with_root __kptr *local_root; 45 52 }; 46 53 47 54 /* This is necessary so that LLVM generates BTF for node_data struct ··· 71 64 __type(value, struct map_value); 72 65 __uint(max_entries, 2); 73 66 } some_nodes SEC(".maps"); 67 + 68 + static bool less(struct bpf_rb_node *a, const struct bpf_rb_node *b) 69 + { 70 + struct node_data *node_a; 71 + struct node_data *node_b; 72 + 73 + node_a = container_of(a, struct node_data, node); 74 + node_b = container_of(b, struct node_data, node); 75 + 76 + return node_a->key < node_b->key; 77 + } 74 78 75 79 static int create_and_stash(int idx, int val) 76 80 { ··· 128 110 res = bpf_kptr_xchg(&mapval->plain, res); 129 111 if (res) 130 112 bpf_obj_drop(res); 113 + return 0; 114 + } 115 + 116 + SEC("tc") 117 + long stash_local_with_root(void *ctx) 118 + { 119 + struct local_with_root *res; 120 + struct map_value *mapval; 121 + struct node_data *n; 122 + int idx = 0; 123 + 124 + mapval = bpf_map_lookup_elem(&some_nodes, &idx); 125 + if (!mapval) 126 + return 1; 127 + 128 + res = bpf_obj_new(typeof(*res)); 129 + if (!res) 130 + return 2; 131 + res->key = 41; 132 + 133 + n = bpf_obj_new(typeof(*n)); 134 + if (!n) { 135 + bpf_obj_drop(res); 136 + return 3; 137 + } 138 + 139 + bpf_spin_lock(&res->l); 140 + bpf_rbtree_add(&res->r, &n->node, less); 141 + bpf_spin_unlock(&res->l); 142 + 143 + res = bpf_kptr_xchg(&mapval->local_root, res); 144 + if (res) { 145 + bpf_obj_drop(res); 146 + return 4; 147 + } 131 148 return 0; 132 149 } 133 150