BPF List
 help / color / mirror / Atom feed
* [PATCH v3 bpf-next 0/5] bpf: Import external pages into an arena
@ 2026-12-02 18:23 Andrea Righi
  2026-12-02 18:23 ` [PATCH 1/5] dax: Declare inode_dax() in linux/dax.h Andrea Righi
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Andrea Righi @ 2026-12-02 18:23 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, John Fastabend, Vishal Verma,
	Dave Jiang, Alison Schofield, Matthew Wilcox, Jan Kara, Tejun Heo,
	David Vernet, Changwoo Min, Josh Don, Vineeth Pillai, Peilin Ye,
	Zhi Wang, Lee Trager, Balbir Singh, Shameer Kolothum,
	Himadri Chhaya-Shailesh, NchangRoy, bpf, sched-ext, nvdimm,
	linux-cxl, linux-fsdevel, linux-kernel

As discussed in the BPF track and the sched_ext microconference at Linux
Plumbers 2026 [1][2], shared BPF arenas can provide a generic channel for
bidirectional zero-copy communication between BPF programs running on a host
and its guests, for example to exchange scheduling hints between host and
guest sched_ext schedulers.

This series teaches the arena to map pages that it didn't allocate, based on
Alexei's prototype [3].

Background
==========

An arena can only map pages it allocated itself, so its memory can be shared
only with the BPF programs and the user space that map the arena. A host and a
guest have no such mapping in common: they need an arena that maps memory
supplied by something else, such as the VMM or a paravirt device.

Design
======

The arena gains a single primitive, which maps pages supplied from outside at
a free arena address. Each call creates one import, kept in an interval tree
with its pages, which belong to their supplier, not to the arena:

 - bpf_arena_free_pages() over any of them releases the whole import: the
   pages are unmapped and given back to their supplier, and the range goes
   back to the arena, so the arena doesn't grow as guests come and go,
 - they are released when the arena is destroyed,
 - they are not counted in the arena memory usage,
 - the arena mapping raises SIGBUS on them (SIGSEGV with
   BPF_F_SEGV_ON_FAULT): user space accesses them through their supplier.

There are three ways to supply the pages. Two are sleepable kfuncs for syscall
programs, one for each side of a host/guest shared arena, which work with
existing VMMs:

  int bpf_arena_import_memfd(void *map, void __arena *addr,
                             u32 page_cnt, int fd, u64 off);

Host side: pins the pages of a memfd, or of any file on tmpfs or hugetlbfs,
with memfd_pin_folios() like udmabuf does. The host hands the same file to the
VMM as a memory backend, with no change to the VMM.

  int bpf_arena_import_dax(void *map, void __arena *addr,
                           u32 page_cnt, u64 src);

Guest side: the VMM exposes the file as a virtio-pmem or NVDIMM device, which
the guest turns into a device-dax device (/dev/daxX.Y). The kfunc pins the
device-dax pages mapped at an address of the calling task, and holds a
reference on their dev_pagemap until it releases them. It needs CONFIG_DAX=y.

Both kfuncs act on the current task, its file descriptors or its address space,
so they fail with -EPERM in async callbacks, which may run in another task.

The third one is a kernel API, for suppliers that own the pages, such as a
paravirt transport:

  int bpf_arena_map_ext_pages(struct bpf_map *map, u64 addr,
                              struct page **pages, u32 page_cnt,
                              void (*release)(struct page **pages,
                                              u32 page_cnt, void *priv),
                              void *priv);
  int bpf_arena_unmap_ext_pages(struct bpf_map *map, u64 addr, void *priv);

The arena calls release() once it no longer maps the pages, whatever the
reason. bpf_arena_unmap_ext_pages() revokes them synchronously: when it
returns, BPF programs can no longer access the pages and release() has run.
release() is called under a mutex of the arena, so it must not take a lock
that is held across bpf_arena_unmap_ext_pages(). The only user in this series
is bpf_testmod: the API is meant for transports like pvsched, see the
discussion on v2 [4][5]. Once one is available, bpf_arena_import_dax() may be
deprecated in favor of it.

Both sides then see the same pages as arena memory, at an offset known to both
of them, so they can agree on a layout in-band, with plain BPF loads and
stores, and keep pointers to it in the rest of their arena state.

Use case: paravirt scheduling
=============================

The git tree below has an additional [DEMO] commit, not meant for upstream,
that provides a real, tested use case of this functionality: a pair of
sched_ext schedulers, scx_pvhost on the host and scx_pvguest in every guest,
which share a few pages per VM through their arenas. The host publishes which
vCPUs it has preempted, its idle CPUs and every VM's fair share of the host
CPUs; the guests publish their demand and the state of their vCPUs. With that,
the guests stop queueing work behind preempted vCPUs and fold onto their share
of the host CPUs only while that pays off.

On an NVIDIA Vera host (arm64), with 8-vCPU VMs on QEMU or Firecracker, at 2x
and 6x overcommit, compared to stock VMs, the paravirt pair is 2.2-3x as fast on
hackbench, 1.3-1.9x on a kernel build and serves 1.6-2x the requests of schbench
and nginx; tools/sched_ext/scx-pvsched.md in the [DEMO] commit describes the
design, the results and how to run it.

Known limitations
=================

 - Imports are released whole: freeing part of an import releases all of it.
 - Imported device-dax pages keep the device in use: unbinding it blocks until
   the arena releases them. Revoking them would need a notification from the
   dax core.
 - BPF programs have no machine check recovery: on a real NVDIMM, the supplier
   has to keep bad pages out of the arena.
 - There is no doorbell: the sched_ext use case polls the shared pages at
   scheduling events.
 - Confidential VMs would need the guest to keep the shared pages shared with
   the host, which the device-dax import doesn't handle. A paravirt transport
   can do that before it maps the pages with bpf_arena_map_ext_pages().

References
==========

[1] https://lpc.events/event/20/contributions/2429/
[2] https://lpc.events/event/20/contributions/2482/
[3] https://lore.kernel.org/r/DM6HVNSP3USV.AJSWS289Z3I9@gmail.com/
[4] https://lore.kernel.org/r/CA+HDTgQ0mx0PgXDps2h2kd8TbJsFWOmJGJmDRGEHZgBZ_qSVwQ@mail.gmail.com/
[5] https://github.com/pvsched/linux

Git tree: git://git.kernel.org/pub/scm/linux/kernel/git/arighi/linux.git scx-pvsched

Changes in v3:
 - Make the import kfuncs fail in async callbacks, with bpf_in_async_cb(): from
   a bpf_task_work callback, a program could import a memfd of another task
   (BPF CI bot)
 - Add bpf_arena_map_ext_pages() and bpf_arena_unmap_ext_pages(), to let
   kernel code import its own pages and revoke them synchronously, and release
   all the imports through a callback of their supplier (Vineeth Pillai)
 - Protect the device-dax import from a concurrent unbind with the dax SRCU
   and dax_alive(), instead of get_online_mems(), which could deadlock with
   mmap_lock; this requires CONFIG_DAX=y, the kfunc returns -EOPNOTSUPP
   otherwise (BPF CI bot)
 - Add a preliminary patch that declares inode_dax() in linux/dax.h, needed to
   call dax_alive() on the device of a device-dax mapping
 - Accept tagged addresses in bpf_arena_import_dax() (BPF CI bot)
 - Document that importing a range of a memfd with holes allocates their
   pages, and which memory cgroup is charged for them (BPF CI bot)
 - Link to v2: https://lore.kernel.org/r/20261117103028.4105609-1-arighi@nvidia.com/

Changes in v2:
 - Drop BPF_F_ARENA_NO_FREE, BPF_F_ARENA_EXPORT and the sized bpffs file, and
   map pages that the arena didn't allocate instead (Alexei Starovoitov)
 - Import the host's pages from a memfd with memfd_pin_folios(), the VMM uses
   the same file as a memory backend (Alexei Starovoitov)
 - Replace the guest NUMA node with a device-dax import, so the guest's pages
   never go back to its buddy allocator (Alexei Starovoitov, Josh Don)
 - Release an import with bpf_arena_free_pages(), instead of keeping the arena
   at its high watermark (Alexei Starovoitov, Josh Don)
 - Map the shared pages at an offset known to both sides, instead of
   discovering it out of band (Josh Don)
 - Track imports in an interval tree instead of a second range tree and a
   list, so that a release finds its import without allocating
 - Link to v1: https://lore.kernel.org/r/20261010224902.1232178-1-arighi@nvidia.com/

Andrea Righi (5):
      dax: Declare inode_dax() in linux/dax.h
      bpf: Allow importing memfd pages into an arena
      bpf: Let kernel code import pages into an arena
      bpf: Allow importing device-dax pages into an arena
      selftests/bpf: Test importing pages into an arena

 drivers/dax/dax-private.h                             |   1 -
 include/linux/bpf.h                                   |  21 +
 include/linux/dax.h                                   |   1 +
 kernel/bpf/arena.c                                    | 607 +++++++++++++++++++++-
 tools/testing/selftests/bpf/prog_tests/arena_import.c | 553 ++++++++++++++++++++
 tools/testing/selftests/bpf/progs/arena_import.c      | 170 ++++++
 tools/testing/selftests/bpf/test_kmods/bpf_testmod.c  |  90 ++++
 .../selftests/bpf/test_kmods/bpf_testmod_kfunc.h      |   3 +
 8 files changed, 1432 insertions(+), 14 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_import.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_import.c

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-12-02 18:51 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 18:23 [PATCH v3 bpf-next 0/5] bpf: Import external pages into an arena Andrea Righi
2026-12-02 18:23 ` [PATCH 1/5] dax: Declare inode_dax() in linux/dax.h Andrea Righi
2026-12-02 18:23 ` [PATCH 2/5] bpf: Allow importing memfd pages into an arena Andrea Righi
2026-12-02 18:51   ` sashiko-bot
2026-12-02 18:23 ` [PATCH 3/5] bpf: Let kernel code import " Andrea Righi
2026-12-02 18:41   ` sashiko-bot
2026-12-02 18:23 ` [PATCH 4/5] bpf: Allow importing device-dax " Andrea Righi
2026-12-02 18:23 ` [PATCH 5/5] selftests/bpf: Test importing " Andrea Righi
2026-12-02 18:42   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox