Skip to contentSkip to conversation

can: gw: use call_rcu() instead of costly synchronize_rcu()

PATCH In review Eric Dumazet 4 participants 5 messages Feb 7, 2022 – Feb 9, 2022

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

PATCH 1/1 can: gw: use call_rcu() instead of costly synchronize_rcu()

From: Eric Dumazet <[email protected]>

Commit fb8696ab14ad ("can: gw: synchronize rcu operations before removing gw job entry") added three synchronize_rcu() calls to make sure one rcu grace period was observed before freeing a "struct cgw_job" (which are tiny objects).

This should be converted to call_rcu() to avoid adding delays in device / network dismantles.

Use the rcu_head that was already in struct cgw_job, not yet used.

Cc Oliver Hartkopp <[email protected]>
---
 net/can/gw.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
net/can/gw.c +10 -6
index d8861e862f157aec36c417b71eb7e8f59bd064b9..20e74fe7d0906dccc65732b8f9e7e14e2d1192c3 100644
@@ -577,6 +577,13 @@ 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)
 {
@@ -596,8 +603,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);
-				synchronize_rcu();
-				kmem_cache_free(cgw_cache, gwj);
+				call_rcu(&gwj->rcu, cgw_job_free_rcu);
 			}
 		}
 	}
@@ -1155,8 +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);
-		synchronize_rcu();
-		kmem_cache_free(cgw_cache, gwj);
+		call_rcu(&gwj->rcu, cgw_job_free_rcu);
 	}
 }
 
@@ -1224,8 +1229,7 @@ static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 		hlist_del(&gwj->list);
 		cgw_unregister_filter(net, gwj);
-		synchronize_rcu();
-		kmem_cache_free(cgw_cache, gwj);
+		call_rcu(&gwj->rcu, cgw_job_free_rcu);
 		err = 0;
 		break;
 	}
Signature · 1 line
2.35.0.263.gb82422642f-goog
maintainer Tested-by on v1 1/1
#1 lore raw
Quoted 7 lines · Eric Dumazet

From: Eric Dumazet <[email protected]>

Commit fb8696ab14ad ("can: gw: synchronize rcu operations before removing gw job entry") added three synchronize_rcu() calls to make sure one rcu grace period was observed before freeing a "struct cgw_job" (which are tiny objects).

Quoted 8 lines

This should be converted to call_rcu() to avoid adding delays in device / network dismantles.

Use the rcu_head that was already in struct cgw_job, not yet used.

Cc Oliver Hartkopp <[email protected]>

Thanks Eric!

Quoted 52 lines
---
  net/can/gw.c | 16 ++++++++++------
  1 file changed, 10 insertions(+), 6 deletions(-)
net/can/gw.c +10 -6
index d8861e862f157aec36c417b71eb7e8f59bd064b9..20e74fe7d0906dccc65732b8f9e7e14e2d1192c3 100644
@@ -577,6 +577,13 @@ 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)
  {
@@ -596,8 +603,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);
-				synchronize_rcu();
-				kmem_cache_free(cgw_cache, gwj);
+				call_rcu(&gwj->rcu, cgw_job_free_rcu);
  			}
  		}
  	}
@@ -1155,8 +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);
-		synchronize_rcu();
-		kmem_cache_free(cgw_cache, gwj);
+		call_rcu(&gwj->rcu, cgw_job_free_rcu);
  	}
  }
 
@@ -1224,8 +1229,7 @@ static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh,
 
  		hlist_del(&gwj->list);
  		cgw_unregister_filter(net, gwj);
-		synchronize_rcu();
-		kmem_cache_free(cgw_cache, gwj);
+		call_rcu(&gwj->rcu, cgw_job_free_rcu);
  		err = 0;
  		break;
  	}
maintainer
#3 lore raw
In reply to Eric Dumazet
Quoted 7 lines · On Mon, 7 Feb 2022 11:07:06 -0800 Eric Dumazet wrote:

From: Eric Dumazet <[email protected]>

Commit fb8696ab14ad ("can: gw: synchronize rcu operations before removing gw job entry") added three synchronize_rcu() calls to make sure one rcu grace period was observed before freeing a "struct cgw_job" (which are tiny objects).

Quoted 8 lines

This should be converted to call_rcu() to avoid adding delays in device / network dismantles.

Use the rcu_head that was already in struct cgw_job, not yet used.

Cc Oliver Hartkopp <[email protected]>

Adding Marc and linux-can to CC.

#2 lore raw
In reply to Oliver Hartkopp
Quoted 13 lines · Oliver Hartkopp
Quoted 10 lines · Eric Dumazet

From: Eric Dumazet <[email protected]>

Commit fb8696ab14ad ("can: gw: synchronize rcu operations before removing gw job entry") added three synchronize_rcu() calls to make sure one rcu grace period was observed before freeing a "struct cgw_job" (which are tiny objects).

This should be converted to call_rcu() to avoid adding delays in device / network dismantles.

Quoted 7 lines
Quoted 5 lines

Use the rcu_head that was already in struct cgw_job, not yet used.

Cc Oliver Hartkopp <[email protected]>

Applied to linux-can-next/testing.

There are several more synchronize_rcu() in net/can. Oliver, can you create similar patches for those?

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
#4 lore raw
In reply to Eric Dumazet
Quoted 7 lines · Eric Dumazet

From: Eric Dumazet <[email protected]>

Commit fb8696ab14ad ("can: gw: synchronize rcu operations before removing gw job entry") added three synchronize_rcu() calls to make sure one rcu grace period was observed before freeing a "struct cgw_job" (which are tiny objects).

Quoted 8 lines

This should be converted to call_rcu() to avoid adding delays in device / network dismantles.

Use the rcu_head that was already in struct cgw_job, not yet used.

Cc Oliver Hartkopp <[email protected]>

Added to linux-can-next/testing.

Thanks,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |