blk-mq.c 57 KB

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