hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/include/linux/idr.h
....@@ -1,9 +1,9 @@
1
+/* SPDX-License-Identifier: GPL-2.0-only */
12 /*
23 * include/linux/idr.h
34 *
45 * 2002-10-18 written by Jim Houston jim.houston@ccur.com
56 * Copyright (C) 2002 by Concurrent Computer Corporation
6
- * Distributed under the GNU GPL license version 2.
77 *
88 * Small id to pointer translation service avoiding fixed sized
99 * tables.
....@@ -171,7 +171,7 @@
171171 */
172172 static inline void idr_preload_end(void)
173173 {
174
- preempt_enable();
174
+ local_unlock(&radix_tree_preloads.lock);
175175 }
176176
177177 /**
....@@ -191,14 +191,17 @@
191191 * idr_for_each_entry_ul() - Iterate over an IDR's elements of a given type.
192192 * @idr: IDR handle.
193193 * @entry: The type * to use as cursor.
194
+ * @tmp: A temporary placeholder for ID.
194195 * @id: Entry ID.
195196 *
196197 * @entry and @id do not need to be initialized before the loop, and
197198 * after normal termination @entry is left with the value NULL. This
198199 * is convenient for a "not found" value.
199200 */
200
-#define idr_for_each_entry_ul(idr, entry, id) \
201
- for (id = 0; ((entry) = idr_get_next_ul(idr, &(id))) != NULL; ++id)
201
+#define idr_for_each_entry_ul(idr, entry, tmp, id) \
202
+ for (tmp = 0, id = 0; \
203
+ tmp <= id && ((entry) = idr_get_next_ul(idr, &(id))) != NULL; \
204
+ tmp = id, ++id)
202205
203206 /**
204207 * idr_for_each_entry_continue() - Continue iteration over an IDR's elements of a given type
....@@ -213,9 +216,22 @@
213216 entry; \
214217 ++id, (entry) = idr_get_next((idr), &(id)))
215218
219
+/**
220
+ * idr_for_each_entry_continue_ul() - Continue iteration over an IDR's elements of a given type
221
+ * @idr: IDR handle.
222
+ * @entry: The type * to use as a cursor.
223
+ * @tmp: A temporary placeholder for ID.
224
+ * @id: Entry ID.
225
+ *
226
+ * Continue to iterate over entries, continuing after the current position.
227
+ */
228
+#define idr_for_each_entry_continue_ul(idr, entry, tmp, id) \
229
+ for (tmp = id; \
230
+ tmp <= id && ((entry) = idr_get_next_ul(idr, &(id))) != NULL; \
231
+ tmp = id, ++id)
232
+
216233 /*
217
- * IDA - IDR based id allocator, use when translation from id to
218
- * pointer isn't necessary.
234
+ * IDA - ID Allocator, use when translation from id to pointer isn't necessary.
219235 */
220236 #define IDA_CHUNK_SIZE 128 /* 128 bytes per chunk */
221237 #define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long))
....@@ -225,14 +241,14 @@
225241 unsigned long bitmap[IDA_BITMAP_LONGS];
226242 };
227243
228
-DECLARE_PER_CPU(struct ida_bitmap *, ida_bitmap);
229
-
230244 struct ida {
231
- struct radix_tree_root ida_rt;
245
+ struct xarray xa;
232246 };
233247
248
+#define IDA_INIT_FLAGS (XA_FLAGS_LOCK_IRQ | XA_FLAGS_ALLOC)
249
+
234250 #define IDA_INIT(name) { \
235
- .ida_rt = RADIX_TREE_INIT(name, IDR_RT_MARKER | GFP_NOWAIT), \
251
+ .xa = XARRAY_INIT(name, IDA_INIT_FLAGS) \
236252 }
237253 #define DEFINE_IDA(name) struct ida name = IDA_INIT(name)
238254
....@@ -247,7 +263,8 @@
247263 *
248264 * Allocate an ID between 0 and %INT_MAX, inclusive.
249265 *
250
- * Context: Any context.
266
+ * Context: Any context. It is safe to call this function without
267
+ * locking in your code.
251268 * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
252269 * or %-ENOSPC if there are no free IDs.
253270 */
....@@ -264,7 +281,8 @@
264281 *
265282 * Allocate an ID between @min and %INT_MAX, inclusive.
266283 *
267
- * Context: Any context.
284
+ * Context: Any context. It is safe to call this function without
285
+ * locking in your code.
268286 * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
269287 * or %-ENOSPC if there are no free IDs.
270288 */
....@@ -281,7 +299,8 @@
281299 *
282300 * Allocate an ID between 0 and @max, inclusive.
283301 *
284
- * Context: Any context.
302
+ * Context: Any context. It is safe to call this function without
303
+ * locking in your code.
285304 * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
286305 * or %-ENOSPC if there are no free IDs.
287306 */
....@@ -292,18 +311,19 @@
292311
293312 static inline void ida_init(struct ida *ida)
294313 {
295
- INIT_RADIX_TREE(&ida->ida_rt, IDR_RT_MARKER | GFP_NOWAIT);
314
+ xa_init_flags(&ida->xa, IDA_INIT_FLAGS);
296315 }
297316
317
+/*
318
+ * ida_simple_get() and ida_simple_remove() are deprecated. Use
319
+ * ida_alloc() and ida_free() instead respectively.
320
+ */
298321 #define ida_simple_get(ida, start, end, gfp) \
299322 ida_alloc_range(ida, start, (end) - 1, gfp)
300323 #define ida_simple_remove(ida, id) ida_free(ida, id)
301324
302325 static inline bool ida_is_empty(const struct ida *ida)
303326 {
304
- return radix_tree_empty(&ida->ida_rt);
327
+ return xa_empty(&ida->xa);
305328 }
306
-
307
-/* in lib/radix-tree.c */
308
-int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
309329 #endif /* __IDR_H__ */