18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci#include <stdio.h> 38c2ecf20Sopenharmony_ci#include <stdlib.h> 48c2ecf20Sopenharmony_ci#include <unistd.h> 58c2ecf20Sopenharmony_ci#include <string.h> 68c2ecf20Sopenharmony_ci#include <errno.h> 78c2ecf20Sopenharmony_ci#include <linux/fcntl.h> 88c2ecf20Sopenharmony_ci#include <malloc.h> 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <sys/ioctl.h> 118c2ecf20Sopenharmony_ci#include <sys/syscall.h> 128c2ecf20Sopenharmony_ci#include <linux/memfd.h> 138c2ecf20Sopenharmony_ci#include <linux/udmabuf.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define TEST_PREFIX "drivers/dma-buf/udmabuf" 168c2ecf20Sopenharmony_ci#define NUM_PAGES 4 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_cistatic int memfd_create(const char *name, unsigned int flags) 198c2ecf20Sopenharmony_ci{ 208c2ecf20Sopenharmony_ci return syscall(__NR_memfd_create, name, flags); 218c2ecf20Sopenharmony_ci} 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ciint main(int argc, char *argv[]) 248c2ecf20Sopenharmony_ci{ 258c2ecf20Sopenharmony_ci struct udmabuf_create create; 268c2ecf20Sopenharmony_ci int devfd, memfd, buf, ret; 278c2ecf20Sopenharmony_ci off_t size; 288c2ecf20Sopenharmony_ci void *mem; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci devfd = open("/dev/udmabuf", O_RDWR); 318c2ecf20Sopenharmony_ci if (devfd < 0) { 328c2ecf20Sopenharmony_ci printf("%s: [skip,no-udmabuf]\n", TEST_PREFIX); 338c2ecf20Sopenharmony_ci exit(77); 348c2ecf20Sopenharmony_ci } 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci memfd = memfd_create("udmabuf-test", MFD_ALLOW_SEALING); 378c2ecf20Sopenharmony_ci if (memfd < 0) { 388c2ecf20Sopenharmony_ci printf("%s: [skip,no-memfd]\n", TEST_PREFIX); 398c2ecf20Sopenharmony_ci exit(77); 408c2ecf20Sopenharmony_ci } 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci ret = fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK); 438c2ecf20Sopenharmony_ci if (ret < 0) { 448c2ecf20Sopenharmony_ci printf("%s: [skip,fcntl-add-seals]\n", TEST_PREFIX); 458c2ecf20Sopenharmony_ci exit(77); 468c2ecf20Sopenharmony_ci } 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci size = getpagesize() * NUM_PAGES; 508c2ecf20Sopenharmony_ci ret = ftruncate(memfd, size); 518c2ecf20Sopenharmony_ci if (ret == -1) { 528c2ecf20Sopenharmony_ci printf("%s: [FAIL,memfd-truncate]\n", TEST_PREFIX); 538c2ecf20Sopenharmony_ci exit(1); 548c2ecf20Sopenharmony_ci } 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci memset(&create, 0, sizeof(create)); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci /* should fail (offset not page aligned) */ 598c2ecf20Sopenharmony_ci create.memfd = memfd; 608c2ecf20Sopenharmony_ci create.offset = getpagesize()/2; 618c2ecf20Sopenharmony_ci create.size = getpagesize(); 628c2ecf20Sopenharmony_ci buf = ioctl(devfd, UDMABUF_CREATE, &create); 638c2ecf20Sopenharmony_ci if (buf >= 0) { 648c2ecf20Sopenharmony_ci printf("%s: [FAIL,test-1]\n", TEST_PREFIX); 658c2ecf20Sopenharmony_ci exit(1); 668c2ecf20Sopenharmony_ci } 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci /* should fail (size not multiple of page) */ 698c2ecf20Sopenharmony_ci create.memfd = memfd; 708c2ecf20Sopenharmony_ci create.offset = 0; 718c2ecf20Sopenharmony_ci create.size = getpagesize()/2; 728c2ecf20Sopenharmony_ci buf = ioctl(devfd, UDMABUF_CREATE, &create); 738c2ecf20Sopenharmony_ci if (buf >= 0) { 748c2ecf20Sopenharmony_ci printf("%s: [FAIL,test-2]\n", TEST_PREFIX); 758c2ecf20Sopenharmony_ci exit(1); 768c2ecf20Sopenharmony_ci } 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci /* should fail (not memfd) */ 798c2ecf20Sopenharmony_ci create.memfd = 0; /* stdin */ 808c2ecf20Sopenharmony_ci create.offset = 0; 818c2ecf20Sopenharmony_ci create.size = size; 828c2ecf20Sopenharmony_ci buf = ioctl(devfd, UDMABUF_CREATE, &create); 838c2ecf20Sopenharmony_ci if (buf >= 0) { 848c2ecf20Sopenharmony_ci printf("%s: [FAIL,test-3]\n", TEST_PREFIX); 858c2ecf20Sopenharmony_ci exit(1); 868c2ecf20Sopenharmony_ci } 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci /* should work */ 898c2ecf20Sopenharmony_ci create.memfd = memfd; 908c2ecf20Sopenharmony_ci create.offset = 0; 918c2ecf20Sopenharmony_ci create.size = size; 928c2ecf20Sopenharmony_ci buf = ioctl(devfd, UDMABUF_CREATE, &create); 938c2ecf20Sopenharmony_ci if (buf < 0) { 948c2ecf20Sopenharmony_ci printf("%s: [FAIL,test-4]\n", TEST_PREFIX); 958c2ecf20Sopenharmony_ci exit(1); 968c2ecf20Sopenharmony_ci } 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci fprintf(stderr, "%s: ok\n", TEST_PREFIX); 998c2ecf20Sopenharmony_ci close(buf); 1008c2ecf20Sopenharmony_ci close(memfd); 1018c2ecf20Sopenharmony_ci close(devfd); 1028c2ecf20Sopenharmony_ci return 0; 1038c2ecf20Sopenharmony_ci} 104