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