cfq-iosched.c 65 KB

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