Skip to contentSkip to conversation

replace call_rcu by kfree_rcu for simple kmem_cache_free callback

PATCH Applied Julia Lawall 15 participants 99 messages Jun 9, 2024 – Oct 9, 2024

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

cover replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---

 arch/powerpc/kvm/book3s_mmu_hpte.c  |    8 +-------
 block/blk-ioc.c                     |    9 +--------
 drivers/net/wireguard/allowedips.c  |    9 ++-------
 fs/ecryptfs/dentry.c                |    8 +-------
 fs/nfsd/nfs4state.c                 |    9 +--------
 fs/tracefs/inode.c                  |   10 +---------
 kernel/time/posix-timers.c          |    9 +--------
 kernel/workqueue.c                  |    8 +-------
 net/bridge/br_fdb.c                 |    9 +--------
 net/can/gw.c                        |   13 +++----------
 net/ipv4/fib_trie.c                 |    8 +-------
 net/ipv4/inetpeer.c                 |    9 ++-------
 net/ipv6/ip6_fib.c                  |    9 +--------
 net/ipv6/xfrm6_tunnel.c             |    8 +-------
 net/kcm/kcmsock.c                   |   10 +---------
 net/netfilter/nf_conncount.c        |   10 +---------
 net/netfilter/nf_conntrack_expect.c |   10 +---------
 net/netfilter/xt_hashlimit.c        |    9 +--------
 18 files changed, 22 insertions(+), 143 deletions(-)
#1 lore raw

PATCH 1/14 wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 drivers/net/wireguard/allowedips.c |    9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/net/wireguard/allowedips.c +2 -7
index 0ba714ca5185..e4e1638fce1b 100644
@@ -48,11 +48,6 @@ static void push_rcu(struct allowedips_node **stack,
 	}
 }
 
-static void node_free_rcu(struct rcu_head *rcu)
-{
-	kmem_cache_free(node_cache, container_of(rcu, struct allowedips_node, rcu));
-}
-
 static void root_free_rcu(struct rcu_head *rcu)
 {
 	struct allowedips_node *node, *stack[MAX_ALLOWEDIPS_DEPTH] = {
@@ -330,13 +325,13 @@ void wg_allowedips_remove_by_peer(struct allowedips *table,
 			child = rcu_dereference_protected(
 					parent->bit[!(node->parent_bit_packed & 1)],
 					lockdep_is_held(lock));
-		call_rcu(&node->rcu, node_free_rcu);
+		kfree_rcu(node, rcu);
 		if (!free_parent)
 			continue;
 		if (child)
 			child->parent_bit_packed = parent->parent_bit_packed;
 		*(struct allowedips_node **)(parent->parent_bit_packed & ~3UL) = child;
-		call_rcu(&parent->rcu, node_free_rcu);
+		kfree_rcu(parent, rcu);
 	}
 }
 
#2 lore raw

Re: [PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Hi Julia & Vlastimil,

Quoted 3 lines · Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Thanks, I applied this to the wireguard tree, and I'll send this out as a fix for 6.10. Let me know if this is unfavorable to you and if you'd like to take this somewhere yourself, in which case I'll give you my ack.

Just a question, though, for Vlastimil -- I know that with the SLOB removal, kfree() is now allowed on kmemcache'd objects. Do you plan to do a blanket s/kmem_cache_free/kfree/g at some point, and then remove kmem_cache_free all together?

Jason

#3 lore raw

Re: [PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 11 lines · Jason A. Donenfeld

Hi Julia & Vlastimil,

Quoted 3 lines · Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Thanks, I applied this to the wireguard tree, and I'll send this out as a fix for 6.10. Let me know if this is unfavorable to you and if you'd like to take this somewhere yourself, in which case I'll give you my ack.

Please push it onward.

julia

Quoted 8 lines

Just a question, though, for Vlastimil -- I know that with the SLOB removal, kfree() is now allowed on kmemcache'd objects. Do you plan to do a blanket s/kmem_cache_free/kfree/g at some point, and then remove kmem_cache_free all together?

Jason

#4 lore raw

Re: [PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Jason A. Donenfeld
Quoted 12 lines · Jason A. Donenfeld

Hi Julia & Vlastimil,

Quoted 3 lines · Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Thanks, I applied this to the wireguard tree, and I'll send this out as a fix for 6.10. Let me know if this is unfavorable to you and if you'd like to take this somewhere yourself, in which case I'll give you my ack.

Quoted 4 lines

Just a question, though, for Vlastimil -- I know that with the SLOB removal, kfree() is now allowed on kmemcache'd objects. Do you plan to do a blanket s/kmem_cache_free/kfree/g at some point, and then remove kmem_cache_free all together?

Hmm, not really, but obligatory Cc for willy who'd love to have "one free() to rule them all" IIRC.

My current thinking is that kmem_cache_free() can save the kmem_cache lookup, or serve as a double check if debugging is enabled, and doesn't have much downside. If someone wants to not care about the kmem_cache pointer, they can use kfree(). Even convert their subsystem at will. But a mass conversion of everything would be rather lot of churn for not much of a benefit, IMHO.

#5 lore raw

Re: [PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Hi Vlastimil,

Quoted 21 lines · Vlastimil Babka
Quoted 12 lines · Jason A. Donenfeld

Hi Julia & Vlastimil,

Quoted 3 lines · Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Thanks, I applied this to the wireguard tree, and I'll send this out as a fix for 6.10. Let me know if this is unfavorable to you and if you'd like to take this somewhere yourself, in which case I'll give you my ack.

Quoted 4 lines

Just a question, though, for Vlastimil -- I know that with the SLOB removal, kfree() is now allowed on kmemcache'd objects. Do you plan to do a blanket s/kmem_cache_free/kfree/g at some point, and then remove kmem_cache_free all together?

Hmm, not really, but obligatory Cc for willy who'd love to have "one free() to rule them all" IIRC.

Quoted 6 lines

My current thinking is that kmem_cache_free() can save the kmem_cache lookup, or serve as a double check if debugging is enabled, and doesn't have much downside. If someone wants to not care about the kmem_cache pointer, they can use kfree(). Even convert their subsystem at will. But a mass conversion of everything would be rather lot of churn for not much of a benefit, IMHO.

Huh, interesting. I can see the practical sense in that, not causing unnecessary churn and such.

At the same time, this doesn't appeal much to some sort of orderly part of my mind. Either all kmalloc/kmem_cache memory is kfree()d as the rule for what is best, or a kmalloc pairs with a kfree and a kmem_cache_alloc pairs with a kmem_cache_free and that's the rule. And those can be checked and enforced and so forth. But saying, "oh, well, they might work a bit different, but whatever you want is basically fine; there's no rhyme or reason" is somehow dissatisfying. Maybe the rule is actually, "use kmem_cache_free if you can because it saves a pointer lookup, but don't go out of your way to do that and certainly don't bloat .text to make it happen," then maybe that makes sense? But I dunno, I find myself wanting a rule and consistency. (Did you find it annoying that in this paragraph, I used () on only one function mention but not on the others? If so, maybe you're like me.) Maybe I should just chill though. Anyway, only my 2¢, and my opinion here isn't worth much, so please regard this as only a gut statement from a bystander.

Jason

#6 lore raw

PATCH 2/14 net: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 net/ipv4/fib_trie.c |    8 +-------
 net/ipv4/inetpeer.c |    9 ++-------
 net/ipv6/ip6_fib.c  |    9 +--------
 3 files changed, 4 insertions(+), 22 deletions(-)
net/ipv4/fib_trie.c +1 -7
index f474106464d2..3ed92e583417 100644
@@ -292,15 +292,9 @@ static const int inflate_threshold = 50;
 static const int halve_threshold_root = 15;
 static const int inflate_threshold_root = 30;
 
-static void __alias_free_mem(struct rcu_head *head)
-{
-	struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
-	kmem_cache_free(fn_alias_kmem, fa);
-}
-
 static inline void alias_free_mem_rcu(struct fib_alias *fa)
 {
-	call_rcu(&fa->rcu, __alias_free_mem);
+	kfree_rcu(fa, rcu);
 }
 
 #define TNODE_VMALLOC_MAX \
net/ipv4/inetpeer.c +2 -7
index 5bd759963451..5ab56f4cb529 100644
@@ -128,11 +128,6 @@ static struct inet_peer *lookup(const struct inetpeer_addr *daddr,
 	return NULL;
 }
 
-static void inetpeer_free_rcu(struct rcu_head *head)
-{
-	kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
-}
-
 /* perform garbage collect on all items stacked during a lookup */
 static void inet_peer_gc(struct inet_peer_base *base,
 			 struct inet_peer *gc_stack[],
@@ -168,7 +163,7 @@ static void inet_peer_gc(struct inet_peer_base *base,
 		if (p) {
 			rb_erase(&p->rb_node, &base->rb_root);
 			base->total--;
-			call_rcu(&p->rcu, inetpeer_free_rcu);
+			kfree_rcu(p, rcu);
 		}
 	}
 }
@@ -242,7 +237,7 @@ void inet_putpeer(struct inet_peer *p)
 	WRITE_ONCE(p->dtime, (__u32)jiffies);
 
 	if (refcount_dec_and_test(&p->refcnt))
-		call_rcu(&p->rcu, inetpeer_free_rcu);
+		kfree_rcu(p, rcu);
 }
 EXPORT_SYMBOL_GPL(inet_putpeer);
 
net/ipv6/ip6_fib.c +1 -8
index d9c9a542a414..bf6908010641 100644
@@ -198,16 +198,9 @@ static void node_free_immediate(struct net *net, struct fib6_node *fn)
 	net->ipv6.rt6_stats->fib_nodes--;
 }
 
-static void node_free_rcu(struct rcu_head *head)
-{
-	struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
-
-	kmem_cache_free(fib6_node_kmem, fn);
-}
-
 static void node_free(struct net *net, struct fib6_node *fn)
 {
-	call_rcu(&fn->rcu, node_free_rcu);
+	kfree_rcu(fn, rcu);
 	net->ipv6.rt6_stats->fib_nodes--;
 }
 
#7 lore raw

PATCH 3/14 KVM: PPC: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 arch/powerpc/kvm/book3s_mmu_hpte.c |    8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
arch/powerpc/kvm/book3s_mmu_hpte.c +1 -7
index ce79ac33e8d3..d904e13e069b 100644
@@ -92,12 +92,6 @@ void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
 	spin_unlock(&vcpu3s->mmu_lock);
 }
 
-static void free_pte_rcu(struct rcu_head *head)
-{
-	struct hpte_cache *pte = container_of(head, struct hpte_cache, rcu_head);
-	kmem_cache_free(hpte_cache, pte);
-}
-
 static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
 {
 	struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
@@ -126,7 +120,7 @@ static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
 
 	spin_unlock(&vcpu3s->mmu_lock);
 
-	call_rcu(&pte->rcu_head, free_pte_rcu);
+	kfree_rcu(pte, rcu_head);
 }
 
 static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu)
#8 lore raw

PATCH 4/14 xfrm6_tunnel: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 net/ipv6/xfrm6_tunnel.c |    8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
net/ipv6/xfrm6_tunnel.c +1 -7
index bf140ef781c1..c3c893ddb6ee 100644
@@ -178,12 +178,6 @@ __be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr)
 }
 EXPORT_SYMBOL(xfrm6_tunnel_alloc_spi);
 
-static void x6spi_destroy_rcu(struct rcu_head *head)
-{
-	kmem_cache_free(xfrm6_tunnel_spi_kmem,
-			container_of(head, struct xfrm6_tunnel_spi, rcu_head));
-}
-
 static void xfrm6_tunnel_free_spi(struct net *net, xfrm_address_t *saddr)
 {
 	struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
@@ -200,7 +194,7 @@ static void xfrm6_tunnel_free_spi(struct net *net, xfrm_address_t *saddr)
 			if (refcount_dec_and_test(&x6spi->refcnt)) {
 				hlist_del_rcu(&x6spi->list_byaddr);
 				hlist_del_rcu(&x6spi->list_byspi);
-				call_rcu(&x6spi->rcu_head, x6spi_destroy_rcu);
+				kfree_rcu(x6spi, rcu_head);
 				break;
 			}
 		}
#9 lore raw

PATCH 5/14 tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 fs/tracefs/inode.c |   10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
fs/tracefs/inode.c +1 -9
index 7c29f4afc23d..338c52168e61 100644
@@ -53,14 +53,6 @@ static struct inode *tracefs_alloc_inode(struct super_block *sb)
 	return &ti->vfs_inode;
 }
 
-static void tracefs_free_inode_rcu(struct rcu_head *rcu)
-{
-	struct tracefs_inode *ti;
-
-	ti = container_of(rcu, struct tracefs_inode, rcu);
-	kmem_cache_free(tracefs_inode_cachep, ti);
-}
-
 static void tracefs_free_inode(struct inode *inode)
 {
 	struct tracefs_inode *ti = get_tracefs(inode);
@@ -70,7 +62,7 @@ static void tracefs_free_inode(struct inode *inode)
 	list_del_rcu(&ti->list);
 	spin_unlock_irqrestore(&tracefs_inode_lock, flags);
 
-	call_rcu(&ti->rcu, tracefs_free_inode_rcu);
+	kfree_rcu(ti, rcu);
 }
 
 static ssize_t default_read_file(struct file *file, char __user *buf,
maintainer
#10 lore raw

Re: [PATCH 05/14] tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 5 lines · On Sun, 9 Jun 2024 10:27:17 +0200 Julia Lawall <[email protected]> wrote:
fs/tracefs/inode.c +0 -0
index 7c29f4afc23d..338c52168e61 100644

fs/tracefs/inode.c static struct inode *tracefs_alloc_inode(struct super_block *sb) · line 61

Quoted 9 lines
fs/tracefs/inode.c +0 -6
@@ -53,14 +53,6 @@ static struct inode *tracefs_alloc_inode(struct super_block *sb)
 	return &ti->vfs_inode;
 }
 
-static void tracefs_free_inode_rcu(struct rcu_head *rcu)
-{
-	struct tracefs_inode *ti;
-
-	ti = container_of(rcu, struct tracefs_inode, rcu);
-	kmem_cache_free(tracefs_inode_cachep, ti);

Does this work?

tracefs needs to be freed via the tracefs_inode_cachep. Does kfree_rcu() handle specific frees for objects that were not allocated via kmalloc()?

-- Steve

Quoted 14 lines
fs/tracefs/inode.c +1 -3
-}
-
 static void tracefs_free_inode(struct inode *inode)
 {
 	struct tracefs_inode *ti = get_tracefs(inode);
@@ -70,7 +62,7 @@ static void tracefs_free_inode(struct inode *inode)
 	list_del_rcu(&ti->list);
 	spin_unlock_irqrestore(&tracefs_inode_lock, flags);
 
-	call_rcu(&ti->rcu, tracefs_free_inode_rcu);
+	kfree_rcu(ti, rcu);
 }
 
 static ssize_t default_read_file(struct file *file, char __user *buf,
#11 lore raw

Re: [PATCH 05/14] tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 20 lines · Steven Rostedt
Quoted 5 lines · On Sun, 9 Jun 2024 10:27:17 +0200 Julia Lawall <[email protected]> wrote:
fs/tracefs/inode.c +0 -0
index 7c29f4afc23d..338c52168e61 100644

fs/tracefs/inode.c static struct inode *tracefs_alloc_inode(struct super_block *sb) · line 61

Quoted 9 lines
fs/tracefs/inode.c +0 -6
@@ -53,14 +53,6 @@ static struct inode *tracefs_alloc_inode(struct super_block *sb)
 	return &ti->vfs_inode;
 }
 
-static void tracefs_free_inode_rcu(struct rcu_head *rcu)
-{
-	struct tracefs_inode *ti;
-
-	ti = container_of(rcu, struct tracefs_inode, rcu);
-	kmem_cache_free(tracefs_inode_cachep, ti);

Does this work?

Quoted 3 lines

tracefs needs to be freed via the tracefs_inode_cachep. Does kfree_rcu() handle specific frees for objects that were not allocated via kmalloc()?

A recent change to kfree() allows it to correctly handle memory allocated via kmem_cache_alloc(). News to me as of a few weeks ago. ;-)

Thanx, Paul

Quoted 18 lines

-- Steve

Quoted 14 lines
fs/tracefs/inode.c +1 -3
-}
-
 static void tracefs_free_inode(struct inode *inode)
 {
 	struct tracefs_inode *ti = get_tracefs(inode);
@@ -70,7 +62,7 @@ static void tracefs_free_inode(struct inode *inode)
 	list_del_rcu(&ti->list);
 	spin_unlock_irqrestore(&tracefs_inode_lock, flags);
 
-	call_rcu(&ti->rcu, tracefs_free_inode_rcu);
+	kfree_rcu(ti, rcu);
 }
 
 static ssize_t default_read_file(struct file *file, char __user *buf,
maintainer Acked-by
#12 lore raw

Re: [PATCH 05/14] tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 16 lines · On Mon, 10 Jun 2024 08:46:42 -0700 "Paul E. McKenney" <[email protected]>…
Quoted 16 lines

fs/tracefs/inode.c static struct inode *tracefs_alloc_inode(struct super_block *sb) · line 61

Quoted 13 lines

index 7c29f4afc23d..338c52168e61 100644

fs/tracefs/inode.c +0 -6
@@ -53,14 +53,6 @@ static struct inode *tracefs_alloc_inode(struct super_block *sb)
 	return &ti->vfs_inode;
 }
 
-static void tracefs_free_inode_rcu(struct rcu_head *rcu)
-{
-	struct tracefs_inode *ti;
-
-	ti = container_of(rcu, struct tracefs_inode, rcu);
-	kmem_cache_free(tracefs_inode_cachep, ti);  

Does this work?

Quoted 6 lines
Quoted 3 lines

tracefs needs to be freed via the tracefs_inode_cachep. Does kfree_rcu() handle specific frees for objects that were not allocated via kmalloc()?

A recent change to kfree() allows it to correctly handle memory allocated via kmem_cache_alloc(). News to me as of a few weeks ago. ;-)

If that's the case then:

Do we have a way to add a "Depends-on" tag so that anyone backporting this will know that it requires the change to whatever allowed that to happen?

Or we need to update the change log to explicitly state that this should *not* be backported.

-- Steve

#13 lore raw

Re: [PATCH 05/14] tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 26 lines · Steven Rostedt
Quoted 16 lines · On Mon, 10 Jun 2024 08:46:42 -0700 "Paul E. McKenney" <[email protected]>…
Quoted 16 lines

fs/tracefs/inode.c static struct inode *tracefs_alloc_inode(struct super_block *sb) · line 61

Quoted 13 lines

index 7c29f4afc23d..338c52168e61 100644

fs/tracefs/inode.c +0 -6
@@ -53,14 +53,6 @@ static struct inode *tracefs_alloc_inode(struct super_block *sb)
 	return &ti->vfs_inode;
 }
 
-static void tracefs_free_inode_rcu(struct rcu_head *rcu)
-{
-	struct tracefs_inode *ti;
-
-	ti = container_of(rcu, struct tracefs_inode, rcu);
-	kmem_cache_free(tracefs_inode_cachep, ti);  

Does this work?

Quoted 6 lines
Quoted 3 lines

tracefs needs to be freed via the tracefs_inode_cachep. Does kfree_rcu() handle specific frees for objects that were not allocated via kmalloc()?

A recent change to kfree() allows it to correctly handle memory allocated via kmem_cache_alloc(). News to me as of a few weeks ago. ;-)

Quoted 6 lines

If that's the case then:

Do we have a way to add a "Depends-on" tag so that anyone backporting this will know that it requires the change to whatever allowed that to happen?

Looks like people use that tag, although no grep hits in Documentation, so Cc'ing workflows@ and Thorsten.

In this case it would be

Depends-on: c9929f0e344a ("mm/slob: remove CONFIG_SLOB")

Quoted 4 lines

Or we need to update the change log to explicitly state that this should *not* be backported.

-- Steve

#14 lore raw

Re: [PATCH 05/14] tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 34 lines · Vlastimil Babka
Quoted 26 lines · Steven Rostedt
Quoted 16 lines · On Mon, 10 Jun 2024 08:46:42 -0700 "Paul E. McKenney" <[email protected]>…
Quoted 16 lines

fs/tracefs/inode.c static struct inode *tracefs_alloc_inode(struct super_block *sb) · line 61

Quoted 13 lines

index 7c29f4afc23d..338c52168e61 100644

fs/tracefs/inode.c +0 -6
@@ -53,14 +53,6 @@ static struct inode *tracefs_alloc_inode(struct super_block *sb)
 	return &ti->vfs_inode;
 }
 
-static void tracefs_free_inode_rcu(struct rcu_head *rcu)
-{
-	struct tracefs_inode *ti;
-
-	ti = container_of(rcu, struct tracefs_inode, rcu);
-	kmem_cache_free(tracefs_inode_cachep, ti);  

Does this work?

Quoted 6 lines
Quoted 3 lines

tracefs needs to be freed via the tracefs_inode_cachep. Does kfree_rcu() handle specific frees for objects that were not allocated via kmalloc()?

A recent change to kfree() allows it to correctly handle memory allocated via kmem_cache_alloc(). News to me as of a few weeks ago. ;-)

Quoted 6 lines

If that's the case then:

Do we have a way to add a "Depends-on" tag so that anyone backporting this will know that it requires the change to whatever allowed that to happen?

Quoted 6 lines

Looks like people use that tag, although no grep hits in Documentation, so Cc'ing workflows@ and Thorsten.

In this case it would be

Depends-on: c9929f0e344a ("mm/slob: remove CONFIG_SLOB")

Ick, no, use the documented way of handling this as described in the stable kernel rules file.

thanks,

greg k-h

#15 lore raw

Re: [PATCH 05/14] tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 38 lines · Greg KH
Quoted 37 lines · Vlastimil Babka
Quoted 26 lines · Steven Rostedt
Quoted 16 lines · On Mon, 10 Jun 2024 08:46:42 -0700 "Paul E. McKenney" <[email protected]>…
Quoted 16 lines

fs/tracefs/inode.c static struct inode *tracefs_alloc_inode(struct super_block *sb) · line 61

Quoted 13 lines

index 7c29f4afc23d..338c52168e61 100644

fs/tracefs/inode.c +0 -6
@@ -53,14 +53,6 @@ static struct inode *tracefs_alloc_inode(struct super_block *sb)
 	return &ti->vfs_inode;
 }
 
-static void tracefs_free_inode_rcu(struct rcu_head *rcu)
-{
-	struct tracefs_inode *ti;
-
-	ti = container_of(rcu, struct tracefs_inode, rcu);
-	kmem_cache_free(tracefs_inode_cachep, ti);  

Does this work?

Quoted 6 lines
Quoted 3 lines

tracefs needs to be freed via the tracefs_inode_cachep. Does kfree_rcu() handle specific frees for objects that were not allocated via kmalloc()?

A recent change to kfree() allows it to correctly handle memory allocated via kmem_cache_alloc(). News to me as of a few weeks ago. ;-)

Quoted 6 lines

If that's the case then:

Do we have a way to add a "Depends-on" tag so that anyone backporting this will know that it requires the change to whatever allowed that to happen?

Looks like people use that tag, although no grep hits in Documentation, so Cc'ing workflows@ and Thorsten.

Quoted 6 lines
Quoted 3 lines

In this case it would be

Depends-on: c9929f0e344a ("mm/slob: remove CONFIG_SLOB")

Ick, no, use the documented way of handling this as described in the stable kernel rules file.

AFAICS that documented way is for a different situation? I assume you mean this part:

* Specify any additional patch prerequisites for cherry picking::

    Cc: <[email protected]> # 3.3.x: a1f84a3: sched: Check for idle

But that would assume we actively want to backport this cleanup patch in the first place. But as I understand Steven's intention, we want just to make sure that if in the future this patch is backported (i.e. as a dependency of something else) it won't be forgotten to also backport c9929f0e344a ("mm/slob: remove CONFIG_SLOB"). How to express that without actively marking this patch for backport at the same time?

Quoted 3 lines

thanks,

greg k-h

#16 lore raw

Re: [PATCH 05/14] tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 51 lines · Vlastimil Babka
Quoted 38 lines · Greg KH
Quoted 37 lines · Vlastimil Babka
Quoted 26 lines · Steven Rostedt
Quoted 16 lines · On Mon, 10 Jun 2024 08:46:42 -0700 "Paul E. McKenney" <[email protected]>…
Quoted 16 lines

fs/tracefs/inode.c static struct inode *tracefs_alloc_inode(struct super_block *sb) · line 61

Quoted 13 lines

index 7c29f4afc23d..338c52168e61 100644

fs/tracefs/inode.c +0 -6
@@ -53,14 +53,6 @@ static struct inode *tracefs_alloc_inode(struct super_block *sb)
 	return &ti->vfs_inode;
 }
 
-static void tracefs_free_inode_rcu(struct rcu_head *rcu)
-{
-	struct tracefs_inode *ti;
-
-	ti = container_of(rcu, struct tracefs_inode, rcu);
-	kmem_cache_free(tracefs_inode_cachep, ti);  

Does this work?

Quoted 6 lines
Quoted 3 lines

tracefs needs to be freed via the tracefs_inode_cachep. Does kfree_rcu() handle specific frees for objects that were not allocated via kmalloc()?

A recent change to kfree() allows it to correctly handle memory allocated via kmem_cache_alloc(). News to me as of a few weeks ago. ;-)

Quoted 6 lines

If that's the case then:

Do we have a way to add a "Depends-on" tag so that anyone backporting this will know that it requires the change to whatever allowed that to happen?

Looks like people use that tag, although no grep hits in Documentation, so Cc'ing workflows@ and Thorsten.

Quoted 6 lines
Quoted 3 lines

In this case it would be

Depends-on: c9929f0e344a ("mm/slob: remove CONFIG_SLOB")

Ick, no, use the documented way of handling this as described in the stable kernel rules file.

AFAICS that documented way is for a different situation? I assume you mean this part:

* Specify any additional patch prerequisites for cherry picking::

Quoted 8 lines
    Cc: <[email protected]> # 3.3.x: a1f84a3: sched: Check for idle

But that would assume we actively want to backport this cleanup patch in the first place. But as I understand Steven's intention, we want just to make sure that if in the future this patch is backported (i.e. as a dependency of something else) it won't be forgotten to also backport c9929f0e344a ("mm/slob: remove CONFIG_SLOB"). How to express that without actively marking this patch for backport at the same time?

Hah, waiting a bit spared me the time to write a similar reply. :-D Writing one now anyway to broaden the scope:

I recently noticed we have the same problem when it comes to the "delayed backporting" aspect, e.g. this part:

"""
* Delay pick up of patches::

Cc: <[email protected]> # after -rc3
"""

I'll bring this up in a maintainers summit proposal I'm currently preparing. But I have no idea how to solve this in an elegant way. "Cc: <[email protected]> # after -rc3" could work, but well, as indicated, that's kinda ugly.

Ciao, Thorsten

maintainer
#17 lore raw

Re: [PATCH 05/14] tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Vlastimil Babka
Quoted 5 lines · On Tue, 11 Jun 2024 10:42:28 +0200 Vlastimil Babka <[email protected]> wrote:

AFAICS that documented way is for a different situation? I assume you mean this part:

* Specify any additional patch prerequisites for cherry picking::

Quoted 8 lines
    Cc: <[email protected]> # 3.3.x: a1f84a3: sched: Check for idle

But that would assume we actively want to backport this cleanup patch in the first place. But as I understand Steven's intention, we want just to make sure that if in the future this patch is backported (i.e. as a dependency of something else) it won't be forgotten to also backport c9929f0e344a ("mm/slob: remove CONFIG_SLOB"). How to express that without actively marking this patch for backport at the same time?

Exactly! This isn't to be tagged as stable. It's just a way to say "if you need this patch for any reason, you also need patch X".

I think "Depends-on" is the way to go, as it is *not* a stable thing, and what is in stable rules is only about stable patches.

-- Steve

#18 lore raw

Re: [PATCH 05/14] tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 17 lines · Steven Rostedt
Quoted 5 lines · On Tue, 11 Jun 2024 10:42:28 +0200 Vlastimil Babka <[email protected]> wrote:

AFAICS that documented way is for a different situation? I assume you mean this part:

* Specify any additional patch prerequisites for cherry picking::

Quoted 8 lines
    Cc: <[email protected]> # 3.3.x: a1f84a3: sched: Check for idle

But that would assume we actively want to backport this cleanup patch in the first place. But as I understand Steven's intention, we want just to make sure that if in the future this patch is backported (i.e. as a dependency of something else) it won't be forgotten to also backport c9929f0e344a ("mm/slob: remove CONFIG_SLOB"). How to express that without actively marking this patch for backport at the same time?

Quoted 5 lines

Exactly! This isn't to be tagged as stable. It's just a way to say "if you need this patch for any reason, you also need patch X".

I think "Depends-on" is the way to go, as it is *not* a stable thing, and what is in stable rules is only about stable patches.

How does "Depends-on" not spiral out of control? There's a *lot* of "Depends-on" relations one could express in commit series and such. Of course a lot of git itself is designed to show some subset of these relationships.

It seems like in most cases, the "Cc: [email protected] # x.y.z+" notation expresses the backporting safety correctly. What is the purpose of saying, "if you need this patch for any reason, you also need patch X"? Who is the intended audience, and are you sure they need this?

I ask these questions because I wind up doing a lot of work backporting patches to stable and marking things properly for that or submitting manually backported stable patches and so forth, and in general, patch applicability for stable things is something I wind up devoting a lot of time to. If I have to *additionally* start caring about the theoretical possibility that somebody in the future, outside of the stable flow, might not understand the context of a given patch and blindly apply it to some random tree here or there, that sounds like a lot of extra brain cycles to consider.

So, is this actually necessary, and how does it not spiral out of control?

maintainer
#19 lore raw

Re: [PATCH 05/14] tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 8 lines · On Wed, 12 Jun 2024 16:09:40 +0200 "Jason A. Donenfeld" <[email protected]> wrote:
Quoted 3 lines

I think "Depends-on" is the way to go, as it is *not* a stable thing, and what is in stable rules is only about stable patches.

How does "Depends-on" not spiral out of control? There's a *lot* of "Depends-on" relations one could express in commit series and such. Of course a lot of git itself is designed to show some subset of these relationships.

If a change occurs because a recent change happened that allows the current change to work, then I think a Depends-on is appropriate.

Like in this example. I thought this change was broken, and it would have been except for a recent change. Having the dependency listed is useful, especially if the dependency is subtle (doesn't break the build and may not show the bug immediately).

Quoted 5 lines

It seems like in most cases, the "Cc: [email protected] # x.y.z+" notation expresses the backporting safety correctly. What is the purpose of saying, "if you need this patch for any reason, you also need patch X"? Who is the intended audience, and are you sure they need this?

The intended audience is someone backporting features and not fixes.

Quoted 11 lines

I ask these questions because I wind up doing a lot of work backporting patches to stable and marking things properly for that or submitting manually backported stable patches and so forth, and in general, patch applicability for stable things is something I wind up devoting a lot of time to. If I have to *additionally* start caring about the theoretical possibility that somebody in the future, outside of the stable flow, might not understand the context of a given patch and blindly apply it to some random tree here or there, that sounds like a lot of extra brain cycles to consider.

Quoted 2 lines

So, is this actually necessary, and how does it not spiral out of control?

How would you see it going out of control? And "Depends-on" would only be used for non stable relationships. If stable backports, we can keep with the current method.

-- Steve

maintainer
#20 lore raw

Re: [PATCH 05/14] tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Greg KH
Quoted 4 lines · On Tue, 11 Jun 2024 08:23:11 +0200 Greg KH <[email protected]> wrote:
Quoted 1 line

Depends-on: c9929f0e344a ("mm/slob: remove CONFIG_SLOB")

Ick, no, use the documented way of handling this as described in the stable kernel rules file.

You mentioned this before, I guess you mean this:

Quoted 22 lines

To send additional instructions to the stable team, use a shell-style inline comment to pass arbitrary or predefined notes:

* Specify any additional patch prerequisites for cherry picking::

    Cc: <[email protected]> # 3.3.x: a1f84a3: sched: Check for idle
    Cc: <[email protected]> # 3.3.x: 1b9508f: sched: Rate-limit newidle
    Cc: <[email protected]> # 3.3.x: fd21073: sched: Fix affinity logic
    Cc: <[email protected]> # 3.3.x
    Signed-off-by: Ingo Molnar <[email protected]>

The tag sequence has the meaning of::

git cherry-pick a1f84a3
git cherry-pick 1b9508f
git cherry-pick fd21073
git cherry-pick <this commit>

Note that for a patch series, you do not have to list as prerequisites the patches present in the series itself. For example, if you have the following patch series::

Quoted 6 lines

patch1
patch2

where patch2 depends on patch1, you do not have to list patch1 as prerequisite of patch2 if you have already marked patch1 for stable inclusion.

What's with the "3.3.x"? Isn't that obsolete? And honestly, I find the above much more "ick" than "Depends-on:". That's because I like to read human readable tags and not machine processing tags. I'm a human, not a machine.

-- Steve

#21 lore raw

Re: [PATCH 05/14] tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Paul E. McKenney
Quoted 21 lines · Paul E. McKenney
Quoted 20 lines · Steven Rostedt
Quoted 5 lines · On Sun, 9 Jun 2024 10:27:17 +0200 Julia Lawall <[email protected]> wrote:
fs/tracefs/inode.c +0 -0
index 7c29f4afc23d..338c52168e61 100644

fs/tracefs/inode.c static struct inode *tracefs_alloc_inode(struct super_block *sb) · line 61

Quoted 9 lines
fs/tracefs/inode.c +0 -6
@@ -53,14 +53,6 @@ static struct inode *tracefs_alloc_inode(struct super_block *sb)
 	return &ti->vfs_inode;
 }
 
-static void tracefs_free_inode_rcu(struct rcu_head *rcu)
-{
-	struct tracefs_inode *ti;
-
-	ti = container_of(rcu, struct tracefs_inode, rcu);
-	kmem_cache_free(tracefs_inode_cachep, ti);

Does this work?

Quoted 6 lines
Quoted 3 lines

tracefs needs to be freed via the tracefs_inode_cachep. Does kfree_rcu() handle specific frees for objects that were not allocated via kmalloc()?

A recent change to kfree() allows it to correctly handle memory allocated via kmem_cache_alloc(). News to me as of a few weeks ago. ;-)

Hey, I did try not to keep that a secret :)
https://lore.kernel.org/all/[email protected]/

Quoted 20 lines

Thanx, Paul

Quoted 18 lines

-- Steve

Quoted 14 lines
fs/tracefs/inode.c +1 -3
-}
-
 static void tracefs_free_inode(struct inode *inode)
 {
 	struct tracefs_inode *ti = get_tracefs(inode);
@@ -70,7 +62,7 @@ static void tracefs_free_inode(struct inode *inode)
 	list_del_rcu(&ti->list);
 	spin_unlock_irqrestore(&tracefs_inode_lock, flags);
 
-	call_rcu(&ti->rcu, tracefs_free_inode_rcu);
+	kfree_rcu(ti, rcu);
 }
 
 static ssize_t default_read_file(struct file *file, char __user *buf,
maintainer
#22 lore raw

Re: [PATCH 05/14] tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 26 lines · On Mon, 10 Jun 2024 22:42:30 +0200 Vlastimil Babka <[email protected]> wrote:
Quoted 25 lines · Paul E. McKenney
Quoted 23 lines · Steven Rostedt
Quoted 5 lines · On Sun, 9 Jun 2024 10:27:17 +0200 Julia Lawall <[email protected]> wrote:
fs/tracefs/inode.c +0 -0
index 7c29f4afc23d..338c52168e61 100644

fs/tracefs/inode.c static struct inode *tracefs_alloc_inode(struct super_block *sb) · line 61

Quoted 9 lines
fs/tracefs/inode.c +0 -6
@@ -53,14 +53,6 @@ static struct inode *tracefs_alloc_inode(struct super_block *sb)
 	return &ti->vfs_inode;
 }
 
-static void tracefs_free_inode_rcu(struct rcu_head *rcu)
-{
-	struct tracefs_inode *ti;
-
-	ti = container_of(rcu, struct tracefs_inode, rcu);
-	kmem_cache_free(tracefs_inode_cachep, ti);  

Does this work?

tracefs needs to be freed via the tracefs_inode_cachep. Does kfree_rcu() handle specific frees for objects that were not allocated via kmalloc()?

Quoted 6 lines
Quoted 2 lines

A recent change to kfree() allows it to correctly handle memory allocated via kmem_cache_alloc(). News to me as of a few weeks ago. ;-)

Hey, I did try not to keep that a secret :)
https://lore.kernel.org/all/[email protected]/

Heh, I didn't look at that patch very deeply.

-- Steve

#23 lore raw

PATCH 6/14 eCryptfs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 fs/ecryptfs/dentry.c |    8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
fs/ecryptfs/dentry.c +1 -7
index acaa0825e9bb..49d626ff33a9 100644
@@ -51,12 +51,6 @@ static int ecryptfs_d_revalidate(struct dentry *dentry, unsigned int flags)
 
 struct kmem_cache *ecryptfs_dentry_info_cache;
 
-static void ecryptfs_dentry_free_rcu(struct rcu_head *head)
-{
-	kmem_cache_free(ecryptfs_dentry_info_cache,
-		container_of(head, struct ecryptfs_dentry_info, rcu));
-}
-
 /**
  * ecryptfs_d_release
  * @dentry: The ecryptfs dentry
@@ -68,7 +62,7 @@ static void ecryptfs_d_release(struct dentry *dentry)
 	struct ecryptfs_dentry_info *p = dentry->d_fsdata;
 	if (p) {
 		path_put(&p->lower_path);
-		call_rcu(&p->rcu, ecryptfs_dentry_free_rcu);
+		kfree_rcu(p, rcu);
 	}
 }
 
#24 lore raw

PATCH 7/14 net: bridge: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 net/bridge/br_fdb.c |    9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
net/bridge/br_fdb.c +1 -8
index c77591e63841..6d04b48f7e2c 100644
@@ -73,13 +73,6 @@ static inline int has_expired(const struct net_bridge *br,
 	       time_before_eq(fdb->updated + hold_time(br), jiffies);
 }
 
-static void fdb_rcu_free(struct rcu_head *head)
-{
-	struct net_bridge_fdb_entry *ent
-		= container_of(head, struct net_bridge_fdb_entry, rcu);
-	kmem_cache_free(br_fdb_cache, ent);
-}
-
 static int fdb_to_nud(const struct net_bridge *br,
 		      const struct net_bridge_fdb_entry *fdb)
 {
@@ -329,7 +322,7 @@ static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
 	if (test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED, &f->flags))
 		atomic_dec(&br->fdb_n_learned);
 	fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
-	call_rcu(&f->rcu, fdb_rcu_free);
+	kfree_rcu(f, rcu);
 }
 
 /* Delete a local entry if no other port had the same address.
maintainer Acked-by on v1 7/14
#25 lore raw

Re: [PATCH 07/14] net: bridge: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 8 lines · Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

[snip]

Quoted 9 lines
---
 net/bridge/br_fdb.c |    9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

Thanks,

#26 lore raw

PATCH 8/14 nfsd: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 fs/nfsd/nfs4state.c |    9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
fs/nfsd/nfs4state.c +1 -8
index a20c2c9d7d45..eba5083504c7 100644
@@ -571,13 +571,6 @@ opaque_hashval(const void *ptr, int nbytes)
 	return x;
 }
 
-static void nfsd4_free_file_rcu(struct rcu_head *rcu)
-{
-	struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
-
-	kmem_cache_free(file_slab, fp);
-}
-
 void
 put_nfs4_file(struct nfs4_file *fi)
 {
@@ -585,7 +578,7 @@ put_nfs4_file(struct nfs4_file *fi)
 		nfsd4_file_hash_remove(fi);
 		WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
 		WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
-		call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
+		kfree_rcu(fi, fi_rcu);
 	}
 }
 
maintainer Reviewed-by on v1 8/14
#27 lore raw

Re: [PATCH 08/14] nfsd: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 111 lines · Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)

Quoted 1 line

call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);

Quoted 1 line
   T x = ...;
   kmem_cache_free(...,x);
Quoted 1 line
   T x;
   x = ...;
   kmem_cache_free(...,x);

)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);

Quoted 1 line
(quoted diff) +0 -2
-  T x = ...;
-  kmem_cache_free(...,x);
Quoted 1 line
(quoted diff) +0 -3
-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 fs/nfsd/nfs4state.c |    9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
fs/nfsd/nfs4state.c +0 -7
index a20c2c9d7d45..eba5083504c7 100644
@@ -571,13 +571,6 @@ opaque_hashval(const void *ptr, int nbytes)
 	return x;
 }
 
-static void nfsd4_free_file_rcu(struct rcu_head *rcu)
-{
-	struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
-
-	kmem_cache_free(file_slab, fp);
-}
-
 void
 put_nfs4_file(struct nfs4_file *fi)
 {

fs/nfsd/nfs4state.c put_nfs4_file(struct nfs4_file *fi) · line 584

Quoted 8 lines
fs/nfsd/nfs4state.c +1 -1
@@ -585,7 +578,7 @@ put_nfs4_file(struct nfs4_file *fi)
 		nfsd4_file_hash_remove(fi);
 		WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
 		WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
-		call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
+		kfree_rcu(fi, fi_rcu);
 	}
 }
 
Acked-by on v1 8/14
#28 lore raw

Re: [PATCH 08/14] nfsd: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Julia Lawall
Quoted 111 lines · Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|

   T x = ...;
   kmem_cache_free(...,x);
|
   T x;
   x = ...;
   kmem_cache_free(...,x);

)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|

(quoted diff) +0 -2
-  T x = ...;
-  kmem_cache_free(...,x);

|

(quoted diff) +0 -3
-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 fs/nfsd/nfs4state.c |    9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
fs/nfsd/nfs4state.c +0 -7
index a20c2c9d7d45..eba5083504c7 100644
@@ -571,13 +571,6 @@ opaque_hashval(const void *ptr, int nbytes)
 	return x;
 }
 
-static void nfsd4_free_file_rcu(struct rcu_head *rcu)
-{
-	struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
-
-	kmem_cache_free(file_slab, fp);
-}
-
 void
 put_nfs4_file(struct nfs4_file *fi)
 {

fs/nfsd/nfs4state.c put_nfs4_file(struct nfs4_file *fi) · line 584

Quoted 8 lines
fs/nfsd/nfs4state.c +1 -1
@@ -585,7 +578,7 @@ put_nfs4_file(struct nfs4_file *fi)
 		nfsd4_file_hash_remove(fi);
 		WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
 		WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
-		call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
+		kfree_rcu(fi, fi_rcu);
 	}
 }
 
Signature · 1 line
Chuck Lever
#29 lore raw

PATCH 9/14 block: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 block/blk-ioc.c |    9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
block/blk-ioc.c +1 -8
index 25dd4db11121..ce82770c72ab 100644
@@ -32,13 +32,6 @@ static void get_io_context(struct io_context *ioc)
 	atomic_long_inc(&ioc->refcount);
 }
 
-static void icq_free_icq_rcu(struct rcu_head *head)
-{
-	struct io_cq *icq = container_of(head, struct io_cq, __rcu_head);
-
-	kmem_cache_free(icq->__rcu_icq_cache, icq);
-}
-
 /*
  * Exit an icq. Called with ioc locked for blk-mq, and with both ioc
  * and queue locked for legacy.
@@ -102,7 +95,7 @@ static void ioc_destroy_icq(struct io_cq *icq)
 	 */
 	icq->__rcu_icq_cache = et->icq_cache;
 	icq->flags |= ICQ_DESTROYED;
-	call_rcu(&icq->__rcu_head, icq_free_icq_rcu);
+	kfree_rcu(icq, __rcu_head);
 }
 
 /*
#30 lore raw

PATCH 10/14 can: gw: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 net/can/gw.c |   13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
net/can/gw.c +3 -10
index 37528826935e..ffb9870e2d01 100644
@@ -577,13 +577,6 @@ static inline void cgw_unregister_filter(struct net *net, struct cgw_job *gwj)
 			  gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
 }
 
-static void cgw_job_free_rcu(struct rcu_head *rcu_head)
-{
-	struct cgw_job *gwj = container_of(rcu_head, struct cgw_job, rcu);
-
-	kmem_cache_free(cgw_cache, gwj);
-}
-
 static int cgw_notifier(struct notifier_block *nb,
 			unsigned long msg, void *ptr)
 {
@@ -603,7 +596,7 @@ static int cgw_notifier(struct notifier_block *nb,
 			if (gwj->src.dev == dev || gwj->dst.dev == dev) {
 				hlist_del(&gwj->list);
 				cgw_unregister_filter(net, gwj);
-				call_rcu(&gwj->rcu, cgw_job_free_rcu);
+				kfree_rcu(gwj, rcu);
 			}
 		}
 	}
@@ -1168,7 +1161,7 @@ static void cgw_remove_all_jobs(struct net *net)
 	hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
 		hlist_del(&gwj->list);
 		cgw_unregister_filter(net, gwj);
-		call_rcu(&gwj->rcu, cgw_job_free_rcu);
+		kfree_rcu(gwj, rcu);
 	}
 }
 
@@ -1236,7 +1229,7 @@ static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 		hlist_del(&gwj->list);
 		cgw_unregister_filter(net, gwj);
-		call_rcu(&gwj->rcu, cgw_job_free_rcu);
+		kfree_rcu(gwj, rcu);
 		err = 0;
 		break;
 	}
maintainer Acked-by on v1 10/14
#31 lore raw

Re: [PATCH 10/14] can: gw: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 83 lines · Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(

    kmem_cache_free(...);
|
    T x = ...;
    kmem_cache_free(...,x);
|
    T x;
    x = ...;
    kmem_cache_free(...,x);

)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|

(quoted diff) +0 -2
-  T x = ...;
-  kmem_cache_free(...,x);

|

(quoted diff) +0 -3
-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

Quoted 3 lines

For net/can/gw.c

Thanks Julia!

Quoted 52 lines
---
  net/can/gw.c |   13 +++----------
  1 file changed, 3 insertions(+), 10 deletions(-)
net/can/gw.c +3 -10
index 37528826935e..ffb9870e2d01 100644
@@ -577,13 +577,6 @@ static inline void cgw_unregister_filter(struct net *net, struct cgw_job *gwj)
  			  gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
  }
 
-static void cgw_job_free_rcu(struct rcu_head *rcu_head)
-{
-	struct cgw_job *gwj = container_of(rcu_head, struct cgw_job, rcu);
-
-	kmem_cache_free(cgw_cache, gwj);
-}
-
  static int cgw_notifier(struct notifier_block *nb,
  			unsigned long msg, void *ptr)
  {
@@ -603,7 +596,7 @@ static int cgw_notifier(struct notifier_block *nb,
  			if (gwj->src.dev == dev || gwj->dst.dev == dev) {
  				hlist_del(&gwj->list);
  				cgw_unregister_filter(net, gwj);
-				call_rcu(&gwj->rcu, cgw_job_free_rcu);
+				kfree_rcu(gwj, rcu);
  			}
  		}
  	}
@@ -1168,7 +1161,7 @@ static void cgw_remove_all_jobs(struct net *net)
  	hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
  		hlist_del(&gwj->list);
  		cgw_unregister_filter(net, gwj);
-		call_rcu(&gwj->rcu, cgw_job_free_rcu);
+		kfree_rcu(gwj, rcu);
  	}
  }
 
@@ -1236,7 +1229,7 @@ static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh,
 
  		hlist_del(&gwj->list);
  		cgw_unregister_filter(net, gwj);
-		call_rcu(&gwj->rcu, cgw_job_free_rcu);
+		kfree_rcu(gwj, rcu);
  		err = 0;
  		break;
  	}
#32 lore raw

PATCH 11/14 posix-timers: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 kernel/time/posix-timers.c |    9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
kernel/time/posix-timers.c +1 -8
index b924f0f096fa..bad5db979664 100644
@@ -412,18 +412,11 @@ static struct k_itimer * alloc_posix_timer(void)
 	return tmr;
 }
 
-static void k_itimer_rcu_free(struct rcu_head *head)
-{
-	struct k_itimer *tmr = container_of(head, struct k_itimer, rcu);
-
-	kmem_cache_free(posix_timers_cache, tmr);
-}
-
 static void posix_timer_free(struct k_itimer *tmr)
 {
 	put_pid(tmr->it_pid);
 	sigqueue_free(tmr->sigq);
-	call_rcu(&tmr->rcu, k_itimer_rcu_free);
+	kfree_rcu(tmr, rcu);
 }
 
 static void posix_timer_unhash_and_free(struct k_itimer *tmr)
#33 lore raw

PATCH 12/14 workqueue: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 kernel/workqueue.c |    8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
kernel/workqueue.c +1 -7
index 3fbaecfc88c2..2ec195dcc2bb 100644
@@ -5038,12 +5038,6 @@ static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
 	return NULL;
 }
 
-static void rcu_free_pwq(struct rcu_head *rcu)
-{
-	kmem_cache_free(pwq_cache,
-			container_of(rcu, struct pool_workqueue, rcu));
-}
-
 /*
  * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero
  * refcnt and needs to be destroyed.
@@ -5089,7 +5083,7 @@ static void pwq_release_workfn(struct kthread_work *work)
 		raw_spin_unlock_irq(&nna->lock);
 	}
 
-	call_rcu(&pwq->rcu, rcu_free_pwq);
+	kfree_rcu(pwq, rcu);
 
 	/*
 	 * If we're the last pwq going away, @wq is already dead and no one
maintainer Applied
#34 lore raw

Re: [PATCH 12/14] workqueue: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

Quoted 7 lines · Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

...

Quoted 3 lines

Applied to wq/for-6.11.

Thanks.

Signature · 1 line
tejun
#35 lore raw

PATCH 13/14 kcm: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 net/kcm/kcmsock.c |   10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
net/kcm/kcmsock.c +1 -9
index 2f191e50d4fc..fbb730cd2d38 100644
@@ -1580,14 +1580,6 @@ static int kcm_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 	return err;
 }
 
-static void free_mux(struct rcu_head *rcu)
-{
-	struct kcm_mux *mux = container_of(rcu,
-	    struct kcm_mux, rcu);
-
-	kmem_cache_free(kcm_muxp, mux);
-}
-
 static void release_mux(struct kcm_mux *mux)
 {
 	struct kcm_net *knet = mux->knet;
@@ -1615,7 +1607,7 @@ static void release_mux(struct kcm_mux *mux)
 	knet->count--;
 	mutex_unlock(&knet->mutex);
 
-	call_rcu(&mux->rcu, free_mux);
+	kfree_rcu(mux, rcu);
 }
 
 static void kcm_done(struct kcm_sock *kcm)
#36 lore raw

PATCH 14/14 netfilter: replace call_rcu by kfree_rcu for simple kmem_cache_free callback

In reply to Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|
T x = ...;
kmem_cache_free(...,x);
|
T x;
x = ...;
kmem_cache_free(...,x);
)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|
- T x = ...;
- kmem_cache_free(...,x);
|

-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---
 net/netfilter/nf_conncount.c        |   10 +---------
 net/netfilter/nf_conntrack_expect.c |   10 +---------
 net/netfilter/xt_hashlimit.c        |    9 +--------
 3 files changed, 3 insertions(+), 26 deletions(-)
net/netfilter/nf_conncount.c +1 -9
index 8715617b02fe..587bfcb79723 100644
@@ -275,14 +275,6 @@ bool nf_conncount_gc_list(struct net *net,
 }
 EXPORT_SYMBOL_GPL(nf_conncount_gc_list);
 
-static void __tree_nodes_free(struct rcu_head *h)
-{
-	struct nf_conncount_rb *rbconn;
-
-	rbconn = container_of(h, struct nf_conncount_rb, rcu_head);
-	kmem_cache_free(conncount_rb_cachep, rbconn);
-}
-
 /* caller must hold tree nf_conncount_locks[] lock */
 static void tree_nodes_free(struct rb_root *root,
 			    struct nf_conncount_rb *gc_nodes[],
@@ -295,7 +287,7 @@ static void tree_nodes_free(struct rb_root *root,
 		spin_lock(&rbconn->list.list_lock);
 		if (!rbconn->list.count) {
 			rb_erase(&rbconn->node, root);
-			call_rcu(&rbconn->rcu_head, __tree_nodes_free);
+			kfree_rcu(rbconn, rcu_head);
 		}
 		spin_unlock(&rbconn->list.list_lock);
 	}
net/netfilter/nf_conntrack_expect.c +1 -9
index 21fa550966f0..9dcaef6f3663 100644
@@ -367,18 +367,10 @@ void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class,
 }
 EXPORT_SYMBOL_GPL(nf_ct_expect_init);
 
-static void nf_ct_expect_free_rcu(struct rcu_head *head)
-{
-	struct nf_conntrack_expect *exp;
-
-	exp = container_of(head, struct nf_conntrack_expect, rcu);
-	kmem_cache_free(nf_ct_expect_cachep, exp);
-}
-
 void nf_ct_expect_put(struct nf_conntrack_expect *exp)
 {
 	if (refcount_dec_and_test(&exp->use))
-		call_rcu(&exp->rcu, nf_ct_expect_free_rcu);
+		kfree_rcu(exp, rcu);
 }
 EXPORT_SYMBOL_GPL(nf_ct_expect_put);
 
net/netfilter/xt_hashlimit.c +1 -8
index 0859b8f76764..c2b9b954eb53 100644
@@ -256,18 +256,11 @@ dsthash_alloc_init(struct xt_hashlimit_htable *ht,
 	return ent;
 }
 
-static void dsthash_free_rcu(struct rcu_head *head)
-{
-	struct dsthash_ent *ent = container_of(head, struct dsthash_ent, rcu);
-
-	kmem_cache_free(hashlimit_cachep, ent);
-}
-
 static inline void
 dsthash_free(struct xt_hashlimit_htable *ht, struct dsthash_ent *ent)
 {
 	hlist_del_rcu(&ent->node);
-	call_rcu(&ent->rcu, dsthash_free_rcu);
+	kfree_rcu(ent, rcu);
 	ht->count--;
 }
 static void htable_gc(struct work_struct *work);
maintainer
#37 lore raw
In reply to Julia Lawall
Quoted 7 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

#38 lore raw
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

[PATCH 02/14] net: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
I don't immediately see the rcu_barrier(), but if there isn't one in there somewhere there probably should be. Caution suggests a need to wait.

[PATCH 03/14] KVM: PPC: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
I don't immediately see the rcu_barrier(), but if there isn't
one in there somewhere there probably should be. Caution
suggests a need to wait.

[PATCH 04/14] xfrm6_tunnel: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see xfrm6_tunnel_fini().

[PATCH 05/14] tracefs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
This one is fine because the tracefs_inode_cachep kmem_cache
is created at boot and never destroyed.

[PATCH 06/14] eCryptfs: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
I don't see a kmem_cache_destroy(), but then again, I also don't see the kmem_cache_create(). Unless someone can see what I am
not seeing, let's wait.

[PATCH 07/14] net: bridge: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see br_fdb_fini() and br_deinit().

[PATCH 08/14] nfsd: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
I don't immediately see the rcu_barrier(), but if there isn't one in there somewhere there probably should be. Caution suggests a need to wait.

[PATCH 09/14] block: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
I don't see a kmem_cache_destroy(), but then again, I also don't see the kmem_cache_create(). Unless someone can see what I am not seeing, let's wait.

[PATCH 10/14] can: gw: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see cgw_module_exit().

[PATCH 11/14] posix-timers: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
This one is fine because the posix_timers_cache kmem_cache is
created at boot and never destroyed.

[PATCH 12/14] workqueue: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
This one is fine because the pwq_cache kmem_cache is created at boot and never destroyed.

[PATCH 13/14] kcm: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
I don't immediately see the rcu_barrier(), but if there isn't one in there somewhere there probably should be. Caution suggests a need to wait.

[PATCH 14/14] netfilter: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see hashlimit_mt_exit().

So 05/14, 11/14 and 12/14 are OK and can go ahead. The rest need some help.

Apologies for my having gotten overly enthusiastic about this change!

Thanx, Paul

maintainer
#39 lore raw
Quoted 2 lines · On Wed, 12 Jun 2024 15:37:55 -0700 Paul E. McKenney wrote:

So 05/14, 11/14 and 12/14 are OK and can go ahead. The rest need some help.

Thank you for the breakdown!

maintainer
#40 lore raw
In reply to Paul E. McKenney
Quoted 4 lines · Paul E. McKenney

[PATCH 09/14] block: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
I don't see a kmem_cache_destroy(), but then again, I also don't see the kmem_cache_create(). Unless someone can see what I am not seeing, let's wait.

It's in that same file:

blk_ioc_init()

the cache itself never goes away, as the ioc code is not unloadable. So I think the change there should be fine.

Signature · 1 line
Jens Axboe
#41 lore raw
Quoted 6 lines · Jens Axboe
Quoted 4 lines · Paul E. McKenney

[PATCH 09/14] block: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
I don't see a kmem_cache_destroy(), but then again, I also don't see the kmem_cache_create(). Unless someone can see what I am not seeing, let's wait.

Quoted 6 lines

It's in that same file:

blk_ioc_init()

the cache itself never goes away, as the ioc code is not unloadable. So I think the change there should be fine.

Thank you, Jens! (And to Jakub for motivating me to go look.)

So to update the scorecared, 05/14, 09/14, 11/14 and 12/14 are OK and can go ahead.

Thanx, Paul

#42 lore raw
In reply to Paul E. McKenney
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

Quoted 7 lines

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

Right, this has exactly the same pattern as the batman-adv issue:

    void wg_allowedips_slab_uninit(void)
    {
            rcu_barrier();
            kmem_cache_destroy(node_cache);
    }

I'll hold off on sending that up until this matter is resolved.

Jason

#43 lore raw
Quoted 26 lines · Jason A. Donenfeld
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

Quoted 7 lines

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

Right, this has exactly the same pattern as the batman-adv issue:

Quoted 7 lines
    void wg_allowedips_slab_uninit(void)
    {
            rcu_barrier();
            kmem_cache_destroy(node_cache);
    }

I'll hold off on sending that up until this matter is resolved.

BTW, I think this whole thing might be caused by:

a35d16905efc ("rcu: Add basic support for kfree_rcu() batching")

The commit message there mentions:

There is an implication with rcu_barrier() with this patch. Since the kfree_rcu() calls can be batched, and may not be handed yet to the RCU machinery in fact, the monitor may not have even run yet to do the queue_rcu_work(), there seems no easy way of implementing rcu_barrier() to wait for those kfree_rcu()s that are already made. So this means a kfree_rcu() followed by an rcu_barrier() does not imply that memory will be freed once rcu_barrier() returns.

Before that, a kfree_rcu() used to just add a normal call_rcu() into the list, but with the function offset < 4096 as a special marker. So the kfree_rcu() calls would be treated alongside the other call_rcu() ones and thus affected by rcu_barrier(). Looks like that behavior is no more since this commit.

Rather than getting rid of the batching, which seems good for efficiency, I wonder if the right fix to this would be adding a should_destroy boolean to kmem_cache, which kmem_cache_destroy() sets to true. And then right after it checks if (number_of_allocations == 0) actually_destroy(), and likewise on each kmem_cache_free(), it could check if (should_destroy && number_of_allocations == 0) actually_destroy(). This way, the work is delayed until it's safe to do so. This might also mitigate other lurking bugs of bad code that calls kmem_cache_destroy() before kmem_cache_free().

Jason

#44 lore raw
Quoted 49 lines · Jason A. Donenfeld
Quoted 26 lines · Jason A. Donenfeld
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

Quoted 7 lines

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

Right, this has exactly the same pattern as the batman-adv issue:

Quoted 7 lines
    void wg_allowedips_slab_uninit(void)
    {
            rcu_barrier();
            kmem_cache_destroy(node_cache);
    }

I'll hold off on sending that up until this matter is resolved.

BTW, I think this whole thing might be caused by:

a35d16905efc ("rcu: Add basic support for kfree_rcu() batching")

The commit message there mentions:

There is an implication with rcu_barrier() with this patch. Since the kfree_rcu() calls can be batched, and may not be handed yet to the RCU machinery in fact, the monitor may not have even run yet to do the queue_rcu_work(), there seems no easy way of implementing rcu_barrier() to wait for those kfree_rcu()s that are already made. So this means a kfree_rcu() followed by an rcu_barrier() does not imply that memory will be freed once rcu_barrier() returns.

Quoted 5 lines

Before that, a kfree_rcu() used to just add a normal call_rcu() into the list, but with the function offset < 4096 as a special marker. So the kfree_rcu() calls would be treated alongside the other call_rcu() ones and thus affected by rcu_barrier(). Looks like that behavior is no more since this commit.

You might well be right, and thank you for digging into this!

Quoted 9 lines

Rather than getting rid of the batching, which seems good for efficiency, I wonder if the right fix to this would be adding a should_destroy boolean to kmem_cache, which kmem_cache_destroy() sets to true. And then right after it checks if (number_of_allocations == 0) actually_destroy(), and likewise on each kmem_cache_free(), it could check if (should_destroy && number_of_allocations == 0) actually_destroy(). This way, the work is delayed until it's safe to do so. This might also mitigate other lurking bugs of bad code that calls kmem_cache_destroy() before kmem_cache_free().

Here are the current options being considered, including those that are completely brain-dead:

o Document current state. (Must use call_rcu() if module
destroys slab of RCU-protected objects.)

Need to review Julia's and Uladzislau's series of patches that change call_rcu() of slab objects to kfree_rcu().

o Make rcu_barrier() wait for kfree_rcu() objects. (This is
surprisingly complex and will wait unnecessarily in some cases. However, it does preserve current code.)

o Make a kfree_rcu_barrier() that waits for kfree_rcu() objects.
(This avoids the unnecessary waits, but adds complexity to kfree_rcu(). This is harder than it looks, but could be done, for example by maintaining pairs of per-CPU counters and handling them in an SRCU-like fashion. Need some way of communicating the index, though.)

(There might be use cases where both rcu_barrier() and kfree_rcu_barrier() would need to be invoked.)

A simpler way to implement this is to scan all of the in-flight objects, and queue each (either separately or in bulk) using call_rcu(). This still has problems with kfree_rcu_mightsleep() under low-memory conditions, in which case there are a bunch of synchronize_rcu() instances waiting. These instances could use SRCU-like per-CPU arrays of counters. Or just protect the calls to synchronize_rcu() and the later frees with an SRCU reader, then have the other end call synchronize_srcu().

o Make the current kmem_cache_destroy() asynchronously wait for
all memory to be returned, then complete the destruction. (This gets rid of a valuable debugging technique because in normal use, it is a bug to attempt to destroy a kmem_cache that has objects still allocated.)

o Make a kmem_cache_destroy_rcu() that asynchronously waits for
all memory to be returned, then completes the destruction. (This raises the question of what to is it takes a "long time" for the objects to be freed.)

o Make a kmem_cache_free_barrier() that blocks until all
objects in the specified kmem_cache have been freed.

o Make a kmem_cache_destroy_wait() that waits for all memory to

	be returned, then does the destruction.  This is equivalent to:

		kmem_cache_free_barrier(&mycache);
		kmem_cache_destroy(&mycache);

Uladzislau has started discussions on the last few of these:
https://lore.kernel.org/all/ZmnL4jkhJLIW924W@pc636/

I have also added this information to a Google Document for easier tracking:
https://docs.google.com/document/d/1v0rcZLvvjVGejT3523W0rDy_sLFu2LWc_NR3fQItZaA/edit?usp=sharing

Other thoughts?

Thanx, Paul

#45 lore raw
Quoted 10 lines · Paul E. McKenney

o Make the current kmem_cache_destroy() asynchronously wait for
all memory to be returned, then complete the destruction. (This gets rid of a valuable debugging technique because in normal use, it is a bug to attempt to destroy a kmem_cache that has objects still allocated.)

o Make a kmem_cache_destroy_rcu() that asynchronously waits for
all memory to be returned, then completes the destruction. (This raises the question of what to is it takes a "long time" for the objects to be freed.)

These seem like the best two options.

Quoted 8 lines

o Make a kmem_cache_free_barrier() that blocks until all
objects in the specified kmem_cache have been freed.

o Make a kmem_cache_destroy_wait() that waits for all memory to

	be returned, then does the destruction.  This is equivalent to:

		kmem_cache_free_barrier(&mycache);
		kmem_cache_destroy(&mycache);

These also seem fine, but I'm less keen about blocking behavior.

Though, along the ideas of kmem_cache_destroy_rcu(), you might also consider renaming this last one to kmem_cache_destroy_rcu_wait/barrier(). This way, it's RCU focused, and you can deal directly with the question of, "how long is too long to block/to memleak?"

Specifically what I mean is that we can still claim a memory leak has occurred if one batched kfree_rcu freeing grace period has elapsed since the last call to kmem_cache_destroy_rcu_wait/barrier() or kmem_cache_destroy_rcu(). In that case, you quit blocking, or you quit asynchronously waiting, and then you splat about a memleak like we have now.

But then, if that mechanism generally works, we don't really need a new function and we can just go with the first option of making kmem_cache_destroy() asynchronously wait. It'll wait, as you described, but then we adjust the tail of every kfree_rcu batch freeing cycle to check if there are _still_ any old outstanding kmem_cache_destroy() requests. If so, then we can splat and keep the old debugging info we currently have for finding memleaks.

Jason

#46 lore raw
Quoted 7 lines · Jason A. Donenfeld
Quoted 6 lines · Paul E. McKenney

o Make the current kmem_cache_destroy() asynchronously wait for
all memory to be returned, then complete the destruction. (This gets rid of a valuable debugging technique because in normal use, it is a bug to attempt to destroy a kmem_cache that has objects still allocated.)

Quoted 6 lines
Quoted 4 lines

o Make a kmem_cache_destroy_rcu() that asynchronously waits for
all memory to be returned, then completes the destruction. (This raises the question of what to is it takes a "long time" for the objects to be freed.)

These seem like the best two options.

I like them myself, but much depends on how much violence they do to the slab subsystem and to debuggability.

Quoted 10 lines
Quoted 8 lines

o Make a kmem_cache_free_barrier() that blocks until all
objects in the specified kmem_cache have been freed.

o Make a kmem_cache_destroy_wait() that waits for all memory to

	be returned, then does the destruction.  This is equivalent to:

		kmem_cache_free_barrier(&mycache);
		kmem_cache_destroy(&mycache);

These also seem fine, but I'm less keen about blocking behavior.

One advantage of the blocking behavior is that it pinpoints memory leaks from that slab. On the other hand, one can argue that you want this to block during testing but to be asynchronous in production. Borrowing someone else's hand, there are probably lots of other arguments one can make.

Quoted 4 lines

Though, along the ideas of kmem_cache_destroy_rcu(), you might also consider renaming this last one to kmem_cache_destroy_rcu_wait/barrier(). This way, it's RCU focused, and you can deal directly with the question of, "how long is too long to block/to memleak?"

Good point!

Quoted 6 lines

Specifically what I mean is that we can still claim a memory leak has occurred if one batched kfree_rcu freeing grace period has elapsed since the last call to kmem_cache_destroy_rcu_wait/barrier() or kmem_cache_destroy_rcu(). In that case, you quit blocking, or you quit asynchronously waiting, and then you splat about a memleak like we have now.

How about a kmem_cache_destroy_rcu() that marks that specified cache for destruction, and then a kmem_cache_destroy_barrier() that waits?

I took the liberty of adding your name to the Google document [1] and adding this section:

kmem_cache_destroy_rcu/_barrier()

The idea here is to provide a asynchronous kmem_cache_destroy_rcu() as described above along with a kmem_cache_destroy_barrier() that waits for the destruction of all prior kmem_cache instances previously passed to kmem_cache_destroy_rcu().  Alternatively,  could return a cookie that could be passed into a later call to kmem_cache_destroy_barrier().  This alternative has the advantage of isolating which kmem_cache instance is suffering the memory leak.

Please let me know if either liberty is in any way problematic.

Quoted 7 lines

But then, if that mechanism generally works, we don't really need a new function and we can just go with the first option of making kmem_cache_destroy() asynchronously wait. It'll wait, as you described, but then we adjust the tail of every kfree_rcu batch freeing cycle to check if there are _still_ any old outstanding kmem_cache_destroy() requests. If so, then we can splat and keep the old debugging info we currently have for finding memleaks.

The mechanism can always be sabotaged by memory-leak bugs on the part of the user of the kmem_cache structure in play, right?

OK, but I see your point. I added this to the existing "kmem_cache_destroy() Lingers for kfree_rcu()" section:

One way of preserving this debugging information is to splat if all of the slab’s memory has not been freed within a reasonable timeframe, perhaps the same 21 seconds that causes an RCU CPU stall warning.

Does that capture it?

Thanx, Paul

[1] https://docs.google.com/document/d/1v0rcZLvvjVGejT3523W0rDy_sLFu2LWc_NR3fQItZaA/edit?usp=sharing

#47 lore raw
Quoted 5 lines · Paul E. McKenney

How about a kmem_cache_destroy_rcu() that marks that specified cache for destruction, and then a kmem_cache_destroy_barrier() that waits?

I took the liberty of adding your name to the Google document [1] and adding this section:

Cool, though no need to make me yellow!

Quoted 14 lines
Quoted 7 lines

But then, if that mechanism generally works, we don't really need a new function and we can just go with the first option of making kmem_cache_destroy() asynchronously wait. It'll wait, as you described, but then we adjust the tail of every kfree_rcu batch freeing cycle to check if there are _still_ any old outstanding kmem_cache_destroy() requests. If so, then we can splat and keep the old debugging info we currently have for finding memleaks.

The mechanism can always be sabotaged by memory-leak bugs on the part of the user of the kmem_cache structure in play, right?

OK, but I see your point. I added this to the existing "kmem_cache_destroy() Lingers for kfree_rcu()" section:

Quoted 6 lines

One way of preserving this debugging information is to splat if all of the slab’s memory has not been freed within a reasonable timeframe, perhaps the same 21 seconds that causes an RCU CPU stall warning.

Does that capture it?

Not quite what I was thinking. Your 21 seconds as a time-based thing I guess could be fine. But I was mostly thinking:

1) kmem_cache_destroy() is called, but there are outstanding objects, so it defers.

2) Sometime later, a kfree_rcu_work batch freeing operation runs.

3) At the end of this batch freeing, the kernel notices that the kmem_cache whose destruction was previously deferred still has outstanding objects and has not been destroyed. It can conclude that there's thus been a memory leak.

In other words, instead of having to do this based on timers, you can just have the batch freeing code ask, "did those pending kmem_cache destructions get completed as a result of this last operation?"

#48 lore raw
Quoted 8 lines · Jason A. Donenfeld
Quoted 5 lines · Paul E. McKenney

How about a kmem_cache_destroy_rcu() that marks that specified cache for destruction, and then a kmem_cache_destroy_barrier() that waits?

I took the liberty of adding your name to the Google document [1] and adding this section:

Cool, though no need to make me yellow!

No worries, Jakub is also colored yellow. People added tomorrow will be cyan if I follow my usual change-color ordering. ;-)

Quoted 21 lines
Quoted 14 lines
Quoted 7 lines

But then, if that mechanism generally works, we don't really need a new function and we can just go with the first option of making kmem_cache_destroy() asynchronously wait. It'll wait, as you described, but then we adjust the tail of every kfree_rcu batch freeing cycle to check if there are _still_ any old outstanding kmem_cache_destroy() requests. If so, then we can splat and keep the old debugging info we currently have for finding memleaks.

The mechanism can always be sabotaged by memory-leak bugs on the part of the user of the kmem_cache structure in play, right?

OK, but I see your point. I added this to the existing "kmem_cache_destroy() Lingers for kfree_rcu()" section:

Quoted 6 lines

One way of preserving this debugging information is to splat if all of the slab’s memory has not been freed within a reasonable timeframe, perhaps the same 21 seconds that causes an RCU CPU stall warning.

Does that capture it?

Quoted 7 lines

Not quite what I was thinking. Your 21 seconds as a time-based thing I guess could be fine. But I was mostly thinking:

1) kmem_cache_destroy() is called, but there are outstanding objects, so it defers.

2) Sometime later, a kfree_rcu_work batch freeing operation runs.

Or not, if there has been a leak and there happens to be no outstanding kfree_rcu() memory.

Quoted 4 lines

3) At the end of this batch freeing, the kernel notices that the kmem_cache whose destruction was previously deferred still has outstanding objects and has not been destroyed. It can conclude that there's thus been a memory leak.

And the batch freeing can be replicated across CPUs, so it would be necessary to determine which was last to do this effective. Don't get me wrong, this can be done, but the performance/latency tradeoffs can be interesting.

Quoted 3 lines

In other words, instead of having to do this based on timers, you can just have the batch freeing code ask, "did those pending kmem_cache destructions get completed as a result of this last operation?"

I agree that kfree_rcu_work-batch time is a good time to evaluate slab (and I have added this to the document), but I do not believe that it can completely replace timeouts.

Thanx, Paul

#49 lore raw
In reply to Jason A. Donenfeld
Quoted 6 lines · Jason A. Donenfeld
Quoted 5 lines · Paul E. McKenney

o Make the current kmem_cache_destroy() asynchronously wait for
all memory to be returned, then complete the destruction. (This gets rid of a valuable debugging technique because in normal use, it is a bug to attempt to destroy a kmem_cache that has objects still allocated.)

This seems like the best option to me. As Jason already said, the debugging technique is not affected significantly, if the warning just occurs asynchronously later. The module can be already unloaded at that point, as the leak is never checked programatically anyway to control further execution, it's just a splat in dmesg.

Quoted 6 lines

Specifically what I mean is that we can still claim a memory leak has occurred if one batched kfree_rcu freeing grace period has elapsed since the last call to kmem_cache_destroy_rcu_wait/barrier() or kmem_cache_destroy_rcu(). In that case, you quit blocking, or you quit asynchronously waiting, and then you splat about a memleak like we have now.

Yes so we'd need the kmem_cache_free_barrier() for a slab kunit test (or the pessimistic variant waiting for the 21 seconds), and a polling variant of the same thing for the asynchronous destruction. Or we don't need a polling variant if it's ok to invoke such a barrier in a schedule_work() workfn.

We should not need any new kmem_cache flag nor kmem_cache_destroy() flag to burden the users of kfree_rcu() with. We have __kmem_cache_shutdown() that will try to flush everything immediately and if it doesn't succeed, we can assume kfree_rcu() might be in flight and try to wait for it asynchronously, without any flags.

SLAB_TYPESAFE_BY_RCU is still handled specially because it has special semantics as well.

As for users of call_rcu() with arbitrary callbacks that might be functions from the module that is about to unload, these should not return from kmem_cache_destroy() with objects in flight. But those should be using rcu_barrier() before calling kmem_cache_destroy() already, and probably we should not try to handle this automagically? Maybe one potential change with the described approach is that today they would get the "cache not empty" warning immediately. But that wouldn't stop the module unload so later the callbacks would try to execute unmapped code anyway. With the new approach the asynchronous handling might delay the "cache not empty" warnings (or not, if kmem_cache_free_barrier() would finish before a rcu_barrier() would) so the unmapped code execution would come first. I don't think that would be a regression.

#50 lore raw
Quoted 8 lines · Vlastimil Babka
Quoted 6 lines · Jason A. Donenfeld
Quoted 5 lines · Paul E. McKenney

o Make the current kmem_cache_destroy() asynchronously wait for
all memory to be returned, then complete the destruction. (This gets rid of a valuable debugging technique because in normal use, it is a bug to attempt to destroy a kmem_cache that has objects still allocated.)

Quoted 5 lines

This seems like the best option to me. As Jason already said, the debugging technique is not affected significantly, if the warning just occurs asynchronously later. The module can be already unloaded at that point, as the leak is never checked programatically anyway to control further execution, it's just a splat in dmesg.

Works for me!

Quoted 12 lines
Quoted 6 lines

Specifically what I mean is that we can still claim a memory leak has occurred if one batched kfree_rcu freeing grace period has elapsed since the last call to kmem_cache_destroy_rcu_wait/barrier() or kmem_cache_destroy_rcu(). In that case, you quit blocking, or you quit asynchronously waiting, and then you splat about a memleak like we have now.

Yes so we'd need the kmem_cache_free_barrier() for a slab kunit test (or the pessimistic variant waiting for the 21 seconds), and a polling variant of the same thing for the asynchronous destruction. Or we don't need a polling variant if it's ok to invoke such a barrier in a schedule_work() workfn.

Quoted 5 lines

We should not need any new kmem_cache flag nor kmem_cache_destroy() flag to burden the users of kfree_rcu() with. We have __kmem_cache_shutdown() that will try to flush everything immediately and if it doesn't succeed, we can assume kfree_rcu() might be in flight and try to wait for it asynchronously, without any flags.

That does sound like a very attractive approach.

Quoted 15 lines

SLAB_TYPESAFE_BY_RCU is still handled specially because it has special semantics as well.

As for users of call_rcu() with arbitrary callbacks that might be functions from the module that is about to unload, these should not return from kmem_cache_destroy() with objects in flight. But those should be using rcu_barrier() before calling kmem_cache_destroy() already, and probably we should not try to handle this automagically? Maybe one potential change with the described approach is that today they would get the "cache not empty" warning immediately. But that wouldn't stop the module unload so later the callbacks would try to execute unmapped code anyway. With the new approach the asynchronous handling might delay the "cache not empty" warnings (or not, if kmem_cache_free_barrier() would finish before a rcu_barrier() would) so the unmapped code execution would come first. I don't think that would be a regression.

Agreed.

There are some use cases where a call_rcu() from a module without an rcu_barrier() would be OK, for example, if the callback function was defined in the core kernel and either: (1) The memory was from kmalloc() or (2) The memory was from kmem_cache_alloc() and your suggested changes above have been applied. My current belief is that these are too special of cases to be worth optimizing for, so that the rule should remain "If you use call_rcu() in a module, you must call rcu_barrier() within the module-unload code."

There have been discussions of having module-unload automatically invoke rcu_barrier() if needed, but thus far we have not come up with a good way to do this. Challenges include things like static inline functions from the core kernel invoking call_rcu(), in which case how to figure out that the rcu_barrier() is not needed?

Thanx, Paul

#51 lore raw
Quoted 9 lines · Paul E. McKenney
Quoted 8 lines · Vlastimil Babka
Quoted 6 lines · Jason A. Donenfeld
Quoted 5 lines · Paul E. McKenney

o Make the current kmem_cache_destroy() asynchronously wait for
all memory to be returned, then complete the destruction. (This gets rid of a valuable debugging technique because in normal use, it is a bug to attempt to destroy a kmem_cache that has objects still allocated.)

Quoted 7 lines
Quoted 5 lines

This seems like the best option to me. As Jason already said, the debugging technique is not affected significantly, if the warning just occurs asynchronously later. The module can be already unloaded at that point, as the leak is never checked programatically anyway to control further execution, it's just a splat in dmesg.

Works for me!

Great. So this is how a prototype could look like, hopefully? The kunit test does generate the splat for me, which should be because the rcu_barrier() in the implementation (marked to be replaced with the real thing) is really insufficient. Note the test itself passes as this kind of error isn't wired up properly.

Another thing to resolve is the marked comment about kasan_shutdown() with potential kfree_rcu()'s in flight.

Also you need CONFIG_SLUB_DEBUG enabled otherwise node_nr_slabs() is a no-op and it might fail to notice the pending slabs. This will need to change.

----8<----

lib/slub_kunit.c +22 -0
index e6667a28c014..e3e4d0ca40b7 100644
@@ -5,6 +5,7 @@
 #include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
+#include <linux/rcupdate.h>
 #include "../mm/slab.h"
 
 static struct kunit_resource resource;
@@ -157,6 +158,26 @@ static void test_kmalloc_redzone_access(struct kunit *test)
 	kmem_cache_destroy(s);
 }
 
+struct test_kfree_rcu_struct {
+	struct rcu_head rcu;
+};
+
+static void test_kfree_rcu(struct kunit *test)
+{
+	struct kmem_cache *s = test_kmem_cache_create("TestSlub_kfree_rcu",
+				sizeof(struct test_kfree_rcu_struct),
+				SLAB_NO_MERGE);
+	struct test_kfree_rcu_struct *p = kmem_cache_alloc(s, GFP_KERNEL);
+
+	kasan_disable_current();
+
+	KUNIT_EXPECT_EQ(test, 0, slab_errors);
+
+	kasan_enable_current();
+	kfree_rcu(p, rcu);
+	kmem_cache_destroy(s);
+}
+
 static int test_init(struct kunit *test)
 {
 	slab_errors = 0;
@@ -177,6 +198,7 @@ static struct kunit_case test_cases[] = {
 
 	KUNIT_CASE(test_clobber_redzone_free),
 	KUNIT_CASE(test_kmalloc_redzone_access),
+	KUNIT_CASE(test_kfree_rcu),
 	{}
 };
 
mm/slab.h +3 -1
index b16e63191578..a0295600af92 100644
@@ -277,6 +277,8 @@ struct kmem_cache {
 	unsigned int red_left_pad;	/* Left redzone padding size */
 	const char *name;		/* Name (only for display!) */
 	struct list_head list;		/* List of slab caches */
+	struct work_struct async_destroy_work;
+
 #ifdef CONFIG_SYSFS
 	struct kobject kobj;		/* For sysfs */
 #endif
@@ -474,7 +476,7 @@ static inline bool is_kmalloc_cache(struct kmem_cache *s)
 			      SLAB_NO_USER_FLAGS)
 
 bool __kmem_cache_empty(struct kmem_cache *);
-int __kmem_cache_shutdown(struct kmem_cache *);
+int __kmem_cache_shutdown(struct kmem_cache *, bool);
 void __kmem_cache_release(struct kmem_cache *);
 int __kmem_cache_shrink(struct kmem_cache *);
 void slab_kmem_cache_release(struct kmem_cache *);
mm/slab_common.c +38 -5

77 lines not on this page · raw message

mm/slub.c +5 -4
index 1617d8014ecd..4d435b3d2b5f 100644
@@ -5342,7 +5342,8 @@ static void list_slab_objects(struct kmem_cache *s, struct slab *slab,
  * This is called from __kmem_cache_shutdown(). We must take list_lock
  * because sysfs file might still access partial list after the shutdowning.
  */
-static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
+static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n,
+			 bool warn_inuse)
 {
 	LIST_HEAD(discard);
 	struct slab *slab, *h;
@@ -5353,7 +5354,7 @@ static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
 		if (!slab->inuse) {
 			remove_partial(n, slab);
 			list_add(&slab->slab_list, &discard);
-		} else {
+		} else if (warn_inuse) {
 			list_slab_objects(s, slab,
 			  "Objects remaining in %s on __kmem_cache_shutdown()");
 		}
@@ -5378,7 +5379,7 @@ bool __kmem_cache_empty(struct kmem_cache *s)
 /*
  * Release all resources used by a slab cache.
  */
-int __kmem_cache_shutdown(struct kmem_cache *s)
+int __kmem_cache_shutdown(struct kmem_cache *s, bool warn_inuse)
 {
 	int node;
 	struct kmem_cache_node *n;
@@ -5386,7 +5387,7 @@ int __kmem_cache_shutdown(struct kmem_cache *s)
 	flush_all_cpus_locked(s);
 	/* Attempt to free all objects */
 	for_each_kmem_cache_node(s, node, n) {
-		free_partial(s, n);
+		free_partial(s, n, warn_inuse);
 		if (n->nr_partial || node_nr_slabs(n))
 			return 1;
 	}
#52 lore raw
Quoted 143 lines · Vlastimil Babka
Quoted 9 lines · Paul E. McKenney
Quoted 8 lines · Vlastimil Babka
Quoted 6 lines · Jason A. Donenfeld
Quoted 5 lines · Paul E. McKenney

o Make the current kmem_cache_destroy() asynchronously wait for
all memory to be returned, then complete the destruction. (This gets rid of a valuable debugging technique because in normal use, it is a bug to attempt to destroy a kmem_cache that has objects still allocated.)

Quoted 7 lines
Quoted 5 lines

This seems like the best option to me. As Jason already said, the debugging technique is not affected significantly, if the warning just occurs asynchronously later. The module can be already unloaded at that point, as the leak is never checked programatically anyway to control further execution, it's just a splat in dmesg.

Works for me!

Great. So this is how a prototype could look like, hopefully? The kunit test does generate the splat for me, which should be because the rcu_barrier() in the implementation (marked to be replaced with the real thing) is really insufficient. Note the test itself passes as this kind of error isn't wired up properly.

Another thing to resolve is the marked comment about kasan_shutdown() with potential kfree_rcu()'s in flight.

Also you need CONFIG_SLUB_DEBUG enabled otherwise node_nr_slabs() is a no-op and it might fail to notice the pending slabs. This will need to change.

----8<----

lib/slub_kunit.c +22 -0
index e6667a28c014..e3e4d0ca40b7 100644
@@ -5,6 +5,7 @@
 #include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
+#include <linux/rcupdate.h>
 #include "../mm/slab.h"
 
 static struct kunit_resource resource;
@@ -157,6 +158,26 @@ static void test_kmalloc_redzone_access(struct kunit *test)
 	kmem_cache_destroy(s);
 }
 
+struct test_kfree_rcu_struct {
+	struct rcu_head rcu;
+};
+
+static void test_kfree_rcu(struct kunit *test)
+{
+	struct kmem_cache *s = test_kmem_cache_create("TestSlub_kfree_rcu",
+				sizeof(struct test_kfree_rcu_struct),
+				SLAB_NO_MERGE);
+	struct test_kfree_rcu_struct *p = kmem_cache_alloc(s, GFP_KERNEL);
+
+	kasan_disable_current();
+
+	KUNIT_EXPECT_EQ(test, 0, slab_errors);
+
+	kasan_enable_current();
+	kfree_rcu(p, rcu);
+	kmem_cache_destroy(s);
+}
+
 static int test_init(struct kunit *test)
 {
 	slab_errors = 0;
@@ -177,6 +198,7 @@ static struct kunit_case test_cases[] = {
 
 	KUNIT_CASE(test_clobber_redzone_free),
 	KUNIT_CASE(test_kmalloc_redzone_access),
+	KUNIT_CASE(test_kfree_rcu),
 	{}
 };
 
mm/slab.h +3 -1
index b16e63191578..a0295600af92 100644
@@ -277,6 +277,8 @@ struct kmem_cache {
 	unsigned int red_left_pad;	/* Left redzone padding size */
 	const char *name;		/* Name (only for display!) */
 	struct list_head list;		/* List of slab caches */
+	struct work_struct async_destroy_work;
+
 #ifdef CONFIG_SYSFS
 	struct kobject kobj;		/* For sysfs */
 #endif
@@ -474,7 +476,7 @@ static inline bool is_kmalloc_cache(struct kmem_cache *s)
 			      SLAB_NO_USER_FLAGS)
 
 bool __kmem_cache_empty(struct kmem_cache *);
-int __kmem_cache_shutdown(struct kmem_cache *);
+int __kmem_cache_shutdown(struct kmem_cache *, bool);
 void __kmem_cache_release(struct kmem_cache *);
 int __kmem_cache_shrink(struct kmem_cache *);
 void slab_kmem_cache_release(struct kmem_cache *);
mm/slab_common.c +9 -2
index 5b1f996bed06..c5c356d0235d 100644
@@ -44,6 +44,8 @@ static LIST_HEAD(slab_caches_to_rcu_destroy);
 static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work);
 static DECLARE_WORK(slab_caches_to_rcu_destroy_work,
 		    slab_caches_to_rcu_destroy_workfn);
+static void kmem_cache_kfree_rcu_destroy_workfn(struct work_struct *work);
+
 
 /*
  * Set of flags that will prevent slab merging
@@ -234,6 +236,7 @@ static struct kmem_cache *create_cache(const char *name,
 
 	s->refcount = 1;
 	list_add(&s->list, &slab_caches);
+	INIT_WORK(&s->async_destroy_work, kmem_cache_kfree_rcu_destroy_workfn);
 	return s;
 
 out_free_cache:
@@ -449,12 +452,16 @@ static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work)
 	}
 }
 
-static int shutdown_cache(struct kmem_cache *s)
+static int shutdown_cache(struct kmem_cache *s, bool warn_inuse)
 {
 	/* free asan quarantined objects */
+	/*
+	 * XXX: is it ok to call this multiple times? and what happens with a
+	 * kfree_rcu() in flight that finishes after or in parallel with this?
+	 */
 	kasan_cache_shutdown(s);
 
-	if (__kmem_cache_shutdown(s) != 0)
+	if (__kmem_cache_shutdown(s, warn_inuse) != 0)
 		return -EBUSY;
 
 	list_del(&s->list);
@@ -477,6 +484,32 @@ void slab_kmem_cache_release(struct kmem_cache *s)
 	kmem_cache_free(kmem_cache, s);
 }

mm/slab_common.c void slab_kmem_cache_release(struct kmem_cache *s) · line 495

Quoted 10 lines
mm/slab_common.c +9 -0
 
+static void kmem_cache_kfree_rcu_destroy_workfn(struct work_struct *work)
+{
+	struct kmem_cache *s;
+	int err = -EBUSY;
+	bool rcu_set;
+
+	s = container_of(work, struct kmem_cache, async_destroy_work);
+
+	// XXX use the real kmem_cache_free_barrier() or similar thing here

It implies that we need to introduce kfree_rcu_barrier(), a new API, which i wanted to avoid initially. Since you do it asynchronous can we just repeat and wait until it a cache is furry freed?

I am asking because inventing a new kfree_rcu_barrier() might not be so straight forward.

--
Uladzislau Rezki

#53 lore raw
Quoted 6 lines · Uladzislau Rezki

mm/slab_common.c void slab_kmem_cache_release(struct kmem_cache *s) · line 495

Quoted 4 lines
mm/slab_common.c +4 -0
+
+	s = container_of(work, struct kmem_cache, async_destroy_work);
+
+	// XXX use the real kmem_cache_free_barrier() or similar thing here

It implies that we need to introduce kfree_rcu_barrier(), a new API, which i wanted to avoid initially.

I wanted to avoid new API or flags for kfree_rcu() users and this would be achieved. The barrier is used internally so I don't consider that an API to avoid. How difficult is the implementation is another question, depending on how the current batching works. Once (if) we have sheaves proven to work and move kfree_rcu() fully into SLUB, the barrier might also look different and hopefully easier. So maybe it's not worth to invest too much into that barrier and just go for the potentially longer, but easier to implement?

Quoted 2 lines

Since you do it asynchronous can we just repeat
and wait until it a cache is furry freed?

The problem is we want to detect the cases when it's not fully freed because there was an actual read. So at some point we'd need to stop the repeats because we know there can no longer be any kfree_rcu()'s in flight since the kmem_cache_destroy() was called.

Quoted 2 lines

I am asking because inventing a new kfree_rcu_barrier() might not be so straight forward.

Agreed.

Quoted 3 lines

--
Uladzislau Rezki

#54 lore raw
Quoted 8 lines · Uladzislau Rezki
Quoted 6 lines

mm/slab_common.c void slab_kmem_cache_release(struct kmem_cache *s) · line 495

Quoted 4 lines
mm/slab_common.c +4 -0
+
+	s = container_of(work, struct kmem_cache, async_destroy_work);
+
+	// XXX use the real kmem_cache_free_barrier() or similar thing here

It implies that we need to introduce kfree_rcu_barrier(), a new API, which i wanted to avoid initially.

Quoted 9 lines

I wanted to avoid new API or flags for kfree_rcu() users and this would be achieved. The barrier is used internally so I don't consider that an API to avoid. How difficult is the implementation is another question, depending on how the current batching works. Once (if) we have sheaves proven to work and move kfree_rcu() fully into SLUB, the barrier might also look different and hopefully easier. So maybe it's not worth to invest too much into that barrier and just go for the potentially longer, but easier to implement?

Right. I agree here. If the cache is not empty, OK, we just defer the work, even we can use a big 21 seconds delay, after that we just "warn" if it is still not empty and leave it as it is, i.e. emit a warning and we are done.

Destroying the cache is not something that must happen right away.

Quoted 8 lines
Quoted 2 lines

Since you do it asynchronous can we just repeat
and wait until it a cache is furry freed?

The problem is we want to detect the cases when it's not fully freed because there was an actual read. So at some point we'd need to stop the repeats because we know there can no longer be any kfree_rcu()'s in flight since the kmem_cache_destroy() was called.

Agree. As noted above, we can go with 21 seconds(as an example) interval and just perform destroy(without repeating).

--
Uladzislau Rezki

#55 lore raw
Quoted 17 lines · Uladzislau Rezki
Quoted 8 lines · Uladzislau Rezki
Quoted 6 lines

mm/slab_common.c void slab_kmem_cache_release(struct kmem_cache *s) · line 495

Quoted 4 lines
mm/slab_common.c +4 -0
+
+	s = container_of(work, struct kmem_cache, async_destroy_work);
+
+	// XXX use the real kmem_cache_free_barrier() or similar thing here

It implies that we need to introduce kfree_rcu_barrier(), a new API, which i wanted to avoid initially.

Quoted 9 lines

I wanted to avoid new API or flags for kfree_rcu() users and this would be achieved. The barrier is used internally so I don't consider that an API to avoid. How difficult is the implementation is another question, depending on how the current batching works. Once (if) we have sheaves proven to work and move kfree_rcu() fully into SLUB, the barrier might also look different and hopefully easier. So maybe it's not worth to invest too much into that barrier and just go for the potentially longer, but easier to implement?

Quoted 6 lines

Right. I agree here. If the cache is not empty, OK, we just defer the work, even we can use a big 21 seconds delay, after that we just "warn" if it is still not empty and leave it as it is, i.e. emit a warning and we are done.

Destroying the cache is not something that must happen right away.

OK, I have to ask...

Suppose that the cache is created and destroyed by a module and init/cleanup time, respectively. Suppose that this module is rmmod'ed then very quickly insmod'ed.

Do we need to fail the insmod if the kmem_cache has not yet been fully cleaned up? If not, do we have two versions of the same kmem_cache in /proc during the overlap time?

Thanx, Paul

Quoted 13 lines
Quoted 8 lines
Quoted 2 lines

Since you do it asynchronous can we just repeat
and wait until it a cache is furry freed?

The problem is we want to detect the cases when it's not fully freed because there was an actual read. So at some point we'd need to stop the repeats because we know there can no longer be any kfree_rcu()'s in flight since the kmem_cache_destroy() was called.

Agree. As noted above, we can go with 21 seconds(as an example) interval and just perform destroy(without repeating).

--
Uladzislau Rezki

#56 lore raw
Quoted 25 lines · Paul E. McKenney
Quoted 17 lines · Uladzislau Rezki
Quoted 8 lines · Uladzislau Rezki
Quoted 6 lines

mm/slab_common.c void slab_kmem_cache_release(struct kmem_cache *s) · line 495

Quoted 4 lines
mm/slab_common.c +4 -0
+
+	s = container_of(work, struct kmem_cache, async_destroy_work);
+
+	// XXX use the real kmem_cache_free_barrier() or similar thing here

It implies that we need to introduce kfree_rcu_barrier(), a new API, which i wanted to avoid initially.

Quoted 9 lines

I wanted to avoid new API or flags for kfree_rcu() users and this would be achieved. The barrier is used internally so I don't consider that an API to avoid. How difficult is the implementation is another question, depending on how the current batching works. Once (if) we have sheaves proven to work and move kfree_rcu() fully into SLUB, the barrier might also look different and hopefully easier. So maybe it's not worth to invest too much into that barrier and just go for the potentially longer, but easier to implement?

Quoted 6 lines

Right. I agree here. If the cache is not empty, OK, we just defer the work, even we can use a big 21 seconds delay, after that we just "warn" if it is still not empty and leave it as it is, i.e. emit a warning and we are done.

Destroying the cache is not something that must happen right away.

Quoted 8 lines

OK, I have to ask...

Suppose that the cache is created and destroyed by a module and init/cleanup time, respectively. Suppose that this module is rmmod'ed then very quickly insmod'ed.

Do we need to fail the insmod if the kmem_cache has not yet been fully cleaned up?

We don't have any such link between kmem_cache and module to detect that, so we would have to start tracking that. Probably not worth the trouble.

Quoted 2 lines

If not, do we have two versions of the same kmem_cache in
/proc during the overlap time?

Hm could happen in /proc/slabinfo but without being harmful other than perhaps confusing someone. We could filter out the caches being destroyed trivially.

Sysfs and debugfs might be more problematic as I suppose directory names would clash. I'll have to check... might be even happening now when we do detect leaked objects and just leave the cache around... thanks for the question.

Quoted 15 lines

Thanx, Paul

Quoted 13 lines
Quoted 8 lines
Quoted 2 lines

Since you do it asynchronous can we just repeat
and wait until it a cache is furry freed?

The problem is we want to detect the cases when it's not fully freed because there was an actual read. So at some point we'd need to stop the repeats because we know there can no longer be any kfree_rcu()'s in flight since the kmem_cache_destroy() was called.

Agree. As noted above, we can go with 21 seconds(as an example) interval and just perform destroy(without repeating).

--
Uladzislau Rezki

#57 lore raw
Quoted 32 lines · Vlastimil Babka
Quoted 31 lines · Paul E. McKenney
Quoted 17 lines · Uladzislau Rezki
Quoted 8 lines · Uladzislau Rezki
Quoted 6 lines

mm/slab_common.c void slab_kmem_cache_release(struct kmem_cache *s) · line 495

Quoted 4 lines
mm/slab_common.c +4 -0
+
+	s = container_of(work, struct kmem_cache, async_destroy_work);
+
+	// XXX use the real kmem_cache_free_barrier() or similar thing here

It implies that we need to introduce kfree_rcu_barrier(), a new API, which i wanted to avoid initially.

Quoted 9 lines

I wanted to avoid new API or flags for kfree_rcu() users and this would be achieved. The barrier is used internally so I don't consider that an API to avoid. How difficult is the implementation is another question, depending on how the current batching works. Once (if) we have sheaves proven to work and move kfree_rcu() fully into SLUB, the barrier might also look different and hopefully easier. So maybe it's not worth to invest too much into that barrier and just go for the potentially longer, but easier to implement?

Quoted 6 lines

Right. I agree here. If the cache is not empty, OK, we just defer the work, even we can use a big 21 seconds delay, after that we just "warn" if it is still not empty and leave it as it is, i.e. emit a warning and we are done.

Destroying the cache is not something that must happen right away.

OK, I have to ask...

Suppose that the cache is created and destroyed by a module and init/cleanup time, respectively. Suppose that this module is rmmod'ed then very quickly insmod'ed.

Quoted 5 lines
Quoted 2 lines

Do we need to fail the insmod if the kmem_cache has not yet been fully cleaned up?

We don't have any such link between kmem_cache and module to detect that, so we would have to start tracking that. Probably not worth the trouble.

Fair enough!

Quoted 6 lines
Quoted 2 lines

If not, do we have two versions of the same kmem_cache in
/proc during the overlap time?

Hm could happen in /proc/slabinfo but without being harmful other than perhaps confusing someone. We could filter out the caches being destroyed trivially.

Or mark them in /proc/slabinfo? Yet another column, yay!!! Or script breakage from flagging the name somehow, for example, trailing "/" character.

Quoted 4 lines

Sysfs and debugfs might be more problematic as I suppose directory names would clash. I'll have to check... might be even happening now when we do detect leaked objects and just leave the cache around... thanks for the question.

"It is a service that I provide." ;-)

But yes, we might be living with it already and there might already be ways people deal with it.

Thanx, Paul

Quoted 14 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
Quoted 2 lines

Since you do it asynchronous can we just repeat
and wait until it a cache is furry freed?

The problem is we want to detect the cases when it's not fully freed because there was an actual read. So at some point we'd need to stop the repeats because we know there can no longer be any kfree_rcu()'s in flight since the kmem_cache_destroy() was called.

Agree. As noted above, we can go with 21 seconds(as an example) interval and just perform destroy(without repeating).

--
Uladzislau Rezki

#58 lore raw
Quoted 44 lines · Paul E. McKenney
Quoted 32 lines · Vlastimil Babka
Quoted 31 lines · Paul E. McKenney
Quoted 17 lines · Uladzislau Rezki
Quoted 8 lines · Uladzislau Rezki
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+	s = container_of(work, struct kmem_cache, async_destroy_work);
+
+	// XXX use the real kmem_cache_free_barrier() or similar thing here

It implies that we need to introduce kfree_rcu_barrier(), a new API, which i wanted to avoid initially.

Quoted 9 lines

I wanted to avoid new API or flags for kfree_rcu() users and this would be achieved. The barrier is used internally so I don't consider that an API to avoid. How difficult is the implementation is another question, depending on how the current batching works. Once (if) we have sheaves proven to work and move kfree_rcu() fully into SLUB, the barrier might also look different and hopefully easier. So maybe it's not worth to invest too much into that barrier and just go for the potentially longer, but easier to implement?

Quoted 6 lines

Right. I agree here. If the cache is not empty, OK, we just defer the work, even we can use a big 21 seconds delay, after that we just "warn" if it is still not empty and leave it as it is, i.e. emit a warning and we are done.

Destroying the cache is not something that must happen right away.

OK, I have to ask...

Suppose that the cache is created and destroyed by a module and init/cleanup time, respectively. Suppose that this module is rmmod'ed then very quickly insmod'ed.

Quoted 5 lines
Quoted 2 lines

Do we need to fail the insmod if the kmem_cache has not yet been fully cleaned up?

We don't have any such link between kmem_cache and module to detect that, so we would have to start tracking that. Probably not worth the trouble.

Fair enough!

Quoted 3 lines
Quoted 2 lines

If not, do we have two versions of the same kmem_cache in
/proc during the overlap time?

Quoted 7 lines
Quoted 3 lines

Hm could happen in /proc/slabinfo but without being harmful other than perhaps confusing someone. We could filter out the caches being destroyed trivially.

Or mark them in /proc/slabinfo? Yet another column, yay!!! Or script breakage from flagging the name somehow, for example, trailing "/" character.

Yeah I've been resisting such changes to the layout and this wouldn't be worth it, apart from changing the name itself but not in a dangerous way like with "/" :)

Quoted 9 lines
Quoted 4 lines

Sysfs and debugfs might be more problematic as I suppose directory names would clash. I'll have to check... might be even happening now when we do detect leaked objects and just leave the cache around... thanks for the question.

"It is a service that I provide." ;-)

But yes, we might be living with it already and there might already be ways people deal with it.

So it seems if the sysfs/debugfs directories already exist, they will silently not be created. Wonder if we have such cases today already because caches with same name exist. I think we do with the zsmalloc using 32 caches with same name that we discussed elsewhere just recently.

Also indeed if the cache has leaked objects and won't be thus destroyed, these directories indeed stay around, as well as the slabinfo entry, and can prevent new ones from being created (slabinfo lines with same name are not prevented).

But it wouldn't be great to introduce this possibility to happen for the temporarily delayed removal due to kfree_rcu() and a module re-insert, since that's a legitimate case and not buggy state due to leaks.

The debugfs directory we could remove immediately before handing over to the scheduled workfn, but if it turns out there was a leak and the workfn leaves the cache around, debugfs dir will be gone and we can't check the alloc_traces/free_traces files there (but we have the per-object info including the traces in the dmesg splat).

The sysfs directory is currently removed only with the whole cache being destryed due to sysfs/kobject lifetime model. I'd love to untangle it for other reasons too, but haven't investigated it yet. But again it might be useful for sysfs dir to stay around for inspection, as for the debugfs.

We could rename the sysfs/debugfs directories before queuing the work? Add some prefix like GOING_AWAY-$name. If leak is detected and cache stays forever, another rename to LEAKED-$name. (and same for the slabinfo). But multiple ones with same name might pile up, so try adding a counter then? Probably messy to implement, but perhaps the most robust in the end? The automatic counter could also solve the general case of people using same name for multiple caches.

Other ideas?

Thanks,
Vlastimil

Quoted 17 lines

Thanx, Paul

Quoted 14 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
Quoted 2 lines

Since you do it asynchronous can we just repeat
and wait until it a cache is furry freed?

The problem is we want to detect the cases when it's not fully freed because there was an actual read. So at some point we'd need to stop the repeats because we know there can no longer be any kfree_rcu()'s in flight since the kmem_cache_destroy() was called.

Agree. As noted above, we can go with 21 seconds(as an example) interval and just perform destroy(without repeating).

--
Uladzislau Rezki

#59 lore raw
Quoted 49 lines · Vlastimil Babka
Quoted 48 lines · Paul E. McKenney
Quoted 32 lines · Vlastimil Babka
Quoted 31 lines · Paul E. McKenney
Quoted 17 lines · Uladzislau Rezki
Quoted 8 lines · Uladzislau Rezki
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+	s = container_of(work, struct kmem_cache, async_destroy_work);
+
+	// XXX use the real kmem_cache_free_barrier() or similar thing here

It implies that we need to introduce kfree_rcu_barrier(), a new API, which i wanted to avoid initially.

Quoted 9 lines

I wanted to avoid new API or flags for kfree_rcu() users and this would be achieved. The barrier is used internally so I don't consider that an API to avoid. How difficult is the implementation is another question, depending on how the current batching works. Once (if) we have sheaves proven to work and move kfree_rcu() fully into SLUB, the barrier might also look different and hopefully easier. So maybe it's not worth to invest too much into that barrier and just go for the potentially longer, but easier to implement?

Quoted 6 lines

Right. I agree here. If the cache is not empty, OK, we just defer the work, even we can use a big 21 seconds delay, after that we just "warn" if it is still not empty and leave it as it is, i.e. emit a warning and we are done.

Destroying the cache is not something that must happen right away.

OK, I have to ask...

Suppose that the cache is created and destroyed by a module and init/cleanup time, respectively. Suppose that this module is rmmod'ed then very quickly insmod'ed.

Quoted 5 lines
Quoted 2 lines

Do we need to fail the insmod if the kmem_cache has not yet been fully cleaned up?

We don't have any such link between kmem_cache and module to detect that, so we would have to start tracking that. Probably not worth the trouble.

Fair enough!

Quoted 6 lines
Quoted 2 lines

If not, do we have two versions of the same kmem_cache in
/proc during the overlap time?

Hm could happen in /proc/slabinfo but without being harmful other than perhaps confusing someone. We could filter out the caches being destroyed trivially.

Quoted 7 lines
Quoted 3 lines

Or mark them in /proc/slabinfo? Yet another column, yay!!! Or script breakage from flagging the name somehow, for example, trailing "/" character.

Yeah I've been resisting such changes to the layout and this wouldn't be worth it, apart from changing the name itself but not in a dangerous way like with "/" :)

;-) ;-) ;-)

Quoted 15 lines
Quoted 9 lines
Quoted 4 lines

Sysfs and debugfs might be more problematic as I suppose directory names would clash. I'll have to check... might be even happening now when we do detect leaked objects and just leave the cache around... thanks for the question.

"It is a service that I provide." ;-)

But yes, we might be living with it already and there might already be ways people deal with it.

So it seems if the sysfs/debugfs directories already exist, they will silently not be created. Wonder if we have such cases today already because caches with same name exist. I think we do with the zsmalloc using 32 caches with same name that we discussed elsewhere just recently.

Quoted 4 lines

Also indeed if the cache has leaked objects and won't be thus destroyed, these directories indeed stay around, as well as the slabinfo entry, and can prevent new ones from being created (slabinfo lines with same name are not prevented).

New one on me!

Quoted 3 lines

But it wouldn't be great to introduce this possibility to happen for the temporarily delayed removal due to kfree_rcu() and a module re-insert, since that's a legitimate case and not buggy state due to leaks.

Agreed.

Quoted 19 lines

The debugfs directory we could remove immediately before handing over to the scheduled workfn, but if it turns out there was a leak and the workfn leaves the cache around, debugfs dir will be gone and we can't check the alloc_traces/free_traces files there (but we have the per-object info including the traces in the dmesg splat).

The sysfs directory is currently removed only with the whole cache being destryed due to sysfs/kobject lifetime model. I'd love to untangle it for other reasons too, but haven't investigated it yet. But again it might be useful for sysfs dir to stay around for inspection, as for the debugfs.

We could rename the sysfs/debugfs directories before queuing the work? Add some prefix like GOING_AWAY-$name. If leak is detected and cache stays forever, another rename to LEAKED-$name. (and same for the slabinfo). But multiple ones with same name might pile up, so try adding a counter then? Probably messy to implement, but perhaps the most robust in the end? The automatic counter could also solve the general case of people using same name for multiple caches.

Quoted 1 line

Other ideas?

Move the going-away files/directories to some new directoriesy? But you would still need a counter or whatever. I honestly cannot say what would be best from the viewpoint of existing software scanning those files and directories.

Thanx, Paul

Quoted 21 lines

Thanks,
Vlastimil

Quoted 17 lines

Thanx, Paul

Quoted 14 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
Quoted 2 lines

Since you do it asynchronous can we just repeat
and wait until it a cache is furry freed?

The problem is we want to detect the cases when it's not fully freed because there was an actual read. So at some point we'd need to stop the repeats because we know there can no longer be any kfree_rcu()'s in flight since the kmem_cache_destroy() was called.

Agree. As noted above, we can go with 21 seconds(as an example) interval and just perform destroy(without repeating).

--
Uladzislau Rezki

#60 lore raw
In reply to Vlastimil Babka
Quoted 100 lines · Vlastimil Babka
Quoted 44 lines · Paul E. McKenney
Quoted 32 lines · Vlastimil Babka
Quoted 31 lines · Paul E. McKenney
Quoted 17 lines · Uladzislau Rezki
Quoted 8 lines · Uladzislau Rezki
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+	s = container_of(work, struct kmem_cache, async_destroy_work);
+
+	// XXX use the real kmem_cache_free_barrier() or similar thing here

It implies that we need to introduce kfree_rcu_barrier(), a new API, which i wanted to avoid initially.

Quoted 9 lines

I wanted to avoid new API or flags for kfree_rcu() users and this would be achieved. The barrier is used internally so I don't consider that an API to avoid. How difficult is the implementation is another question, depending on how the current batching works. Once (if) we have sheaves proven to work and move kfree_rcu() fully into SLUB, the barrier might also look different and hopefully easier. So maybe it's not worth to invest too much into that barrier and just go for the potentially longer, but easier to implement?

Quoted 6 lines

Right. I agree here. If the cache is not empty, OK, we just defer the work, even we can use a big 21 seconds delay, after that we just "warn" if it is still not empty and leave it as it is, i.e. emit a warning and we are done.

Destroying the cache is not something that must happen right away.

OK, I have to ask...

Suppose that the cache is created and destroyed by a module and init/cleanup time, respectively. Suppose that this module is rmmod'ed then very quickly insmod'ed.

Quoted 5 lines
Quoted 2 lines

Do we need to fail the insmod if the kmem_cache has not yet been fully cleaned up?

We don't have any such link between kmem_cache and module to detect that, so we would have to start tracking that. Probably not worth the trouble.

Fair enough!

Quoted 3 lines
Quoted 2 lines

If not, do we have two versions of the same kmem_cache in
/proc during the overlap time?

Quoted 7 lines
Quoted 3 lines

Hm could happen in /proc/slabinfo but without being harmful other than perhaps confusing someone. We could filter out the caches being destroyed trivially.

Or mark them in /proc/slabinfo? Yet another column, yay!!! Or script breakage from flagging the name somehow, for example, trailing "/" character.

Yeah I've been resisting such changes to the layout and this wouldn't be worth it, apart from changing the name itself but not in a dangerous way like with "/" :)

Quoted 9 lines
Quoted 4 lines

Sysfs and debugfs might be more problematic as I suppose directory names would clash. I'll have to check... might be even happening now when we do detect leaked objects and just leave the cache around... thanks for the question.

"It is a service that I provide." ;-)

But yes, we might be living with it already and there might already be ways people deal with it.

So it seems if the sysfs/debugfs directories already exist, they will silently not be created. Wonder if we have such cases today already because caches with same name exist. I think we do with the zsmalloc using 32 caches with same name that we discussed elsewhere just recently.

Also indeed if the cache has leaked objects and won't be thus destroyed, these directories indeed stay around, as well as the slabinfo entry, and can prevent new ones from being created (slabinfo lines with same name are not prevented).

But it wouldn't be great to introduce this possibility to happen for the temporarily delayed removal due to kfree_rcu() and a module re-insert, since that's a legitimate case and not buggy state due to leaks.

The debugfs directory we could remove immediately before handing over to the scheduled workfn, but if it turns out there was a leak and the workfn leaves the cache around, debugfs dir will be gone and we can't check the alloc_traces/free_traces files there (but we have the per-object info including the traces in the dmesg splat).

The sysfs directory is currently removed only with the whole cache being destryed due to sysfs/kobject lifetime model. I'd love to untangle it for other reasons too, but haven't investigated it yet. But again it might be useful for sysfs dir to stay around for inspection, as for the debugfs.

We could rename the sysfs/debugfs directories before queuing the work? Add some prefix like GOING_AWAY-$name. If leak is detected and cache stays forever, another rename to LEAKED-$name. (and same for the slabinfo). But multiple ones with same name might pile up, so try adding a counter then? Probably messy to implement, but perhaps the most robust in the end? The automatic counter could also solve the general case of people using same name for multiple caches.

Quoted 2 lines

Other ideas?

One question. Maybe it is already late but it is better to ask rather than not.

What do you think if we have a small discussion about it on the LPC 2024 as a topic? It might be it is already late or a schedule is set by now. Or we fix it by a conference time.

Just a thought.

--
Uladzislau Rezki

#61 lore raw
Quoted 8 lines · Uladzislau Rezki

On Wed, Jun 19, 2024 at 11:28:13AM +0200, Vlastimil Babka wrote:
One question. Maybe it is already late but it is better to ask rather than not.

What do you think if we have a small discussion about it on the LPC 2024 as a topic? It might be it is already late or a schedule is set by now. Or we fix it by a conference time.

Just a thought.

Sorry for the late reply. The MM MC turned out to be so packed I didn't even propose a slab topic. We could discuss in hallway track or a BOF, but hopefully if the current direction taken by my RFC brings no unexpected surprise, and the necessary RCU barrier side is also feasible, this will be settled by time of plumbers.

Quoted 2 lines

--
Uladzislau Rezki

#62 lore raw
Quoted 8 lines · Vlastimil Babka
Quoted 7 lines · Uladzislau Rezki

On Wed, Jun 19, 2024 at 11:28:13AM +0200, Vlastimil Babka wrote:
One question. Maybe it is already late but it is better to ask rather than not.

What do you think if we have a small discussion about it on the LPC 2024 as a topic? It might be it is already late or a schedule is set by now. Or we fix it by a conference time.

Quoted 7 lines
Quoted 1 line

Just a thought.

Sorry for the late reply. The MM MC turned out to be so packed I didn't even propose a slab topic. We could discuss in hallway track or a BOF, but hopefully if the current direction taken by my RFC brings no unexpected surprise, and the necessary RCU barrier side is also feasible, this will be settled by time of plumbers.

That would be even better!

Thanx, Paul

#63 lore raw
Quoted 11 lines · Paul E. McKenney
Quoted 10 lines · Vlastimil Babka
Quoted 8 lines · Uladzislau Rezki

On Wed, Jun 19, 2024 at 11:28:13AM +0200, Vlastimil Babka wrote:
One question. Maybe it is already late but it is better to ask rather than not.

What do you think if we have a small discussion about it on the LPC 2024 as a topic? It might be it is already late or a schedule is set by now. Or we fix it by a conference time.

Just a thought.

Quoted 7 lines
Quoted 5 lines

Sorry for the late reply. The MM MC turned out to be so packed I didn't even propose a slab topic. We could discuss in hallway track or a BOF, but hopefully if the current direction taken by my RFC brings no unexpected surprise, and the necessary RCU barrier side is also feasible, this will be settled by time of plumbers.

That would be even better!

I should have linked to the RFC :)

https://lore.kernel.org/all/[email protected]/

#64 lore raw
In reply to Paul E. McKenney
Quoted 17 lines · Paul E. McKenney
Quoted 8 lines · Vlastimil Babka
Quoted 7 lines · Uladzislau Rezki

On Wed, Jun 19, 2024 at 11:28:13AM +0200, Vlastimil Babka wrote:
One question. Maybe it is already late but it is better to ask rather than not.

What do you think if we have a small discussion about it on the LPC 2024 as a topic? It might be it is already late or a schedule is set by now. Or we fix it by a conference time.

Quoted 7 lines
Quoted 1 line

Just a thought.

Sorry for the late reply. The MM MC turned out to be so packed I didn't even propose a slab topic. We could discuss in hallway track or a BOF, but hopefully if the current direction taken by my RFC brings no unexpected surprise, and the necessary RCU barrier side is also feasible, this will be settled by time of plumbers.

Quoted 3 lines

That would be even better!

Thanx, Paul

Hah, so it was close but my hope was fulfilled in the end!

commit bdf56c7580d267a123cc71ca0f2459c797b76fde
Merge: efdfcd40ad5e ecc4d6af979b
Author: Linus Torvalds <[email protected]>
Date: Wed Sep 18 08:53:53 2024 +0200

    Merge tag 'slab-for-6.12' of

git://git.kernel.org/pub/scm/linux/kernel/git/vbabka/slab

So that was at 8:53 Vienna time, and Plumbers started at 10:00...

#65 lore raw
Quoted 18 lines · Vlastimil Babka
Quoted 17 lines · Paul E. McKenney
Quoted 15 lines · Vlastimil Babka
Quoted 8 lines · Uladzislau Rezki

On Wed, Jun 19, 2024 at 11:28:13AM +0200, Vlastimil Babka wrote:
One question. Maybe it is already late but it is better to ask rather than not.

What do you think if we have a small discussion about it on the LPC 2024 as a topic? It might be it is already late or a schedule is set by now. Or we fix it by a conference time.

Just a thought.

Sorry for the late reply. The MM MC turned out to be so packed I didn't even propose a slab topic. We could discuss in hallway track or a BOF, but hopefully if the current direction taken by my RFC brings no unexpected surprise, and the necessary RCU barrier side is also feasible, this will be settled by time of plumbers.

Quoted 5 lines
Quoted 3 lines

That would be even better!

Thanx, Paul

Hah, so it was close but my hope was fulfilled in the end!

Nice, and thank you!!!

Thanx, Paul

Quoted 9 lines

commit bdf56c7580d267a123cc71ca0f2459c797b76fde
Merge: efdfcd40ad5e ecc4d6af979b
Author: Linus Torvalds <[email protected]>
Date: Wed Sep 18 08:53:53 2024 +0200

    Merge tag 'slab-for-6.12' of

git://git.kernel.org/pub/scm/linux/kernel/git/vbabka/slab

So that was at 8:53 Vienna time, and Plumbers started at 10:00...

#66 lore raw

Hello,

I have rerun the semantic patch that removes call_rcu calls in cases where the callback function just does some pointer arithmetic and calls kmem_cache_free. Let me know if this looks ok, and if so, I can make a more formal patch submission.

This is against:

commit 75b607fab38d149f232f01eae5e6392b394dd659 (HEAD -> master, origin/master, origin/HEAD)
Merge: 5b7c893ed5ed e0ed52154e86
Author: Linus Torvalds <[email protected]>
Date: Tue Oct 8 12:54:04 2024 -0700

Merge tag 'sched_ext-for-6.12-rc2-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext

julia

arch/powerpc/kvm/book3s_mmu_hpte.c +1 -7
diff -u -p a/arch/powerpc/kvm/book3s_mmu_hpte.c b/arch/powerpc/kvm/book3s_mmu_hpte.c
@@ -92,12 +92,6 @@ void kvmppc_mmu_hpte_cache_map(struct kv
 	spin_unlock(&vcpu3s->mmu_lock);
 }
 
-static void free_pte_rcu(struct rcu_head *head)
-{
-	struct hpte_cache *pte = container_of(head, struct hpte_cache, rcu_head);
-	kmem_cache_free(hpte_cache, pte);
-}
-
 static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
 {
 	struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
@@ -126,7 +120,7 @@ static void invalidate_pte(struct kvm_vc
 
 	spin_unlock(&vcpu3s->mmu_lock);
 
-	call_rcu(&pte->rcu_head, free_pte_rcu);
+	kfree_rcu(pte, rcu_head);
 }
 
 static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu)
block/blk-ioc.c +1 -8
diff -u -p a/block/blk-ioc.c b/block/blk-ioc.c
@@ -32,13 +32,6 @@ static void get_io_context(struct io_con
 	atomic_long_inc(&ioc->refcount);
 }
 
-static void icq_free_icq_rcu(struct rcu_head *head)
-{
-	struct io_cq *icq = container_of(head, struct io_cq, __rcu_head);
-
-	kmem_cache_free(icq->__rcu_icq_cache, icq);
-}
-
 /*
  * Exit an icq. Called with ioc locked for blk-mq, and with both ioc
  * and queue locked for legacy.
@@ -102,7 +95,7 @@ static void ioc_destroy_icq(struct io_cq
 	 */
 	icq->__rcu_icq_cache = et->icq_cache;
 	icq->flags |= ICQ_DESTROYED;
-	call_rcu(&icq->__rcu_head, icq_free_icq_rcu);
+	kfree_rcu(icq, __rcu_head);
 }
 
 /*
drivers/net/wireguard/allowedips.c +2 -7
diff -u -p a/drivers/net/wireguard/allowedips.c b/drivers/net/wireguard/allowedips.c
@@ -48,11 +48,6 @@ static void push_rcu(struct allowedips_n
 	}
 }
 
-static void node_free_rcu(struct rcu_head *rcu)
-{
-	kmem_cache_free(node_cache, container_of(rcu, struct allowedips_node, rcu));
-}
-
 static void root_free_rcu(struct rcu_head *rcu)
 {
 	struct allowedips_node *node, *stack[MAX_ALLOWEDIPS_DEPTH] = {
@@ -330,13 +325,13 @@ void wg_allowedips_remove_by_peer(struct
 			child = rcu_dereference_protected(
 					parent->bit[!(node->parent_bit_packed & 1)],
 					lockdep_is_held(lock));
-		call_rcu(&node->rcu, node_free_rcu);
+		kfree_rcu(node, rcu);
 		if (!free_parent)
 			continue;
 		if (child)
 			child->parent_bit_packed = parent->parent_bit_packed;
 		*(struct allowedips_node **)(parent->parent_bit_packed & ~3UL) = child;
-		call_rcu(&parent->rcu, node_free_rcu);
+		kfree_rcu(parent, rcu);
 	}
 }
 
fs/ecryptfs/dentry.c +1 -7
diff -u -p a/fs/ecryptfs/dentry.c b/fs/ecryptfs/dentry.c
@@ -51,12 +51,6 @@ static int ecryptfs_d_revalidate(struct
 
 struct kmem_cache *ecryptfs_dentry_info_cache;
 
-static void ecryptfs_dentry_free_rcu(struct rcu_head *head)
-{
-	kmem_cache_free(ecryptfs_dentry_info_cache,
-		container_of(head, struct ecryptfs_dentry_info, rcu));
-}
-
 /**
  * ecryptfs_d_release
  * @dentry: The ecryptfs dentry
@@ -68,7 +62,7 @@ static void ecryptfs_d_release(struct de
 	struct ecryptfs_dentry_info *p = dentry->d_fsdata;
 	if (p) {
 		path_put(&p->lower_path);
-		call_rcu(&p->rcu, ecryptfs_dentry_free_rcu);
+		kfree_rcu(p, rcu);
 	}
 }
 
fs/nfsd/nfs4state.c +1 -8
diff -u -p a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
@@ -572,13 +572,6 @@ opaque_hashval(const void *ptr, int nbyt
 	return x;
 }
 
-static void nfsd4_free_file_rcu(struct rcu_head *rcu)
-{
-	struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
-
-	kmem_cache_free(file_slab, fp);
-}
-
 void
 put_nfs4_file(struct nfs4_file *fi)
 {
@@ -586,7 +579,7 @@ put_nfs4_file(struct nfs4_file *fi)
 		nfsd4_file_hash_remove(fi);
 		WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
 		WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
-		call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
+		kfree_rcu(fi, fi_rcu);
 	}
 }
 
kernel/time/posix-timers.c +1 -8
diff -u -p a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
@@ -413,18 +413,11 @@ static struct k_itimer * alloc_posix_tim
 	return tmr;
 }
 
-static void k_itimer_rcu_free(struct rcu_head *head)
-{
-	struct k_itimer *tmr = container_of(head, struct k_itimer, rcu);
-
-	kmem_cache_free(posix_timers_cache, tmr);
-}
-
 static void posix_timer_free(struct k_itimer *tmr)
 {
 	put_pid(tmr->it_pid);
 	sigqueue_free(tmr->sigq);
-	call_rcu(&tmr->rcu, k_itimer_rcu_free);
+	kfree_rcu(tmr, rcu);
 }
 
 static void posix_timer_unhash_and_free(struct k_itimer *tmr)
net/batman-adv/translation-table.c +1 -14
diff -u -p a/net/batman-adv/translation-table.c b/net/batman-adv/translation-table.c
@@ -408,19 +408,6 @@ static void batadv_tt_global_size_dec(st
 }
 
 /**
- * batadv_tt_orig_list_entry_free_rcu() - free the orig_entry
- * @rcu: rcu pointer of the orig_entry
- */
-static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
-{
-	struct batadv_tt_orig_list_entry *orig_entry;
-
-	orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
-
-	kmem_cache_free(batadv_tt_orig_cache, orig_entry);
-}
-
-/**
  * batadv_tt_orig_list_entry_release() - release tt orig entry from lists and
  *  queue for free after rcu grace period
  * @ref: kref pointer of the tt orig entry
@@ -433,7 +420,7 @@ static void batadv_tt_orig_list_entry_re
 				  refcount);
 
 	batadv_orig_node_put(orig_entry->orig_node);
-	call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
+	kfree_rcu(orig_entry, rcu);
 }
 
 /**
net/bridge/br_fdb.c +1 -8
diff -u -p a/net/bridge/br_fdb.c b/net/bridge/br_fdb.c
@@ -73,13 +73,6 @@ static inline int has_expired(const stru
 	       time_before_eq(fdb->updated + hold_time(br), jiffies);
 }
 
-static void fdb_rcu_free(struct rcu_head *head)
-{
-	struct net_bridge_fdb_entry *ent
-		= container_of(head, struct net_bridge_fdb_entry, rcu);
-	kmem_cache_free(br_fdb_cache, ent);
-}
-
 static int fdb_to_nud(const struct net_bridge *br,
 		      const struct net_bridge_fdb_entry *fdb)
 {
@@ -329,7 +322,7 @@ static void fdb_delete(struct net_bridge
 	if (test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED, &f->flags))
 		atomic_dec(&br->fdb_n_learned);
 	fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
-	call_rcu(&f->rcu, fdb_rcu_free);
+	kfree_rcu(f, rcu);
 }
 
 /* Delete a local entry if no other port had the same address.
net/can/gw.c +3 -10
diff -u -p a/net/can/gw.c b/net/can/gw.c
@@ -577,13 +577,6 @@ static inline void cgw_unregister_filter
 			  gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
 }
 
-static void cgw_job_free_rcu(struct rcu_head *rcu_head)
-{
-	struct cgw_job *gwj = container_of(rcu_head, struct cgw_job, rcu);
-
-	kmem_cache_free(cgw_cache, gwj);
-}
-
 static int cgw_notifier(struct notifier_block *nb,
 			unsigned long msg, void *ptr)
 {
@@ -603,7 +596,7 @@ static int cgw_notifier(struct notifier_
 			if (gwj->src.dev == dev || gwj->dst.dev == dev) {
 				hlist_del(&gwj->list);
 				cgw_unregister_filter(net, gwj);
-				call_rcu(&gwj->rcu, cgw_job_free_rcu);
+				kfree_rcu(gwj, rcu);
 			}
 		}
 	}
@@ -1168,7 +1161,7 @@ static void cgw_remove_all_jobs(struct n
 	hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
 		hlist_del(&gwj->list);
 		cgw_unregister_filter(net, gwj);
-		call_rcu(&gwj->rcu, cgw_job_free_rcu);
+		kfree_rcu(gwj, rcu);
 	}
 }
 
@@ -1236,7 +1229,7 @@ static int cgw_remove_job(struct sk_buff
 
 		hlist_del(&gwj->list);
 		cgw_unregister_filter(net, gwj);
-		call_rcu(&gwj->rcu, cgw_job_free_rcu);
+		kfree_rcu(gwj, rcu);
 		err = 0;
 		break;
 	}
net/ipv4/fib_trie.c +1 -7
diff -u -p a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
@@ -292,15 +292,9 @@ static const int inflate_threshold = 50;
 static const int halve_threshold_root = 15;
 static const int inflate_threshold_root = 30;
 
-static void __alias_free_mem(struct rcu_head *head)
-{
-	struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
-	kmem_cache_free(fn_alias_kmem, fa);
-}
-
 static inline void alias_free_mem_rcu(struct fib_alias *fa)
 {
-	call_rcu(&fa->rcu, __alias_free_mem);
+	kfree_rcu(fa, rcu);
 }
 
 #define TNODE_VMALLOC_MAX \
net/ipv4/inetpeer.c +2 -7
diff -u -p a/net/ipv4/inetpeer.c b/net/ipv4/inetpeer.c
@@ -128,11 +128,6 @@ static struct inet_peer *lookup(const st
 	return NULL;
 }
 
-static void inetpeer_free_rcu(struct rcu_head *head)
-{
-	kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
-}
-
 /* perform garbage collect on all items stacked during a lookup */
 static void inet_peer_gc(struct inet_peer_base *base,
 			 struct inet_peer *gc_stack[],
@@ -168,7 +163,7 @@ static void inet_peer_gc(struct inet_pee
 		if (p) {
 			rb_erase(&p->rb_node, &base->rb_root);
 			base->total--;
-			call_rcu(&p->rcu, inetpeer_free_rcu);
+			kfree_rcu(p, rcu);
 		}
 	}
 }
@@ -242,7 +237,7 @@ void inet_putpeer(struct inet_peer *p)
 	WRITE_ONCE(p->dtime, (__u32)jiffies);
 
 	if (refcount_dec_and_test(&p->refcnt))
-		call_rcu(&p->rcu, inetpeer_free_rcu);
+		kfree_rcu(p, rcu);
 }
 EXPORT_SYMBOL_GPL(inet_putpeer);
 
net/ipv6/ip6_fib.c +1 -8
diff -u -p a/net/ipv6/ip6_fib.c b/net/ipv6/ip6_fib.c
@@ -198,16 +198,9 @@ static void node_free_immediate(struct n
 	net->ipv6.rt6_stats->fib_nodes--;
 }
 
-static void node_free_rcu(struct rcu_head *head)
-{
-	struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
-
-	kmem_cache_free(fib6_node_kmem, fn);
-}
-
 static void node_free(struct net *net, struct fib6_node *fn)
 {
-	call_rcu(&fn->rcu, node_free_rcu);
+	kfree_rcu(fn, rcu);
 	net->ipv6.rt6_stats->fib_nodes--;
 }
 
net/ipv6/xfrm6_tunnel.c +1 -7
diff -u -p a/net/ipv6/xfrm6_tunnel.c b/net/ipv6/xfrm6_tunnel.c
@@ -178,12 +178,6 @@ __be32 xfrm6_tunnel_alloc_spi(struct net
 }
 EXPORT_SYMBOL(xfrm6_tunnel_alloc_spi);
 
-static void x6spi_destroy_rcu(struct rcu_head *head)
-{
-	kmem_cache_free(xfrm6_tunnel_spi_kmem,
-			container_of(head, struct xfrm6_tunnel_spi, rcu_head));
-}
-
 static void xfrm6_tunnel_free_spi(struct net *net, xfrm_address_t *saddr)
 {
 	struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
@@ -200,7 +194,7 @@ static void xfrm6_tunnel_free_spi(struct
 			if (refcount_dec_and_test(&x6spi->refcnt)) {
 				hlist_del_rcu(&x6spi->list_byaddr);
 				hlist_del_rcu(&x6spi->list_byspi);
-				call_rcu(&x6spi->rcu_head, x6spi_destroy_rcu);
+				kfree_rcu(x6spi, rcu_head);
 				break;
 			}
 		}
net/kcm/kcmsock.c +1 -9
diff -u -p a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c
@@ -1584,14 +1584,6 @@ static int kcm_ioctl(struct socket *sock
 	return err;
 }
 
-static void free_mux(struct rcu_head *rcu)
-{
-	struct kcm_mux *mux = container_of(rcu,
-	    struct kcm_mux, rcu);
-
-	kmem_cache_free(kcm_muxp, mux);
-}
-
 static void release_mux(struct kcm_mux *mux)
 {
 	struct kcm_net *knet = mux->knet;
@@ -1619,7 +1611,7 @@ static void release_mux(struct kcm_mux *
 	knet->count--;
 	mutex_unlock(&knet->mutex);
 
-	call_rcu(&mux->rcu, free_mux);
+	kfree_rcu(mux, rcu);
 }
 
 static void kcm_done(struct kcm_sock *kcm)
net/netfilter/nf_conncount.c +1 -9
diff -u -p a/net/netfilter/nf_conncount.c b/net/netfilter/nf_conncount.c
@@ -275,14 +275,6 @@ bool nf_conncount_gc_list(struct net *ne
 }
 EXPORT_SYMBOL_GPL(nf_conncount_gc_list);
 
-static void __tree_nodes_free(struct rcu_head *h)
-{
-	struct nf_conncount_rb *rbconn;
-
-	rbconn = container_of(h, struct nf_conncount_rb, rcu_head);
-	kmem_cache_free(conncount_rb_cachep, rbconn);
-}
-
 /* caller must hold tree nf_conncount_locks[] lock */
 static void tree_nodes_free(struct rb_root *root,
 			    struct nf_conncount_rb *gc_nodes[],
@@ -295,7 +287,7 @@ static void tree_nodes_free(struct rb_ro
 		spin_lock(&rbconn->list.list_lock);
 		if (!rbconn->list.count) {
 			rb_erase(&rbconn->node, root);
-			call_rcu(&rbconn->rcu_head, __tree_nodes_free);
+			kfree_rcu(rbconn, rcu_head);
 		}
 		spin_unlock(&rbconn->list.list_lock);
 	}
net/netfilter/nf_conntrack_expect.c +1 -9
diff -u -p a/net/netfilter/nf_conntrack_expect.c b/net/netfilter/nf_conntrack_expect.c
@@ -367,18 +367,10 @@ void nf_ct_expect_init(struct nf_conntra
 }
 EXPORT_SYMBOL_GPL(nf_ct_expect_init);
 
-static void nf_ct_expect_free_rcu(struct rcu_head *head)
-{
-	struct nf_conntrack_expect *exp;
-
-	exp = container_of(head, struct nf_conntrack_expect, rcu);
-	kmem_cache_free(nf_ct_expect_cachep, exp);
-}
-
 void nf_ct_expect_put(struct nf_conntrack_expect *exp)
 {
 	if (refcount_dec_and_test(&exp->use))
-		call_rcu(&exp->rcu, nf_ct_expect_free_rcu);
+		kfree_rcu(exp, rcu);
 }
 EXPORT_SYMBOL_GPL(nf_ct_expect_put);
 
net/netfilter/xt_hashlimit.c +1 -8
diff -u -p a/net/netfilter/xt_hashlimit.c b/net/netfilter/xt_hashlimit.c
@@ -256,18 +256,11 @@ dsthash_alloc_init(struct xt_hashlimit_h
 	return ent;
 }
 
-static void dsthash_free_rcu(struct rcu_head *head)
-{
-	struct dsthash_ent *ent = container_of(head, struct dsthash_ent, rcu);
-
-	kmem_cache_free(hashlimit_cachep, ent);
-}
-
 static inline void
 dsthash_free(struct xt_hashlimit_htable *ht, struct dsthash_ent *ent)
 {
 	hlist_del_rcu(&ent->node);
-	call_rcu(&ent->rcu, dsthash_free_rcu);
+	kfree_rcu(ent, rcu);
 	ht->count--;
 }
 static void htable_gc(struct work_struct *work);
#67 lore raw
Quoted 6 lines · Julia Lawall

Hello,

I have rerun the semantic patch that removes call_rcu calls in cases where the callback function just does some pointer arithmetic and calls kmem_cache_free. Let me know if this looks ok, and if so, I can make a more formal patch submission.

They look good to me, thank you!

Thanx, Paul

Quoted 469 lines

This is against:

commit 75b607fab38d149f232f01eae5e6392b394dd659 (HEAD -> master, origin/master, origin/HEAD)
Merge: 5b7c893ed5ed e0ed52154e86
Author: Linus Torvalds <[email protected]>
Date: Tue Oct 8 12:54:04 2024 -0700

Merge tag 'sched_ext-for-6.12-rc2-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext

julia

diff -u -p a/arch/powerpc/kvm/book3s_mmu_hpte.c b/arch/powerpc/kvm/book3s_mmu_hpte.c

arch/powerpc/kvm/book3s_mmu_hpte.c +1 -7
@@ -92,12 +92,6 @@ void kvmppc_mmu_hpte_cache_map(struct kv
 	spin_unlock(&vcpu3s->mmu_lock);
 }
 
-static void free_pte_rcu(struct rcu_head *head)
-{
-	struct hpte_cache *pte = container_of(head, struct hpte_cache, rcu_head);
-	kmem_cache_free(hpte_cache, pte);
-}
-
 static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
 {
 	struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
@@ -126,7 +120,7 @@ static void invalidate_pte(struct kvm_vc
 
 	spin_unlock(&vcpu3s->mmu_lock);
 
-	call_rcu(&pte->rcu_head, free_pte_rcu);
+	kfree_rcu(pte, rcu_head);
 }
 
 static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu)

diff -u -p a/block/blk-ioc.c b/block/blk-ioc.c

block/blk-ioc.c +1 -8
@@ -32,13 +32,6 @@ static void get_io_context(struct io_con
 	atomic_long_inc(&ioc->refcount);
 }
 
-static void icq_free_icq_rcu(struct rcu_head *head)
-{
-	struct io_cq *icq = container_of(head, struct io_cq, __rcu_head);
-
-	kmem_cache_free(icq->__rcu_icq_cache, icq);
-}
-
 /*
  * Exit an icq. Called with ioc locked for blk-mq, and with both ioc
  * and queue locked for legacy.
@@ -102,7 +95,7 @@ static void ioc_destroy_icq(struct io_cq
 	 */
 	icq->__rcu_icq_cache = et->icq_cache;
 	icq->flags |= ICQ_DESTROYED;
-	call_rcu(&icq->__rcu_head, icq_free_icq_rcu);
+	kfree_rcu(icq, __rcu_head);
 }
 
 /*

diff -u -p a/drivers/net/wireguard/allowedips.c b/drivers/net/wireguard/allowedips.c

drivers/net/wireguard/allowedips.c +2 -7
@@ -48,11 +48,6 @@ static void push_rcu(struct allowedips_n
 	}
 }
 
-static void node_free_rcu(struct rcu_head *rcu)
-{
-	kmem_cache_free(node_cache, container_of(rcu, struct allowedips_node, rcu));
-}
-
 static void root_free_rcu(struct rcu_head *rcu)
 {
 	struct allowedips_node *node, *stack[MAX_ALLOWEDIPS_DEPTH] = {
@@ -330,13 +325,13 @@ void wg_allowedips_remove_by_peer(struct
 			child = rcu_dereference_protected(
 					parent->bit[!(node->parent_bit_packed & 1)],
 					lockdep_is_held(lock));
-		call_rcu(&node->rcu, node_free_rcu);
+		kfree_rcu(node, rcu);
 		if (!free_parent)
 			continue;
 		if (child)
 			child->parent_bit_packed = parent->parent_bit_packed;
 		*(struct allowedips_node **)(parent->parent_bit_packed & ~3UL) = child;
-		call_rcu(&parent->rcu, node_free_rcu);
+		kfree_rcu(parent, rcu);
 	}
 }
 

diff -u -p a/fs/ecryptfs/dentry.c b/fs/ecryptfs/dentry.c

fs/ecryptfs/dentry.c +1 -7
@@ -51,12 +51,6 @@ static int ecryptfs_d_revalidate(struct
 
 struct kmem_cache *ecryptfs_dentry_info_cache;
 
-static void ecryptfs_dentry_free_rcu(struct rcu_head *head)
-{
-	kmem_cache_free(ecryptfs_dentry_info_cache,
-		container_of(head, struct ecryptfs_dentry_info, rcu));
-}
-
 /**
  * ecryptfs_d_release
  * @dentry: The ecryptfs dentry
@@ -68,7 +62,7 @@ static void ecryptfs_d_release(struct de
 	struct ecryptfs_dentry_info *p = dentry->d_fsdata;
 	if (p) {
 		path_put(&p->lower_path);
-		call_rcu(&p->rcu, ecryptfs_dentry_free_rcu);
+		kfree_rcu(p, rcu);
 	}
 }
 

diff -u -p a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c

fs/nfsd/nfs4state.c +1 -8
@@ -572,13 +572,6 @@ opaque_hashval(const void *ptr, int nbyt
 	return x;
 }
 
-static void nfsd4_free_file_rcu(struct rcu_head *rcu)
-{
-	struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
-
-	kmem_cache_free(file_slab, fp);
-}
-
 void
 put_nfs4_file(struct nfs4_file *fi)
 {
@@ -586,7 +579,7 @@ put_nfs4_file(struct nfs4_file *fi)
 		nfsd4_file_hash_remove(fi);
 		WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
 		WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
-		call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
+		kfree_rcu(fi, fi_rcu);
 	}
 }
 

diff -u -p a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c

kernel/time/posix-timers.c +1 -8
@@ -413,18 +413,11 @@ static struct k_itimer * alloc_posix_tim
 	return tmr;
 }
 
-static void k_itimer_rcu_free(struct rcu_head *head)
-{
-	struct k_itimer *tmr = container_of(head, struct k_itimer, rcu);
-
-	kmem_cache_free(posix_timers_cache, tmr);
-}
-
 static void posix_timer_free(struct k_itimer *tmr)
 {
 	put_pid(tmr->it_pid);
 	sigqueue_free(tmr->sigq);
-	call_rcu(&tmr->rcu, k_itimer_rcu_free);
+	kfree_rcu(tmr, rcu);
 }
 
 static void posix_timer_unhash_and_free(struct k_itimer *tmr)

diff -u -p a/net/batman-adv/translation-table.c b/net/batman-adv/translation-table.c

net/batman-adv/translation-table.c +1 -14
@@ -408,19 +408,6 @@ static void batadv_tt_global_size_dec(st
 }
 
 /**
- * batadv_tt_orig_list_entry_free_rcu() - free the orig_entry
- * @rcu: rcu pointer of the orig_entry
- */
-static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
-{
-	struct batadv_tt_orig_list_entry *orig_entry;
-
-	orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
-
-	kmem_cache_free(batadv_tt_orig_cache, orig_entry);
-}
-
-/**
  * batadv_tt_orig_list_entry_release() - release tt orig entry from lists and
  *  queue for free after rcu grace period
  * @ref: kref pointer of the tt orig entry
@@ -433,7 +420,7 @@ static void batadv_tt_orig_list_entry_re
 				  refcount);
 
 	batadv_orig_node_put(orig_entry->orig_node);
-	call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
+	kfree_rcu(orig_entry, rcu);
 }
 
 /**

diff -u -p a/net/bridge/br_fdb.c b/net/bridge/br_fdb.c

net/bridge/br_fdb.c +1 -8
@@ -73,13 +73,6 @@ static inline int has_expired(const stru
 	       time_before_eq(fdb->updated + hold_time(br), jiffies);
 }
 
-static void fdb_rcu_free(struct rcu_head *head)
-{
-	struct net_bridge_fdb_entry *ent
-		= container_of(head, struct net_bridge_fdb_entry, rcu);
-	kmem_cache_free(br_fdb_cache, ent);
-}
-
 static int fdb_to_nud(const struct net_bridge *br,
 		      const struct net_bridge_fdb_entry *fdb)
 {
@@ -329,7 +322,7 @@ static void fdb_delete(struct net_bridge
 	if (test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED, &f->flags))
 		atomic_dec(&br->fdb_n_learned);
 	fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
-	call_rcu(&f->rcu, fdb_rcu_free);
+	kfree_rcu(f, rcu);
 }
 
 /* Delete a local entry if no other port had the same address.

diff -u -p a/net/can/gw.c b/net/can/gw.c

net/can/gw.c +3 -10
@@ -577,13 +577,6 @@ static inline void cgw_unregister_filter
 			  gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
 }
 
-static void cgw_job_free_rcu(struct rcu_head *rcu_head)
-{
-	struct cgw_job *gwj = container_of(rcu_head, struct cgw_job, rcu);
-
-	kmem_cache_free(cgw_cache, gwj);
-}
-
 static int cgw_notifier(struct notifier_block *nb,
 			unsigned long msg, void *ptr)
 {
@@ -603,7 +596,7 @@ static int cgw_notifier(struct notifier_
 			if (gwj->src.dev == dev || gwj->dst.dev == dev) {
 				hlist_del(&gwj->list);
 				cgw_unregister_filter(net, gwj);
-				call_rcu(&gwj->rcu, cgw_job_free_rcu);
+				kfree_rcu(gwj, rcu);
 			}
 		}
 	}
@@ -1168,7 +1161,7 @@ static void cgw_remove_all_jobs(struct n
 	hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
 		hlist_del(&gwj->list);
 		cgw_unregister_filter(net, gwj);
-		call_rcu(&gwj->rcu, cgw_job_free_rcu);
+		kfree_rcu(gwj, rcu);
 	}
 }
 
@@ -1236,7 +1229,7 @@ static int cgw_remove_job(struct sk_buff
 
 		hlist_del(&gwj->list);
 		cgw_unregister_filter(net, gwj);
-		call_rcu(&gwj->rcu, cgw_job_free_rcu);
+		kfree_rcu(gwj, rcu);
 		err = 0;
 		break;
 	}

diff -u -p a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c

net/ipv4/fib_trie.c +1 -7
@@ -292,15 +292,9 @@ static const int inflate_threshold = 50;
 static const int halve_threshold_root = 15;
 static const int inflate_threshold_root = 30;
 
-static void __alias_free_mem(struct rcu_head *head)
-{
-	struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
-	kmem_cache_free(fn_alias_kmem, fa);
-}
-
 static inline void alias_free_mem_rcu(struct fib_alias *fa)
 {
-	call_rcu(&fa->rcu, __alias_free_mem);
+	kfree_rcu(fa, rcu);
 }
 
 #define TNODE_VMALLOC_MAX \

diff -u -p a/net/ipv4/inetpeer.c b/net/ipv4/inetpeer.c

net/ipv4/inetpeer.c +2 -7
@@ -128,11 +128,6 @@ static struct inet_peer *lookup(const st
 	return NULL;
 }
 
-static void inetpeer_free_rcu(struct rcu_head *head)
-{
-	kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
-}
-
 /* perform garbage collect on all items stacked during a lookup */
 static void inet_peer_gc(struct inet_peer_base *base,
 			 struct inet_peer *gc_stack[],
@@ -168,7 +163,7 @@ static void inet_peer_gc(struct inet_pee
 		if (p) {
 			rb_erase(&p->rb_node, &base->rb_root);
 			base->total--;
-			call_rcu(&p->rcu, inetpeer_free_rcu);
+			kfree_rcu(p, rcu);
 		}
 	}
 }
@@ -242,7 +237,7 @@ void inet_putpeer(struct inet_peer *p)
 	WRITE_ONCE(p->dtime, (__u32)jiffies);
 
 	if (refcount_dec_and_test(&p->refcnt))
-		call_rcu(&p->rcu, inetpeer_free_rcu);
+		kfree_rcu(p, rcu);
 }
 EXPORT_SYMBOL_GPL(inet_putpeer);
 

diff -u -p a/net/ipv6/ip6_fib.c b/net/ipv6/ip6_fib.c

net/ipv6/ip6_fib.c +1 -8
@@ -198,16 +198,9 @@ static void node_free_immediate(struct n
 	net->ipv6.rt6_stats->fib_nodes--;
 }
 
-static void node_free_rcu(struct rcu_head *head)
-{
-	struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
-
-	kmem_cache_free(fib6_node_kmem, fn);
-}
-
 static void node_free(struct net *net, struct fib6_node *fn)
 {
-	call_rcu(&fn->rcu, node_free_rcu);
+	kfree_rcu(fn, rcu);
 	net->ipv6.rt6_stats->fib_nodes--;
 }
 

diff -u -p a/net/ipv6/xfrm6_tunnel.c b/net/ipv6/xfrm6_tunnel.c

net/ipv6/xfrm6_tunnel.c +1 -7
@@ -178,12 +178,6 @@ __be32 xfrm6_tunnel_alloc_spi(struct net
 }
 EXPORT_SYMBOL(xfrm6_tunnel_alloc_spi);
 
-static void x6spi_destroy_rcu(struct rcu_head *head)
-{
-	kmem_cache_free(xfrm6_tunnel_spi_kmem,
-			container_of(head, struct xfrm6_tunnel_spi, rcu_head));
-}
-
 static void xfrm6_tunnel_free_spi(struct net *net, xfrm_address_t *saddr)
 {
 	struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
@@ -200,7 +194,7 @@ static void xfrm6_tunnel_free_spi(struct
 			if (refcount_dec_and_test(&x6spi->refcnt)) {
 				hlist_del_rcu(&x6spi->list_byaddr);
 				hlist_del_rcu(&x6spi->list_byspi);
-				call_rcu(&x6spi->rcu_head, x6spi_destroy_rcu);
+				kfree_rcu(x6spi, rcu_head);
 				break;
 			}
 		}

diff -u -p a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c

net/kcm/kcmsock.c +1 -9
@@ -1584,14 +1584,6 @@ static int kcm_ioctl(struct socket *sock
 	return err;
 }
 
-static void free_mux(struct rcu_head *rcu)
-{
-	struct kcm_mux *mux = container_of(rcu,
-	    struct kcm_mux, rcu);
-
-	kmem_cache_free(kcm_muxp, mux);
-}
-
 static void release_mux(struct kcm_mux *mux)
 {
 	struct kcm_net *knet = mux->knet;
@@ -1619,7 +1611,7 @@ static void release_mux(struct kcm_mux *
 	knet->count--;
 	mutex_unlock(&knet->mutex);
 
-	call_rcu(&mux->rcu, free_mux);
+	kfree_rcu(mux, rcu);
 }
 
 static void kcm_done(struct kcm_sock *kcm)

diff -u -p a/net/netfilter/nf_conncount.c b/net/netfilter/nf_conncount.c

net/netfilter/nf_conncount.c +1 -9
@@ -275,14 +275,6 @@ bool nf_conncount_gc_list(struct net *ne
 }
 EXPORT_SYMBOL_GPL(nf_conncount_gc_list);
 
-static void __tree_nodes_free(struct rcu_head *h)
-{
-	struct nf_conncount_rb *rbconn;
-
-	rbconn = container_of(h, struct nf_conncount_rb, rcu_head);
-	kmem_cache_free(conncount_rb_cachep, rbconn);
-}
-
 /* caller must hold tree nf_conncount_locks[] lock */
 static void tree_nodes_free(struct rb_root *root,
 			    struct nf_conncount_rb *gc_nodes[],
@@ -295,7 +287,7 @@ static void tree_nodes_free(struct rb_ro
 		spin_lock(&rbconn->list.list_lock);
 		if (!rbconn->list.count) {
 			rb_erase(&rbconn->node, root);
-			call_rcu(&rbconn->rcu_head, __tree_nodes_free);
+			kfree_rcu(rbconn, rcu_head);
 		}
 		spin_unlock(&rbconn->list.list_lock);
 	}

diff -u -p a/net/netfilter/nf_conntrack_expect.c b/net/netfilter/nf_conntrack_expect.c

net/netfilter/nf_conntrack_expect.c +1 -9
@@ -367,18 +367,10 @@ void nf_ct_expect_init(struct nf_conntra
 }
 EXPORT_SYMBOL_GPL(nf_ct_expect_init);
 
-static void nf_ct_expect_free_rcu(struct rcu_head *head)
-{
-	struct nf_conntrack_expect *exp;
-
-	exp = container_of(head, struct nf_conntrack_expect, rcu);
-	kmem_cache_free(nf_ct_expect_cachep, exp);
-}
-
 void nf_ct_expect_put(struct nf_conntrack_expect *exp)
 {
 	if (refcount_dec_and_test(&exp->use))
-		call_rcu(&exp->rcu, nf_ct_expect_free_rcu);
+		kfree_rcu(exp, rcu);
 }
 EXPORT_SYMBOL_GPL(nf_ct_expect_put);
 

diff -u -p a/net/netfilter/xt_hashlimit.c b/net/netfilter/xt_hashlimit.c

net/netfilter/xt_hashlimit.c +1 -8
@@ -256,18 +256,11 @@ dsthash_alloc_init(struct xt_hashlimit_h
 	return ent;
 }
 
-static void dsthash_free_rcu(struct rcu_head *head)
-{
-	struct dsthash_ent *ent = container_of(head, struct dsthash_ent, rcu);
-
-	kmem_cache_free(hashlimit_cachep, ent);
-}
-
 static inline void
 dsthash_free(struct xt_hashlimit_htable *ht, struct dsthash_ent *ent)
 {
 	hlist_del_rcu(&ent->node);
-	call_rcu(&ent->rcu, dsthash_free_rcu);
+	kfree_rcu(ent, rcu);
 	ht->count--;
 }
 static void htable_gc(struct work_struct *work);
#68 lore raw
In reply to Paul E. McKenney
Quoted 27 lines · Paul E. McKenney
Quoted 17 lines · Uladzislau Rezki
Quoted 8 lines · Uladzislau Rezki
Quoted 6 lines

mm/slab_common.c void slab_kmem_cache_release(struct kmem_cache *s) · line 495

Quoted 4 lines
mm/slab_common.c +4 -0
+
+	s = container_of(work, struct kmem_cache, async_destroy_work);
+
+	// XXX use the real kmem_cache_free_barrier() or similar thing here

It implies that we need to introduce kfree_rcu_barrier(), a new API, which i wanted to avoid initially.

Quoted 9 lines

I wanted to avoid new API or flags for kfree_rcu() users and this would be achieved. The barrier is used internally so I don't consider that an API to avoid. How difficult is the implementation is another question, depending on how the current batching works. Once (if) we have sheaves proven to work and move kfree_rcu() fully into SLUB, the barrier might also look different and hopefully easier. So maybe it's not worth to invest too much into that barrier and just go for the potentially longer, but easier to implement?

Quoted 6 lines

Right. I agree here. If the cache is not empty, OK, we just defer the work, even we can use a big 21 seconds delay, after that we just "warn" if it is still not empty and leave it as it is, i.e. emit a warning and we are done.

Destroying the cache is not something that must happen right away.

OK, I have to ask...

Quoted 8 lines

Suppose that the cache is created and destroyed by a module and init/cleanup time, respectively. Suppose that this module is rmmod'ed then very quickly insmod'ed.

Do we need to fail the insmod if the kmem_cache has not yet been fully cleaned up? If not, do we have two versions of the same kmem_cache in /proc during the overlap time?

No fail :) If same cache is created several times, its s->refcount gets increased, so, it does not create two entries in the "slabinfo". But i agree that your point is good! We need to be carefully with removing and simultaneous creating.

From the first glance, there is a refcounter and a global "slab_mutex" which is used to protect a critical section. Destroying is almost fully protected(as noted above, by a global mutex) with one exception, it is:

static void kmem_cache_release(struct kmem_cache *s)

{
	if (slab_state >= FULL) {
		sysfs_slab_unlink(s);
		sysfs_slab_release(s);
	} else {
		slab_kmem_cache_release(s);
	}
}

this one can race, IMO.

--
Uladzislau Rezki

#69 lore raw
Quoted 32 lines · Uladzislau Rezki
Quoted 31 lines · Paul E. McKenney
Quoted 17 lines · Uladzislau Rezki
Quoted 8 lines · Uladzislau Rezki
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+	s = container_of(work, struct kmem_cache, async_destroy_work);
+
+	// XXX use the real kmem_cache_free_barrier() or similar thing here

It implies that we need to introduce kfree_rcu_barrier(), a new API, which i wanted to avoid initially.

Quoted 9 lines

I wanted to avoid new API or flags for kfree_rcu() users and this would be achieved. The barrier is used internally so I don't consider that an API to avoid. How difficult is the implementation is another question, depending on how the current batching works. Once (if) we have sheaves proven to work and move kfree_rcu() fully into SLUB, the barrier might also look different and hopefully easier. So maybe it's not worth to invest too much into that barrier and just go for the potentially longer, but easier to implement?

Quoted 6 lines

Right. I agree here. If the cache is not empty, OK, we just defer the work, even we can use a big 21 seconds delay, after that we just "warn" if it is still not empty and leave it as it is, i.e. emit a warning and we are done.

Destroying the cache is not something that must happen right away.

OK, I have to ask...

Suppose that the cache is created and destroyed by a module and init/cleanup time, respectively. Suppose that this module is rmmod'ed then very quickly insmod'ed.

Quoted 8 lines
Quoted 4 lines

Do we need to fail the insmod if the kmem_cache has not yet been fully cleaned up? If not, do we have two versions of the same kmem_cache in /proc during the overlap time?

No fail :) If same cache is created several times, its s->refcount gets increased, so, it does not create two entries in the "slabinfo". But i agree that your point is good! We need to be carefully with removing and simultaneous creating.

Note that this merging may be disabled or not happen due to various flags on the cache being incompatible with it. And I want to actually make sure it never happens for caches being already destroyed as that would lead to use-after-free (the workfn doesn't recheck the refcount in case a merge would happen during the grace period)

mm/slab_common.c +3 -2
@@ -150,9 +150,10 @@ int slab_unmergeable(struct kmem_cache *s)
 #endif
 
        /*
-        * We may have set a slab to be unmergeable during bootstrap.
+        * We may have set a cache to be unmergeable during bootstrap.
+        * 0 is for cache being destroyed asynchronously
         */
-       if (s->refcount < 0)
+       if (s->refcount <= 0)
                return 1;
 
        return 0;
Quoted 18 lines

From the first glance, there is a refcounter and a global "slab_mutex" which is used to protect a critical section. Destroying is almost fully protected(as noted above, by a global mutex) with one exception, it is:

static void kmem_cache_release(struct kmem_cache *s)

{
	if (slab_state >= FULL) {
		sysfs_slab_unlink(s);
		sysfs_slab_release(s);
	} else {
		slab_kmem_cache_release(s);
	}
}

this one can race, IMO.

--
Uladzislau Rezki

#70 lore raw
Quoted 54 lines · Vlastimil Babka
Quoted 32 lines · Uladzislau Rezki
Quoted 31 lines · Paul E. McKenney
Quoted 17 lines · Uladzislau Rezki
Quoted 8 lines · Uladzislau Rezki
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+	s = container_of(work, struct kmem_cache, async_destroy_work);
+
+	// XXX use the real kmem_cache_free_barrier() or similar thing here

It implies that we need to introduce kfree_rcu_barrier(), a new API, which i wanted to avoid initially.

Quoted 9 lines

I wanted to avoid new API or flags for kfree_rcu() users and this would be achieved. The barrier is used internally so I don't consider that an API to avoid. How difficult is the implementation is another question, depending on how the current batching works. Once (if) we have sheaves proven to work and move kfree_rcu() fully into SLUB, the barrier might also look different and hopefully easier. So maybe it's not worth to invest too much into that barrier and just go for the potentially longer, but easier to implement?

Quoted 6 lines

Right. I agree here. If the cache is not empty, OK, we just defer the work, even we can use a big 21 seconds delay, after that we just "warn" if it is still not empty and leave it as it is, i.e. emit a warning and we are done.

Destroying the cache is not something that must happen right away.

OK, I have to ask...

Suppose that the cache is created and destroyed by a module and init/cleanup time, respectively. Suppose that this module is rmmod'ed then very quickly insmod'ed.

Quoted 8 lines
Quoted 4 lines

Do we need to fail the insmod if the kmem_cache has not yet been fully cleaned up? If not, do we have two versions of the same kmem_cache in /proc during the overlap time?

No fail :) If same cache is created several times, its s->refcount gets increased, so, it does not create two entries in the "slabinfo". But i agree that your point is good! We need to be carefully with removing and simultaneous creating.

Note that this merging may be disabled or not happen due to various flags on the cache being incompatible with it. And I want to actually make sure it never happens for caches being already destroyed as that would lead to use-after-free (the workfn doesn't recheck the refcount in case a merge would happen during the grace period)

mm/slab_common.c +0 -0
@@ -150,9 +150,10 @@ int slab_unmergeable(struct kmem_cache *s)
 #endif
 

mm/slab_common.c int slab_unmergeable(struct kmem_cache *s) · line 159

Quoted 10 lines
mm/slab_common.c +3 -2
        /*
-        * We may have set a slab to be unmergeable during bootstrap.
+        * We may have set a cache to be unmergeable during bootstrap.
+        * 0 is for cache being destroyed asynchronously
         */
-       if (s->refcount < 0)
+       if (s->refcount <= 0)
                return 1;
 
        return 0;

OK, i see such flags, SLAB_NO_MERGE. Then i was wrong, it can create two different slabs.

Thanks!

--
Uladzislau Rezki

#71 lore raw
In reply to Vlastimil Babka
Quoted 16 lines · Vlastimil Babka
Quoted 15 lines · Paul E. McKenney
Quoted 13 lines · Vlastimil Babka
Quoted 6 lines · Jason A. Donenfeld
Quoted 5 lines · Paul E. McKenney

o Make the current kmem_cache_destroy() asynchronously wait for
all memory to be returned, then complete the destruction. (This gets rid of a valuable debugging technique because in normal use, it is a bug to attempt to destroy a kmem_cache that has objects still allocated.)

This seems like the best option to me. As Jason already said, the debugging technique is not affected significantly, if the warning just occurs asynchronously later. The module can be already unloaded at that point, as the leak is never checked programatically anyway to control further execution, it's just a splat in dmesg.

Quoted 7 lines
Quoted 1 line

Works for me!

Great. So this is how a prototype could look like, hopefully? The kunit test does generate the splat for me, which should be because the rcu_barrier() in the implementation (marked to be replaced with the real thing) is really insufficient. Note the test itself passes as this kind of error isn't wired up properly.

;-) ;-) ;-)

Some might want confirmation that their cleanup efforts succeeded, but if so, I will let them make that known.

Quoted 2 lines

Another thing to resolve is the marked comment about kasan_shutdown() with potential kfree_rcu()'s in flight.

Could that simply move to the worker function? (Hey, had to ask!)

Quoted 2 lines

Also you need CONFIG_SLUB_DEBUG enabled otherwise node_nr_slabs() is a no-op and it might fail to notice the pending slabs. This will need to change.

Agreed.

Looks generally good. A few questions below, to be taken with a grain of salt.

Thanx, Paul

Quoted 25 lines

----8<----

lib/slub_kunit.c +9 -0
index e6667a28c014..e3e4d0ca40b7 100644
@@ -5,6 +5,7 @@
 #include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
+#include <linux/rcupdate.h>
 #include "../mm/slab.h"
 
 static struct kunit_resource resource;
@@ -157,6 +158,26 @@ static void test_kmalloc_redzone_access(struct kunit *test)
 	kmem_cache_destroy(s);
 }
 
+struct test_kfree_rcu_struct {
+	struct rcu_head rcu;
+};
+
+static void test_kfree_rcu(struct kunit *test)
+{
+	struct kmem_cache *s = test_kmem_cache_create("TestSlub_kfree_rcu",
+				sizeof(struct test_kfree_rcu_struct),

lib/slub_kunit.c static void test_kmalloc_redzone_access(struct kunit *test) · line 178

Quoted 10 lines
lib/slub_kunit.c +10 -0
+				SLAB_NO_MERGE);
+	struct test_kfree_rcu_struct *p = kmem_cache_alloc(s, GFP_KERNEL);
+
+	kasan_disable_current();
+
+	KUNIT_EXPECT_EQ(test, 0, slab_errors);
+
+	kasan_enable_current();
+	kfree_rcu(p, rcu);
+	kmem_cache_destroy(s);

Looks like the type of test for this!

Quoted 86 lines
lib/slub_kunit.c +3 -0
+}
+
 static int test_init(struct kunit *test)
 {
 	slab_errors = 0;
@@ -177,6 +198,7 @@ static struct kunit_case test_cases[] = {
 
 	KUNIT_CASE(test_clobber_redzone_free),
 	KUNIT_CASE(test_kmalloc_redzone_access),
+	KUNIT_CASE(test_kfree_rcu),
 	{}
 };
 
mm/slab.h +3 -1
index b16e63191578..a0295600af92 100644
@@ -277,6 +277,8 @@ struct kmem_cache {
 	unsigned int red_left_pad;	/* Left redzone padding size */
 	const char *name;		/* Name (only for display!) */
 	struct list_head list;		/* List of slab caches */
+	struct work_struct async_destroy_work;
+
 #ifdef CONFIG_SYSFS
 	struct kobject kobj;		/* For sysfs */
 #endif
@@ -474,7 +476,7 @@ static inline bool is_kmalloc_cache(struct kmem_cache *s)
 			      SLAB_NO_USER_FLAGS)
 
 bool __kmem_cache_empty(struct kmem_cache *);
-int __kmem_cache_shutdown(struct kmem_cache *);
+int __kmem_cache_shutdown(struct kmem_cache *, bool);
 void __kmem_cache_release(struct kmem_cache *);
 int __kmem_cache_shrink(struct kmem_cache *);
 void slab_kmem_cache_release(struct kmem_cache *);
mm/slab_common.c +16 -2
index 5b1f996bed06..c5c356d0235d 100644
@@ -44,6 +44,8 @@ static LIST_HEAD(slab_caches_to_rcu_destroy);
 static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work);
 static DECLARE_WORK(slab_caches_to_rcu_destroy_work,
 		    slab_caches_to_rcu_destroy_workfn);
+static void kmem_cache_kfree_rcu_destroy_workfn(struct work_struct *work);
+
 
 /*
  * Set of flags that will prevent slab merging
@@ -234,6 +236,7 @@ static struct kmem_cache *create_cache(const char *name,
 
 	s->refcount = 1;
 	list_add(&s->list, &slab_caches);
+	INIT_WORK(&s->async_destroy_work, kmem_cache_kfree_rcu_destroy_workfn);
 	return s;
 
 out_free_cache:
@@ -449,12 +452,16 @@ static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work)
 	}
 }
 
-static int shutdown_cache(struct kmem_cache *s)
+static int shutdown_cache(struct kmem_cache *s, bool warn_inuse)
 {
 	/* free asan quarantined objects */
+	/*
+	 * XXX: is it ok to call this multiple times? and what happens with a
+	 * kfree_rcu() in flight that finishes after or in parallel with this?
+	 */
 	kasan_cache_shutdown(s);
 
-	if (__kmem_cache_shutdown(s) != 0)
+	if (__kmem_cache_shutdown(s, warn_inuse) != 0)
 		return -EBUSY;
 
 	list_del(&s->list);
@@ -477,6 +484,32 @@ void slab_kmem_cache_release(struct kmem_cache *s)
 	kmem_cache_free(kmem_cache, s);
 }
 
+static void kmem_cache_kfree_rcu_destroy_workfn(struct work_struct *work)
+{
+	struct kmem_cache *s;
+	int err = -EBUSY;
+	bool rcu_set;
+
+	s = container_of(work, struct kmem_cache, async_destroy_work);

mm/slab_common.c void slab_kmem_cache_release(struct kmem_cache *s) · line 503

Quoted 10 lines
mm/slab_common.c +10 -0
+
+	// XXX use the real kmem_cache_free_barrier() or similar thing here
+	rcu_barrier();
+
+	cpus_read_lock();
+	mutex_lock(&slab_mutex);
+
+	rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU;
+
+	err = shutdown_cache(s, true);

This is currently the only call to shutdown_cache()? So there is to be a way for the caller to have some influence over the value of that bool?

mm/slab_common.c void slab_kmem_cache_release(struct kmem_cache *s) · line 505

Quoted 2 lines
mm/slab_common.c +2 -0
+	WARN(err, "kmem_cache_destroy %s: Slab cache still has objects",
+	     s->name);

Don't we want to have some sort of delay here? Or is this the 21-second delay and/or kfree_rcu_barrier() mentioned before?

Quoted 64 lines
mm/slab_common.c +9 -3
+	mutex_unlock(&slab_mutex);
+	cpus_read_unlock();
+	if (!err && !rcu_set)
+		kmem_cache_release(s);
+}
+
 void kmem_cache_destroy(struct kmem_cache *s)
 {
 	int err = -EBUSY;
@@ -494,9 +527,9 @@ void kmem_cache_destroy(struct kmem_cache *s)
 	if (s->refcount)
 		goto out_unlock;
 
-	err = shutdown_cache(s);
-	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
-	     __func__, s->name, (void *)_RET_IP_);
+	err = shutdown_cache(s, false);
+	if (err)
+		schedule_work(&s->async_destroy_work);
 out_unlock:
 	mutex_unlock(&slab_mutex);
 	cpus_read_unlock();
mm/slub.c +5 -4
index 1617d8014ecd..4d435b3d2b5f 100644
@@ -5342,7 +5342,8 @@ static void list_slab_objects(struct kmem_cache *s, struct slab *slab,
  * This is called from __kmem_cache_shutdown(). We must take list_lock
  * because sysfs file might still access partial list after the shutdowning.
  */
-static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
+static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n,
+			 bool warn_inuse)
 {
 	LIST_HEAD(discard);
 	struct slab *slab, *h;
@@ -5353,7 +5354,7 @@ static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
 		if (!slab->inuse) {
 			remove_partial(n, slab);
 			list_add(&slab->slab_list, &discard);
-		} else {
+		} else if (warn_inuse) {
 			list_slab_objects(s, slab,
 			  "Objects remaining in %s on __kmem_cache_shutdown()");
 		}
@@ -5378,7 +5379,7 @@ bool __kmem_cache_empty(struct kmem_cache *s)
 /*
  * Release all resources used by a slab cache.
  */
-int __kmem_cache_shutdown(struct kmem_cache *s)
+int __kmem_cache_shutdown(struct kmem_cache *s, bool warn_inuse)
 {
 	int node;
 	struct kmem_cache_node *n;
@@ -5386,7 +5387,7 @@ int __kmem_cache_shutdown(struct kmem_cache *s)
 	flush_all_cpus_locked(s);
 	/* Attempt to free all objects */
 	for_each_kmem_cache_node(s, node, n) {
-		free_partial(s, n);
+		free_partial(s, n, warn_inuse);
 		if (n->nr_partial || node_nr_slabs(n))
 			return 1;
 	}
#72 lore raw
Quoted 19 lines · Paul E. McKenney
Quoted 18 lines · Vlastimil Babka
Quoted 16 lines · Paul E. McKenney
Quoted 8 lines · Vlastimil Babka
Quoted 6 lines · Jason A. Donenfeld
Quoted 5 lines · Paul E. McKenney

o Make the current kmem_cache_destroy() asynchronously wait for
all memory to be returned, then complete the destruction. (This gets rid of a valuable debugging technique because in normal use, it is a bug to attempt to destroy a kmem_cache that has objects still allocated.)

Quoted 5 lines

This seems like the best option to me. As Jason already said, the debugging technique is not affected significantly, if the warning just occurs asynchronously later. The module can be already unloaded at that point, as the leak is never checked programatically anyway to control further execution, it's just a splat in dmesg.

Works for me!

Quoted 7 lines
Quoted 5 lines

Great. So this is how a prototype could look like, hopefully? The kunit test does generate the splat for me, which should be because the rcu_barrier() in the implementation (marked to be replaced with the real thing) is really insufficient. Note the test itself passes as this kind of error isn't wired up properly.

;-) ;-) ;-)

Yeah yeah, I just used the kunit module as a convenient way add the code that should see if there's the splat :)

Quoted 2 lines

Some might want confirmation that their cleanup efforts succeeded, but if so, I will let them make that known.

It could be just the kunit test that could want that, but I don't see how it could wrap and inspect the result of the async handling and suppress the splats for intentionally triggered errors as many of the other tests do.

Quoted 4 lines
Quoted 2 lines

Another thing to resolve is the marked comment about kasan_shutdown() with potential kfree_rcu()'s in flight.

Could that simply move to the worker function? (Hey, had to ask!)

I think I had a reason why not, but I guess it could move. It would just mean that if any objects are quarantined, we'll go for the async freeing even though those could be flushed immediately. Guess that's not too bad.

Quoted 7 lines
Quoted 2 lines

Also you need CONFIG_SLUB_DEBUG enabled otherwise node_nr_slabs() is a no-op and it might fail to notice the pending slabs. This will need to change.

Agreed.

Looks generally good. A few questions below, to be taken with a grain of salt.

Thanks!

Quoted 10 lines

mm/slab_common.c void slab_kmem_cache_release(struct kmem_cache *s) · line 496

Quoted 10 lines
mm/slab_common.c +10 -0
+static void kmem_cache_kfree_rcu_destroy_workfn(struct work_struct *work)
+{
+	struct kmem_cache *s;
+	int err = -EBUSY;
+	bool rcu_set;
+
+	s = container_of(work, struct kmem_cache, async_destroy_work);
+
+	// XXX use the real kmem_cache_free_barrier() or similar thing here
+	rcu_barrier();

Note here's the barrier.

Quoted 9 lines

mm/slab_common.c void slab_kmem_cache_release(struct kmem_cache *s) · line 503

Quoted 6 lines
mm/slab_common.c +6 -0
+	cpus_read_lock();
+	mutex_lock(&slab_mutex);
+
+	rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU;
+
+	err = shutdown_cache(s, true);

This is currently the only call to shutdown_cache()? So there is to be a way for the caller to have some influence over the value of that bool?

Not the only caller, there's still the initial attempt in kmem_cache_destroy() itself below.

Quoted 6 lines

mm/slab_common.c void slab_kmem_cache_release(struct kmem_cache *s) · line 505

Quoted 3 lines
mm/slab_common.c +2 -0
+	WARN(err, "kmem_cache_destroy %s: Slab cache still has objects",
+	     s->name);

Don't we want to have some sort of delay here? Or is this the 21-second delay and/or kfree_rcu_barrier() mentioned before?

Yes this is after the barrier. The first immediate attempt to shutdown doesn't warn.

Quoted 13 lines
Quoted 13 lines
mm/slab_common.c +6 -0
+	mutex_unlock(&slab_mutex);
+	cpus_read_unlock();
+	if (!err && !rcu_set)
+		kmem_cache_release(s);
+}
+
 void kmem_cache_destroy(struct kmem_cache *s)
 {
 	int err = -EBUSY;
@@ -494,9 +527,9 @@ void kmem_cache_destroy(struct kmem_cache *s)
 	if (s->refcount)
 		goto out_unlock;
 
Quoted 6 lines

mm/slab_common.c void kmem_cache_destroy(struct kmem_cache *s) · line 532

Quoted 6 lines
mm/slab_common.c +3 -3
-	err = shutdown_cache(s);
-	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
-	     __func__, s->name, (void *)_RET_IP_);
+	err = shutdown_cache(s, false);
+	if (err)
+		schedule_work(&s->async_destroy_work);

And here's the initial attempt that used to warn but now doesn't and instead schedules the async one.

Quoted 45 lines
Quoted 45 lines
mm/slab_common.c +0 -0
 out_unlock:
 	mutex_unlock(&slab_mutex);
 	cpus_read_unlock();
mm/slub.c +5 -4
index 1617d8014ecd..4d435b3d2b5f 100644
@@ -5342,7 +5342,8 @@ static void list_slab_objects(struct kmem_cache *s, struct slab *slab,
  * This is called from __kmem_cache_shutdown(). We must take list_lock
  * because sysfs file might still access partial list after the shutdowning.
  */
-static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
+static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n,
+			 bool warn_inuse)
 {
 	LIST_HEAD(discard);
 	struct slab *slab, *h;
@@ -5353,7 +5354,7 @@ static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
 		if (!slab->inuse) {
 			remove_partial(n, slab);
 			list_add(&slab->slab_list, &discard);
-		} else {
+		} else if (warn_inuse) {
 			list_slab_objects(s, slab,
 			  "Objects remaining in %s on __kmem_cache_shutdown()");
 		}
@@ -5378,7 +5379,7 @@ bool __kmem_cache_empty(struct kmem_cache *s)
 /*
  * Release all resources used by a slab cache.
  */
-int __kmem_cache_shutdown(struct kmem_cache *s)
+int __kmem_cache_shutdown(struct kmem_cache *s, bool warn_inuse)
 {
 	int node;
 	struct kmem_cache_node *n;
@@ -5386,7 +5387,7 @@ int __kmem_cache_shutdown(struct kmem_cache *s)
 	flush_all_cpus_locked(s);
 	/* Attempt to free all objects */
 	for_each_kmem_cache_node(s, node, n) {
-		free_partial(s, n);
+		free_partial(s, n, warn_inuse);
 		if (n->nr_partial || node_nr_slabs(n))
 			return 1;
 	}
maintainer
#73 lore raw
In reply to Paul E. McKenney
Quoted 3 lines · On Wed, 12 Jun 2024 20:38:02 -0700 Paul E. McKenney wrote:

o Make rcu_barrier() wait for kfree_rcu() objects. (This is
surprisingly complex and will wait unnecessarily in some cases. However, it does preserve current code.)

Not sure how much mental capacity for API variations we expect from people using caches, but I feel like this would score the highest on Rusty's API scale. I'd even venture an opinion that it's less confusing to require cache users to have their own (trivial) callbacks than add API variants we can't error check even at runtime...

#74 lore raw
Quoted 10 lines · Jakub Kicinski
Quoted 3 lines · On Wed, 12 Jun 2024 20:38:02 -0700 Paul E. McKenney wrote:

o Make rcu_barrier() wait for kfree_rcu() objects. (This is
surprisingly complex and will wait unnecessarily in some cases. However, it does preserve current code.)

Not sure how much mental capacity for API variations we expect from people using caches, but I feel like this would score the highest on Rusty's API scale. I'd even venture an opinion that it's less confusing to require cache users to have their own (trivial) callbacks than add API variants we can't error check even at runtime...

Fair point, though please see Jason's emails.

And the underlying within-RCU mechanism is the same either way, so that API decision can be deferred for some time.

But the within-slab mechanism does have the advantage of also possibly simplifying reference-counting and the potential upcoming hazard pointers. On the other hand, I currently have no idea what level of violence this change would make to the slab subsystem.

Thanx, Paul

#75 lore raw
In reply to Paul E. McKenney
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

Quoted 7 lines

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

Also, notably, this patch needs additionally:

drivers/net/wireguard/allowedips.c +0 -1
index e4e1638fce1b..c95f6937c3f1 100644
@@ -377,7 +377,6 @@ int __init wg_allowedips_slab_init(void)
 
 void wg_allowedips_slab_uninit(void)
 {
-	rcu_barrier();
 	kmem_cache_destroy(node_cache);
 }
 

Once kmem_cache_destroy has been fixed to be deferrable.

I assume the other patches are similar -- an rcu_barrier() can be removed. So some manual meddling of these might be in order.

Jason

#76 lore raw
Quoted 40 lines · Jason A. Donenfeld
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

Quoted 7 lines

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

Also, notably, this patch needs additionally:

drivers/net/wireguard/allowedips.c +0 -1
index e4e1638fce1b..c95f6937c3f1 100644
@@ -377,7 +377,6 @@ int __init wg_allowedips_slab_init(void)
 
 void wg_allowedips_slab_uninit(void)
 {
-	rcu_barrier();
 	kmem_cache_destroy(node_cache);
 }
 

Once kmem_cache_destroy has been fixed to be deferrable.

Quoted 2 lines

I assume the other patches are similar -- an rcu_barrier() can be removed. So some manual meddling of these might be in order.

Assuming that the deferrable kmem_cache_destroy() is the option chosen, agreed.

Thanx, Paul

#77 lore raw
Quoted 39 lines · Paul E. McKenney
Quoted 38 lines · Jason A. Donenfeld
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

Quoted 7 lines

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

Also, notably, this patch needs additionally:

drivers/net/wireguard/allowedips.c +0 -1
index e4e1638fce1b..c95f6937c3f1 100644
@@ -377,7 +377,6 @@ int __init wg_allowedips_slab_init(void)
 
 void wg_allowedips_slab_uninit(void)
 {
-	rcu_barrier();
 	kmem_cache_destroy(node_cache);
 }
 
Quoted 8 lines
Quoted 4 lines

Once kmem_cache_destroy has been fixed to be deferrable.

I assume the other patches are similar -- an rcu_barrier() can be removed. So some manual meddling of these might be in order.

Assuming that the deferrable kmem_cache_destroy() is the option chosen, agreed.

<snip>
void kmem_cache_destroy(struct kmem_cache *s)

{
	int err = -EBUSY;
	bool rcu_set;

	if (unlikely(!s) || !kasan_check_byte(s))
		return;

	cpus_read_lock();
	mutex_lock(&slab_mutex);

	rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU;

	s->refcount--;
	if (s->refcount)
		goto out_unlock;

	err = shutdown_cache(s);
	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
	     __func__, s->name, (void *)_RET_IP_);
...
	cpus_read_unlock();
	if (!err && !rcu_set)
		kmem_cache_release(s);
}

<snip>

so we have SLAB_TYPESAFE_BY_RCU flag that defers freeing slab-pages and a cache by a grace period. Similar flag can be added, like SLAB_DESTROY_ONCE_FULLY_FREED, in this case a worker rearm itself if there are still objects which should be freed.

Any thoughts here?

--
Uladzislau Rezki

#78 lore raw
Quoted 74 lines · Uladzislau Rezki
Quoted 39 lines · Paul E. McKenney
Quoted 38 lines · Jason A. Donenfeld
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

Quoted 7 lines

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

Also, notably, this patch needs additionally:

drivers/net/wireguard/allowedips.c +0 -1
index e4e1638fce1b..c95f6937c3f1 100644
@@ -377,7 +377,6 @@ int __init wg_allowedips_slab_init(void)
 
 void wg_allowedips_slab_uninit(void)
 {
-	rcu_barrier();
 	kmem_cache_destroy(node_cache);
 }
 
Quoted 8 lines
Quoted 4 lines

Once kmem_cache_destroy has been fixed to be deferrable.

I assume the other patches are similar -- an rcu_barrier() can be removed. So some manual meddling of these might be in order.

Assuming that the deferrable kmem_cache_destroy() is the option chosen, agreed.

<snip>
void kmem_cache_destroy(struct kmem_cache *s)

{
	int err = -EBUSY;
	bool rcu_set;

	if (unlikely(!s) || !kasan_check_byte(s))
		return;

	cpus_read_lock();
	mutex_lock(&slab_mutex);

	rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU;

	s->refcount--;
	if (s->refcount)
		goto out_unlock;

	err = shutdown_cache(s);
	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
	     __func__, s->name, (void *)_RET_IP_);
...
	cpus_read_unlock();
	if (!err && !rcu_set)
		kmem_cache_release(s);
}
Quoted 8 lines

<snip>

so we have SLAB_TYPESAFE_BY_RCU flag that defers freeing slab-pages and a cache by a grace period. Similar flag can be added, like SLAB_DESTROY_ONCE_FULLY_FREED, in this case a worker rearm itself if there are still objects which should be freed.

Any thoughts here?

Wouldn't we also need some additional code to later check for all objects being freed to the slab, whether or not that code is initiated from kmem_cache_destroy()?

Either way, I am adding the SLAB_DESTROY_ONCE_FULLY_FREED possibility, thank you! [1]

Thanx, Paul

[1] https://docs.google.com/document/d/1v0rcZLvvjVGejT3523W0rDy_sLFu2LWc_NR3fQItZaA/edit?usp=sharing

#79 lore raw
Quoted 82 lines · Paul E. McKenney
Quoted 81 lines · Uladzislau Rezki
Quoted 39 lines · Paul E. McKenney
Quoted 38 lines · Jason A. Donenfeld
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

Quoted 7 lines

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

Also, notably, this patch needs additionally:

drivers/net/wireguard/allowedips.c +0 -1
index e4e1638fce1b..c95f6937c3f1 100644
@@ -377,7 +377,6 @@ int __init wg_allowedips_slab_init(void)
 
 void wg_allowedips_slab_uninit(void)
 {
-	rcu_barrier();
 	kmem_cache_destroy(node_cache);
 }
 
Quoted 8 lines
Quoted 4 lines

Once kmem_cache_destroy has been fixed to be deferrable.

I assume the other patches are similar -- an rcu_barrier() can be removed. So some manual meddling of these might be in order.

Assuming that the deferrable kmem_cache_destroy() is the option chosen, agreed.

<snip>
void kmem_cache_destroy(struct kmem_cache *s)

{
	int err = -EBUSY;
	bool rcu_set;

	if (unlikely(!s) || !kasan_check_byte(s))
		return;

	cpus_read_lock();
	mutex_lock(&slab_mutex);

	rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU;

	s->refcount--;
	if (s->refcount)
		goto out_unlock;

	err = shutdown_cache(s);
	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
	     __func__, s->name, (void *)_RET_IP_);
...
	cpus_read_unlock();
	if (!err && !rcu_set)
		kmem_cache_release(s);
}

<snip>

so we have SLAB_TYPESAFE_BY_RCU flag that defers freeing slab-pages and a cache by a grace period. Similar flag can be added, like SLAB_DESTROY_ONCE_FULLY_FREED, in this case a worker rearm itself if there are still objects which should be freed.

Quoted 6 lines
Quoted 1 line

Any thoughts here?

Wouldn't we also need some additional code to later check for all objects being freed to the slab, whether or not that code is initiated from kmem_cache_destroy()?

Same away as SLAB_TYPESAFE_BY_RCU is handled from the kmem_cache_destroy() function. It checks that flag and if it is true and extra worker is scheduled to perform a deferred(instead of right away) destroy after rcu_barrier() finishes.

--
Uladzislau Rezki

#80 lore raw
Quoted 85 lines · Uladzislau Rezki
Quoted 84 lines · Paul E. McKenney
Quoted 82 lines · Uladzislau Rezki
Quoted 39 lines · Paul E. McKenney
Quoted 38 lines · Jason A. Donenfeld
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

Quoted 7 lines

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

Also, notably, this patch needs additionally:

drivers/net/wireguard/allowedips.c +0 -1
index e4e1638fce1b..c95f6937c3f1 100644
@@ -377,7 +377,6 @@ int __init wg_allowedips_slab_init(void)
 
 void wg_allowedips_slab_uninit(void)
 {
-	rcu_barrier();
 	kmem_cache_destroy(node_cache);
 }
 
Quoted 8 lines
Quoted 4 lines

Once kmem_cache_destroy has been fixed to be deferrable.

I assume the other patches are similar -- an rcu_barrier() can be removed. So some manual meddling of these might be in order.

Assuming that the deferrable kmem_cache_destroy() is the option chosen, agreed.

<snip>
void kmem_cache_destroy(struct kmem_cache *s)

{
	int err = -EBUSY;
	bool rcu_set;

	if (unlikely(!s) || !kasan_check_byte(s))
		return;

	cpus_read_lock();
	mutex_lock(&slab_mutex);

	rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU;

	s->refcount--;
	if (s->refcount)
		goto out_unlock;

	err = shutdown_cache(s);
	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
	     __func__, s->name, (void *)_RET_IP_);
...
	cpus_read_unlock();
	if (!err && !rcu_set)
		kmem_cache_release(s);
}

<snip>

so we have SLAB_TYPESAFE_BY_RCU flag that defers freeing slab-pages and a cache by a grace period. Similar flag can be added, like SLAB_DESTROY_ONCE_FULLY_FREED, in this case a worker rearm itself if there are still objects which should be freed.

Any thoughts here?

Quoted 7 lines
Quoted 4 lines

Wouldn't we also need some additional code to later check for all objects being freed to the slab, whether or not that code is initiated from kmem_cache_destroy()?

Same away as SLAB_TYPESAFE_BY_RCU is handled from the kmem_cache_destroy() function. It checks that flag and if it is true and extra worker is scheduled to perform a deferred(instead of right away) destroy after rcu_barrier() finishes.

Like this?

SLAB_DESTROY_ONCE_FULLY_FREED

Instead of adding a new kmem_cache_destroy_rcu()
or kmem_cache_destroy_wait() API member, instead add a SLAB_DESTROY_ONCE_FULLY_FREED flag that can be passed to the existing kmem_cache_destroy() function.  Use of this flag would suppress any warnings that would otherwise be issued if there was still slab memory yet to be freed, and it would also spawn workqueues (or timers or whatever) to do any needed cleanup work.

Thanx, Paul

#81 lore raw
Quoted 98 lines · Paul E. McKenney
Quoted 85 lines · Uladzislau Rezki
Quoted 84 lines · Paul E. McKenney
Quoted 82 lines · Uladzislau Rezki
Quoted 39 lines · Paul E. McKenney
Quoted 38 lines · Jason A. Donenfeld
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

Quoted 7 lines

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

Also, notably, this patch needs additionally:

drivers/net/wireguard/allowedips.c +0 -1
index e4e1638fce1b..c95f6937c3f1 100644
@@ -377,7 +377,6 @@ int __init wg_allowedips_slab_init(void)
 
 void wg_allowedips_slab_uninit(void)
 {
-	rcu_barrier();
 	kmem_cache_destroy(node_cache);
 }
 
Quoted 8 lines
Quoted 4 lines

Once kmem_cache_destroy has been fixed to be deferrable.

I assume the other patches are similar -- an rcu_barrier() can be removed. So some manual meddling of these might be in order.

Assuming that the deferrable kmem_cache_destroy() is the option chosen, agreed.

<snip>
void kmem_cache_destroy(struct kmem_cache *s)

{
	int err = -EBUSY;
	bool rcu_set;

	if (unlikely(!s) || !kasan_check_byte(s))
		return;

	cpus_read_lock();
	mutex_lock(&slab_mutex);

	rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU;

	s->refcount--;
	if (s->refcount)
		goto out_unlock;

	err = shutdown_cache(s);
	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
	     __func__, s->name, (void *)_RET_IP_);
...
	cpus_read_unlock();
	if (!err && !rcu_set)
		kmem_cache_release(s);
}

<snip>

so we have SLAB_TYPESAFE_BY_RCU flag that defers freeing slab-pages and a cache by a grace period. Similar flag can be added, like SLAB_DESTROY_ONCE_FULLY_FREED, in this case a worker rearm itself if there are still objects which should be freed.

Any thoughts here?

Quoted 7 lines
Quoted 4 lines

Wouldn't we also need some additional code to later check for all objects being freed to the slab, whether or not that code is initiated from kmem_cache_destroy()?

Same away as SLAB_TYPESAFE_BY_RCU is handled from the kmem_cache_destroy() function. It checks that flag and if it is true and extra worker is scheduled to perform a deferred(instead of right away) destroy after rcu_barrier() finishes.

Like this?

SLAB_DESTROY_ONCE_FULLY_FREED

Quoted 9 lines

Instead of adding a new kmem_cache_destroy_rcu()
or kmem_cache_destroy_wait() API member, instead add a SLAB_DESTROY_ONCE_FULLY_FREED flag that can be passed to the existing kmem_cache_destroy() function.  Use of this flag would suppress any warnings that would otherwise be issued if there was still slab memory yet to be freed, and it would also spawn workqueues (or timers or whatever) to do any needed cleanup work.

The flag is passed as all others during creating a cache:

  slab = kmem_cache_create(name, size, ..., SLAB_DESTROY_ONCE_FULLY_FREED | OTHER_FLAGS, NULL);

the rest description is correct to me.

--
Uladzislau Rezki

#82 lore raw
Quoted 108 lines · Uladzislau Rezki
Quoted 98 lines · Paul E. McKenney
Quoted 85 lines · Uladzislau Rezki
Quoted 84 lines · Paul E. McKenney
Quoted 82 lines · Uladzislau Rezki
Quoted 39 lines · Paul E. McKenney
Quoted 38 lines · Jason A. Donenfeld
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

Quoted 7 lines

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

Also, notably, this patch needs additionally:

drivers/net/wireguard/allowedips.c +0 -1
index e4e1638fce1b..c95f6937c3f1 100644
@@ -377,7 +377,6 @@ int __init wg_allowedips_slab_init(void)
 
 void wg_allowedips_slab_uninit(void)
 {
-	rcu_barrier();
 	kmem_cache_destroy(node_cache);
 }
 
Quoted 8 lines
Quoted 4 lines

Once kmem_cache_destroy has been fixed to be deferrable.

I assume the other patches are similar -- an rcu_barrier() can be removed. So some manual meddling of these might be in order.

Assuming that the deferrable kmem_cache_destroy() is the option chosen, agreed.

<snip>
void kmem_cache_destroy(struct kmem_cache *s)

{
	int err = -EBUSY;
	bool rcu_set;

	if (unlikely(!s) || !kasan_check_byte(s))
		return;

	cpus_read_lock();
	mutex_lock(&slab_mutex);

	rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU;

	s->refcount--;
	if (s->refcount)
		goto out_unlock;

	err = shutdown_cache(s);
	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
	     __func__, s->name, (void *)_RET_IP_);
...
	cpus_read_unlock();
	if (!err && !rcu_set)
		kmem_cache_release(s);
}

<snip>

so we have SLAB_TYPESAFE_BY_RCU flag that defers freeing slab-pages and a cache by a grace period. Similar flag can be added, like SLAB_DESTROY_ONCE_FULLY_FREED, in this case a worker rearm itself if there are still objects which should be freed.

Any thoughts here?

Quoted 7 lines
Quoted 4 lines

Wouldn't we also need some additional code to later check for all objects being freed to the slab, whether or not that code is initiated from kmem_cache_destroy()?

Same away as SLAB_TYPESAFE_BY_RCU is handled from the kmem_cache_destroy() function. It checks that flag and if it is true and extra worker is scheduled to perform a deferred(instead of right away) destroy after rcu_barrier() finishes.

Like this?

SLAB_DESTROY_ONCE_FULLY_FREED

Quoted 9 lines

Instead of adding a new kmem_cache_destroy_rcu()
or kmem_cache_destroy_wait() API member, instead add a SLAB_DESTROY_ONCE_FULLY_FREED flag that can be passed to the existing kmem_cache_destroy() function.  Use of this flag would suppress any warnings that would otherwise be issued if there was still slab memory yet to be freed, and it would also spawn workqueues (or timers or whatever) to do any needed cleanup work.

Quoted 5 lines

The flag is passed as all others during creating a cache:

  slab = kmem_cache_create(name, size, ..., SLAB_DESTROY_ONCE_FULLY_FREED | OTHER_FLAGS, NULL);

the rest description is correct to me.

Good catch, fixed, thank you!

Thanx, Paul

#83 lore raw
Quoted 109 lines · Paul E. McKenney
Quoted 108 lines · Uladzislau Rezki
Quoted 107 lines · Paul E. McKenney
Quoted 85 lines · Uladzislau Rezki
Quoted 84 lines · Paul E. McKenney
Quoted 82 lines · Uladzislau Rezki
Quoted 39 lines · Paul E. McKenney
Quoted 38 lines · Jason A. Donenfeld
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

Quoted 7 lines

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

Also, notably, this patch needs additionally:

drivers/net/wireguard/allowedips.c +0 -1
index e4e1638fce1b..c95f6937c3f1 100644
@@ -377,7 +377,6 @@ int __init wg_allowedips_slab_init(void)
 
 void wg_allowedips_slab_uninit(void)
 {
-	rcu_barrier();
 	kmem_cache_destroy(node_cache);
 }
 
Quoted 8 lines
Quoted 4 lines

Once kmem_cache_destroy has been fixed to be deferrable.

I assume the other patches are similar -- an rcu_barrier() can be removed. So some manual meddling of these might be in order.

Assuming that the deferrable kmem_cache_destroy() is the option chosen, agreed.

<snip>
void kmem_cache_destroy(struct kmem_cache *s)

{
	int err = -EBUSY;
	bool rcu_set;

	if (unlikely(!s) || !kasan_check_byte(s))
		return;

	cpus_read_lock();
	mutex_lock(&slab_mutex);

	rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU;

	s->refcount--;
	if (s->refcount)
		goto out_unlock;

	err = shutdown_cache(s);
	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
	     __func__, s->name, (void *)_RET_IP_);
...
	cpus_read_unlock();
	if (!err && !rcu_set)
		kmem_cache_release(s);
}

<snip>

so we have SLAB_TYPESAFE_BY_RCU flag that defers freeing slab-pages and a cache by a grace period. Similar flag can be added, like SLAB_DESTROY_ONCE_FULLY_FREED, in this case a worker rearm itself if there are still objects which should be freed.

Any thoughts here?

Quoted 7 lines
Quoted 4 lines

Wouldn't we also need some additional code to later check for all objects being freed to the slab, whether or not that code is initiated from kmem_cache_destroy()?

Same away as SLAB_TYPESAFE_BY_RCU is handled from the kmem_cache_destroy() function. It checks that flag and if it is true and extra worker is scheduled to perform a deferred(instead of right away) destroy after rcu_barrier() finishes.

Like this?

SLAB_DESTROY_ONCE_FULLY_FREED

Instead of adding a new kmem_cache_destroy_rcu()
or kmem_cache_destroy_wait() API member, instead add a SLAB_DESTROY_ONCE_FULLY_FREED flag that can be passed to the existing kmem_cache_destroy() function.  Use of this flag would suppress any warnings that would otherwise be issued if there was still slab memory yet to be freed, and it would also spawn workqueues (or timers or whatever) to do any needed cleanup work.

Quoted 8 lines
Quoted 5 lines

The flag is passed as all others during creating a cache:

  slab = kmem_cache_create(name, size, ..., SLAB_DESTROY_ONCE_FULLY_FREED | OTHER_FLAGS, NULL);

the rest description is correct to me.

Good catch, fixed, thank you!

And here we go with prototype(untested):

<snip>

include/linux/slab.h +2 -0
index 7247e217e21b..700b8a909f8a 100644
@@ -59,6 +59,7 @@ enum _slab_flag_bits {
 #ifdef CONFIG_SLAB_OBJ_EXT
 	_SLAB_NO_OBJ_EXT,
 #endif
+	_SLAB_DEFER_DESTROY,
 	_SLAB_FLAGS_LAST_BIT
 };
 
@@ -139,6 +140,7 @@ enum _slab_flag_bits {
  */
 /* Defer freeing slabs to RCU */
 #define SLAB_TYPESAFE_BY_RCU	__SLAB_FLAG_BIT(_SLAB_TYPESAFE_BY_RCU)
+#define SLAB_DEFER_DESTROY __SLAB_FLAG_BIT(_SLAB_DEFER_DESTROY)
 /* Trace allocations and frees */
 #define SLAB_TRACE		__SLAB_FLAG_BIT(_SLAB_TRACE)
 
mm/slab_common.c +37 -0
index 1560a1546bb1..99458a0197b5 100644
@@ -45,6 +45,11 @@ static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work);
 static DECLARE_WORK(slab_caches_to_rcu_destroy_work,
 		    slab_caches_to_rcu_destroy_workfn);
 
+static LIST_HEAD(slab_caches_defer_destroy);
+static void slab_caches_defer_destroy_workfn(struct work_struct *work);
+static DECLARE_DELAYED_WORK(slab_caches_defer_destroy_work,
+	slab_caches_defer_destroy_workfn);
+
 /*
  * Set of flags that will prevent slab merging
  */
@@ -448,6 +453,31 @@ static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work)
 	}
 }
 
+static void
+slab_caches_defer_destroy_workfn(struct work_struct *work)
+{
+	struct kmem_cache *s, *s2;
+
+	mutex_lock(&slab_mutex);
+	list_for_each_entry_safe(s, s2, &slab_caches_defer_destroy, list) {
+		if (__kmem_cache_empty(s)) {
+			/* free asan quarantined objects */
+			kasan_cache_shutdown(s);
+			(void) __kmem_cache_shutdown(s);
+
+			list_del(&s->list);
+
+			debugfs_slab_release(s);
+			kfence_shutdown_cache(s);
+			kmem_cache_release(s);
+		}
+	}
+	mutex_unlock(&slab_mutex);
+
+	if (!list_empty(&slab_caches_defer_destroy))
+		schedule_delayed_work(&slab_caches_defer_destroy_work, HZ);
+}
+
 static int shutdown_cache(struct kmem_cache *s)
 {
 	/* free asan quarantined objects */
@@ -493,6 +523,13 @@ void kmem_cache_destroy(struct kmem_cache *s)
 	if (s->refcount)
 		goto out_unlock;
 
+	/* Should a destroy process be deferred? */
+	if (s->flags & SLAB_DEFER_DESTROY) {
+		list_move_tail(&s->list, &slab_caches_defer_destroy);
+		schedule_delayed_work(&slab_caches_defer_destroy_work, HZ);
+		goto out_unlock;
+	}
+
 	err = shutdown_cache(s);
 	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
 	     __func__, s->name, (void *)_RET_IP_);

<snip>

Thanks!

--
Uladzislau Rezki

#84 lore raw
Quoted 112 lines · Uladzislau Rezki
Quoted 111 lines · Paul E. McKenney
Quoted 110 lines · Uladzislau Rezki
Quoted 98 lines · Paul E. McKenney
Quoted 85 lines · Uladzislau Rezki
Quoted 84 lines · Paul E. McKenney
Quoted 82 lines · Uladzislau Rezki
Quoted 39 lines · Paul E. McKenney
Quoted 38 lines · Jason A. Donenfeld
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

Quoted 7 lines

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

Also, notably, this patch needs additionally:

drivers/net/wireguard/allowedips.c +0 -1
index e4e1638fce1b..c95f6937c3f1 100644
@@ -377,7 +377,6 @@ int __init wg_allowedips_slab_init(void)
 
 void wg_allowedips_slab_uninit(void)
 {
-	rcu_barrier();
 	kmem_cache_destroy(node_cache);
 }
 
Quoted 8 lines
Quoted 4 lines

Once kmem_cache_destroy has been fixed to be deferrable.

I assume the other patches are similar -- an rcu_barrier() can be removed. So some manual meddling of these might be in order.

Assuming that the deferrable kmem_cache_destroy() is the option chosen, agreed.

<snip>
void kmem_cache_destroy(struct kmem_cache *s)

{
	int err = -EBUSY;
	bool rcu_set;

	if (unlikely(!s) || !kasan_check_byte(s))
		return;

	cpus_read_lock();
	mutex_lock(&slab_mutex);

	rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU;

	s->refcount--;
	if (s->refcount)
		goto out_unlock;

	err = shutdown_cache(s);
	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
	     __func__, s->name, (void *)_RET_IP_);
...
	cpus_read_unlock();
	if (!err && !rcu_set)
		kmem_cache_release(s);
}

<snip>

so we have SLAB_TYPESAFE_BY_RCU flag that defers freeing slab-pages and a cache by a grace period. Similar flag can be added, like SLAB_DESTROY_ONCE_FULLY_FREED, in this case a worker rearm itself if there are still objects which should be freed.

Any thoughts here?

Quoted 7 lines
Quoted 4 lines

Wouldn't we also need some additional code to later check for all objects being freed to the slab, whether or not that code is initiated from kmem_cache_destroy()?

Same away as SLAB_TYPESAFE_BY_RCU is handled from the kmem_cache_destroy() function. It checks that flag and if it is true and extra worker is scheduled to perform a deferred(instead of right away) destroy after rcu_barrier() finishes.

Like this?

SLAB_DESTROY_ONCE_FULLY_FREED

Quoted 9 lines

Instead of adding a new kmem_cache_destroy_rcu()
or kmem_cache_destroy_wait() API member, instead add a SLAB_DESTROY_ONCE_FULLY_FREED flag that can be passed to the existing kmem_cache_destroy() function.  Use of this flag would suppress any warnings that would otherwise be issued if there was still slab memory yet to be freed, and it would also spawn workqueues (or timers or whatever) to do any needed cleanup work.

The flag is passed as all others during creating a cache:

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
  slab = kmem_cache_create(name, size, ..., SLAB_DESTROY_ONCE_FULLY_FREED | OTHER_FLAGS, NULL);

the rest description is correct to me.

Good catch, fixed, thank you!

And here we go with prototype(untested):

Thank you for putting this together! It looks way simpler than I would have guessed, and quite a bit simpler than I would expect it would be to extend rcu_barrier() to cover kfree_rcu().

Quoted 49 lines

<snip>

include/linux/slab.h +2 -0
index 7247e217e21b..700b8a909f8a 100644
@@ -59,6 +59,7 @@ enum _slab_flag_bits {
 #ifdef CONFIG_SLAB_OBJ_EXT
 	_SLAB_NO_OBJ_EXT,
 #endif
+	_SLAB_DEFER_DESTROY,
 	_SLAB_FLAGS_LAST_BIT
 };
 
@@ -139,6 +140,7 @@ enum _slab_flag_bits {
  */
 /* Defer freeing slabs to RCU */
 #define SLAB_TYPESAFE_BY_RCU	__SLAB_FLAG_BIT(_SLAB_TYPESAFE_BY_RCU)
+#define SLAB_DEFER_DESTROY __SLAB_FLAG_BIT(_SLAB_DEFER_DESTROY)
 /* Trace allocations and frees */
 #define SLAB_TRACE		__SLAB_FLAG_BIT(_SLAB_TRACE)
 
mm/slab_common.c +13 -0
index 1560a1546bb1..99458a0197b5 100644
@@ -45,6 +45,11 @@ static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work);
 static DECLARE_WORK(slab_caches_to_rcu_destroy_work,
 		    slab_caches_to_rcu_destroy_workfn);
 
+static LIST_HEAD(slab_caches_defer_destroy);
+static void slab_caches_defer_destroy_workfn(struct work_struct *work);
+static DECLARE_DELAYED_WORK(slab_caches_defer_destroy_work,
+	slab_caches_defer_destroy_workfn);
+
 /*
  * Set of flags that will prevent slab merging
  */
@@ -448,6 +453,31 @@ static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work)
 	}
 }
 
+static void
+slab_caches_defer_destroy_workfn(struct work_struct *work)
+{
+	struct kmem_cache *s, *s2;
+
+	mutex_lock(&slab_mutex);
+	list_for_each_entry_safe(s, s2, &slab_caches_defer_destroy, list) {
+		if (__kmem_cache_empty(s)) {

mm/slab_common.c static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work) · line 473

Quoted 10 lines
mm/slab_common.c +10 -0
+			/* free asan quarantined objects */
+			kasan_cache_shutdown(s);
+			(void) __kmem_cache_shutdown(s);
+
+			list_del(&s->list);
+
+			debugfs_slab_release(s);
+			kfence_shutdown_cache(s);
+			kmem_cache_release(s);
+		}

My guess is that there would want to be a splat if the slab stuck around for too long, but maybe that should instead be handled elsewhere or in some other way? I must defer to you guys on that one.

Thanx, Paul

Quoted 25 lines
mm/slab_common.c +14 -0
+	}
+	mutex_unlock(&slab_mutex);
+
+	if (!list_empty(&slab_caches_defer_destroy))
+		schedule_delayed_work(&slab_caches_defer_destroy_work, HZ);
+}
+
 static int shutdown_cache(struct kmem_cache *s)
 {
 	/* free asan quarantined objects */
@@ -493,6 +523,13 @@ void kmem_cache_destroy(struct kmem_cache *s)
 	if (s->refcount)
 		goto out_unlock;
 
+	/* Should a destroy process be deferred? */
+	if (s->flags & SLAB_DEFER_DESTROY) {
+		list_move_tail(&s->list, &slab_caches_defer_destroy);
+		schedule_delayed_work(&slab_caches_defer_destroy_work, HZ);
+		goto out_unlock;
+	}
+
 	err = shutdown_cache(s);
 	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
 	     __func__, s->name, (void *)_RET_IP_);
<snip>
#85 lore raw
Quoted 117 lines · Paul E. McKenney
Quoted 116 lines · Uladzislau Rezki
Quoted 115 lines · Paul E. McKenney
Quoted 113 lines · Uladzislau Rezki
Quoted 98 lines · Paul E. McKenney
Quoted 85 lines · Uladzislau Rezki
Quoted 84 lines · Paul E. McKenney
Quoted 82 lines · Uladzislau Rezki
Quoted 39 lines · Paul E. McKenney
Quoted 38 lines · Jason A. Donenfeld
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Sun, 9 Jun 2024 10:27:12 +0200 Julia Lawall wrote:

Since SLOB was removed, it is not necessary to use call_rcu
when the callback only performs kmem_cache_free. Use
kfree_rcu() directly.

Quoted 8 lines
Quoted 3 lines

The changes were done using the following Coccinelle semantic patch.
This semantic patch is designed to ignore cases where the callback
function is used in another way.

How does the discussion on:
[PATCH] Revert "batman-adv: prefer kfree_rcu() over call_rcu() with free-only callbacks"
https://lore.kernel.org/all/[email protected]/
reflect on this series? IIUC we should hold off..

Quoted 7 lines

We do need to hold off for the ones in kernel modules (such as 07/14) where the kmem_cache is destroyed during module unload.

OK, I might as well go through them...

[PATCH 01/14] wireguard: allowedips: replace call_rcu by kfree_rcu for simple kmem_cache_free callback
Needs to wait, see wg_allowedips_slab_uninit().

Also, notably, this patch needs additionally:

drivers/net/wireguard/allowedips.c +0 -1
index e4e1638fce1b..c95f6937c3f1 100644
@@ -377,7 +377,6 @@ int __init wg_allowedips_slab_init(void)
 
 void wg_allowedips_slab_uninit(void)
 {
-	rcu_barrier();
 	kmem_cache_destroy(node_cache);
 }
 
Quoted 8 lines
Quoted 4 lines

Once kmem_cache_destroy has been fixed to be deferrable.

I assume the other patches are similar -- an rcu_barrier() can be removed. So some manual meddling of these might be in order.

Assuming that the deferrable kmem_cache_destroy() is the option chosen, agreed.

<snip>
void kmem_cache_destroy(struct kmem_cache *s)

{
	int err = -EBUSY;
	bool rcu_set;

	if (unlikely(!s) || !kasan_check_byte(s))
		return;

	cpus_read_lock();
	mutex_lock(&slab_mutex);

	rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU;

	s->refcount--;
	if (s->refcount)
		goto out_unlock;

	err = shutdown_cache(s);
	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
	     __func__, s->name, (void *)_RET_IP_);
...
	cpus_read_unlock();
	if (!err && !rcu_set)
		kmem_cache_release(s);
}

<snip>

so we have SLAB_TYPESAFE_BY_RCU flag that defers freeing slab-pages and a cache by a grace period. Similar flag can be added, like SLAB_DESTROY_ONCE_FULLY_FREED, in this case a worker rearm itself if there are still objects which should be freed.

Any thoughts here?

Quoted 7 lines
Quoted 4 lines

Wouldn't we also need some additional code to later check for all objects being freed to the slab, whether or not that code is initiated from kmem_cache_destroy()?

Same away as SLAB_TYPESAFE_BY_RCU is handled from the kmem_cache_destroy() function. It checks that flag and if it is true and extra worker is scheduled to perform a deferred(instead of right away) destroy after rcu_barrier() finishes.

Like this?

SLAB_DESTROY_ONCE_FULLY_FREED

Quoted 9 lines

Instead of adding a new kmem_cache_destroy_rcu()
or kmem_cache_destroy_wait() API member, instead add a SLAB_DESTROY_ONCE_FULLY_FREED flag that can be passed to the existing kmem_cache_destroy() function.  Use of this flag would suppress any warnings that would otherwise be issued if there was still slab memory yet to be freed, and it would also spawn workqueues (or timers or whatever) to do any needed cleanup work.

The flag is passed as all others during creating a cache:

  slab = kmem_cache_create(name, size, ..., SLAB_DESTROY_ONCE_FULLY_FREED | OTHER_FLAGS, NULL);

the rest description is correct to me.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

Good catch, fixed, thank you!

And here we go with prototype(untested):

Thank you for putting this together! It looks way simpler than I would have guessed, and quite a bit simpler than I would expect it would be to extend rcu_barrier() to cover kfree_rcu().

Yep, it should be pretty pretty straightforward. The slab mechanism does not have a functionality when it comes to defer of destroying, i.e. it is not allowed to destroy non-fully-freed-slab:

<snip>
void kmem_cache_destroy(struct kmem_cache *s)
{
...

	err = shutdown_cache(s);
	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
	     __func__, s->name, (void *)_RET_IP_);

...
<snip>

So, this patch extends it.

Quoted 15 lines
Quoted 15 lines
mm/slab_common.c +14 -0
+static void
+slab_caches_defer_destroy_workfn(struct work_struct *work)
+{
+	struct kmem_cache *s, *s2;
+
+	mutex_lock(&slab_mutex);
+	list_for_each_entry_safe(s, s2, &slab_caches_defer_destroy, list) {
+		if (__kmem_cache_empty(s)) {
+			/* free asan quarantined objects */
+			kasan_cache_shutdown(s);
+			(void) __kmem_cache_shutdown(s);
+
+			list_del(&s->list);
+
Quoted 9 lines

mm/slab_common.c static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work) · line 473

Quoted 4 lines
mm/slab_common.c +4 -0
+			debugfs_slab_release(s);
+			kfence_shutdown_cache(s);
+			kmem_cache_release(s);
+		}

My guess is that there would want to be a splat if the slab stuck around for too long, but maybe that should instead be handled elsewhere or in some other way? I must defer to you guys on that one.

Probably yes.

--
Uladzislau Rezki

#86 lore raw
In reply to Uladzislau Rezki

mm/slab_common.c void kmem_cache_destroy(struct kmem_cache *s) · line 531

Quoted 6 lines · Uladzislau Rezki
mm/slab_common.c +6 -0
+	/* Should a destroy process be deferred? */
+	if (s->flags & SLAB_DEFER_DESTROY) {
+		list_move_tail(&s->list, &slab_caches_defer_destroy);
+		schedule_delayed_work(&slab_caches_defer_destroy_work, HZ);
+		goto out_unlock;
+	}

Wouldn't it be smoother to have the actual kmem_cache_free() function check to see if it's been marked for destruction and the refcount is zero, rather than polling every one second? I mentioned this approach
in: https://lore.kernel.org/all/[email protected]/ -

I wonder if the right fix to this would be adding a should_destroy boolean to kmem_cache, which kmem_cache_destroy() sets to true. And then right after it checks if (number_of_allocations == 0) actually_destroy(), and likewise on each kmem_cache_free(), it could check if (should_destroy && number_of_allocations == 0) actually_destroy().

Jason

#87 lore raw
Quoted 13 lines · Jason A. Donenfeld

mm/slab_common.c void kmem_cache_destroy(struct kmem_cache *s) · line 531

Quoted 6 lines · Uladzislau Rezki
mm/slab_common.c +6 -0
+	/* Should a destroy process be deferred? */
+	if (s->flags & SLAB_DEFER_DESTROY) {
+		list_move_tail(&s->list, &slab_caches_defer_destroy);
+		schedule_delayed_work(&slab_caches_defer_destroy_work, HZ);
+		goto out_unlock;
+	}

Wouldn't it be smoother to have the actual kmem_cache_free() function check to see if it's been marked for destruction and the refcount is zero, rather than polling every one second? I mentioned this approach
in: https://lore.kernel.org/all/[email protected]/ -

Quoted 7 lines

I wonder if the right fix to this would be adding a should_destroy boolean to kmem_cache, which kmem_cache_destroy() sets to true. And then right after it checks if (number_of_allocations == 0) actually_destroy(), and likewise on each kmem_cache_free(), it could check if (should_destroy && number_of_allocations == 0) actually_destroy().

I do not find pooling as bad way we can go with. But your proposal sounds reasonable to me also. We can combine both "prototypes" to one and offer.

Can you post a prototype here?

Thanks!

--
Uladzislau Rezki

#88 lore raw
Quoted 21 lines · Uladzislau Rezki
Quoted 13 lines · Jason A. Donenfeld

Review

Quoted 6 lines · Uladzislau Rezki
(quoted diff) +6 -0
+	/* Should a destroy process be deferred? */
+	if (s->flags & SLAB_DEFER_DESTROY) {
+		list_move_tail(&s->list, &slab_caches_defer_destroy);
+		schedule_delayed_work(&slab_caches_defer_destroy_work, HZ);
+		goto out_unlock;
+	}

Wouldn't it be smoother to have the actual kmem_cache_free() function check to see if it's been marked for destruction and the refcount is zero, rather than polling every one second? I mentioned this approach
in: https://lore.kernel.org/all/[email protected]/ -

Quoted 7 lines

I wonder if the right fix to this would be adding a should_destroy boolean to kmem_cache, which kmem_cache_destroy() sets to true. And then right after it checks if (number_of_allocations == 0) actually_destroy(), and likewise on each kmem_cache_free(), it could check if (should_destroy && number_of_allocations == 0) actually_destroy().

Quoted 5 lines

I do not find pooling as bad way we can go with. But your proposal sounds reasonable to me also. We can combine both "prototypes" to one and offer.

Can you post a prototype here?

This is untested, but the simplest, shortest possible version would be:

mm/slab.h +1 -0
index 5f8f47c5bee0..907c0ea56c01 100644
@@ -275,6 +275,7 @@ struct kmem_cache {
 	unsigned int inuse;		/* Offset to metadata */
 	unsigned int align;		/* Alignment */
 	unsigned int red_left_pad;	/* Left redzone padding size */
+	bool is_destroyed;		/* Destruction happens when no objects */
 	const char *name;		/* Name (only for display!) */
 	struct list_head list;		/* List of slab caches */
 #ifdef CONFIG_SYSFS
mm/slab_common.c +2 -2
index 1560a1546bb1..f700bed066d9 100644
@@ -494,8 +494,8 @@ void kmem_cache_destroy(struct kmem_cache *s)
 		goto out_unlock;
 
 	err = shutdown_cache(s);
-	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
-	     __func__, s->name, (void *)_RET_IP_);
+	if (err)
+		s->is_destroyed = true;
 out_unlock:
 	mutex_unlock(&slab_mutex);
 	cpus_read_unlock();
mm/slub.c +2 -3
index 1373ac365a46..7db8fe90a323 100644
@@ -4510,6 +4510,8 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
 		return;
 	trace_kmem_cache_free(_RET_IP_, x, s);
 	slab_free(s, virt_to_slab(x), x, _RET_IP_);
+	if (s->is_destroyed)
+		kmem_cache_destroy(s);
 }
 EXPORT_SYMBOL(kmem_cache_free);
 
@@ -5342,9 +5344,6 @@ static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
 		if (!slab->inuse) {
 			remove_partial(n, slab);
 			list_add(&slab->slab_list, &discard);
-		} else {
-			list_slab_objects(s, slab,
-			  "Objects remaining in %s on __kmem_cache_shutdown()");
 		}
 	}
 	spin_unlock_irq(&n->list_lock);
#89 lore raw
Quoted 47 lines · Jason A. Donenfeld
Quoted 21 lines · Uladzislau Rezki
Quoted 13 lines · Jason A. Donenfeld

Review

Quoted 6 lines · Uladzislau Rezki
(quoted diff) +6 -0
+	/* Should a destroy process be deferred? */
+	if (s->flags & SLAB_DEFER_DESTROY) {
+		list_move_tail(&s->list, &slab_caches_defer_destroy);
+		schedule_delayed_work(&slab_caches_defer_destroy_work, HZ);
+		goto out_unlock;
+	}

Wouldn't it be smoother to have the actual kmem_cache_free() function check to see if it's been marked for destruction and the refcount is zero, rather than polling every one second? I mentioned this approach
in: https://lore.kernel.org/all/[email protected]/ -

Quoted 7 lines

I wonder if the right fix to this would be adding a should_destroy boolean to kmem_cache, which kmem_cache_destroy() sets to true. And then right after it checks if (number_of_allocations == 0) actually_destroy(), and likewise on each kmem_cache_free(), it could check if (should_destroy && number_of_allocations == 0) actually_destroy().

Quoted 5 lines

I do not find pooling as bad way we can go with. But your proposal sounds reasonable to me also. We can combine both "prototypes" to one and offer.

Can you post a prototype here?

This is untested, but the simplest, shortest possible version would be:

mm/slab.h +1 -0
index 5f8f47c5bee0..907c0ea56c01 100644
@@ -275,6 +275,7 @@ struct kmem_cache {
 	unsigned int inuse;		/* Offset to metadata */
 	unsigned int align;		/* Alignment */
 	unsigned int red_left_pad;	/* Left redzone padding size */
+	bool is_destroyed;		/* Destruction happens when no objects */
 	const char *name;		/* Name (only for display!) */
 	struct list_head list;		/* List of slab caches */
 #ifdef CONFIG_SYSFS
mm/slab_common.c +0 -0
index 1560a1546bb1..f700bed066d9 100644

mm/slab_common.c void kmem_cache_destroy(struct kmem_cache *s) · line 499

Quoted 8 lines
mm/slab_common.c +2 -2
@@ -494,8 +494,8 @@ void kmem_cache_destroy(struct kmem_cache *s)
 		goto out_unlock;
 
 	err = shutdown_cache(s);
-	WARN(err, "%s %s: Slab cache still has objects when called from %pS",
-	     __func__, s->name, (void *)_RET_IP_);
+	if (err)
+		s->is_destroyed = true;
 

Here if an "err" is less then "0" means there are still objects whereas "is_destroyed" is set to "true" which is not correlated with a comment:

"Destruction happens when no objects"

Quoted 18 lines
mm/slab_common.c +0 -0
 out_unlock:
 	mutex_unlock(&slab_mutex);
mm/slab_common.c +0 -0
 	cpus_read_unlock();
mm/slub.c +2 -0
index 1373ac365a46..7db8fe90a323 100644
@@ -4510,6 +4510,8 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
 		return;
 	trace_kmem_cache_free(_RET_IP_, x, s);
 	slab_free(s, virt_to_slab(x), x, _RET_IP_);
+	if (s->is_destroyed)
+		kmem_cache_destroy(s);
 }
 EXPORT_SYMBOL(kmem_cache_free);
 

mm/slub.c static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n) · line 5349

Quoted 9 lines
mm/slub.c +0 -3
@@ -5342,9 +5344,6 @@ static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
 		if (!slab->inuse) {
 			remove_partial(n, slab);
 			list_add(&slab->slab_list, &discard);
-		} else {
-			list_slab_objects(s, slab,
-			  "Objects remaining in %s on __kmem_cache_shutdown()");
 		}
 	}
 	spin_unlock_irq(&n->list_lock);

Anyway it looks like it was not welcome to do it in the kmem_cache_free() function due to performance reason.

--
Uladzislau Rezki

#90 lore raw
Quoted 5 lines · Uladzislau Rezki

Here if an "err" is less then "0" means there are still objects whereas "is_destroyed" is set to "true" which is not correlated with a comment:

"Destruction happens when no objects"

The comment is just poorly written. But the logic of the code is right.

Quoted 17 lines
Quoted 17 lines

out_unlock:

      mutex_unlock(&slab_mutex);
      cpus_read_unlock();
mm/slub.c +2 -0
index 1373ac365a46..7db8fe90a323 100644
@@ -4510,6 +4510,8 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
              return;
      trace_kmem_cache_free(_RET_IP_, x, s);
      slab_free(s, virt_to_slab(x), x, _RET_IP_);
+     if (s->is_destroyed)
+             kmem_cache_destroy(s);
 }
 EXPORT_SYMBOL(kmem_cache_free);
 
Quoted 13 lines

mm/slub.c static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n) · line 5349

Quoted 11 lines
mm/slub.c +0 -3
@@ -5342,9 +5344,6 @@ static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
              if (!slab->inuse) {
                      remove_partial(n, slab);
                      list_add(&slab->slab_list, &discard);
-             } else {
-                     list_slab_objects(s, slab,
-                       "Objects remaining in %s on __kmem_cache_shutdown()");
              }
      }
      spin_unlock_irq(&n->list_lock);

Anyway it looks like it was not welcome to do it in the kmem_cache_free() function due to performance reason.

"was not welcome" - Vlastimil mentioned *potential* performance concerns before I posted this. I suspect he might have a different view now, maybe?

Vlastimil, this is just checking a boolean (which could be unlikely()'d), which should have pretty minimal overhead. Is that alright with you?

Jason

#91 lore raw
Quoted 40 lines · Jason A. Donenfeld
Quoted 5 lines · Uladzislau Rezki

Here if an "err" is less then "0" means there are still objects whereas "is_destroyed" is set to "true" which is not correlated with a comment:

"Destruction happens when no objects"

The comment is just poorly written. But the logic of the code is right.

Quoted 17 lines
Quoted 17 lines

out_unlock:

      mutex_unlock(&slab_mutex);
      cpus_read_unlock();
mm/slub.c +2 -0
index 1373ac365a46..7db8fe90a323 100644
@@ -4510,6 +4510,8 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
              return;
      trace_kmem_cache_free(_RET_IP_, x, s);
      slab_free(s, virt_to_slab(x), x, _RET_IP_);
+     if (s->is_destroyed)
+             kmem_cache_destroy(s);
 }
 EXPORT_SYMBOL(kmem_cache_free);
 
Quoted 13 lines

mm/slub.c static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n) · line 5349

Quoted 11 lines
mm/slub.c +0 -3
@@ -5342,9 +5344,6 @@ static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
              if (!slab->inuse) {
                      remove_partial(n, slab);
                      list_add(&slab->slab_list, &discard);
-             } else {
-                     list_slab_objects(s, slab,
-                       "Objects remaining in %s on __kmem_cache_shutdown()");
              }
      }
      spin_unlock_irq(&n->list_lock);

Anyway it looks like it was not welcome to do it in the kmem_cache_free() function due to performance reason.

Quoted 7 lines

"was not welcome" - Vlastimil mentioned *potential* performance concerns before I posted this. I suspect he might have a different view now, maybe?

Vlastimil, this is just checking a boolean (which could be unlikely()'d), which should have pretty minimal overhead. Is that alright with you?

Well I doubt we can just set and check it without any barriers? The completion of the last pending kfree_rcu() might race with kmem_cache_destroy() in a way that will leave the cache there forever, no? And once we add barriers it becomes a perf issue?

Quoted 1 line

Jason

#92 lore raw
Quoted 45 lines · Vlastimil Babka
Quoted 44 lines · Jason A. Donenfeld
Quoted 5 lines · Uladzislau Rezki

Here if an "err" is less then "0" means there are still objects whereas "is_destroyed" is set to "true" which is not correlated with a comment:

"Destruction happens when no objects"

The comment is just poorly written. But the logic of the code is right.

Quoted 17 lines
Quoted 17 lines

out_unlock:

      mutex_unlock(&slab_mutex);
      cpus_read_unlock();
mm/slub.c +2 -0
index 1373ac365a46..7db8fe90a323 100644
@@ -4510,6 +4510,8 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
              return;
      trace_kmem_cache_free(_RET_IP_, x, s);
      slab_free(s, virt_to_slab(x), x, _RET_IP_);
+     if (s->is_destroyed)
+             kmem_cache_destroy(s);
 }
 EXPORT_SYMBOL(kmem_cache_free);
 
Quoted 13 lines

mm/slub.c static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n) · line 5349

Quoted 11 lines
mm/slub.c +0 -3
@@ -5342,9 +5344,6 @@ static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
              if (!slab->inuse) {
                      remove_partial(n, slab);
                      list_add(&slab->slab_list, &discard);
-             } else {
-                     list_slab_objects(s, slab,
-                       "Objects remaining in %s on __kmem_cache_shutdown()");
              }
      }
      spin_unlock_irq(&n->list_lock);

Anyway it looks like it was not welcome to do it in the kmem_cache_free() function due to performance reason.

"was not welcome" - Vlastimil mentioned *potential* performance concerns before I posted this. I suspect he might have a different view now, maybe?

Quoted 8 lines
Quoted 3 lines

Vlastimil, this is just checking a boolean (which could be unlikely()'d), which should have pretty minimal overhead. Is that alright with you?

Well I doubt we can just set and check it without any barriers? The completion of the last pending kfree_rcu() might race with kmem_cache_destroy() in a way that will leave the cache there forever, no? And once we add barriers it becomes a perf issue?

Hm, yea you might be right about barriers being required. But actually, might this point toward a larger problem with no matter what approach, polling or event, is chosen? If the current rule is that kmem_cache_free() must never race with kmem_cache_destroy(), because users have always made diligent use of call_rcu()/rcu_barrier() and such, but now we're going to let those race with each other - either by my thing above or by polling - so we're potentially going to get in trouble and need some barriers anyway.

I think?

Jason

#93 lore raw
Quoted 9 lines · Jason A. Donenfeld
Quoted 8 lines
Quoted 3 lines

Vlastimil, this is just checking a boolean (which could be unlikely()'d), which should have pretty minimal overhead. Is that alright with you?

Well I doubt we can just set and check it without any barriers? The completion of the last pending kfree_rcu() might race with kmem_cache_destroy() in a way that will leave the cache there forever, no? And once we add barriers it becomes a perf issue?

Quoted 4 lines

Hm, yea you might be right about barriers being required. But actually, might this point toward a larger problem with no matter what approach, polling or event, is chosen? If the current rule is that kmem_cache_free() must never race with kmem_cache_destroy(), because

Yes calling alloc/free operations that race with destroy is a bug and we can't prevent that.

Quoted 1 line

users have always made diligent use of call_rcu()/rcu_barrier() and

But the issue we are solving here is a bit different - the users are not buggy, they do kfree_rcu() and then kmem_cache_destroy() and no more operations on the cache afterwards. We need to ensure that the handling of kfree_rcu() (which ultimately is basically kmem_cache_free() but internally to rcu/slub) doesn't race with kmem_cache_destroy().

Quoted 3 lines

such, but now we're going to let those race with each other - either by my thing above or by polling - so we're potentially going to get in trouble and need some barriers anyway.

The barrier in the async part of kmem_cache_destroy() should be enough to make sure all kfree_rcu() have finished before we proceed with the potentially racy parts of destroying, and we should be able to avoid changes in kmem_cache_free().

Quoted 3 lines

I think?

Jason

#94 lore raw
In reply to Jason A. Donenfeld
Quoted 9 lines · Jason A. Donenfeld
Quoted 5 lines · Uladzislau Rezki

Here if an "err" is less then "0" means there are still objects whereas "is_destroyed" is set to "true" which is not correlated with a comment:

"Destruction happens when no objects"

The comment is just poorly written. But the logic of the code is right.

OK.

Quoted 15 lines
Quoted 14 lines
Quoted 9 lines

out_unlock:

      mutex_unlock(&slab_mutex);
      cpus_read_unlock();
mm/slub.c +0 -0
index 1373ac365a46..7db8fe90a323 100644

mm/slub.c void kmem_cache_free(struct kmem_cache *s, void *x) · line 4514

Quoted 5 lines
mm/slub.c +2 -0
@@ -4510,6 +4510,8 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
              return;
      trace_kmem_cache_free(_RET_IP_, x, s);
      slab_free(s, virt_to_slab(x), x, _RET_IP_);
+     if (s->is_destroyed)
+             kmem_cache_destroy(s);

Here i am not follow you. How do you see that a cache has been fully freed? Or is it just super draft code?

Thanks!

--
Uladzislau Rezki

#95 lore raw
Quoted 27 lines · Uladzislau Rezki
Quoted 9 lines · Jason A. Donenfeld
Quoted 5 lines · Uladzislau Rezki

Here if an "err" is less then "0" means there are still objects whereas "is_destroyed" is set to "true" which is not correlated with a comment:

"Destruction happens when no objects"

The comment is just poorly written. But the logic of the code is right.

OK.

Quoted 15 lines
Quoted 14 lines
Quoted 9 lines

out_unlock:

      mutex_unlock(&slab_mutex);
      cpus_read_unlock();
mm/slub.c +0 -0
index 1373ac365a46..7db8fe90a323 100644

mm/slub.c void kmem_cache_free(struct kmem_cache *s, void *x) · line 4514

Quoted 5 lines
mm/slub.c +2 -0
@@ -4510,6 +4510,8 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
              return;
      trace_kmem_cache_free(_RET_IP_, x, s);
      slab_free(s, virt_to_slab(x), x, _RET_IP_);
+     if (s->is_destroyed)
+             kmem_cache_destroy(s);
Quoted 2 lines

Here i am not follow you. How do you see that a cache has been fully freed? Or is it just super draft code?

kmem_cache_destroy() does this in shutdown_cache().

#96 lore raw
Quoted 28 lines · Jason A. Donenfeld
Quoted 27 lines · Uladzislau Rezki
Quoted 9 lines · Jason A. Donenfeld
Quoted 5 lines · Uladzislau Rezki

Here if an "err" is less then "0" means there are still objects whereas "is_destroyed" is set to "true" which is not correlated with a comment:

"Destruction happens when no objects"

The comment is just poorly written. But the logic of the code is right.

OK.

Quoted 15 lines
Quoted 14 lines
Quoted 14 lines

out_unlock:

      mutex_unlock(&slab_mutex);
      cpus_read_unlock();
mm/slub.c +2 -0
index 1373ac365a46..7db8fe90a323 100644
@@ -4510,6 +4510,8 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
              return;
      trace_kmem_cache_free(_RET_IP_, x, s);
      slab_free(s, virt_to_slab(x), x, _RET_IP_);
+     if (s->is_destroyed)
+             kmem_cache_destroy(s);
Quoted 5 lines
Quoted 2 lines

Here i am not follow you. How do you see that a cache has been fully freed? Or is it just super draft code?

kmem_cache_destroy() does this in shutdown_cache().

Right. In this scenario you invoke kmem_cache_destroy() over and over until the last object gets freed. This potentially slowing the kmem_cache_free() which is not OK, at least to me.

--
Uladzislau Rezki

#97 lore raw
In reply to Jason A. Donenfeld
Quoted 13 lines · Jason A. Donenfeld

mm/slab_common.c void kmem_cache_destroy(struct kmem_cache *s) · line 531

Quoted 6 lines · Uladzislau Rezki
mm/slab_common.c +6 -0
+	/* Should a destroy process be deferred? */
+	if (s->flags & SLAB_DEFER_DESTROY) {
+		list_move_tail(&s->list, &slab_caches_defer_destroy);
+		schedule_delayed_work(&slab_caches_defer_destroy_work, HZ);
+		goto out_unlock;
+	}

Wouldn't it be smoother to have the actual kmem_cache_free() function check to see if it's been marked for destruction and the refcount is zero, rather than polling every one second? I mentioned this approach
in: https://lore.kernel.org/all/[email protected]/ -

Quoted 6 lines

I wonder if the right fix to this would be adding a should_destroy boolean to kmem_cache, which kmem_cache_destroy() sets to true. And then right after it checks if (number_of_allocations == 0) actually_destroy(), and likewise on each kmem_cache_free(), it could check if (should_destroy && number_of_allocations == 0) actually_destroy().

I would prefer not to affect the performance of kmem_cache_free() by doing such checks, if possible. Ideally we'd have a way to wait/poll for the kfree_rcu() "grace period" expiring even with the batching that's implemented there. Even if it's pesimistically long to avoid affecting kfree_rcu() performance. The goal here is just to print the warnings if there was a leak and the precise timing of them shouldn't matter. The owning module could be already unloaded at that point? I guess only a kunit test could want to be synchronous and then it could just ask for kmem_cache_free() to wait synchronously.

Quoted 1 line

Jason

#98 lore raw
In reply to Julia Lawall
Quoted 3 lines · Julia Lawall

Since SLOB was removed, it is not necessary to use call_rcu when the callback only performs kmem_cache_free. Use kfree_rcu() directly.

FYI, as of 6.12-rc1 - commit 6c6c47b063b5 ("mm, slab: call kvfree_rcu_barrier() from kmem_cache_destroy()")
it should be possible to restart this effort for the remaining cases which had to be postponed until kmem_cache_destroy() / module unload is sorted out.

Thanks,
Vlastimil

Quoted 104 lines

The changes were done using the following Coccinelle semantic patch. This semantic patch is designed to ignore cases where the callback function is used in another way.

// <smpl>
@r@
expression e;
local idexpression e2;
identifier cb,f;
position p;
@@

(
call_rcu(...,e2)
|
call_rcu(&e->f,cb@p)
)

@r1@
type T;
identifier x,r.cb;
@@

cb(...) {
(
kmem_cache_free(...);
|

   T x = ...;
   kmem_cache_free(...,x);
|
   T x;
   x = ...;
   kmem_cache_free(...,x);

)
}

@s depends on r1@
position p != r.p;
identifier r.cb;
@@

cb@p

@script:ocaml@
cb << r.cb;
p << s.p;
@@

Printf.eprintf "Other use of %s at %s:%d\n"
cb (List.hd p).file (List.hd p).line

@depends on r1 && !s@
expression e;
identifier r.cb,f;
position r.p;
@@

- call_rcu(&e->f,cb@p)
+ kfree_rcu(e,f)

@r1a depends on !s@
type T;
identifier x,r.cb;
@@

- cb(...) {
(
- kmem_cache_free(...);
|

(quoted diff) +0 -2
-  T x = ...;
-  kmem_cache_free(...,x);

|

(quoted diff) +0 -3
-  T x;
-  x = ...;
-  kmem_cache_free(...,x);

)
- }
// </smpl>

---

 arch/powerpc/kvm/book3s_mmu_hpte.c  |    8 +-------
 block/blk-ioc.c                     |    9 +--------
 drivers/net/wireguard/allowedips.c  |    9 ++-------
 fs/ecryptfs/dentry.c                |    8 +-------
 fs/nfsd/nfs4state.c                 |    9 +--------
 fs/tracefs/inode.c                  |   10 +---------
 kernel/time/posix-timers.c          |    9 +--------
 kernel/workqueue.c                  |    8 +-------
 net/bridge/br_fdb.c                 |    9 +--------
 net/can/gw.c                        |   13 +++----------
 net/ipv4/fib_trie.c                 |    8 +-------
 net/ipv4/inetpeer.c                 |    9 ++-------
 net/ipv6/ip6_fib.c                  |    9 +--------
 net/ipv6/xfrm6_tunnel.c             |    8 +-------
 net/kcm/kcmsock.c                   |   10 +---------
 net/netfilter/nf_conncount.c        |   10 +---------
 net/netfilter/nf_conntrack_expect.c |   10 +---------
 net/netfilter/xt_hashlimit.c        |    9 +--------
 18 files changed, 22 insertions(+), 143 deletions(-)