hc
2024-01-31 f9004dbfff8a3fbbd7e2a88c8a4327c7f2f8e5b2
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.
....@@ -169,7 +169,10 @@
169169 * Each idr_preload() should be matched with an invocation of this
170170 * function. See idr_preload() for details.
171171 */
172
-void idr_preload_end(void);
172
+static inline void idr_preload_end(void)
173
+{
174
+ local_unlock(&radix_tree_preloads.lock);
175
+}
173176
174177 /**
175178 * idr_for_each_entry() - Iterate over an IDR's elements of a given type.
....@@ -188,14 +191,17 @@
188191 * idr_for_each_entry_ul() - Iterate over an IDR's elements of a given type.
189192 * @idr: IDR handle.
190193 * @entry: The type * to use as cursor.
194
+ * @tmp: A temporary placeholder for ID.
191195 * @id: Entry ID.
192196 *
193197 * @entry and @id do not need to be initialized before the loop, and
194198 * after normal termination @entry is left with the value NULL. This
195199 * is convenient for a "not found" value.
196200 */
197
-#define idr_for_each_entry_ul(idr, entry, id) \
198
- 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)
199205
200206 /**
201207 * idr_for_each_entry_continue() - Continue iteration over an IDR's elements of a given type
....@@ -210,9 +216,22 @@
210216 entry; \
211217 ++id, (entry) = idr_get_next((idr), &(id)))
212218
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
+
213233 /*
214
- * IDA - IDR based id allocator, use when translation from id to
215
- * pointer isn't necessary.
234
+ * IDA - ID Allocator, use when translation from id to pointer isn't necessary.
216235 */
217236 #define IDA_CHUNK_SIZE 128 /* 128 bytes per chunk */
218237 #define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long))
....@@ -222,14 +241,14 @@
222241 unsigned long bitmap[IDA_BITMAP_LONGS];
223242 };
224243
225
-DECLARE_PER_CPU(struct ida_bitmap *, ida_bitmap);
226
-
227244 struct ida {
228
- struct radix_tree_root ida_rt;
245
+ struct xarray xa;
229246 };
230247
248
+#define IDA_INIT_FLAGS (XA_FLAGS_LOCK_IRQ | XA_FLAGS_ALLOC)
249
+
231250 #define IDA_INIT(name) { \
232
- .ida_rt = RADIX_TREE_INIT(name, IDR_RT_MARKER | GFP_NOWAIT), \
251
+ .xa = XARRAY_INIT(name, IDA_INIT_FLAGS) \
233252 }
234253 #define DEFINE_IDA(name) struct ida name = IDA_INIT(name)
235254
....@@ -244,7 +263,8 @@
244263 *
245264 * Allocate an ID between 0 and %INT_MAX, inclusive.
246265 *
247
- * Context: Any context.
266
+ * Context: Any context. It is safe to call this function without
267
+ * locking in your code.
248268 * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
249269 * or %-ENOSPC if there are no free IDs.
250270 */
....@@ -261,7 +281,8 @@
261281 *
262282 * Allocate an ID between @min and %INT_MAX, inclusive.
263283 *
264
- * Context: Any context.
284
+ * Context: Any context. It is safe to call this function without
285
+ * locking in your code.
265286 * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
266287 * or %-ENOSPC if there are no free IDs.
267288 */
....@@ -278,7 +299,8 @@
278299 *
279300 * Allocate an ID between 0 and @max, inclusive.
280301 *
281
- * Context: Any context.
302
+ * Context: Any context. It is safe to call this function without
303
+ * locking in your code.
282304 * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
283305 * or %-ENOSPC if there are no free IDs.
284306 */
....@@ -289,18 +311,19 @@
289311
290312 static inline void ida_init(struct ida *ida)
291313 {
292
- INIT_RADIX_TREE(&ida->ida_rt, IDR_RT_MARKER | GFP_NOWAIT);
314
+ xa_init_flags(&ida->xa, IDA_INIT_FLAGS);
293315 }
294316
317
+/*
318
+ * ida_simple_get() and ida_simple_remove() are deprecated. Use
319
+ * ida_alloc() and ida_free() instead respectively.
320
+ */
295321 #define ida_simple_get(ida, start, end, gfp) \
296322 ida_alloc_range(ida, start, (end) - 1, gfp)
297323 #define ida_simple_remove(ida, id) ida_free(ida, id)
298324
299325 static inline bool ida_is_empty(const struct ida *ida)
300326 {
301
- return radix_tree_empty(&ida->ida_rt);
327
+ return xa_empty(&ida->xa);
302328 }
303
-
304
-/* in lib/radix-tree.c */
305
-int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
306329 #endif /* __IDR_H__ */