blk-ioc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. static void icq_free_icq_rcu(struct rcu_head *head)
  29. {
  30. struct io_cq *icq = container_of(head, struct io_cq, __rcu_head);
  31. kmem_cache_free(icq->__rcu_icq_cache, icq);
  32. }
  33. /* Exit an icq. Called with both ioc and q locked. */
  34. static void ioc_exit_icq(struct io_cq *icq)
  35. {
  36. struct elevator_type *et = icq->q->elevator->type;
  37. if (icq->flags & ICQ_EXITED)
  38. return;
  39. if (et->ops.elevator_exit_icq_fn)
  40. et->ops.elevator_exit_icq_fn(icq);
  41. icq->flags |= ICQ_EXITED;
  42. }
  43. /* Release an icq. Called with both ioc and q locked. */
  44. static void ioc_destroy_icq(struct io_cq *icq)
  45. {
  46. struct io_context *ioc = icq->ioc;
  47. struct request_queue *q = icq->q;
  48. struct elevator_type *et = q->elevator->type;
  49. lockdep_assert_held(&ioc->lock);
  50. lockdep_assert_held(q->queue_lock);
  51. radix_tree_delete(&ioc->icq_tree, icq->q->id);
  52. hlist_del_init(&icq->ioc_node);
  53. list_del_init(&icq->q_node);
  54. /*
  55. * Both setting lookup hint to and clearing it from @icq are done
  56. * under queue_lock. If it's not pointing to @icq now, it never
  57. * will. Hint assignment itself can race safely.
  58. */
  59. if (rcu_dereference_raw(ioc->icq_hint) == icq)
  60. rcu_assign_pointer(ioc->icq_hint, NULL);
  61. ioc_exit_icq(icq);
  62. /*
  63. * @icq->q might have gone away by the time RCU callback runs
  64. * making it impossible to determine icq_cache. Record it in @icq.
  65. */
  66. icq->__rcu_icq_cache = et->icq_cache;
  67. call_rcu(&icq->__rcu_head, icq_free_icq_rcu);
  68. }
  69. /*
  70. * Slow path for ioc release in put_io_context(). Performs double-lock
  71. * dancing to unlink all icq's and then frees ioc.
  72. */
  73. static void ioc_release_fn(struct work_struct *work)
  74. {
  75. struct io_context *ioc = container_of(work, struct io_context,
  76. release_work);
  77. unsigned long flags;
  78. /*
  79. * Exiting icq may call into put_io_context() through elevator
  80. * which will trigger lockdep warning. The ioc's are guaranteed to
  81. * be different, use a different locking subclass here. Use
  82. * irqsave variant as there's no spin_lock_irq_nested().
  83. */
  84. spin_lock_irqsave_nested(&ioc->lock, flags, 1);
  85. while (!hlist_empty(&ioc->icq_list)) {
  86. struct io_cq *icq = hlist_entry(ioc->icq_list.first,
  87. struct io_cq, ioc_node);
  88. struct request_queue *q = icq->q;
  89. if (spin_trylock(q->queue_lock)) {
  90. ioc_destroy_icq(icq);
  91. spin_unlock(q->queue_lock);
  92. } else {
  93. spin_unlock_irqrestore(&ioc->lock, flags);
  94. cpu_relax();
  95. spin_lock_irqsave_nested(&ioc->lock, flags, 1);
  96. }
  97. }
  98. spin_unlock_irqrestore(&ioc->lock, flags);
  99. kmem_cache_free(iocontext_cachep, ioc);
  100. }
  101. /**
  102. * put_io_context - put a reference of io_context
  103. * @ioc: io_context to put
  104. *
  105. * Decrement reference count of @ioc and release it if the count reaches
  106. * zero.
  107. */
  108. void put_io_context(struct io_context *ioc)
  109. {
  110. unsigned long flags;
  111. bool free_ioc = false;
  112. if (ioc == NULL)
  113. return;
  114. BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
  115. /*
  116. * Releasing ioc requires reverse order double locking and we may
  117. * already be holding a queue_lock. Do it asynchronously from wq.
  118. */
  119. if (atomic_long_dec_and_test(&ioc->refcount)) {
  120. spin_lock_irqsave(&ioc->lock, flags);
  121. if (!hlist_empty(&ioc->icq_list))
  122. schedule_work(&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. /* Called by the exiting task */
  132. void exit_io_context(struct task_struct *task)
  133. {
  134. struct io_context *ioc;
  135. struct io_cq *icq;
  136. struct hlist_node *n;
  137. unsigned long flags;
  138. task_lock(task);
  139. ioc = task->io_context;
  140. task->io_context = NULL;
  141. task_unlock(task);
  142. if (!atomic_dec_and_test(&ioc->nr_tasks)) {
  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, n, &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. /**
  169. * ioc_clear_queue - break any ioc association with the specified queue
  170. * @q: request_queue being cleared
  171. *
  172. * Walk @q->icq_list and exit all io_cq's. Must be called with @q locked.
  173. */
  174. void ioc_clear_queue(struct request_queue *q)
  175. {
  176. lockdep_assert_held(q->queue_lock);
  177. while (!list_empty(&q->icq_list)) {
  178. struct io_cq *icq = list_entry(q->icq_list.next,
  179. struct io_cq, q_node);
  180. struct io_context *ioc = icq->ioc;
  181. spin_lock(&ioc->lock);
  182. ioc_destroy_icq(icq);
  183. spin_unlock(&ioc->lock);
  184. }
  185. }
  186. void create_io_context_slowpath(struct task_struct *task, gfp_t gfp_flags,
  187. int node)
  188. {
  189. struct io_context *ioc;
  190. ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
  191. node);
  192. if (unlikely(!ioc))
  193. return;
  194. /* initialize */
  195. atomic_long_set(&ioc->refcount, 1);
  196. atomic_set(&ioc->nr_tasks, 1);
  197. spin_lock_init(&ioc->lock);
  198. INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC | __GFP_HIGH);
  199. INIT_HLIST_HEAD(&ioc->icq_list);
  200. INIT_WORK(&ioc->release_work, ioc_release_fn);
  201. /*
  202. * Try to install. ioc shouldn't be installed if someone else
  203. * already did or @task, which isn't %current, is exiting. Note
  204. * that we need to allow ioc creation on exiting %current as exit
  205. * path may issue IOs from e.g. exit_files(). The exit path is
  206. * responsible for not issuing IO after exit_io_context().
  207. */
  208. task_lock(task);
  209. if (!task->io_context &&
  210. (task == current || !(task->flags & PF_EXITING)))
  211. task->io_context = ioc;
  212. else
  213. kmem_cache_free(iocontext_cachep, ioc);
  214. task_unlock(task);
  215. }
  216. /**
  217. * get_task_io_context - get io_context of a task
  218. * @task: task of interest
  219. * @gfp_flags: allocation flags, used if allocation is necessary
  220. * @node: allocation node, used if allocation is necessary
  221. *
  222. * Return io_context of @task. If it doesn't exist, it is created with
  223. * @gfp_flags and @node. The returned io_context has its reference count
  224. * incremented.
  225. *
  226. * This function always goes through task_lock() and it's better to use
  227. * %current->io_context + get_io_context() for %current.
  228. */
  229. struct io_context *get_task_io_context(struct task_struct *task,
  230. gfp_t gfp_flags, int node)
  231. {
  232. struct io_context *ioc;
  233. might_sleep_if(gfp_flags & __GFP_WAIT);
  234. do {
  235. task_lock(task);
  236. ioc = task->io_context;
  237. if (likely(ioc)) {
  238. get_io_context(ioc);
  239. task_unlock(task);
  240. return ioc;
  241. }
  242. task_unlock(task);
  243. } while (create_io_context(task, gfp_flags, node));
  244. return NULL;
  245. }
  246. EXPORT_SYMBOL(get_task_io_context);
  247. /**
  248. * ioc_lookup_icq - lookup io_cq from ioc
  249. * @ioc: the associated io_context
  250. * @q: the associated request_queue
  251. *
  252. * Look up io_cq associated with @ioc - @q pair from @ioc. Must be called
  253. * with @q->queue_lock held.
  254. */
  255. struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q)
  256. {
  257. struct io_cq *icq;
  258. lockdep_assert_held(q->queue_lock);
  259. /*
  260. * icq's are indexed from @ioc using radix tree and hint pointer,
  261. * both of which are protected with RCU. All removals are done
  262. * holding both q and ioc locks, and we're holding q lock - if we
  263. * find a icq which points to us, it's guaranteed to be valid.
  264. */
  265. rcu_read_lock();
  266. icq = rcu_dereference(ioc->icq_hint);
  267. if (icq && icq->q == q)
  268. goto out;
  269. icq = radix_tree_lookup(&ioc->icq_tree, q->id);
  270. if (icq && icq->q == q)
  271. rcu_assign_pointer(ioc->icq_hint, icq); /* allowed to race */
  272. else
  273. icq = NULL;
  274. out:
  275. rcu_read_unlock();
  276. return icq;
  277. }
  278. EXPORT_SYMBOL(ioc_lookup_icq);
  279. /**
  280. * ioc_create_icq - create and link io_cq
  281. * @q: request_queue of interest
  282. * @gfp_mask: allocation mask
  283. *
  284. * Make sure io_cq linking %current->io_context and @q exists. If either
  285. * io_context and/or icq don't exist, they will be created using @gfp_mask.
  286. *
  287. * The caller is responsible for ensuring @ioc won't go away and @q is
  288. * alive and will stay alive until this function returns.
  289. */
  290. struct io_cq *ioc_create_icq(struct request_queue *q, gfp_t gfp_mask)
  291. {
  292. struct elevator_type *et = q->elevator->type;
  293. struct io_context *ioc;
  294. struct io_cq *icq;
  295. /* allocate stuff */
  296. ioc = create_io_context(current, gfp_mask, q->node);
  297. if (!ioc)
  298. return NULL;
  299. icq = kmem_cache_alloc_node(et->icq_cache, gfp_mask | __GFP_ZERO,
  300. q->node);
  301. if (!icq)
  302. return NULL;
  303. if (radix_tree_preload(gfp_mask) < 0) {
  304. kmem_cache_free(et->icq_cache, icq);
  305. return NULL;
  306. }
  307. icq->ioc = ioc;
  308. icq->q = q;
  309. INIT_LIST_HEAD(&icq->q_node);
  310. INIT_HLIST_NODE(&icq->ioc_node);
  311. /* lock both q and ioc and try to link @icq */
  312. spin_lock_irq(q->queue_lock);
  313. spin_lock(&ioc->lock);
  314. if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) {
  315. hlist_add_head(&icq->ioc_node, &ioc->icq_list);
  316. list_add(&icq->q_node, &q->icq_list);
  317. if (et->ops.elevator_init_icq_fn)
  318. et->ops.elevator_init_icq_fn(icq);
  319. } else {
  320. kmem_cache_free(et->icq_cache, icq);
  321. icq = ioc_lookup_icq(ioc, q);
  322. if (!icq)
  323. printk(KERN_ERR "cfq: icq link failed!\n");
  324. }
  325. spin_unlock(&ioc->lock);
  326. spin_unlock_irq(q->queue_lock);
  327. radix_tree_preload_end();
  328. return icq;
  329. }
  330. void ioc_set_icq_flags(struct io_context *ioc, unsigned int flags)
  331. {
  332. struct io_cq *icq;
  333. struct hlist_node *n;
  334. hlist_for_each_entry(icq, n, &ioc->icq_list, ioc_node)
  335. icq->flags |= flags;
  336. }
  337. /**
  338. * ioc_ioprio_changed - notify ioprio change
  339. * @ioc: io_context of interest
  340. * @ioprio: new ioprio
  341. *
  342. * @ioc's ioprio has changed to @ioprio. Set %ICQ_IOPRIO_CHANGED for all
  343. * icq's. iosched is responsible for checking the bit and applying it on
  344. * request issue path.
  345. */
  346. void ioc_ioprio_changed(struct io_context *ioc, int ioprio)
  347. {
  348. unsigned long flags;
  349. spin_lock_irqsave(&ioc->lock, flags);
  350. ioc->ioprio = ioprio;
  351. ioc_set_icq_flags(ioc, ICQ_IOPRIO_CHANGED);
  352. spin_unlock_irqrestore(&ioc->lock, flags);
  353. }
  354. /**
  355. * ioc_cgroup_changed - notify cgroup change
  356. * @ioc: io_context of interest
  357. *
  358. * @ioc's cgroup has changed. Set %ICQ_CGROUP_CHANGED for all icq's.
  359. * iosched is responsible for checking the bit and applying it on request
  360. * issue path.
  361. */
  362. void ioc_cgroup_changed(struct io_context *ioc)
  363. {
  364. unsigned long flags;
  365. spin_lock_irqsave(&ioc->lock, flags);
  366. ioc_set_icq_flags(ioc, ICQ_CGROUP_CHANGED);
  367. spin_unlock_irqrestore(&ioc->lock, flags);
  368. }
  369. EXPORT_SYMBOL(ioc_cgroup_changed);
  370. /**
  371. * icq_get_changed - fetch and clear icq changed mask
  372. * @icq: icq of interest
  373. *
  374. * Fetch and clear ICQ_*_CHANGED bits from @icq. Grabs and releases
  375. * @icq->ioc->lock.
  376. */
  377. unsigned icq_get_changed(struct io_cq *icq)
  378. {
  379. unsigned int changed = 0;
  380. unsigned long flags;
  381. if (unlikely(icq->flags & ICQ_CHANGED_MASK)) {
  382. spin_lock_irqsave(&icq->ioc->lock, flags);
  383. changed = icq->flags & ICQ_CHANGED_MASK;
  384. icq->flags &= ~ICQ_CHANGED_MASK;
  385. spin_unlock_irqrestore(&icq->ioc->lock, flags);
  386. }
  387. return changed;
  388. }
  389. EXPORT_SYMBOL(icq_get_changed);
  390. static int __init blk_ioc_init(void)
  391. {
  392. iocontext_cachep = kmem_cache_create("blkdev_ioc",
  393. sizeof(struct io_context), 0, SLAB_PANIC, NULL);
  394. return 0;
  395. }
  396. subsys_initcall(blk_ioc_init);