Alsa-Devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ALSA: fix kernel panic in initialization of mpu401 driver
@ 2006-10-04 20:22 Jiri Kosina
  2006-10-05  8:07 ` Jaroslav Kysela
  0 siblings, 1 reply; 13+ messages in thread
From: Jiri Kosina @ 2006-10-04 20:22 UTC (permalink / raw)
  To: Jaroslav Kysela, Castet Matthieu, Takashi Iwai
  Cc: Andrew Morton, alsa-devel, linux-kernel

I am getting kernel panic (NULL pointer dereference) on boot, with kernel 
compiled with CONFIG_SND_MPU401_UART=y, on machine which does not have 
this piece of hardware.

I have traced the problem down to 
sound/drivers/mpu401/mpu401.c:snd_mpu401_probe() returning EINVAL, when 
either port or IRQ parameters are not specified.

In such case, the drivers/base/bus.c:bus_attach_device() does not perform 
klist_add_tail() call, but rather sets dev->is_registered to 0. This flag 
is however not checked by the driver, so later on, when 
alsa_card_mpu401_init() is called and platform_device_register_simple() 
fails, the following callchain happens, causing NULL pointer dereference: 
alsa_card_mpu401_init() -> platform_device_unregister() -> 
platform_device_del() -> device_del() -> bus_remove_device() -> 
klist_del() -> BOOM (the entry was not added to klist in 
bus_attach_device()).

Proper solution is returning ENODEV from the ->probe() routine, which will 
be correctly handled then by the rest of the device-driver attaching 
subsystem (namely the retval check in bus_attach_device()). The following 
patch fixes the problem, please apply.

Patch against current Linus' git tree.

Signed-off-by: Jiri Kosina <jikos@jikos.cz>

--- a/sound/drivers/mpu401/mpu401.c
+++ b/sound/drivers/mpu401/mpu401.c
@@ -104,11 +104,11 @@ static int __devinit snd_mpu401_probe(st
 
 	if (port[dev] == SNDRV_AUTO_PORT) {
 		snd_printk(KERN_ERR "specify port\n");
-		return -EINVAL;
+		return -ENODEV;
 	}
 	if (irq[dev] == SNDRV_AUTO_IRQ) {
 		snd_printk(KERN_ERR "specify or disable IRQ\n");
-		return -EINVAL;
+		return -ENODEV;
 	}
 	err = snd_mpu401_create(dev, &card);
 	if (err < 0)

-- 
Jiri Kosina

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] ALSA: fix kernel panic in initialization of mpu401 driver
  2006-10-04 20:22 [PATCH] ALSA: fix kernel panic in initialization of mpu401 driver Jiri Kosina
@ 2006-10-05  8:07 ` Jaroslav Kysela
  2006-10-05  9:57   ` Jiri Kosina
  2006-10-05 14:02   ` Alan Stern
  0 siblings, 2 replies; 13+ messages in thread
From: Jaroslav Kysela @ 2006-10-05  8:07 UTC (permalink / raw)
  To: Jiri Kosina
  Cc: Andrew Morton, ALSA development, Takashi Iwai, Greg Kroah-Hartman,
	LKML, Alan Stern, Castet Matthieu

On Wed, 4 Oct 2006, Jiri Kosina wrote:

> I am getting kernel panic (NULL pointer dereference) on boot, with kernel 
> compiled with CONFIG_SND_MPU401_UART=y, on machine which does not have 
> this piece of hardware.
> 
> I have traced the problem down to 
> sound/drivers/mpu401/mpu401.c:snd_mpu401_probe() returning EINVAL, when 
> either port or IRQ parameters are not specified.
> 
> In such case, the drivers/base/bus.c:bus_attach_device() does not perform 
> klist_add_tail() call, but rather sets dev->is_registered to 0. This flag 
> is however not checked by the driver, so later on, when 
> alsa_card_mpu401_init() is called and platform_device_register_simple() 
> fails, the following callchain happens, causing NULL pointer dereference: 
> alsa_card_mpu401_init() -> platform_device_unregister() -> 
> platform_device_del() -> device_del() -> bus_remove_device() -> 
> klist_del() -> BOOM (the entry was not added to klist in 
> bus_attach_device()).
> 
> Proper solution is returning ENODEV from the ->probe() routine, which will 
> be correctly handled then by the rest of the device-driver attaching 
> subsystem (namely the retval check in bus_attach_device()). The following 
> patch fixes the problem, please apply.

Unfortunately, I do not think that it's a proper solution. I think that
platform device layer should play more nicely and if probe() fails for 
a reason and if platform_device_register_simple() does not set 
IS_ERR(), then platform_device_unregister() must be callable to free
all resources.

Also, you've proposed to not fix all returns in snd_mpu401_probe() only 
first two.

I would reject this patch and fix drivers/base/bus.c. The problematic 
change is in commit f2eaae197f4590c4d96f31b09b0ee9067421a95c and this 
patch will probably fix it:

[PATCH] drivers/base - check if device is registered before removal

Without this fix platform_device_unregister() might oops.

Signed-off-by: Jaroslav Kysela <perex@suse.cz>

diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index 12173d1..daa2390 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -428,8 +428,10 @@ void bus_remove_device(struct device * d
 		sysfs_remove_link(&dev->kobj, "bus");
 		sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
 		device_remove_attrs(dev->bus, dev);
-		dev->is_registered = 0;
-		klist_del(&dev->knode_bus);
+		if (dev->is_registered) {
+			dev->is_registered = 0;
+			klist_del(&dev->knode_bus);
+		}
 		pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
 		device_release_driver(dev);
 		put_bus(dev->bus);

						Jaroslav

-----
Jaroslav Kysela <perex@suse.cz>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH] ALSA: fix kernel panic in initialization of mpu401 driver
  2006-10-05  8:07 ` Jaroslav Kysela
@ 2006-10-05  9:57   ` Jiri Kosina
  2006-10-05 14:02   ` Alan Stern
  1 sibling, 0 replies; 13+ messages in thread
From: Jiri Kosina @ 2006-10-05  9:57 UTC (permalink / raw)
  To: Jaroslav Kysela
  Cc: Andrew Morton, ALSA development, Takashi Iwai, Greg Kroah-Hartman,
	LKML, Alan Stern, Castet Matthieu

On Thu, 5 Oct 2006, Jaroslav Kysela wrote:

> Unfortunately, I do not think that it's a proper solution. I think that 
> platform device layer should play more nicely and if probe() fails for a 
> reason and if platform_device_register_simple() does not set IS_ERR(), 
> then platform_device_unregister() must be callable to free all 
> resources.

I agree.

> I would reject this patch and fix drivers/base/bus.c. The problematic 
> change is in commit f2eaae197f4590c4d96f31b09b0ee9067421a95c and this 
> patch will probably fix it:
> [PATCH] drivers/base - check if device is registered before removal
> Without this fix platform_device_unregister() might oops.
> Signed-off-by: Jaroslav Kysela <perex@suse.cz>
> diff --git a/drivers/base/bus.c b/drivers/base/bus.c
> index 12173d1..daa2390 100644
> --- a/drivers/base/bus.c
> +++ b/drivers/base/bus.c
> @@ -428,8 +428,10 @@ void bus_remove_device(struct device * d
>  		sysfs_remove_link(&dev->kobj, "bus");
>  		sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
>  		device_remove_attrs(dev->bus, dev);
> -		dev->is_registered = 0;
> -		klist_del(&dev->knode_bus);
> +		if (dev->is_registered) {
> +			dev->is_registered = 0;
> +			klist_del(&dev->knode_bus);
> +		}
>  		pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
>  		device_release_driver(dev);
>  		put_bus(dev->bus);

Yes, it (among other things) fixes the panic in MPU401 initialization.

Acked-by: Jiri Kosina <jikos@jikos.cz>

-- 
Jiri Kosina

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] ALSA: fix kernel panic in initialization of mpu401 driver
  2006-10-05  8:07 ` Jaroslav Kysela
  2006-10-05  9:57   ` Jiri Kosina
@ 2006-10-05 14:02   ` Alan Stern
  1 sibling, 0 replies; 13+ messages in thread
From: Alan Stern @ 2006-10-05 14:02 UTC (permalink / raw)
  To: Jaroslav Kysela
  Cc: Andrew Morton, ALSA development, Takashi Iwai, Greg Kroah-Hartman,
	LKML, Jiri Kosina, Castet Matthieu

On Thu, 5 Oct 2006, Jaroslav Kysela wrote:

> On Wed, 4 Oct 2006, Jiri Kosina wrote:
> 
> > I am getting kernel panic (NULL pointer dereference) on boot, with kernel 
> > compiled with CONFIG_SND_MPU401_UART=y, on machine which does not have 
> > this piece of hardware.
> > 
> > I have traced the problem down to 
> > sound/drivers/mpu401/mpu401.c:snd_mpu401_probe() returning EINVAL, when 
> > either port or IRQ parameters are not specified.
> > 
> > In such case, the drivers/base/bus.c:bus_attach_device() does not perform 
> > klist_add_tail() call, but rather sets dev->is_registered to 0. This flag 
> > is however not checked by the driver, so later on, when 
> > alsa_card_mpu401_init() is called and platform_device_register_simple() 
> > fails, the following callchain happens, causing NULL pointer dereference: 
> > alsa_card_mpu401_init() -> platform_device_unregister() -> 
> > platform_device_del() -> device_del() -> bus_remove_device() -> 
> > klist_del() -> BOOM (the entry was not added to klist in 
> > bus_attach_device()).
> > 
> > Proper solution is returning ENODEV from the ->probe() routine, which will 
> > be correctly handled then by the rest of the device-driver attaching 
> > subsystem (namely the retval check in bus_attach_device()). The following 
> > patch fixes the problem, please apply.
> 
> Unfortunately, I do not think that it's a proper solution. I think that
> platform device layer should play more nicely and if probe() fails for 
> a reason and if platform_device_register_simple() does not set 
> IS_ERR(), then platform_device_unregister() must be callable to free
> all resources.
> 
> Also, you've proposed to not fix all returns in snd_mpu401_probe() only 
> first two.
> 
> I would reject this patch and fix drivers/base/bus.c. The problematic 
> change is in commit f2eaae197f4590c4d96f31b09b0ee9067421a95c and this 
> patch will probably fix it:

Your patch is right as far as it goes, but it misses part of the problem.  
device_add() shouldn't ignore the return value from bus_attach_device().  
That's why we ended up trying to unregister a device which never was 
properly registered in the first place.

What do you think of this addition to your patch (untested)?

Alan Stern


Index: 18g20/drivers/base/bus.c
===================================================================
--- 18g20.orig/drivers/base/bus.c
+++ 18g20/drivers/base/bus.c
@@ -453,8 +453,10 @@ void bus_remove_device(struct device * d
 		remove_deprecated_bus_links(dev);
 		sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
 		device_remove_attrs(dev->bus, dev);
-		dev->is_registered = 0;
-		klist_del(&dev->knode_bus);
+		if (dev->is_registered) {
+			dev->is_registered = 0;
+			klist_del(&dev->knode_bus);
+		}
 		pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
 		device_release_driver(dev);
 		put_bus(dev->bus);
Index: 18g20/drivers/base/core.c
===================================================================
--- 18g20.orig/drivers/base/core.c
+++ 18g20/drivers/base/core.c
@@ -485,7 +485,8 @@ int device_add(struct device *dev)
 	if ((error = bus_add_device(dev)))
 		goto BusError;
 	kobject_uevent(&dev->kobj, KOBJ_ADD);
-	bus_attach_device(dev);
+	if ((error = bus_attach_device(dev)))
+		goto AttachError;
 	if (parent)
 		klist_add_tail(&dev->knode_parent, &parent->klist_children);
 
@@ -504,6 +505,8 @@ int device_add(struct device *dev)
  	kfree(class_name);
 	put_device(dev);
 	return error;
+ AttachError:
+	bus_remove_device(dev);
  BusError:
 	device_pm_remove(dev);
  PMError:


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] ALSA: fix kernel panic in initialization of mpu401 driver
       [not found] <Pine.LNX.4.44L0.0610050959330.6226-100000@iolanthe.rowland.org>
@ 2006-10-05 14:16 ` Jaroslav Kysela
       [not found] ` <Pine.LNX.4.61.0610051614220.9351@tm8103.perex-int.cz>
  1 sibling, 0 replies; 13+ messages in thread
From: Jaroslav Kysela @ 2006-10-05 14:16 UTC (permalink / raw)
  To: Alan Stern
  Cc: Andrew Morton, ALSA development, Takashi Iwai, Greg Kroah-Hartman,
	LKML, Jiri Kosina, Castet Matthieu

On Thu, 5 Oct 2006, Alan Stern wrote:

> On Thu, 5 Oct 2006, Jaroslav Kysela wrote:
> 
> > On Wed, 4 Oct 2006, Jiri Kosina wrote:
> > 
> > > I am getting kernel panic (NULL pointer dereference) on boot, with kernel 
> > > compiled with CONFIG_SND_MPU401_UART=y, on machine which does not have 
> > > this piece of hardware.
> > > 
> > > I have traced the problem down to 
> > > sound/drivers/mpu401/mpu401.c:snd_mpu401_probe() returning EINVAL, when 
> > > either port or IRQ parameters are not specified.
> > > 
> > > In such case, the drivers/base/bus.c:bus_attach_device() does not perform 
> > > klist_add_tail() call, but rather sets dev->is_registered to 0. This flag 
> > > is however not checked by the driver, so later on, when 
> > > alsa_card_mpu401_init() is called and platform_device_register_simple() 
> > > fails, the following callchain happens, causing NULL pointer dereference: 
> > > alsa_card_mpu401_init() -> platform_device_unregister() -> 
> > > platform_device_del() -> device_del() -> bus_remove_device() -> 
> > > klist_del() -> BOOM (the entry was not added to klist in 
> > > bus_attach_device()).
> > > 
> > > Proper solution is returning ENODEV from the ->probe() routine, which will 
> > > be correctly handled then by the rest of the device-driver attaching 
> > > subsystem (namely the retval check in bus_attach_device()). The following 
> > > patch fixes the problem, please apply.
> > 
> > Unfortunately, I do not think that it's a proper solution. I think that
> > platform device layer should play more nicely and if probe() fails for 
> > a reason and if platform_device_register_simple() does not set 
> > IS_ERR(), then platform_device_unregister() must be callable to free
> > all resources.
> > 
> > Also, you've proposed to not fix all returns in snd_mpu401_probe() only 
> > first two.
> > 
> > I would reject this patch and fix drivers/base/bus.c. The problematic 
> > change is in commit f2eaae197f4590c4d96f31b09b0ee9067421a95c and this 
> > patch will probably fix it:
> 
> Your patch is right as far as it goes, but it misses part of the problem.  
> device_add() shouldn't ignore the return value from bus_attach_device().  
> That's why we ended up trying to unregister a device which never was 
> properly registered in the first place.
> 
> What do you think of this addition to your patch (untested)?

Your addition looks good. Acked from me.

					Jaroslav

> Index: 18g20/drivers/base/bus.c
> ===================================================================
> --- 18g20.orig/drivers/base/bus.c
> +++ 18g20/drivers/base/bus.c
> @@ -453,8 +453,10 @@ void bus_remove_device(struct device * d
>  		remove_deprecated_bus_links(dev);
>  		sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
>  		device_remove_attrs(dev->bus, dev);
> -		dev->is_registered = 0;
> -		klist_del(&dev->knode_bus);
> +		if (dev->is_registered) {
> +			dev->is_registered = 0;
> +			klist_del(&dev->knode_bus);
> +		}
>  		pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
>  		device_release_driver(dev);
>  		put_bus(dev->bus);
> Index: 18g20/drivers/base/core.c
> ===================================================================
> --- 18g20.orig/drivers/base/core.c
> +++ 18g20/drivers/base/core.c
> @@ -485,7 +485,8 @@ int device_add(struct device *dev)
>  	if ((error = bus_add_device(dev)))
>  		goto BusError;
>  	kobject_uevent(&dev->kobj, KOBJ_ADD);
> -	bus_attach_device(dev);
> +	if ((error = bus_attach_device(dev)))
> +		goto AttachError;
>  	if (parent)
>  		klist_add_tail(&dev->knode_parent, &parent->klist_children);
>  
> @@ -504,6 +505,8 @@ int device_add(struct device *dev)
>   	kfree(class_name);
>  	put_device(dev);
>  	return error;
> + AttachError:
> +	bus_remove_device(dev);
>   BusError:
>  	device_pm_remove(dev);
>   PMError:

-----
Jaroslav Kysela <perex@suse.cz>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] ALSA: fix kernel panic in initialization of mpu401 driver
       [not found] ` <Pine.LNX.4.61.0610051614220.9351@tm8103.perex-int.cz>
@ 2006-10-05 17:58   ` Greg KH
  2006-10-05 21:03     ` [PATCH] Driver core: Don't ignore error returns from probing Alan Stern
  0 siblings, 1 reply; 13+ messages in thread
From: Greg KH @ 2006-10-05 17:58 UTC (permalink / raw)
  To: Jaroslav Kysela
  Cc: Andrew Morton, ALSA development, Takashi Iwai, LKML, Jiri Kosina,
	Alan Stern, Castet Matthieu

On Thu, Oct 05, 2006 at 04:16:01PM +0200, Jaroslav Kysela wrote:
> On Thu, 5 Oct 2006, Alan Stern wrote:
> 
> > On Thu, 5 Oct 2006, Jaroslav Kysela wrote:
> > 
> > > On Wed, 4 Oct 2006, Jiri Kosina wrote:
> > > 
> > > > I am getting kernel panic (NULL pointer dereference) on boot, with kernel 
> > > > compiled with CONFIG_SND_MPU401_UART=y, on machine which does not have 
> > > > this piece of hardware.
> > > > 
> > > > I have traced the problem down to 
> > > > sound/drivers/mpu401/mpu401.c:snd_mpu401_probe() returning EINVAL, when 
> > > > either port or IRQ parameters are not specified.
> > > > 
> > > > In such case, the drivers/base/bus.c:bus_attach_device() does not perform 
> > > > klist_add_tail() call, but rather sets dev->is_registered to 0. This flag 
> > > > is however not checked by the driver, so later on, when 
> > > > alsa_card_mpu401_init() is called and platform_device_register_simple() 
> > > > fails, the following callchain happens, causing NULL pointer dereference: 
> > > > alsa_card_mpu401_init() -> platform_device_unregister() -> 
> > > > platform_device_del() -> device_del() -> bus_remove_device() -> 
> > > > klist_del() -> BOOM (the entry was not added to klist in 
> > > > bus_attach_device()).
> > > > 
> > > > Proper solution is returning ENODEV from the ->probe() routine, which will 
> > > > be correctly handled then by the rest of the device-driver attaching 
> > > > subsystem (namely the retval check in bus_attach_device()). The following 
> > > > patch fixes the problem, please apply.
> > > 
> > > Unfortunately, I do not think that it's a proper solution. I think that
> > > platform device layer should play more nicely and if probe() fails for 
> > > a reason and if platform_device_register_simple() does not set 
> > > IS_ERR(), then platform_device_unregister() must be callable to free
> > > all resources.
> > > 
> > > Also, you've proposed to not fix all returns in snd_mpu401_probe() only 
> > > first two.
> > > 
> > > I would reject this patch and fix drivers/base/bus.c. The problematic 
> > > change is in commit f2eaae197f4590c4d96f31b09b0ee9067421a95c and this 
> > > patch will probably fix it:
> > 
> > Your patch is right as far as it goes, but it misses part of the problem.  
> > device_add() shouldn't ignore the return value from bus_attach_device().  
> > That's why we ended up trying to unregister a device which never was 
> > properly registered in the first place.
> > 
> > What do you think of this addition to your patch (untested)?
> 
> Your addition looks good. Acked from me.

Great, Alan, care to resend with a better description and the proper
signed-off-by lines?

thanks,

greg k-h

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH] Driver core: Don't ignore error returns from probing
  2006-10-05 17:58   ` Greg KH
@ 2006-10-05 21:03     ` Alan Stern
  2006-10-06  7:53       ` Cornelia Huck
  0 siblings, 1 reply; 13+ messages in thread
From: Alan Stern @ 2006-10-05 21:03 UTC (permalink / raw)
  To: Greg KH
  Cc: Andrew Morton, ALSA development, Takashi Iwai, LKML, Jiri Kosina,
	Castet Matthieu, Jaroslav Kysela

This patch (as797) fixes device_add() in the driver core.  It needs to
pay attention when the driver for a new device reports an error.

At the same time, since bus_remove_device() undoes the effects of both
bus_add_device() and bus_attach_device(), it needs to check whether
the bus_attach_device step failed.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>

---

On Thu, 5 Oct 2006, Greg KH wrote:

> Great, Alan, care to resend with a better description and the proper
> signed-off-by lines?

Here it is.  Mind you, I still haven't tested it.  And the way the code
was written originally makes me wonder if the probe error wasn't
deliberately ignored.  Although if it was deliberate, there ought to have
been a comment explaining things.

Anyway, the change to bus.c is exactly the same as Jaroslav's patch, and
it is obviously correct.


Index: 18g20/drivers/base/bus.c
===================================================================
--- 18g20.orig/drivers/base/bus.c
+++ 18g20/drivers/base/bus.c
@@ -453,8 +453,10 @@ void bus_remove_device(struct device * d
 		remove_deprecated_bus_links(dev);
 		sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
 		device_remove_attrs(dev->bus, dev);
-		dev->is_registered = 0;
-		klist_del(&dev->knode_bus);
+		if (dev->is_registered) {
+			dev->is_registered = 0;
+			klist_del(&dev->knode_bus);
+		}
 		pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
 		device_release_driver(dev);
 		put_bus(dev->bus);
Index: 18g20/drivers/base/core.c
===================================================================
--- 18g20.orig/drivers/base/core.c
+++ 18g20/drivers/base/core.c
@@ -485,7 +485,8 @@ int device_add(struct device *dev)
 	if ((error = bus_add_device(dev)))
 		goto BusError;
 	kobject_uevent(&dev->kobj, KOBJ_ADD);
-	bus_attach_device(dev);
+	if ((error = bus_attach_device(dev)))
+		goto AttachError;
 	if (parent)
 		klist_add_tail(&dev->knode_parent, &parent->klist_children);
 
@@ -504,6 +505,8 @@ int device_add(struct device *dev)
  	kfree(class_name);
 	put_device(dev);
 	return error;
+ AttachError:
+	bus_remove_device(dev);
  BusError:
 	device_pm_remove(dev);
  PMError:



-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Driver core: Don't ignore error returns from probing
  2006-10-05 21:03     ` [PATCH] Driver core: Don't ignore error returns from probing Alan Stern
@ 2006-10-06  7:53       ` Cornelia Huck
  2006-10-06  9:41         ` Jaroslav Kysela
  0 siblings, 1 reply; 13+ messages in thread
From: Cornelia Huck @ 2006-10-06  7:53 UTC (permalink / raw)
  To: Alan Stern
  Cc: Andrew Morton, ALSA development, Takashi Iwai, Greg KH, LKML,
	Jiri Kosina, Castet Matthieu, Jaroslav Kysela

On Thu, 5 Oct 2006 17:03:24 -0400 (EDT),
Alan Stern <stern@rowland.harvard.edu> wrote:

> Index: 18g20/drivers/base/bus.c
> ===================================================================
> --- 18g20.orig/drivers/base/bus.c
> +++ 18g20/drivers/base/bus.c
> @@ -453,8 +453,10 @@ void bus_remove_device(struct device * d
>  		remove_deprecated_bus_links(dev);
>  		sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
>  		device_remove_attrs(dev->bus, dev);
> -		dev->is_registered = 0;
> -		klist_del(&dev->knode_bus);
> +		if (dev->is_registered) {
> +			dev->is_registered = 0;
> +			klist_del(&dev->knode_bus);
> +		}
>  		pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
>  		device_release_driver(dev);
>  		put_bus(dev->bus);
> Index: 18g20/drivers/base/core.c
> ===================================================================
> --- 18g20.orig/drivers/base/core.c
> +++ 18g20/drivers/base/core.c
> @@ -485,7 +485,8 @@ int device_add(struct device *dev)
>  	if ((error = bus_add_device(dev)))
>  		goto BusError;
>  	kobject_uevent(&dev->kobj, KOBJ_ADD);
> -	bus_attach_device(dev);
> +	if ((error = bus_attach_device(dev)))
> +		goto AttachError;
>  	if (parent)
>  		klist_add_tail(&dev->knode_parent, &parent->klist_children);
>  
> @@ -504,6 +505,8 @@ int device_add(struct device *dev)
>   	kfree(class_name);
>  	put_device(dev);
>  	return error;
> + AttachError:
> +	bus_remove_device(dev);
>   BusError:
>  	device_pm_remove(dev);
>   PMError:

Hm, I don't think we should call device_release_driver if
bus_attach_device failed (and I think calling bus_remove_device if
bus_attach_device failed is unintuitive). I did a patch that added a
function which undid just the things bus_add_device did (here:
http://marc.theaimsgroup.com/?l=linux-kernel&m=115816560424389&w=2),
which unfortunately got lost somewhere... (I'll rebase and resend.)

-- 
Cornelia Huck
Linux for zSeries Developer
Tel.: +49-7031-16-4837, Mail: cornelia.huck@de.ibm.com

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Driver core: Don't ignore error returns from probing
  2006-10-06  7:53       ` Cornelia Huck
@ 2006-10-06  9:41         ` Jaroslav Kysela
  2006-10-06 11:14           ` Cornelia Huck
  0 siblings, 1 reply; 13+ messages in thread
From: Jaroslav Kysela @ 2006-10-06  9:41 UTC (permalink / raw)
  To: Cornelia Huck
  Cc: Andrew Morton, ALSA development, Takashi Iwai, Greg KH, LKML,
	Jiri Kosina, Alan Stern, Castet Matthieu

On Fri, 6 Oct 2006, Cornelia Huck wrote:

> On Thu, 5 Oct 2006 17:03:24 -0400 (EDT),
> Alan Stern <stern@rowland.harvard.edu> wrote:
> 
> > Index: 18g20/drivers/base/bus.c
> > ===================================================================
> > --- 18g20.orig/drivers/base/bus.c
> > +++ 18g20/drivers/base/bus.c
> > @@ -453,8 +453,10 @@ void bus_remove_device(struct device * d
> >  		remove_deprecated_bus_links(dev);
> >  		sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
> >  		device_remove_attrs(dev->bus, dev);
> > -		dev->is_registered = 0;
> > -		klist_del(&dev->knode_bus);
> > +		if (dev->is_registered) {
> > +			dev->is_registered = 0;
> > +			klist_del(&dev->knode_bus);
> > +		}
> >  		pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
> >  		device_release_driver(dev);
> >  		put_bus(dev->bus);
> > Index: 18g20/drivers/base/core.c
> > ===================================================================
> > --- 18g20.orig/drivers/base/core.c
> > +++ 18g20/drivers/base/core.c
> > @@ -485,7 +485,8 @@ int device_add(struct device *dev)
> >  	if ((error = bus_add_device(dev)))
> >  		goto BusError;
> >  	kobject_uevent(&dev->kobj, KOBJ_ADD);
> > -	bus_attach_device(dev);
> > +	if ((error = bus_attach_device(dev)))
> > +		goto AttachError;
> >  	if (parent)
> >  		klist_add_tail(&dev->knode_parent, &parent->klist_children);
> >  
> > @@ -504,6 +505,8 @@ int device_add(struct device *dev)
> >   	kfree(class_name);
> >  	put_device(dev);
> >  	return error;
> > + AttachError:
> > +	bus_remove_device(dev);
> >   BusError:
> >  	device_pm_remove(dev);
> >   PMError:
> 
> Hm, I don't think we should call device_release_driver if
> bus_attach_device failed (and I think calling bus_remove_device if
> bus_attach_device failed is unintuitive). I did a patch that added a
> function which undid just the things bus_add_device did (here:
> http://marc.theaimsgroup.com/?l=linux-kernel&m=115816560424389&w=2),
> which unfortunately got lost somewhere... (I'll rebase and resend.)

Yes, but it might be better to check dev->is_registered flag in 
bus_remove_device() before device_release_driver() call to save some code, 
rather than reuse most of code in bus_delete_device().

						Jaroslav

-----
Jaroslav Kysela <perex@suse.cz>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Driver core: Don't ignore error returns from probing
  2006-10-06  9:41         ` Jaroslav Kysela
@ 2006-10-06 11:14           ` Cornelia Huck
  2006-10-06 11:46             ` Jaroslav Kysela
  2006-10-06 18:12             ` Alan Stern
  0 siblings, 2 replies; 13+ messages in thread
From: Cornelia Huck @ 2006-10-06 11:14 UTC (permalink / raw)
  To: Jaroslav Kysela
  Cc: Andrew Morton, ALSA development, Takashi Iwai, Greg KH, LKML,
	Jiri Kosina, Alan Stern, Castet Matthieu

On Fri, 6 Oct 2006 11:41:05 +0200 (CEST),
Jaroslav Kysela <perex@suse.cz> wrote:

> > Hm, I don't think we should call device_release_driver if
> > bus_attach_device failed (and I think calling bus_remove_device if
> > bus_attach_device failed is unintuitive). I did a patch that added a
> > function which undid just the things bus_add_device did (here:
> > http://marc.theaimsgroup.com/?l=linux-kernel&m=115816560424389&w=2),
> > which unfortunately got lost somewhere... (I'll rebase and resend.)
> 
> Yes, but it might be better to check dev->is_registered flag in 
> bus_remove_device() before device_release_driver() call to save some code, 
> rather than reuse most of code in bus_delete_device().

If we undid things (symlinks et al.) in the order we added them, we can
factor out bus_delete_device() from bus_remove_device() and avoid both
code duplication and calling bus_remove_device() if bus_attach_device()
failed. Something like the patch below (untested).


--- linux-2.6-CH.orig/drivers/base/base.h
+++ linux-2.6-CH/drivers/base/base.h
@@ -17,6 +17,7 @@ extern int attribute_container_init(void
 
 extern int bus_add_device(struct device * dev);
 extern int bus_attach_device(struct device * dev);
+extern void bus_delete_device(struct device * dev);
 extern void bus_remove_device(struct device * dev);
 extern struct bus_type *get_bus(struct bus_type * bus);
 extern void put_bus(struct bus_type * bus);
--- linux-2.6-CH.orig/drivers/base/bus.c
+++ linux-2.6-CH/drivers/base/bus.c
@@ -360,7 +360,7 @@ static void device_remove_attrs(struct b
  *	bus_add_device - add device to bus
  *	@dev:	device being added
  *
- *	- Add the device to its bus's list of devices.
+ *	- Add attributes.
  *	- Create link to device's bus.
  */
 int bus_add_device(struct device * dev)
@@ -424,29 +424,44 @@ int bus_attach_device(struct device * de
 }
 
 /**
- *	bus_remove_device - remove device from bus
- *	@dev:	device to be removed
+ *     bus_delete_device - undo bus_add_device
+ *     @dev:   device being deleted
  *
- *	- Remove symlink from bus's directory.
- *	- Delete device from bus's list.
- *	- Detach from its driver.
- *	- Drop reference taken in bus_add_device().
+ *     - Remove symlink from bus's directory.
+ *     - Remove attributes.
+ *     - Drop reference taken in bus_add_device().
  */
-void bus_remove_device(struct device * dev)
+void bus_delete_device(struct device * dev)
 {
 	if (dev->bus) {
 		sysfs_remove_link(&dev->kobj, "subsystem");
 		sysfs_remove_link(&dev->kobj, "bus");
 		sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
 		device_remove_attrs(dev->bus, dev);
-		dev->is_registered = 0;
-		klist_del(&dev->knode_bus);
-		pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
-		device_release_driver(dev);
 		put_bus(dev->bus);
 	}
 }
 
+/**
+ *	bus_remove_device - remove device from bus
+ *	@dev:	device to be removed
+ *
+ *	- Remove symlink from bus's directory.
+ *	- Delete device from bus's list.
+ *	- Detach from its driver.
+ *	- Drop reference taken in bus_add_device().
+ */
+void bus_remove_device(struct device * dev)
+{
+	if (!dev->bus)
+		return;
+	dev->is_registered = 0;
+	klist_del(&dev->knode_bus);
+	pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
+	device_release_driver(dev);
+	bus_delete_device(dev);
+}
+
 static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
 {
 	int error = 0;
--- linux-2.6-CH.orig/drivers/base/core.c
+++ linux-2.6-CH/drivers/base/core.c
@@ -479,7 +479,9 @@ int device_add(struct device *dev)
 	if ((error = bus_add_device(dev)))
 		goto BusError;
 	kobject_uevent(&dev->kobj, KOBJ_ADD);
-	bus_attach_device(dev);
+	error = bus_attach_device(dev);
+	if (error)
+		goto attachError;
 	if (parent)
 		klist_add_tail(&dev->knode_parent, &parent->klist_children);
 
@@ -498,6 +500,8 @@ int device_add(struct device *dev)
  	kfree(class_name);
 	put_device(dev);
 	return error;
+ attachError:
+	bus_delete_device(dev);
  BusError:
 	device_pm_remove(dev);
  PMError:

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Driver core: Don't ignore error returns from probing
  2006-10-06 11:14           ` Cornelia Huck
@ 2006-10-06 11:46             ` Jaroslav Kysela
  2006-10-06 18:12             ` Alan Stern
  1 sibling, 0 replies; 13+ messages in thread
From: Jaroslav Kysela @ 2006-10-06 11:46 UTC (permalink / raw)
  To: Cornelia Huck
  Cc: Andrew Morton, ALSA development, Takashi Iwai, Greg KH, LKML,
	Jiri Kosina, Alan Stern, Castet Matthieu

On Fri, 6 Oct 2006, Cornelia Huck wrote:

> On Fri, 6 Oct 2006 11:41:05 +0200 (CEST),
> Jaroslav Kysela <perex@suse.cz> wrote:
> 
> > > Hm, I don't think we should call device_release_driver if
> > > bus_attach_device failed (and I think calling bus_remove_device if
> > > bus_attach_device failed is unintuitive). I did a patch that added a
> > > function which undid just the things bus_add_device did (here:
> > > http://marc.theaimsgroup.com/?l=linux-kernel&m=115816560424389&w=2),
> > > which unfortunately got lost somewhere... (I'll rebase and resend.)
> > 
> > Yes, but it might be better to check dev->is_registered flag in 
> > bus_remove_device() before device_release_driver() call to save some code, 
> > rather than reuse most of code in bus_delete_device().
> 
> If we undid things (symlinks et al.) in the order we added them, we can
> factor out bus_delete_device() from bus_remove_device() and avoid both
> code duplication and calling bus_remove_device() if bus_attach_device()
> failed. Something like the patch below (untested).

It looks better, but I think that having only one function with if 
(is_registered) saves a few bytes of instruction memory. Anyway, I do not 
feel myself to judge what's the best.

						Jaroslav

-----
Jaroslav Kysela <perex@suse.cz>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Driver core: Don't ignore error returns from probing
  2006-10-06 11:14           ` Cornelia Huck
  2006-10-06 11:46             ` Jaroslav Kysela
@ 2006-10-06 18:12             ` Alan Stern
  2006-10-09 11:10               ` Cornelia Huck
  1 sibling, 1 reply; 13+ messages in thread
From: Alan Stern @ 2006-10-06 18:12 UTC (permalink / raw)
  To: Cornelia Huck
  Cc: Andrew Morton, ALSA development, Takashi Iwai, Greg KH, LKML,
	Jiri Kosina, Castet Matthieu, Jaroslav Kysela

On Fri, 6 Oct 2006, Cornelia Huck wrote:

> On Fri, 6 Oct 2006 11:41:05 +0200 (CEST),
> Jaroslav Kysela <perex@suse.cz> wrote:
> 
> > > Hm, I don't think we should call device_release_driver if
> > > bus_attach_device failed (and I think calling bus_remove_device if
> > > bus_attach_device failed is unintuitive). I did a patch that added a
> > > function which undid just the things bus_add_device did (here:
> > > http://marc.theaimsgroup.com/?l=linux-kernel&m=115816560424389&w=2),
> > > which unfortunately got lost somewhere... (I'll rebase and resend.)

I'm still not sure why bus_attach_device() was split off from 
bus_add_device() in the first place.  Was it just so that the 
kobject_uevent() call could go in between?

Is there any reason they couldn't be recombined into a single function?

> > Yes, but it might be better to check dev->is_registered flag in 
> > bus_remove_device() before device_release_driver() call to save some code, 
> > rather than reuse most of code in bus_delete_device().

Agreed; I don't like the duplication of code.  It's wasteful and 
error-prone (somebody might change one routine and not the other).

> If we undid things (symlinks et al.) in the order we added them, we can
> factor out bus_delete_device() from bus_remove_device() and avoid both
> code duplication and calling bus_remove_device() if bus_attach_device()
> failed. Something like the patch below (untested).

This looks okay, but it would be better if bus_remove_device() did not
directly call bus_delete_device().  Just add an extra call inside
device_del(), so that everything remains symmetrical.

While I'm harping on style issues, you should also capitalize AttachError
so that it matches the form of the other statement labels nearby.  And in
bus_remove_device() you should put all the code inside the "if" block
instead of returning when dev->bus isn't set, just as the neighboring
subroutines do.

Alan Stern


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Driver core: Don't ignore error returns from probing
  2006-10-06 18:12             ` Alan Stern
@ 2006-10-09 11:10               ` Cornelia Huck
  0 siblings, 0 replies; 13+ messages in thread
From: Cornelia Huck @ 2006-10-09 11:10 UTC (permalink / raw)
  To: Alan Stern
  Cc: Andrew Morton, ALSA development, Takashi Iwai, Greg KH, LKML,
	Jiri Kosina, Castet Matthieu, Jaroslav Kysela

On Fri, 6 Oct 2006 14:12:49 -0400 (EDT),
Alan Stern <stern@rowland.harvard.edu> wrote:

> I'm still not sure why bus_attach_device() was split off from 
> bus_add_device() in the first place.  Was it just so that the 
> kobject_uevent() call could go in between?

I think yes. This was added in
http://marc.theaimsgroup.com/?l=linux-kernel&m=115092084915731&w=2

> This looks okay, but it would be better if bus_remove_device() did not
> directly call bus_delete_device().  Just add an extra call inside
> device_del(), so that everything remains symmetrical.
> 
> While I'm harping on style issues, you should also capitalize AttachError
> so that it matches the form of the other statement labels nearby.  And in
> bus_remove_device() you should put all the code inside the "if" block
> instead of returning when dev->bus isn't set, just as the neighboring
> subroutines do.

OK, new patch on the way.

-- 
Cornelia Huck
Linux for zSeries Developer
Tel.: +49-7031-16-4837, Mail: cornelia.huck@de.ibm.com

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2006-10-09 11:10 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <Pine.LNX.4.44L0.0610050959330.6226-100000@iolanthe.rowland.org>
2006-10-05 14:16 ` [PATCH] ALSA: fix kernel panic in initialization of mpu401 driver Jaroslav Kysela
     [not found] ` <Pine.LNX.4.61.0610051614220.9351@tm8103.perex-int.cz>
2006-10-05 17:58   ` Greg KH
2006-10-05 21:03     ` [PATCH] Driver core: Don't ignore error returns from probing Alan Stern
2006-10-06  7:53       ` Cornelia Huck
2006-10-06  9:41         ` Jaroslav Kysela
2006-10-06 11:14           ` Cornelia Huck
2006-10-06 11:46             ` Jaroslav Kysela
2006-10-06 18:12             ` Alan Stern
2006-10-09 11:10               ` Cornelia Huck
2006-10-04 20:22 [PATCH] ALSA: fix kernel panic in initialization of mpu401 driver Jiri Kosina
2006-10-05  8:07 ` Jaroslav Kysela
2006-10-05  9:57   ` Jiri Kosina
2006-10-05 14:02   ` Alan Stern

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox