svn commit: r363066 - in head: lib/libc/sys sys/compat/linux tests/sys/kern

Kyle Evans kevans at FreeBSD.org
Fri Jul 10 00:45:17 UTC 2020


Author: kevans
Date: Fri Jul 10 00:45:16 2020
New Revision: 363066
URL: https://svnweb.freebsd.org/changeset/base/363066

Log:
  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

Modified:
  head/lib/libc/sys/shm_open.c
  head/sys/compat/linux/linux_file.c
  head/tests/sys/kern/memfd_test.c

Modified: head/lib/libc/sys/shm_open.c
==============================================================================
--- head/lib/libc/sys/shm_open.c	Fri Jul 10 00:43:45 2020	(r363065)
+++ head/lib/libc/sys/shm_open.c	Fri Jul 10 00:45:16 2020	(r363066)
@@ -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)

Modified: head/sys/compat/linux/linux_file.c
==============================================================================
--- head/sys/compat/linux/linux_file.c	Fri Jul 10 00:43:45 2020	(r363065)
+++ head/sys/compat/linux/linux_file.c	Fri Jul 10 00:45:16 2020	(r363066)
@@ -1758,7 +1758,7 @@ linux_memfd_create(struct thread *td, struct linux_mem
 	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)

Modified: head/tests/sys/kern/memfd_test.c
==============================================================================
--- head/tests/sys/kern/memfd_test.c	Fri Jul 10 00:43:45 2020	(r363065)
+++ head/tests/sys/kern/memfd_test.c	Fri Jul 10 00:45:16 2020	(r363066)
@@ -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);
 }


More information about the svn-src-head mailing list