cfq-iosched.c 78 KB

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