Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp708782pxj; Thu, 13 May 2021 15:11:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwQD4TFM8uTfSGsGfz+BnjhbPplTlnnMFtXlMy40Q74XwOpjCKDQBtL0njtx6so8wg6907N X-Received: by 2002:a17:906:8389:: with SMTP id p9mr12845090ejx.106.1620943907599; Thu, 13 May 2021 15:11:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620943907; cv=none; d=google.com; s=arc-20160816; b=reZpgJ9edxqs9LpF7BHfBSb5TL54Mgxo23Jp9ApK4A7KW57IyHtA+sWNFSNw8A6+Vu RqMN42DrbmmfQ4xZSXqk0eBeoEJpat6gZ1voLs3kgIkLyEqZLqxIajQLY7VMX56UfnOS uu13vOJmPABUmlC/eo3WEizVQSOWVSFSB7rJSrNpY/DCyr1QU4OVF+FWJcta4WjlxuJR s5IUGdeMvGm9I3wfmBGE197o0ipHoqeY04WxLxTjuGCgeOgawIM3LgwuHL5/F/hqpPl+ ok7DtpPNDM6hFKbvd4Z5+BmXn4pwxoPKpKwSzKbXMG7LyiMKyn212R2KcXeKDYR1Dpo8 FXng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=zsxjEwD7KwUFslj2KoLqJTFXzD00pqtNje/Xxm3ezW0=; b=ffmE2VN2i81QsI7p/xdDmA1nlPMt65tS+tfRJUpBKLWAQC7q59u5tJ4BDs3oa1MIDJ tQNFNNcptsLueETVBQ4T9kKrU3qrK/OedMxoB0wXowrScII3ABlBNzAvQ32rDIEkGYhw lp8TnXaDfQeAfzi2wyJ96hp50Gexqrtb1I5rigjDjok/nkKyk0zvV1bwPaKh0WhvyP3W UjIITg29xdE9ODMzIfvRdp+m94h0gWHhicop6N5zrDHAcL+lPu+Py0UP1uyOts7tKubQ BdlgE80THlF8iJk+En1IMS1sbROTuXWSr3WaOJ/uBKBaxk8kcM/Nftmd3WR8r+Fu8W6Q T9kQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v5si4657239edi.123.2021.05.13.15.11.09; Thu, 13 May 2021 15:11:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234563AbhEMOhU (ORCPT + 99 others); Thu, 13 May 2021 10:37:20 -0400 Received: from relay11.mail.gandi.net ([217.70.178.231]:55027 "EHLO relay11.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234544AbhEMOhD (ORCPT ); Thu, 13 May 2021 10:37:03 -0400 Received: from [192.168.1.23] (ip-78-45-89-65.net.upcbroadband.cz [78.45.89.65]) (Authenticated sender: i.maximets@ovn.org) by relay11.mail.gandi.net (Postfix) with ESMTPSA id 0B755100002; Thu, 13 May 2021 14:35:48 +0000 (UTC) Subject: Re: [ovs-dev] [PATCH net v2] openvswitch: meter: fix race when getting now_ms. To: Tao Liu , pshelar@ovn.org Cc: dev@openvswitch.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, i.maximets@ovn.org, jean.tourrilhes@hpe.com, kuba@kernel.org, davem@davemloft.net, wenxu@ucloud.cn References: <20210513130800.31913-1-thomas.liu@ucloud.cn> From: Ilya Maximets Message-ID: Date: Thu, 13 May 2021 16:35:47 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <20210513130800.31913-1-thomas.liu@ucloud.cn> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > Suggested-by: Ilya Maximets > --- > 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 > 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. >