cfq-iosched.c 59 KB

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