From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 188103FC5CE for ; Mon, 29 Jun 2026 10:23:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782728619; cv=none; b=G/RLv/Yi25exxV2iSB9867yceTQgO3at47dYVYRtHNdj+SPqAFTWPh8C30wAj97YCIslnvH3VGOoqEOtSaSXeznn1MqBO270u8HoaaEk09sMuYG2m6MNlsgCGPoN7/kJE6zk2j+9kyAmk+2KBaWVvlDK7ipCrdqTjr2zgVDrY/4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782728619; c=relaxed/simple; bh=f5HdFaueCd9qvhYc2cDhlPszPuQMo+u+qPUflTtVNQ8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=B9KIKW/dloUqZeqGQMpp7jo3efbsMNaBSpsYWn/ZP05AOSK1JP8ReScYFWtR0g7XTRlFwziqU7+HfjPOOkSvEnt4CsvTJ2b0ZAA7+TWiKpD1W7aJV7IpNbuuFYX6zvtw3JeTfrFUQP7yR7RL5uw76iCEApjZBFbZ6+LsA7NPVIE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=f3yZOVsI; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="f3yZOVsI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EEC401F000E9; Mon, 29 Jun 2026 10:23:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1782728617; bh=oMPIAqcNhKhzIHxMXNRypzSpgxgq+4eKBgqLmwcn81A=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=f3yZOVsIgo2uyjw3Y0zF2phd3/OPl12+Mu1OToptSS/rJFN8aeTuIiuI1KCFZuPpn kWopVQL6l8c1i4sSAGYQXF8c63uY8pktl+imRVn/nCaWld6pncKfKBN9QwQWOgGMuR PRAfNyysATJ/siBtDK49FHkllPPUPCFiS6qvZ+xh79EQE8U1uUPjI9PvsJPO0TSFw9 bPDWqD1peRxBUjoYnZgPOF5wj13KSzwL8/maCN4anq9eiUMTlFMOyPRksYtc9PrN93 DFeXRYMMR+fS82Gvr87yIslcM7RD8dPoz79AB8DW7Xk1yQZPi064FW9oDyxcH9PBBo iCDTg2Y5p5hew== Date: Mon, 29 Jun 2026 11:23:26 +0100 From: Lorenzo Stoakes To: "David Hildenbrand (Arm)" Cc: Xuewen Wang , akpm@linux-foundation.org, liam@infradead.org, vbabka@kernel.org, jannh@google.com, pfalcato@suse.de, chrisl@kernel.org, kasong@tencent.com, shikemeng@huaweicloud.com, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com, qi.zheng@linux.dev, shakeel.butt@linux.dev, axelrasmussen@google.com, yuanchu@google.com, weixugc@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] mm: annotate data-race in cpu_needs_drain() Message-ID: References: <20260626053700.2036899-1-wangxuewen@kylinos.cn> <04ea3e05-5b83-4c2d-8759-67c544a7b484@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <04ea3e05-5b83-4c2d-8759-67c544a7b484@kernel.org> On Fri, Jun 26, 2026 at 10:17:14AM +0200, David Hildenbrand (Arm) wrote: > On 6/26/26 07:37, Xuewen Wang wrote: > > KCSAN reports a data-race when cpu_needs_drain() reads another CPU's > > per-cpu folio_batch->nr without locking, while the owning CPU writes > > to it via folio_batch_add(). > > > > Reading a slightly stale value is harmless -- cpu_needs_drain() only > > decides whether to schedule a drain, and the next iteration of > > __lru_add_drain_all() will re-check. Use data_race() to annotate > > the intentional race. > > > > Signed-off-by: Xuewen Wang > > --- > > Changes in v3: > > - Wrap the entire || expression in a single data_race() instead of wrapping > > each folio_batch_count() call individually, as suggested by Pedro and Lorenzo. > > This is equally effective and more readable. > > - Remove data_race() from need_mlock_drain(), as it is now covered by the data_race() > > in cpu_needs_drain(). > > v2: > > https://lore.kernel.org/all/20260625065153.1581419-1-wangxuewen@kylinos.cn/ > > v1: > > https://lore.kernel.org/all/20260624092606.1083449-1-wangxuewen@kylinos.cn/ > > --- > > mm/swap.c | 4 ++-- > > 1 file changed, 2 insertions(+), 2 deletions(-) > > > > diff --git a/mm/swap.c b/mm/swap.c > > index 588f50d8f1a8..46ea207e0624 100644 > > --- a/mm/swap.c > > +++ b/mm/swap.c > > @@ -828,13 +828,13 @@ static bool cpu_needs_drain(unsigned int cpu) > > struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu); > > > > /* Check these in order of likelihood that they're not zero */ > > - return folio_batch_count(&fbatches->lru_add) || > > + return data_race(folio_batch_count(&fbatches->lru_add) || > > folio_batch_count(&fbatches->lru_move_tail) || > > folio_batch_count(&fbatches->lru_deactivate_file) || > > folio_batch_count(&fbatches->lru_deactivate) || > > folio_batch_count(&fbatches->lru_lazyfree) || > > folio_batch_count(&fbatches->lru_activate) || > > - need_mlock_drain(cpu) || > > + need_mlock_drain(cpu)) || > > The indentation is a bit suboptimal now. > > Would read nicer as > > diff --git a/mm/swap.c b/mm/swap.c > index 588f50d8f1a8c..5958e6fdd3593 100644 > --- a/mm/swap.c > +++ b/mm/swap.c > @@ -828,13 +828,13 @@ static bool cpu_needs_drain(unsigned int cpu) > struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu); > > /* Check these in order of likelihood that they're not zero */ > - return folio_batch_count(&fbatches->lru_add) || > - folio_batch_count(&fbatches->lru_move_tail) || > - folio_batch_count(&fbatches->lru_deactivate_file) || > - folio_batch_count(&fbatches->lru_deactivate) || > - folio_batch_count(&fbatches->lru_lazyfree) || > - folio_batch_count(&fbatches->lru_activate) || > - need_mlock_drain(cpu) || > + return data_race(folio_batch_count(&fbatches->lru_add) || > + folio_batch_count(&fbatches->lru_move_tail) || > + folio_batch_count(&fbatches->lru_deactivate_file) || > + folio_batch_count(&fbatches->lru_deactivate) || > + folio_batch_count(&fbatches->lru_lazyfree) || > + folio_batch_count(&fbatches->lru_activate) || > + need_mlock_drain(cpu)) || > has_bh_in_lru(cpu, NULL); Yeah that works for me. Andrew - maybe easier if you fix that up? :) > } > > But I'll let others decide :) > > Acked-by: David Hildenbrand (Arm) > > -- > Cheers, > > David Cheers, Lorenzo