From: Szabolcs Nagy <nsz@port70.net>
To: musl@lists.openwall.com
Subject: [PATCH] mlock2 and memfd_create
Date: Tue, 19 Jun 2018 22:43:14 +0200 [thread overview]
Message-ID: <20180619204314.GU4418@port70.net> (raw)
[-- Attachment #1: Type: text/plain, Size: 46 bytes --]
separate patches for mlock2 and memfd_create.
[-- Attachment #2: 0001-Add-mlock2-linux-syscall-wrapper.patch --]
[-- Type: text/x-diff, Size: 1844 bytes --]
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);
+}
--
2.16.3
[-- Attachment #3: 0002-Add-memfd_create-syscall-wrapper.patch --]
[-- Type: text/x-diff, Size: 1407 bytes --]
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
next reply other threads:[~2018-06-19 20:43 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-19 20:43 Szabolcs Nagy [this message]
2018-06-22 0:16 ` Andrei Vagin
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=20180619204314.GU4418@port70.net \
--to=nsz@port70.net \
--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).