All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alan Stern <stern@rowland.harvard.edu>
To: "Rafael J. Wysocki" <rjw@sisk.pl>
Cc: mark gross <640e9920@gmail.com>, Neil Brown <neilb@suse.de>,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	linux-pm@lists.linux-foundation.org
Subject: Re: [RFC][PATCH] PM: Avoid losing wakeup events during suspend
Date: Sun, 20 Jun 2010 12:28:29 -0400 (EDT)	[thread overview]
Message-ID: <Pine.LNX.4.44L0.1006201153420.22272-100000__995.984106748615$1277051359$gmane$org@netrider.rowland.org> (raw)
In-Reply-To: <201006200005.35771.rjw@sisk.pl>

On Sun, 20 Jun 2010, Rafael J. Wysocki wrote:

> Hi,
> 
> One of the arguments during the suspend blockers discussion was that the
> mainline kernel didn't contain any mechanisms allowing it to avoid losing
> wakeup events during system suspend.
> 
> Generally, there are two problems in that area.  First, if a wakeup event
> occurs exactly at the same time when /sys/power/state is being written to,
> the even may be delivered to user space right before the freezing of it,
> in which case the user space consumer of the event may not be able to process
> it before the system is suspended.

Indeed, the same problem arises if the event isn't delivered to
userspace until after userspace is frozen.  Of course, the underlying
issue here is that the kernel has no direct way to know when userspace
has finished processing an event.  Userspace would have to tell it,
which generally would mean rewriting some large number of user
programs.

>  Second, if a wakeup event occurs after user
> space has been frozen and that event is not a wakeup interrupt, the kernel will
> not react to it and the system will be suspended.

I don't quite understand what you mean here.  "Reacting" to an event
involves more than one action.  The kernel has to tell the hardware to
stop generating the wakeup signal, and it has to handle the event
somehow.

If the kernel doesn't tell the hardware to stop generating the wakeup 
signal, the signal will continue to be active until the system goes to 
sleep.  At that point it will cause the system to wake up immediately,
so there won't be any problem.

The real problem arises when the hardware stops generating the wakeup
signal but the kernel suspends before it finishes handling the event.  
For example, an interrupt handler might receive the event and start
processing it by calling pm_request_resume() -- but if the pm workqueue
thread is already frozen then the processing won't finish until
something else wakes the system up.  (IMO this is a potential bug which
could be fixed without too much effort.)

> The following patch illustrates my idea of how these two problems may be
> addressed.  It introduces a new global sysfs attribute,
> /sys/power/wakeup_count, associated with a running counter of wakeup events
> and a helper function, pm_wakeup_event(), that may be used by kernel subsystems
> to increment the wakeup events counter.

In what way is this better than suspend blockers?

> /sys/power/wakeup_count may be read from or written to by user space.  Reads
> will always succeed and return the current value of the wakeup events counter.
> Writes, however, will only succeed if the written number is equal to the
> current value of the wakeup events counter.  If a write is successful, it will
> cause the kernel to save the current value of the wakeup events counter and
> to compare the saved number with the current value of the counter at certain
> points of the subsequent suspend (or hibernate) sequence.  If the two values
> don't match, the suspend will be aborted just as though a wakeup interrupt
> happened.  Reading from /sys/power/wakeup_count again will turn that mechanism
> off.
> 
> The assumption is that there's a user space power manager that will first
> read from /sys/power/wakeup_count.  Then it will check all user space consumers
> of wakeup events known to it for unprocessed events.

What happens if an event arrives just before you read
/sys/power/wakeup_count, but the userspace consumer doesn't realize
there is a new unprocessed event until after the power manager checks
it?  Your plan is missing a critical step: the "handoff" whereby 
responsibility for handling an event passes from the kernel to 
userspace.

With suspend blockers, this handoff occurs when an event queue is 
emptied and its associate suspend blocker is deactivated.  Or with some 
kinds of events for which the Android people have not written an 
explicit handoff, it occurs when a timer expires (timed suspend 
blockers).

>  If there are any, it will
> wait for them to be processed and repeat.  In turn, if there are not any,
> it will try to write to /sys/power/wakeup_count and if the write is successful,
> it will write to /sys/power/state to start suspend, so if any wakeup events
> accur past that point, they will be noticed by the kernel and will eventually
> cause the suspend to be aborted.

This shares with the other alternatives posted recently the need for a
central power-manager process.  And like in-kernel suspend blockers, it
requires changes to wakeup-capable drivers (the wakeup-events counter
has to be incremented).

One advantage of the suspend-blocker approach is that it essentially
uses a single tool to handle both kinds of races (event not fully
handled by the kernel, or event not fully handled by userspace).  
Things aren't quite this simple, because of the need for a special API
to implement userspace suspend blockers, but this does avoid the need
for a power-manager process.

> In addition to the above, the patch adds a wakeup events counter to the
> power member of struct device and makes these per-device wakeup event counters
> available via sysfs, so that it's possible to check the activity of various
> wakeup event sources within the kernel.
> 
> To illustrate how subsystems can use pm_wakeup_event(), I added it to the
> PCI runtime PM wakeup-handling code.
> 
> At the moment the patch only contains code changes (ie. no documentation),
> but I'm going to add comments etc. if people like the idea.
> 
> Please tell me what you think.

While this isn't a bad idea, I don't see how it is superior to the 
other alternatives that have been proposed.

Alan Stern

  parent reply	other threads:[~2010-06-20 16:28 UTC|newest]

Thread overview: 151+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-19 22:05 [RFC][PATCH] PM: Avoid losing wakeup events during suspend Rafael J. Wysocki
2010-06-20  5:52 ` mark gross
2010-06-20  5:52 ` mark gross
2010-06-20 12:49   ` Rafael J. Wysocki
2010-06-20 23:13     ` mark gross
2010-06-20 23:13     ` mark gross
2010-06-20 12:49   ` Rafael J. Wysocki
2010-06-20 16:28 ` Alan Stern [this message]
2010-06-20 16:28 ` Alan Stern
2010-06-20 21:50   ` Rafael J. Wysocki
2010-06-21  2:23     ` Alan Stern
2010-06-21  2:23     ` Alan Stern
2010-06-21  5:32       ` Florian Mickler
2010-06-21  5:32       ` Florian Mickler
2010-06-21 15:23         ` Alan Stern
2010-06-21 20:38           ` Florian Mickler
2010-06-21 22:18             ` Alan Stern
2010-06-21 22:18             ` Alan Stern
2010-06-21 22:40               ` Rafael J. Wysocki
2010-06-21 22:48                 ` Rafael J. Wysocki
2010-06-21 22:48                 ` Rafael J. Wysocki
2010-06-22  0:50                 ` Arve Hjønnevåg
2010-06-22  0:50                 ` Arve Hjønnevåg
2010-06-22 10:21                 ` Rafael J. Wysocki
2010-06-22 10:21                 ` Rafael J. Wysocki
2010-06-22 14:35                   ` Alan Stern
2010-06-22 15:35                     ` Rafael J. Wysocki
2010-06-22 19:55                       ` Alan Stern
2010-06-22 19:55                       ` Alan Stern
2010-06-22 20:58                         ` Rafael J. Wysocki
2010-06-22 20:58                         ` Rafael J. Wysocki
2010-06-22 19:59                       ` [update] " Rafael J. Wysocki
2010-06-24 14:16                         ` Andy Lutomirski
2010-06-24 14:16                         ` Andy Lutomirski
2010-06-24 14:45                           ` Alan Stern
2010-06-24 14:45                           ` Alan Stern
2010-06-24 14:48                           ` Rafael J. Wysocki
2010-06-24 15:21                             ` Andy Lutomirski
2010-06-24 15:21                             ` Andy Lutomirski
2010-06-24 14:48                           ` Rafael J. Wysocki
2010-06-22 19:59                       ` Rafael J. Wysocki
2010-06-22 20:34                         ` Alan Stern
2010-06-22 20:34                         ` Alan Stern
2010-06-22 21:41                           ` Rafael J. Wysocki
2010-06-23  2:12                             ` Alan Stern
2010-06-23  2:12                             ` Alan Stern
2010-06-23 10:09                               ` Rafael J. Wysocki
2010-06-23 10:09                               ` Rafael J. Wysocki
2010-06-23 15:21                                 ` Alan Stern
2010-06-23 15:21                                 ` Alan Stern
2010-06-23 22:17                                   ` Rafael J. Wysocki
2010-06-23 22:17                                   ` Rafael J. Wysocki
2010-06-24 13:13                                     ` [update 2] " Rafael J. Wysocki
2010-06-24 15:06                                       ` Rafael J. Wysocki
2010-06-24 15:35                                         ` Alan Stern
2010-06-24 15:35                                         ` Alan Stern
2010-06-24 23:00                                           ` [update 3] " Rafael J. Wysocki
2010-06-24 23:00                                           ` Rafael J. Wysocki
2010-06-25 14:42                                             ` Alan Stern
2010-06-25 14:42                                             ` Alan Stern
2010-06-25 20:33                                               ` Rafael J. Wysocki
2010-06-25 20:33                                               ` Rafael J. Wysocki
2010-06-24 15:06                                       ` [update 2] " Rafael J. Wysocki
2010-06-24 15:44                                       ` Alan Stern
2010-06-24 15:44                                       ` Alan Stern
2010-06-24 16:19                                         ` Rafael J. Wysocki
2010-06-24 17:09                                           ` Alan Stern
2010-06-24 23:06                                             ` Rafael J. Wysocki
2010-06-24 23:06                                             ` Rafael J. Wysocki
2010-06-25 15:09                                               ` Alan Stern
2010-06-25 15:09                                               ` Alan Stern
2010-06-25 20:37                                                 ` Rafael J. Wysocki
2010-06-25 20:57                                                   ` Alan Stern
2010-06-25 20:57                                                   ` Alan Stern
2010-06-25 20:37                                                 ` Rafael J. Wysocki
2010-06-25  6:40                                             ` Florian Mickler
2010-06-25  6:40                                             ` Florian Mickler
2010-06-25 13:28                                               ` Rafael J. Wysocki
2010-06-25 13:28                                               ` Rafael J. Wysocki
2010-06-24 17:09                                           ` Alan Stern
2010-06-24 16:19                                         ` Rafael J. Wysocki
2010-06-24 13:13                                     ` Rafael J. Wysocki
2010-06-22 21:41                           ` [update] " Rafael J. Wysocki
2010-06-22 15:35                     ` Rafael J. Wysocki
2010-06-22 14:35                   ` Alan Stern
2010-06-22 23:00                   ` mark gross
2010-06-22 23:00                     ` mark gross
2010-06-21 22:40               ` Rafael J. Wysocki
2010-06-21 20:38           ` Florian Mickler
2010-06-21 15:23         ` Alan Stern
2010-06-21 16:54         ` Alan Stern
2010-06-21 20:40           ` Florian Mickler
2010-06-21 20:40           ` Florian Mickler
2010-06-21 21:18           ` Rafael J. Wysocki
2010-06-21 21:18           ` Rafael J. Wysocki
2010-06-21 22:27             ` Alan Stern
2010-06-21 22:27             ` Alan Stern
2010-06-21 16:54         ` Alan Stern
2010-06-21  6:13       ` mark gross
2010-06-21  6:13       ` mark gross
2010-06-21 12:10         ` tytso
2010-06-21 12:10         ` tytso
2010-06-21 12:22           ` Alan Cox
2010-06-21 12:22             ` Alan Cox
2010-06-21 12:26             ` Florian Mickler
2010-06-21 12:26             ` Florian Mickler
2010-06-21 12:26             ` Florian Mickler
2010-06-21 13:42             ` tytso
2010-06-21 14:01               ` Alan Cox
2010-06-21 14:01                 ` Alan Cox
2010-06-21 13:42             ` tytso
2010-06-22  1:07           ` mark gross
2010-06-22  1:07           ` mark gross
2010-06-21 16:01         ` Alan Stern
2010-06-21 16:01         ` Alan Stern
2010-06-22  1:25           ` mark gross
2010-06-22  2:24             ` Alan Stern
2010-06-22  2:24             ` Alan Stern
2010-06-22  1:25           ` mark gross
2010-06-21 21:58       ` Rafael J. Wysocki
2010-06-21 21:58       ` Rafael J. Wysocki
2010-06-20 21:50   ` Rafael J. Wysocki
2010-06-20 22:58   ` mark gross
2010-06-21  2:33     ` Alan Stern
2010-06-21  2:33     ` Alan Stern
2010-06-21  4:04       ` [linux-pm] " David Brownell
2010-06-21  4:04         ` David Brownell
2010-06-21  6:02         ` [linux-pm] " David Brownell
2010-06-21  6:02         ` David Brownell
2010-06-21 15:06         ` Alan Stern
2010-06-21 15:06         ` [linux-pm] " Alan Stern
2010-06-21  5:55       ` mark gross
2010-06-21 12:39         ` Florian Mickler
2010-06-21 12:39         ` Florian Mickler
2010-06-21 15:57         ` Alan Stern
2010-06-21 15:57         ` Alan Stern
2010-06-22  1:58           ` mark gross
2010-06-22  2:46             ` Alan Stern
2010-06-22  2:46             ` Alan Stern
2010-06-22  9:24               ` Rafael J. Wysocki
2010-06-22  9:24               ` Rafael J. Wysocki
2010-06-22  6:18             ` Florian Mickler
2010-06-22  6:18             ` Florian Mickler
2010-06-22 23:22               ` mark gross
2010-06-22 23:22               ` mark gross
2010-06-22  9:29             ` Rafael J. Wysocki
2010-06-22  9:29               ` Rafael J. Wysocki
2010-06-22  1:58           ` mark gross
2010-06-21  5:55       ` mark gross
2010-06-20 22:58   ` mark gross
2010-06-19 22:05 Rafael J. Wysocki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='Pine.LNX.4.44L0.1006201153420.22272-100000__995.984106748615$1277051359$gmane$org@netrider.rowland.org' \
    --to=stern@rowland.harvard.edu \
    --cc=640e9920@gmail.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@lists.linux-foundation.org \
    --cc=neilb@suse.de \
    --cc=rjw@sisk.pl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.