Skip to contentSkip to conversation

PM / Hibernate: Fix s2disk regression related to unlock_system_sleep()

PATCH New Rafael J. Wysocki 4 participants 21 messages Jan 17, 2012 – Jan 19, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PM / Hibernate: Fix s2disk regression related to unlock_system_sleep()

From: Rafael J. Wysocki <[email protected]>

Commit bcda53faf5814c0c6025a0bd47108adfcbe9f199, "PM / Sleep: Replace mutex_[un]lock(&pm_mutex) with [un]lock_system_sleep()", modified snapshot_read() and snapshot_write() in kernel/power/user.c, among other things, by making them use lock_system_sleep() and unlock_system_sleep() instead of just locking and unlocking pm_mutex. Unfortunately, however, this was a mistake, because these routines are supposed to be executed after processes have been frozen (i.e. when system_freezing_cnt is nonzero), so when unlock_system_sleep() is executed by one of them, this causes the caller to execute try_to_freeze() and go into the refrigerator as a result. This, in turn, deadlocks the suspend process and locks up the system.

Fix the problem by reverting the part of commit bcda53faf5814c0c6025a that changed snapshot_read() and snapshot_write(). Additionally, make these functions check system_freezing_cnt and return error codes if it is zero to ensure that they won't do anything if tasks have not been frozen.

---
 kernel/power/user.c |   14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
kernel/power/user.c +10 -4
Index: linux/kernel/power/user.c
===================================================================
@@ -137,7 +137,10 @@ static ssize_t snapshot_read(struct file
 	ssize_t res;
 	loff_t pg_offp = *offp & ~PAGE_MASK;
 
-	lock_system_sleep();
+	if (!atomic_read(&system_freezing_cnt))
+		return -EBUSY;
+
+	mutex_lock(&pm_mutex);
 
 	data = filp->private_data;
 	if (!data->ready) {
@@ -158,7 +161,7 @@ static ssize_t snapshot_read(struct file
 		*offp += res;
 
  Unlock:
-	unlock_system_sleep();
+	mutex_unlock(&pm_mutex);
 
 	return res;
 }
@@ -170,7 +173,10 @@ static ssize_t snapshot_write(struct fil
 	ssize_t res;
 	loff_t pg_offp = *offp & ~PAGE_MASK;
 
-	lock_system_sleep();
+	if (!atomic_read(&system_freezing_cnt))
+		return -EBUSY;
+
+	mutex_lock(&pm_mutex);
 
 	data = filp->private_data;
 
@@ -187,7 +193,7 @@ static ssize_t snapshot_write(struct fil
 	if (res > 0)
 		*offp += res;
 unlock:
-	unlock_system_sleep();
+	mutex_unlock(&pm_mutex);
 
 	return res;
 }
#1 lore raw

PATCH 1/1 PM / Hibernate: Fix s2disk regression related to unlock_system_sleep()

From: Rafael J. Wysocki <[email protected]>
Subject: PM / Hibernate: Fix s2disk regression related to unlock_system_sleep()

Commit bcda53faf5814c0c6025a0bd47108adfcbe9f199, "PM / Sleep: Replace mutex_[un]lock(&pm_mutex) with [un]lock_system_sleep()", modified snapshot_read() and snapshot_write() in kernel/power/user.c, among other things, by making them use lock_system_sleep() and unlock_system_sleep() instead of just locking and unlocking pm_mutex. Unfortunately, however, this was a mistake, because these routines are supposed to be executed after processes have been frozen (i.e. when system_freezing_cnt is nonzero), so when unlock_system_sleep() is executed by one of them, this causes the caller to execute try_to_freeze() and go into the refrigerator as a result. This, in turn, deadlocks the suspend process and locks up the system.

Fix the problem by reverting the part of commit bcda53faf5814c0c6025a that changed snapshot_read() and snapshot_write().

---

Well, it wasn't a good idea to make snapshot_write() check system_freezing_cnt, because the resume process doesn't freeze tasks. So, it's better to simply revert the changes that introduced the original regression.

Thanks,
Rafael

---
 kernel/power/user.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/power/user.c +4 -4
Index: linux/kernel/power/user.c
===================================================================
@@ -137,7 +137,7 @@ static ssize_t snapshot_read(struct file
 	ssize_t res;
 	loff_t pg_offp = *offp & ~PAGE_MASK;
 
-	lock_system_sleep();
+	mutex_lock(&pm_mutex);
 
 	data = filp->private_data;
 	if (!data->ready) {
@@ -158,7 +158,7 @@ static ssize_t snapshot_read(struct file
 		*offp += res;
 
  Unlock:
-	unlock_system_sleep();
+	mutex_unlock(&pm_mutex);
 
 	return res;
 }
@@ -170,7 +170,7 @@ static ssize_t snapshot_write(struct fil
 	ssize_t res;
 	loff_t pg_offp = *offp & ~PAGE_MASK;
 
-	lock_system_sleep();
+	mutex_lock(&pm_mutex);
 
 	data = filp->private_data;
 
@@ -187,7 +187,7 @@ static ssize_t snapshot_write(struct fil
 	if (res > 0)
 		*offp += res;
 unlock:
-	unlock_system_sleep();
+	mutex_unlock(&pm_mutex);
 
 	return res;
 }
#2 lore raw
Quoted 16 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>
Subject: PM / Hibernate: Fix s2disk regression related to unlock_system_sleep()

Commit bcda53faf5814c0c6025a0bd47108adfcbe9f199, "PM / Sleep: Replace mutex_[un]lock(&pm_mutex) with [un]lock_system_sleep()", modified snapshot_read() and snapshot_write() in kernel/power/user.c, among other things, by making them use lock_system_sleep() and unlock_system_sleep() instead of just locking and unlocking pm_mutex. Unfortunately, however, this was a mistake, because these routines are supposed to be executed after processes have been frozen (i.e. when system_freezing_cnt is nonzero), so when unlock_system_sleep() is executed by one of them, this causes the caller to execute try_to_freeze() and go into the refrigerator as a result. This, in turn, deadlocks the suspend process and locks up the system.

Quoted 5 lines

Fix the problem by reverting the part of commit bcda53faf5814c0c6025a that changed snapshot_read() and snapshot_write().

---

This quick fix is good to fix the regression, but I am wondering if going further, it would be better to rewrite unlock_system_sleep() by open-coding a part of freezer_count() and excluding try_to_freeze() in that.

When freezer_count() is used as it is (in other parts of the kernel), it makes sense (that is, the try_to_freeze() embedded within it is justified).

However, in the case of unlock_system_sleep(), I don't quite see why try_to_freeze() would be useful in any case at all... And moreover, it is also semantically misleading because, unlock_system_sleep() tries to freeze us behind our back, when we only intended to grab and release the pm_mutex safely (as advertised by these APIs). IOW, commit bcda53f, while promising to make stuff freezer-safe and nothing else, introduced a fundamental, unneeded functionality change in all call paths that these APIs are invoked in, by having that try_to_freeze() hidden inside. And now we just realized that this change is not just unneeded, but it is harmful too!

So how about something like the following patch?
(This is not on top of your patch. Rather, this patch is an alternative to your fix. Please let me know your thoughts.)

----
From: Srivatsa S. Bhat <[email protected]>
Subject: [PATCH] PM / Hibernate: Rewrite unlock_system_sleep() to fix s2disk regression

Commit 33e638b, "PM / Sleep: Use the freezer_count() functions in [un]lock_system_sleep() APIs" introduced an undesirable change in the behaviour of unlock_system_sleep() since freezer_count() internally calls try_to_freeze() - which we don't need in unlock_system_sleep().

And commit bcda53f, "PM / Sleep: Replace mutex_[un]lock(&pm_mutex) with [un]lock_system_sleep()" made these APIs wide-spread. This caused a regression in suspend-to-disk where snapshot_read() and snapshot_write() were getting frozen due to the try_to_freeze embedded in unlock_system_sleep(), since these functions were invoked when the freezing condition was still in effect.

Fix this by rewriting unlock_system_sleep() by open-coding freezer_count() and dropping the try_to_freeze() part. Not only will this fix the regression but this will also ensure that the API only does what it is intended to do, and nothing more, under the hood.

---

 include/linux/suspend.h |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
include/linux/suspend.h +5 -1
index 95040cc..a1db01f 100644
@@ -364,7 +364,11 @@ static inline void lock_system_sleep(void)
 static inline void unlock_system_sleep(void)
 {
 	mutex_unlock(&pm_mutex);
-	freezer_count();
+	/*
+	 * Don't use freezer_count() because we don't want the
+	 * call to try_to_freeze() here.
+	 */
+	current->flags &= ~PF_FREEZER_SKIP;
 }
 
 #else /* !CONFIG_PM_SLEEP */
#3 lore raw

Hello,

Quoted 4 lines · Srivatsa S. Bhat

+       /*
+        * Don't use freezer_count() because we don't want the
+        * call to try_to_freeze() here.
+        */

Please explain "why" here.

Thanks.

Signature · 1 line
tejun
#4 lore raw

Hello Tejun,

Quoted 11 lines · Tejun Heo

Hello,

Review

Quoted 4 lines · Srivatsa S. Bhat
(quoted diff) +4 -0
+       /*
+        * Don't use freezer_count() because we don't want the
+        * call to try_to_freeze() here.
+        */

Please explain "why" here.

Ok, how about the following patch?
----
From: Srivatsa S. Bhat <[email protected]>
Subject: [PATCH] PM / Hibernate: Rewrite unlock_system_sleep() to fix s2disk regression

Commit 33e638b, "PM / Sleep: Use the freezer_count() functions in [un]lock_system_sleep() APIs" introduced an undesirable change in the behaviour of unlock_system_sleep() since freezer_count() internally calls try_to_freeze() - which we don't need in unlock_system_sleep().

And commit bcda53f, "PM / Sleep: Replace mutex_[un]lock(&pm_mutex) with [un]lock_system_sleep()" made these APIs wide-spread. This caused a regression in suspend-to-disk where snapshot_read() and snapshot_write() were getting frozen due to the try_to_freeze embedded in unlock_system_sleep(), since these functions were invoked when the freezing condition was still in effect.

Fix this by rewriting unlock_system_sleep() by open-coding freezer_count() and dropping the try_to_freeze() part. Not only will this fix the regression but this will also ensure that the API only does what it is intended to do, and nothing more, under the hood.

---

 include/linux/suspend.h |   13 ++++++++++++-
 1 files changed, 12 insertions(+), 1 deletions(-)
include/linux/suspend.h +12 -1
index 95040cc..6e76380 100644
@@ -364,7 +364,18 @@ static inline void lock_system_sleep(void)
 static inline void unlock_system_sleep(void)
 {
 	mutex_unlock(&pm_mutex);
-	freezer_count();
+
+	/*
+	 * Don't use freezer_count() because we don't want the call to
+	 * try_to_freeze() here.
+	 *
+	 * Reason:
+	 * unlock_system_sleep() gets called in snapshot_read() and
+	 * snapshot_write() when the freezing condition is still in effect.
+	 * Which means, if we use try_to_freeze() here, it would make them
+	 * enter the refrigerator, thus causing suspend-to-disk to lockup.
+	 */
+	current->flags &= ~PF_FREEZER_SKIP;
 }
 
 #else /* !CONFIG_PM_SLEEP */
#5 lore raw

Hello,

Quoted 10 lines · Srivatsa S. Bhat

+       /*
+        * Don't use freezer_count() because we don't want the call to
+        * try_to_freeze() here.
+        *
+        * Reason:
+        * unlock_system_sleep() gets called in snapshot_read() and
+        * snapshot_write() when the freezing condition is still in effect.
+        * Which means, if we use try_to_freeze() here, it would make them
+        * enter the refrigerator, thus causing suspend-to-disk to lockup.
+        */

Yeap, much better but wouldn't it be better if the description was higher level? ie. Explain why try_to_freeze() doesn't make sense for this operation semantically and then, optionally, give an example of breakage. It usually is lack of proper rationale / reasoning that confuses people reading the code.

Thank you.

Signature · 1 line
tejun
#6 lore raw

Hello Tejun,

Quoted 15 lines · Tejun Heo

Hello,

include/linux/suspend.h static inline void lock_system_sleep(void) · line 377

Quoted 10 lines · Srivatsa S. Bhat
include/linux/suspend.h +10 -0
+       /*
+        * Don't use freezer_count() because we don't want the call to
+        * try_to_freeze() here.
+        *
+        * Reason:
+        * unlock_system_sleep() gets called in snapshot_read() and
+        * snapshot_write() when the freezing condition is still in effect.
+        * Which means, if we use try_to_freeze() here, it would make them
+        * enter the refrigerator, thus causing suspend-to-disk to lockup.
+        */
Quoted 6 lines

Yeap, much better but wouldn't it be better if the description was higher level? ie. Explain why try_to_freeze() doesn't make sense for this operation semantically and then, optionally, give an example of breakage. It usually is lack of proper rationale / reasoning that confuses people reading the code.

I agree, but I was trying to keep the comment from growing too long ;)

In fact, before updating that patch, I wrote up the following description but then didn't post it. I know you are asking me to update the comment in the code, but let me post my original description as well, atleast just because I took the effort to write it up ;)

Explanation as to why try_to_freeze() must not be used in unlock_system_sleep():

We don't want try_to_freeze() here because of 2 reasons:

a) It is unnecessary at this point:
Freezing always begins and ends in a piece of code which is protected by pm_mutex.

Some code piece, say "X": PM/Freezer call path:

grab pm_mutex
start freezing tasks
[-- lock_system_sleep() --]
skip freezing "X" since
it called freezer_do_not_count()

freezing succeeded. Other things get done (suspend or hibernate etc).

release pm_mutex

lock_system_sleep()
(it is only *now* that this
function returned since pm_mutex
had been grabbed all this while
by the PM path.)

Let us say another PM operation like suspend/hibernate begins.
So try to grab pm_mutex.

/* do something */

unlock_system_sleep()
//Point 1

It is only now that we acquire pm_mutex.
So freezing for example, can start only now. //Point 2

Quoted 1 line

From the above depiction, it is clear that try_to_freeze() at Point 1 is

pointless because freezing can only begin much later, at Point 2. When freezer_count() is called in some generic code, the try_to_freeze() is justified because, we had asked the freezer to skip us earlier, and now we want the freezer to consider us again. And hence, just in case the freezer is about to give up based on its 20 second timeout, we are extra-cooperative
- we immediately call try_to_freeze() ourselves.

However, in the unlock_system_sleep() case, the freezer won't be anywhere near its 20 second timeout, because freezing wouldn't have begun at all! Freezing would start only after we release the pm_mutex in unlock_system_sleep.

So, considering all this, try_to_freeze() is not needed in unlock_system_sleep() path.

b) A more important reason why try_to_freeze() should not be used here is that it causes suspend-to-disk to lock up, as pointed out by Rafael. So its no longer "pointless/unnecessary", it is now "known to make things fail and hence not allowed".

//End of my description :)

I'll see how best I can summarize this in a short comment and post an updated patch when I'm done.

Regards,
Srivatsa S. Bhat

#7 lore raw

Hello, Srivatsa.

Quoted 1 line · Srivatsa S. Bhat

I agree, but I was trying to keep the comment from growing too long ;)

It doesn't have to be long. It just has to give some meaning to the decision. AFAICS, it is correct to call try_to_freeze() on unlock_system_sleep() regardless of 20sec window. There's no guarantee the unlocking task is gonna hit try_to_freeze() elsewhere and not calling it actually makes the interface buggy.

That said, it causes a problem because unlock_system_sleep() is called in a special context during later stage of hibernation where the usual expectation - that a freezable task which sees a freezing condition should freeze - doesn't hold.

The correct solution would be somehow marking that condition so that either try_to_freeze() doesn't get invoked or gets nullified - e.g. making the SKIP thing a counter and ensure the hibernating task has it elevated throughout the whole process. Alternatively, if the code path is limited enough, using a different version of the unlock function, unlock_system_sleep_nofreeze() or whatever, would work too - this is a popular approach for synchronization functions which interacts with scheduler and preemption.

For now, as a quick fix, maybe not calling try_to_freeze() unconditionally is okay, I don't know, but it's a hack.

Thanks.

Signature · 1 line
tejun
#8 lore raw
Quoted 10 lines · Tejun Heo

Hello, Srivatsa.

Quoted 1 line · Srivatsa S. Bhat

I agree, but I was trying to keep the comment from growing too long ;)

It doesn't have to be long. It just has to give some meaning to the decision. AFAICS, it is correct to call try_to_freeze() on unlock_system_sleep() regardless of 20sec window. There's no guarantee the unlocking task is gonna hit try_to_freeze() elsewhere and not calling it actually makes the interface buggy.

To give an example,

/*
* XXX: Open code SKIP clearing for now to avoid invoking
* try_to_freeze(). This isn't correct but this function is
* called from deep inside hibernation path and calling
* try_to_freeze() leads to hang during hibernation. This
* will be properly fixed soon. See commit message for
* more details.
*/

Thanks.

Signature · 1 line
tejun
#9 lore raw

Hi!

Quoted 10 lines
Quoted 7 lines
Quoted 1 line

I agree, but I was trying to keep the comment from growing too long ;)

It doesn't have to be long. It just has to give some meaning to the decision. AFAICS, it is correct to call try_to_freeze() on unlock_system_sleep() regardless of 20sec window. There's no guarantee the unlocking task is gonna hit try_to_freeze() elsewhere and not calling it actually makes the interface buggy.

To give an example,

Quoted 8 lines

/*
* XXX: Open code SKIP clearing for now to avoid invoking
* try_to_freeze(). This isn't correct but this function is
* called from deep inside hibernation path and calling
* try_to_freeze() leads to hang during hibernation. This
* will be properly fixed soon. See commit message for
* more details.
*/

Which paths are affected?

With uswsusp, we have userland in control of hibernation process; I'd say almost anything can be called...
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#10 lore raw
Quoted 21 lines · Pavel Machek

Hi!

Quoted 10 lines
Quoted 7 lines
Quoted 1 line

I agree, but I was trying to keep the comment from growing too long ;)

It doesn't have to be long. It just has to give some meaning to the decision. AFAICS, it is correct to call try_to_freeze() on unlock_system_sleep() regardless of 20sec window. There's no guarantee the unlocking task is gonna hit try_to_freeze() elsewhere and not calling it actually makes the interface buggy.

To give an example,

Quoted 8 lines

/*
* XXX: Open code SKIP clearing for now to avoid invoking
* try_to_freeze(). This isn't correct but this function is
* called from deep inside hibernation path and calling
* try_to_freeze() leads to hang during hibernation. This
* will be properly fixed soon. See commit message for
* more details.
*/

Quoted 4 lines

Which paths are affected?

With uswsusp, we have userland in control of hibernation process; I'd say almost anything can be called...

Hi Pavel,
I am afraid you are looking at a stale comment. We finalized on a different comment altogether.

Here is the latest patch:
http://thread.gmane.org/gmane.linux.kernel/1240493/focus=1240976

Rafael made some suggestions on this one here
http://thread.gmane.org/gmane.linux.kernel/1240493/focus=1241028

I will address that and send out a patch soon.

Do let me know if you have any comments, thanks!

Regards,
Srivatsa S. Bhat

#11 lore raw

Hi!

Quoted 13 lines
Quoted 13 lines
Quoted 10 lines

To give an example,

/*
* XXX: Open code SKIP clearing for now to avoid invoking
* try_to_freeze(). This isn't correct but this function is
* called from deep inside hibernation path and calling
* try_to_freeze() leads to hang during hibernation. This
* will be properly fixed soon. See commit message for
* more details.
*/

Which paths are affected?

Quoted 7 lines
Quoted 2 lines

With uswsusp, we have userland in control of hibernation process; I'd say almost anything can be called...

Hi Pavel,
I am afraid you are looking at a stale comment. We finalized on a different comment altogether.

I know. But the underlying problem (uswsusp code is userland, and can do anything) is still there, right? I guess limitations for applications using uswsusp interface should be documented somewhere.
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#12 lore raw
Quoted 19 lines · Pavel Machek

Hi!

Quoted 17 lines
Quoted 15 lines
Quoted 10 lines

To give an example,

/*
* XXX: Open code SKIP clearing for now to avoid invoking
* try_to_freeze(). This isn't correct but this function is
* called from deep inside hibernation path and calling
* try_to_freeze() leads to hang during hibernation. This
* will be properly fixed soon. See commit message for
* more details.
*/

Which paths are affected?

With uswsusp, we have userland in control of hibernation process; I'd say almost anything can be called...

Quoted 7 lines
Quoted 3 lines

Hi Pavel,
I am afraid you are looking at a stale comment. We finalized on a different comment altogether.

I know. But the underlying problem (uswsusp code is userland, and can do anything) is still there, right? I guess limitations for applications using uswsusp interface should be documented somewhere.

Sorry, but I think you missed the conclusion of the discussion in this
thread: The comment mentioned above is completely wrong! Rewriting unlock_system_sleep() by open coding the SKIP clearing and omitting the try_to_freeze() part is not a hack - instead, it is a clean and sane design. Hence, with the latest version of my patch applied, it would no longer be "hey, please watch out for this, this and this for now; we know things are messy but we will fix it up later". Instead it is like "Don't worry, we have fixed up the buggy unlock_system_sleep() function. Go ahead and do what you want!"

On a more serious note, here is a (hopefully) convincing explanation as to why the patch makes perfect sense:

http://thread.gmane.org/gmane.linux.kernel/1240493/focus=1240892
and
http://thread.gmane.org/gmane.linux.kernel/1240493/focus=1240940

Moreover, we have already advised people to use the lock_system_sleep() and unlock_system_sleep() APIs instead of directly using mutex_lock and mutex_unlock on pm_mutex, in Documentation/power/freezing-of-tasks.txt

But unfortunately, unlock_system_sleep() was actually not quite correct and hence this patch fixes it. So things should be fine now...

But, in case you were hinting at some totally different problem, please let me know..

Regards,
Srivatsa S. Bhat

#13 lore raw
In reply to Tejun Heo
Quoted 11 lines · Tejun Heo

Hello, Srivatsa.

Quoted 1 line · Srivatsa S. Bhat

I agree, but I was trying to keep the comment from growing too long ;)

It doesn't have to be long. It just has to give some meaning to the decision. AFAICS, it is correct to call try_to_freeze() on unlock_system_sleep() regardless of 20sec window. There's no guarantee the unlocking task is gonna hit try_to_freeze() elsewhere and not calling it actually makes the interface buggy.

Not really -

In short, there is a *key* difference between:

freezer_do_not_count() - freezer_count()

vs

lock_system_sleep() - unlock_system_sleep()

And that is, the latter pair acquire and release pm_mutex - and the freezer doesn't do *anything* without first acquiring the same pm_mutex.

Which is convincing enough to start believing that try_to_freeze() might be unnecessary in the latter pair.

Now, more explanation follows:
Consider the 2 cases below:

Some random code:

Case 1:

/* Was doing something */

freezer_do_not_count();

/* Important work */

freezer_count();

Case 2:

/* Was doing something */

lock_system_sleep();

/* Important work */

unlock_system_sleep();

In case 1, we asked the freezer to ignore us until we are done with some "important" work. And precisely because we interfered with the freezer trying to freeze us, it is our duty to cooperate with the freezer when our "important" work is finished. Which is why we call try_to_freeze() inside freezer_count().

However, in case 2, the same story as above applies, but with a catch:
After we asked the freezer to ignore us, we could not even *start* doing our "important" work! Because, we were waiting for pm_mutex, since the freezer had got hold of it!

So we get to do our "important work" only when the freezer and all other PM operations after that is done. And here is another catch: Once we start doing our "important work", no PM operation (freezing in particular) can begin, because now *we* have acquired the pm_mutex :-)

So to summarize, calling try_to_freeze() inside some generic freezer_count() is justified and _needed_. But in case of unlock_system_sleep(), as I depicted in my previous post, it wouldn't buy us anything at all! (because freezing wouldn't have even begun by then).

[ And in the case of freezer_count(), your point is right that calling try_to_freeze() is not to help the freezer meet its 20sec deadline - the freezer could have finished its work and gone away long before we completed our "important" work. So the real reason why we call try_to_freeze in freezer_count is that when some PM operation is running which requires tasks to be frozen, we want to respect that condition and get frozen - but we probably ended up totally bypassing the freezer by calling freezer_do_not_count, but now that we are done with our important work, we want to manually enter the refrigerator, to satisfy the freezing condition requested by the PM operation in progress and hence avoid any havoc by running any further.]

Quoted 7 lines

That said, it causes a problem because unlock_system_sleep() is called in a special context during later stage of hibernation where the usual expectation - that a freezable task which sees a freezing condition should freeze - doesn't hold.

The correct solution would be somehow marking that condition so that either try_to_freeze() doesn't get invoked or gets nullified -

Even I thought this would be the right solution at first, but then realized that having try_to_freeze() inside unlock_system_sleep() is not at all essential, to begin with.

And not having try_to_freeze() within unlock_system_sleep() automatically solves the hibernation problem. In fact, it does even better:
Later stages of hibernation would no longer be a "special context" to watch out for!

Quoted 10 lines

e.g. making the SKIP thing a counter and ensure the hibernating task has it elevated throughout the whole process. Alternatively, if the code path is limited enough, using a different version of the unlock function, unlock_system_sleep_nofreeze() or whatever, would work too - this is a popular approach for synchronization functions which interacts with scheduler and preemption.

For now, as a quick fix, maybe not calling try_to_freeze() unconditionally is okay, I don't know, but it's a hack.

Somehow I don't think its a hack, based on my perception as described above. But feel free to prove me wrong :-)

Regards,
Srivatsa S. Bhat

#14 lore raw

Hello,

Quoted 2 lines · Srivatsa S. Bhat

Somehow I don't think its a hack, based on my perception as described above. But feel free to prove me wrong :-)

Thanks for the explanation. Yeah, I agree and it's much simpler this way, which is nice. So, in short, because freezing state can't change across lock_system_sleep(), there's no reason to check for freezing state on unlock and this nicely resolves the freezer problem together.

The only thing to be careful is, then, we need to set and clear SKIP inside pm_mutex.

Thanks.

Signature · 1 line
tejun
#15 lore raw
Quoted 11 lines · Tejun Heo

Hello,

Quoted 2 lines · Srivatsa S. Bhat

Somehow I don't think its a hack, based on my perception as described above. But feel free to prove me wrong :-)

Thanks for the explanation. Yeah, I agree and it's much simpler this way, which is nice. So, in short, because freezing state can't change across lock_system_sleep(), there's no reason to check for freezing state on unlock and this nicely resolves the freezer problem together.

Absolutely!

Quoted 3 lines

The only thing to be careful is, then, we need to set and clear SKIP inside pm_mutex.

Not exactly. We need to set SKIP before grabbing pm_mutex and clear it inside pm_mutex. The reason is that we decided to set SKIP in the first place just to avoid the freezer from declaring failure when we are blocked on pm_mutex. If we move it to *after* mutex_lock(&pm_mutex), that original intention itself is not satisfied, and we will hit freezing failures - IOW making the set and clear exercise useless!

So, something like this should work perfectly:

lock_system_sleep()

{
	freezer_do_not_count();
	mutex_lock(&pm_mutex);
	current->flags &= ~PF_FREEZER_SKIP;
}

But in the interest of making the code look a bit symmetric, we can do:

lock_system_sleep()

{
	freezer_do_not_count();
	mutex_lock(&pm_mutex);
}

unlock_system_sleep()

{
	current->flags &= ~PF_FREEZER_SKIP;
	mutex_unlock(&pm_mutex);
}

Regards,
Srivatsa S. Bhat
IBM Linux Technology Center

#16 lore raw
Quoted 46 lines · Srivatsa S. Bhat
Quoted 11 lines · Tejun Heo

Hello,

Quoted 2 lines · Srivatsa S. Bhat

Somehow I don't think its a hack, based on my perception as described above. But feel free to prove me wrong :-)

Thanks for the explanation. Yeah, I agree and it's much simpler this way, which is nice. So, in short, because freezing state can't change across lock_system_sleep(), there's no reason to check for freezing state on unlock and this nicely resolves the freezer problem together.

Absolutely!

Quoted 3 lines

The only thing to be careful is, then, we need to set and clear SKIP inside pm_mutex.

Not exactly. We need to set SKIP before grabbing pm_mutex and clear it inside pm_mutex. The reason is that we decided to set SKIP in the first place just to avoid the freezer from declaring failure when we are blocked on pm_mutex. If we move it to *after* mutex_lock(&pm_mutex), that original intention itself is not satisfied, and we will hit freezing failures - IOW making the set and clear exercise useless!

So, something like this should work perfectly:

lock_system_sleep()

{
	freezer_do_not_count();
	mutex_lock(&pm_mutex);
	current->flags &= ~PF_FREEZER_SKIP;
}

But in the interest of making the code look a bit symmetric, we can do:

lock_system_sleep()

{
	freezer_do_not_count();
	mutex_lock(&pm_mutex);
}
Quoted 6 lines

unlock_system_sleep()

{
	current->flags &= ~PF_FREEZER_SKIP;
	mutex_unlock(&pm_mutex);
}

So how about this patch, with the comment updated?
(and the changelog updated as well to reflect the movement of code)

---
From: Srivatsa S. Bhat <[email protected]>
Subject: [PATCH] PM / Hibernate: Rewrite unlock_system_sleep() to fix s2disk regression

Commit 33e638b, "PM / Sleep: Use the freezer_count() functions in [un]lock_system_sleep() APIs" introduced an undesirable change in the behaviour of unlock_system_sleep() since freezer_count() internally calls try_to_freeze() - which we don't need in unlock_system_sleep().

And commit bcda53f, "PM / Sleep: Replace mutex_[un]lock(&pm_mutex) with [un]lock_system_sleep()" made these APIs wide-spread. This caused a regression in suspend-to-disk where snapshot_read() and snapshot_write() were getting frozen due to the try_to_freeze embedded in unlock_system_sleep(), since these functions were invoked when the freezing condition was still in effect.

Fix this by rewriting unlock_system_sleep() by open-coding freezer_count() and dropping the try_to_freeze() part. Not only will this fix the regression but this will also ensure that the API only does what it is intended to do, and nothing more, under the hood.

While at it, make the code more correct and robust by ensuring that the PF_FREEZER_SKIP flag gets cleared with pm_mutex held, to avoid a race with the freezer.

---

 include/linux/suspend.h |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)
include/linux/suspend.h +16 -1
index 95040cc..cb9d3f4 100644
@@ -363,8 +363,23 @@ static inline void lock_system_sleep(void)
 
 static inline void unlock_system_sleep(void)
 {
+	/*
+	 * Don't use freezer_count() because we don't want the call to
+	 * try_to_freeze() here.
+	 *
+	 * Reason:
+	 * Fundamentally, we just don't need it, because freezing condition
+	 * doesn't come into effect until we release the pm_mutex lock,
+	 * since the freezer always works with pm_mutex held.
+	 *
+	 * More importantly, in the case of hibernation,
+	 * unlock_system_sleep() gets called in snapshot_read() and
+	 * snapshot_write() when the freezing condition is still in effect.
+	 * Which means, if we use try_to_freeze() here, it would make them
+	 * enter the refrigerator, thus causing hibernation to lockup.
+	 */
+	current->flags &= ~PF_FREEZER_SKIP;
 	mutex_unlock(&pm_mutex);
-	freezer_count();
 }
 
 #else /* !CONFIG_PM_SLEEP */
Acked-by
#17 lore raw
Quoted 20 lines · Srivatsa S. Bhat

From: Srivatsa S. Bhat <[email protected]>
Subject: [PATCH] PM / Hibernate: Rewrite unlock_system_sleep() to fix s2disk regression

Commit 33e638b, "PM / Sleep: Use the freezer_count() functions in [un]lock_system_sleep() APIs" introduced an undesirable change in the behaviour of unlock_system_sleep() since freezer_count() internally calls try_to_freeze() - which we don't need in unlock_system_sleep().

And commit bcda53f, "PM / Sleep: Replace mutex_[un]lock(&pm_mutex) with [un]lock_system_sleep()" made these APIs wide-spread. This caused a regression in suspend-to-disk where snapshot_read() and snapshot_write() were getting frozen due to the try_to_freeze embedded in unlock_system_sleep(), since these functions were invoked when the freezing condition was still in effect.

Fix this by rewriting unlock_system_sleep() by open-coding freezer_count() and dropping the try_to_freeze() part. Not only will this fix the regression but this will also ensure that the API only does what it is intended to do, and nothing more, under the hood.

Quoted 6 lines

While at it, make the code more correct and robust by ensuring that the PF_FREEZER_SKIP flag gets cleared with pm_mutex held, to avoid a race with the freezer.

Thanks!

Signature · 1 line
tejun
#18 lore raw
In reply to Srivatsa S. Bhat
Quoted 80 lines · Srivatsa S. Bhat
Quoted 46 lines · Srivatsa S. Bhat
Quoted 11 lines · Tejun Heo

Hello,

Quoted 2 lines · Srivatsa S. Bhat

Somehow I don't think its a hack, based on my perception as described above. But feel free to prove me wrong :-)

Thanks for the explanation. Yeah, I agree and it's much simpler this way, which is nice. So, in short, because freezing state can't change across lock_system_sleep(), there's no reason to check for freezing state on unlock and this nicely resolves the freezer problem together.

Absolutely!

Quoted 3 lines

The only thing to be careful is, then, we need to set and clear SKIP inside pm_mutex.

Not exactly. We need to set SKIP before grabbing pm_mutex and clear it inside pm_mutex. The reason is that we decided to set SKIP in the first place just to avoid the freezer from declaring failure when we are blocked on pm_mutex. If we move it to *after* mutex_lock(&pm_mutex), that original intention itself is not satisfied, and we will hit freezing failures - IOW making the set and clear exercise useless!

So, something like this should work perfectly:

lock_system_sleep()

{
	freezer_do_not_count();
	mutex_lock(&pm_mutex);
	current->flags &= ~PF_FREEZER_SKIP;
}

But in the interest of making the code look a bit symmetric, we can do:

lock_system_sleep()

{
	freezer_do_not_count();
	mutex_lock(&pm_mutex);
}
Quoted 6 lines

unlock_system_sleep()

{
	current->flags &= ~PF_FREEZER_SKIP;
	mutex_unlock(&pm_mutex);
}

So how about this patch, with the comment updated?
(and the changelog updated as well to reflect the movement of code)

---
From: Srivatsa S. Bhat <[email protected]>
Subject: [PATCH] PM / Hibernate: Rewrite unlock_system_sleep() to fix s2disk regression

Commit 33e638b, "PM / Sleep: Use the freezer_count() functions in [un]lock_system_sleep() APIs" introduced an undesirable change in the behaviour of unlock_system_sleep() since freezer_count() internally calls try_to_freeze() - which we don't need in unlock_system_sleep().

And commit bcda53f, "PM / Sleep: Replace mutex_[un]lock(&pm_mutex) with [un]lock_system_sleep()" made these APIs wide-spread. This caused a regression in suspend-to-disk where snapshot_read() and snapshot_write() were getting frozen due to the try_to_freeze embedded in unlock_system_sleep(), since these functions were invoked when the freezing condition was still in effect.

Fix this by rewriting unlock_system_sleep() by open-coding freezer_count() and dropping the try_to_freeze() part. Not only will this fix the regression but this will also ensure that the API only does what it is intended to do, and nothing more, under the hood.

Quoted 6 lines

While at it, make the code more correct and robust by ensuring that the PF_FREEZER_SKIP flag gets cleared with pm_mutex held, to avoid a race with the freezer.

So, I'd prefer lock_system_sleep() to be open-coded too, to avoid situations in which someone changes freezer_do_not_count() and freezer_count() leaving unmodified unlock_system_sleep() behind.

Also, have you actually tested this?

Rafael

Quoted 42 lines

---

 include/linux/suspend.h |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)
include/linux/suspend.h +16 -2
index 95040cc..cb9d3f4 100644
@@ -363,8 +363,23 @@ static inline void lock_system_sleep(void)
 
 static inline void unlock_system_sleep(void)
 {
+	/*
+	 * Don't use freezer_count() because we don't want the call to
+	 * try_to_freeze() here.
+	 *
+	 * Reason:
+	 * Fundamentally, we just don't need it, because freezing condition
+	 * doesn't come into effect until we release the pm_mutex lock,
+	 * since the freezer always works with pm_mutex held.
+	 *
+	 * More importantly, in the case of hibernation,
+	 * unlock_system_sleep() gets called in snapshot_read() and
+	 * snapshot_write() when the freezing condition is still in effect.
+	 * Which means, if we use try_to_freeze() here, it would make them
+	 * enter the refrigerator, thus causing hibernation to lockup.
+	 */
+	current->flags &= ~PF_FREEZER_SKIP;
 	mutex_unlock(&pm_mutex);
-	freezer_count();
 }
 
 #else /* !CONFIG_PM_SLEEP */
 
 
--

To unsubscribe from this list: send the line "unsubscribe linux-pm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Acked-by Tejun Heo
#19 lore raw
Quoted 85 lines · Rafael J. Wysocki
Quoted 84 lines · Srivatsa S. Bhat
Quoted 46 lines · Srivatsa S. Bhat
Quoted 11 lines · Tejun Heo

Hello,

Quoted 2 lines · Srivatsa S. Bhat

Somehow I don't think its a hack, based on my perception as described above. But feel free to prove me wrong :-)

Thanks for the explanation. Yeah, I agree and it's much simpler this way, which is nice. So, in short, because freezing state can't change across lock_system_sleep(), there's no reason to check for freezing state on unlock and this nicely resolves the freezer problem together.

Absolutely!

Quoted 3 lines

The only thing to be careful is, then, we need to set and clear SKIP inside pm_mutex.

Not exactly. We need to set SKIP before grabbing pm_mutex and clear it inside pm_mutex. The reason is that we decided to set SKIP in the first place just to avoid the freezer from declaring failure when we are blocked on pm_mutex. If we move it to *after* mutex_lock(&pm_mutex), that original intention itself is not satisfied, and we will hit freezing failures - IOW making the set and clear exercise useless!

So, something like this should work perfectly:

lock_system_sleep()

{
	freezer_do_not_count();
	mutex_lock(&pm_mutex);
	current->flags &= ~PF_FREEZER_SKIP;
}

But in the interest of making the code look a bit symmetric, we can do:

lock_system_sleep()

{
	freezer_do_not_count();
	mutex_lock(&pm_mutex);
}
Quoted 6 lines

unlock_system_sleep()

{
	current->flags &= ~PF_FREEZER_SKIP;
	mutex_unlock(&pm_mutex);
}

So how about this patch, with the comment updated?
(and the changelog updated as well to reflect the movement of code)

---
From: Srivatsa S. Bhat <[email protected]>
Subject: [PATCH] PM / Hibernate: Rewrite unlock_system_sleep() to fix s2disk regression

Commit 33e638b, "PM / Sleep: Use the freezer_count() functions in [un]lock_system_sleep() APIs" introduced an undesirable change in the behaviour of unlock_system_sleep() since freezer_count() internally calls try_to_freeze() - which we don't need in unlock_system_sleep().

And commit bcda53f, "PM / Sleep: Replace mutex_[un]lock(&pm_mutex) with [un]lock_system_sleep()" made these APIs wide-spread. This caused a regression in suspend-to-disk where snapshot_read() and snapshot_write() were getting frozen due to the try_to_freeze embedded in unlock_system_sleep(), since these functions were invoked when the freezing condition was still in effect.

Fix this by rewriting unlock_system_sleep() by open-coding freezer_count() and dropping the try_to_freeze() part. Not only will this fix the regression but this will also ensure that the API only does what it is intended to do, and nothing more, under the hood.

While at it, make the code more correct and robust by ensuring that the PF_FREEZER_SKIP flag gets cleared with pm_mutex held, to avoid a race with the freezer.

Quoted 7 lines
Quoted 2 lines

So, I'd prefer lock_system_sleep() to be open-coded too, to avoid situations in which someone changes freezer_do_not_count() and freezer_count() leaving unmodified unlock_system_sleep() behind.

Hehe, actually, I had left lock_system_sleep() as it is, intentionally, due to a far-fetched idea to keep the code sane: I felt that people who go through this code would naturally feel tempted to replace any open-coded stuff (if possible) with the appropriate function call. And my rationale was that when they see that lock_system_sleep() has a function call but unlock_system_sleep() has been open-coded, they would immediately know that this was not yet another case of "oh I wasn't aware of this function, hence I open-coded it", but it was rather "I was very much aware of the available functions, but I chose to open-code it intentionally - so touch it only if you really know what you are doing" :-)

And I guess, now that job of guarding the code from "obvious code cleanups" like that would be done by the comment I added as per Tejun's suggestion. And moreover, your point to open-code in lock_system_sleep() sounds more compelling than my far-fetched idea :-) So okay, I will open-code it..

Quoted 1 line

Also, have you actually tested this?

Oh I almost forgot to mention: I have not specifically tested this with the userspace s2disk utility (part of uswsusp/suspend-utils package) as such. I have only tested it in general (by invoking hibernate by writing to /sys/power/disk, and testing different levels of pm_test framework etc) to ensure that this patch doesn't break anything else.
So, any help with testing the userspace utilities would be great!

So Rafael, could you please check if the following patch solves the regression for you? Thanks a lot!

---
From: Srivatsa S. Bhat <[email protected]>
Subject: [PATCH] PM / Hibernate: Rewrite unlock_system_sleep() to fix s2disk regression

Commit 33e638b, "PM / Sleep: Use the freezer_count() functions in [un]lock_system_sleep() APIs" introduced an undesirable change in the behaviour of unlock_system_sleep() since freezer_count() internally calls try_to_freeze() - which we don't need in unlock_system_sleep().

And commit bcda53f, "PM / Sleep: Replace mutex_[un]lock(&pm_mutex) with [un]lock_system_sleep()" made these APIs wide-spread. This caused a regression in suspend-to-disk where snapshot_read() and snapshot_write() were getting frozen due to the try_to_freeze embedded in unlock_system_sleep(), since these functions were invoked when the freezing condition was still in effect.

Fix this by rewriting unlock_system_sleep() by open-coding freezer_count() and dropping the try_to_freeze() part. Not only will this fix the regression but this will also ensure that the API only does what it is intended to do, and nothing more, under the hood.

While at it, make the code more correct and robust by ensuring that the PF_FREEZER_SKIP flag gets cleared with pm_mutex held, to avoid a race with the freezer.

Also, to be on the safer side, open-code freezer_do_not_count() as well (inside lock_system_sleep()), to ensure that any unrelated modification to freezer[_do_not]_count() does not break things again!

---

 include/linux/suspend.h |   19 +++++++++++++++++--
 1 files changed, 17 insertions(+), 2 deletions(-)
include/linux/suspend.h +17 -2
index 95040cc..91784a4 100644
@@ -357,14 +357,29 @@ extern bool pm_save_wakeup_count(unsigned int count);
 
 static inline void lock_system_sleep(void)
 {
-	freezer_do_not_count();
+	current->flags |= PF_FREEZER_SKIP;
 	mutex_lock(&pm_mutex);
 }
 
 static inline void unlock_system_sleep(void)
 {
+	/*
+	 * Don't use freezer_count() because we don't want the call to
+	 * try_to_freeze() here.
+	 *
+	 * Reason:
+	 * Fundamentally, we just don't need it, because freezing condition
+	 * doesn't come into effect until we release the pm_mutex lock,
+	 * since the freezer always works with pm_mutex held.
+	 *
+	 * More importantly, in the case of hibernation,
+	 * unlock_system_sleep() gets called in snapshot_read() and
+	 * snapshot_write() when the freezing condition is still in effect.
+	 * Which means, if we use try_to_freeze() here, it would make them
+	 * enter the refrigerator, thus causing hibernation to lockup.
+	 */
+	current->flags &= ~PF_FREEZER_SKIP;
 	mutex_unlock(&pm_mutex);
-	freezer_count();
 }
 
 #else /* !CONFIG_PM_SLEEP */
author Applied
#20 lore raw
Quoted 122 lines · Srivatsa S. Bhat
Quoted 85 lines · Rafael J. Wysocki
Quoted 84 lines · Srivatsa S. Bhat
Quoted 46 lines · Srivatsa S. Bhat
Quoted 11 lines · Tejun Heo

Hello,

Quoted 2 lines · Srivatsa S. Bhat

Somehow I don't think its a hack, based on my perception as described above. But feel free to prove me wrong :-)

Thanks for the explanation. Yeah, I agree and it's much simpler this way, which is nice. So, in short, because freezing state can't change across lock_system_sleep(), there's no reason to check for freezing state on unlock and this nicely resolves the freezer problem together.

Absolutely!

Quoted 3 lines

The only thing to be careful is, then, we need to set and clear SKIP inside pm_mutex.

Not exactly. We need to set SKIP before grabbing pm_mutex and clear it inside pm_mutex. The reason is that we decided to set SKIP in the first place just to avoid the freezer from declaring failure when we are blocked on pm_mutex. If we move it to *after* mutex_lock(&pm_mutex), that original intention itself is not satisfied, and we will hit freezing failures - IOW making the set and clear exercise useless!

So, something like this should work perfectly:

lock_system_sleep()

{
	freezer_do_not_count();
	mutex_lock(&pm_mutex);
	current->flags &= ~PF_FREEZER_SKIP;
}

But in the interest of making the code look a bit symmetric, we can do:

lock_system_sleep()

{
	freezer_do_not_count();
	mutex_lock(&pm_mutex);
}
Quoted 6 lines

unlock_system_sleep()

{
	current->flags &= ~PF_FREEZER_SKIP;
	mutex_unlock(&pm_mutex);
}

So how about this patch, with the comment updated?
(and the changelog updated as well to reflect the movement of code)

---
From: Srivatsa S. Bhat <[email protected]>
Subject: [PATCH] PM / Hibernate: Rewrite unlock_system_sleep() to fix s2disk regression

Commit 33e638b, "PM / Sleep: Use the freezer_count() functions in [un]lock_system_sleep() APIs" introduced an undesirable change in the behaviour of unlock_system_sleep() since freezer_count() internally calls try_to_freeze() - which we don't need in unlock_system_sleep().

And commit bcda53f, "PM / Sleep: Replace mutex_[un]lock(&pm_mutex) with [un]lock_system_sleep()" made these APIs wide-spread. This caused a regression in suspend-to-disk where snapshot_read() and snapshot_write() were getting frozen due to the try_to_freeze embedded in unlock_system_sleep(), since these functions were invoked when the freezing condition was still in effect.

Fix this by rewriting unlock_system_sleep() by open-coding freezer_count() and dropping the try_to_freeze() part. Not only will this fix the regression but this will also ensure that the API only does what it is intended to do, and nothing more, under the hood.

While at it, make the code more correct and robust by ensuring that the PF_FREEZER_SKIP flag gets cleared with pm_mutex held, to avoid a race with the freezer.

Quoted 7 lines
Quoted 2 lines

So, I'd prefer lock_system_sleep() to be open-coded too, to avoid situations in which someone changes freezer_do_not_count() and freezer_count() leaving unmodified unlock_system_sleep() behind.

Hehe, actually, I had left lock_system_sleep() as it is, intentionally, due to a far-fetched idea to keep the code sane: I felt that people who go through this code would naturally feel tempted to replace any open-coded stuff (if possible) with the appropriate function call. And my rationale was that when they see that lock_system_sleep() has a function call but unlock_system_sleep() has been open-coded, they would immediately know that this was not yet another case of "oh I wasn't aware of this function, hence I open-coded it", but it was rather "I was very much aware of the available functions, but I chose to open-code it intentionally - so touch it only if you really know what you are doing" :-)

And I guess, now that job of guarding the code from "obvious code cleanups" like that would be done by the comment I added as per Tejun's suggestion. And moreover, your point to open-code in lock_system_sleep() sounds more compelling than my far-fetched idea :-) So okay, I will open-code it..

Quoted 1 line

Also, have you actually tested this?

Oh I almost forgot to mention: I have not specifically tested this with the userspace s2disk utility (part of uswsusp/suspend-utils package) as such. I have only tested it in general (by invoking hibernate by writing to /sys/power/disk, and testing different levels of pm_test framework etc) to ensure that this patch doesn't break anything else.
So, any help with testing the userspace utilities would be great!

Quoted 2 lines

So Rafael, could you please check if the following patch solves the regression for you? Thanks a lot!

Yes, it does, thanks!

Applied to linux-pm/linux-next.

Thanks,
Rafael

Quoted 81 lines

---
From: Srivatsa S. Bhat <[email protected]>
Subject: [PATCH] PM / Hibernate: Rewrite unlock_system_sleep() to fix s2disk regression

Commit 33e638b, "PM / Sleep: Use the freezer_count() functions in [un]lock_system_sleep() APIs" introduced an undesirable change in the behaviour of unlock_system_sleep() since freezer_count() internally calls try_to_freeze() - which we don't need in unlock_system_sleep().

And commit bcda53f, "PM / Sleep: Replace mutex_[un]lock(&pm_mutex) with [un]lock_system_sleep()" made these APIs wide-spread. This caused a regression in suspend-to-disk where snapshot_read() and snapshot_write() were getting frozen due to the try_to_freeze embedded in unlock_system_sleep(), since these functions were invoked when the freezing condition was still in effect.

Fix this by rewriting unlock_system_sleep() by open-coding freezer_count() and dropping the try_to_freeze() part. Not only will this fix the regression but this will also ensure that the API only does what it is intended to do, and nothing more, under the hood.

While at it, make the code more correct and robust by ensuring that the PF_FREEZER_SKIP flag gets cleared with pm_mutex held, to avoid a race with the freezer.

Also, to be on the safer side, open-code freezer_do_not_count() as well (inside lock_system_sleep()), to ensure that any unrelated modification to freezer[_do_not]_count() does not break things again!

---

 include/linux/suspend.h |   19 +++++++++++++++++--
 1 files changed, 17 insertions(+), 2 deletions(-)
include/linux/suspend.h +17 -3
index 95040cc..91784a4 100644
@@ -357,14 +357,29 @@ extern bool pm_save_wakeup_count(unsigned int count);
 
 static inline void lock_system_sleep(void)
 {
-	freezer_do_not_count();
+	current->flags |= PF_FREEZER_SKIP;
 	mutex_lock(&pm_mutex);
 }
 
 static inline void unlock_system_sleep(void)
 {
+	/*
+	 * Don't use freezer_count() because we don't want the call to
+	 * try_to_freeze() here.
+	 *
+	 * Reason:
+	 * Fundamentally, we just don't need it, because freezing condition
+	 * doesn't come into effect until we release the pm_mutex lock,
+	 * since the freezer always works with pm_mutex held.
+	 *
+	 * More importantly, in the case of hibernation,
+	 * unlock_system_sleep() gets called in snapshot_read() and
+	 * snapshot_write() when the freezing condition is still in effect.
+	 * Which means, if we use try_to_freeze() here, it would make them
+	 * enter the refrigerator, thus causing hibernation to lockup.
+	 */
+	current->flags &= ~PF_FREEZER_SKIP;
 	mutex_unlock(&pm_mutex);
-	freezer_count();
 }
 
 #else /* !CONFIG_PM_SLEEP */
 
 
--

To unsubscribe from this list: send the line "unsubscribe linux-pm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html