Skip to content

Commit e5e4799

Browse files
namhyungAlexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: Add a test for open coded kmem_cache iter
The new subtest runs with bpf_prog_test_run_opts() as a syscall prog. 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]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 2e9a548 commit e5e4799

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-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: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,40 @@ 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+
LIBBPF_OPTS(bpf_test_run_opts, topts);
74+
int err, fd;
75+
76+
/* No need to attach it, just run it directly */
77+
fd = bpf_program__fd(skel->progs.open_coded_iter);
78+
79+
err = bpf_prog_test_run_opts(fd, &topts);
80+
if (!ASSERT_OK(err, "test_run_opts err"))
81+
return;
82+
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
83+
return;
84+
85+
/* It should be same as we've seen from the explicit iterator */
86+
ASSERT_EQ(skel->bss->open_coded_seen, skel->bss->kmem_cache_seen, "open_code_seen_eq");
87+
}
88+
7189
void test_kmem_cache_iter(void)
7290
{
73-
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
7491
struct kmem_cache_iter *skel = NULL;
75-
union bpf_iter_link_info linfo = {};
76-
struct bpf_link *link;
7792
char buf[256];
7893
int iter_fd;
7994

8095
skel = kmem_cache_iter__open_and_load();
8196
if (!ASSERT_OK_PTR(skel, "kmem_cache_iter__open_and_load"))
8297
return;
8398

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"))
99+
if (!ASSERT_OK(kmem_cache_iter__attach(skel), "skel_attach"))
89100
goto destroy;
90101

91-
iter_fd = bpf_iter_create(bpf_link__fd(link));
102+
iter_fd = bpf_iter_create(bpf_link__fd(skel->links.slab_info_collector));
92103
if (!ASSERT_GE(iter_fd, 0, "iter_create"))
93-
goto free_link;
104+
goto destroy;
94105

95106
memset(buf, 0, sizeof(buf));
96107
while (read(iter_fd, buf, sizeof(buf) > 0)) {
@@ -105,11 +116,11 @@ void test_kmem_cache_iter(void)
105116
subtest_kmem_cache_iter_check_task_struct(skel);
106117
if (test__start_subtest("check_slabinfo"))
107118
subtest_kmem_cache_iter_check_slabinfo(skel);
119+
if (test__start_subtest("open_coded_iter"))
120+
subtest_kmem_cache_iter_open_coded(skel);
108121

109122
close(iter_fd);
110123

111-
free_link:
112-
bpf_link__destroy(link);
113124
destroy:
114125
kmem_cache_iter__destroy(skel);
115126
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <vmlinux.h>
44
#include <bpf/bpf_helpers.h>
55
#include <bpf/bpf_tracing.h>
6+
#include "bpf_experimental.h"
67

78
char _license[] SEC("license") = "GPL";
89

@@ -32,6 +33,7 @@ extern struct kmem_cache *bpf_get_kmem_cache(u64 addr) __ksym;
3233
/* Result, will be checked by userspace */
3334
int task_struct_found;
3435
int kmem_cache_seen;
36+
int open_coded_seen;
3537

3638
SEC("iter/kmem_cache")
3739
int slab_info_collector(struct bpf_iter__kmem_cache *ctx)
@@ -84,3 +86,23 @@ int BPF_PROG(check_task_struct)
8486
task_struct_found = -2;
8587
return 0;
8688
}
89+
90+
SEC("syscall")
91+
int open_coded_iter(const void *ctx)
92+
{
93+
struct kmem_cache *s;
94+
95+
bpf_for_each(kmem_cache, s) {
96+
struct kmem_cache_result *r;
97+
98+
r = bpf_map_lookup_elem(&slab_result, &open_coded_seen);
99+
if (!r)
100+
break;
101+
102+
if (r->obj_size != s->size)
103+
break;
104+
105+
open_coded_seen++;
106+
}
107+
return 0;
108+
}

0 commit comments

Comments
 (0)