URL
https://opencores.org/ocsvn/test_project/test_project/trunk
Subversion Repositories test_project
[/] [test_project/] [trunk/] [linux_sd_driver/] [Documentation/] [kobject.txt] - Rev 62
Compare with Previous | Blame | View Log
The kobject InfrastructurePatrick Mochel <mochel@osdl.org>Updated: 3 June 2003Copyright (c) 2003 Patrick MochelCopyright (c) 2003 Open Source Development Labs0. IntroductionThe kobject infrastructure performs basic object management that largerdata structures and subsystems can leverage, rather than reimplementsimilar functionality. This functionality primarily concerns:- Object reference counting.- Maintaining lists (sets) of objects.- Object set locking.- Userspace representation.The infrastructure consists of a number of object types to supportthis functionality. Their programming interfaces are described belowin detail, and briefly here:- kobjects a simple object.- kset a set of objects of a certain type.- ktype a set of helpers for objects of a common type.The kobject infrastructure maintains a close relationship with thesysfs filesystem. Each kobject that is registered with the kobjectcore receives a directory in sysfs. Attributes about the kobject canthen be exported. Please see Documentation/filesystems/sysfs.txt formore information.The kobject infrastructure provides a flexible programming interface,and allows kobjects and ksets to be used without being registered(i.e. with no sysfs representation). This is also described later.1. kobjects1.1 Descriptionstruct kobject is a simple data type that provides a foundation formore complex object types. It provides a set of basic fields thatalmost all complex data types share. kobjects are intended to beembedded in larger data structures and replace fields they duplicate.1.2 Definitionstruct kobject {const char * k_name;struct kref kref;struct list_head entry;struct kobject * parent;struct kset * kset;struct kobj_type * ktype;struct sysfs_dirent * sd;wait_queue_head_t poll;};void kobject_init(struct kobject *);int kobject_add(struct kobject *);int kobject_register(struct kobject *);void kobject_del(struct kobject *);void kobject_unregister(struct kobject *);struct kobject * kobject_get(struct kobject *);void kobject_put(struct kobject *);1.3 kobject Programming Interfacekobjects may be dynamically added and removed from the kobject coreusing kobject_register() and kobject_unregister(). Registrationincludes inserting the kobject in the list of its dominant kset andcreating a directory for it in sysfs.Alternatively, one may use a kobject without adding it to its kset's listor exporting it via sysfs, by simply calling kobject_init(). Aninitialized kobject may later be added to the object hierarchy bycalling kobject_add(). An initialized kobject may be used forreference counting.Note: calling kobject_init() then kobject_add() is functionallyequivalent to calling kobject_register().When a kobject is unregistered, it is removed from its kset's list,removed from the sysfs filesystem, and its reference count is decremented.List and sysfs removal happen in kobject_del(), and may be calledmanually. kobject_put() decrements the reference count, and may alsobe called manually.A kobject's reference count may be incremented with kobject_get(),which returns a valid reference to a kobject; and decremented withkobject_put(). An object's reference count may only be incremented ifit is already positive.When a kobject's reference count reaches 0, the method structkobj_type::release() (which the kobject's kset points to) is called.This allows any memory allocated for the object to be freed.NOTE!!!It is _imperative_ that you supply a destructor for dynamicallyallocated kobjects to free them if you are using kobject referencecounts. The reference count controls the lifetime of the object.If it goes to 0, then it is assumed that the object willbe freed and cannot be used.More importantly, you must free the object there, and not immediatelyafter an unregister call. If someone else is referencing the object(e.g. through a sysfs file), they will obtain a reference to theobject, assume it's valid and operate on it. If the object isunregistered and freed in the meantime, the operation will thenreference freed memory and go boom.This can be prevented, in the simplest case, by defining a releasemethod and freeing the object from there only. Note that this will notsecure reference count/object management models that use a dualreference count or do other wacky things with the reference count(like the networking layer).1.4 sysfsEach kobject receives a directory in sysfs. This directory is createdunder the kobject's parent directory.If a kobject does not have a parent when it is registered, its parentbecomes its dominant kset.If a kobject does not have a parent nor a dominant kset, its directoryis created at the top-level of the sysfs partition.2. ksets2.1 DescriptionA kset is a set of kobjects that are embedded in the same type.struct kset {struct kobj_type * ktype;struct list_head list;struct kobject kobj;struct kset_uevent_ops * uevent_ops;};void kset_init(struct kset * k);int kset_add(struct kset * k);int kset_register(struct kset * k);void kset_unregister(struct kset * k);struct kset * kset_get(struct kset * k);void kset_put(struct kset * k);struct kobject * kset_find_obj(struct kset *, char *);The type that the kobjects are embedded in is described by the ktypepointer.A kset contains a kobject itself, meaning that it may be registered inthe kobject hierarchy and exported via sysfs. More importantly, thekset may be embedded in a larger data type, and may be part of anotherkset (of that object type).For example, a block device is an object (struct gendisk) that iscontained in a set of block devices. It may also contain a set ofpartitions (struct hd_struct) that have been found on the device. Thefollowing code snippet illustrates how to express this properly.struct gendisk * disk;...disk->kset.kobj.kset = &block_kset;disk->kset.ktype = &partition_ktype;kset_register(&disk->kset);- The kset that the disk's embedded object belongs to is theblock_kset, and is pointed to by disk->kset.kobj.kset.- The type of objects on the disk's _subordinate_ list are partitions,and is set in disk->kset.ktype.- The kset is then registered, which handles initializing and addingthe embedded kobject to the hierarchy.2.2 kset Programming InterfaceAll kset functions, except kset_find_obj(), eventually forward thecalls to their embedded kobjects after performing kset-specificoperations. ksets offer a similar programming model to kobjects: theymay be used after they are initialized, without registering them inthe hierarchy.kset_find_obj() may be used to locate a kobject with a particularname. The kobject, if found, is returned.There are also some helper functions which names point to the formerlyexisting "struct subsystem", whose functions have been taken over byksets.decl_subsys(name,type,uevent_ops)Declares a kset named '<name>_subsys' of type <type> withuevent_ops <uevent_ops>. For example,decl_subsys(devices, &ktype_device, &device_uevent_ops);is equivalent to doing:struct kset devices_subsys = {.ktype = &ktype_devices,.uevent_ops = &device_uevent_ops,};kobject_set_name(&devices_subsys, name);The objects that are registered with a subsystem that use thesubsystem's default list must have their kset ptr set properly. Theseobjects may have embedded kobjects or ksets. Thefollowing helper makes setting the kset easier:kobj_set_kset_s(obj,subsys)- Assumes that obj->kobj exists, and is a struct kobject.- Sets the kset of that kobject to the kset <subsys>.int subsystem_register(struct kset *s);void subsystem_unregister(struct kset *s);These are just wrappers around the respective kset_* functions.2.3 sysfsksets are represented in sysfs when their embedded kobjects areregistered. They follow the same rules of parenting, with oneexception. If a kset does not have a parent, nor is its embeddedkobject part of another kset, the kset's parent becomes its dominantsubsystem.If the kset does not have a parent, its directory is created at thesysfs root. This should only happen when the kset registered isembedded in a subsystem itself.3. struct ktype3.1. Descriptionstruct kobj_type {void (*release)(struct kobject *);struct sysfs_ops * sysfs_ops;struct attribute ** default_attrs;};Object types require specific functions for converting between thegeneric object and the more complex type. struct kobj_type providesthe object-specific fields, which include:- release: Called when the kobject's reference count reaches 0. Thisshould convert the object to the more complex type and free it.- sysfs_ops: Provides conversion functions for sysfs access. Pleasesee the sysfs documentation for more information.- default_attrs: Default attributes to be exported via sysfs when theobject is registered.Note that the last attribute has to beinitialized to NULL ! You can find a complete implementationin block/genhd.cInstances of struct kobj_type are not registered; only referenced bythe kset. A kobj_type may be referenced by an arbitrary number ofksets, as there may be disparate sets of identical objects.
