udmabuf.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #define __EXPORTED_HEADERS__
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #include <fcntl.h>
  10. #include <malloc.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/syscall.h>
  13. #include <linux/memfd.h>
  14. #include <linux/udmabuf.h>
  15. #define TEST_PREFIX "drivers/dma-buf/udmabuf"
  16. #define NUM_PAGES 4
  17. static int memfd_create(const char *name, unsigned int flags)
  18. {
  19. return syscall(__NR_memfd_create, name, flags);
  20. }
  21. int main(int argc, char *argv[])
  22. {
  23. struct udmabuf_create create;
  24. int devfd, memfd, buf, ret;
  25. off_t size;
  26. void *mem;
  27. devfd = open("/dev/udmabuf", O_RDWR);
  28. if (devfd < 0) {
  29. printf("%s: [skip,no-udmabuf: Unable to access DMA buffer device file]\n",
  30. TEST_PREFIX);
  31. exit(77);
  32. }
  33. memfd = memfd_create("udmabuf-test", MFD_ALLOW_SEALING);
  34. if (memfd < 0) {
  35. printf("%s: [skip,no-memfd]\n", TEST_PREFIX);
  36. exit(77);
  37. }
  38. ret = fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK);
  39. if (ret < 0) {
  40. printf("%s: [skip,fcntl-add-seals]\n", TEST_PREFIX);
  41. exit(77);
  42. }
  43. size = getpagesize() * NUM_PAGES;
  44. ret = ftruncate(memfd, size);
  45. if (ret == -1) {
  46. printf("%s: [FAIL,memfd-truncate]\n", TEST_PREFIX);
  47. exit(1);
  48. }
  49. memset(&create, 0, sizeof(create));
  50. /* should fail (offset not page aligned) */
  51. create.memfd = memfd;
  52. create.offset = getpagesize()/2;
  53. create.size = getpagesize();
  54. buf = ioctl(devfd, UDMABUF_CREATE, &create);
  55. if (buf >= 0) {
  56. printf("%s: [FAIL,test-1]\n", TEST_PREFIX);
  57. exit(1);
  58. }
  59. /* should fail (size not multiple of page) */
  60. create.memfd = memfd;
  61. create.offset = 0;
  62. create.size = getpagesize()/2;
  63. buf = ioctl(devfd, UDMABUF_CREATE, &create);
  64. if (buf >= 0) {
  65. printf("%s: [FAIL,test-2]\n", TEST_PREFIX);
  66. exit(1);
  67. }
  68. /* should fail (not memfd) */
  69. create.memfd = 0; /* stdin */
  70. create.offset = 0;
  71. create.size = size;
  72. buf = ioctl(devfd, UDMABUF_CREATE, &create);
  73. if (buf >= 0) {
  74. printf("%s: [FAIL,test-3]\n", TEST_PREFIX);
  75. exit(1);
  76. }
  77. /* should work */
  78. create.memfd = memfd;
  79. create.offset = 0;
  80. create.size = size;
  81. buf = ioctl(devfd, UDMABUF_CREATE, &create);
  82. if (buf < 0) {
  83. printf("%s: [FAIL,test-4]\n", TEST_PREFIX);
  84. exit(1);
  85. }
  86. fprintf(stderr, "%s: ok\n", TEST_PREFIX);
  87. close(buf);
  88. close(memfd);
  89. close(devfd);
  90. return 0;
  91. }