Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <shuah@kernel.org>
Cc: Brian Starkey <brian.starkey@arm.com>
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: Laura Abbott <labbott@kernel.org>
Cc: Hridya Valsaraju <hridya@google.com>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Sandeep Patil <sspatil@google.com>
Cc: Daniel Mentz <danielmentz@google.com>
Cc: linux-media@vger.kernel.org
Cc: dri-devel@lists.freedesktop.org
Cc: linux-kselftest@vger.kernel.org
Signed-off-by: John Stultz <john.stultz@linaro.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

John Stultz and committed by
Shuah Khan
1d317c1c 06fc1aae

+86
+86
tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c
··· 218 218 return ret; 219 219 } 220 220 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 + 221 299 /* Test the ioctl version compatibility w/ a smaller structure then expected */ 222 300 static int dmabuf_heap_alloc_older(int fd, size_t len, unsigned int flags, 223 301 int *dmabuf_fd) ··· 461 383 printf("Testing heap: %s\n", dir->d_name); 462 384 printf("=======================================\n"); 463 385 ret = test_alloc_and_import(dir->d_name); 386 + if (ret) 387 + break; 388 + 389 + ret = test_alloc_zeroed(dir->d_name, 4 * 1024); 390 + if (ret) 391 + break; 392 + 393 + ret = test_alloc_zeroed(dir->d_name, ONE_MEG); 464 394 if (ret) 465 395 break; 466 396