cfq-iosched.c 86 KB

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