cfq-iosched.c 55 KB

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