[PATCH master 2/4] lib: idr: make idr_for_each_entry removal safe

Ahmad Fatoum a.fatoum at barebox.org
Sun Jun 22 23:20:30 PDT 2025


There's no idr_for_each_entry_safe and the Linux implementation of
idr_for_each_entry already allows to free while iterating and indeed,
the newly introduced 9P file system makes use of exactly that.

The barebox implementation is home-grown, because we didn't yet import
radix tree support and it did not allow freeing during iteration.

Rework our macro to allow this.

Fixes: d28d3d9159a1 ("include: linux/idr.h: implement more Linux API")
Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 include/linux/idr.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/include/linux/idr.h b/include/linux/idr.h
index 07adde713477..e726d17b591f 100644
--- a/include/linux/idr.h
+++ b/include/linux/idr.h
@@ -37,9 +37,10 @@ struct idr {
  */
 #define idr_for_each_entry(_idr, _entry, _id)				\
 	for (struct idr *iter =						\
-	     list_first_entry_or_null(&(_idr)->list, struct idr, list);	\
-	     (iter && iter != (_idr)) || (_entry = NULL);	\
-	     iter = list_next_entry(iter, list))			\
+	     list_first_entry_or_null(&(_idr)->list, struct idr, list), \
+	     *tmp = iter ? list_next_entry(iter, list) : NULL;		\
+	     (iter && iter != (_idr)) || (_entry = NULL);		\
+	     iter = tmp, tmp = tmp ?  list_next_entry(tmp, list) : NULL)\
 	if ((_entry = iter->ptr, _id = iter->id, true))
 
 struct idr *__idr_find(struct idr *head, int lookup_id);
-- 
2.39.5




More information about the barebox mailing list