From patchwork Fri Apr 14 12:10:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Yu X-Patchwork-Id: 13210907 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C886BC77B70 for ; Fri, 14 Apr 2023 04:15:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229647AbjDNEOo (ORCPT ); Fri, 14 Apr 2023 00:14:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229636AbjDNEOm (ORCPT ); Fri, 14 Apr 2023 00:14:42 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78414210B; Thu, 13 Apr 2023 21:14:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681445678; x=1712981678; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=cuh+cpF2Dbk1u6YNouEfBinVXPTes5iWxZU0sb3auIQ=; b=bv9IsDIIwzXNAeSTi7h/Cifk/N6LvATgmgHVh6YjKZkPPxPQkQg2DUuQ +f6lua8AKRQtyhGOuv9JrXToeAj8zL+pODSO9eyB61NxU4RGssbgGzJ+7 PZlFAoPIToh2kzXYSgLQYJ596Bkp5p1ZE1YUXHk00ZjaEkxg8IvcG9f/W arVMS19HYnazKf6AktlI7LqNmkqoTo1ealAFztjFzDoD3nHawWanP0bTS 3U8jq8aSCIKW71EXdRCiQMooMBNJPe2kqu7lmUbor9CMDJUZ4VieNvAAH t8cuImj7XVd9QyzqQIbQxIr2ya4uLgb0yiB93UpWavFXS1F8eC2sj6kW8 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10679"; a="333142652" X-IronPort-AV: E=Sophos;i="5.99,195,1677571200"; d="scan'208";a="333142652" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Apr 2023 21:14:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10679"; a="935863717" X-IronPort-AV: E=Sophos;i="5.99,195,1677571200"; d="scan'208";a="935863717" Received: from chenyu-dev.sh.intel.com ([10.239.158.170]) by fmsmga006.fm.intel.com with ESMTP; 13 Apr 2023 21:14:35 -0700 From: Chen Yu To: "Rafael J. Wysocki" , Len Brown Cc: Ye Bin , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Pavankumar Kondeti , Yifan Li , Chen Yu Subject: [PATCH v3 1/2] PM: hibernate: Turn snapshot_test into global variable Date: Fri, 14 Apr 2023 20:10:26 +0800 Message-Id: <5235527bde72c568fb26d268cf3091e8ce080306.1681443714.git.yu.c.chen@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org There is need to check snapshot_test and open block device in different mode, so as to avoid the race condition. No functional changes intended. Suggested-by: Pavankumar Kondeti Signed-off-by: Chen Yu --- kernel/power/hibernate.c | 7 ++++++- kernel/power/power.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c index 793c55a2becb..aa551b093c3f 100644 --- a/kernel/power/hibernate.c +++ b/kernel/power/hibernate.c @@ -64,6 +64,7 @@ enum { static int hibernation_mode = HIBERNATION_SHUTDOWN; bool freezer_test_done; +bool snapshot_test; static const struct platform_hibernation_ops *hibernation_ops; @@ -716,7 +717,6 @@ static int load_image_and_restore(void) */ int hibernate(void) { - bool snapshot_test = false; unsigned int sleep_flags; int error; @@ -744,6 +744,9 @@ int hibernate(void) if (error) goto Exit; + /* protected by system_transition_mutex */ + snapshot_test = false; + lock_device_hotplug(); /* Allocate memory management structures */ error = create_basic_memory_bitmaps(); @@ -940,6 +943,8 @@ static int software_resume(void) */ mutex_lock_nested(&system_transition_mutex, SINGLE_DEPTH_NESTING); + snapshot_test = false; + if (swsusp_resume_device) goto Check_image; diff --git a/kernel/power/power.h b/kernel/power/power.h index b4f433943209..b83c8d5e188d 100644 --- a/kernel/power/power.h +++ b/kernel/power/power.h @@ -59,6 +59,7 @@ asmlinkage int swsusp_save(void); /* kernel/power/hibernate.c */ extern bool freezer_test_done; +extern bool snapshot_test; extern int hibernation_snapshot(int platform_mode); extern int hibernation_restore(int platform_mode); From patchwork Fri Apr 14 12:10:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Yu X-Patchwork-Id: 13210908 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD310C77B6E for ; Fri, 14 Apr 2023 04:15:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229663AbjDNEPE (ORCPT ); Fri, 14 Apr 2023 00:15:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229749AbjDNEOx (ORCPT ); Fri, 14 Apr 2023 00:14:53 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90862524B; Thu, 13 Apr 2023 21:14:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681445688; x=1712981688; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dsi8NCRi2VJpWEZHMlW1fz/LRYWvxDuxdrK0U2M3dMA=; b=IQFo4SIaFAKSDP/D9a1qoTmkwiaHDj3xzBfwSDvT6VJTc24oUpIonaLH m88TcR+nQw5Zufwys9vy4dm4iWuUStsaJX0e3g89pAoFOWWDPucUPECYp HgVLOcXqh1uzVl5ZiC3JCvPM8NQia68+5VcMOkKeqGTdxbKfN+FCadZVW uC5HHVMw3nQSjEQLD3eV5Yi41GoZlFyTnIMTtRK/oCgp5qpR9UwLE/KKk Cg8nF1fOQ78ocFUbGI11vZU+/6FDclXYD65AsBQj/67rrXt5UqJDrfrbR WY8mFVYFNE71MsBi5LGDqyqj5rFbRwdMKNefLqXoQ6rv6UXVoDj+9KMZ2 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10679"; a="333142697" X-IronPort-AV: E=Sophos;i="5.99,195,1677571200"; d="scan'208";a="333142697" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Apr 2023 21:14:48 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10679"; a="935863770" X-IronPort-AV: E=Sophos;i="5.99,195,1677571200"; d="scan'208";a="935863770" Received: from chenyu-dev.sh.intel.com ([10.239.158.170]) by fmsmga006.fm.intel.com with ESMTP; 13 Apr 2023 21:14:45 -0700 From: Chen Yu To: "Rafael J. Wysocki" , Len Brown Cc: Ye Bin , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Pavankumar Kondeti , Yifan Li , Chen Yu , Wendy Wang Subject: [PATCH v3 2/2] PM: hibernate: Do not get block device exclusively in test_resume mode Date: Fri, 14 Apr 2023 20:10:42 +0800 Message-Id: <7d9e20406c9309abfc077f776881f810c1e6122a.1681443714.git.yu.c.chen@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The system refused to do a test_resume because it found that the swap device has already been taken by someone else. Specifically, the swsusp_check()->blkdev_get_by_dev(FMODE_EXCL) is supposed to do this check. Steps to reproduce: dd if=/dev/zero of=/swapfile bs=$(cat /proc/meminfo | awk '/MemTotal/ {print $2}') count=1024 conv=notrunc mkswap /swapfile swapon /swapfile swap-offset /swapfile echo 34816 > /sys/power/resume_offset echo test_resume > /sys/power/disk echo disk > /sys/power/state PM: Using 3 thread(s) for compression PM: Compressing and saving image data (293150 pages)... PM: Image saving progress: 0% PM: Image saving progress: 10% ata1: SATA link up 1.5 Gbps (SStatus 113 SControl 300) ata1.00: configured for UDMA/100 ata2: SATA link down (SStatus 0 SControl 300) ata5: SATA link down (SStatus 0 SControl 300) ata6: SATA link down (SStatus 0 SControl 300) ata3: SATA link down (SStatus 0 SControl 300) ata4: SATA link down (SStatus 0 SControl 300) PM: Image saving progress: 20% PM: Image saving progress: 30% PM: Image saving progress: 40% PM: Image saving progress: 50% pcieport 0000:00:02.5: pciehp: Slot(0-5): No device found PM: Image saving progress: 60% PM: Image saving progress: 70% PM: Image saving progress: 80% PM: Image saving progress: 90% PM: Image saving done PM: hibernation: Wrote 1172600 kbytes in 2.70 seconds (434.29 MB/s) PM: S| PM: hibernation: Basic memory bitmaps freed PM: Image not found (code -16) This is because when using the swapfile as the hibernation storage, the block device where the swapfile is located has already been mounted by the OS distribution(usually mounted as the rootfs). This is not an issue for normal hibernation, because software_resume()->swsusp_check() happens before the block device(rootfs) mount. But it is a problem for the test_resume mode. Because when test_resume happens, the block device has been mounted already. Thus remove the FMODE_EXCL for test_resume mode. This would not be a problem because in test_resume stage, the processes have already been frozen, and the race condition described in Commit 39fbef4b0f77 ("PM: hibernate: Get block device exclusively in swsusp_check()") is unlikely to happen. Fixes: 39fbef4b0f77 ("PM: hibernate: Get block device exclusively in swsusp_check()") Reported-by: Yifan Li Suggested-by: Pavankumar Kondeti Tested-by: Pavankumar Kondeti Tested-by: Wendy Wang Signed-off-by: Chen Yu --- kernel/power/hibernate.c | 8 ++++++-- kernel/power/swap.c | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c index aa551b093c3f..30d1274f03f6 100644 --- a/kernel/power/hibernate.c +++ b/kernel/power/hibernate.c @@ -688,18 +688,22 @@ static int load_image_and_restore(void) { int error; unsigned int flags; + fmode_t mode = FMODE_READ; + + if (snapshot_test) + mode |= FMODE_EXCL; pm_pr_dbg("Loading hibernation image.\n"); lock_device_hotplug(); error = create_basic_memory_bitmaps(); if (error) { - swsusp_close(FMODE_READ | FMODE_EXCL); + swsusp_close(mode); goto Unlock; } error = swsusp_read(&flags); - swsusp_close(FMODE_READ | FMODE_EXCL); + swsusp_close(mode); if (!error) error = hibernation_restore(flags & SF_PLATFORM_MODE); diff --git a/kernel/power/swap.c b/kernel/power/swap.c index 36a1df48280c..92e41ed292ad 100644 --- a/kernel/power/swap.c +++ b/kernel/power/swap.c @@ -1518,9 +1518,13 @@ int swsusp_check(void) { int error; void *holder; + fmode_t mode = FMODE_READ; + + if (snapshot_test) + mode |= FMODE_EXCL; hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device, - FMODE_READ | FMODE_EXCL, &holder); + mode, &holder); if (!IS_ERR(hib_resume_bdev)) { set_blocksize(hib_resume_bdev, PAGE_SIZE); clear_page(swsusp_header); @@ -1547,7 +1551,7 @@ int swsusp_check(void) put: if (error) - blkdev_put(hib_resume_bdev, FMODE_READ | FMODE_EXCL); + blkdev_put(hib_resume_bdev, mode); else pr_debug("Image signature found, resuming\n"); } else {