cfq-iosched.c 71 KB

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