Skip to content

Commit 3f2d03a

Browse files
kkdwvdtheihor
authored andcommitted
selftests/bpf: Exercise kptr map update lifetime
Add focused map_kptr coverage for BPF-side map updates that touch values containing referenced kptrs. The new syscall programs stash the testmod refcounted object in an array map, a preallocated hash map, and a no-prealloc hash map, then update the same map from BPF. The refcount must remain elevated after the update, while the userspace runner destroys the skeleton and reuses the existing refcount wait to confirm map teardown releases the kptr. Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
1 parent 7c8ba82 commit 3f2d03a

2 files changed

Lines changed: 142 additions & 3 deletions

File tree

tools/testing/selftests/bpf/prog_tests/map_kptr.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,68 @@ static void wait_for_map_release(void)
143143
map_kptr__destroy(skel);
144144
}
145145

146+
enum map_update_kptr_case {
147+
MAP_UPDATE_KPTR_ARRAY,
148+
MAP_UPDATE_KPTR_HASH,
149+
MAP_UPDATE_KPTR_HASH_MALLOC,
150+
};
151+
152+
static struct bpf_program *map_update_kptr_prog(struct map_kptr *skel,
153+
enum map_update_kptr_case test)
154+
{
155+
switch (test) {
156+
case MAP_UPDATE_KPTR_ARRAY:
157+
return skel->progs.test_array_map_update_kptr;
158+
case MAP_UPDATE_KPTR_HASH:
159+
return skel->progs.test_hash_map_update_kptr;
160+
case MAP_UPDATE_KPTR_HASH_MALLOC:
161+
return skel->progs.test_hash_malloc_map_update_kptr;
162+
}
163+
164+
return NULL;
165+
}
166+
167+
static void test_map_update_kptr(enum map_update_kptr_case test)
168+
{
169+
LIBBPF_OPTS(bpf_test_run_opts, opts);
170+
struct map_kptr *skel;
171+
struct bpf_program *prog;
172+
int ret;
173+
174+
skel = map_kptr__open_and_load();
175+
if (!ASSERT_OK_PTR(skel, "map_kptr__open_and_load"))
176+
return;
177+
178+
prog = map_update_kptr_prog(skel, test);
179+
if (!ASSERT_OK_PTR(prog, "map_update_kptr_prog"))
180+
goto out;
181+
182+
ret = bpf_prog_test_run_opts(bpf_program__fd(prog), &opts);
183+
if (!ASSERT_OK(ret, "map_update_kptr"))
184+
goto out;
185+
if (!ASSERT_OK(opts.retval, "map_update_kptr retval"))
186+
goto out;
187+
188+
ASSERT_EQ(skel->bss->num_of_refs, 3, "refs_after_update");
189+
190+
out:
191+
map_kptr__destroy(skel);
192+
wait_for_map_release();
193+
}
194+
146195
void serial_test_map_kptr(void)
147196
{
148197
struct rcu_tasks_trace_gp *skel;
149198

150199
RUN_TESTS(map_kptr_fail);
151200

201+
if (test__start_subtest("update_array_map_kptr"))
202+
test_map_update_kptr(MAP_UPDATE_KPTR_ARRAY);
203+
if (test__start_subtest("update_hash_map_kptr"))
204+
test_map_update_kptr(MAP_UPDATE_KPTR_HASH);
205+
if (test__start_subtest("update_hash_malloc_map_kptr"))
206+
test_map_update_kptr(MAP_UPDATE_KPTR_HASH_MALLOC);
207+
152208
skel = rcu_tasks_trace_gp__open_and_load();
153209
if (!ASSERT_OK_PTR(skel, "rcu_tasks_trace_gp__open_and_load"))
154210
return;

tools/testing/selftests/bpf/progs/map_kptr.c

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,7 @@ int test_map_kptr_ref3(struct __sk_buff *ctx)
489489

490490
int num_of_refs;
491491

492-
SEC("syscall")
493-
int count_ref(void *ctx)
492+
static __always_inline int read_ref_count(void)
494493
{
495494
struct prog_test_ref_kfunc *p;
496495
unsigned long arg = 0;
@@ -500,11 +499,95 @@ int count_ref(void *ctx)
500499
return 1;
501500

502501
num_of_refs = p->cnt.refs.counter;
503-
504502
bpf_kfunc_call_test_release(p);
505503
return 0;
506504
}
507505

506+
SEC("syscall")
507+
int count_ref(void *ctx)
508+
{
509+
return read_ref_count();
510+
}
511+
512+
static __always_inline int stash_ref_ptr(struct map_value *v)
513+
{
514+
struct prog_test_ref_kfunc *p, *old;
515+
unsigned long arg = 0;
516+
517+
p = bpf_kfunc_call_test_acquire(&arg);
518+
if (!p)
519+
return 1;
520+
521+
old = bpf_kptr_xchg(&v->ref_ptr, p);
522+
if (old) {
523+
bpf_kfunc_call_test_release(old);
524+
old = bpf_kptr_xchg(&v->ref_ptr, NULL);
525+
if (old)
526+
bpf_kfunc_call_test_release(old);
527+
return 2;
528+
}
529+
return 0;
530+
}
531+
532+
static __always_inline int check_refs(int expected)
533+
{
534+
int ret;
535+
536+
ret = read_ref_count();
537+
if (ret)
538+
return ret;
539+
return num_of_refs == expected ? 0 : 3;
540+
}
541+
542+
SEC("syscall")
543+
int test_array_map_update_kptr(void *ctx)
544+
{
545+
struct map_value init = {}, *v;
546+
int key = 0, ret;
547+
548+
v = bpf_map_lookup_elem(&array_map, &key);
549+
if (!v)
550+
return 1;
551+
ret = stash_ref_ptr(v);
552+
if (ret)
553+
return ret;
554+
ret = check_refs(3);
555+
if (ret)
556+
return ret;
557+
ret = bpf_map_update_elem(&array_map, &key, &init, BPF_EXIST);
558+
if (ret)
559+
return 4;
560+
return check_refs(3);
561+
}
562+
563+
#define DEFINE_HASH_UPDATE_KPTR_TEST(name, map) \
564+
SEC("syscall") \
565+
int name(void *ctx) \
566+
{ \
567+
struct map_value init = {}, *v; \
568+
int key = 0, ret; \
569+
\
570+
ret = bpf_map_update_elem(&map, &key, &init, BPF_NOEXIST); \
571+
if (ret) \
572+
return 1; \
573+
v = bpf_map_lookup_elem(&map, &key); \
574+
if (!v) \
575+
return 2; \
576+
ret = stash_ref_ptr(v); \
577+
if (ret) \
578+
return ret; \
579+
ret = check_refs(3); \
580+
if (ret) \
581+
return ret; \
582+
ret = bpf_map_update_elem(&map, &key, &init, BPF_EXIST); \
583+
if (ret) \
584+
return 4; \
585+
return check_refs(3); \
586+
}
587+
588+
DEFINE_HASH_UPDATE_KPTR_TEST(test_hash_map_update_kptr, hash_map)
589+
DEFINE_HASH_UPDATE_KPTR_TEST(test_hash_malloc_map_update_kptr, hash_malloc_map)
590+
508591
SEC("syscall")
509592
int test_ls_map_kptr_ref1(void *ctx)
510593
{

0 commit comments

Comments
 (0)