aboutsummaryrefslogtreecommitdiffstats
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
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>
-rw-r--r--drivers/base/base.h2
-rw-r--r--drivers/base/class.c136
-rw-r--r--drivers/base/core.c6
-rw-r--r--include/linux/device.h14
4 files changed, 120 insertions, 38 deletions
diff --git a/drivers/base/base.h b/drivers/base/base.h
index 31dc0cd84afa..0a5f055dffba 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -54,7 +54,7 @@ struct driver_private {
54 */ 54 */
55struct class_private { 55struct class_private {
56 struct kset class_subsys; 56 struct kset class_subsys;
57 struct list_head class_devices; 57 struct klist class_devices;
58 struct list_head class_interfaces; 58 struct list_head class_interfaces;
59 struct kset class_dirs; 59 struct kset class_dirs;
60 struct mutex class_mutex; 60 struct mutex class_mutex;
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
diff --git a/drivers/base/core.c b/drivers/base/core.c
index d021c98605b3..b98cb1416a2d 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -536,7 +536,6 @@ void device_initialize(struct device *dev)
536 klist_init(&dev->klist_children, klist_children_get, 536 klist_init(&dev->klist_children, klist_children_get,
537 klist_children_put); 537 klist_children_put);
538 INIT_LIST_HEAD(&dev->dma_pools); 538 INIT_LIST_HEAD(&dev->dma_pools);
539 INIT_LIST_HEAD(&dev->node);
540 init_MUTEX(&dev->sem); 539 init_MUTEX(&dev->sem);
541 spin_lock_init(&dev->devres_lock); 540 spin_lock_init(&dev->devres_lock);
542 INIT_LIST_HEAD(&dev->devres_head); 541 INIT_LIST_HEAD(&dev->devres_head);
@@ -916,7 +915,8 @@ int device_add(struct device *dev)
916 if (dev->class) { 915 if (dev->class) {
917 mutex_lock(&dev->class->p->class_mutex); 916 mutex_lock(&dev->class->p->class_mutex);
918 /* tie the class to the device */ 917 /* tie the class to the device */
919 list_add_tail(&dev->node, &dev->class->p->class_devices); 918 klist_add_tail(&dev->knode_class,
919 &dev->class->p->class_devices);
920 920
921 /* notify any interfaces that the device is here */ 921 /* notify any interfaces that the device is here */
922 list_for_each_entry(class_intf, 922 list_for_each_entry(class_intf,
@@ -1032,7 +1032,7 @@ void device_del(struct device *dev)
1032 if (class_intf->remove_dev) 1032 if (class_intf->remove_dev)
1033 class_intf->remove_dev(dev, class_intf); 1033 class_intf->remove_dev(dev, class_intf);
1034 /* remove the device from the class list */ 1034 /* remove the device from the class list */
1035 list_del_init(&dev->node); 1035 klist_del(&dev->knode_class);
1036 mutex_unlock(&dev->class->p->class_mutex); 1036 mutex_unlock(&dev->class->p->class_mutex);
1037 } 1037 }
1038 device_remove_file(dev, &uevent_attr); 1038 device_remove_file(dev, &uevent_attr);
diff --git a/include/linux/device.h b/include/linux/device.h
index 4d8372d135df..246937c9cbc7 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -199,6 +199,11 @@ struct class {
199 struct class_private *p; 199 struct class_private *p;
200}; 200};
201 201
202struct class_dev_iter {
203 struct klist_iter ki;
204 const struct device_type *type;
205};
206
202extern struct kobject *sysfs_dev_block_kobj; 207extern struct kobject *sysfs_dev_block_kobj;
203extern struct kobject *sysfs_dev_char_kobj; 208extern struct kobject *sysfs_dev_char_kobj;
204extern int __must_check __class_register(struct class *class, 209extern int __must_check __class_register(struct class *class,
@@ -213,6 +218,13 @@ extern void class_unregister(struct class *class);
213 __class_register(class, &__key); \ 218 __class_register(class, &__key); \
214}) 219})
215 220
221extern void class_dev_iter_init(struct class_dev_iter *iter,
222 struct class *class,
223 struct device *start,
224 const struct device_type *type);
225extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
226extern void class_dev_iter_exit(struct class_dev_iter *iter);
227
216extern int class_for_each_device(struct class *class, struct device *start, 228extern int class_for_each_device(struct class *class, struct device *start,
217 void *data, 229 void *data,
218 int (*fn)(struct device *dev, void *data)); 230 int (*fn)(struct device *dev, void *data));
@@ -396,7 +408,7 @@ struct device {
396 spinlock_t devres_lock; 408 spinlock_t devres_lock;
397 struct list_head devres_head; 409 struct list_head devres_head;
398 410
399 struct list_head node; 411 struct klist_node knode_class;
400 struct class *class; 412 struct class *class;
401 dev_t devt; /* dev_t, creates the sysfs "dev" */ 413 dev_t devt; /* dev_t, creates the sysfs "dev" */
402 struct attribute_group **groups; /* optional groups */ 414 struct attribute_group **groups; /* optional groups */