.. | .. |
---|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-only */ |
---|
1 | 2 | /* |
---|
2 | 3 | * include/linux/idr.h |
---|
3 | 4 | * |
---|
4 | 5 | * 2002-10-18 written by Jim Houston jim.houston@ccur.com |
---|
5 | 6 | * Copyright (C) 2002 by Concurrent Computer Corporation |
---|
6 | | - * Distributed under the GNU GPL license version 2. |
---|
7 | 7 | * |
---|
8 | 8 | * Small id to pointer translation service avoiding fixed sized |
---|
9 | 9 | * tables. |
---|
.. | .. |
---|
171 | 171 | */ |
---|
172 | 172 | static inline void idr_preload_end(void) |
---|
173 | 173 | { |
---|
174 | | - preempt_enable(); |
---|
| 174 | + local_unlock(&radix_tree_preloads.lock); |
---|
175 | 175 | } |
---|
176 | 176 | |
---|
177 | 177 | /** |
---|
.. | .. |
---|
191 | 191 | * idr_for_each_entry_ul() - Iterate over an IDR's elements of a given type. |
---|
192 | 192 | * @idr: IDR handle. |
---|
193 | 193 | * @entry: The type * to use as cursor. |
---|
| 194 | + * @tmp: A temporary placeholder for ID. |
---|
194 | 195 | * @id: Entry ID. |
---|
195 | 196 | * |
---|
196 | 197 | * @entry and @id do not need to be initialized before the loop, and |
---|
197 | 198 | * after normal termination @entry is left with the value NULL. This |
---|
198 | 199 | * is convenient for a "not found" value. |
---|
199 | 200 | */ |
---|
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) |
---|
202 | 205 | |
---|
203 | 206 | /** |
---|
204 | 207 | * idr_for_each_entry_continue() - Continue iteration over an IDR's elements of a given type |
---|
.. | .. |
---|
213 | 216 | entry; \ |
---|
214 | 217 | ++id, (entry) = idr_get_next((idr), &(id))) |
---|
215 | 218 | |
---|
| 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 | + |
---|
216 | 233 | /* |
---|
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. |
---|
219 | 235 | */ |
---|
220 | 236 | #define IDA_CHUNK_SIZE 128 /* 128 bytes per chunk */ |
---|
221 | 237 | #define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long)) |
---|
.. | .. |
---|
225 | 241 | unsigned long bitmap[IDA_BITMAP_LONGS]; |
---|
226 | 242 | }; |
---|
227 | 243 | |
---|
228 | | -DECLARE_PER_CPU(struct ida_bitmap *, ida_bitmap); |
---|
229 | | - |
---|
230 | 244 | struct ida { |
---|
231 | | - struct radix_tree_root ida_rt; |
---|
| 245 | + struct xarray xa; |
---|
232 | 246 | }; |
---|
233 | 247 | |
---|
| 248 | +#define IDA_INIT_FLAGS (XA_FLAGS_LOCK_IRQ | XA_FLAGS_ALLOC) |
---|
| 249 | + |
---|
234 | 250 | #define IDA_INIT(name) { \ |
---|
235 | | - .ida_rt = RADIX_TREE_INIT(name, IDR_RT_MARKER | GFP_NOWAIT), \ |
---|
| 251 | + .xa = XARRAY_INIT(name, IDA_INIT_FLAGS) \ |
---|
236 | 252 | } |
---|
237 | 253 | #define DEFINE_IDA(name) struct ida name = IDA_INIT(name) |
---|
238 | 254 | |
---|
.. | .. |
---|
247 | 263 | * |
---|
248 | 264 | * Allocate an ID between 0 and %INT_MAX, inclusive. |
---|
249 | 265 | * |
---|
250 | | - * Context: Any context. |
---|
| 266 | + * Context: Any context. It is safe to call this function without |
---|
| 267 | + * locking in your code. |
---|
251 | 268 | * Return: The allocated ID, or %-ENOMEM if memory could not be allocated, |
---|
252 | 269 | * or %-ENOSPC if there are no free IDs. |
---|
253 | 270 | */ |
---|
.. | .. |
---|
264 | 281 | * |
---|
265 | 282 | * Allocate an ID between @min and %INT_MAX, inclusive. |
---|
266 | 283 | * |
---|
267 | | - * Context: Any context. |
---|
| 284 | + * Context: Any context. It is safe to call this function without |
---|
| 285 | + * locking in your code. |
---|
268 | 286 | * Return: The allocated ID, or %-ENOMEM if memory could not be allocated, |
---|
269 | 287 | * or %-ENOSPC if there are no free IDs. |
---|
270 | 288 | */ |
---|
.. | .. |
---|
281 | 299 | * |
---|
282 | 300 | * Allocate an ID between 0 and @max, inclusive. |
---|
283 | 301 | * |
---|
284 | | - * Context: Any context. |
---|
| 302 | + * Context: Any context. It is safe to call this function without |
---|
| 303 | + * locking in your code. |
---|
285 | 304 | * Return: The allocated ID, or %-ENOMEM if memory could not be allocated, |
---|
286 | 305 | * or %-ENOSPC if there are no free IDs. |
---|
287 | 306 | */ |
---|
.. | .. |
---|
292 | 311 | |
---|
293 | 312 | static inline void ida_init(struct ida *ida) |
---|
294 | 313 | { |
---|
295 | | - INIT_RADIX_TREE(&ida->ida_rt, IDR_RT_MARKER | GFP_NOWAIT); |
---|
| 314 | + xa_init_flags(&ida->xa, IDA_INIT_FLAGS); |
---|
296 | 315 | } |
---|
297 | 316 | |
---|
| 317 | +/* |
---|
| 318 | + * ida_simple_get() and ida_simple_remove() are deprecated. Use |
---|
| 319 | + * ida_alloc() and ida_free() instead respectively. |
---|
| 320 | + */ |
---|
298 | 321 | #define ida_simple_get(ida, start, end, gfp) \ |
---|
299 | 322 | ida_alloc_range(ida, start, (end) - 1, gfp) |
---|
300 | 323 | #define ida_simple_remove(ida, id) ida_free(ida, id) |
---|
301 | 324 | |
---|
302 | 325 | static inline bool ida_is_empty(const struct ida *ida) |
---|
303 | 326 | { |
---|
304 | | - return radix_tree_empty(&ida->ida_rt); |
---|
| 327 | + return xa_empty(&ida->xa); |
---|
305 | 328 | } |
---|
306 | | - |
---|
307 | | -/* in lib/radix-tree.c */ |
---|
308 | | -int ida_pre_get(struct ida *ida, gfp_t gfp_mask); |
---|
309 | 329 | #endif /* __IDR_H__ */ |
---|