cfq-iosched.c 114 KB

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