1*5fed00dcSSameer Rahmani===================================================================== 2*5fed00dcSSameer RahmaniEverything you never wanted to know about kobjects, ksets, and ktypes 3*5fed00dcSSameer Rahmani===================================================================== 4*5fed00dcSSameer Rahmani 5*5fed00dcSSameer Rahmani:Author: Greg Kroah-Hartman <gregkh@linuxfoundation.org> 6*5fed00dcSSameer Rahmani:Last updated: December 19, 2007 7*5fed00dcSSameer Rahmani 8*5fed00dcSSameer RahmaniBased on an original article by Jon Corbet for lwn.net written October 1, 9*5fed00dcSSameer Rahmani2003 and located at http://lwn.net/Articles/51437/ 10*5fed00dcSSameer Rahmani 11*5fed00dcSSameer RahmaniPart of the difficulty in understanding the driver model - and the kobject 12*5fed00dcSSameer Rahmaniabstraction upon which it is built - is that there is no obvious starting 13*5fed00dcSSameer Rahmaniplace. Dealing with kobjects requires understanding a few different types, 14*5fed00dcSSameer Rahmaniall of which make reference to each other. In an attempt to make things 15*5fed00dcSSameer Rahmanieasier, we'll take a multi-pass approach, starting with vague terms and 16*5fed00dcSSameer Rahmaniadding detail as we go. To that end, here are some quick definitions of 17*5fed00dcSSameer Rahmanisome terms we will be working with. 18*5fed00dcSSameer Rahmani 19*5fed00dcSSameer Rahmani - A kobject is an object of type struct kobject. Kobjects have a name 20*5fed00dcSSameer Rahmani and a reference count. A kobject also has a parent pointer (allowing 21*5fed00dcSSameer Rahmani objects to be arranged into hierarchies), a specific type, and, 22*5fed00dcSSameer Rahmani usually, a representation in the sysfs virtual filesystem. 23*5fed00dcSSameer Rahmani 24*5fed00dcSSameer Rahmani Kobjects are generally not interesting on their own; instead, they are 25*5fed00dcSSameer Rahmani usually embedded within some other structure which contains the stuff 26*5fed00dcSSameer Rahmani the code is really interested in. 27*5fed00dcSSameer Rahmani 28*5fed00dcSSameer Rahmani No structure should **EVER** have more than one kobject embedded within it. 29*5fed00dcSSameer Rahmani If it does, the reference counting for the object is sure to be messed 30*5fed00dcSSameer Rahmani up and incorrect, and your code will be buggy. So do not do this. 31*5fed00dcSSameer Rahmani 32*5fed00dcSSameer Rahmani - A ktype is the type of object that embeds a kobject. Every structure 33*5fed00dcSSameer Rahmani that embeds a kobject needs a corresponding ktype. The ktype controls 34*5fed00dcSSameer Rahmani what happens to the kobject when it is created and destroyed. 35*5fed00dcSSameer Rahmani 36*5fed00dcSSameer Rahmani - A kset is a group of kobjects. These kobjects can be of the same ktype 37*5fed00dcSSameer Rahmani or belong to different ktypes. The kset is the basic container type for 38*5fed00dcSSameer Rahmani collections of kobjects. Ksets contain their own kobjects, but you can 39*5fed00dcSSameer Rahmani safely ignore that implementation detail as the kset core code handles 40*5fed00dcSSameer Rahmani this kobject automatically. 41*5fed00dcSSameer Rahmani 42*5fed00dcSSameer Rahmani When you see a sysfs directory full of other directories, generally each 43*5fed00dcSSameer Rahmani of those directories corresponds to a kobject in the same kset. 44*5fed00dcSSameer Rahmani 45*5fed00dcSSameer RahmaniWe'll look at how to create and manipulate all of these types. A bottom-up 46*5fed00dcSSameer Rahmaniapproach will be taken, so we'll go back to kobjects. 47*5fed00dcSSameer Rahmani 48*5fed00dcSSameer Rahmani 49*5fed00dcSSameer RahmaniEmbedding kobjects 50*5fed00dcSSameer Rahmani================== 51*5fed00dcSSameer Rahmani 52*5fed00dcSSameer RahmaniIt is rare for kernel code to create a standalone kobject, with one major 53*5fed00dcSSameer Rahmaniexception explained below. Instead, kobjects are used to control access to 54*5fed00dcSSameer Rahmania larger, domain-specific object. To this end, kobjects will be found 55*5fed00dcSSameer Rahmaniembedded in other structures. If you are used to thinking of things in 56*5fed00dcSSameer Rahmaniobject-oriented terms, kobjects can be seen as a top-level, abstract class 57*5fed00dcSSameer Rahmanifrom which other classes are derived. A kobject implements a set of 58*5fed00dcSSameer Rahmanicapabilities which are not particularly useful by themselves, but are 59*5fed00dcSSameer Rahmaninice to have in other objects. The C language does not allow for the 60*5fed00dcSSameer Rahmanidirect expression of inheritance, so other techniques - such as structure 61*5fed00dcSSameer Rahmaniembedding - must be used. 62*5fed00dcSSameer Rahmani 63*5fed00dcSSameer Rahmani(As an aside, for those familiar with the kernel linked list implementation, 64*5fed00dcSSameer Rahmanithis is analogous as to how "list_head" structs are rarely useful on 65*5fed00dcSSameer Rahmanitheir own, but are invariably found embedded in the larger objects of 66*5fed00dcSSameer Rahmaniinterest.) 67*5fed00dcSSameer Rahmani 68*5fed00dcSSameer RahmaniSo, for example, the UIO code in ``drivers/uio/uio.c`` has a structure that 69*5fed00dcSSameer Rahmanidefines the memory region associated with a uio device:: 70*5fed00dcSSameer Rahmani 71*5fed00dcSSameer Rahmani struct uio_map { 72*5fed00dcSSameer Rahmani struct kobject kobj; 73*5fed00dcSSameer Rahmani struct uio_mem *mem; 74*5fed00dcSSameer Rahmani }; 75*5fed00dcSSameer Rahmani 76*5fed00dcSSameer RahmaniIf you have a struct uio_map structure, finding its embedded kobject is 77*5fed00dcSSameer Rahmanijust a matter of using the kobj member. Code that works with kobjects will 78*5fed00dcSSameer Rahmanioften have the opposite problem, however: given a struct kobject pointer, 79*5fed00dcSSameer Rahmaniwhat is the pointer to the containing structure? You must avoid tricks 80*5fed00dcSSameer Rahmani(such as assuming that the kobject is at the beginning of the structure) 81*5fed00dcSSameer Rahmaniand, instead, use the container_of() macro, found in ``<linux/kernel.h>``:: 82*5fed00dcSSameer Rahmani 83*5fed00dcSSameer Rahmani container_of(pointer, type, member) 84*5fed00dcSSameer Rahmani 85*5fed00dcSSameer Rahmaniwhere: 86*5fed00dcSSameer Rahmani 87*5fed00dcSSameer Rahmani * ``pointer`` is the pointer to the embedded kobject, 88*5fed00dcSSameer Rahmani * ``type`` is the type of the containing structure, and 89*5fed00dcSSameer Rahmani * ``member`` is the name of the structure field to which ``pointer`` points. 90*5fed00dcSSameer Rahmani 91*5fed00dcSSameer RahmaniThe return value from container_of() is a pointer to the corresponding 92*5fed00dcSSameer Rahmanicontainer type. So, for example, a pointer ``kp`` to a struct kobject 93*5fed00dcSSameer Rahmaniembedded **within** a struct uio_map could be converted to a pointer to the 94*5fed00dcSSameer Rahmani**containing** uio_map structure with:: 95*5fed00dcSSameer Rahmani 96*5fed00dcSSameer Rahmani struct uio_map *u_map = container_of(kp, struct uio_map, kobj); 97*5fed00dcSSameer Rahmani 98*5fed00dcSSameer RahmaniFor convenience, programmers often define a simple macro for **back-casting** 99*5fed00dcSSameer Rahmanikobject pointers to the containing type. Exactly this happens in the 100*5fed00dcSSameer Rahmaniearlier ``drivers/uio/uio.c``, as you can see here:: 101*5fed00dcSSameer Rahmani 102*5fed00dcSSameer Rahmani struct uio_map { 103*5fed00dcSSameer Rahmani struct kobject kobj; 104*5fed00dcSSameer Rahmani struct uio_mem *mem; 105*5fed00dcSSameer Rahmani }; 106*5fed00dcSSameer Rahmani 107*5fed00dcSSameer Rahmani #define to_map(map) container_of(map, struct uio_map, kobj) 108*5fed00dcSSameer Rahmani 109*5fed00dcSSameer Rahmaniwhere the macro argument "map" is a pointer to the struct kobject in 110*5fed00dcSSameer Rahmaniquestion. That macro is subsequently invoked with:: 111*5fed00dcSSameer Rahmani 112*5fed00dcSSameer Rahmani struct uio_map *map = to_map(kobj); 113*5fed00dcSSameer Rahmani 114*5fed00dcSSameer Rahmani 115*5fed00dcSSameer RahmaniInitialization of kobjects 116*5fed00dcSSameer Rahmani========================== 117*5fed00dcSSameer Rahmani 118*5fed00dcSSameer RahmaniCode which creates a kobject must, of course, initialize that object. Some 119*5fed00dcSSameer Rahmaniof the internal fields are setup with a (mandatory) call to kobject_init():: 120*5fed00dcSSameer Rahmani 121*5fed00dcSSameer Rahmani void kobject_init(struct kobject *kobj, struct kobj_type *ktype); 122*5fed00dcSSameer Rahmani 123*5fed00dcSSameer RahmaniThe ktype is required for a kobject to be created properly, as every kobject 124*5fed00dcSSameer Rahmanimust have an associated kobj_type. After calling kobject_init(), to 125*5fed00dcSSameer Rahmaniregister the kobject with sysfs, the function kobject_add() must be called:: 126*5fed00dcSSameer Rahmani 127*5fed00dcSSameer Rahmani int kobject_add(struct kobject *kobj, struct kobject *parent, 128*5fed00dcSSameer Rahmani const char *fmt, ...); 129*5fed00dcSSameer Rahmani 130*5fed00dcSSameer RahmaniThis sets up the parent of the kobject and the name for the kobject 131*5fed00dcSSameer Rahmaniproperly. If the kobject is to be associated with a specific kset, 132*5fed00dcSSameer Rahmanikobj->kset must be assigned before calling kobject_add(). If a kset is 133*5fed00dcSSameer Rahmaniassociated with a kobject, then the parent for the kobject can be set to 134*5fed00dcSSameer RahmaniNULL in the call to kobject_add() and then the kobject's parent will be the 135*5fed00dcSSameer Rahmanikset itself. 136*5fed00dcSSameer Rahmani 137*5fed00dcSSameer RahmaniAs the name of the kobject is set when it is added to the kernel, the name 138*5fed00dcSSameer Rahmaniof the kobject should never be manipulated directly. If you must change 139*5fed00dcSSameer Rahmanithe name of the kobject, call kobject_rename():: 140*5fed00dcSSameer Rahmani 141*5fed00dcSSameer Rahmani int kobject_rename(struct kobject *kobj, const char *new_name); 142*5fed00dcSSameer Rahmani 143*5fed00dcSSameer Rahmanikobject_rename does not perform any locking or have a solid notion of 144*5fed00dcSSameer Rahmaniwhat names are valid so the caller must provide their own sanity checking 145*5fed00dcSSameer Rahmaniand serialization. 146*5fed00dcSSameer Rahmani 147*5fed00dcSSameer RahmaniThere is a function called kobject_set_name() but that is legacy cruft and 148*5fed00dcSSameer Rahmaniis being removed. If your code needs to call this function, it is 149*5fed00dcSSameer Rahmaniincorrect and needs to be fixed. 150*5fed00dcSSameer Rahmani 151*5fed00dcSSameer RahmaniTo properly access the name of the kobject, use the function 152*5fed00dcSSameer Rahmanikobject_name():: 153*5fed00dcSSameer Rahmani 154*5fed00dcSSameer Rahmani const char *kobject_name(const struct kobject * kobj); 155*5fed00dcSSameer Rahmani 156*5fed00dcSSameer RahmaniThere is a helper function to both initialize and add the kobject to the 157*5fed00dcSSameer Rahmanikernel at the same time, called surprisingly enough kobject_init_and_add():: 158*5fed00dcSSameer Rahmani 159*5fed00dcSSameer Rahmani int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, 160*5fed00dcSSameer Rahmani struct kobject *parent, const char *fmt, ...); 161*5fed00dcSSameer Rahmani 162*5fed00dcSSameer RahmaniThe arguments are the same as the individual kobject_init() and 163*5fed00dcSSameer Rahmanikobject_add() functions described above. 164*5fed00dcSSameer Rahmani 165*5fed00dcSSameer Rahmani 166*5fed00dcSSameer RahmaniUevents 167*5fed00dcSSameer Rahmani======= 168*5fed00dcSSameer Rahmani 169*5fed00dcSSameer RahmaniAfter a kobject has been registered with the kobject core, you need to 170*5fed00dcSSameer Rahmaniannounce to the world that it has been created. This can be done with a 171*5fed00dcSSameer Rahmanicall to kobject_uevent():: 172*5fed00dcSSameer Rahmani 173*5fed00dcSSameer Rahmani int kobject_uevent(struct kobject *kobj, enum kobject_action action); 174*5fed00dcSSameer Rahmani 175*5fed00dcSSameer RahmaniUse the **KOBJ_ADD** action for when the kobject is first added to the kernel. 176*5fed00dcSSameer RahmaniThis should be done only after any attributes or children of the kobject 177*5fed00dcSSameer Rahmanihave been initialized properly, as userspace will instantly start to look 178*5fed00dcSSameer Rahmanifor them when this call happens. 179*5fed00dcSSameer Rahmani 180*5fed00dcSSameer RahmaniWhen the kobject is removed from the kernel (details on how to do that are 181*5fed00dcSSameer Rahmanibelow), the uevent for **KOBJ_REMOVE** will be automatically created by the 182*5fed00dcSSameer Rahmanikobject core, so the caller does not have to worry about doing that by 183*5fed00dcSSameer Rahmanihand. 184*5fed00dcSSameer Rahmani 185*5fed00dcSSameer Rahmani 186*5fed00dcSSameer RahmaniReference counts 187*5fed00dcSSameer Rahmani================ 188*5fed00dcSSameer Rahmani 189*5fed00dcSSameer RahmaniOne of the key functions of a kobject is to serve as a reference counter 190*5fed00dcSSameer Rahmanifor the object in which it is embedded. As long as references to the object 191*5fed00dcSSameer Rahmaniexist, the object (and the code which supports it) must continue to exist. 192*5fed00dcSSameer RahmaniThe low-level functions for manipulating a kobject's reference counts are:: 193*5fed00dcSSameer Rahmani 194*5fed00dcSSameer Rahmani struct kobject *kobject_get(struct kobject *kobj); 195*5fed00dcSSameer Rahmani void kobject_put(struct kobject *kobj); 196*5fed00dcSSameer Rahmani 197*5fed00dcSSameer RahmaniA successful call to kobject_get() will increment the kobject's reference 198*5fed00dcSSameer Rahmanicounter and return the pointer to the kobject. 199*5fed00dcSSameer Rahmani 200*5fed00dcSSameer RahmaniWhen a reference is released, the call to kobject_put() will decrement the 201*5fed00dcSSameer Rahmanireference count and, possibly, free the object. Note that kobject_init() 202*5fed00dcSSameer Rahmanisets the reference count to one, so the code which sets up the kobject will 203*5fed00dcSSameer Rahmanineed to do a kobject_put() eventually to release that reference. 204*5fed00dcSSameer Rahmani 205*5fed00dcSSameer RahmaniBecause kobjects are dynamic, they must not be declared statically or on 206*5fed00dcSSameer Rahmanithe stack, but instead, always allocated dynamically. Future versions of 207*5fed00dcSSameer Rahmanithe kernel will contain a run-time check for kobjects that are created 208*5fed00dcSSameer Rahmanistatically and will warn the developer of this improper usage. 209*5fed00dcSSameer Rahmani 210*5fed00dcSSameer RahmaniIf all that you want to use a kobject for is to provide a reference counter 211*5fed00dcSSameer Rahmanifor your structure, please use the struct kref instead; a kobject would be 212*5fed00dcSSameer Rahmanioverkill. For more information on how to use struct kref, please see the 213*5fed00dcSSameer Rahmanifile Documentation/kref.txt in the Linux kernel source tree. 214*5fed00dcSSameer Rahmani 215*5fed00dcSSameer Rahmani 216*5fed00dcSSameer RahmaniCreating "simple" kobjects 217*5fed00dcSSameer Rahmani========================== 218*5fed00dcSSameer Rahmani 219*5fed00dcSSameer RahmaniSometimes all that a developer wants is a way to create a simple directory 220*5fed00dcSSameer Rahmaniin the sysfs hierarchy, and not have to mess with the whole complication of 221*5fed00dcSSameer Rahmaniksets, show and store functions, and other details. This is the one 222*5fed00dcSSameer Rahmaniexception where a single kobject should be created. To create such an 223*5fed00dcSSameer Rahmanientry, use the function:: 224*5fed00dcSSameer Rahmani 225*5fed00dcSSameer Rahmani struct kobject *kobject_create_and_add(char *name, struct kobject *parent); 226*5fed00dcSSameer Rahmani 227*5fed00dcSSameer RahmaniThis function will create a kobject and place it in sysfs in the location 228*5fed00dcSSameer Rahmaniunderneath the specified parent kobject. To create simple attributes 229*5fed00dcSSameer Rahmaniassociated with this kobject, use:: 230*5fed00dcSSameer Rahmani 231*5fed00dcSSameer Rahmani int sysfs_create_file(struct kobject *kobj, struct attribute *attr); 232*5fed00dcSSameer Rahmani 233*5fed00dcSSameer Rahmanior:: 234*5fed00dcSSameer Rahmani 235*5fed00dcSSameer Rahmani int sysfs_create_group(struct kobject *kobj, struct attribute_group *grp); 236*5fed00dcSSameer Rahmani 237*5fed00dcSSameer RahmaniBoth types of attributes used here, with a kobject that has been created 238*5fed00dcSSameer Rahmaniwith the kobject_create_and_add(), can be of type kobj_attribute, so no 239*5fed00dcSSameer Rahmanispecial custom attribute is needed to be created. 240*5fed00dcSSameer Rahmani 241*5fed00dcSSameer RahmaniSee the example module, ``samples/kobject/kobject-example.c`` for an 242*5fed00dcSSameer Rahmaniimplementation of a simple kobject and attributes. 243*5fed00dcSSameer Rahmani 244*5fed00dcSSameer Rahmani 245*5fed00dcSSameer Rahmani 246*5fed00dcSSameer Rahmaniktypes and release methods 247*5fed00dcSSameer Rahmani========================== 248*5fed00dcSSameer Rahmani 249*5fed00dcSSameer RahmaniOne important thing still missing from the discussion is what happens to a 250*5fed00dcSSameer Rahmanikobject when its reference count reaches zero. The code which created the 251*5fed00dcSSameer Rahmanikobject generally does not know when that will happen; if it did, there 252*5fed00dcSSameer Rahmaniwould be little point in using a kobject in the first place. Even 253*5fed00dcSSameer Rahmanipredictable object lifecycles become more complicated when sysfs is brought 254*5fed00dcSSameer Rahmaniin as other portions of the kernel can get a reference on any kobject that 255*5fed00dcSSameer Rahmaniis registered in the system. 256*5fed00dcSSameer Rahmani 257*5fed00dcSSameer RahmaniThe end result is that a structure protected by a kobject cannot be freed 258*5fed00dcSSameer Rahmanibefore its reference count goes to zero. The reference count is not under 259*5fed00dcSSameer Rahmanithe direct control of the code which created the kobject. So that code must 260*5fed00dcSSameer Rahmanibe notified asynchronously whenever the last reference to one of its 261*5fed00dcSSameer Rahmanikobjects goes away. 262*5fed00dcSSameer Rahmani 263*5fed00dcSSameer RahmaniOnce you registered your kobject via kobject_add(), you must never use 264*5fed00dcSSameer Rahmanikfree() to free it directly. The only safe way is to use kobject_put(). It 265*5fed00dcSSameer Rahmaniis good practice to always use kobject_put() after kobject_init() to avoid 266*5fed00dcSSameer Rahmanierrors creeping in. 267*5fed00dcSSameer Rahmani 268*5fed00dcSSameer RahmaniThis notification is done through a kobject's release() method. Usually 269*5fed00dcSSameer Rahmanisuch a method has a form like:: 270*5fed00dcSSameer Rahmani 271*5fed00dcSSameer Rahmani void my_object_release(struct kobject *kobj) 272*5fed00dcSSameer Rahmani { 273*5fed00dcSSameer Rahmani struct my_object *mine = container_of(kobj, struct my_object, kobj); 274*5fed00dcSSameer Rahmani 275*5fed00dcSSameer Rahmani /* Perform any additional cleanup on this object, then... */ 276*5fed00dcSSameer Rahmani kfree(mine); 277*5fed00dcSSameer Rahmani } 278*5fed00dcSSameer Rahmani 279*5fed00dcSSameer RahmaniOne important point cannot be overstated: every kobject must have a 280*5fed00dcSSameer Rahmanirelease() method, and the kobject must persist (in a consistent state) 281*5fed00dcSSameer Rahmaniuntil that method is called. If these constraints are not met, the code is 282*5fed00dcSSameer Rahmaniflawed. Note that the kernel will warn you if you forget to provide a 283*5fed00dcSSameer Rahmanirelease() method. Do not try to get rid of this warning by providing an 284*5fed00dcSSameer Rahmani"empty" release function. 285*5fed00dcSSameer Rahmani 286*5fed00dcSSameer RahmaniIf all your cleanup function needs to do is call kfree(), then you must 287*5fed00dcSSameer Rahmanicreate a wrapper function which uses container_of() to upcast to the correct 288*5fed00dcSSameer Rahmanitype (as shown in the example above) and then calls kfree() on the overall 289*5fed00dcSSameer Rahmanistructure. 290*5fed00dcSSameer Rahmani 291*5fed00dcSSameer RahmaniNote, the name of the kobject is available in the release function, but it 292*5fed00dcSSameer Rahmanimust NOT be changed within this callback. Otherwise there will be a memory 293*5fed00dcSSameer Rahmanileak in the kobject core, which makes people unhappy. 294*5fed00dcSSameer Rahmani 295*5fed00dcSSameer RahmaniInterestingly, the release() method is not stored in the kobject itself; 296*5fed00dcSSameer Rahmaniinstead, it is associated with the ktype. So let us introduce struct 297*5fed00dcSSameer Rahmanikobj_type:: 298*5fed00dcSSameer Rahmani 299*5fed00dcSSameer Rahmani struct kobj_type { 300*5fed00dcSSameer Rahmani void (*release)(struct kobject *kobj); 301*5fed00dcSSameer Rahmani const struct sysfs_ops *sysfs_ops; 302*5fed00dcSSameer Rahmani struct attribute **default_attrs; 303*5fed00dcSSameer Rahmani const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj); 304*5fed00dcSSameer Rahmani const void *(*namespace)(struct kobject *kobj); 305*5fed00dcSSameer Rahmani }; 306*5fed00dcSSameer Rahmani 307*5fed00dcSSameer RahmaniThis structure is used to describe a particular type of kobject (or, more 308*5fed00dcSSameer Rahmanicorrectly, of containing object). Every kobject needs to have an associated 309*5fed00dcSSameer Rahmanikobj_type structure; a pointer to that structure must be specified when you 310*5fed00dcSSameer Rahmanicall kobject_init() or kobject_init_and_add(). 311*5fed00dcSSameer Rahmani 312*5fed00dcSSameer RahmaniThe release field in struct kobj_type is, of course, a pointer to the 313*5fed00dcSSameer Rahmanirelease() method for this type of kobject. The other two fields (sysfs_ops 314*5fed00dcSSameer Rahmaniand default_attrs) control how objects of this type are represented in 315*5fed00dcSSameer Rahmanisysfs; they are beyond the scope of this document. 316*5fed00dcSSameer Rahmani 317*5fed00dcSSameer RahmaniThe default_attrs pointer is a list of default attributes that will be 318*5fed00dcSSameer Rahmaniautomatically created for any kobject that is registered with this ktype. 319*5fed00dcSSameer Rahmani 320*5fed00dcSSameer Rahmani 321*5fed00dcSSameer Rahmaniksets 322*5fed00dcSSameer Rahmani===== 323*5fed00dcSSameer Rahmani 324*5fed00dcSSameer RahmaniA kset is merely a collection of kobjects that want to be associated with 325*5fed00dcSSameer Rahmanieach other. There is no restriction that they be of the same ktype, but be 326*5fed00dcSSameer Rahmanivery careful if they are not. 327*5fed00dcSSameer Rahmani 328*5fed00dcSSameer RahmaniA kset serves these functions: 329*5fed00dcSSameer Rahmani 330*5fed00dcSSameer Rahmani - It serves as a bag containing a group of objects. A kset can be used by 331*5fed00dcSSameer Rahmani the kernel to track "all block devices" or "all PCI device drivers." 332*5fed00dcSSameer Rahmani 333*5fed00dcSSameer Rahmani - A kset is also a subdirectory in sysfs, where the associated kobjects 334*5fed00dcSSameer Rahmani with the kset can show up. Every kset contains a kobject which can be 335*5fed00dcSSameer Rahmani set up to be the parent of other kobjects; the top-level directories of 336*5fed00dcSSameer Rahmani the sysfs hierarchy are constructed in this way. 337*5fed00dcSSameer Rahmani 338*5fed00dcSSameer Rahmani - Ksets can support the "hotplugging" of kobjects and influence how 339*5fed00dcSSameer Rahmani uevent events are reported to user space. 340*5fed00dcSSameer Rahmani 341*5fed00dcSSameer RahmaniIn object-oriented terms, "kset" is the top-level container class; ksets 342*5fed00dcSSameer Rahmanicontain their own kobject, but that kobject is managed by the kset code and 343*5fed00dcSSameer Rahmanishould not be manipulated by any other user. 344*5fed00dcSSameer Rahmani 345*5fed00dcSSameer RahmaniA kset keeps its children in a standard kernel linked list. Kobjects point 346*5fed00dcSSameer Rahmaniback to their containing kset via their kset field. In almost all cases, 347*5fed00dcSSameer Rahmanithe kobjects belonging to a kset have that kset (or, strictly, its embedded 348*5fed00dcSSameer Rahmanikobject) in their parent. 349*5fed00dcSSameer Rahmani 350*5fed00dcSSameer RahmaniAs a kset contains a kobject within it, it should always be dynamically 351*5fed00dcSSameer Rahmanicreated and never declared statically or on the stack. To create a new 352*5fed00dcSSameer Rahmanikset use:: 353*5fed00dcSSameer Rahmani 354*5fed00dcSSameer Rahmani struct kset *kset_create_and_add(const char *name, 355*5fed00dcSSameer Rahmani struct kset_uevent_ops *u, 356*5fed00dcSSameer Rahmani struct kobject *parent); 357*5fed00dcSSameer Rahmani 358*5fed00dcSSameer RahmaniWhen you are finished with the kset, call:: 359*5fed00dcSSameer Rahmani 360*5fed00dcSSameer Rahmani void kset_unregister(struct kset *kset); 361*5fed00dcSSameer Rahmani 362*5fed00dcSSameer Rahmanito destroy it. This removes the kset from sysfs and decrements its reference 363*5fed00dcSSameer Rahmanicount. When the reference count goes to zero, the kset will be released. 364*5fed00dcSSameer RahmaniBecause other references to the kset may still exist, the release may happen 365*5fed00dcSSameer Rahmaniafter kset_unregister() returns. 366*5fed00dcSSameer Rahmani 367*5fed00dcSSameer RahmaniAn example of using a kset can be seen in the 368*5fed00dcSSameer Rahmani``samples/kobject/kset-example.c`` file in the kernel tree. 369*5fed00dcSSameer Rahmani 370*5fed00dcSSameer RahmaniIf a kset wishes to control the uevent operations of the kobjects 371*5fed00dcSSameer Rahmaniassociated with it, it can use the struct kset_uevent_ops to handle it:: 372*5fed00dcSSameer Rahmani 373*5fed00dcSSameer Rahmani struct kset_uevent_ops { 374*5fed00dcSSameer Rahmani int (*filter)(struct kset *kset, struct kobject *kobj); 375*5fed00dcSSameer Rahmani const char *(*name)(struct kset *kset, struct kobject *kobj); 376*5fed00dcSSameer Rahmani int (*uevent)(struct kset *kset, struct kobject *kobj, 377*5fed00dcSSameer Rahmani struct kobj_uevent_env *env); 378*5fed00dcSSameer Rahmani }; 379*5fed00dcSSameer Rahmani 380*5fed00dcSSameer Rahmani 381*5fed00dcSSameer RahmaniThe filter function allows a kset to prevent a uevent from being emitted to 382*5fed00dcSSameer Rahmaniuserspace for a specific kobject. If the function returns 0, the uevent 383*5fed00dcSSameer Rahmaniwill not be emitted. 384*5fed00dcSSameer Rahmani 385*5fed00dcSSameer RahmaniThe name function will be called to override the default name of the kset 386*5fed00dcSSameer Rahmanithat the uevent sends to userspace. By default, the name will be the same 387*5fed00dcSSameer Rahmanias the kset itself, but this function, if present, can override that name. 388*5fed00dcSSameer Rahmani 389*5fed00dcSSameer RahmaniThe uevent function will be called when the uevent is about to be sent to 390*5fed00dcSSameer Rahmaniuserspace to allow more environment variables to be added to the uevent. 391*5fed00dcSSameer Rahmani 392*5fed00dcSSameer RahmaniOne might ask how, exactly, a kobject is added to a kset, given that no 393*5fed00dcSSameer Rahmanifunctions which perform that function have been presented. The answer is 394*5fed00dcSSameer Rahmanithat this task is handled by kobject_add(). When a kobject is passed to 395*5fed00dcSSameer Rahmanikobject_add(), its kset member should point to the kset to which the 396*5fed00dcSSameer Rahmanikobject will belong. kobject_add() will handle the rest. 397*5fed00dcSSameer Rahmani 398*5fed00dcSSameer RahmaniIf the kobject belonging to a kset has no parent kobject set, it will be 399*5fed00dcSSameer Rahmaniadded to the kset's directory. Not all members of a kset do necessarily 400*5fed00dcSSameer Rahmanilive in the kset directory. If an explicit parent kobject is assigned 401*5fed00dcSSameer Rahmanibefore the kobject is added, the kobject is registered with the kset, but 402*5fed00dcSSameer Rahmaniadded below the parent kobject. 403*5fed00dcSSameer Rahmani 404*5fed00dcSSameer Rahmani 405*5fed00dcSSameer RahmaniKobject removal 406*5fed00dcSSameer Rahmani=============== 407*5fed00dcSSameer Rahmani 408*5fed00dcSSameer RahmaniAfter a kobject has been registered with the kobject core successfully, it 409*5fed00dcSSameer Rahmanimust be cleaned up when the code is finished with it. To do that, call 410*5fed00dcSSameer Rahmanikobject_put(). By doing this, the kobject core will automatically clean up 411*5fed00dcSSameer Rahmaniall of the memory allocated by this kobject. If a ``KOBJ_ADD`` uevent has been 412*5fed00dcSSameer Rahmanisent for the object, a corresponding ``KOBJ_REMOVE`` uevent will be sent, and 413*5fed00dcSSameer Rahmaniany other sysfs housekeeping will be handled for the caller properly. 414*5fed00dcSSameer Rahmani 415*5fed00dcSSameer RahmaniIf you need to do a two-stage delete of the kobject (say you are not 416*5fed00dcSSameer Rahmaniallowed to sleep when you need to destroy the object), then call 417*5fed00dcSSameer Rahmanikobject_del() which will unregister the kobject from sysfs. This makes the 418*5fed00dcSSameer Rahmanikobject "invisible", but it is not cleaned up, and the reference count of 419*5fed00dcSSameer Rahmanithe object is still the same. At a later time call kobject_put() to finish 420*5fed00dcSSameer Rahmanithe cleanup of the memory associated with the kobject. 421*5fed00dcSSameer Rahmani 422*5fed00dcSSameer Rahmanikobject_del() can be used to drop the reference to the parent object, if 423*5fed00dcSSameer Rahmanicircular references are constructed. It is valid in some cases, that a 424*5fed00dcSSameer Rahmaniparent objects references a child. Circular references _must_ be broken 425*5fed00dcSSameer Rahmaniwith an explicit call to kobject_del(), so that a release functions will be 426*5fed00dcSSameer Rahmanicalled, and the objects in the former circle release each other. 427*5fed00dcSSameer Rahmani 428*5fed00dcSSameer Rahmani 429*5fed00dcSSameer RahmaniExample code to copy from 430*5fed00dcSSameer Rahmani========================= 431*5fed00dcSSameer Rahmani 432*5fed00dcSSameer RahmaniFor a more complete example of using ksets and kobjects properly, see the 433*5fed00dcSSameer Rahmaniexample programs ``samples/kobject/{kobject-example.c,kset-example.c}``, 434*5fed00dcSSameer Rahmaniwhich will be built as loadable modules if you select ``CONFIG_SAMPLE_KOBJECT``. 435