blk-mq.c 54 KB

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