mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrea Righi <arighi@nvidia.com>
To: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	Tejun Heo <tj@kernel.org>, David Vernet <void@manifault.com>,
	Changwoo Min <changwoo@igalia.com>, Josh Don <joshdon@google.com>,
	Vineeth Pillai <vineethrp@google.com>,
	Peilin Ye <yepeilin@google.com>, Zhi Wang <zhiw@nvidia.com>,
	Lee Trager <ltrager@nvidia.com>,
	Balbir Singh <balbirs@nvidia.com>,
	Shameer Kolothum <skolothumtho@nvidia.com>,
	Himadri Chhaya-Shailesh <himadrics@protonmail.com>,
	NchangRoy <royfru44@gmail.com>,
	bpf@vger.kernel.org, sched-ext@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 bpf-next 0/3] bpf: Import external pages into an arena
Date: Tue, 17 Nov 2026 11:28:05 +0100	[thread overview]
Message-ID: <20261117103028.4105609-1-arighi@nvidia.com> (raw)

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.

v1 [3] exposed a pinned arena as a sized bpffs file and gave the guest its
share of it as a NUMA node. Following the review, this version drops that
approach and teaches the arena to map pages that it didn't allocate, based on
Alexei's prototype [4].

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.

Sharing it with a guest through a NUMA node, as in v1, has two fundamental
problems: the node is ordinary guest RAM, so the guest arena's pages go back to
the guest's buddy allocator when the guest scheduler exits, while the host can
still write to them; and memory block granularity makes it wasteful (16M of
guest memory for one shared page) and limits it to a few hundred guests per
host scheduler.

Design
======

The arena gains a single primitive, arena_map_ext_pages(), 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 unpinned, 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.

Two sleepable kfuncs for syscall programs supply the pages, one for each side
of a host/guest shared arena:

  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.

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 and a way to tell the BPF programs that the memory is gone.
 - 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 yet.

References
==========

[1] https://lpc.events/event/20/contributions/2429/
[2] https://lpc.events/event/20/contributions/2482/
[3] https://lore.kernel.org/r/20261010224902.1232178-1-arighi@nvidia.com/
[4] https://lore.kernel.org/r/DM6HVNSP3USV.AJSWS289Z3I9@gmail.com/

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

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 (3):
      bpf: Allow importing memfd pages into an arena
      bpf: Allow importing device-dax pages into an arena
      selftests/bpf: Test importing pages into an arena

 kernel/bpf/arena.c                                 | 461 ++++++++++++++++++++-
 .../selftests/bpf/prog_tests/arena_import.c        | 451 ++++++++++++++++++++
 tools/testing/selftests/bpf/progs/arena_import.c   |  93 +++++
 3 files changed, 994 insertions(+), 11 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_import.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_import.c

             reply	other threads:[~2026-11-17 10:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 10:28 Andrea Righi [this message]
2026-11-17 10:28 ` [PATCH 1/3] bpf: Allow importing memfd " Andrea Righi
2026-11-17 11:14   ` bot+bpf-ci
2026-11-17 10:28 ` [PATCH 2/3] bpf: Allow importing device-dax " Andrea Righi
2026-11-17 11:02   ` bot+bpf-ci
2026-11-17 10:28 ` [PATCH 3/3] selftests/bpf: Test importing " Andrea Righi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117103028.4105609-1-arighi@nvidia.com \
    --to=arighi@nvidia.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=balbirs@nvidia.com \
    --cc=bpf@vger.kernel.org \
    --cc=changwoo@igalia.com \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=himadrics@protonmail.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=joshdon@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ltrager@nvidia.com \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=royfru44@gmail.com \
    --cc=sched-ext@lists.linux.dev \
    --cc=skolothumtho@nvidia.com \
    --cc=song@kernel.org \
    --cc=tj@kernel.org \
    --cc=vineethrp@google.com \
    --cc=void@manifault.com \
    --cc=yepeilin@google.com \
    --cc=yonghong.song@linux.dev \
    --cc=zhiw@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®