cfq-iosched.c 77 KB

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