cfq-iosched.c 59 KB

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