Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp3099580rwe; Mon, 29 Aug 2022 05:58:28 -0700 (PDT) X-Google-Smtp-Source: AA6agR6kBP8+Go7Msy5gyr8lPz2W4fUN7yficM//qEBY9f0ak96vn+AGlY0KKAV+H4QiYM1C/dCN X-Received: by 2002:a17:907:2c74:b0:741:657a:89de with SMTP id ib20-20020a1709072c7400b00741657a89demr5181536ejc.58.1661777907005; Mon, 29 Aug 2022 05:58:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661777906; cv=none; d=google.com; s=arc-20160816; b=Ia6gxMxhU5E/1EgoRL2zHoecbnKiD9EYR28GjOLMzBaHECUnKbWKnng/jcL3FOX3Ep TLr+Jm7woCKvN0WxX6F1yafQfhzFdZRpuiH0HC4uZXNz3SgzCMA9jrZKsLm6lR1TuuNI XIjUWQ+SCrXW7Z+slP/9rwlLj7ZJLufWxBs15pu0vpKYlaJoFbV7N2TvsDPXWYPLE3wZ LOp6PGt04RHDF2ZzClcoEPZ5JCbY4QKil0ca08bwX8xhLOc7UjkEMr9t6oGcrK71iywK vTsac7Y6e9w7GaY9ubdgQHlnCKee+taCu6zb+TbVLVvoYRLnnbAdaRhmSifbPuum+6yr aLgg== 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=DrjMqLsHiNtg4M1WzU0tPhScsIoPHiwXUqgdbmkcFH8=; b=0Cc96JuTvnX46gixqer0tFvn0IKugbSeK5K9z72ABM7QhQs2BD9bAitoELqAc90eIv pRHm6ugk2eWFs6IaNGbkKmZMp+6KBA3iTTREd0MJivEGRHSMv7cw3wU/36pyAlB/Ccsd fmqERsPH1RXwYbG+3wOWFLVYinD0SvBeO35tdnkqg0/+yO223djJyaNz2YUCb6HbTgYx KIDJnfLQRs/LKOMX/ry4YVESjl5B+pcYMc2WCC3iS0DwAzauQr1Z6nRoxfuhwoUUjx+l JOfx/m9Bl7W+b7OGrvDqUROSFVYQoCwLIHW7tbXWv5JzIy0yO/CWbP11T3wjIgYxXc/q cOlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WxdszoDu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dr20-20020a170907721400b007307d1063besi7187465ejc.89.2022.08.29.05.58.01; Mon, 29 Aug 2022 05:58:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WxdszoDu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S229523AbiH2Mlh (ORCPT + 99 others); Mon, 29 Aug 2022 08:41:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230316AbiH2Mkv (ORCPT ); Mon, 29 Aug 2022 08:40:51 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9835D13F14; Mon, 29 Aug 2022 05:24:58 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id D5F15B80FA1; Mon, 29 Aug 2022 11:08:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2E407C433D6; Mon, 29 Aug 2022 11:08:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661771314; bh=FufMUVUaQh+PDh29tlw9bNgetLwgT2UPVsGlg+Vo9Jk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WxdszoDuVSmsgqCDUm6KB7181rwnZSXtc7mkK80iJwv+yBCI6A9n8iTR/3sl6ZdCt YeLvkyh6Etg9mYnIb+cbqmfQmneZOtC1L2WNaz8GufXfl4GkwD6v3Vf6ql+R4ZLFEq xivP6eaZzheiVA14PXlgG1jnnmPdx3g4IdTaMzPI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kuniyuki Iwashima , "David S. Miller" , Sasha Levin Subject: [PATCH 5.10 50/86] ratelimit: Fix data-races in ___ratelimit(). Date: Mon, 29 Aug 2022 12:59:16 +0200 Message-Id: <20220829105758.570721185@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220829105756.500128871@linuxfoundation.org> References: <20220829105756.500128871@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kuniyuki Iwashima [ Upstream commit 6bae8ceb90ba76cdba39496db936164fa672b9be ] While reading rs->interval and rs->burst, they can be changed concurrently via sysctl (e.g. net_ratelimit_state). Thus, we need to add READ_ONCE() to their readers. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Kuniyuki Iwashima Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- lib/ratelimit.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/ratelimit.c b/lib/ratelimit.c index e01a93f46f833..ce945c17980b9 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -26,10 +26,16 @@ */ int ___ratelimit(struct ratelimit_state *rs, const char *func) { + /* Paired with WRITE_ONCE() in .proc_handler(). + * Changing two values seperately could be inconsistent + * and some message could be lost. (See: net_ratelimit_state). + */ + int interval = READ_ONCE(rs->interval); + int burst = READ_ONCE(rs->burst); unsigned long flags; int ret; - if (!rs->interval) + if (!interval) return 1; /* @@ -44,7 +50,7 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func) if (!rs->begin) rs->begin = jiffies; - if (time_is_before_jiffies(rs->begin + rs->interval)) { + if (time_is_before_jiffies(rs->begin + interval)) { if (rs->missed) { if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) { printk_deferred(KERN_WARNING @@ -56,7 +62,7 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func) rs->begin = jiffies; rs->printed = 0; } - if (rs->burst && rs->burst > rs->printed) { + if (burst && burst > rs->printed) { rs->printed++; ret = 1; } else { -- 2.35.1