cfq-iosched.c 129 KB

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