Skip to contentSkip to conversation

net: openvswitch: fix data race in ovs_vport_get_upcall_stats

PATCH Applied David Yang 5 participants 5 messages Jan 21 – Jan 22

Subsystems (3)
lore
Share & tools
lore

David Yang

author
#0 lore raw

PATCH v2 1/1 net: openvswitch: fix data race in ovs_vport_get_upcall_stats

In ovs_vport_get_upcall_stats(), some statistics protected by u64_stats_sync, are read and accumulated in ignorance of possible u64_stats_fetch_retry() events. These statistics are already accumulated by u64_stats_inc(). Fix this by reading them into temporary variables first.

Fixes 1933ea365aa7 ("net: openvswitch: Add support to count upcall packets")

---
v1: https://lore.kernel.org/r/[email protected]
- use u64 instead of __u64

 net/openvswitch/vport.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
net/openvswitch/vport.c +6 -5
index 6bbbc16ab778..f0ce8ce1dce0 100644
@@ -310,22 +310,23 @@ void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats)
  */
 int ovs_vport_get_upcall_stats(struct vport *vport, struct sk_buff *skb)
 {
+	u64 tx_success = 0, tx_fail = 0;
 	struct nlattr *nla;
 	int i;
 
-	__u64 tx_success = 0;
-	__u64 tx_fail = 0;
-
 	for_each_possible_cpu(i) {
 		const struct vport_upcall_stats_percpu *stats;
+		u64 n_success, n_fail;
 		unsigned int start;
 
 		stats = per_cpu_ptr(vport->upcall_stats, i);
 		do {
 			start = u64_stats_fetch_begin(&stats->syncp);
-			tx_success += u64_stats_read(&stats->n_success);
-			tx_fail += u64_stats_read(&stats->n_fail);
+			n_success = u64_stats_read(&stats->n_success);
+			n_fail = u64_stats_read(&stats->n_fail);
 		} while (u64_stats_fetch_retry(&stats->syncp, start));
+		tx_success += n_success;
+		tx_fail += n_fail;
 	}
 
 	nla = nla_nest_start_noflag(skb, OVS_VPORT_ATTR_UPCALL_STATS);
Signature · 1 line
2.51.0
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 32 lines · David Yang

In ovs_vport_get_upcall_stats(), some statistics protected by u64_stats_sync, are read and accumulated in ignorance of possible u64_stats_fetch_retry() events. These statistics are already accumulated by u64_stats_inc(). Fix this by reading them into temporary variables first.

Fixes 1933ea365aa7 ("net: openvswitch: Add support to count upcall packets")

---
v1: https://lore.kernel.org/r/[email protected]
- use u64 instead of __u64

 net/openvswitch/vport.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
net/openvswitch/vport.c +2 -3
index 6bbbc16ab778..f0ce8ce1dce0 100644
@@ -310,22 +310,23 @@ void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats)
  */
 int ovs_vport_get_upcall_stats(struct vport *vport, struct sk_buff *skb)
 {
+	u64 tx_success = 0, tx_fail = 0;
 	struct nlattr *nla;
 	int i;
 
-	__u64 tx_success = 0;
-	__u64 tx_fail = 0;
-
 	for_each_possible_cpu(i) {
 		const struct vport_upcall_stats_percpu *stats;
+		u64 n_success, n_fail;

net/openvswitch/vport.c void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats) · line 327

Quoted 10 lines
net/openvswitch/vport.c +2 -2
 		unsigned int start;
 
 		stats = per_cpu_ptr(vport->upcall_stats, i);
 		do {
 			start = u64_stats_fetch_begin(&stats->syncp);
-			tx_success += u64_stats_read(&stats->n_success);
-			tx_fail += u64_stats_read(&stats->n_fail);
+			n_success = u64_stats_read(&stats->n_success);
+			n_fail = u64_stats_read(&stats->n_fail);
 		} while (u64_stats_fetch_retry(&stats->syncp, start));

Would be good to have an empty line here for readability, but a new version just for this seems a bit excessive.

Either way:

Quoted 5 lines
net/openvswitch/vport.c +2 -0
+		tx_success += n_success;
+		tx_fail += n_fail;
 	}
 
 	nla = nla_nest_start_noflag(skb, OVS_VPORT_ATTR_UPCALL_STATS);
Reviewed-by on v2 1/1
#2 lore raw
In reply to David Yang
Quoted 9 lines · David Yang

In ovs_vport_get_upcall_stats(), some statistics protected by u64_stats_sync, are read and accumulated in ignorance of possible u64_stats_fetch_retry() events. These statistics are already accumulated by u64_stats_inc(). Fix this by reading them into temporary variables first.

Fixes 1933ea365aa7 ("net: openvswitch: Add support to count upcall packets")
maintainer Reviewed-by on v2 1/1
#3 lore raw
In reply to David Yang
Quoted 6 lines · David Yang

In ovs_vport_get_upcall_stats(), some statistics protected by u64_stats_sync, are read and accumulated in ignorance of possible u64_stats_fetch_retry() events. These statistics are already accumulated by u64_stats_inc(). Fix this by reading them into temporary variables first.

Quoted 7 lines
Fixes 1933ea365aa7 ("net: openvswitch: Add support to count upcall packets")

---
v1: https://lore.kernel.org/r/[email protected]
- use u64 instead of __u64

 net/openvswitch/vport.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Thanks for the patch - I did a spot-check of the rest of the module with the following:

~/git/linux/net/openvswitch$ grep -nr 'while\(.*fetch.*\)'

and didn't find any other places that seemed to duplicate this mistaken read pattern (at least in the OVS kernel module).

#4 lore raw
In reply to David Yang
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:

Quoted 10 lines · On Wed, 21 Jan 2026 15:29:26 +0800 you wrote:

In ovs_vport_get_upcall_stats(), some statistics protected by u64_stats_sync, are read and accumulated in ignorance of possible u64_stats_fetch_retry() events. These statistics are already accumulated by u64_stats_inc(). Fix this by reading them into temporary variables first.

Fixes 1933ea365aa7 ("net: openvswitch: Add support to count upcall packets")

[...]

Here is the summary with links:
- [net,v2] net: openvswitch: fix data race in ovs_vport_get_upcall_stats

    https://git.kernel.org/netdev/net/c/cc4816bdb086

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html