cfq-iosched.c 58 KB

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