From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 915C8C71157 for ; Tue, 24 Jun 2025 05:57:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D3026B00A9; Tue, 24 Jun 2025 01:57:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 282D86B00AA; Tue, 24 Jun 2025 01:57:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 14C286B00AC; Tue, 24 Jun 2025 01:57:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 00DFD6B00A9 for ; Tue, 24 Jun 2025 01:57:08 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7332C1214CB for ; Tue, 24 Jun 2025 05:57:08 +0000 (UTC) X-FDA: 83589236136.29.92BD783 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2059.outbound.protection.outlook.com [40.107.212.59]) by imf09.hostedemail.com (Postfix) with ESMTP id B6672140004 for ; Tue, 24 Jun 2025 05:57:05 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3j9zdA5e; spf=pass (imf09.hostedemail.com: domain of Raghavendra.KodsaraThimmappa@amd.com designates 40.107.212.59 as permitted sender) smtp.mailfrom=Raghavendra.KodsaraThimmappa@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1750744625; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hglcXND/1hQIbXGjSGlRxrAVs0gQHvtkwFHzUUuRDYI=; b=tcsnnV0LEyqCVEdFIEQOkmtRpXXlBiRtIQVwbFXvGMbWVmnoJL7YqGr4koc1Xr9U4mEYx+ o8G+nVwrBflxq5bJ974WhjySiN4s7q3iEhB1FIoZOX245HdfURm6WczbJGms8pevwl3TYe F4b6URtL/9A/CMi7B+S1gC78RVbWaEo= ARC-Authentication-Results: i=2; imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3j9zdA5e; spf=pass (imf09.hostedemail.com: domain of Raghavendra.KodsaraThimmappa@amd.com designates 40.107.212.59 as permitted sender) smtp.mailfrom=Raghavendra.KodsaraThimmappa@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1750744625; a=rsa-sha256; cv=pass; b=QJsKT/vMEG4GL3wZyA1muGjqiSyqmlgIz3LlmBvoqSKxkYEC/9fwz69Vta8aK+RAk33hZ7 ivZCy4U74MD7opRUKAaDULcCmjpAfJhu1sts9v66nH0VoSq+QfUvSHQGG5Hi4Tdj1IMbD8 gQjqZc0ZQYulsbIj4pyi8M1BJ1oKK0I= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=PtIRBR2/Gi5Y6FDtq+3XQEBbxzqOJ876Xa4VMKSN8PWzkYY8tXp+seXPABckTuekXMRPWpDSNX7XkzHx/TCN07QEwrYHjCEcqjbt7tCkHPbuoof9pa7i6CZI6Jm2IRbR1NDvlXjJeJVQuVTb5lqGCxrti+PhTpD9XQe5Z+VdK4egKVCzIrQZqJW5eSDpL4NHjSvCvz+3O5Z4rhC357whoXIloayhCv1henTM3Lh8ZBJAi4BgBrA2kI4tlHZzYNApRrf9RSA7pKQNu6s0t0BqacbsNq0FtoO0NdNGQTqjTAQTDWVHMgscvjDPg61v/cxgjrHt3D68qxJ6Q1tbqaR1Ig== 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=hglcXND/1hQIbXGjSGlRxrAVs0gQHvtkwFHzUUuRDYI=; b=H2LUO9RDmIvRlQ6likFOoupMalR0dH8eXZ0RnnJZd6I6Mc/0N3W/8Mem0xB+lNcyytBUbUqvkB/1I2eI9BPbF2HGzthfLGNTJm/Ds0BHe0PsBfyRG7V5JU374zQ7+2CrbkaW0KQCdRSJBl9Czee8VVAVTcoBT8vHPuhPbh1msQbe4u0rI0GU/9JLdFzLcEZu+O+sebEVRatVhU7WWjasgb3kUNU9MgTUoh7nOkWwtgmowBb/CaWGLWqsO5oc35JRtYacRQtkk8hbr0+tOmqSlul2tt92EezLS1KThWLJ5LGjDUgUTLKGjETgDXGW/Nx2FZBv4jEKeKD7yTjChDU+Uw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=arm.com 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=hglcXND/1hQIbXGjSGlRxrAVs0gQHvtkwFHzUUuRDYI=; b=3j9zdA5e4CkPTCimLti1pqQhJc2Gl/Ae1g4p41aXZtqQLTtY3eANj5+7DicnESsC3ohsJjLEYFK4n+xoMDLJiaaKdWMw70GorsJfFMR1yulIocwuRTC4V4n38+VT9RNfsZJSnSI0o+CFbsqoMI+ScpQBzo4MPLdPmkjGZfGCA7M= Received: from BN9PR03CA0169.namprd03.prod.outlook.com (2603:10b6:408:f4::24) by CY1PR12MB9604.namprd12.prod.outlook.com (2603:10b6:930:108::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8857.28; Tue, 24 Jun 2025 05:57:00 +0000 Received: from BN1PEPF00006002.namprd05.prod.outlook.com (2603:10b6:408:f4:cafe::fe) by BN9PR03CA0169.outlook.office365.com (2603:10b6:408:f4::24) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8857.30 via Frontend Transport; Tue, 24 Jun 2025 05:57:00 +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=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BN1PEPF00006002.mail.protection.outlook.com (10.167.243.234) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8880.14 via Frontend Transport; Tue, 24 Jun 2025 05:57:00 +0000 Received: from tunga.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 24 Jun 2025 00:56:49 -0500 From: Raghavendra K T To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC PATCH V2 01/13] mm: Add kscand kthread for PTE A bit scan Date: Tue, 24 Jun 2025 05:56:05 +0000 Message-ID: <20250624055617.1291159-2-raghavendra.kt@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250624055617.1291159-1-raghavendra.kt@amd.com> References: <20250624055617.1291159-1-raghavendra.kt@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN1PEPF00006002:EE_|CY1PR12MB9604:EE_ X-MS-Office365-Filtering-Correlation-Id: d6ad5650-416b-4b1d-99ef-08ddb2e3ef9b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|376014|82310400026|7416014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?Byw9aLpx2wPmNIAXmc6rLjZLr1GTfNzk/9vMW9JpL60l82Us8AjjONfOotR+?= =?us-ascii?Q?oVhZ5i0arDyMhcwspKRGeWi0uo3Jha3kuwbjrt6bQNMEcBr0C0pYw+yuKyrk?= =?us-ascii?Q?FFyVyBKQyiwlB4GlvtS7gu6Cj1238vya2dncqL7ngpKfELP7B13g6wbv/ze7?= =?us-ascii?Q?yhsIlagU0jA+aPCToK7qpZwSwsswtvnTqpg8p6j8OsSwnPlM1YAuptRUFXKV?= =?us-ascii?Q?5pLpiDAtJWkGUFyerZBqJi3NjY2z5jPZacZwNHvaDfMiU4rYKAHiNoFgVuc3?= =?us-ascii?Q?yS5p9h9asvxILIlGcfp4Z0RO2ziFuou5sxvIUWsk5UXSLuqtCd/htvoRpiha?= =?us-ascii?Q?PJoTgP/owZLXANXMmJKRv3TO7qYvQzhWKa8NqevYApyT23QWwZxyvIRXs/GS?= =?us-ascii?Q?t7W2dtp9nyf16ZERWLliUR3DtNNt414rwUUiZDuvNkhxc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ZuxPSdSiPlt4zpQ039nNJsyxdMxzmLH121YSRAgb3TRscuT7KY6H58uA3p?= =?us-ascii?Q?jJhocTwuGz5YwNvxuJcUbG4PlgG/2xI54p5u3hWLbesT0ebYVT/EYohWdDKv?= =?us-ascii?Q?TU4hWQ8XXhMgrZaR3eq6BXbpeD2qCu/Kuj4ok7d1AnBqmWu72DqfIQDDRu4p?= =?us-ascii?Q?AUv87WhyBq9sjhoHoHBhaPfJiR8l3HPZd/Uw?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700013)(1800799024)(376014)(82310400026)(7416014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jun 2025 05:57:00.3331 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d6ad5650-416b-4b1d-99ef-08ddb2e3ef9b 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=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN1PEPF00006002.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR12MB9604 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: B6672140004 X-Stat-Signature: qe9ydt8iydzzs3o85ro9cf7dca8qyoz4 X-Rspam-User: X-HE-Tag: 1750744625-899034 X-HE-Meta: U2FsdGVkX19rUtaYY6OkX/54/X4xyKgNw70IJW3jFLWD+7xDozRgG7bTb9x518iLOJCL+FnnGpEb0a9W20s+bdnZ5PTQsXHN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yhBdaFyAlUvk3iqb1IVXoW4qR4YeU8P3sEeT2yr4EeCzy6ZApSPfE45oZ3LGbw58Jzzwtm317+AOwNM0mJo0WS5XaPC7mconloYvHxw8bvghE7ue7EFFUUZ1t7w236aR4kw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Also add a config option for the same. High level design: While (1): Scan the slowtier pages belonging to VMAs of a task. Add to migation list. A separate thread: Migrate scanned pages to a toptier node based on heuristics. The overall code is influenced by khugepaged design. Signed-off-by: Raghavendra K T --- mm/Kconfig | 8 +++ mm/Makefile | 1 + mm/kscand.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 mm/kscand.c diff --git a/mm/Kconfig b/mm/Kconfig index e113f713b493..062fa6bdf3fb 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -746,6 +746,14 @@ config KSM until a program has madvised that an area is MADV_MERGEABLE, and root has set /sys/kernel/mm/ksm/run to 1 (if CONFIG_SYSFS is set). +config KSCAND + bool "Enable PTE A bit scanning and Migration" + depends on NUMA_BALANCING + help + Enable PTE A bit scanning of page. The option creates a separate + kthread for scanning and migration. Accessed slow-tier pages are + migrated to a regular NUMA node to reduce hot page access latency. + config DEFAULT_MMAP_MIN_ADDR int "Low address space to protect from user allocation" depends on MMU diff --git a/mm/Makefile b/mm/Makefile index e7f6bbf8ae5f..f5e02d6fb1bc 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -97,6 +97,7 @@ obj-$(CONFIG_FAIL_PAGE_ALLOC) += fail_page_alloc.o obj-$(CONFIG_MEMTEST) += memtest.o obj-$(CONFIG_MIGRATION) += migrate.o obj-$(CONFIG_NUMA) += memory-tiers.o +obj-$(CONFIG_KSCAND) += kscand.o obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o obj-$(CONFIG_PAGE_COUNTER) += page_counter.o diff --git a/mm/kscand.c b/mm/kscand.c new file mode 100644 index 000000000000..f7bbbc70c86a --- /dev/null +++ b/mm/kscand.c @@ -0,0 +1,163 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "internal.h" + +static struct task_struct *kscand_thread __read_mostly; +static DEFINE_MUTEX(kscand_mutex); + +/* How long to pause between two scan cycles */ +static unsigned int kscand_scan_sleep_ms __read_mostly = 20; + +/* Max number of mms to scan in one scan cycle */ +#define KSCAND_MMS_TO_SCAN (4 * 1024UL) +static unsigned long kscand_mms_to_scan __read_mostly = KSCAND_MMS_TO_SCAN; + +bool kscand_scan_enabled = true; +static bool need_wakeup; + +static unsigned long kscand_sleep_expire; + +static DECLARE_WAIT_QUEUE_HEAD(kscand_wait); + +/* Data structure to keep track of current mm under scan */ +struct kscand_scan { + struct list_head mm_head; +}; + +struct kscand_scan kscand_scan = { + .mm_head = LIST_HEAD_INIT(kscand_scan.mm_head), +}; + +static inline int kscand_has_work(void) +{ + return !list_empty(&kscand_scan.mm_head); +} + +static inline bool kscand_should_wakeup(void) +{ + bool wakeup = kthread_should_stop() || need_wakeup || + time_after_eq(jiffies, kscand_sleep_expire); + + need_wakeup = false; + + return wakeup; +} + +static void kscand_wait_work(void) +{ + const unsigned long scan_sleep_jiffies = + msecs_to_jiffies(kscand_scan_sleep_ms); + + if (!scan_sleep_jiffies) + return; + + kscand_sleep_expire = jiffies + scan_sleep_jiffies; + + /* Allows kthread to pause scanning */ + wait_event_timeout(kscand_wait, kscand_should_wakeup(), + scan_sleep_jiffies); +} +static void kscand_do_scan(void) +{ + unsigned long iter = 0, mms_to_scan; + + mms_to_scan = READ_ONCE(kscand_mms_to_scan); + + while (true) { + if (unlikely(kthread_should_stop()) || + !READ_ONCE(kscand_scan_enabled)) + break; + + if (kscand_has_work()) + msleep(100); + + iter++; + + if (iter >= mms_to_scan) + break; + cond_resched(); + } +} + +static int kscand(void *none) +{ + while (true) { + if (unlikely(kthread_should_stop())) + break; + + while (!READ_ONCE(kscand_scan_enabled)) { + cpu_relax(); + kscand_wait_work(); + } + + kscand_do_scan(); + + kscand_wait_work(); + } + return 0; +} + +static int start_kscand(void) +{ + struct task_struct *kthread; + + guard(mutex)(&kscand_mutex); + + if (kscand_thread) + return 0; + + kthread = kthread_run(kscand, NULL, "kscand"); + if (IS_ERR(kscand_thread)) { + pr_err("kscand: kthread_run(kscand) failed\n"); + return PTR_ERR(kthread); + } + + kscand_thread = kthread; + pr_info("kscand: Successfully started kscand"); + + if (!list_empty(&kscand_scan.mm_head)) + wake_up_interruptible(&kscand_wait); + + return 0; +} + +static int stop_kscand(void) +{ + guard(mutex)(&kscand_mutex); + + if (kscand_thread) { + kthread_stop(kscand_thread); + kscand_thread = NULL; + } + + return 0; +} + +static int __init kscand_init(void) +{ + int err; + + err = start_kscand(); + if (err) + goto err_kscand; + + return 0; + +err_kscand: + stop_kscand(); + + return err; +} +subsys_initcall(kscand_init); -- 2.34.1