diff options
Diffstat (limited to 'Documentation/kobject.txt')
-rw-r--r-- | Documentation/kobject.txt | 489 |
1 files changed, 293 insertions, 196 deletions
diff --git a/Documentation/kobject.txt b/Documentation/kobject.txt index ca86a885ad8f..bf3256e04027 100644 --- a/Documentation/kobject.txt +++ b/Documentation/kobject.txt | |||
@@ -1,289 +1,386 @@ | |||
1 | The kobject Infrastructure | 1 | Everything you never wanted to know about kobjects, ksets, and ktypes |
2 | 2 | ||
3 | Patrick Mochel <mochel@osdl.org> | 3 | Greg Kroah-Hartman <gregkh@suse.de> |
4 | 4 | ||
5 | Updated: 3 June 2003 | 5 | Based on an original article by Jon Corbet for lwn.net written October 1, |
6 | 2003 and located at http://lwn.net/Articles/51437/ | ||
6 | 7 | ||
8 | Last updated December 19, 2007 | ||
7 | 9 | ||
8 | Copyright (c) 2003 Patrick Mochel | ||
9 | Copyright (c) 2003 Open Source Development Labs | ||
10 | 10 | ||
11 | Part of the difficulty in understanding the driver model - and the kobject | ||
12 | abstraction upon which it is built - is that there is no obvious starting | ||
13 | place. Dealing with kobjects requires understanding a few different types, | ||
14 | all of which make reference to each other. In an attempt to make things | ||
15 | easier, we'll take a multi-pass approach, starting with vague terms and | ||
16 | adding detail as we go. To that end, here are some quick definitions of | ||
17 | some terms we will be working with. | ||
11 | 18 | ||
12 | 0. Introduction | 19 | - A kobject is an object of type struct kobject. Kobjects have a name |
20 | and a reference count. A kobject also has a parent pointer (allowing | ||
21 | objects to be arranged into hierarchies), a specific type, and, | ||
22 | usually, a representation in the sysfs virtual filesystem. | ||
13 | 23 | ||
14 | The kobject infrastructure performs basic object management that larger | 24 | Kobjects are generally not interesting on their own; instead, they are |
15 | data structures and subsystems can leverage, rather than reimplement | 25 | usually embedded within some other structure which contains the stuff |
16 | similar functionality. This functionality primarily concerns: | 26 | the code is really interested in. |
17 | 27 | ||
18 | - Object reference counting. | 28 | No structure should EVER have more than one kobject embedded within it. |
19 | - Maintaining lists (sets) of objects. | 29 | If it does, the reference counting for the object is sure to be messed |
20 | - Object set locking. | 30 | up and incorrect, and your code will be buggy. So do not do this. |
21 | - Userspace representation. | ||
22 | 31 | ||
23 | The infrastructure consists of a number of object types to support | 32 | - A ktype is the type of object that embeds a kobject. Every structure |
24 | this functionality. Their programming interfaces are described below | 33 | that embeds a kobject needs a corresponding ktype. The ktype controls |
25 | in detail, and briefly here: | 34 | what happens to the kobject when it is created and destroyed. |
26 | 35 | ||
27 | - kobjects a simple object. | 36 | - A kset is a group of kobjects. These kobjects can be of the same ktype |
28 | - kset a set of objects of a certain type. | 37 | or belong to different ktypes. The kset is the basic container type for |
29 | - ktype a set of helpers for objects of a common type. | 38 | collections of kobjects. Ksets contain their own kobjects, but you can |
39 | safely ignore that implementation detail as the kset core code handles | ||
40 | this kobject automatically. | ||
30 | 41 | ||
42 | When you see a sysfs directory full of other directories, generally each | ||
43 | of those directories corresponds to a kobject in the same kset. | ||
31 | 44 | ||
32 | The kobject infrastructure maintains a close relationship with the | 45 | We'll look at how to create and manipulate all of these types. A bottom-up |
33 | sysfs filesystem. Each kobject that is registered with the kobject | 46 | approach will be taken, so we'll go back to kobjects. |
34 | core receives a directory in sysfs. Attributes about the kobject can | ||
35 | then be exported. Please see Documentation/filesystems/sysfs.txt for | ||
36 | more information. | ||
37 | 47 | ||
38 | The kobject infrastructure provides a flexible programming interface, | ||
39 | and allows kobjects and ksets to be used without being registered | ||
40 | (i.e. with no sysfs representation). This is also described later. | ||
41 | 48 | ||
49 | Embedding kobjects | ||
42 | 50 | ||
43 | 1. kobjects | 51 | It is rare for kernel code to create a standalone kobject, with one major |
52 | exception explained below. Instead, kobjects are used to control access to | ||
53 | a larger, domain-specific object. To this end, kobjects will be found | ||
54 | embedded in other structures. If you are used to thinking of things in | ||
55 | object-oriented terms, kobjects can be seen as a top-level, abstract class | ||
56 | from which other classes are derived. A kobject implements a set of | ||
57 | capabilities which are not particularly useful by themselves, but which are | ||
58 | nice to have in other objects. The C language does not allow for the | ||
59 | direct expression of inheritance, so other techniques - such as structure | ||
60 | embedding - must be used. | ||
44 | 61 | ||
45 | 1.1 Description | 62 | So, for example, the UIO code has a structure that defines the memory |
63 | region associated with a uio device: | ||
46 | 64 | ||
65 | struct uio_mem { | ||
66 | struct kobject kobj; | ||
67 | unsigned long addr; | ||
68 | unsigned long size; | ||
69 | int memtype; | ||
70 | void __iomem *internal_addr; | ||
71 | }; | ||
47 | 72 | ||
48 | struct kobject is a simple data type that provides a foundation for | 73 | If you have a struct uio_mem structure, finding its embedded kobject is |
49 | more complex object types. It provides a set of basic fields that | 74 | just a matter of using the kobj member. Code that works with kobjects will |
50 | almost all complex data types share. kobjects are intended to be | 75 | often have the opposite problem, however: given a struct kobject pointer, |
51 | embedded in larger data structures and replace fields they duplicate. | 76 | what is the pointer to the containing structure? You must avoid tricks |
77 | (such as assuming that the kobject is at the beginning of the structure) | ||
78 | and, instead, use the container_of() macro, found in <linux/kernel.h>: | ||
52 | 79 | ||
53 | 1.2 Definition | 80 | container_of(pointer, type, member) |
54 | 81 | ||
55 | struct kobject { | 82 | where pointer is the pointer to the embedded kobject, type is the type of |
56 | const char * k_name; | 83 | the containing structure, and member is the name of the structure field to |
57 | struct kref kref; | 84 | which pointer points. The return value from container_of() is a pointer to |
58 | struct list_head entry; | 85 | the given type. So, for example, a pointer "kp" to a struct kobject |
59 | struct kobject * parent; | 86 | embedded within a struct uio_mem could be converted to a pointer to the |
60 | struct kset * kset; | 87 | containing uio_mem structure with: |
61 | struct kobj_type * ktype; | ||
62 | struct sysfs_dirent * sd; | ||
63 | wait_queue_head_t poll; | ||
64 | }; | ||
65 | 88 | ||
66 | void kobject_init(struct kobject *); | 89 | struct uio_mem *u_mem = container_of(kp, struct uio_mem, kobj); |
67 | int kobject_add(struct kobject *); | ||
68 | int kobject_register(struct kobject *); | ||
69 | 90 | ||
70 | void kobject_del(struct kobject *); | 91 | Programmers often define a simple macro for "back-casting" kobject pointers |
71 | void kobject_unregister(struct kobject *); | 92 | to the containing type. |
72 | 93 | ||
73 | struct kobject * kobject_get(struct kobject *); | ||
74 | void kobject_put(struct kobject *); | ||
75 | 94 | ||
95 | Initialization of kobjects | ||
76 | 96 | ||
77 | 1.3 kobject Programming Interface | 97 | Code which creates a kobject must, of course, initialize that object. Some |
98 | of the internal fields are setup with a (mandatory) call to kobject_init(): | ||
78 | 99 | ||
79 | kobjects may be dynamically added and removed from the kobject core | 100 | void kobject_init(struct kobject *kobj, struct kobj_type *ktype); |
80 | using kobject_register() and kobject_unregister(). Registration | ||
81 | includes inserting the kobject in the list of its dominant kset and | ||
82 | creating a directory for it in sysfs. | ||
83 | 101 | ||
84 | Alternatively, one may use a kobject without adding it to its kset's list | 102 | The ktype is required for a kobject to be created properly, as every kobject |
85 | or exporting it via sysfs, by simply calling kobject_init(). An | 103 | must have an associated kobj_type. After calling kobject_init(), to |
86 | initialized kobject may later be added to the object hierarchy by | 104 | register the kobject with sysfs, the function kobject_add() must be called: |
87 | calling kobject_add(). An initialized kobject may be used for | ||
88 | reference counting. | ||
89 | 105 | ||
90 | Note: calling kobject_init() then kobject_add() is functionally | 106 | int kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...); |
91 | equivalent to calling kobject_register(). | ||
92 | 107 | ||
93 | When a kobject is unregistered, it is removed from its kset's list, | 108 | This sets up the parent of the kobject and the name for the kobject |
94 | removed from the sysfs filesystem, and its reference count is decremented. | 109 | properly. If the kobject is to be associated with a specific kset, |
95 | List and sysfs removal happen in kobject_del(), and may be called | 110 | kobj->kset must be assigned before calling kobject_add(). If a kset is |
96 | manually. kobject_put() decrements the reference count, and may also | 111 | associated with a kobject, then the parent for the kobject can be set to |
97 | be called manually. | 112 | NULL in the call to kobject_add() and then the kobject's parent will be the |
113 | kset itself. | ||
98 | 114 | ||
99 | A kobject's reference count may be incremented with kobject_get(), | 115 | As the name of the kobject is set when it is added to the kernel, the name |
100 | which returns a valid reference to a kobject; and decremented with | 116 | of the kobject should never be manipulated directly. If you must change |
101 | kobject_put(). An object's reference count may only be incremented if | 117 | the name of the kobject, call kobject_rename(): |
102 | it is already positive. | ||
103 | 118 | ||
104 | When a kobject's reference count reaches 0, the method struct | 119 | int kobject_rename(struct kobject *kobj, const char *new_name); |
105 | kobj_type::release() (which the kobject's kset points to) is called. | ||
106 | This allows any memory allocated for the object to be freed. | ||
107 | 120 | ||
121 | There is a function called kobject_set_name() but that is legacy cruft and | ||
122 | is being removed. If your code needs to call this function, it is | ||
123 | incorrect and needs to be fixed. | ||
108 | 124 | ||
109 | NOTE!!! | 125 | To properly access the name of the kobject, use the function |
126 | kobject_name(): | ||
110 | 127 | ||
111 | It is _imperative_ that you supply a destructor for dynamically | 128 | const char *kobject_name(const struct kobject * kobj); |
112 | allocated kobjects to free them if you are using kobject reference | ||
113 | counts. The reference count controls the lifetime of the object. | ||
114 | If it goes to 0, then it is assumed that the object will | ||
115 | be freed and cannot be used. | ||
116 | 129 | ||
117 | More importantly, you must free the object there, and not immediately | 130 | There is a helper function to both initialize and add the kobject to the |
118 | after an unregister call. If someone else is referencing the object | 131 | kernel at the same time, called supprisingly enough kobject_init_and_add(): |
119 | (e.g. through a sysfs file), they will obtain a reference to the | ||
120 | object, assume it's valid and operate on it. If the object is | ||
121 | unregistered and freed in the meantime, the operation will then | ||
122 | reference freed memory and go boom. | ||
123 | 132 | ||
124 | This can be prevented, in the simplest case, by defining a release | 133 | int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, |
125 | method and freeing the object from there only. Note that this will not | 134 | struct kobject *parent, const char *fmt, ...); |
126 | secure reference count/object management models that use a dual | ||
127 | reference count or do other wacky things with the reference count | ||
128 | (like the networking layer). | ||
129 | 135 | ||
136 | The arguments are the same as the individual kobject_init() and | ||
137 | kobject_add() functions described above. | ||
130 | 138 | ||
131 | 1.4 sysfs | ||
132 | 139 | ||
133 | Each kobject receives a directory in sysfs. This directory is created | 140 | Uevents |
134 | under the kobject's parent directory. | ||
135 | 141 | ||
136 | If a kobject does not have a parent when it is registered, its parent | 142 | After a kobject has been registered with the kobject core, you need to |
137 | becomes its dominant kset. | 143 | announce to the world that it has been created. This can be done with a |
144 | call to kobject_uevent(): | ||
138 | 145 | ||
139 | If a kobject does not have a parent nor a dominant kset, its directory | 146 | int kobject_uevent(struct kobject *kobj, enum kobject_action action); |
140 | is created at the top-level of the sysfs partition. | ||
141 | 147 | ||
148 | Use the KOBJ_ADD action for when the kobject is first added to the kernel. | ||
149 | This should be done only after any attributes or children of the kobject | ||
150 | have been initialized properly, as userspace will instantly start to look | ||
151 | for them when this call happens. | ||
142 | 152 | ||
153 | When the kobject is removed from the kernel (details on how to do that is | ||
154 | below), the uevent for KOBJ_REMOVE will be automatically created by the | ||
155 | kobject core, so the caller does not have to worry about doing that by | ||
156 | hand. | ||
143 | 157 | ||
144 | 2. ksets | ||
145 | 158 | ||
146 | 2.1 Description | 159 | Reference counts |
147 | 160 | ||
148 | A kset is a set of kobjects that are embedded in the same type. | 161 | One of the key functions of a kobject is to serve as a reference counter |
162 | for the object in which it is embedded. As long as references to the object | ||
163 | exist, the object (and the code which supports it) must continue to exist. | ||
164 | The low-level functions for manipulating a kobject's reference counts are: | ||
149 | 165 | ||
166 | struct kobject *kobject_get(struct kobject *kobj); | ||
167 | void kobject_put(struct kobject *kobj); | ||
150 | 168 | ||
151 | struct kset { | 169 | A successful call to kobject_get() will increment the kobject's reference |
152 | struct kobj_type * ktype; | 170 | counter and return the pointer to the kobject. |
153 | struct list_head list; | ||
154 | struct kobject kobj; | ||
155 | struct kset_uevent_ops * uevent_ops; | ||
156 | }; | ||
157 | 171 | ||
172 | When a reference is released, the call to kobject_put() will decrement the | ||
173 | reference count and, possibly, free the object. Note that kobject_init() | ||
174 | sets the reference count to one, so the code which sets up the kobject will | ||
175 | need to do a kobject_put() eventually to release that reference. | ||
158 | 176 | ||
159 | void kset_init(struct kset * k); | 177 | Because kobjects are dynamic, they must not be declared statically or on |
160 | int kset_add(struct kset * k); | 178 | the stack, but instead, always allocated dynamically. Future versions of |
161 | int kset_register(struct kset * k); | 179 | the kernel will contain a run-time check for kobjects that are created |
162 | void kset_unregister(struct kset * k); | 180 | statically and will warn the developer of this improper usage. |
163 | 181 | ||
164 | struct kset * kset_get(struct kset * k); | 182 | If all that you want to use a kobject for is to provide a reference counter |
165 | void kset_put(struct kset * k); | 183 | for your structure, please use the struct kref instead; a kobject would be |
184 | overkill. For more information on how to use struct kref, please see the | ||
185 | file Documentation/kref.txt in the Linux kernel source tree. | ||
166 | 186 | ||
167 | struct kobject * kset_find_obj(struct kset *, char *); | ||
168 | 187 | ||
188 | Creating "simple" kobjects | ||
169 | 189 | ||
170 | The type that the kobjects are embedded in is described by the ktype | 190 | Sometimes all that a developer wants is a way to create a simple directory |
171 | pointer. | 191 | in the sysfs hierarchy, and not have to mess with the whole complication of |
192 | ksets, show and store functions, and other details. This is the one | ||
193 | exception where a single kobject should be created. To create such an | ||
194 | entry, use the function: | ||
172 | 195 | ||
173 | A kset contains a kobject itself, meaning that it may be registered in | 196 | struct kobject *kobject_create_and_add(char *name, struct kobject *parent); |
174 | the kobject hierarchy and exported via sysfs. More importantly, the | ||
175 | kset may be embedded in a larger data type, and may be part of another | ||
176 | kset (of that object type). | ||
177 | 197 | ||
178 | For example, a block device is an object (struct gendisk) that is | 198 | This function will create a kobject and place it in sysfs in the location |
179 | contained in a set of block devices. It may also contain a set of | 199 | underneath the specified parent kobject. To create simple attributes |
180 | partitions (struct hd_struct) that have been found on the device. The | 200 | associated with this kobject, use: |
181 | following code snippet illustrates how to express this properly. | ||
182 | 201 | ||
183 | struct gendisk * disk; | 202 | int sysfs_create_file(struct kobject *kobj, struct attribute *attr); |
184 | ... | 203 | or |
185 | disk->kset.kobj.kset = &block_kset; | 204 | int sysfs_create_group(struct kobject *kobj, struct attribute_group *grp); |
186 | disk->kset.ktype = &partition_ktype; | ||
187 | kset_register(&disk->kset); | ||
188 | 205 | ||
189 | - The kset that the disk's embedded object belongs to is the | 206 | Both types of attributes used here, with a kobject that has been created |
190 | block_kset, and is pointed to by disk->kset.kobj.kset. | 207 | with the kobject_create_and_add(), can be of type kobj_attribute, so no |
208 | special custom attribute is needed to be created. | ||
191 | 209 | ||
192 | - The type of objects on the disk's _subordinate_ list are partitions, | 210 | See the example module, samples/kobject/kobject-example.c for an |
193 | and is set in disk->kset.ktype. | 211 | implementation of a simple kobject and attributes. |
194 | 212 | ||
195 | - The kset is then registered, which handles initializing and adding | ||
196 | the embedded kobject to the hierarchy. | ||
197 | 213 | ||
198 | 214 | ||
199 | 2.2 kset Programming Interface | 215 | ktypes and release methods |
200 | 216 | ||
201 | All kset functions, except kset_find_obj(), eventually forward the | 217 | One important thing still missing from the discussion is what happens to a |
202 | calls to their embedded kobjects after performing kset-specific | 218 | kobject when its reference count reaches zero. The code which created the |
203 | operations. ksets offer a similar programming model to kobjects: they | 219 | kobject generally does not know when that will happen; if it did, there |
204 | may be used after they are initialized, without registering them in | 220 | would be little point in using a kobject in the first place. Even |
205 | the hierarchy. | 221 | predictable object lifecycles become more complicated when sysfs is brought |
222 | in as other portions of the kernel can get a reference on any kobject that | ||
223 | is registered in the system. | ||
206 | 224 | ||
207 | kset_find_obj() may be used to locate a kobject with a particular | 225 | The end result is that a structure protected by a kobject cannot be freed |
208 | name. The kobject, if found, is returned. | 226 | before its reference count goes to zero. The reference count is not under |
227 | the direct control of the code which created the kobject. So that code must | ||
228 | be notified asynchronously whenever the last reference to one of its | ||
229 | kobjects goes away. | ||
209 | 230 | ||
210 | There are also some helper functions which names point to the formerly | 231 | Once you registered your kobject via kobject_add(), you must never use |
211 | existing "struct subsystem", whose functions have been taken over by | 232 | kfree() to free it directly. The only safe way is to use kobject_put(). It |
212 | ksets. | 233 | is good practice to always use kobject_put() after kobject_init() to avoid |
234 | errors creeping in. | ||
213 | 235 | ||
236 | This notification is done through a kobject's release() method. Usually | ||
237 | such a method has a form like: | ||
214 | 238 | ||
215 | decl_subsys(name,type,uevent_ops) | 239 | void my_object_release(struct kobject *kobj) |
240 | { | ||
241 | struct my_object *mine = container_of(kobj, struct my_object, kobj); | ||
216 | 242 | ||
217 | Declares a kset named '<name>_subsys' of type <type> with | 243 | /* Perform any additional cleanup on this object, then... */ |
218 | uevent_ops <uevent_ops>. For example, | 244 | kfree(mine); |
245 | } | ||
219 | 246 | ||
220 | decl_subsys(devices, &ktype_device, &device_uevent_ops); | 247 | One important point cannot be overstated: every kobject must have a |
248 | release() method, and the kobject must persist (in a consistent state) | ||
249 | until that method is called. If these constraints are not met, the code is | ||
250 | flawed. Note that the kernel will warn you if you forget to provide a | ||
251 | release() method. Do not try to get rid of this warning by providing an | ||
252 | "empty" release function; you will be mocked mercilessly by the kobject | ||
253 | maintainer if you attempt this. | ||
221 | 254 | ||
222 | is equivalent to doing: | 255 | Note, the name of the kobject is available in the release function, but it |
256 | must NOT be changed within this callback. Otherwise there will be a memory | ||
257 | leak in the kobject core, which makes people unhappy. | ||
223 | 258 | ||
224 | struct kset devices_subsys = { | 259 | Interestingly, the release() method is not stored in the kobject itself; |
225 | .ktype = &ktype_devices, | 260 | instead, it is associated with the ktype. So let us introduce struct |
226 | .uevent_ops = &device_uevent_ops, | 261 | kobj_type: |
227 | }; | 262 | |
228 | kobject_set_name(&devices_subsys, name); | 263 | struct kobj_type { |
264 | void (*release)(struct kobject *); | ||
265 | struct sysfs_ops *sysfs_ops; | ||
266 | struct attribute **default_attrs; | ||
267 | }; | ||
229 | 268 | ||
230 | The objects that are registered with a subsystem that use the | 269 | This structure is used to describe a particular type of kobject (or, more |
231 | subsystem's default list must have their kset ptr set properly. These | 270 | correctly, of containing object). Every kobject needs to have an associated |
232 | objects may have embedded kobjects or ksets. The | 271 | kobj_type structure; a pointer to that structure must be specified when you |
233 | following helper makes setting the kset easier: | 272 | call kobject_init() or kobject_init_and_add(). |
234 | 273 | ||
274 | The release field in struct kobj_type is, of course, a pointer to the | ||
275 | release() method for this type of kobject. The other two fields (sysfs_ops | ||
276 | and default_attrs) control how objects of this type are represented in | ||
277 | sysfs; they are beyond the scope of this document. | ||
235 | 278 | ||
236 | kobj_set_kset_s(obj,subsys) | 279 | The default_attrs pointer is a list of default attributes that will be |
280 | automatically created for any kobject that is registered with this ktype. | ||
237 | 281 | ||
238 | - Assumes that obj->kobj exists, and is a struct kobject. | ||
239 | - Sets the kset of that kobject to the kset <subsys>. | ||
240 | 282 | ||
241 | int subsystem_register(struct kset *s); | 283 | ksets |
242 | void subsystem_unregister(struct kset *s); | ||
243 | 284 | ||
244 | These are just wrappers around the respective kset_* functions. | 285 | A kset is merely a collection of kobjects that want to be associated with |
286 | each other. There is no restriction that they be of the same ktype, but be | ||
287 | very careful if they are not. | ||
245 | 288 | ||
246 | 2.3 sysfs | 289 | A kset serves these functions: |
247 | 290 | ||
248 | ksets are represented in sysfs when their embedded kobjects are | 291 | - It serves as a bag containing a group of objects. A kset can be used by |
249 | registered. They follow the same rules of parenting, with one | 292 | the kernel to track "all block devices" or "all PCI device drivers." |
250 | exception. If a kset does not have a parent, nor is its embedded | ||
251 | kobject part of another kset, the kset's parent becomes its dominant | ||
252 | subsystem. | ||
253 | 293 | ||
254 | If the kset does not have a parent, its directory is created at the | 294 | - A kset is also a subdirectory in sysfs, where the associated kobjects |
255 | sysfs root. This should only happen when the kset registered is | 295 | with the kset can show up. Every kset contains a kobject which can be |
256 | embedded in a subsystem itself. | 296 | set up to be the parent of other kobjects; the top-level directories of |
297 | the sysfs hierarchy are constructed in this way. | ||
257 | 298 | ||
299 | - Ksets can support the "hotplugging" of kobjects and influence how | ||
300 | uevent events are reported to user space. | ||
258 | 301 | ||
259 | 3. struct ktype | 302 | In object-oriented terms, "kset" is the top-level container class; ksets |
303 | contain their own kobject, but that kobject is managed by the kset code and | ||
304 | should not be manipulated by any other user. | ||
260 | 305 | ||
261 | 3.1. Description | 306 | A kset keeps its children in a standard kernel linked list. Kobjects point |
307 | back to their containing kset via their kset field. In almost all cases, | ||
308 | the kobjects belonging to a ket have that kset (or, strictly, its embedded | ||
309 | kobject) in their parent. | ||
262 | 310 | ||
263 | struct kobj_type { | 311 | As a kset contains a kobject within it, it should always be dynamically |
264 | void (*release)(struct kobject *); | 312 | created and never declared statically or on the stack. To create a new |
265 | struct sysfs_ops * sysfs_ops; | 313 | kset use: |
266 | struct attribute ** default_attrs; | 314 | struct kset *kset_create_and_add(const char *name, |
315 | struct kset_uevent_ops *u, | ||
316 | struct kobject *parent); | ||
317 | |||
318 | When you are finished with the kset, call: | ||
319 | void kset_unregister(struct kset *kset); | ||
320 | to destroy it. | ||
321 | |||
322 | An example of using a kset can be seen in the | ||
323 | samples/kobject/kset-example.c file in the kernel tree. | ||
324 | |||
325 | If a kset wishes to control the uevent operations of the kobjects | ||
326 | associated with it, it can use the struct kset_uevent_ops to handle it: | ||
327 | |||
328 | struct kset_uevent_ops { | ||
329 | int (*filter)(struct kset *kset, struct kobject *kobj); | ||
330 | const char *(*name)(struct kset *kset, struct kobject *kobj); | ||
331 | int (*uevent)(struct kset *kset, struct kobject *kobj, | ||
332 | struct kobj_uevent_env *env); | ||
267 | }; | 333 | }; |
268 | 334 | ||
269 | 335 | ||
270 | Object types require specific functions for converting between the | 336 | The filter function allows a kset to prevent a uevent from being emitted to |
271 | generic object and the more complex type. struct kobj_type provides | 337 | userspace for a specific kobject. If the function returns 0, the uevent |
272 | the object-specific fields, which include: | 338 | will not be emitted. |
339 | |||
340 | The name function will be called to override the default name of the kset | ||
341 | that the uevent sends to userspace. By default, the name will be the same | ||
342 | as the kset itself, but this function, if present, can override that name. | ||
343 | |||
344 | The uevent function will be called when the uevent is about to be sent to | ||
345 | userspace to allow more environment variables to be added to the uevent. | ||
346 | |||
347 | One might ask how, exactly, a kobject is added to a kset, given that no | ||
348 | functions which perform that function have been presented. The answer is | ||
349 | that this task is handled by kobject_add(). When a kobject is passed to | ||
350 | kobject_add(), its kset member should point to the kset to which the | ||
351 | kobject will belong. kobject_add() will handle the rest. | ||
352 | |||
353 | If the kobject belonging to a kset has no parent kobject set, it will be | ||
354 | added to the kset's directory. Not all members of a kset do necessarily | ||
355 | live in the kset directory. If an explicit parent kobject is assigned | ||
356 | before the kobject is added, the kobject is registered with the kset, but | ||
357 | added below the parent kobject. | ||
358 | |||
359 | |||
360 | Kobject removal | ||
273 | 361 | ||
274 | - release: Called when the kobject's reference count reaches 0. This | 362 | After a kobject has been registered with the kobject core successfully, it |
275 | should convert the object to the more complex type and free it. | 363 | must be cleaned up when the code is finished with it. To do that, call |
364 | kobject_put(). By doing this, the kobject core will automatically clean up | ||
365 | all of the memory allocated by this kobject. If a KOBJ_ADD uevent has been | ||
366 | sent for the object, a corresponding KOBJ_REMOVE uevent will be sent, and | ||
367 | any other sysfs housekeeping will be handled for the caller properly. | ||
276 | 368 | ||
277 | - sysfs_ops: Provides conversion functions for sysfs access. Please | 369 | If you need to do a two-stage delete of the kobject (say you are not |
278 | see the sysfs documentation for more information. | 370 | allowed to sleep when you need to destroy the object), then call |
371 | kobject_del() which will unregister the kobject from sysfs. This makes the | ||
372 | kobject "invisible", but it is not cleaned up, and the reference count of | ||
373 | the object is still the same. At a later time call kobject_put() to finish | ||
374 | the cleanup of the memory associated with the kobject. | ||
279 | 375 | ||
280 | - default_attrs: Default attributes to be exported via sysfs when the | 376 | kobject_del() can be used to drop the reference to the parent object, if |
281 | object is registered.Note that the last attribute has to be | 377 | circular references are constructed. It is valid in some cases, that a |
282 | initialized to NULL ! You can find a complete implementation | 378 | parent objects references a child. Circular references _must_ be broken |
283 | in block/genhd.c | 379 | with an explicit call to kobject_del(), so that a release functions will be |
380 | called, and the objects in the former circle release each other. | ||
284 | 381 | ||
285 | 382 | ||
286 | Instances of struct kobj_type are not registered; only referenced by | 383 | Example code to copy from |
287 | the kset. A kobj_type may be referenced by an arbitrary number of | ||
288 | ksets, as there may be disparate sets of identical objects. | ||
289 | 384 | ||
385 | For a more complete example of using ksets and kobjects properly, see the | ||
386 | sample/kobject/kset-example.c code. | ||