cfq-iosched.c 72 KB

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