[RFC PATCH 0/5] Introduce FDBox, and preserve memfd with shmem over KHO

Pratyush Yadav
2025-03-07T00:58:46Z

This series introduces the File Descriptor Box (FDBox), along with
support in memfd and shmem for persisting memfds over KHO using FDBox.

FDBox is a mechanism for userspace to name file descriptors and give
them over to the kernel to hold. They can later be retrieved by passing
in the same name. The primary purpose of it is to be used with Kexec
Handover (KHO) [0]. See Documentation/kho/fdbox.rst for more details.

The original idea for FDBox came from Alex Graf. The main problem it
attempts to solve is to give a name to anonymous file descriptors like
memfd, guest_memfd, iommufd, etc. so they can be retrieved after KHO.
Alex wrote some initial code [1] which this series is based upon, though
that code is quite hacky and proof-of-concept, and has been
significantly refactored and polished with this series. Alex's code
mainly played around with KVM, but since I am more familiar with memfd
and shmem, I have picked those as the first users.

That is not to say this series is in a top notch state already. There is
still a lot of room for improvement, both in FDBox and in memfd and
shmem. The aim of the patches is to present the idea to get early
feedback, and to demonstrate KHO in action, potentially having a
consumer of KHO ready by the time those patches are ready for prime
time.

I have written a simple userspace tool to interact with FDBox and memfd.
It can be found here [2]. It is quite simple currently. When given the
create command, it creates a box, and a memfd, and fills the memfd with data
from a file called "data". It then adds the memfd to the box and seals
it. Then one can do KHO. After KHO, the restore command gets the FD out
of the box and writes the output to a file called "out". The original
and new file can be compared to ensure data consistency.

I have tested using the tool and a 1 GiB file, and the memfd came back
over KHO with the same contents. The performance was fast enough to not
be noticeable to the naked eye, though I have not done much more
performance analysis than that.

The whole process can be seen in action in this Asciinema [4].

Sample instructions to use the tool:

$ make
$ dd if=/dev/urandom of=data bs=1G count=1
$ ./fdbox create
$ echo 1 > /sys/kernel/kho/active
$ kexec -s -l [...]
$ kexec -e

After the kexec is done,

$ ./fdbox restore
$ cmp data out
$ echo $?

The full tree with the patches can be found at [3]. It contains a couple
of my patches on top of Mike's KHO patches [0] to fix some small bugs.

[0] https://lore.kernel.org/lkml/20250206132754.2596694-1-rppt@kernel.org/
[1] https://github.com/agraf/linux-2.6/blob/kvm-kho-gmem-test/drivers/misc/fdbox.c
[2] https://github.com/prati0100/fdbox-utils/blob/main/fdbox.c
[3] https://web.git.kernel.org/pub/scm/linux/kernel/git/pratyush/linux.git/log/?h=kho
[4] https://asciinema.org/a/mnyVpy1w67mueIkKZzqHI0oAN

Pratyush Yadav (5):
misc: introduce FDBox
misc: add documentation for FDBox
mm: shmem: allow callers to specify operations to shmem_undo_range
mm: shmem: allow preserving file over FDBOX + KHO
mm/memfd: allow preserving FD over FDBOX + KHO

Documentation/filesystems/locking.rst | 21 +
Documentation/kho/fdbox.rst | 224 ++++++++
Documentation/kho/index.rst | 3 +
MAINTAINERS | 9 +
drivers/misc/Kconfig | 7 +
drivers/misc/Makefile | 1 +
drivers/misc/fdbox.c | 758 ++++++++++++++++++++++++++
include/linux/fdbox.h | 119 ++++
include/linux/fs.h | 7 +
include/linux/miscdevice.h | 1 +
include/linux/shmem_fs.h | 6 +
include/uapi/linux/fdbox.h | 61 +++
mm/memfd.c | 128 ++++-
mm/shmem.c | 498 +++++++++++++++--
14 files changed, 1800 insertions(+), 43 deletions(-)
create mode 100644 Documentation/kho/fdbox.rst
create mode 100644 drivers/misc/fdbox.c
create mode 100644 include/linux/fdbox.h
create mode 100644 include/uapi/linux/fdbox.h

--
2.47.1