cfq-iosched.c 123 KB

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