memfd_create: turn on SHM_GROW_ON_WRITE

memfd_create fds will no longer require an ftruncate(2) to set the size;
they'll grow (to the extent that it's possible) upon write(2)-like syscalls.

Reviewed by:	kib
Differential Revision:	https://reviews.freebsd.org/D25502
This commit is contained in:
Kyle Evans 2020-07-10 00:45:16 +00:00
parent 3f07b9d9f8
commit 423a033ba7
3 changed files with 15 additions and 6 deletions

View File

@ -84,7 +84,7 @@ memfd_create(const char *name, unsigned int flags)
/* We've already validated that we're sufficiently sized. */
snprintf(memfd_name, NAME_MAX + 1, "%s%s", MEMFD_NAME_PREFIX, name);
oflags = O_RDWR;
shmflags = 0;
shmflags = SHM_GROW_ON_WRITE;
if ((flags & MFD_CLOEXEC) != 0)
oflags |= O_CLOEXEC;
if ((flags & MFD_ALLOW_SEALING) != 0)

View File

@ -1758,7 +1758,7 @@ linux_memfd_create(struct thread *td, struct linux_memfd_create_args *args)
if ((flags & MFD_HUGETLB) != 0)
return (ENOSYS);
oflags = O_RDWR;
shmflags = 0;
shmflags = SHM_GROW_ON_WRITE;
if ((flags & MFD_CLOEXEC) != 0)
oflags |= O_CLOEXEC;
if ((flags & MFD_ALLOW_SEALING) != 0)

View File

@ -30,6 +30,7 @@ __FBSDID("$FreeBSD$");
#include <sys/fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <atf-c.h>
#include <errno.h>
@ -38,18 +39,26 @@ __FBSDID("$FreeBSD$");
ATF_TC_WITHOUT_HEAD(basic);
ATF_TC_BODY(basic, tc)
{
struct stat sb;
int fd;
char buf[8];
ATF_REQUIRE((fd = memfd_create("...", 0)) != -1);
/* File size should be initially 0 */
ATF_REQUIRE(write(fd, buf, sizeof(buf)) == 0);
/* write(2) should grow us out automatically. */
ATF_REQUIRE(write(fd, buf, sizeof(buf)) == sizeof(buf));
ATF_REQUIRE(fstat(fd, &sb) == 0);
ATF_REQUIRE(sb.st_size == sizeof(buf));
/* ftruncate(2) must succeed without seals */
ATF_REQUIRE(ftruncate(fd, sizeof(buf) - 1) == 0);
ATF_REQUIRE(ftruncate(fd, 2 * (sizeof(buf) - 1)) == 0);
ATF_REQUIRE(write(fd, buf, sizeof(buf)) == sizeof(buf) - 1);
/* write(2) again must not be limited by ftruncate(2) size. */
ATF_REQUIRE(write(fd, buf, sizeof(buf)) == sizeof(buf));
/* Sanity check. */
ATF_REQUIRE(fstat(fd, &sb) == 0);
ATF_REQUIRE(sb.st_size == 2 * sizeof(buf));
close(fd);
}