Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1475626ybt; Thu, 9 Jul 2020 07:58:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzjTBHBp1ciaH23zIFVgPqGaxyKwZX4ZKRlTzJS6+FxwytBldDRgZrrUWoAPiSLeHo+IGLj X-Received: by 2002:a05:6402:c06:: with SMTP id co6mr73222696edb.142.1594306693643; Thu, 09 Jul 2020 07:58:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594306693; cv=none; d=google.com; s=arc-20160816; b=FYTAEZ+x7D+UAE7B//V9wNZ9l9dVoqCgVqU8U991CVgJR0KjMa1Hq7UhA8H4PIS3x8 QduNmWWe+xuLNo1IivH6TY0OnjVhXCKOXp011n69HLqmCJH7UC1DMMRlEqLf0dV+ZOlN sugRtfdfI78ziY/o2OMR6v9lHCrNLzNepVuM7n3g62hQe1MFoHRrxhCMLC54rdpOIblM 7V4A3a8zcSwHNHf7Lua8rJNKMVHSpCbGk19Y1scGRZ18xBI8wt/pe8xuxCLjC6CV2KH/ +wN2j+FYI6bI9ak9uZBjG3xCy2xSi9Ie5flFZZtFDpw/LSforPtK6rLOGq2pSit3EtT6 /kAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=dhcEnm5KnUPL4hbFiqn7hsmXHR+dUto53JORUsitTSU=; b=hjvSJAgFx7994wjS/yUDC/ylbyzfNetwaz1EmziOXYODmEbzIvKDjtCYRc9kmadp8m lR65NATZy6c/eSMSPLJkhA3tKH5Atadm0u4JYJnEm0yDX1qg4Be39SN68PZsA/tm0SzS XAKV9mNcK26kUUT/Sbcr4oEimWuKPreQdV5gmV1kVL6lNx0WMPoVHG4rEoE+mpJ5bbAv WQriD31k2rZfyMg0toWpHE12Hd+cn8k/AkeYa1CoZdlcvOtkC4lKARyDl4LQrDijkQ6f 2mfuWVRRof9c1U/dO/Td6rQWtcokTy2uANob96xrxIa1/hoPqMvz1hEhYe1Fdi/SEG6b JvWA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id be25si2248773edb.319.2020.07.09.07.57.50; Thu, 09 Jul 2020 07:58:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728094AbgGIO5R (ORCPT + 99 others); Thu, 9 Jul 2020 10:57:17 -0400 Received: from cloudserver094114.home.pl ([79.96.170.134]:41066 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726729AbgGIO5R (ORCPT ); Thu, 9 Jul 2020 10:57:17 -0400 Received: from 89-64-83-236.dynamic.chello.pl (89.64.83.236) (HELO kreacher.localnet) by serwer1319399.home.pl (79.96.170.134) with SMTP (IdeaSmtpServer 0.83.415) id 6f4b9f3a7ac1aaf9; Thu, 9 Jul 2020 16:57:14 +0200 From: "Rafael J. Wysocki" To: Dan Williams Cc: linux-nvdimm@lists.01.org, Greg Kroah-Hartman , "Rafael J. Wysocki" , Vishal Verma , Doug Ledford , Jason Gunthorpe , Dave Jiang , Ira Weiny , Pavel Machek , Len Brown , linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 11/12] PM, libnvdimm: Add 'mem-quiet' state and callback for firmware activation Date: Thu, 09 Jul 2020 16:57:12 +0200 Message-ID: <23449996.3uVv1d17cZ@kreacher> In-Reply-To: <159408717289.2385045.14094866475168644020.stgit@dwillia2-desk3.amr.corp.intel.com> References: <159408711335.2385045.2567600405906448375.stgit@dwillia2-desk3.amr.corp.intel.com> <159408717289.2385045.14094866475168644020.stgit@dwillia2-desk3.amr.corp.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tuesday, July 7, 2020 3:59:32 AM CEST Dan Williams wrote: > The runtime firmware activation capability of Intel NVDIMM devices > requires memory transactions to be disabled for 100s of microseconds. > This timeout is large enough to cause in-flight DMA to fail and other > application detectable timeouts. Arrange for firmware activation to be > executed while the system is "quiesced", all processes and device-DMA > frozen. > > It is already required that invoking device ->freeze() callbacks is > sufficient to cease DMA. A device that continues memory writes outside > of user-direction violates expectations of the PM core to be to > establish a coherent hibernation image. > > That said, RDMA devices are an example of a device that access memory > outside of user process direction. RDMA drivers also typically assume > the system they are operating in will never be hibernated. A solution > for RDMA collisions with firmware activation is outside the scope of > this change and may need to rely on being able to survive the platform > imposed memory controller quiesce period. Thanks for following my suggestion to use the hibernation infrastructure rather than the suspend one, but I think it would be better to go a bit further with that. Namely, after thinking about this a bit more I have come to the conclusion that what is needed is an ability to execute a function, inside of the kernel, in a "quiet" environment in which memory updates are unlikely. While the hibernation infrastructure as is can be used for that, kind of, IMO it would be cleaner to introduce a helper for that, like in the (untested) patch below, so if the "quiet execution environment" is needed, whoever needs it may simply pass a function to hibernate_quiet_exec() and provide whatever user-space I/F is suitable on top of that. Please let me know what you think. Cheers! --- include/linux/suspend.h | 6 ++ kernel/power/hibernate.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) Index: linux-pm/kernel/power/hibernate.c =================================================================== --- linux-pm.orig/kernel/power/hibernate.c +++ linux-pm/kernel/power/hibernate.c @@ -795,6 +795,103 @@ int hibernate(void) return error; } +/** + * hibernate_quiet_exec - Execute a function with all devices frozen. + * @func: Function to execute. + * @data: Data pointer to pass to @func. + * + * Return the @func return value or an error code if it cannot be executed. + */ +int hibernate_quiet_exec(int (*func)(void *data), void *data) +{ + int error, nr_calls = 0; + + lock_system_sleep(); + + if (!hibernate_acquire()) { + error = -EBUSY; + goto unlock; + } + + pm_prepare_console(); + + error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls); + if (error) { + nr_calls--; + goto exit; + } + + error = freeze_processes(); + if (error) + goto exit; + + lock_device_hotplug(); + + pm_suspend_clear_flags(); + + error = platform_begin(true); + if (error) + goto thaw; + + error = freeze_kernel_threads(); + if (error) + goto thaw; + + error = dpm_prepare(PMSG_FREEZE); + if (error) + goto dpm_complete; + + suspend_console(); + + error = dpm_suspend(PMSG_FREEZE); + if (error) + goto dpm_resume; + + error = dpm_suspend_end(PMSG_FREEZE); + if (error) + goto dpm_resume; + + error = platform_pre_snapshot(true); + if (error) + goto skip; + + error = func(data); + +skip: + platform_finish(true); + + dpm_resume_start(PMSG_THAW); + +dpm_resume: + dpm_resume(PMSG_THAW); + + resume_console(); + +dpm_complete: + dpm_complete(PMSG_THAW); + + thaw_kernel_threads(); + +thaw: + platform_end(true); + + unlock_device_hotplug(); + + thaw_processes(); + +exit: + __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL); + + pm_restore_console(); + + hibernate_release(); + +unlock: + unlock_system_sleep(); + + return error; +} +EXPORT_SYMBOL_GPL(hibernate_quiet_exec); /** * software_resume - Resume from a saved hibernation image. Index: linux-pm/include/linux/suspend.h =================================================================== --- linux-pm.orig/include/linux/suspend.h +++ linux-pm/include/linux/suspend.h @@ -453,6 +453,8 @@ extern bool hibernation_available(void); asmlinkage int swsusp_save(void); extern struct pbe *restore_pblist; int pfn_is_nosave(unsigned long pfn); + +int hibernate_quiet_exec(int (*func)(void *data), void *data); #else /* CONFIG_HIBERNATION */ static inline void register_nosave_region(unsigned long b, unsigned long e) {} static inline void register_nosave_region_late(unsigned long b, unsigned long e) {} @@ -464,6 +466,10 @@ static inline void hibernation_set_ops(c static inline int hibernate(void) { return -ENOSYS; } static inline bool system_entering_hibernation(void) { return false; } static inline bool hibernation_available(void) { return false; } + +static inline hibernate_quiet_exec(int (*func)(void *data), void *data) { + return -ENOTSUPP; +} #endif /* CONFIG_HIBERNATION */ #ifdef CONFIG_HIBERNATION_SNAPSHOT_DEV