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