cfq-iosched.c 58 KB

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