cfq-iosched.c 52 KB

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