cfq-iosched.c 57 KB

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