cfq-iosched.c 64 KB

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