From: Lance Yang <lance.yang@linux.dev>
To: akpm@linux-foundation.org, david@redhat.com, lorenzo.stoakes@oracle.com
Cc: ziy@nvidia.com, baolin.wang@linux.alibaba.com,
Liam.Howlett@oracle.com, npache@redhat.com, ryan.roberts@arm.com,
dev.jain@arm.com, baohua@kernel.org, ioworker0@gmail.com,
richard.weiyang@gmail.com, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, Lance Yang <lance.yang@linux.dev>
Subject: [PATCH mm-new 1/2] mm/khugepaged: optimize PTE scanning with if-else-if-else-if chain
Date: Thu, 2 Oct 2025 15:32:54 +0800 [thread overview]
Message-ID: <20251002073255.14867-2-lance.yang@linux.dev> (raw)
In-Reply-To: <20251002073255.14867-1-lance.yang@linux.dev>
From: Lance Yang <lance.yang@linux.dev>
As pointed out by Dev, the PTE checks for disjoint conditions in the
scanning loops can be optimized. is_swap_pte, (pte_none && is_zero_pfn),
and pte_uffd_wp are mutually exclusive.
This patch refactors the loops in both __collapse_huge_page_isolate() and
hpage_collapse_scan_pmd() to use a continuous if-else-if-else-if chain
instead of separate if blocks.
Also, this is a preparatory step to make it easier to merge the
almost-duplicated scanning logic in these two functions, as suggested
by David.
Suggested-by: Dev Jain <dev.jain@arm.com>
Suggested-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Lance Yang <lance.yang@linux.dev>
---
mm/khugepaged.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index f4f57ba69d72..808523f92c7b 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -548,8 +548,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
_pte++, addr += PAGE_SIZE) {
pte_t pteval = ptep_get(_pte);
- if (pte_none(pteval) || (pte_present(pteval) &&
- is_zero_pfn(pte_pfn(pteval)))) {
+ if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
++none_or_zero;
if (!userfaultfd_armed(vma) &&
(!cc->is_khugepaged ||
@@ -560,12 +559,10 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
count_vm_event(THP_SCAN_EXCEED_NONE_PTE);
goto out;
}
- }
- if (!pte_present(pteval)) {
+ } else if (!pte_present(pteval)) {
result = SCAN_PTE_NON_PRESENT;
goto out;
- }
- if (pte_uffd_wp(pteval)) {
+ } else if (pte_uffd_wp(pteval)) {
result = SCAN_PTE_UFFD_WP;
goto out;
}
@@ -1316,8 +1313,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
count_vm_event(THP_SCAN_EXCEED_SWAP_PTE);
goto out_unmap;
}
- }
- if (pte_uffd_wp(pteval)) {
+ } else if (pte_uffd_wp(pteval)) {
/*
* Don't collapse the page if any of the small
* PTEs are armed with uffd write protection.
--
2.49.0
next prev parent reply other threads:[~2025-10-02 7:35 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-02 7:32 [PATCH mm-new 0/2] mm/khugepaged: refactor and merge PTE scanning logic Lance Yang
2025-10-02 7:32 ` Lance Yang [this message]
2025-10-03 13:21 ` [PATCH mm-new 1/2] mm/khugepaged: optimize PTE scanning with if-else-if-else-if chain Wei Yang
2025-10-03 16:33 ` Dev Jain
2025-10-04 3:08 ` Lance Yang
2025-10-03 17:04 ` Zi Yan
2025-10-02 7:32 ` [PATCH mm-new 2/2] mm/khugepaged: merge PTE scanning logic into a new helper Lance Yang
2025-10-03 17:05 ` Dev Jain
2025-10-04 3:03 ` Lance Yang
2025-10-04 4:42 ` Dev Jain
2025-10-04 5:24 ` Lance Yang
2025-10-04 9:42 ` Wei Yang
2025-10-04 13:11 ` Dev Jain
2025-10-05 2:35 ` Lance Yang
2025-10-05 2:38 ` Zi Yan
2025-10-05 2:44 ` Lance Yang
2025-10-03 17:11 ` Zi Yan
2025-10-04 3:06 ` Lance 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=20251002073255.14867-2-lance.yang@linux.dev \
--to=lance.yang@linux.dev \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@redhat.com \
--cc=dev.jain@arm.com \
--cc=ioworker0@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=npache@redhat.com \
--cc=richard.weiyang@gmail.com \
--cc=ryan.roberts@arm.com \
--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