blk-ioc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 = list_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 = list_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 exitting 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. }
  66. put_io_context(ioc);
  67. }
  68. struct io_context *alloc_io_context(gfp_t gfp_flags, int node)
  69. {
  70. struct io_context *ret;
  71. ret = kmem_cache_alloc_node(iocontext_cachep, gfp_flags, node);
  72. if (ret) {
  73. atomic_long_set(&ret->refcount, 1);
  74. atomic_set(&ret->nr_tasks, 1);
  75. spin_lock_init(&ret->lock);
  76. ret->ioprio_changed = 0;
  77. ret->ioprio = 0;
  78. ret->last_waited = 0; /* doesn't matter... */
  79. ret->nr_batch_requests = 0; /* because this is 0 */
  80. INIT_RADIX_TREE(&ret->radix_root, GFP_ATOMIC | __GFP_HIGH);
  81. INIT_HLIST_HEAD(&ret->cic_list);
  82. ret->ioc_data = NULL;
  83. }
  84. return ret;
  85. }
  86. /*
  87. * If the current task has no IO context then create one and initialise it.
  88. * Otherwise, return its existing IO context.
  89. *
  90. * This returned IO context doesn't have a specifically elevated refcount,
  91. * but since the current task itself holds a reference, the context can be
  92. * used in general code, so long as it stays within `current` context.
  93. */
  94. struct io_context *current_io_context(gfp_t gfp_flags, int node)
  95. {
  96. struct task_struct *tsk = current;
  97. struct io_context *ret;
  98. ret = tsk->io_context;
  99. if (likely(ret))
  100. return ret;
  101. ret = alloc_io_context(gfp_flags, node);
  102. if (ret) {
  103. /* make sure set_task_ioprio() sees the settings above */
  104. smp_wmb();
  105. tsk->io_context = ret;
  106. }
  107. return ret;
  108. }
  109. /*
  110. * If the current task has no IO context then create one and initialise it.
  111. * If it does have a context, take a ref on it.
  112. *
  113. * This is always called in the context of the task which submitted the I/O.
  114. */
  115. struct io_context *get_io_context(gfp_t gfp_flags, int node)
  116. {
  117. struct io_context *ret = NULL;
  118. /*
  119. * Check for unlikely race with exiting task. ioc ref count is
  120. * zero when ioc is being detached.
  121. */
  122. do {
  123. ret = current_io_context(gfp_flags, node);
  124. if (unlikely(!ret))
  125. break;
  126. } while (!atomic_long_inc_not_zero(&ret->refcount));
  127. return ret;
  128. }
  129. EXPORT_SYMBOL(get_io_context);
  130. void copy_io_context(struct io_context **pdst, struct io_context **psrc)
  131. {
  132. struct io_context *src = *psrc;
  133. struct io_context *dst = *pdst;
  134. if (src) {
  135. BUG_ON(atomic_long_read(&src->refcount) == 0);
  136. atomic_long_inc(&src->refcount);
  137. put_io_context(dst);
  138. *pdst = src;
  139. }
  140. }
  141. EXPORT_SYMBOL(copy_io_context);
  142. static int __init blk_ioc_init(void)
  143. {
  144. iocontext_cachep = kmem_cache_create("blkdev_ioc",
  145. sizeof(struct io_context), 0, SLAB_PANIC, NULL);
  146. return 0;
  147. }
  148. subsys_initcall(blk_ioc_init);