cfq-iosched.c 64 KB

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