cfq-iosched.c 57 KB

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