blk-ioc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Functions related to io context handling
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/bio.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
  10. #include <linux/slab.h>
  11. #include "blk.h"
  12. /*
  13. * For io context allocations
  14. */
  15. static struct kmem_cache *iocontext_cachep;
  16. static void cfq_dtor(struct io_context *ioc)
  17. {
  18. if (!hlist_empty(&ioc->cic_list)) {
  19. struct cfq_io_context *cic;
  20. cic = hlist_entry(ioc->cic_list.first, struct cfq_io_context,
  21. cic_list);
  22. cic->dtor(ioc);
  23. }
  24. }
  25. /*
  26. * IO Context helper functions. put_io_context() returns 1 if there are no
  27. * more users of this io context, 0 otherwise.
  28. */
  29. int put_io_context(struct io_context *ioc)
  30. {
  31. if (ioc == NULL)
  32. return 1;
  33. BUG_ON(atomic_long_read(&ioc->refcount) == 0);
  34. if (atomic_long_dec_and_test(&ioc->refcount)) {
  35. rcu_read_lock();
  36. cfq_dtor(ioc);
  37. rcu_read_unlock();
  38. kmem_cache_free(iocontext_cachep, ioc);
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. EXPORT_SYMBOL(put_io_context);
  44. static void cfq_exit(struct io_context *ioc)
  45. {
  46. rcu_read_lock();
  47. if (!hlist_empty(&ioc->cic_list)) {
  48. struct cfq_io_context *cic;
  49. cic = hlist_entry(ioc->cic_list.first, struct cfq_io_context,
  50. cic_list);
  51. cic->exit(ioc);
  52. }
  53. rcu_read_unlock();
  54. }
  55. /* Called by the exiting task */
  56. void exit_io_context(struct task_struct *task)
  57. {
  58. struct io_context *ioc;
  59. task_lock(task);
  60. ioc = task->io_context;
  61. task->io_context = NULL;
  62. task_unlock(task);
  63. if (atomic_dec_and_test(&ioc->nr_tasks))
  64. cfq_exit(ioc);
  65. put_io_context(ioc);
  66. }
  67. struct io_context *alloc_io_context(gfp_t gfp_flags, int node)
  68. {
  69. struct io_context *ret;
  70. ret = kmem_cache_alloc_node(iocontext_cachep, gfp_flags, node);
  71. if (ret) {
  72. atomic_long_set(&ret->refcount, 1);
  73. atomic_set(&ret->nr_tasks, 1);
  74. spin_lock_init(&ret->lock);
  75. ret->ioprio_changed = 0;
  76. ret->ioprio = 0;
  77. ret->last_waited = 0; /* doesn't matter... */
  78. ret->nr_batch_requests = 0; /* because this is 0 */
  79. INIT_RADIX_TREE(&ret->radix_root, GFP_ATOMIC | __GFP_HIGH);
  80. INIT_HLIST_HEAD(&ret->cic_list);
  81. ret->ioc_data = NULL;
  82. #if defined(CONFIG_BLK_CGROUP) || defined(CONFIG_BLK_CGROUP_MODULE)
  83. ret->cgroup_changed = 0;
  84. #endif
  85. }
  86. return ret;
  87. }
  88. /*
  89. * If the current task has no IO context then create one and initialise it.
  90. * Otherwise, return its existing IO context.
  91. *
  92. * This returned IO context doesn't have a specifically elevated refcount,
  93. * but since the current task itself holds a reference, the context can be
  94. * used in general code, so long as it stays within `current` context.
  95. */
  96. struct io_context *current_io_context(gfp_t gfp_flags, int node)
  97. {
  98. struct task_struct *tsk = current;
  99. struct io_context *ret;
  100. ret = tsk->io_context;
  101. if (likely(ret))
  102. return ret;
  103. ret = alloc_io_context(gfp_flags, node);
  104. if (ret) {
  105. /* make sure set_task_ioprio() sees the settings above */
  106. smp_wmb();
  107. tsk->io_context = ret;
  108. }
  109. return ret;
  110. }
  111. /*
  112. * If the current task has no IO context then create one and initialise it.
  113. * If it does have a context, take a ref on it.
  114. *
  115. * This is always called in the context of the task which submitted the I/O.
  116. */
  117. struct io_context *get_io_context(gfp_t gfp_flags, int node)
  118. {
  119. struct io_context *ret = NULL;
  120. /*
  121. * Check for unlikely race with exiting task. ioc ref count is
  122. * zero when ioc is being detached.
  123. */
  124. do {
  125. ret = current_io_context(gfp_flags, node);
  126. if (unlikely(!ret))
  127. break;
  128. } while (!atomic_long_inc_not_zero(&ret->refcount));
  129. return ret;
  130. }
  131. EXPORT_SYMBOL(get_io_context);
  132. static int __init blk_ioc_init(void)
  133. {
  134. iocontext_cachep = kmem_cache_create("blkdev_ioc",
  135. sizeof(struct io_context), 0, SLAB_PANIC, NULL);
  136. return 0;
  137. }
  138. subsys_initcall(blk_ioc_init);