linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] maple_tree: Use kfree_rcu in ma_free_rcu
@ 2025-07-18 17:21 Pedro Falcato
  2025-07-18 17:21 ` [PATCH 2/2] testing/radix-tree/maple: Hack around kfree_rcu not existing Pedro Falcato
  2025-08-06  1:25 ` [PATCH 1/2] maple_tree: Use kfree_rcu in ma_free_rcu Liam R. Howlett
  0 siblings, 2 replies; 8+ messages in thread
From: Pedro Falcato @ 2025-07-18 17:21 UTC (permalink / raw)
  To: Andrew Morton, Liam R. Howlett, Matthew Wilcox
  Cc: maple-tree, linux-mm, linux-kernel, Pedro Falcato

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.

Signed-off-by: Pedro Falcato <pfalcato@suse.de>
---
 lib/maple_tree.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

diff --git a/lib/maple_tree.c b/lib/maple_tree.c
index b4ee2d29d7a9..91da2d9d00c3 100644
--- a/lib/maple_tree.c
+++ b/lib/maple_tree.c
@@ -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);
 }
-- 
2.50.1



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/2] testing/radix-tree/maple: Hack around kfree_rcu not existing
  2025-07-18 17:21 [PATCH 1/2] maple_tree: Use kfree_rcu in ma_free_rcu Pedro Falcato
@ 2025-07-18 17:21 ` Pedro Falcato
  2025-07-18 18:05   ` Sidhartha Kumar
                     ` (2 more replies)
  2025-08-06  1:25 ` [PATCH 1/2] maple_tree: Use kfree_rcu in ma_free_rcu Liam R. Howlett
  1 sibling, 3 replies; 8+ messages in thread
From: Pedro Falcato @ 2025-07-18 17:21 UTC (permalink / raw)
  To: Andrew Morton, Liam R. Howlett, Matthew Wilcox
  Cc: maple-tree, linux-mm, linux-kernel, 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.

Signed-off-by: Pedro Falcato <pfalcato@suse.de>
---
 tools/testing/radix-tree/maple.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/tools/testing/radix-tree/maple.c b/tools/testing/radix-tree/maple.c
index 172700fb7784..bfdc93c36cf9 100644
--- a/tools/testing/radix-tree/maple.c
+++ b/tools/testing/radix-tree/maple.c
@@ -23,6 +23,23 @@
 #define MODULE_LICENSE(x)
 #define dump_stack()	assert(0)
 
+
+#include <linux/maple_tree.h>
+
+static void free_node(struct rcu_head *head)
+{
+	struct maple_node *node = container_of(head, struct maple_node, rcu);
+
+	free(node);
+}
+
+static void kfree_rcu_node(struct maple_node *node)
+{
+	call_rcu(&node->rcu, free_node);
+}
+
+#define kfree_rcu(ptr, memb) kfree_rcu_node(ptr)
+
 #include "../../../lib/maple_tree.c"
 #include "../../../lib/test_maple_tree.c"
 
-- 
2.50.1



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] testing/radix-tree/maple: Hack around kfree_rcu not existing
  2025-07-18 17:21 ` [PATCH 2/2] testing/radix-tree/maple: Hack around kfree_rcu not existing Pedro Falcato
@ 2025-07-18 18:05   ` Sidhartha Kumar
  2025-08-06  1:26   ` Liam R. Howlett
  2025-08-10 14:23   ` Lorenzo Stoakes
  2 siblings, 0 replies; 8+ messages in thread
From: Sidhartha Kumar @ 2025-07-18 18:05 UTC (permalink / raw)
  To: Pedro Falcato, Andrew Morton, Liam R. Howlett, Matthew Wilcox
  Cc: maple-tree, linux-mm, linux-kernel

On 7/18/25 1:21 PM, Pedro Falcato wrote:
> 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.
> 
> Signed-off-by: Pedro Falcato <pfalcato@suse.de>

Reviewed-by: Sidhartha Kumar <sidhartha.kumar@oracle.com>

> ---
>   tools/testing/radix-tree/maple.c | 17 +++++++++++++++++
>   1 file changed, 17 insertions(+)
> 
> diff --git a/tools/testing/radix-tree/maple.c b/tools/testing/radix-tree/maple.c
> index 172700fb7784..bfdc93c36cf9 100644
> --- a/tools/testing/radix-tree/maple.c
> +++ b/tools/testing/radix-tree/maple.c
> @@ -23,6 +23,23 @@
>   #define MODULE_LICENSE(x)
>   #define dump_stack()	assert(0)
>   
> +
> +#include <linux/maple_tree.h>
> +
> +static void free_node(struct rcu_head *head)
> +{
> +	struct maple_node *node = container_of(head, struct maple_node, rcu);
> +
> +	free(node);
> +}
> +
> +static void kfree_rcu_node(struct maple_node *node)
> +{
> +	call_rcu(&node->rcu, free_node);
> +}
> +
> +#define kfree_rcu(ptr, memb) kfree_rcu_node(ptr)
> +
>   #include "../../../lib/maple_tree.c"
>   #include "../../../lib/test_maple_tree.c"
>   



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] maple_tree: Use kfree_rcu in ma_free_rcu
  2025-07-18 17:21 [PATCH 1/2] maple_tree: Use kfree_rcu in ma_free_rcu Pedro Falcato
  2025-07-18 17:21 ` [PATCH 2/2] testing/radix-tree/maple: Hack around kfree_rcu not existing Pedro Falcato
@ 2025-08-06  1:25 ` Liam R. Howlett
  2025-08-06 10:20   ` Pedro Falcato
  1 sibling, 1 reply; 8+ messages in thread
From: Liam R. Howlett @ 2025-08-06  1:25 UTC (permalink / raw)
  To: Pedro Falcato
  Cc: Andrew Morton, Matthew Wilcox, maple-tree, linux-mm, linux-kernel

* Pedro Falcato <pfalcato@suse.de> [250718 13:21]:
> 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.
> 
> Signed-off-by: Pedro Falcato <pfalcato@suse.de>
> ---
>  lib/maple_tree.c | 13 +++----------
>  1 file changed, 3 insertions(+), 10 deletions(-)
> 
> diff --git a/lib/maple_tree.c b/lib/maple_tree.c
> index b4ee2d29d7a9..91da2d9d00c3 100644
> --- a/lib/maple_tree.c
> +++ b/lib/maple_tree.c
> @@ -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);

Why are we still using mt_free_one()?  Couldn't this also be dropped in
favour of kfree() or does kfree() not work for kmem_cache?

>  }
>  
>  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);
>  }
> -- 
> 2.50.1
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] testing/radix-tree/maple: Hack around kfree_rcu not existing
  2025-07-18 17:21 ` [PATCH 2/2] testing/radix-tree/maple: Hack around kfree_rcu not existing Pedro Falcato
  2025-07-18 18:05   ` Sidhartha Kumar
@ 2025-08-06  1:26   ` Liam R. Howlett
  2025-08-06 10:21     ` Pedro Falcato
  2025-08-10 14:23   ` Lorenzo Stoakes
  2 siblings, 1 reply; 8+ messages in thread
From: Liam R. Howlett @ 2025-08-06  1:26 UTC (permalink / raw)
  To: Pedro Falcato
  Cc: Andrew Morton, Matthew Wilcox, maple-tree, linux-mm, linux-kernel

* Pedro Falcato <pfalcato@suse.de> [250718 13:21]:
> 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.
> 
> Signed-off-by: Pedro Falcato <pfalcato@suse.de>

Thanks, but shouldn't this be patch 1 so that the testing continues to
work?

> ---
>  tools/testing/radix-tree/maple.c | 17 +++++++++++++++++
>  1 file changed, 17 insertions(+)
> 
> diff --git a/tools/testing/radix-tree/maple.c b/tools/testing/radix-tree/maple.c
> index 172700fb7784..bfdc93c36cf9 100644
> --- a/tools/testing/radix-tree/maple.c
> +++ b/tools/testing/radix-tree/maple.c
> @@ -23,6 +23,23 @@
>  #define MODULE_LICENSE(x)
>  #define dump_stack()	assert(0)
>  
> +
> +#include <linux/maple_tree.h>
> +
> +static void free_node(struct rcu_head *head)
> +{
> +	struct maple_node *node = container_of(head, struct maple_node, rcu);
> +
> +	free(node);
> +}
> +
> +static void kfree_rcu_node(struct maple_node *node)
> +{
> +	call_rcu(&node->rcu, free_node);
> +}
> +
> +#define kfree_rcu(ptr, memb) kfree_rcu_node(ptr)
> +
>  #include "../../../lib/maple_tree.c"
>  #include "../../../lib/test_maple_tree.c"
>  
> -- 
> 2.50.1
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] maple_tree: Use kfree_rcu in ma_free_rcu
  2025-08-06  1:25 ` [PATCH 1/2] maple_tree: Use kfree_rcu in ma_free_rcu Liam R. Howlett
@ 2025-08-06 10:20   ` Pedro Falcato
  0 siblings, 0 replies; 8+ messages in thread
From: Pedro Falcato @ 2025-08-06 10:20 UTC (permalink / raw)
  To: Liam R. Howlett, Andrew Morton, Matthew Wilcox, maple-tree,
	linux-mm, linux-kernel

On Tue, Aug 05, 2025 at 09:25:13PM -0400, Liam R. Howlett wrote:
> * Pedro Falcato <pfalcato@suse.de> [250718 13:21]:
> > 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.
> [snip]
> >  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);
> 
> Why are we still using mt_free_one()?  Couldn't this also be dropped in
> favour of kfree() or does kfree() not work for kmem_cache?

kfree() also works (since SLOB was dropped). I thought you wanted mt_free_one
for style points, but I can replace all calls with a direct kfree() if you prefer.

-- 
Pedro


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] testing/radix-tree/maple: Hack around kfree_rcu not existing
  2025-08-06  1:26   ` Liam R. Howlett
@ 2025-08-06 10:21     ` Pedro Falcato
  0 siblings, 0 replies; 8+ messages in thread
From: Pedro Falcato @ 2025-08-06 10:21 UTC (permalink / raw)
  To: Liam R. Howlett, Andrew Morton, Matthew Wilcox, maple-tree,
	linux-mm, linux-kernel

On Tue, Aug 05, 2025 at 09:26:47PM -0400, Liam R. Howlett wrote:
> * Pedro Falcato <pfalcato@suse.de> [250718 13:21]:
> > 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.
> > 
> > Signed-off-by: Pedro Falcato <pfalcato@suse.de>
> 
> Thanks, but shouldn't this be patch 1 so that the testing continues to
> work?

Yeah, that makes sense, I'll re-order these two on a possible v2.

-- 
Pedro


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] testing/radix-tree/maple: Hack around kfree_rcu not existing
  2025-07-18 17:21 ` [PATCH 2/2] testing/radix-tree/maple: Hack around kfree_rcu not existing Pedro Falcato
  2025-07-18 18:05   ` Sidhartha Kumar
  2025-08-06  1:26   ` Liam R. Howlett
@ 2025-08-10 14:23   ` Lorenzo Stoakes
  2 siblings, 0 replies; 8+ messages in thread
From: Lorenzo Stoakes @ 2025-08-10 14:23 UTC (permalink / raw)
  To: Pedro Falcato
  Cc: Andrew Morton, Liam R. Howlett, Matthew Wilcox, maple-tree,
	linux-mm, linux-kernel, Sidhartha Kumar

On Fri, Jul 18, 2025 at 06:21:37PM +0100, Pedro Falcato wrote:
> 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.
>
> Signed-off-by: Pedro Falcato <pfalcato@suse.de>

So, patch 1/2 of this series has broken the VMA tests for the same reason.

Let's fix this by putting it in a shared place.

The fix enclosed works for me (assuming the patch is reverted).

Cheers, Lorenzo

> ---
>  tools/testing/radix-tree/maple.c | 17 +++++++++++++++++
>  1 file changed, 17 insertions(+)
>
> diff --git a/tools/testing/radix-tree/maple.c b/tools/testing/radix-tree/maple.c
> index 172700fb7784..bfdc93c36cf9 100644
> --- a/tools/testing/radix-tree/maple.c
> +++ b/tools/testing/radix-tree/maple.c
> @@ -23,6 +23,23 @@
>  #define MODULE_LICENSE(x)
>  #define dump_stack()	assert(0)
>
> +
> +#include <linux/maple_tree.h>
> +
> +static void free_node(struct rcu_head *head)
> +{
> +	struct maple_node *node = container_of(head, struct maple_node, rcu);
> +
> +	free(node);
> +}
> +
> +static void kfree_rcu_node(struct maple_node *node)
> +{
> +	call_rcu(&node->rcu, free_node);
> +}
> +
> +#define kfree_rcu(ptr, memb) kfree_rcu_node(ptr)
> +
>  #include "../../../lib/maple_tree.c"
>  #include "../../../lib/test_maple_tree.c"
>
> --
> 2.50.1
>
>

----8<----
From 7bbe839b520df5e9aa350fcf7ea96a0c1806729f Mon Sep 17 00:00:00 2001
From: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Date: Sun, 10 Aug 2025 15:21:42 +0100
Subject: [PATCH] fix maple/vma tests in shared place

Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
---
 tools/testing/shared/maple-shared.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/tools/testing/shared/maple-shared.h b/tools/testing/shared/maple-shared.h
index dc4d30f3860b..572cd2580123 100644
--- a/tools/testing/shared/maple-shared.h
+++ b/tools/testing/shared/maple-shared.h
@@ -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__ */
--
2.50.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-08-10 14:24 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-18 17:21 [PATCH 1/2] maple_tree: Use kfree_rcu in ma_free_rcu Pedro Falcato
2025-07-18 17:21 ` [PATCH 2/2] testing/radix-tree/maple: Hack around kfree_rcu not existing Pedro Falcato
2025-07-18 18:05   ` Sidhartha Kumar
2025-08-06  1:26   ` Liam R. Howlett
2025-08-06 10:21     ` Pedro Falcato
2025-08-10 14:23   ` Lorenzo Stoakes
2025-08-06  1:25 ` [PATCH 1/2] maple_tree: Use kfree_rcu in ma_free_rcu Liam R. Howlett
2025-08-06 10:20   ` Pedro Falcato

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).