]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
maple_tree: Fix mas_alloc_nodes() off by one in bulk alloc
authorLiam R. Howlett <Liam.Howlett@Oracle.com>
Tue, 20 Oct 2020 16:24:51 +0000 (12:24 -0400)
committerLiam R. Howlett <Liam.Howlett@Oracle.com>
Fri, 30 Oct 2020 19:13:19 +0000 (15:13 -0400)
Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
lib/maple_tree.c

index 5ac7e7519bf39eac2d1954733e9101c71f261aba..cd0d4a84015bfce4c06b73dd4e7b0f5337164a5f 100644 (file)
@@ -18,7 +18,6 @@
 #include <trace/events/maple_tree.h>
 
 #define MA_ROOT_PARENT 1
-#define MAPLE_NODE_MAX MAPLE_NODE_MASK
 
 #define ma_parent_ptr(x) ((struct maple_pnode *)(x))
 #define ma_mnode_ptr(x) ((struct maple_node *)(x))
@@ -906,7 +905,7 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
 
        mas_set_alloc_req(mas, 0);
        if (!mas_allocated(mas) ||
-           mas->alloc->node_count == MAPLE_NODE_SLOTS - 1) {
+           mas->alloc->node_count == MAPLE_NODE_SLOTS - 2) {
                node = (struct maple_alloc *)mt_alloc_one(gfp);
                if (!node)
                        goto nomem;
@@ -924,22 +923,22 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
        success = allocated = node->total;
        while(requested) {
                void **slots = (void**)&node->slot;
-               unsigned int max = MAPLE_NODE_SLOTS - 1;
+               unsigned int max_off = MAPLE_NODE_SLOTS - 1;
 
                if (node->slot[0]) {
-                       slots = (void**)&node->slot[node->node_count + 1];
-                       max -= node->node_count;
+                       unsigned int offset = node->node_count + 1;
+                       slots = (void**)&node->slot[offset];
+                       max_off -= offset;
                }
 
-               count = min(requested, max);
+               count = min(requested, max_off);
                count = mt_alloc_bulk(gfp, count, slots);
                if (!count)
                        goto nomem;
 
+               node->node_count += count;
                if (slots == (void**)&node->slot)
-                       node->node_count = count - 1; // zero indexed.
-               else
-                       node->node_count += count;
+                       node->node_count--; // zero indexed.
 
                success += count;
                nodep = &node->slot[0];
@@ -1031,7 +1030,7 @@ int mas_entry_count(struct ma_state *mas, unsigned long nr_entries)
 
        nr_nodes = DIV_ROUND_UP(nr_entries, MAPLE_RANGE64_SLOTS); // leaves
        nr_nodes += DIV_ROUND_UP(nr_nodes, nonleaf_cap);
-       mas_node_count(mas, min(nr_nodes, (int)MAPLE_NODE_MAX));
+       mas_node_count(mas, nr_nodes);
 
        if (!mas_is_err(mas))
                return 0;