From: Josh Law <hlcj1234567@gmail.com>
To: Matthew Wilcox <willy@infradead.org>,
Andrew Morton <akpm@linux-foundation.org>
Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, Josh Law <objecting@objecting.org>
Subject: [PATCH v3 2/2] lib/idr: fix memory leak in ida_alloc_range() error path
Date: Thu, 12 Mar 2026 18:19:48 +0000 [thread overview]
Message-ID: <20260312181948.20020-3-objecting@objecting.org> (raw)
In-Reply-To: <20260312181948.20020-1-objecting@objecting.org>
In ida_alloc_range(), if the XArray operation encounters an error
(e.g., -ENOSPC) during allocation, the function exits early via
return xas_error(&xas). However, if an intermediate `alloc` bitmap
was allocated via kzalloc() earlier in the function but the XArray
insertion failed, the error path returns without freeing `alloc`.
Reorder the error handling to ensure `alloc` is properly freed when
an XArray error occurs.
Also add a test case in idr-test to ensure coverage of the error
path in the IDA allocation logic.
Signed-off-by: Josh Law <objecting@objecting.org>
---
tools/testing/radix-tree/idr-test.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/tools/testing/radix-tree/idr-test.c b/tools/testing/radix-tree/idr-test.c
index bf6a0da6a50a..f4c3a5ed4ce1 100644
--- a/tools/testing/radix-tree/idr-test.c
+++ b/tools/testing/radix-tree/idr-test.c
@@ -543,6 +543,7 @@ void user_ida_checks(void)
ida_check_nomem();
ida_check_conv_user();
ida_check_random();
+ ida_check_leak();
ida_alloc_free_test();
radix_tree_cpu_dead(1);
@@ -556,6 +557,22 @@ static void *ida_random_fn(void *arg)
return NULL;
}
+/*
+ * Check that an XArray error does not leak the allocated bitmap.
+ */
+static void ida_check_leak(void)
+{
+ DEFINE_IDA(ida);
+
+ /* Allocate up to 128 to ensure we need a new bitmap */
+ ida_alloc_range(&ida, 0, 128, GFP_KERNEL);
+
+ /* Force a failure by providing an invalid range */
+ ida_alloc_range(&ida, 0, 0, GFP_KERNEL);
+
+ ida_destroy(&ida);
+}
+
static void *ida_leak_fn(void *arg)
{
struct ida *ida = arg;
--
2.34.1
prev parent reply other threads:[~2026-03-12 18:19 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-12 18:19 [PATCH v3 0/2] lib/idr: Fixes for infinite loop and memory leak Josh Law
2026-03-12 18:19 ` [PATCH v3 1/2] lib/idr: fix infinite loop in idr_get_next() Josh Law
2026-03-12 20:55 ` Andrew Morton
2026-03-12 20:57 ` Josh Law
2026-03-12 21:15 ` Josh Law
2026-03-12 18:19 ` Josh Law [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=20260312181948.20020-3-objecting@objecting.org \
--to=hlcj1234567@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=objecting@objecting.org \
--cc=willy@infradead.org \
/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