cfq-iosched.c 54 KB

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