public inbox for linux-mm@kvack.org
 help / color / mirror / Atom feed
* [PATCH] lib/idr: fix memory leak in ida_alloc_range() error path
@ 2026-03-12 17:18 Josh Law
  0 siblings, 0 replies; only message in thread
From: Josh Law @ 2026-03-12 17:18 UTC (permalink / raw)
  To: Matthew Wilcox, Andrew Morton
  Cc: linux-fsdevel, linux-mm, linux-kernel, 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.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 lib/idr.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lib/idr.c b/lib/idr.c
index 07098eb4ddc3..2ad96befa17a 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -460,10 +460,12 @@ int ida_alloc_range(struct ida *ida, unsigned int min, unsigned int max,
 		bit = min % IDA_BITMAP_BITS;
 		goto retry;
 	}
-	if (bitmap != alloc)
+	if (xas_error(&xas)) {
 		kfree(alloc);
-	if (xas_error(&xas))
 		return xas_error(&xas);
+	}
+	if (bitmap != alloc)
+		kfree(alloc);
 	return xas.xa_index * IDA_BITMAP_BITS + bit;
 alloc:
 	xas_unlock_irqrestore(&xas, flags);
-- 
2.34.1



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-03-12 17:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-12 17:18 [PATCH] lib/idr: fix memory leak in ida_alloc_range() error path Josh Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox