cfq-iosched.c 77 KB

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