cfq-iosched.c 52 KB

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