blk-ioc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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/slab.h>
  10. #include "blk.h"
  11. /*
  12. * For io context allocations
  13. */
  14. static struct kmem_cache *iocontext_cachep;
  15. /**
  16. * get_io_context - increment reference count to io_context
  17. * @ioc: io_context to get
  18. *
  19. * Increment reference count to @ioc.
  20. */
  21. void get_io_context(struct io_context *ioc)
  22. {
  23. BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
  24. atomic_long_inc(&ioc->refcount);
  25. }
  26. EXPORT_SYMBOL(get_io_context);
  27. static void icq_free_icq_rcu(struct rcu_head *head)
  28. {
  29. struct io_cq *icq = container_of(head, struct io_cq, __rcu_head);
  30. kmem_cache_free(icq->__rcu_icq_cache, icq);
  31. }
  32. /* Exit an icq. Called with both ioc and q locked. */
  33. static void ioc_exit_icq(struct io_cq *icq)
  34. {
  35. struct elevator_type *et = icq->q->elevator->type;
  36. if (icq->flags & ICQ_EXITED)
  37. return;
  38. if (et->ops.elevator_exit_icq_fn)
  39. et->ops.elevator_exit_icq_fn(icq);
  40. icq->flags |= ICQ_EXITED;
  41. }
  42. /* Release an icq. Called with both ioc and q locked. */
  43. static void ioc_destroy_icq(struct io_cq *icq)
  44. {
  45. struct io_context *ioc = icq->ioc;
  46. struct request_queue *q = icq->q;
  47. struct elevator_type *et = q->elevator->type;
  48. lockdep_assert_held(&ioc->lock);
  49. lockdep_assert_held(q->queue_lock);
  50. radix_tree_delete(&ioc->icq_tree, icq->q->id);
  51. hlist_del_init(&icq->ioc_node);
  52. list_del_init(&icq->q_node);
  53. /*
  54. * Both setting lookup hint to and clearing it from @icq are done
  55. * under queue_lock. If it's not pointing to @icq now, it never
  56. * will. Hint assignment itself can race safely.
  57. */
  58. if (rcu_access_pointer(ioc->icq_hint) == icq)
  59. rcu_assign_pointer(ioc->icq_hint, NULL);
  60. ioc_exit_icq(icq);
  61. /*
  62. * @icq->q might have gone away by the time RCU callback runs
  63. * making it impossible to determine icq_cache. Record it in @icq.
  64. */
  65. icq->__rcu_icq_cache = et->icq_cache;
  66. call_rcu(&icq->__rcu_head, icq_free_icq_rcu);
  67. }
  68. /*
  69. * Slow path for ioc release in put_io_context(). Performs double-lock
  70. * dancing to unlink all icq's and then frees ioc.
  71. */
  72. static void ioc_release_fn(struct work_struct *work)
  73. {
  74. struct io_context *ioc = container_of(work, struct io_context,
  75. release_work);
  76. unsigned long flags;
  77. /*
  78. * Exiting icq may call into put_io_context() through elevator
  79. * which will trigger lockdep warning. The ioc's are guaranteed to
  80. * be different, use a different locking subclass here. Use
  81. * irqsave variant as there's no spin_lock_irq_nested().
  82. */
  83. spin_lock_irqsave_nested(&ioc->lock, flags, 1);
  84. while (!hlist_empty(&ioc->icq_list)) {
  85. struct io_cq *icq = hlist_entry(ioc->icq_list.first,
  86. struct io_cq, ioc_node);
  87. struct request_queue *q = icq->q;
  88. if (spin_trylock(q->queue_lock)) {
  89. ioc_destroy_icq(icq);
  90. spin_unlock(q->queue_lock);
  91. } else {
  92. spin_unlock_irqrestore(&ioc->lock, flags);
  93. cpu_relax();
  94. spin_lock_irqsave_nested(&ioc->lock, flags, 1);
  95. }
  96. }
  97. spin_unlock_irqrestore(&ioc->lock, flags);
  98. kmem_cache_free(iocontext_cachep, ioc);
  99. }
  100. /**
  101. * put_io_context - put a reference of io_context
  102. * @ioc: io_context to put
  103. *
  104. * Decrement reference count of @ioc and release it if the count reaches
  105. * zero.
  106. */
  107. void put_io_context(struct io_context *ioc)
  108. {
  109. unsigned long flags;
  110. bool free_ioc = false;
  111. if (ioc == NULL)
  112. return;
  113. BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
  114. /*
  115. * Releasing ioc requires reverse order double locking and we may
  116. * already be holding a queue_lock. Do it asynchronously from wq.
  117. */
  118. if (atomic_long_dec_and_test(&ioc->refcount)) {
  119. spin_lock_irqsave(&ioc->lock, flags);
  120. if (!hlist_empty(&ioc->icq_list))
  121. queue_work(system_power_efficient_wq,
  122. &ioc->release_work);
  123. else
  124. free_ioc = true;
  125. spin_unlock_irqrestore(&ioc->lock, flags);
  126. }
  127. if (free_ioc)
  128. kmem_cache_free(iocontext_cachep, ioc);
  129. }
  130. EXPORT_SYMBOL(put_io_context);
  131. /**
  132. * put_io_context_active - put active reference on ioc
  133. * @ioc: ioc of interest
  134. *
  135. * Undo get_io_context_active(). If active reference reaches zero after
  136. * put, @ioc can never issue further IOs and ioscheds are notified.
  137. */
  138. void put_io_context_active(struct io_context *ioc)
  139. {
  140. unsigned long flags;
  141. struct io_cq *icq;
  142. if (!atomic_dec_and_test(&ioc->active_ref)) {
  143. put_io_context(ioc);
  144. return;
  145. }
  146. /*
  147. * Need ioc lock to walk icq_list and q lock to exit icq. Perform
  148. * reverse double locking. Read comment in ioc_release_fn() for
  149. * explanation on the nested locking annotation.
  150. */
  151. retry:
  152. spin_lock_irqsave_nested(&ioc->lock, flags, 1);
  153. hlist_for_each_entry(icq, &ioc->icq_list, ioc_node) {
  154. if (icq->flags & ICQ_EXITED)
  155. continue;
  156. if (spin_trylock(icq->q->queue_lock)) {
  157. ioc_exit_icq(icq);
  158. spin_unlock(icq->q->queue_lock);
  159. } else {
  160. spin_unlock_irqrestore(&ioc->lock, flags);
  161. cpu_relax();
  162. goto retry;
  163. }
  164. }
  165. spin_unlock_irqrestore(&ioc->lock, flags);
  166. put_io_context(ioc);
  167. }
  168. /* Called by the exiting task */
  169. void exit_io_context(struct task_struct *task)
  170. {
  171. struct io_context *ioc;
  172. task_lock(task);
  173. ioc = task->io_context;
  174. task->io_context = NULL;
  175. task_unlock(task);
  176. atomic_dec(&ioc->nr_tasks);
  177. put_io_context_active(ioc);
  178. }
  179. /**
  180. * ioc_clear_queue - break any ioc association with the specified queue
  181. * @q: request_queue being cleared
  182. *
  183. * Walk @q->icq_list and exit all io_cq's. Must be called with @q locked.
  184. */
  185. void ioc_clear_queue(struct request_queue *q)
  186. {
  187. lockdep_assert_held(q->queue_lock);
  188. while (!list_empty(&q->icq_list)) {
  189. struct io_cq *icq = list_entry(q->icq_list.next,
  190. struct io_cq, q_node);
  191. struct io_context *ioc = icq->ioc;
  192. spin_lock(&ioc->lock);
  193. ioc_destroy_icq(icq);
  194. spin_unlock(&ioc->lock);
  195. }
  196. }
  197. int create_task_io_context(struct task_struct *task, gfp_t gfp_flags, int node)
  198. {
  199. struct io_context *ioc;
  200. int ret;
  201. ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
  202. node);
  203. if (unlikely(!ioc))
  204. return -ENOMEM;
  205. /* initialize */
  206. atomic_long_set(&ioc->refcount, 1);
  207. atomic_set(&ioc->nr_tasks, 1);
  208. atomic_set(&ioc->active_ref, 1);
  209. spin_lock_init(&ioc->lock);
  210. INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC | __GFP_HIGH);
  211. INIT_HLIST_HEAD(&ioc->icq_list);
  212. INIT_WORK(&ioc->release_work, ioc_release_fn);
  213. /*
  214. * Try to install. ioc shouldn't be installed if someone else
  215. * already did or @task, which isn't %current, is exiting. Note
  216. * that we need to allow ioc creation on exiting %current as exit
  217. * path may issue IOs from e.g. exit_files(). The exit path is
  218. * responsible for not issuing IO after exit_io_context().
  219. */
  220. task_lock(task);
  221. if (!task->io_context &&
  222. (task == current || !(task->flags & PF_EXITING)))
  223. task->io_context = ioc;
  224. else
  225. kmem_cache_free(iocontext_cachep, ioc);
  226. ret = task->io_context ? 0 : -EBUSY;
  227. task_unlock(task);
  228. return ret;
  229. }
  230. /**
  231. * get_task_io_context - get io_context of a task
  232. * @task: task of interest
  233. * @gfp_flags: allocation flags, used if allocation is necessary
  234. * @node: allocation node, used if allocation is necessary
  235. *
  236. * Return io_context of @task. If it doesn't exist, it is created with
  237. * @gfp_flags and @node. The returned io_context has its reference count
  238. * incremented.
  239. *
  240. * This function always goes through task_lock() and it's better to use
  241. * %current->io_context + get_io_context() for %current.
  242. */
  243. struct io_context *get_task_io_context(struct task_struct *task,
  244. gfp_t gfp_flags, int node)
  245. {
  246. struct io_context *ioc;
  247. might_sleep_if(gfpflags_allow_blocking(gfp_flags));
  248. do {
  249. task_lock(task);
  250. ioc = task->io_context;
  251. if (likely(ioc)) {
  252. get_io_context(ioc);
  253. task_unlock(task);
  254. return ioc;
  255. }
  256. task_unlock(task);
  257. } while (!create_task_io_context(task, gfp_flags, node));
  258. return NULL;
  259. }
  260. EXPORT_SYMBOL(get_task_io_context);
  261. /**
  262. * ioc_lookup_icq - lookup io_cq from ioc
  263. * @ioc: the associated io_context
  264. * @q: the associated request_queue
  265. *
  266. * Look up io_cq associated with @ioc - @q pair from @ioc. Must be called
  267. * with @q->queue_lock held.
  268. */
  269. struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q)
  270. {
  271. struct io_cq *icq;
  272. lockdep_assert_held(q->queue_lock);
  273. /*
  274. * icq's are indexed from @ioc using radix tree and hint pointer,
  275. * both of which are protected with RCU. All removals are done
  276. * holding both q and ioc locks, and we're holding q lock - if we
  277. * find a icq which points to us, it's guaranteed to be valid.
  278. */
  279. rcu_read_lock();
  280. icq = rcu_dereference(ioc->icq_hint);
  281. if (icq && icq->q == q)
  282. goto out;
  283. icq = radix_tree_lookup(&ioc->icq_tree, q->id);
  284. if (icq && icq->q == q)
  285. rcu_assign_pointer(ioc->icq_hint, icq); /* allowed to race */
  286. else
  287. icq = NULL;
  288. out:
  289. rcu_read_unlock();
  290. return icq;
  291. }
  292. EXPORT_SYMBOL(ioc_lookup_icq);
  293. /**
  294. * ioc_create_icq - create and link io_cq
  295. * @ioc: io_context of interest
  296. * @q: request_queue of interest
  297. * @gfp_mask: allocation mask
  298. *
  299. * Make sure io_cq linking @ioc and @q exists. If icq doesn't exist, they
  300. * will be created using @gfp_mask.
  301. *
  302. * The caller is responsible for ensuring @ioc won't go away and @q is
  303. * alive and will stay alive until this function returns.
  304. */
  305. struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,
  306. gfp_t gfp_mask)
  307. {
  308. struct elevator_type *et = q->elevator->type;
  309. struct io_cq *icq;
  310. /* allocate stuff */
  311. icq = kmem_cache_alloc_node(et->icq_cache, gfp_mask | __GFP_ZERO,
  312. q->node);
  313. if (!icq)
  314. return NULL;
  315. if (radix_tree_maybe_preload(gfp_mask) < 0) {
  316. kmem_cache_free(et->icq_cache, icq);
  317. return NULL;
  318. }
  319. icq->ioc = ioc;
  320. icq->q = q;
  321. INIT_LIST_HEAD(&icq->q_node);
  322. INIT_HLIST_NODE(&icq->ioc_node);
  323. /* lock both q and ioc and try to link @icq */
  324. spin_lock_irq(q->queue_lock);
  325. spin_lock(&ioc->lock);
  326. if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) {
  327. hlist_add_head(&icq->ioc_node, &ioc->icq_list);
  328. list_add(&icq->q_node, &q->icq_list);
  329. if (et->ops.elevator_init_icq_fn)
  330. et->ops.elevator_init_icq_fn(icq);
  331. } else {
  332. kmem_cache_free(et->icq_cache, icq);
  333. icq = ioc_lookup_icq(ioc, q);
  334. if (!icq)
  335. printk(KERN_ERR "cfq: icq link failed!\n");
  336. }
  337. spin_unlock(&ioc->lock);
  338. spin_unlock_irq(q->queue_lock);
  339. radix_tree_preload_end();
  340. return icq;
  341. }
  342. static int __init blk_ioc_init(void)
  343. {
  344. iocontext_cachep = kmem_cache_create("blkdev_ioc",
  345. sizeof(struct io_context), 0, SLAB_PANIC, NULL);
  346. return 0;
  347. }
  348. subsys_initcall(blk_ioc_init);