From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (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 69CAF1ADC7E for ; Sat, 7 Feb 2026 08:12:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770451943; cv=none; b=ncFfdmIywkGyafrDZe35FxSEJ3jHulyKzige/HQEGrQHOYct28la9sSVTVTNo0GXt6+JD4n943MPpWgelOYLrokoDCsPTwYEELrQyNlizrvrdROSgvMC6lBOW5lSIHJ0solX3wiS/2AK88jMxT886HyznvHQIt+OO93SHzC/VJc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770451943; c=relaxed/simple; bh=su3Suvrl6wwjFI8UuIGIuahKJIAsz+BPR6WxBEIs6jQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=pG07jG24wALWEmG26t7AHTyjSIYdIaN/zaeeZHqcT+E8Ld8I7RWfbpXFDbjBt130Ic3/5jUTrLtiLfbPDYI+uVt3CTbrNZpKjZRY6uNWcntkHuPqJpBVt3/6oU2O4A0S6Hc0bte4FuIRKGNKROp0llxNiHWj16WbBe39apznHfg= 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=YvwZbPMX; arc=none smtp.client-ip=209.85.210.172 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="YvwZbPMX" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-8230c33f477so1316157b3a.2 for ; Sat, 07 Feb 2026 00:12:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770451943; x=1771056743; 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=YvwZbPMX+hKoAubdMFraHbWphzBFUBXS+MtJuiuJAmbCWOCYxxjhgYfX8NdFnlWvCh x9GuENRlShj1lHhZ9v8OegNYuY0UAb7BLCf97lpTZI1WzinGctBR6QEv50HicwHJxJvv 0sX6z2HBSBYQ4RJT4es9JyO8DKo30353LBjvA9O/RGuxFeogKkz+1vNdQg0zS3ZXho47 pX7E2aMnu2zK+owOYUUskcumOyTKaen2blDb9MQX0ZJu+/6Vx5u/HdzbgoVPUdMkzEEf wyuqyEy7v71/k0XiJxxhOCrwS9rq4xhkH8H+ZlV8kC/ovCqoVXeO6gU9cA0xJqx06sPB WINA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770451943; x=1771056743; 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=qECxRJYnqSSfQNtqaUx96Uw1hK08rqy/7b+SgZOS2ncFO6Q7HQbsrqdbZG8k89VEi3 suN1c2KLHvXikJTtyQOx9wVk1VTdqrQkfXec5qcN6dvU/p2BPSUjA86cHqP/6QEaxf0Q w+le2wDpEWFpZBlh2Yxvixag9qRGiIiY0Hrwr1iwZuFgyXXT3kvjwlAfjz+1z8twGaaG GgUZN3E6iHs9n65VBkYXb7/YU89Ifp7rZUnlMWqb1dFAucMzBLaDGcxTQPThODEW+c8x oG8gU7fzYJYRExHo/VGpLoKwArDi9I35bvWr3A1F2L2p4f3O8An6/e7SVIwI5+uXlGQt vZhw== X-Forwarded-Encrypted: i=1; AJvYcCUMsb2isr6t2nTtzh5FtcXGqobk/q6oRUyPbdOZIUC3vRd7fzNab0/iD7tb6LMlhMmmgY9BzVwcnOIlYzw=@vger.kernel.org X-Gm-Message-State: AOJu0YwNKa+fQm6AZ7r2eVcLKu1t/FF3v2vQovmr130CeMpQP97LDYyc MzNGe26Sj324uxG6rZ9O0vduaUe1gbGeVAmnudAn1gpSVL8auXLWoUVB X-Gm-Gg: AZuq6aJO1K9KzDM8XmJn7dN7KEYEnoXlxDEuzzF6lWGM3yVRnduxW1wTrOpIC+aiTk3 9f8VzPqyaTfTIg7TcjHfkLWcr+JhRVQNk2/vyxq0yOr6EaYsNZzKuQyIcW93B0fZ1R5FqXtZO0/ laTOk21RC+ec+mUVGFFTzNo1p94E/kPGerET6etBGDYF5N98NEM7kq5DR+ITVLZU+heQwcjWLMW YOLTlXrL3T+lUm7Vx7N6Z0/eVzHC6OmjoA8sj2AD76cIEMahG1JUAp9vZLTDtMiH0gy6L4UzRvi lGowLDeT4bLTLXwnaknI+1qVIEOhW8Fv2bpM7A9yVwit6sIvIeLhyiCLnQSI/eTIboeCw1ORmlQ H/YmpS4n0sHkVW67Qxu9iQdeKxLG6RNxfh8kH0I/ZnrJLpOi6Uh4rMAcllKxfMb8boKvjdPOoQT KLKpPlyfjRmlvLWo9HeR15h69WvLGKgozjbQ== X-Received: by 2002:a05:6a00:a885:b0:81f:9907:e503 with SMTP id d2e1a72fcca58-82441622de5mr4634623b3a.16.1770451942562; Sat, 07 Feb 2026 00:12:22 -0800 (PST) Received: from localhost.localdomain ([114.231.118.96]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-824490442cfsm3951466b3a.16.2026.02.07.00.12.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Feb 2026 00:12:21 -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:11:39 +0800 Message-ID: <20260207081144.588545-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