cfq-iosched.c 53 KB

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