cfq-iosched.c 58 KB

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