0001 =====================================================================
0002 Everything you never wanted to know about kobjects, ksets, and ktypes
0003 =====================================================================
0004
0005 :Author: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
0006 :Last updated: December 19, 2007
0007
0008 Based on an original article by Jon Corbet for lwn.net written October 1,
0009 2003 and located at https://lwn.net/Articles/51437/
0010
0011 Part of the difficulty in understanding the driver model - and the kobject
0012 abstraction upon which it is built - is that there is no obvious starting
0013 place. Dealing with kobjects requires understanding a few different types,
0014 all of which make reference to each other. In an attempt to make things
0015 easier, we'll take a multi-pass approach, starting with vague terms and
0016 adding detail as we go. To that end, here are some quick definitions of
0017 some terms we will be working with.
0018
0019 - A kobject is an object of type struct kobject. Kobjects have a name
0020 and a reference count. A kobject also has a parent pointer (allowing
0021 objects to be arranged into hierarchies), a specific type, and,
0022 usually, a representation in the sysfs virtual filesystem.
0023
0024 Kobjects are generally not interesting on their own; instead, they are
0025 usually embedded within some other structure which contains the stuff
0026 the code is really interested in.
0027
0028 No structure should **EVER** have more than one kobject embedded within it.
0029 If it does, the reference counting for the object is sure to be messed
0030 up and incorrect, and your code will be buggy. So do not do this.
0031
0032 - A ktype is the type of object that embeds a kobject. Every structure
0033 that embeds a kobject needs a corresponding ktype. The ktype controls
0034 what happens to the kobject when it is created and destroyed.
0035
0036 - A kset is a group of kobjects. These kobjects can be of the same ktype
0037 or belong to different ktypes. The kset is the basic container type for
0038 collections of kobjects. Ksets contain their own kobjects, but you can
0039 safely ignore that implementation detail as the kset core code handles
0040 this kobject automatically.
0041
0042 When you see a sysfs directory full of other directories, generally each
0043 of those directories corresponds to a kobject in the same kset.
0044
0045 We'll look at how to create and manipulate all of these types. A bottom-up
0046 approach will be taken, so we'll go back to kobjects.
0047
0048
0049 Embedding kobjects
0050 ==================
0051
0052 It is rare for kernel code to create a standalone kobject, with one major
0053 exception explained below. Instead, kobjects are used to control access to
0054 a larger, domain-specific object. To this end, kobjects will be found
0055 embedded in other structures. If you are used to thinking of things in
0056 object-oriented terms, kobjects can be seen as a top-level, abstract class
0057 from which other classes are derived. A kobject implements a set of
0058 capabilities which are not particularly useful by themselves, but are
0059 nice to have in other objects. The C language does not allow for the
0060 direct expression of inheritance, so other techniques - such as structure
0061 embedding - must be used.
0062
0063 (As an aside, for those familiar with the kernel linked list implementation,
0064 this is analogous as to how "list_head" structs are rarely useful on
0065 their own, but are invariably found embedded in the larger objects of
0066 interest.)
0067
0068 So, for example, the UIO code in ``drivers/uio/uio.c`` has a structure that
0069 defines the memory region associated with a uio device::
0070
0071 struct uio_map {
0072 struct kobject kobj;
0073 struct uio_mem *mem;
0074 };
0075
0076 If you have a struct uio_map structure, finding its embedded kobject is
0077 just a matter of using the kobj member. Code that works with kobjects will
0078 often have the opposite problem, however: given a struct kobject pointer,
0079 what is the pointer to the containing structure? You must avoid tricks
0080 (such as assuming that the kobject is at the beginning of the structure)
0081 and, instead, use the container_of() macro, found in ``<linux/kernel.h>``::
0082
0083 container_of(ptr, type, member)
0084
0085 where:
0086
0087 * ``ptr`` is the pointer to the embedded kobject,
0088 * ``type`` is the type of the containing structure, and
0089 * ``member`` is the name of the structure field to which ``pointer`` points.
0090
0091 The return value from container_of() is a pointer to the corresponding
0092 container type. So, for example, a pointer ``kp`` to a struct kobject
0093 embedded **within** a struct uio_map could be converted to a pointer to the
0094 **containing** uio_map structure with::
0095
0096 struct uio_map *u_map = container_of(kp, struct uio_map, kobj);
0097
0098 For convenience, programmers often define a simple macro for **back-casting**
0099 kobject pointers to the containing type. Exactly this happens in the
0100 earlier ``drivers/uio/uio.c``, as you can see here::
0101
0102 struct uio_map {
0103 struct kobject kobj;
0104 struct uio_mem *mem;
0105 };
0106
0107 #define to_map(map) container_of(map, struct uio_map, kobj)
0108
0109 where the macro argument "map" is a pointer to the struct kobject in
0110 question. That macro is subsequently invoked with::
0111
0112 struct uio_map *map = to_map(kobj);
0113
0114
0115 Initialization of kobjects
0116 ==========================
0117
0118 Code which creates a kobject must, of course, initialize that object. Some
0119 of the internal fields are setup with a (mandatory) call to kobject_init()::
0120
0121 void kobject_init(struct kobject *kobj, const struct kobj_type *ktype);
0122
0123 The ktype is required for a kobject to be created properly, as every kobject
0124 must have an associated kobj_type. After calling kobject_init(), to
0125 register the kobject with sysfs, the function kobject_add() must be called::
0126
0127 int kobject_add(struct kobject *kobj, struct kobject *parent,
0128 const char *fmt, ...);
0129
0130 This sets up the parent of the kobject and the name for the kobject
0131 properly. If the kobject is to be associated with a specific kset,
0132 kobj->kset must be assigned before calling kobject_add(). If a kset is
0133 associated with a kobject, then the parent for the kobject can be set to
0134 NULL in the call to kobject_add() and then the kobject's parent will be the
0135 kset itself.
0136
0137 As the name of the kobject is set when it is added to the kernel, the name
0138 of the kobject should never be manipulated directly. If you must change
0139 the name of the kobject, call kobject_rename()::
0140
0141 int kobject_rename(struct kobject *kobj, const char *new_name);
0142
0143 kobject_rename() does not perform any locking or have a solid notion of
0144 what names are valid so the caller must provide their own sanity checking
0145 and serialization.
0146
0147 There is a function called kobject_set_name() but that is legacy cruft and
0148 is being removed. If your code needs to call this function, it is
0149 incorrect and needs to be fixed.
0150
0151 To properly access the name of the kobject, use the function
0152 kobject_name()::
0153
0154 const char *kobject_name(const struct kobject * kobj);
0155
0156 There is a helper function to both initialize and add the kobject to the
0157 kernel at the same time, called surprisingly enough kobject_init_and_add()::
0158
0159 int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
0160 struct kobject *parent, const char *fmt, ...);
0161
0162 The arguments are the same as the individual kobject_init() and
0163 kobject_add() functions described above.
0164
0165
0166 Uevents
0167 =======
0168
0169 After a kobject has been registered with the kobject core, you need to
0170 announce to the world that it has been created. This can be done with a
0171 call to kobject_uevent()::
0172
0173 int kobject_uevent(struct kobject *kobj, enum kobject_action action);
0174
0175 Use the **KOBJ_ADD** action for when the kobject is first added to the kernel.
0176 This should be done only after any attributes or children of the kobject
0177 have been initialized properly, as userspace will instantly start to look
0178 for them when this call happens.
0179
0180 When the kobject is removed from the kernel (details on how to do that are
0181 below), the uevent for **KOBJ_REMOVE** will be automatically created by the
0182 kobject core, so the caller does not have to worry about doing that by
0183 hand.
0184
0185
0186 Reference counts
0187 ================
0188
0189 One of the key functions of a kobject is to serve as a reference counter
0190 for the object in which it is embedded. As long as references to the object
0191 exist, the object (and the code which supports it) must continue to exist.
0192 The low-level functions for manipulating a kobject's reference counts are::
0193
0194 struct kobject *kobject_get(struct kobject *kobj);
0195 void kobject_put(struct kobject *kobj);
0196
0197 A successful call to kobject_get() will increment the kobject's reference
0198 counter and return the pointer to the kobject.
0199
0200 When a reference is released, the call to kobject_put() will decrement the
0201 reference count and, possibly, free the object. Note that kobject_init()
0202 sets the reference count to one, so the code which sets up the kobject will
0203 need to do a kobject_put() eventually to release that reference.
0204
0205 Because kobjects are dynamic, they must not be declared statically or on
0206 the stack, but instead, always allocated dynamically. Future versions of
0207 the kernel will contain a run-time check for kobjects that are created
0208 statically and will warn the developer of this improper usage.
0209
0210 If all that you want to use a kobject for is to provide a reference counter
0211 for your structure, please use the struct kref instead; a kobject would be
0212 overkill. For more information on how to use struct kref, please see the
0213 file Documentation/core-api/kref.rst in the Linux kernel source tree.
0214
0215
0216 Creating "simple" kobjects
0217 ==========================
0218
0219 Sometimes all that a developer wants is a way to create a simple directory
0220 in the sysfs hierarchy, and not have to mess with the whole complication of
0221 ksets, show and store functions, and other details. This is the one
0222 exception where a single kobject should be created. To create such an
0223 entry, use the function::
0224
0225 struct kobject *kobject_create_and_add(const char *name, struct kobject *parent);
0226
0227 This function will create a kobject and place it in sysfs in the location
0228 underneath the specified parent kobject. To create simple attributes
0229 associated with this kobject, use::
0230
0231 int sysfs_create_file(struct kobject *kobj, const struct attribute *attr);
0232
0233 or::
0234
0235 int sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp);
0236
0237 Both types of attributes used here, with a kobject that has been created
0238 with the kobject_create_and_add(), can be of type kobj_attribute, so no
0239 special custom attribute is needed to be created.
0240
0241 See the example module, ``samples/kobject/kobject-example.c`` for an
0242 implementation of a simple kobject and attributes.
0243
0244
0245
0246 ktypes and release methods
0247 ==========================
0248
0249 One important thing still missing from the discussion is what happens to a
0250 kobject when its reference count reaches zero. The code which created the
0251 kobject generally does not know when that will happen; if it did, there
0252 would be little point in using a kobject in the first place. Even
0253 predictable object lifecycles become more complicated when sysfs is brought
0254 in as other portions of the kernel can get a reference on any kobject that
0255 is registered in the system.
0256
0257 The end result is that a structure protected by a kobject cannot be freed
0258 before its reference count goes to zero. The reference count is not under
0259 the direct control of the code which created the kobject. So that code must
0260 be notified asynchronously whenever the last reference to one of its
0261 kobjects goes away.
0262
0263 Once you registered your kobject via kobject_add(), you must never use
0264 kfree() to free it directly. The only safe way is to use kobject_put(). It
0265 is good practice to always use kobject_put() after kobject_init() to avoid
0266 errors creeping in.
0267
0268 This notification is done through a kobject's release() method. Usually
0269 such a method has a form like::
0270
0271 void my_object_release(struct kobject *kobj)
0272 {
0273 struct my_object *mine = container_of(kobj, struct my_object, kobj);
0274
0275 /* Perform any additional cleanup on this object, then... */
0276 kfree(mine);
0277 }
0278
0279 One important point cannot be overstated: every kobject must have a
0280 release() method, and the kobject must persist (in a consistent state)
0281 until that method is called. If these constraints are not met, the code is
0282 flawed. Note that the kernel will warn you if you forget to provide a
0283 release() method. Do not try to get rid of this warning by providing an
0284 "empty" release function.
0285
0286 If all your cleanup function needs to do is call kfree(), then you must
0287 create a wrapper function which uses container_of() to upcast to the correct
0288 type (as shown in the example above) and then calls kfree() on the overall
0289 structure.
0290
0291 Note, the name of the kobject is available in the release function, but it
0292 must NOT be changed within this callback. Otherwise there will be a memory
0293 leak in the kobject core, which makes people unhappy.
0294
0295 Interestingly, the release() method is not stored in the kobject itself;
0296 instead, it is associated with the ktype. So let us introduce struct
0297 kobj_type::
0298
0299 struct kobj_type {
0300 void (*release)(struct kobject *kobj);
0301 const struct sysfs_ops *sysfs_ops;
0302 const struct attribute_group **default_groups;
0303 const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
0304 const void *(*namespace)(struct kobject *kobj);
0305 void (*get_ownership)(struct kobject *kobj, kuid_t *uid, kgid_t *gid);
0306 };
0307
0308 This structure is used to describe a particular type of kobject (or, more
0309 correctly, of containing object). Every kobject needs to have an associated
0310 kobj_type structure; a pointer to that structure must be specified when you
0311 call kobject_init() or kobject_init_and_add().
0312
0313 The release field in struct kobj_type is, of course, a pointer to the
0314 release() method for this type of kobject. The other two fields (sysfs_ops
0315 and default_groups) control how objects of this type are represented in
0316 sysfs; they are beyond the scope of this document.
0317
0318 The default_groups pointer is a list of default attributes that will be
0319 automatically created for any kobject that is registered with this ktype.
0320
0321
0322 ksets
0323 =====
0324
0325 A kset is merely a collection of kobjects that want to be associated with
0326 each other. There is no restriction that they be of the same ktype, but be
0327 very careful if they are not.
0328
0329 A kset serves these functions:
0330
0331 - It serves as a bag containing a group of objects. A kset can be used by
0332 the kernel to track "all block devices" or "all PCI device drivers."
0333
0334 - A kset is also a subdirectory in sysfs, where the associated kobjects
0335 with the kset can show up. Every kset contains a kobject which can be
0336 set up to be the parent of other kobjects; the top-level directories of
0337 the sysfs hierarchy are constructed in this way.
0338
0339 - Ksets can support the "hotplugging" of kobjects and influence how
0340 uevent events are reported to user space.
0341
0342 In object-oriented terms, "kset" is the top-level container class; ksets
0343 contain their own kobject, but that kobject is managed by the kset code and
0344 should not be manipulated by any other user.
0345
0346 A kset keeps its children in a standard kernel linked list. Kobjects point
0347 back to their containing kset via their kset field. In almost all cases,
0348 the kobjects belonging to a kset have that kset (or, strictly, its embedded
0349 kobject) in their parent.
0350
0351 As a kset contains a kobject within it, it should always be dynamically
0352 created and never declared statically or on the stack. To create a new
0353 kset use::
0354
0355 struct kset *kset_create_and_add(const char *name,
0356 const struct kset_uevent_ops *uevent_ops,
0357 struct kobject *parent_kobj);
0358
0359 When you are finished with the kset, call::
0360
0361 void kset_unregister(struct kset *k);
0362
0363 to destroy it. This removes the kset from sysfs and decrements its reference
0364 count. When the reference count goes to zero, the kset will be released.
0365 Because other references to the kset may still exist, the release may happen
0366 after kset_unregister() returns.
0367
0368 An example of using a kset can be seen in the
0369 ``samples/kobject/kset-example.c`` file in the kernel tree.
0370
0371 If a kset wishes to control the uevent operations of the kobjects
0372 associated with it, it can use the struct kset_uevent_ops to handle it::
0373
0374 struct kset_uevent_ops {
0375 int (* const filter)(struct kobject *kobj);
0376 const char *(* const name)(struct kobject *kobj);
0377 int (* const uevent)(struct kobject *kobj, struct kobj_uevent_env *env);
0378 };
0379
0380
0381 The filter function allows a kset to prevent a uevent from being emitted to
0382 userspace for a specific kobject. If the function returns 0, the uevent
0383 will not be emitted.
0384
0385 The name function will be called to override the default name of the kset
0386 that the uevent sends to userspace. By default, the name will be the same
0387 as the kset itself, but this function, if present, can override that name.
0388
0389 The uevent function will be called when the uevent is about to be sent to
0390 userspace to allow more environment variables to be added to the uevent.
0391
0392 One might ask how, exactly, a kobject is added to a kset, given that no
0393 functions which perform that function have been presented. The answer is
0394 that this task is handled by kobject_add(). When a kobject is passed to
0395 kobject_add(), its kset member should point to the kset to which the
0396 kobject will belong. kobject_add() will handle the rest.
0397
0398 If the kobject belonging to a kset has no parent kobject set, it will be
0399 added to the kset's directory. Not all members of a kset do necessarily
0400 live in the kset directory. If an explicit parent kobject is assigned
0401 before the kobject is added, the kobject is registered with the kset, but
0402 added below the parent kobject.
0403
0404
0405 Kobject removal
0406 ===============
0407
0408 After a kobject has been registered with the kobject core successfully, it
0409 must be cleaned up when the code is finished with it. To do that, call
0410 kobject_put(). By doing this, the kobject core will automatically clean up
0411 all of the memory allocated by this kobject. If a ``KOBJ_ADD`` uevent has been
0412 sent for the object, a corresponding ``KOBJ_REMOVE`` uevent will be sent, and
0413 any other sysfs housekeeping will be handled for the caller properly.
0414
0415 If you need to do a two-stage delete of the kobject (say you are not
0416 allowed to sleep when you need to destroy the object), then call
0417 kobject_del() which will unregister the kobject from sysfs. This makes the
0418 kobject "invisible", but it is not cleaned up, and the reference count of
0419 the object is still the same. At a later time call kobject_put() to finish
0420 the cleanup of the memory associated with the kobject.
0421
0422 kobject_del() can be used to drop the reference to the parent object, if
0423 circular references are constructed. It is valid in some cases, that a
0424 parent objects references a child. Circular references _must_ be broken
0425 with an explicit call to kobject_del(), so that a release functions will be
0426 called, and the objects in the former circle release each other.
0427
0428
0429 Example code to copy from
0430 =========================
0431
0432 For a more complete example of using ksets and kobjects properly, see the
0433 example programs ``samples/kobject/{kobject-example.c,kset-example.c}``,
0434 which will be built as loadable modules if you select ``CONFIG_SAMPLE_KOBJECT``.