blk-mq.c 78 KB

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