blk-ioc.c 4.0 KB

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