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