Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1692437

Re: [PATCH v2 4/7] driver core: add devm_device_add_group() and friends

From Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Newsgroups linux.kernel
Subject Re: [PATCH v2 4/7] driver core: add devm_device_add_group() and friends
Date 2017-07-20 07:20 +0200
Message-ID <u5c2u-2DM-13@gated-at.bofh.it> (permalink)
References <u57vQ-7Pa-5@gated-at.bofh.it> <u57vR-7Pa-27@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Wed, Jul 19, 2017 at 05:24:33PM -0700, Dmitry Torokhov wrote:
> Many drivers create additional driver-specific device attributes when
> binding to the device, and providing managed version of
> device_create_group() will simplify unbinding and error handling in probe
> path for such drivers.
> 
> Without managed version driver writers either have to mix manual and
> managed resources, which is prone to errors, or open-code this function by
> providing a wrapper to device_add_group() and use it with devm_add_action()
> or devm_add_action_or_reset().
> 
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
>  drivers/base/core.c    | 130 +++++++++++++++++++++++++++++++++++++++++++++++++
>  include/linux/device.h |   9 ++++
>  2 files changed, 139 insertions(+)
> 
> diff --git a/drivers/base/core.c b/drivers/base/core.c
> index 14f8cf5c8b05..09723532725d 100644
> --- a/drivers/base/core.c
> +++ b/drivers/base/core.c
> @@ -1035,6 +1035,136 @@ void device_remove_groups(struct device *dev,
>  }
>  EXPORT_SYMBOL_GPL(device_remove_groups);
>  
> +union device_attr_group_devres {
> +	const struct attribute_group *group;
> +	const struct attribute_group **groups;
> +};
> +
> +static int devm_attr_group_match(struct device *dev, void *res, void *data)
> +{
> +	return ((union device_attr_group_devres *)res)->group == data;
> +}
> +
> +static void devm_attr_group_remove(struct device *dev, void *res)
> +{
> +	union device_attr_group_devres *devres = res;
> +	const struct attribute_group *group = devres->group;
> +
> +	dev_dbg(dev, "%s: removing group %p\n", __func__, group);
> +	sysfs_remove_group(&dev->kobj, group);
> +}
> +
> +static void devm_attr_groups_remove(struct device *dev, void *res)
> +{
> +	union device_attr_group_devres *devres = res;
> +	const struct attribute_group **groups = devres->groups;
> +
> +	dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
> +	sysfs_remove_groups(&dev->kobj, groups);
> +}
> +
> +/**
> + * devm_device_add_group - given a device, create a managed attribute group
> + * @dev:	The device to create the group for
> + * @grp:	The attribute group to create
> + *
> + * This function creates a group for the first time.  It will explicitly
> + * warn and error if any of the attribute files being created already exist.
> + *
> + * Returns 0 on success or error code on failure.
> + */
> +int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
> +{
> +	union device_attr_group_devres *devres;
> +	int error;
> +
> +	devres = devres_alloc(devm_attr_group_remove,
> +			      sizeof(*devres), GFP_KERNEL);
> +	if (!devres)
> +		return -ENOMEM;
> +
> +	error = sysfs_create_group(&dev->kobj, grp);

Minor nit, this can now call device_create_group(), right?

Same with below I think as well.

It's fine, these look great, I'll queue them up this afternoon...

Thanks for persisting with these, and sorry it took so long to convince
me I was wrong :)

greg k-h

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v2 0/7] New bind/unbingd uevents Dmitry Torokhov <dmitry.torokhov@gmail.com> - 2017-07-20 02:30 +0200
  [PATCH v2 6/7] Input: synaptics_rmi4 - use devm_device_add_group() for attributes in F01 Dmitry Torokhov <dmitry.torokhov@gmail.com> - 2017-07-20 02:30 +0200
    Re: [PATCH v2 6/7] Input: synaptics_rmi4 - use  devm_device_add_group() for attributes in F01 Guenter Roeck <linux@roeck-us.net> - 2017-07-20 05:30 +0200
  [PATCH v2 4/7] driver core: add devm_device_add_group() and friends Dmitry Torokhov <dmitry.torokhov@gmail.com> - 2017-07-20 02:30 +0200
    Re: [PATCH v2 4/7] driver core: add devm_device_add_group() and  friends Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-07-20 07:20 +0200
      Re: [PATCH v2 4/7] driver core: add devm_device_add_group() and friends Dmitry Torokhov <dmitry.torokhov@gmail.com> - 2017-07-20 10:20 +0200
        Re: [PATCH v2 4/7] driver core: add devm_device_add_group() and  friends Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-07-20 10:30 +0200
          Re: [PATCH v2 4/7] driver core: add devm_device_add_group() and friends Dmitry Torokhov <dmitry.torokhov@gmail.com> - 2017-07-20 18:00 +0200
            Re: [PATCH v2 4/7] driver core: add devm_device_add_group() and  friends Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-07-22 12:10 +0200

csiph-web