cfq-iosched.c 57 KB

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