diff mbox

pci: keep pci device resource name pointer right.

Message ID 1240086446.2411.9.camel@poy (mailing list archive)
State Accepted, archived
Headers show

Commit Message

Kay Sievers April 18, 2009, 8:27 p.m. UTC
On Sat, 2009-04-18 at 22:00 +0200, Kay Sievers wrote:
> On Sat, Apr 18, 2009 at 21:23, Greg KH <gregkh@suse.de> wrote:
> > On Sat, Apr 18, 2009 at 12:19:21PM -0700, Yinghai Lu wrote:
> >> Linus Torvalds wrote:
> 
> >> > Can we not make the rule be that the name should just be set before?
> 
> I guess, that's what they do already, otherwise they could not use dev_name().
> 
> >> pci_bus_add_device will call device_add.
> >>
> >> actually in device_add
> >>
> >>         /* first, register with generic layer. */
> >>         error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));
> >>         if (error)
> >>                 goto Error;
> >>
> >> will get one new name for that kobj, old name is freed.
> >>
> >> Will try to make kobject_add more smart to reuse the old one.
> >
> > I don't understand the problem here, how are you going to change the
> > kobject core?  Is this just because you aren't getting a name for the
> > resource?  If so, why would the driver core care about this?
> 
> Seems a bit odd what we do when registering a device.
> 
> Usually one sets the name with dev_set_name() which will name the
> kobject. When one later registers the device, we reallocate the same
> name, so the pointer changes. I guess the problem here is that the pci
> code remembers the name pointer after it was set, but it will not be
> the same after the device is live.
> 
> We should probably make device_add() pass a NULL as the name, and
> kobject_add() in that case use the name that is properly set already.

Only to see if we are on the right track, does that fix the problem? If
yes, we will fix it properly without the NULL hack deep in the kobject
call.

Thanks,
Kay



--
To unsubscribe from this list: send the line "unsubscribe linux-pci" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Ingo Molnar April 18, 2009, 8:37 p.m. UTC | #1
* Kay Sievers <kay.sievers@vrfy.org> wrote:

> On Sat, 2009-04-18 at 22:00 +0200, Kay Sievers wrote:
> > On Sat, Apr 18, 2009 at 21:23, Greg KH <gregkh@suse.de> wrote:
> > > On Sat, Apr 18, 2009 at 12:19:21PM -0700, Yinghai Lu wrote:
> > >> Linus Torvalds wrote:
> > 
> > >> > Can we not make the rule be that the name should just be set before?
> > 
> > I guess, that's what they do already, otherwise they could not use dev_name().
> > 
> > >> pci_bus_add_device will call device_add.
> > >>
> > >> actually in device_add
> > >>
> > >>         /* first, register with generic layer. */
> > >>         error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));
> > >>         if (error)
> > >>                 goto Error;
> > >>
> > >> will get one new name for that kobj, old name is freed.
> > >>
> > >> Will try to make kobject_add more smart to reuse the old one.
> > >
> > > I don't understand the problem here, how are you going to change the
> > > kobject core?  Is this just because you aren't getting a name for the
> > > resource?  If so, why would the driver core care about this?
> > 
> > Seems a bit odd what we do when registering a device.
> > 
> > Usually one sets the name with dev_set_name() which will name the
> > kobject. When one later registers the device, we reallocate the same
> > name, so the pointer changes. I guess the problem here is that the pci
> > code remembers the name pointer after it was set, but it will not be
> > the same after the device is live.
> > 
> > We should probably make device_add() pass a NULL as the name, and
> > kobject_add() in that case use the name that is properly set already.
> 
> Only to see if we are on the right track, does that fix the problem? If
> yes, we will fix it properly without the NULL hack deep in the kobject
> call.
> 
> Thanks,
> Kay
> 
> diff --git a/drivers/base/core.c b/drivers/base/core.c
> index d230ff4..1969b20 100644
> --- a/drivers/base/core.c
> +++ b/drivers/base/core.c
> @@ -890,8 +890,8 @@ int device_add(struct device *dev)
>  	if (parent)
>  		set_dev_node(dev, dev_to_node(parent));
>  
> -	/* first, register with generic layer. */
> -	error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));
> +	/* we require the name to be set before, and pass NULL */
> +	error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
>  	if (error)
>  		goto Error;
>  
> diff --git a/lib/kobject.c b/lib/kobject.c
> index a6dec32..48565d6 100644
> --- a/lib/kobject.c
> +++ b/lib/kobject.c
> @@ -218,6 +218,9 @@ int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
>  	const char *old_name = kobj->name;
>  	char *s;
>  
> +	if (kobj->name && !fmt)
> +		return 0;
> +
>  	kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
>  	if (!kobj->name)
>  		return -ENOMEM;

This looks much cleaner to me - a NULL format string looks like a 
natural extension.

	Ingo
--
To unsubscribe from this list: send the line "unsubscribe linux-pci" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Yinghai Lu April 18, 2009, 9:07 p.m. UTC | #2
Kay Sievers wrote:
> On Sat, 2009-04-18 at 22:00 +0200, Kay Sievers wrote:
>> On Sat, Apr 18, 2009 at 21:23, Greg KH <gregkh@suse.de> wrote:
>>> On Sat, Apr 18, 2009 at 12:19:21PM -0700, Yinghai Lu wrote:
>>>> Linus Torvalds wrote:
>>>>> Can we not make the rule be that the name should just be set before?
>> I guess, that's what they do already, otherwise they could not use dev_name().
>>
>>>> pci_bus_add_device will call device_add.
>>>>
>>>> actually in device_add
>>>>
>>>>         /* first, register with generic layer. */
>>>>         error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));
>>>>         if (error)
>>>>                 goto Error;
>>>>
>>>> will get one new name for that kobj, old name is freed.
>>>>
>>>> Will try to make kobject_add more smart to reuse the old one.
>>> I don't understand the problem here, how are you going to change the
>>> kobject core?  Is this just because you aren't getting a name for the
>>> resource?  If so, why would the driver core care about this?
>> Seems a bit odd what we do when registering a device.
>>
>> Usually one sets the name with dev_set_name() which will name the
>> kobject. When one later registers the device, we reallocate the same
>> name, so the pointer changes. I guess the problem here is that the pci
>> code remembers the name pointer after it was set, but it will not be
>> the same after the device is live.
>>
>> We should probably make device_add() pass a NULL as the name, and
>> kobject_add() in that case use the name that is properly set already.
> 
> Only to see if we are on the right track, does that fix the problem? If
> yes, we will fix it properly without the NULL hack deep in the kobject
> call.
> 
> Thanks,
> Kay
> 
> diff --git a/drivers/base/core.c b/drivers/base/core.c
> index d230ff4..1969b20 100644
> --- a/drivers/base/core.c
> +++ b/drivers/base/core.c
> @@ -890,8 +890,8 @@ int device_add(struct device *dev)
>  	if (parent)
>  		set_dev_node(dev, dev_to_node(parent));
>  
> -	/* first, register with generic layer. */
> -	error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));
> +	/* we require the name to be set before, and pass NULL */
> +	error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
>  	if (error)
>  		goto Error;
>  
> diff --git a/lib/kobject.c b/lib/kobject.c
> index a6dec32..48565d6 100644
> --- a/lib/kobject.c
> +++ b/lib/kobject.c
> @@ -218,6 +218,9 @@ int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
>  	const char *old_name = kobj->name;
>  	char *s;
>  
> +	if (kobj->name && !fmt)
> +		return 0;
> +
>  	kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
>  	if (!kobj->name)
>  		return -ENOMEM;
> 

it works well. Thanks

YH


--
To unsubscribe from this list: send the line "unsubscribe linux-pci" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Linus Torvalds April 18, 2009, 10:17 p.m. UTC | #3
On Sat, 18 Apr 2009, Kay Sievers wrote:
> 
> Only to see if we are on the right track, does that fix the problem? If
> yes, we will fix it properly without the NULL hack deep in the kobject
> call.

ACK, this patch looks much saner, and would seem to address the core 
issue.

		Linus
--
To unsubscribe from this list: send the line "unsubscribe linux-pci" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/base/core.c b/drivers/base/core.c
index d230ff4..1969b20 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -890,8 +890,8 @@  int device_add(struct device *dev)
 	if (parent)
 		set_dev_node(dev, dev_to_node(parent));
 
-	/* first, register with generic layer. */
-	error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));
+	/* we require the name to be set before, and pass NULL */
+	error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
 	if (error)
 		goto Error;
 
diff --git a/lib/kobject.c b/lib/kobject.c
index a6dec32..48565d6 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -218,6 +218,9 @@  int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
 	const char *old_name = kobj->name;
 	char *s;
 
+	if (kobj->name && !fmt)
+		return 0;
+
 	kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
 	if (!kobj->name)
 		return -ENOMEM;