Skip to content

Commit 1e91efc

Browse files
namhyungKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
selftests/bpf: Add a test for open coded kmem_cache iter
The new subtest is attached to sleepable fentry of syncfs() syscall. It iterates the kmem_cache using bpf_for_each loop and count the number of entries. Finally it checks it with the number of entries from the regular iterator. $ ./vmtest.sh -- ./test_progs -t kmem_cache_iter ... #130/1 kmem_cache_iter/check_task_struct:OK #130/2 kmem_cache_iter/check_slabinfo:OK #130/3 kmem_cache_iter/open_coded_iter:OK #130 kmem_cache_iter:OK Summary: 1/3 PASSED, 0 SKIPPED, 0 FAILED Also simplify the code by using attach routine of the skeleton. Signed-off-by: Namhyung Kim <[email protected]>
1 parent 4355fed commit 1e91efc

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

tools/testing/selftests/bpf/bpf_experimental.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,4 +582,10 @@ extern int bpf_wq_set_callback_impl(struct bpf_wq *wq,
582582
unsigned int flags__k, void *aux__ign) __ksym;
583583
#define bpf_wq_set_callback(timer, cb, flags) \
584584
bpf_wq_set_callback_impl(timer, cb, flags, NULL)
585+
586+
struct bpf_iter_kmem_cache;
587+
extern int bpf_iter_kmem_cache_new(struct bpf_iter_kmem_cache *it) __weak __ksym;
588+
extern struct kmem_cache *bpf_iter_kmem_cache_next(struct bpf_iter_kmem_cache *it) __weak __ksym;
589+
extern void bpf_iter_kmem_cache_destroy(struct bpf_iter_kmem_cache *it) __weak __ksym;
590+
585591
#endif

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,33 @@ static void subtest_kmem_cache_iter_check_slabinfo(struct kmem_cache_iter *skel)
6868
fclose(fp);
6969
}
7070

71+
static void subtest_kmem_cache_iter_open_coded(struct kmem_cache_iter *skel)
72+
{
73+
skel->bss->tgid = getpid();
74+
75+
/* To trigger the open coded iterator attached to the syscall */
76+
syncfs(0);
77+
78+
/* It should be same as we've seen from the explicit iterator */
79+
ASSERT_EQ(skel->bss->open_coded_seen, skel->bss->kmem_cache_seen, "open_code_seen_eq");
80+
}
81+
7182
void test_kmem_cache_iter(void)
7283
{
73-
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
7484
struct kmem_cache_iter *skel = NULL;
75-
union bpf_iter_link_info linfo = {};
76-
struct bpf_link *link;
7785
char buf[256];
7886
int iter_fd;
7987

8088
skel = kmem_cache_iter__open_and_load();
8189
if (!ASSERT_OK_PTR(skel, "kmem_cache_iter__open_and_load"))
8290
return;
8391

84-
opts.link_info = &linfo;
85-
opts.link_info_len = sizeof(linfo);
86-
87-
link = bpf_program__attach_iter(skel->progs.slab_info_collector, &opts);
88-
if (!ASSERT_OK_PTR(link, "attach_iter"))
92+
if (!ASSERT_OK(kmem_cache_iter__attach(skel), "skel_attach"))
8993
goto destroy;
9094

91-
iter_fd = bpf_iter_create(bpf_link__fd(link));
95+
iter_fd = bpf_iter_create(bpf_link__fd(skel->links.slab_info_collector));
9296
if (!ASSERT_GE(iter_fd, 0, "iter_create"))
93-
goto free_link;
97+
goto destroy;
9498

9599
memset(buf, 0, sizeof(buf));
96100
while (read(iter_fd, buf, sizeof(buf) > 0)) {
@@ -105,11 +109,11 @@ void test_kmem_cache_iter(void)
105109
subtest_kmem_cache_iter_check_task_struct(skel);
106110
if (test__start_subtest("check_slabinfo"))
107111
subtest_kmem_cache_iter_check_slabinfo(skel);
112+
if (test__start_subtest("open_coded_iter"))
113+
subtest_kmem_cache_iter_open_coded(skel);
108114

109115
close(iter_fd);
110116

111-
free_link:
112-
bpf_link__destroy(link);
113117
destroy:
114118
kmem_cache_iter__destroy(skel);
115119
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/* Copyright (c) 2024 Google */
33

44
#include "bpf_iter.h"
5+
#include "bpf_experimental.h"
6+
#include "bpf_misc.h"
57
#include <bpf/bpf_helpers.h>
68
#include <bpf/bpf_tracing.h>
79

@@ -30,9 +32,12 @@ struct {
3032

3133
extern struct kmem_cache *bpf_get_kmem_cache(u64 addr) __ksym;
3234

35+
unsigned int tgid;
36+
3337
/* Result, will be checked by userspace */
3438
int task_struct_found;
3539
int kmem_cache_seen;
40+
int open_coded_seen;
3641

3742
SEC("iter/kmem_cache")
3843
int slab_info_collector(struct bpf_iter__kmem_cache *ctx)
@@ -85,3 +90,26 @@ int BPF_PROG(check_task_struct)
8590
task_struct_found = -2;
8691
return 0;
8792
}
93+
94+
SEC("fentry.s/" SYS_PREFIX "sys_syncfs")
95+
int open_coded_iter(const void *ctx)
96+
{
97+
struct kmem_cache *s;
98+
99+
if (tgid != bpf_get_current_pid_tgid() >> 32)
100+
return 0;
101+
102+
bpf_for_each(kmem_cache, s) {
103+
struct kmem_cache_result *r;
104+
105+
r = bpf_map_lookup_elem(&slab_result, &open_coded_seen);
106+
if (!r)
107+
break;
108+
109+
open_coded_seen++;
110+
111+
if (r->obj_size != s->size)
112+
break;
113+
}
114+
return 0;
115+
}

0 commit comments

Comments
 (0)