cfq-iosched.c 58 KB

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