* Re: [PATCH] mm/huge_memory: add folio_reset_partially_mapped()
2026-09-07 18:33 [PATCH] mm/huge_memory: add folio_reset_partially_mapped() Kiryl Shutsemau
@ 2026-09-07 19:46 ` Zi Yan
2026-09-07 19:57 ` David Hildenbrand (Arm)
` (3 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Zi Yan @ 2026-09-07 19:46 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, hannes
Cc: ljs, usama.arif, lance.yang, baolin.wang, liam, nico.pache,
ryan.roberts, dev.jain, baohua, kasong, hughd, balbirs, linux-mm,
linux-kernel, kernel-team, Kiryl Shutsemau (Meta)
On Mon Sep 7, 2026 at 2:33 PM EDT, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> __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) <david@kernel.org>
> Assisted-by: Claude-Code:claude-fable-5-1
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
> mm/huge_memory.c | 31 ++++++++++++++++++++-----------
> 1 file changed, 20 insertions(+), 11 deletions(-)
>
LGTM.
Reviewed-by: Zi Yan <ziy@nvidia.com>
--
Best Regards,
Yan, Zi
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] mm/huge_memory: add folio_reset_partially_mapped()
2026-09-07 18:33 [PATCH] mm/huge_memory: add folio_reset_partially_mapped() Kiryl Shutsemau
2026-09-07 19:46 ` Zi Yan
@ 2026-09-07 19:57 ` David Hildenbrand (Arm)
2026-09-08 6:06 ` Baolin Wang
` (2 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: David Hildenbrand (Arm) @ 2026-09-07 19:57 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, ziy, hannes
Cc: ljs, usama.arif, lance.yang, baolin.wang, liam, nico.pache,
ryan.roberts, dev.jain, baohua, kasong, hughd, balbirs, linux-mm,
linux-kernel, kernel-team, Kiryl Shutsemau (Meta)
On 9/7/26 20:33, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> __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) <david@kernel.org>
> Assisted-by: Claude-Code:claude-fable-5-1
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
Thanks!
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] mm/huge_memory: add folio_reset_partially_mapped()
2026-09-07 18:33 [PATCH] mm/huge_memory: add folio_reset_partially_mapped() Kiryl Shutsemau
2026-09-07 19:46 ` Zi Yan
2026-09-07 19:57 ` David Hildenbrand (Arm)
@ 2026-09-08 6:06 ` Baolin Wang
2026-09-08 9:25 ` Lorenzo Stoakes (ARM)
2026-09-08 11:12 ` Balbir Singh
4 siblings, 0 replies; 8+ messages in thread
From: Baolin Wang @ 2026-09-08 6:06 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, ziy, hannes
Cc: ljs, usama.arif, lance.yang, liam, nico.pache, ryan.roberts,
dev.jain, baohua, kasong, hughd, balbirs, linux-mm, linux-kernel,
kernel-team, Kiryl Shutsemau (Meta)
On 9/8/26 2:33 AM, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> __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) <david@kernel.org>
> Assisted-by: Claude-Code:claude-fable-5-1
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
LGTM.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] mm/huge_memory: add folio_reset_partially_mapped()
2026-09-07 18:33 [PATCH] mm/huge_memory: add folio_reset_partially_mapped() Kiryl Shutsemau
` (2 preceding siblings ...)
2026-09-08 6:06 ` Baolin Wang
@ 2026-09-08 9:25 ` Lorenzo Stoakes (ARM)
2026-09-08 13:09 ` Kiryl Shutsemau
2026-09-08 11:12 ` Balbir Singh
4 siblings, 1 reply; 8+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-09-08 9:25 UTC (permalink / raw)
To: Kiryl Shutsemau
Cc: akpm, david, ziy, hannes, usama.arif, lance.yang, baolin.wang,
liam, nico.pache, ryan.roberts, dev.jain, baohua, kasong, hughd,
balbirs, linux-mm, linux-kernel, kernel-team,
Kiryl Shutsemau (Meta)
On Mon, Sep 07, 2026 at 07:33:40PM +0100, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> __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) <david@kernel.org>
> Assisted-by: Claude-Code:claude-fable-5-1
Note that policy has changed on Assisted-by tags so this should be:
Assisted-by: LLM
https://docs.kernel.org/process/coding-assistants.html
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
One nit above and below, with those addressed LGTM, so:
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> ---
> mm/huge_memory.c | 31 ++++++++++++++++++++-----------
> 1 file changed, 20 insertions(+), 11 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index dd66c6ad5af1..23ef22c2b5db 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -3972,6 +3972,24 @@ static unsigned int folio_cache_ref_count(const struct folio *folio)
> return folio_nr_pages(folio);
> }
>
> +static void folio_reset_partially_mapped(struct folio *folio)
> +{
> + VM_WARN_ON_FOLIO(folio_ref_count(folio), folio);
NIT: I wonder if we could have a folio_is_frozen() that'd make this
self-documenting? As trivial as it'd be :)
If not, then a comment like:
/* Folio must be frozen. */
Would be helpful.
> +
> + 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 +3998,6 @@ static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int n
> {
> struct folio *end_folio = folio_next(folio);
> struct folio *new_folio, *next;
> - int old_order = folio_order(folio);
> int ret = 0;
>
> VM_WARN_ON_ONCE(!mapping && end);
> @@ -3998,11 +4015,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);
>
> if (mapping) {
> int nr = folio_nr_pages(folio);
> @@ -4516,11 +4529,7 @@ bool __folio_unqueue_deferred_split(struct folio *folio)
> memcg = folio_memcg(folio);
> lru = 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);
Nice bit of red :)
> unqueued = true;
> }
> list_lru_unlock_irqrestore(lru, &flags);
>
> base-commit: e3fc12b08aadde9cec7b3799ac0e0c9a1aa245c4
> --
> 2.54.0
>
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] mm/huge_memory: add folio_reset_partially_mapped()
2026-09-08 9:25 ` Lorenzo Stoakes (ARM)
@ 2026-09-08 13:09 ` Kiryl Shutsemau
2026-09-08 13:33 ` Ilya Gladyshev
0 siblings, 1 reply; 8+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 13:09 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM), Gladyshev Ilya
Cc: akpm, david, ziy, hannes, usama.arif, lance.yang, baolin.wang,
liam, nico.pache, ryan.roberts, dev.jain, baohua, kasong, hughd,
balbirs, linux-mm, linux-kernel, kernel-team
On Tue, Sep 08, 2026 at 10:25:07AM +0100, Lorenzo Stoakes (ARM) wrote:
> On Mon, Sep 07, 2026 at 07:33:40PM +0100, Kiryl Shutsemau wrote:
> > Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
> > Assisted-by: Claude-Code:claude-fable-5-1
>
> Note that policy has changed on Assisted-by tags so this should be:
>
> Assisted-by: LLM
I was not aware. Thanks.
Will fix in v2.
> > +static void folio_reset_partially_mapped(struct folio *folio)
> > +{
> > + VM_WARN_ON_FOLIO(folio_ref_count(folio), folio);
>
> NIT: I wonder if we could have a folio_is_frozen() that'd make this
> self-documenting? As trivial as it'd be :)
>
> If not, then a comment like:
>
> /* Folio must be frozen. */
>
> Would be helpful.
There is a series that changes how frozen is encoded and would be the
natural home for such a helper:
https://lore.kernel.org/all/bb1cb750157733f897df2e261cad3d5c42acc172@linux.dev/
The patchset seems to be stuck. Not sure what the plan there. Ilya?
I will go with the comment for v2.
--
Kiryl Shutsemau / Kirill A. Shutemov
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] mm/huge_memory: add folio_reset_partially_mapped()
2026-09-08 13:09 ` Kiryl Shutsemau
@ 2026-09-08 13:33 ` Ilya Gladyshev
0 siblings, 0 replies; 8+ messages in thread
From: Ilya Gladyshev @ 2026-09-08 13:33 UTC (permalink / raw)
To: Kiryl Shutsemau, Lorenzo Stoakes (ARM)
Cc: akpm, david, ziy, hannes, usama.arif, lance.yang, baolin.wang,
liam, nico.pache, ryan.roberts, dev.jain, baohua, kasong, hughd,
balbirs, linux-mm, linux-kernel, kernel-team
On 9/8/26 16:09, Kiryl Shutsemau wrote:
>>> +static void folio_reset_partially_mapped(struct folio *folio)
>>> +{
>>> + VM_WARN_ON_FOLIO(folio_ref_count(folio), folio);
>>
>> NIT: I wonder if we could have a folio_is_frozen() that'd make this
>> self-documenting? As trivial as it'd be :)
>>
>> If not, then a comment like:
>>
>> /* Folio must be frozen. */
>>
>> Would be helpful.
> > There is a series that changes how frozen is encoded and would be the
> natural home for such a helper:
> > https://lore.kernel.org/all/bb1cb750157733f897df2e261cad3d5c42acc172@linux.dev/
> > The patchset seems to be stuck. Not sure what the plan there. Ilya?
JFYI: I restarted my work on this patchset this week and plan to send
a new revision in the next couple of days. There were no problems with
it, except for minor issues with the debug WARN_ONs, so I hope I will
succeed with this plan.
---
Ilya Gladyshev // foxido.dev
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] mm/huge_memory: add folio_reset_partially_mapped()
2026-09-07 18:33 [PATCH] mm/huge_memory: add folio_reset_partially_mapped() Kiryl Shutsemau
` (3 preceding siblings ...)
2026-09-08 9:25 ` Lorenzo Stoakes (ARM)
@ 2026-09-08 11:12 ` Balbir Singh
4 siblings, 0 replies; 8+ messages in thread
From: Balbir Singh @ 2026-09-08 11:12 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, ziy, hannes
Cc: ljs, usama.arif, lance.yang, baolin.wang, liam, nico.pache,
ryan.roberts, dev.jain, baohua, kasong, hughd, linux-mm,
linux-kernel, kernel-team, Kiryl Shutsemau (Meta)
On 9/8/26 4:33 AM, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> __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) <david@kernel.org>
> Assisted-by: Claude-Code:claude-fable-5-1
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
> mm/huge_memory.c | 31 ++++++++++++++++++++-----------
> 1 file changed, 20 insertions(+), 11 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index dd66c6ad5af1..23ef22c2b5db 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -3972,6 +3972,24 @@ static unsigned int folio_cache_ref_count(const struct folio *folio)
> return folio_nr_pages(folio);
> }
>
> +static void folio_reset_partially_mapped(struct folio *folio)
> +{
> + 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 +3998,6 @@ static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int n
> {
> struct folio *end_folio = folio_next(folio);
> struct folio *new_folio, *next;
> - int old_order = folio_order(folio);
> int ret = 0;
>
> VM_WARN_ON_ONCE(!mapping && end);
> @@ -3998,11 +4015,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);
>
> if (mapping) {
> int nr = folio_nr_pages(folio);
> @@ -4516,11 +4529,7 @@ bool __folio_unqueue_deferred_split(struct folio *folio)
> memcg = folio_memcg(folio);
> lru = 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 = true;
> }
> list_lru_unlock_irqrestore(lru, &flags);
>
> base-commit: e3fc12b08aadde9cec7b3799ac0e0c9a1aa245c4
Makes sense
Acked-by: Balbir Singh <balbirs@nvidia.com>
^ permalink raw reply [flat|nested] 8+ messages in thread