Merge tag 'regmap-3.4' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap
[firefly-linux-kernel-4.4.55.git] / lib / idr.c
index 5acf9bb10968caa97ec123ca464906fff4de42bc..12499ba7967e08f5388835834c08c67dc7af5c24 100644 (file)
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -595,8 +595,10 @@ EXPORT_SYMBOL(idr_for_each);
  * Returns pointer to registered object with id, which is next number to
  * given id. After being looked up, *@nextidp will be updated for the next
  * iteration.
+ *
+ * This function can be called under rcu_read_lock(), given that the leaf
+ * pointers lifetimes are correctly managed.
  */
-
 void *idr_get_next(struct idr *idp, int *nextidp)
 {
        struct idr_layer *p, *pa[MAX_LEVEL];
@@ -605,11 +607,11 @@ void *idr_get_next(struct idr *idp, int *nextidp)
        int n, max;
 
        /* find first ent */
-       n = idp->layers * IDR_BITS;
-       max = 1 << n;
        p = rcu_dereference_raw(idp->top);
        if (!p)
                return NULL;
+       n = (p->layer + 1) * IDR_BITS;
+       max = 1 << n;
 
        while (id < max) {
                while (n > 0 && p) {
@@ -767,8 +769,8 @@ EXPORT_SYMBOL(ida_pre_get);
  * @starting_id: id to start search at
  * @p_id:      pointer to the allocated handle
  *
- * Allocate new ID above or equal to @ida.  It should be called with
- * any required locks.
+ * Allocate new ID above or equal to @starting_id.  It should be called
+ * with any required locks.
  *
  * 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
@@ -944,6 +946,7 @@ int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
 {
        int ret, id;
        unsigned int max;
+       unsigned long flags;
 
        BUG_ON((int)start < 0);
        BUG_ON((int)end < 0);
@@ -959,7 +962,7 @@ again:
        if (!ida_pre_get(ida, gfp_mask))
                return -ENOMEM;
 
-       spin_lock(&simple_ida_lock);
+       spin_lock_irqsave(&simple_ida_lock, flags);
        ret = ida_get_new_above(ida, start, &id);
        if (!ret) {
                if (id > max) {
@@ -969,7 +972,7 @@ again:
                        ret = id;
                }
        }
-       spin_unlock(&simple_ida_lock);
+       spin_unlock_irqrestore(&simple_ida_lock, flags);
 
        if (unlikely(ret == -EAGAIN))
                goto again;
@@ -985,10 +988,12 @@ EXPORT_SYMBOL(ida_simple_get);
  */
 void ida_simple_remove(struct ida *ida, unsigned int id)
 {
+       unsigned long flags;
+
        BUG_ON((int)id < 0);
-       spin_lock(&simple_ida_lock);
+       spin_lock_irqsave(&simple_ida_lock, flags);
        ida_remove(ida, id);
-       spin_unlock(&simple_ida_lock);
+       spin_unlock_irqrestore(&simple_ida_lock, flags);
 }
 EXPORT_SYMBOL(ida_simple_remove);