blk-ioc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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/security.h>
  12. #include <linux/sched/task.h>
  13. #include "blk.h"
  14. #include "blk-mq-sched.h"
  15. /*
  16. * For io context allocations
  17. */
  18. static struct kmem_cache *iocontext_cachep;
  19. #ifdef CONFIG_BLK_ICQ
  20. /**
  21. * get_io_context - increment reference count to io_context
  22. * @ioc: io_context to get
  23. *
  24. * Increment reference count to @ioc.
  25. */
  26. static void get_io_context(struct io_context *ioc)
  27. {
  28. BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
  29. atomic_long_inc(&ioc->refcount);
  30. }
  31. static void icq_free_icq_rcu(struct rcu_head *head)
  32. {
  33. struct io_cq *icq = container_of(head, struct io_cq, __rcu_head);
  34. kmem_cache_free(icq->__rcu_icq_cache, icq);
  35. }
  36. /*
  37. * Exit an icq. Called with ioc locked for blk-mq, and with both ioc
  38. * and queue locked for legacy.
  39. */
  40. static void ioc_exit_icq(struct io_cq *icq)
  41. {
  42. struct elevator_type *et = icq->q->elevator->type;
  43. if (icq->flags & ICQ_EXITED)
  44. return;
  45. if (et->ops.exit_icq)
  46. et->ops.exit_icq(icq);
  47. icq->flags |= ICQ_EXITED;
  48. }
  49. static void ioc_exit_icqs(struct io_context *ioc)
  50. {
  51. struct io_cq *icq;
  52. spin_lock_irq(&ioc->lock);
  53. hlist_for_each_entry(icq, &ioc->icq_list, ioc_node)
  54. ioc_exit_icq(icq);
  55. spin_unlock_irq(&ioc->lock);
  56. }
  57. /*
  58. * Release an icq. Called with ioc locked for blk-mq, and with both ioc
  59. * and queue locked for legacy.
  60. */
  61. static void ioc_destroy_icq(struct io_cq *icq)
  62. {
  63. struct io_context *ioc = icq->ioc;
  64. struct request_queue *q = icq->q;
  65. struct elevator_type *et = q->elevator->type;
  66. lockdep_assert_held(&ioc->lock);
  67. lockdep_assert_held(&q->queue_lock);
  68. if (icq->flags & ICQ_DESTROYED)
  69. return;
  70. radix_tree_delete(&ioc->icq_tree, icq->q->id);
  71. hlist_del_init(&icq->ioc_node);
  72. list_del_init(&icq->q_node);
  73. /*
  74. * Both setting lookup hint to and clearing it from @icq are done
  75. * under queue_lock. If it's not pointing to @icq now, it never
  76. * will. Hint assignment itself can race safely.
  77. */
  78. if (rcu_access_pointer(ioc->icq_hint) == icq)
  79. rcu_assign_pointer(ioc->icq_hint, NULL);
  80. ioc_exit_icq(icq);
  81. /*
  82. * @icq->q might have gone away by the time RCU callback runs
  83. * making it impossible to determine icq_cache. Record it in @icq.
  84. */
  85. icq->__rcu_icq_cache = et->icq_cache;
  86. icq->flags |= ICQ_DESTROYED;
  87. call_rcu(&icq->__rcu_head, icq_free_icq_rcu);
  88. }
  89. /*
  90. * Slow path for ioc release in put_io_context(). Performs double-lock
  91. * dancing to unlink all icq's and then frees ioc.
  92. */
  93. static void ioc_release_fn(struct work_struct *work)
  94. {
  95. struct io_context *ioc = container_of(work, struct io_context,
  96. release_work);
  97. spin_lock_irq(&ioc->lock);
  98. while (!hlist_empty(&ioc->icq_list)) {
  99. struct io_cq *icq = hlist_entry(ioc->icq_list.first,
  100. struct io_cq, ioc_node);
  101. struct request_queue *q = icq->q;
  102. if (spin_trylock(&q->queue_lock)) {
  103. ioc_destroy_icq(icq);
  104. spin_unlock(&q->queue_lock);
  105. } else {
  106. /* Make sure q and icq cannot be freed. */
  107. rcu_read_lock();
  108. /* Re-acquire the locks in the correct order. */
  109. spin_unlock(&ioc->lock);
  110. spin_lock(&q->queue_lock);
  111. spin_lock(&ioc->lock);
  112. ioc_destroy_icq(icq);
  113. spin_unlock(&q->queue_lock);
  114. rcu_read_unlock();
  115. }
  116. }
  117. spin_unlock_irq(&ioc->lock);
  118. kmem_cache_free(iocontext_cachep, ioc);
  119. }
  120. /*
  121. * Releasing icqs requires reverse order double locking and we may already be
  122. * holding a queue_lock. Do it asynchronously from a workqueue.
  123. */
  124. static bool ioc_delay_free(struct io_context *ioc)
  125. {
  126. unsigned long flags;
  127. spin_lock_irqsave(&ioc->lock, flags);
  128. if (!hlist_empty(&ioc->icq_list)) {
  129. queue_work(system_power_efficient_wq, &ioc->release_work);
  130. spin_unlock_irqrestore(&ioc->lock, flags);
  131. return true;
  132. }
  133. spin_unlock_irqrestore(&ioc->lock, flags);
  134. return false;
  135. }
  136. /**
  137. * ioc_clear_queue - break any ioc association with the specified queue
  138. * @q: request_queue being cleared
  139. *
  140. * Walk @q->icq_list and exit all io_cq's.
  141. */
  142. void ioc_clear_queue(struct request_queue *q)
  143. {
  144. spin_lock_irq(&q->queue_lock);
  145. while (!list_empty(&q->icq_list)) {
  146. struct io_cq *icq =
  147. list_first_entry(&q->icq_list, struct io_cq, q_node);
  148. /*
  149. * Other context won't hold ioc lock to wait for queue_lock, see
  150. * details in ioc_release_fn().
  151. */
  152. spin_lock(&icq->ioc->lock);
  153. ioc_destroy_icq(icq);
  154. spin_unlock(&icq->ioc->lock);
  155. }
  156. spin_unlock_irq(&q->queue_lock);
  157. }
  158. #else /* CONFIG_BLK_ICQ */
  159. static inline void ioc_exit_icqs(struct io_context *ioc)
  160. {
  161. }
  162. static inline bool ioc_delay_free(struct io_context *ioc)
  163. {
  164. return false;
  165. }
  166. #endif /* CONFIG_BLK_ICQ */
  167. /**
  168. * put_io_context - put a reference of io_context
  169. * @ioc: io_context to put
  170. *
  171. * Decrement reference count of @ioc and release it if the count reaches
  172. * zero.
  173. */
  174. void put_io_context(struct io_context *ioc)
  175. {
  176. BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
  177. if (atomic_long_dec_and_test(&ioc->refcount) && !ioc_delay_free(ioc))
  178. kmem_cache_free(iocontext_cachep, ioc);
  179. }
  180. EXPORT_SYMBOL_GPL(put_io_context);
  181. /* Called by the exiting task */
  182. void exit_io_context(struct task_struct *task)
  183. {
  184. struct io_context *ioc;
  185. task_lock(task);
  186. ioc = task->io_context;
  187. task->io_context = NULL;
  188. task_unlock(task);
  189. if (atomic_dec_and_test(&ioc->active_ref)) {
  190. ioc_exit_icqs(ioc);
  191. put_io_context(ioc);
  192. }
  193. }
  194. static struct io_context *alloc_io_context(gfp_t gfp_flags, int node)
  195. {
  196. struct io_context *ioc;
  197. ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
  198. node);
  199. if (unlikely(!ioc))
  200. return NULL;
  201. atomic_long_set(&ioc->refcount, 1);
  202. atomic_set(&ioc->active_ref, 1);
  203. #ifdef CONFIG_BLK_ICQ
  204. spin_lock_init(&ioc->lock);
  205. INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC);
  206. INIT_HLIST_HEAD(&ioc->icq_list);
  207. INIT_WORK(&ioc->release_work, ioc_release_fn);
  208. #endif
  209. ioc->ioprio = IOPRIO_DEFAULT;
  210. return ioc;
  211. }
  212. int set_task_ioprio(struct task_struct *task, int ioprio)
  213. {
  214. int err;
  215. const struct cred *cred = current_cred(), *tcred;
  216. rcu_read_lock();
  217. tcred = __task_cred(task);
  218. if (!uid_eq(tcred->uid, cred->euid) &&
  219. !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
  220. rcu_read_unlock();
  221. return -EPERM;
  222. }
  223. rcu_read_unlock();
  224. err = security_task_setioprio(task, ioprio);
  225. if (err)
  226. return err;
  227. task_lock(task);
  228. if (unlikely(!task->io_context)) {
  229. struct io_context *ioc;
  230. task_unlock(task);
  231. ioc = alloc_io_context(GFP_ATOMIC, NUMA_NO_NODE);
  232. if (!ioc)
  233. return -ENOMEM;
  234. task_lock(task);
  235. if (task->flags & PF_EXITING) {
  236. kmem_cache_free(iocontext_cachep, ioc);
  237. goto out;
  238. }
  239. if (task->io_context)
  240. kmem_cache_free(iocontext_cachep, ioc);
  241. else
  242. task->io_context = ioc;
  243. }
  244. task->io_context->ioprio = ioprio;
  245. out:
  246. task_unlock(task);
  247. return 0;
  248. }
  249. EXPORT_SYMBOL_GPL(set_task_ioprio);
  250. int __copy_io(unsigned long clone_flags, struct task_struct *tsk)
  251. {
  252. struct io_context *ioc = current->io_context;
  253. /*
  254. * Share io context with parent, if CLONE_IO is set
  255. */
  256. if (clone_flags & CLONE_IO) {
  257. atomic_inc(&ioc->active_ref);
  258. tsk->io_context = ioc;
  259. } else if (ioprio_valid(ioc->ioprio)) {
  260. tsk->io_context = alloc_io_context(GFP_KERNEL, NUMA_NO_NODE);
  261. if (!tsk->io_context)
  262. return -ENOMEM;
  263. tsk->io_context->ioprio = ioc->ioprio;
  264. }
  265. return 0;
  266. }
  267. #ifdef CONFIG_BLK_ICQ
  268. /**
  269. * ioc_lookup_icq - lookup io_cq from ioc
  270. * @q: the associated request_queue
  271. *
  272. * Look up io_cq associated with @ioc - @q pair from @ioc. Must be called
  273. * with @q->queue_lock held.
  274. */
  275. struct io_cq *ioc_lookup_icq(struct request_queue *q)
  276. {
  277. struct io_context *ioc = current->io_context;
  278. struct io_cq *icq;
  279. lockdep_assert_held(&q->queue_lock);
  280. /*
  281. * icq's are indexed from @ioc using radix tree and hint pointer,
  282. * both of which are protected with RCU. All removals are done
  283. * holding both q and ioc locks, and we're holding q lock - if we
  284. * find a icq which points to us, it's guaranteed to be valid.
  285. */
  286. rcu_read_lock();
  287. icq = rcu_dereference(ioc->icq_hint);
  288. if (icq && icq->q == q)
  289. goto out;
  290. icq = radix_tree_lookup(&ioc->icq_tree, q->id);
  291. if (icq && icq->q == q)
  292. rcu_assign_pointer(ioc->icq_hint, icq); /* allowed to race */
  293. else
  294. icq = NULL;
  295. out:
  296. rcu_read_unlock();
  297. return icq;
  298. }
  299. EXPORT_SYMBOL(ioc_lookup_icq);
  300. /**
  301. * ioc_create_icq - create and link io_cq
  302. * @q: request_queue of interest
  303. *
  304. * Make sure io_cq linking @ioc and @q exists. If icq doesn't exist, they
  305. * will be created using @gfp_mask.
  306. *
  307. * The caller is responsible for ensuring @ioc won't go away and @q is
  308. * alive and will stay alive until this function returns.
  309. */
  310. static struct io_cq *ioc_create_icq(struct request_queue *q)
  311. {
  312. struct io_context *ioc = current->io_context;
  313. struct elevator_type *et = q->elevator->type;
  314. struct io_cq *icq;
  315. /* allocate stuff */
  316. icq = kmem_cache_alloc_node(et->icq_cache, GFP_ATOMIC | __GFP_ZERO,
  317. q->node);
  318. if (!icq)
  319. return NULL;
  320. if (radix_tree_maybe_preload(GFP_ATOMIC) < 0) {
  321. kmem_cache_free(et->icq_cache, icq);
  322. return NULL;
  323. }
  324. icq->ioc = ioc;
  325. icq->q = q;
  326. INIT_LIST_HEAD(&icq->q_node);
  327. INIT_HLIST_NODE(&icq->ioc_node);
  328. /* lock both q and ioc and try to link @icq */
  329. spin_lock_irq(&q->queue_lock);
  330. spin_lock(&ioc->lock);
  331. if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) {
  332. hlist_add_head(&icq->ioc_node, &ioc->icq_list);
  333. list_add(&icq->q_node, &q->icq_list);
  334. if (et->ops.init_icq)
  335. et->ops.init_icq(icq);
  336. } else {
  337. kmem_cache_free(et->icq_cache, icq);
  338. icq = ioc_lookup_icq(q);
  339. if (!icq)
  340. printk(KERN_ERR "cfq: icq link failed!\n");
  341. }
  342. spin_unlock(&ioc->lock);
  343. spin_unlock_irq(&q->queue_lock);
  344. radix_tree_preload_end();
  345. return icq;
  346. }
  347. struct io_cq *ioc_find_get_icq(struct request_queue *q)
  348. {
  349. struct io_context *ioc = current->io_context;
  350. struct io_cq *icq = NULL;
  351. if (unlikely(!ioc)) {
  352. ioc = alloc_io_context(GFP_ATOMIC, q->node);
  353. if (!ioc)
  354. return NULL;
  355. task_lock(current);
  356. if (current->io_context) {
  357. kmem_cache_free(iocontext_cachep, ioc);
  358. ioc = current->io_context;
  359. } else {
  360. current->io_context = ioc;
  361. }
  362. get_io_context(ioc);
  363. task_unlock(current);
  364. } else {
  365. get_io_context(ioc);
  366. spin_lock_irq(&q->queue_lock);
  367. icq = ioc_lookup_icq(q);
  368. spin_unlock_irq(&q->queue_lock);
  369. }
  370. if (!icq) {
  371. icq = ioc_create_icq(q);
  372. if (!icq) {
  373. put_io_context(ioc);
  374. return NULL;
  375. }
  376. }
  377. return icq;
  378. }
  379. EXPORT_SYMBOL_GPL(ioc_find_get_icq);
  380. #endif /* CONFIG_BLK_ICQ */
  381. static int __init blk_ioc_init(void)
  382. {
  383. iocontext_cachep = kmem_cache_create("blkdev_ioc",
  384. sizeof(struct io_context), 0, SLAB_PANIC, NULL);
  385. return 0;
  386. }
  387. subsys_initcall(blk_ioc_init);