cfq-iosched.c 64 KB

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