On 6/10/26 4:29 AM, Baolin Wang wrote:
> Similar to the anonymous folios collapse, we should also check the
> 'max_ptes_none' when trying to collapse shmem/file folios, which is
> also intended as preparation for shmem mTHP collapse in the
> following patches.
> 
> Signed-off-by: Baolin Wang <[email protected]>

Makes sense! There is the worry that we might have to move this into one of the
helper functions I plan on introducing for collapse_file(). This function is
very long and messy and needs some refactoring... Working on that.

Reviewed-by: Nico Pache <[email protected]>

> ---
>  mm/khugepaged.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index b12187709f6d..631459172e19 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -2234,6 +2234,7 @@ static void retract_page_tables(struct address_space 
> *mapping, pgoff_t pgoff)
>  static enum scan_result collapse_file(struct mm_struct *mm, unsigned long 
> addr,
>               struct file *file, pgoff_t start, struct collapse_control *cc)
>  {
> +     const unsigned int max_ptes_none = collapse_max_ptes_none(cc, NULL, 
> HPAGE_PMD_ORDER);
>       struct address_space *mapping = file->f_mapping;
>       struct page *dst;
>       struct folio *folio, *tmp, *new_folio;
> @@ -2299,7 +2300,13 @@ static enum scan_result collapse_file(struct mm_struct 
> *mm, unsigned long addr,
>                                               goto xa_locked;
>                                       }
>                               }
> -                             nr_none++;
> +
> +                             if (++nr_none > max_ptes_none) {
> +                                     result = SCAN_EXCEED_NONE_PTE;
> +                                     
> count_vm_event(THP_SCAN_EXCEED_NONE_PTE);
> +                                     goto xa_locked;
> +                             }
> +
>                               index++;
>                               continue;
>                       }


Reply via email to