|
@@ -317,18 +317,12 @@ EXPORT_SYMBOL(idr_replace);
|
|
|
* bit per ID, and so is more space efficient than an IDR. To use an IDA,
|
|
* bit per ID, and so is more space efficient than an IDR. To use an IDA,
|
|
|
* define it using DEFINE_IDA() (or embed a &struct ida in a data structure,
|
|
* define it using DEFINE_IDA() (or embed a &struct ida in a data structure,
|
|
|
* then initialise it using ida_init()). To allocate a new ID, call
|
|
* then initialise it using ida_init()). To allocate a new ID, call
|
|
|
- * ida_simple_get(). To free an ID, call ida_simple_remove().
|
|
|
|
|
|
|
+ * ida_alloc(), ida_alloc_min(), ida_alloc_max() or ida_alloc_range().
|
|
|
|
|
+ * To free an ID, call ida_free().
|
|
|
*
|
|
*
|
|
|
- * If you have more complex locking requirements, use a loop around
|
|
|
|
|
- * ida_pre_get() and ida_get_new() to allocate a new ID. Then use
|
|
|
|
|
- * ida_remove() to free an ID. You must make sure that ida_get_new() and
|
|
|
|
|
- * ida_remove() cannot be called at the same time as each other for the
|
|
|
|
|
- * same IDA.
|
|
|
|
|
- *
|
|
|
|
|
- * You can also use ida_get_new_above() if you need an ID to be allocated
|
|
|
|
|
- * above a particular number. ida_destroy() can be used to dispose of an
|
|
|
|
|
- * IDA without needing to free the individual IDs in it. You can use
|
|
|
|
|
- * ida_is_empty() to find out whether the IDA has any IDs currently allocated.
|
|
|
|
|
|
|
+ * ida_destroy() can be used to dispose of an IDA without needing to
|
|
|
|
|
+ * free the individual IDs in it. You can use ida_is_empty() to find
|
|
|
|
|
+ * out whether the IDA has any IDs currently allocated.
|
|
|
*
|
|
*
|
|
|
* IDs are currently limited to the range [0-INT_MAX]. If this is an awkward
|
|
* IDs are currently limited to the range [0-INT_MAX]. If this is an awkward
|
|
|
* limitation, it should be quite straightforward to raise the maximum.
|
|
* limitation, it should be quite straightforward to raise the maximum.
|
|
@@ -369,25 +363,7 @@ EXPORT_SYMBOL(idr_replace);
|
|
|
|
|
|
|
|
#define IDA_MAX (0x80000000U / IDA_BITMAP_BITS - 1)
|
|
#define IDA_MAX (0x80000000U / IDA_BITMAP_BITS - 1)
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * ida_get_new_above - allocate new ID above or equal to a start id
|
|
|
|
|
- * @ida: ida handle
|
|
|
|
|
- * @start: id to start search at
|
|
|
|
|
- * @id: pointer to the allocated handle
|
|
|
|
|
- *
|
|
|
|
|
- * Allocate new ID above or equal to @start. It should be called
|
|
|
|
|
- * with any required locks to ensure that concurrent calls to
|
|
|
|
|
- * ida_get_new_above() / ida_get_new() / ida_remove() are not allowed.
|
|
|
|
|
- * Consider using ida_simple_get() if you do not have complex locking
|
|
|
|
|
- * requirements.
|
|
|
|
|
- *
|
|
|
|
|
- * If memory is required, it will return %-EAGAIN, you should unlock
|
|
|
|
|
- * and go back to the ida_pre_get() call. If the ida is full, it will
|
|
|
|
|
- * return %-ENOSPC. On success, it will return 0.
|
|
|
|
|
- *
|
|
|
|
|
- * @id returns a value in the range @start ... %0x7fffffff.
|
|
|
|
|
- */
|
|
|
|
|
-int ida_get_new_above(struct ida *ida, int start, int *id)
|
|
|
|
|
|
|
+static int ida_get_new_above(struct ida *ida, int start)
|
|
|
{
|
|
{
|
|
|
struct radix_tree_root *root = &ida->ida_rt;
|
|
struct radix_tree_root *root = &ida->ida_rt;
|
|
|
void __rcu **slot;
|
|
void __rcu **slot;
|
|
@@ -426,8 +402,8 @@ int ida_get_new_above(struct ida *ida, int start, int *id)
|
|
|
if (ebit < BITS_PER_LONG) {
|
|
if (ebit < BITS_PER_LONG) {
|
|
|
tmp |= 1UL << ebit;
|
|
tmp |= 1UL << ebit;
|
|
|
rcu_assign_pointer(*slot, (void *)tmp);
|
|
rcu_assign_pointer(*slot, (void *)tmp);
|
|
|
- *id = new + ebit - RADIX_TREE_EXCEPTIONAL_SHIFT;
|
|
|
|
|
- return 0;
|
|
|
|
|
|
|
+ return new + ebit -
|
|
|
|
|
+ RADIX_TREE_EXCEPTIONAL_SHIFT;
|
|
|
}
|
|
}
|
|
|
bitmap = this_cpu_xchg(ida_bitmap, NULL);
|
|
bitmap = this_cpu_xchg(ida_bitmap, NULL);
|
|
|
if (!bitmap)
|
|
if (!bitmap)
|
|
@@ -458,8 +434,7 @@ int ida_get_new_above(struct ida *ida, int start, int *id)
|
|
|
RADIX_TREE_EXCEPTIONAL_ENTRY);
|
|
RADIX_TREE_EXCEPTIONAL_ENTRY);
|
|
|
radix_tree_iter_replace(root, &iter, slot,
|
|
radix_tree_iter_replace(root, &iter, slot,
|
|
|
bitmap);
|
|
bitmap);
|
|
|
- *id = new;
|
|
|
|
|
- return 0;
|
|
|
|
|
|
|
+ return new;
|
|
|
}
|
|
}
|
|
|
bitmap = this_cpu_xchg(ida_bitmap, NULL);
|
|
bitmap = this_cpu_xchg(ida_bitmap, NULL);
|
|
|
if (!bitmap)
|
|
if (!bitmap)
|
|
@@ -468,20 +443,11 @@ int ida_get_new_above(struct ida *ida, int start, int *id)
|
|
|
radix_tree_iter_replace(root, &iter, slot, bitmap);
|
|
radix_tree_iter_replace(root, &iter, slot, bitmap);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- *id = new;
|
|
|
|
|
- return 0;
|
|
|
|
|
|
|
+ return new;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-EXPORT_SYMBOL(ida_get_new_above);
|
|
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * ida_remove - Free the given ID
|
|
|
|
|
- * @ida: ida handle
|
|
|
|
|
- * @id: ID to free
|
|
|
|
|
- *
|
|
|
|
|
- * This function should not be called at the same time as ida_get_new_above().
|
|
|
|
|
- */
|
|
|
|
|
-void ida_remove(struct ida *ida, int id)
|
|
|
|
|
|
|
+static void ida_remove(struct ida *ida, int id)
|
|
|
{
|
|
{
|
|
|
unsigned long index = id / IDA_BITMAP_BITS;
|
|
unsigned long index = id / IDA_BITMAP_BITS;
|
|
|
unsigned offset = id % IDA_BITMAP_BITS;
|
|
unsigned offset = id % IDA_BITMAP_BITS;
|
|
@@ -518,99 +484,90 @@ void ida_remove(struct ida *ida, int id)
|
|
|
}
|
|
}
|
|
|
return;
|
|
return;
|
|
|
err:
|
|
err:
|
|
|
- WARN(1, "ida_remove called for id=%d which is not allocated.\n", id);
|
|
|
|
|
|
|
+ WARN(1, "ida_free called for id=%d which is not allocated.\n", id);
|
|
|
}
|
|
}
|
|
|
-EXPORT_SYMBOL(ida_remove);
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * ida_destroy - Free the contents of an ida
|
|
|
|
|
- * @ida: ida handle
|
|
|
|
|
|
|
+ * ida_destroy() - Free all IDs.
|
|
|
|
|
+ * @ida: IDA handle.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Calling this function frees all IDs and releases all resources used
|
|
|
|
|
+ * by an IDA. When this call returns, the IDA is empty and can be reused
|
|
|
|
|
+ * or freed. If the IDA is already empty, there is no need to call this
|
|
|
|
|
+ * function.
|
|
|
*
|
|
*
|
|
|
- * Calling this function releases all resources associated with an IDA. When
|
|
|
|
|
- * this call returns, the IDA is empty and can be reused or freed. The caller
|
|
|
|
|
- * should not allow ida_remove() or ida_get_new_above() to be called at the
|
|
|
|
|
- * same time.
|
|
|
|
|
|
|
+ * Context: Any context.
|
|
|
*/
|
|
*/
|
|
|
void ida_destroy(struct ida *ida)
|
|
void ida_destroy(struct ida *ida)
|
|
|
{
|
|
{
|
|
|
|
|
+ unsigned long flags;
|
|
|
struct radix_tree_iter iter;
|
|
struct radix_tree_iter iter;
|
|
|
void __rcu **slot;
|
|
void __rcu **slot;
|
|
|
|
|
|
|
|
|
|
+ xa_lock_irqsave(&ida->ida_rt, flags);
|
|
|
radix_tree_for_each_slot(slot, &ida->ida_rt, &iter, 0) {
|
|
radix_tree_for_each_slot(slot, &ida->ida_rt, &iter, 0) {
|
|
|
struct ida_bitmap *bitmap = rcu_dereference_raw(*slot);
|
|
struct ida_bitmap *bitmap = rcu_dereference_raw(*slot);
|
|
|
if (!radix_tree_exception(bitmap))
|
|
if (!radix_tree_exception(bitmap))
|
|
|
kfree(bitmap);
|
|
kfree(bitmap);
|
|
|
radix_tree_iter_delete(&ida->ida_rt, &iter, slot);
|
|
radix_tree_iter_delete(&ida->ida_rt, &iter, slot);
|
|
|
}
|
|
}
|
|
|
|
|
+ xa_unlock_irqrestore(&ida->ida_rt, flags);
|
|
|
}
|
|
}
|
|
|
EXPORT_SYMBOL(ida_destroy);
|
|
EXPORT_SYMBOL(ida_destroy);
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * ida_simple_get - get a new id.
|
|
|
|
|
- * @ida: the (initialized) ida.
|
|
|
|
|
- * @start: the minimum id (inclusive, < 0x8000000)
|
|
|
|
|
- * @end: the maximum id (exclusive, < 0x8000000 or 0)
|
|
|
|
|
- * @gfp_mask: memory allocation flags
|
|
|
|
|
- *
|
|
|
|
|
- * Allocates an id in the range start <= id < end, or returns -ENOSPC.
|
|
|
|
|
- * On memory allocation failure, returns -ENOMEM.
|
|
|
|
|
|
|
+ * ida_alloc_range() - Allocate an unused ID.
|
|
|
|
|
+ * @ida: IDA handle.
|
|
|
|
|
+ * @min: Lowest ID to allocate.
|
|
|
|
|
+ * @max: Highest ID to allocate.
|
|
|
|
|
+ * @gfp: Memory allocation flags.
|
|
|
*
|
|
*
|
|
|
- * Compared to ida_get_new_above() this function does its own locking, and
|
|
|
|
|
- * should be used unless there are special requirements.
|
|
|
|
|
|
|
+ * Allocate an ID between @min and @max, inclusive. The allocated ID will
|
|
|
|
|
+ * not exceed %INT_MAX, even if @max is larger.
|
|
|
*
|
|
*
|
|
|
- * Use ida_simple_remove() to get rid of an id.
|
|
|
|
|
|
|
+ * Context: Any context.
|
|
|
|
|
+ * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
|
|
|
|
|
+ * or %-ENOSPC if there are no free IDs.
|
|
|
*/
|
|
*/
|
|
|
-int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
|
|
|
|
|
- gfp_t gfp_mask)
|
|
|
|
|
|
|
+int ida_alloc_range(struct ida *ida, unsigned int min, unsigned int max,
|
|
|
|
|
+ gfp_t gfp)
|
|
|
{
|
|
{
|
|
|
- int ret, id;
|
|
|
|
|
- unsigned int max;
|
|
|
|
|
|
|
+ int id = 0;
|
|
|
unsigned long flags;
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
- BUG_ON((int)start < 0);
|
|
|
|
|
- BUG_ON((int)end < 0);
|
|
|
|
|
|
|
+ if ((int)min < 0)
|
|
|
|
|
+ return -ENOSPC;
|
|
|
|
|
|
|
|
- if (end == 0)
|
|
|
|
|
- max = 0x80000000;
|
|
|
|
|
- else {
|
|
|
|
|
- BUG_ON(end < start);
|
|
|
|
|
- max = end - 1;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if ((int)max < 0)
|
|
|
|
|
+ max = INT_MAX;
|
|
|
|
|
|
|
|
again:
|
|
again:
|
|
|
- if (!ida_pre_get(ida, gfp_mask))
|
|
|
|
|
- return -ENOMEM;
|
|
|
|
|
-
|
|
|
|
|
xa_lock_irqsave(&ida->ida_rt, flags);
|
|
xa_lock_irqsave(&ida->ida_rt, flags);
|
|
|
- ret = ida_get_new_above(ida, start, &id);
|
|
|
|
|
- if (!ret) {
|
|
|
|
|
- if (id > max) {
|
|
|
|
|
- ida_remove(ida, id);
|
|
|
|
|
- ret = -ENOSPC;
|
|
|
|
|
- } else {
|
|
|
|
|
- ret = id;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ id = ida_get_new_above(ida, min);
|
|
|
|
|
+ if (id > (int)max) {
|
|
|
|
|
+ ida_remove(ida, id);
|
|
|
|
|
+ id = -ENOSPC;
|
|
|
}
|
|
}
|
|
|
xa_unlock_irqrestore(&ida->ida_rt, flags);
|
|
xa_unlock_irqrestore(&ida->ida_rt, flags);
|
|
|
|
|
|
|
|
- if (unlikely(ret == -EAGAIN))
|
|
|
|
|
|
|
+ if (unlikely(id == -EAGAIN)) {
|
|
|
|
|
+ if (!ida_pre_get(ida, gfp))
|
|
|
|
|
+ return -ENOMEM;
|
|
|
goto again;
|
|
goto again;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return ret;
|
|
|
|
|
|
|
+ return id;
|
|
|
}
|
|
}
|
|
|
-EXPORT_SYMBOL(ida_simple_get);
|
|
|
|
|
|
|
+EXPORT_SYMBOL(ida_alloc_range);
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * ida_simple_remove - remove an allocated id.
|
|
|
|
|
- * @ida: the (initialized) ida.
|
|
|
|
|
- * @id: the id returned by ida_simple_get.
|
|
|
|
|
- *
|
|
|
|
|
- * Use to release an id allocated with ida_simple_get().
|
|
|
|
|
|
|
+ * ida_free() - Release an allocated ID.
|
|
|
|
|
+ * @ida: IDA handle.
|
|
|
|
|
+ * @id: Previously allocated ID.
|
|
|
*
|
|
*
|
|
|
- * Compared to ida_remove() this function does its own locking, and should be
|
|
|
|
|
- * used unless there are special requirements.
|
|
|
|
|
|
|
+ * Context: Any context.
|
|
|
*/
|
|
*/
|
|
|
-void ida_simple_remove(struct ida *ida, unsigned int id)
|
|
|
|
|
|
|
+void ida_free(struct ida *ida, unsigned int id)
|
|
|
{
|
|
{
|
|
|
unsigned long flags;
|
|
unsigned long flags;
|
|
|
|
|
|
|
@@ -619,4 +576,4 @@ void ida_simple_remove(struct ida *ida, unsigned int id)
|
|
|
ida_remove(ida, id);
|
|
ida_remove(ida, id);
|
|
|
xa_unlock_irqrestore(&ida->ida_rt, flags);
|
|
xa_unlock_irqrestore(&ida->ida_rt, flags);
|
|
|
}
|
|
}
|
|
|
-EXPORT_SYMBOL(ida_simple_remove);
|
|
|
|
|
|
|
+EXPORT_SYMBOL(ida_free);
|