[PATCH 3/6] bpf: Let kernel code import pages into an arena
From: Andrea Righi
Date: Sat Dec 05 2026 - 05:07:08 EST
A paravirt transport, such as pvsched, can share guest memory with the
host without a file: the guest registers ordinary pages with a device,
and the host driver pins them. Such a driver needs to map the pages into
an arena, and to revoke them synchronously when the guest detaches or
the VM goes away, without waiting for BPF programs to free them.
Add a kernel API around the imports of the arena:
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 pages are mapped like the ones of a memfd and stay the caller's:
the arena calls release once it no longer maps them, whether
bpf_arena_unmap_ext_pages() revoked them, a BPF program freed them or
the arena was destroyed. bpf_arena_unmap_ext_pages() returns after the
release, so the caller can free the pages then. BPF accesses to revoked
pages fault like the ones to free arena pages.
Suggested-by: Vineeth Pillai <vineethrp@xxxxxxxxxx>
Signed-off-by: Andrea Righi <arighi@xxxxxxxxxx>
---
include/linux/bpf.h | 21 +++++++++
kernel/bpf/arena.c | 109 +++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 128 insertions(+), 2 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 4f8d419fd824e..53521e65fc2f6 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -789,6 +789,27 @@ static inline void *bpf_arena_alloc_pages_sleepable(void *p__map, void *addr__ig
}
#endif
+#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_MMU) && defined(CONFIG_64BIT)
+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);
+#else
+static inline 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)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int bpf_arena_unmap_ext_pages(struct bpf_map *map, u64 addr, void *priv)
+{
+ return -EOPNOTSUPP;
+}
+#endif
+
extern const struct bpf_map_ops bpf_map_offload_ops;
/* bpf_type_flag contains a set of flags that are applicable to the values of
diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
index 2de13091750f3..35704ae12e69b 100644
--- a/kernel/bpf/arena.c
+++ b/kernel/bpf/arena.c
@@ -1101,7 +1101,7 @@ static void zap_pages(struct bpf_arena *arena, long uaddr, long page_cnt)
* from vmalloc area and from all user space vma-s, so that nobody can
* populate it while stale TLB entries or user ptes are still around.
*/
-static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt)
+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;
@@ -1117,7 +1117,7 @@ static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt)
int ret;
/* An overlapping free must not release the range before this one is done */
- guard(mutex)(&arena->zap_mutex);
+ lockdep_assert_held(&arena->zap_mutex);
ret = raw_res_spin_lock_irqsave(&arena->spinlock, flags);
if (ret)
@@ -1194,6 +1194,12 @@ static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt)
return 0;
}
+static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt)
+{
+ guard(mutex)(&arena->zap_mutex);
+ return __arena_free_range(arena, uaddr, page_cnt);
+}
+
static void arena_free_pages(struct bpf_arena *arena, long uaddr, long page_cnt, bool sleepable)
{
/* The first mmap() may be setting the user range right now */
@@ -1599,6 +1605,105 @@ static void arena_free_irq(struct irq_work *iw)
schedule_work(&arena->free_work);
}
+/**
+ * bpf_arena_map_ext_pages - map pages that a kernel supplier keeps into an arena
+ * @map: the arena, which the caller holds a reference to
+ * @addr: page-aligned arena address where the pages are mapped, as BPF
+ * programs see it
+ * @pages: the pages, kept by the caller until @release
+ * @page_cnt: number of pages
+ * @release: called once the pages are no longer mapped
+ * @priv: passed to @release, identifies the pages for bpf_arena_unmap_ext_pages()
+ *
+ * Map @pages at @addr, which must be free, for a supplier such as a
+ * paravirt transport. They are imported like the pages of a memfd, see
+ * bpf_arena_import_memfd(), until bpf_arena_unmap_ext_pages() revokes them,
+ * bpf_arena_free_pages() frees any of them or the arena is destroyed. Then
+ * the arena unmaps them and calls @release, which may sleep: BPF programs
+ * no longer access them. They write the pages through a kernel mapping,
+ * which doesn't mark them dirty.
+ *
+ * @release runs under a mutex of the arena: it must not take a lock that is
+ * held across bpf_arena_unmap_ext_pages().
+ *
+ * Context: process context, may sleep.
+ * Return: 0 on success, negative error code otherwise.
+ */
+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)
+{
+ struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
+
+ if (map->map_type != BPF_MAP_TYPE_ARENA || !page_cnt || !release)
+ return -EINVAL;
+ return arena_map_ext_pages(arena, addr, pages, page_cnt, release, priv);
+}
+EXPORT_SYMBOL_GPL(bpf_arena_map_ext_pages);
+
+/**
+ * bpf_arena_unmap_ext_pages - revoke pages mapped by bpf_arena_map_ext_pages()
+ * @map: the arena, which the caller holds a reference to
+ * @addr: the address the pages were mapped at
+ * @priv: the @priv they were mapped with
+ *
+ * Unmap the pages and call their release callback before returning, unless
+ * the arena released them already. Either way, BPF programs no longer access
+ * them when this returns: their accesses there fault like the ones to a free
+ * page of the arena.
+ *
+ * A bpf_arena_free_pages() of the pages that a BPF program called before this
+ * is complete when this returns, also if the arena deferred it. One that runs
+ * concurrently or later may free whatever is allocated at @addr next: the
+ * caller and the BPF programs have to agree on who releases the pages.
+ *
+ * Context: process context, may sleep.
+ * Return: 0 if the pages were released here, -ENOENT if they were already,
+ * -EINVAL if @map is not an arena or @addr is not page-aligned.
+ */
+int bpf_arena_unmap_ext_pages(struct bpf_map *map, u64 addr, void *priv)
+{
+ struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
+ struct mem_cgroup *new_memcg, *old_memcg;
+ struct arena_import *imp;
+ long pgoff, page_cnt = 0;
+ unsigned long flags;
+
+ if (map->map_type != BPF_MAP_TYPE_ARENA || (addr & ~PAGE_MASK))
+ return -EINVAL;
+ /* Pages were mapped: the user range is set */
+ if (!READ_ONCE(arena->user_vm_start))
+ return -ENOENT;
+ pgoff = compute_pgoff(arena, addr);
+
+ /*
+ * Complete the deferred frees: one of these pages would otherwise run
+ * after the range is free again, and free what is allocated there next
+ */
+ irq_work_sync(&arena->free_irq);
+ flush_work(&arena->free_work);
+
+ /* A concurrent free releases the pages before this finds them gone */
+ guard(mutex)(&arena->zap_mutex);
+
+ /* No other rqspinlock is held, so this one can only time out */
+ while (raw_res_spin_lock_irqsave(&arena->spinlock, flags))
+ cond_resched();
+ imp = arena_find_import(arena, pgoff);
+ if (imp && IMPORT_START(imp) == pgoff && imp->priv == priv)
+ page_cnt = imp->page_cnt;
+ raw_res_spin_unlock_irqrestore(&arena->spinlock, flags);
+ if (!page_cnt)
+ return -ENOENT;
+
+ bpf_map_memcg_enter(&arena->map, &old_memcg, &new_memcg);
+ while (__arena_free_range(arena, (u32)addr, page_cnt))
+ cond_resched();
+ bpf_map_memcg_exit(old_memcg, new_memcg);
+ return 0;
+}
+EXPORT_SYMBOL_GPL(bpf_arena_unmap_ext_pages);
+
__bpf_kfunc_start_defs();
__bpf_kfunc void *bpf_arena_alloc_pages(void *p__map, void *addr__ign, u32 page_cnt,
--
2.56.1