include: sysfs: Add macro to assign show for RO attributes
Subsystems (2)
PATCH 1/1 include: sysfs: Add macro to assign show for RO attributes
In some circumstances, multiple __ATTR_RO attributes need to be assigned with a single show function.
Add this macro to make life easier with simple code.
--- Documentation/filesystems/sysfs.rst | 2 ++ include/linux/sysfs.h | 5 +++++ 2 files changed, 7 insertions(+)
Documentation/filesystems/sysfs.rst +2 -0
@@ -141,6 +141,8 @@ __ATTR_RO_MODE(name, mode): fore more restrictive RO access currently only use case is the EFI System Resource Table (see drivers/firmware/efi/esrt.c) +__ATTR_RO_SHOW(name, show): + assumes default mode 0444 with specified show. __ATTR_RW(name): assumes default name_show, name_store and setting mode to 0644.
include/linux/sysfs.h +5 -0
@@ -117,6 +117,11 @@ struct attribute_group { .show = _name##_show, \ } +#define __ATTR_RO_SHOW (_name, _show) { \ + .attr = { .name = __stringify(_name), .mode = 0444 }, \ + .show = _show, \ +} +#define __ATTR_RO_MODE (_name, _mode) { \ .attr = { .name = __stringify(_name), \ .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
Signature · 1 line
2.7.4
Greg Kroah-Hartman
maintainerQuoted 29 lines · Orson Zhai
In some circumstances, multiple
__ATTR_ROattributes need to be assigned with a single show function.Add this macro to make life easier with simple code.
Signed-off-by Orson Zhai <[email protected]>--- Documentation/filesystems/sysfs.rst | 2 ++ include/linux/sysfs.h | 5 +++++ 2 files changed, 7 insertions(+)Documentation/filesystems/sysfs.rst +2
-0@@ -141,6 +141,8 @@ __ATTR_RO_MODE(name, mode): fore more restrictive RO access currently only use case is the EFI System Resource Table (see drivers/firmware/efi/esrt.c) +__ATTR_RO_SHOW(name, show): + assumes default mode 0444 with specified show. __ATTR_RW(name): assumes default name_show, name_store and setting mode to 0644.include/linux/sysfs.h +0
-0
include/linux/sysfs.h struct attribute_group { · line 123
Quoted 7 lines
include/linux/sysfs.h +4
-0@@ -117,6 +117,11 @@ struct attribute_group { .show = _name##_show, \ } +#define __ATTR_RO_SHOW (_name, _show) { \ + .attr = { .name = __stringify(_name), .mode = 0444 }, \ + .show = _show, \ +}
Do you have a real user for this? Using "raw" kobject attributes is rare and should not be used often, so who needs this?
thanks,
greg k-h
Quoted 33 lines · Greg Kroah-Hartman
Quoted 32 lines · Orson Zhai
In some circumstances, multiple
__ATTR_ROattributes need to be assigned with a single show function.Add this macro to make life easier with simple code.
Signed-off-by Orson Zhai <[email protected]>--- Documentation/filesystems/sysfs.rst | 2 ++ include/linux/sysfs.h | 5 +++++ 2 files changed, 7 insertions(+)Documentation/filesystems/sysfs.rst +2
-0@@ -141,6 +141,8 @@ __ATTR_RO_MODE(name, mode): fore more restrictive RO access currently only use case is the EFI System Resource Table (see drivers/firmware/efi/esrt.c) +__ATTR_RO_SHOW(name, show): + assumes default mode 0444 with specified show. __ATTR_RW(name): assumes default name_show, name_store and setting mode to 0644.include/linux/sysfs.h +0
-0@@ -117,6 +117,11 @@ struct attribute_group { .show = _name##_show, \ }
Quoted 6 lines
include/linux/sysfs.h struct attribute_group { · line 123
Quoted 4 lines
include/linux/sysfs.h +4
-0+#define __ATTR_RO_SHOW (_name, _show) { \ + .attr = { .name = __stringify(_name), .mode = 0444 }, \ + .show = _show, \ +}Do you have a real user for this? Using "raw" kobject attributes is
Yes, I have found at least one user in current kernel code.
Please refer to [1].
The author implemented a similar marcro __ATRR_MRO as mine, plus an __ATRR_MWO with specified restore.
If this patch merged, I'd to replace his marcro with mine.
Quoted 1 line
rare and should not be used often, so who needs this?
Agree. But for some device drivers it might be useful without side effect.
Another example is from Android increment-fs code out there. That driver has 3 sysfs attributes which shared with same show function which only prints "support" to userland.
Best Regards,
Orson
Quoted 4 lines
thanks,
greg k-h
[1] https://elixir.bootlin.com/linux/v5.11-rc5/source/drivers/media/pci/ddbridge/ddbridge-core.c#L2735
Greg Kroah-Hartman
maintainerQuoted 41 lines · Orson Zhai
Quoted 33 lines · Greg Kroah-Hartman
Quoted 32 lines · Orson Zhai
In some circumstances, multiple
__ATTR_ROattributes need to be assigned with a single show function.Add this macro to make life easier with simple code.
Signed-off-by Orson Zhai <[email protected]>--- Documentation/filesystems/sysfs.rst | 2 ++ include/linux/sysfs.h | 5 +++++ 2 files changed, 7 insertions(+)Documentation/filesystems/sysfs.rst +2
-0@@ -141,6 +141,8 @@ __ATTR_RO_MODE(name, mode): fore more restrictive RO access currently only use case is the EFI System Resource Table (see drivers/firmware/efi/esrt.c) +__ATTR_RO_SHOW(name, show): + assumes default mode 0444 with specified show. __ATTR_RW(name): assumes default name_show, name_store and setting mode to 0644.include/linux/sysfs.h +0
-0@@ -117,6 +117,11 @@ struct attribute_group { .show = _name##_show, \ }Quoted 6 lines
include/linux/sysfs.h struct attribute_group { · line 123
Quoted 4 lines
include/linux/sysfs.h +4
-0+#define __ATTR_RO_SHOW (_name, _show) { \ + .attr = { .name = __stringify(_name), .mode = 0444 }, \ + .show = _show, \ +}Do you have a real user for this? Using "raw" kobject attributes is
Quoted 6 lines
Yes, I have found at least one user in current kernel code.
Please refer to [1].
The author implemented a similar marcro
__ATRR_MROas mine, plus an__ATRR_MWOwith specified restore.
Ick, no, that should be using DEVICE_ATTR_RO() as it is a struct device attribute, not a "raw" kobject attribute. So that code should be fixed up anyway, no need for this macro :)
Quoted 6 lines
If this patch merged, I'd to replace his marcro with mine.
Quoted 1 line
rare and should not be used often, so who needs this?
Agree. But for some device drivers it might be useful without side effect.
Drivers should NOT be ever using __ATTR* macros. That is not what they are there for.
Quoted 3 lines
Another example is from Android increment-fs code out there. That driver has 3 sysfs attributes which shared with same show function which only prints "support" to userland.
I can't take patches for out-of-tree code, sorry, you know this :)
thanks,
greg k-h