cfq-iosched.c 58 KB

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