[PATCH v3 5/7] samples/kobject: add is_visible() callback to attribute group

Thomas Weißschuh posted 7 patches 1 month, 3 weeks ago
[PATCH v3 5/7] samples/kobject: add is_visible() callback to attribute group
Posted by Thomas Weißschuh 1 month, 3 weeks ago
There was no example for the is_visible() callback so far.

It will also become an example and test for the constification of
'struct attribute' later.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 samples/kobject/kset-example.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/samples/kobject/kset-example.c b/samples/kobject/kset-example.c
index 579ce150217c6e613887e32a08206573543b3091..1aac595ed9498b30448485a60d9376cb5b5ea1d3 100644
--- a/samples/kobject/kset-example.c
+++ b/samples/kobject/kset-example.c
@@ -178,7 +178,22 @@ static struct attribute *foo_default_attrs[] = {
 	&bar_attribute.attr,
 	NULL,	/* need to NULL terminate the list of attributes */
 };
-ATTRIBUTE_GROUPS(foo_default);
+
+static umode_t foo_default_attrs_is_visible(struct kobject *kobj,
+					    struct attribute *attr,
+					    int n)
+{
+	/* Hide attributes with the same name as the kobject. */
+	if (strcmp(kobject_name(kobj), attr->name) == 0)
+		return 0;
+	return attr->mode;
+}
+
+static const struct attribute_group foo_default_group = {
+	.attrs		= foo_default_attrs,
+	.is_visible	= foo_default_attrs_is_visible,
+};
+__ATTRIBUTE_GROUPS(foo_default);
 
 /*
  * Our own ktype for our kobjects.  Here we specify our sysfs ops, the

-- 
2.50.1

Re: [PATCH v3 5/7] samples/kobject: add is_visible() callback to attribute group
Posted by Greg Kroah-Hartman 1 month, 2 weeks ago
On Mon, Aug 11, 2025 at 11:14:31AM +0200, Thomas Weißschuh wrote:
> There was no example for the is_visible() callback so far.
> 
> It will also become an example and test for the constification of
> 'struct attribute' later.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>  samples/kobject/kset-example.c | 17 ++++++++++++++++-
>  1 file changed, 16 insertions(+), 1 deletion(-)
> 
> diff --git a/samples/kobject/kset-example.c b/samples/kobject/kset-example.c
> index 579ce150217c6e613887e32a08206573543b3091..1aac595ed9498b30448485a60d9376cb5b5ea1d3 100644
> --- a/samples/kobject/kset-example.c
> +++ b/samples/kobject/kset-example.c
> @@ -178,7 +178,22 @@ static struct attribute *foo_default_attrs[] = {
>  	&bar_attribute.attr,
>  	NULL,	/* need to NULL terminate the list of attributes */
>  };
> -ATTRIBUTE_GROUPS(foo_default);
> +
> +static umode_t foo_default_attrs_is_visible(struct kobject *kobj,
> +					    struct attribute *attr,
> +					    int n)
> +{
> +	/* Hide attributes with the same name as the kobject. */
> +	if (strcmp(kobject_name(kobj), attr->name) == 0)
> +		return 0;
> +	return attr->mode;
> +}
> +
> +static const struct attribute_group foo_default_group = {
> +	.attrs		= foo_default_attrs,
> +	.is_visible	= foo_default_attrs_is_visible,
> +};
> +__ATTRIBUTE_GROUPS(foo_default);

Wait, why?  Shouldn't ATTRIBUTE_GROUPS() still work here?  No one should
have to call __ATTRIBUTE_GROUPS() in their code, that's just going to be
too messy over time.

thanks

greg k-h
Re: [PATCH v3 5/7] samples/kobject: add is_visible() callback to attribute group
Posted by Thomas Weißschuh 1 month, 2 weeks ago
On 2025-08-19 13:24:04+0200, Greg Kroah-Hartman wrote:
> On Mon, Aug 11, 2025 at 11:14:31AM +0200, Thomas Weißschuh wrote:
> > There was no example for the is_visible() callback so far.
> > 
> > It will also become an example and test for the constification of
> > 'struct attribute' later.
> > 
> > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> > ---
> >  samples/kobject/kset-example.c | 17 ++++++++++++++++-
> >  1 file changed, 16 insertions(+), 1 deletion(-)
> > 
> > diff --git a/samples/kobject/kset-example.c b/samples/kobject/kset-example.c
> > index 579ce150217c6e613887e32a08206573543b3091..1aac595ed9498b30448485a60d9376cb5b5ea1d3 100644
> > --- a/samples/kobject/kset-example.c
> > +++ b/samples/kobject/kset-example.c
> > @@ -178,7 +178,22 @@ static struct attribute *foo_default_attrs[] = {
> >  	&bar_attribute.attr,
> >  	NULL,	/* need to NULL terminate the list of attributes */
> >  };
> > -ATTRIBUTE_GROUPS(foo_default);
> > +
> > +static umode_t foo_default_attrs_is_visible(struct kobject *kobj,
> > +					    struct attribute *attr,
> > +					    int n)
> > +{
> > +	/* Hide attributes with the same name as the kobject. */
> > +	if (strcmp(kobject_name(kobj), attr->name) == 0)
> > +		return 0;
> > +	return attr->mode;
> > +}
> > +
> > +static const struct attribute_group foo_default_group = {
> > +	.attrs		= foo_default_attrs,
> > +	.is_visible	= foo_default_attrs_is_visible,
> > +};
> > +__ATTRIBUTE_GROUPS(foo_default);
> 
> Wait, why?  Shouldn't ATTRIBUTE_GROUPS() still work here?  No one should
> have to call __ATTRIBUTE_GROUPS() in their code, that's just going to be
> too messy over time.

ATTRIBUTE_GROUPS() can not handle .is_visible().
There are already a few users throughout the tree.

Thomas