From nobody Sat Oct 4 17:30:19 2025 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2081.outbound.protection.outlook.com [40.107.220.81]) (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 5217617AE1D for ; Thu, 14 Aug 2025 15:34:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.81 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755185672; cv=fail; b=Jv/Q4iila+lf1VkSo1LWITEBTlUNFy4H15m6pwlQaOOMXrug/COC6lC2Wp8swelxd+hDbdNOv7yfVUgnJxDaPlRrh3tMixc7AY3r4m92awSNrQEOmZmXpVP+FynnzxpsNIjTBChtMi2E9FTz5lj2y6nnI27mtCW5rptNsGbz0HI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755185672; c=relaxed/simple; bh=tNB6puLZUATBUCN/EA1kKkFaLOe95pDtLDXQK2yw2TY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Xuavqf3cClxx+telmp19UhHYIVAj6gSMBd1UoMXU2kQgBQOmx3CPT8XfiB4I76gmBpZ82fXvcbWTq/Ddb1NJNECNmiP8IJrDDAeV0w0DoEHshOeR5XUi7dU7KjK3TWO/dU/yAOUah2CC7JtV00tJDiV6y+mr9QELWzKC8TC3oaQ= 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=YgA16R4/; arc=fail smtp.client-ip=40.107.220.81 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="YgA16R4/" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=H9Ey9ore+VT8m/tiRLdr9s9RjnR+NuNvTofr4/ti2lm2Q8op4LtHg6pt47S0wbirTdJfU8XogSmOyquZm8RLoadrffkMA7BSEdc6uLEod07s7k4cIxHi5qJE3krLUI4arpM9sJY6VLtrtPvpNBn+H+lQES8hshpcb8WNIyTdTXXmaJCwadxY4tdzpIXCgyetgW6wgPWmc3AAA9YrUQ/bhH3KLVvklEmBTZjH6RR/P9WDvxidzX2YNCqMUDk1hJfOzOxSl6h2ePcqubm8+dpDcEJ2xi5jPmmIo61RUlZeIuQAK7MKoQQjA7UftTdVhz4DdGIUTjsKpq3MkzoXmhKZbA== 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=/6yUiVOmVxuU72JxpsJZhSRgH6o0kl8/xMLa+WIVrPk=; b=nBCUIfIrymmXSYygrDNuwMAKp12HR5QuDjCKUnUxDT5X9tTFA7cjRBhPMtltkdpR3cwrz/mtgT+o5U0HbPFdIe3cyGZ/2LBNJ5XYpM1N5vSjir8yYxVPTar6T0hhkm9/pURWxWzw2bYhVhocmOxSHbWjDayel3vkvLHyHijTyQmZKd5Q15eG9ndrHUMnyYJjJtIvGg4ZkWCJ0VOBv8K8pV1KdRjTRg42CSSEBP2u3+dC+ZYR3hB773+WgPgpMYPVt9HRe0juOpFfXMX/D3Svy6ywlB7ligyuwPBJJLLcepLO1isFrnp1+Bzj0HHa0MKajsvVoPm8q2aMjzCtG3ATZQ== 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=/6yUiVOmVxuU72JxpsJZhSRgH6o0kl8/xMLa+WIVrPk=; b=YgA16R4/mFSD+z0Vn0U3EaQ0aJmWod5efqhDPWiK8Jjmi5Yfa48AycQX9wtV68Nga6Ji9g1MAAtlNVFVC+VnOmZUdkRjrLpFm8RgbsKMxnVY+p49cLyLDlznKgMlrhoQ28kwkqvJ1ojbOIV0BT2Sx7r8gq3Mw0qNh0Jj1JQd1xo= Received: from BN9PR03CA0475.namprd03.prod.outlook.com (2603:10b6:408:139::30) by DS0PR12MB7748.namprd12.prod.outlook.com (2603:10b6:8:130::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9031.15; Thu, 14 Aug 2025 15:34:27 +0000 Received: from BN3PEPF0000B072.namprd04.prod.outlook.com (2603:10b6:408:139:cafe::84) by BN9PR03CA0475.outlook.office365.com (2603:10b6:408:139::30) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9031.18 via Frontend Transport; Thu, 14 Aug 2025 15:34:27 +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 BN3PEPF0000B072.mail.protection.outlook.com (10.167.243.117) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.9031.11 via Frontend Transport; Thu, 14 Aug 2025 15:34:26 +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; Thu, 14 Aug 2025 10:34:14 -0500 From: Raghavendra K T To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC PATCH V3 05/17] mm: Create a separate kthread for migration Date: Thu, 14 Aug 2025 15:32:55 +0000 Message-ID: <20250814153307.1553061-6-raghavendra.kt@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250814153307.1553061-1-raghavendra.kt@amd.com> References: <20250814153307.1553061-1-raghavendra.kt@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: 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: BN3PEPF0000B072:EE_|DS0PR12MB7748:EE_ X-MS-Office365-Filtering-Correlation-Id: 0ecad30c-670c-40ac-5238-08dddb480db2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|7416014|82310400026|36860700013; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?Py1TkQi0/UjwyEVgoUhf3vHIx0vjwvzOW5sVwZAmRiVxQyrawxkHrAytR5d5?= =?us-ascii?Q?KfGLd2kPDyXQKZR5DFJ2cHXmfrwaC4RrFe9EGYV82enaY6SFTpvGjko1G4nl?= =?us-ascii?Q?o/X+2rhw/9Xhgm0dibSEL5FXN/BjVR17LdGy7Huc1RJVpFvi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UgexNIfZK65DXeZyNt9DjkGKRwBgsCMWyELeDgnrEo6lZ/xLq4?= =?us-ascii?Q?8Rd02PCFEeGlK0ei2NYxKZbQTQK7PzhXuqUorka6/qkdCxn/kwYtL74cLE1E?= =?us-ascii?Q?hk3GOsr/LauwrugO8DQE4VntyaNph7OdBVHI622u8qXicfOcIDEh0FM41nl7?= =?us-ascii?Q?YSzbw0+EtgCBDNv1yhJOjh0Cgzr851DXR5QlwqMNkJctZ/3S1Sk9BAK2YnK2?= =?us-ascii?Q?g89nlWhHMBZfsgVkSvYWrlDQk437zsMqS8DfHgM5DL0fiMSGa9w+AS0L35Ny?= =?us-ascii?Q?nreHpTkiHx04xgeSQ9eU1whnC0vSf0ky5YDp6Qgqtx44RwyHNsBk1mmBx+BN?= =?us-ascii?Q?UcZrdq34JXBC4FHuvf7M8FkoMiGuHaiTZ3ColrF3np3QiuP3Rz/vopOzoRAI?= =?us-ascii?Q?1qeXGvaqFJ5RsUEUW6NNmsj5Sossa/nO1iEol2Y5bz8NoU0/uKdR3lyVZ/T6?= =?us-ascii?Q?e70ZensNJYXuO7gj5kTR9v1wJKxd+njdPqXb?= 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)(1800799024)(376014)(7416014)(82310400026)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2025 15:34:26.9960 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0ecad30c-670c-40ac-5238-08dddb480db2 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: BN3PEPF0000B072.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB7748 Content-Type: text/plain; charset="utf-8" Having independent thread helps in: - Alleviating the need for multiple scanning threads - Aids to control batch migration (TBD) - Migration throttling (TBD) Signed-off-by: Raghavendra K T --- mm/kscand.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/mm/kscand.c b/mm/kscand.c index 7552ce32beea..55efd0a6e5ba 100644 --- a/mm/kscand.c +++ b/mm/kscand.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +42,15 @@ static unsigned long kscand_mms_to_scan __read_mostly = =3D KSCAND_MMS_TO_SCAN; =20 bool kscand_scan_enabled =3D true; static bool need_wakeup; +static bool migrated_need_wakeup; + +/* How long to pause between two migration cycles */ +static unsigned int kmigrate_sleep_ms __read_mostly =3D 20; + +static struct task_struct *kmigrated_thread __read_mostly; +static DEFINE_MUTEX(kmigrated_mutex); +static DECLARE_WAIT_QUEUE_HEAD(kmigrated_wait); +static unsigned long kmigrated_sleep_expire; =20 static unsigned long kscand_sleep_expire; =20 @@ -79,6 +89,7 @@ struct kscand_scanctrl { }; =20 struct kscand_scanctrl kscand_scanctrl; + /* Per folio information used for migration */ struct kscand_migrate_info { struct list_head migrate_node; @@ -134,6 +145,19 @@ static inline bool is_valid_folio(struct folio *folio) return true; } =20 +static inline void kmigrated_wait_work(void) +{ + const unsigned long migrate_sleep_jiffies =3D + msecs_to_jiffies(kmigrate_sleep_ms); + + if (!migrate_sleep_jiffies) + return; + + kmigrated_sleep_expire =3D jiffies + migrate_sleep_jiffies; + wait_event_timeout(kmigrated_wait, + true, + migrate_sleep_jiffies); +} =20 static bool folio_idle_clear_pte_refs_one(struct folio *folio, struct vm_area_struct *vma, @@ -537,6 +561,49 @@ static int stop_kscand(void) return 0; } =20 +static int kmigrated(void *arg) +{ + while (true) { + WRITE_ONCE(migrated_need_wakeup, false); + if (unlikely(kthread_should_stop())) + break; + msleep(20); + kmigrated_wait_work(); + } + return 0; +} + +static int start_kmigrated(void) +{ + struct task_struct *kthread; + + guard(mutex)(&kmigrated_mutex); + + /* Someone already succeeded in starting daemon */ + if (kmigrated_thread) + return 0; + + kthread =3D kthread_run(kmigrated, NULL, "kmigrated"); + if (IS_ERR(kmigrated_thread)) { + pr_err("kmigrated: kthread_run(kmigrated) failed\n"); + return PTR_ERR(kthread); + } + + kmigrated_thread =3D kthread; + pr_info("kmigrated: Successfully started kmigrated"); + + wake_up_interruptible(&kmigrated_wait); + + return 0; +} + +static int stop_kmigrated(void) +{ + guard(mutex)(&kmigrated_mutex); + kthread_stop(kmigrated_thread); + return 0; +} + static inline void init_list(void) { INIT_LIST_HEAD(&kscand_scanctrl.scan_list); @@ -559,8 +626,15 @@ static int __init kscand_init(void) if (err) goto err_kscand; =20 + err =3D start_kmigrated(); + if (err) + goto err_kmigrated; + return 0; =20 +err_kmigrated: + stop_kmigrated(); + err_kscand: stop_kscand(); kscand_destroy(); --=20 2.34.1