Skip to contentSkip to conversation

testing/radix-tree/maple: Hack around kfree_rcu not existing

PATCH In review Pedro Falcato 2 participants 6 messages Aug 12, 2025 – Aug 15, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 testing/radix-tree/maple: Hack around kfree_rcu not existing

liburcu doesn't have kfree_rcu (or anything similar). Despite that, we can hack around it in a trivial fashion, by adding a wrapper.

This wrapper only works for maple_nodes, and not anything else (due to us not being able to know rcu_head offsets in any way), and thus we take advantage of the type checking to avoid future silent breakage.

---
v2:
- Move kfree_rcu hack to maple-shared.h, to fix userland VMA tests, per Lorenzo

 tools/testing/shared/maple-shared.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/testing/shared/maple-shared.h +15 -0
index dc4d30f3860b..572cd2580123 100644
@@ -9,5 +9,20 @@
 #include <stdlib.h>
 #include <time.h>
 #include "linux/init.h"
+#include <linux/maple_tree.h>
+
+static inline void free_node(struct rcu_head *head)
+{
+	struct maple_node *node = container_of(head, struct maple_node, rcu);
+
+	free(node);
+}
+
+static inline void kfree_rcu_node(struct maple_node *node)
+{
+	call_rcu(&node->rcu, free_node);
+}
+
+#define kfree_rcu(ptr, memb) kfree_rcu_node(ptr)
 
 #endif /* __MAPLE_SHARED_H__ */
Signature · 1 line
2.50.1
#1 lore raw

PATCH v2 2/3 maple_tree: Use kfree_rcu in ma_free_rcu

kfree_rcu is an optimized version of call_rcu + kfree. It used to not be possible to call it on non-kmalloc objects, but this restriction was lifted ever since SLOB was dropped from the kernel, and since commit 6c6c47b063b5 ("mm, slab: call kvfree_rcu_barrier() from kmem_cache_destroy()").

Thus, replace call_rcu + mt_free_rcu with kfree_rcu.

---
 lib/maple_tree.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
lib/maple_tree.c +3 -10
index b4ee2d29d7a9..91da2d9d00c3 100644
@@ -191,13 +191,6 @@ static inline void mt_free_bulk(size_t size, void __rcu **nodes)
 	kmem_cache_free_bulk(maple_node_cache, size, (void **)nodes);
 }
 
-static void mt_free_rcu(struct rcu_head *head)
-{
-	struct maple_node *node = container_of(head, struct maple_node, rcu);
-
-	kmem_cache_free(maple_node_cache, node);
-}
-
 /*
  * ma_free_rcu() - Use rcu callback to free a maple node
  * @node: The node to free
@@ -208,7 +201,7 @@ static void mt_free_rcu(struct rcu_head *head)
 static void ma_free_rcu(struct maple_node *node)
 {
 	WARN_ON(node->parent != ma_parent_ptr(node));
-	call_rcu(&node->rcu, mt_free_rcu);
+	kfree_rcu(node, rcu);
 }
 
 static void mt_set_height(struct maple_tree *mt, unsigned char height)
@@ -5281,7 +5274,7 @@ static void mt_free_walk(struct rcu_head *head)
 	mt_free_bulk(node->slot_len, slots);
 
 free_leaf:
-	mt_free_rcu(&node->rcu);
+	mt_free_one(node);
 }
 
 static inline void __rcu **mte_destroy_descend(struct maple_enode **enode,
@@ -5365,7 +5358,7 @@ static void mt_destroy_walk(struct maple_enode *enode, struct maple_tree *mt,
 
 free_leaf:
 	if (free)
-		mt_free_rcu(&node->rcu);
+		mt_free_one(node);
 	else
 		mt_clear_meta(mt, node, node->type);
 }
Signature · 1 line
2.50.1
#2 lore raw

PATCH v2 3/3 maple_tree: Replace mt_free_one() with kfree()

In reply to Pedro Falcato

kfree() is a little shorter and works with kmem_cache_alloc'd pointers too. Also lets us remove one more helper.

---
 lib/maple_tree.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
lib/maple_tree.c +4 -9
index 91da2d9d00c3..3b756f1b67fd 100644
@@ -181,11 +181,6 @@ static inline int mt_alloc_bulk(gfp_t gfp, size_t size, void **nodes)
 	return kmem_cache_alloc_bulk(maple_node_cache, gfp, size, nodes);
 }
 
-static inline void mt_free_one(struct maple_node *node)
-{
-	kmem_cache_free(maple_node_cache, node);
-}
-
 static inline void mt_free_bulk(size_t size, void __rcu **nodes)
 {
 	kmem_cache_free_bulk(maple_node_cache, size, (void **)nodes);
@@ -5274,7 +5269,7 @@ static void mt_free_walk(struct rcu_head *head)
 	mt_free_bulk(node->slot_len, slots);
 
 free_leaf:
-	mt_free_one(node);
+	kfree(node);
 }
 
 static inline void __rcu **mte_destroy_descend(struct maple_enode **enode,
@@ -5358,7 +5353,7 @@ static void mt_destroy_walk(struct maple_enode *enode, struct maple_tree *mt,
 
 free_leaf:
 	if (free)
-		mt_free_one(node);
+		kfree(node);
 	else
 		mt_clear_meta(mt, node, node->type);
 }
@@ -5585,7 +5580,7 @@ void mas_destroy(struct ma_state *mas)
 			mt_free_bulk(count, (void __rcu **)&node->slot[1]);
 			total -= count;
 		}
-		mt_free_one(ma_mnode_ptr(node));
+		kfree(ma_mnode_ptr(node));
 		total--;
 	}
 
@@ -6630,7 +6625,7 @@ static void mas_dup_free(struct ma_state *mas)
 	}
 
 	node = mte_to_node(mas->node);
-	mt_free_one(node);
+	kfree(node);
 }
 
 /*
Signature · 1 line
2.50.1
#3 lore raw
In reply to Pedro Falcato
Quoted 7 lines · Pedro Falcato

liburcu doesn't have kfree_rcu (or anything similar). Despite that, we can hack around it in a trivial fashion, by adding a wrapper.

This wrapper only works for maple_nodes, and not anything else (due to us not being able to know rcu_head offsets in any way), and thus we take advantage of the type checking to avoid future silent breakage.

Quoted 6 lines

---
v2:
- Move kfree_rcu hack to maple-shared.h, to fix userland VMA tests, per Lorenzo

This is going to cause issues with sheaves that's probably going to go through vlastimil's branch.

We're going to either get this to go through there with some modification to avoid conflict, or we're going to hold off for now.

Quoted 32 lines
 tools/testing/shared/maple-shared.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/testing/shared/maple-shared.h +15 -0
index dc4d30f3860b..572cd2580123 100644
@@ -9,5 +9,20 @@
 #include <stdlib.h>
 #include <time.h>
 #include "linux/init.h"
+#include <linux/maple_tree.h>
+
+static inline void free_node(struct rcu_head *head)
+{
+	struct maple_node *node = container_of(head, struct maple_node, rcu);
+
+	free(node);
+}
+
+static inline void kfree_rcu_node(struct maple_node *node)
+{
+	call_rcu(&node->rcu, free_node);
+}
+
+#define kfree_rcu(ptr, memb) kfree_rcu_node(ptr)
 
 #endif /* __MAPLE_SHARED_H__ */
Signature · 1 line
2.50.1
#4 lore raw
In reply to Pedro Falcato
Quoted 9 lines · Pedro Falcato

liburcu doesn't have kfree_rcu (or anything similar). Despite that, we can hack around it in a trivial fashion, by adding a wrapper.

This wrapper only works for maple_nodes, and not anything else (due to us not being able to know rcu_head offsets in any way), and thus we take advantage of the type checking to avoid future silent breakage.

Andrew,

Please drop this patch set. We will have it go through Vlastimil's tree to avoid conflicts with other work and to maintain the userspace testing in mm-new while that happens.

Thanks,
Liam

Quoted 36 lines

---
v2:
- Move kfree_rcu hack to maple-shared.h, to fix userland VMA tests, per Lorenzo

 tools/testing/shared/maple-shared.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/testing/shared/maple-shared.h +15 -0
index dc4d30f3860b..572cd2580123 100644
@@ -9,5 +9,20 @@
 #include <stdlib.h>
 #include <time.h>
 #include "linux/init.h"
+#include <linux/maple_tree.h>
+
+static inline void free_node(struct rcu_head *head)
+{
+	struct maple_node *node = container_of(head, struct maple_node, rcu);
+
+	free(node);
+}
+
+static inline void kfree_rcu_node(struct maple_node *node)
+{
+	call_rcu(&node->rcu, free_node);
+}
+
+#define kfree_rcu(ptr, memb) kfree_rcu_node(ptr)
 
 #endif /* __MAPLE_SHARED_H__ */
Signature · 1 line
2.50.1
#5 lore raw
Quoted 12 lines · Liam R. Howlett
Quoted 9 lines · Pedro Falcato

liburcu doesn't have kfree_rcu (or anything similar). Despite that, we can hack around it in a trivial fashion, by adding a wrapper.

This wrapper only works for maple_nodes, and not anything else (due to us not being able to know rcu_head offsets in any way), and thus we take advantage of the type checking to avoid future silent breakage.

Quoted 5 lines

Andrew,

Please drop this patch set. We will have it go through Vlastimil's tree to avoid conflicts with other work and to maintain the userspace testing in mm-new while that happens.

In case this helps, Andrew, it's maple_tree-use-kfree_rcu-in-ma_free_rcu.patch and testing-radix-tree-maple-hack-around-kfree_rcu-not-existing.patch in your series file.

(Yes, I should have used a cover letter here, it would've made it easier to manage, my mistake)

Signature · 1 line
Pedro