From: Andrea Righi <arighi@nvidia.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
Vishal Verma <vishal.l.verma@intel.com>,
Dave Jiang <dave.jiang@intel.com>,
Alison Schofield <alison.schofield@intel.com>,
Matthew Wilcox <willy@infradead.org>, Jan Kara <jack@suse.cz>,
Tejun Heo <tj@kernel.org>, David Vernet <void@manifault.com>,
Changwoo Min <changwoo@igalia.com>, Josh Don <joshdon@google.com>,
Vineeth Pillai <vineethrp@google.com>,
Peilin Ye <yepeilin@google.com>, Zhi Wang <zhiw@nvidia.com>,
Lee Trager <ltrager@nvidia.com>,
Balbir Singh <balbirs@nvidia.com>,
Shameer Kolothum <skolothumtho@nvidia.com>,
Himadri Chhaya-Shailesh <himadrics@protonmail.com>,
NchangRoy <royfru44@gmail.com>,
bpf@vger.kernel.org, sched-ext@lists.linux.dev,
nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 3/6] bpf: Let kernel code import pages into an arena
Date: Sat, 5 Dec 2026 11:03:59 +0100 [thread overview]
Message-ID: <20261205100518.687948-4-arighi@nvidia.com> (raw)
In-Reply-To: <20261205100518.687948-1-arighi@nvidia.com>
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@google.com>
Signed-off-by: Andrea Righi <arighi@nvidia.com>
---
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
next prev parent reply other threads:[~2026-12-05 10:07 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 10:03 [PATCH v4 bpf-next 0/5] bpf: Import external " Andrea Righi
2026-12-05 10:03 ` [PATCH 1/6] dax: Declare inode_dax() in linux/dax.h Andrea Righi
2026-12-05 10:03 ` [PATCH 2/6] bpf: Allow importing memfd pages into an arena Andrea Righi
2026-12-05 10:03 ` Andrea Righi [this message]
2026-12-05 10:04 ` [PATCH 4/6] bpf: Allow importing device-dax " Andrea Righi
2026-12-05 10:04 ` [PATCH 5/6] selftests/bpf: Test importing " Andrea Righi
2026-12-05 10:45 ` bot+bpf-ci
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261205100518.687948-4-arighi@nvidia.com \
--to=arighi@nvidia.com \
--cc=alexei.starovoitov@gmail.com \
--cc=alison.schofield@intel.com \
--cc=andrii@kernel.org \
--cc=balbirs@nvidia.com \
--cc=bpf@vger.kernel.org \
--cc=changwoo@igalia.com \
--cc=daniel@iogearbox.net \
--cc=dave.jiang@intel.com \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=himadrics@protonmail.com \
--cc=ihor.solodrai@linux.dev \
--cc=jack@suse.cz \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=joshdon@google.com \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ltrager@nvidia.com \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=nvdimm@lists.linux.dev \
--cc=royfru44@gmail.com \
--cc=sched-ext@lists.linux.dev \
--cc=skolothumtho@nvidia.com \
--cc=song@kernel.org \
--cc=tj@kernel.org \
--cc=vineethrp@google.com \
--cc=vishal.l.verma@intel.com \
--cc=void@manifault.com \
--cc=willy@infradead.org \
--cc=yepeilin@google.com \
--cc=yonghong.song@linux.dev \
--cc=zhiw@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®