[PATCH] lib/idr: fix infinite loop in idr_get_next()

From: Josh Law

Date: Thu Mar 12 2026 - 13:05:12 EST


From: Josh Law <hlcj1234567@xxxxxxxxx>

From: Josh Law <objecting@xxxxxxxxxxxxx>

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.

Signed-off-by: Josh Law <objecting@xxxxxxxxxxxxx>
---
lib/idr.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/idr.c b/lib/idr.c
index f25bd2b9e9a4..07098eb4ddc3 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -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;
}
--
2.34.1