]> Pileus Git - ~andy/linux/blobdiff - lib/idr.c
idr: fix new kernel-doc warnings
[~andy/linux] / lib / idr.c
index d66e75bfc1a085e2804592005c9dbb9f521786c8..4f82a284c6a2236aed0d0fba61a1871d35ed6707 100644 (file)
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -60,6 +60,16 @@ static int idr_max(int layers)
        return (1 << bits) - 1;
 }
 
+/*
+ * Prefix mask for an idr_layer at @layer.  For layer 0, the prefix mask is
+ * all bits except for the lower IDR_BITS.  For layer 1, 2 * IDR_BITS, and
+ * so on.
+ */
+static int idr_layer_prefix_mask(int layer)
+{
+       return ~idr_max(layer + 1);
+}
+
 static struct idr_layer *get_from_free_list(struct idr *idp)
 {
        struct idr_layer *p;
@@ -127,8 +137,10 @@ static void idr_layer_rcu_free(struct rcu_head *head)
        kmem_cache_free(idr_layer_cache, layer);
 }
 
-static inline void free_layer(struct idr_layer *p)
+static inline void free_layer(struct idr *idr, struct idr_layer *p)
 {
+       if (idr->hint && idr->hint == p)
+               RCU_INIT_POINTER(idr->hint, NULL);
        call_rcu(&p->rcu_head, idr_layer_rcu_free);
 }
 
@@ -202,7 +214,6 @@ EXPORT_SYMBOL(idr_pre_get);
  * sub_alloc - try to allocate an id without growing the tree depth
  * @idp: idr handle
  * @starting_id: id to start search at
- * @id: pointer to the allocated handle
  * @pa: idr_layer[MAX_IDR_LEVEL] used as backtrack buffer
  * @gfp_mask: allocation mask for idr_layer_alloc()
  * @layer_idr: optional idr passed to idr_layer_alloc()
@@ -272,6 +283,7 @@ static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa,
                        if (!new)
                                return -ENOMEM;
                        new->layer = l-1;
+                       new->prefix = id & idr_layer_prefix_mask(new->layer);
                        rcu_assign_pointer(p->ary[m], new);
                        p->count++;
                }
@@ -313,6 +325,7 @@ build_up:
                         * upwards.
                         */
                        p->layer++;
+                       WARN_ON_ONCE(p->prefix);
                        continue;
                }
                if (!(new = idr_layer_alloc(gfp_mask, layer_idr))) {
@@ -334,6 +347,7 @@ build_up:
                new->ary[0] = p;
                new->count = 1;
                new->layer = layers-1;
+               new->prefix = id & idr_layer_prefix_mask(new->layer);
                if (bitmap_full(p->bitmap, IDR_SIZE))
                        __set_bit(0, new->bitmap);
                p = new;
@@ -350,8 +364,12 @@ build_up:
  * @id and @pa are from a successful allocation from idr_get_empty_slot().
  * Install the user pointer @ptr and mark the slot full.
  */
-static void idr_fill_slot(void *ptr, int id, struct idr_layer **pa)
+static void idr_fill_slot(struct idr *idr, void *ptr, int id,
+                         struct idr_layer **pa)
 {
+       /* update hint used for lookup, cleared from free_layer() */
+       rcu_assign_pointer(idr->hint, pa[0]);
+
        rcu_assign_pointer(pa[0]->ary[id & IDR_MASK], (struct idr_layer *)ptr);
        pa[0]->count++;
        idr_mark_full(pa, id);
@@ -384,7 +402,7 @@ int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id)
        if (rv < 0)
                return rv == -ENOMEM ? -EAGAIN : rv;
 
-       idr_fill_slot(ptr, rv, pa);
+       idr_fill_slot(idp, ptr, rv, pa);
        *id = rv;
        return 0;
 }
@@ -491,7 +509,7 @@ int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
        if (unlikely(id > max))
                return -ENOSPC;
 
-       idr_fill_slot(ptr, id, pa);
+       idr_fill_slot(idr, ptr, id, pa);
        return id;
 }
 EXPORT_SYMBOL_GPL(idr_alloc);
@@ -528,14 +546,14 @@ static void sub_remove(struct idr *idp, int shift, int id)
                to_free = NULL;
                while(*paa && ! --((**paa)->count)){
                        if (to_free)
-                               free_layer(to_free);
+                               free_layer(idp, to_free);
                        to_free = **paa;
                        **paa-- = NULL;
                }
                if (!*paa)
                        idp->layers = 0;
                if (to_free)
-                       free_layer(to_free);
+                       free_layer(idp, to_free);
        } else
                idr_remove_warning(id);
 }
@@ -550,7 +568,7 @@ void idr_remove(struct idr *idp, int id)
        struct idr_layer *p;
        struct idr_layer *to_free;
 
-       if (WARN_ON_ONCE(id < 0))
+       if (id < 0)
                return;
 
        sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
@@ -568,7 +586,7 @@ void idr_remove(struct idr *idp, int id)
                --idp->layers;
                to_free->count = 0;
                bitmap_clear(to_free->bitmap, 0, IDR_SIZE);
-               free_layer(to_free);
+               free_layer(idp, to_free);
        }
        while (idp->id_free_cnt >= MAX_IDR_FREE) {
                p = get_from_free_list(idp);
@@ -609,7 +627,7 @@ void __idr_remove_all(struct idr *idp)
                /* Get the highest bit that the above add changed from 0->1. */
                while (n < fls(id ^ bt_mask)) {
                        if (p)
-                               free_layer(p);
+                               free_layer(idp, p);
                        n += IDR_BITS;
                        p = *--paa;
                }
@@ -642,24 +660,12 @@ void idr_destroy(struct idr *idp)
 }
 EXPORT_SYMBOL(idr_destroy);
 
-/**
- * idr_find - return pointer for given id
- * @idp: idr handle
- * @id: lookup key
- *
- * Return the pointer given the id it has been registered with.  A %NULL
- * return indicates that @id is not valid or you passed %NULL in
- * idr_get_new().
- *
- * This function can be called under rcu_read_lock(), given that the leaf
- * pointers lifetimes are correctly managed.
- */
-void *idr_find(struct idr *idp, int id)
+void *idr_find_slowpath(struct idr *idp, int id)
 {
        int n;
        struct idr_layer *p;
 
-       if (WARN_ON_ONCE(id < 0))
+       if (id < 0)
                return NULL;
 
        p = rcu_dereference_raw(idp->top);
@@ -678,7 +684,7 @@ void *idr_find(struct idr *idp, int id)
        }
        return((void *)p);
 }
-EXPORT_SYMBOL(idr_find);
+EXPORT_SYMBOL(idr_find_slowpath);
 
 /**
  * idr_for_each - iterate through all stored pointers
@@ -808,7 +814,7 @@ void *idr_replace(struct idr *idp, void *ptr, int id)
        int n;
        struct idr_layer *p, *old_p;
 
-       if (WARN_ON_ONCE(id < 0))
+       if (id < 0)
                return ERR_PTR(-EINVAL);
 
        p = idp->top;