cfq-iosched.c 56 KB

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