cfq-iosched.c 55 KB

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