From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 570893D3334 for ; Fri, 26 Jun 2026 08:32:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782462742; cv=none; b=jUlrk8XJQ5K3K6UX2DtxMGk2X1/sMsoJErfY35As/pafXf5amWmXUYyGlep+7kicUbYh4yEOX0TqCSNzm+1NRds27gxukD3w6nIbYPDqEj/pnfACAIdHihXRp87odgzn46R9cwuwVOZmYcMxFIWeZbvqbuk6KC/hw8MwORWYZHw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782462742; c=relaxed/simple; bh=xA9z+b0MWSkEUifo8U4BY7TBUU9Pav09Cu4Pl+Fz6s4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=hTeHvD0y851L7bl/YEL/x7AMLtk+qTOmQchNd4atYAXOaOLNze3N8tnH4nrnF+9jKmV7IwcGVqmXSZnGmqWhN+nf0tADtYUhtFc2q12IGPgOLB7p3rKHpOpSkJI65JDk3wSqjAFYnW6w/ilusD82w5n6HckcRBXf2n8HXNTEbVg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=nzjs7Vqe; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=JjKks+Rd; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=Mmi38bNV; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=hs/AdF2p; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="nzjs7Vqe"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="JjKks+Rd"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="Mmi38bNV"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="hs/AdF2p" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 2A2E67174E; Fri, 26 Jun 2026 08:32:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1782462739; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Xocu5qnTvy4+IFzcFqjfxD+zsBo6OI2gkaPHHHV94dk=; b=nzjs7Vqe94IwEmHT+dpEQBNiIks4/hsA/hpreRbClj4j+33JXW3OmJ6S94mB6p8a465S9w t/eTSOJc5zD7Sn2HGmsbvKt7hAu/8nEKKsQnYTk6M0i0/51AwAoyjGjziAXgcfG1VlIDYC ZFTJBpsUzIKJor+3pY7JGIP7mvm21qI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1782462739; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Xocu5qnTvy4+IFzcFqjfxD+zsBo6OI2gkaPHHHV94dk=; b=JjKks+Rds1p2WD+iQdm/y8/FA08HVAFej/MfOytgUxBXct2vs9mS//lbCKjSwMll7akmHK j8Fu4JIgn+lYWeCA== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=Mmi38bNV; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b="hs/AdF2p" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1782462738; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Xocu5qnTvy4+IFzcFqjfxD+zsBo6OI2gkaPHHHV94dk=; b=Mmi38bNVsdAh2FJMF/9bCwDYWrp0u+w2D8zLakjUnNKzdvXz3dzZXVlWK8YrFJAbZB7U6k k7kWhxbjWNPu9x8QyttR7dw9n9v+AzBV80/lvjZtPSIPsM58/nHRKi3W5W92Pxm648JCld E8dSpVCn3FjHBgV/gngv8Tfyr7eje+U= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1782462738; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Xocu5qnTvy4+IFzcFqjfxD+zsBo6OI2gkaPHHHV94dk=; b=hs/AdF2pbreD5/ACcd0Wmo1BJyupq6O+TuwXx3uFBrgPyas0CVywtwjxQNUkiRoLmfpr4v sYebFwGDY7jBXvAg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id C34F4779A8; Fri, 26 Jun 2026 08:32:16 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 5iNOLBA5Pmo2DAAAD6G6ig (envelope-from ); Fri, 26 Jun 2026 08:32:16 +0000 Date: Fri, 26 Jun 2026 09:32:15 +0100 From: Pedro Falcato To: Xuewen Wang Cc: akpm@linux-foundation.org, liam@infradead.org, ljs@kernel.org, vbabka@kernel.org, jannh@google.com, 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> X-Spam-Flag: NO X-Rspamd-Action: no action X-Spam-Level: X-Spamd-Result: default: False [-4.51 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; RCPT_COUNT_TWELVE(0.00)[21]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; FUZZY_RATELIMITED(0.00)[rspamd.com]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; RCVD_TLS_ALL(0.00)[]; TO_DN_SOME(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; FREEMAIL_CC(0.00)[linux-foundation.org,infradead.org,kernel.org,google.com,tencent.com,huaweicloud.com,gmail.com,linux.dev,lge.com,kvack.org,vger.kernel.org]; DNSWL_BLOCKED(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RCVD_VIA_SMTP_AUTH(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; DKIM_TRACE(0.00)[suse.de:+]; MISSING_XM_UA(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[pedro-suse.lan:mid,imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,kylinos.cn:email,suse.de:email,suse.de:dkim] X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Queue-Id: 2A2E67174E X-Spam-Score: -4.51 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 Reviewed-by: Pedro Falcato > --- > 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 > -- Pedro