Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm: khugepaged: refine scan progress number

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

SCAN_PTE_MAPPED_HUGEPAGE is the same, for detailed data, refer to
https://lore.kernel.org/linux-mm/4qdu7owpmxfh3ugsue775fxarw5g2gcggbxdf5psj75nnu7z2u@cv2uu2yocaxq

Link: https://lkml.kernel.org/r/20260221093918.1456187-3-vernon2gm@gmail.com
Signed-off-by: Vernon Yang <yanglincheng@kylinos.cn>
Reviewed-by: Dev Jain <dev.jain@arm.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Barry Song <baohua@kernel.org>
Cc: David Hildenbrand (arm) <david@kernel.org>
Cc: Lance Yang <lance.yang@linux.dev>
Cc: Liam Howlett <Liam.Howlett@oracle.com>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Nico Pache <npache@redhat.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Zi Yan <ziy@nvidia.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Vernon Yang and committed by
Andrew Morton
eeeb79d5 36cec70e

+32 -10
+32 -10
mm/khugepaged.c
··· 68 68 static struct task_struct *khugepaged_thread __read_mostly; 69 69 static DEFINE_MUTEX(khugepaged_mutex); 70 70 71 - /* default scan 8*HPAGE_PMD_NR ptes (or vmas) every 10 second */ 71 + /* 72 + * default scan 8*HPAGE_PMD_NR ptes, pmd_mapped, no_pte_table or vmas 73 + * every 10 second. 74 + */ 72 75 static unsigned int khugepaged_pages_to_scan __read_mostly; 73 76 static unsigned int khugepaged_pages_collapsed; 74 77 static unsigned int khugepaged_full_scans; ··· 1234 1231 } 1235 1232 1236 1233 static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, 1237 - struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, 1234 + struct vm_area_struct *vma, unsigned long start_addr, 1235 + bool *mmap_locked, unsigned int *cur_progress, 1238 1236 struct collapse_control *cc) 1239 1237 { 1240 1238 pmd_t *pmd; ··· 1251 1247 VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK); 1252 1248 1253 1249 result = find_pmd_or_thp_or_none(mm, start_addr, &pmd); 1254 - if (result != SCAN_SUCCEED) 1250 + if (result != SCAN_SUCCEED) { 1251 + if (cur_progress) 1252 + *cur_progress = 1; 1255 1253 goto out; 1254 + } 1256 1255 1257 1256 memset(cc->node_load, 0, sizeof(cc->node_load)); 1258 1257 nodes_clear(cc->alloc_nmask); 1259 1258 pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl); 1260 1259 if (!pte) { 1260 + if (cur_progress) 1261 + *cur_progress = 1; 1261 1262 result = SCAN_NO_PTE_TABLE; 1262 1263 goto out; 1263 1264 } 1264 1265 1265 1266 for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR; 1266 1267 _pte++, addr += PAGE_SIZE) { 1268 + if (cur_progress) 1269 + *cur_progress += 1; 1270 + 1267 1271 pte_t pteval = ptep_get(_pte); 1268 1272 if (pte_none_or_zero(pteval)) { 1269 1273 ++none_or_zero; ··· 2291 2279 return result; 2292 2280 } 2293 2281 2294 - static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr, 2295 - struct file *file, pgoff_t start, struct collapse_control *cc) 2282 + static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, 2283 + unsigned long addr, struct file *file, pgoff_t start, 2284 + unsigned int *cur_progress, struct collapse_control *cc) 2296 2285 { 2297 2286 struct folio *folio = NULL; 2298 2287 struct address_space *mapping = file->f_mapping; ··· 2383 2370 } 2384 2371 } 2385 2372 rcu_read_unlock(); 2373 + if (cur_progress) { 2374 + if (result == SCAN_PTE_MAPPED_HUGEPAGE) 2375 + *cur_progress = 1; 2376 + else 2377 + *cur_progress = HPAGE_PMD_NR; 2378 + } 2386 2379 2387 2380 if (result == SCAN_SUCCEED) { 2388 2381 if (cc->is_khugepaged && ··· 2467 2448 2468 2449 while (khugepaged_scan.address < hend) { 2469 2450 bool mmap_locked = true; 2451 + unsigned int cur_progress = 0; 2470 2452 2471 2453 cond_resched(); 2472 2454 if (unlikely(hpage_collapse_test_exit_or_disable(mm))) ··· 2484 2464 mmap_read_unlock(mm); 2485 2465 mmap_locked = false; 2486 2466 *result = hpage_collapse_scan_file(mm, 2487 - khugepaged_scan.address, file, pgoff, cc); 2467 + khugepaged_scan.address, file, pgoff, 2468 + &cur_progress, cc); 2488 2469 fput(file); 2489 2470 if (*result == SCAN_PTE_MAPPED_HUGEPAGE) { 2490 2471 mmap_read_lock(mm); ··· 2499 2478 } 2500 2479 } else { 2501 2480 *result = hpage_collapse_scan_pmd(mm, vma, 2502 - khugepaged_scan.address, &mmap_locked, cc); 2481 + khugepaged_scan.address, &mmap_locked, 2482 + &cur_progress, cc); 2503 2483 } 2504 2484 2505 2485 if (*result == SCAN_SUCCEED) ··· 2508 2486 2509 2487 /* move to next address */ 2510 2488 khugepaged_scan.address += HPAGE_PMD_SIZE; 2511 - progress += HPAGE_PMD_NR; 2489 + progress += cur_progress; 2512 2490 if (!mmap_locked) 2513 2491 /* 2514 2492 * We released mmap_lock so break loop. Note ··· 2831 2809 mmap_locked = false; 2832 2810 *lock_dropped = true; 2833 2811 result = hpage_collapse_scan_file(mm, addr, file, pgoff, 2834 - cc); 2812 + NULL, cc); 2835 2813 2836 2814 if (result == SCAN_PAGE_DIRTY_OR_WRITEBACK && !triggered_wb && 2837 2815 mapping_can_writeback(file->f_mapping)) { ··· 2846 2824 fput(file); 2847 2825 } else { 2848 2826 result = hpage_collapse_scan_pmd(mm, vma, addr, 2849 - &mmap_locked, cc); 2827 + &mmap_locked, NULL, cc); 2850 2828 } 2851 2829 if (!mmap_locked) 2852 2830 *lock_dropped = true;