Skip to content

Commit 69012b1

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
selftests/bpf: Add test to verify no memleak when updating hash maps
Add two tests to verify that updating hash maps does not leak memory when BPF_KPTR_REF objects are involved. The test performs the following steps: 1. Call update_elem() to insert an initial value. 2. Use bpf_refcount_acquire() to increment the refcount. 3. Store the node pointer in the map value. 4. Add the node to a linked list. 5. Probe-read the refcount and verify it is *2*. 6. Call update_elem() again to trigger refcount decrement. 7. Verify that the field has been reset. Signed-off-by: Leon Hwang <[email protected]>
1 parent 1327520 commit 69012b1

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,96 @@ void test_refcounted_kptr_wrong_owner(void)
4444
ASSERT_OK(opts.retval, "rbtree_wrong_owner_remove_fail_a2 retval");
4545
refcounted_kptr__destroy(skel);
4646
}
47+
48+
static void test_refcnt_leak(void *values, size_t values_sz, u64 flags, bool lock_hash)
49+
{
50+
struct refcounted_kptr *skel;
51+
int ret, fd, key = 0;
52+
struct bpf_map *map;
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+
59+
skel = refcounted_kptr__open_and_load();
60+
if (!ASSERT_OK_PTR(skel, "refcounted_kptr__open_and_load"))
61+
return;
62+
63+
map = skel->maps.pcpu_hash;
64+
if (lock_hash)
65+
map = skel->maps.lock_hash;
66+
67+
ret = bpf_map__update_elem(map, &key, sizeof(key), values, values_sz, flags);
68+
if (!ASSERT_OK(ret, "bpf_map__update_elem first"))
69+
goto out;
70+
71+
fd = bpf_program__fd(skel->progs.pcpu_hash_refcount_leak);
72+
if (lock_hash)
73+
fd = bpf_program__fd(skel->progs.hash_lock_refcount_leak);
74+
75+
ret = bpf_prog_test_run_opts(fd, &opts);
76+
if (!ASSERT_OK(ret, "test_run_opts"))
77+
goto out;
78+
if (!ASSERT_EQ(opts.retval, 2, "retval refcount"))
79+
goto out;
80+
81+
ret = bpf_map__update_elem(map, &key, sizeof(key), values, values_sz, flags);
82+
if (!ASSERT_OK(ret, "bpf_map__update_elem second"))
83+
goto out;
84+
85+
fd = bpf_program__fd(skel->progs.check_pcpu_hash_refcount);
86+
if (lock_hash)
87+
fd = bpf_program__fd(skel->progs.check_hash_lock_refcount);
88+
89+
ret = bpf_prog_test_run_opts(fd, &opts);
90+
if (!ASSERT_OK(ret, "test_run_opts"))
91+
goto out;
92+
if (!ASSERT_EQ(opts.retval, 1, "retval"))
93+
goto out;
94+
95+
out:
96+
refcounted_kptr__destroy(skel);
97+
}
98+
99+
static void test_percpu_hash_refcount_leak(void)
100+
{
101+
size_t values_sz;
102+
u64 *values;
103+
int cpu_nr;
104+
105+
cpu_nr = libbpf_num_possible_cpus();
106+
if (!ASSERT_GT(cpu_nr, 0, "libbpf_num_possible_cpus"))
107+
return;
108+
109+
values = calloc(cpu_nr, sizeof(u64));
110+
if (!ASSERT_OK_PTR(values, "calloc values"))
111+
return;
112+
113+
values_sz = cpu_nr * sizeof(u64);
114+
memset(values, 0, values_sz);
115+
116+
test_refcnt_leak(values, values_sz, 0, false);
117+
118+
free(values);
119+
}
120+
121+
struct hash_lock_value {
122+
struct bpf_spin_lock lock;
123+
u64 node;
124+
};
125+
126+
static void test_hash_lock_refcount_leak(void)
127+
{
128+
struct hash_lock_value value = {};
129+
130+
test_refcnt_leak(&value, sizeof(value), BPF_F_LOCK, true);
131+
}
132+
133+
void test_refcount_leak(void)
134+
{
135+
if (test__start_subtest("percpu_hash_refcount_leak"))
136+
test_percpu_hash_refcount_leak();
137+
if (test__start_subtest("hash_lock_refcount_leak"))
138+
test_hash_lock_refcount_leak();
139+
}

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,4 +568,105 @@ int BPF_PROG(rbtree_sleepable_rcu_no_explicit_rcu_lock,
568568
return 0;
569569
}
570570

571+
static int __insert_in_list(struct bpf_list_head *head, struct bpf_spin_lock *lock,
572+
struct node_data __kptr **node)
573+
{
574+
struct node_data *n, *m;
575+
u32 refcnt;
576+
void *ref;
577+
578+
n = bpf_obj_new(typeof(*n));
579+
if (!n)
580+
return -1;
581+
582+
m = bpf_refcount_acquire(n);
583+
n = bpf_kptr_xchg(node, n);
584+
if (n) {
585+
bpf_obj_drop(n);
586+
bpf_obj_drop(m);
587+
return -2;
588+
}
589+
590+
bpf_spin_lock(lock);
591+
bpf_list_push_front(head, &m->l);
592+
ref = (void *) &m->ref;
593+
bpf_spin_unlock(lock);
594+
595+
bpf_probe_read_kernel(&refcnt, sizeof(refcnt), ref);
596+
return refcnt;
597+
}
598+
599+
static void *__lookup_map(void *map)
600+
{
601+
int key = 0;
602+
603+
return bpf_map_lookup_elem(map, &key);
604+
}
605+
606+
struct {
607+
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
608+
__type(key, int);
609+
__type(value, struct map_value);
610+
__uint(max_entries, 1);
611+
} pcpu_hash SEC(".maps");
612+
613+
SEC("tc")
614+
int pcpu_hash_refcount_leak(void *ctx)
615+
{
616+
struct map_value *v;
617+
618+
v = __lookup_map(&pcpu_hash);
619+
if (!v)
620+
return 0;
621+
622+
return __insert_in_list(&head, &lock, &v->node);
623+
}
624+
625+
SEC("tc")
626+
int check_pcpu_hash_refcount(void *ctx)
627+
{
628+
struct map_value *v;
629+
630+
v = __lookup_map(&pcpu_hash);
631+
return v && v->node == NULL;
632+
}
633+
634+
struct hash_lock_map_value {
635+
struct node_data __kptr *node;
636+
struct bpf_spin_lock lock;
637+
int value;
638+
};
639+
640+
struct {
641+
__uint(type, BPF_MAP_TYPE_HASH);
642+
__type(key, int);
643+
__type(value, struct hash_lock_map_value);
644+
__uint(max_entries, 1);
645+
} lock_hash SEC(".maps");
646+
647+
SEC("tc")
648+
int hash_lock_refcount_leak(void *ctx)
649+
{
650+
struct hash_lock_map_value *v;
651+
652+
v = __lookup_map(&lock_hash);
653+
if (!v)
654+
return 0;
655+
656+
bpf_spin_lock(&v->lock);
657+
v->value = 42;
658+
bpf_spin_unlock(&v->lock);
659+
660+
return __insert_in_list(&head, &lock, &v->node);
661+
}
662+
663+
SEC("tc")
664+
int check_hash_lock_refcount(void *ctx)
665+
{
666+
struct hash_lock_map_value *v;
667+
668+
v = __lookup_map(&lock_hash);
669+
return v && v->node == NULL;
670+
}
671+
571672
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)