cfq-iosched.c 54 KB

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