blk-mq.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  1. /*
  2. * Block multiqueue core code
  3. *
  4. * Copyright (C) 2013-2014 Jens Axboe
  5. * Copyright (C) 2013-2014 Christoph Hellwig
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/backing-dev.h>
  10. #include <linux/bio.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/mm.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/smp.h>
  17. #include <linux/llist.h>
  18. #include <linux/list_sort.h>
  19. #include <linux/cpu.h>
  20. #include <linux/cache.h>
  21. #include <linux/sched/sysctl.h>
  22. #include <linux/delay.h>
  23. #include <trace/events/block.h>
  24. #include <linux/blk-mq.h>
  25. #include "blk.h"
  26. #include "blk-mq.h"
  27. #include "blk-mq-tag.h"
  28. static DEFINE_MUTEX(all_q_mutex);
  29. static LIST_HEAD(all_q_list);
  30. static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
  31. /*
  32. * Check if any of the ctx's have pending work in this hardware queue
  33. */
  34. static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
  35. {
  36. unsigned int i;
  37. for (i = 0; i < hctx->ctx_map.map_size; i++)
  38. if (hctx->ctx_map.map[i].word)
  39. return true;
  40. return false;
  41. }
  42. static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
  43. struct blk_mq_ctx *ctx)
  44. {
  45. return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
  46. }
  47. #define CTX_TO_BIT(hctx, ctx) \
  48. ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
  49. /*
  50. * Mark this ctx as having pending work in this hardware queue
  51. */
  52. static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
  53. struct blk_mq_ctx *ctx)
  54. {
  55. struct blk_align_bitmap *bm = get_bm(hctx, ctx);
  56. if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
  57. set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
  58. }
  59. static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
  60. struct blk_mq_ctx *ctx)
  61. {
  62. struct blk_align_bitmap *bm = get_bm(hctx, ctx);
  63. clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
  64. }
  65. static int blk_mq_queue_enter(struct request_queue *q)
  66. {
  67. while (true) {
  68. int ret;
  69. if (percpu_ref_tryget_live(&q->mq_usage_counter))
  70. return 0;
  71. ret = wait_event_interruptible(q->mq_freeze_wq,
  72. !q->mq_freeze_depth || blk_queue_dying(q));
  73. if (blk_queue_dying(q))
  74. return -ENODEV;
  75. if (ret)
  76. return ret;
  77. }
  78. }
  79. static void blk_mq_queue_exit(struct request_queue *q)
  80. {
  81. percpu_ref_put(&q->mq_usage_counter);
  82. }
  83. static void blk_mq_usage_counter_release(struct percpu_ref *ref)
  84. {
  85. struct request_queue *q =
  86. container_of(ref, struct request_queue, mq_usage_counter);
  87. wake_up_all(&q->mq_freeze_wq);
  88. }
  89. /*
  90. * Guarantee no request is in use, so we can change any data structure of
  91. * the queue afterward.
  92. */
  93. void blk_mq_freeze_queue(struct request_queue *q)
  94. {
  95. bool freeze;
  96. spin_lock_irq(q->queue_lock);
  97. freeze = !q->mq_freeze_depth++;
  98. spin_unlock_irq(q->queue_lock);
  99. if (freeze) {
  100. percpu_ref_kill(&q->mq_usage_counter);
  101. blk_mq_run_queues(q, false);
  102. }
  103. wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
  104. }
  105. static void blk_mq_unfreeze_queue(struct request_queue *q)
  106. {
  107. bool wake;
  108. spin_lock_irq(q->queue_lock);
  109. wake = !--q->mq_freeze_depth;
  110. WARN_ON_ONCE(q->mq_freeze_depth < 0);
  111. spin_unlock_irq(q->queue_lock);
  112. if (wake) {
  113. percpu_ref_reinit(&q->mq_usage_counter);
  114. wake_up_all(&q->mq_freeze_wq);
  115. }
  116. }
  117. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  118. {
  119. return blk_mq_has_free_tags(hctx->tags);
  120. }
  121. EXPORT_SYMBOL(blk_mq_can_queue);
  122. static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
  123. struct request *rq, unsigned int rw_flags)
  124. {
  125. if (blk_queue_io_stat(q))
  126. rw_flags |= REQ_IO_STAT;
  127. INIT_LIST_HEAD(&rq->queuelist);
  128. /* csd/requeue_work/fifo_time is initialized before use */
  129. rq->q = q;
  130. rq->mq_ctx = ctx;
  131. rq->cmd_flags |= rw_flags;
  132. /* do not touch atomic flags, it needs atomic ops against the timer */
  133. rq->cpu = -1;
  134. INIT_HLIST_NODE(&rq->hash);
  135. RB_CLEAR_NODE(&rq->rb_node);
  136. rq->rq_disk = NULL;
  137. rq->part = NULL;
  138. rq->start_time = jiffies;
  139. #ifdef CONFIG_BLK_CGROUP
  140. rq->rl = NULL;
  141. set_start_time_ns(rq);
  142. rq->io_start_time_ns = 0;
  143. #endif
  144. rq->nr_phys_segments = 0;
  145. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  146. rq->nr_integrity_segments = 0;
  147. #endif
  148. rq->special = NULL;
  149. /* tag was already set */
  150. rq->errors = 0;
  151. rq->cmd = rq->__cmd;
  152. rq->extra_len = 0;
  153. rq->sense_len = 0;
  154. rq->resid_len = 0;
  155. rq->sense = NULL;
  156. INIT_LIST_HEAD(&rq->timeout_list);
  157. rq->timeout = 0;
  158. rq->end_io = NULL;
  159. rq->end_io_data = NULL;
  160. rq->next_rq = NULL;
  161. ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
  162. }
  163. static struct request *
  164. __blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw)
  165. {
  166. struct request *rq;
  167. unsigned int tag;
  168. tag = blk_mq_get_tag(data);
  169. if (tag != BLK_MQ_TAG_FAIL) {
  170. rq = data->hctx->tags->rqs[tag];
  171. if (blk_mq_tag_busy(data->hctx)) {
  172. rq->cmd_flags = REQ_MQ_INFLIGHT;
  173. atomic_inc(&data->hctx->nr_active);
  174. }
  175. rq->tag = tag;
  176. blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw);
  177. return rq;
  178. }
  179. return NULL;
  180. }
  181. struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
  182. bool reserved)
  183. {
  184. struct blk_mq_ctx *ctx;
  185. struct blk_mq_hw_ctx *hctx;
  186. struct request *rq;
  187. struct blk_mq_alloc_data alloc_data;
  188. if (blk_mq_queue_enter(q))
  189. return NULL;
  190. ctx = blk_mq_get_ctx(q);
  191. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  192. blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT,
  193. reserved, ctx, hctx);
  194. rq = __blk_mq_alloc_request(&alloc_data, rw);
  195. if (!rq && (gfp & __GFP_WAIT)) {
  196. __blk_mq_run_hw_queue(hctx);
  197. blk_mq_put_ctx(ctx);
  198. ctx = blk_mq_get_ctx(q);
  199. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  200. blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx,
  201. hctx);
  202. rq = __blk_mq_alloc_request(&alloc_data, rw);
  203. ctx = alloc_data.ctx;
  204. }
  205. blk_mq_put_ctx(ctx);
  206. return rq;
  207. }
  208. EXPORT_SYMBOL(blk_mq_alloc_request);
  209. static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
  210. struct blk_mq_ctx *ctx, struct request *rq)
  211. {
  212. const int tag = rq->tag;
  213. struct request_queue *q = rq->q;
  214. if (rq->cmd_flags & REQ_MQ_INFLIGHT)
  215. atomic_dec(&hctx->nr_active);
  216. rq->cmd_flags = 0;
  217. clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
  218. blk_mq_put_tag(hctx, tag, &ctx->last_tag);
  219. blk_mq_queue_exit(q);
  220. }
  221. void blk_mq_free_request(struct request *rq)
  222. {
  223. struct blk_mq_ctx *ctx = rq->mq_ctx;
  224. struct blk_mq_hw_ctx *hctx;
  225. struct request_queue *q = rq->q;
  226. ctx->rq_completed[rq_is_sync(rq)]++;
  227. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  228. __blk_mq_free_request(hctx, ctx, rq);
  229. }
  230. /*
  231. * Clone all relevant state from a request that has been put on hold in
  232. * the flush state machine into the preallocated flush request that hangs
  233. * off the request queue.
  234. *
  235. * For a driver the flush request should be invisible, that's why we are
  236. * impersonating the original request here.
  237. */
  238. void blk_mq_clone_flush_request(struct request *flush_rq,
  239. struct request *orig_rq)
  240. {
  241. struct blk_mq_hw_ctx *hctx =
  242. orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
  243. flush_rq->mq_ctx = orig_rq->mq_ctx;
  244. flush_rq->tag = orig_rq->tag;
  245. memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
  246. hctx->cmd_size);
  247. }
  248. inline void __blk_mq_end_io(struct request *rq, int error)
  249. {
  250. blk_account_io_done(rq);
  251. if (rq->end_io) {
  252. rq->end_io(rq, error);
  253. } else {
  254. if (unlikely(blk_bidi_rq(rq)))
  255. blk_mq_free_request(rq->next_rq);
  256. blk_mq_free_request(rq);
  257. }
  258. }
  259. EXPORT_SYMBOL(__blk_mq_end_io);
  260. void blk_mq_end_io(struct request *rq, int error)
  261. {
  262. if (blk_update_request(rq, error, blk_rq_bytes(rq)))
  263. BUG();
  264. __blk_mq_end_io(rq, error);
  265. }
  266. EXPORT_SYMBOL(blk_mq_end_io);
  267. static void __blk_mq_complete_request_remote(void *data)
  268. {
  269. struct request *rq = data;
  270. rq->q->softirq_done_fn(rq);
  271. }
  272. static void blk_mq_ipi_complete_request(struct request *rq)
  273. {
  274. struct blk_mq_ctx *ctx = rq->mq_ctx;
  275. bool shared = false;
  276. int cpu;
  277. if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
  278. rq->q->softirq_done_fn(rq);
  279. return;
  280. }
  281. cpu = get_cpu();
  282. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
  283. shared = cpus_share_cache(cpu, ctx->cpu);
  284. if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
  285. rq->csd.func = __blk_mq_complete_request_remote;
  286. rq->csd.info = rq;
  287. rq->csd.flags = 0;
  288. smp_call_function_single_async(ctx->cpu, &rq->csd);
  289. } else {
  290. rq->q->softirq_done_fn(rq);
  291. }
  292. put_cpu();
  293. }
  294. void __blk_mq_complete_request(struct request *rq)
  295. {
  296. struct request_queue *q = rq->q;
  297. if (!q->softirq_done_fn)
  298. blk_mq_end_io(rq, rq->errors);
  299. else
  300. blk_mq_ipi_complete_request(rq);
  301. }
  302. /**
  303. * blk_mq_complete_request - end I/O on a request
  304. * @rq: the request being processed
  305. *
  306. * Description:
  307. * Ends all I/O on a request. It does not handle partial completions.
  308. * The actual completion happens out-of-order, through a IPI handler.
  309. **/
  310. void blk_mq_complete_request(struct request *rq)
  311. {
  312. struct request_queue *q = rq->q;
  313. if (unlikely(blk_should_fake_timeout(q)))
  314. return;
  315. if (!blk_mark_rq_complete(rq))
  316. __blk_mq_complete_request(rq);
  317. }
  318. EXPORT_SYMBOL(blk_mq_complete_request);
  319. static void blk_mq_start_request(struct request *rq, bool last)
  320. {
  321. struct request_queue *q = rq->q;
  322. trace_block_rq_issue(q, rq);
  323. rq->resid_len = blk_rq_bytes(rq);
  324. if (unlikely(blk_bidi_rq(rq)))
  325. rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
  326. blk_add_timer(rq);
  327. /*
  328. * Ensure that ->deadline is visible before set the started
  329. * flag and clear the completed flag.
  330. */
  331. smp_mb__before_atomic();
  332. /*
  333. * Mark us as started and clear complete. Complete might have been
  334. * set if requeue raced with timeout, which then marked it as
  335. * complete. So be sure to clear complete again when we start
  336. * the request, otherwise we'll ignore the completion event.
  337. */
  338. if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
  339. set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
  340. if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
  341. clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
  342. if (q->dma_drain_size && blk_rq_bytes(rq)) {
  343. /*
  344. * Make sure space for the drain appears. We know we can do
  345. * this because max_hw_segments has been adjusted to be one
  346. * fewer than the device can handle.
  347. */
  348. rq->nr_phys_segments++;
  349. }
  350. /*
  351. * Flag the last request in the series so that drivers know when IO
  352. * should be kicked off, if they don't do it on a per-request basis.
  353. *
  354. * Note: the flag isn't the only condition drivers should do kick off.
  355. * If drive is busy, the last request might not have the bit set.
  356. */
  357. if (last)
  358. rq->cmd_flags |= REQ_END;
  359. }
  360. static void __blk_mq_requeue_request(struct request *rq)
  361. {
  362. struct request_queue *q = rq->q;
  363. trace_block_rq_requeue(q, rq);
  364. clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
  365. rq->cmd_flags &= ~REQ_END;
  366. if (q->dma_drain_size && blk_rq_bytes(rq))
  367. rq->nr_phys_segments--;
  368. }
  369. void blk_mq_requeue_request(struct request *rq)
  370. {
  371. __blk_mq_requeue_request(rq);
  372. blk_clear_rq_complete(rq);
  373. BUG_ON(blk_queued_rq(rq));
  374. blk_mq_add_to_requeue_list(rq, true);
  375. }
  376. EXPORT_SYMBOL(blk_mq_requeue_request);
  377. static void blk_mq_requeue_work(struct work_struct *work)
  378. {
  379. struct request_queue *q =
  380. container_of(work, struct request_queue, requeue_work);
  381. LIST_HEAD(rq_list);
  382. struct request *rq, *next;
  383. unsigned long flags;
  384. spin_lock_irqsave(&q->requeue_lock, flags);
  385. list_splice_init(&q->requeue_list, &rq_list);
  386. spin_unlock_irqrestore(&q->requeue_lock, flags);
  387. list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
  388. if (!(rq->cmd_flags & REQ_SOFTBARRIER))
  389. continue;
  390. rq->cmd_flags &= ~REQ_SOFTBARRIER;
  391. list_del_init(&rq->queuelist);
  392. blk_mq_insert_request(rq, true, false, false);
  393. }
  394. while (!list_empty(&rq_list)) {
  395. rq = list_entry(rq_list.next, struct request, queuelist);
  396. list_del_init(&rq->queuelist);
  397. blk_mq_insert_request(rq, false, false, false);
  398. }
  399. /*
  400. * Use the start variant of queue running here, so that running
  401. * the requeue work will kick stopped queues.
  402. */
  403. blk_mq_start_hw_queues(q);
  404. }
  405. void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
  406. {
  407. struct request_queue *q = rq->q;
  408. unsigned long flags;
  409. /*
  410. * We abuse this flag that is otherwise used by the I/O scheduler to
  411. * request head insertation from the workqueue.
  412. */
  413. BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
  414. spin_lock_irqsave(&q->requeue_lock, flags);
  415. if (at_head) {
  416. rq->cmd_flags |= REQ_SOFTBARRIER;
  417. list_add(&rq->queuelist, &q->requeue_list);
  418. } else {
  419. list_add_tail(&rq->queuelist, &q->requeue_list);
  420. }
  421. spin_unlock_irqrestore(&q->requeue_lock, flags);
  422. }
  423. EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
  424. void blk_mq_kick_requeue_list(struct request_queue *q)
  425. {
  426. kblockd_schedule_work(&q->requeue_work);
  427. }
  428. EXPORT_SYMBOL(blk_mq_kick_requeue_list);
  429. static inline bool is_flush_request(struct request *rq, unsigned int tag)
  430. {
  431. return ((rq->cmd_flags & REQ_FLUSH_SEQ) &&
  432. rq->q->flush_rq->tag == tag);
  433. }
  434. struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
  435. {
  436. struct request *rq = tags->rqs[tag];
  437. if (!is_flush_request(rq, tag))
  438. return rq;
  439. return rq->q->flush_rq;
  440. }
  441. EXPORT_SYMBOL(blk_mq_tag_to_rq);
  442. struct blk_mq_timeout_data {
  443. struct blk_mq_hw_ctx *hctx;
  444. unsigned long *next;
  445. unsigned int *next_set;
  446. };
  447. static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
  448. {
  449. struct blk_mq_timeout_data *data = __data;
  450. struct blk_mq_hw_ctx *hctx = data->hctx;
  451. unsigned int tag;
  452. /* It may not be in flight yet (this is where
  453. * the REQ_ATOMIC_STARTED flag comes in). The requests are
  454. * statically allocated, so we know it's always safe to access the
  455. * memory associated with a bit offset into ->rqs[].
  456. */
  457. tag = 0;
  458. do {
  459. struct request *rq;
  460. tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
  461. if (tag >= hctx->tags->nr_tags)
  462. break;
  463. rq = blk_mq_tag_to_rq(hctx->tags, tag++);
  464. if (rq->q != hctx->queue)
  465. continue;
  466. if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
  467. continue;
  468. blk_rq_check_expired(rq, data->next, data->next_set);
  469. } while (1);
  470. }
  471. static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
  472. unsigned long *next,
  473. unsigned int *next_set)
  474. {
  475. struct blk_mq_timeout_data data = {
  476. .hctx = hctx,
  477. .next = next,
  478. .next_set = next_set,
  479. };
  480. /*
  481. * Ask the tagging code to iterate busy requests, so we can
  482. * check them for timeout.
  483. */
  484. blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
  485. }
  486. static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
  487. {
  488. struct request_queue *q = rq->q;
  489. /*
  490. * We know that complete is set at this point. If STARTED isn't set
  491. * anymore, then the request isn't active and the "timeout" should
  492. * just be ignored. This can happen due to the bitflag ordering.
  493. * Timeout first checks if STARTED is set, and if it is, assumes
  494. * the request is active. But if we race with completion, then
  495. * we both flags will get cleared. So check here again, and ignore
  496. * a timeout event with a request that isn't active.
  497. */
  498. if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
  499. return BLK_EH_NOT_HANDLED;
  500. if (!q->mq_ops->timeout)
  501. return BLK_EH_RESET_TIMER;
  502. return q->mq_ops->timeout(rq);
  503. }
  504. static void blk_mq_rq_timer(unsigned long data)
  505. {
  506. struct request_queue *q = (struct request_queue *) data;
  507. struct blk_mq_hw_ctx *hctx;
  508. unsigned long next = 0;
  509. int i, next_set = 0;
  510. queue_for_each_hw_ctx(q, hctx, i) {
  511. /*
  512. * If not software queues are currently mapped to this
  513. * hardware queue, there's nothing to check
  514. */
  515. if (!hctx->nr_ctx || !hctx->tags)
  516. continue;
  517. blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
  518. }
  519. if (next_set) {
  520. next = blk_rq_timeout(round_jiffies_up(next));
  521. mod_timer(&q->timeout, next);
  522. } else {
  523. queue_for_each_hw_ctx(q, hctx, i)
  524. blk_mq_tag_idle(hctx);
  525. }
  526. }
  527. /*
  528. * Reverse check our software queue for entries that we could potentially
  529. * merge with. Currently includes a hand-wavy stop count of 8, to not spend
  530. * too much time checking for merges.
  531. */
  532. static bool blk_mq_attempt_merge(struct request_queue *q,
  533. struct blk_mq_ctx *ctx, struct bio *bio)
  534. {
  535. struct request *rq;
  536. int checked = 8;
  537. list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
  538. int el_ret;
  539. if (!checked--)
  540. break;
  541. if (!blk_rq_merge_ok(rq, bio))
  542. continue;
  543. el_ret = blk_try_merge(rq, bio);
  544. if (el_ret == ELEVATOR_BACK_MERGE) {
  545. if (bio_attempt_back_merge(q, rq, bio)) {
  546. ctx->rq_merged++;
  547. return true;
  548. }
  549. break;
  550. } else if (el_ret == ELEVATOR_FRONT_MERGE) {
  551. if (bio_attempt_front_merge(q, rq, bio)) {
  552. ctx->rq_merged++;
  553. return true;
  554. }
  555. break;
  556. }
  557. }
  558. return false;
  559. }
  560. /*
  561. * Process software queues that have been marked busy, splicing them
  562. * to the for-dispatch
  563. */
  564. static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
  565. {
  566. struct blk_mq_ctx *ctx;
  567. int i;
  568. for (i = 0; i < hctx->ctx_map.map_size; i++) {
  569. struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
  570. unsigned int off, bit;
  571. if (!bm->word)
  572. continue;
  573. bit = 0;
  574. off = i * hctx->ctx_map.bits_per_word;
  575. do {
  576. bit = find_next_bit(&bm->word, bm->depth, bit);
  577. if (bit >= bm->depth)
  578. break;
  579. ctx = hctx->ctxs[bit + off];
  580. clear_bit(bit, &bm->word);
  581. spin_lock(&ctx->lock);
  582. list_splice_tail_init(&ctx->rq_list, list);
  583. spin_unlock(&ctx->lock);
  584. bit++;
  585. } while (1);
  586. }
  587. }
  588. /*
  589. * Run this hardware queue, pulling any software queues mapped to it in.
  590. * Note that this function currently has various problems around ordering
  591. * of IO. In particular, we'd like FIFO behaviour on handling existing
  592. * items on the hctx->dispatch list. Ignore that for now.
  593. */
  594. static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
  595. {
  596. struct request_queue *q = hctx->queue;
  597. struct request *rq;
  598. LIST_HEAD(rq_list);
  599. int queued;
  600. WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
  601. if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
  602. return;
  603. hctx->run++;
  604. /*
  605. * Touch any software queue that has pending entries.
  606. */
  607. flush_busy_ctxs(hctx, &rq_list);
  608. /*
  609. * If we have previous entries on our dispatch list, grab them
  610. * and stuff them at the front for more fair dispatch.
  611. */
  612. if (!list_empty_careful(&hctx->dispatch)) {
  613. spin_lock(&hctx->lock);
  614. if (!list_empty(&hctx->dispatch))
  615. list_splice_init(&hctx->dispatch, &rq_list);
  616. spin_unlock(&hctx->lock);
  617. }
  618. /*
  619. * Now process all the entries, sending them to the driver.
  620. */
  621. queued = 0;
  622. while (!list_empty(&rq_list)) {
  623. int ret;
  624. rq = list_first_entry(&rq_list, struct request, queuelist);
  625. list_del_init(&rq->queuelist);
  626. blk_mq_start_request(rq, list_empty(&rq_list));
  627. ret = q->mq_ops->queue_rq(hctx, rq);
  628. switch (ret) {
  629. case BLK_MQ_RQ_QUEUE_OK:
  630. queued++;
  631. continue;
  632. case BLK_MQ_RQ_QUEUE_BUSY:
  633. list_add(&rq->queuelist, &rq_list);
  634. __blk_mq_requeue_request(rq);
  635. break;
  636. default:
  637. pr_err("blk-mq: bad return on queue: %d\n", ret);
  638. case BLK_MQ_RQ_QUEUE_ERROR:
  639. rq->errors = -EIO;
  640. blk_mq_end_io(rq, rq->errors);
  641. break;
  642. }
  643. if (ret == BLK_MQ_RQ_QUEUE_BUSY)
  644. break;
  645. }
  646. if (!queued)
  647. hctx->dispatched[0]++;
  648. else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
  649. hctx->dispatched[ilog2(queued) + 1]++;
  650. /*
  651. * Any items that need requeuing? Stuff them into hctx->dispatch,
  652. * that is where we will continue on next queue run.
  653. */
  654. if (!list_empty(&rq_list)) {
  655. spin_lock(&hctx->lock);
  656. list_splice(&rq_list, &hctx->dispatch);
  657. spin_unlock(&hctx->lock);
  658. }
  659. }
  660. /*
  661. * It'd be great if the workqueue API had a way to pass
  662. * in a mask and had some smarts for more clever placement.
  663. * For now we just round-robin here, switching for every
  664. * BLK_MQ_CPU_WORK_BATCH queued items.
  665. */
  666. static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
  667. {
  668. int cpu = hctx->next_cpu;
  669. if (--hctx->next_cpu_batch <= 0) {
  670. int next_cpu;
  671. next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
  672. if (next_cpu >= nr_cpu_ids)
  673. next_cpu = cpumask_first(hctx->cpumask);
  674. hctx->next_cpu = next_cpu;
  675. hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
  676. }
  677. return cpu;
  678. }
  679. void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  680. {
  681. if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
  682. return;
  683. if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
  684. __blk_mq_run_hw_queue(hctx);
  685. else if (hctx->queue->nr_hw_queues == 1)
  686. kblockd_schedule_delayed_work(&hctx->run_work, 0);
  687. else {
  688. unsigned int cpu;
  689. cpu = blk_mq_hctx_next_cpu(hctx);
  690. kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
  691. }
  692. }
  693. void blk_mq_run_queues(struct request_queue *q, bool async)
  694. {
  695. struct blk_mq_hw_ctx *hctx;
  696. int i;
  697. queue_for_each_hw_ctx(q, hctx, i) {
  698. if ((!blk_mq_hctx_has_pending(hctx) &&
  699. list_empty_careful(&hctx->dispatch)) ||
  700. test_bit(BLK_MQ_S_STOPPED, &hctx->state))
  701. continue;
  702. preempt_disable();
  703. blk_mq_run_hw_queue(hctx, async);
  704. preempt_enable();
  705. }
  706. }
  707. EXPORT_SYMBOL(blk_mq_run_queues);
  708. void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
  709. {
  710. cancel_delayed_work(&hctx->run_work);
  711. cancel_delayed_work(&hctx->delay_work);
  712. set_bit(BLK_MQ_S_STOPPED, &hctx->state);
  713. }
  714. EXPORT_SYMBOL(blk_mq_stop_hw_queue);
  715. void blk_mq_stop_hw_queues(struct request_queue *q)
  716. {
  717. struct blk_mq_hw_ctx *hctx;
  718. int i;
  719. queue_for_each_hw_ctx(q, hctx, i)
  720. blk_mq_stop_hw_queue(hctx);
  721. }
  722. EXPORT_SYMBOL(blk_mq_stop_hw_queues);
  723. void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
  724. {
  725. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  726. preempt_disable();
  727. blk_mq_run_hw_queue(hctx, false);
  728. preempt_enable();
  729. }
  730. EXPORT_SYMBOL(blk_mq_start_hw_queue);
  731. void blk_mq_start_hw_queues(struct request_queue *q)
  732. {
  733. struct blk_mq_hw_ctx *hctx;
  734. int i;
  735. queue_for_each_hw_ctx(q, hctx, i)
  736. blk_mq_start_hw_queue(hctx);
  737. }
  738. EXPORT_SYMBOL(blk_mq_start_hw_queues);
  739. void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
  740. {
  741. struct blk_mq_hw_ctx *hctx;
  742. int i;
  743. queue_for_each_hw_ctx(q, hctx, i) {
  744. if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
  745. continue;
  746. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  747. preempt_disable();
  748. blk_mq_run_hw_queue(hctx, async);
  749. preempt_enable();
  750. }
  751. }
  752. EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
  753. static void blk_mq_run_work_fn(struct work_struct *work)
  754. {
  755. struct blk_mq_hw_ctx *hctx;
  756. hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
  757. __blk_mq_run_hw_queue(hctx);
  758. }
  759. static void blk_mq_delay_work_fn(struct work_struct *work)
  760. {
  761. struct blk_mq_hw_ctx *hctx;
  762. hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
  763. if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
  764. __blk_mq_run_hw_queue(hctx);
  765. }
  766. void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
  767. {
  768. unsigned long tmo = msecs_to_jiffies(msecs);
  769. if (hctx->queue->nr_hw_queues == 1)
  770. kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
  771. else {
  772. unsigned int cpu;
  773. cpu = blk_mq_hctx_next_cpu(hctx);
  774. kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
  775. }
  776. }
  777. EXPORT_SYMBOL(blk_mq_delay_queue);
  778. static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
  779. struct request *rq, bool at_head)
  780. {
  781. struct blk_mq_ctx *ctx = rq->mq_ctx;
  782. trace_block_rq_insert(hctx->queue, rq);
  783. if (at_head)
  784. list_add(&rq->queuelist, &ctx->rq_list);
  785. else
  786. list_add_tail(&rq->queuelist, &ctx->rq_list);
  787. blk_mq_hctx_mark_pending(hctx, ctx);
  788. }
  789. void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
  790. bool async)
  791. {
  792. struct request_queue *q = rq->q;
  793. struct blk_mq_hw_ctx *hctx;
  794. struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
  795. current_ctx = blk_mq_get_ctx(q);
  796. if (!cpu_online(ctx->cpu))
  797. rq->mq_ctx = ctx = current_ctx;
  798. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  799. spin_lock(&ctx->lock);
  800. __blk_mq_insert_request(hctx, rq, at_head);
  801. spin_unlock(&ctx->lock);
  802. if (run_queue)
  803. blk_mq_run_hw_queue(hctx, async);
  804. blk_mq_put_ctx(current_ctx);
  805. }
  806. static void blk_mq_insert_requests(struct request_queue *q,
  807. struct blk_mq_ctx *ctx,
  808. struct list_head *list,
  809. int depth,
  810. bool from_schedule)
  811. {
  812. struct blk_mq_hw_ctx *hctx;
  813. struct blk_mq_ctx *current_ctx;
  814. trace_block_unplug(q, depth, !from_schedule);
  815. current_ctx = blk_mq_get_ctx(q);
  816. if (!cpu_online(ctx->cpu))
  817. ctx = current_ctx;
  818. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  819. /*
  820. * preemption doesn't flush plug list, so it's possible ctx->cpu is
  821. * offline now
  822. */
  823. spin_lock(&ctx->lock);
  824. while (!list_empty(list)) {
  825. struct request *rq;
  826. rq = list_first_entry(list, struct request, queuelist);
  827. list_del_init(&rq->queuelist);
  828. rq->mq_ctx = ctx;
  829. __blk_mq_insert_request(hctx, rq, false);
  830. }
  831. spin_unlock(&ctx->lock);
  832. blk_mq_run_hw_queue(hctx, from_schedule);
  833. blk_mq_put_ctx(current_ctx);
  834. }
  835. static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
  836. {
  837. struct request *rqa = container_of(a, struct request, queuelist);
  838. struct request *rqb = container_of(b, struct request, queuelist);
  839. return !(rqa->mq_ctx < rqb->mq_ctx ||
  840. (rqa->mq_ctx == rqb->mq_ctx &&
  841. blk_rq_pos(rqa) < blk_rq_pos(rqb)));
  842. }
  843. void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  844. {
  845. struct blk_mq_ctx *this_ctx;
  846. struct request_queue *this_q;
  847. struct request *rq;
  848. LIST_HEAD(list);
  849. LIST_HEAD(ctx_list);
  850. unsigned int depth;
  851. list_splice_init(&plug->mq_list, &list);
  852. list_sort(NULL, &list, plug_ctx_cmp);
  853. this_q = NULL;
  854. this_ctx = NULL;
  855. depth = 0;
  856. while (!list_empty(&list)) {
  857. rq = list_entry_rq(list.next);
  858. list_del_init(&rq->queuelist);
  859. BUG_ON(!rq->q);
  860. if (rq->mq_ctx != this_ctx) {
  861. if (this_ctx) {
  862. blk_mq_insert_requests(this_q, this_ctx,
  863. &ctx_list, depth,
  864. from_schedule);
  865. }
  866. this_ctx = rq->mq_ctx;
  867. this_q = rq->q;
  868. depth = 0;
  869. }
  870. depth++;
  871. list_add_tail(&rq->queuelist, &ctx_list);
  872. }
  873. /*
  874. * If 'this_ctx' is set, we know we have entries to complete
  875. * on 'ctx_list'. Do those.
  876. */
  877. if (this_ctx) {
  878. blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
  879. from_schedule);
  880. }
  881. }
  882. static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
  883. {
  884. init_request_from_bio(rq, bio);
  885. if (blk_do_io_stat(rq))
  886. blk_account_io_start(rq, 1);
  887. }
  888. static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
  889. {
  890. return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
  891. !blk_queue_nomerges(hctx->queue);
  892. }
  893. static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
  894. struct blk_mq_ctx *ctx,
  895. struct request *rq, struct bio *bio)
  896. {
  897. if (!hctx_allow_merges(hctx)) {
  898. blk_mq_bio_to_request(rq, bio);
  899. spin_lock(&ctx->lock);
  900. insert_rq:
  901. __blk_mq_insert_request(hctx, rq, false);
  902. spin_unlock(&ctx->lock);
  903. return false;
  904. } else {
  905. struct request_queue *q = hctx->queue;
  906. spin_lock(&ctx->lock);
  907. if (!blk_mq_attempt_merge(q, ctx, bio)) {
  908. blk_mq_bio_to_request(rq, bio);
  909. goto insert_rq;
  910. }
  911. spin_unlock(&ctx->lock);
  912. __blk_mq_free_request(hctx, ctx, rq);
  913. return true;
  914. }
  915. }
  916. struct blk_map_ctx {
  917. struct blk_mq_hw_ctx *hctx;
  918. struct blk_mq_ctx *ctx;
  919. };
  920. static struct request *blk_mq_map_request(struct request_queue *q,
  921. struct bio *bio,
  922. struct blk_map_ctx *data)
  923. {
  924. struct blk_mq_hw_ctx *hctx;
  925. struct blk_mq_ctx *ctx;
  926. struct request *rq;
  927. int rw = bio_data_dir(bio);
  928. struct blk_mq_alloc_data alloc_data;
  929. if (unlikely(blk_mq_queue_enter(q))) {
  930. bio_endio(bio, -EIO);
  931. return NULL;
  932. }
  933. ctx = blk_mq_get_ctx(q);
  934. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  935. if (rw_is_sync(bio->bi_rw))
  936. rw |= REQ_SYNC;
  937. trace_block_getrq(q, bio, rw);
  938. blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx,
  939. hctx);
  940. rq = __blk_mq_alloc_request(&alloc_data, rw);
  941. if (unlikely(!rq)) {
  942. __blk_mq_run_hw_queue(hctx);
  943. blk_mq_put_ctx(ctx);
  944. trace_block_sleeprq(q, bio, rw);
  945. ctx = blk_mq_get_ctx(q);
  946. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  947. blk_mq_set_alloc_data(&alloc_data, q,
  948. __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx);
  949. rq = __blk_mq_alloc_request(&alloc_data, rw);
  950. ctx = alloc_data.ctx;
  951. hctx = alloc_data.hctx;
  952. }
  953. hctx->queued++;
  954. data->hctx = hctx;
  955. data->ctx = ctx;
  956. return rq;
  957. }
  958. /*
  959. * Multiple hardware queue variant. This will not use per-process plugs,
  960. * but will attempt to bypass the hctx queueing if we can go straight to
  961. * hardware for SYNC IO.
  962. */
  963. static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
  964. {
  965. const int is_sync = rw_is_sync(bio->bi_rw);
  966. const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
  967. struct blk_map_ctx data;
  968. struct request *rq;
  969. blk_queue_bounce(q, &bio);
  970. if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
  971. bio_endio(bio, -EIO);
  972. return;
  973. }
  974. rq = blk_mq_map_request(q, bio, &data);
  975. if (unlikely(!rq))
  976. return;
  977. if (unlikely(is_flush_fua)) {
  978. blk_mq_bio_to_request(rq, bio);
  979. blk_insert_flush(rq);
  980. goto run_queue;
  981. }
  982. if (is_sync) {
  983. int ret;
  984. blk_mq_bio_to_request(rq, bio);
  985. blk_mq_start_request(rq, true);
  986. /*
  987. * For OK queue, we are done. For error, kill it. Any other
  988. * error (busy), just add it to our list as we previously
  989. * would have done
  990. */
  991. ret = q->mq_ops->queue_rq(data.hctx, rq);
  992. if (ret == BLK_MQ_RQ_QUEUE_OK)
  993. goto done;
  994. else {
  995. __blk_mq_requeue_request(rq);
  996. if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
  997. rq->errors = -EIO;
  998. blk_mq_end_io(rq, rq->errors);
  999. goto done;
  1000. }
  1001. }
  1002. }
  1003. if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
  1004. /*
  1005. * For a SYNC request, send it to the hardware immediately. For
  1006. * an ASYNC request, just ensure that we run it later on. The
  1007. * latter allows for merging opportunities and more efficient
  1008. * dispatching.
  1009. */
  1010. run_queue:
  1011. blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
  1012. }
  1013. done:
  1014. blk_mq_put_ctx(data.ctx);
  1015. }
  1016. /*
  1017. * Single hardware queue variant. This will attempt to use any per-process
  1018. * plug for merging and IO deferral.
  1019. */
  1020. static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
  1021. {
  1022. const int is_sync = rw_is_sync(bio->bi_rw);
  1023. const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
  1024. unsigned int use_plug, request_count = 0;
  1025. struct blk_map_ctx data;
  1026. struct request *rq;
  1027. /*
  1028. * If we have multiple hardware queues, just go directly to
  1029. * one of those for sync IO.
  1030. */
  1031. use_plug = !is_flush_fua && !is_sync;
  1032. blk_queue_bounce(q, &bio);
  1033. if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
  1034. bio_endio(bio, -EIO);
  1035. return;
  1036. }
  1037. if (use_plug && !blk_queue_nomerges(q) &&
  1038. blk_attempt_plug_merge(q, bio, &request_count))
  1039. return;
  1040. rq = blk_mq_map_request(q, bio, &data);
  1041. if (unlikely(!rq))
  1042. return;
  1043. if (unlikely(is_flush_fua)) {
  1044. blk_mq_bio_to_request(rq, bio);
  1045. blk_insert_flush(rq);
  1046. goto run_queue;
  1047. }
  1048. /*
  1049. * A task plug currently exists. Since this is completely lockless,
  1050. * utilize that to temporarily store requests until the task is
  1051. * either done or scheduled away.
  1052. */
  1053. if (use_plug) {
  1054. struct blk_plug *plug = current->plug;
  1055. if (plug) {
  1056. blk_mq_bio_to_request(rq, bio);
  1057. if (list_empty(&plug->mq_list))
  1058. trace_block_plug(q);
  1059. else if (request_count >= BLK_MAX_REQUEST_COUNT) {
  1060. blk_flush_plug_list(plug, false);
  1061. trace_block_plug(q);
  1062. }
  1063. list_add_tail(&rq->queuelist, &plug->mq_list);
  1064. blk_mq_put_ctx(data.ctx);
  1065. return;
  1066. }
  1067. }
  1068. if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
  1069. /*
  1070. * For a SYNC request, send it to the hardware immediately. For
  1071. * an ASYNC request, just ensure that we run it later on. The
  1072. * latter allows for merging opportunities and more efficient
  1073. * dispatching.
  1074. */
  1075. run_queue:
  1076. blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
  1077. }
  1078. blk_mq_put_ctx(data.ctx);
  1079. }
  1080. /*
  1081. * Default mapping to a software queue, since we use one per CPU.
  1082. */
  1083. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
  1084. {
  1085. return q->queue_hw_ctx[q->mq_map[cpu]];
  1086. }
  1087. EXPORT_SYMBOL(blk_mq_map_queue);
  1088. static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
  1089. struct blk_mq_tags *tags, unsigned int hctx_idx)
  1090. {
  1091. struct page *page;
  1092. if (tags->rqs && set->ops->exit_request) {
  1093. int i;
  1094. for (i = 0; i < tags->nr_tags; i++) {
  1095. if (!tags->rqs[i])
  1096. continue;
  1097. set->ops->exit_request(set->driver_data, tags->rqs[i],
  1098. hctx_idx, i);
  1099. tags->rqs[i] = NULL;
  1100. }
  1101. }
  1102. while (!list_empty(&tags->page_list)) {
  1103. page = list_first_entry(&tags->page_list, struct page, lru);
  1104. list_del_init(&page->lru);
  1105. __free_pages(page, page->private);
  1106. }
  1107. kfree(tags->rqs);
  1108. blk_mq_free_tags(tags);
  1109. }
  1110. static size_t order_to_size(unsigned int order)
  1111. {
  1112. return (size_t)PAGE_SIZE << order;
  1113. }
  1114. static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
  1115. unsigned int hctx_idx)
  1116. {
  1117. struct blk_mq_tags *tags;
  1118. unsigned int i, j, entries_per_page, max_order = 4;
  1119. size_t rq_size, left;
  1120. tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
  1121. set->numa_node);
  1122. if (!tags)
  1123. return NULL;
  1124. INIT_LIST_HEAD(&tags->page_list);
  1125. tags->rqs = kzalloc_node(set->queue_depth * sizeof(struct request *),
  1126. GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
  1127. set->numa_node);
  1128. if (!tags->rqs) {
  1129. blk_mq_free_tags(tags);
  1130. return NULL;
  1131. }
  1132. /*
  1133. * rq_size is the size of the request plus driver payload, rounded
  1134. * to the cacheline size
  1135. */
  1136. rq_size = round_up(sizeof(struct request) + set->cmd_size,
  1137. cache_line_size());
  1138. left = rq_size * set->queue_depth;
  1139. for (i = 0; i < set->queue_depth; ) {
  1140. int this_order = max_order;
  1141. struct page *page;
  1142. int to_do;
  1143. void *p;
  1144. while (left < order_to_size(this_order - 1) && this_order)
  1145. this_order--;
  1146. do {
  1147. page = alloc_pages_node(set->numa_node,
  1148. GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
  1149. this_order);
  1150. if (page)
  1151. break;
  1152. if (!this_order--)
  1153. break;
  1154. if (order_to_size(this_order) < rq_size)
  1155. break;
  1156. } while (1);
  1157. if (!page)
  1158. goto fail;
  1159. page->private = this_order;
  1160. list_add_tail(&page->lru, &tags->page_list);
  1161. p = page_address(page);
  1162. entries_per_page = order_to_size(this_order) / rq_size;
  1163. to_do = min(entries_per_page, set->queue_depth - i);
  1164. left -= to_do * rq_size;
  1165. for (j = 0; j < to_do; j++) {
  1166. tags->rqs[i] = p;
  1167. tags->rqs[i]->atomic_flags = 0;
  1168. tags->rqs[i]->cmd_flags = 0;
  1169. if (set->ops->init_request) {
  1170. if (set->ops->init_request(set->driver_data,
  1171. tags->rqs[i], hctx_idx, i,
  1172. set->numa_node)) {
  1173. tags->rqs[i] = NULL;
  1174. goto fail;
  1175. }
  1176. }
  1177. p += rq_size;
  1178. i++;
  1179. }
  1180. }
  1181. return tags;
  1182. fail:
  1183. blk_mq_free_rq_map(set, tags, hctx_idx);
  1184. return NULL;
  1185. }
  1186. static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
  1187. {
  1188. kfree(bitmap->map);
  1189. }
  1190. static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
  1191. {
  1192. unsigned int bpw = 8, total, num_maps, i;
  1193. bitmap->bits_per_word = bpw;
  1194. num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
  1195. bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
  1196. GFP_KERNEL, node);
  1197. if (!bitmap->map)
  1198. return -ENOMEM;
  1199. bitmap->map_size = num_maps;
  1200. total = nr_cpu_ids;
  1201. for (i = 0; i < num_maps; i++) {
  1202. bitmap->map[i].depth = min(total, bitmap->bits_per_word);
  1203. total -= bitmap->map[i].depth;
  1204. }
  1205. return 0;
  1206. }
  1207. static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
  1208. {
  1209. struct request_queue *q = hctx->queue;
  1210. struct blk_mq_ctx *ctx;
  1211. LIST_HEAD(tmp);
  1212. /*
  1213. * Move ctx entries to new CPU, if this one is going away.
  1214. */
  1215. ctx = __blk_mq_get_ctx(q, cpu);
  1216. spin_lock(&ctx->lock);
  1217. if (!list_empty(&ctx->rq_list)) {
  1218. list_splice_init(&ctx->rq_list, &tmp);
  1219. blk_mq_hctx_clear_pending(hctx, ctx);
  1220. }
  1221. spin_unlock(&ctx->lock);
  1222. if (list_empty(&tmp))
  1223. return NOTIFY_OK;
  1224. ctx = blk_mq_get_ctx(q);
  1225. spin_lock(&ctx->lock);
  1226. while (!list_empty(&tmp)) {
  1227. struct request *rq;
  1228. rq = list_first_entry(&tmp, struct request, queuelist);
  1229. rq->mq_ctx = ctx;
  1230. list_move_tail(&rq->queuelist, &ctx->rq_list);
  1231. }
  1232. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  1233. blk_mq_hctx_mark_pending(hctx, ctx);
  1234. spin_unlock(&ctx->lock);
  1235. blk_mq_run_hw_queue(hctx, true);
  1236. blk_mq_put_ctx(ctx);
  1237. return NOTIFY_OK;
  1238. }
  1239. static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
  1240. {
  1241. struct request_queue *q = hctx->queue;
  1242. struct blk_mq_tag_set *set = q->tag_set;
  1243. if (set->tags[hctx->queue_num])
  1244. return NOTIFY_OK;
  1245. set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
  1246. if (!set->tags[hctx->queue_num])
  1247. return NOTIFY_STOP;
  1248. hctx->tags = set->tags[hctx->queue_num];
  1249. return NOTIFY_OK;
  1250. }
  1251. static int blk_mq_hctx_notify(void *data, unsigned long action,
  1252. unsigned int cpu)
  1253. {
  1254. struct blk_mq_hw_ctx *hctx = data;
  1255. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
  1256. return blk_mq_hctx_cpu_offline(hctx, cpu);
  1257. else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
  1258. return blk_mq_hctx_cpu_online(hctx, cpu);
  1259. return NOTIFY_OK;
  1260. }
  1261. static void blk_mq_exit_hw_queues(struct request_queue *q,
  1262. struct blk_mq_tag_set *set, int nr_queue)
  1263. {
  1264. struct blk_mq_hw_ctx *hctx;
  1265. unsigned int i;
  1266. queue_for_each_hw_ctx(q, hctx, i) {
  1267. if (i == nr_queue)
  1268. break;
  1269. blk_mq_tag_idle(hctx);
  1270. if (set->ops->exit_hctx)
  1271. set->ops->exit_hctx(hctx, i);
  1272. blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
  1273. kfree(hctx->ctxs);
  1274. blk_mq_free_bitmap(&hctx->ctx_map);
  1275. }
  1276. }
  1277. static void blk_mq_free_hw_queues(struct request_queue *q,
  1278. struct blk_mq_tag_set *set)
  1279. {
  1280. struct blk_mq_hw_ctx *hctx;
  1281. unsigned int i;
  1282. queue_for_each_hw_ctx(q, hctx, i) {
  1283. free_cpumask_var(hctx->cpumask);
  1284. kfree(hctx);
  1285. }
  1286. }
  1287. static int blk_mq_init_hw_queues(struct request_queue *q,
  1288. struct blk_mq_tag_set *set)
  1289. {
  1290. struct blk_mq_hw_ctx *hctx;
  1291. unsigned int i;
  1292. /*
  1293. * Initialize hardware queues
  1294. */
  1295. queue_for_each_hw_ctx(q, hctx, i) {
  1296. int node;
  1297. node = hctx->numa_node;
  1298. if (node == NUMA_NO_NODE)
  1299. node = hctx->numa_node = set->numa_node;
  1300. INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
  1301. INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
  1302. spin_lock_init(&hctx->lock);
  1303. INIT_LIST_HEAD(&hctx->dispatch);
  1304. hctx->queue = q;
  1305. hctx->queue_num = i;
  1306. hctx->flags = set->flags;
  1307. hctx->cmd_size = set->cmd_size;
  1308. blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
  1309. blk_mq_hctx_notify, hctx);
  1310. blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
  1311. hctx->tags = set->tags[i];
  1312. /*
  1313. * Allocate space for all possible cpus to avoid allocation at
  1314. * runtime
  1315. */
  1316. hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
  1317. GFP_KERNEL, node);
  1318. if (!hctx->ctxs)
  1319. break;
  1320. if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
  1321. break;
  1322. hctx->nr_ctx = 0;
  1323. if (set->ops->init_hctx &&
  1324. set->ops->init_hctx(hctx, set->driver_data, i))
  1325. break;
  1326. }
  1327. if (i == q->nr_hw_queues)
  1328. return 0;
  1329. /*
  1330. * Init failed
  1331. */
  1332. blk_mq_exit_hw_queues(q, set, i);
  1333. return 1;
  1334. }
  1335. static void blk_mq_init_cpu_queues(struct request_queue *q,
  1336. unsigned int nr_hw_queues)
  1337. {
  1338. unsigned int i;
  1339. for_each_possible_cpu(i) {
  1340. struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
  1341. struct blk_mq_hw_ctx *hctx;
  1342. memset(__ctx, 0, sizeof(*__ctx));
  1343. __ctx->cpu = i;
  1344. spin_lock_init(&__ctx->lock);
  1345. INIT_LIST_HEAD(&__ctx->rq_list);
  1346. __ctx->queue = q;
  1347. /* If the cpu isn't online, the cpu is mapped to first hctx */
  1348. if (!cpu_online(i))
  1349. continue;
  1350. hctx = q->mq_ops->map_queue(q, i);
  1351. cpumask_set_cpu(i, hctx->cpumask);
  1352. hctx->nr_ctx++;
  1353. /*
  1354. * Set local node, IFF we have more than one hw queue. If
  1355. * not, we remain on the home node of the device
  1356. */
  1357. if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
  1358. hctx->numa_node = cpu_to_node(i);
  1359. }
  1360. }
  1361. static void blk_mq_map_swqueue(struct request_queue *q)
  1362. {
  1363. unsigned int i;
  1364. struct blk_mq_hw_ctx *hctx;
  1365. struct blk_mq_ctx *ctx;
  1366. queue_for_each_hw_ctx(q, hctx, i) {
  1367. cpumask_clear(hctx->cpumask);
  1368. hctx->nr_ctx = 0;
  1369. }
  1370. /*
  1371. * Map software to hardware queues
  1372. */
  1373. queue_for_each_ctx(q, ctx, i) {
  1374. /* If the cpu isn't online, the cpu is mapped to first hctx */
  1375. if (!cpu_online(i))
  1376. continue;
  1377. hctx = q->mq_ops->map_queue(q, i);
  1378. cpumask_set_cpu(i, hctx->cpumask);
  1379. ctx->index_hw = hctx->nr_ctx;
  1380. hctx->ctxs[hctx->nr_ctx++] = ctx;
  1381. }
  1382. queue_for_each_hw_ctx(q, hctx, i) {
  1383. /*
  1384. * If no software queues are mapped to this hardware queue,
  1385. * disable it and free the request entries.
  1386. */
  1387. if (!hctx->nr_ctx) {
  1388. struct blk_mq_tag_set *set = q->tag_set;
  1389. if (set->tags[i]) {
  1390. blk_mq_free_rq_map(set, set->tags[i], i);
  1391. set->tags[i] = NULL;
  1392. hctx->tags = NULL;
  1393. }
  1394. continue;
  1395. }
  1396. /*
  1397. * Initialize batch roundrobin counts
  1398. */
  1399. hctx->next_cpu = cpumask_first(hctx->cpumask);
  1400. hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
  1401. }
  1402. }
  1403. static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
  1404. {
  1405. struct blk_mq_hw_ctx *hctx;
  1406. struct request_queue *q;
  1407. bool shared;
  1408. int i;
  1409. if (set->tag_list.next == set->tag_list.prev)
  1410. shared = false;
  1411. else
  1412. shared = true;
  1413. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  1414. blk_mq_freeze_queue(q);
  1415. queue_for_each_hw_ctx(q, hctx, i) {
  1416. if (shared)
  1417. hctx->flags |= BLK_MQ_F_TAG_SHARED;
  1418. else
  1419. hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
  1420. }
  1421. blk_mq_unfreeze_queue(q);
  1422. }
  1423. }
  1424. static void blk_mq_del_queue_tag_set(struct request_queue *q)
  1425. {
  1426. struct blk_mq_tag_set *set = q->tag_set;
  1427. mutex_lock(&set->tag_list_lock);
  1428. list_del_init(&q->tag_set_list);
  1429. blk_mq_update_tag_set_depth(set);
  1430. mutex_unlock(&set->tag_list_lock);
  1431. }
  1432. static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
  1433. struct request_queue *q)
  1434. {
  1435. q->tag_set = set;
  1436. mutex_lock(&set->tag_list_lock);
  1437. list_add_tail(&q->tag_set_list, &set->tag_list);
  1438. blk_mq_update_tag_set_depth(set);
  1439. mutex_unlock(&set->tag_list_lock);
  1440. }
  1441. struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
  1442. {
  1443. struct blk_mq_hw_ctx **hctxs;
  1444. struct blk_mq_ctx __percpu *ctx;
  1445. struct request_queue *q;
  1446. unsigned int *map;
  1447. int i;
  1448. ctx = alloc_percpu(struct blk_mq_ctx);
  1449. if (!ctx)
  1450. return ERR_PTR(-ENOMEM);
  1451. hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
  1452. set->numa_node);
  1453. if (!hctxs)
  1454. goto err_percpu;
  1455. map = blk_mq_make_queue_map(set);
  1456. if (!map)
  1457. goto err_map;
  1458. for (i = 0; i < set->nr_hw_queues; i++) {
  1459. int node = blk_mq_hw_queue_to_node(map, i);
  1460. hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
  1461. GFP_KERNEL, node);
  1462. if (!hctxs[i])
  1463. goto err_hctxs;
  1464. if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
  1465. goto err_hctxs;
  1466. atomic_set(&hctxs[i]->nr_active, 0);
  1467. hctxs[i]->numa_node = node;
  1468. hctxs[i]->queue_num = i;
  1469. }
  1470. q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
  1471. if (!q)
  1472. goto err_hctxs;
  1473. /*
  1474. * Init percpu_ref in atomic mode so that it's faster to shutdown.
  1475. * See blk_register_queue() for details.
  1476. */
  1477. if (percpu_ref_init(&q->mq_usage_counter, blk_mq_usage_counter_release,
  1478. PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
  1479. goto err_map;
  1480. setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
  1481. blk_queue_rq_timeout(q, 30000);
  1482. q->nr_queues = nr_cpu_ids;
  1483. q->nr_hw_queues = set->nr_hw_queues;
  1484. q->mq_map = map;
  1485. q->queue_ctx = ctx;
  1486. q->queue_hw_ctx = hctxs;
  1487. q->mq_ops = set->ops;
  1488. q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
  1489. if (!(set->flags & BLK_MQ_F_SG_MERGE))
  1490. q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
  1491. q->sg_reserved_size = INT_MAX;
  1492. INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
  1493. INIT_LIST_HEAD(&q->requeue_list);
  1494. spin_lock_init(&q->requeue_lock);
  1495. if (q->nr_hw_queues > 1)
  1496. blk_queue_make_request(q, blk_mq_make_request);
  1497. else
  1498. blk_queue_make_request(q, blk_sq_make_request);
  1499. blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
  1500. if (set->timeout)
  1501. blk_queue_rq_timeout(q, set->timeout);
  1502. /*
  1503. * Do this after blk_queue_make_request() overrides it...
  1504. */
  1505. q->nr_requests = set->queue_depth;
  1506. if (set->ops->complete)
  1507. blk_queue_softirq_done(q, set->ops->complete);
  1508. blk_mq_init_flush(q);
  1509. blk_mq_init_cpu_queues(q, set->nr_hw_queues);
  1510. q->flush_rq = kzalloc(round_up(sizeof(struct request) +
  1511. set->cmd_size, cache_line_size()),
  1512. GFP_KERNEL);
  1513. if (!q->flush_rq)
  1514. goto err_hw;
  1515. if (blk_mq_init_hw_queues(q, set))
  1516. goto err_flush_rq;
  1517. mutex_lock(&all_q_mutex);
  1518. list_add_tail(&q->all_q_node, &all_q_list);
  1519. mutex_unlock(&all_q_mutex);
  1520. blk_mq_add_queue_tag_set(set, q);
  1521. blk_mq_map_swqueue(q);
  1522. return q;
  1523. err_flush_rq:
  1524. kfree(q->flush_rq);
  1525. err_hw:
  1526. blk_cleanup_queue(q);
  1527. err_hctxs:
  1528. kfree(map);
  1529. for (i = 0; i < set->nr_hw_queues; i++) {
  1530. if (!hctxs[i])
  1531. break;
  1532. free_cpumask_var(hctxs[i]->cpumask);
  1533. kfree(hctxs[i]);
  1534. }
  1535. err_map:
  1536. kfree(hctxs);
  1537. err_percpu:
  1538. free_percpu(ctx);
  1539. return ERR_PTR(-ENOMEM);
  1540. }
  1541. EXPORT_SYMBOL(blk_mq_init_queue);
  1542. void blk_mq_free_queue(struct request_queue *q)
  1543. {
  1544. struct blk_mq_tag_set *set = q->tag_set;
  1545. blk_mq_del_queue_tag_set(q);
  1546. blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
  1547. blk_mq_free_hw_queues(q, set);
  1548. percpu_ref_exit(&q->mq_usage_counter);
  1549. free_percpu(q->queue_ctx);
  1550. kfree(q->queue_hw_ctx);
  1551. kfree(q->mq_map);
  1552. q->queue_ctx = NULL;
  1553. q->queue_hw_ctx = NULL;
  1554. q->mq_map = NULL;
  1555. mutex_lock(&all_q_mutex);
  1556. list_del_init(&q->all_q_node);
  1557. mutex_unlock(&all_q_mutex);
  1558. }
  1559. /* Basically redo blk_mq_init_queue with queue frozen */
  1560. static void blk_mq_queue_reinit(struct request_queue *q)
  1561. {
  1562. blk_mq_freeze_queue(q);
  1563. blk_mq_sysfs_unregister(q);
  1564. blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
  1565. /*
  1566. * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
  1567. * we should change hctx numa_node according to new topology (this
  1568. * involves free and re-allocate memory, worthy doing?)
  1569. */
  1570. blk_mq_map_swqueue(q);
  1571. blk_mq_sysfs_register(q);
  1572. blk_mq_unfreeze_queue(q);
  1573. }
  1574. static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
  1575. unsigned long action, void *hcpu)
  1576. {
  1577. struct request_queue *q;
  1578. /*
  1579. * Before new mappings are established, hotadded cpu might already
  1580. * start handling requests. This doesn't break anything as we map
  1581. * offline CPUs to first hardware queue. We will re-init the queue
  1582. * below to get optimal settings.
  1583. */
  1584. if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
  1585. action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
  1586. return NOTIFY_OK;
  1587. mutex_lock(&all_q_mutex);
  1588. list_for_each_entry(q, &all_q_list, all_q_node)
  1589. blk_mq_queue_reinit(q);
  1590. mutex_unlock(&all_q_mutex);
  1591. return NOTIFY_OK;
  1592. }
  1593. static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
  1594. {
  1595. int i;
  1596. for (i = 0; i < set->nr_hw_queues; i++) {
  1597. set->tags[i] = blk_mq_init_rq_map(set, i);
  1598. if (!set->tags[i])
  1599. goto out_unwind;
  1600. }
  1601. return 0;
  1602. out_unwind:
  1603. while (--i >= 0)
  1604. blk_mq_free_rq_map(set, set->tags[i], i);
  1605. return -ENOMEM;
  1606. }
  1607. /*
  1608. * Allocate the request maps associated with this tag_set. Note that this
  1609. * may reduce the depth asked for, if memory is tight. set->queue_depth
  1610. * will be updated to reflect the allocated depth.
  1611. */
  1612. static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
  1613. {
  1614. unsigned int depth;
  1615. int err;
  1616. depth = set->queue_depth;
  1617. do {
  1618. err = __blk_mq_alloc_rq_maps(set);
  1619. if (!err)
  1620. break;
  1621. set->queue_depth >>= 1;
  1622. if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
  1623. err = -ENOMEM;
  1624. break;
  1625. }
  1626. } while (set->queue_depth);
  1627. if (!set->queue_depth || err) {
  1628. pr_err("blk-mq: failed to allocate request map\n");
  1629. return -ENOMEM;
  1630. }
  1631. if (depth != set->queue_depth)
  1632. pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
  1633. depth, set->queue_depth);
  1634. return 0;
  1635. }
  1636. /*
  1637. * Alloc a tag set to be associated with one or more request queues.
  1638. * May fail with EINVAL for various error conditions. May adjust the
  1639. * requested depth down, if if it too large. In that case, the set
  1640. * value will be stored in set->queue_depth.
  1641. */
  1642. int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
  1643. {
  1644. if (!set->nr_hw_queues)
  1645. return -EINVAL;
  1646. if (!set->queue_depth)
  1647. return -EINVAL;
  1648. if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
  1649. return -EINVAL;
  1650. if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
  1651. return -EINVAL;
  1652. if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
  1653. pr_info("blk-mq: reduced tag depth to %u\n",
  1654. BLK_MQ_MAX_DEPTH);
  1655. set->queue_depth = BLK_MQ_MAX_DEPTH;
  1656. }
  1657. set->tags = kmalloc_node(set->nr_hw_queues *
  1658. sizeof(struct blk_mq_tags *),
  1659. GFP_KERNEL, set->numa_node);
  1660. if (!set->tags)
  1661. return -ENOMEM;
  1662. if (blk_mq_alloc_rq_maps(set))
  1663. goto enomem;
  1664. mutex_init(&set->tag_list_lock);
  1665. INIT_LIST_HEAD(&set->tag_list);
  1666. return 0;
  1667. enomem:
  1668. kfree(set->tags);
  1669. set->tags = NULL;
  1670. return -ENOMEM;
  1671. }
  1672. EXPORT_SYMBOL(blk_mq_alloc_tag_set);
  1673. void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
  1674. {
  1675. int i;
  1676. for (i = 0; i < set->nr_hw_queues; i++) {
  1677. if (set->tags[i])
  1678. blk_mq_free_rq_map(set, set->tags[i], i);
  1679. }
  1680. kfree(set->tags);
  1681. set->tags = NULL;
  1682. }
  1683. EXPORT_SYMBOL(blk_mq_free_tag_set);
  1684. int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
  1685. {
  1686. struct blk_mq_tag_set *set = q->tag_set;
  1687. struct blk_mq_hw_ctx *hctx;
  1688. int i, ret;
  1689. if (!set || nr > set->queue_depth)
  1690. return -EINVAL;
  1691. ret = 0;
  1692. queue_for_each_hw_ctx(q, hctx, i) {
  1693. ret = blk_mq_tag_update_depth(hctx->tags, nr);
  1694. if (ret)
  1695. break;
  1696. }
  1697. if (!ret)
  1698. q->nr_requests = nr;
  1699. return ret;
  1700. }
  1701. void blk_mq_disable_hotplug(void)
  1702. {
  1703. mutex_lock(&all_q_mutex);
  1704. }
  1705. void blk_mq_enable_hotplug(void)
  1706. {
  1707. mutex_unlock(&all_q_mutex);
  1708. }
  1709. static int __init blk_mq_init(void)
  1710. {
  1711. blk_mq_cpu_init();
  1712. hotcpu_notifier(blk_mq_queue_reinit_notify, 0);
  1713. return 0;
  1714. }
  1715. subsys_initcall(blk_mq_init);