cfq-iosched.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522
  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. static DEFINE_RWLOCK(cfq_exit_lock);
  45. /*
  46. * for the hash of cfqq inside the cfqd
  47. */
  48. #define CFQ_QHASH_SHIFT 6
  49. #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
  50. #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
  51. /*
  52. * for the hash of crq inside the cfqq
  53. */
  54. #define CFQ_MHASH_SHIFT 6
  55. #define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
  56. #define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
  57. #define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
  58. #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
  59. #define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
  60. #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
  61. #define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
  62. #define RQ_DATA(rq) (rq)->elevator_private
  63. /*
  64. * rb-tree defines
  65. */
  66. #define RB_NONE (2)
  67. #define RB_EMPTY(node) ((node)->rb_node == NULL)
  68. #define RB_CLEAR_COLOR(node) (node)->rb_color = RB_NONE
  69. #define RB_CLEAR(node) do { \
  70. (node)->rb_parent = NULL; \
  71. RB_CLEAR_COLOR((node)); \
  72. (node)->rb_right = NULL; \
  73. (node)->rb_left = NULL; \
  74. } while (0)
  75. #define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
  76. #define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
  77. #define rq_rb_key(rq) (rq)->sector
  78. static kmem_cache_t *crq_pool;
  79. static kmem_cache_t *cfq_pool;
  80. static kmem_cache_t *cfq_ioc_pool;
  81. #define CFQ_PRIO_LISTS IOPRIO_BE_NR
  82. #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
  83. #define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
  84. #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
  85. #define ASYNC (0)
  86. #define SYNC (1)
  87. #define cfq_cfqq_dispatched(cfqq) \
  88. ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
  89. #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
  90. #define cfq_cfqq_sync(cfqq) \
  91. (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
  92. /*
  93. * Per block device queue structure
  94. */
  95. struct cfq_data {
  96. atomic_t ref;
  97. request_queue_t *queue;
  98. /*
  99. * rr list of queues with requests and the count of them
  100. */
  101. struct list_head rr_list[CFQ_PRIO_LISTS];
  102. struct list_head busy_rr;
  103. struct list_head cur_rr;
  104. struct list_head idle_rr;
  105. unsigned int busy_queues;
  106. /*
  107. * non-ordered list of empty cfqq's
  108. */
  109. struct list_head empty_list;
  110. /*
  111. * cfqq lookup hash
  112. */
  113. struct hlist_head *cfq_hash;
  114. /*
  115. * global crq hash for all queues
  116. */
  117. struct hlist_head *crq_hash;
  118. unsigned int max_queued;
  119. mempool_t *crq_pool;
  120. int rq_in_driver;
  121. /*
  122. * schedule slice state info
  123. */
  124. /*
  125. * idle window management
  126. */
  127. struct timer_list idle_slice_timer;
  128. struct work_struct unplug_work;
  129. struct cfq_queue *active_queue;
  130. struct cfq_io_context *active_cic;
  131. int cur_prio, cur_end_prio;
  132. unsigned int dispatch_slice;
  133. struct timer_list idle_class_timer;
  134. sector_t last_sector;
  135. unsigned long last_end_request;
  136. unsigned int rq_starved;
  137. /*
  138. * tunables, see top of file
  139. */
  140. unsigned int cfq_quantum;
  141. unsigned int cfq_queued;
  142. unsigned int cfq_fifo_expire[2];
  143. unsigned int cfq_back_penalty;
  144. unsigned int cfq_back_max;
  145. unsigned int cfq_slice[2];
  146. unsigned int cfq_slice_async_rq;
  147. unsigned int cfq_slice_idle;
  148. unsigned int cfq_max_depth;
  149. struct list_head cic_list;
  150. };
  151. /*
  152. * Per process-grouping structure
  153. */
  154. struct cfq_queue {
  155. /* reference count */
  156. atomic_t ref;
  157. /* parent cfq_data */
  158. struct cfq_data *cfqd;
  159. /* cfqq lookup hash */
  160. struct hlist_node cfq_hash;
  161. /* hash key */
  162. unsigned int key;
  163. /* on either rr or empty list of cfqd */
  164. struct list_head cfq_list;
  165. /* sorted list of pending requests */
  166. struct rb_root sort_list;
  167. /* if fifo isn't expired, next request to serve */
  168. struct cfq_rq *next_crq;
  169. /* requests queued in sort_list */
  170. int queued[2];
  171. /* currently allocated requests */
  172. int allocated[2];
  173. /* fifo list of requests in sort_list */
  174. struct list_head fifo;
  175. unsigned long slice_start;
  176. unsigned long slice_end;
  177. unsigned long slice_left;
  178. unsigned long service_last;
  179. /* number of requests that are on the dispatch list */
  180. int on_dispatch[2];
  181. /* io prio of this group */
  182. unsigned short ioprio, org_ioprio;
  183. unsigned short ioprio_class, org_ioprio_class;
  184. /* various state flags, see below */
  185. unsigned int flags;
  186. };
  187. struct cfq_rq {
  188. struct rb_node rb_node;
  189. sector_t rb_key;
  190. struct request *request;
  191. struct hlist_node hash;
  192. struct cfq_queue *cfq_queue;
  193. struct cfq_io_context *io_context;
  194. unsigned int crq_flags;
  195. };
  196. enum cfqq_state_flags {
  197. CFQ_CFQQ_FLAG_on_rr = 0,
  198. CFQ_CFQQ_FLAG_wait_request,
  199. CFQ_CFQQ_FLAG_must_alloc,
  200. CFQ_CFQQ_FLAG_must_alloc_slice,
  201. CFQ_CFQQ_FLAG_must_dispatch,
  202. CFQ_CFQQ_FLAG_fifo_expire,
  203. CFQ_CFQQ_FLAG_idle_window,
  204. CFQ_CFQQ_FLAG_prio_changed,
  205. };
  206. #define CFQ_CFQQ_FNS(name) \
  207. static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
  208. { \
  209. cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
  210. } \
  211. static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
  212. { \
  213. cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
  214. } \
  215. static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
  216. { \
  217. return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
  218. }
  219. CFQ_CFQQ_FNS(on_rr);
  220. CFQ_CFQQ_FNS(wait_request);
  221. CFQ_CFQQ_FNS(must_alloc);
  222. CFQ_CFQQ_FNS(must_alloc_slice);
  223. CFQ_CFQQ_FNS(must_dispatch);
  224. CFQ_CFQQ_FNS(fifo_expire);
  225. CFQ_CFQQ_FNS(idle_window);
  226. CFQ_CFQQ_FNS(prio_changed);
  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->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. }
  612. cfqd->active_queue = cfqq;
  613. }
  614. /*
  615. * current cfqq expired its slice (or was too idle), select new one
  616. */
  617. static void
  618. __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  619. int preempted)
  620. {
  621. unsigned long now = jiffies;
  622. if (cfq_cfqq_wait_request(cfqq))
  623. del_timer(&cfqd->idle_slice_timer);
  624. if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
  625. cfqq->service_last = now;
  626. cfq_schedule_dispatch(cfqd);
  627. }
  628. cfq_clear_cfqq_must_dispatch(cfqq);
  629. cfq_clear_cfqq_wait_request(cfqq);
  630. /*
  631. * store what was left of this slice, if the queue idled out
  632. * or was preempted
  633. */
  634. if (time_after(cfqq->slice_end, now))
  635. cfqq->slice_left = cfqq->slice_end - now;
  636. else
  637. cfqq->slice_left = 0;
  638. if (cfq_cfqq_on_rr(cfqq))
  639. cfq_resort_rr_list(cfqq, preempted);
  640. if (cfqq == cfqd->active_queue)
  641. cfqd->active_queue = NULL;
  642. if (cfqd->active_cic) {
  643. put_io_context(cfqd->active_cic->ioc);
  644. cfqd->active_cic = NULL;
  645. }
  646. cfqd->dispatch_slice = 0;
  647. }
  648. static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
  649. {
  650. struct cfq_queue *cfqq = cfqd->active_queue;
  651. if (cfqq)
  652. __cfq_slice_expired(cfqd, cfqq, preempted);
  653. }
  654. /*
  655. * 0
  656. * 0,1
  657. * 0,1,2
  658. * 0,1,2,3
  659. * 0,1,2,3,4
  660. * 0,1,2,3,4,5
  661. * 0,1,2,3,4,5,6
  662. * 0,1,2,3,4,5,6,7
  663. */
  664. static int cfq_get_next_prio_level(struct cfq_data *cfqd)
  665. {
  666. int prio, wrap;
  667. prio = -1;
  668. wrap = 0;
  669. do {
  670. int p;
  671. for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
  672. if (!list_empty(&cfqd->rr_list[p])) {
  673. prio = p;
  674. break;
  675. }
  676. }
  677. if (prio != -1)
  678. break;
  679. cfqd->cur_prio = 0;
  680. if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
  681. cfqd->cur_end_prio = 0;
  682. if (wrap)
  683. break;
  684. wrap = 1;
  685. }
  686. } while (1);
  687. if (unlikely(prio == -1))
  688. return -1;
  689. BUG_ON(prio >= CFQ_PRIO_LISTS);
  690. list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
  691. cfqd->cur_prio = prio + 1;
  692. if (cfqd->cur_prio > cfqd->cur_end_prio) {
  693. cfqd->cur_end_prio = cfqd->cur_prio;
  694. cfqd->cur_prio = 0;
  695. }
  696. if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
  697. cfqd->cur_prio = 0;
  698. cfqd->cur_end_prio = 0;
  699. }
  700. return prio;
  701. }
  702. static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
  703. {
  704. struct cfq_queue *cfqq = NULL;
  705. /*
  706. * if current list is non-empty, grab first entry. if it is empty,
  707. * get next prio level and grab first entry then if any are spliced
  708. */
  709. if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
  710. cfqq = list_entry_cfqq(cfqd->cur_rr.next);
  711. /*
  712. * if we have idle queues and no rt or be queues had pending
  713. * requests, either allow immediate service if the grace period
  714. * has passed or arm the idle grace timer
  715. */
  716. if (!cfqq && !list_empty(&cfqd->idle_rr)) {
  717. unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
  718. if (time_after_eq(jiffies, end))
  719. cfqq = list_entry_cfqq(cfqd->idle_rr.next);
  720. else
  721. mod_timer(&cfqd->idle_class_timer, end);
  722. }
  723. __cfq_set_active_queue(cfqd, cfqq);
  724. return cfqq;
  725. }
  726. static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  727. {
  728. unsigned long sl;
  729. WARN_ON(!RB_EMPTY(&cfqq->sort_list));
  730. WARN_ON(cfqq != cfqd->active_queue);
  731. /*
  732. * idle is disabled, either manually or by past process history
  733. */
  734. if (!cfqd->cfq_slice_idle)
  735. return 0;
  736. if (!cfq_cfqq_idle_window(cfqq))
  737. return 0;
  738. /*
  739. * task has exited, don't wait
  740. */
  741. if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
  742. return 0;
  743. cfq_mark_cfqq_must_dispatch(cfqq);
  744. cfq_mark_cfqq_wait_request(cfqq);
  745. sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
  746. mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
  747. return 1;
  748. }
  749. static void cfq_dispatch_insert(request_queue_t *q, struct cfq_rq *crq)
  750. {
  751. struct cfq_data *cfqd = q->elevator->elevator_data;
  752. struct cfq_queue *cfqq = crq->cfq_queue;
  753. cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
  754. cfq_remove_request(crq->request);
  755. cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
  756. elv_dispatch_sort(q, crq->request);
  757. }
  758. /*
  759. * return expired entry, or NULL to just start from scratch in rbtree
  760. */
  761. static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
  762. {
  763. struct cfq_data *cfqd = cfqq->cfqd;
  764. struct request *rq;
  765. struct cfq_rq *crq;
  766. if (cfq_cfqq_fifo_expire(cfqq))
  767. return NULL;
  768. if (!list_empty(&cfqq->fifo)) {
  769. int fifo = cfq_cfqq_class_sync(cfqq);
  770. crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
  771. rq = crq->request;
  772. if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
  773. cfq_mark_cfqq_fifo_expire(cfqq);
  774. return crq;
  775. }
  776. }
  777. return NULL;
  778. }
  779. /*
  780. * Scale schedule slice based on io priority. Use the sync time slice only
  781. * if a queue is marked sync and has sync io queued. A sync queue with async
  782. * io only, should not get full sync slice length.
  783. */
  784. static inline int
  785. cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  786. {
  787. const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
  788. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  789. return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
  790. }
  791. static inline void
  792. cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  793. {
  794. cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
  795. }
  796. static inline int
  797. cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  798. {
  799. const int base_rq = cfqd->cfq_slice_async_rq;
  800. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  801. return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
  802. }
  803. /*
  804. * get next queue for service
  805. */
  806. static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
  807. {
  808. unsigned long now = jiffies;
  809. struct cfq_queue *cfqq;
  810. cfqq = cfqd->active_queue;
  811. if (!cfqq)
  812. goto new_queue;
  813. /*
  814. * slice has expired
  815. */
  816. if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
  817. goto expire;
  818. /*
  819. * if queue has requests, dispatch one. if not, check if
  820. * enough slice is left to wait for one
  821. */
  822. if (!RB_EMPTY(&cfqq->sort_list))
  823. goto keep_queue;
  824. else if (cfq_cfqq_class_sync(cfqq) &&
  825. time_before(now, cfqq->slice_end)) {
  826. if (cfq_arm_slice_timer(cfqd, cfqq))
  827. return NULL;
  828. }
  829. expire:
  830. cfq_slice_expired(cfqd, 0);
  831. new_queue:
  832. cfqq = cfq_set_active_queue(cfqd);
  833. keep_queue:
  834. return cfqq;
  835. }
  836. static int
  837. __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  838. int max_dispatch)
  839. {
  840. int dispatched = 0;
  841. BUG_ON(RB_EMPTY(&cfqq->sort_list));
  842. do {
  843. struct cfq_rq *crq;
  844. /*
  845. * follow expired path, else get first next available
  846. */
  847. if ((crq = cfq_check_fifo(cfqq)) == NULL)
  848. crq = cfqq->next_crq;
  849. /*
  850. * finally, insert request into driver dispatch list
  851. */
  852. cfq_dispatch_insert(cfqd->queue, crq);
  853. cfqd->dispatch_slice++;
  854. dispatched++;
  855. if (!cfqd->active_cic) {
  856. atomic_inc(&crq->io_context->ioc->refcount);
  857. cfqd->active_cic = crq->io_context;
  858. }
  859. if (RB_EMPTY(&cfqq->sort_list))
  860. break;
  861. } while (dispatched < max_dispatch);
  862. /*
  863. * if slice end isn't set yet, set it. if at least one request was
  864. * sync, use the sync time slice value
  865. */
  866. if (!cfqq->slice_end)
  867. cfq_set_prio_slice(cfqd, cfqq);
  868. /*
  869. * expire an async queue immediately if it has used up its slice. idle
  870. * queue always expire after 1 dispatch round.
  871. */
  872. if ((!cfq_cfqq_sync(cfqq) &&
  873. cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
  874. cfq_class_idle(cfqq))
  875. cfq_slice_expired(cfqd, 0);
  876. return dispatched;
  877. }
  878. static int
  879. cfq_forced_dispatch_cfqqs(struct list_head *list)
  880. {
  881. int dispatched = 0;
  882. struct cfq_queue *cfqq, *next;
  883. struct cfq_rq *crq;
  884. list_for_each_entry_safe(cfqq, next, list, cfq_list) {
  885. while ((crq = cfqq->next_crq)) {
  886. cfq_dispatch_insert(cfqq->cfqd->queue, crq);
  887. dispatched++;
  888. }
  889. BUG_ON(!list_empty(&cfqq->fifo));
  890. }
  891. return dispatched;
  892. }
  893. static int
  894. cfq_forced_dispatch(struct cfq_data *cfqd)
  895. {
  896. int i, dispatched = 0;
  897. for (i = 0; i < CFQ_PRIO_LISTS; i++)
  898. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
  899. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
  900. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
  901. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
  902. cfq_slice_expired(cfqd, 0);
  903. BUG_ON(cfqd->busy_queues);
  904. return dispatched;
  905. }
  906. static int
  907. cfq_dispatch_requests(request_queue_t *q, int force)
  908. {
  909. struct cfq_data *cfqd = q->elevator->elevator_data;
  910. struct cfq_queue *cfqq;
  911. if (!cfqd->busy_queues)
  912. return 0;
  913. if (unlikely(force))
  914. return cfq_forced_dispatch(cfqd);
  915. cfqq = cfq_select_queue(cfqd);
  916. if (cfqq) {
  917. int max_dispatch;
  918. /*
  919. * if idle window is disabled, allow queue buildup
  920. */
  921. if (!cfq_cfqq_idle_window(cfqq) &&
  922. cfqd->rq_in_driver >= cfqd->cfq_max_depth)
  923. return 0;
  924. cfq_clear_cfqq_must_dispatch(cfqq);
  925. cfq_clear_cfqq_wait_request(cfqq);
  926. del_timer(&cfqd->idle_slice_timer);
  927. max_dispatch = cfqd->cfq_quantum;
  928. if (cfq_class_idle(cfqq))
  929. max_dispatch = 1;
  930. return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
  931. }
  932. return 0;
  933. }
  934. /*
  935. * task holds one reference to the queue, dropped when task exits. each crq
  936. * in-flight on this queue also holds a reference, dropped when crq is freed.
  937. *
  938. * queue lock must be held here.
  939. */
  940. static void cfq_put_queue(struct cfq_queue *cfqq)
  941. {
  942. struct cfq_data *cfqd = cfqq->cfqd;
  943. BUG_ON(atomic_read(&cfqq->ref) <= 0);
  944. if (!atomic_dec_and_test(&cfqq->ref))
  945. return;
  946. BUG_ON(rb_first(&cfqq->sort_list));
  947. BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
  948. BUG_ON(cfq_cfqq_on_rr(cfqq));
  949. if (unlikely(cfqd->active_queue == cfqq))
  950. __cfq_slice_expired(cfqd, cfqq, 0);
  951. cfq_put_cfqd(cfqq->cfqd);
  952. /*
  953. * it's on the empty list and still hashed
  954. */
  955. list_del(&cfqq->cfq_list);
  956. hlist_del(&cfqq->cfq_hash);
  957. kmem_cache_free(cfq_pool, cfqq);
  958. }
  959. static inline struct cfq_queue *
  960. __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
  961. const int hashval)
  962. {
  963. struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
  964. struct hlist_node *entry, *next;
  965. hlist_for_each_safe(entry, next, hash_list) {
  966. struct cfq_queue *__cfqq = list_entry_qhash(entry);
  967. const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
  968. if (__cfqq->key == key && (__p == prio || prio == CFQ_KEY_ANY))
  969. return __cfqq;
  970. }
  971. return NULL;
  972. }
  973. static struct cfq_queue *
  974. cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
  975. {
  976. return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
  977. }
  978. static void cfq_free_io_context(struct cfq_io_context *cic)
  979. {
  980. struct cfq_io_context *__cic;
  981. struct list_head *entry, *next;
  982. list_for_each_safe(entry, next, &cic->list) {
  983. __cic = list_entry(entry, struct cfq_io_context, list);
  984. kmem_cache_free(cfq_ioc_pool, __cic);
  985. }
  986. kmem_cache_free(cfq_ioc_pool, cic);
  987. }
  988. static void cfq_trim(struct io_context *ioc)
  989. {
  990. ioc->set_ioprio = NULL;
  991. if (ioc->cic)
  992. cfq_free_io_context(ioc->cic);
  993. }
  994. /*
  995. * Called with interrupts disabled
  996. */
  997. static void cfq_exit_single_io_context(struct cfq_io_context *cic)
  998. {
  999. struct cfq_data *cfqd = cic->key;
  1000. request_queue_t *q;
  1001. if (!cfqd)
  1002. return;
  1003. q = cfqd->queue;
  1004. WARN_ON(!irqs_disabled());
  1005. spin_lock(q->queue_lock);
  1006. if (cic->cfqq[ASYNC]) {
  1007. if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
  1008. __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
  1009. cfq_put_queue(cic->cfqq[ASYNC]);
  1010. cic->cfqq[ASYNC] = NULL;
  1011. }
  1012. if (cic->cfqq[SYNC]) {
  1013. if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
  1014. __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
  1015. cfq_put_queue(cic->cfqq[SYNC]);
  1016. cic->cfqq[SYNC] = NULL;
  1017. }
  1018. cic->key = NULL;
  1019. list_del_init(&cic->queue_list);
  1020. spin_unlock(q->queue_lock);
  1021. }
  1022. /*
  1023. * Another task may update the task cic list, if it is doing a queue lookup
  1024. * on its behalf. cfq_cic_lock excludes such concurrent updates
  1025. */
  1026. static void cfq_exit_io_context(struct cfq_io_context *cic)
  1027. {
  1028. struct cfq_io_context *__cic;
  1029. struct list_head *entry;
  1030. unsigned long flags;
  1031. local_irq_save(flags);
  1032. /*
  1033. * put the reference this task is holding to the various queues
  1034. */
  1035. read_lock(&cfq_exit_lock);
  1036. list_for_each(entry, &cic->list) {
  1037. __cic = list_entry(entry, struct cfq_io_context, list);
  1038. cfq_exit_single_io_context(__cic);
  1039. }
  1040. cfq_exit_single_io_context(cic);
  1041. read_unlock(&cfq_exit_lock);
  1042. local_irq_restore(flags);
  1043. }
  1044. static struct cfq_io_context *
  1045. cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
  1046. {
  1047. struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
  1048. if (cic) {
  1049. INIT_LIST_HEAD(&cic->list);
  1050. cic->cfqq[ASYNC] = NULL;
  1051. cic->cfqq[SYNC] = NULL;
  1052. cic->key = NULL;
  1053. cic->last_end_request = jiffies;
  1054. cic->ttime_total = 0;
  1055. cic->ttime_samples = 0;
  1056. cic->ttime_mean = 0;
  1057. cic->dtor = cfq_free_io_context;
  1058. cic->exit = cfq_exit_io_context;
  1059. INIT_LIST_HEAD(&cic->queue_list);
  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_io_context *cic)
  1105. {
  1106. struct cfq_data *cfqd = cic->key;
  1107. struct cfq_queue *cfqq;
  1108. if (cfqd) {
  1109. spin_lock(cfqd->queue->queue_lock);
  1110. cfqq = cic->cfqq[ASYNC];
  1111. if (cfqq) {
  1112. cfq_mark_cfqq_prio_changed(cfqq);
  1113. cfq_init_prio_data(cfqq);
  1114. }
  1115. cfqq = cic->cfqq[SYNC];
  1116. if (cfqq) {
  1117. cfq_mark_cfqq_prio_changed(cfqq);
  1118. cfq_init_prio_data(cfqq);
  1119. }
  1120. spin_unlock(cfqd->queue->queue_lock);
  1121. }
  1122. }
  1123. /*
  1124. * callback from sys_ioprio_set, irqs are disabled
  1125. */
  1126. static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
  1127. {
  1128. struct cfq_io_context *cic;
  1129. write_lock(&cfq_exit_lock);
  1130. cic = ioc->cic;
  1131. changed_ioprio(cic);
  1132. list_for_each_entry(cic, &cic->list, list)
  1133. changed_ioprio(cic);
  1134. write_unlock(&cfq_exit_lock);
  1135. return 0;
  1136. }
  1137. static struct cfq_queue *
  1138. cfq_get_queue(struct cfq_data *cfqd, unsigned int key, unsigned short ioprio,
  1139. gfp_t gfp_mask)
  1140. {
  1141. const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
  1142. struct cfq_queue *cfqq, *new_cfqq = NULL;
  1143. retry:
  1144. cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
  1145. if (!cfqq) {
  1146. if (new_cfqq) {
  1147. cfqq = new_cfqq;
  1148. new_cfqq = NULL;
  1149. } else if (gfp_mask & __GFP_WAIT) {
  1150. spin_unlock_irq(cfqd->queue->queue_lock);
  1151. new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
  1152. spin_lock_irq(cfqd->queue->queue_lock);
  1153. goto retry;
  1154. } else {
  1155. cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
  1156. if (!cfqq)
  1157. goto out;
  1158. }
  1159. memset(cfqq, 0, sizeof(*cfqq));
  1160. INIT_HLIST_NODE(&cfqq->cfq_hash);
  1161. INIT_LIST_HEAD(&cfqq->cfq_list);
  1162. RB_CLEAR_ROOT(&cfqq->sort_list);
  1163. INIT_LIST_HEAD(&cfqq->fifo);
  1164. cfqq->key = key;
  1165. hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
  1166. atomic_set(&cfqq->ref, 0);
  1167. cfqq->cfqd = cfqd;
  1168. atomic_inc(&cfqd->ref);
  1169. cfqq->service_last = 0;
  1170. /*
  1171. * set ->slice_left to allow preemption for a new process
  1172. */
  1173. cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
  1174. cfq_mark_cfqq_idle_window(cfqq);
  1175. cfq_mark_cfqq_prio_changed(cfqq);
  1176. cfq_init_prio_data(cfqq);
  1177. }
  1178. if (new_cfqq)
  1179. kmem_cache_free(cfq_pool, new_cfqq);
  1180. atomic_inc(&cfqq->ref);
  1181. out:
  1182. WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
  1183. return cfqq;
  1184. }
  1185. /*
  1186. * Setup general io context and cfq io context. There can be several cfq
  1187. * io contexts per general io context, if this process is doing io to more
  1188. * than one device managed by cfq. Note that caller is holding a reference to
  1189. * cfqq, so we don't need to worry about it disappearing
  1190. */
  1191. static struct cfq_io_context *
  1192. cfq_get_io_context(struct cfq_data *cfqd, pid_t pid, gfp_t gfp_mask)
  1193. {
  1194. struct io_context *ioc = NULL;
  1195. struct cfq_io_context *cic;
  1196. might_sleep_if(gfp_mask & __GFP_WAIT);
  1197. ioc = get_io_context(gfp_mask);
  1198. if (!ioc)
  1199. return NULL;
  1200. restart:
  1201. if ((cic = ioc->cic) == NULL) {
  1202. cic = cfq_alloc_io_context(cfqd, gfp_mask);
  1203. if (cic == NULL)
  1204. goto err;
  1205. /*
  1206. * manually increment generic io_context usage count, it
  1207. * cannot go away since we are already holding one ref to it
  1208. */
  1209. cic->ioc = ioc;
  1210. cic->key = cfqd;
  1211. read_lock(&cfq_exit_lock);
  1212. ioc->set_ioprio = cfq_ioc_set_ioprio;
  1213. ioc->cic = cic;
  1214. list_add(&cic->queue_list, &cfqd->cic_list);
  1215. read_unlock(&cfq_exit_lock);
  1216. } else {
  1217. struct cfq_io_context *__cic;
  1218. /*
  1219. * the first cic on the list is actually the head itself
  1220. */
  1221. if (cic->key == cfqd)
  1222. goto out;
  1223. if (unlikely(!cic->key)) {
  1224. read_lock(&cfq_exit_lock);
  1225. if (list_empty(&cic->list))
  1226. ioc->cic = NULL;
  1227. else
  1228. ioc->cic = list_entry(cic->list.next,
  1229. struct cfq_io_context,
  1230. list);
  1231. read_unlock(&cfq_exit_lock);
  1232. kmem_cache_free(cfq_ioc_pool, cic);
  1233. goto restart;
  1234. }
  1235. /*
  1236. * cic exists, check if we already are there. linear search
  1237. * should be ok here, the list will usually not be more than
  1238. * 1 or a few entries long
  1239. */
  1240. list_for_each_entry(__cic, &cic->list, list) {
  1241. /*
  1242. * this process is already holding a reference to
  1243. * this queue, so no need to get one more
  1244. */
  1245. if (__cic->key == cfqd) {
  1246. cic = __cic;
  1247. goto out;
  1248. }
  1249. if (unlikely(!__cic->key)) {
  1250. read_lock(&cfq_exit_lock);
  1251. list_del(&__cic->list);
  1252. read_unlock(&cfq_exit_lock);
  1253. kmem_cache_free(cfq_ioc_pool, __cic);
  1254. goto restart;
  1255. }
  1256. }
  1257. /*
  1258. * nope, process doesn't have a cic assoicated with this
  1259. * cfqq yet. get a new one and add to list
  1260. */
  1261. __cic = cfq_alloc_io_context(cfqd, gfp_mask);
  1262. if (__cic == NULL)
  1263. goto err;
  1264. __cic->ioc = ioc;
  1265. __cic->key = cfqd;
  1266. read_lock(&cfq_exit_lock);
  1267. list_add(&__cic->list, &cic->list);
  1268. list_add(&__cic->queue_list, &cfqd->cic_list);
  1269. read_unlock(&cfq_exit_lock);
  1270. cic = __cic;
  1271. }
  1272. out:
  1273. return cic;
  1274. err:
  1275. put_io_context(ioc);
  1276. return NULL;
  1277. }
  1278. static void
  1279. cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
  1280. {
  1281. unsigned long elapsed, ttime;
  1282. /*
  1283. * if this context already has stuff queued, thinktime is from
  1284. * last queue not last end
  1285. */
  1286. #if 0
  1287. if (time_after(cic->last_end_request, cic->last_queue))
  1288. elapsed = jiffies - cic->last_end_request;
  1289. else
  1290. elapsed = jiffies - cic->last_queue;
  1291. #else
  1292. elapsed = jiffies - cic->last_end_request;
  1293. #endif
  1294. ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
  1295. cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
  1296. cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
  1297. cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
  1298. }
  1299. #define sample_valid(samples) ((samples) > 80)
  1300. /*
  1301. * Disable idle window if the process thinks too long or seeks so much that
  1302. * it doesn't matter
  1303. */
  1304. static void
  1305. cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1306. struct cfq_io_context *cic)
  1307. {
  1308. int enable_idle = cfq_cfqq_idle_window(cfqq);
  1309. if (!cic->ioc->task || !cfqd->cfq_slice_idle)
  1310. enable_idle = 0;
  1311. else if (sample_valid(cic->ttime_samples)) {
  1312. if (cic->ttime_mean > cfqd->cfq_slice_idle)
  1313. enable_idle = 0;
  1314. else
  1315. enable_idle = 1;
  1316. }
  1317. if (enable_idle)
  1318. cfq_mark_cfqq_idle_window(cfqq);
  1319. else
  1320. cfq_clear_cfqq_idle_window(cfqq);
  1321. }
  1322. /*
  1323. * Check if new_cfqq should preempt the currently active queue. Return 0 for
  1324. * no or if we aren't sure, a 1 will cause a preempt.
  1325. */
  1326. static int
  1327. cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
  1328. struct cfq_rq *crq)
  1329. {
  1330. struct cfq_queue *cfqq = cfqd->active_queue;
  1331. if (cfq_class_idle(new_cfqq))
  1332. return 0;
  1333. if (!cfqq)
  1334. return 1;
  1335. if (cfq_class_idle(cfqq))
  1336. return 1;
  1337. if (!cfq_cfqq_wait_request(new_cfqq))
  1338. return 0;
  1339. /*
  1340. * if it doesn't have slice left, forget it
  1341. */
  1342. if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
  1343. return 0;
  1344. if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
  1345. return 1;
  1346. return 0;
  1347. }
  1348. /*
  1349. * cfqq preempts the active queue. if we allowed preempt with no slice left,
  1350. * let it have half of its nominal slice.
  1351. */
  1352. static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1353. {
  1354. struct cfq_queue *__cfqq, *next;
  1355. list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
  1356. cfq_resort_rr_list(__cfqq, 1);
  1357. if (!cfqq->slice_left)
  1358. cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
  1359. cfqq->slice_end = cfqq->slice_left + jiffies;
  1360. __cfq_slice_expired(cfqd, cfqq, 1);
  1361. __cfq_set_active_queue(cfqd, cfqq);
  1362. }
  1363. /*
  1364. * should really be a ll_rw_blk.c helper
  1365. */
  1366. static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1367. {
  1368. request_queue_t *q = cfqd->queue;
  1369. if (!blk_queue_plugged(q))
  1370. q->request_fn(q);
  1371. else
  1372. __generic_unplug_device(q);
  1373. }
  1374. /*
  1375. * Called when a new fs request (crq) is added (to cfqq). Check if there's
  1376. * something we should do about it
  1377. */
  1378. static void
  1379. cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1380. struct cfq_rq *crq)
  1381. {
  1382. struct cfq_io_context *cic;
  1383. cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
  1384. /*
  1385. * we never wait for an async request and we don't allow preemption
  1386. * of an async request. so just return early
  1387. */
  1388. if (!cfq_crq_is_sync(crq))
  1389. return;
  1390. cic = crq->io_context;
  1391. cfq_update_io_thinktime(cfqd, cic);
  1392. cfq_update_idle_window(cfqd, cfqq, cic);
  1393. cic->last_queue = jiffies;
  1394. if (cfqq == cfqd->active_queue) {
  1395. /*
  1396. * if we are waiting for a request for this queue, let it rip
  1397. * immediately and flag that we must not expire this queue
  1398. * just now
  1399. */
  1400. if (cfq_cfqq_wait_request(cfqq)) {
  1401. cfq_mark_cfqq_must_dispatch(cfqq);
  1402. del_timer(&cfqd->idle_slice_timer);
  1403. cfq_start_queueing(cfqd, cfqq);
  1404. }
  1405. } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
  1406. /*
  1407. * not the active queue - expire current slice if it is
  1408. * idle and has expired it's mean thinktime or this new queue
  1409. * has some old slice time left and is of higher priority
  1410. */
  1411. cfq_preempt_queue(cfqd, cfqq);
  1412. cfq_mark_cfqq_must_dispatch(cfqq);
  1413. cfq_start_queueing(cfqd, cfqq);
  1414. }
  1415. }
  1416. static void cfq_insert_request(request_queue_t *q, struct request *rq)
  1417. {
  1418. struct cfq_data *cfqd = q->elevator->elevator_data;
  1419. struct cfq_rq *crq = RQ_DATA(rq);
  1420. struct cfq_queue *cfqq = crq->cfq_queue;
  1421. cfq_init_prio_data(cfqq);
  1422. cfq_add_crq_rb(crq);
  1423. list_add_tail(&rq->queuelist, &cfqq->fifo);
  1424. if (rq_mergeable(rq))
  1425. cfq_add_crq_hash(cfqd, crq);
  1426. cfq_crq_enqueued(cfqd, cfqq, crq);
  1427. }
  1428. static void cfq_completed_request(request_queue_t *q, struct request *rq)
  1429. {
  1430. struct cfq_rq *crq = RQ_DATA(rq);
  1431. struct cfq_queue *cfqq = crq->cfq_queue;
  1432. struct cfq_data *cfqd = cfqq->cfqd;
  1433. const int sync = cfq_crq_is_sync(crq);
  1434. unsigned long now;
  1435. now = jiffies;
  1436. WARN_ON(!cfqd->rq_in_driver);
  1437. WARN_ON(!cfqq->on_dispatch[sync]);
  1438. cfqd->rq_in_driver--;
  1439. cfqq->on_dispatch[sync]--;
  1440. if (!cfq_class_idle(cfqq))
  1441. cfqd->last_end_request = now;
  1442. if (!cfq_cfqq_dispatched(cfqq)) {
  1443. if (cfq_cfqq_on_rr(cfqq)) {
  1444. cfqq->service_last = now;
  1445. cfq_resort_rr_list(cfqq, 0);
  1446. }
  1447. cfq_schedule_dispatch(cfqd);
  1448. }
  1449. if (cfq_crq_is_sync(crq))
  1450. crq->io_context->last_end_request = now;
  1451. }
  1452. static struct request *
  1453. cfq_former_request(request_queue_t *q, struct request *rq)
  1454. {
  1455. struct cfq_rq *crq = RQ_DATA(rq);
  1456. struct rb_node *rbprev = rb_prev(&crq->rb_node);
  1457. if (rbprev)
  1458. return rb_entry_crq(rbprev)->request;
  1459. return NULL;
  1460. }
  1461. static struct request *
  1462. cfq_latter_request(request_queue_t *q, struct request *rq)
  1463. {
  1464. struct cfq_rq *crq = RQ_DATA(rq);
  1465. struct rb_node *rbnext = rb_next(&crq->rb_node);
  1466. if (rbnext)
  1467. return rb_entry_crq(rbnext)->request;
  1468. return NULL;
  1469. }
  1470. /*
  1471. * we temporarily boost lower priority queues if they are holding fs exclusive
  1472. * resources. they are boosted to normal prio (CLASS_BE/4)
  1473. */
  1474. static void cfq_prio_boost(struct cfq_queue *cfqq)
  1475. {
  1476. const int ioprio_class = cfqq->ioprio_class;
  1477. const int ioprio = cfqq->ioprio;
  1478. if (has_fs_excl()) {
  1479. /*
  1480. * boost idle prio on transactions that would lock out other
  1481. * users of the filesystem
  1482. */
  1483. if (cfq_class_idle(cfqq))
  1484. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  1485. if (cfqq->ioprio > IOPRIO_NORM)
  1486. cfqq->ioprio = IOPRIO_NORM;
  1487. } else {
  1488. /*
  1489. * check if we need to unboost the queue
  1490. */
  1491. if (cfqq->ioprio_class != cfqq->org_ioprio_class)
  1492. cfqq->ioprio_class = cfqq->org_ioprio_class;
  1493. if (cfqq->ioprio != cfqq->org_ioprio)
  1494. cfqq->ioprio = cfqq->org_ioprio;
  1495. }
  1496. /*
  1497. * refile between round-robin lists if we moved the priority class
  1498. */
  1499. if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
  1500. cfq_cfqq_on_rr(cfqq))
  1501. cfq_resort_rr_list(cfqq, 0);
  1502. }
  1503. static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
  1504. {
  1505. if (rw == READ || process_sync(task))
  1506. return task->pid;
  1507. return CFQ_KEY_ASYNC;
  1508. }
  1509. static inline int
  1510. __cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1511. struct task_struct *task, int rw)
  1512. {
  1513. #if 1
  1514. if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
  1515. !cfq_cfqq_must_alloc_slice(cfqq)) {
  1516. cfq_mark_cfqq_must_alloc_slice(cfqq);
  1517. return ELV_MQUEUE_MUST;
  1518. }
  1519. return ELV_MQUEUE_MAY;
  1520. #else
  1521. if (!cfqq || task->flags & PF_MEMALLOC)
  1522. return ELV_MQUEUE_MAY;
  1523. if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) {
  1524. if (cfq_cfqq_wait_request(cfqq))
  1525. return ELV_MQUEUE_MUST;
  1526. /*
  1527. * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
  1528. * can quickly flood the queue with writes from a single task
  1529. */
  1530. if (rw == READ || !cfq_cfqq_must_alloc_slice(cfqq)) {
  1531. cfq_mark_cfqq_must_alloc_slice(cfqq);
  1532. return ELV_MQUEUE_MUST;
  1533. }
  1534. return ELV_MQUEUE_MAY;
  1535. }
  1536. if (cfq_class_idle(cfqq))
  1537. return ELV_MQUEUE_NO;
  1538. if (cfqq->allocated[rw] >= cfqd->max_queued) {
  1539. struct io_context *ioc = get_io_context(GFP_ATOMIC);
  1540. int ret = ELV_MQUEUE_NO;
  1541. if (ioc && ioc->nr_batch_requests)
  1542. ret = ELV_MQUEUE_MAY;
  1543. put_io_context(ioc);
  1544. return ret;
  1545. }
  1546. return ELV_MQUEUE_MAY;
  1547. #endif
  1548. }
  1549. static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
  1550. {
  1551. struct cfq_data *cfqd = q->elevator->elevator_data;
  1552. struct task_struct *tsk = current;
  1553. struct cfq_queue *cfqq;
  1554. /*
  1555. * don't force setup of a queue from here, as a call to may_queue
  1556. * does not necessarily imply that a request actually will be queued.
  1557. * so just lookup a possibly existing queue, or return 'may queue'
  1558. * if that fails
  1559. */
  1560. cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
  1561. if (cfqq) {
  1562. cfq_init_prio_data(cfqq);
  1563. cfq_prio_boost(cfqq);
  1564. return __cfq_may_queue(cfqd, cfqq, tsk, rw);
  1565. }
  1566. return ELV_MQUEUE_MAY;
  1567. }
  1568. static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
  1569. {
  1570. struct cfq_data *cfqd = q->elevator->elevator_data;
  1571. struct request_list *rl = &q->rq;
  1572. if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
  1573. smp_mb();
  1574. if (waitqueue_active(&rl->wait[READ]))
  1575. wake_up(&rl->wait[READ]);
  1576. }
  1577. if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
  1578. smp_mb();
  1579. if (waitqueue_active(&rl->wait[WRITE]))
  1580. wake_up(&rl->wait[WRITE]);
  1581. }
  1582. }
  1583. /*
  1584. * queue lock held here
  1585. */
  1586. static void cfq_put_request(request_queue_t *q, struct request *rq)
  1587. {
  1588. struct cfq_data *cfqd = q->elevator->elevator_data;
  1589. struct cfq_rq *crq = RQ_DATA(rq);
  1590. if (crq) {
  1591. struct cfq_queue *cfqq = crq->cfq_queue;
  1592. const int rw = rq_data_dir(rq);
  1593. BUG_ON(!cfqq->allocated[rw]);
  1594. cfqq->allocated[rw]--;
  1595. put_io_context(crq->io_context->ioc);
  1596. mempool_free(crq, cfqd->crq_pool);
  1597. rq->elevator_private = NULL;
  1598. cfq_check_waiters(q, cfqq);
  1599. cfq_put_queue(cfqq);
  1600. }
  1601. }
  1602. /*
  1603. * Allocate cfq data structures associated with this request.
  1604. */
  1605. static int
  1606. cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
  1607. gfp_t gfp_mask)
  1608. {
  1609. struct cfq_data *cfqd = q->elevator->elevator_data;
  1610. struct task_struct *tsk = current;
  1611. struct cfq_io_context *cic;
  1612. const int rw = rq_data_dir(rq);
  1613. pid_t key = cfq_queue_pid(tsk, rw);
  1614. struct cfq_queue *cfqq;
  1615. struct cfq_rq *crq;
  1616. unsigned long flags;
  1617. int is_sync = key != CFQ_KEY_ASYNC;
  1618. might_sleep_if(gfp_mask & __GFP_WAIT);
  1619. cic = cfq_get_io_context(cfqd, key, gfp_mask);
  1620. spin_lock_irqsave(q->queue_lock, flags);
  1621. if (!cic)
  1622. goto queue_fail;
  1623. if (!cic->cfqq[is_sync]) {
  1624. cfqq = cfq_get_queue(cfqd, key, tsk->ioprio, gfp_mask);
  1625. if (!cfqq)
  1626. goto queue_fail;
  1627. cic->cfqq[is_sync] = cfqq;
  1628. } else
  1629. cfqq = cic->cfqq[is_sync];
  1630. cfqq->allocated[rw]++;
  1631. cfq_clear_cfqq_must_alloc(cfqq);
  1632. cfqd->rq_starved = 0;
  1633. atomic_inc(&cfqq->ref);
  1634. spin_unlock_irqrestore(q->queue_lock, flags);
  1635. crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
  1636. if (crq) {
  1637. RB_CLEAR(&crq->rb_node);
  1638. crq->rb_key = 0;
  1639. crq->request = rq;
  1640. INIT_HLIST_NODE(&crq->hash);
  1641. crq->cfq_queue = cfqq;
  1642. crq->io_context = cic;
  1643. if (is_sync)
  1644. cfq_mark_crq_is_sync(crq);
  1645. else
  1646. cfq_clear_crq_is_sync(crq);
  1647. rq->elevator_private = crq;
  1648. return 0;
  1649. }
  1650. spin_lock_irqsave(q->queue_lock, flags);
  1651. cfqq->allocated[rw]--;
  1652. if (!(cfqq->allocated[0] + cfqq->allocated[1]))
  1653. cfq_mark_cfqq_must_alloc(cfqq);
  1654. cfq_put_queue(cfqq);
  1655. queue_fail:
  1656. if (cic)
  1657. put_io_context(cic->ioc);
  1658. /*
  1659. * mark us rq allocation starved. we need to kickstart the process
  1660. * ourselves if there are no pending requests that can do it for us.
  1661. * that would be an extremely rare OOM situation
  1662. */
  1663. cfqd->rq_starved = 1;
  1664. cfq_schedule_dispatch(cfqd);
  1665. spin_unlock_irqrestore(q->queue_lock, flags);
  1666. return 1;
  1667. }
  1668. static void cfq_kick_queue(void *data)
  1669. {
  1670. request_queue_t *q = data;
  1671. struct cfq_data *cfqd = q->elevator->elevator_data;
  1672. unsigned long flags;
  1673. spin_lock_irqsave(q->queue_lock, flags);
  1674. if (cfqd->rq_starved) {
  1675. struct request_list *rl = &q->rq;
  1676. /*
  1677. * we aren't guaranteed to get a request after this, but we
  1678. * have to be opportunistic
  1679. */
  1680. smp_mb();
  1681. if (waitqueue_active(&rl->wait[READ]))
  1682. wake_up(&rl->wait[READ]);
  1683. if (waitqueue_active(&rl->wait[WRITE]))
  1684. wake_up(&rl->wait[WRITE]);
  1685. }
  1686. blk_remove_plug(q);
  1687. q->request_fn(q);
  1688. spin_unlock_irqrestore(q->queue_lock, flags);
  1689. }
  1690. /*
  1691. * Timer running if the active_queue is currently idling inside its time slice
  1692. */
  1693. static void cfq_idle_slice_timer(unsigned long data)
  1694. {
  1695. struct cfq_data *cfqd = (struct cfq_data *) data;
  1696. struct cfq_queue *cfqq;
  1697. unsigned long flags;
  1698. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  1699. if ((cfqq = cfqd->active_queue) != NULL) {
  1700. unsigned long now = jiffies;
  1701. /*
  1702. * expired
  1703. */
  1704. if (time_after(now, cfqq->slice_end))
  1705. goto expire;
  1706. /*
  1707. * only expire and reinvoke request handler, if there are
  1708. * other queues with pending requests
  1709. */
  1710. if (!cfqd->busy_queues) {
  1711. cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
  1712. add_timer(&cfqd->idle_slice_timer);
  1713. goto out_cont;
  1714. }
  1715. /*
  1716. * not expired and it has a request pending, let it dispatch
  1717. */
  1718. if (!RB_EMPTY(&cfqq->sort_list)) {
  1719. cfq_mark_cfqq_must_dispatch(cfqq);
  1720. goto out_kick;
  1721. }
  1722. }
  1723. expire:
  1724. cfq_slice_expired(cfqd, 0);
  1725. out_kick:
  1726. cfq_schedule_dispatch(cfqd);
  1727. out_cont:
  1728. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  1729. }
  1730. /*
  1731. * Timer running if an idle class queue is waiting for service
  1732. */
  1733. static void cfq_idle_class_timer(unsigned long data)
  1734. {
  1735. struct cfq_data *cfqd = (struct cfq_data *) data;
  1736. unsigned long flags, end;
  1737. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  1738. /*
  1739. * race with a non-idle queue, reset timer
  1740. */
  1741. end = cfqd->last_end_request + CFQ_IDLE_GRACE;
  1742. if (!time_after_eq(jiffies, end)) {
  1743. cfqd->idle_class_timer.expires = end;
  1744. add_timer(&cfqd->idle_class_timer);
  1745. } else
  1746. cfq_schedule_dispatch(cfqd);
  1747. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  1748. }
  1749. static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
  1750. {
  1751. del_timer_sync(&cfqd->idle_slice_timer);
  1752. del_timer_sync(&cfqd->idle_class_timer);
  1753. blk_sync_queue(cfqd->queue);
  1754. }
  1755. static void cfq_put_cfqd(struct cfq_data *cfqd)
  1756. {
  1757. if (!atomic_dec_and_test(&cfqd->ref))
  1758. return;
  1759. cfq_shutdown_timer_wq(cfqd);
  1760. mempool_destroy(cfqd->crq_pool);
  1761. kfree(cfqd->crq_hash);
  1762. kfree(cfqd->cfq_hash);
  1763. kfree(cfqd);
  1764. }
  1765. static void cfq_exit_queue(elevator_t *e)
  1766. {
  1767. struct cfq_data *cfqd = e->elevator_data;
  1768. request_queue_t *q = cfqd->queue;
  1769. cfq_shutdown_timer_wq(cfqd);
  1770. write_lock(&cfq_exit_lock);
  1771. spin_lock_irq(q->queue_lock);
  1772. if (cfqd->active_queue)
  1773. __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
  1774. while(!list_empty(&cfqd->cic_list)) {
  1775. struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
  1776. struct cfq_io_context,
  1777. queue_list);
  1778. if (cic->cfqq[ASYNC]) {
  1779. cfq_put_queue(cic->cfqq[ASYNC]);
  1780. cic->cfqq[ASYNC] = NULL;
  1781. }
  1782. if (cic->cfqq[SYNC]) {
  1783. cfq_put_queue(cic->cfqq[SYNC]);
  1784. cic->cfqq[SYNC] = NULL;
  1785. }
  1786. cic->key = NULL;
  1787. list_del_init(&cic->queue_list);
  1788. }
  1789. spin_unlock_irq(q->queue_lock);
  1790. write_unlock(&cfq_exit_lock);
  1791. cfq_put_cfqd(cfqd);
  1792. }
  1793. static int cfq_init_queue(request_queue_t *q, elevator_t *e)
  1794. {
  1795. struct cfq_data *cfqd;
  1796. int i;
  1797. cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
  1798. if (!cfqd)
  1799. return -ENOMEM;
  1800. memset(cfqd, 0, sizeof(*cfqd));
  1801. for (i = 0; i < CFQ_PRIO_LISTS; i++)
  1802. INIT_LIST_HEAD(&cfqd->rr_list[i]);
  1803. INIT_LIST_HEAD(&cfqd->busy_rr);
  1804. INIT_LIST_HEAD(&cfqd->cur_rr);
  1805. INIT_LIST_HEAD(&cfqd->idle_rr);
  1806. INIT_LIST_HEAD(&cfqd->empty_list);
  1807. INIT_LIST_HEAD(&cfqd->cic_list);
  1808. cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
  1809. if (!cfqd->crq_hash)
  1810. goto out_crqhash;
  1811. cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
  1812. if (!cfqd->cfq_hash)
  1813. goto out_cfqhash;
  1814. cfqd->crq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, crq_pool);
  1815. if (!cfqd->crq_pool)
  1816. goto out_crqpool;
  1817. for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
  1818. INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
  1819. for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
  1820. INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
  1821. e->elevator_data = cfqd;
  1822. cfqd->queue = q;
  1823. cfqd->max_queued = q->nr_requests / 4;
  1824. q->nr_batching = cfq_queued;
  1825. init_timer(&cfqd->idle_slice_timer);
  1826. cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
  1827. cfqd->idle_slice_timer.data = (unsigned long) cfqd;
  1828. init_timer(&cfqd->idle_class_timer);
  1829. cfqd->idle_class_timer.function = cfq_idle_class_timer;
  1830. cfqd->idle_class_timer.data = (unsigned long) cfqd;
  1831. INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
  1832. atomic_set(&cfqd->ref, 1);
  1833. cfqd->cfq_queued = cfq_queued;
  1834. cfqd->cfq_quantum = cfq_quantum;
  1835. cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
  1836. cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
  1837. cfqd->cfq_back_max = cfq_back_max;
  1838. cfqd->cfq_back_penalty = cfq_back_penalty;
  1839. cfqd->cfq_slice[0] = cfq_slice_async;
  1840. cfqd->cfq_slice[1] = cfq_slice_sync;
  1841. cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
  1842. cfqd->cfq_slice_idle = cfq_slice_idle;
  1843. cfqd->cfq_max_depth = cfq_max_depth;
  1844. return 0;
  1845. out_crqpool:
  1846. kfree(cfqd->cfq_hash);
  1847. out_cfqhash:
  1848. kfree(cfqd->crq_hash);
  1849. out_crqhash:
  1850. kfree(cfqd);
  1851. return -ENOMEM;
  1852. }
  1853. static void cfq_slab_kill(void)
  1854. {
  1855. if (crq_pool)
  1856. kmem_cache_destroy(crq_pool);
  1857. if (cfq_pool)
  1858. kmem_cache_destroy(cfq_pool);
  1859. if (cfq_ioc_pool)
  1860. kmem_cache_destroy(cfq_ioc_pool);
  1861. }
  1862. static int __init cfq_slab_setup(void)
  1863. {
  1864. crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
  1865. NULL, NULL);
  1866. if (!crq_pool)
  1867. goto fail;
  1868. cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
  1869. NULL, NULL);
  1870. if (!cfq_pool)
  1871. goto fail;
  1872. cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
  1873. sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
  1874. if (!cfq_ioc_pool)
  1875. goto fail;
  1876. return 0;
  1877. fail:
  1878. cfq_slab_kill();
  1879. return -ENOMEM;
  1880. }
  1881. /*
  1882. * sysfs parts below -->
  1883. */
  1884. struct cfq_fs_entry {
  1885. struct attribute attr;
  1886. ssize_t (*show)(struct cfq_data *, char *);
  1887. ssize_t (*store)(struct cfq_data *, const char *, size_t);
  1888. };
  1889. static ssize_t
  1890. cfq_var_show(unsigned int var, char *page)
  1891. {
  1892. return sprintf(page, "%d\n", var);
  1893. }
  1894. static ssize_t
  1895. cfq_var_store(unsigned int *var, const char *page, size_t count)
  1896. {
  1897. char *p = (char *) page;
  1898. *var = simple_strtoul(p, &p, 10);
  1899. return count;
  1900. }
  1901. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  1902. static ssize_t __FUNC(struct cfq_data *cfqd, char *page) \
  1903. { \
  1904. unsigned int __data = __VAR; \
  1905. if (__CONV) \
  1906. __data = jiffies_to_msecs(__data); \
  1907. return cfq_var_show(__data, (page)); \
  1908. }
  1909. SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
  1910. SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
  1911. SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
  1912. SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
  1913. SHOW_FUNCTION(cfq_back_max_show, cfqd->cfq_back_max, 0);
  1914. SHOW_FUNCTION(cfq_back_penalty_show, cfqd->cfq_back_penalty, 0);
  1915. SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
  1916. SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
  1917. SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
  1918. SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
  1919. SHOW_FUNCTION(cfq_max_depth_show, cfqd->cfq_max_depth, 0);
  1920. #undef SHOW_FUNCTION
  1921. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  1922. static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count) \
  1923. { \
  1924. unsigned int __data; \
  1925. int ret = cfq_var_store(&__data, (page), count); \
  1926. if (__data < (MIN)) \
  1927. __data = (MIN); \
  1928. else if (__data > (MAX)) \
  1929. __data = (MAX); \
  1930. if (__CONV) \
  1931. *(__PTR) = msecs_to_jiffies(__data); \
  1932. else \
  1933. *(__PTR) = __data; \
  1934. return ret; \
  1935. }
  1936. STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
  1937. STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
  1938. STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
  1939. STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
  1940. STORE_FUNCTION(cfq_back_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
  1941. STORE_FUNCTION(cfq_back_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
  1942. STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
  1943. STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
  1944. STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
  1945. STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
  1946. STORE_FUNCTION(cfq_max_depth_store, &cfqd->cfq_max_depth, 1, UINT_MAX, 0);
  1947. #undef STORE_FUNCTION
  1948. static struct cfq_fs_entry cfq_quantum_entry = {
  1949. .attr = {.name = "quantum", .mode = S_IRUGO | S_IWUSR },
  1950. .show = cfq_quantum_show,
  1951. .store = cfq_quantum_store,
  1952. };
  1953. static struct cfq_fs_entry cfq_queued_entry = {
  1954. .attr = {.name = "queued", .mode = S_IRUGO | S_IWUSR },
  1955. .show = cfq_queued_show,
  1956. .store = cfq_queued_store,
  1957. };
  1958. static struct cfq_fs_entry cfq_fifo_expire_sync_entry = {
  1959. .attr = {.name = "fifo_expire_sync", .mode = S_IRUGO | S_IWUSR },
  1960. .show = cfq_fifo_expire_sync_show,
  1961. .store = cfq_fifo_expire_sync_store,
  1962. };
  1963. static struct cfq_fs_entry cfq_fifo_expire_async_entry = {
  1964. .attr = {.name = "fifo_expire_async", .mode = S_IRUGO | S_IWUSR },
  1965. .show = cfq_fifo_expire_async_show,
  1966. .store = cfq_fifo_expire_async_store,
  1967. };
  1968. static struct cfq_fs_entry cfq_back_max_entry = {
  1969. .attr = {.name = "back_seek_max", .mode = S_IRUGO | S_IWUSR },
  1970. .show = cfq_back_max_show,
  1971. .store = cfq_back_max_store,
  1972. };
  1973. static struct cfq_fs_entry cfq_back_penalty_entry = {
  1974. .attr = {.name = "back_seek_penalty", .mode = S_IRUGO | S_IWUSR },
  1975. .show = cfq_back_penalty_show,
  1976. .store = cfq_back_penalty_store,
  1977. };
  1978. static struct cfq_fs_entry cfq_slice_sync_entry = {
  1979. .attr = {.name = "slice_sync", .mode = S_IRUGO | S_IWUSR },
  1980. .show = cfq_slice_sync_show,
  1981. .store = cfq_slice_sync_store,
  1982. };
  1983. static struct cfq_fs_entry cfq_slice_async_entry = {
  1984. .attr = {.name = "slice_async", .mode = S_IRUGO | S_IWUSR },
  1985. .show = cfq_slice_async_show,
  1986. .store = cfq_slice_async_store,
  1987. };
  1988. static struct cfq_fs_entry cfq_slice_async_rq_entry = {
  1989. .attr = {.name = "slice_async_rq", .mode = S_IRUGO | S_IWUSR },
  1990. .show = cfq_slice_async_rq_show,
  1991. .store = cfq_slice_async_rq_store,
  1992. };
  1993. static struct cfq_fs_entry cfq_slice_idle_entry = {
  1994. .attr = {.name = "slice_idle", .mode = S_IRUGO | S_IWUSR },
  1995. .show = cfq_slice_idle_show,
  1996. .store = cfq_slice_idle_store,
  1997. };
  1998. static struct cfq_fs_entry cfq_max_depth_entry = {
  1999. .attr = {.name = "max_depth", .mode = S_IRUGO | S_IWUSR },
  2000. .show = cfq_max_depth_show,
  2001. .store = cfq_max_depth_store,
  2002. };
  2003. static struct attribute *default_attrs[] = {
  2004. &cfq_quantum_entry.attr,
  2005. &cfq_queued_entry.attr,
  2006. &cfq_fifo_expire_sync_entry.attr,
  2007. &cfq_fifo_expire_async_entry.attr,
  2008. &cfq_back_max_entry.attr,
  2009. &cfq_back_penalty_entry.attr,
  2010. &cfq_slice_sync_entry.attr,
  2011. &cfq_slice_async_entry.attr,
  2012. &cfq_slice_async_rq_entry.attr,
  2013. &cfq_slice_idle_entry.attr,
  2014. &cfq_max_depth_entry.attr,
  2015. NULL,
  2016. };
  2017. #define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
  2018. static ssize_t
  2019. cfq_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  2020. {
  2021. elevator_t *e = container_of(kobj, elevator_t, kobj);
  2022. struct cfq_fs_entry *entry = to_cfq(attr);
  2023. if (!entry->show)
  2024. return -EIO;
  2025. return entry->show(e->elevator_data, page);
  2026. }
  2027. static ssize_t
  2028. cfq_attr_store(struct kobject *kobj, struct attribute *attr,
  2029. const char *page, size_t length)
  2030. {
  2031. elevator_t *e = container_of(kobj, elevator_t, kobj);
  2032. struct cfq_fs_entry *entry = to_cfq(attr);
  2033. if (!entry->store)
  2034. return -EIO;
  2035. return entry->store(e->elevator_data, page, length);
  2036. }
  2037. static struct sysfs_ops cfq_sysfs_ops = {
  2038. .show = cfq_attr_show,
  2039. .store = cfq_attr_store,
  2040. };
  2041. static struct kobj_type cfq_ktype = {
  2042. .sysfs_ops = &cfq_sysfs_ops,
  2043. .default_attrs = default_attrs,
  2044. };
  2045. static struct elevator_type iosched_cfq = {
  2046. .ops = {
  2047. .elevator_merge_fn = cfq_merge,
  2048. .elevator_merged_fn = cfq_merged_request,
  2049. .elevator_merge_req_fn = cfq_merged_requests,
  2050. .elevator_dispatch_fn = cfq_dispatch_requests,
  2051. .elevator_add_req_fn = cfq_insert_request,
  2052. .elevator_activate_req_fn = cfq_activate_request,
  2053. .elevator_deactivate_req_fn = cfq_deactivate_request,
  2054. .elevator_queue_empty_fn = cfq_queue_empty,
  2055. .elevator_completed_req_fn = cfq_completed_request,
  2056. .elevator_former_req_fn = cfq_former_request,
  2057. .elevator_latter_req_fn = cfq_latter_request,
  2058. .elevator_set_req_fn = cfq_set_request,
  2059. .elevator_put_req_fn = cfq_put_request,
  2060. .elevator_may_queue_fn = cfq_may_queue,
  2061. .elevator_init_fn = cfq_init_queue,
  2062. .elevator_exit_fn = cfq_exit_queue,
  2063. .trim = cfq_trim,
  2064. },
  2065. .elevator_ktype = &cfq_ktype,
  2066. .elevator_name = "cfq",
  2067. .elevator_owner = THIS_MODULE,
  2068. };
  2069. static int __init cfq_init(void)
  2070. {
  2071. int ret;
  2072. /*
  2073. * could be 0 on HZ < 1000 setups
  2074. */
  2075. if (!cfq_slice_async)
  2076. cfq_slice_async = 1;
  2077. if (!cfq_slice_idle)
  2078. cfq_slice_idle = 1;
  2079. if (cfq_slab_setup())
  2080. return -ENOMEM;
  2081. ret = elv_register(&iosched_cfq);
  2082. if (ret)
  2083. cfq_slab_kill();
  2084. return ret;
  2085. }
  2086. static void __exit cfq_exit(void)
  2087. {
  2088. elv_unregister(&iosched_cfq);
  2089. cfq_slab_kill();
  2090. }
  2091. module_init(cfq_init);
  2092. module_exit(cfq_exit);
  2093. MODULE_AUTHOR("Jens Axboe");
  2094. MODULE_LICENSE("GPL");
  2095. MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");