cfq-iosched.c 65 KB

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