blk-ioc.c 4.1 KB

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