Skip to contentSkip to conversation

PCM OSS fixes

PATCH In review Takashi Iwai 2 participants 15 messages Jan 8, 2018 – Jan 9, 2018 Sound

lore
Share & tools
lore
#0 lore raw

cover PCM OSS fixes

Hi,

this contains two fixes for PCM OSS bugs that have been triggered by syzbot. These are simple and straightforward, and I'm going to queue for 4.15-rc8.

thanks,

Takashi

===

Takashi Iwai (2):
ALSA: pcm: Abort properly at pending signal in OSS read/write loops
ALSA: pcm: Allow aborting mutex lock at OSS read/write loops

 sound/core/oss/pcm_oss.c | 40 +++++++++++++++++++++++++++-------------
 1 file changed, 27 insertions(+), 13 deletions(-)
Signature · 1 line
2.15.1
#1 lore raw

PATCH 1/2 ALSA: pcm: Abort properly at pending signal in OSS read/write loops

The loops for read and write in PCM OSS emulation have no proper check of pending signals, and they keep processing even after user tries to break. This results in a very long delay, often seen as RCU stall when a huge unprocessed bytes remain queued. The bug could be easily triggered by syzkaller.

As a simple workaround, this patch adds the proper check of pending signals and aborts the loop appropriately.

---
 sound/core/oss/pcm_oss.c | 8 ++++++++
 1 file changed, 8 insertions(+)
sound/core/oss/pcm_oss.c +8 -0
index ceaa51f76591..e317964bd2ea 100644
@@ -1381,6 +1381,10 @@ static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const cha
 			    tmp != runtime->oss.period_bytes)
 				break;
 		}
+		if (signal_pending(current)) {
+			tmp = -ERESTARTSYS;
+			goto err;
+		}
 	}
 	mutex_unlock(&runtime->oss.params_lock);
 	return xfer;
@@ -1466,6 +1470,10 @@ static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __use
 			bytes -= tmp;
 			xfer += tmp;
 		}
+		if (signal_pending(current)) {
+			tmp = -ERESTARTSYS;
+			goto err;
+		}
 	}
 	mutex_unlock(&runtime->oss.params_lock);
 	return xfer;
Signature · 1 line
2.15.1
#2 lore raw

PATCH 2/2 ALSA: pcm: Allow aborting mutex lock at OSS read/write loops

In reply to Takashi Iwai

PCM OSS read/write loops keep taking the mutex lock for the whole read/write, and this might take very long when the exceptionally high amount of data is given. Also, since it invokes with mutex_lock(), the concurrent read/write becomes unbreakable.

This patch tries to address these issues by replacing mutex_lock() with mutex_lock_interruptible(), and also splits / re-takes the lock at each read/write period chunk, so that it can switch the context more finely if requested.

---
 sound/core/oss/pcm_oss.c | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)
sound/core/oss/pcm_oss.c +21 -15
index e317964bd2ea..5e334642903e 100644
@@ -1334,8 +1334,11 @@ static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const cha
 
 	if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
 		return tmp;
-	mutex_lock(&runtime->oss.params_lock);
 	while (bytes > 0) {
+		if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
+			tmp = -EINTR;
+			break;
+		}
 		if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
 			tmp = bytes;
 			if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
@@ -1379,18 +1382,18 @@ static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const cha
 			xfer += tmp;
 			if ((substream->f_flags & O_NONBLOCK) != 0 &&
 			    tmp != runtime->oss.period_bytes)
-				break;
+				tmp = -EAGAIN;
 		}
+ err:
+		mutex_unlock(&runtime->oss.params_lock);
+		if (tmp < 0)
+			break;
 		if (signal_pending(current)) {
 			tmp = -ERESTARTSYS;
-			goto err;
+			break;
 		}
+		tmp = 0;
 	}
-	mutex_unlock(&runtime->oss.params_lock);
-	return xfer;
-
- err:
-	mutex_unlock(&runtime->oss.params_lock);
 	return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
 }
 
@@ -1438,8 +1441,11 @@ static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __use
 
 	if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
 		return tmp;
-	mutex_lock(&runtime->oss.params_lock);
 	while (bytes > 0) {
+		if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
+			tmp = -EINTR;
+			break;
+		}
 		if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
 			if (runtime->oss.buffer_used == 0) {
 				tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
@@ -1470,16 +1476,16 @@ static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __use
 			bytes -= tmp;
 			xfer += tmp;
 		}
+ err:
+		mutex_unlock(&runtime->oss.params_lock);
+		if (tmp < 0)
+			break;
 		if (signal_pending(current)) {
 			tmp = -ERESTARTSYS;
-			goto err;
+			break;
 		}
+		tmp = 0;
 	}
-	mutex_unlock(&runtime->oss.params_lock);
-	return xfer;
-
- err:
-	mutex_unlock(&runtime->oss.params_lock);
 	return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
 }
 
Signature · 1 line
2.15.1
#3 lore raw

Re: [PATCH 2/2] ALSA: pcm: Allow aborting mutex lock at OSS read/write loops

sound/core/oss/pcm_oss.c static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __use · line 1446

Quoted 8 lines · Takashi Iwai
sound/core/oss/pcm_oss.c +2 -1
@@ -1438,8 +1441,11 @@ static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __use
 
 	if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
 		return tmp;
-	mutex_lock(&runtime->oss.params_lock);
 	while (bytes > 0) {
+		if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
+			tmp = -EINTR;

For consistency ERESTARTSYS I guess? In the end this is the same as signal_pending().

Quoted 29 lines
sound/core/oss/pcm_oss.c +8 -6
+			break;
+		}
 		if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
 			if (runtime->oss.buffer_used == 0) {
 				tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
@@ -1470,16 +1476,16 @@ static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __use
 			bytes -= tmp;
 			xfer += tmp;
 		}
+ err:
+		mutex_unlock(&runtime->oss.params_lock);
+		if (tmp < 0)
+			break;
 		if (signal_pending(current)) {
 			tmp = -ERESTARTSYS;
-			goto err;
+			break;
 		}
+		tmp = 0;
 	}
-	mutex_unlock(&runtime->oss.params_lock);
-	return xfer;
-
- err:
-	mutex_unlock(&runtime->oss.params_lock);
 	return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
 }
 
#4 lore raw

Re: [PATCH 2/2] ALSA: pcm: Allow aborting mutex lock at OSS read/write loops

Quoted 4 lines · Lars-Peter Clausen
Quoted 2 lines · Takashi Iwai
(quoted diff) +0 -0
@@ -1438,8 +1441,11 @@ static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __use
 
Quoted 9 lines

sound/core/oss/pcm_oss.c static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const cha · line 1339

Quoted 6 lines
sound/core/oss/pcm_oss.c +2 -1
 	if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
 		return tmp;
-	mutex_lock(&runtime->oss.params_lock);
 	while (bytes > 0) {
+		if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
+			tmp = -EINTR;

For consistency ERESTARTSYS I guess? In the end this is the same as signal_pending().

Right, it's a better one.

thanks,

Takashi

#5 lore raw

Re: [PATCH 2/2] ALSA: pcm: Allow aborting mutex lock at OSS read/write loops

Quoted 17 lines · Takashi Iwai
Quoted 4 lines · Lars-Peter Clausen
Quoted 2 lines · Takashi Iwai
(quoted diff) +0 -0
@@ -1438,8 +1441,11 @@ static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __use
 
Quoted 9 lines

sound/core/oss/pcm_oss.c static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const cha · line 1339

Quoted 6 lines
sound/core/oss/pcm_oss.c +2 -1
 	if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
 		return tmp;
-	mutex_lock(&runtime->oss.params_lock);
 	while (bytes > 0) {
+		if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
+			tmp = -EINTR;

For consistency ERESTARTSYS I guess? In the end this is the same as signal_pending().

Quoted 1 line

Right, it's a better one.

BTW, this spotted out another point still using -EINTR in pcm_oss.c. Will fix it as well. Thanks for reminding it.

Takashi

#6 lore raw
In reply to Takashi Iwai
Quoted 5 lines · Takashi Iwai

Hi,

this contains two fixes for PCM OSS bugs that have been triggered by syzbot. These are simple and straightforward, and I'm going to queue for 4.15-rc8.

I tried to reproduce the issue as well with the reproducer generated by syzbot. But what syzbot reported was that the CPU didn't sleep for 140 seconds. But with a long write() we'll go to sleep in between frames. So when I tried the lockup detector never triggered.

Were you able to reproduce the same CPU lockup as syzbot?

- Lars

#7 lore raw
Quoted 8 lines · Lars-Peter Clausen
Quoted 5 lines · Takashi Iwai

Hi,

this contains two fixes for PCM OSS bugs that have been triggered by syzbot. These are simple and straightforward, and I'm going to queue for 4.15-rc8.

Quoted 6 lines

I tried to reproduce the issue as well with the reproducer generated by syzbot. But what syzbot reported was that the CPU didn't sleep for 140 seconds. But with a long write() we'll go to sleep in between frames. So when I tried the lockup detector never triggered.

Were you able to reproduce the same CPU lockup as syzbot?

Yes, I could reproduce RCU stalls with two reproducers ([email protected] and
[email protected])

The patches seem curing them, so far, in combination with other patches in for-linus branch of sound.git tree.

thanks,

Takashi

#8 lore raw
Quoted 15 lines · Takashi Iwai
Quoted 13 lines · Lars-Peter Clausen
Quoted 5 lines · Takashi Iwai

Hi,

this contains two fixes for PCM OSS bugs that have been triggered by syzbot. These are simple and straightforward, and I'm going to queue for 4.15-rc8.

I tried to reproduce the issue as well with the reproducer generated by syzbot. But what syzbot reported was that the CPU didn't sleep for 140 seconds. But with a long write() we'll go to sleep in between frames. So when I tried the lockup detector never triggered.

Quoted 9 lines
Quoted 1 line

Were you able to reproduce the same CPU lockup as syzbot?

Yes, I could reproduce RCU stalls with two reproducers ([email protected] and
[email protected])

The patches seem curing them, so far, in combination with other patches in for-linus branch of sound.git tree.

Hm, interesting. Are you using aloop for the backend or real hardware?

#9 lore raw
Quoted 23 lines · Lars-Peter Clausen
Quoted 21 lines · Takashi Iwai
Quoted 8 lines · Lars-Peter Clausen
Quoted 5 lines · Takashi Iwai

Hi,

this contains two fixes for PCM OSS bugs that have been triggered by syzbot. These are simple and straightforward, and I'm going to queue for 4.15-rc8.

Quoted 6 lines

I tried to reproduce the issue as well with the reproducer generated by syzbot. But what syzbot reported was that the CPU didn't sleep for 140 seconds. But with a long write() we'll go to sleep in between frames. So when I tried the lockup detector never triggered.

Were you able to reproduce the same CPU lockup as syzbot?

Yes, I could reproduce RCU stalls with two reproducers ([email protected] and
[email protected])

Quoted 5 lines
Quoted 3 lines

The patches seem curing them, so far, in combination with other patches in for-linus branch of sound.git tree.

Hm, interesting. Are you using aloop for the backend or real hardware?

The RCU stall happened also with dummy driver. Just feeding a GB of chunk to /dev/audio and aborting the stream concurrently, then it stalls at that point.

Takashi

#10 lore raw
Quoted 29 lines · Takashi Iwai
Quoted 27 lines · Lars-Peter Clausen
Quoted 24 lines · Takashi Iwai
Quoted 8 lines · Lars-Peter Clausen
Quoted 5 lines · Takashi Iwai

Hi,

this contains two fixes for PCM OSS bugs that have been triggered by syzbot. These are simple and straightforward, and I'm going to queue for 4.15-rc8.

Quoted 6 lines

I tried to reproduce the issue as well with the reproducer generated by syzbot. But what syzbot reported was that the CPU didn't sleep for 140 seconds. But with a long write() we'll go to sleep in between frames. So when I tried the lockup detector never triggered.

Were you able to reproduce the same CPU lockup as syzbot?

Yes, I could reproduce RCU stalls with two reproducers ([email protected] and
[email protected])

The patches seem curing them, so far, in combination with other patches in for-linus branch of sound.git tree.

Quoted 5 lines
Quoted 1 line

Hm, interesting. Are you using aloop for the backend or real hardware?

The RCU stall happened also with dummy driver. Just feeding a GB of chunk to /dev/audio and aborting the stream concurrently, then it stalls at that point.

Hm, does that mean that snd_pcm_playback_avail() is never 0 for the dummy driver? Usually we should catch the signal_pending() in wait_for_avail().

This might not be OSS specific and maybe it is possible to trigger it from ALSA as well.

#11 lore raw
Quoted 30 lines · Lars-Peter Clausen
Quoted 29 lines · Takashi Iwai
Quoted 27 lines · Lars-Peter Clausen
Quoted 24 lines · Takashi Iwai
Quoted 8 lines · Lars-Peter Clausen
Quoted 5 lines · Takashi Iwai

Hi,

this contains two fixes for PCM OSS bugs that have been triggered by syzbot. These are simple and straightforward, and I'm going to queue for 4.15-rc8.

Quoted 6 lines

I tried to reproduce the issue as well with the reproducer generated by syzbot. But what syzbot reported was that the CPU didn't sleep for 140 seconds. But with a long write() we'll go to sleep in between frames. So when I tried the lockup detector never triggered.

Were you able to reproduce the same CPU lockup as syzbot?

Yes, I could reproduce RCU stalls with two reproducers ([email protected] and
[email protected])

The patches seem curing them, so far, in combination with other patches in for-linus branch of sound.git tree.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Hm, interesting. Are you using aloop for the backend or real hardware?

The RCU stall happened also with dummy driver. Just feeding a GB of chunk to /dev/audio and aborting the stream concurrently, then it stalls at that point.

Hm, does that mean that snd_pcm_playback_avail() is never 0 for the dummy driver? Usually we should catch the signal_pending() in wait_for_avail().

Even with the dummy driver I'm not able to reproduce it. wait_for_avail() gets called, thread goes to sleep and a Ctrl+C wakes it up with signal_pending() set and it exits the syscall.

#12 lore raw
Quoted 38 lines · Lars-Peter Clausen
Quoted 36 lines · Lars-Peter Clausen
Quoted 34 lines · Takashi Iwai
Quoted 23 lines · Lars-Peter Clausen
Quoted 21 lines · Takashi Iwai
Quoted 8 lines · Lars-Peter Clausen
Quoted 5 lines · Takashi Iwai

Hi,

this contains two fixes for PCM OSS bugs that have been triggered by syzbot. These are simple and straightforward, and I'm going to queue for 4.15-rc8.

Quoted 6 lines

I tried to reproduce the issue as well with the reproducer generated by syzbot. But what syzbot reported was that the CPU didn't sleep for 140 seconds. But with a long write() we'll go to sleep in between frames. So when I tried the lockup detector never triggered.

Were you able to reproduce the same CPU lockup as syzbot?

Yes, I could reproduce RCU stalls with two reproducers ([email protected] and
[email protected])

Quoted 5 lines
Quoted 3 lines

The patches seem curing them, so far, in combination with other patches in for-linus branch of sound.git tree.

Hm, interesting. Are you using aloop for the backend or real hardware?

The RCU stall happened also with dummy driver. Just feeding a GB of chunk to /dev/audio and aborting the stream concurrently, then it stalls at that point.

Quoted 6 lines
Quoted 2 lines

Hm, does that mean that snd_pcm_playback_avail() is never 0 for the dummy driver? Usually we should catch the signal_pending() in wait_for_avail().

Even with the dummy driver I'm not able to reproduce it. wait_for_avail() gets called, thread goes to sleep and a Ctrl+C wakes it up with signal_pending() set and it exits the syscall.

Is the machine too fast? But mine isn't that slow, either.

FWIW, below is the kconfig I'm using. I ran KVM with -smp 2.

Takashi

#13 lore raw
In reply to Lars-Peter Clausen
Quoted 37 lines · Lars-Peter Clausen
Quoted 29 lines · Takashi Iwai
Quoted 27 lines · Lars-Peter Clausen
Quoted 24 lines · Takashi Iwai
Quoted 8 lines · Lars-Peter Clausen
Quoted 5 lines · Takashi Iwai

Hi,

this contains two fixes for PCM OSS bugs that have been triggered by syzbot. These are simple and straightforward, and I'm going to queue for 4.15-rc8.

Quoted 6 lines

I tried to reproduce the issue as well with the reproducer generated by syzbot. But what syzbot reported was that the CPU didn't sleep for 140 seconds. But with a long write() we'll go to sleep in between frames. So when I tried the lockup detector never triggered.

Were you able to reproduce the same CPU lockup as syzbot?

Yes, I could reproduce RCU stalls with two reproducers ([email protected] and
[email protected])

The patches seem curing them, so far, in combination with other patches in for-linus branch of sound.git tree.

Quoted 5 lines
Quoted 1 line

Hm, interesting. Are you using aloop for the backend or real hardware?

The RCU stall happened also with dummy driver. Just feeding a GB of chunk to /dev/audio and aborting the stream concurrently, then it stalls at that point.

Quoted 5 lines

Hm, does that mean that snd_pcm_playback_avail() is never 0 for the dummy driver? Usually we should catch the signal_pending() in wait_for_avail().

This might not be OSS specific and maybe it is possible to trigger it from ALSA as well.

This seems specific to OSS. The problem is that OSS emulation has two nested loops in a mutex protected context. The outer loop is like:

	while (bytes > 0) {
		__snd_pcm_lib_xfer();
		if (error)
			break;
	}
	return xfer > 0 ? xfer : error;

and the inner loop (__snd_pcm_lib_xfer()) is like:

	while (bytes > 0) {
		if (avail() > 0)
			write();
		else if (wait_for_avail() < 0)
			break;
	}
	return xfer > 0 ? xfer : error;

Actually, wait_for_avail() does check signal_pending() and returns -ERESTARTSYS properly. This makes the inner loop aborted.

However, the problem is that OSS emulation sets avail_min=1: implying that, when there is any samples empty in the ring buffer, it can continue. In the case above, when some samples have been processed before wait_for_avail(), it keeps the outer loop spinning again.

Since each iteration takes ca 2ms on my VM, 8000Hz rate gives 1 or 2 samples free at each iteration. Thus at each time snd_pcm_lib_xfer() is invoked, it processes 1 or 2 samples, then returns due to pending signals, and the outer loop continues until the whole given samples are consumed. If the machine is faster or the sample rate is lower, the outer loop may exit, OTOH.

In ALSA-native API, there is no outer loop in the kernel context, and each read/write merely calling a single shot snd_pcm_lib_xfer(). So, when the signal is pending, it aborts properly.

Takashi

#14 lore raw

On 01/09/2018 08:33 AM, Takashi Iwai wrote:
[...]

Quoted 11 lines

However, the problem is that OSS emulation sets avail_min=1: implying that, when there is any samples empty in the ring buffer, it can continue. In the case above, when some samples have been processed before wait_for_avail(), it keeps the outer loop spinning again.

Since each iteration takes ca 2ms on my VM, 8000Hz rate gives 1 or 2 samples free at each iteration. Thus at each time snd_pcm_lib_xfer() is invoked, it processes 1 or 2 samples, then returns due to pending signals, and the outer loop continues until the whole given samples are consumed. If the machine is faster or the sample rate is lower, the outer loop may exit, OTOH.

OK, that makes sense. I didn't think about that case. In my mind I was working with avail_min=period_size.

Thanks for clearing this up.

- Lars