cfq-iosched.c 84 KB

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