blk-mq.c 72 KB

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