From nobody Tue Oct 7 15:57:25 2025 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2080.outbound.protection.outlook.com [40.107.96.80]) (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 F160B28BAB5; Wed, 9 Jul 2025 10:05:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.96.80 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752055556; cv=fail; b=QANUH87ZDcj0JxwY6qFvCElFOhcNnskZp/YxQFLeMY73ot2PAZ7iCWA6dZGWvKkAdPMatV1IgCsgnTwS8cscAZhGv2PhebyInQkJJkG3k/H/ejzje/SpRr8uNuuGg50W7Pv/6sKFS8Ns2OdQf/Hq8PdR04XIFy0UjS7IG0b7Brc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752055556; c=relaxed/simple; bh=gxCKMvwPA8vcVBQChOLDDUjBgyF0rI7YkuL8BkQ40zg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=n/GdXCDyle1l/nGO1yJL2Bwa2tX8iYOHyUiHSJ3qdYRqiRZKnlHTYLp1SytvqcNDmL9snk35NDEETuabBkwxK50s0dSwohdCY+q0v/bkl1U1fpe6XyFgc6SIKi6pIq1x+9YcUMBs3QEGA9NH5Z5LHNXjVuXpsACo8OvOsSp2aYQ= 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=zBz8+s11; arc=fail smtp.client-ip=40.107.96.80 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="zBz8+s11" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ZDragFtfPiLTk/SxBSgxyAK4MfNy7W0JtigIeYRKRc9oEhHFaqfF6kvoZeT6Ykdc8nsGWuGQueIzh/8yV7xugB0mdXJbt3QEI+pQOfrwcZDGszPGR5Qt0fenuYl+fOsvAJJrwUsZJO3Y7VNLWfklg7tFchlBGC6AIBUNEo+gR8ncE/qoWr3MrxMd12WCf04KQ9VWBpHKIEJ1P7w17BVHCAVVZkBmwEg29qNxlbg0CeY33zhgQpvPTMu4DGhCMlkqNjfTyKiqo7ip6u73+ZorXsT1Cn8yUFyxUmnlcdVldgrW76LxBNNHdIiQ1WyCmqdhCvu1T4DLF221SWCP9pwaPA== 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=iPUolXeLQdrBVInU0Qawb5A+BH4a310feGspIdffEug=; b=BnIMYZvLPApaNZL/apQKtPfjUcFhOAOepOw5yG3SmZng3wixEaqstpIga8xFKiuLl0a2kddarb4Z7UtYhN5mcXqK6cRyw4biPkC9jO4s8yzzBQBTYV2+YvsGRUuPe0zfbNdfH/33JJtvmRWXsY18xnzgcLmvRu+P/+4wamgAKnUaUbwmrjR9SDRjn0oLLLWRYDlfoYURrcghMxx0BYuePz+aDkHa9YjxB8IwEPX7BmixnRIE1sedr1FQAsfU2oLWft1n5LUWHGkZMe63w2dlcRhWLy+H4gRv9B9uY5nJjc7mYsdaMXX0OXc28lCMBrCTHIaRjzuO5/+D74HR3xl55g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=iPUolXeLQdrBVInU0Qawb5A+BH4a310feGspIdffEug=; b=zBz8+s117C9dylCxk8vpgLcbgqyi1s1HiiqVpdA52b3Fu6lIxfaFDOnnVjOoTPEKwVu70rgKteHvass12rGyntBnafeOim+IM3q88La8MjdP6HphtufQVgLcMp2dT78HejG/ANWkpMyYwqd38ylFRGyXsYIMScXPkHElOnxAC88= Received: from SA9P223CA0013.NAMP223.PROD.OUTLOOK.COM (2603:10b6:806:26::18) by PH7PR12MB6833.namprd12.prod.outlook.com (2603:10b6:510:1af::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8901.26; Wed, 9 Jul 2025 10:05:48 +0000 Received: from SA2PEPF00003AE5.namprd02.prod.outlook.com (2603:10b6:806:26:cafe::c) by SA9P223CA0013.outlook.office365.com (2603:10b6:806:26::18) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8922.22 via Frontend Transport; Wed, 9 Jul 2025 10:05:48 +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=SATLEXMB03.amd.com; pr=C Received: from SATLEXMB03.amd.com (165.204.84.17) by SA2PEPF00003AE5.mail.protection.outlook.com (10.167.248.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8901.15 via Frontend Transport; Wed, 9 Jul 2025 10:05:48 +0000 Received: from SATLEXMB06.amd.com (10.181.40.147) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Wed, 9 Jul 2025 05:05:47 -0500 Received: from SATLEXMB03.amd.com (10.181.40.144) by SATLEXMB06.amd.com (10.181.40.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Wed, 9 Jul 2025 05:05:47 -0500 Received: from hjbog-srdc-41.amd.com (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server id 15.1.2507.39 via Frontend Transport; Wed, 9 Jul 2025 05:05:41 -0500 From: Samuel Zhang To: , , , , , , , , , , , , , , CC: , , , , , , , , , , "Samuel Zhang" Subject: [PATCH v5 3/5] PM: hibernate: shrink shmem pages after dev_pm_ops.prepare() Date: Wed, 9 Jul 2025 18:05:10 +0800 Message-ID: <20250709100512.3762063-4-guoqing.zhang@amd.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250709100512.3762063-1-guoqing.zhang@amd.com> References: <20250709100512.3762063-1-guoqing.zhang@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-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA2PEPF00003AE5:EE_|PH7PR12MB6833:EE_ X-MS-Office365-Filtering-Correlation-Id: 0e45c198-6233-4c32-d577-08ddbed02d70 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|36860700013|376014|7416014|82310400026|7053199007|921020; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?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O1Qx2vrXUKVz?= =?us-ascii?Q?y3xbn/ct04ouXNEynwnguVC+npsRioed7b4RrFrAYua+Aqe8p8lCf10py2pg?= =?us-ascii?Q?2etiXQZiXhWlUEy4FGGRsA7+8Ay1jEjetl8cB8op2pYh2+OECcgrjO4tlGwz?= =?us-ascii?Q?1pCfvKdGU15ZBxhoyFzMTPkbYz50r0hj8ay6ofU4MNz58uWbAFnH4lcpzFhy?= =?us-ascii?Q?Vb2i1o9I68QR0YWRps1S2ancUN893T1dZSSngFGunz8Bw7vJFl1jF+brnr0D?= =?us-ascii?Q?aLmnhckjPqApcJ//hWj29CaWGSmQv+hNHJJTAzLlBjt++AWNAuDAZjG1cR2W?= =?us-ascii?Q?1zwOwukIjFgYXf6QbahLLahUiJm89Yo6EhX870CaI2ytSKjOcXB/Y+MzhZWr?= =?us-ascii?Q?jN6qMAWlgUbwWnMaa590xSXE5QRiMk/84aYXjqYPP4ZMDuEbO6hDR+9jV2aq?= =?us-ascii?Q?sit49abGdJAWvDvtaiNn/dSxtC6PFJNUmG8OSpdoZEC7VixxuzInAQpuxkIL?= =?us-ascii?Q?+kBERAmxvlqFEE/CJWksFqao8Glq2qNCg4FyNCKzEPEtu481hbsZNDG2hbKf?= =?us-ascii?Q?KVuMNEJjMd+4H6aSBc2circVEfVESVKFz6r6t3Kdy2+QdhOsBpLpLk1Jwrq2?= =?us-ascii?Q?+fQTqaAV6E76DT08UUrlN4KZGYeqvfUJ0o29tK6jqfC+arbZ7AnCA6GZfA?= =?us-ascii?Q?=3D=3D?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(36860700013)(376014)(7416014)(82310400026)(7053199007)(921020);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jul 2025 10:05:48.0585 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0e45c198-6233-4c32-d577-08ddbed02d70 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=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SA2PEPF00003AE5.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6833 Content-Type: text/plain; charset="utf-8" When hibernate with data center dGPUs, huge number of VRAM data will be moved to shmem during dev_pm_ops.prepare(). These shmem pages take a lot of system memory so that there's no enough free memory for creating the hibernation image. This will cause hibernation fail and abort. After dev_pm_ops.prepare(), call shrink_all_memory() to force move shmem pages to swap disk and reclaim the pages, so that there's enough system memory for hibernation image and less pages needed to copy to the image. This patch can only flush and free about half shmem pages. It will be better to flush and free more pages, even all of shmem pages, so that there're less pages to be copied to the hibernation image and the overall hibernation time can be reduced. Signed-off-by: Samuel Zhang Acked-by: Rafael J. Wysocki --- kernel/power/hibernate.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c index 10a01af63a80..7ae9d9a7aa1d 100644 --- a/kernel/power/hibernate.c +++ b/kernel/power/hibernate.c @@ -370,6 +370,23 @@ static int create_image(int platform_mode) return error; } =20 +static void shrink_shmem_memory(void) +{ + struct sysinfo info; + unsigned long nr_shmem_pages, nr_freed_pages; + + si_meminfo(&info); + nr_shmem_pages =3D info.sharedram; /* current page count used for shmem */ + /* + * The intent is to reclaim all shmem pages. Though shrink_all_memory() c= an + * only reclaim about half of them, it's enough for creating the hibernat= ion + * image. + */ + nr_freed_pages =3D shrink_all_memory(nr_shmem_pages); + pr_debug("requested to reclaim %lu shmem pages, actually freed %lu pages\= n", + nr_shmem_pages, nr_freed_pages); +} + /** * hibernation_snapshot - Quiesce devices and create a hibernation image. * @platform_mode: If set, use platform driver to prepare for the transiti= on. @@ -411,6 +428,15 @@ int hibernation_snapshot(int platform_mode) goto Thaw; } =20 + /* + * Device drivers may move lots of data to shmem in dpm_prepare(). The sh= mem + * pages will use lots of system memory, causing hibernation image creati= on + * fail due to insufficient free memory. + * This call is to force flush the shmem pages to swap disk and reclaim + * the system memory so that image creation can succeed. + */ + shrink_shmem_memory(); + suspend_console(); pm_restrict_gfp_mask(); =20 --=20 2.43.5