mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Andrei Vagin <avagin@gmail.com>
To: musl@lists.openwall.com
Subject: Re: [PATCH] mlock2 and memfd_create
Date: Thu, 21 Jun 2018 17:16:03 -0700	[thread overview]
Message-ID: <20180622001602.GA13372@gmail.com> (raw)
In-Reply-To: <20180619204314.GU4418@port70.net>

On Tue, Jun 19, 2018 at 10:43:14PM +0200, Szabolcs Nagy wrote:
> separate patches for mlock2 and memfd_create.

> From ee3fa6fea375a941f0f11da9a5767f35dd53c6f0 Mon Sep 17 00:00:00 2001
> From: Szabolcs Nagy <nsz@port70.net>
> Date: Sat, 28 Apr 2018 17:25:41 +0000
> Subject: [PATCH 1/2] Add mlock2 linux syscall wrapper
> 
> mlock2 syscall was added in linux v4.4 and glibc has api for it.
> It falls back to mlock in case of flags==0, so that case works
> even on older kernels.
> 
> MLOCK_ONFAULT is moved under _GNU_SOURCE following glibc.
> ---
>  include/sys/mman.h | 11 ++++++++---
>  src/linux/mlock2.c | 10 ++++++++++
>  2 files changed, 18 insertions(+), 3 deletions(-)
>  create mode 100644 src/linux/mlock2.c
> 
> diff --git a/include/sys/mman.h b/include/sys/mman.h
> index 19dd844e..80e1da75 100644
> --- a/include/sys/mman.h
> +++ b/include/sys/mman.h
> @@ -94,6 +94,13 @@ extern "C" {
>  #define MADV_SOFT_OFFLINE 101
>  #endif
>  
> +#ifdef _GNU_SOURCE
> +#define MREMAP_MAYMOVE 1
> +#define MREMAP_FIXED 2
> +
> +#define MLOCK_ONFAULT 0x01
> +#endif
> +
>  #include <bits/mman.h>
>  
>  void *mmap (void *, size_t, int, int, int, off_t);
> @@ -110,14 +117,12 @@ int mlockall (int);
>  int munlockall (void);
>  
>  #ifdef _GNU_SOURCE
> -#define MREMAP_MAYMOVE 1
> -#define MREMAP_FIXED 2
>  void *mremap (void *, size_t, size_t, int, ...);
>  int remap_file_pages (void *, size_t, int, size_t, int);
> +int mlock2 (const void *, size_t, unsigned);
>  #endif
>  
>  #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
> -#define MLOCK_ONFAULT   0x01
>  int madvise (void *, size_t, int);
>  int mincore (void *, size_t, unsigned char *);
>  #endif
> diff --git a/src/linux/mlock2.c b/src/linux/mlock2.c
> new file mode 100644
> index 00000000..10132742
> --- /dev/null
> +++ b/src/linux/mlock2.c
> @@ -0,0 +1,10 @@
> +#define _GNU_SOURCE 1
> +#include <sys/mman.h>
> +#include "syscall.h"
> +
> +int mlock2(const void *addr, size_t len, unsigned flags)
> +{
> +	if (flags == 0)
> +		return mlock(addr, len);
> +	return syscall(SYS_mlock2, addr, len, flags);

I would prefer another way to support old kernels:

	int ret;

	ret = syscall(SYS_mlock2, addr, len, flags);
	if (ret == -1 && errno == ENOSYS && flags == 0)
		return mlock(addr, len);
	return ret;

This way works a bit slower on old kernels, but it doesn't have side
effects if mlock2 is supported.

For example, the user can set seccomp rules, and he will not expect that
the mlock syscall will be executed, when he calls mlock2() in a code.

Thanks,
Andrei

> +}
> -- 
> 2.16.3
> 

> From ed7309bd4156c77d813390ae5b2507719b43face Mon Sep 17 00:00:00 2001
> From: Szabolcs Nagy <nsz@port70.net>
> Date: Tue, 19 Jun 2018 20:28:03 +0000
> Subject: [PATCH 2/2] Add memfd_create syscall wrapper
> 
> memfd_create was added in linux v3.17 and glibc has api for it.
> ---
>  include/sys/mman.h       | 5 +++++
>  src/linux/memfd_create.c | 8 ++++++++
>  2 files changed, 13 insertions(+)
>  create mode 100644 src/linux/memfd_create.c
> 
> diff --git a/include/sys/mman.h b/include/sys/mman.h
> index 80e1da75..99d02a2e 100644
> --- a/include/sys/mman.h
> +++ b/include/sys/mman.h
> @@ -99,6 +99,10 @@ extern "C" {
>  #define MREMAP_FIXED 2
>  
>  #define MLOCK_ONFAULT 0x01
> +
> +#define MFD_CLOEXEC 0x0001U
> +#define MFD_ALLOW_SEALING 0x0002U
> +#define MFD_HUGETLB 0x0004U
>  #endif
>  
>  #include <bits/mman.h>
> @@ -119,6 +123,7 @@ int munlockall (void);
>  #ifdef _GNU_SOURCE
>  void *mremap (void *, size_t, size_t, int, ...);
>  int remap_file_pages (void *, size_t, int, size_t, int);
> +int memfd_create (const char *, unsigned);
>  int mlock2 (const void *, size_t, unsigned);
>  #endif
>  
> diff --git a/src/linux/memfd_create.c b/src/linux/memfd_create.c
> new file mode 100644
> index 00000000..1649fe55
> --- /dev/null
> +++ b/src/linux/memfd_create.c
> @@ -0,0 +1,8 @@
> +#define _GNU_SOURCE 1
> +#include <sys/mman.h>
> +#include "syscall.h"
> +
> +int memfd_create(const char *name, unsigned flags)
> +{
> +	return syscall(SYS_memfd_create, name, flags);
> +}
> -- 
> 2.16.3
> 



  reply	other threads:[~2018-06-22  0:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-19 20:43 Szabolcs Nagy
2018-06-22  0:16 ` Andrei Vagin [this message]
2018-06-22  9:10   ` Szabolcs Nagy
2018-06-22 17:58     ` Andrei Vagin
2018-06-22 18:25       ` Szabolcs Nagy
2018-06-22 19:02     ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180622001602.GA13372@gmail.com \
    --to=avagin@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).