diff options
author | Mauro Carvalho Chehab <mchehab@s-opensource.com> | 2017-05-14 15:25:04 -0400 |
---|---|---|
committer | Jonathan Corbet <corbet@lwn.net> | 2017-07-14 15:51:44 -0400 |
commit | 7472723305906f3d60e95de1ecf3e31b20c2a854 (patch) | |
tree | 7f1a2aa82148a69a3dba73787af5d4e1156503d7 /Documentation/kobject.txt | |
parent | 7d98c21bd0257ec9f56910c758519bee71767517 (diff) |
kobject.txt: standardize document format
Each text file under Documentation follows a different
format. Some doesn't even have titles!
Change its representation to follow the adopted standard,
using ReST markups for it to be parseable by Sphinx:
- Add markups for titles;
- mark literal blocks as such;
- add needed whitespace/blank lines;
- use :Author: and :Last updated: for authorship.
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Diffstat (limited to 'Documentation/kobject.txt')
-rw-r--r-- | Documentation/kobject.txt | 69 |
1 files changed, 42 insertions, 27 deletions
diff --git a/Documentation/kobject.txt b/Documentation/kobject.txt index 1be59a3a521c..fc9485d79061 100644 --- a/Documentation/kobject.txt +++ b/Documentation/kobject.txt | |||
@@ -1,13 +1,13 @@ | |||
1 | ===================================================================== | ||
1 | Everything you never wanted to know about kobjects, ksets, and ktypes | 2 | Everything you never wanted to know about kobjects, ksets, and ktypes |
3 | ===================================================================== | ||
2 | 4 | ||
3 | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 5 | :Author: Greg Kroah-Hartman <gregkh@linuxfoundation.org> |
6 | :Last updated: December 19, 2007 | ||
4 | 7 | ||
5 | Based on an original article by Jon Corbet for lwn.net written October 1, | 8 | Based on an original article by Jon Corbet for lwn.net written October 1, |
6 | 2003 and located at http://lwn.net/Articles/51437/ | 9 | 2003 and located at http://lwn.net/Articles/51437/ |
7 | 10 | ||
8 | Last updated December 19, 2007 | ||
9 | |||
10 | |||
11 | Part of the difficulty in understanding the driver model - and the kobject | 11 | Part of the difficulty in understanding the driver model - and the kobject |
12 | abstraction upon which it is built - is that there is no obvious starting | 12 | abstraction upon which it is built - is that there is no obvious starting |
13 | place. Dealing with kobjects requires understanding a few different types, | 13 | place. Dealing with kobjects requires understanding a few different types, |
@@ -47,6 +47,7 @@ approach will be taken, so we'll go back to kobjects. | |||
47 | 47 | ||
48 | 48 | ||
49 | Embedding kobjects | 49 | Embedding kobjects |
50 | ================== | ||
50 | 51 | ||
51 | It is rare for kernel code to create a standalone kobject, with one major | 52 | It is rare for kernel code to create a standalone kobject, with one major |
52 | exception explained below. Instead, kobjects are used to control access to | 53 | exception explained below. Instead, kobjects are used to control access to |
@@ -65,7 +66,7 @@ their own, but are invariably found embedded in the larger objects of | |||
65 | interest.) | 66 | interest.) |
66 | 67 | ||
67 | So, for example, the UIO code in drivers/uio/uio.c has a structure that | 68 | So, for example, the UIO code in drivers/uio/uio.c has a structure that |
68 | defines the memory region associated with a uio device: | 69 | defines the memory region associated with a uio device:: |
69 | 70 | ||
70 | struct uio_map { | 71 | struct uio_map { |
71 | struct kobject kobj; | 72 | struct kobject kobj; |
@@ -77,7 +78,7 @@ just a matter of using the kobj member. Code that works with kobjects will | |||
77 | often have the opposite problem, however: given a struct kobject pointer, | 78 | often have the opposite problem, however: given a struct kobject pointer, |
78 | what is the pointer to the containing structure? You must avoid tricks | 79 | what is the pointer to the containing structure? You must avoid tricks |
79 | (such as assuming that the kobject is at the beginning of the structure) | 80 | (such as assuming that the kobject is at the beginning of the structure) |
80 | and, instead, use the container_of() macro, found in <linux/kernel.h>: | 81 | and, instead, use the container_of() macro, found in <linux/kernel.h>:: |
81 | 82 | ||
82 | container_of(pointer, type, member) | 83 | container_of(pointer, type, member) |
83 | 84 | ||
@@ -90,13 +91,13 @@ where: | |||
90 | The return value from container_of() is a pointer to the corresponding | 91 | The return value from container_of() is a pointer to the corresponding |
91 | container type. So, for example, a pointer "kp" to a struct kobject | 92 | container type. So, for example, a pointer "kp" to a struct kobject |
92 | embedded *within* a struct uio_map could be converted to a pointer to the | 93 | embedded *within* a struct uio_map could be converted to a pointer to the |
93 | *containing* uio_map structure with: | 94 | *containing* uio_map structure with:: |
94 | 95 | ||
95 | struct uio_map *u_map = container_of(kp, struct uio_map, kobj); | 96 | struct uio_map *u_map = container_of(kp, struct uio_map, kobj); |
96 | 97 | ||
97 | For convenience, programmers often define a simple macro for "back-casting" | 98 | For convenience, programmers often define a simple macro for "back-casting" |
98 | kobject pointers to the containing type. Exactly this happens in the | 99 | kobject pointers to the containing type. Exactly this happens in the |
99 | earlier drivers/uio/uio.c, as you can see here: | 100 | earlier drivers/uio/uio.c, as you can see here:: |
100 | 101 | ||
101 | struct uio_map { | 102 | struct uio_map { |
102 | struct kobject kobj; | 103 | struct kobject kobj; |
@@ -106,23 +107,25 @@ earlier drivers/uio/uio.c, as you can see here: | |||
106 | #define to_map(map) container_of(map, struct uio_map, kobj) | 107 | #define to_map(map) container_of(map, struct uio_map, kobj) |
107 | 108 | ||
108 | where the macro argument "map" is a pointer to the struct kobject in | 109 | where the macro argument "map" is a pointer to the struct kobject in |
109 | question. That macro is subsequently invoked with: | 110 | question. That macro is subsequently invoked with:: |
110 | 111 | ||
111 | struct uio_map *map = to_map(kobj); | 112 | struct uio_map *map = to_map(kobj); |
112 | 113 | ||
113 | 114 | ||
114 | Initialization of kobjects | 115 | Initialization of kobjects |
116 | ========================== | ||
115 | 117 | ||
116 | Code which creates a kobject must, of course, initialize that object. Some | 118 | Code which creates a kobject must, of course, initialize that object. Some |
117 | of the internal fields are setup with a (mandatory) call to kobject_init(): | 119 | of the internal fields are setup with a (mandatory) call to kobject_init():: |
118 | 120 | ||
119 | void kobject_init(struct kobject *kobj, struct kobj_type *ktype); | 121 | void kobject_init(struct kobject *kobj, struct kobj_type *ktype); |
120 | 122 | ||
121 | The ktype is required for a kobject to be created properly, as every kobject | 123 | The ktype is required for a kobject to be created properly, as every kobject |
122 | must have an associated kobj_type. After calling kobject_init(), to | 124 | must have an associated kobj_type. After calling kobject_init(), to |
123 | register the kobject with sysfs, the function kobject_add() must be called: | 125 | register the kobject with sysfs, the function kobject_add() must be called:: |
124 | 126 | ||
125 | int kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...); | 127 | int kobject_add(struct kobject *kobj, struct kobject *parent, |
128 | const char *fmt, ...); | ||
126 | 129 | ||
127 | This sets up the parent of the kobject and the name for the kobject | 130 | This sets up the parent of the kobject and the name for the kobject |
128 | properly. If the kobject is to be associated with a specific kset, | 131 | properly. If the kobject is to be associated with a specific kset, |
@@ -133,7 +136,7 @@ kset itself. | |||
133 | 136 | ||
134 | As the name of the kobject is set when it is added to the kernel, the name | 137 | As the name of the kobject is set when it is added to the kernel, the name |
135 | of the kobject should never be manipulated directly. If you must change | 138 | of the kobject should never be manipulated directly. If you must change |
136 | the name of the kobject, call kobject_rename(): | 139 | the name of the kobject, call kobject_rename():: |
137 | 140 | ||
138 | int kobject_rename(struct kobject *kobj, const char *new_name); | 141 | int kobject_rename(struct kobject *kobj, const char *new_name); |
139 | 142 | ||
@@ -146,12 +149,12 @@ is being removed. If your code needs to call this function, it is | |||
146 | incorrect and needs to be fixed. | 149 | incorrect and needs to be fixed. |
147 | 150 | ||
148 | To properly access the name of the kobject, use the function | 151 | To properly access the name of the kobject, use the function |
149 | kobject_name(): | 152 | kobject_name():: |
150 | 153 | ||
151 | const char *kobject_name(const struct kobject * kobj); | 154 | const char *kobject_name(const struct kobject * kobj); |
152 | 155 | ||
153 | There is a helper function to both initialize and add the kobject to the | 156 | There is a helper function to both initialize and add the kobject to the |
154 | kernel at the same time, called surprisingly enough kobject_init_and_add(): | 157 | kernel at the same time, called surprisingly enough kobject_init_and_add():: |
155 | 158 | ||
156 | int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, | 159 | int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, |
157 | struct kobject *parent, const char *fmt, ...); | 160 | struct kobject *parent, const char *fmt, ...); |
@@ -161,10 +164,11 @@ kobject_add() functions described above. | |||
161 | 164 | ||
162 | 165 | ||
163 | Uevents | 166 | Uevents |
167 | ======= | ||
164 | 168 | ||
165 | After a kobject has been registered with the kobject core, you need to | 169 | After a kobject has been registered with the kobject core, you need to |
166 | announce to the world that it has been created. This can be done with a | 170 | announce to the world that it has been created. This can be done with a |
167 | call to kobject_uevent(): | 171 | call to kobject_uevent():: |
168 | 172 | ||
169 | int kobject_uevent(struct kobject *kobj, enum kobject_action action); | 173 | int kobject_uevent(struct kobject *kobj, enum kobject_action action); |
170 | 174 | ||
@@ -180,11 +184,12 @@ hand. | |||
180 | 184 | ||
181 | 185 | ||
182 | Reference counts | 186 | Reference counts |
187 | ================ | ||
183 | 188 | ||
184 | One of the key functions of a kobject is to serve as a reference counter | 189 | One of the key functions of a kobject is to serve as a reference counter |
185 | for the object in which it is embedded. As long as references to the object | 190 | for the object in which it is embedded. As long as references to the object |
186 | exist, the object (and the code which supports it) must continue to exist. | 191 | exist, the object (and the code which supports it) must continue to exist. |
187 | The low-level functions for manipulating a kobject's reference counts are: | 192 | The low-level functions for manipulating a kobject's reference counts are:: |
188 | 193 | ||
189 | struct kobject *kobject_get(struct kobject *kobj); | 194 | struct kobject *kobject_get(struct kobject *kobj); |
190 | void kobject_put(struct kobject *kobj); | 195 | void kobject_put(struct kobject *kobj); |
@@ -209,21 +214,24 @@ file Documentation/kref.txt in the Linux kernel source tree. | |||
209 | 214 | ||
210 | 215 | ||
211 | Creating "simple" kobjects | 216 | Creating "simple" kobjects |
217 | ========================== | ||
212 | 218 | ||
213 | Sometimes all that a developer wants is a way to create a simple directory | 219 | Sometimes all that a developer wants is a way to create a simple directory |
214 | in the sysfs hierarchy, and not have to mess with the whole complication of | 220 | in the sysfs hierarchy, and not have to mess with the whole complication of |
215 | ksets, show and store functions, and other details. This is the one | 221 | ksets, show and store functions, and other details. This is the one |
216 | exception where a single kobject should be created. To create such an | 222 | exception where a single kobject should be created. To create such an |
217 | entry, use the function: | 223 | entry, use the function:: |
218 | 224 | ||
219 | struct kobject *kobject_create_and_add(char *name, struct kobject *parent); | 225 | struct kobject *kobject_create_and_add(char *name, struct kobject *parent); |
220 | 226 | ||
221 | This function will create a kobject and place it in sysfs in the location | 227 | This function will create a kobject and place it in sysfs in the location |
222 | underneath the specified parent kobject. To create simple attributes | 228 | underneath the specified parent kobject. To create simple attributes |
223 | associated with this kobject, use: | 229 | associated with this kobject, use:: |
224 | 230 | ||
225 | int sysfs_create_file(struct kobject *kobj, struct attribute *attr); | 231 | int sysfs_create_file(struct kobject *kobj, struct attribute *attr); |
226 | or | 232 | |
233 | or:: | ||
234 | |||
227 | int sysfs_create_group(struct kobject *kobj, struct attribute_group *grp); | 235 | int sysfs_create_group(struct kobject *kobj, struct attribute_group *grp); |
228 | 236 | ||
229 | Both types of attributes used here, with a kobject that has been created | 237 | Both types of attributes used here, with a kobject that has been created |
@@ -236,6 +244,7 @@ implementation of a simple kobject and attributes. | |||
236 | 244 | ||
237 | 245 | ||
238 | ktypes and release methods | 246 | ktypes and release methods |
247 | ========================== | ||
239 | 248 | ||
240 | One important thing still missing from the discussion is what happens to a | 249 | One important thing still missing from the discussion is what happens to a |
241 | kobject when its reference count reaches zero. The code which created the | 250 | kobject when its reference count reaches zero. The code which created the |
@@ -257,7 +266,7 @@ is good practice to always use kobject_put() after kobject_init() to avoid | |||
257 | errors creeping in. | 266 | errors creeping in. |
258 | 267 | ||
259 | This notification is done through a kobject's release() method. Usually | 268 | This notification is done through a kobject's release() method. Usually |
260 | such a method has a form like: | 269 | such a method has a form like:: |
261 | 270 | ||
262 | void my_object_release(struct kobject *kobj) | 271 | void my_object_release(struct kobject *kobj) |
263 | { | 272 | { |
@@ -281,7 +290,7 @@ leak in the kobject core, which makes people unhappy. | |||
281 | 290 | ||
282 | Interestingly, the release() method is not stored in the kobject itself; | 291 | Interestingly, the release() method is not stored in the kobject itself; |
283 | instead, it is associated with the ktype. So let us introduce struct | 292 | instead, it is associated with the ktype. So let us introduce struct |
284 | kobj_type: | 293 | kobj_type:: |
285 | 294 | ||
286 | struct kobj_type { | 295 | struct kobj_type { |
287 | void (*release)(struct kobject *kobj); | 296 | void (*release)(struct kobject *kobj); |
@@ -306,6 +315,7 @@ automatically created for any kobject that is registered with this ktype. | |||
306 | 315 | ||
307 | 316 | ||
308 | ksets | 317 | ksets |
318 | ===== | ||
309 | 319 | ||
310 | A kset is merely a collection of kobjects that want to be associated with | 320 | A kset is merely a collection of kobjects that want to be associated with |
311 | each other. There is no restriction that they be of the same ktype, but be | 321 | each other. There is no restriction that they be of the same ktype, but be |
@@ -335,13 +345,16 @@ kobject) in their parent. | |||
335 | 345 | ||
336 | As a kset contains a kobject within it, it should always be dynamically | 346 | As a kset contains a kobject within it, it should always be dynamically |
337 | created and never declared statically or on the stack. To create a new | 347 | created and never declared statically or on the stack. To create a new |
338 | kset use: | 348 | kset use:: |
349 | |||
339 | struct kset *kset_create_and_add(const char *name, | 350 | struct kset *kset_create_and_add(const char *name, |
340 | struct kset_uevent_ops *u, | 351 | struct kset_uevent_ops *u, |
341 | struct kobject *parent); | 352 | struct kobject *parent); |
342 | 353 | ||
343 | When you are finished with the kset, call: | 354 | When you are finished with the kset, call:: |
355 | |||
344 | void kset_unregister(struct kset *kset); | 356 | void kset_unregister(struct kset *kset); |
357 | |||
345 | to destroy it. This removes the kset from sysfs and decrements its reference | 358 | to destroy it. This removes the kset from sysfs and decrements its reference |
346 | count. When the reference count goes to zero, the kset will be released. | 359 | count. When the reference count goes to zero, the kset will be released. |
347 | Because other references to the kset may still exist, the release may happen | 360 | Because other references to the kset may still exist, the release may happen |
@@ -351,14 +364,14 @@ An example of using a kset can be seen in the | |||
351 | samples/kobject/kset-example.c file in the kernel tree. | 364 | samples/kobject/kset-example.c file in the kernel tree. |
352 | 365 | ||
353 | If a kset wishes to control the uevent operations of the kobjects | 366 | If a kset wishes to control the uevent operations of the kobjects |
354 | associated with it, it can use the struct kset_uevent_ops to handle it: | 367 | associated with it, it can use the struct kset_uevent_ops to handle it:: |
355 | 368 | ||
356 | struct kset_uevent_ops { | 369 | struct kset_uevent_ops { |
357 | int (*filter)(struct kset *kset, struct kobject *kobj); | 370 | int (*filter)(struct kset *kset, struct kobject *kobj); |
358 | const char *(*name)(struct kset *kset, struct kobject *kobj); | 371 | const char *(*name)(struct kset *kset, struct kobject *kobj); |
359 | int (*uevent)(struct kset *kset, struct kobject *kobj, | 372 | int (*uevent)(struct kset *kset, struct kobject *kobj, |
360 | struct kobj_uevent_env *env); | 373 | struct kobj_uevent_env *env); |
361 | }; | 374 | }; |
362 | 375 | ||
363 | 376 | ||
364 | The filter function allows a kset to prevent a uevent from being emitted to | 377 | The filter function allows a kset to prevent a uevent from being emitted to |
@@ -386,6 +399,7 @@ added below the parent kobject. | |||
386 | 399 | ||
387 | 400 | ||
388 | Kobject removal | 401 | Kobject removal |
402 | =============== | ||
389 | 403 | ||
390 | After a kobject has been registered with the kobject core successfully, it | 404 | After a kobject has been registered with the kobject core successfully, it |
391 | must be cleaned up when the code is finished with it. To do that, call | 405 | must be cleaned up when the code is finished with it. To do that, call |
@@ -409,6 +423,7 @@ called, and the objects in the former circle release each other. | |||
409 | 423 | ||
410 | 424 | ||
411 | Example code to copy from | 425 | Example code to copy from |
426 | ========================= | ||
412 | 427 | ||
413 | For a more complete example of using ksets and kobjects properly, see the | 428 | For a more complete example of using ksets and kobjects properly, see the |
414 | example programs samples/kobject/{kobject-example.c,kset-example.c}, | 429 | example programs samples/kobject/{kobject-example.c,kset-example.c}, |