Skip to contentSkip to conversation

locklessly protect left members in struct rps_dev_flow

PATCH Superseded Jason Xing 1 participant 5 messages Apr 16, 2024 – Apr 17, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jason Xing

author
#0 lore raw

cover locklessly protect left members in struct rps_dev_flow

From: Jason Xing <[email protected]>

Since Eric did a more complicated locklessly change to last_qtail member[1] in struct rps_dev_flow, the left members are easier to change as the same.

[1]:
https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git/commit/?id=3b4cf29bdab

Jason Xing (3):
net: rps: protect last_qtail with rps_input_queue_tail_save() helper
net: rps: protect filter locklessly
net: rps: locklessly access rflow->cpu

 net/core/dev.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
Signature · 1 line
2.37.3
author
#1 lore raw

PATCH 1/3 net: rps: protect last_qtail with rps_input_queue_tail_save() helper

From: Jason Xing <[email protected]>

Only one left place should be proctected locklessly. This patch made it.

---
 net/core/dev.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/core/dev.c +3 -3
index 854a3a28a8d8..cd97eeae8218 100644
@@ -4501,7 +4501,7 @@ set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
 		struct netdev_rx_queue *rxqueue;
 		struct rps_dev_flow_table *flow_table;
 		struct rps_dev_flow *old_rflow;
-		u32 flow_id;
+		u32 flow_id, head;
 		u16 rxq_index;
 		int rc;
 
@@ -4529,8 +4529,8 @@ set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
 			old_rflow->filter = RPS_NO_FILTER;
 	out:
 #endif
-		rflow->last_qtail =
-			READ_ONCE(per_cpu(softnet_data, next_cpu).input_queue_head);
+		head = READ_ONCE(per_cpu(softnet_data, next_cpu).input_queue_head);
+		rps_input_queue_tail_save(rflow->last_qtail, head);
 	}
 
 	rflow->cpu = next_cpu;
Signature · 1 line
2.37.3
author
#2 lore raw

Re: [PATCH net-next 1/3] net: rps: protect last_qtail with rps_input_queue_tail_save() helper

Quoted 24 lines · Jason Xing

From: Jason Xing <[email protected]>

Only one left place should be proctected locklessly. This patch made it.

---
 net/core/dev.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/core/dev.c +1 -1
index 854a3a28a8d8..cd97eeae8218 100644
@@ -4501,7 +4501,7 @@ set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
                struct netdev_rx_queue *rxqueue;
                struct rps_dev_flow_table *flow_table;
                struct rps_dev_flow *old_rflow;
-               u32 flow_id;
+               u32 flow_id, head;
                u16 rxq_index;
                int rc;
 

net/core/dev.c set_rps_cpu(struct net_device *dev, struct sk_buff *skb, · line 4533

Quoted 7 lines
net/core/dev.c +2 -2
@@ -4529,8 +4529,8 @@ set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
                        old_rflow->filter = RPS_NO_FILTER;
        out:
 #endif
-               rflow->last_qtail =
-                       READ_ONCE(per_cpu(softnet_data, next_cpu).input_queue_head);
+               head = READ_ONCE(per_cpu(softnet_data, next_cpu).input_queue_head);
+               rps_input_queue_tail_save(rflow->last_qtail, head);

I made a mistake. I should pass &rflow->last_qtail actually. Will update it.

Quoted 6 lines
net/core/dev.c +0 -1
        }
 
        rflow->cpu = next_cpu;
--

2.37.3

author
#3 lore raw

PATCH 2/3 net: rps: protect filter locklessly

In reply to Jason Xing

From: Jason Xing <[email protected]>

As we can see, rflow->filter can be written/read concurrently, so lockless access is needed.

---
I'm not very sure if the READ_ONCE in set_rps_cpu() is useful. I scaned/checked the codes and found no lock can prevent multiple threads from calling set_rps_cpu() and handling the same flow simultaneously. The same question still exists in patch [3/3].

---
 net/core/dev.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/core/dev.c +3 -3
index cd97eeae8218..6892682f9cbf 100644
@@ -4524,8 +4524,8 @@ set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
 			goto out;
 		old_rflow = rflow;
 		rflow = &flow_table->flows[flow_id];
-		rflow->filter = rc;
-		if (old_rflow->filter == rflow->filter)
+		WRITE_ONCE(rflow->filter, rc);
+		if (old_rflow->filter == READ_ONCE(rflow->filter))
 			old_rflow->filter = RPS_NO_FILTER;
 	out:
 #endif
@@ -4666,7 +4666,7 @@ bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
 	if (flow_table && flow_id <= flow_table->mask) {
 		rflow = &flow_table->flows[flow_id];
 		cpu = READ_ONCE(rflow->cpu);
-		if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
+		if (READ_ONCE(rflow->filter) == filter_id && cpu < nr_cpu_ids &&
 		    ((int)(READ_ONCE(per_cpu(softnet_data, cpu).input_queue_head) -
 			   READ_ONCE(rflow->last_qtail)) <
 		     (int)(10 * flow_table->mask)))
Signature · 1 line
2.37.3
author
#4 lore raw

PATCH 3/3 net: rps: locklessly access rflow->cpu

In reply to Jason Xing

From: Jason Xing <[email protected]>

This is the last member in struct rps_dev_flow which should be protected locklessly. So finish it.

---
 net/core/dev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/dev.c +2 -2
index 6892682f9cbf..6003553c05fc 100644
@@ -4533,7 +4533,7 @@ set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
 		rps_input_queue_tail_save(rflow->last_qtail, head);
 	}
 
-	rflow->cpu = next_cpu;
+	WRITE_ONCE(rflow->cpu, next_cpu);
 	return rflow;
 }
 
@@ -4597,7 +4597,7 @@ static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
 		 * we can look at the local (per receive queue) flow table
 		 */
 		rflow = &flow_table->flows[hash & flow_table->mask];
-		tcpu = rflow->cpu;
+		tcpu = READ_ONCE(rflow->cpu);
 
 		/*
 		 * If the desired CPU (where last recvmsg was done) is
Signature · 1 line
2.37.3