Skip to contentSkip to conversation

Questions on locking, snd-usb-caiaq

PATCH In review Mark Hills 3 participants 13 messages Sep 20, 2009 – Oct 18, 2009

Subsystems (2)
lore
Share & tools
lore

Mark Hills

author
#0 lore raw

I fixed some race issues in snd-usb-caiaq, but have some questions on the interface between the driver and ALSA.

First fix is a lock in pointer(), which caused snd_pcm_update_hw_ptr_pos() to report an out of range buffer position (even though it was sucessfully corrected to zero).

Second is a lock as part of trigger(). On testing, this appears to fix an issue where white noise could sometimes be transferred by the driver.

According to the documentation, trigger() and pointer() are called with local interrupts disabled. This means they should really be non-irqsave locks?

(http://www.alsa-project.org/~tiwai/writing-an-alsa-driver/ch05s08.html)

Even for callbacks documented as atomic, if read_completed() and write_completed() can be called at any time interrupts are enabled (eg. on another CPU) it seems that additional locking my be required. Candidates are:

snd_usb_caiaq_substream_open
snd_usb_caiaq_substream_close
snd_usb_caiaq_pcm_hw_free
snd_usb_caiaq_pcm_prepare

Are there any guarantees on these functions which mean the lock is not required?

To lock in prepare() would (I think) require demoting a spin_lock_irqsave() to a spin_lock() to send initialisation commands. If so, what is the best design for this?

Thanks,

Signature · 1 line
Mark
sound/usb/caiaq/audio.c +15 -3
index 121af06..c659f81 100644
@@ -62,10 +62,15 @@ static void
  activate_substream(struct snd_usb_caiaqdev *dev,
  	           struct snd_pcm_substream *sub)
  {
+	unsigned long flags;
+	spin_lock_irqsave(&dev->spinlock, flags);
+
  	if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
  		dev->sub_playback[sub->number] = sub;
  	else
  		dev->sub_capture[sub->number] = sub;
+
+	spin_unlock_irqrestore(&dev->spinlock, flags);
  }
 
  static void
@@ -269,16 +274,23 @@ snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
  {
  	int index = sub->number;
  	struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
+	unsigned long flags;
+	snd_pcm_uframes_t ptr;
+
+	spin_lock_irqsave(&dev->spinlock, flags);
 
  	if (dev->input_panic || dev->output_panic)
-		return SNDRV_PCM_POS_XRUN;
+		ptr = SNDRV_PCM_POS_XRUN;
 
  	if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
-		return bytes_to_frames(sub->runtime,
+		ptr = bytes_to_frames(sub->runtime,
  					dev->audio_out_buf_pos[index]);
  	else
-		return bytes_to_frames(sub->runtime,
+		ptr = bytes_to_frames(sub->runtime,
  					dev->audio_in_buf_pos[index]);
+
+	spin_unlock_irqrestore(&dev->spinlock, flags);
+	return ptr;
  }
 
  /* operators for both playback and capture */
#1 lore raw

At Sun, 20 Sep 2009 13:07:05 +0100 (BST),

Quoted 10 lines · Mark Hills

I fixed some race issues in snd-usb-caiaq, but have some questions on the interface between the driver and ALSA.

First fix is a lock in pointer(), which caused snd_pcm_update_hw_ptr_pos() to report an out of range buffer position (even though it was sucessfully corrected to zero).

Second is a lock as part of trigger(). On testing, this appears to fix an issue where white noise could sometimes be transferred by the driver.

Both look fine to me.

Quoted 3 lines

According to the documentation, trigger() and pointer() are called with local interrupts disabled. This means they should really be non-irqsave locks?

They can be non-irqsave one, but not necessarily to be.

Quoted 7 lines

(http://www.alsa-project.org/~tiwai/writing-an-alsa-driver/ch05s08.html)

Even for callbacks documented as atomic, if read_completed() and write_completed() can be called at any time interrupts are enabled (eg. on another CPU) it seems that additional locking my be required. Candidates are:

Quoted 7 lines

snd_usb_caiaq_substream_open
snd_usb_caiaq_substream_close
snd_usb_caiaq_pcm_hw_free
snd_usb_caiaq_pcm_prepare

Are there any guarantees on these functions which mean the lock is not required?

They are all protected via PCM substream lock, thus they aren't racy.

Quoted 3 lines

To lock in prepare() would (I think) require demoting a spin_lock_irqsave() to a spin_lock() to send initialisation commands. If so, what is the best design for this?

The best is to sync the deactivation. Right now, activation / deactivation are done in asynchronous way in caiaq driver. At the beginning of prepare callback, you should be sure that all URBs are killed, then proceed the rest preparation job.

Takashi

#2 lore raw
Quoted 6 lines · Takashi Iwai

At Sun, 20 Sep 2009 13:07:05 +0100 (BST),

Quoted 4 lines · Mark Hills

I fixed some race issues in snd-usb-caiaq, but have some questions on the interface between the driver and ALSA.

Quoted 8 lines
Quoted 6 lines

First fix is a lock in pointer(), which caused snd_pcm_update_hw_ptr_pos() to report an out of range buffer position (even though it was sucessfully corrected to zero).

Second is a lock as part of trigger(). On testing, this appears to fix an issue where white noise could sometimes be transferred by the driver.

Both look fine to me.

To me as well, but I'm currently unable to test them. But I trust Mark - he's been tracking that issue for a long time already and was the only one so far observing this bug. If those patches fix it for him, they should definitely go in. So assume my Acked-by: on them :)

Thanks,
Daniel

author
#3 lore raw
Quoted 14 lines · Daniel Mack
Quoted 13 lines · Takashi Iwai

At Sun, 20 Sep 2009 13:07:05 +0100 (BST),

Quoted 10 lines · Mark Hills

I fixed some race issues in snd-usb-caiaq, but have some questions on the interface between the driver and ALSA.

First fix is a lock in pointer(), which caused snd_pcm_update_hw_ptr_pos() to report an out of range buffer position (even though it was sucessfully corrected to zero).

Second is a lock as part of trigger(). On testing, this appears to fix an issue where white noise could sometimes be transferred by the driver.

Quoted 6 lines
Quoted 1 line

Both look fine to me.

To me as well, but I'm currently unable to test them. But I trust Mark - he's been tracking that issue for a long time already and was the only one so far observing this bug. If those patches fix it for him, they should definitely go in. So assume my Acked-by: on them :)

Thanks, once I've done a little more widespread testing for visible bugs, I'll follow up with proper patches. I also made the change to spin_lock() where appropriate.

Signature · 1 line
Mark
author
#4 lore raw

PATCH 1/4 snd-usb-caiaq: Do not disable local interrupts

The driver documentation states that local interrupts are already disabled when trigger() is called, so only a spinlock is required.

---
 sound/usb/caiaq/audio.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
sound/usb/caiaq/audio.c +2 -3
index 121af06..ce193a5 100644
@@ -72,15 +72,14 @@ static void
 deactivate_substream(struct snd_usb_caiaqdev *dev,
 		     struct snd_pcm_substream *sub)
 {
-	unsigned long flags;
-	spin_lock_irqsave(&dev->spinlock, flags);
+	spin_lock(&dev->spinlock);
 
 	if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
 		dev->sub_playback[sub->number] = NULL;
 	else
 		dev->sub_capture[sub->number] = NULL;
 
-	spin_unlock_irqrestore(&dev->spinlock, flags);
+	spin_unlock(&dev->spinlock);
 }
 
 static int
Signature · 1 line
1.6.4.4
author
#5 lore raw

PATCH 2/4 snd-usb-caiaq: Missing lock around use of buffer positions

Fix a race which causes snd_pcm_update_hw_ptr_pos() to report a bug.

---
 sound/usb/caiaq/audio.c |   12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)
sound/usb/caiaq/audio.c +9 -3
index ce193a5..9856c00 100644
@@ -268,16 +268,22 @@ snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
 {
 	int index = sub->number;
 	struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
+	snd_pcm_uframes_t ptr;
+
+	spin_lock(&dev->spinlock);
 
 	if (dev->input_panic || dev->output_panic)
-		return SNDRV_PCM_POS_XRUN;
+		ptr = SNDRV_PCM_POS_XRUN;
 
 	if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
-		return bytes_to_frames(sub->runtime,
+		ptr = bytes_to_frames(sub->runtime,
 					dev->audio_out_buf_pos[index]);
 	else
-		return bytes_to_frames(sub->runtime,
+		ptr = bytes_to_frames(sub->runtime,
 					dev->audio_in_buf_pos[index]);
+
+	spin_unlock(&dev->spinlock);
+	return ptr;
 }
 
 /* operators for both playback and capture */
Signature · 1 line
1.6.4.4
author
#6 lore raw

PATCH 3/4 snd-usb-caiaq: Lock on stream start/unpause

Fix a bug which can result in white noise from the driver after stream start or unpause.

---
 sound/usb/caiaq/audio.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
sound/usb/caiaq/audio.c +4 -0
index 9856c00..de9cf1e 100644
@@ -62,10 +62,14 @@ static void
 activate_substream(struct snd_usb_caiaqdev *dev,
 	           struct snd_pcm_substream *sub)
 {
+	spin_lock(&dev->spinlock);
+
 	if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
 		dev->sub_playback[sub->number] = sub;
 	else
 		dev->sub_capture[sub->number] = sub;
+
+	spin_unlock(&dev->spinlock);
 }
 
 static void
Signature · 1 line
1.6.4.4
author
#7 lore raw

PATCH 4/4 snd-usb-caiaq: Bump version number to 1.3.20

---
 sound/usb/caiaq/device.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
sound/usb/caiaq/device.c +1 -1
index 83e6c13..a3f02dd 100644
@@ -35,7 +35,7 @@
 #include "input.h"
 
 MODULE_AUTHOR("Daniel Mack <[email protected]>");
-MODULE_DESCRIPTION("caiaq USB audio, version 1.3.19");
+MODULE_DESCRIPTION("caiaq USB audio, version 1.3.20");
 MODULE_LICENSE("GPL");
 MODULE_SUPPORTED_DEVICE("{{Native Instruments, RigKontrol2},"
 			 "{Native Instruments, RigKontrol3},"
Signature · 1 line
1.6.4.4
#8 lore raw

Re: [PATCH 1/4] snd-usb-caiaq: Do not disable local interrupts

In reply to Mark Hills
Quoted 2 lines · Mark Hills

The driver documentation states that local interrupts are already disabled when trigger() is called, so only a spinlock is required.

Hmm. I assume you tested them well. But I remember to have had quite some trouble at this point on SMP machines and ended up with that irqsave code. However, if you're sure that fixes a bug or is not necessary, let's take the patches and I'll test them once I'm back home around mid Oct.

Btw - your Signed-off-by: is missing on all of them :)

Thanks,
Daniel

Quoted 29 lines
---
 sound/usb/caiaq/audio.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
sound/usb/caiaq/audio.c +2 -3
index 121af06..ce193a5 100644
@@ -72,15 +72,14 @@ static void
 deactivate_substream(struct snd_usb_caiaqdev *dev,
 		     struct snd_pcm_substream *sub)
 {
-	unsigned long flags;
-	spin_lock_irqsave(&dev->spinlock, flags);
+	spin_lock(&dev->spinlock);
 
 	if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
 		dev->sub_playback[sub->number] = NULL;
 	else
 		dev->sub_capture[sub->number] = NULL;
 
-	spin_unlock_irqrestore(&dev->spinlock, flags);
+	spin_unlock(&dev->spinlock);
 }
 
 static int
Signature · 1 line
1.6.4.4
author
#9 lore raw

Re: [PATCH 1/4] snd-usb-caiaq: Do not disable local interrupts

Quoted 9 lines · Daniel Mack
Quoted 2 lines · Mark Hills

The driver documentation states that local interrupts are already disabled when trigger() is called, so only a spinlock is required.

Hmm. I assume you tested them well. But I remember to have had quite some trouble at this point on SMP machines and ended up with that irqsave code. However, if you're sure that fixes a bug or is not necessary, let's take the patches and I'll test them once I'm back home around mid Oct.

I did check both non and SMP machines, and it corresponds to the docs. I also removed the patches to verify I could reproduce the original problems.

As I missed off the the Signed-off-by, I'll not rush to resubmit them; let's wait until you have also tested them.

Quoted 36 lines

Btw - your Signed-off-by: is missing on all of them :)

Thanks,
Daniel

Quoted 29 lines
---
 sound/usb/caiaq/audio.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
sound/usb/caiaq/audio.c +2 -4
index 121af06..ce193a5 100644
@@ -72,15 +72,14 @@ static void
 deactivate_substream(struct snd_usb_caiaqdev *dev,
 		     struct snd_pcm_substream *sub)
 {
-	unsigned long flags;
-	spin_lock_irqsave(&dev->spinlock, flags);
+	spin_lock(&dev->spinlock);
 
 	if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
 		dev->sub_playback[sub->number] = NULL;
 	else
 		dev->sub_capture[sub->number] = NULL;
 
-	spin_unlock_irqrestore(&dev->spinlock, flags);
+	spin_unlock(&dev->spinlock);
 }
 
 static int
--

1.6.4.4

Signature · 1 line
Mark
#10 lore raw

Re: [PATCH 1/4] snd-usb-caiaq: Do not disable local interrupts

At Sat, 26 Sep 2009 17:06:23 +0100 (BST),

Quoted 13 lines · Mark Hills
Quoted 9 lines · Daniel Mack
Quoted 2 lines · Mark Hills

The driver documentation states that local interrupts are already disabled when trigger() is called, so only a spinlock is required.

Hmm. I assume you tested them well. But I remember to have had quite some trouble at this point on SMP machines and ended up with that irqsave code. However, if you're sure that fixes a bug or is not necessary, let's take the patches and I'll test them once I'm back home around mid Oct.

Quoted 3 lines

I did check both non and SMP machines, and it corresponds to the docs. I also removed the patches to verify I could reproduce the original problems.

The irqsave is needed for deactivate_substream() because it's called also in your hw_free callback, which is non-atomic.
In the real case, it's hard to hit this codepath, though.

Meanwhile, activate_substream() is called only from the trigger callback, and thus it's safe to use without irqsave version.

thanks,

Takashi

#11 lore raw

Re: [PATCH 1/4] snd-usb-caiaq: Do not disable local interrupts

Quoted 15 lines · Takashi Iwai

At Sat, 26 Sep 2009 17:06:23 +0100 (BST),

Quoted 13 lines · Mark Hills
Quoted 9 lines · Daniel Mack
Quoted 2 lines · Mark Hills

The driver documentation states that local interrupts are already disabled when trigger() is called, so only a spinlock is required.

Hmm. I assume you tested them well. But I remember to have had quite some trouble at this point on SMP machines and ended up with that irqsave code. However, if you're sure that fixes a bug or is not necessary, let's take the patches and I'll test them once I'm back home around mid Oct.

Quoted 7 lines
Quoted 3 lines

I did check both non and SMP machines, and it corresponds to the docs. I also removed the patches to verify I could reproduce the original problems.

The irqsave is needed for deactivate_substream() because it's called also in your hw_free callback, which is non-atomic.
In the real case, it's hard to hit this codepath, though.

Well, the nature of race conditions is that they are hard to hit, right? ;)

However, I tested the patches now, and couldn't reproduce anything like the things I remember I had.

So from my side, they can go in then. Sorry for the long delay on this.

Thanks,
Daniel

author
#12 lore raw

Re: [PATCH 1/4] snd-usb-caiaq: Do not disable local interrupts

Quoted 24 lines · Daniel Mack
Quoted 15 lines · Takashi Iwai

At Sat, 26 Sep 2009 17:06:23 +0100 (BST),

Quoted 13 lines · Mark Hills
Quoted 9 lines · Daniel Mack
Quoted 2 lines · Mark Hills

The driver documentation states that local interrupts are already disabled when trigger() is called, so only a spinlock is required.

Hmm. I assume you tested them well. But I remember to have had quite some trouble at this point on SMP machines and ended up with that irqsave code. However, if you're sure that fixes a bug or is not necessary, let's take the patches and I'll test them once I'm back home around mid Oct.

Quoted 7 lines
Quoted 3 lines

I did check both non and SMP machines, and it corresponds to the docs. I also removed the patches to verify I could reproduce the original problems.

The irqsave is needed for deactivate_substream() because it's called also in your hw_free callback, which is non-atomic.
In the real case, it's hard to hit this codepath, though.

Quoted 6 lines

Well, the nature of race conditions is that they are hard to hit, right? ;)

However, I tested the patches now, and couldn't reproduce anything like the things I remember I had.

So from my side, they can go in then. Sorry for the long delay on this.

No, Takashi is correct -- deactivate_substream() is also used in hw_free. It means that "[PATCH 1/4] Do not disable local interrupts" should not be applied. It was meant to be a cleanup only but introduces a new problem.

The other patches (2-4) are good and should be applied. I'll resubmit with my Signed-off-by, but this will be in a week or so.

Signature · 1 line
Mark