cfq-iosched.c 127 KB

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