cfq-iosched.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  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@suse.de>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/elevator.h>
  12. #include <linux/hash.h>
  13. #include <linux/rbtree.h>
  14. #include <linux/ioprio.h>
  15. /*
  16. * tunables
  17. */
  18. static const int cfq_quantum = 4; /* max queue in one round of service */
  19. static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
  20. static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
  21. static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
  22. static const int cfq_slice_sync = HZ / 10;
  23. static int cfq_slice_async = HZ / 25;
  24. static const int cfq_slice_async_rq = 2;
  25. static int cfq_slice_idle = HZ / 125;
  26. #define CFQ_IDLE_GRACE (HZ / 10)
  27. #define CFQ_SLICE_SCALE (5)
  28. #define CFQ_KEY_ASYNC (0)
  29. /*
  30. * for the hash of cfqq inside the cfqd
  31. */
  32. #define CFQ_QHASH_SHIFT 6
  33. #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
  34. #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
  35. #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
  36. #define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
  37. #define RQ_CFQQ(rq) ((rq)->elevator_private2)
  38. static kmem_cache_t *cfq_pool;
  39. static kmem_cache_t *cfq_ioc_pool;
  40. static DEFINE_PER_CPU(unsigned long, ioc_count);
  41. static struct completion *ioc_gone;
  42. #define CFQ_PRIO_LISTS IOPRIO_BE_NR
  43. #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
  44. #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
  45. #define ASYNC (0)
  46. #define SYNC (1)
  47. #define cfq_cfqq_dispatched(cfqq) \
  48. ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
  49. #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
  50. #define cfq_cfqq_sync(cfqq) \
  51. (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
  52. #define sample_valid(samples) ((samples) > 80)
  53. /*
  54. * Per block device queue structure
  55. */
  56. struct cfq_data {
  57. request_queue_t *queue;
  58. /*
  59. * rr list of queues with requests and the count of them
  60. */
  61. struct list_head rr_list[CFQ_PRIO_LISTS];
  62. struct list_head busy_rr;
  63. struct list_head cur_rr;
  64. struct list_head idle_rr;
  65. unsigned int busy_queues;
  66. /*
  67. * non-ordered list of empty cfqq's
  68. */
  69. struct list_head empty_list;
  70. /*
  71. * cfqq lookup hash
  72. */
  73. struct hlist_head *cfq_hash;
  74. int rq_in_driver;
  75. int hw_tag;
  76. /*
  77. * idle window management
  78. */
  79. struct timer_list idle_slice_timer;
  80. struct work_struct unplug_work;
  81. struct cfq_queue *active_queue;
  82. struct cfq_io_context *active_cic;
  83. int cur_prio, cur_end_prio;
  84. unsigned int dispatch_slice;
  85. struct timer_list idle_class_timer;
  86. sector_t last_sector;
  87. unsigned long last_end_request;
  88. /*
  89. * tunables, see top of file
  90. */
  91. unsigned int cfq_quantum;
  92. unsigned int cfq_fifo_expire[2];
  93. unsigned int cfq_back_penalty;
  94. unsigned int cfq_back_max;
  95. unsigned int cfq_slice[2];
  96. unsigned int cfq_slice_async_rq;
  97. unsigned int cfq_slice_idle;
  98. struct list_head cic_list;
  99. };
  100. /*
  101. * Per process-grouping structure
  102. */
  103. struct cfq_queue {
  104. /* reference count */
  105. atomic_t ref;
  106. /* parent cfq_data */
  107. struct cfq_data *cfqd;
  108. /* cfqq lookup hash */
  109. struct hlist_node cfq_hash;
  110. /* hash key */
  111. unsigned int key;
  112. /* on either rr or empty list of cfqd */
  113. struct list_head cfq_list;
  114. /* sorted list of pending requests */
  115. struct rb_root sort_list;
  116. /* if fifo isn't expired, next request to serve */
  117. struct request *next_rq;
  118. /* requests queued in sort_list */
  119. int queued[2];
  120. /* currently allocated requests */
  121. int allocated[2];
  122. /* fifo list of requests in sort_list */
  123. struct list_head fifo;
  124. unsigned long slice_start;
  125. unsigned long slice_end;
  126. unsigned long slice_left;
  127. unsigned long service_last;
  128. /* number of requests that are on the dispatch list */
  129. int on_dispatch[2];
  130. /* io prio of this group */
  131. unsigned short ioprio, org_ioprio;
  132. unsigned short ioprio_class, org_ioprio_class;
  133. /* various state flags, see below */
  134. unsigned int flags;
  135. };
  136. enum cfqq_state_flags {
  137. CFQ_CFQQ_FLAG_on_rr = 0,
  138. CFQ_CFQQ_FLAG_wait_request,
  139. CFQ_CFQQ_FLAG_must_alloc,
  140. CFQ_CFQQ_FLAG_must_alloc_slice,
  141. CFQ_CFQQ_FLAG_must_dispatch,
  142. CFQ_CFQQ_FLAG_fifo_expire,
  143. CFQ_CFQQ_FLAG_idle_window,
  144. CFQ_CFQQ_FLAG_prio_changed,
  145. };
  146. #define CFQ_CFQQ_FNS(name) \
  147. static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
  148. { \
  149. cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
  150. } \
  151. static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
  152. { \
  153. cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
  154. } \
  155. static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
  156. { \
  157. return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
  158. }
  159. CFQ_CFQQ_FNS(on_rr);
  160. CFQ_CFQQ_FNS(wait_request);
  161. CFQ_CFQQ_FNS(must_alloc);
  162. CFQ_CFQQ_FNS(must_alloc_slice);
  163. CFQ_CFQQ_FNS(must_dispatch);
  164. CFQ_CFQQ_FNS(fifo_expire);
  165. CFQ_CFQQ_FNS(idle_window);
  166. CFQ_CFQQ_FNS(prio_changed);
  167. #undef CFQ_CFQQ_FNS
  168. static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
  169. static void cfq_dispatch_insert(request_queue_t *, struct request *);
  170. static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk, gfp_t gfp_mask);
  171. /*
  172. * scheduler run of queue, if there are requests pending and no one in the
  173. * driver that will restart queueing
  174. */
  175. static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
  176. {
  177. if (cfqd->busy_queues)
  178. kblockd_schedule_work(&cfqd->unplug_work);
  179. }
  180. static int cfq_queue_empty(request_queue_t *q)
  181. {
  182. struct cfq_data *cfqd = q->elevator->elevator_data;
  183. return !cfqd->busy_queues;
  184. }
  185. static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
  186. {
  187. if (rw == READ || rw == WRITE_SYNC)
  188. return task->pid;
  189. return CFQ_KEY_ASYNC;
  190. }
  191. /*
  192. * Lifted from AS - choose which of rq1 and rq2 that is best served now.
  193. * We choose the request that is closest to the head right now. Distance
  194. * behind the head is penalized and only allowed to a certain extent.
  195. */
  196. static struct request *
  197. cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
  198. {
  199. sector_t last, s1, s2, d1 = 0, d2 = 0;
  200. unsigned long back_max;
  201. #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
  202. #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
  203. unsigned wrap = 0; /* bit mask: requests behind the disk head? */
  204. if (rq1 == NULL || rq1 == rq2)
  205. return rq2;
  206. if (rq2 == NULL)
  207. return rq1;
  208. if (rq_is_sync(rq1) && !rq_is_sync(rq2))
  209. return rq1;
  210. else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
  211. return rq2;
  212. s1 = rq1->sector;
  213. s2 = rq2->sector;
  214. last = cfqd->last_sector;
  215. /*
  216. * by definition, 1KiB is 2 sectors
  217. */
  218. back_max = cfqd->cfq_back_max * 2;
  219. /*
  220. * Strict one way elevator _except_ in the case where we allow
  221. * short backward seeks which are biased as twice the cost of a
  222. * similar forward seek.
  223. */
  224. if (s1 >= last)
  225. d1 = s1 - last;
  226. else if (s1 + back_max >= last)
  227. d1 = (last - s1) * cfqd->cfq_back_penalty;
  228. else
  229. wrap |= CFQ_RQ1_WRAP;
  230. if (s2 >= last)
  231. d2 = s2 - last;
  232. else if (s2 + back_max >= last)
  233. d2 = (last - s2) * cfqd->cfq_back_penalty;
  234. else
  235. wrap |= CFQ_RQ2_WRAP;
  236. /* Found required data */
  237. /*
  238. * By doing switch() on the bit mask "wrap" we avoid having to
  239. * check two variables for all permutations: --> faster!
  240. */
  241. switch (wrap) {
  242. case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
  243. if (d1 < d2)
  244. return rq1;
  245. else if (d2 < d1)
  246. return rq2;
  247. else {
  248. if (s1 >= s2)
  249. return rq1;
  250. else
  251. return rq2;
  252. }
  253. case CFQ_RQ2_WRAP:
  254. return rq1;
  255. case CFQ_RQ1_WRAP:
  256. return rq2;
  257. case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
  258. default:
  259. /*
  260. * Since both rqs are wrapped,
  261. * start with the one that's further behind head
  262. * (--> only *one* back seek required),
  263. * since back seek takes more time than forward.
  264. */
  265. if (s1 <= s2)
  266. return rq1;
  267. else
  268. return rq2;
  269. }
  270. }
  271. /*
  272. * would be nice to take fifo expire time into account as well
  273. */
  274. static struct request *
  275. cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  276. struct request *last)
  277. {
  278. struct rb_node *rbnext = rb_next(&last->rb_node);
  279. struct rb_node *rbprev = rb_prev(&last->rb_node);
  280. struct request *next = NULL, *prev = NULL;
  281. BUG_ON(RB_EMPTY_NODE(&last->rb_node));
  282. if (rbprev)
  283. prev = rb_entry_rq(rbprev);
  284. if (rbnext)
  285. next = rb_entry_rq(rbnext);
  286. else {
  287. rbnext = rb_first(&cfqq->sort_list);
  288. if (rbnext && rbnext != &last->rb_node)
  289. next = rb_entry_rq(rbnext);
  290. }
  291. return cfq_choose_req(cfqd, next, prev);
  292. }
  293. static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
  294. {
  295. struct cfq_data *cfqd = cfqq->cfqd;
  296. struct list_head *list, *entry;
  297. BUG_ON(!cfq_cfqq_on_rr(cfqq));
  298. list_del(&cfqq->cfq_list);
  299. if (cfq_class_rt(cfqq))
  300. list = &cfqd->cur_rr;
  301. else if (cfq_class_idle(cfqq))
  302. list = &cfqd->idle_rr;
  303. else {
  304. /*
  305. * if cfqq has requests in flight, don't allow it to be
  306. * found in cfq_set_active_queue before it has finished them.
  307. * this is done to increase fairness between a process that
  308. * has lots of io pending vs one that only generates one
  309. * sporadically or synchronously
  310. */
  311. if (cfq_cfqq_dispatched(cfqq))
  312. list = &cfqd->busy_rr;
  313. else
  314. list = &cfqd->rr_list[cfqq->ioprio];
  315. }
  316. /*
  317. * if queue was preempted, just add to front to be fair. busy_rr
  318. * isn't sorted, but insert at the back for fairness.
  319. */
  320. if (preempted || list == &cfqd->busy_rr) {
  321. if (preempted)
  322. list = list->prev;
  323. list_add_tail(&cfqq->cfq_list, list);
  324. return;
  325. }
  326. /*
  327. * sort by when queue was last serviced
  328. */
  329. entry = list;
  330. while ((entry = entry->prev) != list) {
  331. struct cfq_queue *__cfqq = list_entry_cfqq(entry);
  332. if (!__cfqq->service_last)
  333. break;
  334. if (time_before(__cfqq->service_last, cfqq->service_last))
  335. break;
  336. }
  337. list_add(&cfqq->cfq_list, entry);
  338. }
  339. /*
  340. * add to busy list of queues for service, trying to be fair in ordering
  341. * the pending list according to last request service
  342. */
  343. static inline void
  344. cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  345. {
  346. BUG_ON(cfq_cfqq_on_rr(cfqq));
  347. cfq_mark_cfqq_on_rr(cfqq);
  348. cfqd->busy_queues++;
  349. cfq_resort_rr_list(cfqq, 0);
  350. }
  351. static inline void
  352. cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  353. {
  354. BUG_ON(!cfq_cfqq_on_rr(cfqq));
  355. cfq_clear_cfqq_on_rr(cfqq);
  356. list_move(&cfqq->cfq_list, &cfqd->empty_list);
  357. BUG_ON(!cfqd->busy_queues);
  358. cfqd->busy_queues--;
  359. }
  360. /*
  361. * rb tree support functions
  362. */
  363. static inline void cfq_del_rq_rb(struct request *rq)
  364. {
  365. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  366. struct cfq_data *cfqd = cfqq->cfqd;
  367. const int sync = rq_is_sync(rq);
  368. BUG_ON(!cfqq->queued[sync]);
  369. cfqq->queued[sync]--;
  370. elv_rb_del(&cfqq->sort_list, rq);
  371. if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
  372. cfq_del_cfqq_rr(cfqd, cfqq);
  373. }
  374. static void cfq_add_rq_rb(struct request *rq)
  375. {
  376. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  377. struct cfq_data *cfqd = cfqq->cfqd;
  378. struct request *__alias;
  379. cfqq->queued[rq_is_sync(rq)]++;
  380. /*
  381. * looks a little odd, but the first insert might return an alias.
  382. * if that happens, put the alias on the dispatch list
  383. */
  384. while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
  385. cfq_dispatch_insert(cfqd->queue, __alias);
  386. }
  387. static inline void
  388. cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
  389. {
  390. elv_rb_del(&cfqq->sort_list, rq);
  391. cfqq->queued[rq_is_sync(rq)]--;
  392. cfq_add_rq_rb(rq);
  393. }
  394. static struct request *
  395. cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
  396. {
  397. struct task_struct *tsk = current;
  398. pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio));
  399. struct cfq_queue *cfqq;
  400. cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
  401. if (cfqq) {
  402. sector_t sector = bio->bi_sector + bio_sectors(bio);
  403. return elv_rb_find(&cfqq->sort_list, sector);
  404. }
  405. return NULL;
  406. }
  407. static void cfq_activate_request(request_queue_t *q, struct request *rq)
  408. {
  409. struct cfq_data *cfqd = q->elevator->elevator_data;
  410. cfqd->rq_in_driver++;
  411. /*
  412. * If the depth is larger 1, it really could be queueing. But lets
  413. * make the mark a little higher - idling could still be good for
  414. * low queueing, and a low queueing number could also just indicate
  415. * a SCSI mid layer like behaviour where limit+1 is often seen.
  416. */
  417. if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
  418. cfqd->hw_tag = 1;
  419. }
  420. static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
  421. {
  422. struct cfq_data *cfqd = q->elevator->elevator_data;
  423. WARN_ON(!cfqd->rq_in_driver);
  424. cfqd->rq_in_driver--;
  425. }
  426. static void cfq_remove_request(struct request *rq)
  427. {
  428. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  429. if (cfqq->next_rq == rq)
  430. cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
  431. list_del_init(&rq->queuelist);
  432. cfq_del_rq_rb(rq);
  433. }
  434. static int
  435. cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
  436. {
  437. struct cfq_data *cfqd = q->elevator->elevator_data;
  438. struct request *__rq;
  439. __rq = cfq_find_rq_fmerge(cfqd, bio);
  440. if (__rq && elv_rq_merge_ok(__rq, bio)) {
  441. *req = __rq;
  442. return ELEVATOR_FRONT_MERGE;
  443. }
  444. return ELEVATOR_NO_MERGE;
  445. }
  446. static void cfq_merged_request(request_queue_t *q, struct request *req,
  447. int type)
  448. {
  449. if (type == ELEVATOR_FRONT_MERGE) {
  450. struct cfq_queue *cfqq = RQ_CFQQ(req);
  451. cfq_reposition_rq_rb(cfqq, req);
  452. }
  453. }
  454. static void
  455. cfq_merged_requests(request_queue_t *q, struct request *rq,
  456. struct request *next)
  457. {
  458. /*
  459. * reposition in fifo if next is older than rq
  460. */
  461. if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
  462. time_before(next->start_time, rq->start_time))
  463. list_move(&rq->queuelist, &next->queuelist);
  464. cfq_remove_request(next);
  465. }
  466. static inline void
  467. __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  468. {
  469. if (cfqq) {
  470. /*
  471. * stop potential idle class queues waiting service
  472. */
  473. del_timer(&cfqd->idle_class_timer);
  474. cfqq->slice_start = jiffies;
  475. cfqq->slice_end = 0;
  476. cfqq->slice_left = 0;
  477. cfq_clear_cfqq_must_alloc_slice(cfqq);
  478. cfq_clear_cfqq_fifo_expire(cfqq);
  479. }
  480. cfqd->active_queue = cfqq;
  481. }
  482. /*
  483. * current cfqq expired its slice (or was too idle), select new one
  484. */
  485. static void
  486. __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  487. int preempted)
  488. {
  489. unsigned long now = jiffies;
  490. if (cfq_cfqq_wait_request(cfqq))
  491. del_timer(&cfqd->idle_slice_timer);
  492. if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
  493. cfqq->service_last = now;
  494. cfq_schedule_dispatch(cfqd);
  495. }
  496. cfq_clear_cfqq_must_dispatch(cfqq);
  497. cfq_clear_cfqq_wait_request(cfqq);
  498. /*
  499. * store what was left of this slice, if the queue idled out
  500. * or was preempted
  501. */
  502. if (time_after(cfqq->slice_end, now))
  503. cfqq->slice_left = cfqq->slice_end - now;
  504. else
  505. cfqq->slice_left = 0;
  506. if (cfq_cfqq_on_rr(cfqq))
  507. cfq_resort_rr_list(cfqq, preempted);
  508. if (cfqq == cfqd->active_queue)
  509. cfqd->active_queue = NULL;
  510. if (cfqd->active_cic) {
  511. put_io_context(cfqd->active_cic->ioc);
  512. cfqd->active_cic = NULL;
  513. }
  514. cfqd->dispatch_slice = 0;
  515. }
  516. static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
  517. {
  518. struct cfq_queue *cfqq = cfqd->active_queue;
  519. if (cfqq)
  520. __cfq_slice_expired(cfqd, cfqq, preempted);
  521. }
  522. /*
  523. * 0
  524. * 0,1
  525. * 0,1,2
  526. * 0,1,2,3
  527. * 0,1,2,3,4
  528. * 0,1,2,3,4,5
  529. * 0,1,2,3,4,5,6
  530. * 0,1,2,3,4,5,6,7
  531. */
  532. static int cfq_get_next_prio_level(struct cfq_data *cfqd)
  533. {
  534. int prio, wrap;
  535. prio = -1;
  536. wrap = 0;
  537. do {
  538. int p;
  539. for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
  540. if (!list_empty(&cfqd->rr_list[p])) {
  541. prio = p;
  542. break;
  543. }
  544. }
  545. if (prio != -1)
  546. break;
  547. cfqd->cur_prio = 0;
  548. if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
  549. cfqd->cur_end_prio = 0;
  550. if (wrap)
  551. break;
  552. wrap = 1;
  553. }
  554. } while (1);
  555. if (unlikely(prio == -1))
  556. return -1;
  557. BUG_ON(prio >= CFQ_PRIO_LISTS);
  558. list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
  559. cfqd->cur_prio = prio + 1;
  560. if (cfqd->cur_prio > cfqd->cur_end_prio) {
  561. cfqd->cur_end_prio = cfqd->cur_prio;
  562. cfqd->cur_prio = 0;
  563. }
  564. if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
  565. cfqd->cur_prio = 0;
  566. cfqd->cur_end_prio = 0;
  567. }
  568. return prio;
  569. }
  570. static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
  571. {
  572. struct cfq_queue *cfqq = NULL;
  573. if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1) {
  574. /*
  575. * if current list is non-empty, grab first entry. if it is
  576. * empty, get next prio level and grab first entry then if any
  577. * are spliced
  578. */
  579. cfqq = list_entry_cfqq(cfqd->cur_rr.next);
  580. } else if (!list_empty(&cfqd->busy_rr)) {
  581. /*
  582. * If no new queues are available, check if the busy list has
  583. * some before falling back to idle io.
  584. */
  585. cfqq = list_entry_cfqq(cfqd->busy_rr.next);
  586. } else if (!list_empty(&cfqd->idle_rr)) {
  587. /*
  588. * if we have idle queues and no rt or be queues had pending
  589. * requests, either allow immediate service if the grace period
  590. * has passed or arm the idle grace timer
  591. */
  592. unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
  593. if (time_after_eq(jiffies, end))
  594. cfqq = list_entry_cfqq(cfqd->idle_rr.next);
  595. else
  596. mod_timer(&cfqd->idle_class_timer, end);
  597. }
  598. __cfq_set_active_queue(cfqd, cfqq);
  599. return cfqq;
  600. }
  601. #define CIC_SEEKY(cic) ((cic)->seek_mean > (128 * 1024))
  602. static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  603. {
  604. struct cfq_io_context *cic;
  605. unsigned long sl;
  606. WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
  607. WARN_ON(cfqq != cfqd->active_queue);
  608. /*
  609. * idle is disabled, either manually or by past process history
  610. */
  611. if (!cfqd->cfq_slice_idle)
  612. return 0;
  613. if (!cfq_cfqq_idle_window(cfqq))
  614. return 0;
  615. /*
  616. * task has exited, don't wait
  617. */
  618. cic = cfqd->active_cic;
  619. if (!cic || !cic->ioc->task)
  620. return 0;
  621. cfq_mark_cfqq_must_dispatch(cfqq);
  622. cfq_mark_cfqq_wait_request(cfqq);
  623. sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
  624. /*
  625. * we don't want to idle for seeks, but we do want to allow
  626. * fair distribution of slice time for a process doing back-to-back
  627. * seeks. so allow a little bit of time for him to submit a new rq
  628. */
  629. if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
  630. sl = min(sl, msecs_to_jiffies(2));
  631. mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
  632. return 1;
  633. }
  634. static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
  635. {
  636. struct cfq_data *cfqd = q->elevator->elevator_data;
  637. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  638. cfq_remove_request(rq);
  639. cfqq->on_dispatch[rq_is_sync(rq)]++;
  640. elv_dispatch_sort(q, rq);
  641. rq = list_entry(q->queue_head.prev, struct request, queuelist);
  642. cfqd->last_sector = rq->sector + rq->nr_sectors;
  643. }
  644. /*
  645. * return expired entry, or NULL to just start from scratch in rbtree
  646. */
  647. static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
  648. {
  649. struct cfq_data *cfqd = cfqq->cfqd;
  650. struct request *rq;
  651. int fifo;
  652. if (cfq_cfqq_fifo_expire(cfqq))
  653. return NULL;
  654. if (list_empty(&cfqq->fifo))
  655. return NULL;
  656. fifo = cfq_cfqq_class_sync(cfqq);
  657. rq = rq_entry_fifo(cfqq->fifo.next);
  658. if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
  659. cfq_mark_cfqq_fifo_expire(cfqq);
  660. return rq;
  661. }
  662. return NULL;
  663. }
  664. /*
  665. * Scale schedule slice based on io priority. Use the sync time slice only
  666. * if a queue is marked sync and has sync io queued. A sync queue with async
  667. * io only, should not get full sync slice length.
  668. */
  669. static inline int
  670. cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  671. {
  672. const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
  673. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  674. return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
  675. }
  676. static inline void
  677. cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  678. {
  679. cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
  680. }
  681. static inline int
  682. cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  683. {
  684. const int base_rq = cfqd->cfq_slice_async_rq;
  685. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  686. return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
  687. }
  688. /*
  689. * get next queue for service
  690. */
  691. static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
  692. {
  693. unsigned long now = jiffies;
  694. struct cfq_queue *cfqq;
  695. cfqq = cfqd->active_queue;
  696. if (!cfqq)
  697. goto new_queue;
  698. /*
  699. * slice has expired
  700. */
  701. if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
  702. goto expire;
  703. /*
  704. * if queue has requests, dispatch one. if not, check if
  705. * enough slice is left to wait for one
  706. */
  707. if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  708. goto keep_queue;
  709. else if (cfq_cfqq_dispatched(cfqq)) {
  710. cfqq = NULL;
  711. goto keep_queue;
  712. } else if (cfq_cfqq_class_sync(cfqq)) {
  713. if (cfq_arm_slice_timer(cfqd, cfqq))
  714. return NULL;
  715. }
  716. expire:
  717. cfq_slice_expired(cfqd, 0);
  718. new_queue:
  719. cfqq = cfq_set_active_queue(cfqd);
  720. keep_queue:
  721. return cfqq;
  722. }
  723. static int
  724. __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  725. int max_dispatch)
  726. {
  727. int dispatched = 0;
  728. BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
  729. do {
  730. struct request *rq;
  731. /*
  732. * follow expired path, else get first next available
  733. */
  734. if ((rq = cfq_check_fifo(cfqq)) == NULL)
  735. rq = cfqq->next_rq;
  736. /*
  737. * finally, insert request into driver dispatch list
  738. */
  739. cfq_dispatch_insert(cfqd->queue, rq);
  740. cfqd->dispatch_slice++;
  741. dispatched++;
  742. if (!cfqd->active_cic) {
  743. atomic_inc(&RQ_CIC(rq)->ioc->refcount);
  744. cfqd->active_cic = RQ_CIC(rq);
  745. }
  746. if (RB_EMPTY_ROOT(&cfqq->sort_list))
  747. break;
  748. } while (dispatched < max_dispatch);
  749. /*
  750. * if slice end isn't set yet, set it.
  751. */
  752. if (!cfqq->slice_end)
  753. cfq_set_prio_slice(cfqd, cfqq);
  754. /*
  755. * expire an async queue immediately if it has used up its slice. idle
  756. * queue always expire after 1 dispatch round.
  757. */
  758. if ((!cfq_cfqq_sync(cfqq) &&
  759. cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
  760. cfq_class_idle(cfqq) ||
  761. !cfq_cfqq_idle_window(cfqq))
  762. cfq_slice_expired(cfqd, 0);
  763. return dispatched;
  764. }
  765. static int
  766. cfq_forced_dispatch_cfqqs(struct list_head *list)
  767. {
  768. struct cfq_queue *cfqq, *next;
  769. int dispatched;
  770. dispatched = 0;
  771. list_for_each_entry_safe(cfqq, next, list, cfq_list) {
  772. while (cfqq->next_rq) {
  773. cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
  774. dispatched++;
  775. }
  776. BUG_ON(!list_empty(&cfqq->fifo));
  777. }
  778. return dispatched;
  779. }
  780. static int
  781. cfq_forced_dispatch(struct cfq_data *cfqd)
  782. {
  783. int i, dispatched = 0;
  784. for (i = 0; i < CFQ_PRIO_LISTS; i++)
  785. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
  786. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
  787. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
  788. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
  789. cfq_slice_expired(cfqd, 0);
  790. BUG_ON(cfqd->busy_queues);
  791. return dispatched;
  792. }
  793. static int
  794. cfq_dispatch_requests(request_queue_t *q, int force)
  795. {
  796. struct cfq_data *cfqd = q->elevator->elevator_data;
  797. struct cfq_queue *cfqq, *prev_cfqq;
  798. int dispatched;
  799. if (!cfqd->busy_queues)
  800. return 0;
  801. if (unlikely(force))
  802. return cfq_forced_dispatch(cfqd);
  803. dispatched = 0;
  804. prev_cfqq = NULL;
  805. while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
  806. int max_dispatch;
  807. /*
  808. * Don't repeat dispatch from the previous queue.
  809. */
  810. if (prev_cfqq == cfqq)
  811. break;
  812. cfq_clear_cfqq_must_dispatch(cfqq);
  813. cfq_clear_cfqq_wait_request(cfqq);
  814. del_timer(&cfqd->idle_slice_timer);
  815. max_dispatch = cfqd->cfq_quantum;
  816. if (cfq_class_idle(cfqq))
  817. max_dispatch = 1;
  818. dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
  819. /*
  820. * If the dispatch cfqq has idling enabled and is still
  821. * the active queue, break out.
  822. */
  823. if (cfq_cfqq_idle_window(cfqq) && cfqd->active_queue)
  824. break;
  825. prev_cfqq = cfqq;
  826. }
  827. return dispatched;
  828. }
  829. /*
  830. * task holds one reference to the queue, dropped when task exits. each rq
  831. * in-flight on this queue also holds a reference, dropped when rq is freed.
  832. *
  833. * queue lock must be held here.
  834. */
  835. static void cfq_put_queue(struct cfq_queue *cfqq)
  836. {
  837. struct cfq_data *cfqd = cfqq->cfqd;
  838. BUG_ON(atomic_read(&cfqq->ref) <= 0);
  839. if (!atomic_dec_and_test(&cfqq->ref))
  840. return;
  841. BUG_ON(rb_first(&cfqq->sort_list));
  842. BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
  843. BUG_ON(cfq_cfqq_on_rr(cfqq));
  844. if (unlikely(cfqd->active_queue == cfqq))
  845. __cfq_slice_expired(cfqd, cfqq, 0);
  846. /*
  847. * it's on the empty list and still hashed
  848. */
  849. list_del(&cfqq->cfq_list);
  850. hlist_del(&cfqq->cfq_hash);
  851. kmem_cache_free(cfq_pool, cfqq);
  852. }
  853. static inline struct cfq_queue *
  854. __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
  855. const int hashval)
  856. {
  857. struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
  858. struct hlist_node *entry;
  859. struct cfq_queue *__cfqq;
  860. hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
  861. const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
  862. if (__cfqq->key == key && (__p == prio || !prio))
  863. return __cfqq;
  864. }
  865. return NULL;
  866. }
  867. static struct cfq_queue *
  868. cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
  869. {
  870. return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
  871. }
  872. static void cfq_free_io_context(struct io_context *ioc)
  873. {
  874. struct cfq_io_context *__cic;
  875. struct rb_node *n;
  876. int freed = 0;
  877. while ((n = rb_first(&ioc->cic_root)) != NULL) {
  878. __cic = rb_entry(n, struct cfq_io_context, rb_node);
  879. rb_erase(&__cic->rb_node, &ioc->cic_root);
  880. kmem_cache_free(cfq_ioc_pool, __cic);
  881. freed++;
  882. }
  883. elv_ioc_count_mod(ioc_count, -freed);
  884. if (ioc_gone && !elv_ioc_count_read(ioc_count))
  885. complete(ioc_gone);
  886. }
  887. static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  888. {
  889. if (unlikely(cfqq == cfqd->active_queue))
  890. __cfq_slice_expired(cfqd, cfqq, 0);
  891. cfq_put_queue(cfqq);
  892. }
  893. static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
  894. struct cfq_io_context *cic)
  895. {
  896. list_del_init(&cic->queue_list);
  897. smp_wmb();
  898. cic->key = NULL;
  899. if (cic->cfqq[ASYNC]) {
  900. cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
  901. cic->cfqq[ASYNC] = NULL;
  902. }
  903. if (cic->cfqq[SYNC]) {
  904. cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
  905. cic->cfqq[SYNC] = NULL;
  906. }
  907. }
  908. /*
  909. * Called with interrupts disabled
  910. */
  911. static void cfq_exit_single_io_context(struct cfq_io_context *cic)
  912. {
  913. struct cfq_data *cfqd = cic->key;
  914. if (cfqd) {
  915. request_queue_t *q = cfqd->queue;
  916. spin_lock_irq(q->queue_lock);
  917. __cfq_exit_single_io_context(cfqd, cic);
  918. spin_unlock_irq(q->queue_lock);
  919. }
  920. }
  921. static void cfq_exit_io_context(struct io_context *ioc)
  922. {
  923. struct cfq_io_context *__cic;
  924. struct rb_node *n;
  925. /*
  926. * put the reference this task is holding to the various queues
  927. */
  928. n = rb_first(&ioc->cic_root);
  929. while (n != NULL) {
  930. __cic = rb_entry(n, struct cfq_io_context, rb_node);
  931. cfq_exit_single_io_context(__cic);
  932. n = rb_next(n);
  933. }
  934. }
  935. static struct cfq_io_context *
  936. cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
  937. {
  938. struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
  939. if (cic) {
  940. memset(cic, 0, sizeof(*cic));
  941. cic->last_end_request = jiffies;
  942. INIT_LIST_HEAD(&cic->queue_list);
  943. cic->dtor = cfq_free_io_context;
  944. cic->exit = cfq_exit_io_context;
  945. elv_ioc_count_inc(ioc_count);
  946. }
  947. return cic;
  948. }
  949. static void cfq_init_prio_data(struct cfq_queue *cfqq)
  950. {
  951. struct task_struct *tsk = current;
  952. int ioprio_class;
  953. if (!cfq_cfqq_prio_changed(cfqq))
  954. return;
  955. ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
  956. switch (ioprio_class) {
  957. default:
  958. printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
  959. case IOPRIO_CLASS_NONE:
  960. /*
  961. * no prio set, place us in the middle of the BE classes
  962. */
  963. cfqq->ioprio = task_nice_ioprio(tsk);
  964. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  965. break;
  966. case IOPRIO_CLASS_RT:
  967. cfqq->ioprio = task_ioprio(tsk);
  968. cfqq->ioprio_class = IOPRIO_CLASS_RT;
  969. break;
  970. case IOPRIO_CLASS_BE:
  971. cfqq->ioprio = task_ioprio(tsk);
  972. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  973. break;
  974. case IOPRIO_CLASS_IDLE:
  975. cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
  976. cfqq->ioprio = 7;
  977. cfq_clear_cfqq_idle_window(cfqq);
  978. break;
  979. }
  980. /*
  981. * keep track of original prio settings in case we have to temporarily
  982. * elevate the priority of this queue
  983. */
  984. cfqq->org_ioprio = cfqq->ioprio;
  985. cfqq->org_ioprio_class = cfqq->ioprio_class;
  986. if (cfq_cfqq_on_rr(cfqq))
  987. cfq_resort_rr_list(cfqq, 0);
  988. cfq_clear_cfqq_prio_changed(cfqq);
  989. }
  990. static inline void changed_ioprio(struct cfq_io_context *cic)
  991. {
  992. struct cfq_data *cfqd = cic->key;
  993. struct cfq_queue *cfqq;
  994. if (unlikely(!cfqd))
  995. return;
  996. spin_lock(cfqd->queue->queue_lock);
  997. cfqq = cic->cfqq[ASYNC];
  998. if (cfqq) {
  999. struct cfq_queue *new_cfqq;
  1000. new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
  1001. GFP_ATOMIC);
  1002. if (new_cfqq) {
  1003. cic->cfqq[ASYNC] = new_cfqq;
  1004. cfq_put_queue(cfqq);
  1005. }
  1006. }
  1007. cfqq = cic->cfqq[SYNC];
  1008. if (cfqq)
  1009. cfq_mark_cfqq_prio_changed(cfqq);
  1010. spin_unlock(cfqd->queue->queue_lock);
  1011. }
  1012. static void cfq_ioc_set_ioprio(struct io_context *ioc)
  1013. {
  1014. struct cfq_io_context *cic;
  1015. struct rb_node *n;
  1016. ioc->ioprio_changed = 0;
  1017. n = rb_first(&ioc->cic_root);
  1018. while (n != NULL) {
  1019. cic = rb_entry(n, struct cfq_io_context, rb_node);
  1020. changed_ioprio(cic);
  1021. n = rb_next(n);
  1022. }
  1023. }
  1024. static struct cfq_queue *
  1025. cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
  1026. gfp_t gfp_mask)
  1027. {
  1028. const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
  1029. struct cfq_queue *cfqq, *new_cfqq = NULL;
  1030. unsigned short ioprio;
  1031. retry:
  1032. ioprio = tsk->ioprio;
  1033. cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
  1034. if (!cfqq) {
  1035. if (new_cfqq) {
  1036. cfqq = new_cfqq;
  1037. new_cfqq = NULL;
  1038. } else if (gfp_mask & __GFP_WAIT) {
  1039. /*
  1040. * Inform the allocator of the fact that we will
  1041. * just repeat this allocation if it fails, to allow
  1042. * the allocator to do whatever it needs to attempt to
  1043. * free memory.
  1044. */
  1045. spin_unlock_irq(cfqd->queue->queue_lock);
  1046. new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask|__GFP_NOFAIL);
  1047. spin_lock_irq(cfqd->queue->queue_lock);
  1048. goto retry;
  1049. } else {
  1050. cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
  1051. if (!cfqq)
  1052. goto out;
  1053. }
  1054. memset(cfqq, 0, sizeof(*cfqq));
  1055. INIT_HLIST_NODE(&cfqq->cfq_hash);
  1056. INIT_LIST_HEAD(&cfqq->cfq_list);
  1057. INIT_LIST_HEAD(&cfqq->fifo);
  1058. cfqq->key = key;
  1059. hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
  1060. atomic_set(&cfqq->ref, 0);
  1061. cfqq->cfqd = cfqd;
  1062. cfqq->service_last = 0;
  1063. /*
  1064. * set ->slice_left to allow preemption for a new process
  1065. */
  1066. cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
  1067. cfq_mark_cfqq_idle_window(cfqq);
  1068. cfq_mark_cfqq_prio_changed(cfqq);
  1069. cfq_init_prio_data(cfqq);
  1070. }
  1071. if (new_cfqq)
  1072. kmem_cache_free(cfq_pool, new_cfqq);
  1073. atomic_inc(&cfqq->ref);
  1074. out:
  1075. WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
  1076. return cfqq;
  1077. }
  1078. static void
  1079. cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
  1080. {
  1081. WARN_ON(!list_empty(&cic->queue_list));
  1082. rb_erase(&cic->rb_node, &ioc->cic_root);
  1083. kmem_cache_free(cfq_ioc_pool, cic);
  1084. elv_ioc_count_dec(ioc_count);
  1085. }
  1086. static struct cfq_io_context *
  1087. cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
  1088. {
  1089. struct rb_node *n;
  1090. struct cfq_io_context *cic;
  1091. void *k, *key = cfqd;
  1092. restart:
  1093. n = ioc->cic_root.rb_node;
  1094. while (n) {
  1095. cic = rb_entry(n, struct cfq_io_context, rb_node);
  1096. /* ->key must be copied to avoid race with cfq_exit_queue() */
  1097. k = cic->key;
  1098. if (unlikely(!k)) {
  1099. cfq_drop_dead_cic(ioc, cic);
  1100. goto restart;
  1101. }
  1102. if (key < k)
  1103. n = n->rb_left;
  1104. else if (key > k)
  1105. n = n->rb_right;
  1106. else
  1107. return cic;
  1108. }
  1109. return NULL;
  1110. }
  1111. static inline void
  1112. cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
  1113. struct cfq_io_context *cic)
  1114. {
  1115. struct rb_node **p;
  1116. struct rb_node *parent;
  1117. struct cfq_io_context *__cic;
  1118. void *k;
  1119. cic->ioc = ioc;
  1120. cic->key = cfqd;
  1121. restart:
  1122. parent = NULL;
  1123. p = &ioc->cic_root.rb_node;
  1124. while (*p) {
  1125. parent = *p;
  1126. __cic = rb_entry(parent, struct cfq_io_context, rb_node);
  1127. /* ->key must be copied to avoid race with cfq_exit_queue() */
  1128. k = __cic->key;
  1129. if (unlikely(!k)) {
  1130. cfq_drop_dead_cic(ioc, __cic);
  1131. goto restart;
  1132. }
  1133. if (cic->key < k)
  1134. p = &(*p)->rb_left;
  1135. else if (cic->key > k)
  1136. p = &(*p)->rb_right;
  1137. else
  1138. BUG();
  1139. }
  1140. rb_link_node(&cic->rb_node, parent, p);
  1141. rb_insert_color(&cic->rb_node, &ioc->cic_root);
  1142. spin_lock_irq(cfqd->queue->queue_lock);
  1143. list_add(&cic->queue_list, &cfqd->cic_list);
  1144. spin_unlock_irq(cfqd->queue->queue_lock);
  1145. }
  1146. /*
  1147. * Setup general io context and cfq io context. There can be several cfq
  1148. * io contexts per general io context, if this process is doing io to more
  1149. * than one device managed by cfq.
  1150. */
  1151. static struct cfq_io_context *
  1152. cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
  1153. {
  1154. struct io_context *ioc = NULL;
  1155. struct cfq_io_context *cic;
  1156. might_sleep_if(gfp_mask & __GFP_WAIT);
  1157. ioc = get_io_context(gfp_mask);
  1158. if (!ioc)
  1159. return NULL;
  1160. cic = cfq_cic_rb_lookup(cfqd, ioc);
  1161. if (cic)
  1162. goto out;
  1163. cic = cfq_alloc_io_context(cfqd, gfp_mask);
  1164. if (cic == NULL)
  1165. goto err;
  1166. cfq_cic_link(cfqd, ioc, cic);
  1167. out:
  1168. smp_read_barrier_depends();
  1169. if (unlikely(ioc->ioprio_changed))
  1170. cfq_ioc_set_ioprio(ioc);
  1171. return cic;
  1172. err:
  1173. put_io_context(ioc);
  1174. return NULL;
  1175. }
  1176. static void
  1177. cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
  1178. {
  1179. unsigned long elapsed, ttime;
  1180. /*
  1181. * if this context already has stuff queued, thinktime is from
  1182. * last queue not last end
  1183. */
  1184. #if 0
  1185. if (time_after(cic->last_end_request, cic->last_queue))
  1186. elapsed = jiffies - cic->last_end_request;
  1187. else
  1188. elapsed = jiffies - cic->last_queue;
  1189. #else
  1190. elapsed = jiffies - cic->last_end_request;
  1191. #endif
  1192. ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
  1193. cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
  1194. cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
  1195. cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
  1196. }
  1197. static void
  1198. cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
  1199. struct request *rq)
  1200. {
  1201. sector_t sdist;
  1202. u64 total;
  1203. if (cic->last_request_pos < rq->sector)
  1204. sdist = rq->sector - cic->last_request_pos;
  1205. else
  1206. sdist = cic->last_request_pos - rq->sector;
  1207. /*
  1208. * Don't allow the seek distance to get too large from the
  1209. * odd fragment, pagein, etc
  1210. */
  1211. if (cic->seek_samples <= 60) /* second&third seek */
  1212. sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
  1213. else
  1214. sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
  1215. cic->seek_samples = (7*cic->seek_samples + 256) / 8;
  1216. cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
  1217. total = cic->seek_total + (cic->seek_samples/2);
  1218. do_div(total, cic->seek_samples);
  1219. cic->seek_mean = (sector_t)total;
  1220. }
  1221. /*
  1222. * Disable idle window if the process thinks too long or seeks so much that
  1223. * it doesn't matter
  1224. */
  1225. static void
  1226. cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1227. struct cfq_io_context *cic)
  1228. {
  1229. int enable_idle = cfq_cfqq_idle_window(cfqq);
  1230. if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
  1231. (cfqd->hw_tag && CIC_SEEKY(cic)))
  1232. enable_idle = 0;
  1233. else if (sample_valid(cic->ttime_samples)) {
  1234. if (cic->ttime_mean > cfqd->cfq_slice_idle)
  1235. enable_idle = 0;
  1236. else
  1237. enable_idle = 1;
  1238. }
  1239. if (enable_idle)
  1240. cfq_mark_cfqq_idle_window(cfqq);
  1241. else
  1242. cfq_clear_cfqq_idle_window(cfqq);
  1243. }
  1244. /*
  1245. * Check if new_cfqq should preempt the currently active queue. Return 0 for
  1246. * no or if we aren't sure, a 1 will cause a preempt.
  1247. */
  1248. static int
  1249. cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
  1250. struct request *rq)
  1251. {
  1252. struct cfq_queue *cfqq = cfqd->active_queue;
  1253. if (cfq_class_idle(new_cfqq))
  1254. return 0;
  1255. if (!cfqq)
  1256. return 0;
  1257. if (cfq_class_idle(cfqq))
  1258. return 1;
  1259. if (!cfq_cfqq_wait_request(new_cfqq))
  1260. return 0;
  1261. /*
  1262. * if it doesn't have slice left, forget it
  1263. */
  1264. if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
  1265. return 0;
  1266. if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
  1267. return 1;
  1268. return 0;
  1269. }
  1270. /*
  1271. * cfqq preempts the active queue. if we allowed preempt with no slice left,
  1272. * let it have half of its nominal slice.
  1273. */
  1274. static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1275. {
  1276. struct cfq_queue *__cfqq, *next;
  1277. list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
  1278. cfq_resort_rr_list(__cfqq, 1);
  1279. if (!cfqq->slice_left)
  1280. cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
  1281. cfqq->slice_end = cfqq->slice_left + jiffies;
  1282. cfq_slice_expired(cfqd, 1);
  1283. __cfq_set_active_queue(cfqd, cfqq);
  1284. }
  1285. /*
  1286. * should really be a ll_rw_blk.c helper
  1287. */
  1288. static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1289. {
  1290. request_queue_t *q = cfqd->queue;
  1291. if (!blk_queue_plugged(q))
  1292. q->request_fn(q);
  1293. else
  1294. __generic_unplug_device(q);
  1295. }
  1296. /*
  1297. * Called when a new fs request (rq) is added (to cfqq). Check if there's
  1298. * something we should do about it
  1299. */
  1300. static void
  1301. cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1302. struct request *rq)
  1303. {
  1304. struct cfq_io_context *cic = RQ_CIC(rq);
  1305. /*
  1306. * check if this request is a better next-serve candidate)) {
  1307. */
  1308. cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
  1309. BUG_ON(!cfqq->next_rq);
  1310. /*
  1311. * we never wait for an async request and we don't allow preemption
  1312. * of an async request. so just return early
  1313. */
  1314. if (!rq_is_sync(rq)) {
  1315. /*
  1316. * sync process issued an async request, if it's waiting
  1317. * then expire it and kick rq handling.
  1318. */
  1319. if (cic == cfqd->active_cic &&
  1320. del_timer(&cfqd->idle_slice_timer)) {
  1321. cfq_slice_expired(cfqd, 0);
  1322. cfq_start_queueing(cfqd, cfqq);
  1323. }
  1324. return;
  1325. }
  1326. cfq_update_io_thinktime(cfqd, cic);
  1327. cfq_update_io_seektime(cfqd, cic, rq);
  1328. cfq_update_idle_window(cfqd, cfqq, cic);
  1329. cic->last_queue = jiffies;
  1330. cic->last_request_pos = rq->sector + rq->nr_sectors;
  1331. if (cfqq == cfqd->active_queue) {
  1332. /*
  1333. * if we are waiting for a request for this queue, let it rip
  1334. * immediately and flag that we must not expire this queue
  1335. * just now
  1336. */
  1337. if (cfq_cfqq_wait_request(cfqq)) {
  1338. cfq_mark_cfqq_must_dispatch(cfqq);
  1339. del_timer(&cfqd->idle_slice_timer);
  1340. cfq_start_queueing(cfqd, cfqq);
  1341. }
  1342. } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
  1343. /*
  1344. * not the active queue - expire current slice if it is
  1345. * idle and has expired it's mean thinktime or this new queue
  1346. * has some old slice time left and is of higher priority
  1347. */
  1348. cfq_preempt_queue(cfqd, cfqq);
  1349. cfq_mark_cfqq_must_dispatch(cfqq);
  1350. cfq_start_queueing(cfqd, cfqq);
  1351. }
  1352. }
  1353. static void cfq_insert_request(request_queue_t *q, struct request *rq)
  1354. {
  1355. struct cfq_data *cfqd = q->elevator->elevator_data;
  1356. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  1357. cfq_init_prio_data(cfqq);
  1358. cfq_add_rq_rb(rq);
  1359. if (!cfq_cfqq_on_rr(cfqq))
  1360. cfq_add_cfqq_rr(cfqd, cfqq);
  1361. list_add_tail(&rq->queuelist, &cfqq->fifo);
  1362. cfq_rq_enqueued(cfqd, cfqq, rq);
  1363. }
  1364. static void cfq_completed_request(request_queue_t *q, struct request *rq)
  1365. {
  1366. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  1367. struct cfq_data *cfqd = cfqq->cfqd;
  1368. const int sync = rq_is_sync(rq);
  1369. unsigned long now;
  1370. now = jiffies;
  1371. WARN_ON(!cfqd->rq_in_driver);
  1372. WARN_ON(!cfqq->on_dispatch[sync]);
  1373. cfqd->rq_in_driver--;
  1374. cfqq->on_dispatch[sync]--;
  1375. if (!cfq_class_idle(cfqq))
  1376. cfqd->last_end_request = now;
  1377. if (!cfq_cfqq_dispatched(cfqq)) {
  1378. if (cfq_cfqq_on_rr(cfqq)) {
  1379. cfqq->service_last = now;
  1380. cfq_resort_rr_list(cfqq, 0);
  1381. }
  1382. }
  1383. if (sync)
  1384. RQ_CIC(rq)->last_end_request = now;
  1385. /*
  1386. * If this is the active queue, check if it needs to be expired,
  1387. * or if we want to idle in case it has no pending requests.
  1388. */
  1389. if (cfqd->active_queue == cfqq) {
  1390. if (time_after(now, cfqq->slice_end))
  1391. cfq_slice_expired(cfqd, 0);
  1392. else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list)) {
  1393. if (!cfq_arm_slice_timer(cfqd, cfqq))
  1394. cfq_schedule_dispatch(cfqd);
  1395. }
  1396. }
  1397. }
  1398. /*
  1399. * we temporarily boost lower priority queues if they are holding fs exclusive
  1400. * resources. they are boosted to normal prio (CLASS_BE/4)
  1401. */
  1402. static void cfq_prio_boost(struct cfq_queue *cfqq)
  1403. {
  1404. const int ioprio_class = cfqq->ioprio_class;
  1405. const int ioprio = cfqq->ioprio;
  1406. if (has_fs_excl()) {
  1407. /*
  1408. * boost idle prio on transactions that would lock out other
  1409. * users of the filesystem
  1410. */
  1411. if (cfq_class_idle(cfqq))
  1412. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  1413. if (cfqq->ioprio > IOPRIO_NORM)
  1414. cfqq->ioprio = IOPRIO_NORM;
  1415. } else {
  1416. /*
  1417. * check if we need to unboost the queue
  1418. */
  1419. if (cfqq->ioprio_class != cfqq->org_ioprio_class)
  1420. cfqq->ioprio_class = cfqq->org_ioprio_class;
  1421. if (cfqq->ioprio != cfqq->org_ioprio)
  1422. cfqq->ioprio = cfqq->org_ioprio;
  1423. }
  1424. /*
  1425. * refile between round-robin lists if we moved the priority class
  1426. */
  1427. if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
  1428. cfq_cfqq_on_rr(cfqq))
  1429. cfq_resort_rr_list(cfqq, 0);
  1430. }
  1431. static inline int __cfq_may_queue(struct cfq_queue *cfqq)
  1432. {
  1433. if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
  1434. !cfq_cfqq_must_alloc_slice(cfqq)) {
  1435. cfq_mark_cfqq_must_alloc_slice(cfqq);
  1436. return ELV_MQUEUE_MUST;
  1437. }
  1438. return ELV_MQUEUE_MAY;
  1439. }
  1440. static int cfq_may_queue(request_queue_t *q, int rw)
  1441. {
  1442. struct cfq_data *cfqd = q->elevator->elevator_data;
  1443. struct task_struct *tsk = current;
  1444. struct cfq_queue *cfqq;
  1445. /*
  1446. * don't force setup of a queue from here, as a call to may_queue
  1447. * does not necessarily imply that a request actually will be queued.
  1448. * so just lookup a possibly existing queue, or return 'may queue'
  1449. * if that fails
  1450. */
  1451. cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
  1452. if (cfqq) {
  1453. cfq_init_prio_data(cfqq);
  1454. cfq_prio_boost(cfqq);
  1455. return __cfq_may_queue(cfqq);
  1456. }
  1457. return ELV_MQUEUE_MAY;
  1458. }
  1459. /*
  1460. * queue lock held here
  1461. */
  1462. static void cfq_put_request(request_queue_t *q, struct request *rq)
  1463. {
  1464. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  1465. if (cfqq) {
  1466. const int rw = rq_data_dir(rq);
  1467. BUG_ON(!cfqq->allocated[rw]);
  1468. cfqq->allocated[rw]--;
  1469. put_io_context(RQ_CIC(rq)->ioc);
  1470. rq->elevator_private = NULL;
  1471. rq->elevator_private2 = NULL;
  1472. cfq_put_queue(cfqq);
  1473. }
  1474. }
  1475. /*
  1476. * Allocate cfq data structures associated with this request.
  1477. */
  1478. static int
  1479. cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
  1480. {
  1481. struct cfq_data *cfqd = q->elevator->elevator_data;
  1482. struct task_struct *tsk = current;
  1483. struct cfq_io_context *cic;
  1484. const int rw = rq_data_dir(rq);
  1485. pid_t key = cfq_queue_pid(tsk, rw);
  1486. struct cfq_queue *cfqq;
  1487. unsigned long flags;
  1488. int is_sync = key != CFQ_KEY_ASYNC;
  1489. might_sleep_if(gfp_mask & __GFP_WAIT);
  1490. cic = cfq_get_io_context(cfqd, gfp_mask);
  1491. spin_lock_irqsave(q->queue_lock, flags);
  1492. if (!cic)
  1493. goto queue_fail;
  1494. if (!cic->cfqq[is_sync]) {
  1495. cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
  1496. if (!cfqq)
  1497. goto queue_fail;
  1498. cic->cfqq[is_sync] = cfqq;
  1499. } else
  1500. cfqq = cic->cfqq[is_sync];
  1501. cfqq->allocated[rw]++;
  1502. cfq_clear_cfqq_must_alloc(cfqq);
  1503. atomic_inc(&cfqq->ref);
  1504. spin_unlock_irqrestore(q->queue_lock, flags);
  1505. rq->elevator_private = cic;
  1506. rq->elevator_private2 = cfqq;
  1507. return 0;
  1508. queue_fail:
  1509. if (cic)
  1510. put_io_context(cic->ioc);
  1511. cfq_schedule_dispatch(cfqd);
  1512. spin_unlock_irqrestore(q->queue_lock, flags);
  1513. return 1;
  1514. }
  1515. static void cfq_kick_queue(void *data)
  1516. {
  1517. request_queue_t *q = data;
  1518. unsigned long flags;
  1519. spin_lock_irqsave(q->queue_lock, flags);
  1520. blk_remove_plug(q);
  1521. q->request_fn(q);
  1522. spin_unlock_irqrestore(q->queue_lock, flags);
  1523. }
  1524. /*
  1525. * Timer running if the active_queue is currently idling inside its time slice
  1526. */
  1527. static void cfq_idle_slice_timer(unsigned long data)
  1528. {
  1529. struct cfq_data *cfqd = (struct cfq_data *) data;
  1530. struct cfq_queue *cfqq;
  1531. unsigned long flags;
  1532. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  1533. if ((cfqq = cfqd->active_queue) != NULL) {
  1534. unsigned long now = jiffies;
  1535. /*
  1536. * expired
  1537. */
  1538. if (time_after(now, cfqq->slice_end))
  1539. goto expire;
  1540. /*
  1541. * only expire and reinvoke request handler, if there are
  1542. * other queues with pending requests
  1543. */
  1544. if (!cfqd->busy_queues)
  1545. goto out_cont;
  1546. /*
  1547. * not expired and it has a request pending, let it dispatch
  1548. */
  1549. if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
  1550. cfq_mark_cfqq_must_dispatch(cfqq);
  1551. goto out_kick;
  1552. }
  1553. }
  1554. expire:
  1555. cfq_slice_expired(cfqd, 0);
  1556. out_kick:
  1557. cfq_schedule_dispatch(cfqd);
  1558. out_cont:
  1559. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  1560. }
  1561. /*
  1562. * Timer running if an idle class queue is waiting for service
  1563. */
  1564. static void cfq_idle_class_timer(unsigned long data)
  1565. {
  1566. struct cfq_data *cfqd = (struct cfq_data *) data;
  1567. unsigned long flags, end;
  1568. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  1569. /*
  1570. * race with a non-idle queue, reset timer
  1571. */
  1572. end = cfqd->last_end_request + CFQ_IDLE_GRACE;
  1573. if (!time_after_eq(jiffies, end))
  1574. mod_timer(&cfqd->idle_class_timer, end);
  1575. else
  1576. cfq_schedule_dispatch(cfqd);
  1577. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  1578. }
  1579. static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
  1580. {
  1581. del_timer_sync(&cfqd->idle_slice_timer);
  1582. del_timer_sync(&cfqd->idle_class_timer);
  1583. blk_sync_queue(cfqd->queue);
  1584. }
  1585. static void cfq_exit_queue(elevator_t *e)
  1586. {
  1587. struct cfq_data *cfqd = e->elevator_data;
  1588. request_queue_t *q = cfqd->queue;
  1589. cfq_shutdown_timer_wq(cfqd);
  1590. spin_lock_irq(q->queue_lock);
  1591. if (cfqd->active_queue)
  1592. __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
  1593. while (!list_empty(&cfqd->cic_list)) {
  1594. struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
  1595. struct cfq_io_context,
  1596. queue_list);
  1597. __cfq_exit_single_io_context(cfqd, cic);
  1598. }
  1599. spin_unlock_irq(q->queue_lock);
  1600. cfq_shutdown_timer_wq(cfqd);
  1601. kfree(cfqd->cfq_hash);
  1602. kfree(cfqd);
  1603. }
  1604. static void *cfq_init_queue(request_queue_t *q, elevator_t *e)
  1605. {
  1606. struct cfq_data *cfqd;
  1607. int i;
  1608. cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
  1609. if (!cfqd)
  1610. return NULL;
  1611. memset(cfqd, 0, sizeof(*cfqd));
  1612. for (i = 0; i < CFQ_PRIO_LISTS; i++)
  1613. INIT_LIST_HEAD(&cfqd->rr_list[i]);
  1614. INIT_LIST_HEAD(&cfqd->busy_rr);
  1615. INIT_LIST_HEAD(&cfqd->cur_rr);
  1616. INIT_LIST_HEAD(&cfqd->idle_rr);
  1617. INIT_LIST_HEAD(&cfqd->empty_list);
  1618. INIT_LIST_HEAD(&cfqd->cic_list);
  1619. cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
  1620. if (!cfqd->cfq_hash)
  1621. goto out_free;
  1622. for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
  1623. INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
  1624. cfqd->queue = q;
  1625. init_timer(&cfqd->idle_slice_timer);
  1626. cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
  1627. cfqd->idle_slice_timer.data = (unsigned long) cfqd;
  1628. init_timer(&cfqd->idle_class_timer);
  1629. cfqd->idle_class_timer.function = cfq_idle_class_timer;
  1630. cfqd->idle_class_timer.data = (unsigned long) cfqd;
  1631. INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
  1632. cfqd->cfq_quantum = cfq_quantum;
  1633. cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
  1634. cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
  1635. cfqd->cfq_back_max = cfq_back_max;
  1636. cfqd->cfq_back_penalty = cfq_back_penalty;
  1637. cfqd->cfq_slice[0] = cfq_slice_async;
  1638. cfqd->cfq_slice[1] = cfq_slice_sync;
  1639. cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
  1640. cfqd->cfq_slice_idle = cfq_slice_idle;
  1641. return cfqd;
  1642. out_free:
  1643. kfree(cfqd);
  1644. return NULL;
  1645. }
  1646. static void cfq_slab_kill(void)
  1647. {
  1648. if (cfq_pool)
  1649. kmem_cache_destroy(cfq_pool);
  1650. if (cfq_ioc_pool)
  1651. kmem_cache_destroy(cfq_ioc_pool);
  1652. }
  1653. static int __init cfq_slab_setup(void)
  1654. {
  1655. cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
  1656. NULL, NULL);
  1657. if (!cfq_pool)
  1658. goto fail;
  1659. cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
  1660. sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
  1661. if (!cfq_ioc_pool)
  1662. goto fail;
  1663. return 0;
  1664. fail:
  1665. cfq_slab_kill();
  1666. return -ENOMEM;
  1667. }
  1668. /*
  1669. * sysfs parts below -->
  1670. */
  1671. static ssize_t
  1672. cfq_var_show(unsigned int var, char *page)
  1673. {
  1674. return sprintf(page, "%d\n", var);
  1675. }
  1676. static ssize_t
  1677. cfq_var_store(unsigned int *var, const char *page, size_t count)
  1678. {
  1679. char *p = (char *) page;
  1680. *var = simple_strtoul(p, &p, 10);
  1681. return count;
  1682. }
  1683. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  1684. static ssize_t __FUNC(elevator_t *e, char *page) \
  1685. { \
  1686. struct cfq_data *cfqd = e->elevator_data; \
  1687. unsigned int __data = __VAR; \
  1688. if (__CONV) \
  1689. __data = jiffies_to_msecs(__data); \
  1690. return cfq_var_show(__data, (page)); \
  1691. }
  1692. SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
  1693. SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
  1694. SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
  1695. SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
  1696. SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
  1697. SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
  1698. SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
  1699. SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
  1700. SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
  1701. #undef SHOW_FUNCTION
  1702. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  1703. static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
  1704. { \
  1705. struct cfq_data *cfqd = e->elevator_data; \
  1706. unsigned int __data; \
  1707. int ret = cfq_var_store(&__data, (page), count); \
  1708. if (__data < (MIN)) \
  1709. __data = (MIN); \
  1710. else if (__data > (MAX)) \
  1711. __data = (MAX); \
  1712. if (__CONV) \
  1713. *(__PTR) = msecs_to_jiffies(__data); \
  1714. else \
  1715. *(__PTR) = __data; \
  1716. return ret; \
  1717. }
  1718. STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
  1719. STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
  1720. STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
  1721. STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
  1722. STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
  1723. STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
  1724. STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
  1725. STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
  1726. STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
  1727. #undef STORE_FUNCTION
  1728. #define CFQ_ATTR(name) \
  1729. __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
  1730. static struct elv_fs_entry cfq_attrs[] = {
  1731. CFQ_ATTR(quantum),
  1732. CFQ_ATTR(fifo_expire_sync),
  1733. CFQ_ATTR(fifo_expire_async),
  1734. CFQ_ATTR(back_seek_max),
  1735. CFQ_ATTR(back_seek_penalty),
  1736. CFQ_ATTR(slice_sync),
  1737. CFQ_ATTR(slice_async),
  1738. CFQ_ATTR(slice_async_rq),
  1739. CFQ_ATTR(slice_idle),
  1740. __ATTR_NULL
  1741. };
  1742. static struct elevator_type iosched_cfq = {
  1743. .ops = {
  1744. .elevator_merge_fn = cfq_merge,
  1745. .elevator_merged_fn = cfq_merged_request,
  1746. .elevator_merge_req_fn = cfq_merged_requests,
  1747. .elevator_dispatch_fn = cfq_dispatch_requests,
  1748. .elevator_add_req_fn = cfq_insert_request,
  1749. .elevator_activate_req_fn = cfq_activate_request,
  1750. .elevator_deactivate_req_fn = cfq_deactivate_request,
  1751. .elevator_queue_empty_fn = cfq_queue_empty,
  1752. .elevator_completed_req_fn = cfq_completed_request,
  1753. .elevator_former_req_fn = elv_rb_former_request,
  1754. .elevator_latter_req_fn = elv_rb_latter_request,
  1755. .elevator_set_req_fn = cfq_set_request,
  1756. .elevator_put_req_fn = cfq_put_request,
  1757. .elevator_may_queue_fn = cfq_may_queue,
  1758. .elevator_init_fn = cfq_init_queue,
  1759. .elevator_exit_fn = cfq_exit_queue,
  1760. .trim = cfq_free_io_context,
  1761. },
  1762. .elevator_attrs = cfq_attrs,
  1763. .elevator_name = "cfq",
  1764. .elevator_owner = THIS_MODULE,
  1765. };
  1766. static int __init cfq_init(void)
  1767. {
  1768. int ret;
  1769. /*
  1770. * could be 0 on HZ < 1000 setups
  1771. */
  1772. if (!cfq_slice_async)
  1773. cfq_slice_async = 1;
  1774. if (!cfq_slice_idle)
  1775. cfq_slice_idle = 1;
  1776. if (cfq_slab_setup())
  1777. return -ENOMEM;
  1778. ret = elv_register(&iosched_cfq);
  1779. if (ret)
  1780. cfq_slab_kill();
  1781. return ret;
  1782. }
  1783. static void __exit cfq_exit(void)
  1784. {
  1785. DECLARE_COMPLETION(all_gone);
  1786. elv_unregister(&iosched_cfq);
  1787. ioc_gone = &all_gone;
  1788. /* ioc_gone's update must be visible before reading ioc_count */
  1789. smp_wmb();
  1790. if (elv_ioc_count_read(ioc_count))
  1791. wait_for_completion(ioc_gone);
  1792. synchronize_rcu();
  1793. cfq_slab_kill();
  1794. }
  1795. module_init(cfq_init);
  1796. module_exit(cfq_exit);
  1797. MODULE_AUTHOR("Jens Axboe");
  1798. MODULE_LICENSE("GPL");
  1799. MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");