Merge master.kernel.org:/pub/scm/linux/kernel/git/mchehab/v4l-dvb
[powerpc.git] / lib / radix-tree.c
index 88511c3..637d556 100644 (file)
 
 
 #ifdef __KERNEL__
-#define RADIX_TREE_MAP_SHIFT   6
+#define RADIX_TREE_MAP_SHIFT   (CONFIG_BASE_SMALL ? 4 : 6)
 #else
 #define RADIX_TREE_MAP_SHIFT   3       /* For more stressful testing */
 #endif
-#define RADIX_TREE_TAGS                2
 
 #define RADIX_TREE_MAP_SIZE    (1UL << RADIX_TREE_MAP_SHIFT)
 #define RADIX_TREE_MAP_MASK    (RADIX_TREE_MAP_SIZE-1)
@@ -48,7 +47,7 @@
 struct radix_tree_node {
        unsigned int    count;
        void            *slots[RADIX_TREE_MAP_SIZE];
-       unsigned long   tags[RADIX_TREE_TAGS][RADIX_TREE_TAG_LONGS];
+       unsigned long   tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
 };
 
 struct radix_tree_path {
@@ -75,6 +74,11 @@ struct radix_tree_preload {
 };
 DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
 
+static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
+{
+       return root->gfp_mask & __GFP_BITS_MASK;
+}
+
 /*
  * This assumes that the caller has performed appropriate preallocation, and
  * that the caller has pinned this thread of control to the current CPU.
@@ -83,9 +87,10 @@ static struct radix_tree_node *
 radix_tree_node_alloc(struct radix_tree_root *root)
 {
        struct radix_tree_node *ret;
+       gfp_t gfp_mask = root_gfp_mask(root);
 
-       ret = kmem_cache_alloc(radix_tree_node_cachep, root->gfp_mask);
-       if (ret == NULL && !(root->gfp_mask & __GFP_WAIT)) {
+       ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
+       if (ret == NULL && !(gfp_mask & __GFP_WAIT)) {
                struct radix_tree_preload *rtp;
 
                rtp = &__get_cpu_var(radix_tree_preloads);
@@ -135,20 +140,57 @@ out:
        return ret;
 }
 
-static inline void tag_set(struct radix_tree_node *node, int tag, int offset)
+static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
+               int offset)
+{
+       __set_bit(offset, node->tags[tag]);
+}
+
+static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
+               int offset)
+{
+       __clear_bit(offset, node->tags[tag]);
+}
+
+static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
+               int offset)
+{
+       return test_bit(offset, node->tags[tag]);
+}
+
+static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
+{
+       root->gfp_mask |= (1 << (tag + __GFP_BITS_SHIFT));
+}
+
+
+static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
 {
-       if (!test_bit(offset, &node->tags[tag][0]))
-               __set_bit(offset, &node->tags[tag][0]);
+       root->gfp_mask &= ~(1 << (tag + __GFP_BITS_SHIFT));
 }
 
-static inline void tag_clear(struct radix_tree_node *node, int tag, int offset)
+static inline void root_tag_clear_all(struct radix_tree_root *root)
 {
-       __clear_bit(offset, &node->tags[tag][0]);
+       root->gfp_mask &= __GFP_BITS_MASK;
 }
 
-static inline int tag_get(struct radix_tree_node *node, int tag, int offset)
+static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
 {
-       return test_bit(offset, &node->tags[tag][0]);
+       return root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
+}
+
+/*
+ * Returns 1 if any slot in the node has this tag set.
+ * Otherwise returns 0.
+ */
+static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
+{
+       int idx;
+       for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
+               if (node->tags[tag][idx])
+                       return 1;
+       }
+       return 0;
 }
 
 /*
@@ -167,7 +209,6 @@ static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
 {
        struct radix_tree_node *node;
        unsigned int height;
-       char tags[RADIX_TREE_TAGS];
        int tag;
 
        /* Figure out what the height should be.  */
@@ -180,22 +221,6 @@ static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
                goto out;
        }
 
-       /*
-        * Prepare the tag status of the top-level node for propagation
-        * into the newly-pushed top-level node(s)
-        */
-       for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
-               int idx;
-
-               tags[tag] = 0;
-               for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
-                       if (root->rnode->tags[tag][idx]) {
-                               tags[tag] = 1;
-                               break;
-                       }
-               }
-       }
-
        do {
                if (!(node = radix_tree_node_alloc(root)))
                        return -ENOMEM;
@@ -204,8 +229,8 @@ static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
                node->slots[0] = root->rnode;
 
                /* Propagate the aggregated tag info into the new root */
-               for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
-                       if (tags[tag])
+               for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
+                       if (root_tag_get(root, tag))
                                tag_set(node, tag, 0);
                }
 
@@ -234,8 +259,7 @@ int radix_tree_insert(struct radix_tree_root *root,
        int error;
 
        /* Make sure the tree is high enough.  */
-       if ((!index && !root->rnode) ||
-                       index > radix_tree_maxindex(root->height)) {
+       if (index > radix_tree_maxindex(root->height)) {
                error = radix_tree_extend(root, index);
                if (error)
                        return error;
@@ -274,8 +298,11 @@ int radix_tree_insert(struct radix_tree_root *root,
                node->slots[offset] = item;
                BUG_ON(tag_get(node, 0, offset));
                BUG_ON(tag_get(node, 1, offset));
-       } else
+       } else {
                root->rnode = item;
+               BUG_ON(root_tag_get(root, 0));
+               BUG_ON(root_tag_get(root, 1));
+       }
 
        return 0;
 }
@@ -288,9 +315,13 @@ static inline void **__lookup_slot(struct radix_tree_root *root,
        struct radix_tree_node **slot;
 
        height = root->height;
+
        if (index > radix_tree_maxindex(height))
                return NULL;
 
+       if (height == 0 && root->rnode)
+               return (void **)&root->rnode;
+
        shift = (height-1) * RADIX_TREE_MAP_SHIFT;
        slot = &root->rnode;
 
@@ -344,36 +375,41 @@ EXPORT_SYMBOL(radix_tree_lookup);
  *     @index:         index key
  *     @tag:           tag index
  *
- *     Set the search tag corresponging to @index in the radix tree.  From
+ *     Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
+ *     corresponding to @index in the radix tree.  From
  *     the root all the way down to the leaf node.
  *
  *     Returns the address of the tagged item.   Setting a tag on a not-present
  *     item is a bug.
  */
 void *radix_tree_tag_set(struct radix_tree_root *root,
-                       unsigned long index, int tag)
+                       unsigned long index, unsigned int tag)
 {
        unsigned int height, shift;
        struct radix_tree_node *slot;
 
        height = root->height;
-       if (index > radix_tree_maxindex(height))
-               return NULL;
+       BUG_ON(index > radix_tree_maxindex(height));
 
-       shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
        slot = root->rnode;
+       shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
 
        while (height > 0) {
                int offset;
 
                offset = (index >> shift) & RADIX_TREE_MAP_MASK;
-               tag_set(slot, tag, offset);
+               if (!tag_get(slot, tag, offset))
+                       tag_set(slot, tag, offset);
                slot = slot->slots[offset];
                BUG_ON(slot == NULL);
                shift -= RADIX_TREE_MAP_SHIFT;
                height--;
        }
 
+       /* set the root's tag bit */
+       if (slot && !root_tag_get(root, tag))
+               root_tag_set(root, tag);
+
        return slot;
 }
 EXPORT_SYMBOL(radix_tree_tag_set);
@@ -384,7 +420,8 @@ EXPORT_SYMBOL(radix_tree_tag_set);
  *     @index:         index key
  *     @tag:           tag index
  *
- *     Clear the search tag corresponging to @index in the radix tree.  If
+ *     Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
+ *     corresponding to @index in the radix tree.  If
  *     this causes the leaf node to have no tags set then clear the tag in the
  *     next-to-leaf node, etc.
  *
@@ -392,12 +429,11 @@ EXPORT_SYMBOL(radix_tree_tag_set);
  *     has the same return value and semantics as radix_tree_lookup().
  */
 void *radix_tree_tag_clear(struct radix_tree_root *root,
-                       unsigned long index, int tag)
+                       unsigned long index, unsigned int tag)
 {
        struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
-       struct radix_tree_node *slot;
+       struct radix_tree_node *slot = NULL;
        unsigned int height, shift;
-       void *ret = NULL;
 
        height = root->height;
        if (index > radix_tree_maxindex(height))
@@ -422,22 +458,24 @@ void *radix_tree_tag_clear(struct radix_tree_root *root,
                height--;
        }
 
-       ret = slot;
-       if (ret == NULL)
+       if (slot == NULL)
                goto out;
 
-       do {
-               int idx;
-
+       while (pathp->node) {
+               if (!tag_get(pathp->node, tag, pathp->offset))
+                       goto out;
                tag_clear(pathp->node, tag, pathp->offset);
-               for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
-                       if (pathp->node->tags[tag][idx])
-                               goto out;
-               }
+               if (any_tag_set(pathp->node, tag))
+                       goto out;
                pathp--;
-       } while (pathp->node);
+       }
+
+       /* clear the root's tag bit */
+       if (root_tag_get(root, tag))
+               root_tag_clear(root, tag);
+
 out:
-       return ret;
+       return slot;
 }
 EXPORT_SYMBOL(radix_tree_tag_clear);
 
@@ -446,16 +484,15 @@ EXPORT_SYMBOL(radix_tree_tag_clear);
  * radix_tree_tag_get - get a tag on a radix tree node
  * @root:              radix tree root
  * @index:             index key
- * @tag:               tag index
+ * @tag:               tag index (< RADIX_TREE_MAX_TAGS)
  *
  * Return values:
  *
- *  0: tag not present
- *  1: tag present, set
- * -1: tag present, unset
+ *  0: tag not present or not set
+ *  1: tag set
  */
 int radix_tree_tag_get(struct radix_tree_root *root,
-                       unsigned long index, int tag)
+                       unsigned long index, unsigned int tag)
 {
        unsigned int height, shift;
        struct radix_tree_node *slot;
@@ -465,6 +502,13 @@ int radix_tree_tag_get(struct radix_tree_root *root,
        if (index > radix_tree_maxindex(height))
                return 0;
 
+       /* check the root's tag bit */
+       if (!root_tag_get(root, tag))
+               return 0;
+
+       if (height == 0)
+               return 1;
+
        shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
        slot = root->rnode;
 
@@ -486,7 +530,7 @@ int radix_tree_tag_get(struct radix_tree_root *root,
                        int ret = tag_get(slot, tag, offset);
 
                        BUG_ON(ret && saw_unset_tag);
-                       return ret ? 1 : -1;
+                       return !!ret;
                }
                slot = slot->slots[offset];
                shift -= RADIX_TREE_MAP_SHIFT;
@@ -506,8 +550,11 @@ __lookup(struct radix_tree_root *root, void **results, unsigned long index,
        unsigned long i;
 
        height = root->height;
-       if (height == 0)
+       if (height == 0) {
+               if (root->rnode && index == 0)
+                       results[nr_found++] = root->rnode;
                goto out;
+       }
 
        shift = (height-1) * RADIX_TREE_MAP_SHIFT;
        slot = root->rnode;
@@ -588,17 +635,23 @@ EXPORT_SYMBOL(radix_tree_gang_lookup);
  */
 static unsigned int
 __lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
-       unsigned int max_items, unsigned long *next_index, int tag)
+       unsigned int max_items, unsigned long *next_index, unsigned int tag)
 {
        unsigned int nr_found = 0;
        unsigned int shift;
        unsigned int height = root->height;
        struct radix_tree_node *slot;
 
+       if (height == 0) {
+               if (root->rnode && index == 0)
+                       results[nr_found++] = root->rnode;
+               goto out;
+       }
+
        shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
        slot = root->rnode;
 
-       while (height > 0) {
+       do {
                unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
 
                for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
@@ -629,7 +682,7 @@ __lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
                }
                shift -= RADIX_TREE_MAP_SHIFT;
                slot = slot->slots[i];
-       }
+       } while (height > 0);
 out:
        *next_index = index;
        return nr_found;
@@ -642,7 +695,7 @@ out:
  *     @results:       where the results of the lookup are placed
  *     @first_index:   start the lookup from this key
  *     @max_items:     place up to this many items at *results
- *     @tag:           the tag index
+ *     @tag:           the tag index (< RADIX_TREE_MAX_TAGS)
  *
  *     Performs an index-ascending scan of the tree for present items which
  *     have the tag indexed by @tag set.  Places the items at *@results and
@@ -650,12 +703,17 @@ out:
  */
 unsigned int
 radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
-               unsigned long first_index, unsigned int max_items, int tag)
+               unsigned long first_index, unsigned int max_items,
+               unsigned int tag)
 {
        const unsigned long max_index = radix_tree_maxindex(root->height);
        unsigned long cur_index = first_index;
        unsigned int ret = 0;
 
+       /* check the root's tag bit */
+       if (!root_tag_get(root, tag))
+               return 0;
+
        while (ret < max_items) {
                unsigned int nr_found;
                unsigned long next_index;       /* Index of next search */
@@ -673,6 +731,29 @@ radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
 }
 EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
 
+/**
+ *     radix_tree_shrink    -    shrink height of a radix tree to minimal
+ *     @root           radix tree root
+ */
+static inline void radix_tree_shrink(struct radix_tree_root *root)
+{
+       /* try to shrink tree height */
+       while (root->height > 0 &&
+                       root->rnode->count == 1 &&
+                       root->rnode->slots[0]) {
+               struct radix_tree_node *to_free = root->rnode;
+
+               root->rnode = to_free->slots[0];
+               root->height--;
+               /* must only free zeroed nodes into the slab */
+               tag_clear(to_free, 0, 0);
+               tag_clear(to_free, 1, 0);
+               to_free->slots[0] = NULL;
+               to_free->count = 0;
+               radix_tree_node_free(to_free);
+       }
+}
+
 /**
  *     radix_tree_delete    -    delete an item from a radix tree
  *     @root:          radix tree root
@@ -685,81 +766,71 @@ EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
 void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
 {
        struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
-       struct radix_tree_path *orig_pathp;
-       struct radix_tree_node *slot;
+       struct radix_tree_node *slot = NULL;
        unsigned int height, shift;
-       void *ret = NULL;
-       char tags[RADIX_TREE_TAGS];
-       int nr_cleared_tags;
+       int tag;
+       int offset;
 
        height = root->height;
        if (index > radix_tree_maxindex(height))
                goto out;
 
-       shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
-       pathp->node = NULL;
        slot = root->rnode;
+       if (height == 0 && root->rnode) {
+               root_tag_clear_all(root);
+               root->rnode = NULL;
+               goto out;
+       }
 
-       for ( ; height > 0; height--) {
-               int offset;
+       shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
+       pathp->node = NULL;
 
+       do {
                if (slot == NULL)
                        goto out;
 
+               pathp++;
                offset = (index >> shift) & RADIX_TREE_MAP_MASK;
-               pathp[1].offset = offset;
-               pathp[1].node = slot;
+               pathp->offset = offset;
+               pathp->node = slot;
                slot = slot->slots[offset];
-               pathp++;
                shift -= RADIX_TREE_MAP_SHIFT;
-       }
+               height--;
+       } while (height > 0);
 
-       ret = slot;
-       if (ret == NULL)
+       if (slot == NULL)
                goto out;
 
-       orig_pathp = pathp;
-
        /*
         * Clear all tags associated with the just-deleted item
         */
-       memset(tags, 0, sizeof(tags));
-       do {
-               int tag;
-
-               nr_cleared_tags = RADIX_TREE_TAGS;
-               for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
-                       int idx;
-
-                       if (tags[tag])
-                               continue;
-
-                       tag_clear(pathp->node, tag, pathp->offset);
-
-                       for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
-                               if (pathp->node->tags[tag][idx]) {
-                                       tags[tag] = 1;
-                                       nr_cleared_tags--;
-                                       break;
-                               }
-                       }
-               }
-               pathp--;
-       } while (pathp->node && nr_cleared_tags);
+       for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
+               if (tag_get(pathp->node, tag, pathp->offset))
+                       radix_tree_tag_clear(root, index, tag);
+       }
 
        /* Now free the nodes we do not need anymore */
-       for (pathp = orig_pathp; pathp->node; pathp--) {
+       while (pathp->node) {
                pathp->node->slots[pathp->offset] = NULL;
-               if (--pathp->node->count)
+               pathp->node->count--;
+
+               if (pathp->node->count) {
+                       if (pathp->node == root->rnode)
+                               radix_tree_shrink(root);
                        goto out;
+               }
 
                /* Node with zero slots in use so free it */
                radix_tree_node_free(pathp->node);
+
+               pathp--;
        }
-       root->rnode = NULL;
+       root_tag_clear_all(root);
        root->height = 0;
+       root->rnode = NULL;
+
 out:
-       return ret;
+       return slot;
 }
 EXPORT_SYMBOL(radix_tree_delete);
 
@@ -768,17 +839,9 @@ EXPORT_SYMBOL(radix_tree_delete);
  *     @root:          radix tree root
  *     @tag:           tag to test
  */
-int radix_tree_tagged(struct radix_tree_root *root, int tag)
+int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
 {
-       int idx;
-
-       if (!root->rnode)
-               return 0;
-       for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
-               if (root->rnode->tags[tag][idx])
-                       return 1;
-       }
-       return 0;
+       return root_tag_get(root, tag);
 }
 EXPORT_SYMBOL(radix_tree_tagged);