cfq-iosched.c 66 KB

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