cfq-iosched.c 77 KB

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