From: Andrea Righi <arighi@nvidia.com>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
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/5] bpf: Let kernel code import pages into an arena
Date: Wed, 2 Dec 2026 19:23:15 +0100 [thread overview]
Message-ID: <20261202182648.2239421-4-arighi@nvidia.com> (raw)
In-Reply-To: <20261202182648.2239421-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 | 96 ++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 115 insertions(+), 2 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 0fef8e2f43cc0..28872d5c854e4 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -787,6 +787,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 019261fb190d4..4c3a28d33546a 100644
--- a/kernel/bpf/arena.c
+++ b/kernel/bpf/arena.c
@@ -1108,7 +1108,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;
@@ -1124,7 +1124,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)
@@ -1199,6 +1199,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 */
@@ -1603,6 +1609,92 @@ 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.
+ *
+ * Context: process context, may sleep.
+ * Return: 0 if the pages were released here, -ENOENT if they were already.
+ */
+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);
+
+ /* 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-02 18:27 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 18:23 [PATCH v3 bpf-next 0/5] bpf: Import external " Andrea Righi
2026-12-02 18:23 ` [PATCH 1/5] dax: Declare inode_dax() in linux/dax.h Andrea Righi
2026-12-02 18:23 ` [PATCH 2/5] bpf: Allow importing memfd pages into an arena Andrea Righi
2026-12-02 18:23 ` Andrea Righi [this message]
2026-12-03 4:25 ` [PATCH 3/5] bpf: Let kernel code import " Alexei Starovoitov
2026-12-03 6:55 ` Andrea Righi
2026-12-03 7:51 ` Konstantin Ryabitsev
2026-12-02 18:23 ` [PATCH 4/5] bpf: Allow importing device-dax " Andrea Righi
2026-12-02 18:23 ` [PATCH 5/5] selftests/bpf: Test importing " Andrea Righi
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=20261202182648.2239421-4-arighi@nvidia.com \
--to=arighi@nvidia.com \
--cc=alison.schofield@intel.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=balbirs@nvidia.com \
--cc=bpf@vger.kernel.org \
--cc=changwoo@igalia.com \
--cc=daniel@iogearbox.net \
--cc=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®