aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base/class.c
diff options
context:
space:
mode:
authorTejun Heo <tj@kernel.org>2008-08-25 13:50:19 -0400
committerJens Axboe <jens.axboe@oracle.com>2008-10-09 02:56:04 -0400
commit5a3ceb861663040f9ef0176df4aaa494bba5e352 (patch)
tree035472164252c3b54071f044c161b312b6bc8b44 /drivers/base/class.c
parenta1ed5b0cffe4b16a93a6a3390e8cee0fbef94f86 (diff)
driver-core: use klist for class device list and implement iterator
Iterating over entries using callback usually isn't too fun especially when the entry being iterated over can't be manipulated freely. This patch converts class->p->class_devices to klist and implements class device iterator so that the users can freely build their own control structure. The users are also free to call back into class code without worrying about locking. class_for_each_device() and class_find_device() are converted to use the new iterators, so their users don't have to worry about locking anymore either. Note: This depends on klist-dont-iterate-over-deleted-entries patch because class_intf->add/remove_dev() depends on proper synchronization with device removal. Signed-off-by: Tejun Heo <tj@kernel.org> Cc: Greg Kroah-Hartman <gregkh@suse.de> Cc: Jens Axboe <jens.axboe@oracle.com> Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
Diffstat (limited to 'drivers/base/class.c')
-rw-r--r--drivers/base/class.c136
1 files changed, 103 insertions, 33 deletions
diff --git a/drivers/base/class.c b/drivers/base/class.c
index cc5e28c8885c..eb85e4312301 100644
--- a/drivers/base/class.c
+++ b/drivers/base/class.c
@@ -135,6 +135,20 @@ static void remove_class_attrs(struct class *cls)
135 } 135 }
136} 136}
137 137
138static void klist_class_dev_get(struct klist_node *n)
139{
140 struct device *dev = container_of(n, struct device, knode_class);
141
142 get_device(dev);
143}
144
145static void klist_class_dev_put(struct klist_node *n)
146{
147 struct device *dev = container_of(n, struct device, knode_class);
148
149 put_device(dev);
150}
151
138int __class_register(struct class *cls, struct lock_class_key *key) 152int __class_register(struct class *cls, struct lock_class_key *key)
139{ 153{
140 struct class_private *cp; 154 struct class_private *cp;
@@ -145,7 +159,7 @@ int __class_register(struct class *cls, struct lock_class_key *key)
145 cp = kzalloc(sizeof(*cp), GFP_KERNEL); 159 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
146 if (!cp) 160 if (!cp)
147 return -ENOMEM; 161 return -ENOMEM;
148 INIT_LIST_HEAD(&cp->class_devices); 162 klist_init(&cp->class_devices, klist_class_dev_get, klist_class_dev_put);
149 INIT_LIST_HEAD(&cp->class_interfaces); 163 INIT_LIST_HEAD(&cp->class_interfaces);
150 kset_init(&cp->class_dirs); 164 kset_init(&cp->class_dirs);
151 __mutex_init(&cp->class_mutex, "struct class mutex", key); 165 __mutex_init(&cp->class_mutex, "struct class mutex", key);
@@ -269,6 +283,71 @@ char *make_class_name(const char *name, struct kobject *kobj)
269#endif 283#endif
270 284
271/** 285/**
286 * class_dev_iter_init - initialize class device iterator
287 * @iter: class iterator to initialize
288 * @class: the class we wanna iterate over
289 * @start: the device to start iterating from, if any
290 * @type: device_type of the devices to iterate over, NULL for all
291 *
292 * Initialize class iterator @iter such that it iterates over devices
293 * of @class. If @start is set, the list iteration will start there,
294 * otherwise if it is NULL, the iteration starts at the beginning of
295 * the list.
296 */
297void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
298 struct device *start, const struct device_type *type)
299{
300 struct klist_node *start_knode = NULL;
301
302 if (start)
303 start_knode = &start->knode_class;
304 klist_iter_init_node(&class->p->class_devices, &iter->ki, start_knode);
305 iter->type = type;
306}
307EXPORT_SYMBOL_GPL(class_dev_iter_init);
308
309/**
310 * class_dev_iter_next - iterate to the next device
311 * @iter: class iterator to proceed
312 *
313 * Proceed @iter to the next device and return it. Returns NULL if
314 * iteration is complete.
315 *
316 * The returned device is referenced and won't be released till
317 * iterator is proceed to the next device or exited. The caller is
318 * free to do whatever it wants to do with the device including
319 * calling back into class code.
320 */
321struct device *class_dev_iter_next(struct class_dev_iter *iter)
322{
323 struct klist_node *knode;
324 struct device *dev;
325
326 while (1) {
327 knode = klist_next(&iter->ki);
328 if (!knode)
329 return NULL;
330 dev = container_of(knode, struct device, knode_class);
331 if (!iter->type || iter->type == dev->type)
332 return dev;
333 }
334}
335EXPORT_SYMBOL_GPL(class_dev_iter_next);
336
337/**
338 * class_dev_iter_exit - finish iteration
339 * @iter: class iterator to finish
340 *
341 * Finish an iteration. Always call this function after iteration is
342 * complete whether the iteration ran till the end or not.
343 */
344void class_dev_iter_exit(struct class_dev_iter *iter)
345{
346 klist_iter_exit(&iter->ki);
347}
348EXPORT_SYMBOL_GPL(class_dev_iter_exit);
349
350/**
272 * class_for_each_device - device iterator 351 * class_for_each_device - device iterator
273 * @class: the class we're iterating 352 * @class: the class we're iterating
274 * @start: the device to start with in the list, if any. 353 * @start: the device to start with in the list, if any.
@@ -283,13 +362,13 @@ char *make_class_name(const char *name, struct kobject *kobj)
283 * We check the return of @fn each time. If it returns anything 362 * We check the return of @fn each time. If it returns anything
284 * other than 0, we break out and return that value. 363 * other than 0, we break out and return that value.
285 * 364 *
286 * Note, we hold class->class_mutex in this function, so it can not be 365 * @fn is allowed to do anything including calling back into class
287 * re-acquired in @fn, otherwise it will self-deadlocking. For 366 * code. There's no locking restriction.
288 * example, calls to add or remove class members would be verboten.
289 */ 367 */
290int class_for_each_device(struct class *class, struct device *start, 368int class_for_each_device(struct class *class, struct device *start,
291 void *data, int (*fn)(struct device *, void *)) 369 void *data, int (*fn)(struct device *, void *))
292{ 370{
371 struct class_dev_iter iter;
293 struct device *dev; 372 struct device *dev;
294 int error = 0; 373 int error = 0;
295 374
@@ -301,20 +380,13 @@ int class_for_each_device(struct class *class, struct device *start,
301 return -EINVAL; 380 return -EINVAL;
302 } 381 }
303 382
304 mutex_lock(&class->p->class_mutex); 383 class_dev_iter_init(&iter, class, start, NULL);
305 list_for_each_entry(dev, &class->p->class_devices, node) { 384 while ((dev = class_dev_iter_next(&iter))) {
306 if (start) {
307 if (start == dev)
308 start = NULL;
309 continue;
310 }
311 dev = get_device(dev);
312 error = fn(dev, data); 385 error = fn(dev, data);
313 put_device(dev);
314 if (error) 386 if (error)
315 break; 387 break;
316 } 388 }
317 mutex_unlock(&class->p->class_mutex); 389 class_dev_iter_exit(&iter);
318 390
319 return error; 391 return error;
320} 392}
@@ -337,16 +409,15 @@ EXPORT_SYMBOL_GPL(class_for_each_device);
337 * 409 *
338 * Note, you will need to drop the reference with put_device() after use. 410 * Note, you will need to drop the reference with put_device() after use.
339 * 411 *
340 * We hold class->class_mutex in this function, so it can not be 412 * @fn is allowed to do anything including calling back into class
341 * re-acquired in @match, otherwise it will self-deadlocking. For 413 * code. There's no locking restriction.
342 * example, calls to add or remove class members would be verboten.
343 */ 414 */
344struct device *class_find_device(struct class *class, struct device *start, 415struct device *class_find_device(struct class *class, struct device *start,
345 void *data, 416 void *data,
346 int (*match)(struct device *, void *)) 417 int (*match)(struct device *, void *))
347{ 418{
419 struct class_dev_iter iter;
348 struct device *dev; 420 struct device *dev;
349 int found = 0;
350 421
351 if (!class) 422 if (!class)
352 return NULL; 423 return NULL;
@@ -356,29 +427,23 @@ struct device *class_find_device(struct class *class, struct device *start,
356 return NULL; 427 return NULL;
357 } 428 }
358 429
359 mutex_lock(&class->p->class_mutex); 430 class_dev_iter_init(&iter, class, start, NULL);
360 list_for_each_entry(dev, &class->p->class_devices, node) { 431 while ((dev = class_dev_iter_next(&iter))) {
361 if (start) {
362 if (start == dev)
363 start = NULL;
364 continue;
365 }
366 dev = get_device(dev);
367 if (match(dev, data)) { 432 if (match(dev, data)) {
368 found = 1; 433 get_device(dev);
369 break; 434 break;
370 } else 435 }
371 put_device(dev);
372 } 436 }
373 mutex_unlock(&class->p->class_mutex); 437 class_dev_iter_exit(&iter);
374 438
375 return found ? dev : NULL; 439 return dev;
376} 440}
377EXPORT_SYMBOL_GPL(class_find_device); 441EXPORT_SYMBOL_GPL(class_find_device);
378 442
379int class_interface_register(struct class_interface *class_intf) 443int class_interface_register(struct class_interface *class_intf)
380{ 444{
381 struct class *parent; 445 struct class *parent;
446 struct class_dev_iter iter;
382 struct device *dev; 447 struct device *dev;
383 448
384 if (!class_intf || !class_intf->class) 449 if (!class_intf || !class_intf->class)
@@ -391,8 +456,10 @@ int class_interface_register(struct class_interface *class_intf)
391 mutex_lock(&parent->p->class_mutex); 456 mutex_lock(&parent->p->class_mutex);
392 list_add_tail(&class_intf->node, &parent->p->class_interfaces); 457 list_add_tail(&class_intf->node, &parent->p->class_interfaces);
393 if (class_intf->add_dev) { 458 if (class_intf->add_dev) {
394 list_for_each_entry(dev, &parent->p->class_devices, node) 459 class_dev_iter_init(&iter, parent, NULL, NULL);
460 while ((dev = class_dev_iter_next(&iter)))
395 class_intf->add_dev(dev, class_intf); 461 class_intf->add_dev(dev, class_intf);
462 class_dev_iter_exit(&iter);
396 } 463 }
397 mutex_unlock(&parent->p->class_mutex); 464 mutex_unlock(&parent->p->class_mutex);
398 465
@@ -402,6 +469,7 @@ int class_interface_register(struct class_interface *class_intf)
402void class_interface_unregister(struct class_interface *class_intf) 469void class_interface_unregister(struct class_interface *class_intf)
403{ 470{
404 struct class *parent = class_intf->class; 471 struct class *parent = class_intf->class;
472 struct class_dev_iter iter;
405 struct device *dev; 473 struct device *dev;
406 474
407 if (!parent) 475 if (!parent)
@@ -410,8 +478,10 @@ void class_interface_unregister(struct class_interface *class_intf)
410 mutex_lock(&parent->p->class_mutex); 478 mutex_lock(&parent->p->class_mutex);
411 list_del_init(&class_intf->node); 479 list_del_init(&class_intf->node);
412 if (class_intf->remove_dev) { 480 if (class_intf->remove_dev) {
413 list_for_each_entry(dev, &parent->p->class_devices, node) 481 class_dev_iter_init(&iter, parent, NULL, NULL);
482 while ((dev = class_dev_iter_next(&iter)))
414 class_intf->remove_dev(dev, class_intf); 483 class_intf->remove_dev(dev, class_intf);
484 class_dev_iter_exit(&iter);
415 } 485 }
416 mutex_unlock(&parent->p->class_mutex); 486 mutex_unlock(&parent->p->class_mutex);
417 487