blk-ioc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. /* PF_EXITING prevents new io_context from being attached to @task */
  201. WARN_ON_ONCE(!(current->flags & PF_EXITING));
  202. task_lock(task);
  203. ioc = task->io_context;
  204. task->io_context = NULL;
  205. task_unlock(task);
  206. atomic_dec(&ioc->nr_tasks);
  207. put_io_context(ioc, NULL);
  208. }
  209. /**
  210. * ioc_clear_queue - break any ioc association with the specified queue
  211. * @q: request_queue being cleared
  212. *
  213. * Walk @q->icq_list and exit all io_cq's. Must be called with @q locked.
  214. */
  215. void ioc_clear_queue(struct request_queue *q)
  216. {
  217. lockdep_assert_held(q->queue_lock);
  218. while (!list_empty(&q->icq_list)) {
  219. struct io_cq *icq = list_entry(q->icq_list.next,
  220. struct io_cq, q_node);
  221. struct io_context *ioc = icq->ioc;
  222. spin_lock(&ioc->lock);
  223. ioc_exit_icq(icq);
  224. spin_unlock(&ioc->lock);
  225. }
  226. }
  227. void create_io_context_slowpath(struct task_struct *task, gfp_t gfp_flags,
  228. int node)
  229. {
  230. struct io_context *ioc;
  231. ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
  232. node);
  233. if (unlikely(!ioc))
  234. return;
  235. /* initialize */
  236. atomic_long_set(&ioc->refcount, 1);
  237. atomic_set(&ioc->nr_tasks, 1);
  238. spin_lock_init(&ioc->lock);
  239. INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC | __GFP_HIGH);
  240. INIT_HLIST_HEAD(&ioc->icq_list);
  241. INIT_WORK(&ioc->release_work, ioc_release_fn);
  242. /* try to install, somebody might already have beaten us to it */
  243. task_lock(task);
  244. if (!task->io_context && !(task->flags & PF_EXITING))
  245. task->io_context = ioc;
  246. else
  247. kmem_cache_free(iocontext_cachep, ioc);
  248. task_unlock(task);
  249. }
  250. /**
  251. * get_task_io_context - get io_context of a task
  252. * @task: task of interest
  253. * @gfp_flags: allocation flags, used if allocation is necessary
  254. * @node: allocation node, used if allocation is necessary
  255. *
  256. * Return io_context of @task. If it doesn't exist, it is created with
  257. * @gfp_flags and @node. The returned io_context has its reference count
  258. * incremented.
  259. *
  260. * This function always goes through task_lock() and it's better to use
  261. * %current->io_context + get_io_context() for %current.
  262. */
  263. struct io_context *get_task_io_context(struct task_struct *task,
  264. gfp_t gfp_flags, int node)
  265. {
  266. struct io_context *ioc;
  267. might_sleep_if(gfp_flags & __GFP_WAIT);
  268. do {
  269. task_lock(task);
  270. ioc = task->io_context;
  271. if (likely(ioc)) {
  272. get_io_context(ioc);
  273. task_unlock(task);
  274. return ioc;
  275. }
  276. task_unlock(task);
  277. } while (create_io_context(task, gfp_flags, node));
  278. return NULL;
  279. }
  280. EXPORT_SYMBOL(get_task_io_context);
  281. /**
  282. * ioc_lookup_icq - lookup io_cq from ioc
  283. * @ioc: the associated io_context
  284. * @q: the associated request_queue
  285. *
  286. * Look up io_cq associated with @ioc - @q pair from @ioc. Must be called
  287. * with @q->queue_lock held.
  288. */
  289. struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q)
  290. {
  291. struct io_cq *icq;
  292. lockdep_assert_held(q->queue_lock);
  293. /*
  294. * icq's are indexed from @ioc using radix tree and hint pointer,
  295. * both of which are protected with RCU. All removals are done
  296. * holding both q and ioc locks, and we're holding q lock - if we
  297. * find a icq which points to us, it's guaranteed to be valid.
  298. */
  299. rcu_read_lock();
  300. icq = rcu_dereference(ioc->icq_hint);
  301. if (icq && icq->q == q)
  302. goto out;
  303. icq = radix_tree_lookup(&ioc->icq_tree, q->id);
  304. if (icq && icq->q == q)
  305. rcu_assign_pointer(ioc->icq_hint, icq); /* allowed to race */
  306. else
  307. icq = NULL;
  308. out:
  309. rcu_read_unlock();
  310. return icq;
  311. }
  312. EXPORT_SYMBOL(ioc_lookup_icq);
  313. /**
  314. * ioc_create_icq - create and link io_cq
  315. * @q: request_queue of interest
  316. * @gfp_mask: allocation mask
  317. *
  318. * Make sure io_cq linking %current->io_context and @q exists. If either
  319. * io_context and/or icq don't exist, they will be created using @gfp_mask.
  320. *
  321. * The caller is responsible for ensuring @ioc won't go away and @q is
  322. * alive and will stay alive until this function returns.
  323. */
  324. struct io_cq *ioc_create_icq(struct request_queue *q, gfp_t gfp_mask)
  325. {
  326. struct elevator_type *et = q->elevator->type;
  327. struct io_context *ioc;
  328. struct io_cq *icq;
  329. /* allocate stuff */
  330. ioc = create_io_context(current, gfp_mask, q->node);
  331. if (!ioc)
  332. return NULL;
  333. icq = kmem_cache_alloc_node(et->icq_cache, gfp_mask | __GFP_ZERO,
  334. q->node);
  335. if (!icq)
  336. return NULL;
  337. if (radix_tree_preload(gfp_mask) < 0) {
  338. kmem_cache_free(et->icq_cache, icq);
  339. return NULL;
  340. }
  341. icq->ioc = ioc;
  342. icq->q = q;
  343. INIT_LIST_HEAD(&icq->q_node);
  344. INIT_HLIST_NODE(&icq->ioc_node);
  345. /* lock both q and ioc and try to link @icq */
  346. spin_lock_irq(q->queue_lock);
  347. spin_lock(&ioc->lock);
  348. if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) {
  349. hlist_add_head(&icq->ioc_node, &ioc->icq_list);
  350. list_add(&icq->q_node, &q->icq_list);
  351. if (et->ops.elevator_init_icq_fn)
  352. et->ops.elevator_init_icq_fn(icq);
  353. } else {
  354. kmem_cache_free(et->icq_cache, icq);
  355. icq = ioc_lookup_icq(ioc, q);
  356. if (!icq)
  357. printk(KERN_ERR "cfq: icq link failed!\n");
  358. }
  359. spin_unlock(&ioc->lock);
  360. spin_unlock_irq(q->queue_lock);
  361. radix_tree_preload_end();
  362. return icq;
  363. }
  364. void ioc_set_changed(struct io_context *ioc, int which)
  365. {
  366. struct io_cq *icq;
  367. struct hlist_node *n;
  368. hlist_for_each_entry(icq, n, &ioc->icq_list, ioc_node)
  369. set_bit(which, &icq->changed);
  370. }
  371. /**
  372. * ioc_ioprio_changed - notify ioprio change
  373. * @ioc: io_context of interest
  374. * @ioprio: new ioprio
  375. *
  376. * @ioc's ioprio has changed to @ioprio. Set %ICQ_IOPRIO_CHANGED for all
  377. * icq's. iosched is responsible for checking the bit and applying it on
  378. * request issue path.
  379. */
  380. void ioc_ioprio_changed(struct io_context *ioc, int ioprio)
  381. {
  382. unsigned long flags;
  383. spin_lock_irqsave(&ioc->lock, flags);
  384. ioc->ioprio = ioprio;
  385. ioc_set_changed(ioc, ICQ_IOPRIO_CHANGED);
  386. spin_unlock_irqrestore(&ioc->lock, flags);
  387. }
  388. /**
  389. * ioc_cgroup_changed - notify cgroup change
  390. * @ioc: io_context of interest
  391. *
  392. * @ioc's cgroup has changed. Set %ICQ_CGROUP_CHANGED for all icq's.
  393. * iosched is responsible for checking the bit and applying it on request
  394. * issue path.
  395. */
  396. void ioc_cgroup_changed(struct io_context *ioc)
  397. {
  398. unsigned long flags;
  399. spin_lock_irqsave(&ioc->lock, flags);
  400. ioc_set_changed(ioc, ICQ_CGROUP_CHANGED);
  401. spin_unlock_irqrestore(&ioc->lock, flags);
  402. }
  403. static int __init blk_ioc_init(void)
  404. {
  405. iocontext_cachep = kmem_cache_create("blkdev_ioc",
  406. sizeof(struct io_context), 0, SLAB_PANIC, NULL);
  407. return 0;
  408. }
  409. subsys_initcall(blk_ioc_init);