1*4882a593Smuzhiyun===================================================================== 2*4882a593SmuzhiyunEverything you never wanted to know about kobjects, ksets, and ktypes 3*4882a593Smuzhiyun===================================================================== 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun:Author: Greg Kroah-Hartman <gregkh@linuxfoundation.org> 6*4882a593Smuzhiyun:Last updated: December 19, 2007 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunBased on an original article by Jon Corbet for lwn.net written October 1, 9*4882a593Smuzhiyun2003 and located at https://lwn.net/Articles/51437/ 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunPart of the difficulty in understanding the driver model - and the kobject 12*4882a593Smuzhiyunabstraction upon which it is built - is that there is no obvious starting 13*4882a593Smuzhiyunplace. Dealing with kobjects requires understanding a few different types, 14*4882a593Smuzhiyunall of which make reference to each other. In an attempt to make things 15*4882a593Smuzhiyuneasier, we'll take a multi-pass approach, starting with vague terms and 16*4882a593Smuzhiyunadding detail as we go. To that end, here are some quick definitions of 17*4882a593Smuzhiyunsome terms we will be working with. 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun - A kobject is an object of type struct kobject. Kobjects have a name 20*4882a593Smuzhiyun and a reference count. A kobject also has a parent pointer (allowing 21*4882a593Smuzhiyun objects to be arranged into hierarchies), a specific type, and, 22*4882a593Smuzhiyun usually, a representation in the sysfs virtual filesystem. 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun Kobjects are generally not interesting on their own; instead, they are 25*4882a593Smuzhiyun usually embedded within some other structure which contains the stuff 26*4882a593Smuzhiyun the code is really interested in. 27*4882a593Smuzhiyun 28*4882a593Smuzhiyun No structure should **EVER** have more than one kobject embedded within it. 29*4882a593Smuzhiyun If it does, the reference counting for the object is sure to be messed 30*4882a593Smuzhiyun up and incorrect, and your code will be buggy. So do not do this. 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun - A ktype is the type of object that embeds a kobject. Every structure 33*4882a593Smuzhiyun that embeds a kobject needs a corresponding ktype. The ktype controls 34*4882a593Smuzhiyun what happens to the kobject when it is created and destroyed. 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun - A kset is a group of kobjects. These kobjects can be of the same ktype 37*4882a593Smuzhiyun or belong to different ktypes. The kset is the basic container type for 38*4882a593Smuzhiyun collections of kobjects. Ksets contain their own kobjects, but you can 39*4882a593Smuzhiyun safely ignore that implementation detail as the kset core code handles 40*4882a593Smuzhiyun this kobject automatically. 41*4882a593Smuzhiyun 42*4882a593Smuzhiyun When you see a sysfs directory full of other directories, generally each 43*4882a593Smuzhiyun of those directories corresponds to a kobject in the same kset. 44*4882a593Smuzhiyun 45*4882a593SmuzhiyunWe'll look at how to create and manipulate all of these types. A bottom-up 46*4882a593Smuzhiyunapproach will be taken, so we'll go back to kobjects. 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun 49*4882a593SmuzhiyunEmbedding kobjects 50*4882a593Smuzhiyun================== 51*4882a593Smuzhiyun 52*4882a593SmuzhiyunIt is rare for kernel code to create a standalone kobject, with one major 53*4882a593Smuzhiyunexception explained below. Instead, kobjects are used to control access to 54*4882a593Smuzhiyuna larger, domain-specific object. To this end, kobjects will be found 55*4882a593Smuzhiyunembedded in other structures. If you are used to thinking of things in 56*4882a593Smuzhiyunobject-oriented terms, kobjects can be seen as a top-level, abstract class 57*4882a593Smuzhiyunfrom which other classes are derived. A kobject implements a set of 58*4882a593Smuzhiyuncapabilities which are not particularly useful by themselves, but are 59*4882a593Smuzhiyunnice to have in other objects. The C language does not allow for the 60*4882a593Smuzhiyundirect expression of inheritance, so other techniques - such as structure 61*4882a593Smuzhiyunembedding - must be used. 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun(As an aside, for those familiar with the kernel linked list implementation, 64*4882a593Smuzhiyunthis is analogous as to how "list_head" structs are rarely useful on 65*4882a593Smuzhiyuntheir own, but are invariably found embedded in the larger objects of 66*4882a593Smuzhiyuninterest.) 67*4882a593Smuzhiyun 68*4882a593SmuzhiyunSo, for example, the UIO code in ``drivers/uio/uio.c`` has a structure that 69*4882a593Smuzhiyundefines the memory region associated with a uio device:: 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun struct uio_map { 72*4882a593Smuzhiyun struct kobject kobj; 73*4882a593Smuzhiyun struct uio_mem *mem; 74*4882a593Smuzhiyun }; 75*4882a593Smuzhiyun 76*4882a593SmuzhiyunIf you have a struct uio_map structure, finding its embedded kobject is 77*4882a593Smuzhiyunjust a matter of using the kobj member. Code that works with kobjects will 78*4882a593Smuzhiyunoften have the opposite problem, however: given a struct kobject pointer, 79*4882a593Smuzhiyunwhat is the pointer to the containing structure? You must avoid tricks 80*4882a593Smuzhiyun(such as assuming that the kobject is at the beginning of the structure) 81*4882a593Smuzhiyunand, instead, use the container_of() macro, found in ``<linux/kernel.h>``:: 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun container_of(ptr, type, member) 84*4882a593Smuzhiyun 85*4882a593Smuzhiyunwhere: 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun * ``ptr`` is the pointer to the embedded kobject, 88*4882a593Smuzhiyun * ``type`` is the type of the containing structure, and 89*4882a593Smuzhiyun * ``member`` is the name of the structure field to which ``pointer`` points. 90*4882a593Smuzhiyun 91*4882a593SmuzhiyunThe return value from container_of() is a pointer to the corresponding 92*4882a593Smuzhiyuncontainer type. So, for example, a pointer ``kp`` to a struct kobject 93*4882a593Smuzhiyunembedded **within** a struct uio_map could be converted to a pointer to the 94*4882a593Smuzhiyun**containing** uio_map structure with:: 95*4882a593Smuzhiyun 96*4882a593Smuzhiyun struct uio_map *u_map = container_of(kp, struct uio_map, kobj); 97*4882a593Smuzhiyun 98*4882a593SmuzhiyunFor convenience, programmers often define a simple macro for **back-casting** 99*4882a593Smuzhiyunkobject pointers to the containing type. Exactly this happens in the 100*4882a593Smuzhiyunearlier ``drivers/uio/uio.c``, as you can see here:: 101*4882a593Smuzhiyun 102*4882a593Smuzhiyun struct uio_map { 103*4882a593Smuzhiyun struct kobject kobj; 104*4882a593Smuzhiyun struct uio_mem *mem; 105*4882a593Smuzhiyun }; 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun #define to_map(map) container_of(map, struct uio_map, kobj) 108*4882a593Smuzhiyun 109*4882a593Smuzhiyunwhere the macro argument "map" is a pointer to the struct kobject in 110*4882a593Smuzhiyunquestion. That macro is subsequently invoked with:: 111*4882a593Smuzhiyun 112*4882a593Smuzhiyun struct uio_map *map = to_map(kobj); 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun 115*4882a593SmuzhiyunInitialization of kobjects 116*4882a593Smuzhiyun========================== 117*4882a593Smuzhiyun 118*4882a593SmuzhiyunCode which creates a kobject must, of course, initialize that object. Some 119*4882a593Smuzhiyunof the internal fields are setup with a (mandatory) call to kobject_init():: 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun void kobject_init(struct kobject *kobj, struct kobj_type *ktype); 122*4882a593Smuzhiyun 123*4882a593SmuzhiyunThe ktype is required for a kobject to be created properly, as every kobject 124*4882a593Smuzhiyunmust have an associated kobj_type. After calling kobject_init(), to 125*4882a593Smuzhiyunregister the kobject with sysfs, the function kobject_add() must be called:: 126*4882a593Smuzhiyun 127*4882a593Smuzhiyun int kobject_add(struct kobject *kobj, struct kobject *parent, 128*4882a593Smuzhiyun const char *fmt, ...); 129*4882a593Smuzhiyun 130*4882a593SmuzhiyunThis sets up the parent of the kobject and the name for the kobject 131*4882a593Smuzhiyunproperly. If the kobject is to be associated with a specific kset, 132*4882a593Smuzhiyunkobj->kset must be assigned before calling kobject_add(). If a kset is 133*4882a593Smuzhiyunassociated with a kobject, then the parent for the kobject can be set to 134*4882a593SmuzhiyunNULL in the call to kobject_add() and then the kobject's parent will be the 135*4882a593Smuzhiyunkset itself. 136*4882a593Smuzhiyun 137*4882a593SmuzhiyunAs the name of the kobject is set when it is added to the kernel, the name 138*4882a593Smuzhiyunof the kobject should never be manipulated directly. If you must change 139*4882a593Smuzhiyunthe name of the kobject, call kobject_rename():: 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun int kobject_rename(struct kobject *kobj, const char *new_name); 142*4882a593Smuzhiyun 143*4882a593Smuzhiyunkobject_rename() does not perform any locking or have a solid notion of 144*4882a593Smuzhiyunwhat names are valid so the caller must provide their own sanity checking 145*4882a593Smuzhiyunand serialization. 146*4882a593Smuzhiyun 147*4882a593SmuzhiyunThere is a function called kobject_set_name() but that is legacy cruft and 148*4882a593Smuzhiyunis being removed. If your code needs to call this function, it is 149*4882a593Smuzhiyunincorrect and needs to be fixed. 150*4882a593Smuzhiyun 151*4882a593SmuzhiyunTo properly access the name of the kobject, use the function 152*4882a593Smuzhiyunkobject_name():: 153*4882a593Smuzhiyun 154*4882a593Smuzhiyun const char *kobject_name(const struct kobject * kobj); 155*4882a593Smuzhiyun 156*4882a593SmuzhiyunThere is a helper function to both initialize and add the kobject to the 157*4882a593Smuzhiyunkernel at the same time, called surprisingly enough kobject_init_and_add():: 158*4882a593Smuzhiyun 159*4882a593Smuzhiyun int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, 160*4882a593Smuzhiyun struct kobject *parent, const char *fmt, ...); 161*4882a593Smuzhiyun 162*4882a593SmuzhiyunThe arguments are the same as the individual kobject_init() and 163*4882a593Smuzhiyunkobject_add() functions described above. 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun 166*4882a593SmuzhiyunUevents 167*4882a593Smuzhiyun======= 168*4882a593Smuzhiyun 169*4882a593SmuzhiyunAfter a kobject has been registered with the kobject core, you need to 170*4882a593Smuzhiyunannounce to the world that it has been created. This can be done with a 171*4882a593Smuzhiyuncall to kobject_uevent():: 172*4882a593Smuzhiyun 173*4882a593Smuzhiyun int kobject_uevent(struct kobject *kobj, enum kobject_action action); 174*4882a593Smuzhiyun 175*4882a593SmuzhiyunUse the **KOBJ_ADD** action for when the kobject is first added to the kernel. 176*4882a593SmuzhiyunThis should be done only after any attributes or children of the kobject 177*4882a593Smuzhiyunhave been initialized properly, as userspace will instantly start to look 178*4882a593Smuzhiyunfor them when this call happens. 179*4882a593Smuzhiyun 180*4882a593SmuzhiyunWhen the kobject is removed from the kernel (details on how to do that are 181*4882a593Smuzhiyunbelow), the uevent for **KOBJ_REMOVE** will be automatically created by the 182*4882a593Smuzhiyunkobject core, so the caller does not have to worry about doing that by 183*4882a593Smuzhiyunhand. 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun 186*4882a593SmuzhiyunReference counts 187*4882a593Smuzhiyun================ 188*4882a593Smuzhiyun 189*4882a593SmuzhiyunOne of the key functions of a kobject is to serve as a reference counter 190*4882a593Smuzhiyunfor the object in which it is embedded. As long as references to the object 191*4882a593Smuzhiyunexist, the object (and the code which supports it) must continue to exist. 192*4882a593SmuzhiyunThe low-level functions for manipulating a kobject's reference counts are:: 193*4882a593Smuzhiyun 194*4882a593Smuzhiyun struct kobject *kobject_get(struct kobject *kobj); 195*4882a593Smuzhiyun void kobject_put(struct kobject *kobj); 196*4882a593Smuzhiyun 197*4882a593SmuzhiyunA successful call to kobject_get() will increment the kobject's reference 198*4882a593Smuzhiyuncounter and return the pointer to the kobject. 199*4882a593Smuzhiyun 200*4882a593SmuzhiyunWhen a reference is released, the call to kobject_put() will decrement the 201*4882a593Smuzhiyunreference count and, possibly, free the object. Note that kobject_init() 202*4882a593Smuzhiyunsets the reference count to one, so the code which sets up the kobject will 203*4882a593Smuzhiyunneed to do a kobject_put() eventually to release that reference. 204*4882a593Smuzhiyun 205*4882a593SmuzhiyunBecause kobjects are dynamic, they must not be declared statically or on 206*4882a593Smuzhiyunthe stack, but instead, always allocated dynamically. Future versions of 207*4882a593Smuzhiyunthe kernel will contain a run-time check for kobjects that are created 208*4882a593Smuzhiyunstatically and will warn the developer of this improper usage. 209*4882a593Smuzhiyun 210*4882a593SmuzhiyunIf all that you want to use a kobject for is to provide a reference counter 211*4882a593Smuzhiyunfor your structure, please use the struct kref instead; a kobject would be 212*4882a593Smuzhiyunoverkill. For more information on how to use struct kref, please see the 213*4882a593Smuzhiyunfile Documentation/core-api/kref.rst in the Linux kernel source tree. 214*4882a593Smuzhiyun 215*4882a593Smuzhiyun 216*4882a593SmuzhiyunCreating "simple" kobjects 217*4882a593Smuzhiyun========================== 218*4882a593Smuzhiyun 219*4882a593SmuzhiyunSometimes all that a developer wants is a way to create a simple directory 220*4882a593Smuzhiyunin the sysfs hierarchy, and not have to mess with the whole complication of 221*4882a593Smuzhiyunksets, show and store functions, and other details. This is the one 222*4882a593Smuzhiyunexception where a single kobject should be created. To create such an 223*4882a593Smuzhiyunentry, use the function:: 224*4882a593Smuzhiyun 225*4882a593Smuzhiyun struct kobject *kobject_create_and_add(const char *name, struct kobject *parent); 226*4882a593Smuzhiyun 227*4882a593SmuzhiyunThis function will create a kobject and place it in sysfs in the location 228*4882a593Smuzhiyununderneath the specified parent kobject. To create simple attributes 229*4882a593Smuzhiyunassociated with this kobject, use:: 230*4882a593Smuzhiyun 231*4882a593Smuzhiyun int sysfs_create_file(struct kobject *kobj, const struct attribute *attr); 232*4882a593Smuzhiyun 233*4882a593Smuzhiyunor:: 234*4882a593Smuzhiyun 235*4882a593Smuzhiyun int sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp); 236*4882a593Smuzhiyun 237*4882a593SmuzhiyunBoth types of attributes used here, with a kobject that has been created 238*4882a593Smuzhiyunwith the kobject_create_and_add(), can be of type kobj_attribute, so no 239*4882a593Smuzhiyunspecial custom attribute is needed to be created. 240*4882a593Smuzhiyun 241*4882a593SmuzhiyunSee the example module, ``samples/kobject/kobject-example.c`` for an 242*4882a593Smuzhiyunimplementation of a simple kobject and attributes. 243*4882a593Smuzhiyun 244*4882a593Smuzhiyun 245*4882a593Smuzhiyun 246*4882a593Smuzhiyunktypes and release methods 247*4882a593Smuzhiyun========================== 248*4882a593Smuzhiyun 249*4882a593SmuzhiyunOne important thing still missing from the discussion is what happens to a 250*4882a593Smuzhiyunkobject when its reference count reaches zero. The code which created the 251*4882a593Smuzhiyunkobject generally does not know when that will happen; if it did, there 252*4882a593Smuzhiyunwould be little point in using a kobject in the first place. Even 253*4882a593Smuzhiyunpredictable object lifecycles become more complicated when sysfs is brought 254*4882a593Smuzhiyunin as other portions of the kernel can get a reference on any kobject that 255*4882a593Smuzhiyunis registered in the system. 256*4882a593Smuzhiyun 257*4882a593SmuzhiyunThe end result is that a structure protected by a kobject cannot be freed 258*4882a593Smuzhiyunbefore its reference count goes to zero. The reference count is not under 259*4882a593Smuzhiyunthe direct control of the code which created the kobject. So that code must 260*4882a593Smuzhiyunbe notified asynchronously whenever the last reference to one of its 261*4882a593Smuzhiyunkobjects goes away. 262*4882a593Smuzhiyun 263*4882a593SmuzhiyunOnce you registered your kobject via kobject_add(), you must never use 264*4882a593Smuzhiyunkfree() to free it directly. The only safe way is to use kobject_put(). It 265*4882a593Smuzhiyunis good practice to always use kobject_put() after kobject_init() to avoid 266*4882a593Smuzhiyunerrors creeping in. 267*4882a593Smuzhiyun 268*4882a593SmuzhiyunThis notification is done through a kobject's release() method. Usually 269*4882a593Smuzhiyunsuch a method has a form like:: 270*4882a593Smuzhiyun 271*4882a593Smuzhiyun void my_object_release(struct kobject *kobj) 272*4882a593Smuzhiyun { 273*4882a593Smuzhiyun struct my_object *mine = container_of(kobj, struct my_object, kobj); 274*4882a593Smuzhiyun 275*4882a593Smuzhiyun /* Perform any additional cleanup on this object, then... */ 276*4882a593Smuzhiyun kfree(mine); 277*4882a593Smuzhiyun } 278*4882a593Smuzhiyun 279*4882a593SmuzhiyunOne important point cannot be overstated: every kobject must have a 280*4882a593Smuzhiyunrelease() method, and the kobject must persist (in a consistent state) 281*4882a593Smuzhiyununtil that method is called. If these constraints are not met, the code is 282*4882a593Smuzhiyunflawed. Note that the kernel will warn you if you forget to provide a 283*4882a593Smuzhiyunrelease() method. Do not try to get rid of this warning by providing an 284*4882a593Smuzhiyun"empty" release function. 285*4882a593Smuzhiyun 286*4882a593SmuzhiyunIf all your cleanup function needs to do is call kfree(), then you must 287*4882a593Smuzhiyuncreate a wrapper function which uses container_of() to upcast to the correct 288*4882a593Smuzhiyuntype (as shown in the example above) and then calls kfree() on the overall 289*4882a593Smuzhiyunstructure. 290*4882a593Smuzhiyun 291*4882a593SmuzhiyunNote, the name of the kobject is available in the release function, but it 292*4882a593Smuzhiyunmust NOT be changed within this callback. Otherwise there will be a memory 293*4882a593Smuzhiyunleak in the kobject core, which makes people unhappy. 294*4882a593Smuzhiyun 295*4882a593SmuzhiyunInterestingly, the release() method is not stored in the kobject itself; 296*4882a593Smuzhiyuninstead, it is associated with the ktype. So let us introduce struct 297*4882a593Smuzhiyunkobj_type:: 298*4882a593Smuzhiyun 299*4882a593Smuzhiyun struct kobj_type { 300*4882a593Smuzhiyun void (*release)(struct kobject *kobj); 301*4882a593Smuzhiyun const struct sysfs_ops *sysfs_ops; 302*4882a593Smuzhiyun struct attribute **default_attrs; 303*4882a593Smuzhiyun const struct attribute_group **default_groups; 304*4882a593Smuzhiyun const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj); 305*4882a593Smuzhiyun const void *(*namespace)(struct kobject *kobj); 306*4882a593Smuzhiyun void (*get_ownership)(struct kobject *kobj, kuid_t *uid, kgid_t *gid); 307*4882a593Smuzhiyun }; 308*4882a593Smuzhiyun 309*4882a593SmuzhiyunThis structure is used to describe a particular type of kobject (or, more 310*4882a593Smuzhiyuncorrectly, of containing object). Every kobject needs to have an associated 311*4882a593Smuzhiyunkobj_type structure; a pointer to that structure must be specified when you 312*4882a593Smuzhiyuncall kobject_init() or kobject_init_and_add(). 313*4882a593Smuzhiyun 314*4882a593SmuzhiyunThe release field in struct kobj_type is, of course, a pointer to the 315*4882a593Smuzhiyunrelease() method for this type of kobject. The other two fields (sysfs_ops 316*4882a593Smuzhiyunand default_attrs) control how objects of this type are represented in 317*4882a593Smuzhiyunsysfs; they are beyond the scope of this document. 318*4882a593Smuzhiyun 319*4882a593SmuzhiyunThe default_attrs pointer is a list of default attributes that will be 320*4882a593Smuzhiyunautomatically created for any kobject that is registered with this ktype. 321*4882a593Smuzhiyun 322*4882a593Smuzhiyun 323*4882a593Smuzhiyunksets 324*4882a593Smuzhiyun===== 325*4882a593Smuzhiyun 326*4882a593SmuzhiyunA kset is merely a collection of kobjects that want to be associated with 327*4882a593Smuzhiyuneach other. There is no restriction that they be of the same ktype, but be 328*4882a593Smuzhiyunvery careful if they are not. 329*4882a593Smuzhiyun 330*4882a593SmuzhiyunA kset serves these functions: 331*4882a593Smuzhiyun 332*4882a593Smuzhiyun - It serves as a bag containing a group of objects. A kset can be used by 333*4882a593Smuzhiyun the kernel to track "all block devices" or "all PCI device drivers." 334*4882a593Smuzhiyun 335*4882a593Smuzhiyun - A kset is also a subdirectory in sysfs, where the associated kobjects 336*4882a593Smuzhiyun with the kset can show up. Every kset contains a kobject which can be 337*4882a593Smuzhiyun set up to be the parent of other kobjects; the top-level directories of 338*4882a593Smuzhiyun the sysfs hierarchy are constructed in this way. 339*4882a593Smuzhiyun 340*4882a593Smuzhiyun - Ksets can support the "hotplugging" of kobjects and influence how 341*4882a593Smuzhiyun uevent events are reported to user space. 342*4882a593Smuzhiyun 343*4882a593SmuzhiyunIn object-oriented terms, "kset" is the top-level container class; ksets 344*4882a593Smuzhiyuncontain their own kobject, but that kobject is managed by the kset code and 345*4882a593Smuzhiyunshould not be manipulated by any other user. 346*4882a593Smuzhiyun 347*4882a593SmuzhiyunA kset keeps its children in a standard kernel linked list. Kobjects point 348*4882a593Smuzhiyunback to their containing kset via their kset field. In almost all cases, 349*4882a593Smuzhiyunthe kobjects belonging to a kset have that kset (or, strictly, its embedded 350*4882a593Smuzhiyunkobject) in their parent. 351*4882a593Smuzhiyun 352*4882a593SmuzhiyunAs a kset contains a kobject within it, it should always be dynamically 353*4882a593Smuzhiyuncreated and never declared statically or on the stack. To create a new 354*4882a593Smuzhiyunkset use:: 355*4882a593Smuzhiyun 356*4882a593Smuzhiyun struct kset *kset_create_and_add(const char *name, 357*4882a593Smuzhiyun const struct kset_uevent_ops *uevent_ops, 358*4882a593Smuzhiyun struct kobject *parent_kobj); 359*4882a593Smuzhiyun 360*4882a593SmuzhiyunWhen you are finished with the kset, call:: 361*4882a593Smuzhiyun 362*4882a593Smuzhiyun void kset_unregister(struct kset *k); 363*4882a593Smuzhiyun 364*4882a593Smuzhiyunto destroy it. This removes the kset from sysfs and decrements its reference 365*4882a593Smuzhiyuncount. When the reference count goes to zero, the kset will be released. 366*4882a593SmuzhiyunBecause other references to the kset may still exist, the release may happen 367*4882a593Smuzhiyunafter kset_unregister() returns. 368*4882a593Smuzhiyun 369*4882a593SmuzhiyunAn example of using a kset can be seen in the 370*4882a593Smuzhiyun``samples/kobject/kset-example.c`` file in the kernel tree. 371*4882a593Smuzhiyun 372*4882a593SmuzhiyunIf a kset wishes to control the uevent operations of the kobjects 373*4882a593Smuzhiyunassociated with it, it can use the struct kset_uevent_ops to handle it:: 374*4882a593Smuzhiyun 375*4882a593Smuzhiyun struct kset_uevent_ops { 376*4882a593Smuzhiyun int (* const filter)(struct kset *kset, struct kobject *kobj); 377*4882a593Smuzhiyun const char *(* const name)(struct kset *kset, struct kobject *kobj); 378*4882a593Smuzhiyun int (* const uevent)(struct kset *kset, struct kobject *kobj, 379*4882a593Smuzhiyun struct kobj_uevent_env *env); 380*4882a593Smuzhiyun }; 381*4882a593Smuzhiyun 382*4882a593Smuzhiyun 383*4882a593SmuzhiyunThe filter function allows a kset to prevent a uevent from being emitted to 384*4882a593Smuzhiyunuserspace for a specific kobject. If the function returns 0, the uevent 385*4882a593Smuzhiyunwill not be emitted. 386*4882a593Smuzhiyun 387*4882a593SmuzhiyunThe name function will be called to override the default name of the kset 388*4882a593Smuzhiyunthat the uevent sends to userspace. By default, the name will be the same 389*4882a593Smuzhiyunas the kset itself, but this function, if present, can override that name. 390*4882a593Smuzhiyun 391*4882a593SmuzhiyunThe uevent function will be called when the uevent is about to be sent to 392*4882a593Smuzhiyunuserspace to allow more environment variables to be added to the uevent. 393*4882a593Smuzhiyun 394*4882a593SmuzhiyunOne might ask how, exactly, a kobject is added to a kset, given that no 395*4882a593Smuzhiyunfunctions which perform that function have been presented. The answer is 396*4882a593Smuzhiyunthat this task is handled by kobject_add(). When a kobject is passed to 397*4882a593Smuzhiyunkobject_add(), its kset member should point to the kset to which the 398*4882a593Smuzhiyunkobject will belong. kobject_add() will handle the rest. 399*4882a593Smuzhiyun 400*4882a593SmuzhiyunIf the kobject belonging to a kset has no parent kobject set, it will be 401*4882a593Smuzhiyunadded to the kset's directory. Not all members of a kset do necessarily 402*4882a593Smuzhiyunlive in the kset directory. If an explicit parent kobject is assigned 403*4882a593Smuzhiyunbefore the kobject is added, the kobject is registered with the kset, but 404*4882a593Smuzhiyunadded below the parent kobject. 405*4882a593Smuzhiyun 406*4882a593Smuzhiyun 407*4882a593SmuzhiyunKobject removal 408*4882a593Smuzhiyun=============== 409*4882a593Smuzhiyun 410*4882a593SmuzhiyunAfter a kobject has been registered with the kobject core successfully, it 411*4882a593Smuzhiyunmust be cleaned up when the code is finished with it. To do that, call 412*4882a593Smuzhiyunkobject_put(). By doing this, the kobject core will automatically clean up 413*4882a593Smuzhiyunall of the memory allocated by this kobject. If a ``KOBJ_ADD`` uevent has been 414*4882a593Smuzhiyunsent for the object, a corresponding ``KOBJ_REMOVE`` uevent will be sent, and 415*4882a593Smuzhiyunany other sysfs housekeeping will be handled for the caller properly. 416*4882a593Smuzhiyun 417*4882a593SmuzhiyunIf you need to do a two-stage delete of the kobject (say you are not 418*4882a593Smuzhiyunallowed to sleep when you need to destroy the object), then call 419*4882a593Smuzhiyunkobject_del() which will unregister the kobject from sysfs. This makes the 420*4882a593Smuzhiyunkobject "invisible", but it is not cleaned up, and the reference count of 421*4882a593Smuzhiyunthe object is still the same. At a later time call kobject_put() to finish 422*4882a593Smuzhiyunthe cleanup of the memory associated with the kobject. 423*4882a593Smuzhiyun 424*4882a593Smuzhiyunkobject_del() can be used to drop the reference to the parent object, if 425*4882a593Smuzhiyuncircular references are constructed. It is valid in some cases, that a 426*4882a593Smuzhiyunparent objects references a child. Circular references _must_ be broken 427*4882a593Smuzhiyunwith an explicit call to kobject_del(), so that a release functions will be 428*4882a593Smuzhiyuncalled, and the objects in the former circle release each other. 429*4882a593Smuzhiyun 430*4882a593Smuzhiyun 431*4882a593SmuzhiyunExample code to copy from 432*4882a593Smuzhiyun========================= 433*4882a593Smuzhiyun 434*4882a593SmuzhiyunFor a more complete example of using ksets and kobjects properly, see the 435*4882a593Smuzhiyunexample programs ``samples/kobject/{kobject-example.c,kset-example.c}``, 436*4882a593Smuzhiyunwhich will be built as loadable modules if you select ``CONFIG_SAMPLE_KOBJECT``. 437