bfq-iosched.c 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154
  1. /*
  2. * Budget Fair Queueing (BFQ) I/O scheduler.
  3. *
  4. * Based on ideas and code from CFQ:
  5. * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
  6. *
  7. * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
  8. * Paolo Valente <paolo.valente@unimore.it>
  9. *
  10. * Copyright (C) 2010 Paolo Valente <paolo.valente@unimore.it>
  11. * Arianna Avanzini <avanzini@google.com>
  12. *
  13. * Copyright (C) 2017 Paolo Valente <paolo.valente@linaro.org>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * General Public License for more details.
  24. *
  25. * BFQ is a proportional-share I/O scheduler, with some extra
  26. * low-latency capabilities. BFQ also supports full hierarchical
  27. * scheduling through cgroups. Next paragraphs provide an introduction
  28. * on BFQ inner workings. Details on BFQ benefits, usage and
  29. * limitations can be found in Documentation/block/bfq-iosched.txt.
  30. *
  31. * BFQ is a proportional-share storage-I/O scheduling algorithm based
  32. * on the slice-by-slice service scheme of CFQ. But BFQ assigns
  33. * budgets, measured in number of sectors, to processes instead of
  34. * time slices. The device is not granted to the in-service process
  35. * for a given time slice, but until it has exhausted its assigned
  36. * budget. This change from the time to the service domain enables BFQ
  37. * to distribute the device throughput among processes as desired,
  38. * without any distortion due to throughput fluctuations, or to device
  39. * internal queueing. BFQ uses an ad hoc internal scheduler, called
  40. * B-WF2Q+, to schedule processes according to their budgets. More
  41. * precisely, BFQ schedules queues associated with processes. Each
  42. * process/queue is assigned a user-configurable weight, and B-WF2Q+
  43. * guarantees that each queue receives a fraction of the throughput
  44. * proportional to its weight. Thanks to the accurate policy of
  45. * B-WF2Q+, BFQ can afford to assign high budgets to I/O-bound
  46. * processes issuing sequential requests (to boost the throughput),
  47. * and yet guarantee a low latency to interactive and soft real-time
  48. * applications.
  49. *
  50. * In particular, to provide these low-latency guarantees, BFQ
  51. * explicitly privileges the I/O of two classes of time-sensitive
  52. * applications: interactive and soft real-time. This feature enables
  53. * BFQ to provide applications in these classes with a very low
  54. * latency. Finally, BFQ also features additional heuristics for
  55. * preserving both a low latency and a high throughput on NCQ-capable,
  56. * rotational or flash-based devices, and to get the job done quickly
  57. * for applications consisting in many I/O-bound processes.
  58. *
  59. * NOTE: if the main or only goal, with a given device, is to achieve
  60. * the maximum-possible throughput at all times, then do switch off
  61. * all low-latency heuristics for that device, by setting low_latency
  62. * to 0.
  63. *
  64. * BFQ is described in [1], where also a reference to the initial, more
  65. * theoretical paper on BFQ can be found. The interested reader can find
  66. * in the latter paper full details on the main algorithm, as well as
  67. * formulas of the guarantees and formal proofs of all the properties.
  68. * With respect to the version of BFQ presented in these papers, this
  69. * implementation adds a few more heuristics, such as the one that
  70. * guarantees a low latency to soft real-time applications, and a
  71. * hierarchical extension based on H-WF2Q+.
  72. *
  73. * B-WF2Q+ is based on WF2Q+, which is described in [2], together with
  74. * H-WF2Q+, while the augmented tree used here to implement B-WF2Q+
  75. * with O(log N) complexity derives from the one introduced with EEVDF
  76. * in [3].
  77. *
  78. * [1] P. Valente, A. Avanzini, "Evolution of the BFQ Storage I/O
  79. * Scheduler", Proceedings of the First Workshop on Mobile System
  80. * Technologies (MST-2015), May 2015.
  81. * http://algogroup.unimore.it/people/paolo/disk_sched/mst-2015.pdf
  82. *
  83. * [2] Jon C.R. Bennett and H. Zhang, "Hierarchical Packet Fair Queueing
  84. * Algorithms", IEEE/ACM Transactions on Networking, 5(5):675-689,
  85. * Oct 1997.
  86. *
  87. * http://www.cs.cmu.edu/~hzhang/papers/TON-97-Oct.ps.gz
  88. *
  89. * [3] I. Stoica and H. Abdel-Wahab, "Earliest Eligible Virtual Deadline
  90. * First: A Flexible and Accurate Mechanism for Proportional Share
  91. * Resource Allocation", technical report.
  92. *
  93. * http://www.cs.berkeley.edu/~istoica/papers/eevdf-tr-95.pdf
  94. */
  95. #include <linux/module.h>
  96. #include <linux/slab.h>
  97. #include <linux/blkdev.h>
  98. #include <linux/cgroup.h>
  99. #include <linux/elevator.h>
  100. #include <linux/ktime.h>
  101. #include <linux/rbtree.h>
  102. #include <linux/ioprio.h>
  103. #include <linux/sbitmap.h>
  104. #include <linux/delay.h>
  105. #include "blk.h"
  106. #include "blk-mq.h"
  107. #include "blk-mq-tag.h"
  108. #include "blk-mq-sched.h"
  109. #include "bfq-iosched.h"
  110. #include "blk-wbt.h"
  111. #define BFQ_BFQQ_FNS(name) \
  112. void bfq_mark_bfqq_##name(struct bfq_queue *bfqq) \
  113. { \
  114. __set_bit(BFQQF_##name, &(bfqq)->flags); \
  115. } \
  116. void bfq_clear_bfqq_##name(struct bfq_queue *bfqq) \
  117. { \
  118. __clear_bit(BFQQF_##name, &(bfqq)->flags); \
  119. } \
  120. int bfq_bfqq_##name(const struct bfq_queue *bfqq) \
  121. { \
  122. return test_bit(BFQQF_##name, &(bfqq)->flags); \
  123. }
  124. BFQ_BFQQ_FNS(just_created);
  125. BFQ_BFQQ_FNS(busy);
  126. BFQ_BFQQ_FNS(wait_request);
  127. BFQ_BFQQ_FNS(non_blocking_wait_rq);
  128. BFQ_BFQQ_FNS(fifo_expire);
  129. BFQ_BFQQ_FNS(has_short_ttime);
  130. BFQ_BFQQ_FNS(sync);
  131. BFQ_BFQQ_FNS(IO_bound);
  132. BFQ_BFQQ_FNS(in_large_burst);
  133. BFQ_BFQQ_FNS(coop);
  134. BFQ_BFQQ_FNS(split_coop);
  135. BFQ_BFQQ_FNS(softrt_update);
  136. #undef BFQ_BFQQ_FNS \
  137. /* Expiration time of sync (0) and async (1) requests, in ns. */
  138. static const u64 bfq_fifo_expire[2] = { NSEC_PER_SEC / 4, NSEC_PER_SEC / 8 };
  139. /* Maximum backwards seek (magic number lifted from CFQ), in KiB. */
  140. static const int bfq_back_max = 16 * 1024;
  141. /* Penalty of a backwards seek, in number of sectors. */
  142. static const int bfq_back_penalty = 2;
  143. /* Idling period duration, in ns. */
  144. static u64 bfq_slice_idle = NSEC_PER_SEC / 125;
  145. /* Minimum number of assigned budgets for which stats are safe to compute. */
  146. static const int bfq_stats_min_budgets = 194;
  147. /* Default maximum budget values, in sectors and number of requests. */
  148. static const int bfq_default_max_budget = 16 * 1024;
  149. /*
  150. * Async to sync throughput distribution is controlled as follows:
  151. * when an async request is served, the entity is charged the number
  152. * of sectors of the request, multiplied by the factor below
  153. */
  154. static const int bfq_async_charge_factor = 10;
  155. /* Default timeout values, in jiffies, approximating CFQ defaults. */
  156. const int bfq_timeout = HZ / 8;
  157. static struct kmem_cache *bfq_pool;
  158. /* Below this threshold (in ns), we consider thinktime immediate. */
  159. #define BFQ_MIN_TT (2 * NSEC_PER_MSEC)
  160. /* hw_tag detection: parallel requests threshold and min samples needed. */
  161. #define BFQ_HW_QUEUE_THRESHOLD 4
  162. #define BFQ_HW_QUEUE_SAMPLES 32
  163. #define BFQQ_SEEK_THR (sector_t)(8 * 100)
  164. #define BFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
  165. #define BFQQ_CLOSE_THR (sector_t)(8 * 1024)
  166. #define BFQQ_SEEKY(bfqq) (hweight32(bfqq->seek_history) > 32/8)
  167. /* Min number of samples required to perform peak-rate update */
  168. #define BFQ_RATE_MIN_SAMPLES 32
  169. /* Min observation time interval required to perform a peak-rate update (ns) */
  170. #define BFQ_RATE_MIN_INTERVAL (300*NSEC_PER_MSEC)
  171. /* Target observation time interval for a peak-rate update (ns) */
  172. #define BFQ_RATE_REF_INTERVAL NSEC_PER_SEC
  173. /* Shift used for peak rate fixed precision calculations. */
  174. #define BFQ_RATE_SHIFT 16
  175. /*
  176. * By default, BFQ computes the duration of the weight raising for
  177. * interactive applications automatically, using the following formula:
  178. * duration = (R / r) * T, where r is the peak rate of the device, and
  179. * R and T are two reference parameters.
  180. * In particular, R is the peak rate of the reference device (see below),
  181. * and T is a reference time: given the systems that are likely to be
  182. * installed on the reference device according to its speed class, T is
  183. * about the maximum time needed, under BFQ and while reading two files in
  184. * parallel, to load typical large applications on these systems.
  185. * In practice, the slower/faster the device at hand is, the more/less it
  186. * takes to load applications with respect to the reference device.
  187. * Accordingly, the longer/shorter BFQ grants weight raising to interactive
  188. * applications.
  189. *
  190. * BFQ uses four different reference pairs (R, T), depending on:
  191. * . whether the device is rotational or non-rotational;
  192. * . whether the device is slow, such as old or portable HDDs, as well as
  193. * SD cards, or fast, such as newer HDDs and SSDs.
  194. *
  195. * The device's speed class is dynamically (re)detected in
  196. * bfq_update_peak_rate() every time the estimated peak rate is updated.
  197. *
  198. * In the following definitions, R_slow[0]/R_fast[0] and
  199. * T_slow[0]/T_fast[0] are the reference values for a slow/fast
  200. * rotational device, whereas R_slow[1]/R_fast[1] and
  201. * T_slow[1]/T_fast[1] are the reference values for a slow/fast
  202. * non-rotational device. Finally, device_speed_thresh are the
  203. * thresholds used to switch between speed classes. The reference
  204. * rates are not the actual peak rates of the devices used as a
  205. * reference, but slightly lower values. The reason for using these
  206. * slightly lower values is that the peak-rate estimator tends to
  207. * yield slightly lower values than the actual peak rate (it can yield
  208. * the actual peak rate only if there is only one process doing I/O,
  209. * and the process does sequential I/O).
  210. *
  211. * Both the reference peak rates and the thresholds are measured in
  212. * sectors/usec, left-shifted by BFQ_RATE_SHIFT.
  213. */
  214. static int R_slow[2] = {1000, 10700};
  215. static int R_fast[2] = {14000, 33000};
  216. /*
  217. * To improve readability, a conversion function is used to initialize the
  218. * following arrays, which entails that they can be initialized only in a
  219. * function.
  220. */
  221. static int T_slow[2];
  222. static int T_fast[2];
  223. static int device_speed_thresh[2];
  224. #define RQ_BIC(rq) icq_to_bic((rq)->elv.priv[0])
  225. #define RQ_BFQQ(rq) ((rq)->elv.priv[1])
  226. struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync)
  227. {
  228. return bic->bfqq[is_sync];
  229. }
  230. void bic_set_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq, bool is_sync)
  231. {
  232. bic->bfqq[is_sync] = bfqq;
  233. }
  234. struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic)
  235. {
  236. return bic->icq.q->elevator->elevator_data;
  237. }
  238. /**
  239. * icq_to_bic - convert iocontext queue structure to bfq_io_cq.
  240. * @icq: the iocontext queue.
  241. */
  242. static struct bfq_io_cq *icq_to_bic(struct io_cq *icq)
  243. {
  244. /* bic->icq is the first member, %NULL will convert to %NULL */
  245. return container_of(icq, struct bfq_io_cq, icq);
  246. }
  247. /**
  248. * bfq_bic_lookup - search into @ioc a bic associated to @bfqd.
  249. * @bfqd: the lookup key.
  250. * @ioc: the io_context of the process doing I/O.
  251. * @q: the request queue.
  252. */
  253. static struct bfq_io_cq *bfq_bic_lookup(struct bfq_data *bfqd,
  254. struct io_context *ioc,
  255. struct request_queue *q)
  256. {
  257. if (ioc) {
  258. unsigned long flags;
  259. struct bfq_io_cq *icq;
  260. spin_lock_irqsave(q->queue_lock, flags);
  261. icq = icq_to_bic(ioc_lookup_icq(ioc, q));
  262. spin_unlock_irqrestore(q->queue_lock, flags);
  263. return icq;
  264. }
  265. return NULL;
  266. }
  267. /*
  268. * Scheduler run of queue, if there are requests pending and no one in the
  269. * driver that will restart queueing.
  270. */
  271. void bfq_schedule_dispatch(struct bfq_data *bfqd)
  272. {
  273. if (bfqd->queued != 0) {
  274. bfq_log(bfqd, "schedule dispatch");
  275. blk_mq_run_hw_queues(bfqd->queue, true);
  276. }
  277. }
  278. #define bfq_class_idle(bfqq) ((bfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
  279. #define bfq_class_rt(bfqq) ((bfqq)->ioprio_class == IOPRIO_CLASS_RT)
  280. #define bfq_sample_valid(samples) ((samples) > 80)
  281. /*
  282. * Lifted from AS - choose which of rq1 and rq2 that is best served now.
  283. * We choose the request that is closesr to the head right now. Distance
  284. * behind the head is penalized and only allowed to a certain extent.
  285. */
  286. static struct request *bfq_choose_req(struct bfq_data *bfqd,
  287. struct request *rq1,
  288. struct request *rq2,
  289. sector_t last)
  290. {
  291. sector_t s1, s2, d1 = 0, d2 = 0;
  292. unsigned long back_max;
  293. #define BFQ_RQ1_WRAP 0x01 /* request 1 wraps */
  294. #define BFQ_RQ2_WRAP 0x02 /* request 2 wraps */
  295. unsigned int wrap = 0; /* bit mask: requests behind the disk head? */
  296. if (!rq1 || rq1 == rq2)
  297. return rq2;
  298. if (!rq2)
  299. return rq1;
  300. if (rq_is_sync(rq1) && !rq_is_sync(rq2))
  301. return rq1;
  302. else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
  303. return rq2;
  304. if ((rq1->cmd_flags & REQ_META) && !(rq2->cmd_flags & REQ_META))
  305. return rq1;
  306. else if ((rq2->cmd_flags & REQ_META) && !(rq1->cmd_flags & REQ_META))
  307. return rq2;
  308. s1 = blk_rq_pos(rq1);
  309. s2 = blk_rq_pos(rq2);
  310. /*
  311. * By definition, 1KiB is 2 sectors.
  312. */
  313. back_max = bfqd->bfq_back_max * 2;
  314. /*
  315. * Strict one way elevator _except_ in the case where we allow
  316. * short backward seeks which are biased as twice the cost of a
  317. * similar forward seek.
  318. */
  319. if (s1 >= last)
  320. d1 = s1 - last;
  321. else if (s1 + back_max >= last)
  322. d1 = (last - s1) * bfqd->bfq_back_penalty;
  323. else
  324. wrap |= BFQ_RQ1_WRAP;
  325. if (s2 >= last)
  326. d2 = s2 - last;
  327. else if (s2 + back_max >= last)
  328. d2 = (last - s2) * bfqd->bfq_back_penalty;
  329. else
  330. wrap |= BFQ_RQ2_WRAP;
  331. /* Found required data */
  332. /*
  333. * By doing switch() on the bit mask "wrap" we avoid having to
  334. * check two variables for all permutations: --> faster!
  335. */
  336. switch (wrap) {
  337. case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
  338. if (d1 < d2)
  339. return rq1;
  340. else if (d2 < d1)
  341. return rq2;
  342. if (s1 >= s2)
  343. return rq1;
  344. else
  345. return rq2;
  346. case BFQ_RQ2_WRAP:
  347. return rq1;
  348. case BFQ_RQ1_WRAP:
  349. return rq2;
  350. case BFQ_RQ1_WRAP|BFQ_RQ2_WRAP: /* both rqs wrapped */
  351. default:
  352. /*
  353. * Since both rqs are wrapped,
  354. * start with the one that's further behind head
  355. * (--> only *one* back seek required),
  356. * since back seek takes more time than forward.
  357. */
  358. if (s1 <= s2)
  359. return rq1;
  360. else
  361. return rq2;
  362. }
  363. }
  364. static struct bfq_queue *
  365. bfq_rq_pos_tree_lookup(struct bfq_data *bfqd, struct rb_root *root,
  366. sector_t sector, struct rb_node **ret_parent,
  367. struct rb_node ***rb_link)
  368. {
  369. struct rb_node **p, *parent;
  370. struct bfq_queue *bfqq = NULL;
  371. parent = NULL;
  372. p = &root->rb_node;
  373. while (*p) {
  374. struct rb_node **n;
  375. parent = *p;
  376. bfqq = rb_entry(parent, struct bfq_queue, pos_node);
  377. /*
  378. * Sort strictly based on sector. Smallest to the left,
  379. * largest to the right.
  380. */
  381. if (sector > blk_rq_pos(bfqq->next_rq))
  382. n = &(*p)->rb_right;
  383. else if (sector < blk_rq_pos(bfqq->next_rq))
  384. n = &(*p)->rb_left;
  385. else
  386. break;
  387. p = n;
  388. bfqq = NULL;
  389. }
  390. *ret_parent = parent;
  391. if (rb_link)
  392. *rb_link = p;
  393. bfq_log(bfqd, "rq_pos_tree_lookup %llu: returning %d",
  394. (unsigned long long)sector,
  395. bfqq ? bfqq->pid : 0);
  396. return bfqq;
  397. }
  398. void bfq_pos_tree_add_move(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  399. {
  400. struct rb_node **p, *parent;
  401. struct bfq_queue *__bfqq;
  402. if (bfqq->pos_root) {
  403. rb_erase(&bfqq->pos_node, bfqq->pos_root);
  404. bfqq->pos_root = NULL;
  405. }
  406. if (bfq_class_idle(bfqq))
  407. return;
  408. if (!bfqq->next_rq)
  409. return;
  410. bfqq->pos_root = &bfq_bfqq_to_bfqg(bfqq)->rq_pos_tree;
  411. __bfqq = bfq_rq_pos_tree_lookup(bfqd, bfqq->pos_root,
  412. blk_rq_pos(bfqq->next_rq), &parent, &p);
  413. if (!__bfqq) {
  414. rb_link_node(&bfqq->pos_node, parent, p);
  415. rb_insert_color(&bfqq->pos_node, bfqq->pos_root);
  416. } else
  417. bfqq->pos_root = NULL;
  418. }
  419. /*
  420. * Tell whether there are active queues or groups with differentiated weights.
  421. */
  422. static bool bfq_differentiated_weights(struct bfq_data *bfqd)
  423. {
  424. /*
  425. * For weights to differ, at least one of the trees must contain
  426. * at least two nodes.
  427. */
  428. return (!RB_EMPTY_ROOT(&bfqd->queue_weights_tree) &&
  429. (bfqd->queue_weights_tree.rb_node->rb_left ||
  430. bfqd->queue_weights_tree.rb_node->rb_right)
  431. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  432. ) ||
  433. (!RB_EMPTY_ROOT(&bfqd->group_weights_tree) &&
  434. (bfqd->group_weights_tree.rb_node->rb_left ||
  435. bfqd->group_weights_tree.rb_node->rb_right)
  436. #endif
  437. );
  438. }
  439. /*
  440. * The following function returns true if every queue must receive the
  441. * same share of the throughput (this condition is used when deciding
  442. * whether idling may be disabled, see the comments in the function
  443. * bfq_bfqq_may_idle()).
  444. *
  445. * Such a scenario occurs when:
  446. * 1) all active queues have the same weight,
  447. * 2) all active groups at the same level in the groups tree have the same
  448. * weight,
  449. * 3) all active groups at the same level in the groups tree have the same
  450. * number of children.
  451. *
  452. * Unfortunately, keeping the necessary state for evaluating exactly the
  453. * above symmetry conditions would be quite complex and time-consuming.
  454. * Therefore this function evaluates, instead, the following stronger
  455. * sub-conditions, for which it is much easier to maintain the needed
  456. * state:
  457. * 1) all active queues have the same weight,
  458. * 2) all active groups have the same weight,
  459. * 3) all active groups have at most one active child each.
  460. * In particular, the last two conditions are always true if hierarchical
  461. * support and the cgroups interface are not enabled, thus no state needs
  462. * to be maintained in this case.
  463. */
  464. static bool bfq_symmetric_scenario(struct bfq_data *bfqd)
  465. {
  466. return !bfq_differentiated_weights(bfqd);
  467. }
  468. /*
  469. * If the weight-counter tree passed as input contains no counter for
  470. * the weight of the input entity, then add that counter; otherwise just
  471. * increment the existing counter.
  472. *
  473. * Note that weight-counter trees contain few nodes in mostly symmetric
  474. * scenarios. For example, if all queues have the same weight, then the
  475. * weight-counter tree for the queues may contain at most one node.
  476. * This holds even if low_latency is on, because weight-raised queues
  477. * are not inserted in the tree.
  478. * In most scenarios, the rate at which nodes are created/destroyed
  479. * should be low too.
  480. */
  481. void bfq_weights_tree_add(struct bfq_data *bfqd, struct bfq_entity *entity,
  482. struct rb_root *root)
  483. {
  484. struct rb_node **new = &(root->rb_node), *parent = NULL;
  485. /*
  486. * Do not insert if the entity is already associated with a
  487. * counter, which happens if:
  488. * 1) the entity is associated with a queue,
  489. * 2) a request arrival has caused the queue to become both
  490. * non-weight-raised, and hence change its weight, and
  491. * backlogged; in this respect, each of the two events
  492. * causes an invocation of this function,
  493. * 3) this is the invocation of this function caused by the
  494. * second event. This second invocation is actually useless,
  495. * and we handle this fact by exiting immediately. More
  496. * efficient or clearer solutions might possibly be adopted.
  497. */
  498. if (entity->weight_counter)
  499. return;
  500. while (*new) {
  501. struct bfq_weight_counter *__counter = container_of(*new,
  502. struct bfq_weight_counter,
  503. weights_node);
  504. parent = *new;
  505. if (entity->weight == __counter->weight) {
  506. entity->weight_counter = __counter;
  507. goto inc_counter;
  508. }
  509. if (entity->weight < __counter->weight)
  510. new = &((*new)->rb_left);
  511. else
  512. new = &((*new)->rb_right);
  513. }
  514. entity->weight_counter = kzalloc(sizeof(struct bfq_weight_counter),
  515. GFP_ATOMIC);
  516. /*
  517. * In the unlucky event of an allocation failure, we just
  518. * exit. This will cause the weight of entity to not be
  519. * considered in bfq_differentiated_weights, which, in its
  520. * turn, causes the scenario to be deemed wrongly symmetric in
  521. * case entity's weight would have been the only weight making
  522. * the scenario asymmetric. On the bright side, no unbalance
  523. * will however occur when entity becomes inactive again (the
  524. * invocation of this function is triggered by an activation
  525. * of entity). In fact, bfq_weights_tree_remove does nothing
  526. * if !entity->weight_counter.
  527. */
  528. if (unlikely(!entity->weight_counter))
  529. return;
  530. entity->weight_counter->weight = entity->weight;
  531. rb_link_node(&entity->weight_counter->weights_node, parent, new);
  532. rb_insert_color(&entity->weight_counter->weights_node, root);
  533. inc_counter:
  534. entity->weight_counter->num_active++;
  535. }
  536. /*
  537. * Decrement the weight counter associated with the entity, and, if the
  538. * counter reaches 0, remove the counter from the tree.
  539. * See the comments to the function bfq_weights_tree_add() for considerations
  540. * about overhead.
  541. */
  542. void bfq_weights_tree_remove(struct bfq_data *bfqd, struct bfq_entity *entity,
  543. struct rb_root *root)
  544. {
  545. if (!entity->weight_counter)
  546. return;
  547. entity->weight_counter->num_active--;
  548. if (entity->weight_counter->num_active > 0)
  549. goto reset_entity_pointer;
  550. rb_erase(&entity->weight_counter->weights_node, root);
  551. kfree(entity->weight_counter);
  552. reset_entity_pointer:
  553. entity->weight_counter = NULL;
  554. }
  555. /*
  556. * Return expired entry, or NULL to just start from scratch in rbtree.
  557. */
  558. static struct request *bfq_check_fifo(struct bfq_queue *bfqq,
  559. struct request *last)
  560. {
  561. struct request *rq;
  562. if (bfq_bfqq_fifo_expire(bfqq))
  563. return NULL;
  564. bfq_mark_bfqq_fifo_expire(bfqq);
  565. rq = rq_entry_fifo(bfqq->fifo.next);
  566. if (rq == last || ktime_get_ns() < rq->fifo_time)
  567. return NULL;
  568. bfq_log_bfqq(bfqq->bfqd, bfqq, "check_fifo: returned %p", rq);
  569. return rq;
  570. }
  571. static struct request *bfq_find_next_rq(struct bfq_data *bfqd,
  572. struct bfq_queue *bfqq,
  573. struct request *last)
  574. {
  575. struct rb_node *rbnext = rb_next(&last->rb_node);
  576. struct rb_node *rbprev = rb_prev(&last->rb_node);
  577. struct request *next, *prev = NULL;
  578. /* Follow expired path, else get first next available. */
  579. next = bfq_check_fifo(bfqq, last);
  580. if (next)
  581. return next;
  582. if (rbprev)
  583. prev = rb_entry_rq(rbprev);
  584. if (rbnext)
  585. next = rb_entry_rq(rbnext);
  586. else {
  587. rbnext = rb_first(&bfqq->sort_list);
  588. if (rbnext && rbnext != &last->rb_node)
  589. next = rb_entry_rq(rbnext);
  590. }
  591. return bfq_choose_req(bfqd, next, prev, blk_rq_pos(last));
  592. }
  593. /* see the definition of bfq_async_charge_factor for details */
  594. static unsigned long bfq_serv_to_charge(struct request *rq,
  595. struct bfq_queue *bfqq)
  596. {
  597. if (bfq_bfqq_sync(bfqq) || bfqq->wr_coeff > 1)
  598. return blk_rq_sectors(rq);
  599. /*
  600. * If there are no weight-raised queues, then amplify service
  601. * by just the async charge factor; otherwise amplify service
  602. * by twice the async charge factor, to further reduce latency
  603. * for weight-raised queues.
  604. */
  605. if (bfqq->bfqd->wr_busy_queues == 0)
  606. return blk_rq_sectors(rq) * bfq_async_charge_factor;
  607. return blk_rq_sectors(rq) * 2 * bfq_async_charge_factor;
  608. }
  609. /**
  610. * bfq_updated_next_req - update the queue after a new next_rq selection.
  611. * @bfqd: the device data the queue belongs to.
  612. * @bfqq: the queue to update.
  613. *
  614. * If the first request of a queue changes we make sure that the queue
  615. * has enough budget to serve at least its first request (if the
  616. * request has grown). We do this because if the queue has not enough
  617. * budget for its first request, it has to go through two dispatch
  618. * rounds to actually get it dispatched.
  619. */
  620. static void bfq_updated_next_req(struct bfq_data *bfqd,
  621. struct bfq_queue *bfqq)
  622. {
  623. struct bfq_entity *entity = &bfqq->entity;
  624. struct request *next_rq = bfqq->next_rq;
  625. unsigned long new_budget;
  626. if (!next_rq)
  627. return;
  628. if (bfqq == bfqd->in_service_queue)
  629. /*
  630. * In order not to break guarantees, budgets cannot be
  631. * changed after an entity has been selected.
  632. */
  633. return;
  634. new_budget = max_t(unsigned long, bfqq->max_budget,
  635. bfq_serv_to_charge(next_rq, bfqq));
  636. if (entity->budget != new_budget) {
  637. entity->budget = new_budget;
  638. bfq_log_bfqq(bfqd, bfqq, "updated next rq: new budget %lu",
  639. new_budget);
  640. bfq_requeue_bfqq(bfqd, bfqq, false);
  641. }
  642. }
  643. static void
  644. bfq_bfqq_resume_state(struct bfq_queue *bfqq, struct bfq_data *bfqd,
  645. struct bfq_io_cq *bic, bool bfq_already_existing)
  646. {
  647. unsigned int old_wr_coeff = bfqq->wr_coeff;
  648. bool busy = bfq_already_existing && bfq_bfqq_busy(bfqq);
  649. if (bic->saved_has_short_ttime)
  650. bfq_mark_bfqq_has_short_ttime(bfqq);
  651. else
  652. bfq_clear_bfqq_has_short_ttime(bfqq);
  653. if (bic->saved_IO_bound)
  654. bfq_mark_bfqq_IO_bound(bfqq);
  655. else
  656. bfq_clear_bfqq_IO_bound(bfqq);
  657. bfqq->ttime = bic->saved_ttime;
  658. bfqq->wr_coeff = bic->saved_wr_coeff;
  659. bfqq->wr_start_at_switch_to_srt = bic->saved_wr_start_at_switch_to_srt;
  660. bfqq->last_wr_start_finish = bic->saved_last_wr_start_finish;
  661. bfqq->wr_cur_max_time = bic->saved_wr_cur_max_time;
  662. if (bfqq->wr_coeff > 1 && (bfq_bfqq_in_large_burst(bfqq) ||
  663. time_is_before_jiffies(bfqq->last_wr_start_finish +
  664. bfqq->wr_cur_max_time))) {
  665. bfq_log_bfqq(bfqq->bfqd, bfqq,
  666. "resume state: switching off wr");
  667. bfqq->wr_coeff = 1;
  668. }
  669. /* make sure weight will be updated, however we got here */
  670. bfqq->entity.prio_changed = 1;
  671. if (likely(!busy))
  672. return;
  673. if (old_wr_coeff == 1 && bfqq->wr_coeff > 1)
  674. bfqd->wr_busy_queues++;
  675. else if (old_wr_coeff > 1 && bfqq->wr_coeff == 1)
  676. bfqd->wr_busy_queues--;
  677. }
  678. static int bfqq_process_refs(struct bfq_queue *bfqq)
  679. {
  680. return bfqq->ref - bfqq->allocated - bfqq->entity.on_st;
  681. }
  682. /* Empty burst list and add just bfqq (see comments on bfq_handle_burst) */
  683. static void bfq_reset_burst_list(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  684. {
  685. struct bfq_queue *item;
  686. struct hlist_node *n;
  687. hlist_for_each_entry_safe(item, n, &bfqd->burst_list, burst_list_node)
  688. hlist_del_init(&item->burst_list_node);
  689. hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list);
  690. bfqd->burst_size = 1;
  691. bfqd->burst_parent_entity = bfqq->entity.parent;
  692. }
  693. /* Add bfqq to the list of queues in current burst (see bfq_handle_burst) */
  694. static void bfq_add_to_burst(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  695. {
  696. /* Increment burst size to take into account also bfqq */
  697. bfqd->burst_size++;
  698. if (bfqd->burst_size == bfqd->bfq_large_burst_thresh) {
  699. struct bfq_queue *pos, *bfqq_item;
  700. struct hlist_node *n;
  701. /*
  702. * Enough queues have been activated shortly after each
  703. * other to consider this burst as large.
  704. */
  705. bfqd->large_burst = true;
  706. /*
  707. * We can now mark all queues in the burst list as
  708. * belonging to a large burst.
  709. */
  710. hlist_for_each_entry(bfqq_item, &bfqd->burst_list,
  711. burst_list_node)
  712. bfq_mark_bfqq_in_large_burst(bfqq_item);
  713. bfq_mark_bfqq_in_large_burst(bfqq);
  714. /*
  715. * From now on, and until the current burst finishes, any
  716. * new queue being activated shortly after the last queue
  717. * was inserted in the burst can be immediately marked as
  718. * belonging to a large burst. So the burst list is not
  719. * needed any more. Remove it.
  720. */
  721. hlist_for_each_entry_safe(pos, n, &bfqd->burst_list,
  722. burst_list_node)
  723. hlist_del_init(&pos->burst_list_node);
  724. } else /*
  725. * Burst not yet large: add bfqq to the burst list. Do
  726. * not increment the ref counter for bfqq, because bfqq
  727. * is removed from the burst list before freeing bfqq
  728. * in put_queue.
  729. */
  730. hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list);
  731. }
  732. /*
  733. * If many queues belonging to the same group happen to be created
  734. * shortly after each other, then the processes associated with these
  735. * queues have typically a common goal. In particular, bursts of queue
  736. * creations are usually caused by services or applications that spawn
  737. * many parallel threads/processes. Examples are systemd during boot,
  738. * or git grep. To help these processes get their job done as soon as
  739. * possible, it is usually better to not grant either weight-raising
  740. * or device idling to their queues.
  741. *
  742. * In this comment we describe, firstly, the reasons why this fact
  743. * holds, and, secondly, the next function, which implements the main
  744. * steps needed to properly mark these queues so that they can then be
  745. * treated in a different way.
  746. *
  747. * The above services or applications benefit mostly from a high
  748. * throughput: the quicker the requests of the activated queues are
  749. * cumulatively served, the sooner the target job of these queues gets
  750. * completed. As a consequence, weight-raising any of these queues,
  751. * which also implies idling the device for it, is almost always
  752. * counterproductive. In most cases it just lowers throughput.
  753. *
  754. * On the other hand, a burst of queue creations may be caused also by
  755. * the start of an application that does not consist of a lot of
  756. * parallel I/O-bound threads. In fact, with a complex application,
  757. * several short processes may need to be executed to start-up the
  758. * application. In this respect, to start an application as quickly as
  759. * possible, the best thing to do is in any case to privilege the I/O
  760. * related to the application with respect to all other
  761. * I/O. Therefore, the best strategy to start as quickly as possible
  762. * an application that causes a burst of queue creations is to
  763. * weight-raise all the queues created during the burst. This is the
  764. * exact opposite of the best strategy for the other type of bursts.
  765. *
  766. * In the end, to take the best action for each of the two cases, the
  767. * two types of bursts need to be distinguished. Fortunately, this
  768. * seems relatively easy, by looking at the sizes of the bursts. In
  769. * particular, we found a threshold such that only bursts with a
  770. * larger size than that threshold are apparently caused by
  771. * services or commands such as systemd or git grep. For brevity,
  772. * hereafter we call just 'large' these bursts. BFQ *does not*
  773. * weight-raise queues whose creation occurs in a large burst. In
  774. * addition, for each of these queues BFQ performs or does not perform
  775. * idling depending on which choice boosts the throughput more. The
  776. * exact choice depends on the device and request pattern at
  777. * hand.
  778. *
  779. * Unfortunately, false positives may occur while an interactive task
  780. * is starting (e.g., an application is being started). The
  781. * consequence is that the queues associated with the task do not
  782. * enjoy weight raising as expected. Fortunately these false positives
  783. * are very rare. They typically occur if some service happens to
  784. * start doing I/O exactly when the interactive task starts.
  785. *
  786. * Turning back to the next function, it implements all the steps
  787. * needed to detect the occurrence of a large burst and to properly
  788. * mark all the queues belonging to it (so that they can then be
  789. * treated in a different way). This goal is achieved by maintaining a
  790. * "burst list" that holds, temporarily, the queues that belong to the
  791. * burst in progress. The list is then used to mark these queues as
  792. * belonging to a large burst if the burst does become large. The main
  793. * steps are the following.
  794. *
  795. * . when the very first queue is created, the queue is inserted into the
  796. * list (as it could be the first queue in a possible burst)
  797. *
  798. * . if the current burst has not yet become large, and a queue Q that does
  799. * not yet belong to the burst is activated shortly after the last time
  800. * at which a new queue entered the burst list, then the function appends
  801. * Q to the burst list
  802. *
  803. * . if, as a consequence of the previous step, the burst size reaches
  804. * the large-burst threshold, then
  805. *
  806. * . all the queues in the burst list are marked as belonging to a
  807. * large burst
  808. *
  809. * . the burst list is deleted; in fact, the burst list already served
  810. * its purpose (keeping temporarily track of the queues in a burst,
  811. * so as to be able to mark them as belonging to a large burst in the
  812. * previous sub-step), and now is not needed any more
  813. *
  814. * . the device enters a large-burst mode
  815. *
  816. * . if a queue Q that does not belong to the burst is created while
  817. * the device is in large-burst mode and shortly after the last time
  818. * at which a queue either entered the burst list or was marked as
  819. * belonging to the current large burst, then Q is immediately marked
  820. * as belonging to a large burst.
  821. *
  822. * . if a queue Q that does not belong to the burst is created a while
  823. * later, i.e., not shortly after, than the last time at which a queue
  824. * either entered the burst list or was marked as belonging to the
  825. * current large burst, then the current burst is deemed as finished and:
  826. *
  827. * . the large-burst mode is reset if set
  828. *
  829. * . the burst list is emptied
  830. *
  831. * . Q is inserted in the burst list, as Q may be the first queue
  832. * in a possible new burst (then the burst list contains just Q
  833. * after this step).
  834. */
  835. static void bfq_handle_burst(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  836. {
  837. /*
  838. * If bfqq is already in the burst list or is part of a large
  839. * burst, or finally has just been split, then there is
  840. * nothing else to do.
  841. */
  842. if (!hlist_unhashed(&bfqq->burst_list_node) ||
  843. bfq_bfqq_in_large_burst(bfqq) ||
  844. time_is_after_eq_jiffies(bfqq->split_time +
  845. msecs_to_jiffies(10)))
  846. return;
  847. /*
  848. * If bfqq's creation happens late enough, or bfqq belongs to
  849. * a different group than the burst group, then the current
  850. * burst is finished, and related data structures must be
  851. * reset.
  852. *
  853. * In this respect, consider the special case where bfqq is
  854. * the very first queue created after BFQ is selected for this
  855. * device. In this case, last_ins_in_burst and
  856. * burst_parent_entity are not yet significant when we get
  857. * here. But it is easy to verify that, whether or not the
  858. * following condition is true, bfqq will end up being
  859. * inserted into the burst list. In particular the list will
  860. * happen to contain only bfqq. And this is exactly what has
  861. * to happen, as bfqq may be the first queue of the first
  862. * burst.
  863. */
  864. if (time_is_before_jiffies(bfqd->last_ins_in_burst +
  865. bfqd->bfq_burst_interval) ||
  866. bfqq->entity.parent != bfqd->burst_parent_entity) {
  867. bfqd->large_burst = false;
  868. bfq_reset_burst_list(bfqd, bfqq);
  869. goto end;
  870. }
  871. /*
  872. * If we get here, then bfqq is being activated shortly after the
  873. * last queue. So, if the current burst is also large, we can mark
  874. * bfqq as belonging to this large burst immediately.
  875. */
  876. if (bfqd->large_burst) {
  877. bfq_mark_bfqq_in_large_burst(bfqq);
  878. goto end;
  879. }
  880. /*
  881. * If we get here, then a large-burst state has not yet been
  882. * reached, but bfqq is being activated shortly after the last
  883. * queue. Then we add bfqq to the burst.
  884. */
  885. bfq_add_to_burst(bfqd, bfqq);
  886. end:
  887. /*
  888. * At this point, bfqq either has been added to the current
  889. * burst or has caused the current burst to terminate and a
  890. * possible new burst to start. In particular, in the second
  891. * case, bfqq has become the first queue in the possible new
  892. * burst. In both cases last_ins_in_burst needs to be moved
  893. * forward.
  894. */
  895. bfqd->last_ins_in_burst = jiffies;
  896. }
  897. static int bfq_bfqq_budget_left(struct bfq_queue *bfqq)
  898. {
  899. struct bfq_entity *entity = &bfqq->entity;
  900. return entity->budget - entity->service;
  901. }
  902. /*
  903. * If enough samples have been computed, return the current max budget
  904. * stored in bfqd, which is dynamically updated according to the
  905. * estimated disk peak rate; otherwise return the default max budget
  906. */
  907. static int bfq_max_budget(struct bfq_data *bfqd)
  908. {
  909. if (bfqd->budgets_assigned < bfq_stats_min_budgets)
  910. return bfq_default_max_budget;
  911. else
  912. return bfqd->bfq_max_budget;
  913. }
  914. /*
  915. * Return min budget, which is a fraction of the current or default
  916. * max budget (trying with 1/32)
  917. */
  918. static int bfq_min_budget(struct bfq_data *bfqd)
  919. {
  920. if (bfqd->budgets_assigned < bfq_stats_min_budgets)
  921. return bfq_default_max_budget / 32;
  922. else
  923. return bfqd->bfq_max_budget / 32;
  924. }
  925. /*
  926. * The next function, invoked after the input queue bfqq switches from
  927. * idle to busy, updates the budget of bfqq. The function also tells
  928. * whether the in-service queue should be expired, by returning
  929. * true. The purpose of expiring the in-service queue is to give bfqq
  930. * the chance to possibly preempt the in-service queue, and the reason
  931. * for preempting the in-service queue is to achieve one of the two
  932. * goals below.
  933. *
  934. * 1. Guarantee to bfqq its reserved bandwidth even if bfqq has
  935. * expired because it has remained idle. In particular, bfqq may have
  936. * expired for one of the following two reasons:
  937. *
  938. * - BFQQE_NO_MORE_REQUESTS bfqq did not enjoy any device idling
  939. * and did not make it to issue a new request before its last
  940. * request was served;
  941. *
  942. * - BFQQE_TOO_IDLE bfqq did enjoy device idling, but did not issue
  943. * a new request before the expiration of the idling-time.
  944. *
  945. * Even if bfqq has expired for one of the above reasons, the process
  946. * associated with the queue may be however issuing requests greedily,
  947. * and thus be sensitive to the bandwidth it receives (bfqq may have
  948. * remained idle for other reasons: CPU high load, bfqq not enjoying
  949. * idling, I/O throttling somewhere in the path from the process to
  950. * the I/O scheduler, ...). But if, after every expiration for one of
  951. * the above two reasons, bfqq has to wait for the service of at least
  952. * one full budget of another queue before being served again, then
  953. * bfqq is likely to get a much lower bandwidth or resource time than
  954. * its reserved ones. To address this issue, two countermeasures need
  955. * to be taken.
  956. *
  957. * First, the budget and the timestamps of bfqq need to be updated in
  958. * a special way on bfqq reactivation: they need to be updated as if
  959. * bfqq did not remain idle and did not expire. In fact, if they are
  960. * computed as if bfqq expired and remained idle until reactivation,
  961. * then the process associated with bfqq is treated as if, instead of
  962. * being greedy, it stopped issuing requests when bfqq remained idle,
  963. * and restarts issuing requests only on this reactivation. In other
  964. * words, the scheduler does not help the process recover the "service
  965. * hole" between bfqq expiration and reactivation. As a consequence,
  966. * the process receives a lower bandwidth than its reserved one. In
  967. * contrast, to recover this hole, the budget must be updated as if
  968. * bfqq was not expired at all before this reactivation, i.e., it must
  969. * be set to the value of the remaining budget when bfqq was
  970. * expired. Along the same line, timestamps need to be assigned the
  971. * value they had the last time bfqq was selected for service, i.e.,
  972. * before last expiration. Thus timestamps need to be back-shifted
  973. * with respect to their normal computation (see [1] for more details
  974. * on this tricky aspect).
  975. *
  976. * Secondly, to allow the process to recover the hole, the in-service
  977. * queue must be expired too, to give bfqq the chance to preempt it
  978. * immediately. In fact, if bfqq has to wait for a full budget of the
  979. * in-service queue to be completed, then it may become impossible to
  980. * let the process recover the hole, even if the back-shifted
  981. * timestamps of bfqq are lower than those of the in-service queue. If
  982. * this happens for most or all of the holes, then the process may not
  983. * receive its reserved bandwidth. In this respect, it is worth noting
  984. * that, being the service of outstanding requests unpreemptible, a
  985. * little fraction of the holes may however be unrecoverable, thereby
  986. * causing a little loss of bandwidth.
  987. *
  988. * The last important point is detecting whether bfqq does need this
  989. * bandwidth recovery. In this respect, the next function deems the
  990. * process associated with bfqq greedy, and thus allows it to recover
  991. * the hole, if: 1) the process is waiting for the arrival of a new
  992. * request (which implies that bfqq expired for one of the above two
  993. * reasons), and 2) such a request has arrived soon. The first
  994. * condition is controlled through the flag non_blocking_wait_rq,
  995. * while the second through the flag arrived_in_time. If both
  996. * conditions hold, then the function computes the budget in the
  997. * above-described special way, and signals that the in-service queue
  998. * should be expired. Timestamp back-shifting is done later in
  999. * __bfq_activate_entity.
  1000. *
  1001. * 2. Reduce latency. Even if timestamps are not backshifted to let
  1002. * the process associated with bfqq recover a service hole, bfqq may
  1003. * however happen to have, after being (re)activated, a lower finish
  1004. * timestamp than the in-service queue. That is, the next budget of
  1005. * bfqq may have to be completed before the one of the in-service
  1006. * queue. If this is the case, then preempting the in-service queue
  1007. * allows this goal to be achieved, apart from the unpreemptible,
  1008. * outstanding requests mentioned above.
  1009. *
  1010. * Unfortunately, regardless of which of the above two goals one wants
  1011. * to achieve, service trees need first to be updated to know whether
  1012. * the in-service queue must be preempted. To have service trees
  1013. * correctly updated, the in-service queue must be expired and
  1014. * rescheduled, and bfqq must be scheduled too. This is one of the
  1015. * most costly operations (in future versions, the scheduling
  1016. * mechanism may be re-designed in such a way to make it possible to
  1017. * know whether preemption is needed without needing to update service
  1018. * trees). In addition, queue preemptions almost always cause random
  1019. * I/O, and thus loss of throughput. Because of these facts, the next
  1020. * function adopts the following simple scheme to avoid both costly
  1021. * operations and too frequent preemptions: it requests the expiration
  1022. * of the in-service queue (unconditionally) only for queues that need
  1023. * to recover a hole, or that either are weight-raised or deserve to
  1024. * be weight-raised.
  1025. */
  1026. static bool bfq_bfqq_update_budg_for_activation(struct bfq_data *bfqd,
  1027. struct bfq_queue *bfqq,
  1028. bool arrived_in_time,
  1029. bool wr_or_deserves_wr)
  1030. {
  1031. struct bfq_entity *entity = &bfqq->entity;
  1032. if (bfq_bfqq_non_blocking_wait_rq(bfqq) && arrived_in_time) {
  1033. /*
  1034. * We do not clear the flag non_blocking_wait_rq here, as
  1035. * the latter is used in bfq_activate_bfqq to signal
  1036. * that timestamps need to be back-shifted (and is
  1037. * cleared right after).
  1038. */
  1039. /*
  1040. * In next assignment we rely on that either
  1041. * entity->service or entity->budget are not updated
  1042. * on expiration if bfqq is empty (see
  1043. * __bfq_bfqq_recalc_budget). Thus both quantities
  1044. * remain unchanged after such an expiration, and the
  1045. * following statement therefore assigns to
  1046. * entity->budget the remaining budget on such an
  1047. * expiration. For clarity, entity->service is not
  1048. * updated on expiration in any case, and, in normal
  1049. * operation, is reset only when bfqq is selected for
  1050. * service (see bfq_get_next_queue).
  1051. */
  1052. entity->budget = min_t(unsigned long,
  1053. bfq_bfqq_budget_left(bfqq),
  1054. bfqq->max_budget);
  1055. return true;
  1056. }
  1057. entity->budget = max_t(unsigned long, bfqq->max_budget,
  1058. bfq_serv_to_charge(bfqq->next_rq, bfqq));
  1059. bfq_clear_bfqq_non_blocking_wait_rq(bfqq);
  1060. return wr_or_deserves_wr;
  1061. }
  1062. static unsigned int bfq_wr_duration(struct bfq_data *bfqd)
  1063. {
  1064. u64 dur;
  1065. if (bfqd->bfq_wr_max_time > 0)
  1066. return bfqd->bfq_wr_max_time;
  1067. dur = bfqd->RT_prod;
  1068. do_div(dur, bfqd->peak_rate);
  1069. /*
  1070. * Limit duration between 3 and 13 seconds. Tests show that
  1071. * higher values than 13 seconds often yield the opposite of
  1072. * the desired result, i.e., worsen responsiveness by letting
  1073. * non-interactive and non-soft-real-time applications
  1074. * preserve weight raising for a too long time interval.
  1075. *
  1076. * On the other end, lower values than 3 seconds make it
  1077. * difficult for most interactive tasks to complete their jobs
  1078. * before weight-raising finishes.
  1079. */
  1080. if (dur > msecs_to_jiffies(13000))
  1081. dur = msecs_to_jiffies(13000);
  1082. else if (dur < msecs_to_jiffies(3000))
  1083. dur = msecs_to_jiffies(3000);
  1084. return dur;
  1085. }
  1086. /*
  1087. * Return the farthest future time instant according to jiffies
  1088. * macros.
  1089. */
  1090. static unsigned long bfq_greatest_from_now(void)
  1091. {
  1092. return jiffies + MAX_JIFFY_OFFSET;
  1093. }
  1094. /*
  1095. * Return the farthest past time instant according to jiffies
  1096. * macros.
  1097. */
  1098. static unsigned long bfq_smallest_from_now(void)
  1099. {
  1100. return jiffies - MAX_JIFFY_OFFSET;
  1101. }
  1102. static void bfq_update_bfqq_wr_on_rq_arrival(struct bfq_data *bfqd,
  1103. struct bfq_queue *bfqq,
  1104. unsigned int old_wr_coeff,
  1105. bool wr_or_deserves_wr,
  1106. bool interactive,
  1107. bool in_burst,
  1108. bool soft_rt)
  1109. {
  1110. if (old_wr_coeff == 1 && wr_or_deserves_wr) {
  1111. /* start a weight-raising period */
  1112. if (interactive) {
  1113. bfqq->wr_coeff = bfqd->bfq_wr_coeff;
  1114. bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
  1115. } else {
  1116. /*
  1117. * No interactive weight raising in progress
  1118. * here: assign minus infinity to
  1119. * wr_start_at_switch_to_srt, to make sure
  1120. * that, at the end of the soft-real-time
  1121. * weight raising periods that is starting
  1122. * now, no interactive weight-raising period
  1123. * may be wrongly considered as still in
  1124. * progress (and thus actually started by
  1125. * mistake).
  1126. */
  1127. bfqq->wr_start_at_switch_to_srt =
  1128. bfq_smallest_from_now();
  1129. bfqq->wr_coeff = bfqd->bfq_wr_coeff *
  1130. BFQ_SOFTRT_WEIGHT_FACTOR;
  1131. bfqq->wr_cur_max_time =
  1132. bfqd->bfq_wr_rt_max_time;
  1133. }
  1134. /*
  1135. * If needed, further reduce budget to make sure it is
  1136. * close to bfqq's backlog, so as to reduce the
  1137. * scheduling-error component due to a too large
  1138. * budget. Do not care about throughput consequences,
  1139. * but only about latency. Finally, do not assign a
  1140. * too small budget either, to avoid increasing
  1141. * latency by causing too frequent expirations.
  1142. */
  1143. bfqq->entity.budget = min_t(unsigned long,
  1144. bfqq->entity.budget,
  1145. 2 * bfq_min_budget(bfqd));
  1146. } else if (old_wr_coeff > 1) {
  1147. if (interactive) { /* update wr coeff and duration */
  1148. bfqq->wr_coeff = bfqd->bfq_wr_coeff;
  1149. bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
  1150. } else if (in_burst)
  1151. bfqq->wr_coeff = 1;
  1152. else if (soft_rt) {
  1153. /*
  1154. * The application is now or still meeting the
  1155. * requirements for being deemed soft rt. We
  1156. * can then correctly and safely (re)charge
  1157. * the weight-raising duration for the
  1158. * application with the weight-raising
  1159. * duration for soft rt applications.
  1160. *
  1161. * In particular, doing this recharge now, i.e.,
  1162. * before the weight-raising period for the
  1163. * application finishes, reduces the probability
  1164. * of the following negative scenario:
  1165. * 1) the weight of a soft rt application is
  1166. * raised at startup (as for any newly
  1167. * created application),
  1168. * 2) since the application is not interactive,
  1169. * at a certain time weight-raising is
  1170. * stopped for the application,
  1171. * 3) at that time the application happens to
  1172. * still have pending requests, and hence
  1173. * is destined to not have a chance to be
  1174. * deemed soft rt before these requests are
  1175. * completed (see the comments to the
  1176. * function bfq_bfqq_softrt_next_start()
  1177. * for details on soft rt detection),
  1178. * 4) these pending requests experience a high
  1179. * latency because the application is not
  1180. * weight-raised while they are pending.
  1181. */
  1182. if (bfqq->wr_cur_max_time !=
  1183. bfqd->bfq_wr_rt_max_time) {
  1184. bfqq->wr_start_at_switch_to_srt =
  1185. bfqq->last_wr_start_finish;
  1186. bfqq->wr_cur_max_time =
  1187. bfqd->bfq_wr_rt_max_time;
  1188. bfqq->wr_coeff = bfqd->bfq_wr_coeff *
  1189. BFQ_SOFTRT_WEIGHT_FACTOR;
  1190. }
  1191. bfqq->last_wr_start_finish = jiffies;
  1192. }
  1193. }
  1194. }
  1195. static bool bfq_bfqq_idle_for_long_time(struct bfq_data *bfqd,
  1196. struct bfq_queue *bfqq)
  1197. {
  1198. return bfqq->dispatched == 0 &&
  1199. time_is_before_jiffies(
  1200. bfqq->budget_timeout +
  1201. bfqd->bfq_wr_min_idle_time);
  1202. }
  1203. static void bfq_bfqq_handle_idle_busy_switch(struct bfq_data *bfqd,
  1204. struct bfq_queue *bfqq,
  1205. int old_wr_coeff,
  1206. struct request *rq,
  1207. bool *interactive)
  1208. {
  1209. bool soft_rt, in_burst, wr_or_deserves_wr,
  1210. bfqq_wants_to_preempt,
  1211. idle_for_long_time = bfq_bfqq_idle_for_long_time(bfqd, bfqq),
  1212. /*
  1213. * See the comments on
  1214. * bfq_bfqq_update_budg_for_activation for
  1215. * details on the usage of the next variable.
  1216. */
  1217. arrived_in_time = ktime_get_ns() <=
  1218. bfqq->ttime.last_end_request +
  1219. bfqd->bfq_slice_idle * 3;
  1220. bfqg_stats_update_io_add(bfqq_group(RQ_BFQQ(rq)), bfqq, rq->cmd_flags);
  1221. /*
  1222. * bfqq deserves to be weight-raised if:
  1223. * - it is sync,
  1224. * - it does not belong to a large burst,
  1225. * - it has been idle for enough time or is soft real-time,
  1226. * - is linked to a bfq_io_cq (it is not shared in any sense).
  1227. */
  1228. in_burst = bfq_bfqq_in_large_burst(bfqq);
  1229. soft_rt = bfqd->bfq_wr_max_softrt_rate > 0 &&
  1230. !in_burst &&
  1231. time_is_before_jiffies(bfqq->soft_rt_next_start);
  1232. *interactive = !in_burst && idle_for_long_time;
  1233. wr_or_deserves_wr = bfqd->low_latency &&
  1234. (bfqq->wr_coeff > 1 ||
  1235. (bfq_bfqq_sync(bfqq) &&
  1236. bfqq->bic && (*interactive || soft_rt)));
  1237. /*
  1238. * Using the last flag, update budget and check whether bfqq
  1239. * may want to preempt the in-service queue.
  1240. */
  1241. bfqq_wants_to_preempt =
  1242. bfq_bfqq_update_budg_for_activation(bfqd, bfqq,
  1243. arrived_in_time,
  1244. wr_or_deserves_wr);
  1245. /*
  1246. * If bfqq happened to be activated in a burst, but has been
  1247. * idle for much more than an interactive queue, then we
  1248. * assume that, in the overall I/O initiated in the burst, the
  1249. * I/O associated with bfqq is finished. So bfqq does not need
  1250. * to be treated as a queue belonging to a burst
  1251. * anymore. Accordingly, we reset bfqq's in_large_burst flag
  1252. * if set, and remove bfqq from the burst list if it's
  1253. * there. We do not decrement burst_size, because the fact
  1254. * that bfqq does not need to belong to the burst list any
  1255. * more does not invalidate the fact that bfqq was created in
  1256. * a burst.
  1257. */
  1258. if (likely(!bfq_bfqq_just_created(bfqq)) &&
  1259. idle_for_long_time &&
  1260. time_is_before_jiffies(
  1261. bfqq->budget_timeout +
  1262. msecs_to_jiffies(10000))) {
  1263. hlist_del_init(&bfqq->burst_list_node);
  1264. bfq_clear_bfqq_in_large_burst(bfqq);
  1265. }
  1266. bfq_clear_bfqq_just_created(bfqq);
  1267. if (!bfq_bfqq_IO_bound(bfqq)) {
  1268. if (arrived_in_time) {
  1269. bfqq->requests_within_timer++;
  1270. if (bfqq->requests_within_timer >=
  1271. bfqd->bfq_requests_within_timer)
  1272. bfq_mark_bfqq_IO_bound(bfqq);
  1273. } else
  1274. bfqq->requests_within_timer = 0;
  1275. }
  1276. if (bfqd->low_latency) {
  1277. if (unlikely(time_is_after_jiffies(bfqq->split_time)))
  1278. /* wraparound */
  1279. bfqq->split_time =
  1280. jiffies - bfqd->bfq_wr_min_idle_time - 1;
  1281. if (time_is_before_jiffies(bfqq->split_time +
  1282. bfqd->bfq_wr_min_idle_time)) {
  1283. bfq_update_bfqq_wr_on_rq_arrival(bfqd, bfqq,
  1284. old_wr_coeff,
  1285. wr_or_deserves_wr,
  1286. *interactive,
  1287. in_burst,
  1288. soft_rt);
  1289. if (old_wr_coeff != bfqq->wr_coeff)
  1290. bfqq->entity.prio_changed = 1;
  1291. }
  1292. }
  1293. bfqq->last_idle_bklogged = jiffies;
  1294. bfqq->service_from_backlogged = 0;
  1295. bfq_clear_bfqq_softrt_update(bfqq);
  1296. bfq_add_bfqq_busy(bfqd, bfqq);
  1297. /*
  1298. * Expire in-service queue only if preemption may be needed
  1299. * for guarantees. In this respect, the function
  1300. * next_queue_may_preempt just checks a simple, necessary
  1301. * condition, and not a sufficient condition based on
  1302. * timestamps. In fact, for the latter condition to be
  1303. * evaluated, timestamps would need first to be updated, and
  1304. * this operation is quite costly (see the comments on the
  1305. * function bfq_bfqq_update_budg_for_activation).
  1306. */
  1307. if (bfqd->in_service_queue && bfqq_wants_to_preempt &&
  1308. bfqd->in_service_queue->wr_coeff < bfqq->wr_coeff &&
  1309. next_queue_may_preempt(bfqd))
  1310. bfq_bfqq_expire(bfqd, bfqd->in_service_queue,
  1311. false, BFQQE_PREEMPTED);
  1312. }
  1313. static void bfq_add_request(struct request *rq)
  1314. {
  1315. struct bfq_queue *bfqq = RQ_BFQQ(rq);
  1316. struct bfq_data *bfqd = bfqq->bfqd;
  1317. struct request *next_rq, *prev;
  1318. unsigned int old_wr_coeff = bfqq->wr_coeff;
  1319. bool interactive = false;
  1320. bfq_log_bfqq(bfqd, bfqq, "add_request %d", rq_is_sync(rq));
  1321. bfqq->queued[rq_is_sync(rq)]++;
  1322. bfqd->queued++;
  1323. elv_rb_add(&bfqq->sort_list, rq);
  1324. /*
  1325. * Check if this request is a better next-serve candidate.
  1326. */
  1327. prev = bfqq->next_rq;
  1328. next_rq = bfq_choose_req(bfqd, bfqq->next_rq, rq, bfqd->last_position);
  1329. bfqq->next_rq = next_rq;
  1330. /*
  1331. * Adjust priority tree position, if next_rq changes.
  1332. */
  1333. if (prev != bfqq->next_rq)
  1334. bfq_pos_tree_add_move(bfqd, bfqq);
  1335. if (!bfq_bfqq_busy(bfqq)) /* switching to busy ... */
  1336. bfq_bfqq_handle_idle_busy_switch(bfqd, bfqq, old_wr_coeff,
  1337. rq, &interactive);
  1338. else {
  1339. if (bfqd->low_latency && old_wr_coeff == 1 && !rq_is_sync(rq) &&
  1340. time_is_before_jiffies(
  1341. bfqq->last_wr_start_finish +
  1342. bfqd->bfq_wr_min_inter_arr_async)) {
  1343. bfqq->wr_coeff = bfqd->bfq_wr_coeff;
  1344. bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
  1345. bfqd->wr_busy_queues++;
  1346. bfqq->entity.prio_changed = 1;
  1347. }
  1348. if (prev != bfqq->next_rq)
  1349. bfq_updated_next_req(bfqd, bfqq);
  1350. }
  1351. /*
  1352. * Assign jiffies to last_wr_start_finish in the following
  1353. * cases:
  1354. *
  1355. * . if bfqq is not going to be weight-raised, because, for
  1356. * non weight-raised queues, last_wr_start_finish stores the
  1357. * arrival time of the last request; as of now, this piece
  1358. * of information is used only for deciding whether to
  1359. * weight-raise async queues
  1360. *
  1361. * . if bfqq is not weight-raised, because, if bfqq is now
  1362. * switching to weight-raised, then last_wr_start_finish
  1363. * stores the time when weight-raising starts
  1364. *
  1365. * . if bfqq is interactive, because, regardless of whether
  1366. * bfqq is currently weight-raised, the weight-raising
  1367. * period must start or restart (this case is considered
  1368. * separately because it is not detected by the above
  1369. * conditions, if bfqq is already weight-raised)
  1370. *
  1371. * last_wr_start_finish has to be updated also if bfqq is soft
  1372. * real-time, because the weight-raising period is constantly
  1373. * restarted on idle-to-busy transitions for these queues, but
  1374. * this is already done in bfq_bfqq_handle_idle_busy_switch if
  1375. * needed.
  1376. */
  1377. if (bfqd->low_latency &&
  1378. (old_wr_coeff == 1 || bfqq->wr_coeff == 1 || interactive))
  1379. bfqq->last_wr_start_finish = jiffies;
  1380. }
  1381. static struct request *bfq_find_rq_fmerge(struct bfq_data *bfqd,
  1382. struct bio *bio,
  1383. struct request_queue *q)
  1384. {
  1385. struct bfq_queue *bfqq = bfqd->bio_bfqq;
  1386. if (bfqq)
  1387. return elv_rb_find(&bfqq->sort_list, bio_end_sector(bio));
  1388. return NULL;
  1389. }
  1390. static sector_t get_sdist(sector_t last_pos, struct request *rq)
  1391. {
  1392. if (last_pos)
  1393. return abs(blk_rq_pos(rq) - last_pos);
  1394. return 0;
  1395. }
  1396. #if 0 /* Still not clear if we can do without next two functions */
  1397. static void bfq_activate_request(struct request_queue *q, struct request *rq)
  1398. {
  1399. struct bfq_data *bfqd = q->elevator->elevator_data;
  1400. bfqd->rq_in_driver++;
  1401. }
  1402. static void bfq_deactivate_request(struct request_queue *q, struct request *rq)
  1403. {
  1404. struct bfq_data *bfqd = q->elevator->elevator_data;
  1405. bfqd->rq_in_driver--;
  1406. }
  1407. #endif
  1408. static void bfq_remove_request(struct request_queue *q,
  1409. struct request *rq)
  1410. {
  1411. struct bfq_queue *bfqq = RQ_BFQQ(rq);
  1412. struct bfq_data *bfqd = bfqq->bfqd;
  1413. const int sync = rq_is_sync(rq);
  1414. if (bfqq->next_rq == rq) {
  1415. bfqq->next_rq = bfq_find_next_rq(bfqd, bfqq, rq);
  1416. bfq_updated_next_req(bfqd, bfqq);
  1417. }
  1418. if (rq->queuelist.prev != &rq->queuelist)
  1419. list_del_init(&rq->queuelist);
  1420. bfqq->queued[sync]--;
  1421. bfqd->queued--;
  1422. elv_rb_del(&bfqq->sort_list, rq);
  1423. elv_rqhash_del(q, rq);
  1424. if (q->last_merge == rq)
  1425. q->last_merge = NULL;
  1426. if (RB_EMPTY_ROOT(&bfqq->sort_list)) {
  1427. bfqq->next_rq = NULL;
  1428. if (bfq_bfqq_busy(bfqq) && bfqq != bfqd->in_service_queue) {
  1429. bfq_del_bfqq_busy(bfqd, bfqq, false);
  1430. /*
  1431. * bfqq emptied. In normal operation, when
  1432. * bfqq is empty, bfqq->entity.service and
  1433. * bfqq->entity.budget must contain,
  1434. * respectively, the service received and the
  1435. * budget used last time bfqq emptied. These
  1436. * facts do not hold in this case, as at least
  1437. * this last removal occurred while bfqq is
  1438. * not in service. To avoid inconsistencies,
  1439. * reset both bfqq->entity.service and
  1440. * bfqq->entity.budget, if bfqq has still a
  1441. * process that may issue I/O requests to it.
  1442. */
  1443. bfqq->entity.budget = bfqq->entity.service = 0;
  1444. }
  1445. /*
  1446. * Remove queue from request-position tree as it is empty.
  1447. */
  1448. if (bfqq->pos_root) {
  1449. rb_erase(&bfqq->pos_node, bfqq->pos_root);
  1450. bfqq->pos_root = NULL;
  1451. }
  1452. }
  1453. if (rq->cmd_flags & REQ_META)
  1454. bfqq->meta_pending--;
  1455. bfqg_stats_update_io_remove(bfqq_group(bfqq), rq->cmd_flags);
  1456. }
  1457. static bool bfq_bio_merge(struct blk_mq_hw_ctx *hctx, struct bio *bio)
  1458. {
  1459. struct request_queue *q = hctx->queue;
  1460. struct bfq_data *bfqd = q->elevator->elevator_data;
  1461. struct request *free = NULL;
  1462. /*
  1463. * bfq_bic_lookup grabs the queue_lock: invoke it now and
  1464. * store its return value for later use, to avoid nesting
  1465. * queue_lock inside the bfqd->lock. We assume that the bic
  1466. * returned by bfq_bic_lookup does not go away before
  1467. * bfqd->lock is taken.
  1468. */
  1469. struct bfq_io_cq *bic = bfq_bic_lookup(bfqd, current->io_context, q);
  1470. bool ret;
  1471. spin_lock_irq(&bfqd->lock);
  1472. if (bic)
  1473. bfqd->bio_bfqq = bic_to_bfqq(bic, op_is_sync(bio->bi_opf));
  1474. else
  1475. bfqd->bio_bfqq = NULL;
  1476. bfqd->bio_bic = bic;
  1477. ret = blk_mq_sched_try_merge(q, bio, &free);
  1478. if (free)
  1479. blk_mq_free_request(free);
  1480. spin_unlock_irq(&bfqd->lock);
  1481. return ret;
  1482. }
  1483. static int bfq_request_merge(struct request_queue *q, struct request **req,
  1484. struct bio *bio)
  1485. {
  1486. struct bfq_data *bfqd = q->elevator->elevator_data;
  1487. struct request *__rq;
  1488. __rq = bfq_find_rq_fmerge(bfqd, bio, q);
  1489. if (__rq && elv_bio_merge_ok(__rq, bio)) {
  1490. *req = __rq;
  1491. return ELEVATOR_FRONT_MERGE;
  1492. }
  1493. return ELEVATOR_NO_MERGE;
  1494. }
  1495. static void bfq_request_merged(struct request_queue *q, struct request *req,
  1496. enum elv_merge type)
  1497. {
  1498. if (type == ELEVATOR_FRONT_MERGE &&
  1499. rb_prev(&req->rb_node) &&
  1500. blk_rq_pos(req) <
  1501. blk_rq_pos(container_of(rb_prev(&req->rb_node),
  1502. struct request, rb_node))) {
  1503. struct bfq_queue *bfqq = RQ_BFQQ(req);
  1504. struct bfq_data *bfqd = bfqq->bfqd;
  1505. struct request *prev, *next_rq;
  1506. /* Reposition request in its sort_list */
  1507. elv_rb_del(&bfqq->sort_list, req);
  1508. elv_rb_add(&bfqq->sort_list, req);
  1509. /* Choose next request to be served for bfqq */
  1510. prev = bfqq->next_rq;
  1511. next_rq = bfq_choose_req(bfqd, bfqq->next_rq, req,
  1512. bfqd->last_position);
  1513. bfqq->next_rq = next_rq;
  1514. /*
  1515. * If next_rq changes, update both the queue's budget to
  1516. * fit the new request and the queue's position in its
  1517. * rq_pos_tree.
  1518. */
  1519. if (prev != bfqq->next_rq) {
  1520. bfq_updated_next_req(bfqd, bfqq);
  1521. bfq_pos_tree_add_move(bfqd, bfqq);
  1522. }
  1523. }
  1524. }
  1525. static void bfq_requests_merged(struct request_queue *q, struct request *rq,
  1526. struct request *next)
  1527. {
  1528. struct bfq_queue *bfqq = RQ_BFQQ(rq), *next_bfqq = RQ_BFQQ(next);
  1529. if (!RB_EMPTY_NODE(&rq->rb_node))
  1530. goto end;
  1531. /*
  1532. * If next and rq belong to the same bfq_queue and next is older
  1533. * than rq, then reposition rq in the fifo (by substituting next
  1534. * with rq). Otherwise, if next and rq belong to different
  1535. * bfq_queues, never reposition rq: in fact, we would have to
  1536. * reposition it with respect to next's position in its own fifo,
  1537. * which would most certainly be too expensive with respect to
  1538. * the benefits.
  1539. */
  1540. if (bfqq == next_bfqq &&
  1541. !list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
  1542. next->fifo_time < rq->fifo_time) {
  1543. list_del_init(&rq->queuelist);
  1544. list_replace_init(&next->queuelist, &rq->queuelist);
  1545. rq->fifo_time = next->fifo_time;
  1546. }
  1547. if (bfqq->next_rq == next)
  1548. bfqq->next_rq = rq;
  1549. bfq_remove_request(q, next);
  1550. end:
  1551. bfqg_stats_update_io_merged(bfqq_group(bfqq), next->cmd_flags);
  1552. }
  1553. /* Must be called with bfqq != NULL */
  1554. static void bfq_bfqq_end_wr(struct bfq_queue *bfqq)
  1555. {
  1556. if (bfq_bfqq_busy(bfqq))
  1557. bfqq->bfqd->wr_busy_queues--;
  1558. bfqq->wr_coeff = 1;
  1559. bfqq->wr_cur_max_time = 0;
  1560. bfqq->last_wr_start_finish = jiffies;
  1561. /*
  1562. * Trigger a weight change on the next invocation of
  1563. * __bfq_entity_update_weight_prio.
  1564. */
  1565. bfqq->entity.prio_changed = 1;
  1566. }
  1567. void bfq_end_wr_async_queues(struct bfq_data *bfqd,
  1568. struct bfq_group *bfqg)
  1569. {
  1570. int i, j;
  1571. for (i = 0; i < 2; i++)
  1572. for (j = 0; j < IOPRIO_BE_NR; j++)
  1573. if (bfqg->async_bfqq[i][j])
  1574. bfq_bfqq_end_wr(bfqg->async_bfqq[i][j]);
  1575. if (bfqg->async_idle_bfqq)
  1576. bfq_bfqq_end_wr(bfqg->async_idle_bfqq);
  1577. }
  1578. static void bfq_end_wr(struct bfq_data *bfqd)
  1579. {
  1580. struct bfq_queue *bfqq;
  1581. spin_lock_irq(&bfqd->lock);
  1582. list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list)
  1583. bfq_bfqq_end_wr(bfqq);
  1584. list_for_each_entry(bfqq, &bfqd->idle_list, bfqq_list)
  1585. bfq_bfqq_end_wr(bfqq);
  1586. bfq_end_wr_async(bfqd);
  1587. spin_unlock_irq(&bfqd->lock);
  1588. }
  1589. static sector_t bfq_io_struct_pos(void *io_struct, bool request)
  1590. {
  1591. if (request)
  1592. return blk_rq_pos(io_struct);
  1593. else
  1594. return ((struct bio *)io_struct)->bi_iter.bi_sector;
  1595. }
  1596. static int bfq_rq_close_to_sector(void *io_struct, bool request,
  1597. sector_t sector)
  1598. {
  1599. return abs(bfq_io_struct_pos(io_struct, request) - sector) <=
  1600. BFQQ_CLOSE_THR;
  1601. }
  1602. static struct bfq_queue *bfqq_find_close(struct bfq_data *bfqd,
  1603. struct bfq_queue *bfqq,
  1604. sector_t sector)
  1605. {
  1606. struct rb_root *root = &bfq_bfqq_to_bfqg(bfqq)->rq_pos_tree;
  1607. struct rb_node *parent, *node;
  1608. struct bfq_queue *__bfqq;
  1609. if (RB_EMPTY_ROOT(root))
  1610. return NULL;
  1611. /*
  1612. * First, if we find a request starting at the end of the last
  1613. * request, choose it.
  1614. */
  1615. __bfqq = bfq_rq_pos_tree_lookup(bfqd, root, sector, &parent, NULL);
  1616. if (__bfqq)
  1617. return __bfqq;
  1618. /*
  1619. * If the exact sector wasn't found, the parent of the NULL leaf
  1620. * will contain the closest sector (rq_pos_tree sorted by
  1621. * next_request position).
  1622. */
  1623. __bfqq = rb_entry(parent, struct bfq_queue, pos_node);
  1624. if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector))
  1625. return __bfqq;
  1626. if (blk_rq_pos(__bfqq->next_rq) < sector)
  1627. node = rb_next(&__bfqq->pos_node);
  1628. else
  1629. node = rb_prev(&__bfqq->pos_node);
  1630. if (!node)
  1631. return NULL;
  1632. __bfqq = rb_entry(node, struct bfq_queue, pos_node);
  1633. if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector))
  1634. return __bfqq;
  1635. return NULL;
  1636. }
  1637. static struct bfq_queue *bfq_find_close_cooperator(struct bfq_data *bfqd,
  1638. struct bfq_queue *cur_bfqq,
  1639. sector_t sector)
  1640. {
  1641. struct bfq_queue *bfqq;
  1642. /*
  1643. * We shall notice if some of the queues are cooperating,
  1644. * e.g., working closely on the same area of the device. In
  1645. * that case, we can group them together and: 1) don't waste
  1646. * time idling, and 2) serve the union of their requests in
  1647. * the best possible order for throughput.
  1648. */
  1649. bfqq = bfqq_find_close(bfqd, cur_bfqq, sector);
  1650. if (!bfqq || bfqq == cur_bfqq)
  1651. return NULL;
  1652. return bfqq;
  1653. }
  1654. static struct bfq_queue *
  1655. bfq_setup_merge(struct bfq_queue *bfqq, struct bfq_queue *new_bfqq)
  1656. {
  1657. int process_refs, new_process_refs;
  1658. struct bfq_queue *__bfqq;
  1659. /*
  1660. * If there are no process references on the new_bfqq, then it is
  1661. * unsafe to follow the ->new_bfqq chain as other bfqq's in the chain
  1662. * may have dropped their last reference (not just their last process
  1663. * reference).
  1664. */
  1665. if (!bfqq_process_refs(new_bfqq))
  1666. return NULL;
  1667. /* Avoid a circular list and skip interim queue merges. */
  1668. while ((__bfqq = new_bfqq->new_bfqq)) {
  1669. if (__bfqq == bfqq)
  1670. return NULL;
  1671. new_bfqq = __bfqq;
  1672. }
  1673. process_refs = bfqq_process_refs(bfqq);
  1674. new_process_refs = bfqq_process_refs(new_bfqq);
  1675. /*
  1676. * If the process for the bfqq has gone away, there is no
  1677. * sense in merging the queues.
  1678. */
  1679. if (process_refs == 0 || new_process_refs == 0)
  1680. return NULL;
  1681. bfq_log_bfqq(bfqq->bfqd, bfqq, "scheduling merge with queue %d",
  1682. new_bfqq->pid);
  1683. /*
  1684. * Merging is just a redirection: the requests of the process
  1685. * owning one of the two queues are redirected to the other queue.
  1686. * The latter queue, in its turn, is set as shared if this is the
  1687. * first time that the requests of some process are redirected to
  1688. * it.
  1689. *
  1690. * We redirect bfqq to new_bfqq and not the opposite, because
  1691. * we are in the context of the process owning bfqq, thus we
  1692. * have the io_cq of this process. So we can immediately
  1693. * configure this io_cq to redirect the requests of the
  1694. * process to new_bfqq. In contrast, the io_cq of new_bfqq is
  1695. * not available any more (new_bfqq->bic == NULL).
  1696. *
  1697. * Anyway, even in case new_bfqq coincides with the in-service
  1698. * queue, redirecting requests the in-service queue is the
  1699. * best option, as we feed the in-service queue with new
  1700. * requests close to the last request served and, by doing so,
  1701. * are likely to increase the throughput.
  1702. */
  1703. bfqq->new_bfqq = new_bfqq;
  1704. new_bfqq->ref += process_refs;
  1705. return new_bfqq;
  1706. }
  1707. static bool bfq_may_be_close_cooperator(struct bfq_queue *bfqq,
  1708. struct bfq_queue *new_bfqq)
  1709. {
  1710. if (bfq_class_idle(bfqq) || bfq_class_idle(new_bfqq) ||
  1711. (bfqq->ioprio_class != new_bfqq->ioprio_class))
  1712. return false;
  1713. /*
  1714. * If either of the queues has already been detected as seeky,
  1715. * then merging it with the other queue is unlikely to lead to
  1716. * sequential I/O.
  1717. */
  1718. if (BFQQ_SEEKY(bfqq) || BFQQ_SEEKY(new_bfqq))
  1719. return false;
  1720. /*
  1721. * Interleaved I/O is known to be done by (some) applications
  1722. * only for reads, so it does not make sense to merge async
  1723. * queues.
  1724. */
  1725. if (!bfq_bfqq_sync(bfqq) || !bfq_bfqq_sync(new_bfqq))
  1726. return false;
  1727. return true;
  1728. }
  1729. /*
  1730. * If this function returns true, then bfqq cannot be merged. The idea
  1731. * is that true cooperation happens very early after processes start
  1732. * to do I/O. Usually, late cooperations are just accidental false
  1733. * positives. In case bfqq is weight-raised, such false positives
  1734. * would evidently degrade latency guarantees for bfqq.
  1735. */
  1736. static bool wr_from_too_long(struct bfq_queue *bfqq)
  1737. {
  1738. return bfqq->wr_coeff > 1 &&
  1739. time_is_before_jiffies(bfqq->last_wr_start_finish +
  1740. msecs_to_jiffies(100));
  1741. }
  1742. /*
  1743. * Attempt to schedule a merge of bfqq with the currently in-service
  1744. * queue or with a close queue among the scheduled queues. Return
  1745. * NULL if no merge was scheduled, a pointer to the shared bfq_queue
  1746. * structure otherwise.
  1747. *
  1748. * The OOM queue is not allowed to participate to cooperation: in fact, since
  1749. * the requests temporarily redirected to the OOM queue could be redirected
  1750. * again to dedicated queues at any time, the state needed to correctly
  1751. * handle merging with the OOM queue would be quite complex and expensive
  1752. * to maintain. Besides, in such a critical condition as an out of memory,
  1753. * the benefits of queue merging may be little relevant, or even negligible.
  1754. *
  1755. * Weight-raised queues can be merged only if their weight-raising
  1756. * period has just started. In fact cooperating processes are usually
  1757. * started together. Thus, with this filter we avoid false positives
  1758. * that would jeopardize low-latency guarantees.
  1759. *
  1760. * WARNING: queue merging may impair fairness among non-weight raised
  1761. * queues, for at least two reasons: 1) the original weight of a
  1762. * merged queue may change during the merged state, 2) even being the
  1763. * weight the same, a merged queue may be bloated with many more
  1764. * requests than the ones produced by its originally-associated
  1765. * process.
  1766. */
  1767. static struct bfq_queue *
  1768. bfq_setup_cooperator(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  1769. void *io_struct, bool request)
  1770. {
  1771. struct bfq_queue *in_service_bfqq, *new_bfqq;
  1772. if (bfqq->new_bfqq)
  1773. return bfqq->new_bfqq;
  1774. if (!io_struct ||
  1775. wr_from_too_long(bfqq) ||
  1776. unlikely(bfqq == &bfqd->oom_bfqq))
  1777. return NULL;
  1778. /* If there is only one backlogged queue, don't search. */
  1779. if (bfqd->busy_queues == 1)
  1780. return NULL;
  1781. in_service_bfqq = bfqd->in_service_queue;
  1782. if (!in_service_bfqq || in_service_bfqq == bfqq
  1783. || wr_from_too_long(in_service_bfqq) ||
  1784. unlikely(in_service_bfqq == &bfqd->oom_bfqq))
  1785. goto check_scheduled;
  1786. if (bfq_rq_close_to_sector(io_struct, request, bfqd->last_position) &&
  1787. bfqq->entity.parent == in_service_bfqq->entity.parent &&
  1788. bfq_may_be_close_cooperator(bfqq, in_service_bfqq)) {
  1789. new_bfqq = bfq_setup_merge(bfqq, in_service_bfqq);
  1790. if (new_bfqq)
  1791. return new_bfqq;
  1792. }
  1793. /*
  1794. * Check whether there is a cooperator among currently scheduled
  1795. * queues. The only thing we need is that the bio/request is not
  1796. * NULL, as we need it to establish whether a cooperator exists.
  1797. */
  1798. check_scheduled:
  1799. new_bfqq = bfq_find_close_cooperator(bfqd, bfqq,
  1800. bfq_io_struct_pos(io_struct, request));
  1801. if (new_bfqq && !wr_from_too_long(new_bfqq) &&
  1802. likely(new_bfqq != &bfqd->oom_bfqq) &&
  1803. bfq_may_be_close_cooperator(bfqq, new_bfqq))
  1804. return bfq_setup_merge(bfqq, new_bfqq);
  1805. return NULL;
  1806. }
  1807. static void bfq_bfqq_save_state(struct bfq_queue *bfqq)
  1808. {
  1809. struct bfq_io_cq *bic = bfqq->bic;
  1810. /*
  1811. * If !bfqq->bic, the queue is already shared or its requests
  1812. * have already been redirected to a shared queue; both idle window
  1813. * and weight raising state have already been saved. Do nothing.
  1814. */
  1815. if (!bic)
  1816. return;
  1817. bic->saved_ttime = bfqq->ttime;
  1818. bic->saved_has_short_ttime = bfq_bfqq_has_short_ttime(bfqq);
  1819. bic->saved_IO_bound = bfq_bfqq_IO_bound(bfqq);
  1820. bic->saved_in_large_burst = bfq_bfqq_in_large_burst(bfqq);
  1821. bic->was_in_burst_list = !hlist_unhashed(&bfqq->burst_list_node);
  1822. bic->saved_wr_coeff = bfqq->wr_coeff;
  1823. bic->saved_wr_start_at_switch_to_srt = bfqq->wr_start_at_switch_to_srt;
  1824. bic->saved_last_wr_start_finish = bfqq->last_wr_start_finish;
  1825. bic->saved_wr_cur_max_time = bfqq->wr_cur_max_time;
  1826. }
  1827. static void
  1828. bfq_merge_bfqqs(struct bfq_data *bfqd, struct bfq_io_cq *bic,
  1829. struct bfq_queue *bfqq, struct bfq_queue *new_bfqq)
  1830. {
  1831. bfq_log_bfqq(bfqd, bfqq, "merging with queue %lu",
  1832. (unsigned long)new_bfqq->pid);
  1833. /* Save weight raising and idle window of the merged queues */
  1834. bfq_bfqq_save_state(bfqq);
  1835. bfq_bfqq_save_state(new_bfqq);
  1836. if (bfq_bfqq_IO_bound(bfqq))
  1837. bfq_mark_bfqq_IO_bound(new_bfqq);
  1838. bfq_clear_bfqq_IO_bound(bfqq);
  1839. /*
  1840. * If bfqq is weight-raised, then let new_bfqq inherit
  1841. * weight-raising. To reduce false positives, neglect the case
  1842. * where bfqq has just been created, but has not yet made it
  1843. * to be weight-raised (which may happen because EQM may merge
  1844. * bfqq even before bfq_add_request is executed for the first
  1845. * time for bfqq). Handling this case would however be very
  1846. * easy, thanks to the flag just_created.
  1847. */
  1848. if (new_bfqq->wr_coeff == 1 && bfqq->wr_coeff > 1) {
  1849. new_bfqq->wr_coeff = bfqq->wr_coeff;
  1850. new_bfqq->wr_cur_max_time = bfqq->wr_cur_max_time;
  1851. new_bfqq->last_wr_start_finish = bfqq->last_wr_start_finish;
  1852. new_bfqq->wr_start_at_switch_to_srt =
  1853. bfqq->wr_start_at_switch_to_srt;
  1854. if (bfq_bfqq_busy(new_bfqq))
  1855. bfqd->wr_busy_queues++;
  1856. new_bfqq->entity.prio_changed = 1;
  1857. }
  1858. if (bfqq->wr_coeff > 1) { /* bfqq has given its wr to new_bfqq */
  1859. bfqq->wr_coeff = 1;
  1860. bfqq->entity.prio_changed = 1;
  1861. if (bfq_bfqq_busy(bfqq))
  1862. bfqd->wr_busy_queues--;
  1863. }
  1864. bfq_log_bfqq(bfqd, new_bfqq, "merge_bfqqs: wr_busy %d",
  1865. bfqd->wr_busy_queues);
  1866. /*
  1867. * Merge queues (that is, let bic redirect its requests to new_bfqq)
  1868. */
  1869. bic_set_bfqq(bic, new_bfqq, 1);
  1870. bfq_mark_bfqq_coop(new_bfqq);
  1871. /*
  1872. * new_bfqq now belongs to at least two bics (it is a shared queue):
  1873. * set new_bfqq->bic to NULL. bfqq either:
  1874. * - does not belong to any bic any more, and hence bfqq->bic must
  1875. * be set to NULL, or
  1876. * - is a queue whose owning bics have already been redirected to a
  1877. * different queue, hence the queue is destined to not belong to
  1878. * any bic soon and bfqq->bic is already NULL (therefore the next
  1879. * assignment causes no harm).
  1880. */
  1881. new_bfqq->bic = NULL;
  1882. bfqq->bic = NULL;
  1883. /* release process reference to bfqq */
  1884. bfq_put_queue(bfqq);
  1885. }
  1886. static bool bfq_allow_bio_merge(struct request_queue *q, struct request *rq,
  1887. struct bio *bio)
  1888. {
  1889. struct bfq_data *bfqd = q->elevator->elevator_data;
  1890. bool is_sync = op_is_sync(bio->bi_opf);
  1891. struct bfq_queue *bfqq = bfqd->bio_bfqq, *new_bfqq;
  1892. /*
  1893. * Disallow merge of a sync bio into an async request.
  1894. */
  1895. if (is_sync && !rq_is_sync(rq))
  1896. return false;
  1897. /*
  1898. * Lookup the bfqq that this bio will be queued with. Allow
  1899. * merge only if rq is queued there.
  1900. */
  1901. if (!bfqq)
  1902. return false;
  1903. /*
  1904. * We take advantage of this function to perform an early merge
  1905. * of the queues of possible cooperating processes.
  1906. */
  1907. new_bfqq = bfq_setup_cooperator(bfqd, bfqq, bio, false);
  1908. if (new_bfqq) {
  1909. /*
  1910. * bic still points to bfqq, then it has not yet been
  1911. * redirected to some other bfq_queue, and a queue
  1912. * merge beween bfqq and new_bfqq can be safely
  1913. * fulfillled, i.e., bic can be redirected to new_bfqq
  1914. * and bfqq can be put.
  1915. */
  1916. bfq_merge_bfqqs(bfqd, bfqd->bio_bic, bfqq,
  1917. new_bfqq);
  1918. /*
  1919. * If we get here, bio will be queued into new_queue,
  1920. * so use new_bfqq to decide whether bio and rq can be
  1921. * merged.
  1922. */
  1923. bfqq = new_bfqq;
  1924. /*
  1925. * Change also bqfd->bio_bfqq, as
  1926. * bfqd->bio_bic now points to new_bfqq, and
  1927. * this function may be invoked again (and then may
  1928. * use again bqfd->bio_bfqq).
  1929. */
  1930. bfqd->bio_bfqq = bfqq;
  1931. }
  1932. return bfqq == RQ_BFQQ(rq);
  1933. }
  1934. /*
  1935. * Set the maximum time for the in-service queue to consume its
  1936. * budget. This prevents seeky processes from lowering the throughput.
  1937. * In practice, a time-slice service scheme is used with seeky
  1938. * processes.
  1939. */
  1940. static void bfq_set_budget_timeout(struct bfq_data *bfqd,
  1941. struct bfq_queue *bfqq)
  1942. {
  1943. unsigned int timeout_coeff;
  1944. if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time)
  1945. timeout_coeff = 1;
  1946. else
  1947. timeout_coeff = bfqq->entity.weight / bfqq->entity.orig_weight;
  1948. bfqd->last_budget_start = ktime_get();
  1949. bfqq->budget_timeout = jiffies +
  1950. bfqd->bfq_timeout * timeout_coeff;
  1951. }
  1952. static void __bfq_set_in_service_queue(struct bfq_data *bfqd,
  1953. struct bfq_queue *bfqq)
  1954. {
  1955. if (bfqq) {
  1956. bfqg_stats_update_avg_queue_size(bfqq_group(bfqq));
  1957. bfq_clear_bfqq_fifo_expire(bfqq);
  1958. bfqd->budgets_assigned = (bfqd->budgets_assigned * 7 + 256) / 8;
  1959. if (time_is_before_jiffies(bfqq->last_wr_start_finish) &&
  1960. bfqq->wr_coeff > 1 &&
  1961. bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time &&
  1962. time_is_before_jiffies(bfqq->budget_timeout)) {
  1963. /*
  1964. * For soft real-time queues, move the start
  1965. * of the weight-raising period forward by the
  1966. * time the queue has not received any
  1967. * service. Otherwise, a relatively long
  1968. * service delay is likely to cause the
  1969. * weight-raising period of the queue to end,
  1970. * because of the short duration of the
  1971. * weight-raising period of a soft real-time
  1972. * queue. It is worth noting that this move
  1973. * is not so dangerous for the other queues,
  1974. * because soft real-time queues are not
  1975. * greedy.
  1976. *
  1977. * To not add a further variable, we use the
  1978. * overloaded field budget_timeout to
  1979. * determine for how long the queue has not
  1980. * received service, i.e., how much time has
  1981. * elapsed since the queue expired. However,
  1982. * this is a little imprecise, because
  1983. * budget_timeout is set to jiffies if bfqq
  1984. * not only expires, but also remains with no
  1985. * request.
  1986. */
  1987. if (time_after(bfqq->budget_timeout,
  1988. bfqq->last_wr_start_finish))
  1989. bfqq->last_wr_start_finish +=
  1990. jiffies - bfqq->budget_timeout;
  1991. else
  1992. bfqq->last_wr_start_finish = jiffies;
  1993. }
  1994. bfq_set_budget_timeout(bfqd, bfqq);
  1995. bfq_log_bfqq(bfqd, bfqq,
  1996. "set_in_service_queue, cur-budget = %d",
  1997. bfqq->entity.budget);
  1998. }
  1999. bfqd->in_service_queue = bfqq;
  2000. }
  2001. /*
  2002. * Get and set a new queue for service.
  2003. */
  2004. static struct bfq_queue *bfq_set_in_service_queue(struct bfq_data *bfqd)
  2005. {
  2006. struct bfq_queue *bfqq = bfq_get_next_queue(bfqd);
  2007. __bfq_set_in_service_queue(bfqd, bfqq);
  2008. return bfqq;
  2009. }
  2010. static void bfq_arm_slice_timer(struct bfq_data *bfqd)
  2011. {
  2012. struct bfq_queue *bfqq = bfqd->in_service_queue;
  2013. u32 sl;
  2014. bfq_mark_bfqq_wait_request(bfqq);
  2015. /*
  2016. * We don't want to idle for seeks, but we do want to allow
  2017. * fair distribution of slice time for a process doing back-to-back
  2018. * seeks. So allow a little bit of time for him to submit a new rq.
  2019. */
  2020. sl = bfqd->bfq_slice_idle;
  2021. /*
  2022. * Unless the queue is being weight-raised or the scenario is
  2023. * asymmetric, grant only minimum idle time if the queue
  2024. * is seeky. A long idling is preserved for a weight-raised
  2025. * queue, or, more in general, in an asymmetric scenario,
  2026. * because a long idling is needed for guaranteeing to a queue
  2027. * its reserved share of the throughput (in particular, it is
  2028. * needed if the queue has a higher weight than some other
  2029. * queue).
  2030. */
  2031. if (BFQQ_SEEKY(bfqq) && bfqq->wr_coeff == 1 &&
  2032. bfq_symmetric_scenario(bfqd))
  2033. sl = min_t(u64, sl, BFQ_MIN_TT);
  2034. else if (bfqq->wr_coeff > 1)
  2035. sl = max_t(u32, sl, 20ULL * NSEC_PER_MSEC);
  2036. bfqd->last_idling_start = ktime_get();
  2037. hrtimer_start(&bfqd->idle_slice_timer, ns_to_ktime(sl),
  2038. HRTIMER_MODE_REL);
  2039. bfqg_stats_set_start_idle_time(bfqq_group(bfqq));
  2040. }
  2041. /*
  2042. * In autotuning mode, max_budget is dynamically recomputed as the
  2043. * amount of sectors transferred in timeout at the estimated peak
  2044. * rate. This enables BFQ to utilize a full timeslice with a full
  2045. * budget, even if the in-service queue is served at peak rate. And
  2046. * this maximises throughput with sequential workloads.
  2047. */
  2048. static unsigned long bfq_calc_max_budget(struct bfq_data *bfqd)
  2049. {
  2050. return (u64)bfqd->peak_rate * USEC_PER_MSEC *
  2051. jiffies_to_msecs(bfqd->bfq_timeout)>>BFQ_RATE_SHIFT;
  2052. }
  2053. /*
  2054. * Update parameters related to throughput and responsiveness, as a
  2055. * function of the estimated peak rate. See comments on
  2056. * bfq_calc_max_budget(), and on T_slow and T_fast arrays.
  2057. */
  2058. static void update_thr_responsiveness_params(struct bfq_data *bfqd)
  2059. {
  2060. int dev_type = blk_queue_nonrot(bfqd->queue);
  2061. if (bfqd->bfq_user_max_budget == 0)
  2062. bfqd->bfq_max_budget =
  2063. bfq_calc_max_budget(bfqd);
  2064. if (bfqd->device_speed == BFQ_BFQD_FAST &&
  2065. bfqd->peak_rate < device_speed_thresh[dev_type]) {
  2066. bfqd->device_speed = BFQ_BFQD_SLOW;
  2067. bfqd->RT_prod = R_slow[dev_type] *
  2068. T_slow[dev_type];
  2069. } else if (bfqd->device_speed == BFQ_BFQD_SLOW &&
  2070. bfqd->peak_rate > device_speed_thresh[dev_type]) {
  2071. bfqd->device_speed = BFQ_BFQD_FAST;
  2072. bfqd->RT_prod = R_fast[dev_type] *
  2073. T_fast[dev_type];
  2074. }
  2075. bfq_log(bfqd,
  2076. "dev_type %s dev_speed_class = %s (%llu sects/sec), thresh %llu setcs/sec",
  2077. dev_type == 0 ? "ROT" : "NONROT",
  2078. bfqd->device_speed == BFQ_BFQD_FAST ? "FAST" : "SLOW",
  2079. bfqd->device_speed == BFQ_BFQD_FAST ?
  2080. (USEC_PER_SEC*(u64)R_fast[dev_type])>>BFQ_RATE_SHIFT :
  2081. (USEC_PER_SEC*(u64)R_slow[dev_type])>>BFQ_RATE_SHIFT,
  2082. (USEC_PER_SEC*(u64)device_speed_thresh[dev_type])>>
  2083. BFQ_RATE_SHIFT);
  2084. }
  2085. static void bfq_reset_rate_computation(struct bfq_data *bfqd,
  2086. struct request *rq)
  2087. {
  2088. if (rq != NULL) { /* new rq dispatch now, reset accordingly */
  2089. bfqd->last_dispatch = bfqd->first_dispatch = ktime_get_ns();
  2090. bfqd->peak_rate_samples = 1;
  2091. bfqd->sequential_samples = 0;
  2092. bfqd->tot_sectors_dispatched = bfqd->last_rq_max_size =
  2093. blk_rq_sectors(rq);
  2094. } else /* no new rq dispatched, just reset the number of samples */
  2095. bfqd->peak_rate_samples = 0; /* full re-init on next disp. */
  2096. bfq_log(bfqd,
  2097. "reset_rate_computation at end, sample %u/%u tot_sects %llu",
  2098. bfqd->peak_rate_samples, bfqd->sequential_samples,
  2099. bfqd->tot_sectors_dispatched);
  2100. }
  2101. static void bfq_update_rate_reset(struct bfq_data *bfqd, struct request *rq)
  2102. {
  2103. u32 rate, weight, divisor;
  2104. /*
  2105. * For the convergence property to hold (see comments on
  2106. * bfq_update_peak_rate()) and for the assessment to be
  2107. * reliable, a minimum number of samples must be present, and
  2108. * a minimum amount of time must have elapsed. If not so, do
  2109. * not compute new rate. Just reset parameters, to get ready
  2110. * for a new evaluation attempt.
  2111. */
  2112. if (bfqd->peak_rate_samples < BFQ_RATE_MIN_SAMPLES ||
  2113. bfqd->delta_from_first < BFQ_RATE_MIN_INTERVAL)
  2114. goto reset_computation;
  2115. /*
  2116. * If a new request completion has occurred after last
  2117. * dispatch, then, to approximate the rate at which requests
  2118. * have been served by the device, it is more precise to
  2119. * extend the observation interval to the last completion.
  2120. */
  2121. bfqd->delta_from_first =
  2122. max_t(u64, bfqd->delta_from_first,
  2123. bfqd->last_completion - bfqd->first_dispatch);
  2124. /*
  2125. * Rate computed in sects/usec, and not sects/nsec, for
  2126. * precision issues.
  2127. */
  2128. rate = div64_ul(bfqd->tot_sectors_dispatched<<BFQ_RATE_SHIFT,
  2129. div_u64(bfqd->delta_from_first, NSEC_PER_USEC));
  2130. /*
  2131. * Peak rate not updated if:
  2132. * - the percentage of sequential dispatches is below 3/4 of the
  2133. * total, and rate is below the current estimated peak rate
  2134. * - rate is unreasonably high (> 20M sectors/sec)
  2135. */
  2136. if ((bfqd->sequential_samples < (3 * bfqd->peak_rate_samples)>>2 &&
  2137. rate <= bfqd->peak_rate) ||
  2138. rate > 20<<BFQ_RATE_SHIFT)
  2139. goto reset_computation;
  2140. /*
  2141. * We have to update the peak rate, at last! To this purpose,
  2142. * we use a low-pass filter. We compute the smoothing constant
  2143. * of the filter as a function of the 'weight' of the new
  2144. * measured rate.
  2145. *
  2146. * As can be seen in next formulas, we define this weight as a
  2147. * quantity proportional to how sequential the workload is,
  2148. * and to how long the observation time interval is.
  2149. *
  2150. * The weight runs from 0 to 8. The maximum value of the
  2151. * weight, 8, yields the minimum value for the smoothing
  2152. * constant. At this minimum value for the smoothing constant,
  2153. * the measured rate contributes for half of the next value of
  2154. * the estimated peak rate.
  2155. *
  2156. * So, the first step is to compute the weight as a function
  2157. * of how sequential the workload is. Note that the weight
  2158. * cannot reach 9, because bfqd->sequential_samples cannot
  2159. * become equal to bfqd->peak_rate_samples, which, in its
  2160. * turn, holds true because bfqd->sequential_samples is not
  2161. * incremented for the first sample.
  2162. */
  2163. weight = (9 * bfqd->sequential_samples) / bfqd->peak_rate_samples;
  2164. /*
  2165. * Second step: further refine the weight as a function of the
  2166. * duration of the observation interval.
  2167. */
  2168. weight = min_t(u32, 8,
  2169. div_u64(weight * bfqd->delta_from_first,
  2170. BFQ_RATE_REF_INTERVAL));
  2171. /*
  2172. * Divisor ranging from 10, for minimum weight, to 2, for
  2173. * maximum weight.
  2174. */
  2175. divisor = 10 - weight;
  2176. /*
  2177. * Finally, update peak rate:
  2178. *
  2179. * peak_rate = peak_rate * (divisor-1) / divisor + rate / divisor
  2180. */
  2181. bfqd->peak_rate *= divisor-1;
  2182. bfqd->peak_rate /= divisor;
  2183. rate /= divisor; /* smoothing constant alpha = 1/divisor */
  2184. bfqd->peak_rate += rate;
  2185. update_thr_responsiveness_params(bfqd);
  2186. reset_computation:
  2187. bfq_reset_rate_computation(bfqd, rq);
  2188. }
  2189. /*
  2190. * Update the read/write peak rate (the main quantity used for
  2191. * auto-tuning, see update_thr_responsiveness_params()).
  2192. *
  2193. * It is not trivial to estimate the peak rate (correctly): because of
  2194. * the presence of sw and hw queues between the scheduler and the
  2195. * device components that finally serve I/O requests, it is hard to
  2196. * say exactly when a given dispatched request is served inside the
  2197. * device, and for how long. As a consequence, it is hard to know
  2198. * precisely at what rate a given set of requests is actually served
  2199. * by the device.
  2200. *
  2201. * On the opposite end, the dispatch time of any request is trivially
  2202. * available, and, from this piece of information, the "dispatch rate"
  2203. * of requests can be immediately computed. So, the idea in the next
  2204. * function is to use what is known, namely request dispatch times
  2205. * (plus, when useful, request completion times), to estimate what is
  2206. * unknown, namely in-device request service rate.
  2207. *
  2208. * The main issue is that, because of the above facts, the rate at
  2209. * which a certain set of requests is dispatched over a certain time
  2210. * interval can vary greatly with respect to the rate at which the
  2211. * same requests are then served. But, since the size of any
  2212. * intermediate queue is limited, and the service scheme is lossless
  2213. * (no request is silently dropped), the following obvious convergence
  2214. * property holds: the number of requests dispatched MUST become
  2215. * closer and closer to the number of requests completed as the
  2216. * observation interval grows. This is the key property used in
  2217. * the next function to estimate the peak service rate as a function
  2218. * of the observed dispatch rate. The function assumes to be invoked
  2219. * on every request dispatch.
  2220. */
  2221. static void bfq_update_peak_rate(struct bfq_data *bfqd, struct request *rq)
  2222. {
  2223. u64 now_ns = ktime_get_ns();
  2224. if (bfqd->peak_rate_samples == 0) { /* first dispatch */
  2225. bfq_log(bfqd, "update_peak_rate: goto reset, samples %d",
  2226. bfqd->peak_rate_samples);
  2227. bfq_reset_rate_computation(bfqd, rq);
  2228. goto update_last_values; /* will add one sample */
  2229. }
  2230. /*
  2231. * Device idle for very long: the observation interval lasting
  2232. * up to this dispatch cannot be a valid observation interval
  2233. * for computing a new peak rate (similarly to the late-
  2234. * completion event in bfq_completed_request()). Go to
  2235. * update_rate_and_reset to have the following three steps
  2236. * taken:
  2237. * - close the observation interval at the last (previous)
  2238. * request dispatch or completion
  2239. * - compute rate, if possible, for that observation interval
  2240. * - start a new observation interval with this dispatch
  2241. */
  2242. if (now_ns - bfqd->last_dispatch > 100*NSEC_PER_MSEC &&
  2243. bfqd->rq_in_driver == 0)
  2244. goto update_rate_and_reset;
  2245. /* Update sampling information */
  2246. bfqd->peak_rate_samples++;
  2247. if ((bfqd->rq_in_driver > 0 ||
  2248. now_ns - bfqd->last_completion < BFQ_MIN_TT)
  2249. && get_sdist(bfqd->last_position, rq) < BFQQ_SEEK_THR)
  2250. bfqd->sequential_samples++;
  2251. bfqd->tot_sectors_dispatched += blk_rq_sectors(rq);
  2252. /* Reset max observed rq size every 32 dispatches */
  2253. if (likely(bfqd->peak_rate_samples % 32))
  2254. bfqd->last_rq_max_size =
  2255. max_t(u32, blk_rq_sectors(rq), bfqd->last_rq_max_size);
  2256. else
  2257. bfqd->last_rq_max_size = blk_rq_sectors(rq);
  2258. bfqd->delta_from_first = now_ns - bfqd->first_dispatch;
  2259. /* Target observation interval not yet reached, go on sampling */
  2260. if (bfqd->delta_from_first < BFQ_RATE_REF_INTERVAL)
  2261. goto update_last_values;
  2262. update_rate_and_reset:
  2263. bfq_update_rate_reset(bfqd, rq);
  2264. update_last_values:
  2265. bfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
  2266. bfqd->last_dispatch = now_ns;
  2267. }
  2268. /*
  2269. * Remove request from internal lists.
  2270. */
  2271. static void bfq_dispatch_remove(struct request_queue *q, struct request *rq)
  2272. {
  2273. struct bfq_queue *bfqq = RQ_BFQQ(rq);
  2274. /*
  2275. * For consistency, the next instruction should have been
  2276. * executed after removing the request from the queue and
  2277. * dispatching it. We execute instead this instruction before
  2278. * bfq_remove_request() (and hence introduce a temporary
  2279. * inconsistency), for efficiency. In fact, should this
  2280. * dispatch occur for a non in-service bfqq, this anticipated
  2281. * increment prevents two counters related to bfqq->dispatched
  2282. * from risking to be, first, uselessly decremented, and then
  2283. * incremented again when the (new) value of bfqq->dispatched
  2284. * happens to be taken into account.
  2285. */
  2286. bfqq->dispatched++;
  2287. bfq_update_peak_rate(q->elevator->elevator_data, rq);
  2288. bfq_remove_request(q, rq);
  2289. }
  2290. static void __bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  2291. {
  2292. /*
  2293. * If this bfqq is shared between multiple processes, check
  2294. * to make sure that those processes are still issuing I/Os
  2295. * within the mean seek distance. If not, it may be time to
  2296. * break the queues apart again.
  2297. */
  2298. if (bfq_bfqq_coop(bfqq) && BFQQ_SEEKY(bfqq))
  2299. bfq_mark_bfqq_split_coop(bfqq);
  2300. if (RB_EMPTY_ROOT(&bfqq->sort_list)) {
  2301. if (bfqq->dispatched == 0)
  2302. /*
  2303. * Overloading budget_timeout field to store
  2304. * the time at which the queue remains with no
  2305. * backlog and no outstanding request; used by
  2306. * the weight-raising mechanism.
  2307. */
  2308. bfqq->budget_timeout = jiffies;
  2309. bfq_del_bfqq_busy(bfqd, bfqq, true);
  2310. } else {
  2311. bfq_requeue_bfqq(bfqd, bfqq, true);
  2312. /*
  2313. * Resort priority tree of potential close cooperators.
  2314. */
  2315. bfq_pos_tree_add_move(bfqd, bfqq);
  2316. }
  2317. /*
  2318. * All in-service entities must have been properly deactivated
  2319. * or requeued before executing the next function, which
  2320. * resets all in-service entites as no more in service.
  2321. */
  2322. __bfq_bfqd_reset_in_service(bfqd);
  2323. }
  2324. /**
  2325. * __bfq_bfqq_recalc_budget - try to adapt the budget to the @bfqq behavior.
  2326. * @bfqd: device data.
  2327. * @bfqq: queue to update.
  2328. * @reason: reason for expiration.
  2329. *
  2330. * Handle the feedback on @bfqq budget at queue expiration.
  2331. * See the body for detailed comments.
  2332. */
  2333. static void __bfq_bfqq_recalc_budget(struct bfq_data *bfqd,
  2334. struct bfq_queue *bfqq,
  2335. enum bfqq_expiration reason)
  2336. {
  2337. struct request *next_rq;
  2338. int budget, min_budget;
  2339. min_budget = bfq_min_budget(bfqd);
  2340. if (bfqq->wr_coeff == 1)
  2341. budget = bfqq->max_budget;
  2342. else /*
  2343. * Use a constant, low budget for weight-raised queues,
  2344. * to help achieve a low latency. Keep it slightly higher
  2345. * than the minimum possible budget, to cause a little
  2346. * bit fewer expirations.
  2347. */
  2348. budget = 2 * min_budget;
  2349. bfq_log_bfqq(bfqd, bfqq, "recalc_budg: last budg %d, budg left %d",
  2350. bfqq->entity.budget, bfq_bfqq_budget_left(bfqq));
  2351. bfq_log_bfqq(bfqd, bfqq, "recalc_budg: last max_budg %d, min budg %d",
  2352. budget, bfq_min_budget(bfqd));
  2353. bfq_log_bfqq(bfqd, bfqq, "recalc_budg: sync %d, seeky %d",
  2354. bfq_bfqq_sync(bfqq), BFQQ_SEEKY(bfqd->in_service_queue));
  2355. if (bfq_bfqq_sync(bfqq) && bfqq->wr_coeff == 1) {
  2356. switch (reason) {
  2357. /*
  2358. * Caveat: in all the following cases we trade latency
  2359. * for throughput.
  2360. */
  2361. case BFQQE_TOO_IDLE:
  2362. /*
  2363. * This is the only case where we may reduce
  2364. * the budget: if there is no request of the
  2365. * process still waiting for completion, then
  2366. * we assume (tentatively) that the timer has
  2367. * expired because the batch of requests of
  2368. * the process could have been served with a
  2369. * smaller budget. Hence, betting that
  2370. * process will behave in the same way when it
  2371. * becomes backlogged again, we reduce its
  2372. * next budget. As long as we guess right,
  2373. * this budget cut reduces the latency
  2374. * experienced by the process.
  2375. *
  2376. * However, if there are still outstanding
  2377. * requests, then the process may have not yet
  2378. * issued its next request just because it is
  2379. * still waiting for the completion of some of
  2380. * the still outstanding ones. So in this
  2381. * subcase we do not reduce its budget, on the
  2382. * contrary we increase it to possibly boost
  2383. * the throughput, as discussed in the
  2384. * comments to the BUDGET_TIMEOUT case.
  2385. */
  2386. if (bfqq->dispatched > 0) /* still outstanding reqs */
  2387. budget = min(budget * 2, bfqd->bfq_max_budget);
  2388. else {
  2389. if (budget > 5 * min_budget)
  2390. budget -= 4 * min_budget;
  2391. else
  2392. budget = min_budget;
  2393. }
  2394. break;
  2395. case BFQQE_BUDGET_TIMEOUT:
  2396. /*
  2397. * We double the budget here because it gives
  2398. * the chance to boost the throughput if this
  2399. * is not a seeky process (and has bumped into
  2400. * this timeout because of, e.g., ZBR).
  2401. */
  2402. budget = min(budget * 2, bfqd->bfq_max_budget);
  2403. break;
  2404. case BFQQE_BUDGET_EXHAUSTED:
  2405. /*
  2406. * The process still has backlog, and did not
  2407. * let either the budget timeout or the disk
  2408. * idling timeout expire. Hence it is not
  2409. * seeky, has a short thinktime and may be
  2410. * happy with a higher budget too. So
  2411. * definitely increase the budget of this good
  2412. * candidate to boost the disk throughput.
  2413. */
  2414. budget = min(budget * 4, bfqd->bfq_max_budget);
  2415. break;
  2416. case BFQQE_NO_MORE_REQUESTS:
  2417. /*
  2418. * For queues that expire for this reason, it
  2419. * is particularly important to keep the
  2420. * budget close to the actual service they
  2421. * need. Doing so reduces the timestamp
  2422. * misalignment problem described in the
  2423. * comments in the body of
  2424. * __bfq_activate_entity. In fact, suppose
  2425. * that a queue systematically expires for
  2426. * BFQQE_NO_MORE_REQUESTS and presents a
  2427. * new request in time to enjoy timestamp
  2428. * back-shifting. The larger the budget of the
  2429. * queue is with respect to the service the
  2430. * queue actually requests in each service
  2431. * slot, the more times the queue can be
  2432. * reactivated with the same virtual finish
  2433. * time. It follows that, even if this finish
  2434. * time is pushed to the system virtual time
  2435. * to reduce the consequent timestamp
  2436. * misalignment, the queue unjustly enjoys for
  2437. * many re-activations a lower finish time
  2438. * than all newly activated queues.
  2439. *
  2440. * The service needed by bfqq is measured
  2441. * quite precisely by bfqq->entity.service.
  2442. * Since bfqq does not enjoy device idling,
  2443. * bfqq->entity.service is equal to the number
  2444. * of sectors that the process associated with
  2445. * bfqq requested to read/write before waiting
  2446. * for request completions, or blocking for
  2447. * other reasons.
  2448. */
  2449. budget = max_t(int, bfqq->entity.service, min_budget);
  2450. break;
  2451. default:
  2452. return;
  2453. }
  2454. } else if (!bfq_bfqq_sync(bfqq)) {
  2455. /*
  2456. * Async queues get always the maximum possible
  2457. * budget, as for them we do not care about latency
  2458. * (in addition, their ability to dispatch is limited
  2459. * by the charging factor).
  2460. */
  2461. budget = bfqd->bfq_max_budget;
  2462. }
  2463. bfqq->max_budget = budget;
  2464. if (bfqd->budgets_assigned >= bfq_stats_min_budgets &&
  2465. !bfqd->bfq_user_max_budget)
  2466. bfqq->max_budget = min(bfqq->max_budget, bfqd->bfq_max_budget);
  2467. /*
  2468. * If there is still backlog, then assign a new budget, making
  2469. * sure that it is large enough for the next request. Since
  2470. * the finish time of bfqq must be kept in sync with the
  2471. * budget, be sure to call __bfq_bfqq_expire() *after* this
  2472. * update.
  2473. *
  2474. * If there is no backlog, then no need to update the budget;
  2475. * it will be updated on the arrival of a new request.
  2476. */
  2477. next_rq = bfqq->next_rq;
  2478. if (next_rq)
  2479. bfqq->entity.budget = max_t(unsigned long, bfqq->max_budget,
  2480. bfq_serv_to_charge(next_rq, bfqq));
  2481. bfq_log_bfqq(bfqd, bfqq, "head sect: %u, new budget %d",
  2482. next_rq ? blk_rq_sectors(next_rq) : 0,
  2483. bfqq->entity.budget);
  2484. }
  2485. /*
  2486. * Return true if the process associated with bfqq is "slow". The slow
  2487. * flag is used, in addition to the budget timeout, to reduce the
  2488. * amount of service provided to seeky processes, and thus reduce
  2489. * their chances to lower the throughput. More details in the comments
  2490. * on the function bfq_bfqq_expire().
  2491. *
  2492. * An important observation is in order: as discussed in the comments
  2493. * on the function bfq_update_peak_rate(), with devices with internal
  2494. * queues, it is hard if ever possible to know when and for how long
  2495. * an I/O request is processed by the device (apart from the trivial
  2496. * I/O pattern where a new request is dispatched only after the
  2497. * previous one has been completed). This makes it hard to evaluate
  2498. * the real rate at which the I/O requests of each bfq_queue are
  2499. * served. In fact, for an I/O scheduler like BFQ, serving a
  2500. * bfq_queue means just dispatching its requests during its service
  2501. * slot (i.e., until the budget of the queue is exhausted, or the
  2502. * queue remains idle, or, finally, a timeout fires). But, during the
  2503. * service slot of a bfq_queue, around 100 ms at most, the device may
  2504. * be even still processing requests of bfq_queues served in previous
  2505. * service slots. On the opposite end, the requests of the in-service
  2506. * bfq_queue may be completed after the service slot of the queue
  2507. * finishes.
  2508. *
  2509. * Anyway, unless more sophisticated solutions are used
  2510. * (where possible), the sum of the sizes of the requests dispatched
  2511. * during the service slot of a bfq_queue is probably the only
  2512. * approximation available for the service received by the bfq_queue
  2513. * during its service slot. And this sum is the quantity used in this
  2514. * function to evaluate the I/O speed of a process.
  2515. */
  2516. static bool bfq_bfqq_is_slow(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  2517. bool compensate, enum bfqq_expiration reason,
  2518. unsigned long *delta_ms)
  2519. {
  2520. ktime_t delta_ktime;
  2521. u32 delta_usecs;
  2522. bool slow = BFQQ_SEEKY(bfqq); /* if delta too short, use seekyness */
  2523. if (!bfq_bfqq_sync(bfqq))
  2524. return false;
  2525. if (compensate)
  2526. delta_ktime = bfqd->last_idling_start;
  2527. else
  2528. delta_ktime = ktime_get();
  2529. delta_ktime = ktime_sub(delta_ktime, bfqd->last_budget_start);
  2530. delta_usecs = ktime_to_us(delta_ktime);
  2531. /* don't use too short time intervals */
  2532. if (delta_usecs < 1000) {
  2533. if (blk_queue_nonrot(bfqd->queue))
  2534. /*
  2535. * give same worst-case guarantees as idling
  2536. * for seeky
  2537. */
  2538. *delta_ms = BFQ_MIN_TT / NSEC_PER_MSEC;
  2539. else /* charge at least one seek */
  2540. *delta_ms = bfq_slice_idle / NSEC_PER_MSEC;
  2541. return slow;
  2542. }
  2543. *delta_ms = delta_usecs / USEC_PER_MSEC;
  2544. /*
  2545. * Use only long (> 20ms) intervals to filter out excessive
  2546. * spikes in service rate estimation.
  2547. */
  2548. if (delta_usecs > 20000) {
  2549. /*
  2550. * Caveat for rotational devices: processes doing I/O
  2551. * in the slower disk zones tend to be slow(er) even
  2552. * if not seeky. In this respect, the estimated peak
  2553. * rate is likely to be an average over the disk
  2554. * surface. Accordingly, to not be too harsh with
  2555. * unlucky processes, a process is deemed slow only if
  2556. * its rate has been lower than half of the estimated
  2557. * peak rate.
  2558. */
  2559. slow = bfqq->entity.service < bfqd->bfq_max_budget / 2;
  2560. }
  2561. bfq_log_bfqq(bfqd, bfqq, "bfq_bfqq_is_slow: slow %d", slow);
  2562. return slow;
  2563. }
  2564. /*
  2565. * To be deemed as soft real-time, an application must meet two
  2566. * requirements. First, the application must not require an average
  2567. * bandwidth higher than the approximate bandwidth required to playback or
  2568. * record a compressed high-definition video.
  2569. * The next function is invoked on the completion of the last request of a
  2570. * batch, to compute the next-start time instant, soft_rt_next_start, such
  2571. * that, if the next request of the application does not arrive before
  2572. * soft_rt_next_start, then the above requirement on the bandwidth is met.
  2573. *
  2574. * The second requirement is that the request pattern of the application is
  2575. * isochronous, i.e., that, after issuing a request or a batch of requests,
  2576. * the application stops issuing new requests until all its pending requests
  2577. * have been completed. After that, the application may issue a new batch,
  2578. * and so on.
  2579. * For this reason the next function is invoked to compute
  2580. * soft_rt_next_start only for applications that meet this requirement,
  2581. * whereas soft_rt_next_start is set to infinity for applications that do
  2582. * not.
  2583. *
  2584. * Unfortunately, even a greedy application may happen to behave in an
  2585. * isochronous way if the CPU load is high. In fact, the application may
  2586. * stop issuing requests while the CPUs are busy serving other processes,
  2587. * then restart, then stop again for a while, and so on. In addition, if
  2588. * the disk achieves a low enough throughput with the request pattern
  2589. * issued by the application (e.g., because the request pattern is random
  2590. * and/or the device is slow), then the application may meet the above
  2591. * bandwidth requirement too. To prevent such a greedy application to be
  2592. * deemed as soft real-time, a further rule is used in the computation of
  2593. * soft_rt_next_start: soft_rt_next_start must be higher than the current
  2594. * time plus the maximum time for which the arrival of a request is waited
  2595. * for when a sync queue becomes idle, namely bfqd->bfq_slice_idle.
  2596. * This filters out greedy applications, as the latter issue instead their
  2597. * next request as soon as possible after the last one has been completed
  2598. * (in contrast, when a batch of requests is completed, a soft real-time
  2599. * application spends some time processing data).
  2600. *
  2601. * Unfortunately, the last filter may easily generate false positives if
  2602. * only bfqd->bfq_slice_idle is used as a reference time interval and one
  2603. * or both the following cases occur:
  2604. * 1) HZ is so low that the duration of a jiffy is comparable to or higher
  2605. * than bfqd->bfq_slice_idle. This happens, e.g., on slow devices with
  2606. * HZ=100.
  2607. * 2) jiffies, instead of increasing at a constant rate, may stop increasing
  2608. * for a while, then suddenly 'jump' by several units to recover the lost
  2609. * increments. This seems to happen, e.g., inside virtual machines.
  2610. * To address this issue, we do not use as a reference time interval just
  2611. * bfqd->bfq_slice_idle, but bfqd->bfq_slice_idle plus a few jiffies. In
  2612. * particular we add the minimum number of jiffies for which the filter
  2613. * seems to be quite precise also in embedded systems and KVM/QEMU virtual
  2614. * machines.
  2615. */
  2616. static unsigned long bfq_bfqq_softrt_next_start(struct bfq_data *bfqd,
  2617. struct bfq_queue *bfqq)
  2618. {
  2619. return max(bfqq->last_idle_bklogged +
  2620. HZ * bfqq->service_from_backlogged /
  2621. bfqd->bfq_wr_max_softrt_rate,
  2622. jiffies + nsecs_to_jiffies(bfqq->bfqd->bfq_slice_idle) + 4);
  2623. }
  2624. /**
  2625. * bfq_bfqq_expire - expire a queue.
  2626. * @bfqd: device owning the queue.
  2627. * @bfqq: the queue to expire.
  2628. * @compensate: if true, compensate for the time spent idling.
  2629. * @reason: the reason causing the expiration.
  2630. *
  2631. * If the process associated with bfqq does slow I/O (e.g., because it
  2632. * issues random requests), we charge bfqq with the time it has been
  2633. * in service instead of the service it has received (see
  2634. * bfq_bfqq_charge_time for details on how this goal is achieved). As
  2635. * a consequence, bfqq will typically get higher timestamps upon
  2636. * reactivation, and hence it will be rescheduled as if it had
  2637. * received more service than what it has actually received. In the
  2638. * end, bfqq receives less service in proportion to how slowly its
  2639. * associated process consumes its budgets (and hence how seriously it
  2640. * tends to lower the throughput). In addition, this time-charging
  2641. * strategy guarantees time fairness among slow processes. In
  2642. * contrast, if the process associated with bfqq is not slow, we
  2643. * charge bfqq exactly with the service it has received.
  2644. *
  2645. * Charging time to the first type of queues and the exact service to
  2646. * the other has the effect of using the WF2Q+ policy to schedule the
  2647. * former on a timeslice basis, without violating service domain
  2648. * guarantees among the latter.
  2649. */
  2650. void bfq_bfqq_expire(struct bfq_data *bfqd,
  2651. struct bfq_queue *bfqq,
  2652. bool compensate,
  2653. enum bfqq_expiration reason)
  2654. {
  2655. bool slow;
  2656. unsigned long delta = 0;
  2657. struct bfq_entity *entity = &bfqq->entity;
  2658. int ref;
  2659. /*
  2660. * Check whether the process is slow (see bfq_bfqq_is_slow).
  2661. */
  2662. slow = bfq_bfqq_is_slow(bfqd, bfqq, compensate, reason, &delta);
  2663. /*
  2664. * Increase service_from_backlogged before next statement,
  2665. * because the possible next invocation of
  2666. * bfq_bfqq_charge_time would likely inflate
  2667. * entity->service. In contrast, service_from_backlogged must
  2668. * contain real service, to enable the soft real-time
  2669. * heuristic to correctly compute the bandwidth consumed by
  2670. * bfqq.
  2671. */
  2672. bfqq->service_from_backlogged += entity->service;
  2673. /*
  2674. * As above explained, charge slow (typically seeky) and
  2675. * timed-out queues with the time and not the service
  2676. * received, to favor sequential workloads.
  2677. *
  2678. * Processes doing I/O in the slower disk zones will tend to
  2679. * be slow(er) even if not seeky. Therefore, since the
  2680. * estimated peak rate is actually an average over the disk
  2681. * surface, these processes may timeout just for bad luck. To
  2682. * avoid punishing them, do not charge time to processes that
  2683. * succeeded in consuming at least 2/3 of their budget. This
  2684. * allows BFQ to preserve enough elasticity to still perform
  2685. * bandwidth, and not time, distribution with little unlucky
  2686. * or quasi-sequential processes.
  2687. */
  2688. if (bfqq->wr_coeff == 1 &&
  2689. (slow ||
  2690. (reason == BFQQE_BUDGET_TIMEOUT &&
  2691. bfq_bfqq_budget_left(bfqq) >= entity->budget / 3)))
  2692. bfq_bfqq_charge_time(bfqd, bfqq, delta);
  2693. if (reason == BFQQE_TOO_IDLE &&
  2694. entity->service <= 2 * entity->budget / 10)
  2695. bfq_clear_bfqq_IO_bound(bfqq);
  2696. if (bfqd->low_latency && bfqq->wr_coeff == 1)
  2697. bfqq->last_wr_start_finish = jiffies;
  2698. if (bfqd->low_latency && bfqd->bfq_wr_max_softrt_rate > 0 &&
  2699. RB_EMPTY_ROOT(&bfqq->sort_list)) {
  2700. /*
  2701. * If we get here, and there are no outstanding
  2702. * requests, then the request pattern is isochronous
  2703. * (see the comments on the function
  2704. * bfq_bfqq_softrt_next_start()). Thus we can compute
  2705. * soft_rt_next_start. If, instead, the queue still
  2706. * has outstanding requests, then we have to wait for
  2707. * the completion of all the outstanding requests to
  2708. * discover whether the request pattern is actually
  2709. * isochronous.
  2710. */
  2711. if (bfqq->dispatched == 0)
  2712. bfqq->soft_rt_next_start =
  2713. bfq_bfqq_softrt_next_start(bfqd, bfqq);
  2714. else {
  2715. /*
  2716. * The application is still waiting for the
  2717. * completion of one or more requests:
  2718. * prevent it from possibly being incorrectly
  2719. * deemed as soft real-time by setting its
  2720. * soft_rt_next_start to infinity. In fact,
  2721. * without this assignment, the application
  2722. * would be incorrectly deemed as soft
  2723. * real-time if:
  2724. * 1) it issued a new request before the
  2725. * completion of all its in-flight
  2726. * requests, and
  2727. * 2) at that time, its soft_rt_next_start
  2728. * happened to be in the past.
  2729. */
  2730. bfqq->soft_rt_next_start =
  2731. bfq_greatest_from_now();
  2732. /*
  2733. * Schedule an update of soft_rt_next_start to when
  2734. * the task may be discovered to be isochronous.
  2735. */
  2736. bfq_mark_bfqq_softrt_update(bfqq);
  2737. }
  2738. }
  2739. bfq_log_bfqq(bfqd, bfqq,
  2740. "expire (%d, slow %d, num_disp %d, short_ttime %d)", reason,
  2741. slow, bfqq->dispatched, bfq_bfqq_has_short_ttime(bfqq));
  2742. /*
  2743. * Increase, decrease or leave budget unchanged according to
  2744. * reason.
  2745. */
  2746. __bfq_bfqq_recalc_budget(bfqd, bfqq, reason);
  2747. ref = bfqq->ref;
  2748. __bfq_bfqq_expire(bfqd, bfqq);
  2749. /* mark bfqq as waiting a request only if a bic still points to it */
  2750. if (ref > 1 && !bfq_bfqq_busy(bfqq) &&
  2751. reason != BFQQE_BUDGET_TIMEOUT &&
  2752. reason != BFQQE_BUDGET_EXHAUSTED)
  2753. bfq_mark_bfqq_non_blocking_wait_rq(bfqq);
  2754. }
  2755. /*
  2756. * Budget timeout is not implemented through a dedicated timer, but
  2757. * just checked on request arrivals and completions, as well as on
  2758. * idle timer expirations.
  2759. */
  2760. static bool bfq_bfqq_budget_timeout(struct bfq_queue *bfqq)
  2761. {
  2762. return time_is_before_eq_jiffies(bfqq->budget_timeout);
  2763. }
  2764. /*
  2765. * If we expire a queue that is actively waiting (i.e., with the
  2766. * device idled) for the arrival of a new request, then we may incur
  2767. * the timestamp misalignment problem described in the body of the
  2768. * function __bfq_activate_entity. Hence we return true only if this
  2769. * condition does not hold, or if the queue is slow enough to deserve
  2770. * only to be kicked off for preserving a high throughput.
  2771. */
  2772. static bool bfq_may_expire_for_budg_timeout(struct bfq_queue *bfqq)
  2773. {
  2774. bfq_log_bfqq(bfqq->bfqd, bfqq,
  2775. "may_budget_timeout: wait_request %d left %d timeout %d",
  2776. bfq_bfqq_wait_request(bfqq),
  2777. bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3,
  2778. bfq_bfqq_budget_timeout(bfqq));
  2779. return (!bfq_bfqq_wait_request(bfqq) ||
  2780. bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3)
  2781. &&
  2782. bfq_bfqq_budget_timeout(bfqq);
  2783. }
  2784. /*
  2785. * For a queue that becomes empty, device idling is allowed only if
  2786. * this function returns true for the queue. As a consequence, since
  2787. * device idling plays a critical role in both throughput boosting and
  2788. * service guarantees, the return value of this function plays a
  2789. * critical role in both these aspects as well.
  2790. *
  2791. * In a nutshell, this function returns true only if idling is
  2792. * beneficial for throughput or, even if detrimental for throughput,
  2793. * idling is however necessary to preserve service guarantees (low
  2794. * latency, desired throughput distribution, ...). In particular, on
  2795. * NCQ-capable devices, this function tries to return false, so as to
  2796. * help keep the drives' internal queues full, whenever this helps the
  2797. * device boost the throughput without causing any service-guarantee
  2798. * issue.
  2799. *
  2800. * In more detail, the return value of this function is obtained by,
  2801. * first, computing a number of boolean variables that take into
  2802. * account throughput and service-guarantee issues, and, then,
  2803. * combining these variables in a logical expression. Most of the
  2804. * issues taken into account are not trivial. We discuss these issues
  2805. * individually while introducing the variables.
  2806. */
  2807. static bool bfq_bfqq_may_idle(struct bfq_queue *bfqq)
  2808. {
  2809. struct bfq_data *bfqd = bfqq->bfqd;
  2810. bool rot_without_queueing =
  2811. !blk_queue_nonrot(bfqd->queue) && !bfqd->hw_tag,
  2812. bfqq_sequential_and_IO_bound,
  2813. idling_boosts_thr, idling_boosts_thr_without_issues,
  2814. idling_needed_for_service_guarantees,
  2815. asymmetric_scenario;
  2816. if (bfqd->strict_guarantees)
  2817. return true;
  2818. /*
  2819. * Idling is performed only if slice_idle > 0. In addition, we
  2820. * do not idle if
  2821. * (a) bfqq is async
  2822. * (b) bfqq is in the idle io prio class: in this case we do
  2823. * not idle because we want to minimize the bandwidth that
  2824. * queues in this class can steal to higher-priority queues
  2825. */
  2826. if (bfqd->bfq_slice_idle == 0 || !bfq_bfqq_sync(bfqq) ||
  2827. bfq_class_idle(bfqq))
  2828. return false;
  2829. bfqq_sequential_and_IO_bound = !BFQQ_SEEKY(bfqq) &&
  2830. bfq_bfqq_IO_bound(bfqq) && bfq_bfqq_has_short_ttime(bfqq);
  2831. /*
  2832. * The next variable takes into account the cases where idling
  2833. * boosts the throughput.
  2834. *
  2835. * The value of the variable is computed considering, first, that
  2836. * idling is virtually always beneficial for the throughput if:
  2837. * (a) the device is not NCQ-capable and rotational, or
  2838. * (b) regardless of the presence of NCQ, the device is rotational and
  2839. * the request pattern for bfqq is I/O-bound and sequential, or
  2840. * (c) regardless of whether it is rotational, the device is
  2841. * not NCQ-capable and the request pattern for bfqq is
  2842. * I/O-bound and sequential.
  2843. *
  2844. * Secondly, and in contrast to the above item (b), idling an
  2845. * NCQ-capable flash-based device would not boost the
  2846. * throughput even with sequential I/O; rather it would lower
  2847. * the throughput in proportion to how fast the device
  2848. * is. Accordingly, the next variable is true if any of the
  2849. * above conditions (a), (b) or (c) is true, and, in
  2850. * particular, happens to be false if bfqd is an NCQ-capable
  2851. * flash-based device.
  2852. */
  2853. idling_boosts_thr = rot_without_queueing ||
  2854. ((!blk_queue_nonrot(bfqd->queue) || !bfqd->hw_tag) &&
  2855. bfqq_sequential_and_IO_bound);
  2856. /*
  2857. * The value of the next variable,
  2858. * idling_boosts_thr_without_issues, is equal to that of
  2859. * idling_boosts_thr, unless a special case holds. In this
  2860. * special case, described below, idling may cause problems to
  2861. * weight-raised queues.
  2862. *
  2863. * When the request pool is saturated (e.g., in the presence
  2864. * of write hogs), if the processes associated with
  2865. * non-weight-raised queues ask for requests at a lower rate,
  2866. * then processes associated with weight-raised queues have a
  2867. * higher probability to get a request from the pool
  2868. * immediately (or at least soon) when they need one. Thus
  2869. * they have a higher probability to actually get a fraction
  2870. * of the device throughput proportional to their high
  2871. * weight. This is especially true with NCQ-capable drives,
  2872. * which enqueue several requests in advance, and further
  2873. * reorder internally-queued requests.
  2874. *
  2875. * For this reason, we force to false the value of
  2876. * idling_boosts_thr_without_issues if there are weight-raised
  2877. * busy queues. In this case, and if bfqq is not weight-raised,
  2878. * this guarantees that the device is not idled for bfqq (if,
  2879. * instead, bfqq is weight-raised, then idling will be
  2880. * guaranteed by another variable, see below). Combined with
  2881. * the timestamping rules of BFQ (see [1] for details), this
  2882. * behavior causes bfqq, and hence any sync non-weight-raised
  2883. * queue, to get a lower number of requests served, and thus
  2884. * to ask for a lower number of requests from the request
  2885. * pool, before the busy weight-raised queues get served
  2886. * again. This often mitigates starvation problems in the
  2887. * presence of heavy write workloads and NCQ, thereby
  2888. * guaranteeing a higher application and system responsiveness
  2889. * in these hostile scenarios.
  2890. */
  2891. idling_boosts_thr_without_issues = idling_boosts_thr &&
  2892. bfqd->wr_busy_queues == 0;
  2893. /*
  2894. * There is then a case where idling must be performed not
  2895. * for throughput concerns, but to preserve service
  2896. * guarantees.
  2897. *
  2898. * To introduce this case, we can note that allowing the drive
  2899. * to enqueue more than one request at a time, and hence
  2900. * delegating de facto final scheduling decisions to the
  2901. * drive's internal scheduler, entails loss of control on the
  2902. * actual request service order. In particular, the critical
  2903. * situation is when requests from different processes happen
  2904. * to be present, at the same time, in the internal queue(s)
  2905. * of the drive. In such a situation, the drive, by deciding
  2906. * the service order of the internally-queued requests, does
  2907. * determine also the actual throughput distribution among
  2908. * these processes. But the drive typically has no notion or
  2909. * concern about per-process throughput distribution, and
  2910. * makes its decisions only on a per-request basis. Therefore,
  2911. * the service distribution enforced by the drive's internal
  2912. * scheduler is likely to coincide with the desired
  2913. * device-throughput distribution only in a completely
  2914. * symmetric scenario where:
  2915. * (i) each of these processes must get the same throughput as
  2916. * the others;
  2917. * (ii) all these processes have the same I/O pattern
  2918. (either sequential or random).
  2919. * In fact, in such a scenario, the drive will tend to treat
  2920. * the requests of each of these processes in about the same
  2921. * way as the requests of the others, and thus to provide
  2922. * each of these processes with about the same throughput
  2923. * (which is exactly the desired throughput distribution). In
  2924. * contrast, in any asymmetric scenario, device idling is
  2925. * certainly needed to guarantee that bfqq receives its
  2926. * assigned fraction of the device throughput (see [1] for
  2927. * details).
  2928. *
  2929. * We address this issue by controlling, actually, only the
  2930. * symmetry sub-condition (i), i.e., provided that
  2931. * sub-condition (i) holds, idling is not performed,
  2932. * regardless of whether sub-condition (ii) holds. In other
  2933. * words, only if sub-condition (i) holds, then idling is
  2934. * allowed, and the device tends to be prevented from queueing
  2935. * many requests, possibly of several processes. The reason
  2936. * for not controlling also sub-condition (ii) is that we
  2937. * exploit preemption to preserve guarantees in case of
  2938. * symmetric scenarios, even if (ii) does not hold, as
  2939. * explained in the next two paragraphs.
  2940. *
  2941. * Even if a queue, say Q, is expired when it remains idle, Q
  2942. * can still preempt the new in-service queue if the next
  2943. * request of Q arrives soon (see the comments on
  2944. * bfq_bfqq_update_budg_for_activation). If all queues and
  2945. * groups have the same weight, this form of preemption,
  2946. * combined with the hole-recovery heuristic described in the
  2947. * comments on function bfq_bfqq_update_budg_for_activation,
  2948. * are enough to preserve a correct bandwidth distribution in
  2949. * the mid term, even without idling. In fact, even if not
  2950. * idling allows the internal queues of the device to contain
  2951. * many requests, and thus to reorder requests, we can rather
  2952. * safely assume that the internal scheduler still preserves a
  2953. * minimum of mid-term fairness. The motivation for using
  2954. * preemption instead of idling is that, by not idling,
  2955. * service guarantees are preserved without minimally
  2956. * sacrificing throughput. In other words, both a high
  2957. * throughput and its desired distribution are obtained.
  2958. *
  2959. * More precisely, this preemption-based, idleless approach
  2960. * provides fairness in terms of IOPS, and not sectors per
  2961. * second. This can be seen with a simple example. Suppose
  2962. * that there are two queues with the same weight, but that
  2963. * the first queue receives requests of 8 sectors, while the
  2964. * second queue receives requests of 1024 sectors. In
  2965. * addition, suppose that each of the two queues contains at
  2966. * most one request at a time, which implies that each queue
  2967. * always remains idle after it is served. Finally, after
  2968. * remaining idle, each queue receives very quickly a new
  2969. * request. It follows that the two queues are served
  2970. * alternatively, preempting each other if needed. This
  2971. * implies that, although both queues have the same weight,
  2972. * the queue with large requests receives a service that is
  2973. * 1024/8 times as high as the service received by the other
  2974. * queue.
  2975. *
  2976. * On the other hand, device idling is performed, and thus
  2977. * pure sector-domain guarantees are provided, for the
  2978. * following queues, which are likely to need stronger
  2979. * throughput guarantees: weight-raised queues, and queues
  2980. * with a higher weight than other queues. When such queues
  2981. * are active, sub-condition (i) is false, which triggers
  2982. * device idling.
  2983. *
  2984. * According to the above considerations, the next variable is
  2985. * true (only) if sub-condition (i) holds. To compute the
  2986. * value of this variable, we not only use the return value of
  2987. * the function bfq_symmetric_scenario(), but also check
  2988. * whether bfqq is being weight-raised, because
  2989. * bfq_symmetric_scenario() does not take into account also
  2990. * weight-raised queues (see comments on
  2991. * bfq_weights_tree_add()). In particular, if bfqq is being
  2992. * weight-raised, it is important to idle only if there are
  2993. * other, non-weight-raised queues that may steal throughput
  2994. * to bfqq. Actually, we should be even more precise, and
  2995. * differentiate between interactive weight raising and
  2996. * soft real-time weight raising.
  2997. *
  2998. * As a side note, it is worth considering that the above
  2999. * device-idling countermeasures may however fail in the
  3000. * following unlucky scenario: if idling is (correctly)
  3001. * disabled in a time period during which all symmetry
  3002. * sub-conditions hold, and hence the device is allowed to
  3003. * enqueue many requests, but at some later point in time some
  3004. * sub-condition stops to hold, then it may become impossible
  3005. * to let requests be served in the desired order until all
  3006. * the requests already queued in the device have been served.
  3007. */
  3008. asymmetric_scenario = (bfqq->wr_coeff > 1 &&
  3009. bfqd->wr_busy_queues < bfqd->busy_queues) ||
  3010. !bfq_symmetric_scenario(bfqd);
  3011. /*
  3012. * Finally, there is a case where maximizing throughput is the
  3013. * best choice even if it may cause unfairness toward
  3014. * bfqq. Such a case is when bfqq became active in a burst of
  3015. * queue activations. Queues that became active during a large
  3016. * burst benefit only from throughput, as discussed in the
  3017. * comments on bfq_handle_burst. Thus, if bfqq became active
  3018. * in a burst and not idling the device maximizes throughput,
  3019. * then the device must no be idled, because not idling the
  3020. * device provides bfqq and all other queues in the burst with
  3021. * maximum benefit. Combining this and the above case, we can
  3022. * now establish when idling is actually needed to preserve
  3023. * service guarantees.
  3024. */
  3025. idling_needed_for_service_guarantees =
  3026. asymmetric_scenario && !bfq_bfqq_in_large_burst(bfqq);
  3027. /*
  3028. * We have now all the components we need to compute the
  3029. * return value of the function, which is true only if idling
  3030. * either boosts the throughput (without issues), or is
  3031. * necessary to preserve service guarantees.
  3032. */
  3033. return idling_boosts_thr_without_issues ||
  3034. idling_needed_for_service_guarantees;
  3035. }
  3036. /*
  3037. * If the in-service queue is empty but the function bfq_bfqq_may_idle
  3038. * returns true, then:
  3039. * 1) the queue must remain in service and cannot be expired, and
  3040. * 2) the device must be idled to wait for the possible arrival of a new
  3041. * request for the queue.
  3042. * See the comments on the function bfq_bfqq_may_idle for the reasons
  3043. * why performing device idling is the best choice to boost the throughput
  3044. * and preserve service guarantees when bfq_bfqq_may_idle itself
  3045. * returns true.
  3046. */
  3047. static bool bfq_bfqq_must_idle(struct bfq_queue *bfqq)
  3048. {
  3049. return RB_EMPTY_ROOT(&bfqq->sort_list) && bfq_bfqq_may_idle(bfqq);
  3050. }
  3051. /*
  3052. * Select a queue for service. If we have a current queue in service,
  3053. * check whether to continue servicing it, or retrieve and set a new one.
  3054. */
  3055. static struct bfq_queue *bfq_select_queue(struct bfq_data *bfqd)
  3056. {
  3057. struct bfq_queue *bfqq;
  3058. struct request *next_rq;
  3059. enum bfqq_expiration reason = BFQQE_BUDGET_TIMEOUT;
  3060. bfqq = bfqd->in_service_queue;
  3061. if (!bfqq)
  3062. goto new_queue;
  3063. bfq_log_bfqq(bfqd, bfqq, "select_queue: already in-service queue");
  3064. if (bfq_may_expire_for_budg_timeout(bfqq) &&
  3065. !bfq_bfqq_wait_request(bfqq) &&
  3066. !bfq_bfqq_must_idle(bfqq))
  3067. goto expire;
  3068. check_queue:
  3069. /*
  3070. * This loop is rarely executed more than once. Even when it
  3071. * happens, it is much more convenient to re-execute this loop
  3072. * than to return NULL and trigger a new dispatch to get a
  3073. * request served.
  3074. */
  3075. next_rq = bfqq->next_rq;
  3076. /*
  3077. * If bfqq has requests queued and it has enough budget left to
  3078. * serve them, keep the queue, otherwise expire it.
  3079. */
  3080. if (next_rq) {
  3081. if (bfq_serv_to_charge(next_rq, bfqq) >
  3082. bfq_bfqq_budget_left(bfqq)) {
  3083. /*
  3084. * Expire the queue for budget exhaustion,
  3085. * which makes sure that the next budget is
  3086. * enough to serve the next request, even if
  3087. * it comes from the fifo expired path.
  3088. */
  3089. reason = BFQQE_BUDGET_EXHAUSTED;
  3090. goto expire;
  3091. } else {
  3092. /*
  3093. * The idle timer may be pending because we may
  3094. * not disable disk idling even when a new request
  3095. * arrives.
  3096. */
  3097. if (bfq_bfqq_wait_request(bfqq)) {
  3098. /*
  3099. * If we get here: 1) at least a new request
  3100. * has arrived but we have not disabled the
  3101. * timer because the request was too small,
  3102. * 2) then the block layer has unplugged
  3103. * the device, causing the dispatch to be
  3104. * invoked.
  3105. *
  3106. * Since the device is unplugged, now the
  3107. * requests are probably large enough to
  3108. * provide a reasonable throughput.
  3109. * So we disable idling.
  3110. */
  3111. bfq_clear_bfqq_wait_request(bfqq);
  3112. hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
  3113. bfqg_stats_update_idle_time(bfqq_group(bfqq));
  3114. }
  3115. goto keep_queue;
  3116. }
  3117. }
  3118. /*
  3119. * No requests pending. However, if the in-service queue is idling
  3120. * for a new request, or has requests waiting for a completion and
  3121. * may idle after their completion, then keep it anyway.
  3122. */
  3123. if (bfq_bfqq_wait_request(bfqq) ||
  3124. (bfqq->dispatched != 0 && bfq_bfqq_may_idle(bfqq))) {
  3125. bfqq = NULL;
  3126. goto keep_queue;
  3127. }
  3128. reason = BFQQE_NO_MORE_REQUESTS;
  3129. expire:
  3130. bfq_bfqq_expire(bfqd, bfqq, false, reason);
  3131. new_queue:
  3132. bfqq = bfq_set_in_service_queue(bfqd);
  3133. if (bfqq) {
  3134. bfq_log_bfqq(bfqd, bfqq, "select_queue: checking new queue");
  3135. goto check_queue;
  3136. }
  3137. keep_queue:
  3138. if (bfqq)
  3139. bfq_log_bfqq(bfqd, bfqq, "select_queue: returned this queue");
  3140. else
  3141. bfq_log(bfqd, "select_queue: no queue returned");
  3142. return bfqq;
  3143. }
  3144. static void bfq_update_wr_data(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  3145. {
  3146. struct bfq_entity *entity = &bfqq->entity;
  3147. if (bfqq->wr_coeff > 1) { /* queue is being weight-raised */
  3148. bfq_log_bfqq(bfqd, bfqq,
  3149. "raising period dur %u/%u msec, old coeff %u, w %d(%d)",
  3150. jiffies_to_msecs(jiffies - bfqq->last_wr_start_finish),
  3151. jiffies_to_msecs(bfqq->wr_cur_max_time),
  3152. bfqq->wr_coeff,
  3153. bfqq->entity.weight, bfqq->entity.orig_weight);
  3154. if (entity->prio_changed)
  3155. bfq_log_bfqq(bfqd, bfqq, "WARN: pending prio change");
  3156. /*
  3157. * If the queue was activated in a burst, or too much
  3158. * time has elapsed from the beginning of this
  3159. * weight-raising period, then end weight raising.
  3160. */
  3161. if (bfq_bfqq_in_large_burst(bfqq))
  3162. bfq_bfqq_end_wr(bfqq);
  3163. else if (time_is_before_jiffies(bfqq->last_wr_start_finish +
  3164. bfqq->wr_cur_max_time)) {
  3165. if (bfqq->wr_cur_max_time != bfqd->bfq_wr_rt_max_time ||
  3166. time_is_before_jiffies(bfqq->wr_start_at_switch_to_srt +
  3167. bfq_wr_duration(bfqd)))
  3168. bfq_bfqq_end_wr(bfqq);
  3169. else {
  3170. /* switch back to interactive wr */
  3171. bfqq->wr_coeff = bfqd->bfq_wr_coeff;
  3172. bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
  3173. bfqq->last_wr_start_finish =
  3174. bfqq->wr_start_at_switch_to_srt;
  3175. bfqq->entity.prio_changed = 1;
  3176. }
  3177. }
  3178. }
  3179. /*
  3180. * To improve latency (for this or other queues), immediately
  3181. * update weight both if it must be raised and if it must be
  3182. * lowered. Since, entity may be on some active tree here, and
  3183. * might have a pending change of its ioprio class, invoke
  3184. * next function with the last parameter unset (see the
  3185. * comments on the function).
  3186. */
  3187. if ((entity->weight > entity->orig_weight) != (bfqq->wr_coeff > 1))
  3188. __bfq_entity_update_weight_prio(bfq_entity_service_tree(entity),
  3189. entity, false);
  3190. }
  3191. /*
  3192. * Dispatch next request from bfqq.
  3193. */
  3194. static struct request *bfq_dispatch_rq_from_bfqq(struct bfq_data *bfqd,
  3195. struct bfq_queue *bfqq)
  3196. {
  3197. struct request *rq = bfqq->next_rq;
  3198. unsigned long service_to_charge;
  3199. service_to_charge = bfq_serv_to_charge(rq, bfqq);
  3200. bfq_bfqq_served(bfqq, service_to_charge);
  3201. bfq_dispatch_remove(bfqd->queue, rq);
  3202. /*
  3203. * If weight raising has to terminate for bfqq, then next
  3204. * function causes an immediate update of bfqq's weight,
  3205. * without waiting for next activation. As a consequence, on
  3206. * expiration, bfqq will be timestamped as if has never been
  3207. * weight-raised during this service slot, even if it has
  3208. * received part or even most of the service as a
  3209. * weight-raised queue. This inflates bfqq's timestamps, which
  3210. * is beneficial, as bfqq is then more willing to leave the
  3211. * device immediately to possible other weight-raised queues.
  3212. */
  3213. bfq_update_wr_data(bfqd, bfqq);
  3214. /*
  3215. * Expire bfqq, pretending that its budget expired, if bfqq
  3216. * belongs to CLASS_IDLE and other queues are waiting for
  3217. * service.
  3218. */
  3219. if (bfqd->busy_queues > 1 && bfq_class_idle(bfqq))
  3220. goto expire;
  3221. return rq;
  3222. expire:
  3223. bfq_bfqq_expire(bfqd, bfqq, false, BFQQE_BUDGET_EXHAUSTED);
  3224. return rq;
  3225. }
  3226. static bool bfq_has_work(struct blk_mq_hw_ctx *hctx)
  3227. {
  3228. struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
  3229. /*
  3230. * Avoiding lock: a race on bfqd->busy_queues should cause at
  3231. * most a call to dispatch for nothing
  3232. */
  3233. return !list_empty_careful(&bfqd->dispatch) ||
  3234. bfqd->busy_queues > 0;
  3235. }
  3236. static struct request *__bfq_dispatch_request(struct blk_mq_hw_ctx *hctx)
  3237. {
  3238. struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
  3239. struct request *rq = NULL;
  3240. struct bfq_queue *bfqq = NULL;
  3241. if (!list_empty(&bfqd->dispatch)) {
  3242. rq = list_first_entry(&bfqd->dispatch, struct request,
  3243. queuelist);
  3244. list_del_init(&rq->queuelist);
  3245. bfqq = RQ_BFQQ(rq);
  3246. if (bfqq) {
  3247. /*
  3248. * Increment counters here, because this
  3249. * dispatch does not follow the standard
  3250. * dispatch flow (where counters are
  3251. * incremented)
  3252. */
  3253. bfqq->dispatched++;
  3254. goto inc_in_driver_start_rq;
  3255. }
  3256. /*
  3257. * We exploit the put_rq_private hook to decrement
  3258. * rq_in_driver, but put_rq_private will not be
  3259. * invoked on this request. So, to avoid unbalance,
  3260. * just start this request, without incrementing
  3261. * rq_in_driver. As a negative consequence,
  3262. * rq_in_driver is deceptively lower than it should be
  3263. * while this request is in service. This may cause
  3264. * bfq_schedule_dispatch to be invoked uselessly.
  3265. *
  3266. * As for implementing an exact solution, the
  3267. * put_request hook, if defined, is probably invoked
  3268. * also on this request. So, by exploiting this hook,
  3269. * we could 1) increment rq_in_driver here, and 2)
  3270. * decrement it in put_request. Such a solution would
  3271. * let the value of the counter be always accurate,
  3272. * but it would entail using an extra interface
  3273. * function. This cost seems higher than the benefit,
  3274. * being the frequency of non-elevator-private
  3275. * requests very low.
  3276. */
  3277. goto start_rq;
  3278. }
  3279. bfq_log(bfqd, "dispatch requests: %d busy queues", bfqd->busy_queues);
  3280. if (bfqd->busy_queues == 0)
  3281. goto exit;
  3282. /*
  3283. * Force device to serve one request at a time if
  3284. * strict_guarantees is true. Forcing this service scheme is
  3285. * currently the ONLY way to guarantee that the request
  3286. * service order enforced by the scheduler is respected by a
  3287. * queueing device. Otherwise the device is free even to make
  3288. * some unlucky request wait for as long as the device
  3289. * wishes.
  3290. *
  3291. * Of course, serving one request at at time may cause loss of
  3292. * throughput.
  3293. */
  3294. if (bfqd->strict_guarantees && bfqd->rq_in_driver > 0)
  3295. goto exit;
  3296. bfqq = bfq_select_queue(bfqd);
  3297. if (!bfqq)
  3298. goto exit;
  3299. rq = bfq_dispatch_rq_from_bfqq(bfqd, bfqq);
  3300. if (rq) {
  3301. inc_in_driver_start_rq:
  3302. bfqd->rq_in_driver++;
  3303. start_rq:
  3304. rq->rq_flags |= RQF_STARTED;
  3305. }
  3306. exit:
  3307. return rq;
  3308. }
  3309. static struct request *bfq_dispatch_request(struct blk_mq_hw_ctx *hctx)
  3310. {
  3311. struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
  3312. struct request *rq;
  3313. spin_lock_irq(&bfqd->lock);
  3314. rq = __bfq_dispatch_request(hctx);
  3315. spin_unlock_irq(&bfqd->lock);
  3316. return rq;
  3317. }
  3318. /*
  3319. * Task holds one reference to the queue, dropped when task exits. Each rq
  3320. * in-flight on this queue also holds a reference, dropped when rq is freed.
  3321. *
  3322. * Scheduler lock must be held here. Recall not to use bfqq after calling
  3323. * this function on it.
  3324. */
  3325. void bfq_put_queue(struct bfq_queue *bfqq)
  3326. {
  3327. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  3328. struct bfq_group *bfqg = bfqq_group(bfqq);
  3329. #endif
  3330. if (bfqq->bfqd)
  3331. bfq_log_bfqq(bfqq->bfqd, bfqq, "put_queue: %p %d",
  3332. bfqq, bfqq->ref);
  3333. bfqq->ref--;
  3334. if (bfqq->ref)
  3335. return;
  3336. if (bfq_bfqq_sync(bfqq))
  3337. /*
  3338. * The fact that this queue is being destroyed does not
  3339. * invalidate the fact that this queue may have been
  3340. * activated during the current burst. As a consequence,
  3341. * although the queue does not exist anymore, and hence
  3342. * needs to be removed from the burst list if there,
  3343. * the burst size has not to be decremented.
  3344. */
  3345. hlist_del_init(&bfqq->burst_list_node);
  3346. kmem_cache_free(bfq_pool, bfqq);
  3347. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  3348. bfqg_and_blkg_put(bfqg);
  3349. #endif
  3350. }
  3351. static void bfq_put_cooperator(struct bfq_queue *bfqq)
  3352. {
  3353. struct bfq_queue *__bfqq, *next;
  3354. /*
  3355. * If this queue was scheduled to merge with another queue, be
  3356. * sure to drop the reference taken on that queue (and others in
  3357. * the merge chain). See bfq_setup_merge and bfq_merge_bfqqs.
  3358. */
  3359. __bfqq = bfqq->new_bfqq;
  3360. while (__bfqq) {
  3361. if (__bfqq == bfqq)
  3362. break;
  3363. next = __bfqq->new_bfqq;
  3364. bfq_put_queue(__bfqq);
  3365. __bfqq = next;
  3366. }
  3367. }
  3368. static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  3369. {
  3370. if (bfqq == bfqd->in_service_queue) {
  3371. __bfq_bfqq_expire(bfqd, bfqq);
  3372. bfq_schedule_dispatch(bfqd);
  3373. }
  3374. bfq_log_bfqq(bfqd, bfqq, "exit_bfqq: %p, %d", bfqq, bfqq->ref);
  3375. bfq_put_cooperator(bfqq);
  3376. bfq_put_queue(bfqq); /* release process reference */
  3377. }
  3378. static void bfq_exit_icq_bfqq(struct bfq_io_cq *bic, bool is_sync)
  3379. {
  3380. struct bfq_queue *bfqq = bic_to_bfqq(bic, is_sync);
  3381. struct bfq_data *bfqd;
  3382. if (bfqq)
  3383. bfqd = bfqq->bfqd; /* NULL if scheduler already exited */
  3384. if (bfqq && bfqd) {
  3385. unsigned long flags;
  3386. spin_lock_irqsave(&bfqd->lock, flags);
  3387. bfqq->bic = NULL;
  3388. bfq_exit_bfqq(bfqd, bfqq);
  3389. bic_set_bfqq(bic, NULL, is_sync);
  3390. spin_unlock_irqrestore(&bfqd->lock, flags);
  3391. }
  3392. }
  3393. static void bfq_exit_icq(struct io_cq *icq)
  3394. {
  3395. struct bfq_io_cq *bic = icq_to_bic(icq);
  3396. bfq_exit_icq_bfqq(bic, true);
  3397. bfq_exit_icq_bfqq(bic, false);
  3398. }
  3399. /*
  3400. * Update the entity prio values; note that the new values will not
  3401. * be used until the next (re)activation.
  3402. */
  3403. static void
  3404. bfq_set_next_ioprio_data(struct bfq_queue *bfqq, struct bfq_io_cq *bic)
  3405. {
  3406. struct task_struct *tsk = current;
  3407. int ioprio_class;
  3408. struct bfq_data *bfqd = bfqq->bfqd;
  3409. if (!bfqd)
  3410. return;
  3411. ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio);
  3412. switch (ioprio_class) {
  3413. default:
  3414. dev_err(bfqq->bfqd->queue->backing_dev_info->dev,
  3415. "bfq: bad prio class %d\n", ioprio_class);
  3416. /* fall through */
  3417. case IOPRIO_CLASS_NONE:
  3418. /*
  3419. * No prio set, inherit CPU scheduling settings.
  3420. */
  3421. bfqq->new_ioprio = task_nice_ioprio(tsk);
  3422. bfqq->new_ioprio_class = task_nice_ioclass(tsk);
  3423. break;
  3424. case IOPRIO_CLASS_RT:
  3425. bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
  3426. bfqq->new_ioprio_class = IOPRIO_CLASS_RT;
  3427. break;
  3428. case IOPRIO_CLASS_BE:
  3429. bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
  3430. bfqq->new_ioprio_class = IOPRIO_CLASS_BE;
  3431. break;
  3432. case IOPRIO_CLASS_IDLE:
  3433. bfqq->new_ioprio_class = IOPRIO_CLASS_IDLE;
  3434. bfqq->new_ioprio = 7;
  3435. break;
  3436. }
  3437. if (bfqq->new_ioprio >= IOPRIO_BE_NR) {
  3438. pr_crit("bfq_set_next_ioprio_data: new_ioprio %d\n",
  3439. bfqq->new_ioprio);
  3440. bfqq->new_ioprio = IOPRIO_BE_NR;
  3441. }
  3442. bfqq->entity.new_weight = bfq_ioprio_to_weight(bfqq->new_ioprio);
  3443. bfqq->entity.prio_changed = 1;
  3444. }
  3445. static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
  3446. struct bio *bio, bool is_sync,
  3447. struct bfq_io_cq *bic);
  3448. static void bfq_check_ioprio_change(struct bfq_io_cq *bic, struct bio *bio)
  3449. {
  3450. struct bfq_data *bfqd = bic_to_bfqd(bic);
  3451. struct bfq_queue *bfqq;
  3452. int ioprio = bic->icq.ioc->ioprio;
  3453. /*
  3454. * This condition may trigger on a newly created bic, be sure to
  3455. * drop the lock before returning.
  3456. */
  3457. if (unlikely(!bfqd) || likely(bic->ioprio == ioprio))
  3458. return;
  3459. bic->ioprio = ioprio;
  3460. bfqq = bic_to_bfqq(bic, false);
  3461. if (bfqq) {
  3462. /* release process reference on this queue */
  3463. bfq_put_queue(bfqq);
  3464. bfqq = bfq_get_queue(bfqd, bio, BLK_RW_ASYNC, bic);
  3465. bic_set_bfqq(bic, bfqq, false);
  3466. }
  3467. bfqq = bic_to_bfqq(bic, true);
  3468. if (bfqq)
  3469. bfq_set_next_ioprio_data(bfqq, bic);
  3470. }
  3471. static void bfq_init_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  3472. struct bfq_io_cq *bic, pid_t pid, int is_sync)
  3473. {
  3474. RB_CLEAR_NODE(&bfqq->entity.rb_node);
  3475. INIT_LIST_HEAD(&bfqq->fifo);
  3476. INIT_HLIST_NODE(&bfqq->burst_list_node);
  3477. bfqq->ref = 0;
  3478. bfqq->bfqd = bfqd;
  3479. if (bic)
  3480. bfq_set_next_ioprio_data(bfqq, bic);
  3481. if (is_sync) {
  3482. /*
  3483. * No need to mark as has_short_ttime if in
  3484. * idle_class, because no device idling is performed
  3485. * for queues in idle class
  3486. */
  3487. if (!bfq_class_idle(bfqq))
  3488. /* tentatively mark as has_short_ttime */
  3489. bfq_mark_bfqq_has_short_ttime(bfqq);
  3490. bfq_mark_bfqq_sync(bfqq);
  3491. bfq_mark_bfqq_just_created(bfqq);
  3492. } else
  3493. bfq_clear_bfqq_sync(bfqq);
  3494. /* set end request to minus infinity from now */
  3495. bfqq->ttime.last_end_request = ktime_get_ns() + 1;
  3496. bfq_mark_bfqq_IO_bound(bfqq);
  3497. bfqq->pid = pid;
  3498. /* Tentative initial value to trade off between thr and lat */
  3499. bfqq->max_budget = (2 * bfq_max_budget(bfqd)) / 3;
  3500. bfqq->budget_timeout = bfq_smallest_from_now();
  3501. bfqq->wr_coeff = 1;
  3502. bfqq->last_wr_start_finish = jiffies;
  3503. bfqq->wr_start_at_switch_to_srt = bfq_smallest_from_now();
  3504. bfqq->split_time = bfq_smallest_from_now();
  3505. /*
  3506. * Set to the value for which bfqq will not be deemed as
  3507. * soft rt when it becomes backlogged.
  3508. */
  3509. bfqq->soft_rt_next_start = bfq_greatest_from_now();
  3510. /* first request is almost certainly seeky */
  3511. bfqq->seek_history = 1;
  3512. }
  3513. static struct bfq_queue **bfq_async_queue_prio(struct bfq_data *bfqd,
  3514. struct bfq_group *bfqg,
  3515. int ioprio_class, int ioprio)
  3516. {
  3517. switch (ioprio_class) {
  3518. case IOPRIO_CLASS_RT:
  3519. return &bfqg->async_bfqq[0][ioprio];
  3520. case IOPRIO_CLASS_NONE:
  3521. ioprio = IOPRIO_NORM;
  3522. /* fall through */
  3523. case IOPRIO_CLASS_BE:
  3524. return &bfqg->async_bfqq[1][ioprio];
  3525. case IOPRIO_CLASS_IDLE:
  3526. return &bfqg->async_idle_bfqq;
  3527. default:
  3528. return NULL;
  3529. }
  3530. }
  3531. static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
  3532. struct bio *bio, bool is_sync,
  3533. struct bfq_io_cq *bic)
  3534. {
  3535. const int ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
  3536. const int ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio);
  3537. struct bfq_queue **async_bfqq = NULL;
  3538. struct bfq_queue *bfqq;
  3539. struct bfq_group *bfqg;
  3540. rcu_read_lock();
  3541. bfqg = bfq_find_set_group(bfqd, bio_blkcg(bio));
  3542. if (!bfqg) {
  3543. bfqq = &bfqd->oom_bfqq;
  3544. goto out;
  3545. }
  3546. if (!is_sync) {
  3547. async_bfqq = bfq_async_queue_prio(bfqd, bfqg, ioprio_class,
  3548. ioprio);
  3549. bfqq = *async_bfqq;
  3550. if (bfqq)
  3551. goto out;
  3552. }
  3553. bfqq = kmem_cache_alloc_node(bfq_pool,
  3554. GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN,
  3555. bfqd->queue->node);
  3556. if (bfqq) {
  3557. bfq_init_bfqq(bfqd, bfqq, bic, current->pid,
  3558. is_sync);
  3559. bfq_init_entity(&bfqq->entity, bfqg);
  3560. bfq_log_bfqq(bfqd, bfqq, "allocated");
  3561. } else {
  3562. bfqq = &bfqd->oom_bfqq;
  3563. bfq_log_bfqq(bfqd, bfqq, "using oom bfqq");
  3564. goto out;
  3565. }
  3566. /*
  3567. * Pin the queue now that it's allocated, scheduler exit will
  3568. * prune it.
  3569. */
  3570. if (async_bfqq) {
  3571. bfqq->ref++; /*
  3572. * Extra group reference, w.r.t. sync
  3573. * queue. This extra reference is removed
  3574. * only if bfqq->bfqg disappears, to
  3575. * guarantee that this queue is not freed
  3576. * until its group goes away.
  3577. */
  3578. bfq_log_bfqq(bfqd, bfqq, "get_queue, bfqq not in async: %p, %d",
  3579. bfqq, bfqq->ref);
  3580. *async_bfqq = bfqq;
  3581. }
  3582. out:
  3583. bfqq->ref++; /* get a process reference to this queue */
  3584. bfq_log_bfqq(bfqd, bfqq, "get_queue, at end: %p, %d", bfqq, bfqq->ref);
  3585. rcu_read_unlock();
  3586. return bfqq;
  3587. }
  3588. static void bfq_update_io_thinktime(struct bfq_data *bfqd,
  3589. struct bfq_queue *bfqq)
  3590. {
  3591. struct bfq_ttime *ttime = &bfqq->ttime;
  3592. u64 elapsed = ktime_get_ns() - bfqq->ttime.last_end_request;
  3593. elapsed = min_t(u64, elapsed, 2ULL * bfqd->bfq_slice_idle);
  3594. ttime->ttime_samples = (7*bfqq->ttime.ttime_samples + 256) / 8;
  3595. ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8);
  3596. ttime->ttime_mean = div64_ul(ttime->ttime_total + 128,
  3597. ttime->ttime_samples);
  3598. }
  3599. static void
  3600. bfq_update_io_seektime(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  3601. struct request *rq)
  3602. {
  3603. bfqq->seek_history <<= 1;
  3604. bfqq->seek_history |=
  3605. get_sdist(bfqq->last_request_pos, rq) > BFQQ_SEEK_THR &&
  3606. (!blk_queue_nonrot(bfqd->queue) ||
  3607. blk_rq_sectors(rq) < BFQQ_SECT_THR_NONROT);
  3608. }
  3609. static void bfq_update_has_short_ttime(struct bfq_data *bfqd,
  3610. struct bfq_queue *bfqq,
  3611. struct bfq_io_cq *bic)
  3612. {
  3613. bool has_short_ttime = true;
  3614. /*
  3615. * No need to update has_short_ttime if bfqq is async or in
  3616. * idle io prio class, or if bfq_slice_idle is zero, because
  3617. * no device idling is performed for bfqq in this case.
  3618. */
  3619. if (!bfq_bfqq_sync(bfqq) || bfq_class_idle(bfqq) ||
  3620. bfqd->bfq_slice_idle == 0)
  3621. return;
  3622. /* Idle window just restored, statistics are meaningless. */
  3623. if (time_is_after_eq_jiffies(bfqq->split_time +
  3624. bfqd->bfq_wr_min_idle_time))
  3625. return;
  3626. /* Think time is infinite if no process is linked to
  3627. * bfqq. Otherwise check average think time to
  3628. * decide whether to mark as has_short_ttime
  3629. */
  3630. if (atomic_read(&bic->icq.ioc->active_ref) == 0 ||
  3631. (bfq_sample_valid(bfqq->ttime.ttime_samples) &&
  3632. bfqq->ttime.ttime_mean > bfqd->bfq_slice_idle))
  3633. has_short_ttime = false;
  3634. bfq_log_bfqq(bfqd, bfqq, "update_has_short_ttime: has_short_ttime %d",
  3635. has_short_ttime);
  3636. if (has_short_ttime)
  3637. bfq_mark_bfqq_has_short_ttime(bfqq);
  3638. else
  3639. bfq_clear_bfqq_has_short_ttime(bfqq);
  3640. }
  3641. /*
  3642. * Called when a new fs request (rq) is added to bfqq. Check if there's
  3643. * something we should do about it.
  3644. */
  3645. static void bfq_rq_enqueued(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  3646. struct request *rq)
  3647. {
  3648. struct bfq_io_cq *bic = RQ_BIC(rq);
  3649. if (rq->cmd_flags & REQ_META)
  3650. bfqq->meta_pending++;
  3651. bfq_update_io_thinktime(bfqd, bfqq);
  3652. bfq_update_has_short_ttime(bfqd, bfqq, bic);
  3653. bfq_update_io_seektime(bfqd, bfqq, rq);
  3654. bfq_log_bfqq(bfqd, bfqq,
  3655. "rq_enqueued: has_short_ttime=%d (seeky %d)",
  3656. bfq_bfqq_has_short_ttime(bfqq), BFQQ_SEEKY(bfqq));
  3657. bfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
  3658. if (bfqq == bfqd->in_service_queue && bfq_bfqq_wait_request(bfqq)) {
  3659. bool small_req = bfqq->queued[rq_is_sync(rq)] == 1 &&
  3660. blk_rq_sectors(rq) < 32;
  3661. bool budget_timeout = bfq_bfqq_budget_timeout(bfqq);
  3662. /*
  3663. * There is just this request queued: if the request
  3664. * is small and the queue is not to be expired, then
  3665. * just exit.
  3666. *
  3667. * In this way, if the device is being idled to wait
  3668. * for a new request from the in-service queue, we
  3669. * avoid unplugging the device and committing the
  3670. * device to serve just a small request. On the
  3671. * contrary, we wait for the block layer to decide
  3672. * when to unplug the device: hopefully, new requests
  3673. * will be merged to this one quickly, then the device
  3674. * will be unplugged and larger requests will be
  3675. * dispatched.
  3676. */
  3677. if (small_req && !budget_timeout)
  3678. return;
  3679. /*
  3680. * A large enough request arrived, or the queue is to
  3681. * be expired: in both cases disk idling is to be
  3682. * stopped, so clear wait_request flag and reset
  3683. * timer.
  3684. */
  3685. bfq_clear_bfqq_wait_request(bfqq);
  3686. hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
  3687. bfqg_stats_update_idle_time(bfqq_group(bfqq));
  3688. /*
  3689. * The queue is not empty, because a new request just
  3690. * arrived. Hence we can safely expire the queue, in
  3691. * case of budget timeout, without risking that the
  3692. * timestamps of the queue are not updated correctly.
  3693. * See [1] for more details.
  3694. */
  3695. if (budget_timeout)
  3696. bfq_bfqq_expire(bfqd, bfqq, false,
  3697. BFQQE_BUDGET_TIMEOUT);
  3698. }
  3699. }
  3700. static void __bfq_insert_request(struct bfq_data *bfqd, struct request *rq)
  3701. {
  3702. struct bfq_queue *bfqq = RQ_BFQQ(rq),
  3703. *new_bfqq = bfq_setup_cooperator(bfqd, bfqq, rq, true);
  3704. if (new_bfqq) {
  3705. if (bic_to_bfqq(RQ_BIC(rq), 1) != bfqq)
  3706. new_bfqq = bic_to_bfqq(RQ_BIC(rq), 1);
  3707. /*
  3708. * Release the request's reference to the old bfqq
  3709. * and make sure one is taken to the shared queue.
  3710. */
  3711. new_bfqq->allocated++;
  3712. bfqq->allocated--;
  3713. new_bfqq->ref++;
  3714. bfq_clear_bfqq_just_created(bfqq);
  3715. /*
  3716. * If the bic associated with the process
  3717. * issuing this request still points to bfqq
  3718. * (and thus has not been already redirected
  3719. * to new_bfqq or even some other bfq_queue),
  3720. * then complete the merge and redirect it to
  3721. * new_bfqq.
  3722. */
  3723. if (bic_to_bfqq(RQ_BIC(rq), 1) == bfqq)
  3724. bfq_merge_bfqqs(bfqd, RQ_BIC(rq),
  3725. bfqq, new_bfqq);
  3726. /*
  3727. * rq is about to be enqueued into new_bfqq,
  3728. * release rq reference on bfqq
  3729. */
  3730. bfq_put_queue(bfqq);
  3731. rq->elv.priv[1] = new_bfqq;
  3732. bfqq = new_bfqq;
  3733. }
  3734. bfq_add_request(rq);
  3735. rq->fifo_time = ktime_get_ns() + bfqd->bfq_fifo_expire[rq_is_sync(rq)];
  3736. list_add_tail(&rq->queuelist, &bfqq->fifo);
  3737. bfq_rq_enqueued(bfqd, bfqq, rq);
  3738. }
  3739. static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
  3740. bool at_head)
  3741. {
  3742. struct request_queue *q = hctx->queue;
  3743. struct bfq_data *bfqd = q->elevator->elevator_data;
  3744. spin_lock_irq(&bfqd->lock);
  3745. if (blk_mq_sched_try_insert_merge(q, rq)) {
  3746. spin_unlock_irq(&bfqd->lock);
  3747. return;
  3748. }
  3749. spin_unlock_irq(&bfqd->lock);
  3750. blk_mq_sched_request_inserted(rq);
  3751. spin_lock_irq(&bfqd->lock);
  3752. if (at_head || blk_rq_is_passthrough(rq)) {
  3753. if (at_head)
  3754. list_add(&rq->queuelist, &bfqd->dispatch);
  3755. else
  3756. list_add_tail(&rq->queuelist, &bfqd->dispatch);
  3757. } else {
  3758. __bfq_insert_request(bfqd, rq);
  3759. if (rq_mergeable(rq)) {
  3760. elv_rqhash_add(q, rq);
  3761. if (!q->last_merge)
  3762. q->last_merge = rq;
  3763. }
  3764. }
  3765. spin_unlock_irq(&bfqd->lock);
  3766. }
  3767. static void bfq_insert_requests(struct blk_mq_hw_ctx *hctx,
  3768. struct list_head *list, bool at_head)
  3769. {
  3770. while (!list_empty(list)) {
  3771. struct request *rq;
  3772. rq = list_first_entry(list, struct request, queuelist);
  3773. list_del_init(&rq->queuelist);
  3774. bfq_insert_request(hctx, rq, at_head);
  3775. }
  3776. }
  3777. static void bfq_update_hw_tag(struct bfq_data *bfqd)
  3778. {
  3779. bfqd->max_rq_in_driver = max_t(int, bfqd->max_rq_in_driver,
  3780. bfqd->rq_in_driver);
  3781. if (bfqd->hw_tag == 1)
  3782. return;
  3783. /*
  3784. * This sample is valid if the number of outstanding requests
  3785. * is large enough to allow a queueing behavior. Note that the
  3786. * sum is not exact, as it's not taking into account deactivated
  3787. * requests.
  3788. */
  3789. if (bfqd->rq_in_driver + bfqd->queued < BFQ_HW_QUEUE_THRESHOLD)
  3790. return;
  3791. if (bfqd->hw_tag_samples++ < BFQ_HW_QUEUE_SAMPLES)
  3792. return;
  3793. bfqd->hw_tag = bfqd->max_rq_in_driver > BFQ_HW_QUEUE_THRESHOLD;
  3794. bfqd->max_rq_in_driver = 0;
  3795. bfqd->hw_tag_samples = 0;
  3796. }
  3797. static void bfq_completed_request(struct bfq_queue *bfqq, struct bfq_data *bfqd)
  3798. {
  3799. u64 now_ns;
  3800. u32 delta_us;
  3801. bfq_update_hw_tag(bfqd);
  3802. bfqd->rq_in_driver--;
  3803. bfqq->dispatched--;
  3804. if (!bfqq->dispatched && !bfq_bfqq_busy(bfqq)) {
  3805. /*
  3806. * Set budget_timeout (which we overload to store the
  3807. * time at which the queue remains with no backlog and
  3808. * no outstanding request; used by the weight-raising
  3809. * mechanism).
  3810. */
  3811. bfqq->budget_timeout = jiffies;
  3812. bfq_weights_tree_remove(bfqd, &bfqq->entity,
  3813. &bfqd->queue_weights_tree);
  3814. }
  3815. now_ns = ktime_get_ns();
  3816. bfqq->ttime.last_end_request = now_ns;
  3817. /*
  3818. * Using us instead of ns, to get a reasonable precision in
  3819. * computing rate in next check.
  3820. */
  3821. delta_us = div_u64(now_ns - bfqd->last_completion, NSEC_PER_USEC);
  3822. /*
  3823. * If the request took rather long to complete, and, according
  3824. * to the maximum request size recorded, this completion latency
  3825. * implies that the request was certainly served at a very low
  3826. * rate (less than 1M sectors/sec), then the whole observation
  3827. * interval that lasts up to this time instant cannot be a
  3828. * valid time interval for computing a new peak rate. Invoke
  3829. * bfq_update_rate_reset to have the following three steps
  3830. * taken:
  3831. * - close the observation interval at the last (previous)
  3832. * request dispatch or completion
  3833. * - compute rate, if possible, for that observation interval
  3834. * - reset to zero samples, which will trigger a proper
  3835. * re-initialization of the observation interval on next
  3836. * dispatch
  3837. */
  3838. if (delta_us > BFQ_MIN_TT/NSEC_PER_USEC &&
  3839. (bfqd->last_rq_max_size<<BFQ_RATE_SHIFT)/delta_us <
  3840. 1UL<<(BFQ_RATE_SHIFT - 10))
  3841. bfq_update_rate_reset(bfqd, NULL);
  3842. bfqd->last_completion = now_ns;
  3843. /*
  3844. * If we are waiting to discover whether the request pattern
  3845. * of the task associated with the queue is actually
  3846. * isochronous, and both requisites for this condition to hold
  3847. * are now satisfied, then compute soft_rt_next_start (see the
  3848. * comments on the function bfq_bfqq_softrt_next_start()). We
  3849. * schedule this delayed check when bfqq expires, if it still
  3850. * has in-flight requests.
  3851. */
  3852. if (bfq_bfqq_softrt_update(bfqq) && bfqq->dispatched == 0 &&
  3853. RB_EMPTY_ROOT(&bfqq->sort_list))
  3854. bfqq->soft_rt_next_start =
  3855. bfq_bfqq_softrt_next_start(bfqd, bfqq);
  3856. /*
  3857. * If this is the in-service queue, check if it needs to be expired,
  3858. * or if we want to idle in case it has no pending requests.
  3859. */
  3860. if (bfqd->in_service_queue == bfqq) {
  3861. if (bfqq->dispatched == 0 && bfq_bfqq_must_idle(bfqq)) {
  3862. bfq_arm_slice_timer(bfqd);
  3863. return;
  3864. } else if (bfq_may_expire_for_budg_timeout(bfqq))
  3865. bfq_bfqq_expire(bfqd, bfqq, false,
  3866. BFQQE_BUDGET_TIMEOUT);
  3867. else if (RB_EMPTY_ROOT(&bfqq->sort_list) &&
  3868. (bfqq->dispatched == 0 ||
  3869. !bfq_bfqq_may_idle(bfqq)))
  3870. bfq_bfqq_expire(bfqd, bfqq, false,
  3871. BFQQE_NO_MORE_REQUESTS);
  3872. }
  3873. if (!bfqd->rq_in_driver)
  3874. bfq_schedule_dispatch(bfqd);
  3875. }
  3876. static void bfq_put_rq_priv_body(struct bfq_queue *bfqq)
  3877. {
  3878. bfqq->allocated--;
  3879. bfq_put_queue(bfqq);
  3880. }
  3881. static void bfq_finish_request(struct request *rq)
  3882. {
  3883. struct bfq_queue *bfqq;
  3884. struct bfq_data *bfqd;
  3885. if (!rq->elv.icq)
  3886. return;
  3887. bfqq = RQ_BFQQ(rq);
  3888. bfqd = bfqq->bfqd;
  3889. if (rq->rq_flags & RQF_STARTED)
  3890. bfqg_stats_update_completion(bfqq_group(bfqq),
  3891. rq_start_time_ns(rq),
  3892. rq_io_start_time_ns(rq),
  3893. rq->cmd_flags);
  3894. if (likely(rq->rq_flags & RQF_STARTED)) {
  3895. unsigned long flags;
  3896. spin_lock_irqsave(&bfqd->lock, flags);
  3897. bfq_completed_request(bfqq, bfqd);
  3898. bfq_put_rq_priv_body(bfqq);
  3899. spin_unlock_irqrestore(&bfqd->lock, flags);
  3900. } else {
  3901. /*
  3902. * Request rq may be still/already in the scheduler,
  3903. * in which case we need to remove it. And we cannot
  3904. * defer such a check and removal, to avoid
  3905. * inconsistencies in the time interval from the end
  3906. * of this function to the start of the deferred work.
  3907. * This situation seems to occur only in process
  3908. * context, as a consequence of a merge. In the
  3909. * current version of the code, this implies that the
  3910. * lock is held.
  3911. */
  3912. if (!RB_EMPTY_NODE(&rq->rb_node))
  3913. bfq_remove_request(rq->q, rq);
  3914. bfq_put_rq_priv_body(bfqq);
  3915. }
  3916. rq->elv.priv[0] = NULL;
  3917. rq->elv.priv[1] = NULL;
  3918. }
  3919. /*
  3920. * Returns NULL if a new bfqq should be allocated, or the old bfqq if this
  3921. * was the last process referring to that bfqq.
  3922. */
  3923. static struct bfq_queue *
  3924. bfq_split_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq)
  3925. {
  3926. bfq_log_bfqq(bfqq->bfqd, bfqq, "splitting queue");
  3927. if (bfqq_process_refs(bfqq) == 1) {
  3928. bfqq->pid = current->pid;
  3929. bfq_clear_bfqq_coop(bfqq);
  3930. bfq_clear_bfqq_split_coop(bfqq);
  3931. return bfqq;
  3932. }
  3933. bic_set_bfqq(bic, NULL, 1);
  3934. bfq_put_cooperator(bfqq);
  3935. bfq_put_queue(bfqq);
  3936. return NULL;
  3937. }
  3938. static struct bfq_queue *bfq_get_bfqq_handle_split(struct bfq_data *bfqd,
  3939. struct bfq_io_cq *bic,
  3940. struct bio *bio,
  3941. bool split, bool is_sync,
  3942. bool *new_queue)
  3943. {
  3944. struct bfq_queue *bfqq = bic_to_bfqq(bic, is_sync);
  3945. if (likely(bfqq && bfqq != &bfqd->oom_bfqq))
  3946. return bfqq;
  3947. if (new_queue)
  3948. *new_queue = true;
  3949. if (bfqq)
  3950. bfq_put_queue(bfqq);
  3951. bfqq = bfq_get_queue(bfqd, bio, is_sync, bic);
  3952. bic_set_bfqq(bic, bfqq, is_sync);
  3953. if (split && is_sync) {
  3954. if ((bic->was_in_burst_list && bfqd->large_burst) ||
  3955. bic->saved_in_large_burst)
  3956. bfq_mark_bfqq_in_large_burst(bfqq);
  3957. else {
  3958. bfq_clear_bfqq_in_large_burst(bfqq);
  3959. if (bic->was_in_burst_list)
  3960. hlist_add_head(&bfqq->burst_list_node,
  3961. &bfqd->burst_list);
  3962. }
  3963. bfqq->split_time = jiffies;
  3964. }
  3965. return bfqq;
  3966. }
  3967. /*
  3968. * Allocate bfq data structures associated with this request.
  3969. */
  3970. static void bfq_prepare_request(struct request *rq, struct bio *bio)
  3971. {
  3972. struct request_queue *q = rq->q;
  3973. struct bfq_data *bfqd = q->elevator->elevator_data;
  3974. struct bfq_io_cq *bic;
  3975. const int is_sync = rq_is_sync(rq);
  3976. struct bfq_queue *bfqq;
  3977. bool new_queue = false;
  3978. bool bfqq_already_existing = false, split = false;
  3979. /*
  3980. * Even if we don't have an icq attached, we should still clear
  3981. * the scheduler pointers, as they might point to previously
  3982. * allocated bic/bfqq structs.
  3983. */
  3984. if (!rq->elv.icq) {
  3985. rq->elv.priv[0] = rq->elv.priv[1] = NULL;
  3986. return;
  3987. }
  3988. bic = icq_to_bic(rq->elv.icq);
  3989. spin_lock_irq(&bfqd->lock);
  3990. bfq_check_ioprio_change(bic, bio);
  3991. bfq_bic_update_cgroup(bic, bio);
  3992. bfqq = bfq_get_bfqq_handle_split(bfqd, bic, bio, false, is_sync,
  3993. &new_queue);
  3994. if (likely(!new_queue)) {
  3995. /* If the queue was seeky for too long, break it apart. */
  3996. if (bfq_bfqq_coop(bfqq) && bfq_bfqq_split_coop(bfqq)) {
  3997. bfq_log_bfqq(bfqd, bfqq, "breaking apart bfqq");
  3998. /* Update bic before losing reference to bfqq */
  3999. if (bfq_bfqq_in_large_burst(bfqq))
  4000. bic->saved_in_large_burst = true;
  4001. bfqq = bfq_split_bfqq(bic, bfqq);
  4002. split = true;
  4003. if (!bfqq)
  4004. bfqq = bfq_get_bfqq_handle_split(bfqd, bic, bio,
  4005. true, is_sync,
  4006. NULL);
  4007. else
  4008. bfqq_already_existing = true;
  4009. }
  4010. }
  4011. bfqq->allocated++;
  4012. bfqq->ref++;
  4013. bfq_log_bfqq(bfqd, bfqq, "get_request %p: bfqq %p, %d",
  4014. rq, bfqq, bfqq->ref);
  4015. rq->elv.priv[0] = bic;
  4016. rq->elv.priv[1] = bfqq;
  4017. /*
  4018. * If a bfq_queue has only one process reference, it is owned
  4019. * by only this bic: we can then set bfqq->bic = bic. in
  4020. * addition, if the queue has also just been split, we have to
  4021. * resume its state.
  4022. */
  4023. if (likely(bfqq != &bfqd->oom_bfqq) && bfqq_process_refs(bfqq) == 1) {
  4024. bfqq->bic = bic;
  4025. if (split) {
  4026. /*
  4027. * The queue has just been split from a shared
  4028. * queue: restore the idle window and the
  4029. * possible weight raising period.
  4030. */
  4031. bfq_bfqq_resume_state(bfqq, bfqd, bic,
  4032. bfqq_already_existing);
  4033. }
  4034. }
  4035. if (unlikely(bfq_bfqq_just_created(bfqq)))
  4036. bfq_handle_burst(bfqd, bfqq);
  4037. spin_unlock_irq(&bfqd->lock);
  4038. }
  4039. static void
  4040. bfq_idle_slice_timer_body(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  4041. {
  4042. enum bfqq_expiration reason;
  4043. unsigned long flags;
  4044. spin_lock_irqsave(&bfqd->lock, flags);
  4045. /*
  4046. * Considering that bfqq may be in race, we should firstly check
  4047. * whether bfqq is in service before doing something on it. If
  4048. * the bfqq in race is not in service, it has already been expired
  4049. * through __bfq_bfqq_expire func and its wait_request flags has
  4050. * been cleared in __bfq_bfqd_reset_in_service func.
  4051. */
  4052. if (bfqq != bfqd->in_service_queue) {
  4053. spin_unlock_irqrestore(&bfqd->lock, flags);
  4054. return;
  4055. }
  4056. bfq_clear_bfqq_wait_request(bfqq);
  4057. if (bfq_bfqq_budget_timeout(bfqq))
  4058. /*
  4059. * Also here the queue can be safely expired
  4060. * for budget timeout without wasting
  4061. * guarantees
  4062. */
  4063. reason = BFQQE_BUDGET_TIMEOUT;
  4064. else if (bfqq->queued[0] == 0 && bfqq->queued[1] == 0)
  4065. /*
  4066. * The queue may not be empty upon timer expiration,
  4067. * because we may not disable the timer when the
  4068. * first request of the in-service queue arrives
  4069. * during disk idling.
  4070. */
  4071. reason = BFQQE_TOO_IDLE;
  4072. else
  4073. goto schedule_dispatch;
  4074. bfq_bfqq_expire(bfqd, bfqq, true, reason);
  4075. schedule_dispatch:
  4076. spin_unlock_irqrestore(&bfqd->lock, flags);
  4077. bfq_schedule_dispatch(bfqd);
  4078. }
  4079. /*
  4080. * Handler of the expiration of the timer running if the in-service queue
  4081. * is idling inside its time slice.
  4082. */
  4083. static enum hrtimer_restart bfq_idle_slice_timer(struct hrtimer *timer)
  4084. {
  4085. struct bfq_data *bfqd = container_of(timer, struct bfq_data,
  4086. idle_slice_timer);
  4087. struct bfq_queue *bfqq = bfqd->in_service_queue;
  4088. /*
  4089. * Theoretical race here: the in-service queue can be NULL or
  4090. * different from the queue that was idling if a new request
  4091. * arrives for the current queue and there is a full dispatch
  4092. * cycle that changes the in-service queue. This can hardly
  4093. * happen, but in the worst case we just expire a queue too
  4094. * early.
  4095. */
  4096. if (bfqq)
  4097. bfq_idle_slice_timer_body(bfqd, bfqq);
  4098. return HRTIMER_NORESTART;
  4099. }
  4100. static void __bfq_put_async_bfqq(struct bfq_data *bfqd,
  4101. struct bfq_queue **bfqq_ptr)
  4102. {
  4103. struct bfq_queue *bfqq = *bfqq_ptr;
  4104. bfq_log(bfqd, "put_async_bfqq: %p", bfqq);
  4105. if (bfqq) {
  4106. bfq_bfqq_move(bfqd, bfqq, bfqd->root_group);
  4107. bfq_log_bfqq(bfqd, bfqq, "put_async_bfqq: putting %p, %d",
  4108. bfqq, bfqq->ref);
  4109. bfq_put_queue(bfqq);
  4110. *bfqq_ptr = NULL;
  4111. }
  4112. }
  4113. /*
  4114. * Release all the bfqg references to its async queues. If we are
  4115. * deallocating the group these queues may still contain requests, so
  4116. * we reparent them to the root cgroup (i.e., the only one that will
  4117. * exist for sure until all the requests on a device are gone).
  4118. */
  4119. void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg)
  4120. {
  4121. int i, j;
  4122. for (i = 0; i < 2; i++)
  4123. for (j = 0; j < IOPRIO_BE_NR; j++)
  4124. __bfq_put_async_bfqq(bfqd, &bfqg->async_bfqq[i][j]);
  4125. __bfq_put_async_bfqq(bfqd, &bfqg->async_idle_bfqq);
  4126. }
  4127. static void bfq_exit_queue(struct elevator_queue *e)
  4128. {
  4129. struct bfq_data *bfqd = e->elevator_data;
  4130. struct bfq_queue *bfqq, *n;
  4131. hrtimer_cancel(&bfqd->idle_slice_timer);
  4132. spin_lock_irq(&bfqd->lock);
  4133. list_for_each_entry_safe(bfqq, n, &bfqd->idle_list, bfqq_list)
  4134. bfq_deactivate_bfqq(bfqd, bfqq, false, false);
  4135. spin_unlock_irq(&bfqd->lock);
  4136. hrtimer_cancel(&bfqd->idle_slice_timer);
  4137. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  4138. blkcg_deactivate_policy(bfqd->queue, &blkcg_policy_bfq);
  4139. #else
  4140. spin_lock_irq(&bfqd->lock);
  4141. bfq_put_async_queues(bfqd, bfqd->root_group);
  4142. kfree(bfqd->root_group);
  4143. spin_unlock_irq(&bfqd->lock);
  4144. #endif
  4145. kfree(bfqd);
  4146. }
  4147. static void bfq_init_root_group(struct bfq_group *root_group,
  4148. struct bfq_data *bfqd)
  4149. {
  4150. int i;
  4151. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  4152. root_group->entity.parent = NULL;
  4153. root_group->my_entity = NULL;
  4154. root_group->bfqd = bfqd;
  4155. #endif
  4156. root_group->rq_pos_tree = RB_ROOT;
  4157. for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
  4158. root_group->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
  4159. root_group->sched_data.bfq_class_idle_last_service = jiffies;
  4160. }
  4161. static int bfq_init_queue(struct request_queue *q, struct elevator_type *e)
  4162. {
  4163. struct bfq_data *bfqd;
  4164. struct elevator_queue *eq;
  4165. eq = elevator_alloc(q, e);
  4166. if (!eq)
  4167. return -ENOMEM;
  4168. bfqd = kzalloc_node(sizeof(*bfqd), GFP_KERNEL, q->node);
  4169. if (!bfqd) {
  4170. kobject_put(&eq->kobj);
  4171. return -ENOMEM;
  4172. }
  4173. eq->elevator_data = bfqd;
  4174. spin_lock_irq(q->queue_lock);
  4175. q->elevator = eq;
  4176. spin_unlock_irq(q->queue_lock);
  4177. /*
  4178. * Our fallback bfqq if bfq_find_alloc_queue() runs into OOM issues.
  4179. * Grab a permanent reference to it, so that the normal code flow
  4180. * will not attempt to free it.
  4181. */
  4182. bfq_init_bfqq(bfqd, &bfqd->oom_bfqq, NULL, 1, 0);
  4183. bfqd->oom_bfqq.ref++;
  4184. bfqd->oom_bfqq.new_ioprio = BFQ_DEFAULT_QUEUE_IOPRIO;
  4185. bfqd->oom_bfqq.new_ioprio_class = IOPRIO_CLASS_BE;
  4186. bfqd->oom_bfqq.entity.new_weight =
  4187. bfq_ioprio_to_weight(bfqd->oom_bfqq.new_ioprio);
  4188. /* oom_bfqq does not participate to bursts */
  4189. bfq_clear_bfqq_just_created(&bfqd->oom_bfqq);
  4190. /*
  4191. * Trigger weight initialization, according to ioprio, at the
  4192. * oom_bfqq's first activation. The oom_bfqq's ioprio and ioprio
  4193. * class won't be changed any more.
  4194. */
  4195. bfqd->oom_bfqq.entity.prio_changed = 1;
  4196. bfqd->queue = q;
  4197. INIT_LIST_HEAD(&bfqd->dispatch);
  4198. hrtimer_init(&bfqd->idle_slice_timer, CLOCK_MONOTONIC,
  4199. HRTIMER_MODE_REL);
  4200. bfqd->idle_slice_timer.function = bfq_idle_slice_timer;
  4201. bfqd->queue_weights_tree = RB_ROOT;
  4202. bfqd->group_weights_tree = RB_ROOT;
  4203. INIT_LIST_HEAD(&bfqd->active_list);
  4204. INIT_LIST_HEAD(&bfqd->idle_list);
  4205. INIT_HLIST_HEAD(&bfqd->burst_list);
  4206. bfqd->hw_tag = -1;
  4207. bfqd->bfq_max_budget = bfq_default_max_budget;
  4208. bfqd->bfq_fifo_expire[0] = bfq_fifo_expire[0];
  4209. bfqd->bfq_fifo_expire[1] = bfq_fifo_expire[1];
  4210. bfqd->bfq_back_max = bfq_back_max;
  4211. bfqd->bfq_back_penalty = bfq_back_penalty;
  4212. bfqd->bfq_slice_idle = bfq_slice_idle;
  4213. bfqd->bfq_timeout = bfq_timeout;
  4214. bfqd->bfq_requests_within_timer = 120;
  4215. bfqd->bfq_large_burst_thresh = 8;
  4216. bfqd->bfq_burst_interval = msecs_to_jiffies(180);
  4217. bfqd->low_latency = true;
  4218. /*
  4219. * Trade-off between responsiveness and fairness.
  4220. */
  4221. bfqd->bfq_wr_coeff = 30;
  4222. bfqd->bfq_wr_rt_max_time = msecs_to_jiffies(300);
  4223. bfqd->bfq_wr_max_time = 0;
  4224. bfqd->bfq_wr_min_idle_time = msecs_to_jiffies(2000);
  4225. bfqd->bfq_wr_min_inter_arr_async = msecs_to_jiffies(500);
  4226. bfqd->bfq_wr_max_softrt_rate = 7000; /*
  4227. * Approximate rate required
  4228. * to playback or record a
  4229. * high-definition compressed
  4230. * video.
  4231. */
  4232. bfqd->wr_busy_queues = 0;
  4233. /*
  4234. * Begin by assuming, optimistically, that the device is a
  4235. * high-speed one, and that its peak rate is equal to 2/3 of
  4236. * the highest reference rate.
  4237. */
  4238. bfqd->RT_prod = R_fast[blk_queue_nonrot(bfqd->queue)] *
  4239. T_fast[blk_queue_nonrot(bfqd->queue)];
  4240. bfqd->peak_rate = R_fast[blk_queue_nonrot(bfqd->queue)] * 2 / 3;
  4241. bfqd->device_speed = BFQ_BFQD_FAST;
  4242. spin_lock_init(&bfqd->lock);
  4243. /*
  4244. * The invocation of the next bfq_create_group_hierarchy
  4245. * function is the head of a chain of function calls
  4246. * (bfq_create_group_hierarchy->blkcg_activate_policy->
  4247. * blk_mq_freeze_queue) that may lead to the invocation of the
  4248. * has_work hook function. For this reason,
  4249. * bfq_create_group_hierarchy is invoked only after all
  4250. * scheduler data has been initialized, apart from the fields
  4251. * that can be initialized only after invoking
  4252. * bfq_create_group_hierarchy. This, in particular, enables
  4253. * has_work to correctly return false. Of course, to avoid
  4254. * other inconsistencies, the blk-mq stack must then refrain
  4255. * from invoking further scheduler hooks before this init
  4256. * function is finished.
  4257. */
  4258. bfqd->root_group = bfq_create_group_hierarchy(bfqd, q->node);
  4259. if (!bfqd->root_group)
  4260. goto out_free;
  4261. bfq_init_root_group(bfqd->root_group, bfqd);
  4262. bfq_init_entity(&bfqd->oom_bfqq.entity, bfqd->root_group);
  4263. wbt_disable_default(q);
  4264. return 0;
  4265. out_free:
  4266. kfree(bfqd);
  4267. kobject_put(&eq->kobj);
  4268. return -ENOMEM;
  4269. }
  4270. static void bfq_slab_kill(void)
  4271. {
  4272. kmem_cache_destroy(bfq_pool);
  4273. }
  4274. static int __init bfq_slab_setup(void)
  4275. {
  4276. bfq_pool = KMEM_CACHE(bfq_queue, 0);
  4277. if (!bfq_pool)
  4278. return -ENOMEM;
  4279. return 0;
  4280. }
  4281. static ssize_t bfq_var_show(unsigned int var, char *page)
  4282. {
  4283. return sprintf(page, "%u\n", var);
  4284. }
  4285. static int bfq_var_store(unsigned long *var, const char *page)
  4286. {
  4287. unsigned long new_val;
  4288. int ret = kstrtoul(page, 10, &new_val);
  4289. if (ret)
  4290. return ret;
  4291. *var = new_val;
  4292. return 0;
  4293. }
  4294. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  4295. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  4296. { \
  4297. struct bfq_data *bfqd = e->elevator_data; \
  4298. u64 __data = __VAR; \
  4299. if (__CONV == 1) \
  4300. __data = jiffies_to_msecs(__data); \
  4301. else if (__CONV == 2) \
  4302. __data = div_u64(__data, NSEC_PER_MSEC); \
  4303. return bfq_var_show(__data, (page)); \
  4304. }
  4305. SHOW_FUNCTION(bfq_fifo_expire_sync_show, bfqd->bfq_fifo_expire[1], 2);
  4306. SHOW_FUNCTION(bfq_fifo_expire_async_show, bfqd->bfq_fifo_expire[0], 2);
  4307. SHOW_FUNCTION(bfq_back_seek_max_show, bfqd->bfq_back_max, 0);
  4308. SHOW_FUNCTION(bfq_back_seek_penalty_show, bfqd->bfq_back_penalty, 0);
  4309. SHOW_FUNCTION(bfq_slice_idle_show, bfqd->bfq_slice_idle, 2);
  4310. SHOW_FUNCTION(bfq_max_budget_show, bfqd->bfq_user_max_budget, 0);
  4311. SHOW_FUNCTION(bfq_timeout_sync_show, bfqd->bfq_timeout, 1);
  4312. SHOW_FUNCTION(bfq_strict_guarantees_show, bfqd->strict_guarantees, 0);
  4313. SHOW_FUNCTION(bfq_low_latency_show, bfqd->low_latency, 0);
  4314. #undef SHOW_FUNCTION
  4315. #define USEC_SHOW_FUNCTION(__FUNC, __VAR) \
  4316. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  4317. { \
  4318. struct bfq_data *bfqd = e->elevator_data; \
  4319. u64 __data = __VAR; \
  4320. __data = div_u64(__data, NSEC_PER_USEC); \
  4321. return bfq_var_show(__data, (page)); \
  4322. }
  4323. USEC_SHOW_FUNCTION(bfq_slice_idle_us_show, bfqd->bfq_slice_idle);
  4324. #undef USEC_SHOW_FUNCTION
  4325. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  4326. static ssize_t \
  4327. __FUNC(struct elevator_queue *e, const char *page, size_t count) \
  4328. { \
  4329. struct bfq_data *bfqd = e->elevator_data; \
  4330. unsigned long __data, __min = (MIN), __max = (MAX); \
  4331. int ret; \
  4332. \
  4333. ret = bfq_var_store(&__data, (page)); \
  4334. if (ret) \
  4335. return ret; \
  4336. if (__data < __min) \
  4337. __data = __min; \
  4338. else if (__data > __max) \
  4339. __data = __max; \
  4340. if (__CONV == 1) \
  4341. *(__PTR) = msecs_to_jiffies(__data); \
  4342. else if (__CONV == 2) \
  4343. *(__PTR) = (u64)__data * NSEC_PER_MSEC; \
  4344. else \
  4345. *(__PTR) = __data; \
  4346. return count; \
  4347. }
  4348. STORE_FUNCTION(bfq_fifo_expire_sync_store, &bfqd->bfq_fifo_expire[1], 1,
  4349. INT_MAX, 2);
  4350. STORE_FUNCTION(bfq_fifo_expire_async_store, &bfqd->bfq_fifo_expire[0], 1,
  4351. INT_MAX, 2);
  4352. STORE_FUNCTION(bfq_back_seek_max_store, &bfqd->bfq_back_max, 0, INT_MAX, 0);
  4353. STORE_FUNCTION(bfq_back_seek_penalty_store, &bfqd->bfq_back_penalty, 1,
  4354. INT_MAX, 0);
  4355. STORE_FUNCTION(bfq_slice_idle_store, &bfqd->bfq_slice_idle, 0, INT_MAX, 2);
  4356. #undef STORE_FUNCTION
  4357. #define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
  4358. static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count)\
  4359. { \
  4360. struct bfq_data *bfqd = e->elevator_data; \
  4361. unsigned long __data, __min = (MIN), __max = (MAX); \
  4362. int ret; \
  4363. \
  4364. ret = bfq_var_store(&__data, (page)); \
  4365. if (ret) \
  4366. return ret; \
  4367. if (__data < __min) \
  4368. __data = __min; \
  4369. else if (__data > __max) \
  4370. __data = __max; \
  4371. *(__PTR) = (u64)__data * NSEC_PER_USEC; \
  4372. return count; \
  4373. }
  4374. USEC_STORE_FUNCTION(bfq_slice_idle_us_store, &bfqd->bfq_slice_idle, 0,
  4375. UINT_MAX);
  4376. #undef USEC_STORE_FUNCTION
  4377. static ssize_t bfq_max_budget_store(struct elevator_queue *e,
  4378. const char *page, size_t count)
  4379. {
  4380. struct bfq_data *bfqd = e->elevator_data;
  4381. unsigned long __data;
  4382. int ret;
  4383. ret = bfq_var_store(&__data, (page));
  4384. if (ret)
  4385. return ret;
  4386. if (__data == 0)
  4387. bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd);
  4388. else {
  4389. if (__data > INT_MAX)
  4390. __data = INT_MAX;
  4391. bfqd->bfq_max_budget = __data;
  4392. }
  4393. bfqd->bfq_user_max_budget = __data;
  4394. return count;
  4395. }
  4396. /*
  4397. * Leaving this name to preserve name compatibility with cfq
  4398. * parameters, but this timeout is used for both sync and async.
  4399. */
  4400. static ssize_t bfq_timeout_sync_store(struct elevator_queue *e,
  4401. const char *page, size_t count)
  4402. {
  4403. struct bfq_data *bfqd = e->elevator_data;
  4404. unsigned long __data;
  4405. int ret;
  4406. ret = bfq_var_store(&__data, (page));
  4407. if (ret)
  4408. return ret;
  4409. if (__data < 1)
  4410. __data = 1;
  4411. else if (__data > INT_MAX)
  4412. __data = INT_MAX;
  4413. bfqd->bfq_timeout = msecs_to_jiffies(__data);
  4414. if (bfqd->bfq_user_max_budget == 0)
  4415. bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd);
  4416. return count;
  4417. }
  4418. static ssize_t bfq_strict_guarantees_store(struct elevator_queue *e,
  4419. const char *page, size_t count)
  4420. {
  4421. struct bfq_data *bfqd = e->elevator_data;
  4422. unsigned long __data;
  4423. int ret;
  4424. ret = bfq_var_store(&__data, (page));
  4425. if (ret)
  4426. return ret;
  4427. if (__data > 1)
  4428. __data = 1;
  4429. if (!bfqd->strict_guarantees && __data == 1
  4430. && bfqd->bfq_slice_idle < 8 * NSEC_PER_MSEC)
  4431. bfqd->bfq_slice_idle = 8 * NSEC_PER_MSEC;
  4432. bfqd->strict_guarantees = __data;
  4433. return count;
  4434. }
  4435. static ssize_t bfq_low_latency_store(struct elevator_queue *e,
  4436. const char *page, size_t count)
  4437. {
  4438. struct bfq_data *bfqd = e->elevator_data;
  4439. unsigned long __data;
  4440. int ret;
  4441. ret = bfq_var_store(&__data, (page));
  4442. if (ret)
  4443. return ret;
  4444. if (__data > 1)
  4445. __data = 1;
  4446. if (__data == 0 && bfqd->low_latency != 0)
  4447. bfq_end_wr(bfqd);
  4448. bfqd->low_latency = __data;
  4449. return count;
  4450. }
  4451. #define BFQ_ATTR(name) \
  4452. __ATTR(name, 0644, bfq_##name##_show, bfq_##name##_store)
  4453. static struct elv_fs_entry bfq_attrs[] = {
  4454. BFQ_ATTR(fifo_expire_sync),
  4455. BFQ_ATTR(fifo_expire_async),
  4456. BFQ_ATTR(back_seek_max),
  4457. BFQ_ATTR(back_seek_penalty),
  4458. BFQ_ATTR(slice_idle),
  4459. BFQ_ATTR(slice_idle_us),
  4460. BFQ_ATTR(max_budget),
  4461. BFQ_ATTR(timeout_sync),
  4462. BFQ_ATTR(strict_guarantees),
  4463. BFQ_ATTR(low_latency),
  4464. __ATTR_NULL
  4465. };
  4466. static struct elevator_type iosched_bfq_mq = {
  4467. .ops.mq = {
  4468. .prepare_request = bfq_prepare_request,
  4469. .finish_request = bfq_finish_request,
  4470. .exit_icq = bfq_exit_icq,
  4471. .insert_requests = bfq_insert_requests,
  4472. .dispatch_request = bfq_dispatch_request,
  4473. .next_request = elv_rb_latter_request,
  4474. .former_request = elv_rb_former_request,
  4475. .allow_merge = bfq_allow_bio_merge,
  4476. .bio_merge = bfq_bio_merge,
  4477. .request_merge = bfq_request_merge,
  4478. .requests_merged = bfq_requests_merged,
  4479. .request_merged = bfq_request_merged,
  4480. .has_work = bfq_has_work,
  4481. .init_sched = bfq_init_queue,
  4482. .exit_sched = bfq_exit_queue,
  4483. },
  4484. .uses_mq = true,
  4485. .icq_size = sizeof(struct bfq_io_cq),
  4486. .icq_align = __alignof__(struct bfq_io_cq),
  4487. .elevator_attrs = bfq_attrs,
  4488. .elevator_name = "bfq",
  4489. .elevator_owner = THIS_MODULE,
  4490. };
  4491. MODULE_ALIAS("bfq-iosched");
  4492. static int __init bfq_init(void)
  4493. {
  4494. int ret;
  4495. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  4496. ret = blkcg_policy_register(&blkcg_policy_bfq);
  4497. if (ret)
  4498. return ret;
  4499. #endif
  4500. ret = -ENOMEM;
  4501. if (bfq_slab_setup())
  4502. goto err_pol_unreg;
  4503. /*
  4504. * Times to load large popular applications for the typical
  4505. * systems installed on the reference devices (see the
  4506. * comments before the definitions of the next two
  4507. * arrays). Actually, we use slightly slower values, as the
  4508. * estimated peak rate tends to be smaller than the actual
  4509. * peak rate. The reason for this last fact is that estimates
  4510. * are computed over much shorter time intervals than the long
  4511. * intervals typically used for benchmarking. Why? First, to
  4512. * adapt more quickly to variations. Second, because an I/O
  4513. * scheduler cannot rely on a peak-rate-evaluation workload to
  4514. * be run for a long time.
  4515. */
  4516. T_slow[0] = msecs_to_jiffies(3500); /* actually 4 sec */
  4517. T_slow[1] = msecs_to_jiffies(6000); /* actually 6.5 sec */
  4518. T_fast[0] = msecs_to_jiffies(7000); /* actually 8 sec */
  4519. T_fast[1] = msecs_to_jiffies(2500); /* actually 3 sec */
  4520. /*
  4521. * Thresholds that determine the switch between speed classes
  4522. * (see the comments before the definition of the array
  4523. * device_speed_thresh). These thresholds are biased towards
  4524. * transitions to the fast class. This is safer than the
  4525. * opposite bias. In fact, a wrong transition to the slow
  4526. * class results in short weight-raising periods, because the
  4527. * speed of the device then tends to be higher that the
  4528. * reference peak rate. On the opposite end, a wrong
  4529. * transition to the fast class tends to increase
  4530. * weight-raising periods, because of the opposite reason.
  4531. */
  4532. device_speed_thresh[0] = (4 * R_slow[0]) / 3;
  4533. device_speed_thresh[1] = (4 * R_slow[1]) / 3;
  4534. ret = elv_register(&iosched_bfq_mq);
  4535. if (ret)
  4536. goto slab_kill;
  4537. return 0;
  4538. slab_kill:
  4539. bfq_slab_kill();
  4540. err_pol_unreg:
  4541. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  4542. blkcg_policy_unregister(&blkcg_policy_bfq);
  4543. #endif
  4544. return ret;
  4545. }
  4546. static void __exit bfq_exit(void)
  4547. {
  4548. elv_unregister(&iosched_bfq_mq);
  4549. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  4550. blkcg_policy_unregister(&blkcg_policy_bfq);
  4551. #endif
  4552. bfq_slab_kill();
  4553. }
  4554. module_init(bfq_init);
  4555. module_exit(bfq_exit);
  4556. MODULE_AUTHOR("Paolo Valente");
  4557. MODULE_LICENSE("GPL");
  4558. MODULE_DESCRIPTION("MQ Budget Fair Queueing I/O Scheduler");