maple_tree: introduce mas_wr_store_type()
authorSidhartha Kumar <sidhartha.kumar@oracle.com>
Fri, 29 Sep 2023 20:10:47 +0000 (13:10 -0700)
committerLiam R. Howlett <Liam.Howlett@oracle.com>
Sat, 20 Apr 2024 01:40:08 +0000 (21:40 -0400)
Abstract the calculation used to determine the number of nodes needed for
a store operation into a separate function mas_prealloc_calc().

Signed-off-by: Sidhartha Kumar <sidhartha.kumar@oracle.com>
lib/maple_tree.c

index 40cfda620c44bcf161a0facae5c7542e67a7d850..234fa6eedbf78bea0394b42a061a6010b8a92ed3 100644 (file)
@@ -4280,6 +4280,150 @@ static inline void mas_wr_prealloc_setup(struct ma_wr_state *wr_mas)
        wr_mas->content = mas_start(mas);
 }
 
+/**
+ * mas_prealloc_calc() - Calculate number of nodes needed for a
+ * given store oepration
+ * @mas: The maple state.
+ *
+ * Return: Number of nodes required for preallocation.
+ */
+static inline int mas_prealloc_calc(struct ma_state *mas, void *entry)
+{
+       int ret = mas_mt_height(mas) * 3 + 1;
+
+       switch (mas->store_type) {
+       case wr_invalid:
+               WARN_ON_ONCE(1);
+               break;
+       case wr_new_root:
+               ret = 1;
+               break;
+       case wr_store_root:
+               if (likely((mas->last != 0) || (mas->index != 0)))
+                       ret = 1;
+               else if (((unsigned long) (entry) & 3) == 2)
+                       ret = 1;
+               else
+                       ret = 0;
+               break;
+       case wr_spanning_store:
+               ret =  mas_mt_height(mas) * 3 + 1;
+               break;
+       case wr_split_store:
+               ret =  mas_mt_height(mas) * 2 + 1;
+               break;
+       case wr_rebalance:
+               ret =  mas_mt_height(mas) * 2 - 1;
+               break;
+       case wr_node_store:
+       case wr_bnode:
+               ret = mt_in_rcu(mas->tree) ? 1 : 0;
+               break;
+       case wr_append:
+       case wr_exact_fit:
+       case wr_slot_store:
+               ret = 0;
+       }
+
+       return ret;
+}
+
+/*
+ * mas_wr_store_type() - Set the store type for a given
+ * store operation.
+ * @wr_mas: The maple write state
+ */
+static inline void mas_wr_store_type(struct ma_wr_state *wr_mas)
+{
+       struct ma_state *mas = wr_mas->mas;
+       unsigned char new_end;
+
+       if (unlikely(mas_is_none(mas) || mas_is_ptr(mas))) {
+               mas->store_type = wr_store_root;
+               return;
+       }
+
+       if (unlikely(!mas_wr_walk(wr_mas))) {
+               mas->store_type = wr_spanning_store;
+               return;
+       }
+
+       /* At this point, we are at the leaf node that needs to be altered. */
+       mas_wr_end_piv(wr_mas);
+       if (!wr_mas->entry)
+               mas_wr_extend_null(wr_mas);
+
+       new_end = mas_wr_new_end(wr_mas);
+       if ((wr_mas->r_min == mas->index) && (wr_mas->r_max == mas->last)) {
+               mas->store_type = wr_exact_fit;
+               return;
+       }
+
+       if (unlikely(!mas->index && mas->last == ULONG_MAX)) {
+               mas->store_type = wr_new_root;
+               return;
+       }
+
+       /* Potential spanning rebalance collapsing a node */
+       if (new_end < mt_min_slots[wr_mas->type]) {
+               if (!mte_is_root(mas->node)) {
+                       mas->store_type = wr_rebalance;
+                       return;
+               }
+               mas->store_type = wr_node_store;
+               return;
+       }
+
+       if (new_end >= mt_slots[wr_mas->type]) {
+               mas->store_type = wr_split_store;
+               return;
+       }
+
+       if (!mt_in_rcu(mas->tree) && (mas->offset == mas->end)) {
+               mas->store_type = wr_append;
+               return;
+       }
+
+       if ((new_end == mas->end) && (!mt_in_rcu(mas->tree) ||
+               (wr_mas->offset_end - mas->offset == 1))) {
+               mas->store_type = wr_slot_store;
+               return;
+       }
+
+       if (mte_is_root(mas->node) || !(new_end <= mt_min_slots[wr_mas->type]) ||
+               (mas->mas_flags & MA_STATE_BULK)) {
+               mas->store_type = wr_node_store;
+               return;
+       }
+
+       mas->store_type = wr_bnode;
+}
+
+/**
+ * mas_wr_preallocate() - Preallocate enough nodes for a store operation
+ * @wr_mas: The maple write state
+ * @entry: The entry that will be stored
+ * @gfp: The GFP_FLAGS to use for allocations.
+ *
+ */
+static inline void mas_wr_preallocate(struct ma_wr_state *wr_mas, void *entry, gfp_t gfp)
+{
+       struct ma_state *mas = wr_mas->mas;
+       int request;
+
+       mas_wr_prealloc_setup(wr_mas);
+       mas_wr_store_type(wr_mas);
+       request = mas_prealloc_calc(mas, entry);
+       if (!request)
+               return;
+
+       mas_node_count_gfp(mas, request, gfp);
+       if (likely(!mas_is_err(mas)))
+               return;
+
+       mas_set_alloc_req(mas, 0);
+}
+
 /**
  * mas_insert() - Internal call to insert a value
  * @mas: The maple state
@@ -5458,69 +5602,17 @@ EXPORT_SYMBOL_GPL(mas_store_prealloc);
 int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp)
 {
        MA_WR_STATE(wr_mas, mas, entry);
-       unsigned char node_size;
-       int request = 1;
-       int ret;
-
-
-       if (unlikely(!mas->index && mas->last == ULONG_MAX))
-               goto ask_now;
-
-       mas_wr_prealloc_setup(&wr_mas);
-       /* Root expand */
-       if (unlikely(mas_is_none(mas) || mas_is_ptr(mas)))
-               goto ask_now;
-
-       if (unlikely(!mas_wr_walk(&wr_mas))) {
-               /* Spanning store, use worst case for now */
-               request = 1 + mas_mt_height(mas) * 3;
-               goto ask_now;
-       }
-
-       /* At this point, we are at the leaf node that needs to be altered. */
-       /* Exact fit, no nodes needed. */
-       if (wr_mas.r_min == mas->index && wr_mas.r_max == mas->last)
-               return 0;
-
-       mas_wr_end_piv(&wr_mas);
-       node_size = mas_wr_new_end(&wr_mas);
-
-       /* Slot store, does not require additional nodes */
-       if (node_size == mas->end) {
-               /* reuse node */
-               if (!mt_in_rcu(mas->tree))
-                       return 0;
-               /* shifting boundary */
-               if (wr_mas.offset_end - mas->offset == 1)
-                       return 0;
-       }
+       int ret = 0;
 
-       if (node_size >= mt_slots[wr_mas.type]) {
-               /* Split, worst case for now. */
-               request = 1 + mas_mt_height(mas) * 2;
-               goto ask_now;
+       mas_wr_preallocate(&wr_mas, entry, gfp);
+       if (mas_is_err(mas)) {
+               ret = xa_err(mas->node);
+               mas_destroy(mas);
+               mas_reset(mas);
+               return ret;
        }
 
-       /* New root needs a single node */
-       if (unlikely(mte_is_root(mas->node)))
-               goto ask_now;
-
-       /* Potential spanning rebalance collapsing a node, use worst-case */
-       if (node_size  - 1 <= mt_min_slots[wr_mas.type])
-               request = mas_mt_height(mas) * 2 - 1;
-
-       /* node store, slot store needs one node */
-ask_now:
-       mas_node_count_gfp(mas, request, gfp);
        mas->mas_flags |= MA_STATE_PREALLOC;
-       if (likely(!mas_is_err(mas)))
-               return 0;
-
-       mas_set_alloc_req(mas, 0);
-       ret = xa_err(mas->node);
-       mas_reset(mas);
-       mas_destroy(mas);
-       mas_reset(mas);
        return ret;
 }
 EXPORT_SYMBOL_GPL(mas_preallocate);