Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Tejun Heo and committed by
Jens Axboe
5a3ceb86 a1ed5b0c

+120 -38
+1 -1
drivers/base/base.h
··· 54 54 */ 55 55 struct class_private { 56 56 struct kset class_subsys; 57 - struct list_head class_devices; 57 + struct klist class_devices; 58 58 struct list_head class_interfaces; 59 59 struct kset class_dirs; 60 60 struct mutex class_mutex;
+103 -33
drivers/base/class.c
··· 135 135 } 136 136 } 137 137 138 + static 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 + 145 + static 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 + 138 152 int __class_register(struct class *cls, struct lock_class_key *key) 139 153 { 140 154 struct class_private *cp; ··· 159 145 cp = kzalloc(sizeof(*cp), GFP_KERNEL); 160 146 if (!cp) 161 147 return -ENOMEM; 162 - INIT_LIST_HEAD(&cp->class_devices); 148 + klist_init(&cp->class_devices, klist_class_dev_get, klist_class_dev_put); 163 149 INIT_LIST_HEAD(&cp->class_interfaces); 164 150 kset_init(&cp->class_dirs); 165 151 __mutex_init(&cp->class_mutex, "struct class mutex", key); ··· 283 269 #endif 284 270 285 271 /** 272 + * class_dev_iter_init - initialize class device iterator 273 + * @iter: class iterator to initialize 274 + * @class: the class we wanna iterate over 275 + * @start: the device to start iterating from, if any 276 + * @type: device_type of the devices to iterate over, NULL for all 277 + * 278 + * Initialize class iterator @iter such that it iterates over devices 279 + * of @class. If @start is set, the list iteration will start there, 280 + * otherwise if it is NULL, the iteration starts at the beginning of 281 + * the list. 282 + */ 283 + void class_dev_iter_init(struct class_dev_iter *iter, struct class *class, 284 + struct device *start, const struct device_type *type) 285 + { 286 + struct klist_node *start_knode = NULL; 287 + 288 + if (start) 289 + start_knode = &start->knode_class; 290 + klist_iter_init_node(&class->p->class_devices, &iter->ki, start_knode); 291 + iter->type = type; 292 + } 293 + EXPORT_SYMBOL_GPL(class_dev_iter_init); 294 + 295 + /** 296 + * class_dev_iter_next - iterate to the next device 297 + * @iter: class iterator to proceed 298 + * 299 + * Proceed @iter to the next device and return it. Returns NULL if 300 + * iteration is complete. 301 + * 302 + * The returned device is referenced and won't be released till 303 + * iterator is proceed to the next device or exited. The caller is 304 + * free to do whatever it wants to do with the device including 305 + * calling back into class code. 306 + */ 307 + struct device *class_dev_iter_next(struct class_dev_iter *iter) 308 + { 309 + struct klist_node *knode; 310 + struct device *dev; 311 + 312 + while (1) { 313 + knode = klist_next(&iter->ki); 314 + if (!knode) 315 + return NULL; 316 + dev = container_of(knode, struct device, knode_class); 317 + if (!iter->type || iter->type == dev->type) 318 + return dev; 319 + } 320 + } 321 + EXPORT_SYMBOL_GPL(class_dev_iter_next); 322 + 323 + /** 324 + * class_dev_iter_exit - finish iteration 325 + * @iter: class iterator to finish 326 + * 327 + * Finish an iteration. Always call this function after iteration is 328 + * complete whether the iteration ran till the end or not. 329 + */ 330 + void class_dev_iter_exit(struct class_dev_iter *iter) 331 + { 332 + klist_iter_exit(&iter->ki); 333 + } 334 + EXPORT_SYMBOL_GPL(class_dev_iter_exit); 335 + 336 + /** 286 337 * class_for_each_device - device iterator 287 338 * @class: the class we're iterating 288 339 * @start: the device to start with in the list, if any. ··· 362 283 * We check the return of @fn each time. If it returns anything 363 284 * other than 0, we break out and return that value. 364 285 * 365 - * Note, we hold class->class_mutex in this function, so it can not be 366 - * re-acquired in @fn, otherwise it will self-deadlocking. For 367 - * example, calls to add or remove class members would be verboten. 286 + * @fn is allowed to do anything including calling back into class 287 + * code. There's no locking restriction. 368 288 */ 369 289 int class_for_each_device(struct class *class, struct device *start, 370 290 void *data, int (*fn)(struct device *, void *)) 371 291 { 292 + struct class_dev_iter iter; 372 293 struct device *dev; 373 294 int error = 0; 374 295 ··· 380 301 return -EINVAL; 381 302 } 382 303 383 - mutex_lock(&class->p->class_mutex); 384 - list_for_each_entry(dev, &class->p->class_devices, node) { 385 - if (start) { 386 - if (start == dev) 387 - start = NULL; 388 - continue; 389 - } 390 - dev = get_device(dev); 304 + class_dev_iter_init(&iter, class, start, NULL); 305 + while ((dev = class_dev_iter_next(&iter))) { 391 306 error = fn(dev, data); 392 - put_device(dev); 393 307 if (error) 394 308 break; 395 309 } 396 - mutex_unlock(&class->p->class_mutex); 310 + class_dev_iter_exit(&iter); 397 311 398 312 return error; 399 313 } ··· 409 337 * 410 338 * Note, you will need to drop the reference with put_device() after use. 411 339 * 412 - * We hold class->class_mutex in this function, so it can not be 413 - * re-acquired in @match, otherwise it will self-deadlocking. For 414 - * example, calls to add or remove class members would be verboten. 340 + * @fn is allowed to do anything including calling back into class 341 + * code. There's no locking restriction. 415 342 */ 416 343 struct device *class_find_device(struct class *class, struct device *start, 417 344 void *data, 418 345 int (*match)(struct device *, void *)) 419 346 { 347 + struct class_dev_iter iter; 420 348 struct device *dev; 421 - int found = 0; 422 349 423 350 if (!class) 424 351 return NULL; ··· 427 356 return NULL; 428 357 } 429 358 430 - mutex_lock(&class->p->class_mutex); 431 - list_for_each_entry(dev, &class->p->class_devices, node) { 432 - if (start) { 433 - if (start == dev) 434 - start = NULL; 435 - continue; 436 - } 437 - dev = get_device(dev); 359 + class_dev_iter_init(&iter, class, start, NULL); 360 + while ((dev = class_dev_iter_next(&iter))) { 438 361 if (match(dev, data)) { 439 - found = 1; 362 + get_device(dev); 440 363 break; 441 - } else 442 - put_device(dev); 364 + } 443 365 } 444 - mutex_unlock(&class->p->class_mutex); 366 + class_dev_iter_exit(&iter); 445 367 446 - return found ? dev : NULL; 368 + return dev; 447 369 } 448 370 EXPORT_SYMBOL_GPL(class_find_device); 449 371 450 372 int class_interface_register(struct class_interface *class_intf) 451 373 { 452 374 struct class *parent; 375 + struct class_dev_iter iter; 453 376 struct device *dev; 454 377 455 378 if (!class_intf || !class_intf->class) ··· 456 391 mutex_lock(&parent->p->class_mutex); 457 392 list_add_tail(&class_intf->node, &parent->p->class_interfaces); 458 393 if (class_intf->add_dev) { 459 - list_for_each_entry(dev, &parent->p->class_devices, node) 394 + class_dev_iter_init(&iter, parent, NULL, NULL); 395 + while ((dev = class_dev_iter_next(&iter))) 460 396 class_intf->add_dev(dev, class_intf); 397 + class_dev_iter_exit(&iter); 461 398 } 462 399 mutex_unlock(&parent->p->class_mutex); 463 400 ··· 469 402 void class_interface_unregister(struct class_interface *class_intf) 470 403 { 471 404 struct class *parent = class_intf->class; 405 + struct class_dev_iter iter; 472 406 struct device *dev; 473 407 474 408 if (!parent) ··· 478 410 mutex_lock(&parent->p->class_mutex); 479 411 list_del_init(&class_intf->node); 480 412 if (class_intf->remove_dev) { 481 - list_for_each_entry(dev, &parent->p->class_devices, node) 413 + class_dev_iter_init(&iter, parent, NULL, NULL); 414 + while ((dev = class_dev_iter_next(&iter))) 482 415 class_intf->remove_dev(dev, class_intf); 416 + class_dev_iter_exit(&iter); 483 417 } 484 418 mutex_unlock(&parent->p->class_mutex); 485 419
+3 -3
drivers/base/core.c
··· 536 536 klist_init(&dev->klist_children, klist_children_get, 537 537 klist_children_put); 538 538 INIT_LIST_HEAD(&dev->dma_pools); 539 - INIT_LIST_HEAD(&dev->node); 540 539 init_MUTEX(&dev->sem); 541 540 spin_lock_init(&dev->devres_lock); 542 541 INIT_LIST_HEAD(&dev->devres_head); ··· 915 916 if (dev->class) { 916 917 mutex_lock(&dev->class->p->class_mutex); 917 918 /* tie the class to the device */ 918 - list_add_tail(&dev->node, &dev->class->p->class_devices); 919 + klist_add_tail(&dev->knode_class, 920 + &dev->class->p->class_devices); 919 921 920 922 /* notify any interfaces that the device is here */ 921 923 list_for_each_entry(class_intf, ··· 1032 1032 if (class_intf->remove_dev) 1033 1033 class_intf->remove_dev(dev, class_intf); 1034 1034 /* remove the device from the class list */ 1035 - list_del_init(&dev->node); 1035 + klist_del(&dev->knode_class); 1036 1036 mutex_unlock(&dev->class->p->class_mutex); 1037 1037 } 1038 1038 device_remove_file(dev, &uevent_attr);
+13 -1
include/linux/device.h
··· 199 199 struct class_private *p; 200 200 }; 201 201 202 + struct class_dev_iter { 203 + struct klist_iter ki; 204 + const struct device_type *type; 205 + }; 206 + 202 207 extern struct kobject *sysfs_dev_block_kobj; 203 208 extern struct kobject *sysfs_dev_char_kobj; 204 209 extern int __must_check __class_register(struct class *class, ··· 217 212 static struct lock_class_key __key; \ 218 213 __class_register(class, &__key); \ 219 214 }) 215 + 216 + extern void class_dev_iter_init(struct class_dev_iter *iter, 217 + struct class *class, 218 + struct device *start, 219 + const struct device_type *type); 220 + extern struct device *class_dev_iter_next(struct class_dev_iter *iter); 221 + extern void class_dev_iter_exit(struct class_dev_iter *iter); 220 222 221 223 extern int class_for_each_device(struct class *class, struct device *start, 222 224 void *data, ··· 408 396 spinlock_t devres_lock; 409 397 struct list_head devres_head; 410 398 411 - struct list_head node; 399 + struct klist_node knode_class; 412 400 struct class *class; 413 401 dev_t devt; /* dev_t, creates the sysfs "dev" */ 414 402 struct attribute_group **groups; /* optional groups */