From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/12924 Path: news.gmane.org!.POSTED!not-for-mail From: Szabolcs Nagy Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH] mlock2 and memfd_create Date: Tue, 19 Jun 2018 22:43:14 +0200 Message-ID: <20180619204314.GU4418@port70.net> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="6BvahUXLYAruDZOj" X-Trace: blaine.gmane.org 1529440883 12499 195.159.176.226 (19 Jun 2018 20:41:23 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 19 Jun 2018 20:41:23 +0000 (UTC) User-Agent: Mutt/1.9.1 (2017-09-22) To: musl@lists.openwall.com Original-X-From: musl-return-12940-gllmg-musl=m.gmane.org@lists.openwall.com Tue Jun 19 22:41:18 2018 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1fVNRG-00039a-1u for gllmg-musl@m.gmane.org; Tue, 19 Jun 2018 22:41:18 +0200 Original-Received: (qmail 13474 invoked by uid 550); 19 Jun 2018 20:43:26 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 13436 invoked from network); 19 Jun 2018 20:43:26 -0000 Mail-Followup-To: musl@lists.openwall.com Content-Disposition: inline Xref: news.gmane.org gmane.linux.lib.musl.general:12924 Archived-At: --6BvahUXLYAruDZOj Content-Type: text/plain; charset=us-ascii Content-Disposition: inline separate patches for mlock2 and memfd_create. --6BvahUXLYAruDZOj Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Add-mlock2-linux-syscall-wrapper.patch" >From ee3fa6fea375a941f0f11da9a5767f35dd53c6f0 Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy 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 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 +#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 --6BvahUXLYAruDZOj Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0002-Add-memfd_create-syscall-wrapper.patch" >From ed7309bd4156c77d813390ae5b2507719b43face Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy 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 @@ -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 +#include "syscall.h" + +int memfd_create(const char *name, unsigned flags) +{ + return syscall(SYS_memfd_create, name, flags); +} -- 2.16.3 --6BvahUXLYAruDZOj--