blk-mq-cpu.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * CPU notifier helper code for blk-mq
  3. *
  4. * Copyright (C) 2013-2014 Jens Axboe
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/list.h>
  11. #include <linux/llist.h>
  12. #include <linux/smp.h>
  13. #include <linux/cpu.h>
  14. #include <linux/blk-mq.h>
  15. #include "blk-mq.h"
  16. static LIST_HEAD(blk_mq_cpu_notify_list);
  17. static DEFINE_RAW_SPINLOCK(blk_mq_cpu_notify_lock);
  18. static int blk_mq_main_cpu_notify(struct notifier_block *self,
  19. unsigned long action, void *hcpu)
  20. {
  21. unsigned int cpu = (unsigned long) hcpu;
  22. struct blk_mq_cpu_notifier *notify;
  23. int ret = NOTIFY_OK;
  24. raw_spin_lock(&blk_mq_cpu_notify_lock);
  25. list_for_each_entry(notify, &blk_mq_cpu_notify_list, list) {
  26. ret = notify->notify(notify->data, action, cpu);
  27. if (ret != NOTIFY_OK)
  28. break;
  29. }
  30. raw_spin_unlock(&blk_mq_cpu_notify_lock);
  31. return ret;
  32. }
  33. void blk_mq_register_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
  34. {
  35. BUG_ON(!notifier->notify);
  36. raw_spin_lock(&blk_mq_cpu_notify_lock);
  37. list_add_tail(&notifier->list, &blk_mq_cpu_notify_list);
  38. raw_spin_unlock(&blk_mq_cpu_notify_lock);
  39. }
  40. void blk_mq_unregister_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
  41. {
  42. raw_spin_lock(&blk_mq_cpu_notify_lock);
  43. list_del(&notifier->list);
  44. raw_spin_unlock(&blk_mq_cpu_notify_lock);
  45. }
  46. void blk_mq_init_cpu_notifier(struct blk_mq_cpu_notifier *notifier,
  47. int (*fn)(void *, unsigned long, unsigned int),
  48. void *data)
  49. {
  50. notifier->notify = fn;
  51. notifier->data = data;
  52. }
  53. void __init blk_mq_cpu_init(void)
  54. {
  55. hotcpu_notifier(blk_mq_main_cpu_notify, 0);
  56. }