cfq-iosched.c 73 KB

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