blk-mq.c 56 KB

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