cfq-iosched.c 64 KB

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