162306a36Sopenharmony_ci=====================================================================
262306a36Sopenharmony_ciEverything you never wanted to know about kobjects, ksets, and ktypes
362306a36Sopenharmony_ci=====================================================================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci:Author: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
662306a36Sopenharmony_ci:Last updated: December 19, 2007
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciBased on an original article by Jon Corbet for lwn.net written October 1,
962306a36Sopenharmony_ci2003 and located at https://lwn.net/Articles/51437/
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciPart of the difficulty in understanding the driver model - and the kobject
1262306a36Sopenharmony_ciabstraction upon which it is built - is that there is no obvious starting
1362306a36Sopenharmony_ciplace. Dealing with kobjects requires understanding a few different types,
1462306a36Sopenharmony_ciall of which make reference to each other. In an attempt to make things
1562306a36Sopenharmony_cieasier, we'll take a multi-pass approach, starting with vague terms and
1662306a36Sopenharmony_ciadding detail as we go. To that end, here are some quick definitions of
1762306a36Sopenharmony_cisome terms we will be working with.
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci - A kobject is an object of type struct kobject.  Kobjects have a name
2062306a36Sopenharmony_ci   and a reference count.  A kobject also has a parent pointer (allowing
2162306a36Sopenharmony_ci   objects to be arranged into hierarchies), a specific type, and,
2262306a36Sopenharmony_ci   usually, a representation in the sysfs virtual filesystem.
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci   Kobjects are generally not interesting on their own; instead, they are
2562306a36Sopenharmony_ci   usually embedded within some other structure which contains the stuff
2662306a36Sopenharmony_ci   the code is really interested in.
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci   No structure should **EVER** have more than one kobject embedded within it.
2962306a36Sopenharmony_ci   If it does, the reference counting for the object is sure to be messed
3062306a36Sopenharmony_ci   up and incorrect, and your code will be buggy.  So do not do this.
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci - A ktype is the type of object that embeds a kobject.  Every structure
3362306a36Sopenharmony_ci   that embeds a kobject needs a corresponding ktype.  The ktype controls
3462306a36Sopenharmony_ci   what happens to the kobject when it is created and destroyed.
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci - A kset is a group of kobjects.  These kobjects can be of the same ktype
3762306a36Sopenharmony_ci   or belong to different ktypes.  The kset is the basic container type for
3862306a36Sopenharmony_ci   collections of kobjects. Ksets contain their own kobjects, but you can
3962306a36Sopenharmony_ci   safely ignore that implementation detail as the kset core code handles
4062306a36Sopenharmony_ci   this kobject automatically.
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci   When you see a sysfs directory full of other directories, generally each
4362306a36Sopenharmony_ci   of those directories corresponds to a kobject in the same kset.
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ciWe'll look at how to create and manipulate all of these types. A bottom-up
4662306a36Sopenharmony_ciapproach will be taken, so we'll go back to kobjects.
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ciEmbedding kobjects
5062306a36Sopenharmony_ci==================
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciIt is rare for kernel code to create a standalone kobject, with one major
5362306a36Sopenharmony_ciexception explained below.  Instead, kobjects are used to control access to
5462306a36Sopenharmony_cia larger, domain-specific object.  To this end, kobjects will be found
5562306a36Sopenharmony_ciembedded in other structures.  If you are used to thinking of things in
5662306a36Sopenharmony_ciobject-oriented terms, kobjects can be seen as a top-level, abstract class
5762306a36Sopenharmony_cifrom which other classes are derived.  A kobject implements a set of
5862306a36Sopenharmony_cicapabilities which are not particularly useful by themselves, but are
5962306a36Sopenharmony_cinice to have in other objects.  The C language does not allow for the
6062306a36Sopenharmony_cidirect expression of inheritance, so other techniques - such as structure
6162306a36Sopenharmony_ciembedding - must be used.
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci(As an aside, for those familiar with the kernel linked list implementation,
6462306a36Sopenharmony_cithis is analogous as to how "list_head" structs are rarely useful on
6562306a36Sopenharmony_citheir own, but are invariably found embedded in the larger objects of
6662306a36Sopenharmony_ciinterest.)
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ciSo, for example, the UIO code in ``drivers/uio/uio.c`` has a structure that
6962306a36Sopenharmony_cidefines the memory region associated with a uio device::
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci    struct uio_map {
7262306a36Sopenharmony_ci            struct kobject kobj;
7362306a36Sopenharmony_ci            struct uio_mem *mem;
7462306a36Sopenharmony_ci    };
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ciIf you have a struct uio_map structure, finding its embedded kobject is
7762306a36Sopenharmony_cijust a matter of using the kobj member.  Code that works with kobjects will
7862306a36Sopenharmony_cioften have the opposite problem, however: given a struct kobject pointer,
7962306a36Sopenharmony_ciwhat is the pointer to the containing structure?  You must avoid tricks
8062306a36Sopenharmony_ci(such as assuming that the kobject is at the beginning of the structure)
8162306a36Sopenharmony_ciand, instead, use the container_of() macro, found in ``<linux/kernel.h>``::
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci    container_of(ptr, type, member)
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ciwhere:
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci  * ``ptr`` is the pointer to the embedded kobject,
8862306a36Sopenharmony_ci  * ``type`` is the type of the containing structure, and
8962306a36Sopenharmony_ci  * ``member`` is the name of the structure field to which ``pointer`` points.
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ciThe return value from container_of() is a pointer to the corresponding
9262306a36Sopenharmony_cicontainer type. So, for example, a pointer ``kp`` to a struct kobject
9362306a36Sopenharmony_ciembedded **within** a struct uio_map could be converted to a pointer to the
9462306a36Sopenharmony_ci**containing** uio_map structure with::
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci    struct uio_map *u_map = container_of(kp, struct uio_map, kobj);
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ciFor convenience, programmers often define a simple macro for **back-casting**
9962306a36Sopenharmony_cikobject pointers to the containing type.  Exactly this happens in the
10062306a36Sopenharmony_ciearlier ``drivers/uio/uio.c``, as you can see here::
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci    struct uio_map {
10362306a36Sopenharmony_ci            struct kobject kobj;
10462306a36Sopenharmony_ci            struct uio_mem *mem;
10562306a36Sopenharmony_ci    };
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci    #define to_map(map) container_of(map, struct uio_map, kobj)
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ciwhere the macro argument "map" is a pointer to the struct kobject in
11062306a36Sopenharmony_ciquestion.  That macro is subsequently invoked with::
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci    struct uio_map *map = to_map(kobj);
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciInitialization of kobjects
11662306a36Sopenharmony_ci==========================
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ciCode which creates a kobject must, of course, initialize that object. Some
11962306a36Sopenharmony_ciof the internal fields are setup with a (mandatory) call to kobject_init()::
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci    void kobject_init(struct kobject *kobj, const struct kobj_type *ktype);
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ciThe ktype is required for a kobject to be created properly, as every kobject
12462306a36Sopenharmony_cimust have an associated kobj_type.  After calling kobject_init(), to
12562306a36Sopenharmony_ciregister the kobject with sysfs, the function kobject_add() must be called::
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci    int kobject_add(struct kobject *kobj, struct kobject *parent,
12862306a36Sopenharmony_ci                    const char *fmt, ...);
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ciThis sets up the parent of the kobject and the name for the kobject
13162306a36Sopenharmony_ciproperly.  If the kobject is to be associated with a specific kset,
13262306a36Sopenharmony_cikobj->kset must be assigned before calling kobject_add().  If a kset is
13362306a36Sopenharmony_ciassociated with a kobject, then the parent for the kobject can be set to
13462306a36Sopenharmony_ciNULL in the call to kobject_add() and then the kobject's parent will be the
13562306a36Sopenharmony_cikset itself.
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ciAs the name of the kobject is set when it is added to the kernel, the name
13862306a36Sopenharmony_ciof the kobject should never be manipulated directly.  If you must change
13962306a36Sopenharmony_cithe name of the kobject, call kobject_rename()::
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci    int kobject_rename(struct kobject *kobj, const char *new_name);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_cikobject_rename() does not perform any locking or have a solid notion of
14462306a36Sopenharmony_ciwhat names are valid so the caller must provide their own sanity checking
14562306a36Sopenharmony_ciand serialization.
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ciThere is a function called kobject_set_name() but that is legacy cruft and
14862306a36Sopenharmony_ciis being removed.  If your code needs to call this function, it is
14962306a36Sopenharmony_ciincorrect and needs to be fixed.
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ciTo properly access the name of the kobject, use the function
15262306a36Sopenharmony_cikobject_name()::
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci    const char *kobject_name(const struct kobject * kobj);
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ciThere is a helper function to both initialize and add the kobject to the
15762306a36Sopenharmony_cikernel at the same time, called surprisingly enough kobject_init_and_add()::
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci    int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
16062306a36Sopenharmony_ci                             struct kobject *parent, const char *fmt, ...);
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ciThe arguments are the same as the individual kobject_init() and
16362306a36Sopenharmony_cikobject_add() functions described above.
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ciUevents
16762306a36Sopenharmony_ci=======
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ciAfter a kobject has been registered with the kobject core, you need to
17062306a36Sopenharmony_ciannounce to the world that it has been created.  This can be done with a
17162306a36Sopenharmony_cicall to kobject_uevent()::
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci    int kobject_uevent(struct kobject *kobj, enum kobject_action action);
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ciUse the **KOBJ_ADD** action for when the kobject is first added to the kernel.
17662306a36Sopenharmony_ciThis should be done only after any attributes or children of the kobject
17762306a36Sopenharmony_cihave been initialized properly, as userspace will instantly start to look
17862306a36Sopenharmony_cifor them when this call happens.
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ciWhen the kobject is removed from the kernel (details on how to do that are
18162306a36Sopenharmony_cibelow), the uevent for **KOBJ_REMOVE** will be automatically created by the
18262306a36Sopenharmony_cikobject core, so the caller does not have to worry about doing that by
18362306a36Sopenharmony_cihand.
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ciReference counts
18762306a36Sopenharmony_ci================
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ciOne of the key functions of a kobject is to serve as a reference counter
19062306a36Sopenharmony_cifor the object in which it is embedded. As long as references to the object
19162306a36Sopenharmony_ciexist, the object (and the code which supports it) must continue to exist.
19262306a36Sopenharmony_ciThe low-level functions for manipulating a kobject's reference counts are::
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci    struct kobject *kobject_get(struct kobject *kobj);
19562306a36Sopenharmony_ci    void kobject_put(struct kobject *kobj);
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ciA successful call to kobject_get() will increment the kobject's reference
19862306a36Sopenharmony_cicounter and return the pointer to the kobject.
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ciWhen a reference is released, the call to kobject_put() will decrement the
20162306a36Sopenharmony_cireference count and, possibly, free the object. Note that kobject_init()
20262306a36Sopenharmony_cisets the reference count to one, so the code which sets up the kobject will
20362306a36Sopenharmony_cineed to do a kobject_put() eventually to release that reference.
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ciBecause kobjects are dynamic, they must not be declared statically or on
20662306a36Sopenharmony_cithe stack, but instead, always allocated dynamically.  Future versions of
20762306a36Sopenharmony_cithe kernel will contain a run-time check for kobjects that are created
20862306a36Sopenharmony_cistatically and will warn the developer of this improper usage.
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ciIf all that you want to use a kobject for is to provide a reference counter
21162306a36Sopenharmony_cifor your structure, please use the struct kref instead; a kobject would be
21262306a36Sopenharmony_cioverkill.  For more information on how to use struct kref, please see the
21362306a36Sopenharmony_cifile Documentation/core-api/kref.rst in the Linux kernel source tree.
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ciCreating "simple" kobjects
21762306a36Sopenharmony_ci==========================
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ciSometimes all that a developer wants is a way to create a simple directory
22062306a36Sopenharmony_ciin the sysfs hierarchy, and not have to mess with the whole complication of
22162306a36Sopenharmony_ciksets, show and store functions, and other details.  This is the one
22262306a36Sopenharmony_ciexception where a single kobject should be created.  To create such an
22362306a36Sopenharmony_cientry, use the function::
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci    struct kobject *kobject_create_and_add(const char *name, struct kobject *parent);
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ciThis function will create a kobject and place it in sysfs in the location
22862306a36Sopenharmony_ciunderneath the specified parent kobject.  To create simple attributes
22962306a36Sopenharmony_ciassociated with this kobject, use::
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci    int sysfs_create_file(struct kobject *kobj, const struct attribute *attr);
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_cior::
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci    int sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp);
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ciBoth types of attributes used here, with a kobject that has been created
23862306a36Sopenharmony_ciwith the kobject_create_and_add(), can be of type kobj_attribute, so no
23962306a36Sopenharmony_cispecial custom attribute is needed to be created.
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ciSee the example module, ``samples/kobject/kobject-example.c`` for an
24262306a36Sopenharmony_ciimplementation of a simple kobject and attributes.
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ciktypes and release methods
24762306a36Sopenharmony_ci==========================
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ciOne important thing still missing from the discussion is what happens to a
25062306a36Sopenharmony_cikobject when its reference count reaches zero. The code which created the
25162306a36Sopenharmony_cikobject generally does not know when that will happen; if it did, there
25262306a36Sopenharmony_ciwould be little point in using a kobject in the first place. Even
25362306a36Sopenharmony_cipredictable object lifecycles become more complicated when sysfs is brought
25462306a36Sopenharmony_ciin as other portions of the kernel can get a reference on any kobject that
25562306a36Sopenharmony_ciis registered in the system.
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ciThe end result is that a structure protected by a kobject cannot be freed
25862306a36Sopenharmony_cibefore its reference count goes to zero. The reference count is not under
25962306a36Sopenharmony_cithe direct control of the code which created the kobject. So that code must
26062306a36Sopenharmony_cibe notified asynchronously whenever the last reference to one of its
26162306a36Sopenharmony_cikobjects goes away.
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ciOnce you registered your kobject via kobject_add(), you must never use
26462306a36Sopenharmony_cikfree() to free it directly. The only safe way is to use kobject_put(). It
26562306a36Sopenharmony_ciis good practice to always use kobject_put() after kobject_init() to avoid
26662306a36Sopenharmony_cierrors creeping in.
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ciThis notification is done through a kobject's release() method. Usually
26962306a36Sopenharmony_cisuch a method has a form like::
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci    void my_object_release(struct kobject *kobj)
27262306a36Sopenharmony_ci    {
27362306a36Sopenharmony_ci            struct my_object *mine = container_of(kobj, struct my_object, kobj);
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci            /* Perform any additional cleanup on this object, then... */
27662306a36Sopenharmony_ci            kfree(mine);
27762306a36Sopenharmony_ci    }
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ciOne important point cannot be overstated: every kobject must have a
28062306a36Sopenharmony_cirelease() method, and the kobject must persist (in a consistent state)
28162306a36Sopenharmony_ciuntil that method is called. If these constraints are not met, the code is
28262306a36Sopenharmony_ciflawed. Note that the kernel will warn you if you forget to provide a
28362306a36Sopenharmony_cirelease() method.  Do not try to get rid of this warning by providing an
28462306a36Sopenharmony_ci"empty" release function.
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ciIf all your cleanup function needs to do is call kfree(), then you must
28762306a36Sopenharmony_cicreate a wrapper function which uses container_of() to upcast to the correct
28862306a36Sopenharmony_citype (as shown in the example above) and then calls kfree() on the overall
28962306a36Sopenharmony_cistructure.
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ciNote, the name of the kobject is available in the release function, but it
29262306a36Sopenharmony_cimust NOT be changed within this callback.  Otherwise there will be a memory
29362306a36Sopenharmony_cileak in the kobject core, which makes people unhappy.
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ciInterestingly, the release() method is not stored in the kobject itself;
29662306a36Sopenharmony_ciinstead, it is associated with the ktype. So let us introduce struct
29762306a36Sopenharmony_cikobj_type::
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci    struct kobj_type {
30062306a36Sopenharmony_ci            void (*release)(struct kobject *kobj);
30162306a36Sopenharmony_ci            const struct sysfs_ops *sysfs_ops;
30262306a36Sopenharmony_ci            const struct attribute_group **default_groups;
30362306a36Sopenharmony_ci            const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
30462306a36Sopenharmony_ci            const void *(*namespace)(struct kobject *kobj);
30562306a36Sopenharmony_ci            void (*get_ownership)(struct kobject *kobj, kuid_t *uid, kgid_t *gid);
30662306a36Sopenharmony_ci    };
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ciThis structure is used to describe a particular type of kobject (or, more
30962306a36Sopenharmony_cicorrectly, of containing object). Every kobject needs to have an associated
31062306a36Sopenharmony_cikobj_type structure; a pointer to that structure must be specified when you
31162306a36Sopenharmony_cicall kobject_init() or kobject_init_and_add().
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ciThe release field in struct kobj_type is, of course, a pointer to the
31462306a36Sopenharmony_cirelease() method for this type of kobject. The other two fields (sysfs_ops
31562306a36Sopenharmony_ciand default_groups) control how objects of this type are represented in
31662306a36Sopenharmony_cisysfs; they are beyond the scope of this document.
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ciThe default_groups pointer is a list of default attributes that will be
31962306a36Sopenharmony_ciautomatically created for any kobject that is registered with this ktype.
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ciksets
32362306a36Sopenharmony_ci=====
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ciA kset is merely a collection of kobjects that want to be associated with
32662306a36Sopenharmony_cieach other.  There is no restriction that they be of the same ktype, but be
32762306a36Sopenharmony_civery careful if they are not.
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ciA kset serves these functions:
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci - It serves as a bag containing a group of objects. A kset can be used by
33262306a36Sopenharmony_ci   the kernel to track "all block devices" or "all PCI device drivers."
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci - A kset is also a subdirectory in sysfs, where the associated kobjects
33562306a36Sopenharmony_ci   with the kset can show up.  Every kset contains a kobject which can be
33662306a36Sopenharmony_ci   set up to be the parent of other kobjects; the top-level directories of
33762306a36Sopenharmony_ci   the sysfs hierarchy are constructed in this way.
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci - Ksets can support the "hotplugging" of kobjects and influence how
34062306a36Sopenharmony_ci   uevent events are reported to user space.
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ciIn object-oriented terms, "kset" is the top-level container class; ksets
34362306a36Sopenharmony_cicontain their own kobject, but that kobject is managed by the kset code and
34462306a36Sopenharmony_cishould not be manipulated by any other user.
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ciA kset keeps its children in a standard kernel linked list.  Kobjects point
34762306a36Sopenharmony_ciback to their containing kset via their kset field. In almost all cases,
34862306a36Sopenharmony_cithe kobjects belonging to a kset have that kset (or, strictly, its embedded
34962306a36Sopenharmony_cikobject) in their parent.
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ciAs a kset contains a kobject within it, it should always be dynamically
35262306a36Sopenharmony_cicreated and never declared statically or on the stack.  To create a new
35362306a36Sopenharmony_cikset use::
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci  struct kset *kset_create_and_add(const char *name,
35662306a36Sopenharmony_ci                                   const struct kset_uevent_ops *uevent_ops,
35762306a36Sopenharmony_ci                                   struct kobject *parent_kobj);
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_ciWhen you are finished with the kset, call::
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ci  void kset_unregister(struct kset *k);
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_cito destroy it.  This removes the kset from sysfs and decrements its reference
36462306a36Sopenharmony_cicount.  When the reference count goes to zero, the kset will be released.
36562306a36Sopenharmony_ciBecause other references to the kset may still exist, the release may happen
36662306a36Sopenharmony_ciafter kset_unregister() returns.
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ciAn example of using a kset can be seen in the
36962306a36Sopenharmony_ci``samples/kobject/kset-example.c`` file in the kernel tree.
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ciIf a kset wishes to control the uevent operations of the kobjects
37262306a36Sopenharmony_ciassociated with it, it can use the struct kset_uevent_ops to handle it::
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci  struct kset_uevent_ops {
37562306a36Sopenharmony_ci          int (* const filter)(struct kobject *kobj);
37662306a36Sopenharmony_ci          const char *(* const name)(struct kobject *kobj);
37762306a36Sopenharmony_ci          int (* const uevent)(struct kobject *kobj, struct kobj_uevent_env *env);
37862306a36Sopenharmony_ci  };
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ciThe filter function allows a kset to prevent a uevent from being emitted to
38262306a36Sopenharmony_ciuserspace for a specific kobject.  If the function returns 0, the uevent
38362306a36Sopenharmony_ciwill not be emitted.
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ciThe name function will be called to override the default name of the kset
38662306a36Sopenharmony_cithat the uevent sends to userspace.  By default, the name will be the same
38762306a36Sopenharmony_cias the kset itself, but this function, if present, can override that name.
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ciThe uevent function will be called when the uevent is about to be sent to
39062306a36Sopenharmony_ciuserspace to allow more environment variables to be added to the uevent.
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ciOne might ask how, exactly, a kobject is added to a kset, given that no
39362306a36Sopenharmony_cifunctions which perform that function have been presented.  The answer is
39462306a36Sopenharmony_cithat this task is handled by kobject_add().  When a kobject is passed to
39562306a36Sopenharmony_cikobject_add(), its kset member should point to the kset to which the
39662306a36Sopenharmony_cikobject will belong.  kobject_add() will handle the rest.
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ciIf the kobject belonging to a kset has no parent kobject set, it will be
39962306a36Sopenharmony_ciadded to the kset's directory.  Not all members of a kset do necessarily
40062306a36Sopenharmony_cilive in the kset directory.  If an explicit parent kobject is assigned
40162306a36Sopenharmony_cibefore the kobject is added, the kobject is registered with the kset, but
40262306a36Sopenharmony_ciadded below the parent kobject.
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ciKobject removal
40662306a36Sopenharmony_ci===============
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ciAfter a kobject has been registered with the kobject core successfully, it
40962306a36Sopenharmony_cimust be cleaned up when the code is finished with it.  To do that, call
41062306a36Sopenharmony_cikobject_put().  By doing this, the kobject core will automatically clean up
41162306a36Sopenharmony_ciall of the memory allocated by this kobject.  If a ``KOBJ_ADD`` uevent has been
41262306a36Sopenharmony_cisent for the object, a corresponding ``KOBJ_REMOVE`` uevent will be sent, and
41362306a36Sopenharmony_ciany other sysfs housekeeping will be handled for the caller properly.
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ciIf you need to do a two-stage delete of the kobject (say you are not
41662306a36Sopenharmony_ciallowed to sleep when you need to destroy the object), then call
41762306a36Sopenharmony_cikobject_del() which will unregister the kobject from sysfs.  This makes the
41862306a36Sopenharmony_cikobject "invisible", but it is not cleaned up, and the reference count of
41962306a36Sopenharmony_cithe object is still the same.  At a later time call kobject_put() to finish
42062306a36Sopenharmony_cithe cleanup of the memory associated with the kobject.
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_cikobject_del() can be used to drop the reference to the parent object, if
42362306a36Sopenharmony_cicircular references are constructed.  It is valid in some cases, that a
42462306a36Sopenharmony_ciparent objects references a child.  Circular references _must_ be broken
42562306a36Sopenharmony_ciwith an explicit call to kobject_del(), so that a release functions will be
42662306a36Sopenharmony_cicalled, and the objects in the former circle release each other.
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ciExample code to copy from
43062306a36Sopenharmony_ci=========================
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ciFor a more complete example of using ksets and kobjects properly, see the
43362306a36Sopenharmony_ciexample programs ``samples/kobject/{kobject-example.c,kset-example.c}``,
43462306a36Sopenharmony_ciwhich will be built as loadable modules if you select ``CONFIG_SAMPLE_KOBJECT``.
435