cfq-iosched.c 79 KB

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