2007-01-25 11:06:48

by Ingo Molnar

[permalink] [raw]
Subject: [patch] suspend/resume debugging: device filter

Subject: [patch] suspend/resume debugging: device filter
From: Ingo Molnar <[email protected]>

this patch implements the /sys/power/filter attribute, which takes a
string. If a device's name matches the filter string (exactly), then
that device is excluded from suspend/resume.

this can be helpful in a number of ways when debugging suspend and
resume problems:

- if CONFIG_DISABLE_CONSOLE_SUSPEND is used then the serial
console is still suspended after which point there's no
log output. Doing "echo serial > /sys/power/filter" keeps
the serial port active, so any messages (and crash info)
after that point is displayed.

- if a device is suspected to be the reason of resume failure
then it can be excluded via the filter. That device obviously
wont work, but users can thus help us debug resume problems
in combination with pm_trace, without having to hack the kernel.

(note that you can obvious break suspend/resume via the filter, by
excluding a vital device - so it is only to be used when suspend or
resume is broken to begin with.)

it might be better to do this centrally in sysfs, via a per-device
attribute, to individually enable suspend and resume on a per device
basis, but my sysfs-fu is not strong enough for that now ;-)

Signed-off-by: Ingo Molnar <[email protected]>
---
drivers/base/power/resume.c | 6 ++++
drivers/base/power/suspend.c | 3 +-
include/linux/resume-trace.h | 6 ++++
kernel/power/main.c | 58 +++++++++++++++++++++++++++++++++++++++++++
4 files changed, 72 insertions(+), 1 deletion(-)

Index: linux/drivers/base/power/resume.c
===================================================================
--- linux.orig/drivers/base/power/resume.c
+++ linux/drivers/base/power/resume.c
@@ -24,6 +24,9 @@ int resume_device(struct device * dev)
{
int error = 0;

+ if (power_filter(dev))
+ return 0;
+
TRACE_DEVICE(dev);
TRACE_RESUME(0);
down(&dev->sem);
@@ -52,6 +55,9 @@ static int resume_device_early(struct de
{
int error = 0;

+ if (power_filter(dev))
+ return 0;
+
TRACE_DEVICE(dev);
TRACE_RESUME(0);
if (dev->bus && dev->bus->resume_early) {
Index: linux/drivers/base/power/suspend.c
===================================================================
--- linux.orig/drivers/base/power/suspend.c
+++ linux/drivers/base/power/suspend.c
@@ -10,6 +10,7 @@

#include <linux/device.h>
#include <linux/kallsyms.h>
+#include <linux/resume-trace.h>
#include <linux/pm.h>
#include "../base.h"
#include "power.h"
@@ -78,7 +79,7 @@ int suspend_device(struct device * dev,
suspend_report_result(dev->class->suspend, error);
}

- if (!error && dev->bus && dev->bus->suspend && !dev->power.power_state.event) {
+ if (!error && dev->bus && dev->bus->suspend && !dev->power.power_state.event && !power_filter(dev)) {
dev_dbg(dev, "%s%s\n",
suspend_verb(state.event),
((state.event == PM_EVENT_SUSPEND)
Index: linux/include/linux/resume-trace.h
===================================================================
--- linux.orig/include/linux/resume-trace.h
+++ linux/include/linux/resume-trace.h
@@ -9,6 +9,8 @@ struct device;
extern void set_trace_device(struct device *);
extern void generate_resume_trace(void *tracedata, unsigned int user);

+extern int power_filter(struct device *dev);
+
#define TRACE_DEVICE(dev) set_trace_device(dev)
#define TRACE_RESUME(user) do { \
if (pm_trace_enabled) { \
@@ -28,6 +30,10 @@ extern void generate_resume_trace(void *

#define TRACE_DEVICE(dev) do { } while (0)
#define TRACE_RESUME(dev) do { } while (0)
+static inline int power_filter(struct device *dev)
+{
+ return 0;
+}

#endif

Index: linux/kernel/power/main.c
===================================================================
--- linux.orig/kernel/power/main.c
+++ linux/kernel/power/main.c
@@ -15,6 +15,7 @@
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/init.h>
+#include <linux/device.h>
#include <linux/pm.h>
#include <linux/console.h>
#include <linux/cpu.h>
@@ -306,9 +307,66 @@ pm_trace_store(struct subsystem * subsys

power_attr(pm_trace);

+/**
+ * filter - exclude drivers from suspend and resume
+ *
+ * show() returns the current filter
+ *
+ * store() accepts a new filter (up to 128 chars long)
+ *
+ * Do "echo serial > /sys/power/filter" to exclude the
+ * serial driver from suspension - this can be useful to
+ * get kernel messages out after the serial console, or to
+ * see which device causes a resume failure.
+ */
+
+#define FILTER_LEN 128
+
+static char power_filter_str[FILTER_LEN+1] = "<none>";
+
+int power_filter(struct device *dev)
+{
+ const char *str = dev_driver_string(dev);
+
+ if (!strcmp(str, power_filter_str)) {
+ printk(KERN_INFO "power filter match for device: %s\n", str);
+ return 1;
+ }
+ return 0;
+}
+
+static ssize_t filter_show(struct subsystem * subsys, char * buf)
+{
+ char *s = buf;
+
+ s += sprintf(s, "%s\n", power_filter_str);
+
+ return (s - buf);
+}
+
+static ssize_t
+filter_store(struct subsystem * subsys, const char * buf, size_t n)
+{
+ unsigned int len;
+
+ strncpy(power_filter_str, buf, FILTER_LEN);
+
+ len = strlen(power_filter_str);
+ /*
+ * Strip off any trailing '\n':
+ */
+ if (len && power_filter_str[len-1] == '\n')
+ power_filter_str[len-1] = 0;
+
+ return len;
+}
+
+power_attr(filter);
+
static struct attribute * g[] = {
&state_attr.attr,
&pm_trace_attr.attr,
+ &filter_attr.attr,
NULL,
};
#else


2007-01-25 11:28:49

by Nigel Cunningham

[permalink] [raw]
Subject: Re: [patch] suspend/resume debugging: device filter

Hi.

On Thu, 2007-01-25 at 12:05 +0100, Ingo Molnar wrote:
> it might be better to do this centrally in sysfs, via a per-device
> attribute, to individually enable suspend and resume on a per device
> basis, but my sysfs-fu is not strong enough for that now ;-)

Yeah. I was thinking recently of doing a per-device attribute, but like
so many things at the moment, getting around to it is a little bit of a
problem.

> Signed-off-by: Ingo Molnar <[email protected]>
> ---
> drivers/base/power/resume.c | 6 ++++
> drivers/base/power/suspend.c | 3 +-
> include/linux/resume-trace.h | 6 ++++
> kernel/power/main.c | 58 +++++++++++++++++++++++++++++++++++++++++++

Should the sysfs stuff (not just this) be in kernel/power/main.c? I
wonder if it would be better put in drivers/base?

Regards,

Nigel

2007-01-25 11:32:58

by Pavel Machek

[permalink] [raw]
Subject: Re: [patch] suspend/resume debugging: device filter

Hi!

> Subject: [patch] suspend/resume debugging: device filter
> From: Ingo Molnar <[email protected]>
>
> this patch implements the /sys/power/filter attribute, which takes a
> string. If a device's name matches the filter string (exactly), then
> that device is excluded from suspend/resume.
>
> this can be helpful in a number of ways when debugging suspend and
> resume problems:
>
> - if CONFIG_DISABLE_CONSOLE_SUSPEND is used then the serial
> console is still suspended after which point there's no
> log output. Doing "echo serial > /sys/power/filter" keeps
> the serial port active, so any messages (and crash info)
> after that point is displayed.
>
> - if a device is suspected to be the reason of resume failure
> then it can be excluded via the filter. That device obviously
> wont work, but users can thus help us debug resume problems
> in combination with pm_trace, without having to hack the kernel.
>
> (note that you can obvious break suspend/resume via the filter, by
> excluding a vital device - so it is only to be used when suspend or
> resume is broken to begin with.)

Should this go to Documentation/power?

> it might be better to do this centrally in sysfs, via a per-device
> attribute, to individually enable suspend and resume on a per device
> basis, but my sysfs-fu is not strong enough for that now ;-)

Yep, I think it should go to per-device attribute. Also it would be
nice to name it somehow like debug_suspend_filter or something, so
that people have less tendency to play with it.
Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

2007-01-26 01:56:32

by Greg KH

[permalink] [raw]
Subject: Re: [patch] suspend/resume debugging: device filter

On Thu, Jan 25, 2007 at 12:05:01PM +0100, Ingo Molnar wrote:
> Subject: [patch] suspend/resume debugging: device filter
> From: Ingo Molnar <[email protected]>
>
> this patch implements the /sys/power/filter attribute, which takes a
> string. If a device's name matches the filter string (exactly), then
> that device is excluded from suspend/resume.
>
> this can be helpful in a number of ways when debugging suspend and
> resume problems:
>
> - if CONFIG_DISABLE_CONSOLE_SUSPEND is used then the serial
> console is still suspended after which point there's no
> log output. Doing "echo serial > /sys/power/filter" keeps
> the serial port active, so any messages (and crash info)
> after that point is displayed.
>
> - if a device is suspected to be the reason of resume failure
> then it can be excluded via the filter. That device obviously
> wont work, but users can thus help us debug resume problems
> in combination with pm_trace, without having to hack the kernel.
>
> (note that you can obvious break suspend/resume via the filter, by
> excluding a vital device - so it is only to be used when suspend or
> resume is broken to begin with.)
>
> it might be better to do this centrally in sysfs, via a per-device
> attribute, to individually enable suspend and resume on a per device
> basis, but my sysfs-fu is not strong enough for that now ;-)

Here's a (compile tested only) patch that does this on a per-device
basis, which is smaller, and should work just as well as your patch.

It creates a new file in the power/ directory for every device called
"can_suspend". Write a '0' to it to prevent that device from being
suspended.

Does this work for you?

Yeah, the wording of the filename and variable isn't the best, I'm open
to better choices if anyone has them.

thanks,

greg k-h

---
drivers/base/power/suspend.c | 2 +-
drivers/base/power/sysfs.c | 30 ++++++++++++++++++++++++++++++
include/linux/device.h | 1 +
3 files changed, 32 insertions(+), 1 deletion(-)

--- gregkh-2.6.orig/drivers/base/power/suspend.c
+++ gregkh-2.6/drivers/base/power/suspend.c
@@ -78,7 +78,7 @@ int suspend_device(struct device * dev,
suspend_report_result(dev->class->suspend, error);
}

- if (!error && dev->bus && dev->bus->suspend && !dev->power.power_state.event) {
+ if (!error && !dev->no_suspend && dev->bus && dev->bus->suspend && !dev->power.power_state.event) {
dev_dbg(dev, "%s%s\n",
suspend_verb(state.event),
((state.event == PM_EVENT_SUSPEND)
--- gregkh-2.6.orig/drivers/base/power/sysfs.c
+++ gregkh-2.6/drivers/base/power/sysfs.c
@@ -141,12 +141,42 @@ wake_store(struct device * dev, struct d

static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);

+static ssize_t can_suspend_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ return sprintf(buf, "%s\n", dev->no_suspend ? "no" : "yes");
+}
+
+static ssize_t can_suspend_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t n)
+{
+ if (!n)
+ return -EINVAL;
+
+ switch (buf[0]) {
+ case 'y':
+ case 'Y':
+ case '1':
+ dev->no_suspend = 0;
+ break;
+ case 'n':
+ case 'N':
+ case '0':
+ dev->no_suspend = 1;
+ break;
+ }
+
+ return n;
+}
+static DEVICE_ATTR(can_suspend, 0644, can_suspend_show, can_suspend_store);

static struct attribute * power_attrs[] = {
#ifdef CONFIG_PM_SYSFS_DEPRECATED
&dev_attr_state.attr,
#endif
&dev_attr_wakeup.attr,
+ &dev_attr_can_suspend.attr,
NULL,
};
static struct attribute_group pm_attr_group = {
--- gregkh-2.6.orig/include/linux/device.h
+++ gregkh-2.6/include/linux/device.h
@@ -365,6 +365,7 @@ struct device {
char bus_id[BUS_ID_SIZE]; /* position on parent bus */
struct device_type *type;
unsigned is_registered:1;
+ unsigned no_suspend:1;
struct device_attribute uevent_attr;
struct device_attribute *devt_attr;

2007-01-26 09:36:58

by Pavel Machek

[permalink] [raw]
Subject: Re: [patch] suspend/resume debugging: device filter

Hi!

> > it might be better to do this centrally in sysfs, via a per-device
> > attribute, to individually enable suspend and resume on a per device
> > basis, but my sysfs-fu is not strong enough for that now ;-)
>
> Here's a (compile tested only) patch that does this on a per-device
> basis, which is smaller, and should work just as well as your patch.
>
> It creates a new file in the power/ directory for every device called
> "can_suspend". Write a '0' to it to prevent that device from being
> suspended.

Maybe we could just introduce debug_flags?

> +static ssize_t can_suspend_store(struct device *dev,
> + struct device_attribute *attr,
> + const char *buf, size_t n)
> +{
> + if (!n)
> + return -EINVAL;
> +
> + switch (buf[0]) {
> + case 'y':
> + case 'Y':
> + case '1':
> + dev->no_suspend = 0;
> + break;
> + case 'n':
> + case 'N':
> + case '0':
> + dev->no_suspend = 1;
> + break;

default: return -EINVAL ?

Pavel

--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

2007-05-05 09:24:41

by Ingo Molnar

[permalink] [raw]
Subject: Re: [patch] suspend/resume debugging: device filter


* Greg KH <[email protected]> wrote:

> Here's a (compile tested only) patch that does this on a per-device
> basis, which is smaller, and should work just as well as your patch.
>
> It creates a new file in the power/ directory for every device called
> "can_suspend". Write a '0' to it to prevent that device from being
> suspended.
>
> Does this work for you?

yeah, i was able to use this too to debug suspend/resume problems. But
i've added the check to the resume path too - for example sw-suspend
does a resume of devices during its suspend cycle, cutting off much of
the netconsole output.

which makes the can_suspend flag mis-named - perhaps rename it to
exclude_pm ?

updated patch below, against v2.6.21. Could we get this into v2.6.22
please? It's a real time-saver.

Ingo

---
drivers/base/power/resume.c | 6 ++++++
drivers/base/power/suspend.c | 2 +-
drivers/base/power/sysfs.c | 30 ++++++++++++++++++++++++++++++
include/linux/device.h | 1 +
4 files changed, 38 insertions(+), 1 deletion(-)

Index: linux/drivers/base/power/resume.c
===================================================================
--- linux.orig/drivers/base/power/resume.c
+++ linux/drivers/base/power/resume.c
@@ -24,6 +24,9 @@ int resume_device(struct device * dev)
{
int error = 0;

+ if (dev->no_suspend)
+ return 0;
+
TRACE_DEVICE(dev);
TRACE_RESUME(0);
down(&dev->sem);
@@ -52,6 +55,9 @@ static int resume_device_early(struct de
{
int error = 0;

+ if (dev->no_suspend)
+ return 0;
+
TRACE_DEVICE(dev);
TRACE_RESUME(0);
if (dev->bus && dev->bus->resume_early) {
Index: linux/drivers/base/power/suspend.c
===================================================================
--- linux.orig/drivers/base/power/suspend.c
+++ linux/drivers/base/power/suspend.c
@@ -78,7 +78,7 @@ int suspend_device(struct device * dev,
suspend_report_result(dev->class->suspend, error);
}

- if (!error && dev->bus && dev->bus->suspend && !dev->power.power_state.event) {
+ if (!error && !dev->no_suspend && dev->bus && dev->bus->suspend && !dev->power.power_state.event) {
dev_dbg(dev, "%s%s\n",
suspend_verb(state.event),
((state.event == PM_EVENT_SUSPEND)
Index: linux/drivers/base/power/sysfs.c
===================================================================
--- linux.orig/drivers/base/power/sysfs.c
+++ linux/drivers/base/power/sysfs.c
@@ -141,12 +141,42 @@ wake_store(struct device * dev, struct d

static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);

+static ssize_t can_suspend_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ return sprintf(buf, "%s\n", dev->no_suspend ? "no" : "yes");
+}
+
+static ssize_t can_suspend_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t n)
+{
+ if (!n)
+ return -EINVAL;
+
+ switch (buf[0]) {
+ case 'y':
+ case 'Y':
+ case '1':
+ dev->no_suspend = 0;
+ break;
+ case 'n':
+ case 'N':
+ case '0':
+ dev->no_suspend = 1;
+ break;
+ }
+
+ return n;
+}
+static DEVICE_ATTR(can_suspend, 0644, can_suspend_show, can_suspend_store);

static struct attribute * power_attrs[] = {
#ifdef CONFIG_PM_SYSFS_DEPRECATED
&dev_attr_state.attr,
#endif
&dev_attr_wakeup.attr,
+ &dev_attr_can_suspend.attr,
NULL,
};
static struct attribute_group pm_attr_group = {
Index: linux/include/linux/device.h
===================================================================
--- linux.orig/include/linux/device.h
+++ linux/include/linux/device.h
@@ -402,6 +402,7 @@ struct device {
char bus_id[BUS_ID_SIZE]; /* position on parent bus */
struct device_type *type;
unsigned is_registered:1;
+ unsigned no_suspend:1;
struct device_attribute uevent_attr;
struct device_attribute *devt_attr;

2007-05-05 09:35:09

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [patch] suspend/resume debugging: device filter

On Saturday, 5 May 2007 11:24, Ingo Molnar wrote:
>
> * Greg KH <[email protected]> wrote:
>
> > Here's a (compile tested only) patch that does this on a per-device
> > basis, which is smaller, and should work just as well as your patch.
> >
> > It creates a new file in the power/ directory for every device called
> > "can_suspend". Write a '0' to it to prevent that device from being
> > suspended.
> >
> > Does this work for you?
>
> yeah, i was able to use this too to debug suspend/resume problems. But
> i've added the check to the resume path too - for example sw-suspend
> does a resume of devices during its suspend cycle, cutting off much of
> the netconsole output.
>
> which makes the can_suspend flag mis-named - perhaps rename it to
> exclude_pm ?
>
> updated patch below, against v2.6.21. Could we get this into v2.6.22
> please? It's a real time-saver.

ACK

Greetings,
Rafael

2007-05-05 21:15:51

by Pavel Machek

[permalink] [raw]
Subject: Re: [patch] suspend/resume debugging: device filter

Hi!

> > Here's a (compile tested only) patch that does this on a per-device
> > basis, which is smaller, and should work just as well as your patch.
> >
> > It creates a new file in the power/ directory for every device called
> > "can_suspend". Write a '0' to it to prevent that device from being
> > suspended.
> >
> > Does this work for you?
>
> yeah, i was able to use this too to debug suspend/resume problems. But
> i've added the check to the resume path too - for example sw-suspend
> does a resume of devices during its suspend cycle, cutting off much of
> the netconsole output.
>
> which makes the can_suspend flag mis-named - perhaps rename it to
> exclude_pm ?

debug_exclude_pm? I do not want people playing with it, then
complaining that they broke the suspend.
Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

2007-05-08 02:55:23

by Greg KH

[permalink] [raw]
Subject: Re: [linux-pm] Re: [patch] suspend/resume debugging: device filter

On Sat, May 05, 2007 at 12:08:55PM +0200, Pavel Machek wrote:
> Hi!
>
> > > Here's a (compile tested only) patch that does this on a per-device
> > > basis, which is smaller, and should work just as well as your patch.
> > >
> > > It creates a new file in the power/ directory for every device called
> > > "can_suspend". Write a '0' to it to prevent that device from being
> > > suspended.
> > >
> > > Does this work for you?
> >
> > yeah, i was able to use this too to debug suspend/resume problems. But
> > i've added the check to the resume path too - for example sw-suspend
> > does a resume of devices during its suspend cycle, cutting off much of
> > the netconsole output.
> >
> > which makes the can_suspend flag mis-named - perhaps rename it to
> > exclude_pm ?
>
> debug_exclude_pm? I do not want people playing with it, then
> complaining that they broke the suspend.

I can make the attribute only show up if CONFIG_DEBUG_DRIVER is enabled.
Would that be better? We can also write to the syslog when the
attribute is changed that they just might have broken something.

thanks,

greg k-h