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 AAD5F1EB5C2 for ; Sun, 28 Jun 2026 04:30:33 +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=1782621034; cv=none; b=tPXFpzyJ33tpmorBNEy34IwnKibe/o3hDB5QAU7NRNfIxHe6Rt4kuo8B6KUH34Oc99jTvhwEARfoB+cJKcEjJn5997XmswWnEdax0VPoThVSKuWpII+I00N386WWnPl/B3dh+R5uSfP9z0kbcYBG0FM2z7urbw3Rj3VICyW6Ypc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782621034; c=relaxed/simple; bh=0+UoIT7t1xLE9TqOFCVZ+IXwiWro7l2kkfbpJpNNVSk=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=u9+CsWJyqA52TqRnFI0uLxAHpAlbCOyz2CFOtDD+3AvbBYV0Q09SAqyYACFIgz1SAX/LcQFhDkWWlb4kc4g5WttG1oJ631sfm5eWMoxVBnEtYfi2GH9lDAs0vc1bZ0aIXSk/jLZkeBfC9iXZVAAMnkwrrsmvkRbm06EyoiFvp5c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=c+7YCt5A; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="c+7YCt5A" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6AF7D1F000E9; Sun, 28 Jun 2026 04:30:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1782621033; bh=oqgHl0lkQFdVKyA6jHTymGbQizShIgDETRUXN0bbAlA=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=c+7YCt5ApKBi+stQPHGgQADZucP/8AX/bKW9Nq/uTFRDzhYV0XdUXwhHH25G0NDSd +pn/ZHAQ6y/uer1MXI/qWUSaw+sAMAeo3zgyMO+4f3/VsXp4fpkuLaDok86YtMNOXK ulnyZ8Ufz7j/k+2Tb+ab1Tvev51k8tax8LtmrrSw= Date: Sat, 27 Jun 2026 21:30:31 -0700 From: Andrew Morton To: Hui Zhu Cc: David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Kairui Song , Qi Zheng , Shakeel Butt , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Hui Zhu Subject: Re: [PATCH v6] mm: assert exclusive nid/zonenum bits at the page/folio access sites Message-Id: <20260627213031.88e3bfb3489553afb1102399@linux-foundation.org> In-Reply-To: <20260626020629.1042041-1-hui.zhu@linux.dev> References: <20260626020629.1042041-1-hui.zhu@linux.dev> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) 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-Transfer-Encoding: 7bit On Fri, 26 Jun 2026 10:06:29 +0800 Hui Zhu wrote: > From: Hui Zhu > > KCSAN reports a data race between page_to_nid()/folio_pgdat() reading > page->flags and folio_trylock()/folio_lock() concurrently doing > test_and_set_bit_lock(PG_locked, ...) on the same word, e.g.: > > BUG: KCSAN: data-race in __lruvec_stat_mod_folio / shmem_get_folio_gfp > > The node id and zone id occupy fixed bit-ranges of page->flags that > are set once at page init and never modified afterwards, so they can > never overlap with the low PG_locked/PG_waiters bits touched by the > folio lock path. > > ASSERT_EXCLUSIVE_BITS(mdf.f, ...) inside memdesc_nid()/memdesc_zonenum() > used to check a by-value copy of the flags word, not the actual shared > page->flags/folio->flags being modified concurrently, so it didn't > reliably assert anything about the real race. > > For zonenum, move the assertion out of memdesc_zonenum() into > page_zonenum() and folio_zonenum(), where flags is dereferenced > directly from the page/folio. > > For nid, turn memdesc_nid() into a macro instead, so the mdf argument > is expanded as the caller's own flags expression > (PF_POISONED_CHECK(page)->flags or folio->flags) rather than copied > into a function parameter, letting ASSERT_EXCLUSIVE_BITS() check the > real page->flags/folio->flags directly. > > On CONFIG_NUMA=n, NODES_MASK is 0 and the old memdesc_nid() body > folded to a constant, so page->flags/folio->flags was never actually > read. ASSERT_EXCLUSIVE_BITS() is a real runtime check that can't be > folded away, so doing it unconditionally would add a pointless read > of page->flags/folio->flags and a check that can never fire. Keep > page_to_nid()/folio_nid() as plain "return 0" static inline stubs > under CONFIG_NUMA=n instead. > Thanks for persisting with this. ASSERT_EXCLUSIVE_BITS is defined in kcsan-checks.h and I'm not immediately seeing a reliable route by which mm.h and mmzone.h get to include that file. I don't think kasan.h gives that to us, surprisingly. Unless I've missed it, we're presently getting kcsan-checks.h thanks to good luck. Let's not rely upon good luck ;) Also, Sashiko is saying stuff again. Does it look legitimate? https://sashiko.dev/#/patchset/20260626020629.1042041-1-hui.zhu@linux.dev