Skip to content

Commit 7baa308

Browse files
tweejKernel Patches Daemon
authored andcommitted
selftests/bpf: Add test for truncated dmabuf_iter reads
If many dmabufs are present, reads of the dmabuf iterator can be truncated at PAGE_SIZE or user buffer size boundaries before the fix in "selftests/bpf: Add test for open coded dmabuf_iter". Add a test to confirm truncation does not occur. Signed-off-by: T.J. Mercier <[email protected]>
1 parent 82266ce commit 7baa308

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,10 @@ static int create_udmabuf(void)
7373
return -1;
7474
}
7575

76-
static int create_sys_heap_dmabuf(void)
76+
static int create_sys_heap_dmabuf(size_t bytes)
7777
{
78-
sysheap_test_buffer_size = 20 * getpagesize();
79-
8078
struct dma_heap_allocation_data data = {
81-
.len = sysheap_test_buffer_size,
79+
.len = bytes,
8280
.fd = 0,
8381
.fd_flags = O_RDWR | O_CLOEXEC,
8482
.heap_flags = 0,
@@ -110,7 +108,9 @@ static int create_sys_heap_dmabuf(void)
110108
static int create_test_buffers(void)
111109
{
112110
udmabuf = create_udmabuf();
113-
sysheap_dmabuf = create_sys_heap_dmabuf();
111+
112+
sysheap_test_buffer_size = 20 * getpagesize();
113+
sysheap_dmabuf = create_sys_heap_dmabuf(sysheap_test_buffer_size);
114114

115115
if (udmabuf < 0 || sysheap_dmabuf < 0)
116116
return -1;
@@ -219,6 +219,26 @@ 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_lots_of_buffers(struct dmabuf_iter *skel)
223+
{
224+
int iter_fd;
225+
char buf[1024];
226+
size_t total_bytes_read = 0;
227+
ssize_t bytes_read;
228+
229+
iter_fd = bpf_iter_create(bpf_link__fd(skel->links.dmabuf_collector));
230+
if (!ASSERT_OK_FD(iter_fd, "iter_create"))
231+
return;
232+
233+
while ((bytes_read = read(iter_fd, buf, sizeof(buf))) > 0)
234+
total_bytes_read += bytes_read;
235+
236+
ASSERT_GT(total_bytes_read, getpagesize(), "total_bytes_read");
237+
238+
close(iter_fd);
239+
}
240+
241+
222242
static void subtest_dmabuf_iter_check_open_coded(struct dmabuf_iter *skel, int map_fd)
223243
{
224244
LIBBPF_OPTS(bpf_test_run_opts, topts);
@@ -275,6 +295,23 @@ void test_dmabuf_iter(void)
275295
subtest_dmabuf_iter_check_no_infinite_reads(skel);
276296
if (test__start_subtest("default_iter"))
277297
subtest_dmabuf_iter_check_default_iter(skel);
298+
if (test__start_subtest("lots_of_buffers")) {
299+
size_t NUM_BUFS = 100;
300+
int buffers[NUM_BUFS];
301+
int i;
302+
303+
for (i = 0; i < NUM_BUFS; ++i) {
304+
buffers[i] = create_sys_heap_dmabuf(getpagesize());
305+
if (!ASSERT_OK_FD(buffers[i], "dmabuf_fd"))
306+
goto cleanup_bufs;
307+
}
308+
309+
subtest_dmabuf_iter_check_lots_of_buffers(skel);
310+
311+
cleanup_bufs:
312+
for (--i; i >= 0; --i)
313+
close(buffers[i]);
314+
}
278315
if (test__start_subtest("open_coded"))
279316
subtest_dmabuf_iter_check_open_coded(skel, map_fd);
280317

0 commit comments

Comments
 (0)