From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796236958; b=pLS9eWAD1Rkvz//BayZ7oQghFuPwNImRk+QdBmYKk8e02HsvF89MiNnilz8i9ivBls5+ BRepQthotjEzAkJZuSPpO+is8B5TnOKTgvjsbPx7d10MAhKWThzT++Nej7F2yh7mmCvLe c2SoS6MWrwRwSpzpRdk0Eg8vTWaXzX0Pm56ejwOczBa4/8q83xqZOI/24mMweglhPfFFm 9XBHzWM3e+MRxC+xWFnLSYsNitMhzjlBoS0W0FLHTL09Hrlstwmhv+kMXX8NIBy3pRyrA dehFeHrCF406AeurEjJuP5hp4U2K4sckMxd51Yx3QWoMggNgg0mgStCrYcdmNR4SVTw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796236958; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=MXUYFLZtP4ZjnUgKeUok90Qc+QFDWtDA/Mn2482Aqc4=; b=nkUr50aq1KAxe968IXtu+M5UVjsNLQ3Y88ysGUWrb8Rjdy4Tfh0WmPNgzRH5a5JL/emn 2lFkc5uhhOWzMjPEbbqV5TFzGErWFBB8/lb1PpZJyaHwVjcQMChqq7VH9U9seJuhq/boj QTyXC8dSdVCjJ/PQ8Y9ZZNubsXjFl97/tFZdRV6hRk5J2pe1x4oCzeGBpt7Ir6OrrUsrN /DI/RzVL1QdH4dQBa9OPhADGrAIi/ACbgYqZJngdxtSX5vedSPfY/EQz7wRu0yWxVXj/+ +7N87w38ODB4NFcxHoRG8+hx7uYxkvrNZDD+zbwwmVrSXOVU6b5k4n4/ecWIcjgM8KA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=A+zdH0da; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15BCB606A6A9; Wed, 2 Dec 2026 18:42:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=A+zdH0da Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5BF0A1F0089F; Wed, 2 Dec 2026 18:42:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796236957; bh=MXUYFLZtP4ZjnUgKeUok90Qc+QFDWtDA/Mn2482Aqc4=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=A+zdH0da8z+1X4gkH7gvI7LyqeRbAf7osF+56VZC7YR1lxY8HNmIFOxi8gGeB7GgX /dbXP+VfjSKJWvWMWgKGyhS25FVKgoY62eHROVBIZzL3I+Askjf+gVPME12crVmKXN /zuAc24FkdTp71oh04uDMGJb8sTypAJk0qhvyIWtSa8Tbl5yJChoaj4dgFj25A1R4s loiFpxn945OVKt8CMVJukwriEpev0L+sTe0zUjzeGfng363ODuu4evkJ5zZZ4CEZyk jULiAcrHn6nDdYRv7eJYlaPzgWqSjcTbIdrU0OdVKLtSsaqWL4YCFQT4J/isqk1FQL SNLcUy496oJLg== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 5/5] selftests/bpf: Test importing pages into an arena Reply-To: sashiko-reviews@lists.linux.dev To: "Andrea Righi" Cc: bpf@vger.kernel.org, sched-ext@lists.linux.dev, nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org In-Reply-To: <20261202182648.2239421-6-arighi@nvidia.com> References: <20261202182648.2239421-1-arighi@nvidia.com> <20261202182648.2239421-6-arighi@nvidia.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 18:42:36 +0000 X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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/tool= s/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 =3D MAP_FAILED, *shm =3D MAP_FAILED; > + struct arena_import *skel; > + int fd =3D -1; > + > + skel =3D open_skel(); > + if (!skel) > + return; > + > + anon =3D mmap(NULL, 2 * ps, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_A= NONYMOUS, -1, 0); > + if (!ASSERT_NEQ(anon, MAP_FAILED, "mmap_anon")) > + goto out; > + anon[0] =3D 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 mo= dule */ if (!IS_BUILTIN(CONFIG_DAX)) return -EOPNOTSUPP; /* Kernel code may run the program without an address space */ src =3D 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 = =3D=3D 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(voi= d) > { > } > =20 > +#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 ar= ena */ > +__bpf_kfunc int bpf_kfunc_arena_ext_map(void *p__map, void *addr__ign, u= 32 page_cnt) > +{ > + struct bpf_map *map =3D p__map; > + struct page **pages; > + u32 i; > + int ret; > + > + if (!page_cnt || page_cnt > 16) > + return -EINVAL; > + if (arena_ext.map) > + return -EBUSY; > + > + pages =3D kvcalloc(page_cnt, sizeof(*pages), GFP_KERNEL); > + if (!pages) > + return -ENOMEM; > + for (i =3D 0; i < page_cnt; i++) { > + pages[i] =3D alloc_page(GFP_KERNEL | __GFP_ZERO); > + if (!pages[i]) { > + ret =3D -ENOMEM; > + goto err; > + } > + *(u64 *)page_address(pages[i]) =3D ARENA_EXT_MAGIC + i; > + } > + > + ret =3D 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 =3D map; > + arena_ext.addr =3D (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 =3D bpf_arena_unmap_ext_pages(p__map, (u64)addr__ign, &arena_ext); > + if (p__map =3D=3D arena_ext.map && (u64)addr__ign =3D=3D arena_ext.addr= ) { > + bpf_map_put(arena_ext.map); > + arena_ext.map =3D 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 =3D=3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202182648.2239= 421-1-arighi@nvidia.com?part=3D5