cfq-iosched.c 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935
  1. /*
  2. * CFQ, or complete fairness queueing, disk scheduler.
  3. *
  4. * Based on ideas from a previously unfinished io
  5. * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
  6. *
  7. * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/elevator.h>
  13. #include <linux/ktime.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/ioprio.h>
  16. #include <linux/blktrace_api.h>
  17. #include <linux/blk-cgroup.h>
  18. #include "blk.h"
  19. #include "blk-wbt.h"
  20. /*
  21. * tunables
  22. */
  23. /* max queue in one round of service */
  24. static const int cfq_quantum = 8;
  25. static const u64 cfq_fifo_expire[2] = { NSEC_PER_SEC / 4, NSEC_PER_SEC / 8 };
  26. /* maximum backwards seek, in KiB */
  27. static const int cfq_back_max = 16 * 1024;
  28. /* penalty of a backwards seek */
  29. static const int cfq_back_penalty = 2;
  30. static const u64 cfq_slice_sync = NSEC_PER_SEC / 10;
  31. static u64 cfq_slice_async = NSEC_PER_SEC / 25;
  32. static const int cfq_slice_async_rq = 2;
  33. static u64 cfq_slice_idle = NSEC_PER_SEC / 125;
  34. static u64 cfq_group_idle = NSEC_PER_SEC / 125;
  35. static const u64 cfq_target_latency = (u64)NSEC_PER_SEC * 3/10; /* 300 ms */
  36. static const int cfq_hist_divisor = 4;
  37. /*
  38. * offset from end of service tree
  39. */
  40. #define CFQ_IDLE_DELAY (NSEC_PER_SEC / 5)
  41. /*
  42. * below this threshold, we consider thinktime immediate
  43. */
  44. #define CFQ_MIN_TT (2 * NSEC_PER_SEC / HZ)
  45. #define CFQ_SLICE_SCALE (5)
  46. #define CFQ_HW_QUEUE_MIN (5)
  47. #define CFQ_SERVICE_SHIFT 12
  48. #define CFQQ_SEEK_THR (sector_t)(8 * 100)
  49. #define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
  50. #define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
  51. #define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
  52. #define RQ_CIC(rq) icq_to_cic((rq)->elv.icq)
  53. #define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0])
  54. #define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1])
  55. static struct kmem_cache *cfq_pool;
  56. #define CFQ_PRIO_LISTS IOPRIO_BE_NR
  57. #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
  58. #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
  59. #define sample_valid(samples) ((samples) > 80)
  60. #define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
  61. /* blkio-related constants */
  62. #define CFQ_WEIGHT_LEGACY_MIN 10
  63. #define CFQ_WEIGHT_LEGACY_DFL 500
  64. #define CFQ_WEIGHT_LEGACY_MAX 1000
  65. struct cfq_ttime {
  66. u64 last_end_request;
  67. u64 ttime_total;
  68. u64 ttime_mean;
  69. unsigned long ttime_samples;
  70. };
  71. /*
  72. * Most of our rbtree usage is for sorting with min extraction, so
  73. * if we cache the leftmost node we don't have to walk down the tree
  74. * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
  75. * move this into the elevator for the rq sorting as well.
  76. */
  77. struct cfq_rb_root {
  78. struct rb_root rb;
  79. struct rb_node *left;
  80. unsigned count;
  81. u64 min_vdisktime;
  82. struct cfq_ttime ttime;
  83. };
  84. #define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
  85. .ttime = {.last_end_request = ktime_get_ns(),},}
  86. /*
  87. * Per process-grouping structure
  88. */
  89. struct cfq_queue {
  90. /* reference count */
  91. int ref;
  92. /* various state flags, see below */
  93. unsigned int flags;
  94. /* parent cfq_data */
  95. struct cfq_data *cfqd;
  96. /* service_tree member */
  97. struct rb_node rb_node;
  98. /* service_tree key */
  99. u64 rb_key;
  100. /* prio tree member */
  101. struct rb_node p_node;
  102. /* prio tree root we belong to, if any */
  103. struct rb_root *p_root;
  104. /* sorted list of pending requests */
  105. struct rb_root sort_list;
  106. /* if fifo isn't expired, next request to serve */
  107. struct request *next_rq;
  108. /* requests queued in sort_list */
  109. int queued[2];
  110. /* currently allocated requests */
  111. int allocated[2];
  112. /* fifo list of requests in sort_list */
  113. struct list_head fifo;
  114. /* time when queue got scheduled in to dispatch first request. */
  115. u64 dispatch_start;
  116. u64 allocated_slice;
  117. u64 slice_dispatch;
  118. /* time when first request from queue completed and slice started. */
  119. u64 slice_start;
  120. u64 slice_end;
  121. s64 slice_resid;
  122. /* pending priority requests */
  123. int prio_pending;
  124. /* number of requests that are on the dispatch list or inside driver */
  125. int dispatched;
  126. /* io prio of this group */
  127. unsigned short ioprio, org_ioprio;
  128. unsigned short ioprio_class, org_ioprio_class;
  129. pid_t pid;
  130. u32 seek_history;
  131. sector_t last_request_pos;
  132. struct cfq_rb_root *service_tree;
  133. struct cfq_queue *new_cfqq;
  134. struct cfq_group *cfqg;
  135. /* Number of sectors dispatched from queue in single dispatch round */
  136. unsigned long nr_sectors;
  137. };
  138. /*
  139. * First index in the service_trees.
  140. * IDLE is handled separately, so it has negative index
  141. */
  142. enum wl_class_t {
  143. BE_WORKLOAD = 0,
  144. RT_WORKLOAD = 1,
  145. IDLE_WORKLOAD = 2,
  146. CFQ_PRIO_NR,
  147. };
  148. /*
  149. * Second index in the service_trees.
  150. */
  151. enum wl_type_t {
  152. ASYNC_WORKLOAD = 0,
  153. SYNC_NOIDLE_WORKLOAD = 1,
  154. SYNC_WORKLOAD = 2
  155. };
  156. struct cfqg_stats {
  157. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  158. /* number of ios merged */
  159. struct blkg_rwstat merged;
  160. /* total time spent on device in ns, may not be accurate w/ queueing */
  161. struct blkg_rwstat service_time;
  162. /* total time spent waiting in scheduler queue in ns */
  163. struct blkg_rwstat wait_time;
  164. /* number of IOs queued up */
  165. struct blkg_rwstat queued;
  166. /* total disk time and nr sectors dispatched by this group */
  167. struct blkg_stat time;
  168. #ifdef CONFIG_DEBUG_BLK_CGROUP
  169. /* time not charged to this cgroup */
  170. struct blkg_stat unaccounted_time;
  171. /* sum of number of ios queued across all samples */
  172. struct blkg_stat avg_queue_size_sum;
  173. /* count of samples taken for average */
  174. struct blkg_stat avg_queue_size_samples;
  175. /* how many times this group has been removed from service tree */
  176. struct blkg_stat dequeue;
  177. /* total time spent waiting for it to be assigned a timeslice. */
  178. struct blkg_stat group_wait_time;
  179. /* time spent idling for this blkcg_gq */
  180. struct blkg_stat idle_time;
  181. /* total time with empty current active q with other requests queued */
  182. struct blkg_stat empty_time;
  183. /* fields after this shouldn't be cleared on stat reset */
  184. uint64_t start_group_wait_time;
  185. uint64_t start_idle_time;
  186. uint64_t start_empty_time;
  187. uint16_t flags;
  188. #endif /* CONFIG_DEBUG_BLK_CGROUP */
  189. #endif /* CONFIG_CFQ_GROUP_IOSCHED */
  190. };
  191. /* Per-cgroup data */
  192. struct cfq_group_data {
  193. /* must be the first member */
  194. struct blkcg_policy_data cpd;
  195. unsigned int weight;
  196. unsigned int leaf_weight;
  197. };
  198. /* This is per cgroup per device grouping structure */
  199. struct cfq_group {
  200. /* must be the first member */
  201. struct blkg_policy_data pd;
  202. /* group service_tree member */
  203. struct rb_node rb_node;
  204. /* group service_tree key */
  205. u64 vdisktime;
  206. /*
  207. * The number of active cfqgs and sum of their weights under this
  208. * cfqg. This covers this cfqg's leaf_weight and all children's
  209. * weights, but does not cover weights of further descendants.
  210. *
  211. * If a cfqg is on the service tree, it's active. An active cfqg
  212. * also activates its parent and contributes to the children_weight
  213. * of the parent.
  214. */
  215. int nr_active;
  216. unsigned int children_weight;
  217. /*
  218. * vfraction is the fraction of vdisktime that the tasks in this
  219. * cfqg are entitled to. This is determined by compounding the
  220. * ratios walking up from this cfqg to the root.
  221. *
  222. * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all
  223. * vfractions on a service tree is approximately 1. The sum may
  224. * deviate a bit due to rounding errors and fluctuations caused by
  225. * cfqgs entering and leaving the service tree.
  226. */
  227. unsigned int vfraction;
  228. /*
  229. * There are two weights - (internal) weight is the weight of this
  230. * cfqg against the sibling cfqgs. leaf_weight is the wight of
  231. * this cfqg against the child cfqgs. For the root cfqg, both
  232. * weights are kept in sync for backward compatibility.
  233. */
  234. unsigned int weight;
  235. unsigned int new_weight;
  236. unsigned int dev_weight;
  237. unsigned int leaf_weight;
  238. unsigned int new_leaf_weight;
  239. unsigned int dev_leaf_weight;
  240. /* number of cfqq currently on this group */
  241. int nr_cfqq;
  242. /*
  243. * Per group busy queues average. Useful for workload slice calc. We
  244. * create the array for each prio class but at run time it is used
  245. * only for RT and BE class and slot for IDLE class remains unused.
  246. * This is primarily done to avoid confusion and a gcc warning.
  247. */
  248. unsigned int busy_queues_avg[CFQ_PRIO_NR];
  249. /*
  250. * rr lists of queues with requests. We maintain service trees for
  251. * RT and BE classes. These trees are subdivided in subclasses
  252. * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
  253. * class there is no subclassification and all the cfq queues go on
  254. * a single tree service_tree_idle.
  255. * Counts are embedded in the cfq_rb_root
  256. */
  257. struct cfq_rb_root service_trees[2][3];
  258. struct cfq_rb_root service_tree_idle;
  259. u64 saved_wl_slice;
  260. enum wl_type_t saved_wl_type;
  261. enum wl_class_t saved_wl_class;
  262. /* number of requests that are on the dispatch list or inside driver */
  263. int dispatched;
  264. struct cfq_ttime ttime;
  265. struct cfqg_stats stats; /* stats for this cfqg */
  266. /* async queue for each priority case */
  267. struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
  268. struct cfq_queue *async_idle_cfqq;
  269. };
  270. struct cfq_io_cq {
  271. struct io_cq icq; /* must be the first member */
  272. struct cfq_queue *cfqq[2];
  273. struct cfq_ttime ttime;
  274. int ioprio; /* the current ioprio */
  275. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  276. uint64_t blkcg_serial_nr; /* the current blkcg serial */
  277. #endif
  278. };
  279. /*
  280. * Per block device queue structure
  281. */
  282. struct cfq_data {
  283. struct request_queue *queue;
  284. /* Root service tree for cfq_groups */
  285. struct cfq_rb_root grp_service_tree;
  286. struct cfq_group *root_group;
  287. /*
  288. * The priority currently being served
  289. */
  290. enum wl_class_t serving_wl_class;
  291. enum wl_type_t serving_wl_type;
  292. u64 workload_expires;
  293. struct cfq_group *serving_group;
  294. /*
  295. * Each priority tree is sorted by next_request position. These
  296. * trees are used when determining if two or more queues are
  297. * interleaving requests (see cfq_close_cooperator).
  298. */
  299. struct rb_root prio_trees[CFQ_PRIO_LISTS];
  300. unsigned int busy_queues;
  301. unsigned int busy_sync_queues;
  302. int rq_in_driver;
  303. int rq_in_flight[2];
  304. /*
  305. * queue-depth detection
  306. */
  307. int rq_queued;
  308. int hw_tag;
  309. /*
  310. * hw_tag can be
  311. * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
  312. * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
  313. * 0 => no NCQ
  314. */
  315. int hw_tag_est_depth;
  316. unsigned int hw_tag_samples;
  317. /*
  318. * idle window management
  319. */
  320. struct hrtimer idle_slice_timer;
  321. struct work_struct unplug_work;
  322. struct cfq_queue *active_queue;
  323. struct cfq_io_cq *active_cic;
  324. sector_t last_position;
  325. /*
  326. * tunables, see top of file
  327. */
  328. unsigned int cfq_quantum;
  329. unsigned int cfq_back_penalty;
  330. unsigned int cfq_back_max;
  331. unsigned int cfq_slice_async_rq;
  332. unsigned int cfq_latency;
  333. u64 cfq_fifo_expire[2];
  334. u64 cfq_slice[2];
  335. u64 cfq_slice_idle;
  336. u64 cfq_group_idle;
  337. u64 cfq_target_latency;
  338. /*
  339. * Fallback dummy cfqq for extreme OOM conditions
  340. */
  341. struct cfq_queue oom_cfqq;
  342. u64 last_delayed_sync;
  343. };
  344. static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
  345. static void cfq_put_queue(struct cfq_queue *cfqq);
  346. static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
  347. enum wl_class_t class,
  348. enum wl_type_t type)
  349. {
  350. if (!cfqg)
  351. return NULL;
  352. if (class == IDLE_WORKLOAD)
  353. return &cfqg->service_tree_idle;
  354. return &cfqg->service_trees[class][type];
  355. }
  356. enum cfqq_state_flags {
  357. CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
  358. CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
  359. CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
  360. CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
  361. CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
  362. CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
  363. CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
  364. CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
  365. CFQ_CFQQ_FLAG_sync, /* synchronous queue */
  366. CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
  367. CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
  368. CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
  369. CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
  370. };
  371. #define CFQ_CFQQ_FNS(name) \
  372. static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
  373. { \
  374. (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
  375. } \
  376. static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
  377. { \
  378. (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
  379. } \
  380. static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
  381. { \
  382. return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
  383. }
  384. CFQ_CFQQ_FNS(on_rr);
  385. CFQ_CFQQ_FNS(wait_request);
  386. CFQ_CFQQ_FNS(must_dispatch);
  387. CFQ_CFQQ_FNS(must_alloc_slice);
  388. CFQ_CFQQ_FNS(fifo_expire);
  389. CFQ_CFQQ_FNS(idle_window);
  390. CFQ_CFQQ_FNS(prio_changed);
  391. CFQ_CFQQ_FNS(slice_new);
  392. CFQ_CFQQ_FNS(sync);
  393. CFQ_CFQQ_FNS(coop);
  394. CFQ_CFQQ_FNS(split_coop);
  395. CFQ_CFQQ_FNS(deep);
  396. CFQ_CFQQ_FNS(wait_busy);
  397. #undef CFQ_CFQQ_FNS
  398. #if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
  399. /* cfqg stats flags */
  400. enum cfqg_stats_flags {
  401. CFQG_stats_waiting = 0,
  402. CFQG_stats_idling,
  403. CFQG_stats_empty,
  404. };
  405. #define CFQG_FLAG_FNS(name) \
  406. static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
  407. { \
  408. stats->flags |= (1 << CFQG_stats_##name); \
  409. } \
  410. static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \
  411. { \
  412. stats->flags &= ~(1 << CFQG_stats_##name); \
  413. } \
  414. static inline int cfqg_stats_##name(struct cfqg_stats *stats) \
  415. { \
  416. return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
  417. } \
  418. CFQG_FLAG_FNS(waiting)
  419. CFQG_FLAG_FNS(idling)
  420. CFQG_FLAG_FNS(empty)
  421. #undef CFQG_FLAG_FNS
  422. /* This should be called with the queue_lock held. */
  423. static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats)
  424. {
  425. unsigned long long now;
  426. if (!cfqg_stats_waiting(stats))
  427. return;
  428. now = sched_clock();
  429. if (time_after64(now, stats->start_group_wait_time))
  430. blkg_stat_add(&stats->group_wait_time,
  431. now - stats->start_group_wait_time);
  432. cfqg_stats_clear_waiting(stats);
  433. }
  434. /* This should be called with the queue_lock held. */
  435. static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg,
  436. struct cfq_group *curr_cfqg)
  437. {
  438. struct cfqg_stats *stats = &cfqg->stats;
  439. if (cfqg_stats_waiting(stats))
  440. return;
  441. if (cfqg == curr_cfqg)
  442. return;
  443. stats->start_group_wait_time = sched_clock();
  444. cfqg_stats_mark_waiting(stats);
  445. }
  446. /* This should be called with the queue_lock held. */
  447. static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
  448. {
  449. unsigned long long now;
  450. if (!cfqg_stats_empty(stats))
  451. return;
  452. now = sched_clock();
  453. if (time_after64(now, stats->start_empty_time))
  454. blkg_stat_add(&stats->empty_time,
  455. now - stats->start_empty_time);
  456. cfqg_stats_clear_empty(stats);
  457. }
  458. static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
  459. {
  460. blkg_stat_add(&cfqg->stats.dequeue, 1);
  461. }
  462. static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
  463. {
  464. struct cfqg_stats *stats = &cfqg->stats;
  465. if (blkg_rwstat_total(&stats->queued))
  466. return;
  467. /*
  468. * group is already marked empty. This can happen if cfqq got new
  469. * request in parent group and moved to this group while being added
  470. * to service tree. Just ignore the event and move on.
  471. */
  472. if (cfqg_stats_empty(stats))
  473. return;
  474. stats->start_empty_time = sched_clock();
  475. cfqg_stats_mark_empty(stats);
  476. }
  477. static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
  478. {
  479. struct cfqg_stats *stats = &cfqg->stats;
  480. if (cfqg_stats_idling(stats)) {
  481. unsigned long long now = sched_clock();
  482. if (time_after64(now, stats->start_idle_time))
  483. blkg_stat_add(&stats->idle_time,
  484. now - stats->start_idle_time);
  485. cfqg_stats_clear_idling(stats);
  486. }
  487. }
  488. static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
  489. {
  490. struct cfqg_stats *stats = &cfqg->stats;
  491. BUG_ON(cfqg_stats_idling(stats));
  492. stats->start_idle_time = sched_clock();
  493. cfqg_stats_mark_idling(stats);
  494. }
  495. static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
  496. {
  497. struct cfqg_stats *stats = &cfqg->stats;
  498. blkg_stat_add(&stats->avg_queue_size_sum,
  499. blkg_rwstat_total(&stats->queued));
  500. blkg_stat_add(&stats->avg_queue_size_samples, 1);
  501. cfqg_stats_update_group_wait_time(stats);
  502. }
  503. #else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
  504. static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { }
  505. static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { }
  506. static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { }
  507. static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { }
  508. static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { }
  509. static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { }
  510. static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { }
  511. #endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
  512. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  513. static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
  514. {
  515. return pd ? container_of(pd, struct cfq_group, pd) : NULL;
  516. }
  517. static struct cfq_group_data
  518. *cpd_to_cfqgd(struct blkcg_policy_data *cpd)
  519. {
  520. return cpd ? container_of(cpd, struct cfq_group_data, cpd) : NULL;
  521. }
  522. static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
  523. {
  524. return pd_to_blkg(&cfqg->pd);
  525. }
  526. static struct blkcg_policy blkcg_policy_cfq;
  527. static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg)
  528. {
  529. return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq));
  530. }
  531. static struct cfq_group_data *blkcg_to_cfqgd(struct blkcg *blkcg)
  532. {
  533. return cpd_to_cfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_cfq));
  534. }
  535. static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg)
  536. {
  537. struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent;
  538. return pblkg ? blkg_to_cfqg(pblkg) : NULL;
  539. }
  540. static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
  541. struct cfq_group *ancestor)
  542. {
  543. return cgroup_is_descendant(cfqg_to_blkg(cfqg)->blkcg->css.cgroup,
  544. cfqg_to_blkg(ancestor)->blkcg->css.cgroup);
  545. }
  546. static inline void cfqg_get(struct cfq_group *cfqg)
  547. {
  548. return blkg_get(cfqg_to_blkg(cfqg));
  549. }
  550. static inline void cfqg_put(struct cfq_group *cfqg)
  551. {
  552. return blkg_put(cfqg_to_blkg(cfqg));
  553. }
  554. #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
  555. char __pbuf[128]; \
  556. \
  557. blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
  558. blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
  559. cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
  560. cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
  561. __pbuf, ##args); \
  562. } while (0)
  563. #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
  564. char __pbuf[128]; \
  565. \
  566. blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
  567. blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
  568. } while (0)
  569. static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
  570. struct cfq_group *curr_cfqg,
  571. unsigned int op)
  572. {
  573. blkg_rwstat_add(&cfqg->stats.queued, op, 1);
  574. cfqg_stats_end_empty_time(&cfqg->stats);
  575. cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
  576. }
  577. static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
  578. uint64_t time, unsigned long unaccounted_time)
  579. {
  580. blkg_stat_add(&cfqg->stats.time, time);
  581. #ifdef CONFIG_DEBUG_BLK_CGROUP
  582. blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
  583. #endif
  584. }
  585. static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
  586. unsigned int op)
  587. {
  588. blkg_rwstat_add(&cfqg->stats.queued, op, -1);
  589. }
  590. static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
  591. unsigned int op)
  592. {
  593. blkg_rwstat_add(&cfqg->stats.merged, op, 1);
  594. }
  595. static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
  596. uint64_t start_time, uint64_t io_start_time,
  597. unsigned int op)
  598. {
  599. struct cfqg_stats *stats = &cfqg->stats;
  600. unsigned long long now = sched_clock();
  601. if (time_after64(now, io_start_time))
  602. blkg_rwstat_add(&stats->service_time, op, now - io_start_time);
  603. if (time_after64(io_start_time, start_time))
  604. blkg_rwstat_add(&stats->wait_time, op,
  605. io_start_time - start_time);
  606. }
  607. /* @stats = 0 */
  608. static void cfqg_stats_reset(struct cfqg_stats *stats)
  609. {
  610. /* queued stats shouldn't be cleared */
  611. blkg_rwstat_reset(&stats->merged);
  612. blkg_rwstat_reset(&stats->service_time);
  613. blkg_rwstat_reset(&stats->wait_time);
  614. blkg_stat_reset(&stats->time);
  615. #ifdef CONFIG_DEBUG_BLK_CGROUP
  616. blkg_stat_reset(&stats->unaccounted_time);
  617. blkg_stat_reset(&stats->avg_queue_size_sum);
  618. blkg_stat_reset(&stats->avg_queue_size_samples);
  619. blkg_stat_reset(&stats->dequeue);
  620. blkg_stat_reset(&stats->group_wait_time);
  621. blkg_stat_reset(&stats->idle_time);
  622. blkg_stat_reset(&stats->empty_time);
  623. #endif
  624. }
  625. /* @to += @from */
  626. static void cfqg_stats_add_aux(struct cfqg_stats *to, struct cfqg_stats *from)
  627. {
  628. /* queued stats shouldn't be cleared */
  629. blkg_rwstat_add_aux(&to->merged, &from->merged);
  630. blkg_rwstat_add_aux(&to->service_time, &from->service_time);
  631. blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
  632. blkg_stat_add_aux(&from->time, &from->time);
  633. #ifdef CONFIG_DEBUG_BLK_CGROUP
  634. blkg_stat_add_aux(&to->unaccounted_time, &from->unaccounted_time);
  635. blkg_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
  636. blkg_stat_add_aux(&to->avg_queue_size_samples, &from->avg_queue_size_samples);
  637. blkg_stat_add_aux(&to->dequeue, &from->dequeue);
  638. blkg_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
  639. blkg_stat_add_aux(&to->idle_time, &from->idle_time);
  640. blkg_stat_add_aux(&to->empty_time, &from->empty_time);
  641. #endif
  642. }
  643. /*
  644. * Transfer @cfqg's stats to its parent's aux counts so that the ancestors'
  645. * recursive stats can still account for the amount used by this cfqg after
  646. * it's gone.
  647. */
  648. static void cfqg_stats_xfer_dead(struct cfq_group *cfqg)
  649. {
  650. struct cfq_group *parent = cfqg_parent(cfqg);
  651. lockdep_assert_held(cfqg_to_blkg(cfqg)->q->queue_lock);
  652. if (unlikely(!parent))
  653. return;
  654. cfqg_stats_add_aux(&parent->stats, &cfqg->stats);
  655. cfqg_stats_reset(&cfqg->stats);
  656. }
  657. #else /* CONFIG_CFQ_GROUP_IOSCHED */
  658. static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; }
  659. static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
  660. struct cfq_group *ancestor)
  661. {
  662. return true;
  663. }
  664. static inline void cfqg_get(struct cfq_group *cfqg) { }
  665. static inline void cfqg_put(struct cfq_group *cfqg) { }
  666. #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
  667. blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
  668. cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
  669. cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
  670. ##args)
  671. #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
  672. static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
  673. struct cfq_group *curr_cfqg, unsigned int op) { }
  674. static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
  675. uint64_t time, unsigned long unaccounted_time) { }
  676. static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
  677. unsigned int op) { }
  678. static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
  679. unsigned int op) { }
  680. static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
  681. uint64_t start_time, uint64_t io_start_time,
  682. unsigned int op) { }
  683. #endif /* CONFIG_CFQ_GROUP_IOSCHED */
  684. #define cfq_log(cfqd, fmt, args...) \
  685. blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
  686. /* Traverses through cfq group service trees */
  687. #define for_each_cfqg_st(cfqg, i, j, st) \
  688. for (i = 0; i <= IDLE_WORKLOAD; i++) \
  689. for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
  690. : &cfqg->service_tree_idle; \
  691. (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
  692. (i == IDLE_WORKLOAD && j == 0); \
  693. j++, st = i < IDLE_WORKLOAD ? \
  694. &cfqg->service_trees[i][j]: NULL) \
  695. static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
  696. struct cfq_ttime *ttime, bool group_idle)
  697. {
  698. u64 slice;
  699. if (!sample_valid(ttime->ttime_samples))
  700. return false;
  701. if (group_idle)
  702. slice = cfqd->cfq_group_idle;
  703. else
  704. slice = cfqd->cfq_slice_idle;
  705. return ttime->ttime_mean > slice;
  706. }
  707. static inline bool iops_mode(struct cfq_data *cfqd)
  708. {
  709. /*
  710. * If we are not idling on queues and it is a NCQ drive, parallel
  711. * execution of requests is on and measuring time is not possible
  712. * in most of the cases until and unless we drive shallower queue
  713. * depths and that becomes a performance bottleneck. In such cases
  714. * switch to start providing fairness in terms of number of IOs.
  715. */
  716. if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
  717. return true;
  718. else
  719. return false;
  720. }
  721. static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
  722. {
  723. if (cfq_class_idle(cfqq))
  724. return IDLE_WORKLOAD;
  725. if (cfq_class_rt(cfqq))
  726. return RT_WORKLOAD;
  727. return BE_WORKLOAD;
  728. }
  729. static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
  730. {
  731. if (!cfq_cfqq_sync(cfqq))
  732. return ASYNC_WORKLOAD;
  733. if (!cfq_cfqq_idle_window(cfqq))
  734. return SYNC_NOIDLE_WORKLOAD;
  735. return SYNC_WORKLOAD;
  736. }
  737. static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
  738. struct cfq_data *cfqd,
  739. struct cfq_group *cfqg)
  740. {
  741. if (wl_class == IDLE_WORKLOAD)
  742. return cfqg->service_tree_idle.count;
  743. return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
  744. cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
  745. cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
  746. }
  747. static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
  748. struct cfq_group *cfqg)
  749. {
  750. return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
  751. cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
  752. }
  753. static void cfq_dispatch_insert(struct request_queue *, struct request *);
  754. static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
  755. struct cfq_io_cq *cic, struct bio *bio);
  756. static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
  757. {
  758. /* cic->icq is the first member, %NULL will convert to %NULL */
  759. return container_of(icq, struct cfq_io_cq, icq);
  760. }
  761. static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
  762. struct io_context *ioc)
  763. {
  764. if (ioc)
  765. return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
  766. return NULL;
  767. }
  768. static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
  769. {
  770. return cic->cfqq[is_sync];
  771. }
  772. static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
  773. bool is_sync)
  774. {
  775. cic->cfqq[is_sync] = cfqq;
  776. }
  777. static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
  778. {
  779. return cic->icq.q->elevator->elevator_data;
  780. }
  781. /*
  782. * scheduler run of queue, if there are requests pending and no one in the
  783. * driver that will restart queueing
  784. */
  785. static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
  786. {
  787. if (cfqd->busy_queues) {
  788. cfq_log(cfqd, "schedule dispatch");
  789. kblockd_schedule_work(&cfqd->unplug_work);
  790. }
  791. }
  792. /*
  793. * Scale schedule slice based on io priority. Use the sync time slice only
  794. * if a queue is marked sync and has sync io queued. A sync queue with async
  795. * io only, should not get full sync slice length.
  796. */
  797. static inline u64 cfq_prio_slice(struct cfq_data *cfqd, bool sync,
  798. unsigned short prio)
  799. {
  800. u64 base_slice = cfqd->cfq_slice[sync];
  801. u64 slice = div_u64(base_slice, CFQ_SLICE_SCALE);
  802. WARN_ON(prio >= IOPRIO_BE_NR);
  803. return base_slice + (slice * (4 - prio));
  804. }
  805. static inline u64
  806. cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  807. {
  808. return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
  809. }
  810. /**
  811. * cfqg_scale_charge - scale disk time charge according to cfqg weight
  812. * @charge: disk time being charged
  813. * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
  814. *
  815. * Scale @charge according to @vfraction, which is in range (0, 1]. The
  816. * scaling is inversely proportional.
  817. *
  818. * scaled = charge / vfraction
  819. *
  820. * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
  821. */
  822. static inline u64 cfqg_scale_charge(u64 charge,
  823. unsigned int vfraction)
  824. {
  825. u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */
  826. /* charge / vfraction */
  827. c <<= CFQ_SERVICE_SHIFT;
  828. return div_u64(c, vfraction);
  829. }
  830. static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
  831. {
  832. s64 delta = (s64)(vdisktime - min_vdisktime);
  833. if (delta > 0)
  834. min_vdisktime = vdisktime;
  835. return min_vdisktime;
  836. }
  837. static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
  838. {
  839. s64 delta = (s64)(vdisktime - min_vdisktime);
  840. if (delta < 0)
  841. min_vdisktime = vdisktime;
  842. return min_vdisktime;
  843. }
  844. static void update_min_vdisktime(struct cfq_rb_root *st)
  845. {
  846. struct cfq_group *cfqg;
  847. if (st->left) {
  848. cfqg = rb_entry_cfqg(st->left);
  849. st->min_vdisktime = max_vdisktime(st->min_vdisktime,
  850. cfqg->vdisktime);
  851. }
  852. }
  853. /*
  854. * get averaged number of queues of RT/BE priority.
  855. * average is updated, with a formula that gives more weight to higher numbers,
  856. * to quickly follows sudden increases and decrease slowly
  857. */
  858. static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
  859. struct cfq_group *cfqg, bool rt)
  860. {
  861. unsigned min_q, max_q;
  862. unsigned mult = cfq_hist_divisor - 1;
  863. unsigned round = cfq_hist_divisor / 2;
  864. unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
  865. min_q = min(cfqg->busy_queues_avg[rt], busy);
  866. max_q = max(cfqg->busy_queues_avg[rt], busy);
  867. cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
  868. cfq_hist_divisor;
  869. return cfqg->busy_queues_avg[rt];
  870. }
  871. static inline u64
  872. cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
  873. {
  874. return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
  875. }
  876. static inline u64
  877. cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  878. {
  879. u64 slice = cfq_prio_to_slice(cfqd, cfqq);
  880. if (cfqd->cfq_latency) {
  881. /*
  882. * interested queues (we consider only the ones with the same
  883. * priority class in the cfq group)
  884. */
  885. unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
  886. cfq_class_rt(cfqq));
  887. u64 sync_slice = cfqd->cfq_slice[1];
  888. u64 expect_latency = sync_slice * iq;
  889. u64 group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
  890. if (expect_latency > group_slice) {
  891. u64 base_low_slice = 2 * cfqd->cfq_slice_idle;
  892. u64 low_slice;
  893. /* scale low_slice according to IO priority
  894. * and sync vs async */
  895. low_slice = div64_u64(base_low_slice*slice, sync_slice);
  896. low_slice = min(slice, low_slice);
  897. /* the adapted slice value is scaled to fit all iqs
  898. * into the target latency */
  899. slice = div64_u64(slice*group_slice, expect_latency);
  900. slice = max(slice, low_slice);
  901. }
  902. }
  903. return slice;
  904. }
  905. static inline void
  906. cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  907. {
  908. u64 slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
  909. u64 now = ktime_get_ns();
  910. cfqq->slice_start = now;
  911. cfqq->slice_end = now + slice;
  912. cfqq->allocated_slice = slice;
  913. cfq_log_cfqq(cfqd, cfqq, "set_slice=%llu", cfqq->slice_end - now);
  914. }
  915. /*
  916. * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
  917. * isn't valid until the first request from the dispatch is activated
  918. * and the slice time set.
  919. */
  920. static inline bool cfq_slice_used(struct cfq_queue *cfqq)
  921. {
  922. if (cfq_cfqq_slice_new(cfqq))
  923. return false;
  924. if (ktime_get_ns() < cfqq->slice_end)
  925. return false;
  926. return true;
  927. }
  928. /*
  929. * Lifted from AS - choose which of rq1 and rq2 that is best served now.
  930. * We choose the request that is closest to the head right now. Distance
  931. * behind the head is penalized and only allowed to a certain extent.
  932. */
  933. static struct request *
  934. cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
  935. {
  936. sector_t s1, s2, d1 = 0, d2 = 0;
  937. unsigned long back_max;
  938. #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
  939. #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
  940. unsigned wrap = 0; /* bit mask: requests behind the disk head? */
  941. if (rq1 == NULL || rq1 == rq2)
  942. return rq2;
  943. if (rq2 == NULL)
  944. return rq1;
  945. if (rq_is_sync(rq1) != rq_is_sync(rq2))
  946. return rq_is_sync(rq1) ? rq1 : rq2;
  947. if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
  948. return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
  949. s1 = blk_rq_pos(rq1);
  950. s2 = blk_rq_pos(rq2);
  951. /*
  952. * by definition, 1KiB is 2 sectors
  953. */
  954. back_max = cfqd->cfq_back_max * 2;
  955. /*
  956. * Strict one way elevator _except_ in the case where we allow
  957. * short backward seeks which are biased as twice the cost of a
  958. * similar forward seek.
  959. */
  960. if (s1 >= last)
  961. d1 = s1 - last;
  962. else if (s1 + back_max >= last)
  963. d1 = (last - s1) * cfqd->cfq_back_penalty;
  964. else
  965. wrap |= CFQ_RQ1_WRAP;
  966. if (s2 >= last)
  967. d2 = s2 - last;
  968. else if (s2 + back_max >= last)
  969. d2 = (last - s2) * cfqd->cfq_back_penalty;
  970. else
  971. wrap |= CFQ_RQ2_WRAP;
  972. /* Found required data */
  973. /*
  974. * By doing switch() on the bit mask "wrap" we avoid having to
  975. * check two variables for all permutations: --> faster!
  976. */
  977. switch (wrap) {
  978. case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
  979. if (d1 < d2)
  980. return rq1;
  981. else if (d2 < d1)
  982. return rq2;
  983. else {
  984. if (s1 >= s2)
  985. return rq1;
  986. else
  987. return rq2;
  988. }
  989. case CFQ_RQ2_WRAP:
  990. return rq1;
  991. case CFQ_RQ1_WRAP:
  992. return rq2;
  993. case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
  994. default:
  995. /*
  996. * Since both rqs are wrapped,
  997. * start with the one that's further behind head
  998. * (--> only *one* back seek required),
  999. * since back seek takes more time than forward.
  1000. */
  1001. if (s1 <= s2)
  1002. return rq1;
  1003. else
  1004. return rq2;
  1005. }
  1006. }
  1007. /*
  1008. * The below is leftmost cache rbtree addon
  1009. */
  1010. static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
  1011. {
  1012. /* Service tree is empty */
  1013. if (!root->count)
  1014. return NULL;
  1015. if (!root->left)
  1016. root->left = rb_first(&root->rb);
  1017. if (root->left)
  1018. return rb_entry(root->left, struct cfq_queue, rb_node);
  1019. return NULL;
  1020. }
  1021. static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
  1022. {
  1023. if (!root->left)
  1024. root->left = rb_first(&root->rb);
  1025. if (root->left)
  1026. return rb_entry_cfqg(root->left);
  1027. return NULL;
  1028. }
  1029. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  1030. {
  1031. rb_erase(n, root);
  1032. RB_CLEAR_NODE(n);
  1033. }
  1034. static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
  1035. {
  1036. if (root->left == n)
  1037. root->left = NULL;
  1038. rb_erase_init(n, &root->rb);
  1039. --root->count;
  1040. }
  1041. /*
  1042. * would be nice to take fifo expire time into account as well
  1043. */
  1044. static struct request *
  1045. cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1046. struct request *last)
  1047. {
  1048. struct rb_node *rbnext = rb_next(&last->rb_node);
  1049. struct rb_node *rbprev = rb_prev(&last->rb_node);
  1050. struct request *next = NULL, *prev = NULL;
  1051. BUG_ON(RB_EMPTY_NODE(&last->rb_node));
  1052. if (rbprev)
  1053. prev = rb_entry_rq(rbprev);
  1054. if (rbnext)
  1055. next = rb_entry_rq(rbnext);
  1056. else {
  1057. rbnext = rb_first(&cfqq->sort_list);
  1058. if (rbnext && rbnext != &last->rb_node)
  1059. next = rb_entry_rq(rbnext);
  1060. }
  1061. return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
  1062. }
  1063. static u64 cfq_slice_offset(struct cfq_data *cfqd,
  1064. struct cfq_queue *cfqq)
  1065. {
  1066. /*
  1067. * just an approximation, should be ok.
  1068. */
  1069. return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
  1070. cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
  1071. }
  1072. static inline s64
  1073. cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
  1074. {
  1075. return cfqg->vdisktime - st->min_vdisktime;
  1076. }
  1077. static void
  1078. __cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
  1079. {
  1080. struct rb_node **node = &st->rb.rb_node;
  1081. struct rb_node *parent = NULL;
  1082. struct cfq_group *__cfqg;
  1083. s64 key = cfqg_key(st, cfqg);
  1084. int left = 1;
  1085. while (*node != NULL) {
  1086. parent = *node;
  1087. __cfqg = rb_entry_cfqg(parent);
  1088. if (key < cfqg_key(st, __cfqg))
  1089. node = &parent->rb_left;
  1090. else {
  1091. node = &parent->rb_right;
  1092. left = 0;
  1093. }
  1094. }
  1095. if (left)
  1096. st->left = &cfqg->rb_node;
  1097. rb_link_node(&cfqg->rb_node, parent, node);
  1098. rb_insert_color(&cfqg->rb_node, &st->rb);
  1099. }
  1100. /*
  1101. * This has to be called only on activation of cfqg
  1102. */
  1103. static void
  1104. cfq_update_group_weight(struct cfq_group *cfqg)
  1105. {
  1106. if (cfqg->new_weight) {
  1107. cfqg->weight = cfqg->new_weight;
  1108. cfqg->new_weight = 0;
  1109. }
  1110. }
  1111. static void
  1112. cfq_update_group_leaf_weight(struct cfq_group *cfqg)
  1113. {
  1114. BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
  1115. if (cfqg->new_leaf_weight) {
  1116. cfqg->leaf_weight = cfqg->new_leaf_weight;
  1117. cfqg->new_leaf_weight = 0;
  1118. }
  1119. }
  1120. static void
  1121. cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
  1122. {
  1123. unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */
  1124. struct cfq_group *pos = cfqg;
  1125. struct cfq_group *parent;
  1126. bool propagate;
  1127. /* add to the service tree */
  1128. BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
  1129. /*
  1130. * Update leaf_weight. We cannot update weight at this point
  1131. * because cfqg might already have been activated and is
  1132. * contributing its current weight to the parent's child_weight.
  1133. */
  1134. cfq_update_group_leaf_weight(cfqg);
  1135. __cfq_group_service_tree_add(st, cfqg);
  1136. /*
  1137. * Activate @cfqg and calculate the portion of vfraction @cfqg is
  1138. * entitled to. vfraction is calculated by walking the tree
  1139. * towards the root calculating the fraction it has at each level.
  1140. * The compounded ratio is how much vfraction @cfqg owns.
  1141. *
  1142. * Start with the proportion tasks in this cfqg has against active
  1143. * children cfqgs - its leaf_weight against children_weight.
  1144. */
  1145. propagate = !pos->nr_active++;
  1146. pos->children_weight += pos->leaf_weight;
  1147. vfr = vfr * pos->leaf_weight / pos->children_weight;
  1148. /*
  1149. * Compound ->weight walking up the tree. Both activation and
  1150. * vfraction calculation are done in the same loop. Propagation
  1151. * stops once an already activated node is met. vfraction
  1152. * calculation should always continue to the root.
  1153. */
  1154. while ((parent = cfqg_parent(pos))) {
  1155. if (propagate) {
  1156. cfq_update_group_weight(pos);
  1157. propagate = !parent->nr_active++;
  1158. parent->children_weight += pos->weight;
  1159. }
  1160. vfr = vfr * pos->weight / parent->children_weight;
  1161. pos = parent;
  1162. }
  1163. cfqg->vfraction = max_t(unsigned, vfr, 1);
  1164. }
  1165. static void
  1166. cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
  1167. {
  1168. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  1169. struct cfq_group *__cfqg;
  1170. struct rb_node *n;
  1171. cfqg->nr_cfqq++;
  1172. if (!RB_EMPTY_NODE(&cfqg->rb_node))
  1173. return;
  1174. /*
  1175. * Currently put the group at the end. Later implement something
  1176. * so that groups get lesser vtime based on their weights, so that
  1177. * if group does not loose all if it was not continuously backlogged.
  1178. */
  1179. n = rb_last(&st->rb);
  1180. if (n) {
  1181. __cfqg = rb_entry_cfqg(n);
  1182. cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
  1183. } else
  1184. cfqg->vdisktime = st->min_vdisktime;
  1185. cfq_group_service_tree_add(st, cfqg);
  1186. }
  1187. static void
  1188. cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
  1189. {
  1190. struct cfq_group *pos = cfqg;
  1191. bool propagate;
  1192. /*
  1193. * Undo activation from cfq_group_service_tree_add(). Deactivate
  1194. * @cfqg and propagate deactivation upwards.
  1195. */
  1196. propagate = !--pos->nr_active;
  1197. pos->children_weight -= pos->leaf_weight;
  1198. while (propagate) {
  1199. struct cfq_group *parent = cfqg_parent(pos);
  1200. /* @pos has 0 nr_active at this point */
  1201. WARN_ON_ONCE(pos->children_weight);
  1202. pos->vfraction = 0;
  1203. if (!parent)
  1204. break;
  1205. propagate = !--parent->nr_active;
  1206. parent->children_weight -= pos->weight;
  1207. pos = parent;
  1208. }
  1209. /* remove from the service tree */
  1210. if (!RB_EMPTY_NODE(&cfqg->rb_node))
  1211. cfq_rb_erase(&cfqg->rb_node, st);
  1212. }
  1213. static void
  1214. cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
  1215. {
  1216. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  1217. BUG_ON(cfqg->nr_cfqq < 1);
  1218. cfqg->nr_cfqq--;
  1219. /* If there are other cfq queues under this group, don't delete it */
  1220. if (cfqg->nr_cfqq)
  1221. return;
  1222. cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
  1223. cfq_group_service_tree_del(st, cfqg);
  1224. cfqg->saved_wl_slice = 0;
  1225. cfqg_stats_update_dequeue(cfqg);
  1226. }
  1227. static inline u64 cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
  1228. u64 *unaccounted_time)
  1229. {
  1230. u64 slice_used;
  1231. u64 now = ktime_get_ns();
  1232. /*
  1233. * Queue got expired before even a single request completed or
  1234. * got expired immediately after first request completion.
  1235. */
  1236. if (!cfqq->slice_start || cfqq->slice_start == now) {
  1237. /*
  1238. * Also charge the seek time incurred to the group, otherwise
  1239. * if there are mutiple queues in the group, each can dispatch
  1240. * a single request on seeky media and cause lots of seek time
  1241. * and group will never know it.
  1242. */
  1243. slice_used = max_t(u64, (now - cfqq->dispatch_start),
  1244. jiffies_to_nsecs(1));
  1245. } else {
  1246. slice_used = now - cfqq->slice_start;
  1247. if (slice_used > cfqq->allocated_slice) {
  1248. *unaccounted_time = slice_used - cfqq->allocated_slice;
  1249. slice_used = cfqq->allocated_slice;
  1250. }
  1251. if (cfqq->slice_start > cfqq->dispatch_start)
  1252. *unaccounted_time += cfqq->slice_start -
  1253. cfqq->dispatch_start;
  1254. }
  1255. return slice_used;
  1256. }
  1257. static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
  1258. struct cfq_queue *cfqq)
  1259. {
  1260. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  1261. u64 used_sl, charge, unaccounted_sl = 0;
  1262. int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
  1263. - cfqg->service_tree_idle.count;
  1264. unsigned int vfr;
  1265. u64 now = ktime_get_ns();
  1266. BUG_ON(nr_sync < 0);
  1267. used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
  1268. if (iops_mode(cfqd))
  1269. charge = cfqq->slice_dispatch;
  1270. else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
  1271. charge = cfqq->allocated_slice;
  1272. /*
  1273. * Can't update vdisktime while on service tree and cfqg->vfraction
  1274. * is valid only while on it. Cache vfr, leave the service tree,
  1275. * update vdisktime and go back on. The re-addition to the tree
  1276. * will also update the weights as necessary.
  1277. */
  1278. vfr = cfqg->vfraction;
  1279. cfq_group_service_tree_del(st, cfqg);
  1280. cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
  1281. cfq_group_service_tree_add(st, cfqg);
  1282. /* This group is being expired. Save the context */
  1283. if (cfqd->workload_expires > now) {
  1284. cfqg->saved_wl_slice = cfqd->workload_expires - now;
  1285. cfqg->saved_wl_type = cfqd->serving_wl_type;
  1286. cfqg->saved_wl_class = cfqd->serving_wl_class;
  1287. } else
  1288. cfqg->saved_wl_slice = 0;
  1289. cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
  1290. st->min_vdisktime);
  1291. cfq_log_cfqq(cfqq->cfqd, cfqq,
  1292. "sl_used=%llu disp=%llu charge=%llu iops=%u sect=%lu",
  1293. used_sl, cfqq->slice_dispatch, charge,
  1294. iops_mode(cfqd), cfqq->nr_sectors);
  1295. cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
  1296. cfqg_stats_set_start_empty_time(cfqg);
  1297. }
  1298. /**
  1299. * cfq_init_cfqg_base - initialize base part of a cfq_group
  1300. * @cfqg: cfq_group to initialize
  1301. *
  1302. * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
  1303. * is enabled or not.
  1304. */
  1305. static void cfq_init_cfqg_base(struct cfq_group *cfqg)
  1306. {
  1307. struct cfq_rb_root *st;
  1308. int i, j;
  1309. for_each_cfqg_st(cfqg, i, j, st)
  1310. *st = CFQ_RB_ROOT;
  1311. RB_CLEAR_NODE(&cfqg->rb_node);
  1312. cfqg->ttime.last_end_request = ktime_get_ns();
  1313. }
  1314. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  1315. static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
  1316. bool on_dfl, bool reset_dev, bool is_leaf_weight);
  1317. static void cfqg_stats_exit(struct cfqg_stats *stats)
  1318. {
  1319. blkg_rwstat_exit(&stats->merged);
  1320. blkg_rwstat_exit(&stats->service_time);
  1321. blkg_rwstat_exit(&stats->wait_time);
  1322. blkg_rwstat_exit(&stats->queued);
  1323. blkg_stat_exit(&stats->time);
  1324. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1325. blkg_stat_exit(&stats->unaccounted_time);
  1326. blkg_stat_exit(&stats->avg_queue_size_sum);
  1327. blkg_stat_exit(&stats->avg_queue_size_samples);
  1328. blkg_stat_exit(&stats->dequeue);
  1329. blkg_stat_exit(&stats->group_wait_time);
  1330. blkg_stat_exit(&stats->idle_time);
  1331. blkg_stat_exit(&stats->empty_time);
  1332. #endif
  1333. }
  1334. static int cfqg_stats_init(struct cfqg_stats *stats, gfp_t gfp)
  1335. {
  1336. if (blkg_rwstat_init(&stats->merged, gfp) ||
  1337. blkg_rwstat_init(&stats->service_time, gfp) ||
  1338. blkg_rwstat_init(&stats->wait_time, gfp) ||
  1339. blkg_rwstat_init(&stats->queued, gfp) ||
  1340. blkg_stat_init(&stats->time, gfp))
  1341. goto err;
  1342. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1343. if (blkg_stat_init(&stats->unaccounted_time, gfp) ||
  1344. blkg_stat_init(&stats->avg_queue_size_sum, gfp) ||
  1345. blkg_stat_init(&stats->avg_queue_size_samples, gfp) ||
  1346. blkg_stat_init(&stats->dequeue, gfp) ||
  1347. blkg_stat_init(&stats->group_wait_time, gfp) ||
  1348. blkg_stat_init(&stats->idle_time, gfp) ||
  1349. blkg_stat_init(&stats->empty_time, gfp))
  1350. goto err;
  1351. #endif
  1352. return 0;
  1353. err:
  1354. cfqg_stats_exit(stats);
  1355. return -ENOMEM;
  1356. }
  1357. static struct blkcg_policy_data *cfq_cpd_alloc(gfp_t gfp)
  1358. {
  1359. struct cfq_group_data *cgd;
  1360. cgd = kzalloc(sizeof(*cgd), gfp);
  1361. if (!cgd)
  1362. return NULL;
  1363. return &cgd->cpd;
  1364. }
  1365. static void cfq_cpd_init(struct blkcg_policy_data *cpd)
  1366. {
  1367. struct cfq_group_data *cgd = cpd_to_cfqgd(cpd);
  1368. unsigned int weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
  1369. CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
  1370. if (cpd_to_blkcg(cpd) == &blkcg_root)
  1371. weight *= 2;
  1372. cgd->weight = weight;
  1373. cgd->leaf_weight = weight;
  1374. }
  1375. static void cfq_cpd_free(struct blkcg_policy_data *cpd)
  1376. {
  1377. kfree(cpd_to_cfqgd(cpd));
  1378. }
  1379. static void cfq_cpd_bind(struct blkcg_policy_data *cpd)
  1380. {
  1381. struct blkcg *blkcg = cpd_to_blkcg(cpd);
  1382. bool on_dfl = cgroup_subsys_on_dfl(io_cgrp_subsys);
  1383. unsigned int weight = on_dfl ? CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
  1384. if (blkcg == &blkcg_root)
  1385. weight *= 2;
  1386. WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, false));
  1387. WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, true));
  1388. }
  1389. static struct blkg_policy_data *cfq_pd_alloc(gfp_t gfp, int node)
  1390. {
  1391. struct cfq_group *cfqg;
  1392. cfqg = kzalloc_node(sizeof(*cfqg), gfp, node);
  1393. if (!cfqg)
  1394. return NULL;
  1395. cfq_init_cfqg_base(cfqg);
  1396. if (cfqg_stats_init(&cfqg->stats, gfp)) {
  1397. kfree(cfqg);
  1398. return NULL;
  1399. }
  1400. return &cfqg->pd;
  1401. }
  1402. static void cfq_pd_init(struct blkg_policy_data *pd)
  1403. {
  1404. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1405. struct cfq_group_data *cgd = blkcg_to_cfqgd(pd->blkg->blkcg);
  1406. cfqg->weight = cgd->weight;
  1407. cfqg->leaf_weight = cgd->leaf_weight;
  1408. }
  1409. static void cfq_pd_offline(struct blkg_policy_data *pd)
  1410. {
  1411. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1412. int i;
  1413. for (i = 0; i < IOPRIO_BE_NR; i++) {
  1414. if (cfqg->async_cfqq[0][i])
  1415. cfq_put_queue(cfqg->async_cfqq[0][i]);
  1416. if (cfqg->async_cfqq[1][i])
  1417. cfq_put_queue(cfqg->async_cfqq[1][i]);
  1418. }
  1419. if (cfqg->async_idle_cfqq)
  1420. cfq_put_queue(cfqg->async_idle_cfqq);
  1421. /*
  1422. * @blkg is going offline and will be ignored by
  1423. * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so
  1424. * that they don't get lost. If IOs complete after this point, the
  1425. * stats for them will be lost. Oh well...
  1426. */
  1427. cfqg_stats_xfer_dead(cfqg);
  1428. }
  1429. static void cfq_pd_free(struct blkg_policy_data *pd)
  1430. {
  1431. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1432. cfqg_stats_exit(&cfqg->stats);
  1433. return kfree(cfqg);
  1434. }
  1435. static void cfq_pd_reset_stats(struct blkg_policy_data *pd)
  1436. {
  1437. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1438. cfqg_stats_reset(&cfqg->stats);
  1439. }
  1440. static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
  1441. struct blkcg *blkcg)
  1442. {
  1443. struct blkcg_gq *blkg;
  1444. blkg = blkg_lookup(blkcg, cfqd->queue);
  1445. if (likely(blkg))
  1446. return blkg_to_cfqg(blkg);
  1447. return NULL;
  1448. }
  1449. static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
  1450. {
  1451. cfqq->cfqg = cfqg;
  1452. /* cfqq reference on cfqg */
  1453. cfqg_get(cfqg);
  1454. }
  1455. static u64 cfqg_prfill_weight_device(struct seq_file *sf,
  1456. struct blkg_policy_data *pd, int off)
  1457. {
  1458. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1459. if (!cfqg->dev_weight)
  1460. return 0;
  1461. return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
  1462. }
  1463. static int cfqg_print_weight_device(struct seq_file *sf, void *v)
  1464. {
  1465. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1466. cfqg_prfill_weight_device, &blkcg_policy_cfq,
  1467. 0, false);
  1468. return 0;
  1469. }
  1470. static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
  1471. struct blkg_policy_data *pd, int off)
  1472. {
  1473. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1474. if (!cfqg->dev_leaf_weight)
  1475. return 0;
  1476. return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
  1477. }
  1478. static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v)
  1479. {
  1480. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1481. cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq,
  1482. 0, false);
  1483. return 0;
  1484. }
  1485. static int cfq_print_weight(struct seq_file *sf, void *v)
  1486. {
  1487. struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
  1488. struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
  1489. unsigned int val = 0;
  1490. if (cgd)
  1491. val = cgd->weight;
  1492. seq_printf(sf, "%u\n", val);
  1493. return 0;
  1494. }
  1495. static int cfq_print_leaf_weight(struct seq_file *sf, void *v)
  1496. {
  1497. struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
  1498. struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
  1499. unsigned int val = 0;
  1500. if (cgd)
  1501. val = cgd->leaf_weight;
  1502. seq_printf(sf, "%u\n", val);
  1503. return 0;
  1504. }
  1505. static ssize_t __cfqg_set_weight_device(struct kernfs_open_file *of,
  1506. char *buf, size_t nbytes, loff_t off,
  1507. bool on_dfl, bool is_leaf_weight)
  1508. {
  1509. unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
  1510. unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
  1511. struct blkcg *blkcg = css_to_blkcg(of_css(of));
  1512. struct blkg_conf_ctx ctx;
  1513. struct cfq_group *cfqg;
  1514. struct cfq_group_data *cfqgd;
  1515. int ret;
  1516. u64 v;
  1517. ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
  1518. if (ret)
  1519. return ret;
  1520. if (sscanf(ctx.body, "%llu", &v) == 1) {
  1521. /* require "default" on dfl */
  1522. ret = -ERANGE;
  1523. if (!v && on_dfl)
  1524. goto out_finish;
  1525. } else if (!strcmp(strim(ctx.body), "default")) {
  1526. v = 0;
  1527. } else {
  1528. ret = -EINVAL;
  1529. goto out_finish;
  1530. }
  1531. cfqg = blkg_to_cfqg(ctx.blkg);
  1532. cfqgd = blkcg_to_cfqgd(blkcg);
  1533. ret = -ERANGE;
  1534. if (!v || (v >= min && v <= max)) {
  1535. if (!is_leaf_weight) {
  1536. cfqg->dev_weight = v;
  1537. cfqg->new_weight = v ?: cfqgd->weight;
  1538. } else {
  1539. cfqg->dev_leaf_weight = v;
  1540. cfqg->new_leaf_weight = v ?: cfqgd->leaf_weight;
  1541. }
  1542. ret = 0;
  1543. }
  1544. out_finish:
  1545. blkg_conf_finish(&ctx);
  1546. return ret ?: nbytes;
  1547. }
  1548. static ssize_t cfqg_set_weight_device(struct kernfs_open_file *of,
  1549. char *buf, size_t nbytes, loff_t off)
  1550. {
  1551. return __cfqg_set_weight_device(of, buf, nbytes, off, false, false);
  1552. }
  1553. static ssize_t cfqg_set_leaf_weight_device(struct kernfs_open_file *of,
  1554. char *buf, size_t nbytes, loff_t off)
  1555. {
  1556. return __cfqg_set_weight_device(of, buf, nbytes, off, false, true);
  1557. }
  1558. static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
  1559. bool on_dfl, bool reset_dev, bool is_leaf_weight)
  1560. {
  1561. unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
  1562. unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
  1563. struct blkcg *blkcg = css_to_blkcg(css);
  1564. struct blkcg_gq *blkg;
  1565. struct cfq_group_data *cfqgd;
  1566. int ret = 0;
  1567. if (val < min || val > max)
  1568. return -ERANGE;
  1569. spin_lock_irq(&blkcg->lock);
  1570. cfqgd = blkcg_to_cfqgd(blkcg);
  1571. if (!cfqgd) {
  1572. ret = -EINVAL;
  1573. goto out;
  1574. }
  1575. if (!is_leaf_weight)
  1576. cfqgd->weight = val;
  1577. else
  1578. cfqgd->leaf_weight = val;
  1579. hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
  1580. struct cfq_group *cfqg = blkg_to_cfqg(blkg);
  1581. if (!cfqg)
  1582. continue;
  1583. if (!is_leaf_weight) {
  1584. if (reset_dev)
  1585. cfqg->dev_weight = 0;
  1586. if (!cfqg->dev_weight)
  1587. cfqg->new_weight = cfqgd->weight;
  1588. } else {
  1589. if (reset_dev)
  1590. cfqg->dev_leaf_weight = 0;
  1591. if (!cfqg->dev_leaf_weight)
  1592. cfqg->new_leaf_weight = cfqgd->leaf_weight;
  1593. }
  1594. }
  1595. out:
  1596. spin_unlock_irq(&blkcg->lock);
  1597. return ret;
  1598. }
  1599. static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
  1600. u64 val)
  1601. {
  1602. return __cfq_set_weight(css, val, false, false, false);
  1603. }
  1604. static int cfq_set_leaf_weight(struct cgroup_subsys_state *css,
  1605. struct cftype *cft, u64 val)
  1606. {
  1607. return __cfq_set_weight(css, val, false, false, true);
  1608. }
  1609. static int cfqg_print_stat(struct seq_file *sf, void *v)
  1610. {
  1611. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
  1612. &blkcg_policy_cfq, seq_cft(sf)->private, false);
  1613. return 0;
  1614. }
  1615. static int cfqg_print_rwstat(struct seq_file *sf, void *v)
  1616. {
  1617. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
  1618. &blkcg_policy_cfq, seq_cft(sf)->private, true);
  1619. return 0;
  1620. }
  1621. static u64 cfqg_prfill_stat_recursive(struct seq_file *sf,
  1622. struct blkg_policy_data *pd, int off)
  1623. {
  1624. u64 sum = blkg_stat_recursive_sum(pd_to_blkg(pd),
  1625. &blkcg_policy_cfq, off);
  1626. return __blkg_prfill_u64(sf, pd, sum);
  1627. }
  1628. static u64 cfqg_prfill_rwstat_recursive(struct seq_file *sf,
  1629. struct blkg_policy_data *pd, int off)
  1630. {
  1631. struct blkg_rwstat sum = blkg_rwstat_recursive_sum(pd_to_blkg(pd),
  1632. &blkcg_policy_cfq, off);
  1633. return __blkg_prfill_rwstat(sf, pd, &sum);
  1634. }
  1635. static int cfqg_print_stat_recursive(struct seq_file *sf, void *v)
  1636. {
  1637. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1638. cfqg_prfill_stat_recursive, &blkcg_policy_cfq,
  1639. seq_cft(sf)->private, false);
  1640. return 0;
  1641. }
  1642. static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
  1643. {
  1644. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1645. cfqg_prfill_rwstat_recursive, &blkcg_policy_cfq,
  1646. seq_cft(sf)->private, true);
  1647. return 0;
  1648. }
  1649. static u64 cfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
  1650. int off)
  1651. {
  1652. u64 sum = blkg_rwstat_total(&pd->blkg->stat_bytes);
  1653. return __blkg_prfill_u64(sf, pd, sum >> 9);
  1654. }
  1655. static int cfqg_print_stat_sectors(struct seq_file *sf, void *v)
  1656. {
  1657. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1658. cfqg_prfill_sectors, &blkcg_policy_cfq, 0, false);
  1659. return 0;
  1660. }
  1661. static u64 cfqg_prfill_sectors_recursive(struct seq_file *sf,
  1662. struct blkg_policy_data *pd, int off)
  1663. {
  1664. struct blkg_rwstat tmp = blkg_rwstat_recursive_sum(pd->blkg, NULL,
  1665. offsetof(struct blkcg_gq, stat_bytes));
  1666. u64 sum = atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) +
  1667. atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]);
  1668. return __blkg_prfill_u64(sf, pd, sum >> 9);
  1669. }
  1670. static int cfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
  1671. {
  1672. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1673. cfqg_prfill_sectors_recursive, &blkcg_policy_cfq, 0,
  1674. false);
  1675. return 0;
  1676. }
  1677. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1678. static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
  1679. struct blkg_policy_data *pd, int off)
  1680. {
  1681. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1682. u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
  1683. u64 v = 0;
  1684. if (samples) {
  1685. v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
  1686. v = div64_u64(v, samples);
  1687. }
  1688. __blkg_prfill_u64(sf, pd, v);
  1689. return 0;
  1690. }
  1691. /* print avg_queue_size */
  1692. static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v)
  1693. {
  1694. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1695. cfqg_prfill_avg_queue_size, &blkcg_policy_cfq,
  1696. 0, false);
  1697. return 0;
  1698. }
  1699. #endif /* CONFIG_DEBUG_BLK_CGROUP */
  1700. static struct cftype cfq_blkcg_legacy_files[] = {
  1701. /* on root, weight is mapped to leaf_weight */
  1702. {
  1703. .name = "weight_device",
  1704. .flags = CFTYPE_ONLY_ON_ROOT,
  1705. .seq_show = cfqg_print_leaf_weight_device,
  1706. .write = cfqg_set_leaf_weight_device,
  1707. },
  1708. {
  1709. .name = "weight",
  1710. .flags = CFTYPE_ONLY_ON_ROOT,
  1711. .seq_show = cfq_print_leaf_weight,
  1712. .write_u64 = cfq_set_leaf_weight,
  1713. },
  1714. /* no such mapping necessary for !roots */
  1715. {
  1716. .name = "weight_device",
  1717. .flags = CFTYPE_NOT_ON_ROOT,
  1718. .seq_show = cfqg_print_weight_device,
  1719. .write = cfqg_set_weight_device,
  1720. },
  1721. {
  1722. .name = "weight",
  1723. .flags = CFTYPE_NOT_ON_ROOT,
  1724. .seq_show = cfq_print_weight,
  1725. .write_u64 = cfq_set_weight,
  1726. },
  1727. {
  1728. .name = "leaf_weight_device",
  1729. .seq_show = cfqg_print_leaf_weight_device,
  1730. .write = cfqg_set_leaf_weight_device,
  1731. },
  1732. {
  1733. .name = "leaf_weight",
  1734. .seq_show = cfq_print_leaf_weight,
  1735. .write_u64 = cfq_set_leaf_weight,
  1736. },
  1737. /* statistics, covers only the tasks in the cfqg */
  1738. {
  1739. .name = "time",
  1740. .private = offsetof(struct cfq_group, stats.time),
  1741. .seq_show = cfqg_print_stat,
  1742. },
  1743. {
  1744. .name = "sectors",
  1745. .seq_show = cfqg_print_stat_sectors,
  1746. },
  1747. {
  1748. .name = "io_service_bytes",
  1749. .private = (unsigned long)&blkcg_policy_cfq,
  1750. .seq_show = blkg_print_stat_bytes,
  1751. },
  1752. {
  1753. .name = "io_serviced",
  1754. .private = (unsigned long)&blkcg_policy_cfq,
  1755. .seq_show = blkg_print_stat_ios,
  1756. },
  1757. {
  1758. .name = "io_service_time",
  1759. .private = offsetof(struct cfq_group, stats.service_time),
  1760. .seq_show = cfqg_print_rwstat,
  1761. },
  1762. {
  1763. .name = "io_wait_time",
  1764. .private = offsetof(struct cfq_group, stats.wait_time),
  1765. .seq_show = cfqg_print_rwstat,
  1766. },
  1767. {
  1768. .name = "io_merged",
  1769. .private = offsetof(struct cfq_group, stats.merged),
  1770. .seq_show = cfqg_print_rwstat,
  1771. },
  1772. {
  1773. .name = "io_queued",
  1774. .private = offsetof(struct cfq_group, stats.queued),
  1775. .seq_show = cfqg_print_rwstat,
  1776. },
  1777. /* the same statictics which cover the cfqg and its descendants */
  1778. {
  1779. .name = "time_recursive",
  1780. .private = offsetof(struct cfq_group, stats.time),
  1781. .seq_show = cfqg_print_stat_recursive,
  1782. },
  1783. {
  1784. .name = "sectors_recursive",
  1785. .seq_show = cfqg_print_stat_sectors_recursive,
  1786. },
  1787. {
  1788. .name = "io_service_bytes_recursive",
  1789. .private = (unsigned long)&blkcg_policy_cfq,
  1790. .seq_show = blkg_print_stat_bytes_recursive,
  1791. },
  1792. {
  1793. .name = "io_serviced_recursive",
  1794. .private = (unsigned long)&blkcg_policy_cfq,
  1795. .seq_show = blkg_print_stat_ios_recursive,
  1796. },
  1797. {
  1798. .name = "io_service_time_recursive",
  1799. .private = offsetof(struct cfq_group, stats.service_time),
  1800. .seq_show = cfqg_print_rwstat_recursive,
  1801. },
  1802. {
  1803. .name = "io_wait_time_recursive",
  1804. .private = offsetof(struct cfq_group, stats.wait_time),
  1805. .seq_show = cfqg_print_rwstat_recursive,
  1806. },
  1807. {
  1808. .name = "io_merged_recursive",
  1809. .private = offsetof(struct cfq_group, stats.merged),
  1810. .seq_show = cfqg_print_rwstat_recursive,
  1811. },
  1812. {
  1813. .name = "io_queued_recursive",
  1814. .private = offsetof(struct cfq_group, stats.queued),
  1815. .seq_show = cfqg_print_rwstat_recursive,
  1816. },
  1817. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1818. {
  1819. .name = "avg_queue_size",
  1820. .seq_show = cfqg_print_avg_queue_size,
  1821. },
  1822. {
  1823. .name = "group_wait_time",
  1824. .private = offsetof(struct cfq_group, stats.group_wait_time),
  1825. .seq_show = cfqg_print_stat,
  1826. },
  1827. {
  1828. .name = "idle_time",
  1829. .private = offsetof(struct cfq_group, stats.idle_time),
  1830. .seq_show = cfqg_print_stat,
  1831. },
  1832. {
  1833. .name = "empty_time",
  1834. .private = offsetof(struct cfq_group, stats.empty_time),
  1835. .seq_show = cfqg_print_stat,
  1836. },
  1837. {
  1838. .name = "dequeue",
  1839. .private = offsetof(struct cfq_group, stats.dequeue),
  1840. .seq_show = cfqg_print_stat,
  1841. },
  1842. {
  1843. .name = "unaccounted_time",
  1844. .private = offsetof(struct cfq_group, stats.unaccounted_time),
  1845. .seq_show = cfqg_print_stat,
  1846. },
  1847. #endif /* CONFIG_DEBUG_BLK_CGROUP */
  1848. { } /* terminate */
  1849. };
  1850. static int cfq_print_weight_on_dfl(struct seq_file *sf, void *v)
  1851. {
  1852. struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
  1853. struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
  1854. seq_printf(sf, "default %u\n", cgd->weight);
  1855. blkcg_print_blkgs(sf, blkcg, cfqg_prfill_weight_device,
  1856. &blkcg_policy_cfq, 0, false);
  1857. return 0;
  1858. }
  1859. static ssize_t cfq_set_weight_on_dfl(struct kernfs_open_file *of,
  1860. char *buf, size_t nbytes, loff_t off)
  1861. {
  1862. char *endp;
  1863. int ret;
  1864. u64 v;
  1865. buf = strim(buf);
  1866. /* "WEIGHT" or "default WEIGHT" sets the default weight */
  1867. v = simple_strtoull(buf, &endp, 0);
  1868. if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) {
  1869. ret = __cfq_set_weight(of_css(of), v, true, false, false);
  1870. return ret ?: nbytes;
  1871. }
  1872. /* "MAJ:MIN WEIGHT" */
  1873. return __cfqg_set_weight_device(of, buf, nbytes, off, true, false);
  1874. }
  1875. static struct cftype cfq_blkcg_files[] = {
  1876. {
  1877. .name = "weight",
  1878. .flags = CFTYPE_NOT_ON_ROOT,
  1879. .seq_show = cfq_print_weight_on_dfl,
  1880. .write = cfq_set_weight_on_dfl,
  1881. },
  1882. { } /* terminate */
  1883. };
  1884. #else /* GROUP_IOSCHED */
  1885. static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
  1886. struct blkcg *blkcg)
  1887. {
  1888. return cfqd->root_group;
  1889. }
  1890. static inline void
  1891. cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
  1892. cfqq->cfqg = cfqg;
  1893. }
  1894. #endif /* GROUP_IOSCHED */
  1895. /*
  1896. * The cfqd->service_trees holds all pending cfq_queue's that have
  1897. * requests waiting to be processed. It is sorted in the order that
  1898. * we will service the queues.
  1899. */
  1900. static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1901. bool add_front)
  1902. {
  1903. struct rb_node **p, *parent;
  1904. struct cfq_queue *__cfqq;
  1905. u64 rb_key;
  1906. struct cfq_rb_root *st;
  1907. int left;
  1908. int new_cfqq = 1;
  1909. u64 now = ktime_get_ns();
  1910. st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
  1911. if (cfq_class_idle(cfqq)) {
  1912. rb_key = CFQ_IDLE_DELAY;
  1913. parent = rb_last(&st->rb);
  1914. if (parent && parent != &cfqq->rb_node) {
  1915. __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
  1916. rb_key += __cfqq->rb_key;
  1917. } else
  1918. rb_key += now;
  1919. } else if (!add_front) {
  1920. /*
  1921. * Get our rb key offset. Subtract any residual slice
  1922. * value carried from last service. A negative resid
  1923. * count indicates slice overrun, and this should position
  1924. * the next service time further away in the tree.
  1925. */
  1926. rb_key = cfq_slice_offset(cfqd, cfqq) + now;
  1927. rb_key -= cfqq->slice_resid;
  1928. cfqq->slice_resid = 0;
  1929. } else {
  1930. rb_key = -NSEC_PER_SEC;
  1931. __cfqq = cfq_rb_first(st);
  1932. rb_key += __cfqq ? __cfqq->rb_key : now;
  1933. }
  1934. if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
  1935. new_cfqq = 0;
  1936. /*
  1937. * same position, nothing more to do
  1938. */
  1939. if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
  1940. return;
  1941. cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
  1942. cfqq->service_tree = NULL;
  1943. }
  1944. left = 1;
  1945. parent = NULL;
  1946. cfqq->service_tree = st;
  1947. p = &st->rb.rb_node;
  1948. while (*p) {
  1949. parent = *p;
  1950. __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
  1951. /*
  1952. * sort by key, that represents service time.
  1953. */
  1954. if (rb_key < __cfqq->rb_key)
  1955. p = &parent->rb_left;
  1956. else {
  1957. p = &parent->rb_right;
  1958. left = 0;
  1959. }
  1960. }
  1961. if (left)
  1962. st->left = &cfqq->rb_node;
  1963. cfqq->rb_key = rb_key;
  1964. rb_link_node(&cfqq->rb_node, parent, p);
  1965. rb_insert_color(&cfqq->rb_node, &st->rb);
  1966. st->count++;
  1967. if (add_front || !new_cfqq)
  1968. return;
  1969. cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
  1970. }
  1971. static struct cfq_queue *
  1972. cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
  1973. sector_t sector, struct rb_node **ret_parent,
  1974. struct rb_node ***rb_link)
  1975. {
  1976. struct rb_node **p, *parent;
  1977. struct cfq_queue *cfqq = NULL;
  1978. parent = NULL;
  1979. p = &root->rb_node;
  1980. while (*p) {
  1981. struct rb_node **n;
  1982. parent = *p;
  1983. cfqq = rb_entry(parent, struct cfq_queue, p_node);
  1984. /*
  1985. * Sort strictly based on sector. Smallest to the left,
  1986. * largest to the right.
  1987. */
  1988. if (sector > blk_rq_pos(cfqq->next_rq))
  1989. n = &(*p)->rb_right;
  1990. else if (sector < blk_rq_pos(cfqq->next_rq))
  1991. n = &(*p)->rb_left;
  1992. else
  1993. break;
  1994. p = n;
  1995. cfqq = NULL;
  1996. }
  1997. *ret_parent = parent;
  1998. if (rb_link)
  1999. *rb_link = p;
  2000. return cfqq;
  2001. }
  2002. static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2003. {
  2004. struct rb_node **p, *parent;
  2005. struct cfq_queue *__cfqq;
  2006. if (cfqq->p_root) {
  2007. rb_erase(&cfqq->p_node, cfqq->p_root);
  2008. cfqq->p_root = NULL;
  2009. }
  2010. if (cfq_class_idle(cfqq))
  2011. return;
  2012. if (!cfqq->next_rq)
  2013. return;
  2014. cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
  2015. __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
  2016. blk_rq_pos(cfqq->next_rq), &parent, &p);
  2017. if (!__cfqq) {
  2018. rb_link_node(&cfqq->p_node, parent, p);
  2019. rb_insert_color(&cfqq->p_node, cfqq->p_root);
  2020. } else
  2021. cfqq->p_root = NULL;
  2022. }
  2023. /*
  2024. * Update cfqq's position in the service tree.
  2025. */
  2026. static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2027. {
  2028. /*
  2029. * Resorting requires the cfqq to be on the RR list already.
  2030. */
  2031. if (cfq_cfqq_on_rr(cfqq)) {
  2032. cfq_service_tree_add(cfqd, cfqq, 0);
  2033. cfq_prio_tree_add(cfqd, cfqq);
  2034. }
  2035. }
  2036. /*
  2037. * add to busy list of queues for service, trying to be fair in ordering
  2038. * the pending list according to last request service
  2039. */
  2040. static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2041. {
  2042. cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
  2043. BUG_ON(cfq_cfqq_on_rr(cfqq));
  2044. cfq_mark_cfqq_on_rr(cfqq);
  2045. cfqd->busy_queues++;
  2046. if (cfq_cfqq_sync(cfqq))
  2047. cfqd->busy_sync_queues++;
  2048. cfq_resort_rr_list(cfqd, cfqq);
  2049. }
  2050. /*
  2051. * Called when the cfqq no longer has requests pending, remove it from
  2052. * the service tree.
  2053. */
  2054. static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2055. {
  2056. cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
  2057. BUG_ON(!cfq_cfqq_on_rr(cfqq));
  2058. cfq_clear_cfqq_on_rr(cfqq);
  2059. if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
  2060. cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
  2061. cfqq->service_tree = NULL;
  2062. }
  2063. if (cfqq->p_root) {
  2064. rb_erase(&cfqq->p_node, cfqq->p_root);
  2065. cfqq->p_root = NULL;
  2066. }
  2067. cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
  2068. BUG_ON(!cfqd->busy_queues);
  2069. cfqd->busy_queues--;
  2070. if (cfq_cfqq_sync(cfqq))
  2071. cfqd->busy_sync_queues--;
  2072. }
  2073. /*
  2074. * rb tree support functions
  2075. */
  2076. static void cfq_del_rq_rb(struct request *rq)
  2077. {
  2078. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2079. const int sync = rq_is_sync(rq);
  2080. BUG_ON(!cfqq->queued[sync]);
  2081. cfqq->queued[sync]--;
  2082. elv_rb_del(&cfqq->sort_list, rq);
  2083. if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
  2084. /*
  2085. * Queue will be deleted from service tree when we actually
  2086. * expire it later. Right now just remove it from prio tree
  2087. * as it is empty.
  2088. */
  2089. if (cfqq->p_root) {
  2090. rb_erase(&cfqq->p_node, cfqq->p_root);
  2091. cfqq->p_root = NULL;
  2092. }
  2093. }
  2094. }
  2095. static void cfq_add_rq_rb(struct request *rq)
  2096. {
  2097. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2098. struct cfq_data *cfqd = cfqq->cfqd;
  2099. struct request *prev;
  2100. cfqq->queued[rq_is_sync(rq)]++;
  2101. elv_rb_add(&cfqq->sort_list, rq);
  2102. if (!cfq_cfqq_on_rr(cfqq))
  2103. cfq_add_cfqq_rr(cfqd, cfqq);
  2104. /*
  2105. * check if this request is a better next-serve candidate
  2106. */
  2107. prev = cfqq->next_rq;
  2108. cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
  2109. /*
  2110. * adjust priority tree position, if ->next_rq changes
  2111. */
  2112. if (prev != cfqq->next_rq)
  2113. cfq_prio_tree_add(cfqd, cfqq);
  2114. BUG_ON(!cfqq->next_rq);
  2115. }
  2116. static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
  2117. {
  2118. elv_rb_del(&cfqq->sort_list, rq);
  2119. cfqq->queued[rq_is_sync(rq)]--;
  2120. cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
  2121. cfq_add_rq_rb(rq);
  2122. cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
  2123. rq->cmd_flags);
  2124. }
  2125. static struct request *
  2126. cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
  2127. {
  2128. struct task_struct *tsk = current;
  2129. struct cfq_io_cq *cic;
  2130. struct cfq_queue *cfqq;
  2131. cic = cfq_cic_lookup(cfqd, tsk->io_context);
  2132. if (!cic)
  2133. return NULL;
  2134. cfqq = cic_to_cfqq(cic, op_is_sync(bio->bi_opf));
  2135. if (cfqq)
  2136. return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio));
  2137. return NULL;
  2138. }
  2139. static void cfq_activate_request(struct request_queue *q, struct request *rq)
  2140. {
  2141. struct cfq_data *cfqd = q->elevator->elevator_data;
  2142. cfqd->rq_in_driver++;
  2143. cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
  2144. cfqd->rq_in_driver);
  2145. cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
  2146. }
  2147. static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
  2148. {
  2149. struct cfq_data *cfqd = q->elevator->elevator_data;
  2150. WARN_ON(!cfqd->rq_in_driver);
  2151. cfqd->rq_in_driver--;
  2152. cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
  2153. cfqd->rq_in_driver);
  2154. }
  2155. static void cfq_remove_request(struct request *rq)
  2156. {
  2157. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2158. if (cfqq->next_rq == rq)
  2159. cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
  2160. list_del_init(&rq->queuelist);
  2161. cfq_del_rq_rb(rq);
  2162. cfqq->cfqd->rq_queued--;
  2163. cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
  2164. if (rq->cmd_flags & REQ_PRIO) {
  2165. WARN_ON(!cfqq->prio_pending);
  2166. cfqq->prio_pending--;
  2167. }
  2168. }
  2169. static int cfq_merge(struct request_queue *q, struct request **req,
  2170. struct bio *bio)
  2171. {
  2172. struct cfq_data *cfqd = q->elevator->elevator_data;
  2173. struct request *__rq;
  2174. __rq = cfq_find_rq_fmerge(cfqd, bio);
  2175. if (__rq && elv_bio_merge_ok(__rq, bio)) {
  2176. *req = __rq;
  2177. return ELEVATOR_FRONT_MERGE;
  2178. }
  2179. return ELEVATOR_NO_MERGE;
  2180. }
  2181. static void cfq_merged_request(struct request_queue *q, struct request *req,
  2182. int type)
  2183. {
  2184. if (type == ELEVATOR_FRONT_MERGE) {
  2185. struct cfq_queue *cfqq = RQ_CFQQ(req);
  2186. cfq_reposition_rq_rb(cfqq, req);
  2187. }
  2188. }
  2189. static void cfq_bio_merged(struct request_queue *q, struct request *req,
  2190. struct bio *bio)
  2191. {
  2192. cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_opf);
  2193. }
  2194. static void
  2195. cfq_merged_requests(struct request_queue *q, struct request *rq,
  2196. struct request *next)
  2197. {
  2198. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2199. struct cfq_data *cfqd = q->elevator->elevator_data;
  2200. /*
  2201. * reposition in fifo if next is older than rq
  2202. */
  2203. if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
  2204. next->fifo_time < rq->fifo_time &&
  2205. cfqq == RQ_CFQQ(next)) {
  2206. list_move(&rq->queuelist, &next->queuelist);
  2207. rq->fifo_time = next->fifo_time;
  2208. }
  2209. if (cfqq->next_rq == next)
  2210. cfqq->next_rq = rq;
  2211. cfq_remove_request(next);
  2212. cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
  2213. cfqq = RQ_CFQQ(next);
  2214. /*
  2215. * all requests of this queue are merged to other queues, delete it
  2216. * from the service tree. If it's the active_queue,
  2217. * cfq_dispatch_requests() will choose to expire it or do idle
  2218. */
  2219. if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
  2220. cfqq != cfqd->active_queue)
  2221. cfq_del_cfqq_rr(cfqd, cfqq);
  2222. }
  2223. static int cfq_allow_bio_merge(struct request_queue *q, struct request *rq,
  2224. struct bio *bio)
  2225. {
  2226. struct cfq_data *cfqd = q->elevator->elevator_data;
  2227. bool is_sync = op_is_sync(bio->bi_opf);
  2228. struct cfq_io_cq *cic;
  2229. struct cfq_queue *cfqq;
  2230. /*
  2231. * Disallow merge of a sync bio into an async request.
  2232. */
  2233. if (is_sync && !rq_is_sync(rq))
  2234. return false;
  2235. /*
  2236. * Lookup the cfqq that this bio will be queued with and allow
  2237. * merge only if rq is queued there.
  2238. */
  2239. cic = cfq_cic_lookup(cfqd, current->io_context);
  2240. if (!cic)
  2241. return false;
  2242. cfqq = cic_to_cfqq(cic, is_sync);
  2243. return cfqq == RQ_CFQQ(rq);
  2244. }
  2245. static int cfq_allow_rq_merge(struct request_queue *q, struct request *rq,
  2246. struct request *next)
  2247. {
  2248. return RQ_CFQQ(rq) == RQ_CFQQ(next);
  2249. }
  2250. static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2251. {
  2252. hrtimer_try_to_cancel(&cfqd->idle_slice_timer);
  2253. cfqg_stats_update_idle_time(cfqq->cfqg);
  2254. }
  2255. static void __cfq_set_active_queue(struct cfq_data *cfqd,
  2256. struct cfq_queue *cfqq)
  2257. {
  2258. if (cfqq) {
  2259. cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
  2260. cfqd->serving_wl_class, cfqd->serving_wl_type);
  2261. cfqg_stats_update_avg_queue_size(cfqq->cfqg);
  2262. cfqq->slice_start = 0;
  2263. cfqq->dispatch_start = ktime_get_ns();
  2264. cfqq->allocated_slice = 0;
  2265. cfqq->slice_end = 0;
  2266. cfqq->slice_dispatch = 0;
  2267. cfqq->nr_sectors = 0;
  2268. cfq_clear_cfqq_wait_request(cfqq);
  2269. cfq_clear_cfqq_must_dispatch(cfqq);
  2270. cfq_clear_cfqq_must_alloc_slice(cfqq);
  2271. cfq_clear_cfqq_fifo_expire(cfqq);
  2272. cfq_mark_cfqq_slice_new(cfqq);
  2273. cfq_del_timer(cfqd, cfqq);
  2274. }
  2275. cfqd->active_queue = cfqq;
  2276. }
  2277. /*
  2278. * current cfqq expired its slice (or was too idle), select new one
  2279. */
  2280. static void
  2281. __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  2282. bool timed_out)
  2283. {
  2284. cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
  2285. if (cfq_cfqq_wait_request(cfqq))
  2286. cfq_del_timer(cfqd, cfqq);
  2287. cfq_clear_cfqq_wait_request(cfqq);
  2288. cfq_clear_cfqq_wait_busy(cfqq);
  2289. /*
  2290. * If this cfqq is shared between multiple processes, check to
  2291. * make sure that those processes are still issuing I/Os within
  2292. * the mean seek distance. If not, it may be time to break the
  2293. * queues apart again.
  2294. */
  2295. if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
  2296. cfq_mark_cfqq_split_coop(cfqq);
  2297. /*
  2298. * store what was left of this slice, if the queue idled/timed out
  2299. */
  2300. if (timed_out) {
  2301. if (cfq_cfqq_slice_new(cfqq))
  2302. cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
  2303. else
  2304. cfqq->slice_resid = cfqq->slice_end - ktime_get_ns();
  2305. cfq_log_cfqq(cfqd, cfqq, "resid=%lld", cfqq->slice_resid);
  2306. }
  2307. cfq_group_served(cfqd, cfqq->cfqg, cfqq);
  2308. if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
  2309. cfq_del_cfqq_rr(cfqd, cfqq);
  2310. cfq_resort_rr_list(cfqd, cfqq);
  2311. if (cfqq == cfqd->active_queue)
  2312. cfqd->active_queue = NULL;
  2313. if (cfqd->active_cic) {
  2314. put_io_context(cfqd->active_cic->icq.ioc);
  2315. cfqd->active_cic = NULL;
  2316. }
  2317. }
  2318. static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
  2319. {
  2320. struct cfq_queue *cfqq = cfqd->active_queue;
  2321. if (cfqq)
  2322. __cfq_slice_expired(cfqd, cfqq, timed_out);
  2323. }
  2324. /*
  2325. * Get next queue for service. Unless we have a queue preemption,
  2326. * we'll simply select the first cfqq in the service tree.
  2327. */
  2328. static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
  2329. {
  2330. struct cfq_rb_root *st = st_for(cfqd->serving_group,
  2331. cfqd->serving_wl_class, cfqd->serving_wl_type);
  2332. if (!cfqd->rq_queued)
  2333. return NULL;
  2334. /* There is nothing to dispatch */
  2335. if (!st)
  2336. return NULL;
  2337. if (RB_EMPTY_ROOT(&st->rb))
  2338. return NULL;
  2339. return cfq_rb_first(st);
  2340. }
  2341. static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
  2342. {
  2343. struct cfq_group *cfqg;
  2344. struct cfq_queue *cfqq;
  2345. int i, j;
  2346. struct cfq_rb_root *st;
  2347. if (!cfqd->rq_queued)
  2348. return NULL;
  2349. cfqg = cfq_get_next_cfqg(cfqd);
  2350. if (!cfqg)
  2351. return NULL;
  2352. for_each_cfqg_st(cfqg, i, j, st)
  2353. if ((cfqq = cfq_rb_first(st)) != NULL)
  2354. return cfqq;
  2355. return NULL;
  2356. }
  2357. /*
  2358. * Get and set a new active queue for service.
  2359. */
  2360. static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
  2361. struct cfq_queue *cfqq)
  2362. {
  2363. if (!cfqq)
  2364. cfqq = cfq_get_next_queue(cfqd);
  2365. __cfq_set_active_queue(cfqd, cfqq);
  2366. return cfqq;
  2367. }
  2368. static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
  2369. struct request *rq)
  2370. {
  2371. if (blk_rq_pos(rq) >= cfqd->last_position)
  2372. return blk_rq_pos(rq) - cfqd->last_position;
  2373. else
  2374. return cfqd->last_position - blk_rq_pos(rq);
  2375. }
  2376. static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  2377. struct request *rq)
  2378. {
  2379. return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
  2380. }
  2381. static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
  2382. struct cfq_queue *cur_cfqq)
  2383. {
  2384. struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
  2385. struct rb_node *parent, *node;
  2386. struct cfq_queue *__cfqq;
  2387. sector_t sector = cfqd->last_position;
  2388. if (RB_EMPTY_ROOT(root))
  2389. return NULL;
  2390. /*
  2391. * First, if we find a request starting at the end of the last
  2392. * request, choose it.
  2393. */
  2394. __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
  2395. if (__cfqq)
  2396. return __cfqq;
  2397. /*
  2398. * If the exact sector wasn't found, the parent of the NULL leaf
  2399. * will contain the closest sector.
  2400. */
  2401. __cfqq = rb_entry(parent, struct cfq_queue, p_node);
  2402. if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
  2403. return __cfqq;
  2404. if (blk_rq_pos(__cfqq->next_rq) < sector)
  2405. node = rb_next(&__cfqq->p_node);
  2406. else
  2407. node = rb_prev(&__cfqq->p_node);
  2408. if (!node)
  2409. return NULL;
  2410. __cfqq = rb_entry(node, struct cfq_queue, p_node);
  2411. if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
  2412. return __cfqq;
  2413. return NULL;
  2414. }
  2415. /*
  2416. * cfqd - obvious
  2417. * cur_cfqq - passed in so that we don't decide that the current queue is
  2418. * closely cooperating with itself.
  2419. *
  2420. * So, basically we're assuming that that cur_cfqq has dispatched at least
  2421. * one request, and that cfqd->last_position reflects a position on the disk
  2422. * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
  2423. * assumption.
  2424. */
  2425. static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
  2426. struct cfq_queue *cur_cfqq)
  2427. {
  2428. struct cfq_queue *cfqq;
  2429. if (cfq_class_idle(cur_cfqq))
  2430. return NULL;
  2431. if (!cfq_cfqq_sync(cur_cfqq))
  2432. return NULL;
  2433. if (CFQQ_SEEKY(cur_cfqq))
  2434. return NULL;
  2435. /*
  2436. * Don't search priority tree if it's the only queue in the group.
  2437. */
  2438. if (cur_cfqq->cfqg->nr_cfqq == 1)
  2439. return NULL;
  2440. /*
  2441. * We should notice if some of the queues are cooperating, eg
  2442. * working closely on the same area of the disk. In that case,
  2443. * we can group them together and don't waste time idling.
  2444. */
  2445. cfqq = cfqq_close(cfqd, cur_cfqq);
  2446. if (!cfqq)
  2447. return NULL;
  2448. /* If new queue belongs to different cfq_group, don't choose it */
  2449. if (cur_cfqq->cfqg != cfqq->cfqg)
  2450. return NULL;
  2451. /*
  2452. * It only makes sense to merge sync queues.
  2453. */
  2454. if (!cfq_cfqq_sync(cfqq))
  2455. return NULL;
  2456. if (CFQQ_SEEKY(cfqq))
  2457. return NULL;
  2458. /*
  2459. * Do not merge queues of different priority classes
  2460. */
  2461. if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
  2462. return NULL;
  2463. return cfqq;
  2464. }
  2465. /*
  2466. * Determine whether we should enforce idle window for this queue.
  2467. */
  2468. static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2469. {
  2470. enum wl_class_t wl_class = cfqq_class(cfqq);
  2471. struct cfq_rb_root *st = cfqq->service_tree;
  2472. BUG_ON(!st);
  2473. BUG_ON(!st->count);
  2474. if (!cfqd->cfq_slice_idle)
  2475. return false;
  2476. /* We never do for idle class queues. */
  2477. if (wl_class == IDLE_WORKLOAD)
  2478. return false;
  2479. /* We do for queues that were marked with idle window flag. */
  2480. if (cfq_cfqq_idle_window(cfqq) &&
  2481. !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
  2482. return true;
  2483. /*
  2484. * Otherwise, we do only if they are the last ones
  2485. * in their service tree.
  2486. */
  2487. if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
  2488. !cfq_io_thinktime_big(cfqd, &st->ttime, false))
  2489. return true;
  2490. cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
  2491. return false;
  2492. }
  2493. static void cfq_arm_slice_timer(struct cfq_data *cfqd)
  2494. {
  2495. struct cfq_queue *cfqq = cfqd->active_queue;
  2496. struct cfq_rb_root *st = cfqq->service_tree;
  2497. struct cfq_io_cq *cic;
  2498. u64 sl, group_idle = 0;
  2499. u64 now = ktime_get_ns();
  2500. /*
  2501. * SSD device without seek penalty, disable idling. But only do so
  2502. * for devices that support queuing, otherwise we still have a problem
  2503. * with sync vs async workloads.
  2504. */
  2505. if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
  2506. return;
  2507. WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
  2508. WARN_ON(cfq_cfqq_slice_new(cfqq));
  2509. /*
  2510. * idle is disabled, either manually or by past process history
  2511. */
  2512. if (!cfq_should_idle(cfqd, cfqq)) {
  2513. /* no queue idling. Check for group idling */
  2514. if (cfqd->cfq_group_idle)
  2515. group_idle = cfqd->cfq_group_idle;
  2516. else
  2517. return;
  2518. }
  2519. /*
  2520. * still active requests from this queue, don't idle
  2521. */
  2522. if (cfqq->dispatched)
  2523. return;
  2524. /*
  2525. * task has exited, don't wait
  2526. */
  2527. cic = cfqd->active_cic;
  2528. if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
  2529. return;
  2530. /*
  2531. * If our average think time is larger than the remaining time
  2532. * slice, then don't idle. This avoids overrunning the allotted
  2533. * time slice.
  2534. */
  2535. if (sample_valid(cic->ttime.ttime_samples) &&
  2536. (cfqq->slice_end - now < cic->ttime.ttime_mean)) {
  2537. cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%llu",
  2538. cic->ttime.ttime_mean);
  2539. return;
  2540. }
  2541. /*
  2542. * There are other queues in the group or this is the only group and
  2543. * it has too big thinktime, don't do group idle.
  2544. */
  2545. if (group_idle &&
  2546. (cfqq->cfqg->nr_cfqq > 1 ||
  2547. cfq_io_thinktime_big(cfqd, &st->ttime, true)))
  2548. return;
  2549. cfq_mark_cfqq_wait_request(cfqq);
  2550. if (group_idle)
  2551. sl = cfqd->cfq_group_idle;
  2552. else
  2553. sl = cfqd->cfq_slice_idle;
  2554. hrtimer_start(&cfqd->idle_slice_timer, ns_to_ktime(sl),
  2555. HRTIMER_MODE_REL);
  2556. cfqg_stats_set_start_idle_time(cfqq->cfqg);
  2557. cfq_log_cfqq(cfqd, cfqq, "arm_idle: %llu group_idle: %d", sl,
  2558. group_idle ? 1 : 0);
  2559. }
  2560. /*
  2561. * Move request from internal lists to the request queue dispatch list.
  2562. */
  2563. static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
  2564. {
  2565. struct cfq_data *cfqd = q->elevator->elevator_data;
  2566. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2567. cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
  2568. cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
  2569. cfq_remove_request(rq);
  2570. cfqq->dispatched++;
  2571. (RQ_CFQG(rq))->dispatched++;
  2572. elv_dispatch_sort(q, rq);
  2573. cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
  2574. cfqq->nr_sectors += blk_rq_sectors(rq);
  2575. }
  2576. /*
  2577. * return expired entry, or NULL to just start from scratch in rbtree
  2578. */
  2579. static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
  2580. {
  2581. struct request *rq = NULL;
  2582. if (cfq_cfqq_fifo_expire(cfqq))
  2583. return NULL;
  2584. cfq_mark_cfqq_fifo_expire(cfqq);
  2585. if (list_empty(&cfqq->fifo))
  2586. return NULL;
  2587. rq = rq_entry_fifo(cfqq->fifo.next);
  2588. if (ktime_get_ns() < rq->fifo_time)
  2589. rq = NULL;
  2590. return rq;
  2591. }
  2592. static inline int
  2593. cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2594. {
  2595. const int base_rq = cfqd->cfq_slice_async_rq;
  2596. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  2597. return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
  2598. }
  2599. /*
  2600. * Must be called with the queue_lock held.
  2601. */
  2602. static int cfqq_process_refs(struct cfq_queue *cfqq)
  2603. {
  2604. int process_refs, io_refs;
  2605. io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
  2606. process_refs = cfqq->ref - io_refs;
  2607. BUG_ON(process_refs < 0);
  2608. return process_refs;
  2609. }
  2610. static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
  2611. {
  2612. int process_refs, new_process_refs;
  2613. struct cfq_queue *__cfqq;
  2614. /*
  2615. * If there are no process references on the new_cfqq, then it is
  2616. * unsafe to follow the ->new_cfqq chain as other cfqq's in the
  2617. * chain may have dropped their last reference (not just their
  2618. * last process reference).
  2619. */
  2620. if (!cfqq_process_refs(new_cfqq))
  2621. return;
  2622. /* Avoid a circular list and skip interim queue merges */
  2623. while ((__cfqq = new_cfqq->new_cfqq)) {
  2624. if (__cfqq == cfqq)
  2625. return;
  2626. new_cfqq = __cfqq;
  2627. }
  2628. process_refs = cfqq_process_refs(cfqq);
  2629. new_process_refs = cfqq_process_refs(new_cfqq);
  2630. /*
  2631. * If the process for the cfqq has gone away, there is no
  2632. * sense in merging the queues.
  2633. */
  2634. if (process_refs == 0 || new_process_refs == 0)
  2635. return;
  2636. /*
  2637. * Merge in the direction of the lesser amount of work.
  2638. */
  2639. if (new_process_refs >= process_refs) {
  2640. cfqq->new_cfqq = new_cfqq;
  2641. new_cfqq->ref += process_refs;
  2642. } else {
  2643. new_cfqq->new_cfqq = cfqq;
  2644. cfqq->ref += new_process_refs;
  2645. }
  2646. }
  2647. static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
  2648. struct cfq_group *cfqg, enum wl_class_t wl_class)
  2649. {
  2650. struct cfq_queue *queue;
  2651. int i;
  2652. bool key_valid = false;
  2653. u64 lowest_key = 0;
  2654. enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
  2655. for (i = 0; i <= SYNC_WORKLOAD; ++i) {
  2656. /* select the one with lowest rb_key */
  2657. queue = cfq_rb_first(st_for(cfqg, wl_class, i));
  2658. if (queue &&
  2659. (!key_valid || queue->rb_key < lowest_key)) {
  2660. lowest_key = queue->rb_key;
  2661. cur_best = i;
  2662. key_valid = true;
  2663. }
  2664. }
  2665. return cur_best;
  2666. }
  2667. static void
  2668. choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
  2669. {
  2670. u64 slice;
  2671. unsigned count;
  2672. struct cfq_rb_root *st;
  2673. u64 group_slice;
  2674. enum wl_class_t original_class = cfqd->serving_wl_class;
  2675. u64 now = ktime_get_ns();
  2676. /* Choose next priority. RT > BE > IDLE */
  2677. if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
  2678. cfqd->serving_wl_class = RT_WORKLOAD;
  2679. else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
  2680. cfqd->serving_wl_class = BE_WORKLOAD;
  2681. else {
  2682. cfqd->serving_wl_class = IDLE_WORKLOAD;
  2683. cfqd->workload_expires = now + jiffies_to_nsecs(1);
  2684. return;
  2685. }
  2686. if (original_class != cfqd->serving_wl_class)
  2687. goto new_workload;
  2688. /*
  2689. * For RT and BE, we have to choose also the type
  2690. * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
  2691. * expiration time
  2692. */
  2693. st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
  2694. count = st->count;
  2695. /*
  2696. * check workload expiration, and that we still have other queues ready
  2697. */
  2698. if (count && !(now > cfqd->workload_expires))
  2699. return;
  2700. new_workload:
  2701. /* otherwise select new workload type */
  2702. cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
  2703. cfqd->serving_wl_class);
  2704. st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
  2705. count = st->count;
  2706. /*
  2707. * the workload slice is computed as a fraction of target latency
  2708. * proportional to the number of queues in that workload, over
  2709. * all the queues in the same priority class
  2710. */
  2711. group_slice = cfq_group_slice(cfqd, cfqg);
  2712. slice = div_u64(group_slice * count,
  2713. max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
  2714. cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
  2715. cfqg)));
  2716. if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
  2717. u64 tmp;
  2718. /*
  2719. * Async queues are currently system wide. Just taking
  2720. * proportion of queues with-in same group will lead to higher
  2721. * async ratio system wide as generally root group is going
  2722. * to have higher weight. A more accurate thing would be to
  2723. * calculate system wide asnc/sync ratio.
  2724. */
  2725. tmp = cfqd->cfq_target_latency *
  2726. cfqg_busy_async_queues(cfqd, cfqg);
  2727. tmp = div_u64(tmp, cfqd->busy_queues);
  2728. slice = min_t(u64, slice, tmp);
  2729. /* async workload slice is scaled down according to
  2730. * the sync/async slice ratio. */
  2731. slice = div64_u64(slice*cfqd->cfq_slice[0], cfqd->cfq_slice[1]);
  2732. } else
  2733. /* sync workload slice is at least 2 * cfq_slice_idle */
  2734. slice = max(slice, 2 * cfqd->cfq_slice_idle);
  2735. slice = max_t(u64, slice, CFQ_MIN_TT);
  2736. cfq_log(cfqd, "workload slice:%llu", slice);
  2737. cfqd->workload_expires = now + slice;
  2738. }
  2739. static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
  2740. {
  2741. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  2742. struct cfq_group *cfqg;
  2743. if (RB_EMPTY_ROOT(&st->rb))
  2744. return NULL;
  2745. cfqg = cfq_rb_first_group(st);
  2746. update_min_vdisktime(st);
  2747. return cfqg;
  2748. }
  2749. static void cfq_choose_cfqg(struct cfq_data *cfqd)
  2750. {
  2751. struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
  2752. u64 now = ktime_get_ns();
  2753. cfqd->serving_group = cfqg;
  2754. /* Restore the workload type data */
  2755. if (cfqg->saved_wl_slice) {
  2756. cfqd->workload_expires = now + cfqg->saved_wl_slice;
  2757. cfqd->serving_wl_type = cfqg->saved_wl_type;
  2758. cfqd->serving_wl_class = cfqg->saved_wl_class;
  2759. } else
  2760. cfqd->workload_expires = now - 1;
  2761. choose_wl_class_and_type(cfqd, cfqg);
  2762. }
  2763. /*
  2764. * Select a queue for service. If we have a current active queue,
  2765. * check whether to continue servicing it, or retrieve and set a new one.
  2766. */
  2767. static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
  2768. {
  2769. struct cfq_queue *cfqq, *new_cfqq = NULL;
  2770. u64 now = ktime_get_ns();
  2771. cfqq = cfqd->active_queue;
  2772. if (!cfqq)
  2773. goto new_queue;
  2774. if (!cfqd->rq_queued)
  2775. return NULL;
  2776. /*
  2777. * We were waiting for group to get backlogged. Expire the queue
  2778. */
  2779. if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
  2780. goto expire;
  2781. /*
  2782. * The active queue has run out of time, expire it and select new.
  2783. */
  2784. if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
  2785. /*
  2786. * If slice had not expired at the completion of last request
  2787. * we might not have turned on wait_busy flag. Don't expire
  2788. * the queue yet. Allow the group to get backlogged.
  2789. *
  2790. * The very fact that we have used the slice, that means we
  2791. * have been idling all along on this queue and it should be
  2792. * ok to wait for this request to complete.
  2793. */
  2794. if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
  2795. && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
  2796. cfqq = NULL;
  2797. goto keep_queue;
  2798. } else
  2799. goto check_group_idle;
  2800. }
  2801. /*
  2802. * The active queue has requests and isn't expired, allow it to
  2803. * dispatch.
  2804. */
  2805. if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  2806. goto keep_queue;
  2807. /*
  2808. * If another queue has a request waiting within our mean seek
  2809. * distance, let it run. The expire code will check for close
  2810. * cooperators and put the close queue at the front of the service
  2811. * tree. If possible, merge the expiring queue with the new cfqq.
  2812. */
  2813. new_cfqq = cfq_close_cooperator(cfqd, cfqq);
  2814. if (new_cfqq) {
  2815. if (!cfqq->new_cfqq)
  2816. cfq_setup_merge(cfqq, new_cfqq);
  2817. goto expire;
  2818. }
  2819. /*
  2820. * No requests pending. If the active queue still has requests in
  2821. * flight or is idling for a new request, allow either of these
  2822. * conditions to happen (or time out) before selecting a new queue.
  2823. */
  2824. if (hrtimer_active(&cfqd->idle_slice_timer)) {
  2825. cfqq = NULL;
  2826. goto keep_queue;
  2827. }
  2828. /*
  2829. * This is a deep seek queue, but the device is much faster than
  2830. * the queue can deliver, don't idle
  2831. **/
  2832. if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
  2833. (cfq_cfqq_slice_new(cfqq) ||
  2834. (cfqq->slice_end - now > now - cfqq->slice_start))) {
  2835. cfq_clear_cfqq_deep(cfqq);
  2836. cfq_clear_cfqq_idle_window(cfqq);
  2837. }
  2838. if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
  2839. cfqq = NULL;
  2840. goto keep_queue;
  2841. }
  2842. /*
  2843. * If group idle is enabled and there are requests dispatched from
  2844. * this group, wait for requests to complete.
  2845. */
  2846. check_group_idle:
  2847. if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
  2848. cfqq->cfqg->dispatched &&
  2849. !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
  2850. cfqq = NULL;
  2851. goto keep_queue;
  2852. }
  2853. expire:
  2854. cfq_slice_expired(cfqd, 0);
  2855. new_queue:
  2856. /*
  2857. * Current queue expired. Check if we have to switch to a new
  2858. * service tree
  2859. */
  2860. if (!new_cfqq)
  2861. cfq_choose_cfqg(cfqd);
  2862. cfqq = cfq_set_active_queue(cfqd, new_cfqq);
  2863. keep_queue:
  2864. return cfqq;
  2865. }
  2866. static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
  2867. {
  2868. int dispatched = 0;
  2869. while (cfqq->next_rq) {
  2870. cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
  2871. dispatched++;
  2872. }
  2873. BUG_ON(!list_empty(&cfqq->fifo));
  2874. /* By default cfqq is not expired if it is empty. Do it explicitly */
  2875. __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
  2876. return dispatched;
  2877. }
  2878. /*
  2879. * Drain our current requests. Used for barriers and when switching
  2880. * io schedulers on-the-fly.
  2881. */
  2882. static int cfq_forced_dispatch(struct cfq_data *cfqd)
  2883. {
  2884. struct cfq_queue *cfqq;
  2885. int dispatched = 0;
  2886. /* Expire the timeslice of the current active queue first */
  2887. cfq_slice_expired(cfqd, 0);
  2888. while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
  2889. __cfq_set_active_queue(cfqd, cfqq);
  2890. dispatched += __cfq_forced_dispatch_cfqq(cfqq);
  2891. }
  2892. BUG_ON(cfqd->busy_queues);
  2893. cfq_log(cfqd, "forced_dispatch=%d", dispatched);
  2894. return dispatched;
  2895. }
  2896. static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
  2897. struct cfq_queue *cfqq)
  2898. {
  2899. u64 now = ktime_get_ns();
  2900. /* the queue hasn't finished any request, can't estimate */
  2901. if (cfq_cfqq_slice_new(cfqq))
  2902. return true;
  2903. if (now + cfqd->cfq_slice_idle * cfqq->dispatched > cfqq->slice_end)
  2904. return true;
  2905. return false;
  2906. }
  2907. static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2908. {
  2909. unsigned int max_dispatch;
  2910. if (cfq_cfqq_must_dispatch(cfqq))
  2911. return true;
  2912. /*
  2913. * Drain async requests before we start sync IO
  2914. */
  2915. if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
  2916. return false;
  2917. /*
  2918. * If this is an async queue and we have sync IO in flight, let it wait
  2919. */
  2920. if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
  2921. return false;
  2922. max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
  2923. if (cfq_class_idle(cfqq))
  2924. max_dispatch = 1;
  2925. /*
  2926. * Does this cfqq already have too much IO in flight?
  2927. */
  2928. if (cfqq->dispatched >= max_dispatch) {
  2929. bool promote_sync = false;
  2930. /*
  2931. * idle queue must always only have a single IO in flight
  2932. */
  2933. if (cfq_class_idle(cfqq))
  2934. return false;
  2935. /*
  2936. * If there is only one sync queue
  2937. * we can ignore async queue here and give the sync
  2938. * queue no dispatch limit. The reason is a sync queue can
  2939. * preempt async queue, limiting the sync queue doesn't make
  2940. * sense. This is useful for aiostress test.
  2941. */
  2942. if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
  2943. promote_sync = true;
  2944. /*
  2945. * We have other queues, don't allow more IO from this one
  2946. */
  2947. if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
  2948. !promote_sync)
  2949. return false;
  2950. /*
  2951. * Sole queue user, no limit
  2952. */
  2953. if (cfqd->busy_queues == 1 || promote_sync)
  2954. max_dispatch = -1;
  2955. else
  2956. /*
  2957. * Normally we start throttling cfqq when cfq_quantum/2
  2958. * requests have been dispatched. But we can drive
  2959. * deeper queue depths at the beginning of slice
  2960. * subjected to upper limit of cfq_quantum.
  2961. * */
  2962. max_dispatch = cfqd->cfq_quantum;
  2963. }
  2964. /*
  2965. * Async queues must wait a bit before being allowed dispatch.
  2966. * We also ramp up the dispatch depth gradually for async IO,
  2967. * based on the last sync IO we serviced
  2968. */
  2969. if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
  2970. u64 last_sync = ktime_get_ns() - cfqd->last_delayed_sync;
  2971. unsigned int depth;
  2972. depth = div64_u64(last_sync, cfqd->cfq_slice[1]);
  2973. if (!depth && !cfqq->dispatched)
  2974. depth = 1;
  2975. if (depth < max_dispatch)
  2976. max_dispatch = depth;
  2977. }
  2978. /*
  2979. * If we're below the current max, allow a dispatch
  2980. */
  2981. return cfqq->dispatched < max_dispatch;
  2982. }
  2983. /*
  2984. * Dispatch a request from cfqq, moving them to the request queue
  2985. * dispatch list.
  2986. */
  2987. static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2988. {
  2989. struct request *rq;
  2990. BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
  2991. rq = cfq_check_fifo(cfqq);
  2992. if (rq)
  2993. cfq_mark_cfqq_must_dispatch(cfqq);
  2994. if (!cfq_may_dispatch(cfqd, cfqq))
  2995. return false;
  2996. /*
  2997. * follow expired path, else get first next available
  2998. */
  2999. if (!rq)
  3000. rq = cfqq->next_rq;
  3001. else
  3002. cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
  3003. /*
  3004. * insert request into driver dispatch list
  3005. */
  3006. cfq_dispatch_insert(cfqd->queue, rq);
  3007. if (!cfqd->active_cic) {
  3008. struct cfq_io_cq *cic = RQ_CIC(rq);
  3009. atomic_long_inc(&cic->icq.ioc->refcount);
  3010. cfqd->active_cic = cic;
  3011. }
  3012. return true;
  3013. }
  3014. /*
  3015. * Find the cfqq that we need to service and move a request from that to the
  3016. * dispatch list
  3017. */
  3018. static int cfq_dispatch_requests(struct request_queue *q, int force)
  3019. {
  3020. struct cfq_data *cfqd = q->elevator->elevator_data;
  3021. struct cfq_queue *cfqq;
  3022. if (!cfqd->busy_queues)
  3023. return 0;
  3024. if (unlikely(force))
  3025. return cfq_forced_dispatch(cfqd);
  3026. cfqq = cfq_select_queue(cfqd);
  3027. if (!cfqq)
  3028. return 0;
  3029. /*
  3030. * Dispatch a request from this cfqq, if it is allowed
  3031. */
  3032. if (!cfq_dispatch_request(cfqd, cfqq))
  3033. return 0;
  3034. cfqq->slice_dispatch++;
  3035. cfq_clear_cfqq_must_dispatch(cfqq);
  3036. /*
  3037. * expire an async queue immediately if it has used up its slice. idle
  3038. * queue always expire after 1 dispatch round.
  3039. */
  3040. if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
  3041. cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
  3042. cfq_class_idle(cfqq))) {
  3043. cfqq->slice_end = ktime_get_ns() + 1;
  3044. cfq_slice_expired(cfqd, 0);
  3045. }
  3046. cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
  3047. return 1;
  3048. }
  3049. /*
  3050. * task holds one reference to the queue, dropped when task exits. each rq
  3051. * in-flight on this queue also holds a reference, dropped when rq is freed.
  3052. *
  3053. * Each cfq queue took a reference on the parent group. Drop it now.
  3054. * queue lock must be held here.
  3055. */
  3056. static void cfq_put_queue(struct cfq_queue *cfqq)
  3057. {
  3058. struct cfq_data *cfqd = cfqq->cfqd;
  3059. struct cfq_group *cfqg;
  3060. BUG_ON(cfqq->ref <= 0);
  3061. cfqq->ref--;
  3062. if (cfqq->ref)
  3063. return;
  3064. cfq_log_cfqq(cfqd, cfqq, "put_queue");
  3065. BUG_ON(rb_first(&cfqq->sort_list));
  3066. BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
  3067. cfqg = cfqq->cfqg;
  3068. if (unlikely(cfqd->active_queue == cfqq)) {
  3069. __cfq_slice_expired(cfqd, cfqq, 0);
  3070. cfq_schedule_dispatch(cfqd);
  3071. }
  3072. BUG_ON(cfq_cfqq_on_rr(cfqq));
  3073. kmem_cache_free(cfq_pool, cfqq);
  3074. cfqg_put(cfqg);
  3075. }
  3076. static void cfq_put_cooperator(struct cfq_queue *cfqq)
  3077. {
  3078. struct cfq_queue *__cfqq, *next;
  3079. /*
  3080. * If this queue was scheduled to merge with another queue, be
  3081. * sure to drop the reference taken on that queue (and others in
  3082. * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
  3083. */
  3084. __cfqq = cfqq->new_cfqq;
  3085. while (__cfqq) {
  3086. if (__cfqq == cfqq) {
  3087. WARN(1, "cfqq->new_cfqq loop detected\n");
  3088. break;
  3089. }
  3090. next = __cfqq->new_cfqq;
  3091. cfq_put_queue(__cfqq);
  3092. __cfqq = next;
  3093. }
  3094. }
  3095. static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  3096. {
  3097. if (unlikely(cfqq == cfqd->active_queue)) {
  3098. __cfq_slice_expired(cfqd, cfqq, 0);
  3099. cfq_schedule_dispatch(cfqd);
  3100. }
  3101. cfq_put_cooperator(cfqq);
  3102. cfq_put_queue(cfqq);
  3103. }
  3104. static void cfq_init_icq(struct io_cq *icq)
  3105. {
  3106. struct cfq_io_cq *cic = icq_to_cic(icq);
  3107. cic->ttime.last_end_request = ktime_get_ns();
  3108. }
  3109. static void cfq_exit_icq(struct io_cq *icq)
  3110. {
  3111. struct cfq_io_cq *cic = icq_to_cic(icq);
  3112. struct cfq_data *cfqd = cic_to_cfqd(cic);
  3113. if (cic_to_cfqq(cic, false)) {
  3114. cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, false));
  3115. cic_set_cfqq(cic, NULL, false);
  3116. }
  3117. if (cic_to_cfqq(cic, true)) {
  3118. cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, true));
  3119. cic_set_cfqq(cic, NULL, true);
  3120. }
  3121. }
  3122. static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
  3123. {
  3124. struct task_struct *tsk = current;
  3125. int ioprio_class;
  3126. if (!cfq_cfqq_prio_changed(cfqq))
  3127. return;
  3128. ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
  3129. switch (ioprio_class) {
  3130. default:
  3131. printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
  3132. case IOPRIO_CLASS_NONE:
  3133. /*
  3134. * no prio set, inherit CPU scheduling settings
  3135. */
  3136. cfqq->ioprio = task_nice_ioprio(tsk);
  3137. cfqq->ioprio_class = task_nice_ioclass(tsk);
  3138. break;
  3139. case IOPRIO_CLASS_RT:
  3140. cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
  3141. cfqq->ioprio_class = IOPRIO_CLASS_RT;
  3142. break;
  3143. case IOPRIO_CLASS_BE:
  3144. cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
  3145. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  3146. break;
  3147. case IOPRIO_CLASS_IDLE:
  3148. cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
  3149. cfqq->ioprio = 7;
  3150. cfq_clear_cfqq_idle_window(cfqq);
  3151. break;
  3152. }
  3153. /*
  3154. * keep track of original prio settings in case we have to temporarily
  3155. * elevate the priority of this queue
  3156. */
  3157. cfqq->org_ioprio = cfqq->ioprio;
  3158. cfqq->org_ioprio_class = cfqq->ioprio_class;
  3159. cfq_clear_cfqq_prio_changed(cfqq);
  3160. }
  3161. static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
  3162. {
  3163. int ioprio = cic->icq.ioc->ioprio;
  3164. struct cfq_data *cfqd = cic_to_cfqd(cic);
  3165. struct cfq_queue *cfqq;
  3166. /*
  3167. * Check whether ioprio has changed. The condition may trigger
  3168. * spuriously on a newly created cic but there's no harm.
  3169. */
  3170. if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
  3171. return;
  3172. cfqq = cic_to_cfqq(cic, false);
  3173. if (cfqq) {
  3174. cfq_put_queue(cfqq);
  3175. cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio);
  3176. cic_set_cfqq(cic, cfqq, false);
  3177. }
  3178. cfqq = cic_to_cfqq(cic, true);
  3179. if (cfqq)
  3180. cfq_mark_cfqq_prio_changed(cfqq);
  3181. cic->ioprio = ioprio;
  3182. }
  3183. static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  3184. pid_t pid, bool is_sync)
  3185. {
  3186. RB_CLEAR_NODE(&cfqq->rb_node);
  3187. RB_CLEAR_NODE(&cfqq->p_node);
  3188. INIT_LIST_HEAD(&cfqq->fifo);
  3189. cfqq->ref = 0;
  3190. cfqq->cfqd = cfqd;
  3191. cfq_mark_cfqq_prio_changed(cfqq);
  3192. if (is_sync) {
  3193. if (!cfq_class_idle(cfqq))
  3194. cfq_mark_cfqq_idle_window(cfqq);
  3195. cfq_mark_cfqq_sync(cfqq);
  3196. }
  3197. cfqq->pid = pid;
  3198. }
  3199. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3200. static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
  3201. {
  3202. struct cfq_data *cfqd = cic_to_cfqd(cic);
  3203. struct cfq_queue *cfqq;
  3204. uint64_t serial_nr;
  3205. bool nonroot_cg;
  3206. rcu_read_lock();
  3207. serial_nr = bio_blkcg(bio)->css.serial_nr;
  3208. nonroot_cg = bio_blkcg(bio) != &blkcg_root;
  3209. rcu_read_unlock();
  3210. /*
  3211. * Check whether blkcg has changed. The condition may trigger
  3212. * spuriously on a newly created cic but there's no harm.
  3213. */
  3214. if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr))
  3215. return;
  3216. /*
  3217. * If we have a non-root cgroup, we can depend on that to
  3218. * do proper throttling of writes. Turn off wbt for that
  3219. * case, if it was enabled by default.
  3220. */
  3221. if (nonroot_cg)
  3222. wbt_disable_default(cfqd->queue);
  3223. /*
  3224. * Drop reference to queues. New queues will be assigned in new
  3225. * group upon arrival of fresh requests.
  3226. */
  3227. cfqq = cic_to_cfqq(cic, false);
  3228. if (cfqq) {
  3229. cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
  3230. cic_set_cfqq(cic, NULL, false);
  3231. cfq_put_queue(cfqq);
  3232. }
  3233. cfqq = cic_to_cfqq(cic, true);
  3234. if (cfqq) {
  3235. cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
  3236. cic_set_cfqq(cic, NULL, true);
  3237. cfq_put_queue(cfqq);
  3238. }
  3239. cic->blkcg_serial_nr = serial_nr;
  3240. }
  3241. #else
  3242. static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) { }
  3243. #endif /* CONFIG_CFQ_GROUP_IOSCHED */
  3244. static struct cfq_queue **
  3245. cfq_async_queue_prio(struct cfq_group *cfqg, int ioprio_class, int ioprio)
  3246. {
  3247. switch (ioprio_class) {
  3248. case IOPRIO_CLASS_RT:
  3249. return &cfqg->async_cfqq[0][ioprio];
  3250. case IOPRIO_CLASS_NONE:
  3251. ioprio = IOPRIO_NORM;
  3252. /* fall through */
  3253. case IOPRIO_CLASS_BE:
  3254. return &cfqg->async_cfqq[1][ioprio];
  3255. case IOPRIO_CLASS_IDLE:
  3256. return &cfqg->async_idle_cfqq;
  3257. default:
  3258. BUG();
  3259. }
  3260. }
  3261. static struct cfq_queue *
  3262. cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
  3263. struct bio *bio)
  3264. {
  3265. int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
  3266. int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
  3267. struct cfq_queue **async_cfqq = NULL;
  3268. struct cfq_queue *cfqq;
  3269. struct cfq_group *cfqg;
  3270. rcu_read_lock();
  3271. cfqg = cfq_lookup_cfqg(cfqd, bio_blkcg(bio));
  3272. if (!cfqg) {
  3273. cfqq = &cfqd->oom_cfqq;
  3274. goto out;
  3275. }
  3276. if (!is_sync) {
  3277. if (!ioprio_valid(cic->ioprio)) {
  3278. struct task_struct *tsk = current;
  3279. ioprio = task_nice_ioprio(tsk);
  3280. ioprio_class = task_nice_ioclass(tsk);
  3281. }
  3282. async_cfqq = cfq_async_queue_prio(cfqg, ioprio_class, ioprio);
  3283. cfqq = *async_cfqq;
  3284. if (cfqq)
  3285. goto out;
  3286. }
  3287. cfqq = kmem_cache_alloc_node(cfq_pool,
  3288. GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN,
  3289. cfqd->queue->node);
  3290. if (!cfqq) {
  3291. cfqq = &cfqd->oom_cfqq;
  3292. goto out;
  3293. }
  3294. cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
  3295. cfq_init_prio_data(cfqq, cic);
  3296. cfq_link_cfqq_cfqg(cfqq, cfqg);
  3297. cfq_log_cfqq(cfqd, cfqq, "alloced");
  3298. if (async_cfqq) {
  3299. /* a new async queue is created, pin and remember */
  3300. cfqq->ref++;
  3301. *async_cfqq = cfqq;
  3302. }
  3303. out:
  3304. cfqq->ref++;
  3305. rcu_read_unlock();
  3306. return cfqq;
  3307. }
  3308. static void
  3309. __cfq_update_io_thinktime(struct cfq_ttime *ttime, u64 slice_idle)
  3310. {
  3311. u64 elapsed = ktime_get_ns() - ttime->last_end_request;
  3312. elapsed = min(elapsed, 2UL * slice_idle);
  3313. ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
  3314. ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8);
  3315. ttime->ttime_mean = div64_ul(ttime->ttime_total + 128,
  3316. ttime->ttime_samples);
  3317. }
  3318. static void
  3319. cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  3320. struct cfq_io_cq *cic)
  3321. {
  3322. if (cfq_cfqq_sync(cfqq)) {
  3323. __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
  3324. __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
  3325. cfqd->cfq_slice_idle);
  3326. }
  3327. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3328. __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
  3329. #endif
  3330. }
  3331. static void
  3332. cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  3333. struct request *rq)
  3334. {
  3335. sector_t sdist = 0;
  3336. sector_t n_sec = blk_rq_sectors(rq);
  3337. if (cfqq->last_request_pos) {
  3338. if (cfqq->last_request_pos < blk_rq_pos(rq))
  3339. sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
  3340. else
  3341. sdist = cfqq->last_request_pos - blk_rq_pos(rq);
  3342. }
  3343. cfqq->seek_history <<= 1;
  3344. if (blk_queue_nonrot(cfqd->queue))
  3345. cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
  3346. else
  3347. cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
  3348. }
  3349. static inline bool req_noidle(struct request *req)
  3350. {
  3351. return req_op(req) == REQ_OP_WRITE &&
  3352. (req->cmd_flags & (REQ_SYNC | REQ_IDLE)) == REQ_SYNC;
  3353. }
  3354. /*
  3355. * Disable idle window if the process thinks too long or seeks so much that
  3356. * it doesn't matter
  3357. */
  3358. static void
  3359. cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  3360. struct cfq_io_cq *cic)
  3361. {
  3362. int old_idle, enable_idle;
  3363. /*
  3364. * Don't idle for async or idle io prio class
  3365. */
  3366. if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
  3367. return;
  3368. enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
  3369. if (cfqq->queued[0] + cfqq->queued[1] >= 4)
  3370. cfq_mark_cfqq_deep(cfqq);
  3371. if (cfqq->next_rq && req_noidle(cfqq->next_rq))
  3372. enable_idle = 0;
  3373. else if (!atomic_read(&cic->icq.ioc->active_ref) ||
  3374. !cfqd->cfq_slice_idle ||
  3375. (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
  3376. enable_idle = 0;
  3377. else if (sample_valid(cic->ttime.ttime_samples)) {
  3378. if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
  3379. enable_idle = 0;
  3380. else
  3381. enable_idle = 1;
  3382. }
  3383. if (old_idle != enable_idle) {
  3384. cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
  3385. if (enable_idle)
  3386. cfq_mark_cfqq_idle_window(cfqq);
  3387. else
  3388. cfq_clear_cfqq_idle_window(cfqq);
  3389. }
  3390. }
  3391. /*
  3392. * Check if new_cfqq should preempt the currently active queue. Return 0 for
  3393. * no or if we aren't sure, a 1 will cause a preempt.
  3394. */
  3395. static bool
  3396. cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
  3397. struct request *rq)
  3398. {
  3399. struct cfq_queue *cfqq;
  3400. cfqq = cfqd->active_queue;
  3401. if (!cfqq)
  3402. return false;
  3403. if (cfq_class_idle(new_cfqq))
  3404. return false;
  3405. if (cfq_class_idle(cfqq))
  3406. return true;
  3407. /*
  3408. * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
  3409. */
  3410. if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
  3411. return false;
  3412. /*
  3413. * if the new request is sync, but the currently running queue is
  3414. * not, let the sync request have priority.
  3415. */
  3416. if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
  3417. return true;
  3418. /*
  3419. * Treat ancestors of current cgroup the same way as current cgroup.
  3420. * For anybody else we disallow preemption to guarantee service
  3421. * fairness among cgroups.
  3422. */
  3423. if (!cfqg_is_descendant(cfqq->cfqg, new_cfqq->cfqg))
  3424. return false;
  3425. if (cfq_slice_used(cfqq))
  3426. return true;
  3427. /*
  3428. * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
  3429. */
  3430. if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
  3431. return true;
  3432. WARN_ON_ONCE(cfqq->ioprio_class != new_cfqq->ioprio_class);
  3433. /* Allow preemption only if we are idling on sync-noidle tree */
  3434. if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
  3435. cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
  3436. RB_EMPTY_ROOT(&cfqq->sort_list))
  3437. return true;
  3438. /*
  3439. * So both queues are sync. Let the new request get disk time if
  3440. * it's a metadata request and the current queue is doing regular IO.
  3441. */
  3442. if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
  3443. return true;
  3444. /* An idle queue should not be idle now for some reason */
  3445. if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
  3446. return true;
  3447. if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
  3448. return false;
  3449. /*
  3450. * if this request is as-good as one we would expect from the
  3451. * current cfqq, let it preempt
  3452. */
  3453. if (cfq_rq_close(cfqd, cfqq, rq))
  3454. return true;
  3455. return false;
  3456. }
  3457. /*
  3458. * cfqq preempts the active queue. if we allowed preempt with no slice left,
  3459. * let it have half of its nominal slice.
  3460. */
  3461. static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  3462. {
  3463. enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
  3464. cfq_log_cfqq(cfqd, cfqq, "preempt");
  3465. cfq_slice_expired(cfqd, 1);
  3466. /*
  3467. * workload type is changed, don't save slice, otherwise preempt
  3468. * doesn't happen
  3469. */
  3470. if (old_type != cfqq_type(cfqq))
  3471. cfqq->cfqg->saved_wl_slice = 0;
  3472. /*
  3473. * Put the new queue at the front of the of the current list,
  3474. * so we know that it will be selected next.
  3475. */
  3476. BUG_ON(!cfq_cfqq_on_rr(cfqq));
  3477. cfq_service_tree_add(cfqd, cfqq, 1);
  3478. cfqq->slice_end = 0;
  3479. cfq_mark_cfqq_slice_new(cfqq);
  3480. }
  3481. /*
  3482. * Called when a new fs request (rq) is added (to cfqq). Check if there's
  3483. * something we should do about it
  3484. */
  3485. static void
  3486. cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  3487. struct request *rq)
  3488. {
  3489. struct cfq_io_cq *cic = RQ_CIC(rq);
  3490. cfqd->rq_queued++;
  3491. if (rq->cmd_flags & REQ_PRIO)
  3492. cfqq->prio_pending++;
  3493. cfq_update_io_thinktime(cfqd, cfqq, cic);
  3494. cfq_update_io_seektime(cfqd, cfqq, rq);
  3495. cfq_update_idle_window(cfqd, cfqq, cic);
  3496. cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
  3497. if (cfqq == cfqd->active_queue) {
  3498. /*
  3499. * Remember that we saw a request from this process, but
  3500. * don't start queuing just yet. Otherwise we risk seeing lots
  3501. * of tiny requests, because we disrupt the normal plugging
  3502. * and merging. If the request is already larger than a single
  3503. * page, let it rip immediately. For that case we assume that
  3504. * merging is already done. Ditto for a busy system that
  3505. * has other work pending, don't risk delaying until the
  3506. * idle timer unplug to continue working.
  3507. */
  3508. if (cfq_cfqq_wait_request(cfqq)) {
  3509. if (blk_rq_bytes(rq) > PAGE_SIZE ||
  3510. cfqd->busy_queues > 1) {
  3511. cfq_del_timer(cfqd, cfqq);
  3512. cfq_clear_cfqq_wait_request(cfqq);
  3513. __blk_run_queue(cfqd->queue);
  3514. } else {
  3515. cfqg_stats_update_idle_time(cfqq->cfqg);
  3516. cfq_mark_cfqq_must_dispatch(cfqq);
  3517. }
  3518. }
  3519. } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
  3520. /*
  3521. * not the active queue - expire current slice if it is
  3522. * idle and has expired it's mean thinktime or this new queue
  3523. * has some old slice time left and is of higher priority or
  3524. * this new queue is RT and the current one is BE
  3525. */
  3526. cfq_preempt_queue(cfqd, cfqq);
  3527. __blk_run_queue(cfqd->queue);
  3528. }
  3529. }
  3530. static void cfq_insert_request(struct request_queue *q, struct request *rq)
  3531. {
  3532. struct cfq_data *cfqd = q->elevator->elevator_data;
  3533. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  3534. cfq_log_cfqq(cfqd, cfqq, "insert_request");
  3535. cfq_init_prio_data(cfqq, RQ_CIC(rq));
  3536. rq->fifo_time = ktime_get_ns() + cfqd->cfq_fifo_expire[rq_is_sync(rq)];
  3537. list_add_tail(&rq->queuelist, &cfqq->fifo);
  3538. cfq_add_rq_rb(rq);
  3539. cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
  3540. rq->cmd_flags);
  3541. cfq_rq_enqueued(cfqd, cfqq, rq);
  3542. }
  3543. /*
  3544. * Update hw_tag based on peak queue depth over 50 samples under
  3545. * sufficient load.
  3546. */
  3547. static void cfq_update_hw_tag(struct cfq_data *cfqd)
  3548. {
  3549. struct cfq_queue *cfqq = cfqd->active_queue;
  3550. if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
  3551. cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
  3552. if (cfqd->hw_tag == 1)
  3553. return;
  3554. if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
  3555. cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
  3556. return;
  3557. /*
  3558. * If active queue hasn't enough requests and can idle, cfq might not
  3559. * dispatch sufficient requests to hardware. Don't zero hw_tag in this
  3560. * case
  3561. */
  3562. if (cfqq && cfq_cfqq_idle_window(cfqq) &&
  3563. cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
  3564. CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
  3565. return;
  3566. if (cfqd->hw_tag_samples++ < 50)
  3567. return;
  3568. if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
  3569. cfqd->hw_tag = 1;
  3570. else
  3571. cfqd->hw_tag = 0;
  3572. }
  3573. static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  3574. {
  3575. struct cfq_io_cq *cic = cfqd->active_cic;
  3576. u64 now = ktime_get_ns();
  3577. /* If the queue already has requests, don't wait */
  3578. if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  3579. return false;
  3580. /* If there are other queues in the group, don't wait */
  3581. if (cfqq->cfqg->nr_cfqq > 1)
  3582. return false;
  3583. /* the only queue in the group, but think time is big */
  3584. if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
  3585. return false;
  3586. if (cfq_slice_used(cfqq))
  3587. return true;
  3588. /* if slice left is less than think time, wait busy */
  3589. if (cic && sample_valid(cic->ttime.ttime_samples)
  3590. && (cfqq->slice_end - now < cic->ttime.ttime_mean))
  3591. return true;
  3592. /*
  3593. * If think times is less than a jiffy than ttime_mean=0 and above
  3594. * will not be true. It might happen that slice has not expired yet
  3595. * but will expire soon (4-5 ns) during select_queue(). To cover the
  3596. * case where think time is less than a jiffy, mark the queue wait
  3597. * busy if only 1 jiffy is left in the slice.
  3598. */
  3599. if (cfqq->slice_end - now <= jiffies_to_nsecs(1))
  3600. return true;
  3601. return false;
  3602. }
  3603. static void cfq_completed_request(struct request_queue *q, struct request *rq)
  3604. {
  3605. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  3606. struct cfq_data *cfqd = cfqq->cfqd;
  3607. const int sync = rq_is_sync(rq);
  3608. u64 now = ktime_get_ns();
  3609. cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", req_noidle(rq));
  3610. cfq_update_hw_tag(cfqd);
  3611. WARN_ON(!cfqd->rq_in_driver);
  3612. WARN_ON(!cfqq->dispatched);
  3613. cfqd->rq_in_driver--;
  3614. cfqq->dispatched--;
  3615. (RQ_CFQG(rq))->dispatched--;
  3616. cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
  3617. rq_io_start_time_ns(rq), rq->cmd_flags);
  3618. cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
  3619. if (sync) {
  3620. struct cfq_rb_root *st;
  3621. RQ_CIC(rq)->ttime.last_end_request = now;
  3622. if (cfq_cfqq_on_rr(cfqq))
  3623. st = cfqq->service_tree;
  3624. else
  3625. st = st_for(cfqq->cfqg, cfqq_class(cfqq),
  3626. cfqq_type(cfqq));
  3627. st->ttime.last_end_request = now;
  3628. /*
  3629. * We have to do this check in jiffies since start_time is in
  3630. * jiffies and it is not trivial to convert to ns. If
  3631. * cfq_fifo_expire[1] ever comes close to 1 jiffie, this test
  3632. * will become problematic but so far we are fine (the default
  3633. * is 128 ms).
  3634. */
  3635. if (!time_after(rq->start_time +
  3636. nsecs_to_jiffies(cfqd->cfq_fifo_expire[1]),
  3637. jiffies))
  3638. cfqd->last_delayed_sync = now;
  3639. }
  3640. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3641. cfqq->cfqg->ttime.last_end_request = now;
  3642. #endif
  3643. /*
  3644. * If this is the active queue, check if it needs to be expired,
  3645. * or if we want to idle in case it has no pending requests.
  3646. */
  3647. if (cfqd->active_queue == cfqq) {
  3648. const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
  3649. if (cfq_cfqq_slice_new(cfqq)) {
  3650. cfq_set_prio_slice(cfqd, cfqq);
  3651. cfq_clear_cfqq_slice_new(cfqq);
  3652. }
  3653. /*
  3654. * Should we wait for next request to come in before we expire
  3655. * the queue.
  3656. */
  3657. if (cfq_should_wait_busy(cfqd, cfqq)) {
  3658. u64 extend_sl = cfqd->cfq_slice_idle;
  3659. if (!cfqd->cfq_slice_idle)
  3660. extend_sl = cfqd->cfq_group_idle;
  3661. cfqq->slice_end = now + extend_sl;
  3662. cfq_mark_cfqq_wait_busy(cfqq);
  3663. cfq_log_cfqq(cfqd, cfqq, "will busy wait");
  3664. }
  3665. /*
  3666. * Idling is not enabled on:
  3667. * - expired queues
  3668. * - idle-priority queues
  3669. * - async queues
  3670. * - queues with still some requests queued
  3671. * - when there is a close cooperator
  3672. */
  3673. if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
  3674. cfq_slice_expired(cfqd, 1);
  3675. else if (sync && cfqq_empty &&
  3676. !cfq_close_cooperator(cfqd, cfqq)) {
  3677. cfq_arm_slice_timer(cfqd);
  3678. }
  3679. }
  3680. if (!cfqd->rq_in_driver)
  3681. cfq_schedule_dispatch(cfqd);
  3682. }
  3683. static void cfqq_boost_on_prio(struct cfq_queue *cfqq, unsigned int op)
  3684. {
  3685. /*
  3686. * If REQ_PRIO is set, boost class and prio level, if it's below
  3687. * BE/NORM. If prio is not set, restore the potentially boosted
  3688. * class/prio level.
  3689. */
  3690. if (!(op & REQ_PRIO)) {
  3691. cfqq->ioprio_class = cfqq->org_ioprio_class;
  3692. cfqq->ioprio = cfqq->org_ioprio;
  3693. } else {
  3694. if (cfq_class_idle(cfqq))
  3695. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  3696. if (cfqq->ioprio > IOPRIO_NORM)
  3697. cfqq->ioprio = IOPRIO_NORM;
  3698. }
  3699. }
  3700. static inline int __cfq_may_queue(struct cfq_queue *cfqq)
  3701. {
  3702. if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
  3703. cfq_mark_cfqq_must_alloc_slice(cfqq);
  3704. return ELV_MQUEUE_MUST;
  3705. }
  3706. return ELV_MQUEUE_MAY;
  3707. }
  3708. static int cfq_may_queue(struct request_queue *q, unsigned int op)
  3709. {
  3710. struct cfq_data *cfqd = q->elevator->elevator_data;
  3711. struct task_struct *tsk = current;
  3712. struct cfq_io_cq *cic;
  3713. struct cfq_queue *cfqq;
  3714. /*
  3715. * don't force setup of a queue from here, as a call to may_queue
  3716. * does not necessarily imply that a request actually will be queued.
  3717. * so just lookup a possibly existing queue, or return 'may queue'
  3718. * if that fails
  3719. */
  3720. cic = cfq_cic_lookup(cfqd, tsk->io_context);
  3721. if (!cic)
  3722. return ELV_MQUEUE_MAY;
  3723. cfqq = cic_to_cfqq(cic, op_is_sync(op));
  3724. if (cfqq) {
  3725. cfq_init_prio_data(cfqq, cic);
  3726. cfqq_boost_on_prio(cfqq, op);
  3727. return __cfq_may_queue(cfqq);
  3728. }
  3729. return ELV_MQUEUE_MAY;
  3730. }
  3731. /*
  3732. * queue lock held here
  3733. */
  3734. static void cfq_put_request(struct request *rq)
  3735. {
  3736. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  3737. if (cfqq) {
  3738. const int rw = rq_data_dir(rq);
  3739. BUG_ON(!cfqq->allocated[rw]);
  3740. cfqq->allocated[rw]--;
  3741. /* Put down rq reference on cfqg */
  3742. cfqg_put(RQ_CFQG(rq));
  3743. rq->elv.priv[0] = NULL;
  3744. rq->elv.priv[1] = NULL;
  3745. cfq_put_queue(cfqq);
  3746. }
  3747. }
  3748. static struct cfq_queue *
  3749. cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
  3750. struct cfq_queue *cfqq)
  3751. {
  3752. cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
  3753. cic_set_cfqq(cic, cfqq->new_cfqq, 1);
  3754. cfq_mark_cfqq_coop(cfqq->new_cfqq);
  3755. cfq_put_queue(cfqq);
  3756. return cic_to_cfqq(cic, 1);
  3757. }
  3758. /*
  3759. * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
  3760. * was the last process referring to said cfqq.
  3761. */
  3762. static struct cfq_queue *
  3763. split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
  3764. {
  3765. if (cfqq_process_refs(cfqq) == 1) {
  3766. cfqq->pid = current->pid;
  3767. cfq_clear_cfqq_coop(cfqq);
  3768. cfq_clear_cfqq_split_coop(cfqq);
  3769. return cfqq;
  3770. }
  3771. cic_set_cfqq(cic, NULL, 1);
  3772. cfq_put_cooperator(cfqq);
  3773. cfq_put_queue(cfqq);
  3774. return NULL;
  3775. }
  3776. /*
  3777. * Allocate cfq data structures associated with this request.
  3778. */
  3779. static int
  3780. cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
  3781. gfp_t gfp_mask)
  3782. {
  3783. struct cfq_data *cfqd = q->elevator->elevator_data;
  3784. struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
  3785. const int rw = rq_data_dir(rq);
  3786. const bool is_sync = rq_is_sync(rq);
  3787. struct cfq_queue *cfqq;
  3788. spin_lock_irq(q->queue_lock);
  3789. check_ioprio_changed(cic, bio);
  3790. check_blkcg_changed(cic, bio);
  3791. new_queue:
  3792. cfqq = cic_to_cfqq(cic, is_sync);
  3793. if (!cfqq || cfqq == &cfqd->oom_cfqq) {
  3794. if (cfqq)
  3795. cfq_put_queue(cfqq);
  3796. cfqq = cfq_get_queue(cfqd, is_sync, cic, bio);
  3797. cic_set_cfqq(cic, cfqq, is_sync);
  3798. } else {
  3799. /*
  3800. * If the queue was seeky for too long, break it apart.
  3801. */
  3802. if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
  3803. cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
  3804. cfqq = split_cfqq(cic, cfqq);
  3805. if (!cfqq)
  3806. goto new_queue;
  3807. }
  3808. /*
  3809. * Check to see if this queue is scheduled to merge with
  3810. * another, closely cooperating queue. The merging of
  3811. * queues happens here as it must be done in process context.
  3812. * The reference on new_cfqq was taken in merge_cfqqs.
  3813. */
  3814. if (cfqq->new_cfqq)
  3815. cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
  3816. }
  3817. cfqq->allocated[rw]++;
  3818. cfqq->ref++;
  3819. cfqg_get(cfqq->cfqg);
  3820. rq->elv.priv[0] = cfqq;
  3821. rq->elv.priv[1] = cfqq->cfqg;
  3822. spin_unlock_irq(q->queue_lock);
  3823. return 0;
  3824. }
  3825. static void cfq_kick_queue(struct work_struct *work)
  3826. {
  3827. struct cfq_data *cfqd =
  3828. container_of(work, struct cfq_data, unplug_work);
  3829. struct request_queue *q = cfqd->queue;
  3830. spin_lock_irq(q->queue_lock);
  3831. __blk_run_queue(cfqd->queue);
  3832. spin_unlock_irq(q->queue_lock);
  3833. }
  3834. /*
  3835. * Timer running if the active_queue is currently idling inside its time slice
  3836. */
  3837. static enum hrtimer_restart cfq_idle_slice_timer(struct hrtimer *timer)
  3838. {
  3839. struct cfq_data *cfqd = container_of(timer, struct cfq_data,
  3840. idle_slice_timer);
  3841. struct cfq_queue *cfqq;
  3842. unsigned long flags;
  3843. int timed_out = 1;
  3844. cfq_log(cfqd, "idle timer fired");
  3845. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  3846. cfqq = cfqd->active_queue;
  3847. if (cfqq) {
  3848. timed_out = 0;
  3849. /*
  3850. * We saw a request before the queue expired, let it through
  3851. */
  3852. if (cfq_cfqq_must_dispatch(cfqq))
  3853. goto out_kick;
  3854. /*
  3855. * expired
  3856. */
  3857. if (cfq_slice_used(cfqq))
  3858. goto expire;
  3859. /*
  3860. * only expire and reinvoke request handler, if there are
  3861. * other queues with pending requests
  3862. */
  3863. if (!cfqd->busy_queues)
  3864. goto out_cont;
  3865. /*
  3866. * not expired and it has a request pending, let it dispatch
  3867. */
  3868. if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  3869. goto out_kick;
  3870. /*
  3871. * Queue depth flag is reset only when the idle didn't succeed
  3872. */
  3873. cfq_clear_cfqq_deep(cfqq);
  3874. }
  3875. expire:
  3876. cfq_slice_expired(cfqd, timed_out);
  3877. out_kick:
  3878. cfq_schedule_dispatch(cfqd);
  3879. out_cont:
  3880. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  3881. return HRTIMER_NORESTART;
  3882. }
  3883. static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
  3884. {
  3885. hrtimer_cancel(&cfqd->idle_slice_timer);
  3886. cancel_work_sync(&cfqd->unplug_work);
  3887. }
  3888. static void cfq_exit_queue(struct elevator_queue *e)
  3889. {
  3890. struct cfq_data *cfqd = e->elevator_data;
  3891. struct request_queue *q = cfqd->queue;
  3892. cfq_shutdown_timer_wq(cfqd);
  3893. spin_lock_irq(q->queue_lock);
  3894. if (cfqd->active_queue)
  3895. __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
  3896. spin_unlock_irq(q->queue_lock);
  3897. cfq_shutdown_timer_wq(cfqd);
  3898. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3899. blkcg_deactivate_policy(q, &blkcg_policy_cfq);
  3900. #else
  3901. kfree(cfqd->root_group);
  3902. #endif
  3903. kfree(cfqd);
  3904. }
  3905. static int cfq_init_queue(struct request_queue *q, struct elevator_type *e)
  3906. {
  3907. struct cfq_data *cfqd;
  3908. struct blkcg_gq *blkg __maybe_unused;
  3909. int i, ret;
  3910. struct elevator_queue *eq;
  3911. eq = elevator_alloc(q, e);
  3912. if (!eq)
  3913. return -ENOMEM;
  3914. cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
  3915. if (!cfqd) {
  3916. kobject_put(&eq->kobj);
  3917. return -ENOMEM;
  3918. }
  3919. eq->elevator_data = cfqd;
  3920. cfqd->queue = q;
  3921. spin_lock_irq(q->queue_lock);
  3922. q->elevator = eq;
  3923. spin_unlock_irq(q->queue_lock);
  3924. /* Init root service tree */
  3925. cfqd->grp_service_tree = CFQ_RB_ROOT;
  3926. /* Init root group and prefer root group over other groups by default */
  3927. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3928. ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
  3929. if (ret)
  3930. goto out_free;
  3931. cfqd->root_group = blkg_to_cfqg(q->root_blkg);
  3932. #else
  3933. ret = -ENOMEM;
  3934. cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
  3935. GFP_KERNEL, cfqd->queue->node);
  3936. if (!cfqd->root_group)
  3937. goto out_free;
  3938. cfq_init_cfqg_base(cfqd->root_group);
  3939. cfqd->root_group->weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
  3940. cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
  3941. #endif
  3942. /*
  3943. * Not strictly needed (since RB_ROOT just clears the node and we
  3944. * zeroed cfqd on alloc), but better be safe in case someone decides
  3945. * to add magic to the rb code
  3946. */
  3947. for (i = 0; i < CFQ_PRIO_LISTS; i++)
  3948. cfqd->prio_trees[i] = RB_ROOT;
  3949. /*
  3950. * Our fallback cfqq if cfq_get_queue() runs into OOM issues.
  3951. * Grab a permanent reference to it, so that the normal code flow
  3952. * will not attempt to free it. oom_cfqq is linked to root_group
  3953. * but shouldn't hold a reference as it'll never be unlinked. Lose
  3954. * the reference from linking right away.
  3955. */
  3956. cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
  3957. cfqd->oom_cfqq.ref++;
  3958. spin_lock_irq(q->queue_lock);
  3959. cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
  3960. cfqg_put(cfqd->root_group);
  3961. spin_unlock_irq(q->queue_lock);
  3962. hrtimer_init(&cfqd->idle_slice_timer, CLOCK_MONOTONIC,
  3963. HRTIMER_MODE_REL);
  3964. cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
  3965. INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
  3966. cfqd->cfq_quantum = cfq_quantum;
  3967. cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
  3968. cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
  3969. cfqd->cfq_back_max = cfq_back_max;
  3970. cfqd->cfq_back_penalty = cfq_back_penalty;
  3971. cfqd->cfq_slice[0] = cfq_slice_async;
  3972. cfqd->cfq_slice[1] = cfq_slice_sync;
  3973. cfqd->cfq_target_latency = cfq_target_latency;
  3974. cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
  3975. cfqd->cfq_slice_idle = cfq_slice_idle;
  3976. cfqd->cfq_group_idle = cfq_group_idle;
  3977. cfqd->cfq_latency = 1;
  3978. cfqd->hw_tag = -1;
  3979. /*
  3980. * we optimistically start assuming sync ops weren't delayed in last
  3981. * second, in order to have larger depth for async operations.
  3982. */
  3983. cfqd->last_delayed_sync = ktime_get_ns() - NSEC_PER_SEC;
  3984. return 0;
  3985. out_free:
  3986. kfree(cfqd);
  3987. kobject_put(&eq->kobj);
  3988. return ret;
  3989. }
  3990. static void cfq_registered_queue(struct request_queue *q)
  3991. {
  3992. struct elevator_queue *e = q->elevator;
  3993. struct cfq_data *cfqd = e->elevator_data;
  3994. /*
  3995. * Default to IOPS mode with no idling for SSDs
  3996. */
  3997. if (blk_queue_nonrot(q))
  3998. cfqd->cfq_slice_idle = 0;
  3999. }
  4000. /*
  4001. * sysfs parts below -->
  4002. */
  4003. static ssize_t
  4004. cfq_var_show(unsigned int var, char *page)
  4005. {
  4006. return sprintf(page, "%u\n", var);
  4007. }
  4008. static ssize_t
  4009. cfq_var_store(unsigned int *var, const char *page, size_t count)
  4010. {
  4011. char *p = (char *) page;
  4012. *var = simple_strtoul(p, &p, 10);
  4013. return count;
  4014. }
  4015. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  4016. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  4017. { \
  4018. struct cfq_data *cfqd = e->elevator_data; \
  4019. u64 __data = __VAR; \
  4020. if (__CONV) \
  4021. __data = div_u64(__data, NSEC_PER_MSEC); \
  4022. return cfq_var_show(__data, (page)); \
  4023. }
  4024. SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
  4025. SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
  4026. SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
  4027. SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
  4028. SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
  4029. SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
  4030. SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
  4031. SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
  4032. SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
  4033. SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
  4034. SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
  4035. SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
  4036. #undef SHOW_FUNCTION
  4037. #define USEC_SHOW_FUNCTION(__FUNC, __VAR) \
  4038. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  4039. { \
  4040. struct cfq_data *cfqd = e->elevator_data; \
  4041. u64 __data = __VAR; \
  4042. __data = div_u64(__data, NSEC_PER_USEC); \
  4043. return cfq_var_show(__data, (page)); \
  4044. }
  4045. USEC_SHOW_FUNCTION(cfq_slice_idle_us_show, cfqd->cfq_slice_idle);
  4046. USEC_SHOW_FUNCTION(cfq_group_idle_us_show, cfqd->cfq_group_idle);
  4047. USEC_SHOW_FUNCTION(cfq_slice_sync_us_show, cfqd->cfq_slice[1]);
  4048. USEC_SHOW_FUNCTION(cfq_slice_async_us_show, cfqd->cfq_slice[0]);
  4049. USEC_SHOW_FUNCTION(cfq_target_latency_us_show, cfqd->cfq_target_latency);
  4050. #undef USEC_SHOW_FUNCTION
  4051. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  4052. static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
  4053. { \
  4054. struct cfq_data *cfqd = e->elevator_data; \
  4055. unsigned int __data; \
  4056. int ret = cfq_var_store(&__data, (page), count); \
  4057. if (__data < (MIN)) \
  4058. __data = (MIN); \
  4059. else if (__data > (MAX)) \
  4060. __data = (MAX); \
  4061. if (__CONV) \
  4062. *(__PTR) = (u64)__data * NSEC_PER_MSEC; \
  4063. else \
  4064. *(__PTR) = __data; \
  4065. return ret; \
  4066. }
  4067. STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
  4068. STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
  4069. UINT_MAX, 1);
  4070. STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
  4071. UINT_MAX, 1);
  4072. STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
  4073. STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
  4074. UINT_MAX, 0);
  4075. STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
  4076. STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
  4077. STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
  4078. STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
  4079. STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
  4080. UINT_MAX, 0);
  4081. STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
  4082. STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
  4083. #undef STORE_FUNCTION
  4084. #define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
  4085. static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
  4086. { \
  4087. struct cfq_data *cfqd = e->elevator_data; \
  4088. unsigned int __data; \
  4089. int ret = cfq_var_store(&__data, (page), count); \
  4090. if (__data < (MIN)) \
  4091. __data = (MIN); \
  4092. else if (__data > (MAX)) \
  4093. __data = (MAX); \
  4094. *(__PTR) = (u64)__data * NSEC_PER_USEC; \
  4095. return ret; \
  4096. }
  4097. USEC_STORE_FUNCTION(cfq_slice_idle_us_store, &cfqd->cfq_slice_idle, 0, UINT_MAX);
  4098. USEC_STORE_FUNCTION(cfq_group_idle_us_store, &cfqd->cfq_group_idle, 0, UINT_MAX);
  4099. USEC_STORE_FUNCTION(cfq_slice_sync_us_store, &cfqd->cfq_slice[1], 1, UINT_MAX);
  4100. USEC_STORE_FUNCTION(cfq_slice_async_us_store, &cfqd->cfq_slice[0], 1, UINT_MAX);
  4101. USEC_STORE_FUNCTION(cfq_target_latency_us_store, &cfqd->cfq_target_latency, 1, UINT_MAX);
  4102. #undef USEC_STORE_FUNCTION
  4103. #define CFQ_ATTR(name) \
  4104. __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
  4105. static struct elv_fs_entry cfq_attrs[] = {
  4106. CFQ_ATTR(quantum),
  4107. CFQ_ATTR(fifo_expire_sync),
  4108. CFQ_ATTR(fifo_expire_async),
  4109. CFQ_ATTR(back_seek_max),
  4110. CFQ_ATTR(back_seek_penalty),
  4111. CFQ_ATTR(slice_sync),
  4112. CFQ_ATTR(slice_sync_us),
  4113. CFQ_ATTR(slice_async),
  4114. CFQ_ATTR(slice_async_us),
  4115. CFQ_ATTR(slice_async_rq),
  4116. CFQ_ATTR(slice_idle),
  4117. CFQ_ATTR(slice_idle_us),
  4118. CFQ_ATTR(group_idle),
  4119. CFQ_ATTR(group_idle_us),
  4120. CFQ_ATTR(low_latency),
  4121. CFQ_ATTR(target_latency),
  4122. CFQ_ATTR(target_latency_us),
  4123. __ATTR_NULL
  4124. };
  4125. static struct elevator_type iosched_cfq = {
  4126. .ops = {
  4127. .elevator_merge_fn = cfq_merge,
  4128. .elevator_merged_fn = cfq_merged_request,
  4129. .elevator_merge_req_fn = cfq_merged_requests,
  4130. .elevator_allow_bio_merge_fn = cfq_allow_bio_merge,
  4131. .elevator_allow_rq_merge_fn = cfq_allow_rq_merge,
  4132. .elevator_bio_merged_fn = cfq_bio_merged,
  4133. .elevator_dispatch_fn = cfq_dispatch_requests,
  4134. .elevator_add_req_fn = cfq_insert_request,
  4135. .elevator_activate_req_fn = cfq_activate_request,
  4136. .elevator_deactivate_req_fn = cfq_deactivate_request,
  4137. .elevator_completed_req_fn = cfq_completed_request,
  4138. .elevator_former_req_fn = elv_rb_former_request,
  4139. .elevator_latter_req_fn = elv_rb_latter_request,
  4140. .elevator_init_icq_fn = cfq_init_icq,
  4141. .elevator_exit_icq_fn = cfq_exit_icq,
  4142. .elevator_set_req_fn = cfq_set_request,
  4143. .elevator_put_req_fn = cfq_put_request,
  4144. .elevator_may_queue_fn = cfq_may_queue,
  4145. .elevator_init_fn = cfq_init_queue,
  4146. .elevator_exit_fn = cfq_exit_queue,
  4147. .elevator_registered_fn = cfq_registered_queue,
  4148. },
  4149. .icq_size = sizeof(struct cfq_io_cq),
  4150. .icq_align = __alignof__(struct cfq_io_cq),
  4151. .elevator_attrs = cfq_attrs,
  4152. .elevator_name = "cfq",
  4153. .elevator_owner = THIS_MODULE,
  4154. };
  4155. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  4156. static struct blkcg_policy blkcg_policy_cfq = {
  4157. .dfl_cftypes = cfq_blkcg_files,
  4158. .legacy_cftypes = cfq_blkcg_legacy_files,
  4159. .cpd_alloc_fn = cfq_cpd_alloc,
  4160. .cpd_init_fn = cfq_cpd_init,
  4161. .cpd_free_fn = cfq_cpd_free,
  4162. .cpd_bind_fn = cfq_cpd_bind,
  4163. .pd_alloc_fn = cfq_pd_alloc,
  4164. .pd_init_fn = cfq_pd_init,
  4165. .pd_offline_fn = cfq_pd_offline,
  4166. .pd_free_fn = cfq_pd_free,
  4167. .pd_reset_stats_fn = cfq_pd_reset_stats,
  4168. };
  4169. #endif
  4170. static int __init cfq_init(void)
  4171. {
  4172. int ret;
  4173. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  4174. ret = blkcg_policy_register(&blkcg_policy_cfq);
  4175. if (ret)
  4176. return ret;
  4177. #else
  4178. cfq_group_idle = 0;
  4179. #endif
  4180. ret = -ENOMEM;
  4181. cfq_pool = KMEM_CACHE(cfq_queue, 0);
  4182. if (!cfq_pool)
  4183. goto err_pol_unreg;
  4184. ret = elv_register(&iosched_cfq);
  4185. if (ret)
  4186. goto err_free_pool;
  4187. return 0;
  4188. err_free_pool:
  4189. kmem_cache_destroy(cfq_pool);
  4190. err_pol_unreg:
  4191. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  4192. blkcg_policy_unregister(&blkcg_policy_cfq);
  4193. #endif
  4194. return ret;
  4195. }
  4196. static void __exit cfq_exit(void)
  4197. {
  4198. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  4199. blkcg_policy_unregister(&blkcg_policy_cfq);
  4200. #endif
  4201. elv_unregister(&iosched_cfq);
  4202. kmem_cache_destroy(cfq_pool);
  4203. }
  4204. module_init(cfq_init);
  4205. module_exit(cfq_exit);
  4206. MODULE_AUTHOR("Jens Axboe");
  4207. MODULE_LICENSE("GPL");
  4208. MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");