cfq-iosched.c 105 KB

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