diff options
author | Damjan Marion <damarion@cisco.com> | 2018-10-19 12:54:42 +0200 |
---|---|---|
committer | Dave Barach <openvpp@barachs.net> | 2018-10-19 12:26:50 +0000 |
commit | 1636b1629339c8235e344e7a95d88ecede642cd5 (patch) | |
tree | 8359c97957a0151e411c7b746d8284312a219ce7 /src/vppinfra/linux/mem.c | |
parent | da41d729b9339528b6c9bd1e859792303d15eb78 (diff) |
vppinfra: use memfd_create for hugepage mounts if supported
Starting with kernel 4.14 hugepage fd can be retrieved with
memfd_systemcall
Change-Id: I0f3bd6d0a7757ffe7b98e83763502013ac763ecb
Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vppinfra/linux/mem.c')
-rw-r--r-- | src/vppinfra/linux/mem.c | 76 |
1 files changed, 51 insertions, 25 deletions
diff --git a/src/vppinfra/linux/mem.c b/src/vppinfra/linux/mem.c index 227e55b6d56..eb4312b2725 100644 --- a/src/vppinfra/linux/mem.c +++ b/src/vppinfra/linux/mem.c @@ -77,6 +77,55 @@ clib_mem_vm_randomize_va (uword * requested_va, u32 log2_page_size) (clib_cpu_time_now () & bit_mask) * (1ull << log2_page_size); } +#ifndef MFD_HUGETLB +#define MFD_HUGETLB 0x0004U +#endif + +clib_error_t * +clib_mem_create_hugetlb_fd (char *name, int *fdp) +{ + clib_error_t *err = 0; + int fd = -1; + static int memfd_hugetlb_supported = 1; + char *mount_dir; + char template[] = "/tmp/hugepage_mount.XXXXXX"; + u8 *filename; + + if (memfd_hugetlb_supported) + { + if ((fd = memfd_create (name, MFD_HUGETLB)) != -1) + goto done; + + /* avoid further tries if memfd MFD_HUGETLB is not supported */ + if (errno == EINVAL && strnlen (name, 256) <= 249) + memfd_hugetlb_supported = 0; + } + + mount_dir = mkdtemp (template); + if (mount_dir == 0) + return clib_error_return_unix (0, "mkdtemp \'%s\'", template); + + if (mount ("none", (char *) mount_dir, "hugetlbfs", 0, NULL)) + { + rmdir ((char *) mount_dir); + err = clib_error_return_unix (0, "mount hugetlb directory '%s'", + mount_dir); + } + + filename = format (0, "%s/%s%c", mount_dir, name, 0); + fd = open ((char *) filename, O_CREAT | O_RDWR, 0755); + umount2 ((char *) mount_dir, MNT_DETACH); + rmdir ((char *) mount_dir); + + if (fd == -1) + err = clib_error_return_unix (0, "open"); + +done: + if (fd != -1) + fdp[0] = fd; + return err; +} + clib_error_t * clib_mem_vm_ext_alloc (clib_mem_vm_alloc_t * a) { @@ -119,32 +168,9 @@ clib_mem_vm_ext_alloc (clib_mem_vm_alloc_t * a) /* if hugepages are needed we need to create mount point */ if (a->flags & CLIB_MEM_VM_F_HUGETLB) { - char *mount_dir; - char template[] = "/tmp/hugepage_mount.XXXXXX"; - - mount_dir = mkdtemp (template); - if (mount_dir == 0) - return clib_error_return_unix (0, "mkdtemp \'%s\'", template); + if ((err = clib_mem_create_hugetlb_fd (a->name, &fd))) + goto error; - if (mount ("none", (char *) mount_dir, "hugetlbfs", 0, NULL)) - { - rmdir ((char *) mount_dir); - err = clib_error_return_unix (0, "mount hugetlb directory '%s'", - mount_dir); - goto error; - } - - filename = format (0, "%s/%s%c", mount_dir, a->name, 0); - - if ((fd = open ((char *) filename, O_CREAT | O_RDWR, 0755)) == -1) - { - umount2 ((char *) mount_dir, MNT_DETACH); - rmdir ((char *) mount_dir); - err = clib_error_return_unix (0, "open"); - goto error; - } - umount2 ((char *) mount_dir, MNT_DETACH); - rmdir ((char *) mount_dir); mmap_flags |= MAP_LOCKED; } else |