Skip to contentSkip to conversation

(s): Use spinlocks instead of STI/CLI in SoundBlaster

PATCH In review Chris Rankin 2 participants 3 messages Jan 26, 2001 – Jan 27, 2001

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 (s): Use spinlocks instead of STI/CLI in SoundBlaster

Hi,

I hear on the grapevine that 2.4 kernel modules should use spinlocks in preference to cli() and sti(). Well I'm not sure how big a win it is, particularly on a UP machine, but here's a patch for the SoundBlaster. I've added a spinlock_t to the "struct b_devc" so that multiple SoundBlasters each get their own lock. After all, each SB has its own IRQ and IO, correct?

There also seems to be something here called a Jazz16. This has a global lock because it looks like there can only be one of them.

drivers/sound/sb.h renamed from drivers/sound/sb.h.orig +2 -0
@@ -137,6 +137,8 @@
 	   void (*midi_input_intr) (int dev, unsigned char data);
 	   void *midi_irq_cookie;		/* IRQ cookie for the midi */
 
+	   spinlock_t lock;
+
 	   struct sb_module_options sbmo;	/* Module options */
 
 	} sb_devc;
drivers/sound/sb_audio.c renamed from drivers/sound/sb_audio.c.orig +31 -41

229 lines not on this page · raw message

drivers/sound/sb_ess.c renamed from drivers/sound/sb_ess.c.orig +9 -12
@@ -186,6 +186,7 @@
  */
 
 #include <linux/delay.h>
+#include <linux/spinlock.h>
 
 #include "sound_config.h"
 #include "sb_mixer.h"
@@ -524,10 +525,9 @@
 	unsigned long flags;
 	sb_devc *devc = audio_devs[dev]->devc;
 
-	save_flags(flags);
-	cli();
+	spin_lock_irqsave(&devc->lock, flags);
 	sb_dsp_reset(devc);
-	restore_flags(flags);
+	spin_unlock_irqrestore(&devc->lock, flags);
 
 	/*
 	 * Audio 2 may still be operational! Creates awful sounds!
@@ -969,8 +969,7 @@
 	unsigned int val;
 	unsigned long flags;
 
-	save_flags(flags);
-	cli();
+	spin_lock_irqsave(&devc->lock, flags);
 	outb(((unsigned char) (0x40 & 0xff)), MIXER_ADDR);
 
 	udelay(20);
@@ -978,7 +977,7 @@
 	udelay(20);
 	val |= inb(MIXER_DATA);
 	udelay(20);
-	restore_flags(flags);
+	spin_unlock_irqrestore(&devc->lock, flags);
 
 	return val;
 }
@@ -1565,8 +1564,7 @@
 printk(KERN_INFO "FKS: write mixer %x: %x\n", port, value);
 #endif
 
-	save_flags(flags);
-	cli();
+	spin_lock_irqsave(&devc->lock, flags);
 	if (port >= 0xa0) {
 		ess_write (devc, port, value);
 	} else {
@@ -1576,7 +1574,7 @@
 		outb(((unsigned char) (value & 0xff)), MIXER_DATA);
 		udelay(20);
 	};
-	restore_flags(flags);
+	spin_unlock_irqrestore(&devc->lock, flags);
 }
 
 unsigned int ess_getmixer (sb_devc * devc, unsigned int port)
@@ -1584,8 +1582,7 @@
 	unsigned int val;
 	unsigned long flags;
 
-	save_flags(flags);
-	cli();
+	spin_lock_irqsave(&devc->lock, flags);
 
 	if (port >= 0xa0) {
 		val = ess_read (devc, port);
@@ -1596,7 +1593,7 @@
 		val = inb(MIXER_DATA);
 		udelay(20);
 	}
-	restore_flags(flags);
+	spin_unlock_irqrestore(&devc->lock, flags);
 
 	return val;
 }
drivers/sound/sb_midi.c renamed from drivers/sound/sb_midi.c.orig +9 -10
@@ -11,6 +11,8 @@
  * for more info.
  */
 
+#include <linux/spinlock.h>
+
 #include "sound_config.h"
 
 #include "sb.h"
@@ -36,15 +38,14 @@
 	if (devc == NULL)
 		return -ENXIO;
 
-	save_flags(flags);
-	cli();
+	spin_lock_irqsave(&devc->lock, flags);
 	if (devc->opened)
 	{
-		restore_flags(flags);
+		spin_unlock_irqrestore(&devc->lock, flags);
 		return -EBUSY;
 	}
 	devc->opened = 1;
-	restore_flags(flags);
+	spin_unlock_irqrestore(&devc->lock, flags);
 
 	devc->irq_mode = IMODE_MIDI;
 	devc->midi_broken = 0;
@@ -74,13 +75,12 @@
 	if (devc == NULL)
 		return;
 
-	save_flags(flags);
-	cli();
+	spin_lock_irqsave(&devc->lock, flags);
 	sb_dsp_reset(devc);
 	devc->intr_active = 0;
 	devc->input_opened = 0;
 	devc->opened = 0;
-	restore_flags(flags);
+	spin_unlock_irqrestore(&devc->lock, flags);
 }
 
 static int sb_midi_out(int dev, unsigned char midi_byte)
@@ -131,14 +131,13 @@
 	if (devc == NULL)
 		return;
 
-	save_flags(flags);
-	cli();
+	spin_lock_irqsave(&devc->lock, flags);
 
 	data = inb(DSP_READ);
 	if (devc->input_opened)
 		devc->midi_input_intr(devc->my_mididev, data);
 
-	restore_flags(flags);
+	spin_unlock_irqrestore(&devc->lock, flags);
 }
 
 #define MIDI_SYNTH_NAME	"Sound Blaster Midi"
drivers/sound/sb_common.c renamed from drivers/sound/sb_common.c.orig +28 -24

165 lines not on this page · raw message

And for an encore, I noticed that my SoundBlaster module loaded the uart401 module. So here's a spinlock patch for the uart401:

drivers/sound/uart401.c renamed from drivers/sound/uart401.c.orig +11 -9
@@ -16,6 +16,7 @@
  *				Fixed to allow IRQ > 15
  *	Christoph Hellwig	Adapted to module_init/module_exit
  *	Arnaldo C. de Melo	got rid of check_region
+ *	Chris Rankin		Used spinlocks instead of STI/CLI
  *
  * Status:
  *		Untested
@@ -23,6 +24,7 @@
 
 #include <linux/init.h>
 #include <linux/module.h>
+#include <linux/spinlock.h> 
 
 #include "sound_config.h"
 
@@ -38,6 +40,7 @@
 	volatile unsigned char input_byte;
 	int             my_dev;
 	int             share_irq;
+	spinlock_t      lock;
 }
 uart401_devc;
 
@@ -152,13 +155,12 @@
 	 * Test for input since pending input seems to block the output.
 	 */
 
-	save_flags(flags);
-	cli();
+	spin_lock_irqsave(&devc->lock, flags);
 
 	if (input_avail(devc))
 		uart401_input_loop(devc);
 
-	restore_flags(flags);
+	spin_unlock_irqrestore(&devc->lock, flags);
 
 	/*
 	 * Sometimes it takes about 13000 loops before the output becomes ready
@@ -222,8 +224,8 @@
 	int ok, timeout;
 	unsigned long flags;
 
-	save_flags(flags);
-	cli();
+	spin_lock_irqsave(&devc->lock, flags);
+
 	for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
 
 	devc->input_byte = 0;
@@ -237,7 +239,7 @@
 			if (uart401_read(devc) == MPU_ACK)
 				ok = 1;
 
-	restore_flags(flags);
+	spin_unlock_irqrestore(&devc->lock, flags);
 }
 
 static int reset_uart401(uart401_devc * devc)
@@ -320,11 +322,11 @@
 	devc->input_byte = 0;
 	devc->my_dev = 0;
 	devc->share_irq = 0;
+	devc->lock = SPIN_LOCK_UNLOCKED;
 
-	save_flags(flags);
-	cli();
+	spin_lock_irqsave(&devc->lock, flags);
 	ok = reset_uart401(devc);
-	restore_flags(flags);
+	spin_unlock_irqrestore(&devc->lock, flags);
 
 	if (!ok)
 		goto cleanup_devc;

Then I got cocky and noticed a CLI/STI pair in the sound_open_dma() and sound_close_dma() functions. I couldn't identify a device to associate the spinlock with, and so I made it global:

drivers/sound/soundcard.c renamed from drivers/sound/soundcard.c.orig +10 -8
@@ -20,6 +20,7 @@
  * Richard Gooch     : moved common (non OSS-specific) devices to sound_core.c
  * Rob Riggs	     : Added persistent DMA buffers support (1998/10/17)
  * Christoph Hellwig : Some cleanup work (2000/03/01)
+ * Chris Rankin      : Protect DMA access with a spinlock
  */
 
 #include <linux/config.h>
@@ -44,6 +45,9 @@
 #include <linux/delay.h>
 #include <linux/proc_fs.h>
 #include <linux/smp_lock.h>
+#include <linux/spinlock.h>
+
+static spinlock_t dma_lock = SPIN_LOCK_UNLOCKED;
 
 /*
  * This ought to be moved into include/asm/dma.h
@@ -672,16 +676,15 @@
 		printk(KERN_ERR "sound_open_dma: Invalid DMA channel %d\n", chn);
 		return 1;
 	}
-	save_flags(flags);
-	cli();
+	spin_lock_irqsave(&dma_lock, flags);
 
 	if (dma_alloc_map[chn] != DMA_MAP_FREE) {
+		spin_unlock_irqrestore(&dma_lock, flags);
 		printk("sound_open_dma: DMA channel %d busy or not allocated (%d)\n", chn, dma_alloc_map[chn]);
-		restore_flags(flags);
 		return 1;
 	}
 	dma_alloc_map[chn] = DMA_MAP_BUSY;
-	restore_flags(flags);
+	spin_unlock_irqrestore(&dma_lock, flags);
 	return 0;
 }
 
@@ -699,16 +702,15 @@
 {
 	unsigned long   flags;
 
-	save_flags(flags);
-	cli();
+	spin_lock_irqsave(&dma_lock, flags);
 
 	if (dma_alloc_map[chn] != DMA_MAP_BUSY) {
+		spin_unlock_irqrestore(&dma_lock, flags);
 		printk(KERN_ERR "sound_close_dma: Bad access to DMA channel %d\n", chn);
-		restore_flags(flags);
 		return;
 	}
 	dma_alloc_map[chn] = DMA_MAP_FREE;
-	restore_flags(flags);
+	spin_unlock_irqrestore(&dma_lock, flags);
 }
 
 static void do_sequencer_timer(unsigned long dummy)

And THEN I thought that it might be a good idea to put a few "const" declarations into a few function prototypes. I think that this kind of stuff doesn't hurt, and might even allow someone (?) to move a few structs (which shouldn't be changing anyway) into the .text section:

drivers/sound/uart401.c renamed from drivers/sound/uart401.c.orig +1 -1
@@ -292,7 +292,7 @@
 int probe_uart401(struct address_info *hw_config, struct module *owner)
 {
 	uart401_devc *devc;
-	char *name = "MPU-401 (UART) MIDI";
+	const char *name = "MPU-401 (UART) MIDI";
 	int ok = 0;
 	unsigned long flags;
 
drivers/sound/sound_calls.h renamed from drivers/sound/sound_calls.h.orig +2 -2
@@ -80,8 +80,8 @@
 /*	From soundcard.c	*/
 void request_sound_timer (int count);
 void sound_stop_timer(void);
-void conf_printf(char *name, struct address_info *hw_config);
-void conf_printf2(char *name, int base, int irq, int dma, int dma2);
+void conf_printf(const char *name, const struct address_info *hw_config);
+void conf_printf2(const char *name, int base, int irq, int dma, int dma2);
 
 /*	From sound_timer.c */
 void sound_timer_interrupt(void);
drivers/sound/soundcard.c renamed from drivers/sound/soundcard.c.orig +2 -2
@@ -745,7 +745,7 @@
 	del_timer(&seq_timer);;
 }
 
-void conf_printf(char *name, struct address_info *hw_config)
+void conf_printf(const char *name, const struct address_info *hw_config)
 {
 #ifndef CONFIG_SOUND_TRACEINIT
 	return;
@@ -765,7 +765,7 @@
 #endif
 }
 
-void conf_printf2(char *name, int base, int irq, int dma, int dma2)
+void conf_printf2(const char *name, int base, int irq, int dma, int dma2)
 {
 #ifndef CONFIG_SOUND_TRACEINIT
 	return;
drivers/sound/dev_table.h +4 -4
@@ -376,16 +377,16 @@
 extern int num_sound_timers;
 #endif	/* _DEV_TABLE_C_ */
 
-extern int sound_map_buffer (int dev, struct dma_buffparms *dmap, buffmem_desc *info);
 void sound_timer_init (struct sound_lowlev_timer *t, char *name);
-void sound_dma_intr (int dev, struct dma_buffparms *dmap, int chan);
 
 #define AUDIO_DRIVER_VERSION	2
 #define MIXER_DRIVER_VERSION	2
-int sound_install_audiodrv(int vers, char *name, struct audio_driver *driver,
+int sound_install_audiodrv(int vers, const char *name,
+			const struct audio_driver *driver,
 			int driver_size, int flags, unsigned int format_mask,
 			void *devc, int dma1, int dma2);
-int sound_install_mixer(int vers, char *name, struct mixer_operations *driver,
+int sound_install_mixer(int vers, const char *name,
+			const struct mixer_operations *driver,
 			int driver_size, void *devc);
 
 void sound_unload_audiodev(int dev);
drivers/sound/dev_table.c +9 -7
@@ -16,7 +16,8 @@
 #define _DEV_TABLE_C_
 #include "sound_config.h"
 
-int sound_install_audiodrv(int vers, char *name, struct audio_driver *driver,
+int sound_install_audiodrv(int vers, const char *name,
+			const struct audio_driver *driver,
 			int driver_size, int flags, unsigned int format_mask,
 			void *devc, int dma1, int dma2)
 {
@@ -48,14 +49,14 @@
 		sound_unload_audiodev(num);
 		return -(ENOMEM);
 	}
-	memset((char *) op, 0, sizeof(struct audio_operations));
+	memset(op, 0, sizeof(struct audio_operations));
 	init_waitqueue_head(&op->in_sleeper);
 	init_waitqueue_head(&op->out_sleeper);	
 	init_waitqueue_head(&op->poll_sleeper);
 	if (driver_size < sizeof(struct audio_driver))
-		memset((char *) d, 0, sizeof(struct audio_driver));
+		memset(d, 0, sizeof(struct audio_driver));
 
-	memcpy((char *) d, (char *) driver, driver_size);
+	memcpy(d, driver, driver_size);
 
 	op->d = d;
 	l = strlen(name) + 1;
@@ -78,7 +79,8 @@
 	return num;
 }
 
-int sound_install_mixer(int vers, char *name, struct mixer_operations *driver,
+int sound_install_mixer(int vers, const char *name,
+	const struct mixer_operations *driver,
 	int driver_size, void *devc)
 {
 	struct mixer_operations *op;
@@ -107,8 +109,8 @@
 		printk(KERN_ERR "Sound: Can't allocate mixer driver for (%s)\n", name);
 		return -ENOMEM;
 	}
-	memset((char *) op, 0, sizeof(struct mixer_operations));
-	memcpy((char *) op, (char *) driver, driver_size);
+	memset(op, 0, sizeof(struct mixer_operations));
+	memcpy(op, driver, driver_size);
 
 	l = strlen(name) + 1;
 	if (l > sizeof(op->name))

OK, that's all. I think that the const-stuff is trivial in the sense that once the function bodies compile then you're home and dry because their callers don't care. I'm more concerned about the spinlock stuff
- would 2.4's improved locking scheme make some of them completely redundant anyway?

Cheers,
Chris

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/

#1 lore raw

Hi!

Quoted 9 lines

I hear on the grapevine that 2.4 kernel modules should use spinlocks in preference to cli() and sti(). Well I'm not sure how big a win it is, particularly on a UP machine, but here's a patch for the SoundBlaster. I've added a spinlock_t to the "struct b_devc" so that multiple SoundBlasters each get their own lock. After all, each SB has its own IRQ and IO, correct?

There also seems to be something here called a Jazz16. This has a global lock because it looks like there can only be one of them.

Should not you initialize spinlock?

drivers/sound/sb.h line 141

Quoted 8 lines
drivers/sound/sb.h renamed from drivers/sound/sb.h.orig +2 -0
@@ -137,6 +137,8 @@
 	   void (*midi_input_intr) (int dev, unsigned char data);
 	   void *midi_irq_cookie;		/* IRQ cookie for the midi */
 
+	   spinlock_t lock;
+

Pavel

Signature · 6 lines
I'm [email protected]. "In my country we have almost anarchy and I don't care."
Panos Katsaloulis describing me w.r.t. patents at [email protected]
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/
#2 lore raw
Quoted 8 lines

drivers/sound/sb.h line 141

Quoted 8 lines
drivers/sound/sb.h renamed from drivers/sound/sb.h.orig +2 -0
@@ -137,6 +137,8 @@
 	   void (*midi_input_intr) (int dev, unsigned char data);
 	   void *midi_irq_cookie;		/* IRQ cookie for the midi */
 
+	   spinlock_t lock;
+

I do; that declaration is just a typedef for the device struct. The spinlock is explicitly initialised in the sb_dsp_detect() function.

Chris
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/