cfq-iosched.c 53 KB

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