Skip to contentSkip to conversation

linux-user: add memfd_create

PATCH Applied Shu-Chun Weng via Qemu-devel 5 participants 10 messages Aug 16, 2019 – Sep 10, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: add memfd_create

Add support for the memfd_create syscall. If the host does not have the libc wrapper, translate to a direct syscall with NC-macro.

---
 include/qemu/memfd.h |  4 ++++
 linux-user/syscall.c | 11 +++++++++++
 util/memfd.c         |  2 +-
 3 files changed, 16 insertions(+), 1 deletion(-)
include/qemu/memfd.h +4 -0
index d551c28b68..975b6bdb77 100644
@@ -32,6 +32,10 @@
 #define MFD_HUGE_SHIFT 26
 #endif
 
+#if defined CONFIG_LINUX && !defined CONFIG_MEMFD
+int memfd_create(const char *name, unsigned int flags);
+#endif
+
 int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
                       uint64_t hugetlbsize, unsigned int seals, Error **errp);
 bool qemu_memfd_alloc_check(void);
linux-user/syscall.c +11 -0
index 8367cb138d..b506c1f40e 100644
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "qemu/cutils.h"
 #include "qemu/path.h"
+#include "qemu/memfd.h"
 #include <elf.h>
 #include <endian.h>
 #include <grp.h>
@@ -11938,6 +11939,16 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         /* PowerPC specific.  */
         return do_swapcontext(cpu_env, arg1, arg2, arg3);
 #endif
+#ifdef TARGET_NR_memfd_create
+    case TARGET_NR_memfd_create:
+        p = lock_user_string(arg1);
+        if (!p) {
+            return -TARGET_EFAULT;
+        }
+        ret = get_errno(memfd_create(p, arg2));
+        unlock_user(p, arg1, 0);
+        return ret;
+#endif
 
     default:
         qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
util/memfd.c +1 -1
index 00334e5b21..4a3c07e0be 100644
@@ -35,7 +35,7 @@
 #include <sys/syscall.h>
 #include <asm/unistd.h>
 
-static int memfd_create(const char *name, unsigned int flags)
+int memfd_create(const char *name, unsigned int flags)
 {
 #ifdef __NR_memfd_create
     return syscall(__NR_memfd_create, name, flags);
Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
#1 lore raw

Cc'ing Marc-André

Quoted 69 lines · Shu-Chun Weng via Qemu-devel

Add support for the memfd_create syscall. If the host does not have the libc wrapper, translate to a direct syscall with NC-macro.

---
 include/qemu/memfd.h |  4 ++++
 linux-user/syscall.c | 11 +++++++++++
 util/memfd.c         |  2 +-
 3 files changed, 16 insertions(+), 1 deletion(-)
include/qemu/memfd.h +4 -0
index d551c28b68..975b6bdb77 100644
@@ -32,6 +32,10 @@
 #define MFD_HUGE_SHIFT 26
 #endif
 
+#if defined CONFIG_LINUX && !defined CONFIG_MEMFD
+int memfd_create(const char *name, unsigned int flags);
+#endif
+
 int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
                       uint64_t hugetlbsize, unsigned int seals, Error **errp);
 bool qemu_memfd_alloc_check(void);
linux-user/syscall.c +11 -0
index 8367cb138d..b506c1f40e 100644
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "qemu/cutils.h"
 #include "qemu/path.h"
+#include "qemu/memfd.h"
 #include <elf.h>
 #include <endian.h>
 #include <grp.h>
@@ -11938,6 +11939,16 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         /* PowerPC specific.  */
         return do_swapcontext(cpu_env, arg1, arg2, arg3);
 #endif
+#ifdef TARGET_NR_memfd_create
+    case TARGET_NR_memfd_create:
+        p = lock_user_string(arg1);
+        if (!p) {
+            return -TARGET_EFAULT;
+        }
+        ret = get_errno(memfd_create(p, arg2));
+        unlock_user(p, arg1, 0);
+        return ret;
+#endif
 
     default:
         qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
util/memfd.c +1 -1
index 00334e5b21..4a3c07e0be 100644
@@ -35,7 +35,7 @@
 #include <sys/syscall.h>
 #include <asm/unistd.h>
 
-static int memfd_create(const char *name, unsigned int flags)
+int memfd_create(const char *name, unsigned int flags)
 {
 #ifdef __NR_memfd_create
     return syscall(__NR_memfd_create, name, flags);
Reviewed-by on v1 1/1
#2 lore raw
In reply to Shu-Chun Weng via Qemu-devel
Quoted 6 lines · Shu-Chun Weng via Qemu-devel

Add support for the memfd_create syscall. If the host does not have the libc wrapper, translate to a direct syscall with NC-macro.

Quoted 67 lines
---
 include/qemu/memfd.h |  4 ++++
 linux-user/syscall.c | 11 +++++++++++
 util/memfd.c         |  2 +-
 3 files changed, 16 insertions(+), 1 deletion(-)
include/qemu/memfd.h +4 -0
index d551c28b68..975b6bdb77 100644
@@ -32,6 +32,10 @@
 #define MFD_HUGE_SHIFT 26
 #endif
 
+#if defined CONFIG_LINUX && !defined CONFIG_MEMFD
+int memfd_create(const char *name, unsigned int flags);
+#endif
+
 int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
                       uint64_t hugetlbsize, unsigned int seals, Error **errp);
 bool qemu_memfd_alloc_check(void);
linux-user/syscall.c +11 -0
index 8367cb138d..b506c1f40e 100644
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "qemu/cutils.h"
 #include "qemu/path.h"
+#include "qemu/memfd.h"
 #include <elf.h>
 #include <endian.h>
 #include <grp.h>
@@ -11938,6 +11939,16 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         /* PowerPC specific.  */
         return do_swapcontext(cpu_env, arg1, arg2, arg3);
 #endif
+#ifdef TARGET_NR_memfd_create
+    case TARGET_NR_memfd_create:
+        p = lock_user_string(arg1);
+        if (!p) {
+            return -TARGET_EFAULT;
+        }
+        ret = get_errno(memfd_create(p, arg2));
+        unlock_user(p, arg1, 0);
+        return ret;
+#endif
 
     default:
         qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
util/memfd.c +1 -2
index 00334e5b21..4a3c07e0be 100644
@@ -35,7 +35,7 @@
 #include <sys/syscall.h>
 #include <asm/unistd.h>
 
-static int memfd_create(const char *name, unsigned int flags)
+int memfd_create(const char *name, unsigned int flags)
 {
 #ifdef __NR_memfd_create
     return syscall(__NR_memfd_create, name, flags);
--

2.23.0.rc1.153.gdeed80330f-goog

Signature · 1 line
Marc-André Lureau
#3 lore raw
In reply to Shu-Chun Weng via Qemu-devel
Quoted 40 lines · Shu-Chun Weng via Qemu-devel

Add support for the memfd_create syscall. If the host does not have the libc wrapper, translate to a direct syscall with NC-macro.

---
 include/qemu/memfd.h |  4 ++++
 linux-user/syscall.c | 11 +++++++++++
 util/memfd.c         |  2 +-
 3 files changed, 16 insertions(+), 1 deletion(-)
include/qemu/memfd.h +4 -0
index d551c28b68..975b6bdb77 100644
@@ -32,6 +32,10 @@
 #define MFD_HUGE_SHIFT 26
 #endif
 
+#if defined CONFIG_LINUX && !defined CONFIG_MEMFD
+int memfd_create(const char *name, unsigned int flags);
+#endif
+
 int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
                       uint64_t hugetlbsize, unsigned int seals, Error **errp);
 bool qemu_memfd_alloc_check(void);
linux-user/syscall.c +1 -0
index 8367cb138d..b506c1f40e 100644
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "qemu/cutils.h"
 #include "qemu/path.h"
+#include "qemu/memfd.h"
 #include <elf.h>
 #include <endian.h>
 #include <grp.h>

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 11948

Quoted 10 lines
linux-user/syscall.c +7 -0
@@ -11938,6 +11939,16 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         /* PowerPC specific.  */
         return do_swapcontext(cpu_env, arg1, arg2, arg3);
 #endif
+#ifdef TARGET_NR_memfd_create
+    case TARGET_NR_memfd_create:
+        p = lock_user_string(arg1);
+        if (!p) {
+            return -TARGET_EFAULT;
+        }
+        ret = get_errno(memfd_create(p, arg2));

I think here you may need to call

           fd_trans_unregister(ret);

since memfd_create() returns a file descriptor. (This call unregisters any pre-existing hooks for "we need to mangle the data for reads/writes of this file descriptor", in case the fd was previously being used for a file descriptor type that needed that.) This is what eg NR_open does.

Laurent -- do I have this right? It's not entirely clear to me that we could ever be in a situation where a syscall like open or memfd_create returns an fd that's got an fd_trans hook active, because that would imply we'd failed to delete the hook on fd-close somehow. Is this just a belt-and-braces bit of coding ?

thanks
-- PMM

#4 lore raw

PATCH v2 1/1 linux-user: add memfd_create

Add support for the memfd_create syscall. If the host does not have the libc wrapper, translate to a direct syscall with NC-macro.

---
 include/qemu/memfd.h |  4 ++++
 linux-user/syscall.c | 11 +++++++++++
 util/memfd.c         |  2 +-
 3 files changed, 16 insertions(+), 1 deletion(-)
include/qemu/memfd.h +4 -0
index d551c28b68..975b6bdb77 100644
@@ -32,6 +32,10 @@
 #define MFD_HUGE_SHIFT 26
 #endif
 
+#if defined CONFIG_LINUX && !defined CONFIG_MEMFD
+int memfd_create(const char *name, unsigned int flags);
+#endif
+
 int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
                       uint64_t hugetlbsize, unsigned int seals, Error **errp);
 bool qemu_memfd_alloc_check(void);
linux-user/syscall.c +11 -0
index 8367cb138d..b506c1f40e 100644
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "qemu/cutils.h"
 #include "qemu/path.h"
+#include "qemu/memfd.h"
 #include <elf.h>
 #include <endian.h>
 #include <grp.h>
@@ -11938,6 +11939,16 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         /* PowerPC specific.  */
         return do_swapcontext(cpu_env, arg1, arg2, arg3);
 #endif
+#ifdef TARGET_NR_memfd_create
+    case TARGET_NR_memfd_create:
+        p = lock_user_string(arg1);
+        if (!p) {
+            return -TARGET_EFAULT;
+        }
+        ret = get_errno(memfd_create(p, arg2));
+        unlock_user(p, arg1, 0);
+        return ret;
+#endif
 
     default:
         qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
util/memfd.c +1 -1
index 00334e5b21..4a3c07e0be 100644
@@ -35,7 +35,7 @@
 #include <sys/syscall.h>
 #include <asm/unistd.h>
 
-static int memfd_create(const char *name, unsigned int flags)
+int memfd_create(const char *name, unsigned int flags)
 {
 #ifdef __NR_memfd_create
     return syscall(__NR_memfd_create, name, flags);
Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
#5 lore raw

PATCH v3 1/1 linux-user: add memfd_create

In reply to Peter Maydell

Add support for the memfd_create syscall. If the host does not have the libc wrapper, translate to a direct syscall with NC-macro.

---
 include/qemu/memfd.h |  4 ++++
 linux-user/syscall.c | 12 ++++++++++++
 util/memfd.c         |  2 +-
 3 files changed, 17 insertions(+), 1 deletion(-)
include/qemu/memfd.h +4 -0
index d551c28b68..975b6bdb77 100644
@@ -32,6 +32,10 @@
 #define MFD_HUGE_SHIFT 26
 #endif
 
+#if defined CONFIG_LINUX && !defined CONFIG_MEMFD
+int memfd_create(const char *name, unsigned int flags);
+#endif
+
 int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
                       uint64_t hugetlbsize, unsigned int seals, Error **errp);
 bool qemu_memfd_alloc_check(void);
linux-user/syscall.c +12 -0
index 8367cb138d..f3f9311e9c 100644
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "qemu/cutils.h"
 #include "qemu/path.h"
+#include "qemu/memfd.h"
 #include <elf.h>
 #include <endian.h>
 #include <grp.h>
@@ -11938,6 +11939,17 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         /* PowerPC specific.  */
         return do_swapcontext(cpu_env, arg1, arg2, arg3);
 #endif
+#ifdef TARGET_NR_memfd_create
+    case TARGET_NR_memfd_create:
+        p = lock_user_string(arg1);
+        if (!p) {
+            return -TARGET_EFAULT;
+        }
+        ret = get_errno(memfd_create(p, arg2));
+        fd_trans_unregister(ret);
+        unlock_user(p, arg1, 0);
+        return ret;
+#endif
 
     default:
         qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
util/memfd.c +1 -1
index 00334e5b21..4a3c07e0be 100644
@@ -35,7 +35,7 @@
 #include <sys/syscall.h>
 #include <asm/unistd.h>
 
-static int memfd_create(const char *name, unsigned int flags)
+int memfd_create(const char *name, unsigned int flags)
 {
 #ifdef __NR_memfd_create
     return syscall(__NR_memfd_create, name, flags);
Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
Reviewed-by on v3 1/1
#6 lore raw

Le 19/08/2019 à 20:09, Shu-Chun Weng via Qemu-devel a écrit :

Quoted 6 lines

Add support for the memfd_create syscall. If the host does not have the libc wrapper, translate to a direct syscall with NC-macro.

---

Please, next time, to help review, add an history here, something like:

v3: add fd_trans_unregister()
v2: no change

And don't send new versions as a reply to the previous ones.

Quoted 56 lines
 include/qemu/memfd.h |  4 ++++
 linux-user/syscall.c | 12 ++++++++++++
 util/memfd.c         |  2 +-
 3 files changed, 17 insertions(+), 1 deletion(-)
include/qemu/memfd.h +4 -0
index d551c28b68..975b6bdb77 100644
@@ -32,6 +32,10 @@
 #define MFD_HUGE_SHIFT 26
 #endif
 
+#if defined CONFIG_LINUX && !defined CONFIG_MEMFD
+int memfd_create(const char *name, unsigned int flags);
+#endif
+
 int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
                       uint64_t hugetlbsize, unsigned int seals, Error **errp);
 bool qemu_memfd_alloc_check(void);
linux-user/syscall.c +12 -0
index 8367cb138d..f3f9311e9c 100644
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "qemu/cutils.h"
 #include "qemu/path.h"
+#include "qemu/memfd.h"
 #include <elf.h>
 #include <endian.h>
 #include <grp.h>
@@ -11938,6 +11939,17 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         /* PowerPC specific.  */
         return do_swapcontext(cpu_env, arg1, arg2, arg3);
 #endif
+#ifdef TARGET_NR_memfd_create
+    case TARGET_NR_memfd_create:
+        p = lock_user_string(arg1);
+        if (!p) {
+            return -TARGET_EFAULT;
+        }
+        ret = get_errno(memfd_create(p, arg2));
+        fd_trans_unregister(ret);
+        unlock_user(p, arg1, 0);
+        return ret;
+#endif
 
     default:
         qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
util/memfd.c +0 -0
index 00334e5b21..4a3c07e0be 100644

util/memfd.c line 41

Quoted 8 lines
util/memfd.c +1 -1
@@ -35,7 +35,7 @@
 #include <sys/syscall.h>
 #include <asm/unistd.h>
 
-static int memfd_create(const char *name, unsigned int flags)
+int memfd_create(const char *name, unsigned int flags)
 {
 #ifdef __NR_memfd_create
     return syscall(__NR_memfd_create, name, flags);
#7 lore raw
In reply to Shu-Chun Weng via Qemu-devel
Quoted 10 lines · Shu-Chun Weng

Add support for the memfd_create syscall. If the host does not have the libc wrapper, translate to a direct syscall with NC-macro.

---
 include/qemu/memfd.h |  4 ++++
 linux-user/syscall.c | 12 ++++++++++++
 util/memfd.c         |  2 +-
 3 files changed, 17 insertions(+), 1 deletion(-)

A nice follow up to this patch would be to implement the file-sealing APIs provided by fnctl (see memfd_create(2) and fcntl(2), File sealing).

Thanks,
Laurent

#8 lore raw
In reply to Peter Maydell

Le 19/08/2019 à 13:56, Peter Maydell a écrit :

Quoted 62 lines
Quoted 40 lines · Shu-Chun Weng via Qemu-devel

Add support for the memfd_create syscall. If the host does not have the libc wrapper, translate to a direct syscall with NC-macro.

---
 include/qemu/memfd.h |  4 ++++
 linux-user/syscall.c | 11 +++++++++++
 util/memfd.c         |  2 +-
 3 files changed, 16 insertions(+), 1 deletion(-)
include/qemu/memfd.h +4 -0
index d551c28b68..975b6bdb77 100644
@@ -32,6 +32,10 @@
 #define MFD_HUGE_SHIFT 26
 #endif
 
+#if defined CONFIG_LINUX && !defined CONFIG_MEMFD
+int memfd_create(const char *name, unsigned int flags);
+#endif
+
 int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
                       uint64_t hugetlbsize, unsigned int seals, Error **errp);
 bool qemu_memfd_alloc_check(void);
linux-user/syscall.c +1 -0
index 8367cb138d..b506c1f40e 100644
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "qemu/cutils.h"
 #include "qemu/path.h"
+#include "qemu/memfd.h"
 #include <elf.h>
 #include <endian.h>
 #include <grp.h>

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 11948

Quoted 10 lines
linux-user/syscall.c +7 -0
@@ -11938,6 +11939,16 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         /* PowerPC specific.  */
         return do_swapcontext(cpu_env, arg1, arg2, arg3);
 #endif
+#ifdef TARGET_NR_memfd_create
+    case TARGET_NR_memfd_create:
+        p = lock_user_string(arg1);
+        if (!p) {
+            return -TARGET_EFAULT;
+        }
+        ret = get_errno(memfd_create(p, arg2));

I think here you may need to call

           fd_trans_unregister(ret);

since memfd_create() returns a file descriptor. (This call unregisters any pre-existing hooks for "we need to mangle the data for reads/writes of this file descriptor", in case the fd was previously being used for a file descriptor type that needed that.) This is what eg NR_open does.

Quoted 6 lines

Laurent -- do I have this right? It's not entirely clear to me that we could ever be in a situation where a syscall like open or memfd_create returns an fd that's got an fd_trans hook active, because that would imply we'd failed to delete the hook on fd-close somehow. Is this just a belt-and-braces bit of coding ?

Exactly.

It's in case we missed one fd close and then to be sure an existing "fd translator" will not mess with data of the new one.

Thanks,
Laurent

#9 lore raw
In reply to Shu-Chun Weng via Qemu-devel

Le 16/08/2019 à 23:10, Shu-Chun Weng via Qemu-devel a écrit :

Quoted 61 lines

Add support for the memfd_create syscall. If the host does not have the libc wrapper, translate to a direct syscall with NC-macro.

---
 include/qemu/memfd.h |  4 ++++
 linux-user/syscall.c | 11 +++++++++++
 util/memfd.c         |  2 +-
 3 files changed, 16 insertions(+), 1 deletion(-)
include/qemu/memfd.h +4 -0
index d551c28b68..975b6bdb77 100644
@@ -32,6 +32,10 @@
 #define MFD_HUGE_SHIFT 26
 #endif
 
+#if defined CONFIG_LINUX && !defined CONFIG_MEMFD
+int memfd_create(const char *name, unsigned int flags);
+#endif
+
 int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
                       uint64_t hugetlbsize, unsigned int seals, Error **errp);
 bool qemu_memfd_alloc_check(void);
linux-user/syscall.c +11 -0
index 8367cb138d..b506c1f40e 100644
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "qemu/cutils.h"
 #include "qemu/path.h"
+#include "qemu/memfd.h"
 #include <elf.h>
 #include <endian.h>
 #include <grp.h>
@@ -11938,6 +11939,16 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         /* PowerPC specific.  */
         return do_swapcontext(cpu_env, arg1, arg2, arg3);
 #endif
+#ifdef TARGET_NR_memfd_create
+    case TARGET_NR_memfd_create:
+        p = lock_user_string(arg1);
+        if (!p) {
+            return -TARGET_EFAULT;
+        }
+        ret = get_errno(memfd_create(p, arg2));
+        unlock_user(p, arg1, 0);
+        return ret;
+#endif
 
     default:
         qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
util/memfd.c +0 -0
index 00334e5b21..4a3c07e0be 100644

util/memfd.c line 41

Quoted 8 lines
util/memfd.c +1 -1
@@ -35,7 +35,7 @@
 #include <sys/syscall.h>
 #include <asm/unistd.h>
 
-static int memfd_create(const char *name, unsigned int flags)
+int memfd_create(const char *name, unsigned int flags)
 {
 #ifdef __NR_memfd_create
     return syscall(__NR_memfd_create, name, flags);

Applied to my linux-user branch.

Thanks,
Laurent