cfq-iosched.c 65 KB

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