linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Vernon Yang <vernon2gm@gmail.com>, akpm@linux-foundation.org
Cc: lorenzo.stoakes@oracle.com, ziy@nvidia.com, dev.jain@arm.com,
	baohua@kernel.org, lance.yang@linux.dev, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org,
	Vernon Yang <yanglincheng@kylinos.cn>
Subject: Re: [PATCH mm-new v7 2/5] mm: khugepaged: refine scan progress number
Date: Wed, 18 Feb 2026 09:05:22 +0100	[thread overview]
Message-ID: <392f45e0-0942-47c9-8833-d54833842e9e@kernel.org> (raw)
In-Reply-To: <4qdu7owpmxfh3ugsue775fxarw5g2gcggbxdf5psj75nnu7z2u@cv2uu2yocaxq>

On 2/18/26 04:55, Vernon Yang wrote:
> On Sat, Feb 07, 2026 at 04:16:10PM +0800, Vernon Yang wrote:
>> From: Vernon Yang <yanglincheng@kylinos.cn>
>>
>> Currently, each scan always increases "progress" by HPAGE_PMD_NR,
>> even if only scanning a single PTE/PMD entry.
>>
>> - When only scanning a sigle PTE entry, let me provide a detailed
>>    example:
>>
>> static int hpage_collapse_scan_pmd()
>> {
>> 	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
>> 	     _pte++, addr += PAGE_SIZE) {
>> 		pte_t pteval = ptep_get(_pte);
>> 		...
>> 		if (pte_uffd_wp(pteval)) { <-- first scan hit
>> 			result = SCAN_PTE_UFFD_WP;
>> 			goto out_unmap;
>> 		}
>> 	}
>> }
>>
>> During the first scan, if pte_uffd_wp(pteval) is true, the loop exits
>> directly. In practice, only one PTE is scanned before termination.
>> Here, "progress += 1" reflects the actual number of PTEs scanned, but
>> previously "progress += HPAGE_PMD_NR" always.
>>
>> - When the memory has been collapsed to PMD, let me provide a detailed
>>    example:
>>
>> The following data is traced by bpftrace on a desktop system. After
>> the system has been left idle for 10 minutes upon booting, a lot of
>> SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE are observed during a full scan
>> by khugepaged.
>>
>>  From trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file, the
>> following statuses were observed, with frequency mentioned next to them:
>>
>> SCAN_SUCCEED          : 1
>> SCAN_EXCEED_SHARED_PTE: 2
>> SCAN_PMD_MAPPED       : 142
>> SCAN_NO_PTE_TABLE     : 178
>> total progress size   : 674 MB
>> Total time            : 419 seconds, include khugepaged_scan_sleep_millisecs
>>
>> The khugepaged_scan list save all task that support collapse into hugepage,
>> as long as the task is not destroyed, khugepaged will not remove it from
>> the khugepaged_scan list. This exist a phenomenon where task has already
>> collapsed all memory regions into hugepage, but khugepaged continues to
>> scan it, which wastes CPU time and invalid, and due to
>> khugepaged_scan_sleep_millisecs (default 10s) causes a long wait for
>> scanning a large number of invalid task, so scanning really valid task
>> is later.
>>
>> After applying this patch, when the memory is either SCAN_PMD_MAPPED or
>> SCAN_NO_PTE_TABLE, just skip it, as follow:
>>
>> SCAN_EXCEED_SHARED_PTE: 2
>> SCAN_PMD_MAPPED       : 147
>> SCAN_NO_PTE_TABLE     : 173
>> total progress size   : 45 MB
>> Total time            : 20 seconds
>>
>> Signed-off-by: Vernon Yang <yanglincheng@kylinos.cn>
>> ---
>>   mm/khugepaged.c | 38 ++++++++++++++++++++++++++++----------
>>   1 file changed, 28 insertions(+), 10 deletions(-)
>>
>> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
>> index 4049234e1c8b..8b68ae3bc2c5 100644
>> --- a/mm/khugepaged.c
>> +++ b/mm/khugepaged.c
>> @@ -68,7 +68,10 @@ enum scan_result {
>>   static struct task_struct *khugepaged_thread __read_mostly;
>>   static DEFINE_MUTEX(khugepaged_mutex);
>>
>> -/* default scan 8*HPAGE_PMD_NR ptes (or vmas) every 10 second */
>> +/*
>> + * default scan 8*HPAGE_PMD_NR ptes, pmd_mapped, no_pte_table or vmas
>> + * every 10 second.
>> + */
>>   static unsigned int khugepaged_pages_to_scan __read_mostly;
>>   static unsigned int khugepaged_pages_collapsed;
>>   static unsigned int khugepaged_full_scans;
>> @@ -1240,7 +1243,8 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
>>   }
>>
>>   static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
>> -		struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked,
>> +		struct vm_area_struct *vma, unsigned long start_addr,
>> +		bool *mmap_locked, unsigned int *cur_progress,
>>   		struct collapse_control *cc)
>>   {
>>   	pmd_t *pmd;
>> @@ -1256,19 +1260,27 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
>>   	VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK);
>>
>>   	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
>> -	if (result != SCAN_SUCCEED)
>> +	if (result != SCAN_SUCCEED) {
>> +		if (cur_progress)
>> +			*cur_progress = 1;
>>   		goto out;
>> +	}
>>
>>   	memset(cc->node_load, 0, sizeof(cc->node_load));
>>   	nodes_clear(cc->alloc_nmask);
>>   	pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
>>   	if (!pte) {
>> +		if (cur_progress)
>> +			*cur_progress = 1;
>>   		result = SCAN_NO_PTE_TABLE;
>>   		goto out;
>>   	}
>>
>>   	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
>>   	     _pte++, addr += PAGE_SIZE) {
>> +		if (cur_progress)
>> +			*cur_progress += 1;
>> +
>>   		pte_t pteval = ptep_get(_pte);
>>   		if (pte_none_or_zero(pteval)) {
>>   			++none_or_zero;
>> @@ -2288,8 +2300,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
>>   	return result;
>>   }
>>
>> -static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
>> -		struct file *file, pgoff_t start, struct collapse_control *cc)
>> +static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm,
>> +		unsigned long addr, struct file *file, pgoff_t start,
>> +		unsigned int *cur_progress, struct collapse_control *cc)
>>   {
>>   	struct folio *folio = NULL;
>>   	struct address_space *mapping = file->f_mapping;
>> @@ -2378,6 +2391,8 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned
>>   			cond_resched_rcu();
>>   		}
>>   	}
>> +	if (cur_progress)
>> +		*cur_progress = HPAGE_PMD_NR;
> 
> Hi David,
> 
> When using Fedora Server, I found a lot of SCAN_PTE_MAPPED_HUGEPAGE.
> 
> The following data is traced by bpftrace[1] on Fedora Server. After
> the system has been left idle for 10 minutes upon booting, a lot of
> SCAN_PTE_MAPPED_HUGEPAGE are observed during a full scan by khugepaged,
> as shown below:
> 
> SCAN_SUCCEED            : 1
> SCAN_PMD_MAPPED         : 22
> SCAN_EXCEED_NONE_PTE    : 67
> SCAN_PTE_MAPPED_HUGEPAGE: 919
> 
> I simply handled SCAN_PTE_MAPPED_HUGEPAGE by "cur_progress" equal to 1,
> as follows:
> 
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 437783cf2873..7f301bebfb11 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -2405,8 +2405,12 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm,
>   		}
>   	}
>   	rcu_read_unlock();
> -	if (cur_progress)
> -		*cur_progress = HPAGE_PMD_NR;
> +	if (cur_progress) {
> +		if (result == SCAN_PTE_MAPPED_HUGEPAGE)
> +			*cur_progress = 1;
> +		else
> +			*cur_progress = HPAGE_PMD_NR;
> +	}

That makes sense to me.

-- 
Cheers,

David


  reply	other threads:[~2026-02-18  8:05 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-07  8:16 [PATCH mm-new v7 0/5] Improve khugepaged scan logic Vernon Yang
2026-02-07  8:16 ` [PATCH mm-new v7 1/5] mm: khugepaged: add trace_mm_khugepaged_scan event Vernon Yang
2026-02-07  8:16 ` [PATCH mm-new v7 2/5] mm: khugepaged: refine scan progress number Vernon Yang
2026-02-08  9:17   ` Dev Jain
2026-02-08 13:25     ` Vernon Yang
2026-02-18  3:55   ` Vernon Yang
2026-02-18  8:05     ` David Hildenbrand (Arm) [this message]
2026-02-07  8:16 ` [PATCH mm-new v7 3/5] mm: add folio_test_lazyfree helper Vernon Yang
2026-02-07  8:16 ` [PATCH mm-new v7 4/5] mm: khugepaged: skip lazy-free folios Vernon Yang
2026-02-07  8:34   ` Barry Song
2026-02-07 13:51     ` Lance Yang
2026-02-07 21:38       ` David Hildenbrand (Arm)
2026-02-07 22:01         ` Barry Song
2026-02-07 22:05           ` David Hildenbrand (Arm)
2026-02-07 22:17             ` Barry Song
2026-02-07 22:25               ` David Hildenbrand (Arm)
2026-02-07 22:31                 ` Barry Song
2026-02-08 13:26                 ` Vernon Yang
2026-02-08  4:06         ` Lance Yang
2026-02-07  8:16 ` [PATCH mm-new v7 5/5] mm: khugepaged: set to next mm direct when mm has MMF_DISABLE_THP_COMPLETELY Vernon Yang
  -- strict thread matches above, loose matches on Subject: below --
2026-02-07  8:11 [PATCH mm-new v7 0/5] Improve khugepaged scan logic Vernon Yang
2026-02-07  8:11 ` [PATCH mm-new v7 2/5] mm: khugepaged: refine scan progress number Vernon Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=392f45e0-0942-47c9-8833-d54833842e9e@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=lance.yang@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=vernon2gm@gmail.com \
    --cc=yanglincheng@kylinos.cn \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox