cfq-iosched.c 53 KB

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