1 #include <linux/bitmap.h>
2 #include <linux/export.h>
4 #include <linux/slab.h>
5 #include <linux/spinlock.h>
7 DEFINE_PER_CPU(struct ida_bitmap *, ida_bitmap);
8 static DEFINE_SPINLOCK(simple_ida_lock);
10 int idr_alloc_cmn(struct idr *idr, void *ptr, unsigned long *index,
11 unsigned long start, unsigned long end, gfp_t gfp,
14 struct radix_tree_iter iter;
17 if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr)))
20 radix_tree_iter_init(&iter, start);
22 slot = idr_get_free_ext(&idr->idr_rt, &iter, gfp, end);
24 slot = idr_get_free(&idr->idr_rt, &iter, gfp, end);
28 radix_tree_iter_replace(&idr->idr_rt, &iter, slot, ptr);
29 radix_tree_iter_tag_clear(&idr->idr_rt, &iter, IDR_FREE);
35 EXPORT_SYMBOL_GPL(idr_alloc_cmn);
38 * idr_alloc_cyclic - allocate new idr entry in a cyclical fashion
40 * @ptr: pointer to be associated with the new id
41 * @start: the minimum id (inclusive)
42 * @end: the maximum id (exclusive)
43 * @gfp: memory allocation flags
45 * Allocates an ID larger than the last ID allocated if one is available.
46 * If not, it will attempt to allocate the smallest ID that is larger or
49 int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
51 int id, curr = idr->idr_next;
56 id = idr_alloc(idr, ptr, curr, end, gfp);
57 if ((id == -ENOSPC) && (curr > start))
58 id = idr_alloc(idr, ptr, start, curr, gfp);
61 idr->idr_next = id + 1U;
65 EXPORT_SYMBOL(idr_alloc_cyclic);
68 * idr_for_each - iterate through all stored pointers
70 * @fn: function to be called for each pointer
71 * @data: data passed to callback function
73 * The callback function will be called for each entry in @idr, passing
74 * the id, the pointer and the data pointer passed to this function.
76 * If @fn returns anything other than %0, the iteration stops and that
77 * value is returned from this function.
79 * idr_for_each() can be called concurrently with idr_alloc() and
80 * idr_remove() if protected by RCU. Newly added entries may not be
81 * seen and deleted entries may be seen, but adding and removing entries
82 * will not cause other entries to be skipped, nor spurious ones to be seen.
84 int idr_for_each(const struct idr *idr,
85 int (*fn)(int id, void *p, void *data), void *data)
87 struct radix_tree_iter iter;
90 radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, 0) {
91 int ret = fn(iter.index, rcu_dereference_raw(*slot), data);
98 EXPORT_SYMBOL(idr_for_each);
101 * idr_get_next - Find next populated entry
103 * @nextid: Pointer to lowest possible ID to return
105 * Returns the next populated entry in the tree with an ID greater than
106 * or equal to the value pointed to by @nextid. On exit, @nextid is updated
107 * to the ID of the found value. To use in a loop, the value pointed to by
108 * nextid must be incremented by the user.
110 void *idr_get_next(struct idr *idr, int *nextid)
112 struct radix_tree_iter iter;
115 slot = radix_tree_iter_find(&idr->idr_rt, &iter, *nextid);
119 *nextid = iter.index;
120 return rcu_dereference_raw(*slot);
122 EXPORT_SYMBOL(idr_get_next);
124 void *idr_get_next_ext(struct idr *idr, unsigned long *nextid)
126 struct radix_tree_iter iter;
129 slot = radix_tree_iter_find(&idr->idr_rt, &iter, *nextid);
133 *nextid = iter.index;
134 return rcu_dereference_raw(*slot);
136 EXPORT_SYMBOL(idr_get_next_ext);
139 * idr_replace - replace pointer for given id
141 * @ptr: New pointer to associate with the ID
144 * Replace the pointer registered with an ID and return the old value.
145 * This function can be called under the RCU read lock concurrently with
146 * idr_alloc() and idr_remove() (as long as the ID being removed is not
147 * the one being replaced!).
149 * Returns: the old value on success. %-ENOENT indicates that @id was not
150 * found. %-EINVAL indicates that @ptr was not valid.
152 void *idr_replace(struct idr *idr, void *ptr, unsigned long id)
154 struct radix_tree_node *node;
155 void __rcu **slot = NULL;
158 if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr)))
159 return ERR_PTR(-EINVAL);
161 entry = __radix_tree_lookup(&idr->idr_rt, id, &node, &slot);
162 if (!slot || radix_tree_tag_get(&idr->idr_rt, id, IDR_FREE))
163 return ERR_PTR(-ENOENT);
165 __radix_tree_replace(&idr->idr_rt, node, slot, ptr, NULL);
169 EXPORT_SYMBOL(idr_replace);
172 * DOC: IDA description
174 * The IDA is an ID allocator which does not provide the ability to
175 * associate an ID with a pointer. As such, it only needs to store one
176 * bit per ID, and so is more space efficient than an IDR. To use an IDA,
177 * define it using DEFINE_IDA() (or embed a &struct ida in a data structure,
178 * then initialise it using ida_init()). To allocate a new ID, call
179 * ida_simple_get(). To free an ID, call ida_simple_remove().
181 * If you have more complex locking requirements, use a loop around
182 * ida_pre_get() and ida_get_new() to allocate a new ID. Then use
183 * ida_remove() to free an ID. You must make sure that ida_get_new() and
184 * ida_remove() cannot be called at the same time as each other for the
187 * You can also use ida_get_new_above() if you need an ID to be allocated
188 * above a particular number. ida_destroy() can be used to dispose of an
189 * IDA without needing to free the individual IDs in it. You can use
190 * ida_is_empty() to find out whether the IDA has any IDs currently allocated.
192 * IDs are currently limited to the range [0-INT_MAX]. If this is an awkward
193 * limitation, it should be quite straightforward to raise the maximum.
199 * The IDA uses the functionality provided by the IDR & radix tree to store
200 * bitmaps in each entry. The IDR_FREE tag means there is at least one bit
201 * free, unlike the IDR where it means at least one entry is free.
203 * I considered telling the radix tree that each slot is an order-10 node
204 * and storing the bit numbers in the radix tree, but the radix tree can't
205 * allow a single multiorder entry at index 0, which would significantly
206 * increase memory consumption for the IDA. So instead we divide the index
207 * by the number of bits in the leaf bitmap before doing a radix tree lookup.
209 * As an optimisation, if there are only a few low bits set in any given
210 * leaf, instead of allocating a 128-byte bitmap, we use the 'exceptional
211 * entry' functionality of the radix tree to store BITS_PER_LONG - 2 bits
212 * directly in the entry. By being really tricksy, we could store
213 * BITS_PER_LONG - 1 bits, but there're diminishing returns after optimising
214 * for 0-3 allocated IDs.
216 * We allow the radix tree 'exceptional' count to get out of date. Nothing
217 * in the IDA nor the radix tree code checks it. If it becomes important
218 * to maintain an accurate exceptional count, switch the rcu_assign_pointer()
219 * calls to radix_tree_iter_replace() which will correct the exceptional
222 * The IDA always requires a lock to alloc/free. If we add a 'test_bit'
223 * equivalent, it will still need locking. Going to RCU lookup would require
224 * using RCU to free bitmaps, and that's not trivial without embedding an
225 * RCU head in the bitmap, which adds a 2-pointer overhead to each 128-byte
226 * bitmap, which is excessive.
229 #define IDA_MAX (0x80000000U / IDA_BITMAP_BITS)
232 * ida_get_new_above - allocate new ID above or equal to a start id
234 * @start: id to start search at
235 * @id: pointer to the allocated handle
237 * Allocate new ID above or equal to @start. It should be called
238 * with any required locks to ensure that concurrent calls to
239 * ida_get_new_above() / ida_get_new() / ida_remove() are not allowed.
240 * Consider using ida_simple_get() if you do not have complex locking
243 * If memory is required, it will return %-EAGAIN, you should unlock
244 * and go back to the ida_pre_get() call. If the ida is full, it will
245 * return %-ENOSPC. On success, it will return 0.
247 * @id returns a value in the range @start ... %0x7fffffff.
249 int ida_get_new_above(struct ida *ida, int start, int *id)
251 struct radix_tree_root *root = &ida->ida_rt;
253 struct radix_tree_iter iter;
254 struct ida_bitmap *bitmap;
259 index = start / IDA_BITMAP_BITS;
260 bit = start % IDA_BITMAP_BITS;
261 ebit = bit + RADIX_TREE_EXCEPTIONAL_SHIFT;
263 slot = radix_tree_iter_init(&iter, index);
266 slot = radix_tree_next_slot(slot, &iter,
267 RADIX_TREE_ITER_TAGGED);
269 slot = idr_get_free(root, &iter, GFP_NOWAIT, IDA_MAX);
271 if (slot == ERR_PTR(-ENOMEM))
273 return PTR_ERR(slot);
276 if (iter.index > index) {
278 ebit = RADIX_TREE_EXCEPTIONAL_SHIFT;
280 new = iter.index * IDA_BITMAP_BITS;
281 bitmap = rcu_dereference_raw(*slot);
282 if (radix_tree_exception(bitmap)) {
283 unsigned long tmp = (unsigned long)bitmap;
284 ebit = find_next_zero_bit(&tmp, BITS_PER_LONG, ebit);
285 if (ebit < BITS_PER_LONG) {
287 rcu_assign_pointer(*slot, (void *)tmp);
288 *id = new + ebit - RADIX_TREE_EXCEPTIONAL_SHIFT;
291 bitmap = this_cpu_xchg(ida_bitmap, NULL);
294 memset(bitmap, 0, sizeof(*bitmap));
295 bitmap->bitmap[0] = tmp >> RADIX_TREE_EXCEPTIONAL_SHIFT;
296 rcu_assign_pointer(*slot, bitmap);
300 bit = find_next_zero_bit(bitmap->bitmap,
301 IDA_BITMAP_BITS, bit);
305 if (bit == IDA_BITMAP_BITS)
308 __set_bit(bit, bitmap->bitmap);
309 if (bitmap_full(bitmap->bitmap, IDA_BITMAP_BITS))
310 radix_tree_iter_tag_clear(root, &iter,
316 if (ebit < BITS_PER_LONG) {
317 bitmap = (void *)((1UL << ebit) |
318 RADIX_TREE_EXCEPTIONAL_ENTRY);
319 radix_tree_iter_replace(root, &iter, slot,
324 bitmap = this_cpu_xchg(ida_bitmap, NULL);
327 memset(bitmap, 0, sizeof(*bitmap));
328 __set_bit(bit, bitmap->bitmap);
329 radix_tree_iter_replace(root, &iter, slot, bitmap);
336 EXPORT_SYMBOL(ida_get_new_above);
339 * ida_remove - Free the given ID
343 * This function should not be called at the same time as ida_get_new_above().
345 void ida_remove(struct ida *ida, int id)
347 unsigned long index = id / IDA_BITMAP_BITS;
348 unsigned offset = id % IDA_BITMAP_BITS;
349 struct ida_bitmap *bitmap;
351 struct radix_tree_iter iter;
354 slot = radix_tree_iter_lookup(&ida->ida_rt, &iter, index);
358 bitmap = rcu_dereference_raw(*slot);
359 if (radix_tree_exception(bitmap)) {
360 btmp = (unsigned long *)slot;
361 offset += RADIX_TREE_EXCEPTIONAL_SHIFT;
362 if (offset >= BITS_PER_LONG)
365 btmp = bitmap->bitmap;
367 if (!test_bit(offset, btmp))
370 __clear_bit(offset, btmp);
371 radix_tree_iter_tag_set(&ida->ida_rt, &iter, IDR_FREE);
372 if (radix_tree_exception(bitmap)) {
373 if (rcu_dereference_raw(*slot) ==
374 (void *)RADIX_TREE_EXCEPTIONAL_ENTRY)
375 radix_tree_iter_delete(&ida->ida_rt, &iter, slot);
376 } else if (bitmap_empty(btmp, IDA_BITMAP_BITS)) {
378 radix_tree_iter_delete(&ida->ida_rt, &iter, slot);
382 WARN(1, "ida_remove called for id=%d which is not allocated.\n", id);
384 EXPORT_SYMBOL(ida_remove);
387 * ida_destroy - Free the contents of an ida
390 * Calling this function releases all resources associated with an IDA. When
391 * this call returns, the IDA is empty and can be reused or freed. The caller
392 * should not allow ida_remove() or ida_get_new_above() to be called at the
395 void ida_destroy(struct ida *ida)
397 struct radix_tree_iter iter;
400 radix_tree_for_each_slot(slot, &ida->ida_rt, &iter, 0) {
401 struct ida_bitmap *bitmap = rcu_dereference_raw(*slot);
402 if (!radix_tree_exception(bitmap))
404 radix_tree_iter_delete(&ida->ida_rt, &iter, slot);
407 EXPORT_SYMBOL(ida_destroy);
410 * ida_simple_get - get a new id.
411 * @ida: the (initialized) ida.
412 * @start: the minimum id (inclusive, < 0x8000000)
413 * @end: the maximum id (exclusive, < 0x8000000 or 0)
414 * @gfp_mask: memory allocation flags
416 * Allocates an id in the range start <= id < end, or returns -ENOSPC.
417 * On memory allocation failure, returns -ENOMEM.
419 * Compared to ida_get_new_above() this function does its own locking, and
420 * should be used unless there are special requirements.
422 * Use ida_simple_remove() to get rid of an id.
424 int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
431 BUG_ON((int)start < 0);
432 BUG_ON((int)end < 0);
442 if (!ida_pre_get(ida, gfp_mask))
445 spin_lock_irqsave(&simple_ida_lock, flags);
446 ret = ida_get_new_above(ida, start, &id);
455 spin_unlock_irqrestore(&simple_ida_lock, flags);
457 if (unlikely(ret == -EAGAIN))
462 EXPORT_SYMBOL(ida_simple_get);
465 * ida_simple_remove - remove an allocated id.
466 * @ida: the (initialized) ida.
467 * @id: the id returned by ida_simple_get.
469 * Use to release an id allocated with ida_simple_get().
471 * Compared to ida_remove() this function does its own locking, and should be
472 * used unless there are special requirements.
474 void ida_simple_remove(struct ida *ida, unsigned int id)
479 spin_lock_irqsave(&simple_ida_lock, flags);
481 spin_unlock_irqrestore(&simple_ida_lock, flags);
483 EXPORT_SYMBOL(ida_simple_remove);