18 #include <sys/types.h> 21 #include <sys/mount.h> 24 #include <linux/mempolicy.h> 25 #include <linux/memfd.h> 34 #ifndef F_LINUX_SPECIFIC_BASE 35 #define F_LINUX_SPECIFIC_BASE 1024 39 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) 40 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10) 42 #define F_SEAL_SEAL 0x0001 43 #define F_SEAL_SHRINK 0x0002 44 #define F_SEAL_GROW 0x0004 45 #define F_SEAL_WRITE 0x0008 51 struct stat st = { 0 };
52 if (fstat (fd, &st) == -1)
64 int mmap_flags = MAP_SHARED;
68 u64 old_mask[16] = { 0 };
75 get_mempolicy (&old_mpol, old_mask,
sizeof (old_mask) * 8 + 1, 0, 0);
96 char template[] =
"/tmp/hugepage_mount.XXXXXX";
98 mount_dir = mkdtemp (
template);
102 if (mount (
"none", (
char *) mount_dir,
"hugetlbfs", 0,
NULL))
109 filename =
format (0,
"%s/%s%c", mount_dir, a->
name, 0);
111 if ((fd = open ((
char *) filename, O_CREAT | O_RDWR, 0755)) == -1)
116 umount2 ((
char *) mount_dir, MNT_DETACH);
117 rmdir ((
char *) mount_dir);
118 mmap_flags |= MAP_LOCKED;
136 if (log2_page_size == 0)
146 mmap_flags |= MAP_HUGETLB | MAP_PRIVATE | MAP_ANONYMOUS;
151 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
152 log2_page_size =
min_log2 (sysconf (_SC_PAGESIZE));
156 n_pages = ((a->
size - 1) >> log2_page_size) + 1;
162 1 << (log2_page_size - 10),
170 if ((ftruncate (fd, a->
size)) == -1)
179 u64 mask[16] = { 0 };
189 addr = mmap (0, a->
size, (PROT_READ | PROT_WRITE), mmap_flags, fd, 0);
190 if (addr == MAP_FAILED)
197 if (old_mpol != -1 &&
198 set_mempolicy (old_mpol, old_mask,
sizeof (old_mask) * 8 + 1) == -1)
222 int pagesize = sysconf (_SC_PAGESIZE);
227 if ((fd = open ((
char *)
"/proc/self/pagemap", O_RDONLY)) == -1)
230 for (i = 0; i < n_pages; i++)
232 u64 seek, pagemap = 0;
234 seek = ((
u64) vaddr / pagesize) *
sizeof (
u64);
235 if (lseek (fd, seek, SEEK_SET) != seek)
238 if (read (fd, &pagemap,
sizeof (pagemap)) != (
sizeof (pagemap)))
241 if ((pagemap & (1ULL << 63)) == 0)
#define CLIB_MEM_VM_F_HUGETLB
sll srl srl sll sra u16x4 i
#define MFD_ALLOW_SEALING
#define CLIB_MEM_VM_F_NUMA_PREFER
void * addr
Pointer to allocated memory, set on successful allocation.
static int memfd_create(const char *name, unsigned int flags)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
int numa_node
numa node preference.
static uword min_log2(uword x)
clib_error_t * clib_mem_vm_ext_alloc(clib_mem_vm_alloc_t *a)
static long set_mempolicy(int mode, const unsigned long *nodemask, unsigned long maxnode)
static uword pow2_mask(uword x)
char * name
Name for memory allocation, set by caller.
uword size
Allocation size, set by caller.
static uword pointer_to_uword(const void *p)
#define CLIB_MEM_VM_F_SHARED
int fd
File desriptor, set on successful allocation if CLIB_MEM_VM_F_SHARED is set.
#define clib_error_return_unix(e, args...)
#define CLIB_MEM_VM_F_NUMA_FORCE
static int get_mempolicy(int *mode, unsigned long *nodemask, unsigned long maxnode, void *addr, unsigned long flags)
#define vec_free(V)
Free vector's memory (no header).
u32 flags
vm allocation flags: CLIB_MEM_VM_F_SHARED: request shared memory, file destiptor will be provided o...
int clib_mem_vm_get_log2_page_size(int fd)
#define CLIB_MEM_VM_F_HUGETLB_PREALLOC
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 * clib_mem_vm_get_paddr(void *mem, int log2_page_size, int n_pages)
clib_error_t * clib_sysfs_prealloc_hugepages(int numa_node, int page_size, int nr)