blk-mq.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107
  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/sched/topology.h>
  24. #include <linux/sched/signal.h>
  25. #include <linux/delay.h>
  26. #include <linux/crash_dump.h>
  27. #include <linux/prefetch.h>
  28. #include <trace/events/block.h>
  29. #include <linux/blk-mq.h>
  30. #include "blk.h"
  31. #include "blk-mq.h"
  32. #include "blk-mq-debugfs.h"
  33. #include "blk-mq-tag.h"
  34. #include "blk-stat.h"
  35. #include "blk-mq-sched.h"
  36. #include "blk-rq-qos.h"
  37. static bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie);
  38. static void blk_mq_poll_stats_start(struct request_queue *q);
  39. static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);
  40. static int blk_mq_poll_stats_bkt(const struct request *rq)
  41. {
  42. int ddir, bytes, bucket;
  43. ddir = rq_data_dir(rq);
  44. bytes = blk_rq_bytes(rq);
  45. bucket = ddir + 2*(ilog2(bytes) - 9);
  46. if (bucket < 0)
  47. return -1;
  48. else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
  49. return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
  50. return bucket;
  51. }
  52. /*
  53. * Check if any of the ctx's have pending work in this hardware queue
  54. */
  55. static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
  56. {
  57. return !list_empty_careful(&hctx->dispatch) ||
  58. sbitmap_any_bit_set(&hctx->ctx_map) ||
  59. blk_mq_sched_has_work(hctx);
  60. }
  61. /*
  62. * Mark this ctx as having pending work in this hardware queue
  63. */
  64. static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
  65. struct blk_mq_ctx *ctx)
  66. {
  67. if (!sbitmap_test_bit(&hctx->ctx_map, ctx->index_hw))
  68. sbitmap_set_bit(&hctx->ctx_map, ctx->index_hw);
  69. }
  70. static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
  71. struct blk_mq_ctx *ctx)
  72. {
  73. sbitmap_clear_bit(&hctx->ctx_map, ctx->index_hw);
  74. }
  75. struct mq_inflight {
  76. struct hd_struct *part;
  77. unsigned int *inflight;
  78. };
  79. static void blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx,
  80. struct request *rq, void *priv,
  81. bool reserved)
  82. {
  83. struct mq_inflight *mi = priv;
  84. /*
  85. * index[0] counts the specific partition that was asked for. index[1]
  86. * counts the ones that are active on the whole device, so increment
  87. * that if mi->part is indeed a partition, and not a whole device.
  88. */
  89. if (rq->part == mi->part)
  90. mi->inflight[0]++;
  91. if (mi->part->partno)
  92. mi->inflight[1]++;
  93. }
  94. void blk_mq_in_flight(struct request_queue *q, struct hd_struct *part,
  95. unsigned int inflight[2])
  96. {
  97. struct mq_inflight mi = { .part = part, .inflight = inflight, };
  98. inflight[0] = inflight[1] = 0;
  99. blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
  100. }
  101. static void blk_mq_check_inflight_rw(struct blk_mq_hw_ctx *hctx,
  102. struct request *rq, void *priv,
  103. bool reserved)
  104. {
  105. struct mq_inflight *mi = priv;
  106. if (rq->part == mi->part)
  107. mi->inflight[rq_data_dir(rq)]++;
  108. }
  109. void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,
  110. unsigned int inflight[2])
  111. {
  112. struct mq_inflight mi = { .part = part, .inflight = inflight, };
  113. inflight[0] = inflight[1] = 0;
  114. blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight_rw, &mi);
  115. }
  116. void blk_freeze_queue_start(struct request_queue *q)
  117. {
  118. int freeze_depth;
  119. freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
  120. if (freeze_depth == 1) {
  121. percpu_ref_kill(&q->q_usage_counter);
  122. if (q->mq_ops)
  123. blk_mq_run_hw_queues(q, false);
  124. }
  125. }
  126. EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
  127. void blk_mq_freeze_queue_wait(struct request_queue *q)
  128. {
  129. wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  130. }
  131. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
  132. int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
  133. unsigned long timeout)
  134. {
  135. return wait_event_timeout(q->mq_freeze_wq,
  136. percpu_ref_is_zero(&q->q_usage_counter),
  137. timeout);
  138. }
  139. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
  140. /*
  141. * Guarantee no request is in use, so we can change any data structure of
  142. * the queue afterward.
  143. */
  144. void blk_freeze_queue(struct request_queue *q)
  145. {
  146. /*
  147. * In the !blk_mq case we are only calling this to kill the
  148. * q_usage_counter, otherwise this increases the freeze depth
  149. * and waits for it to return to zero. For this reason there is
  150. * no blk_unfreeze_queue(), and blk_freeze_queue() is not
  151. * exported to drivers as the only user for unfreeze is blk_mq.
  152. */
  153. blk_freeze_queue_start(q);
  154. if (!q->mq_ops)
  155. blk_drain_queue(q);
  156. blk_mq_freeze_queue_wait(q);
  157. }
  158. void blk_mq_freeze_queue(struct request_queue *q)
  159. {
  160. /*
  161. * ...just an alias to keep freeze and unfreeze actions balanced
  162. * in the blk_mq_* namespace
  163. */
  164. blk_freeze_queue(q);
  165. }
  166. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
  167. void blk_mq_unfreeze_queue(struct request_queue *q)
  168. {
  169. int freeze_depth;
  170. freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
  171. WARN_ON_ONCE(freeze_depth < 0);
  172. if (!freeze_depth) {
  173. percpu_ref_reinit(&q->q_usage_counter);
  174. wake_up_all(&q->mq_freeze_wq);
  175. }
  176. }
  177. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
  178. /*
  179. * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
  180. * mpt3sas driver such that this function can be removed.
  181. */
  182. void blk_mq_quiesce_queue_nowait(struct request_queue *q)
  183. {
  184. blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
  185. }
  186. EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
  187. /**
  188. * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
  189. * @q: request queue.
  190. *
  191. * Note: this function does not prevent that the struct request end_io()
  192. * callback function is invoked. Once this function is returned, we make
  193. * sure no dispatch can happen until the queue is unquiesced via
  194. * blk_mq_unquiesce_queue().
  195. */
  196. void blk_mq_quiesce_queue(struct request_queue *q)
  197. {
  198. struct blk_mq_hw_ctx *hctx;
  199. unsigned int i;
  200. bool rcu = false;
  201. blk_mq_quiesce_queue_nowait(q);
  202. queue_for_each_hw_ctx(q, hctx, i) {
  203. if (hctx->flags & BLK_MQ_F_BLOCKING)
  204. synchronize_srcu(hctx->srcu);
  205. else
  206. rcu = true;
  207. }
  208. if (rcu)
  209. synchronize_rcu();
  210. }
  211. EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
  212. /*
  213. * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
  214. * @q: request queue.
  215. *
  216. * This function recovers queue into the state before quiescing
  217. * which is done by blk_mq_quiesce_queue.
  218. */
  219. void blk_mq_unquiesce_queue(struct request_queue *q)
  220. {
  221. blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
  222. /* dispatch requests which are inserted during quiescing */
  223. blk_mq_run_hw_queues(q, true);
  224. }
  225. EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
  226. void blk_mq_wake_waiters(struct request_queue *q)
  227. {
  228. struct blk_mq_hw_ctx *hctx;
  229. unsigned int i;
  230. queue_for_each_hw_ctx(q, hctx, i)
  231. if (blk_mq_hw_queue_mapped(hctx))
  232. blk_mq_tag_wakeup_all(hctx->tags, true);
  233. }
  234. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  235. {
  236. return blk_mq_has_free_tags(hctx->tags);
  237. }
  238. EXPORT_SYMBOL(blk_mq_can_queue);
  239. static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
  240. unsigned int tag, unsigned int op)
  241. {
  242. struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
  243. struct request *rq = tags->static_rqs[tag];
  244. req_flags_t rq_flags = 0;
  245. if (data->flags & BLK_MQ_REQ_INTERNAL) {
  246. rq->tag = -1;
  247. rq->internal_tag = tag;
  248. } else {
  249. if (blk_mq_tag_busy(data->hctx)) {
  250. rq_flags = RQF_MQ_INFLIGHT;
  251. atomic_inc(&data->hctx->nr_active);
  252. }
  253. rq->tag = tag;
  254. rq->internal_tag = -1;
  255. data->hctx->tags->rqs[rq->tag] = rq;
  256. }
  257. /* csd/requeue_work/fifo_time is initialized before use */
  258. rq->q = data->q;
  259. rq->mq_ctx = data->ctx;
  260. rq->rq_flags = rq_flags;
  261. rq->cpu = -1;
  262. rq->cmd_flags = op;
  263. if (data->flags & BLK_MQ_REQ_PREEMPT)
  264. rq->rq_flags |= RQF_PREEMPT;
  265. if (blk_queue_io_stat(data->q))
  266. rq->rq_flags |= RQF_IO_STAT;
  267. INIT_LIST_HEAD(&rq->queuelist);
  268. INIT_HLIST_NODE(&rq->hash);
  269. RB_CLEAR_NODE(&rq->rb_node);
  270. rq->rq_disk = NULL;
  271. rq->part = NULL;
  272. rq->start_time_ns = ktime_get_ns();
  273. rq->io_start_time_ns = 0;
  274. rq->nr_phys_segments = 0;
  275. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  276. rq->nr_integrity_segments = 0;
  277. #endif
  278. rq->special = NULL;
  279. /* tag was already set */
  280. rq->extra_len = 0;
  281. rq->__deadline = 0;
  282. INIT_LIST_HEAD(&rq->timeout_list);
  283. rq->timeout = 0;
  284. rq->end_io = NULL;
  285. rq->end_io_data = NULL;
  286. rq->next_rq = NULL;
  287. #ifdef CONFIG_BLK_CGROUP
  288. rq->rl = NULL;
  289. #endif
  290. data->ctx->rq_dispatched[op_is_sync(op)]++;
  291. refcount_set(&rq->ref, 1);
  292. return rq;
  293. }
  294. static struct request *blk_mq_get_request(struct request_queue *q,
  295. struct bio *bio, unsigned int op,
  296. struct blk_mq_alloc_data *data)
  297. {
  298. struct elevator_queue *e = q->elevator;
  299. struct request *rq;
  300. unsigned int tag;
  301. bool put_ctx_on_error = false;
  302. blk_queue_enter_live(q);
  303. data->q = q;
  304. if (likely(!data->ctx)) {
  305. data->ctx = blk_mq_get_ctx(q);
  306. put_ctx_on_error = true;
  307. }
  308. if (likely(!data->hctx))
  309. data->hctx = blk_mq_map_queue(q, data->ctx->cpu);
  310. if (op & REQ_NOWAIT)
  311. data->flags |= BLK_MQ_REQ_NOWAIT;
  312. if (e) {
  313. data->flags |= BLK_MQ_REQ_INTERNAL;
  314. /*
  315. * Flush requests are special and go directly to the
  316. * dispatch list. Don't include reserved tags in the
  317. * limiting, as it isn't useful.
  318. */
  319. if (!op_is_flush(op) && e->type->ops.mq.limit_depth &&
  320. !(data->flags & BLK_MQ_REQ_RESERVED))
  321. e->type->ops.mq.limit_depth(op, data);
  322. }
  323. tag = blk_mq_get_tag(data);
  324. if (tag == BLK_MQ_TAG_FAIL) {
  325. if (put_ctx_on_error) {
  326. blk_mq_put_ctx(data->ctx);
  327. data->ctx = NULL;
  328. }
  329. blk_queue_exit(q);
  330. return NULL;
  331. }
  332. rq = blk_mq_rq_ctx_init(data, tag, op);
  333. if (!op_is_flush(op)) {
  334. rq->elv.icq = NULL;
  335. if (e && e->type->ops.mq.prepare_request) {
  336. if (e->type->icq_cache && rq_ioc(bio))
  337. blk_mq_sched_assign_ioc(rq, bio);
  338. e->type->ops.mq.prepare_request(rq, bio);
  339. rq->rq_flags |= RQF_ELVPRIV;
  340. }
  341. }
  342. data->hctx->queued++;
  343. return rq;
  344. }
  345. struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
  346. blk_mq_req_flags_t flags)
  347. {
  348. struct blk_mq_alloc_data alloc_data = { .flags = flags };
  349. struct request *rq;
  350. int ret;
  351. ret = blk_queue_enter(q, flags);
  352. if (ret)
  353. return ERR_PTR(ret);
  354. rq = blk_mq_get_request(q, NULL, op, &alloc_data);
  355. blk_queue_exit(q);
  356. if (!rq)
  357. return ERR_PTR(-EWOULDBLOCK);
  358. blk_mq_put_ctx(alloc_data.ctx);
  359. rq->__data_len = 0;
  360. rq->__sector = (sector_t) -1;
  361. rq->bio = rq->biotail = NULL;
  362. return rq;
  363. }
  364. EXPORT_SYMBOL(blk_mq_alloc_request);
  365. struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
  366. unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
  367. {
  368. struct blk_mq_alloc_data alloc_data = { .flags = flags };
  369. struct request *rq;
  370. unsigned int cpu;
  371. int ret;
  372. /*
  373. * If the tag allocator sleeps we could get an allocation for a
  374. * different hardware context. No need to complicate the low level
  375. * allocator for this for the rare use case of a command tied to
  376. * a specific queue.
  377. */
  378. if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
  379. return ERR_PTR(-EINVAL);
  380. if (hctx_idx >= q->nr_hw_queues)
  381. return ERR_PTR(-EIO);
  382. ret = blk_queue_enter(q, flags);
  383. if (ret)
  384. return ERR_PTR(ret);
  385. /*
  386. * Check if the hardware context is actually mapped to anything.
  387. * If not tell the caller that it should skip this queue.
  388. */
  389. alloc_data.hctx = q->queue_hw_ctx[hctx_idx];
  390. if (!blk_mq_hw_queue_mapped(alloc_data.hctx)) {
  391. blk_queue_exit(q);
  392. return ERR_PTR(-EXDEV);
  393. }
  394. cpu = cpumask_first_and(alloc_data.hctx->cpumask, cpu_online_mask);
  395. alloc_data.ctx = __blk_mq_get_ctx(q, cpu);
  396. rq = blk_mq_get_request(q, NULL, op, &alloc_data);
  397. blk_queue_exit(q);
  398. if (!rq)
  399. return ERR_PTR(-EWOULDBLOCK);
  400. return rq;
  401. }
  402. EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
  403. static void __blk_mq_free_request(struct request *rq)
  404. {
  405. struct request_queue *q = rq->q;
  406. struct blk_mq_ctx *ctx = rq->mq_ctx;
  407. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
  408. const int sched_tag = rq->internal_tag;
  409. if (rq->tag != -1)
  410. blk_mq_put_tag(hctx, hctx->tags, ctx, rq->tag);
  411. if (sched_tag != -1)
  412. blk_mq_put_tag(hctx, hctx->sched_tags, ctx, sched_tag);
  413. blk_mq_sched_restart(hctx);
  414. blk_queue_exit(q);
  415. }
  416. void blk_mq_free_request(struct request *rq)
  417. {
  418. struct request_queue *q = rq->q;
  419. struct elevator_queue *e = q->elevator;
  420. struct blk_mq_ctx *ctx = rq->mq_ctx;
  421. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
  422. if (rq->rq_flags & RQF_ELVPRIV) {
  423. if (e && e->type->ops.mq.finish_request)
  424. e->type->ops.mq.finish_request(rq);
  425. if (rq->elv.icq) {
  426. put_io_context(rq->elv.icq->ioc);
  427. rq->elv.icq = NULL;
  428. }
  429. }
  430. ctx->rq_completed[rq_is_sync(rq)]++;
  431. if (rq->rq_flags & RQF_MQ_INFLIGHT)
  432. atomic_dec(&hctx->nr_active);
  433. if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
  434. laptop_io_completion(q->backing_dev_info);
  435. rq_qos_done(q, rq);
  436. if (blk_rq_rl(rq))
  437. blk_put_rl(blk_rq_rl(rq));
  438. WRITE_ONCE(rq->state, MQ_RQ_IDLE);
  439. if (refcount_dec_and_test(&rq->ref))
  440. __blk_mq_free_request(rq);
  441. }
  442. EXPORT_SYMBOL_GPL(blk_mq_free_request);
  443. inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
  444. {
  445. u64 now = ktime_get_ns();
  446. if (rq->rq_flags & RQF_STATS) {
  447. blk_mq_poll_stats_start(rq->q);
  448. blk_stat_add(rq, now);
  449. }
  450. blk_account_io_done(rq, now);
  451. if (rq->end_io) {
  452. rq_qos_done(rq->q, rq);
  453. rq->end_io(rq, error);
  454. } else {
  455. if (unlikely(blk_bidi_rq(rq)))
  456. blk_mq_free_request(rq->next_rq);
  457. blk_mq_free_request(rq);
  458. }
  459. }
  460. EXPORT_SYMBOL(__blk_mq_end_request);
  461. void blk_mq_end_request(struct request *rq, blk_status_t error)
  462. {
  463. if (blk_update_request(rq, error, blk_rq_bytes(rq)))
  464. BUG();
  465. __blk_mq_end_request(rq, error);
  466. }
  467. EXPORT_SYMBOL(blk_mq_end_request);
  468. static void __blk_mq_complete_request_remote(void *data)
  469. {
  470. struct request *rq = data;
  471. rq->q->softirq_done_fn(rq);
  472. }
  473. static void __blk_mq_complete_request(struct request *rq)
  474. {
  475. struct blk_mq_ctx *ctx = rq->mq_ctx;
  476. bool shared = false;
  477. int cpu;
  478. if (cmpxchg(&rq->state, MQ_RQ_IN_FLIGHT, MQ_RQ_COMPLETE) !=
  479. MQ_RQ_IN_FLIGHT)
  480. return;
  481. if (rq->internal_tag != -1)
  482. blk_mq_sched_completed_request(rq);
  483. if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
  484. rq->q->softirq_done_fn(rq);
  485. return;
  486. }
  487. cpu = get_cpu();
  488. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
  489. shared = cpus_share_cache(cpu, ctx->cpu);
  490. if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
  491. rq->csd.func = __blk_mq_complete_request_remote;
  492. rq->csd.info = rq;
  493. rq->csd.flags = 0;
  494. smp_call_function_single_async(ctx->cpu, &rq->csd);
  495. } else {
  496. rq->q->softirq_done_fn(rq);
  497. }
  498. put_cpu();
  499. }
  500. static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
  501. __releases(hctx->srcu)
  502. {
  503. if (!(hctx->flags & BLK_MQ_F_BLOCKING))
  504. rcu_read_unlock();
  505. else
  506. srcu_read_unlock(hctx->srcu, srcu_idx);
  507. }
  508. static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
  509. __acquires(hctx->srcu)
  510. {
  511. if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
  512. /* shut up gcc false positive */
  513. *srcu_idx = 0;
  514. rcu_read_lock();
  515. } else
  516. *srcu_idx = srcu_read_lock(hctx->srcu);
  517. }
  518. /**
  519. * blk_mq_complete_request - end I/O on a request
  520. * @rq: the request being processed
  521. *
  522. * Description:
  523. * Ends all I/O on a request. It does not handle partial completions.
  524. * The actual completion happens out-of-order, through a IPI handler.
  525. **/
  526. void blk_mq_complete_request(struct request *rq)
  527. {
  528. if (unlikely(blk_should_fake_timeout(rq->q)))
  529. return;
  530. __blk_mq_complete_request(rq);
  531. }
  532. EXPORT_SYMBOL(blk_mq_complete_request);
  533. int blk_mq_request_started(struct request *rq)
  534. {
  535. return blk_mq_rq_state(rq) != MQ_RQ_IDLE;
  536. }
  537. EXPORT_SYMBOL_GPL(blk_mq_request_started);
  538. void blk_mq_start_request(struct request *rq)
  539. {
  540. struct request_queue *q = rq->q;
  541. blk_mq_sched_started_request(rq);
  542. trace_block_rq_issue(q, rq);
  543. if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
  544. rq->io_start_time_ns = ktime_get_ns();
  545. #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
  546. rq->throtl_size = blk_rq_sectors(rq);
  547. #endif
  548. rq->rq_flags |= RQF_STATS;
  549. rq_qos_issue(q, rq);
  550. }
  551. WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
  552. blk_add_timer(rq);
  553. WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
  554. if (q->dma_drain_size && blk_rq_bytes(rq)) {
  555. /*
  556. * Make sure space for the drain appears. We know we can do
  557. * this because max_hw_segments has been adjusted to be one
  558. * fewer than the device can handle.
  559. */
  560. rq->nr_phys_segments++;
  561. }
  562. }
  563. EXPORT_SYMBOL(blk_mq_start_request);
  564. static void __blk_mq_requeue_request(struct request *rq)
  565. {
  566. struct request_queue *q = rq->q;
  567. blk_mq_put_driver_tag(rq);
  568. trace_block_rq_requeue(q, rq);
  569. rq_qos_requeue(q, rq);
  570. if (blk_mq_request_started(rq)) {
  571. WRITE_ONCE(rq->state, MQ_RQ_IDLE);
  572. rq->rq_flags &= ~RQF_TIMED_OUT;
  573. if (q->dma_drain_size && blk_rq_bytes(rq))
  574. rq->nr_phys_segments--;
  575. }
  576. }
  577. void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
  578. {
  579. __blk_mq_requeue_request(rq);
  580. /* this request will be re-inserted to io scheduler queue */
  581. blk_mq_sched_requeue_request(rq);
  582. BUG_ON(blk_queued_rq(rq));
  583. blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
  584. }
  585. EXPORT_SYMBOL(blk_mq_requeue_request);
  586. static void blk_mq_requeue_work(struct work_struct *work)
  587. {
  588. struct request_queue *q =
  589. container_of(work, struct request_queue, requeue_work.work);
  590. LIST_HEAD(rq_list);
  591. struct request *rq, *next;
  592. spin_lock_irq(&q->requeue_lock);
  593. list_splice_init(&q->requeue_list, &rq_list);
  594. spin_unlock_irq(&q->requeue_lock);
  595. list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
  596. if (!(rq->rq_flags & RQF_SOFTBARRIER))
  597. continue;
  598. rq->rq_flags &= ~RQF_SOFTBARRIER;
  599. list_del_init(&rq->queuelist);
  600. blk_mq_sched_insert_request(rq, true, false, false);
  601. }
  602. while (!list_empty(&rq_list)) {
  603. rq = list_entry(rq_list.next, struct request, queuelist);
  604. list_del_init(&rq->queuelist);
  605. blk_mq_sched_insert_request(rq, false, false, false);
  606. }
  607. blk_mq_run_hw_queues(q, false);
  608. }
  609. void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
  610. bool kick_requeue_list)
  611. {
  612. struct request_queue *q = rq->q;
  613. unsigned long flags;
  614. /*
  615. * We abuse this flag that is otherwise used by the I/O scheduler to
  616. * request head insertion from the workqueue.
  617. */
  618. BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
  619. spin_lock_irqsave(&q->requeue_lock, flags);
  620. if (at_head) {
  621. rq->rq_flags |= RQF_SOFTBARRIER;
  622. list_add(&rq->queuelist, &q->requeue_list);
  623. } else {
  624. list_add_tail(&rq->queuelist, &q->requeue_list);
  625. }
  626. spin_unlock_irqrestore(&q->requeue_lock, flags);
  627. if (kick_requeue_list)
  628. blk_mq_kick_requeue_list(q);
  629. }
  630. EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
  631. void blk_mq_kick_requeue_list(struct request_queue *q)
  632. {
  633. kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
  634. }
  635. EXPORT_SYMBOL(blk_mq_kick_requeue_list);
  636. void blk_mq_delay_kick_requeue_list(struct request_queue *q,
  637. unsigned long msecs)
  638. {
  639. kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
  640. msecs_to_jiffies(msecs));
  641. }
  642. EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
  643. struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
  644. {
  645. if (tag < tags->nr_tags) {
  646. prefetch(tags->rqs[tag]);
  647. return tags->rqs[tag];
  648. }
  649. return NULL;
  650. }
  651. EXPORT_SYMBOL(blk_mq_tag_to_rq);
  652. static void blk_mq_rq_timed_out(struct request *req, bool reserved)
  653. {
  654. req->rq_flags |= RQF_TIMED_OUT;
  655. if (req->q->mq_ops->timeout) {
  656. enum blk_eh_timer_return ret;
  657. ret = req->q->mq_ops->timeout(req, reserved);
  658. if (ret == BLK_EH_DONE)
  659. return;
  660. WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
  661. }
  662. blk_add_timer(req);
  663. }
  664. static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
  665. {
  666. unsigned long deadline;
  667. if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
  668. return false;
  669. if (rq->rq_flags & RQF_TIMED_OUT)
  670. return false;
  671. deadline = blk_rq_deadline(rq);
  672. if (time_after_eq(jiffies, deadline))
  673. return true;
  674. if (*next == 0)
  675. *next = deadline;
  676. else if (time_after(*next, deadline))
  677. *next = deadline;
  678. return false;
  679. }
  680. static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
  681. struct request *rq, void *priv, bool reserved)
  682. {
  683. unsigned long *next = priv;
  684. /*
  685. * Just do a quick check if it is expired before locking the request in
  686. * so we're not unnecessarilly synchronizing across CPUs.
  687. */
  688. if (!blk_mq_req_expired(rq, next))
  689. return;
  690. /*
  691. * We have reason to believe the request may be expired. Take a
  692. * reference on the request to lock this request lifetime into its
  693. * currently allocated context to prevent it from being reallocated in
  694. * the event the completion by-passes this timeout handler.
  695. *
  696. * If the reference was already released, then the driver beat the
  697. * timeout handler to posting a natural completion.
  698. */
  699. if (!refcount_inc_not_zero(&rq->ref))
  700. return;
  701. /*
  702. * The request is now locked and cannot be reallocated underneath the
  703. * timeout handler's processing. Re-verify this exact request is truly
  704. * expired; if it is not expired, then the request was completed and
  705. * reallocated as a new request.
  706. */
  707. if (blk_mq_req_expired(rq, next))
  708. blk_mq_rq_timed_out(rq, reserved);
  709. if (refcount_dec_and_test(&rq->ref))
  710. __blk_mq_free_request(rq);
  711. }
  712. static void blk_mq_timeout_work(struct work_struct *work)
  713. {
  714. struct request_queue *q =
  715. container_of(work, struct request_queue, timeout_work);
  716. unsigned long next = 0;
  717. struct blk_mq_hw_ctx *hctx;
  718. int i;
  719. /* A deadlock might occur if a request is stuck requiring a
  720. * timeout at the same time a queue freeze is waiting
  721. * completion, since the timeout code would not be able to
  722. * acquire the queue reference here.
  723. *
  724. * That's why we don't use blk_queue_enter here; instead, we use
  725. * percpu_ref_tryget directly, because we need to be able to
  726. * obtain a reference even in the short window between the queue
  727. * starting to freeze, by dropping the first reference in
  728. * blk_freeze_queue_start, and the moment the last request is
  729. * consumed, marked by the instant q_usage_counter reaches
  730. * zero.
  731. */
  732. if (!percpu_ref_tryget(&q->q_usage_counter))
  733. return;
  734. blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
  735. if (next != 0) {
  736. mod_timer(&q->timeout, next);
  737. } else {
  738. /*
  739. * Request timeouts are handled as a forward rolling timer. If
  740. * we end up here it means that no requests are pending and
  741. * also that no request has been pending for a while. Mark
  742. * each hctx as idle.
  743. */
  744. queue_for_each_hw_ctx(q, hctx, i) {
  745. /* the hctx may be unmapped, so check it here */
  746. if (blk_mq_hw_queue_mapped(hctx))
  747. blk_mq_tag_idle(hctx);
  748. }
  749. }
  750. blk_queue_exit(q);
  751. }
  752. struct flush_busy_ctx_data {
  753. struct blk_mq_hw_ctx *hctx;
  754. struct list_head *list;
  755. };
  756. static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
  757. {
  758. struct flush_busy_ctx_data *flush_data = data;
  759. struct blk_mq_hw_ctx *hctx = flush_data->hctx;
  760. struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
  761. spin_lock(&ctx->lock);
  762. list_splice_tail_init(&ctx->rq_list, flush_data->list);
  763. sbitmap_clear_bit(sb, bitnr);
  764. spin_unlock(&ctx->lock);
  765. return true;
  766. }
  767. /*
  768. * Process software queues that have been marked busy, splicing them
  769. * to the for-dispatch
  770. */
  771. void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
  772. {
  773. struct flush_busy_ctx_data data = {
  774. .hctx = hctx,
  775. .list = list,
  776. };
  777. sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
  778. }
  779. EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
  780. struct dispatch_rq_data {
  781. struct blk_mq_hw_ctx *hctx;
  782. struct request *rq;
  783. };
  784. static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
  785. void *data)
  786. {
  787. struct dispatch_rq_data *dispatch_data = data;
  788. struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
  789. struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
  790. spin_lock(&ctx->lock);
  791. if (!list_empty(&ctx->rq_list)) {
  792. dispatch_data->rq = list_entry_rq(ctx->rq_list.next);
  793. list_del_init(&dispatch_data->rq->queuelist);
  794. if (list_empty(&ctx->rq_list))
  795. sbitmap_clear_bit(sb, bitnr);
  796. }
  797. spin_unlock(&ctx->lock);
  798. return !dispatch_data->rq;
  799. }
  800. struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
  801. struct blk_mq_ctx *start)
  802. {
  803. unsigned off = start ? start->index_hw : 0;
  804. struct dispatch_rq_data data = {
  805. .hctx = hctx,
  806. .rq = NULL,
  807. };
  808. __sbitmap_for_each_set(&hctx->ctx_map, off,
  809. dispatch_rq_from_ctx, &data);
  810. return data.rq;
  811. }
  812. static inline unsigned int queued_to_index(unsigned int queued)
  813. {
  814. if (!queued)
  815. return 0;
  816. return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
  817. }
  818. bool blk_mq_get_driver_tag(struct request *rq)
  819. {
  820. struct blk_mq_alloc_data data = {
  821. .q = rq->q,
  822. .hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu),
  823. .flags = BLK_MQ_REQ_NOWAIT,
  824. };
  825. if (rq->tag != -1)
  826. goto done;
  827. if (blk_mq_tag_is_reserved(data.hctx->sched_tags, rq->internal_tag))
  828. data.flags |= BLK_MQ_REQ_RESERVED;
  829. rq->tag = blk_mq_get_tag(&data);
  830. if (rq->tag >= 0) {
  831. if (blk_mq_tag_busy(data.hctx)) {
  832. rq->rq_flags |= RQF_MQ_INFLIGHT;
  833. atomic_inc(&data.hctx->nr_active);
  834. }
  835. data.hctx->tags->rqs[rq->tag] = rq;
  836. }
  837. done:
  838. return rq->tag != -1;
  839. }
  840. static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
  841. int flags, void *key)
  842. {
  843. struct blk_mq_hw_ctx *hctx;
  844. hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
  845. spin_lock(&hctx->dispatch_wait_lock);
  846. list_del_init(&wait->entry);
  847. spin_unlock(&hctx->dispatch_wait_lock);
  848. blk_mq_run_hw_queue(hctx, true);
  849. return 1;
  850. }
  851. /*
  852. * Mark us waiting for a tag. For shared tags, this involves hooking us into
  853. * the tag wakeups. For non-shared tags, we can simply mark us needing a
  854. * restart. For both cases, take care to check the condition again after
  855. * marking us as waiting.
  856. */
  857. static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
  858. struct request *rq)
  859. {
  860. struct wait_queue_head *wq;
  861. wait_queue_entry_t *wait;
  862. bool ret;
  863. if (!(hctx->flags & BLK_MQ_F_TAG_SHARED)) {
  864. if (!test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
  865. set_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
  866. /*
  867. * It's possible that a tag was freed in the window between the
  868. * allocation failure and adding the hardware queue to the wait
  869. * queue.
  870. *
  871. * Don't clear RESTART here, someone else could have set it.
  872. * At most this will cost an extra queue run.
  873. */
  874. return blk_mq_get_driver_tag(rq);
  875. }
  876. wait = &hctx->dispatch_wait;
  877. if (!list_empty_careful(&wait->entry))
  878. return false;
  879. wq = &bt_wait_ptr(&hctx->tags->bitmap_tags, hctx)->wait;
  880. spin_lock_irq(&wq->lock);
  881. spin_lock(&hctx->dispatch_wait_lock);
  882. if (!list_empty(&wait->entry)) {
  883. spin_unlock(&hctx->dispatch_wait_lock);
  884. spin_unlock_irq(&wq->lock);
  885. return false;
  886. }
  887. wait->flags &= ~WQ_FLAG_EXCLUSIVE;
  888. __add_wait_queue(wq, wait);
  889. /*
  890. * It's possible that a tag was freed in the window between the
  891. * allocation failure and adding the hardware queue to the wait
  892. * queue.
  893. */
  894. ret = blk_mq_get_driver_tag(rq);
  895. if (!ret) {
  896. spin_unlock(&hctx->dispatch_wait_lock);
  897. spin_unlock_irq(&wq->lock);
  898. return false;
  899. }
  900. /*
  901. * We got a tag, remove ourselves from the wait queue to ensure
  902. * someone else gets the wakeup.
  903. */
  904. list_del_init(&wait->entry);
  905. spin_unlock(&hctx->dispatch_wait_lock);
  906. spin_unlock_irq(&wq->lock);
  907. return true;
  908. }
  909. #define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
  910. #define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
  911. /*
  912. * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
  913. * - EWMA is one simple way to compute running average value
  914. * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
  915. * - take 4 as factor for avoiding to get too small(0) result, and this
  916. * factor doesn't matter because EWMA decreases exponentially
  917. */
  918. static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
  919. {
  920. unsigned int ewma;
  921. if (hctx->queue->elevator)
  922. return;
  923. ewma = hctx->dispatch_busy;
  924. if (!ewma && !busy)
  925. return;
  926. ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
  927. if (busy)
  928. ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
  929. ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
  930. hctx->dispatch_busy = ewma;
  931. }
  932. #define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
  933. /*
  934. * Returns true if we did some work AND can potentially do more.
  935. */
  936. bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list,
  937. bool got_budget)
  938. {
  939. struct blk_mq_hw_ctx *hctx;
  940. struct request *rq, *nxt;
  941. bool no_tag = false;
  942. int errors, queued;
  943. blk_status_t ret = BLK_STS_OK;
  944. if (list_empty(list))
  945. return false;
  946. WARN_ON(!list_is_singular(list) && got_budget);
  947. /*
  948. * Now process all the entries, sending them to the driver.
  949. */
  950. errors = queued = 0;
  951. do {
  952. struct blk_mq_queue_data bd;
  953. rq = list_first_entry(list, struct request, queuelist);
  954. hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu);
  955. if (!got_budget && !blk_mq_get_dispatch_budget(hctx))
  956. break;
  957. if (!blk_mq_get_driver_tag(rq)) {
  958. /*
  959. * The initial allocation attempt failed, so we need to
  960. * rerun the hardware queue when a tag is freed. The
  961. * waitqueue takes care of that. If the queue is run
  962. * before we add this entry back on the dispatch list,
  963. * we'll re-run it below.
  964. */
  965. if (!blk_mq_mark_tag_wait(hctx, rq)) {
  966. blk_mq_put_dispatch_budget(hctx);
  967. /*
  968. * For non-shared tags, the RESTART check
  969. * will suffice.
  970. */
  971. if (hctx->flags & BLK_MQ_F_TAG_SHARED)
  972. no_tag = true;
  973. break;
  974. }
  975. }
  976. list_del_init(&rq->queuelist);
  977. bd.rq = rq;
  978. /*
  979. * Flag last if we have no more requests, or if we have more
  980. * but can't assign a driver tag to it.
  981. */
  982. if (list_empty(list))
  983. bd.last = true;
  984. else {
  985. nxt = list_first_entry(list, struct request, queuelist);
  986. bd.last = !blk_mq_get_driver_tag(nxt);
  987. }
  988. ret = q->mq_ops->queue_rq(hctx, &bd);
  989. if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) {
  990. /*
  991. * If an I/O scheduler has been configured and we got a
  992. * driver tag for the next request already, free it
  993. * again.
  994. */
  995. if (!list_empty(list)) {
  996. nxt = list_first_entry(list, struct request, queuelist);
  997. blk_mq_put_driver_tag(nxt);
  998. }
  999. list_add(&rq->queuelist, list);
  1000. __blk_mq_requeue_request(rq);
  1001. break;
  1002. }
  1003. if (unlikely(ret != BLK_STS_OK)) {
  1004. errors++;
  1005. blk_mq_end_request(rq, BLK_STS_IOERR);
  1006. continue;
  1007. }
  1008. queued++;
  1009. } while (!list_empty(list));
  1010. hctx->dispatched[queued_to_index(queued)]++;
  1011. /*
  1012. * Any items that need requeuing? Stuff them into hctx->dispatch,
  1013. * that is where we will continue on next queue run.
  1014. */
  1015. if (!list_empty(list)) {
  1016. bool needs_restart;
  1017. spin_lock(&hctx->lock);
  1018. list_splice_init(list, &hctx->dispatch);
  1019. spin_unlock(&hctx->lock);
  1020. /*
  1021. * If SCHED_RESTART was set by the caller of this function and
  1022. * it is no longer set that means that it was cleared by another
  1023. * thread and hence that a queue rerun is needed.
  1024. *
  1025. * If 'no_tag' is set, that means that we failed getting
  1026. * a driver tag with an I/O scheduler attached. If our dispatch
  1027. * waitqueue is no longer active, ensure that we run the queue
  1028. * AFTER adding our entries back to the list.
  1029. *
  1030. * If no I/O scheduler has been configured it is possible that
  1031. * the hardware queue got stopped and restarted before requests
  1032. * were pushed back onto the dispatch list. Rerun the queue to
  1033. * avoid starvation. Notes:
  1034. * - blk_mq_run_hw_queue() checks whether or not a queue has
  1035. * been stopped before rerunning a queue.
  1036. * - Some but not all block drivers stop a queue before
  1037. * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
  1038. * and dm-rq.
  1039. *
  1040. * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
  1041. * bit is set, run queue after a delay to avoid IO stalls
  1042. * that could otherwise occur if the queue is idle.
  1043. */
  1044. needs_restart = blk_mq_sched_needs_restart(hctx);
  1045. if (!needs_restart ||
  1046. (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
  1047. blk_mq_run_hw_queue(hctx, true);
  1048. else if (needs_restart && (ret == BLK_STS_RESOURCE))
  1049. blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
  1050. blk_mq_update_dispatch_busy(hctx, true);
  1051. return false;
  1052. } else
  1053. blk_mq_update_dispatch_busy(hctx, false);
  1054. /*
  1055. * If the host/device is unable to accept more work, inform the
  1056. * caller of that.
  1057. */
  1058. if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
  1059. return false;
  1060. return (queued + errors) != 0;
  1061. }
  1062. static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
  1063. {
  1064. int srcu_idx;
  1065. /*
  1066. * We should be running this queue from one of the CPUs that
  1067. * are mapped to it.
  1068. *
  1069. * There are at least two related races now between setting
  1070. * hctx->next_cpu from blk_mq_hctx_next_cpu() and running
  1071. * __blk_mq_run_hw_queue():
  1072. *
  1073. * - hctx->next_cpu is found offline in blk_mq_hctx_next_cpu(),
  1074. * but later it becomes online, then this warning is harmless
  1075. * at all
  1076. *
  1077. * - hctx->next_cpu is found online in blk_mq_hctx_next_cpu(),
  1078. * but later it becomes offline, then the warning can't be
  1079. * triggered, and we depend on blk-mq timeout handler to
  1080. * handle dispatched requests to this hctx
  1081. */
  1082. if (!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
  1083. cpu_online(hctx->next_cpu)) {
  1084. printk(KERN_WARNING "run queue from wrong CPU %d, hctx %s\n",
  1085. raw_smp_processor_id(),
  1086. cpumask_empty(hctx->cpumask) ? "inactive": "active");
  1087. dump_stack();
  1088. }
  1089. /*
  1090. * We can't run the queue inline with ints disabled. Ensure that
  1091. * we catch bad users of this early.
  1092. */
  1093. WARN_ON_ONCE(in_interrupt());
  1094. might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
  1095. hctx_lock(hctx, &srcu_idx);
  1096. blk_mq_sched_dispatch_requests(hctx);
  1097. hctx_unlock(hctx, srcu_idx);
  1098. }
  1099. static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
  1100. {
  1101. int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
  1102. if (cpu >= nr_cpu_ids)
  1103. cpu = cpumask_first(hctx->cpumask);
  1104. return cpu;
  1105. }
  1106. /*
  1107. * It'd be great if the workqueue API had a way to pass
  1108. * in a mask and had some smarts for more clever placement.
  1109. * For now we just round-robin here, switching for every
  1110. * BLK_MQ_CPU_WORK_BATCH queued items.
  1111. */
  1112. static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
  1113. {
  1114. bool tried = false;
  1115. int next_cpu = hctx->next_cpu;
  1116. if (hctx->queue->nr_hw_queues == 1)
  1117. return WORK_CPU_UNBOUND;
  1118. if (--hctx->next_cpu_batch <= 0) {
  1119. select_cpu:
  1120. next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
  1121. cpu_online_mask);
  1122. if (next_cpu >= nr_cpu_ids)
  1123. next_cpu = blk_mq_first_mapped_cpu(hctx);
  1124. hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
  1125. }
  1126. /*
  1127. * Do unbound schedule if we can't find a online CPU for this hctx,
  1128. * and it should only happen in the path of handling CPU DEAD.
  1129. */
  1130. if (!cpu_online(next_cpu)) {
  1131. if (!tried) {
  1132. tried = true;
  1133. goto select_cpu;
  1134. }
  1135. /*
  1136. * Make sure to re-select CPU next time once after CPUs
  1137. * in hctx->cpumask become online again.
  1138. */
  1139. hctx->next_cpu = next_cpu;
  1140. hctx->next_cpu_batch = 1;
  1141. return WORK_CPU_UNBOUND;
  1142. }
  1143. hctx->next_cpu = next_cpu;
  1144. return next_cpu;
  1145. }
  1146. static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
  1147. unsigned long msecs)
  1148. {
  1149. if (unlikely(blk_mq_hctx_stopped(hctx)))
  1150. return;
  1151. if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
  1152. int cpu = get_cpu();
  1153. if (cpumask_test_cpu(cpu, hctx->cpumask)) {
  1154. __blk_mq_run_hw_queue(hctx);
  1155. put_cpu();
  1156. return;
  1157. }
  1158. put_cpu();
  1159. }
  1160. kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
  1161. msecs_to_jiffies(msecs));
  1162. }
  1163. void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
  1164. {
  1165. __blk_mq_delay_run_hw_queue(hctx, true, msecs);
  1166. }
  1167. EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
  1168. bool blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  1169. {
  1170. int srcu_idx;
  1171. bool need_run;
  1172. /*
  1173. * When queue is quiesced, we may be switching io scheduler, or
  1174. * updating nr_hw_queues, or other things, and we can't run queue
  1175. * any more, even __blk_mq_hctx_has_pending() can't be called safely.
  1176. *
  1177. * And queue will be rerun in blk_mq_unquiesce_queue() if it is
  1178. * quiesced.
  1179. */
  1180. hctx_lock(hctx, &srcu_idx);
  1181. need_run = !blk_queue_quiesced(hctx->queue) &&
  1182. blk_mq_hctx_has_pending(hctx);
  1183. hctx_unlock(hctx, srcu_idx);
  1184. if (need_run) {
  1185. __blk_mq_delay_run_hw_queue(hctx, async, 0);
  1186. return true;
  1187. }
  1188. return false;
  1189. }
  1190. EXPORT_SYMBOL(blk_mq_run_hw_queue);
  1191. void blk_mq_run_hw_queues(struct request_queue *q, bool async)
  1192. {
  1193. struct blk_mq_hw_ctx *hctx;
  1194. int i;
  1195. queue_for_each_hw_ctx(q, hctx, i) {
  1196. if (blk_mq_hctx_stopped(hctx))
  1197. continue;
  1198. blk_mq_run_hw_queue(hctx, async);
  1199. }
  1200. }
  1201. EXPORT_SYMBOL(blk_mq_run_hw_queues);
  1202. /**
  1203. * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
  1204. * @q: request queue.
  1205. *
  1206. * The caller is responsible for serializing this function against
  1207. * blk_mq_{start,stop}_hw_queue().
  1208. */
  1209. bool blk_mq_queue_stopped(struct request_queue *q)
  1210. {
  1211. struct blk_mq_hw_ctx *hctx;
  1212. int i;
  1213. queue_for_each_hw_ctx(q, hctx, i)
  1214. if (blk_mq_hctx_stopped(hctx))
  1215. return true;
  1216. return false;
  1217. }
  1218. EXPORT_SYMBOL(blk_mq_queue_stopped);
  1219. /*
  1220. * This function is often used for pausing .queue_rq() by driver when
  1221. * there isn't enough resource or some conditions aren't satisfied, and
  1222. * BLK_STS_RESOURCE is usually returned.
  1223. *
  1224. * We do not guarantee that dispatch can be drained or blocked
  1225. * after blk_mq_stop_hw_queue() returns. Please use
  1226. * blk_mq_quiesce_queue() for that requirement.
  1227. */
  1228. void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
  1229. {
  1230. cancel_delayed_work(&hctx->run_work);
  1231. set_bit(BLK_MQ_S_STOPPED, &hctx->state);
  1232. }
  1233. EXPORT_SYMBOL(blk_mq_stop_hw_queue);
  1234. /*
  1235. * This function is often used for pausing .queue_rq() by driver when
  1236. * there isn't enough resource or some conditions aren't satisfied, and
  1237. * BLK_STS_RESOURCE is usually returned.
  1238. *
  1239. * We do not guarantee that dispatch can be drained or blocked
  1240. * after blk_mq_stop_hw_queues() returns. Please use
  1241. * blk_mq_quiesce_queue() for that requirement.
  1242. */
  1243. void blk_mq_stop_hw_queues(struct request_queue *q)
  1244. {
  1245. struct blk_mq_hw_ctx *hctx;
  1246. int i;
  1247. queue_for_each_hw_ctx(q, hctx, i)
  1248. blk_mq_stop_hw_queue(hctx);
  1249. }
  1250. EXPORT_SYMBOL(blk_mq_stop_hw_queues);
  1251. void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
  1252. {
  1253. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  1254. blk_mq_run_hw_queue(hctx, false);
  1255. }
  1256. EXPORT_SYMBOL(blk_mq_start_hw_queue);
  1257. void blk_mq_start_hw_queues(struct request_queue *q)
  1258. {
  1259. struct blk_mq_hw_ctx *hctx;
  1260. int i;
  1261. queue_for_each_hw_ctx(q, hctx, i)
  1262. blk_mq_start_hw_queue(hctx);
  1263. }
  1264. EXPORT_SYMBOL(blk_mq_start_hw_queues);
  1265. void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  1266. {
  1267. if (!blk_mq_hctx_stopped(hctx))
  1268. return;
  1269. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  1270. blk_mq_run_hw_queue(hctx, async);
  1271. }
  1272. EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
  1273. void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
  1274. {
  1275. struct blk_mq_hw_ctx *hctx;
  1276. int i;
  1277. queue_for_each_hw_ctx(q, hctx, i)
  1278. blk_mq_start_stopped_hw_queue(hctx, async);
  1279. }
  1280. EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
  1281. static void blk_mq_run_work_fn(struct work_struct *work)
  1282. {
  1283. struct blk_mq_hw_ctx *hctx;
  1284. hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
  1285. /*
  1286. * If we are stopped, don't run the queue.
  1287. */
  1288. if (test_bit(BLK_MQ_S_STOPPED, &hctx->state))
  1289. return;
  1290. __blk_mq_run_hw_queue(hctx);
  1291. }
  1292. static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
  1293. struct request *rq,
  1294. bool at_head)
  1295. {
  1296. struct blk_mq_ctx *ctx = rq->mq_ctx;
  1297. lockdep_assert_held(&ctx->lock);
  1298. trace_block_rq_insert(hctx->queue, rq);
  1299. if (at_head)
  1300. list_add(&rq->queuelist, &ctx->rq_list);
  1301. else
  1302. list_add_tail(&rq->queuelist, &ctx->rq_list);
  1303. }
  1304. void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
  1305. bool at_head)
  1306. {
  1307. struct blk_mq_ctx *ctx = rq->mq_ctx;
  1308. lockdep_assert_held(&ctx->lock);
  1309. __blk_mq_insert_req_list(hctx, rq, at_head);
  1310. blk_mq_hctx_mark_pending(hctx, ctx);
  1311. }
  1312. /*
  1313. * Should only be used carefully, when the caller knows we want to
  1314. * bypass a potential IO scheduler on the target device.
  1315. */
  1316. void blk_mq_request_bypass_insert(struct request *rq, bool run_queue)
  1317. {
  1318. struct blk_mq_ctx *ctx = rq->mq_ctx;
  1319. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(rq->q, ctx->cpu);
  1320. spin_lock(&hctx->lock);
  1321. list_add_tail(&rq->queuelist, &hctx->dispatch);
  1322. spin_unlock(&hctx->lock);
  1323. if (run_queue)
  1324. blk_mq_run_hw_queue(hctx, false);
  1325. }
  1326. void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
  1327. struct list_head *list)
  1328. {
  1329. struct request *rq;
  1330. /*
  1331. * preemption doesn't flush plug list, so it's possible ctx->cpu is
  1332. * offline now
  1333. */
  1334. list_for_each_entry(rq, list, queuelist) {
  1335. BUG_ON(rq->mq_ctx != ctx);
  1336. trace_block_rq_insert(hctx->queue, rq);
  1337. }
  1338. spin_lock(&ctx->lock);
  1339. list_splice_tail_init(list, &ctx->rq_list);
  1340. blk_mq_hctx_mark_pending(hctx, ctx);
  1341. spin_unlock(&ctx->lock);
  1342. }
  1343. static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
  1344. {
  1345. struct request *rqa = container_of(a, struct request, queuelist);
  1346. struct request *rqb = container_of(b, struct request, queuelist);
  1347. return !(rqa->mq_ctx < rqb->mq_ctx ||
  1348. (rqa->mq_ctx == rqb->mq_ctx &&
  1349. blk_rq_pos(rqa) < blk_rq_pos(rqb)));
  1350. }
  1351. void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  1352. {
  1353. struct blk_mq_ctx *this_ctx;
  1354. struct request_queue *this_q;
  1355. struct request *rq;
  1356. LIST_HEAD(list);
  1357. LIST_HEAD(ctx_list);
  1358. unsigned int depth;
  1359. list_splice_init(&plug->mq_list, &list);
  1360. list_sort(NULL, &list, plug_ctx_cmp);
  1361. this_q = NULL;
  1362. this_ctx = NULL;
  1363. depth = 0;
  1364. while (!list_empty(&list)) {
  1365. rq = list_entry_rq(list.next);
  1366. list_del_init(&rq->queuelist);
  1367. BUG_ON(!rq->q);
  1368. if (rq->mq_ctx != this_ctx) {
  1369. if (this_ctx) {
  1370. trace_block_unplug(this_q, depth, from_schedule);
  1371. blk_mq_sched_insert_requests(this_q, this_ctx,
  1372. &ctx_list,
  1373. from_schedule);
  1374. }
  1375. this_ctx = rq->mq_ctx;
  1376. this_q = rq->q;
  1377. depth = 0;
  1378. }
  1379. depth++;
  1380. list_add_tail(&rq->queuelist, &ctx_list);
  1381. }
  1382. /*
  1383. * If 'this_ctx' is set, we know we have entries to complete
  1384. * on 'ctx_list'. Do those.
  1385. */
  1386. if (this_ctx) {
  1387. trace_block_unplug(this_q, depth, from_schedule);
  1388. blk_mq_sched_insert_requests(this_q, this_ctx, &ctx_list,
  1389. from_schedule);
  1390. }
  1391. }
  1392. static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
  1393. {
  1394. blk_init_request_from_bio(rq, bio);
  1395. blk_rq_set_rl(rq, blk_get_rl(rq->q, bio));
  1396. blk_account_io_start(rq, true);
  1397. }
  1398. static blk_qc_t request_to_qc_t(struct blk_mq_hw_ctx *hctx, struct request *rq)
  1399. {
  1400. if (rq->tag != -1)
  1401. return blk_tag_to_qc_t(rq->tag, hctx->queue_num, false);
  1402. return blk_tag_to_qc_t(rq->internal_tag, hctx->queue_num, true);
  1403. }
  1404. static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
  1405. struct request *rq,
  1406. blk_qc_t *cookie)
  1407. {
  1408. struct request_queue *q = rq->q;
  1409. struct blk_mq_queue_data bd = {
  1410. .rq = rq,
  1411. .last = true,
  1412. };
  1413. blk_qc_t new_cookie;
  1414. blk_status_t ret;
  1415. new_cookie = request_to_qc_t(hctx, rq);
  1416. /*
  1417. * For OK queue, we are done. For error, caller may kill it.
  1418. * Any other error (busy), just add it to our list as we
  1419. * previously would have done.
  1420. */
  1421. ret = q->mq_ops->queue_rq(hctx, &bd);
  1422. switch (ret) {
  1423. case BLK_STS_OK:
  1424. *cookie = new_cookie;
  1425. break;
  1426. case BLK_STS_RESOURCE:
  1427. case BLK_STS_DEV_RESOURCE:
  1428. __blk_mq_requeue_request(rq);
  1429. break;
  1430. default:
  1431. *cookie = BLK_QC_T_NONE;
  1432. break;
  1433. }
  1434. return ret;
  1435. }
  1436. static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
  1437. struct request *rq,
  1438. blk_qc_t *cookie,
  1439. bool bypass_insert)
  1440. {
  1441. struct request_queue *q = rq->q;
  1442. bool run_queue = true;
  1443. /*
  1444. * RCU or SRCU read lock is needed before checking quiesced flag.
  1445. *
  1446. * When queue is stopped or quiesced, ignore 'bypass_insert' from
  1447. * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
  1448. * and avoid driver to try to dispatch again.
  1449. */
  1450. if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
  1451. run_queue = false;
  1452. bypass_insert = false;
  1453. goto insert;
  1454. }
  1455. if (q->elevator && !bypass_insert)
  1456. goto insert;
  1457. if (!blk_mq_get_dispatch_budget(hctx))
  1458. goto insert;
  1459. if (!blk_mq_get_driver_tag(rq)) {
  1460. blk_mq_put_dispatch_budget(hctx);
  1461. goto insert;
  1462. }
  1463. return __blk_mq_issue_directly(hctx, rq, cookie);
  1464. insert:
  1465. if (bypass_insert)
  1466. return BLK_STS_RESOURCE;
  1467. blk_mq_sched_insert_request(rq, false, run_queue, false);
  1468. return BLK_STS_OK;
  1469. }
  1470. static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
  1471. struct request *rq, blk_qc_t *cookie)
  1472. {
  1473. blk_status_t ret;
  1474. int srcu_idx;
  1475. might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
  1476. hctx_lock(hctx, &srcu_idx);
  1477. ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false);
  1478. if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
  1479. blk_mq_sched_insert_request(rq, false, true, false);
  1480. else if (ret != BLK_STS_OK)
  1481. blk_mq_end_request(rq, ret);
  1482. hctx_unlock(hctx, srcu_idx);
  1483. }
  1484. blk_status_t blk_mq_request_issue_directly(struct request *rq)
  1485. {
  1486. blk_status_t ret;
  1487. int srcu_idx;
  1488. blk_qc_t unused_cookie;
  1489. struct blk_mq_ctx *ctx = rq->mq_ctx;
  1490. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(rq->q, ctx->cpu);
  1491. hctx_lock(hctx, &srcu_idx);
  1492. ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true);
  1493. hctx_unlock(hctx, srcu_idx);
  1494. return ret;
  1495. }
  1496. static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
  1497. {
  1498. const int is_sync = op_is_sync(bio->bi_opf);
  1499. const int is_flush_fua = op_is_flush(bio->bi_opf);
  1500. struct blk_mq_alloc_data data = { .flags = 0 };
  1501. struct request *rq;
  1502. unsigned int request_count = 0;
  1503. struct blk_plug *plug;
  1504. struct request *same_queue_rq = NULL;
  1505. blk_qc_t cookie;
  1506. blk_queue_bounce(q, &bio);
  1507. blk_queue_split(q, &bio);
  1508. if (!bio_integrity_prep(bio))
  1509. return BLK_QC_T_NONE;
  1510. if (!is_flush_fua && !blk_queue_nomerges(q) &&
  1511. blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
  1512. return BLK_QC_T_NONE;
  1513. if (blk_mq_sched_bio_merge(q, bio))
  1514. return BLK_QC_T_NONE;
  1515. rq_qos_throttle(q, bio, NULL);
  1516. trace_block_getrq(q, bio, bio->bi_opf);
  1517. rq = blk_mq_get_request(q, bio, bio->bi_opf, &data);
  1518. if (unlikely(!rq)) {
  1519. rq_qos_cleanup(q, bio);
  1520. if (bio->bi_opf & REQ_NOWAIT)
  1521. bio_wouldblock_error(bio);
  1522. return BLK_QC_T_NONE;
  1523. }
  1524. rq_qos_track(q, rq, bio);
  1525. cookie = request_to_qc_t(data.hctx, rq);
  1526. plug = current->plug;
  1527. if (unlikely(is_flush_fua)) {
  1528. blk_mq_put_ctx(data.ctx);
  1529. blk_mq_bio_to_request(rq, bio);
  1530. /* bypass scheduler for flush rq */
  1531. blk_insert_flush(rq);
  1532. blk_mq_run_hw_queue(data.hctx, true);
  1533. } else if (plug && q->nr_hw_queues == 1) {
  1534. struct request *last = NULL;
  1535. blk_mq_put_ctx(data.ctx);
  1536. blk_mq_bio_to_request(rq, bio);
  1537. /*
  1538. * @request_count may become stale because of schedule
  1539. * out, so check the list again.
  1540. */
  1541. if (list_empty(&plug->mq_list))
  1542. request_count = 0;
  1543. else if (blk_queue_nomerges(q))
  1544. request_count = blk_plug_queued_count(q);
  1545. if (!request_count)
  1546. trace_block_plug(q);
  1547. else
  1548. last = list_entry_rq(plug->mq_list.prev);
  1549. if (request_count >= BLK_MAX_REQUEST_COUNT || (last &&
  1550. blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
  1551. blk_flush_plug_list(plug, false);
  1552. trace_block_plug(q);
  1553. }
  1554. list_add_tail(&rq->queuelist, &plug->mq_list);
  1555. } else if (plug && !blk_queue_nomerges(q)) {
  1556. blk_mq_bio_to_request(rq, bio);
  1557. /*
  1558. * We do limited plugging. If the bio can be merged, do that.
  1559. * Otherwise the existing request in the plug list will be
  1560. * issued. So the plug list will have one request at most
  1561. * The plug list might get flushed before this. If that happens,
  1562. * the plug list is empty, and same_queue_rq is invalid.
  1563. */
  1564. if (list_empty(&plug->mq_list))
  1565. same_queue_rq = NULL;
  1566. if (same_queue_rq)
  1567. list_del_init(&same_queue_rq->queuelist);
  1568. list_add_tail(&rq->queuelist, &plug->mq_list);
  1569. blk_mq_put_ctx(data.ctx);
  1570. if (same_queue_rq) {
  1571. data.hctx = blk_mq_map_queue(q,
  1572. same_queue_rq->mq_ctx->cpu);
  1573. blk_mq_try_issue_directly(data.hctx, same_queue_rq,
  1574. &cookie);
  1575. }
  1576. } else if (q->nr_hw_queues > 1 && is_sync) {
  1577. blk_mq_put_ctx(data.ctx);
  1578. blk_mq_bio_to_request(rq, bio);
  1579. blk_mq_try_issue_directly(data.hctx, rq, &cookie);
  1580. } else {
  1581. blk_mq_put_ctx(data.ctx);
  1582. blk_mq_bio_to_request(rq, bio);
  1583. blk_mq_sched_insert_request(rq, false, true, true);
  1584. }
  1585. return cookie;
  1586. }
  1587. void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
  1588. unsigned int hctx_idx)
  1589. {
  1590. struct page *page;
  1591. if (tags->rqs && set->ops->exit_request) {
  1592. int i;
  1593. for (i = 0; i < tags->nr_tags; i++) {
  1594. struct request *rq = tags->static_rqs[i];
  1595. if (!rq)
  1596. continue;
  1597. set->ops->exit_request(set, rq, hctx_idx);
  1598. tags->static_rqs[i] = NULL;
  1599. }
  1600. }
  1601. while (!list_empty(&tags->page_list)) {
  1602. page = list_first_entry(&tags->page_list, struct page, lru);
  1603. list_del_init(&page->lru);
  1604. /*
  1605. * Remove kmemleak object previously allocated in
  1606. * blk_mq_init_rq_map().
  1607. */
  1608. kmemleak_free(page_address(page));
  1609. __free_pages(page, page->private);
  1610. }
  1611. }
  1612. void blk_mq_free_rq_map(struct blk_mq_tags *tags)
  1613. {
  1614. kfree(tags->rqs);
  1615. tags->rqs = NULL;
  1616. kfree(tags->static_rqs);
  1617. tags->static_rqs = NULL;
  1618. blk_mq_free_tags(tags);
  1619. }
  1620. struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
  1621. unsigned int hctx_idx,
  1622. unsigned int nr_tags,
  1623. unsigned int reserved_tags)
  1624. {
  1625. struct blk_mq_tags *tags;
  1626. int node;
  1627. node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx);
  1628. if (node == NUMA_NO_NODE)
  1629. node = set->numa_node;
  1630. tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
  1631. BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
  1632. if (!tags)
  1633. return NULL;
  1634. tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
  1635. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
  1636. node);
  1637. if (!tags->rqs) {
  1638. blk_mq_free_tags(tags);
  1639. return NULL;
  1640. }
  1641. tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
  1642. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
  1643. node);
  1644. if (!tags->static_rqs) {
  1645. kfree(tags->rqs);
  1646. blk_mq_free_tags(tags);
  1647. return NULL;
  1648. }
  1649. return tags;
  1650. }
  1651. static size_t order_to_size(unsigned int order)
  1652. {
  1653. return (size_t)PAGE_SIZE << order;
  1654. }
  1655. static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
  1656. unsigned int hctx_idx, int node)
  1657. {
  1658. int ret;
  1659. if (set->ops->init_request) {
  1660. ret = set->ops->init_request(set, rq, hctx_idx, node);
  1661. if (ret)
  1662. return ret;
  1663. }
  1664. WRITE_ONCE(rq->state, MQ_RQ_IDLE);
  1665. return 0;
  1666. }
  1667. int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
  1668. unsigned int hctx_idx, unsigned int depth)
  1669. {
  1670. unsigned int i, j, entries_per_page, max_order = 4;
  1671. size_t rq_size, left;
  1672. int node;
  1673. node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx);
  1674. if (node == NUMA_NO_NODE)
  1675. node = set->numa_node;
  1676. INIT_LIST_HEAD(&tags->page_list);
  1677. /*
  1678. * rq_size is the size of the request plus driver payload, rounded
  1679. * to the cacheline size
  1680. */
  1681. rq_size = round_up(sizeof(struct request) + set->cmd_size,
  1682. cache_line_size());
  1683. left = rq_size * depth;
  1684. for (i = 0; i < depth; ) {
  1685. int this_order = max_order;
  1686. struct page *page;
  1687. int to_do;
  1688. void *p;
  1689. while (this_order && left < order_to_size(this_order - 1))
  1690. this_order--;
  1691. do {
  1692. page = alloc_pages_node(node,
  1693. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
  1694. this_order);
  1695. if (page)
  1696. break;
  1697. if (!this_order--)
  1698. break;
  1699. if (order_to_size(this_order) < rq_size)
  1700. break;
  1701. } while (1);
  1702. if (!page)
  1703. goto fail;
  1704. page->private = this_order;
  1705. list_add_tail(&page->lru, &tags->page_list);
  1706. p = page_address(page);
  1707. /*
  1708. * Allow kmemleak to scan these pages as they contain pointers
  1709. * to additional allocations like via ops->init_request().
  1710. */
  1711. kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
  1712. entries_per_page = order_to_size(this_order) / rq_size;
  1713. to_do = min(entries_per_page, depth - i);
  1714. left -= to_do * rq_size;
  1715. for (j = 0; j < to_do; j++) {
  1716. struct request *rq = p;
  1717. tags->static_rqs[i] = rq;
  1718. if (blk_mq_init_request(set, rq, hctx_idx, node)) {
  1719. tags->static_rqs[i] = NULL;
  1720. goto fail;
  1721. }
  1722. p += rq_size;
  1723. i++;
  1724. }
  1725. }
  1726. return 0;
  1727. fail:
  1728. blk_mq_free_rqs(set, tags, hctx_idx);
  1729. return -ENOMEM;
  1730. }
  1731. /*
  1732. * 'cpu' is going away. splice any existing rq_list entries from this
  1733. * software queue to the hw queue dispatch list, and ensure that it
  1734. * gets run.
  1735. */
  1736. static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
  1737. {
  1738. struct blk_mq_hw_ctx *hctx;
  1739. struct blk_mq_ctx *ctx;
  1740. LIST_HEAD(tmp);
  1741. hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
  1742. ctx = __blk_mq_get_ctx(hctx->queue, cpu);
  1743. spin_lock(&ctx->lock);
  1744. if (!list_empty(&ctx->rq_list)) {
  1745. list_splice_init(&ctx->rq_list, &tmp);
  1746. blk_mq_hctx_clear_pending(hctx, ctx);
  1747. }
  1748. spin_unlock(&ctx->lock);
  1749. if (list_empty(&tmp))
  1750. return 0;
  1751. spin_lock(&hctx->lock);
  1752. list_splice_tail_init(&tmp, &hctx->dispatch);
  1753. spin_unlock(&hctx->lock);
  1754. blk_mq_run_hw_queue(hctx, true);
  1755. return 0;
  1756. }
  1757. static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
  1758. {
  1759. cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
  1760. &hctx->cpuhp_dead);
  1761. }
  1762. /* hctx->ctxs will be freed in queue's release handler */
  1763. static void blk_mq_exit_hctx(struct request_queue *q,
  1764. struct blk_mq_tag_set *set,
  1765. struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
  1766. {
  1767. blk_mq_debugfs_unregister_hctx(hctx);
  1768. if (blk_mq_hw_queue_mapped(hctx))
  1769. blk_mq_tag_idle(hctx);
  1770. if (set->ops->exit_request)
  1771. set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx);
  1772. blk_mq_sched_exit_hctx(q, hctx, hctx_idx);
  1773. if (set->ops->exit_hctx)
  1774. set->ops->exit_hctx(hctx, hctx_idx);
  1775. if (hctx->flags & BLK_MQ_F_BLOCKING)
  1776. cleanup_srcu_struct(hctx->srcu);
  1777. blk_mq_remove_cpuhp(hctx);
  1778. blk_free_flush_queue(hctx->fq);
  1779. sbitmap_free(&hctx->ctx_map);
  1780. }
  1781. static void blk_mq_exit_hw_queues(struct request_queue *q,
  1782. struct blk_mq_tag_set *set, int nr_queue)
  1783. {
  1784. struct blk_mq_hw_ctx *hctx;
  1785. unsigned int i;
  1786. queue_for_each_hw_ctx(q, hctx, i) {
  1787. if (i == nr_queue)
  1788. break;
  1789. blk_mq_exit_hctx(q, set, hctx, i);
  1790. }
  1791. }
  1792. static int blk_mq_init_hctx(struct request_queue *q,
  1793. struct blk_mq_tag_set *set,
  1794. struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
  1795. {
  1796. int node;
  1797. node = hctx->numa_node;
  1798. if (node == NUMA_NO_NODE)
  1799. node = hctx->numa_node = set->numa_node;
  1800. INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
  1801. spin_lock_init(&hctx->lock);
  1802. INIT_LIST_HEAD(&hctx->dispatch);
  1803. hctx->queue = q;
  1804. hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
  1805. cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
  1806. hctx->tags = set->tags[hctx_idx];
  1807. /*
  1808. * Allocate space for all possible cpus to avoid allocation at
  1809. * runtime
  1810. */
  1811. hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
  1812. GFP_KERNEL, node);
  1813. if (!hctx->ctxs)
  1814. goto unregister_cpu_notifier;
  1815. if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), GFP_KERNEL,
  1816. node))
  1817. goto free_ctxs;
  1818. hctx->nr_ctx = 0;
  1819. spin_lock_init(&hctx->dispatch_wait_lock);
  1820. init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
  1821. INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
  1822. if (set->ops->init_hctx &&
  1823. set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
  1824. goto free_bitmap;
  1825. if (blk_mq_sched_init_hctx(q, hctx, hctx_idx))
  1826. goto exit_hctx;
  1827. hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size);
  1828. if (!hctx->fq)
  1829. goto sched_exit_hctx;
  1830. if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx, node))
  1831. goto free_fq;
  1832. if (hctx->flags & BLK_MQ_F_BLOCKING)
  1833. init_srcu_struct(hctx->srcu);
  1834. blk_mq_debugfs_register_hctx(q, hctx);
  1835. return 0;
  1836. free_fq:
  1837. kfree(hctx->fq);
  1838. sched_exit_hctx:
  1839. blk_mq_sched_exit_hctx(q, hctx, hctx_idx);
  1840. exit_hctx:
  1841. if (set->ops->exit_hctx)
  1842. set->ops->exit_hctx(hctx, hctx_idx);
  1843. free_bitmap:
  1844. sbitmap_free(&hctx->ctx_map);
  1845. free_ctxs:
  1846. kfree(hctx->ctxs);
  1847. unregister_cpu_notifier:
  1848. blk_mq_remove_cpuhp(hctx);
  1849. return -1;
  1850. }
  1851. static void blk_mq_init_cpu_queues(struct request_queue *q,
  1852. unsigned int nr_hw_queues)
  1853. {
  1854. unsigned int i;
  1855. for_each_possible_cpu(i) {
  1856. struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
  1857. struct blk_mq_hw_ctx *hctx;
  1858. __ctx->cpu = i;
  1859. spin_lock_init(&__ctx->lock);
  1860. INIT_LIST_HEAD(&__ctx->rq_list);
  1861. __ctx->queue = q;
  1862. /*
  1863. * Set local node, IFF we have more than one hw queue. If
  1864. * not, we remain on the home node of the device
  1865. */
  1866. hctx = blk_mq_map_queue(q, i);
  1867. if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
  1868. hctx->numa_node = local_memory_node(cpu_to_node(i));
  1869. }
  1870. }
  1871. static bool __blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, int hctx_idx)
  1872. {
  1873. int ret = 0;
  1874. set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
  1875. set->queue_depth, set->reserved_tags);
  1876. if (!set->tags[hctx_idx])
  1877. return false;
  1878. ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
  1879. set->queue_depth);
  1880. if (!ret)
  1881. return true;
  1882. blk_mq_free_rq_map(set->tags[hctx_idx]);
  1883. set->tags[hctx_idx] = NULL;
  1884. return false;
  1885. }
  1886. static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
  1887. unsigned int hctx_idx)
  1888. {
  1889. if (set->tags[hctx_idx]) {
  1890. blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
  1891. blk_mq_free_rq_map(set->tags[hctx_idx]);
  1892. set->tags[hctx_idx] = NULL;
  1893. }
  1894. }
  1895. static void blk_mq_map_swqueue(struct request_queue *q)
  1896. {
  1897. unsigned int i, hctx_idx;
  1898. struct blk_mq_hw_ctx *hctx;
  1899. struct blk_mq_ctx *ctx;
  1900. struct blk_mq_tag_set *set = q->tag_set;
  1901. /*
  1902. * Avoid others reading imcomplete hctx->cpumask through sysfs
  1903. */
  1904. mutex_lock(&q->sysfs_lock);
  1905. queue_for_each_hw_ctx(q, hctx, i) {
  1906. cpumask_clear(hctx->cpumask);
  1907. hctx->nr_ctx = 0;
  1908. hctx->dispatch_from = NULL;
  1909. }
  1910. /*
  1911. * Map software to hardware queues.
  1912. *
  1913. * If the cpu isn't present, the cpu is mapped to first hctx.
  1914. */
  1915. for_each_possible_cpu(i) {
  1916. hctx_idx = q->mq_map[i];
  1917. /* unmapped hw queue can be remapped after CPU topo changed */
  1918. if (!set->tags[hctx_idx] &&
  1919. !__blk_mq_alloc_rq_map(set, hctx_idx)) {
  1920. /*
  1921. * If tags initialization fail for some hctx,
  1922. * that hctx won't be brought online. In this
  1923. * case, remap the current ctx to hctx[0] which
  1924. * is guaranteed to always have tags allocated
  1925. */
  1926. q->mq_map[i] = 0;
  1927. }
  1928. ctx = per_cpu_ptr(q->queue_ctx, i);
  1929. hctx = blk_mq_map_queue(q, i);
  1930. cpumask_set_cpu(i, hctx->cpumask);
  1931. ctx->index_hw = hctx->nr_ctx;
  1932. hctx->ctxs[hctx->nr_ctx++] = ctx;
  1933. }
  1934. mutex_unlock(&q->sysfs_lock);
  1935. queue_for_each_hw_ctx(q, hctx, i) {
  1936. /*
  1937. * If no software queues are mapped to this hardware queue,
  1938. * disable it and free the request entries.
  1939. */
  1940. if (!hctx->nr_ctx) {
  1941. /* Never unmap queue 0. We need it as a
  1942. * fallback in case of a new remap fails
  1943. * allocation
  1944. */
  1945. if (i && set->tags[i])
  1946. blk_mq_free_map_and_requests(set, i);
  1947. hctx->tags = NULL;
  1948. continue;
  1949. }
  1950. hctx->tags = set->tags[i];
  1951. WARN_ON(!hctx->tags);
  1952. /*
  1953. * Set the map size to the number of mapped software queues.
  1954. * This is more accurate and more efficient than looping
  1955. * over all possibly mapped software queues.
  1956. */
  1957. sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
  1958. /*
  1959. * Initialize batch roundrobin counts
  1960. */
  1961. hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
  1962. hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
  1963. }
  1964. }
  1965. /*
  1966. * Caller needs to ensure that we're either frozen/quiesced, or that
  1967. * the queue isn't live yet.
  1968. */
  1969. static void queue_set_hctx_shared(struct request_queue *q, bool shared)
  1970. {
  1971. struct blk_mq_hw_ctx *hctx;
  1972. int i;
  1973. queue_for_each_hw_ctx(q, hctx, i) {
  1974. if (shared)
  1975. hctx->flags |= BLK_MQ_F_TAG_SHARED;
  1976. else
  1977. hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
  1978. }
  1979. }
  1980. static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
  1981. bool shared)
  1982. {
  1983. struct request_queue *q;
  1984. lockdep_assert_held(&set->tag_list_lock);
  1985. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  1986. blk_mq_freeze_queue(q);
  1987. queue_set_hctx_shared(q, shared);
  1988. blk_mq_unfreeze_queue(q);
  1989. }
  1990. }
  1991. static void blk_mq_del_queue_tag_set(struct request_queue *q)
  1992. {
  1993. struct blk_mq_tag_set *set = q->tag_set;
  1994. mutex_lock(&set->tag_list_lock);
  1995. list_del_rcu(&q->tag_set_list);
  1996. if (list_is_singular(&set->tag_list)) {
  1997. /* just transitioned to unshared */
  1998. set->flags &= ~BLK_MQ_F_TAG_SHARED;
  1999. /* update existing queue */
  2000. blk_mq_update_tag_set_depth(set, false);
  2001. }
  2002. mutex_unlock(&set->tag_list_lock);
  2003. INIT_LIST_HEAD(&q->tag_set_list);
  2004. }
  2005. static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
  2006. struct request_queue *q)
  2007. {
  2008. q->tag_set = set;
  2009. mutex_lock(&set->tag_list_lock);
  2010. /*
  2011. * Check to see if we're transitioning to shared (from 1 to 2 queues).
  2012. */
  2013. if (!list_empty(&set->tag_list) &&
  2014. !(set->flags & BLK_MQ_F_TAG_SHARED)) {
  2015. set->flags |= BLK_MQ_F_TAG_SHARED;
  2016. /* update existing queue */
  2017. blk_mq_update_tag_set_depth(set, true);
  2018. }
  2019. if (set->flags & BLK_MQ_F_TAG_SHARED)
  2020. queue_set_hctx_shared(q, true);
  2021. list_add_tail_rcu(&q->tag_set_list, &set->tag_list);
  2022. mutex_unlock(&set->tag_list_lock);
  2023. }
  2024. /*
  2025. * It is the actual release handler for mq, but we do it from
  2026. * request queue's release handler for avoiding use-after-free
  2027. * and headache because q->mq_kobj shouldn't have been introduced,
  2028. * but we can't group ctx/kctx kobj without it.
  2029. */
  2030. void blk_mq_release(struct request_queue *q)
  2031. {
  2032. struct blk_mq_hw_ctx *hctx;
  2033. unsigned int i;
  2034. /* hctx kobj stays in hctx */
  2035. queue_for_each_hw_ctx(q, hctx, i) {
  2036. if (!hctx)
  2037. continue;
  2038. kobject_put(&hctx->kobj);
  2039. }
  2040. q->mq_map = NULL;
  2041. kfree(q->queue_hw_ctx);
  2042. /*
  2043. * release .mq_kobj and sw queue's kobject now because
  2044. * both share lifetime with request queue.
  2045. */
  2046. blk_mq_sysfs_deinit(q);
  2047. free_percpu(q->queue_ctx);
  2048. }
  2049. struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
  2050. {
  2051. struct request_queue *uninit_q, *q;
  2052. uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node, NULL);
  2053. if (!uninit_q)
  2054. return ERR_PTR(-ENOMEM);
  2055. q = blk_mq_init_allocated_queue(set, uninit_q);
  2056. if (IS_ERR(q))
  2057. blk_cleanup_queue(uninit_q);
  2058. return q;
  2059. }
  2060. EXPORT_SYMBOL(blk_mq_init_queue);
  2061. static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
  2062. {
  2063. int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
  2064. BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
  2065. __alignof__(struct blk_mq_hw_ctx)) !=
  2066. sizeof(struct blk_mq_hw_ctx));
  2067. if (tag_set->flags & BLK_MQ_F_BLOCKING)
  2068. hw_ctx_size += sizeof(struct srcu_struct);
  2069. return hw_ctx_size;
  2070. }
  2071. static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
  2072. struct request_queue *q)
  2073. {
  2074. int i, j;
  2075. struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
  2076. blk_mq_sysfs_unregister(q);
  2077. /* protect against switching io scheduler */
  2078. mutex_lock(&q->sysfs_lock);
  2079. for (i = 0; i < set->nr_hw_queues; i++) {
  2080. int node;
  2081. if (hctxs[i])
  2082. continue;
  2083. node = blk_mq_hw_queue_to_node(q->mq_map, i);
  2084. hctxs[i] = kzalloc_node(blk_mq_hw_ctx_size(set),
  2085. GFP_KERNEL, node);
  2086. if (!hctxs[i])
  2087. break;
  2088. if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL,
  2089. node)) {
  2090. kfree(hctxs[i]);
  2091. hctxs[i] = NULL;
  2092. break;
  2093. }
  2094. atomic_set(&hctxs[i]->nr_active, 0);
  2095. hctxs[i]->numa_node = node;
  2096. hctxs[i]->queue_num = i;
  2097. if (blk_mq_init_hctx(q, set, hctxs[i], i)) {
  2098. free_cpumask_var(hctxs[i]->cpumask);
  2099. kfree(hctxs[i]);
  2100. hctxs[i] = NULL;
  2101. break;
  2102. }
  2103. blk_mq_hctx_kobj_init(hctxs[i]);
  2104. }
  2105. for (j = i; j < q->nr_hw_queues; j++) {
  2106. struct blk_mq_hw_ctx *hctx = hctxs[j];
  2107. if (hctx) {
  2108. if (hctx->tags)
  2109. blk_mq_free_map_and_requests(set, j);
  2110. blk_mq_exit_hctx(q, set, hctx, j);
  2111. kobject_put(&hctx->kobj);
  2112. hctxs[j] = NULL;
  2113. }
  2114. }
  2115. q->nr_hw_queues = i;
  2116. mutex_unlock(&q->sysfs_lock);
  2117. blk_mq_sysfs_register(q);
  2118. }
  2119. struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
  2120. struct request_queue *q)
  2121. {
  2122. /* mark the queue as mq asap */
  2123. q->mq_ops = set->ops;
  2124. q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
  2125. blk_mq_poll_stats_bkt,
  2126. BLK_MQ_POLL_STATS_BKTS, q);
  2127. if (!q->poll_cb)
  2128. goto err_exit;
  2129. q->queue_ctx = alloc_percpu(struct blk_mq_ctx);
  2130. if (!q->queue_ctx)
  2131. goto err_exit;
  2132. /* init q->mq_kobj and sw queues' kobjects */
  2133. blk_mq_sysfs_init(q);
  2134. q->queue_hw_ctx = kcalloc_node(nr_cpu_ids, sizeof(*(q->queue_hw_ctx)),
  2135. GFP_KERNEL, set->numa_node);
  2136. if (!q->queue_hw_ctx)
  2137. goto err_percpu;
  2138. q->mq_map = set->mq_map;
  2139. blk_mq_realloc_hw_ctxs(set, q);
  2140. if (!q->nr_hw_queues)
  2141. goto err_hctxs;
  2142. INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
  2143. blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
  2144. q->nr_queues = nr_cpu_ids;
  2145. q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
  2146. if (!(set->flags & BLK_MQ_F_SG_MERGE))
  2147. queue_flag_set_unlocked(QUEUE_FLAG_NO_SG_MERGE, q);
  2148. q->sg_reserved_size = INT_MAX;
  2149. INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
  2150. INIT_LIST_HEAD(&q->requeue_list);
  2151. spin_lock_init(&q->requeue_lock);
  2152. blk_queue_make_request(q, blk_mq_make_request);
  2153. if (q->mq_ops->poll)
  2154. q->poll_fn = blk_mq_poll;
  2155. /*
  2156. * Do this after blk_queue_make_request() overrides it...
  2157. */
  2158. q->nr_requests = set->queue_depth;
  2159. /*
  2160. * Default to classic polling
  2161. */
  2162. q->poll_nsec = -1;
  2163. if (set->ops->complete)
  2164. blk_queue_softirq_done(q, set->ops->complete);
  2165. blk_mq_init_cpu_queues(q, set->nr_hw_queues);
  2166. blk_mq_add_queue_tag_set(set, q);
  2167. blk_mq_map_swqueue(q);
  2168. if (!(set->flags & BLK_MQ_F_NO_SCHED)) {
  2169. int ret;
  2170. ret = elevator_init_mq(q);
  2171. if (ret)
  2172. return ERR_PTR(ret);
  2173. }
  2174. return q;
  2175. err_hctxs:
  2176. kfree(q->queue_hw_ctx);
  2177. err_percpu:
  2178. free_percpu(q->queue_ctx);
  2179. err_exit:
  2180. q->mq_ops = NULL;
  2181. return ERR_PTR(-ENOMEM);
  2182. }
  2183. EXPORT_SYMBOL(blk_mq_init_allocated_queue);
  2184. void blk_mq_free_queue(struct request_queue *q)
  2185. {
  2186. struct blk_mq_tag_set *set = q->tag_set;
  2187. blk_mq_del_queue_tag_set(q);
  2188. blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
  2189. }
  2190. /* Basically redo blk_mq_init_queue with queue frozen */
  2191. static void blk_mq_queue_reinit(struct request_queue *q)
  2192. {
  2193. WARN_ON_ONCE(!atomic_read(&q->mq_freeze_depth));
  2194. blk_mq_debugfs_unregister_hctxs(q);
  2195. blk_mq_sysfs_unregister(q);
  2196. /*
  2197. * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
  2198. * we should change hctx numa_node according to the new topology (this
  2199. * involves freeing and re-allocating memory, worth doing?)
  2200. */
  2201. blk_mq_map_swqueue(q);
  2202. blk_mq_sysfs_register(q);
  2203. blk_mq_debugfs_register_hctxs(q);
  2204. }
  2205. static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
  2206. {
  2207. int i;
  2208. for (i = 0; i < set->nr_hw_queues; i++)
  2209. if (!__blk_mq_alloc_rq_map(set, i))
  2210. goto out_unwind;
  2211. return 0;
  2212. out_unwind:
  2213. while (--i >= 0)
  2214. blk_mq_free_rq_map(set->tags[i]);
  2215. return -ENOMEM;
  2216. }
  2217. /*
  2218. * Allocate the request maps associated with this tag_set. Note that this
  2219. * may reduce the depth asked for, if memory is tight. set->queue_depth
  2220. * will be updated to reflect the allocated depth.
  2221. */
  2222. static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
  2223. {
  2224. unsigned int depth;
  2225. int err;
  2226. depth = set->queue_depth;
  2227. do {
  2228. err = __blk_mq_alloc_rq_maps(set);
  2229. if (!err)
  2230. break;
  2231. set->queue_depth >>= 1;
  2232. if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
  2233. err = -ENOMEM;
  2234. break;
  2235. }
  2236. } while (set->queue_depth);
  2237. if (!set->queue_depth || err) {
  2238. pr_err("blk-mq: failed to allocate request map\n");
  2239. return -ENOMEM;
  2240. }
  2241. if (depth != set->queue_depth)
  2242. pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
  2243. depth, set->queue_depth);
  2244. return 0;
  2245. }
  2246. static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
  2247. {
  2248. if (set->ops->map_queues) {
  2249. /*
  2250. * transport .map_queues is usually done in the following
  2251. * way:
  2252. *
  2253. * for (queue = 0; queue < set->nr_hw_queues; queue++) {
  2254. * mask = get_cpu_mask(queue)
  2255. * for_each_cpu(cpu, mask)
  2256. * set->mq_map[cpu] = queue;
  2257. * }
  2258. *
  2259. * When we need to remap, the table has to be cleared for
  2260. * killing stale mapping since one CPU may not be mapped
  2261. * to any hw queue.
  2262. */
  2263. blk_mq_clear_mq_map(set);
  2264. return set->ops->map_queues(set);
  2265. } else
  2266. return blk_mq_map_queues(set);
  2267. }
  2268. /*
  2269. * Alloc a tag set to be associated with one or more request queues.
  2270. * May fail with EINVAL for various error conditions. May adjust the
  2271. * requested depth down, if it's too large. In that case, the set
  2272. * value will be stored in set->queue_depth.
  2273. */
  2274. int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
  2275. {
  2276. int ret;
  2277. BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
  2278. if (!set->nr_hw_queues)
  2279. return -EINVAL;
  2280. if (!set->queue_depth)
  2281. return -EINVAL;
  2282. if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
  2283. return -EINVAL;
  2284. if (!set->ops->queue_rq)
  2285. return -EINVAL;
  2286. if (!set->ops->get_budget ^ !set->ops->put_budget)
  2287. return -EINVAL;
  2288. if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
  2289. pr_info("blk-mq: reduced tag depth to %u\n",
  2290. BLK_MQ_MAX_DEPTH);
  2291. set->queue_depth = BLK_MQ_MAX_DEPTH;
  2292. }
  2293. /*
  2294. * If a crashdump is active, then we are potentially in a very
  2295. * memory constrained environment. Limit us to 1 queue and
  2296. * 64 tags to prevent using too much memory.
  2297. */
  2298. if (is_kdump_kernel()) {
  2299. set->nr_hw_queues = 1;
  2300. set->queue_depth = min(64U, set->queue_depth);
  2301. }
  2302. /*
  2303. * There is no use for more h/w queues than cpus.
  2304. */
  2305. if (set->nr_hw_queues > nr_cpu_ids)
  2306. set->nr_hw_queues = nr_cpu_ids;
  2307. set->tags = kcalloc_node(nr_cpu_ids, sizeof(struct blk_mq_tags *),
  2308. GFP_KERNEL, set->numa_node);
  2309. if (!set->tags)
  2310. return -ENOMEM;
  2311. ret = -ENOMEM;
  2312. set->mq_map = kcalloc_node(nr_cpu_ids, sizeof(*set->mq_map),
  2313. GFP_KERNEL, set->numa_node);
  2314. if (!set->mq_map)
  2315. goto out_free_tags;
  2316. ret = blk_mq_update_queue_map(set);
  2317. if (ret)
  2318. goto out_free_mq_map;
  2319. ret = blk_mq_alloc_rq_maps(set);
  2320. if (ret)
  2321. goto out_free_mq_map;
  2322. mutex_init(&set->tag_list_lock);
  2323. INIT_LIST_HEAD(&set->tag_list);
  2324. return 0;
  2325. out_free_mq_map:
  2326. kfree(set->mq_map);
  2327. set->mq_map = NULL;
  2328. out_free_tags:
  2329. kfree(set->tags);
  2330. set->tags = NULL;
  2331. return ret;
  2332. }
  2333. EXPORT_SYMBOL(blk_mq_alloc_tag_set);
  2334. void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
  2335. {
  2336. int i;
  2337. for (i = 0; i < nr_cpu_ids; i++)
  2338. blk_mq_free_map_and_requests(set, i);
  2339. kfree(set->mq_map);
  2340. set->mq_map = NULL;
  2341. kfree(set->tags);
  2342. set->tags = NULL;
  2343. }
  2344. EXPORT_SYMBOL(blk_mq_free_tag_set);
  2345. int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
  2346. {
  2347. struct blk_mq_tag_set *set = q->tag_set;
  2348. struct blk_mq_hw_ctx *hctx;
  2349. int i, ret;
  2350. if (!set)
  2351. return -EINVAL;
  2352. blk_mq_freeze_queue(q);
  2353. blk_mq_quiesce_queue(q);
  2354. ret = 0;
  2355. queue_for_each_hw_ctx(q, hctx, i) {
  2356. if (!hctx->tags)
  2357. continue;
  2358. /*
  2359. * If we're using an MQ scheduler, just update the scheduler
  2360. * queue depth. This is similar to what the old code would do.
  2361. */
  2362. if (!hctx->sched_tags) {
  2363. ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
  2364. false);
  2365. } else {
  2366. ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
  2367. nr, true);
  2368. }
  2369. if (ret)
  2370. break;
  2371. }
  2372. if (!ret)
  2373. q->nr_requests = nr;
  2374. blk_mq_unquiesce_queue(q);
  2375. blk_mq_unfreeze_queue(q);
  2376. return ret;
  2377. }
  2378. static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
  2379. int nr_hw_queues)
  2380. {
  2381. struct request_queue *q;
  2382. lockdep_assert_held(&set->tag_list_lock);
  2383. if (nr_hw_queues > nr_cpu_ids)
  2384. nr_hw_queues = nr_cpu_ids;
  2385. if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
  2386. return;
  2387. list_for_each_entry(q, &set->tag_list, tag_set_list)
  2388. blk_mq_freeze_queue(q);
  2389. set->nr_hw_queues = nr_hw_queues;
  2390. blk_mq_update_queue_map(set);
  2391. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  2392. blk_mq_realloc_hw_ctxs(set, q);
  2393. blk_mq_queue_reinit(q);
  2394. }
  2395. list_for_each_entry(q, &set->tag_list, tag_set_list)
  2396. blk_mq_unfreeze_queue(q);
  2397. }
  2398. void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
  2399. {
  2400. mutex_lock(&set->tag_list_lock);
  2401. __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
  2402. mutex_unlock(&set->tag_list_lock);
  2403. }
  2404. EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
  2405. /* Enable polling stats and return whether they were already enabled. */
  2406. static bool blk_poll_stats_enable(struct request_queue *q)
  2407. {
  2408. if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
  2409. blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
  2410. return true;
  2411. blk_stat_add_callback(q, q->poll_cb);
  2412. return false;
  2413. }
  2414. static void blk_mq_poll_stats_start(struct request_queue *q)
  2415. {
  2416. /*
  2417. * We don't arm the callback if polling stats are not enabled or the
  2418. * callback is already active.
  2419. */
  2420. if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
  2421. blk_stat_is_active(q->poll_cb))
  2422. return;
  2423. blk_stat_activate_msecs(q->poll_cb, 100);
  2424. }
  2425. static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
  2426. {
  2427. struct request_queue *q = cb->data;
  2428. int bucket;
  2429. for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
  2430. if (cb->stat[bucket].nr_samples)
  2431. q->poll_stat[bucket] = cb->stat[bucket];
  2432. }
  2433. }
  2434. static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
  2435. struct blk_mq_hw_ctx *hctx,
  2436. struct request *rq)
  2437. {
  2438. unsigned long ret = 0;
  2439. int bucket;
  2440. /*
  2441. * If stats collection isn't on, don't sleep but turn it on for
  2442. * future users
  2443. */
  2444. if (!blk_poll_stats_enable(q))
  2445. return 0;
  2446. /*
  2447. * As an optimistic guess, use half of the mean service time
  2448. * for this type of request. We can (and should) make this smarter.
  2449. * For instance, if the completion latencies are tight, we can
  2450. * get closer than just half the mean. This is especially
  2451. * important on devices where the completion latencies are longer
  2452. * than ~10 usec. We do use the stats for the relevant IO size
  2453. * if available which does lead to better estimates.
  2454. */
  2455. bucket = blk_mq_poll_stats_bkt(rq);
  2456. if (bucket < 0)
  2457. return ret;
  2458. if (q->poll_stat[bucket].nr_samples)
  2459. ret = (q->poll_stat[bucket].mean + 1) / 2;
  2460. return ret;
  2461. }
  2462. static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
  2463. struct blk_mq_hw_ctx *hctx,
  2464. struct request *rq)
  2465. {
  2466. struct hrtimer_sleeper hs;
  2467. enum hrtimer_mode mode;
  2468. unsigned int nsecs;
  2469. ktime_t kt;
  2470. if (rq->rq_flags & RQF_MQ_POLL_SLEPT)
  2471. return false;
  2472. /*
  2473. * poll_nsec can be:
  2474. *
  2475. * -1: don't ever hybrid sleep
  2476. * 0: use half of prev avg
  2477. * >0: use this specific value
  2478. */
  2479. if (q->poll_nsec == -1)
  2480. return false;
  2481. else if (q->poll_nsec > 0)
  2482. nsecs = q->poll_nsec;
  2483. else
  2484. nsecs = blk_mq_poll_nsecs(q, hctx, rq);
  2485. if (!nsecs)
  2486. return false;
  2487. rq->rq_flags |= RQF_MQ_POLL_SLEPT;
  2488. /*
  2489. * This will be replaced with the stats tracking code, using
  2490. * 'avg_completion_time / 2' as the pre-sleep target.
  2491. */
  2492. kt = nsecs;
  2493. mode = HRTIMER_MODE_REL;
  2494. hrtimer_init_on_stack(&hs.timer, CLOCK_MONOTONIC, mode);
  2495. hrtimer_set_expires(&hs.timer, kt);
  2496. hrtimer_init_sleeper(&hs, current);
  2497. do {
  2498. if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
  2499. break;
  2500. set_current_state(TASK_UNINTERRUPTIBLE);
  2501. hrtimer_start_expires(&hs.timer, mode);
  2502. if (hs.task)
  2503. io_schedule();
  2504. hrtimer_cancel(&hs.timer);
  2505. mode = HRTIMER_MODE_ABS;
  2506. } while (hs.task && !signal_pending(current));
  2507. __set_current_state(TASK_RUNNING);
  2508. destroy_hrtimer_on_stack(&hs.timer);
  2509. return true;
  2510. }
  2511. static bool __blk_mq_poll(struct blk_mq_hw_ctx *hctx, struct request *rq)
  2512. {
  2513. struct request_queue *q = hctx->queue;
  2514. long state;
  2515. /*
  2516. * If we sleep, have the caller restart the poll loop to reset
  2517. * the state. Like for the other success return cases, the
  2518. * caller is responsible for checking if the IO completed. If
  2519. * the IO isn't complete, we'll get called again and will go
  2520. * straight to the busy poll loop.
  2521. */
  2522. if (blk_mq_poll_hybrid_sleep(q, hctx, rq))
  2523. return true;
  2524. hctx->poll_considered++;
  2525. state = current->state;
  2526. while (!need_resched()) {
  2527. int ret;
  2528. hctx->poll_invoked++;
  2529. ret = q->mq_ops->poll(hctx, rq->tag);
  2530. if (ret > 0) {
  2531. hctx->poll_success++;
  2532. set_current_state(TASK_RUNNING);
  2533. return true;
  2534. }
  2535. if (signal_pending_state(state, current))
  2536. set_current_state(TASK_RUNNING);
  2537. if (current->state == TASK_RUNNING)
  2538. return true;
  2539. if (ret < 0)
  2540. break;
  2541. cpu_relax();
  2542. }
  2543. __set_current_state(TASK_RUNNING);
  2544. return false;
  2545. }
  2546. static bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie)
  2547. {
  2548. struct blk_mq_hw_ctx *hctx;
  2549. struct request *rq;
  2550. if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
  2551. return false;
  2552. hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
  2553. if (!blk_qc_t_is_internal(cookie))
  2554. rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
  2555. else {
  2556. rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
  2557. /*
  2558. * With scheduling, if the request has completed, we'll
  2559. * get a NULL return here, as we clear the sched tag when
  2560. * that happens. The request still remains valid, like always,
  2561. * so we should be safe with just the NULL check.
  2562. */
  2563. if (!rq)
  2564. return false;
  2565. }
  2566. return __blk_mq_poll(hctx, rq);
  2567. }
  2568. static int __init blk_mq_init(void)
  2569. {
  2570. cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
  2571. blk_mq_hctx_notify_dead);
  2572. return 0;
  2573. }
  2574. subsys_initcall(blk_mq_init);