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 F399234E762 for ; Mon, 29 Jun 2026 10:22:54 +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=1782728575; cv=none; b=ric/YxTN3TKC0fQH8PiydEsb/mfoSRD6PcWxoxo4kJKZih8DCeqxH1ZpZ0NICQp4NCX3tlIKYewOhPiTAlHRt6ksVB6TGi9q0RgB1LOnvQJXQ+C2tiBMFnRi6E1QGY99RdE63BbuYH+MCDoQ1pEE88AwnWRQR8UJRsYmCpSgpYY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782728575; c=relaxed/simple; bh=ZdGycji9S1R7gdVXe7T7IgoF6gKaJ2AxEdKBnjN27SQ=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=C2ISMP9lgE2JY09VAhl9Of5Uzu2Ng0YuvEnCACbwjWlr7WLxx+BCbv6Rndz+E7PjgvRv2I7sJo0QVeoywLxqAsnVKobe7lgQ2pZlIoOmI9D/nWifGLQXH31/RKPfpVF7K2U+tZ+KpIqw4KMTrSjmt8X453Q8FEtYCMOUY3AbYeM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fbg37ISe; 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="fbg37ISe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E3F141F000E9; Mon, 29 Jun 2026 10:22:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1782728574; bh=6kM8IHFXxn1BiZ37aNZDtKgU9blWWoTbWVsLQd54j+8=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=fbg37ISekOIFEy1JQ1fRpHf7tcIW5rV9KfXPBlKxkAFMXh0gIgtZsFrkV2ZccpSSk 8HYAsqNUIrEjvX5N1Sd+j+8QRiv8CgdX1+B8n/jNlRaDniCWW6TQVRVhmxHbFLDoh2 6aaobsUnoCc1OBwPgvtP0ywsmMTibCwONJDx2Esmci8GLOYzGUxgpAjugelDr9vCiA rh3SFVlD6HfzlmRRelf7+nmZbx+iIxVeHDaolUkTSZ+Gca/2fXpt26jm4aYzR6mRej vbUCdfqX1MKaYfhVoK5FTZbAzibcWN/lTioKAk8cIU5yEvvnBp2u8LMJpd4TIWKDMX 3zy7GDG8xYIyg== Date: Mon, 29 Jun 2026 11:22:43 +0100 From: Lorenzo Stoakes To: Xuewen Wang Cc: 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, david@kernel.org, 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> 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: <20260626053700.2036899-1-wangxuewen@kylinos.cn> On Fri, Jun 26, 2026 at 01:37:00PM +0800, 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 LGTM, so: Reviewed-by: Lorenzo Stoakes > --- > 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)) || > has_bh_in_lru(cpu, NULL); > } > > -- > 2.25.1 > Cheers, Lorenzo