blob: 2abd7769c430a250dd5426a2c055fc09f8584ca1 [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
Tejun Heod5c74092013-02-27 17:03:55 -080010/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -050011 * idr_alloc - allocate an id
12 * @idr: idr handle
Tejun Heod5c74092013-02-27 17:03:55 -080013 * @ptr: pointer to be associated with the new id
14 * @start: the minimum id (inclusive)
Matthew Wilcox0a835c42016-12-20 10:27:56 -050015 * @end: the maximum id (exclusive)
16 * @gfp: memory allocation flags
Tejun Heod5c74092013-02-27 17:03:55 -080017 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050018 * Allocates an unused ID in the range [start, end). Returns -ENOSPC
19 * if there are no unused IDs in that range.
Tejun Heod5c74092013-02-27 17:03:55 -080020 *
21 * Note that @end is treated as max when <= 0. This is to always allow
22 * using @start + N as @end as long as N is inside integer range.
23 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050024 * Simultaneous modifications to the @idr are not allowed and should be
25 * prevented by the user, usually with a lock. idr_alloc() may be called
26 * concurrently with read-only accesses to the @idr, such as idr_find() and
27 * idr_for_each_entry().
Tejun Heod5c74092013-02-27 17:03:55 -080028 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050029int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
Tejun Heod5c74092013-02-27 17:03:55 -080030{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050031 void **slot;
32 struct radix_tree_iter iter;
Tejun Heod5c74092013-02-27 17:03:55 -080033
Tejun Heod5c74092013-02-27 17:03:55 -080034 if (WARN_ON_ONCE(start < 0))
35 return -EINVAL;
Matthew Wilcox0a835c42016-12-20 10:27:56 -050036 if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr)))
37 return -EINVAL;
Tejun Heod5c74092013-02-27 17:03:55 -080038
Matthew Wilcox0a835c42016-12-20 10:27:56 -050039 radix_tree_iter_init(&iter, start);
40 slot = idr_get_free(&idr->idr_rt, &iter, gfp, end);
41 if (IS_ERR(slot))
42 return PTR_ERR(slot);
Tejun Heod5c74092013-02-27 17:03:55 -080043
Matthew Wilcox0a835c42016-12-20 10:27:56 -050044 radix_tree_iter_replace(&idr->idr_rt, &iter, slot, ptr);
45 radix_tree_iter_tag_clear(&idr->idr_rt, &iter, IDR_FREE);
46 return iter.index;
Tejun Heod5c74092013-02-27 17:03:55 -080047}
48EXPORT_SYMBOL_GPL(idr_alloc);
49
Jeff Layton3e6628c42013-04-29 16:21:16 -070050/**
51 * idr_alloc_cyclic - allocate new idr entry in a cyclical fashion
Matthew Wilcox0a835c42016-12-20 10:27:56 -050052 * @idr: idr handle
Jeff Layton3e6628c42013-04-29 16:21:16 -070053 * @ptr: pointer to be associated with the new id
54 * @start: the minimum id (inclusive)
Matthew Wilcox0a835c42016-12-20 10:27:56 -050055 * @end: the maximum id (exclusive)
56 * @gfp: memory allocation flags
Jeff Layton3e6628c42013-04-29 16:21:16 -070057 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050058 * Allocates an ID larger than the last ID allocated if one is available.
59 * If not, it will attempt to allocate the smallest ID that is larger or
60 * equal to @start.
Jeff Layton3e6628c42013-04-29 16:21:16 -070061 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050062int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
Jeff Layton3e6628c42013-04-29 16:21:16 -070063{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050064 int id, curr = idr->idr_next;
Jeff Layton3e6628c42013-04-29 16:21:16 -070065
Matthew Wilcox0a835c42016-12-20 10:27:56 -050066 if (curr < start)
67 curr = start;
Jeff Layton3e6628c42013-04-29 16:21:16 -070068
Matthew Wilcox0a835c42016-12-20 10:27:56 -050069 id = idr_alloc(idr, ptr, curr, end, gfp);
70 if ((id == -ENOSPC) && (curr > start))
71 id = idr_alloc(idr, ptr, start, curr, gfp);
72
73 if (id >= 0)
74 idr->idr_next = id + 1U;
75
Jeff Layton3e6628c42013-04-29 16:21:16 -070076 return id;
77}
78EXPORT_SYMBOL(idr_alloc_cyclic);
79
Jeff Mahoney5806f072006-06-26 00:27:19 -070080/**
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070081 * idr_for_each - iterate through all stored pointers
Matthew Wilcox0a835c42016-12-20 10:27:56 -050082 * @idr: idr handle
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070083 * @fn: function to be called for each pointer
Matthew Wilcox0a835c42016-12-20 10:27:56 -050084 * @data: data passed to callback function
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070085 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050086 * The callback function will be called for each entry in @idr, passing
87 * the id, the pointer and the data pointer passed to this function.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070088 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050089 * If @fn returns anything other than %0, the iteration stops and that
90 * value is returned from this function.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070091 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -050092 * idr_for_each() can be called concurrently with idr_alloc() and
93 * idr_remove() if protected by RCU. Newly added entries may not be
94 * seen and deleted entries may be seen, but adding and removing entries
95 * will not cause other entries to be skipped, nor spurious ones to be seen.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070096 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050097int idr_for_each(const struct idr *idr,
98 int (*fn)(int id, void *p, void *data), void *data)
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070099{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500100 struct radix_tree_iter iter;
101 void **slot;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700102
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500103 radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, 0) {
104 int ret = fn(iter.index, rcu_dereference_raw(*slot), data);
105 if (ret)
106 return ret;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700107 }
108
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500109 return 0;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700110}
111EXPORT_SYMBOL(idr_for_each);
112
113/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500114 * idr_get_next - Find next populated entry
115 * @idr: idr handle
116 * @nextid: Pointer to lowest possible ID to return
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700117 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500118 * Returns the next populated entry in the tree with an ID greater than
119 * or equal to the value pointed to by @nextid. On exit, @nextid is updated
120 * to the ID of the found value. To use in a loop, the value pointed to by
121 * nextid must be incremented by the user.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700122 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500123void *idr_get_next(struct idr *idr, int *nextid)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700124{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500125 struct radix_tree_iter iter;
126 void **slot;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700127
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500128 slot = radix_tree_iter_find(&idr->idr_rt, &iter, *nextid);
129 if (!slot)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700130 return NULL;
131
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500132 *nextid = iter.index;
133 return rcu_dereference_raw(*slot);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700134}
Ben Hutchings4d1ee802010-01-29 20:59:17 +0000135EXPORT_SYMBOL(idr_get_next);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700136
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700137/**
Jeff Mahoney5806f072006-06-26 00:27:19 -0700138 * idr_replace - replace pointer for given id
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500139 * @idr: idr handle
140 * @ptr: New pointer to associate with the ID
141 * @id: Lookup key
Jeff Mahoney5806f072006-06-26 00:27:19 -0700142 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500143 * Replace the pointer registered with an ID and return the old value.
144 * This function can be called under the RCU read lock concurrently with
145 * idr_alloc() and idr_remove() (as long as the ID being removed is not
146 * the one being replaced!).
Jeff Mahoney5806f072006-06-26 00:27:19 -0700147 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500148 * Returns: 0 on success. %-ENOENT indicates that @id was not found.
149 * %-EINVAL indicates that @id or @ptr were not valid.
Jeff Mahoney5806f072006-06-26 00:27:19 -0700150 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500151void *idr_replace(struct idr *idr, void *ptr, int id)
Jeff Mahoney5806f072006-06-26 00:27:19 -0700152{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500153 struct radix_tree_node *node;
154 void **slot = NULL;
155 void *entry;
Jeff Mahoney5806f072006-06-26 00:27:19 -0700156
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500157 if (WARN_ON_ONCE(id < 0))
158 return ERR_PTR(-EINVAL);
159 if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr)))
Tejun Heoe8c8d1b2013-02-27 17:05:04 -0800160 return ERR_PTR(-EINVAL);
161
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500162 entry = __radix_tree_lookup(&idr->idr_rt, id, &node, &slot);
163 if (!slot || radix_tree_tag_get(&idr->idr_rt, id, IDR_FREE))
Lai Jiangshanb93804b2014-06-06 14:37:13 -0700164 return ERR_PTR(-ENOENT);
Manfred Spraul6ff2d392008-12-01 13:14:02 -0800165
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500166 __radix_tree_replace(&idr->idr_rt, node, slot, ptr, NULL, NULL);
Jeff Mahoney5806f072006-06-26 00:27:19 -0700167
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500168 return entry;
Jeff Mahoney5806f072006-06-26 00:27:19 -0700169}
170EXPORT_SYMBOL(idr_replace);
171
Randy Dunlap56083ab2010-10-26 14:19:08 -0700172/**
173 * DOC: IDA description
Tejun Heo72dba582007-06-14 03:45:13 +0900174 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500175 * The IDA is an ID allocator which does not provide the ability to
176 * associate an ID with a pointer. As such, it only needs to store one
177 * bit per ID, and so is more space efficient than an IDR. To use an IDA,
178 * define it using DEFINE_IDA() (or embed a &struct ida in a data structure,
179 * then initialise it using ida_init()). To allocate a new ID, call
180 * ida_simple_get(). To free an ID, call ida_simple_remove().
Tejun Heo72dba582007-06-14 03:45:13 +0900181 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500182 * If you have more complex locking requirements, use a loop around
183 * ida_pre_get() and ida_get_new() to allocate a new ID. Then use
184 * ida_remove() to free an ID. You must make sure that ida_get_new() and
185 * ida_remove() cannot be called at the same time as each other for the
186 * same IDA.
187 *
188 * You can also use ida_get_new_above() if you need an ID to be allocated
189 * above a particular number. ida_destroy() can be used to dispose of an
190 * IDA without needing to free the individual IDs in it. You can use
191 * ida_is_empty() to find out whether the IDA has any IDs currently allocated.
192 *
193 * IDs are currently limited to the range [0-INT_MAX]. If this is an awkward
194 * limitation, it should be quite straightforward to raise the maximum.
Tejun Heo72dba582007-06-14 03:45:13 +0900195 */
196
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500197#define IDA_MAX (0x80000000U / IDA_BITMAP_BITS)
198
Tejun Heo72dba582007-06-14 03:45:13 +0900199/**
200 * ida_get_new_above - allocate new ID above or equal to a start id
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500201 * @ida: ida handle
202 * @start: id to start search at
203 * @id: pointer to the allocated handle
Tejun Heo72dba582007-06-14 03:45:13 +0900204 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500205 * Allocate new ID above or equal to @start. It should be called
206 * with any required locks to ensure that concurrent calls to
207 * ida_get_new_above() / ida_get_new() / ida_remove() are not allowed.
208 * Consider using ida_simple_get() if you do not have complex locking
209 * requirements.
Tejun Heo72dba582007-06-14 03:45:13 +0900210 *
Randy Dunlap56083ab2010-10-26 14:19:08 -0700211 * If memory is required, it will return %-EAGAIN, you should unlock
Tejun Heo72dba582007-06-14 03:45:13 +0900212 * and go back to the ida_pre_get() call. If the ida is full, it will
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500213 * return %-ENOSPC. On success, it will return 0.
Tejun Heo72dba582007-06-14 03:45:13 +0900214 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500215 * @id returns a value in the range @start ... %0x7fffffff.
Tejun Heo72dba582007-06-14 03:45:13 +0900216 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500217int ida_get_new_above(struct ida *ida, int start, int *id)
Tejun Heo72dba582007-06-14 03:45:13 +0900218{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500219 struct radix_tree_root *root = &ida->ida_rt;
220 void **slot;
221 struct radix_tree_iter iter;
Tejun Heo72dba582007-06-14 03:45:13 +0900222 struct ida_bitmap *bitmap;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500223 unsigned long index;
224 unsigned bit;
225 int new;
Tejun Heo72dba582007-06-14 03:45:13 +0900226
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500227 index = start / IDA_BITMAP_BITS;
228 bit = start % IDA_BITMAP_BITS;
Tejun Heo72dba582007-06-14 03:45:13 +0900229
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500230 slot = radix_tree_iter_init(&iter, index);
231 for (;;) {
232 if (slot)
233 slot = radix_tree_next_slot(slot, &iter,
234 RADIX_TREE_ITER_TAGGED);
235 if (!slot) {
236 slot = idr_get_free(root, &iter, GFP_NOWAIT, IDA_MAX);
237 if (IS_ERR(slot)) {
238 if (slot == ERR_PTR(-ENOMEM))
239 return -EAGAIN;
240 return PTR_ERR(slot);
241 }
242 }
243 if (iter.index > index)
244 bit = 0;
245 new = iter.index * IDA_BITMAP_BITS;
246 bitmap = rcu_dereference_raw(*slot);
247 if (bitmap) {
248 bit = find_next_zero_bit(bitmap->bitmap,
249 IDA_BITMAP_BITS, bit);
250 new += bit;
251 if (new < 0)
252 return -ENOSPC;
253 if (bit == IDA_BITMAP_BITS)
254 continue;
Tejun Heo72dba582007-06-14 03:45:13 +0900255
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500256 __set_bit(bit, bitmap->bitmap);
257 if (bitmap_full(bitmap->bitmap, IDA_BITMAP_BITS))
258 radix_tree_iter_tag_clear(root, &iter,
259 IDR_FREE);
260 } else {
261 new += bit;
262 if (new < 0)
263 return -ENOSPC;
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -0500264 bitmap = this_cpu_xchg(ida_bitmap, NULL);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500265 if (!bitmap)
266 return -EAGAIN;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500267 memset(bitmap, 0, sizeof(*bitmap));
268 __set_bit(bit, bitmap->bitmap);
269 radix_tree_iter_replace(root, &iter, slot, bitmap);
270 }
Tejun Heo72dba582007-06-14 03:45:13 +0900271
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500272 *id = new;
273 return 0;
Tejun Heo72dba582007-06-14 03:45:13 +0900274 }
Tejun Heo72dba582007-06-14 03:45:13 +0900275}
276EXPORT_SYMBOL(ida_get_new_above);
277
278/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500279 * ida_remove - Free the given ID
280 * @ida: ida handle
281 * @id: ID to free
282 *
283 * This function should not be called at the same time as ida_get_new_above().
Tejun Heo72dba582007-06-14 03:45:13 +0900284 */
285void ida_remove(struct ida *ida, int id)
286{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500287 unsigned long index = id / IDA_BITMAP_BITS;
288 unsigned offset = id % IDA_BITMAP_BITS;
Tejun Heo72dba582007-06-14 03:45:13 +0900289 struct ida_bitmap *bitmap;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500290 struct radix_tree_iter iter;
291 void **slot;
Tejun Heo72dba582007-06-14 03:45:13 +0900292
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500293 slot = radix_tree_iter_lookup(&ida->ida_rt, &iter, index);
294 if (!slot)
Lai Jiangshan8f9f6652014-06-06 14:37:11 -0700295 goto err;
296
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500297 bitmap = rcu_dereference_raw(*slot);
298 if (!test_bit(offset, bitmap->bitmap))
Tejun Heo72dba582007-06-14 03:45:13 +0900299 goto err;
300
Tejun Heo72dba582007-06-14 03:45:13 +0900301 __clear_bit(offset, bitmap->bitmap);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500302 radix_tree_iter_tag_set(&ida->ida_rt, &iter, IDR_FREE);
303 if (bitmap_empty(bitmap->bitmap, IDA_BITMAP_BITS)) {
304 kfree(bitmap);
305 radix_tree_iter_delete(&ida->ida_rt, &iter, slot);
Tejun Heo72dba582007-06-14 03:45:13 +0900306 }
Tejun Heo72dba582007-06-14 03:45:13 +0900307 return;
Tejun Heo72dba582007-06-14 03:45:13 +0900308 err:
Jean Delvaredd04b452013-07-03 15:08:47 -0700309 WARN(1, "ida_remove called for id=%d which is not allocated.\n", id);
Tejun Heo72dba582007-06-14 03:45:13 +0900310}
311EXPORT_SYMBOL(ida_remove);
312
313/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500314 * ida_destroy - Free the contents of an ida
315 * @ida: ida handle
316 *
317 * Calling this function releases all resources associated with an IDA. When
318 * this call returns, the IDA is empty and can be reused or freed. The caller
319 * should not allow ida_remove() or ida_get_new_above() to be called at the
320 * same time.
Tejun Heo72dba582007-06-14 03:45:13 +0900321 */
322void ida_destroy(struct ida *ida)
323{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500324 struct radix_tree_iter iter;
325 void **slot;
326
327 radix_tree_for_each_slot(slot, &ida->ida_rt, &iter, 0) {
328 struct ida_bitmap *bitmap = rcu_dereference_raw(*slot);
329 kfree(bitmap);
330 radix_tree_iter_delete(&ida->ida_rt, &iter, slot);
331 }
Tejun Heo72dba582007-06-14 03:45:13 +0900332}
333EXPORT_SYMBOL(ida_destroy);
334
335/**
Rusty Russell88eca022011-08-03 16:21:06 -0700336 * ida_simple_get - get a new id.
337 * @ida: the (initialized) ida.
338 * @start: the minimum id (inclusive, < 0x8000000)
339 * @end: the maximum id (exclusive, < 0x8000000 or 0)
340 * @gfp_mask: memory allocation flags
341 *
342 * Allocates an id in the range start <= id < end, or returns -ENOSPC.
343 * On memory allocation failure, returns -ENOMEM.
344 *
Daniel Vettera2ef9472016-12-12 16:46:20 -0800345 * Compared to ida_get_new_above() this function does its own locking, and
346 * should be used unless there are special requirements.
347 *
Rusty Russell88eca022011-08-03 16:21:06 -0700348 * Use ida_simple_remove() to get rid of an id.
349 */
350int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
351 gfp_t gfp_mask)
352{
353 int ret, id;
354 unsigned int max;
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700355 unsigned long flags;
Rusty Russell88eca022011-08-03 16:21:06 -0700356
357 BUG_ON((int)start < 0);
358 BUG_ON((int)end < 0);
359
360 if (end == 0)
361 max = 0x80000000;
362 else {
363 BUG_ON(end < start);
364 max = end - 1;
365 }
366
367again:
368 if (!ida_pre_get(ida, gfp_mask))
369 return -ENOMEM;
370
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700371 spin_lock_irqsave(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700372 ret = ida_get_new_above(ida, start, &id);
373 if (!ret) {
374 if (id > max) {
375 ida_remove(ida, id);
376 ret = -ENOSPC;
377 } else {
378 ret = id;
379 }
380 }
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700381 spin_unlock_irqrestore(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700382
383 if (unlikely(ret == -EAGAIN))
384 goto again;
385
386 return ret;
387}
388EXPORT_SYMBOL(ida_simple_get);
389
390/**
391 * ida_simple_remove - remove an allocated id.
392 * @ida: the (initialized) ida.
393 * @id: the id returned by ida_simple_get.
Daniel Vettera2ef9472016-12-12 16:46:20 -0800394 *
395 * Use to release an id allocated with ida_simple_get().
396 *
397 * Compared to ida_remove() this function does its own locking, and should be
398 * used unless there are special requirements.
Rusty Russell88eca022011-08-03 16:21:06 -0700399 */
400void ida_simple_remove(struct ida *ida, unsigned int id)
401{
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700402 unsigned long flags;
403
Rusty Russell88eca022011-08-03 16:21:06 -0700404 BUG_ON((int)id < 0);
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700405 spin_lock_irqsave(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700406 ida_remove(ida, id);
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700407 spin_unlock_irqrestore(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -0700408}
409EXPORT_SYMBOL(ida_simple_remove);