cfq-iosched.c 51 KB

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