cfq-iosched.c 52 KB

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