apm-emulation: Fix mutex race condition
Subsystems (2)
PATCH 1/1 apm-emulation: Fix mutex race condition
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().
---
drivers/char/apm-emulation.c +2 -1
@@ -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);
Quoted 14 lines · Paul Parsons
A process suspending the system (e.g. apm -s) locks
apm_mutexat the start ofapm_ioctl()and keeps it locked while callingpm_suspend(). A second process trying to ACK the suspend (e.g. apmd) may then block inapm_ioctl()trying to lock the same mutex. The first process tries to freeze processes, but the ACK process cannot be frozen becausemutex_lock()is uninterruptible. Consequentlypm_suspend()will ultimately fail intry_to_freeze_tasks(). This patch allows the ACK process to be frozen if it is blocked inapm_ioctl().Signed-off-by Paul Parsons <[email protected]>---
drivers/char/apm-emulation.c apm_ioctl(struct file *filp, u_int cmd, · line 279
Quoted 6 lines
drivers/char/apm-emulation.c +2
-1@@ -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?
drivers/char/apm-emulation.c apm_ioctl(struct file *filp, u_int cmd, · line 282
Quoted 3 lines
drivers/char/apm-emulation.c +0
-0switch (cmd) {case APM_IOC_SUSPEND :mutex_lock (&state_lock);
Jiri, are you going to maintain apm-emulation.c too?
Thanks,
Rafael
Hi Rafael,
Quoted 4 lines · Rafael J. Wysocki
Hmm. I'd rather use
mutex_trylock()and calltry_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
Quoted 9 lines · Paul Parsons
Hi Rafael,
Quoted 4 lines · Rafael J. Wysocki
Hmm. I'd rather use
mutex_trylock()and calltry_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
Quoted 11 lines · Rafael J. Wysocki
Quoted 8 lines
Quoted 4 lines · Rafael J. Wysocki
Hmm. I'd rather use
mutex_trylock()and calltry_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.
Quoted 1 line
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,
Signature · 2 lines
Jiri Kosina SUSE Labs
Quoted 14 lines · Jiri Kosina
Quoted 11 lines · Rafael J. Wysocki
Quoted 8 lines
Quoted 4 lines · Rafael J. Wysocki
Hmm. I'd rather use
mutex_trylock()and calltry_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.
Quoted 8 lines
Right, neither do I.
Quoted 1 line
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.cas 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
Jiri Kosina
AppliedQuoted 6 lines · Rafael J. Wysocki
Quoted 4 lines
I can add
drivers/char/apm-emulation.cas 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.
Signature · 2 lines
Jiri Kosina SUSE Labs