cfq-iosched.c 50 KB

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