1*29f34d1dSMarc-André Lureau // SPDX-License-Identifier: GPL-2.0 2*29f34d1dSMarc-André Lureau #define _GNU_SOURCE 3*29f34d1dSMarc-André Lureau #define __EXPORTED_HEADERS__ 4*29f34d1dSMarc-André Lureau 5*29f34d1dSMarc-André Lureau #include <stdio.h> 6*29f34d1dSMarc-André Lureau #include <stdlib.h> 7*29f34d1dSMarc-André Lureau #include <linux/fcntl.h> 8*29f34d1dSMarc-André Lureau #include <linux/memfd.h> 9*29f34d1dSMarc-André Lureau #include <unistd.h> 10*29f34d1dSMarc-André Lureau #include <sys/syscall.h> 11*29f34d1dSMarc-André Lureau 12*29f34d1dSMarc-André Lureau #include "common.h" 13*29f34d1dSMarc-André Lureau 14*29f34d1dSMarc-André Lureau int hugetlbfs_test = 0; 15*29f34d1dSMarc-André Lureau 16*29f34d1dSMarc-André Lureau /* 17*29f34d1dSMarc-André Lureau * Copied from mlock2-tests.c 18*29f34d1dSMarc-André Lureau */ default_huge_page_size(void)19*29f34d1dSMarc-André Lureauunsigned long default_huge_page_size(void) 20*29f34d1dSMarc-André Lureau { 21*29f34d1dSMarc-André Lureau unsigned long hps = 0; 22*29f34d1dSMarc-André Lureau char *line = NULL; 23*29f34d1dSMarc-André Lureau size_t linelen = 0; 24*29f34d1dSMarc-André Lureau FILE *f = fopen("/proc/meminfo", "r"); 25*29f34d1dSMarc-André Lureau 26*29f34d1dSMarc-André Lureau if (!f) 27*29f34d1dSMarc-André Lureau return 0; 28*29f34d1dSMarc-André Lureau while (getline(&line, &linelen, f) > 0) { 29*29f34d1dSMarc-André Lureau if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) { 30*29f34d1dSMarc-André Lureau hps <<= 10; 31*29f34d1dSMarc-André Lureau break; 32*29f34d1dSMarc-André Lureau } 33*29f34d1dSMarc-André Lureau } 34*29f34d1dSMarc-André Lureau 35*29f34d1dSMarc-André Lureau free(line); 36*29f34d1dSMarc-André Lureau fclose(f); 37*29f34d1dSMarc-André Lureau return hps; 38*29f34d1dSMarc-André Lureau } 39*29f34d1dSMarc-André Lureau sys_memfd_create(const char * name,unsigned int flags)40*29f34d1dSMarc-André Lureauint sys_memfd_create(const char *name, unsigned int flags) 41*29f34d1dSMarc-André Lureau { 42*29f34d1dSMarc-André Lureau if (hugetlbfs_test) 43*29f34d1dSMarc-André Lureau flags |= MFD_HUGETLB; 44*29f34d1dSMarc-André Lureau 45*29f34d1dSMarc-André Lureau return syscall(__NR_memfd_create, name, flags); 46*29f34d1dSMarc-André Lureau } 47