18 #include <sys/types.h> 21 #include <sys/mount.h> 24 #include <linux/mempolicy.h> 25 #include <linux/memfd.h> 35 #ifndef F_LINUX_SPECIFIC_BASE 36 #define F_LINUX_SPECIFIC_BASE 1024 40 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) 41 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10) 43 #define F_SEAL_SEAL 0x0001 44 #define F_SEAL_SHRINK 0x0002 45 #define F_SEAL_GROW 0x0004 46 #define F_SEAL_WRITE 0x0008 52 struct stat st = { 0 };
53 if (fstat (fd, &st) == -1)
69 if (log2_page_size <= 12)
71 else if (log2_page_size > 12 && log2_page_size <= 16)
90 long unsigned int old_mask[16] = { 0 };
96 rv =
get_mempolicy (&old_mpol, old_mask,
sizeof (old_mask) * 8 + 1,
112 mmap_flags |= MAP_LOCKED;
117 mmap_flags |= MAP_SHARED;
122 char template[] =
"/tmp/hugepage_mount.XXXXXX";
124 mount_dir = mkdtemp (
template);
128 if (mount (
"none", (
char *) mount_dir,
"hugetlbfs", 0,
NULL))
130 rmdir ((
char *) mount_dir);
136 filename =
format (0,
"%s/%s%c", mount_dir, a->
name, 0);
138 if ((fd = open ((
char *) filename, O_CREAT | O_RDWR, 0755)) == -1)
140 umount2 ((
char *) mount_dir, MNT_DETACH);
141 rmdir ((
char *) mount_dir);
145 umount2 ((
char *) mount_dir, MNT_DETACH);
146 rmdir ((
char *) mount_dir);
147 mmap_flags |= MAP_LOCKED;
165 if (log2_page_size == 0)
174 mmap_flags |= MAP_FIXED;
179 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
182 mmap_flags |= MAP_HUGETLB;
187 log2_page_size =
min_log2 (sysconf (_SC_PAGESIZE));
191 n_pages = ((a->
size - 1) >> log2_page_size) + 1;
196 1 << (log2_page_size - 10),
204 if ((ftruncate (fd, (
u64) n_pages * (1 << log2_page_size))) == -1)
213 long unsigned int mask[16] = { 0 };
225 (PROT_READ | PROT_WRITE), mmap_flags, fd, 0);
226 if (addr == MAP_FAILED)
233 if (old_mpol != -1 &&
234 set_mempolicy (old_mpol, old_mask,
sizeof (old_mask) * 8 + 1) == -1)
258 int pagesize = sysconf (_SC_PAGESIZE);
263 if ((fd = open ((
char *)
"/proc/self/pagemap", O_RDONLY)) == -1)
266 for (i = 0; i < n_pages; i++)
268 u64 seek, pagemap = 0;
270 seek = ((
u64) vaddr / pagesize) *
sizeof (
u64);
271 if (lseek (fd, seek, SEEK_SET) != seek)
274 if (read (fd, &pagemap,
sizeof (pagemap)) != (
sizeof (pagemap)))
277 if ((pagemap & (1ULL << 63)) == 0)
297 int mmap_flags = MAP_SHARED;
301 mmap_flags |= MAP_FIXED;
304 PROT_READ | PROT_WRITE, mmap_flags, a->
fd, 0);
306 if (addr == MAP_FAILED)
#define CLIB_MEM_VM_F_HUGETLB
#define CLIB_MEM_VM_F_NUMA_PREFER
u64 clib_mem_vm_get_page_size(int fd)
void * addr
Pointer to allocated memory, set on successful allocation.
uword requested_va
Request fixed position mapping.
static int memfd_create(const char *name, unsigned int flags)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
static u64 clib_cpu_time_now(void)
int numa_node
numa node preference.
static uword min_log2(uword x)
#define MFD_ALLOW_SEALING
clib_error_t * clib_mem_vm_ext_map(clib_mem_vm_map_t *a)
clib_error_t * clib_mem_vm_ext_alloc(clib_mem_vm_alloc_t *a)
uword requested_va
Request fixed position mapping.
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.
#define CLIB_MEM_VM_F_SHARED
int fd
File descriptor, 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 descriptor will be provided ...
int clib_mem_vm_get_log2_page_size(int fd)
#define CLIB_MEM_VM_F_HUGETLB_PREALLOC
#define uword_to_pointer(u, type)
#define CLIB_MEM_VM_F_LOCKED
static uword pointer_to_uword(const void *p)
#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)
void clib_mem_vm_randomize_va(uword *requested_va, u32 log2_page_size)
int fd
File descriptor to be mapped.
void * addr
Pointer to mapped memory, if successful.
clib_error_t * clib_sysfs_prealloc_hugepages(int numa_node, int page_size, int nr)