Commit e756bc5670d0f801ca43dc55b8eacde42a5b818b
Committed by
Greg Kroah-Hartman
1 parent
bfc5c17337
Exists in
master
and in
16 other branches
kobject: fix kset sample error path
Previously, example_init() leaked a kset if any of the object creations failed. This fixes the leak by calling kset_unregister() in the error path. Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Showing 1 changed file with 1 additions and 0 deletions Inline Diff
samples/kobject/kset-example.c
1 | /* | 1 | /* |
2 | * Sample kset and ktype implementation | 2 | * Sample kset and ktype implementation |
3 | * | 3 | * |
4 | * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com> | 4 | * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com> |
5 | * Copyright (C) 2007 Novell Inc. | 5 | * Copyright (C) 2007 Novell Inc. |
6 | * | 6 | * |
7 | * Released under the GPL version 2 only. | 7 | * Released under the GPL version 2 only. |
8 | * | 8 | * |
9 | */ | 9 | */ |
10 | #include <linux/kobject.h> | 10 | #include <linux/kobject.h> |
11 | #include <linux/string.h> | 11 | #include <linux/string.h> |
12 | #include <linux/sysfs.h> | 12 | #include <linux/sysfs.h> |
13 | #include <linux/slab.h> | 13 | #include <linux/slab.h> |
14 | #include <linux/module.h> | 14 | #include <linux/module.h> |
15 | #include <linux/init.h> | 15 | #include <linux/init.h> |
16 | 16 | ||
17 | /* | 17 | /* |
18 | * This module shows how to create a kset in sysfs called | 18 | * This module shows how to create a kset in sysfs called |
19 | * /sys/kernel/kset-example | 19 | * /sys/kernel/kset-example |
20 | * Then tree kobjects are created and assigned to this kset, "foo", "baz", | 20 | * Then tree kobjects are created and assigned to this kset, "foo", "baz", |
21 | * and "bar". In those kobjects, attributes of the same name are also | 21 | * and "bar". In those kobjects, attributes of the same name are also |
22 | * created and if an integer is written to these files, it can be later | 22 | * created and if an integer is written to these files, it can be later |
23 | * read out of it. | 23 | * read out of it. |
24 | */ | 24 | */ |
25 | 25 | ||
26 | 26 | ||
27 | /* | 27 | /* |
28 | * This is our "object" that we will create a few of and register them with | 28 | * This is our "object" that we will create a few of and register them with |
29 | * sysfs. | 29 | * sysfs. |
30 | */ | 30 | */ |
31 | struct foo_obj { | 31 | struct foo_obj { |
32 | struct kobject kobj; | 32 | struct kobject kobj; |
33 | int foo; | 33 | int foo; |
34 | int baz; | 34 | int baz; |
35 | int bar; | 35 | int bar; |
36 | }; | 36 | }; |
37 | #define to_foo_obj(x) container_of(x, struct foo_obj, kobj) | 37 | #define to_foo_obj(x) container_of(x, struct foo_obj, kobj) |
38 | 38 | ||
39 | /* a custom attribute that works just for a struct foo_obj. */ | 39 | /* a custom attribute that works just for a struct foo_obj. */ |
40 | struct foo_attribute { | 40 | struct foo_attribute { |
41 | struct attribute attr; | 41 | struct attribute attr; |
42 | ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf); | 42 | ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf); |
43 | ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count); | 43 | ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count); |
44 | }; | 44 | }; |
45 | #define to_foo_attr(x) container_of(x, struct foo_attribute, attr) | 45 | #define to_foo_attr(x) container_of(x, struct foo_attribute, attr) |
46 | 46 | ||
47 | /* | 47 | /* |
48 | * The default show function that must be passed to sysfs. This will be | 48 | * The default show function that must be passed to sysfs. This will be |
49 | * called by sysfs for whenever a show function is called by the user on a | 49 | * called by sysfs for whenever a show function is called by the user on a |
50 | * sysfs file associated with the kobjects we have registered. We need to | 50 | * sysfs file associated with the kobjects we have registered. We need to |
51 | * transpose back from a "default" kobject to our custom struct foo_obj and | 51 | * transpose back from a "default" kobject to our custom struct foo_obj and |
52 | * then call the show function for that specific object. | 52 | * then call the show function for that specific object. |
53 | */ | 53 | */ |
54 | static ssize_t foo_attr_show(struct kobject *kobj, | 54 | static ssize_t foo_attr_show(struct kobject *kobj, |
55 | struct attribute *attr, | 55 | struct attribute *attr, |
56 | char *buf) | 56 | char *buf) |
57 | { | 57 | { |
58 | struct foo_attribute *attribute; | 58 | struct foo_attribute *attribute; |
59 | struct foo_obj *foo; | 59 | struct foo_obj *foo; |
60 | 60 | ||
61 | attribute = to_foo_attr(attr); | 61 | attribute = to_foo_attr(attr); |
62 | foo = to_foo_obj(kobj); | 62 | foo = to_foo_obj(kobj); |
63 | 63 | ||
64 | if (!attribute->show) | 64 | if (!attribute->show) |
65 | return -EIO; | 65 | return -EIO; |
66 | 66 | ||
67 | return attribute->show(foo, attribute, buf); | 67 | return attribute->show(foo, attribute, buf); |
68 | } | 68 | } |
69 | 69 | ||
70 | /* | 70 | /* |
71 | * Just like the default show function above, but this one is for when the | 71 | * Just like the default show function above, but this one is for when the |
72 | * sysfs "store" is requested (when a value is written to a file.) | 72 | * sysfs "store" is requested (when a value is written to a file.) |
73 | */ | 73 | */ |
74 | static ssize_t foo_attr_store(struct kobject *kobj, | 74 | static ssize_t foo_attr_store(struct kobject *kobj, |
75 | struct attribute *attr, | 75 | struct attribute *attr, |
76 | const char *buf, size_t len) | 76 | const char *buf, size_t len) |
77 | { | 77 | { |
78 | struct foo_attribute *attribute; | 78 | struct foo_attribute *attribute; |
79 | struct foo_obj *foo; | 79 | struct foo_obj *foo; |
80 | 80 | ||
81 | attribute = to_foo_attr(attr); | 81 | attribute = to_foo_attr(attr); |
82 | foo = to_foo_obj(kobj); | 82 | foo = to_foo_obj(kobj); |
83 | 83 | ||
84 | if (!attribute->store) | 84 | if (!attribute->store) |
85 | return -EIO; | 85 | return -EIO; |
86 | 86 | ||
87 | return attribute->store(foo, attribute, buf, len); | 87 | return attribute->store(foo, attribute, buf, len); |
88 | } | 88 | } |
89 | 89 | ||
90 | /* Our custom sysfs_ops that we will associate with our ktype later on */ | 90 | /* Our custom sysfs_ops that we will associate with our ktype later on */ |
91 | static const struct sysfs_ops foo_sysfs_ops = { | 91 | static const struct sysfs_ops foo_sysfs_ops = { |
92 | .show = foo_attr_show, | 92 | .show = foo_attr_show, |
93 | .store = foo_attr_store, | 93 | .store = foo_attr_store, |
94 | }; | 94 | }; |
95 | 95 | ||
96 | /* | 96 | /* |
97 | * The release function for our object. This is REQUIRED by the kernel to | 97 | * The release function for our object. This is REQUIRED by the kernel to |
98 | * have. We free the memory held in our object here. | 98 | * have. We free the memory held in our object here. |
99 | * | 99 | * |
100 | * NEVER try to get away with just a "blank" release function to try to be | 100 | * NEVER try to get away with just a "blank" release function to try to be |
101 | * smarter than the kernel. Turns out, no one ever is... | 101 | * smarter than the kernel. Turns out, no one ever is... |
102 | */ | 102 | */ |
103 | static void foo_release(struct kobject *kobj) | 103 | static void foo_release(struct kobject *kobj) |
104 | { | 104 | { |
105 | struct foo_obj *foo; | 105 | struct foo_obj *foo; |
106 | 106 | ||
107 | foo = to_foo_obj(kobj); | 107 | foo = to_foo_obj(kobj); |
108 | kfree(foo); | 108 | kfree(foo); |
109 | } | 109 | } |
110 | 110 | ||
111 | /* | 111 | /* |
112 | * The "foo" file where the .foo variable is read from and written to. | 112 | * The "foo" file where the .foo variable is read from and written to. |
113 | */ | 113 | */ |
114 | static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr, | 114 | static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr, |
115 | char *buf) | 115 | char *buf) |
116 | { | 116 | { |
117 | return sprintf(buf, "%d\n", foo_obj->foo); | 117 | return sprintf(buf, "%d\n", foo_obj->foo); |
118 | } | 118 | } |
119 | 119 | ||
120 | static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr, | 120 | static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr, |
121 | const char *buf, size_t count) | 121 | const char *buf, size_t count) |
122 | { | 122 | { |
123 | sscanf(buf, "%du", &foo_obj->foo); | 123 | sscanf(buf, "%du", &foo_obj->foo); |
124 | return count; | 124 | return count; |
125 | } | 125 | } |
126 | 126 | ||
127 | static struct foo_attribute foo_attribute = | 127 | static struct foo_attribute foo_attribute = |
128 | __ATTR(foo, 0666, foo_show, foo_store); | 128 | __ATTR(foo, 0666, foo_show, foo_store); |
129 | 129 | ||
130 | /* | 130 | /* |
131 | * More complex function where we determine which variable is being accessed by | 131 | * More complex function where we determine which variable is being accessed by |
132 | * looking at the attribute for the "baz" and "bar" files. | 132 | * looking at the attribute for the "baz" and "bar" files. |
133 | */ | 133 | */ |
134 | static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr, | 134 | static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr, |
135 | char *buf) | 135 | char *buf) |
136 | { | 136 | { |
137 | int var; | 137 | int var; |
138 | 138 | ||
139 | if (strcmp(attr->attr.name, "baz") == 0) | 139 | if (strcmp(attr->attr.name, "baz") == 0) |
140 | var = foo_obj->baz; | 140 | var = foo_obj->baz; |
141 | else | 141 | else |
142 | var = foo_obj->bar; | 142 | var = foo_obj->bar; |
143 | return sprintf(buf, "%d\n", var); | 143 | return sprintf(buf, "%d\n", var); |
144 | } | 144 | } |
145 | 145 | ||
146 | static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr, | 146 | static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr, |
147 | const char *buf, size_t count) | 147 | const char *buf, size_t count) |
148 | { | 148 | { |
149 | int var; | 149 | int var; |
150 | 150 | ||
151 | sscanf(buf, "%du", &var); | 151 | sscanf(buf, "%du", &var); |
152 | if (strcmp(attr->attr.name, "baz") == 0) | 152 | if (strcmp(attr->attr.name, "baz") == 0) |
153 | foo_obj->baz = var; | 153 | foo_obj->baz = var; |
154 | else | 154 | else |
155 | foo_obj->bar = var; | 155 | foo_obj->bar = var; |
156 | return count; | 156 | return count; |
157 | } | 157 | } |
158 | 158 | ||
159 | static struct foo_attribute baz_attribute = | 159 | static struct foo_attribute baz_attribute = |
160 | __ATTR(baz, 0666, b_show, b_store); | 160 | __ATTR(baz, 0666, b_show, b_store); |
161 | static struct foo_attribute bar_attribute = | 161 | static struct foo_attribute bar_attribute = |
162 | __ATTR(bar, 0666, b_show, b_store); | 162 | __ATTR(bar, 0666, b_show, b_store); |
163 | 163 | ||
164 | /* | 164 | /* |
165 | * Create a group of attributes so that we can create and destroy them all | 165 | * Create a group of attributes so that we can create and destroy them all |
166 | * at once. | 166 | * at once. |
167 | */ | 167 | */ |
168 | static struct attribute *foo_default_attrs[] = { | 168 | static struct attribute *foo_default_attrs[] = { |
169 | &foo_attribute.attr, | 169 | &foo_attribute.attr, |
170 | &baz_attribute.attr, | 170 | &baz_attribute.attr, |
171 | &bar_attribute.attr, | 171 | &bar_attribute.attr, |
172 | NULL, /* need to NULL terminate the list of attributes */ | 172 | NULL, /* need to NULL terminate the list of attributes */ |
173 | }; | 173 | }; |
174 | 174 | ||
175 | /* | 175 | /* |
176 | * Our own ktype for our kobjects. Here we specify our sysfs ops, the | 176 | * Our own ktype for our kobjects. Here we specify our sysfs ops, the |
177 | * release function, and the set of default attributes we want created | 177 | * release function, and the set of default attributes we want created |
178 | * whenever a kobject of this type is registered with the kernel. | 178 | * whenever a kobject of this type is registered with the kernel. |
179 | */ | 179 | */ |
180 | static struct kobj_type foo_ktype = { | 180 | static struct kobj_type foo_ktype = { |
181 | .sysfs_ops = &foo_sysfs_ops, | 181 | .sysfs_ops = &foo_sysfs_ops, |
182 | .release = foo_release, | 182 | .release = foo_release, |
183 | .default_attrs = foo_default_attrs, | 183 | .default_attrs = foo_default_attrs, |
184 | }; | 184 | }; |
185 | 185 | ||
186 | static struct kset *example_kset; | 186 | static struct kset *example_kset; |
187 | static struct foo_obj *foo_obj; | 187 | static struct foo_obj *foo_obj; |
188 | static struct foo_obj *bar_obj; | 188 | static struct foo_obj *bar_obj; |
189 | static struct foo_obj *baz_obj; | 189 | static struct foo_obj *baz_obj; |
190 | 190 | ||
191 | static struct foo_obj *create_foo_obj(const char *name) | 191 | static struct foo_obj *create_foo_obj(const char *name) |
192 | { | 192 | { |
193 | struct foo_obj *foo; | 193 | struct foo_obj *foo; |
194 | int retval; | 194 | int retval; |
195 | 195 | ||
196 | /* allocate the memory for the whole object */ | 196 | /* allocate the memory for the whole object */ |
197 | foo = kzalloc(sizeof(*foo), GFP_KERNEL); | 197 | foo = kzalloc(sizeof(*foo), GFP_KERNEL); |
198 | if (!foo) | 198 | if (!foo) |
199 | return NULL; | 199 | return NULL; |
200 | 200 | ||
201 | /* | 201 | /* |
202 | * As we have a kset for this kobject, we need to set it before calling | 202 | * As we have a kset for this kobject, we need to set it before calling |
203 | * the kobject core. | 203 | * the kobject core. |
204 | */ | 204 | */ |
205 | foo->kobj.kset = example_kset; | 205 | foo->kobj.kset = example_kset; |
206 | 206 | ||
207 | /* | 207 | /* |
208 | * Initialize and add the kobject to the kernel. All the default files | 208 | * Initialize and add the kobject to the kernel. All the default files |
209 | * will be created here. As we have already specified a kset for this | 209 | * will be created here. As we have already specified a kset for this |
210 | * kobject, we don't have to set a parent for the kobject, the kobject | 210 | * kobject, we don't have to set a parent for the kobject, the kobject |
211 | * will be placed beneath that kset automatically. | 211 | * will be placed beneath that kset automatically. |
212 | */ | 212 | */ |
213 | retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name); | 213 | retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name); |
214 | if (retval) { | 214 | if (retval) { |
215 | kobject_put(&foo->kobj); | 215 | kobject_put(&foo->kobj); |
216 | return NULL; | 216 | return NULL; |
217 | } | 217 | } |
218 | 218 | ||
219 | /* | 219 | /* |
220 | * We are always responsible for sending the uevent that the kobject | 220 | * We are always responsible for sending the uevent that the kobject |
221 | * was added to the system. | 221 | * was added to the system. |
222 | */ | 222 | */ |
223 | kobject_uevent(&foo->kobj, KOBJ_ADD); | 223 | kobject_uevent(&foo->kobj, KOBJ_ADD); |
224 | 224 | ||
225 | return foo; | 225 | return foo; |
226 | } | 226 | } |
227 | 227 | ||
228 | static void destroy_foo_obj(struct foo_obj *foo) | 228 | static void destroy_foo_obj(struct foo_obj *foo) |
229 | { | 229 | { |
230 | kobject_put(&foo->kobj); | 230 | kobject_put(&foo->kobj); |
231 | } | 231 | } |
232 | 232 | ||
233 | static int __init example_init(void) | 233 | static int __init example_init(void) |
234 | { | 234 | { |
235 | /* | 235 | /* |
236 | * Create a kset with the name of "kset_example", | 236 | * Create a kset with the name of "kset_example", |
237 | * located under /sys/kernel/ | 237 | * located under /sys/kernel/ |
238 | */ | 238 | */ |
239 | example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj); | 239 | example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj); |
240 | if (!example_kset) | 240 | if (!example_kset) |
241 | return -ENOMEM; | 241 | return -ENOMEM; |
242 | 242 | ||
243 | /* | 243 | /* |
244 | * Create three objects and register them with our kset | 244 | * Create three objects and register them with our kset |
245 | */ | 245 | */ |
246 | foo_obj = create_foo_obj("foo"); | 246 | foo_obj = create_foo_obj("foo"); |
247 | if (!foo_obj) | 247 | if (!foo_obj) |
248 | goto foo_error; | 248 | goto foo_error; |
249 | 249 | ||
250 | bar_obj = create_foo_obj("bar"); | 250 | bar_obj = create_foo_obj("bar"); |
251 | if (!bar_obj) | 251 | if (!bar_obj) |
252 | goto bar_error; | 252 | goto bar_error; |
253 | 253 | ||
254 | baz_obj = create_foo_obj("baz"); | 254 | baz_obj = create_foo_obj("baz"); |
255 | if (!baz_obj) | 255 | if (!baz_obj) |
256 | goto baz_error; | 256 | goto baz_error; |
257 | 257 | ||
258 | return 0; | 258 | return 0; |
259 | 259 | ||
260 | baz_error: | 260 | baz_error: |
261 | destroy_foo_obj(bar_obj); | 261 | destroy_foo_obj(bar_obj); |
262 | bar_error: | 262 | bar_error: |
263 | destroy_foo_obj(foo_obj); | 263 | destroy_foo_obj(foo_obj); |
264 | foo_error: | 264 | foo_error: |
265 | kset_unregister(example_kset); | ||
265 | return -EINVAL; | 266 | return -EINVAL; |
266 | } | 267 | } |
267 | 268 | ||
268 | static void __exit example_exit(void) | 269 | static void __exit example_exit(void) |
269 | { | 270 | { |
270 | destroy_foo_obj(baz_obj); | 271 | destroy_foo_obj(baz_obj); |
271 | destroy_foo_obj(bar_obj); | 272 | destroy_foo_obj(bar_obj); |
272 | destroy_foo_obj(foo_obj); | 273 | destroy_foo_obj(foo_obj); |
273 | kset_unregister(example_kset); | 274 | kset_unregister(example_kset); |
274 | } | 275 | } |
275 | 276 | ||
276 | module_init(example_init); | 277 | module_init(example_init); |
277 | module_exit(example_exit); | 278 | module_exit(example_exit); |
278 | MODULE_LICENSE("GPL"); | 279 | MODULE_LICENSE("GPL"); |
279 | MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>"); | 280 | MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>"); |
280 | 281 |