xref: /linux/include/linux/idr.h (revision 2233228b65291f8e9b04ee8be52bd2f694e12889)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * include/linux/idr.h
4  *
5  * 2002-10-18  written by Jim Houston jim.houston@ccur.com
6  *	Copyright (C) 2002 by Concurrent Computer Corporation
7  *
8  * Small id to pointer translation service avoiding fixed sized
9  * tables.
10  */
11 
12 #ifndef __IDR_H__
13 #define __IDR_H__
14 
15 #include <linux/radix-tree.h>
16 #include <linux/gfp.h>
17 #include <linux/percpu.h>
18 #include <linux/cleanup.h>
19 #include <linux/compiler.h>
20 
21 struct idr {
22 	struct radix_tree_root	idr_rt;
23 	unsigned int		idr_base;
24 	unsigned int		idr_next;
25 };
26 
27 /*
28  * The IDR API does not expose the tagging functionality of the radix tree
29  * to users.  Use tag 0 to track whether a node has free space below it.
30  */
31 #define IDR_FREE	0
32 
33 /* Set the IDR flag and the IDR_FREE tag */
34 #define IDR_RT_MARKER	(ROOT_IS_IDR | (__force gfp_t)			\
35 					(1 << (ROOT_TAG_SHIFT + IDR_FREE)))
36 
37 #define IDR_INIT_BASE(name, base) {					\
38 	.idr_rt = RADIX_TREE_INIT(name, IDR_RT_MARKER),			\
39 	.idr_base = (base),						\
40 	.idr_next = 0,							\
41 }
42 
43 /**
44  * IDR_INIT() - Initialise an IDR.
45  * @name: Name of IDR.
46  *
47  * A freshly-initialised IDR contains no IDs.
48  */
49 #define IDR_INIT(name)	IDR_INIT_BASE(name, 0)
50 
51 /**
52  * DEFINE_IDR() - Define a statically-allocated IDR.
53  * @name: Name of IDR.
54  *
55  * An IDR defined using this macro is ready for use with no additional
56  * initialisation required.  It contains no IDs.
57  */
58 #define DEFINE_IDR(name)	struct idr name = IDR_INIT(name)
59 
60 /**
61  * idr_get_cursor - Return the current position of the cyclic allocator
62  * @idr: idr handle
63  *
64  * The value returned is the value that will be next returned from
65  * idr_alloc_cyclic() if it is free (otherwise the search will start from
66  * this position).
67  */
68 static inline unsigned int idr_get_cursor(const struct idr *idr)
69 {
70 	return READ_ONCE(idr->idr_next);
71 }
72 
73 /**
74  * idr_set_cursor - Set the current position of the cyclic allocator
75  * @idr: idr handle
76  * @val: new position
77  *
78  * The next call to idr_alloc_cyclic() will return @val if it is free
79  * (otherwise the search will start from this position).
80  */
81 static inline void idr_set_cursor(struct idr *idr, unsigned int val)
82 {
83 	WRITE_ONCE(idr->idr_next, val);
84 }
85 
86 /**
87  * DOC: idr sync
88  * idr synchronization (stolen from radix-tree.h)
89  *
90  * idr_find() is able to be called locklessly, using RCU. The caller must
91  * ensure calls to this function are made within rcu_read_lock() regions.
92  * Other readers (lock-free or otherwise) and modifications may be running
93  * concurrently.
94  *
95  * It is still required that the caller manage the synchronization and
96  * lifetimes of the items. So if RCU lock-free lookups are used, typically
97  * this would mean that the items have their own locks, or are amenable to
98  * lock-free access; and that the items are freed by RCU (or only freed after
99  * having been deleted from the idr tree *and* a synchronize_rcu() grace
100  * period).
101  */
102 
103 #define idr_lock(idr)		xa_lock(&(idr)->idr_rt)
104 #define idr_unlock(idr)		xa_unlock(&(idr)->idr_rt)
105 #define idr_lock_bh(idr)	xa_lock_bh(&(idr)->idr_rt)
106 #define idr_unlock_bh(idr)	xa_unlock_bh(&(idr)->idr_rt)
107 #define idr_lock_irq(idr)	xa_lock_irq(&(idr)->idr_rt)
108 #define idr_unlock_irq(idr)	xa_unlock_irq(&(idr)->idr_rt)
109 #define idr_lock_irqsave(idr, flags) \
110 				xa_lock_irqsave(&(idr)->idr_rt, flags)
111 #define idr_unlock_irqrestore(idr, flags) \
112 				xa_unlock_irqrestore(&(idr)->idr_rt, flags)
113 
114 void idr_preload(gfp_t gfp_mask);
115 
116 int idr_alloc(struct idr *, void *ptr, int start, int end, gfp_t);
117 int __must_check idr_alloc_u32(struct idr *, void *ptr, u32 *id,
118 				unsigned long max, gfp_t);
119 int idr_alloc_cyclic(struct idr *, void *ptr, int start, int end, gfp_t);
120 void *idr_remove(struct idr *, unsigned long id);
121 void *idr_find(const struct idr *, unsigned long id);
122 int idr_for_each(const struct idr *,
123 		 int (*fn)(int id, void *p, void *data), void *data);
124 void *idr_get_next(struct idr *, int *nextid);
125 void *idr_get_next_ul(struct idr *, unsigned long *nextid);
126 void *idr_replace(struct idr *, void *, unsigned long id);
127 void idr_destroy(struct idr *);
128 
129 struct __class_idr {
130 	struct idr *idr;
131 	int id;
132 };
133 
134 #define idr_null ((struct __class_idr){ NULL, -1 })
135 #define take_idr_id(id) __get_and_null(id, idr_null)
136 
137 DEFINE_CLASS(idr_alloc, struct __class_idr,
138 	     if (_T.id >= 0) idr_remove(_T.idr, _T.id),
139 	     ((struct __class_idr){
140 	     	.idr = idr,
141 		.id = idr_alloc(idr, ptr, start, end, gfp),
142 	     }),
143 	     struct idr *idr, void *ptr, int start, int end, gfp_t gfp);
144 
145 /**
146  * idr_init_base() - Initialise an IDR.
147  * @idr: IDR handle.
148  * @base: The base value for the IDR.
149  *
150  * This variation of idr_init() creates an IDR which will allocate IDs
151  * starting at %base.
152  */
153 static inline void idr_init_base(struct idr *idr, int base)
154 {
155 	INIT_RADIX_TREE(&idr->idr_rt, IDR_RT_MARKER);
156 	idr->idr_base = base;
157 	idr->idr_next = 0;
158 }
159 
160 /**
161  * idr_init() - Initialise an IDR.
162  * @idr: IDR handle.
163  *
164  * Initialise a dynamically allocated IDR.  To initialise a
165  * statically allocated IDR, use DEFINE_IDR().
166  */
167 static inline void idr_init(struct idr *idr)
168 {
169 	idr_init_base(idr, 0);
170 }
171 
172 /**
173  * idr_is_empty() - Are there any IDs allocated?
174  * @idr: IDR handle.
175  *
176  * Return: %true if any IDs have been allocated from this IDR.
177  */
178 static inline bool idr_is_empty(const struct idr *idr)
179 {
180 	return radix_tree_empty(&idr->idr_rt) &&
181 		radix_tree_tagged(&idr->idr_rt, IDR_FREE);
182 }
183 
184 /**
185  * idr_preload_end - end preload section started with idr_preload()
186  *
187  * Each idr_preload() should be matched with an invocation of this
188  * function.  See idr_preload() for details.
189  */
190 static inline void idr_preload_end(void)
191 {
192 	local_unlock(&radix_tree_preloads.lock);
193 }
194 
195 /**
196  * idr_for_each_entry() - Iterate over an IDR's elements of a given type.
197  * @idr: IDR handle.
198  * @entry: The type * to use as cursor
199  * @id: Entry ID.
200  *
201  * @entry and @id do not need to be initialized before the loop, and
202  * after normal termination @entry is left with the value NULL.  This
203  * is convenient for a "not found" value.
204  */
205 #define idr_for_each_entry(idr, entry, id)			\
206 	for (id = 0; ((entry) = idr_get_next(idr, &(id))) != NULL; id += 1U)
207 
208 /**
209  * idr_for_each_entry_ul() - Iterate over an IDR's elements of a given type.
210  * @idr: IDR handle.
211  * @entry: The type * to use as cursor.
212  * @tmp: A temporary placeholder for ID.
213  * @id: Entry ID.
214  *
215  * @entry and @id do not need to be initialized before the loop, and
216  * after normal termination @entry is left with the value NULL.  This
217  * is convenient for a "not found" value.
218  */
219 #define idr_for_each_entry_ul(idr, entry, tmp, id)			\
220 	for (tmp = 0, id = 0;						\
221 	     ((entry) = tmp <= id ? idr_get_next_ul(idr, &(id)) : NULL) != NULL; \
222 	     tmp = id, ++id)
223 
224 /**
225  * idr_for_each_entry_continue() - Continue iteration over an IDR's elements of a given type
226  * @idr: IDR handle.
227  * @entry: The type * to use as a cursor.
228  * @id: Entry ID.
229  *
230  * Continue to iterate over entries, continuing after the current position.
231  */
232 #define idr_for_each_entry_continue(idr, entry, id)			\
233 	for ((entry) = idr_get_next((idr), &(id));			\
234 	     entry;							\
235 	     ++id, (entry) = idr_get_next((idr), &(id)))
236 
237 /**
238  * idr_for_each_entry_continue_ul() - Continue iteration over an IDR's elements of a given type
239  * @idr: IDR handle.
240  * @entry: The type * to use as a cursor.
241  * @tmp: A temporary placeholder for ID.
242  * @id: Entry ID.
243  *
244  * Continue to iterate over entries, continuing after the current position.
245  * After normal termination @entry is left with the value NULL.  This
246  * is convenient for a "not found" value.
247  */
248 #define idr_for_each_entry_continue_ul(idr, entry, tmp, id)		\
249 	for (tmp = id;							\
250 	     ((entry) = tmp <= id ? idr_get_next_ul(idr, &(id)) : NULL) != NULL; \
251 	     tmp = id, ++id)
252 
253 /*
254  * IDA - ID Allocator, use when translation from id to pointer isn't necessary.
255  */
256 #define IDA_CHUNK_SIZE		128	/* 128 bytes per chunk */
257 #define IDA_BITMAP_LONGS	(IDA_CHUNK_SIZE / sizeof(long))
258 #define IDA_BITMAP_BITS 	(IDA_BITMAP_LONGS * sizeof(long) * 8)
259 
260 struct ida_bitmap {
261 	unsigned long		bitmap[IDA_BITMAP_LONGS];
262 };
263 
264 struct ida {
265 	struct xarray xa;
266 };
267 
268 #define IDA_INIT_FLAGS	(XA_FLAGS_LOCK_IRQ | XA_FLAGS_ALLOC)
269 
270 #define IDA_INIT(name)	{						\
271 	.xa = XARRAY_INIT(name, IDA_INIT_FLAGS)				\
272 }
273 #define DEFINE_IDA(name)							\
274 	struct ida name = IDA_INIT(name);					\
275 	ASSERT_STATIC_STORAGE(name)
276 
277 int ida_alloc_range(struct ida *, unsigned int min, unsigned int max, gfp_t);
278 void ida_free(struct ida *, unsigned int id);
279 void ida_destroy(struct ida *ida);
280 int ida_find_first_range(struct ida *ida, unsigned int min, unsigned int max);
281 
282 /**
283  * ida_alloc() - Allocate an unused ID.
284  * @ida: IDA handle.
285  * @gfp: Memory allocation flags.
286  *
287  * Allocate an ID between 0 and %INT_MAX, inclusive.
288  *
289  * Context: Any context. It is safe to call this function without
290  * locking in your code.
291  * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
292  * or %-ENOSPC if there are no free IDs.
293  */
294 static inline int ida_alloc(struct ida *ida, gfp_t gfp)
295 {
296 	return ida_alloc_range(ida, 0, ~0, gfp);
297 }
298 
299 /**
300  * ida_alloc_min() - Allocate an unused ID.
301  * @ida: IDA handle.
302  * @min: Lowest ID to allocate.
303  * @gfp: Memory allocation flags.
304  *
305  * Allocate an ID between @min and %INT_MAX, inclusive.
306  *
307  * Context: Any context. It is safe to call this function without
308  * locking in your code.
309  * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
310  * or %-ENOSPC if there are no free IDs.
311  */
312 static inline int ida_alloc_min(struct ida *ida, unsigned int min, gfp_t gfp)
313 {
314 	return ida_alloc_range(ida, min, ~0, gfp);
315 }
316 
317 /**
318  * ida_alloc_max() - Allocate an unused ID.
319  * @ida: IDA handle.
320  * @max: Highest ID to allocate.
321  * @gfp: Memory allocation flags.
322  *
323  * Allocate an ID between 0 and @max, inclusive.
324  *
325  * Context: Any context. It is safe to call this function without
326  * locking in your code.
327  * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
328  * or %-ENOSPC if there are no free IDs.
329  */
330 static inline int ida_alloc_max(struct ida *ida, unsigned int max, gfp_t gfp)
331 {
332 	return ida_alloc_range(ida, 0, max, gfp);
333 }
334 
335 static inline void ida_init(struct ida *ida)
336 {
337 	xa_init_flags(&ida->xa, IDA_INIT_FLAGS);
338 }
339 
340 static inline bool ida_is_empty(const struct ida *ida)
341 {
342 	return xa_empty(&ida->xa);
343 }
344 
345 static inline bool ida_exists(struct ida *ida, unsigned int id)
346 {
347 	return ida_find_first_range(ida, id, id) == id;
348 }
349 
350 static inline int ida_find_first(struct ida *ida)
351 {
352 	return ida_find_first_range(ida, 0, ~0);
353 }
354 #endif /* __IDR_H__ */
355