cfq-iosched.c 51 KB

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