summaryrefslogtreecommitdiffstats
path: root/Documentation/kobject.txt
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab@s-opensource.com>2017-05-14 15:25:04 -0400
committerJonathan Corbet <corbet@lwn.net>2017-07-14 15:51:44 -0400
commit7472723305906f3d60e95de1ecf3e31b20c2a854 (patch)
tree7f1a2aa82148a69a3dba73787af5d4e1156503d7 /Documentation/kobject.txt
parent7d98c21bd0257ec9f56910c758519bee71767517 (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.txt69
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=====================================================================
1Everything you never wanted to know about kobjects, ksets, and ktypes 2Everything you never wanted to know about kobjects, ksets, and ktypes
3=====================================================================
2 4
3Greg Kroah-Hartman <gregkh@linuxfoundation.org> 5:Author: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
6:Last updated: December 19, 2007
4 7
5Based on an original article by Jon Corbet for lwn.net written October 1, 8Based on an original article by Jon Corbet for lwn.net written October 1,
62003 and located at http://lwn.net/Articles/51437/ 92003 and located at http://lwn.net/Articles/51437/
7 10
8Last updated December 19, 2007
9
10
11Part of the difficulty in understanding the driver model - and the kobject 11Part of the difficulty in understanding the driver model - and the kobject
12abstraction upon which it is built - is that there is no obvious starting 12abstraction upon which it is built - is that there is no obvious starting
13place. Dealing with kobjects requires understanding a few different types, 13place. 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
49Embedding kobjects 49Embedding kobjects
50==================
50 51
51It is rare for kernel code to create a standalone kobject, with one major 52It is rare for kernel code to create a standalone kobject, with one major
52exception explained below. Instead, kobjects are used to control access to 53exception 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
65interest.) 66interest.)
66 67
67So, for example, the UIO code in drivers/uio/uio.c has a structure that 68So, for example, the UIO code in drivers/uio/uio.c has a structure that
68defines the memory region associated with a uio device: 69defines 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
77often have the opposite problem, however: given a struct kobject pointer, 78often have the opposite problem, however: given a struct kobject pointer,
78what is the pointer to the containing structure? You must avoid tricks 79what 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)
80and, instead, use the container_of() macro, found in <linux/kernel.h>: 81and, 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:
90The return value from container_of() is a pointer to the corresponding 91The return value from container_of() is a pointer to the corresponding
91container type. So, for example, a pointer "kp" to a struct kobject 92container type. So, for example, a pointer "kp" to a struct kobject
92embedded *within* a struct uio_map could be converted to a pointer to the 93embedded *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
97For convenience, programmers often define a simple macro for "back-casting" 98For convenience, programmers often define a simple macro for "back-casting"
98kobject pointers to the containing type. Exactly this happens in the 99kobject pointers to the containing type. Exactly this happens in the
99earlier drivers/uio/uio.c, as you can see here: 100earlier 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
108where the macro argument "map" is a pointer to the struct kobject in 109where the macro argument "map" is a pointer to the struct kobject in
109question. That macro is subsequently invoked with: 110question. 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
114Initialization of kobjects 115Initialization of kobjects
116==========================
115 117
116Code which creates a kobject must, of course, initialize that object. Some 118Code which creates a kobject must, of course, initialize that object. Some
117of the internal fields are setup with a (mandatory) call to kobject_init(): 119of 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
121The ktype is required for a kobject to be created properly, as every kobject 123The ktype is required for a kobject to be created properly, as every kobject
122must have an associated kobj_type. After calling kobject_init(), to 124must have an associated kobj_type. After calling kobject_init(), to
123register the kobject with sysfs, the function kobject_add() must be called: 125register 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
127This sets up the parent of the kobject and the name for the kobject 130This sets up the parent of the kobject and the name for the kobject
128properly. If the kobject is to be associated with a specific kset, 131properly. If the kobject is to be associated with a specific kset,
@@ -133,7 +136,7 @@ kset itself.
133 136
134As the name of the kobject is set when it is added to the kernel, the name 137As the name of the kobject is set when it is added to the kernel, the name
135of the kobject should never be manipulated directly. If you must change 138of the kobject should never be manipulated directly. If you must change
136the name of the kobject, call kobject_rename(): 139the 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
146incorrect and needs to be fixed. 149incorrect and needs to be fixed.
147 150
148To properly access the name of the kobject, use the function 151To properly access the name of the kobject, use the function
149kobject_name(): 152kobject_name()::
150 153
151 const char *kobject_name(const struct kobject * kobj); 154 const char *kobject_name(const struct kobject * kobj);
152 155
153There is a helper function to both initialize and add the kobject to the 156There is a helper function to both initialize and add the kobject to the
154kernel at the same time, called surprisingly enough kobject_init_and_add(): 157kernel 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
163Uevents 166Uevents
167=======
164 168
165After a kobject has been registered with the kobject core, you need to 169After a kobject has been registered with the kobject core, you need to
166announce to the world that it has been created. This can be done with a 170announce to the world that it has been created. This can be done with a
167call to kobject_uevent(): 171call 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
182Reference counts 186Reference counts
187================
183 188
184One of the key functions of a kobject is to serve as a reference counter 189One of the key functions of a kobject is to serve as a reference counter
185for the object in which it is embedded. As long as references to the object 190for the object in which it is embedded. As long as references to the object
186exist, the object (and the code which supports it) must continue to exist. 191exist, the object (and the code which supports it) must continue to exist.
187The low-level functions for manipulating a kobject's reference counts are: 192The 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
211Creating "simple" kobjects 216Creating "simple" kobjects
217==========================
212 218
213Sometimes all that a developer wants is a way to create a simple directory 219Sometimes all that a developer wants is a way to create a simple directory
214in the sysfs hierarchy, and not have to mess with the whole complication of 220in the sysfs hierarchy, and not have to mess with the whole complication of
215ksets, show and store functions, and other details. This is the one 221ksets, show and store functions, and other details. This is the one
216exception where a single kobject should be created. To create such an 222exception where a single kobject should be created. To create such an
217entry, use the function: 223entry, 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
221This function will create a kobject and place it in sysfs in the location 227This function will create a kobject and place it in sysfs in the location
222underneath the specified parent kobject. To create simple attributes 228underneath the specified parent kobject. To create simple attributes
223associated with this kobject, use: 229associated 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);
226or 232
233or::
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
229Both types of attributes used here, with a kobject that has been created 237Both 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
238ktypes and release methods 246ktypes and release methods
247==========================
239 248
240One important thing still missing from the discussion is what happens to a 249One important thing still missing from the discussion is what happens to a
241kobject when its reference count reaches zero. The code which created the 250kobject 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
257errors creeping in. 266errors creeping in.
258 267
259This notification is done through a kobject's release() method. Usually 268This notification is done through a kobject's release() method. Usually
260such a method has a form like: 269such 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
282Interestingly, the release() method is not stored in the kobject itself; 291Interestingly, the release() method is not stored in the kobject itself;
283instead, it is associated with the ktype. So let us introduce struct 292instead, it is associated with the ktype. So let us introduce struct
284kobj_type: 293kobj_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
308ksets 317ksets
318=====
309 319
310A kset is merely a collection of kobjects that want to be associated with 320A kset is merely a collection of kobjects that want to be associated with
311each other. There is no restriction that they be of the same ktype, but be 321each other. There is no restriction that they be of the same ktype, but be
@@ -335,13 +345,16 @@ kobject) in their parent.
335 345
336As a kset contains a kobject within it, it should always be dynamically 346As a kset contains a kobject within it, it should always be dynamically
337created and never declared statically or on the stack. To create a new 347created and never declared statically or on the stack. To create a new
338kset use: 348kset 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
343When you are finished with the kset, call: 354When you are finished with the kset, call::
355
344 void kset_unregister(struct kset *kset); 356 void kset_unregister(struct kset *kset);
357
345to destroy it. This removes the kset from sysfs and decrements its reference 358to destroy it. This removes the kset from sysfs and decrements its reference
346count. When the reference count goes to zero, the kset will be released. 359count. When the reference count goes to zero, the kset will be released.
347Because other references to the kset may still exist, the release may happen 360Because 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
351samples/kobject/kset-example.c file in the kernel tree. 364samples/kobject/kset-example.c file in the kernel tree.
352 365
353If a kset wishes to control the uevent operations of the kobjects 366If a kset wishes to control the uevent operations of the kobjects
354associated with it, it can use the struct kset_uevent_ops to handle it: 367associated with it, it can use the struct kset_uevent_ops to handle it::
355 368
356struct 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
364The filter function allows a kset to prevent a uevent from being emitted to 377The 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
388Kobject removal 401Kobject removal
402===============
389 403
390After a kobject has been registered with the kobject core successfully, it 404After a kobject has been registered with the kobject core successfully, it
391must be cleaned up when the code is finished with it. To do that, call 405must 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
411Example code to copy from 425Example code to copy from
426=========================
412 427
413For a more complete example of using ksets and kobjects properly, see the 428For a more complete example of using ksets and kobjects properly, see the
414example programs samples/kobject/{kobject-example.c,kset-example.c}, 429example programs samples/kobject/{kobject-example.c,kset-example.c},