blk-mq.c 56 KB

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