[RFC PATCH 1/2] maple_tree: add __mtree_insert_range function

Andrew Ballance
2025-04-05T06:03:35Z

adds the __mtree_insert_range which is identical to mtree_insert_range
but does not aquire ma_lock.
This function is needed for the rust bindings for maple trees because
the locking is handled on the rust side.

Signed-off-by: Andrew Ballance <andrewjballance@gmail.com>
---
include/linux/maple_tree.h | 2 ++
lib/maple_tree.c | 37 +++++++++++++++++++++++++++++++++++++
2 files changed, 39 insertions(+)

diff --git a/include/linux/maple_tree.h b/include/linux/maple_tree.h
index cbbcd18d4186..b849d57e627e 100644
--- a/include/linux/maple_tree.h
+++ b/include/linux/maple_tree.h
@@ -329,6 +329,8 @@ int mtree_insert(struct maple_tree *mt, unsigned long index,
void *entry, gfp_t gfp);
int mtree_insert_range(struct maple_tree *mt, unsigned long first,
unsigned long last, void *entry, gfp_t gfp);
+int __mtree_insert_range(struct maple_tree *mt, unsigned long first,
+ unsigned long last, void *entry, gfp_t gfp);
int mtree_alloc_range(struct maple_tree *mt, unsigned long *startp,
void *entry, unsigned long size, unsigned long min,
unsigned long max, gfp_t gfp);
diff --git a/lib/maple_tree.c b/lib/maple_tree.c
index f7153ade1be5..e0db5d3b5254 100644
--- a/lib/maple_tree.c
+++ b/lib/maple_tree.c
@@ -6387,6 +6387,43 @@ int mtree_insert_range(struct maple_tree *mt, unsigned long first,
return ret;
}
EXPORT_SYMBOL(mtree_insert_range);
+/**
+ * __mtree_insert_range() - Insert an entry at a given range if there is no value. without locking
+ * @mt: The maple tree
+ * @first: The start of the range
+ * @last: The end of the range
+ * @entry: The entry to store
+ * @gfp: The GFP_FLAGS to use for allocations.
+ *
+ * Return: 0 on success, -EEXISTS if the range is occupied, -EINVAL on invalid
+ * request, -ENOMEM if memory could not be allocated.
+ * Note that the user needs to manually lock the tree.
+ */
+int __mtree_insert_range(struct maple_tree *mt, unsigned long first,
+ unsigned long last, void *entry, gfp_t gfp)
+{
+ MA_STATE(ms, mt, first, last);
+ int ret = 0;
+
+ if (WARN_ON_ONCE(xa_is_advanced(entry)))
+ return -EINVAL;
+
+ if (first > last)
+ return -EINVAL;
+
+retry:
+ mas_insert(&ms, entry);
+ if (mas_nomem(&ms, gfp))
+ goto retry;
+
+ if (mas_is_err(&ms))
+ ret = xa_err(ms.node);
+
+ mas_destroy(&ms);
+ return ret;
+
+}
+EXPORT_SYMBOL(__mtree_insert_range);

/**
* mtree_insert() - Insert an entry at a given index if there is no value.
--
2.49.0