cfq-iosched.c 60 KB

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