From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 329CC21D599 for ; Sat, 7 Feb 2026 08:16:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770452192; cv=none; b=Ktjm724sBVOj7Q2ek444B6nEm1MeHrGsmaf94YjYUduLIWO8IZbx5JG+KCD6SMol6TKKK0aIXYwUqNx6A5TfoSim293ZTSyphdkYGi4HGCwgUryVw+CxBHPE0MBUP+Cdblcdsn53lK6SWoWCZvk/WtscOWCG9NChxfA8njj5iFs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770452192; c=relaxed/simple; bh=su3Suvrl6wwjFI8UuIGIuahKJIAsz+BPR6WxBEIs6jQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=mKJH+sz8mrNWIsHjEPPSwiYSSWDuuAyzT2ERcYCx28lA0qBMXI8xwxIJxp1AO6MJOw7jPwHnoSGq0vU9qSQ/VRJEmh+0AJWbfWKCYwezLG9HdKQ34uNdR+p6HElPCBTmmO8LcM8CmJs4xCfeWXhrP9/LQ/w+ttKkt4efOrNaUV4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gykmgEBA; arc=none smtp.client-ip=209.85.216.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gykmgEBA" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-35621aa8c7fso41287a91.2 for ; Sat, 07 Feb 2026 00:16:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770452191; x=1771056991; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Av016yHVQTZQvLAEB6lorvBQy4JiwiUlhJeSthydisA=; b=gykmgEBAuLPguPHKe/OIbG5KpxBbnrv1B2qgpDtOPgdbNOTpz8OdAD3SGlrI7AbC8c Wf4iRoJeF04GSgTBuKG85dZ4iNKv2W/pWi4f2edHhD76OMwLNx2L56PkWud0r2nJQpym yg8n+vGgGROjH3eoPZRX4lIJq31awNpu//TkeYORyNa2Stisaf9g11W8zAQn8HcK0X8V XQ9SwpXAXcmM4ZbbmiVRNKmWJvcMgleBTUDZiDBHoVv/hYwUrXS0uW3ucnPQszCnjeGN TGCbO8aeU7Tt5h18SqcicQ5wqAX+r1fgYvi2QiTwYRVjKjJDIGqBfPJbUtImyGJbpDfo ZHng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770452191; x=1771056991; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Av016yHVQTZQvLAEB6lorvBQy4JiwiUlhJeSthydisA=; b=k51i3Cr2+OmHthW7WAAKZ8mBXQj5NL2EohEnDFYLV7esy6rnpjhF0PA5YyCXJnppmR nIZHQ8HVx9wi/9HO2Lfjkp3HZ9UJVfn6FMTBHp7oC3vy+VUJehc0zB+ML0U676HtdJG4 Zs614MGe5gBme+A1gWJHPZYJCKo+qxUHk5PwlrDh0y1LKxGhKlpEwnSUgqYO7Se8gKhP 8MbFNY31f5K7aGrmfaCossruzTJpnxoOqsVE/BMIh+VYDZ8ImzGT2HLguxO3B6eXyISV SNkI/CP7na8g48dijbcOkr8TSA5iTR+m3wG+2ZXXFPSKbZwWGJfZqXojR1930F2siteP SCnw== X-Forwarded-Encrypted: i=1; AJvYcCVC3KZ/bWdjealy6xVMhsmxdLQmWUwJCc7Af9/s+ZhrMR3CE8P/mkpFtOOV/dn1l+v3WXOy8ZeBu2AS6vo=@vger.kernel.org X-Gm-Message-State: AOJu0YxmCs+oghFYJCjWjA6+9FC7TxTafejsKOKyodoObiQLXoGa2Gz0 SjkxMclyF4ucn/GtRPih/LX3omKTHvKBom0o810yljCGlZPkxyR+K6Vj X-Gm-Gg: AZuq6aLuYVBLfB8F+euNgWrAlg/h9RmQqql54p2tA6hz7P0w0acuNqhU6Qw9qt/Kb84 AINGatAJZA2YUm4VB1kLhdNeYKRfmPH9GdAPJFrGAqS6wvTdfQf+ybAPpA7m2nPVwLbwmdhtgrD fM+WKbAZawFn9mYq7yuN6AABulaPak7Pcr+ijdMQCjGH8HkJmlvKIu/dMyRJrf5v2NYbCcMiW+s JY7opg16jjhBiI2OJqjGq5NQyo9cZ/RFtE1tjTnDHbqFfBLZQdL7585uSGGIQIaajKbtvxOFCBy XJ2vcT9XZ39j25MZGUzppGhaYM/BMlnpKTVEg9AEOhKO67V3tOXBtAHAwOfIjMJ3UOwPF/8XW6z +6Rzzm4JdI5PnUWjJEkEhC9r/ei6u0tu0XMT8qfn7MAkOQ8U/68fPBPvqSECSzZ/jSYqXcZ5Ud9 d5WNVD8ax7oLbhHs/FAzbC3KY= X-Received: by 2002:a17:90b:38ce:b0:340:25f0:a9b with SMTP id 98e67ed59e1d1-354b3e6441dmr4597928a91.33.1770452191364; Sat, 07 Feb 2026 00:16:31 -0800 (PST) Received: from localhost.localdomain ([114.231.118.96]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3549c5955a7sm8028189a91.17.2026.02.07.00.16.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Feb 2026 00:16:30 -0800 (PST) From: Vernon Yang To: akpm@linux-foundation.org, david@kernel.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 Subject: [PATCH mm-new v7 0/5] Improve khugepaged scan logic Date: Sat, 7 Feb 2026 16:16:08 +0800 Message-ID: <20260207081613.588598-1-vernon2gm@gmail.com> X-Mailer: git-send-email 2.51.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Vernon Yang Hi all, This series is improve the khugepaged scan logic, reduce CPU consumption, prioritize scanning task that access memory frequently. The following data is traced by bpftrace[1] 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. @scan_pmd_status[1]: 1 ## SCAN_SUCCEED @scan_pmd_status[6]: 2 ## SCAN_EXCEED_SHARED_PTE @scan_pmd_status[3]: 142 ## SCAN_PMD_MAPPED @scan_pmd_status[2]: 178 ## SCAN_NO_PTE_TABLE total progress size: 674 MB Total time : 419 seconds ## include khugepaged_scan_sleep_millisecs The khugepaged has below phenomenon: the khugepaged list is scanned in a FIFO manner, as long as the task is not destroyed, 1. the task no longer has memory that can be collapsed into hugepage, continues scan it always. 2. the task at the front of the khugepaged scan list is cold, they are still scanned first. 3. everyone scan at intervals of khugepaged_scan_sleep_millisecs (default 10s). If we always scan the above two cases first, the valid scan will have to wait for a long time. For the first case, when the memory is either SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, just skip it. For the second case, if the user has explicitly informed us via MADV_FREE that these folios will be freed, just skip it only. The below is some performance test results. kernbench results (testing on x86_64 machine): baseline w/o patches test w/ patches Amean user-32 18522.51 ( 0.00%) 18333.64 * 1.02%* Amean syst-32 1137.96 ( 0.00%) 1113.79 * 2.12%* Amean elsp-32 666.04 ( 0.00%) 659.44 * 0.99%* BAmean-95 user-32 18520.01 ( 0.00%) 18323.57 ( 1.06%) BAmean-95 syst-32 1137.68 ( 0.00%) 1110.50 ( 2.39%) BAmean-95 elsp-32 665.92 ( 0.00%) 659.06 ( 1.03%) BAmean-99 user-32 18520.01 ( 0.00%) 18323.57 ( 1.06%) BAmean-99 syst-32 1137.68 ( 0.00%) 1110.50 ( 2.39%) BAmean-99 elsp-32 665.92 ( 0.00%) 659.06 ( 1.03%) Create three task[2]: hot1 -> cold -> hot2. After all three task are created, each allocate memory 128MB. the hot1/hot2 task continuously access 128 MB memory, while the cold task only accesses its memory briefly andthen call madvise(MADV_FREE). Here are the performance test results: (Throughput bigger is better, other smaller is better) Testing on x86_64 machine: | task hot2 | without patch | with patch | delta | |---------------------|---------------|---------------|---------| | total accesses time | 3.14 sec | 2.93 sec | -6.69% | | cycles per access | 4.96 | 2.21 | -55.44% | | Throughput | 104.38 M/sec | 111.89 M/sec | +7.19% | | dTLB-load-misses | 284814532 | 69597236 | -75.56% | Testing on qemu-system-x86_64 -enable-kvm: | task hot2 | without patch | with patch | delta | |---------------------|---------------|---------------|---------| | total accesses time | 3.35 sec | 2.96 sec | -11.64% | | cycles per access | 7.29 | 2.07 | -71.60% | | Throughput | 97.67 M/sec | 110.77 M/sec | +13.41% | | dTLB-load-misses | 241600871 | 3216108 | -98.67% | This series is based on mm-new. Thank you very much for your comments and discussions. V6 -> V7: - Use "*cur_progress += 1" at the beginning of the loop in anon case. - Always "cur_progress" equal to HPAGE_PMD_NR in file case. - Some cleaning, and pickup Acked-by and Reviewed-by. V5 -> V6: - Simplify hpage_collapse_scan_file() [3] and hpage_collapse_scan_pmd(). - Skip lazy-free folios in the khugepaged only [4]. - pickup Reviewed-by. V4 -> V5: - Patch #3 are squashed to Patch #2 - File patch utilize "xas->xa_index" to fix issue. - folio_is_lazyfree() to folio_test_lazyfree() - Just skip lazyfree folio simply. - Again test kernbench in the performance mode to improve stability. - pickup Acked-by and Reviewed-by. V3 -> V4: - Rebase on mm-new. - Make Patch #2 cleaner - Fix the lazyfree folio continue to be collapsed when skipped ahead. V2 -> V3: - Refine scan progress number, add folio_is_lazyfree helper - Fix warnings at SCAN_PTE_MAPPED_HUGEPAGE. - For MADV_FREE, we will skip the lazy-free folios instead. - For MADV_COLD, remove it. - Used hpage_collapse_test_exit_or_disable() instead of vma = NULL. - pickup Reviewed-by. V1 -> V2: - Rename full to full_scan_finished, pickup Acked-by. - Just skip SCAN_PMD_MAPPED/NO_PTE_TABLE memory, not remove mm. - Set VM_NOHUGEPAGE flag when MADV_COLD/MADV_FREE to just skip, not move mm. - Again test performance at the v6.19-rc2. V6 : https://lore.kernel.org/linux-mm/20260201122554.1470071-1-vernon2gm@gmail.com V5 : https://lore.kernel.org/linux-mm/20260123082232.16413-1-vernon2gm@gmail.com V4 : https://lore.kernel.org/linux-mm/20260111121909.8410-1-yanglincheng@kylinos.cn V3 : https://lore.kernel.org/linux-mm/20260104054112.4541-1-yanglincheng@kylinos.cn V2 : https://lore.kernel.org/linux-mm/20251229055151.54887-1-yanglincheng@kylinos.cn V1 : https://lore.kernel.org/linux-mm/20251215090419.174418-1-yanglincheng@kylinos.cn [1] https://github.com/vernon2gh/app_and_module/blob/main/khugepaged/khugepaged_mm.bt [2] https://github.com/vernon2gh/app_and_module/blob/main/khugepaged/app.c [3] https://lore.kernel.org/linux-mm/4c35391e-a944-4e62-9103-4a1c4961f62a@arm.com [4] https://lore.kernel.org/linux-mm/CACZaFFNY8+UKLzBGnmB3ij9amzBdKJgytcSNtA8fLCake8Ua=A@mail.gmail.com Vernon Yang (5): mm: khugepaged: add trace_mm_khugepaged_scan event mm: khugepaged: refine scan progress number mm: add folio_test_lazyfree helper mm: khugepaged: skip lazy-free folios mm: khugepaged: set to next mm direct when mm has MMF_DISABLE_THP_COMPLETELY include/linux/page-flags.h | 5 +++ include/trace/events/huge_memory.h | 26 ++++++++++++++ mm/khugepaged.c | 57 +++++++++++++++++++++++------- mm/rmap.c | 2 +- mm/vmscan.c | 5 ++- 5 files changed, 79 insertions(+), 16 deletions(-) base-commit: a1a876489abcc1e75b03bd3b2f6739ceeaaec8c5 -- 2.51.0