15fed00dcSSameer Rahmani=====================================================================
25fed00dcSSameer RahmaniEverything you never wanted to know about kobjects, ksets, and ktypes
35fed00dcSSameer Rahmani=====================================================================
45fed00dcSSameer Rahmani
55fed00dcSSameer Rahmani:Author: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
65fed00dcSSameer Rahmani:Last updated: December 19, 2007
75fed00dcSSameer Rahmani
85fed00dcSSameer RahmaniBased on an original article by Jon Corbet for lwn.net written October 1,
95fed00dcSSameer Rahmani2003 and located at http://lwn.net/Articles/51437/
105fed00dcSSameer Rahmani
115fed00dcSSameer RahmaniPart of the difficulty in understanding the driver model - and the kobject
125fed00dcSSameer Rahmaniabstraction upon which it is built - is that there is no obvious starting
135fed00dcSSameer Rahmaniplace. Dealing with kobjects requires understanding a few different types,
145fed00dcSSameer Rahmaniall of which make reference to each other. In an attempt to make things
155fed00dcSSameer Rahmanieasier, we'll take a multi-pass approach, starting with vague terms and
165fed00dcSSameer Rahmaniadding detail as we go. To that end, here are some quick definitions of
175fed00dcSSameer Rahmanisome terms we will be working with.
185fed00dcSSameer Rahmani
195fed00dcSSameer Rahmani - A kobject is an object of type struct kobject.  Kobjects have a name
205fed00dcSSameer Rahmani   and a reference count.  A kobject also has a parent pointer (allowing
215fed00dcSSameer Rahmani   objects to be arranged into hierarchies), a specific type, and,
225fed00dcSSameer Rahmani   usually, a representation in the sysfs virtual filesystem.
235fed00dcSSameer Rahmani
245fed00dcSSameer Rahmani   Kobjects are generally not interesting on their own; instead, they are
255fed00dcSSameer Rahmani   usually embedded within some other structure which contains the stuff
265fed00dcSSameer Rahmani   the code is really interested in.
275fed00dcSSameer Rahmani
285fed00dcSSameer Rahmani   No structure should **EVER** have more than one kobject embedded within it.
295fed00dcSSameer Rahmani   If it does, the reference counting for the object is sure to be messed
305fed00dcSSameer Rahmani   up and incorrect, and your code will be buggy.  So do not do this.
315fed00dcSSameer Rahmani
325fed00dcSSameer Rahmani - A ktype is the type of object that embeds a kobject.  Every structure
335fed00dcSSameer Rahmani   that embeds a kobject needs a corresponding ktype.  The ktype controls
345fed00dcSSameer Rahmani   what happens to the kobject when it is created and destroyed.
355fed00dcSSameer Rahmani
365fed00dcSSameer Rahmani - A kset is a group of kobjects.  These kobjects can be of the same ktype
375fed00dcSSameer Rahmani   or belong to different ktypes.  The kset is the basic container type for
385fed00dcSSameer Rahmani   collections of kobjects. Ksets contain their own kobjects, but you can
395fed00dcSSameer Rahmani   safely ignore that implementation detail as the kset core code handles
405fed00dcSSameer Rahmani   this kobject automatically.
415fed00dcSSameer Rahmani
425fed00dcSSameer Rahmani   When you see a sysfs directory full of other directories, generally each
435fed00dcSSameer Rahmani   of those directories corresponds to a kobject in the same kset.
445fed00dcSSameer Rahmani
455fed00dcSSameer RahmaniWe'll look at how to create and manipulate all of these types. A bottom-up
465fed00dcSSameer Rahmaniapproach will be taken, so we'll go back to kobjects.
475fed00dcSSameer Rahmani
485fed00dcSSameer Rahmani
495fed00dcSSameer RahmaniEmbedding kobjects
505fed00dcSSameer Rahmani==================
515fed00dcSSameer Rahmani
525fed00dcSSameer RahmaniIt is rare for kernel code to create a standalone kobject, with one major
535fed00dcSSameer Rahmaniexception explained below.  Instead, kobjects are used to control access to
545fed00dcSSameer Rahmania larger, domain-specific object.  To this end, kobjects will be found
555fed00dcSSameer Rahmaniembedded in other structures.  If you are used to thinking of things in
565fed00dcSSameer Rahmaniobject-oriented terms, kobjects can be seen as a top-level, abstract class
575fed00dcSSameer Rahmanifrom which other classes are derived.  A kobject implements a set of
585fed00dcSSameer Rahmanicapabilities which are not particularly useful by themselves, but are
595fed00dcSSameer Rahmaninice to have in other objects.  The C language does not allow for the
605fed00dcSSameer Rahmanidirect expression of inheritance, so other techniques - such as structure
615fed00dcSSameer Rahmaniembedding - must be used.
625fed00dcSSameer Rahmani
635fed00dcSSameer Rahmani(As an aside, for those familiar with the kernel linked list implementation,
645fed00dcSSameer Rahmanithis is analogous as to how "list_head" structs are rarely useful on
655fed00dcSSameer Rahmanitheir own, but are invariably found embedded in the larger objects of
665fed00dcSSameer Rahmaniinterest.)
675fed00dcSSameer Rahmani
685fed00dcSSameer RahmaniSo, for example, the UIO code in ``drivers/uio/uio.c`` has a structure that
695fed00dcSSameer Rahmanidefines the memory region associated with a uio device::
705fed00dcSSameer Rahmani
715fed00dcSSameer Rahmani    struct uio_map {
725fed00dcSSameer Rahmani            struct kobject kobj;
735fed00dcSSameer Rahmani            struct uio_mem *mem;
745fed00dcSSameer Rahmani    };
755fed00dcSSameer Rahmani
765fed00dcSSameer RahmaniIf you have a struct uio_map structure, finding its embedded kobject is
775fed00dcSSameer Rahmanijust a matter of using the kobj member.  Code that works with kobjects will
785fed00dcSSameer Rahmanioften have the opposite problem, however: given a struct kobject pointer,
795fed00dcSSameer Rahmaniwhat is the pointer to the containing structure?  You must avoid tricks
805fed00dcSSameer Rahmani(such as assuming that the kobject is at the beginning of the structure)
815fed00dcSSameer Rahmaniand, instead, use the container_of() macro, found in ``<linux/kernel.h>``::
825fed00dcSSameer Rahmani
833eaa3bfaSQi Zheng    container_of(ptr, type, member)
845fed00dcSSameer Rahmani
855fed00dcSSameer Rahmaniwhere:
865fed00dcSSameer Rahmani
873eaa3bfaSQi Zheng  * ``ptr`` is the pointer to the embedded kobject,
885fed00dcSSameer Rahmani  * ``type`` is the type of the containing structure, and
895fed00dcSSameer Rahmani  * ``member`` is the name of the structure field to which ``pointer`` points.
905fed00dcSSameer Rahmani
915fed00dcSSameer RahmaniThe return value from container_of() is a pointer to the corresponding
925fed00dcSSameer Rahmanicontainer type. So, for example, a pointer ``kp`` to a struct kobject
935fed00dcSSameer Rahmaniembedded **within** a struct uio_map could be converted to a pointer to the
945fed00dcSSameer Rahmani**containing** uio_map structure with::
955fed00dcSSameer Rahmani
965fed00dcSSameer Rahmani    struct uio_map *u_map = container_of(kp, struct uio_map, kobj);
975fed00dcSSameer Rahmani
985fed00dcSSameer RahmaniFor convenience, programmers often define a simple macro for **back-casting**
995fed00dcSSameer Rahmanikobject pointers to the containing type.  Exactly this happens in the
1005fed00dcSSameer Rahmaniearlier ``drivers/uio/uio.c``, as you can see here::
1015fed00dcSSameer Rahmani
1025fed00dcSSameer Rahmani    struct uio_map {
1035fed00dcSSameer Rahmani            struct kobject kobj;
1045fed00dcSSameer Rahmani            struct uio_mem *mem;
1055fed00dcSSameer Rahmani    };
1065fed00dcSSameer Rahmani
1075fed00dcSSameer Rahmani    #define to_map(map) container_of(map, struct uio_map, kobj)
1085fed00dcSSameer Rahmani
1095fed00dcSSameer Rahmaniwhere the macro argument "map" is a pointer to the struct kobject in
1105fed00dcSSameer Rahmaniquestion.  That macro is subsequently invoked with::
1115fed00dcSSameer Rahmani
1125fed00dcSSameer Rahmani    struct uio_map *map = to_map(kobj);
1135fed00dcSSameer Rahmani
1145fed00dcSSameer Rahmani
1155fed00dcSSameer RahmaniInitialization of kobjects
1165fed00dcSSameer Rahmani==========================
1175fed00dcSSameer Rahmani
1185fed00dcSSameer RahmaniCode which creates a kobject must, of course, initialize that object. Some
1195fed00dcSSameer Rahmaniof the internal fields are setup with a (mandatory) call to kobject_init()::
1205fed00dcSSameer Rahmani
1215fed00dcSSameer Rahmani    void kobject_init(struct kobject *kobj, struct kobj_type *ktype);
1225fed00dcSSameer Rahmani
1235fed00dcSSameer RahmaniThe ktype is required for a kobject to be created properly, as every kobject
1245fed00dcSSameer Rahmanimust have an associated kobj_type.  After calling kobject_init(), to
1255fed00dcSSameer Rahmaniregister the kobject with sysfs, the function kobject_add() must be called::
1265fed00dcSSameer Rahmani
1275fed00dcSSameer Rahmani    int kobject_add(struct kobject *kobj, struct kobject *parent,
1285fed00dcSSameer Rahmani                    const char *fmt, ...);
1295fed00dcSSameer Rahmani
1305fed00dcSSameer RahmaniThis sets up the parent of the kobject and the name for the kobject
1315fed00dcSSameer Rahmaniproperly.  If the kobject is to be associated with a specific kset,
1325fed00dcSSameer Rahmanikobj->kset must be assigned before calling kobject_add().  If a kset is
1335fed00dcSSameer Rahmaniassociated with a kobject, then the parent for the kobject can be set to
1345fed00dcSSameer RahmaniNULL in the call to kobject_add() and then the kobject's parent will be the
1355fed00dcSSameer Rahmanikset itself.
1365fed00dcSSameer Rahmani
1375fed00dcSSameer RahmaniAs the name of the kobject is set when it is added to the kernel, the name
1385fed00dcSSameer Rahmaniof the kobject should never be manipulated directly.  If you must change
1395fed00dcSSameer Rahmanithe name of the kobject, call kobject_rename()::
1405fed00dcSSameer Rahmani
1415fed00dcSSameer Rahmani    int kobject_rename(struct kobject *kobj, const char *new_name);
1425fed00dcSSameer Rahmani
1433eaa3bfaSQi Zhengkobject_rename() does not perform any locking or have a solid notion of
1445fed00dcSSameer Rahmaniwhat names are valid so the caller must provide their own sanity checking
1455fed00dcSSameer Rahmaniand serialization.
1465fed00dcSSameer Rahmani
1475fed00dcSSameer RahmaniThere is a function called kobject_set_name() but that is legacy cruft and
1485fed00dcSSameer Rahmaniis being removed.  If your code needs to call this function, it is
1495fed00dcSSameer Rahmaniincorrect and needs to be fixed.
1505fed00dcSSameer Rahmani
1515fed00dcSSameer RahmaniTo properly access the name of the kobject, use the function
1525fed00dcSSameer Rahmanikobject_name()::
1535fed00dcSSameer Rahmani
1545fed00dcSSameer Rahmani    const char *kobject_name(const struct kobject * kobj);
1555fed00dcSSameer Rahmani
1565fed00dcSSameer RahmaniThere is a helper function to both initialize and add the kobject to the
1575fed00dcSSameer Rahmanikernel at the same time, called surprisingly enough kobject_init_and_add()::
1585fed00dcSSameer Rahmani
1595fed00dcSSameer Rahmani    int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
1605fed00dcSSameer Rahmani                             struct kobject *parent, const char *fmt, ...);
1615fed00dcSSameer Rahmani
1625fed00dcSSameer RahmaniThe arguments are the same as the individual kobject_init() and
1635fed00dcSSameer Rahmanikobject_add() functions described above.
1645fed00dcSSameer Rahmani
1655fed00dcSSameer Rahmani
1665fed00dcSSameer RahmaniUevents
1675fed00dcSSameer Rahmani=======
1685fed00dcSSameer Rahmani
1695fed00dcSSameer RahmaniAfter a kobject has been registered with the kobject core, you need to
1705fed00dcSSameer Rahmaniannounce to the world that it has been created.  This can be done with a
1715fed00dcSSameer Rahmanicall to kobject_uevent()::
1725fed00dcSSameer Rahmani
1735fed00dcSSameer Rahmani    int kobject_uevent(struct kobject *kobj, enum kobject_action action);
1745fed00dcSSameer Rahmani
1755fed00dcSSameer RahmaniUse the **KOBJ_ADD** action for when the kobject is first added to the kernel.
1765fed00dcSSameer RahmaniThis should be done only after any attributes or children of the kobject
1775fed00dcSSameer Rahmanihave been initialized properly, as userspace will instantly start to look
1785fed00dcSSameer Rahmanifor them when this call happens.
1795fed00dcSSameer Rahmani
1805fed00dcSSameer RahmaniWhen the kobject is removed from the kernel (details on how to do that are
1815fed00dcSSameer Rahmanibelow), the uevent for **KOBJ_REMOVE** will be automatically created by the
1825fed00dcSSameer Rahmanikobject core, so the caller does not have to worry about doing that by
1835fed00dcSSameer Rahmanihand.
1845fed00dcSSameer Rahmani
1855fed00dcSSameer Rahmani
1865fed00dcSSameer RahmaniReference counts
1875fed00dcSSameer Rahmani================
1885fed00dcSSameer Rahmani
1895fed00dcSSameer RahmaniOne of the key functions of a kobject is to serve as a reference counter
1905fed00dcSSameer Rahmanifor the object in which it is embedded. As long as references to the object
1915fed00dcSSameer Rahmaniexist, the object (and the code which supports it) must continue to exist.
1925fed00dcSSameer RahmaniThe low-level functions for manipulating a kobject's reference counts are::
1935fed00dcSSameer Rahmani
1945fed00dcSSameer Rahmani    struct kobject *kobject_get(struct kobject *kobj);
1955fed00dcSSameer Rahmani    void kobject_put(struct kobject *kobj);
1965fed00dcSSameer Rahmani
1975fed00dcSSameer RahmaniA successful call to kobject_get() will increment the kobject's reference
1985fed00dcSSameer Rahmanicounter and return the pointer to the kobject.
1995fed00dcSSameer Rahmani
2005fed00dcSSameer RahmaniWhen a reference is released, the call to kobject_put() will decrement the
2015fed00dcSSameer Rahmanireference count and, possibly, free the object. Note that kobject_init()
2025fed00dcSSameer Rahmanisets the reference count to one, so the code which sets up the kobject will
2035fed00dcSSameer Rahmanineed to do a kobject_put() eventually to release that reference.
2045fed00dcSSameer Rahmani
2055fed00dcSSameer RahmaniBecause kobjects are dynamic, they must not be declared statically or on
2065fed00dcSSameer Rahmanithe stack, but instead, always allocated dynamically.  Future versions of
2075fed00dcSSameer Rahmanithe kernel will contain a run-time check for kobjects that are created
2085fed00dcSSameer Rahmanistatically and will warn the developer of this improper usage.
2095fed00dcSSameer Rahmani
2105fed00dcSSameer RahmaniIf all that you want to use a kobject for is to provide a reference counter
2115fed00dcSSameer Rahmanifor your structure, please use the struct kref instead; a kobject would be
2125fed00dcSSameer Rahmanioverkill.  For more information on how to use struct kref, please see the
2135fed00dcSSameer Rahmanifile Documentation/kref.txt in the Linux kernel source tree.
2145fed00dcSSameer Rahmani
2155fed00dcSSameer Rahmani
2165fed00dcSSameer RahmaniCreating "simple" kobjects
2175fed00dcSSameer Rahmani==========================
2185fed00dcSSameer Rahmani
2195fed00dcSSameer RahmaniSometimes all that a developer wants is a way to create a simple directory
2205fed00dcSSameer Rahmaniin the sysfs hierarchy, and not have to mess with the whole complication of
2215fed00dcSSameer Rahmaniksets, show and store functions, and other details.  This is the one
2225fed00dcSSameer Rahmaniexception where a single kobject should be created.  To create such an
2235fed00dcSSameer Rahmanientry, use the function::
2245fed00dcSSameer Rahmani
2253eaa3bfaSQi Zheng    struct kobject *kobject_create_and_add(const char *name, struct kobject *parent);
2265fed00dcSSameer Rahmani
2275fed00dcSSameer RahmaniThis function will create a kobject and place it in sysfs in the location
2285fed00dcSSameer Rahmaniunderneath the specified parent kobject.  To create simple attributes
2295fed00dcSSameer Rahmaniassociated with this kobject, use::
2305fed00dcSSameer Rahmani
2313eaa3bfaSQi Zheng    int sysfs_create_file(struct kobject *kobj, const struct attribute *attr);
2325fed00dcSSameer Rahmani
2335fed00dcSSameer Rahmanior::
2345fed00dcSSameer Rahmani
2353eaa3bfaSQi Zheng    int sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp);
2365fed00dcSSameer Rahmani
2375fed00dcSSameer RahmaniBoth types of attributes used here, with a kobject that has been created
2385fed00dcSSameer Rahmaniwith the kobject_create_and_add(), can be of type kobj_attribute, so no
2395fed00dcSSameer Rahmanispecial custom attribute is needed to be created.
2405fed00dcSSameer Rahmani
2415fed00dcSSameer RahmaniSee the example module, ``samples/kobject/kobject-example.c`` for an
2425fed00dcSSameer Rahmaniimplementation of a simple kobject and attributes.
2435fed00dcSSameer Rahmani
2445fed00dcSSameer Rahmani
2455fed00dcSSameer Rahmani
2465fed00dcSSameer Rahmaniktypes and release methods
2475fed00dcSSameer Rahmani==========================
2485fed00dcSSameer Rahmani
2495fed00dcSSameer RahmaniOne important thing still missing from the discussion is what happens to a
2505fed00dcSSameer Rahmanikobject when its reference count reaches zero. The code which created the
2515fed00dcSSameer Rahmanikobject generally does not know when that will happen; if it did, there
2525fed00dcSSameer Rahmaniwould be little point in using a kobject in the first place. Even
2535fed00dcSSameer Rahmanipredictable object lifecycles become more complicated when sysfs is brought
2545fed00dcSSameer Rahmaniin as other portions of the kernel can get a reference on any kobject that
2555fed00dcSSameer Rahmaniis registered in the system.
2565fed00dcSSameer Rahmani
2575fed00dcSSameer RahmaniThe end result is that a structure protected by a kobject cannot be freed
2585fed00dcSSameer Rahmanibefore its reference count goes to zero. The reference count is not under
2595fed00dcSSameer Rahmanithe direct control of the code which created the kobject. So that code must
2605fed00dcSSameer Rahmanibe notified asynchronously whenever the last reference to one of its
2615fed00dcSSameer Rahmanikobjects goes away.
2625fed00dcSSameer Rahmani
2635fed00dcSSameer RahmaniOnce you registered your kobject via kobject_add(), you must never use
2645fed00dcSSameer Rahmanikfree() to free it directly. The only safe way is to use kobject_put(). It
2655fed00dcSSameer Rahmaniis good practice to always use kobject_put() after kobject_init() to avoid
2665fed00dcSSameer Rahmanierrors creeping in.
2675fed00dcSSameer Rahmani
2685fed00dcSSameer RahmaniThis notification is done through a kobject's release() method. Usually
2695fed00dcSSameer Rahmanisuch a method has a form like::
2705fed00dcSSameer Rahmani
2715fed00dcSSameer Rahmani    void my_object_release(struct kobject *kobj)
2725fed00dcSSameer Rahmani    {
2735fed00dcSSameer Rahmani            struct my_object *mine = container_of(kobj, struct my_object, kobj);
2745fed00dcSSameer Rahmani
2755fed00dcSSameer Rahmani            /* Perform any additional cleanup on this object, then... */
2765fed00dcSSameer Rahmani            kfree(mine);
2775fed00dcSSameer Rahmani    }
2785fed00dcSSameer Rahmani
2795fed00dcSSameer RahmaniOne important point cannot be overstated: every kobject must have a
2805fed00dcSSameer Rahmanirelease() method, and the kobject must persist (in a consistent state)
2815fed00dcSSameer Rahmaniuntil that method is called. If these constraints are not met, the code is
2825fed00dcSSameer Rahmaniflawed. Note that the kernel will warn you if you forget to provide a
2835fed00dcSSameer Rahmanirelease() method.  Do not try to get rid of this warning by providing an
2845fed00dcSSameer Rahmani"empty" release function.
2855fed00dcSSameer Rahmani
2865fed00dcSSameer RahmaniIf all your cleanup function needs to do is call kfree(), then you must
2875fed00dcSSameer Rahmanicreate a wrapper function which uses container_of() to upcast to the correct
2885fed00dcSSameer Rahmanitype (as shown in the example above) and then calls kfree() on the overall
2895fed00dcSSameer Rahmanistructure.
2905fed00dcSSameer Rahmani
2915fed00dcSSameer RahmaniNote, the name of the kobject is available in the release function, but it
2925fed00dcSSameer Rahmanimust NOT be changed within this callback.  Otherwise there will be a memory
2935fed00dcSSameer Rahmanileak in the kobject core, which makes people unhappy.
2945fed00dcSSameer Rahmani
2955fed00dcSSameer RahmaniInterestingly, the release() method is not stored in the kobject itself;
2965fed00dcSSameer Rahmaniinstead, it is associated with the ktype. So let us introduce struct
2975fed00dcSSameer Rahmanikobj_type::
2985fed00dcSSameer Rahmani
2995fed00dcSSameer Rahmani    struct kobj_type {
3005fed00dcSSameer Rahmani            void (*release)(struct kobject *kobj);
3015fed00dcSSameer Rahmani            const struct sysfs_ops *sysfs_ops;
3025fed00dcSSameer Rahmani            struct attribute **default_attrs;
3033eaa3bfaSQi Zheng            const struct attribute_group **default_groups;
3045fed00dcSSameer Rahmani            const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
3055fed00dcSSameer Rahmani            const void *(*namespace)(struct kobject *kobj);
3063eaa3bfaSQi Zheng            void (*get_ownership)(struct kobject *kobj, kuid_t *uid, kgid_t *gid);
3075fed00dcSSameer Rahmani    };
3085fed00dcSSameer Rahmani
3095fed00dcSSameer RahmaniThis structure is used to describe a particular type of kobject (or, more
3105fed00dcSSameer Rahmanicorrectly, of containing object). Every kobject needs to have an associated
3115fed00dcSSameer Rahmanikobj_type structure; a pointer to that structure must be specified when you
3125fed00dcSSameer Rahmanicall kobject_init() or kobject_init_and_add().
3135fed00dcSSameer Rahmani
3145fed00dcSSameer RahmaniThe release field in struct kobj_type is, of course, a pointer to the
3155fed00dcSSameer Rahmanirelease() method for this type of kobject. The other two fields (sysfs_ops
3165fed00dcSSameer Rahmaniand default_attrs) control how objects of this type are represented in
3175fed00dcSSameer Rahmanisysfs; they are beyond the scope of this document.
3185fed00dcSSameer Rahmani
3195fed00dcSSameer RahmaniThe default_attrs pointer is a list of default attributes that will be
3205fed00dcSSameer Rahmaniautomatically created for any kobject that is registered with this ktype.
3215fed00dcSSameer Rahmani
3225fed00dcSSameer Rahmani
3235fed00dcSSameer Rahmaniksets
3245fed00dcSSameer Rahmani=====
3255fed00dcSSameer Rahmani
3265fed00dcSSameer RahmaniA kset is merely a collection of kobjects that want to be associated with
3275fed00dcSSameer Rahmanieach other.  There is no restriction that they be of the same ktype, but be
3285fed00dcSSameer Rahmanivery careful if they are not.
3295fed00dcSSameer Rahmani
3305fed00dcSSameer RahmaniA kset serves these functions:
3315fed00dcSSameer Rahmani
3325fed00dcSSameer Rahmani - It serves as a bag containing a group of objects. A kset can be used by
3335fed00dcSSameer Rahmani   the kernel to track "all block devices" or "all PCI device drivers."
3345fed00dcSSameer Rahmani
3355fed00dcSSameer Rahmani - A kset is also a subdirectory in sysfs, where the associated kobjects
3365fed00dcSSameer Rahmani   with the kset can show up.  Every kset contains a kobject which can be
3375fed00dcSSameer Rahmani   set up to be the parent of other kobjects; the top-level directories of
3385fed00dcSSameer Rahmani   the sysfs hierarchy are constructed in this way.
3395fed00dcSSameer Rahmani
3405fed00dcSSameer Rahmani - Ksets can support the "hotplugging" of kobjects and influence how
3415fed00dcSSameer Rahmani   uevent events are reported to user space.
3425fed00dcSSameer Rahmani
3435fed00dcSSameer RahmaniIn object-oriented terms, "kset" is the top-level container class; ksets
3445fed00dcSSameer Rahmanicontain their own kobject, but that kobject is managed by the kset code and
3455fed00dcSSameer Rahmanishould not be manipulated by any other user.
3465fed00dcSSameer Rahmani
3475fed00dcSSameer RahmaniA kset keeps its children in a standard kernel linked list.  Kobjects point
3485fed00dcSSameer Rahmaniback to their containing kset via their kset field. In almost all cases,
3495fed00dcSSameer Rahmanithe kobjects belonging to a kset have that kset (or, strictly, its embedded
3505fed00dcSSameer Rahmanikobject) in their parent.
3515fed00dcSSameer Rahmani
3525fed00dcSSameer RahmaniAs a kset contains a kobject within it, it should always be dynamically
3535fed00dcSSameer Rahmanicreated and never declared statically or on the stack.  To create a new
3545fed00dcSSameer Rahmanikset use::
3555fed00dcSSameer Rahmani
3565fed00dcSSameer Rahmani  struct kset *kset_create_and_add(const char *name,
3573eaa3bfaSQi Zheng                                   const struct kset_uevent_ops *uevent_ops,
3583eaa3bfaSQi Zheng                                   struct kobject *parent_kobj);
3595fed00dcSSameer Rahmani
3605fed00dcSSameer RahmaniWhen you are finished with the kset, call::
3615fed00dcSSameer Rahmani
3623eaa3bfaSQi Zheng  void kset_unregister(struct kset *k);
3635fed00dcSSameer Rahmani
3645fed00dcSSameer Rahmanito destroy it.  This removes the kset from sysfs and decrements its reference
3655fed00dcSSameer Rahmanicount.  When the reference count goes to zero, the kset will be released.
3665fed00dcSSameer RahmaniBecause other references to the kset may still exist, the release may happen
3675fed00dcSSameer Rahmaniafter kset_unregister() returns.
3685fed00dcSSameer Rahmani
3695fed00dcSSameer RahmaniAn example of using a kset can be seen in the
3705fed00dcSSameer Rahmani``samples/kobject/kset-example.c`` file in the kernel tree.
3715fed00dcSSameer Rahmani
3725fed00dcSSameer RahmaniIf a kset wishes to control the uevent operations of the kobjects
3735fed00dcSSameer Rahmaniassociated with it, it can use the struct kset_uevent_ops to handle it::
3745fed00dcSSameer Rahmani
3755fed00dcSSameer Rahmani  struct kset_uevent_ops {
3763eaa3bfaSQi Zheng          int (* const filter)(struct kset *kset, struct kobject *kobj);
3773eaa3bfaSQi Zheng          const char *(* const name)(struct kset *kset, struct kobject *kobj);
3783eaa3bfaSQi Zheng          int (* const uevent)(struct kset *kset, struct kobject *kobj,
3795fed00dcSSameer Rahmani                        struct kobj_uevent_env *env);
3805fed00dcSSameer Rahmani  };
3815fed00dcSSameer Rahmani
3825fed00dcSSameer Rahmani
3835fed00dcSSameer RahmaniThe filter function allows a kset to prevent a uevent from being emitted to
3845fed00dcSSameer Rahmaniuserspace for a specific kobject.  If the function returns 0, the uevent
3855fed00dcSSameer Rahmaniwill not be emitted.
3865fed00dcSSameer Rahmani
3875fed00dcSSameer RahmaniThe name function will be called to override the default name of the kset
3885fed00dcSSameer Rahmanithat the uevent sends to userspace.  By default, the name will be the same
3895fed00dcSSameer Rahmanias the kset itself, but this function, if present, can override that name.
3905fed00dcSSameer Rahmani
3915fed00dcSSameer RahmaniThe uevent function will be called when the uevent is about to be sent to
3925fed00dcSSameer Rahmaniuserspace to allow more environment variables to be added to the uevent.
3935fed00dcSSameer Rahmani
3945fed00dcSSameer RahmaniOne might ask how, exactly, a kobject is added to a kset, given that no
3955fed00dcSSameer Rahmanifunctions which perform that function have been presented.  The answer is
3965fed00dcSSameer Rahmanithat this task is handled by kobject_add().  When a kobject is passed to
3975fed00dcSSameer Rahmanikobject_add(), its kset member should point to the kset to which the
3985fed00dcSSameer Rahmanikobject will belong.  kobject_add() will handle the rest.
3995fed00dcSSameer Rahmani
4005fed00dcSSameer RahmaniIf the kobject belonging to a kset has no parent kobject set, it will be
4015fed00dcSSameer Rahmaniadded to the kset's directory.  Not all members of a kset do necessarily
4025fed00dcSSameer Rahmanilive in the kset directory.  If an explicit parent kobject is assigned
4035fed00dcSSameer Rahmanibefore the kobject is added, the kobject is registered with the kset, but
4045fed00dcSSameer Rahmaniadded below the parent kobject.
4055fed00dcSSameer Rahmani
4065fed00dcSSameer Rahmani
4075fed00dcSSameer RahmaniKobject removal
4085fed00dcSSameer Rahmani===============
4095fed00dcSSameer Rahmani
4105fed00dcSSameer RahmaniAfter a kobject has been registered with the kobject core successfully, it
4115fed00dcSSameer Rahmanimust be cleaned up when the code is finished with it.  To do that, call
4125fed00dcSSameer Rahmanikobject_put().  By doing this, the kobject core will automatically clean up
4135fed00dcSSameer Rahmaniall of the memory allocated by this kobject.  If a ``KOBJ_ADD`` uevent has been
4145fed00dcSSameer Rahmanisent for the object, a corresponding ``KOBJ_REMOVE`` uevent will be sent, and
4155fed00dcSSameer Rahmaniany other sysfs housekeeping will be handled for the caller properly.
4165fed00dcSSameer Rahmani
4175fed00dcSSameer RahmaniIf you need to do a two-stage delete of the kobject (say you are not
4185fed00dcSSameer Rahmaniallowed to sleep when you need to destroy the object), then call
4195fed00dcSSameer Rahmanikobject_del() which will unregister the kobject from sysfs.  This makes the
4205fed00dcSSameer Rahmanikobject "invisible", but it is not cleaned up, and the reference count of
4215fed00dcSSameer Rahmanithe object is still the same.  At a later time call kobject_put() to finish
4225fed00dcSSameer Rahmanithe cleanup of the memory associated with the kobject.
4235fed00dcSSameer Rahmani
4245fed00dcSSameer Rahmanikobject_del() can be used to drop the reference to the parent object, if
4255fed00dcSSameer Rahmanicircular references are constructed.  It is valid in some cases, that a
4265fed00dcSSameer Rahmaniparent objects references a child.  Circular references _must_ be broken
4275fed00dcSSameer Rahmaniwith an explicit call to kobject_del(), so that a release functions will be
4285fed00dcSSameer Rahmanicalled, and the objects in the former circle release each other.
4295fed00dcSSameer Rahmani
4305fed00dcSSameer Rahmani
4315fed00dcSSameer RahmaniExample code to copy from
4325fed00dcSSameer Rahmani=========================
4335fed00dcSSameer Rahmani
4345fed00dcSSameer RahmaniFor a more complete example of using ksets and kobjects properly, see the
4355fed00dcSSameer Rahmaniexample programs ``samples/kobject/{kobject-example.c,kset-example.c}``,
4365fed00dcSSameer Rahmaniwhich will be built as loadable modules if you select ``CONFIG_SAMPLE_KOBJECT``.
437