cfq-iosched.c 54 KB

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