* [PATCH] mm/thp: support mTHP in thp_underused()
@ 2026-07-15 6:21 Ye Liu
2026-07-15 6:42 ` Nico Pache
0 siblings, 1 reply; 4+ messages in thread
From: Ye Liu @ 2026-07-15 6:21 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Lorenzo Stoakes
Cc: Ye Liu, Zi Yan, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
linux-mm, linux-kernel
From: Ye Liu <liuye@kylinos.cn>
When khugepaged_max_ptes_none is configured to a non-default value,
scale it for non-PMD-mappable folios to match the logic in
collapse_max_ptes_none(), so mTHP can be classified as underused
instead of being silently skipped due to unscaled PMD-sized thresholds.
Signed-off-by: Ye Liu <liuye@kylinos.cn>
---
mm/huge_memory.c | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 25612af72dca..c642bec967fa 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -4489,24 +4489,29 @@ static unsigned long deferred_split_count(struct shrinker *shrink,
static bool thp_underused(struct folio *folio)
{
int num_zero_pages = 0, num_filled_pages = 0;
+ unsigned int max_ptes_none = khugepaged_max_ptes_none;
+ unsigned int folio_nr = folio_nr_pages(folio);
int i;
- if (khugepaged_max_ptes_none == HPAGE_PMD_NR - 1)
+ if (!folio_test_pmd_mappable(folio)) {
+ if (max_ptes_none == HPAGE_PMD_NR - 1)
+ max_ptes_none = folio_nr - 1;
+ else if (max_ptes_none)
+ return false;
+ }
+
+ if (max_ptes_none == folio_nr - 1)
return false;
if (folio_contain_hwpoisoned_page(folio))
return false;
- for (i = 0; i < folio_nr_pages(folio); i++) {
+ for (i = 0; i < folio_nr; i++) {
if (pages_identical(folio_page(folio, i), ZERO_PAGE(0))) {
- if (++num_zero_pages > khugepaged_max_ptes_none)
+ if (++num_zero_pages > max_ptes_none)
return true;
} else {
- /*
- * Another path for early exit once the number
- * of non-zero filled pages exceeds threshold.
- */
- if (++num_filled_pages >= HPAGE_PMD_NR - khugepaged_max_ptes_none)
+ if (++num_filled_pages >= folio_nr - max_ptes_none)
return false;
}
}
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] mm/thp: support mTHP in thp_underused()
2026-07-15 6:21 [PATCH] mm/thp: support mTHP in thp_underused() Ye Liu
@ 2026-07-15 6:42 ` Nico Pache
2026-07-15 7:35 ` Baolin Wang
0 siblings, 1 reply; 4+ messages in thread
From: Nico Pache @ 2026-07-15 6:42 UTC (permalink / raw)
To: Ye Liu
Cc: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, Ye Liu, Zi Yan,
Baolin Wang, Liam R. Howlett, Ryan Roberts, Dev Jain, Barry Song,
Lance Yang, Usama Arif, linux-mm, linux-kernel
On Wed, Jul 15, 2026 at 12:28 AM Ye Liu <ye.liu@linux.dev> wrote:
>
> From: Ye Liu <liuye@kylinos.cn>
>
> When khugepaged_max_ptes_none is configured to a non-default value,
> scale it for non-PMD-mappable folios to match the logic in
> collapse_max_ptes_none(), so mTHP can be classified as underused
> instead of being silently skipped due to unscaled PMD-sized thresholds.
IIUC we don't actually add non-PMD folios to the deferred split list
and only operate on fully mapped folios, so consequently, we never try
to shrink them with the underused shrinker.
Let me know if im missing something!
Cheers,
-- Nico
>
> Signed-off-by: Ye Liu <liuye@kylinos.cn>
> ---
> mm/huge_memory.c | 21 +++++++++++++--------
> 1 file changed, 13 insertions(+), 8 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 25612af72dca..c642bec967fa 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -4489,24 +4489,29 @@ static unsigned long deferred_split_count(struct shrinker *shrink,
> static bool thp_underused(struct folio *folio)
> {
> int num_zero_pages = 0, num_filled_pages = 0;
> + unsigned int max_ptes_none = khugepaged_max_ptes_none;
> + unsigned int folio_nr = folio_nr_pages(folio);
> int i;
>
> - if (khugepaged_max_ptes_none == HPAGE_PMD_NR - 1)
> + if (!folio_test_pmd_mappable(folio)) {
> + if (max_ptes_none == HPAGE_PMD_NR - 1)
> + max_ptes_none = folio_nr - 1;
> + else if (max_ptes_none)
> + return false;
> + }
> +
> + if (max_ptes_none == folio_nr - 1)
> return false;
>
> if (folio_contain_hwpoisoned_page(folio))
> return false;
>
> - for (i = 0; i < folio_nr_pages(folio); i++) {
> + for (i = 0; i < folio_nr; i++) {
> if (pages_identical(folio_page(folio, i), ZERO_PAGE(0))) {
> - if (++num_zero_pages > khugepaged_max_ptes_none)
> + if (++num_zero_pages > max_ptes_none)
> return true;
> } else {
> - /*
> - * Another path for early exit once the number
> - * of non-zero filled pages exceeds threshold.
> - */
> - if (++num_filled_pages >= HPAGE_PMD_NR - khugepaged_max_ptes_none)
> + if (++num_filled_pages >= folio_nr - max_ptes_none)
> return false;
> }
> }
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] mm/thp: support mTHP in thp_underused()
2026-07-15 6:42 ` Nico Pache
@ 2026-07-15 7:35 ` Baolin Wang
2026-07-15 8:22 ` Ye Liu
0 siblings, 1 reply; 4+ messages in thread
From: Baolin Wang @ 2026-07-15 7:35 UTC (permalink / raw)
To: Nico Pache, Ye Liu
Cc: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, Ye Liu, Zi Yan,
Liam R. Howlett, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, linux-mm, linux-kernel, joannelkoong
CC Joanne.
On 7/15/26 2:42 PM, Nico Pache wrote:
> On Wed, Jul 15, 2026 at 12:28 AM Ye Liu <ye.liu@linux.dev> wrote:
>>
>> From: Ye Liu <liuye@kylinos.cn>
>>
>> When khugepaged_max_ptes_none is configured to a non-default value,
>> scale it for non-PMD-mappable folios to match the logic in
>> collapse_max_ptes_none(), so mTHP can be classified as underused
>> instead of being silently skipped due to unscaled PMD-sized thresholds.
>
> IIUC we don't actually add non-PMD folios to the deferred split list
> and only operate on fully mapped folios, so consequently, we never try
> to shrink them with the underused shrinker.
Right. Joanne sent a similar patch set[1] to split underused mTHP
folios, but David said "needs more proper thought".
[1]
https://lore.kernel.org/all/20260707201735.4113107-1-joannelkoong@gmail.com/
>> Signed-off-by: Ye Liu <liuye@kylinos.cn>
>> ---
>> mm/huge_memory.c | 21 +++++++++++++--------
>> 1 file changed, 13 insertions(+), 8 deletions(-)
>>
>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
>> index 25612af72dca..c642bec967fa 100644
>> --- a/mm/huge_memory.c
>> +++ b/mm/huge_memory.c
>> @@ -4489,24 +4489,29 @@ static unsigned long deferred_split_count(struct shrinker *shrink,
>> static bool thp_underused(struct folio *folio)
>> {
>> int num_zero_pages = 0, num_filled_pages = 0;
>> + unsigned int max_ptes_none = khugepaged_max_ptes_none;
>> + unsigned int folio_nr = folio_nr_pages(folio);
>> int i;
>>
>> - if (khugepaged_max_ptes_none == HPAGE_PMD_NR - 1)
>> + if (!folio_test_pmd_mappable(folio)) {
>> + if (max_ptes_none == HPAGE_PMD_NR - 1)
>> + max_ptes_none = folio_nr - 1;
>> + else if (max_ptes_none)
>> + return false;
>> + }
>> +
>> + if (max_ptes_none == folio_nr - 1)
>> return false;
>>
>> if (folio_contain_hwpoisoned_page(folio))
>> return false;
>>
>> - for (i = 0; i < folio_nr_pages(folio); i++) {
>> + for (i = 0; i < folio_nr; i++) {
>> if (pages_identical(folio_page(folio, i), ZERO_PAGE(0))) {
>> - if (++num_zero_pages > khugepaged_max_ptes_none)
>> + if (++num_zero_pages > max_ptes_none)
>> return true;
>> } else {
>> - /*
>> - * Another path for early exit once the number
>> - * of non-zero filled pages exceeds threshold.
>> - */
>> - if (++num_filled_pages >= HPAGE_PMD_NR - khugepaged_max_ptes_none)
>> + if (++num_filled_pages >= folio_nr - max_ptes_none)
>> return false;
>> }
>> }
>> --
>> 2.43.0
>>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] mm/thp: support mTHP in thp_underused()
2026-07-15 7:35 ` Baolin Wang
@ 2026-07-15 8:22 ` Ye Liu
0 siblings, 0 replies; 4+ messages in thread
From: Ye Liu @ 2026-07-15 8:22 UTC (permalink / raw)
To: Baolin Wang, Nico Pache
Cc: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, Ye Liu, Zi Yan,
Liam R. Howlett, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, linux-mm, linux-kernel, joannelkoong
在 2026/7/15 15:35, Baolin Wang 写道:
> CC Joanne.
>
> On 7/15/26 2:42 PM, Nico Pache wrote:
>> On Wed, Jul 15, 2026 at 12:28 AM Ye Liu <ye.liu@linux.dev> wrote:
>>>
>>> From: Ye Liu <liuye@kylinos.cn>
>>>
>>> When khugepaged_max_ptes_none is configured to a non-default value,
>>> scale it for non-PMD-mappable folios to match the logic in
>>> collapse_max_ptes_none(), so mTHP can be classified as underused
>>> instead of being silently skipped due to unscaled PMD-sized thresholds.
>>
>> IIUC we don't actually add non-PMD folios to the deferred split list
>> and only operate on fully mapped folios, so consequently, we never try
>> to shrink them with the underused shrinker.
>
> Right. Joanne sent a similar patch set[1] to split underused mTHP folios, but David said "needs more proper thought".
>
Hi Nico,Baolin
Thanks for the review and to Baolin for providing the relevant information.
You're right – non‑PMD folios aren't on the deferred split list,
so this patch alone is pointless.
I'll drop it and continue to follow the discussions surrounding the Joanne
patch series instead. Sorry for the noise.
> [1] https://lore.kernel.org/all/20260707201735.4113107-1-joannelkoong@gmail.com/
>
>>> Signed-off-by: Ye Liu <liuye@kylinos.cn>
>>> ---
>>> mm/huge_memory.c | 21 +++++++++++++--------
>>> 1 file changed, 13 insertions(+), 8 deletions(-)
>>>
>>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
>>> index 25612af72dca..c642bec967fa 100644
>>> --- a/mm/huge_memory.c
>>> +++ b/mm/huge_memory.c
>>> @@ -4489,24 +4489,29 @@ static unsigned long deferred_split_count(struct shrinker *shrink,
>>> static bool thp_underused(struct folio *folio)
>>> {
>>> int num_zero_pages = 0, num_filled_pages = 0;
>>> + unsigned int max_ptes_none = khugepaged_max_ptes_none;
>>> + unsigned int folio_nr = folio_nr_pages(folio);
>>> int i;
>>>
>>> - if (khugepaged_max_ptes_none == HPAGE_PMD_NR - 1)
>>> + if (!folio_test_pmd_mappable(folio)) {
>>> + if (max_ptes_none == HPAGE_PMD_NR - 1)
>>> + max_ptes_none = folio_nr - 1;
>>> + else if (max_ptes_none)
>>> + return false;
>>> + }
>>> +
>>> + if (max_ptes_none == folio_nr - 1)
>>> return false;
>>>
>>> if (folio_contain_hwpoisoned_page(folio))
>>> return false;
>>>
>>> - for (i = 0; i < folio_nr_pages(folio); i++) {
>>> + for (i = 0; i < folio_nr; i++) {
>>> if (pages_identical(folio_page(folio, i), ZERO_PAGE(0))) {
>>> - if (++num_zero_pages > khugepaged_max_ptes_none)
>>> + if (++num_zero_pages > max_ptes_none)
>>> return true;
>>> } else {
>>> - /*
>>> - * Another path for early exit once the number
>>> - * of non-zero filled pages exceeds threshold.
>>> - */
>>> - if (++num_filled_pages >= HPAGE_PMD_NR - khugepaged_max_ptes_none)
>>> + if (++num_filled_pages >= folio_nr - max_ptes_none)
>>> return false;
>>> }
>>> }
>>> --
>>> 2.43.0
>>>
>
--
Thanks,
Ye Liu
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-07-15 8:22 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-15 6:21 [PATCH] mm/thp: support mTHP in thp_underused() Ye Liu
2026-07-15 6:42 ` Nico Pache
2026-07-15 7:35 ` Baolin Wang
2026-07-15 8:22 ` Ye Liu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox