Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp3054275rwe; Mon, 29 Aug 2022 05:12:48 -0700 (PDT) X-Google-Smtp-Source: AA6agR7Zr0211tCOxBLIwBAj6lowiheBp0hmUqomV5+x0cWQo63ZADNybK0pnqi91DrEumMUSMZd X-Received: by 2002:a17:90b:2496:b0:1ef:a94:7048 with SMTP id nt22-20020a17090b249600b001ef0a947048mr18107790pjb.244.1661775168573; Mon, 29 Aug 2022 05:12:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661775168; cv=none; d=google.com; s=arc-20160816; b=LSxdL573XmDfSMSSXfbAjL/uyBWwP+rXh5DjT+ZdeGN+lQp5CvDvUb7RIFGoUjrgeX Inq53PrP+QYSFjpWT83kd1uCpqLDbsHQZiVz198W2xUO2QhXGsEqQ+sYTEt7ogKTxD1H FUyb5joe35OCevlnVlN3Zqn8RBq8yIEvNw+kzv/0RaWSgOm2AUozyHbFkxbt6xQN3qat xqHAVL2xRrtvgmZQID3xIsu+GpuGmIHpFRYFFh8COz6USysadvJX9zzaSVCpwOb6+vK7 bDzCzGuVcgZVuEFHg0s6bwCX6b3xQROxusWVqlK2gye6iARaK8w8RrBzavCoz16kvsN2 7/Zg== 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=H3tBZ7fXMyeEEotNF6cfvVzv197t1BIw1NHAH8K4akGTyhekeT1IHc756nD0rrKKc/ tUAvEGXZRdPQ5shmc3QuXuing060P7xP4RybJr+wsC1MwB/H4mnnoD5WCPfD05RsD5R8 0fBaf4AoCiugYhSmIYD9AMEE4Ke04WaD2wnmZ+DfOQcXw9wEsU5QX7HjAl3WmYEjaqwp w+t5CtmaEB469uWz8qVEv4ehKicMZFoEfSogLqOoV/weKc+4dr0/61wZAnOmalBNdFeL 22Z3WqX9wgL0fLb5Ql1yQeTc2GwelkoK3h/WCaBR4Lc+GWvey/Pg4vteTjDHOuzargcL Y0aw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=EvIB9YLI; 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 cr8-20020a056a000f0800b0053646d7782fsi9475177pfb.80.2022.08.29.05.12.36; Mon, 29 Aug 2022 05:12:48 -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=EvIB9YLI; 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 S232165AbiH2L06 (ORCPT + 99 others); Mon, 29 Aug 2022 07:26:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232265AbiH2LYi (ORCPT ); Mon, 29 Aug 2022 07:24:38 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABEA2760E4; Mon, 29 Aug 2022 04:15:38 -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 65475B80FA8; Mon, 29 Aug 2022 11:15:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B1010C433C1; Mon, 29 Aug 2022 11:15:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661771707; bh=FufMUVUaQh+PDh29tlw9bNgetLwgT2UPVsGlg+Vo9Jk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EvIB9YLIzRbN+blTGnOVN4y+73RoKREYjubQ2c1k2x9Jg8vbuPAUHgQK4S4KP5UO/ +MHrrDqZRDFNXXE516VhZAYbCQHx/V4NEWTOJE8EPBYjx4byfopT432dUsQpJXMgMP B7HRsRdc4qoE3iZOUL1zlp088MNbJl4P4hUgKyyw= 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.19 069/158] ratelimit: Fix data-races in ___ratelimit(). Date: Mon, 29 Aug 2022 12:58:39 +0200 Message-Id: <20220829105811.888810970@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220829105808.828227973@linuxfoundation.org> References: <20220829105808.828227973@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