Skip to contentSkip to conversation

Remote mapping

RFC PATCH In review Adalbert Lazăr 2 participants 9 messages Sep 3, 2020 – Sep 4, 2020

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

cover Remote mapping

This patchset adds support for the remote mapping feature. Remote mapping, as its name suggests, is a means for transparent and zero-copy access of a remote process' address space. access of a remote process' address space.

The feature was designed according to a specification suggested by Paolo Bonzini:

Quoted 45 lines
Quoted 45 lines

The proposed API is a new pidfd system call, through which the parent can map portions of its virtual address space into a file descriptor and then pass that file descriptor to a child.

This should be:

- upstreamable, pidfd is the new cool thing and we could sell it as a better way to do PTRACE_{PEEK,POKE}DATA

- relatively easy to do based on the bitdefender remote process mapping patches at.

- pidfd_mem() takes a pidfd and some flags (which are 0) and returns two file descriptors for respectively the control plane and the memory access.

- the control plane accepts three ioctls

PIDFD_MEM_MAP takes a struct like

    struct pidfd_mem_map {
         uint64_t address;
         off_t offset;
         off_t size;
         int flags;
         int padding[7];
    }

After this is done, the memory access fd can be mmap-ed at range [offset,
offset+size), and it will read memory from range [address, address+size) of the target descriptor.

PIDFD_MEM_UNMAP takes a struct like

    struct pidfd_mem_unmap {
         off_t offset;
         off_t size;
    }

and unmaps the corresponding range of course.

Finally PIDFD_MEM_LOCK forbids subsequent PIDFD_MEM_MAP or PIDFD_MEM_UNMAP. For now I think it should just check that the argument is zero, bells and whistles can be added later.

Quoted 5 lines
Quoted 5 lines

- the memory access fd can be mmap-ed as in the bitdefender patches but also accessed with read/write/pread/pwrite/... As in the BitDefender patches, MMU notifiers can be used to adjust any mmap-ed regions when the source address space changes. In this case, PIDFD_MEM_UNMAP could also cause a pre-existing mmap to "disappear".

(it currently doesn't support read/write/pread/pwrite/...)

The main remote mapping patch also contains the legacy implementation which creates a region the size of the whole process address space by means of the REMOTE_PROC_MAP ioctl. The user is then free to mmap() any region of the address space it wishes.

VMAs obtained by mmap()ing memory access fds mirror the contents of the remote process address space within the specified range. Pages are installed in the current process page tables at fault time and removed by the mmu_interval_notifier invalidate callbck. No further memory management is involved.
On attempts to access a hole, or if a mapping was removed by PIDFD_MEM_UNMAP, or if the remote process address space was reaped by OOM, the remote mapping fault handler returns VM_FAULT_SIGBUS.

At Bitdefender we are using remote mapping for virtual machine introspection:
- the QEMU running the introspected machine creates the pair of file descriptors, passes the access fd to the introspector QEMU, and uses the control fd to allow access to the memslots it creates for its machine
- the QEMU running the introspector machine receives the access fd and mmap()s the regions made available, then hotplugs the obtained memory in its machine Having this setup creates nested invalidate_range_start/end MMU notifier calls.

Patch organization:
- patch 1 allows unmap_page_range() to run without rescheduling Needed for remote mapping to zap current process page tables when OOM calls mmu_notifier_invalidate_range_start_nonblock(&range)

- patch 2 creates VMA-specific zapping behavior
A remote mapping VMA does not own the pages it maps, so all it has to do is clear the PTEs.

- patch 3 removed MMU notifier lockdep map
It was just incompatible with our use case.

- patch 4 is the remote mapping implementation

- patch 5 adds suggested pidfd_mem system call

Mircea Cirjaliu (5):
mm: add atomic capability to zap_details
mm: let the VMA decide how zap_pte_range() acts on mapped pages mm/mmu_notifier: remove lockdep map, allow mmu notifier to be used in
nested scenarios
mm/remote_mapping: use a pidfd to access memory belonging to unrelated
process
pidfd_mem: implemented remote memory mapping system call

 arch/x86/entry/syscalls/syscall_32.tbl |    1 +
 arch/x86/entry/syscalls/syscall_64.tbl |    1 +
 include/linux/mm.h                     |   22 +
 include/linux/mmu_notifier.h           |    5 +-
 include/linux/pid.h                    |    1 +
 include/linux/remote_mapping.h         |   22 +
 include/linux/syscalls.h               |    1 +
 include/uapi/asm-generic/unistd.h      |    2 +
 include/uapi/linux/remote_mapping.h    |   36 +
 kernel/exit.c                          |    2 +-
 kernel/pid.c                           |   55 +
 mm/Kconfig                             |   11 +
 mm/Makefile                            |    1 +
 mm/memory.c                            |  193 ++--
 mm/mmu_notifier.c                      |   19 -
 mm/remote_mapping.c                    | 1273 ++++++++++++++++++++++++
 16 files changed, 1535 insertions(+), 110 deletions(-)
 create mode 100644 include/linux/remote_mapping.h
 create mode 100644 include/uapi/linux/remote_mapping.h
 create mode 100644 mm/remote_mapping.c

CC:Christian Brauner <[email protected]>
base-commit: ae83d0b416db002fe95601e7f97f64b59514d936

#1 lore raw

RFC PATCH 1/5 mm: add atomic capability to zap_details

From: Mircea Cirjaliu <[email protected]>

Force zap_xxx_range() functions to loop without rescheduling. Useful for unmapping memory in an atomic context, although no checks for atomic context are being made.

---
 include/linux/mm.h |  6 ++++++
 mm/memory.c        | 11 +++++++----
 2 files changed, 13 insertions(+), 4 deletions(-)
include/linux/mm.h +6 -0
index 5a323422d783..1be4482a7b81 100644
@@ -1601,8 +1601,14 @@ struct zap_details {
 	struct address_space *check_mapping;	/* Check page->mapping if set */
 	pgoff_t	first_index;			/* Lowest page->index to unmap */
 	pgoff_t last_index;			/* Highest page->index to unmap */
+	bool atomic;				/* Do not sleep. */
 };
 
+static inline bool zap_is_atomic(struct zap_details *details)
+{
+	return (unlikely(details) && details->atomic);
+}
+
 struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
 			     pte_t pte);
 struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
mm/memory.c +7 -4
index f703fe8c8346..8e78fb151f8f 100644
@@ -1056,7 +1056,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
 		if (pte_none(ptent))
 			continue;
 
-		if (need_resched())
+		if (!zap_is_atomic(details) && need_resched())
 			break;
 
 		if (pte_present(ptent)) {
@@ -1159,7 +1159,8 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
 	}
 
 	if (addr != end) {
-		cond_resched();
+		if (!zap_is_atomic(details))
+			cond_resched();
 		goto again;
 	}
 
@@ -1195,7 +1196,8 @@ static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
 			goto next;
 		next = zap_pte_range(tlb, vma, pmd, addr, next, details);
 next:
-		cond_resched();
+		if (!zap_is_atomic(details))
+			cond_resched();
 	} while (pmd++, addr = next, addr != end);
 
 	return addr;
@@ -1224,7 +1226,8 @@ static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
 			continue;
 		next = zap_pmd_range(tlb, vma, pud, addr, next, details);
 next:
-		cond_resched();
+		if (!zap_is_atomic(details))
+			cond_resched();
 	} while (pud++, addr = next, addr != end);
 
 	return addr;
#2 lore raw

RFC PATCH 2/5 mm: let the VMA decide how zap_pte_range() acts on mapped pages

In reply to Adalbert Lazăr

From: Mircea Cirjaliu <[email protected]>

Instead of having one big function to handle all cases of page unmapping, have multiple implementation-defined callbacks, each for its own VMA type. In the future, exotic VMA implementations won't have to bloat the unique zapping function with another case of mappings.

---
 include/linux/mm.h |  16 ++++
 mm/memory.c        | 182 +++++++++++++++++++++++++--------------------
 2 files changed, 116 insertions(+), 82 deletions(-)
include/linux/mm.h +16 -0
index 1be4482a7b81..39e55467aa49 100644
@@ -36,6 +36,7 @@ struct file_ra_state;
 struct user_struct;
 struct writeback_control;
 struct bdi_writeback;
+struct zap_details;
 
 void init_mm_internals(void);
 
@@ -601,6 +602,14 @@ struct vm_operations_struct {
 	 */
 	struct page *(*find_special_page)(struct vm_area_struct *vma,
 					  unsigned long addr);
+
+	/*
+	 * Called by zap_pte_range() for use by special VMAs that implement
+	 * custom zapping behavior.
+	 */
+	int (*zap_pte)(struct vm_area_struct *vma, unsigned long addr,
+		       pte_t *pte, int rss[], struct mmu_gather *tlb,
+		       struct zap_details *details);
 };
 
 static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm)
@@ -1594,6 +1603,13 @@ static inline bool can_do_mlock(void) { return false; }
 extern int user_shm_lock(size_t, struct user_struct *);
 extern void user_shm_unlock(size_t, struct user_struct *);
 
+/*
+ * Flags returned by zap_pte implementations
+ */
+#define ZAP_PTE_CONTINUE	0
+#define ZAP_PTE_FLUSH		(1 << 0)	/* Ask for TLB flush. */
+#define ZAP_PTE_BREAK		(1 << 1)	/* Break PTE iteration. */
+
 /*
  * Parameter block passed down to zap_pte_range in exceptional cases.
  */
mm/memory.c +100 -82

218 lines not on this page · raw message

#3 lore raw

RFC PATCH 3/5 mm/mmu_notifier: remove lockdep map, allow mmu notifier to be used in nested scenarios

In reply to Adalbert Lazăr

From: Mircea Cirjaliu <[email protected]>

The combination of remote mapping + KVM causes nested range invalidations, which reports lockdep warnings.

---
 include/linux/mmu_notifier.h |  5 +----
 mm/mmu_notifier.c            | 19 -------------------
 2 files changed, 1 insertion(+), 23 deletions(-)
include/linux/mmu_notifier.h +1 -4
index 736f6918335e..81ea457d41be 100644
@@ -440,12 +440,10 @@ mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
 {
 	might_sleep();
 
-	lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
 	if (mm_has_notifiers(range->mm)) {
 		range->flags |= MMU_NOTIFIER_RANGE_BLOCKABLE;
 		__mmu_notifier_invalidate_range_start(range);
 	}
-	lock_map_release(&__mmu_notifier_invalidate_range_start_map);
 }
 
 static inline int
@@ -453,12 +451,11 @@ mmu_notifier_invalidate_range_start_nonblock(struct mmu_notifier_range *range)
 {
 	int ret = 0;
 
-	lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
 	if (mm_has_notifiers(range->mm)) {
 		range->flags &= ~MMU_NOTIFIER_RANGE_BLOCKABLE;
 		ret = __mmu_notifier_invalidate_range_start(range);
 	}
-	lock_map_release(&__mmu_notifier_invalidate_range_start_map);
+
 	return ret;
 }
 
mm/mmu_notifier.c +0 -19
index 06852b896fa6..928751bd8630 100644
@@ -22,12 +22,6 @@
 /* global SRCU for all MMs */
 DEFINE_STATIC_SRCU(srcu);
 
-#ifdef CONFIG_LOCKDEP
-struct lockdep_map __mmu_notifier_invalidate_range_start_map = {
-	.name = "mmu_notifier_invalidate_range_start"
-};
-#endif
-
 /*
  * The mmu_notifier_subscriptions structure is allocated and installed in
  * mm->notifier_subscriptions inside the mm_take_all_locks() protected
@@ -242,8 +236,6 @@ mmu_interval_read_begin(struct mmu_interval_notifier *interval_sub)
 	 * will always clear the below sleep in some reasonable time as
 	 * subscriptions->invalidate_seq is even in the idle state.
 	 */
-	lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
-	lock_map_release(&__mmu_notifier_invalidate_range_start_map);
 	if (is_invalidating)
 		wait_event(subscriptions->wq,
 			   READ_ONCE(subscriptions->invalidate_seq) != seq);
@@ -572,13 +564,11 @@ void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range,
 	struct mmu_notifier_subscriptions *subscriptions =
 		range->mm->notifier_subscriptions;
 
-	lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
 	if (subscriptions->has_itree)
 		mn_itree_inv_end(subscriptions);
 
 	if (!hlist_empty(&subscriptions->list))
 		mn_hlist_invalidate_end(subscriptions, range, only_end);
-	lock_map_release(&__mmu_notifier_invalidate_range_start_map);
 }
 
 void __mmu_notifier_invalidate_range(struct mm_struct *mm,
@@ -612,13 +602,6 @@ int __mmu_notifier_register(struct mmu_notifier *subscription,
 	lockdep_assert_held_write(&mm->mmap_sem);
 	BUG_ON(atomic_read(&mm->mm_users) <= 0);
 
-	if (IS_ENABLED(CONFIG_LOCKDEP)) {
-		fs_reclaim_acquire(GFP_KERNEL);
-		lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
-		lock_map_release(&__mmu_notifier_invalidate_range_start_map);
-		fs_reclaim_release(GFP_KERNEL);
-	}
-
 	if (!mm->notifier_subscriptions) {
 		/*
 		 * kmalloc cannot be called under mm_take_all_locks(), but we
@@ -1062,8 +1045,6 @@ void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub)
 	 * The possible sleep on progress in the invalidation requires the
 	 * caller not hold any locks held by invalidation callbacks.
 	 */
-	lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
-	lock_map_release(&__mmu_notifier_invalidate_range_start_map);
 	if (seq)
 		wait_event(subscriptions->wq,
 			   READ_ONCE(subscriptions->invalidate_seq) != seq);
#4 lore raw

RFC PATCH 4/5 mm/remote_mapping: use a pidfd to access memory belonging to unrelated process

In reply to Adalbert Lazăr

From: Mircea Cirjaliu <[email protected]>

Remote mapping creates a mirror VMA that exposes memory owned by another process in a zero-copy manner. The pages are mapped in the current process' address space with no memory management involved and little impact on the remote process operation. Currently incompatible with THP.

---
 include/linux/remote_mapping.h      |   22 +
 include/uapi/linux/remote_mapping.h |   36 +
 mm/Kconfig                          |   11 +
 mm/Makefile                         |    1 +
 mm/remote_mapping.c                 | 1273 +++++++++++++++++++++++++++
 5 files changed, 1343 insertions(+)
 create mode 100644 include/linux/remote_mapping.h
 create mode 100644 include/uapi/linux/remote_mapping.h
 create mode 100644 mm/remote_mapping.c
include/linux/remote_mapping.h new file +22 -0
new file mode 100644
index 000000000000..5c1d43e8f669
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _LINUX_REMOTE_MAPPING_H
+#define _LINUX_REMOTE_MAPPING_H
+
+#include <linux/sched.h>
+
+#ifdef CONFIG_REMOTE_MAPPING
+
+extern int task_remote_map(struct task_struct *task, int fds[]);
+
+#else /* CONFIG_REMOTE_MAPPING */
+
+static inline int task_remote_map(struct task_struct *task, int fds[])
+{
+	return -EINVAL;
+}
+
+#endif /* CONFIG_REMOTE_MAPPING */
+
+
+#endif /* _LINUX_REMOTE_MAPPING_H */
include/uapi/linux/remote_mapping.h new file +36 -0
new file mode 100644
index 000000000000..5d2828a6aa47
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+
+#ifndef __UAPI_REMOTE_MAPPING_H__
+#define __UAPI_REMOTE_MAPPING_H__
+
+#include <linux/types.h>
+#include <linux/ioctl.h>
+
+// device file interface
+#define REMOTE_PROC_MAP	_IOW('r', 0x01, int)
+
+
+// pidfd interface
+#define PIDFD_IO_MAGIC	'p'
+
+struct pidfd_mem_map {
+	uint64_t address;
+	off_t offset;
+	off_t size;
+	int flags;
+	int padding[7];
+};
+
+struct pidfd_mem_unmap {
+	off_t offset;
+	off_t size;
+};
+
+#define PIDFD_MEM_MAP	_IOW(PIDFD_IO_MAGIC, 0x01, struct pidfd_mem_map)
+#define PIDFD_MEM_UNMAP _IOW(PIDFD_IO_MAGIC, 0x02, struct pidfd_mem_unmap)
+#define PIDFD_MEM_LOCK	_IOW(PIDFD_IO_MAGIC, 0x03, int)
+
+#define PIDFD_MEM_REMAP _IOW(PIDFD_IO_MAGIC, 0x04, unsigned long)
+// TODO: actually this is not for pidfd, find better names
+
+#endif /* __UAPI_REMOTE_MAPPING_H__ */
mm/Kconfig +11 -0
index c1acc34c1c35..0ecc3f41a98e 100644
@@ -804,6 +804,17 @@ config HMM_MIRROR
 	bool
 	depends on MMU
 
+config REMOTE_MAPPING
+	bool "Remote memory mapping"
+	depends on X86_64 && MMU && !TRANSPARENT_HUGEPAGE
+	select MMU_NOTIFIER
+	default n
+	help
+	  Allows a client process to gain access to an unrelated process'
+	  address space on a range-basis. The feature maps pages found at
+	  the remote equivalent address in the current process' page tables
+	  in a lightweight manner.
+
 config DEVICE_PRIVATE
 	bool "Unaddressable device memory (GPU memory, ...)"
 	depends on ZONE_DEVICE
mm/Makefile +1 -0
index fccd3756b25f..ce1a00e7bc8c 100644
@@ -112,3 +112,4 @@ obj-$(CONFIG_MEMFD_CREATE) += memfd.o
 obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += mapping_dirty_helpers.o
 obj-$(CONFIG_PTDUMP_CORE) += ptdump.o
 obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o
+obj-$(CONFIG_REMOTE_MAPPING) += remote_mapping.o
mm/remote_mapping.c new file +1273 -0

1,273 lines not on this page · raw message

#5 lore raw

RFC PATCH 5/5 pidfd_mem: implemented remote memory mapping system call

In reply to Adalbert Lazăr

From: Mircea Cirjaliu <[email protected]>

This system call returns 2 fds for inspecting the address space of a remote process: one for control and one for access. Use according to remote mapping specifications.

---
 arch/x86/entry/syscalls/syscall_32.tbl |  1 +
 arch/x86/entry/syscalls/syscall_64.tbl |  1 +
 include/linux/pid.h                    |  1 +
 include/linux/syscalls.h               |  1 +
 include/uapi/asm-generic/unistd.h      |  2 +
 kernel/exit.c                          |  2 +-
 kernel/pid.c                           | 55 ++++++++++++++++++++++++++
 7 files changed, 62 insertions(+), 1 deletion(-)
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index 54581ac671b4..ca1b5a32dbc5 100644
@@ -440,5 +440,6 @@
 433	i386	fspick			sys_fspick
 434	i386	pidfd_open		sys_pidfd_open
 435	i386	clone3			sys_clone3
+436     i386    pidfd_mem               sys_pidfd_mem
 437	i386	openat2			sys_openat2
 438	i386	pidfd_getfd		sys_pidfd_getfd
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index 37b844f839bc..6138d3d023f8 100644
@@ -357,6 +357,7 @@
 433	common	fspick			sys_fspick
 434	common	pidfd_open		sys_pidfd_open
 435	common	clone3			sys_clone3
+436     common  pidfd_mem               sys_pidfd_mem
 437	common	openat2			sys_openat2
 438	common	pidfd_getfd		sys_pidfd_getfd
 
include/linux/pid.h +1 -0
index cc896f0fc4e3..9ec23ab23fd4 100644
@@ -76,6 +76,7 @@ extern const struct file_operations pidfd_fops;
 
 struct file;
 
+extern struct pid *pidfd_get_pid(unsigned int fd);
 extern struct pid *pidfd_pid(const struct file *file);
 
 static inline struct pid *get_pid(struct pid *pid)
include/linux/syscalls.h +1 -0
index 1815065d52f3..621f3d52ed4e 100644
@@ -934,6 +934,7 @@ asmlinkage long sys_clock_adjtime32(clockid_t which_clock,
 asmlinkage long sys_syncfs(int fd);
 asmlinkage long sys_setns(int fd, int nstype);
 asmlinkage long sys_pidfd_open(pid_t pid, unsigned int flags);
+asmlinkage long sys_pidfd_mem(int pidfd, int __user *fds, unsigned int flags);
 asmlinkage long sys_sendmmsg(int fd, struct mmsghdr __user *msg,
 			     unsigned int vlen, unsigned flags);
 asmlinkage long sys_process_vm_readv(pid_t pid,
include/uapi/asm-generic/unistd.h +2 -0
index 3a3201e4618e..2663afc03c86 100644
@@ -850,6 +850,8 @@ __SYSCALL(__NR_pidfd_open, sys_pidfd_open)
 #define __NR_clone3 435
 __SYSCALL(__NR_clone3, sys_clone3)
 #endif
+#define __NR_pidfd_mem 436
+__SYSCALL(__NR_pidfd_mem, sys_pidfd_mem)
 
 #define __NR_openat2 437
 __SYSCALL(__NR_openat2, sys_openat2)
kernel/exit.c +1 -1
index 389a88cb3081..37cd8949e606 100644
@@ -1464,7 +1464,7 @@ static long do_wait(struct wait_opts *wo)
 	return retval;
 }
 
-static struct pid *pidfd_get_pid(unsigned int fd)
+struct pid *pidfd_get_pid(unsigned int fd)
 {
 	struct fd f;
 	struct pid *pid;
kernel/pid.c +55 -0

67 lines not on this page · raw message

#6 lore raw
In reply to Adalbert Lazăr

CC+= Mihai, Mircea

Quoted 128 lines · Adalbert Lazăr

This patchset adds support for the remote mapping feature. Remote mapping, as its name suggests, is a means for transparent and zero-copy access of a remote process' address space. access of a remote process' address space.

The feature was designed according to a specification suggested by Paolo Bonzini:

Quoted 45 lines
Quoted 45 lines

The proposed API is a new pidfd system call, through which the parent can map portions of its virtual address space into a file descriptor and then pass that file descriptor to a child.

This should be:

- upstreamable, pidfd is the new cool thing and we could sell it as a better way to do PTRACE_{PEEK,POKE}DATA

- relatively easy to do based on the bitdefender remote process mapping patches at.

- pidfd_mem() takes a pidfd and some flags (which are 0) and returns two file descriptors for respectively the control plane and the memory access.

- the control plane accepts three ioctls

PIDFD_MEM_MAP takes a struct like

    struct pidfd_mem_map {
         uint64_t address;
         off_t offset;
         off_t size;
         int flags;
         int padding[7];
    }

After this is done, the memory access fd can be mmap-ed at range [offset,
offset+size), and it will read memory from range [address, address+size) of the target descriptor.

PIDFD_MEM_UNMAP takes a struct like

    struct pidfd_mem_unmap {
         off_t offset;
         off_t size;
    }

and unmaps the corresponding range of course.

Finally PIDFD_MEM_LOCK forbids subsequent PIDFD_MEM_MAP or PIDFD_MEM_UNMAP. For now I think it should just check that the argument is zero, bells and whistles can be added later.

Quoted 5 lines
Quoted 5 lines

- the memory access fd can be mmap-ed as in the bitdefender patches but also accessed with read/write/pread/pwrite/... As in the BitDefender patches, MMU notifiers can be used to adjust any mmap-ed regions when the source address space changes. In this case, PIDFD_MEM_UNMAP could also cause a pre-existing mmap to "disappear".

(it currently doesn't support read/write/pread/pwrite/...)

The main remote mapping patch also contains the legacy implementation which creates a region the size of the whole process address space by means of the REMOTE_PROC_MAP ioctl. The user is then free to mmap() any region of the address space it wishes.

VMAs obtained by mmap()ing memory access fds mirror the contents of the remote process address space within the specified range. Pages are installed in the current process page tables at fault time and removed by the mmu_interval_notifier invalidate callbck. No further memory management is involved.
On attempts to access a hole, or if a mapping was removed by PIDFD_MEM_UNMAP, or if the remote process address space was reaped by OOM, the remote mapping fault handler returns VM_FAULT_SIGBUS.

At Bitdefender we are using remote mapping for virtual machine introspection:
- the QEMU running the introspected machine creates the pair of file descriptors, passes the access fd to the introspector QEMU, and uses the control fd to allow access to the memslots it creates for its machine
- the QEMU running the introspector machine receives the access fd and mmap()s the regions made available, then hotplugs the obtained memory in its machine Having this setup creates nested invalidate_range_start/end MMU notifier calls.

Patch organization:
- patch 1 allows unmap_page_range() to run without rescheduling Needed for remote mapping to zap current process page tables when OOM calls mmu_notifier_invalidate_range_start_nonblock(&range)

- patch 2 creates VMA-specific zapping behavior
A remote mapping VMA does not own the pages it maps, so all it has to do is clear the PTEs.

- patch 3 removed MMU notifier lockdep map
It was just incompatible with our use case.

- patch 4 is the remote mapping implementation

- patch 5 adds suggested pidfd_mem system call

Mircea Cirjaliu (5):
mm: add atomic capability to zap_details
mm: let the VMA decide how zap_pte_range() acts on mapped pages mm/mmu_notifier: remove lockdep map, allow mmu notifier to be used in
nested scenarios
mm/remote_mapping: use a pidfd to access memory belonging to unrelated
process
pidfd_mem: implemented remote memory mapping system call

 arch/x86/entry/syscalls/syscall_32.tbl |    1 +
 arch/x86/entry/syscalls/syscall_64.tbl |    1 +
 include/linux/mm.h                     |   22 +
 include/linux/mmu_notifier.h           |    5 +-
 include/linux/pid.h                    |    1 +
 include/linux/remote_mapping.h         |   22 +
 include/linux/syscalls.h               |    1 +
 include/uapi/asm-generic/unistd.h      |    2 +
 include/uapi/linux/remote_mapping.h    |   36 +
 kernel/exit.c                          |    2 +-
 kernel/pid.c                           |   55 +
 mm/Kconfig                             |   11 +
 mm/Makefile                            |    1 +
 mm/memory.c                            |  193 ++--
 mm/mmu_notifier.c                      |   19 -
 mm/remote_mapping.c                    | 1273 ++++++++++++++++++++++++
 16 files changed, 1535 insertions(+), 110 deletions(-)
 create mode 100644 include/linux/remote_mapping.h
 create mode 100644 include/uapi/linux/remote_mapping.h
 create mode 100644 mm/remote_mapping.c

CC:Christian Brauner <[email protected]>
base-commit: ae83d0b416db002fe95601e7f97f64b59514d936

#7 lore raw
In reply to Adalbert Lazăr
Quoted 4 lines · Adalbert Lazăr

This patchset adds support for the remote mapping feature. Remote mapping, as its name suggests, is a means for transparent and zero-copy access of a remote process' address space. access of a remote process' address space.

Hey Adalbert,

Thanks for the patch. When you resend this patch series, could you please make sure that everyone Cced on any individual patch receives the full patch series? I only got patch 5/5 and it's a bit annoying because one completely lacks context of what's going on. I first thought "did someone just add a syscall with 3 lines of commit message?". :)

Could you please resend the patch series with linux-api, me and the following people Cced:

Andy Lutomirski <[email protected]>
Arnd Bergmann <[email protected]>
Sargun Dhillon <[email protected]>
Aleksa Sarai <[email protected]>
Oleg Nesterov <[email protected]>
Jann Horn <[email protected]>
Kees Cook <[email protected]>
Matthew Wilcox <[email protected]>
[email protected]

Christian

#8 lore raw
Quoted 8 lines · Christian Brauner
Quoted 4 lines · Adalbert Lazăr

This patchset adds support for the remote mapping feature. Remote mapping, as its name suggests, is a means for transparent and zero-copy access of a remote process' address space. access of a remote process' address space.

Hey Adalbert,

Quoted 8 lines

Thanks for the patch. When you resend this patch series, could you please make sure that everyone Cced on any individual patch receives the full patch series? I only got patch 5/5 and it's a bit annoying because one completely lacks context of what's going on. I first thought "did someone just add a syscall with 3 lines of commit message?". :)

Could you please resend the patch series with linux-api, me and the following people Cced:

Done :D
Thank you, Christian