cfq-iosched.c 116 KB

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