Skip to contentSkip to conversation

get_device in device_create_file

DISCUSSION Pete Zaitcev 3 participants 4 messages Aug 1, 2006 – Aug 3, 2006

lore
Share & tools
lore
#0 lore raw

Hi, Greg:

This code makes no sense to me:

Quoted 9 lines

int device_create_file(struct device * dev, struct device_attribute * attr)

{
	int error = 0;
	if (get_device(dev)) {
		error = sysfs_create_file(&dev->kobj, &attr->attr);
		put_device(dev);
	}
	return error;
}

If the struct device *dev, and its presumably enclosing structure, can be freed by a different CPU (or pre-empt), then get_device does not protect it. It can be freed before get_device is reached. Buf it not, and the caller has a reference, then the call to get_device is redundant.

How is this supposed to work?

-- Pete

#1 lore raw
Quoted 14 lines · Pete Zaitcev

Hi, Greg:

This code makes no sense to me:

Quoted 9 lines

int device_create_file(struct device * dev, struct device_attribute * attr)

{
	int error = 0;
	if (get_device(dev)) {
		error = sysfs_create_file(&dev->kobj, &attr->attr);
		put_device(dev);
	}
	return error;
}
Quoted 3 lines

If the struct device *dev, and its presumably enclosing structure, can be freed by a different CPU (or pre-empt), then get_device does not protect it. It can be freed before get_device is reached.

*nod*

Quoted 3 lines

Buf it not, and the caller has a reference, then the call to get_device is redundant.

Yes it is. There are few of redundant gets and puts sprinkled around in the driver core, but the last time I mentioned that Greg was not quite ready to get rid of them ;)

Signature · 1 line
Dmitry
#2 lore raw
Quoted 11 lines · Dmitry Torokhov
Quoted 7 lines
Quoted 4 lines
	if (get_device(dev)) {
		error = sysfs_create_file(&dev->kobj, &attr->attr);
		put_device(dev);
	}

Buf it not, and the caller has a reference, then the call to get_device is redundant.

Yes it is. There are few of redundant gets and puts sprinkled around in the driver core, but the last time I mentioned that Greg was not quite ready to get rid of them ;)

I see a small, but nonzero harm from keeping them, for two reasons. One, I reviewed a patch for RHEL-4 today, where the submitter copied this code without thinking. So, if we don't flush these, they will proliferate. Hackers assume that if Greg wrote that, it must be the right way. Two, these calls shrink the race window and may be masking something.

-- Pete

#3 lore raw
In reply to Pete Zaitcev
Quoted 14 lines · Pete Zaitcev

Hi, Greg:

This code makes no sense to me:

Quoted 9 lines

int device_create_file(struct device * dev, struct device_attribute * attr)

{
	int error = 0;
	if (get_device(dev)) {
		error = sysfs_create_file(&dev->kobj, &attr->attr);
		put_device(dev);
	}
	return error;
}
Quoted 5 lines

If the struct device *dev, and its presumably enclosing structure, can be freed by a different CPU (or pre-empt), then get_device does not protect it. It can be freed before get_device is reached. Buf it not, and the caller has a reference, then the call to get_device is redundant.

Yes, it is redundant, sorry. I know there are a few places that we gratuitously grab references in the core that we don't really need to do so.

It's interesting that someone would cut-and-paste from a driver core file into something new. What kind of code did they do that for?

Anyway, patches to clean this kind of stuff up is gladly accepted.

thanks,

greg k-h