Skip to contentSkip to conversation

lib/idr: Fixes for infinite loop and memory leak

PATCH In review Josh Law 2 participants 6 messages Mar 12

Subsystems (4)
lore
Share & tools
lore

Josh Law

author
#0 lore raw

v3 cover lib/idr: Fixes for infinite loop and memory leak

This series includes two fixes for the IDR and IDA APIs, along with their corresponding test cases as requested by reviewers.

1. Fix an infinite loop condition in idr_get_next() that occurs when iterating over an ID > INT_MAX.
2. Fix a memory leak in ida_alloc_range() where an intermediate allocated bitmap is not freed if a subsequent XArray insertion fails.

Both fixes update tools/testing/radix-tree/idr-test.c to ensure the error paths are covered by the test suite.

Changes since v2:
- Added idr-test.c updates to both commits to formally test the fix conditions.

Josh Law (2):
lib/idr: fix infinite loop in idr_get_next() lib/idr: fix memory leak in ida_alloc_range() error path

 lib/idr.c                           |  4 +++-
 tools/testing/radix-tree/idr-test.c | 19 ++++++++++++++++++-
 2 files changed, 21 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/2 lib/idr: fix infinite loop in idr_get_next()

In idr_get_next(), if the returned id from idr_get_next_ul() is greater than INT_MAX, the function issues a warning and returns NULL without updating the *nextid pointer. This causes a soft lockup for any caller iterating over an IDR (e.g. via idr_for_each_entry) because they will receive NULL, fail to advance their index, and repeatedly query the same state forever.

Fix this by setting *nextid to INT_MAX when the bounds check fails, ensuring the caller's iteration will terminate.

Also update the idr_get_next() test case in the radix-tree test suite to expect INT_MAX instead of 0 when hitting this condition.

---
 lib/idr.c                           | 4 +++-
 tools/testing/radix-tree/idr-test.c | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)
lib/idr.c +3 -1
index f25bd2b9e9a4..07098eb4ddc3 100644
@@ -268,8 +268,10 @@ void *idr_get_next(struct idr *idr, int *nextid)
 	unsigned long id = *nextid;
 	void *entry = idr_get_next_ul(idr, &id);
 
-	if (WARN_ON_ONCE(id > INT_MAX))
+	if (WARN_ON_ONCE(id > INT_MAX)) {
+		*nextid = INT_MAX;
 		return NULL;
+	}
 	*nextid = id;
 	return entry;
 }
tools/testing/radix-tree/idr-test.c +1 -1
index 945144e98507..bf6a0da6a50a 100644
@@ -213,7 +213,7 @@ void idr_u32_test1(struct idr *idr, u32 handle)
 	ptr = idr_get_next(idr, &sid);
 	if (id > INT_MAX) {
 		BUG_ON(ptr != NULL);
-		BUG_ON(sid != 0);
+		BUG_ON(sid != INT_MAX);
 	} else {
 		BUG_ON(ptr != DUMMY_PTR);
 		BUG_ON(sid != id);
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v3 2/2 lib/idr: fix memory leak in ida_alloc_range() error path

In reply to Josh Law

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.

---
 tools/testing/radix-tree/idr-test.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
tools/testing/radix-tree/idr-test.c +17 -0
index bf6a0da6a50a..f4c3a5ed4ce1 100644
@@ -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;
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v3 1/2] lib/idr: fix infinite loop in idr_get_next()

In reply to Josh Law
Quoted 6 lines · On Thu, 12 Mar 2026 18:19:47 +0000 Josh Law <[email protected]> wrote:

In idr_get_next(), if the returned id from idr_get_next_ul() is greater than INT_MAX, the function issues a warning and returns NULL without updating the *nextid pointer. This causes a soft lockup for any caller iterating over an IDR (e.g. via idr_for_each_entry) because they will receive NULL, fail to advance their index, and repeatedly query the same state forever.

This assumes that the idr_get_next() caller ignores the NULL return and just keeps on looping. Isn't that a caller bug and if so, do we need to defend against it here?

author
#3 lore raw

Re: [PATCH v3 1/2] lib/idr: fix infinite loop in idr_get_next()

12 Mar 2026 20:55:15 Andrew Morton <[email protected]>:

Quoted 9 lines · On Thu, 12 Mar 2026 18:19:47 +0000 Josh Law <[email protected]> wrote:
Quoted 7 lines

In idr_get_next(), if the returned id from idr_get_next_ul() is greater than INT_MAX, the function issues a warning and returns NULL without updating the *nextid pointer. This causes a soft lockup for any caller iterating over an IDR (e.g. via idr_for_each_entry) because they will receive NULL, fail to advance their index, and repeatedly query the same state forever.

Quoted 3 lines

This assumes that the idr_get_next() caller ignores the NULL return and just keeps on looping.  Isn't that a caller bug and if so, do we need to defend against it here?

The risk isn't just a single loop failure; it's that idr_get_next() breaks the 'forward-progress' guarantee of the iterator.
In macros like idr_for_each_entry_continue, if idr_get_next() returns NULL without advancing the pointer, the caller is left in a permanent trap. Any attempt to resume or retry the iteration results in an infinite loop of the same warning because the index is never incremented past the problematic ID. Advancing the pointer ensures the infrastructure is robust against these 'soft lockups', even if the caller's error handling is imperfect..

author
#4 lore raw

Re: [PATCH v3 1/2] lib/idr: fix infinite loop in idr_get_next()

12 Mar 2026 20:57:48 Josh Law <[email protected]>:

Quoted 11 lines

12 Mar 2026 20:55:15 Andrew Morton <[email protected]>:

Quoted 9 lines · On Thu, 12 Mar 2026 18:19:47 +0000 Josh Law <[email protected]> wrote:
Quoted 7 lines

In idr_get_next(), if the returned id from idr_get_next_ul() is greater than INT_MAX, the function issues a warning and returns NULL without updating the *nextid pointer. This causes a soft lockup for any caller iterating over an IDR (e.g. via idr_for_each_entry) because they will receive NULL, fail to advance their index, and repeatedly query the same state forever.

Quoted 7 lines
Quoted 3 lines

This assumes that the idr_get_next() caller ignores the NULL return and just keeps on looping.  Isn't that a caller bug and if so, do we need to defend against it here?

The risk isn't just a single loop failure; it's that idr_get_next() breaks the 'forward-progress' guarantee of the iterator.
In macros like idr_for_each_entry_continue, if idr_get_next() returns NULL without advancing the pointer, the caller is left in a permanent trap. Any attempt to resume or retry the iteration results in an infinite loop of the same warning because the index is never incremented past the problematic ID. Advancing the pointer ensures the infrastructure is robust against these 'soft lockups', even if the caller's error handling is imperfect..

This most definitely needs to be merged.

V/R