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