blk-mq.c 50 KB

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