From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 1FDBA31355C for ; Sun, 1 Feb 2026 12:26:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769948802; cv=none; b=JFnM9LFI/rFXigTlKQQzPE0xkd9g8SR1VDGoy28WoA2vCwh9vbDJQUTwnC3EvE6IoDHW5zbpZ32bugh6Q1m22AWbWvDYepENQp6evcrDXUu/3PcxGJcHHcqXfX42viTOZwlDRyqZqgw1aQ5THYyo3ZTbiwk+FpKfCPQuDJK/CZo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769948802; c=relaxed/simple; bh=0CNub0YRHLXkZuMqF4PrEKwD/v8oDvPDGA3Qag+dIow=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=d10B568pH3NSHnPVUYhms/f/r66xsmDD/xw/CYvGltkgweu2aNS57R6iPvZCTYOtsKZzhqS1EXiFzfdK5F+jY0qnjQ8yEuuAgzqiK6GPDBxLD9ULGgjMLja1hDXYxDDYJPQ736eOsMUcCgaqZY17i+eBLdnJBm2hQUz9IwqJ6t0= 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=iBSUmefl; arc=none smtp.client-ip=209.85.214.176 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="iBSUmefl" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-2a07fac8aa1so26597645ad.1 for ; Sun, 01 Feb 2026 04:26:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769948800; x=1770553600; 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=ZOp1GFQHyzeEszgnaKmemF0L8YwiMTjqGQb7miA/TDA=; b=iBSUmeflCv/LP1LZgkwWpMMBmqnO9qr5OzPmQ35SNuikvA4OfOyamF9c2RvaAym2Wr CPW9xK4UeTa6fK5ub/tRTkgi69LQMn+GXZI0Y5xxGH7LUpxzBc4ilGL+4oDIFS/sQpEM 4MXzkqqfj1sRaONXwBKZv12rxajX5+UyLOXN+FUBfjbEIg0LMDfHWY/YccLiJolVOkv1 apWLRnmbAODj//S1v1dbNH1DzV5W/dEmZzHxUGQDc+/56Wk09RSIy7q8TLK/7rc6TUhL y8AUoqanYEGnlK7S+N80z4uGpv9nb5ZQqTj3x721h73TepUYvJjDCWRcjUt3Kctwnaml 8sFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769948800; x=1770553600; 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=ZOp1GFQHyzeEszgnaKmemF0L8YwiMTjqGQb7miA/TDA=; b=OXpylfUt//8nz66uw0rS0BafdK/zzr8ATRUjhtPOr0Higr7zn9pjsBKfY4Y4g+/bu9 fCEuJGTYbwGmSnBg/nbC8IC404Yfnstir3uMl1K72IHEQ5b5SVIFc4MZgRq7B/lyK6+s qS4Ox/Q2TvU2/cOuA4nsEuFoeiENAY/cxgaJ8ThHAY4GMtxaB31sfauE+emPeTKvyH3W jYeCyKPJovyz3ARGBf6unuNfLaYSj8hjX+wIJQmO4qsSiHGyB5/9peeuHEbnb9pS6ASN BZHQkVO+cw0zC4XYjkheTM5hgRqGiEkL7wu8cc0O0nqaFDvNz/8jLZlJjxgMlPxZM4SJ vcpQ== X-Forwarded-Encrypted: i=1; AJvYcCWpLkHcqRpNx11j76S6ksZc0YEnz2BPzKGKr+dXlzReBEFA6mX+Bj5uZ1bAxPZSdiYO/TWZYOQtFlMgKjc=@vger.kernel.org X-Gm-Message-State: AOJu0Yz0VYMB+y5vgHBc7qVpHFBzleRWBkvJuW118CNsQEMgq+kE8X+r yfTs4M+/6UAb2NeAZrazmD/ZM2vWiDCqTycvyiIjZich4Zm5X0WiZzxnipYsFGt4iTA= X-Gm-Gg: AZuq6aIoHwoX9qgxOpj6Gcip/O0R17j9NErgjBNkIbJNnkWcsBh3ax3aGZGUvE6TfU/ /txnJrUy9A8Bvu7pMSNY5gNKYl8aMn72GHH+/l+pgoCnPSk6lgOUxg3ztrlf3czALNKuBqlTpHG bOon0JoED3DiMmIx4whSQ5nUC/Zxw7np/VmL2U0RJUKHfoZzCQ6cRhdmZn4YQqgEz0NU0QCfl2N F14i4DshNv/0PhABQqMVlLqgIH5KTit1IJ2Nk19BrvV45VC4hFNzGX60RjNFenf6Xm3HlzRMT1L jHSmB/NYRX9GEogCP9sOuImkhU6b4Q99PCQljObu5cQCb5RiGy3WKqbJeq0G+dhiVjJ9h+BMlJT 3v/yBElWZuRYe/PxSqdk6K3m4r56rJxqNw5fziALoc5phMAbqa0aOnq51tz6pVW5/mHejHMHhUO 5wXpuvi602X/hpHilDme/qeFaG X-Received: by 2002:a17:903:2b04:b0:2a1:2ebc:e633 with SMTP id d9443c01a7336-2a8d7eb53ecmr81513165ad.4.1769948800330; Sun, 01 Feb 2026 04:26:40 -0800 (PST) Received: from localhost.localdomain ([114.231.118.96]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a88b6e5ac4sm116941185ad.86.2026.02.01.04.26.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Feb 2026 04:26:39 -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 v6 0/5] Improve khugepaged scan logic Date: Sun, 1 Feb 2026 20:25:49 +0800 Message-ID: <20260201122554.1470071-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. 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. 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 | 60 ++++++++++++++++++++++++------ mm/rmap.c | 2 +- mm/vmscan.c | 5 +-- 5 files changed, 82 insertions(+), 16 deletions(-) -- 2.51.0