diff options
| author | Florian Weimer <fweimer@redhat.com> | 2017-11-23 10:00:40 +0100 |
|---|---|---|
| committer | Florian Weimer <fweimer@redhat.com> | 2017-11-23 10:00:40 +0100 |
| commit | 59d2cbb1fe4b8601d5cbd359c3806973eab6c62d (patch) | |
| tree | b0dc954009b8a65f9091d88e471db3476da7b2ad | |
| parent | 0a9d1d62b36cc46bf893ce404013a7b2d0bcfce8 (diff) | |
| download | glibc-59d2cbb1fe4b8601d5cbd359c3806973eab6c62d.tar.xz glibc-59d2cbb1fe4b8601d5cbd359c3806973eab6c62d.zip | |
Linux: Add memfd_create system call wrapper
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.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Reviewed-by: Christian Brauner <christian.brauner@ubuntu.com>
36 files changed, 249 insertions, 1 deletions
@@ -1,3 +1,17 @@ +2017-11-23 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. + 2017-11-22 Joseph Myers <joseph@codesourcery.com> * localedata/gen-locale.sh: Fix typo in variable name. @@ -35,6 +35,8 @@ 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 implements the memfd_create function on Linux. + 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 41c3e068d1..8b2f599c79 100644 --- a/manual/llio.texi +++ b/manual/llio.texi @@ -1801,6 +1801,69 @@ the given @var{name} previously created by @code{shm_open}. On failure @code{errno} is set. @end deftypefn +@deftypefun int memfd_create (const char *@var{name}, unsigned int @var{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 returned by +@code{memfd_create} does not correspond to a named object; the +@var{name} argument is used for debugging purposes only (e.g., will +appear in @file{/proc}), and separate invocations of @code{memfd_create} +with the same @var{name} will not return descriptors for the same region +of memory. The descriptor can also 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: + +@vtable @code +@item MFD_CLOEXEC +@standards{Linux, sys/mman.h} +The descriptor is created with the @code{O_CLOEXEC} flag. + +@item MFD_ALLOW_SEALING +@standards{Linux, sys/mman.h} +The descriptor supports the addition of seals using the @code{fcntl} +function. + +@item MFD_HUGETLB +@standards{Linux, sys/mman.h} +This requests that mappings created using the returned file descriptor +use a larger page size. See @code{MAP_HUGETLB} above for details. + +This flag is incompatible with @code{MFD_ALLOW_SEALING}. +@end vtable + +@code{memfd_create} returns a file descriptor on success, and @math{-1} +on failure. + +The following @code{errno} error conditions are defined for this +function: + +@table @code +@item EINVAL +An invalid combination is specified in @var{flags}, or @var{name} is +too long. + +@item EFAULT +The @var{name} argument does not point to a string. + +@item EMFILE +The operation would exceed the file descriptor limit for this process. + +@item ENFILE +The operation would exceed the system-wide file descriptor limit. + +@item ENOMEM +There is not enough memory for the operation. +@end table +@end deftypefun + @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 c6675b3aa5..c484d2688a 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -44,7 +44,7 @@ sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \ tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \ tst-quota tst-sync_file_range tst-sysconf-iov_max tst-ttyname \ - test-errno-linux + test-errno-linux 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 78c07c890a..1ffa5490af 100644 --- a/sysdeps/unix/sysv/linux/bits/mman-linux.h +++ b/sysdeps/unix/sysv/linux/bits/mman-linux.h @@ -111,3 +111,19 @@ # 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 +# define MFD_HUGETLB 4U + +__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/ |
