bpf: Import external pages into an arena
Subsystems (4)
Andrea Righi
reviewerv2 cover bpf: Import external pages into an arena
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/[email protected]/
[4] https://lore.kernel.org/r/[email protected]/
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/[email protected]/
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
Andrea Righi
reviewerPATCH v2 1/3 bpf: Allow importing memfd pages into an arena
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.
Teach the arena to map pages that it didn't allocate and add a sleepable kfunc for syscall programs that imports the pages of a memfd, or of any file on tmpfs or hugetlbfs:
int bpf_arena_import_memfd(void *map, void __arena *addr,
u32 page_cnt, int fd, u64 off);
The folios are pinned with memfd_pin_folios(), like udmabuf does, and mapped at addr. Whatever maps the file then sees the same pages as the BPF programs, for example a VMM that maps it into a guest.
Each import is kept in an interval tree with its pages, which belong to the file, not to the arena:
- bpf_arena_free_pages() over any of them releases the whole import:
the pages are unmapped and unpinned, the data stays in the file,
- 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 by mapping the file.
The file must be open for reading and writing and the LSMs must allow a shared mapping of it. It must not be append-only or sealed against writes.
F_SEAL_SHRINK is not required, unlike udmabuf: a file on tmpfs, such as a VMM memory backend, can't be sealed. If the supplier shrinks the file or punches a hole in the imported range, the arena keeps the pages it pinned, which are no longer the file's.
While its pages are imported, the file can't be executed, as when it's open for writing.
The pins aren't charged to locked_vm: like the rest of BPF memory, the pages are accounted to a memory cgroup, the one of the supplier that allocated them (hugetlbfs pages come from the hugetlb pool). Pinning them adds no memory, but keeps it from being swapped out until the arena releases it. Only a program with CAP_BPF, or a BPF token, can import pages.
--- kernel/bpf/arena.c | 340 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 329 insertions(+), 11 deletions(-)
kernel/bpf/arena.c +329 -11
@@ -4,7 +4,10 @@#include <linux/btf.h> #include <linux/cacheflush.h> #include <linux/err.h> +#include <linux/file.h> +#include <linux/interval_tree_generic.h> #include <linux/irq_work.h> +#include <linux/memfd.h> #include "linux/filter.h" #include <linux/llist.h> #include <linux/mman.h>
@@ -14,6 +17,7 @@#include <linux/printk.h> #include <linux/ratelimit.h> #include <linux/rbtree_latch.h> +#include <linux/security.h> #include <asm/tlbflush.h> #include "range_tree.h"
@@ -64,7 +68,9 @@ struct bpf_arena {struct vm_struct *kern_vm;struct page *scratch_page;struct range_tree rt; -/* protects rt and nr_pages */ +/* pages the arena didn't allocate, see struct arena_import */ +struct rb_root_cached imports; +/* protects rt, imports and nr_pages */ rqspinlock_t spinlock;/* number of pages currently populated in the arena */ u64 nr_pages;
@@ -114,6 +120,28 @@ struct arena_free_span {u32 page_cnt; }; +/* +* Pages mapped by one import, pinned once each. They are released together, +* when the arena frees any of them or is destroyed. +*/ +struct arena_import { +struct rb_node rb; +long subtree_last; +/* on a list while being released */ +struct list_head head; +struct page **pages; +/* the file, with write access on its inode */ +struct file *file; +long pgoff; +long page_cnt; +u32 uaddr;/* lower 32 bits of the user address */ +}; + +#define IMPORT_START (imp) ((imp)->pgoff) +#define IMPORT_LAST (imp) ((imp)->pgoff + (imp)->page_cnt - 1) +INTERVAL_TREE_DEFINE(struct arena_import, rb, long, subtree_last, IMPORT_START, IMPORT_LAST, + static inline __maybe_unused, arena_import_it) +u64 bpf_arena_get_kern_vm_start (struct bpf_arena *arena) {return arena ? (u64 ) (long ) arena->kern_vm->addr +GUARD_SZ /2 :0 ;
@@ -210,6 +238,8 @@ static long compute_pgoff(struct bpf_arena *arena, long uaddr)struct apply_range_data {struct bpf_arena *arena;struct llist_head *pages; +/* imported pages, they can't be linked through pcp_llist */ +struct page **ext_pages;int i; };
@@ -221,17 +251,21 @@ struct clear_range_data {static int apply_range_set_cb (pte_t *pte,unsigned long addr,void *data) {struct apply_range_data *d = data; -struct llist_node *node; +struct llist_node *node =NULL ;struct page *page;pte_t pteval;if (!data)return 0 ; - node =READ_ONCE (d->pages->first); -if (WARN_ON_ONCE (!node)) -return -EINVAL ; - page = llist_entry(node, struct page, pcp_llist); +if (d->ext_pages) { + page = d->ext_pages[d->i]; + }else { + node =READ_ONCE (d->pages->first); +if (WARN_ON_ONCE (!node)) +return -EINVAL ; + page = llist_entry(node, struct page, pcp_llist); + }/* paranoia, similar to vmap_pages_pte_range() */ if (WARN_ON_ONCE (!pfn_valid (page_to_pfn (page))))return -EINVAL ;
@@ -264,8 +298,11 @@ static int apply_range_set_cb(pte_t *pte, unsigned long addr, void *data)return -EBUSY ;set_pte_at (&init_mm, addr, pte, pteval);#endif -WARN_ON_ONCE (llist_del_first (d->pages) != node); d->i++; +/* Imported pages are accounted to their supplier */ +if (!node) +return 0 ; +WARN_ON_ONCE (llist_del_first (d->pages) != node);WRITE_ONCE (d->arena->nr_pages, d->arena->nr_pages +1 );return 0 ; }
@@ -324,6 +361,47 @@ static int apply_range_set_scratch_cb(pte_t *pte, unsigned long addr, void *datareturn 0 ; } +static int apply_range_clear_ext_cb (pte_t *pte,unsigned long addr,void *data) +{ +ptep_get_and_clear (&init_mm, addr, pte); +return 0 ; +} + +/* Under arena->spinlock, or when nothing else can use the arena anymore */ +static struct arena_import *arena_find_import (struct bpf_arena *arena,long pgoff) +{ +return arena_import_it_iter_first (&arena->imports, pgoff, pgoff); +} + +static u64 arena_import_kaddr (struct bpf_arena *arena,struct arena_import *imp) +{ +return bpf_arena_get_kern_vm_start (arena) + imp->uaddr; +} + +/* Like arena_find_import(), the caller flushes the TLB after */ +static void arena_unmap_import (struct bpf_arena *arena,struct arena_import *imp) +{ +apply_to_existing_page_range (&init_mm,arena_import_kaddr (arena, imp), + imp->page_cnt <<PAGE_SHIFT , apply_range_clear_ext_cb,NULL ); +} + +static void arena_flush_import (struct bpf_arena *arena,struct arena_import *imp) +{ +u64 kaddr =arena_import_kaddr (arena, imp); + +flush_tlb_kernel_range (kaddr, kaddr + (imp->page_cnt <<PAGE_SHIFT )); +} + +static void arena_release_import (struct arena_import *imp) +{ +/* BPF programs write through the kernel mapping, which doesn't track dirty */ +unpin_user_pages_dirty_lock (imp->pages, imp->page_cnt, true); +put_write_access (file_inode (imp->file)); +fput (imp->file); +kvfree (imp->pages); +kfree (imp); +} +static int populate_pgtable_except_pte (struct bpf_arena *arena) {/* Populate intermediates for the recovery range (4 GiB + upper half-guard). */
@@ -375,6 +453,7 @@ static struct bpf_map *arena_map_alloc(union bpf_attr *attr) arena->user_vm_end = arena->user_vm_start + vm_range;INIT_LIST_HEAD (&arena->vma_list); + arena->imports =RB_ROOT_CACHED ;init_llist_head (&arena->free_spans);init_irq_work (&arena->free_irq, arena_free_irq);INIT_WORK (&arena->free_work, arena_free_worker);
@@ -443,6 +522,15 @@ static void arena_map_destroy(struct work_struct *work) { struct bpf_arena *arena = container_of(to_rcu_work(work), struct bpf_arena, destroy_work); +struct arena_import *imp; + +/* Imported pages go back to their supplier */ +while ((imp =arena_import_it_iter_first (&arena->imports,0 ,LONG_MAX ))) { +arena_import_it_remove (imp, &arena->imports); +arena_unmap_import (arena, imp); +arena_flush_import (arena, imp); +arena_release_import (imp); + }/* * free_vm_area() calls remove_vm_area() that calls free_unmap_vmap_area().
@@ -569,7 +657,7 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)struct mem_cgroup *new_memcg, *old_memcg;LLIST_HEAD (pages);struct page *page, *new_page =NULL ; -struct apply_range_data data; +struct apply_range_data data = {};vm_fault_t fault_ret;long kbase, kaddr;unsigned long flags;
@@ -605,6 +693,15 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf) page =vmalloc_to_page ((void *)kaddr);if (page) { +/* +* Not the arena's: user space maps the file or the device the +* pages come from +*/ +if (arena_find_import (arena, vmf->pgoff)) { + fault_ret = (arena->map.map_flags &BPF_F_SEGV_ON_FAULT ) + ?VM_FAULT_SIGSEGV :VM_FAULT_SIGBUS ; + goto out_err_locked; + }if (page == arena->scratch_page) {/* BPF triggered scratch here; don't lazy-alloc over it */ fault_ret = (arena->map.map_flags &BPF_F_SEGV_ON_FAULT )
@@ -855,7 +952,7 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cntlong page_cnt_max = (arena->user_vm_end - arena->user_vm_start) >>PAGE_SHIFT ;u64 kern_vm_start =bpf_arena_get_kern_vm_start (arena);struct mem_cgroup *new_memcg, *old_memcg; -struct apply_range_data data; +struct apply_range_data data = {};LLIST_HEAD (pages);long mapped =0 ;unsigned long flags;
@@ -1010,9 +1107,12 @@ static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt)u64 full_uaddr =clear_lo32 (arena->user_vm_start) + uaddr;long kaddr =bpf_arena_get_kern_vm_start (arena) + uaddr;long pgoff =compute_pgoff (arena, uaddr); +long first = pgoff, last = pgoff + page_cnt -1 ; +struct arena_import *imp, *tmp;struct clear_range_data cdata;struct llist_node *pos, *t;LLIST_HEAD (free_pages); +LIST_HEAD (imports);unsigned long flags;struct page *page;int ret;
@@ -1033,6 +1133,15 @@ static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt)return 0 ; } +/* Imports are released whole, also the ones that are only partly in the range */ +while ((imp =arena_import_it_iter_first (&arena->imports, pgoff, pgoff + page_cnt -1 ))) { + first =min (first,IMPORT_START (imp)); + last =max (last,IMPORT_LAST (imp)); +arena_import_it_remove (imp, &arena->imports); +arena_unmap_import (arena, imp); +list_add (&imp->head, &imports); + } + cdata.arena = arena; cdata.free_pages = &free_pages;/* clear ptes and collect struct pages */
@@ -1050,6 +1159,8 @@ static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt)/* ensure no stale TLB entries */ flush_tlb_kernel_range (kaddr, kaddr + (page_cnt *PAGE_SIZE )); +list_for_each_entry (imp, &imports, head) +arena_flush_import (arena, imp);if (page_cnt >1 )/* bulk zap if multiple pages being freed */
@@ -1070,8 +1181,15 @@ static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt)/* The callers hold no other rqspinlock, so this one can only time out */ while (raw_res_spin_lock_irqsave (&arena->spinlock, flags))cond_resched (); -range_tree_set (&arena->rt, pgoff, page_cnt); +/* +* The range and the imports it overlaps are contiguous: set them at once, +* so that the imports need no more range tree nodes than the range alone +*/ +range_tree_set (&arena->rt, first, last - first +1 );raw_res_spin_unlock_irqrestore (&arena->spinlock, flags); + +list_for_each_entry_safe (imp, tmp, &imports, head) +arena_release_import (imp);return 0 ; }
@@ -1272,6 +1390,160 @@ static int arena_reserve_pages(struct bpf_arena *arena, long uaddr, u32 page_cntreturn ret; } +/* +* Map @page_cnt pages that the arena didn't allocate at @uaddr, which must be +* free. On success the arena keeps @pages, their pins and the write access on +* @file until it releases them, see struct arena_import. +*/ +static int arena_map_ext_pages (struct bpf_arena *arena,long uaddr,struct page **pages, +long page_cnt,struct file *file) +{ +/* The first mmap() may be setting the user range: a non-zero start is final */ +u64 user_vm_start =READ_ONCE (arena->user_vm_start); +struct apply_range_data data = { .arena = arena, .ext_pages = pages }; +struct mem_cgroup *new_memcg, *old_memcg; +long page_cnt_max, pgoff, kaddr; +struct arena_import *imp; +unsigned long flags; +int ret; + +if (!user_vm_start) +return -EINVAL ; + page_cnt_max = ((long )READ_ONCE (arena->user_vm_end) - (long )user_vm_start) >>PAGE_SHIFT ; +if (!uaddr || (uaddr & ~PAGE_MASK ) || page_cnt > page_cnt_max) +return -EINVAL ; + pgoff = (u32)(uaddr - (u32 )user_vm_start) >>PAGE_SHIFT ; +if (pgoff > page_cnt_max - page_cnt) +return -EINVAL ; + kaddr =bpf_arena_get_kern_vm_start (arena) + (u32 )uaddr; + + imp =kmalloc_obj (*imp,GFP_KERNEL_ACCOUNT ); +if (!imp) +return -ENOMEM ; + imp->pages = pages; + imp->file = file; + imp->pgoff = pgoff; + imp->page_cnt = page_cnt; + imp->uaddr = (u32 )uaddr; + +bpf_map_memcg_enter (&arena->map, &old_memcg, &new_memcg); +if (raw_res_spin_lock_irqsave (&arena->spinlock, flags)) { + ret = -EBUSY ; + goto out; + } + ret =is_range_tree_set (&arena->rt, pgoff, page_cnt) ? -EBUSY : +range_tree_clear (&arena->rt, pgoff, page_cnt); +if (!ret) { +/* The page tables are there, and a free range has no pages */ +WARN_ON_ONCE (apply_to_page_range (&init_mm, kaddr, page_cnt <<PAGE_SHIFT , + apply_range_set_cb, &data)); +flush_vmap_cache (kaddr, page_cnt <<PAGE_SHIFT ); +get_file (file); +arena_import_it_insert (imp, &arena->imports); + } +raw_res_spin_unlock_irqrestore (&arena->spinlock, flags); +out: +bpf_map_memcg_exit (old_memcg, new_memcg); +if (ret) +kfree (imp); +return ret; +} + +/* +* Import the pages of a memfd or of a file on tmpfs or hugetlbfs. Every file +* there has seals: one that isn't a memfd reports F_SEAL_SEAL alone. Other +* files have none and can't be imported. +*/ +static int arena_import_file (struct bpf_arena *arena,long uaddr,u32 page_cnt, +struct file *file,u64 off) +{ +long i, nr_folios, first, cnt =0 ; +struct folio **folios; +struct page **pages; +pgoff_t folio_off; +int seals, ret; + +/* +* Like a writable open: exec of the file fails with -ETXTBSY until the +* pages are released, and a file being executed can't be imported. +* Keeping the file open isn't enough, a memfd holds no write access. +*/ + ret =get_write_access (file_inode (file)); +if (ret) +return ret; + + folios =kvmalloc_array (page_cnt,sizeof (*folios),GFP_KERNEL_ACCOUNT ); + pages =kvmalloc_array (page_cnt,sizeof (*pages),GFP_KERNEL_ACCOUNT ); +if (!folios || !pages) { + ret = -ENOMEM ; + goto out; + } + +/* +* Adding F_SEAL_WRITE takes the inode lock and fails while pages are +* pinned: hold it until they are, like udmabuf +*/ +inode_lock_shared (file_inode (file)); + seals =memfd_get_seals (file); +if (seals <0 ) + nr_folios = -EINVAL ; +else if ((seals & (F_SEAL_WRITE |F_SEAL_FUTURE_WRITE )) ||IS_APPEND (file_inode (file))) + nr_folios = -EPERM ; +else + nr_folios =memfd_pin_folios (file, off, + off + ((u64 )page_cnt <<PAGE_SHIFT ) -1 , + folios, page_cnt, &folio_off); +inode_unlock_shared (file_inode (file)); +if (nr_folios <=0 ) { + ret = nr_folios ?: -EINVAL ; + goto out; + } + +/* One pin per page, like pin_user_pages() */ +for (i =0 ; i < nr_folios; i++, folio_off =0 ) { + first = cnt; +for (; folio_off <folio_size (folios[i]) && cnt < page_cnt; + folio_off +=PAGE_SIZE ) + pages[cnt++] =folio_page (folios[i], folio_off >>PAGE_SHIFT ); +if (cnt - first >1 &&folio_add_pins (folios[i], cnt - first -1 )) { +unpin_folios (folios + i, nr_folios - i); + cnt = first; + ret = -ENOMEM ; + goto out; + } + } + + ret = cnt == page_cnt ?arena_map_ext_pages (arena, uaddr, pages, page_cnt, file) : -EINVAL ; +out: +if (ret) { +unpin_user_pages (pages, cnt); +kvfree (pages); +put_write_access (file_inode (file)); + } +kvfree (folios); +return ret; +} + +static int arena_import_memfd (struct bpf_arena *arena,long uaddr,u32 page_cnt,int fd, +u64 off) +{ +int ret; + +if (off & ~PAGE_MASK ) +return -EINVAL ; + +CLASS (fd, f)(fd); +if (fd_empty (f)) +return -EBADF ; +/* BPF programs read and write it like a shared mapping, which needs the same */ +if ((fd_file (f)->f_mode & (FMODE_READ |FMODE_WRITE )) != (FMODE_READ |FMODE_WRITE )) +return -EPERM ; + ret =security_mmap_file (fd_file (f),PROT_READ |PROT_WRITE ,MAP_SHARED ); +if (ret) +return ret; +return arena_import_file (arena, uaddr, page_cnt,fd_file (f), off); +} +static void arena_free_worker (struct work_struct *work) { struct bpf_arena *arena = container_of(work, struct bpf_arena, free_work);
@@ -1369,6 +1641,42 @@ __bpf_kfunc int bpf_arena_reserve_pages(void *p__map, void *ptr__ign, u32 page_creturn arena_reserve_pages (arena, (long )ptr__ign, page_cnt); } + +/** +* bpf_arena_import_memfd - map the pages of a memfd into an arena +* @p__map: the arena +* @addr__ign: page-aligned arena address where the pages are mapped +* @page_cnt: number of pages to import +* @fd: memfd, or file on tmpfs or hugetlbfs, open for reading and writing +* @off: page-aligned offset in the file +* +* Pin the pages of @fd at @off and map them at @addr__ign, which must be free. +* They stay until bpf_arena_free_pages() frees any of them, which releases the +* whole import, or until the arena is destroyed. Their content stays in the +* file. User space accesses them by mapping @fd: the arena mapping raises +* SIGBUS (SIGSEGV with BPF_F_SEGV_ON_FAULT) at their addresses. The first page +* of an arena that starts on a 4GB boundary can't be imported: its address +* reaches the kfunc as NULL. +* +* F_SEAL_SHRINK is not required, a file on tmpfs can't be sealed. The supplier +* must not shrink the file below the imported range or punch holes in it: the +* arena would keep the pages it pinned, which are no longer the file's. +* +* While its pages are imported, the file can't be executed, as when it's open +* for writing, and a file being executed can't be imported. +* +* Return: 0 on success, negative error code otherwise. +*/ +__bpf_kfunc int bpf_arena_import_memfd(void *p__map, void *addr__ign, u32 page_cnt, +int fd,u64 off) +{ +struct bpf_map *map = p__map; + struct bpf_arena *arena = container_of(map, struct bpf_arena, map); + +if (map->map_type !=BPF_MAP_TYPE_ARENA || !page_cnt || page_cnt > map->max_entries) +return -EINVAL ; +return arena_import_memfd (arena, (long )addr__ign, page_cnt, fd, off); +}__bpf_kfunc_end_defs ();BTF_KFUNCS_START (arena_kfuncs)
@@ -1382,9 +1690,19 @@ static const struct btf_kfunc_id_set common_kfunc_set = { .set = &arena_kfuncs, }; +BTF_KFUNCS_START (arena_syscall_kfuncs) +BTF_ID_FLAGS(func, bpf_arena_import_memfd, KF_ARENA_ARG2 | KF_SLEEPABLE) +BTF_KFUNCS_END (arena_syscall_kfuncs) + +static const struct btf_kfunc_id_set syscall_kfunc_set = { + .owner =THIS_MODULE , + .set = &arena_syscall_kfuncs, +}; + static int __init kfunc_init(void) { -return register_btf_kfunc_id_set (BPF_PROG_TYPE_UNSPEC , &common_kfunc_set); +return register_btf_kfunc_id_set (BPF_PROG_TYPE_UNSPEC , &common_kfunc_set) ?: +register_btf_kfunc_id_set (BPF_PROG_TYPE_SYSCALL , &syscall_kfunc_set); }late_initcall (kfunc_init);
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH 1/3] bpf: Allow importing memfd pages into an arena
TL;DR: - bpf_arena_import_memfd() may run in another task via bpf_task_work, mapping a victim's memfd into the caller's arena without CAP_PERFMON - Commit message memcg accounting claim may be wrong…
Quoted 1 line
bpf: Allow importing memfd pages into an arena
TL;DR:
- bpf_arena_import_memfd() may run in another task via bpf_task_work, mapping a victim's memfd into the caller's arena without CAP_PERFMON
- Commit message memcg accounting claim may be wrong for memfd holes
Quoted 44 lines
bpf: Allow importing memfd pages into an arena
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.
Teach the arena to map pages that it didn't allocate and add a sleepable kfunc for syscall programs that imports the pages of a memfd, or of any file on tmpfs or hugetlbfs:
int bpf_arena_import_memfd(void *map, void
__arena*addr,u32 page_cnt, int fd, u64 off);The folios are pinned with
memfd_pin_folios(), like udmabuf does, and mapped at addr. Whatever maps the file then sees the same pages as the BPF programs, for example a VMM that maps it into a guest.Each import is kept in an interval tree with its pages, which belong to the file, not to the arena:
-bpf_arena_free_pages()over any of them releases the whole import:
the pages are unmapped and unpinned, the data stays in the file,
- 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 withBPF_F_SEGV_ON_FAULT): user space accesses them by mapping the file.The file must be open for reading and writing and the LSMs must allow a shared mapping of it. It must not be append-only or sealed against writes.
F_SEAL_SHRINKis not required, unlike udmabuf: a file on tmpfs, such as a VMM memory backend, can't be sealed. If the supplier shrinks the file or punches a hole in the imported range, the arena keeps the pages it pinned, which are no longer the file's.While its pages are imported, the file can't be executed, as when it's open for writing.
The pins aren't charged to
locked_vm: like the rest of BPF memory, the pages are accounted to a memory cgroup, the one of the supplier that allocated them (hugetlbfs pages come from the hugetlb pool). Pinning them adds no memory, but keeps it from being swapped out until the arena releases it. Only a program withCAP_BPF, or a BPF token, can import pages.
Quoted 3 lines
Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Andrea Righi <[email protected]>
Doesn't memfd_pin_folios() allocate folios for any holes in the range? A memfd that was only ftruncate()d would get every page allocated by this call (shmem_read_folio() for shmem, a pool allocation for hugetlbfs).
arena_import_file() has no bpf_map_memcg_enter() around memfd_pin_folios(), so those shmem pages look like they are charged to the memcg of the task running the syscall program, not to the supplier or the map. They also stay in the file's page cache after the import is released.
The changelog says "Pinning them adds no memory" and that the pages are accounted to "the one of the supplier that allocated them". Could it describe this case instead?
---
Quoted 4 lines
kernel/bpf/arena.c +0
-0
[ ... ]
Quoted 9 lines
kernel/bpf/arena.c +9
-0+static int arena_import_memfd (struct bpf_arena *arena,long uaddr,u32 page_cnt,int fd, +u64 off) +{ +int ret; + +if (off & ~PAGE_MASK ) +return -EINVAL ; + +CLASS (fd, f)(fd);
kernel/bpf/arena.c static int arena_reserve_pages(struct bpf_arena *arena, long uaddr, u32 page_cnt · line 1545
Quoted 10 lines
kernel/bpf/arena.c +10
-0+if (fd_empty (f)) +return -EBADF ; +/* BPF programs read and write it like a shared mapping, which needs the same */ +if ((fd_file (f)->f_mode & (FMODE_READ |FMODE_WRITE )) != (FMODE_READ |FMODE_WRITE )) +return -EPERM ; + ret =security_mmap_file (fd_file (f),PROT_READ |PROT_WRITE ,MAP_SHARED ); +if (ret) +return ret; +return arena_import_file (arena, uaddr, page_cnt,fd_file (f), off); +}
The fd here is resolved with CLASS(fd, f)(fd) against current, and the FMODE and security_mmap_file() checks run with current's credentials. The kfunc is KF_SLEEPABLE, and the verifier treats bpf_task_work callbacks as sleepable, so a BPF_PROG_TYPE_SYSCALL program can call it from one.
Can this run in the context of a different task? For example:
bpf_task_from_pid(victim_pid) bpf_task_work_schedule_signal(task, &val->tw, &map, cb)
cb()
bpf_arena_import_memfd(&arena, addr, n, victim_fd, off)
bpf_task_work_callback() runs cb in the target task, so victim_fd is looked up in the victim's fd table and security_mmap_file() is checked against the victim's credentials. A read/write memfd held by the victim (a VMM guest memory backend, for instance) would then be pinned and mapped into the caller's arena, where its BPF programs can read and write it, and copy it into ordinary arena pages that the caller's user space maps.
bpf_task_from_pid() and bpf_task_work_schedule_*() need no CAP_PERFMON, while other ways for BPF to read another task's user memory (bpf_copy_from_user_task, the probe_read_user paths) do. The commit message only says "Only a program with CAP_BPF, or a BPF token, can import pages".
A bpf_wq callback runs in a kworker, so there the fd is resolved against the kthread's file table instead.
Should the kfunc refuse to run from async callbacks, check that current is the task running BPF_PROG_RUN, or require CAP_PERFMON (KF_PERFMON) or a ptrace_may_access()-style check?
[ ... ]
kernel/bpf/arena.c static const struct btf_kfunc_id_set common_kfunc_set = { · line 1695
Quoted 3 lines
kernel/bpf/arena.c +3
-0+BTF_KFUNCS_START(arena_syscall_kfuncs) +BTF_ID_FLAGS(func, bpf_arena_import_memfd, KF_ARENA_ARG2 | KF_SLEEPABLE) +BTF_KFUNCS_END(arena_syscall_kfuncs)
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44067070819
Andrea Righi
reviewerPATCH v2 2/3 bpf: Allow importing device-dax pages into an arena
In reply to Andrea Righibpf_arena_import_memfd() covers the host side of a host/guest shared
arena: the host imports a memfd and hands the same file to the VMM. The guest has no such file. The host memory can be exposed to the guest as a virtio-pmem or NVDIMM device, which the guest turns into a device-dax device, /dev/daxX.Y.
Add a sleepable kfunc for syscall programs that imports the device-dax pages mapped at an address of the calling task:
int bpf_arena_import_dax(void *map, void __arena *addr,
u32 page_cnt, u64 src);
The pages are pinned with pin_user_pages(FOLL_LONGTERM) and mapped like memfd imports. Only a device-dax mapping is accepted, checked under mmap_lock together with the pin.
A pin doesn't keep the dev_pagemap of device pages alive, so the import holds a reference on it until the pages are released: unbinding the device waits until then. Until the reference is held, an unbind could free the memmap of the pinned pages, so mem_hotplug_lock is held from the pin until then.
So removing the device waits for the arena: an unbind blocks until the pages are freed or the arena is destroyed. Revoking the pages instead would need a notification from the dax core and a way to tell the BPF programs that their shared memory is gone, rather than remapping it under them.
--- kernel/bpf/arena.c | 137 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 129 insertions(+), 8 deletions(-)
kernel/bpf/arena.c +129 -8
@@ -7,7 +7,10 @@#include <linux/file.h> #include <linux/interval_tree_generic.h> #include <linux/irq_work.h> +#include <linux/magic.h> #include <linux/memfd.h> +#include <linux/memory_hotplug.h> +#include <linux/memremap.h> #include "linux/filter.h" #include <linux/llist.h> #include <linux/mman.h>
@@ -130,7 +133,9 @@ struct arena_import {/* on a list while being released */ struct list_head head;struct page **pages; -/* the file, with write access on its inode */ +/* device pages: one reference keeps them all, NULL otherwise */ +struct dev_pagemap *pgmap; +/* file pages: the file, with write access on its inode, NULL otherwise */ struct file *file;long pgoff;long page_cnt;
@@ -396,8 +401,12 @@ static void arena_release_import(struct arena_import *imp) {/* BPF programs write through the kernel mapping, which doesn't track dirty */ unpin_user_pages_dirty_lock (imp->pages, imp->page_cnt, true); -put_write_access (file_inode (imp->file)); -fput (imp->file); +/* After the pins: the memmap of device pages goes away with it */ +put_dev_pagemap (imp->pgmap); +if (imp->file) { +put_write_access (file_inode (imp->file)); +fput (imp->file); + }kvfree (imp->pages);kfree (imp); }
@@ -1392,11 +1401,11 @@ static int arena_reserve_pages(struct bpf_arena *arena, long uaddr, u32 page_cnt/* * Map @page_cnt pages that the arena didn't allocate at @uaddr, which must be -* free. On success the arena keeps @pages, their pins and the write access on -* @file until it releases them, see struct arena_import. +* free. On success the arena keeps @pages, their pins, @pgmap and the write +* access on @file until it releases them, see struct arena_import. */ static int arena_map_ext_pages (struct bpf_arena *arena,long uaddr,struct page **pages, -long page_cnt,struct file *file) +long page_cnt,struct dev_pagemap *pgmap,struct file *file) {/* The first mmap() may be setting the user range: a non-zero start is final */ u64 user_vm_start =READ_ONCE (arena->user_vm_start);
@@ -1421,6 +1430,7 @@ static int arena_map_ext_pages(struct bpf_arena *arena, long uaddr, struct pageif (!imp)return -ENOMEM ; imp->pages = pages; + imp->pgmap = pgmap; imp->file = file; imp->pgoff = pgoff; imp->page_cnt = page_cnt;
@@ -1438,7 +1448,8 @@ static int arena_map_ext_pages(struct bpf_arena *arena, long uaddr, struct pageWARN_ON_ONCE (apply_to_page_range (&init_mm, kaddr, page_cnt <<PAGE_SHIFT , apply_range_set_cb, &data));flush_vmap_cache (kaddr, page_cnt <<PAGE_SHIFT ); -get_file (file); +if (file) +get_file (file);arena_import_it_insert (imp, &arena->imports); }raw_res_spin_unlock_irqrestore (&arena->spinlock, flags);
@@ -1513,7 +1524,8 @@ static int arena_import_file(struct bpf_arena *arena, long uaddr, u32 page_cnt, } } - ret = cnt == page_cnt ?arena_map_ext_pages (arena, uaddr, pages, page_cnt, file) : -EINVAL ; + ret = cnt == page_cnt ?arena_map_ext_pages (arena, uaddr, pages, page_cnt,NULL , file) : + -EINVAL ; out:if (ret) {unpin_user_pages (pages, cnt);
@@ -1544,6 +1556,82 @@ static int arena_import_memfd(struct bpf_arena *arena, long uaddr, u32 page_cnt,return arena_import_file (arena, uaddr, page_cnt,fd_file (f), off); } +/* +* A device-dax file maps the inode of its dax_device, which lives on the dax +* pseudo filesystem, whatever CONFIG_FS_DAX says (S_DAX is 0 without it). +*/ +static bool vma_is_devdax (struct vm_area_struct *vma) +{ +struct file *file = vma->vm_file; + +return file && file->f_mapping->host->i_sb->s_magic ==DAXFS_MAGIC ; +} + +/* +* Import the device-dax pages mapped at [src, src + page_cnt * PAGE_SIZE) in +* the calling task. Only device-dax is accepted: other drivers hand out +* MEMORY_DEVICE_GENERIC pages too (e.g. Xen grants). +*/ +static int arena_import_dax (struct bpf_arena *arena,long uaddr,u32 page_cnt,u64 src) +{ +struct mm_struct *mm = current->mm; +struct dev_pagemap *pgmap =NULL ; +struct vm_area_struct *vma; +long i, pinned =0 ; +struct page **pages; +int ret = -EINVAL ; + +/* A sleepable callback, like a bpf_wq one, may run without an address space */ +if (!mm || (src & ~PAGE_MASK )) +return -EINVAL ; + + pages =kvmalloc_array (page_cnt,sizeof (*pages),GFP_KERNEL_ACCOUNT ); +if (!pages) +return -ENOMEM ; + +/* +* A pin doesn't keep the memmap of device pages: an unbind can free it +* until a dev_pagemap reference is held, but only under mem_hotplug_lock, +* which nests outside of mmap_lock +*/ +get_online_mems (); +mmap_read_lock (mm); + vma =vma_lookup (mm, src); +if (!vma || !vma_is_devdax (vma) || + ((unsigned long )page_cnt <<PAGE_SHIFT ) > vma->vm_end - src) + goto out_unlock; + pinned =pin_user_pages (src, page_cnt,FOLL_WRITE |FOLL_LONGTERM , pages); +if (pinned <0 ) + ret = pinned; +else if (pinned == page_cnt &&is_zone_device_page (pages[0 ])) + pgmap =get_dev_pagemap (page_to_pfn (pages[0 ])); +if (pgmap && pgmap->type ==MEMORY_DEVICE_GENERIC ) { + ret =0 ; +/* All of the pages are on the device of the mapping */ +for (i =1 ; i < pinned; i++) +if (!is_zone_device_page (pages[i]) ||page_pgmap (pages[i]) != pgmap) + ret = -EINVAL ; + } +/* Still under the lock: there may be no dev_pagemap reference */ +if (ret && pinned >0 ) { +unpin_user_pages (pages, pinned); + pinned =0 ; + } +out_unlock: +mmap_read_unlock (mm); +put_online_mems (); + +if (!ret) + ret =arena_map_ext_pages (arena, uaddr, pages, page_cnt, pgmap,NULL ); +if (ret) { +if (pinned >0 ) +unpin_user_pages (pages, pinned); +put_dev_pagemap (pgmap); +kvfree (pages); + } +return ret; +} +static void arena_free_worker (struct work_struct *work) { struct bpf_arena *arena = container_of(work, struct bpf_arena, free_work);
@@ -1677,6 +1765,38 @@ __bpf_kfunc int bpf_arena_import_memfd(void *p__map, void *addr__ign, u32 page_creturn -EINVAL ;return arena_import_memfd (arena, (long )addr__ign, page_cnt, fd, off); } + +/** +* bpf_arena_import_dax - map device-dax pages of the calling task into an arena +* @p__map: the arena +* @addr__ign: page-aligned arena address where the pages are mapped +* @page_cnt: number of pages to import +* @src: page-aligned address of a writable device-dax mapping in the calling task +* +* Pin the pages backing @src and map them at @addr__ign, like +* bpf_arena_import_memfd() does with the pages of a file. The whole range must +* be in one mapping of a device-dax device, e.g. the /dev/daxX.Y that a guest +* gets from a virtio-pmem or NVDIMM device backed by host memory, which the +* host imports by file. User space keeps accessing the pages through @src. +* +* Imported pages keep the device in use: unbinding or removing it blocks until +* the arena releases them, for as long as a map fd or a bpffs pin keeps the +* arena alive. +* +* BPF programs have no machine check recovery: on a real NVDIMM, the supplier +* has to keep bad pages out of the arena. +* +* Return: 0 on success, negative error code otherwise. +*/ +__bpf_kfunc int bpf_arena_import_dax(void *p__map, void *addr__ign, u32 page_cnt, u64 src) +{ +struct bpf_map *map = p__map; + struct bpf_arena *arena = container_of(map, struct bpf_arena, map); + +if (map->map_type !=BPF_MAP_TYPE_ARENA || !page_cnt || page_cnt > map->max_entries) +return -EINVAL ; +return arena_import_dax (arena, (long )addr__ign, page_cnt, src); +}__bpf_kfunc_end_defs ();BTF_KFUNCS_START (arena_kfuncs)
@@ -1692,6 +1812,7 @@ static const struct btf_kfunc_id_set common_kfunc_set = { BTF_KFUNCS_START(arena_syscall_kfuncs) BTF_ID_FLAGS(func, bpf_arena_import_memfd, KF_ARENA_ARG2 | KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_arena_import_dax, KF_ARENA_ARG2 | KF_SLEEPABLE) BTF_KFUNCS_END(arena_syscall_kfuncs) static const struct btf_kfunc_id_set syscall_kfunc_set = {
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH 2/3] bpf: Allow importing device-dax pages into an arena
TL;DR: - Possible ABBA deadlock: get_online_mems() then mmap_read_lock() inverts the order nouveau/dmirror use, so a hang looks possible - Minor: tagged user pointers are not untagged before…
Quoted 1 line
bpf: Allow importing device-dax pages into an arena
TL;DR:
- Possible ABBA deadlock: get_online_mems() then mmap_read_lock() inverts the order nouveau/dmirror use, so a hang looks possible
- Minor: tagged user pointers are not untagged before vma_lookup()
Quoted 3 lines
kernel/bpf/arena.c +0
-0
[ ... ]
Quoted 2 lines
kernel/bpf/arena.c +2
-0+static int arena_import_dax (struct bpf_arena *arena,long uaddr,u32 page_cnt,u64 src) +{
[ ... ]
Quoted 3 lines
kernel/bpf/arena.c +3
-0+/* A sleepable callback, like a bpf_wq one, may run without an address space */ +if (!mm || (src & ~PAGE_MASK )) +return -EINVAL ;
[ ... ]
kernel/bpf/arena.c static int arena_import_memfd(struct bpf_arena *arena, long uaddr, u32 page_cnt, · line 1598
Quoted 7 lines
kernel/bpf/arena.c +7
-0+/* +* A pin doesn't keep the memmap of device pages: an unbind can free it +* until a dev_pagemap reference is held, but only under mem_hotplug_lock, +* which nests outside of mmap_lock +*/ +get_online_mems (); +mmap_read_lock (mm);
Is the lock order stated in this comment correct? The tree also has paths that take mem_hotplug_lock for write while holding a user mmap_read_lock:
nouveau_svmm_bind() mmap_read_lock(mm)
nouveau_dmem_migrate_vma() -> nouveau_dmem_migrate_chunk() ->
nouveau_dmem_migrate_copy_one() -> nouveau_dmem_page_alloc_locked() ->
nouveau_dmem_chunk_alloc() -> memremap_pages() -> pagemap_range() ->
mem_hotplug_begin() -> percpu_down_write(&mem_hotplug_lock) dmirror_migrate_to_device() mmap_read_lock(mm)
dmirror_migrate_alloc_and_copy() -> dmirror_devmem_alloc_page() ->
dmirror_allocate_chunk() -> memremap_pages()That is the opposite order from get_online_mems() followed by mmap_read_lock(mm) here, so lockdep should report an ABBA cycle once both chains are seen.
A hang with a single mm looks possible with three threads. T1 holds mmap_read_lock and waits in percpu_down_write() for T2. T2, in this kfunc, holds mem_hotplug_lock for read and calls mmap_read_lock(mm). T3 waits in mmap_write_lock() on T1's read hold, and once it sets the rwsem handoff flag T2 can no longer take the read lock and queues behind T3. No thread can make progress.
The x86 ptdump debugfs path already uses the same order as this patch, so the inversion is not new, but this kfunc makes it reachable from a much more common path.
Could the dev_pagemap reference be taken without holding mem_hotplug_lock across mmap_lock, for example by taking the dev_dax pgmap from the VMA's file before pinning?
kernel/bpf/arena.c static int arena_import_memfd(struct bpf_arena *arena, long uaddr, u32 page_cnt, · line 1599
Quoted 1 line
kernel/bpf/arena.c +1
-0+ vma =vma_lookup (mm, src);
A subsystem pattern flags this as potentially concerning: src is a user-supplied u64 that goes to vma_lookup() and to the vma->vm_end - src range check without untagged_addr(), and the only earlier test is (src & ~PAGE_MASK). A tagged pointer (arm64 TBI/MTE, x86 LAM) is above every VMA, so vma_lookup() returns NULL and the import fails with -EINVAL, while pin_user_pages() would have accepted the same address because __get_user_pages() untags it.
This fails closed, so the checked VMA and the pinned range cannot diverge, and addresses returned by mmap() of /dev/daxX.Y are untagged. Should this use untagged_addr(src) on a copy before vma_lookup() and the range check, or should the kdoc say that @src must be untagged?
kernel/bpf/arena.c static int arena_import_memfd(struct bpf_arena *arena, long uaddr, u32 page_cnt, · line 1602
Quoted 3 lines
kernel/bpf/arena.c +3
-0+if (!vma || !vma_is_devdax (vma) || + ((unsigned long )page_cnt <<PAGE_SHIFT ) > vma->vm_end - src) + goto out_unlock;
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44067070819
Andrea Righi
reviewerPATCH v2 3/3 selftests/bpf: Test importing pages into an arena
In reply to Andrea RighiCheck that pages imported with bpf_arena_import_memfd() are shared between BPF programs and user space mapping the memfd, that they outlive the arena, that freeing any of them releases the whole import while their data stays in the memfd, that invalid or overlapping imports are rejected, and that user space faults on them through the arena mapping.
Also check that bpf_arena_import_dax() rejects anything but a device-dax mapping. Importing device-dax pages needs a device-dax device.
--- .../selftests/bpf/prog_tests/arena_import.c | 451 ++++++++++++++++++ .../selftests/bpf/progs/arena_import.c | 93 ++++ 2 files changed, 544 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_import.c create mode 100644 tools/testing/selftests/bpf/progs/arena_import.c
tools/testing/selftests/bpf/prog_tests/arena_import.c new file +451 -0
@@ -0,0 +1,451 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 NVIDIA Corporation */ + +#include <test_progs.h> +#include <fcntl.h> +#include <signal.h> +#include <stdatomic.h> +#include <sys/mman.h> +#include <sys/wait.h> +#include <unistd.h> + +#include "arena_import.skel.h" + +#define MEMFD_PAGES 4 + +#ifndef F_SEAL_FUTURE_WRITE +#define F_SEAL_FUTURE_WRITE 0x0010 +#endif + +static long ps; + +static int run (struct bpf_program *prog) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts); +int err; + + err =bpf_prog_test_run_opts (bpf_program__fd (prog), &opts); +if (!ASSERT_OK (err,bpf_program__name (prog))) +return -1 ; +return opts.retval; +} + +static int do_import (struct arena_import *skel,int fd,long fd_off,__u32 pg,__u32 cnt) +{ + skel->bss->fd = fd; + skel->bss->fd_off = fd_off; + skel->bss->pg = pg; + skel->bss->page_cnt = cnt; + skel->bss->ret =1 ; +if (run (skel->progs.op_import)) +return 1 ; +return skel->bss->ret; +} + +static int do_import_dax (struct arena_import *skel,void *src,__u32 pg,__u32 cnt) +{ + skel->bss->src = (unsigned long )src; + skel->bss->pg = pg; + skel->bss->page_cnt = cnt; + skel->bss->ret =1 ; +if (run (skel->progs.op_import_dax)) +return 1 ; +return skel->bss->ret; +} + +static int do_alloc (struct arena_import *skel,__u32 pg,__u32 cnt) +{ + skel->bss->pg = pg; + skel->bss->page_cnt = cnt; + skel->bss->ret =1 ; +if (run (skel->progs.op_alloc)) +return 1 ; +return skel->bss->ret; +} + +static void do_free (struct arena_import *skel,__u32 pg,__u32 cnt) +{ + skel->bss->pg = pg; + skel->bss->page_cnt = cnt; +run (skel->progs.op_free); +} + +static void do_write (struct arena_import *skel,__u32 pg,__u64 val) +{ + skel->bss->pg = pg; + skel->bss->val = val; +run (skel->progs.op_write); +} + +static __u64 do_read (struct arena_import *skel,__u32 pg) +{ + skel->bss->pg = pg; + skel->bss->val =0 ; +run (skel->progs.op_read); +return skel->bss->val; +} + +static int make_memfd (unsigned int seals) +{ +int fd; + + fd =memfd_create ("arena_import" ,MFD_ALLOW_SEALING ); +if (fd <0 && errno ==ENOSYS ) { +printf ("%s:SKIP:no memfd_create()\n" , __func__); +test__skip (); +return -1 ; + } +if (!ASSERT_OK_FD (fd,"memfd_create" )) +return -1 ; +if (!ASSERT_OK (ftruncate (fd,MEMFD_PAGES * ps),"ftruncate" )) + goto err; +if (seals && !ASSERT_OK (fcntl (fd,F_ADD_SEALS , seals),"add_seals" )) + goto err; +return fd; +err: +close (fd); +return -1 ; +} + +static struct arena_import *open_skel (void ) +{ +struct arena_import *skel; +size_t sz; + + skel =arena_import__open_and_load (); +if (!skel && errno ==EOPNOTSUPP ) { +printf ("%s:SKIP:no arena support\n" , __func__); +test__skip (); +return NULL ; + } +if (!ASSERT_OK_PTR (skel,"open_and_load" )) +return NULL ; +if (skel->rodata->skip) { +printf ("%s:SKIP:the compiler lacks arena address space casts\n" , __func__); +test__skip (); +arena_import__destroy (skel); +return NULL ; + } + skel->bss->base =bpf_map__initial_value (skel->maps.arena, &sz); +if (!ASSERT_OK_PTR (skel->bss->base,"arena_base" )) { +arena_import__destroy (skel); +return NULL ; + } + skel->bss->page_size = ps; +return skel; +} + +/* Imported pages are not allocated by the arena and must not be counted */ +static long map_memlock (int map_fd) +{ +char path[64 ], line[128 ]; +long memlock =-1 ; +FILE *f; + +snprintf (path,sizeof (path),"/proc/self/fdinfo/%d" , map_fd); + f =fopen (path,"r" ); +if (!ASSERT_OK_PTR (f,"open_fdinfo" )) +return -1 ; +while (fgets (line,sizeof (line), f)) { +if (sscanf (line,"memlock:\t%ld" , &memlock) ==1 ) +break ; + } +fclose (f); +return memlock; +} + +/* Data written on either side is visible on the other one */ +static void test_share (void ) +{ +struct arena_import *skel; +__u64 *shm =MAP_FAILED ; +__u64 val; +int fd, i; + + fd =make_memfd (F_SEAL_SHRINK ); +if (fd <0 ) +return ; + skel =open_skel (); +if (!skel) + goto out; + +/* Import memfd pages 1-2 at test pages 2-3, see page_addr() */ +if (!ASSERT_OK (do_import (skel, fd, ps,2 ,2 ),"import" )) + goto out; +ASSERT_EQ (map_memlock (bpf_map__fd (skel->maps.arena)),0 ,"memlock" ); + + shm =mmap (NULL ,MEMFD_PAGES * ps,PROT_READ |PROT_WRITE ,MAP_SHARED , fd,0 ); +if (!ASSERT_NEQ (shm,MAP_FAILED ,"mmap_memfd" )) + goto out; + +do_write (skel,2 ,0xcafe ); +do_write (skel,3 ,0xbeef ); +ASSERT_EQ (shm[ps /sizeof (*shm)],0xcafe ,"bpf_to_user_pg2" ); +ASSERT_EQ (shm[2 * ps /sizeof (*shm)],0xbeef ,"bpf_to_user_pg3" ); + + shm[ps /sizeof (*shm)] =0xf00d ; +ASSERT_EQ (do_read (skel,2 ),0xf00d ,"user_to_bpf" ); + +/* Pages outside of the imported range are not shared */ +if (!ASSERT_OK (do_alloc (skel,0 ,1 ),"alloc" )) + goto out; +do_write (skel,0 ,0xdead ); +ASSERT_EQ (do_read (skel,0 ),0xdead ,"allocated" ); +ASSERT_EQ (shm[0 ],0 ,"not_shared" ); + +/* +* The data outlives the arena. The arena releases the pages after an +* RCU grace period: sealing the memfd against writes fails while they +* are pinned, so wait until it works (no writable mapping either). +*/ +arena_import__destroy (skel); + skel =NULL ; +munmap (shm,MEMFD_PAGES * ps); + shm =MAP_FAILED ; +for (i =0 ; i <500 &&fcntl (fd,F_ADD_SEALS ,F_SEAL_WRITE ); i++) +usleep (10000 ); +if (ASSERT_LT (i,500 ,"released" ) && +ASSERT_EQ (pread (fd, &val,sizeof (val),2 * ps),sizeof (val),"pread" )) +ASSERT_EQ (val,0xbeef ,"after_destroy" ); +out: +if (shm !=MAP_FAILED ) +munmap (shm,MEMFD_PAGES * ps); +arena_import__destroy (skel); +close (fd); +} + +/* Freeing any imported page releases the whole import, the data stays in the memfd */ +static void test_free (void ) +{ +struct arena_import *skel; +__u64 *shm =MAP_FAILED ; +int fd; + + fd =make_memfd (F_SEAL_SHRINK ); +if (fd <0 ) +return ; + skel =open_skel (); +if (!skel) + goto out; + + shm =mmap (NULL ,MEMFD_PAGES * ps,PROT_READ |PROT_WRITE ,MAP_SHARED , fd,0 ); +if (!ASSERT_NEQ (shm,MAP_FAILED ,"mmap_memfd" )) + goto out; + +/* Allocated page 0, memfd pages 0-1 imported at 1-2, allocated page 3 */ +if (!ASSERT_OK (do_alloc (skel,0 ,1 ),"alloc_pg0" ) || + !ASSERT_OK (do_import (skel, fd,0 ,1 ,2 ),"import" ) || + !ASSERT_OK (do_alloc (skel,3 ,1 ),"alloc_pg3" )) + goto out; +do_write (skel,0 ,0x1 ); +do_write (skel,1 ,0x1234 ); +do_write (skel,2 ,0x5678 ); +do_write (skel,3 ,0x3 ); + +/* Page 2 only: pages 1-2 go away, 0 and 3 stay */ +do_free (skel,2 ,1 ); +ASSERT_EQ (do_read (skel,1 ),0 ,"pg1_unmapped" ); +ASSERT_EQ (do_read (skel,2 ),0 ,"pg2_unmapped" ); +ASSERT_EQ (do_read (skel,0 ),0x1 ,"pg0_kept" ); +ASSERT_EQ (do_read (skel,3 ),0x3 ,"pg3_kept" ); +ASSERT_EQ (shm[0 ],0x1234 ,"memfd_kept" ); + +/* The range can be imported again */ +if (!ASSERT_OK (do_import (skel, fd,0 ,1 ,2 ),"reimport" )) + goto out; +ASSERT_EQ (do_read (skel,2 ),0x5678 ,"reimported" ); + +/* Across allocated and imported pages, then the whole arena is free */ +do_free (skel,0 ,4 ); +ASSERT_EQ (map_memlock (bpf_map__fd (skel->maps.arena)),0 ,"memlock" ); +ASSERT_OK (do_alloc (skel,0 ,8 ),"alloc_all" ); +out: +if (shm !=MAP_FAILED ) +munmap (shm,MEMFD_PAGES * ps); +arena_import__destroy (skel); +close (fd); +} + +static void test_invalid (void ) +{ +int fd, ro_fd =-1 , wo_fd =-1 , null_fd =-1 ; +struct arena_import *skel; +char path[64 ]; + + skel =open_skel (); +if (!skel) +return ; + +/* Write seals are not allowed */ + fd =make_memfd (F_SEAL_SHRINK |F_SEAL_FUTURE_WRITE ); +if (fd >=0 ) { +ASSERT_EQ (do_import (skel, fd,0 ,0 ,1 ), -EPERM ,"write_seal" ); +close (fd); + } + + fd =make_memfd (F_SEAL_SHRINK ); +if (fd <0 ) + goto out; + +/* The memfd must be open for reading and writing, like for a shared mapping */ +snprintf (path,sizeof (path),"/proc/self/fd/%d" , fd); + ro_fd =open (path,O_RDONLY ); +if (ASSERT_OK_FD (ro_fd,"open_ro" )) +ASSERT_EQ (do_import (skel, ro_fd,0 ,0 ,1 ), -EPERM ,"read_only" ); + wo_fd =open (path,O_WRONLY ); +if (ASSERT_OK_FD (wo_fd,"open_wo" )) +ASSERT_EQ (do_import (skel, wo_fd,0 ,0 ,1 ), -EPERM ,"write_only" ); + +/* Not a memfd */ + null_fd =open ("/dev/null" ,O_RDWR ); +if (ASSERT_OK_FD (null_fd,"open_null" )) +ASSERT_EQ (do_import (skel, null_fd,0 ,0 ,1 ), -EINVAL ,"not_memfd" ); + +ASSERT_EQ (do_import (skel,-1 ,0 ,0 ,1 ), -EBADF ,"bad_fd" ); +ASSERT_EQ (do_import (skel, fd,1 ,0 ,1 ), -EINVAL ,"unaligned_off" ); +ASSERT_EQ (do_import (skel, fd,0 ,0 ,MEMFD_PAGES +1 ), -EINVAL ,"beyond_memfd" ); +ASSERT_EQ (do_import (skel, fd,0 ,7 ,2 ), -EINVAL ,"beyond_arena" ); +ASSERT_EQ (do_import (skel, fd,0 ,0 ,0 ), -EINVAL ,"zero_pages" ); + +/* The range must be free */ +if (ASSERT_OK (do_alloc (skel,0 ,1 ),"alloc" )) +ASSERT_EQ (do_import (skel, fd,0 ,0 ,2 ), -EBUSY ,"over_allocated" ); +if (ASSERT_OK (do_import (skel, fd,0 ,2 ,2 ),"import" )) +ASSERT_EQ (do_import (skel, fd,0 ,3 ,1 ), -EBUSY ,"over_imported" ); +do_free (skel,0 ,4 ); + +/* Nothing above may leave a range behind */ +ASSERT_OK (do_alloc (skel,0 ,8 ),"alloc_all" ); +out: +if (null_fd >=0 ) +close (null_fd); +if (ro_fd >=0 ) +close (ro_fd); +if (wo_fd >=0 ) +close (wo_fd); +if (fd >=0 ) +close (fd); +arena_import__destroy (skel); +} + +static char *fault_expected; + +static void fault_handler (int sig,siginfo_t *si,void *uc) +{ +_exit (si->si_addr == fault_expected ?3 :4 ); +} + +/* User space accesses imported pages through the memfd, not through the arena */ +static void test_user_fault (void ) +{ +struct arena_import *skel; +int fd, status, ret; +pid_t pid; + + fd =make_memfd (F_SEAL_SHRINK ); +if (fd <0 ) +return ; + skel =open_skel (); +if (!skel) + goto out; + +if (!ASSERT_OK (do_import (skel, fd,0 ,1 ,1 ),"import" )) + goto out; + + pid =fork (); +if (!ASSERT_GE (pid,0 ,"fork" )) + goto out; +if (!pid) { +/* The arena mapping is not inherited, map it again */ +struct sigaction sa = { .sa_sigaction = fault_handler, .sa_flags =SA_SIGINFO }; +size_t sz =bpf_map__max_entries (skel->maps.arena) * ps; +char *p; + + p =mmap (skel->bss->base, sz,PROT_READ |PROT_WRITE , +MAP_SHARED |MAP_FIXED ,bpf_map__fd (skel->maps.arena),0 ); +if (p ==MAP_FAILED ||sigaction (SIGBUS , &sa,NULL )) +_exit (1 ); +/* Test pages 0 and 1, see page_addr() */ + p[ps] =1 ;/* allocated on demand */ + fault_expected = p +2 * ps; +/* Set before the store that faults */ +atomic_signal_fence (memory_order_seq_cst); + p[2 * ps] =1 ;/* imported */ +_exit (2 ); + } +/* The watchdog of the runner interrupts system calls */ +do { + ret =waitpid (pid, &status,0 ); + }while (ret <0 && errno ==EINTR ); +if (ASSERT_EQ (ret, pid,"waitpid" )) { +ASSERT_TRUE (WIFEXITED (status),"exited" ); +/* 3: SIGBUS on the imported page, 4: elsewhere, 2: no fault */ +ASSERT_EQ (WEXITSTATUS (status),3 ,"sigbus_on_imported" ); + } +out: +arena_import__destroy (skel); +close (fd); +} + +/* +* Only a device-dax mapping can be imported by address. This needs no +* device-dax device: it only checks that everything else is refused. +*/ +static void test_dax_invalid (void ) +{ +char *anon =MAP_FAILED , *shm =MAP_FAILED ; +struct arena_import *skel; +int fd =-1 ; + + skel =open_skel (); +if (!skel) +return ; + + anon =mmap (NULL ,2 * ps,PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (!ASSERT_NEQ (anon,MAP_FAILED ,"mmap_anon" )) + goto out; + anon[0 ] =1 ; +ASSERT_EQ (do_import_dax (skel, anon,0 ,1 ), -EINVAL ,"anon" ); +ASSERT_EQ (do_import_dax (skel, anon +1 ,0 ,1 ), -EINVAL ,"unaligned_src" ); +ASSERT_EQ (do_import_dax (skel, anon,0 ,0 ), -EINVAL ,"zero_pages" ); +ASSERT_EQ (do_import_dax (skel, anon,0 ,bpf_map__max_entries (skel->maps.arena) +1 ), + -EINVAL ,"too_many_pages" ); +if (ASSERT_OK (munmap (anon + ps, ps),"munmap" )) +ASSERT_EQ (do_import_dax (skel, anon + ps,0 ,1 ), -EINVAL ,"unmapped" ); + +/* Memory with a file behind it is imported by file */ + fd =make_memfd (F_SEAL_SHRINK ); +if (fd <0 ) + goto out; + shm =mmap (NULL , ps,PROT_READ |PROT_WRITE ,MAP_SHARED , fd,0 ); +if (ASSERT_NEQ (shm,MAP_FAILED ,"mmap_memfd" )) +ASSERT_EQ (do_import_dax (skel, shm,0 ,1 ), -EINVAL ,"memfd" ); + +/* Nothing above may leave a range behind */ +ASSERT_OK (do_alloc (skel,0 ,8 ),"alloc_all" ); +out: +if (shm !=MAP_FAILED ) +munmap (shm, ps); +/* Both pages, the second one may still be mapped */ +if (anon !=MAP_FAILED ) +munmap (anon,2 * ps); +if (fd >=0 ) +close (fd); +arena_import__destroy (skel); +} + +void serial_test_arena_import (void ) +{ + ps =sysconf (_SC_PAGESIZE); + +if (test__start_subtest ("share" )) +test_share (); +if (test__start_subtest ("free" )) +test_free (); +if (test__start_subtest ("invalid" )) +test_invalid (); +if (test__start_subtest ("user_fault" )) +test_user_fault (); +if (test__start_subtest ("dax_invalid" )) +test_dax_invalid (); +}
tools/testing/selftests/bpf/progs/arena_import.c new file +93 -0
93 lines not on this page · raw message
Signature · 1 line
2.56.1
Hi Andrea,
Thanks for the v2 and for sharing the scx_pvhost/scx_pvguest demo branch. Thought of discussing the possibility of using the pvsched transport with the bpf arena framework implemented in this series.
Quoted 66 lines · Andrea Righi
As discussed in the BPF track and the
sched_extmicroconference 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 guestsched_extschedulers.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 withBPF_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 theirdev_pagemapuntil 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
=============================
Quoted 9 lines
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_extschedulers,scx_pvhoston the host andscx_pvguestin 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.
As Josh mentioned on v1, we have been developing pvsched [5] -- a lightweight guest/VMM/host paravirt transport (with QEMU and Cloud Hypervisor support) based on the "guest allocates ordinary RAM, passes GPAs via a small PCI BAR + synchronous doorbell, host pins via /dev/pvsched" model. We recently added external-policy support so the shared pages after a small negotiation header are completely opaque to the kernel framework.
Looking at Patch 1, arena_map_ext_pages() (along with import_memfd for host/userspace sharing) already takes an arbitrary struct page ** array and maps it contiguously into arena VA space. That means for VMs we can layer your shared-arena model directly on top of the pvsched transport (on both host and guest) and get the best of both approaches:
1. No extra memory backend or devdax/nvdimm stack:
Instead of a separate 16 MiB pmem file per VM (plus guest ZONE_DEVICE memmap overhead, libnvdimm/dax_pmem config, and sysfs rebinding) for sharing a few pages at a hardcoded 2 MiB offset, the guest allocates ordinary 4 KiB RAM pages (including from the right guest NUMA nodes) and registers their GPAs in BAR1. Both sides still map that page list as a flat, contiguous virtual slice in their BPF arenas via arena_map_ext_pages(), and Patch 2 (import_dax, along with its dev_pagemap unbind blocking and mem_hotplug_lock ordering) wouldn't be needed for VMs.
2. Clean lifetime and synchronous revocation:
In the guest, pvsched_guest owns the allocated RAM pages across guest scheduler reloads, so scx_pvguest exiting never returns shared pages to the guest buddy allocator while attached; on driver unbind or kexec, a synchronous DOORBELL=0 detaches on the host first. On the host, /dev/pvsched detach callbacks know the exact instant a VM detaches or exits and can synchronously unmap the imported arena PTEs (relying on ex_handler_bpf zeroing loads/dropping stores), avoiding stale writes or blocked device unbinds.
3. Up-front negotiation, authoritative vCPU identity, and doorbell:
BAR0 + the page header give an explicit handshake for policy name, version, and layout before anything goes live, and /dev/pvsched binds pages directly to VMM runner TIDs and KVM vcpu_ids (avoiding inotify + /proc/*/maps + thread comm scraping across QEMU, CHV, Firecracker, crosvm, etc.). BAR0 also provides a kick doorbell and optional KVM boundary hooks when a policy wants event-driven wakeups in addition to polling.
4. Consumer-agnostic on both ends:
Decoupling the transport from the consumer lets scx_pvhost <-> scx_pvguest use shared BPF arenas as in your demo, while also allowing mixed setups -- e.g., keeping stock EEVDF in the guest (publishing via thin in-kernel hooks without replacing guest task scheduling, which you noted in scx-pvsched.md's TODO) consumed by a host sched_ext scheduler or kernel module.
5. Live migration and Confidential VMs:
With memfd + arena->kern_vm, host BPF writes go through init_mm and aren't tracked by user PTE / KVM dirty logging during pre-copy migration, and x-ignore-shared conflicts with VMs whose main RAM uses shared memfd (e.g. for vhost-user). With guest-RAM pages + pvsched, pages migrate as normal guest RAM: source VMM detaches at stop-and-copy (unmapping the arena import and marking pages dirty before the final sync), and destination VMM replays BAR1 into destination /dev/pvsched before vCPU resume (immediately disabling via header status if the destination runs no host scheduler, with zero blackout). Guest-allocated pages also map naturally to CoCo VMs via set_memory_decrypted() before GPA registration.
On the pvsched side, supporting scx_pvhost/scx_pvguest cleanly would just mean:
- making the pvsched PCI transport + /dev/pvsched arch-independent so arm64 works alongside x86_64,
- allowing an ordered multi-page VM slice in BAR1 (not just 1 page per vCPU), and
- exposing a thin kernel helper around arena_map_ext_pages() / arena_unmap_import() in kernel/bpf/arena.c so host and guest kfuncs can import and synchronously revoke those pages.
I am working on a PoC using the paravirt transport from pvsched for the bpf arena framework in this series. Will share it soon.
We are also thinking of extracting the transport side of pvsched into a standalone pvshm framework, with pvsched as a consumer of pvshm. I am envisioning pvshm as the transport framework for guest/host BPF arena imports as well, while pvsched remains a standalone scheduling framework supporting kernel-module or BPF-based policies.
Please let me know your thoughts about this.
Thanks,
Vineeth
[5] https://github.com/pvsched/linux
Quoted 56 lines
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.mdin 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: thesched_extuse 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/[email protected]/
[4] https://lore.kernel.org/r/[email protected]/Git tree:
git://git.kernel.org/pub/scm/linux/kernel/git/arighi/linux.gitscx-pvschedChanges in v2:
- DropBPF_F_ARENA_NO_FREE,BPF_F_ARENA_EXPORTand the sized bpffs file, and map pages that the arena didn't allocate instead (Alexei Starovoitov)
- Import the host's pages from a memfd withmemfd_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 withbpf_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/[email protected]/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 arenakernel/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
Andrea Righi
reviewerHi Vineeth,
Quoted 5 lines · Vineeth Pillai
Hi Andrea,
Thanks for the v2 and for sharing the scx_pvhost/scx_pvguest demo branch. Thought of discussing the possibility of using the pvsched transport with the bpf arena framework implemented in this series.
Thanks for looking at this and for the detailed write-up, this is very helpful. Comments below.
Quoted 86 lines · Andrea Righi
Quoted 66 lines
As discussed in the BPF track and the
sched_extmicroconference 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 guestsched_extschedulers.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 withBPF_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 theirdev_pagemapuntil 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
=============================Quoted 9 lines
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_extschedulers,scx_pvhoston the host andscx_pvguestin 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.As Josh mentioned on v1, we have been developing pvsched [5] -- a lightweight guest/VMM/host paravirt transport (with QEMU and Cloud Hypervisor support) based on the "guest allocates ordinary RAM, passes GPAs via a small PCI BAR + synchronous doorbell, host pins via
/dev/pvsched" model. We recently added external-policy support so the shared pages after a small negotiation header are completely opaque to the kernel framework.
Quoted 5 lines
Looking at Patch 1,
arena_map_ext_pages()(along withimport_memfdfor host/userspace sharing) already takes an arbitrary struct page ** array and maps it contiguously into arena VA space. That means for VMs we can layer your shared-arena model directly on top of the pvsched transport (on both host and guest) and get the best of both approaches:
Agreed, that was the idea behind keeping arena_map_ext_pages() generic:
the arena doesn't care where the pages come from, memfd and device-dax are just the two suppliers I could use without touching the VMM.
Quoted 11 lines
1. No extra memory backend or devdax/nvdimm stack:
Instead of a separate 16 MiB pmem file per VM (plus guestZONE_DEVICEmemmap overhead, libnvdimm/dax_pmem config, and sysfs rebinding) for sharing a few pages at a hardcoded 2 MiB offset, the guest allocates ordinary 4 KiB RAM pages (including from the right guest NUMA nodes) and registers their GPAs in BAR1. Both sides still map that page list as a flat, contiguous virtual slice in their BPF arenas viaarena_map_ext_pages(), and Patch 2 (import_dax, along with itsdev_pagemapunbind blocking andmem_hotplug_lockordering) wouldn't be needed for VMs.
Yes, the device-dax path is mostly a way to get the whole machinery working today with stock QEMU and Firecracker. The memory cost is small:
the backing file is sparse on the host, so only the shared pages (and the namespace info block) are allocated, and the guest pays ~256 KB for the memmap of the 16 MiB device. But it does need the extra device, the nvdimm/dax stack and the namespace setup in the guest, and with a proper transport the guest side becomes much simpler.
I'd still like to keep the device-dax import as an optional supplier, since it works with existing VMMs and lets people experiment with shared arenas right now. It may be deprecated in the future in favor of a paravirt transport like pvsched/pvshm once that is available. Does that sound reasonable?
Quoted 10 lines
2. Clean lifetime and synchronous revocation:
In the guest,pvsched_guestowns the allocated RAM pages across guest scheduler reloads, soscx_pvguestexiting never returns shared pages to the guest buddy allocator while attached; on driver unbind or kexec, a synchronous DOORBELL=0 detaches on the host first. On the host,/dev/pvscheddetach callbacks know the exact instant a VM detaches or exits and can synchronously unmap the imported arena PTEs (relying onex_handler_bpfzeroing loads/dropping stores), avoiding stale writes or blocked device unbinds.
Yeah, this is the part I'm missing with device-dax: the dax core doesn't tell its users that a device is going away, so there's no way to revoke the pages and the unbind just blocks.
Quoted 9 lines
3. Up-front negotiation, authoritative vCPU identity, and doorbell:
BAR0 + the page header give an explicit handshake for policy name, version, and layout before anything goes live, and/dev/pvschedbinds pages directly to VMM runner TIDs and KVMvcpu_ids(avoiding inotify +/proc/*/maps+ thread comm scraping across QEMU, CHV, Firecracker, crosvm, etc.). BAR0 also provides a kick doorbell and optional KVM boundary hooks when a policy wants event-driven wakeups in addition to polling.
Agreed, the vCPU discovery in the demo is just there to avoid touching the VMM, it's the least robust part and an authoritative binding from the transport would be much better.
Quoted 8 lines
4. Consumer-agnostic on both ends:
Decoupling the transport from the consumer letsscx_pvhost<->scx_pvguestuse shared BPF arenas as in your demo, while also allowing mixed setups -- e.g., keeping stock EEVDF in the guest (publishing via thin in-kernel hooks without replacing guest task scheduling, which you noted in scx-pvsched.md's TODO) consumed by a hostsched_extscheduler or kernel module.
Makes sense, and that's the main reason I'd like the arena side to stay a generic primitive and not grow anything pvsched/VM-specific.
Quoted 13 lines
5. Live migration and Confidential VMs:
With memfd +arena->kern_vm, host BPF writes go throughinit_mmand aren't tracked by user PTE / KVM dirty logging during pre-copy migration, and x-ignore-shared conflicts with VMs whose main RAM uses shared memfd (e.g. for vhost-user). With guest-RAM pages + pvsched, pages migrate as normal guest RAM: source VMM detaches at stop-and-copy (unmapping the arena import and marking pages dirty before the final sync), and destination VMM replays BAR1 into destination/dev/pvschedbefore vCPU resume (immediately disabling via header status if the destination runs no host scheduler, with zero blackout). Guest-allocated pages also map naturally to CoCo VMs viaset_memory_decrypted()before GPA registration.
Live migration actually works with the scx-pvsched demo already: the shared pages only hold soft state that both sides can rebuild. Every time the host picks up a VM it writes a new epoch in the shared header. If the migration copies the shared pages, the destination host sees its epoch overwritten by the source host's and writes its own back; if it doesn't, the guest sees a new epoch and writes its entries again. Either way the guest is back under the new host's fair share within a second or two, and if the destination runs no host scheduler, the heartbeat stops and the guest behaves like a normal VM.
That said, having the transport detach and re-attach the pages explicitly is cleaner than relying on the epoch, and it would let the destination host know about the VM before it resumes.
Confidential VMs are a known limitation of v2, and a transport that registers the pages after set_memory_decrypted() solves that naturally.
Quoted 10 lines
On the pvsched side, supporting scx_pvhost/scx_pvguest cleanly would just mean:
- making the pvsched PCI transport +/dev/pvschedarch-independent so arm64 works alongsidex86_64,
- allowing an ordered multi-page VM slice in BAR1 (not just 1 page per vCPU), and
- exposing a thin kernel helper aroundarena_map_ext_pages()/arena_unmap_import()inkernel/bpf/arena.cso host and guest kfuncs can import and synchronously revoke those pages.
I can add something like this in v3:
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 caller keeps ownership of the pages: the arena calls the release callback exactly once, whether the import goes away through bpf_arena_unmap_ext_pages(), bpf_arena_free_pages() or arena destruction (the memfd and dax kfuncs use the same callback).
The unmap is synchronous: on return the PTEs are gone, the TLB is flushed and the callback has run. The range then reads as zero from BPF and goes back to the arena. It returns -ENOENT if addr isn't the start of the caller's import (priv must match) or if it was already released.
Would that work for the /dev/pvsched detach path?
Quoted 3 lines
I am working on a PoC using the paravirt transport from pvsched for the bpf arena framework in this series. Will share it soon.
Great, looking forward to it!
Quoted 6 lines
We are also thinking of extracting the transport side of pvsched into a standalone pvshm framework, with pvsched as a consumer of pvshm. I am envisioning pvshm as the transport framework for guest/host BPF arena imports as well, while pvsched remains a standalone scheduling framework supporting kernel-module or BPF-based policies.
I like this split and I think a generic shared memory transport, with BPF arenas as one consumer and pvsched as another, fits well with keeping the arena side generic.
Thanks,
-Andrea
Quoted 65 lines
Please let me know your thoughts about this.
Thanks,
Vineeth[5] https://github.com/pvsched/linux
Quoted 56 lines
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.mdin 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: thesched_extuse 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/[email protected]/
[4] https://lore.kernel.org/r/[email protected]/Git tree:
git://git.kernel.org/pub/scm/linux/kernel/git/arighi/linux.gitscx-pvschedChanges in v2:
- DropBPF_F_ARENA_NO_FREE,BPF_F_ARENA_EXPORTand the sized bpffs file, and map pages that the arena didn't allocate instead (Alexei Starovoitov)
- Import the host's pages from a memfd withmemfd_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 withbpf_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/[email protected]/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 arenakernel/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