Skip to content

Commit 2d76fdc

Browse files
t-8chgregkh
authored andcommitted
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>
1 parent c301a2e commit 2d76fdc

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

samples/kobject/kset-example.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ struct foo_obj {
3737
/* a custom attribute that works just for a struct foo_obj. */
3838
struct foo_attribute {
3939
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);
4243
};
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)
4445

4546
/*
4647
* The default show function that must be passed to sysfs. This will be
@@ -53,7 +54,7 @@ static ssize_t foo_attr_show(struct kobject *kobj,
5354
struct attribute *attr,
5455
char *buf)
5556
{
56-
struct foo_attribute *attribute;
57+
const struct foo_attribute *attribute;
5758
struct foo_obj *foo;
5859

5960
attribute = to_foo_attr(attr);
@@ -73,7 +74,7 @@ static ssize_t foo_attr_store(struct kobject *kobj,
7374
struct attribute *attr,
7475
const char *buf, size_t len)
7576
{
76-
struct foo_attribute *attribute;
77+
const struct foo_attribute *attribute;
7778
struct foo_obj *foo;
7879

7980
attribute = to_foo_attr(attr);
@@ -109,13 +110,13 @@ static void foo_release(struct kobject *kobj)
109110
/*
110111
* The "foo" file where the .foo variable is read from and written to.
111112
*/
112-
static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
113+
static ssize_t foo_show(struct foo_obj *foo_obj, const struct foo_attribute *attr,
113114
char *buf)
114115
{
115116
return sysfs_emit(buf, "%d\n", foo_obj->foo);
116117
}
117118

118-
static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
119+
static ssize_t foo_store(struct foo_obj *foo_obj, const struct foo_attribute *attr,
119120
const char *buf, size_t count)
120121
{
121122
int ret;
@@ -128,14 +129,14 @@ static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
128129
}
129130

130131
/* Sysfs attributes cannot be world-writable. */
131-
static struct foo_attribute foo_attribute =
132+
static const struct foo_attribute foo_attribute =
132133
__ATTR(foo, 0664, foo_show, foo_store);
133134

134135
/*
135136
* More complex function where we determine which variable is being accessed by
136137
* looking at the attribute for the "baz" and "bar" files.
137138
*/
138-
static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
139+
static ssize_t b_show(struct foo_obj *foo_obj, const struct foo_attribute *attr,
139140
char *buf)
140141
{
141142
int var;
@@ -147,7 +148,7 @@ static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
147148
return sysfs_emit(buf, "%d\n", var);
148149
}
149150

150-
static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
151+
static ssize_t b_store(struct foo_obj *foo_obj, const struct foo_attribute *attr,
151152
const char *buf, size_t count)
152153
{
153154
int var, ret;
@@ -163,24 +164,24 @@ static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
163164
return count;
164165
}
165166

166-
static struct foo_attribute baz_attribute =
167+
static const struct foo_attribute baz_attribute =
167168
__ATTR(baz, 0664, b_show, b_store);
168-
static struct foo_attribute bar_attribute =
169+
static const struct foo_attribute bar_attribute =
169170
__ATTR(bar, 0664, b_show, b_store);
170171

171172
/*
172173
* Create a group of attributes so that we can create and destroy them all
173174
* at once.
174175
*/
175-
static struct attribute *foo_default_attrs[] = {
176+
static const struct attribute *const foo_default_attrs[] = {
176177
&foo_attribute.attr,
177178
&baz_attribute.attr,
178179
&bar_attribute.attr,
179180
NULL, /* need to NULL terminate the list of attributes */
180181
};
181182

182183
static umode_t foo_default_attrs_is_visible(struct kobject *kobj,
183-
struct attribute *attr,
184+
const struct attribute *attr,
184185
int n)
185186
{
186187
/* Hide attributes with the same name as the kobject. */
@@ -190,8 +191,8 @@ static umode_t foo_default_attrs_is_visible(struct kobject *kobj,
190191
}
191192

192193
static const struct attribute_group foo_default_group = {
193-
.attrs = foo_default_attrs,
194-
.is_visible = foo_default_attrs_is_visible,
194+
.attrs_const = foo_default_attrs,
195+
.is_visible_const = foo_default_attrs_is_visible,
195196
};
196197
__ATTRIBUTE_GROUPS(foo_default);
197198

0 commit comments

Comments
 (0)