Skip to content

Commit 1d317c1

Browse files
johnstultz-workshuahkh
authored andcommitted
kselftests: dmabuf-heaps: Add extra checking that allocated buffers are zeroed
Add a check to validate that buffers allocated from the heaps are properly zeroed before being given to userland. It is done by allocating a number of buffers, and filling them with a nonzero pattern, then closing and reallocating more buffers and checking that they are all properly zeroed. This is helpful to validate any cached buffers are zeroed before being given back out. Cc: Shuah Khan <[email protected]> Cc: Brian Starkey <[email protected]> Cc: Sumit Semwal <[email protected]> Cc: Laura Abbott <[email protected]> Cc: Hridya Valsaraju <[email protected]> Cc: Suren Baghdasaryan <[email protected]> Cc: Sandeep Patil <[email protected]> Cc: Daniel Mentz <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Signed-off-by: John Stultz <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 06fc1aa commit 1d317c1

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,84 @@ static int test_alloc_and_import(char *heap_name)
218218
return ret;
219219
}
220220

221+
static int test_alloc_zeroed(char *heap_name, size_t size)
222+
{
223+
int heap_fd = -1, dmabuf_fd[32];
224+
int i, j, ret;
225+
void *p = NULL;
226+
char *c;
227+
228+
printf(" Testing alloced %ldk buffers are zeroed: ", size / 1024);
229+
heap_fd = dmabuf_heap_open(heap_name);
230+
if (heap_fd < 0)
231+
return -1;
232+
233+
/* Allocate and fill a bunch of buffers */
234+
for (i = 0; i < 32; i++) {
235+
ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]);
236+
if (ret < 0) {
237+
printf("FAIL (Allocation (%i) failed)\n", i);
238+
goto out;
239+
}
240+
/* mmap and fill with simple pattern */
241+
p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0);
242+
if (p == MAP_FAILED) {
243+
printf("FAIL (mmap() failed!)\n");
244+
ret = -1;
245+
goto out;
246+
}
247+
dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_START);
248+
memset(p, 0xff, size);
249+
dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_END);
250+
munmap(p, size);
251+
}
252+
/* close them all */
253+
for (i = 0; i < 32; i++)
254+
close(dmabuf_fd[i]);
255+
256+
/* Allocate and validate all buffers are zeroed */
257+
for (i = 0; i < 32; i++) {
258+
ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]);
259+
if (ret < 0) {
260+
printf("FAIL (Allocation (%i) failed)\n", i);
261+
goto out;
262+
}
263+
264+
/* mmap and validate everything is zero */
265+
p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0);
266+
if (p == MAP_FAILED) {
267+
printf("FAIL (mmap() failed!)\n");
268+
ret = -1;
269+
goto out;
270+
}
271+
dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_START);
272+
c = (char *)p;
273+
for (j = 0; j < size; j++) {
274+
if (c[j] != 0) {
275+
printf("FAIL (Allocated buffer not zeroed @ %i)\n", j);
276+
break;
277+
}
278+
}
279+
dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_END);
280+
munmap(p, size);
281+
}
282+
/* close them all */
283+
for (i = 0; i < 32; i++)
284+
close(dmabuf_fd[i]);
285+
286+
close(heap_fd);
287+
printf("OK\n");
288+
return 0;
289+
290+
out:
291+
while (i > 0) {
292+
close(dmabuf_fd[i]);
293+
i--;
294+
}
295+
close(heap_fd);
296+
return ret;
297+
}
298+
221299
/* Test the ioctl version compatibility w/ a smaller structure then expected */
222300
static int dmabuf_heap_alloc_older(int fd, size_t len, unsigned int flags,
223301
int *dmabuf_fd)
@@ -386,6 +464,14 @@ int main(void)
386464
if (ret)
387465
break;
388466

467+
ret = test_alloc_zeroed(dir->d_name, 4 * 1024);
468+
if (ret)
469+
break;
470+
471+
ret = test_alloc_zeroed(dir->d_name, ONE_MEG);
472+
if (ret)
473+
break;
474+
389475
ret = test_alloc_compat(dir->d_name);
390476
if (ret)
391477
break;

0 commit comments

Comments
 (0)