cfq-iosched.c 105 KB

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