cfq-iosched.c 54 KB

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