Skip to contentSkip to conversation

azt3328 fatal deadlock fix (very rare, though)

PATCH In review Andreas Mohr 3 participants 9 messages Jun 21, 2006 – Jun 27, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 azt3328 fatal deadlock fix (very rare, though)

Hi all,

while listening to my 315th MP3 file played on this soundcard I had the extreme mispleasure of experiencing a complete lockup of my box right during shutdown of this file's playback, not even Magic SysRq worked (characteristic sign of spinlock conflicts).

I determined that it must have been the combination of not doing the _irqsave() variant in the playback trigger function and then having another IRQ occur (which tried to spinlock again, against the active lock in the playback trigger function). By always doing _irqsave() everywhere we also don't need separate locking in the IRQ handler any more (managed to reduce CPU load, too).

ChangeLog:
- fix spinlocks to avoid entering spinlocking IRQ with lock held in non-IRQ part

Note that at least als4000.c has the same issue in the trigger functions, so this deadlock has the potential of happening there, too.
I have an ALS4000, so I could do further work if needed.

Also, with this change switching from console to X11 will cause playback to click, most likely due to interrupting the app during spinlock held in non-IRQ part, so no IRQ possible during that time to handle buffer updating (I don't think it clicked before, and this is most likely due to weird interrupt fiddling of X11 during a console switch).

Changes extensively tested on 2.6.17-rc6-mm2 (but not nearly enough to have at least the same amount of testing as in the time range before this *very rare* lockup, to be able to determine whether this truly fixed it!!).

Diff against current rsynced version (had some unexpected rejects, but should be fine).

pci/azt3328.c renamed from pci/azt3328.c.orig +18 -18
@@ -899,6 +899,7 @@
 	struct snd_pcm_runtime *runtime = substream->runtime;
 	int result = 0;
 	unsigned int status1;
+	unsigned long flags;
 
 	snd_azf3328_dbgcalls("snd_azf3328_playback_trigger cmd %d\n", cmd);
 
@@ -914,7 +915,7 @@
 			snd_pcm_format_width(runtime->format),
 			runtime->channels);
 
-		spin_lock(&chip->reg_lock);
+		spin_lock_irqsave(&chip->reg_lock, flags);
 		/* stop playback */
 		status1 = snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS);
 		status1 &= ~DMA_RESUME;
@@ -922,14 +923,14 @@
 	    
 		/* FIXME: clear interrupts or what??? */
 		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_IRQTYPE, 0xffff);
-		spin_unlock(&chip->reg_lock);
+		spin_unlock_irqrestore(&chip->reg_lock, flags);
 
 		snd_azf3328_setdmaa(chip, runtime->dma_addr,
 			snd_pcm_lib_period_bytes(substream),
 			snd_pcm_lib_buffer_bytes(substream),
 			0);
 
-		spin_lock(&chip->reg_lock);
+		spin_lock_irqsave(&chip->reg_lock, flags);
 #ifdef WIN9X
 		/* FIXME: enable playback/recording??? */
 		status1 |= DMA_PLAY_SOMETHING1 | DMA_PLAY_SOMETHING2;
@@ -953,7 +954,7 @@
 			DMA_EPILOGUE_SOMETHING |
 			DMA_SOMETHING_ELSE);
 #endif
-		spin_unlock(&chip->reg_lock);
+		spin_unlock_irqrestore(&chip->reg_lock, flags);
 
 		/* now unmute WaveOut */
 		snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 0);
@@ -967,7 +968,7 @@
 		/* mute WaveOut */
 		snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 1);
 
-		spin_lock(&chip->reg_lock);
+		spin_lock_irqsave(&chip->reg_lock, flags);
 		/* stop playback */
 		status1 = snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS);
 
@@ -981,7 +982,7 @@
 
 		status1 &= ~DMA_PLAY_SOMETHING1;
 		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
-		spin_unlock(&chip->reg_lock);
+		spin_unlock_irqrestore(&chip->reg_lock, flags);
 	    
 		/* now unmute WaveOut */
 		snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 0);
@@ -1011,6 +1012,7 @@
 	struct snd_pcm_runtime *runtime = substream->runtime;
 	int result = 0;
 	unsigned int status1;
+	unsigned long flags;
 
 	snd_azf3328_dbgcalls("snd_azf3328_capture_trigger cmd %d\n", cmd);
 
@@ -1024,7 +1026,7 @@
 			snd_pcm_format_width(runtime->format),
 			runtime->channels);
 
-		spin_lock(&chip->reg_lock);
+		spin_lock_irqsave(&chip->reg_lock, flags);
 		/* stop recording */
 		status1 = snd_azf3328_codec_inw(chip, IDX_IO_REC_FLAGS);
 		status1 &= ~DMA_RESUME;
@@ -1032,14 +1034,14 @@
 	    
 		/* FIXME: clear interrupts or what??? */
 		snd_azf3328_codec_outw(chip, IDX_IO_REC_IRQTYPE, 0xffff);
-		spin_unlock(&chip->reg_lock);
+		spin_unlock_irqrestore(&chip->reg_lock, flags);
 
 		snd_azf3328_setdmaa(chip, runtime->dma_addr,
 			snd_pcm_lib_period_bytes(substream),
 			snd_pcm_lib_buffer_bytes(substream),
 			1);
 
-		spin_lock(&chip->reg_lock);
+		spin_lock_irqsave(&chip->reg_lock, flags);
 #ifdef WIN9X
 		/* FIXME: enable playback/recording??? */
 		status1 |= DMA_PLAY_SOMETHING1 | DMA_PLAY_SOMETHING2;
@@ -1063,7 +1065,7 @@
 			DMA_EPILOGUE_SOMETHING |
 			DMA_SOMETHING_ELSE);
 #endif
-		spin_unlock(&chip->reg_lock);
+		spin_unlock_irqrestore(&chip->reg_lock, flags);
 
 		chip->is_recording = 1;
 		snd_azf3328_dbgplay("STARTED CAPTURE\n");
@@ -1071,7 +1073,7 @@
         case SNDRV_PCM_TRIGGER_STOP:
 		snd_azf3328_dbgplay("STOP CAPTURE\n");
 
-		spin_lock(&chip->reg_lock);
+		spin_lock_irqsave(&chip->reg_lock, flags);
 		/* stop recording */
 		status1 = snd_azf3328_codec_inw(chip, IDX_IO_REC_FLAGS);
 
@@ -1083,7 +1085,7 @@
 
 		status1 &= ~DMA_PLAY_SOMETHING1;
 		snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
-		spin_unlock(&chip->reg_lock);
+		spin_unlock_irqrestore(&chip->reg_lock, flags);
 	    
 		chip->is_recording = 0;
 		snd_azf3328_dbgplay("STOPPED CAPTURE\n");
@@ -1151,6 +1153,10 @@
 	u8 status, which;
 	static unsigned long irq_count;
 
+	/* no need to lock I/O accesses here, since we're in IRQ section.
+	 * However this means that all non-IRQ users must lock via _irqsave()
+	 * to prevent our IRQ I/O here while they're doing their I/O. */
+
 	status = snd_azf3328_codec_inb(chip, IDX_IO_IRQSTATUS);
 
         /* fast path out, to ease interrupt sharing */
@@ -1169,18 +1175,14 @@
 		if (chip->timer)
 			snd_timer_interrupt(chip->timer, chip->timer->sticks);
 		/* ACK timer */
-                spin_lock(&chip->reg_lock);
 		snd_azf3328_codec_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x07);
-		spin_unlock(&chip->reg_lock);
 		snd_azf3328_dbgplay("azt3328: timer IRQ\n");
 	}
 	if (status & IRQ_PLAYBACK)
 	{
-		spin_lock(&chip->reg_lock);
 		which = snd_azf3328_codec_inb(chip, IDX_IO_PLAY_IRQTYPE);
 		/* ack all IRQ types immediately */
 		snd_azf3328_codec_outb(chip, IDX_IO_PLAY_IRQTYPE, which);
-               	spin_unlock(&chip->reg_lock);
 
 		if (chip->pcm && chip->playback_substream)
 		{
@@ -1196,11 +1198,9 @@
 	}
 	if (status & IRQ_RECORDING)
 	{
-                spin_lock(&chip->reg_lock);
 		which = snd_azf3328_codec_inb(chip, IDX_IO_REC_IRQTYPE);
 		/* ack all IRQ types immediately */
 		snd_azf3328_codec_outb(chip, IDX_IO_REC_IRQTYPE, which);
-		spin_unlock(&chip->reg_lock);
 
 		if (chip->pcm && chip->capture_substream)
 		{
#1 lore raw

At Wed, 21 Jun 2006 10:32:34 +0200,

Quoted 8 lines · Andreas Mohr

Hi all,

while listening to my 315th MP3 file played on this soundcard I had the extreme mispleasure of experiencing a complete lockup of my box right during shutdown of this file's playback, not even Magic SysRq worked (characteristic sign of spinlock conflicts).

Quoted 5 lines

I determined that it must have been the combination of not doing the _irqsave() variant in the playback trigger function and then having another IRQ occur (which tried to spinlock again, against the active lock in the playback trigger function). By always doing _irqsave() everywhere we also don't need separate locking in the IRQ handler any more (managed to reduce CPU load, too).

I don't understand this. The local irq is already disabled in either snd_pcm_lib_write1() or snd_pcm_lib_read1() that are the sole callers of snd_pcm_start(). Ditto for snd_pcm_stop().

Takashi

#2 lore raw

Hi,

Quoted 16 lines · Takashi Iwai

At Wed, 21 Jun 2006 10:32:34 +0200,

Quoted 8 lines · Andreas Mohr

Hi all,

while listening to my 315th MP3 file played on this soundcard I had the extreme mispleasure of experiencing a complete lockup of my box right during shutdown of this file's playback, not even Magic SysRq worked (characteristic sign of spinlock conflicts).

Quoted 5 lines

I determined that it must have been the combination of not doing the _irqsave() variant in the playback trigger function and then having another IRQ occur (which tried to spinlock again, against the active lock in the playback trigger function). By always doing _irqsave() everywhere we also don't need separate locking in the IRQ handler any more (managed to reduce CPU load, too).

Quoted 3 lines

I don't understand this. The local irq is already disabled in either snd_pcm_lib_write1() or snd_pcm_lib_read1() that are the sole callers of snd_pcm_start(). Ditto for snd_pcm_stop().

Argh, then I'm not sure what's wrong.
Facts:
- my fix is a best guess about the crash cause, not true knowledge
- there's no way to prove that my fix corrected the problem (very rare bug)
- the system locked up *directly* during closing of this MP3 file's playback (this would strongly point towards trigger functions)
- Magic SysRq didn't work -> locking problem. Hmm, or does an IRQ storm also block SysRq processing totally? If so, then that may have been the problem here (maybe there's an interrupt reason which I'm not properly ACKing yet)

Andreas

#3 lore raw
In reply to Takashi Iwai

Hi,

Quoted 16 lines · Takashi Iwai

At Wed, 21 Jun 2006 10:32:34 +0200,

Quoted 8 lines · Andreas Mohr

Hi all,

while listening to my 315th MP3 file played on this soundcard I had the extreme mispleasure of experiencing a complete lockup of my box right during shutdown of this file's playback, not even Magic SysRq worked (characteristic sign of spinlock conflicts).

Quoted 5 lines

I determined that it must have been the combination of not doing the _irqsave() variant in the playback trigger function and then having another IRQ occur (which tried to spinlock again, against the active lock in the playback trigger function). By always doing _irqsave() everywhere we also don't need separate locking in the IRQ handler any more (managed to reduce CPU load, too).

Quoted 3 lines

I don't understand this. The local irq is already disabled in either snd_pcm_lib_write1() or snd_pcm_lib_read1() that are the sole callers of snd_pcm_start(). Ditto for snd_pcm_stop().

Doh, this may have been caused by my semi-broken AU8820 sharing the same IRQ as the azt3328...
Note that I'm not quite sure whether it's the hardware or ALSA AU8820 driver which is broken (the driver is still quite incomplete), but since the seller said that he suspected the AU8820 card to be broken, I now have every reason to believe that this was true.
(my AU8830 in the same machine works 100% correctly, not only "sometimes")

Sorry for the noise! (let's just hope it was noise and not a real driver bug)

Hmm, or is my driver broken on IRQ sharing? Will investigate a bit more...

Andreas

Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

#4 lore raw

At Tue, 27 Jun 2006 13:28:23 +0200,

Quoted 32 lines · Andreas Mohr

Hi,

Quoted 16 lines · Takashi Iwai

At Wed, 21 Jun 2006 10:32:34 +0200,

Quoted 8 lines · Andreas Mohr

Hi all,

while listening to my 315th MP3 file played on this soundcard I had the extreme mispleasure of experiencing a complete lockup of my box right during shutdown of this file's playback, not even Magic SysRq worked (characteristic sign of spinlock conflicts).

Quoted 5 lines

I determined that it must have been the combination of not doing the _irqsave() variant in the playback trigger function and then having another IRQ occur (which tried to spinlock again, against the active lock in the playback trigger function). By always doing _irqsave() everywhere we also don't need separate locking in the IRQ handler any more (managed to reduce CPU load, too).

Quoted 3 lines

I don't understand this. The local irq is already disabled in either snd_pcm_lib_write1() or snd_pcm_lib_read1() that are the sole callers of snd_pcm_start(). Ditto for snd_pcm_stop().

Doh, this may have been caused by my semi-broken AU8820 sharing the same IRQ as the azt3328...
Note that I'm not quite sure whether it's the hardware or ALSA AU8820 driver which is broken (the driver is still quite incomplete), but since the seller said that he suspected the AU8820 card to be broken, I now have every reason to believe that this was true.
(my AU8830 in the same machine works 100% correctly, not only "sometimes")

Quoted 1 line

Sorry for the noise! (let's just hope it was noise and not a real driver bug)

Glad to hear that there is no real bug ;)

Quoted 1 line

Hmm, or is my driver broken on IRQ sharing? Will investigate a bit more...

I don't see problematic codes there at a quick glance.

BTW, do you still keep CONFIG_EXPERIMENTAL for AZT3328 driver? If it works for you well, we can remove that flag since there is no many bug reports.

Takashi

Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

#5 lore raw

Hi,

Quoted 1 line · Takashi Iwai

Glad to hear that there is no real bug ;)

Yeah.

You seem to be a frightening bit too sure about that, though... ;)

Quoted 3 lines
Quoted 1 line

Hmm, or is my driver broken on IRQ sharing? Will investigate a bit more...

I don't see problematic codes there at a quick glance.

OK.

Quoted 3 lines

BTW, do you still keep CONFIG_EXPERIMENTAL for AZT3328 driver? If it works for you well, we can remove that flag since there is no many bug reports.

Been thinking about that before (since the driver is almost as feature-complete as it can get with an entirely undocumented card), but I wanted to run the Wine audio test suite on it directly before removing the flag (since that could easily reveal some issues due to much more varied sound traffic), but I haven't managed to do that yet.

Oh, and needless to say it's a *very good* idea in general to run the Wine test suite on all sorts of ALSA drivers...

Andreas Mohr

Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

#6 lore raw

At Tue, 27 Jun 2006 15:45:34 +0200,

Quoted 10 lines · Andreas Mohr
Quoted 4 lines

BTW, do you still keep CONFIG_EXPERIMENTAL for AZT3328 driver? If it works for you well, we can remove that flag since there is no many bug reports.

Been thinking about that before (since the driver is almost as feature-complete as it can get with an entirely undocumented card), but I wanted to run the Wine audio test suite on it directly before removing the flag (since that could easily reveal some issues due to much more varied sound traffic), but I haven't managed to do that yet.

The reason I asked is because I'm adding a new configure option --with-experimental to toggle on/off CONFIG_EXPERIMENTAL for alsa-driver tree.

Also, from my understanding, CONFIG_EXPERIMENTAL refers to stability of the driver.

Quoted 2 lines

Oh, and needless to say it's a *very good* idea in general to run the Wine test suite on all sorts of ALSA drivers...

That's true. We'd need own sound test suite, too.

Takashi

Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

#7 lore raw

Hi,

Quoted 13 lines · Takashi Iwai

At Tue, 27 Jun 2006 15:45:34 +0200,

Quoted 10 lines · Andreas Mohr
Quoted 4 lines

BTW, do you still keep CONFIG_EXPERIMENTAL for AZT3328 driver? If it works for you well, we can remove that flag since there is no many bug reports.

Been thinking about that before (since the driver is almost as feature-complete as it can get with an entirely undocumented card), but I wanted to run the Wine audio test suite on it directly before removing the flag (since that could easily reveal some issues due to much more varied sound traffic), but I haven't managed to do that yet.

Quoted 3 lines

The reason I asked is because I'm adding a new configure option --with-experimental to toggle on/off CONFIG_EXPERIMENTAL for alsa-driver tree.

Ah. Nice option, but this probably means that all CONFIG_EXPERIMENTAL drivers won't get built by default, right?

In which case I'd have to act in my driver's area, since it certainly absolutely should get built by default.

Quoted 2 lines

Also, from my understanding, CONFIG_EXPERIMENTAL refers to stability of the driver.

I'd think so too, which is why I wanted to do the Wine test since I'm not absolutely 130% certain that my driver is stable. It is certainly 99.9% stable, but there are still some remaining tiny nagging doubts...

Quoted 4 lines
Quoted 2 lines

Oh, and needless to say it's a *very good* idea in general to run the Wine test suite on all sorts of ALSA drivers...

That's true. We'd need own sound test suite, too.

Oh, ALSA doesn't have anything here yet?
That's pretty crucial if you want to guarantee a certain service quality over the full range of ALSA sound drivers...
(but I'm stating the obvious here ;)

Andreas Mohr

Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

#8 lore raw

At Tue, 27 Jun 2006 16:06:30 +0200,

Quoted 17 lines · Andreas Mohr

Hi,

Quoted 13 lines · Takashi Iwai

At Tue, 27 Jun 2006 15:45:34 +0200,

Quoted 10 lines · Andreas Mohr
Quoted 4 lines

BTW, do you still keep CONFIG_EXPERIMENTAL for AZT3328 driver? If it works for you well, we can remove that flag since there is no many bug reports.

Been thinking about that before (since the driver is almost as feature-complete as it can get with an entirely undocumented card), but I wanted to run the Wine audio test suite on it directly before removing the flag (since that could easily reveal some issues due to much more varied sound traffic), but I haven't managed to do that yet.

Quoted 6 lines
Quoted 3 lines

The reason I asked is because I'm adding a new configure option --with-experimental to toggle on/off CONFIG_EXPERIMENTAL for alsa-driver tree.

Ah. Nice option, but this probably means that all CONFIG_EXPERIMENTAL drivers won't get built by default, right?

No, I'll set it on as default (for avoiding confusion).

Quoted 9 lines

In which case I'd have to act in my driver's area, since it certainly absolutely should get built by default.

Quoted 2 lines

Also, from my understanding, CONFIG_EXPERIMENTAL refers to stability of the driver.

I'd think so too, which is why I wanted to do the Wine test since I'm not absolutely 130% certain that my driver is stable. It is certainly 99.9% stable, but there are still some remaining tiny nagging doubts...

OK, it's your baby :)

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Oh, and needless to say it's a *very good* idea in general to run the Wine test suite on all sorts of ALSA drivers...

That's true. We'd need own sound test suite, too.

Oh, ALSA doesn't have anything here yet?
That's pretty crucial if you want to guarantee a certain service quality over the full range of ALSA sound drivers...
(but I'm stating the obvious here ;)

Well, we have "standard" programs that can be used for various tests, such as speaker-test. But, they are not bundled as "test suite".

A set of neat shell scripts would be really appreciated.

Takashi

Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642