cfq-iosched.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427
  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. But only do so
  740. * for devices that support queuing, otherwise we still have a problem
  741. * with sync vs async workloads.
  742. */
  743. if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
  744. return;
  745. WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
  746. WARN_ON(cfq_cfqq_slice_new(cfqq));
  747. /*
  748. * idle is disabled, either manually or by past process history
  749. */
  750. if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
  751. return;
  752. /*
  753. * still requests with the driver, don't idle
  754. */
  755. if (cfqd->rq_in_driver)
  756. return;
  757. /*
  758. * task has exited, don't wait
  759. */
  760. cic = cfqd->active_cic;
  761. if (!cic || !atomic_read(&cic->ioc->nr_tasks))
  762. return;
  763. /*
  764. * See if this prio level has a good candidate
  765. */
  766. if (cfq_close_cooperator(cfqd, cfqq) &&
  767. (sample_valid(cic->ttime_samples) && cic->ttime_mean > 2))
  768. return;
  769. cfq_mark_cfqq_must_dispatch(cfqq);
  770. cfq_mark_cfqq_wait_request(cfqq);
  771. /*
  772. * we don't want to idle for seeks, but we do want to allow
  773. * fair distribution of slice time for a process doing back-to-back
  774. * seeks. so allow a little bit of time for him to submit a new rq
  775. */
  776. sl = cfqd->cfq_slice_idle;
  777. if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
  778. sl = min(sl, msecs_to_jiffies(CFQ_MIN_TT));
  779. mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
  780. cfq_log(cfqd, "arm_idle: %lu", sl);
  781. }
  782. /*
  783. * Move request from internal lists to the request queue dispatch list.
  784. */
  785. static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
  786. {
  787. struct cfq_data *cfqd = q->elevator->elevator_data;
  788. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  789. cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
  790. cfq_remove_request(rq);
  791. cfqq->dispatched++;
  792. elv_dispatch_sort(q, rq);
  793. if (cfq_cfqq_sync(cfqq))
  794. cfqd->sync_flight++;
  795. }
  796. /*
  797. * return expired entry, or NULL to just start from scratch in rbtree
  798. */
  799. static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
  800. {
  801. struct cfq_data *cfqd = cfqq->cfqd;
  802. struct request *rq;
  803. int fifo;
  804. if (cfq_cfqq_fifo_expire(cfqq))
  805. return NULL;
  806. cfq_mark_cfqq_fifo_expire(cfqq);
  807. if (list_empty(&cfqq->fifo))
  808. return NULL;
  809. fifo = cfq_cfqq_sync(cfqq);
  810. rq = rq_entry_fifo(cfqq->fifo.next);
  811. if (time_before(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo]))
  812. rq = NULL;
  813. cfq_log_cfqq(cfqd, cfqq, "fifo=%p", rq);
  814. return rq;
  815. }
  816. static inline int
  817. cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  818. {
  819. const int base_rq = cfqd->cfq_slice_async_rq;
  820. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  821. return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
  822. }
  823. /*
  824. * Select a queue for service. If we have a current active queue,
  825. * check whether to continue servicing it, or retrieve and set a new one.
  826. */
  827. static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
  828. {
  829. struct cfq_queue *cfqq;
  830. cfqq = cfqd->active_queue;
  831. if (!cfqq)
  832. goto new_queue;
  833. /*
  834. * The active queue has run out of time, expire it and select new.
  835. */
  836. if (cfq_slice_used(cfqq))
  837. goto expire;
  838. /*
  839. * The active queue has requests and isn't expired, allow it to
  840. * dispatch.
  841. */
  842. if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  843. goto keep_queue;
  844. /*
  845. * No requests pending. If the active queue still has requests in
  846. * flight or is idling for a new request, allow either of these
  847. * conditions to happen (or time out) before selecting a new queue.
  848. */
  849. if (timer_pending(&cfqd->idle_slice_timer) ||
  850. (cfqq->dispatched && cfq_cfqq_idle_window(cfqq))) {
  851. cfqq = NULL;
  852. goto keep_queue;
  853. }
  854. expire:
  855. cfq_slice_expired(cfqd, 0);
  856. new_queue:
  857. cfqq = cfq_set_active_queue(cfqd);
  858. keep_queue:
  859. return cfqq;
  860. }
  861. /*
  862. * Dispatch some requests from cfqq, moving them to the request queue
  863. * dispatch list.
  864. */
  865. static int
  866. __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  867. int max_dispatch)
  868. {
  869. int dispatched = 0;
  870. BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
  871. do {
  872. struct request *rq;
  873. /*
  874. * follow expired path, else get first next available
  875. */
  876. rq = cfq_check_fifo(cfqq);
  877. if (rq == NULL)
  878. rq = cfqq->next_rq;
  879. /*
  880. * finally, insert request into driver dispatch list
  881. */
  882. cfq_dispatch_insert(cfqd->queue, rq);
  883. dispatched++;
  884. if (!cfqd->active_cic) {
  885. atomic_inc(&RQ_CIC(rq)->ioc->refcount);
  886. cfqd->active_cic = RQ_CIC(rq);
  887. }
  888. if (RB_EMPTY_ROOT(&cfqq->sort_list))
  889. break;
  890. } while (dispatched < max_dispatch);
  891. /*
  892. * expire an async queue immediately if it has used up its slice. idle
  893. * queue always expire after 1 dispatch round.
  894. */
  895. if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
  896. dispatched >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
  897. cfq_class_idle(cfqq))) {
  898. cfqq->slice_end = jiffies + 1;
  899. cfq_slice_expired(cfqd, 0);
  900. }
  901. return dispatched;
  902. }
  903. static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
  904. {
  905. int dispatched = 0;
  906. while (cfqq->next_rq) {
  907. cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
  908. dispatched++;
  909. }
  910. BUG_ON(!list_empty(&cfqq->fifo));
  911. return dispatched;
  912. }
  913. /*
  914. * Drain our current requests. Used for barriers and when switching
  915. * io schedulers on-the-fly.
  916. */
  917. static int cfq_forced_dispatch(struct cfq_data *cfqd)
  918. {
  919. struct cfq_queue *cfqq;
  920. int dispatched = 0;
  921. while ((cfqq = cfq_rb_first(&cfqd->service_tree)) != NULL)
  922. dispatched += __cfq_forced_dispatch_cfqq(cfqq);
  923. cfq_slice_expired(cfqd, 0);
  924. BUG_ON(cfqd->busy_queues);
  925. cfq_log(cfqd, "forced_dispatch=%d\n", dispatched);
  926. return dispatched;
  927. }
  928. static int cfq_dispatch_requests(struct request_queue *q, int force)
  929. {
  930. struct cfq_data *cfqd = q->elevator->elevator_data;
  931. struct cfq_queue *cfqq;
  932. int dispatched;
  933. if (!cfqd->busy_queues)
  934. return 0;
  935. if (unlikely(force))
  936. return cfq_forced_dispatch(cfqd);
  937. dispatched = 0;
  938. while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
  939. int max_dispatch;
  940. max_dispatch = cfqd->cfq_quantum;
  941. if (cfq_class_idle(cfqq))
  942. max_dispatch = 1;
  943. if (cfqq->dispatched >= max_dispatch && cfqd->busy_queues > 1)
  944. break;
  945. if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
  946. break;
  947. cfq_clear_cfqq_must_dispatch(cfqq);
  948. cfq_clear_cfqq_wait_request(cfqq);
  949. del_timer(&cfqd->idle_slice_timer);
  950. dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
  951. }
  952. cfq_log(cfqd, "dispatched=%d", dispatched);
  953. return dispatched;
  954. }
  955. /*
  956. * task holds one reference to the queue, dropped when task exits. each rq
  957. * in-flight on this queue also holds a reference, dropped when rq is freed.
  958. *
  959. * queue lock must be held here.
  960. */
  961. static void cfq_put_queue(struct cfq_queue *cfqq)
  962. {
  963. struct cfq_data *cfqd = cfqq->cfqd;
  964. BUG_ON(atomic_read(&cfqq->ref) <= 0);
  965. if (!atomic_dec_and_test(&cfqq->ref))
  966. return;
  967. cfq_log_cfqq(cfqd, cfqq, "put_queue");
  968. BUG_ON(rb_first(&cfqq->sort_list));
  969. BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
  970. BUG_ON(cfq_cfqq_on_rr(cfqq));
  971. if (unlikely(cfqd->active_queue == cfqq)) {
  972. __cfq_slice_expired(cfqd, cfqq, 0);
  973. cfq_schedule_dispatch(cfqd);
  974. }
  975. kmem_cache_free(cfq_pool, cfqq);
  976. }
  977. /*
  978. * Must always be called with the rcu_read_lock() held
  979. */
  980. static void
  981. __call_for_each_cic(struct io_context *ioc,
  982. void (*func)(struct io_context *, struct cfq_io_context *))
  983. {
  984. struct cfq_io_context *cic;
  985. struct hlist_node *n;
  986. hlist_for_each_entry_rcu(cic, n, &ioc->cic_list, cic_list)
  987. func(ioc, cic);
  988. }
  989. /*
  990. * Call func for each cic attached to this ioc.
  991. */
  992. static void
  993. call_for_each_cic(struct io_context *ioc,
  994. void (*func)(struct io_context *, struct cfq_io_context *))
  995. {
  996. rcu_read_lock();
  997. __call_for_each_cic(ioc, func);
  998. rcu_read_unlock();
  999. }
  1000. static void cfq_cic_free_rcu(struct rcu_head *head)
  1001. {
  1002. struct cfq_io_context *cic;
  1003. cic = container_of(head, struct cfq_io_context, rcu_head);
  1004. kmem_cache_free(cfq_ioc_pool, cic);
  1005. elv_ioc_count_dec(ioc_count);
  1006. if (ioc_gone) {
  1007. /*
  1008. * CFQ scheduler is exiting, grab exit lock and check
  1009. * the pending io context count. If it hits zero,
  1010. * complete ioc_gone and set it back to NULL
  1011. */
  1012. spin_lock(&ioc_gone_lock);
  1013. if (ioc_gone && !elv_ioc_count_read(ioc_count)) {
  1014. complete(ioc_gone);
  1015. ioc_gone = NULL;
  1016. }
  1017. spin_unlock(&ioc_gone_lock);
  1018. }
  1019. }
  1020. static void cfq_cic_free(struct cfq_io_context *cic)
  1021. {
  1022. call_rcu(&cic->rcu_head, cfq_cic_free_rcu);
  1023. }
  1024. static void cic_free_func(struct io_context *ioc, struct cfq_io_context *cic)
  1025. {
  1026. unsigned long flags;
  1027. BUG_ON(!cic->dead_key);
  1028. spin_lock_irqsave(&ioc->lock, flags);
  1029. radix_tree_delete(&ioc->radix_root, cic->dead_key);
  1030. hlist_del_rcu(&cic->cic_list);
  1031. spin_unlock_irqrestore(&ioc->lock, flags);
  1032. cfq_cic_free(cic);
  1033. }
  1034. /*
  1035. * Must be called with rcu_read_lock() held or preemption otherwise disabled.
  1036. * Only two callers of this - ->dtor() which is called with the rcu_read_lock(),
  1037. * and ->trim() which is called with the task lock held
  1038. */
  1039. static void cfq_free_io_context(struct io_context *ioc)
  1040. {
  1041. /*
  1042. * ioc->refcount is zero here, or we are called from elv_unregister(),
  1043. * so no more cic's are allowed to be linked into this ioc. So it
  1044. * should be ok to iterate over the known list, we will see all cic's
  1045. * since no new ones are added.
  1046. */
  1047. __call_for_each_cic(ioc, cic_free_func);
  1048. }
  1049. static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1050. {
  1051. if (unlikely(cfqq == cfqd->active_queue)) {
  1052. __cfq_slice_expired(cfqd, cfqq, 0);
  1053. cfq_schedule_dispatch(cfqd);
  1054. }
  1055. cfq_put_queue(cfqq);
  1056. }
  1057. static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
  1058. struct cfq_io_context *cic)
  1059. {
  1060. struct io_context *ioc = cic->ioc;
  1061. list_del_init(&cic->queue_list);
  1062. /*
  1063. * Make sure key == NULL is seen for dead queues
  1064. */
  1065. smp_wmb();
  1066. cic->dead_key = (unsigned long) cic->key;
  1067. cic->key = NULL;
  1068. if (ioc->ioc_data == cic)
  1069. rcu_assign_pointer(ioc->ioc_data, NULL);
  1070. if (cic->cfqq[ASYNC]) {
  1071. cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
  1072. cic->cfqq[ASYNC] = NULL;
  1073. }
  1074. if (cic->cfqq[SYNC]) {
  1075. cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
  1076. cic->cfqq[SYNC] = NULL;
  1077. }
  1078. }
  1079. static void cfq_exit_single_io_context(struct io_context *ioc,
  1080. struct cfq_io_context *cic)
  1081. {
  1082. struct cfq_data *cfqd = cic->key;
  1083. if (cfqd) {
  1084. struct request_queue *q = cfqd->queue;
  1085. unsigned long flags;
  1086. spin_lock_irqsave(q->queue_lock, flags);
  1087. /*
  1088. * Ensure we get a fresh copy of the ->key to prevent
  1089. * race between exiting task and queue
  1090. */
  1091. smp_read_barrier_depends();
  1092. if (cic->key)
  1093. __cfq_exit_single_io_context(cfqd, cic);
  1094. spin_unlock_irqrestore(q->queue_lock, flags);
  1095. }
  1096. }
  1097. /*
  1098. * The process that ioc belongs to has exited, we need to clean up
  1099. * and put the internal structures we have that belongs to that process.
  1100. */
  1101. static void cfq_exit_io_context(struct io_context *ioc)
  1102. {
  1103. call_for_each_cic(ioc, cfq_exit_single_io_context);
  1104. }
  1105. static struct cfq_io_context *
  1106. cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
  1107. {
  1108. struct cfq_io_context *cic;
  1109. cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask | __GFP_ZERO,
  1110. cfqd->queue->node);
  1111. if (cic) {
  1112. cic->last_end_request = jiffies;
  1113. INIT_LIST_HEAD(&cic->queue_list);
  1114. INIT_HLIST_NODE(&cic->cic_list);
  1115. cic->dtor = cfq_free_io_context;
  1116. cic->exit = cfq_exit_io_context;
  1117. elv_ioc_count_inc(ioc_count);
  1118. }
  1119. return cic;
  1120. }
  1121. static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
  1122. {
  1123. struct task_struct *tsk = current;
  1124. int ioprio_class;
  1125. if (!cfq_cfqq_prio_changed(cfqq))
  1126. return;
  1127. ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
  1128. switch (ioprio_class) {
  1129. default:
  1130. printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
  1131. case IOPRIO_CLASS_NONE:
  1132. /*
  1133. * no prio set, inherit CPU scheduling settings
  1134. */
  1135. cfqq->ioprio = task_nice_ioprio(tsk);
  1136. cfqq->ioprio_class = task_nice_ioclass(tsk);
  1137. break;
  1138. case IOPRIO_CLASS_RT:
  1139. cfqq->ioprio = task_ioprio(ioc);
  1140. cfqq->ioprio_class = IOPRIO_CLASS_RT;
  1141. break;
  1142. case IOPRIO_CLASS_BE:
  1143. cfqq->ioprio = task_ioprio(ioc);
  1144. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  1145. break;
  1146. case IOPRIO_CLASS_IDLE:
  1147. cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
  1148. cfqq->ioprio = 7;
  1149. cfq_clear_cfqq_idle_window(cfqq);
  1150. break;
  1151. }
  1152. /*
  1153. * keep track of original prio settings in case we have to temporarily
  1154. * elevate the priority of this queue
  1155. */
  1156. cfqq->org_ioprio = cfqq->ioprio;
  1157. cfqq->org_ioprio_class = cfqq->ioprio_class;
  1158. cfq_clear_cfqq_prio_changed(cfqq);
  1159. }
  1160. static void changed_ioprio(struct io_context *ioc, struct cfq_io_context *cic)
  1161. {
  1162. struct cfq_data *cfqd = cic->key;
  1163. struct cfq_queue *cfqq;
  1164. unsigned long flags;
  1165. if (unlikely(!cfqd))
  1166. return;
  1167. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  1168. cfqq = cic->cfqq[ASYNC];
  1169. if (cfqq) {
  1170. struct cfq_queue *new_cfqq;
  1171. new_cfqq = cfq_get_queue(cfqd, ASYNC, cic->ioc, GFP_ATOMIC);
  1172. if (new_cfqq) {
  1173. cic->cfqq[ASYNC] = new_cfqq;
  1174. cfq_put_queue(cfqq);
  1175. }
  1176. }
  1177. cfqq = cic->cfqq[SYNC];
  1178. if (cfqq)
  1179. cfq_mark_cfqq_prio_changed(cfqq);
  1180. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  1181. }
  1182. static void cfq_ioc_set_ioprio(struct io_context *ioc)
  1183. {
  1184. call_for_each_cic(ioc, changed_ioprio);
  1185. ioc->ioprio_changed = 0;
  1186. }
  1187. static struct cfq_queue *
  1188. cfq_find_alloc_queue(struct cfq_data *cfqd, int is_sync,
  1189. struct io_context *ioc, gfp_t gfp_mask)
  1190. {
  1191. struct cfq_queue *cfqq, *new_cfqq = NULL;
  1192. struct cfq_io_context *cic;
  1193. retry:
  1194. cic = cfq_cic_lookup(cfqd, ioc);
  1195. /* cic always exists here */
  1196. cfqq = cic_to_cfqq(cic, is_sync);
  1197. if (!cfqq) {
  1198. if (new_cfqq) {
  1199. cfqq = new_cfqq;
  1200. new_cfqq = NULL;
  1201. } else if (gfp_mask & __GFP_WAIT) {
  1202. /*
  1203. * Inform the allocator of the fact that we will
  1204. * just repeat this allocation if it fails, to allow
  1205. * the allocator to do whatever it needs to attempt to
  1206. * free memory.
  1207. */
  1208. spin_unlock_irq(cfqd->queue->queue_lock);
  1209. new_cfqq = kmem_cache_alloc_node(cfq_pool,
  1210. gfp_mask | __GFP_NOFAIL | __GFP_ZERO,
  1211. cfqd->queue->node);
  1212. spin_lock_irq(cfqd->queue->queue_lock);
  1213. goto retry;
  1214. } else {
  1215. cfqq = kmem_cache_alloc_node(cfq_pool,
  1216. gfp_mask | __GFP_ZERO,
  1217. cfqd->queue->node);
  1218. if (!cfqq)
  1219. goto out;
  1220. }
  1221. RB_CLEAR_NODE(&cfqq->rb_node);
  1222. INIT_LIST_HEAD(&cfqq->fifo);
  1223. atomic_set(&cfqq->ref, 0);
  1224. cfqq->cfqd = cfqd;
  1225. cfq_mark_cfqq_prio_changed(cfqq);
  1226. cfq_mark_cfqq_queue_new(cfqq);
  1227. cfq_init_prio_data(cfqq, ioc);
  1228. if (is_sync) {
  1229. if (!cfq_class_idle(cfqq))
  1230. cfq_mark_cfqq_idle_window(cfqq);
  1231. cfq_mark_cfqq_sync(cfqq);
  1232. }
  1233. cfqq->pid = current->pid;
  1234. cfq_log_cfqq(cfqd, cfqq, "alloced");
  1235. }
  1236. if (new_cfqq)
  1237. kmem_cache_free(cfq_pool, new_cfqq);
  1238. out:
  1239. WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
  1240. return cfqq;
  1241. }
  1242. static struct cfq_queue **
  1243. cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
  1244. {
  1245. switch (ioprio_class) {
  1246. case IOPRIO_CLASS_RT:
  1247. return &cfqd->async_cfqq[0][ioprio];
  1248. case IOPRIO_CLASS_BE:
  1249. return &cfqd->async_cfqq[1][ioprio];
  1250. case IOPRIO_CLASS_IDLE:
  1251. return &cfqd->async_idle_cfqq;
  1252. default:
  1253. BUG();
  1254. }
  1255. }
  1256. static struct cfq_queue *
  1257. cfq_get_queue(struct cfq_data *cfqd, int is_sync, struct io_context *ioc,
  1258. gfp_t gfp_mask)
  1259. {
  1260. const int ioprio = task_ioprio(ioc);
  1261. const int ioprio_class = task_ioprio_class(ioc);
  1262. struct cfq_queue **async_cfqq = NULL;
  1263. struct cfq_queue *cfqq = NULL;
  1264. if (!is_sync) {
  1265. async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
  1266. cfqq = *async_cfqq;
  1267. }
  1268. if (!cfqq) {
  1269. cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
  1270. if (!cfqq)
  1271. return NULL;
  1272. }
  1273. /*
  1274. * pin the queue now that it's allocated, scheduler exit will prune it
  1275. */
  1276. if (!is_sync && !(*async_cfqq)) {
  1277. atomic_inc(&cfqq->ref);
  1278. *async_cfqq = cfqq;
  1279. }
  1280. atomic_inc(&cfqq->ref);
  1281. return cfqq;
  1282. }
  1283. /*
  1284. * We drop cfq io contexts lazily, so we may find a dead one.
  1285. */
  1286. static void
  1287. cfq_drop_dead_cic(struct cfq_data *cfqd, struct io_context *ioc,
  1288. struct cfq_io_context *cic)
  1289. {
  1290. unsigned long flags;
  1291. WARN_ON(!list_empty(&cic->queue_list));
  1292. spin_lock_irqsave(&ioc->lock, flags);
  1293. BUG_ON(ioc->ioc_data == cic);
  1294. radix_tree_delete(&ioc->radix_root, (unsigned long) cfqd);
  1295. hlist_del_rcu(&cic->cic_list);
  1296. spin_unlock_irqrestore(&ioc->lock, flags);
  1297. cfq_cic_free(cic);
  1298. }
  1299. static struct cfq_io_context *
  1300. cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
  1301. {
  1302. struct cfq_io_context *cic;
  1303. unsigned long flags;
  1304. void *k;
  1305. if (unlikely(!ioc))
  1306. return NULL;
  1307. rcu_read_lock();
  1308. /*
  1309. * we maintain a last-hit cache, to avoid browsing over the tree
  1310. */
  1311. cic = rcu_dereference(ioc->ioc_data);
  1312. if (cic && cic->key == cfqd) {
  1313. rcu_read_unlock();
  1314. return cic;
  1315. }
  1316. do {
  1317. cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd);
  1318. rcu_read_unlock();
  1319. if (!cic)
  1320. break;
  1321. /* ->key must be copied to avoid race with cfq_exit_queue() */
  1322. k = cic->key;
  1323. if (unlikely(!k)) {
  1324. cfq_drop_dead_cic(cfqd, ioc, cic);
  1325. rcu_read_lock();
  1326. continue;
  1327. }
  1328. spin_lock_irqsave(&ioc->lock, flags);
  1329. rcu_assign_pointer(ioc->ioc_data, cic);
  1330. spin_unlock_irqrestore(&ioc->lock, flags);
  1331. break;
  1332. } while (1);
  1333. return cic;
  1334. }
  1335. /*
  1336. * Add cic into ioc, using cfqd as the search key. This enables us to lookup
  1337. * the process specific cfq io context when entered from the block layer.
  1338. * Also adds the cic to a per-cfqd list, used when this queue is removed.
  1339. */
  1340. static int cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
  1341. struct cfq_io_context *cic, gfp_t gfp_mask)
  1342. {
  1343. unsigned long flags;
  1344. int ret;
  1345. ret = radix_tree_preload(gfp_mask);
  1346. if (!ret) {
  1347. cic->ioc = ioc;
  1348. cic->key = cfqd;
  1349. spin_lock_irqsave(&ioc->lock, flags);
  1350. ret = radix_tree_insert(&ioc->radix_root,
  1351. (unsigned long) cfqd, cic);
  1352. if (!ret)
  1353. hlist_add_head_rcu(&cic->cic_list, &ioc->cic_list);
  1354. spin_unlock_irqrestore(&ioc->lock, flags);
  1355. radix_tree_preload_end();
  1356. if (!ret) {
  1357. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  1358. list_add(&cic->queue_list, &cfqd->cic_list);
  1359. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  1360. }
  1361. }
  1362. if (ret)
  1363. printk(KERN_ERR "cfq: cic link failed!\n");
  1364. return ret;
  1365. }
  1366. /*
  1367. * Setup general io context and cfq io context. There can be several cfq
  1368. * io contexts per general io context, if this process is doing io to more
  1369. * than one device managed by cfq.
  1370. */
  1371. static struct cfq_io_context *
  1372. cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
  1373. {
  1374. struct io_context *ioc = NULL;
  1375. struct cfq_io_context *cic;
  1376. might_sleep_if(gfp_mask & __GFP_WAIT);
  1377. ioc = get_io_context(gfp_mask, cfqd->queue->node);
  1378. if (!ioc)
  1379. return NULL;
  1380. cic = cfq_cic_lookup(cfqd, ioc);
  1381. if (cic)
  1382. goto out;
  1383. cic = cfq_alloc_io_context(cfqd, gfp_mask);
  1384. if (cic == NULL)
  1385. goto err;
  1386. if (cfq_cic_link(cfqd, ioc, cic, gfp_mask))
  1387. goto err_free;
  1388. out:
  1389. smp_read_barrier_depends();
  1390. if (unlikely(ioc->ioprio_changed))
  1391. cfq_ioc_set_ioprio(ioc);
  1392. return cic;
  1393. err_free:
  1394. cfq_cic_free(cic);
  1395. err:
  1396. put_io_context(ioc);
  1397. return NULL;
  1398. }
  1399. static void
  1400. cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
  1401. {
  1402. unsigned long elapsed = jiffies - cic->last_end_request;
  1403. unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
  1404. cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
  1405. cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
  1406. cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
  1407. }
  1408. static void
  1409. cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
  1410. struct request *rq)
  1411. {
  1412. sector_t sdist;
  1413. u64 total;
  1414. if (cic->last_request_pos < rq->sector)
  1415. sdist = rq->sector - cic->last_request_pos;
  1416. else
  1417. sdist = cic->last_request_pos - rq->sector;
  1418. /*
  1419. * Don't allow the seek distance to get too large from the
  1420. * odd fragment, pagein, etc
  1421. */
  1422. if (cic->seek_samples <= 60) /* second&third seek */
  1423. sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
  1424. else
  1425. sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
  1426. cic->seek_samples = (7*cic->seek_samples + 256) / 8;
  1427. cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
  1428. total = cic->seek_total + (cic->seek_samples/2);
  1429. do_div(total, cic->seek_samples);
  1430. cic->seek_mean = (sector_t)total;
  1431. }
  1432. /*
  1433. * Disable idle window if the process thinks too long or seeks so much that
  1434. * it doesn't matter
  1435. */
  1436. static void
  1437. cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1438. struct cfq_io_context *cic)
  1439. {
  1440. int old_idle, enable_idle;
  1441. /*
  1442. * Don't idle for async or idle io prio class
  1443. */
  1444. if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
  1445. return;
  1446. enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
  1447. if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle ||
  1448. (cfqd->hw_tag && CIC_SEEKY(cic)))
  1449. enable_idle = 0;
  1450. else if (sample_valid(cic->ttime_samples)) {
  1451. if (cic->ttime_mean > cfqd->cfq_slice_idle)
  1452. enable_idle = 0;
  1453. else
  1454. enable_idle = 1;
  1455. }
  1456. if (old_idle != enable_idle) {
  1457. cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
  1458. if (enable_idle)
  1459. cfq_mark_cfqq_idle_window(cfqq);
  1460. else
  1461. cfq_clear_cfqq_idle_window(cfqq);
  1462. }
  1463. }
  1464. /*
  1465. * Check if new_cfqq should preempt the currently active queue. Return 0 for
  1466. * no or if we aren't sure, a 1 will cause a preempt.
  1467. */
  1468. static int
  1469. cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
  1470. struct request *rq)
  1471. {
  1472. struct cfq_queue *cfqq;
  1473. cfqq = cfqd->active_queue;
  1474. if (!cfqq)
  1475. return 0;
  1476. if (cfq_slice_used(cfqq))
  1477. return 1;
  1478. if (cfq_class_idle(new_cfqq))
  1479. return 0;
  1480. if (cfq_class_idle(cfqq))
  1481. return 1;
  1482. /*
  1483. * if the new request is sync, but the currently running queue is
  1484. * not, let the sync request have priority.
  1485. */
  1486. if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
  1487. return 1;
  1488. /*
  1489. * So both queues are sync. Let the new request get disk time if
  1490. * it's a metadata request and the current queue is doing regular IO.
  1491. */
  1492. if (rq_is_meta(rq) && !cfqq->meta_pending)
  1493. return 1;
  1494. if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
  1495. return 0;
  1496. /*
  1497. * if this request is as-good as one we would expect from the
  1498. * current cfqq, let it preempt
  1499. */
  1500. if (cfq_rq_close(cfqd, rq))
  1501. return 1;
  1502. return 0;
  1503. }
  1504. /*
  1505. * cfqq preempts the active queue. if we allowed preempt with no slice left,
  1506. * let it have half of its nominal slice.
  1507. */
  1508. static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1509. {
  1510. cfq_log_cfqq(cfqd, cfqq, "preempt");
  1511. cfq_slice_expired(cfqd, 1);
  1512. /*
  1513. * Put the new queue at the front of the of the current list,
  1514. * so we know that it will be selected next.
  1515. */
  1516. BUG_ON(!cfq_cfqq_on_rr(cfqq));
  1517. cfq_service_tree_add(cfqd, cfqq, 1);
  1518. cfqq->slice_end = 0;
  1519. cfq_mark_cfqq_slice_new(cfqq);
  1520. }
  1521. /*
  1522. * Called when a new fs request (rq) is added (to cfqq). Check if there's
  1523. * something we should do about it
  1524. */
  1525. static void
  1526. cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1527. struct request *rq)
  1528. {
  1529. struct cfq_io_context *cic = RQ_CIC(rq);
  1530. cfqd->rq_queued++;
  1531. if (rq_is_meta(rq))
  1532. cfqq->meta_pending++;
  1533. cfq_update_io_thinktime(cfqd, cic);
  1534. cfq_update_io_seektime(cfqd, cic, rq);
  1535. cfq_update_idle_window(cfqd, cfqq, cic);
  1536. cic->last_request_pos = rq->sector + rq->nr_sectors;
  1537. if (cfqq == cfqd->active_queue) {
  1538. /*
  1539. * if we are waiting for a request for this queue, let it rip
  1540. * immediately and flag that we must not expire this queue
  1541. * just now
  1542. */
  1543. if (cfq_cfqq_wait_request(cfqq)) {
  1544. cfq_mark_cfqq_must_dispatch(cfqq);
  1545. del_timer(&cfqd->idle_slice_timer);
  1546. blk_start_queueing(cfqd->queue);
  1547. }
  1548. } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
  1549. /*
  1550. * not the active queue - expire current slice if it is
  1551. * idle and has expired it's mean thinktime or this new queue
  1552. * has some old slice time left and is of higher priority
  1553. */
  1554. cfq_preempt_queue(cfqd, cfqq);
  1555. cfq_mark_cfqq_must_dispatch(cfqq);
  1556. blk_start_queueing(cfqd->queue);
  1557. }
  1558. }
  1559. static void cfq_insert_request(struct request_queue *q, struct request *rq)
  1560. {
  1561. struct cfq_data *cfqd = q->elevator->elevator_data;
  1562. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  1563. cfq_log_cfqq(cfqd, cfqq, "insert_request");
  1564. cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);
  1565. cfq_add_rq_rb(rq);
  1566. list_add_tail(&rq->queuelist, &cfqq->fifo);
  1567. cfq_rq_enqueued(cfqd, cfqq, rq);
  1568. }
  1569. /*
  1570. * Update hw_tag based on peak queue depth over 50 samples under
  1571. * sufficient load.
  1572. */
  1573. static void cfq_update_hw_tag(struct cfq_data *cfqd)
  1574. {
  1575. if (cfqd->rq_in_driver > cfqd->rq_in_driver_peak)
  1576. cfqd->rq_in_driver_peak = cfqd->rq_in_driver;
  1577. if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
  1578. cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
  1579. return;
  1580. if (cfqd->hw_tag_samples++ < 50)
  1581. return;
  1582. if (cfqd->rq_in_driver_peak >= CFQ_HW_QUEUE_MIN)
  1583. cfqd->hw_tag = 1;
  1584. else
  1585. cfqd->hw_tag = 0;
  1586. cfqd->hw_tag_samples = 0;
  1587. cfqd->rq_in_driver_peak = 0;
  1588. }
  1589. static void cfq_completed_request(struct request_queue *q, struct request *rq)
  1590. {
  1591. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  1592. struct cfq_data *cfqd = cfqq->cfqd;
  1593. const int sync = rq_is_sync(rq);
  1594. unsigned long now;
  1595. now = jiffies;
  1596. cfq_log_cfqq(cfqd, cfqq, "complete");
  1597. cfq_update_hw_tag(cfqd);
  1598. WARN_ON(!cfqd->rq_in_driver);
  1599. WARN_ON(!cfqq->dispatched);
  1600. cfqd->rq_in_driver--;
  1601. cfqq->dispatched--;
  1602. if (cfq_cfqq_sync(cfqq))
  1603. cfqd->sync_flight--;
  1604. if (!cfq_class_idle(cfqq))
  1605. cfqd->last_end_request = now;
  1606. if (sync)
  1607. RQ_CIC(rq)->last_end_request = now;
  1608. /*
  1609. * If this is the active queue, check if it needs to be expired,
  1610. * or if we want to idle in case it has no pending requests.
  1611. */
  1612. if (cfqd->active_queue == cfqq) {
  1613. if (cfq_cfqq_slice_new(cfqq)) {
  1614. cfq_set_prio_slice(cfqd, cfqq);
  1615. cfq_clear_cfqq_slice_new(cfqq);
  1616. }
  1617. if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
  1618. cfq_slice_expired(cfqd, 1);
  1619. else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list))
  1620. cfq_arm_slice_timer(cfqd);
  1621. }
  1622. if (!cfqd->rq_in_driver)
  1623. cfq_schedule_dispatch(cfqd);
  1624. }
  1625. /*
  1626. * we temporarily boost lower priority queues if they are holding fs exclusive
  1627. * resources. they are boosted to normal prio (CLASS_BE/4)
  1628. */
  1629. static void cfq_prio_boost(struct cfq_queue *cfqq)
  1630. {
  1631. if (has_fs_excl()) {
  1632. /*
  1633. * boost idle prio on transactions that would lock out other
  1634. * users of the filesystem
  1635. */
  1636. if (cfq_class_idle(cfqq))
  1637. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  1638. if (cfqq->ioprio > IOPRIO_NORM)
  1639. cfqq->ioprio = IOPRIO_NORM;
  1640. } else {
  1641. /*
  1642. * check if we need to unboost the queue
  1643. */
  1644. if (cfqq->ioprio_class != cfqq->org_ioprio_class)
  1645. cfqq->ioprio_class = cfqq->org_ioprio_class;
  1646. if (cfqq->ioprio != cfqq->org_ioprio)
  1647. cfqq->ioprio = cfqq->org_ioprio;
  1648. }
  1649. }
  1650. static inline int __cfq_may_queue(struct cfq_queue *cfqq)
  1651. {
  1652. if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
  1653. !cfq_cfqq_must_alloc_slice(cfqq)) {
  1654. cfq_mark_cfqq_must_alloc_slice(cfqq);
  1655. return ELV_MQUEUE_MUST;
  1656. }
  1657. return ELV_MQUEUE_MAY;
  1658. }
  1659. static int cfq_may_queue(struct request_queue *q, int rw)
  1660. {
  1661. struct cfq_data *cfqd = q->elevator->elevator_data;
  1662. struct task_struct *tsk = current;
  1663. struct cfq_io_context *cic;
  1664. struct cfq_queue *cfqq;
  1665. /*
  1666. * don't force setup of a queue from here, as a call to may_queue
  1667. * does not necessarily imply that a request actually will be queued.
  1668. * so just lookup a possibly existing queue, or return 'may queue'
  1669. * if that fails
  1670. */
  1671. cic = cfq_cic_lookup(cfqd, tsk->io_context);
  1672. if (!cic)
  1673. return ELV_MQUEUE_MAY;
  1674. cfqq = cic_to_cfqq(cic, rw & REQ_RW_SYNC);
  1675. if (cfqq) {
  1676. cfq_init_prio_data(cfqq, cic->ioc);
  1677. cfq_prio_boost(cfqq);
  1678. return __cfq_may_queue(cfqq);
  1679. }
  1680. return ELV_MQUEUE_MAY;
  1681. }
  1682. /*
  1683. * queue lock held here
  1684. */
  1685. static void cfq_put_request(struct request *rq)
  1686. {
  1687. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  1688. if (cfqq) {
  1689. const int rw = rq_data_dir(rq);
  1690. BUG_ON(!cfqq->allocated[rw]);
  1691. cfqq->allocated[rw]--;
  1692. put_io_context(RQ_CIC(rq)->ioc);
  1693. rq->elevator_private = NULL;
  1694. rq->elevator_private2 = NULL;
  1695. cfq_put_queue(cfqq);
  1696. }
  1697. }
  1698. /*
  1699. * Allocate cfq data structures associated with this request.
  1700. */
  1701. static int
  1702. cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
  1703. {
  1704. struct cfq_data *cfqd = q->elevator->elevator_data;
  1705. struct cfq_io_context *cic;
  1706. const int rw = rq_data_dir(rq);
  1707. const int is_sync = rq_is_sync(rq);
  1708. struct cfq_queue *cfqq;
  1709. unsigned long flags;
  1710. might_sleep_if(gfp_mask & __GFP_WAIT);
  1711. cic = cfq_get_io_context(cfqd, gfp_mask);
  1712. spin_lock_irqsave(q->queue_lock, flags);
  1713. if (!cic)
  1714. goto queue_fail;
  1715. cfqq = cic_to_cfqq(cic, is_sync);
  1716. if (!cfqq) {
  1717. cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);
  1718. if (!cfqq)
  1719. goto queue_fail;
  1720. cic_set_cfqq(cic, cfqq, is_sync);
  1721. }
  1722. cfqq->allocated[rw]++;
  1723. cfq_clear_cfqq_must_alloc(cfqq);
  1724. atomic_inc(&cfqq->ref);
  1725. spin_unlock_irqrestore(q->queue_lock, flags);
  1726. rq->elevator_private = cic;
  1727. rq->elevator_private2 = cfqq;
  1728. return 0;
  1729. queue_fail:
  1730. if (cic)
  1731. put_io_context(cic->ioc);
  1732. cfq_schedule_dispatch(cfqd);
  1733. spin_unlock_irqrestore(q->queue_lock, flags);
  1734. cfq_log(cfqd, "set_request fail");
  1735. return 1;
  1736. }
  1737. static void cfq_kick_queue(struct work_struct *work)
  1738. {
  1739. struct cfq_data *cfqd =
  1740. container_of(work, struct cfq_data, unplug_work);
  1741. struct request_queue *q = cfqd->queue;
  1742. unsigned long flags;
  1743. spin_lock_irqsave(q->queue_lock, flags);
  1744. blk_start_queueing(q);
  1745. spin_unlock_irqrestore(q->queue_lock, flags);
  1746. }
  1747. /*
  1748. * Timer running if the active_queue is currently idling inside its time slice
  1749. */
  1750. static void cfq_idle_slice_timer(unsigned long data)
  1751. {
  1752. struct cfq_data *cfqd = (struct cfq_data *) data;
  1753. struct cfq_queue *cfqq;
  1754. unsigned long flags;
  1755. int timed_out = 1;
  1756. cfq_log(cfqd, "idle timer fired");
  1757. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  1758. cfqq = cfqd->active_queue;
  1759. if (cfqq) {
  1760. timed_out = 0;
  1761. /*
  1762. * expired
  1763. */
  1764. if (cfq_slice_used(cfqq))
  1765. goto expire;
  1766. /*
  1767. * only expire and reinvoke request handler, if there are
  1768. * other queues with pending requests
  1769. */
  1770. if (!cfqd->busy_queues)
  1771. goto out_cont;
  1772. /*
  1773. * not expired and it has a request pending, let it dispatch
  1774. */
  1775. if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
  1776. cfq_mark_cfqq_must_dispatch(cfqq);
  1777. goto out_kick;
  1778. }
  1779. }
  1780. expire:
  1781. cfq_slice_expired(cfqd, timed_out);
  1782. out_kick:
  1783. cfq_schedule_dispatch(cfqd);
  1784. out_cont:
  1785. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  1786. }
  1787. static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
  1788. {
  1789. del_timer_sync(&cfqd->idle_slice_timer);
  1790. cancel_work_sync(&cfqd->unplug_work);
  1791. }
  1792. static void cfq_put_async_queues(struct cfq_data *cfqd)
  1793. {
  1794. int i;
  1795. for (i = 0; i < IOPRIO_BE_NR; i++) {
  1796. if (cfqd->async_cfqq[0][i])
  1797. cfq_put_queue(cfqd->async_cfqq[0][i]);
  1798. if (cfqd->async_cfqq[1][i])
  1799. cfq_put_queue(cfqd->async_cfqq[1][i]);
  1800. }
  1801. if (cfqd->async_idle_cfqq)
  1802. cfq_put_queue(cfqd->async_idle_cfqq);
  1803. }
  1804. static void cfq_exit_queue(struct elevator_queue *e)
  1805. {
  1806. struct cfq_data *cfqd = e->elevator_data;
  1807. struct request_queue *q = cfqd->queue;
  1808. cfq_shutdown_timer_wq(cfqd);
  1809. spin_lock_irq(q->queue_lock);
  1810. if (cfqd->active_queue)
  1811. __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
  1812. while (!list_empty(&cfqd->cic_list)) {
  1813. struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
  1814. struct cfq_io_context,
  1815. queue_list);
  1816. __cfq_exit_single_io_context(cfqd, cic);
  1817. }
  1818. cfq_put_async_queues(cfqd);
  1819. spin_unlock_irq(q->queue_lock);
  1820. cfq_shutdown_timer_wq(cfqd);
  1821. kfree(cfqd);
  1822. }
  1823. static void *cfq_init_queue(struct request_queue *q)
  1824. {
  1825. struct cfq_data *cfqd;
  1826. cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
  1827. if (!cfqd)
  1828. return NULL;
  1829. cfqd->service_tree = CFQ_RB_ROOT;
  1830. INIT_LIST_HEAD(&cfqd->cic_list);
  1831. cfqd->queue = q;
  1832. init_timer(&cfqd->idle_slice_timer);
  1833. cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
  1834. cfqd->idle_slice_timer.data = (unsigned long) cfqd;
  1835. INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
  1836. cfqd->last_end_request = jiffies;
  1837. cfqd->cfq_quantum = cfq_quantum;
  1838. cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
  1839. cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
  1840. cfqd->cfq_back_max = cfq_back_max;
  1841. cfqd->cfq_back_penalty = cfq_back_penalty;
  1842. cfqd->cfq_slice[0] = cfq_slice_async;
  1843. cfqd->cfq_slice[1] = cfq_slice_sync;
  1844. cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
  1845. cfqd->cfq_slice_idle = cfq_slice_idle;
  1846. cfqd->hw_tag = 1;
  1847. return cfqd;
  1848. }
  1849. static void cfq_slab_kill(void)
  1850. {
  1851. /*
  1852. * Caller already ensured that pending RCU callbacks are completed,
  1853. * so we should have no busy allocations at this point.
  1854. */
  1855. if (cfq_pool)
  1856. kmem_cache_destroy(cfq_pool);
  1857. if (cfq_ioc_pool)
  1858. kmem_cache_destroy(cfq_ioc_pool);
  1859. }
  1860. static int __init cfq_slab_setup(void)
  1861. {
  1862. cfq_pool = KMEM_CACHE(cfq_queue, 0);
  1863. if (!cfq_pool)
  1864. goto fail;
  1865. cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
  1866. if (!cfq_ioc_pool)
  1867. goto fail;
  1868. return 0;
  1869. fail:
  1870. cfq_slab_kill();
  1871. return -ENOMEM;
  1872. }
  1873. /*
  1874. * sysfs parts below -->
  1875. */
  1876. static ssize_t
  1877. cfq_var_show(unsigned int var, char *page)
  1878. {
  1879. return sprintf(page, "%d\n", var);
  1880. }
  1881. static ssize_t
  1882. cfq_var_store(unsigned int *var, const char *page, size_t count)
  1883. {
  1884. char *p = (char *) page;
  1885. *var = simple_strtoul(p, &p, 10);
  1886. return count;
  1887. }
  1888. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  1889. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  1890. { \
  1891. struct cfq_data *cfqd = e->elevator_data; \
  1892. unsigned int __data = __VAR; \
  1893. if (__CONV) \
  1894. __data = jiffies_to_msecs(__data); \
  1895. return cfq_var_show(__data, (page)); \
  1896. }
  1897. SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
  1898. SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
  1899. SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
  1900. SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
  1901. SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
  1902. SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
  1903. SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
  1904. SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
  1905. SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
  1906. #undef SHOW_FUNCTION
  1907. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  1908. static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
  1909. { \
  1910. struct cfq_data *cfqd = e->elevator_data; \
  1911. unsigned int __data; \
  1912. int ret = cfq_var_store(&__data, (page), count); \
  1913. if (__data < (MIN)) \
  1914. __data = (MIN); \
  1915. else if (__data > (MAX)) \
  1916. __data = (MAX); \
  1917. if (__CONV) \
  1918. *(__PTR) = msecs_to_jiffies(__data); \
  1919. else \
  1920. *(__PTR) = __data; \
  1921. return ret; \
  1922. }
  1923. STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
  1924. STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
  1925. UINT_MAX, 1);
  1926. STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
  1927. UINT_MAX, 1);
  1928. STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
  1929. STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
  1930. UINT_MAX, 0);
  1931. STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
  1932. STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
  1933. STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
  1934. STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
  1935. UINT_MAX, 0);
  1936. #undef STORE_FUNCTION
  1937. #define CFQ_ATTR(name) \
  1938. __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
  1939. static struct elv_fs_entry cfq_attrs[] = {
  1940. CFQ_ATTR(quantum),
  1941. CFQ_ATTR(fifo_expire_sync),
  1942. CFQ_ATTR(fifo_expire_async),
  1943. CFQ_ATTR(back_seek_max),
  1944. CFQ_ATTR(back_seek_penalty),
  1945. CFQ_ATTR(slice_sync),
  1946. CFQ_ATTR(slice_async),
  1947. CFQ_ATTR(slice_async_rq),
  1948. CFQ_ATTR(slice_idle),
  1949. __ATTR_NULL
  1950. };
  1951. static struct elevator_type iosched_cfq = {
  1952. .ops = {
  1953. .elevator_merge_fn = cfq_merge,
  1954. .elevator_merged_fn = cfq_merged_request,
  1955. .elevator_merge_req_fn = cfq_merged_requests,
  1956. .elevator_allow_merge_fn = cfq_allow_merge,
  1957. .elevator_dispatch_fn = cfq_dispatch_requests,
  1958. .elevator_add_req_fn = cfq_insert_request,
  1959. .elevator_activate_req_fn = cfq_activate_request,
  1960. .elevator_deactivate_req_fn = cfq_deactivate_request,
  1961. .elevator_queue_empty_fn = cfq_queue_empty,
  1962. .elevator_completed_req_fn = cfq_completed_request,
  1963. .elevator_former_req_fn = elv_rb_former_request,
  1964. .elevator_latter_req_fn = elv_rb_latter_request,
  1965. .elevator_set_req_fn = cfq_set_request,
  1966. .elevator_put_req_fn = cfq_put_request,
  1967. .elevator_may_queue_fn = cfq_may_queue,
  1968. .elevator_init_fn = cfq_init_queue,
  1969. .elevator_exit_fn = cfq_exit_queue,
  1970. .trim = cfq_free_io_context,
  1971. },
  1972. .elevator_attrs = cfq_attrs,
  1973. .elevator_name = "cfq",
  1974. .elevator_owner = THIS_MODULE,
  1975. };
  1976. static int __init cfq_init(void)
  1977. {
  1978. /*
  1979. * could be 0 on HZ < 1000 setups
  1980. */
  1981. if (!cfq_slice_async)
  1982. cfq_slice_async = 1;
  1983. if (!cfq_slice_idle)
  1984. cfq_slice_idle = 1;
  1985. if (cfq_slab_setup())
  1986. return -ENOMEM;
  1987. elv_register(&iosched_cfq);
  1988. return 0;
  1989. }
  1990. static void __exit cfq_exit(void)
  1991. {
  1992. DECLARE_COMPLETION_ONSTACK(all_gone);
  1993. elv_unregister(&iosched_cfq);
  1994. ioc_gone = &all_gone;
  1995. /* ioc_gone's update must be visible before reading ioc_count */
  1996. smp_wmb();
  1997. /*
  1998. * this also protects us from entering cfq_slab_kill() with
  1999. * pending RCU callbacks
  2000. */
  2001. if (elv_ioc_count_read(ioc_count))
  2002. wait_for_completion(&all_gone);
  2003. cfq_slab_kill();
  2004. }
  2005. module_init(cfq_init);
  2006. module_exit(cfq_exit);
  2007. MODULE_AUTHOR("Jens Axboe");
  2008. MODULE_LICENSE("GPL");
  2009. MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");