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. struct request *rq;
  786. cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
  787. cfq_remove_request(crq->request);
  788. cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
  789. elv_dispatch_sort(q, crq->request);
  790. rq = list_entry(q->queue_head.prev, struct request, queuelist);
  791. cfqd->last_sector = rq->sector + rq->nr_sectors;
  792. }
  793. /*
  794. * return expired entry, or NULL to just start from scratch in rbtree
  795. */
  796. static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
  797. {
  798. struct cfq_data *cfqd = cfqq->cfqd;
  799. struct request *rq;
  800. struct cfq_rq *crq;
  801. if (cfq_cfqq_fifo_expire(cfqq))
  802. return NULL;
  803. if (!list_empty(&cfqq->fifo)) {
  804. int fifo = cfq_cfqq_class_sync(cfqq);
  805. crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
  806. rq = crq->request;
  807. if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
  808. cfq_mark_cfqq_fifo_expire(cfqq);
  809. return crq;
  810. }
  811. }
  812. return NULL;
  813. }
  814. /*
  815. * Scale schedule slice based on io priority. Use the sync time slice only
  816. * if a queue is marked sync and has sync io queued. A sync queue with async
  817. * io only, should not get full sync slice length.
  818. */
  819. static inline int
  820. cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  821. {
  822. const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
  823. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  824. return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
  825. }
  826. static inline void
  827. cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  828. {
  829. cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
  830. }
  831. static inline int
  832. cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  833. {
  834. const int base_rq = cfqd->cfq_slice_async_rq;
  835. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  836. return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
  837. }
  838. /*
  839. * get next queue for service
  840. */
  841. static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
  842. {
  843. unsigned long now = jiffies;
  844. struct cfq_queue *cfqq;
  845. cfqq = cfqd->active_queue;
  846. if (!cfqq)
  847. goto new_queue;
  848. /*
  849. * slice has expired
  850. */
  851. if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
  852. goto expire;
  853. /*
  854. * if queue has requests, dispatch one. if not, check if
  855. * enough slice is left to wait for one
  856. */
  857. if (!RB_EMPTY(&cfqq->sort_list))
  858. goto keep_queue;
  859. else if (cfq_cfqq_dispatched(cfqq)) {
  860. cfqq = NULL;
  861. goto keep_queue;
  862. } else if (cfq_cfqq_class_sync(cfqq)) {
  863. if (cfq_arm_slice_timer(cfqd, cfqq))
  864. return NULL;
  865. }
  866. expire:
  867. cfq_slice_expired(cfqd, 0);
  868. new_queue:
  869. cfqq = cfq_set_active_queue(cfqd);
  870. keep_queue:
  871. return cfqq;
  872. }
  873. static int
  874. __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  875. int max_dispatch)
  876. {
  877. int dispatched = 0;
  878. BUG_ON(RB_EMPTY(&cfqq->sort_list));
  879. do {
  880. struct cfq_rq *crq;
  881. /*
  882. * follow expired path, else get first next available
  883. */
  884. if ((crq = cfq_check_fifo(cfqq)) == NULL)
  885. crq = cfqq->next_crq;
  886. /*
  887. * finally, insert request into driver dispatch list
  888. */
  889. cfq_dispatch_insert(cfqd->queue, crq);
  890. cfqd->dispatch_slice++;
  891. dispatched++;
  892. if (!cfqd->active_cic) {
  893. atomic_inc(&crq->io_context->ioc->refcount);
  894. cfqd->active_cic = crq->io_context;
  895. }
  896. if (RB_EMPTY(&cfqq->sort_list))
  897. break;
  898. } while (dispatched < max_dispatch);
  899. /*
  900. * if slice end isn't set yet, set it.
  901. */
  902. if (!cfqq->slice_end)
  903. cfq_set_prio_slice(cfqd, cfqq);
  904. /*
  905. * expire an async queue immediately if it has used up its slice. idle
  906. * queue always expire after 1 dispatch round.
  907. */
  908. if ((!cfq_cfqq_sync(cfqq) &&
  909. cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
  910. cfq_class_idle(cfqq) ||
  911. !cfq_cfqq_idle_window(cfqq))
  912. cfq_slice_expired(cfqd, 0);
  913. return dispatched;
  914. }
  915. static int
  916. cfq_forced_dispatch_cfqqs(struct list_head *list)
  917. {
  918. struct cfq_queue *cfqq, *next;
  919. struct cfq_rq *crq;
  920. int dispatched;
  921. dispatched = 0;
  922. list_for_each_entry_safe(cfqq, next, list, cfq_list) {
  923. while ((crq = cfqq->next_crq)) {
  924. cfq_dispatch_insert(cfqq->cfqd->queue, crq);
  925. dispatched++;
  926. }
  927. BUG_ON(!list_empty(&cfqq->fifo));
  928. }
  929. return dispatched;
  930. }
  931. static int
  932. cfq_forced_dispatch(struct cfq_data *cfqd)
  933. {
  934. int i, dispatched = 0;
  935. for (i = 0; i < CFQ_PRIO_LISTS; i++)
  936. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
  937. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
  938. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
  939. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
  940. cfq_slice_expired(cfqd, 0);
  941. BUG_ON(cfqd->busy_queues);
  942. return dispatched;
  943. }
  944. static int
  945. cfq_dispatch_requests(request_queue_t *q, int force)
  946. {
  947. struct cfq_data *cfqd = q->elevator->elevator_data;
  948. struct cfq_queue *cfqq, *prev_cfqq;
  949. int dispatched;
  950. if (!cfqd->busy_queues)
  951. return 0;
  952. if (unlikely(force))
  953. return cfq_forced_dispatch(cfqd);
  954. dispatched = 0;
  955. prev_cfqq = NULL;
  956. while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
  957. int max_dispatch;
  958. /*
  959. * Don't repeat dispatch from the previous queue.
  960. */
  961. if (prev_cfqq == cfqq)
  962. break;
  963. cfq_clear_cfqq_must_dispatch(cfqq);
  964. cfq_clear_cfqq_wait_request(cfqq);
  965. del_timer(&cfqd->idle_slice_timer);
  966. max_dispatch = cfqd->cfq_quantum;
  967. if (cfq_class_idle(cfqq))
  968. max_dispatch = 1;
  969. dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
  970. /*
  971. * If the dispatch cfqq has idling enabled and is still
  972. * the active queue, break out.
  973. */
  974. if (cfq_cfqq_idle_window(cfqq) && cfqd->active_queue)
  975. break;
  976. prev_cfqq = cfqq;
  977. }
  978. return dispatched;
  979. }
  980. /*
  981. * task holds one reference to the queue, dropped when task exits. each crq
  982. * in-flight on this queue also holds a reference, dropped when crq is freed.
  983. *
  984. * queue lock must be held here.
  985. */
  986. static void cfq_put_queue(struct cfq_queue *cfqq)
  987. {
  988. struct cfq_data *cfqd = cfqq->cfqd;
  989. BUG_ON(atomic_read(&cfqq->ref) <= 0);
  990. if (!atomic_dec_and_test(&cfqq->ref))
  991. return;
  992. BUG_ON(rb_first(&cfqq->sort_list));
  993. BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
  994. BUG_ON(cfq_cfqq_on_rr(cfqq));
  995. if (unlikely(cfqd->active_queue == cfqq))
  996. __cfq_slice_expired(cfqd, cfqq, 0);
  997. /*
  998. * it's on the empty list and still hashed
  999. */
  1000. list_del(&cfqq->cfq_list);
  1001. hlist_del(&cfqq->cfq_hash);
  1002. kmem_cache_free(cfq_pool, cfqq);
  1003. }
  1004. static inline struct cfq_queue *
  1005. __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
  1006. const int hashval)
  1007. {
  1008. struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
  1009. struct hlist_node *entry;
  1010. struct cfq_queue *__cfqq;
  1011. hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
  1012. const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
  1013. if (__cfqq->key == key && (__p == prio || !prio))
  1014. return __cfqq;
  1015. }
  1016. return NULL;
  1017. }
  1018. static struct cfq_queue *
  1019. cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
  1020. {
  1021. return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
  1022. }
  1023. static void cfq_free_io_context(struct io_context *ioc)
  1024. {
  1025. struct cfq_io_context *__cic;
  1026. struct rb_node *n;
  1027. int freed = 0;
  1028. while ((n = rb_first(&ioc->cic_root)) != NULL) {
  1029. __cic = rb_entry(n, struct cfq_io_context, rb_node);
  1030. rb_erase(&__cic->rb_node, &ioc->cic_root);
  1031. kmem_cache_free(cfq_ioc_pool, __cic);
  1032. freed++;
  1033. }
  1034. if (atomic_sub_and_test(freed, &ioc_count) && ioc_gone)
  1035. complete(ioc_gone);
  1036. }
  1037. static void cfq_trim(struct io_context *ioc)
  1038. {
  1039. ioc->set_ioprio = NULL;
  1040. cfq_free_io_context(ioc);
  1041. }
  1042. /*
  1043. * Called with interrupts disabled
  1044. */
  1045. static void cfq_exit_single_io_context(struct cfq_io_context *cic)
  1046. {
  1047. struct cfq_data *cfqd = cic->key;
  1048. request_queue_t *q;
  1049. if (!cfqd)
  1050. return;
  1051. q = cfqd->queue;
  1052. WARN_ON(!irqs_disabled());
  1053. spin_lock(q->queue_lock);
  1054. if (cic->cfqq[ASYNC]) {
  1055. if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
  1056. __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
  1057. cfq_put_queue(cic->cfqq[ASYNC]);
  1058. cic->cfqq[ASYNC] = NULL;
  1059. }
  1060. if (cic->cfqq[SYNC]) {
  1061. if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
  1062. __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
  1063. cfq_put_queue(cic->cfqq[SYNC]);
  1064. cic->cfqq[SYNC] = NULL;
  1065. }
  1066. cic->key = NULL;
  1067. list_del_init(&cic->queue_list);
  1068. spin_unlock(q->queue_lock);
  1069. }
  1070. static void cfq_exit_io_context(struct io_context *ioc)
  1071. {
  1072. struct cfq_io_context *__cic;
  1073. unsigned long flags;
  1074. struct rb_node *n;
  1075. /*
  1076. * put the reference this task is holding to the various queues
  1077. */
  1078. spin_lock_irqsave(&cfq_exit_lock, flags);
  1079. n = rb_first(&ioc->cic_root);
  1080. while (n != NULL) {
  1081. __cic = rb_entry(n, struct cfq_io_context, rb_node);
  1082. cfq_exit_single_io_context(__cic);
  1083. n = rb_next(n);
  1084. }
  1085. spin_unlock_irqrestore(&cfq_exit_lock, flags);
  1086. }
  1087. static struct cfq_io_context *
  1088. cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
  1089. {
  1090. struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
  1091. if (cic) {
  1092. memset(cic, 0, sizeof(*cic));
  1093. cic->last_end_request = jiffies;
  1094. INIT_LIST_HEAD(&cic->queue_list);
  1095. cic->dtor = cfq_free_io_context;
  1096. cic->exit = cfq_exit_io_context;
  1097. atomic_inc(&ioc_count);
  1098. }
  1099. return cic;
  1100. }
  1101. static void cfq_init_prio_data(struct cfq_queue *cfqq)
  1102. {
  1103. struct task_struct *tsk = current;
  1104. int ioprio_class;
  1105. if (!cfq_cfqq_prio_changed(cfqq))
  1106. return;
  1107. ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
  1108. switch (ioprio_class) {
  1109. default:
  1110. printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
  1111. case IOPRIO_CLASS_NONE:
  1112. /*
  1113. * no prio set, place us in the middle of the BE classes
  1114. */
  1115. cfqq->ioprio = task_nice_ioprio(tsk);
  1116. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  1117. break;
  1118. case IOPRIO_CLASS_RT:
  1119. cfqq->ioprio = task_ioprio(tsk);
  1120. cfqq->ioprio_class = IOPRIO_CLASS_RT;
  1121. break;
  1122. case IOPRIO_CLASS_BE:
  1123. cfqq->ioprio = task_ioprio(tsk);
  1124. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  1125. break;
  1126. case IOPRIO_CLASS_IDLE:
  1127. cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
  1128. cfqq->ioprio = 7;
  1129. cfq_clear_cfqq_idle_window(cfqq);
  1130. break;
  1131. }
  1132. /*
  1133. * keep track of original prio settings in case we have to temporarily
  1134. * elevate the priority of this queue
  1135. */
  1136. cfqq->org_ioprio = cfqq->ioprio;
  1137. cfqq->org_ioprio_class = cfqq->ioprio_class;
  1138. if (cfq_cfqq_on_rr(cfqq))
  1139. cfq_resort_rr_list(cfqq, 0);
  1140. cfq_clear_cfqq_prio_changed(cfqq);
  1141. }
  1142. static inline void changed_ioprio(struct cfq_io_context *cic)
  1143. {
  1144. struct cfq_data *cfqd = cic->key;
  1145. struct cfq_queue *cfqq;
  1146. if (unlikely(!cfqd))
  1147. return;
  1148. spin_lock(cfqd->queue->queue_lock);
  1149. cfqq = cic->cfqq[ASYNC];
  1150. if (cfqq) {
  1151. struct cfq_queue *new_cfqq;
  1152. new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
  1153. GFP_ATOMIC);
  1154. if (new_cfqq) {
  1155. cic->cfqq[ASYNC] = new_cfqq;
  1156. cfq_put_queue(cfqq);
  1157. }
  1158. }
  1159. cfqq = cic->cfqq[SYNC];
  1160. if (cfqq)
  1161. cfq_mark_cfqq_prio_changed(cfqq);
  1162. spin_unlock(cfqd->queue->queue_lock);
  1163. }
  1164. /*
  1165. * callback from sys_ioprio_set, irqs are disabled
  1166. */
  1167. static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
  1168. {
  1169. struct cfq_io_context *cic;
  1170. struct rb_node *n;
  1171. spin_lock(&cfq_exit_lock);
  1172. n = rb_first(&ioc->cic_root);
  1173. while (n != NULL) {
  1174. cic = rb_entry(n, struct cfq_io_context, rb_node);
  1175. changed_ioprio(cic);
  1176. n = rb_next(n);
  1177. }
  1178. spin_unlock(&cfq_exit_lock);
  1179. return 0;
  1180. }
  1181. static struct cfq_queue *
  1182. cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
  1183. gfp_t gfp_mask)
  1184. {
  1185. const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
  1186. struct cfq_queue *cfqq, *new_cfqq = NULL;
  1187. unsigned short ioprio;
  1188. retry:
  1189. ioprio = tsk->ioprio;
  1190. cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
  1191. if (!cfqq) {
  1192. if (new_cfqq) {
  1193. cfqq = new_cfqq;
  1194. new_cfqq = NULL;
  1195. } else if (gfp_mask & __GFP_WAIT) {
  1196. spin_unlock_irq(cfqd->queue->queue_lock);
  1197. new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
  1198. spin_lock_irq(cfqd->queue->queue_lock);
  1199. goto retry;
  1200. } else {
  1201. cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
  1202. if (!cfqq)
  1203. goto out;
  1204. }
  1205. memset(cfqq, 0, sizeof(*cfqq));
  1206. INIT_HLIST_NODE(&cfqq->cfq_hash);
  1207. INIT_LIST_HEAD(&cfqq->cfq_list);
  1208. RB_CLEAR_ROOT(&cfqq->sort_list);
  1209. INIT_LIST_HEAD(&cfqq->fifo);
  1210. cfqq->key = key;
  1211. hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
  1212. atomic_set(&cfqq->ref, 0);
  1213. cfqq->cfqd = cfqd;
  1214. cfqq->service_last = 0;
  1215. /*
  1216. * set ->slice_left to allow preemption for a new process
  1217. */
  1218. cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
  1219. cfq_mark_cfqq_idle_window(cfqq);
  1220. cfq_mark_cfqq_prio_changed(cfqq);
  1221. cfq_init_prio_data(cfqq);
  1222. }
  1223. if (new_cfqq)
  1224. kmem_cache_free(cfq_pool, new_cfqq);
  1225. atomic_inc(&cfqq->ref);
  1226. out:
  1227. WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
  1228. return cfqq;
  1229. }
  1230. static void
  1231. cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
  1232. {
  1233. spin_lock(&cfq_exit_lock);
  1234. rb_erase(&cic->rb_node, &ioc->cic_root);
  1235. list_del_init(&cic->queue_list);
  1236. spin_unlock(&cfq_exit_lock);
  1237. kmem_cache_free(cfq_ioc_pool, cic);
  1238. atomic_dec(&ioc_count);
  1239. }
  1240. static struct cfq_io_context *
  1241. cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
  1242. {
  1243. struct rb_node *n;
  1244. struct cfq_io_context *cic;
  1245. void *k, *key = cfqd;
  1246. restart:
  1247. n = ioc->cic_root.rb_node;
  1248. while (n) {
  1249. cic = rb_entry(n, struct cfq_io_context, rb_node);
  1250. /* ->key must be copied to avoid race with cfq_exit_queue() */
  1251. k = cic->key;
  1252. if (unlikely(!k)) {
  1253. cfq_drop_dead_cic(ioc, cic);
  1254. goto restart;
  1255. }
  1256. if (key < k)
  1257. n = n->rb_left;
  1258. else if (key > k)
  1259. n = n->rb_right;
  1260. else
  1261. return cic;
  1262. }
  1263. return NULL;
  1264. }
  1265. static inline void
  1266. cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
  1267. struct cfq_io_context *cic)
  1268. {
  1269. struct rb_node **p;
  1270. struct rb_node *parent;
  1271. struct cfq_io_context *__cic;
  1272. void *k;
  1273. cic->ioc = ioc;
  1274. cic->key = cfqd;
  1275. ioc->set_ioprio = cfq_ioc_set_ioprio;
  1276. restart:
  1277. parent = NULL;
  1278. p = &ioc->cic_root.rb_node;
  1279. while (*p) {
  1280. parent = *p;
  1281. __cic = rb_entry(parent, struct cfq_io_context, rb_node);
  1282. /* ->key must be copied to avoid race with cfq_exit_queue() */
  1283. k = __cic->key;
  1284. if (unlikely(!k)) {
  1285. cfq_drop_dead_cic(ioc, cic);
  1286. goto restart;
  1287. }
  1288. if (cic->key < k)
  1289. p = &(*p)->rb_left;
  1290. else if (cic->key > k)
  1291. p = &(*p)->rb_right;
  1292. else
  1293. BUG();
  1294. }
  1295. spin_lock(&cfq_exit_lock);
  1296. rb_link_node(&cic->rb_node, parent, p);
  1297. rb_insert_color(&cic->rb_node, &ioc->cic_root);
  1298. list_add(&cic->queue_list, &cfqd->cic_list);
  1299. spin_unlock(&cfq_exit_lock);
  1300. }
  1301. /*
  1302. * Setup general io context and cfq io context. There can be several cfq
  1303. * io contexts per general io context, if this process is doing io to more
  1304. * than one device managed by cfq.
  1305. */
  1306. static struct cfq_io_context *
  1307. cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
  1308. {
  1309. struct io_context *ioc = NULL;
  1310. struct cfq_io_context *cic;
  1311. might_sleep_if(gfp_mask & __GFP_WAIT);
  1312. ioc = get_io_context(gfp_mask);
  1313. if (!ioc)
  1314. return NULL;
  1315. cic = cfq_cic_rb_lookup(cfqd, ioc);
  1316. if (cic)
  1317. goto out;
  1318. cic = cfq_alloc_io_context(cfqd, gfp_mask);
  1319. if (cic == NULL)
  1320. goto err;
  1321. cfq_cic_link(cfqd, ioc, cic);
  1322. out:
  1323. return cic;
  1324. err:
  1325. put_io_context(ioc);
  1326. return NULL;
  1327. }
  1328. static void
  1329. cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
  1330. {
  1331. unsigned long elapsed, ttime;
  1332. /*
  1333. * if this context already has stuff queued, thinktime is from
  1334. * last queue not last end
  1335. */
  1336. #if 0
  1337. if (time_after(cic->last_end_request, cic->last_queue))
  1338. elapsed = jiffies - cic->last_end_request;
  1339. else
  1340. elapsed = jiffies - cic->last_queue;
  1341. #else
  1342. elapsed = jiffies - cic->last_end_request;
  1343. #endif
  1344. ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
  1345. cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
  1346. cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
  1347. cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
  1348. }
  1349. static void
  1350. cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
  1351. struct cfq_rq *crq)
  1352. {
  1353. sector_t sdist;
  1354. u64 total;
  1355. if (cic->last_request_pos < crq->request->sector)
  1356. sdist = crq->request->sector - cic->last_request_pos;
  1357. else
  1358. sdist = cic->last_request_pos - crq->request->sector;
  1359. /*
  1360. * Don't allow the seek distance to get too large from the
  1361. * odd fragment, pagein, etc
  1362. */
  1363. if (cic->seek_samples <= 60) /* second&third seek */
  1364. sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
  1365. else
  1366. sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
  1367. cic->seek_samples = (7*cic->seek_samples + 256) / 8;
  1368. cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
  1369. total = cic->seek_total + (cic->seek_samples/2);
  1370. do_div(total, cic->seek_samples);
  1371. cic->seek_mean = (sector_t)total;
  1372. }
  1373. /*
  1374. * Disable idle window if the process thinks too long or seeks so much that
  1375. * it doesn't matter
  1376. */
  1377. static void
  1378. cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1379. struct cfq_io_context *cic)
  1380. {
  1381. int enable_idle = cfq_cfqq_idle_window(cfqq);
  1382. if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
  1383. (cfqd->hw_tag && CIC_SEEKY(cic)))
  1384. enable_idle = 0;
  1385. else if (sample_valid(cic->ttime_samples)) {
  1386. if (cic->ttime_mean > cfqd->cfq_slice_idle)
  1387. enable_idle = 0;
  1388. else
  1389. enable_idle = 1;
  1390. }
  1391. if (enable_idle)
  1392. cfq_mark_cfqq_idle_window(cfqq);
  1393. else
  1394. cfq_clear_cfqq_idle_window(cfqq);
  1395. }
  1396. /*
  1397. * Check if new_cfqq should preempt the currently active queue. Return 0 for
  1398. * no or if we aren't sure, a 1 will cause a preempt.
  1399. */
  1400. static int
  1401. cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
  1402. struct cfq_rq *crq)
  1403. {
  1404. struct cfq_queue *cfqq = cfqd->active_queue;
  1405. if (cfq_class_idle(new_cfqq))
  1406. return 0;
  1407. if (!cfqq)
  1408. return 0;
  1409. if (cfq_class_idle(cfqq))
  1410. return 1;
  1411. if (!cfq_cfqq_wait_request(new_cfqq))
  1412. return 0;
  1413. /*
  1414. * if it doesn't have slice left, forget it
  1415. */
  1416. if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
  1417. return 0;
  1418. if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
  1419. return 1;
  1420. return 0;
  1421. }
  1422. /*
  1423. * cfqq preempts the active queue. if we allowed preempt with no slice left,
  1424. * let it have half of its nominal slice.
  1425. */
  1426. static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1427. {
  1428. struct cfq_queue *__cfqq, *next;
  1429. list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
  1430. cfq_resort_rr_list(__cfqq, 1);
  1431. if (!cfqq->slice_left)
  1432. cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
  1433. cfqq->slice_end = cfqq->slice_left + jiffies;
  1434. cfq_slice_expired(cfqd, 1);
  1435. __cfq_set_active_queue(cfqd, cfqq);
  1436. }
  1437. /*
  1438. * should really be a ll_rw_blk.c helper
  1439. */
  1440. static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1441. {
  1442. request_queue_t *q = cfqd->queue;
  1443. if (!blk_queue_plugged(q))
  1444. q->request_fn(q);
  1445. else
  1446. __generic_unplug_device(q);
  1447. }
  1448. /*
  1449. * Called when a new fs request (crq) is added (to cfqq). Check if there's
  1450. * something we should do about it
  1451. */
  1452. static void
  1453. cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1454. struct cfq_rq *crq)
  1455. {
  1456. struct cfq_io_context *cic = crq->io_context;
  1457. /*
  1458. * we never wait for an async request and we don't allow preemption
  1459. * of an async request. so just return early
  1460. */
  1461. if (!cfq_crq_is_sync(crq)) {
  1462. /*
  1463. * sync process issued an async request, if it's waiting
  1464. * then expire it and kick rq handling.
  1465. */
  1466. if (cic == cfqd->active_cic &&
  1467. del_timer(&cfqd->idle_slice_timer)) {
  1468. cfq_slice_expired(cfqd, 0);
  1469. cfq_start_queueing(cfqd, cfqq);
  1470. }
  1471. return;
  1472. }
  1473. cfq_update_io_thinktime(cfqd, cic);
  1474. cfq_update_io_seektime(cfqd, cic, crq);
  1475. cfq_update_idle_window(cfqd, cfqq, cic);
  1476. cic->last_queue = jiffies;
  1477. cic->last_request_pos = crq->request->sector + crq->request->nr_sectors;
  1478. if (cfqq == cfqd->active_queue) {
  1479. /*
  1480. * if we are waiting for a request for this queue, let it rip
  1481. * immediately and flag that we must not expire this queue
  1482. * just now
  1483. */
  1484. if (cfq_cfqq_wait_request(cfqq)) {
  1485. cfq_mark_cfqq_must_dispatch(cfqq);
  1486. del_timer(&cfqd->idle_slice_timer);
  1487. cfq_start_queueing(cfqd, cfqq);
  1488. }
  1489. } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
  1490. /*
  1491. * not the active queue - expire current slice if it is
  1492. * idle and has expired it's mean thinktime or this new queue
  1493. * has some old slice time left and is of higher priority
  1494. */
  1495. cfq_preempt_queue(cfqd, cfqq);
  1496. cfq_mark_cfqq_must_dispatch(cfqq);
  1497. cfq_start_queueing(cfqd, cfqq);
  1498. }
  1499. }
  1500. static void cfq_insert_request(request_queue_t *q, struct request *rq)
  1501. {
  1502. struct cfq_data *cfqd = q->elevator->elevator_data;
  1503. struct cfq_rq *crq = RQ_DATA(rq);
  1504. struct cfq_queue *cfqq = crq->cfq_queue;
  1505. cfq_init_prio_data(cfqq);
  1506. cfq_add_crq_rb(crq);
  1507. list_add_tail(&rq->queuelist, &cfqq->fifo);
  1508. if (rq_mergeable(rq))
  1509. cfq_add_crq_hash(cfqd, crq);
  1510. cfq_crq_enqueued(cfqd, cfqq, crq);
  1511. }
  1512. static void cfq_completed_request(request_queue_t *q, struct request *rq)
  1513. {
  1514. struct cfq_rq *crq = RQ_DATA(rq);
  1515. struct cfq_queue *cfqq = crq->cfq_queue;
  1516. struct cfq_data *cfqd = cfqq->cfqd;
  1517. const int sync = cfq_crq_is_sync(crq);
  1518. unsigned long now;
  1519. now = jiffies;
  1520. WARN_ON(!cfqd->rq_in_driver);
  1521. WARN_ON(!cfqq->on_dispatch[sync]);
  1522. cfqd->rq_in_driver--;
  1523. cfqq->on_dispatch[sync]--;
  1524. if (!cfq_class_idle(cfqq))
  1525. cfqd->last_end_request = now;
  1526. if (!cfq_cfqq_dispatched(cfqq)) {
  1527. if (cfq_cfqq_on_rr(cfqq)) {
  1528. cfqq->service_last = now;
  1529. cfq_resort_rr_list(cfqq, 0);
  1530. }
  1531. }
  1532. if (sync)
  1533. crq->io_context->last_end_request = now;
  1534. /*
  1535. * If this is the active queue, check if it needs to be expired,
  1536. * or if we want to idle in case it has no pending requests.
  1537. */
  1538. if (cfqd->active_queue == cfqq) {
  1539. if (time_after(now, cfqq->slice_end))
  1540. cfq_slice_expired(cfqd, 0);
  1541. else if (sync && RB_EMPTY(&cfqq->sort_list)) {
  1542. if (!cfq_arm_slice_timer(cfqd, cfqq))
  1543. cfq_schedule_dispatch(cfqd);
  1544. }
  1545. }
  1546. }
  1547. static struct request *
  1548. cfq_former_request(request_queue_t *q, struct request *rq)
  1549. {
  1550. struct cfq_rq *crq = RQ_DATA(rq);
  1551. struct rb_node *rbprev = rb_prev(&crq->rb_node);
  1552. if (rbprev)
  1553. return rb_entry_crq(rbprev)->request;
  1554. return NULL;
  1555. }
  1556. static struct request *
  1557. cfq_latter_request(request_queue_t *q, struct request *rq)
  1558. {
  1559. struct cfq_rq *crq = RQ_DATA(rq);
  1560. struct rb_node *rbnext = rb_next(&crq->rb_node);
  1561. if (rbnext)
  1562. return rb_entry_crq(rbnext)->request;
  1563. return NULL;
  1564. }
  1565. /*
  1566. * we temporarily boost lower priority queues if they are holding fs exclusive
  1567. * resources. they are boosted to normal prio (CLASS_BE/4)
  1568. */
  1569. static void cfq_prio_boost(struct cfq_queue *cfqq)
  1570. {
  1571. const int ioprio_class = cfqq->ioprio_class;
  1572. const int ioprio = cfqq->ioprio;
  1573. if (has_fs_excl()) {
  1574. /*
  1575. * boost idle prio on transactions that would lock out other
  1576. * users of the filesystem
  1577. */
  1578. if (cfq_class_idle(cfqq))
  1579. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  1580. if (cfqq->ioprio > IOPRIO_NORM)
  1581. cfqq->ioprio = IOPRIO_NORM;
  1582. } else {
  1583. /*
  1584. * check if we need to unboost the queue
  1585. */
  1586. if (cfqq->ioprio_class != cfqq->org_ioprio_class)
  1587. cfqq->ioprio_class = cfqq->org_ioprio_class;
  1588. if (cfqq->ioprio != cfqq->org_ioprio)
  1589. cfqq->ioprio = cfqq->org_ioprio;
  1590. }
  1591. /*
  1592. * refile between round-robin lists if we moved the priority class
  1593. */
  1594. if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
  1595. cfq_cfqq_on_rr(cfqq))
  1596. cfq_resort_rr_list(cfqq, 0);
  1597. }
  1598. static inline int
  1599. __cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1600. struct task_struct *task, int rw)
  1601. {
  1602. if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
  1603. !cfq_cfqq_must_alloc_slice(cfqq)) {
  1604. cfq_mark_cfqq_must_alloc_slice(cfqq);
  1605. return ELV_MQUEUE_MUST;
  1606. }
  1607. return ELV_MQUEUE_MAY;
  1608. }
  1609. static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
  1610. {
  1611. struct cfq_data *cfqd = q->elevator->elevator_data;
  1612. struct task_struct *tsk = current;
  1613. struct cfq_queue *cfqq;
  1614. /*
  1615. * don't force setup of a queue from here, as a call to may_queue
  1616. * does not necessarily imply that a request actually will be queued.
  1617. * so just lookup a possibly existing queue, or return 'may queue'
  1618. * if that fails
  1619. */
  1620. cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
  1621. if (cfqq) {
  1622. cfq_init_prio_data(cfqq);
  1623. cfq_prio_boost(cfqq);
  1624. return __cfq_may_queue(cfqd, cfqq, tsk, rw);
  1625. }
  1626. return ELV_MQUEUE_MAY;
  1627. }
  1628. static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
  1629. {
  1630. struct cfq_data *cfqd = q->elevator->elevator_data;
  1631. if (unlikely(cfqd->rq_starved)) {
  1632. struct request_list *rl = &q->rq;
  1633. smp_mb();
  1634. if (waitqueue_active(&rl->wait[READ]))
  1635. wake_up(&rl->wait[READ]);
  1636. if (waitqueue_active(&rl->wait[WRITE]))
  1637. wake_up(&rl->wait[WRITE]);
  1638. }
  1639. }
  1640. /*
  1641. * queue lock held here
  1642. */
  1643. static void cfq_put_request(request_queue_t *q, struct request *rq)
  1644. {
  1645. struct cfq_data *cfqd = q->elevator->elevator_data;
  1646. struct cfq_rq *crq = RQ_DATA(rq);
  1647. if (crq) {
  1648. struct cfq_queue *cfqq = crq->cfq_queue;
  1649. const int rw = rq_data_dir(rq);
  1650. BUG_ON(!cfqq->allocated[rw]);
  1651. cfqq->allocated[rw]--;
  1652. put_io_context(crq->io_context->ioc);
  1653. mempool_free(crq, cfqd->crq_pool);
  1654. rq->elevator_private = NULL;
  1655. cfq_check_waiters(q, cfqq);
  1656. cfq_put_queue(cfqq);
  1657. }
  1658. }
  1659. /*
  1660. * Allocate cfq data structures associated with this request.
  1661. */
  1662. static int
  1663. cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
  1664. gfp_t gfp_mask)
  1665. {
  1666. struct cfq_data *cfqd = q->elevator->elevator_data;
  1667. struct task_struct *tsk = current;
  1668. struct cfq_io_context *cic;
  1669. const int rw = rq_data_dir(rq);
  1670. pid_t key = cfq_queue_pid(tsk, rw);
  1671. struct cfq_queue *cfqq;
  1672. struct cfq_rq *crq;
  1673. unsigned long flags;
  1674. int is_sync = key != CFQ_KEY_ASYNC;
  1675. might_sleep_if(gfp_mask & __GFP_WAIT);
  1676. cic = cfq_get_io_context(cfqd, gfp_mask);
  1677. spin_lock_irqsave(q->queue_lock, flags);
  1678. if (!cic)
  1679. goto queue_fail;
  1680. if (!cic->cfqq[is_sync]) {
  1681. cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
  1682. if (!cfqq)
  1683. goto queue_fail;
  1684. cic->cfqq[is_sync] = cfqq;
  1685. } else
  1686. cfqq = cic->cfqq[is_sync];
  1687. cfqq->allocated[rw]++;
  1688. cfq_clear_cfqq_must_alloc(cfqq);
  1689. cfqd->rq_starved = 0;
  1690. atomic_inc(&cfqq->ref);
  1691. spin_unlock_irqrestore(q->queue_lock, flags);
  1692. crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
  1693. if (crq) {
  1694. RB_CLEAR(&crq->rb_node);
  1695. crq->rb_key = 0;
  1696. crq->request = rq;
  1697. INIT_HLIST_NODE(&crq->hash);
  1698. crq->cfq_queue = cfqq;
  1699. crq->io_context = cic;
  1700. if (is_sync)
  1701. cfq_mark_crq_is_sync(crq);
  1702. else
  1703. cfq_clear_crq_is_sync(crq);
  1704. rq->elevator_private = crq;
  1705. return 0;
  1706. }
  1707. spin_lock_irqsave(q->queue_lock, flags);
  1708. cfqq->allocated[rw]--;
  1709. if (!(cfqq->allocated[0] + cfqq->allocated[1]))
  1710. cfq_mark_cfqq_must_alloc(cfqq);
  1711. cfq_put_queue(cfqq);
  1712. queue_fail:
  1713. if (cic)
  1714. put_io_context(cic->ioc);
  1715. /*
  1716. * mark us rq allocation starved. we need to kickstart the process
  1717. * ourselves if there are no pending requests that can do it for us.
  1718. * that would be an extremely rare OOM situation
  1719. */
  1720. cfqd->rq_starved = 1;
  1721. cfq_schedule_dispatch(cfqd);
  1722. spin_unlock_irqrestore(q->queue_lock, flags);
  1723. return 1;
  1724. }
  1725. static void cfq_kick_queue(void *data)
  1726. {
  1727. request_queue_t *q = data;
  1728. struct cfq_data *cfqd = q->elevator->elevator_data;
  1729. unsigned long flags;
  1730. spin_lock_irqsave(q->queue_lock, flags);
  1731. if (cfqd->rq_starved) {
  1732. struct request_list *rl = &q->rq;
  1733. /*
  1734. * we aren't guaranteed to get a request after this, but we
  1735. * have to be opportunistic
  1736. */
  1737. smp_mb();
  1738. if (waitqueue_active(&rl->wait[READ]))
  1739. wake_up(&rl->wait[READ]);
  1740. if (waitqueue_active(&rl->wait[WRITE]))
  1741. wake_up(&rl->wait[WRITE]);
  1742. }
  1743. blk_remove_plug(q);
  1744. q->request_fn(q);
  1745. spin_unlock_irqrestore(q->queue_lock, flags);
  1746. }
  1747. /*
  1748. * Timer running if the active_queue is currently idling inside its time slice
  1749. */
  1750. static void cfq_idle_slice_timer(unsigned long data)
  1751. {
  1752. struct cfq_data *cfqd = (struct cfq_data *) data;
  1753. struct cfq_queue *cfqq;
  1754. unsigned long flags;
  1755. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  1756. if ((cfqq = cfqd->active_queue) != NULL) {
  1757. unsigned long now = jiffies;
  1758. /*
  1759. * expired
  1760. */
  1761. if (time_after(now, cfqq->slice_end))
  1762. goto expire;
  1763. /*
  1764. * only expire and reinvoke request handler, if there are
  1765. * other queues with pending requests
  1766. */
  1767. if (!cfqd->busy_queues)
  1768. goto out_cont;
  1769. /*
  1770. * not expired and it has a request pending, let it dispatch
  1771. */
  1772. if (!RB_EMPTY(&cfqq->sort_list)) {
  1773. cfq_mark_cfqq_must_dispatch(cfqq);
  1774. goto out_kick;
  1775. }
  1776. }
  1777. expire:
  1778. cfq_slice_expired(cfqd, 0);
  1779. out_kick:
  1780. cfq_schedule_dispatch(cfqd);
  1781. out_cont:
  1782. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  1783. }
  1784. /*
  1785. * Timer running if an idle class queue is waiting for service
  1786. */
  1787. static void cfq_idle_class_timer(unsigned long data)
  1788. {
  1789. struct cfq_data *cfqd = (struct cfq_data *) data;
  1790. unsigned long flags, end;
  1791. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  1792. /*
  1793. * race with a non-idle queue, reset timer
  1794. */
  1795. end = cfqd->last_end_request + CFQ_IDLE_GRACE;
  1796. if (!time_after_eq(jiffies, end))
  1797. mod_timer(&cfqd->idle_class_timer, end);
  1798. else
  1799. cfq_schedule_dispatch(cfqd);
  1800. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  1801. }
  1802. static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
  1803. {
  1804. del_timer_sync(&cfqd->idle_slice_timer);
  1805. del_timer_sync(&cfqd->idle_class_timer);
  1806. blk_sync_queue(cfqd->queue);
  1807. }
  1808. static void cfq_exit_queue(elevator_t *e)
  1809. {
  1810. struct cfq_data *cfqd = e->elevator_data;
  1811. request_queue_t *q = cfqd->queue;
  1812. cfq_shutdown_timer_wq(cfqd);
  1813. spin_lock(&cfq_exit_lock);
  1814. spin_lock_irq(q->queue_lock);
  1815. if (cfqd->active_queue)
  1816. __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
  1817. while (!list_empty(&cfqd->cic_list)) {
  1818. struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
  1819. struct cfq_io_context,
  1820. queue_list);
  1821. if (cic->cfqq[ASYNC]) {
  1822. cfq_put_queue(cic->cfqq[ASYNC]);
  1823. cic->cfqq[ASYNC] = NULL;
  1824. }
  1825. if (cic->cfqq[SYNC]) {
  1826. cfq_put_queue(cic->cfqq[SYNC]);
  1827. cic->cfqq[SYNC] = NULL;
  1828. }
  1829. cic->key = NULL;
  1830. list_del_init(&cic->queue_list);
  1831. }
  1832. spin_unlock_irq(q->queue_lock);
  1833. spin_unlock(&cfq_exit_lock);
  1834. cfq_shutdown_timer_wq(cfqd);
  1835. mempool_destroy(cfqd->crq_pool);
  1836. kfree(cfqd->crq_hash);
  1837. kfree(cfqd->cfq_hash);
  1838. kfree(cfqd);
  1839. }
  1840. static void *cfq_init_queue(request_queue_t *q, elevator_t *e)
  1841. {
  1842. struct cfq_data *cfqd;
  1843. int i;
  1844. cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
  1845. if (!cfqd)
  1846. return NULL;
  1847. memset(cfqd, 0, sizeof(*cfqd));
  1848. for (i = 0; i < CFQ_PRIO_LISTS; i++)
  1849. INIT_LIST_HEAD(&cfqd->rr_list[i]);
  1850. INIT_LIST_HEAD(&cfqd->busy_rr);
  1851. INIT_LIST_HEAD(&cfqd->cur_rr);
  1852. INIT_LIST_HEAD(&cfqd->idle_rr);
  1853. INIT_LIST_HEAD(&cfqd->empty_list);
  1854. INIT_LIST_HEAD(&cfqd->cic_list);
  1855. cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
  1856. if (!cfqd->crq_hash)
  1857. goto out_crqhash;
  1858. cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
  1859. if (!cfqd->cfq_hash)
  1860. goto out_cfqhash;
  1861. cfqd->crq_pool = mempool_create_slab_pool(BLKDEV_MIN_RQ, crq_pool);
  1862. if (!cfqd->crq_pool)
  1863. goto out_crqpool;
  1864. for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
  1865. INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
  1866. for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
  1867. INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
  1868. cfqd->queue = q;
  1869. init_timer(&cfqd->idle_slice_timer);
  1870. cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
  1871. cfqd->idle_slice_timer.data = (unsigned long) cfqd;
  1872. init_timer(&cfqd->idle_class_timer);
  1873. cfqd->idle_class_timer.function = cfq_idle_class_timer;
  1874. cfqd->idle_class_timer.data = (unsigned long) cfqd;
  1875. INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
  1876. cfqd->cfq_queued = cfq_queued;
  1877. cfqd->cfq_quantum = cfq_quantum;
  1878. cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
  1879. cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
  1880. cfqd->cfq_back_max = cfq_back_max;
  1881. cfqd->cfq_back_penalty = cfq_back_penalty;
  1882. cfqd->cfq_slice[0] = cfq_slice_async;
  1883. cfqd->cfq_slice[1] = cfq_slice_sync;
  1884. cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
  1885. cfqd->cfq_slice_idle = cfq_slice_idle;
  1886. return cfqd;
  1887. out_crqpool:
  1888. kfree(cfqd->cfq_hash);
  1889. out_cfqhash:
  1890. kfree(cfqd->crq_hash);
  1891. out_crqhash:
  1892. kfree(cfqd);
  1893. return NULL;
  1894. }
  1895. static void cfq_slab_kill(void)
  1896. {
  1897. if (crq_pool)
  1898. kmem_cache_destroy(crq_pool);
  1899. if (cfq_pool)
  1900. kmem_cache_destroy(cfq_pool);
  1901. if (cfq_ioc_pool)
  1902. kmem_cache_destroy(cfq_ioc_pool);
  1903. }
  1904. static int __init cfq_slab_setup(void)
  1905. {
  1906. crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
  1907. NULL, NULL);
  1908. if (!crq_pool)
  1909. goto fail;
  1910. cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
  1911. NULL, NULL);
  1912. if (!cfq_pool)
  1913. goto fail;
  1914. cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
  1915. sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
  1916. if (!cfq_ioc_pool)
  1917. goto fail;
  1918. return 0;
  1919. fail:
  1920. cfq_slab_kill();
  1921. return -ENOMEM;
  1922. }
  1923. /*
  1924. * sysfs parts below -->
  1925. */
  1926. static ssize_t
  1927. cfq_var_show(unsigned int var, char *page)
  1928. {
  1929. return sprintf(page, "%d\n", var);
  1930. }
  1931. static ssize_t
  1932. cfq_var_store(unsigned int *var, const char *page, size_t count)
  1933. {
  1934. char *p = (char *) page;
  1935. *var = simple_strtoul(p, &p, 10);
  1936. return count;
  1937. }
  1938. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  1939. static ssize_t __FUNC(elevator_t *e, char *page) \
  1940. { \
  1941. struct cfq_data *cfqd = e->elevator_data; \
  1942. unsigned int __data = __VAR; \
  1943. if (__CONV) \
  1944. __data = jiffies_to_msecs(__data); \
  1945. return cfq_var_show(__data, (page)); \
  1946. }
  1947. SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
  1948. SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
  1949. SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
  1950. SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
  1951. SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
  1952. SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
  1953. SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
  1954. SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
  1955. SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
  1956. SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
  1957. #undef SHOW_FUNCTION
  1958. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  1959. static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
  1960. { \
  1961. struct cfq_data *cfqd = e->elevator_data; \
  1962. unsigned int __data; \
  1963. int ret = cfq_var_store(&__data, (page), count); \
  1964. if (__data < (MIN)) \
  1965. __data = (MIN); \
  1966. else if (__data > (MAX)) \
  1967. __data = (MAX); \
  1968. if (__CONV) \
  1969. *(__PTR) = msecs_to_jiffies(__data); \
  1970. else \
  1971. *(__PTR) = __data; \
  1972. return ret; \
  1973. }
  1974. STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
  1975. STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
  1976. STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
  1977. STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
  1978. STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
  1979. STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
  1980. STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
  1981. STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
  1982. STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
  1983. STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
  1984. #undef STORE_FUNCTION
  1985. #define CFQ_ATTR(name) \
  1986. __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
  1987. static struct elv_fs_entry cfq_attrs[] = {
  1988. CFQ_ATTR(quantum),
  1989. CFQ_ATTR(queued),
  1990. CFQ_ATTR(fifo_expire_sync),
  1991. CFQ_ATTR(fifo_expire_async),
  1992. CFQ_ATTR(back_seek_max),
  1993. CFQ_ATTR(back_seek_penalty),
  1994. CFQ_ATTR(slice_sync),
  1995. CFQ_ATTR(slice_async),
  1996. CFQ_ATTR(slice_async_rq),
  1997. CFQ_ATTR(slice_idle),
  1998. __ATTR_NULL
  1999. };
  2000. static struct elevator_type iosched_cfq = {
  2001. .ops = {
  2002. .elevator_merge_fn = cfq_merge,
  2003. .elevator_merged_fn = cfq_merged_request,
  2004. .elevator_merge_req_fn = cfq_merged_requests,
  2005. .elevator_dispatch_fn = cfq_dispatch_requests,
  2006. .elevator_add_req_fn = cfq_insert_request,
  2007. .elevator_activate_req_fn = cfq_activate_request,
  2008. .elevator_deactivate_req_fn = cfq_deactivate_request,
  2009. .elevator_queue_empty_fn = cfq_queue_empty,
  2010. .elevator_completed_req_fn = cfq_completed_request,
  2011. .elevator_former_req_fn = cfq_former_request,
  2012. .elevator_latter_req_fn = cfq_latter_request,
  2013. .elevator_set_req_fn = cfq_set_request,
  2014. .elevator_put_req_fn = cfq_put_request,
  2015. .elevator_may_queue_fn = cfq_may_queue,
  2016. .elevator_init_fn = cfq_init_queue,
  2017. .elevator_exit_fn = cfq_exit_queue,
  2018. .trim = cfq_trim,
  2019. },
  2020. .elevator_attrs = cfq_attrs,
  2021. .elevator_name = "cfq",
  2022. .elevator_owner = THIS_MODULE,
  2023. };
  2024. static int __init cfq_init(void)
  2025. {
  2026. int ret;
  2027. /*
  2028. * could be 0 on HZ < 1000 setups
  2029. */
  2030. if (!cfq_slice_async)
  2031. cfq_slice_async = 1;
  2032. if (!cfq_slice_idle)
  2033. cfq_slice_idle = 1;
  2034. if (cfq_slab_setup())
  2035. return -ENOMEM;
  2036. ret = elv_register(&iosched_cfq);
  2037. if (ret)
  2038. cfq_slab_kill();
  2039. return ret;
  2040. }
  2041. static void __exit cfq_exit(void)
  2042. {
  2043. DECLARE_COMPLETION(all_gone);
  2044. elv_unregister(&iosched_cfq);
  2045. ioc_gone = &all_gone;
  2046. /* ioc_gone's update must be visible before reading ioc_count */
  2047. smp_wmb();
  2048. if (atomic_read(&ioc_count))
  2049. wait_for_completion(ioc_gone);
  2050. synchronize_rcu();
  2051. cfq_slab_kill();
  2052. }
  2053. module_init(cfq_init);
  2054. module_exit(cfq_exit);
  2055. MODULE_AUTHOR("Jens Axboe");
  2056. MODULE_LICENSE("GPL");
  2057. MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");