Implement the mlock2 function

Fallback using mlock is provided if the flags argument is zero.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
This commit is contained in:
Florian Weimer 2017-11-27 17:14:29 +01:00
parent a23aa5b727
commit 4bab02240e
37 changed files with 194 additions and 5 deletions

View File

@ -1,3 +1,16 @@
2017-11-27 Florian Weimer <fweimer@redhat.com>
* sysdeps/unix/sysv/linux/mlock2.c: New file.
* sysdeps/unix/sysv/linux/tst-mlock2.c: Likewise.
* sysdeps/unix/sysv/linux/Makefile (routines): Add mlock2.
(tests): Add tst-mlock2.
* sysdeps/unix/sysv/linux/Versions (GLIBC_2.27): Export mlock2.
* sysdeps/unix/sysv/linux/kernel-features.h (__ASSUME_MLOCK2)
[__LINUX_KERNEL_VERSION >= 4.4]: Define.
* sysdeps/unix/sysv/linux/libc**.abilist: Update.
* manual/memory.texi (Page Lock Functions): Move @end deftypefun
for mlock. Document mlock2.
2017-11-27 Joseph Myers <joseph@codesourcery.com>
* sysdeps/ia64/Makeconfig (float64x-alias-fcts): New variable.

2
NEWS
View File

@ -41,7 +41,7 @@ Major new features:
18661-3:2015. These are corresponding interfaces to those supported for
_Float128.
* glibc now implements the memfd_create function on Linux.
* glibc now implements the memfd_create and mlock2 functions on Linux.
Deprecated and removed features, and other changes affecting compatibility:

View File

@ -3337,6 +3337,36 @@ The calling process is not superuser.
The kernel does not provide @code{mlock} capability.
@end table
@end deftypefun
@deftypefun int mlock2 (const void *@var{addr}, size_t @var{len}, unsigned int @var{flags})
@standards{Linux, sys/mman.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
This function is similar to @code{mlock}. If @var{flags} is zero, a
call to @code{mlock2} behaves exactly as the equivalent call to @code{mlock}.
The @var{flags} argument must be a combination of zero or more of the
following flags:
@vtable @code
@item MLOCK_ONFAULT
@standards{Linux, sys/mman.h}
Only those pages in the specified address range which are already in
memory are locked immediately. Additional pages in the range are
automatically locked in case of a page fault and allocation of memory.
@end vtable
Like @code{mlock}, @code{mlock2} returns zero on success and @code{-1}
on failure, setting @code{errno} accordingly. Additional @code{errno}
values defined for @code{mlock2} are:
@table @code
@item EINVAL
The specified (non-zero) @var{flags} argument is not supported by this
system.
@end table
@end deftypefun
You can lock @emph{all} a process' memory with @code{mlockall}. You
unlock memory with @code{munlock} or @code{munlockall}.
@ -3346,8 +3376,6 @@ To avoid all page faults in a C program, you have to use
from the C code, e.g. the stack and automatic variables, and you
wouldn't know what address to tell @code{mlock}.
@end deftypefun
@deftypefun int munlock (const void *@var{addr}, size_t @var{len})
@standards{POSIX.1b, sys/mman.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}

View File

@ -18,7 +18,7 @@ sysdep_routines += clone umount umount2 readahead \
setfsuid setfsgid epoll_pwait signalfd \
eventfd eventfd_read eventfd_write prlimit \
personality epoll_wait tee vmsplice splice \
open_by_handle_at
open_by_handle_at mlock2
CFLAGS-gethostid.c = -fexceptions
CFLAGS-tee.c = -fexceptions -fasynchronous-unwind-tables
@ -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 tst-memfd_create
test-errno-linux tst-memfd_create tst-mlock2
# Generate the list of SYS_* macros for the system calls (__NR_*
# macros). The file syscall-names.list contains all possible system

View File

@ -168,6 +168,7 @@ libc {
}
GLIBC_2.27 {
memfd_create;
mlock2;
}
GLIBC_PRIVATE {
# functions used in other libraries

View File

@ -2107,6 +2107,7 @@ 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 mlock2 F
GLIBC_2.27 strfromf128 F
GLIBC_2.27 strfromf64x F
GLIBC_2.27 strtof128 F

View File

@ -2018,6 +2018,7 @@ 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 mlock2 F
GLIBC_2.27 strfromf128 F
GLIBC_2.27 strfromf64x F
GLIBC_2.27 strtof128 F

View File

@ -108,6 +108,7 @@ 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 mlock2 F
GLIBC_2.4 GLIBC_2.4 A
GLIBC_2.4 _Exit F
GLIBC_2.4 _IO_2_1_stderr_ D 0xa0

View File

@ -28,12 +28,21 @@
# define MFD_HUGETLB 4U
# endif
/* Flags for mlock2. */
# ifndef MLOCK_ONFAULT
# define MLOCK_ONFAULT 1U
# endif
__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;
/* Lock pages from ADDR (inclusive) to ADDR + LENGTH (exclusive) into
memory. FLAGS is a combination of the MLOCK_* flags above. */
int mlock2 (const void *__addr, size_t __length, unsigned int __flags) __THROW;
__END_DECLS
#endif /* __USE_GNU */

View File

@ -1872,6 +1872,7 @@ 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 mlock2 F
GLIBC_2.3 GLIBC_2.3 A
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F

View File

@ -2037,6 +2037,7 @@ 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 mlock2 F
GLIBC_2.27 strfromf64x F
GLIBC_2.27 strtof64x F
GLIBC_2.27 strtof64x_l F

View File

@ -1901,6 +1901,7 @@ 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 mlock2 F
GLIBC_2.27 strfromf64x F
GLIBC_2.27 strtof64x F
GLIBC_2.27 strtof64x_l F

View File

@ -107,3 +107,7 @@
#if __LINUX_KERNEL_VERSION >= 0x031300
# define __ASSUME_EXECVEAT 1
#endif
#if __LINUX_KERNEL_VERSION >= 0x040400
# define __ASSUME_MLOCK2 1
#endif

View File

@ -109,6 +109,7 @@ 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 mlock2 F
GLIBC_2.4 GLIBC_2.4 A
GLIBC_2.4 _Exit F
GLIBC_2.4 _IO_2_1_stderr_ D 0x98

View File

@ -1986,6 +1986,7 @@ 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 mlock2 F
GLIBC_2.3 GLIBC_2.3 A
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F

View File

@ -2107,3 +2107,4 @@ 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 mlock2 F

View File

@ -1961,6 +1961,7 @@ 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 mlock2 F
GLIBC_2.3 GLIBC_2.3 A
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F

View File

@ -1959,6 +1959,7 @@ 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 mlock2 F
GLIBC_2.3 GLIBC_2.3 A
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F

View File

@ -1957,6 +1957,7 @@ 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 mlock2 F
GLIBC_2.27 strfromf128 F
GLIBC_2.27 strfromf64x F
GLIBC_2.27 strtof128 F

View File

@ -1952,6 +1952,7 @@ 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 mlock2 F
GLIBC_2.27 strfromf128 F
GLIBC_2.27 strfromf64x F
GLIBC_2.27 strtof128 F

View File

@ -0,0 +1,40 @@
/* Wrapper for the mlock2 system call with fallback to mlock.
Copyright (C) 2017 Free Software Foundation, Inc.
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 <sys/mman.h>
#include <errno.h>
#include <sysdep.h>
int
mlock2 (const void *addr, size_t length, unsigned int flags)
{
#ifdef __ASSUME_MLOCK2
return INLINE_SYSCALL_CALL (mlock2, addr, length, flags);
#else
if (flags == 0)
return INLINE_SYSCALL_CALL (mlock, addr, length);
# ifdef __NR_mlock2
int ret = INLINE_SYSCALL_CALL (mlock2, addr, length, flags);
if (ret == 0 || errno != ENOSYS)
return ret;
# endif /* __NR_mlock2 */
/* Treat the missing system call as an invalid (non-zero) flag
argument. */
__set_errno (EINVAL);
return -1;
#endif /* __ASSUME_MLOCK2 */
}

View File

@ -2148,3 +2148,4 @@ 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 mlock2 F

View File

@ -1990,6 +1990,7 @@ 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 mlock2 F
GLIBC_2.3 GLIBC_2.3 A
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F

View File

@ -1995,6 +1995,7 @@ 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 mlock2 F
GLIBC_2.3 GLIBC_2.3 A
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F

View File

@ -2202,6 +2202,7 @@ 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 mlock2 F
GLIBC_2.27 strfromf64x F
GLIBC_2.27 strtof64x F
GLIBC_2.27 strtof64x_l F

View File

@ -109,6 +109,7 @@ 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 mlock2 F
GLIBC_2.3 GLIBC_2.3 A
GLIBC_2.3 _Exit F
GLIBC_2.3 _IO_2_1_stderr_ D 0xe0

View File

@ -1990,6 +1990,7 @@ 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 mlock2 F
GLIBC_2.27 strfromf128 F
GLIBC_2.27 strfromf64x F
GLIBC_2.27 strtof128 F

View File

@ -1891,6 +1891,7 @@ 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 mlock2 F
GLIBC_2.27 strfromf128 F
GLIBC_2.27 strfromf64x F
GLIBC_2.27 strtof128 F

View File

@ -1876,6 +1876,7 @@ 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 mlock2 F
GLIBC_2.3 GLIBC_2.3 A
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F

View File

@ -1983,6 +1983,7 @@ 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 mlock2 F
GLIBC_2.27 strfromf128 F
GLIBC_2.27 strfromf64x F
GLIBC_2.27 strtof128 F

View File

@ -1920,6 +1920,7 @@ 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 mlock2 F
GLIBC_2.27 strfromf128 F
GLIBC_2.27 strfromf64x F
GLIBC_2.27 strtof128 F

View File

@ -2114,3 +2114,4 @@ 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 mlock2 F

View File

@ -2114,3 +2114,4 @@ 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 mlock2 F

View File

@ -2114,3 +2114,4 @@ 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 mlock2 F

View File

@ -0,0 +1,66 @@
/* Test the mlock2 function.
Copyright (C) 2017 Free Software Foundation, Inc.
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 <stdio.h>
#include <support/check.h>
#include <support/xunistd.h>
#include <sys/mman.h>
/* Allocate a page using mmap. */
static void *
get_page (void)
{
return xmmap (NULL, 1, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1);
}
static int
do_test (void)
{
/* Current kernels have a small reserve of locked memory, so this
test does not need any privileges to run. */
void *page = get_page ();
if (mlock (page, 1) != 0)
FAIL_EXIT1 ("mlock: %m\n");
xmunmap (page, 1);
page = get_page ();
if (mlock2 (page, 1, 0) != 0)
/* Should be implemented using mlock if necessary. */
FAIL_EXIT1 ("mlock2 (0): %m\n");
xmunmap (page, 1);
page = get_page ();
int ret = mlock2 (page, 1, MLOCK_ONFAULT);
if (ret != 0)
{
TEST_VERIFY (ret == -1);
if (errno != EINVAL)
/* EINVAL means the system does not support the mlock2 system
call. */
FAIL_EXIT1 ("mlock2 (0): %m\n");
else
puts ("warning: mlock2 system call not supported");
}
xmunmap (page, 1);
return 0;
}
#include <support/test-driver.c>

View File

@ -1878,6 +1878,7 @@ 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 mlock2 F
GLIBC_2.27 strfromf64x F
GLIBC_2.27 strtof64x F
GLIBC_2.27 strtof64x_l F

View File

@ -2121,6 +2121,7 @@ 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 mlock2 F
GLIBC_2.27 strfromf64x F
GLIBC_2.27 strtof64x F
GLIBC_2.27 strtof64x_l F