From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 35E4731BC84 for ; Fri, 23 Jan 2026 08:22:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769156579; cv=none; b=QQ0qoAmm9Jcn0cMIytsoMNkCTWpOGc1hSX1robT6skrD8agfy908MktEjJQ09K4+prqMjsiaChmtXYo91Per0PPtN6KfZP8gSdLpS2p0d2nA9I4BHVyO4ujfQww+lP5tLaS34lYYlvEvn/qHN+Cq5FqQJPBTaee8gt4rD8DE3Ck= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769156579; c=relaxed/simple; bh=N4TeBgSWWYqsJS5a+PIyajsO5BXoK2uYylix8C3ONaw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=tXOaglcRJifvnIlAl+0orPjiEIOa5jOIw7BMQdGTSq+NUoc2yMzzR36JAJohKJj1YgDR4RTCCcEUJ/97pHw5CHpwEz7Dtt9rXMvlVvT20xQ1YH2CgQS8XxgC95Kfpvde6qX2c6YBotfn4WOm96rfkFnH84ytDoKIXHatyDoVPI8= 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=J0oFQAKz; arc=none smtp.client-ip=209.85.210.175 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="J0oFQAKz" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-7f89d0b37f0so1526045b3a.0 for ; Fri, 23 Jan 2026 00:22:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769156577; x=1769761377; 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=9IUJFlA7XSFnWdLZVGcPsTawS/pp01Gi6bNp5Y5CBw4=; b=J0oFQAKzbV/sGZhPpwcKD3afuuvpY4NqA83HfLkj36hiVcGQwwDS4jtLNNpoVexaUW kDNCCa9gnxnSVFLyi3w22MwIpNzp5KQ21k35x3f6GmEKRa1rbmKxwOEu92dVlk8/3NQM 2o8SFNX3pqlWfe+ANAoHzfuiLP/nyugfUP3h2loOAp9+BTtcEL86oAArSWfoNuOD8MKX HuDsvtU6GAZ5+TMUrhf5gkrkTn6HZAOZc12Rc38OsO4RxLE9Ov759J3xtVoGPgSoT74J 89ziYamfzDzrjLVxdKJy+fwUZYDNcN8+woeOSXrkiHuv5css9nYxohECds8qdl54daTs FdnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769156577; x=1769761377; 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=9IUJFlA7XSFnWdLZVGcPsTawS/pp01Gi6bNp5Y5CBw4=; b=IxyaTW9LiO9jsAyQoS2p4f560wejzUCeeCuc9d/OnaUUrok3oyv72YWaqSMY/hnntj kAgxV9ncZdPdbSBJUcOUT2OtL1YI8NmiFAFtCk6G4nVVHbRHA4LOJzxmPHzOclmXuyqo encuc1rblEc5wPyfpgpf/KkACBNVM6YY5GiZ3LVRBqz8qAeJW2+DCSmCPw0bu4CZum+j ddzubRUrXBAGTER2JiXsLobKXRbQSznMMU+31zlikfGrJpyVtryyg7ti7bLaNddcaww4 FnwPyME/FxBDW3O1pUWPWnvqVGgwRnZ8BzLXoYfW+G6sSfDQIlUXPQn8xJgNCrkieR7Z jJwQ== X-Forwarded-Encrypted: i=1; AJvYcCWafHtp+pSk7G1uGZnhWR5VamJSxzNViTquKxqvv6Mx0edzrEVWlr4aEt+bS8PeoywtwEk+9AzXm1gWBrU=@vger.kernel.org X-Gm-Message-State: AOJu0YyIIt1QY5mtl134oY6FSJmA60vEYppBUOdecqoQ1C/45emHg3+F mwm0ljH+5pTfJPMW/ZcQcK2nhjdwoqNhTtE4lS0kUPMlcXJGRfbkwSzB X-Gm-Gg: AZuq6aLct35IGfTxpXx85j/EUcknX3mwlejGuqifwZoz6+3zTGSoD3EDRBdoRMSpAJe N0A34VIYSBAYXCTea7Sd12cMV7ko4FOs4z+Vb3iVwhSJzXqvcDzjevvPU7mMQoHPKbWZhCYsoIx ahAjTurOz1GV01Cr9viXttRr0V9H7AzJJHS7uo84RPyUm5uuqFdtThFY88jyROSo4WSncHMsqPz jCfyqRfU8JKOFhrvFRv2nj4E/JjXmq1T/AVBSls+djiN6ok7xskF+yhtESnAIylDchRaJJYCEWY hurpIuBd6TGIYkAqRsN83BbYegKPLmITLj0uHIaUVV7kz0YY0HUkIkWYqY3nJRyfjSldSZk6BhJ 430eWLu1fhXR+Eq1xiVVcN1m/wBxHqImwRWHBRkL0eUMlwLUmwJzUtYuhjQSu8y43YTEEl9MEe7 hEyl5EmdqeUls3pLjk1Lciroug X-Received: by 2002:a05:6a21:6d86:b0:35f:31bb:5a5d with SMTP id adf61e73a8af0-38e6ff9573amr2198598637.2.1769156577492; Fri, 23 Jan 2026 00:22:57 -0800 (PST) Received: from localhost.localdomain ([221.227.246.159]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c635a43f11csm1348363a12.35.2026.01.23.00.22.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jan 2026 00:22:56 -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 v5 0/5] Improve khugepaged scan logic Date: Fri, 23 Jan 2026 16:22:27 +0800 Message-ID: <20260123082232.16413-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. 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. 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 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/linux/xarray.h | 9 +++++ include/trace/events/huge_memory.h | 26 +++++++++++++ mm/khugepaged.c | 62 +++++++++++++++++++++++++----- mm/rmap.c | 2 +- mm/vmscan.c | 5 +-- 6 files changed, 95 insertions(+), 14 deletions(-) -- 2.51.0