Skip to content

Commit dd04bdf

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 873f6f8 commit dd04bdf

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-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,31 @@ 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+
/* To trigger the open coded iterator attached to the syscall */
74+
syncfs(0);
75+
76+
/* It should be same as we've seen from the explicit iterator */
77+
ASSERT_EQ(skel->bss->open_coded_seen, skel->bss->kmem_cache_seen, "open_code_seen_eq");
78+
}
79+
7180
void test_kmem_cache_iter(void)
7281
{
73-
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
7482
struct kmem_cache_iter *skel = NULL;
75-
union bpf_iter_link_info linfo = {};
76-
struct bpf_link *link;
7783
char buf[256];
7884
int iter_fd;
7985

8086
skel = kmem_cache_iter__open_and_load();
8187
if (!ASSERT_OK_PTR(skel, "kmem_cache_iter__open_and_load"))
8288
return;
8389

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"))
90+
if (!ASSERT_OK(kmem_cache_iter__attach(skel), "skel_attach"))
8991
goto destroy;
9092

91-
iter_fd = bpf_iter_create(bpf_link__fd(link));
93+
iter_fd = bpf_iter_create(bpf_link__fd(skel->links.slab_info_collector));
9294
if (!ASSERT_GE(iter_fd, 0, "iter_create"))
93-
goto free_link;
95+
goto detach;
9496

9597
memset(buf, 0, sizeof(buf));
9698
while (read(iter_fd, buf, sizeof(buf) > 0)) {
@@ -105,11 +107,13 @@ void test_kmem_cache_iter(void)
105107
subtest_kmem_cache_iter_check_task_struct(skel);
106108
if (test__start_subtest("check_slabinfo"))
107109
subtest_kmem_cache_iter_check_slabinfo(skel);
110+
if (test__start_subtest("open_coded_iter"))
111+
subtest_kmem_cache_iter_open_coded(skel);
108112

109113
close(iter_fd);
110114

111-
free_link:
112-
bpf_link__destroy(link);
115+
detach:
116+
kmem_cache_iter__detach(skel);
113117
destroy:
114118
kmem_cache_iter__destroy(skel);
115119
}

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

Lines changed: 24 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

@@ -33,6 +35,7 @@ extern struct kmem_cache *bpf_get_kmem_cache(u64 addr) __ksym;
3335
/* Result, will be checked by userspace */
3436
int task_struct_found;
3537
int kmem_cache_seen;
38+
int open_coded_seen;
3639

3740
SEC("iter/kmem_cache")
3841
int slab_info_collector(struct bpf_iter__kmem_cache *ctx)
@@ -85,3 +88,24 @@ int BPF_PROG(check_task_struct)
8588
task_struct_found = -2;
8689
return 0;
8790
}
91+
92+
SEC("fentry.s/" SYS_PREFIX "sys_syncfs")
93+
int open_coded_iter(const void *ctx)
94+
{
95+
struct kmem_cache *s;
96+
97+
bpf_for_each(kmem_cache, s) {
98+
struct kmem_cache_result *r;
99+
int idx = open_coded_seen;
100+
101+
r = bpf_map_lookup_elem(&slab_result, &idx);
102+
if (r == NULL)
103+
break;
104+
105+
open_coded_seen++;
106+
107+
if (r->obj_size != s->size)
108+
break;
109+
}
110+
return 0;
111+
}

0 commit comments

Comments
 (0)