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
This commit is contained in:
Brooks Davis 2023-11-27 17:07:06 +00:00
parent c3207e2d25
commit c96772227b

View file

@ -35,7 +35,6 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@ -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);
}