blob: 6d4ec2c2982b661d92fd5c27ec3a2ba0407ab029 [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
Matthew Wilcoxe096f6a2017-11-28 10:14:27 -050010/**
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()
20 * is exclusive.
21 *
22 * The caller should provide their own locking to ensure that two
23 * concurrent modifications to the IDR are not possible. Read-only
24 * accesses to the IDR may be done under the RCU read lock or may
25 * exclude simultaneous writers.
26 *
27 * Return: 0 if an ID was allocated, -ENOMEM if memory allocation failed,
28 * or -ENOSPC if no free IDs could be found. If an error occurred,
29 * @nextid is unchanged.
30 */
31int idr_alloc_u32(struct idr *idr, void *ptr, u32 *nextid,
32 unsigned long max, gfp_t gfp)
33{
34 unsigned long tmp = *nextid;
35 int ret = idr_alloc_ext(idr, ptr, &tmp, tmp, max + 1, gfp);
36 *nextid = tmp;
37 return ret;
38}
39EXPORT_SYMBOL_GPL(idr_alloc_u32);
40
Chris Mi388f79f2017-08-30 02:31:57 -040041int idr_alloc_cmn(struct idr *idr, void *ptr, unsigned long *index,
42 unsigned long start, unsigned long end, gfp_t gfp,
43 bool ext)
Tejun Heod5c74092013-02-27 17:03:55 -080044{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050045 struct radix_tree_iter iter;
Chris Mi388f79f2017-08-30 02:31:57 -040046 void __rcu **slot;
Tejun Heod5c74092013-02-27 17:03:55 -080047
Matthew Wilcox0a835c42016-12-20 10:27:56 -050048 if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr)))
49 return -EINVAL;
Tejun Heod5c74092013-02-27 17:03:55 -080050
Matthew Wilcox0a835c42016-12-20 10:27:56 -050051 radix_tree_iter_init(&iter, start);
Chris Mi388f79f2017-08-30 02:31:57 -040052 if (ext)
53 slot = idr_get_free_ext(&idr->idr_rt, &iter, gfp, end);
54 else
55 slot = idr_get_free(&idr->idr_rt, &iter, gfp, end);
Matthew Wilcox0a835c42016-12-20 10:27:56 -050056 if (IS_ERR(slot))
57 return PTR_ERR(slot);
Tejun Heod5c74092013-02-27 17:03:55 -080058
Matthew Wilcox0a835c42016-12-20 10:27:56 -050059 radix_tree_iter_replace(&idr->idr_rt, &iter, slot, ptr);
60 radix_tree_iter_tag_clear(&idr->idr_rt, &iter, IDR_FREE);
Chris Mi388f79f2017-08-30 02:31:57 -040061
62 if (index)
63 *index = iter.index;
64 return 0;
Tejun Heod5c74092013-02-27 17:03:55 -080065}
Chris Mi388f79f2017-08-30 02:31:57 -040066EXPORT_SYMBOL_GPL(idr_alloc_cmn);
Tejun Heod5c74092013-02-27 17:03:55 -080067
Jeff Layton3e6628c42013-04-29 16:21:16 -070068/**
69 * idr_alloc_cyclic - allocate new idr entry in a cyclical fashion
Matthew Wilcox0a835c42016-12-20 10:27:56 -050070 * @idr: idr handle
Jeff Layton3e6628c42013-04-29 16:21:16 -070071 * @ptr: pointer to be associated with the new id
72 * @start: the minimum id (inclusive)
Matthew Wilcox0a835c42016-12-20 10:27:56 -050073 * @end: the maximum id (exclusive)
74 * @gfp: memory allocation flags
Jeff Layton3e6628c42013-04-29 16:21:16 -070075 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050076 * Allocates an ID larger than the last ID allocated if one is available.
77 * If not, it will attempt to allocate the smallest ID that is larger or
78 * equal to @start.
Jeff Layton3e6628c42013-04-29 16:21:16 -070079 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050080int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
Jeff Layton3e6628c42013-04-29 16:21:16 -070081{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050082 int id, curr = idr->idr_next;
Jeff Layton3e6628c42013-04-29 16:21:16 -070083
Matthew Wilcox0a835c42016-12-20 10:27:56 -050084 if (curr < start)
85 curr = start;
Jeff Layton3e6628c42013-04-29 16:21:16 -070086
Matthew Wilcox0a835c42016-12-20 10:27:56 -050087 id = idr_alloc(idr, ptr, curr, end, gfp);
88 if ((id == -ENOSPC) && (curr > start))
89 id = idr_alloc(idr, ptr, start, curr, gfp);
90
91 if (id >= 0)
92 idr->idr_next = id + 1U;
93
Jeff Layton3e6628c42013-04-29 16:21:16 -070094 return id;
95}
96EXPORT_SYMBOL(idr_alloc_cyclic);
97
Jeff Mahoney5806f072006-06-26 00:27:19 -070098/**
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070099 * idr_for_each - iterate through all stored pointers
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500100 * @idr: idr handle
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700101 * @fn: function to be called for each pointer
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500102 * @data: data passed to callback function
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700103 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500104 * The callback function will be called for each entry in @idr, passing
105 * the id, the pointer and the data pointer passed to this function.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700106 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500107 * If @fn returns anything other than %0, the iteration stops and that
108 * value is returned from this function.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700109 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500110 * idr_for_each() can be called concurrently with idr_alloc() and
111 * idr_remove() if protected by RCU. Newly added entries may not be
112 * seen and deleted entries may be seen, but adding and removing entries
113 * will not cause other entries to be skipped, nor spurious ones to be seen.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700114 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500115int idr_for_each(const struct idr *idr,
116 int (*fn)(int id, void *p, void *data), void *data)
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700117{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500118 struct radix_tree_iter iter;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -0500119 void __rcu **slot;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700120
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500121 radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, 0) {
122 int ret = fn(iter.index, rcu_dereference_raw(*slot), data);
123 if (ret)
124 return ret;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700125 }
126
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500127 return 0;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700128}
129EXPORT_SYMBOL(idr_for_each);
130
131/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500132 * idr_get_next - Find next populated entry
133 * @idr: idr handle
134 * @nextid: Pointer to lowest possible ID to return
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700135 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500136 * Returns the next populated entry in the tree with an ID greater than
137 * or equal to the value pointed to by @nextid. On exit, @nextid is updated
138 * to the ID of the found value. To use in a loop, the value pointed to by
139 * nextid must be incremented by the user.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700140 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500141void *idr_get_next(struct idr *idr, int *nextid)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700142{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500143 struct radix_tree_iter iter;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -0500144 void __rcu **slot;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700145
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500146 slot = radix_tree_iter_find(&idr->idr_rt, &iter, *nextid);
147 if (!slot)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700148 return NULL;
149
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500150 *nextid = iter.index;
151 return rcu_dereference_raw(*slot);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700152}
Ben Hutchings4d1ee802010-01-29 20:59:17 +0000153EXPORT_SYMBOL(idr_get_next);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700154
Chris Mi388f79f2017-08-30 02:31:57 -0400155void *idr_get_next_ext(struct idr *idr, unsigned long *nextid)
156{
157 struct radix_tree_iter iter;
158 void __rcu **slot;
159
160 slot = radix_tree_iter_find(&idr->idr_rt, &iter, *nextid);
161 if (!slot)
162 return NULL;
163
164 *nextid = iter.index;
165 return rcu_dereference_raw(*slot);
166}
167EXPORT_SYMBOL(idr_get_next_ext);
168
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700169/**
Jeff Mahoney5806f072006-06-26 00:27:19 -0700170 * idr_replace - replace pointer for given id
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500171 * @idr: idr handle
172 * @ptr: New pointer to associate with the ID
173 * @id: Lookup key
Jeff Mahoney5806f072006-06-26 00:27:19 -0700174 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500175 * Replace the pointer registered with an ID and return the old value.
176 * This function can be called under the RCU read lock concurrently with
177 * idr_alloc() and idr_remove() (as long as the ID being removed is not
178 * the one being replaced!).
Jeff Mahoney5806f072006-06-26 00:27:19 -0700179 *
Eric Biggersa70e43a2017-10-03 16:16:13 -0700180 * Returns: the old value on success. %-ENOENT indicates that @id was not
Matthew Wilcox234a4622017-11-28 09:56:36 -0500181 * found. %-EINVAL indicates that @ptr was not valid.
Jeff Mahoney5806f072006-06-26 00:27:19 -0700182 */
Matthew Wilcox234a4622017-11-28 09:56:36 -0500183void *idr_replace(struct idr *idr, void *ptr, unsigned long id)
Chris Mi388f79f2017-08-30 02:31:57 -0400184{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500185 struct radix_tree_node *node;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -0500186 void __rcu **slot = NULL;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500187 void *entry;
Jeff Mahoney5806f072006-06-26 00:27:19 -0700188
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500189 if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr)))
Tejun Heoe8c8d1b2013-02-27 17:05:04 -0800190 return ERR_PTR(-EINVAL);
191
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500192 entry = __radix_tree_lookup(&idr->idr_rt, id, &node, &slot);
193 if (!slot || radix_tree_tag_get(&idr->idr_rt, id, IDR_FREE))
Lai Jiangshanb93804b2014-06-06 14:37:13 -0700194 return ERR_PTR(-ENOENT);
Manfred Spraul6ff2d392008-12-01 13:14:02 -0800195
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800196 __radix_tree_replace(&idr->idr_rt, node, slot, ptr, NULL);
Jeff Mahoney5806f072006-06-26 00:27:19 -0700197
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500198 return entry;
Jeff Mahoney5806f072006-06-26 00:27:19 -0700199}
Matthew Wilcox234a4622017-11-28 09:56:36 -0500200EXPORT_SYMBOL(idr_replace);
Jeff Mahoney5806f072006-06-26 00:27:19 -0700201
Randy Dunlap56083ab2010-10-26 14:19:08 -0700202/**
203 * DOC: IDA description
Tejun Heo72dba582007-06-14 03:45:13 +0900204 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500205 * The IDA is an ID allocator which does not provide the ability to
206 * associate an ID with a pointer. As such, it only needs to store one
207 * bit per ID, and so is more space efficient than an IDR. To use an IDA,
208 * define it using DEFINE_IDA() (or embed a &struct ida in a data structure,
209 * then initialise it using ida_init()). To allocate a new ID, call
210 * ida_simple_get(). To free an ID, call ida_simple_remove().
Tejun Heo72dba582007-06-14 03:45:13 +0900211 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500212 * If you have more complex locking requirements, use a loop around
213 * ida_pre_get() and ida_get_new() to allocate a new ID. Then use
214 * ida_remove() to free an ID. You must make sure that ida_get_new() and
215 * ida_remove() cannot be called at the same time as each other for the
216 * same IDA.
217 *
218 * You can also use ida_get_new_above() if you need an ID to be allocated
219 * above a particular number. ida_destroy() can be used to dispose of an
220 * IDA without needing to free the individual IDs in it. You can use
221 * ida_is_empty() to find out whether the IDA has any IDs currently allocated.
222 *
223 * IDs are currently limited to the range [0-INT_MAX]. If this is an awkward
224 * limitation, it should be quite straightforward to raise the maximum.
Tejun Heo72dba582007-06-14 03:45:13 +0900225 */
226
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500227/*
228 * Developer's notes:
229 *
230 * The IDA uses the functionality provided by the IDR & radix tree to store
231 * bitmaps in each entry. The IDR_FREE tag means there is at least one bit
232 * free, unlike the IDR where it means at least one entry is free.
233 *
234 * I considered telling the radix tree that each slot is an order-10 node
235 * and storing the bit numbers in the radix tree, but the radix tree can't
236 * allow a single multiorder entry at index 0, which would significantly
237 * increase memory consumption for the IDA. So instead we divide the index
238 * by the number of bits in the leaf bitmap before doing a radix tree lookup.
239 *
240 * As an optimisation, if there are only a few low bits set in any given
241 * leaf, instead of allocating a 128-byte bitmap, we use the 'exceptional
242 * entry' functionality of the radix tree to store BITS_PER_LONG - 2 bits
243 * directly in the entry. By being really tricksy, we could store
244 * BITS_PER_LONG - 1 bits, but there're diminishing returns after optimising
245 * for 0-3 allocated IDs.
246 *
247 * We allow the radix tree 'exceptional' count to get out of date. Nothing
248 * in the IDA nor the radix tree code checks it. If it becomes important
249 * to maintain an accurate exceptional count, switch the rcu_assign_pointer()
250 * calls to radix_tree_iter_replace() which will correct the exceptional
251 * count.
252 *
253 * The IDA always requires a lock to alloc/free. If we add a 'test_bit'
254 * equivalent, it will still need locking. Going to RCU lookup would require
255 * using RCU to free bitmaps, and that's not trivial without embedding an
256 * RCU head in the bitmap, which adds a 2-pointer overhead to each 128-byte
257 * bitmap, which is excessive.
258 */
259
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500260#define IDA_MAX (0x80000000U / IDA_BITMAP_BITS)
261
Tejun Heo72dba582007-06-14 03:45:13 +0900262/**
263 * ida_get_new_above - allocate new ID above or equal to a start id
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500264 * @ida: ida handle
265 * @start: id to start search at
266 * @id: pointer to the allocated handle
Tejun Heo72dba582007-06-14 03:45:13 +0900267 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500268 * Allocate new ID above or equal to @start. It should be called
269 * with any required locks to ensure that concurrent calls to
270 * ida_get_new_above() / ida_get_new() / ida_remove() are not allowed.
271 * Consider using ida_simple_get() if you do not have complex locking
272 * requirements.
Tejun Heo72dba582007-06-14 03:45:13 +0900273 *
Randy Dunlap56083ab2010-10-26 14:19:08 -0700274 * If memory is required, it will return %-EAGAIN, you should unlock
Tejun Heo72dba582007-06-14 03:45:13 +0900275 * and go back to the ida_pre_get() call. If the ida is full, it will
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500276 * return %-ENOSPC. On success, it will return 0.
Tejun Heo72dba582007-06-14 03:45:13 +0900277 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500278 * @id returns a value in the range @start ... %0x7fffffff.
Tejun Heo72dba582007-06-14 03:45:13 +0900279 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500280int ida_get_new_above(struct ida *ida, int start, int *id)
Tejun Heo72dba582007-06-14 03:45:13 +0900281{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500282 struct radix_tree_root *root = &ida->ida_rt;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -0500283 void __rcu **slot;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500284 struct radix_tree_iter iter;
Tejun Heo72dba582007-06-14 03:45:13 +0900285 struct ida_bitmap *bitmap;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500286 unsigned long index;
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500287 unsigned bit, ebit;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500288 int new;
Tejun Heo72dba582007-06-14 03:45:13 +0900289
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500290 index = start / IDA_BITMAP_BITS;
291 bit = start % IDA_BITMAP_BITS;
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500292 ebit = bit + RADIX_TREE_EXCEPTIONAL_SHIFT;
Tejun Heo72dba582007-06-14 03:45:13 +0900293
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500294 slot = radix_tree_iter_init(&iter, index);
295 for (;;) {
296 if (slot)
297 slot = radix_tree_next_slot(slot, &iter,
298 RADIX_TREE_ITER_TAGGED);
299 if (!slot) {
300 slot = idr_get_free(root, &iter, GFP_NOWAIT, IDA_MAX);
301 if (IS_ERR(slot)) {
302 if (slot == ERR_PTR(-ENOMEM))
303 return -EAGAIN;
304 return PTR_ERR(slot);
305 }
306 }
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500307 if (iter.index > index) {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500308 bit = 0;
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500309 ebit = RADIX_TREE_EXCEPTIONAL_SHIFT;
310 }
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500311 new = iter.index * IDA_BITMAP_BITS;
312 bitmap = rcu_dereference_raw(*slot);
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500313 if (radix_tree_exception(bitmap)) {
314 unsigned long tmp = (unsigned long)bitmap;
315 ebit = find_next_zero_bit(&tmp, BITS_PER_LONG, ebit);
316 if (ebit < BITS_PER_LONG) {
317 tmp |= 1UL << ebit;
318 rcu_assign_pointer(*slot, (void *)tmp);
319 *id = new + ebit - RADIX_TREE_EXCEPTIONAL_SHIFT;
320 return 0;
321 }
322 bitmap = this_cpu_xchg(ida_bitmap, NULL);
323 if (!bitmap)
324 return -EAGAIN;
325 memset(bitmap, 0, sizeof(*bitmap));
326 bitmap->bitmap[0] = tmp >> RADIX_TREE_EXCEPTIONAL_SHIFT;
327 rcu_assign_pointer(*slot, bitmap);
328 }
329
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500330 if (bitmap) {
331 bit = find_next_zero_bit(bitmap->bitmap,
332 IDA_BITMAP_BITS, bit);
333 new += bit;
334 if (new < 0)
335 return -ENOSPC;
336 if (bit == IDA_BITMAP_BITS)
337 continue;
Tejun Heo72dba582007-06-14 03:45:13 +0900338
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500339 __set_bit(bit, bitmap->bitmap);
340 if (bitmap_full(bitmap->bitmap, IDA_BITMAP_BITS))
341 radix_tree_iter_tag_clear(root, &iter,
342 IDR_FREE);
343 } else {
344 new += bit;
345 if (new < 0)
346 return -ENOSPC;
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500347 if (ebit < BITS_PER_LONG) {
348 bitmap = (void *)((1UL << ebit) |
349 RADIX_TREE_EXCEPTIONAL_ENTRY);
350 radix_tree_iter_replace(root, &iter, slot,
351 bitmap);
352 *id = new;
353 return 0;
354 }
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -0500355 bitmap = this_cpu_xchg(ida_bitmap, NULL);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500356 if (!bitmap)
357 return -EAGAIN;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500358 memset(bitmap, 0, sizeof(*bitmap));
359 __set_bit(bit, bitmap->bitmap);
360 radix_tree_iter_replace(root, &iter, slot, bitmap);
361 }
Tejun Heo72dba582007-06-14 03:45:13 +0900362
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500363 *id = new;
364 return 0;
Tejun Heo72dba582007-06-14 03:45:13 +0900365 }
Tejun Heo72dba582007-06-14 03:45:13 +0900366}
367EXPORT_SYMBOL(ida_get_new_above);
368
369/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500370 * ida_remove - Free the given ID
371 * @ida: ida handle
372 * @id: ID to free
373 *
374 * This function should not be called at the same time as ida_get_new_above().
Tejun Heo72dba582007-06-14 03:45:13 +0900375 */
376void ida_remove(struct ida *ida, int id)
377{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500378 unsigned long index = id / IDA_BITMAP_BITS;
379 unsigned offset = id % IDA_BITMAP_BITS;
Tejun Heo72dba582007-06-14 03:45:13 +0900380 struct ida_bitmap *bitmap;
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500381 unsigned long *btmp;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500382 struct radix_tree_iter iter;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -0500383 void __rcu **slot;
Tejun Heo72dba582007-06-14 03:45:13 +0900384
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500385 slot = radix_tree_iter_lookup(&ida->ida_rt, &iter, index);
386 if (!slot)
Lai Jiangshan8f9f6652014-06-06 14:37:11 -0700387 goto err;
388
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500389 bitmap = rcu_dereference_raw(*slot);
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500390 if (radix_tree_exception(bitmap)) {
391 btmp = (unsigned long *)slot;
392 offset += RADIX_TREE_EXCEPTIONAL_SHIFT;
393 if (offset >= BITS_PER_LONG)
394 goto err;
395 } else {
396 btmp = bitmap->bitmap;
397 }
398 if (!test_bit(offset, btmp))
Tejun Heo72dba582007-06-14 03:45:13 +0900399 goto err;
400
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500401 __clear_bit(offset, btmp);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500402 radix_tree_iter_tag_set(&ida->ida_rt, &iter, IDR_FREE);
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500403 if (radix_tree_exception(bitmap)) {
404 if (rcu_dereference_raw(*slot) ==
405 (void *)RADIX_TREE_EXCEPTIONAL_ENTRY)
406 radix_tree_iter_delete(&ida->ida_rt, &iter, slot);
407 } else if (bitmap_empty(btmp, IDA_BITMAP_BITS)) {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500408 kfree(bitmap);
409 radix_tree_iter_delete(&ida->ida_rt, &iter, slot);
Tejun Heo72dba582007-06-14 03:45:13 +0900410 }
Tejun Heo72dba582007-06-14 03:45:13 +0900411 return;
Tejun Heo72dba582007-06-14 03:45:13 +0900412 err:
Jean Delvaredd04b452013-07-03 15:08:47 -0700413 WARN(1, "ida_remove called for id=%d which is not allocated.\n", id);
Tejun Heo72dba582007-06-14 03:45:13 +0900414}
415EXPORT_SYMBOL(ida_remove);
416
417/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500418 * ida_destroy - Free the contents of an ida
419 * @ida: ida handle
420 *
421 * Calling this function releases all resources associated with an IDA. When
422 * this call returns, the IDA is empty and can be reused or freed. The caller
423 * should not allow ida_remove() or ida_get_new_above() to be called at the
424 * same time.
Tejun Heo72dba582007-06-14 03:45:13 +0900425 */
426void ida_destroy(struct ida *ida)
427{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500428 struct radix_tree_iter iter;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -0500429 void __rcu **slot;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500430
431 radix_tree_for_each_slot(slot, &ida->ida_rt, &iter, 0) {
432 struct ida_bitmap *bitmap = rcu_dereference_raw(*slot);
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500433 if (!radix_tree_exception(bitmap))
434 kfree(bitmap);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500435 radix_tree_iter_delete(&ida->ida_rt, &iter, slot);
436 }
Tejun Heo72dba582007-06-14 03:45:13 +0900437}
438EXPORT_SYMBOL(ida_destroy);
439
440/**
Rusty Russell88eca022011-08-03 16:21:06 -0700441 * ida_simple_get - get a new id.
442 * @ida: the (initialized) ida.
443 * @start: the minimum id (inclusive, < 0x8000000)
444 * @end: the maximum id (exclusive, < 0x8000000 or 0)
445 * @gfp_mask: memory allocation flags
446 *
447 * Allocates an id in the range start <= id < end, or returns -ENOSPC.
448 * On memory allocation failure, returns -ENOMEM.
449 *
Daniel Vettera2ef9472016-12-12 16:46:20 -0800450 * Compared to ida_get_new_above() this function does its own locking, and
451 * should be used unless there are special requirements.
452 *
Rusty Russell88eca022011-08-03 16:21:06 -0700453 * Use ida_simple_remove() to get rid of an id.
454 */
455int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
456 gfp_t gfp_mask)
457{
458 int ret, id;
459 unsigned int max;
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700460 unsigned long flags;
Rusty Russell88eca022011-08-03 16:21:06 -0700461
462 BUG_ON((int)start < 0);
463 BUG_ON((int)end < 0);
464
465 if (end == 0)
466 max = 0x80000000;
467 else {
468 BUG_ON(end < start);
469 max = end - 1;
470 }
471
472again:
473 if (!ida_pre_get(ida, gfp_mask))
474 return -ENOMEM;
475
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700476 spin_lock_irqsave(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700477 ret = ida_get_new_above(ida, start, &id);
478 if (!ret) {
479 if (id > max) {
480 ida_remove(ida, id);
481 ret = -ENOSPC;
482 } else {
483 ret = id;
484 }
485 }
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700486 spin_unlock_irqrestore(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700487
488 if (unlikely(ret == -EAGAIN))
489 goto again;
490
491 return ret;
492}
493EXPORT_SYMBOL(ida_simple_get);
494
495/**
496 * ida_simple_remove - remove an allocated id.
497 * @ida: the (initialized) ida.
498 * @id: the id returned by ida_simple_get.
Daniel Vettera2ef9472016-12-12 16:46:20 -0800499 *
500 * Use to release an id allocated with ida_simple_get().
501 *
502 * Compared to ida_remove() this function does its own locking, and should be
503 * used unless there are special requirements.
Rusty Russell88eca022011-08-03 16:21:06 -0700504 */
505void ida_simple_remove(struct ida *ida, unsigned int id)
506{
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700507 unsigned long flags;
508
Rusty Russell88eca022011-08-03 16:21:06 -0700509 BUG_ON((int)id < 0);
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700510 spin_lock_irqsave(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700511 ida_remove(ida, id);
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700512 spin_unlock_irqrestore(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700513}
514EXPORT_SYMBOL(ida_simple_remove);