bfq-iosched.c 132 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208
  1. /*
  2. * Budget Fair Queueing (BFQ) disk 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. *
  12. * Licensed under the GPL-2 as detailed in the accompanying COPYING.BFQ
  13. * file.
  14. *
  15. * BFQ is a proportional-share storage-I/O scheduling algorithm based on
  16. * the slice-by-slice service scheme of CFQ. But BFQ assigns budgets,
  17. * measured in number of sectors, to processes instead of time slices. The
  18. * device is not granted to the in-service process for a given time slice,
  19. * but until it has exhausted its assigned budget. This change from the time
  20. * to the service domain allows BFQ to distribute the device throughput
  21. * among processes as desired, without any distortion due to ZBR, workload
  22. * fluctuations or other factors. BFQ uses an ad hoc internal scheduler,
  23. * called B-WF2Q+, to schedule processes according to their budgets. More
  24. * precisely, BFQ schedules queues associated to processes. Thanks to the
  25. * accurate policy of B-WF2Q+, BFQ can afford to assign high budgets to
  26. * I/O-bound processes issuing sequential requests (to boost the
  27. * throughput), and yet guarantee a low latency to interactive and soft
  28. * real-time applications.
  29. *
  30. * BFQ is described in [1], where also a reference to the initial, more
  31. * theoretical paper on BFQ can be found. The interested reader can find
  32. * in the latter paper full details on the main algorithm, as well as
  33. * formulas of the guarantees and formal proofs of all the properties.
  34. * With respect to the version of BFQ presented in these papers, this
  35. * implementation adds a few more heuristics, such as the one that
  36. * guarantees a low latency to soft real-time applications, and a
  37. * hierarchical extension based on H-WF2Q+.
  38. *
  39. * B-WF2Q+ is based on WF2Q+, that is described in [2], together with
  40. * H-WF2Q+, while the augmented tree used to implement B-WF2Q+ with O(log N)
  41. * complexity derives from the one introduced with EEVDF in [3].
  42. *
  43. * [1] P. Valente and M. Andreolini, ``Improving Application Responsiveness
  44. * with the BFQ Disk I/O Scheduler'',
  45. * Proceedings of the 5th Annual International Systems and Storage
  46. * Conference (SYSTOR '12), June 2012.
  47. *
  48. * http://algogroup.unimo.it/people/paolo/disk_sched/bf1-v1-suite-results.pdf
  49. *
  50. * [2] Jon C.R. Bennett and H. Zhang, ``Hierarchical Packet Fair Queueing
  51. * Algorithms,'' IEEE/ACM Transactions on Networking, 5(5):675-689,
  52. * Oct 1997.
  53. *
  54. * http://www.cs.cmu.edu/~hzhang/papers/TON-97-Oct.ps.gz
  55. *
  56. * [3] I. Stoica and H. Abdel-Wahab, ``Earliest Eligible Virtual Deadline
  57. * First: A Flexible and Accurate Mechanism for Proportional Share
  58. * Resource Allocation,'' technical report.
  59. *
  60. * http://www.cs.berkeley.edu/~istoica/papers/eevdf-tr-95.pdf
  61. */
  62. #include <linux/module.h>
  63. #include <linux/slab.h>
  64. #include <linux/blkdev.h>
  65. #include <linux/cgroup.h>
  66. #include <linux/elevator.h>
  67. #include <linux/jiffies.h>
  68. #include <linux/rbtree.h>
  69. #include <linux/ioprio.h>
  70. #include "bfq.h"
  71. #include "blk.h"
  72. /* Expiration time of sync (0) and async (1) requests, in jiffies. */
  73. static const int bfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
  74. /* Maximum backwards seek, in KiB. */
  75. static const int bfq_back_max = 16 * 1024;
  76. /* Penalty of a backwards seek, in number of sectors. */
  77. static const int bfq_back_penalty = 2;
  78. /* Idling period duration, in jiffies. */
  79. static int bfq_slice_idle = HZ / 125;
  80. /* Default maximum budget values, in sectors and number of requests. */
  81. static const int bfq_default_max_budget = 16 * 1024;
  82. static const int bfq_max_budget_async_rq = 4;
  83. /*
  84. * Async to sync throughput distribution is controlled as follows:
  85. * when an async request is served, the entity is charged the number
  86. * of sectors of the request, multiplied by the factor below
  87. */
  88. static const int bfq_async_charge_factor = 10;
  89. /* Default timeout values, in jiffies, approximating CFQ defaults. */
  90. static const int bfq_timeout_sync = HZ / 8;
  91. static int bfq_timeout_async = HZ / 25;
  92. struct kmem_cache *bfq_pool;
  93. /* Below this threshold (in ms), we consider thinktime immediate. */
  94. #define BFQ_MIN_TT 2
  95. /* hw_tag detection: parallel requests threshold and min samples needed. */
  96. #define BFQ_HW_QUEUE_THRESHOLD 4
  97. #define BFQ_HW_QUEUE_SAMPLES 32
  98. #define BFQQ_SEEK_THR (sector_t)(8 * 1024)
  99. #define BFQQ_SEEKY(bfqq) ((bfqq)->seek_mean > BFQQ_SEEK_THR)
  100. /* Min samples used for peak rate estimation (for autotuning). */
  101. #define BFQ_PEAK_RATE_SAMPLES 32
  102. /* Shift used for peak rate fixed precision calculations. */
  103. #define BFQ_RATE_SHIFT 16
  104. /*
  105. * By default, BFQ computes the duration of the weight raising for
  106. * interactive applications automatically, using the following formula:
  107. * duration = (R / r) * T, where r is the peak rate of the device, and
  108. * R and T are two reference parameters.
  109. * In particular, R is the peak rate of the reference device (see below),
  110. * and T is a reference time: given the systems that are likely to be
  111. * installed on the reference device according to its speed class, T is
  112. * about the maximum time needed, under BFQ and while reading two files in
  113. * parallel, to load typical large applications on these systems.
  114. * In practice, the slower/faster the device at hand is, the more/less it
  115. * takes to load applications with respect to the reference device.
  116. * Accordingly, the longer/shorter BFQ grants weight raising to interactive
  117. * applications.
  118. *
  119. * BFQ uses four different reference pairs (R, T), depending on:
  120. * . whether the device is rotational or non-rotational;
  121. * . whether the device is slow, such as old or portable HDDs, as well as
  122. * SD cards, or fast, such as newer HDDs and SSDs.
  123. *
  124. * The device's speed class is dynamically (re)detected in
  125. * bfq_update_peak_rate() every time the estimated peak rate is updated.
  126. *
  127. * In the following definitions, R_slow[0]/R_fast[0] and T_slow[0]/T_fast[0]
  128. * are the reference values for a slow/fast rotational device, whereas
  129. * R_slow[1]/R_fast[1] and T_slow[1]/T_fast[1] are the reference values for
  130. * a slow/fast non-rotational device. Finally, device_speed_thresh are the
  131. * thresholds used to switch between speed classes.
  132. * Both the reference peak rates and the thresholds are measured in
  133. * sectors/usec, left-shifted by BFQ_RATE_SHIFT.
  134. */
  135. static int R_slow[2] = {1536, 10752};
  136. static int R_fast[2] = {17415, 34791};
  137. /*
  138. * To improve readability, a conversion function is used to initialize the
  139. * following arrays, which entails that they can be initialized only in a
  140. * function.
  141. */
  142. static int T_slow[2];
  143. static int T_fast[2];
  144. static int device_speed_thresh[2];
  145. #define BFQ_SERVICE_TREE_INIT ((struct bfq_service_tree) \
  146. { RB_ROOT, RB_ROOT, NULL, NULL, 0, 0 })
  147. #define RQ_BIC(rq) ((struct bfq_io_cq *) (rq)->elv.priv[0])
  148. #define RQ_BFQQ(rq) ((rq)->elv.priv[1])
  149. static inline void bfq_schedule_dispatch(struct bfq_data *bfqd);
  150. #include "bfq-ioc.c"
  151. #include "bfq-sched.c"
  152. #include "bfq-cgroup.c"
  153. #define bfq_class_idle(bfqq) ((bfqq)->entity.ioprio_class ==\
  154. IOPRIO_CLASS_IDLE)
  155. #define bfq_class_rt(bfqq) ((bfqq)->entity.ioprio_class ==\
  156. IOPRIO_CLASS_RT)
  157. #define bfq_sample_valid(samples) ((samples) > 80)
  158. /*
  159. * The following macro groups conditions that need to be evaluated when
  160. * checking if existing queues and groups form a symmetric scenario
  161. * and therefore idling can be reduced or disabled for some of the
  162. * queues. See the comment to the function bfq_bfqq_must_not_expire()
  163. * for further details.
  164. */
  165. #ifdef CONFIG_CGROUP_BFQIO
  166. #define symmetric_scenario (!bfqd->active_numerous_groups && \
  167. !bfq_differentiated_weights(bfqd))
  168. #else
  169. #define symmetric_scenario (!bfq_differentiated_weights(bfqd))
  170. #endif
  171. /*
  172. * We regard a request as SYNC, if either it's a read or has the SYNC bit
  173. * set (in which case it could also be a direct WRITE).
  174. */
  175. static inline int bfq_bio_sync(struct bio *bio)
  176. {
  177. if (bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC))
  178. return 1;
  179. return 0;
  180. }
  181. /*
  182. * Scheduler run of queue, if there are requests pending and no one in the
  183. * driver that will restart queueing.
  184. */
  185. static inline void bfq_schedule_dispatch(struct bfq_data *bfqd)
  186. {
  187. if (bfqd->queued != 0) {
  188. bfq_log(bfqd, "schedule dispatch");
  189. kblockd_schedule_work(bfqd->queue, &bfqd->unplug_work);
  190. }
  191. }
  192. /*
  193. * Lifted from AS - choose which of rq1 and rq2 that is best served now.
  194. * We choose the request that is closesr to the head right now. Distance
  195. * behind the head is penalized and only allowed to a certain extent.
  196. */
  197. static struct request *bfq_choose_req(struct bfq_data *bfqd,
  198. struct request *rq1,
  199. struct request *rq2,
  200. sector_t last)
  201. {
  202. sector_t s1, s2, d1 = 0, d2 = 0;
  203. unsigned long back_max;
  204. #define BFQ_RQ1_WRAP 0x01 /* request 1 wraps */
  205. #define BFQ_RQ2_WRAP 0x02 /* request 2 wraps */
  206. unsigned wrap = 0; /* bit mask: requests behind the disk head? */
  207. if (rq1 == NULL || rq1 == rq2)
  208. return rq2;
  209. if (rq2 == NULL)
  210. return rq1;
  211. if (rq_is_sync(rq1) && !rq_is_sync(rq2))
  212. return rq1;
  213. else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
  214. return rq2;
  215. if ((rq1->cmd_flags & REQ_META) && !(rq2->cmd_flags & REQ_META))
  216. return rq1;
  217. else if ((rq2->cmd_flags & REQ_META) && !(rq1->cmd_flags & REQ_META))
  218. return rq2;
  219. s1 = blk_rq_pos(rq1);
  220. s2 = blk_rq_pos(rq2);
  221. /*
  222. * By definition, 1KiB is 2 sectors.
  223. */
  224. back_max = bfqd->bfq_back_max * 2;
  225. /*
  226. * Strict one way elevator _except_ in the case where we allow
  227. * short backward seeks which are biased as twice the cost of a
  228. * similar forward seek.
  229. */
  230. if (s1 >= last)
  231. d1 = s1 - last;
  232. else if (s1 + back_max >= last)
  233. d1 = (last - s1) * bfqd->bfq_back_penalty;
  234. else
  235. wrap |= BFQ_RQ1_WRAP;
  236. if (s2 >= last)
  237. d2 = s2 - last;
  238. else if (s2 + back_max >= last)
  239. d2 = (last - s2) * bfqd->bfq_back_penalty;
  240. else
  241. wrap |= BFQ_RQ2_WRAP;
  242. /* Found required data */
  243. /*
  244. * By doing switch() on the bit mask "wrap" we avoid having to
  245. * check two variables for all permutations: --> faster!
  246. */
  247. switch (wrap) {
  248. case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
  249. if (d1 < d2)
  250. return rq1;
  251. else if (d2 < d1)
  252. return rq2;
  253. else {
  254. if (s1 >= s2)
  255. return rq1;
  256. else
  257. return rq2;
  258. }
  259. case BFQ_RQ2_WRAP:
  260. return rq1;
  261. case BFQ_RQ1_WRAP:
  262. return rq2;
  263. case (BFQ_RQ1_WRAP|BFQ_RQ2_WRAP): /* both rqs wrapped */
  264. default:
  265. /*
  266. * Since both rqs are wrapped,
  267. * start with the one that's further behind head
  268. * (--> only *one* back seek required),
  269. * since back seek takes more time than forward.
  270. */
  271. if (s1 <= s2)
  272. return rq1;
  273. else
  274. return rq2;
  275. }
  276. }
  277. static struct bfq_queue *
  278. bfq_rq_pos_tree_lookup(struct bfq_data *bfqd, struct rb_root *root,
  279. sector_t sector, struct rb_node **ret_parent,
  280. struct rb_node ***rb_link)
  281. {
  282. struct rb_node **p, *parent;
  283. struct bfq_queue *bfqq = NULL;
  284. parent = NULL;
  285. p = &root->rb_node;
  286. while (*p) {
  287. struct rb_node **n;
  288. parent = *p;
  289. bfqq = rb_entry(parent, struct bfq_queue, pos_node);
  290. /*
  291. * Sort strictly based on sector. Smallest to the left,
  292. * largest to the right.
  293. */
  294. if (sector > blk_rq_pos(bfqq->next_rq))
  295. n = &(*p)->rb_right;
  296. else if (sector < blk_rq_pos(bfqq->next_rq))
  297. n = &(*p)->rb_left;
  298. else
  299. break;
  300. p = n;
  301. bfqq = NULL;
  302. }
  303. *ret_parent = parent;
  304. if (rb_link)
  305. *rb_link = p;
  306. bfq_log(bfqd, "rq_pos_tree_lookup %llu: returning %d",
  307. (long long unsigned)sector,
  308. bfqq != NULL ? bfqq->pid : 0);
  309. return bfqq;
  310. }
  311. static void bfq_rq_pos_tree_add(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  312. {
  313. struct rb_node **p, *parent;
  314. struct bfq_queue *__bfqq;
  315. if (bfqq->pos_root != NULL) {
  316. rb_erase(&bfqq->pos_node, bfqq->pos_root);
  317. bfqq->pos_root = NULL;
  318. }
  319. if (bfq_class_idle(bfqq))
  320. return;
  321. if (!bfqq->next_rq)
  322. return;
  323. bfqq->pos_root = &bfqd->rq_pos_tree;
  324. __bfqq = bfq_rq_pos_tree_lookup(bfqd, bfqq->pos_root,
  325. blk_rq_pos(bfqq->next_rq), &parent, &p);
  326. if (__bfqq == NULL) {
  327. rb_link_node(&bfqq->pos_node, parent, p);
  328. rb_insert_color(&bfqq->pos_node, bfqq->pos_root);
  329. } else
  330. bfqq->pos_root = NULL;
  331. }
  332. /*
  333. * Tell whether there are active queues or groups with differentiated weights.
  334. */
  335. static inline bool bfq_differentiated_weights(struct bfq_data *bfqd)
  336. {
  337. /*
  338. * For weights to differ, at least one of the trees must contain
  339. * at least two nodes.
  340. */
  341. return (!RB_EMPTY_ROOT(&bfqd->queue_weights_tree) &&
  342. (bfqd->queue_weights_tree.rb_node->rb_left ||
  343. bfqd->queue_weights_tree.rb_node->rb_right)
  344. #ifdef CONFIG_CGROUP_BFQIO
  345. ) ||
  346. (!RB_EMPTY_ROOT(&bfqd->group_weights_tree) &&
  347. (bfqd->group_weights_tree.rb_node->rb_left ||
  348. bfqd->group_weights_tree.rb_node->rb_right)
  349. #endif
  350. );
  351. }
  352. /*
  353. * If the weight-counter tree passed as input contains no counter for
  354. * the weight of the input entity, then add that counter; otherwise just
  355. * increment the existing counter.
  356. *
  357. * Note that weight-counter trees contain few nodes in mostly symmetric
  358. * scenarios. For example, if all queues have the same weight, then the
  359. * weight-counter tree for the queues may contain at most one node.
  360. * This holds even if low_latency is on, because weight-raised queues
  361. * are not inserted in the tree.
  362. * In most scenarios, the rate at which nodes are created/destroyed
  363. * should be low too.
  364. */
  365. static void bfq_weights_tree_add(struct bfq_data *bfqd,
  366. struct bfq_entity *entity,
  367. struct rb_root *root)
  368. {
  369. struct rb_node **new = &(root->rb_node), *parent = NULL;
  370. /*
  371. * Do not insert if the entity is already associated with a
  372. * counter, which happens if:
  373. * 1) the entity is associated with a queue,
  374. * 2) a request arrival has caused the queue to become both
  375. * non-weight-raised, and hence change its weight, and
  376. * backlogged; in this respect, each of the two events
  377. * causes an invocation of this function,
  378. * 3) this is the invocation of this function caused by the
  379. * second event. This second invocation is actually useless,
  380. * and we handle this fact by exiting immediately. More
  381. * efficient or clearer solutions might possibly be adopted.
  382. */
  383. if (entity->weight_counter)
  384. return;
  385. while (*new) {
  386. struct bfq_weight_counter *__counter = container_of(*new,
  387. struct bfq_weight_counter,
  388. weights_node);
  389. parent = *new;
  390. if (entity->weight == __counter->weight) {
  391. entity->weight_counter = __counter;
  392. goto inc_counter;
  393. }
  394. if (entity->weight < __counter->weight)
  395. new = &((*new)->rb_left);
  396. else
  397. new = &((*new)->rb_right);
  398. }
  399. entity->weight_counter = kzalloc(sizeof(struct bfq_weight_counter),
  400. GFP_ATOMIC);
  401. entity->weight_counter->weight = entity->weight;
  402. rb_link_node(&entity->weight_counter->weights_node, parent, new);
  403. rb_insert_color(&entity->weight_counter->weights_node, root);
  404. inc_counter:
  405. entity->weight_counter->num_active++;
  406. }
  407. /*
  408. * Decrement the weight counter associated with the entity, and, if the
  409. * counter reaches 0, remove the counter from the tree.
  410. * See the comments to the function bfq_weights_tree_add() for considerations
  411. * about overhead.
  412. */
  413. static void bfq_weights_tree_remove(struct bfq_data *bfqd,
  414. struct bfq_entity *entity,
  415. struct rb_root *root)
  416. {
  417. if (!entity->weight_counter)
  418. return;
  419. BUG_ON(RB_EMPTY_ROOT(root));
  420. BUG_ON(entity->weight_counter->weight != entity->weight);
  421. BUG_ON(!entity->weight_counter->num_active);
  422. entity->weight_counter->num_active--;
  423. if (entity->weight_counter->num_active > 0)
  424. goto reset_entity_pointer;
  425. rb_erase(&entity->weight_counter->weights_node, root);
  426. kfree(entity->weight_counter);
  427. reset_entity_pointer:
  428. entity->weight_counter = NULL;
  429. }
  430. static struct request *bfq_find_next_rq(struct bfq_data *bfqd,
  431. struct bfq_queue *bfqq,
  432. struct request *last)
  433. {
  434. struct rb_node *rbnext = rb_next(&last->rb_node);
  435. struct rb_node *rbprev = rb_prev(&last->rb_node);
  436. struct request *next = NULL, *prev = NULL;
  437. BUG_ON(RB_EMPTY_NODE(&last->rb_node));
  438. if (rbprev != NULL)
  439. prev = rb_entry_rq(rbprev);
  440. if (rbnext != NULL)
  441. next = rb_entry_rq(rbnext);
  442. else {
  443. rbnext = rb_first(&bfqq->sort_list);
  444. if (rbnext && rbnext != &last->rb_node)
  445. next = rb_entry_rq(rbnext);
  446. }
  447. return bfq_choose_req(bfqd, next, prev, blk_rq_pos(last));
  448. }
  449. /* see the definition of bfq_async_charge_factor for details */
  450. static inline unsigned long bfq_serv_to_charge(struct request *rq,
  451. struct bfq_queue *bfqq)
  452. {
  453. return blk_rq_sectors(rq) *
  454. (1 + ((!bfq_bfqq_sync(bfqq)) * (bfqq->wr_coeff == 1) *
  455. bfq_async_charge_factor));
  456. }
  457. /**
  458. * bfq_updated_next_req - update the queue after a new next_rq selection.
  459. * @bfqd: the device data the queue belongs to.
  460. * @bfqq: the queue to update.
  461. *
  462. * If the first request of a queue changes we make sure that the queue
  463. * has enough budget to serve at least its first request (if the
  464. * request has grown). We do this because if the queue has not enough
  465. * budget for its first request, it has to go through two dispatch
  466. * rounds to actually get it dispatched.
  467. */
  468. static void bfq_updated_next_req(struct bfq_data *bfqd,
  469. struct bfq_queue *bfqq)
  470. {
  471. struct bfq_entity *entity = &bfqq->entity;
  472. struct bfq_service_tree *st = bfq_entity_service_tree(entity);
  473. struct request *next_rq = bfqq->next_rq;
  474. unsigned long new_budget;
  475. if (next_rq == NULL)
  476. return;
  477. if (bfqq == bfqd->in_service_queue)
  478. /*
  479. * In order not to break guarantees, budgets cannot be
  480. * changed after an entity has been selected.
  481. */
  482. return;
  483. BUG_ON(entity->tree != &st->active);
  484. BUG_ON(entity == entity->sched_data->in_service_entity);
  485. new_budget = max_t(unsigned long, bfqq->max_budget,
  486. bfq_serv_to_charge(next_rq, bfqq));
  487. if (entity->budget != new_budget) {
  488. entity->budget = new_budget;
  489. bfq_log_bfqq(bfqd, bfqq, "updated next rq: new budget %lu",
  490. new_budget);
  491. bfq_activate_bfqq(bfqd, bfqq);
  492. }
  493. }
  494. static inline unsigned int bfq_wr_duration(struct bfq_data *bfqd)
  495. {
  496. u64 dur;
  497. if (bfqd->bfq_wr_max_time > 0)
  498. return bfqd->bfq_wr_max_time;
  499. dur = bfqd->RT_prod;
  500. do_div(dur, bfqd->peak_rate);
  501. return dur;
  502. }
  503. static inline unsigned
  504. bfq_bfqq_cooperations(struct bfq_queue *bfqq)
  505. {
  506. return bfqq->bic ? bfqq->bic->cooperations : 0;
  507. }
  508. static inline void
  509. bfq_bfqq_resume_state(struct bfq_queue *bfqq, struct bfq_io_cq *bic)
  510. {
  511. if (bic->saved_idle_window)
  512. bfq_mark_bfqq_idle_window(bfqq);
  513. else
  514. bfq_clear_bfqq_idle_window(bfqq);
  515. if (bic->saved_IO_bound)
  516. bfq_mark_bfqq_IO_bound(bfqq);
  517. else
  518. bfq_clear_bfqq_IO_bound(bfqq);
  519. /* Assuming that the flag in_large_burst is already correctly set */
  520. if (bic->wr_time_left && bfqq->bfqd->low_latency &&
  521. !bfq_bfqq_in_large_burst(bfqq) &&
  522. bic->cooperations < bfqq->bfqd->bfq_coop_thresh) {
  523. /*
  524. * Start a weight raising period with the duration given by
  525. * the raising_time_left snapshot.
  526. */
  527. if (bfq_bfqq_busy(bfqq))
  528. bfqq->bfqd->wr_busy_queues++;
  529. bfqq->wr_coeff = bfqq->bfqd->bfq_wr_coeff;
  530. bfqq->wr_cur_max_time = bic->wr_time_left;
  531. bfqq->last_wr_start_finish = jiffies;
  532. bfqq->entity.ioprio_changed = 1;
  533. }
  534. /*
  535. * Clear wr_time_left to prevent bfq_bfqq_save_state() from
  536. * getting confused about the queue's need of a weight-raising
  537. * period.
  538. */
  539. bic->wr_time_left = 0;
  540. }
  541. /* Must be called with the queue_lock held. */
  542. static int bfqq_process_refs(struct bfq_queue *bfqq)
  543. {
  544. int process_refs, io_refs;
  545. io_refs = bfqq->allocated[READ] + bfqq->allocated[WRITE];
  546. process_refs = atomic_read(&bfqq->ref) - io_refs - bfqq->entity.on_st;
  547. BUG_ON(process_refs < 0);
  548. return process_refs;
  549. }
  550. /* Empty burst list and add just bfqq (see comments to bfq_handle_burst) */
  551. static inline void bfq_reset_burst_list(struct bfq_data *bfqd,
  552. struct bfq_queue *bfqq)
  553. {
  554. struct bfq_queue *item;
  555. struct hlist_node *pos, *n;
  556. hlist_for_each_entry_safe(item, pos, n,
  557. &bfqd->burst_list, burst_list_node)
  558. hlist_del_init(&item->burst_list_node);
  559. hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list);
  560. bfqd->burst_size = 1;
  561. }
  562. /* Add bfqq to the list of queues in current burst (see bfq_handle_burst) */
  563. static void bfq_add_to_burst(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  564. {
  565. /* Increment burst size to take into account also bfqq */
  566. bfqd->burst_size++;
  567. if (bfqd->burst_size == bfqd->bfq_large_burst_thresh) {
  568. struct bfq_queue *pos, *bfqq_item;
  569. struct hlist_node *p, *n;
  570. /*
  571. * Enough queues have been activated shortly after each
  572. * other to consider this burst as large.
  573. */
  574. bfqd->large_burst = true;
  575. /*
  576. * We can now mark all queues in the burst list as
  577. * belonging to a large burst.
  578. */
  579. hlist_for_each_entry(bfqq_item, n, &bfqd->burst_list,
  580. burst_list_node)
  581. bfq_mark_bfqq_in_large_burst(bfqq_item);
  582. bfq_mark_bfqq_in_large_burst(bfqq);
  583. /*
  584. * From now on, and until the current burst finishes, any
  585. * new queue being activated shortly after the last queue
  586. * was inserted in the burst can be immediately marked as
  587. * belonging to a large burst. So the burst list is not
  588. * needed any more. Remove it.
  589. */
  590. hlist_for_each_entry_safe(pos, p, n, &bfqd->burst_list,
  591. burst_list_node)
  592. hlist_del_init(&pos->burst_list_node);
  593. } else /* burst not yet large: add bfqq to the burst list */
  594. hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list);
  595. }
  596. /*
  597. * If many queues happen to become active shortly after each other, then,
  598. * to help the processes associated to these queues get their job done as
  599. * soon as possible, it is usually better to not grant either weight-raising
  600. * or device idling to these queues. In this comment we describe, firstly,
  601. * the reasons why this fact holds, and, secondly, the next function, which
  602. * implements the main steps needed to properly mark these queues so that
  603. * they can then be treated in a different way.
  604. *
  605. * As for the terminology, we say that a queue becomes active, i.e.,
  606. * switches from idle to backlogged, either when it is created (as a
  607. * consequence of the arrival of an I/O request), or, if already existing,
  608. * when a new request for the queue arrives while the queue is idle.
  609. * Bursts of activations, i.e., activations of different queues occurring
  610. * shortly after each other, are typically caused by services or applications
  611. * that spawn or reactivate many parallel threads/processes. Examples are
  612. * systemd during boot or git grep.
  613. *
  614. * These services or applications benefit mostly from a high throughput:
  615. * the quicker the requests of the activated queues are cumulatively served,
  616. * the sooner the target job of these queues gets completed. As a consequence,
  617. * weight-raising any of these queues, which also implies idling the device
  618. * for it, is almost always counterproductive: in most cases it just lowers
  619. * throughput.
  620. *
  621. * On the other hand, a burst of activations may be also caused by the start
  622. * of an application that does not consist in a lot of parallel I/O-bound
  623. * threads. In fact, with a complex application, the burst may be just a
  624. * consequence of the fact that several processes need to be executed to
  625. * start-up the application. To start an application as quickly as possible,
  626. * the best thing to do is to privilege the I/O related to the application
  627. * with respect to all other I/O. Therefore, the best strategy to start as
  628. * quickly as possible an application that causes a burst of activations is
  629. * to weight-raise all the queues activated during the burst. This is the
  630. * exact opposite of the best strategy for the other type of bursts.
  631. *
  632. * In the end, to take the best action for each of the two cases, the two
  633. * types of bursts need to be distinguished. Fortunately, this seems
  634. * relatively easy to do, by looking at the sizes of the bursts. In
  635. * particular, we found a threshold such that bursts with a larger size
  636. * than that threshold are apparently caused only by services or commands
  637. * such as systemd or git grep. For brevity, hereafter we call just 'large'
  638. * these bursts. BFQ *does not* weight-raise queues whose activations occur
  639. * in a large burst. In addition, for each of these queues BFQ performs or
  640. * does not perform idling depending on which choice boosts the throughput
  641. * most. The exact choice depends on the device and request pattern at
  642. * hand.
  643. *
  644. * Turning back to the next function, it implements all the steps needed
  645. * to detect the occurrence of a large burst and to properly mark all the
  646. * queues belonging to it (so that they can then be treated in a different
  647. * way). This goal is achieved by maintaining a special "burst list" that
  648. * holds, temporarily, the queues that belong to the burst in progress. The
  649. * list is then used to mark these queues as belonging to a large burst if
  650. * the burst does become large. The main steps are the following.
  651. *
  652. * . when the very first queue is activated, the queue is inserted into the
  653. * list (as it could be the first queue in a possible burst)
  654. *
  655. * . if the current burst has not yet become large, and a queue Q that does
  656. * not yet belong to the burst is activated shortly after the last time
  657. * at which a new queue entered the burst list, then the function appends
  658. * Q to the burst list
  659. *
  660. * . if, as a consequence of the previous step, the burst size reaches
  661. * the large-burst threshold, then
  662. *
  663. * . all the queues in the burst list are marked as belonging to a
  664. * large burst
  665. *
  666. * . the burst list is deleted; in fact, the burst list already served
  667. * its purpose (keeping temporarily track of the queues in a burst,
  668. * so as to be able to mark them as belonging to a large burst in the
  669. * previous sub-step), and now is not needed any more
  670. *
  671. * . the device enters a large-burst mode
  672. *
  673. * . if a queue Q that does not belong to the burst is activated while
  674. * the device is in large-burst mode and shortly after the last time
  675. * at which a queue either entered the burst list or was marked as
  676. * belonging to the current large burst, then Q is immediately marked
  677. * as belonging to a large burst.
  678. *
  679. * . if a queue Q that does not belong to the burst is activated a while
  680. * later, i.e., not shortly after, than the last time at which a queue
  681. * either entered the burst list or was marked as belonging to the
  682. * current large burst, then the current burst is deemed as finished and:
  683. *
  684. * . the large-burst mode is reset if set
  685. *
  686. * . the burst list is emptied
  687. *
  688. * . Q is inserted in the burst list, as Q may be the first queue
  689. * in a possible new burst (then the burst list contains just Q
  690. * after this step).
  691. */
  692. static void bfq_handle_burst(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  693. bool idle_for_long_time)
  694. {
  695. /*
  696. * If bfqq happened to be activated in a burst, but has been idle
  697. * for at least as long as an interactive queue, then we assume
  698. * that, in the overall I/O initiated in the burst, the I/O
  699. * associated to bfqq is finished. So bfqq does not need to be
  700. * treated as a queue belonging to a burst anymore. Accordingly,
  701. * we reset bfqq's in_large_burst flag if set, and remove bfqq
  702. * from the burst list if it's there. We do not decrement instead
  703. * burst_size, because the fact that bfqq does not need to belong
  704. * to the burst list any more does not invalidate the fact that
  705. * bfqq may have been activated during the current burst.
  706. */
  707. if (idle_for_long_time) {
  708. hlist_del_init(&bfqq->burst_list_node);
  709. bfq_clear_bfqq_in_large_burst(bfqq);
  710. }
  711. /*
  712. * If bfqq is already in the burst list or is part of a large
  713. * burst, then there is nothing else to do.
  714. */
  715. if (!hlist_unhashed(&bfqq->burst_list_node) ||
  716. bfq_bfqq_in_large_burst(bfqq))
  717. return;
  718. /*
  719. * If bfqq's activation happens late enough, then the current
  720. * burst is finished, and related data structures must be reset.
  721. *
  722. * In this respect, consider the special case where bfqq is the very
  723. * first queue being activated. In this case, last_ins_in_burst is
  724. * not yet significant when we get here. But it is easy to verify
  725. * that, whether or not the following condition is true, bfqq will
  726. * end up being inserted into the burst list. In particular the
  727. * list will happen to contain only bfqq. And this is exactly what
  728. * has to happen, as bfqq may be the first queue in a possible
  729. * burst.
  730. */
  731. if (time_is_before_jiffies(bfqd->last_ins_in_burst +
  732. bfqd->bfq_burst_interval)) {
  733. bfqd->large_burst = false;
  734. bfq_reset_burst_list(bfqd, bfqq);
  735. return;
  736. }
  737. /*
  738. * If we get here, then bfqq is being activated shortly after the
  739. * last queue. So, if the current burst is also large, we can mark
  740. * bfqq as belonging to this large burst immediately.
  741. */
  742. if (bfqd->large_burst) {
  743. bfq_mark_bfqq_in_large_burst(bfqq);
  744. return;
  745. }
  746. /*
  747. * If we get here, then a large-burst state has not yet been
  748. * reached, but bfqq is being activated shortly after the last
  749. * queue. Then we add bfqq to the burst.
  750. */
  751. bfq_add_to_burst(bfqd, bfqq);
  752. }
  753. static void bfq_add_request(struct request *rq)
  754. {
  755. struct bfq_queue *bfqq = RQ_BFQQ(rq);
  756. struct bfq_entity *entity = &bfqq->entity;
  757. struct bfq_data *bfqd = bfqq->bfqd;
  758. struct request *next_rq, *prev;
  759. unsigned long old_wr_coeff = bfqq->wr_coeff;
  760. bool interactive = false;
  761. bfq_log_bfqq(bfqd, bfqq, "add_request %d", rq_is_sync(rq));
  762. bfqq->queued[rq_is_sync(rq)]++;
  763. bfqd->queued++;
  764. elv_rb_add(&bfqq->sort_list, rq);
  765. /*
  766. * Check if this request is a better next-serve candidate.
  767. */
  768. prev = bfqq->next_rq;
  769. next_rq = bfq_choose_req(bfqd, bfqq->next_rq, rq, bfqd->last_position);
  770. BUG_ON(next_rq == NULL);
  771. bfqq->next_rq = next_rq;
  772. /*
  773. * Adjust priority tree position, if next_rq changes.
  774. */
  775. if (prev != bfqq->next_rq)
  776. bfq_rq_pos_tree_add(bfqd, bfqq);
  777. if (!bfq_bfqq_busy(bfqq)) {
  778. bool soft_rt, coop_or_in_burst,
  779. idle_for_long_time = time_is_before_jiffies(
  780. bfqq->budget_timeout +
  781. bfqd->bfq_wr_min_idle_time);
  782. if (bfq_bfqq_sync(bfqq)) {
  783. bool already_in_burst =
  784. !hlist_unhashed(&bfqq->burst_list_node) ||
  785. bfq_bfqq_in_large_burst(bfqq);
  786. bfq_handle_burst(bfqd, bfqq, idle_for_long_time);
  787. /*
  788. * If bfqq was not already in the current burst,
  789. * then, at this point, bfqq either has been
  790. * added to the current burst or has caused the
  791. * current burst to terminate. In particular, in
  792. * the second case, bfqq has become the first
  793. * queue in a possible new burst.
  794. * In both cases last_ins_in_burst needs to be
  795. * moved forward.
  796. */
  797. if (!already_in_burst)
  798. bfqd->last_ins_in_burst = jiffies;
  799. }
  800. coop_or_in_burst = bfq_bfqq_in_large_burst(bfqq) ||
  801. bfq_bfqq_cooperations(bfqq) >= bfqd->bfq_coop_thresh;
  802. soft_rt = bfqd->bfq_wr_max_softrt_rate > 0 &&
  803. !coop_or_in_burst &&
  804. time_is_before_jiffies(bfqq->soft_rt_next_start);
  805. interactive = !coop_or_in_burst && idle_for_long_time;
  806. entity->budget = max_t(unsigned long, bfqq->max_budget,
  807. bfq_serv_to_charge(next_rq, bfqq));
  808. if (!bfq_bfqq_IO_bound(bfqq)) {
  809. if (time_before(jiffies,
  810. RQ_BIC(rq)->ttime.last_end_request +
  811. bfqd->bfq_slice_idle)) {
  812. bfqq->requests_within_timer++;
  813. if (bfqq->requests_within_timer >=
  814. bfqd->bfq_requests_within_timer)
  815. bfq_mark_bfqq_IO_bound(bfqq);
  816. } else
  817. bfqq->requests_within_timer = 0;
  818. }
  819. if (!bfqd->low_latency)
  820. goto add_bfqq_busy;
  821. if (bfq_bfqq_just_split(bfqq))
  822. goto set_ioprio_changed;
  823. /*
  824. * If the queue:
  825. * - is not being boosted,
  826. * - has been idle for enough time,
  827. * - is not a sync queue or is linked to a bfq_io_cq (it is
  828. * shared "for its nature" or it is not shared and its
  829. * requests have not been redirected to a shared queue)
  830. * start a weight-raising period.
  831. */
  832. if (old_wr_coeff == 1 && (interactive || soft_rt) &&
  833. (!bfq_bfqq_sync(bfqq) || bfqq->bic != NULL)) {
  834. bfqq->wr_coeff = bfqd->bfq_wr_coeff;
  835. if (interactive)
  836. bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
  837. else
  838. bfqq->wr_cur_max_time =
  839. bfqd->bfq_wr_rt_max_time;
  840. bfq_log_bfqq(bfqd, bfqq,
  841. "wrais starting at %lu, rais_max_time %u",
  842. jiffies,
  843. jiffies_to_msecs(bfqq->wr_cur_max_time));
  844. } else if (old_wr_coeff > 1) {
  845. if (interactive)
  846. bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
  847. else if (coop_or_in_burst ||
  848. (bfqq->wr_cur_max_time ==
  849. bfqd->bfq_wr_rt_max_time &&
  850. !soft_rt)) {
  851. bfqq->wr_coeff = 1;
  852. bfq_log_bfqq(bfqd, bfqq,
  853. "wrais ending at %lu, rais_max_time %u",
  854. jiffies,
  855. jiffies_to_msecs(bfqq->
  856. wr_cur_max_time));
  857. } else if (time_before(
  858. bfqq->last_wr_start_finish +
  859. bfqq->wr_cur_max_time,
  860. jiffies +
  861. bfqd->bfq_wr_rt_max_time) &&
  862. soft_rt) {
  863. /*
  864. *
  865. * The remaining weight-raising time is lower
  866. * than bfqd->bfq_wr_rt_max_time, which means
  867. * that the application is enjoying weight
  868. * raising either because deemed soft-rt in
  869. * the near past, or because deemed interactive
  870. * a long ago.
  871. * In both cases, resetting now the current
  872. * remaining weight-raising time for the
  873. * application to the weight-raising duration
  874. * for soft rt applications would not cause any
  875. * latency increase for the application (as the
  876. * new duration would be higher than the
  877. * remaining time).
  878. *
  879. * In addition, the application is now meeting
  880. * the requirements for being deemed soft rt.
  881. * In the end we can correctly and safely
  882. * (re)charge the weight-raising duration for
  883. * the application with the weight-raising
  884. * duration for soft rt applications.
  885. *
  886. * In particular, doing this recharge now, i.e.,
  887. * before the weight-raising period for the
  888. * application finishes, reduces the probability
  889. * of the following negative scenario:
  890. * 1) the weight of a soft rt application is
  891. * raised at startup (as for any newly
  892. * created application),
  893. * 2) since the application is not interactive,
  894. * at a certain time weight-raising is
  895. * stopped for the application,
  896. * 3) at that time the application happens to
  897. * still have pending requests, and hence
  898. * is destined to not have a chance to be
  899. * deemed soft rt before these requests are
  900. * completed (see the comments to the
  901. * function bfq_bfqq_softrt_next_start()
  902. * for details on soft rt detection),
  903. * 4) these pending requests experience a high
  904. * latency because the application is not
  905. * weight-raised while they are pending.
  906. */
  907. bfqq->last_wr_start_finish = jiffies;
  908. bfqq->wr_cur_max_time =
  909. bfqd->bfq_wr_rt_max_time;
  910. }
  911. }
  912. set_ioprio_changed:
  913. if (old_wr_coeff != bfqq->wr_coeff)
  914. entity->ioprio_changed = 1;
  915. add_bfqq_busy:
  916. bfqq->last_idle_bklogged = jiffies;
  917. bfqq->service_from_backlogged = 0;
  918. bfq_clear_bfqq_softrt_update(bfqq);
  919. bfq_add_bfqq_busy(bfqd, bfqq);
  920. } else {
  921. if (bfqd->low_latency && old_wr_coeff == 1 && !rq_is_sync(rq) &&
  922. time_is_before_jiffies(
  923. bfqq->last_wr_start_finish +
  924. bfqd->bfq_wr_min_inter_arr_async)) {
  925. bfqq->wr_coeff = bfqd->bfq_wr_coeff;
  926. bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
  927. bfqd->wr_busy_queues++;
  928. entity->ioprio_changed = 1;
  929. bfq_log_bfqq(bfqd, bfqq,
  930. "non-idle wrais starting at %lu, rais_max_time %u",
  931. jiffies,
  932. jiffies_to_msecs(bfqq->wr_cur_max_time));
  933. }
  934. if (prev != bfqq->next_rq)
  935. bfq_updated_next_req(bfqd, bfqq);
  936. }
  937. if (bfqd->low_latency &&
  938. (old_wr_coeff == 1 || bfqq->wr_coeff == 1 || interactive))
  939. bfqq->last_wr_start_finish = jiffies;
  940. }
  941. static struct request *bfq_find_rq_fmerge(struct bfq_data *bfqd,
  942. struct bio *bio)
  943. {
  944. struct task_struct *tsk = current;
  945. struct bfq_io_cq *bic;
  946. struct bfq_queue *bfqq;
  947. bic = bfq_bic_lookup(bfqd, tsk->io_context);
  948. if (bic == NULL)
  949. return NULL;
  950. bfqq = bic_to_bfqq(bic, bfq_bio_sync(bio));
  951. if (bfqq != NULL) {
  952. sector_t sector = bio->bi_sector + bio_sectors(bio);
  953. return elv_rb_find(&bfqq->sort_list, sector);
  954. }
  955. return NULL;
  956. }
  957. static void bfq_activate_request(struct request_queue *q, struct request *rq)
  958. {
  959. struct bfq_data *bfqd = q->elevator->elevator_data;
  960. bfqd->rq_in_driver++;
  961. bfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
  962. bfq_log(bfqd, "activate_request: new bfqd->last_position %llu",
  963. (long long unsigned)bfqd->last_position);
  964. }
  965. static inline void bfq_deactivate_request(struct request_queue *q,
  966. struct request *rq)
  967. {
  968. struct bfq_data *bfqd = q->elevator->elevator_data;
  969. BUG_ON(bfqd->rq_in_driver == 0);
  970. bfqd->rq_in_driver--;
  971. }
  972. static void bfq_remove_request(struct request *rq)
  973. {
  974. struct bfq_queue *bfqq = RQ_BFQQ(rq);
  975. struct bfq_data *bfqd = bfqq->bfqd;
  976. const int sync = rq_is_sync(rq);
  977. if (bfqq->next_rq == rq) {
  978. bfqq->next_rq = bfq_find_next_rq(bfqd, bfqq, rq);
  979. bfq_updated_next_req(bfqd, bfqq);
  980. }
  981. if (rq->queuelist.prev != &rq->queuelist)
  982. list_del_init(&rq->queuelist);
  983. BUG_ON(bfqq->queued[sync] == 0);
  984. bfqq->queued[sync]--;
  985. bfqd->queued--;
  986. elv_rb_del(&bfqq->sort_list, rq);
  987. if (RB_EMPTY_ROOT(&bfqq->sort_list)) {
  988. if (bfq_bfqq_busy(bfqq) && bfqq != bfqd->in_service_queue)
  989. bfq_del_bfqq_busy(bfqd, bfqq, 1);
  990. /*
  991. * Remove queue from request-position tree as it is empty.
  992. */
  993. if (bfqq->pos_root != NULL) {
  994. rb_erase(&bfqq->pos_node, bfqq->pos_root);
  995. bfqq->pos_root = NULL;
  996. }
  997. }
  998. if (rq->cmd_flags & REQ_META) {
  999. BUG_ON(bfqq->meta_pending == 0);
  1000. bfqq->meta_pending--;
  1001. }
  1002. }
  1003. static int bfq_merge(struct request_queue *q, struct request **req,
  1004. struct bio *bio)
  1005. {
  1006. struct bfq_data *bfqd = q->elevator->elevator_data;
  1007. struct request *__rq;
  1008. __rq = bfq_find_rq_fmerge(bfqd, bio);
  1009. if (__rq != NULL && elv_rq_merge_ok(__rq, bio)) {
  1010. *req = __rq;
  1011. return ELEVATOR_FRONT_MERGE;
  1012. }
  1013. return ELEVATOR_NO_MERGE;
  1014. }
  1015. static void bfq_merged_request(struct request_queue *q, struct request *req,
  1016. int type)
  1017. {
  1018. if (type == ELEVATOR_FRONT_MERGE &&
  1019. rb_prev(&req->rb_node) &&
  1020. blk_rq_pos(req) <
  1021. blk_rq_pos(container_of(rb_prev(&req->rb_node),
  1022. struct request, rb_node))) {
  1023. struct bfq_queue *bfqq = RQ_BFQQ(req);
  1024. struct bfq_data *bfqd = bfqq->bfqd;
  1025. struct request *prev, *next_rq;
  1026. /* Reposition request in its sort_list */
  1027. elv_rb_del(&bfqq->sort_list, req);
  1028. elv_rb_add(&bfqq->sort_list, req);
  1029. /* Choose next request to be served for bfqq */
  1030. prev = bfqq->next_rq;
  1031. next_rq = bfq_choose_req(bfqd, bfqq->next_rq, req,
  1032. bfqd->last_position);
  1033. BUG_ON(next_rq == NULL);
  1034. bfqq->next_rq = next_rq;
  1035. /*
  1036. * If next_rq changes, update both the queue's budget to
  1037. * fit the new request and the queue's position in its
  1038. * rq_pos_tree.
  1039. */
  1040. if (prev != bfqq->next_rq) {
  1041. bfq_updated_next_req(bfqd, bfqq);
  1042. bfq_rq_pos_tree_add(bfqd, bfqq);
  1043. }
  1044. }
  1045. }
  1046. static void bfq_merged_requests(struct request_queue *q, struct request *rq,
  1047. struct request *next)
  1048. {
  1049. struct bfq_queue *bfqq = RQ_BFQQ(rq), *next_bfqq = RQ_BFQQ(next);
  1050. /*
  1051. * If next and rq belong to the same bfq_queue and next is older
  1052. * than rq, then reposition rq in the fifo (by substituting next
  1053. * with rq). Otherwise, if next and rq belong to different
  1054. * bfq_queues, never reposition rq: in fact, we would have to
  1055. * reposition it with respect to next's position in its own fifo,
  1056. * which would most certainly be too expensive with respect to
  1057. * the benefits.
  1058. */
  1059. if (bfqq == next_bfqq &&
  1060. !list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
  1061. time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
  1062. list_del_init(&rq->queuelist);
  1063. list_replace_init(&next->queuelist, &rq->queuelist);
  1064. rq_set_fifo_time(rq, rq_fifo_time(next));
  1065. }
  1066. if (bfqq->next_rq == next)
  1067. bfqq->next_rq = rq;
  1068. bfq_remove_request(next);
  1069. }
  1070. /* Must be called with bfqq != NULL */
  1071. static inline void bfq_bfqq_end_wr(struct bfq_queue *bfqq)
  1072. {
  1073. BUG_ON(bfqq == NULL);
  1074. if (bfq_bfqq_busy(bfqq))
  1075. bfqq->bfqd->wr_busy_queues--;
  1076. bfqq->wr_coeff = 1;
  1077. bfqq->wr_cur_max_time = 0;
  1078. /* Trigger a weight change on the next activation of the queue */
  1079. bfqq->entity.ioprio_changed = 1;
  1080. }
  1081. static void bfq_end_wr_async_queues(struct bfq_data *bfqd,
  1082. struct bfq_group *bfqg)
  1083. {
  1084. int i, j;
  1085. for (i = 0; i < 2; i++)
  1086. for (j = 0; j < IOPRIO_BE_NR; j++)
  1087. if (bfqg->async_bfqq[i][j] != NULL)
  1088. bfq_bfqq_end_wr(bfqg->async_bfqq[i][j]);
  1089. if (bfqg->async_idle_bfqq != NULL)
  1090. bfq_bfqq_end_wr(bfqg->async_idle_bfqq);
  1091. }
  1092. static void bfq_end_wr(struct bfq_data *bfqd)
  1093. {
  1094. struct bfq_queue *bfqq;
  1095. spin_lock_irq(bfqd->queue->queue_lock);
  1096. list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list)
  1097. bfq_bfqq_end_wr(bfqq);
  1098. list_for_each_entry(bfqq, &bfqd->idle_list, bfqq_list)
  1099. bfq_bfqq_end_wr(bfqq);
  1100. bfq_end_wr_async(bfqd);
  1101. spin_unlock_irq(bfqd->queue->queue_lock);
  1102. }
  1103. static inline sector_t bfq_io_struct_pos(void *io_struct, bool request)
  1104. {
  1105. if (request)
  1106. return blk_rq_pos(io_struct);
  1107. else
  1108. return ((struct bio *)io_struct)->bi_sector;
  1109. }
  1110. static inline sector_t bfq_dist_from(sector_t pos1,
  1111. sector_t pos2)
  1112. {
  1113. if (pos1 >= pos2)
  1114. return pos1 - pos2;
  1115. else
  1116. return pos2 - pos1;
  1117. }
  1118. static inline int bfq_rq_close_to_sector(void *io_struct, bool request,
  1119. sector_t sector)
  1120. {
  1121. return bfq_dist_from(bfq_io_struct_pos(io_struct, request), sector) <=
  1122. BFQQ_SEEK_THR;
  1123. }
  1124. static struct bfq_queue *bfqq_close(struct bfq_data *bfqd, sector_t sector)
  1125. {
  1126. struct rb_root *root = &bfqd->rq_pos_tree;
  1127. struct rb_node *parent, *node;
  1128. struct bfq_queue *__bfqq;
  1129. if (RB_EMPTY_ROOT(root))
  1130. return NULL;
  1131. /*
  1132. * First, if we find a request starting at the end of the last
  1133. * request, choose it.
  1134. */
  1135. __bfqq = bfq_rq_pos_tree_lookup(bfqd, root, sector, &parent, NULL);
  1136. if (__bfqq != NULL)
  1137. return __bfqq;
  1138. /*
  1139. * If the exact sector wasn't found, the parent of the NULL leaf
  1140. * will contain the closest sector (rq_pos_tree sorted by
  1141. * next_request position).
  1142. */
  1143. __bfqq = rb_entry(parent, struct bfq_queue, pos_node);
  1144. if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector))
  1145. return __bfqq;
  1146. if (blk_rq_pos(__bfqq->next_rq) < sector)
  1147. node = rb_next(&__bfqq->pos_node);
  1148. else
  1149. node = rb_prev(&__bfqq->pos_node);
  1150. if (node == NULL)
  1151. return NULL;
  1152. __bfqq = rb_entry(node, struct bfq_queue, pos_node);
  1153. if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector))
  1154. return __bfqq;
  1155. return NULL;
  1156. }
  1157. /*
  1158. * bfqd - obvious
  1159. * cur_bfqq - passed in so that we don't decide that the current queue
  1160. * is closely cooperating with itself
  1161. * sector - used as a reference point to search for a close queue
  1162. */
  1163. static struct bfq_queue *bfq_close_cooperator(struct bfq_data *bfqd,
  1164. struct bfq_queue *cur_bfqq,
  1165. sector_t sector)
  1166. {
  1167. struct bfq_queue *bfqq;
  1168. if (bfq_class_idle(cur_bfqq))
  1169. return NULL;
  1170. if (!bfq_bfqq_sync(cur_bfqq))
  1171. return NULL;
  1172. if (BFQQ_SEEKY(cur_bfqq))
  1173. return NULL;
  1174. /* If device has only one backlogged bfq_queue, don't search. */
  1175. if (bfqd->busy_queues == 1)
  1176. return NULL;
  1177. /*
  1178. * We should notice if some of the queues are cooperating, e.g.
  1179. * working closely on the same area of the disk. In that case,
  1180. * we can group them together and don't waste time idling.
  1181. */
  1182. bfqq = bfqq_close(bfqd, sector);
  1183. if (bfqq == NULL || bfqq == cur_bfqq)
  1184. return NULL;
  1185. /*
  1186. * Do not merge queues from different bfq_groups.
  1187. */
  1188. if (bfqq->entity.parent != cur_bfqq->entity.parent)
  1189. return NULL;
  1190. /*
  1191. * It only makes sense to merge sync queues.
  1192. */
  1193. if (!bfq_bfqq_sync(bfqq))
  1194. return NULL;
  1195. if (BFQQ_SEEKY(bfqq))
  1196. return NULL;
  1197. /*
  1198. * Do not merge queues of different priority classes.
  1199. */
  1200. if (bfq_class_rt(bfqq) != bfq_class_rt(cur_bfqq))
  1201. return NULL;
  1202. return bfqq;
  1203. }
  1204. static struct bfq_queue *
  1205. bfq_setup_merge(struct bfq_queue *bfqq, struct bfq_queue *new_bfqq)
  1206. {
  1207. int process_refs, new_process_refs;
  1208. struct bfq_queue *__bfqq;
  1209. /*
  1210. * If there are no process references on the new_bfqq, then it is
  1211. * unsafe to follow the ->new_bfqq chain as other bfqq's in the chain
  1212. * may have dropped their last reference (not just their last process
  1213. * reference).
  1214. */
  1215. if (!bfqq_process_refs(new_bfqq))
  1216. return NULL;
  1217. /* Avoid a circular list and skip interim queue merges. */
  1218. while ((__bfqq = new_bfqq->new_bfqq)) {
  1219. if (__bfqq == bfqq)
  1220. return NULL;
  1221. new_bfqq = __bfqq;
  1222. }
  1223. process_refs = bfqq_process_refs(bfqq);
  1224. new_process_refs = bfqq_process_refs(new_bfqq);
  1225. /*
  1226. * If the process for the bfqq has gone away, there is no
  1227. * sense in merging the queues.
  1228. */
  1229. if (process_refs == 0 || new_process_refs == 0)
  1230. return NULL;
  1231. bfq_log_bfqq(bfqq->bfqd, bfqq, "scheduling merge with queue %d",
  1232. new_bfqq->pid);
  1233. /*
  1234. * Merging is just a redirection: the requests of the process
  1235. * owning one of the two queues are redirected to the other queue.
  1236. * The latter queue, in its turn, is set as shared if this is the
  1237. * first time that the requests of some process are redirected to
  1238. * it.
  1239. *
  1240. * We redirect bfqq to new_bfqq and not the opposite, because we
  1241. * are in the context of the process owning bfqq, hence we have
  1242. * the io_cq of this process. So we can immediately configure this
  1243. * io_cq to redirect the requests of the process to new_bfqq.
  1244. *
  1245. * NOTE, even if new_bfqq coincides with the in-service queue, the
  1246. * io_cq of new_bfqq is not available, because, if the in-service
  1247. * queue is shared, bfqd->in_service_bic may not point to the
  1248. * io_cq of the in-service queue.
  1249. * Redirecting the requests of the process owning bfqq to the
  1250. * currently in-service queue is in any case the best option, as
  1251. * we feed the in-service queue with new requests close to the
  1252. * last request served and, by doing so, hopefully increase the
  1253. * throughput.
  1254. */
  1255. bfqq->new_bfqq = new_bfqq;
  1256. atomic_add(process_refs, &new_bfqq->ref);
  1257. return new_bfqq;
  1258. }
  1259. /*
  1260. * Attempt to schedule a merge of bfqq with the currently in-service queue
  1261. * or with a close queue among the scheduled queues.
  1262. * Return NULL if no merge was scheduled, a pointer to the shared bfq_queue
  1263. * structure otherwise.
  1264. *
  1265. * The OOM queue is not allowed to participate to cooperation: in fact, since
  1266. * the requests temporarily redirected to the OOM queue could be redirected
  1267. * again to dedicated queues at any time, the state needed to correctly
  1268. * handle merging with the OOM queue would be quite complex and expensive
  1269. * to maintain. Besides, in such a critical condition as an out of memory,
  1270. * the benefits of queue merging may be little relevant, or even negligible.
  1271. */
  1272. static struct bfq_queue *
  1273. bfq_setup_cooperator(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  1274. void *io_struct, bool request)
  1275. {
  1276. struct bfq_queue *in_service_bfqq, *new_bfqq;
  1277. if (bfqq->new_bfqq)
  1278. return bfqq->new_bfqq;
  1279. if (!io_struct || unlikely(bfqq == &bfqd->oom_bfqq))
  1280. return NULL;
  1281. in_service_bfqq = bfqd->in_service_queue;
  1282. if (in_service_bfqq == NULL || in_service_bfqq == bfqq ||
  1283. !bfqd->in_service_bic ||
  1284. unlikely(in_service_bfqq == &bfqd->oom_bfqq))
  1285. goto check_scheduled;
  1286. if (bfq_class_idle(in_service_bfqq) || bfq_class_idle(bfqq))
  1287. goto check_scheduled;
  1288. if (bfq_class_rt(in_service_bfqq) != bfq_class_rt(bfqq))
  1289. goto check_scheduled;
  1290. if (in_service_bfqq->entity.parent != bfqq->entity.parent)
  1291. goto check_scheduled;
  1292. if (bfq_rq_close_to_sector(io_struct, request, bfqd->last_position) &&
  1293. bfq_bfqq_sync(in_service_bfqq) && bfq_bfqq_sync(bfqq)) {
  1294. new_bfqq = bfq_setup_merge(bfqq, in_service_bfqq);
  1295. if (new_bfqq != NULL)
  1296. return new_bfqq; /* Merge with in-service queue */
  1297. }
  1298. /*
  1299. * Check whether there is a cooperator among currently scheduled
  1300. * queues. The only thing we need is that the bio/request is not
  1301. * NULL, as we need it to establish whether a cooperator exists.
  1302. */
  1303. check_scheduled:
  1304. new_bfqq = bfq_close_cooperator(bfqd, bfqq,
  1305. bfq_io_struct_pos(io_struct, request));
  1306. if (new_bfqq && likely(new_bfqq != &bfqd->oom_bfqq))
  1307. return bfq_setup_merge(bfqq, new_bfqq);
  1308. return NULL;
  1309. }
  1310. static inline void
  1311. bfq_bfqq_save_state(struct bfq_queue *bfqq)
  1312. {
  1313. /*
  1314. * If bfqq->bic == NULL, the queue is already shared or its requests
  1315. * have already been redirected to a shared queue; both idle window
  1316. * and weight raising state have already been saved. Do nothing.
  1317. */
  1318. if (bfqq->bic == NULL)
  1319. return;
  1320. if (bfqq->bic->wr_time_left)
  1321. /*
  1322. * This is the queue of a just-started process, and would
  1323. * deserve weight raising: we set wr_time_left to the full
  1324. * weight-raising duration to trigger weight-raising when
  1325. * and if the queue is split and the first request of the
  1326. * queue is enqueued.
  1327. */
  1328. bfqq->bic->wr_time_left = bfq_wr_duration(bfqq->bfqd);
  1329. else if (bfqq->wr_coeff > 1) {
  1330. unsigned long wr_duration =
  1331. jiffies - bfqq->last_wr_start_finish;
  1332. /*
  1333. * It may happen that a queue's weight raising period lasts
  1334. * longer than its wr_cur_max_time, as weight raising is
  1335. * handled only when a request is enqueued or dispatched (it
  1336. * does not use any timer). If the weight raising period is
  1337. * about to end, don't save it.
  1338. */
  1339. if (bfqq->wr_cur_max_time <= wr_duration)
  1340. bfqq->bic->wr_time_left = 0;
  1341. else
  1342. bfqq->bic->wr_time_left =
  1343. bfqq->wr_cur_max_time - wr_duration;
  1344. /*
  1345. * The bfq_queue is becoming shared or the requests of the
  1346. * process owning the queue are being redirected to a shared
  1347. * queue. Stop the weight raising period of the queue, as in
  1348. * both cases it should not be owned by an interactive or
  1349. * soft real-time application.
  1350. */
  1351. bfq_bfqq_end_wr(bfqq);
  1352. } else
  1353. bfqq->bic->wr_time_left = 0;
  1354. bfqq->bic->saved_idle_window = bfq_bfqq_idle_window(bfqq);
  1355. bfqq->bic->saved_IO_bound = bfq_bfqq_IO_bound(bfqq);
  1356. bfqq->bic->saved_in_large_burst = bfq_bfqq_in_large_burst(bfqq);
  1357. bfqq->bic->was_in_burst_list = !hlist_unhashed(&bfqq->burst_list_node);
  1358. bfqq->bic->cooperations++;
  1359. bfqq->bic->failed_cooperations = 0;
  1360. }
  1361. static inline void
  1362. bfq_get_bic_reference(struct bfq_queue *bfqq)
  1363. {
  1364. /*
  1365. * If bfqq->bic has a non-NULL value, the bic to which it belongs
  1366. * is about to begin using a shared bfq_queue.
  1367. */
  1368. if (bfqq->bic)
  1369. atomic_long_inc(&bfqq->bic->icq.ioc->refcount);
  1370. }
  1371. static void
  1372. bfq_merge_bfqqs(struct bfq_data *bfqd, struct bfq_io_cq *bic,
  1373. struct bfq_queue *bfqq, struct bfq_queue *new_bfqq)
  1374. {
  1375. bfq_log_bfqq(bfqd, bfqq, "merging with queue %lu",
  1376. (long unsigned)new_bfqq->pid);
  1377. /* Save weight raising and idle window of the merged queues */
  1378. bfq_bfqq_save_state(bfqq);
  1379. bfq_bfqq_save_state(new_bfqq);
  1380. if (bfq_bfqq_IO_bound(bfqq))
  1381. bfq_mark_bfqq_IO_bound(new_bfqq);
  1382. bfq_clear_bfqq_IO_bound(bfqq);
  1383. /*
  1384. * Grab a reference to the bic, to prevent it from being destroyed
  1385. * before being possibly touched by a bfq_split_bfqq().
  1386. */
  1387. bfq_get_bic_reference(bfqq);
  1388. bfq_get_bic_reference(new_bfqq);
  1389. /*
  1390. * Merge queues (that is, let bic redirect its requests to new_bfqq)
  1391. */
  1392. bic_set_bfqq(bic, new_bfqq, 1);
  1393. bfq_mark_bfqq_coop(new_bfqq);
  1394. /*
  1395. * new_bfqq now belongs to at least two bics (it is a shared queue):
  1396. * set new_bfqq->bic to NULL. bfqq either:
  1397. * - does not belong to any bic any more, and hence bfqq->bic must
  1398. * be set to NULL, or
  1399. * - is a queue whose owning bics have already been redirected to a
  1400. * different queue, hence the queue is destined to not belong to
  1401. * any bic soon and bfqq->bic is already NULL (therefore the next
  1402. * assignment causes no harm).
  1403. */
  1404. new_bfqq->bic = NULL;
  1405. bfqq->bic = NULL;
  1406. bfq_put_queue(bfqq);
  1407. }
  1408. static inline void bfq_bfqq_increase_failed_cooperations(struct bfq_queue *bfqq)
  1409. {
  1410. struct bfq_io_cq *bic = bfqq->bic;
  1411. struct bfq_data *bfqd = bfqq->bfqd;
  1412. if (bic && bfq_bfqq_cooperations(bfqq) >= bfqd->bfq_coop_thresh) {
  1413. bic->failed_cooperations++;
  1414. if (bic->failed_cooperations >= bfqd->bfq_failed_cooperations)
  1415. bic->cooperations = 0;
  1416. }
  1417. }
  1418. static int bfq_allow_merge(struct request_queue *q, struct request *rq,
  1419. struct bio *bio)
  1420. {
  1421. struct bfq_data *bfqd = q->elevator->elevator_data;
  1422. struct bfq_io_cq *bic;
  1423. struct bfq_queue *bfqq, *new_bfqq;
  1424. /*
  1425. * Disallow merge of a sync bio into an async request.
  1426. */
  1427. if (bfq_bio_sync(bio) && !rq_is_sync(rq))
  1428. return 0;
  1429. /*
  1430. * Lookup the bfqq that this bio will be queued with. Allow
  1431. * merge only if rq is queued there.
  1432. * Queue lock is held here.
  1433. */
  1434. bic = bfq_bic_lookup(bfqd, current->io_context);
  1435. if (bic == NULL)
  1436. return 0;
  1437. bfqq = bic_to_bfqq(bic, bfq_bio_sync(bio));
  1438. /*
  1439. * We take advantage of this function to perform an early merge
  1440. * of the queues of possible cooperating processes.
  1441. */
  1442. if (bfqq != NULL) {
  1443. new_bfqq = bfq_setup_cooperator(bfqd, bfqq, bio, false);
  1444. if (new_bfqq != NULL) {
  1445. bfq_merge_bfqqs(bfqd, bic, bfqq, new_bfqq);
  1446. /*
  1447. * If we get here, the bio will be queued in the
  1448. * shared queue, i.e., new_bfqq, so use new_bfqq
  1449. * to decide whether bio and rq can be merged.
  1450. */
  1451. bfqq = new_bfqq;
  1452. } else
  1453. bfq_bfqq_increase_failed_cooperations(bfqq);
  1454. }
  1455. return bfqq == RQ_BFQQ(rq);
  1456. }
  1457. static void __bfq_set_in_service_queue(struct bfq_data *bfqd,
  1458. struct bfq_queue *bfqq)
  1459. {
  1460. if (bfqq != NULL) {
  1461. bfq_mark_bfqq_must_alloc(bfqq);
  1462. bfq_mark_bfqq_budget_new(bfqq);
  1463. bfq_clear_bfqq_fifo_expire(bfqq);
  1464. bfqd->budgets_assigned = (bfqd->budgets_assigned*7 + 256) / 8;
  1465. bfq_log_bfqq(bfqd, bfqq,
  1466. "set_in_service_queue, cur-budget = %lu",
  1467. bfqq->entity.budget);
  1468. }
  1469. bfqd->in_service_queue = bfqq;
  1470. }
  1471. /*
  1472. * Get and set a new queue for service.
  1473. */
  1474. static struct bfq_queue *bfq_set_in_service_queue(struct bfq_data *bfqd)
  1475. {
  1476. struct bfq_queue *bfqq = bfq_get_next_queue(bfqd);
  1477. __bfq_set_in_service_queue(bfqd, bfqq);
  1478. return bfqq;
  1479. }
  1480. /*
  1481. * If enough samples have been computed, return the current max budget
  1482. * stored in bfqd, which is dynamically updated according to the
  1483. * estimated disk peak rate; otherwise return the default max budget
  1484. */
  1485. static inline unsigned long bfq_max_budget(struct bfq_data *bfqd)
  1486. {
  1487. if (bfqd->budgets_assigned < 194)
  1488. return bfq_default_max_budget;
  1489. else
  1490. return bfqd->bfq_max_budget;
  1491. }
  1492. /*
  1493. * Return min budget, which is a fraction of the current or default
  1494. * max budget (trying with 1/32)
  1495. */
  1496. static inline unsigned long bfq_min_budget(struct bfq_data *bfqd)
  1497. {
  1498. if (bfqd->budgets_assigned < 194)
  1499. return bfq_default_max_budget / 32;
  1500. else
  1501. return bfqd->bfq_max_budget / 32;
  1502. }
  1503. static void bfq_arm_slice_timer(struct bfq_data *bfqd)
  1504. {
  1505. struct bfq_queue *bfqq = bfqd->in_service_queue;
  1506. struct bfq_io_cq *bic;
  1507. unsigned long sl;
  1508. BUG_ON(!RB_EMPTY_ROOT(&bfqq->sort_list));
  1509. /* Processes have exited, don't wait. */
  1510. bic = bfqd->in_service_bic;
  1511. if (bic == NULL || atomic_read(&bic->icq.ioc->nr_tasks) == 0)
  1512. return;
  1513. bfq_mark_bfqq_wait_request(bfqq);
  1514. /*
  1515. * We don't want to idle for seeks, but we do want to allow
  1516. * fair distribution of slice time for a process doing back-to-back
  1517. * seeks. So allow a little bit of time for him to submit a new rq.
  1518. *
  1519. * To prevent processes with (partly) seeky workloads from
  1520. * being too ill-treated, grant them a small fraction of the
  1521. * assigned budget before reducing the waiting time to
  1522. * BFQ_MIN_TT. This happened to help reduce latency.
  1523. */
  1524. sl = bfqd->bfq_slice_idle;
  1525. /*
  1526. * Unless the queue is being weight-raised or the scenario is
  1527. * asymmetric, grant only minimum idle time if the queue either
  1528. * has been seeky for long enough or has already proved to be
  1529. * constantly seeky.
  1530. */
  1531. if (bfq_sample_valid(bfqq->seek_samples) &&
  1532. ((BFQQ_SEEKY(bfqq) && bfqq->entity.service >
  1533. bfq_max_budget(bfqq->bfqd) / 8) ||
  1534. bfq_bfqq_constantly_seeky(bfqq)) && bfqq->wr_coeff == 1 &&
  1535. symmetric_scenario)
  1536. sl = min(sl, msecs_to_jiffies(BFQ_MIN_TT));
  1537. else if (bfqq->wr_coeff > 1)
  1538. sl = sl * 3;
  1539. bfqd->last_idling_start = ktime_get();
  1540. mod_timer(&bfqd->idle_slice_timer, jiffies + sl);
  1541. bfq_log(bfqd, "arm idle: %u/%u ms",
  1542. jiffies_to_msecs(sl), jiffies_to_msecs(bfqd->bfq_slice_idle));
  1543. }
  1544. /*
  1545. * Set the maximum time for the in-service queue to consume its
  1546. * budget. This prevents seeky processes from lowering the disk
  1547. * throughput (always guaranteed with a time slice scheme as in CFQ).
  1548. */
  1549. static void bfq_set_budget_timeout(struct bfq_data *bfqd)
  1550. {
  1551. struct bfq_queue *bfqq = bfqd->in_service_queue;
  1552. unsigned int timeout_coeff;
  1553. if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time)
  1554. timeout_coeff = 1;
  1555. else
  1556. timeout_coeff = bfqq->entity.weight / bfqq->entity.orig_weight;
  1557. bfqd->last_budget_start = ktime_get();
  1558. bfq_clear_bfqq_budget_new(bfqq);
  1559. bfqq->budget_timeout = jiffies +
  1560. bfqd->bfq_timeout[bfq_bfqq_sync(bfqq)] * timeout_coeff;
  1561. bfq_log_bfqq(bfqd, bfqq, "set budget_timeout %u",
  1562. jiffies_to_msecs(bfqd->bfq_timeout[bfq_bfqq_sync(bfqq)] *
  1563. timeout_coeff));
  1564. }
  1565. /*
  1566. * Move request from internal lists to the request queue dispatch list.
  1567. */
  1568. static void bfq_dispatch_insert(struct request_queue *q, struct request *rq)
  1569. {
  1570. struct bfq_data *bfqd = q->elevator->elevator_data;
  1571. struct bfq_queue *bfqq = RQ_BFQQ(rq);
  1572. /*
  1573. * For consistency, the next instruction should have been executed
  1574. * after removing the request from the queue and dispatching it.
  1575. * We execute instead this instruction before bfq_remove_request()
  1576. * (and hence introduce a temporary inconsistency), for efficiency.
  1577. * In fact, in a forced_dispatch, this prevents two counters related
  1578. * to bfqq->dispatched to risk to be uselessly decremented if bfqq
  1579. * is not in service, and then to be incremented again after
  1580. * incrementing bfqq->dispatched.
  1581. */
  1582. bfqq->dispatched++;
  1583. bfq_remove_request(rq);
  1584. elv_dispatch_sort(q, rq);
  1585. if (bfq_bfqq_sync(bfqq))
  1586. bfqd->sync_flight++;
  1587. }
  1588. /*
  1589. * Return expired entry, or NULL to just start from scratch in rbtree.
  1590. */
  1591. static struct request *bfq_check_fifo(struct bfq_queue *bfqq)
  1592. {
  1593. struct request *rq = NULL;
  1594. if (bfq_bfqq_fifo_expire(bfqq))
  1595. return NULL;
  1596. bfq_mark_bfqq_fifo_expire(bfqq);
  1597. if (list_empty(&bfqq->fifo))
  1598. return NULL;
  1599. rq = rq_entry_fifo(bfqq->fifo.next);
  1600. if (time_before(jiffies, rq_fifo_time(rq)))
  1601. return NULL;
  1602. return rq;
  1603. }
  1604. static inline unsigned long bfq_bfqq_budget_left(struct bfq_queue *bfqq)
  1605. {
  1606. struct bfq_entity *entity = &bfqq->entity;
  1607. return entity->budget - entity->service;
  1608. }
  1609. static void __bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  1610. {
  1611. BUG_ON(bfqq != bfqd->in_service_queue);
  1612. __bfq_bfqd_reset_in_service(bfqd);
  1613. /*
  1614. * If this bfqq is shared between multiple processes, check
  1615. * to make sure that those processes are still issuing I/Os
  1616. * within the mean seek distance. If not, it may be time to
  1617. * break the queues apart again.
  1618. */
  1619. if (bfq_bfqq_coop(bfqq) && BFQQ_SEEKY(bfqq))
  1620. bfq_mark_bfqq_split_coop(bfqq);
  1621. if (RB_EMPTY_ROOT(&bfqq->sort_list)) {
  1622. /*
  1623. * Overloading budget_timeout field to store the time
  1624. * at which the queue remains with no backlog; used by
  1625. * the weight-raising mechanism.
  1626. */
  1627. bfqq->budget_timeout = jiffies;
  1628. bfq_del_bfqq_busy(bfqd, bfqq, 1);
  1629. } else {
  1630. bfq_activate_bfqq(bfqd, bfqq);
  1631. /*
  1632. * Resort priority tree of potential close cooperators.
  1633. */
  1634. bfq_rq_pos_tree_add(bfqd, bfqq);
  1635. }
  1636. }
  1637. /**
  1638. * __bfq_bfqq_recalc_budget - try to adapt the budget to the @bfqq behavior.
  1639. * @bfqd: device data.
  1640. * @bfqq: queue to update.
  1641. * @reason: reason for expiration.
  1642. *
  1643. * Handle the feedback on @bfqq budget. See the body for detailed
  1644. * comments.
  1645. */
  1646. static void __bfq_bfqq_recalc_budget(struct bfq_data *bfqd,
  1647. struct bfq_queue *bfqq,
  1648. enum bfqq_expiration reason)
  1649. {
  1650. struct request *next_rq;
  1651. unsigned long budget, min_budget;
  1652. budget = bfqq->max_budget;
  1653. min_budget = bfq_min_budget(bfqd);
  1654. BUG_ON(bfqq != bfqd->in_service_queue);
  1655. bfq_log_bfqq(bfqd, bfqq, "recalc_budg: last budg %lu, budg left %lu",
  1656. bfqq->entity.budget, bfq_bfqq_budget_left(bfqq));
  1657. bfq_log_bfqq(bfqd, bfqq, "recalc_budg: last max_budg %lu, min budg %lu",
  1658. budget, bfq_min_budget(bfqd));
  1659. bfq_log_bfqq(bfqd, bfqq, "recalc_budg: sync %d, seeky %d",
  1660. bfq_bfqq_sync(bfqq), BFQQ_SEEKY(bfqd->in_service_queue));
  1661. if (bfq_bfqq_sync(bfqq)) {
  1662. switch (reason) {
  1663. /*
  1664. * Caveat: in all the following cases we trade latency
  1665. * for throughput.
  1666. */
  1667. case BFQ_BFQQ_TOO_IDLE:
  1668. /*
  1669. * This is the only case where we may reduce
  1670. * the budget: if there is no request of the
  1671. * process still waiting for completion, then
  1672. * we assume (tentatively) that the timer has
  1673. * expired because the batch of requests of
  1674. * the process could have been served with a
  1675. * smaller budget. Hence, betting that
  1676. * process will behave in the same way when it
  1677. * becomes backlogged again, we reduce its
  1678. * next budget. As long as we guess right,
  1679. * this budget cut reduces the latency
  1680. * experienced by the process.
  1681. *
  1682. * However, if there are still outstanding
  1683. * requests, then the process may have not yet
  1684. * issued its next request just because it is
  1685. * still waiting for the completion of some of
  1686. * the still outstanding ones. So in this
  1687. * subcase we do not reduce its budget, on the
  1688. * contrary we increase it to possibly boost
  1689. * the throughput, as discussed in the
  1690. * comments to the BUDGET_TIMEOUT case.
  1691. */
  1692. if (bfqq->dispatched > 0) /* still outstanding reqs */
  1693. budget = min(budget * 2, bfqd->bfq_max_budget);
  1694. else {
  1695. if (budget > 5 * min_budget)
  1696. budget -= 4 * min_budget;
  1697. else
  1698. budget = min_budget;
  1699. }
  1700. break;
  1701. case BFQ_BFQQ_BUDGET_TIMEOUT:
  1702. /*
  1703. * We double the budget here because: 1) it
  1704. * gives the chance to boost the throughput if
  1705. * this is not a seeky process (which may have
  1706. * bumped into this timeout because of, e.g.,
  1707. * ZBR), 2) together with charge_full_budget
  1708. * it helps give seeky processes higher
  1709. * timestamps, and hence be served less
  1710. * frequently.
  1711. */
  1712. budget = min(budget * 2, bfqd->bfq_max_budget);
  1713. break;
  1714. case BFQ_BFQQ_BUDGET_EXHAUSTED:
  1715. /*
  1716. * The process still has backlog, and did not
  1717. * let either the budget timeout or the disk
  1718. * idling timeout expire. Hence it is not
  1719. * seeky, has a short thinktime and may be
  1720. * happy with a higher budget too. So
  1721. * definitely increase the budget of this good
  1722. * candidate to boost the disk throughput.
  1723. */
  1724. budget = min(budget * 4, bfqd->bfq_max_budget);
  1725. break;
  1726. case BFQ_BFQQ_NO_MORE_REQUESTS:
  1727. /*
  1728. * Leave the budget unchanged.
  1729. */
  1730. default:
  1731. return;
  1732. }
  1733. } else /* async queue */
  1734. /* async queues get always the maximum possible budget
  1735. * (their ability to dispatch is limited by
  1736. * @bfqd->bfq_max_budget_async_rq).
  1737. */
  1738. budget = bfqd->bfq_max_budget;
  1739. bfqq->max_budget = budget;
  1740. if (bfqd->budgets_assigned >= 194 && bfqd->bfq_user_max_budget == 0 &&
  1741. bfqq->max_budget > bfqd->bfq_max_budget)
  1742. bfqq->max_budget = bfqd->bfq_max_budget;
  1743. /*
  1744. * Make sure that we have enough budget for the next request.
  1745. * Since the finish time of the bfqq must be kept in sync with
  1746. * the budget, be sure to call __bfq_bfqq_expire() after the
  1747. * update.
  1748. */
  1749. next_rq = bfqq->next_rq;
  1750. if (next_rq != NULL)
  1751. bfqq->entity.budget = max_t(unsigned long, bfqq->max_budget,
  1752. bfq_serv_to_charge(next_rq, bfqq));
  1753. else
  1754. bfqq->entity.budget = bfqq->max_budget;
  1755. bfq_log_bfqq(bfqd, bfqq, "head sect: %u, new budget %lu",
  1756. next_rq != NULL ? blk_rq_sectors(next_rq) : 0,
  1757. bfqq->entity.budget);
  1758. }
  1759. static unsigned long bfq_calc_max_budget(u64 peak_rate, u64 timeout)
  1760. {
  1761. unsigned long max_budget;
  1762. /*
  1763. * The max_budget calculated when autotuning is equal to the
  1764. * amount of sectors transfered in timeout_sync at the
  1765. * estimated peak rate.
  1766. */
  1767. max_budget = (unsigned long)(peak_rate * 1000 *
  1768. timeout >> BFQ_RATE_SHIFT);
  1769. return max_budget;
  1770. }
  1771. /*
  1772. * In addition to updating the peak rate, checks whether the process
  1773. * is "slow", and returns 1 if so. This slow flag is used, in addition
  1774. * to the budget timeout, to reduce the amount of service provided to
  1775. * seeky processes, and hence reduce their chances to lower the
  1776. * throughput. See the code for more details.
  1777. */
  1778. static int bfq_update_peak_rate(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  1779. int compensate, enum bfqq_expiration reason)
  1780. {
  1781. u64 bw, usecs, expected, timeout;
  1782. ktime_t delta;
  1783. int update = 0;
  1784. if (!bfq_bfqq_sync(bfqq) || bfq_bfqq_budget_new(bfqq))
  1785. return 0;
  1786. if (compensate)
  1787. delta = bfqd->last_idling_start;
  1788. else
  1789. delta = ktime_get();
  1790. delta = ktime_sub(delta, bfqd->last_budget_start);
  1791. usecs = ktime_to_us(delta);
  1792. /* Don't trust short/unrealistic values. */
  1793. if (usecs < 100 || usecs >= LONG_MAX)
  1794. return 0;
  1795. /*
  1796. * Calculate the bandwidth for the last slice. We use a 64 bit
  1797. * value to store the peak rate, in sectors per usec in fixed
  1798. * point math. We do so to have enough precision in the estimate
  1799. * and to avoid overflows.
  1800. */
  1801. bw = (u64)bfqq->entity.service << BFQ_RATE_SHIFT;
  1802. do_div(bw, (unsigned long)usecs);
  1803. timeout = jiffies_to_msecs(bfqd->bfq_timeout[BLK_RW_SYNC]);
  1804. /*
  1805. * Use only long (> 20ms) intervals to filter out spikes for
  1806. * the peak rate estimation.
  1807. */
  1808. if (usecs > 20000) {
  1809. if (bw > bfqd->peak_rate ||
  1810. (!BFQQ_SEEKY(bfqq) &&
  1811. reason == BFQ_BFQQ_BUDGET_TIMEOUT)) {
  1812. bfq_log(bfqd, "measured bw =%llu", bw);
  1813. /*
  1814. * To smooth oscillations use a low-pass filter with
  1815. * alpha=7/8, i.e.,
  1816. * new_rate = (7/8) * old_rate + (1/8) * bw
  1817. */
  1818. do_div(bw, 8);
  1819. if (bw == 0)
  1820. return 0;
  1821. bfqd->peak_rate *= 7;
  1822. do_div(bfqd->peak_rate, 8);
  1823. bfqd->peak_rate += bw;
  1824. update = 1;
  1825. bfq_log(bfqd, "new peak_rate=%llu", bfqd->peak_rate);
  1826. }
  1827. update |= bfqd->peak_rate_samples == BFQ_PEAK_RATE_SAMPLES - 1;
  1828. if (bfqd->peak_rate_samples < BFQ_PEAK_RATE_SAMPLES)
  1829. bfqd->peak_rate_samples++;
  1830. if (bfqd->peak_rate_samples == BFQ_PEAK_RATE_SAMPLES &&
  1831. update) {
  1832. int dev_type = blk_queue_nonrot(bfqd->queue);
  1833. if (bfqd->bfq_user_max_budget == 0) {
  1834. bfqd->bfq_max_budget =
  1835. bfq_calc_max_budget(bfqd->peak_rate,
  1836. timeout);
  1837. bfq_log(bfqd, "new max_budget=%lu",
  1838. bfqd->bfq_max_budget);
  1839. }
  1840. if (bfqd->device_speed == BFQ_BFQD_FAST &&
  1841. bfqd->peak_rate < device_speed_thresh[dev_type]) {
  1842. bfqd->device_speed = BFQ_BFQD_SLOW;
  1843. bfqd->RT_prod = R_slow[dev_type] *
  1844. T_slow[dev_type];
  1845. } else if (bfqd->device_speed == BFQ_BFQD_SLOW &&
  1846. bfqd->peak_rate > device_speed_thresh[dev_type]) {
  1847. bfqd->device_speed = BFQ_BFQD_FAST;
  1848. bfqd->RT_prod = R_fast[dev_type] *
  1849. T_fast[dev_type];
  1850. }
  1851. }
  1852. }
  1853. /*
  1854. * If the process has been served for a too short time
  1855. * interval to let its possible sequential accesses prevail on
  1856. * the initial seek time needed to move the disk head on the
  1857. * first sector it requested, then give the process a chance
  1858. * and for the moment return false.
  1859. */
  1860. if (bfqq->entity.budget <= bfq_max_budget(bfqd) / 8)
  1861. return 0;
  1862. /*
  1863. * A process is considered ``slow'' (i.e., seeky, so that we
  1864. * cannot treat it fairly in the service domain, as it would
  1865. * slow down too much the other processes) if, when a slice
  1866. * ends for whatever reason, it has received service at a
  1867. * rate that would not be high enough to complete the budget
  1868. * before the budget timeout expiration.
  1869. */
  1870. expected = bw * 1000 * timeout >> BFQ_RATE_SHIFT;
  1871. /*
  1872. * Caveat: processes doing IO in the slower disk zones will
  1873. * tend to be slow(er) even if not seeky. And the estimated
  1874. * peak rate will actually be an average over the disk
  1875. * surface. Hence, to not be too harsh with unlucky processes,
  1876. * we keep a budget/3 margin of safety before declaring a
  1877. * process slow.
  1878. */
  1879. return expected > (4 * bfqq->entity.budget) / 3;
  1880. }
  1881. /*
  1882. * To be deemed as soft real-time, an application must meet two
  1883. * requirements. First, the application must not require an average
  1884. * bandwidth higher than the approximate bandwidth required to playback or
  1885. * record a compressed high-definition video.
  1886. * The next function is invoked on the completion of the last request of a
  1887. * batch, to compute the next-start time instant, soft_rt_next_start, such
  1888. * that, if the next request of the application does not arrive before
  1889. * soft_rt_next_start, then the above requirement on the bandwidth is met.
  1890. *
  1891. * The second requirement is that the request pattern of the application is
  1892. * isochronous, i.e., that, after issuing a request or a batch of requests,
  1893. * the application stops issuing new requests until all its pending requests
  1894. * have been completed. After that, the application may issue a new batch,
  1895. * and so on.
  1896. * For this reason the next function is invoked to compute
  1897. * soft_rt_next_start only for applications that meet this requirement,
  1898. * whereas soft_rt_next_start is set to infinity for applications that do
  1899. * not.
  1900. *
  1901. * Unfortunately, even a greedy application may happen to behave in an
  1902. * isochronous way if the CPU load is high. In fact, the application may
  1903. * stop issuing requests while the CPUs are busy serving other processes,
  1904. * then restart, then stop again for a while, and so on. In addition, if
  1905. * the disk achieves a low enough throughput with the request pattern
  1906. * issued by the application (e.g., because the request pattern is random
  1907. * and/or the device is slow), then the application may meet the above
  1908. * bandwidth requirement too. To prevent such a greedy application to be
  1909. * deemed as soft real-time, a further rule is used in the computation of
  1910. * soft_rt_next_start: soft_rt_next_start must be higher than the current
  1911. * time plus the maximum time for which the arrival of a request is waited
  1912. * for when a sync queue becomes idle, namely bfqd->bfq_slice_idle.
  1913. * This filters out greedy applications, as the latter issue instead their
  1914. * next request as soon as possible after the last one has been completed
  1915. * (in contrast, when a batch of requests is completed, a soft real-time
  1916. * application spends some time processing data).
  1917. *
  1918. * Unfortunately, the last filter may easily generate false positives if
  1919. * only bfqd->bfq_slice_idle is used as a reference time interval and one
  1920. * or both the following cases occur:
  1921. * 1) HZ is so low that the duration of a jiffy is comparable to or higher
  1922. * than bfqd->bfq_slice_idle. This happens, e.g., on slow devices with
  1923. * HZ=100.
  1924. * 2) jiffies, instead of increasing at a constant rate, may stop increasing
  1925. * for a while, then suddenly 'jump' by several units to recover the lost
  1926. * increments. This seems to happen, e.g., inside virtual machines.
  1927. * To address this issue, we do not use as a reference time interval just
  1928. * bfqd->bfq_slice_idle, but bfqd->bfq_slice_idle plus a few jiffies. In
  1929. * particular we add the minimum number of jiffies for which the filter
  1930. * seems to be quite precise also in embedded systems and KVM/QEMU virtual
  1931. * machines.
  1932. */
  1933. static inline unsigned long bfq_bfqq_softrt_next_start(struct bfq_data *bfqd,
  1934. struct bfq_queue *bfqq)
  1935. {
  1936. return max(bfqq->last_idle_bklogged +
  1937. HZ * bfqq->service_from_backlogged /
  1938. bfqd->bfq_wr_max_softrt_rate,
  1939. jiffies + bfqq->bfqd->bfq_slice_idle + 4);
  1940. }
  1941. /*
  1942. * Return the largest-possible time instant such that, for as long as possible,
  1943. * the current time will be lower than this time instant according to the macro
  1944. * time_is_before_jiffies().
  1945. */
  1946. static inline unsigned long bfq_infinity_from_now(unsigned long now)
  1947. {
  1948. return now + ULONG_MAX / 2;
  1949. }
  1950. /**
  1951. * bfq_bfqq_expire - expire a queue.
  1952. * @bfqd: device owning the queue.
  1953. * @bfqq: the queue to expire.
  1954. * @compensate: if true, compensate for the time spent idling.
  1955. * @reason: the reason causing the expiration.
  1956. *
  1957. *
  1958. * If the process associated to the queue is slow (i.e., seeky), or in
  1959. * case of budget timeout, or, finally, if it is async, we
  1960. * artificially charge it an entire budget (independently of the
  1961. * actual service it received). As a consequence, the queue will get
  1962. * higher timestamps than the correct ones upon reactivation, and
  1963. * hence it will be rescheduled as if it had received more service
  1964. * than what it actually received. In the end, this class of processes
  1965. * will receive less service in proportion to how slowly they consume
  1966. * their budgets (and hence how seriously they tend to lower the
  1967. * throughput).
  1968. *
  1969. * In contrast, when a queue expires because it has been idling for
  1970. * too much or because it exhausted its budget, we do not touch the
  1971. * amount of service it has received. Hence when the queue will be
  1972. * reactivated and its timestamps updated, the latter will be in sync
  1973. * with the actual service received by the queue until expiration.
  1974. *
  1975. * Charging a full budget to the first type of queues and the exact
  1976. * service to the others has the effect of using the WF2Q+ policy to
  1977. * schedule the former on a timeslice basis, without violating the
  1978. * service domain guarantees of the latter.
  1979. */
  1980. static void bfq_bfqq_expire(struct bfq_data *bfqd,
  1981. struct bfq_queue *bfqq,
  1982. int compensate,
  1983. enum bfqq_expiration reason)
  1984. {
  1985. int slow;
  1986. BUG_ON(bfqq != bfqd->in_service_queue);
  1987. /* Update disk peak rate for autotuning and check whether the
  1988. * process is slow (see bfq_update_peak_rate).
  1989. */
  1990. slow = bfq_update_peak_rate(bfqd, bfqq, compensate, reason);
  1991. /*
  1992. * As above explained, 'punish' slow (i.e., seeky), timed-out
  1993. * and async queues, to favor sequential sync workloads.
  1994. *
  1995. * Processes doing I/O in the slower disk zones will tend to be
  1996. * slow(er) even if not seeky. Hence, since the estimated peak
  1997. * rate is actually an average over the disk surface, these
  1998. * processes may timeout just for bad luck. To avoid punishing
  1999. * them we do not charge a full budget to a process that
  2000. * succeeded in consuming at least 2/3 of its budget.
  2001. */
  2002. if (slow || (reason == BFQ_BFQQ_BUDGET_TIMEOUT &&
  2003. bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3))
  2004. bfq_bfqq_charge_full_budget(bfqq);
  2005. bfqq->service_from_backlogged += bfqq->entity.service;
  2006. if (BFQQ_SEEKY(bfqq) && reason == BFQ_BFQQ_BUDGET_TIMEOUT &&
  2007. !bfq_bfqq_constantly_seeky(bfqq)) {
  2008. bfq_mark_bfqq_constantly_seeky(bfqq);
  2009. if (!blk_queue_nonrot(bfqd->queue))
  2010. bfqd->const_seeky_busy_in_flight_queues++;
  2011. }
  2012. if (reason == BFQ_BFQQ_TOO_IDLE &&
  2013. bfqq->entity.service <= 2 * bfqq->entity.budget / 10 )
  2014. bfq_clear_bfqq_IO_bound(bfqq);
  2015. if (bfqd->low_latency && bfqq->wr_coeff == 1)
  2016. bfqq->last_wr_start_finish = jiffies;
  2017. if (bfqd->low_latency && bfqd->bfq_wr_max_softrt_rate > 0 &&
  2018. RB_EMPTY_ROOT(&bfqq->sort_list)) {
  2019. /*
  2020. * If we get here, and there are no outstanding requests,
  2021. * then the request pattern is isochronous (see the comments
  2022. * to the function bfq_bfqq_softrt_next_start()). Hence we
  2023. * can compute soft_rt_next_start. If, instead, the queue
  2024. * still has outstanding requests, then we have to wait
  2025. * for the completion of all the outstanding requests to
  2026. * discover whether the request pattern is actually
  2027. * isochronous.
  2028. */
  2029. if (bfqq->dispatched == 0)
  2030. bfqq->soft_rt_next_start =
  2031. bfq_bfqq_softrt_next_start(bfqd, bfqq);
  2032. else {
  2033. /*
  2034. * The application is still waiting for the
  2035. * completion of one or more requests:
  2036. * prevent it from possibly being incorrectly
  2037. * deemed as soft real-time by setting its
  2038. * soft_rt_next_start to infinity. In fact,
  2039. * without this assignment, the application
  2040. * would be incorrectly deemed as soft
  2041. * real-time if:
  2042. * 1) it issued a new request before the
  2043. * completion of all its in-flight
  2044. * requests, and
  2045. * 2) at that time, its soft_rt_next_start
  2046. * happened to be in the past.
  2047. */
  2048. bfqq->soft_rt_next_start =
  2049. bfq_infinity_from_now(jiffies);
  2050. /*
  2051. * Schedule an update of soft_rt_next_start to when
  2052. * the task may be discovered to be isochronous.
  2053. */
  2054. bfq_mark_bfqq_softrt_update(bfqq);
  2055. }
  2056. }
  2057. bfq_log_bfqq(bfqd, bfqq,
  2058. "expire (%d, slow %d, num_disp %d, idle_win %d)", reason,
  2059. slow, bfqq->dispatched, bfq_bfqq_idle_window(bfqq));
  2060. /*
  2061. * Increase, decrease or leave budget unchanged according to
  2062. * reason.
  2063. */
  2064. __bfq_bfqq_recalc_budget(bfqd, bfqq, reason);
  2065. __bfq_bfqq_expire(bfqd, bfqq);
  2066. }
  2067. /*
  2068. * Budget timeout is not implemented through a dedicated timer, but
  2069. * just checked on request arrivals and completions, as well as on
  2070. * idle timer expirations.
  2071. */
  2072. static int bfq_bfqq_budget_timeout(struct bfq_queue *bfqq)
  2073. {
  2074. if (bfq_bfqq_budget_new(bfqq) ||
  2075. time_before(jiffies, bfqq->budget_timeout))
  2076. return 0;
  2077. return 1;
  2078. }
  2079. /*
  2080. * If we expire a queue that is waiting for the arrival of a new
  2081. * request, we may prevent the fictitious timestamp back-shifting that
  2082. * allows the guarantees of the queue to be preserved (see [1] for
  2083. * this tricky aspect). Hence we return true only if this condition
  2084. * does not hold, or if the queue is slow enough to deserve only to be
  2085. * kicked off for preserving a high throughput.
  2086. */
  2087. static inline int bfq_may_expire_for_budg_timeout(struct bfq_queue *bfqq)
  2088. {
  2089. bfq_log_bfqq(bfqq->bfqd, bfqq,
  2090. "may_budget_timeout: wait_request %d left %d timeout %d",
  2091. bfq_bfqq_wait_request(bfqq),
  2092. bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3,
  2093. bfq_bfqq_budget_timeout(bfqq));
  2094. return (!bfq_bfqq_wait_request(bfqq) ||
  2095. bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3)
  2096. &&
  2097. bfq_bfqq_budget_timeout(bfqq);
  2098. }
  2099. /*
  2100. * Device idling is allowed only for the queues for which this function
  2101. * returns true. For this reason, the return value of this function plays a
  2102. * critical role for both throughput boosting and service guarantees. The
  2103. * return value is computed through a logical expression. In this rather
  2104. * long comment, we try to briefly describe all the details and motivations
  2105. * behind the components of this logical expression.
  2106. *
  2107. * First, the expression is false if bfqq is not sync, or if: bfqq happened
  2108. * to become active during a large burst of queue activations, and the
  2109. * pattern of requests bfqq contains boosts the throughput if bfqq is
  2110. * expired. In fact, queues that became active during a large burst benefit
  2111. * only from throughput, as discussed in the comments to bfq_handle_burst.
  2112. * In this respect, expiring bfqq certainly boosts the throughput on NCQ-
  2113. * capable flash-based devices, whereas, on rotational devices, it boosts
  2114. * the throughput only if bfqq contains random requests.
  2115. *
  2116. * On the opposite end, if (a) bfqq is sync, (b) the above burst-related
  2117. * condition does not hold, and (c) bfqq is being weight-raised, then the
  2118. * expression always evaluates to true, as device idling is instrumental
  2119. * for preserving low-latency guarantees (see [1]). If, instead, conditions
  2120. * (a) and (b) do hold, but (c) does not, then the expression evaluates to
  2121. * true only if: (1) bfqq is I/O-bound and has a non-null idle window, and
  2122. * (2) at least one of the following two conditions holds.
  2123. * The first condition is that the device is not performing NCQ, because
  2124. * idling the device most certainly boosts the throughput if this condition
  2125. * holds and bfqq is I/O-bound and has been granted a non-null idle window.
  2126. * The second compound condition is made of the logical AND of two components.
  2127. *
  2128. * The first component is true only if there is no weight-raised busy
  2129. * queue. This guarantees that the device is not idled for a sync non-
  2130. * weight-raised queue when there are busy weight-raised queues. The former
  2131. * is then expired immediately if empty. Combined with the timestamping
  2132. * rules of BFQ (see [1] for details), this causes sync non-weight-raised
  2133. * queues to get a lower number of requests served, and hence to ask for a
  2134. * lower number of requests from the request pool, before the busy weight-
  2135. * raised queues get served again.
  2136. *
  2137. * This is beneficial for the processes associated with weight-raised
  2138. * queues, when the request pool is saturated (e.g., in the presence of
  2139. * write hogs). In fact, if the processes associated with the other queues
  2140. * ask for requests at a lower rate, then weight-raised processes have a
  2141. * higher probability to get a request from the pool immediately (or at
  2142. * least soon) when they need one. Hence they have a higher probability to
  2143. * actually get a fraction of the disk throughput proportional to their
  2144. * high weight. This is especially true with NCQ-capable drives, which
  2145. * enqueue several requests in advance and further reorder internally-
  2146. * queued requests.
  2147. *
  2148. * In the end, mistreating non-weight-raised queues when there are busy
  2149. * weight-raised queues seems to mitigate starvation problems in the
  2150. * presence of heavy write workloads and NCQ, and hence to guarantee a
  2151. * higher application and system responsiveness in these hostile scenarios.
  2152. *
  2153. * If the first component of the compound condition is instead true, i.e.,
  2154. * there is no weight-raised busy queue, then the second component of the
  2155. * compound condition takes into account service-guarantee and throughput
  2156. * issues related to NCQ (recall that the compound condition is evaluated
  2157. * only if the device is detected as supporting NCQ).
  2158. *
  2159. * As for service guarantees, allowing the drive to enqueue more than one
  2160. * request at a time, and hence delegating de facto final scheduling
  2161. * decisions to the drive's internal scheduler, causes loss of control on
  2162. * the actual request service order. In this respect, when the drive is
  2163. * allowed to enqueue more than one request at a time, the service
  2164. * distribution enforced by the drive's internal scheduler is likely to
  2165. * coincide with the desired device-throughput distribution only in the
  2166. * following, perfectly symmetric, scenario:
  2167. * 1) all active queues have the same weight,
  2168. * 2) all active groups at the same level in the groups tree have the same
  2169. * weight,
  2170. * 3) all active groups at the same level in the groups tree have the same
  2171. * number of children.
  2172. *
  2173. * Even in such a scenario, sequential I/O may still receive a preferential
  2174. * treatment, but this is not likely to be a big issue with flash-based
  2175. * devices, because of their non-dramatic loss of throughput with random
  2176. * I/O. Things do differ with HDDs, for which additional care is taken, as
  2177. * explained after completing the discussion for flash-based devices.
  2178. *
  2179. * Unfortunately, keeping the necessary state for evaluating exactly the
  2180. * above symmetry conditions would be quite complex and time-consuming.
  2181. * Therefore BFQ evaluates instead the following stronger sub-conditions,
  2182. * for which it is much easier to maintain the needed state:
  2183. * 1) all active queues have the same weight,
  2184. * 2) all active groups have the same weight,
  2185. * 3) all active groups have at most one active child each.
  2186. * In particular, the last two conditions are always true if hierarchical
  2187. * support and the cgroups interface are not enabled, hence no state needs
  2188. * to be maintained in this case.
  2189. *
  2190. * According to the above considerations, the second component of the
  2191. * compound condition evaluates to true if any of the above symmetry
  2192. * sub-condition does not hold, or the device is not flash-based. Therefore,
  2193. * if also the first component is true, then idling is allowed for a sync
  2194. * queue. These are the only sub-conditions considered if the device is
  2195. * flash-based, as, for such a device, it is sensible to force idling only
  2196. * for service-guarantee issues. In fact, as for throughput, idling
  2197. * NCQ-capable flash-based devices would not boost the throughput even
  2198. * with sequential I/O; rather it would lower the throughput in proportion
  2199. * to how fast the device is. In the end, (only) if all the three
  2200. * sub-conditions hold and the device is flash-based, the compound
  2201. * condition evaluates to false and therefore no idling is performed.
  2202. *
  2203. * As already said, things change with a rotational device, where idling
  2204. * boosts the throughput with sequential I/O (even with NCQ). Hence, for
  2205. * such a device the second component of the compound condition evaluates
  2206. * to true also if the following additional sub-condition does not hold:
  2207. * the queue is constantly seeky. Unfortunately, this different behavior
  2208. * with respect to flash-based devices causes an additional asymmetry: if
  2209. * some sync queues enjoy idling and some other sync queues do not, then
  2210. * the latter get a low share of the device throughput, simply because the
  2211. * former get many requests served after being set as in service, whereas
  2212. * the latter do not. As a consequence, to guarantee the desired throughput
  2213. * distribution, on HDDs the compound expression evaluates to true (and
  2214. * hence device idling is performed) also if the following last symmetry
  2215. * condition does not hold: no other queue is benefiting from idling. Also
  2216. * this last condition is actually replaced with a simpler-to-maintain and
  2217. * stronger condition: there is no busy queue which is not constantly seeky
  2218. * (and hence may also benefit from idling).
  2219. *
  2220. * To sum up, when all the required symmetry and throughput-boosting
  2221. * sub-conditions hold, the second component of the compound condition
  2222. * evaluates to false, and hence no idling is performed. This helps to
  2223. * keep the drives' internal queues full on NCQ-capable devices, and hence
  2224. * to boost the throughput, without causing 'almost' any loss of service
  2225. * guarantees. The 'almost' follows from the fact that, if the internal
  2226. * queue of one such device is filled while all the sub-conditions hold,
  2227. * but at some point in time some sub-condition stops to hold, then it may
  2228. * become impossible to let requests be served in the new desired order
  2229. * until all the requests already queued in the device have been served.
  2230. */
  2231. static inline bool bfq_bfqq_must_not_expire(struct bfq_queue *bfqq)
  2232. {
  2233. struct bfq_data *bfqd = bfqq->bfqd;
  2234. #define cond_for_seeky_on_ncq_hdd (bfq_bfqq_constantly_seeky(bfqq) && \
  2235. bfqd->busy_in_flight_queues == \
  2236. bfqd->const_seeky_busy_in_flight_queues)
  2237. #define cond_for_expiring_in_burst (bfq_bfqq_in_large_burst(bfqq) && \
  2238. bfqd->hw_tag && \
  2239. (blk_queue_nonrot(bfqd->queue) || \
  2240. bfq_bfqq_constantly_seeky(bfqq)))
  2241. /*
  2242. * Condition for expiring a non-weight-raised queue (and hence not idling
  2243. * the device).
  2244. */
  2245. #define cond_for_expiring_non_wr (bfqd->hw_tag && \
  2246. (bfqd->wr_busy_queues > 0 || \
  2247. (blk_queue_nonrot(bfqd->queue) || \
  2248. cond_for_seeky_on_ncq_hdd)))
  2249. return bfq_bfqq_sync(bfqq) &&
  2250. !cond_for_expiring_in_burst &&
  2251. (bfqq->wr_coeff > 1 || !symmetric_scenario ||
  2252. (bfq_bfqq_IO_bound(bfqq) && bfq_bfqq_idle_window(bfqq) &&
  2253. !cond_for_expiring_non_wr)
  2254. );
  2255. }
  2256. /*
  2257. * If the in-service queue is empty but sync, and the function
  2258. * bfq_bfqq_must_not_expire returns true, then:
  2259. * 1) the queue must remain in service and cannot be expired, and
  2260. * 2) the disk must be idled to wait for the possible arrival of a new
  2261. * request for the queue.
  2262. * See the comments to the function bfq_bfqq_must_not_expire for the reasons
  2263. * why performing device idling is the best choice to boost the throughput
  2264. * and preserve service guarantees when bfq_bfqq_must_not_expire itself
  2265. * returns true.
  2266. */
  2267. static inline bool bfq_bfqq_must_idle(struct bfq_queue *bfqq)
  2268. {
  2269. struct bfq_data *bfqd = bfqq->bfqd;
  2270. return RB_EMPTY_ROOT(&bfqq->sort_list) && bfqd->bfq_slice_idle != 0 &&
  2271. bfq_bfqq_must_not_expire(bfqq);
  2272. }
  2273. /*
  2274. * Select a queue for service. If we have a current queue in service,
  2275. * check whether to continue servicing it, or retrieve and set a new one.
  2276. */
  2277. static struct bfq_queue *bfq_select_queue(struct bfq_data *bfqd)
  2278. {
  2279. struct bfq_queue *bfqq;
  2280. struct request *next_rq;
  2281. enum bfqq_expiration reason = BFQ_BFQQ_BUDGET_TIMEOUT;
  2282. bfqq = bfqd->in_service_queue;
  2283. if (bfqq == NULL)
  2284. goto new_queue;
  2285. bfq_log_bfqq(bfqd, bfqq, "select_queue: already in-service queue");
  2286. if (bfq_may_expire_for_budg_timeout(bfqq) &&
  2287. !timer_pending(&bfqd->idle_slice_timer) &&
  2288. !bfq_bfqq_must_idle(bfqq))
  2289. goto expire;
  2290. next_rq = bfqq->next_rq;
  2291. /*
  2292. * If bfqq has requests queued and it has enough budget left to
  2293. * serve them, keep the queue, otherwise expire it.
  2294. */
  2295. if (next_rq != NULL) {
  2296. if (bfq_serv_to_charge(next_rq, bfqq) >
  2297. bfq_bfqq_budget_left(bfqq)) {
  2298. reason = BFQ_BFQQ_BUDGET_EXHAUSTED;
  2299. goto expire;
  2300. } else {
  2301. /*
  2302. * The idle timer may be pending because we may
  2303. * not disable disk idling even when a new request
  2304. * arrives.
  2305. */
  2306. if (timer_pending(&bfqd->idle_slice_timer)) {
  2307. /*
  2308. * If we get here: 1) at least a new request
  2309. * has arrived but we have not disabled the
  2310. * timer because the request was too small,
  2311. * 2) then the block layer has unplugged
  2312. * the device, causing the dispatch to be
  2313. * invoked.
  2314. *
  2315. * Since the device is unplugged, now the
  2316. * requests are probably large enough to
  2317. * provide a reasonable throughput.
  2318. * So we disable idling.
  2319. */
  2320. bfq_clear_bfqq_wait_request(bfqq);
  2321. del_timer(&bfqd->idle_slice_timer);
  2322. }
  2323. goto keep_queue;
  2324. }
  2325. }
  2326. /*
  2327. * No requests pending. However, if the in-service queue is idling
  2328. * for a new request, or has requests waiting for a completion and
  2329. * may idle after their completion, then keep it anyway.
  2330. */
  2331. if (timer_pending(&bfqd->idle_slice_timer) ||
  2332. (bfqq->dispatched != 0 && bfq_bfqq_must_not_expire(bfqq))) {
  2333. bfqq = NULL;
  2334. goto keep_queue;
  2335. }
  2336. reason = BFQ_BFQQ_NO_MORE_REQUESTS;
  2337. expire:
  2338. bfq_bfqq_expire(bfqd, bfqq, 0, reason);
  2339. new_queue:
  2340. bfqq = bfq_set_in_service_queue(bfqd);
  2341. bfq_log(bfqd, "select_queue: new queue %d returned",
  2342. bfqq != NULL ? bfqq->pid : 0);
  2343. keep_queue:
  2344. return bfqq;
  2345. }
  2346. static void bfq_update_wr_data(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  2347. {
  2348. struct bfq_entity *entity = &bfqq->entity;
  2349. if (bfqq->wr_coeff > 1) { /* queue is being weight-raised */
  2350. bfq_log_bfqq(bfqd, bfqq,
  2351. "raising period dur %u/%u msec, old coeff %u, w %d(%d)",
  2352. jiffies_to_msecs(jiffies - bfqq->last_wr_start_finish),
  2353. jiffies_to_msecs(bfqq->wr_cur_max_time),
  2354. bfqq->wr_coeff,
  2355. bfqq->entity.weight, bfqq->entity.orig_weight);
  2356. BUG_ON(bfqq != bfqd->in_service_queue && entity->weight !=
  2357. entity->orig_weight * bfqq->wr_coeff);
  2358. if (entity->ioprio_changed)
  2359. bfq_log_bfqq(bfqd, bfqq, "WARN: pending prio change");
  2360. /*
  2361. * If the queue was activated in a burst, or
  2362. * too much time has elapsed from the beginning
  2363. * of this weight-raising period, or the queue has
  2364. * exceeded the acceptable number of cooperations,
  2365. * then end weight raising.
  2366. */
  2367. if (bfq_bfqq_in_large_burst(bfqq) ||
  2368. bfq_bfqq_cooperations(bfqq) >= bfqd->bfq_coop_thresh ||
  2369. time_is_before_jiffies(bfqq->last_wr_start_finish +
  2370. bfqq->wr_cur_max_time)) {
  2371. bfqq->last_wr_start_finish = jiffies;
  2372. bfq_log_bfqq(bfqd, bfqq,
  2373. "wrais ending at %lu, rais_max_time %u",
  2374. bfqq->last_wr_start_finish,
  2375. jiffies_to_msecs(bfqq->wr_cur_max_time));
  2376. bfq_bfqq_end_wr(bfqq);
  2377. }
  2378. }
  2379. /* Update weight both if it must be raised and if it must be lowered */
  2380. if ((entity->weight > entity->orig_weight) != (bfqq->wr_coeff > 1))
  2381. __bfq_entity_update_weight_prio(
  2382. bfq_entity_service_tree(entity),
  2383. entity);
  2384. }
  2385. /*
  2386. * Dispatch one request from bfqq, moving it to the request queue
  2387. * dispatch list.
  2388. */
  2389. static int bfq_dispatch_request(struct bfq_data *bfqd,
  2390. struct bfq_queue *bfqq)
  2391. {
  2392. int dispatched = 0;
  2393. struct request *rq;
  2394. unsigned long service_to_charge;
  2395. BUG_ON(RB_EMPTY_ROOT(&bfqq->sort_list));
  2396. /* Follow expired path, else get first next available. */
  2397. rq = bfq_check_fifo(bfqq);
  2398. if (rq == NULL)
  2399. rq = bfqq->next_rq;
  2400. service_to_charge = bfq_serv_to_charge(rq, bfqq);
  2401. if (service_to_charge > bfq_bfqq_budget_left(bfqq)) {
  2402. /*
  2403. * This may happen if the next rq is chosen in fifo order
  2404. * instead of sector order. The budget is properly
  2405. * dimensioned to be always sufficient to serve the next
  2406. * request only if it is chosen in sector order. The reason
  2407. * is that it would be quite inefficient and little useful
  2408. * to always make sure that the budget is large enough to
  2409. * serve even the possible next rq in fifo order.
  2410. * In fact, requests are seldom served in fifo order.
  2411. *
  2412. * Expire the queue for budget exhaustion, and make sure
  2413. * that the next act_budget is enough to serve the next
  2414. * request, even if it comes from the fifo expired path.
  2415. */
  2416. bfqq->next_rq = rq;
  2417. /*
  2418. * Since this dispatch is failed, make sure that
  2419. * a new one will be performed
  2420. */
  2421. if (!bfqd->rq_in_driver)
  2422. bfq_schedule_dispatch(bfqd);
  2423. goto expire;
  2424. }
  2425. /* Finally, insert request into driver dispatch list. */
  2426. bfq_bfqq_served(bfqq, service_to_charge);
  2427. bfq_dispatch_insert(bfqd->queue, rq);
  2428. bfq_update_wr_data(bfqd, bfqq);
  2429. bfq_log_bfqq(bfqd, bfqq,
  2430. "dispatched %u sec req (%llu), budg left %lu",
  2431. blk_rq_sectors(rq),
  2432. (long long unsigned)blk_rq_pos(rq),
  2433. bfq_bfqq_budget_left(bfqq));
  2434. dispatched++;
  2435. if (bfqd->in_service_bic == NULL) {
  2436. atomic_long_inc(&RQ_BIC(rq)->icq.ioc->refcount);
  2437. bfqd->in_service_bic = RQ_BIC(rq);
  2438. }
  2439. if (bfqd->busy_queues > 1 && ((!bfq_bfqq_sync(bfqq) &&
  2440. dispatched >= bfqd->bfq_max_budget_async_rq) ||
  2441. bfq_class_idle(bfqq)))
  2442. goto expire;
  2443. return dispatched;
  2444. expire:
  2445. bfq_bfqq_expire(bfqd, bfqq, 0, BFQ_BFQQ_BUDGET_EXHAUSTED);
  2446. return dispatched;
  2447. }
  2448. static int __bfq_forced_dispatch_bfqq(struct bfq_queue *bfqq)
  2449. {
  2450. int dispatched = 0;
  2451. while (bfqq->next_rq != NULL) {
  2452. bfq_dispatch_insert(bfqq->bfqd->queue, bfqq->next_rq);
  2453. dispatched++;
  2454. }
  2455. BUG_ON(!list_empty(&bfqq->fifo));
  2456. return dispatched;
  2457. }
  2458. /*
  2459. * Drain our current requests.
  2460. * Used for barriers and when switching io schedulers on-the-fly.
  2461. */
  2462. static int bfq_forced_dispatch(struct bfq_data *bfqd)
  2463. {
  2464. struct bfq_queue *bfqq, *n;
  2465. struct bfq_service_tree *st;
  2466. int dispatched = 0;
  2467. bfqq = bfqd->in_service_queue;
  2468. if (bfqq != NULL)
  2469. __bfq_bfqq_expire(bfqd, bfqq);
  2470. /*
  2471. * Loop through classes, and be careful to leave the scheduler
  2472. * in a consistent state, as feedback mechanisms and vtime
  2473. * updates cannot be disabled during the process.
  2474. */
  2475. list_for_each_entry_safe(bfqq, n, &bfqd->active_list, bfqq_list) {
  2476. st = bfq_entity_service_tree(&bfqq->entity);
  2477. dispatched += __bfq_forced_dispatch_bfqq(bfqq);
  2478. bfqq->max_budget = bfq_max_budget(bfqd);
  2479. bfq_forget_idle(st);
  2480. }
  2481. BUG_ON(bfqd->busy_queues != 0);
  2482. return dispatched;
  2483. }
  2484. static int bfq_dispatch_requests(struct request_queue *q, int force)
  2485. {
  2486. struct bfq_data *bfqd = q->elevator->elevator_data;
  2487. struct bfq_queue *bfqq;
  2488. int max_dispatch;
  2489. bfq_log(bfqd, "dispatch requests: %d busy queues", bfqd->busy_queues);
  2490. if (bfqd->busy_queues == 0)
  2491. return 0;
  2492. if (unlikely(force))
  2493. return bfq_forced_dispatch(bfqd);
  2494. bfqq = bfq_select_queue(bfqd);
  2495. if (bfqq == NULL)
  2496. return 0;
  2497. if (bfq_class_idle(bfqq))
  2498. max_dispatch = 1;
  2499. if (!bfq_bfqq_sync(bfqq))
  2500. max_dispatch = bfqd->bfq_max_budget_async_rq;
  2501. if (!bfq_bfqq_sync(bfqq) && bfqq->dispatched >= max_dispatch) {
  2502. if (bfqd->busy_queues > 1)
  2503. return 0;
  2504. if (bfqq->dispatched >= 4 * max_dispatch)
  2505. return 0;
  2506. }
  2507. if (bfqd->sync_flight != 0 && !bfq_bfqq_sync(bfqq))
  2508. return 0;
  2509. bfq_clear_bfqq_wait_request(bfqq);
  2510. BUG_ON(timer_pending(&bfqd->idle_slice_timer));
  2511. if (!bfq_dispatch_request(bfqd, bfqq))
  2512. return 0;
  2513. bfq_log_bfqq(bfqd, bfqq, "dispatched %s request",
  2514. bfq_bfqq_sync(bfqq) ? "sync" : "async");
  2515. return 1;
  2516. }
  2517. /*
  2518. * Task holds one reference to the queue, dropped when task exits. Each rq
  2519. * in-flight on this queue also holds a reference, dropped when rq is freed.
  2520. *
  2521. * Queue lock must be held here.
  2522. */
  2523. static void bfq_put_queue(struct bfq_queue *bfqq)
  2524. {
  2525. struct bfq_data *bfqd = bfqq->bfqd;
  2526. BUG_ON(atomic_read(&bfqq->ref) <= 0);
  2527. bfq_log_bfqq(bfqd, bfqq, "put_queue: %p %d", bfqq,
  2528. atomic_read(&bfqq->ref));
  2529. if (!atomic_dec_and_test(&bfqq->ref))
  2530. return;
  2531. BUG_ON(rb_first(&bfqq->sort_list) != NULL);
  2532. BUG_ON(bfqq->allocated[READ] + bfqq->allocated[WRITE] != 0);
  2533. BUG_ON(bfqq->entity.tree != NULL);
  2534. BUG_ON(bfq_bfqq_busy(bfqq));
  2535. BUG_ON(bfqd->in_service_queue == bfqq);
  2536. if (bfq_bfqq_sync(bfqq))
  2537. /*
  2538. * The fact that this queue is being destroyed does not
  2539. * invalidate the fact that this queue may have been
  2540. * activated during the current burst. As a consequence,
  2541. * although the queue does not exist anymore, and hence
  2542. * needs to be removed from the burst list if there,
  2543. * the burst size has not to be decremented.
  2544. */
  2545. hlist_del_init(&bfqq->burst_list_node);
  2546. bfq_log_bfqq(bfqd, bfqq, "put_queue: %p freed", bfqq);
  2547. kmem_cache_free(bfq_pool, bfqq);
  2548. }
  2549. static void bfq_put_cooperator(struct bfq_queue *bfqq)
  2550. {
  2551. struct bfq_queue *__bfqq, *next;
  2552. /*
  2553. * If this queue was scheduled to merge with another queue, be
  2554. * sure to drop the reference taken on that queue (and others in
  2555. * the merge chain). See bfq_setup_merge and bfq_merge_bfqqs.
  2556. */
  2557. __bfqq = bfqq->new_bfqq;
  2558. while (__bfqq) {
  2559. if (__bfqq == bfqq)
  2560. break;
  2561. next = __bfqq->new_bfqq;
  2562. bfq_put_queue(__bfqq);
  2563. __bfqq = next;
  2564. }
  2565. }
  2566. static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  2567. {
  2568. if (bfqq == bfqd->in_service_queue) {
  2569. __bfq_bfqq_expire(bfqd, bfqq);
  2570. bfq_schedule_dispatch(bfqd);
  2571. }
  2572. bfq_log_bfqq(bfqd, bfqq, "exit_bfqq: %p, %d", bfqq,
  2573. atomic_read(&bfqq->ref));
  2574. bfq_put_cooperator(bfqq);
  2575. bfq_put_queue(bfqq);
  2576. }
  2577. static inline void bfq_init_icq(struct io_cq *icq)
  2578. {
  2579. struct bfq_io_cq *bic = icq_to_bic(icq);
  2580. bic->ttime.last_end_request = jiffies;
  2581. /*
  2582. * A newly created bic indicates that the process has just
  2583. * started doing I/O, and is probably mapping into memory its
  2584. * executable and libraries: it definitely needs weight raising.
  2585. * There is however the possibility that the process performs,
  2586. * for a while, I/O close to some other process. EQM intercepts
  2587. * this behavior and may merge the queue corresponding to the
  2588. * process with some other queue, BEFORE the weight of the queue
  2589. * is raised. Merged queues are not weight-raised (they are assumed
  2590. * to belong to processes that benefit only from high throughput).
  2591. * If the merge is basically the consequence of an accident, then
  2592. * the queue will be split soon and will get back its old weight.
  2593. * It is then important to write down somewhere that this queue
  2594. * does need weight raising, even if it did not make it to get its
  2595. * weight raised before being merged. To this purpose, we overload
  2596. * the field raising_time_left and assign 1 to it, to mark the queue
  2597. * as needing weight raising.
  2598. */
  2599. bic->wr_time_left = 1;
  2600. }
  2601. static void bfq_exit_icq(struct io_cq *icq)
  2602. {
  2603. struct bfq_io_cq *bic = icq_to_bic(icq);
  2604. struct bfq_data *bfqd = bic_to_bfqd(bic);
  2605. if (bic->bfqq[BLK_RW_ASYNC]) {
  2606. bfq_exit_bfqq(bfqd, bic->bfqq[BLK_RW_ASYNC]);
  2607. bic->bfqq[BLK_RW_ASYNC] = NULL;
  2608. }
  2609. if (bic->bfqq[BLK_RW_SYNC]) {
  2610. /*
  2611. * If the bic is using a shared queue, put the reference
  2612. * taken on the io_context when the bic started using a
  2613. * shared bfq_queue.
  2614. */
  2615. if (bfq_bfqq_coop(bic->bfqq[BLK_RW_SYNC]))
  2616. put_io_context(icq->ioc);
  2617. bfq_exit_bfqq(bfqd, bic->bfqq[BLK_RW_SYNC]);
  2618. bic->bfqq[BLK_RW_SYNC] = NULL;
  2619. }
  2620. }
  2621. /*
  2622. * Update the entity prio values; note that the new values will not
  2623. * be used until the next (re)activation.
  2624. */
  2625. static void bfq_set_next_ioprio_data(struct bfq_queue *bfqq, struct bfq_io_cq *bic)
  2626. {
  2627. struct task_struct *tsk = current;
  2628. struct io_context *ioc = bic->icq.ioc;
  2629. int ioprio_class;
  2630. ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
  2631. switch (ioprio_class) {
  2632. default:
  2633. dev_err(bfqq->bfqd->queue->backing_dev_info.dev,
  2634. "bfq: bad prio class %d\n", ioprio_class);
  2635. case IOPRIO_CLASS_NONE:
  2636. /*
  2637. * No prio set, inherit CPU scheduling settings.
  2638. */
  2639. bfqq->entity.new_ioprio = task_nice_ioprio(tsk);
  2640. bfqq->entity.new_ioprio_class = task_nice_ioclass(tsk);
  2641. break;
  2642. case IOPRIO_CLASS_RT:
  2643. bfqq->entity.new_ioprio = task_ioprio(ioc);
  2644. bfqq->entity.new_ioprio_class = IOPRIO_CLASS_RT;
  2645. break;
  2646. case IOPRIO_CLASS_BE:
  2647. bfqq->entity.new_ioprio = task_ioprio(ioc);
  2648. bfqq->entity.new_ioprio_class = IOPRIO_CLASS_BE;
  2649. break;
  2650. case IOPRIO_CLASS_IDLE:
  2651. bfqq->entity.new_ioprio_class = IOPRIO_CLASS_IDLE;
  2652. bfqq->entity.new_ioprio = 7;
  2653. bfq_clear_bfqq_idle_window(bfqq);
  2654. break;
  2655. }
  2656. if (bfqq->entity.new_ioprio < 0 ||
  2657. bfqq->entity.new_ioprio >= IOPRIO_BE_NR) {
  2658. printk(KERN_CRIT "bfq_set_next_ioprio_data: new_ioprio %d\n",
  2659. bfqq->entity.new_ioprio);
  2660. BUG();
  2661. }
  2662. bfqq->entity.new_weight = bfq_ioprio_to_weight(bfqq->entity.new_ioprio);
  2663. bfqq->entity.ioprio_changed = 1;
  2664. }
  2665. static void bfq_check_ioprio_change(struct io_context *ioc,
  2666. struct bfq_io_cq *bic)
  2667. {
  2668. struct bfq_data *bfqd;
  2669. struct bfq_queue *bfqq, *new_bfqq;
  2670. struct bfq_group *bfqg;
  2671. unsigned long uninitialized_var(flags);
  2672. int ioprio = bic->icq.ioc->ioprio;
  2673. bfqd = bfq_get_bfqd_locked(&(bic->icq.q->elevator->elevator_data),
  2674. &flags);
  2675. if (unlikely(bfqd == NULL))
  2676. return;
  2677. bic->ioprio = ioprio;
  2678. bfqq = bic->bfqq[BLK_RW_ASYNC];
  2679. if (bfqq != NULL) {
  2680. bfqg = container_of(bfqq->entity.sched_data, struct bfq_group,
  2681. sched_data);
  2682. new_bfqq = bfq_get_queue(bfqd, bfqg, BLK_RW_ASYNC, bic->icq.ioc,
  2683. GFP_ATOMIC);
  2684. if (new_bfqq != NULL) {
  2685. bic->bfqq[BLK_RW_ASYNC] = new_bfqq;
  2686. bfq_log_bfqq(bfqd, bfqq,
  2687. "check_ioprio_change: bfqq %p %d",
  2688. bfqq, atomic_read(&bfqq->ref));
  2689. bfq_put_queue(bfqq);
  2690. }
  2691. }
  2692. bfqq = bic->bfqq[BLK_RW_SYNC];
  2693. if (bfqq != NULL)
  2694. bfq_set_next_ioprio_data(bfqq, bic);
  2695. bfq_put_bfqd_unlock(bfqd, &flags);
  2696. }
  2697. static void bfq_init_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  2698. struct bfq_io_cq *bic, pid_t pid, int is_sync)
  2699. {
  2700. RB_CLEAR_NODE(&bfqq->entity.rb_node);
  2701. INIT_LIST_HEAD(&bfqq->fifo);
  2702. INIT_HLIST_NODE(&bfqq->burst_list_node);
  2703. atomic_set(&bfqq->ref, 0);
  2704. bfqq->bfqd = bfqd;
  2705. if (bic)
  2706. bfq_set_next_ioprio_data(bfqq, bic);
  2707. if (is_sync) {
  2708. if (!bfq_class_idle(bfqq))
  2709. bfq_mark_bfqq_idle_window(bfqq);
  2710. bfq_mark_bfqq_sync(bfqq);
  2711. }
  2712. bfq_mark_bfqq_IO_bound(bfqq);
  2713. /* Tentative initial value to trade off between thr and lat */
  2714. bfqq->max_budget = (2 * bfq_max_budget(bfqd)) / 3;
  2715. bfqq->pid = pid;
  2716. bfqq->wr_coeff = 1;
  2717. bfqq->last_wr_start_finish = 0;
  2718. /*
  2719. * Set to the value for which bfqq will not be deemed as
  2720. * soft rt when it becomes backlogged.
  2721. */
  2722. bfqq->soft_rt_next_start = bfq_infinity_from_now(jiffies);
  2723. }
  2724. static struct bfq_queue *bfq_find_alloc_queue(struct bfq_data *bfqd,
  2725. struct bfq_group *bfqg,
  2726. int is_sync,
  2727. struct io_context *ioc,
  2728. gfp_t gfp_mask)
  2729. {
  2730. struct bfq_queue *bfqq, *new_bfqq = NULL;
  2731. struct bfq_io_cq *bic;
  2732. retry:
  2733. bic = bfq_bic_lookup(bfqd, ioc);
  2734. /* bic always exists here */
  2735. bfqq = bic_to_bfqq(bic, is_sync);
  2736. /*
  2737. * Always try a new alloc if we fall back to the OOM bfqq
  2738. * originally, since it should just be a temporary situation.
  2739. */
  2740. if (bfqq == NULL || bfqq == &bfqd->oom_bfqq) {
  2741. bfqq = NULL;
  2742. if (new_bfqq != NULL) {
  2743. bfqq = new_bfqq;
  2744. new_bfqq = NULL;
  2745. } else if (gfp_mask & __GFP_WAIT) {
  2746. spin_unlock_irq(bfqd->queue->queue_lock);
  2747. new_bfqq = kmem_cache_alloc_node(bfq_pool,
  2748. gfp_mask | __GFP_ZERO,
  2749. bfqd->queue->node);
  2750. spin_lock_irq(bfqd->queue->queue_lock);
  2751. if (new_bfqq != NULL)
  2752. goto retry;
  2753. } else {
  2754. bfqq = kmem_cache_alloc_node(bfq_pool,
  2755. gfp_mask | __GFP_ZERO,
  2756. bfqd->queue->node);
  2757. }
  2758. if (bfqq != NULL) {
  2759. bfq_init_bfqq(bfqd, bfqq, bic, current->pid,
  2760. is_sync);
  2761. bfq_init_entity(&bfqq->entity, bfqg);
  2762. bfq_log_bfqq(bfqd, bfqq, "allocated");
  2763. } else {
  2764. bfqq = &bfqd->oom_bfqq;
  2765. bfq_log_bfqq(bfqd, bfqq, "using oom bfqq");
  2766. }
  2767. }
  2768. if (new_bfqq != NULL)
  2769. kmem_cache_free(bfq_pool, new_bfqq);
  2770. return bfqq;
  2771. }
  2772. static struct bfq_queue **bfq_async_queue_prio(struct bfq_data *bfqd,
  2773. struct bfq_group *bfqg,
  2774. int ioprio_class, int ioprio)
  2775. {
  2776. switch (ioprio_class) {
  2777. case IOPRIO_CLASS_RT:
  2778. return &bfqg->async_bfqq[0][ioprio];
  2779. case IOPRIO_CLASS_BE:
  2780. return &bfqg->async_bfqq[1][ioprio];
  2781. case IOPRIO_CLASS_IDLE:
  2782. return &bfqg->async_idle_bfqq;
  2783. default:
  2784. BUG();
  2785. }
  2786. }
  2787. static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
  2788. struct bfq_group *bfqg, int is_sync,
  2789. struct io_context *ioc, gfp_t gfp_mask)
  2790. {
  2791. const int ioprio = task_ioprio(ioc);
  2792. const int ioprio_class = task_ioprio_class(ioc);
  2793. struct bfq_queue **async_bfqq = NULL;
  2794. struct bfq_queue *bfqq = NULL;
  2795. if (!is_sync) {
  2796. async_bfqq = bfq_async_queue_prio(bfqd, bfqg, ioprio_class,
  2797. ioprio);
  2798. bfqq = *async_bfqq;
  2799. }
  2800. if (bfqq == NULL)
  2801. bfqq = bfq_find_alloc_queue(bfqd, bfqg, is_sync, ioc, gfp_mask);
  2802. /*
  2803. * Pin the queue now that it's allocated, scheduler exit will
  2804. * prune it.
  2805. */
  2806. if (!is_sync && *async_bfqq == NULL) {
  2807. atomic_inc(&bfqq->ref);
  2808. bfq_log_bfqq(bfqd, bfqq, "get_queue, bfqq not in async: %p, %d",
  2809. bfqq, atomic_read(&bfqq->ref));
  2810. *async_bfqq = bfqq;
  2811. }
  2812. atomic_inc(&bfqq->ref);
  2813. bfq_log_bfqq(bfqd, bfqq, "get_queue, at end: %p, %d", bfqq,
  2814. atomic_read(&bfqq->ref));
  2815. return bfqq;
  2816. }
  2817. static void bfq_update_io_thinktime(struct bfq_data *bfqd,
  2818. struct bfq_io_cq *bic)
  2819. {
  2820. unsigned long elapsed = jiffies - bic->ttime.last_end_request;
  2821. unsigned long ttime = min(elapsed, 2UL * bfqd->bfq_slice_idle);
  2822. bic->ttime.ttime_samples = (7*bic->ttime.ttime_samples + 256) / 8;
  2823. bic->ttime.ttime_total = (7*bic->ttime.ttime_total + 256*ttime) / 8;
  2824. bic->ttime.ttime_mean = (bic->ttime.ttime_total + 128) /
  2825. bic->ttime.ttime_samples;
  2826. }
  2827. static void bfq_update_io_seektime(struct bfq_data *bfqd,
  2828. struct bfq_queue *bfqq,
  2829. struct request *rq)
  2830. {
  2831. sector_t sdist;
  2832. u64 total;
  2833. if (bfqq->last_request_pos < blk_rq_pos(rq))
  2834. sdist = blk_rq_pos(rq) - bfqq->last_request_pos;
  2835. else
  2836. sdist = bfqq->last_request_pos - blk_rq_pos(rq);
  2837. /*
  2838. * Don't allow the seek distance to get too large from the
  2839. * odd fragment, pagein, etc.
  2840. */
  2841. if (bfqq->seek_samples == 0) /* first request, not really a seek */
  2842. sdist = 0;
  2843. else if (bfqq->seek_samples <= 60) /* second & third seek */
  2844. sdist = min(sdist, (bfqq->seek_mean * 4) + 2*1024*1024);
  2845. else
  2846. sdist = min(sdist, (bfqq->seek_mean * 4) + 2*1024*64);
  2847. bfqq->seek_samples = (7*bfqq->seek_samples + 256) / 8;
  2848. bfqq->seek_total = (7*bfqq->seek_total + (u64)256*sdist) / 8;
  2849. total = bfqq->seek_total + (bfqq->seek_samples/2);
  2850. do_div(total, bfqq->seek_samples);
  2851. bfqq->seek_mean = (sector_t)total;
  2852. bfq_log_bfqq(bfqd, bfqq, "dist=%llu mean=%llu", (u64)sdist,
  2853. (u64)bfqq->seek_mean);
  2854. }
  2855. /*
  2856. * Disable idle window if the process thinks too long or seeks so much that
  2857. * it doesn't matter.
  2858. */
  2859. static void bfq_update_idle_window(struct bfq_data *bfqd,
  2860. struct bfq_queue *bfqq,
  2861. struct bfq_io_cq *bic)
  2862. {
  2863. int enable_idle;
  2864. /* Don't idle for async or idle io prio class. */
  2865. if (!bfq_bfqq_sync(bfqq) || bfq_class_idle(bfqq))
  2866. return;
  2867. /* Idle window just restored, statistics are meaningless. */
  2868. if (bfq_bfqq_just_split(bfqq))
  2869. return;
  2870. enable_idle = bfq_bfqq_idle_window(bfqq);
  2871. if (atomic_read(&bic->icq.ioc->nr_tasks) == 0 ||
  2872. bfqd->bfq_slice_idle == 0 ||
  2873. (bfqd->hw_tag && BFQQ_SEEKY(bfqq) &&
  2874. bfqq->wr_coeff == 1))
  2875. enable_idle = 0;
  2876. else if (bfq_sample_valid(bic->ttime.ttime_samples)) {
  2877. if (bic->ttime.ttime_mean > bfqd->bfq_slice_idle &&
  2878. bfqq->wr_coeff == 1)
  2879. enable_idle = 0;
  2880. else
  2881. enable_idle = 1;
  2882. }
  2883. bfq_log_bfqq(bfqd, bfqq, "update_idle_window: enable_idle %d",
  2884. enable_idle);
  2885. if (enable_idle)
  2886. bfq_mark_bfqq_idle_window(bfqq);
  2887. else
  2888. bfq_clear_bfqq_idle_window(bfqq);
  2889. }
  2890. /*
  2891. * Called when a new fs request (rq) is added to bfqq. Check if there's
  2892. * something we should do about it.
  2893. */
  2894. static void bfq_rq_enqueued(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  2895. struct request *rq)
  2896. {
  2897. struct bfq_io_cq *bic = RQ_BIC(rq);
  2898. if (rq->cmd_flags & REQ_META)
  2899. bfqq->meta_pending++;
  2900. bfq_update_io_thinktime(bfqd, bic);
  2901. bfq_update_io_seektime(bfqd, bfqq, rq);
  2902. if (!BFQQ_SEEKY(bfqq) && bfq_bfqq_constantly_seeky(bfqq)) {
  2903. bfq_clear_bfqq_constantly_seeky(bfqq);
  2904. if (!blk_queue_nonrot(bfqd->queue)) {
  2905. BUG_ON(!bfqd->const_seeky_busy_in_flight_queues);
  2906. bfqd->const_seeky_busy_in_flight_queues--;
  2907. }
  2908. }
  2909. if (bfqq->entity.service > bfq_max_budget(bfqd) / 8 ||
  2910. !BFQQ_SEEKY(bfqq))
  2911. bfq_update_idle_window(bfqd, bfqq, bic);
  2912. bfq_clear_bfqq_just_split(bfqq);
  2913. bfq_log_bfqq(bfqd, bfqq,
  2914. "rq_enqueued: idle_window=%d (seeky %d, mean %llu)",
  2915. bfq_bfqq_idle_window(bfqq), BFQQ_SEEKY(bfqq),
  2916. (long long unsigned)bfqq->seek_mean);
  2917. bfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
  2918. if (bfqq == bfqd->in_service_queue && bfq_bfqq_wait_request(bfqq)) {
  2919. int small_req = bfqq->queued[rq_is_sync(rq)] == 1 &&
  2920. blk_rq_sectors(rq) < 32;
  2921. int budget_timeout = bfq_bfqq_budget_timeout(bfqq);
  2922. /*
  2923. * There is just this request queued: if the request
  2924. * is small and the queue is not to be expired, then
  2925. * just exit.
  2926. *
  2927. * In this way, if the disk is being idled to wait for
  2928. * a new request from the in-service queue, we avoid
  2929. * unplugging the device and committing the disk to serve
  2930. * just a small request. On the contrary, we wait for
  2931. * the block layer to decide when to unplug the device:
  2932. * hopefully, new requests will be merged to this one
  2933. * quickly, then the device will be unplugged and
  2934. * larger requests will be dispatched.
  2935. */
  2936. if (small_req && !budget_timeout)
  2937. return;
  2938. /*
  2939. * A large enough request arrived, or the queue is to
  2940. * be expired: in both cases disk idling is to be
  2941. * stopped, so clear wait_request flag and reset
  2942. * timer.
  2943. */
  2944. bfq_clear_bfqq_wait_request(bfqq);
  2945. del_timer(&bfqd->idle_slice_timer);
  2946. /*
  2947. * The queue is not empty, because a new request just
  2948. * arrived. Hence we can safely expire the queue, in
  2949. * case of budget timeout, without risking that the
  2950. * timestamps of the queue are not updated correctly.
  2951. * See [1] for more details.
  2952. */
  2953. if (budget_timeout)
  2954. bfq_bfqq_expire(bfqd, bfqq, 0, BFQ_BFQQ_BUDGET_TIMEOUT);
  2955. /*
  2956. * Let the request rip immediately, or let a new queue be
  2957. * selected if bfqq has just been expired.
  2958. */
  2959. __blk_run_queue(bfqd->queue);
  2960. }
  2961. }
  2962. static void bfq_insert_request(struct request_queue *q, struct request *rq)
  2963. {
  2964. struct bfq_data *bfqd = q->elevator->elevator_data;
  2965. struct bfq_queue *bfqq = RQ_BFQQ(rq), *new_bfqq;
  2966. assert_spin_locked(bfqd->queue->queue_lock);
  2967. /*
  2968. * An unplug may trigger a requeue of a request from the device
  2969. * driver: make sure we are in process context while trying to
  2970. * merge two bfq_queues.
  2971. */
  2972. if (!in_interrupt()) {
  2973. new_bfqq = bfq_setup_cooperator(bfqd, bfqq, rq, true);
  2974. if (new_bfqq != NULL) {
  2975. if (bic_to_bfqq(RQ_BIC(rq), 1) != bfqq)
  2976. new_bfqq = bic_to_bfqq(RQ_BIC(rq), 1);
  2977. /*
  2978. * Release the request's reference to the old bfqq
  2979. * and make sure one is taken to the shared queue.
  2980. */
  2981. new_bfqq->allocated[rq_data_dir(rq)]++;
  2982. bfqq->allocated[rq_data_dir(rq)]--;
  2983. atomic_inc(&new_bfqq->ref);
  2984. bfq_put_queue(bfqq);
  2985. if (bic_to_bfqq(RQ_BIC(rq), 1) == bfqq)
  2986. bfq_merge_bfqqs(bfqd, RQ_BIC(rq),
  2987. bfqq, new_bfqq);
  2988. rq->elv.priv[1] = new_bfqq;
  2989. bfqq = new_bfqq;
  2990. } else
  2991. bfq_bfqq_increase_failed_cooperations(bfqq);
  2992. }
  2993. bfq_add_request(rq);
  2994. /*
  2995. * Here a newly-created bfq_queue has already started a weight-raising
  2996. * period: clear raising_time_left to prevent bfq_bfqq_save_state()
  2997. * from assigning it a full weight-raising period. See the detailed
  2998. * comments about this field in bfq_init_icq().
  2999. */
  3000. if (bfqq->bic != NULL)
  3001. bfqq->bic->wr_time_left = 0;
  3002. rq_set_fifo_time(rq, jiffies + bfqd->bfq_fifo_expire[rq_is_sync(rq)]);
  3003. list_add_tail(&rq->queuelist, &bfqq->fifo);
  3004. bfq_rq_enqueued(bfqd, bfqq, rq);
  3005. }
  3006. static void bfq_update_hw_tag(struct bfq_data *bfqd)
  3007. {
  3008. bfqd->max_rq_in_driver = max(bfqd->max_rq_in_driver,
  3009. bfqd->rq_in_driver);
  3010. if (bfqd->hw_tag == 1)
  3011. return;
  3012. /*
  3013. * This sample is valid if the number of outstanding requests
  3014. * is large enough to allow a queueing behavior. Note that the
  3015. * sum is not exact, as it's not taking into account deactivated
  3016. * requests.
  3017. */
  3018. if (bfqd->rq_in_driver + bfqd->queued < BFQ_HW_QUEUE_THRESHOLD)
  3019. return;
  3020. if (bfqd->hw_tag_samples++ < BFQ_HW_QUEUE_SAMPLES)
  3021. return;
  3022. bfqd->hw_tag = bfqd->max_rq_in_driver > BFQ_HW_QUEUE_THRESHOLD;
  3023. bfqd->max_rq_in_driver = 0;
  3024. bfqd->hw_tag_samples = 0;
  3025. }
  3026. static void bfq_completed_request(struct request_queue *q, struct request *rq)
  3027. {
  3028. struct bfq_queue *bfqq = RQ_BFQQ(rq);
  3029. struct bfq_data *bfqd = bfqq->bfqd;
  3030. bool sync = bfq_bfqq_sync(bfqq);
  3031. bfq_log_bfqq(bfqd, bfqq, "completed one req with %u sects left (%d)",
  3032. blk_rq_sectors(rq), sync);
  3033. bfq_update_hw_tag(bfqd);
  3034. BUG_ON(!bfqd->rq_in_driver);
  3035. BUG_ON(!bfqq->dispatched);
  3036. bfqd->rq_in_driver--;
  3037. bfqq->dispatched--;
  3038. if (!bfqq->dispatched && !bfq_bfqq_busy(bfqq)) {
  3039. bfq_weights_tree_remove(bfqd, &bfqq->entity,
  3040. &bfqd->queue_weights_tree);
  3041. if (!blk_queue_nonrot(bfqd->queue)) {
  3042. BUG_ON(!bfqd->busy_in_flight_queues);
  3043. bfqd->busy_in_flight_queues--;
  3044. if (bfq_bfqq_constantly_seeky(bfqq)) {
  3045. BUG_ON(!bfqd->
  3046. const_seeky_busy_in_flight_queues);
  3047. bfqd->const_seeky_busy_in_flight_queues--;
  3048. }
  3049. }
  3050. }
  3051. if (sync) {
  3052. bfqd->sync_flight--;
  3053. RQ_BIC(rq)->ttime.last_end_request = jiffies;
  3054. }
  3055. /*
  3056. * If we are waiting to discover whether the request pattern of the
  3057. * task associated with the queue is actually isochronous, and
  3058. * both requisites for this condition to hold are satisfied, then
  3059. * compute soft_rt_next_start (see the comments to the function
  3060. * bfq_bfqq_softrt_next_start()).
  3061. */
  3062. if (bfq_bfqq_softrt_update(bfqq) && bfqq->dispatched == 0 &&
  3063. RB_EMPTY_ROOT(&bfqq->sort_list))
  3064. bfqq->soft_rt_next_start =
  3065. bfq_bfqq_softrt_next_start(bfqd, bfqq);
  3066. /*
  3067. * If this is the in-service queue, check if it needs to be expired,
  3068. * or if we want to idle in case it has no pending requests.
  3069. */
  3070. if (bfqd->in_service_queue == bfqq) {
  3071. if (bfq_bfqq_budget_new(bfqq))
  3072. bfq_set_budget_timeout(bfqd);
  3073. if (bfq_bfqq_must_idle(bfqq)) {
  3074. bfq_arm_slice_timer(bfqd);
  3075. goto out;
  3076. } else if (bfq_may_expire_for_budg_timeout(bfqq))
  3077. bfq_bfqq_expire(bfqd, bfqq, 0, BFQ_BFQQ_BUDGET_TIMEOUT);
  3078. else if (RB_EMPTY_ROOT(&bfqq->sort_list) &&
  3079. (bfqq->dispatched == 0 ||
  3080. !bfq_bfqq_must_not_expire(bfqq)))
  3081. bfq_bfqq_expire(bfqd, bfqq, 0,
  3082. BFQ_BFQQ_NO_MORE_REQUESTS);
  3083. }
  3084. if (!bfqd->rq_in_driver)
  3085. bfq_schedule_dispatch(bfqd);
  3086. out:
  3087. return;
  3088. }
  3089. static inline int __bfq_may_queue(struct bfq_queue *bfqq)
  3090. {
  3091. if (bfq_bfqq_wait_request(bfqq) && bfq_bfqq_must_alloc(bfqq)) {
  3092. bfq_clear_bfqq_must_alloc(bfqq);
  3093. return ELV_MQUEUE_MUST;
  3094. }
  3095. return ELV_MQUEUE_MAY;
  3096. }
  3097. static int bfq_may_queue(struct request_queue *q, int rw)
  3098. {
  3099. struct bfq_data *bfqd = q->elevator->elevator_data;
  3100. struct task_struct *tsk = current;
  3101. struct bfq_io_cq *bic;
  3102. struct bfq_queue *bfqq;
  3103. /*
  3104. * Don't force setup of a queue from here, as a call to may_queue
  3105. * does not necessarily imply that a request actually will be
  3106. * queued. So just lookup a possibly existing queue, or return
  3107. * 'may queue' if that fails.
  3108. */
  3109. bic = bfq_bic_lookup(bfqd, tsk->io_context);
  3110. if (bic == NULL)
  3111. return ELV_MQUEUE_MAY;
  3112. bfqq = bic_to_bfqq(bic, rw_is_sync(rw));
  3113. if (bfqq != NULL)
  3114. return __bfq_may_queue(bfqq);
  3115. return ELV_MQUEUE_MAY;
  3116. }
  3117. /*
  3118. * Queue lock held here.
  3119. */
  3120. static void bfq_put_request(struct request *rq)
  3121. {
  3122. struct bfq_queue *bfqq = RQ_BFQQ(rq);
  3123. if (bfqq != NULL) {
  3124. const int rw = rq_data_dir(rq);
  3125. BUG_ON(!bfqq->allocated[rw]);
  3126. bfqq->allocated[rw]--;
  3127. rq->elv.priv[0] = NULL;
  3128. rq->elv.priv[1] = NULL;
  3129. bfq_log_bfqq(bfqq->bfqd, bfqq, "put_request %p, %d",
  3130. bfqq, atomic_read(&bfqq->ref));
  3131. bfq_put_queue(bfqq);
  3132. }
  3133. }
  3134. /*
  3135. * Returns NULL if a new bfqq should be allocated, or the old bfqq if this
  3136. * was the last process referring to said bfqq.
  3137. */
  3138. static struct bfq_queue *
  3139. bfq_split_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq)
  3140. {
  3141. bfq_log_bfqq(bfqq->bfqd, bfqq, "splitting queue");
  3142. put_io_context(bic->icq.ioc);
  3143. if (bfqq_process_refs(bfqq) == 1) {
  3144. bfqq->pid = current->pid;
  3145. bfq_clear_bfqq_coop(bfqq);
  3146. bfq_clear_bfqq_split_coop(bfqq);
  3147. return bfqq;
  3148. }
  3149. bic_set_bfqq(bic, NULL, 1);
  3150. bfq_put_cooperator(bfqq);
  3151. bfq_put_queue(bfqq);
  3152. return NULL;
  3153. }
  3154. /*
  3155. * Allocate bfq data structures associated with this request.
  3156. */
  3157. static int bfq_set_request(struct request_queue *q, struct request *rq,
  3158. gfp_t gfp_mask)
  3159. {
  3160. struct bfq_data *bfqd = q->elevator->elevator_data;
  3161. struct bfq_io_cq *bic = icq_to_bic(rq->elv.icq);
  3162. const int rw = rq_data_dir(rq);
  3163. const int is_sync = rq_is_sync(rq);
  3164. struct bfq_queue *bfqq;
  3165. struct bfq_group *bfqg;
  3166. unsigned long flags;
  3167. bool split = false;
  3168. /* handle changed prio notifications; cgroup change is handled separately */
  3169. if (unlikely(icq_get_changed(&bic->icq) & ICQ_IOPRIO_CHANGED))
  3170. bfq_check_ioprio_change(bic->icq.ioc, bic);
  3171. might_sleep_if(gfp_mask & __GFP_WAIT);
  3172. spin_lock_irqsave(q->queue_lock, flags);
  3173. if (bic == NULL)
  3174. goto queue_fail;
  3175. bfqg = bfq_bic_update_cgroup(bic);
  3176. new_queue:
  3177. bfqq = bic_to_bfqq(bic, is_sync);
  3178. if (bfqq == NULL || bfqq == &bfqd->oom_bfqq) {
  3179. bfqq = bfq_get_queue(bfqd, bfqg, is_sync, bic->icq.ioc, gfp_mask);
  3180. bic_set_bfqq(bic, bfqq, is_sync);
  3181. if (split && is_sync) {
  3182. if ((bic->was_in_burst_list && bfqd->large_burst) ||
  3183. bic->saved_in_large_burst)
  3184. bfq_mark_bfqq_in_large_burst(bfqq);
  3185. else {
  3186. bfq_clear_bfqq_in_large_burst(bfqq);
  3187. if (bic->was_in_burst_list)
  3188. hlist_add_head(&bfqq->burst_list_node,
  3189. &bfqd->burst_list);
  3190. }
  3191. }
  3192. } else {
  3193. /* If the queue was seeky for too long, break it apart. */
  3194. if (bfq_bfqq_coop(bfqq) && bfq_bfqq_split_coop(bfqq)) {
  3195. bfq_log_bfqq(bfqd, bfqq, "breaking apart bfqq");
  3196. bfqq = bfq_split_bfqq(bic, bfqq);
  3197. split = true;
  3198. if (!bfqq)
  3199. goto new_queue;
  3200. }
  3201. }
  3202. bfqq->allocated[rw]++;
  3203. atomic_inc(&bfqq->ref);
  3204. bfq_log_bfqq(bfqd, bfqq, "set_request: bfqq %p, %d", bfqq,
  3205. atomic_read(&bfqq->ref));
  3206. rq->elv.priv[0] = bic;
  3207. rq->elv.priv[1] = bfqq;
  3208. /*
  3209. * If a bfq_queue has only one process reference, it is owned
  3210. * by only one bfq_io_cq: we can set the bic field of the
  3211. * bfq_queue to the address of that structure. Also, if the
  3212. * queue has just been split, mark a flag so that the
  3213. * information is available to the other scheduler hooks.
  3214. */
  3215. if (likely(bfqq != &bfqd->oom_bfqq) && bfqq_process_refs(bfqq) == 1) {
  3216. bfqq->bic = bic;
  3217. if (split) {
  3218. bfq_mark_bfqq_just_split(bfqq);
  3219. /*
  3220. * If the queue has just been split from a shared
  3221. * queue, restore the idle window and the possible
  3222. * weight raising period.
  3223. */
  3224. bfq_bfqq_resume_state(bfqq, bic);
  3225. }
  3226. }
  3227. spin_unlock_irqrestore(q->queue_lock, flags);
  3228. return 0;
  3229. queue_fail:
  3230. bfq_schedule_dispatch(bfqd);
  3231. spin_unlock_irqrestore(q->queue_lock, flags);
  3232. return 1;
  3233. }
  3234. static void bfq_kick_queue(struct work_struct *work)
  3235. {
  3236. struct bfq_data *bfqd =
  3237. container_of(work, struct bfq_data, unplug_work);
  3238. struct request_queue *q = bfqd->queue;
  3239. spin_lock_irq(q->queue_lock);
  3240. __blk_run_queue(q);
  3241. spin_unlock_irq(q->queue_lock);
  3242. }
  3243. /*
  3244. * Handler of the expiration of the timer running if the in-service queue
  3245. * is idling inside its time slice.
  3246. */
  3247. static void bfq_idle_slice_timer(unsigned long data)
  3248. {
  3249. struct bfq_data *bfqd = (struct bfq_data *)data;
  3250. struct bfq_queue *bfqq;
  3251. unsigned long flags;
  3252. enum bfqq_expiration reason;
  3253. spin_lock_irqsave(bfqd->queue->queue_lock, flags);
  3254. bfqq = bfqd->in_service_queue;
  3255. /*
  3256. * Theoretical race here: the in-service queue can be NULL or
  3257. * different from the queue that was idling if the timer handler
  3258. * spins on the queue_lock and a new request arrives for the
  3259. * current queue and there is a full dispatch cycle that changes
  3260. * the in-service queue. This can hardly happen, but in the worst
  3261. * case we just expire a queue too early.
  3262. */
  3263. if (bfqq != NULL) {
  3264. bfq_log_bfqq(bfqd, bfqq, "slice_timer expired");
  3265. if (bfq_bfqq_budget_timeout(bfqq))
  3266. /*
  3267. * Also here the queue can be safely expired
  3268. * for budget timeout without wasting
  3269. * guarantees
  3270. */
  3271. reason = BFQ_BFQQ_BUDGET_TIMEOUT;
  3272. else if (bfqq->queued[0] == 0 && bfqq->queued[1] == 0)
  3273. /*
  3274. * The queue may not be empty upon timer expiration,
  3275. * because we may not disable the timer when the
  3276. * first request of the in-service queue arrives
  3277. * during disk idling.
  3278. */
  3279. reason = BFQ_BFQQ_TOO_IDLE;
  3280. else
  3281. goto schedule_dispatch;
  3282. bfq_bfqq_expire(bfqd, bfqq, 1, reason);
  3283. }
  3284. schedule_dispatch:
  3285. bfq_schedule_dispatch(bfqd);
  3286. spin_unlock_irqrestore(bfqd->queue->queue_lock, flags);
  3287. }
  3288. static void bfq_shutdown_timer_wq(struct bfq_data *bfqd)
  3289. {
  3290. del_timer_sync(&bfqd->idle_slice_timer);
  3291. cancel_work_sync(&bfqd->unplug_work);
  3292. }
  3293. static inline void __bfq_put_async_bfqq(struct bfq_data *bfqd,
  3294. struct bfq_queue **bfqq_ptr)
  3295. {
  3296. struct bfq_group *root_group = bfqd->root_group;
  3297. struct bfq_queue *bfqq = *bfqq_ptr;
  3298. bfq_log(bfqd, "put_async_bfqq: %p", bfqq);
  3299. if (bfqq != NULL) {
  3300. bfq_bfqq_move(bfqd, bfqq, &bfqq->entity, root_group);
  3301. bfq_log_bfqq(bfqd, bfqq, "put_async_bfqq: putting %p, %d",
  3302. bfqq, atomic_read(&bfqq->ref));
  3303. bfq_put_queue(bfqq);
  3304. *bfqq_ptr = NULL;
  3305. }
  3306. }
  3307. /*
  3308. * Release all the bfqg references to its async queues. If we are
  3309. * deallocating the group these queues may still contain requests, so
  3310. * we reparent them to the root cgroup (i.e., the only one that will
  3311. * exist for sure until all the requests on a device are gone).
  3312. */
  3313. static void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg)
  3314. {
  3315. int i, j;
  3316. for (i = 0; i < 2; i++)
  3317. for (j = 0; j < IOPRIO_BE_NR; j++)
  3318. __bfq_put_async_bfqq(bfqd, &bfqg->async_bfqq[i][j]);
  3319. __bfq_put_async_bfqq(bfqd, &bfqg->async_idle_bfqq);
  3320. }
  3321. static void bfq_exit_queue(struct elevator_queue *e)
  3322. {
  3323. struct bfq_data *bfqd = e->elevator_data;
  3324. struct request_queue *q = bfqd->queue;
  3325. struct bfq_queue *bfqq, *n;
  3326. bfq_shutdown_timer_wq(bfqd);
  3327. spin_lock_irq(q->queue_lock);
  3328. BUG_ON(bfqd->in_service_queue != NULL);
  3329. list_for_each_entry_safe(bfqq, n, &bfqd->idle_list, bfqq_list)
  3330. bfq_deactivate_bfqq(bfqd, bfqq, 0);
  3331. bfq_disconnect_groups(bfqd);
  3332. spin_unlock_irq(q->queue_lock);
  3333. bfq_shutdown_timer_wq(bfqd);
  3334. synchronize_rcu();
  3335. BUG_ON(timer_pending(&bfqd->idle_slice_timer));
  3336. bfq_free_root_group(bfqd);
  3337. kfree(bfqd);
  3338. }
  3339. static void *bfq_init_queue(struct request_queue *q)
  3340. {
  3341. struct bfq_group *bfqg;
  3342. struct bfq_data *bfqd;
  3343. bfqd = kzalloc_node(sizeof(*bfqd), GFP_KERNEL, q->node);
  3344. if (bfqd == NULL)
  3345. return NULL;
  3346. /*
  3347. * Our fallback bfqq if bfq_find_alloc_queue() runs into OOM issues.
  3348. * Grab a permanent reference to it, so that the normal code flow
  3349. * will not attempt to free it.
  3350. */
  3351. bfq_init_bfqq(bfqd, &bfqd->oom_bfqq, NULL, 1, 0);
  3352. atomic_inc(&bfqd->oom_bfqq.ref);
  3353. bfqd->oom_bfqq.entity.new_ioprio = BFQ_DEFAULT_QUEUE_IOPRIO;
  3354. bfqd->oom_bfqq.entity.new_ioprio_class = IOPRIO_CLASS_BE;
  3355. bfqd->oom_bfqq.entity.new_weight =
  3356. bfq_ioprio_to_weight(bfqd->oom_bfqq.entity.new_ioprio);
  3357. /*
  3358. * Trigger weight initialization, according to ioprio, at the
  3359. * oom_bfqq's first activation. The oom_bfqq's ioprio and ioprio
  3360. * class won't be changed any more.
  3361. */
  3362. bfqd->oom_bfqq.entity.ioprio_changed = 1;
  3363. bfqd->queue = q;
  3364. bfqg = bfq_alloc_root_group(bfqd, q->node);
  3365. if (bfqg == NULL) {
  3366. kfree(bfqd);
  3367. return NULL;
  3368. }
  3369. bfqd->root_group = bfqg;
  3370. bfq_init_entity(&bfqd->oom_bfqq.entity, bfqd->root_group);
  3371. #ifdef CONFIG_CGROUP_BFQIO
  3372. bfqd->active_numerous_groups = 0;
  3373. #endif
  3374. init_timer(&bfqd->idle_slice_timer);
  3375. bfqd->idle_slice_timer.function = bfq_idle_slice_timer;
  3376. bfqd->idle_slice_timer.data = (unsigned long)bfqd;
  3377. bfqd->rq_pos_tree = RB_ROOT;
  3378. bfqd->queue_weights_tree = RB_ROOT;
  3379. bfqd->group_weights_tree = RB_ROOT;
  3380. INIT_WORK(&bfqd->unplug_work, bfq_kick_queue);
  3381. INIT_LIST_HEAD(&bfqd->active_list);
  3382. INIT_LIST_HEAD(&bfqd->idle_list);
  3383. INIT_HLIST_HEAD(&bfqd->burst_list);
  3384. bfqd->hw_tag = -1;
  3385. bfqd->bfq_max_budget = bfq_default_max_budget;
  3386. bfqd->bfq_fifo_expire[0] = bfq_fifo_expire[0];
  3387. bfqd->bfq_fifo_expire[1] = bfq_fifo_expire[1];
  3388. bfqd->bfq_back_max = bfq_back_max;
  3389. bfqd->bfq_back_penalty = bfq_back_penalty;
  3390. bfqd->bfq_slice_idle = bfq_slice_idle;
  3391. bfqd->bfq_class_idle_last_service = 0;
  3392. bfqd->bfq_max_budget_async_rq = bfq_max_budget_async_rq;
  3393. bfqd->bfq_timeout[BLK_RW_ASYNC] = bfq_timeout_async;
  3394. bfqd->bfq_timeout[BLK_RW_SYNC] = bfq_timeout_sync;
  3395. bfqd->bfq_coop_thresh = 2;
  3396. bfqd->bfq_failed_cooperations = 7000;
  3397. bfqd->bfq_requests_within_timer = 120;
  3398. bfqd->bfq_large_burst_thresh = 11;
  3399. bfqd->bfq_burst_interval = msecs_to_jiffies(500);
  3400. bfqd->low_latency = true;
  3401. bfqd->bfq_wr_coeff = 20;
  3402. bfqd->bfq_wr_rt_max_time = msecs_to_jiffies(300);
  3403. bfqd->bfq_wr_max_time = 0;
  3404. bfqd->bfq_wr_min_idle_time = msecs_to_jiffies(2000);
  3405. bfqd->bfq_wr_min_inter_arr_async = msecs_to_jiffies(500);
  3406. bfqd->bfq_wr_max_softrt_rate = 7000; /*
  3407. * Approximate rate required
  3408. * to playback or record a
  3409. * high-definition compressed
  3410. * video.
  3411. */
  3412. bfqd->wr_busy_queues = 0;
  3413. bfqd->busy_in_flight_queues = 0;
  3414. bfqd->const_seeky_busy_in_flight_queues = 0;
  3415. /*
  3416. * Begin by assuming, optimistically, that the device peak rate is
  3417. * equal to the highest reference rate.
  3418. */
  3419. bfqd->RT_prod = R_fast[blk_queue_nonrot(bfqd->queue)] *
  3420. T_fast[blk_queue_nonrot(bfqd->queue)];
  3421. bfqd->peak_rate = R_fast[blk_queue_nonrot(bfqd->queue)];
  3422. bfqd->device_speed = BFQ_BFQD_FAST;
  3423. return bfqd;
  3424. }
  3425. static void bfq_slab_kill(void)
  3426. {
  3427. if (bfq_pool != NULL)
  3428. kmem_cache_destroy(bfq_pool);
  3429. }
  3430. static int __init bfq_slab_setup(void)
  3431. {
  3432. bfq_pool = KMEM_CACHE(bfq_queue, 0);
  3433. if (bfq_pool == NULL)
  3434. return -ENOMEM;
  3435. return 0;
  3436. }
  3437. static ssize_t bfq_var_show(unsigned int var, char *page)
  3438. {
  3439. return sprintf(page, "%d\n", var);
  3440. }
  3441. static ssize_t bfq_var_store(unsigned long *var, const char *page,
  3442. size_t count)
  3443. {
  3444. unsigned long new_val;
  3445. int ret = kstrtoul(page, 10, &new_val);
  3446. if (ret == 0)
  3447. *var = new_val;
  3448. return count;
  3449. }
  3450. static ssize_t bfq_wr_max_time_show(struct elevator_queue *e, char *page)
  3451. {
  3452. struct bfq_data *bfqd = e->elevator_data;
  3453. return sprintf(page, "%d\n", bfqd->bfq_wr_max_time > 0 ?
  3454. jiffies_to_msecs(bfqd->bfq_wr_max_time) :
  3455. jiffies_to_msecs(bfq_wr_duration(bfqd)));
  3456. }
  3457. static ssize_t bfq_weights_show(struct elevator_queue *e, char *page)
  3458. {
  3459. struct bfq_queue *bfqq;
  3460. struct bfq_data *bfqd = e->elevator_data;
  3461. ssize_t num_char = 0;
  3462. num_char += sprintf(page + num_char, "Tot reqs queued %d\n\n",
  3463. bfqd->queued);
  3464. spin_lock_irq(bfqd->queue->queue_lock);
  3465. num_char += sprintf(page + num_char, "Active:\n");
  3466. list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list) {
  3467. num_char += sprintf(page + num_char,
  3468. "pid%d: weight %hu, nr_queued %d %d, dur %d/%u\n",
  3469. bfqq->pid,
  3470. bfqq->entity.weight,
  3471. bfqq->queued[0],
  3472. bfqq->queued[1],
  3473. jiffies_to_msecs(jiffies - bfqq->last_wr_start_finish),
  3474. jiffies_to_msecs(bfqq->wr_cur_max_time));
  3475. }
  3476. num_char += sprintf(page + num_char, "Idle:\n");
  3477. list_for_each_entry(bfqq, &bfqd->idle_list, bfqq_list) {
  3478. num_char += sprintf(page + num_char,
  3479. "pid%d: weight %hu, dur %d/%u\n",
  3480. bfqq->pid,
  3481. bfqq->entity.weight,
  3482. jiffies_to_msecs(jiffies -
  3483. bfqq->last_wr_start_finish),
  3484. jiffies_to_msecs(bfqq->wr_cur_max_time));
  3485. }
  3486. spin_unlock_irq(bfqd->queue->queue_lock);
  3487. return num_char;
  3488. }
  3489. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  3490. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  3491. { \
  3492. struct bfq_data *bfqd = e->elevator_data; \
  3493. unsigned int __data = __VAR; \
  3494. if (__CONV) \
  3495. __data = jiffies_to_msecs(__data); \
  3496. return bfq_var_show(__data, (page)); \
  3497. }
  3498. SHOW_FUNCTION(bfq_fifo_expire_sync_show, bfqd->bfq_fifo_expire[1], 1);
  3499. SHOW_FUNCTION(bfq_fifo_expire_async_show, bfqd->bfq_fifo_expire[0], 1);
  3500. SHOW_FUNCTION(bfq_back_seek_max_show, bfqd->bfq_back_max, 0);
  3501. SHOW_FUNCTION(bfq_back_seek_penalty_show, bfqd->bfq_back_penalty, 0);
  3502. SHOW_FUNCTION(bfq_slice_idle_show, bfqd->bfq_slice_idle, 1);
  3503. SHOW_FUNCTION(bfq_max_budget_show, bfqd->bfq_user_max_budget, 0);
  3504. SHOW_FUNCTION(bfq_max_budget_async_rq_show,
  3505. bfqd->bfq_max_budget_async_rq, 0);
  3506. SHOW_FUNCTION(bfq_timeout_sync_show, bfqd->bfq_timeout[BLK_RW_SYNC], 1);
  3507. SHOW_FUNCTION(bfq_timeout_async_show, bfqd->bfq_timeout[BLK_RW_ASYNC], 1);
  3508. SHOW_FUNCTION(bfq_low_latency_show, bfqd->low_latency, 0);
  3509. SHOW_FUNCTION(bfq_wr_coeff_show, bfqd->bfq_wr_coeff, 0);
  3510. SHOW_FUNCTION(bfq_wr_rt_max_time_show, bfqd->bfq_wr_rt_max_time, 1);
  3511. SHOW_FUNCTION(bfq_wr_min_idle_time_show, bfqd->bfq_wr_min_idle_time, 1);
  3512. SHOW_FUNCTION(bfq_wr_min_inter_arr_async_show, bfqd->bfq_wr_min_inter_arr_async,
  3513. 1);
  3514. SHOW_FUNCTION(bfq_wr_max_softrt_rate_show, bfqd->bfq_wr_max_softrt_rate, 0);
  3515. #undef SHOW_FUNCTION
  3516. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  3517. static ssize_t \
  3518. __FUNC(struct elevator_queue *e, const char *page, size_t count) \
  3519. { \
  3520. struct bfq_data *bfqd = e->elevator_data; \
  3521. unsigned long uninitialized_var(__data); \
  3522. int ret = bfq_var_store(&__data, (page), count); \
  3523. if (__data < (MIN)) \
  3524. __data = (MIN); \
  3525. else if (__data > (MAX)) \
  3526. __data = (MAX); \
  3527. if (__CONV) \
  3528. *(__PTR) = msecs_to_jiffies(__data); \
  3529. else \
  3530. *(__PTR) = __data; \
  3531. return ret; \
  3532. }
  3533. STORE_FUNCTION(bfq_fifo_expire_sync_store, &bfqd->bfq_fifo_expire[1], 1,
  3534. INT_MAX, 1);
  3535. STORE_FUNCTION(bfq_fifo_expire_async_store, &bfqd->bfq_fifo_expire[0], 1,
  3536. INT_MAX, 1);
  3537. STORE_FUNCTION(bfq_back_seek_max_store, &bfqd->bfq_back_max, 0, INT_MAX, 0);
  3538. STORE_FUNCTION(bfq_back_seek_penalty_store, &bfqd->bfq_back_penalty, 1,
  3539. INT_MAX, 0);
  3540. STORE_FUNCTION(bfq_slice_idle_store, &bfqd->bfq_slice_idle, 0, INT_MAX, 1);
  3541. STORE_FUNCTION(bfq_max_budget_async_rq_store, &bfqd->bfq_max_budget_async_rq,
  3542. 1, INT_MAX, 0);
  3543. STORE_FUNCTION(bfq_timeout_async_store, &bfqd->bfq_timeout[BLK_RW_ASYNC], 0,
  3544. INT_MAX, 1);
  3545. STORE_FUNCTION(bfq_wr_coeff_store, &bfqd->bfq_wr_coeff, 1, INT_MAX, 0);
  3546. STORE_FUNCTION(bfq_wr_max_time_store, &bfqd->bfq_wr_max_time, 0, INT_MAX, 1);
  3547. STORE_FUNCTION(bfq_wr_rt_max_time_store, &bfqd->bfq_wr_rt_max_time, 0, INT_MAX,
  3548. 1);
  3549. STORE_FUNCTION(bfq_wr_min_idle_time_store, &bfqd->bfq_wr_min_idle_time, 0,
  3550. INT_MAX, 1);
  3551. STORE_FUNCTION(bfq_wr_min_inter_arr_async_store,
  3552. &bfqd->bfq_wr_min_inter_arr_async, 0, INT_MAX, 1);
  3553. STORE_FUNCTION(bfq_wr_max_softrt_rate_store, &bfqd->bfq_wr_max_softrt_rate, 0,
  3554. INT_MAX, 0);
  3555. #undef STORE_FUNCTION
  3556. /* do nothing for the moment */
  3557. static ssize_t bfq_weights_store(struct elevator_queue *e,
  3558. const char *page, size_t count)
  3559. {
  3560. return count;
  3561. }
  3562. static inline unsigned long bfq_estimated_max_budget(struct bfq_data *bfqd)
  3563. {
  3564. u64 timeout = jiffies_to_msecs(bfqd->bfq_timeout[BLK_RW_SYNC]);
  3565. if (bfqd->peak_rate_samples >= BFQ_PEAK_RATE_SAMPLES)
  3566. return bfq_calc_max_budget(bfqd->peak_rate, timeout);
  3567. else
  3568. return bfq_default_max_budget;
  3569. }
  3570. static ssize_t bfq_max_budget_store(struct elevator_queue *e,
  3571. const char *page, size_t count)
  3572. {
  3573. struct bfq_data *bfqd = e->elevator_data;
  3574. unsigned long uninitialized_var(__data);
  3575. int ret = bfq_var_store(&__data, (page), count);
  3576. if (__data == 0)
  3577. bfqd->bfq_max_budget = bfq_estimated_max_budget(bfqd);
  3578. else {
  3579. if (__data > INT_MAX)
  3580. __data = INT_MAX;
  3581. bfqd->bfq_max_budget = __data;
  3582. }
  3583. bfqd->bfq_user_max_budget = __data;
  3584. return ret;
  3585. }
  3586. static ssize_t bfq_timeout_sync_store(struct elevator_queue *e,
  3587. const char *page, size_t count)
  3588. {
  3589. struct bfq_data *bfqd = e->elevator_data;
  3590. unsigned long uninitialized_var(__data);
  3591. int ret = bfq_var_store(&__data, (page), count);
  3592. if (__data < 1)
  3593. __data = 1;
  3594. else if (__data > INT_MAX)
  3595. __data = INT_MAX;
  3596. bfqd->bfq_timeout[BLK_RW_SYNC] = msecs_to_jiffies(__data);
  3597. if (bfqd->bfq_user_max_budget == 0)
  3598. bfqd->bfq_max_budget = bfq_estimated_max_budget(bfqd);
  3599. return ret;
  3600. }
  3601. static ssize_t bfq_low_latency_store(struct elevator_queue *e,
  3602. const char *page, size_t count)
  3603. {
  3604. struct bfq_data *bfqd = e->elevator_data;
  3605. unsigned long uninitialized_var(__data);
  3606. int ret = bfq_var_store(&__data, (page), count);
  3607. if (__data > 1)
  3608. __data = 1;
  3609. if (__data == 0 && bfqd->low_latency != 0)
  3610. bfq_end_wr(bfqd);
  3611. bfqd->low_latency = __data;
  3612. return ret;
  3613. }
  3614. #define BFQ_ATTR(name) \
  3615. __ATTR(name, S_IRUGO|S_IWUSR, bfq_##name##_show, bfq_##name##_store)
  3616. static struct elv_fs_entry bfq_attrs[] = {
  3617. BFQ_ATTR(fifo_expire_sync),
  3618. BFQ_ATTR(fifo_expire_async),
  3619. BFQ_ATTR(back_seek_max),
  3620. BFQ_ATTR(back_seek_penalty),
  3621. BFQ_ATTR(slice_idle),
  3622. BFQ_ATTR(max_budget),
  3623. BFQ_ATTR(max_budget_async_rq),
  3624. BFQ_ATTR(timeout_sync),
  3625. BFQ_ATTR(timeout_async),
  3626. BFQ_ATTR(low_latency),
  3627. BFQ_ATTR(wr_coeff),
  3628. BFQ_ATTR(wr_max_time),
  3629. BFQ_ATTR(wr_rt_max_time),
  3630. BFQ_ATTR(wr_min_idle_time),
  3631. BFQ_ATTR(wr_min_inter_arr_async),
  3632. BFQ_ATTR(wr_max_softrt_rate),
  3633. BFQ_ATTR(weights),
  3634. __ATTR_NULL
  3635. };
  3636. static struct elevator_type iosched_bfq = {
  3637. .ops = {
  3638. .elevator_merge_fn = bfq_merge,
  3639. .elevator_merged_fn = bfq_merged_request,
  3640. .elevator_merge_req_fn = bfq_merged_requests,
  3641. .elevator_allow_merge_fn = bfq_allow_merge,
  3642. .elevator_dispatch_fn = bfq_dispatch_requests,
  3643. .elevator_add_req_fn = bfq_insert_request,
  3644. .elevator_activate_req_fn = bfq_activate_request,
  3645. .elevator_deactivate_req_fn = bfq_deactivate_request,
  3646. .elevator_completed_req_fn = bfq_completed_request,
  3647. .elevator_former_req_fn = elv_rb_former_request,
  3648. .elevator_latter_req_fn = elv_rb_latter_request,
  3649. .elevator_init_icq_fn = bfq_init_icq,
  3650. .elevator_exit_icq_fn = bfq_exit_icq,
  3651. .elevator_set_req_fn = bfq_set_request,
  3652. .elevator_put_req_fn = bfq_put_request,
  3653. .elevator_may_queue_fn = bfq_may_queue,
  3654. .elevator_init_fn = bfq_init_queue,
  3655. .elevator_exit_fn = bfq_exit_queue,
  3656. },
  3657. .icq_size = sizeof(struct bfq_io_cq),
  3658. .icq_align = __alignof__(struct bfq_io_cq),
  3659. .elevator_attrs = bfq_attrs,
  3660. .elevator_name = "bfq",
  3661. .elevator_owner = THIS_MODULE,
  3662. };
  3663. static int __init bfq_init(void)
  3664. {
  3665. /*
  3666. * Can be 0 on HZ < 1000 setups.
  3667. */
  3668. if (bfq_slice_idle == 0)
  3669. bfq_slice_idle = 1;
  3670. if (bfq_timeout_async == 0)
  3671. bfq_timeout_async = 1;
  3672. if (bfq_slab_setup())
  3673. return -ENOMEM;
  3674. /*
  3675. * Times to load large popular applications for the typical systems
  3676. * installed on the reference devices (see the comments before the
  3677. * definitions of the two arrays).
  3678. */
  3679. T_slow[0] = msecs_to_jiffies(2600);
  3680. T_slow[1] = msecs_to_jiffies(1000);
  3681. T_fast[0] = msecs_to_jiffies(5500);
  3682. T_fast[1] = msecs_to_jiffies(2000);
  3683. /*
  3684. * Thresholds that determine the switch between speed classes (see
  3685. * the comments before the definition of the array).
  3686. */
  3687. device_speed_thresh[0] = (R_fast[0] + R_slow[0]) / 2;
  3688. device_speed_thresh[1] = (R_fast[1] + R_slow[1]) / 2;
  3689. elv_register(&iosched_bfq);
  3690. pr_info("BFQ I/O-scheduler: v7r8");
  3691. return 0;
  3692. }
  3693. static void __exit bfq_exit(void)
  3694. {
  3695. elv_unregister(&iosched_bfq);
  3696. bfq_slab_kill();
  3697. }
  3698. module_init(bfq_init);
  3699. module_exit(bfq_exit);
  3700. MODULE_AUTHOR("Fabio Checconi, Paolo Valente");
  3701. MODULE_LICENSE("GPL");