cfq-iosched.c 98 KB

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