From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-171.mta0.migadu.com (out-171.mta0.migadu.com [91.218.175.171]) (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 CC505EAC7 for ; Wed, 25 Feb 2026 07:17:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772003849; cv=none; b=hiTao2oAcn94B+AOX7YUdirvCUlrigMmBivG3DfaxBU55RsVkJQzNOa4RK3wpCUoTXUxkCU4W14j69kjcmpQNpeZiMzHv9ffahJXB01X76Lvi9DU/VYV4HAJq3rRUY90y5ODucozD7DBqX5Tmn3XjF5+V51jL/pH7Yb4pDY8uLo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772003849; c=relaxed/simple; bh=iCXvxrhlXGZjbLP2SAUlzZzgkqEPl+GVzo8EC5FfGo4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=MobYv5aBblq7hJQHx+/MzPgswZHcVc9qiSpq0Jn0mdNlLBzoytcrOyvpPdR49Rak37e2FJlb8FsphAuBAykSs4lTpRjrgSfLtEd377oVyAJrMo8iCvBhbotIKOMVuzvI4w8f2TfCuyyUyq604tmo0ObsJ7pLhZemdbpR3GkQhZc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=xeZRIzmg; arc=none smtp.client-ip=91.218.175.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="xeZRIzmg" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1772003845; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rG+b8/+can7Q58VDOAAydD277OLHsbwrqz0TrhL3A8A=; b=xeZRIzmgu6uoFXLEXLQ5cUpGo9+vHYhjPw+MJa7/Tvk0waDx83N8W1wSQoOwxWHq9mMjp3 iPO/tJ20ahHiu56yOqX3DcVrHXEVkX2yJNw5N6Be4aYd4BdEvNPgISfP0CE+GrkMiUQLzr gfbHN0cpSv+iH1Hzyi64scd3pwqoIFs= From: Jiayuan Chen To: willy@infradead.org Cc: linux-mm@kvack.org, jiayuan.chen@linux.dev, Jiayuan Chen , syzbot+006987d1be3586e13555@syzkaller.appspotmail.com, Andrew Morton , linux-kernel@vger.kernel.org Subject: [PATCH v1] radix-tree: fix memory leak of intermediate nodes on insert failure Date: Wed, 25 Feb 2026 15:16:18 +0800 Message-ID: <20260225071623.41275-1-jiayuan.chen@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT From: Jiayuan Chen __radix_tree_create() builds a path by allocating and immediately linking intermediate nodes into the tree one by one. If an allocation fails partway through, the already-linked nodes remain in the tree with no corresponding leaf entry. These nodes are never reclaimed because radix_tree_for_each_slot() only visits slots containing leaf values and there is no radix_tree_destroy() to walk all structural nodes. The natural alternative—migrating callers to xarray and relying on xa_destroy()—does not cover global or long-lived trees whose lifetime is tied to the system. xa_destroy() is never called for such trees, so orphaned nodes would persist until reboot. Fix this directly in __radix_tree_create() by tracking the first newly linked slot. On allocation failure, sever the partial path with rcu_assign_pointer() and free the orphaned nodes via call_rcu() to maintain RCU safety for concurrent readers. Reported-by: syzbot+006987d1be3586e13555@syzkaller.appspotmail.com Closes: https://lore.kernel.org/all/000000000000bfba3a060bf4ffcf@google.com/T/ Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Jiayuan Chen --- lib/radix-tree.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/radix-tree.c b/lib/radix-tree.c index 976b9bd02a1b..2bdf2be71b95 100644 --- a/lib/radix-tree.c +++ b/lib/radix-tree.c @@ -600,6 +600,11 @@ static int __radix_tree_create(struct radix_tree_root *root, void __rcu ***slotp) { struct radix_tree_node *node = NULL, *child; + /* Track newly allocated nodes for rollback on failure */ + struct radix_tree_node *new_nodes[RADIX_TREE_MAX_PATH]; + void __rcu **first_new_slot = NULL; + struct radix_tree_node *first_new_parent = NULL; + int new_count = 0, i; void __rcu **slot = (void __rcu **)&root->xa_head; unsigned long maxindex; unsigned int shift, offset = 0; @@ -623,8 +628,23 @@ static int __radix_tree_create(struct radix_tree_root *root, /* Have to add a child node. */ child = radix_tree_node_alloc(gfp, node, root, shift, offset, 0, 0); - if (!child) + if (!child) { + /* Rollback: sever and free all newly allocated nodes */ + if (first_new_slot) { + rcu_assign_pointer(*first_new_slot, NULL); + if (first_new_parent) + first_new_parent->count--; + for (i = 0; i < new_count; i++) + radix_tree_node_free(new_nodes[i]); + } return -ENOMEM; + } + /* Record first new slot and parent for potential rollback */ + if (!first_new_slot) { + first_new_slot = slot; + first_new_parent = node; + } + new_nodes[new_count++] = child; rcu_assign_pointer(*slot, node_to_entry(child)); if (node) node->count++; -- 2.43.0