From c96772227b7dfcaf4eec4d07acb5c916643aca3a Mon Sep 17 00:00:00 2001 From: Brooks Davis Date: Mon, 27 Nov 2023 17:07:06 +0000 Subject: [PATCH] memfd_create: don't allocate heap memory Rather than calling calloc() to allocate space for a page size array to pass to getpagesizes(), just follow the getpagesizes() implementation and allocate MAXPAGESIZES elements on the stack. This avoids the need for the allocation. While this does mean that a new libc is required to take advantage of a new huge page size, that was already true due to getpagesizes() using a static buffer of MAXPAGESIZES elements. Reviewed by: kevans, imp, emaste Sponsored by: DARPA Differential Revision: https://reviews.freebsd.org/D42710 --- lib/libc/gen/memfd_create.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/lib/libc/gen/memfd_create.c b/lib/libc/gen/memfd_create.c index b26d638656a4..78131f46d7b1 100644 --- a/lib/libc/gen/memfd_create.c +++ b/lib/libc/gen/memfd_create.c @@ -35,7 +35,6 @@ #include #include #include -#include #include #include @@ -52,7 +51,8 @@ int memfd_create(const char *name, unsigned int flags) { char memfd_name[NAME_MAX + 1]; - size_t namelen, *pgs, pgsize; + size_t pgs[MAXPAGESIZES]; + size_t namelen, pgsize; struct shm_largepage_conf slc; int error, fd, npgs, oflags, pgidx, saved_errno, shmflags; @@ -92,16 +92,9 @@ memfd_create(const char *name, unsigned int flags) if (fd == -1 || (flags & MFD_HUGETLB) == 0) return (fd); - pgs = NULL; - npgs = getpagesizes(NULL, 0); + npgs = getpagesizes(pgs, nitems(pgs)); if (npgs == -1) goto clean; - pgs = calloc(npgs, sizeof(size_t)); - if (pgs == NULL) - goto clean; - error = getpagesizes(pgs, npgs); - if (error == -1) - goto clean; pgsize = (size_t)1 << ((flags & MFD_HUGE_MASK) >> MFD_HUGE_SHIFT); for (pgidx = 0; pgidx < npgs; pgidx++) { if (pgsize == pgs[pgidx]) @@ -111,8 +104,6 @@ memfd_create(const char *name, unsigned int flags) errno = EOPNOTSUPP; goto clean; } - free(pgs); - pgs = NULL; memset(&slc, 0, sizeof(slc)); slc.psind = pgidx; @@ -125,7 +116,6 @@ memfd_create(const char *name, unsigned int flags) clean: saved_errno = errno; close(fd); - free(pgs); errno = saved_errno; return (-1); }