From nobody Fri Dec 19 15:01:58 2025 Received: from CH5PR02CU005.outbound.protection.outlook.com (mail-northcentralusazon11012012.outbound.protection.outlook.com [40.107.200.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 16D5C4C81 for ; Sat, 6 Dec 2025 10:18:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.200.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765016290; cv=fail; b=QwcL6qW6IufyOd+BqDrRj9xEAwZw1Dp3+OZ6T9uSPdwomo4a37Yqi8q+VQnUxXiX2ib9uU/2ahK757OxCbuLmeMVEo8luSg4yHq58ZDNUfa60tM1WyTKhHfXMRIlWsHnzzmZCbCFcpClVVXvroGXg/VVWWQuUux1XzxmZrT1rpA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765016290; c=relaxed/simple; bh=nsQMy5e+hjeMffRkkPmJjIOQvpfpDcBKlAtq6raPdOs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=UepiXLZiz1FydDGHTKOt9Eglcohu+woeBYbYKJs4kl14Z859qBgqZgd4c8X6Gv8lK4KqP5b8MuhIxf0mGCExqaV6s5kA3P1nVK9jiqh3+0rXQQTIPqGFGidNSOJnrdXOGKkAo0GHm/hbsoZA3jgVrUe+Piqz53qas0JJ4QYUTRg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=IiMoLUBC; arc=fail smtp.client-ip=40.107.200.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="IiMoLUBC" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=h8sz6ZaMoKAOnWl7IUcH9A9QTdo7JJAlEKm6qNfuueOL3dULbWt+RRECiF/MPkqBjzRE4CtA1mu8+4vgEcMH/TEfCv4HViS/etoiL4ZCJ9F0WpUYo7o1i1Izc/Q9cmPwhYqszAUXjl8skYvtpFKXnuaUBse6qZ4xygn9ci0iQDEujv+nJTu2vSm6YnUIasr/0N3/dlajbB+QyAxLU9WQ3sv+2FCUuF7dhdWgxuPfvWYiPUItlgkaoCodrzyulT9yD+wlsvrM0Q9iuwTM37DFxiufq4eQ31mvBkkD/UwCXTgGtOyX2u8xoRo9kQO/wZtO39pFAbKEdtdiTUR06D7MEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=1mtr/yUK/fFN8x0gOs384tjd1y//9IFDajUEaKF5ZMo=; b=RrRo9ip34k/kxXjsBaqlft5yieIsJ8/03DhpQI18TWukTz8obV0MbxDIVTTbX757ib5YjCdE4e25TpAm/+gMhsX3wV9vGkrcc4c0Q93EjK5Nmq7jVMaRuUpHkpsqNBZ6ulduVWGcBHFRiIpBO2jrtQ+nZFIWI1ccZC4aujVmkqDzfbL10KwfXylewyHYunzzZZVi+KVct+vDHSvy6Id2lzIt9Agvi6Xsqmxxs14AIJnMAteSI2IiHmmmKdn+3tLGU0oc2gb8YAz5H/mg8HxsUuY/151uKR3n82vtA+PbLP4Bi+2LtblGhH2yDjCq1haKYuKoySc54Eb1fQcjKOf+Sw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1mtr/yUK/fFN8x0gOs384tjd1y//9IFDajUEaKF5ZMo=; b=IiMoLUBCKC86VFyemfWzU5aZ8atfbhHeznTOXATFZ9Zbds3OTK9h+UXYtUBxxTiiT1F7m/DOjrhacy9Z0jzqrsKlUsScbP2KX3Aq1DO15bwP9mGxzHuDeHHA/JFqWoGv4VoR6oHog81eJfj1xfTlcVqJuEU7d1jIUbKAzD/xMSM= Received: from MN2PR20CA0010.namprd20.prod.outlook.com (2603:10b6:208:e8::23) by DM6PR12MB4123.namprd12.prod.outlook.com (2603:10b6:5:21f::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9388.9; Sat, 6 Dec 2025 10:18:04 +0000 Received: from MN1PEPF0000F0DE.namprd04.prod.outlook.com (2603:10b6:208:e8:cafe::11) by MN2PR20CA0010.outlook.office365.com (2603:10b6:208:e8::23) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9388.12 via Frontend Transport; Sat, 6 Dec 2025 10:18:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by MN1PEPF0000F0DE.mail.protection.outlook.com (10.167.242.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9412.4 via Frontend Transport; Sat, 6 Dec 2025 10:18:04 +0000 Received: from BLR-L-BHARARAO.amd.com (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.17; Sat, 6 Dec 2025 04:17:57 -0600 From: Bharata B Rao To: , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , Bharata B Rao Subject: [RFC PATCH v4 7/9] mm: klruscand: use mglru scanning for page promotion Date: Sat, 6 Dec 2025 15:44:21 +0530 Message-ID: <20251206101423.5004-8-bharata@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251206101423.5004-1-bharata@amd.com> References: <20251206101423.5004-1-bharata@amd.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN1PEPF0000F0DE:EE_|DM6PR12MB4123:EE_ X-MS-Office365-Filtering-Correlation-Id: ce9f85ea-b85f-456b-b5ca-08de34b0be65 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|82310400026|376014|7416014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?0/e/Qga7mE/sQ24p0r1+KdNnCvn2GEX9JTPbB7sGs/P4dtqGz0srEwqC2NfC?= =?us-ascii?Q?Y4N+u8o4d/faOAJwbpd9d6YV+cCvlhjAF3d+sQgxmAnRFusUcIe1O5AwDoNe?= =?us-ascii?Q?ZmhWVrtGOR4ovc8gXsquxsQUtM6ro8eI9GqzY3cFSyFUaFCy6a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sbww7Fq61+PT6ZJ/8YAiZi66qfPGV5CRYG93q4vzD9b2zKW?= =?us-ascii?Q?haNvmK79lV/seZ1+WRBNFuzQMYpKirgB8g7vg7pGdNgtNbUiyOFILzSKAoCu?= =?us-ascii?Q?iqfWn/A1K/FKVfBeMrnZYfoFZ6z7X6Xjq1ZkPHb8ZcNhAyNJ2LfxjVbXJqco?= =?us-ascii?Q?9o4vM/tf5c470i8xXsvkqfYPHejACgtcyXbpL0qDtLQw6nja3ln6u+58oEJm?= =?us-ascii?Q?+TxvD19Q9VQNBh6kz5yhXWmyKCTdDd4Dw/JkWHXBKrU2VLnK+/rSp1X254Kn?= =?us-ascii?Q?byVHu7pMbgkSIGn/7R4oAW/sKY9DTbmAeFVWxQmBXw/C5e8lAIKdlB1E2HZO?= =?us-ascii?Q?6CgxB8jplIsGYQBtLkYyAA17KIJ1J98lF9MHhd5o6/JBvAtTKxwPhypybpD/?= =?us-ascii?Q?GEZaDXTgmCBjXi/DkqVqrH+AylqfYgkuvZ2QwEo/XYJ31jKsWxSXkL+96hWM?= =?us-ascii?Q?roahMDc5V0/pCXRuqEythNwBGnY50wuBQohbj0w7e3Hlqwvy7BAsTQGjZnWU?= =?us-ascii?Q?IJ+qrbkeYdskokUf1Ao=3D?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700013)(1800799024)(82310400026)(376014)(7416014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Dec 2025 10:18:04.6054 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ce9f85ea-b85f-456b-b5ca-08de34b0be65 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: MN1PEPF0000F0DE.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4123 Content-Type: text/plain; charset="utf-8" From: Kinsey Ho Introduce a new kernel daemon, klruscand, that periodically invokes the MGLRU page table walk. It leverages the new callbacks to gather access information and forwards it to pghot sub-system for promotion decisions. This benefits from reusing the existing MGLRU page table walk infrastructure, which is optimized with features such as hierarchical scanning and bloom filters to reduce CPU overhead. As an additional optimization to be added in the future, we can tune the scan intervals for each memcg. Signed-off-by: Kinsey Ho Signed-off-by: Yuanchu Xie [Reduced the scan interval to 500ms, KLRUSCAND to default n in config] Signed-off-by: Bharata B Rao --- mm/Kconfig | 8 ++++ mm/Makefile | 1 + mm/klruscand.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 mm/klruscand.c diff --git a/mm/Kconfig b/mm/Kconfig index 6057c96b9cd3..80221594413d 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1403,6 +1403,14 @@ config HWMEM_PROFILER rolled up to PGHOT sub-system for further action like hot page promotion or NUMA Balancing =20 +config KLRUSCAND + bool "Kernel lower tier access scan daemon" + default n + depends on PGHOT && LRU_GEN_WALKS_MMU + help + Scan for accesses from lower tiers by invoking MGLRU to perform + page table walks. + source "mm/damon/Kconfig" =20 endmenu diff --git a/mm/Makefile b/mm/Makefile index a6fac171c36e..1c0c79fec106 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -147,3 +147,4 @@ obj-$(CONFIG_EXECMEM) +=3D execmem.o obj-$(CONFIG_TMPFS_QUOTA) +=3D shmem_quota.o obj-$(CONFIG_PT_RECLAIM) +=3D pt_reclaim.o obj-$(CONFIG_PGHOT) +=3D pghot.o +obj-$(CONFIG_KLRUSCAND) +=3D klruscand.o diff --git a/mm/klruscand.c b/mm/klruscand.c new file mode 100644 index 000000000000..13a41b38d67d --- /dev/null +++ b/mm/klruscand.c @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include +#include + +#include "internal.h" + +#define KLRUSCAND_INTERVAL 500 +#define BATCH_SIZE (2 << 16) + +static struct task_struct *scan_thread; +static unsigned long pfn_batch[BATCH_SIZE]; +static int batch_index; + +static void flush_cb(void) +{ + int i; + + for (i =3D 0; i < batch_index; i++) { + unsigned long pfn =3D pfn_batch[i]; + + pghot_record_access(pfn, NUMA_NO_NODE, PGHOT_PGTABLE_SCAN, jiffies); + + if (i % 16 =3D=3D 0) + cond_resched(); + } + batch_index =3D 0; +} + +static bool accessed_cb(unsigned long pfn) +{ + WARN_ON_ONCE(batch_index =3D=3D BATCH_SIZE); + + if (batch_index < BATCH_SIZE) + pfn_batch[batch_index++] =3D pfn; + + return batch_index =3D=3D BATCH_SIZE; +} + +static int klruscand_run(void *unused) +{ + struct lru_gen_mm_walk *walk; + + walk =3D kzalloc(sizeof(*walk), + __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN); + if (!walk) + return -ENOMEM; + + while (!kthread_should_stop()) { + unsigned long next_wake_time; + long sleep_time; + struct mem_cgroup *memcg; + int flags; + int nid; + + next_wake_time =3D jiffies + msecs_to_jiffies(KLRUSCAND_INTERVAL); + + for_each_node_state(nid, N_MEMORY) { + pg_data_t *pgdat =3D NODE_DATA(nid); + struct reclaim_state rs =3D { 0 }; + + if (node_is_toptier(nid)) + continue; + + rs.mm_walk =3D walk; + set_task_reclaim_state(current, &rs); + flags =3D memalloc_noreclaim_save(); + + memcg =3D mem_cgroup_iter(NULL, NULL, NULL); + do { + struct lruvec *lruvec =3D + mem_cgroup_lruvec(memcg, pgdat); + unsigned long max_seq =3D + READ_ONCE((lruvec)->lrugen.max_seq); + + lru_gen_scan_lruvec(lruvec, max_seq, accessed_cb, flush_cb); + cond_resched(); + } while ((memcg =3D mem_cgroup_iter(NULL, memcg, NULL))); + + memalloc_noreclaim_restore(flags); + set_task_reclaim_state(current, NULL); + memset(walk, 0, sizeof(*walk)); + } + + sleep_time =3D next_wake_time - jiffies; + if (sleep_time > 0 && sleep_time !=3D MAX_SCHEDULE_TIMEOUT) + schedule_timeout_idle(sleep_time); + } + kfree(walk); + return 0; +} + +static int __init klruscand_init(void) +{ + struct task_struct *task; + + task =3D kthread_run(klruscand_run, NULL, "klruscand"); + + if (IS_ERR(task)) { + pr_err("Failed to create klruscand kthread\n"); + return PTR_ERR(task); + } + + scan_thread =3D task; + return 0; +} +module_init(klruscand_init); --=20 2.34.1