Skip to contentSkip to conversation

openvswitch: meter: fix race when getting now_ms.

PATCH Applied Tao Liu 3 participants 3 messages May 13, 2021

Subsystems (3)
lore
Share & tools
lore

Tao Liu

author
#0 lore raw

PATCH v2 1/1 openvswitch: meter: fix race when getting now_ms.

We have observed meters working unexpected if traffic is 3+Gbit/s with multiple connections.

now_ms is not pretected by meter->lock, we may get a negative long_delta_ms when another cpu updated meter->used, then:

    delta_ms = (u32)long_delta_ms;

which will be a large value.

    band->bucket += delta_ms * band->rate;

then we get a wrong band->bucket.

OpenVswitch userspace datapath has fixed the same issue[1] some time ago, and we port the implementation to kernel datapath.

[1] https://patchwork.ozlabs.org/project/openvswitch/patch/[email protected]/

Fixes 96fbc13d7e77 ("openvswitch: Add meter infrastructure")

---
Changelog:
v2: just set negative long_delta_ms to zero in case of race for meter lock.
v1: make now_ms protected by meter lock.

---
 net/openvswitch/meter.c | 8 ++++++++
 1 file changed, 8 insertions(+)
net/openvswitch/meter.c +8 -0
index 96b524c..896b8f5 100644
@@ -611,6 +611,14 @@ bool ovs_meter_execute(struct datapath *dp, struct sk_buff *skb,
 	spin_lock(&meter->lock);
 
 	long_delta_ms = (now_ms - meter->used); /* ms */
+	if (long_delta_ms < 0) {
+		/* This condition means that we have several threads fighting
+		 * for a meter lock, and the one who received the packets a
+		 * bit later wins. Assuming that all racing threads received
+		 * packets at the same time to avoid overflow.
+		 */
+		long_delta_ms = 0;
+	}
 
 	/* Make sure delta_ms will not be too large, so that bucket will not
 	 * wrap around below.
Signature · 1 line
1.8.3.1
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 16 lines · Tao Liu

We have observed meters working unexpected if traffic is 3+Gbit/s with multiple connections.

now_ms is not pretected by meter->lock, we may get a negative long_delta_ms when another cpu updated meter->used, then:

    delta_ms = (u32)long_delta_ms;

which will be a large value.

    band->bucket += delta_ms * band->rate;

then we get a wrong band->bucket.

OpenVswitch userspace datapath has fixed the same issue[1] some time ago, and we port the implementation to kernel datapath.

[1] https://patchwork.ozlabs.org/project/openvswitch/patch/[email protected]/

Quoted 8 lines
Fixes 96fbc13d7e77 ("openvswitch: Add meter infrastructure")

---
Changelog:
v2: just set negative long_delta_ms to zero in case of race for meter lock.
v1: make now_ms protected by meter lock.
---

Thanks!
I didn't test it, but the change looks good to me.

Quoted 23 lines
 net/openvswitch/meter.c | 8 ++++++++
 1 file changed, 8 insertions(+)
net/openvswitch/meter.c +8 -0
index 96b524c..896b8f5 100644
@@ -611,6 +611,14 @@ bool ovs_meter_execute(struct datapath *dp, struct sk_buff *skb,
 	spin_lock(&meter->lock);
 
 	long_delta_ms = (now_ms - meter->used); /* ms */
+	if (long_delta_ms < 0) {
+		/* This condition means that we have several threads fighting
+		 * for a meter lock, and the one who received the packets a
+		 * bit later wins. Assuming that all racing threads received
+		 * packets at the same time to avoid overflow.
+		 */
+		long_delta_ms = 0;
+	}
 
 	/* Make sure delta_ms will not be too large, so that bucket will not
 	 * wrap around below.
#2 lore raw
In reply to Tao Liu
This patch was applied to netdev/net.git (refs/heads/master):

Hello:

This patch was applied to netdev/net.git (refs/heads/master):

Quoted 9 lines · On Thu, 13 May 2021 21:08:00 +0800 you wrote:

We have observed meters working unexpected if traffic is 3+Gbit/s with multiple connections.

now_ms is not pretected by meter->lock, we may get a negative long_delta_ms when another cpu updated meter->used, then:

    delta_ms = (u32)long_delta_ms;

which will be a large value.

[...]

Here is the summary with links:
- [ovs-dev,net,v2] openvswitch: meter: fix race when getting now_ms.

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

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html