Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp1974275rwe; Fri, 2 Sep 2022 06:50:54 -0700 (PDT) X-Google-Smtp-Source: AA6agR415Q6yyIaT0Jegi3P7AxGfIJIz3fYtaIZiJZihzpeQNv5DwXPE9V5BkWuCKbUKMKv09CJZ X-Received: by 2002:a17:90a:c402:b0:1f7:75ce:1206 with SMTP id i2-20020a17090ac40200b001f775ce1206mr5030205pjt.68.1662126654026; Fri, 02 Sep 2022 06:50:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662126654; cv=none; d=google.com; s=arc-20160816; b=lis4JhYHCAHapGzijAGlkxpGqxUqVmPgznGNk+XN8I/gKH6Smx8FoXgciAF/rHtKgt 4Tz2XeyS6fZ9hWeWAKRwtIQEC24A017Qfop8mYVog11srCz5xkA7MFANZx15DZH/PH4j zbK2NBhHoxpqinqqrdEkdwwdy7RJynq+8Xbbnwkg8hBaoKczk7+mi4AD0szisiU53Ksb esjgK7vRCUlR5sYWgZO2DtkZtPVfoXmW0BzMYqS7tOW+4yhtuvZeIVBPziCf3yYDpBGX FPjSkFqxQnBDriAxp/MnGYMffhmMjgMeei/KknwIv4HN1aSfmDsdX9cupa3mQMBkNPzj x0bg== 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=ZHDi042WGoJzw2x0F/DxSiY488zXGwnsp+HEgA8VAMM=; b=BqvDsZFb1kR1gf7d6MMosegLT/Q247SsYta//YZBHhp+oVxesJlGw6YMRx4CdDWnPL HO8oi56JZhA+xvDo1UJhPrjEpdHmSUK9el7nIK5BYwxXykogNWhUsumfWuYHQ3y5o56v Tli6IoYFxEa1lKhJ1YK1af1ToPg9IptOipLfaqNziYQsYif1WXQ9IIeQqtzpwTTOlps+ kVbueSk+yxbUcws4Lh4lxEIM5kB+VGygKNbIuWCpGSknEOT3XW8r20X2DoQWMLVivBBz criz+XAuxXo4dvcysCepHLJ8w7u5e8ERfyLWrr/0a7bFD8MNYdgCcxLZCkCXvxVho4nJ J+ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GqJeItBX; 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 j4-20020a170902da8400b0016dd2643111si1927632plx.311.2022.09.02.06.50.30; Fri, 02 Sep 2022 06:50:54 -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=GqJeItBX; 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 S232701AbiIBMWV (ORCPT + 99 others); Fri, 2 Sep 2022 08:22:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235907AbiIBMVP (ORCPT ); Fri, 2 Sep 2022 08:21:15 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEBE77F0A1; Fri, 2 Sep 2022 05:20:55 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 5DCD4620EB; Fri, 2 Sep 2022 12:20:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5316DC433D6; Fri, 2 Sep 2022 12:20:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1662121254; bh=0VtpXqFvjdzolSkUYnkn3ks1g0xBS+pk9oJcQgLaFCA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GqJeItBXxICCJSz05bSCTGN3UmI1WLKK5BCscS6z7VoIeX9BfJiwCMFOqBbdwErHr 7jQ9DQZu+BQA7hIzEIt9v0vOLWpHR0wwdtYMyRSeB4Xf+RrypMAUc0ThXo5dB6BEL2 iQ67h3caXtZNjjvqWsOy1JHSJFi26CvX6CiGwY6Q= 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 4.9 07/31] ratelimit: Fix data-races in ___ratelimit(). Date: Fri, 2 Sep 2022 14:18:33 +0200 Message-Id: <20220902121357.020913818@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220902121356.732130937@linuxfoundation.org> References: <20220902121356.732130937@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 d01f471352390..b805702de84dd 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -27,10 +27,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; /* @@ -45,7 +51,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 @@ -57,7 +63,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