32 Memory Leaks on Error Paths
A message that is not in the archive (sent off-list or not yet received): [email protected]
sound/oss/ymfpci.c line 2643
Quoted 12 lines
Date: Tue, 23 Sep 2003 13:15:15 -0700
From: Chris Wright <[email protected]>sound/oss/ymfpci.c +1
-0@@ -2638,6 +2638,7 @@ out_free:if (codec->ac97_codec[0 ])ac97_release_codec (codec->ac97_codec[0 ]); +kfree (codec);return -ENODEV ; }
Someone was savaging sound code, adding these bugs. For 2.6, the above patch is probably ok; it's immaterial, because ALSA won long ago.
A patch for 2.4 to undo the damage is attached.
-- Pete
drivers/sound/ymfpci.c +20 -21
@@ -173,43 +173,40 @@static void ymfpci_codec_write (struct ac97_codec *dev,u8 reg,u16 val) { -ymfpci_t *codec = dev->private_data; +ymfpci_t *unit = dev->private_data;u32 cmd; -spin_lock (&codec->ac97_lock); +down (&unit->ac97_lock);/* XXX Do make use of dev->id */ -ymfpci_codec_ready (codec,0 ,0 ); +ymfpci_codec_ready (unit,0 ,0 ); cmd = ((YDSXG_AC97WRITECMD | reg) <<16 ) | val; -ymfpci_writel (codec,YDSXGR_AC97CMDDATA , cmd); -spin_unlock (&codec->ac97_lock); +ymfpci_writel (unit,YDSXGR_AC97CMDDATA , cmd); +up (&unit->ac97_lock); } -static u16 _ymfpci_codec_read (ymfpci_t *unit,u8 reg) +static u16 ymfpci_codec_read (struct ac97_codec *dev,u8 reg) { +ymfpci_t *unit = dev->private_data;int i; +u16 ret; +down (&unit->ac97_lock);if (ymfpci_codec_ready (unit,0 ,0 )) -return ~0 ; + goto out_none;ymfpci_writew (unit,YDSXGR_AC97CMDADR ,YDSXG_AC97READCMD | reg);if (ymfpci_codec_ready (unit,0 ,0 )) -return ~0 ; + goto out_none;if (unit->pci->device ==PCI_DEVICE_ID_YAMAHA_744 && unit->rev <2 ) {for (i =0 ; i <600 ; i++)ymfpci_readw (unit,YDSXGR_PRISTATUSDATA ); } -return ymfpci_readw (unit,YDSXGR_PRISTATUSDATA ); -} - -static u16 ymfpci_codec_read (struct ac97_codec *dev,u8 reg) -{ -ymfpci_t *unit = dev->private_data; -u16 ret; - -spin_lock (&unit->ac97_lock); - ret =_ymfpci_codec_read (unit, reg); -spin_unlock (&unit->ac97_lock); - + ret =ymfpci_readw (unit,YDSXGR_PRISTATUSDATA ); +up (&unit->ac97_lock);return ret; + +out_none: +up (&unit->ac97_lock); +return ~0 ; }/*
@@ -2526,7 +2523,7 @@spin_lock_init (&codec->reg_lock);spin_lock_init (&codec->voice_lock); -spin_lock_init (&codec->ac97_lock); +init_MUTEX (&codec->ac97_lock);init_MUTEX (&codec->open_sem);INIT_LIST_HEAD (&codec->states); codec->pci = pcidev;
@@ -2625,6 +2622,7 @@ out_release_region:release_mem_region (pci_resource_start (pcidev,0 ),0x8000 ); out_free: +kfree (codec);return -ENODEV ; }
@@ -2652,6 +2650,7 @@ unload_uart401(&codec->mpu_data); }#endif /* CONFIG_SOUND_YMFPCI_LEGACY */ +kfree (codec); }MODULE_AUTHOR ("Jaroslav Kysela" );
drivers/sound/ymfpci.h +1 -1
@@ -275,7 +275,7 @@spinlock_t reg_lock;spinlock_t voice_lock; -spinlock_t ac97_lock; +struct semaphore ac97_lock;/* soundcore stuff */ int dev_audio;
drivers/sound/ymfpci.c line 181
Quoted 4 lines · Pete Zaitcev
drivers/sound/ymfpci.c +1
-2-spin_lock (&codec->ac97_lock); +down (&unit->ac97_lock);/* XXX Do make use of dev->id */ -ymfpci_codec_ready (codec,0 ,0 );
This breaks ac97 locking and should not be applied. The core ac97 code is called some times with interrupts disabled. That is unavoidable.
The only change that is relevant is the kfree
Quoted 9 lines
drivers/sound/ymfpci.c line 181
Quoted 4 lines
drivers/sound/ymfpci.c +1
-2-spin_lock (&codec->ac97_lock); +down (&unit->ac97_lock);/* XXX Do make use of dev->id */ -ymfpci_codec_ready (codec,0 ,0 );This breaks ac97 locking and should not be applied. The core ac97 code is called some times with interrupts disabled. That is unavoidable.
The only change that is relevant is the kfree
In that case, whoever added spinlocks should have removed schedule() from ymfpci_ready_wait().
-- Pete
Quoted 17 lines · Pete Zaitcev
sound/oss/ymfpci.c line 2643
Quoted 12 lines
Date: Tue, 23 Sep 2003 13:15:15 -0700
From: Chris Wright <[email protected]>sound/oss/ymfpci.c +1
-0@@ -2638,6 +2638,7 @@ out_free:if (codec->ac97_codec[0 ])ac97_release_codec (codec->ac97_codec[0 ]); +kfree (codec);return -ENODEV ; }Someone was savaging sound code, adding these bugs. For 2.6, the above patch is probably ok; it's immaterial, because ALSA won long ago.
Quoted 1 line
A patch for 2.4 to undo the damage is attached.
Pete,
So let me see if I get it right: The above "+ kfree(codec)" addition caused the bug?
Quoted 19 lines
Date: Mon, 29 Sep 2003 14:48:33 -0300 (BRT)
From: Marcelo Tosatti <[email protected]>Quoted 16 lines
sound/oss/ymfpci.c line 2642
Quoted 11 lines
Date: Tue, 23 Sep 2003 13:15:15 -0700
From: Chris Wright <[email protected]>sound/oss/ymfpci.c +1
-0@@ -2638,6 +2638,7 @@ out_free:if (codec->ac97_codec[0 ])ac97_release_codec (codec->ac97_codec[0 ]); +kfree (codec);return -ENODEV ;Someone was savaging sound code, adding these bugs. For 2.6, the above patch is probably ok; it's immaterial, because ALSA won long ago.
Quoted 3 lines
Quoted 1 line
A patch for 2.4 to undo the damage is attached.
So let me see if I get it right: The above "+ kfree(codec)" addition caused the bug?
No, bugs were added for 2.4.22 (a diff between 2.4.21 & .22 removes kfree's). Chris adds some kfrees back, but not all.
Also, 2.4.22 adds spinlocks around schedule(). My bigger patch replaces them with semaphores, because interrupts are never involved and they were done from a process context only. However, Alan noted that ac97 operations can be called with interrupts closed, and so the right approach is to retain spinlocks, but replace schedule() with mdelay(). This is gonna be one huge sleep under cli... Nonetheless, I'll send a better patch soonish.
-- Pete
Quoted 12 lines · Marcelo Tosatti
Quoted 12 lines
sound/oss/ymfpci.c line 2643
Quoted 12 lines
Date: Tue, 23 Sep 2003 13:15:15 -0700
From: Chris Wright <[email protected]>sound/oss/ymfpci.c +1
-0@@ -2638,6 +2638,7 @@ out_free:if (codec->ac97_codec[0 ])ac97_release_codec (codec->ac97_codec[0 ]); +kfree (codec);return -ENODEV ; }
I got around to fixing both missing kfrees and the schedule under lock (for 2.4). Alan should be happy now.
-- Pete
P.S. I do not want to fix the 2.6. For crying out loud, the driver was copied bit by bit from Jaroslav's ALSA code. Now that we have ALSA in the kernel, can we let go already? I do not see why anyone would want to use my driver instead of Jaroslav's driver.
drivers/sound/ymfpci.c +26 -30
@@ -152,22 +152,19 @@writel (val, codec->reg_area_virt + offset); } -static int ymfpci_codec_ready (ymfpci_t *codec,int secondary,int sched) +static int ymfpci_codec_ready (ymfpci_t *unit,int secondary) { -signed long end_time; +enum {READY_STEP =10 };u32 reg = secondary ?YDSXGR_SECSTATUSADR :YDSXGR_PRISTATUSADR ; +int i; - end_time = jiffies +3 * (HZ /4 ); -do { -if ((ymfpci_readw (codec, reg) &0x8000 ) ==0 ) +for (i =0 ; i < ((3 *1000 )/4 ) /READY_STEP ; i++) { +if ((ymfpci_readw (unit, reg) &0x8000 ) ==0 )return 0 ; -if (sched) { -set_current_state (TASK_UNINTERRUPTIBLE ); -schedule_timeout (1 ); - } - }while (end_time - (signed long )jiffies >=0 ); +mdelay (READY_STEP ); + }printk (KERN_ERR "ymfpci_codec_ready: codec %i is not ready [0x%x]\n" , - secondary,ymfpci_readw (codec, reg)); + secondary,ymfpci_readw (unit, reg));return -EBUSY ; }
@@ -178,38 +175,35 @@spin_lock (&codec->ac97_lock);/* XXX Do make use of dev->id */ -ymfpci_codec_ready (codec,0 ,0 ); +ymfpci_codec_ready (codec,0 ); cmd = ((YDSXG_AC97WRITECMD | reg) <<16 ) | val;ymfpci_writel (codec,YDSXGR_AC97CMDDATA , cmd);spin_unlock (&codec->ac97_lock); } -static u16 _ymfpci_codec_read (ymfpci_t *unit,u8 reg) +static u16 ymfpci_codec_read (struct ac97_codec *dev,u8 reg) { +ymfpci_t *unit = dev->private_data; +u16 ret;int i; -if (ymfpci_codec_ready (unit,0 ,0 )) -return ~0 ; +spin_lock (&unit->ac97_lock); +if (ymfpci_codec_ready (unit,0 )) + goto out_err;ymfpci_writew (unit,YDSXGR_AC97CMDADR ,YDSXG_AC97READCMD | reg); -if (ymfpci_codec_ready (unit,0 ,0 )) -return ~0 ; +if (ymfpci_codec_ready (unit,0 )) + goto out_err;if (unit->pci->device ==PCI_DEVICE_ID_YAMAHA_744 && unit->rev <2 ) {for (i =0 ; i <600 ; i++)ymfpci_readw (unit,YDSXGR_PRISTATUSDATA ); } -return ymfpci_readw (unit,YDSXGR_PRISTATUSDATA ); -} - -static u16 ymfpci_codec_read (struct ac97_codec *dev,u8 reg) -{ -ymfpci_t *unit = dev->private_data; -u16 ret; - -spin_lock (&unit->ac97_lock); - ret =_ymfpci_codec_read (unit, reg); + ret =ymfpci_readw (unit,YDSXGR_PRISTATUSDATA );spin_unlock (&unit->ac97_lock); -return ret; + + out_err: +spin_unlock (&unit->ac97_lock); +return ~0 ; }/*
@@ -2123,7 +2117,7 @@int i;ymfpci_aclink_reset (unit->pci); -ymfpci_codec_ready (unit,0 ,1 );/* prints diag if not ready. */ +ymfpci_codec_ready (unit,0 );/* prints diag if not ready. */ #ifdef CONFIG_SOUND_YMFPCI_LEGACY /* XXX At this time the legacy registers are probably deprogrammed. */
@@ -2549,7 +2543,7 @@ (char *)ent->driver_data, base, pcidev->irq);ymfpci_aclink_reset (pcidev); -if (ymfpci_codec_ready (codec,0 ,1 ) <0 ) +if (ymfpci_codec_ready (codec,0 ) <0 ) goto out_unmap;#ifdef CONFIG_SOUND_YMFPCI_LEGACY
@@ -2625,6 +2619,7 @@ out_release_region:release_mem_region (pci_resource_start (pcidev,0 ),0x8000 ); out_free: +kfree (codec);return -ENODEV ; }
@@ -2652,6 +2647,7 @@ unload_uart401(&codec->mpu_data); }#endif /* CONFIG_SOUND_YMFPCI_LEGACY */ +kfree (codec); }MODULE_AUTHOR ("Jaroslav Kysela" );