blob: b87056e2cc4c72f54c7b5dce5eab2ad862fcb085 [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
Rusty Russell88eca022011-08-03 16:21:06 -07007static DEFINE_SPINLOCK(simple_ida_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Tejun Heod5c74092013-02-27 17:03:55 -08009/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -050010 * idr_alloc - allocate an id
11 * @idr: idr handle
Tejun Heod5c74092013-02-27 17:03:55 -080012 * @ptr: pointer to be associated with the new id
13 * @start: the minimum id (inclusive)
Matthew Wilcox0a835c42016-12-20 10:27:56 -050014 * @end: the maximum id (exclusive)
15 * @gfp: memory allocation flags
Tejun Heod5c74092013-02-27 17:03:55 -080016 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050017 * Allocates an unused ID in the range [start, end). Returns -ENOSPC
18 * if there are no unused IDs in that range.
Tejun Heod5c74092013-02-27 17:03:55 -080019 *
20 * Note that @end is treated as max when <= 0. This is to always allow
21 * using @start + N as @end as long as N is inside integer range.
22 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050023 * Simultaneous modifications to the @idr are not allowed and should be
24 * prevented by the user, usually with a lock. idr_alloc() may be called
25 * concurrently with read-only accesses to the @idr, such as idr_find() and
26 * idr_for_each_entry().
Tejun Heod5c74092013-02-27 17:03:55 -080027 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050028int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
Tejun Heod5c74092013-02-27 17:03:55 -080029{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050030 void **slot;
31 struct radix_tree_iter iter;
Tejun Heod5c74092013-02-27 17:03:55 -080032
Tejun Heod5c74092013-02-27 17:03:55 -080033 if (WARN_ON_ONCE(start < 0))
34 return -EINVAL;
Matthew Wilcox0a835c42016-12-20 10:27:56 -050035 if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr)))
36 return -EINVAL;
Tejun Heod5c74092013-02-27 17:03:55 -080037
Matthew Wilcox0a835c42016-12-20 10:27:56 -050038 radix_tree_iter_init(&iter, start);
39 slot = idr_get_free(&idr->idr_rt, &iter, gfp, end);
40 if (IS_ERR(slot))
41 return PTR_ERR(slot);
Tejun Heod5c74092013-02-27 17:03:55 -080042
Matthew Wilcox0a835c42016-12-20 10:27:56 -050043 radix_tree_iter_replace(&idr->idr_rt, &iter, slot, ptr);
44 radix_tree_iter_tag_clear(&idr->idr_rt, &iter, IDR_FREE);
45 return iter.index;
Tejun Heod5c74092013-02-27 17:03:55 -080046}
47EXPORT_SYMBOL_GPL(idr_alloc);
48
Jeff Layton3e6628c42013-04-29 16:21:16 -070049/**
50 * idr_alloc_cyclic - allocate new idr entry in a cyclical fashion
Matthew Wilcox0a835c42016-12-20 10:27:56 -050051 * @idr: idr handle
Jeff Layton3e6628c42013-04-29 16:21:16 -070052 * @ptr: pointer to be associated with the new id
53 * @start: the minimum id (inclusive)
Matthew Wilcox0a835c42016-12-20 10:27:56 -050054 * @end: the maximum id (exclusive)
55 * @gfp: memory allocation flags
Jeff Layton3e6628c42013-04-29 16:21:16 -070056 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050057 * Allocates an ID larger than the last ID allocated if one is available.
58 * If not, it will attempt to allocate the smallest ID that is larger or
59 * equal to @start.
Jeff Layton3e6628c42013-04-29 16:21:16 -070060 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050061int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
Jeff Layton3e6628c42013-04-29 16:21:16 -070062{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050063 int id, curr = idr->idr_next;
Jeff Layton3e6628c42013-04-29 16:21:16 -070064
Matthew Wilcox0a835c42016-12-20 10:27:56 -050065 if (curr < start)
66 curr = start;
Jeff Layton3e6628c42013-04-29 16:21:16 -070067
Matthew Wilcox0a835c42016-12-20 10:27:56 -050068 id = idr_alloc(idr, ptr, curr, end, gfp);
69 if ((id == -ENOSPC) && (curr > start))
70 id = idr_alloc(idr, ptr, start, curr, gfp);
71
72 if (id >= 0)
73 idr->idr_next = id + 1U;
74
Jeff Layton3e6628c42013-04-29 16:21:16 -070075 return id;
76}
77EXPORT_SYMBOL(idr_alloc_cyclic);
78
Jeff Mahoney5806f072006-06-26 00:27:19 -070079/**
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070080 * idr_for_each - iterate through all stored pointers
Matthew Wilcox0a835c42016-12-20 10:27:56 -050081 * @idr: idr handle
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070082 * @fn: function to be called for each pointer
Matthew Wilcox0a835c42016-12-20 10:27:56 -050083 * @data: data passed to callback function
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070084 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050085 * The callback function will be called for each entry in @idr, passing
86 * the id, the pointer and the data pointer passed to this function.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070087 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050088 * If @fn returns anything other than %0, the iteration stops and that
89 * value is returned from this function.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070090 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050091 * idr_for_each() can be called concurrently with idr_alloc() and
92 * idr_remove() if protected by RCU. Newly added entries may not be
93 * seen and deleted entries may be seen, but adding and removing entries
94 * will not cause other entries to be skipped, nor spurious ones to be seen.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070095 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050096int idr_for_each(const struct idr *idr,
97 int (*fn)(int id, void *p, void *data), void *data)
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070098{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050099 struct radix_tree_iter iter;
100 void **slot;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700101
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500102 radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, 0) {
103 int ret = fn(iter.index, rcu_dereference_raw(*slot), data);
104 if (ret)
105 return ret;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700106 }
107
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500108 return 0;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700109}
110EXPORT_SYMBOL(idr_for_each);
111
112/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500113 * idr_get_next - Find next populated entry
114 * @idr: idr handle
115 * @nextid: Pointer to lowest possible ID to return
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700116 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500117 * Returns the next populated entry in the tree with an ID greater than
118 * or equal to the value pointed to by @nextid. On exit, @nextid is updated
119 * to the ID of the found value. To use in a loop, the value pointed to by
120 * nextid must be incremented by the user.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700121 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500122void *idr_get_next(struct idr *idr, int *nextid)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700123{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500124 struct radix_tree_iter iter;
125 void **slot;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700126
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500127 slot = radix_tree_iter_find(&idr->idr_rt, &iter, *nextid);
128 if (!slot)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700129 return NULL;
130
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500131 *nextid = iter.index;
132 return rcu_dereference_raw(*slot);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700133}
Ben Hutchings4d1ee802010-01-29 20:59:17 +0000134EXPORT_SYMBOL(idr_get_next);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700135
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700136/**
Jeff Mahoney5806f072006-06-26 00:27:19 -0700137 * idr_replace - replace pointer for given id
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500138 * @idr: idr handle
139 * @ptr: New pointer to associate with the ID
140 * @id: Lookup key
Jeff Mahoney5806f072006-06-26 00:27:19 -0700141 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500142 * Replace the pointer registered with an ID and return the old value.
143 * This function can be called under the RCU read lock concurrently with
144 * idr_alloc() and idr_remove() (as long as the ID being removed is not
145 * the one being replaced!).
Jeff Mahoney5806f072006-06-26 00:27:19 -0700146 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500147 * Returns: 0 on success. %-ENOENT indicates that @id was not found.
148 * %-EINVAL indicates that @id or @ptr were not valid.
Jeff Mahoney5806f072006-06-26 00:27:19 -0700149 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500150void *idr_replace(struct idr *idr, void *ptr, int id)
Jeff Mahoney5806f072006-06-26 00:27:19 -0700151{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500152 struct radix_tree_node *node;
153 void **slot = NULL;
154 void *entry;
Jeff Mahoney5806f072006-06-26 00:27:19 -0700155
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500156 if (WARN_ON_ONCE(id < 0))
157 return ERR_PTR(-EINVAL);
158 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
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500165 __radix_tree_replace(&idr->idr_rt, node, slot, ptr, NULL, 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}
169EXPORT_SYMBOL(idr_replace);
170
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
Tejun Heo72dba582007-06-14 03:45:13 +0900196/**
197 * ida_pre_get - reserve resources for ida allocation
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500198 * @ida: ida handle
199 * @gfp: memory allocation flags
Tejun Heo72dba582007-06-14 03:45:13 +0900200 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500201 * This function should be called before calling ida_get_new_above(). If it
202 * is unable to allocate memory, it will return %0. On success, it returns %1.
Tejun Heo72dba582007-06-14 03:45:13 +0900203 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500204int ida_pre_get(struct ida *ida, gfp_t gfp)
Tejun Heo72dba582007-06-14 03:45:13 +0900205{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500206 struct ida_bitmap *bitmap;
Tejun Heo72dba582007-06-14 03:45:13 +0900207
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500208 /*
209 * This looks weird, but the IDA API has no preload_end() equivalent.
210 * Instead, ida_get_new() can return -EAGAIN, prompting the caller
211 * to return to the ida_pre_get() step.
212 */
213 idr_preload(gfp);
214 idr_preload_end();
215
Tejun Heo72dba582007-06-14 03:45:13 +0900216 if (!ida->free_bitmap) {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500217 bitmap = kmalloc(sizeof(struct ida_bitmap), gfp);
Tejun Heo72dba582007-06-14 03:45:13 +0900218 if (!bitmap)
219 return 0;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500220 bitmap = xchg(&ida->free_bitmap, bitmap);
221 kfree(bitmap);
Tejun Heo72dba582007-06-14 03:45:13 +0900222 }
223
224 return 1;
225}
226EXPORT_SYMBOL(ida_pre_get);
227
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500228#define IDA_MAX (0x80000000U / IDA_BITMAP_BITS)
229
Tejun Heo72dba582007-06-14 03:45:13 +0900230/**
231 * ida_get_new_above - allocate new ID above or equal to a start id
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500232 * @ida: ida handle
233 * @start: id to start search at
234 * @id: pointer to the allocated handle
Tejun Heo72dba582007-06-14 03:45:13 +0900235 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500236 * Allocate new ID above or equal to @start. It should be called
237 * with any required locks to ensure that concurrent calls to
238 * ida_get_new_above() / ida_get_new() / ida_remove() are not allowed.
239 * Consider using ida_simple_get() if you do not have complex locking
240 * requirements.
Tejun Heo72dba582007-06-14 03:45:13 +0900241 *
Randy Dunlap56083ab2010-10-26 14:19:08 -0700242 * If memory is required, it will return %-EAGAIN, you should unlock
Tejun Heo72dba582007-06-14 03:45:13 +0900243 * and go back to the ida_pre_get() call. If the ida is full, it will
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500244 * return %-ENOSPC. On success, it will return 0.
Tejun Heo72dba582007-06-14 03:45:13 +0900245 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500246 * @id returns a value in the range @start ... %0x7fffffff.
Tejun Heo72dba582007-06-14 03:45:13 +0900247 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500248int ida_get_new_above(struct ida *ida, int start, int *id)
Tejun Heo72dba582007-06-14 03:45:13 +0900249{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500250 struct radix_tree_root *root = &ida->ida_rt;
251 void **slot;
252 struct radix_tree_iter iter;
Tejun Heo72dba582007-06-14 03:45:13 +0900253 struct ida_bitmap *bitmap;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500254 unsigned long index;
255 unsigned bit;
256 int new;
Tejun Heo72dba582007-06-14 03:45:13 +0900257
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500258 index = start / IDA_BITMAP_BITS;
259 bit = start % IDA_BITMAP_BITS;
Tejun Heo72dba582007-06-14 03:45:13 +0900260
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500261 slot = radix_tree_iter_init(&iter, index);
262 for (;;) {
263 if (slot)
264 slot = radix_tree_next_slot(slot, &iter,
265 RADIX_TREE_ITER_TAGGED);
266 if (!slot) {
267 slot = idr_get_free(root, &iter, GFP_NOWAIT, IDA_MAX);
268 if (IS_ERR(slot)) {
269 if (slot == ERR_PTR(-ENOMEM))
270 return -EAGAIN;
271 return PTR_ERR(slot);
272 }
273 }
274 if (iter.index > index)
275 bit = 0;
276 new = iter.index * IDA_BITMAP_BITS;
277 bitmap = rcu_dereference_raw(*slot);
278 if (bitmap) {
279 bit = find_next_zero_bit(bitmap->bitmap,
280 IDA_BITMAP_BITS, bit);
281 new += bit;
282 if (new < 0)
283 return -ENOSPC;
284 if (bit == IDA_BITMAP_BITS)
285 continue;
Tejun Heo72dba582007-06-14 03:45:13 +0900286
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500287 __set_bit(bit, bitmap->bitmap);
288 if (bitmap_full(bitmap->bitmap, IDA_BITMAP_BITS))
289 radix_tree_iter_tag_clear(root, &iter,
290 IDR_FREE);
291 } else {
292 new += bit;
293 if (new < 0)
294 return -ENOSPC;
295 bitmap = ida->free_bitmap;
296 if (!bitmap)
297 return -EAGAIN;
298 ida->free_bitmap = NULL;
299 memset(bitmap, 0, sizeof(*bitmap));
300 __set_bit(bit, bitmap->bitmap);
301 radix_tree_iter_replace(root, &iter, slot, bitmap);
302 }
Tejun Heo72dba582007-06-14 03:45:13 +0900303
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500304 *id = new;
305 return 0;
Tejun Heo72dba582007-06-14 03:45:13 +0900306 }
Tejun Heo72dba582007-06-14 03:45:13 +0900307}
308EXPORT_SYMBOL(ida_get_new_above);
309
310/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500311 * ida_remove - Free the given ID
312 * @ida: ida handle
313 * @id: ID to free
314 *
315 * This function should not be called at the same time as ida_get_new_above().
Tejun Heo72dba582007-06-14 03:45:13 +0900316 */
317void ida_remove(struct ida *ida, int id)
318{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500319 unsigned long index = id / IDA_BITMAP_BITS;
320 unsigned offset = id % IDA_BITMAP_BITS;
Tejun Heo72dba582007-06-14 03:45:13 +0900321 struct ida_bitmap *bitmap;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500322 struct radix_tree_iter iter;
323 void **slot;
Tejun Heo72dba582007-06-14 03:45:13 +0900324
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500325 slot = radix_tree_iter_lookup(&ida->ida_rt, &iter, index);
326 if (!slot)
Lai Jiangshan8f9f6652014-06-06 14:37:11 -0700327 goto err;
328
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500329 bitmap = rcu_dereference_raw(*slot);
330 if (!test_bit(offset, bitmap->bitmap))
Tejun Heo72dba582007-06-14 03:45:13 +0900331 goto err;
332
Tejun Heo72dba582007-06-14 03:45:13 +0900333 __clear_bit(offset, bitmap->bitmap);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500334 radix_tree_iter_tag_set(&ida->ida_rt, &iter, IDR_FREE);
335 if (bitmap_empty(bitmap->bitmap, IDA_BITMAP_BITS)) {
336 kfree(bitmap);
337 radix_tree_iter_delete(&ida->ida_rt, &iter, slot);
Tejun Heo72dba582007-06-14 03:45:13 +0900338 }
Tejun Heo72dba582007-06-14 03:45:13 +0900339 return;
Tejun Heo72dba582007-06-14 03:45:13 +0900340 err:
Jean Delvaredd04b452013-07-03 15:08:47 -0700341 WARN(1, "ida_remove called for id=%d which is not allocated.\n", id);
Tejun Heo72dba582007-06-14 03:45:13 +0900342}
343EXPORT_SYMBOL(ida_remove);
344
345/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500346 * ida_destroy - Free the contents of an ida
347 * @ida: ida handle
348 *
349 * Calling this function releases all resources associated with an IDA. When
350 * this call returns, the IDA is empty and can be reused or freed. The caller
351 * should not allow ida_remove() or ida_get_new_above() to be called at the
352 * same time.
Tejun Heo72dba582007-06-14 03:45:13 +0900353 */
354void ida_destroy(struct ida *ida)
355{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500356 struct radix_tree_iter iter;
357 void **slot;
358
359 radix_tree_for_each_slot(slot, &ida->ida_rt, &iter, 0) {
360 struct ida_bitmap *bitmap = rcu_dereference_raw(*slot);
361 kfree(bitmap);
362 radix_tree_iter_delete(&ida->ida_rt, &iter, slot);
363 }
364
Tejun Heo72dba582007-06-14 03:45:13 +0900365 kfree(ida->free_bitmap);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500366 ida->free_bitmap = NULL;
Tejun Heo72dba582007-06-14 03:45:13 +0900367}
368EXPORT_SYMBOL(ida_destroy);
369
370/**
Rusty Russell88eca022011-08-03 16:21:06 -0700371 * ida_simple_get - get a new id.
372 * @ida: the (initialized) ida.
373 * @start: the minimum id (inclusive, < 0x8000000)
374 * @end: the maximum id (exclusive, < 0x8000000 or 0)
375 * @gfp_mask: memory allocation flags
376 *
377 * Allocates an id in the range start <= id < end, or returns -ENOSPC.
378 * On memory allocation failure, returns -ENOMEM.
379 *
Daniel Vettera2ef9472016-12-12 16:46:20 -0800380 * Compared to ida_get_new_above() this function does its own locking, and
381 * should be used unless there are special requirements.
382 *
Rusty Russell88eca022011-08-03 16:21:06 -0700383 * Use ida_simple_remove() to get rid of an id.
384 */
385int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
386 gfp_t gfp_mask)
387{
388 int ret, id;
389 unsigned int max;
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700390 unsigned long flags;
Rusty Russell88eca022011-08-03 16:21:06 -0700391
392 BUG_ON((int)start < 0);
393 BUG_ON((int)end < 0);
394
395 if (end == 0)
396 max = 0x80000000;
397 else {
398 BUG_ON(end < start);
399 max = end - 1;
400 }
401
402again:
403 if (!ida_pre_get(ida, gfp_mask))
404 return -ENOMEM;
405
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700406 spin_lock_irqsave(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700407 ret = ida_get_new_above(ida, start, &id);
408 if (!ret) {
409 if (id > max) {
410 ida_remove(ida, id);
411 ret = -ENOSPC;
412 } else {
413 ret = id;
414 }
415 }
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700416 spin_unlock_irqrestore(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700417
418 if (unlikely(ret == -EAGAIN))
419 goto again;
420
421 return ret;
422}
423EXPORT_SYMBOL(ida_simple_get);
424
425/**
426 * ida_simple_remove - remove an allocated id.
427 * @ida: the (initialized) ida.
428 * @id: the id returned by ida_simple_get.
Daniel Vettera2ef9472016-12-12 16:46:20 -0800429 *
430 * Use to release an id allocated with ida_simple_get().
431 *
432 * Compared to ida_remove() this function does its own locking, and should be
433 * used unless there are special requirements.
Rusty Russell88eca022011-08-03 16:21:06 -0700434 */
435void ida_simple_remove(struct ida *ida, unsigned int id)
436{
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700437 unsigned long flags;
438
Rusty Russell88eca022011-08-03 16:21:06 -0700439 BUG_ON((int)id < 0);
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700440 spin_lock_irqsave(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700441 ida_remove(ida, id);
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700442 spin_unlock_irqrestore(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700443}
444EXPORT_SYMBOL(ida_simple_remove);