Skip to content

Commit 9eff81c

Browse files
T.J. MercierKernel Patches Daemon
T.J. Mercier
authored and
Kernel Patches Daemon
committed
selftests/bpf: Add test for open coded dmabuf_iter
Use the same test buffers as the traditional iterator and a new BPF map to verify the test buffers can be found with the open coded dmabuf iterator. Signed-off-by: T.J. Mercier <[email protected]> Acked-by: Christian König <[email protected]> Acked-by: Song Liu <[email protected]>
1 parent ff9a337 commit 9eff81c

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

tools/testing/selftests/bpf/bpf_experimental.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,4 +591,9 @@ extern int bpf_iter_kmem_cache_new(struct bpf_iter_kmem_cache *it) __weak __ksym
591591
extern struct kmem_cache *bpf_iter_kmem_cache_next(struct bpf_iter_kmem_cache *it) __weak __ksym;
592592
extern void bpf_iter_kmem_cache_destroy(struct bpf_iter_kmem_cache *it) __weak __ksym;
593593

594+
struct bpf_iter_dmabuf;
595+
extern int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it) __weak __ksym;
596+
extern struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it) __weak __ksym;
597+
extern void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it) __weak __ksym;
598+
594599
#endif

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,52 @@ static void subtest_dmabuf_iter_check_default_iter(struct dmabuf_iter *skel)
219219
close(iter_fd);
220220
}
221221

222+
static void subtest_dmabuf_iter_check_open_coded(struct dmabuf_iter *skel, int map_fd)
223+
{
224+
LIBBPF_OPTS(bpf_test_run_opts, topts);
225+
char key[DMA_BUF_NAME_LEN];
226+
int err, fd;
227+
bool found;
228+
229+
/* No need to attach it, just run it directly */
230+
fd = bpf_program__fd(skel->progs.iter_dmabuf_for_each);
231+
232+
err = bpf_prog_test_run_opts(fd, &topts);
233+
if (!ASSERT_OK(err, "test_run_opts err"))
234+
return;
235+
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
236+
return;
237+
238+
if (!ASSERT_OK(bpf_map_get_next_key(map_fd, NULL, key), "get next key"))
239+
return;
240+
241+
do {
242+
ASSERT_OK(bpf_map_lookup_elem(map_fd, key, &found), "lookup");
243+
ASSERT_TRUE(found, "found test buffer");
244+
} while (bpf_map_get_next_key(map_fd, key, key));
245+
}
246+
222247
void test_dmabuf_iter(void)
223248
{
224249
struct dmabuf_iter *skel = NULL;
250+
int map_fd;
251+
const bool f = false;
225252

226253
skel = dmabuf_iter__open_and_load();
227254
if (!ASSERT_OK_PTR(skel, "dmabuf_iter__open_and_load"))
228255
return;
229256

257+
map_fd = bpf_map__fd(skel->maps.testbuf_hash);
258+
if (!ASSERT_OK_FD(map_fd, "map_fd"))
259+
goto destroy_skel;
260+
261+
if (!ASSERT_OK(bpf_map_update_elem(map_fd, udmabuf_test_buffer_name, &f, BPF_ANY),
262+
"insert udmabuf"))
263+
goto destroy_skel;
264+
if (!ASSERT_OK(bpf_map_update_elem(map_fd, sysheap_test_buffer_name, &f, BPF_ANY),
265+
"insert sysheap buffer"))
266+
goto destroy_skel;
267+
230268
if (!ASSERT_OK(create_test_buffers(), "create_test_buffers"))
231269
goto destroy;
232270

@@ -237,8 +275,11 @@ void test_dmabuf_iter(void)
237275
subtest_dmabuf_iter_check_no_infinite_reads(skel);
238276
if (test__start_subtest("default_iter"))
239277
subtest_dmabuf_iter_check_default_iter(skel);
278+
if (test__start_subtest("open_coded"))
279+
subtest_dmabuf_iter_check_open_coded(skel, map_fd);
240280

241281
destroy:
242282
destroy_test_buffers();
283+
destroy_skel:
243284
dmabuf_iter__destroy(skel);
244285
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99

1010
char _license[] SEC("license") = "GPL";
1111

12+
struct {
13+
__uint(type, BPF_MAP_TYPE_HASH);
14+
__uint(key_size, DMA_BUF_NAME_LEN);
15+
__type(value, bool);
16+
__uint(max_entries, 5);
17+
} testbuf_hash SEC(".maps");
18+
1219
/*
1320
* Fields output by this iterator are delimited by newlines. Convert any
1421
* newlines in user-provided printed strings to spaces.
@@ -51,3 +58,34 @@ int dmabuf_collector(struct bpf_iter__dmabuf *ctx)
5158
BPF_SEQ_PRINTF(seq, "%lu\n%llu\n%s\n%s\n", inode, size, name, exporter);
5259
return 0;
5360
}
61+
62+
SEC("syscall")
63+
int iter_dmabuf_for_each(const void *ctx)
64+
{
65+
struct dma_buf *d;
66+
67+
bpf_for_each(dmabuf, d) {
68+
char name[DMA_BUF_NAME_LEN];
69+
const char *pname;
70+
bool *found;
71+
72+
if (bpf_core_read(&pname, sizeof(pname), &d->name))
73+
return 1;
74+
75+
/* Buffers are not required to be named */
76+
if (!pname)
77+
continue;
78+
79+
if (bpf_probe_read_kernel(name, sizeof(name), pname))
80+
return 1;
81+
82+
found = bpf_map_lookup_elem(&testbuf_hash, name);
83+
if (found) {
84+
bool t = true;
85+
86+
bpf_map_update_elem(&testbuf_hash, name, &t, BPF_EXIST);
87+
}
88+
}
89+
90+
return 0;
91+
}

0 commit comments

Comments
 (0)