cfq-iosched.c 108 KB

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