cfq-iosched.c 76 KB

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