cfq-iosched.c 108 KB

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