mm: memfd with write notifications
Subsystems (8)
cover mm: memfd with write notifications
I want to propose a kernel facility to have user space create memory regions that can generate notifications on write access. This is useful as a cross-process communication mechanism, where a producer writes to the memory, and a consumer polls for new data to be available.
I'm including a minimalistic proof-of-concept implementation meant as a vehicle to demonstrate the idea and clarify semantics. It works by mapping the memory region read-only, so write accesses will generate page faults. The page_mkwrite handler can thus trigger notifications. It also allows the mappings to become writable temporarily until the mechanism gets rearmed.
Intended usage looks as follows (cf. selftest code included):
1. Call memfd_tripwire to create an instance, ftruncate to configure its size.
2. Pass the file descriptor for mmap()ing to the producer and consumer (potentially across process boundaries).
3a. The producer writes to the memory region whenever there is new
data to publish to the consumer.
3b. The consumer runs a poll loop:
* Wait for POLLIN event.
* ioctl(MEMFD_TRIPWIRE_ACK) to (1) re-arm for subsequent notifications and (2) make sure prior writes are visible.
* Examine the memory region to collect and process the data provided by the producer.
The intention is to guarantee that no change to the memory region can slip through undetected by the consumer. The ACK semantics are instrumental in achieving that. When the ACK returns, we assure the consumer that (possibly concurrent) writes to the region are either visible, or if not they will trigger a subsequent POLLIN event. Note that there is no guarantee that each write will generate an individual event. Neither are any details on the triggering write provided (address, value). The consumer is expected to inspect the memory to find out what has changed. The details of this depend on the communication protocol producer and consumer are following.
A direct consequence of the above semantics is that for a write to be detectable by the consumer, the write must change data in the memory region. Re-writing an already-present value would generally not be detectable. Sequences where a location is written briefly with a changed value and then restored to the previous value can't be reliably detected by the consumer either. I'm calling this out as a noteworthy restriction that will prevent certain usage patterns.
I also want to call attention to an inherent race condition. Page faults signal that a write is being attempted, but obviously they fire before the actual write happens. Thus, write notifications are generated before the write, and race with the write actually taking place. If the consumer wins the race and gets their ACK through before the write manages to land, the consumer could see unchanged memory, and the producer would fault again. This is perfectly compatible with the desired semantics, but creates the risk of a lifelock situation. In practice, it is hopefully exceedingly unlikely for the consumer to win the race consistently so that this can be ignored.
Switching to motivation: Producer / consumer communication can be implemented in many different ways. Pairing shared memory with a notification mechanism such as eventfd gets the job done nicely, but requires the producer to operate an eventfd. This can be undesirable or impractical for cases where the producer is designed to talk to a hardware interface in which a register write also conveys a "doorbell" to trigger hardware processing. The proposed mechanism allows software implementations of the consumer side that behave functionally equivalent to the popular ring buffer + doorbell consumer implementation in hardware. This is particularly useful in contexts where hardware is simulated, for example in VFIO-user server implementations. The tripwire mechanism isn't restricted to that use case though and is generic enough to be useful for other purposes as well.
In terms of related technology, there is some overlap with both userfaultfd and KVM's ioeventfd. userfaultfd has a write protect mode that will generate notifications upon write access. This can be used to construct a similar notification mechanism to the one proposed. However, the producer will be blocked until the consumer resolves the fault and provides a writable page. The consumer will then have to give the producer time to carry out the write before re-arming write protection. Furthermore, userfaultfd is scoped by design to a process / mm, whereas the proposed tripwire mechanism makes the fault handling and notification mechanism a property of the memory region represented by the file descriptor. The latter is simpler to integrate with existing IPC protocols that already exchange file descriptors (such as VFIO-user), avoiding the need for additional setup code in the producer to instantiate a userfaultfd and pass it to the consumer. Also, the tripwire mechanism doesn't make an attempt at providing a generic fault handling framework, which sidesteps the access control complications of userfaultfd when it comes to handling faults generated in kernel context. In contrast to ioeventfd, memfd_tripwire does provide regular memory semantics, whereas ioeventfd discards written values.
There are also a number of design choices / alternatives that warrant consideration. For simplicity, the proof-of-concept implementation generates POLLIN events on the file descriptor representing the memory region. That's somewhat unconventional given that POLLIN is originally meant to indicate a file descriptor's readiness to be read, which is always the case for memory-backed files. An alternative design might associate a separate eventfd at setup time to deliver events to. If we were to deliver events via an eventfd, we could possibly also fold the ACK operation into the read() that clears the eventfd, which might be considered a cleaner API.
Finally, I acknowledge that the proof-of-concept implementation has a number of gaps that would need to be filled in for a production implementation. This includes read and write file_operations, support for sealing and THP, etc. These features are already implemented in shmem, so a production version would likely make most sense as a new shmem feature. I wanted to get high-level feedback on the concept before starting work on that though.
Mattias Nissler (2):
mm: memfd_tripwire proof-of-concept
selftests: memfd_tripwire selftest
arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/syscalls.h | 1 + include/uapi/asm-generic/unistd.h | 5 +- include/uapi/linux/memfd_tripwire.h | 19 + kernel/sys_ni.c | 3 + mm/Kconfig | 9 + mm/Makefile | 1 + mm/memfd_tripwire.c | 246 +++++++ scripts/syscall.tbl | 1 + tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/memfd_tripwire.c | 695 ++++++++++++++++++++ 11 files changed, 981 insertions(+), 1 deletion(-) create mode 100644 include/uapi/linux/memfd_tripwire.h create mode 100644 mm/memfd_tripwire.c create mode 100644 tools/testing/selftests/mm/memfd_tripwire.c
Signature · 1 line
2.52.0
RFC PATCH 1/2 mm: `memfd_tripwire` proof-of-concept
memfd_tripwire() creates a file descriptor referring to a memory region that generates poll notifications when it is written. This works by installing read-only mappings. Write accesses then trigger a fault and invoke the page_mkwrite() handler, which queues a POLLIN event and allows the PTE to be made writable. After observing the POLLIN, the consumer is expected to invoke the MEMFD_TRIPWIRE_ACK ioctl, which write-protects the mappings again to re-arm the mechanism. It also guarantees that previous writes are visible to the consumer.
--- arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/syscalls.h | 1 + include/uapi/asm-generic/unistd.h | 5 +- include/uapi/linux/memfd_tripwire.h | 19 ++ kernel/sys_ni.c | 3 + mm/Kconfig | 9 + mm/Makefile | 1 + mm/memfd_tripwire.c | 246 +++++++++++++++++++++++++ scripts/syscall.tbl | 1 + 9 files changed, 285 insertions(+), 1 deletion(-) create mode 100644 include/uapi/linux/memfd_tripwire.h create mode 100644 mm/memfd_tripwire.c
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -396,6 +396,7 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common memfd_tripwire sys_memfd_tripwire # # Due to a historical design error, certain syscalls are numbered differently
include/linux/syscalls.h +1 -0
@@ -940,6 +940,7 @@ asmlinkage long sys_seccomp(unsigned int op, unsigned int flags,asmlinkage long sys_getrandom (char __user *buf,size_t count,unsigned int flags);asmlinkage long sys_memfd_create (const char __user *uname_ptr,unsigned int flags); +asmlinkage long sys_memfd_tripwire (unsigned int flags);asmlinkage long sys_bpf (int cmd,union bpf_attr __user *attr,unsigned int size); asmlinkage long sys_execveat(int dfd, const char __user *filename, const char __user *const __user *argv,
include/uapi/asm-generic/unistd.h +4 -1
@@ -863,8 +863,11 @@ __SYSCALL(__NR_listns, sys_listns)#define __NR_rseq_slice_yield 471 __SYSCALL(__NR_rseq_slice_yield, sys_rseq_slice_yield) +#define __NR_memfd_tripwire 472 +__SYSCALL(__NR_memfd_tripwire, sys_memfd_tripwire) +#undef __NR_syscalls -#define __NR_syscalls 472 +#define __NR_syscalls 473/* * 32 bit systems traditionally used different
include/uapi/linux/memfd_tripwire.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_LINUX_MEMFD_TRIPWIRE_H +#define _UAPI_LINUX_MEMFD_TRIPWIRE_H + +#include <linux/ioctl.h> + +#define MEMFD_TRIPWIRE_IOC 0xDA + +/* +* MEMFD_TRIPWIRE_ACK serves two purpose: First, it re-arms the mechanism to +* make sure future write activity triggers a POLLIN notification. Second, it +* makes sure that all writes up to the ACK are visible to the calling process. +* It is also guaranteed that no writes can sneak through unnoticed, i.e. after +* ACK concurrent writes have either taken place and are visible to the +* consumer, or will generate subsequent POLLIN events. +*/ +#define MEMFD_TRIPWIRE_ACK _IO(MEMFD_TRIPWIRE_IOC, 0x00) + +#endif /* _UAPI_LINUX_MEMFD_TRIPWIRE_H */
kernel/sys_ni.c +3 -0
@@ -270,6 +270,9 @@ COND_SYSCALL(pkey_free);/* memfd_secret */ COND_SYSCALL (memfd_secret); +/* memfd_tripwire */ +COND_SYSCALL (memfd_tripwire); +/* * Architecture specific weak syscall entries. */
mm/Kconfig +9 -0
@@ -1323,6 +1323,15 @@ config SECRETMEM memory areas visible only in the context of the owning process and not mapped to other processes and other kernel page tables. +config MEMFD_TRIPWIRE + bool "Enable memfd_tripwire() system call" + depends on MMU + help + Enable the memfd_tripwire() system call, which creates an anonymous + shared memory region with built-in write notification support. The + returned file descriptor supports poll() for detecting writes to the + mapped memory and an ioctl for re-arming notifications. + config ANON_VMA_NAME bool "Anonymous VMA name support" depends on PROC_FS && ADVISE_SYSCALLS && MMU
mm/Makefile +1 -0
@@ -141,6 +141,7 @@ obj-$(CONFIG_PERCPU_STATS) += percpu-stats.o obj-$(CONFIG_ZONE_DEVICE) += memremap.o obj-$(CONFIG_HMM_MIRROR) += hmm.o obj-$(CONFIG_MEMFD_CREATE) += memfd.o +obj-$(CONFIG_MEMFD_TRIPWIRE) += memfd_tripwire.o obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += mapping_dirty_helpers.o obj-$(CONFIG_PTDUMP) += ptdump.o obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o
mm/memfd_tripwire.c new file +246 -0
246 lines not on this page · raw message
scripts/syscall.tbl +1 -0
@@ -412,3 +412,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common memfd_tripwire sys_memfd_tripwire
Signature · 1 line
2.52.0
RFC PATCH 2/2 selftests: `memfd_tripwire` selftest
In reply to Mattias NisslerAdd test cases for memfd_tripwire, exercising basic semantics as well as verifying behavior under stress.
--- tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/memfd_tripwire.c | 695 ++++++++++++++++++++ 2 files changed, 696 insertions(+) create mode 100644 tools/testing/selftests/mm/memfd_tripwire.c
tools/testing/selftests/mm/Makefile +1 -0
@@ -106,6 +106,7 @@ TEST_GEN_FILES += guard-regions TEST_GEN_FILES += merge TEST_GEN_FILES += rmap TEST_GEN_FILES += folio_split_race_test +TEST_GEN_FILES += memfd_tripwire ifneq ($(ARCH),arm64) TEST_GEN_FILES += soft-dirty
tools/testing/selftests/mm/memfd_tripwire.c new file +695 -0
695 lines not on this page · raw message
Signature · 1 line
2.52.0
Add more MM core maintainers.
Quoted 56 lines · Mattias Nissler
I want to propose a kernel facility to have user space create memory regions that can generate notifications on write access. This is useful as a cross-process communication mechanism, where a producer writes to the memory, and a consumer polls for new data to be available.
I'm including a minimalistic proof-of-concept implementation meant as a vehicle to demonstrate the idea and clarify semantics. It works by mapping the memory region read-only, so write accesses will generate page faults. The
page_mkwritehandler can thus trigger notifications. It also allows the mappings to become writable temporarily until the mechanism gets rearmed.Intended usage looks as follows (cf. selftest code included):
1. Callmemfd_tripwireto create an instance,ftruncateto configure its size.
2. Pass the file descriptor formmap()ing to the producer and consumer (potentially across process boundaries).
3a. The producer writes to the memory region whenever there is new
data to publish to the consumer.
3b. The consumer runs a poll loop:
* Wait forPOLLINevent.
*ioctl(MEMFD_TRIPWIRE_ACK)to (1) re-arm for subsequent notifications and (2) make sure prior writes are visible.
* Examine the memory region to collect and process the data provided by the producer.The intention is to guarantee that no change to the memory region can slip through undetected by the consumer. The ACK semantics are instrumental in achieving that. When the ACK returns, we assure the consumer that (possibly concurrent) writes to the region are either visible, or if not they will trigger a subsequent
POLLINevent. Note that there is no guarantee that each write will generate an individual event. Neither are any details on the triggering write provided (address, value). The consumer is expected to inspect the memory to find out what has changed. The details of this depend on the communication protocol producer and consumer are following.A direct consequence of the above semantics is that for a write to be detectable by the consumer, the write must change data in the memory region. Re-writing an already-present value would generally not be detectable. Sequences where a location is written briefly with a changed value and then restored to the previous value can't be reliably detected by the consumer either. I'm calling this out as a noteworthy restriction that will prevent certain usage patterns.
I also want to call attention to an inherent race condition. Page faults signal that a write is being attempted, but obviously they fire before the actual write happens. Thus, write notifications are generated before the write, and race with the write actually taking place. If the consumer wins the race and gets their ACK through before the write manages to land, the consumer could see unchanged memory, and the producer would fault again. This is perfectly compatible with the desired semantics, but creates the risk of a lifelock situation. In practice, it is hopefully exceedingly unlikely for the consumer to win the race consistently so that this can be ignored.
Quoted 13 lines
Switching to motivation: Producer / consumer communication can be implemented in many different ways. Pairing shared memory with a notification mechanism such as
eventfdgets the job done nicely, but requires the producer to operate aneventfd. This can be undesirable or impractical for cases where the producer is designed to talk to a hardware interface in which a register write also conveys a "doorbell" to trigger hardware processing. The proposed mechanism allows software implementations of the consumer side that behave functionally equivalent to the popular ring buffer + doorbell consumer implementation in hardware. This is particularly useful in contexts where hardware is simulated, for example in VFIO-user server implementations. The tripwire mechanism isn't restricted to that use case though and is generic enough to be useful for other purposes as well.
From your earlier requirements description, my first thought was also the shmem + eventfd approach. It seems straightforward and avoids introducing a heavy new system call to reimplement functionality that already exists.
Moreover, I haven’t fully understood what hardware constraints you’re referring to here.
Anyway, let’s also see what others think.
Quoted 59 lines
In terms of related technology, there is some overlap with both
userfaultfdand KVM'sioeventfd.userfaultfdhas a write protect mode that will generate notifications upon write access. This can be used to construct a similar notification mechanism to the one proposed. However, the producer will be blocked until the consumer resolves the fault and provides a writable page. The consumer will then have to give the producer time to carry out the write before re-arming write protection. Furthermore,userfaultfdis scoped by design to a process /mm, whereas the proposed tripwire mechanism makes the fault handling and notification mechanism a property of the memory region represented by the file descriptor. The latter is simpler to integrate with existing IPC protocols that already exchange file descriptors (such as VFIO-user), avoiding the need for additional setup code in the producer to instantiate auserfaultfdand pass it to the consumer. Also, the tripwire mechanism doesn't make an attempt at providing a generic fault handling framework, which sidesteps the access control complications ofuserfaultfdwhen it comes to handling faults generated in kernel context. In contrast toioeventfd,memfd_tripwiredoes provide regular memory semantics, whereasioeventfddiscards written values.There are also a number of design choices / alternatives that warrant consideration. For simplicity, the proof-of-concept implementation generates
POLLINevents on the file descriptor representing the memory region. That's somewhat unconventional given thatPOLLINis originally meant to indicate a file descriptor's readiness to be read, which is always the case for memory-backed files. An alternative design might associate a separateeventfdat setup time to deliver events to. If we were to deliver events via aneventfd, we could possibly also fold the ACK operation into theread()that clears theeventfd, which might be considered a cleaner API.Finally, I acknowledge that the proof-of-concept implementation has a number of gaps that would need to be filled in for a production implementation. This includes read and write
file_operations, support for sealing and THP, etc. These features are already implemented inshmem, so a production version would likely make most sense as a newshmemfeature. I wanted to get high-level feedback on the concept before starting work on that though.Mattias Nissler (2):
mm:memfd_tripwireproof-of-concept
selftests:memfd_tripwireselftestarch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/syscalls.h | 1 + include/uapi/asm-generic/unistd.h | 5 +- include/uapi/linux/memfd_tripwire.h | 19 + kernel/sys_ni.c | 3 + mm/Kconfig | 9 + mm/Makefile | 1 + mm/memfd_tripwire.c | 246 +++++++ scripts/syscall.tbl | 1 + tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/memfd_tripwire.c | 695 ++++++++++++++++++++ 11 files changed, 981 insertions(+), 1 deletion(-)create mode 100644
include/uapi/linux/memfd_tripwire.hcreate mode 100644mm/memfd_tripwire.c
create mode 100644tools/testing/selftests/mm/memfd_tripwire.c
Quoted 74 lines · Baolin Wang
Add more MM core maintainers.
Quoted 56 lines · Mattias Nissler
I want to propose a kernel facility to have user space create memory regions that can generate notifications on write access. This is useful as a cross-process communication mechanism, where a producer writes to the memory, and a consumer polls for new data to be available.
I'm including a minimalistic proof-of-concept implementation meant as a vehicle to demonstrate the idea and clarify semantics. It works by mapping the memory region read-only, so write accesses will generate page faults. The
page_mkwritehandler can thus trigger notifications. It also allows the mappings to become writable temporarily until the mechanism gets rearmed.Intended usage looks as follows (cf. selftest code included):
1. Callmemfd_tripwireto create an instance,ftruncateto configure its size.
2. Pass the file descriptor formmap()ing to the producer and consumer (potentially across process boundaries).
3a. The producer writes to the memory region whenever there is new
data to publish to the consumer.
3b. The consumer runs a poll loop:
* Wait forPOLLINevent.
*ioctl(MEMFD_TRIPWIRE_ACK)to (1) re-arm for subsequent notifications and (2) make sure prior writes are visible.
* Examine the memory region to collect and process the data provided by the producer.The intention is to guarantee that no change to the memory region can slip through undetected by the consumer. The ACK semantics are instrumental in achieving that. When the ACK returns, we assure the consumer that (possibly concurrent) writes to the region are either visible, or if not they will trigger a subsequent
POLLINevent. Note that there is no guarantee that each write will generate an individual event. Neither are any details on the triggering write provided (address, value). The consumer is expected to inspect the memory to find out what has changed. The details of this depend on the communication protocol producer and consumer are following.A direct consequence of the above semantics is that for a write to be detectable by the consumer, the write must change data in the memory region. Re-writing an already-present value would generally not be detectable. Sequences where a location is written briefly with a changed value and then restored to the previous value can't be reliably detected by the consumer either. I'm calling this out as a noteworthy restriction that will prevent certain usage patterns.
I also want to call attention to an inherent race condition. Page faults signal that a write is being attempted, but obviously they fire before the actual write happens. Thus, write notifications are generated before the write, and race with the write actually taking place. If the consumer wins the race and gets their ACK through before the write manages to land, the consumer could see unchanged memory, and the producer would fault again. This is perfectly compatible with the desired semantics, but creates the risk of a lifelock situation. In practice, it is hopefully exceedingly unlikely for the consumer to win the race consistently so that this can be ignored.
Quoted 13 lines
Switching to motivation: Producer / consumer communication can be implemented in many different ways. Pairing shared memory with a notification mechanism such as
eventfdgets the job done nicely, but requires the producer to operate aneventfd. This can be undesirable or impractical for cases where the producer is designed to talk to a hardware interface in which a register write also conveys a "doorbell" to trigger hardware processing. The proposed mechanism allows software implementations of the consumer side that behave functionally equivalent to the popular ring buffer + doorbell consumer implementation in hardware. This is particularly useful in contexts where hardware is simulated, for example in VFIO-user server implementations. The tripwire mechanism isn't restricted to that use case though and is generic enough to be useful for other purposes as well.
Quoted 7 lines
From your earlier requirements description, my first thought was also
the shmem + eventfd approach. It seems straightforward and avoids introducing a heavy new system call to reimplement functionality that already exists.Moreover, I haven’t fully understood what hardware constraints you’re referring to here.
To elaborate on this: If you are free to define/adjust the protocol between producer and consumer, and it's not too inconvenient for producer/consumer to exchange the eventfd, then I don't see a good reason why you wouldn't do exactly that. However, VFIO-user models out-of-process PCI devices, and the common case is that the consumer exposes a simulated BAR which contains doorbell registers. The producer side is usually driver code written to work against that hardware interface. The mechanism I'm proposing lets the consumer simulate doorbell registers that behave equivalent to actual hardware. I will note that if the producer runs in a virtual machine, there's the alternative option to have the VMM trap the write and then notify the consumer, but that doesn't work if the producer is a plain user space process.
Also, to make you aware, I've left Meta in the meantime (voluntarily! :-D), and it wasn't clear whether anyone would pick this up. Thus, I suggest we determine whether the proposal adds enough value to stand on its own. If there is interest, I'd be willing to spend some of my own time iterating on this. If there isn't, then I'm happy to put this on ice for now.
Quoted 64 lines
Anyway, let’s also see what others think.
Quoted 59 lines
In terms of related technology, there is some overlap with both
userfaultfdand KVM'sioeventfd.userfaultfdhas a write protect mode that will generate notifications upon write access. This can be used to construct a similar notification mechanism to the one proposed. However, the producer will be blocked until the consumer resolves the fault and provides a writable page. The consumer will then have to give the producer time to carry out the write before re-arming write protection. Furthermore,userfaultfdis scoped by design to a process /mm, whereas the proposed tripwire mechanism makes the fault handling and notification mechanism a property of the memory region represented by the file descriptor. The latter is simpler to integrate with existing IPC protocols that already exchange file descriptors (such as VFIO-user), avoiding the need for additional setup code in the producer to instantiate auserfaultfdand pass it to the consumer. Also, the tripwire mechanism doesn't make an attempt at providing a generic fault handling framework, which sidesteps the access control complications ofuserfaultfdwhen it comes to handling faults generated in kernel context. In contrast toioeventfd,memfd_tripwiredoes provide regular memory semantics, whereasioeventfddiscards written values.There are also a number of design choices / alternatives that warrant consideration. For simplicity, the proof-of-concept implementation generates
POLLINevents on the file descriptor representing the memory region. That's somewhat unconventional given thatPOLLINis originally meant to indicate a file descriptor's readiness to be read, which is always the case for memory-backed files. An alternative design might associate a separateeventfdat setup time to deliver events to. If we were to deliver events via aneventfd, we could possibly also fold the ACK operation into theread()that clears theeventfd, which might be considered a cleaner API.Finally, I acknowledge that the proof-of-concept implementation has a number of gaps that would need to be filled in for a production implementation. This includes read and write
file_operations, support for sealing and THP, etc. These features are already implemented inshmem, so a production version would likely make most sense as a newshmemfeature. I wanted to get high-level feedback on the concept before starting work on that though.Mattias Nissler (2):
mm:memfd_tripwireproof-of-concept
selftests:memfd_tripwireselftestarch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/syscalls.h | 1 + include/uapi/asm-generic/unistd.h | 5 +- include/uapi/linux/memfd_tripwire.h | 19 + kernel/sys_ni.c | 3 + mm/Kconfig | 9 + mm/Makefile | 1 + mm/memfd_tripwire.c | 246 +++++++ scripts/syscall.tbl | 1 + tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/memfd_tripwire.c | 695 ++++++++++++++++++++ 11 files changed, 981 insertions(+), 1 deletion(-)create mode 100644
include/uapi/linux/memfd_tripwire.hcreate mode 100644mm/memfd_tripwire.c
create mode 100644tools/testing/selftests/mm/memfd_tripwire.c
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): CAERLvmQyOAvCN971uUx1PDqTXExOv-BHbNgo-oByaHavUmLgfw@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 20 lines · David Hildenbrand (Arm)
Quoted 18 lines · Mattias Nissler
Quoted 15 lines · David Hildenbrand (Arm)
Quoted 2 lines · Baolin Wang
Add more MM core maintainers.
FWIW, I was raising a couple of times that a userfaultfd-like mechanism on files would likely be helpful. I think we also discussed that in the context of
guest_memfd.For example, for VM live-migration in QEMU with vhost-user processes, we have to play some weird games to protect the shared memory in each and every process, such that we get proper missing-faults.
Background snapshots that uses uffd-wp in QEMU is not supported on shared memory, because the communication with the other processes would be a nightmare.
Quoted 8 lines
Quoted 4 lines
Thanks for bringing up that context. So this is mostly dirty page tracking? I reckon you'll need a way to learn which pages have been written, rather than just a notification if any page in the region gets hit?
Yes, for QEMU to use it as replacement for uffd targeted at multi-process setups (i.e., vhost-user), I think you'd need something similar like uffd, but on the file level, and less uffd-like :)
This was probably a bit tongue-in-cheek, but if I may ask, what aspects of uffd are problematic? Obviously it being process-scoped is a major mismatch for your use case, but are there any other challenges you have in mind?
Quoted 3 lines
But in general, protecting/unprotecting file ranges (read-only, read-write), notifications on access, notifications when filling holes etc.
Do you need notifications to be synchronous (stopping the faulting process) or is asynchronous (firing a notification and write-unprotecting automatically) sufficient?
Quoted 7 lines
uffd gives you that of course, but at the cost of the kernel
Quoted 2 lines
having to perform more bookkeeping. Trying to wrap my head around what a good trade-off for complexity / user space API could be.
Yes. For a simple doorbell mechanism (IIUC your proposal correctly), it feels rather odd to embed it like that in memfd.
So what you have in mind would operate at the file level, but work for any kind of file?
Btw. thanks for bringing a different perspective to this conversation to help explore the design space, this is exactly what I was hoping for.
Quoted 7 lines
I might be missing something important, though.
--
Cheers,David
David Hildenbrand (Arm)
maintainerQuoted 12 lines · Mattias Nissler
Quoted 10 lines · David Hildenbrand (Arm)
Quoted 7 lines · Mattias Nissler
On Mon, Jun 15, 2026 at 7:43 PM David Hildenbrand (Arm) <[email protected]> wrote:
Thanks for bringing up that context. So this is mostly dirty page tracking? I reckon you'll need a way to learn which pages have been written, rather than just a notification if any page in the region gets hit?
Quoted 6 lines
Quoted 3 lines
Yes, for QEMU to use it as replacement for uffd targeted at multi-process setups (i.e., vhost-user), I think you'd need something similar like uffd, but on the file level, and less uffd-like :)
This was probably a bit tongue-in-cheek, but if I may ask, what aspects of uffd are problematic?
Heh, there are several.
Let's ignoring the implementation-wise issues that people keep complaining about. Two (related) problems I am aware of:
1) User-space handling
Right now you always need a user-space handler that POLLs for events to handle them. For each event, you have to context-switch to user space, sometimes a couple of times. Scalability problems (many threads faulting at the same time, in-kernel locking) was raised as a problem in the past.
2) Blocking nature
If you look into the details of the history of UFFD_USER_MODE_ONLY + /proc/sys/vm/unprivileged_userfaultfd, the problem is that userfaultfd can block at various places, some of them possibly being able to hurt the kernel.
We inherently rely on user space to make progress.
Using BPF[1] could avoid both problems in some scenarios, but there are certainly use cases where you would still have to block.
[1] https://dl.acm.org/doi/10.1145/3672197.3673432
Quoted 3 lines
Obviously it being process-scoped is
a major mismatch for your use case, but are there any other challenges you have in mind?
Right, that's a conceptual thing: userfaultfd protects VMA ranges, not file ranges. To emulate protecting file ranges, you have to protect all mmap's in all involved processes.
Obviously, things like read() or write() instead of mmap() cannot be handled by userfaultfd.
Quoted 8 lines
Quoted 4 lines
But in general, protecting/unprotecting file ranges (read-only, read-write), notifications on access, notifications when filling holes etc.
Do you need notifications to be synchronous (stopping the faulting process) or is asynchronous (firing a notification and write-unprotecting automatically) sufficient?
Most use cases I am aware of need to be synchronous. Only some could be relaxed to asynchronous handling.
With postcopy live-migration, you really have to place the page with the right content before the faulting thread can continue running. You cannot just place zero-filled pages. Similarly with CRIU.
With VM background snapshots, you really have to save away page content before un-protecting and modifying the page.
For electric fences[2], it might be sufficient to just detect "wrong page accessed" asynchronously. But it doesn't really work on files.
Protecting unplugged virtio-mem memory in VMs from re-access (sparse memory regions where some parts should no be accessed by the VM) could likely get away with asynchronous handling, but similar to electric fences, synchronous events might be better to debug the "what did actually do something wrong".
I assume garbage collection similarly requires synchronous notifications (but I would suspect that this is usually anonymous memory).
There are some upcoming use cases around working-set tracking [3]. IIRC, asynchronous handling is usually fine (or not requiring a notification at all and instead inspecting the accessed-state).
[2] https://gitlab.com/efency/efency
[3] https://lore.kernel.org/r/[email protected]
Quoted 11 lines
Quoted 8 lines
uffd gives you that of course, but at the cost of the kernel
Quoted 2 lines
having to perform more bookkeeping. Trying to wrap my head around what a good trade-off for complexity / user space API could be.
Yes. For a simple doorbell mechanism (IIUC your proposal correctly), it feels rather odd to embed it like that in memfd.
So what you have in mind would operate at the file level, but work for any kind of file?
VMs with file-backed memory usually rely on shmem/hugetlb/memfd (+guest_memfd in the near future). Other file systems are uncommon.
For CRIU, I could imagine that other file systems could be reasonable, but I don't know enough about how they handle files.
Quoted 4 lines
Btw. thanks for bringing a different perspective to this conversation to help explore the design space, this is exactly what I was hoping for.
Sure! I guess my main point is: most use cases I am aware of would need synchronous handling (and have ways to fix it up, like userfaultfd). For a simple doorbell, this might not really be what you want.
Signature · 3 lines
Cheers, David