blk-ioc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_long_read(&ioc->refcount) == 0);
  33. if (atomic_long_dec_and_test(&ioc->refcount)) {
  34. rcu_read_lock();
  35. cfq_dtor(ioc);
  36. rcu_read_unlock();
  37. kmem_cache_free(iocontext_cachep, ioc);
  38. return 1;
  39. }
  40. return 0;
  41. }
  42. EXPORT_SYMBOL(put_io_context);
  43. static void cfq_exit(struct io_context *ioc)
  44. {
  45. rcu_read_lock();
  46. if (!hlist_empty(&ioc->cic_list)) {
  47. struct cfq_io_context *cic;
  48. cic = list_entry(ioc->cic_list.first, struct cfq_io_context,
  49. cic_list);
  50. cic->exit(ioc);
  51. }
  52. rcu_read_unlock();
  53. }
  54. /* Called by the exitting task */
  55. void exit_io_context(struct task_struct *task)
  56. {
  57. struct io_context *ioc;
  58. task_lock(task);
  59. ioc = task->io_context;
  60. task->io_context = NULL;
  61. task_unlock(task);
  62. if (atomic_dec_and_test(&ioc->nr_tasks)) {
  63. cfq_exit(ioc);
  64. }
  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. }
  83. return ret;
  84. }
  85. /*
  86. * If the current task has no IO context then create one and initialise it.
  87. * Otherwise, return its existing IO context.
  88. *
  89. * This returned IO context doesn't have a specifically elevated refcount,
  90. * but since the current task itself holds a reference, the context can be
  91. * used in general code, so long as it stays within `current` context.
  92. */
  93. struct io_context *current_io_context(gfp_t gfp_flags, int node)
  94. {
  95. struct task_struct *tsk = current;
  96. struct io_context *ret;
  97. ret = tsk->io_context;
  98. if (likely(ret))
  99. return ret;
  100. ret = alloc_io_context(gfp_flags, node);
  101. if (ret) {
  102. /* make sure set_task_ioprio() sees the settings above */
  103. smp_wmb();
  104. tsk->io_context = ret;
  105. }
  106. return ret;
  107. }
  108. /*
  109. * If the current task has no IO context then create one and initialise it.
  110. * If it does have a context, take a ref on it.
  111. *
  112. * This is always called in the context of the task which submitted the I/O.
  113. */
  114. struct io_context *get_io_context(gfp_t gfp_flags, int node)
  115. {
  116. struct io_context *ret = NULL;
  117. /*
  118. * Check for unlikely race with exiting task. ioc ref count is
  119. * zero when ioc is being detached.
  120. */
  121. do {
  122. ret = current_io_context(gfp_flags, node);
  123. if (unlikely(!ret))
  124. break;
  125. } while (!atomic_long_inc_not_zero(&ret->refcount));
  126. return ret;
  127. }
  128. EXPORT_SYMBOL(get_io_context);
  129. void copy_io_context(struct io_context **pdst, struct io_context **psrc)
  130. {
  131. struct io_context *src = *psrc;
  132. struct io_context *dst = *pdst;
  133. if (src) {
  134. BUG_ON(atomic_long_read(&src->refcount) == 0);
  135. atomic_long_inc(&src->refcount);
  136. put_io_context(dst);
  137. *pdst = src;
  138. }
  139. }
  140. EXPORT_SYMBOL(copy_io_context);
  141. static int __init blk_ioc_init(void)
  142. {
  143. iocontext_cachep = kmem_cache_create("blkdev_ioc",
  144. sizeof(struct io_context), 0, SLAB_PANIC, NULL);
  145. return 0;
  146. }
  147. subsys_initcall(blk_ioc_init);