blk-mq.c 133 KB

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