cfq-iosched.c 55 KB

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