linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Zhang Rui <rui.zhang@intel.com>,
	"Rafael J. Wysocki" <rjw@sisk.pl>,
	LKML <linux-kernel@vger.kernel.org>,
	ACPI Devel Maling List <linux-acpi@vger.kernel.org>,
	pm list <linux-pm@lists.linux-foundation.org>
Subject: Re: [GIT PULL] PM updates for 2.6.33
Date: Mon, 7 Dec 2009 08:31:39 -0800 (PST)	[thread overview]
Message-ID: <alpine.LFD.2.00.0912070737150.3560@localhost.localdomain> (raw)
In-Reply-To: <Pine.LNX.4.44L0.0912071003270.3064-100000@iolanthe.rowland.org>



On Mon, 7 Dec 2009, Alan Stern wrote:
> > 
> > I dunno. Maybe I'm overlooking something, but the above is much closer to 
> > what I think would be worth doing.
> 
> You're overlooking resume.  It's more difficult than suspend.  The
> issue is that a child can't start its async part until the parent's
> synchronous part is finished.

No, I haven't overlooked resume at all. I just assumed that it was 
obvious. It's the exact same thing, except in reverse (the locking ends 
up being slightly different, but the changes are actually fairly 
straightforward).

And by reverse, I mean that you walk the tree in the reverse order too, 
exactly like we already do - on suspend we walk it children-first, on 
resume we walk it parents-first (small detail: we actually just walk a 
simple linked list, but the list is topologically ordered, so walking it 
forwards/backwards is topologically the same thing as doing that 
depth-first search).

> So for example, suppose the device listing contains P, C, Q, where C is
> a child of P, Q is unrelated, and P has a long-lasting asynchronous
> requirement.  The resume process will stall upon reaching C, waiting
> for P to finish.  Thus even though P and Q might be able to resume in
> parallel, they won't get the chance.

No. The resume process does EXCTLY THE SAME THING as I outlined for 
suspend, but just all in reverse. So now the resume process becomes the 
same two-phase thing:

	# Phase one
	resume(root)
	{
		// This can do things asynchronously if it wants,
		// but needs to take the write lock on itself until
		// it is done if it does
		resume_one_node(root);
		for_each_child(root)
			resume(child);
	}

	# Phase two
	post_resume(root)
	{
		post_resume_one_node(root);
		for_each_child(root)
			post_resume(child);
	}

Notice? It's _exactly_ the same thing as suspend - except all turned 
around. We do the nodes before the children ("walk the list backwards"), 
and we also do the locking the other way around (ie on suspend we'd lock 
the _parent_ if we wanted to do async stuff - to keep it around - but on 
resume we lock _ourselves_, so that the children can have something to 
wait on. Also note how we take a _write_ lock rather than a read lock).

(And again, I've only written it out in email, I've not tested it or 
thought about it all that deeply, so you'll excuse any stupid thinkos.)

Now, for something like PCI, I'd suggest (once more) leaving all drivers 
totally unchanged, and you end up with the exact same behavior as we had 
before (no real change to the whole resume ordering, and everything is 
synchronous so there is no relevant locking). 

But how would the USB layer do this?

Simple: all the normal leaf devices would have their resume callback be 
called at "post_resume()" time (exactly the reverse of the suspend phase: 
we suspend early, and we resume late - it's all a mirror image). And I'd 
suggest that the USB layer do it all totally asynchronously, except again 
turned around the other way.

Remember how on suspend, the suspend of a leaf device ended up being an 
issue of asynchronously calling a function that did the suspend, and then 
released the read-lock of the parent. Resume is the same, except now we'd 
actually want to take the parent read-lock asynchronously too, so you'd do

	down_write(leaf->lock);
	async_schedule(usb_node_resume, leaf);

where that function simply does

	usb_node_resume(node)
	{
		/* Wait for the parent to have resumed completely */
		down_read(node->parent->lock);
		node->resume(node)
		up_read(node->parent->lock);
		up_write(node->lock);
	}

and you're all done. Once more the ordering and the locking takes care of 
any need to serialize - there is no data structures to keep track of.

And what about USB hubs? They get resumed in the first phase (again, 
exactly the mirror image of the suspend), and the only thing they need to 
do is that _exact_ same thing above:

	down_write(hub->lock);
	async_schedule(usb_node_resume, hub);

 - Ta-daa! All done.

Notice? It's really pretty straightforward, and there are _zero_ new 
concepts. And again, no callbacks, no nothing.  Just the obvious mirror 
image of what happened when suspending. We do everything with simple async 
calls. And none of the tree walking actually blocks (yes, we do a 
"down_write()" on the nodes as we schedule the resume code, but it won't 
be a blocking one, since that is the first time we encounter that node: 
the blocking will come later when the async threads actually need to wait 
for things).

Again, I do not guarantee that I've dotted every i, and crossed every t. 
It's just that I'm pretty sure that we really don't need any fancy 
"infrastructure" for something this simple. And I really much prefer 
"conceptually simple high-level model" over a model of "keep track of all 
the relationships and have some complex model of devices".

So let's just look at your example:

> So for example, suppose the device listing contains P, C, Q, where C is
> a child of P, Q is unrelated, and P has a long-lasting asynchronous
> requirement.

The tree is:

	... -> P -> C
	    -> Q

and with what I suggest, during phase one, P will asynchronously start the 
resume. As part of its async resume it will have to wait for it's parents, 
of course, but all of that happens in a separate context, and the tree 
traversal goes on. 

And during phase #1, C and Q won't do anything at all. We _could_ do them 
during this phase, and it would actually all work out fine, but we 
wouldn't want to do that for a simple reason: we _want_ the pre_suspend 
and post_resume phases to be total mirror images, because if we end up 
doing error handling for the pre-suspend case, then the post-resume phase 
would be the "fixup" for it, so we actually want leaf things to happen 
during phase #2 - not because it would screw up locking or ordering, but 
because of other issues.

When we hit phase #2, we then do C and Q, and do the same thing - we have 
an async call that does the read-lock on the parent to make sure it's 
all resumed, and then we resume C and Q. And they'll automatically resume 
in parallel (unless C is waiting for P, of course, in which case P and Q 
end up resuming in parallel, and C ends up waiting).

Now, the above just takes care of the inter-device ordering. There are 
unrelated semantics we want to give, like "all devices will have resumed 
before we start waking up user space". Those are unrelated to the 
topological requirements, of course, and are not a requirement imposed by 
the device tree, but by our _other_ semantics (IOW, in this respect it's 
kind of like how we wanted pre-suspend and post-resume to be mirror images 
for other outside reasons).

So we'd actually have a "phase #3", but that phase wouldn't be visible to 
the devices themselves, it would be a 

	# Phase tree: make sure everything is resumed
	for_each_device() {
		read_lock(dev->lock);
		read_unlock(dev->lock);
	}

but as you can see, there's no actual device callbacks involved. It would 
be just the code device layer saying "ok, now I'm going to wait for all 
the devices to have finished their resume".

			Linus

  reply	other threads:[~2009-12-07 16:32 UTC|newest]

Thread overview: 235+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-05 21:16 [GIT PULL] PM updates for 2.6.33 Rafael J. Wysocki
2009-12-05 21:43 ` Linus Torvalds
2009-12-05 21:58   ` Linus Torvalds
2009-12-05 23:55     ` Rafael J. Wysocki
2009-12-06  0:45       ` Arjan van de Ven
2009-12-06  1:26         ` Rafael J. Wysocki
2009-12-06  1:58           ` Arjan van de Ven
2009-12-06  8:39             ` Ingo Molnar
2009-12-06  0:48       ` Linus Torvalds
2009-12-06  1:54         ` Rafael J. Wysocki
2009-12-06  1:57           ` Rafael J. Wysocki
2009-12-06  2:05           ` Linus Torvalds
2009-12-06  2:36             ` Rafael J. Wysocki
2009-12-06 15:23             ` Alan Stern
2009-12-06 19:04               ` [linux-pm] " Victor Lowther
2009-12-07  3:57               ` Zhang Rui
2009-12-07  5:57                 ` Linus Torvalds
2009-12-07  6:15                   ` Linus Torvalds
2009-12-17 23:28                     ` Benjamin Herrenschmidt
2009-12-07  6:37                   ` Arjan van de Ven
2009-12-07 15:13                   ` Alan Stern
2009-12-07 16:31                     ` Linus Torvalds [this message]
2009-12-07 16:55                       ` Linus Torvalds
2009-12-07 17:52                       ` Alan Stern
2009-12-07 18:05                         ` Linus Torvalds
2009-12-07 20:37                           ` Alan Stern
2009-12-07 20:48                             ` Linus Torvalds
2009-12-07 21:32                               ` Alan Stern
2009-12-07 21:41                                 ` Linus Torvalds
2009-12-07 21:47                                   ` Rafael J. Wysocki
2009-12-07 22:01                                   ` Alan Stern
2009-12-07 22:06                                     ` Linus Torvalds
2009-12-07 22:21                                       ` Alan Stern
2009-12-07 22:26                                         ` Linus Torvalds
2009-12-07 23:16                                           ` Alan Stern
2009-12-07 22:02                                   ` Rafael J. Wysocki
2009-12-07 22:16                                     ` Linus Torvalds
2009-12-07 23:51                                       ` Rafael J. Wysocki
2009-12-08  3:27                                         ` Alan Stern
2009-12-08 12:23                                           ` Async resume patch (was: Re: [GIT PULL] PM updates for 2.6.33) Rafael J. Wysocki
2009-12-08 12:35                                             ` Rafael J. Wysocki
2009-12-08 15:35                                             ` Linus Torvalds
2009-12-08 15:55                                               ` Alan Stern
2009-12-08 16:42                                                 ` Linus Torvalds
2009-12-08 18:08                                                   ` Alan Stern
2009-12-08 18:41                                                     ` Linus Torvalds
2009-12-08 18:52                                                       ` Linus Torvalds
2009-12-08 19:34                                                         ` Alan Stern
2009-12-08 19:30                                                       ` Alan Stern
2009-12-08 20:48                                                         ` Linus Torvalds
2009-12-08 21:32                                                           ` Alan Stern
2009-12-08 21:52                                                             ` Christian Borntraeger
2009-12-08 22:16                                                             ` Linus Torvalds
2009-12-09 19:06                                                               ` Alan Stern
2009-12-09 21:52                                                                 ` Linus Torvalds
2009-12-08 19:44                                               ` Rafael J. Wysocki
2009-12-08 20:16                                                 ` Alan Stern
2009-12-08 20:30                                                   ` Rafael J. Wysocki
2009-12-08 20:44                                                     ` Alan Stern
2009-12-08 20:52                                                       ` Rafael J. Wysocki
2009-12-08 21:40                                                         ` Alan Stern
2009-12-08 21:48                                                           ` spinlock in completion_done() (was: Re: Async resume patch (was: Re: [GIT PULL] PM updates for 2.6.33)) Rafael J. Wysocki
2009-12-09  9:29                                                             ` Ingo Molnar
2009-12-09 22:37                                                               ` Rafael J. Wysocki
2009-12-10  7:59                                                                 ` Ingo Molnar
2009-12-11  4:10                                                                   ` Dave Chinner
2009-12-11  7:54                                                                     ` Ingo Molnar
2009-12-12 23:07                                                                   ` [PATCH] sched: Make wakeup side variants of completion API irq safe (was: Re: spinlock in completion_done()) Rafael J. Wysocki
2009-12-13  7:36                                                                     ` [tip:sched/urgent] sched: Make wakeup side and atomic variants of completion API irq safe tip-bot for Rafael J. Wysocki
2009-12-08 22:18                                                           ` Async resume patch (was: Re: [GIT PULL] PM updates for 2.6.33) Linus Torvalds
2009-12-09  2:11                                                             ` Alan Stern
2009-12-08 21:08                                                   ` Linus Torvalds
2009-12-08 21:13                                                     ` Linus Torvalds
2009-12-08 22:07                                                     ` Alan Stern
2009-12-08 22:30                                                       ` Rafael J. Wysocki
2009-12-09  2:23                                                         ` Alan Stern
2009-12-09 21:56                                                           ` Rafael J. Wysocki
2009-12-09 22:27                                                             ` Alan Stern
2009-12-08 22:32                                                       ` Linus Torvalds
2009-12-09  2:35                                                         ` Alan Stern
2009-12-09  2:54                                                           ` Linus Torvalds
2009-12-09 15:24                                                             ` Alan Stern
2009-12-09 15:38                                                               ` Linus Torvalds
2009-12-09 15:57                                                                 ` Alan Stern
2009-12-25 17:09                                                                   ` Pavel Machek
2009-12-09 13:38                                                           ` Mark Brown
2009-12-09 15:49                                                             ` Alan Stern
2009-12-09 16:02                                                               ` Mark Brown
2009-12-09 16:23                                                                 ` Alan Stern
2009-12-09 16:46                                                                   ` Mark Brown
2009-12-09 16:57                                                                     ` Linus Torvalds
2009-12-09 17:45                                                                       ` Mark Brown
2009-12-09 17:57                                                                         ` Linus Torvalds
2009-12-09 18:27                                                                           ` Mark Brown
2009-12-09 17:10                                                                     ` Alan Stern
2009-12-09 17:19                                                                       ` Linus Torvalds
2009-12-09 18:08                                                                       ` Mark Brown
2009-12-08 21:04                                                 ` Linus Torvalds
2009-12-08 21:40                                                   ` Rafael J. Wysocki
2009-12-08 22:03                                                     ` Rafael J. Wysocki
2009-12-08 22:55                                                       ` Async suspend-resume patch w/ rwsems " Rafael J. Wysocki
2009-12-08 23:24                                                         ` Rafael J. Wysocki
2009-12-09 20:15                                                         ` Alan Stern
2009-12-09 22:18                                                           ` Rafael J. Wysocki
2009-12-09 22:38                                                             ` Alan Stern
2009-12-09 23:18                                                               ` Async suspend-resume patch w/ completions (was: Re: Async suspend-resume patch w/ rwsems) Rafael J. Wysocki
2009-12-10  2:51                                                                 ` Linus Torvalds
2009-12-10 19:40                                                                   ` Rafael J. Wysocki
2009-12-10 23:30                                                                     ` Linus Torvalds
2009-12-11  1:02                                                                       ` Rafael J. Wysocki
2009-12-11  1:25                                                                         ` Linus Torvalds
2009-12-11  3:42                                                                           ` Alan Stern
2009-12-11 22:17                                                                             ` Rafael J. Wysocki
2009-12-12  0:38                                                                               ` Alan Stern
2009-12-11 22:11                                                                           ` Rafael J. Wysocki
2009-12-11 22:31                                                                             ` Linus Torvalds
2009-12-11 23:48                                                                               ` Rafael J. Wysocki
2009-12-11 23:53                                                                                 ` Linus Torvalds
2009-12-12 17:48                                                                                   ` Rafael J. Wysocki
2009-12-12 18:54                                                                                     ` Linus Torvalds
2009-12-12 22:34                                                                                       ` Rafael J. Wysocki
2009-12-12 22:40                                                                                         ` Rafael J. Wysocki
2009-12-14 18:21                                                                                         ` Linus Torvalds
2009-12-14 22:11                                                                                           ` Rafael J. Wysocki
2009-12-14 22:41                                                                                             ` Linus Torvalds
2009-12-14 22:43                                                                                               ` Linus Torvalds
2009-12-14 23:18                                                                                               ` Rafael J. Wysocki
2009-12-15  0:10                                                                                                 ` Linus Torvalds
2009-12-15  0:11                                                                                                   ` Linus Torvalds
2009-12-15 11:14                                                                                                     ` Rafael J. Wysocki
2009-12-15 15:31                                                                                                       ` Linus Torvalds
2009-12-15 11:03                                                                                                   ` Rafael J. Wysocki
2009-12-15 15:26                                                                                                     ` Linus Torvalds
2009-12-15 15:55                                                                                                       ` Alan Stern
2009-12-15 16:28                                                                                                         ` Linus Torvalds
2009-12-15 18:57                                                                                                           ` Linus Torvalds
2009-12-15 20:26                                                                                                           ` Alan Stern
2009-12-15 21:26                                                                                                             ` Rafael J. Wysocki
2009-12-15 22:01                                                                                                               ` Alan Stern
2009-12-15 21:54                                                                                                             ` Linus Torvalds
2009-12-15 22:27                                                                                                               ` Alan Stern
2009-12-16  2:11                                                                                                       ` Rafael J. Wysocki
2009-12-16  6:40                                                                                                         ` Dmitry Torokhov
2009-12-18 22:43                                                                                                           ` Rafael J. Wysocki
2009-12-19 19:59                                                                                                             ` Dmitry Torokhov
2009-12-19 21:33                                                                                                               ` Rafael J. Wysocki
2009-12-19 22:29                                                                                                                 ` Rafael J. Wysocki
2009-12-19 22:43                                                                                                                   ` Dmitry Torokhov
2009-12-19 22:47                                                                                                                 ` Dmitry Torokhov
2009-12-19 23:10                                                                                                                   ` Rafael J. Wysocki
2009-12-19 23:22                                                                                                                     ` Dmitry Torokhov
2009-12-19 23:33                                                                                                                       ` Rafael J. Wysocki
2009-12-19 23:23                                                                                                                     ` Linus Torvalds
2009-12-19 23:40                                                                                                                       ` Rafael J. Wysocki
2009-12-19 23:46                                                                                                                         ` Linus Torvalds
2009-12-19 23:47                                                                                                                           ` Linus Torvalds
2009-12-19 23:54                                                                                                                             ` Rafael J. Wysocki
2009-12-19 23:53                                                                                                                           ` Rafael J. Wysocki
2009-12-20  0:09                                                                                                                             ` Linus Torvalds
2009-12-20  0:35                                                                                                                               ` Rafael J. Wysocki
2009-12-20  2:41                                                                                                                               ` Dmitry Torokhov
2009-12-20 19:25                                                                                                                                 ` [linux-pm] " Rafael J. Wysocki
2009-12-21  7:39                                                                                                                                   ` [linux-pm] Async suspend-resume patch w/ completions (was: Re: Async?suspend-resume " Dmitry Torokhov
2009-12-21 11:20                                                                                                                                     ` Vojtech Pavlik
2009-12-20  2:45                                                                                                                             ` Async suspend-resume patch w/ completions (was: Re: Async suspend-resume " Dmitry Torokhov
2009-12-20  3:59                                                                                                                         ` Alan Stern
2009-12-20 12:52                                                                                                                           ` Rafael J. Wysocki
2009-12-20 17:12                                                                                                                             ` Alan Stern
2009-12-20 18:10                                                                                                                               ` Rafael J. Wysocki
2009-12-20 19:38                                                                                                                                 ` Alan Stern
2009-12-20 19:51                                                                                                                                   ` Rafael J. Wysocki
2009-12-16 15:22                                                                                                         ` Alan Stern
2009-12-16 19:26                                                                                                           ` Rafael J. Wysocki
2009-12-16 15:47                                                                                                         ` Linus Torvalds
2009-12-16 19:27                                                                                                           ` Rafael J. Wysocki
2009-12-16 20:59                                                                                                             ` Linus Torvalds
2009-12-16 21:57                                                                                                               ` Rafael J. Wysocki
2009-12-16 22:11                                                                                                                 ` Linus Torvalds
2009-12-16 22:33                                                                                                                   ` Rafael J. Wysocki
2009-12-16 23:04                                                                                                                 ` Alan Stern
2009-12-16 23:18                                                                                                                   ` Rafael J. Wysocki
2009-12-17  1:30                                                                                                                     ` Rafael J. Wysocki
2009-12-17  1:49                                                                                                                 ` Rafael J. Wysocki
2009-12-17 20:06                                                                                                                   ` Alan Stern
2009-12-17 20:36                                                                                                                     ` Rafael J. Wysocki
2009-12-18  1:51                                                                                                                   ` Rafael J. Wysocki
2009-12-18 17:26                                                                                                                     ` Alan Stern
2009-12-19 21:41                                                                                                                       ` Rafael J. Wysocki
2009-12-20  3:48                                                                                                                         ` Alan Stern
2009-12-20 12:55                                                                                                                           ` Rafael J. Wysocki
2009-12-18 23:42                                                                                                                     ` Rafael J. Wysocki
2009-12-13 13:08                                                                                       ` Rafael J. Wysocki
2009-12-13 17:30                                                                                       ` Alan Stern
2009-12-13 19:02                                                                                         ` [linux-pm] " Alan Stern
2009-12-12  0:43                                                                                 ` Alan Stern
2009-12-12 17:35                                                                                   ` Rafael J. Wysocki
2009-12-10 15:31                                                                 ` Alan Stern
2009-12-10 15:45                                                                   ` Linus Torvalds
2009-12-10 18:37                                                                     ` Alan Stern
2009-12-10 23:51                                                                       ` Linus Torvalds
2009-12-10 21:14                                                                   ` Rafael J. Wysocki
2009-12-10 22:17                                                                     ` Alan Stern
2009-12-10 23:45                                                                       ` Rafael J. Wysocki
2009-12-07 15:15                   ` [GIT PULL] PM updates for 2.6.33 Rafael J. Wysocki
2009-12-07 16:37                     ` Linus Torvalds
2009-12-07 20:47                       ` Rafael J. Wysocki
2009-12-07 20:56                         ` Linus Torvalds
2009-12-07  5:20               ` Linus Torvalds
2009-12-07 15:42                 ` Alan Stern
2009-12-06 19:35             ` Arjan van de Ven
2009-12-06 19:58               ` Linus Torvalds
2009-12-06 20:18                 ` Arjan van de Ven
2009-12-06 21:08                   ` Linus Torvalds
2009-12-06 22:54                   ` Dmitry Torokhov
2009-12-07  0:55                     ` Arjan van de Ven
2009-12-07  2:27                       ` Dmitry Torokhov
2009-12-07  5:26                         ` Arjan van de Ven
2009-12-07  6:00                           ` Dmitry Torokhov
2009-12-21  9:01                             ` Pavel Machek
2009-12-07  1:18                     ` Arjan van de Ven
2009-12-07  2:27                       ` Dmitry Torokhov
2009-12-07  5:31                         ` Arjan van de Ven
2009-12-07  6:15                           ` Dmitry Torokhov
2009-12-07  6:31                             ` Arjan van de Ven
2009-12-07  6:32                               ` Dmitry Torokhov
2009-12-07 15:17                                 ` Rafael J. Wysocki
2009-12-06 20:36               ` Alan Stern
2009-12-06 21:17                 ` Arjan van de Ven
2009-12-06 21:46                   ` Alan Stern
2009-12-06 21:57                     ` Arjan van de Ven
2009-12-06 22:04                       ` Alan Stern
2009-12-06  0:29   ` Rafael J. Wysocki
2009-12-06  0:52     ` Linus Torvalds
2009-12-06  1:24       ` Rafael J. Wysocki
2009-12-06  1:50         ` Linus Torvalds

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=alpine.LFD.2.00.0912070737150.3560@localhost.localdomain \
    --to=torvalds@linux-foundation.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@lists.linux-foundation.org \
    --cc=rjw@sisk.pl \
    --cc=rui.zhang@intel.com \
    --cc=stern@rowland.harvard.edu \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).