cfq-iosched.c 128 KB

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