cfq-iosched.c 53 KB

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