blk-ioc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. /**
  17. * get_io_context - increment reference count to io_context
  18. * @ioc: io_context to get
  19. *
  20. * Increment reference count to @ioc.
  21. */
  22. void get_io_context(struct io_context *ioc)
  23. {
  24. BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
  25. atomic_long_inc(&ioc->refcount);
  26. }
  27. EXPORT_SYMBOL(get_io_context);
  28. /*
  29. * Releasing ioc may nest into another put_io_context() leading to nested
  30. * fast path release. As the ioc's can't be the same, this is okay but
  31. * makes lockdep whine. Keep track of nesting and use it as subclass.
  32. */
  33. #ifdef CONFIG_LOCKDEP
  34. #define ioc_release_depth(q) ((q) ? (q)->ioc_release_depth : 0)
  35. #define ioc_release_depth_inc(q) (q)->ioc_release_depth++
  36. #define ioc_release_depth_dec(q) (q)->ioc_release_depth--
  37. #else
  38. #define ioc_release_depth(q) 0
  39. #define ioc_release_depth_inc(q) do { } while (0)
  40. #define ioc_release_depth_dec(q) do { } while (0)
  41. #endif
  42. static void icq_free_icq_rcu(struct rcu_head *head)
  43. {
  44. struct io_cq *icq = container_of(head, struct io_cq, __rcu_head);
  45. kmem_cache_free(icq->__rcu_icq_cache, icq);
  46. }
  47. /*
  48. * Exit and free an icq. Called with both ioc and q locked.
  49. */
  50. static void ioc_exit_icq(struct io_cq *icq)
  51. {
  52. struct io_context *ioc = icq->ioc;
  53. struct request_queue *q = icq->q;
  54. struct elevator_type *et = q->elevator->type;
  55. lockdep_assert_held(&ioc->lock);
  56. lockdep_assert_held(q->queue_lock);
  57. radix_tree_delete(&ioc->icq_tree, icq->q->id);
  58. hlist_del_init(&icq->ioc_node);
  59. list_del_init(&icq->q_node);
  60. /*
  61. * Both setting lookup hint to and clearing it from @icq are done
  62. * under queue_lock. If it's not pointing to @icq now, it never
  63. * will. Hint assignment itself can race safely.
  64. */
  65. if (rcu_dereference_raw(ioc->icq_hint) == icq)
  66. rcu_assign_pointer(ioc->icq_hint, NULL);
  67. if (et->ops.elevator_exit_icq_fn) {
  68. ioc_release_depth_inc(q);
  69. et->ops.elevator_exit_icq_fn(icq);
  70. ioc_release_depth_dec(q);
  71. }
  72. /*
  73. * @icq->q might have gone away by the time RCU callback runs
  74. * making it impossible to determine icq_cache. Record it in @icq.
  75. */
  76. icq->__rcu_icq_cache = et->icq_cache;
  77. call_rcu(&icq->__rcu_head, icq_free_icq_rcu);
  78. }
  79. /*
  80. * Slow path for ioc release in put_io_context(). Performs double-lock
  81. * dancing to unlink all icq's and then frees ioc.
  82. */
  83. static void ioc_release_fn(struct work_struct *work)
  84. {
  85. struct io_context *ioc = container_of(work, struct io_context,
  86. release_work);
  87. struct request_queue *last_q = NULL;
  88. spin_lock_irq(&ioc->lock);
  89. while (!hlist_empty(&ioc->icq_list)) {
  90. struct io_cq *icq = hlist_entry(ioc->icq_list.first,
  91. struct io_cq, ioc_node);
  92. struct request_queue *this_q = icq->q;
  93. if (this_q != last_q) {
  94. /*
  95. * Need to switch to @this_q. Once we release
  96. * @ioc->lock, it can go away along with @cic.
  97. * Hold on to it.
  98. */
  99. __blk_get_queue(this_q);
  100. /*
  101. * blk_put_queue() might sleep thanks to kobject
  102. * idiocy. Always release both locks, put and
  103. * restart.
  104. */
  105. if (last_q) {
  106. spin_unlock(last_q->queue_lock);
  107. spin_unlock_irq(&ioc->lock);
  108. blk_put_queue(last_q);
  109. } else {
  110. spin_unlock_irq(&ioc->lock);
  111. }
  112. last_q = this_q;
  113. spin_lock_irq(this_q->queue_lock);
  114. spin_lock(&ioc->lock);
  115. continue;
  116. }
  117. ioc_exit_icq(icq);
  118. }
  119. if (last_q) {
  120. spin_unlock(last_q->queue_lock);
  121. spin_unlock_irq(&ioc->lock);
  122. blk_put_queue(last_q);
  123. } else {
  124. spin_unlock_irq(&ioc->lock);
  125. }
  126. kmem_cache_free(iocontext_cachep, ioc);
  127. }
  128. /**
  129. * put_io_context - put a reference of io_context
  130. * @ioc: io_context to put
  131. * @locked_q: request_queue the caller is holding queue_lock of (hint)
  132. *
  133. * Decrement reference count of @ioc and release it if the count reaches
  134. * zero. If the caller is holding queue_lock of a queue, it can indicate
  135. * that with @locked_q. This is an optimization hint and the caller is
  136. * allowed to pass in %NULL even when it's holding a queue_lock.
  137. */
  138. void put_io_context(struct io_context *ioc, struct request_queue *locked_q)
  139. {
  140. struct request_queue *last_q = locked_q;
  141. unsigned long flags;
  142. if (ioc == NULL)
  143. return;
  144. BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
  145. if (locked_q)
  146. lockdep_assert_held(locked_q->queue_lock);
  147. if (!atomic_long_dec_and_test(&ioc->refcount))
  148. return;
  149. /*
  150. * Destroy @ioc. This is a bit messy because icq's are chained
  151. * from both ioc and queue, and ioc->lock nests inside queue_lock.
  152. * The inner ioc->lock should be held to walk our icq_list and then
  153. * for each icq the outer matching queue_lock should be grabbed.
  154. * ie. We need to do reverse-order double lock dancing.
  155. *
  156. * Another twist is that we are often called with one of the
  157. * matching queue_locks held as indicated by @locked_q, which
  158. * prevents performing double-lock dance for other queues.
  159. *
  160. * So, we do it in two stages. The fast path uses the queue_lock
  161. * the caller is holding and, if other queues need to be accessed,
  162. * uses trylock to avoid introducing locking dependency. This can
  163. * handle most cases, especially if @ioc was performing IO on only
  164. * single device.
  165. *
  166. * If trylock doesn't cut it, we defer to @ioc->release_work which
  167. * can do all the double-locking dancing.
  168. */
  169. spin_lock_irqsave_nested(&ioc->lock, flags,
  170. ioc_release_depth(locked_q));
  171. while (!hlist_empty(&ioc->icq_list)) {
  172. struct io_cq *icq = hlist_entry(ioc->icq_list.first,
  173. struct io_cq, ioc_node);
  174. struct request_queue *this_q = icq->q;
  175. if (this_q != last_q) {
  176. if (last_q && last_q != locked_q)
  177. spin_unlock(last_q->queue_lock);
  178. last_q = NULL;
  179. if (!spin_trylock(this_q->queue_lock))
  180. break;
  181. last_q = this_q;
  182. continue;
  183. }
  184. ioc_exit_icq(icq);
  185. }
  186. if (last_q && last_q != locked_q)
  187. spin_unlock(last_q->queue_lock);
  188. spin_unlock_irqrestore(&ioc->lock, flags);
  189. /* if no icq is left, we're done; otherwise, kick release_work */
  190. if (hlist_empty(&ioc->icq_list))
  191. kmem_cache_free(iocontext_cachep, ioc);
  192. else
  193. schedule_work(&ioc->release_work);
  194. }
  195. EXPORT_SYMBOL(put_io_context);
  196. /* Called by the exiting task */
  197. void exit_io_context(struct task_struct *task)
  198. {
  199. struct io_context *ioc;
  200. task_lock(task);
  201. ioc = task->io_context;
  202. task->io_context = NULL;
  203. task_unlock(task);
  204. atomic_dec(&ioc->nr_tasks);
  205. put_io_context(ioc, NULL);
  206. }
  207. /**
  208. * ioc_clear_queue - break any ioc association with the specified queue
  209. * @q: request_queue being cleared
  210. *
  211. * Walk @q->icq_list and exit all io_cq's. Must be called with @q locked.
  212. */
  213. void ioc_clear_queue(struct request_queue *q)
  214. {
  215. lockdep_assert_held(q->queue_lock);
  216. while (!list_empty(&q->icq_list)) {
  217. struct io_cq *icq = list_entry(q->icq_list.next,
  218. struct io_cq, q_node);
  219. struct io_context *ioc = icq->ioc;
  220. spin_lock(&ioc->lock);
  221. ioc_exit_icq(icq);
  222. spin_unlock(&ioc->lock);
  223. }
  224. }
  225. void create_io_context_slowpath(struct task_struct *task, gfp_t gfp_flags,
  226. int node)
  227. {
  228. struct io_context *ioc;
  229. ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
  230. node);
  231. if (unlikely(!ioc))
  232. return;
  233. /* initialize */
  234. atomic_long_set(&ioc->refcount, 1);
  235. atomic_set(&ioc->nr_tasks, 1);
  236. spin_lock_init(&ioc->lock);
  237. INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC | __GFP_HIGH);
  238. INIT_HLIST_HEAD(&ioc->icq_list);
  239. INIT_WORK(&ioc->release_work, ioc_release_fn);
  240. /*
  241. * Try to install. ioc shouldn't be installed if someone else
  242. * already did or @task, which isn't %current, is exiting. Note
  243. * that we need to allow ioc creation on exiting %current as exit
  244. * path may issue IOs from e.g. exit_files(). The exit path is
  245. * responsible for not issuing IO after exit_io_context().
  246. */
  247. task_lock(task);
  248. if (!task->io_context &&
  249. (task == current || !(task->flags & PF_EXITING)))
  250. task->io_context = ioc;
  251. else
  252. kmem_cache_free(iocontext_cachep, ioc);
  253. task_unlock(task);
  254. }
  255. /**
  256. * get_task_io_context - get io_context of a task
  257. * @task: task of interest
  258. * @gfp_flags: allocation flags, used if allocation is necessary
  259. * @node: allocation node, used if allocation is necessary
  260. *
  261. * Return io_context of @task. If it doesn't exist, it is created with
  262. * @gfp_flags and @node. The returned io_context has its reference count
  263. * incremented.
  264. *
  265. * This function always goes through task_lock() and it's better to use
  266. * %current->io_context + get_io_context() for %current.
  267. */
  268. struct io_context *get_task_io_context(struct task_struct *task,
  269. gfp_t gfp_flags, int node)
  270. {
  271. struct io_context *ioc;
  272. might_sleep_if(gfp_flags & __GFP_WAIT);
  273. do {
  274. task_lock(task);
  275. ioc = task->io_context;
  276. if (likely(ioc)) {
  277. get_io_context(ioc);
  278. task_unlock(task);
  279. return ioc;
  280. }
  281. task_unlock(task);
  282. } while (create_io_context(task, gfp_flags, node));
  283. return NULL;
  284. }
  285. EXPORT_SYMBOL(get_task_io_context);
  286. /**
  287. * ioc_lookup_icq - lookup io_cq from ioc
  288. * @ioc: the associated io_context
  289. * @q: the associated request_queue
  290. *
  291. * Look up io_cq associated with @ioc - @q pair from @ioc. Must be called
  292. * with @q->queue_lock held.
  293. */
  294. struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q)
  295. {
  296. struct io_cq *icq;
  297. lockdep_assert_held(q->queue_lock);
  298. /*
  299. * icq's are indexed from @ioc using radix tree and hint pointer,
  300. * both of which are protected with RCU. All removals are done
  301. * holding both q and ioc locks, and we're holding q lock - if we
  302. * find a icq which points to us, it's guaranteed to be valid.
  303. */
  304. rcu_read_lock();
  305. icq = rcu_dereference(ioc->icq_hint);
  306. if (icq && icq->q == q)
  307. goto out;
  308. icq = radix_tree_lookup(&ioc->icq_tree, q->id);
  309. if (icq && icq->q == q)
  310. rcu_assign_pointer(ioc->icq_hint, icq); /* allowed to race */
  311. else
  312. icq = NULL;
  313. out:
  314. rcu_read_unlock();
  315. return icq;
  316. }
  317. EXPORT_SYMBOL(ioc_lookup_icq);
  318. /**
  319. * ioc_create_icq - create and link io_cq
  320. * @q: request_queue of interest
  321. * @gfp_mask: allocation mask
  322. *
  323. * Make sure io_cq linking %current->io_context and @q exists. If either
  324. * io_context and/or icq don't exist, they will be created using @gfp_mask.
  325. *
  326. * The caller is responsible for ensuring @ioc won't go away and @q is
  327. * alive and will stay alive until this function returns.
  328. */
  329. struct io_cq *ioc_create_icq(struct request_queue *q, gfp_t gfp_mask)
  330. {
  331. struct elevator_type *et = q->elevator->type;
  332. struct io_context *ioc;
  333. struct io_cq *icq;
  334. /* allocate stuff */
  335. ioc = create_io_context(current, gfp_mask, q->node);
  336. if (!ioc)
  337. return NULL;
  338. icq = kmem_cache_alloc_node(et->icq_cache, gfp_mask | __GFP_ZERO,
  339. q->node);
  340. if (!icq)
  341. return NULL;
  342. if (radix_tree_preload(gfp_mask) < 0) {
  343. kmem_cache_free(et->icq_cache, icq);
  344. return NULL;
  345. }
  346. icq->ioc = ioc;
  347. icq->q = q;
  348. INIT_LIST_HEAD(&icq->q_node);
  349. INIT_HLIST_NODE(&icq->ioc_node);
  350. /* lock both q and ioc and try to link @icq */
  351. spin_lock_irq(q->queue_lock);
  352. spin_lock(&ioc->lock);
  353. if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) {
  354. hlist_add_head(&icq->ioc_node, &ioc->icq_list);
  355. list_add(&icq->q_node, &q->icq_list);
  356. if (et->ops.elevator_init_icq_fn)
  357. et->ops.elevator_init_icq_fn(icq);
  358. } else {
  359. kmem_cache_free(et->icq_cache, icq);
  360. icq = ioc_lookup_icq(ioc, q);
  361. if (!icq)
  362. printk(KERN_ERR "cfq: icq link failed!\n");
  363. }
  364. spin_unlock(&ioc->lock);
  365. spin_unlock_irq(q->queue_lock);
  366. radix_tree_preload_end();
  367. return icq;
  368. }
  369. void ioc_set_changed(struct io_context *ioc, int which)
  370. {
  371. struct io_cq *icq;
  372. struct hlist_node *n;
  373. hlist_for_each_entry(icq, n, &ioc->icq_list, ioc_node)
  374. set_bit(which, &icq->changed);
  375. }
  376. /**
  377. * ioc_ioprio_changed - notify ioprio change
  378. * @ioc: io_context of interest
  379. * @ioprio: new ioprio
  380. *
  381. * @ioc's ioprio has changed to @ioprio. Set %ICQ_IOPRIO_CHANGED for all
  382. * icq's. iosched is responsible for checking the bit and applying it on
  383. * request issue path.
  384. */
  385. void ioc_ioprio_changed(struct io_context *ioc, int ioprio)
  386. {
  387. unsigned long flags;
  388. spin_lock_irqsave(&ioc->lock, flags);
  389. ioc->ioprio = ioprio;
  390. ioc_set_changed(ioc, ICQ_IOPRIO_CHANGED);
  391. spin_unlock_irqrestore(&ioc->lock, flags);
  392. }
  393. /**
  394. * ioc_cgroup_changed - notify cgroup change
  395. * @ioc: io_context of interest
  396. *
  397. * @ioc's cgroup has changed. Set %ICQ_CGROUP_CHANGED for all icq's.
  398. * iosched is responsible for checking the bit and applying it on request
  399. * issue path.
  400. */
  401. void ioc_cgroup_changed(struct io_context *ioc)
  402. {
  403. unsigned long flags;
  404. spin_lock_irqsave(&ioc->lock, flags);
  405. ioc_set_changed(ioc, ICQ_CGROUP_CHANGED);
  406. spin_unlock_irqrestore(&ioc->lock, flags);
  407. }
  408. EXPORT_SYMBOL(ioc_cgroup_changed);
  409. static int __init blk_ioc_init(void)
  410. {
  411. iocontext_cachep = kmem_cache_create("blkdev_ioc",
  412. sizeof(struct io_context), 0, SLAB_PANIC, NULL);
  413. return 0;
  414. }
  415. subsys_initcall(blk_ioc_init);