diff mbox series

[ovs-dev,net,v2] openvswitch: meter: fix race when getting now_ms.

Message ID 20210513130800.31913-1-thomas.liu@ucloud.cn (mailing list archive)
State Accepted
Commit e4df1b0c24350a0f00229ff895a91f1072bd850d
Delegated to: Netdev Maintainers
Headers show
Series [ovs-dev,net,v2] openvswitch: meter: fix race when getting now_ms. | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for net
netdev/subject_prefix success Link
netdev/cc_maintainers fail 1 blamed authors not CCed: azhou@ovn.org; 1 maintainers not CCed: azhou@ovn.org
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 14 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Tao Liu May 13, 2021, 1:08 p.m. UTC
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/20191025114436.9746-1-i.maximets@ovn.org/

Fixes: 96fbc13d7e77 ("openvswitch: Add meter infrastructure")
Signed-off-by: Tao Liu <thomas.liu@ucloud.cn>
Suggested-by: Ilya Maximets <i.maximets@ovn.org>
---
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(+)

Comments

Ilya Maximets May 13, 2021, 2:35 p.m. UTC | #1
On 5/13/21 3:08 PM, Tao Liu 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.
> 
>     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/20191025114436.9746-1-i.maximets@ovn.org/
> 
> Fixes: 96fbc13d7e77 ("openvswitch: Add meter infrastructure")
> Signed-off-by: Tao Liu <thomas.liu@ucloud.cn>
> Suggested-by: Ilya Maximets <i.maximets@ovn.org>
> ---
> 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.

Reviewed-by: Ilya Maximets <i.maximets@ovn.org>

>  net/openvswitch/meter.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/net/openvswitch/meter.c b/net/openvswitch/meter.c
> index 96b524c..896b8f5 100644
> --- a/net/openvswitch/meter.c
> +++ b/net/openvswitch/meter.c
> @@ -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.
>
patchwork-bot+netdevbpf@kernel.org May 13, 2021, 11 p.m. UTC | #2
Hello:

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

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
diff mbox series

Patch

diff --git a/net/openvswitch/meter.c b/net/openvswitch/meter.c
index 96b524c..896b8f5 100644
--- a/net/openvswitch/meter.c
+++ b/net/openvswitch/meter.c
@@ -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.