Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2749697pxj; Mon, 31 May 2021 09:47:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJze42zt+ypfrUo9jujWsxsS/PaQaHzHLJNMh9avgor4N3RYQDC/Ej5YLWW1TeyD2TmG6KPN X-Received: by 2002:a05:6638:2728:: with SMTP id m40mr21425468jav.55.1622479623994; Mon, 31 May 2021 09:47:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622479623; cv=none; d=google.com; s=arc-20160816; b=mhrv+5IJ5qAByYt9obMzRPDvp2tuRkWvn3aQ/CgPbZrN7Vsnu3u2QsQlQGpMOQygIJ 8V+f/7wurYi0mu/1FKQVjjXn1KL0eT8HWmykAb7K9KNguIzHjw/eOq/e8LvvxhGSps// JoEB2s6O//Y9vGzoVv2aDv57PrVow0WcMvwRfOS6vw3Z6B4vvG6dsL2NBU47eMZ1Azn+ 36aYFbPUL1+6NjiPbC95Enu/G5MvSFngVYw6gyrPGe/lE29L/iD/+bGBIF3VfMZR5Mnb 1RO+cP6NuyWuZUvun2dAW/4spLayupGXcYA3i5EKYj6GDP64MC+6NKEk0ZIEV6NuWFL0 sIoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=1CWocLgt1B7OfpyQ3it93xHPTFShMU/CwXh+bl4I6Ao=; b=DEuuhB3TUvBISg3Z+CKyGsaK2LC8/E2kQ7P4HlEpL9kmE8cabRCL2OcvO5iq4+v9Y1 ZgIWmuaeZ0qys4aXFQmw+EZsq23GaljN/1hOI7o2OAEp7sHLL6oIPn7LuhjZuZNA5v6y kzfaKTzYFpNzhb9XlXdOaWxzIkan+JSm9VsktHkV2Ko783fHCW8atf/t9PfxiD1oYiv7 GM9ZslvCBU4ipO+1MM5oSrAaghdaJcPqqUbMlhqEClj8ovk/nfjiNcio2XGEQSHKobss 8w9u6n8m4/aQzG4cWMMRG291FTo0vv+DwIMoFkvs/6byKFgm7spZ5qnSDcU28Wsiquta zw3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=sSWfg1Uq; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h6si19099974iow.71.2021.05.31.09.46.50; Mon, 31 May 2021 09:47:03 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=sSWfg1Uq; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233271AbhEaQry (ORCPT + 99 others); Mon, 31 May 2021 12:47:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:45938 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234095AbhEaOxq (ORCPT ); Mon, 31 May 2021 10:53:46 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C978B61936; Mon, 31 May 2021 13:59:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622469553; bh=1mNxDuzzYQ5c3jQGFmTrE83ZE0+KtLLBOnm8/X6TgSY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sSWfg1Uqiyxp7GhQkciwPueHnvbKD7HrX+gNUXurJ01cIH0KEWCwRzaVJP4/1buzE /hnt9XTzR+r4dkZ9iPIaM5poJPj4NNKmjz+mZzXhyKbE1Nx9UQ4CTZkjeIFsVPIX6h hd91EmIpQM1yZjyKM2dpJmtmRbyiSOIeBDuAvVOM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tao Liu , Ilya Maximets , "David S. Miller" , Sasha Levin Subject: [PATCH 5.12 240/296] openvswitch: meter: fix race when getting now_ms. Date: Mon, 31 May 2021 15:14:55 +0200 Message-Id: <20210531130711.866368692@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130703.762129381@linuxfoundation.org> References: <20210531130703.762129381@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tao Liu [ Upstream commit e4df1b0c24350a0f00229ff895a91f1072bd850d ] 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 Reviewed-by: Ilya Maximets Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/openvswitch/meter.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/net/openvswitch/meter.c b/net/openvswitch/meter.c index 15424d26e85d..ca3c37f2f1e6 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. -- 2.30.2