cfq-iosched.c 52 KB

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