Patchworkβ [v3,1/6] ACPI: dock: convert sysfs attributes to an attribute_group

login
register
about
Submitter Alexander Chiang
Date 2009-10-16 21:14:59
Message ID <20091016211459.12126.60739.stgit@bob.kio>
Download mbox | patch
Permalink /patch/54427/
State New
Headers show

Comments

Alexander Chiang - 2009-10-16 21:14:59
As suggested by Dmitry Torokhov, convert the individual sysfs
attributes into an attribute group.

This change eliminates quite a bit of copy/paste code in the
error handling paths.

Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Signed-off-by: Alex Chiang <achiang@hp.com>
---

 drivers/acpi/dock.c |   80 +++++++++++++++------------------------------------
 1 files changed, 23 insertions(+), 57 deletions(-)


--
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Dmitry Torokhov - 2009-10-18 07:16:30
Hi Alex,

On Fri, Oct 16, 2009 at 03:14:59PM -0600, Alex Chiang wrote:
> As suggested by Dmitry Torokhov, convert the individual sysfs
> attributes into an attribute group.
> 
> This change eliminates quite a bit of copy/paste code in the
> error handling paths.
> 

Looks much better, one more suggestion though:

> +err_unregister:
> +	printk(KERN_ERR "%s encountered error %d\n", __func__, ret);

If you want to print error this it should probably go down, right before
"return ret".

> +	sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);

It begs another label right here. There are cases when yo0u already
registered the platform device but haven't added the sysfs group, right?

>  	platform_device_unregister(dock_device);
> +out:
>  	kfree(dock_station);
>  	dock_station = NULL;
>  	return ret;
> @@ -1076,11 +1046,7 @@ static int dock_remove(struct dock_station *dock_station)
>  	    kfree(dd);
>  
>  	/* cleanup sysfs */
> -	device_remove_file(&dock_device->dev, &dev_attr_type);
> -	device_remove_file(&dock_device->dev, &dev_attr_docked);
> -	device_remove_file(&dock_device->dev, &dev_attr_undock);
> -	device_remove_file(&dock_device->dev, &dev_attr_uid);
> -	device_remove_file(&dock_device->dev, &dev_attr_flags);
> +	sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
>  	platform_device_unregister(dock_device);
>  
>  	/* free dock station memory */
>
Alexander Chiang - 2009-10-19 17:21:38
Hi Dmitry,

* Dmitry Torokhov <dmitry.torokhov@gmail.com>:
> On Fri, Oct 16, 2009 at 03:14:59PM -0600, Alex Chiang wrote:
> > As suggested by Dmitry Torokhov, convert the individual sysfs
> > attributes into an attribute group.
> > 
> > This change eliminates quite a bit of copy/paste code in the
> > error handling paths.
> > 
> 
> Looks much better, one more suggestion though:
> 
> > +err_unregister:
> > +	printk(KERN_ERR "%s encountered error %d\n", __func__, ret);
> 
> If you want to print error this it should probably go down, right before
> "return ret".

This is true for this patch, 1/6... but by the end of the series,
the problem has resolved itself.

I agree that it's sloppy to have this bit of inconsistency in the
middle of the patch series, but I'm reluctant to spin the entire
series again, for sake of a printk.

> > +	sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
> 
> It begs another label right here. There are cases when yo0u already
> registered the platform device but haven't added the sysfs group, right?

This isn't quite true. In this patch, 1/6, our sequence goes:

	platform_device_register_simple()
	platform_device_add_data()
	/* twiddle some state in the platform device, no error paths though */
	sysfs_create_group()

Arguably, the platform_device_add_data() call could fail with
-ENOMEM, but the code today doesn't deal with that error
condition, and I didn't touch the platform_device_add_data()
line.

So really, there are no other exit paths between registering the
platform device and adding the sysfs group.

By the end of the patch series, I combine the _register_simple()
call with the _add_data() call and the final sequence looks like
this:

	if (platform_device_register_data() == error)
		return error;

	/* twiddle local state in platform device */

	if (sysfs_create_group())
		goto err_unregister;

	/* other stuff */

	err_unregister:
		printk(KERN_ERR "%s encountered error %d\n", __func__, ret);
		sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
		platform_device_unregister(dd);
		return ret;

Checking other callsites of sysfs_remove_group(), it seems to be
valid to call that API even if the creation step failed.

Basically, I don't see the necessity of adding another label.

Below is the final end state of dock_add(). Hopefully the code is
a lot clearer than before. If there are still semantic issues,
please let me know and I'll happily respin.

Thanks.

/ac

static int dock_add(acpi_handle handle)
{
	int ret, id;
	struct dock_station ds, *dock_station;
	struct platform_device *dd;

	id = dock_station_count;
	dd = platform_device_register_data(NULL, "dock", id, &ds, sizeof(ds));
	if (IS_ERR(dd))
		return PTR_ERR(dd);

	dock_station = dd->dev.platform_data;

	dock_station->handle = handle;
	dock_station->dock_device = dd;
	dock_station->last_dock_time = jiffies - HZ;

	mutex_init(&dock_station->hp_lock);
	spin_lock_init(&dock_station->dd_lock);
	INIT_LIST_HEAD(&dock_station->sibling);
	INIT_LIST_HEAD(&dock_station->hotplug_devices);
	ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
	INIT_LIST_HEAD(&dock_station->dependent_devices);

	/* we want the dock device to send uevents */
	dev_set_uevent_suppress(&dd->dev, 0);

	if (is_dock(handle))
		dock_station->flags |= DOCK_IS_DOCK;
	if (is_ata(handle))
		dock_station->flags |= DOCK_IS_ATA;
	if (is_battery(handle))
		dock_station->flags |= DOCK_IS_BAT;

	ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
	if (ret)
		goto err_unregister;

	/* Find dependent devices */
	acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
			    find_dock_devices, dock_station, NULL);

	/* add the dock station as a device dependent on itself */
	ret = add_dock_dependent_device(dock_station, handle);
	if (ret)
		goto err_unregister;

	dock_station_count++;
	list_add(&dock_station->sibling, &dock_stations);
	return 0;

err_unregister:
	printk(KERN_ERR "%s encountered error %d\n", __func__, ret);
	sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
	platform_device_unregister(dd);
	return ret;
}
--
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Dmitry Torokhov - 2009-10-19 17:56:17
On Mon, Oct 19, 2009 at 11:21:38AM -0600, Alex Chiang wrote:
> Hi Dmitry,
> 
> * Dmitry Torokhov <dmitry.torokhov@gmail.com>:
> > On Fri, Oct 16, 2009 at 03:14:59PM -0600, Alex Chiang wrote:
> > > As suggested by Dmitry Torokhov, convert the individual sysfs
> > > attributes into an attribute group.
> > > 
> > > This change eliminates quite a bit of copy/paste code in the
> > > error handling paths.
> > > 
> > 
> > Looks much better, one more suggestion though:
> > 
> > > +err_unregister:
> > > +	printk(KERN_ERR "%s encountered error %d\n", __func__, ret);
> > 
> > If you want to print error this it should probably go down, right before
> > "return ret".
> 
> This is true for this patch, 1/6... but by the end of the series,
> the problem has resolved itself.
> 
> I agree that it's sloppy to have this bit of inconsistency in the
> middle of the patch series, but I'm reluctant to spin the entire
> series again, for sake of a printk.
> 
> > > +	sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
> > 
> > It begs another label right here. There are cases when yo0u already
> > registered the platform device but haven't added the sysfs group, right?
> 
> This isn't quite true. In this patch, 1/6, our sequence goes:
> 
> 	platform_device_register_simple()
> 	platform_device_add_data()
> 	/* twiddle some state in the platform device, no error paths though */
> 	sysfs_create_group()
> 
> Arguably, the platform_device_add_data() call could fail with
> -ENOMEM, but the code today doesn't deal with that error
> condition, and I didn't touch the platform_device_add_data()
> line.
> 
> So really, there are no other exit paths between registering the
> platform device and adding the sysfs group.
>

If sysfs_create_group() fails you will go to err_unregister: which will
try to remove the non-existing group. While the current sysfs code is
relsilient against such errors it may not be so in the future. It is
better to have a separate label and bypass sysfs_remove_group() if
sysfs_create_group() returned error.

> By the end of the patch series, I combine the _register_simple()
> call with the _add_data() call and the final sequence looks like
> this:
> 
> 	if (platform_device_register_data() == error)
> 		return error;
> 
> 	/* twiddle local state in platform device */
> 
> 	if (sysfs_create_group())
> 		goto err_unregister;
> 
> 	/* other stuff */
> 
> 	err_unregister:
> 		printk(KERN_ERR "%s encountered error %d\n", __func__, ret);
> 		sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
> 		platform_device_unregister(dd);
> 		return ret;
>
Alexander Chiang - 2009-10-19 19:36:15
* Dmitry Torokhov <dmitry.torokhov@gmail.com>:
> 
> If sysfs_create_group() fails you will go to err_unregister:
> which will try to remove the non-existing group. While the
> current sysfs code is relsilient against such errors it may not
> be so in the future. It is better to have a separate label and
> bypass sysfs_remove_group() if sysfs_create_group() returned
> error.

Yeah, you're right. I was trying to be lazy (everyone else is
doing it!), but I guess you won't let me. ;)

I'll respin.

Thanks for the review.

/ac

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

Patch

diff --git a/drivers/acpi/dock.c b/drivers/acpi/dock.c
index 7338b6a..3f71f0a 100644
--- a/drivers/acpi/dock.c
+++ b/drivers/acpi/dock.c
@@ -936,6 +936,19 @@  static ssize_t show_dock_type(struct device *dev,
 }
 static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
 
+static struct attribute *dock_attributes[] = {
+	&dev_attr_docked.attr,
+	&dev_attr_flags.attr,
+	&dev_attr_undock.attr,
+	&dev_attr_uid.attr,
+	&dev_attr_type.attr,
+	NULL
+};
+
+static struct attribute_group dock_attribute_group = {
+	.attrs = dock_attributes
+};
+
 /**
  * dock_add - add a new dock station
  * @handle: the dock station handle
@@ -969,9 +982,8 @@  static int dock_add(acpi_handle handle)
 			dock_station_count, NULL, 0);
 	dock_device = dock_station->dock_device;
 	if (IS_ERR(dock_device)) {
-		kfree(dock_station);
-		dock_station = NULL;
-		return PTR_ERR(dock_device);
+		ret = PTR_ERR(dock_device);
+		goto out;
 	}
 	platform_device_add_data(dock_device, &dock_station,
 		sizeof(struct dock_station *));
@@ -986,47 +998,9 @@  static int dock_add(acpi_handle handle)
 	if (is_battery(handle))
 		dock_station->flags |= DOCK_IS_BAT;
 
-	ret = device_create_file(&dock_device->dev, &dev_attr_docked);
-	if (ret) {
-		printk(KERN_ERR "Error %d adding sysfs file\n", ret);
-		platform_device_unregister(dock_device);
-		kfree(dock_station);
-		dock_station = NULL;
-		return ret;
-	}
-	ret = device_create_file(&dock_device->dev, &dev_attr_undock);
-	if (ret) {
-		printk(KERN_ERR "Error %d adding sysfs file\n", ret);
-		device_remove_file(&dock_device->dev, &dev_attr_docked);
-		platform_device_unregister(dock_device);
-		kfree(dock_station);
-		dock_station = NULL;
-		return ret;
-	}
-	ret = device_create_file(&dock_device->dev, &dev_attr_uid);
-	if (ret) {
-		printk(KERN_ERR "Error %d adding sysfs file\n", ret);
-		device_remove_file(&dock_device->dev, &dev_attr_docked);
-		device_remove_file(&dock_device->dev, &dev_attr_undock);
-		platform_device_unregister(dock_device);
-		kfree(dock_station);
-		dock_station = NULL;
-		return ret;
-	}
-	ret = device_create_file(&dock_device->dev, &dev_attr_flags);
-	if (ret) {
-		printk(KERN_ERR "Error %d adding sysfs file\n", ret);
-		device_remove_file(&dock_device->dev, &dev_attr_docked);
-		device_remove_file(&dock_device->dev, &dev_attr_undock);
-		device_remove_file(&dock_device->dev, &dev_attr_uid);
-		platform_device_unregister(dock_device);
-		kfree(dock_station);
-		dock_station = NULL;
-		return ret;
-	}
-	ret = device_create_file(&dock_device->dev, &dev_attr_type);
+	ret = sysfs_create_group(&dock_device->dev.kobj, &dock_attribute_group);
 	if (ret)
-		printk(KERN_ERR"Error %d adding sysfs file\n", ret);
+		goto err_unregister;
 
 	/* Find dependent devices */
 	acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
@@ -1036,10 +1010,8 @@  static int dock_add(acpi_handle handle)
 	/* add the dock station as a device dependent on itself */
 	dd = alloc_dock_dependent_device(handle);
 	if (!dd) {
-		kfree(dock_station);
-		dock_station = NULL;
 		ret = -ENOMEM;
-		goto dock_add_err_unregister;
+		goto err_unregister;
 	}
 	add_dock_dependent_device(dock_station, dd);
 
@@ -1047,13 +1019,11 @@  static int dock_add(acpi_handle handle)
 	list_add(&dock_station->sibling, &dock_stations);
 	return 0;
 
-dock_add_err_unregister:
-	device_remove_file(&dock_device->dev, &dev_attr_type);
-	device_remove_file(&dock_device->dev, &dev_attr_docked);
-	device_remove_file(&dock_device->dev, &dev_attr_undock);
-	device_remove_file(&dock_device->dev, &dev_attr_uid);
-	device_remove_file(&dock_device->dev, &dev_attr_flags);
+err_unregister:
+	printk(KERN_ERR "%s encountered error %d\n", __func__, ret);
+	sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
 	platform_device_unregister(dock_device);
+out:
 	kfree(dock_station);
 	dock_station = NULL;
 	return ret;
@@ -1076,11 +1046,7 @@  static int dock_remove(struct dock_station *dock_station)
 	    kfree(dd);
 
 	/* cleanup sysfs */
-	device_remove_file(&dock_device->dev, &dev_attr_type);
-	device_remove_file(&dock_device->dev, &dev_attr_docked);
-	device_remove_file(&dock_device->dev, &dev_attr_undock);
-	device_remove_file(&dock_device->dev, &dev_attr_uid);
-	device_remove_file(&dock_device->dev, &dev_attr_flags);
+	sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
 	platform_device_unregister(dock_device);
 
 	/* free dock station memory */