[PATCH 2/7] sysfs: Introduce macros to simplify creation of visible attribute groups

David E. Box posted 7 patches 7 months, 3 weeks ago
[PATCH 2/7] sysfs: Introduce macros to simplify creation of visible attribute groups
Posted by David E. Box 7 months, 3 weeks ago
Introduce macros to reduce boilerplate in attribute group definitions.
Combine DEFINE_SYSFS_ATTRIBUTE_GROUP_[COMBO]_VISIBILITY() with attribute
definitions in order to simplify group declarations involving visibility
logic.

Signed-off-by: David E. Box <david.e.box@linux.intel.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 include/linux/sysfs.h | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index 00dc88776f21..0804bffd6013 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -303,6 +303,30 @@ static const struct attribute_group _name##_group = {		\
 };								\
 __ATTRIBUTE_GROUPS(_name)
 
+#define NAMED_ATTRIBUTE_GROUP_VISIBLE(_name)		\
+DEFINE_SYSFS_GROUP_VISIBILITY(_name);			\
+static const struct attribute_group _name##_group = {	\
+	.name = __stringify(_name),			\
+	.attrs = _name##_attrs,				\
+	.is_visible = SYSFS_GROUP_VISIBLE(_name),	\
+}
+
+#define NAMED_ATTRIBUTE_GROUPS_VISIBLE(_name)	\
+NAMED_ATTRIBUTE_GROUP_VISIBLE(_name);		\
+__ATTRIBUTE_GROUPS(_name)
+
+#define NAMED_ATTRIBUTE_GROUP_COMBO_VISIBLE(_name)	\
+DEFINE_SYSFS_GROUP_COMBO_VISIBILITY(_name);		\
+static const struct attribute_group _name##_group = {	\
+	.name = __stringify(_name),			\
+	.attrs = _name##_attrs,				\
+	.is_visible = SYSFS_GROUP_VISIBLE(_name),	\
+}
+
+#define NAMED_ATTRIBUTE_GROUPS_COMBO_VISIBLE(_name)	\
+NAMED_ATTRIBUTE_GROUP_COMBO_VISIBLE(_name);		\
+__ATTRIBUTE_GROUPS(_name)
+
 struct file;
 struct vm_area_struct;
 struct address_space;
-- 
2.43.0
Re: [PATCH 2/7] sysfs: Introduce macros to simplify creation of visible attribute groups
Posted by Dan Williams 7 months, 3 weeks ago
David E. Box wrote:
> Introduce macros to reduce boilerplate in attribute group definitions.
> Combine DEFINE_SYSFS_ATTRIBUTE_GROUP_[COMBO]_VISIBILITY() with attribute
> definitions in order to simplify group declarations involving visibility
> logic.
> 
> Signed-off-by: David E. Box <david.e.box@linux.intel.com>
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  include/linux/sysfs.h | 24 ++++++++++++++++++++++++
>  1 file changed, 24 insertions(+)
> 
> diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
> index 00dc88776f21..0804bffd6013 100644
> --- a/include/linux/sysfs.h
> +++ b/include/linux/sysfs.h
> @@ -303,6 +303,30 @@ static const struct attribute_group _name##_group = {		\
>  };								\
>  __ATTRIBUTE_GROUPS(_name)
>  
> +#define NAMED_ATTRIBUTE_GROUP_VISIBLE(_name)		\
> +DEFINE_SYSFS_GROUP_VISIBILITY(_name);			\
> +static const struct attribute_group _name##_group = {	\
> +	.name = __stringify(_name),			\
> +	.attrs = _name##_attrs,				\
> +	.is_visible = SYSFS_GROUP_VISIBLE(_name),	\
> +}
> +
> +#define NAMED_ATTRIBUTE_GROUPS_VISIBLE(_name)	\
> +NAMED_ATTRIBUTE_GROUP_VISIBLE(_name);		\
> +__ATTRIBUTE_GROUPS(_name)
> +
> +#define NAMED_ATTRIBUTE_GROUP_COMBO_VISIBLE(_name)	\
> +DEFINE_SYSFS_GROUP_COMBO_VISIBILITY(_name);		\
> +static const struct attribute_group _name##_group = {	\
> +	.name = __stringify(_name),			\
> +	.attrs = _name##_attrs,				\
> +	.is_visible = SYSFS_GROUP_VISIBLE(_name),	\
> +}
> +
> +#define NAMED_ATTRIBUTE_GROUPS_COMBO_VISIBLE(_name)	\
> +NAMED_ATTRIBUTE_GROUP_COMBO_VISIBLE(_name);		\
> +__ATTRIBUTE_GROUPS(_name)
> +

Looks good to me, I like that this makes clear that it is setting {
.name } in the resulting group.

I would not mind a comment like "See DEFINE_SYSFS_GROUP_COMBO_VISIBILITY
for method definition requirements", but only add that as a follow-on
patch if someone else acks that idea.

Reviewed-by: Dan Williams <dan.j.williams@intel.com>