From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A36B3606A333 for ; Mon, 30 Nov 2026 14:40:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=c+77tGNf Received: by kanga.kvack.org (Postfix) id C71E36B05B0; Mon, 30 Nov 2026 09:40:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C22586B05B1; Mon, 30 Nov 2026 09:40:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B38236B05B2; Mon, 30 Nov 2026 09:40:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 8CCAE6B05B0 for ; Mon, 30 Nov 2026 09:40:37 -0500 (EST) Received: from smtpin03.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0F656C0A99 for ; Mon, 30 Nov 2026 14:40:37 +0000 (UTC) X-FDA: 85492046514.03.C2CD2C8 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf06.hostedemail.com (Postfix) with ESMTP id 1F365180012 for ; Mon, 30 Nov 2026 14:40:34 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=c+77tGNf; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of ljs@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=ljs@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796049635; b=JrsJt+LLWsVVa8YeY15mOcUHeo8RUdtJMYvpXjQMuzOcg8YoxqrHrvy4fQQqJFQ463Dsw+ h5WYwGYmvMsYuzku+WIciCZ4Sze7d2AQ2PqsYo3x8m1gAebw9lS48rQX0NqRMn/OcKsUDV UQgGLtoGaU0VGNmU4l+Cb2+FtGYG4ss= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796049635; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ZmenU9AGsFP3zIu/rVkdgSFz9jHMTLj9C5Le2/n4wAM=; b=rgsUSLEaahMfM14WNVZd9l0q3aYEgElDdYpSIN81WHkzW328P7Xp5dgKW9twazGL4ME96l eIAJN6pail+0mSFt1J6JlTBMjXxKuqGQEqZgUkBSBlb9kBBhwGi1Hq51fYFogiOmjq8lQ0 H0wsbVzsPjUUZ6mSKeOE6q+jkn0h1zI= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=c+77tGNf; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of ljs@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=ljs@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 39F3142DFD; Mon, 30 Nov 2026 14:40:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 864DB1F000C1; Mon, 30 Nov 2026 14:40:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796049634; bh=ZmenU9AGsFP3zIu/rVkdgSFz9jHMTLj9C5Le2/n4wAM=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=c+77tGNfsdWFmkn71lvS51psTO3piGjggrryBLx5e+zHlbAzxzmskGNkhfloDV97W ljNItQC8H4bnUF+F48JhGVYC7p4UeboLDXJicyDRGtwxXZHs1vniTsNBWnFua3m7IQ lMMTcx6qsldfWq89SPg4oL2G+RtcB6k115Zg4JRi9EwFUC1f1C7IxJHRFds1tc/Eay nCHkW0cBh+Rejy+bHRK/pTZbnsEWj2B8ZUH2NeZzq9jxOrz2M+pu/qVgnEA9pDUSZk 2jrVkA54/yuXZ8ZTg5svH0gdB3YAQy/9P9ILEiyWpw4M0GAg5fg0PDfLjs0NiIYVEN w0Bh5B/fhEddQ== Date: Mon, 30 Nov 2026 14:40:27 +0000 From: "Lorenzo Stoakes (ARM)" To: "David Hildenbrand (Arm)" Cc: Baolin Wang , Zhenwen Liang , Zi Yan , Andrew Morton , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Kiryl Shutsemau , linux-mm@kvack.org Subject: Re: [PATCH v2] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load Message-ID: References: <20261129125223.1182465-1-zhenwenliang@google.com> <0f23727a-400f-44b6-8932-e06179b34ae6@linux.alibaba.com> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Stat-Signature: zyae995htasignrqghtn44uf8kojg3mw X-Rspamd-Queue-Id: 1F365180012 X-Rspam-User: X-Rspamd-Server: rspam03 X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796049634-621581 X-HE-Meta: U2FsdGVkX18jA4wGyFlEnb7cDYR7rXcTj8Ob3LGmdRYnMU7xfSjSMaYrHMN9mp2NBf4dJgi7yxRHrtln6tFReIu1RQrXwRl0V+FKvfKb9djBZD1572Q0FCU/g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8LNgljX00vbhCghP0H/wnSfrxCxzahfWCOdBkKqvI7gbHZoW0vc98UMa6tScCrnGrs/1J5zzFM1Ovb7hQr3dxQt1CfQIgxkB8XB6XUX3kFe5Si2YlKlOMgFzblRjvfF7RPOyLYXvWEy5qfXJDTylip7Zmk2osqnQma6br+8y6+0vZaCjMJmJwEcFQQ64xJ6iL/tAUFPYKDJWswoAFx05eOmCTmFMJDrI2AfaX+GoI/utMH4ExrBMzyWb8k2pGsdL+/LAs7GnMJ/qvOu7pks6UOcrTSWUNe6yEXk5/kwdhuhCaY3cGanerTkGNlApJxEmULvJQf/4CoK2OgLsSVB25mnkrIXj0pmAkZ0ZprDIDg+O Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Mon, Nov 30, 2026 at 01:23:44PM +0100, David Hildenbrand (Arm) wrote: > On 11/30/26 09:11, Baolin Wang wrote: > > > > > > On 11/29/26 8:52 PM, Zhenwen Liang wrote: > >> With CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n, mm_get_huge_zero_folio() tests > >> MMF_HUGE_ZERO_FOLIO with a plain test_bit() and then loads > >> huge_zero_folio, ordered only by a control dependency. A reader can see > >> the flag set by another thread of the same mm and still load NULL; the > >> fault then falls back to a normal page. Add smp_rmb(), paired with the > >> full barrier of mm_flags_test_and_set(). > > > > Yes, this is possible. But ... > > > >> > >> The problem was found, and this patch prepared, with the help of a > >> formal analysis tool we are developing that uses AI assistance; I have > >> reviewed the analysis and the patch. > >> > >> Fixes: 6fcb52a56ff6 ("thp: reduce usage of huge zero page's atomic counter") I am not sure it really matter that much. The window is tiny, and the outcome is something has smaller folios, it's not a big deal. I definitely wouldn't cc: stable anyway. > >> Cc: Lorenzo Stoakes > >> Signed-off-by: Zhenwen Liang > >> --- > >> v2: Shorten the changelog (David Hildenbrand). No code change. > >> v1: https://lore.kernel.org/r/20261129024728.282267-1-zhenwenliang@google.com/ > >> > >> mm/huge_memory.c | 5 ++++- > >> 1 file changed, 4 insertions(+), 1 deletion(-) > >> > >> diff --git a/mm/huge_memory.c b/mm/huge_memory.c > >> index 75e8641bf9ff..c31fc1c7d01e 100644 > >> --- a/mm/huge_memory.c > >> +++ b/mm/huge_memory.c > >> @@ -395,8 +395,11 @@ static void __init huge_zero_shrinker_exit(void) > >> > >> struct folio *mm_get_huge_zero_folio(struct mm_struct *mm) > >> { > >> - if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm)) > >> + if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm)) { > >> + /* Paired with the full barrier of mm_flags_test_and_set(). */ > >> + smp_rmb(); > >> return READ_ONCE(huge_zero_folio); > >> + } This whole thing is another byproduct of the mess that is the non-preallocated huge zero folio. How I hate it... Utterly cursed :) There was a significant bug with how all that was implemented that flummoxed many mm developers (because memory barriers always do that). I still feel like we should still default to that :) need to un-RFC my patch on that. Anyway I agree it's more cromulent to use an acquire test here. Thanks Baolin. > > > > Not a fan of adding a separate rmb() barrier. Why not use the acquire > > bit test? Something like: > > > > static inline bool mm_flags_test_acquire(int flag, const struct > > mm_struct *mm) > > { > > return test_bit_acquire(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags)); > > } > > That looks better indeed. Micht still want to document the corresponding barrier. More than might please :) Any use of the arcane arts (memory barriers) must specify what it is paired with, otherwise it becomes something that people are afraid to touch. But I think you don't want that in the mm_flags_test_acquire() function you want it at the caller. So something like: < ... add mm_flags_test_acquire() to mm.h ... > struct folio *mm_get_huge_zero_folio(struct mm_struct *mm) { + /* Paired with full barrier of mm_flags_test_and_set(). */ - if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm)) + if (mm_flags_test_acquire(MMF_HUGE_ZERO_FOLIO, mm)) return READ_ONCE(huge_zero_folio); if (!get_huge_zero_folio()) return NULL; if (mm_flags_test_and_set(MMF_HUGE_ZERO_FOLIO, mm)) put_huge_zero_folio(); return READ_ONCE(huge_zero_folio); } That version LGTM so feel free to add: Reviewed-by: Lorenzo Stoakes (ARM) To that! -- Cheers, Lorenzo