cfq-iosched.c 59 KB

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