[PATCH v13 mm-new 05/16] khugepaged: introduce is_mthp_order helper

Nico Pache posted 16 patches 2 months, 1 week ago
There is a newer version of this series
[PATCH v13 mm-new 05/16] khugepaged: introduce is_mthp_order helper
Posted by Nico Pache 2 months, 1 week ago
In order to add mTHP support, we will often be checking if a given order
is a mTHP or PMD order. Lets create a simple helper function to keep the
code clean and readable.

Suggested-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Signed-off-by: Nico Pache <npache@redhat.com>
---
 mm/khugepaged.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 8599c7fa112e..9c041141b2e3 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -338,6 +338,11 @@ static bool pte_none_or_zero(pte_t pte)
 	return pte_present(pte) && is_zero_pfn(pte_pfn(pte));
 }
 
+static bool is_mthp_order(unsigned int order)
+{
+	return order != HPAGE_PMD_ORDER;
+}
+
 int hugepage_madvise(struct vm_area_struct *vma,
 		     vm_flags_t *vm_flags, int advice)
 {
@@ -1071,13 +1076,13 @@ static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm,
 	folio = __folio_alloc(gfp, order, node, &cc->alloc_nmask);
 	if (!folio) {
 		*foliop = NULL;
-		if (order == HPAGE_PMD_ORDER)
+		if (!is_mthp_order(order))
 			count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
 		count_mthp_stat(order, MTHP_STAT_COLLAPSE_ALLOC_FAILED);
 		return SCAN_ALLOC_HUGE_PAGE_FAIL;
 	}
 
-	if (order == HPAGE_PMD_ORDER)
+	if (!is_mthp_order(order))
 		count_vm_event(THP_COLLAPSE_ALLOC);
 	count_mthp_stat(order, MTHP_STAT_COLLAPSE_ALLOC);
 
-- 
2.51.1
Re: [PATCH v13 mm-new 05/16] khugepaged: introduce is_mthp_order helper
Posted by Lorenzo Stoakes 1 month ago
On Mon, Dec 01, 2025 at 10:46:16AM -0700, Nico Pache wrote:
> In order to add mTHP support, we will often be checking if a given order
> is a mTHP or PMD order. Lets create a simple helper function to keep the
> code clean and readable.
>
> Suggested-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> Signed-off-by: Nico Pache <npache@redhat.com>

Notwithstanding what Zi said re: using elsewhere (please do :), LGTM so:

Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

> ---
>  mm/khugepaged.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 8599c7fa112e..9c041141b2e3 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -338,6 +338,11 @@ static bool pte_none_or_zero(pte_t pte)
>  	return pte_present(pte) && is_zero_pfn(pte_pfn(pte));
>  }
>
> +static bool is_mthp_order(unsigned int order)
> +{
> +	return order != HPAGE_PMD_ORDER;
> +}
> +
>  int hugepage_madvise(struct vm_area_struct *vma,
>  		     vm_flags_t *vm_flags, int advice)
>  {
> @@ -1071,13 +1076,13 @@ static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm,
>  	folio = __folio_alloc(gfp, order, node, &cc->alloc_nmask);
>  	if (!folio) {
>  		*foliop = NULL;
> -		if (order == HPAGE_PMD_ORDER)
> +		if (!is_mthp_order(order))
>  			count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
>  		count_mthp_stat(order, MTHP_STAT_COLLAPSE_ALLOC_FAILED);
>  		return SCAN_ALLOC_HUGE_PAGE_FAIL;
>  	}
>
> -	if (order == HPAGE_PMD_ORDER)
> +	if (!is_mthp_order(order))
>  		count_vm_event(THP_COLLAPSE_ALLOC);
>  	count_mthp_stat(order, MTHP_STAT_COLLAPSE_ALLOC);
>
> --
> 2.51.1
>
Re: [PATCH v13 mm-new 05/16] khugepaged: introduce is_mthp_order helper
Posted by Zi Yan 2 months, 1 week ago
On 1 Dec 2025, at 12:46, Nico Pache wrote:

> In order to add mTHP support, we will often be checking if a given order
> is a mTHP or PMD order. Lets create a simple helper function to keep the
> code clean and readable.
>
> Suggested-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> Signed-off-by: Nico Pache <npache@redhat.com>
> ---
>  mm/khugepaged.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 8599c7fa112e..9c041141b2e3 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -338,6 +338,11 @@ static bool pte_none_or_zero(pte_t pte)
>  	return pte_present(pte) && is_zero_pfn(pte_pfn(pte));
>  }
>
> +static bool is_mthp_order(unsigned int order)
> +{
> +	return order != HPAGE_PMD_ORDER;
> +}
> +

The code at the bottom of __folio_split() in mm/huge_memory.c
can use this helper.

The code in deferred_split_folio() uses folio_test_pmd_mappable()
instead of order == HPAGE_PMD_ORDER, but the code can be changed
to use the helper.

>  int hugepage_madvise(struct vm_area_struct *vma,
>  		     vm_flags_t *vm_flags, int advice)
>  {
> @@ -1071,13 +1076,13 @@ static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm,
>  	folio = __folio_alloc(gfp, order, node, &cc->alloc_nmask);
>  	if (!folio) {
>  		*foliop = NULL;
> -		if (order == HPAGE_PMD_ORDER)
> +		if (!is_mthp_order(order))
>  			count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
>  		count_mthp_stat(order, MTHP_STAT_COLLAPSE_ALLOC_FAILED);
>  		return SCAN_ALLOC_HUGE_PAGE_FAIL;
>  	}
>
> -	if (order == HPAGE_PMD_ORDER)
> +	if (!is_mthp_order(order))
>  		count_vm_event(THP_COLLAPSE_ALLOC);
>  	count_mthp_stat(order, MTHP_STAT_COLLAPSE_ALLOC);
>
> -- 
> 2.51.1


Best Regards,
Yan, Zi
Re: [PATCH v13 mm-new 05/16] khugepaged: introduce is_mthp_order helper
Posted by Nico Pache 3 weeks, 5 days ago
On Tue, Dec 2, 2025 at 8:13 PM Zi Yan <ziy@nvidia.com> wrote:
>
> On 1 Dec 2025, at 12:46, Nico Pache wrote:
>
> > In order to add mTHP support, we will often be checking if a given order
> > is a mTHP or PMD order. Lets create a simple helper function to keep the
> > code clean and readable.
> >
> > Suggested-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> > Signed-off-by: Nico Pache <npache@redhat.com>
> > ---
> >  mm/khugepaged.c | 9 +++++++--
> >  1 file changed, 7 insertions(+), 2 deletions(-)
> >
> > diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> > index 8599c7fa112e..9c041141b2e3 100644
> > --- a/mm/khugepaged.c
> > +++ b/mm/khugepaged.c
> > @@ -338,6 +338,11 @@ static bool pte_none_or_zero(pte_t pte)
> >       return pte_present(pte) && is_zero_pfn(pte_pfn(pte));
> >  }
> >
> > +static bool is_mthp_order(unsigned int order)
> > +{
> > +     return order != HPAGE_PMD_ORDER;
> > +}
> > +
>
> The code at the bottom of __folio_split() in mm/huge_memory.c
> can use this helper.
>
> The code in deferred_split_folio() uses folio_test_pmd_mappable()
> instead of order == HPAGE_PMD_ORDER, but the code can be changed
> to use the helper.

static inline bool folio_test_pmd_mappable(struct folio *folio)
{
return folio_order(folio) >= HPAGE_PMD_ORDER;
}

Should we make the is_mthp_order() check all orders that are less than
HPAGE_PMD_ORDER.

ie)

+static bool is_mthp_order(unsigned int order)
+{
+     return order < HPAGE_PMD_ORDER;
}

This way it's true to its mTHP nature being smaller than the PMD.

Cheers,
-- Nico

>
> >  int hugepage_madvise(struct vm_area_struct *vma,
> >                    vm_flags_t *vm_flags, int advice)
> >  {
> > @@ -1071,13 +1076,13 @@ static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm,
> >       folio = __folio_alloc(gfp, order, node, &cc->alloc_nmask);
> >       if (!folio) {
> >               *foliop = NULL;
> > -             if (order == HPAGE_PMD_ORDER)
> > +             if (!is_mthp_order(order))
> >                       count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
> >               count_mthp_stat(order, MTHP_STAT_COLLAPSE_ALLOC_FAILED);
> >               return SCAN_ALLOC_HUGE_PAGE_FAIL;
> >       }
> >
> > -     if (order == HPAGE_PMD_ORDER)
> > +     if (!is_mthp_order(order))
> >               count_vm_event(THP_COLLAPSE_ALLOC);
> >       count_mthp_stat(order, MTHP_STAT_COLLAPSE_ALLOC);
> >
> > --
> > 2.51.1
>
>
> Best Regards,
> Yan, Zi
>
Re: [PATCH v13 mm-new 05/16] khugepaged: introduce is_mthp_order helper
Posted by Zi Yan 3 weeks, 5 days ago
On 13 Jan 2026, at 21:38, Nico Pache wrote:

> On Tue, Dec 2, 2025 at 8:13 PM Zi Yan <ziy@nvidia.com> wrote:
>>
>> On 1 Dec 2025, at 12:46, Nico Pache wrote:
>>
>>> In order to add mTHP support, we will often be checking if a given order
>>> is a mTHP or PMD order. Lets create a simple helper function to keep the
>>> code clean and readable.
>>>
>>> Suggested-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
>>> Signed-off-by: Nico Pache <npache@redhat.com>
>>> ---
>>>  mm/khugepaged.c | 9 +++++++--
>>>  1 file changed, 7 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
>>> index 8599c7fa112e..9c041141b2e3 100644
>>> --- a/mm/khugepaged.c
>>> +++ b/mm/khugepaged.c
>>> @@ -338,6 +338,11 @@ static bool pte_none_or_zero(pte_t pte)
>>>       return pte_present(pte) && is_zero_pfn(pte_pfn(pte));
>>>  }
>>>
>>> +static bool is_mthp_order(unsigned int order)
>>> +{
>>> +     return order != HPAGE_PMD_ORDER;
>>> +}
>>> +
>>
>> The code at the bottom of __folio_split() in mm/huge_memory.c
>> can use this helper.
>>
>> The code in deferred_split_folio() uses folio_test_pmd_mappable()
>> instead of order == HPAGE_PMD_ORDER, but the code can be changed
>> to use the helper.
>
> static inline bool folio_test_pmd_mappable(struct folio *folio)
> {
> return folio_order(folio) >= HPAGE_PMD_ORDER;
> }
>
> Should we make the is_mthp_order() check all orders that are less than
> HPAGE_PMD_ORDER.
>
> ie)
>
> +static bool is_mthp_order(unsigned int order)
> +{
> +     return order < HPAGE_PMD_ORDER;
> }
>
> This way it's true to its mTHP nature being smaller than the PMD.

I am not sure. It really depends on in the future whether we call
> 9 order (can be mapped by multiple PMDs) large folio mTHP or not.
My current feeling is that THP is a legacy name for PMD THP and
any other large folios are mTHP. So your current definition of
is_mthp_order() looks right to me. This is just my feeling and
I am open to other opinions.

folio_test_pmd_mappable() is different, since as long as a folio's
order is greater than HPAGE_PMD_ORDER, it indeed can be mapped
by PMDs.

--
Best Regards,
Yan, Zi