From nobody Tue Sep 29 08:26:18 2026 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D679A3E2769; Mon, 10 Aug 2026 13:49:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786369766; cv=none; b=j5eronq76BK0QTWQVTjO/NBmfF5u6+VX6IHIv9Q4NkSNokasInwWv7VZ2oHGMZn/sFZEqAQ0v8b77hJl9YCAmLRNxgCPRypt2Xo2V5KegDWlijB0Rl08MIEop7xLxK6rx6+GtXP/EYP16MC8jlqGirGIS/j2DHj7LpqVj0yc/wE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786369766; c=relaxed/simple; bh=NiyyWUH1prkVse1OkE9B2qMJ6JZL7ZE6Xekm99UEJDo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Br0Fim2KSiIrrEoJ92n7eGawSQGan1oe52tEhXm3VOrNCXrZ1gQMadfjFk4tpuvhtBIw2gmyAF0rY3dUpTcxFL/t5wW8syqvP0S1qt03CHdtPwUP/JfpR+gjMdgvM8CCSRAMUy+jLpqh2763yAPZDfSfYXXKmp9DT0GYNUC5GxQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=Pkpwnf24; arc=none smtp.client-ip=220.197.31.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="Pkpwnf24" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=+J HuhUJjxcmnFhFTzIqRT3sPERPM9fOZMeWKzbtsUd0=; b=Pkpwnf24l5G1mcfd26 r9exk9hJfBFhveYYt5PmUlA2K6yNwt+DTBLSgr6Hr7pG+aY/2lA87BeBduKRY3qW CrR8t4HOGL8B8jw+OzpZK46smIsfKuU8/xYBRMhkPoupSPX9Yoahb35tgykmOhVw PwPWfIMRktXzv4ynTxPCR2P1k= Received: from nec8-i7 (unknown []) by gzga-smtp-mtada-g1-4 (Coremail) with SMTP id _____wCnJImg1nlqBKwLPA--.27752S3; Mon, 10 Aug 2026 21:48:20 +0800 (CST) From: chenyuan_fl@163.com To: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Yuan Chen Subject: [PATCH bpf-next v3 1/3] bpf, arena: fix range_tree_clear inconsistency on kmalloc_nolock failure Date: Mon, 10 Aug 2026 21:47:58 +0800 Message-ID: <20260810134800.2875487-2-chenyuan_fl@163.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260810134800.2875487-1-chenyuan_fl@163.com> References: <20260810134800.2875487-1-chenyuan_fl@163.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _____wCnJImg1nlqBKwLPA--.27752S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7Cw48Xw45trWrXFyUuFW8tFb_yoW8GF1fpw 4UK34fAws0q3yUWrZa9F4vkr98Can3Xr48K343tw4kAr15A3Z7urnY9r4293ZrAFZFy3Wr tF1jva9rK3WUuFDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UZeOXUUUUU= X-CM-SenderInfo: xfkh05pxdqswro6rljoofrz/xtbDAgXXlWp51qVw-wAA3q Content-Type: text/plain; charset="utf-8" From: Yuan Chen range_tree_clear() pre-allocates the right-half node before modifying the tree, so an allocation failure returns -ENOMEM without altering the range tree. Signed-off-by: Yuan Chen Reviewed-by: Emil Tsalapatis --- kernel/bpf/range_tree.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/kernel/bpf/range_tree.c b/kernel/bpf/range_tree.c index 2f28886f3ff7..15b588377a76 100644 --- a/kernel/bpf/range_tree.c +++ b/kernel/bpf/range_tree.c @@ -143,16 +143,22 @@ int range_tree_clear(struct range_tree *rt, u32 start= , u32 len) if (rn->rn_start < start && rn->rn_last > last) { u32 old_last =3D rn->rn_last; =20 + /* + * Pre-allocate the right-half node before modifying + * the tree. If allocation fails we return -ENOMEM + * without altering the range tree. + */ + new_rn =3D kmalloc_nolock(sizeof(struct range_node), + __GFP_ACCOUNT, NUMA_NO_NODE); + if (!new_rn) + return -ENOMEM; + /* Overlaps with the entire clearing range */ range_it_remove(rn, rt); rn->rn_last =3D start - 1; range_it_insert(rn, rt); =20 - /* Add a range */ - new_rn =3D kmalloc_nolock(sizeof(struct range_node), __GFP_ACCOUNT, - NUMA_NO_NODE); - if (!new_rn) - return -ENOMEM; + /* Add right-half range */ new_rn->rn_start =3D last + 1; new_rn->rn_last =3D old_last; range_it_insert(new_rn, rt); --=20 2.54.0 From nobody Tue Sep 29 08:26:18 2026 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30EC03AF66C; Mon, 10 Aug 2026 13:49:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786369760; cv=none; b=FWJ/tdOdvFqdkQ0fNIpWllYAgNOQHk4XcuNYSL5ZX1/9bR1JboOteZn5XJwvVYlDDxGKBX7vj7fe6jnnq+LEoKhlJMbACg90hxstaHqu1VBu/lNZDq7ofX7wIxWxv9D7vijSMV8dhqhVlgLNnqxSu7oVyCktV+XVky+AfH+Kw2U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786369760; c=relaxed/simple; bh=s0eDlEyhWhKwZINONwJAuf9I/2mSlEEBqqnzWw018TU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iilpDQGIfZKb57n6OS7IeQP9jY4vHFcs13TIo4T7GRGnvZ8KsRnPWAQYgHrz1GydCNaFlvPxtPKKuyqEOKO8m73gooeBo86HNpK3CI4WIpYT+OuSn9GmIIHl2YbwqqiGvdhGzIGDZYaaFoBZPe5WHwM0dCXkUyZSt3ikDr5cX9M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=cB12lP72; arc=none smtp.client-ip=117.135.210.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="cB12lP72" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=tL TL2ZpglUla0GwJf+IdatjY2pI7kZBakztj9ClJ+Y0=; b=cB12lP7289in/rzHBk nQC11ueGSrdXZ7n7lmI7OsfH3bODfr2Tz/V2uPUGpl5mssq10mL7J3NU5C2nSD0V YYHT1fImZypR07LmtWUhsvjJ7fanYpejFNZmqgcn3ftouMWlpHeYu9NMJnSi6waD uFIneZxMBhHs4QcL7CYi2JVmc= Received: from nec8-i7 (unknown []) by gzga-smtp-mtada-g1-4 (Coremail) with SMTP id _____wCnJImg1nlqBKwLPA--.27752S4; Mon, 10 Aug 2026 21:48:22 +0800 (CST) From: chenyuan_fl@163.com To: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Yuan Chen Subject: [PATCH bpf-next v3 2/3] bpf, arena: fix range_tree_set inconsistency on kmalloc_nolock failure Date: Mon, 10 Aug 2026 21:47:59 +0800 Message-ID: <20260810134800.2875487-3-chenyuan_fl@163.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260810134800.2875487-1-chenyuan_fl@163.com> References: <20260810134800.2875487-1-chenyuan_fl@163.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _____wCnJImg1nlqBKwLPA--.27752S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxCw13AF1kWr48GF4xAFy8Grg_yoW5CF4Upw 45KrWrJrsxJ3yxWr1Svr4ruryrCw1fXws5trZrGw4kA3ZxAr97Arn0kF4jkFWDAFykCr15 tF1jva1UKF4UuFDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jbIDcUUUUU= X-CM-SenderInfo: xfkh05pxdqswro6rljoofrz/xtbDAgbXlWp51qZxFQAA3B Content-Type: text/plain; charset="utf-8" From: Yuan Chen range_tree_set() pre-allocates the node needed for a brand-new range before calling range_tree_clear(), so an allocation failure returns -ENOMEM without having modified the tree (previously the overlapping nodes were already removed by range_tree_clear() before the allocation was attempted, permanently losing the cleared sub-ranges). Signed-off-by: Yuan Chen Reviewed-by: Emil Tsalapatis --- kernel/bpf/range_tree.c | 45 +++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/kernel/bpf/range_tree.c b/kernel/bpf/range_tree.c index 15b588377a76..54055b1fe541 100644 --- a/kernel/bpf/range_tree.c +++ b/kernel/bpf/range_tree.c @@ -199,6 +199,7 @@ int is_range_tree_set(struct range_tree *rt, u32 start,= u32 len) int range_tree_set(struct range_tree *rt, u32 start, u32 len) { u32 last =3D start + len - 1; + struct range_node *new_rn =3D NULL; struct range_node *right; struct range_node *left; int err; @@ -208,20 +209,40 @@ int range_tree_set(struct range_tree *rt, u32 start, = u32 len) if (left && left->rn_start <=3D start && left->rn_last >=3D last) return 0; =20 + /* + * A new node is needed only when the range has no adjacent free + * range on either side. This is known before clearing: any range + * covering start - 1 or last + 1 survives the clear as an adjacent + * piece. Allocate only in that case, before modifying the tree, so + * a failure leaves the range tree unmodified + */ + left =3D range_it_iter_first(rt, start - 1, start - 1); + right =3D range_it_iter_first(rt, last + 1, last + 1); + if (!left && !right) { + new_rn =3D kmalloc_nolock(sizeof(struct range_node), + __GFP_ACCOUNT, NUMA_NO_NODE); + if (!new_rn) + return -ENOMEM; + } + /* Clear out everything in the range we want to set. */ err =3D range_tree_clear(rt, start, len); if (err) - return err; + goto out_free_new; =20 /* Do we have a left-adjacent range ? */ left =3D range_it_iter_first(rt, start - 1, start - 1); - if (left && left->rn_last + 1 !=3D start) - return -EFAULT; + if (left && left->rn_last + 1 !=3D start) { + err =3D -EFAULT; + goto out_free_new; + } =20 /* Do we have a right-adjacent range ? */ right =3D range_it_iter_first(rt, last + 1, last + 1); - if (right && right->rn_start !=3D last + 1) - return -EFAULT; + if (right && right->rn_start !=3D last + 1) { + err =3D -EFAULT; + goto out_free_new; + } =20 if (left && right) { /* Combine left and right adjacent ranges */ @@ -241,14 +262,16 @@ int range_tree_set(struct range_tree *rt, u32 start, = u32 len) right->rn_start =3D start; range_it_insert(right, rt); } else { - left =3D kmalloc_nolock(sizeof(struct range_node), __GFP_ACCOUNT, NUMA_N= O_NODE); - if (!left) - return -ENOMEM; - left->rn_start =3D start; - left->rn_last =3D last; - range_it_insert(left, rt); + /* No adjacent ranges; use the pre-allocated node */ + new_rn->rn_start =3D start; + new_rn->rn_last =3D last; + range_it_insert(new_rn, rt); } return 0; + +out_free_new: + kfree_nolock(new_rn); + return err; } =20 void range_tree_destroy(struct range_tree *rt) --=20 2.54.0 From nobody Tue Sep 29 08:26:18 2026 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 074583E168A; Mon, 10 Aug 2026 13:49:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786369764; cv=none; b=JY4VpxOUXkS+swwsHWKM5nWm/D92+5kGOeQ3K71n7GHoaEUJLlz2O0hqozp3O249lCXAbp14amarAp+INHQSnbinUWNtUnKpX9Ts/ZpZ92n7b2bPJ5qEbimeg4qh2/U/KhONuMGNwgN8VxuEcVOEXtQwpronryf7VkNt4MwnVz4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786369764; c=relaxed/simple; bh=Ow+GUhBa0rYrOzu+TOd8Giv7ZdKkyrHqBi7vdrmrpak=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=q6078q5rQEaNFxgV8DEFMbnZ+eIglmhd7OPqUyqLSdqdxOFXG0RolxgNV0D3Hrx/9PKDsLKr7vhUsqeGbl2D3spOpMNG8Kt1dZ/gIIa6nvPZ+zuIRpDbfiwZATU+6GJbUm6zbNDYT3aDNWTlYBhApfUIQsnDHDHSfEnJZx3JAYc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=kpj/rklD; arc=none smtp.client-ip=117.135.210.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="kpj/rklD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=9Y Og8U53lS/fw3psYdBRrJ5Kb6g8ticHNZVmkoqWgo0=; b=kpj/rklDl1H/PMA4sj bR77oTDdkjXdyTmVX7KFZ1b4LVu294Q7MJKVlAxQFGITlphlop29vw3WILDoxqpe X/00dISTuy82bpDlCbvm8zYGI8pMfgFkeNZL/Lz2mpQZrPCB7BuJ3orhvkMTimXZ qEij/6yoMN7E3R3Bj/cslubqA= Received: from nec8-i7 (unknown []) by gzga-smtp-mtada-g1-4 (Coremail) with SMTP id _____wCnJImg1nlqBKwLPA--.27752S5; Mon, 10 Aug 2026 21:48:23 +0800 (CST) From: chenyuan_fl@163.com To: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Yuan Chen Subject: [PATCH bpf-next v3 3/3] bpf, arena: check range_tree_set return in arena_free_pages and arena_free_worker Date: Mon, 10 Aug 2026 21:48:00 +0800 Message-ID: <20260810134800.2875487-4-chenyuan_fl@163.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260810134800.2875487-1-chenyuan_fl@163.com> References: <20260810134800.2875487-1-chenyuan_fl@163.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _____wCnJImg1nlqBKwLPA--.27752S5 X-Coremail-Antispam: 1Uf129KBjvJXoWxCrWrCFy7CF17Gr47Xw1rCrg_yoW5Xw1fpF 4UJa9Yyrs5Jw4xAr43GrWv9rn8G39Yqa18G34UK3WrZ34av3srtF48AF1UuFyUGFW8Xr4U KF4UXw1YkrWDZFDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07j9pnPUUUUU= X-CM-SenderInfo: xfkh05pxdqswro6rljoofrz/xtbC5gjYlmp51qjLVgAA3Q Content-Type: text/plain; charset="utf-8" From: Yuan Chen arena_free_pages() and arena_free_worker() now handle range_tree_set() errors. arena_free_pages() aborts the free on error, and arena_free_worker() moves range_tree_set() before PTE clearing so that a failed tree update leaves the PTEs intact instead of freeing pages that the arena free tree does not track. Also check the range_tree_set() return value in arena_alloc_pages()'s error path, which restores the unpopulated tail of a partially allocated range; log a warning instead of silently leaking the virtual range when the tree update fails. Signed-off-by: Yuan Chen Reviewed-by: Emil Tsalapatis --- kernel/bpf/arena.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index 555ee2531ef9..d57a0b617525 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -766,7 +766,9 @@ static long arena_alloc_pages(struct bpf_arena *arena, = long uaddr, long page_cnt bpf_map_memcg_exit(old_memcg, new_memcg); return clear_lo32(arena->user_vm_start) + uaddr32; out: - range_tree_set(&arena->rt, pgoff + mapped, page_cnt - mapped); + if (range_tree_set(&arena->rt, pgoff + mapped, page_cnt - mapped)) + pr_warn_ratelimited("bpf_arena: failed to restore free range %ld+%ld aft= er partial alloc\n", + pgoff + mapped, page_cnt - mapped); raw_res_spin_unlock_irqrestore(&arena->spinlock, flags); if (mapped) { flush_vmap_cache(kern_vm_start + uaddr32, mapped << PAGE_SHIFT); @@ -881,7 +883,12 @@ static void arena_free_pages(struct bpf_arena *arena, = long uaddr, long page_cnt, if (ret) goto defer; =20 - range_tree_set(&arena->rt, pgoff, page_cnt); + ret =3D range_tree_set(&arena->rt, pgoff, page_cnt); + if (ret) { + raw_res_spin_unlock_irqrestore(&arena->spinlock, flags); + bpf_map_memcg_exit(old_memcg, new_memcg); + return; + } =20 init_llist_head(&free_pages); cdata.arena =3D arena; @@ -1004,11 +1011,18 @@ static void arena_free_worker(struct work_struct *w= ork) kaddr =3D arena_vm_start + s->uaddr; pgoff =3D compute_pgoff(arena, s->uaddr); =20 + /* + * Mark the range as free before clearing PTEs so that + * if the range tree update fails we leave the PTEs + * intact, avoiding an unrecoverable state where pages + * are freed but the arena free tree does not track them. + */ + if (range_tree_set(&arena->rt, pgoff, page_cnt)) + continue; + /* clear ptes and collect pages in free_pages llist */ apply_to_existing_page_range(&init_mm, kaddr, page_cnt << PAGE_SHIFT, apply_range_clear_cb, &cdata); - - range_tree_set(&arena->rt, pgoff, page_cnt); } raw_res_spin_unlock_irqrestore(&arena->spinlock, flags); =20 --=20 2.54.0