cfq-iosched.c 77 KB

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