Skip to content

Commit

Permalink
Merge branch 'pm-sleep'
Browse files Browse the repository at this point in the history
Merge updates related to system sleep for 5.16-rc1:

 - Clean up hib_wait_io() (Falla Coulibaly).

 - Fix sparse warnings in hibernation-related code (Anders Roxell).

 - Use vzalloc() and kzalloc() instead of their open-coded
   equivalents in hibernation-related code (Cai Huoqing).

 - Prevent user space from crashing the kernel by attempting to
   restore the system state from a swap partition in use (Ye Bin).

 - Do not let "syscore" devices runtime-suspend during system PM
   transitions (Rafael Wysocki).

 - Do not pause cpuidle in the suspend-to-idle path (Rafael Wysocki).

 - Pause cpuidle later and resume it earlier during system PM
   transitions (Rafael Wysocki).

 - Make system suspend code use valid_state() consistently (Rafael
   Wysocki).

 - Add support for enabling wakeup IRQs after invoking the
   ->runtime_suspend() callback and make two drivers use it (Chunfeng
   Yun).

* pm-sleep:
  usb: mtu3: enable wake-up interrupt after runtime_suspend called
  usb: xhci-mtk: enable wake-up interrupt after runtime_suspend called
  PM / wakeirq: support enabling wake-up irq after runtime_suspend called
  PM: suspend: Use valid_state() consistently
  PM: sleep: Pause cpuidle later and resume it earlier during system transitions
  PM: suspend: Do not pause cpuidle in the suspend-to-idle path
  PM: sleep: Do not let "syscore" devices runtime-suspend during system transitions
  PM: hibernate: Get block device exclusively in swsusp_check()
  PM: hibernate: swap: Use vzalloc() and kzalloc()
  PM: hibernate: fix sparse warnings
  Revert "PM: sleep: Do not assume that "mem" is always present"
  PM: hibernate: Remove blk_status_to_errno in hib_wait_io
  PM: sleep: Do not assume that "mem" is always present
  • Loading branch information
Rafael J. Wysocki committed Nov 2, 2021
2 parents 1fec161 + 7ddae8c commit b62b306
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 65 deletions.
14 changes: 5 additions & 9 deletions drivers/base/power/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include <linux/suspend.h>
#include <trace/events/power.h>
#include <linux/cpufreq.h>
#include <linux/cpuidle.h>
#include <linux/devfreq.h>
#include <linux/timer.h>

Expand Down Expand Up @@ -747,8 +746,6 @@ void dpm_resume_noirq(pm_message_t state)

resume_device_irqs();
device_wakeup_disarm_wake_irqs();

cpuidle_resume();
}

/**
Expand Down Expand Up @@ -1051,7 +1048,7 @@ static void device_complete(struct device *dev, pm_message_t state)
const char *info = NULL;

if (dev->power.syscore)
return;
goto out;

device_lock(dev);

Expand Down Expand Up @@ -1081,6 +1078,7 @@ static void device_complete(struct device *dev, pm_message_t state)

device_unlock(dev);

out:
pm_runtime_put(dev);
}

Expand Down Expand Up @@ -1336,8 +1334,6 @@ int dpm_suspend_noirq(pm_message_t state)
{
int ret;

cpuidle_pause();

device_wakeup_arm_wake_irqs();
suspend_device_irqs();

Expand Down Expand Up @@ -1794,9 +1790,6 @@ static int device_prepare(struct device *dev, pm_message_t state)
int (*callback)(struct device *) = NULL;
int ret = 0;

if (dev->power.syscore)
return 0;

/*
* If a device's parent goes into runtime suspend at the wrong time,
* it won't be possible to resume the device. To prevent this we
Expand All @@ -1805,6 +1798,9 @@ static int device_prepare(struct device *dev, pm_message_t state)
*/
pm_runtime_get_noresume(dev);

if (dev->power.syscore)
return 0;

device_lock(dev);

dev->power.wakeup_path = false;
Expand Down
7 changes: 5 additions & 2 deletions drivers/base/power/power.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ extern u64 pm_runtime_active_time(struct device *dev);

#define WAKE_IRQ_DEDICATED_ALLOCATED BIT(0)
#define WAKE_IRQ_DEDICATED_MANAGED BIT(1)
#define WAKE_IRQ_DEDICATED_REVERSE BIT(2)
#define WAKE_IRQ_DEDICATED_MASK (WAKE_IRQ_DEDICATED_ALLOCATED | \
WAKE_IRQ_DEDICATED_MANAGED)
WAKE_IRQ_DEDICATED_MANAGED | \
WAKE_IRQ_DEDICATED_REVERSE)

struct wake_irq {
struct device *dev;
Expand All @@ -39,7 +41,8 @@ extern void dev_pm_arm_wake_irq(struct wake_irq *wirq);
extern void dev_pm_disarm_wake_irq(struct wake_irq *wirq);
extern void dev_pm_enable_wake_irq_check(struct device *dev,
bool can_change_status);
extern void dev_pm_disable_wake_irq_check(struct device *dev);
extern void dev_pm_disable_wake_irq_check(struct device *dev, bool cond_disable);
extern void dev_pm_enable_wake_irq_complete(struct device *dev);

#ifdef CONFIG_PM_SLEEP

Expand Down
6 changes: 4 additions & 2 deletions drivers/base/power/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,8 @@ static int rpm_suspend(struct device *dev, int rpmflags)
if (retval)
goto fail;

dev_pm_enable_wake_irq_complete(dev);

no_callback:
__update_runtime_status(dev, RPM_SUSPENDED);
pm_runtime_deactivate_timer(dev);
Expand Down Expand Up @@ -690,7 +692,7 @@ static int rpm_suspend(struct device *dev, int rpmflags)
return retval;

fail:
dev_pm_disable_wake_irq_check(dev);
dev_pm_disable_wake_irq_check(dev, true);
__update_runtime_status(dev, RPM_ACTIVE);
dev->power.deferred_resume = false;
wake_up_all(&dev->power.wait_queue);
Expand Down Expand Up @@ -873,7 +875,7 @@ static int rpm_resume(struct device *dev, int rpmflags)

callback = RPM_GET_CALLBACK(dev, runtime_resume);

dev_pm_disable_wake_irq_check(dev);
dev_pm_disable_wake_irq_check(dev, false);
retval = rpm_callback(callback, dev);
if (retval) {
__update_runtime_status(dev, RPM_SUSPENDED);
Expand Down
101 changes: 80 additions & 21 deletions drivers/base/power/wakeirq.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,7 @@ static irqreturn_t handle_threaded_wake_irq(int irq, void *_wirq)
return IRQ_HANDLED;
}

/**
* dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt
* @dev: Device entry
* @irq: Device wake-up interrupt
*
* Unless your hardware has separate wake-up interrupts in addition
* to the device IO interrupts, you don't need this.
*
* Sets up a threaded interrupt handler for a device that has
* a dedicated wake-up interrupt in addition to the device IO
* interrupt.
*
* The interrupt starts disabled, and needs to be managed for
* the device by the bus code or the device driver using
* dev_pm_enable_wake_irq() and dev_pm_disable_wake_irq()
* functions.
*/
int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
static int __dev_pm_set_dedicated_wake_irq(struct device *dev, int irq, unsigned int flag)
{
struct wake_irq *wirq;
int err;
Expand Down Expand Up @@ -197,7 +180,7 @@ int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
if (err)
goto err_free_irq;

wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED;
wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED | flag;

return err;

Expand All @@ -210,8 +193,57 @@ int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)

return err;
}


/**
* dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt
* @dev: Device entry
* @irq: Device wake-up interrupt
*
* Unless your hardware has separate wake-up interrupts in addition
* to the device IO interrupts, you don't need this.
*
* Sets up a threaded interrupt handler for a device that has
* a dedicated wake-up interrupt in addition to the device IO
* interrupt.
*
* The interrupt starts disabled, and needs to be managed for
* the device by the bus code or the device driver using
* dev_pm_enable_wake_irq*() and dev_pm_disable_wake_irq*()
* functions.
*/
int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
{
return __dev_pm_set_dedicated_wake_irq(dev, irq, 0);
}
EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);

/**
* dev_pm_set_dedicated_wake_irq_reverse - Request a dedicated wake-up interrupt
* with reverse enable ordering
* @dev: Device entry
* @irq: Device wake-up interrupt
*
* Unless your hardware has separate wake-up interrupts in addition
* to the device IO interrupts, you don't need this.
*
* Sets up a threaded interrupt handler for a device that has a dedicated
* wake-up interrupt in addition to the device IO interrupt. It sets
* the status of WAKE_IRQ_DEDICATED_REVERSE to tell rpm_suspend()
* to enable dedicated wake-up interrupt after running the runtime suspend
* callback for @dev.
*
* The interrupt starts disabled, and needs to be managed for
* the device by the bus code or the device driver using
* dev_pm_enable_wake_irq*() and dev_pm_disable_wake_irq*()
* functions.
*/
int dev_pm_set_dedicated_wake_irq_reverse(struct device *dev, int irq)
{
return __dev_pm_set_dedicated_wake_irq(dev, irq, WAKE_IRQ_DEDICATED_REVERSE);
}
EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq_reverse);

/**
* dev_pm_enable_wake_irq - Enable device wake-up interrupt
* @dev: Device
Expand Down Expand Up @@ -282,27 +314,54 @@ void dev_pm_enable_wake_irq_check(struct device *dev,
return;

enable:
enable_irq(wirq->irq);
if (!can_change_status || !(wirq->status & WAKE_IRQ_DEDICATED_REVERSE))
enable_irq(wirq->irq);
}

/**
* dev_pm_disable_wake_irq_check - Checks and disables wake-up interrupt
* @dev: Device
* @cond_disable: if set, also check WAKE_IRQ_DEDICATED_REVERSE
*
* Disables wake-up interrupt conditionally based on status.
* Should be only called from rpm_suspend() and rpm_resume() path.
*/
void dev_pm_disable_wake_irq_check(struct device *dev)
void dev_pm_disable_wake_irq_check(struct device *dev, bool cond_disable)
{
struct wake_irq *wirq = dev->power.wakeirq;

if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))
return;

if (cond_disable && (wirq->status & WAKE_IRQ_DEDICATED_REVERSE))
return;

if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED)
disable_irq_nosync(wirq->irq);
}

/**
* dev_pm_enable_wake_irq_complete - enable wake IRQ not enabled before
* @dev: Device using the wake IRQ
*
* Enable wake IRQ conditionally based on status, mainly used if want to
* enable wake IRQ after running ->runtime_suspend() which depends on
* WAKE_IRQ_DEDICATED_REVERSE.
*
* Should be only called from rpm_suspend() path.
*/
void dev_pm_enable_wake_irq_complete(struct device *dev)
{
struct wake_irq *wirq = dev->power.wakeirq;

if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))
return;

if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED &&
wirq->status & WAKE_IRQ_DEDICATED_REVERSE)
enable_irq(wirq->irq);
}

/**
* dev_pm_arm_wake_irq - Arm device wake-up
* @wirq: Device wake-up interrupt
Expand Down
2 changes: 1 addition & 1 deletion drivers/usb/host/xhci-mtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ static int xhci_mtk_probe(struct platform_device *pdev)
goto dealloc_usb2_hcd;

if (wakeup_irq > 0) {
ret = dev_pm_set_dedicated_wake_irq(dev, wakeup_irq);
ret = dev_pm_set_dedicated_wake_irq_reverse(dev, wakeup_irq);
if (ret) {
dev_err(dev, "set wakeup irq %d failed\n", wakeup_irq);
goto dealloc_usb3_hcd;
Expand Down
2 changes: 1 addition & 1 deletion drivers/usb/mtu3/mtu3_plat.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ static int mtu3_probe(struct platform_device *pdev)
goto comm_init_err;

if (ssusb->wakeup_irq > 0) {
ret = dev_pm_set_dedicated_wake_irq(dev, ssusb->wakeup_irq);
ret = dev_pm_set_dedicated_wake_irq_reverse(dev, ssusb->wakeup_irq);
if (ret) {
dev_err(dev, "failed to set wakeup irq %d\n", ssusb->wakeup_irq);
goto comm_exit;
Expand Down
9 changes: 7 additions & 2 deletions include/linux/pm_wakeirq.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#ifdef CONFIG_PM

extern int dev_pm_set_wake_irq(struct device *dev, int irq);
extern int dev_pm_set_dedicated_wake_irq(struct device *dev,
int irq);
extern int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq);
extern int dev_pm_set_dedicated_wake_irq_reverse(struct device *dev, int irq);
extern void dev_pm_clear_wake_irq(struct device *dev);
extern void dev_pm_enable_wake_irq(struct device *dev);
extern void dev_pm_disable_wake_irq(struct device *dev);
Expand All @@ -35,6 +35,11 @@ static inline int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
return 0;
}

static inline int dev_pm_set_dedicated_wake_irq_reverse(struct device *dev, int irq)
{
return 0;
}

static inline void dev_pm_clear_wake_irq(struct device *dev)
{
}
Expand Down
12 changes: 7 additions & 5 deletions kernel/power/hibernate.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ static int create_image(int platform_mode)
if (error || hibernation_test(TEST_PLATFORM))
goto Platform_finish;

error = suspend_disable_secondary_cpus();
error = pm_sleep_disable_secondary_cpus();
if (error || hibernation_test(TEST_CPUS))
goto Enable_cpus;

Expand Down Expand Up @@ -342,7 +342,7 @@ static int create_image(int platform_mode)
local_irq_enable();

Enable_cpus:
suspend_enable_secondary_cpus();
pm_sleep_enable_secondary_cpus();

/* Allow architectures to do nosmt-specific post-resume dances */
if (!in_suspend)
Expand Down Expand Up @@ -466,6 +466,8 @@ static int resume_target_kernel(bool platform_mode)
if (error)
goto Cleanup;

cpuidle_pause();

error = hibernate_resume_nonboot_cpu_disable();
if (error)
goto Enable_cpus;
Expand Down Expand Up @@ -509,7 +511,7 @@ static int resume_target_kernel(bool platform_mode)
local_irq_enable();

Enable_cpus:
suspend_enable_secondary_cpus();
pm_sleep_enable_secondary_cpus();

Cleanup:
platform_restore_cleanup(platform_mode);
Expand Down Expand Up @@ -587,7 +589,7 @@ int hibernation_platform_enter(void)
if (error)
goto Platform_finish;

error = suspend_disable_secondary_cpus();
error = pm_sleep_disable_secondary_cpus();
if (error)
goto Enable_cpus;

Expand All @@ -609,7 +611,7 @@ int hibernation_platform_enter(void)
local_irq_enable();

Enable_cpus:
suspend_enable_secondary_cpus();
pm_sleep_enable_secondary_cpus();

Platform_finish:
hibernation_ops->finish();
Expand Down
14 changes: 14 additions & 0 deletions kernel/power/power.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <linux/utsname.h>
#include <linux/freezer.h>
#include <linux/compiler.h>
#include <linux/cpu.h>
#include <linux/cpuidle.h>

struct swsusp_info {
struct new_utsname uts;
Expand Down Expand Up @@ -310,3 +312,15 @@ extern int pm_wake_lock(const char *buf);
extern int pm_wake_unlock(const char *buf);

#endif /* !CONFIG_PM_WAKELOCKS */

static inline int pm_sleep_disable_secondary_cpus(void)
{
cpuidle_pause();
return suspend_disable_secondary_cpus();
}

static inline void pm_sleep_enable_secondary_cpus(void)
{
suspend_enable_secondary_cpus();
cpuidle_resume();
}
Loading

0 comments on commit b62b306

Please sign in to comment.