cfq-iosched.c 64 KB

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