Linux: Add memfd_create system call wrapper

Message ID 1ae3b4f9-77eb-a1bc-4ba1-2c6660558ff9@redhat.com
State Superseded
Headers

Commit Message

Florian Weimer Nov. 12, 2017, 2:51 p.m. UTC
  On 11/12/2017 10:04 AM, Florian Weimer wrote:
> If that's not acceptable, we should probably not add <sys/memfd.h> at
> all, but replicate the UAPI constants in <sys/mman.h>.
> 
> I see that this also affects the test.  So we probably should go the
> <sys/mman.h> route.

I had already taken care of that, I forgot.  Anyway, the attached patch 
has the <sys/mman.h> variant of the system call wrapper.

Thanks,
Florian
  

Comments

Adhemerval Zanella Netto Nov. 16, 2017, 10:34 a.m. UTC | #1
On 12/11/2017 12:51, Florian Weimer wrote:
> On 11/12/2017 10:04 AM, Florian Weimer wrote:
>> If that's not acceptable, we should probably not add <sys/memfd.h> at
>> all, but replicate the UAPI constants in <sys/mman.h>.
>>
>> I see that this also affects the test.  So we probably should go the
>> <sys/mman.h> route.
> 
> I had already taken care of that, I forgot.  Anyway, the attached patch has the <sys/mman.h> variant of the system call wrapper.
> 
> Thanks,
> Florian
> 
> memfd.patch
> 
> 
> The system call is somewhat obscure because it is closely related
> to file descriptor sealing.  However, it is also the recommended
> way to create alias mappings, which is why it has more general use.
> 
> No emulation is provided.  Except for the name of the
> /proc/self/fd links, it would be possible to implement an
> approximation using O_TMPFILE and tmpfs, but this does not appear
> to be worth the added complexity.
> 
> 2017-11-12  Florian Weimer  <fweimer@redhat.com>
> 
> 	Linux: Add memfd_create system call wrapper
> 	* sysdeps/unix/sysv/linux/Makefile [misc] (tests): Add
> 	tst-memfd_create.
> 	* sysdeps/unix/sysv/linux/bits/mman-linux.h [__USE_GNU]
> 	(MFD_CLOEXEC, MFD_ALLOW_SEALING): Define.
> 	[__USE_GNU] (memfd_create): Declare.
> 	* sysdeps/unix/sysv/linux/Versions (GLIBC_2.27): Add memfd_create.
> 	* sysdeps/unix/sysv/linux/syscalls.list (memfd_create): Add.
> 	* sysdeps/unix/sysv/linux/tst-memfd_create.c: New file.
> 	* sysdeps/unix/sysv/linux/**.abilist: Update.
> 	* manual/llio.texi (Memory-mapped I/O): Document memfd_create.

LGTM with just an update below.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>

> 
> diff --git a/NEWS b/NEWS
> index b7281621f4..eca0c00a9b 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -35,6 +35,9 @@ Major new features:
>    are the same interfaces added in version 2.26 for some platforms where
>    this format is supported but is not the format of long double.
>  
> +* glibc now provides the <sys/memfd.h> header file and the memfd_create
> +  system call.
> +
>  Deprecated and removed features, and other changes affecting compatibility:
>  
>  * On GNU/Linux, the obsolete Linux constant PTRACE_SEIZE_DEVEL is no longer

Ok.

> diff --git a/manual/llio.texi b/manual/llio.texi
> index 10ad546723..59eb3af711 100644
> --- a/manual/llio.texi
> +++ b/manual/llio.texi
> @@ -1775,6 +1775,40 @@ the given @var{name} previously created by @code{shm_open}.
>  On failure @code{errno} is set.
>  @end deftypefn
>  
> +@deftypefn Function int memfd_create (const char *@var{name}, unsigned int flags)
> +@standards{Linux, sys/mman.h}
> +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
> +The @code{memfd_create} function returns a file descriptor which can be
> +used to create memory mappings using the @code{mmap} function.  It is
> +similar to the @code{shm_open} function in the sense that these mappings
> +are not backed by actual files.  However, the descriptor return by
> +@code{memfd_create} does not correspond to a named object; the
> +@var{name} argument is used for informative purposes only.  The
> +descriptor can be passed between processes (for example, over local
> +domain sockets), and mappings at the same offset refer to the same
> +memory, or the descriptor can be used to create alias mappings within
> +the same process.
> +
> +The descriptor initially refers to a zero-length file.  Before mappings
> +can be created which are backed by memory, the file size needs to be
> +increased with the @code{ftruncate} function.  @xref{File Size}.
> +
> +The @var{flags} argument can be a combination of the following flags:
> +
> +@table @code
> +@item MFD_CLOEXEC
> +The descriptor is created with the @code{O_CLOEXEC} flag.
> +
> +@item MFD_ALLOW_SEALING
> +The descriptor supports the addition of seals using the @code{fcntl}
> +function.
> +@end table
> +
> +The @code{memfd_create} function is specific to Linux.
> +
> +@end deftypefn
> +
> +
>  @node Waiting for I/O
>  @section Waiting for Input or Output
>  @cindex waiting for input or output

Linux 4.14 (749df87bd7be) added a new flag MFD_HUGETLB. I think ww should add
and document it.

> diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
> index bf76b8773d..f974ac446b 100644
> --- a/sysdeps/unix/sysv/linux/Makefile
> +++ b/sysdeps/unix/sysv/linux/Makefile
> @@ -43,7 +43,8 @@ sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \
>  		  bits/siginfo-arch.h bits/siginfo-consts-arch.h
>  
>  tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \
> -	 tst-quota tst-sync_file_range test-errno-linux tst-sysconf-iov_max
> +	 tst-quota tst-sync_file_range test-errno-linux tst-sysconf-iov_max \
> +	 tst-memfd_create
>  
>  # Generate the list of SYS_* macros for the system calls (__NR_*
>  # macros).  The file syscall-names.list contains all possible system

Ok.

> diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
> index d3dbcde8c9..6f2fe516d7 100644
> --- a/sysdeps/unix/sysv/linux/Versions
> +++ b/sysdeps/unix/sysv/linux/Versions
> @@ -166,6 +166,9 @@ libc {
>    GLIBC_2.15 {
>      process_vm_readv; process_vm_writev;
>    }
> +  GLIBC_2.27 {
> +    memfd_create;
> +  }
>    GLIBC_PRIVATE {
>      # functions used in other libraries
>      __syscall_rt_sigqueueinfo;

Ok.

> diff --git a/sysdeps/unix/sysv/linux/aarch64/libc.abilist b/sysdeps/unix/sysv/linux/aarch64/libc.abilist
> index ed598aedac..140ca28abc 100644
> --- a/sysdeps/unix/sysv/linux/aarch64/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/aarch64/libc.abilist
> @@ -2106,6 +2106,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.27 strfromf128 F
>  GLIBC_2.27 strtof128 F
>  GLIBC_2.27 strtof128_l F
> diff --git a/sysdeps/unix/sysv/linux/alpha/libc.abilist b/sysdeps/unix/sysv/linux/alpha/libc.abilist
> index 4e57f36bcf..f698e1b2f4 100644
> --- a/sysdeps/unix/sysv/linux/alpha/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/alpha/libc.abilist
> @@ -2017,6 +2017,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.27 strfromf128 F
>  GLIBC_2.27 strtof128 F
>  GLIBC_2.27 strtof128_l F
> diff --git a/sysdeps/unix/sysv/linux/arm/libc.abilist b/sysdeps/unix/sysv/linux/arm/libc.abilist
> index 5b70e1bfc9..8a8af3e3e4 100644
> --- a/sysdeps/unix/sysv/linux/arm/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/arm/libc.abilist
> @@ -107,6 +107,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.4 GLIBC_2.4 A
>  GLIBC_2.4 _Exit F
>  GLIBC_2.4 _IO_2_1_stderr_ D 0xa0
> diff --git a/sysdeps/unix/sysv/linux/bits/mman-linux.h b/sysdeps/unix/sysv/linux/bits/mman-linux.h
> index b091181960..396829482a 100644
> --- a/sysdeps/unix/sysv/linux/bits/mman-linux.h
> +++ b/sysdeps/unix/sysv/linux/bits/mman-linux.h
> @@ -109,3 +109,18 @@
>  # define MCL_ONFAULT	4		/* Lock all pages that are
>  					   faulted in.  */
>  #endif
> +
> +#ifdef __USE_GNU
> +/* Flags for memfd_create.  */
> +# define MFD_CLOEXEC 1U
> +# define MFD_ALLOW_SEALING 2U
> +
> +__BEGIN_DECLS
> +
> +/* Create a new memory file descriptor.  NAME is a name for debugging.
> +   FLAGS is a combination of the MFD_* constants.  */
> +int memfd_create (const char *__name, unsigned int __flags) __THROW;
> +
> +__END_DECLS
> +
> +#endif /* __USE_GNU */

As before I think we should include MFD_HUGETLB.

> diff --git a/sysdeps/unix/sysv/linux/hppa/libc.abilist b/sysdeps/unix/sysv/linux/hppa/libc.abilist
> index 6a2500a8b3..5b81a6cd7d 100644
> --- a/sysdeps/unix/sysv/linux/hppa/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/hppa/libc.abilist
> @@ -1871,6 +1871,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.3 GLIBC_2.3 A
>  GLIBC_2.3 __ctype_b_loc F
>  GLIBC_2.3 __ctype_tolower_loc F
> diff --git a/sysdeps/unix/sysv/linux/i386/libc.abilist b/sysdeps/unix/sysv/linux/i386/libc.abilist
> index 9ab4e3642a..51ead9e867 100644
> --- a/sysdeps/unix/sysv/linux/i386/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/i386/libc.abilist
> @@ -2036,6 +2036,7 @@ GLIBC_2.26 wcstof128_l F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.3 GLIBC_2.3 A
>  GLIBC_2.3 __ctype_b_loc F
>  GLIBC_2.3 __ctype_tolower_loc F
> diff --git a/sysdeps/unix/sysv/linux/ia64/libc.abilist b/sysdeps/unix/sysv/linux/ia64/libc.abilist
> index 81bb623fe8..78b4ee8d40 100644
> --- a/sysdeps/unix/sysv/linux/ia64/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/ia64/libc.abilist
> @@ -1900,6 +1900,7 @@ GLIBC_2.26 wcstof128_l F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.3 GLIBC_2.3 A
>  GLIBC_2.3 __ctype_b_loc F
>  GLIBC_2.3 __ctype_tolower_loc F
> diff --git a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
> index 5a33b57390..d9c97779e4 100644
> --- a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
> @@ -108,6 +108,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.4 GLIBC_2.4 A
>  GLIBC_2.4 _Exit F
>  GLIBC_2.4 _IO_2_1_stderr_ D 0x98
> diff --git a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
> index 50a86e74fa..4acbf7eeed 100644
> --- a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
> @@ -1985,6 +1985,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.3 GLIBC_2.3 A
>  GLIBC_2.3 __ctype_b_loc F
>  GLIBC_2.3 __ctype_tolower_loc F
> diff --git a/sysdeps/unix/sysv/linux/microblaze/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/libc.abilist
> index 250ef305c3..93f02f08ce 100644
> --- a/sysdeps/unix/sysv/linux/microblaze/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/microblaze/libc.abilist
> @@ -2106,3 +2106,4 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
> diff --git a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
> index 87a1dc4ad7..795e85de70 100644
> --- a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
> @@ -1960,6 +1960,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.3 GLIBC_2.3 A
>  GLIBC_2.3 __ctype_b_loc F
>  GLIBC_2.3 __ctype_tolower_loc F
> diff --git a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
> index f2b35f250e..dc714057b7 100644
> --- a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
> @@ -1958,6 +1958,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.3 GLIBC_2.3 A
>  GLIBC_2.3 __ctype_b_loc F
>  GLIBC_2.3 __ctype_tolower_loc F
> diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
> index ade654dbea..ce7bc9b175 100644
> --- a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
> @@ -1956,6 +1956,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.27 strfromf128 F
>  GLIBC_2.27 strtof128 F
>  GLIBC_2.27 strtof128_l F
> diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
> index 56032c3f82..3fdd85eace 100644
> --- a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
> @@ -1951,6 +1951,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.27 strfromf128 F
>  GLIBC_2.27 strtof128 F
>  GLIBC_2.27 strtof128_l F
> diff --git a/sysdeps/unix/sysv/linux/nios2/libc.abilist b/sysdeps/unix/sysv/linux/nios2/libc.abilist
> index c599dd9212..3e0bcb2a5c 100644
> --- a/sysdeps/unix/sysv/linux/nios2/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/nios2/libc.abilist
> @@ -2147,3 +2147,4 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
> diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
> index 385409aa6e..375c69d9d1 100644
> --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
> @@ -1989,6 +1989,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.3 GLIBC_2.3 A
>  GLIBC_2.3 __ctype_b_loc F
>  GLIBC_2.3 __ctype_tolower_loc F
> diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
> index e99cb454b5..a88172a906 100644
> --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
> @@ -1994,6 +1994,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.3 GLIBC_2.3 A
>  GLIBC_2.3 __ctype_b_loc F
>  GLIBC_2.3 __ctype_tolower_loc F
> diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist
> index 173672ab5e..fa026a332c 100644
> --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist
> +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist
> @@ -2201,3 +2201,4 @@ GLIBC_2.26 wcstof128_l F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
> diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist
> index 8a654436ab..838f395d78 100644
> --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist
> @@ -108,6 +108,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.3 GLIBC_2.3 A
>  GLIBC_2.3 _Exit F
>  GLIBC_2.3 _IO_2_1_stderr_ D 0xe0
> diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
> index dbd411ceb1..41b79c496a 100644
> --- a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
> @@ -1989,6 +1989,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.27 strfromf128 F
>  GLIBC_2.27 strtof128 F
>  GLIBC_2.27 strtof128_l F
> diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
> index 5617784ca0..68251a0e69 100644
> --- a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
> @@ -1890,6 +1890,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.27 strfromf128 F
>  GLIBC_2.27 strtof128 F
>  GLIBC_2.27 strtof128_l F
> diff --git a/sysdeps/unix/sysv/linux/sh/libc.abilist b/sysdeps/unix/sysv/linux/sh/libc.abilist
> index 0f840e6e88..bc1aae275e 100644
> --- a/sysdeps/unix/sysv/linux/sh/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/sh/libc.abilist
> @@ -1875,6 +1875,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.3 GLIBC_2.3 A
>  GLIBC_2.3 __ctype_b_loc F
>  GLIBC_2.3 __ctype_tolower_loc F
> diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
> index bb7e1042c7..93e6d092ac 100644
> --- a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
> @@ -1982,6 +1982,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.27 strfromf128 F
>  GLIBC_2.27 strtof128 F
>  GLIBC_2.27 strtof128_l F
> diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
> index 4053b0a51c..b11d6764d4 100644
> --- a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
> @@ -1919,6 +1919,7 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.27 strfromf128 F
>  GLIBC_2.27 strtof128 F
>  GLIBC_2.27 strtof128_l F
> diff --git a/sysdeps/unix/sysv/linux/syscalls.list b/sysdeps/unix/sysv/linux/syscalls.list
> index 8bfb080d80..40c4fbb9ea 100644
> --- a/sysdeps/unix/sysv/linux/syscalls.list
> +++ b/sysdeps/unix/sysv/linux/syscalls.list
> @@ -109,3 +109,4 @@ setns		EXTRA	setns		i:ii	setns
>  
>  process_vm_readv EXTRA	process_vm_readv i:ipipii process_vm_readv
>  process_vm_writev EXTRA	process_vm_writev i:ipipii process_vm_writev
> +memfd_create    EXTRA	memfd_create	i:si    memfd_create
> diff --git a/sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist b/sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist
> index 38a96d3a02..e9eb4ff7bd 100644
> --- a/sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist
> @@ -2113,3 +2113,4 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
> diff --git a/sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist b/sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist
> index 572b917d7d..8f08e909cd 100644
> --- a/sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist
> @@ -2113,3 +2113,4 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
> diff --git a/sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist b/sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist
> index 38a96d3a02..e9eb4ff7bd 100644
> --- a/sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist
> @@ -2113,3 +2113,4 @@ GLIBC_2.26 reallocarray F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
> diff --git a/sysdeps/unix/sysv/linux/tst-memfd_create.c b/sysdeps/unix/sysv/linux/tst-memfd_create.c
> new file mode 100644
> index 0000000000..fd173ff71d
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/tst-memfd_create.c
> @@ -0,0 +1,121 @@
> +/* Test for the memfd_create system call.
> +   Copyright (C) 2017 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <http://www.gnu.org/licenses/>.  */
> +
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <support/check.h>
> +#include <support/support.h>
> +#include <support/test-driver.h>
> +#include <support/xunistd.h>
> +#include <sys/mman.h>
> +
> +/* Return true if the descriptor has the FD_CLOEXEC flag set.  */
> +static bool
> +is_cloexec (int fd)
> +{
> +  int flags = fcntl (fd, F_GETFD);
> +  TEST_VERIFY (flags >= 0);
> +  return flags & FD_CLOEXEC;
> +}
> +
> +/* Return the seals set on FD.  */
> +static int
> +get_seals (int fd)
> +{
> +  int flags = fcntl (fd, F_GET_SEALS);
> +  TEST_VERIFY (flags >= 0);
> +  return flags;
> +}
> +
> +/* Return true if the F_SEAL_SEAL flag is set on the descriptor.  */
> +static bool
> +is_sealed (int fd)
> +{
> +  return get_seals (fd) & F_SEAL_SEAL;
> +}
> +
> +static int
> +do_test (void)
> +{
> +  /* Initialized by the first call to memfd_create to 0 (memfd_create
> +     unsupported) or 1 (memfd_create is implemented in the kernel).
> +     Subsequent iterations check that the success/failure state is
> +     consistent.  */
> +  int supported = -1;
> +
> +  for (int do_cloexec = 0; do_cloexec < 2; ++do_cloexec)
> +    for (int do_sealing = 0; do_sealing < 2; ++do_sealing)
> +      {
> +        int flags = 0;
> +        if (do_cloexec)
> +          flags |= MFD_CLOEXEC;
> +        if (do_sealing)
> +          flags |= MFD_ALLOW_SEALING;
> +        if  (test_verbose > 0)
> +          printf ("info: memfd_create with flags=0x%x\n", flags);
> +        int fd = memfd_create ("tst-memfd_create", flags);
> +        if (fd < 0)
> +          {
> +            if (errno == ENOSYS)
> +              {
> +                if (supported < 0)
> +                  {
> +                    printf ("warning: memfd_create is unsupported\n");
> +                    supported = 0;
> +                    continue;
> +                  }
> +                TEST_VERIFY (supported == 0);
> +                continue;
> +              }
> +            else
> +              FAIL_EXIT1 ("memfd_create: %m");
> +          }
> +        if (supported < 0)
> +          supported = 1;
> +        TEST_VERIFY (supported > 0);
> +
> +        char *fd_path = xasprintf ("/proc/self/fd/%d", fd);
> +        char *link = xreadlink (fd_path);
> +        if (test_verbose > 0)
> +          printf ("info: memfd link: %s\n", link);
> +        TEST_VERIFY (strcmp (link, "memfd:tst-memfd_create (deleted)"));
> +        TEST_VERIFY (is_cloexec (fd) == do_cloexec);
> +        TEST_VERIFY (is_sealed (fd) == !do_sealing);
> +        if (do_sealing)
> +          {
> +            TEST_VERIFY (fcntl (fd, F_ADD_SEALS, F_SEAL_WRITE) == 0);
> +            TEST_VERIFY (!is_sealed (fd));
> +            TEST_VERIFY (get_seals (fd) & F_SEAL_WRITE);
> +            TEST_VERIFY (fcntl (fd, F_ADD_SEALS, F_SEAL_SEAL) == 0);
> +            TEST_VERIFY (is_sealed (fd));
> +          }
> +        xclose (fd);
> +        free (fd_path);
> +        free (link);
> +      }
> +
> +  if (supported == 0)
> +    return EXIT_UNSUPPORTED;
> +  return 0;
> +}
> +
> +#include <support/test-driver.c>

Ok.

> diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
> index b83d25c2e3..0a4f7797ac 100644
> --- a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
> @@ -1877,6 +1877,7 @@ GLIBC_2.26 wcstof128_l F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>  GLIBC_2.3 GLIBC_2.3 A
>  GLIBC_2.3 __ctype_b_loc F
>  GLIBC_2.3 __ctype_tolower_loc F
> diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
> index cba1d59057..23f6a91429 100644
> --- a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
> +++ b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
> @@ -2120,3 +2120,4 @@ GLIBC_2.26 wcstof128_l F
>  GLIBC_2.27 GLIBC_2.27 A
>  GLIBC_2.27 glob F
>  GLIBC_2.27 glob64 F
> +GLIBC_2.27 memfd_create F
>
  
Christian Brauner Nov. 16, 2017, 1:30 p.m. UTC | #2
On Thu, Nov 16, 2017 at 08:34:26AM -0200, Adhemerval Zanella wrote:
> 
> 
> On 12/11/2017 12:51, Florian Weimer wrote:
> > On 11/12/2017 10:04 AM, Florian Weimer wrote:
> >> If that's not acceptable, we should probably not add <sys/memfd.h> at
> >> all, but replicate the UAPI constants in <sys/mman.h>.

+1 that sounds like a good idea.

> >>
> >> I see that this also affects the test.  So we probably should go the
> >> <sys/mman.h> route.
> > 
> > I had already taken care of that, I forgot.  Anyway, the attached patch has the <sys/mman.h> variant of the system call wrapper.
> > 
> > Thanks,
> > Florian
> > 
> > memfd.patch
> > 
> > 
> > The system call is somewhat obscure because it is closely related
> > to file descriptor sealing.  However, it is also the recommended

Sure and the api is somewhat bogus given this nonesensical "name" argument in
there but it actually has a lot more use-cases for userspace.

> > way to create alias mappings, which is why it has more general use.

For example, systemd and lxc both carry their own memfd_create() definitions
including the appropriate syscall number and option numbers. A concrete use-case
is e.g. using mmap() + memfd_create() to create an efficient (Linux specific)
in-memory ringbuffer ( https://github.com/lxc/lxc/blob/master/src/lxc/ringbuf.c
). In general, both have valid use cases to only have an in-memory file that
can't be covered by mmap() alone. So this is really good to finally have!

> > 
> > No emulation is provided.  Except for the name of the
> > /proc/self/fd links, it would be possible to implement an
> > approximation using O_TMPFILE and tmpfs, but this does not appear
> > to be worth the added complexity.
> > 
> > 2017-11-12  Florian Weimer  <fweimer@redhat.com>
> > 
> > 	Linux: Add memfd_create system call wrapper
> > 	* sysdeps/unix/sysv/linux/Makefile [misc] (tests): Add
> > 	tst-memfd_create.
> > 	* sysdeps/unix/sysv/linux/bits/mman-linux.h [__USE_GNU]
> > 	(MFD_CLOEXEC, MFD_ALLOW_SEALING): Define.
> > 	[__USE_GNU] (memfd_create): Declare.
> > 	* sysdeps/unix/sysv/linux/Versions (GLIBC_2.27): Add memfd_create.
> > 	* sysdeps/unix/sysv/linux/syscalls.list (memfd_create): Add.
> > 	* sysdeps/unix/sysv/linux/tst-memfd_create.c: New file.
> > 	* sysdeps/unix/sysv/linux/**.abilist: Update.
> > 	* manual/llio.texi (Memory-mapped I/O): Document memfd_create.
> 
> LGTM with just an update below.
> 
> Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>

Reviewed-by: Christian Brauner <christian.brauner@ubuntu.com>

> 
> > 
> > diff --git a/NEWS b/NEWS
> > index b7281621f4..eca0c00a9b 100644
> > --- a/NEWS
> > +++ b/NEWS
> > @@ -35,6 +35,9 @@ Major new features:
> >    are the same interfaces added in version 2.26 for some platforms where
> >    this format is supported but is not the format of long double.
> >  
> > +* glibc now provides the <sys/memfd.h> header file and the memfd_create
> > +  system call.
> > +
> >  Deprecated and removed features, and other changes affecting compatibility:
> >  
> >  * On GNU/Linux, the obsolete Linux constant PTRACE_SEIZE_DEVEL is no longer
> 
> Ok.
> 
> > diff --git a/manual/llio.texi b/manual/llio.texi
> > index 10ad546723..59eb3af711 100644
> > --- a/manual/llio.texi
> > +++ b/manual/llio.texi
> > @@ -1775,6 +1775,40 @@ the given @var{name} previously created by @code{shm_open}.
> >  On failure @code{errno} is set.
> >  @end deftypefn
> >  
> > +@deftypefn Function int memfd_create (const char *@var{name}, unsigned int flags)
> > +@standards{Linux, sys/mman.h}
> > +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
> > +The @code{memfd_create} function returns a file descriptor which can be
> > +used to create memory mappings using the @code{mmap} function.  It is
> > +similar to the @code{shm_open} function in the sense that these mappings
> > +are not backed by actual files.  However, the descriptor return by
> > +@code{memfd_create} does not correspond to a named object; the
> > +@var{name} argument is used for informative purposes only.  The
> > +descriptor can be passed between processes (for example, over local
> > +domain sockets), and mappings at the same offset refer to the same
> > +memory, or the descriptor can be used to create alias mappings within
> > +the same process.
> > +
> > +The descriptor initially refers to a zero-length file.  Before mappings
> > +can be created which are backed by memory, the file size needs to be
> > +increased with the @code{ftruncate} function.  @xref{File Size}.
> > +
> > +The @var{flags} argument can be a combination of the following flags:
> > +
> > +@table @code
> > +@item MFD_CLOEXEC
> > +The descriptor is created with the @code{O_CLOEXEC} flag.
> > +
> > +@item MFD_ALLOW_SEALING
> > +The descriptor supports the addition of seals using the @code{fcntl}
> > +function.
> > +@end table
> > +
> > +The @code{memfd_create} function is specific to Linux.
> > +
> > +@end deftypefn
> > +
> > +
> >  @node Waiting for I/O
> >  @section Waiting for Input or Output
> >  @cindex waiting for input or output
> 
> Linux 4.14 (749df87bd7be) added a new flag MFD_HUGETLB. I think ww should add
> and document it.
> 
> > diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
> > index bf76b8773d..f974ac446b 100644
> > --- a/sysdeps/unix/sysv/linux/Makefile
> > +++ b/sysdeps/unix/sysv/linux/Makefile
> > @@ -43,7 +43,8 @@ sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \
> >  		  bits/siginfo-arch.h bits/siginfo-consts-arch.h
> >  
> >  tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \
> > -	 tst-quota tst-sync_file_range test-errno-linux tst-sysconf-iov_max
> > +	 tst-quota tst-sync_file_range test-errno-linux tst-sysconf-iov_max \
> > +	 tst-memfd_create
> >  
> >  # Generate the list of SYS_* macros for the system calls (__NR_*
> >  # macros).  The file syscall-names.list contains all possible system
> 
> Ok.
> 
> > diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
> > index d3dbcde8c9..6f2fe516d7 100644
> > --- a/sysdeps/unix/sysv/linux/Versions
> > +++ b/sysdeps/unix/sysv/linux/Versions
> > @@ -166,6 +166,9 @@ libc {
> >    GLIBC_2.15 {
> >      process_vm_readv; process_vm_writev;
> >    }
> > +  GLIBC_2.27 {
> > +    memfd_create;
> > +  }
> >    GLIBC_PRIVATE {
> >      # functions used in other libraries
> >      __syscall_rt_sigqueueinfo;
> 
> Ok.
> 
> > diff --git a/sysdeps/unix/sysv/linux/aarch64/libc.abilist b/sysdeps/unix/sysv/linux/aarch64/libc.abilist
> > index ed598aedac..140ca28abc 100644
> > --- a/sysdeps/unix/sysv/linux/aarch64/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/aarch64/libc.abilist
> > @@ -2106,6 +2106,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.27 strfromf128 F
> >  GLIBC_2.27 strtof128 F
> >  GLIBC_2.27 strtof128_l F
> > diff --git a/sysdeps/unix/sysv/linux/alpha/libc.abilist b/sysdeps/unix/sysv/linux/alpha/libc.abilist
> > index 4e57f36bcf..f698e1b2f4 100644
> > --- a/sysdeps/unix/sysv/linux/alpha/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/alpha/libc.abilist
> > @@ -2017,6 +2017,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.27 strfromf128 F
> >  GLIBC_2.27 strtof128 F
> >  GLIBC_2.27 strtof128_l F
> > diff --git a/sysdeps/unix/sysv/linux/arm/libc.abilist b/sysdeps/unix/sysv/linux/arm/libc.abilist
> > index 5b70e1bfc9..8a8af3e3e4 100644
> > --- a/sysdeps/unix/sysv/linux/arm/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/arm/libc.abilist
> > @@ -107,6 +107,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.4 GLIBC_2.4 A
> >  GLIBC_2.4 _Exit F
> >  GLIBC_2.4 _IO_2_1_stderr_ D 0xa0
> > diff --git a/sysdeps/unix/sysv/linux/bits/mman-linux.h b/sysdeps/unix/sysv/linux/bits/mman-linux.h
> > index b091181960..396829482a 100644
> > --- a/sysdeps/unix/sysv/linux/bits/mman-linux.h
> > +++ b/sysdeps/unix/sysv/linux/bits/mman-linux.h
> > @@ -109,3 +109,18 @@
> >  # define MCL_ONFAULT	4		/* Lock all pages that are
> >  					   faulted in.  */
> >  #endif
> > +
> > +#ifdef __USE_GNU
> > +/* Flags for memfd_create.  */
> > +# define MFD_CLOEXEC 1U
> > +# define MFD_ALLOW_SEALING 2U
> > +
> > +__BEGIN_DECLS
> > +
> > +/* Create a new memory file descriptor.  NAME is a name for debugging.
> > +   FLAGS is a combination of the MFD_* constants.  */
> > +int memfd_create (const char *__name, unsigned int __flags) __THROW;
> > +
> > +__END_DECLS
> > +
> > +#endif /* __USE_GNU */
> 
> As before I think we should include MFD_HUGETLB.
> 
> > diff --git a/sysdeps/unix/sysv/linux/hppa/libc.abilist b/sysdeps/unix/sysv/linux/hppa/libc.abilist
> > index 6a2500a8b3..5b81a6cd7d 100644
> > --- a/sysdeps/unix/sysv/linux/hppa/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/hppa/libc.abilist
> > @@ -1871,6 +1871,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.3 GLIBC_2.3 A
> >  GLIBC_2.3 __ctype_b_loc F
> >  GLIBC_2.3 __ctype_tolower_loc F
> > diff --git a/sysdeps/unix/sysv/linux/i386/libc.abilist b/sysdeps/unix/sysv/linux/i386/libc.abilist
> > index 9ab4e3642a..51ead9e867 100644
> > --- a/sysdeps/unix/sysv/linux/i386/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/i386/libc.abilist
> > @@ -2036,6 +2036,7 @@ GLIBC_2.26 wcstof128_l F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.3 GLIBC_2.3 A
> >  GLIBC_2.3 __ctype_b_loc F
> >  GLIBC_2.3 __ctype_tolower_loc F
> > diff --git a/sysdeps/unix/sysv/linux/ia64/libc.abilist b/sysdeps/unix/sysv/linux/ia64/libc.abilist
> > index 81bb623fe8..78b4ee8d40 100644
> > --- a/sysdeps/unix/sysv/linux/ia64/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/ia64/libc.abilist
> > @@ -1900,6 +1900,7 @@ GLIBC_2.26 wcstof128_l F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.3 GLIBC_2.3 A
> >  GLIBC_2.3 __ctype_b_loc F
> >  GLIBC_2.3 __ctype_tolower_loc F
> > diff --git a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
> > index 5a33b57390..d9c97779e4 100644
> > --- a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
> > @@ -108,6 +108,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.4 GLIBC_2.4 A
> >  GLIBC_2.4 _Exit F
> >  GLIBC_2.4 _IO_2_1_stderr_ D 0x98
> > diff --git a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
> > index 50a86e74fa..4acbf7eeed 100644
> > --- a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
> > @@ -1985,6 +1985,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.3 GLIBC_2.3 A
> >  GLIBC_2.3 __ctype_b_loc F
> >  GLIBC_2.3 __ctype_tolower_loc F
> > diff --git a/sysdeps/unix/sysv/linux/microblaze/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/libc.abilist
> > index 250ef305c3..93f02f08ce 100644
> > --- a/sysdeps/unix/sysv/linux/microblaze/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/microblaze/libc.abilist
> > @@ -2106,3 +2106,4 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> > diff --git a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
> > index 87a1dc4ad7..795e85de70 100644
> > --- a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
> > @@ -1960,6 +1960,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.3 GLIBC_2.3 A
> >  GLIBC_2.3 __ctype_b_loc F
> >  GLIBC_2.3 __ctype_tolower_loc F
> > diff --git a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
> > index f2b35f250e..dc714057b7 100644
> > --- a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
> > @@ -1958,6 +1958,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.3 GLIBC_2.3 A
> >  GLIBC_2.3 __ctype_b_loc F
> >  GLIBC_2.3 __ctype_tolower_loc F
> > diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
> > index ade654dbea..ce7bc9b175 100644
> > --- a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
> > @@ -1956,6 +1956,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.27 strfromf128 F
> >  GLIBC_2.27 strtof128 F
> >  GLIBC_2.27 strtof128_l F
> > diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
> > index 56032c3f82..3fdd85eace 100644
> > --- a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
> > @@ -1951,6 +1951,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.27 strfromf128 F
> >  GLIBC_2.27 strtof128 F
> >  GLIBC_2.27 strtof128_l F
> > diff --git a/sysdeps/unix/sysv/linux/nios2/libc.abilist b/sysdeps/unix/sysv/linux/nios2/libc.abilist
> > index c599dd9212..3e0bcb2a5c 100644
> > --- a/sysdeps/unix/sysv/linux/nios2/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/nios2/libc.abilist
> > @@ -2147,3 +2147,4 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> > diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
> > index 385409aa6e..375c69d9d1 100644
> > --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
> > @@ -1989,6 +1989,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.3 GLIBC_2.3 A
> >  GLIBC_2.3 __ctype_b_loc F
> >  GLIBC_2.3 __ctype_tolower_loc F
> > diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
> > index e99cb454b5..a88172a906 100644
> > --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
> > @@ -1994,6 +1994,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.3 GLIBC_2.3 A
> >  GLIBC_2.3 __ctype_b_loc F
> >  GLIBC_2.3 __ctype_tolower_loc F
> > diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist
> > index 173672ab5e..fa026a332c 100644
> > --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist
> > +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist
> > @@ -2201,3 +2201,4 @@ GLIBC_2.26 wcstof128_l F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> > diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist
> > index 8a654436ab..838f395d78 100644
> > --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist
> > @@ -108,6 +108,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.3 GLIBC_2.3 A
> >  GLIBC_2.3 _Exit F
> >  GLIBC_2.3 _IO_2_1_stderr_ D 0xe0
> > diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
> > index dbd411ceb1..41b79c496a 100644
> > --- a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
> > @@ -1989,6 +1989,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.27 strfromf128 F
> >  GLIBC_2.27 strtof128 F
> >  GLIBC_2.27 strtof128_l F
> > diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
> > index 5617784ca0..68251a0e69 100644
> > --- a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
> > @@ -1890,6 +1890,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.27 strfromf128 F
> >  GLIBC_2.27 strtof128 F
> >  GLIBC_2.27 strtof128_l F
> > diff --git a/sysdeps/unix/sysv/linux/sh/libc.abilist b/sysdeps/unix/sysv/linux/sh/libc.abilist
> > index 0f840e6e88..bc1aae275e 100644
> > --- a/sysdeps/unix/sysv/linux/sh/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/sh/libc.abilist
> > @@ -1875,6 +1875,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.3 GLIBC_2.3 A
> >  GLIBC_2.3 __ctype_b_loc F
> >  GLIBC_2.3 __ctype_tolower_loc F
> > diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
> > index bb7e1042c7..93e6d092ac 100644
> > --- a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
> > @@ -1982,6 +1982,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.27 strfromf128 F
> >  GLIBC_2.27 strtof128 F
> >  GLIBC_2.27 strtof128_l F
> > diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
> > index 4053b0a51c..b11d6764d4 100644
> > --- a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
> > @@ -1919,6 +1919,7 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.27 strfromf128 F
> >  GLIBC_2.27 strtof128 F
> >  GLIBC_2.27 strtof128_l F
> > diff --git a/sysdeps/unix/sysv/linux/syscalls.list b/sysdeps/unix/sysv/linux/syscalls.list
> > index 8bfb080d80..40c4fbb9ea 100644
> > --- a/sysdeps/unix/sysv/linux/syscalls.list
> > +++ b/sysdeps/unix/sysv/linux/syscalls.list
> > @@ -109,3 +109,4 @@ setns		EXTRA	setns		i:ii	setns
> >  
> >  process_vm_readv EXTRA	process_vm_readv i:ipipii process_vm_readv
> >  process_vm_writev EXTRA	process_vm_writev i:ipipii process_vm_writev
> > +memfd_create    EXTRA	memfd_create	i:si    memfd_create
> > diff --git a/sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist b/sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist
> > index 38a96d3a02..e9eb4ff7bd 100644
> > --- a/sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist
> > @@ -2113,3 +2113,4 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> > diff --git a/sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist b/sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist
> > index 572b917d7d..8f08e909cd 100644
> > --- a/sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist
> > @@ -2113,3 +2113,4 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> > diff --git a/sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist b/sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist
> > index 38a96d3a02..e9eb4ff7bd 100644
> > --- a/sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist
> > @@ -2113,3 +2113,4 @@ GLIBC_2.26 reallocarray F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> > diff --git a/sysdeps/unix/sysv/linux/tst-memfd_create.c b/sysdeps/unix/sysv/linux/tst-memfd_create.c
> > new file mode 100644
> > index 0000000000..fd173ff71d
> > --- /dev/null
> > +++ b/sysdeps/unix/sysv/linux/tst-memfd_create.c
> > @@ -0,0 +1,121 @@
> > +/* Test for the memfd_create system call.
> > +   Copyright (C) 2017 Free Software Foundation, Inc.
> > +   This file is part of the GNU C Library.
> > +
> > +   The GNU C Library is free software; you can redistribute it and/or
> > +   modify it under the terms of the GNU Lesser General Public
> > +   License as published by the Free Software Foundation; either
> > +   version 2.1 of the License, or (at your option) any later version.
> > +
> > +   The GNU C Library is distributed in the hope that it will be useful,
> > +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> > +   Lesser General Public License for more details.
> > +
> > +   You should have received a copy of the GNU Lesser General Public
> > +   License along with the GNU C Library; if not, see
> > +   <http://www.gnu.org/licenses/>.  */
> > +
> > +#include <errno.h>
> > +#include <fcntl.h>
> > +#include <stdbool.h>
> > +#include <stdio.h>
> > +#include <stdlib.h>
> > +#include <string.h>
> > +#include <support/check.h>
> > +#include <support/support.h>
> > +#include <support/test-driver.h>
> > +#include <support/xunistd.h>
> > +#include <sys/mman.h>
> > +
> > +/* Return true if the descriptor has the FD_CLOEXEC flag set.  */
> > +static bool
> > +is_cloexec (int fd)
> > +{
> > +  int flags = fcntl (fd, F_GETFD);
> > +  TEST_VERIFY (flags >= 0);
> > +  return flags & FD_CLOEXEC;
> > +}
> > +
> > +/* Return the seals set on FD.  */
> > +static int
> > +get_seals (int fd)
> > +{
> > +  int flags = fcntl (fd, F_GET_SEALS);
> > +  TEST_VERIFY (flags >= 0);
> > +  return flags;
> > +}
> > +
> > +/* Return true if the F_SEAL_SEAL flag is set on the descriptor.  */
> > +static bool
> > +is_sealed (int fd)
> > +{
> > +  return get_seals (fd) & F_SEAL_SEAL;
> > +}
> > +
> > +static int
> > +do_test (void)
> > +{
> > +  /* Initialized by the first call to memfd_create to 0 (memfd_create
> > +     unsupported) or 1 (memfd_create is implemented in the kernel).
> > +     Subsequent iterations check that the success/failure state is
> > +     consistent.  */
> > +  int supported = -1;
> > +
> > +  for (int do_cloexec = 0; do_cloexec < 2; ++do_cloexec)
> > +    for (int do_sealing = 0; do_sealing < 2; ++do_sealing)
> > +      {
> > +        int flags = 0;
> > +        if (do_cloexec)
> > +          flags |= MFD_CLOEXEC;
> > +        if (do_sealing)
> > +          flags |= MFD_ALLOW_SEALING;
> > +        if  (test_verbose > 0)
> > +          printf ("info: memfd_create with flags=0x%x\n", flags);
> > +        int fd = memfd_create ("tst-memfd_create", flags);
> > +        if (fd < 0)
> > +          {
> > +            if (errno == ENOSYS)
> > +              {
> > +                if (supported < 0)
> > +                  {
> > +                    printf ("warning: memfd_create is unsupported\n");
> > +                    supported = 0;
> > +                    continue;
> > +                  }
> > +                TEST_VERIFY (supported == 0);
> > +                continue;
> > +              }
> > +            else
> > +              FAIL_EXIT1 ("memfd_create: %m");
> > +          }
> > +        if (supported < 0)
> > +          supported = 1;
> > +        TEST_VERIFY (supported > 0);
> > +
> > +        char *fd_path = xasprintf ("/proc/self/fd/%d", fd);
> > +        char *link = xreadlink (fd_path);
> > +        if (test_verbose > 0)
> > +          printf ("info: memfd link: %s\n", link);
> > +        TEST_VERIFY (strcmp (link, "memfd:tst-memfd_create (deleted)"));
> > +        TEST_VERIFY (is_cloexec (fd) == do_cloexec);
> > +        TEST_VERIFY (is_sealed (fd) == !do_sealing);
> > +        if (do_sealing)
> > +          {
> > +            TEST_VERIFY (fcntl (fd, F_ADD_SEALS, F_SEAL_WRITE) == 0);
> > +            TEST_VERIFY (!is_sealed (fd));
> > +            TEST_VERIFY (get_seals (fd) & F_SEAL_WRITE);
> > +            TEST_VERIFY (fcntl (fd, F_ADD_SEALS, F_SEAL_SEAL) == 0);
> > +            TEST_VERIFY (is_sealed (fd));
> > +          }
> > +        xclose (fd);
> > +        free (fd_path);
> > +        free (link);
> > +      }
> > +
> > +  if (supported == 0)
> > +    return EXIT_UNSUPPORTED;
> > +  return 0;
> > +}
> > +
> > +#include <support/test-driver.c>
> 
> Ok.
> 
> > diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
> > index b83d25c2e3..0a4f7797ac 100644
> > --- a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
> > @@ -1877,6 +1877,7 @@ GLIBC_2.26 wcstof128_l F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >  GLIBC_2.3 GLIBC_2.3 A
> >  GLIBC_2.3 __ctype_b_loc F
> >  GLIBC_2.3 __ctype_tolower_loc F
> > diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
> > index cba1d59057..23f6a91429 100644
> > --- a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
> > +++ b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
> > @@ -2120,3 +2120,4 @@ GLIBC_2.26 wcstof128_l F
> >  GLIBC_2.27 GLIBC_2.27 A
> >  GLIBC_2.27 glob F
> >  GLIBC_2.27 glob64 F
> > +GLIBC_2.27 memfd_create F
> >
  
Florian Weimer Nov. 17, 2017, 10:20 p.m. UTC | #3
On 11/16/2017 11:34 AM, Adhemerval Zanella wrote:
> Linux 4.14 (749df87bd7be) added a new flag MFD_HUGETLB. I think ww should add
> and document it.

We currently do not document MAP_HUGETLB, which is closely related and 
not really simple to describe.  The manual page and kernel documentation 
do not cover it in detail, either.  The page size selection in 
particular is missing, and I would have to research how an application 
can discover supported hugepage sizes.

In short, this is not exactly a trivial request, and I would kindly ask 
you to reconsider.

Thanks,
Florian
  
Joseph Myers Nov. 17, 2017, 10:41 p.m. UTC | #4
On Fri, 17 Nov 2017, Florian Weimer wrote:

> On 11/16/2017 11:34 AM, Adhemerval Zanella wrote:
> > Linux 4.14 (749df87bd7be) added a new flag MFD_HUGETLB. I think ww should
> > add
> > and document it.
> 
> We currently do not document MAP_HUGETLB, which is closely related and not
> really simple to describe.  The manual page and kernel documentation do not
> cover it in detail, either.  The page size selection in particular is missing,
> and I would have to research how an application can discover supported
> hugepage sizes.
> 
> In short, this is not exactly a trivial request, and I would kindly ask you to
> reconsider.

Maybe say the new flag should at least be added to the header (unless 
automatically visible via an include of a <linux/*.h> UAPI header), absent 
a reason not to do so, but given the difficulties of doing so we might for 
now not document it (generically, for new constants from new Linux kernel 
releases, as opposed to new functions, that's been the practice for a long 
time).
  
Florian Weimer Nov. 17, 2017, 10:53 p.m. UTC | #5
On 11/17/2017 11:41 PM, Joseph Myers wrote:
> On Fri, 17 Nov 2017, Florian Weimer wrote:
> 
>> On 11/16/2017 11:34 AM, Adhemerval Zanella wrote:
>>> Linux 4.14 (749df87bd7be) added a new flag MFD_HUGETLB. I think ww should
>>> add
>>> and document it.
>>
>> We currently do not document MAP_HUGETLB, which is closely related and not
>> really simple to describe.  The manual page and kernel documentation do not
>> cover it in detail, either.  The page size selection in particular is missing,
>> and I would have to research how an application can discover supported
>> hugepage sizes.
>>
>> In short, this is not exactly a trivial request, and I would kindly ask you to
>> reconsider.
> 
> Maybe say the new flag should at least be added to the header (unless
> automatically visible via an include of a <linux/*.h> UAPI header), absent
> a reason not to do so, but given the difficulties of doing so we might for
> now not document it (generically, for new constants from new Linux kernel
> releases, as opposed to new functions, that's been the practice for a long
> time).

Would it be okay to pretend that the hugepage size selection mechanism 
doesn't exist?

We haven't added that yet for SHM_HUGETLB, either.

Thanks,
Florian
  
Joseph Myers Nov. 17, 2017, 11:08 p.m. UTC | #6
On Fri, 17 Nov 2017, Florian Weimer wrote:

> Would it be okay to pretend that the hugepage size selection mechanism doesn't
> exist?

Sure.
  
Adhemerval Zanella Netto Nov. 18, 2017, 12:16 a.m. UTC | #7
> Il giorno 17 nov 2017, alle ore 21:08, Joseph Myers <joseph@codesourcery.com> ha scritto:
> 
>> On Fri, 17 Nov 2017, Florian Weimer wrote:
>> 
>> Would it be okay to pretend that the hugepage size selection mechanism doesn't
>> exist?
> 
> Sure.

Sounds reasonable to me as well.
  

Patch


The system call is somewhat obscure because it is closely related
to file descriptor sealing.  However, it is also the recommended
way to create alias mappings, which is why it has more general use.

No emulation is provided.  Except for the name of the
/proc/self/fd links, it would be possible to implement an
approximation using O_TMPFILE and tmpfs, but this does not appear
to be worth the added complexity.

2017-11-12  Florian Weimer  <fweimer@redhat.com>

	Linux: Add memfd_create system call wrapper
	* sysdeps/unix/sysv/linux/Makefile [misc] (tests): Add
	tst-memfd_create.
	* sysdeps/unix/sysv/linux/bits/mman-linux.h [__USE_GNU]
	(MFD_CLOEXEC, MFD_ALLOW_SEALING): Define.
	[__USE_GNU] (memfd_create): Declare.
	* sysdeps/unix/sysv/linux/Versions (GLIBC_2.27): Add memfd_create.
	* sysdeps/unix/sysv/linux/syscalls.list (memfd_create): Add.
	* sysdeps/unix/sysv/linux/tst-memfd_create.c: New file.
	* sysdeps/unix/sysv/linux/**.abilist: Update.
	* manual/llio.texi (Memory-mapped I/O): Document memfd_create.

diff --git a/NEWS b/NEWS
index b7281621f4..eca0c00a9b 100644
--- a/NEWS
+++ b/NEWS
@@ -35,6 +35,9 @@  Major new features:
   are the same interfaces added in version 2.26 for some platforms where
   this format is supported but is not the format of long double.
 
+* glibc now provides the <sys/memfd.h> header file and the memfd_create
+  system call.
+
 Deprecated and removed features, and other changes affecting compatibility:
 
 * On GNU/Linux, the obsolete Linux constant PTRACE_SEIZE_DEVEL is no longer
diff --git a/manual/llio.texi b/manual/llio.texi
index 10ad546723..59eb3af711 100644
--- a/manual/llio.texi
+++ b/manual/llio.texi
@@ -1775,6 +1775,40 @@  the given @var{name} previously created by @code{shm_open}.
 On failure @code{errno} is set.
 @end deftypefn
 
+@deftypefn Function int memfd_create (const char *@var{name}, unsigned int flags)
+@standards{Linux, sys/mman.h}
+@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
+The @code{memfd_create} function returns a file descriptor which can be
+used to create memory mappings using the @code{mmap} function.  It is
+similar to the @code{shm_open} function in the sense that these mappings
+are not backed by actual files.  However, the descriptor return by
+@code{memfd_create} does not correspond to a named object; the
+@var{name} argument is used for informative purposes only.  The
+descriptor can be passed between processes (for example, over local
+domain sockets), and mappings at the same offset refer to the same
+memory, or the descriptor can be used to create alias mappings within
+the same process.
+
+The descriptor initially refers to a zero-length file.  Before mappings
+can be created which are backed by memory, the file size needs to be
+increased with the @code{ftruncate} function.  @xref{File Size}.
+
+The @var{flags} argument can be a combination of the following flags:
+
+@table @code
+@item MFD_CLOEXEC
+The descriptor is created with the @code{O_CLOEXEC} flag.
+
+@item MFD_ALLOW_SEALING
+The descriptor supports the addition of seals using the @code{fcntl}
+function.
+@end table
+
+The @code{memfd_create} function is specific to Linux.
+
+@end deftypefn
+
+
 @node Waiting for I/O
 @section Waiting for Input or Output
 @cindex waiting for input or output
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index bf76b8773d..f974ac446b 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -43,7 +43,8 @@  sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \
 		  bits/siginfo-arch.h bits/siginfo-consts-arch.h
 
 tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \
-	 tst-quota tst-sync_file_range test-errno-linux tst-sysconf-iov_max
+	 tst-quota tst-sync_file_range test-errno-linux tst-sysconf-iov_max \
+	 tst-memfd_create
 
 # Generate the list of SYS_* macros for the system calls (__NR_*
 # macros).  The file syscall-names.list contains all possible system
diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
index d3dbcde8c9..6f2fe516d7 100644
--- a/sysdeps/unix/sysv/linux/Versions
+++ b/sysdeps/unix/sysv/linux/Versions
@@ -166,6 +166,9 @@  libc {
   GLIBC_2.15 {
     process_vm_readv; process_vm_writev;
   }
+  GLIBC_2.27 {
+    memfd_create;
+  }
   GLIBC_PRIVATE {
     # functions used in other libraries
     __syscall_rt_sigqueueinfo;
diff --git a/sysdeps/unix/sysv/linux/aarch64/libc.abilist b/sysdeps/unix/sysv/linux/aarch64/libc.abilist
index ed598aedac..140ca28abc 100644
--- a/sysdeps/unix/sysv/linux/aarch64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/aarch64/libc.abilist
@@ -2106,6 +2106,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
diff --git a/sysdeps/unix/sysv/linux/alpha/libc.abilist b/sysdeps/unix/sysv/linux/alpha/libc.abilist
index 4e57f36bcf..f698e1b2f4 100644
--- a/sysdeps/unix/sysv/linux/alpha/libc.abilist
+++ b/sysdeps/unix/sysv/linux/alpha/libc.abilist
@@ -2017,6 +2017,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
diff --git a/sysdeps/unix/sysv/linux/arm/libc.abilist b/sysdeps/unix/sysv/linux/arm/libc.abilist
index 5b70e1bfc9..8a8af3e3e4 100644
--- a/sysdeps/unix/sysv/linux/arm/libc.abilist
+++ b/sysdeps/unix/sysv/linux/arm/libc.abilist
@@ -107,6 +107,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.4 GLIBC_2.4 A
 GLIBC_2.4 _Exit F
 GLIBC_2.4 _IO_2_1_stderr_ D 0xa0
diff --git a/sysdeps/unix/sysv/linux/bits/mman-linux.h b/sysdeps/unix/sysv/linux/bits/mman-linux.h
index b091181960..396829482a 100644
--- a/sysdeps/unix/sysv/linux/bits/mman-linux.h
+++ b/sysdeps/unix/sysv/linux/bits/mman-linux.h
@@ -109,3 +109,18 @@ 
 # define MCL_ONFAULT	4		/* Lock all pages that are
 					   faulted in.  */
 #endif
+
+#ifdef __USE_GNU
+/* Flags for memfd_create.  */
+# define MFD_CLOEXEC 1U
+# define MFD_ALLOW_SEALING 2U
+
+__BEGIN_DECLS
+
+/* Create a new memory file descriptor.  NAME is a name for debugging.
+   FLAGS is a combination of the MFD_* constants.  */
+int memfd_create (const char *__name, unsigned int __flags) __THROW;
+
+__END_DECLS
+
+#endif /* __USE_GNU */
diff --git a/sysdeps/unix/sysv/linux/hppa/libc.abilist b/sysdeps/unix/sysv/linux/hppa/libc.abilist
index 6a2500a8b3..5b81a6cd7d 100644
--- a/sysdeps/unix/sysv/linux/hppa/libc.abilist
+++ b/sysdeps/unix/sysv/linux/hppa/libc.abilist
@@ -1871,6 +1871,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
diff --git a/sysdeps/unix/sysv/linux/i386/libc.abilist b/sysdeps/unix/sysv/linux/i386/libc.abilist
index 9ab4e3642a..51ead9e867 100644
--- a/sysdeps/unix/sysv/linux/i386/libc.abilist
+++ b/sysdeps/unix/sysv/linux/i386/libc.abilist
@@ -2036,6 +2036,7 @@  GLIBC_2.26 wcstof128_l F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
diff --git a/sysdeps/unix/sysv/linux/ia64/libc.abilist b/sysdeps/unix/sysv/linux/ia64/libc.abilist
index 81bb623fe8..78b4ee8d40 100644
--- a/sysdeps/unix/sysv/linux/ia64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/ia64/libc.abilist
@@ -1900,6 +1900,7 @@  GLIBC_2.26 wcstof128_l F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
diff --git a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
index 5a33b57390..d9c97779e4 100644
--- a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
+++ b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
@@ -108,6 +108,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.4 GLIBC_2.4 A
 GLIBC_2.4 _Exit F
 GLIBC_2.4 _IO_2_1_stderr_ D 0x98
diff --git a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
index 50a86e74fa..4acbf7eeed 100644
--- a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
+++ b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
@@ -1985,6 +1985,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
diff --git a/sysdeps/unix/sysv/linux/microblaze/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/libc.abilist
index 250ef305c3..93f02f08ce 100644
--- a/sysdeps/unix/sysv/linux/microblaze/libc.abilist
+++ b/sysdeps/unix/sysv/linux/microblaze/libc.abilist
@@ -2106,3 +2106,4 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
diff --git a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
index 87a1dc4ad7..795e85de70 100644
--- a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
@@ -1960,6 +1960,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
diff --git a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
index f2b35f250e..dc714057b7 100644
--- a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
@@ -1958,6 +1958,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
index ade654dbea..ce7bc9b175 100644
--- a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
@@ -1956,6 +1956,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
index 56032c3f82..3fdd85eace 100644
--- a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
@@ -1951,6 +1951,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
diff --git a/sysdeps/unix/sysv/linux/nios2/libc.abilist b/sysdeps/unix/sysv/linux/nios2/libc.abilist
index c599dd9212..3e0bcb2a5c 100644
--- a/sysdeps/unix/sysv/linux/nios2/libc.abilist
+++ b/sysdeps/unix/sysv/linux/nios2/libc.abilist
@@ -2147,3 +2147,4 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
index 385409aa6e..375c69d9d1 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
@@ -1989,6 +1989,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
index e99cb454b5..a88172a906 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
@@ -1994,6 +1994,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist
index 173672ab5e..fa026a332c 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist
@@ -2201,3 +2201,4 @@  GLIBC_2.26 wcstof128_l F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist
index 8a654436ab..838f395d78 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist
@@ -108,6 +108,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 _Exit F
 GLIBC_2.3 _IO_2_1_stderr_ D 0xe0
diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
index dbd411ceb1..41b79c496a 100644
--- a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
@@ -1989,6 +1989,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
index 5617784ca0..68251a0e69 100644
--- a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
@@ -1890,6 +1890,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
diff --git a/sysdeps/unix/sysv/linux/sh/libc.abilist b/sysdeps/unix/sysv/linux/sh/libc.abilist
index 0f840e6e88..bc1aae275e 100644
--- a/sysdeps/unix/sysv/linux/sh/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sh/libc.abilist
@@ -1875,6 +1875,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
index bb7e1042c7..93e6d092ac 100644
--- a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
@@ -1982,6 +1982,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
index 4053b0a51c..b11d6764d4 100644
--- a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
@@ -1919,6 +1919,7 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
diff --git a/sysdeps/unix/sysv/linux/syscalls.list b/sysdeps/unix/sysv/linux/syscalls.list
index 8bfb080d80..40c4fbb9ea 100644
--- a/sysdeps/unix/sysv/linux/syscalls.list
+++ b/sysdeps/unix/sysv/linux/syscalls.list
@@ -109,3 +109,4 @@  setns		EXTRA	setns		i:ii	setns
 
 process_vm_readv EXTRA	process_vm_readv i:ipipii process_vm_readv
 process_vm_writev EXTRA	process_vm_writev i:ipipii process_vm_writev
+memfd_create    EXTRA	memfd_create	i:si    memfd_create
diff --git a/sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist b/sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist
index 38a96d3a02..e9eb4ff7bd 100644
--- a/sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist
@@ -2113,3 +2113,4 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
diff --git a/sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist b/sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist
index 572b917d7d..8f08e909cd 100644
--- a/sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist
@@ -2113,3 +2113,4 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
diff --git a/sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist b/sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist
index 38a96d3a02..e9eb4ff7bd 100644
--- a/sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist
+++ b/sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist
@@ -2113,3 +2113,4 @@  GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
diff --git a/sysdeps/unix/sysv/linux/tst-memfd_create.c b/sysdeps/unix/sysv/linux/tst-memfd_create.c
new file mode 100644
index 0000000000..fd173ff71d
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-memfd_create.c
@@ -0,0 +1,121 @@ 
+/* Test for the memfd_create system call.
+   Copyright (C) 2017 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <support/check.h>
+#include <support/support.h>
+#include <support/test-driver.h>
+#include <support/xunistd.h>
+#include <sys/mman.h>
+
+/* Return true if the descriptor has the FD_CLOEXEC flag set.  */
+static bool
+is_cloexec (int fd)
+{
+  int flags = fcntl (fd, F_GETFD);
+  TEST_VERIFY (flags >= 0);
+  return flags & FD_CLOEXEC;
+}
+
+/* Return the seals set on FD.  */
+static int
+get_seals (int fd)
+{
+  int flags = fcntl (fd, F_GET_SEALS);
+  TEST_VERIFY (flags >= 0);
+  return flags;
+}
+
+/* Return true if the F_SEAL_SEAL flag is set on the descriptor.  */
+static bool
+is_sealed (int fd)
+{
+  return get_seals (fd) & F_SEAL_SEAL;
+}
+
+static int
+do_test (void)
+{
+  /* Initialized by the first call to memfd_create to 0 (memfd_create
+     unsupported) or 1 (memfd_create is implemented in the kernel).
+     Subsequent iterations check that the success/failure state is
+     consistent.  */
+  int supported = -1;
+
+  for (int do_cloexec = 0; do_cloexec < 2; ++do_cloexec)
+    for (int do_sealing = 0; do_sealing < 2; ++do_sealing)
+      {
+        int flags = 0;
+        if (do_cloexec)
+          flags |= MFD_CLOEXEC;
+        if (do_sealing)
+          flags |= MFD_ALLOW_SEALING;
+        if  (test_verbose > 0)
+          printf ("info: memfd_create with flags=0x%x\n", flags);
+        int fd = memfd_create ("tst-memfd_create", flags);
+        if (fd < 0)
+          {
+            if (errno == ENOSYS)
+              {
+                if (supported < 0)
+                  {
+                    printf ("warning: memfd_create is unsupported\n");
+                    supported = 0;
+                    continue;
+                  }
+                TEST_VERIFY (supported == 0);
+                continue;
+              }
+            else
+              FAIL_EXIT1 ("memfd_create: %m");
+          }
+        if (supported < 0)
+          supported = 1;
+        TEST_VERIFY (supported > 0);
+
+        char *fd_path = xasprintf ("/proc/self/fd/%d", fd);
+        char *link = xreadlink (fd_path);
+        if (test_verbose > 0)
+          printf ("info: memfd link: %s\n", link);
+        TEST_VERIFY (strcmp (link, "memfd:tst-memfd_create (deleted)"));
+        TEST_VERIFY (is_cloexec (fd) == do_cloexec);
+        TEST_VERIFY (is_sealed (fd) == !do_sealing);
+        if (do_sealing)
+          {
+            TEST_VERIFY (fcntl (fd, F_ADD_SEALS, F_SEAL_WRITE) == 0);
+            TEST_VERIFY (!is_sealed (fd));
+            TEST_VERIFY (get_seals (fd) & F_SEAL_WRITE);
+            TEST_VERIFY (fcntl (fd, F_ADD_SEALS, F_SEAL_SEAL) == 0);
+            TEST_VERIFY (is_sealed (fd));
+          }
+        xclose (fd);
+        free (fd_path);
+        free (link);
+      }
+
+  if (supported == 0)
+    return EXIT_UNSUPPORTED;
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
index b83d25c2e3..0a4f7797ac 100644
--- a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
@@ -1877,6 +1877,7 @@  GLIBC_2.26 wcstof128_l F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
index cba1d59057..23f6a91429 100644
--- a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
@@ -2120,3 +2120,4 @@  GLIBC_2.26 wcstof128_l F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F