cfq-iosched.c 71 KB

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