cfq-iosched.c 98 KB

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