cfq-iosched.c 59 KB

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