Skip to content

Commit a3651f4

Browse files
tweejKernel Patches Daemon
authored andcommitted
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 3e66b15 commit a3651f4

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
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: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,18 @@ static int create_sys_heap_dmabuf(void)
106106
return -1;
107107
}
108108

109-
static int create_test_buffers(void)
109+
static int create_test_buffers(int map_fd)
110110
{
111+
bool f = false;
112+
111113
udmabuf = create_udmabuf();
112114
sysheap_dmabuf = create_sys_heap_dmabuf();
113115

114116
if (udmabuf < 0 || sysheap_dmabuf < 0)
115117
return -1;
116118

117-
return 0;
119+
return bpf_map_update_elem(map_fd, udmabuf_test_buffer_name, &f, BPF_ANY) ||
120+
bpf_map_update_elem(map_fd, sysheap_test_buffer_name, &f, BPF_ANY);
118121
}
119122

120123
static void destroy_test_buffers(void)
@@ -215,15 +218,45 @@ static void subtest_dmabuf_iter_check_default_iter(struct dmabuf_iter *skel)
215218
close(iter_fd);
216219
}
217220

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

222251
skel = dmabuf_iter__open_and_load();
223252
if (!ASSERT_OK_PTR(skel, "dmabuf_iter__open_and_load"))
224253
return;
225254

226-
if (!ASSERT_OK(create_test_buffers(), "create_test_buffers"))
255+
map_fd = bpf_map__fd(skel->maps.testbuf_hash);
256+
if (!ASSERT_OK_FD(map_fd, "map_fd"))
257+
goto destroy_skel;
258+
259+
if (!ASSERT_OK(create_test_buffers(map_fd), "create_test_buffers"))
227260
goto destroy;
228261

229262
if (!ASSERT_OK(dmabuf_iter__attach(skel), "skel_attach"))
@@ -233,8 +266,11 @@ void test_dmabuf_iter(void)
233266
subtest_dmabuf_iter_check_no_infinite_reads(skel);
234267
if (test__start_subtest("default_iter"))
235268
subtest_dmabuf_iter_check_default_iter(skel);
269+
if (test__start_subtest("open_coded"))
270+
subtest_dmabuf_iter_check_open_coded(skel, map_fd);
236271

237272
destroy:
238273
destroy_test_buffers();
274+
destroy_skel:
239275
dmabuf_iter__destroy(skel);
240276
}

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)