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 u64 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))
135 filename =
format (0,
"%s/%s%c", mount_dir, a->
name, 0);
137 if ((fd = open ((
char *) filename, O_CREAT | O_RDWR, 0755)) == -1)
142 umount2 ((
char *) mount_dir, MNT_DETACH);
143 rmdir ((
char *) mount_dir);
144 mmap_flags |= MAP_LOCKED;
162 if (log2_page_size == 0)
171 mmap_flags |= MAP_FIXED;
176 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
179 mmap_flags |= MAP_HUGETLB;
184 log2_page_size =
min_log2 (sysconf (_SC_PAGESIZE));
188 n_pages = ((a->
size - 1) >> log2_page_size) + 1;
193 1 << (log2_page_size - 10),
201 if ((ftruncate (fd, (
u64) n_pages * (1 << log2_page_size))) == -1)
210 u64 mask[16] = { 0 };
221 (PROT_READ | PROT_WRITE), mmap_flags, fd, 0);
222 if (addr == MAP_FAILED)
229 if (old_mpol != -1 &&
230 set_mempolicy (old_mpol, old_mask,
sizeof (old_mask) * 8 + 1) == -1)
254 int pagesize = sysconf (_SC_PAGESIZE);
259 if ((fd = open ((
char *)
"/proc/self/pagemap", O_RDONLY)) == -1)
262 for (i = 0; i < n_pages; i++)
264 u64 seek, pagemap = 0;
266 seek = ((
u64) vaddr / pagesize) *
sizeof (
u64);
267 if (lseek (fd, seek, SEEK_SET) != seek)
270 if (read (fd, &pagemap,
sizeof (pagemap)) != (
sizeof (pagemap)))
273 if ((pagemap & (1ULL << 63)) == 0)
293 int mmap_flags = MAP_SHARED;
297 mmap_flags |= MAP_FIXED;
300 PROT_READ | PROT_WRITE, mmap_flags, a->
fd, 0);
302 if (addr == MAP_FAILED)
#define CLIB_MEM_VM_F_HUGETLB
#define CLIB_MEM_VM_F_NUMA_PREFER
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)
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.
static uword pointer_to_uword(const void *p)
#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
uword clib_mem_vm_get_page_size(int fd)
#define MFD_ALLOW_SEALING
static int get_mempolicy(int *mode, unsigned long *nodemask, unsigned long maxnode, void *addr, unsigned long flags)
#define uword_to_pointer(u, type)
#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 CLIB_MEM_VM_F_LOCKED
#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)