cfq-iosched.c 57 KB

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