blk-ioc.c 11 KB

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