Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

samples/kobject: constify 'struct foo_attribute'

Showcase and test the new 'struct attribute' constification facilities.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Link: https://patch.msgid.link/20251029-sysfs-const-attr-prep-v5-6-ea7d745acff4@weissschuh.net
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Thomas Weißschuh and committed by
Greg Kroah-Hartman
2d76fdc1 c301a2e2

+17 -16
+17 -16
samples/kobject/kset-example.c
··· 37 37 /* a custom attribute that works just for a struct foo_obj. */ 38 38 struct foo_attribute { 39 39 struct attribute attr; 40 - ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf); 41 - ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count); 40 + ssize_t (*show)(struct foo_obj *foo, const struct foo_attribute *attr, char *buf); 41 + ssize_t (*store)(struct foo_obj *foo, const struct foo_attribute *attr, 42 + const char *buf, size_t count); 42 43 }; 43 - #define to_foo_attr(x) container_of(x, struct foo_attribute, attr) 44 + #define to_foo_attr(x) container_of_const(x, struct foo_attribute, attr) 44 45 45 46 /* 46 47 * The default show function that must be passed to sysfs. This will be ··· 54 53 struct attribute *attr, 55 54 char *buf) 56 55 { 57 - struct foo_attribute *attribute; 56 + const struct foo_attribute *attribute; 58 57 struct foo_obj *foo; 59 58 60 59 attribute = to_foo_attr(attr); ··· 74 73 struct attribute *attr, 75 74 const char *buf, size_t len) 76 75 { 77 - struct foo_attribute *attribute; 76 + const struct foo_attribute *attribute; 78 77 struct foo_obj *foo; 79 78 80 79 attribute = to_foo_attr(attr); ··· 110 109 /* 111 110 * The "foo" file where the .foo variable is read from and written to. 112 111 */ 113 - static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr, 112 + static ssize_t foo_show(struct foo_obj *foo_obj, const struct foo_attribute *attr, 114 113 char *buf) 115 114 { 116 115 return sysfs_emit(buf, "%d\n", foo_obj->foo); 117 116 } 118 117 119 - static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr, 118 + static ssize_t foo_store(struct foo_obj *foo_obj, const struct foo_attribute *attr, 120 119 const char *buf, size_t count) 121 120 { 122 121 int ret; ··· 129 128 } 130 129 131 130 /* Sysfs attributes cannot be world-writable. */ 132 - static struct foo_attribute foo_attribute = 131 + static const struct foo_attribute foo_attribute = 133 132 __ATTR(foo, 0664, foo_show, foo_store); 134 133 135 134 /* 136 135 * More complex function where we determine which variable is being accessed by 137 136 * looking at the attribute for the "baz" and "bar" files. 138 137 */ 139 - static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr, 138 + static ssize_t b_show(struct foo_obj *foo_obj, const struct foo_attribute *attr, 140 139 char *buf) 141 140 { 142 141 int var; ··· 148 147 return sysfs_emit(buf, "%d\n", var); 149 148 } 150 149 151 - static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr, 150 + static ssize_t b_store(struct foo_obj *foo_obj, const struct foo_attribute *attr, 152 151 const char *buf, size_t count) 153 152 { 154 153 int var, ret; ··· 164 163 return count; 165 164 } 166 165 167 - static struct foo_attribute baz_attribute = 166 + static const struct foo_attribute baz_attribute = 168 167 __ATTR(baz, 0664, b_show, b_store); 169 - static struct foo_attribute bar_attribute = 168 + static const struct foo_attribute bar_attribute = 170 169 __ATTR(bar, 0664, b_show, b_store); 171 170 172 171 /* 173 172 * Create a group of attributes so that we can create and destroy them all 174 173 * at once. 175 174 */ 176 - static struct attribute *foo_default_attrs[] = { 175 + static const struct attribute *const foo_default_attrs[] = { 177 176 &foo_attribute.attr, 178 177 &baz_attribute.attr, 179 178 &bar_attribute.attr, ··· 181 180 }; 182 181 183 182 static umode_t foo_default_attrs_is_visible(struct kobject *kobj, 184 - struct attribute *attr, 183 + const struct attribute *attr, 185 184 int n) 186 185 { 187 186 /* Hide attributes with the same name as the kobject. */ ··· 191 190 } 192 191 193 192 static const struct attribute_group foo_default_group = { 194 - .attrs = foo_default_attrs, 195 - .is_visible = foo_default_attrs_is_visible, 193 + .attrs_const = foo_default_attrs, 194 + .is_visible_const = foo_default_attrs_is_visible, 196 195 }; 197 196 __ATTRIBUTE_GROUPS(foo_default); 198 197