BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Andrea Righi" <arighi@nvidia.com>
Cc: bpf@vger.kernel.org, sched-ext@lists.linux.dev,
	nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org
Subject: Re: [PATCH 5/5] selftests/bpf: Test importing pages into an arena
Date: Wed, 02 Dec 2026 18:42:36 +0000	[thread overview]
Message-ID: <sashiko-outbox-246443@kernel.org> (raw)
In-Reply-To: <20261202182648.2239421-6-arighi@nvidia.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] selftests/bpf: test_dax_invalid() fails when CONFIG_DAX is
  not built-in
- [Medium] selftests/bpf: unsynchronized arena_ext state in bpf_testmod
  kfuncs
--

commit 8cbf53ce81f32ae5f961370e9a60385a4198f27f
Author: Andrea Righi <arighi@nvidia.com>

selftests/bpf: Test importing pages into an arena

This commit adds BPF selftests and bpf_testmod kfuncs to test importing
memfd, device-dax, and external kernel pages into a BPF arena.

> diff --git a/tools/testing/selftests/bpf/prog_tests/arena_import.c b/tools/testing/selftests/bpf/prog_tests/arena_import.c
> new file mode 100644
> index 0000000000000..95d7371fd8532
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/arena_import.c
[ ... ]
> +/*
> + * 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");

[Severity: Medium]
Does test_dax_invalid() fail instead of skipping when CONFIG_DAX is disabled
or built as a module (including kernels built with
tools/testing/selftests/bpf/config)?

In kernel/bpf/arena.c, arena_import_dax() returns -EOPNOTSUPP before
checking src alignment or VMA type:

kernel/bpf/arena.c:arena_import_dax() {
	/* The dax SRCU, below, can't be reached from here if the dax core is a module */
	if (!IS_BUILTIN(CONFIG_DAX))
		return -EOPNOTSUPP;
	/* Kernel code may run the program without an address space */
	src = untagged_addr(src);
	if (!mm || (src & ~PAGE_MASK))
		return -EINVAL;
    ...
}

When CONFIG_DAX is not built-in, the do_import_dax() calls with page_cnt == 1
return -EOPNOTSUPP rather than -EINVAL, failing these assertions.

> diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
> index 86d25638a7278..57f56bd01bbf2 100644
> --- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
> +++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
> @@ -1607,6 +1607,91 @@ __bpf_kfunc void bpf_kfunc_call_test_sleepable(void)
>  {
>  }
>  
> +#define ARENA_EXT_MAGIC	0xa5e0a5e0ULL
> +
> +/* Pages that the module imports into an arena, one import at a time */
> +static struct {
> +	struct bpf_map *map;
> +	u64 addr;
> +	atomic_t released;
> +} arena_ext;
[ ... ]
> +/* Like a paravirt transport: map pages that the module keeps into an arena */
> +__bpf_kfunc int bpf_kfunc_arena_ext_map(void *p__map, void *addr__ign, u32 page_cnt)
> +{
> +	struct bpf_map *map = p__map;
> +	struct page **pages;
> +	u32 i;
> +	int ret;
> +
> +	if (!page_cnt || page_cnt > 16)
> +		return -EINVAL;
> +	if (arena_ext.map)
> +		return -EBUSY;
> +
> +	pages = kvcalloc(page_cnt, sizeof(*pages), GFP_KERNEL);
> +	if (!pages)
> +		return -ENOMEM;
> +	for (i = 0; i < page_cnt; i++) {
> +		pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
> +		if (!pages[i]) {
> +			ret = -ENOMEM;
> +			goto err;
> +		}
> +		*(u64 *)page_address(pages[i]) = ARENA_EXT_MAGIC + i;
> +	}
> +
> +	ret = bpf_arena_map_ext_pages(map, (u64)addr__ign, pages, page_cnt,
> +				      bpf_testmod_arena_ext_release, &arena_ext);
> +	if (ret)
> +		goto err;
> +	/* Until the pages are revoked, also when BPF frees them first */
> +	bpf_map_inc(map);
> +	arena_ext.map = map;
> +	arena_ext.addr = (u64)addr__ign;
> +	return 0;
> +err:
> +	while (i--)
> +		__free_page(pages[i]);
> +	kvfree(pages);
> +	return ret;
> +}
> +
> +__bpf_kfunc int bpf_kfunc_arena_ext_unmap(void *p__map, void *addr__ign)
> +{
> +	int ret;
> +
> +	ret = bpf_arena_unmap_ext_pages(p__map, (u64)addr__ign, &arena_ext);
> +	if (p__map == arena_ext.map && (u64)addr__ign == arena_ext.addr) {
> +		bpf_map_put(arena_ext.map);
> +		arena_ext.map = NULL;
> +	}
> +	return ret;
> +}

[Severity: Medium]
Can concurrent calls to bpf_kfunc_arena_ext_map() and
bpf_kfunc_arena_ext_unmap() race on arena_ext.map and arena_ext.addr?

Both sleepable kfuncs access arena_ext without a lock.  In
bpf_kfunc_arena_ext_unmap(), two concurrent callers passing the active map
and addr can both pass the check and call bpf_map_put(arena_ext.map),
causing a double bpf_map_put() and use-after-free on struct bpf_map, or a
NULL pointer dereference in bpf_map_put(NULL) if arena_ext.map is reloaded
after being cleared.

In bpf_kfunc_arena_ext_map(), two concurrent callers can both observe
arena_ext.map == NULL across the sleeping allocations and
bpf_arena_map_ext_pages(), overwriting arena_ext.map and leaking the first
map reference (leaving the first import active if bpf_testmod is unloaded).

Also, bpf_arena_map_ext_pages() and bpf_arena_unmap_ext_pages() truncate the
address to u32 when indexing the arena, whereas arena_ext.addr compares the
full 64-bit value, so unmapping with different upper 32 bits revokes the
import without dropping arena_ext.map.  Serializing these kfuncs with a
mutex and comparing (u32)(u64)addr__ign would avoid both issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202182648.2239421-1-arighi@nvidia.com?part=5

      reply	other threads:[~2026-12-02 18:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 18:23 [PATCH v3 bpf-next 0/5] bpf: Import external pages into an arena 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 ` [PATCH 3/5] bpf: Let kernel code import " Andrea Righi
2026-12-02 18:41   ` sashiko-bot
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
2026-12-02 18:42   ` sashiko-bot [this message]

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=sashiko-outbox-246443@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=arighi@nvidia.com \
    --cc=bpf@vger.kernel.org \
    --cc=linux-cxl@vger.kernel.org \
    --cc=nvdimm@lists.linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sched-ext@lists.linux.dev \
    /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