From nobody Fri Sep 25 21:41:32 2026 Received: from fout-a6-smtp.messagingengine.com (fout-a6-smtp.messagingengine.com [103.168.172.149]) (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 BDF685650F3 for ; Tue, 8 Sep 2026 13:28:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.149 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788874123; cv=none; b=JXvcTkuO3ner7WsC6kF5OpsGsbIhNmDiX/GLJcsbLoGrCa1LbhzqAft8NtByj7yEWKPq6UuIRK15ikqNFvChuiPo3QrAOnLM5CVekK0geKvGscCyR7/ccgVWsugRMGNIE7X9liu9bq6HwM4c8ejBhUMSLxL4uUx3pcTRNILaAL0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788874123; c=relaxed/simple; bh=SAct09KE3g4oxw9pZrvfjCez4EL74Dm3dxHgzwxyyLc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=XSOUzScFJDOvKWcbFiCkleRtMTuHq+9m4wIJLwkIHSxbgVjNEqB+/JIJhqNtROnUVZwUxIXd69e0Y8jCGEoWGtaPrv860F0n1EnsR1vWvajMNiVq+6FQbwiwHSuEINrhgMSkfSOQxmG5JTBEdHS4dNxtZps4MQ6Rpjdgtj3yeL0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name; spf=pass smtp.mailfrom=shutemov.name; dkim=pass (2048-bit key) header.d=shutemov.name header.i=@shutemov.name header.b=eRzvCtnI; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=JC68X4HO; arc=none smtp.client-ip=103.168.172.149 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=shutemov.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=shutemov.name header.i=@shutemov.name header.b="eRzvCtnI"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="JC68X4HO" Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfout.phl.internal (Postfix) with ESMTP id D6A67EC011C; Tue, 8 Sep 2026 09:28:25 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Tue, 08 Sep 2026 09:28:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov.name; h=cc:cc:content-transfer-encoding:content-type:date:date:from :from:in-reply-to:message-id:mime-version:reply-to:subject :subject:to:to; s=fm2; t=1788874105; x=1788960505; bh=XRGPzws2sb +9IlW6HXf+MYksDpUmRu+6YrbXkwnjRR4=; b=eRzvCtnIT9WujyoUL86zSn0Iiu +jtlY65TKWG6j8D1S1gxW4Cw7mFn+hVNMri9+i3yndrEzfLTmrX3bX6HVTGf9ekN MyKtYLHA1mPifWp7wUxfu7xV046blBlKgVYIFk958pA8wYSZGt1J462l6ag/kdpm aAdeQIszxfs7tYMd6nM5mz3xFZGslPzbRQ+hfxIVT2m0fpVCARY4zKJqT9aw2OnL lUaeBLDAj/rjeWjKNW+l4mans+y+BkZDlP071NMpYEcJry6oPk9oRoc2keUF/Q8I dstCCj8ASNZ+cfYz+ttOWPFbu/iql51DhasrbEVFCQUFAmHon8qvxvvWDCrQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:message-id:mime-version:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1788874105; x=1788960505; bh=XRGPzws2sb+9IlW6HXf+MYksDpUmRu+6Yrb XkwnjRR4=; b=JC68X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ME-Proxy: Feedback-ID: ie3994620:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 8 Sep 2026 09:28:23 -0400 (EDT) From: Kiryl Shutsemau To: Andrew Morton , David Hildenbrand , Zi Yan , Johannes Weiner , Lorenzo Stoakes , Baolin Wang , Balbir Singh Cc: Usama Arif , Lance Yang , "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Kairui Song , Hugh Dickins , Gladyshev Ilya , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, "Kiryl Shutsemau (Meta)" Subject: [PATCH v2] mm/huge_memory: add folio_reset_partially_mapped() Date: Tue, 8 Sep 2026 14:28:21 +0100 Message-ID: <20260908132821.1517475-1-kirill@shutemov.name> X-Mailer: git-send-email 2.55.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Kiryl Shutsemau (Meta)" __folio_unqueue_deferred_split() and __folio_freeze_and_split_unmapped() both clear PG_partially_mapped and take the folio out of MTHP_STAT_NR_ANON_PARTIALLY_MAPPED with the same five lines. Move the block into folio_reset_partially_mapped() and call it from both places. The helper asserts what both callers rely on: the folio is frozen, so deferred_split_folio() cannot set the flag again under it, and the folio is already off the deferred split queue. The list check sits behind the flag test because order-1 folios have no _deferred_list. folio_order() is safe to use at this point in the split process: it still shows the pre-split order. Suggested-by: David Hildenbrand (Arm) Assisted-by: LLM Acked-by: David Hildenbrand (Arm) Acked-by: Balbir Singh Reviewed-by: Zi Yan Reviewed-by: Baolin Wang Reviewed-by: Lorenzo Stoakes (ARM) Signed-off-by: Kiryl Shutsemau (Meta) --- v2: - Comment that the folio must be frozen above the refcount assert (Lorenzo) - Assisted-by tag in the format coding-assistants.rst asks for (Lorenzo) - Collect tags - Cc Ilya Gladyshev, whose refcount series would be the home for a folio_is_frozen() mm/huge_memory.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index dd66c6ad5af1..fae959633279 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3972,6 +3972,25 @@ static unsigned int folio_cache_ref_count(const stru= ct folio *folio) return folio_nr_pages(folio); } =20 +static void folio_reset_partially_mapped(struct folio *folio) +{ + /* Folio must be frozen. */ + VM_WARN_ON_FOLIO(folio_ref_count(folio), folio); + + if (!folio_test_partially_mapped(folio)) + return; + + /* + * Order-1 folios have no _deferred_list. The flag is only ever set + * on folios that do, so the list can be checked after the flag. + */ + VM_WARN_ON_FOLIO(!list_empty(&folio->_deferred_list), folio); + + folio_clear_partially_mapped(folio); + mod_mthp_stat(folio_order(folio), + MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1); +} + static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned= int new_order, struct page *split_at, struct xa_state *xas, struct address_space *mapping, bool do_lru, @@ -3980,7 +3999,6 @@ static int __folio_freeze_and_split_unmapped(struct f= olio *folio, unsigned int n { struct folio *end_folio =3D folio_next(folio); struct folio *new_folio, *next; - int old_order =3D folio_order(folio); int ret =3D 0; =20 VM_WARN_ON_ONCE(!mapping && end); @@ -3998,11 +4016,7 @@ static int __folio_freeze_and_split_unmapped(struct = folio *folio, unsigned int n * leaves PG_partially_mapped set. * Clear it here: the flag does not survive the split. */ - if (folio_test_partially_mapped(folio)) { - folio_clear_partially_mapped(folio); - mod_mthp_stat(old_order, - MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1); - } + folio_reset_partially_mapped(folio); =20 if (mapping) { int nr =3D folio_nr_pages(folio); @@ -4516,11 +4530,7 @@ bool __folio_unqueue_deferred_split(struct folio *fo= lio) memcg =3D folio_memcg(folio); lru =3D list_lru_lock_irqsave(&deferred_split_lru, nid, &memcg, &flags); if (__list_lru_del(&deferred_split_lru, lru, &folio->_deferred_list, nid)= ) { - if (folio_test_partially_mapped(folio)) { - folio_clear_partially_mapped(folio); - mod_mthp_stat(folio_order(folio), - MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1); - } + folio_reset_partially_mapped(folio); unqueued =3D true; } list_lru_unlock_irqrestore(lru, &flags); base-commit: e3fc12b08aadde9cec7b3799ac0e0c9a1aa245c4 --=20 2.54.0