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 BE7C2606A31C for ; Mon, 30 Nov 2026 16:06:23 +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=oa/4iv5/ Received: by kanga.kvack.org (Postfix) id D2CD46B05B0; Mon, 30 Nov 2026 11:06:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D04936B05B2; Mon, 30 Nov 2026 11:06:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C1A326B05B5; Mon, 30 Nov 2026 11:06:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 51F946B05B0 for ; Mon, 30 Nov 2026 11:06:21 -0500 (EST) Received: from smtpin10.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E0C22A0B39 for ; Mon, 30 Nov 2026 16:06:20 +0000 (UTC) X-FDA: 85492262520.10.613BB24 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf23.hostedemail.com (Postfix) with ESMTP id 38E64140017 for ; Mon, 30 Nov 2026 16:06:19 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b="oa/4iv5/"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf23.hostedemail.com: domain of ljs@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=ljs@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796054779; b=zwkqZIev8QjBOPhGEb52Flu24BeInbUB+fYmHLofjuOF/jMKcPfM+l5/vRLaZHLSA47J7W ZRJ7H8UcVSCssJh7ZEYSQ2WPp1w38R+pdHskC/vtF9ugBc6Bk5GY5K6pnR+/QWnKHfPjDd U8Olqje3IF9gNVZ67EKUbf7Pi+OMGS4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796054779; 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=vyTB+woMLMgPEQV2Aw8y+s8LzPSEswQjDJYG9/A9hfE=; b=T6shNJKCUkBDdKa7r3NLVhrNxSfgMTMUaB0HSS92mNcoVyY/Zxd4IzYMjdVCD+YQUPDDgq iYhjDfMWi/YQ+2z70J89NXBLraWdBUWGgupn0y8cIvenil5hY/L2e9wceHdnKBApOgtI/F Svhbjk0/Z+RcMrq5RqWy66vqfVhNLBs= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b="oa/4iv5/"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf23.hostedemail.com: domain of ljs@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=ljs@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id BDA2360F5F; Mon, 30 Nov 2026 16:06:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4057D1F00896; Mon, 30 Nov 2026 16:06:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796054778; bh=vyTB+woMLMgPEQV2Aw8y+s8LzPSEswQjDJYG9/A9hfE=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=oa/4iv5/I+LocJ49b7VB4G49S7Cdyt7dhSS8sjAd5BJxrXzJ6Z8wV7qKqrJ1eWujP 3X8Ento4EF9PAm+oHlHxsq20U7nYDqz5RY4KyDyFk9uxZeb97NyP6FQALsZDEGXlRZ hzYaX8Ka+vvw8QpZbheJJeoqwiykd/9bBnHyZYDlndIYvZjNBvBHobxUCG0/HVJWBn WULgg+KLsMJultQnT2Qxq0I3IB/NAqoAGq5FnET/aG5d+gBgpUl7TNxLr+4zSQU+tp cnb30eINVo+aQBXuzbCwDtuUWJR/x3DvgbXd8NgfYBMnqp6uQBwxidiZ3vG7cE5cxH NzueopRRWX3hQ== Date: Mon, 30 Nov 2026 16:06:11 +0000 From: "Lorenzo Stoakes (ARM)" To: Zhenwen Liang Cc: David Hildenbrand , Zi Yan , Andrew Morton , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Kiryl Shutsemau , linux-mm@kvack.org, Vlastimil Babka , Mike Rapoport , Michal Hocko Subject: Re: [PATCH v3] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load Message-ID: References: <20261130125301.211855-1-zhenwenliang@google.com> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261130125301.211855-1-zhenwenliang@google.com> X-Rspamd-Queue-Id: 38E64140017 X-Stat-Signature: wa911j1faze538qzpudhjhc1sfyr7rh3 X-Rspam-User: 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-Rspamd-Server: rspam07 X-HE-Tag: 1796054779-161737 X-HE-Meta: U2FsdGVkX19YHz52+iKXIPHTt6FUrspUMux4MzYcjLmwX1Ytil5t9z664f64evb135dmRuRTCu0LVqJDEVL6VVyF6b1YZGxD5+rn1bcIow3IolDDP27r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LxiC8Tgx+5zIPYMkRCrQjAATYRMUux3eTMMaJf9/xvy1R9NPonxPrzB+a8PUlC5uz1cx8Eqo5AOLs7eC7tw2rwvh7yhc0A3J5PS+DjBo2YpqBjc58QQh26BqDX+LAM9MzNLjEdVYO6lcX8/4mkidR6DqRS1tbaOLtb/LxRMxF54I/ISbFIGpAX4r9JxJfAE9D9t5BuqP1Ys0OV6zwXzTujGOBWqJ/VVC/NOV9A74DPB+TzyQMtUvBA1hDRutsb+qZeAyIJFeU6NZRhL/94/E/4SHxFPTfg2den4lAC2HLrEaaPvjpkHeOl8S+596f+c4G7OHfim/QkQLeHoGotj3rYjX9AOpYHVX7rnfN5XsrbZA4Ia 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 12:53:01PM +0000, 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 mm_flags_test_acquire() and > use it for this test; it pairs with the full barrier of > mm_flags_test_and_set(). > > Compile-tested only. > > 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") > Cc: Lorenzo Stoakes > Signed-off-by: Zhenwen Liang Little annoyed to have wasted my time replying to v2 only to see there's a v3 already, and also already taken, but anyway. FWIW: Reviewed-by: Lorenzo Stoakes (ARM) > --- > v3: Use an acquire bit test instead of smp_rmb() (Baolin Wang, David > Hildenbrand); document the pairing barrier (David Hildenbrand, Zi Yan). > Dropped Zi Yan's Reviewed-by because the code changed. > v2: https://lore.kernel.org/r/20261129125223.1182465-1-zhenwenliang@google.com/ > > include/linux/mm.h | 5 +++++ > mm/huge_memory.c | 6 +++++- > 2 files changed, 10 insertions(+), 1 deletion(-) > > diff --git a/include/linux/mm.h b/include/linux/mm.h > index 040cff64381b..e26de2ec7b0d 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -929,6 +929,11 @@ static inline bool mm_flags_test(int flag, const struct mm_struct *mm) > return test_bit(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags)); > } > > +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)); > +} > + The headers make life difficult with 'stuff you own', at least ostensibly. mm.h is in MM - CORE, but a ton of VMA flag and mm flag and etc. stuff is there. I am only aware of this API effectively changing because I happened to be cc-d :) I wonder if we shouldn't split headers out a bit for this kind of thing, assuming it doesn't somehow make the header bloat thing even worse. > static inline bool mm_flags_test_and_set(int flag, struct mm_struct *mm) > { > return test_and_set_bit(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags)); > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index d065b26a94df..139471a861ed 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -395,7 +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)) > + /* > + * Paired with the full barrier of mm_flags_test_and_set(). > + * The flag implies huge_zero_folio is visible. This line seems redundant. But not the end of the world given it's taken already... > + */ > + if (mm_flags_test_acquire(MMF_HUGE_ZERO_FOLIO, mm)) > return READ_ONCE(huge_zero_folio); > > if (!get_huge_zero_folio()) > -- > 2.56.1.839.g22a8290db9-goog > -- Cheers, Lorenzo