]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
0a835c4f | 2 | #include <linux/bitmap.h> |
460488c5 | 3 | #include <linux/bug.h> |
8bc3bcc9 | 4 | #include <linux/export.h> |
1da177e4 | 5 | #include <linux/idr.h> |
0a835c4f | 6 | #include <linux/slab.h> |
88eca020 | 7 | #include <linux/spinlock.h> |
b94078e6 | 8 | #include <linux/xarray.h> |
1da177e4 | 9 | |
e096f6a7 MW |
10 | /** |
11 | * idr_alloc_u32() - Allocate an ID. | |
12 | * @idr: IDR handle. | |
13 | * @ptr: Pointer to be associated with the new ID. | |
14 | * @nextid: Pointer to an ID. | |
15 | * @max: The maximum ID to allocate (inclusive). | |
16 | * @gfp: Memory allocation flags. | |
17 | * | |
18 | * Allocates an unused ID in the range specified by @nextid and @max. | |
19 | * Note that @max is inclusive whereas the @end parameter to idr_alloc() | |
460488c5 MW |
20 | * is exclusive. The new ID is assigned to @nextid before the pointer |
21 | * is inserted into the IDR, so if @nextid points into the object pointed | |
22 | * to by @ptr, a concurrent lookup will not find an uninitialised ID. | |
e096f6a7 MW |
23 | * |
24 | * The caller should provide their own locking to ensure that two | |
25 | * concurrent modifications to the IDR are not possible. Read-only | |
26 | * accesses to the IDR may be done under the RCU read lock or may | |
27 | * exclude simultaneous writers. | |
28 | * | |
29 | * Return: 0 if an ID was allocated, -ENOMEM if memory allocation failed, | |
30 | * or -ENOSPC if no free IDs could be found. If an error occurred, | |
31 | * @nextid is unchanged. | |
32 | */ | |
33 | int idr_alloc_u32(struct idr *idr, void *ptr, u32 *nextid, | |
34 | unsigned long max, gfp_t gfp) | |
d5c7409f | 35 | { |
0a835c4f | 36 | struct radix_tree_iter iter; |
388f79fd | 37 | void __rcu **slot; |
4b0ad076 MW |
38 | unsigned int base = idr->idr_base; |
39 | unsigned int id = *nextid; | |
d5c7409f | 40 | |
f8d5d0cc MW |
41 | if (WARN_ON_ONCE(!(idr->idr_rt.xa_flags & ROOT_IS_IDR))) |
42 | idr->idr_rt.xa_flags |= IDR_RT_MARKER; | |
d5c7409f | 43 | |
6ce711f2 MW |
44 | id = (id < base) ? 0 : id - base; |
45 | radix_tree_iter_init(&iter, id); | |
46 | slot = idr_get_free(&idr->idr_rt, &iter, gfp, max - base); | |
0a835c4f MW |
47 | if (IS_ERR(slot)) |
48 | return PTR_ERR(slot); | |
d5c7409f | 49 | |
6ce711f2 | 50 | *nextid = iter.index + base; |
460488c5 | 51 | /* there is a memory barrier inside radix_tree_iter_replace() */ |
0a835c4f MW |
52 | radix_tree_iter_replace(&idr->idr_rt, &iter, slot, ptr); |
53 | radix_tree_iter_tag_clear(&idr->idr_rt, &iter, IDR_FREE); | |
388f79fd | 54 | |
388f79fd | 55 | return 0; |
d5c7409f | 56 | } |
460488c5 | 57 | EXPORT_SYMBOL_GPL(idr_alloc_u32); |
d5c7409f | 58 | |
3e6628c4 | 59 | /** |
460488c5 MW |
60 | * idr_alloc() - Allocate an ID. |
61 | * @idr: IDR handle. | |
62 | * @ptr: Pointer to be associated with the new ID. | |
63 | * @start: The minimum ID (inclusive). | |
64 | * @end: The maximum ID (exclusive). | |
65 | * @gfp: Memory allocation flags. | |
9bb26bc1 | 66 | * |
460488c5 MW |
67 | * Allocates an unused ID in the range specified by @start and @end. If |
68 | * @end is <= 0, it is treated as one larger than %INT_MAX. This allows | |
69 | * callers to use @start + N as @end as long as N is within integer range. | |
70 | * | |
71 | * The caller should provide their own locking to ensure that two | |
72 | * concurrent modifications to the IDR are not possible. Read-only | |
73 | * accesses to the IDR may be done under the RCU read lock or may | |
74 | * exclude simultaneous writers. | |
75 | * | |
76 | * Return: The newly allocated ID, -ENOMEM if memory allocation failed, | |
77 | * or -ENOSPC if no free IDs could be found. | |
8d3b3591 | 78 | */ |
460488c5 | 79 | int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp) |
1da177e4 | 80 | { |
460488c5 MW |
81 | u32 id = start; |
82 | int ret; | |
83 | ||
84 | if (WARN_ON_ONCE(start < 0)) | |
85 | return -EINVAL; | |
86 | ||
87 | ret = idr_alloc_u32(idr, ptr, &id, end > 0 ? end - 1 : INT_MAX, gfp); | |
88 | if (ret) | |
89 | return ret; | |
1da177e4 | 90 | |
460488c5 MW |
91 | return id; |
92 | } | |
93 | EXPORT_SYMBOL_GPL(idr_alloc); | |
94 | ||
95 | /** | |
96 | * idr_alloc_cyclic() - Allocate an ID cyclically. | |
97 | * @idr: IDR handle. | |
98 | * @ptr: Pointer to be associated with the new ID. | |
99 | * @start: The minimum ID (inclusive). | |
100 | * @end: The maximum ID (exclusive). | |
101 | * @gfp: Memory allocation flags. | |
102 | * | |
2a15de80 | 103 | * Allocates an unused ID in the range specified by @start and @end. If |
460488c5 MW |
104 | * @end is <= 0, it is treated as one larger than %INT_MAX. This allows |
105 | * callers to use @start + N as @end as long as N is within integer range. | |
106 | * The search for an unused ID will start at the last ID allocated and will | |
107 | * wrap around to @start if no free IDs are found before reaching @end. | |
108 | * | |
109 | * The caller should provide their own locking to ensure that two | |
110 | * concurrent modifications to the IDR are not possible. Read-only | |
111 | * accesses to the IDR may be done under the RCU read lock or may | |
112 | * exclude simultaneous writers. | |
113 | * | |
114 | * Return: The newly allocated ID, -ENOMEM if memory allocation failed, | |
115 | * or -ENOSPC if no free IDs could be found. | |
116 | */ | |
117 | int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp) | |
118 | { | |
119 | u32 id = idr->idr_next; | |
120 | int err, max = end > 0 ? end - 1 : INT_MAX; | |
e8c8d1bc | 121 | |
460488c5 MW |
122 | if ((int)id < start) |
123 | id = start; | |
1da177e4 | 124 | |
460488c5 MW |
125 | err = idr_alloc_u32(idr, ptr, &id, max, gfp); |
126 | if ((err == -ENOSPC) && (id > start)) { | |
127 | id = start; | |
128 | err = idr_alloc_u32(idr, ptr, &id, max, gfp); | |
129 | } | |
130 | if (err) | |
131 | return err; | |
1da177e4 | 132 | |
460488c5 | 133 | idr->idr_next = id + 1; |
0a835c4f | 134 | return id; |
1da177e4 | 135 | } |
0a835c4f | 136 | EXPORT_SYMBOL(idr_alloc_cyclic); |
1da177e4 | 137 | |
6ce711f2 MW |
138 | /** |
139 | * idr_remove() - Remove an ID from the IDR. | |
140 | * @idr: IDR handle. | |
141 | * @id: Pointer ID. | |
142 | * | |
143 | * Removes this ID from the IDR. If the ID was not previously in the IDR, | |
144 | * this function returns %NULL. | |
145 | * | |
146 | * Since this function modifies the IDR, the caller should provide their | |
147 | * own locking to ensure that concurrent modification of the same IDR is | |
148 | * not possible. | |
149 | * | |
150 | * Return: The pointer formerly associated with this ID. | |
151 | */ | |
152 | void *idr_remove(struct idr *idr, unsigned long id) | |
153 | { | |
154 | return radix_tree_delete_item(&idr->idr_rt, id - idr->idr_base, NULL); | |
155 | } | |
156 | EXPORT_SYMBOL_GPL(idr_remove); | |
157 | ||
158 | /** | |
159 | * idr_find() - Return pointer for given ID. | |
160 | * @idr: IDR handle. | |
161 | * @id: Pointer ID. | |
162 | * | |
163 | * Looks up the pointer associated with this ID. A %NULL pointer may | |
164 | * indicate that @id is not allocated or that the %NULL pointer was | |
165 | * associated with this ID. | |
166 | * | |
167 | * This function can be called under rcu_read_lock(), given that the leaf | |
168 | * pointers lifetimes are correctly managed. | |
169 | * | |
170 | * Return: The pointer associated with this ID. | |
171 | */ | |
172 | void *idr_find(const struct idr *idr, unsigned long id) | |
173 | { | |
174 | return radix_tree_lookup(&idr->idr_rt, id - idr->idr_base); | |
175 | } | |
176 | EXPORT_SYMBOL_GPL(idr_find); | |
177 | ||
96d7fa42 | 178 | /** |
7a457577 MW |
179 | * idr_for_each() - Iterate through all stored pointers. |
180 | * @idr: IDR handle. | |
181 | * @fn: Function to be called for each pointer. | |
182 | * @data: Data passed to callback function. | |
96d7fa42 | 183 | * |
0a835c4f | 184 | * The callback function will be called for each entry in @idr, passing |
7a457577 | 185 | * the ID, the entry and @data. |
96d7fa42 | 186 | * |
0a835c4f MW |
187 | * If @fn returns anything other than %0, the iteration stops and that |
188 | * value is returned from this function. | |
96d7fa42 | 189 | * |
0a835c4f MW |
190 | * idr_for_each() can be called concurrently with idr_alloc() and |
191 | * idr_remove() if protected by RCU. Newly added entries may not be | |
192 | * seen and deleted entries may be seen, but adding and removing entries | |
193 | * will not cause other entries to be skipped, nor spurious ones to be seen. | |
96d7fa42 | 194 | */ |
0a835c4f MW |
195 | int idr_for_each(const struct idr *idr, |
196 | int (*fn)(int id, void *p, void *data), void *data) | |
96d7fa42 | 197 | { |
0a835c4f | 198 | struct radix_tree_iter iter; |
7e73eb0b | 199 | void __rcu **slot; |
6ce711f2 | 200 | int base = idr->idr_base; |
96d7fa42 | 201 | |
0a835c4f | 202 | radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, 0) { |
72fd6c7b | 203 | int ret; |
4b0ad076 | 204 | unsigned long id = iter.index + base; |
72fd6c7b | 205 | |
4b0ad076 | 206 | if (WARN_ON_ONCE(id > INT_MAX)) |
72fd6c7b | 207 | break; |
4b0ad076 | 208 | ret = fn(id, rcu_dereference_raw(*slot), data); |
0a835c4f MW |
209 | if (ret) |
210 | return ret; | |
96d7fa42 KH |
211 | } |
212 | ||
0a835c4f | 213 | return 0; |
96d7fa42 KH |
214 | } |
215 | EXPORT_SYMBOL(idr_for_each); | |
216 | ||
38460b48 | 217 | /** |
5a74ac4c | 218 | * idr_get_next_ul() - Find next populated entry. |
7a457577 MW |
219 | * @idr: IDR handle. |
220 | * @nextid: Pointer to an ID. | |
0a835c4f MW |
221 | * |
222 | * Returns the next populated entry in the tree with an ID greater than | |
223 | * or equal to the value pointed to by @nextid. On exit, @nextid is updated | |
224 | * to the ID of the found value. To use in a loop, the value pointed to by | |
225 | * nextid must be incremented by the user. | |
38460b48 | 226 | */ |
5a74ac4c | 227 | void *idr_get_next_ul(struct idr *idr, unsigned long *nextid) |
38460b48 | 228 | { |
0a835c4f | 229 | struct radix_tree_iter iter; |
7e73eb0b | 230 | void __rcu **slot; |
5c089fd0 | 231 | void *entry = NULL; |
4b0ad076 MW |
232 | unsigned long base = idr->idr_base; |
233 | unsigned long id = *nextid; | |
38460b48 | 234 | |
6ce711f2 | 235 | id = (id < base) ? 0 : id - base; |
5c089fd0 MWO |
236 | radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, id) { |
237 | entry = rcu_dereference_raw(*slot); | |
238 | if (!entry) | |
239 | continue; | |
240 | if (!xa_is_internal(entry)) | |
241 | break; | |
242 | if (slot != &idr->idr_rt.xa_head && !xa_is_retry(entry)) | |
243 | break; | |
244 | slot = radix_tree_iter_retry(&iter); | |
245 | } | |
0a835c4f | 246 | if (!slot) |
38460b48 | 247 | return NULL; |
72fd6c7b | 248 | |
5a74ac4c | 249 | *nextid = iter.index + base; |
5c089fd0 | 250 | return entry; |
38460b48 | 251 | } |
5a74ac4c | 252 | EXPORT_SYMBOL(idr_get_next_ul); |
38460b48 | 253 | |
7a457577 | 254 | /** |
5a74ac4c | 255 | * idr_get_next() - Find next populated entry. |
7a457577 MW |
256 | * @idr: IDR handle. |
257 | * @nextid: Pointer to an ID. | |
258 | * | |
259 | * Returns the next populated entry in the tree with an ID greater than | |
260 | * or equal to the value pointed to by @nextid. On exit, @nextid is updated | |
261 | * to the ID of the found value. To use in a loop, the value pointed to by | |
262 | * nextid must be incremented by the user. | |
263 | */ | |
5a74ac4c | 264 | void *idr_get_next(struct idr *idr, int *nextid) |
388f79fd | 265 | { |
6ce711f2 | 266 | unsigned long id = *nextid; |
5a74ac4c | 267 | void *entry = idr_get_next_ul(idr, &id); |
388f79fd | 268 | |
5a74ac4c | 269 | if (WARN_ON_ONCE(id > INT_MAX)) |
388f79fd | 270 | return NULL; |
5a74ac4c MWO |
271 | *nextid = id; |
272 | return entry; | |
388f79fd | 273 | } |
5a74ac4c | 274 | EXPORT_SYMBOL(idr_get_next); |
388f79fd | 275 | |
5806f07c | 276 | /** |
460488c5 MW |
277 | * idr_replace() - replace pointer for given ID. |
278 | * @idr: IDR handle. | |
279 | * @ptr: New pointer to associate with the ID. | |
280 | * @id: ID to change. | |
5806f07c | 281 | * |
0a835c4f MW |
282 | * Replace the pointer registered with an ID and return the old value. |
283 | * This function can be called under the RCU read lock concurrently with | |
284 | * idr_alloc() and idr_remove() (as long as the ID being removed is not | |
285 | * the one being replaced!). | |
5806f07c | 286 | * |
a70e43a5 | 287 | * Returns: the old value on success. %-ENOENT indicates that @id was not |
234a4624 | 288 | * found. %-EINVAL indicates that @ptr was not valid. |
5806f07c | 289 | */ |
234a4624 | 290 | void *idr_replace(struct idr *idr, void *ptr, unsigned long id) |
5806f07c | 291 | { |
0a835c4f | 292 | struct radix_tree_node *node; |
7e73eb0b | 293 | void __rcu **slot = NULL; |
0a835c4f | 294 | void *entry; |
5806f07c | 295 | |
6ce711f2 | 296 | id -= idr->idr_base; |
e8c8d1bc | 297 | |
0a835c4f MW |
298 | entry = __radix_tree_lookup(&idr->idr_rt, id, &node, &slot); |
299 | if (!slot || radix_tree_tag_get(&idr->idr_rt, id, IDR_FREE)) | |
5806f07c JM |
300 | return ERR_PTR(-ENOENT); |
301 | ||
1cf56f9d | 302 | __radix_tree_replace(&idr->idr_rt, node, slot, ptr); |
5806f07c | 303 | |
0a835c4f | 304 | return entry; |
5806f07c | 305 | } |
234a4624 | 306 | EXPORT_SYMBOL(idr_replace); |
5806f07c | 307 | |
56083ab1 RD |
308 | /** |
309 | * DOC: IDA description | |
72dba584 | 310 | * |
0a835c4f MW |
311 | * The IDA is an ID allocator which does not provide the ability to |
312 | * associate an ID with a pointer. As such, it only needs to store one | |
313 | * bit per ID, and so is more space efficient than an IDR. To use an IDA, | |
314 | * define it using DEFINE_IDA() (or embed a &struct ida in a data structure, | |
315 | * then initialise it using ida_init()). To allocate a new ID, call | |
5ade60dd MW |
316 | * ida_alloc(), ida_alloc_min(), ida_alloc_max() or ida_alloc_range(). |
317 | * To free an ID, call ida_free(). | |
0a835c4f | 318 | * |
b03f8e43 MW |
319 | * ida_destroy() can be used to dispose of an IDA without needing to |
320 | * free the individual IDs in it. You can use ida_is_empty() to find | |
321 | * out whether the IDA has any IDs currently allocated. | |
0a835c4f | 322 | * |
f32f004c MW |
323 | * The IDA handles its own locking. It is safe to call any of the IDA |
324 | * functions without synchronisation in your code. | |
325 | * | |
0a835c4f MW |
326 | * IDs are currently limited to the range [0-INT_MAX]. If this is an awkward |
327 | * limitation, it should be quite straightforward to raise the maximum. | |
72dba584 TH |
328 | */ |
329 | ||
d37cacc5 MW |
330 | /* |
331 | * Developer's notes: | |
332 | * | |
f32f004c MW |
333 | * The IDA uses the functionality provided by the XArray to store bitmaps in |
334 | * each entry. The XA_FREE_MARK is only cleared when all bits in the bitmap | |
335 | * have been set. | |
d37cacc5 | 336 | * |
f32f004c MW |
337 | * I considered telling the XArray that each slot is an order-10 node |
338 | * and indexing by bit number, but the XArray can't allow a single multi-index | |
339 | * entry in the head, which would significantly increase memory consumption | |
340 | * for the IDA. So instead we divide the index by the number of bits in the | |
341 | * leaf bitmap before doing a radix tree lookup. | |
d37cacc5 MW |
342 | * |
343 | * As an optimisation, if there are only a few low bits set in any given | |
3159f943 | 344 | * leaf, instead of allocating a 128-byte bitmap, we store the bits |
f32f004c MW |
345 | * as a value entry. Value entries never have the XA_FREE_MARK cleared |
346 | * because we can always convert them into a bitmap entry. | |
347 | * | |
348 | * It would be possible to optimise further; once we've run out of a | |
349 | * single 128-byte bitmap, we currently switch to a 576-byte node, put | |
350 | * the 128-byte bitmap in the first entry and then start allocating extra | |
351 | * 128-byte entries. We could instead use the 512 bytes of the node's | |
352 | * data as a bitmap before moving to that scheme. I do not believe this | |
353 | * is a worthwhile optimisation; Rasmus Villemoes surveyed the current | |
354 | * users of the IDA and almost none of them use more than 1024 entries. | |
355 | * Those that do use more than the 8192 IDs that the 512 bytes would | |
356 | * provide. | |
357 | * | |
358 | * The IDA always uses a lock to alloc/free. If we add a 'test_bit' | |
d37cacc5 MW |
359 | * equivalent, it will still need locking. Going to RCU lookup would require |
360 | * using RCU to free bitmaps, and that's not trivial without embedding an | |
361 | * RCU head in the bitmap, which adds a 2-pointer overhead to each 128-byte | |
362 | * bitmap, which is excessive. | |
363 | */ | |
364 | ||
f32f004c MW |
365 | /** |
366 | * ida_alloc_range() - Allocate an unused ID. | |
367 | * @ida: IDA handle. | |
368 | * @min: Lowest ID to allocate. | |
369 | * @max: Highest ID to allocate. | |
370 | * @gfp: Memory allocation flags. | |
371 | * | |
372 | * Allocate an ID between @min and @max, inclusive. The allocated ID will | |
373 | * not exceed %INT_MAX, even if @max is larger. | |
374 | * | |
3b674261 SB |
375 | * Context: Any context. It is safe to call this function without |
376 | * locking in your code. | |
f32f004c MW |
377 | * Return: The allocated ID, or %-ENOMEM if memory could not be allocated, |
378 | * or %-ENOSPC if there are no free IDs. | |
379 | */ | |
380 | int ida_alloc_range(struct ida *ida, unsigned int min, unsigned int max, | |
381 | gfp_t gfp) | |
72dba584 | 382 | { |
f32f004c MW |
383 | XA_STATE(xas, &ida->xa, min / IDA_BITMAP_BITS); |
384 | unsigned bit = min % IDA_BITMAP_BITS; | |
385 | unsigned long flags; | |
386 | struct ida_bitmap *bitmap, *alloc = NULL; | |
387 | ||
388 | if ((int)min < 0) | |
389 | return -ENOSPC; | |
390 | ||
391 | if ((int)max < 0) | |
392 | max = INT_MAX; | |
393 | ||
394 | retry: | |
395 | xas_lock_irqsave(&xas, flags); | |
396 | next: | |
397 | bitmap = xas_find_marked(&xas, max / IDA_BITMAP_BITS, XA_FREE_MARK); | |
398 | if (xas.xa_index > min / IDA_BITMAP_BITS) | |
399 | bit = 0; | |
400 | if (xas.xa_index * IDA_BITMAP_BITS + bit > max) | |
401 | goto nospc; | |
402 | ||
403 | if (xa_is_value(bitmap)) { | |
404 | unsigned long tmp = xa_to_value(bitmap); | |
405 | ||
406 | if (bit < BITS_PER_XA_VALUE) { | |
407 | bit = find_next_zero_bit(&tmp, BITS_PER_XA_VALUE, bit); | |
408 | if (xas.xa_index * IDA_BITMAP_BITS + bit > max) | |
409 | goto nospc; | |
410 | if (bit < BITS_PER_XA_VALUE) { | |
411 | tmp |= 1UL << bit; | |
412 | xas_store(&xas, xa_mk_value(tmp)); | |
413 | goto out; | |
0a835c4f MW |
414 | } |
415 | } | |
f32f004c MW |
416 | bitmap = alloc; |
417 | if (!bitmap) | |
418 | bitmap = kzalloc(sizeof(*bitmap), GFP_NOWAIT); | |
419 | if (!bitmap) | |
420 | goto alloc; | |
421 | bitmap->bitmap[0] = tmp; | |
422 | xas_store(&xas, bitmap); | |
423 | if (xas_error(&xas)) { | |
424 | bitmap->bitmap[0] = 0; | |
425 | goto out; | |
d37cacc5 | 426 | } |
f32f004c | 427 | } |
d37cacc5 | 428 | |
f32f004c MW |
429 | if (bitmap) { |
430 | bit = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, bit); | |
431 | if (xas.xa_index * IDA_BITMAP_BITS + bit > max) | |
432 | goto nospc; | |
433 | if (bit == IDA_BITMAP_BITS) | |
434 | goto next; | |
72dba584 | 435 | |
f32f004c MW |
436 | __set_bit(bit, bitmap->bitmap); |
437 | if (bitmap_full(bitmap->bitmap, IDA_BITMAP_BITS)) | |
438 | xas_clear_mark(&xas, XA_FREE_MARK); | |
439 | } else { | |
440 | if (bit < BITS_PER_XA_VALUE) { | |
441 | bitmap = xa_mk_value(1UL << bit); | |
0a835c4f | 442 | } else { |
f32f004c MW |
443 | bitmap = alloc; |
444 | if (!bitmap) | |
445 | bitmap = kzalloc(sizeof(*bitmap), GFP_NOWAIT); | |
446 | if (!bitmap) | |
447 | goto alloc; | |
448 | __set_bit(bit, bitmap->bitmap); | |
0a835c4f | 449 | } |
f32f004c MW |
450 | xas_store(&xas, bitmap); |
451 | } | |
452 | out: | |
453 | xas_unlock_irqrestore(&xas, flags); | |
454 | if (xas_nomem(&xas, gfp)) { | |
455 | xas.xa_index = min / IDA_BITMAP_BITS; | |
456 | bit = min % IDA_BITMAP_BITS; | |
457 | goto retry; | |
72dba584 | 458 | } |
f32f004c MW |
459 | if (bitmap != alloc) |
460 | kfree(alloc); | |
461 | if (xas_error(&xas)) | |
462 | return xas_error(&xas); | |
463 | return xas.xa_index * IDA_BITMAP_BITS + bit; | |
464 | alloc: | |
465 | xas_unlock_irqrestore(&xas, flags); | |
466 | alloc = kzalloc(sizeof(*bitmap), gfp); | |
467 | if (!alloc) | |
468 | return -ENOMEM; | |
469 | xas_set(&xas, min / IDA_BITMAP_BITS); | |
470 | bit = min % IDA_BITMAP_BITS; | |
471 | goto retry; | |
472 | nospc: | |
473 | xas_unlock_irqrestore(&xas, flags); | |
a219b856 | 474 | kfree(alloc); |
f32f004c | 475 | return -ENOSPC; |
72dba584 | 476 | } |
f32f004c | 477 | EXPORT_SYMBOL(ida_alloc_range); |
72dba584 | 478 | |
f32f004c MW |
479 | /** |
480 | * ida_free() - Release an allocated ID. | |
481 | * @ida: IDA handle. | |
482 | * @id: Previously allocated ID. | |
483 | * | |
3b674261 SB |
484 | * Context: Any context. It is safe to call this function without |
485 | * locking in your code. | |
f32f004c MW |
486 | */ |
487 | void ida_free(struct ida *ida, unsigned int id) | |
72dba584 | 488 | { |
f32f004c MW |
489 | XA_STATE(xas, &ida->xa, id / IDA_BITMAP_BITS); |
490 | unsigned bit = id % IDA_BITMAP_BITS; | |
72dba584 | 491 | struct ida_bitmap *bitmap; |
f32f004c | 492 | unsigned long flags; |
72dba584 | 493 | |
fc82bbf4 LT |
494 | if ((int)id < 0) |
495 | return; | |
f32f004c MW |
496 | |
497 | xas_lock_irqsave(&xas, flags); | |
498 | bitmap = xas_load(&xas); | |
8f9f665a | 499 | |
3159f943 | 500 | if (xa_is_value(bitmap)) { |
f32f004c MW |
501 | unsigned long v = xa_to_value(bitmap); |
502 | if (bit >= BITS_PER_XA_VALUE) | |
503 | goto err; | |
504 | if (!(v & (1UL << bit))) | |
d37cacc5 | 505 | goto err; |
f32f004c MW |
506 | v &= ~(1UL << bit); |
507 | if (!v) | |
508 | goto delete; | |
509 | xas_store(&xas, xa_mk_value(v)); | |
d37cacc5 | 510 | } else { |
f32f004c MW |
511 | if (!test_bit(bit, bitmap->bitmap)) |
512 | goto err; | |
513 | __clear_bit(bit, bitmap->bitmap); | |
514 | xas_set_mark(&xas, XA_FREE_MARK); | |
515 | if (bitmap_empty(bitmap->bitmap, IDA_BITMAP_BITS)) { | |
516 | kfree(bitmap); | |
517 | delete: | |
518 | xas_store(&xas, NULL); | |
519 | } | |
72dba584 | 520 | } |
f32f004c | 521 | xas_unlock_irqrestore(&xas, flags); |
72dba584 | 522 | return; |
72dba584 | 523 | err: |
f32f004c | 524 | xas_unlock_irqrestore(&xas, flags); |
b03f8e43 | 525 | WARN(1, "ida_free called for id=%d which is not allocated.\n", id); |
72dba584 | 526 | } |
f32f004c | 527 | EXPORT_SYMBOL(ida_free); |
72dba584 TH |
528 | |
529 | /** | |
50d97d50 MW |
530 | * ida_destroy() - Free all IDs. |
531 | * @ida: IDA handle. | |
532 | * | |
533 | * Calling this function frees all IDs and releases all resources used | |
534 | * by an IDA. When this call returns, the IDA is empty and can be reused | |
535 | * or freed. If the IDA is already empty, there is no need to call this | |
536 | * function. | |
0a835c4f | 537 | * |
3b674261 SB |
538 | * Context: Any context. It is safe to call this function without |
539 | * locking in your code. | |
72dba584 TH |
540 | */ |
541 | void ida_destroy(struct ida *ida) | |
542 | { | |
f32f004c MW |
543 | XA_STATE(xas, &ida->xa, 0); |
544 | struct ida_bitmap *bitmap; | |
50d97d50 | 545 | unsigned long flags; |
0a835c4f | 546 | |
f32f004c MW |
547 | xas_lock_irqsave(&xas, flags); |
548 | xas_for_each(&xas, bitmap, ULONG_MAX) { | |
3159f943 | 549 | if (!xa_is_value(bitmap)) |
d37cacc5 | 550 | kfree(bitmap); |
f32f004c | 551 | xas_store(&xas, NULL); |
0a835c4f | 552 | } |
f32f004c | 553 | xas_unlock_irqrestore(&xas, flags); |
72dba584 TH |
554 | } |
555 | EXPORT_SYMBOL(ida_destroy); | |
556 | ||
f32f004c MW |
557 | #ifndef __KERNEL__ |
558 | extern void xa_dump_index(unsigned long index, unsigned int shift); | |
559 | #define IDA_CHUNK_SHIFT ilog2(IDA_BITMAP_BITS) | |
88eca020 | 560 | |
f32f004c MW |
561 | static void ida_dump_entry(void *entry, unsigned long index) |
562 | { | |
563 | unsigned long i; | |
564 | ||
565 | if (!entry) | |
566 | return; | |
567 | ||
568 | if (xa_is_node(entry)) { | |
569 | struct xa_node *node = xa_to_node(entry); | |
570 | unsigned int shift = node->shift + IDA_CHUNK_SHIFT + | |
571 | XA_CHUNK_SHIFT; | |
572 | ||
573 | xa_dump_index(index * IDA_BITMAP_BITS, shift); | |
574 | xa_dump_node(node); | |
575 | for (i = 0; i < XA_CHUNK_SIZE; i++) | |
576 | ida_dump_entry(node->slots[i], | |
577 | index | (i << node->shift)); | |
578 | } else if (xa_is_value(entry)) { | |
579 | xa_dump_index(index * IDA_BITMAP_BITS, ilog2(BITS_PER_LONG)); | |
580 | pr_cont("value: data %lx [%px]\n", xa_to_value(entry), entry); | |
581 | } else { | |
582 | struct ida_bitmap *bitmap = entry; | |
88eca020 | 583 | |
f32f004c MW |
584 | xa_dump_index(index * IDA_BITMAP_BITS, IDA_CHUNK_SHIFT); |
585 | pr_cont("bitmap: %p data", bitmap); | |
586 | for (i = 0; i < IDA_BITMAP_LONGS; i++) | |
587 | pr_cont(" %lx", bitmap->bitmap[i]); | |
588 | pr_cont("\n"); | |
5ade60dd | 589 | } |
88eca020 | 590 | } |
88eca020 | 591 | |
f32f004c | 592 | static void ida_dump(struct ida *ida) |
88eca020 | 593 | { |
f32f004c MW |
594 | struct xarray *xa = &ida->xa; |
595 | pr_debug("ida: %p node %p free %d\n", ida, xa->xa_head, | |
596 | xa->xa_flags >> ROOT_TAG_SHIFT); | |
597 | ida_dump_entry(xa->xa_head, 0); | |
88eca020 | 598 | } |
f32f004c | 599 | #endif |