cfq-iosched.c 64 KB

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