cfq-iosched.c 54 KB

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