blk-mq.c 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Block multiqueue core code
  4. *
  5. * Copyright (C) 2013-2014 Jens Axboe
  6. * Copyright (C) 2013-2014 Christoph Hellwig
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/backing-dev.h>
  11. #include <linux/bio.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/blk-integrity.h>
  14. #include <linux/kmemleak.h>
  15. #include <linux/mm.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/smp.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/llist.h>
  22. #include <linux/cpu.h>
  23. #include <linux/cache.h>
  24. #include <linux/sched/topology.h>
  25. #include <linux/sched/signal.h>
  26. #include <linux/delay.h>
  27. #include <linux/crash_dump.h>
  28. #include <linux/prefetch.h>
  29. #include <linux/blk-crypto.h>
  30. #include <linux/part_stat.h>
  31. #include <linux/sched/isolation.h>
  32. #include <trace/events/block.h>
  33. #include <linux/t10-pi.h>
  34. #include "blk.h"
  35. #include "blk-mq.h"
  36. #include "blk-mq-debugfs.h"
  37. #include "blk-pm.h"
  38. #include "blk-stat.h"
  39. #include "blk-mq-sched.h"
  40. #include "blk-rq-qos.h"
  41. static DEFINE_PER_CPU(struct llist_head, blk_cpu_done);
  42. static DEFINE_PER_CPU(call_single_data_t, blk_cpu_csd);
  43. static DEFINE_MUTEX(blk_mq_cpuhp_lock);
  44. static void blk_mq_insert_request(struct request *rq, blk_insert_t flags);
  45. static void blk_mq_request_bypass_insert(struct request *rq,
  46. blk_insert_t flags);
  47. static void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
  48. struct list_head *list);
  49. static int blk_hctx_poll(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
  50. struct io_comp_batch *iob, unsigned int flags);
  51. /*
  52. * Check if any of the ctx, dispatch list or elevator
  53. * 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. const int bit = ctx->index_hw[hctx->type];
  68. if (!sbitmap_test_bit(&hctx->ctx_map, bit))
  69. sbitmap_set_bit(&hctx->ctx_map, bit);
  70. }
  71. static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
  72. struct blk_mq_ctx *ctx)
  73. {
  74. const int bit = ctx->index_hw[hctx->type];
  75. sbitmap_clear_bit(&hctx->ctx_map, bit);
  76. }
  77. struct mq_inflight {
  78. struct block_device *part;
  79. unsigned int inflight[2];
  80. };
  81. static bool blk_mq_check_inflight(struct request *rq, void *priv)
  82. {
  83. struct mq_inflight *mi = priv;
  84. if (rq->part && blk_do_io_stat(rq) &&
  85. (!bdev_is_partition(mi->part) || rq->part == mi->part) &&
  86. blk_mq_rq_state(rq) == MQ_RQ_IN_FLIGHT)
  87. mi->inflight[rq_data_dir(rq)]++;
  88. return true;
  89. }
  90. unsigned int blk_mq_in_flight(struct request_queue *q,
  91. struct block_device *part)
  92. {
  93. struct mq_inflight mi = { .part = part };
  94. blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
  95. return mi.inflight[0] + mi.inflight[1];
  96. }
  97. void blk_mq_in_flight_rw(struct request_queue *q, struct block_device *part,
  98. unsigned int inflight[2])
  99. {
  100. struct mq_inflight mi = { .part = part };
  101. blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
  102. inflight[0] = mi.inflight[0];
  103. inflight[1] = mi.inflight[1];
  104. }
  105. #ifdef CONFIG_LOCKDEP
  106. static bool blk_freeze_set_owner(struct request_queue *q,
  107. struct task_struct *owner)
  108. {
  109. if (!owner)
  110. return false;
  111. if (!q->mq_freeze_depth) {
  112. q->mq_freeze_owner = owner;
  113. q->mq_freeze_owner_depth = 1;
  114. return true;
  115. }
  116. if (owner == q->mq_freeze_owner)
  117. q->mq_freeze_owner_depth += 1;
  118. return false;
  119. }
  120. /* verify the last unfreeze in owner context */
  121. static bool blk_unfreeze_check_owner(struct request_queue *q)
  122. {
  123. if (!q->mq_freeze_owner)
  124. return false;
  125. if (q->mq_freeze_owner != current)
  126. return false;
  127. if (--q->mq_freeze_owner_depth == 0) {
  128. q->mq_freeze_owner = NULL;
  129. return true;
  130. }
  131. return false;
  132. }
  133. #else
  134. static bool blk_freeze_set_owner(struct request_queue *q,
  135. struct task_struct *owner)
  136. {
  137. return false;
  138. }
  139. static bool blk_unfreeze_check_owner(struct request_queue *q)
  140. {
  141. return false;
  142. }
  143. #endif
  144. bool __blk_freeze_queue_start(struct request_queue *q,
  145. struct task_struct *owner)
  146. {
  147. bool freeze;
  148. mutex_lock(&q->mq_freeze_lock);
  149. freeze = blk_freeze_set_owner(q, owner);
  150. if (++q->mq_freeze_depth == 1) {
  151. percpu_ref_kill(&q->q_usage_counter);
  152. mutex_unlock(&q->mq_freeze_lock);
  153. if (queue_is_mq(q))
  154. blk_mq_run_hw_queues(q, false);
  155. } else {
  156. mutex_unlock(&q->mq_freeze_lock);
  157. }
  158. return freeze;
  159. }
  160. void blk_freeze_queue_start(struct request_queue *q)
  161. {
  162. if (__blk_freeze_queue_start(q, current))
  163. blk_freeze_acquire_lock(q, false, false);
  164. }
  165. EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
  166. void blk_mq_freeze_queue_wait(struct request_queue *q)
  167. {
  168. wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  169. }
  170. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
  171. int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
  172. unsigned long timeout)
  173. {
  174. return wait_event_timeout(q->mq_freeze_wq,
  175. percpu_ref_is_zero(&q->q_usage_counter),
  176. timeout);
  177. }
  178. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
  179. /*
  180. * Guarantee no request is in use, so we can change any data structure of
  181. * the queue afterward.
  182. */
  183. void blk_freeze_queue(struct request_queue *q)
  184. {
  185. /*
  186. * In the !blk_mq case we are only calling this to kill the
  187. * q_usage_counter, otherwise this increases the freeze depth
  188. * and waits for it to return to zero. For this reason there is
  189. * no blk_unfreeze_queue(), and blk_freeze_queue() is not
  190. * exported to drivers as the only user for unfreeze is blk_mq.
  191. */
  192. blk_freeze_queue_start(q);
  193. blk_mq_freeze_queue_wait(q);
  194. }
  195. void blk_mq_freeze_queue(struct request_queue *q)
  196. {
  197. /*
  198. * ...just an alias to keep freeze and unfreeze actions balanced
  199. * in the blk_mq_* namespace
  200. */
  201. blk_freeze_queue(q);
  202. }
  203. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
  204. bool __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic)
  205. {
  206. bool unfreeze;
  207. mutex_lock(&q->mq_freeze_lock);
  208. if (force_atomic)
  209. q->q_usage_counter.data->force_atomic = true;
  210. q->mq_freeze_depth--;
  211. WARN_ON_ONCE(q->mq_freeze_depth < 0);
  212. if (!q->mq_freeze_depth) {
  213. percpu_ref_resurrect(&q->q_usage_counter);
  214. wake_up_all(&q->mq_freeze_wq);
  215. }
  216. unfreeze = blk_unfreeze_check_owner(q);
  217. mutex_unlock(&q->mq_freeze_lock);
  218. return unfreeze;
  219. }
  220. void blk_mq_unfreeze_queue(struct request_queue *q)
  221. {
  222. if (__blk_mq_unfreeze_queue(q, false))
  223. blk_unfreeze_release_lock(q, false, false);
  224. }
  225. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
  226. /*
  227. * non_owner variant of blk_freeze_queue_start
  228. *
  229. * Unlike blk_freeze_queue_start, the queue doesn't need to be unfrozen
  230. * by the same task. This is fragile and should not be used if at all
  231. * possible.
  232. */
  233. void blk_freeze_queue_start_non_owner(struct request_queue *q)
  234. {
  235. __blk_freeze_queue_start(q, NULL);
  236. }
  237. EXPORT_SYMBOL_GPL(blk_freeze_queue_start_non_owner);
  238. /* non_owner variant of blk_mq_unfreeze_queue */
  239. void blk_mq_unfreeze_queue_non_owner(struct request_queue *q)
  240. {
  241. __blk_mq_unfreeze_queue(q, false);
  242. }
  243. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue_non_owner);
  244. /*
  245. * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
  246. * mpt3sas driver such that this function can be removed.
  247. */
  248. void blk_mq_quiesce_queue_nowait(struct request_queue *q)
  249. {
  250. unsigned long flags;
  251. spin_lock_irqsave(&q->queue_lock, flags);
  252. if (!q->quiesce_depth++)
  253. blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
  254. spin_unlock_irqrestore(&q->queue_lock, flags);
  255. }
  256. EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
  257. /**
  258. * blk_mq_wait_quiesce_done() - wait until in-progress quiesce is done
  259. * @set: tag_set to wait on
  260. *
  261. * Note: it is driver's responsibility for making sure that quiesce has
  262. * been started on or more of the request_queues of the tag_set. This
  263. * function only waits for the quiesce on those request_queues that had
  264. * the quiesce flag set using blk_mq_quiesce_queue_nowait.
  265. */
  266. void blk_mq_wait_quiesce_done(struct blk_mq_tag_set *set)
  267. {
  268. if (set->flags & BLK_MQ_F_BLOCKING)
  269. synchronize_srcu(set->srcu);
  270. else
  271. synchronize_rcu();
  272. }
  273. EXPORT_SYMBOL_GPL(blk_mq_wait_quiesce_done);
  274. /**
  275. * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
  276. * @q: request queue.
  277. *
  278. * Note: this function does not prevent that the struct request end_io()
  279. * callback function is invoked. Once this function is returned, we make
  280. * sure no dispatch can happen until the queue is unquiesced via
  281. * blk_mq_unquiesce_queue().
  282. */
  283. void blk_mq_quiesce_queue(struct request_queue *q)
  284. {
  285. blk_mq_quiesce_queue_nowait(q);
  286. /* nothing to wait for non-mq queues */
  287. if (queue_is_mq(q))
  288. blk_mq_wait_quiesce_done(q->tag_set);
  289. }
  290. EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
  291. /*
  292. * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
  293. * @q: request queue.
  294. *
  295. * This function recovers queue into the state before quiescing
  296. * which is done by blk_mq_quiesce_queue.
  297. */
  298. void blk_mq_unquiesce_queue(struct request_queue *q)
  299. {
  300. unsigned long flags;
  301. bool run_queue = false;
  302. spin_lock_irqsave(&q->queue_lock, flags);
  303. if (WARN_ON_ONCE(q->quiesce_depth <= 0)) {
  304. ;
  305. } else if (!--q->quiesce_depth) {
  306. blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
  307. run_queue = true;
  308. }
  309. spin_unlock_irqrestore(&q->queue_lock, flags);
  310. /* dispatch requests which are inserted during quiescing */
  311. if (run_queue)
  312. blk_mq_run_hw_queues(q, true);
  313. }
  314. EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
  315. void blk_mq_quiesce_tagset(struct blk_mq_tag_set *set)
  316. {
  317. struct request_queue *q;
  318. mutex_lock(&set->tag_list_lock);
  319. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  320. if (!blk_queue_skip_tagset_quiesce(q))
  321. blk_mq_quiesce_queue_nowait(q);
  322. }
  323. mutex_unlock(&set->tag_list_lock);
  324. blk_mq_wait_quiesce_done(set);
  325. }
  326. EXPORT_SYMBOL_GPL(blk_mq_quiesce_tagset);
  327. void blk_mq_unquiesce_tagset(struct blk_mq_tag_set *set)
  328. {
  329. struct request_queue *q;
  330. mutex_lock(&set->tag_list_lock);
  331. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  332. if (!blk_queue_skip_tagset_quiesce(q))
  333. blk_mq_unquiesce_queue(q);
  334. }
  335. mutex_unlock(&set->tag_list_lock);
  336. }
  337. EXPORT_SYMBOL_GPL(blk_mq_unquiesce_tagset);
  338. void blk_mq_wake_waiters(struct request_queue *q)
  339. {
  340. struct blk_mq_hw_ctx *hctx;
  341. unsigned long i;
  342. queue_for_each_hw_ctx(q, hctx, i)
  343. if (blk_mq_hw_queue_mapped(hctx))
  344. blk_mq_tag_wakeup_all(hctx->tags, true);
  345. }
  346. void blk_rq_init(struct request_queue *q, struct request *rq)
  347. {
  348. memset(rq, 0, sizeof(*rq));
  349. INIT_LIST_HEAD(&rq->queuelist);
  350. rq->q = q;
  351. rq->__sector = (sector_t) -1;
  352. INIT_HLIST_NODE(&rq->hash);
  353. RB_CLEAR_NODE(&rq->rb_node);
  354. rq->tag = BLK_MQ_NO_TAG;
  355. rq->internal_tag = BLK_MQ_NO_TAG;
  356. rq->start_time_ns = blk_time_get_ns();
  357. rq->part = NULL;
  358. blk_crypto_rq_set_defaults(rq);
  359. }
  360. EXPORT_SYMBOL(blk_rq_init);
  361. /* Set start and alloc time when the allocated request is actually used */
  362. static inline void blk_mq_rq_time_init(struct request *rq, u64 alloc_time_ns)
  363. {
  364. if (blk_mq_need_time_stamp(rq))
  365. rq->start_time_ns = blk_time_get_ns();
  366. else
  367. rq->start_time_ns = 0;
  368. #ifdef CONFIG_BLK_RQ_ALLOC_TIME
  369. if (blk_queue_rq_alloc_time(rq->q))
  370. rq->alloc_time_ns = alloc_time_ns ?: rq->start_time_ns;
  371. else
  372. rq->alloc_time_ns = 0;
  373. #endif
  374. }
  375. static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
  376. struct blk_mq_tags *tags, unsigned int tag)
  377. {
  378. struct blk_mq_ctx *ctx = data->ctx;
  379. struct blk_mq_hw_ctx *hctx = data->hctx;
  380. struct request_queue *q = data->q;
  381. struct request *rq = tags->static_rqs[tag];
  382. rq->q = q;
  383. rq->mq_ctx = ctx;
  384. rq->mq_hctx = hctx;
  385. rq->cmd_flags = data->cmd_flags;
  386. if (data->flags & BLK_MQ_REQ_PM)
  387. data->rq_flags |= RQF_PM;
  388. if (blk_queue_io_stat(q))
  389. data->rq_flags |= RQF_IO_STAT;
  390. rq->rq_flags = data->rq_flags;
  391. if (data->rq_flags & RQF_SCHED_TAGS) {
  392. rq->tag = BLK_MQ_NO_TAG;
  393. rq->internal_tag = tag;
  394. } else {
  395. rq->tag = tag;
  396. rq->internal_tag = BLK_MQ_NO_TAG;
  397. }
  398. rq->timeout = 0;
  399. rq->part = NULL;
  400. rq->io_start_time_ns = 0;
  401. rq->stats_sectors = 0;
  402. rq->nr_phys_segments = 0;
  403. rq->nr_integrity_segments = 0;
  404. rq->end_io = NULL;
  405. rq->end_io_data = NULL;
  406. blk_crypto_rq_set_defaults(rq);
  407. INIT_LIST_HEAD(&rq->queuelist);
  408. /* tag was already set */
  409. WRITE_ONCE(rq->deadline, 0);
  410. req_ref_set(rq, 1);
  411. if (rq->rq_flags & RQF_USE_SCHED) {
  412. struct elevator_queue *e = data->q->elevator;
  413. INIT_HLIST_NODE(&rq->hash);
  414. RB_CLEAR_NODE(&rq->rb_node);
  415. if (e->type->ops.prepare_request)
  416. e->type->ops.prepare_request(rq);
  417. }
  418. return rq;
  419. }
  420. static inline struct request *
  421. __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data)
  422. {
  423. unsigned int tag, tag_offset;
  424. struct blk_mq_tags *tags;
  425. struct request *rq;
  426. unsigned long tag_mask;
  427. int i, nr = 0;
  428. tag_mask = blk_mq_get_tags(data, data->nr_tags, &tag_offset);
  429. if (unlikely(!tag_mask))
  430. return NULL;
  431. tags = blk_mq_tags_from_data(data);
  432. for (i = 0; tag_mask; i++) {
  433. if (!(tag_mask & (1UL << i)))
  434. continue;
  435. tag = tag_offset + i;
  436. prefetch(tags->static_rqs[tag]);
  437. tag_mask &= ~(1UL << i);
  438. rq = blk_mq_rq_ctx_init(data, tags, tag);
  439. rq_list_add(data->cached_rq, rq);
  440. nr++;
  441. }
  442. if (!(data->rq_flags & RQF_SCHED_TAGS))
  443. blk_mq_add_active_requests(data->hctx, nr);
  444. /* caller already holds a reference, add for remainder */
  445. percpu_ref_get_many(&data->q->q_usage_counter, nr - 1);
  446. data->nr_tags -= nr;
  447. return rq_list_pop(data->cached_rq);
  448. }
  449. static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data)
  450. {
  451. struct request_queue *q = data->q;
  452. u64 alloc_time_ns = 0;
  453. struct request *rq;
  454. unsigned int tag;
  455. /* alloc_time includes depth and tag waits */
  456. if (blk_queue_rq_alloc_time(q))
  457. alloc_time_ns = blk_time_get_ns();
  458. if (data->cmd_flags & REQ_NOWAIT)
  459. data->flags |= BLK_MQ_REQ_NOWAIT;
  460. retry:
  461. data->ctx = blk_mq_get_ctx(q);
  462. data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx);
  463. if (q->elevator) {
  464. /*
  465. * All requests use scheduler tags when an I/O scheduler is
  466. * enabled for the queue.
  467. */
  468. data->rq_flags |= RQF_SCHED_TAGS;
  469. /*
  470. * Flush/passthrough requests are special and go directly to the
  471. * dispatch list.
  472. */
  473. if ((data->cmd_flags & REQ_OP_MASK) != REQ_OP_FLUSH &&
  474. !blk_op_is_passthrough(data->cmd_flags)) {
  475. struct elevator_mq_ops *ops = &q->elevator->type->ops;
  476. WARN_ON_ONCE(data->flags & BLK_MQ_REQ_RESERVED);
  477. data->rq_flags |= RQF_USE_SCHED;
  478. if (ops->limit_depth)
  479. ops->limit_depth(data->cmd_flags, data);
  480. }
  481. } else {
  482. blk_mq_tag_busy(data->hctx);
  483. }
  484. if (data->flags & BLK_MQ_REQ_RESERVED)
  485. data->rq_flags |= RQF_RESV;
  486. /*
  487. * Try batched alloc if we want more than 1 tag.
  488. */
  489. if (data->nr_tags > 1) {
  490. rq = __blk_mq_alloc_requests_batch(data);
  491. if (rq) {
  492. blk_mq_rq_time_init(rq, alloc_time_ns);
  493. return rq;
  494. }
  495. data->nr_tags = 1;
  496. }
  497. /*
  498. * Waiting allocations only fail because of an inactive hctx. In that
  499. * case just retry the hctx assignment and tag allocation as CPU hotplug
  500. * should have migrated us to an online CPU by now.
  501. */
  502. tag = blk_mq_get_tag(data);
  503. if (tag == BLK_MQ_NO_TAG) {
  504. if (data->flags & BLK_MQ_REQ_NOWAIT)
  505. return NULL;
  506. /*
  507. * Give up the CPU and sleep for a random short time to
  508. * ensure that thread using a realtime scheduling class
  509. * are migrated off the CPU, and thus off the hctx that
  510. * is going away.
  511. */
  512. msleep(3);
  513. goto retry;
  514. }
  515. if (!(data->rq_flags & RQF_SCHED_TAGS))
  516. blk_mq_inc_active_requests(data->hctx);
  517. rq = blk_mq_rq_ctx_init(data, blk_mq_tags_from_data(data), tag);
  518. blk_mq_rq_time_init(rq, alloc_time_ns);
  519. return rq;
  520. }
  521. static struct request *blk_mq_rq_cache_fill(struct request_queue *q,
  522. struct blk_plug *plug,
  523. blk_opf_t opf,
  524. blk_mq_req_flags_t flags)
  525. {
  526. struct blk_mq_alloc_data data = {
  527. .q = q,
  528. .flags = flags,
  529. .cmd_flags = opf,
  530. .nr_tags = plug->nr_ios,
  531. .cached_rq = &plug->cached_rq,
  532. };
  533. struct request *rq;
  534. if (blk_queue_enter(q, flags))
  535. return NULL;
  536. plug->nr_ios = 1;
  537. rq = __blk_mq_alloc_requests(&data);
  538. if (unlikely(!rq))
  539. blk_queue_exit(q);
  540. return rq;
  541. }
  542. static struct request *blk_mq_alloc_cached_request(struct request_queue *q,
  543. blk_opf_t opf,
  544. blk_mq_req_flags_t flags)
  545. {
  546. struct blk_plug *plug = current->plug;
  547. struct request *rq;
  548. if (!plug)
  549. return NULL;
  550. if (rq_list_empty(plug->cached_rq)) {
  551. if (plug->nr_ios == 1)
  552. return NULL;
  553. rq = blk_mq_rq_cache_fill(q, plug, opf, flags);
  554. if (!rq)
  555. return NULL;
  556. } else {
  557. rq = rq_list_peek(&plug->cached_rq);
  558. if (!rq || rq->q != q)
  559. return NULL;
  560. if (blk_mq_get_hctx_type(opf) != rq->mq_hctx->type)
  561. return NULL;
  562. if (op_is_flush(rq->cmd_flags) != op_is_flush(opf))
  563. return NULL;
  564. plug->cached_rq = rq_list_next(rq);
  565. blk_mq_rq_time_init(rq, 0);
  566. }
  567. rq->cmd_flags = opf;
  568. INIT_LIST_HEAD(&rq->queuelist);
  569. return rq;
  570. }
  571. struct request *blk_mq_alloc_request(struct request_queue *q, blk_opf_t opf,
  572. blk_mq_req_flags_t flags)
  573. {
  574. struct request *rq;
  575. rq = blk_mq_alloc_cached_request(q, opf, flags);
  576. if (!rq) {
  577. struct blk_mq_alloc_data data = {
  578. .q = q,
  579. .flags = flags,
  580. .cmd_flags = opf,
  581. .nr_tags = 1,
  582. };
  583. int ret;
  584. ret = blk_queue_enter(q, flags);
  585. if (ret)
  586. return ERR_PTR(ret);
  587. rq = __blk_mq_alloc_requests(&data);
  588. if (!rq)
  589. goto out_queue_exit;
  590. }
  591. rq->__data_len = 0;
  592. rq->__sector = (sector_t) -1;
  593. rq->bio = rq->biotail = NULL;
  594. return rq;
  595. out_queue_exit:
  596. blk_queue_exit(q);
  597. return ERR_PTR(-EWOULDBLOCK);
  598. }
  599. EXPORT_SYMBOL(blk_mq_alloc_request);
  600. struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
  601. blk_opf_t opf, blk_mq_req_flags_t flags, unsigned int hctx_idx)
  602. {
  603. struct blk_mq_alloc_data data = {
  604. .q = q,
  605. .flags = flags,
  606. .cmd_flags = opf,
  607. .nr_tags = 1,
  608. };
  609. u64 alloc_time_ns = 0;
  610. struct request *rq;
  611. unsigned int cpu;
  612. unsigned int tag;
  613. int ret;
  614. /* alloc_time includes depth and tag waits */
  615. if (blk_queue_rq_alloc_time(q))
  616. alloc_time_ns = blk_time_get_ns();
  617. /*
  618. * If the tag allocator sleeps we could get an allocation for a
  619. * different hardware context. No need to complicate the low level
  620. * allocator for this for the rare use case of a command tied to
  621. * a specific queue.
  622. */
  623. if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)) ||
  624. WARN_ON_ONCE(!(flags & BLK_MQ_REQ_RESERVED)))
  625. return ERR_PTR(-EINVAL);
  626. if (hctx_idx >= q->nr_hw_queues)
  627. return ERR_PTR(-EIO);
  628. ret = blk_queue_enter(q, flags);
  629. if (ret)
  630. return ERR_PTR(ret);
  631. /*
  632. * Check if the hardware context is actually mapped to anything.
  633. * If not tell the caller that it should skip this queue.
  634. */
  635. ret = -EXDEV;
  636. data.hctx = xa_load(&q->hctx_table, hctx_idx);
  637. if (!blk_mq_hw_queue_mapped(data.hctx))
  638. goto out_queue_exit;
  639. cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
  640. if (cpu >= nr_cpu_ids)
  641. goto out_queue_exit;
  642. data.ctx = __blk_mq_get_ctx(q, cpu);
  643. if (q->elevator)
  644. data.rq_flags |= RQF_SCHED_TAGS;
  645. else
  646. blk_mq_tag_busy(data.hctx);
  647. if (flags & BLK_MQ_REQ_RESERVED)
  648. data.rq_flags |= RQF_RESV;
  649. ret = -EWOULDBLOCK;
  650. tag = blk_mq_get_tag(&data);
  651. if (tag == BLK_MQ_NO_TAG)
  652. goto out_queue_exit;
  653. if (!(data.rq_flags & RQF_SCHED_TAGS))
  654. blk_mq_inc_active_requests(data.hctx);
  655. rq = blk_mq_rq_ctx_init(&data, blk_mq_tags_from_data(&data), tag);
  656. blk_mq_rq_time_init(rq, alloc_time_ns);
  657. rq->__data_len = 0;
  658. rq->__sector = (sector_t) -1;
  659. rq->bio = rq->biotail = NULL;
  660. return rq;
  661. out_queue_exit:
  662. blk_queue_exit(q);
  663. return ERR_PTR(ret);
  664. }
  665. EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
  666. static void blk_mq_finish_request(struct request *rq)
  667. {
  668. struct request_queue *q = rq->q;
  669. blk_zone_finish_request(rq);
  670. if (rq->rq_flags & RQF_USE_SCHED) {
  671. q->elevator->type->ops.finish_request(rq);
  672. /*
  673. * For postflush request that may need to be
  674. * completed twice, we should clear this flag
  675. * to avoid double finish_request() on the rq.
  676. */
  677. rq->rq_flags &= ~RQF_USE_SCHED;
  678. }
  679. }
  680. static void __blk_mq_free_request(struct request *rq)
  681. {
  682. struct request_queue *q = rq->q;
  683. struct blk_mq_ctx *ctx = rq->mq_ctx;
  684. struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
  685. const int sched_tag = rq->internal_tag;
  686. blk_crypto_free_request(rq);
  687. blk_pm_mark_last_busy(rq);
  688. rq->mq_hctx = NULL;
  689. if (rq->tag != BLK_MQ_NO_TAG) {
  690. blk_mq_dec_active_requests(hctx);
  691. blk_mq_put_tag(hctx->tags, ctx, rq->tag);
  692. }
  693. if (sched_tag != BLK_MQ_NO_TAG)
  694. blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
  695. blk_mq_sched_restart(hctx);
  696. blk_queue_exit(q);
  697. }
  698. void blk_mq_free_request(struct request *rq)
  699. {
  700. struct request_queue *q = rq->q;
  701. blk_mq_finish_request(rq);
  702. if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
  703. laptop_io_completion(q->disk->bdi);
  704. rq_qos_done(q, rq);
  705. WRITE_ONCE(rq->state, MQ_RQ_IDLE);
  706. if (req_ref_put_and_test(rq))
  707. __blk_mq_free_request(rq);
  708. }
  709. EXPORT_SYMBOL_GPL(blk_mq_free_request);
  710. void blk_mq_free_plug_rqs(struct blk_plug *plug)
  711. {
  712. struct request *rq;
  713. while ((rq = rq_list_pop(&plug->cached_rq)) != NULL)
  714. blk_mq_free_request(rq);
  715. }
  716. void blk_dump_rq_flags(struct request *rq, char *msg)
  717. {
  718. printk(KERN_INFO "%s: dev %s: flags=%llx\n", msg,
  719. rq->q->disk ? rq->q->disk->disk_name : "?",
  720. (__force unsigned long long) rq->cmd_flags);
  721. printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
  722. (unsigned long long)blk_rq_pos(rq),
  723. blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
  724. printk(KERN_INFO " bio %p, biotail %p, len %u\n",
  725. rq->bio, rq->biotail, blk_rq_bytes(rq));
  726. }
  727. EXPORT_SYMBOL(blk_dump_rq_flags);
  728. static void blk_account_io_completion(struct request *req, unsigned int bytes)
  729. {
  730. if (req->part && blk_do_io_stat(req)) {
  731. const int sgrp = op_stat_group(req_op(req));
  732. part_stat_lock();
  733. part_stat_add(req->part, sectors[sgrp], bytes >> 9);
  734. part_stat_unlock();
  735. }
  736. }
  737. static void blk_print_req_error(struct request *req, blk_status_t status)
  738. {
  739. printk_ratelimited(KERN_ERR
  740. "%s error, dev %s, sector %llu op 0x%x:(%s) flags 0x%x "
  741. "phys_seg %u prio class %u\n",
  742. blk_status_to_str(status),
  743. req->q->disk ? req->q->disk->disk_name : "?",
  744. blk_rq_pos(req), (__force u32)req_op(req),
  745. blk_op_str(req_op(req)),
  746. (__force u32)(req->cmd_flags & ~REQ_OP_MASK),
  747. req->nr_phys_segments,
  748. IOPRIO_PRIO_CLASS(req->ioprio));
  749. }
  750. /*
  751. * Fully end IO on a request. Does not support partial completions, or
  752. * errors.
  753. */
  754. static void blk_complete_request(struct request *req)
  755. {
  756. const bool is_flush = (req->rq_flags & RQF_FLUSH_SEQ) != 0;
  757. int total_bytes = blk_rq_bytes(req);
  758. struct bio *bio = req->bio;
  759. trace_block_rq_complete(req, BLK_STS_OK, total_bytes);
  760. if (!bio)
  761. return;
  762. if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ)
  763. blk_integrity_complete(req, total_bytes);
  764. /*
  765. * Upper layers may call blk_crypto_evict_key() anytime after the last
  766. * bio_endio(). Therefore, the keyslot must be released before that.
  767. */
  768. blk_crypto_rq_put_keyslot(req);
  769. blk_account_io_completion(req, total_bytes);
  770. do {
  771. struct bio *next = bio->bi_next;
  772. /* Completion has already been traced */
  773. bio_clear_flag(bio, BIO_TRACE_COMPLETION);
  774. blk_zone_update_request_bio(req, bio);
  775. if (!is_flush)
  776. bio_endio(bio);
  777. bio = next;
  778. } while (bio);
  779. /*
  780. * Reset counters so that the request stacking driver
  781. * can find how many bytes remain in the request
  782. * later.
  783. */
  784. if (!req->end_io) {
  785. req->bio = NULL;
  786. req->__data_len = 0;
  787. }
  788. }
  789. /**
  790. * blk_update_request - Complete multiple bytes without completing the request
  791. * @req: the request being processed
  792. * @error: block status code
  793. * @nr_bytes: number of bytes to complete for @req
  794. *
  795. * Description:
  796. * Ends I/O on a number of bytes attached to @req, but doesn't complete
  797. * the request structure even if @req doesn't have leftover.
  798. * If @req has leftover, sets it up for the next range of segments.
  799. *
  800. * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
  801. * %false return from this function.
  802. *
  803. * Note:
  804. * The RQF_SPECIAL_PAYLOAD flag is ignored on purpose in this function
  805. * except in the consistency check at the end of this function.
  806. *
  807. * Return:
  808. * %false - this request doesn't have any more data
  809. * %true - this request has more data
  810. **/
  811. bool blk_update_request(struct request *req, blk_status_t error,
  812. unsigned int nr_bytes)
  813. {
  814. bool is_flush = req->rq_flags & RQF_FLUSH_SEQ;
  815. bool quiet = req->rq_flags & RQF_QUIET;
  816. int total_bytes;
  817. trace_block_rq_complete(req, error, nr_bytes);
  818. if (!req->bio)
  819. return false;
  820. if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ &&
  821. error == BLK_STS_OK)
  822. blk_integrity_complete(req, nr_bytes);
  823. /*
  824. * Upper layers may call blk_crypto_evict_key() anytime after the last
  825. * bio_endio(). Therefore, the keyslot must be released before that.
  826. */
  827. if (blk_crypto_rq_has_keyslot(req) && nr_bytes >= blk_rq_bytes(req))
  828. __blk_crypto_rq_put_keyslot(req);
  829. if (unlikely(error && !blk_rq_is_passthrough(req) && !quiet) &&
  830. !test_bit(GD_DEAD, &req->q->disk->state)) {
  831. blk_print_req_error(req, error);
  832. trace_block_rq_error(req, error, nr_bytes);
  833. }
  834. blk_account_io_completion(req, nr_bytes);
  835. total_bytes = 0;
  836. while (req->bio) {
  837. struct bio *bio = req->bio;
  838. unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
  839. if (unlikely(error))
  840. bio->bi_status = error;
  841. if (bio_bytes == bio->bi_iter.bi_size) {
  842. req->bio = bio->bi_next;
  843. } else if (bio_is_zone_append(bio) && error == BLK_STS_OK) {
  844. /*
  845. * Partial zone append completions cannot be supported
  846. * as the BIO fragments may end up not being written
  847. * sequentially.
  848. */
  849. bio->bi_status = BLK_STS_IOERR;
  850. }
  851. /* Completion has already been traced */
  852. bio_clear_flag(bio, BIO_TRACE_COMPLETION);
  853. if (unlikely(quiet))
  854. bio_set_flag(bio, BIO_QUIET);
  855. bio_advance(bio, bio_bytes);
  856. /* Don't actually finish bio if it's part of flush sequence */
  857. if (!bio->bi_iter.bi_size) {
  858. blk_zone_update_request_bio(req, bio);
  859. if (!is_flush)
  860. bio_endio(bio);
  861. }
  862. total_bytes += bio_bytes;
  863. nr_bytes -= bio_bytes;
  864. if (!nr_bytes)
  865. break;
  866. }
  867. /*
  868. * completely done
  869. */
  870. if (!req->bio) {
  871. /*
  872. * Reset counters so that the request stacking driver
  873. * can find how many bytes remain in the request
  874. * later.
  875. */
  876. req->__data_len = 0;
  877. return false;
  878. }
  879. req->__data_len -= total_bytes;
  880. /* update sector only for requests with clear definition of sector */
  881. if (!blk_rq_is_passthrough(req))
  882. req->__sector += total_bytes >> 9;
  883. /* mixed attributes always follow the first bio */
  884. if (req->rq_flags & RQF_MIXED_MERGE) {
  885. req->cmd_flags &= ~REQ_FAILFAST_MASK;
  886. req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK;
  887. }
  888. if (!(req->rq_flags & RQF_SPECIAL_PAYLOAD)) {
  889. /*
  890. * If total number of sectors is less than the first segment
  891. * size, something has gone terribly wrong.
  892. */
  893. if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
  894. blk_dump_rq_flags(req, "request botched");
  895. req->__data_len = blk_rq_cur_bytes(req);
  896. }
  897. /* recalculate the number of segments */
  898. req->nr_phys_segments = blk_recalc_rq_segments(req);
  899. }
  900. return true;
  901. }
  902. EXPORT_SYMBOL_GPL(blk_update_request);
  903. static inline void blk_account_io_done(struct request *req, u64 now)
  904. {
  905. trace_block_io_done(req);
  906. /*
  907. * Account IO completion. flush_rq isn't accounted as a
  908. * normal IO on queueing nor completion. Accounting the
  909. * containing request is enough.
  910. */
  911. if (blk_do_io_stat(req) && req->part &&
  912. !(req->rq_flags & RQF_FLUSH_SEQ)) {
  913. const int sgrp = op_stat_group(req_op(req));
  914. part_stat_lock();
  915. update_io_ticks(req->part, jiffies, true);
  916. part_stat_inc(req->part, ios[sgrp]);
  917. part_stat_add(req->part, nsecs[sgrp], now - req->start_time_ns);
  918. part_stat_local_dec(req->part,
  919. in_flight[op_is_write(req_op(req))]);
  920. part_stat_unlock();
  921. }
  922. }
  923. static inline void blk_account_io_start(struct request *req)
  924. {
  925. trace_block_io_start(req);
  926. if (blk_do_io_stat(req)) {
  927. /*
  928. * All non-passthrough requests are created from a bio with one
  929. * exception: when a flush command that is part of a flush sequence
  930. * generated by the state machine in blk-flush.c is cloned onto the
  931. * lower device by dm-multipath we can get here without a bio.
  932. */
  933. if (req->bio)
  934. req->part = req->bio->bi_bdev;
  935. else
  936. req->part = req->q->disk->part0;
  937. part_stat_lock();
  938. update_io_ticks(req->part, jiffies, false);
  939. part_stat_local_inc(req->part,
  940. in_flight[op_is_write(req_op(req))]);
  941. part_stat_unlock();
  942. }
  943. }
  944. static inline void __blk_mq_end_request_acct(struct request *rq, u64 now)
  945. {
  946. if (rq->rq_flags & RQF_STATS)
  947. blk_stat_add(rq, now);
  948. blk_mq_sched_completed_request(rq, now);
  949. blk_account_io_done(rq, now);
  950. }
  951. inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
  952. {
  953. if (blk_mq_need_time_stamp(rq))
  954. __blk_mq_end_request_acct(rq, blk_time_get_ns());
  955. blk_mq_finish_request(rq);
  956. if (rq->end_io) {
  957. rq_qos_done(rq->q, rq);
  958. if (rq->end_io(rq, error) == RQ_END_IO_FREE)
  959. blk_mq_free_request(rq);
  960. } else {
  961. blk_mq_free_request(rq);
  962. }
  963. }
  964. EXPORT_SYMBOL(__blk_mq_end_request);
  965. void blk_mq_end_request(struct request *rq, blk_status_t error)
  966. {
  967. if (blk_update_request(rq, error, blk_rq_bytes(rq)))
  968. BUG();
  969. __blk_mq_end_request(rq, error);
  970. }
  971. EXPORT_SYMBOL(blk_mq_end_request);
  972. #define TAG_COMP_BATCH 32
  973. static inline void blk_mq_flush_tag_batch(struct blk_mq_hw_ctx *hctx,
  974. int *tag_array, int nr_tags)
  975. {
  976. struct request_queue *q = hctx->queue;
  977. blk_mq_sub_active_requests(hctx, nr_tags);
  978. blk_mq_put_tags(hctx->tags, tag_array, nr_tags);
  979. percpu_ref_put_many(&q->q_usage_counter, nr_tags);
  980. }
  981. void blk_mq_end_request_batch(struct io_comp_batch *iob)
  982. {
  983. int tags[TAG_COMP_BATCH], nr_tags = 0;
  984. struct blk_mq_hw_ctx *cur_hctx = NULL;
  985. struct request *rq;
  986. u64 now = 0;
  987. if (iob->need_ts)
  988. now = blk_time_get_ns();
  989. while ((rq = rq_list_pop(&iob->req_list)) != NULL) {
  990. prefetch(rq->bio);
  991. prefetch(rq->rq_next);
  992. blk_complete_request(rq);
  993. if (iob->need_ts)
  994. __blk_mq_end_request_acct(rq, now);
  995. blk_mq_finish_request(rq);
  996. rq_qos_done(rq->q, rq);
  997. /*
  998. * If end_io handler returns NONE, then it still has
  999. * ownership of the request.
  1000. */
  1001. if (rq->end_io && rq->end_io(rq, 0) == RQ_END_IO_NONE)
  1002. continue;
  1003. WRITE_ONCE(rq->state, MQ_RQ_IDLE);
  1004. if (!req_ref_put_and_test(rq))
  1005. continue;
  1006. blk_crypto_free_request(rq);
  1007. blk_pm_mark_last_busy(rq);
  1008. if (nr_tags == TAG_COMP_BATCH || cur_hctx != rq->mq_hctx) {
  1009. if (cur_hctx)
  1010. blk_mq_flush_tag_batch(cur_hctx, tags, nr_tags);
  1011. nr_tags = 0;
  1012. cur_hctx = rq->mq_hctx;
  1013. }
  1014. tags[nr_tags++] = rq->tag;
  1015. }
  1016. if (nr_tags)
  1017. blk_mq_flush_tag_batch(cur_hctx, tags, nr_tags);
  1018. }
  1019. EXPORT_SYMBOL_GPL(blk_mq_end_request_batch);
  1020. static void blk_complete_reqs(struct llist_head *list)
  1021. {
  1022. struct llist_node *entry = llist_reverse_order(llist_del_all(list));
  1023. struct request *rq, *next;
  1024. llist_for_each_entry_safe(rq, next, entry, ipi_list)
  1025. rq->q->mq_ops->complete(rq);
  1026. }
  1027. static __latent_entropy void blk_done_softirq(void)
  1028. {
  1029. blk_complete_reqs(this_cpu_ptr(&blk_cpu_done));
  1030. }
  1031. static int blk_softirq_cpu_dead(unsigned int cpu)
  1032. {
  1033. blk_complete_reqs(&per_cpu(blk_cpu_done, cpu));
  1034. return 0;
  1035. }
  1036. static void __blk_mq_complete_request_remote(void *data)
  1037. {
  1038. __raise_softirq_irqoff(BLOCK_SOFTIRQ);
  1039. }
  1040. static inline bool blk_mq_complete_need_ipi(struct request *rq)
  1041. {
  1042. int cpu = raw_smp_processor_id();
  1043. if (!IS_ENABLED(CONFIG_SMP) ||
  1044. !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags))
  1045. return false;
  1046. /*
  1047. * With force threaded interrupts enabled, raising softirq from an SMP
  1048. * function call will always result in waking the ksoftirqd thread.
  1049. * This is probably worse than completing the request on a different
  1050. * cache domain.
  1051. */
  1052. if (force_irqthreads())
  1053. return false;
  1054. /* same CPU or cache domain and capacity? Complete locally */
  1055. if (cpu == rq->mq_ctx->cpu ||
  1056. (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) &&
  1057. cpus_share_cache(cpu, rq->mq_ctx->cpu) &&
  1058. cpus_equal_capacity(cpu, rq->mq_ctx->cpu)))
  1059. return false;
  1060. /* don't try to IPI to an offline CPU */
  1061. return cpu_online(rq->mq_ctx->cpu);
  1062. }
  1063. static void blk_mq_complete_send_ipi(struct request *rq)
  1064. {
  1065. unsigned int cpu;
  1066. cpu = rq->mq_ctx->cpu;
  1067. if (llist_add(&rq->ipi_list, &per_cpu(blk_cpu_done, cpu)))
  1068. smp_call_function_single_async(cpu, &per_cpu(blk_cpu_csd, cpu));
  1069. }
  1070. static void blk_mq_raise_softirq(struct request *rq)
  1071. {
  1072. struct llist_head *list;
  1073. preempt_disable();
  1074. list = this_cpu_ptr(&blk_cpu_done);
  1075. if (llist_add(&rq->ipi_list, list))
  1076. raise_softirq(BLOCK_SOFTIRQ);
  1077. preempt_enable();
  1078. }
  1079. bool blk_mq_complete_request_remote(struct request *rq)
  1080. {
  1081. WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
  1082. /*
  1083. * For request which hctx has only one ctx mapping,
  1084. * or a polled request, always complete locally,
  1085. * it's pointless to redirect the completion.
  1086. */
  1087. if ((rq->mq_hctx->nr_ctx == 1 &&
  1088. rq->mq_ctx->cpu == raw_smp_processor_id()) ||
  1089. rq->cmd_flags & REQ_POLLED)
  1090. return false;
  1091. if (blk_mq_complete_need_ipi(rq)) {
  1092. blk_mq_complete_send_ipi(rq);
  1093. return true;
  1094. }
  1095. if (rq->q->nr_hw_queues == 1) {
  1096. blk_mq_raise_softirq(rq);
  1097. return true;
  1098. }
  1099. return false;
  1100. }
  1101. EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote);
  1102. /**
  1103. * blk_mq_complete_request - end I/O on a request
  1104. * @rq: the request being processed
  1105. *
  1106. * Description:
  1107. * Complete a request by scheduling the ->complete_rq operation.
  1108. **/
  1109. void blk_mq_complete_request(struct request *rq)
  1110. {
  1111. if (!blk_mq_complete_request_remote(rq))
  1112. rq->q->mq_ops->complete(rq);
  1113. }
  1114. EXPORT_SYMBOL(blk_mq_complete_request);
  1115. /**
  1116. * blk_mq_start_request - Start processing a request
  1117. * @rq: Pointer to request to be started
  1118. *
  1119. * Function used by device drivers to notify the block layer that a request
  1120. * is going to be processed now, so blk layer can do proper initializations
  1121. * such as starting the timeout timer.
  1122. */
  1123. void blk_mq_start_request(struct request *rq)
  1124. {
  1125. struct request_queue *q = rq->q;
  1126. trace_block_rq_issue(rq);
  1127. if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags) &&
  1128. !blk_rq_is_passthrough(rq)) {
  1129. rq->io_start_time_ns = blk_time_get_ns();
  1130. rq->stats_sectors = blk_rq_sectors(rq);
  1131. rq->rq_flags |= RQF_STATS;
  1132. rq_qos_issue(q, rq);
  1133. }
  1134. WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
  1135. blk_add_timer(rq);
  1136. WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
  1137. rq->mq_hctx->tags->rqs[rq->tag] = rq;
  1138. if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
  1139. blk_integrity_prepare(rq);
  1140. if (rq->bio && rq->bio->bi_opf & REQ_POLLED)
  1141. WRITE_ONCE(rq->bio->bi_cookie, rq->mq_hctx->queue_num);
  1142. }
  1143. EXPORT_SYMBOL(blk_mq_start_request);
  1144. /*
  1145. * Allow 2x BLK_MAX_REQUEST_COUNT requests on plug queue for multiple
  1146. * queues. This is important for md arrays to benefit from merging
  1147. * requests.
  1148. */
  1149. static inline unsigned short blk_plug_max_rq_count(struct blk_plug *plug)
  1150. {
  1151. if (plug->multiple_queues)
  1152. return BLK_MAX_REQUEST_COUNT * 2;
  1153. return BLK_MAX_REQUEST_COUNT;
  1154. }
  1155. static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
  1156. {
  1157. struct request *last = rq_list_peek(&plug->mq_list);
  1158. if (!plug->rq_count) {
  1159. trace_block_plug(rq->q);
  1160. } else if (plug->rq_count >= blk_plug_max_rq_count(plug) ||
  1161. (!blk_queue_nomerges(rq->q) &&
  1162. blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
  1163. blk_mq_flush_plug_list(plug, false);
  1164. last = NULL;
  1165. trace_block_plug(rq->q);
  1166. }
  1167. if (!plug->multiple_queues && last && last->q != rq->q)
  1168. plug->multiple_queues = true;
  1169. /*
  1170. * Any request allocated from sched tags can't be issued to
  1171. * ->queue_rqs() directly
  1172. */
  1173. if (!plug->has_elevator && (rq->rq_flags & RQF_SCHED_TAGS))
  1174. plug->has_elevator = true;
  1175. rq->rq_next = NULL;
  1176. rq_list_add(&plug->mq_list, rq);
  1177. plug->rq_count++;
  1178. }
  1179. /**
  1180. * blk_execute_rq_nowait - insert a request to I/O scheduler for execution
  1181. * @rq: request to insert
  1182. * @at_head: insert request at head or tail of queue
  1183. *
  1184. * Description:
  1185. * Insert a fully prepared request at the back of the I/O scheduler queue
  1186. * for execution. Don't wait for completion.
  1187. *
  1188. * Note:
  1189. * This function will invoke @done directly if the queue is dead.
  1190. */
  1191. void blk_execute_rq_nowait(struct request *rq, bool at_head)
  1192. {
  1193. struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
  1194. WARN_ON(irqs_disabled());
  1195. WARN_ON(!blk_rq_is_passthrough(rq));
  1196. blk_account_io_start(rq);
  1197. if (current->plug && !at_head) {
  1198. blk_add_rq_to_plug(current->plug, rq);
  1199. return;
  1200. }
  1201. blk_mq_insert_request(rq, at_head ? BLK_MQ_INSERT_AT_HEAD : 0);
  1202. blk_mq_run_hw_queue(hctx, hctx->flags & BLK_MQ_F_BLOCKING);
  1203. }
  1204. EXPORT_SYMBOL_GPL(blk_execute_rq_nowait);
  1205. struct blk_rq_wait {
  1206. struct completion done;
  1207. blk_status_t ret;
  1208. };
  1209. static enum rq_end_io_ret blk_end_sync_rq(struct request *rq, blk_status_t ret)
  1210. {
  1211. struct blk_rq_wait *wait = rq->end_io_data;
  1212. wait->ret = ret;
  1213. complete(&wait->done);
  1214. return RQ_END_IO_NONE;
  1215. }
  1216. bool blk_rq_is_poll(struct request *rq)
  1217. {
  1218. if (!rq->mq_hctx)
  1219. return false;
  1220. if (rq->mq_hctx->type != HCTX_TYPE_POLL)
  1221. return false;
  1222. return true;
  1223. }
  1224. EXPORT_SYMBOL_GPL(blk_rq_is_poll);
  1225. static void blk_rq_poll_completion(struct request *rq, struct completion *wait)
  1226. {
  1227. do {
  1228. blk_hctx_poll(rq->q, rq->mq_hctx, NULL, 0);
  1229. cond_resched();
  1230. } while (!completion_done(wait));
  1231. }
  1232. /**
  1233. * blk_execute_rq - insert a request into queue for execution
  1234. * @rq: request to insert
  1235. * @at_head: insert request at head or tail of queue
  1236. *
  1237. * Description:
  1238. * Insert a fully prepared request at the back of the I/O scheduler queue
  1239. * for execution and wait for completion.
  1240. * Return: The blk_status_t result provided to blk_mq_end_request().
  1241. */
  1242. blk_status_t blk_execute_rq(struct request *rq, bool at_head)
  1243. {
  1244. struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
  1245. struct blk_rq_wait wait = {
  1246. .done = COMPLETION_INITIALIZER_ONSTACK(wait.done),
  1247. };
  1248. WARN_ON(irqs_disabled());
  1249. WARN_ON(!blk_rq_is_passthrough(rq));
  1250. rq->end_io_data = &wait;
  1251. rq->end_io = blk_end_sync_rq;
  1252. blk_account_io_start(rq);
  1253. blk_mq_insert_request(rq, at_head ? BLK_MQ_INSERT_AT_HEAD : 0);
  1254. blk_mq_run_hw_queue(hctx, false);
  1255. if (blk_rq_is_poll(rq))
  1256. blk_rq_poll_completion(rq, &wait.done);
  1257. else
  1258. blk_wait_io(&wait.done);
  1259. return wait.ret;
  1260. }
  1261. EXPORT_SYMBOL(blk_execute_rq);
  1262. static void __blk_mq_requeue_request(struct request *rq)
  1263. {
  1264. struct request_queue *q = rq->q;
  1265. blk_mq_put_driver_tag(rq);
  1266. trace_block_rq_requeue(rq);
  1267. rq_qos_requeue(q, rq);
  1268. if (blk_mq_request_started(rq)) {
  1269. WRITE_ONCE(rq->state, MQ_RQ_IDLE);
  1270. rq->rq_flags &= ~RQF_TIMED_OUT;
  1271. }
  1272. }
  1273. void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
  1274. {
  1275. struct request_queue *q = rq->q;
  1276. unsigned long flags;
  1277. __blk_mq_requeue_request(rq);
  1278. /* this request will be re-inserted to io scheduler queue */
  1279. blk_mq_sched_requeue_request(rq);
  1280. spin_lock_irqsave(&q->requeue_lock, flags);
  1281. list_add_tail(&rq->queuelist, &q->requeue_list);
  1282. spin_unlock_irqrestore(&q->requeue_lock, flags);
  1283. if (kick_requeue_list)
  1284. blk_mq_kick_requeue_list(q);
  1285. }
  1286. EXPORT_SYMBOL(blk_mq_requeue_request);
  1287. static void blk_mq_requeue_work(struct work_struct *work)
  1288. {
  1289. struct request_queue *q =
  1290. container_of(work, struct request_queue, requeue_work.work);
  1291. LIST_HEAD(rq_list);
  1292. LIST_HEAD(flush_list);
  1293. struct request *rq;
  1294. spin_lock_irq(&q->requeue_lock);
  1295. list_splice_init(&q->requeue_list, &rq_list);
  1296. list_splice_init(&q->flush_list, &flush_list);
  1297. spin_unlock_irq(&q->requeue_lock);
  1298. while (!list_empty(&rq_list)) {
  1299. rq = list_entry(rq_list.next, struct request, queuelist);
  1300. /*
  1301. * If RQF_DONTPREP ist set, the request has been started by the
  1302. * driver already and might have driver-specific data allocated
  1303. * already. Insert it into the hctx dispatch list to avoid
  1304. * block layer merges for the request.
  1305. */
  1306. if (rq->rq_flags & RQF_DONTPREP) {
  1307. list_del_init(&rq->queuelist);
  1308. blk_mq_request_bypass_insert(rq, 0);
  1309. } else {
  1310. list_del_init(&rq->queuelist);
  1311. blk_mq_insert_request(rq, BLK_MQ_INSERT_AT_HEAD);
  1312. }
  1313. }
  1314. while (!list_empty(&flush_list)) {
  1315. rq = list_entry(flush_list.next, struct request, queuelist);
  1316. list_del_init(&rq->queuelist);
  1317. blk_mq_insert_request(rq, 0);
  1318. }
  1319. blk_mq_run_hw_queues(q, false);
  1320. }
  1321. void blk_mq_kick_requeue_list(struct request_queue *q)
  1322. {
  1323. kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
  1324. }
  1325. EXPORT_SYMBOL(blk_mq_kick_requeue_list);
  1326. void blk_mq_delay_kick_requeue_list(struct request_queue *q,
  1327. unsigned long msecs)
  1328. {
  1329. kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
  1330. msecs_to_jiffies(msecs));
  1331. }
  1332. EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
  1333. static bool blk_is_flush_data_rq(struct request *rq)
  1334. {
  1335. return (rq->rq_flags & RQF_FLUSH_SEQ) && !is_flush_rq(rq);
  1336. }
  1337. static bool blk_mq_rq_inflight(struct request *rq, void *priv)
  1338. {
  1339. /*
  1340. * If we find a request that isn't idle we know the queue is busy
  1341. * as it's checked in the iter.
  1342. * Return false to stop the iteration.
  1343. *
  1344. * In case of queue quiesce, if one flush data request is completed,
  1345. * don't count it as inflight given the flush sequence is suspended,
  1346. * and the original flush data request is invisible to driver, just
  1347. * like other pending requests because of quiesce
  1348. */
  1349. if (blk_mq_request_started(rq) && !(blk_queue_quiesced(rq->q) &&
  1350. blk_is_flush_data_rq(rq) &&
  1351. blk_mq_request_completed(rq))) {
  1352. bool *busy = priv;
  1353. *busy = true;
  1354. return false;
  1355. }
  1356. return true;
  1357. }
  1358. bool blk_mq_queue_inflight(struct request_queue *q)
  1359. {
  1360. bool busy = false;
  1361. blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
  1362. return busy;
  1363. }
  1364. EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
  1365. static void blk_mq_rq_timed_out(struct request *req)
  1366. {
  1367. req->rq_flags |= RQF_TIMED_OUT;
  1368. if (req->q->mq_ops->timeout) {
  1369. enum blk_eh_timer_return ret;
  1370. ret = req->q->mq_ops->timeout(req);
  1371. if (ret == BLK_EH_DONE)
  1372. return;
  1373. WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
  1374. }
  1375. blk_add_timer(req);
  1376. }
  1377. struct blk_expired_data {
  1378. bool has_timedout_rq;
  1379. unsigned long next;
  1380. unsigned long timeout_start;
  1381. };
  1382. static bool blk_mq_req_expired(struct request *rq, struct blk_expired_data *expired)
  1383. {
  1384. unsigned long deadline;
  1385. if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
  1386. return false;
  1387. if (rq->rq_flags & RQF_TIMED_OUT)
  1388. return false;
  1389. deadline = READ_ONCE(rq->deadline);
  1390. if (time_after_eq(expired->timeout_start, deadline))
  1391. return true;
  1392. if (expired->next == 0)
  1393. expired->next = deadline;
  1394. else if (time_after(expired->next, deadline))
  1395. expired->next = deadline;
  1396. return false;
  1397. }
  1398. void blk_mq_put_rq_ref(struct request *rq)
  1399. {
  1400. if (is_flush_rq(rq)) {
  1401. if (rq->end_io(rq, 0) == RQ_END_IO_FREE)
  1402. blk_mq_free_request(rq);
  1403. } else if (req_ref_put_and_test(rq)) {
  1404. __blk_mq_free_request(rq);
  1405. }
  1406. }
  1407. static bool blk_mq_check_expired(struct request *rq, void *priv)
  1408. {
  1409. struct blk_expired_data *expired = priv;
  1410. /*
  1411. * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot
  1412. * be reallocated underneath the timeout handler's processing, then
  1413. * the expire check is reliable. If the request is not expired, then
  1414. * it was completed and reallocated as a new request after returning
  1415. * from blk_mq_check_expired().
  1416. */
  1417. if (blk_mq_req_expired(rq, expired)) {
  1418. expired->has_timedout_rq = true;
  1419. return false;
  1420. }
  1421. return true;
  1422. }
  1423. static bool blk_mq_handle_expired(struct request *rq, void *priv)
  1424. {
  1425. struct blk_expired_data *expired = priv;
  1426. if (blk_mq_req_expired(rq, expired))
  1427. blk_mq_rq_timed_out(rq);
  1428. return true;
  1429. }
  1430. static void blk_mq_timeout_work(struct work_struct *work)
  1431. {
  1432. struct request_queue *q =
  1433. container_of(work, struct request_queue, timeout_work);
  1434. struct blk_expired_data expired = {
  1435. .timeout_start = jiffies,
  1436. };
  1437. struct blk_mq_hw_ctx *hctx;
  1438. unsigned long i;
  1439. /* A deadlock might occur if a request is stuck requiring a
  1440. * timeout at the same time a queue freeze is waiting
  1441. * completion, since the timeout code would not be able to
  1442. * acquire the queue reference here.
  1443. *
  1444. * That's why we don't use blk_queue_enter here; instead, we use
  1445. * percpu_ref_tryget directly, because we need to be able to
  1446. * obtain a reference even in the short window between the queue
  1447. * starting to freeze, by dropping the first reference in
  1448. * blk_freeze_queue_start, and the moment the last request is
  1449. * consumed, marked by the instant q_usage_counter reaches
  1450. * zero.
  1451. */
  1452. if (!percpu_ref_tryget(&q->q_usage_counter))
  1453. return;
  1454. /* check if there is any timed-out request */
  1455. blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &expired);
  1456. if (expired.has_timedout_rq) {
  1457. /*
  1458. * Before walking tags, we must ensure any submit started
  1459. * before the current time has finished. Since the submit
  1460. * uses srcu or rcu, wait for a synchronization point to
  1461. * ensure all running submits have finished
  1462. */
  1463. blk_mq_wait_quiesce_done(q->tag_set);
  1464. expired.next = 0;
  1465. blk_mq_queue_tag_busy_iter(q, blk_mq_handle_expired, &expired);
  1466. }
  1467. if (expired.next != 0) {
  1468. mod_timer(&q->timeout, expired.next);
  1469. } else {
  1470. /*
  1471. * Request timeouts are handled as a forward rolling timer. If
  1472. * we end up here it means that no requests are pending and
  1473. * also that no request has been pending for a while. Mark
  1474. * each hctx as idle.
  1475. */
  1476. queue_for_each_hw_ctx(q, hctx, i) {
  1477. /* the hctx may be unmapped, so check it here */
  1478. if (blk_mq_hw_queue_mapped(hctx))
  1479. blk_mq_tag_idle(hctx);
  1480. }
  1481. }
  1482. blk_queue_exit(q);
  1483. }
  1484. struct flush_busy_ctx_data {
  1485. struct blk_mq_hw_ctx *hctx;
  1486. struct list_head *list;
  1487. };
  1488. static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
  1489. {
  1490. struct flush_busy_ctx_data *flush_data = data;
  1491. struct blk_mq_hw_ctx *hctx = flush_data->hctx;
  1492. struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
  1493. enum hctx_type type = hctx->type;
  1494. spin_lock(&ctx->lock);
  1495. list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
  1496. sbitmap_clear_bit(sb, bitnr);
  1497. spin_unlock(&ctx->lock);
  1498. return true;
  1499. }
  1500. /*
  1501. * Process software queues that have been marked busy, splicing them
  1502. * to the for-dispatch
  1503. */
  1504. void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
  1505. {
  1506. struct flush_busy_ctx_data data = {
  1507. .hctx = hctx,
  1508. .list = list,
  1509. };
  1510. sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
  1511. }
  1512. EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
  1513. struct dispatch_rq_data {
  1514. struct blk_mq_hw_ctx *hctx;
  1515. struct request *rq;
  1516. };
  1517. static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
  1518. void *data)
  1519. {
  1520. struct dispatch_rq_data *dispatch_data = data;
  1521. struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
  1522. struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
  1523. enum hctx_type type = hctx->type;
  1524. spin_lock(&ctx->lock);
  1525. if (!list_empty(&ctx->rq_lists[type])) {
  1526. dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
  1527. list_del_init(&dispatch_data->rq->queuelist);
  1528. if (list_empty(&ctx->rq_lists[type]))
  1529. sbitmap_clear_bit(sb, bitnr);
  1530. }
  1531. spin_unlock(&ctx->lock);
  1532. return !dispatch_data->rq;
  1533. }
  1534. struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
  1535. struct blk_mq_ctx *start)
  1536. {
  1537. unsigned off = start ? start->index_hw[hctx->type] : 0;
  1538. struct dispatch_rq_data data = {
  1539. .hctx = hctx,
  1540. .rq = NULL,
  1541. };
  1542. __sbitmap_for_each_set(&hctx->ctx_map, off,
  1543. dispatch_rq_from_ctx, &data);
  1544. return data.rq;
  1545. }
  1546. bool __blk_mq_alloc_driver_tag(struct request *rq)
  1547. {
  1548. struct sbitmap_queue *bt = &rq->mq_hctx->tags->bitmap_tags;
  1549. unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags;
  1550. int tag;
  1551. blk_mq_tag_busy(rq->mq_hctx);
  1552. if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) {
  1553. bt = &rq->mq_hctx->tags->breserved_tags;
  1554. tag_offset = 0;
  1555. } else {
  1556. if (!hctx_may_queue(rq->mq_hctx, bt))
  1557. return false;
  1558. }
  1559. tag = __sbitmap_queue_get(bt);
  1560. if (tag == BLK_MQ_NO_TAG)
  1561. return false;
  1562. rq->tag = tag + tag_offset;
  1563. blk_mq_inc_active_requests(rq->mq_hctx);
  1564. return true;
  1565. }
  1566. static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
  1567. int flags, void *key)
  1568. {
  1569. struct blk_mq_hw_ctx *hctx;
  1570. hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
  1571. spin_lock(&hctx->dispatch_wait_lock);
  1572. if (!list_empty(&wait->entry)) {
  1573. struct sbitmap_queue *sbq;
  1574. list_del_init(&wait->entry);
  1575. sbq = &hctx->tags->bitmap_tags;
  1576. atomic_dec(&sbq->ws_active);
  1577. }
  1578. spin_unlock(&hctx->dispatch_wait_lock);
  1579. blk_mq_run_hw_queue(hctx, true);
  1580. return 1;
  1581. }
  1582. /*
  1583. * Mark us waiting for a tag. For shared tags, this involves hooking us into
  1584. * the tag wakeups. For non-shared tags, we can simply mark us needing a
  1585. * restart. For both cases, take care to check the condition again after
  1586. * marking us as waiting.
  1587. */
  1588. static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
  1589. struct request *rq)
  1590. {
  1591. struct sbitmap_queue *sbq;
  1592. struct wait_queue_head *wq;
  1593. wait_queue_entry_t *wait;
  1594. bool ret;
  1595. if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) &&
  1596. !(blk_mq_is_shared_tags(hctx->flags))) {
  1597. blk_mq_sched_mark_restart_hctx(hctx);
  1598. /*
  1599. * It's possible that a tag was freed in the window between the
  1600. * allocation failure and adding the hardware queue to the wait
  1601. * queue.
  1602. *
  1603. * Don't clear RESTART here, someone else could have set it.
  1604. * At most this will cost an extra queue run.
  1605. */
  1606. return blk_mq_get_driver_tag(rq);
  1607. }
  1608. wait = &hctx->dispatch_wait;
  1609. if (!list_empty_careful(&wait->entry))
  1610. return false;
  1611. if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag))
  1612. sbq = &hctx->tags->breserved_tags;
  1613. else
  1614. sbq = &hctx->tags->bitmap_tags;
  1615. wq = &bt_wait_ptr(sbq, hctx)->wait;
  1616. spin_lock_irq(&wq->lock);
  1617. spin_lock(&hctx->dispatch_wait_lock);
  1618. if (!list_empty(&wait->entry)) {
  1619. spin_unlock(&hctx->dispatch_wait_lock);
  1620. spin_unlock_irq(&wq->lock);
  1621. return false;
  1622. }
  1623. atomic_inc(&sbq->ws_active);
  1624. wait->flags &= ~WQ_FLAG_EXCLUSIVE;
  1625. __add_wait_queue(wq, wait);
  1626. /*
  1627. * Add one explicit barrier since blk_mq_get_driver_tag() may
  1628. * not imply barrier in case of failure.
  1629. *
  1630. * Order adding us to wait queue and allocating driver tag.
  1631. *
  1632. * The pair is the one implied in sbitmap_queue_wake_up() which
  1633. * orders clearing sbitmap tag bits and waitqueue_active() in
  1634. * __sbitmap_queue_wake_up(), since waitqueue_active() is lockless
  1635. *
  1636. * Otherwise, re-order of adding wait queue and getting driver tag
  1637. * may cause __sbitmap_queue_wake_up() to wake up nothing because
  1638. * the waitqueue_active() may not observe us in wait queue.
  1639. */
  1640. smp_mb();
  1641. /*
  1642. * It's possible that a tag was freed in the window between the
  1643. * allocation failure and adding the hardware queue to the wait
  1644. * queue.
  1645. */
  1646. ret = blk_mq_get_driver_tag(rq);
  1647. if (!ret) {
  1648. spin_unlock(&hctx->dispatch_wait_lock);
  1649. spin_unlock_irq(&wq->lock);
  1650. return false;
  1651. }
  1652. /*
  1653. * We got a tag, remove ourselves from the wait queue to ensure
  1654. * someone else gets the wakeup.
  1655. */
  1656. list_del_init(&wait->entry);
  1657. atomic_dec(&sbq->ws_active);
  1658. spin_unlock(&hctx->dispatch_wait_lock);
  1659. spin_unlock_irq(&wq->lock);
  1660. return true;
  1661. }
  1662. #define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
  1663. #define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
  1664. /*
  1665. * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
  1666. * - EWMA is one simple way to compute running average value
  1667. * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
  1668. * - take 4 as factor for avoiding to get too small(0) result, and this
  1669. * factor doesn't matter because EWMA decreases exponentially
  1670. */
  1671. static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
  1672. {
  1673. unsigned int ewma;
  1674. ewma = hctx->dispatch_busy;
  1675. if (!ewma && !busy)
  1676. return;
  1677. ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
  1678. if (busy)
  1679. ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
  1680. ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
  1681. hctx->dispatch_busy = ewma;
  1682. }
  1683. #define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
  1684. static void blk_mq_handle_dev_resource(struct request *rq,
  1685. struct list_head *list)
  1686. {
  1687. list_add(&rq->queuelist, list);
  1688. __blk_mq_requeue_request(rq);
  1689. }
  1690. enum prep_dispatch {
  1691. PREP_DISPATCH_OK,
  1692. PREP_DISPATCH_NO_TAG,
  1693. PREP_DISPATCH_NO_BUDGET,
  1694. };
  1695. static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq,
  1696. bool need_budget)
  1697. {
  1698. struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
  1699. int budget_token = -1;
  1700. if (need_budget) {
  1701. budget_token = blk_mq_get_dispatch_budget(rq->q);
  1702. if (budget_token < 0) {
  1703. blk_mq_put_driver_tag(rq);
  1704. return PREP_DISPATCH_NO_BUDGET;
  1705. }
  1706. blk_mq_set_rq_budget_token(rq, budget_token);
  1707. }
  1708. if (!blk_mq_get_driver_tag(rq)) {
  1709. /*
  1710. * The initial allocation attempt failed, so we need to
  1711. * rerun the hardware queue when a tag is freed. The
  1712. * waitqueue takes care of that. If the queue is run
  1713. * before we add this entry back on the dispatch list,
  1714. * we'll re-run it below.
  1715. */
  1716. if (!blk_mq_mark_tag_wait(hctx, rq)) {
  1717. /*
  1718. * All budgets not got from this function will be put
  1719. * together during handling partial dispatch
  1720. */
  1721. if (need_budget)
  1722. blk_mq_put_dispatch_budget(rq->q, budget_token);
  1723. return PREP_DISPATCH_NO_TAG;
  1724. }
  1725. }
  1726. return PREP_DISPATCH_OK;
  1727. }
  1728. /* release all allocated budgets before calling to blk_mq_dispatch_rq_list */
  1729. static void blk_mq_release_budgets(struct request_queue *q,
  1730. struct list_head *list)
  1731. {
  1732. struct request *rq;
  1733. list_for_each_entry(rq, list, queuelist) {
  1734. int budget_token = blk_mq_get_rq_budget_token(rq);
  1735. if (budget_token >= 0)
  1736. blk_mq_put_dispatch_budget(q, budget_token);
  1737. }
  1738. }
  1739. /*
  1740. * blk_mq_commit_rqs will notify driver using bd->last that there is no
  1741. * more requests. (See comment in struct blk_mq_ops for commit_rqs for
  1742. * details)
  1743. * Attention, we should explicitly call this in unusual cases:
  1744. * 1) did not queue everything initially scheduled to queue
  1745. * 2) the last attempt to queue a request failed
  1746. */
  1747. static void blk_mq_commit_rqs(struct blk_mq_hw_ctx *hctx, int queued,
  1748. bool from_schedule)
  1749. {
  1750. if (hctx->queue->mq_ops->commit_rqs && queued) {
  1751. trace_block_unplug(hctx->queue, queued, !from_schedule);
  1752. hctx->queue->mq_ops->commit_rqs(hctx);
  1753. }
  1754. }
  1755. /*
  1756. * Returns true if we did some work AND can potentially do more.
  1757. */
  1758. bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list,
  1759. unsigned int nr_budgets)
  1760. {
  1761. enum prep_dispatch prep;
  1762. struct request_queue *q = hctx->queue;
  1763. struct request *rq;
  1764. int queued;
  1765. blk_status_t ret = BLK_STS_OK;
  1766. bool needs_resource = false;
  1767. if (list_empty(list))
  1768. return false;
  1769. /*
  1770. * Now process all the entries, sending them to the driver.
  1771. */
  1772. queued = 0;
  1773. do {
  1774. struct blk_mq_queue_data bd;
  1775. rq = list_first_entry(list, struct request, queuelist);
  1776. WARN_ON_ONCE(hctx != rq->mq_hctx);
  1777. prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets);
  1778. if (prep != PREP_DISPATCH_OK)
  1779. break;
  1780. list_del_init(&rq->queuelist);
  1781. bd.rq = rq;
  1782. bd.last = list_empty(list);
  1783. /*
  1784. * once the request is queued to lld, no need to cover the
  1785. * budget any more
  1786. */
  1787. if (nr_budgets)
  1788. nr_budgets--;
  1789. ret = q->mq_ops->queue_rq(hctx, &bd);
  1790. switch (ret) {
  1791. case BLK_STS_OK:
  1792. queued++;
  1793. break;
  1794. case BLK_STS_RESOURCE:
  1795. needs_resource = true;
  1796. fallthrough;
  1797. case BLK_STS_DEV_RESOURCE:
  1798. blk_mq_handle_dev_resource(rq, list);
  1799. goto out;
  1800. default:
  1801. blk_mq_end_request(rq, ret);
  1802. }
  1803. } while (!list_empty(list));
  1804. out:
  1805. /* If we didn't flush the entire list, we could have told the driver
  1806. * there was more coming, but that turned out to be a lie.
  1807. */
  1808. if (!list_empty(list) || ret != BLK_STS_OK)
  1809. blk_mq_commit_rqs(hctx, queued, false);
  1810. /*
  1811. * Any items that need requeuing? Stuff them into hctx->dispatch,
  1812. * that is where we will continue on next queue run.
  1813. */
  1814. if (!list_empty(list)) {
  1815. bool needs_restart;
  1816. /* For non-shared tags, the RESTART check will suffice */
  1817. bool no_tag = prep == PREP_DISPATCH_NO_TAG &&
  1818. ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) ||
  1819. blk_mq_is_shared_tags(hctx->flags));
  1820. if (nr_budgets)
  1821. blk_mq_release_budgets(q, list);
  1822. spin_lock(&hctx->lock);
  1823. list_splice_tail_init(list, &hctx->dispatch);
  1824. spin_unlock(&hctx->lock);
  1825. /*
  1826. * Order adding requests to hctx->dispatch and checking
  1827. * SCHED_RESTART flag. The pair of this smp_mb() is the one
  1828. * in blk_mq_sched_restart(). Avoid restart code path to
  1829. * miss the new added requests to hctx->dispatch, meantime
  1830. * SCHED_RESTART is observed here.
  1831. */
  1832. smp_mb();
  1833. /*
  1834. * If SCHED_RESTART was set by the caller of this function and
  1835. * it is no longer set that means that it was cleared by another
  1836. * thread and hence that a queue rerun is needed.
  1837. *
  1838. * If 'no_tag' is set, that means that we failed getting
  1839. * a driver tag with an I/O scheduler attached. If our dispatch
  1840. * waitqueue is no longer active, ensure that we run the queue
  1841. * AFTER adding our entries back to the list.
  1842. *
  1843. * If no I/O scheduler has been configured it is possible that
  1844. * the hardware queue got stopped and restarted before requests
  1845. * were pushed back onto the dispatch list. Rerun the queue to
  1846. * avoid starvation. Notes:
  1847. * - blk_mq_run_hw_queue() checks whether or not a queue has
  1848. * been stopped before rerunning a queue.
  1849. * - Some but not all block drivers stop a queue before
  1850. * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
  1851. * and dm-rq.
  1852. *
  1853. * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
  1854. * bit is set, run queue after a delay to avoid IO stalls
  1855. * that could otherwise occur if the queue is idle. We'll do
  1856. * similar if we couldn't get budget or couldn't lock a zone
  1857. * and SCHED_RESTART is set.
  1858. */
  1859. needs_restart = blk_mq_sched_needs_restart(hctx);
  1860. if (prep == PREP_DISPATCH_NO_BUDGET)
  1861. needs_resource = true;
  1862. if (!needs_restart ||
  1863. (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
  1864. blk_mq_run_hw_queue(hctx, true);
  1865. else if (needs_resource)
  1866. blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
  1867. blk_mq_update_dispatch_busy(hctx, true);
  1868. return false;
  1869. }
  1870. blk_mq_update_dispatch_busy(hctx, false);
  1871. return true;
  1872. }
  1873. static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
  1874. {
  1875. int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
  1876. if (cpu >= nr_cpu_ids)
  1877. cpu = cpumask_first(hctx->cpumask);
  1878. return cpu;
  1879. }
  1880. /*
  1881. * ->next_cpu is always calculated from hctx->cpumask, so simply use
  1882. * it for speeding up the check
  1883. */
  1884. static bool blk_mq_hctx_empty_cpumask(struct blk_mq_hw_ctx *hctx)
  1885. {
  1886. return hctx->next_cpu >= nr_cpu_ids;
  1887. }
  1888. /*
  1889. * It'd be great if the workqueue API had a way to pass
  1890. * in a mask and had some smarts for more clever placement.
  1891. * For now we just round-robin here, switching for every
  1892. * BLK_MQ_CPU_WORK_BATCH queued items.
  1893. */
  1894. static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
  1895. {
  1896. bool tried = false;
  1897. int next_cpu = hctx->next_cpu;
  1898. /* Switch to unbound if no allowable CPUs in this hctx */
  1899. if (hctx->queue->nr_hw_queues == 1 || blk_mq_hctx_empty_cpumask(hctx))
  1900. return WORK_CPU_UNBOUND;
  1901. if (--hctx->next_cpu_batch <= 0) {
  1902. select_cpu:
  1903. next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
  1904. cpu_online_mask);
  1905. if (next_cpu >= nr_cpu_ids)
  1906. next_cpu = blk_mq_first_mapped_cpu(hctx);
  1907. hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
  1908. }
  1909. /*
  1910. * Do unbound schedule if we can't find a online CPU for this hctx,
  1911. * and it should only happen in the path of handling CPU DEAD.
  1912. */
  1913. if (!cpu_online(next_cpu)) {
  1914. if (!tried) {
  1915. tried = true;
  1916. goto select_cpu;
  1917. }
  1918. /*
  1919. * Make sure to re-select CPU next time once after CPUs
  1920. * in hctx->cpumask become online again.
  1921. */
  1922. hctx->next_cpu = next_cpu;
  1923. hctx->next_cpu_batch = 1;
  1924. return WORK_CPU_UNBOUND;
  1925. }
  1926. hctx->next_cpu = next_cpu;
  1927. return next_cpu;
  1928. }
  1929. /**
  1930. * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
  1931. * @hctx: Pointer to the hardware queue to run.
  1932. * @msecs: Milliseconds of delay to wait before running the queue.
  1933. *
  1934. * Run a hardware queue asynchronously with a delay of @msecs.
  1935. */
  1936. void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
  1937. {
  1938. if (unlikely(blk_mq_hctx_stopped(hctx)))
  1939. return;
  1940. kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
  1941. msecs_to_jiffies(msecs));
  1942. }
  1943. EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
  1944. static inline bool blk_mq_hw_queue_need_run(struct blk_mq_hw_ctx *hctx)
  1945. {
  1946. bool need_run;
  1947. /*
  1948. * When queue is quiesced, we may be switching io scheduler, or
  1949. * updating nr_hw_queues, or other things, and we can't run queue
  1950. * any more, even blk_mq_hctx_has_pending() can't be called safely.
  1951. *
  1952. * And queue will be rerun in blk_mq_unquiesce_queue() if it is
  1953. * quiesced.
  1954. */
  1955. __blk_mq_run_dispatch_ops(hctx->queue, false,
  1956. need_run = !blk_queue_quiesced(hctx->queue) &&
  1957. blk_mq_hctx_has_pending(hctx));
  1958. return need_run;
  1959. }
  1960. /**
  1961. * blk_mq_run_hw_queue - Start to run a hardware queue.
  1962. * @hctx: Pointer to the hardware queue to run.
  1963. * @async: If we want to run the queue asynchronously.
  1964. *
  1965. * Check if the request queue is not in a quiesced state and if there are
  1966. * pending requests to be sent. If this is true, run the queue to send requests
  1967. * to hardware.
  1968. */
  1969. void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  1970. {
  1971. bool need_run;
  1972. /*
  1973. * We can't run the queue inline with interrupts disabled.
  1974. */
  1975. WARN_ON_ONCE(!async && in_interrupt());
  1976. might_sleep_if(!async && hctx->flags & BLK_MQ_F_BLOCKING);
  1977. need_run = blk_mq_hw_queue_need_run(hctx);
  1978. if (!need_run) {
  1979. unsigned long flags;
  1980. /*
  1981. * Synchronize with blk_mq_unquiesce_queue(), because we check
  1982. * if hw queue is quiesced locklessly above, we need the use
  1983. * ->queue_lock to make sure we see the up-to-date status to
  1984. * not miss rerunning the hw queue.
  1985. */
  1986. spin_lock_irqsave(&hctx->queue->queue_lock, flags);
  1987. need_run = blk_mq_hw_queue_need_run(hctx);
  1988. spin_unlock_irqrestore(&hctx->queue->queue_lock, flags);
  1989. if (!need_run)
  1990. return;
  1991. }
  1992. if (async || !cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)) {
  1993. blk_mq_delay_run_hw_queue(hctx, 0);
  1994. return;
  1995. }
  1996. blk_mq_run_dispatch_ops(hctx->queue,
  1997. blk_mq_sched_dispatch_requests(hctx));
  1998. }
  1999. EXPORT_SYMBOL(blk_mq_run_hw_queue);
  2000. /*
  2001. * Return prefered queue to dispatch from (if any) for non-mq aware IO
  2002. * scheduler.
  2003. */
  2004. static struct blk_mq_hw_ctx *blk_mq_get_sq_hctx(struct request_queue *q)
  2005. {
  2006. struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
  2007. /*
  2008. * If the IO scheduler does not respect hardware queues when
  2009. * dispatching, we just don't bother with multiple HW queues and
  2010. * dispatch from hctx for the current CPU since running multiple queues
  2011. * just causes lock contention inside the scheduler and pointless cache
  2012. * bouncing.
  2013. */
  2014. struct blk_mq_hw_ctx *hctx = ctx->hctxs[HCTX_TYPE_DEFAULT];
  2015. if (!blk_mq_hctx_stopped(hctx))
  2016. return hctx;
  2017. return NULL;
  2018. }
  2019. /**
  2020. * blk_mq_run_hw_queues - Run all hardware queues in a request queue.
  2021. * @q: Pointer to the request queue to run.
  2022. * @async: If we want to run the queue asynchronously.
  2023. */
  2024. void blk_mq_run_hw_queues(struct request_queue *q, bool async)
  2025. {
  2026. struct blk_mq_hw_ctx *hctx, *sq_hctx;
  2027. unsigned long i;
  2028. sq_hctx = NULL;
  2029. if (blk_queue_sq_sched(q))
  2030. sq_hctx = blk_mq_get_sq_hctx(q);
  2031. queue_for_each_hw_ctx(q, hctx, i) {
  2032. if (blk_mq_hctx_stopped(hctx))
  2033. continue;
  2034. /*
  2035. * Dispatch from this hctx either if there's no hctx preferred
  2036. * by IO scheduler or if it has requests that bypass the
  2037. * scheduler.
  2038. */
  2039. if (!sq_hctx || sq_hctx == hctx ||
  2040. !list_empty_careful(&hctx->dispatch))
  2041. blk_mq_run_hw_queue(hctx, async);
  2042. }
  2043. }
  2044. EXPORT_SYMBOL(blk_mq_run_hw_queues);
  2045. /**
  2046. * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously.
  2047. * @q: Pointer to the request queue to run.
  2048. * @msecs: Milliseconds of delay to wait before running the queues.
  2049. */
  2050. void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs)
  2051. {
  2052. struct blk_mq_hw_ctx *hctx, *sq_hctx;
  2053. unsigned long i;
  2054. sq_hctx = NULL;
  2055. if (blk_queue_sq_sched(q))
  2056. sq_hctx = blk_mq_get_sq_hctx(q);
  2057. queue_for_each_hw_ctx(q, hctx, i) {
  2058. if (blk_mq_hctx_stopped(hctx))
  2059. continue;
  2060. /*
  2061. * If there is already a run_work pending, leave the
  2062. * pending delay untouched. Otherwise, a hctx can stall
  2063. * if another hctx is re-delaying the other's work
  2064. * before the work executes.
  2065. */
  2066. if (delayed_work_pending(&hctx->run_work))
  2067. continue;
  2068. /*
  2069. * Dispatch from this hctx either if there's no hctx preferred
  2070. * by IO scheduler or if it has requests that bypass the
  2071. * scheduler.
  2072. */
  2073. if (!sq_hctx || sq_hctx == hctx ||
  2074. !list_empty_careful(&hctx->dispatch))
  2075. blk_mq_delay_run_hw_queue(hctx, msecs);
  2076. }
  2077. }
  2078. EXPORT_SYMBOL(blk_mq_delay_run_hw_queues);
  2079. /*
  2080. * This function is often used for pausing .queue_rq() by driver when
  2081. * there isn't enough resource or some conditions aren't satisfied, and
  2082. * BLK_STS_RESOURCE is usually returned.
  2083. *
  2084. * We do not guarantee that dispatch can be drained or blocked
  2085. * after blk_mq_stop_hw_queue() returns. Please use
  2086. * blk_mq_quiesce_queue() for that requirement.
  2087. */
  2088. void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
  2089. {
  2090. cancel_delayed_work(&hctx->run_work);
  2091. set_bit(BLK_MQ_S_STOPPED, &hctx->state);
  2092. }
  2093. EXPORT_SYMBOL(blk_mq_stop_hw_queue);
  2094. /*
  2095. * This function is often used for pausing .queue_rq() by driver when
  2096. * there isn't enough resource or some conditions aren't satisfied, and
  2097. * BLK_STS_RESOURCE is usually returned.
  2098. *
  2099. * We do not guarantee that dispatch can be drained or blocked
  2100. * after blk_mq_stop_hw_queues() returns. Please use
  2101. * blk_mq_quiesce_queue() for that requirement.
  2102. */
  2103. void blk_mq_stop_hw_queues(struct request_queue *q)
  2104. {
  2105. struct blk_mq_hw_ctx *hctx;
  2106. unsigned long i;
  2107. queue_for_each_hw_ctx(q, hctx, i)
  2108. blk_mq_stop_hw_queue(hctx);
  2109. }
  2110. EXPORT_SYMBOL(blk_mq_stop_hw_queues);
  2111. void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
  2112. {
  2113. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  2114. blk_mq_run_hw_queue(hctx, hctx->flags & BLK_MQ_F_BLOCKING);
  2115. }
  2116. EXPORT_SYMBOL(blk_mq_start_hw_queue);
  2117. void blk_mq_start_hw_queues(struct request_queue *q)
  2118. {
  2119. struct blk_mq_hw_ctx *hctx;
  2120. unsigned long i;
  2121. queue_for_each_hw_ctx(q, hctx, i)
  2122. blk_mq_start_hw_queue(hctx);
  2123. }
  2124. EXPORT_SYMBOL(blk_mq_start_hw_queues);
  2125. void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  2126. {
  2127. if (!blk_mq_hctx_stopped(hctx))
  2128. return;
  2129. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  2130. /*
  2131. * Pairs with the smp_mb() in blk_mq_hctx_stopped() to order the
  2132. * clearing of BLK_MQ_S_STOPPED above and the checking of dispatch
  2133. * list in the subsequent routine.
  2134. */
  2135. smp_mb__after_atomic();
  2136. blk_mq_run_hw_queue(hctx, async);
  2137. }
  2138. EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
  2139. void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
  2140. {
  2141. struct blk_mq_hw_ctx *hctx;
  2142. unsigned long i;
  2143. queue_for_each_hw_ctx(q, hctx, i)
  2144. blk_mq_start_stopped_hw_queue(hctx, async ||
  2145. (hctx->flags & BLK_MQ_F_BLOCKING));
  2146. }
  2147. EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
  2148. static void blk_mq_run_work_fn(struct work_struct *work)
  2149. {
  2150. struct blk_mq_hw_ctx *hctx =
  2151. container_of(work, struct blk_mq_hw_ctx, run_work.work);
  2152. blk_mq_run_dispatch_ops(hctx->queue,
  2153. blk_mq_sched_dispatch_requests(hctx));
  2154. }
  2155. /**
  2156. * blk_mq_request_bypass_insert - Insert a request at dispatch list.
  2157. * @rq: Pointer to request to be inserted.
  2158. * @flags: BLK_MQ_INSERT_*
  2159. *
  2160. * Should only be used carefully, when the caller knows we want to
  2161. * bypass a potential IO scheduler on the target device.
  2162. */
  2163. static void blk_mq_request_bypass_insert(struct request *rq, blk_insert_t flags)
  2164. {
  2165. struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
  2166. spin_lock(&hctx->lock);
  2167. if (flags & BLK_MQ_INSERT_AT_HEAD)
  2168. list_add(&rq->queuelist, &hctx->dispatch);
  2169. else
  2170. list_add_tail(&rq->queuelist, &hctx->dispatch);
  2171. spin_unlock(&hctx->lock);
  2172. }
  2173. static void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx,
  2174. struct blk_mq_ctx *ctx, struct list_head *list,
  2175. bool run_queue_async)
  2176. {
  2177. struct request *rq;
  2178. enum hctx_type type = hctx->type;
  2179. /*
  2180. * Try to issue requests directly if the hw queue isn't busy to save an
  2181. * extra enqueue & dequeue to the sw queue.
  2182. */
  2183. if (!hctx->dispatch_busy && !run_queue_async) {
  2184. blk_mq_run_dispatch_ops(hctx->queue,
  2185. blk_mq_try_issue_list_directly(hctx, list));
  2186. if (list_empty(list))
  2187. goto out;
  2188. }
  2189. /*
  2190. * preemption doesn't flush plug list, so it's possible ctx->cpu is
  2191. * offline now
  2192. */
  2193. list_for_each_entry(rq, list, queuelist) {
  2194. BUG_ON(rq->mq_ctx != ctx);
  2195. trace_block_rq_insert(rq);
  2196. if (rq->cmd_flags & REQ_NOWAIT)
  2197. run_queue_async = true;
  2198. }
  2199. spin_lock(&ctx->lock);
  2200. list_splice_tail_init(list, &ctx->rq_lists[type]);
  2201. blk_mq_hctx_mark_pending(hctx, ctx);
  2202. spin_unlock(&ctx->lock);
  2203. out:
  2204. blk_mq_run_hw_queue(hctx, run_queue_async);
  2205. }
  2206. static void blk_mq_insert_request(struct request *rq, blk_insert_t flags)
  2207. {
  2208. struct request_queue *q = rq->q;
  2209. struct blk_mq_ctx *ctx = rq->mq_ctx;
  2210. struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
  2211. if (blk_rq_is_passthrough(rq)) {
  2212. /*
  2213. * Passthrough request have to be added to hctx->dispatch
  2214. * directly. The device may be in a situation where it can't
  2215. * handle FS request, and always returns BLK_STS_RESOURCE for
  2216. * them, which gets them added to hctx->dispatch.
  2217. *
  2218. * If a passthrough request is required to unblock the queues,
  2219. * and it is added to the scheduler queue, there is no chance to
  2220. * dispatch it given we prioritize requests in hctx->dispatch.
  2221. */
  2222. blk_mq_request_bypass_insert(rq, flags);
  2223. } else if (req_op(rq) == REQ_OP_FLUSH) {
  2224. /*
  2225. * Firstly normal IO request is inserted to scheduler queue or
  2226. * sw queue, meantime we add flush request to dispatch queue(
  2227. * hctx->dispatch) directly and there is at most one in-flight
  2228. * flush request for each hw queue, so it doesn't matter to add
  2229. * flush request to tail or front of the dispatch queue.
  2230. *
  2231. * Secondly in case of NCQ, flush request belongs to non-NCQ
  2232. * command, and queueing it will fail when there is any
  2233. * in-flight normal IO request(NCQ command). When adding flush
  2234. * rq to the front of hctx->dispatch, it is easier to introduce
  2235. * extra time to flush rq's latency because of S_SCHED_RESTART
  2236. * compared with adding to the tail of dispatch queue, then
  2237. * chance of flush merge is increased, and less flush requests
  2238. * will be issued to controller. It is observed that ~10% time
  2239. * is saved in blktests block/004 on disk attached to AHCI/NCQ
  2240. * drive when adding flush rq to the front of hctx->dispatch.
  2241. *
  2242. * Simply queue flush rq to the front of hctx->dispatch so that
  2243. * intensive flush workloads can benefit in case of NCQ HW.
  2244. */
  2245. blk_mq_request_bypass_insert(rq, BLK_MQ_INSERT_AT_HEAD);
  2246. } else if (q->elevator) {
  2247. LIST_HEAD(list);
  2248. WARN_ON_ONCE(rq->tag != BLK_MQ_NO_TAG);
  2249. list_add(&rq->queuelist, &list);
  2250. q->elevator->type->ops.insert_requests(hctx, &list, flags);
  2251. } else {
  2252. trace_block_rq_insert(rq);
  2253. spin_lock(&ctx->lock);
  2254. if (flags & BLK_MQ_INSERT_AT_HEAD)
  2255. list_add(&rq->queuelist, &ctx->rq_lists[hctx->type]);
  2256. else
  2257. list_add_tail(&rq->queuelist,
  2258. &ctx->rq_lists[hctx->type]);
  2259. blk_mq_hctx_mark_pending(hctx, ctx);
  2260. spin_unlock(&ctx->lock);
  2261. }
  2262. }
  2263. static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
  2264. unsigned int nr_segs)
  2265. {
  2266. int err;
  2267. if (bio->bi_opf & REQ_RAHEAD)
  2268. rq->cmd_flags |= REQ_FAILFAST_MASK;
  2269. rq->__sector = bio->bi_iter.bi_sector;
  2270. rq->write_hint = bio->bi_write_hint;
  2271. blk_rq_bio_prep(rq, bio, nr_segs);
  2272. if (bio_integrity(bio))
  2273. rq->nr_integrity_segments = blk_rq_count_integrity_sg(rq->q,
  2274. bio);
  2275. /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */
  2276. err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO);
  2277. WARN_ON_ONCE(err);
  2278. blk_account_io_start(rq);
  2279. }
  2280. static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
  2281. struct request *rq, bool last)
  2282. {
  2283. struct request_queue *q = rq->q;
  2284. struct blk_mq_queue_data bd = {
  2285. .rq = rq,
  2286. .last = last,
  2287. };
  2288. blk_status_t ret;
  2289. /*
  2290. * For OK queue, we are done. For error, caller may kill it.
  2291. * Any other error (busy), just add it to our list as we
  2292. * previously would have done.
  2293. */
  2294. ret = q->mq_ops->queue_rq(hctx, &bd);
  2295. switch (ret) {
  2296. case BLK_STS_OK:
  2297. blk_mq_update_dispatch_busy(hctx, false);
  2298. break;
  2299. case BLK_STS_RESOURCE:
  2300. case BLK_STS_DEV_RESOURCE:
  2301. blk_mq_update_dispatch_busy(hctx, true);
  2302. __blk_mq_requeue_request(rq);
  2303. break;
  2304. default:
  2305. blk_mq_update_dispatch_busy(hctx, false);
  2306. break;
  2307. }
  2308. return ret;
  2309. }
  2310. static bool blk_mq_get_budget_and_tag(struct request *rq)
  2311. {
  2312. int budget_token;
  2313. budget_token = blk_mq_get_dispatch_budget(rq->q);
  2314. if (budget_token < 0)
  2315. return false;
  2316. blk_mq_set_rq_budget_token(rq, budget_token);
  2317. if (!blk_mq_get_driver_tag(rq)) {
  2318. blk_mq_put_dispatch_budget(rq->q, budget_token);
  2319. return false;
  2320. }
  2321. return true;
  2322. }
  2323. /**
  2324. * blk_mq_try_issue_directly - Try to send a request directly to device driver.
  2325. * @hctx: Pointer of the associated hardware queue.
  2326. * @rq: Pointer to request to be sent.
  2327. *
  2328. * If the device has enough resources to accept a new request now, send the
  2329. * request directly to device driver. Else, insert at hctx->dispatch queue, so
  2330. * we can try send it another time in the future. Requests inserted at this
  2331. * queue have higher priority.
  2332. */
  2333. static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
  2334. struct request *rq)
  2335. {
  2336. blk_status_t ret;
  2337. if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(rq->q)) {
  2338. blk_mq_insert_request(rq, 0);
  2339. blk_mq_run_hw_queue(hctx, false);
  2340. return;
  2341. }
  2342. if ((rq->rq_flags & RQF_USE_SCHED) || !blk_mq_get_budget_and_tag(rq)) {
  2343. blk_mq_insert_request(rq, 0);
  2344. blk_mq_run_hw_queue(hctx, rq->cmd_flags & REQ_NOWAIT);
  2345. return;
  2346. }
  2347. ret = __blk_mq_issue_directly(hctx, rq, true);
  2348. switch (ret) {
  2349. case BLK_STS_OK:
  2350. break;
  2351. case BLK_STS_RESOURCE:
  2352. case BLK_STS_DEV_RESOURCE:
  2353. blk_mq_request_bypass_insert(rq, 0);
  2354. blk_mq_run_hw_queue(hctx, false);
  2355. break;
  2356. default:
  2357. blk_mq_end_request(rq, ret);
  2358. break;
  2359. }
  2360. }
  2361. static blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
  2362. {
  2363. struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
  2364. if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(rq->q)) {
  2365. blk_mq_insert_request(rq, 0);
  2366. blk_mq_run_hw_queue(hctx, false);
  2367. return BLK_STS_OK;
  2368. }
  2369. if (!blk_mq_get_budget_and_tag(rq))
  2370. return BLK_STS_RESOURCE;
  2371. return __blk_mq_issue_directly(hctx, rq, last);
  2372. }
  2373. static void blk_mq_plug_issue_direct(struct blk_plug *plug)
  2374. {
  2375. struct blk_mq_hw_ctx *hctx = NULL;
  2376. struct request *rq;
  2377. int queued = 0;
  2378. blk_status_t ret = BLK_STS_OK;
  2379. while ((rq = rq_list_pop(&plug->mq_list))) {
  2380. bool last = rq_list_empty(plug->mq_list);
  2381. if (hctx != rq->mq_hctx) {
  2382. if (hctx) {
  2383. blk_mq_commit_rqs(hctx, queued, false);
  2384. queued = 0;
  2385. }
  2386. hctx = rq->mq_hctx;
  2387. }
  2388. ret = blk_mq_request_issue_directly(rq, last);
  2389. switch (ret) {
  2390. case BLK_STS_OK:
  2391. queued++;
  2392. break;
  2393. case BLK_STS_RESOURCE:
  2394. case BLK_STS_DEV_RESOURCE:
  2395. blk_mq_request_bypass_insert(rq, 0);
  2396. blk_mq_run_hw_queue(hctx, false);
  2397. goto out;
  2398. default:
  2399. blk_mq_end_request(rq, ret);
  2400. break;
  2401. }
  2402. }
  2403. out:
  2404. if (ret != BLK_STS_OK)
  2405. blk_mq_commit_rqs(hctx, queued, false);
  2406. }
  2407. static void __blk_mq_flush_plug_list(struct request_queue *q,
  2408. struct blk_plug *plug)
  2409. {
  2410. if (blk_queue_quiesced(q))
  2411. return;
  2412. q->mq_ops->queue_rqs(&plug->mq_list);
  2413. }
  2414. static void blk_mq_dispatch_plug_list(struct blk_plug *plug, bool from_sched)
  2415. {
  2416. struct blk_mq_hw_ctx *this_hctx = NULL;
  2417. struct blk_mq_ctx *this_ctx = NULL;
  2418. struct request *requeue_list = NULL;
  2419. struct request **requeue_lastp = &requeue_list;
  2420. unsigned int depth = 0;
  2421. bool is_passthrough = false;
  2422. LIST_HEAD(list);
  2423. do {
  2424. struct request *rq = rq_list_pop(&plug->mq_list);
  2425. if (!this_hctx) {
  2426. this_hctx = rq->mq_hctx;
  2427. this_ctx = rq->mq_ctx;
  2428. is_passthrough = blk_rq_is_passthrough(rq);
  2429. } else if (this_hctx != rq->mq_hctx || this_ctx != rq->mq_ctx ||
  2430. is_passthrough != blk_rq_is_passthrough(rq)) {
  2431. rq_list_add_tail(&requeue_lastp, rq);
  2432. continue;
  2433. }
  2434. list_add(&rq->queuelist, &list);
  2435. depth++;
  2436. } while (!rq_list_empty(plug->mq_list));
  2437. plug->mq_list = requeue_list;
  2438. trace_block_unplug(this_hctx->queue, depth, !from_sched);
  2439. percpu_ref_get(&this_hctx->queue->q_usage_counter);
  2440. /* passthrough requests should never be issued to the I/O scheduler */
  2441. if (is_passthrough) {
  2442. spin_lock(&this_hctx->lock);
  2443. list_splice_tail_init(&list, &this_hctx->dispatch);
  2444. spin_unlock(&this_hctx->lock);
  2445. blk_mq_run_hw_queue(this_hctx, from_sched);
  2446. } else if (this_hctx->queue->elevator) {
  2447. this_hctx->queue->elevator->type->ops.insert_requests(this_hctx,
  2448. &list, 0);
  2449. blk_mq_run_hw_queue(this_hctx, from_sched);
  2450. } else {
  2451. blk_mq_insert_requests(this_hctx, this_ctx, &list, from_sched);
  2452. }
  2453. percpu_ref_put(&this_hctx->queue->q_usage_counter);
  2454. }
  2455. void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  2456. {
  2457. struct request *rq;
  2458. unsigned int depth;
  2459. /*
  2460. * We may have been called recursively midway through handling
  2461. * plug->mq_list via a schedule() in the driver's queue_rq() callback.
  2462. * To avoid mq_list changing under our feet, clear rq_count early and
  2463. * bail out specifically if rq_count is 0 rather than checking
  2464. * whether the mq_list is empty.
  2465. */
  2466. if (plug->rq_count == 0)
  2467. return;
  2468. depth = plug->rq_count;
  2469. plug->rq_count = 0;
  2470. if (!plug->multiple_queues && !plug->has_elevator && !from_schedule) {
  2471. struct request_queue *q;
  2472. rq = rq_list_peek(&plug->mq_list);
  2473. q = rq->q;
  2474. trace_block_unplug(q, depth, true);
  2475. /*
  2476. * Peek first request and see if we have a ->queue_rqs() hook.
  2477. * If we do, we can dispatch the whole plug list in one go. We
  2478. * already know at this point that all requests belong to the
  2479. * same queue, caller must ensure that's the case.
  2480. */
  2481. if (q->mq_ops->queue_rqs) {
  2482. blk_mq_run_dispatch_ops(q,
  2483. __blk_mq_flush_plug_list(q, plug));
  2484. if (rq_list_empty(plug->mq_list))
  2485. return;
  2486. }
  2487. blk_mq_run_dispatch_ops(q,
  2488. blk_mq_plug_issue_direct(plug));
  2489. if (rq_list_empty(plug->mq_list))
  2490. return;
  2491. }
  2492. do {
  2493. blk_mq_dispatch_plug_list(plug, from_schedule);
  2494. } while (!rq_list_empty(plug->mq_list));
  2495. }
  2496. static void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
  2497. struct list_head *list)
  2498. {
  2499. int queued = 0;
  2500. blk_status_t ret = BLK_STS_OK;
  2501. while (!list_empty(list)) {
  2502. struct request *rq = list_first_entry(list, struct request,
  2503. queuelist);
  2504. list_del_init(&rq->queuelist);
  2505. ret = blk_mq_request_issue_directly(rq, list_empty(list));
  2506. switch (ret) {
  2507. case BLK_STS_OK:
  2508. queued++;
  2509. break;
  2510. case BLK_STS_RESOURCE:
  2511. case BLK_STS_DEV_RESOURCE:
  2512. blk_mq_request_bypass_insert(rq, 0);
  2513. if (list_empty(list))
  2514. blk_mq_run_hw_queue(hctx, false);
  2515. goto out;
  2516. default:
  2517. blk_mq_end_request(rq, ret);
  2518. break;
  2519. }
  2520. }
  2521. out:
  2522. if (ret != BLK_STS_OK)
  2523. blk_mq_commit_rqs(hctx, queued, false);
  2524. }
  2525. static bool blk_mq_attempt_bio_merge(struct request_queue *q,
  2526. struct bio *bio, unsigned int nr_segs)
  2527. {
  2528. if (!blk_queue_nomerges(q) && bio_mergeable(bio)) {
  2529. if (blk_attempt_plug_merge(q, bio, nr_segs))
  2530. return true;
  2531. if (blk_mq_sched_bio_merge(q, bio, nr_segs))
  2532. return true;
  2533. }
  2534. return false;
  2535. }
  2536. static struct request *blk_mq_get_new_requests(struct request_queue *q,
  2537. struct blk_plug *plug,
  2538. struct bio *bio,
  2539. unsigned int nsegs)
  2540. {
  2541. struct blk_mq_alloc_data data = {
  2542. .q = q,
  2543. .nr_tags = 1,
  2544. .cmd_flags = bio->bi_opf,
  2545. };
  2546. struct request *rq;
  2547. rq_qos_throttle(q, bio);
  2548. if (plug) {
  2549. data.nr_tags = plug->nr_ios;
  2550. plug->nr_ios = 1;
  2551. data.cached_rq = &plug->cached_rq;
  2552. }
  2553. rq = __blk_mq_alloc_requests(&data);
  2554. if (rq)
  2555. return rq;
  2556. rq_qos_cleanup(q, bio);
  2557. if (bio->bi_opf & REQ_NOWAIT)
  2558. bio_wouldblock_error(bio);
  2559. return NULL;
  2560. }
  2561. /*
  2562. * Check if there is a suitable cached request and return it.
  2563. */
  2564. static struct request *blk_mq_peek_cached_request(struct blk_plug *plug,
  2565. struct request_queue *q, blk_opf_t opf)
  2566. {
  2567. enum hctx_type type = blk_mq_get_hctx_type(opf);
  2568. struct request *rq;
  2569. if (!plug)
  2570. return NULL;
  2571. rq = rq_list_peek(&plug->cached_rq);
  2572. if (!rq || rq->q != q)
  2573. return NULL;
  2574. if (type != rq->mq_hctx->type &&
  2575. (type != HCTX_TYPE_READ || rq->mq_hctx->type != HCTX_TYPE_DEFAULT))
  2576. return NULL;
  2577. if (op_is_flush(rq->cmd_flags) != op_is_flush(opf))
  2578. return NULL;
  2579. return rq;
  2580. }
  2581. static void blk_mq_use_cached_rq(struct request *rq, struct blk_plug *plug,
  2582. struct bio *bio)
  2583. {
  2584. WARN_ON_ONCE(rq_list_peek(&plug->cached_rq) != rq);
  2585. /*
  2586. * If any qos ->throttle() end up blocking, we will have flushed the
  2587. * plug and hence killed the cached_rq list as well. Pop this entry
  2588. * before we throttle.
  2589. */
  2590. plug->cached_rq = rq_list_next(rq);
  2591. rq_qos_throttle(rq->q, bio);
  2592. blk_mq_rq_time_init(rq, 0);
  2593. rq->cmd_flags = bio->bi_opf;
  2594. INIT_LIST_HEAD(&rq->queuelist);
  2595. }
  2596. static bool bio_unaligned(const struct bio *bio, struct request_queue *q)
  2597. {
  2598. unsigned int bs_mask = queue_logical_block_size(q) - 1;
  2599. /* .bi_sector of any zero sized bio need to be initialized */
  2600. if ((bio->bi_iter.bi_size & bs_mask) ||
  2601. ((bio->bi_iter.bi_sector << SECTOR_SHIFT) & bs_mask))
  2602. return true;
  2603. return false;
  2604. }
  2605. /**
  2606. * blk_mq_submit_bio - Create and send a request to block device.
  2607. * @bio: Bio pointer.
  2608. *
  2609. * Builds up a request structure from @q and @bio and send to the device. The
  2610. * request may not be queued directly to hardware if:
  2611. * * This request can be merged with another one
  2612. * * We want to place request at plug queue for possible future merging
  2613. * * There is an IO scheduler active at this queue
  2614. *
  2615. * It will not queue the request if there is an error with the bio, or at the
  2616. * request creation.
  2617. */
  2618. void blk_mq_submit_bio(struct bio *bio)
  2619. {
  2620. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  2621. struct blk_plug *plug = current->plug;
  2622. const int is_sync = op_is_sync(bio->bi_opf);
  2623. struct blk_mq_hw_ctx *hctx;
  2624. unsigned int nr_segs;
  2625. struct request *rq;
  2626. blk_status_t ret;
  2627. /*
  2628. * If the plug has a cached request for this queue, try to use it.
  2629. */
  2630. rq = blk_mq_peek_cached_request(plug, q, bio->bi_opf);
  2631. /*
  2632. * A BIO that was released from a zone write plug has already been
  2633. * through the preparation in this function, already holds a reference
  2634. * on the queue usage counter, and is the only write BIO in-flight for
  2635. * the target zone. Go straight to preparing a request for it.
  2636. */
  2637. if (bio_zone_write_plugging(bio)) {
  2638. nr_segs = bio->__bi_nr_segments;
  2639. if (rq)
  2640. blk_queue_exit(q);
  2641. goto new_request;
  2642. }
  2643. bio = blk_queue_bounce(bio, q);
  2644. /*
  2645. * The cached request already holds a q_usage_counter reference and we
  2646. * don't have to acquire a new one if we use it.
  2647. */
  2648. if (!rq) {
  2649. if (unlikely(bio_queue_enter(bio)))
  2650. return;
  2651. }
  2652. /*
  2653. * Device reconfiguration may change logical block size or reduce the
  2654. * number of poll queues, so the checks for alignment and poll support
  2655. * have to be done with queue usage counter held.
  2656. */
  2657. if (unlikely(bio_unaligned(bio, q))) {
  2658. bio_io_error(bio);
  2659. goto queue_exit;
  2660. }
  2661. if ((bio->bi_opf & REQ_POLLED) && !blk_mq_can_poll(q)) {
  2662. bio->bi_status = BLK_STS_NOTSUPP;
  2663. bio_endio(bio);
  2664. goto queue_exit;
  2665. }
  2666. bio = __bio_split_to_limits(bio, &q->limits, &nr_segs);
  2667. if (!bio)
  2668. goto queue_exit;
  2669. if (!bio_integrity_prep(bio))
  2670. goto queue_exit;
  2671. if (blk_mq_attempt_bio_merge(q, bio, nr_segs))
  2672. goto queue_exit;
  2673. if (blk_queue_is_zoned(q) && blk_zone_plug_bio(bio, nr_segs))
  2674. goto queue_exit;
  2675. new_request:
  2676. if (!rq) {
  2677. rq = blk_mq_get_new_requests(q, plug, bio, nr_segs);
  2678. if (unlikely(!rq))
  2679. goto queue_exit;
  2680. } else {
  2681. blk_mq_use_cached_rq(rq, plug, bio);
  2682. }
  2683. trace_block_getrq(bio);
  2684. rq_qos_track(q, rq, bio);
  2685. blk_mq_bio_to_request(rq, bio, nr_segs);
  2686. ret = blk_crypto_rq_get_keyslot(rq);
  2687. if (ret != BLK_STS_OK) {
  2688. bio->bi_status = ret;
  2689. bio_endio(bio);
  2690. blk_mq_free_request(rq);
  2691. return;
  2692. }
  2693. if (bio_zone_write_plugging(bio))
  2694. blk_zone_write_plug_init_request(rq);
  2695. if (op_is_flush(bio->bi_opf) && blk_insert_flush(rq))
  2696. return;
  2697. if (plug) {
  2698. blk_add_rq_to_plug(plug, rq);
  2699. return;
  2700. }
  2701. hctx = rq->mq_hctx;
  2702. if ((rq->rq_flags & RQF_USE_SCHED) ||
  2703. (hctx->dispatch_busy && (q->nr_hw_queues == 1 || !is_sync))) {
  2704. blk_mq_insert_request(rq, 0);
  2705. blk_mq_run_hw_queue(hctx, true);
  2706. } else {
  2707. blk_mq_run_dispatch_ops(q, blk_mq_try_issue_directly(hctx, rq));
  2708. }
  2709. return;
  2710. queue_exit:
  2711. /*
  2712. * Don't drop the queue reference if we were trying to use a cached
  2713. * request and thus didn't acquire one.
  2714. */
  2715. if (!rq)
  2716. blk_queue_exit(q);
  2717. }
  2718. #ifdef CONFIG_BLK_MQ_STACKING
  2719. /**
  2720. * blk_insert_cloned_request - Helper for stacking drivers to submit a request
  2721. * @rq: the request being queued
  2722. */
  2723. blk_status_t blk_insert_cloned_request(struct request *rq)
  2724. {
  2725. struct request_queue *q = rq->q;
  2726. unsigned int max_sectors = blk_queue_get_max_sectors(rq);
  2727. unsigned int max_segments = blk_rq_get_max_segments(rq);
  2728. blk_status_t ret;
  2729. if (blk_rq_sectors(rq) > max_sectors) {
  2730. /*
  2731. * SCSI device does not have a good way to return if
  2732. * Write Same/Zero is actually supported. If a device rejects
  2733. * a non-read/write command (discard, write same,etc.) the
  2734. * low-level device driver will set the relevant queue limit to
  2735. * 0 to prevent blk-lib from issuing more of the offending
  2736. * operations. Commands queued prior to the queue limit being
  2737. * reset need to be completed with BLK_STS_NOTSUPP to avoid I/O
  2738. * errors being propagated to upper layers.
  2739. */
  2740. if (max_sectors == 0)
  2741. return BLK_STS_NOTSUPP;
  2742. printk(KERN_ERR "%s: over max size limit. (%u > %u)\n",
  2743. __func__, blk_rq_sectors(rq), max_sectors);
  2744. return BLK_STS_IOERR;
  2745. }
  2746. /*
  2747. * The queue settings related to segment counting may differ from the
  2748. * original queue.
  2749. */
  2750. rq->nr_phys_segments = blk_recalc_rq_segments(rq);
  2751. if (rq->nr_phys_segments > max_segments) {
  2752. printk(KERN_ERR "%s: over max segments limit. (%u > %u)\n",
  2753. __func__, rq->nr_phys_segments, max_segments);
  2754. return BLK_STS_IOERR;
  2755. }
  2756. if (q->disk && should_fail_request(q->disk->part0, blk_rq_bytes(rq)))
  2757. return BLK_STS_IOERR;
  2758. ret = blk_crypto_rq_get_keyslot(rq);
  2759. if (ret != BLK_STS_OK)
  2760. return ret;
  2761. blk_account_io_start(rq);
  2762. /*
  2763. * Since we have a scheduler attached on the top device,
  2764. * bypass a potential scheduler on the bottom device for
  2765. * insert.
  2766. */
  2767. blk_mq_run_dispatch_ops(q,
  2768. ret = blk_mq_request_issue_directly(rq, true));
  2769. if (ret)
  2770. blk_account_io_done(rq, blk_time_get_ns());
  2771. return ret;
  2772. }
  2773. EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
  2774. /**
  2775. * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
  2776. * @rq: the clone request to be cleaned up
  2777. *
  2778. * Description:
  2779. * Free all bios in @rq for a cloned request.
  2780. */
  2781. void blk_rq_unprep_clone(struct request *rq)
  2782. {
  2783. struct bio *bio;
  2784. while ((bio = rq->bio) != NULL) {
  2785. rq->bio = bio->bi_next;
  2786. bio_put(bio);
  2787. }
  2788. }
  2789. EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
  2790. /**
  2791. * blk_rq_prep_clone - Helper function to setup clone request
  2792. * @rq: the request to be setup
  2793. * @rq_src: original request to be cloned
  2794. * @bs: bio_set that bios for clone are allocated from
  2795. * @gfp_mask: memory allocation mask for bio
  2796. * @bio_ctr: setup function to be called for each clone bio.
  2797. * Returns %0 for success, non %0 for failure.
  2798. * @data: private data to be passed to @bio_ctr
  2799. *
  2800. * Description:
  2801. * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
  2802. * Also, pages which the original bios are pointing to are not copied
  2803. * and the cloned bios just point same pages.
  2804. * So cloned bios must be completed before original bios, which means
  2805. * the caller must complete @rq before @rq_src.
  2806. */
  2807. int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
  2808. struct bio_set *bs, gfp_t gfp_mask,
  2809. int (*bio_ctr)(struct bio *, struct bio *, void *),
  2810. void *data)
  2811. {
  2812. struct bio *bio, *bio_src;
  2813. if (!bs)
  2814. bs = &fs_bio_set;
  2815. __rq_for_each_bio(bio_src, rq_src) {
  2816. bio = bio_alloc_clone(rq->q->disk->part0, bio_src, gfp_mask,
  2817. bs);
  2818. if (!bio)
  2819. goto free_and_out;
  2820. if (bio_ctr && bio_ctr(bio, bio_src, data))
  2821. goto free_and_out;
  2822. if (rq->bio) {
  2823. rq->biotail->bi_next = bio;
  2824. rq->biotail = bio;
  2825. } else {
  2826. rq->bio = rq->biotail = bio;
  2827. }
  2828. bio = NULL;
  2829. }
  2830. /* Copy attributes of the original request to the clone request. */
  2831. rq->__sector = blk_rq_pos(rq_src);
  2832. rq->__data_len = blk_rq_bytes(rq_src);
  2833. if (rq_src->rq_flags & RQF_SPECIAL_PAYLOAD) {
  2834. rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
  2835. rq->special_vec = rq_src->special_vec;
  2836. }
  2837. rq->nr_phys_segments = rq_src->nr_phys_segments;
  2838. rq->ioprio = rq_src->ioprio;
  2839. rq->write_hint = rq_src->write_hint;
  2840. if (rq->bio && blk_crypto_rq_bio_prep(rq, rq->bio, gfp_mask) < 0)
  2841. goto free_and_out;
  2842. return 0;
  2843. free_and_out:
  2844. if (bio)
  2845. bio_put(bio);
  2846. blk_rq_unprep_clone(rq);
  2847. return -ENOMEM;
  2848. }
  2849. EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
  2850. #endif /* CONFIG_BLK_MQ_STACKING */
  2851. /*
  2852. * Steal bios from a request and add them to a bio list.
  2853. * The request must not have been partially completed before.
  2854. */
  2855. void blk_steal_bios(struct bio_list *list, struct request *rq)
  2856. {
  2857. if (rq->bio) {
  2858. if (list->tail)
  2859. list->tail->bi_next = rq->bio;
  2860. else
  2861. list->head = rq->bio;
  2862. list->tail = rq->biotail;
  2863. rq->bio = NULL;
  2864. rq->biotail = NULL;
  2865. }
  2866. rq->__data_len = 0;
  2867. }
  2868. EXPORT_SYMBOL_GPL(blk_steal_bios);
  2869. static size_t order_to_size(unsigned int order)
  2870. {
  2871. return (size_t)PAGE_SIZE << order;
  2872. }
  2873. /* called before freeing request pool in @tags */
  2874. static void blk_mq_clear_rq_mapping(struct blk_mq_tags *drv_tags,
  2875. struct blk_mq_tags *tags)
  2876. {
  2877. struct page *page;
  2878. unsigned long flags;
  2879. /*
  2880. * There is no need to clear mapping if driver tags is not initialized
  2881. * or the mapping belongs to the driver tags.
  2882. */
  2883. if (!drv_tags || drv_tags == tags)
  2884. return;
  2885. list_for_each_entry(page, &tags->page_list, lru) {
  2886. unsigned long start = (unsigned long)page_address(page);
  2887. unsigned long end = start + order_to_size(page->private);
  2888. int i;
  2889. for (i = 0; i < drv_tags->nr_tags; i++) {
  2890. struct request *rq = drv_tags->rqs[i];
  2891. unsigned long rq_addr = (unsigned long)rq;
  2892. if (rq_addr >= start && rq_addr < end) {
  2893. WARN_ON_ONCE(req_ref_read(rq) != 0);
  2894. cmpxchg(&drv_tags->rqs[i], rq, NULL);
  2895. }
  2896. }
  2897. }
  2898. /*
  2899. * Wait until all pending iteration is done.
  2900. *
  2901. * Request reference is cleared and it is guaranteed to be observed
  2902. * after the ->lock is released.
  2903. */
  2904. spin_lock_irqsave(&drv_tags->lock, flags);
  2905. spin_unlock_irqrestore(&drv_tags->lock, flags);
  2906. }
  2907. void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
  2908. unsigned int hctx_idx)
  2909. {
  2910. struct blk_mq_tags *drv_tags;
  2911. struct page *page;
  2912. if (list_empty(&tags->page_list))
  2913. return;
  2914. if (blk_mq_is_shared_tags(set->flags))
  2915. drv_tags = set->shared_tags;
  2916. else
  2917. drv_tags = set->tags[hctx_idx];
  2918. if (tags->static_rqs && set->ops->exit_request) {
  2919. int i;
  2920. for (i = 0; i < tags->nr_tags; i++) {
  2921. struct request *rq = tags->static_rqs[i];
  2922. if (!rq)
  2923. continue;
  2924. set->ops->exit_request(set, rq, hctx_idx);
  2925. tags->static_rqs[i] = NULL;
  2926. }
  2927. }
  2928. blk_mq_clear_rq_mapping(drv_tags, tags);
  2929. while (!list_empty(&tags->page_list)) {
  2930. page = list_first_entry(&tags->page_list, struct page, lru);
  2931. list_del_init(&page->lru);
  2932. /*
  2933. * Remove kmemleak object previously allocated in
  2934. * blk_mq_alloc_rqs().
  2935. */
  2936. kmemleak_free(page_address(page));
  2937. __free_pages(page, page->private);
  2938. }
  2939. }
  2940. void blk_mq_free_rq_map(struct blk_mq_tags *tags)
  2941. {
  2942. kfree(tags->rqs);
  2943. tags->rqs = NULL;
  2944. kfree(tags->static_rqs);
  2945. tags->static_rqs = NULL;
  2946. blk_mq_free_tags(tags);
  2947. }
  2948. static enum hctx_type hctx_idx_to_type(struct blk_mq_tag_set *set,
  2949. unsigned int hctx_idx)
  2950. {
  2951. int i;
  2952. for (i = 0; i < set->nr_maps; i++) {
  2953. unsigned int start = set->map[i].queue_offset;
  2954. unsigned int end = start + set->map[i].nr_queues;
  2955. if (hctx_idx >= start && hctx_idx < end)
  2956. break;
  2957. }
  2958. if (i >= set->nr_maps)
  2959. i = HCTX_TYPE_DEFAULT;
  2960. return i;
  2961. }
  2962. static int blk_mq_get_hctx_node(struct blk_mq_tag_set *set,
  2963. unsigned int hctx_idx)
  2964. {
  2965. enum hctx_type type = hctx_idx_to_type(set, hctx_idx);
  2966. return blk_mq_hw_queue_to_node(&set->map[type], hctx_idx);
  2967. }
  2968. static struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
  2969. unsigned int hctx_idx,
  2970. unsigned int nr_tags,
  2971. unsigned int reserved_tags)
  2972. {
  2973. int node = blk_mq_get_hctx_node(set, hctx_idx);
  2974. struct blk_mq_tags *tags;
  2975. if (node == NUMA_NO_NODE)
  2976. node = set->numa_node;
  2977. tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
  2978. BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
  2979. if (!tags)
  2980. return NULL;
  2981. tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
  2982. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
  2983. node);
  2984. if (!tags->rqs)
  2985. goto err_free_tags;
  2986. tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
  2987. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
  2988. node);
  2989. if (!tags->static_rqs)
  2990. goto err_free_rqs;
  2991. return tags;
  2992. err_free_rqs:
  2993. kfree(tags->rqs);
  2994. err_free_tags:
  2995. blk_mq_free_tags(tags);
  2996. return NULL;
  2997. }
  2998. static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
  2999. unsigned int hctx_idx, int node)
  3000. {
  3001. int ret;
  3002. if (set->ops->init_request) {
  3003. ret = set->ops->init_request(set, rq, hctx_idx, node);
  3004. if (ret)
  3005. return ret;
  3006. }
  3007. WRITE_ONCE(rq->state, MQ_RQ_IDLE);
  3008. return 0;
  3009. }
  3010. static int blk_mq_alloc_rqs(struct blk_mq_tag_set *set,
  3011. struct blk_mq_tags *tags,
  3012. unsigned int hctx_idx, unsigned int depth)
  3013. {
  3014. unsigned int i, j, entries_per_page, max_order = 4;
  3015. int node = blk_mq_get_hctx_node(set, hctx_idx);
  3016. size_t rq_size, left;
  3017. if (node == NUMA_NO_NODE)
  3018. node = set->numa_node;
  3019. INIT_LIST_HEAD(&tags->page_list);
  3020. /*
  3021. * rq_size is the size of the request plus driver payload, rounded
  3022. * to the cacheline size
  3023. */
  3024. rq_size = round_up(sizeof(struct request) + set->cmd_size,
  3025. cache_line_size());
  3026. left = rq_size * depth;
  3027. for (i = 0; i < depth; ) {
  3028. int this_order = max_order;
  3029. struct page *page;
  3030. int to_do;
  3031. void *p;
  3032. while (this_order && left < order_to_size(this_order - 1))
  3033. this_order--;
  3034. do {
  3035. page = alloc_pages_node(node,
  3036. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
  3037. this_order);
  3038. if (page)
  3039. break;
  3040. if (!this_order--)
  3041. break;
  3042. if (order_to_size(this_order) < rq_size)
  3043. break;
  3044. } while (1);
  3045. if (!page)
  3046. goto fail;
  3047. page->private = this_order;
  3048. list_add_tail(&page->lru, &tags->page_list);
  3049. p = page_address(page);
  3050. /*
  3051. * Allow kmemleak to scan these pages as they contain pointers
  3052. * to additional allocations like via ops->init_request().
  3053. */
  3054. kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
  3055. entries_per_page = order_to_size(this_order) / rq_size;
  3056. to_do = min(entries_per_page, depth - i);
  3057. left -= to_do * rq_size;
  3058. for (j = 0; j < to_do; j++) {
  3059. struct request *rq = p;
  3060. tags->static_rqs[i] = rq;
  3061. if (blk_mq_init_request(set, rq, hctx_idx, node)) {
  3062. tags->static_rqs[i] = NULL;
  3063. goto fail;
  3064. }
  3065. p += rq_size;
  3066. i++;
  3067. }
  3068. }
  3069. return 0;
  3070. fail:
  3071. blk_mq_free_rqs(set, tags, hctx_idx);
  3072. return -ENOMEM;
  3073. }
  3074. struct rq_iter_data {
  3075. struct blk_mq_hw_ctx *hctx;
  3076. bool has_rq;
  3077. };
  3078. static bool blk_mq_has_request(struct request *rq, void *data)
  3079. {
  3080. struct rq_iter_data *iter_data = data;
  3081. if (rq->mq_hctx != iter_data->hctx)
  3082. return true;
  3083. iter_data->has_rq = true;
  3084. return false;
  3085. }
  3086. static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
  3087. {
  3088. struct blk_mq_tags *tags = hctx->sched_tags ?
  3089. hctx->sched_tags : hctx->tags;
  3090. struct rq_iter_data data = {
  3091. .hctx = hctx,
  3092. };
  3093. blk_mq_all_tag_iter(tags, blk_mq_has_request, &data);
  3094. return data.has_rq;
  3095. }
  3096. static bool blk_mq_hctx_has_online_cpu(struct blk_mq_hw_ctx *hctx,
  3097. unsigned int this_cpu)
  3098. {
  3099. enum hctx_type type = hctx->type;
  3100. int cpu;
  3101. /*
  3102. * hctx->cpumask has to rule out isolated CPUs, but userspace still
  3103. * might submit IOs on these isolated CPUs, so use the queue map to
  3104. * check if all CPUs mapped to this hctx are offline
  3105. */
  3106. for_each_online_cpu(cpu) {
  3107. struct blk_mq_hw_ctx *h = blk_mq_map_queue_type(hctx->queue,
  3108. type, cpu);
  3109. if (h != hctx)
  3110. continue;
  3111. /* this hctx has at least one online CPU */
  3112. if (this_cpu != cpu)
  3113. return true;
  3114. }
  3115. return false;
  3116. }
  3117. static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
  3118. {
  3119. struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
  3120. struct blk_mq_hw_ctx, cpuhp_online);
  3121. if (blk_mq_hctx_has_online_cpu(hctx, cpu))
  3122. return 0;
  3123. /*
  3124. * Prevent new request from being allocated on the current hctx.
  3125. *
  3126. * The smp_mb__after_atomic() Pairs with the implied barrier in
  3127. * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is
  3128. * seen once we return from the tag allocator.
  3129. */
  3130. set_bit(BLK_MQ_S_INACTIVE, &hctx->state);
  3131. smp_mb__after_atomic();
  3132. /*
  3133. * Try to grab a reference to the queue and wait for any outstanding
  3134. * requests. If we could not grab a reference the queue has been
  3135. * frozen and there are no requests.
  3136. */
  3137. if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) {
  3138. while (blk_mq_hctx_has_requests(hctx))
  3139. msleep(5);
  3140. percpu_ref_put(&hctx->queue->q_usage_counter);
  3141. }
  3142. return 0;
  3143. }
  3144. /*
  3145. * Check if one CPU is mapped to the specified hctx
  3146. *
  3147. * Isolated CPUs have been ruled out from hctx->cpumask, which is supposed
  3148. * to be used for scheduling kworker only. For other usage, please call this
  3149. * helper for checking if one CPU belongs to the specified hctx
  3150. */
  3151. static bool blk_mq_cpu_mapped_to_hctx(unsigned int cpu,
  3152. const struct blk_mq_hw_ctx *hctx)
  3153. {
  3154. struct blk_mq_hw_ctx *mapped_hctx = blk_mq_map_queue_type(hctx->queue,
  3155. hctx->type, cpu);
  3156. return mapped_hctx == hctx;
  3157. }
  3158. static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node)
  3159. {
  3160. struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
  3161. struct blk_mq_hw_ctx, cpuhp_online);
  3162. if (blk_mq_cpu_mapped_to_hctx(cpu, hctx))
  3163. clear_bit(BLK_MQ_S_INACTIVE, &hctx->state);
  3164. return 0;
  3165. }
  3166. /*
  3167. * 'cpu' is going away. splice any existing rq_list entries from this
  3168. * software queue to the hw queue dispatch list, and ensure that it
  3169. * gets run.
  3170. */
  3171. static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
  3172. {
  3173. struct blk_mq_hw_ctx *hctx;
  3174. struct blk_mq_ctx *ctx;
  3175. LIST_HEAD(tmp);
  3176. enum hctx_type type;
  3177. hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
  3178. if (!blk_mq_cpu_mapped_to_hctx(cpu, hctx))
  3179. return 0;
  3180. ctx = __blk_mq_get_ctx(hctx->queue, cpu);
  3181. type = hctx->type;
  3182. spin_lock(&ctx->lock);
  3183. if (!list_empty(&ctx->rq_lists[type])) {
  3184. list_splice_init(&ctx->rq_lists[type], &tmp);
  3185. blk_mq_hctx_clear_pending(hctx, ctx);
  3186. }
  3187. spin_unlock(&ctx->lock);
  3188. if (list_empty(&tmp))
  3189. return 0;
  3190. spin_lock(&hctx->lock);
  3191. list_splice_tail_init(&tmp, &hctx->dispatch);
  3192. spin_unlock(&hctx->lock);
  3193. blk_mq_run_hw_queue(hctx, true);
  3194. return 0;
  3195. }
  3196. static void __blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
  3197. {
  3198. lockdep_assert_held(&blk_mq_cpuhp_lock);
  3199. if (!(hctx->flags & BLK_MQ_F_STACKING) &&
  3200. !hlist_unhashed(&hctx->cpuhp_online)) {
  3201. cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
  3202. &hctx->cpuhp_online);
  3203. INIT_HLIST_NODE(&hctx->cpuhp_online);
  3204. }
  3205. if (!hlist_unhashed(&hctx->cpuhp_dead)) {
  3206. cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
  3207. &hctx->cpuhp_dead);
  3208. INIT_HLIST_NODE(&hctx->cpuhp_dead);
  3209. }
  3210. }
  3211. static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
  3212. {
  3213. mutex_lock(&blk_mq_cpuhp_lock);
  3214. __blk_mq_remove_cpuhp(hctx);
  3215. mutex_unlock(&blk_mq_cpuhp_lock);
  3216. }
  3217. static void __blk_mq_add_cpuhp(struct blk_mq_hw_ctx *hctx)
  3218. {
  3219. lockdep_assert_held(&blk_mq_cpuhp_lock);
  3220. if (!(hctx->flags & BLK_MQ_F_STACKING) &&
  3221. hlist_unhashed(&hctx->cpuhp_online))
  3222. cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
  3223. &hctx->cpuhp_online);
  3224. if (hlist_unhashed(&hctx->cpuhp_dead))
  3225. cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD,
  3226. &hctx->cpuhp_dead);
  3227. }
  3228. static void __blk_mq_remove_cpuhp_list(struct list_head *head)
  3229. {
  3230. struct blk_mq_hw_ctx *hctx;
  3231. lockdep_assert_held(&blk_mq_cpuhp_lock);
  3232. list_for_each_entry(hctx, head, hctx_list)
  3233. __blk_mq_remove_cpuhp(hctx);
  3234. }
  3235. /*
  3236. * Unregister cpuhp callbacks from exited hw queues
  3237. *
  3238. * Safe to call if this `request_queue` is live
  3239. */
  3240. static void blk_mq_remove_hw_queues_cpuhp(struct request_queue *q)
  3241. {
  3242. LIST_HEAD(hctx_list);
  3243. spin_lock(&q->unused_hctx_lock);
  3244. list_splice_init(&q->unused_hctx_list, &hctx_list);
  3245. spin_unlock(&q->unused_hctx_lock);
  3246. mutex_lock(&blk_mq_cpuhp_lock);
  3247. __blk_mq_remove_cpuhp_list(&hctx_list);
  3248. mutex_unlock(&blk_mq_cpuhp_lock);
  3249. spin_lock(&q->unused_hctx_lock);
  3250. list_splice(&hctx_list, &q->unused_hctx_list);
  3251. spin_unlock(&q->unused_hctx_lock);
  3252. }
  3253. /*
  3254. * Register cpuhp callbacks from all hw queues
  3255. *
  3256. * Safe to call if this `request_queue` is live
  3257. */
  3258. static void blk_mq_add_hw_queues_cpuhp(struct request_queue *q)
  3259. {
  3260. struct blk_mq_hw_ctx *hctx;
  3261. unsigned long i;
  3262. mutex_lock(&blk_mq_cpuhp_lock);
  3263. queue_for_each_hw_ctx(q, hctx, i)
  3264. __blk_mq_add_cpuhp(hctx);
  3265. mutex_unlock(&blk_mq_cpuhp_lock);
  3266. }
  3267. /*
  3268. * Before freeing hw queue, clearing the flush request reference in
  3269. * tags->rqs[] for avoiding potential UAF.
  3270. */
  3271. static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags,
  3272. unsigned int queue_depth, struct request *flush_rq)
  3273. {
  3274. int i;
  3275. unsigned long flags;
  3276. /* The hw queue may not be mapped yet */
  3277. if (!tags)
  3278. return;
  3279. WARN_ON_ONCE(req_ref_read(flush_rq) != 0);
  3280. for (i = 0; i < queue_depth; i++)
  3281. cmpxchg(&tags->rqs[i], flush_rq, NULL);
  3282. /*
  3283. * Wait until all pending iteration is done.
  3284. *
  3285. * Request reference is cleared and it is guaranteed to be observed
  3286. * after the ->lock is released.
  3287. */
  3288. spin_lock_irqsave(&tags->lock, flags);
  3289. spin_unlock_irqrestore(&tags->lock, flags);
  3290. }
  3291. /* hctx->ctxs will be freed in queue's release handler */
  3292. static void blk_mq_exit_hctx(struct request_queue *q,
  3293. struct blk_mq_tag_set *set,
  3294. struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
  3295. {
  3296. struct request *flush_rq = hctx->fq->flush_rq;
  3297. if (blk_mq_hw_queue_mapped(hctx))
  3298. blk_mq_tag_idle(hctx);
  3299. if (blk_queue_init_done(q))
  3300. blk_mq_clear_flush_rq_mapping(set->tags[hctx_idx],
  3301. set->queue_depth, flush_rq);
  3302. if (set->ops->exit_request)
  3303. set->ops->exit_request(set, flush_rq, hctx_idx);
  3304. if (set->ops->exit_hctx)
  3305. set->ops->exit_hctx(hctx, hctx_idx);
  3306. xa_erase(&q->hctx_table, hctx_idx);
  3307. spin_lock(&q->unused_hctx_lock);
  3308. list_add(&hctx->hctx_list, &q->unused_hctx_list);
  3309. spin_unlock(&q->unused_hctx_lock);
  3310. }
  3311. static void blk_mq_exit_hw_queues(struct request_queue *q,
  3312. struct blk_mq_tag_set *set, int nr_queue)
  3313. {
  3314. struct blk_mq_hw_ctx *hctx;
  3315. unsigned long i;
  3316. queue_for_each_hw_ctx(q, hctx, i) {
  3317. if (i == nr_queue)
  3318. break;
  3319. blk_mq_remove_cpuhp(hctx);
  3320. blk_mq_exit_hctx(q, set, hctx, i);
  3321. }
  3322. }
  3323. static int blk_mq_init_hctx(struct request_queue *q,
  3324. struct blk_mq_tag_set *set,
  3325. struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
  3326. {
  3327. hctx->queue_num = hctx_idx;
  3328. hctx->tags = set->tags[hctx_idx];
  3329. if (set->ops->init_hctx &&
  3330. set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
  3331. goto fail;
  3332. if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
  3333. hctx->numa_node))
  3334. goto exit_hctx;
  3335. if (xa_insert(&q->hctx_table, hctx_idx, hctx, GFP_KERNEL))
  3336. goto exit_flush_rq;
  3337. if (!(hctx->flags & BLK_MQ_F_STACKING))
  3338. cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
  3339. &hctx->cpuhp_online);
  3340. cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
  3341. return 0;
  3342. exit_flush_rq:
  3343. if (set->ops->exit_request)
  3344. set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx);
  3345. exit_hctx:
  3346. if (set->ops->exit_hctx)
  3347. set->ops->exit_hctx(hctx, hctx_idx);
  3348. fail:
  3349. return -1;
  3350. }
  3351. static struct blk_mq_hw_ctx *
  3352. blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
  3353. int node)
  3354. {
  3355. struct blk_mq_hw_ctx *hctx;
  3356. gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
  3357. hctx = kzalloc_node(sizeof(struct blk_mq_hw_ctx), gfp, node);
  3358. if (!hctx)
  3359. goto fail_alloc_hctx;
  3360. if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
  3361. goto free_hctx;
  3362. atomic_set(&hctx->nr_active, 0);
  3363. if (node == NUMA_NO_NODE)
  3364. node = set->numa_node;
  3365. hctx->numa_node = node;
  3366. INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
  3367. spin_lock_init(&hctx->lock);
  3368. INIT_LIST_HEAD(&hctx->dispatch);
  3369. INIT_HLIST_NODE(&hctx->cpuhp_dead);
  3370. INIT_HLIST_NODE(&hctx->cpuhp_online);
  3371. hctx->queue = q;
  3372. hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED;
  3373. INIT_LIST_HEAD(&hctx->hctx_list);
  3374. /*
  3375. * Allocate space for all possible cpus to avoid allocation at
  3376. * runtime
  3377. */
  3378. hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
  3379. gfp, node);
  3380. if (!hctx->ctxs)
  3381. goto free_cpumask;
  3382. if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
  3383. gfp, node, false, false))
  3384. goto free_ctxs;
  3385. hctx->nr_ctx = 0;
  3386. spin_lock_init(&hctx->dispatch_wait_lock);
  3387. init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
  3388. INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
  3389. hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp);
  3390. if (!hctx->fq)
  3391. goto free_bitmap;
  3392. blk_mq_hctx_kobj_init(hctx);
  3393. return hctx;
  3394. free_bitmap:
  3395. sbitmap_free(&hctx->ctx_map);
  3396. free_ctxs:
  3397. kfree(hctx->ctxs);
  3398. free_cpumask:
  3399. free_cpumask_var(hctx->cpumask);
  3400. free_hctx:
  3401. kfree(hctx);
  3402. fail_alloc_hctx:
  3403. return NULL;
  3404. }
  3405. static void blk_mq_init_cpu_queues(struct request_queue *q,
  3406. unsigned int nr_hw_queues)
  3407. {
  3408. struct blk_mq_tag_set *set = q->tag_set;
  3409. unsigned int i, j;
  3410. for_each_possible_cpu(i) {
  3411. struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
  3412. struct blk_mq_hw_ctx *hctx;
  3413. int k;
  3414. __ctx->cpu = i;
  3415. spin_lock_init(&__ctx->lock);
  3416. for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
  3417. INIT_LIST_HEAD(&__ctx->rq_lists[k]);
  3418. __ctx->queue = q;
  3419. /*
  3420. * Set local node, IFF we have more than one hw queue. If
  3421. * not, we remain on the home node of the device
  3422. */
  3423. for (j = 0; j < set->nr_maps; j++) {
  3424. hctx = blk_mq_map_queue_type(q, j, i);
  3425. if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
  3426. hctx->numa_node = cpu_to_node(i);
  3427. }
  3428. }
  3429. }
  3430. struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
  3431. unsigned int hctx_idx,
  3432. unsigned int depth)
  3433. {
  3434. struct blk_mq_tags *tags;
  3435. int ret;
  3436. tags = blk_mq_alloc_rq_map(set, hctx_idx, depth, set->reserved_tags);
  3437. if (!tags)
  3438. return NULL;
  3439. ret = blk_mq_alloc_rqs(set, tags, hctx_idx, depth);
  3440. if (ret) {
  3441. blk_mq_free_rq_map(tags);
  3442. return NULL;
  3443. }
  3444. return tags;
  3445. }
  3446. static bool __blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
  3447. int hctx_idx)
  3448. {
  3449. if (blk_mq_is_shared_tags(set->flags)) {
  3450. set->tags[hctx_idx] = set->shared_tags;
  3451. return true;
  3452. }
  3453. set->tags[hctx_idx] = blk_mq_alloc_map_and_rqs(set, hctx_idx,
  3454. set->queue_depth);
  3455. return set->tags[hctx_idx];
  3456. }
  3457. void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
  3458. struct blk_mq_tags *tags,
  3459. unsigned int hctx_idx)
  3460. {
  3461. if (tags) {
  3462. blk_mq_free_rqs(set, tags, hctx_idx);
  3463. blk_mq_free_rq_map(tags);
  3464. }
  3465. }
  3466. static void __blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
  3467. unsigned int hctx_idx)
  3468. {
  3469. if (!blk_mq_is_shared_tags(set->flags))
  3470. blk_mq_free_map_and_rqs(set, set->tags[hctx_idx], hctx_idx);
  3471. set->tags[hctx_idx] = NULL;
  3472. }
  3473. static void blk_mq_map_swqueue(struct request_queue *q)
  3474. {
  3475. unsigned int j, hctx_idx;
  3476. unsigned long i;
  3477. struct blk_mq_hw_ctx *hctx;
  3478. struct blk_mq_ctx *ctx;
  3479. struct blk_mq_tag_set *set = q->tag_set;
  3480. queue_for_each_hw_ctx(q, hctx, i) {
  3481. cpumask_clear(hctx->cpumask);
  3482. hctx->nr_ctx = 0;
  3483. hctx->dispatch_from = NULL;
  3484. }
  3485. /*
  3486. * Map software to hardware queues.
  3487. *
  3488. * If the cpu isn't present, the cpu is mapped to first hctx.
  3489. */
  3490. for_each_possible_cpu(i) {
  3491. ctx = per_cpu_ptr(q->queue_ctx, i);
  3492. for (j = 0; j < set->nr_maps; j++) {
  3493. if (!set->map[j].nr_queues) {
  3494. ctx->hctxs[j] = blk_mq_map_queue_type(q,
  3495. HCTX_TYPE_DEFAULT, i);
  3496. continue;
  3497. }
  3498. hctx_idx = set->map[j].mq_map[i];
  3499. /* unmapped hw queue can be remapped after CPU topo changed */
  3500. if (!set->tags[hctx_idx] &&
  3501. !__blk_mq_alloc_map_and_rqs(set, hctx_idx)) {
  3502. /*
  3503. * If tags initialization fail for some hctx,
  3504. * that hctx won't be brought online. In this
  3505. * case, remap the current ctx to hctx[0] which
  3506. * is guaranteed to always have tags allocated
  3507. */
  3508. set->map[j].mq_map[i] = 0;
  3509. }
  3510. hctx = blk_mq_map_queue_type(q, j, i);
  3511. ctx->hctxs[j] = hctx;
  3512. /*
  3513. * If the CPU is already set in the mask, then we've
  3514. * mapped this one already. This can happen if
  3515. * devices share queues across queue maps.
  3516. */
  3517. if (cpumask_test_cpu(i, hctx->cpumask))
  3518. continue;
  3519. cpumask_set_cpu(i, hctx->cpumask);
  3520. hctx->type = j;
  3521. ctx->index_hw[hctx->type] = hctx->nr_ctx;
  3522. hctx->ctxs[hctx->nr_ctx++] = ctx;
  3523. /*
  3524. * If the nr_ctx type overflows, we have exceeded the
  3525. * amount of sw queues we can support.
  3526. */
  3527. BUG_ON(!hctx->nr_ctx);
  3528. }
  3529. for (; j < HCTX_MAX_TYPES; j++)
  3530. ctx->hctxs[j] = blk_mq_map_queue_type(q,
  3531. HCTX_TYPE_DEFAULT, i);
  3532. }
  3533. queue_for_each_hw_ctx(q, hctx, i) {
  3534. int cpu;
  3535. /*
  3536. * If no software queues are mapped to this hardware queue,
  3537. * disable it and free the request entries.
  3538. */
  3539. if (!hctx->nr_ctx) {
  3540. /* Never unmap queue 0. We need it as a
  3541. * fallback in case of a new remap fails
  3542. * allocation
  3543. */
  3544. if (i)
  3545. __blk_mq_free_map_and_rqs(set, i);
  3546. hctx->tags = NULL;
  3547. continue;
  3548. }
  3549. hctx->tags = set->tags[i];
  3550. WARN_ON(!hctx->tags);
  3551. /*
  3552. * Set the map size to the number of mapped software queues.
  3553. * This is more accurate and more efficient than looping
  3554. * over all possibly mapped software queues.
  3555. */
  3556. sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
  3557. /*
  3558. * Rule out isolated CPUs from hctx->cpumask to avoid
  3559. * running block kworker on isolated CPUs
  3560. */
  3561. for_each_cpu(cpu, hctx->cpumask) {
  3562. if (cpu_is_isolated(cpu))
  3563. cpumask_clear_cpu(cpu, hctx->cpumask);
  3564. }
  3565. /*
  3566. * Initialize batch roundrobin counts
  3567. */
  3568. hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
  3569. hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
  3570. }
  3571. }
  3572. /*
  3573. * Caller needs to ensure that we're either frozen/quiesced, or that
  3574. * the queue isn't live yet.
  3575. */
  3576. static void queue_set_hctx_shared(struct request_queue *q, bool shared)
  3577. {
  3578. struct blk_mq_hw_ctx *hctx;
  3579. unsigned long i;
  3580. queue_for_each_hw_ctx(q, hctx, i) {
  3581. if (shared) {
  3582. hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
  3583. } else {
  3584. blk_mq_tag_idle(hctx);
  3585. hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
  3586. }
  3587. }
  3588. }
  3589. static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set,
  3590. bool shared)
  3591. {
  3592. struct request_queue *q;
  3593. lockdep_assert_held(&set->tag_list_lock);
  3594. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  3595. blk_mq_freeze_queue(q);
  3596. queue_set_hctx_shared(q, shared);
  3597. blk_mq_unfreeze_queue(q);
  3598. }
  3599. }
  3600. static void blk_mq_del_queue_tag_set(struct request_queue *q)
  3601. {
  3602. struct blk_mq_tag_set *set = q->tag_set;
  3603. mutex_lock(&set->tag_list_lock);
  3604. list_del(&q->tag_set_list);
  3605. if (list_is_singular(&set->tag_list)) {
  3606. /* just transitioned to unshared */
  3607. set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
  3608. /* update existing queue */
  3609. blk_mq_update_tag_set_shared(set, false);
  3610. }
  3611. mutex_unlock(&set->tag_list_lock);
  3612. INIT_LIST_HEAD(&q->tag_set_list);
  3613. }
  3614. static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
  3615. struct request_queue *q)
  3616. {
  3617. mutex_lock(&set->tag_list_lock);
  3618. /*
  3619. * Check to see if we're transitioning to shared (from 1 to 2 queues).
  3620. */
  3621. if (!list_empty(&set->tag_list) &&
  3622. !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
  3623. set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
  3624. /* update existing queue */
  3625. blk_mq_update_tag_set_shared(set, true);
  3626. }
  3627. if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
  3628. queue_set_hctx_shared(q, true);
  3629. list_add_tail(&q->tag_set_list, &set->tag_list);
  3630. mutex_unlock(&set->tag_list_lock);
  3631. }
  3632. /* All allocations will be freed in release handler of q->mq_kobj */
  3633. static int blk_mq_alloc_ctxs(struct request_queue *q)
  3634. {
  3635. struct blk_mq_ctxs *ctxs;
  3636. int cpu;
  3637. ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
  3638. if (!ctxs)
  3639. return -ENOMEM;
  3640. ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
  3641. if (!ctxs->queue_ctx)
  3642. goto fail;
  3643. for_each_possible_cpu(cpu) {
  3644. struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
  3645. ctx->ctxs = ctxs;
  3646. }
  3647. q->mq_kobj = &ctxs->kobj;
  3648. q->queue_ctx = ctxs->queue_ctx;
  3649. return 0;
  3650. fail:
  3651. kfree(ctxs);
  3652. return -ENOMEM;
  3653. }
  3654. /*
  3655. * It is the actual release handler for mq, but we do it from
  3656. * request queue's release handler for avoiding use-after-free
  3657. * and headache because q->mq_kobj shouldn't have been introduced,
  3658. * but we can't group ctx/kctx kobj without it.
  3659. */
  3660. void blk_mq_release(struct request_queue *q)
  3661. {
  3662. struct blk_mq_hw_ctx *hctx, *next;
  3663. unsigned long i;
  3664. queue_for_each_hw_ctx(q, hctx, i)
  3665. WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
  3666. /* all hctx are in .unused_hctx_list now */
  3667. list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
  3668. list_del_init(&hctx->hctx_list);
  3669. kobject_put(&hctx->kobj);
  3670. }
  3671. xa_destroy(&q->hctx_table);
  3672. /*
  3673. * release .mq_kobj and sw queue's kobject now because
  3674. * both share lifetime with request queue.
  3675. */
  3676. blk_mq_sysfs_deinit(q);
  3677. }
  3678. struct request_queue *blk_mq_alloc_queue(struct blk_mq_tag_set *set,
  3679. struct queue_limits *lim, void *queuedata)
  3680. {
  3681. struct queue_limits default_lim = { };
  3682. struct request_queue *q;
  3683. int ret;
  3684. if (!lim)
  3685. lim = &default_lim;
  3686. lim->features |= BLK_FEAT_IO_STAT | BLK_FEAT_NOWAIT;
  3687. if (set->nr_maps > HCTX_TYPE_POLL)
  3688. lim->features |= BLK_FEAT_POLL;
  3689. q = blk_alloc_queue(lim, set->numa_node);
  3690. if (IS_ERR(q))
  3691. return q;
  3692. q->queuedata = queuedata;
  3693. ret = blk_mq_init_allocated_queue(set, q);
  3694. if (ret) {
  3695. blk_put_queue(q);
  3696. return ERR_PTR(ret);
  3697. }
  3698. return q;
  3699. }
  3700. EXPORT_SYMBOL(blk_mq_alloc_queue);
  3701. /**
  3702. * blk_mq_destroy_queue - shutdown a request queue
  3703. * @q: request queue to shutdown
  3704. *
  3705. * This shuts down a request queue allocated by blk_mq_alloc_queue(). All future
  3706. * requests will be failed with -ENODEV. The caller is responsible for dropping
  3707. * the reference from blk_mq_alloc_queue() by calling blk_put_queue().
  3708. *
  3709. * Context: can sleep
  3710. */
  3711. void blk_mq_destroy_queue(struct request_queue *q)
  3712. {
  3713. WARN_ON_ONCE(!queue_is_mq(q));
  3714. WARN_ON_ONCE(blk_queue_registered(q));
  3715. might_sleep();
  3716. blk_queue_flag_set(QUEUE_FLAG_DYING, q);
  3717. blk_queue_start_drain(q);
  3718. blk_mq_freeze_queue_wait(q);
  3719. blk_sync_queue(q);
  3720. blk_mq_cancel_work_sync(q);
  3721. blk_mq_exit_queue(q);
  3722. }
  3723. EXPORT_SYMBOL(blk_mq_destroy_queue);
  3724. struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set,
  3725. struct queue_limits *lim, void *queuedata,
  3726. struct lock_class_key *lkclass)
  3727. {
  3728. struct request_queue *q;
  3729. struct gendisk *disk;
  3730. q = blk_mq_alloc_queue(set, lim, queuedata);
  3731. if (IS_ERR(q))
  3732. return ERR_CAST(q);
  3733. disk = __alloc_disk_node(q, set->numa_node, lkclass);
  3734. if (!disk) {
  3735. blk_mq_destroy_queue(q);
  3736. blk_put_queue(q);
  3737. return ERR_PTR(-ENOMEM);
  3738. }
  3739. set_bit(GD_OWNS_QUEUE, &disk->state);
  3740. return disk;
  3741. }
  3742. EXPORT_SYMBOL(__blk_mq_alloc_disk);
  3743. struct gendisk *blk_mq_alloc_disk_for_queue(struct request_queue *q,
  3744. struct lock_class_key *lkclass)
  3745. {
  3746. struct gendisk *disk;
  3747. if (!blk_get_queue(q))
  3748. return NULL;
  3749. disk = __alloc_disk_node(q, NUMA_NO_NODE, lkclass);
  3750. if (!disk)
  3751. blk_put_queue(q);
  3752. return disk;
  3753. }
  3754. EXPORT_SYMBOL(blk_mq_alloc_disk_for_queue);
  3755. /*
  3756. * Only hctx removed from cpuhp list can be reused
  3757. */
  3758. static bool blk_mq_hctx_is_reusable(struct blk_mq_hw_ctx *hctx)
  3759. {
  3760. return hlist_unhashed(&hctx->cpuhp_online) &&
  3761. hlist_unhashed(&hctx->cpuhp_dead);
  3762. }
  3763. static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
  3764. struct blk_mq_tag_set *set, struct request_queue *q,
  3765. int hctx_idx, int node)
  3766. {
  3767. struct blk_mq_hw_ctx *hctx = NULL, *tmp;
  3768. /* reuse dead hctx first */
  3769. spin_lock(&q->unused_hctx_lock);
  3770. list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
  3771. if (tmp->numa_node == node && blk_mq_hctx_is_reusable(tmp)) {
  3772. hctx = tmp;
  3773. break;
  3774. }
  3775. }
  3776. if (hctx)
  3777. list_del_init(&hctx->hctx_list);
  3778. spin_unlock(&q->unused_hctx_lock);
  3779. if (!hctx)
  3780. hctx = blk_mq_alloc_hctx(q, set, node);
  3781. if (!hctx)
  3782. goto fail;
  3783. if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
  3784. goto free_hctx;
  3785. return hctx;
  3786. free_hctx:
  3787. kobject_put(&hctx->kobj);
  3788. fail:
  3789. return NULL;
  3790. }
  3791. static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
  3792. struct request_queue *q)
  3793. {
  3794. struct blk_mq_hw_ctx *hctx;
  3795. unsigned long i, j;
  3796. /* protect against switching io scheduler */
  3797. mutex_lock(&q->sysfs_lock);
  3798. for (i = 0; i < set->nr_hw_queues; i++) {
  3799. int old_node;
  3800. int node = blk_mq_get_hctx_node(set, i);
  3801. struct blk_mq_hw_ctx *old_hctx = xa_load(&q->hctx_table, i);
  3802. if (old_hctx) {
  3803. old_node = old_hctx->numa_node;
  3804. blk_mq_exit_hctx(q, set, old_hctx, i);
  3805. }
  3806. if (!blk_mq_alloc_and_init_hctx(set, q, i, node)) {
  3807. if (!old_hctx)
  3808. break;
  3809. pr_warn("Allocate new hctx on node %d fails, fallback to previous one on node %d\n",
  3810. node, old_node);
  3811. hctx = blk_mq_alloc_and_init_hctx(set, q, i, old_node);
  3812. WARN_ON_ONCE(!hctx);
  3813. }
  3814. }
  3815. /*
  3816. * Increasing nr_hw_queues fails. Free the newly allocated
  3817. * hctxs and keep the previous q->nr_hw_queues.
  3818. */
  3819. if (i != set->nr_hw_queues) {
  3820. j = q->nr_hw_queues;
  3821. } else {
  3822. j = i;
  3823. q->nr_hw_queues = set->nr_hw_queues;
  3824. }
  3825. xa_for_each_start(&q->hctx_table, j, hctx, j)
  3826. blk_mq_exit_hctx(q, set, hctx, j);
  3827. mutex_unlock(&q->sysfs_lock);
  3828. /* unregister cpuhp callbacks for exited hctxs */
  3829. blk_mq_remove_hw_queues_cpuhp(q);
  3830. /* register cpuhp for new initialized hctxs */
  3831. blk_mq_add_hw_queues_cpuhp(q);
  3832. }
  3833. int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
  3834. struct request_queue *q)
  3835. {
  3836. /* mark the queue as mq asap */
  3837. q->mq_ops = set->ops;
  3838. /*
  3839. * ->tag_set has to be setup before initialize hctx, which cpuphp
  3840. * handler needs it for checking queue mapping
  3841. */
  3842. q->tag_set = set;
  3843. if (blk_mq_alloc_ctxs(q))
  3844. goto err_exit;
  3845. /* init q->mq_kobj and sw queues' kobjects */
  3846. blk_mq_sysfs_init(q);
  3847. INIT_LIST_HEAD(&q->unused_hctx_list);
  3848. spin_lock_init(&q->unused_hctx_lock);
  3849. xa_init(&q->hctx_table);
  3850. blk_mq_realloc_hw_ctxs(set, q);
  3851. if (!q->nr_hw_queues)
  3852. goto err_hctxs;
  3853. INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
  3854. blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
  3855. q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
  3856. INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
  3857. INIT_LIST_HEAD(&q->flush_list);
  3858. INIT_LIST_HEAD(&q->requeue_list);
  3859. spin_lock_init(&q->requeue_lock);
  3860. q->nr_requests = set->queue_depth;
  3861. blk_mq_init_cpu_queues(q, set->nr_hw_queues);
  3862. blk_mq_add_queue_tag_set(set, q);
  3863. blk_mq_map_swqueue(q);
  3864. return 0;
  3865. err_hctxs:
  3866. blk_mq_release(q);
  3867. err_exit:
  3868. q->mq_ops = NULL;
  3869. return -ENOMEM;
  3870. }
  3871. EXPORT_SYMBOL(blk_mq_init_allocated_queue);
  3872. /* tags can _not_ be used after returning from blk_mq_exit_queue */
  3873. void blk_mq_exit_queue(struct request_queue *q)
  3874. {
  3875. struct blk_mq_tag_set *set = q->tag_set;
  3876. /* Checks hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED. */
  3877. blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
  3878. /* May clear BLK_MQ_F_TAG_QUEUE_SHARED in hctx->flags. */
  3879. blk_mq_del_queue_tag_set(q);
  3880. }
  3881. static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
  3882. {
  3883. int i;
  3884. if (blk_mq_is_shared_tags(set->flags)) {
  3885. set->shared_tags = blk_mq_alloc_map_and_rqs(set,
  3886. BLK_MQ_NO_HCTX_IDX,
  3887. set->queue_depth);
  3888. if (!set->shared_tags)
  3889. return -ENOMEM;
  3890. }
  3891. for (i = 0; i < set->nr_hw_queues; i++) {
  3892. if (!__blk_mq_alloc_map_and_rqs(set, i))
  3893. goto out_unwind;
  3894. cond_resched();
  3895. }
  3896. return 0;
  3897. out_unwind:
  3898. while (--i >= 0)
  3899. __blk_mq_free_map_and_rqs(set, i);
  3900. if (blk_mq_is_shared_tags(set->flags)) {
  3901. blk_mq_free_map_and_rqs(set, set->shared_tags,
  3902. BLK_MQ_NO_HCTX_IDX);
  3903. }
  3904. return -ENOMEM;
  3905. }
  3906. /*
  3907. * Allocate the request maps associated with this tag_set. Note that this
  3908. * may reduce the depth asked for, if memory is tight. set->queue_depth
  3909. * will be updated to reflect the allocated depth.
  3910. */
  3911. static int blk_mq_alloc_set_map_and_rqs(struct blk_mq_tag_set *set)
  3912. {
  3913. unsigned int depth;
  3914. int err;
  3915. depth = set->queue_depth;
  3916. do {
  3917. err = __blk_mq_alloc_rq_maps(set);
  3918. if (!err)
  3919. break;
  3920. set->queue_depth >>= 1;
  3921. if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
  3922. err = -ENOMEM;
  3923. break;
  3924. }
  3925. } while (set->queue_depth);
  3926. if (!set->queue_depth || err) {
  3927. pr_err("blk-mq: failed to allocate request map\n");
  3928. return -ENOMEM;
  3929. }
  3930. if (depth != set->queue_depth)
  3931. pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
  3932. depth, set->queue_depth);
  3933. return 0;
  3934. }
  3935. static void blk_mq_update_queue_map(struct blk_mq_tag_set *set)
  3936. {
  3937. /*
  3938. * blk_mq_map_queues() and multiple .map_queues() implementations
  3939. * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the
  3940. * number of hardware queues.
  3941. */
  3942. if (set->nr_maps == 1)
  3943. set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues;
  3944. if (set->ops->map_queues) {
  3945. int i;
  3946. /*
  3947. * transport .map_queues is usually done in the following
  3948. * way:
  3949. *
  3950. * for (queue = 0; queue < set->nr_hw_queues; queue++) {
  3951. * mask = get_cpu_mask(queue)
  3952. * for_each_cpu(cpu, mask)
  3953. * set->map[x].mq_map[cpu] = queue;
  3954. * }
  3955. *
  3956. * When we need to remap, the table has to be cleared for
  3957. * killing stale mapping since one CPU may not be mapped
  3958. * to any hw queue.
  3959. */
  3960. for (i = 0; i < set->nr_maps; i++)
  3961. blk_mq_clear_mq_map(&set->map[i]);
  3962. set->ops->map_queues(set);
  3963. } else {
  3964. BUG_ON(set->nr_maps > 1);
  3965. blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
  3966. }
  3967. }
  3968. static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set,
  3969. int new_nr_hw_queues)
  3970. {
  3971. struct blk_mq_tags **new_tags;
  3972. int i;
  3973. if (set->nr_hw_queues >= new_nr_hw_queues)
  3974. goto done;
  3975. new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *),
  3976. GFP_KERNEL, set->numa_node);
  3977. if (!new_tags)
  3978. return -ENOMEM;
  3979. if (set->tags)
  3980. memcpy(new_tags, set->tags, set->nr_hw_queues *
  3981. sizeof(*set->tags));
  3982. kfree(set->tags);
  3983. set->tags = new_tags;
  3984. for (i = set->nr_hw_queues; i < new_nr_hw_queues; i++) {
  3985. if (!__blk_mq_alloc_map_and_rqs(set, i)) {
  3986. while (--i >= set->nr_hw_queues)
  3987. __blk_mq_free_map_and_rqs(set, i);
  3988. return -ENOMEM;
  3989. }
  3990. cond_resched();
  3991. }
  3992. done:
  3993. set->nr_hw_queues = new_nr_hw_queues;
  3994. return 0;
  3995. }
  3996. /*
  3997. * Alloc a tag set to be associated with one or more request queues.
  3998. * May fail with EINVAL for various error conditions. May adjust the
  3999. * requested depth down, if it's too large. In that case, the set
  4000. * value will be stored in set->queue_depth.
  4001. */
  4002. int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
  4003. {
  4004. int i, ret;
  4005. BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
  4006. if (!set->nr_hw_queues)
  4007. return -EINVAL;
  4008. if (!set->queue_depth)
  4009. return -EINVAL;
  4010. if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
  4011. return -EINVAL;
  4012. if (!set->ops->queue_rq)
  4013. return -EINVAL;
  4014. if (!set->ops->get_budget ^ !set->ops->put_budget)
  4015. return -EINVAL;
  4016. if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
  4017. pr_info("blk-mq: reduced tag depth to %u\n",
  4018. BLK_MQ_MAX_DEPTH);
  4019. set->queue_depth = BLK_MQ_MAX_DEPTH;
  4020. }
  4021. if (!set->nr_maps)
  4022. set->nr_maps = 1;
  4023. else if (set->nr_maps > HCTX_MAX_TYPES)
  4024. return -EINVAL;
  4025. /*
  4026. * If a crashdump is active, then we are potentially in a very
  4027. * memory constrained environment. Limit us to 64 tags to prevent
  4028. * using too much memory.
  4029. */
  4030. if (is_kdump_kernel())
  4031. set->queue_depth = min(64U, set->queue_depth);
  4032. /*
  4033. * There is no use for more h/w queues than cpus if we just have
  4034. * a single map
  4035. */
  4036. if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
  4037. set->nr_hw_queues = nr_cpu_ids;
  4038. if (set->flags & BLK_MQ_F_BLOCKING) {
  4039. set->srcu = kmalloc(sizeof(*set->srcu), GFP_KERNEL);
  4040. if (!set->srcu)
  4041. return -ENOMEM;
  4042. ret = init_srcu_struct(set->srcu);
  4043. if (ret)
  4044. goto out_free_srcu;
  4045. }
  4046. ret = -ENOMEM;
  4047. set->tags = kcalloc_node(set->nr_hw_queues,
  4048. sizeof(struct blk_mq_tags *), GFP_KERNEL,
  4049. set->numa_node);
  4050. if (!set->tags)
  4051. goto out_cleanup_srcu;
  4052. for (i = 0; i < set->nr_maps; i++) {
  4053. set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
  4054. sizeof(set->map[i].mq_map[0]),
  4055. GFP_KERNEL, set->numa_node);
  4056. if (!set->map[i].mq_map)
  4057. goto out_free_mq_map;
  4058. set->map[i].nr_queues = set->nr_hw_queues;
  4059. }
  4060. blk_mq_update_queue_map(set);
  4061. ret = blk_mq_alloc_set_map_and_rqs(set);
  4062. if (ret)
  4063. goto out_free_mq_map;
  4064. mutex_init(&set->tag_list_lock);
  4065. INIT_LIST_HEAD(&set->tag_list);
  4066. return 0;
  4067. out_free_mq_map:
  4068. for (i = 0; i < set->nr_maps; i++) {
  4069. kfree(set->map[i].mq_map);
  4070. set->map[i].mq_map = NULL;
  4071. }
  4072. kfree(set->tags);
  4073. set->tags = NULL;
  4074. out_cleanup_srcu:
  4075. if (set->flags & BLK_MQ_F_BLOCKING)
  4076. cleanup_srcu_struct(set->srcu);
  4077. out_free_srcu:
  4078. if (set->flags & BLK_MQ_F_BLOCKING)
  4079. kfree(set->srcu);
  4080. return ret;
  4081. }
  4082. EXPORT_SYMBOL(blk_mq_alloc_tag_set);
  4083. /* allocate and initialize a tagset for a simple single-queue device */
  4084. int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set,
  4085. const struct blk_mq_ops *ops, unsigned int queue_depth,
  4086. unsigned int set_flags)
  4087. {
  4088. memset(set, 0, sizeof(*set));
  4089. set->ops = ops;
  4090. set->nr_hw_queues = 1;
  4091. set->nr_maps = 1;
  4092. set->queue_depth = queue_depth;
  4093. set->numa_node = NUMA_NO_NODE;
  4094. set->flags = set_flags;
  4095. return blk_mq_alloc_tag_set(set);
  4096. }
  4097. EXPORT_SYMBOL_GPL(blk_mq_alloc_sq_tag_set);
  4098. void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
  4099. {
  4100. int i, j;
  4101. for (i = 0; i < set->nr_hw_queues; i++)
  4102. __blk_mq_free_map_and_rqs(set, i);
  4103. if (blk_mq_is_shared_tags(set->flags)) {
  4104. blk_mq_free_map_and_rqs(set, set->shared_tags,
  4105. BLK_MQ_NO_HCTX_IDX);
  4106. }
  4107. for (j = 0; j < set->nr_maps; j++) {
  4108. kfree(set->map[j].mq_map);
  4109. set->map[j].mq_map = NULL;
  4110. }
  4111. kfree(set->tags);
  4112. set->tags = NULL;
  4113. if (set->flags & BLK_MQ_F_BLOCKING) {
  4114. cleanup_srcu_struct(set->srcu);
  4115. kfree(set->srcu);
  4116. }
  4117. }
  4118. EXPORT_SYMBOL(blk_mq_free_tag_set);
  4119. int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
  4120. {
  4121. struct blk_mq_tag_set *set = q->tag_set;
  4122. struct blk_mq_hw_ctx *hctx;
  4123. int ret;
  4124. unsigned long i;
  4125. if (WARN_ON_ONCE(!q->mq_freeze_depth))
  4126. return -EINVAL;
  4127. if (!set)
  4128. return -EINVAL;
  4129. if (q->nr_requests == nr)
  4130. return 0;
  4131. blk_mq_quiesce_queue(q);
  4132. ret = 0;
  4133. queue_for_each_hw_ctx(q, hctx, i) {
  4134. if (!hctx->tags)
  4135. continue;
  4136. /*
  4137. * If we're using an MQ scheduler, just update the scheduler
  4138. * queue depth. This is similar to what the old code would do.
  4139. */
  4140. if (hctx->sched_tags) {
  4141. ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
  4142. nr, true);
  4143. } else {
  4144. ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
  4145. false);
  4146. }
  4147. if (ret)
  4148. break;
  4149. if (q->elevator && q->elevator->type->ops.depth_updated)
  4150. q->elevator->type->ops.depth_updated(hctx);
  4151. }
  4152. if (!ret) {
  4153. q->nr_requests = nr;
  4154. if (blk_mq_is_shared_tags(set->flags)) {
  4155. if (q->elevator)
  4156. blk_mq_tag_update_sched_shared_tags(q);
  4157. else
  4158. blk_mq_tag_resize_shared_tags(set, nr);
  4159. }
  4160. }
  4161. blk_mq_unquiesce_queue(q);
  4162. return ret;
  4163. }
  4164. /*
  4165. * request_queue and elevator_type pair.
  4166. * It is just used by __blk_mq_update_nr_hw_queues to cache
  4167. * the elevator_type associated with a request_queue.
  4168. */
  4169. struct blk_mq_qe_pair {
  4170. struct list_head node;
  4171. struct request_queue *q;
  4172. struct elevator_type *type;
  4173. };
  4174. /*
  4175. * Cache the elevator_type in qe pair list and switch the
  4176. * io scheduler to 'none'
  4177. */
  4178. static bool blk_mq_elv_switch_none(struct list_head *head,
  4179. struct request_queue *q)
  4180. {
  4181. struct blk_mq_qe_pair *qe;
  4182. qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
  4183. if (!qe)
  4184. return false;
  4185. /* q->elevator needs protection from ->sysfs_lock */
  4186. mutex_lock(&q->sysfs_lock);
  4187. /* the check has to be done with holding sysfs_lock */
  4188. if (!q->elevator) {
  4189. kfree(qe);
  4190. goto unlock;
  4191. }
  4192. INIT_LIST_HEAD(&qe->node);
  4193. qe->q = q;
  4194. qe->type = q->elevator->type;
  4195. /* keep a reference to the elevator module as we'll switch back */
  4196. __elevator_get(qe->type);
  4197. list_add(&qe->node, head);
  4198. elevator_disable(q);
  4199. unlock:
  4200. mutex_unlock(&q->sysfs_lock);
  4201. return true;
  4202. }
  4203. static struct blk_mq_qe_pair *blk_lookup_qe_pair(struct list_head *head,
  4204. struct request_queue *q)
  4205. {
  4206. struct blk_mq_qe_pair *qe;
  4207. list_for_each_entry(qe, head, node)
  4208. if (qe->q == q)
  4209. return qe;
  4210. return NULL;
  4211. }
  4212. static void blk_mq_elv_switch_back(struct list_head *head,
  4213. struct request_queue *q)
  4214. {
  4215. struct blk_mq_qe_pair *qe;
  4216. struct elevator_type *t;
  4217. qe = blk_lookup_qe_pair(head, q);
  4218. if (!qe)
  4219. return;
  4220. t = qe->type;
  4221. list_del(&qe->node);
  4222. kfree(qe);
  4223. mutex_lock(&q->sysfs_lock);
  4224. elevator_switch(q, t);
  4225. /* drop the reference acquired in blk_mq_elv_switch_none */
  4226. elevator_put(t);
  4227. mutex_unlock(&q->sysfs_lock);
  4228. }
  4229. static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
  4230. int nr_hw_queues)
  4231. {
  4232. struct request_queue *q;
  4233. LIST_HEAD(head);
  4234. int prev_nr_hw_queues = set->nr_hw_queues;
  4235. int i;
  4236. lockdep_assert_held(&set->tag_list_lock);
  4237. if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
  4238. nr_hw_queues = nr_cpu_ids;
  4239. if (nr_hw_queues < 1)
  4240. return;
  4241. if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues)
  4242. return;
  4243. list_for_each_entry(q, &set->tag_list, tag_set_list)
  4244. blk_mq_freeze_queue(q);
  4245. /*
  4246. * Switch IO scheduler to 'none', cleaning up the data associated
  4247. * with the previous scheduler. We will switch back once we are done
  4248. * updating the new sw to hw queue mappings.
  4249. */
  4250. list_for_each_entry(q, &set->tag_list, tag_set_list)
  4251. if (!blk_mq_elv_switch_none(&head, q))
  4252. goto switch_back;
  4253. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  4254. blk_mq_debugfs_unregister_hctxs(q);
  4255. blk_mq_sysfs_unregister_hctxs(q);
  4256. }
  4257. if (blk_mq_realloc_tag_set_tags(set, nr_hw_queues) < 0)
  4258. goto reregister;
  4259. fallback:
  4260. blk_mq_update_queue_map(set);
  4261. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  4262. blk_mq_realloc_hw_ctxs(set, q);
  4263. if (q->nr_hw_queues != set->nr_hw_queues) {
  4264. int i = prev_nr_hw_queues;
  4265. pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
  4266. nr_hw_queues, prev_nr_hw_queues);
  4267. for (; i < set->nr_hw_queues; i++)
  4268. __blk_mq_free_map_and_rqs(set, i);
  4269. set->nr_hw_queues = prev_nr_hw_queues;
  4270. goto fallback;
  4271. }
  4272. blk_mq_map_swqueue(q);
  4273. }
  4274. reregister:
  4275. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  4276. blk_mq_sysfs_register_hctxs(q);
  4277. blk_mq_debugfs_register_hctxs(q);
  4278. }
  4279. switch_back:
  4280. list_for_each_entry(q, &set->tag_list, tag_set_list)
  4281. blk_mq_elv_switch_back(&head, q);
  4282. list_for_each_entry(q, &set->tag_list, tag_set_list)
  4283. blk_mq_unfreeze_queue(q);
  4284. /* Free the excess tags when nr_hw_queues shrink. */
  4285. for (i = set->nr_hw_queues; i < prev_nr_hw_queues; i++)
  4286. __blk_mq_free_map_and_rqs(set, i);
  4287. }
  4288. void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
  4289. {
  4290. mutex_lock(&set->tag_list_lock);
  4291. __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
  4292. mutex_unlock(&set->tag_list_lock);
  4293. }
  4294. EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
  4295. static int blk_hctx_poll(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
  4296. struct io_comp_batch *iob, unsigned int flags)
  4297. {
  4298. long state = get_current_state();
  4299. int ret;
  4300. do {
  4301. ret = q->mq_ops->poll(hctx, iob);
  4302. if (ret > 0) {
  4303. __set_current_state(TASK_RUNNING);
  4304. return ret;
  4305. }
  4306. if (signal_pending_state(state, current))
  4307. __set_current_state(TASK_RUNNING);
  4308. if (task_is_running(current))
  4309. return 1;
  4310. if (ret < 0 || (flags & BLK_POLL_ONESHOT))
  4311. break;
  4312. cpu_relax();
  4313. } while (!need_resched());
  4314. __set_current_state(TASK_RUNNING);
  4315. return 0;
  4316. }
  4317. int blk_mq_poll(struct request_queue *q, blk_qc_t cookie,
  4318. struct io_comp_batch *iob, unsigned int flags)
  4319. {
  4320. if (!blk_mq_can_poll(q))
  4321. return 0;
  4322. return blk_hctx_poll(q, xa_load(&q->hctx_table, cookie), iob, flags);
  4323. }
  4324. int blk_rq_poll(struct request *rq, struct io_comp_batch *iob,
  4325. unsigned int poll_flags)
  4326. {
  4327. struct request_queue *q = rq->q;
  4328. int ret;
  4329. if (!blk_rq_is_poll(rq))
  4330. return 0;
  4331. if (!percpu_ref_tryget(&q->q_usage_counter))
  4332. return 0;
  4333. ret = blk_hctx_poll(q, rq->mq_hctx, iob, poll_flags);
  4334. blk_queue_exit(q);
  4335. return ret;
  4336. }
  4337. EXPORT_SYMBOL_GPL(blk_rq_poll);
  4338. unsigned int blk_mq_rq_cpu(struct request *rq)
  4339. {
  4340. return rq->mq_ctx->cpu;
  4341. }
  4342. EXPORT_SYMBOL(blk_mq_rq_cpu);
  4343. void blk_mq_cancel_work_sync(struct request_queue *q)
  4344. {
  4345. struct blk_mq_hw_ctx *hctx;
  4346. unsigned long i;
  4347. cancel_delayed_work_sync(&q->requeue_work);
  4348. queue_for_each_hw_ctx(q, hctx, i)
  4349. cancel_delayed_work_sync(&hctx->run_work);
  4350. }
  4351. static int __init blk_mq_init(void)
  4352. {
  4353. int i;
  4354. for_each_possible_cpu(i)
  4355. init_llist_head(&per_cpu(blk_cpu_done, i));
  4356. for_each_possible_cpu(i)
  4357. INIT_CSD(&per_cpu(blk_cpu_csd, i),
  4358. __blk_mq_complete_request_remote, NULL);
  4359. open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
  4360. cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
  4361. "block/softirq:dead", NULL,
  4362. blk_softirq_cpu_dead);
  4363. cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
  4364. blk_mq_hctx_notify_dead);
  4365. cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online",
  4366. blk_mq_hctx_notify_online,
  4367. blk_mq_hctx_notify_offline);
  4368. return 0;
  4369. }
  4370. subsys_initcall(blk_mq_init);