Skip to contentSkip to conversation

apm-emulation: Fix mutex race condition

PATCH Applied Paul Parsons 3 participants 7 messages May 3, 2011 – May 4, 2011

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

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);
#1 lore raw
Quoted 14 lines · Paul Parsons

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 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 -0
 	switch (cmd) {
 	case APM_IOC_SUSPEND:
 		mutex_lock(&state_lock);

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

Thanks,
Rafael

#2 lore raw

Hi Rafael,

Quoted 4 lines · Rafael J. Wysocki

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

#3 lore raw
Quoted 9 lines · Paul Parsons

Hi Rafael,

Quoted 4 lines · Rafael J. Wysocki

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

#4 lore raw
Quoted 11 lines · Rafael J. Wysocki
Quoted 8 lines
Quoted 4 lines · Rafael J. Wysocki

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.

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
#5 lore raw
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 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.

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.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

Applied
#6 lore raw
Quoted 6 lines · Rafael J. Wysocki
Quoted 4 lines

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.

Signature · 2 lines
Jiri Kosina
SUSE Labs