From nobody Mon Nov 23 12:44:19 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795350979; b=om3uj2vc0JIaRTNnJ7KO3nwaiNDm3Hvd1vTKb7B7DDfssrIjg4ocPNswSIYPcrTs7je1 EfWcaw14xjNjaawVLOJkgCfyng99hYhcwIOQAuyx9Mwaz1rubaiGVBIHXl/CmZ0PTgsDV IG24IH82BE+XXGJVLreuE7+BF70P2F7QkD5JkSvqTvwaEOwC3bZx8rg5jToZw1IQSAdfW T7sDJqCW5SKa/xcM/oES+apCumBIwcXJs8F4te3Y4HCH22YfYrSe3hRDnZZcBVwX12g7O G3z7PTUm6sN3flNx6PGrmohj5nT7EXRXiT4udHWNripgCf9l30rkPjHwhZS6ipvE/gQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795350979; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=H8i5YVhr8pMD+/XAphh5neD9o3f3Uxu0RhXm2n+uogo=; b=AxDS5cwZrRe8ZNcnTX9vJucjiD1OMnPoYm/SdxpeJSHPuG161OclDECbbXo/mPCKDaqf 7Mv3BLRI1vEK/78Q5nTgUr1hVVRJrH4wBx5UsFs4m5OaIF1fTaA5AIRegSuSBhgiAKl2u MxyMeVBfryJ4d3ItMkUvGbOEbcHWvOuRlBBLgfeAAKMTQbW9Ax5aVxLMB/evLWPSI4Zpc 8ol9984jimO4w+p73Sq1BpeJmE/zTK74WJqc0DeKFmnYiSSJ1LUHCvOv9YBH0hjJEbaym UKAVDw2cBNfPzKxF/5y6QKccEATEWAUA81BjZ5vsQdkQ5XcLtILPzDsDL1DpE9oES2Q== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="cQpMD/YI"; arc=pass header.oldest-pass=0 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 4F4A5606A335; Sun, 22 Nov 2026 12:36:19 +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=cQpMD/YI Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795350979; b=miekTAsB90M7PvTA/NQ54JMKFQOFR1QDcNqhIYNmYZQkA3Bgt335prvKoap+/ehZrX8F 6RilfhUSAWqBlqGpRTM0Zk2OXBysYBN2VwtnFfrXTcqs6euY+D8FmDsTvRjmkG4xlUVZr NF05lf8FP8MyKxQH23xqZf6ju/Th1XCpGroTL2481BLl0g5ALJaglaPxMdT6ppsYHVy+T 3X3zv2eOgq5U+3HOk/11GvCIphKqxcj32u9ODUoIAAtPYz5WKLF0+5fMbtu/IAp5036U6 pvRFKOnCiaxo09FhxSCbPfAPFz0rSlqQXn3TNNduAb910MgZIYyd6iCbXwTPSAzz7bA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795350979; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=H8i5YVhr8pMD+/XAphh5neD9o3f3Uxu0RhXm2n+uogo=; b=TrAWAZien0NgSUNbV42CPMKMZfb+DJJHiR4b7LIYSiljko1Pxz1dVOYbvMvxoy4546ZO s2zdfFrdRku28oDVcfI6ymdiw/GvFu0KMQkeL0tdKs+4BHZ4xfKy9QOIo8WdKAE6RS7t+ HRe1Fu/RiII6DR7w5PTdE5+c0X7fJqEM+VrG0gOxvkSnK4owdmo1MEIkvMFkYdg3OLSxc 8jqjaObUFIz6bqZn+EH47rw50cebnWCXYhzZtzznn9MKT9Qyy7no6TK+FxXmVC7FB3l6Q 6qAKILV9mw2cxx+LzMVl7hFXcddjoYp5HwL+14eYGeTegHDfn/z/BiMRHQ6fsF7mtaA== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 8EF7E1F000CF; Sun, 22 Nov 2026 12:36:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795350979; bh=H8i5YVhr8pMD+/XAphh5neD9o3f3Uxu0RhXm2n+uogo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=cQpMD/YIpItzmUcUcii8EFmqUNHM4IMKZ5eqpjsU6uydU4HHCrtjMe5oOhxG5/mLf FvtOFDXseycqXyUFtcDX81w1wuhPTEj32l+0gEsRp7BO+TiTxJGfhs3FxX0YkAkeO6 vVt5zqXdyqlYq1tc2hkW3OLZ1VLlKy658d+MRnBN5v+8GHXRef/5U94enTc22T8Fqg 6/LWW81hdegJVfHKrRYjA3IjAyh5H/i0TO+N0WPBoaDVhjwP39ZIFVzDRFD2Jg1Gj9 qyeADsutrOUXysHpQQq53x3wK0yaIey36s3n6t0nDarzjV45ACgIFEXcpqdl9sz8QA o96hFG0xX5G8g== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 0CF974C02B; Sun, 22 Nov 2026 12:36:12 +0000 (UTC) From: YanXin Li via B4 Relay Date: Sun, 22 Nov 2026 20:35:36 +0800 Subject: [PATCH bpf v4 1/2] bpf: bound arena allocations to the user VMA X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261122-b4-03-bpf-arena-vmalloc-oob-v4-8090cdc58ff8-v4-1-d542d1233623@pm.me> References: <20261122-b4-03-bpf-arena-vmalloc-oob-v4-8090cdc58ff8-v4-0-d542d1233623@pm.me> In-Reply-To: <20261122-b4-03-bpf-arena-vmalloc-oob-v4-8090cdc58ff8-v4-0-d542d1233623@pm.me> To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi Cc: Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795350970; l=3854; i=fadouse@pm.me; s=b4-relay-20261122; h=from:subject:message-id; bh=NydRQKFXTXjxJi7zfPj1uOAvJMlBbK7Axhfoiyd0aNI=; b=bZgyB0O70KqrBqCTyh4nuOAUMlqdRdmQqJMTbqIhaKz7NM3HzkWae3OT3hSYll3PKjH9dHnFi A6opsC1BliVBVdg4BwChZYsjBbbTQVwgY/VHtG4mf02mErpSPOyoM3o X-Developer-Key: i=fadouse@pm.me; a=ed25519; pk=b4DyzHJDRd8853IHUSKPA5wwzChj8eF4MTnZuyPz1PQ= X-Endpoint-Received: by B4 Relay for fadouse@pm.me/b4-relay-20261122 with auth_id=1597 X-Original-From: YanXin Li Reply-To: fadouse@pm.me Content-Transfer-Encoding: quoted-printable From: YanXin Li An arena map initializes its free range tree to max_entries pages. When map_extra is zero, the first mmap() may cover fewer pages. Fixed-address allocations are bounded by the VMA size, but automatic allocations use range_tree_find() across the whole tree and can select a range beyond the VMA. arena_free_pages() clips frees to the VMA, so these pages cannot be released through the arena kfunc. With 4 KiB pages, set max_entries to 1 << 20 and mmap() a 288-page VMA at 0xffee0000, ending at the next 4 GiB boundary. A 287-page automatic allocation takes offsets [0, 287). A subsequent 288-page allocation starts at offset 287, so its kernel mapping starts at kern_vm_start + 0xfffff000. One page lies within the 4 GiB data band, eight lie in the arena's upper half-guard, and 279 lie beyond its requested vmalloc area. The first of those 279 is the vmalloc guard page. The remaining 278 lie beyond the full reservation. The current apply_to_page_range() path can install PTEs there. An unpatched local test returned a non-NULL pointer for this allocation. Another run triggered a WARNING in apply_range_set_cb() on a non-scratch PTE. Out-of-VMA offsets need not wrap the low 32 bits. For example, when the user VMA starts at a 4 GiB aligned address, they can still map inside the arena's kernel area. They remain invalid because the VMA does not cover them. The range-tree conversion replaced the bounded mtree_alloc_range() search with range_tree_find() over max_entries. The later switch from vm_area_map_pages() to apply_to_page_range() removed the vm_struct bounds check, allowing these selections to reach beyond the arena's vmalloc reservation. On the first mmap() without map_extra, remove [vma_pages, max_entries) from the free range tree before publishing the VMA bounds. The VMA size is fixed after this mmap(), and the fault, reserve and free paths do not add offsets outside it. Automatic allocations consequently stay inside the VMA. This issue was found through LLM-assisted code audit and manual code inspection of kernel/bpf/arena.c, then checked with a Z3 SMT model and a custom BPF test in an x86-64 QEMU/KVM guest. The test kernel identified itself as 7.4.0-rc3-84074-gc44104f3642d. KASAN, UBSAN, lockdep and DEBUG_OBJECTS were enabled. After the patch, the 288-page request returns NULL, the 287-page in-VMA request succeeds, and the test reports no kernel warnings. Fixes: b795379757eb ("bpf: Introduce range_tree data structure and use it i= n bpf arena") Cc: stable@vger.kernel.org Signed-off-by: YanXin Li --- kernel/bpf/arena.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index ec2bda1a6eb..94321dec638 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -694,6 +694,31 @@ static int arena_map_mmap(struct bpf_map *map, struct = vm_area_struct *vma) if (remember_vma(arena, vma)) return -ENOMEM; =20 + if (!arena->user_vm_start && vma_pages(vma) < map->max_entries) { + /* + * Without map_extra, the first mmap() can be shorter than + * max_entries. Remove offsets outside the VMA from the free + * range tree so automatic allocations stay within the VMA. + */ + unsigned long flags; + int ret =3D -EBUSY; + + if (!raw_res_spin_lock_irqsave(&arena->spinlock, flags)) { + ret =3D range_tree_clear(&arena->rt, vma_pages(vma), + map->max_entries - vma_pages(vma)); + raw_res_spin_unlock_irqrestore(&arena->spinlock, flags); + WARN_ON_ONCE(ret); + } + if (ret) { + struct vma_list *vml =3D vma->vm_private_data; + + list_del(&vml->head); + vma->vm_private_data =3D NULL; + kfree(vml); + return ret; + } + } + arena->user_vm_start =3D vma->vm_start; arena->user_vm_end =3D vma->vm_end; /* --=20 Git-157) From nobody Mon Nov 23 12:44:19 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795350982; b=csPT4eHNeK5l9yAim4XMpGLmT5xA8p6erTLbS7UMY7gSnzsWdE/LRDRR4xKn1EbLIJEG MnpsSgcPLsW0eDBMHAxUShhZROmcadSoOKMlIP76CbexteGl3CulUoYn31ggIN27AnL0f LwIIDEkZI3t5L3c0/ZENERdH+dDYx5NatuzvADXgKfwgyffsD4/fyVrwGnWvuiXQoEDer PelVC/CyuUKXN8s3bA3VLDX21YGXVGFyRLnVsdcIhmyLPs6NZE+xnqKbWc02djdrNrFDV PfaYhnb78IE5UgS/SgZ7fsAzh5fPGmdCOYT564s7RI25jX3uLn9n7mQIfM9JWTKivpA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795350982; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=FfDO0f0KQSC409TsDN1PUSGBpdCVQz7p/HXZoa5TnKk=; b=wzZyCpcWdATPlQOY9zR2+HQj1bC8oxq3uXN/UNNresF+TjtzcUN4DPaku4BRyfSCIBXH hOULy7hxzV8McHljaXecmp7gQKndB1fO9eVUcCR7ixBT6fVR7NF021dWycbniSAjGpLuu n/C7tTe4vXHNfHRE6+tFeROuk6Jrtydt7y635dezp9gtBent3eguMGsj2ILotsj4M0e9+ s3k5lIWgSTyihS+lAlCf1rUq1QMT8Gz3ZGqr8TVMzrWI6JwHGfKQxL/MCO7MQ3cXTkhzQ d2S5Ye/IRGmQHt/kb1IVNtXAL3BOwjKeHaWMNuTNJjnQxhh01vvZxlMDjN5651KPSxw== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=mWiCJGp4; arc=pass header.oldest-pass=0 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 CF7DC606A331; Sun, 22 Nov 2026 12:36:22 +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=mWiCJGp4 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795350982; b=UdInRKuZy4DUluSrE1AtQVBuqfMH4qjUTQzmWegj4PbnHDhI4z9UEhZPpssWYeVNAwQy pCh8XAHEUfkmuOIGIbl30YFI01KplJJiIgUW2wy8pehYpOujgngLdxpmfLeJ1xgfNGpxW eezjPgUMyYzYaWnGso5it/eBcyt29MwYB7er1a2xCzUI9HpgC9hWNO9RsRrUJJe4zEVUo n6ypqio+aSe3VKtgNkSA1iBBoTP7oaHudEAiLBzz4AgLM9jkIy2f7UFraOdQzgzbCdxGN hd/2BAMrkUNAVn9ZMbiBla/hO6teIHUDes1IpLAguuPWNfosOCHFqZZ1cIQzEVCpIjw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795350982; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=FfDO0f0KQSC409TsDN1PUSGBpdCVQz7p/HXZoa5TnKk=; b=nEe2aqcTAVjJr4Flam5TLFjezct/UB5n439LyED6g8ZxrD5cVNN+iA9JJeUtc+BqXwzq /BXtqOhydCUWxrnQf6g6gsyHnr73LrC0rTAyAakyngV4BWwHo7Wzi6JUnMQeG+54wwSuN 3XItPhGbIPHquAXjod6abqW2JTJj3p/5iudk7gWI+MPJxXkjx3l3ng5OKg2dvPvMIwJf2 FQwfxhg7NSlOb5zYeCGbfK6Uqx1Ox/DNwvNcFGNlmaw/qLHh99wck6cPQh1tOed4BTLT6 N2FQnuvg4RsreX33XYwtkbuz+zl0v8ZzYHgnDqi+lKwOF8vXAIIRvOfV96dFJp/4sdQ== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 9F1191F000CA; Sun, 22 Nov 2026 12:36:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795350982; bh=FfDO0f0KQSC409TsDN1PUSGBpdCVQz7p/HXZoa5TnKk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=mWiCJGp4ZRzmH3YDFJyOy/blOIV3y9S6ztuAoHfIzN1fDeoJdJWghGAmydNeHFPch 6FLI9JwM5J6NuLMjV1efvPt9AOsgU6044R49mPkpj8r4D68STX6x/6Ym/ZSGjGy60o v5Mp3OYVM9Bd7g/bOwjjaPE8fLHW3t0nECQYomYmHaaMWS9zW4OnYvsVEHXWRaCrk9 880F1fa3U5vsKO7O7nxVjZt+PESKC8XM6mEijnm1oklDfYRXar4cNT9uPlcDNZ8C6z eW+N2ghEC4YaZjQx2VWzkWxjf4i2kTRr5nwhzl44hgpoON++YGDEalRfdAUELjRsvi Dclffx3P7cyFQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 27E474C039; Sun, 22 Nov 2026 12:36:12 +0000 (UTC) From: YanXin Li via B4 Relay Date: Sun, 22 Nov 2026 20:35:37 +0800 Subject: [PATCH bpf v4 2/2] selftests/bpf: test arena allocation after a short mmap X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261122-b4-03-bpf-arena-vmalloc-oob-v4-8090cdc58ff8-v4-2-d542d1233623@pm.me> References: <20261122-b4-03-bpf-arena-vmalloc-oob-v4-8090cdc58ff8-v4-0-d542d1233623@pm.me> In-Reply-To: <20261122-b4-03-bpf-arena-vmalloc-oob-v4-8090cdc58ff8-v4-0-d542d1233623@pm.me> To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi Cc: Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , bpf@vger.kernel.org, linux-kernel@vger.kernel.org X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795350970; l=4531; i=fadouse@pm.me; s=b4-relay-20261122; h=from:subject:message-id; bh=12pf/DDajqKWIUwZFygjYQjcysYQ35Yyf+7SvH1neXs=; b=fVfGRrZf0kiKVP2LKGaSIX/uR/eyOguX6/Qmruahgzv2FWL+SHwDe4d7mI/D2Pf/18F5CgN/J G8bWfg5VzBsDTJzawjXmrA6TQfeTyYdeFbs5rhQYyFXRAxCyCH7uFj4 X-Developer-Key: i=fadouse@pm.me; a=ed25519; pk=b4DyzHJDRd8853IHUSKPA5wwzChj8eF4MTnZuyPz1PQ= X-Endpoint-Received: by B4 Relay for fadouse@pm.me/b4-relay-20261122 with auth_id=1597 X-Original-From: YanXin Li Reply-To: fadouse@pm.me Content-Transfer-Encoding: quoted-printable From: YanXin Li Without map_extra, the first mmap of an arena can be shorter than the map's max_entries. Automatic allocations must stay within that VMA. Create an eight-page arena and mmap only four pages before loading a BPF program. Reuse the map FD so libbpf does not mmap the full arena. The program occupies offset zero and allocates two more pages, leaving one page within the VMA. A two-page request must fail, and a final one-page request must succeed. Before the preceding fix, the two-page request succeeds outside the VMA. When JIT is disabled, arena map creation can succeed while loading a program that uses the map returns -EOPNOTSUPP. Skip the subtest in that case. Signed-off-by: YanXin Li --- .../testing/selftests/bpf/prog_tests/arena_mmap.c | 53 ++++++++++++++++++= ++++ .../testing/selftests/bpf/progs/arena_mmap_alloc.c | 33 ++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/arena_mmap.c b/tools/te= sting/selftests/bpf/prog_tests/arena_mmap.c index f55d950720f..ff1fe0a6f22 100644 --- a/tools/testing/selftests/bpf/prog_tests/arena_mmap.c +++ b/tools/testing/selftests/bpf/prog_tests/arena_mmap.c @@ -2,6 +2,7 @@ =20 #include #include +#include "arena_mmap_alloc.skel.h" =20 #define ARENA_PAGES 8 =20 @@ -34,6 +35,56 @@ static void test_refused(__u64 map_extra, __u64 addr, in= t pages, int flags, int close(fd); } =20 +static void test_short_mmap_alloc(void) +{ + LIBBPF_OPTS(bpf_map_create_opts, map_opts, .map_flags =3D BPF_F_MMAPABLE); + LIBBPF_OPTS(bpf_test_run_opts, run_opts); + struct arena_mmap_alloc *skel =3D NULL; + size_t size =3D 4 * getpagesize(); + void *area; + int fd, err; + + fd =3D bpf_map_create(BPF_MAP_TYPE_ARENA, "arena", 0, 0, + ARENA_PAGES, &map_opts); + if (fd =3D=3D -EOPNOTSUPP) { + test__skip(); + return; + } + if (!ASSERT_GE(fd, 0, "bpf_map_create")) + return; + + /* The short mapping must be the first mmap() of this arena. */ + area =3D mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (!ASSERT_NEQ(area, MAP_FAILED, "short_mmap")) + goto out_close; + + /* Reuse the map so libbpf does not mmap all ARENA_PAGES on load. */ + skel =3D arena_mmap_alloc__open(); + if (!ASSERT_OK_PTR(skel, "arena_mmap_alloc__open")) + goto out_unmap; + err =3D bpf_map__reuse_fd(skel->maps.arena, fd); + if (!ASSERT_OK(err, "reuse_arena_fd")) + goto out_destroy; + err =3D arena_mmap_alloc__load(skel); + if (err =3D=3D -EOPNOTSUPP) { + test__skip(); + goto out_destroy; + } + if (!ASSERT_OK(err, "arena_mmap_alloc__load")) + goto out_destroy; + err =3D bpf_prog_test_run_opts(bpf_program__fd(skel->progs.alloc_beyond_v= ma), + &run_opts); + if (ASSERT_OK(err, "alloc_beyond_vma")) + ASSERT_OK(run_opts.retval, "alloc_beyond_vma_retval"); + +out_destroy: + arena_mmap_alloc__destroy(skel); +out_unmap: + munmap(area, size); +out_close: + close(fd); +} + void serial_test_arena_mmap(void) { __u64 page =3D getpagesize(), last =3D -(ARENA_PAGES * page); @@ -50,4 +101,6 @@ void serial_test_arena_mmap(void) test_refused(0, -page, ARENA_PAGES, MAP_FIXED, 0); if (test__start_subtest("map_extra_last_pages")) test_refused(last, last, ARENA_PAGES, MAP_FIXED, 0); + if (test__start_subtest("short_mmap_alloc")) + test_short_mmap_alloc(); } diff --git a/tools/testing/selftests/bpf/progs/arena_mmap_alloc.c b/tools/t= esting/selftests/bpf/progs/arena_mmap_alloc.c new file mode 100644 index 00000000000..9c46a393eae --- /dev/null +++ b/tools/testing/selftests/bpf/progs/arena_mmap_alloc.c @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define BPF_NO_KFUNC_PROTOTYPES +#include +#include +#include "bpf_arena_common.h" + +struct { + __uint(type, BPF_MAP_TYPE_ARENA); + __uint(map_flags, BPF_F_MMAPABLE); + __uint(max_entries, 8); +} arena SEC(".maps"); + +SEC("syscall") +int alloc_beyond_vma(void *ctx) +{ + void __arena *page; + + page =3D bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0); + if (!page) + return 4; + page =3D bpf_arena_alloc_pages(&arena, NULL, 2, NUMA_NO_NODE, 0); + if (!page) + return 1; + /* Only one of the four mmap()ed pages remains available. */ + page =3D bpf_arena_alloc_pages(&arena, NULL, 2, NUMA_NO_NODE, 0); + if (page) + return 2; + page =3D bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0); + return page ? 0 : 3; +} + +char _license[] SEC("license") =3D "GPL"; --=20 Git-157)