selftests/bpf: Add a test for objects stuck in free_by_rcu_ttrace

Delete all elements of BPF_F_NO_PREALLOC hash map in one batch. The first
free_bulk() starts RCU tasks trace GP and the rest of the elements are
freed while it's in flight. Wait for call_rcu_ttrace_in_progress to clear
in bpf_mem_cache of every cpu and check that free_by_rcu_ttrace and
waiting_for_gp_ttrace lists are empty.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20260930095920.601738-4-alexei.starovoitov@gmail.com
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
This commit is contained in:
Alexei Starovoitov 2026-09-30 09:59:20 +00:00 committed by Kumar Kartikeya Dwivedi
parent fbd97dd1d3
commit aeb709c767
No known key found for this signature in database
GPG Key ID: 472D377B63542F83
2 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,60 @@
// SPDX-License-Identifier: GPL-2.0
#include <test_progs.h>
#include "bpf_ma_ttrace.skel.h"
#define NR_ELEMS 4096
/*
* The first free_bulk() starts RCU tasks trace GP. The rest of the elements are
* deleted while it's in flight. They should be freed without further alloc or
* free from this map.
*/
void test_bpf_ma_ttrace(void)
{
LIBBPF_OPTS(bpf_test_run_opts, opts);
struct bpf_ma_ttrace *skel;
__u32 cnt = NR_ELEMS;
long *vals = NULL;
int *keys = NULL;
int i, err, fd, nr_cpus;
skel = bpf_ma_ttrace__open_and_load();
if (!ASSERT_OK_PTR(skel, "open_and_load"))
return;
nr_cpus = libbpf_num_possible_cpus();
if (!ASSERT_GT(nr_cpus, 0, "nr_cpus"))
goto out;
skel->bss->nr_cpus = nr_cpus;
keys = calloc(NR_ELEMS, sizeof(*keys));
vals = calloc(NR_ELEMS, sizeof(*vals));
if (!ASSERT_OK_PTR(keys, "keys") || !ASSERT_OK_PTR(vals, "vals"))
goto out;
for (i = 0; i < NR_ELEMS; i++)
keys[i] = i;
fd = bpf_map__fd(skel->maps.htab);
err = bpf_map_update_batch(fd, keys, vals, &cnt, NULL);
if (!ASSERT_OK(err, "update_batch") || !ASSERT_EQ(cnt, NR_ELEMS, "update_cnt"))
goto out;
err = bpf_map_delete_batch(fd, keys, &cnt, NULL);
if (!ASSERT_OK(err, "delete_batch") || !ASSERT_EQ(cnt, NR_ELEMS, "delete_cnt"))
goto out;
/* Wait for all __free_rcu() callbacks to finish */
for (i = 0; i < 300; i++) {
err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.check_ttrace), &opts);
if (!ASSERT_OK(err, "test_run") || !ASSERT_OK(opts.retval, "retval"))
goto out;
if (!skel->bss->in_progress)
break;
usleep(100000);
}
ASSERT_EQ(skel->bss->nr_caches, nr_cpus, "nr_caches");
ASSERT_EQ(skel->bss->in_progress, 0, "in_progress");
ASSERT_EQ(skel->bss->not_freed, 0, "not_freed");
out:
free(keys);
free(vals);
bpf_ma_ttrace__destroy(skel);
}

View File

@ -0,0 +1,50 @@
// SPDX-License-Identifier: GPL-2.0
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include "bpf_kfuncs.h"
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(map_flags, BPF_F_NO_PREALLOC);
__uint(max_entries, 4096);
__type(key, int);
__type(value, long);
} htab SEC(".maps");
extern const void __per_cpu_offset __ksym;
int nr_cpus;
int nr_caches;
int in_progress;
int not_freed;
/* Look at bpf_mem_cache of every cpu that htab allocates its elements from */
SEC("syscall")
int check_ttrace(void *ctx)
{
struct bpf_htab *h = bpf_core_cast(&htab, struct bpf_htab);
unsigned long cache = (unsigned long)BPF_CORE_READ(h, ma.cache);
const unsigned long *offsets = &__per_cpu_offset;
struct bpf_mem_cache *c;
unsigned long off;
int cpu;
nr_caches = 0;
in_progress = 0;
not_freed = 0;
bpf_for(cpu, 0, nr_cpus) {
if (bpf_probe_read_kernel(&off, sizeof(off), offsets + cpu))
return -1;
c = bpf_core_cast((void *)(cache + off), struct bpf_mem_cache);
if (c->unit_size)
nr_caches++;
if (c->call_rcu_ttrace_in_progress.counter)
in_progress++;
if (c->free_by_rcu_ttrace.first || c->waiting_for_gp_ttrace.first)
not_freed++;
}
return 0;
}
char _license[] SEC("license") = "GPL";