blob: 577bfd4fe5c2dbc52d3c080088734f98ed763109 [file] [log] [blame]
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001#include <linux/bitmap.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05002#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/idr.h>
Matthew Wilcox0a835c42016-12-20 10:27:56 -05004#include <linux/slab.h>
Rusty Russell88eca022011-08-03 16:21:06 -07005#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05007DEFINE_PER_CPU(struct ida_bitmap *, ida_bitmap);
Rusty Russell88eca022011-08-03 16:21:06 -07008static DEFINE_SPINLOCK(simple_ida_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Chris Mi388f79f2017-08-30 02:31:57 -040010int idr_alloc_cmn(struct idr *idr, void *ptr, unsigned long *index,
11 unsigned long start, unsigned long end, gfp_t gfp,
12 bool ext)
Tejun Heod5c74092013-02-27 17:03:55 -080013{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050014 struct radix_tree_iter iter;
Chris Mi388f79f2017-08-30 02:31:57 -040015 void __rcu **slot;
Tejun Heod5c74092013-02-27 17:03:55 -080016
Matthew Wilcox0a835c42016-12-20 10:27:56 -050017 if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr)))
18 return -EINVAL;
Tejun Heod5c74092013-02-27 17:03:55 -080019
Matthew Wilcox0a835c42016-12-20 10:27:56 -050020 radix_tree_iter_init(&iter, start);
Chris Mi388f79f2017-08-30 02:31:57 -040021 if (ext)
22 slot = idr_get_free_ext(&idr->idr_rt, &iter, gfp, end);
23 else
24 slot = idr_get_free(&idr->idr_rt, &iter, gfp, end);
Matthew Wilcox0a835c42016-12-20 10:27:56 -050025 if (IS_ERR(slot))
26 return PTR_ERR(slot);
Tejun Heod5c74092013-02-27 17:03:55 -080027
Matthew Wilcox0a835c42016-12-20 10:27:56 -050028 radix_tree_iter_replace(&idr->idr_rt, &iter, slot, ptr);
29 radix_tree_iter_tag_clear(&idr->idr_rt, &iter, IDR_FREE);
Chris Mi388f79f2017-08-30 02:31:57 -040030
31 if (index)
32 *index = iter.index;
33 return 0;
Tejun Heod5c74092013-02-27 17:03:55 -080034}
Chris Mi388f79f2017-08-30 02:31:57 -040035EXPORT_SYMBOL_GPL(idr_alloc_cmn);
Tejun Heod5c74092013-02-27 17:03:55 -080036
Jeff Layton3e6628c42013-04-29 16:21:16 -070037/**
38 * idr_alloc_cyclic - allocate new idr entry in a cyclical fashion
Matthew Wilcox0a835c42016-12-20 10:27:56 -050039 * @idr: idr handle
Jeff Layton3e6628c42013-04-29 16:21:16 -070040 * @ptr: pointer to be associated with the new id
41 * @start: the minimum id (inclusive)
Matthew Wilcox0a835c42016-12-20 10:27:56 -050042 * @end: the maximum id (exclusive)
43 * @gfp: memory allocation flags
Jeff Layton3e6628c42013-04-29 16:21:16 -070044 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050045 * 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
47 * equal to @start.
Jeff Layton3e6628c42013-04-29 16:21:16 -070048 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050049int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
Jeff Layton3e6628c42013-04-29 16:21:16 -070050{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050051 int id, curr = idr->idr_next;
Jeff Layton3e6628c42013-04-29 16:21:16 -070052
Matthew Wilcox0a835c42016-12-20 10:27:56 -050053 if (curr < start)
54 curr = start;
Jeff Layton3e6628c42013-04-29 16:21:16 -070055
Matthew Wilcox0a835c42016-12-20 10:27:56 -050056 id = idr_alloc(idr, ptr, curr, end, gfp);
57 if ((id == -ENOSPC) && (curr > start))
58 id = idr_alloc(idr, ptr, start, curr, gfp);
59
60 if (id >= 0)
61 idr->idr_next = id + 1U;
62
Jeff Layton3e6628c42013-04-29 16:21:16 -070063 return id;
64}
65EXPORT_SYMBOL(idr_alloc_cyclic);
66
Jeff Mahoney5806f072006-06-26 00:27:19 -070067/**
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070068 * idr_for_each - iterate through all stored pointers
Matthew Wilcox0a835c42016-12-20 10:27:56 -050069 * @idr: idr handle
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070070 * @fn: function to be called for each pointer
Matthew Wilcox0a835c42016-12-20 10:27:56 -050071 * @data: data passed to callback function
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070072 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050073 * 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.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070075 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050076 * If @fn returns anything other than %0, the iteration stops and that
77 * value is returned from this function.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070078 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050079 * 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.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070083 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050084int idr_for_each(const struct idr *idr,
85 int (*fn)(int id, void *p, void *data), void *data)
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070086{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050087 struct radix_tree_iter iter;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -050088 void __rcu **slot;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070089
Matthew Wilcox0a835c42016-12-20 10:27:56 -050090 radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, 0) {
91 int ret = fn(iter.index, rcu_dereference_raw(*slot), data);
92 if (ret)
93 return ret;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070094 }
95
Matthew Wilcox0a835c42016-12-20 10:27:56 -050096 return 0;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070097}
98EXPORT_SYMBOL(idr_for_each);
99
100/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500101 * idr_get_next - Find next populated entry
102 * @idr: idr handle
103 * @nextid: Pointer to lowest possible ID to return
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700104 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500105 * 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.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700109 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500110void *idr_get_next(struct idr *idr, int *nextid)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700111{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500112 struct radix_tree_iter iter;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -0500113 void __rcu **slot;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700114
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500115 slot = radix_tree_iter_find(&idr->idr_rt, &iter, *nextid);
116 if (!slot)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700117 return NULL;
118
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500119 *nextid = iter.index;
120 return rcu_dereference_raw(*slot);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700121}
Ben Hutchings4d1ee802010-01-29 20:59:17 +0000122EXPORT_SYMBOL(idr_get_next);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700123
Chris Mi388f79f2017-08-30 02:31:57 -0400124void *idr_get_next_ext(struct idr *idr, unsigned long *nextid)
125{
126 struct radix_tree_iter iter;
127 void __rcu **slot;
128
129 slot = radix_tree_iter_find(&idr->idr_rt, &iter, *nextid);
130 if (!slot)
131 return NULL;
132
133 *nextid = iter.index;
134 return rcu_dereference_raw(*slot);
135}
136EXPORT_SYMBOL(idr_get_next_ext);
137
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700138/**
Jeff Mahoney5806f072006-06-26 00:27:19 -0700139 * idr_replace - replace pointer for given id
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500140 * @idr: idr handle
141 * @ptr: New pointer to associate with the ID
142 * @id: Lookup key
Jeff Mahoney5806f072006-06-26 00:27:19 -0700143 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500144 * 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!).
Jeff Mahoney5806f072006-06-26 00:27:19 -0700148 *
Eric Biggersa70e43a2017-10-03 16:16:13 -0700149 * Returns: the old value on success. %-ENOENT indicates that @id was not
Matthew Wilcox234a4622017-11-28 09:56:36 -0500150 * found. %-EINVAL indicates that @ptr was not valid.
Jeff Mahoney5806f072006-06-26 00:27:19 -0700151 */
Matthew Wilcox234a4622017-11-28 09:56:36 -0500152void *idr_replace(struct idr *idr, void *ptr, unsigned long id)
Chris Mi388f79f2017-08-30 02:31:57 -0400153{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500154 struct radix_tree_node *node;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -0500155 void __rcu **slot = NULL;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500156 void *entry;
Jeff Mahoney5806f072006-06-26 00:27:19 -0700157
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500158 if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr)))
Tejun Heoe8c8d1b2013-02-27 17:05:04 -0800159 return ERR_PTR(-EINVAL);
160
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500161 entry = __radix_tree_lookup(&idr->idr_rt, id, &node, &slot);
162 if (!slot || radix_tree_tag_get(&idr->idr_rt, id, IDR_FREE))
Lai Jiangshanb93804b2014-06-06 14:37:13 -0700163 return ERR_PTR(-ENOENT);
Manfred Spraul6ff2d392008-12-01 13:14:02 -0800164
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800165 __radix_tree_replace(&idr->idr_rt, node, slot, ptr, NULL);
Jeff Mahoney5806f072006-06-26 00:27:19 -0700166
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500167 return entry;
Jeff Mahoney5806f072006-06-26 00:27:19 -0700168}
Matthew Wilcox234a4622017-11-28 09:56:36 -0500169EXPORT_SYMBOL(idr_replace);
Jeff Mahoney5806f072006-06-26 00:27:19 -0700170
Randy Dunlap56083ab2010-10-26 14:19:08 -0700171/**
172 * DOC: IDA description
Tejun Heo72dba582007-06-14 03:45:13 +0900173 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500174 * 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().
Tejun Heo72dba582007-06-14 03:45:13 +0900180 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500181 * 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
185 * same IDA.
186 *
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.
191 *
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.
Tejun Heo72dba582007-06-14 03:45:13 +0900194 */
195
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500196/*
197 * Developer's notes:
198 *
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.
202 *
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.
208 *
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.
215 *
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
220 * count.
221 *
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.
227 */
228
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500229#define IDA_MAX (0x80000000U / IDA_BITMAP_BITS)
230
Tejun Heo72dba582007-06-14 03:45:13 +0900231/**
232 * ida_get_new_above - allocate new ID above or equal to a start id
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500233 * @ida: ida handle
234 * @start: id to start search at
235 * @id: pointer to the allocated handle
Tejun Heo72dba582007-06-14 03:45:13 +0900236 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500237 * 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
241 * requirements.
Tejun Heo72dba582007-06-14 03:45:13 +0900242 *
Randy Dunlap56083ab2010-10-26 14:19:08 -0700243 * If memory is required, it will return %-EAGAIN, you should unlock
Tejun Heo72dba582007-06-14 03:45:13 +0900244 * and go back to the ida_pre_get() call. If the ida is full, it will
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500245 * return %-ENOSPC. On success, it will return 0.
Tejun Heo72dba582007-06-14 03:45:13 +0900246 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500247 * @id returns a value in the range @start ... %0x7fffffff.
Tejun Heo72dba582007-06-14 03:45:13 +0900248 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500249int ida_get_new_above(struct ida *ida, int start, int *id)
Tejun Heo72dba582007-06-14 03:45:13 +0900250{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500251 struct radix_tree_root *root = &ida->ida_rt;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -0500252 void __rcu **slot;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500253 struct radix_tree_iter iter;
Tejun Heo72dba582007-06-14 03:45:13 +0900254 struct ida_bitmap *bitmap;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500255 unsigned long index;
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500256 unsigned bit, ebit;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500257 int new;
Tejun Heo72dba582007-06-14 03:45:13 +0900258
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500259 index = start / IDA_BITMAP_BITS;
260 bit = start % IDA_BITMAP_BITS;
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500261 ebit = bit + RADIX_TREE_EXCEPTIONAL_SHIFT;
Tejun Heo72dba582007-06-14 03:45:13 +0900262
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500263 slot = radix_tree_iter_init(&iter, index);
264 for (;;) {
265 if (slot)
266 slot = radix_tree_next_slot(slot, &iter,
267 RADIX_TREE_ITER_TAGGED);
268 if (!slot) {
269 slot = idr_get_free(root, &iter, GFP_NOWAIT, IDA_MAX);
270 if (IS_ERR(slot)) {
271 if (slot == ERR_PTR(-ENOMEM))
272 return -EAGAIN;
273 return PTR_ERR(slot);
274 }
275 }
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500276 if (iter.index > index) {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500277 bit = 0;
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500278 ebit = RADIX_TREE_EXCEPTIONAL_SHIFT;
279 }
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500280 new = iter.index * IDA_BITMAP_BITS;
281 bitmap = rcu_dereference_raw(*slot);
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500282 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) {
286 tmp |= 1UL << ebit;
287 rcu_assign_pointer(*slot, (void *)tmp);
288 *id = new + ebit - RADIX_TREE_EXCEPTIONAL_SHIFT;
289 return 0;
290 }
291 bitmap = this_cpu_xchg(ida_bitmap, NULL);
292 if (!bitmap)
293 return -EAGAIN;
294 memset(bitmap, 0, sizeof(*bitmap));
295 bitmap->bitmap[0] = tmp >> RADIX_TREE_EXCEPTIONAL_SHIFT;
296 rcu_assign_pointer(*slot, bitmap);
297 }
298
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500299 if (bitmap) {
300 bit = find_next_zero_bit(bitmap->bitmap,
301 IDA_BITMAP_BITS, bit);
302 new += bit;
303 if (new < 0)
304 return -ENOSPC;
305 if (bit == IDA_BITMAP_BITS)
306 continue;
Tejun Heo72dba582007-06-14 03:45:13 +0900307
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500308 __set_bit(bit, bitmap->bitmap);
309 if (bitmap_full(bitmap->bitmap, IDA_BITMAP_BITS))
310 radix_tree_iter_tag_clear(root, &iter,
311 IDR_FREE);
312 } else {
313 new += bit;
314 if (new < 0)
315 return -ENOSPC;
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500316 if (ebit < BITS_PER_LONG) {
317 bitmap = (void *)((1UL << ebit) |
318 RADIX_TREE_EXCEPTIONAL_ENTRY);
319 radix_tree_iter_replace(root, &iter, slot,
320 bitmap);
321 *id = new;
322 return 0;
323 }
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -0500324 bitmap = this_cpu_xchg(ida_bitmap, NULL);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500325 if (!bitmap)
326 return -EAGAIN;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500327 memset(bitmap, 0, sizeof(*bitmap));
328 __set_bit(bit, bitmap->bitmap);
329 radix_tree_iter_replace(root, &iter, slot, bitmap);
330 }
Tejun Heo72dba582007-06-14 03:45:13 +0900331
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500332 *id = new;
333 return 0;
Tejun Heo72dba582007-06-14 03:45:13 +0900334 }
Tejun Heo72dba582007-06-14 03:45:13 +0900335}
336EXPORT_SYMBOL(ida_get_new_above);
337
338/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500339 * ida_remove - Free the given ID
340 * @ida: ida handle
341 * @id: ID to free
342 *
343 * This function should not be called at the same time as ida_get_new_above().
Tejun Heo72dba582007-06-14 03:45:13 +0900344 */
345void ida_remove(struct ida *ida, int id)
346{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500347 unsigned long index = id / IDA_BITMAP_BITS;
348 unsigned offset = id % IDA_BITMAP_BITS;
Tejun Heo72dba582007-06-14 03:45:13 +0900349 struct ida_bitmap *bitmap;
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500350 unsigned long *btmp;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500351 struct radix_tree_iter iter;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -0500352 void __rcu **slot;
Tejun Heo72dba582007-06-14 03:45:13 +0900353
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500354 slot = radix_tree_iter_lookup(&ida->ida_rt, &iter, index);
355 if (!slot)
Lai Jiangshan8f9f6652014-06-06 14:37:11 -0700356 goto err;
357
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500358 bitmap = rcu_dereference_raw(*slot);
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500359 if (radix_tree_exception(bitmap)) {
360 btmp = (unsigned long *)slot;
361 offset += RADIX_TREE_EXCEPTIONAL_SHIFT;
362 if (offset >= BITS_PER_LONG)
363 goto err;
364 } else {
365 btmp = bitmap->bitmap;
366 }
367 if (!test_bit(offset, btmp))
Tejun Heo72dba582007-06-14 03:45:13 +0900368 goto err;
369
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500370 __clear_bit(offset, btmp);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500371 radix_tree_iter_tag_set(&ida->ida_rt, &iter, IDR_FREE);
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500372 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)) {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500377 kfree(bitmap);
378 radix_tree_iter_delete(&ida->ida_rt, &iter, slot);
Tejun Heo72dba582007-06-14 03:45:13 +0900379 }
Tejun Heo72dba582007-06-14 03:45:13 +0900380 return;
Tejun Heo72dba582007-06-14 03:45:13 +0900381 err:
Jean Delvaredd04b452013-07-03 15:08:47 -0700382 WARN(1, "ida_remove called for id=%d which is not allocated.\n", id);
Tejun Heo72dba582007-06-14 03:45:13 +0900383}
384EXPORT_SYMBOL(ida_remove);
385
386/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500387 * ida_destroy - Free the contents of an ida
388 * @ida: ida handle
389 *
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
393 * same time.
Tejun Heo72dba582007-06-14 03:45:13 +0900394 */
395void ida_destroy(struct ida *ida)
396{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500397 struct radix_tree_iter iter;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -0500398 void __rcu **slot;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500399
400 radix_tree_for_each_slot(slot, &ida->ida_rt, &iter, 0) {
401 struct ida_bitmap *bitmap = rcu_dereference_raw(*slot);
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500402 if (!radix_tree_exception(bitmap))
403 kfree(bitmap);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500404 radix_tree_iter_delete(&ida->ida_rt, &iter, slot);
405 }
Tejun Heo72dba582007-06-14 03:45:13 +0900406}
407EXPORT_SYMBOL(ida_destroy);
408
409/**
Rusty Russell88eca022011-08-03 16:21:06 -0700410 * 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
415 *
416 * Allocates an id in the range start <= id < end, or returns -ENOSPC.
417 * On memory allocation failure, returns -ENOMEM.
418 *
Daniel Vettera2ef9472016-12-12 16:46:20 -0800419 * Compared to ida_get_new_above() this function does its own locking, and
420 * should be used unless there are special requirements.
421 *
Rusty Russell88eca022011-08-03 16:21:06 -0700422 * Use ida_simple_remove() to get rid of an id.
423 */
424int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
425 gfp_t gfp_mask)
426{
427 int ret, id;
428 unsigned int max;
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700429 unsigned long flags;
Rusty Russell88eca022011-08-03 16:21:06 -0700430
431 BUG_ON((int)start < 0);
432 BUG_ON((int)end < 0);
433
434 if (end == 0)
435 max = 0x80000000;
436 else {
437 BUG_ON(end < start);
438 max = end - 1;
439 }
440
441again:
442 if (!ida_pre_get(ida, gfp_mask))
443 return -ENOMEM;
444
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700445 spin_lock_irqsave(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700446 ret = ida_get_new_above(ida, start, &id);
447 if (!ret) {
448 if (id > max) {
449 ida_remove(ida, id);
450 ret = -ENOSPC;
451 } else {
452 ret = id;
453 }
454 }
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700455 spin_unlock_irqrestore(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700456
457 if (unlikely(ret == -EAGAIN))
458 goto again;
459
460 return ret;
461}
462EXPORT_SYMBOL(ida_simple_get);
463
464/**
465 * ida_simple_remove - remove an allocated id.
466 * @ida: the (initialized) ida.
467 * @id: the id returned by ida_simple_get.
Daniel Vettera2ef9472016-12-12 16:46:20 -0800468 *
469 * Use to release an id allocated with ida_simple_get().
470 *
471 * Compared to ida_remove() this function does its own locking, and should be
472 * used unless there are special requirements.
Rusty Russell88eca022011-08-03 16:21:06 -0700473 */
474void ida_simple_remove(struct ida *ida, unsigned int id)
475{
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700476 unsigned long flags;
477
Rusty Russell88eca022011-08-03 16:21:06 -0700478 BUG_ON((int)id < 0);
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700479 spin_lock_irqsave(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700480 ida_remove(ida, id);
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700481 spin_unlock_irqrestore(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700482}
483EXPORT_SYMBOL(ida_simple_remove);