public inbox for linux-pm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] apm-emulation: Fix mutex race condition
@ 2011-05-03  0:16 Paul Parsons
  2011-05-03 18:23 ` Rafael J. Wysocki
  0 siblings, 1 reply; 7+ messages in thread
From: Paul Parsons @ 2011-05-03  0:16 UTC (permalink / raw)
  To: linux-pm

A process suspending the system (e.g. apm -s) locks apm_mutex at the start of apm_ioctl() and keeps it locked while calling pm_suspend(). A second process trying to ACK the suspend (e.g. apmd) may then block in apm_ioctl() trying to lock the same mutex. The first process tries to freeze processes, but the ACK process cannot be frozen because mutex_lock() is uninterruptible. Consequently pm_suspend() will ultimately fail in try_to_freeze_tasks(). This patch allows the ACK process to be frozen if it is blocked in apm_ioctl().

Signed-off-by: Paul Parsons <lost.distance@yahoo.com>
---
--- clean-2.6.39-rc5/drivers/char/apm-emulation.c	2011-03-15 01:20:32.000000000 +0000
+++ linux-2.6.39-rc5/drivers/char/apm-emulation.c	2011-05-03 00:34:05.467660807 +0100
@@ -275,7 +275,8 @@ apm_ioctl(struct file *filp, u_int cmd, 
 	if (!as->suser || !as->writer)
 		return -EPERM;
 
-	mutex_lock(&apm_mutex);
+	while (mutex_lock_interruptible(&apm_mutex) < 0)
+		try_to_freeze();
 	switch (cmd) {
 	case APM_IOC_SUSPEND:
 		mutex_lock(&state_lock);

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] apm-emulation: Fix mutex race condition
  2011-05-03  0:16 [PATCH] apm-emulation: Fix mutex race condition Paul Parsons
@ 2011-05-03 18:23 ` Rafael J. Wysocki
  2011-05-03 19:19   ` Paul Parsons
  0 siblings, 1 reply; 7+ messages in thread
From: Rafael J. Wysocki @ 2011-05-03 18:23 UTC (permalink / raw)
  To: Paul Parsons; +Cc: Jiri Kosina, linux-pm

On Tuesday, May 03, 2011, Paul Parsons wrote:
> A process suspending the system (e.g. apm -s) locks apm_mutex at the
> start of apm_ioctl() and keeps it locked while calling pm_suspend().
> A second process trying to ACK the suspend (e.g. apmd) may then block
> in apm_ioctl() trying to lock the same mutex. The first process tries
> to freeze processes, but the ACK process cannot be frozen because
> mutex_lock() is uninterruptible. Consequently pm_suspend() will
> ultimately fail in try_to_freeze_tasks(). This patch allows the ACK
> process to be frozen if it is blocked in apm_ioctl().
> 
> Signed-off-by: Paul Parsons <lost.distance@yahoo.com>
> ---
> --- clean-2.6.39-rc5/drivers/char/apm-emulation.c	2011-03-15 01:20:32.000000000 +0000
> +++ linux-2.6.39-rc5/drivers/char/apm-emulation.c	2011-05-03 00:34:05.467660807 +0100
> @@ -275,7 +275,8 @@ apm_ioctl(struct file *filp, u_int cmd, 
>  	if (!as->suser || !as->writer)
>  		return -EPERM;
>  
> -	mutex_lock(&apm_mutex);
> +	while (mutex_lock_interruptible(&apm_mutex) < 0)
> +		try_to_freeze();

Hmm.  I'd rather use mutex_trylock() and call try_to_freeze() if
that fails.

Is there anything wrong with this approach?

>  	switch (cmd) {
>  	case APM_IOC_SUSPEND:
>  		mutex_lock(&state_lock);

Jiri, are you going to maintain apm-emulation.c too?

Thanks,
Rafael

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] apm-emulation: Fix mutex race condition
  2011-05-03 18:23 ` Rafael J. Wysocki
@ 2011-05-03 19:19   ` Paul Parsons
  2011-05-03 20:33     ` Rafael J. Wysocki
  0 siblings, 1 reply; 7+ messages in thread
From: Paul Parsons @ 2011-05-03 19:19 UTC (permalink / raw)
  To: Rafael J. Wysocki; +Cc: Jiri Kosina, linux-pm

Hi Rafael,

--- On Tue, 3/5/11, Rafael J. Wysocki <rjw@sisk.pl> wrote:
> Hmm.  I'd rather use mutex_trylock() and call try_to_freeze() if
> that fails.
> 
> Is there anything wrong with this approach?

Well, mutex_trylock() doesn't wait if it cannot acquire the mutex, so won't the calling process enter a busy loop if it fails?

Regards,
Paul

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] apm-emulation: Fix mutex race condition
  2011-05-03 19:19   ` Paul Parsons
@ 2011-05-03 20:33     ` Rafael J. Wysocki
  2011-05-04 11:44       ` Jiri Kosina
  0 siblings, 1 reply; 7+ messages in thread
From: Rafael J. Wysocki @ 2011-05-03 20:33 UTC (permalink / raw)
  To: Paul Parsons; +Cc: Jiri Kosina, linux-pm

On Tuesday, May 03, 2011, Paul Parsons wrote:
> Hi Rafael,
> 
> --- On Tue, 3/5/11, Rafael J. Wysocki <rjw@sisk.pl> wrote:
> > Hmm.  I'd rather use mutex_trylock() and call try_to_freeze() if
> > that fails.
> > 
> > Is there anything wrong with this approach?
> 
> Well, mutex_trylock() doesn't wait if it cannot acquire the mutex, so won't the calling process enter a busy loop if it fails?

Right.  So I don't see how we could do better that in your patch at the
moment.

I'm not sure if Jiri is going to take your patch.  I will if he doesn't.

Thanks,
Rafael

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] apm-emulation: Fix mutex race condition
  2011-05-03 20:33     ` Rafael J. Wysocki
@ 2011-05-04 11:44       ` Jiri Kosina
  2011-05-04 16:43         ` Rafael J. Wysocki
  0 siblings, 1 reply; 7+ messages in thread
From: Jiri Kosina @ 2011-05-04 11:44 UTC (permalink / raw)
  To: Rafael J. Wysocki; +Cc: linux-pm

On Tue, 3 May 2011, Rafael J. Wysocki wrote:

> > --- On Tue, 3/5/11, Rafael J. Wysocki <rjw@sisk.pl> wrote:
> > > Hmm.  I'd rather use mutex_trylock() and call try_to_freeze() if
> > > that fails.
> > > 
> > > Is there anything wrong with this approach?
> > 
> > Well, mutex_trylock() doesn't wait if it cannot acquire the mutex, so 
> > won't the calling process enter a busy loop if it fails?
> 
> Right.  So I don't see how we could do better that in your patch at the
> moment.

Right, neither do I.

> I'm not sure if Jiri is going to take your patch.  I will if he doesn't.

I can add drivers/char/apm-emulation.c as the one maintained under the 
'APM' roof, and take this, no problem.

Rafael, I take it that I can add your Ack to this patch, right?

Thanks,

-- 
Jiri Kosina
SUSE Labs

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] apm-emulation: Fix mutex race condition
  2011-05-04 11:44       ` Jiri Kosina
@ 2011-05-04 16:43         ` Rafael J. Wysocki
  2011-05-04 18:58           ` Jiri Kosina
  0 siblings, 1 reply; 7+ messages in thread
From: Rafael J. Wysocki @ 2011-05-04 16:43 UTC (permalink / raw)
  To: Jiri Kosina; +Cc: linux-pm

On Wednesday, May 04, 2011, Jiri Kosina wrote:
> On Tue, 3 May 2011, Rafael J. Wysocki wrote:
> 
> > > --- On Tue, 3/5/11, Rafael J. Wysocki <rjw@sisk.pl> wrote:
> > > > Hmm.  I'd rather use mutex_trylock() and call try_to_freeze() if
> > > > that fails.
> > > > 
> > > > Is there anything wrong with this approach?
> > > 
> > > Well, mutex_trylock() doesn't wait if it cannot acquire the mutex, so 
> > > won't the calling process enter a busy loop if it fails?
> > 
> > Right.  So I don't see how we could do better that in your patch at the
> > moment.
> 
> Right, neither do I.
> 
> > I'm not sure if Jiri is going to take your patch.  I will if he doesn't.
> 
> I can add drivers/char/apm-emulation.c as the one maintained under the 
> 'APM' roof, and take this, no problem.
> 
> Rafael, I take it that I can add your Ack to this patch, right?

That's correct.

Thanks,
Rafael

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] apm-emulation: Fix mutex race condition
  2011-05-04 16:43         ` Rafael J. Wysocki
@ 2011-05-04 18:58           ` Jiri Kosina
  0 siblings, 0 replies; 7+ messages in thread
From: Jiri Kosina @ 2011-05-04 18:58 UTC (permalink / raw)
  To: Rafael J. Wysocki; +Cc: linux-pm

On Wed, 4 May 2011, Rafael J. Wysocki wrote:

> > I can add drivers/char/apm-emulation.c as the one maintained under the 
> > 'APM' roof, and take this, no problem.
> > 
> > Rafael, I take it that I can add your Ack to this patch, right?
> 
> That's correct.

Applied, thanks everybody.

-- 
Jiri Kosina
SUSE Labs

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-05-04 18:58 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-05-03  0:16 [PATCH] apm-emulation: Fix mutex race condition Paul Parsons
2011-05-03 18:23 ` Rafael J. Wysocki
2011-05-03 19:19   ` Paul Parsons
2011-05-03 20:33     ` Rafael J. Wysocki
2011-05-04 11:44       ` Jiri Kosina
2011-05-04 16:43         ` Rafael J. Wysocki
2011-05-04 18:58           ` Jiri Kosina

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox