fair.c 147 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827
  1. /*
  2. * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  5. *
  6. * Interactivity improvements by Mike Galbraith
  7. * (C) 2007 Mike Galbraith <efault@gmx.de>
  8. *
  9. * Various enhancements by Dmitry Adamushko.
  10. * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
  11. *
  12. * Group scheduling enhancements by Srivatsa Vaddagiri
  13. * Copyright IBM Corporation, 2007
  14. * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
  15. *
  16. * Scaled math optimizations by Thomas Gleixner
  17. * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
  18. *
  19. * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
  20. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  21. */
  22. #include <linux/latencytop.h>
  23. #include <linux/sched.h>
  24. #include <linux/cpumask.h>
  25. #include <linux/slab.h>
  26. #include <linux/profile.h>
  27. #include <linux/interrupt.h>
  28. #include <trace/events/sched.h>
  29. #include "sched.h"
  30. /*
  31. * Targeted preemption latency for CPU-bound tasks:
  32. * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
  33. *
  34. * NOTE: this latency value is not the same as the concept of
  35. * 'timeslice length' - timeslices in CFS are of variable length
  36. * and have no persistent notion like in traditional, time-slice
  37. * based scheduling concepts.
  38. *
  39. * (to see the precise effective timeslice length of your workload,
  40. * run vmstat and monitor the context-switches (cs) field)
  41. */
  42. unsigned int sysctl_sched_latency = 6000000ULL;
  43. unsigned int normalized_sysctl_sched_latency = 6000000ULL;
  44. /*
  45. * The initial- and re-scaling of tunables is configurable
  46. * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
  47. *
  48. * Options are:
  49. * SCHED_TUNABLESCALING_NONE - unscaled, always *1
  50. * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
  51. * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
  52. */
  53. enum sched_tunable_scaling sysctl_sched_tunable_scaling
  54. = SCHED_TUNABLESCALING_LOG;
  55. /*
  56. * Minimal preemption granularity for CPU-bound tasks:
  57. * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
  58. */
  59. unsigned int sysctl_sched_min_granularity = 750000ULL;
  60. unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
  61. /*
  62. * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
  63. */
  64. static unsigned int sched_nr_latency = 8;
  65. /*
  66. * After fork, child runs first. If set to 0 (default) then
  67. * parent will (try to) run first.
  68. */
  69. unsigned int sysctl_sched_child_runs_first __read_mostly;
  70. /*
  71. * SCHED_OTHER wake-up granularity.
  72. * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
  73. *
  74. * This option delays the preemption effects of decoupled workloads
  75. * and reduces their over-scheduling. Synchronous workloads will still
  76. * have immediate wakeup/sleep latencies.
  77. */
  78. unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
  79. unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
  80. const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
  81. /*
  82. * The exponential sliding window over which load is averaged for shares
  83. * distribution.
  84. * (default: 10msec)
  85. */
  86. unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
  87. #ifdef CONFIG_CFS_BANDWIDTH
  88. /*
  89. * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
  90. * each time a cfs_rq requests quota.
  91. *
  92. * Note: in the case that the slice exceeds the runtime remaining (either due
  93. * to consumption or the quota being specified to be smaller than the slice)
  94. * we will always only issue the remaining available time.
  95. *
  96. * default: 5 msec, units: microseconds
  97. */
  98. unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
  99. #endif
  100. /*
  101. * Increase the granularity value when there are more CPUs,
  102. * because with more CPUs the 'effective latency' as visible
  103. * to users decreases. But the relationship is not linear,
  104. * so pick a second-best guess by going with the log2 of the
  105. * number of CPUs.
  106. *
  107. * This idea comes from the SD scheduler of Con Kolivas:
  108. */
  109. static int get_update_sysctl_factor(void)
  110. {
  111. unsigned int cpus = min_t(int, num_online_cpus(), 8);
  112. unsigned int factor;
  113. switch (sysctl_sched_tunable_scaling) {
  114. case SCHED_TUNABLESCALING_NONE:
  115. factor = 1;
  116. break;
  117. case SCHED_TUNABLESCALING_LINEAR:
  118. factor = cpus;
  119. break;
  120. case SCHED_TUNABLESCALING_LOG:
  121. default:
  122. factor = 1 + ilog2(cpus);
  123. break;
  124. }
  125. return factor;
  126. }
  127. static void update_sysctl(void)
  128. {
  129. unsigned int factor = get_update_sysctl_factor();
  130. #define SET_SYSCTL(name) \
  131. (sysctl_##name = (factor) * normalized_sysctl_##name)
  132. SET_SYSCTL(sched_min_granularity);
  133. SET_SYSCTL(sched_latency);
  134. SET_SYSCTL(sched_wakeup_granularity);
  135. #undef SET_SYSCTL
  136. }
  137. void sched_init_granularity(void)
  138. {
  139. update_sysctl();
  140. }
  141. #if BITS_PER_LONG == 32
  142. # define WMULT_CONST (~0UL)
  143. #else
  144. # define WMULT_CONST (1UL << 32)
  145. #endif
  146. #define WMULT_SHIFT 32
  147. /*
  148. * Shift right and round:
  149. */
  150. #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
  151. /*
  152. * delta *= weight / lw
  153. */
  154. static unsigned long
  155. calc_delta_mine(unsigned long delta_exec, unsigned long weight,
  156. struct load_weight *lw)
  157. {
  158. u64 tmp;
  159. /*
  160. * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
  161. * entities since MIN_SHARES = 2. Treat weight as 1 if less than
  162. * 2^SCHED_LOAD_RESOLUTION.
  163. */
  164. if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
  165. tmp = (u64)delta_exec * scale_load_down(weight);
  166. else
  167. tmp = (u64)delta_exec;
  168. if (!lw->inv_weight) {
  169. unsigned long w = scale_load_down(lw->weight);
  170. if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
  171. lw->inv_weight = 1;
  172. else if (unlikely(!w))
  173. lw->inv_weight = WMULT_CONST;
  174. else
  175. lw->inv_weight = WMULT_CONST / w;
  176. }
  177. /*
  178. * Check whether we'd overflow the 64-bit multiplication:
  179. */
  180. if (unlikely(tmp > WMULT_CONST))
  181. tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
  182. WMULT_SHIFT/2);
  183. else
  184. tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
  185. return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
  186. }
  187. const struct sched_class fair_sched_class;
  188. /**************************************************************
  189. * CFS operations on generic schedulable entities:
  190. */
  191. #ifdef CONFIG_FAIR_GROUP_SCHED
  192. /* cpu runqueue to which this cfs_rq is attached */
  193. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  194. {
  195. return cfs_rq->rq;
  196. }
  197. /* An entity is a task if it doesn't "own" a runqueue */
  198. #define entity_is_task(se) (!se->my_q)
  199. static inline struct task_struct *task_of(struct sched_entity *se)
  200. {
  201. #ifdef CONFIG_SCHED_DEBUG
  202. WARN_ON_ONCE(!entity_is_task(se));
  203. #endif
  204. return container_of(se, struct task_struct, se);
  205. }
  206. /* Walk up scheduling entities hierarchy */
  207. #define for_each_sched_entity(se) \
  208. for (; se; se = se->parent)
  209. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  210. {
  211. return p->se.cfs_rq;
  212. }
  213. /* runqueue on which this entity is (to be) queued */
  214. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  215. {
  216. return se->cfs_rq;
  217. }
  218. /* runqueue "owned" by this group */
  219. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  220. {
  221. return grp->my_q;
  222. }
  223. static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
  224. int force_update);
  225. static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
  226. {
  227. if (!cfs_rq->on_list) {
  228. /*
  229. * Ensure we either appear before our parent (if already
  230. * enqueued) or force our parent to appear after us when it is
  231. * enqueued. The fact that we always enqueue bottom-up
  232. * reduces this to two cases.
  233. */
  234. if (cfs_rq->tg->parent &&
  235. cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
  236. list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
  237. &rq_of(cfs_rq)->leaf_cfs_rq_list);
  238. } else {
  239. list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
  240. &rq_of(cfs_rq)->leaf_cfs_rq_list);
  241. }
  242. cfs_rq->on_list = 1;
  243. /* We should have no load, but we need to update last_decay. */
  244. update_cfs_rq_blocked_load(cfs_rq, 0);
  245. }
  246. }
  247. static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
  248. {
  249. if (cfs_rq->on_list) {
  250. list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
  251. cfs_rq->on_list = 0;
  252. }
  253. }
  254. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  255. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  256. list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  257. /* Do the two (enqueued) entities belong to the same group ? */
  258. static inline int
  259. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  260. {
  261. if (se->cfs_rq == pse->cfs_rq)
  262. return 1;
  263. return 0;
  264. }
  265. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  266. {
  267. return se->parent;
  268. }
  269. /* return depth at which a sched entity is present in the hierarchy */
  270. static inline int depth_se(struct sched_entity *se)
  271. {
  272. int depth = 0;
  273. for_each_sched_entity(se)
  274. depth++;
  275. return depth;
  276. }
  277. static void
  278. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  279. {
  280. int se_depth, pse_depth;
  281. /*
  282. * preemption test can be made between sibling entities who are in the
  283. * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
  284. * both tasks until we find their ancestors who are siblings of common
  285. * parent.
  286. */
  287. /* First walk up until both entities are at same depth */
  288. se_depth = depth_se(*se);
  289. pse_depth = depth_se(*pse);
  290. while (se_depth > pse_depth) {
  291. se_depth--;
  292. *se = parent_entity(*se);
  293. }
  294. while (pse_depth > se_depth) {
  295. pse_depth--;
  296. *pse = parent_entity(*pse);
  297. }
  298. while (!is_same_group(*se, *pse)) {
  299. *se = parent_entity(*se);
  300. *pse = parent_entity(*pse);
  301. }
  302. }
  303. #else /* !CONFIG_FAIR_GROUP_SCHED */
  304. static inline struct task_struct *task_of(struct sched_entity *se)
  305. {
  306. return container_of(se, struct task_struct, se);
  307. }
  308. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  309. {
  310. return container_of(cfs_rq, struct rq, cfs);
  311. }
  312. #define entity_is_task(se) 1
  313. #define for_each_sched_entity(se) \
  314. for (; se; se = NULL)
  315. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  316. {
  317. return &task_rq(p)->cfs;
  318. }
  319. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  320. {
  321. struct task_struct *p = task_of(se);
  322. struct rq *rq = task_rq(p);
  323. return &rq->cfs;
  324. }
  325. /* runqueue "owned" by this group */
  326. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  327. {
  328. return NULL;
  329. }
  330. static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
  331. {
  332. }
  333. static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
  334. {
  335. }
  336. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  337. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  338. static inline int
  339. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  340. {
  341. return 1;
  342. }
  343. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  344. {
  345. return NULL;
  346. }
  347. static inline void
  348. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  349. {
  350. }
  351. #endif /* CONFIG_FAIR_GROUP_SCHED */
  352. static __always_inline
  353. void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec);
  354. /**************************************************************
  355. * Scheduling class tree data structure manipulation methods:
  356. */
  357. static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
  358. {
  359. s64 delta = (s64)(vruntime - min_vruntime);
  360. if (delta > 0)
  361. min_vruntime = vruntime;
  362. return min_vruntime;
  363. }
  364. static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
  365. {
  366. s64 delta = (s64)(vruntime - min_vruntime);
  367. if (delta < 0)
  368. min_vruntime = vruntime;
  369. return min_vruntime;
  370. }
  371. static inline int entity_before(struct sched_entity *a,
  372. struct sched_entity *b)
  373. {
  374. return (s64)(a->vruntime - b->vruntime) < 0;
  375. }
  376. static void update_min_vruntime(struct cfs_rq *cfs_rq)
  377. {
  378. u64 vruntime = cfs_rq->min_vruntime;
  379. if (cfs_rq->curr)
  380. vruntime = cfs_rq->curr->vruntime;
  381. if (cfs_rq->rb_leftmost) {
  382. struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
  383. struct sched_entity,
  384. run_node);
  385. if (!cfs_rq->curr)
  386. vruntime = se->vruntime;
  387. else
  388. vruntime = min_vruntime(vruntime, se->vruntime);
  389. }
  390. cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
  391. #ifndef CONFIG_64BIT
  392. smp_wmb();
  393. cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
  394. #endif
  395. }
  396. /*
  397. * Enqueue an entity into the rb-tree:
  398. */
  399. static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  400. {
  401. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  402. struct rb_node *parent = NULL;
  403. struct sched_entity *entry;
  404. int leftmost = 1;
  405. /*
  406. * Find the right place in the rbtree:
  407. */
  408. while (*link) {
  409. parent = *link;
  410. entry = rb_entry(parent, struct sched_entity, run_node);
  411. /*
  412. * We dont care about collisions. Nodes with
  413. * the same key stay together.
  414. */
  415. if (entity_before(se, entry)) {
  416. link = &parent->rb_left;
  417. } else {
  418. link = &parent->rb_right;
  419. leftmost = 0;
  420. }
  421. }
  422. /*
  423. * Maintain a cache of leftmost tree entries (it is frequently
  424. * used):
  425. */
  426. if (leftmost)
  427. cfs_rq->rb_leftmost = &se->run_node;
  428. rb_link_node(&se->run_node, parent, link);
  429. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  430. }
  431. static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  432. {
  433. if (cfs_rq->rb_leftmost == &se->run_node) {
  434. struct rb_node *next_node;
  435. next_node = rb_next(&se->run_node);
  436. cfs_rq->rb_leftmost = next_node;
  437. }
  438. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  439. }
  440. struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
  441. {
  442. struct rb_node *left = cfs_rq->rb_leftmost;
  443. if (!left)
  444. return NULL;
  445. return rb_entry(left, struct sched_entity, run_node);
  446. }
  447. static struct sched_entity *__pick_next_entity(struct sched_entity *se)
  448. {
  449. struct rb_node *next = rb_next(&se->run_node);
  450. if (!next)
  451. return NULL;
  452. return rb_entry(next, struct sched_entity, run_node);
  453. }
  454. #ifdef CONFIG_SCHED_DEBUG
  455. struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  456. {
  457. struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
  458. if (!last)
  459. return NULL;
  460. return rb_entry(last, struct sched_entity, run_node);
  461. }
  462. /**************************************************************
  463. * Scheduling class statistics methods:
  464. */
  465. int sched_proc_update_handler(struct ctl_table *table, int write,
  466. void __user *buffer, size_t *lenp,
  467. loff_t *ppos)
  468. {
  469. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  470. int factor = get_update_sysctl_factor();
  471. if (ret || !write)
  472. return ret;
  473. sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
  474. sysctl_sched_min_granularity);
  475. #define WRT_SYSCTL(name) \
  476. (normalized_sysctl_##name = sysctl_##name / (factor))
  477. WRT_SYSCTL(sched_min_granularity);
  478. WRT_SYSCTL(sched_latency);
  479. WRT_SYSCTL(sched_wakeup_granularity);
  480. #undef WRT_SYSCTL
  481. return 0;
  482. }
  483. #endif
  484. /*
  485. * delta /= w
  486. */
  487. static inline unsigned long
  488. calc_delta_fair(unsigned long delta, struct sched_entity *se)
  489. {
  490. if (unlikely(se->load.weight != NICE_0_LOAD))
  491. delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
  492. return delta;
  493. }
  494. /*
  495. * The idea is to set a period in which each task runs once.
  496. *
  497. * When there are too many tasks (sched_nr_latency) we have to stretch
  498. * this period because otherwise the slices get too small.
  499. *
  500. * p = (nr <= nl) ? l : l*nr/nl
  501. */
  502. static u64 __sched_period(unsigned long nr_running)
  503. {
  504. u64 period = sysctl_sched_latency;
  505. unsigned long nr_latency = sched_nr_latency;
  506. if (unlikely(nr_running > nr_latency)) {
  507. period = sysctl_sched_min_granularity;
  508. period *= nr_running;
  509. }
  510. return period;
  511. }
  512. /*
  513. * We calculate the wall-time slice from the period by taking a part
  514. * proportional to the weight.
  515. *
  516. * s = p*P[w/rw]
  517. */
  518. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  519. {
  520. u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
  521. for_each_sched_entity(se) {
  522. struct load_weight *load;
  523. struct load_weight lw;
  524. cfs_rq = cfs_rq_of(se);
  525. load = &cfs_rq->load;
  526. if (unlikely(!se->on_rq)) {
  527. lw = cfs_rq->load;
  528. update_load_add(&lw, se->load.weight);
  529. load = &lw;
  530. }
  531. slice = calc_delta_mine(slice, se->load.weight, load);
  532. }
  533. return slice;
  534. }
  535. /*
  536. * We calculate the vruntime slice of a to be inserted task
  537. *
  538. * vs = s/w
  539. */
  540. static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  541. {
  542. return calc_delta_fair(sched_slice(cfs_rq, se), se);
  543. }
  544. static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update);
  545. static void update_cfs_shares(struct cfs_rq *cfs_rq);
  546. /*
  547. * Update the current task's runtime statistics. Skip current tasks that
  548. * are not in our scheduling class.
  549. */
  550. static inline void
  551. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  552. unsigned long delta_exec)
  553. {
  554. unsigned long delta_exec_weighted;
  555. schedstat_set(curr->statistics.exec_max,
  556. max((u64)delta_exec, curr->statistics.exec_max));
  557. curr->sum_exec_runtime += delta_exec;
  558. schedstat_add(cfs_rq, exec_clock, delta_exec);
  559. delta_exec_weighted = calc_delta_fair(delta_exec, curr);
  560. curr->vruntime += delta_exec_weighted;
  561. update_min_vruntime(cfs_rq);
  562. #if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
  563. cfs_rq->load_unacc_exec_time += delta_exec;
  564. #endif
  565. }
  566. static void update_curr(struct cfs_rq *cfs_rq)
  567. {
  568. struct sched_entity *curr = cfs_rq->curr;
  569. u64 now = rq_of(cfs_rq)->clock_task;
  570. unsigned long delta_exec;
  571. if (unlikely(!curr))
  572. return;
  573. /*
  574. * Get the amount of time the current task was running
  575. * since the last time we changed load (this cannot
  576. * overflow on 32 bits):
  577. */
  578. delta_exec = (unsigned long)(now - curr->exec_start);
  579. if (!delta_exec)
  580. return;
  581. __update_curr(cfs_rq, curr, delta_exec);
  582. curr->exec_start = now;
  583. if (entity_is_task(curr)) {
  584. struct task_struct *curtask = task_of(curr);
  585. trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
  586. cpuacct_charge(curtask, delta_exec);
  587. account_group_exec_runtime(curtask, delta_exec);
  588. }
  589. account_cfs_rq_runtime(cfs_rq, delta_exec);
  590. }
  591. static inline void
  592. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  593. {
  594. schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
  595. }
  596. /*
  597. * Task is being enqueued - update stats:
  598. */
  599. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  600. {
  601. /*
  602. * Are we enqueueing a waiting task? (for current tasks
  603. * a dequeue/enqueue event is a NOP)
  604. */
  605. if (se != cfs_rq->curr)
  606. update_stats_wait_start(cfs_rq, se);
  607. }
  608. static void
  609. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  610. {
  611. schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
  612. rq_of(cfs_rq)->clock - se->statistics.wait_start));
  613. schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
  614. schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
  615. rq_of(cfs_rq)->clock - se->statistics.wait_start);
  616. #ifdef CONFIG_SCHEDSTATS
  617. if (entity_is_task(se)) {
  618. trace_sched_stat_wait(task_of(se),
  619. rq_of(cfs_rq)->clock - se->statistics.wait_start);
  620. }
  621. #endif
  622. schedstat_set(se->statistics.wait_start, 0);
  623. }
  624. static inline void
  625. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  626. {
  627. /*
  628. * Mark the end of the wait period if dequeueing a
  629. * waiting task:
  630. */
  631. if (se != cfs_rq->curr)
  632. update_stats_wait_end(cfs_rq, se);
  633. }
  634. /*
  635. * We are picking a new current task - update its stats:
  636. */
  637. static inline void
  638. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  639. {
  640. /*
  641. * We are starting a new run period:
  642. */
  643. se->exec_start = rq_of(cfs_rq)->clock_task;
  644. }
  645. /**************************************************
  646. * Scheduling class queueing methods:
  647. */
  648. static void
  649. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  650. {
  651. update_load_add(&cfs_rq->load, se->load.weight);
  652. if (!parent_entity(se))
  653. update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
  654. #ifdef CONFIG_SMP
  655. if (entity_is_task(se))
  656. list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
  657. #endif
  658. cfs_rq->nr_running++;
  659. }
  660. static void
  661. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  662. {
  663. update_load_sub(&cfs_rq->load, se->load.weight);
  664. if (!parent_entity(se))
  665. update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
  666. if (entity_is_task(se))
  667. list_del_init(&se->group_node);
  668. cfs_rq->nr_running--;
  669. }
  670. #ifdef CONFIG_FAIR_GROUP_SCHED
  671. /* we need this in update_cfs_load and load-balance functions below */
  672. static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
  673. # ifdef CONFIG_SMP
  674. static void update_cfs_rq_load_contribution(struct cfs_rq *cfs_rq,
  675. int global_update)
  676. {
  677. struct task_group *tg = cfs_rq->tg;
  678. long load_avg;
  679. load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1);
  680. load_avg -= cfs_rq->load_contribution;
  681. if (global_update || abs(load_avg) > cfs_rq->load_contribution / 8) {
  682. atomic_add(load_avg, &tg->load_weight);
  683. cfs_rq->load_contribution += load_avg;
  684. }
  685. }
  686. static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
  687. {
  688. u64 period = sysctl_sched_shares_window;
  689. u64 now, delta;
  690. unsigned long load = cfs_rq->load.weight;
  691. if (cfs_rq->tg == &root_task_group || throttled_hierarchy(cfs_rq))
  692. return;
  693. now = rq_of(cfs_rq)->clock_task;
  694. delta = now - cfs_rq->load_stamp;
  695. /* truncate load history at 4 idle periods */
  696. if (cfs_rq->load_stamp > cfs_rq->load_last &&
  697. now - cfs_rq->load_last > 4 * period) {
  698. cfs_rq->load_period = 0;
  699. cfs_rq->load_avg = 0;
  700. delta = period - 1;
  701. }
  702. cfs_rq->load_stamp = now;
  703. cfs_rq->load_unacc_exec_time = 0;
  704. cfs_rq->load_period += delta;
  705. if (load) {
  706. cfs_rq->load_last = now;
  707. cfs_rq->load_avg += delta * load;
  708. }
  709. /* consider updating load contribution on each fold or truncate */
  710. if (global_update || cfs_rq->load_period > period
  711. || !cfs_rq->load_period)
  712. update_cfs_rq_load_contribution(cfs_rq, global_update);
  713. while (cfs_rq->load_period > period) {
  714. /*
  715. * Inline assembly required to prevent the compiler
  716. * optimising this loop into a divmod call.
  717. * See __iter_div_u64_rem() for another example of this.
  718. */
  719. asm("" : "+rm" (cfs_rq->load_period));
  720. cfs_rq->load_period /= 2;
  721. cfs_rq->load_avg /= 2;
  722. }
  723. if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg)
  724. list_del_leaf_cfs_rq(cfs_rq);
  725. }
  726. static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
  727. {
  728. long tg_weight;
  729. /*
  730. * Use this CPU's actual weight instead of the last load_contribution
  731. * to gain a more accurate current total weight. See
  732. * update_cfs_rq_load_contribution().
  733. */
  734. tg_weight = atomic_read(&tg->load_weight);
  735. tg_weight -= cfs_rq->load_contribution;
  736. tg_weight += cfs_rq->load.weight;
  737. return tg_weight;
  738. }
  739. static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
  740. {
  741. long tg_weight, load, shares;
  742. tg_weight = calc_tg_weight(tg, cfs_rq);
  743. load = cfs_rq->load.weight;
  744. shares = (tg->shares * load);
  745. if (tg_weight)
  746. shares /= tg_weight;
  747. if (shares < MIN_SHARES)
  748. shares = MIN_SHARES;
  749. if (shares > tg->shares)
  750. shares = tg->shares;
  751. return shares;
  752. }
  753. static void update_entity_shares_tick(struct cfs_rq *cfs_rq)
  754. {
  755. if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) {
  756. update_cfs_load(cfs_rq, 0);
  757. update_cfs_shares(cfs_rq);
  758. }
  759. }
  760. # else /* CONFIG_SMP */
  761. static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
  762. {
  763. }
  764. static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
  765. {
  766. return tg->shares;
  767. }
  768. static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
  769. {
  770. }
  771. # endif /* CONFIG_SMP */
  772. static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
  773. unsigned long weight)
  774. {
  775. if (se->on_rq) {
  776. /* commit outstanding execution time */
  777. if (cfs_rq->curr == se)
  778. update_curr(cfs_rq);
  779. account_entity_dequeue(cfs_rq, se);
  780. }
  781. update_load_set(&se->load, weight);
  782. if (se->on_rq)
  783. account_entity_enqueue(cfs_rq, se);
  784. }
  785. static void update_cfs_shares(struct cfs_rq *cfs_rq)
  786. {
  787. struct task_group *tg;
  788. struct sched_entity *se;
  789. long shares;
  790. tg = cfs_rq->tg;
  791. se = tg->se[cpu_of(rq_of(cfs_rq))];
  792. if (!se || throttled_hierarchy(cfs_rq))
  793. return;
  794. #ifndef CONFIG_SMP
  795. if (likely(se->load.weight == tg->shares))
  796. return;
  797. #endif
  798. shares = calc_cfs_shares(cfs_rq, tg);
  799. reweight_entity(cfs_rq_of(se), se, shares);
  800. }
  801. #else /* CONFIG_FAIR_GROUP_SCHED */
  802. static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
  803. {
  804. }
  805. static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
  806. {
  807. }
  808. static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
  809. {
  810. }
  811. #endif /* CONFIG_FAIR_GROUP_SCHED */
  812. #ifdef CONFIG_SMP
  813. /*
  814. * Approximate:
  815. * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
  816. */
  817. static __always_inline u64 decay_load(u64 val, u64 n)
  818. {
  819. for (; n && val; n--) {
  820. val *= 4008;
  821. val >>= 12;
  822. }
  823. return val;
  824. }
  825. /*
  826. * We can represent the historical contribution to runnable average as the
  827. * coefficients of a geometric series. To do this we sub-divide our runnable
  828. * history into segments of approximately 1ms (1024us); label the segment that
  829. * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
  830. *
  831. * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
  832. * p0 p1 p2
  833. * (now) (~1ms ago) (~2ms ago)
  834. *
  835. * Let u_i denote the fraction of p_i that the entity was runnable.
  836. *
  837. * We then designate the fractions u_i as our co-efficients, yielding the
  838. * following representation of historical load:
  839. * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
  840. *
  841. * We choose y based on the with of a reasonably scheduling period, fixing:
  842. * y^32 = 0.5
  843. *
  844. * This means that the contribution to load ~32ms ago (u_32) will be weighted
  845. * approximately half as much as the contribution to load within the last ms
  846. * (u_0).
  847. *
  848. * When a period "rolls over" and we have new u_0`, multiplying the previous
  849. * sum again by y is sufficient to update:
  850. * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
  851. * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
  852. */
  853. static __always_inline int __update_entity_runnable_avg(u64 now,
  854. struct sched_avg *sa,
  855. int runnable)
  856. {
  857. u64 delta;
  858. int delta_w, decayed = 0;
  859. delta = now - sa->last_runnable_update;
  860. /*
  861. * This should only happen when time goes backwards, which it
  862. * unfortunately does during sched clock init when we swap over to TSC.
  863. */
  864. if ((s64)delta < 0) {
  865. sa->last_runnable_update = now;
  866. return 0;
  867. }
  868. /*
  869. * Use 1024ns as the unit of measurement since it's a reasonable
  870. * approximation of 1us and fast to compute.
  871. */
  872. delta >>= 10;
  873. if (!delta)
  874. return 0;
  875. sa->last_runnable_update = now;
  876. /* delta_w is the amount already accumulated against our next period */
  877. delta_w = sa->runnable_avg_period % 1024;
  878. if (delta + delta_w >= 1024) {
  879. /* period roll-over */
  880. decayed = 1;
  881. /*
  882. * Now that we know we're crossing a period boundary, figure
  883. * out how much from delta we need to complete the current
  884. * period and accrue it.
  885. */
  886. delta_w = 1024 - delta_w;
  887. BUG_ON(delta_w > delta);
  888. do {
  889. if (runnable)
  890. sa->runnable_avg_sum += delta_w;
  891. sa->runnable_avg_period += delta_w;
  892. /*
  893. * Remainder of delta initiates a new period, roll over
  894. * the previous.
  895. */
  896. sa->runnable_avg_sum =
  897. decay_load(sa->runnable_avg_sum, 1);
  898. sa->runnable_avg_period =
  899. decay_load(sa->runnable_avg_period, 1);
  900. delta -= delta_w;
  901. /* New period is empty */
  902. delta_w = 1024;
  903. } while (delta >= 1024);
  904. }
  905. /* Remainder of delta accrued against u_0` */
  906. if (runnable)
  907. sa->runnable_avg_sum += delta;
  908. sa->runnable_avg_period += delta;
  909. return decayed;
  910. }
  911. /* Synchronize an entity's decay with its parenting cfs_rq.*/
  912. static inline u64 __synchronize_entity_decay(struct sched_entity *se)
  913. {
  914. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  915. u64 decays = atomic64_read(&cfs_rq->decay_counter);
  916. decays -= se->avg.decay_count;
  917. if (!decays)
  918. return 0;
  919. se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays);
  920. se->avg.decay_count = 0;
  921. return decays;
  922. }
  923. #ifdef CONFIG_FAIR_GROUP_SCHED
  924. static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
  925. int force_update)
  926. {
  927. struct task_group *tg = cfs_rq->tg;
  928. s64 tg_contrib;
  929. tg_contrib = cfs_rq->runnable_load_avg + cfs_rq->blocked_load_avg;
  930. tg_contrib -= cfs_rq->tg_load_contrib;
  931. if (force_update || abs64(tg_contrib) > cfs_rq->tg_load_contrib / 8) {
  932. atomic64_add(tg_contrib, &tg->load_avg);
  933. cfs_rq->tg_load_contrib += tg_contrib;
  934. }
  935. }
  936. /*
  937. * Aggregate cfs_rq runnable averages into an equivalent task_group
  938. * representation for computing load contributions.
  939. */
  940. static inline void __update_tg_runnable_avg(struct sched_avg *sa,
  941. struct cfs_rq *cfs_rq)
  942. {
  943. struct task_group *tg = cfs_rq->tg;
  944. long contrib;
  945. /* The fraction of a cpu used by this cfs_rq */
  946. contrib = div_u64(sa->runnable_avg_sum << NICE_0_SHIFT,
  947. sa->runnable_avg_period + 1);
  948. contrib -= cfs_rq->tg_runnable_contrib;
  949. if (abs(contrib) > cfs_rq->tg_runnable_contrib / 64) {
  950. atomic_add(contrib, &tg->runnable_avg);
  951. cfs_rq->tg_runnable_contrib += contrib;
  952. }
  953. }
  954. static inline void __update_group_entity_contrib(struct sched_entity *se)
  955. {
  956. struct cfs_rq *cfs_rq = group_cfs_rq(se);
  957. struct task_group *tg = cfs_rq->tg;
  958. int runnable_avg;
  959. u64 contrib;
  960. contrib = cfs_rq->tg_load_contrib * tg->shares;
  961. se->avg.load_avg_contrib = div64_u64(contrib,
  962. atomic64_read(&tg->load_avg) + 1);
  963. /*
  964. * For group entities we need to compute a correction term in the case
  965. * that they are consuming <1 cpu so that we would contribute the same
  966. * load as a task of equal weight.
  967. *
  968. * Explicitly co-ordinating this measurement would be expensive, but
  969. * fortunately the sum of each cpus contribution forms a usable
  970. * lower-bound on the true value.
  971. *
  972. * Consider the aggregate of 2 contributions. Either they are disjoint
  973. * (and the sum represents true value) or they are disjoint and we are
  974. * understating by the aggregate of their overlap.
  975. *
  976. * Extending this to N cpus, for a given overlap, the maximum amount we
  977. * understand is then n_i(n_i+1)/2 * w_i where n_i is the number of
  978. * cpus that overlap for this interval and w_i is the interval width.
  979. *
  980. * On a small machine; the first term is well-bounded which bounds the
  981. * total error since w_i is a subset of the period. Whereas on a
  982. * larger machine, while this first term can be larger, if w_i is the
  983. * of consequential size guaranteed to see n_i*w_i quickly converge to
  984. * our upper bound of 1-cpu.
  985. */
  986. runnable_avg = atomic_read(&tg->runnable_avg);
  987. if (runnable_avg < NICE_0_LOAD) {
  988. se->avg.load_avg_contrib *= runnable_avg;
  989. se->avg.load_avg_contrib >>= NICE_0_SHIFT;
  990. }
  991. }
  992. #else
  993. static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
  994. int force_update) {}
  995. static inline void __update_tg_runnable_avg(struct sched_avg *sa,
  996. struct cfs_rq *cfs_rq) {}
  997. static inline void __update_group_entity_contrib(struct sched_entity *se) {}
  998. #endif
  999. static inline void __update_task_entity_contrib(struct sched_entity *se)
  1000. {
  1001. u32 contrib;
  1002. /* avoid overflowing a 32-bit type w/ SCHED_LOAD_SCALE */
  1003. contrib = se->avg.runnable_avg_sum * scale_load_down(se->load.weight);
  1004. contrib /= (se->avg.runnable_avg_period + 1);
  1005. se->avg.load_avg_contrib = scale_load(contrib);
  1006. }
  1007. /* Compute the current contribution to load_avg by se, return any delta */
  1008. static long __update_entity_load_avg_contrib(struct sched_entity *se)
  1009. {
  1010. long old_contrib = se->avg.load_avg_contrib;
  1011. if (entity_is_task(se)) {
  1012. __update_task_entity_contrib(se);
  1013. } else {
  1014. __update_tg_runnable_avg(&se->avg, group_cfs_rq(se));
  1015. __update_group_entity_contrib(se);
  1016. }
  1017. return se->avg.load_avg_contrib - old_contrib;
  1018. }
  1019. static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq,
  1020. long load_contrib)
  1021. {
  1022. if (likely(load_contrib < cfs_rq->blocked_load_avg))
  1023. cfs_rq->blocked_load_avg -= load_contrib;
  1024. else
  1025. cfs_rq->blocked_load_avg = 0;
  1026. }
  1027. /* Update a sched_entity's runnable average */
  1028. static inline void update_entity_load_avg(struct sched_entity *se,
  1029. int update_cfs_rq)
  1030. {
  1031. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  1032. long contrib_delta;
  1033. if (!__update_entity_runnable_avg(rq_of(cfs_rq)->clock_task, &se->avg,
  1034. se->on_rq))
  1035. return;
  1036. contrib_delta = __update_entity_load_avg_contrib(se);
  1037. if (!update_cfs_rq)
  1038. return;
  1039. if (se->on_rq)
  1040. cfs_rq->runnable_load_avg += contrib_delta;
  1041. else
  1042. subtract_blocked_load_contrib(cfs_rq, -contrib_delta);
  1043. }
  1044. /*
  1045. * Decay the load contributed by all blocked children and account this so that
  1046. * their contribution may appropriately discounted when they wake up.
  1047. */
  1048. static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, int force_update)
  1049. {
  1050. u64 now = rq_of(cfs_rq)->clock_task >> 20;
  1051. u64 decays;
  1052. decays = now - cfs_rq->last_decay;
  1053. if (!decays && !force_update)
  1054. return;
  1055. if (atomic64_read(&cfs_rq->removed_load)) {
  1056. u64 removed_load = atomic64_xchg(&cfs_rq->removed_load, 0);
  1057. subtract_blocked_load_contrib(cfs_rq, removed_load);
  1058. }
  1059. if (decays) {
  1060. cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg,
  1061. decays);
  1062. atomic64_add(decays, &cfs_rq->decay_counter);
  1063. cfs_rq->last_decay = now;
  1064. }
  1065. __update_cfs_rq_tg_load_contrib(cfs_rq, force_update);
  1066. }
  1067. static inline void update_rq_runnable_avg(struct rq *rq, int runnable)
  1068. {
  1069. __update_entity_runnable_avg(rq->clock_task, &rq->avg, runnable);
  1070. __update_tg_runnable_avg(&rq->avg, &rq->cfs);
  1071. }
  1072. /* Add the load generated by se into cfs_rq's child load-average */
  1073. static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
  1074. struct sched_entity *se,
  1075. int wakeup)
  1076. {
  1077. /*
  1078. * We track migrations using entity decay_count <= 0, on a wake-up
  1079. * migration we use a negative decay count to track the remote decays
  1080. * accumulated while sleeping.
  1081. */
  1082. if (unlikely(se->avg.decay_count <= 0)) {
  1083. se->avg.last_runnable_update = rq_of(cfs_rq)->clock_task;
  1084. if (se->avg.decay_count) {
  1085. /*
  1086. * In a wake-up migration we have to approximate the
  1087. * time sleeping. This is because we can't synchronize
  1088. * clock_task between the two cpus, and it is not
  1089. * guaranteed to be read-safe. Instead, we can
  1090. * approximate this using our carried decays, which are
  1091. * explicitly atomically readable.
  1092. */
  1093. se->avg.last_runnable_update -= (-se->avg.decay_count)
  1094. << 20;
  1095. update_entity_load_avg(se, 0);
  1096. /* Indicate that we're now synchronized and on-rq */
  1097. se->avg.decay_count = 0;
  1098. }
  1099. wakeup = 0;
  1100. } else {
  1101. __synchronize_entity_decay(se);
  1102. }
  1103. /* migrated tasks did not contribute to our blocked load */
  1104. if (wakeup) {
  1105. subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib);
  1106. update_entity_load_avg(se, 0);
  1107. }
  1108. cfs_rq->runnable_load_avg += se->avg.load_avg_contrib;
  1109. /* we force update consideration on load-balancer moves */
  1110. update_cfs_rq_blocked_load(cfs_rq, !wakeup);
  1111. }
  1112. /*
  1113. * Remove se's load from this cfs_rq child load-average, if the entity is
  1114. * transitioning to a blocked state we track its projected decay using
  1115. * blocked_load_avg.
  1116. */
  1117. static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
  1118. struct sched_entity *se,
  1119. int sleep)
  1120. {
  1121. update_entity_load_avg(se, 1);
  1122. /* we force update consideration on load-balancer moves */
  1123. update_cfs_rq_blocked_load(cfs_rq, !sleep);
  1124. cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib;
  1125. if (sleep) {
  1126. cfs_rq->blocked_load_avg += se->avg.load_avg_contrib;
  1127. se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
  1128. } /* migrations, e.g. sleep=0 leave decay_count == 0 */
  1129. }
  1130. #else
  1131. static inline void update_entity_load_avg(struct sched_entity *se,
  1132. int update_cfs_rq) {}
  1133. static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {}
  1134. static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
  1135. struct sched_entity *se,
  1136. int wakeup) {}
  1137. static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
  1138. struct sched_entity *se,
  1139. int sleep) {}
  1140. static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
  1141. int force_update) {}
  1142. #endif
  1143. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  1144. {
  1145. #ifdef CONFIG_SCHEDSTATS
  1146. struct task_struct *tsk = NULL;
  1147. if (entity_is_task(se))
  1148. tsk = task_of(se);
  1149. if (se->statistics.sleep_start) {
  1150. u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
  1151. if ((s64)delta < 0)
  1152. delta = 0;
  1153. if (unlikely(delta > se->statistics.sleep_max))
  1154. se->statistics.sleep_max = delta;
  1155. se->statistics.sleep_start = 0;
  1156. se->statistics.sum_sleep_runtime += delta;
  1157. if (tsk) {
  1158. account_scheduler_latency(tsk, delta >> 10, 1);
  1159. trace_sched_stat_sleep(tsk, delta);
  1160. }
  1161. }
  1162. if (se->statistics.block_start) {
  1163. u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
  1164. if ((s64)delta < 0)
  1165. delta = 0;
  1166. if (unlikely(delta > se->statistics.block_max))
  1167. se->statistics.block_max = delta;
  1168. se->statistics.block_start = 0;
  1169. se->statistics.sum_sleep_runtime += delta;
  1170. if (tsk) {
  1171. if (tsk->in_iowait) {
  1172. se->statistics.iowait_sum += delta;
  1173. se->statistics.iowait_count++;
  1174. trace_sched_stat_iowait(tsk, delta);
  1175. }
  1176. trace_sched_stat_blocked(tsk, delta);
  1177. /*
  1178. * Blocking time is in units of nanosecs, so shift by
  1179. * 20 to get a milliseconds-range estimation of the
  1180. * amount of time that the task spent sleeping:
  1181. */
  1182. if (unlikely(prof_on == SLEEP_PROFILING)) {
  1183. profile_hits(SLEEP_PROFILING,
  1184. (void *)get_wchan(tsk),
  1185. delta >> 20);
  1186. }
  1187. account_scheduler_latency(tsk, delta >> 10, 0);
  1188. }
  1189. }
  1190. #endif
  1191. }
  1192. static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  1193. {
  1194. #ifdef CONFIG_SCHED_DEBUG
  1195. s64 d = se->vruntime - cfs_rq->min_vruntime;
  1196. if (d < 0)
  1197. d = -d;
  1198. if (d > 3*sysctl_sched_latency)
  1199. schedstat_inc(cfs_rq, nr_spread_over);
  1200. #endif
  1201. }
  1202. static void
  1203. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  1204. {
  1205. u64 vruntime = cfs_rq->min_vruntime;
  1206. /*
  1207. * The 'current' period is already promised to the current tasks,
  1208. * however the extra weight of the new task will slow them down a
  1209. * little, place the new task so that it fits in the slot that
  1210. * stays open at the end.
  1211. */
  1212. if (initial && sched_feat(START_DEBIT))
  1213. vruntime += sched_vslice(cfs_rq, se);
  1214. /* sleeps up to a single latency don't count. */
  1215. if (!initial) {
  1216. unsigned long thresh = sysctl_sched_latency;
  1217. /*
  1218. * Halve their sleep time's effect, to allow
  1219. * for a gentler effect of sleepers:
  1220. */
  1221. if (sched_feat(GENTLE_FAIR_SLEEPERS))
  1222. thresh >>= 1;
  1223. vruntime -= thresh;
  1224. }
  1225. /* ensure we never gain time by being placed backwards. */
  1226. vruntime = max_vruntime(se->vruntime, vruntime);
  1227. se->vruntime = vruntime;
  1228. }
  1229. static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
  1230. static void
  1231. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
  1232. {
  1233. /*
  1234. * Update the normalized vruntime before updating min_vruntime
  1235. * through callig update_curr().
  1236. */
  1237. if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
  1238. se->vruntime += cfs_rq->min_vruntime;
  1239. /*
  1240. * Update run-time statistics of the 'current'.
  1241. */
  1242. update_curr(cfs_rq);
  1243. update_cfs_load(cfs_rq, 0);
  1244. enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP);
  1245. account_entity_enqueue(cfs_rq, se);
  1246. update_cfs_shares(cfs_rq);
  1247. if (flags & ENQUEUE_WAKEUP) {
  1248. place_entity(cfs_rq, se, 0);
  1249. enqueue_sleeper(cfs_rq, se);
  1250. }
  1251. update_stats_enqueue(cfs_rq, se);
  1252. check_spread(cfs_rq, se);
  1253. if (se != cfs_rq->curr)
  1254. __enqueue_entity(cfs_rq, se);
  1255. se->on_rq = 1;
  1256. if (cfs_rq->nr_running == 1) {
  1257. list_add_leaf_cfs_rq(cfs_rq);
  1258. check_enqueue_throttle(cfs_rq);
  1259. }
  1260. }
  1261. static void __clear_buddies_last(struct sched_entity *se)
  1262. {
  1263. for_each_sched_entity(se) {
  1264. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  1265. if (cfs_rq->last == se)
  1266. cfs_rq->last = NULL;
  1267. else
  1268. break;
  1269. }
  1270. }
  1271. static void __clear_buddies_next(struct sched_entity *se)
  1272. {
  1273. for_each_sched_entity(se) {
  1274. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  1275. if (cfs_rq->next == se)
  1276. cfs_rq->next = NULL;
  1277. else
  1278. break;
  1279. }
  1280. }
  1281. static void __clear_buddies_skip(struct sched_entity *se)
  1282. {
  1283. for_each_sched_entity(se) {
  1284. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  1285. if (cfs_rq->skip == se)
  1286. cfs_rq->skip = NULL;
  1287. else
  1288. break;
  1289. }
  1290. }
  1291. static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
  1292. {
  1293. if (cfs_rq->last == se)
  1294. __clear_buddies_last(se);
  1295. if (cfs_rq->next == se)
  1296. __clear_buddies_next(se);
  1297. if (cfs_rq->skip == se)
  1298. __clear_buddies_skip(se);
  1299. }
  1300. static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
  1301. static void
  1302. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
  1303. {
  1304. /*
  1305. * Update run-time statistics of the 'current'.
  1306. */
  1307. update_curr(cfs_rq);
  1308. dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP);
  1309. update_stats_dequeue(cfs_rq, se);
  1310. if (flags & DEQUEUE_SLEEP) {
  1311. #ifdef CONFIG_SCHEDSTATS
  1312. if (entity_is_task(se)) {
  1313. struct task_struct *tsk = task_of(se);
  1314. if (tsk->state & TASK_INTERRUPTIBLE)
  1315. se->statistics.sleep_start = rq_of(cfs_rq)->clock;
  1316. if (tsk->state & TASK_UNINTERRUPTIBLE)
  1317. se->statistics.block_start = rq_of(cfs_rq)->clock;
  1318. }
  1319. #endif
  1320. }
  1321. clear_buddies(cfs_rq, se);
  1322. if (se != cfs_rq->curr)
  1323. __dequeue_entity(cfs_rq, se);
  1324. se->on_rq = 0;
  1325. update_cfs_load(cfs_rq, 0);
  1326. account_entity_dequeue(cfs_rq, se);
  1327. /*
  1328. * Normalize the entity after updating the min_vruntime because the
  1329. * update can refer to the ->curr item and we need to reflect this
  1330. * movement in our normalized position.
  1331. */
  1332. if (!(flags & DEQUEUE_SLEEP))
  1333. se->vruntime -= cfs_rq->min_vruntime;
  1334. /* return excess runtime on last dequeue */
  1335. return_cfs_rq_runtime(cfs_rq);
  1336. update_min_vruntime(cfs_rq);
  1337. update_cfs_shares(cfs_rq);
  1338. }
  1339. /*
  1340. * Preempt the current task with a newly woken task if needed:
  1341. */
  1342. static void
  1343. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  1344. {
  1345. unsigned long ideal_runtime, delta_exec;
  1346. struct sched_entity *se;
  1347. s64 delta;
  1348. ideal_runtime = sched_slice(cfs_rq, curr);
  1349. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  1350. if (delta_exec > ideal_runtime) {
  1351. resched_task(rq_of(cfs_rq)->curr);
  1352. /*
  1353. * The current task ran long enough, ensure it doesn't get
  1354. * re-elected due to buddy favours.
  1355. */
  1356. clear_buddies(cfs_rq, curr);
  1357. return;
  1358. }
  1359. /*
  1360. * Ensure that a task that missed wakeup preemption by a
  1361. * narrow margin doesn't have to wait for a full slice.
  1362. * This also mitigates buddy induced latencies under load.
  1363. */
  1364. if (delta_exec < sysctl_sched_min_granularity)
  1365. return;
  1366. se = __pick_first_entity(cfs_rq);
  1367. delta = curr->vruntime - se->vruntime;
  1368. if (delta < 0)
  1369. return;
  1370. if (delta > ideal_runtime)
  1371. resched_task(rq_of(cfs_rq)->curr);
  1372. }
  1373. static void
  1374. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  1375. {
  1376. /* 'current' is not kept within the tree. */
  1377. if (se->on_rq) {
  1378. /*
  1379. * Any task has to be enqueued before it get to execute on
  1380. * a CPU. So account for the time it spent waiting on the
  1381. * runqueue.
  1382. */
  1383. update_stats_wait_end(cfs_rq, se);
  1384. __dequeue_entity(cfs_rq, se);
  1385. }
  1386. update_stats_curr_start(cfs_rq, se);
  1387. cfs_rq->curr = se;
  1388. #ifdef CONFIG_SCHEDSTATS
  1389. /*
  1390. * Track our maximum slice length, if the CPU's load is at
  1391. * least twice that of our own weight (i.e. dont track it
  1392. * when there are only lesser-weight tasks around):
  1393. */
  1394. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  1395. se->statistics.slice_max = max(se->statistics.slice_max,
  1396. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  1397. }
  1398. #endif
  1399. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  1400. }
  1401. static int
  1402. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
  1403. /*
  1404. * Pick the next process, keeping these things in mind, in this order:
  1405. * 1) keep things fair between processes/task groups
  1406. * 2) pick the "next" process, since someone really wants that to run
  1407. * 3) pick the "last" process, for cache locality
  1408. * 4) do not run the "skip" process, if something else is available
  1409. */
  1410. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  1411. {
  1412. struct sched_entity *se = __pick_first_entity(cfs_rq);
  1413. struct sched_entity *left = se;
  1414. /*
  1415. * Avoid running the skip buddy, if running something else can
  1416. * be done without getting too unfair.
  1417. */
  1418. if (cfs_rq->skip == se) {
  1419. struct sched_entity *second = __pick_next_entity(se);
  1420. if (second && wakeup_preempt_entity(second, left) < 1)
  1421. se = second;
  1422. }
  1423. /*
  1424. * Prefer last buddy, try to return the CPU to a preempted task.
  1425. */
  1426. if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
  1427. se = cfs_rq->last;
  1428. /*
  1429. * Someone really wants this to run. If it's not unfair, run it.
  1430. */
  1431. if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
  1432. se = cfs_rq->next;
  1433. clear_buddies(cfs_rq, se);
  1434. return se;
  1435. }
  1436. static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
  1437. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  1438. {
  1439. /*
  1440. * If still on the runqueue then deactivate_task()
  1441. * was not called and update_curr() has to be done:
  1442. */
  1443. if (prev->on_rq)
  1444. update_curr(cfs_rq);
  1445. /* throttle cfs_rqs exceeding runtime */
  1446. check_cfs_rq_runtime(cfs_rq);
  1447. check_spread(cfs_rq, prev);
  1448. if (prev->on_rq) {
  1449. update_stats_wait_start(cfs_rq, prev);
  1450. /* Put 'current' back into the tree. */
  1451. __enqueue_entity(cfs_rq, prev);
  1452. /* in !on_rq case, update occurred at dequeue */
  1453. update_entity_load_avg(prev, 1);
  1454. }
  1455. cfs_rq->curr = NULL;
  1456. }
  1457. static void
  1458. entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  1459. {
  1460. /*
  1461. * Update run-time statistics of the 'current'.
  1462. */
  1463. update_curr(cfs_rq);
  1464. /*
  1465. * Ensure that runnable average is periodically updated.
  1466. */
  1467. update_entity_load_avg(curr, 1);
  1468. update_cfs_rq_blocked_load(cfs_rq, 1);
  1469. /*
  1470. * Update share accounting for long-running entities.
  1471. */
  1472. update_entity_shares_tick(cfs_rq);
  1473. #ifdef CONFIG_SCHED_HRTICK
  1474. /*
  1475. * queued ticks are scheduled to match the slice, so don't bother
  1476. * validating it and just reschedule.
  1477. */
  1478. if (queued) {
  1479. resched_task(rq_of(cfs_rq)->curr);
  1480. return;
  1481. }
  1482. /*
  1483. * don't let the period tick interfere with the hrtick preemption
  1484. */
  1485. if (!sched_feat(DOUBLE_TICK) &&
  1486. hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
  1487. return;
  1488. #endif
  1489. if (cfs_rq->nr_running > 1)
  1490. check_preempt_tick(cfs_rq, curr);
  1491. }
  1492. /**************************************************
  1493. * CFS bandwidth control machinery
  1494. */
  1495. #ifdef CONFIG_CFS_BANDWIDTH
  1496. #ifdef HAVE_JUMP_LABEL
  1497. static struct static_key __cfs_bandwidth_used;
  1498. static inline bool cfs_bandwidth_used(void)
  1499. {
  1500. return static_key_false(&__cfs_bandwidth_used);
  1501. }
  1502. void account_cfs_bandwidth_used(int enabled, int was_enabled)
  1503. {
  1504. /* only need to count groups transitioning between enabled/!enabled */
  1505. if (enabled && !was_enabled)
  1506. static_key_slow_inc(&__cfs_bandwidth_used);
  1507. else if (!enabled && was_enabled)
  1508. static_key_slow_dec(&__cfs_bandwidth_used);
  1509. }
  1510. #else /* HAVE_JUMP_LABEL */
  1511. static bool cfs_bandwidth_used(void)
  1512. {
  1513. return true;
  1514. }
  1515. void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
  1516. #endif /* HAVE_JUMP_LABEL */
  1517. /*
  1518. * default period for cfs group bandwidth.
  1519. * default: 0.1s, units: nanoseconds
  1520. */
  1521. static inline u64 default_cfs_period(void)
  1522. {
  1523. return 100000000ULL;
  1524. }
  1525. static inline u64 sched_cfs_bandwidth_slice(void)
  1526. {
  1527. return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
  1528. }
  1529. /*
  1530. * Replenish runtime according to assigned quota and update expiration time.
  1531. * We use sched_clock_cpu directly instead of rq->clock to avoid adding
  1532. * additional synchronization around rq->lock.
  1533. *
  1534. * requires cfs_b->lock
  1535. */
  1536. void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
  1537. {
  1538. u64 now;
  1539. if (cfs_b->quota == RUNTIME_INF)
  1540. return;
  1541. now = sched_clock_cpu(smp_processor_id());
  1542. cfs_b->runtime = cfs_b->quota;
  1543. cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
  1544. }
  1545. static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
  1546. {
  1547. return &tg->cfs_bandwidth;
  1548. }
  1549. /* returns 0 on failure to allocate runtime */
  1550. static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  1551. {
  1552. struct task_group *tg = cfs_rq->tg;
  1553. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
  1554. u64 amount = 0, min_amount, expires;
  1555. /* note: this is a positive sum as runtime_remaining <= 0 */
  1556. min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
  1557. raw_spin_lock(&cfs_b->lock);
  1558. if (cfs_b->quota == RUNTIME_INF)
  1559. amount = min_amount;
  1560. else {
  1561. /*
  1562. * If the bandwidth pool has become inactive, then at least one
  1563. * period must have elapsed since the last consumption.
  1564. * Refresh the global state and ensure bandwidth timer becomes
  1565. * active.
  1566. */
  1567. if (!cfs_b->timer_active) {
  1568. __refill_cfs_bandwidth_runtime(cfs_b);
  1569. __start_cfs_bandwidth(cfs_b);
  1570. }
  1571. if (cfs_b->runtime > 0) {
  1572. amount = min(cfs_b->runtime, min_amount);
  1573. cfs_b->runtime -= amount;
  1574. cfs_b->idle = 0;
  1575. }
  1576. }
  1577. expires = cfs_b->runtime_expires;
  1578. raw_spin_unlock(&cfs_b->lock);
  1579. cfs_rq->runtime_remaining += amount;
  1580. /*
  1581. * we may have advanced our local expiration to account for allowed
  1582. * spread between our sched_clock and the one on which runtime was
  1583. * issued.
  1584. */
  1585. if ((s64)(expires - cfs_rq->runtime_expires) > 0)
  1586. cfs_rq->runtime_expires = expires;
  1587. return cfs_rq->runtime_remaining > 0;
  1588. }
  1589. /*
  1590. * Note: This depends on the synchronization provided by sched_clock and the
  1591. * fact that rq->clock snapshots this value.
  1592. */
  1593. static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  1594. {
  1595. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  1596. struct rq *rq = rq_of(cfs_rq);
  1597. /* if the deadline is ahead of our clock, nothing to do */
  1598. if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
  1599. return;
  1600. if (cfs_rq->runtime_remaining < 0)
  1601. return;
  1602. /*
  1603. * If the local deadline has passed we have to consider the
  1604. * possibility that our sched_clock is 'fast' and the global deadline
  1605. * has not truly expired.
  1606. *
  1607. * Fortunately we can check determine whether this the case by checking
  1608. * whether the global deadline has advanced.
  1609. */
  1610. if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
  1611. /* extend local deadline, drift is bounded above by 2 ticks */
  1612. cfs_rq->runtime_expires += TICK_NSEC;
  1613. } else {
  1614. /* global deadline is ahead, expiration has passed */
  1615. cfs_rq->runtime_remaining = 0;
  1616. }
  1617. }
  1618. static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
  1619. unsigned long delta_exec)
  1620. {
  1621. /* dock delta_exec before expiring quota (as it could span periods) */
  1622. cfs_rq->runtime_remaining -= delta_exec;
  1623. expire_cfs_rq_runtime(cfs_rq);
  1624. if (likely(cfs_rq->runtime_remaining > 0))
  1625. return;
  1626. /*
  1627. * if we're unable to extend our runtime we resched so that the active
  1628. * hierarchy can be throttled
  1629. */
  1630. if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
  1631. resched_task(rq_of(cfs_rq)->curr);
  1632. }
  1633. static __always_inline
  1634. void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
  1635. {
  1636. if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
  1637. return;
  1638. __account_cfs_rq_runtime(cfs_rq, delta_exec);
  1639. }
  1640. static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
  1641. {
  1642. return cfs_bandwidth_used() && cfs_rq->throttled;
  1643. }
  1644. /* check whether cfs_rq, or any parent, is throttled */
  1645. static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
  1646. {
  1647. return cfs_bandwidth_used() && cfs_rq->throttle_count;
  1648. }
  1649. /*
  1650. * Ensure that neither of the group entities corresponding to src_cpu or
  1651. * dest_cpu are members of a throttled hierarchy when performing group
  1652. * load-balance operations.
  1653. */
  1654. static inline int throttled_lb_pair(struct task_group *tg,
  1655. int src_cpu, int dest_cpu)
  1656. {
  1657. struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
  1658. src_cfs_rq = tg->cfs_rq[src_cpu];
  1659. dest_cfs_rq = tg->cfs_rq[dest_cpu];
  1660. return throttled_hierarchy(src_cfs_rq) ||
  1661. throttled_hierarchy(dest_cfs_rq);
  1662. }
  1663. /* updated child weight may affect parent so we have to do this bottom up */
  1664. static int tg_unthrottle_up(struct task_group *tg, void *data)
  1665. {
  1666. struct rq *rq = data;
  1667. struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
  1668. cfs_rq->throttle_count--;
  1669. #ifdef CONFIG_SMP
  1670. if (!cfs_rq->throttle_count) {
  1671. u64 delta = rq->clock_task - cfs_rq->load_stamp;
  1672. /* leaving throttled state, advance shares averaging windows */
  1673. cfs_rq->load_stamp += delta;
  1674. cfs_rq->load_last += delta;
  1675. /* update entity weight now that we are on_rq again */
  1676. update_cfs_shares(cfs_rq);
  1677. }
  1678. #endif
  1679. return 0;
  1680. }
  1681. static int tg_throttle_down(struct task_group *tg, void *data)
  1682. {
  1683. struct rq *rq = data;
  1684. struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
  1685. /* group is entering throttled state, record last load */
  1686. if (!cfs_rq->throttle_count)
  1687. update_cfs_load(cfs_rq, 0);
  1688. cfs_rq->throttle_count++;
  1689. return 0;
  1690. }
  1691. static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
  1692. {
  1693. struct rq *rq = rq_of(cfs_rq);
  1694. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  1695. struct sched_entity *se;
  1696. long task_delta, dequeue = 1;
  1697. se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
  1698. /* account load preceding throttle */
  1699. rcu_read_lock();
  1700. walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
  1701. rcu_read_unlock();
  1702. task_delta = cfs_rq->h_nr_running;
  1703. for_each_sched_entity(se) {
  1704. struct cfs_rq *qcfs_rq = cfs_rq_of(se);
  1705. /* throttled entity or throttle-on-deactivate */
  1706. if (!se->on_rq)
  1707. break;
  1708. if (dequeue)
  1709. dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
  1710. qcfs_rq->h_nr_running -= task_delta;
  1711. if (qcfs_rq->load.weight)
  1712. dequeue = 0;
  1713. }
  1714. if (!se)
  1715. rq->nr_running -= task_delta;
  1716. cfs_rq->throttled = 1;
  1717. cfs_rq->throttled_timestamp = rq->clock;
  1718. raw_spin_lock(&cfs_b->lock);
  1719. list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
  1720. raw_spin_unlock(&cfs_b->lock);
  1721. }
  1722. void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
  1723. {
  1724. struct rq *rq = rq_of(cfs_rq);
  1725. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  1726. struct sched_entity *se;
  1727. int enqueue = 1;
  1728. long task_delta;
  1729. se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
  1730. cfs_rq->throttled = 0;
  1731. raw_spin_lock(&cfs_b->lock);
  1732. cfs_b->throttled_time += rq->clock - cfs_rq->throttled_timestamp;
  1733. list_del_rcu(&cfs_rq->throttled_list);
  1734. raw_spin_unlock(&cfs_b->lock);
  1735. cfs_rq->throttled_timestamp = 0;
  1736. update_rq_clock(rq);
  1737. /* update hierarchical throttle state */
  1738. walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
  1739. if (!cfs_rq->load.weight)
  1740. return;
  1741. task_delta = cfs_rq->h_nr_running;
  1742. for_each_sched_entity(se) {
  1743. if (se->on_rq)
  1744. enqueue = 0;
  1745. cfs_rq = cfs_rq_of(se);
  1746. if (enqueue)
  1747. enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
  1748. cfs_rq->h_nr_running += task_delta;
  1749. if (cfs_rq_throttled(cfs_rq))
  1750. break;
  1751. }
  1752. if (!se)
  1753. rq->nr_running += task_delta;
  1754. /* determine whether we need to wake up potentially idle cpu */
  1755. if (rq->curr == rq->idle && rq->cfs.nr_running)
  1756. resched_task(rq->curr);
  1757. }
  1758. static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
  1759. u64 remaining, u64 expires)
  1760. {
  1761. struct cfs_rq *cfs_rq;
  1762. u64 runtime = remaining;
  1763. rcu_read_lock();
  1764. list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
  1765. throttled_list) {
  1766. struct rq *rq = rq_of(cfs_rq);
  1767. raw_spin_lock(&rq->lock);
  1768. if (!cfs_rq_throttled(cfs_rq))
  1769. goto next;
  1770. runtime = -cfs_rq->runtime_remaining + 1;
  1771. if (runtime > remaining)
  1772. runtime = remaining;
  1773. remaining -= runtime;
  1774. cfs_rq->runtime_remaining += runtime;
  1775. cfs_rq->runtime_expires = expires;
  1776. /* we check whether we're throttled above */
  1777. if (cfs_rq->runtime_remaining > 0)
  1778. unthrottle_cfs_rq(cfs_rq);
  1779. next:
  1780. raw_spin_unlock(&rq->lock);
  1781. if (!remaining)
  1782. break;
  1783. }
  1784. rcu_read_unlock();
  1785. return remaining;
  1786. }
  1787. /*
  1788. * Responsible for refilling a task_group's bandwidth and unthrottling its
  1789. * cfs_rqs as appropriate. If there has been no activity within the last
  1790. * period the timer is deactivated until scheduling resumes; cfs_b->idle is
  1791. * used to track this state.
  1792. */
  1793. static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
  1794. {
  1795. u64 runtime, runtime_expires;
  1796. int idle = 1, throttled;
  1797. raw_spin_lock(&cfs_b->lock);
  1798. /* no need to continue the timer with no bandwidth constraint */
  1799. if (cfs_b->quota == RUNTIME_INF)
  1800. goto out_unlock;
  1801. throttled = !list_empty(&cfs_b->throttled_cfs_rq);
  1802. /* idle depends on !throttled (for the case of a large deficit) */
  1803. idle = cfs_b->idle && !throttled;
  1804. cfs_b->nr_periods += overrun;
  1805. /* if we're going inactive then everything else can be deferred */
  1806. if (idle)
  1807. goto out_unlock;
  1808. __refill_cfs_bandwidth_runtime(cfs_b);
  1809. if (!throttled) {
  1810. /* mark as potentially idle for the upcoming period */
  1811. cfs_b->idle = 1;
  1812. goto out_unlock;
  1813. }
  1814. /* account preceding periods in which throttling occurred */
  1815. cfs_b->nr_throttled += overrun;
  1816. /*
  1817. * There are throttled entities so we must first use the new bandwidth
  1818. * to unthrottle them before making it generally available. This
  1819. * ensures that all existing debts will be paid before a new cfs_rq is
  1820. * allowed to run.
  1821. */
  1822. runtime = cfs_b->runtime;
  1823. runtime_expires = cfs_b->runtime_expires;
  1824. cfs_b->runtime = 0;
  1825. /*
  1826. * This check is repeated as we are holding onto the new bandwidth
  1827. * while we unthrottle. This can potentially race with an unthrottled
  1828. * group trying to acquire new bandwidth from the global pool.
  1829. */
  1830. while (throttled && runtime > 0) {
  1831. raw_spin_unlock(&cfs_b->lock);
  1832. /* we can't nest cfs_b->lock while distributing bandwidth */
  1833. runtime = distribute_cfs_runtime(cfs_b, runtime,
  1834. runtime_expires);
  1835. raw_spin_lock(&cfs_b->lock);
  1836. throttled = !list_empty(&cfs_b->throttled_cfs_rq);
  1837. }
  1838. /* return (any) remaining runtime */
  1839. cfs_b->runtime = runtime;
  1840. /*
  1841. * While we are ensured activity in the period following an
  1842. * unthrottle, this also covers the case in which the new bandwidth is
  1843. * insufficient to cover the existing bandwidth deficit. (Forcing the
  1844. * timer to remain active while there are any throttled entities.)
  1845. */
  1846. cfs_b->idle = 0;
  1847. out_unlock:
  1848. if (idle)
  1849. cfs_b->timer_active = 0;
  1850. raw_spin_unlock(&cfs_b->lock);
  1851. return idle;
  1852. }
  1853. /* a cfs_rq won't donate quota below this amount */
  1854. static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
  1855. /* minimum remaining period time to redistribute slack quota */
  1856. static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
  1857. /* how long we wait to gather additional slack before distributing */
  1858. static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
  1859. /* are we near the end of the current quota period? */
  1860. static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
  1861. {
  1862. struct hrtimer *refresh_timer = &cfs_b->period_timer;
  1863. u64 remaining;
  1864. /* if the call-back is running a quota refresh is already occurring */
  1865. if (hrtimer_callback_running(refresh_timer))
  1866. return 1;
  1867. /* is a quota refresh about to occur? */
  1868. remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
  1869. if (remaining < min_expire)
  1870. return 1;
  1871. return 0;
  1872. }
  1873. static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
  1874. {
  1875. u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
  1876. /* if there's a quota refresh soon don't bother with slack */
  1877. if (runtime_refresh_within(cfs_b, min_left))
  1878. return;
  1879. start_bandwidth_timer(&cfs_b->slack_timer,
  1880. ns_to_ktime(cfs_bandwidth_slack_period));
  1881. }
  1882. /* we know any runtime found here is valid as update_curr() precedes return */
  1883. static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  1884. {
  1885. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  1886. s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
  1887. if (slack_runtime <= 0)
  1888. return;
  1889. raw_spin_lock(&cfs_b->lock);
  1890. if (cfs_b->quota != RUNTIME_INF &&
  1891. cfs_rq->runtime_expires == cfs_b->runtime_expires) {
  1892. cfs_b->runtime += slack_runtime;
  1893. /* we are under rq->lock, defer unthrottling using a timer */
  1894. if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
  1895. !list_empty(&cfs_b->throttled_cfs_rq))
  1896. start_cfs_slack_bandwidth(cfs_b);
  1897. }
  1898. raw_spin_unlock(&cfs_b->lock);
  1899. /* even if it's not valid for return we don't want to try again */
  1900. cfs_rq->runtime_remaining -= slack_runtime;
  1901. }
  1902. static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  1903. {
  1904. if (!cfs_bandwidth_used())
  1905. return;
  1906. if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
  1907. return;
  1908. __return_cfs_rq_runtime(cfs_rq);
  1909. }
  1910. /*
  1911. * This is done with a timer (instead of inline with bandwidth return) since
  1912. * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
  1913. */
  1914. static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
  1915. {
  1916. u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
  1917. u64 expires;
  1918. /* confirm we're still not at a refresh boundary */
  1919. if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
  1920. return;
  1921. raw_spin_lock(&cfs_b->lock);
  1922. if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
  1923. runtime = cfs_b->runtime;
  1924. cfs_b->runtime = 0;
  1925. }
  1926. expires = cfs_b->runtime_expires;
  1927. raw_spin_unlock(&cfs_b->lock);
  1928. if (!runtime)
  1929. return;
  1930. runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
  1931. raw_spin_lock(&cfs_b->lock);
  1932. if (expires == cfs_b->runtime_expires)
  1933. cfs_b->runtime = runtime;
  1934. raw_spin_unlock(&cfs_b->lock);
  1935. }
  1936. /*
  1937. * When a group wakes up we want to make sure that its quota is not already
  1938. * expired/exceeded, otherwise it may be allowed to steal additional ticks of
  1939. * runtime as update_curr() throttling can not not trigger until it's on-rq.
  1940. */
  1941. static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
  1942. {
  1943. if (!cfs_bandwidth_used())
  1944. return;
  1945. /* an active group must be handled by the update_curr()->put() path */
  1946. if (!cfs_rq->runtime_enabled || cfs_rq->curr)
  1947. return;
  1948. /* ensure the group is not already throttled */
  1949. if (cfs_rq_throttled(cfs_rq))
  1950. return;
  1951. /* update runtime allocation */
  1952. account_cfs_rq_runtime(cfs_rq, 0);
  1953. if (cfs_rq->runtime_remaining <= 0)
  1954. throttle_cfs_rq(cfs_rq);
  1955. }
  1956. /* conditionally throttle active cfs_rq's from put_prev_entity() */
  1957. static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  1958. {
  1959. if (!cfs_bandwidth_used())
  1960. return;
  1961. if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
  1962. return;
  1963. /*
  1964. * it's possible for a throttled entity to be forced into a running
  1965. * state (e.g. set_curr_task), in this case we're finished.
  1966. */
  1967. if (cfs_rq_throttled(cfs_rq))
  1968. return;
  1969. throttle_cfs_rq(cfs_rq);
  1970. }
  1971. static inline u64 default_cfs_period(void);
  1972. static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
  1973. static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
  1974. static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
  1975. {
  1976. struct cfs_bandwidth *cfs_b =
  1977. container_of(timer, struct cfs_bandwidth, slack_timer);
  1978. do_sched_cfs_slack_timer(cfs_b);
  1979. return HRTIMER_NORESTART;
  1980. }
  1981. static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
  1982. {
  1983. struct cfs_bandwidth *cfs_b =
  1984. container_of(timer, struct cfs_bandwidth, period_timer);
  1985. ktime_t now;
  1986. int overrun;
  1987. int idle = 0;
  1988. for (;;) {
  1989. now = hrtimer_cb_get_time(timer);
  1990. overrun = hrtimer_forward(timer, now, cfs_b->period);
  1991. if (!overrun)
  1992. break;
  1993. idle = do_sched_cfs_period_timer(cfs_b, overrun);
  1994. }
  1995. return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
  1996. }
  1997. void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
  1998. {
  1999. raw_spin_lock_init(&cfs_b->lock);
  2000. cfs_b->runtime = 0;
  2001. cfs_b->quota = RUNTIME_INF;
  2002. cfs_b->period = ns_to_ktime(default_cfs_period());
  2003. INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
  2004. hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2005. cfs_b->period_timer.function = sched_cfs_period_timer;
  2006. hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2007. cfs_b->slack_timer.function = sched_cfs_slack_timer;
  2008. }
  2009. static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  2010. {
  2011. cfs_rq->runtime_enabled = 0;
  2012. INIT_LIST_HEAD(&cfs_rq->throttled_list);
  2013. }
  2014. /* requires cfs_b->lock, may release to reprogram timer */
  2015. void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
  2016. {
  2017. /*
  2018. * The timer may be active because we're trying to set a new bandwidth
  2019. * period or because we're racing with the tear-down path
  2020. * (timer_active==0 becomes visible before the hrtimer call-back
  2021. * terminates). In either case we ensure that it's re-programmed
  2022. */
  2023. while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
  2024. raw_spin_unlock(&cfs_b->lock);
  2025. /* ensure cfs_b->lock is available while we wait */
  2026. hrtimer_cancel(&cfs_b->period_timer);
  2027. raw_spin_lock(&cfs_b->lock);
  2028. /* if someone else restarted the timer then we're done */
  2029. if (cfs_b->timer_active)
  2030. return;
  2031. }
  2032. cfs_b->timer_active = 1;
  2033. start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
  2034. }
  2035. static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
  2036. {
  2037. hrtimer_cancel(&cfs_b->period_timer);
  2038. hrtimer_cancel(&cfs_b->slack_timer);
  2039. }
  2040. static void unthrottle_offline_cfs_rqs(struct rq *rq)
  2041. {
  2042. struct cfs_rq *cfs_rq;
  2043. for_each_leaf_cfs_rq(rq, cfs_rq) {
  2044. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  2045. if (!cfs_rq->runtime_enabled)
  2046. continue;
  2047. /*
  2048. * clock_task is not advancing so we just need to make sure
  2049. * there's some valid quota amount
  2050. */
  2051. cfs_rq->runtime_remaining = cfs_b->quota;
  2052. if (cfs_rq_throttled(cfs_rq))
  2053. unthrottle_cfs_rq(cfs_rq);
  2054. }
  2055. }
  2056. #else /* CONFIG_CFS_BANDWIDTH */
  2057. static __always_inline
  2058. void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) {}
  2059. static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
  2060. static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
  2061. static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
  2062. static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
  2063. {
  2064. return 0;
  2065. }
  2066. static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
  2067. {
  2068. return 0;
  2069. }
  2070. static inline int throttled_lb_pair(struct task_group *tg,
  2071. int src_cpu, int dest_cpu)
  2072. {
  2073. return 0;
  2074. }
  2075. void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
  2076. #ifdef CONFIG_FAIR_GROUP_SCHED
  2077. static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
  2078. #endif
  2079. static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
  2080. {
  2081. return NULL;
  2082. }
  2083. static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
  2084. static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
  2085. #endif /* CONFIG_CFS_BANDWIDTH */
  2086. /**************************************************
  2087. * CFS operations on tasks:
  2088. */
  2089. #ifdef CONFIG_SCHED_HRTICK
  2090. static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  2091. {
  2092. struct sched_entity *se = &p->se;
  2093. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  2094. WARN_ON(task_rq(p) != rq);
  2095. if (cfs_rq->nr_running > 1) {
  2096. u64 slice = sched_slice(cfs_rq, se);
  2097. u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
  2098. s64 delta = slice - ran;
  2099. if (delta < 0) {
  2100. if (rq->curr == p)
  2101. resched_task(p);
  2102. return;
  2103. }
  2104. /*
  2105. * Don't schedule slices shorter than 10000ns, that just
  2106. * doesn't make sense. Rely on vruntime for fairness.
  2107. */
  2108. if (rq->curr != p)
  2109. delta = max_t(s64, 10000LL, delta);
  2110. hrtick_start(rq, delta);
  2111. }
  2112. }
  2113. /*
  2114. * called from enqueue/dequeue and updates the hrtick when the
  2115. * current task is from our class and nr_running is low enough
  2116. * to matter.
  2117. */
  2118. static void hrtick_update(struct rq *rq)
  2119. {
  2120. struct task_struct *curr = rq->curr;
  2121. if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
  2122. return;
  2123. if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
  2124. hrtick_start_fair(rq, curr);
  2125. }
  2126. #else /* !CONFIG_SCHED_HRTICK */
  2127. static inline void
  2128. hrtick_start_fair(struct rq *rq, struct task_struct *p)
  2129. {
  2130. }
  2131. static inline void hrtick_update(struct rq *rq)
  2132. {
  2133. }
  2134. #endif
  2135. /*
  2136. * The enqueue_task method is called before nr_running is
  2137. * increased. Here we update the fair scheduling stats and
  2138. * then put the task into the rbtree:
  2139. */
  2140. static void
  2141. enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
  2142. {
  2143. struct cfs_rq *cfs_rq;
  2144. struct sched_entity *se = &p->se;
  2145. for_each_sched_entity(se) {
  2146. if (se->on_rq)
  2147. break;
  2148. cfs_rq = cfs_rq_of(se);
  2149. enqueue_entity(cfs_rq, se, flags);
  2150. /*
  2151. * end evaluation on encountering a throttled cfs_rq
  2152. *
  2153. * note: in the case of encountering a throttled cfs_rq we will
  2154. * post the final h_nr_running increment below.
  2155. */
  2156. if (cfs_rq_throttled(cfs_rq))
  2157. break;
  2158. cfs_rq->h_nr_running++;
  2159. flags = ENQUEUE_WAKEUP;
  2160. }
  2161. for_each_sched_entity(se) {
  2162. cfs_rq = cfs_rq_of(se);
  2163. cfs_rq->h_nr_running++;
  2164. if (cfs_rq_throttled(cfs_rq))
  2165. break;
  2166. update_cfs_load(cfs_rq, 0);
  2167. update_cfs_shares(cfs_rq);
  2168. update_entity_load_avg(se, 1);
  2169. }
  2170. if (!se) {
  2171. update_rq_runnable_avg(rq, rq->nr_running);
  2172. inc_nr_running(rq);
  2173. }
  2174. hrtick_update(rq);
  2175. }
  2176. static void set_next_buddy(struct sched_entity *se);
  2177. /*
  2178. * The dequeue_task method is called before nr_running is
  2179. * decreased. We remove the task from the rbtree and
  2180. * update the fair scheduling stats:
  2181. */
  2182. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
  2183. {
  2184. struct cfs_rq *cfs_rq;
  2185. struct sched_entity *se = &p->se;
  2186. int task_sleep = flags & DEQUEUE_SLEEP;
  2187. for_each_sched_entity(se) {
  2188. cfs_rq = cfs_rq_of(se);
  2189. dequeue_entity(cfs_rq, se, flags);
  2190. /*
  2191. * end evaluation on encountering a throttled cfs_rq
  2192. *
  2193. * note: in the case of encountering a throttled cfs_rq we will
  2194. * post the final h_nr_running decrement below.
  2195. */
  2196. if (cfs_rq_throttled(cfs_rq))
  2197. break;
  2198. cfs_rq->h_nr_running--;
  2199. /* Don't dequeue parent if it has other entities besides us */
  2200. if (cfs_rq->load.weight) {
  2201. /*
  2202. * Bias pick_next to pick a task from this cfs_rq, as
  2203. * p is sleeping when it is within its sched_slice.
  2204. */
  2205. if (task_sleep && parent_entity(se))
  2206. set_next_buddy(parent_entity(se));
  2207. /* avoid re-evaluating load for this entity */
  2208. se = parent_entity(se);
  2209. break;
  2210. }
  2211. flags |= DEQUEUE_SLEEP;
  2212. }
  2213. for_each_sched_entity(se) {
  2214. cfs_rq = cfs_rq_of(se);
  2215. cfs_rq->h_nr_running--;
  2216. if (cfs_rq_throttled(cfs_rq))
  2217. break;
  2218. update_cfs_load(cfs_rq, 0);
  2219. update_cfs_shares(cfs_rq);
  2220. update_entity_load_avg(se, 1);
  2221. }
  2222. if (!se) {
  2223. dec_nr_running(rq);
  2224. update_rq_runnable_avg(rq, 1);
  2225. }
  2226. hrtick_update(rq);
  2227. }
  2228. #ifdef CONFIG_SMP
  2229. /* Used instead of source_load when we know the type == 0 */
  2230. static unsigned long weighted_cpuload(const int cpu)
  2231. {
  2232. return cpu_rq(cpu)->load.weight;
  2233. }
  2234. /*
  2235. * Return a low guess at the load of a migration-source cpu weighted
  2236. * according to the scheduling class and "nice" value.
  2237. *
  2238. * We want to under-estimate the load of migration sources, to
  2239. * balance conservatively.
  2240. */
  2241. static unsigned long source_load(int cpu, int type)
  2242. {
  2243. struct rq *rq = cpu_rq(cpu);
  2244. unsigned long total = weighted_cpuload(cpu);
  2245. if (type == 0 || !sched_feat(LB_BIAS))
  2246. return total;
  2247. return min(rq->cpu_load[type-1], total);
  2248. }
  2249. /*
  2250. * Return a high guess at the load of a migration-target cpu weighted
  2251. * according to the scheduling class and "nice" value.
  2252. */
  2253. static unsigned long target_load(int cpu, int type)
  2254. {
  2255. struct rq *rq = cpu_rq(cpu);
  2256. unsigned long total = weighted_cpuload(cpu);
  2257. if (type == 0 || !sched_feat(LB_BIAS))
  2258. return total;
  2259. return max(rq->cpu_load[type-1], total);
  2260. }
  2261. static unsigned long power_of(int cpu)
  2262. {
  2263. return cpu_rq(cpu)->cpu_power;
  2264. }
  2265. static unsigned long cpu_avg_load_per_task(int cpu)
  2266. {
  2267. struct rq *rq = cpu_rq(cpu);
  2268. unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
  2269. if (nr_running)
  2270. return rq->load.weight / nr_running;
  2271. return 0;
  2272. }
  2273. static void task_waking_fair(struct task_struct *p)
  2274. {
  2275. struct sched_entity *se = &p->se;
  2276. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  2277. u64 min_vruntime;
  2278. #ifndef CONFIG_64BIT
  2279. u64 min_vruntime_copy;
  2280. do {
  2281. min_vruntime_copy = cfs_rq->min_vruntime_copy;
  2282. smp_rmb();
  2283. min_vruntime = cfs_rq->min_vruntime;
  2284. } while (min_vruntime != min_vruntime_copy);
  2285. #else
  2286. min_vruntime = cfs_rq->min_vruntime;
  2287. #endif
  2288. se->vruntime -= min_vruntime;
  2289. }
  2290. #ifdef CONFIG_FAIR_GROUP_SCHED
  2291. /*
  2292. * effective_load() calculates the load change as seen from the root_task_group
  2293. *
  2294. * Adding load to a group doesn't make a group heavier, but can cause movement
  2295. * of group shares between cpus. Assuming the shares were perfectly aligned one
  2296. * can calculate the shift in shares.
  2297. *
  2298. * Calculate the effective load difference if @wl is added (subtracted) to @tg
  2299. * on this @cpu and results in a total addition (subtraction) of @wg to the
  2300. * total group weight.
  2301. *
  2302. * Given a runqueue weight distribution (rw_i) we can compute a shares
  2303. * distribution (s_i) using:
  2304. *
  2305. * s_i = rw_i / \Sum rw_j (1)
  2306. *
  2307. * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
  2308. * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
  2309. * shares distribution (s_i):
  2310. *
  2311. * rw_i = { 2, 4, 1, 0 }
  2312. * s_i = { 2/7, 4/7, 1/7, 0 }
  2313. *
  2314. * As per wake_affine() we're interested in the load of two CPUs (the CPU the
  2315. * task used to run on and the CPU the waker is running on), we need to
  2316. * compute the effect of waking a task on either CPU and, in case of a sync
  2317. * wakeup, compute the effect of the current task going to sleep.
  2318. *
  2319. * So for a change of @wl to the local @cpu with an overall group weight change
  2320. * of @wl we can compute the new shares distribution (s'_i) using:
  2321. *
  2322. * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
  2323. *
  2324. * Suppose we're interested in CPUs 0 and 1, and want to compute the load
  2325. * differences in waking a task to CPU 0. The additional task changes the
  2326. * weight and shares distributions like:
  2327. *
  2328. * rw'_i = { 3, 4, 1, 0 }
  2329. * s'_i = { 3/8, 4/8, 1/8, 0 }
  2330. *
  2331. * We can then compute the difference in effective weight by using:
  2332. *
  2333. * dw_i = S * (s'_i - s_i) (3)
  2334. *
  2335. * Where 'S' is the group weight as seen by its parent.
  2336. *
  2337. * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
  2338. * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
  2339. * 4/7) times the weight of the group.
  2340. */
  2341. static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
  2342. {
  2343. struct sched_entity *se = tg->se[cpu];
  2344. if (!tg->parent) /* the trivial, non-cgroup case */
  2345. return wl;
  2346. for_each_sched_entity(se) {
  2347. long w, W;
  2348. tg = se->my_q->tg;
  2349. /*
  2350. * W = @wg + \Sum rw_j
  2351. */
  2352. W = wg + calc_tg_weight(tg, se->my_q);
  2353. /*
  2354. * w = rw_i + @wl
  2355. */
  2356. w = se->my_q->load.weight + wl;
  2357. /*
  2358. * wl = S * s'_i; see (2)
  2359. */
  2360. if (W > 0 && w < W)
  2361. wl = (w * tg->shares) / W;
  2362. else
  2363. wl = tg->shares;
  2364. /*
  2365. * Per the above, wl is the new se->load.weight value; since
  2366. * those are clipped to [MIN_SHARES, ...) do so now. See
  2367. * calc_cfs_shares().
  2368. */
  2369. if (wl < MIN_SHARES)
  2370. wl = MIN_SHARES;
  2371. /*
  2372. * wl = dw_i = S * (s'_i - s_i); see (3)
  2373. */
  2374. wl -= se->load.weight;
  2375. /*
  2376. * Recursively apply this logic to all parent groups to compute
  2377. * the final effective load change on the root group. Since
  2378. * only the @tg group gets extra weight, all parent groups can
  2379. * only redistribute existing shares. @wl is the shift in shares
  2380. * resulting from this level per the above.
  2381. */
  2382. wg = 0;
  2383. }
  2384. return wl;
  2385. }
  2386. #else
  2387. static inline unsigned long effective_load(struct task_group *tg, int cpu,
  2388. unsigned long wl, unsigned long wg)
  2389. {
  2390. return wl;
  2391. }
  2392. #endif
  2393. static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
  2394. {
  2395. s64 this_load, load;
  2396. int idx, this_cpu, prev_cpu;
  2397. unsigned long tl_per_task;
  2398. struct task_group *tg;
  2399. unsigned long weight;
  2400. int balanced;
  2401. idx = sd->wake_idx;
  2402. this_cpu = smp_processor_id();
  2403. prev_cpu = task_cpu(p);
  2404. load = source_load(prev_cpu, idx);
  2405. this_load = target_load(this_cpu, idx);
  2406. /*
  2407. * If sync wakeup then subtract the (maximum possible)
  2408. * effect of the currently running task from the load
  2409. * of the current CPU:
  2410. */
  2411. if (sync) {
  2412. tg = task_group(current);
  2413. weight = current->se.load.weight;
  2414. this_load += effective_load(tg, this_cpu, -weight, -weight);
  2415. load += effective_load(tg, prev_cpu, 0, -weight);
  2416. }
  2417. tg = task_group(p);
  2418. weight = p->se.load.weight;
  2419. /*
  2420. * In low-load situations, where prev_cpu is idle and this_cpu is idle
  2421. * due to the sync cause above having dropped this_load to 0, we'll
  2422. * always have an imbalance, but there's really nothing you can do
  2423. * about that, so that's good too.
  2424. *
  2425. * Otherwise check if either cpus are near enough in load to allow this
  2426. * task to be woken on this_cpu.
  2427. */
  2428. if (this_load > 0) {
  2429. s64 this_eff_load, prev_eff_load;
  2430. this_eff_load = 100;
  2431. this_eff_load *= power_of(prev_cpu);
  2432. this_eff_load *= this_load +
  2433. effective_load(tg, this_cpu, weight, weight);
  2434. prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
  2435. prev_eff_load *= power_of(this_cpu);
  2436. prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
  2437. balanced = this_eff_load <= prev_eff_load;
  2438. } else
  2439. balanced = true;
  2440. /*
  2441. * If the currently running task will sleep within
  2442. * a reasonable amount of time then attract this newly
  2443. * woken task:
  2444. */
  2445. if (sync && balanced)
  2446. return 1;
  2447. schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
  2448. tl_per_task = cpu_avg_load_per_task(this_cpu);
  2449. if (balanced ||
  2450. (this_load <= load &&
  2451. this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
  2452. /*
  2453. * This domain has SD_WAKE_AFFINE and
  2454. * p is cache cold in this domain, and
  2455. * there is no bad imbalance.
  2456. */
  2457. schedstat_inc(sd, ttwu_move_affine);
  2458. schedstat_inc(p, se.statistics.nr_wakeups_affine);
  2459. return 1;
  2460. }
  2461. return 0;
  2462. }
  2463. /*
  2464. * find_idlest_group finds and returns the least busy CPU group within the
  2465. * domain.
  2466. */
  2467. static struct sched_group *
  2468. find_idlest_group(struct sched_domain *sd, struct task_struct *p,
  2469. int this_cpu, int load_idx)
  2470. {
  2471. struct sched_group *idlest = NULL, *group = sd->groups;
  2472. unsigned long min_load = ULONG_MAX, this_load = 0;
  2473. int imbalance = 100 + (sd->imbalance_pct-100)/2;
  2474. do {
  2475. unsigned long load, avg_load;
  2476. int local_group;
  2477. int i;
  2478. /* Skip over this group if it has no CPUs allowed */
  2479. if (!cpumask_intersects(sched_group_cpus(group),
  2480. tsk_cpus_allowed(p)))
  2481. continue;
  2482. local_group = cpumask_test_cpu(this_cpu,
  2483. sched_group_cpus(group));
  2484. /* Tally up the load of all CPUs in the group */
  2485. avg_load = 0;
  2486. for_each_cpu(i, sched_group_cpus(group)) {
  2487. /* Bias balancing toward cpus of our domain */
  2488. if (local_group)
  2489. load = source_load(i, load_idx);
  2490. else
  2491. load = target_load(i, load_idx);
  2492. avg_load += load;
  2493. }
  2494. /* Adjust by relative CPU power of the group */
  2495. avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
  2496. if (local_group) {
  2497. this_load = avg_load;
  2498. } else if (avg_load < min_load) {
  2499. min_load = avg_load;
  2500. idlest = group;
  2501. }
  2502. } while (group = group->next, group != sd->groups);
  2503. if (!idlest || 100*this_load < imbalance*min_load)
  2504. return NULL;
  2505. return idlest;
  2506. }
  2507. /*
  2508. * find_idlest_cpu - find the idlest cpu among the cpus in group.
  2509. */
  2510. static int
  2511. find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
  2512. {
  2513. unsigned long load, min_load = ULONG_MAX;
  2514. int idlest = -1;
  2515. int i;
  2516. /* Traverse only the allowed CPUs */
  2517. for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
  2518. load = weighted_cpuload(i);
  2519. if (load < min_load || (load == min_load && i == this_cpu)) {
  2520. min_load = load;
  2521. idlest = i;
  2522. }
  2523. }
  2524. return idlest;
  2525. }
  2526. /*
  2527. * Try and locate an idle CPU in the sched_domain.
  2528. */
  2529. static int select_idle_sibling(struct task_struct *p, int target)
  2530. {
  2531. int cpu = smp_processor_id();
  2532. int prev_cpu = task_cpu(p);
  2533. struct sched_domain *sd;
  2534. struct sched_group *sg;
  2535. int i;
  2536. /*
  2537. * If the task is going to be woken-up on this cpu and if it is
  2538. * already idle, then it is the right target.
  2539. */
  2540. if (target == cpu && idle_cpu(cpu))
  2541. return cpu;
  2542. /*
  2543. * If the task is going to be woken-up on the cpu where it previously
  2544. * ran and if it is currently idle, then it the right target.
  2545. */
  2546. if (target == prev_cpu && idle_cpu(prev_cpu))
  2547. return prev_cpu;
  2548. /*
  2549. * Otherwise, iterate the domains and find an elegible idle cpu.
  2550. */
  2551. sd = rcu_dereference(per_cpu(sd_llc, target));
  2552. for_each_lower_domain(sd) {
  2553. sg = sd->groups;
  2554. do {
  2555. if (!cpumask_intersects(sched_group_cpus(sg),
  2556. tsk_cpus_allowed(p)))
  2557. goto next;
  2558. for_each_cpu(i, sched_group_cpus(sg)) {
  2559. if (!idle_cpu(i))
  2560. goto next;
  2561. }
  2562. target = cpumask_first_and(sched_group_cpus(sg),
  2563. tsk_cpus_allowed(p));
  2564. goto done;
  2565. next:
  2566. sg = sg->next;
  2567. } while (sg != sd->groups);
  2568. }
  2569. done:
  2570. return target;
  2571. }
  2572. /*
  2573. * sched_balance_self: balance the current task (running on cpu) in domains
  2574. * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
  2575. * SD_BALANCE_EXEC.
  2576. *
  2577. * Balance, ie. select the least loaded group.
  2578. *
  2579. * Returns the target CPU number, or the same CPU if no balancing is needed.
  2580. *
  2581. * preempt must be disabled.
  2582. */
  2583. static int
  2584. select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
  2585. {
  2586. struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
  2587. int cpu = smp_processor_id();
  2588. int prev_cpu = task_cpu(p);
  2589. int new_cpu = cpu;
  2590. int want_affine = 0;
  2591. int sync = wake_flags & WF_SYNC;
  2592. if (p->nr_cpus_allowed == 1)
  2593. return prev_cpu;
  2594. if (sd_flag & SD_BALANCE_WAKE) {
  2595. if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
  2596. want_affine = 1;
  2597. new_cpu = prev_cpu;
  2598. }
  2599. rcu_read_lock();
  2600. for_each_domain(cpu, tmp) {
  2601. if (!(tmp->flags & SD_LOAD_BALANCE))
  2602. continue;
  2603. /*
  2604. * If both cpu and prev_cpu are part of this domain,
  2605. * cpu is a valid SD_WAKE_AFFINE target.
  2606. */
  2607. if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
  2608. cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
  2609. affine_sd = tmp;
  2610. break;
  2611. }
  2612. if (tmp->flags & sd_flag)
  2613. sd = tmp;
  2614. }
  2615. if (affine_sd) {
  2616. if (cpu != prev_cpu && wake_affine(affine_sd, p, sync))
  2617. prev_cpu = cpu;
  2618. new_cpu = select_idle_sibling(p, prev_cpu);
  2619. goto unlock;
  2620. }
  2621. while (sd) {
  2622. int load_idx = sd->forkexec_idx;
  2623. struct sched_group *group;
  2624. int weight;
  2625. if (!(sd->flags & sd_flag)) {
  2626. sd = sd->child;
  2627. continue;
  2628. }
  2629. if (sd_flag & SD_BALANCE_WAKE)
  2630. load_idx = sd->wake_idx;
  2631. group = find_idlest_group(sd, p, cpu, load_idx);
  2632. if (!group) {
  2633. sd = sd->child;
  2634. continue;
  2635. }
  2636. new_cpu = find_idlest_cpu(group, p, cpu);
  2637. if (new_cpu == -1 || new_cpu == cpu) {
  2638. /* Now try balancing at a lower domain level of cpu */
  2639. sd = sd->child;
  2640. continue;
  2641. }
  2642. /* Now try balancing at a lower domain level of new_cpu */
  2643. cpu = new_cpu;
  2644. weight = sd->span_weight;
  2645. sd = NULL;
  2646. for_each_domain(cpu, tmp) {
  2647. if (weight <= tmp->span_weight)
  2648. break;
  2649. if (tmp->flags & sd_flag)
  2650. sd = tmp;
  2651. }
  2652. /* while loop will break here if sd == NULL */
  2653. }
  2654. unlock:
  2655. rcu_read_unlock();
  2656. return new_cpu;
  2657. }
  2658. /*
  2659. * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
  2660. * cfs_rq_of(p) references at time of call are still valid and identify the
  2661. * previous cpu. However, the caller only guarantees p->pi_lock is held; no
  2662. * other assumptions, including the state of rq->lock, should be made.
  2663. */
  2664. static void
  2665. migrate_task_rq_fair(struct task_struct *p, int next_cpu)
  2666. {
  2667. struct sched_entity *se = &p->se;
  2668. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  2669. /*
  2670. * Load tracking: accumulate removed load so that it can be processed
  2671. * when we next update owning cfs_rq under rq->lock. Tasks contribute
  2672. * to blocked load iff they have a positive decay-count. It can never
  2673. * be negative here since on-rq tasks have decay-count == 0.
  2674. */
  2675. if (se->avg.decay_count) {
  2676. se->avg.decay_count = -__synchronize_entity_decay(se);
  2677. atomic64_add(se->avg.load_avg_contrib, &cfs_rq->removed_load);
  2678. }
  2679. }
  2680. #endif /* CONFIG_SMP */
  2681. static unsigned long
  2682. wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
  2683. {
  2684. unsigned long gran = sysctl_sched_wakeup_granularity;
  2685. /*
  2686. * Since its curr running now, convert the gran from real-time
  2687. * to virtual-time in his units.
  2688. *
  2689. * By using 'se' instead of 'curr' we penalize light tasks, so
  2690. * they get preempted easier. That is, if 'se' < 'curr' then
  2691. * the resulting gran will be larger, therefore penalizing the
  2692. * lighter, if otoh 'se' > 'curr' then the resulting gran will
  2693. * be smaller, again penalizing the lighter task.
  2694. *
  2695. * This is especially important for buddies when the leftmost
  2696. * task is higher priority than the buddy.
  2697. */
  2698. return calc_delta_fair(gran, se);
  2699. }
  2700. /*
  2701. * Should 'se' preempt 'curr'.
  2702. *
  2703. * |s1
  2704. * |s2
  2705. * |s3
  2706. * g
  2707. * |<--->|c
  2708. *
  2709. * w(c, s1) = -1
  2710. * w(c, s2) = 0
  2711. * w(c, s3) = 1
  2712. *
  2713. */
  2714. static int
  2715. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
  2716. {
  2717. s64 gran, vdiff = curr->vruntime - se->vruntime;
  2718. if (vdiff <= 0)
  2719. return -1;
  2720. gran = wakeup_gran(curr, se);
  2721. if (vdiff > gran)
  2722. return 1;
  2723. return 0;
  2724. }
  2725. static void set_last_buddy(struct sched_entity *se)
  2726. {
  2727. if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
  2728. return;
  2729. for_each_sched_entity(se)
  2730. cfs_rq_of(se)->last = se;
  2731. }
  2732. static void set_next_buddy(struct sched_entity *se)
  2733. {
  2734. if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
  2735. return;
  2736. for_each_sched_entity(se)
  2737. cfs_rq_of(se)->next = se;
  2738. }
  2739. static void set_skip_buddy(struct sched_entity *se)
  2740. {
  2741. for_each_sched_entity(se)
  2742. cfs_rq_of(se)->skip = se;
  2743. }
  2744. /*
  2745. * Preempt the current task with a newly woken task if needed:
  2746. */
  2747. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
  2748. {
  2749. struct task_struct *curr = rq->curr;
  2750. struct sched_entity *se = &curr->se, *pse = &p->se;
  2751. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  2752. int scale = cfs_rq->nr_running >= sched_nr_latency;
  2753. int next_buddy_marked = 0;
  2754. if (unlikely(se == pse))
  2755. return;
  2756. /*
  2757. * This is possible from callers such as move_task(), in which we
  2758. * unconditionally check_prempt_curr() after an enqueue (which may have
  2759. * lead to a throttle). This both saves work and prevents false
  2760. * next-buddy nomination below.
  2761. */
  2762. if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
  2763. return;
  2764. if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
  2765. set_next_buddy(pse);
  2766. next_buddy_marked = 1;
  2767. }
  2768. /*
  2769. * We can come here with TIF_NEED_RESCHED already set from new task
  2770. * wake up path.
  2771. *
  2772. * Note: this also catches the edge-case of curr being in a throttled
  2773. * group (e.g. via set_curr_task), since update_curr() (in the
  2774. * enqueue of curr) will have resulted in resched being set. This
  2775. * prevents us from potentially nominating it as a false LAST_BUDDY
  2776. * below.
  2777. */
  2778. if (test_tsk_need_resched(curr))
  2779. return;
  2780. /* Idle tasks are by definition preempted by non-idle tasks. */
  2781. if (unlikely(curr->policy == SCHED_IDLE) &&
  2782. likely(p->policy != SCHED_IDLE))
  2783. goto preempt;
  2784. /*
  2785. * Batch and idle tasks do not preempt non-idle tasks (their preemption
  2786. * is driven by the tick):
  2787. */
  2788. if (unlikely(p->policy != SCHED_NORMAL))
  2789. return;
  2790. find_matching_se(&se, &pse);
  2791. update_curr(cfs_rq_of(se));
  2792. BUG_ON(!pse);
  2793. if (wakeup_preempt_entity(se, pse) == 1) {
  2794. /*
  2795. * Bias pick_next to pick the sched entity that is
  2796. * triggering this preemption.
  2797. */
  2798. if (!next_buddy_marked)
  2799. set_next_buddy(pse);
  2800. goto preempt;
  2801. }
  2802. return;
  2803. preempt:
  2804. resched_task(curr);
  2805. /*
  2806. * Only set the backward buddy when the current task is still
  2807. * on the rq. This can happen when a wakeup gets interleaved
  2808. * with schedule on the ->pre_schedule() or idle_balance()
  2809. * point, either of which can * drop the rq lock.
  2810. *
  2811. * Also, during early boot the idle thread is in the fair class,
  2812. * for obvious reasons its a bad idea to schedule back to it.
  2813. */
  2814. if (unlikely(!se->on_rq || curr == rq->idle))
  2815. return;
  2816. if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
  2817. set_last_buddy(se);
  2818. }
  2819. static struct task_struct *pick_next_task_fair(struct rq *rq)
  2820. {
  2821. struct task_struct *p;
  2822. struct cfs_rq *cfs_rq = &rq->cfs;
  2823. struct sched_entity *se;
  2824. if (!cfs_rq->nr_running)
  2825. return NULL;
  2826. do {
  2827. se = pick_next_entity(cfs_rq);
  2828. set_next_entity(cfs_rq, se);
  2829. cfs_rq = group_cfs_rq(se);
  2830. } while (cfs_rq);
  2831. p = task_of(se);
  2832. if (hrtick_enabled(rq))
  2833. hrtick_start_fair(rq, p);
  2834. return p;
  2835. }
  2836. /*
  2837. * Account for a descheduled task:
  2838. */
  2839. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  2840. {
  2841. struct sched_entity *se = &prev->se;
  2842. struct cfs_rq *cfs_rq;
  2843. for_each_sched_entity(se) {
  2844. cfs_rq = cfs_rq_of(se);
  2845. put_prev_entity(cfs_rq, se);
  2846. }
  2847. }
  2848. /*
  2849. * sched_yield() is very simple
  2850. *
  2851. * The magic of dealing with the ->skip buddy is in pick_next_entity.
  2852. */
  2853. static void yield_task_fair(struct rq *rq)
  2854. {
  2855. struct task_struct *curr = rq->curr;
  2856. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  2857. struct sched_entity *se = &curr->se;
  2858. /*
  2859. * Are we the only task in the tree?
  2860. */
  2861. if (unlikely(rq->nr_running == 1))
  2862. return;
  2863. clear_buddies(cfs_rq, se);
  2864. if (curr->policy != SCHED_BATCH) {
  2865. update_rq_clock(rq);
  2866. /*
  2867. * Update run-time statistics of the 'current'.
  2868. */
  2869. update_curr(cfs_rq);
  2870. /*
  2871. * Tell update_rq_clock() that we've just updated,
  2872. * so we don't do microscopic update in schedule()
  2873. * and double the fastpath cost.
  2874. */
  2875. rq->skip_clock_update = 1;
  2876. }
  2877. set_skip_buddy(se);
  2878. }
  2879. static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
  2880. {
  2881. struct sched_entity *se = &p->se;
  2882. /* throttled hierarchies are not runnable */
  2883. if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
  2884. return false;
  2885. /* Tell the scheduler that we'd really like pse to run next. */
  2886. set_next_buddy(se);
  2887. yield_task_fair(rq);
  2888. return true;
  2889. }
  2890. #ifdef CONFIG_SMP
  2891. /**************************************************
  2892. * Fair scheduling class load-balancing methods:
  2893. */
  2894. static unsigned long __read_mostly max_load_balance_interval = HZ/10;
  2895. #define LBF_ALL_PINNED 0x01
  2896. #define LBF_NEED_BREAK 0x02
  2897. #define LBF_SOME_PINNED 0x04
  2898. struct lb_env {
  2899. struct sched_domain *sd;
  2900. struct rq *src_rq;
  2901. int src_cpu;
  2902. int dst_cpu;
  2903. struct rq *dst_rq;
  2904. struct cpumask *dst_grpmask;
  2905. int new_dst_cpu;
  2906. enum cpu_idle_type idle;
  2907. long imbalance;
  2908. /* The set of CPUs under consideration for load-balancing */
  2909. struct cpumask *cpus;
  2910. unsigned int flags;
  2911. unsigned int loop;
  2912. unsigned int loop_break;
  2913. unsigned int loop_max;
  2914. };
  2915. /*
  2916. * move_task - move a task from one runqueue to another runqueue.
  2917. * Both runqueues must be locked.
  2918. */
  2919. static void move_task(struct task_struct *p, struct lb_env *env)
  2920. {
  2921. deactivate_task(env->src_rq, p, 0);
  2922. set_task_cpu(p, env->dst_cpu);
  2923. activate_task(env->dst_rq, p, 0);
  2924. check_preempt_curr(env->dst_rq, p, 0);
  2925. }
  2926. /*
  2927. * Is this task likely cache-hot:
  2928. */
  2929. static int
  2930. task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
  2931. {
  2932. s64 delta;
  2933. if (p->sched_class != &fair_sched_class)
  2934. return 0;
  2935. if (unlikely(p->policy == SCHED_IDLE))
  2936. return 0;
  2937. /*
  2938. * Buddy candidates are cache hot:
  2939. */
  2940. if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
  2941. (&p->se == cfs_rq_of(&p->se)->next ||
  2942. &p->se == cfs_rq_of(&p->se)->last))
  2943. return 1;
  2944. if (sysctl_sched_migration_cost == -1)
  2945. return 1;
  2946. if (sysctl_sched_migration_cost == 0)
  2947. return 0;
  2948. delta = now - p->se.exec_start;
  2949. return delta < (s64)sysctl_sched_migration_cost;
  2950. }
  2951. /*
  2952. * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
  2953. */
  2954. static
  2955. int can_migrate_task(struct task_struct *p, struct lb_env *env)
  2956. {
  2957. int tsk_cache_hot = 0;
  2958. /*
  2959. * We do not migrate tasks that are:
  2960. * 1) running (obviously), or
  2961. * 2) cannot be migrated to this CPU due to cpus_allowed, or
  2962. * 3) are cache-hot on their current CPU.
  2963. */
  2964. if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
  2965. int new_dst_cpu;
  2966. schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
  2967. /*
  2968. * Remember if this task can be migrated to any other cpu in
  2969. * our sched_group. We may want to revisit it if we couldn't
  2970. * meet load balance goals by pulling other tasks on src_cpu.
  2971. *
  2972. * Also avoid computing new_dst_cpu if we have already computed
  2973. * one in current iteration.
  2974. */
  2975. if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED))
  2976. return 0;
  2977. new_dst_cpu = cpumask_first_and(env->dst_grpmask,
  2978. tsk_cpus_allowed(p));
  2979. if (new_dst_cpu < nr_cpu_ids) {
  2980. env->flags |= LBF_SOME_PINNED;
  2981. env->new_dst_cpu = new_dst_cpu;
  2982. }
  2983. return 0;
  2984. }
  2985. /* Record that we found atleast one task that could run on dst_cpu */
  2986. env->flags &= ~LBF_ALL_PINNED;
  2987. if (task_running(env->src_rq, p)) {
  2988. schedstat_inc(p, se.statistics.nr_failed_migrations_running);
  2989. return 0;
  2990. }
  2991. /*
  2992. * Aggressive migration if:
  2993. * 1) task is cache cold, or
  2994. * 2) too many balance attempts have failed.
  2995. */
  2996. tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
  2997. if (!tsk_cache_hot ||
  2998. env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
  2999. #ifdef CONFIG_SCHEDSTATS
  3000. if (tsk_cache_hot) {
  3001. schedstat_inc(env->sd, lb_hot_gained[env->idle]);
  3002. schedstat_inc(p, se.statistics.nr_forced_migrations);
  3003. }
  3004. #endif
  3005. return 1;
  3006. }
  3007. if (tsk_cache_hot) {
  3008. schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
  3009. return 0;
  3010. }
  3011. return 1;
  3012. }
  3013. /*
  3014. * move_one_task tries to move exactly one task from busiest to this_rq, as
  3015. * part of active balancing operations within "domain".
  3016. * Returns 1 if successful and 0 otherwise.
  3017. *
  3018. * Called with both runqueues locked.
  3019. */
  3020. static int move_one_task(struct lb_env *env)
  3021. {
  3022. struct task_struct *p, *n;
  3023. list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
  3024. if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
  3025. continue;
  3026. if (!can_migrate_task(p, env))
  3027. continue;
  3028. move_task(p, env);
  3029. /*
  3030. * Right now, this is only the second place move_task()
  3031. * is called, so we can safely collect move_task()
  3032. * stats here rather than inside move_task().
  3033. */
  3034. schedstat_inc(env->sd, lb_gained[env->idle]);
  3035. return 1;
  3036. }
  3037. return 0;
  3038. }
  3039. static unsigned long task_h_load(struct task_struct *p);
  3040. static const unsigned int sched_nr_migrate_break = 32;
  3041. /*
  3042. * move_tasks tries to move up to imbalance weighted load from busiest to
  3043. * this_rq, as part of a balancing operation within domain "sd".
  3044. * Returns 1 if successful and 0 otherwise.
  3045. *
  3046. * Called with both runqueues locked.
  3047. */
  3048. static int move_tasks(struct lb_env *env)
  3049. {
  3050. struct list_head *tasks = &env->src_rq->cfs_tasks;
  3051. struct task_struct *p;
  3052. unsigned long load;
  3053. int pulled = 0;
  3054. if (env->imbalance <= 0)
  3055. return 0;
  3056. while (!list_empty(tasks)) {
  3057. p = list_first_entry(tasks, struct task_struct, se.group_node);
  3058. env->loop++;
  3059. /* We've more or less seen every task there is, call it quits */
  3060. if (env->loop > env->loop_max)
  3061. break;
  3062. /* take a breather every nr_migrate tasks */
  3063. if (env->loop > env->loop_break) {
  3064. env->loop_break += sched_nr_migrate_break;
  3065. env->flags |= LBF_NEED_BREAK;
  3066. break;
  3067. }
  3068. if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
  3069. goto next;
  3070. load = task_h_load(p);
  3071. if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
  3072. goto next;
  3073. if ((load / 2) > env->imbalance)
  3074. goto next;
  3075. if (!can_migrate_task(p, env))
  3076. goto next;
  3077. move_task(p, env);
  3078. pulled++;
  3079. env->imbalance -= load;
  3080. #ifdef CONFIG_PREEMPT
  3081. /*
  3082. * NEWIDLE balancing is a source of latency, so preemptible
  3083. * kernels will stop after the first task is pulled to minimize
  3084. * the critical section.
  3085. */
  3086. if (env->idle == CPU_NEWLY_IDLE)
  3087. break;
  3088. #endif
  3089. /*
  3090. * We only want to steal up to the prescribed amount of
  3091. * weighted load.
  3092. */
  3093. if (env->imbalance <= 0)
  3094. break;
  3095. continue;
  3096. next:
  3097. list_move_tail(&p->se.group_node, tasks);
  3098. }
  3099. /*
  3100. * Right now, this is one of only two places move_task() is called,
  3101. * so we can safely collect move_task() stats here rather than
  3102. * inside move_task().
  3103. */
  3104. schedstat_add(env->sd, lb_gained[env->idle], pulled);
  3105. return pulled;
  3106. }
  3107. #ifdef CONFIG_FAIR_GROUP_SCHED
  3108. /*
  3109. * update tg->load_weight by folding this cpu's load_avg
  3110. */
  3111. static int update_shares_cpu(struct task_group *tg, int cpu)
  3112. {
  3113. struct cfs_rq *cfs_rq;
  3114. unsigned long flags;
  3115. struct rq *rq;
  3116. if (!tg->se[cpu])
  3117. return 0;
  3118. rq = cpu_rq(cpu);
  3119. cfs_rq = tg->cfs_rq[cpu];
  3120. raw_spin_lock_irqsave(&rq->lock, flags);
  3121. update_rq_clock(rq);
  3122. update_cfs_load(cfs_rq, 1);
  3123. update_cfs_rq_blocked_load(cfs_rq, 1);
  3124. /*
  3125. * We need to update shares after updating tg->load_weight in
  3126. * order to adjust the weight of groups with long running tasks.
  3127. */
  3128. update_cfs_shares(cfs_rq);
  3129. raw_spin_unlock_irqrestore(&rq->lock, flags);
  3130. return 0;
  3131. }
  3132. static void update_shares(int cpu)
  3133. {
  3134. struct cfs_rq *cfs_rq;
  3135. struct rq *rq = cpu_rq(cpu);
  3136. rcu_read_lock();
  3137. /*
  3138. * Iterates the task_group tree in a bottom up fashion, see
  3139. * list_add_leaf_cfs_rq() for details.
  3140. */
  3141. for_each_leaf_cfs_rq(rq, cfs_rq) {
  3142. /* throttled entities do not contribute to load */
  3143. if (throttled_hierarchy(cfs_rq))
  3144. continue;
  3145. update_shares_cpu(cfs_rq->tg, cpu);
  3146. }
  3147. rcu_read_unlock();
  3148. }
  3149. /*
  3150. * Compute the cpu's hierarchical load factor for each task group.
  3151. * This needs to be done in a top-down fashion because the load of a child
  3152. * group is a fraction of its parents load.
  3153. */
  3154. static int tg_load_down(struct task_group *tg, void *data)
  3155. {
  3156. unsigned long load;
  3157. long cpu = (long)data;
  3158. if (!tg->parent) {
  3159. load = cpu_rq(cpu)->load.weight;
  3160. } else {
  3161. load = tg->parent->cfs_rq[cpu]->h_load;
  3162. load *= tg->se[cpu]->load.weight;
  3163. load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
  3164. }
  3165. tg->cfs_rq[cpu]->h_load = load;
  3166. return 0;
  3167. }
  3168. static void update_h_load(long cpu)
  3169. {
  3170. struct rq *rq = cpu_rq(cpu);
  3171. unsigned long now = jiffies;
  3172. if (rq->h_load_throttle == now)
  3173. return;
  3174. rq->h_load_throttle = now;
  3175. rcu_read_lock();
  3176. walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
  3177. rcu_read_unlock();
  3178. }
  3179. static unsigned long task_h_load(struct task_struct *p)
  3180. {
  3181. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  3182. unsigned long load;
  3183. load = p->se.load.weight;
  3184. load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
  3185. return load;
  3186. }
  3187. #else
  3188. static inline void update_shares(int cpu)
  3189. {
  3190. }
  3191. static inline void update_h_load(long cpu)
  3192. {
  3193. }
  3194. static unsigned long task_h_load(struct task_struct *p)
  3195. {
  3196. return p->se.load.weight;
  3197. }
  3198. #endif
  3199. /********** Helpers for find_busiest_group ************************/
  3200. /*
  3201. * sd_lb_stats - Structure to store the statistics of a sched_domain
  3202. * during load balancing.
  3203. */
  3204. struct sd_lb_stats {
  3205. struct sched_group *busiest; /* Busiest group in this sd */
  3206. struct sched_group *this; /* Local group in this sd */
  3207. unsigned long total_load; /* Total load of all groups in sd */
  3208. unsigned long total_pwr; /* Total power of all groups in sd */
  3209. unsigned long avg_load; /* Average load across all groups in sd */
  3210. /** Statistics of this group */
  3211. unsigned long this_load;
  3212. unsigned long this_load_per_task;
  3213. unsigned long this_nr_running;
  3214. unsigned long this_has_capacity;
  3215. unsigned int this_idle_cpus;
  3216. /* Statistics of the busiest group */
  3217. unsigned int busiest_idle_cpus;
  3218. unsigned long max_load;
  3219. unsigned long busiest_load_per_task;
  3220. unsigned long busiest_nr_running;
  3221. unsigned long busiest_group_capacity;
  3222. unsigned long busiest_has_capacity;
  3223. unsigned int busiest_group_weight;
  3224. int group_imb; /* Is there imbalance in this sd */
  3225. };
  3226. /*
  3227. * sg_lb_stats - stats of a sched_group required for load_balancing
  3228. */
  3229. struct sg_lb_stats {
  3230. unsigned long avg_load; /*Avg load across the CPUs of the group */
  3231. unsigned long group_load; /* Total load over the CPUs of the group */
  3232. unsigned long sum_nr_running; /* Nr tasks running in the group */
  3233. unsigned long sum_weighted_load; /* Weighted load of group's tasks */
  3234. unsigned long group_capacity;
  3235. unsigned long idle_cpus;
  3236. unsigned long group_weight;
  3237. int group_imb; /* Is there an imbalance in the group ? */
  3238. int group_has_capacity; /* Is there extra capacity in the group? */
  3239. };
  3240. /**
  3241. * get_sd_load_idx - Obtain the load index for a given sched domain.
  3242. * @sd: The sched_domain whose load_idx is to be obtained.
  3243. * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
  3244. */
  3245. static inline int get_sd_load_idx(struct sched_domain *sd,
  3246. enum cpu_idle_type idle)
  3247. {
  3248. int load_idx;
  3249. switch (idle) {
  3250. case CPU_NOT_IDLE:
  3251. load_idx = sd->busy_idx;
  3252. break;
  3253. case CPU_NEWLY_IDLE:
  3254. load_idx = sd->newidle_idx;
  3255. break;
  3256. default:
  3257. load_idx = sd->idle_idx;
  3258. break;
  3259. }
  3260. return load_idx;
  3261. }
  3262. unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
  3263. {
  3264. return SCHED_POWER_SCALE;
  3265. }
  3266. unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
  3267. {
  3268. return default_scale_freq_power(sd, cpu);
  3269. }
  3270. unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
  3271. {
  3272. unsigned long weight = sd->span_weight;
  3273. unsigned long smt_gain = sd->smt_gain;
  3274. smt_gain /= weight;
  3275. return smt_gain;
  3276. }
  3277. unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
  3278. {
  3279. return default_scale_smt_power(sd, cpu);
  3280. }
  3281. unsigned long scale_rt_power(int cpu)
  3282. {
  3283. struct rq *rq = cpu_rq(cpu);
  3284. u64 total, available, age_stamp, avg;
  3285. /*
  3286. * Since we're reading these variables without serialization make sure
  3287. * we read them once before doing sanity checks on them.
  3288. */
  3289. age_stamp = ACCESS_ONCE(rq->age_stamp);
  3290. avg = ACCESS_ONCE(rq->rt_avg);
  3291. total = sched_avg_period() + (rq->clock - age_stamp);
  3292. if (unlikely(total < avg)) {
  3293. /* Ensures that power won't end up being negative */
  3294. available = 0;
  3295. } else {
  3296. available = total - avg;
  3297. }
  3298. if (unlikely((s64)total < SCHED_POWER_SCALE))
  3299. total = SCHED_POWER_SCALE;
  3300. total >>= SCHED_POWER_SHIFT;
  3301. return div_u64(available, total);
  3302. }
  3303. static void update_cpu_power(struct sched_domain *sd, int cpu)
  3304. {
  3305. unsigned long weight = sd->span_weight;
  3306. unsigned long power = SCHED_POWER_SCALE;
  3307. struct sched_group *sdg = sd->groups;
  3308. if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
  3309. if (sched_feat(ARCH_POWER))
  3310. power *= arch_scale_smt_power(sd, cpu);
  3311. else
  3312. power *= default_scale_smt_power(sd, cpu);
  3313. power >>= SCHED_POWER_SHIFT;
  3314. }
  3315. sdg->sgp->power_orig = power;
  3316. if (sched_feat(ARCH_POWER))
  3317. power *= arch_scale_freq_power(sd, cpu);
  3318. else
  3319. power *= default_scale_freq_power(sd, cpu);
  3320. power >>= SCHED_POWER_SHIFT;
  3321. power *= scale_rt_power(cpu);
  3322. power >>= SCHED_POWER_SHIFT;
  3323. if (!power)
  3324. power = 1;
  3325. cpu_rq(cpu)->cpu_power = power;
  3326. sdg->sgp->power = power;
  3327. }
  3328. void update_group_power(struct sched_domain *sd, int cpu)
  3329. {
  3330. struct sched_domain *child = sd->child;
  3331. struct sched_group *group, *sdg = sd->groups;
  3332. unsigned long power;
  3333. unsigned long interval;
  3334. interval = msecs_to_jiffies(sd->balance_interval);
  3335. interval = clamp(interval, 1UL, max_load_balance_interval);
  3336. sdg->sgp->next_update = jiffies + interval;
  3337. if (!child) {
  3338. update_cpu_power(sd, cpu);
  3339. return;
  3340. }
  3341. power = 0;
  3342. if (child->flags & SD_OVERLAP) {
  3343. /*
  3344. * SD_OVERLAP domains cannot assume that child groups
  3345. * span the current group.
  3346. */
  3347. for_each_cpu(cpu, sched_group_cpus(sdg))
  3348. power += power_of(cpu);
  3349. } else {
  3350. /*
  3351. * !SD_OVERLAP domains can assume that child groups
  3352. * span the current group.
  3353. */
  3354. group = child->groups;
  3355. do {
  3356. power += group->sgp->power;
  3357. group = group->next;
  3358. } while (group != child->groups);
  3359. }
  3360. sdg->sgp->power_orig = sdg->sgp->power = power;
  3361. }
  3362. /*
  3363. * Try and fix up capacity for tiny siblings, this is needed when
  3364. * things like SD_ASYM_PACKING need f_b_g to select another sibling
  3365. * which on its own isn't powerful enough.
  3366. *
  3367. * See update_sd_pick_busiest() and check_asym_packing().
  3368. */
  3369. static inline int
  3370. fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
  3371. {
  3372. /*
  3373. * Only siblings can have significantly less than SCHED_POWER_SCALE
  3374. */
  3375. if (!(sd->flags & SD_SHARE_CPUPOWER))
  3376. return 0;
  3377. /*
  3378. * If ~90% of the cpu_power is still there, we're good.
  3379. */
  3380. if (group->sgp->power * 32 > group->sgp->power_orig * 29)
  3381. return 1;
  3382. return 0;
  3383. }
  3384. /**
  3385. * update_sg_lb_stats - Update sched_group's statistics for load balancing.
  3386. * @env: The load balancing environment.
  3387. * @group: sched_group whose statistics are to be updated.
  3388. * @load_idx: Load index of sched_domain of this_cpu for load calc.
  3389. * @local_group: Does group contain this_cpu.
  3390. * @balance: Should we balance.
  3391. * @sgs: variable to hold the statistics for this group.
  3392. */
  3393. static inline void update_sg_lb_stats(struct lb_env *env,
  3394. struct sched_group *group, int load_idx,
  3395. int local_group, int *balance, struct sg_lb_stats *sgs)
  3396. {
  3397. unsigned long nr_running, max_nr_running, min_nr_running;
  3398. unsigned long load, max_cpu_load, min_cpu_load;
  3399. unsigned int balance_cpu = -1, first_idle_cpu = 0;
  3400. unsigned long avg_load_per_task = 0;
  3401. int i;
  3402. if (local_group)
  3403. balance_cpu = group_balance_cpu(group);
  3404. /* Tally up the load of all CPUs in the group */
  3405. max_cpu_load = 0;
  3406. min_cpu_load = ~0UL;
  3407. max_nr_running = 0;
  3408. min_nr_running = ~0UL;
  3409. for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
  3410. struct rq *rq = cpu_rq(i);
  3411. nr_running = rq->nr_running;
  3412. /* Bias balancing toward cpus of our domain */
  3413. if (local_group) {
  3414. if (idle_cpu(i) && !first_idle_cpu &&
  3415. cpumask_test_cpu(i, sched_group_mask(group))) {
  3416. first_idle_cpu = 1;
  3417. balance_cpu = i;
  3418. }
  3419. load = target_load(i, load_idx);
  3420. } else {
  3421. load = source_load(i, load_idx);
  3422. if (load > max_cpu_load)
  3423. max_cpu_load = load;
  3424. if (min_cpu_load > load)
  3425. min_cpu_load = load;
  3426. if (nr_running > max_nr_running)
  3427. max_nr_running = nr_running;
  3428. if (min_nr_running > nr_running)
  3429. min_nr_running = nr_running;
  3430. }
  3431. sgs->group_load += load;
  3432. sgs->sum_nr_running += nr_running;
  3433. sgs->sum_weighted_load += weighted_cpuload(i);
  3434. if (idle_cpu(i))
  3435. sgs->idle_cpus++;
  3436. }
  3437. /*
  3438. * First idle cpu or the first cpu(busiest) in this sched group
  3439. * is eligible for doing load balancing at this and above
  3440. * domains. In the newly idle case, we will allow all the cpu's
  3441. * to do the newly idle load balance.
  3442. */
  3443. if (local_group) {
  3444. if (env->idle != CPU_NEWLY_IDLE) {
  3445. if (balance_cpu != env->dst_cpu) {
  3446. *balance = 0;
  3447. return;
  3448. }
  3449. update_group_power(env->sd, env->dst_cpu);
  3450. } else if (time_after_eq(jiffies, group->sgp->next_update))
  3451. update_group_power(env->sd, env->dst_cpu);
  3452. }
  3453. /* Adjust by relative CPU power of the group */
  3454. sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
  3455. /*
  3456. * Consider the group unbalanced when the imbalance is larger
  3457. * than the average weight of a task.
  3458. *
  3459. * APZ: with cgroup the avg task weight can vary wildly and
  3460. * might not be a suitable number - should we keep a
  3461. * normalized nr_running number somewhere that negates
  3462. * the hierarchy?
  3463. */
  3464. if (sgs->sum_nr_running)
  3465. avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
  3466. if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
  3467. (max_nr_running - min_nr_running) > 1)
  3468. sgs->group_imb = 1;
  3469. sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
  3470. SCHED_POWER_SCALE);
  3471. if (!sgs->group_capacity)
  3472. sgs->group_capacity = fix_small_capacity(env->sd, group);
  3473. sgs->group_weight = group->group_weight;
  3474. if (sgs->group_capacity > sgs->sum_nr_running)
  3475. sgs->group_has_capacity = 1;
  3476. }
  3477. /**
  3478. * update_sd_pick_busiest - return 1 on busiest group
  3479. * @env: The load balancing environment.
  3480. * @sds: sched_domain statistics
  3481. * @sg: sched_group candidate to be checked for being the busiest
  3482. * @sgs: sched_group statistics
  3483. *
  3484. * Determine if @sg is a busier group than the previously selected
  3485. * busiest group.
  3486. */
  3487. static bool update_sd_pick_busiest(struct lb_env *env,
  3488. struct sd_lb_stats *sds,
  3489. struct sched_group *sg,
  3490. struct sg_lb_stats *sgs)
  3491. {
  3492. if (sgs->avg_load <= sds->max_load)
  3493. return false;
  3494. if (sgs->sum_nr_running > sgs->group_capacity)
  3495. return true;
  3496. if (sgs->group_imb)
  3497. return true;
  3498. /*
  3499. * ASYM_PACKING needs to move all the work to the lowest
  3500. * numbered CPUs in the group, therefore mark all groups
  3501. * higher than ourself as busy.
  3502. */
  3503. if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
  3504. env->dst_cpu < group_first_cpu(sg)) {
  3505. if (!sds->busiest)
  3506. return true;
  3507. if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
  3508. return true;
  3509. }
  3510. return false;
  3511. }
  3512. /**
  3513. * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
  3514. * @env: The load balancing environment.
  3515. * @balance: Should we balance.
  3516. * @sds: variable to hold the statistics for this sched_domain.
  3517. */
  3518. static inline void update_sd_lb_stats(struct lb_env *env,
  3519. int *balance, struct sd_lb_stats *sds)
  3520. {
  3521. struct sched_domain *child = env->sd->child;
  3522. struct sched_group *sg = env->sd->groups;
  3523. struct sg_lb_stats sgs;
  3524. int load_idx, prefer_sibling = 0;
  3525. if (child && child->flags & SD_PREFER_SIBLING)
  3526. prefer_sibling = 1;
  3527. load_idx = get_sd_load_idx(env->sd, env->idle);
  3528. do {
  3529. int local_group;
  3530. local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
  3531. memset(&sgs, 0, sizeof(sgs));
  3532. update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs);
  3533. if (local_group && !(*balance))
  3534. return;
  3535. sds->total_load += sgs.group_load;
  3536. sds->total_pwr += sg->sgp->power;
  3537. /*
  3538. * In case the child domain prefers tasks go to siblings
  3539. * first, lower the sg capacity to one so that we'll try
  3540. * and move all the excess tasks away. We lower the capacity
  3541. * of a group only if the local group has the capacity to fit
  3542. * these excess tasks, i.e. nr_running < group_capacity. The
  3543. * extra check prevents the case where you always pull from the
  3544. * heaviest group when it is already under-utilized (possible
  3545. * with a large weight task outweighs the tasks on the system).
  3546. */
  3547. if (prefer_sibling && !local_group && sds->this_has_capacity)
  3548. sgs.group_capacity = min(sgs.group_capacity, 1UL);
  3549. if (local_group) {
  3550. sds->this_load = sgs.avg_load;
  3551. sds->this = sg;
  3552. sds->this_nr_running = sgs.sum_nr_running;
  3553. sds->this_load_per_task = sgs.sum_weighted_load;
  3554. sds->this_has_capacity = sgs.group_has_capacity;
  3555. sds->this_idle_cpus = sgs.idle_cpus;
  3556. } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
  3557. sds->max_load = sgs.avg_load;
  3558. sds->busiest = sg;
  3559. sds->busiest_nr_running = sgs.sum_nr_running;
  3560. sds->busiest_idle_cpus = sgs.idle_cpus;
  3561. sds->busiest_group_capacity = sgs.group_capacity;
  3562. sds->busiest_load_per_task = sgs.sum_weighted_load;
  3563. sds->busiest_has_capacity = sgs.group_has_capacity;
  3564. sds->busiest_group_weight = sgs.group_weight;
  3565. sds->group_imb = sgs.group_imb;
  3566. }
  3567. sg = sg->next;
  3568. } while (sg != env->sd->groups);
  3569. }
  3570. /**
  3571. * check_asym_packing - Check to see if the group is packed into the
  3572. * sched doman.
  3573. *
  3574. * This is primarily intended to used at the sibling level. Some
  3575. * cores like POWER7 prefer to use lower numbered SMT threads. In the
  3576. * case of POWER7, it can move to lower SMT modes only when higher
  3577. * threads are idle. When in lower SMT modes, the threads will
  3578. * perform better since they share less core resources. Hence when we
  3579. * have idle threads, we want them to be the higher ones.
  3580. *
  3581. * This packing function is run on idle threads. It checks to see if
  3582. * the busiest CPU in this domain (core in the P7 case) has a higher
  3583. * CPU number than the packing function is being run on. Here we are
  3584. * assuming lower CPU number will be equivalent to lower a SMT thread
  3585. * number.
  3586. *
  3587. * Returns 1 when packing is required and a task should be moved to
  3588. * this CPU. The amount of the imbalance is returned in *imbalance.
  3589. *
  3590. * @env: The load balancing environment.
  3591. * @sds: Statistics of the sched_domain which is to be packed
  3592. */
  3593. static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
  3594. {
  3595. int busiest_cpu;
  3596. if (!(env->sd->flags & SD_ASYM_PACKING))
  3597. return 0;
  3598. if (!sds->busiest)
  3599. return 0;
  3600. busiest_cpu = group_first_cpu(sds->busiest);
  3601. if (env->dst_cpu > busiest_cpu)
  3602. return 0;
  3603. env->imbalance = DIV_ROUND_CLOSEST(
  3604. sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
  3605. return 1;
  3606. }
  3607. /**
  3608. * fix_small_imbalance - Calculate the minor imbalance that exists
  3609. * amongst the groups of a sched_domain, during
  3610. * load balancing.
  3611. * @env: The load balancing environment.
  3612. * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
  3613. */
  3614. static inline
  3615. void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
  3616. {
  3617. unsigned long tmp, pwr_now = 0, pwr_move = 0;
  3618. unsigned int imbn = 2;
  3619. unsigned long scaled_busy_load_per_task;
  3620. if (sds->this_nr_running) {
  3621. sds->this_load_per_task /= sds->this_nr_running;
  3622. if (sds->busiest_load_per_task >
  3623. sds->this_load_per_task)
  3624. imbn = 1;
  3625. } else {
  3626. sds->this_load_per_task =
  3627. cpu_avg_load_per_task(env->dst_cpu);
  3628. }
  3629. scaled_busy_load_per_task = sds->busiest_load_per_task
  3630. * SCHED_POWER_SCALE;
  3631. scaled_busy_load_per_task /= sds->busiest->sgp->power;
  3632. if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
  3633. (scaled_busy_load_per_task * imbn)) {
  3634. env->imbalance = sds->busiest_load_per_task;
  3635. return;
  3636. }
  3637. /*
  3638. * OK, we don't have enough imbalance to justify moving tasks,
  3639. * however we may be able to increase total CPU power used by
  3640. * moving them.
  3641. */
  3642. pwr_now += sds->busiest->sgp->power *
  3643. min(sds->busiest_load_per_task, sds->max_load);
  3644. pwr_now += sds->this->sgp->power *
  3645. min(sds->this_load_per_task, sds->this_load);
  3646. pwr_now /= SCHED_POWER_SCALE;
  3647. /* Amount of load we'd subtract */
  3648. tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
  3649. sds->busiest->sgp->power;
  3650. if (sds->max_load > tmp)
  3651. pwr_move += sds->busiest->sgp->power *
  3652. min(sds->busiest_load_per_task, sds->max_load - tmp);
  3653. /* Amount of load we'd add */
  3654. if (sds->max_load * sds->busiest->sgp->power <
  3655. sds->busiest_load_per_task * SCHED_POWER_SCALE)
  3656. tmp = (sds->max_load * sds->busiest->sgp->power) /
  3657. sds->this->sgp->power;
  3658. else
  3659. tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
  3660. sds->this->sgp->power;
  3661. pwr_move += sds->this->sgp->power *
  3662. min(sds->this_load_per_task, sds->this_load + tmp);
  3663. pwr_move /= SCHED_POWER_SCALE;
  3664. /* Move if we gain throughput */
  3665. if (pwr_move > pwr_now)
  3666. env->imbalance = sds->busiest_load_per_task;
  3667. }
  3668. /**
  3669. * calculate_imbalance - Calculate the amount of imbalance present within the
  3670. * groups of a given sched_domain during load balance.
  3671. * @env: load balance environment
  3672. * @sds: statistics of the sched_domain whose imbalance is to be calculated.
  3673. */
  3674. static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
  3675. {
  3676. unsigned long max_pull, load_above_capacity = ~0UL;
  3677. sds->busiest_load_per_task /= sds->busiest_nr_running;
  3678. if (sds->group_imb) {
  3679. sds->busiest_load_per_task =
  3680. min(sds->busiest_load_per_task, sds->avg_load);
  3681. }
  3682. /*
  3683. * In the presence of smp nice balancing, certain scenarios can have
  3684. * max load less than avg load(as we skip the groups at or below
  3685. * its cpu_power, while calculating max_load..)
  3686. */
  3687. if (sds->max_load < sds->avg_load) {
  3688. env->imbalance = 0;
  3689. return fix_small_imbalance(env, sds);
  3690. }
  3691. if (!sds->group_imb) {
  3692. /*
  3693. * Don't want to pull so many tasks that a group would go idle.
  3694. */
  3695. load_above_capacity = (sds->busiest_nr_running -
  3696. sds->busiest_group_capacity);
  3697. load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
  3698. load_above_capacity /= sds->busiest->sgp->power;
  3699. }
  3700. /*
  3701. * We're trying to get all the cpus to the average_load, so we don't
  3702. * want to push ourselves above the average load, nor do we wish to
  3703. * reduce the max loaded cpu below the average load. At the same time,
  3704. * we also don't want to reduce the group load below the group capacity
  3705. * (so that we can implement power-savings policies etc). Thus we look
  3706. * for the minimum possible imbalance.
  3707. * Be careful of negative numbers as they'll appear as very large values
  3708. * with unsigned longs.
  3709. */
  3710. max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
  3711. /* How much load to actually move to equalise the imbalance */
  3712. env->imbalance = min(max_pull * sds->busiest->sgp->power,
  3713. (sds->avg_load - sds->this_load) * sds->this->sgp->power)
  3714. / SCHED_POWER_SCALE;
  3715. /*
  3716. * if *imbalance is less than the average load per runnable task
  3717. * there is no guarantee that any tasks will be moved so we'll have
  3718. * a think about bumping its value to force at least one task to be
  3719. * moved
  3720. */
  3721. if (env->imbalance < sds->busiest_load_per_task)
  3722. return fix_small_imbalance(env, sds);
  3723. }
  3724. /******* find_busiest_group() helpers end here *********************/
  3725. /**
  3726. * find_busiest_group - Returns the busiest group within the sched_domain
  3727. * if there is an imbalance. If there isn't an imbalance, and
  3728. * the user has opted for power-savings, it returns a group whose
  3729. * CPUs can be put to idle by rebalancing those tasks elsewhere, if
  3730. * such a group exists.
  3731. *
  3732. * Also calculates the amount of weighted load which should be moved
  3733. * to restore balance.
  3734. *
  3735. * @env: The load balancing environment.
  3736. * @balance: Pointer to a variable indicating if this_cpu
  3737. * is the appropriate cpu to perform load balancing at this_level.
  3738. *
  3739. * Returns: - the busiest group if imbalance exists.
  3740. * - If no imbalance and user has opted for power-savings balance,
  3741. * return the least loaded group whose CPUs can be
  3742. * put to idle by rebalancing its tasks onto our group.
  3743. */
  3744. static struct sched_group *
  3745. find_busiest_group(struct lb_env *env, int *balance)
  3746. {
  3747. struct sd_lb_stats sds;
  3748. memset(&sds, 0, sizeof(sds));
  3749. /*
  3750. * Compute the various statistics relavent for load balancing at
  3751. * this level.
  3752. */
  3753. update_sd_lb_stats(env, balance, &sds);
  3754. /*
  3755. * this_cpu is not the appropriate cpu to perform load balancing at
  3756. * this level.
  3757. */
  3758. if (!(*balance))
  3759. goto ret;
  3760. if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
  3761. check_asym_packing(env, &sds))
  3762. return sds.busiest;
  3763. /* There is no busy sibling group to pull tasks from */
  3764. if (!sds.busiest || sds.busiest_nr_running == 0)
  3765. goto out_balanced;
  3766. sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
  3767. /*
  3768. * If the busiest group is imbalanced the below checks don't
  3769. * work because they assumes all things are equal, which typically
  3770. * isn't true due to cpus_allowed constraints and the like.
  3771. */
  3772. if (sds.group_imb)
  3773. goto force_balance;
  3774. /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
  3775. if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
  3776. !sds.busiest_has_capacity)
  3777. goto force_balance;
  3778. /*
  3779. * If the local group is more busy than the selected busiest group
  3780. * don't try and pull any tasks.
  3781. */
  3782. if (sds.this_load >= sds.max_load)
  3783. goto out_balanced;
  3784. /*
  3785. * Don't pull any tasks if this group is already above the domain
  3786. * average load.
  3787. */
  3788. if (sds.this_load >= sds.avg_load)
  3789. goto out_balanced;
  3790. if (env->idle == CPU_IDLE) {
  3791. /*
  3792. * This cpu is idle. If the busiest group load doesn't
  3793. * have more tasks than the number of available cpu's and
  3794. * there is no imbalance between this and busiest group
  3795. * wrt to idle cpu's, it is balanced.
  3796. */
  3797. if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
  3798. sds.busiest_nr_running <= sds.busiest_group_weight)
  3799. goto out_balanced;
  3800. } else {
  3801. /*
  3802. * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
  3803. * imbalance_pct to be conservative.
  3804. */
  3805. if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
  3806. goto out_balanced;
  3807. }
  3808. force_balance:
  3809. /* Looks like there is an imbalance. Compute it */
  3810. calculate_imbalance(env, &sds);
  3811. return sds.busiest;
  3812. out_balanced:
  3813. ret:
  3814. env->imbalance = 0;
  3815. return NULL;
  3816. }
  3817. /*
  3818. * find_busiest_queue - find the busiest runqueue among the cpus in group.
  3819. */
  3820. static struct rq *find_busiest_queue(struct lb_env *env,
  3821. struct sched_group *group)
  3822. {
  3823. struct rq *busiest = NULL, *rq;
  3824. unsigned long max_load = 0;
  3825. int i;
  3826. for_each_cpu(i, sched_group_cpus(group)) {
  3827. unsigned long power = power_of(i);
  3828. unsigned long capacity = DIV_ROUND_CLOSEST(power,
  3829. SCHED_POWER_SCALE);
  3830. unsigned long wl;
  3831. if (!capacity)
  3832. capacity = fix_small_capacity(env->sd, group);
  3833. if (!cpumask_test_cpu(i, env->cpus))
  3834. continue;
  3835. rq = cpu_rq(i);
  3836. wl = weighted_cpuload(i);
  3837. /*
  3838. * When comparing with imbalance, use weighted_cpuload()
  3839. * which is not scaled with the cpu power.
  3840. */
  3841. if (capacity && rq->nr_running == 1 && wl > env->imbalance)
  3842. continue;
  3843. /*
  3844. * For the load comparisons with the other cpu's, consider
  3845. * the weighted_cpuload() scaled with the cpu power, so that
  3846. * the load can be moved away from the cpu that is potentially
  3847. * running at a lower capacity.
  3848. */
  3849. wl = (wl * SCHED_POWER_SCALE) / power;
  3850. if (wl > max_load) {
  3851. max_load = wl;
  3852. busiest = rq;
  3853. }
  3854. }
  3855. return busiest;
  3856. }
  3857. /*
  3858. * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
  3859. * so long as it is large enough.
  3860. */
  3861. #define MAX_PINNED_INTERVAL 512
  3862. /* Working cpumask for load_balance and load_balance_newidle. */
  3863. DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
  3864. static int need_active_balance(struct lb_env *env)
  3865. {
  3866. struct sched_domain *sd = env->sd;
  3867. if (env->idle == CPU_NEWLY_IDLE) {
  3868. /*
  3869. * ASYM_PACKING needs to force migrate tasks from busy but
  3870. * higher numbered CPUs in order to pack all tasks in the
  3871. * lowest numbered CPUs.
  3872. */
  3873. if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
  3874. return 1;
  3875. }
  3876. return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
  3877. }
  3878. static int active_load_balance_cpu_stop(void *data);
  3879. /*
  3880. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  3881. * tasks if there is an imbalance.
  3882. */
  3883. static int load_balance(int this_cpu, struct rq *this_rq,
  3884. struct sched_domain *sd, enum cpu_idle_type idle,
  3885. int *balance)
  3886. {
  3887. int ld_moved, cur_ld_moved, active_balance = 0;
  3888. int lb_iterations, max_lb_iterations;
  3889. struct sched_group *group;
  3890. struct rq *busiest;
  3891. unsigned long flags;
  3892. struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
  3893. struct lb_env env = {
  3894. .sd = sd,
  3895. .dst_cpu = this_cpu,
  3896. .dst_rq = this_rq,
  3897. .dst_grpmask = sched_group_cpus(sd->groups),
  3898. .idle = idle,
  3899. .loop_break = sched_nr_migrate_break,
  3900. .cpus = cpus,
  3901. };
  3902. cpumask_copy(cpus, cpu_active_mask);
  3903. max_lb_iterations = cpumask_weight(env.dst_grpmask);
  3904. schedstat_inc(sd, lb_count[idle]);
  3905. redo:
  3906. group = find_busiest_group(&env, balance);
  3907. if (*balance == 0)
  3908. goto out_balanced;
  3909. if (!group) {
  3910. schedstat_inc(sd, lb_nobusyg[idle]);
  3911. goto out_balanced;
  3912. }
  3913. busiest = find_busiest_queue(&env, group);
  3914. if (!busiest) {
  3915. schedstat_inc(sd, lb_nobusyq[idle]);
  3916. goto out_balanced;
  3917. }
  3918. BUG_ON(busiest == env.dst_rq);
  3919. schedstat_add(sd, lb_imbalance[idle], env.imbalance);
  3920. ld_moved = 0;
  3921. lb_iterations = 1;
  3922. if (busiest->nr_running > 1) {
  3923. /*
  3924. * Attempt to move tasks. If find_busiest_group has found
  3925. * an imbalance but busiest->nr_running <= 1, the group is
  3926. * still unbalanced. ld_moved simply stays zero, so it is
  3927. * correctly treated as an imbalance.
  3928. */
  3929. env.flags |= LBF_ALL_PINNED;
  3930. env.src_cpu = busiest->cpu;
  3931. env.src_rq = busiest;
  3932. env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
  3933. update_h_load(env.src_cpu);
  3934. more_balance:
  3935. local_irq_save(flags);
  3936. double_rq_lock(env.dst_rq, busiest);
  3937. /*
  3938. * cur_ld_moved - load moved in current iteration
  3939. * ld_moved - cumulative load moved across iterations
  3940. */
  3941. cur_ld_moved = move_tasks(&env);
  3942. ld_moved += cur_ld_moved;
  3943. double_rq_unlock(env.dst_rq, busiest);
  3944. local_irq_restore(flags);
  3945. if (env.flags & LBF_NEED_BREAK) {
  3946. env.flags &= ~LBF_NEED_BREAK;
  3947. goto more_balance;
  3948. }
  3949. /*
  3950. * some other cpu did the load balance for us.
  3951. */
  3952. if (cur_ld_moved && env.dst_cpu != smp_processor_id())
  3953. resched_cpu(env.dst_cpu);
  3954. /*
  3955. * Revisit (affine) tasks on src_cpu that couldn't be moved to
  3956. * us and move them to an alternate dst_cpu in our sched_group
  3957. * where they can run. The upper limit on how many times we
  3958. * iterate on same src_cpu is dependent on number of cpus in our
  3959. * sched_group.
  3960. *
  3961. * This changes load balance semantics a bit on who can move
  3962. * load to a given_cpu. In addition to the given_cpu itself
  3963. * (or a ilb_cpu acting on its behalf where given_cpu is
  3964. * nohz-idle), we now have balance_cpu in a position to move
  3965. * load to given_cpu. In rare situations, this may cause
  3966. * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
  3967. * _independently_ and at _same_ time to move some load to
  3968. * given_cpu) causing exceess load to be moved to given_cpu.
  3969. * This however should not happen so much in practice and
  3970. * moreover subsequent load balance cycles should correct the
  3971. * excess load moved.
  3972. */
  3973. if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 &&
  3974. lb_iterations++ < max_lb_iterations) {
  3975. env.dst_rq = cpu_rq(env.new_dst_cpu);
  3976. env.dst_cpu = env.new_dst_cpu;
  3977. env.flags &= ~LBF_SOME_PINNED;
  3978. env.loop = 0;
  3979. env.loop_break = sched_nr_migrate_break;
  3980. /*
  3981. * Go back to "more_balance" rather than "redo" since we
  3982. * need to continue with same src_cpu.
  3983. */
  3984. goto more_balance;
  3985. }
  3986. /* All tasks on this runqueue were pinned by CPU affinity */
  3987. if (unlikely(env.flags & LBF_ALL_PINNED)) {
  3988. cpumask_clear_cpu(cpu_of(busiest), cpus);
  3989. if (!cpumask_empty(cpus)) {
  3990. env.loop = 0;
  3991. env.loop_break = sched_nr_migrate_break;
  3992. goto redo;
  3993. }
  3994. goto out_balanced;
  3995. }
  3996. }
  3997. if (!ld_moved) {
  3998. schedstat_inc(sd, lb_failed[idle]);
  3999. /*
  4000. * Increment the failure counter only on periodic balance.
  4001. * We do not want newidle balance, which can be very
  4002. * frequent, pollute the failure counter causing
  4003. * excessive cache_hot migrations and active balances.
  4004. */
  4005. if (idle != CPU_NEWLY_IDLE)
  4006. sd->nr_balance_failed++;
  4007. if (need_active_balance(&env)) {
  4008. raw_spin_lock_irqsave(&busiest->lock, flags);
  4009. /* don't kick the active_load_balance_cpu_stop,
  4010. * if the curr task on busiest cpu can't be
  4011. * moved to this_cpu
  4012. */
  4013. if (!cpumask_test_cpu(this_cpu,
  4014. tsk_cpus_allowed(busiest->curr))) {
  4015. raw_spin_unlock_irqrestore(&busiest->lock,
  4016. flags);
  4017. env.flags |= LBF_ALL_PINNED;
  4018. goto out_one_pinned;
  4019. }
  4020. /*
  4021. * ->active_balance synchronizes accesses to
  4022. * ->active_balance_work. Once set, it's cleared
  4023. * only after active load balance is finished.
  4024. */
  4025. if (!busiest->active_balance) {
  4026. busiest->active_balance = 1;
  4027. busiest->push_cpu = this_cpu;
  4028. active_balance = 1;
  4029. }
  4030. raw_spin_unlock_irqrestore(&busiest->lock, flags);
  4031. if (active_balance) {
  4032. stop_one_cpu_nowait(cpu_of(busiest),
  4033. active_load_balance_cpu_stop, busiest,
  4034. &busiest->active_balance_work);
  4035. }
  4036. /*
  4037. * We've kicked active balancing, reset the failure
  4038. * counter.
  4039. */
  4040. sd->nr_balance_failed = sd->cache_nice_tries+1;
  4041. }
  4042. } else
  4043. sd->nr_balance_failed = 0;
  4044. if (likely(!active_balance)) {
  4045. /* We were unbalanced, so reset the balancing interval */
  4046. sd->balance_interval = sd->min_interval;
  4047. } else {
  4048. /*
  4049. * If we've begun active balancing, start to back off. This
  4050. * case may not be covered by the all_pinned logic if there
  4051. * is only 1 task on the busy runqueue (because we don't call
  4052. * move_tasks).
  4053. */
  4054. if (sd->balance_interval < sd->max_interval)
  4055. sd->balance_interval *= 2;
  4056. }
  4057. goto out;
  4058. out_balanced:
  4059. schedstat_inc(sd, lb_balanced[idle]);
  4060. sd->nr_balance_failed = 0;
  4061. out_one_pinned:
  4062. /* tune up the balancing interval */
  4063. if (((env.flags & LBF_ALL_PINNED) &&
  4064. sd->balance_interval < MAX_PINNED_INTERVAL) ||
  4065. (sd->balance_interval < sd->max_interval))
  4066. sd->balance_interval *= 2;
  4067. ld_moved = 0;
  4068. out:
  4069. return ld_moved;
  4070. }
  4071. /*
  4072. * idle_balance is called by schedule() if this_cpu is about to become
  4073. * idle. Attempts to pull tasks from other CPUs.
  4074. */
  4075. void idle_balance(int this_cpu, struct rq *this_rq)
  4076. {
  4077. struct sched_domain *sd;
  4078. int pulled_task = 0;
  4079. unsigned long next_balance = jiffies + HZ;
  4080. this_rq->idle_stamp = this_rq->clock;
  4081. if (this_rq->avg_idle < sysctl_sched_migration_cost)
  4082. return;
  4083. update_rq_runnable_avg(this_rq, 1);
  4084. /*
  4085. * Drop the rq->lock, but keep IRQ/preempt disabled.
  4086. */
  4087. raw_spin_unlock(&this_rq->lock);
  4088. update_shares(this_cpu);
  4089. rcu_read_lock();
  4090. for_each_domain(this_cpu, sd) {
  4091. unsigned long interval;
  4092. int balance = 1;
  4093. if (!(sd->flags & SD_LOAD_BALANCE))
  4094. continue;
  4095. if (sd->flags & SD_BALANCE_NEWIDLE) {
  4096. /* If we've pulled tasks over stop searching: */
  4097. pulled_task = load_balance(this_cpu, this_rq,
  4098. sd, CPU_NEWLY_IDLE, &balance);
  4099. }
  4100. interval = msecs_to_jiffies(sd->balance_interval);
  4101. if (time_after(next_balance, sd->last_balance + interval))
  4102. next_balance = sd->last_balance + interval;
  4103. if (pulled_task) {
  4104. this_rq->idle_stamp = 0;
  4105. break;
  4106. }
  4107. }
  4108. rcu_read_unlock();
  4109. raw_spin_lock(&this_rq->lock);
  4110. if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
  4111. /*
  4112. * We are going idle. next_balance may be set based on
  4113. * a busy processor. So reset next_balance.
  4114. */
  4115. this_rq->next_balance = next_balance;
  4116. }
  4117. }
  4118. /*
  4119. * active_load_balance_cpu_stop is run by cpu stopper. It pushes
  4120. * running tasks off the busiest CPU onto idle CPUs. It requires at
  4121. * least 1 task to be running on each physical CPU where possible, and
  4122. * avoids physical / logical imbalances.
  4123. */
  4124. static int active_load_balance_cpu_stop(void *data)
  4125. {
  4126. struct rq *busiest_rq = data;
  4127. int busiest_cpu = cpu_of(busiest_rq);
  4128. int target_cpu = busiest_rq->push_cpu;
  4129. struct rq *target_rq = cpu_rq(target_cpu);
  4130. struct sched_domain *sd;
  4131. raw_spin_lock_irq(&busiest_rq->lock);
  4132. /* make sure the requested cpu hasn't gone down in the meantime */
  4133. if (unlikely(busiest_cpu != smp_processor_id() ||
  4134. !busiest_rq->active_balance))
  4135. goto out_unlock;
  4136. /* Is there any task to move? */
  4137. if (busiest_rq->nr_running <= 1)
  4138. goto out_unlock;
  4139. /*
  4140. * This condition is "impossible", if it occurs
  4141. * we need to fix it. Originally reported by
  4142. * Bjorn Helgaas on a 128-cpu setup.
  4143. */
  4144. BUG_ON(busiest_rq == target_rq);
  4145. /* move a task from busiest_rq to target_rq */
  4146. double_lock_balance(busiest_rq, target_rq);
  4147. /* Search for an sd spanning us and the target CPU. */
  4148. rcu_read_lock();
  4149. for_each_domain(target_cpu, sd) {
  4150. if ((sd->flags & SD_LOAD_BALANCE) &&
  4151. cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
  4152. break;
  4153. }
  4154. if (likely(sd)) {
  4155. struct lb_env env = {
  4156. .sd = sd,
  4157. .dst_cpu = target_cpu,
  4158. .dst_rq = target_rq,
  4159. .src_cpu = busiest_rq->cpu,
  4160. .src_rq = busiest_rq,
  4161. .idle = CPU_IDLE,
  4162. };
  4163. schedstat_inc(sd, alb_count);
  4164. if (move_one_task(&env))
  4165. schedstat_inc(sd, alb_pushed);
  4166. else
  4167. schedstat_inc(sd, alb_failed);
  4168. }
  4169. rcu_read_unlock();
  4170. double_unlock_balance(busiest_rq, target_rq);
  4171. out_unlock:
  4172. busiest_rq->active_balance = 0;
  4173. raw_spin_unlock_irq(&busiest_rq->lock);
  4174. return 0;
  4175. }
  4176. #ifdef CONFIG_NO_HZ
  4177. /*
  4178. * idle load balancing details
  4179. * - When one of the busy CPUs notice that there may be an idle rebalancing
  4180. * needed, they will kick the idle load balancer, which then does idle
  4181. * load balancing for all the idle CPUs.
  4182. */
  4183. static struct {
  4184. cpumask_var_t idle_cpus_mask;
  4185. atomic_t nr_cpus;
  4186. unsigned long next_balance; /* in jiffy units */
  4187. } nohz ____cacheline_aligned;
  4188. static inline int find_new_ilb(int call_cpu)
  4189. {
  4190. int ilb = cpumask_first(nohz.idle_cpus_mask);
  4191. if (ilb < nr_cpu_ids && idle_cpu(ilb))
  4192. return ilb;
  4193. return nr_cpu_ids;
  4194. }
  4195. /*
  4196. * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
  4197. * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
  4198. * CPU (if there is one).
  4199. */
  4200. static void nohz_balancer_kick(int cpu)
  4201. {
  4202. int ilb_cpu;
  4203. nohz.next_balance++;
  4204. ilb_cpu = find_new_ilb(cpu);
  4205. if (ilb_cpu >= nr_cpu_ids)
  4206. return;
  4207. if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
  4208. return;
  4209. /*
  4210. * Use smp_send_reschedule() instead of resched_cpu().
  4211. * This way we generate a sched IPI on the target cpu which
  4212. * is idle. And the softirq performing nohz idle load balance
  4213. * will be run before returning from the IPI.
  4214. */
  4215. smp_send_reschedule(ilb_cpu);
  4216. return;
  4217. }
  4218. static inline void nohz_balance_exit_idle(int cpu)
  4219. {
  4220. if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
  4221. cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
  4222. atomic_dec(&nohz.nr_cpus);
  4223. clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
  4224. }
  4225. }
  4226. static inline void set_cpu_sd_state_busy(void)
  4227. {
  4228. struct sched_domain *sd;
  4229. int cpu = smp_processor_id();
  4230. if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
  4231. return;
  4232. clear_bit(NOHZ_IDLE, nohz_flags(cpu));
  4233. rcu_read_lock();
  4234. for_each_domain(cpu, sd)
  4235. atomic_inc(&sd->groups->sgp->nr_busy_cpus);
  4236. rcu_read_unlock();
  4237. }
  4238. void set_cpu_sd_state_idle(void)
  4239. {
  4240. struct sched_domain *sd;
  4241. int cpu = smp_processor_id();
  4242. if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
  4243. return;
  4244. set_bit(NOHZ_IDLE, nohz_flags(cpu));
  4245. rcu_read_lock();
  4246. for_each_domain(cpu, sd)
  4247. atomic_dec(&sd->groups->sgp->nr_busy_cpus);
  4248. rcu_read_unlock();
  4249. }
  4250. /*
  4251. * This routine will record that the cpu is going idle with tick stopped.
  4252. * This info will be used in performing idle load balancing in the future.
  4253. */
  4254. void nohz_balance_enter_idle(int cpu)
  4255. {
  4256. /*
  4257. * If this cpu is going down, then nothing needs to be done.
  4258. */
  4259. if (!cpu_active(cpu))
  4260. return;
  4261. if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
  4262. return;
  4263. cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
  4264. atomic_inc(&nohz.nr_cpus);
  4265. set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
  4266. }
  4267. static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
  4268. unsigned long action, void *hcpu)
  4269. {
  4270. switch (action & ~CPU_TASKS_FROZEN) {
  4271. case CPU_DYING:
  4272. nohz_balance_exit_idle(smp_processor_id());
  4273. return NOTIFY_OK;
  4274. default:
  4275. return NOTIFY_DONE;
  4276. }
  4277. }
  4278. #endif
  4279. static DEFINE_SPINLOCK(balancing);
  4280. /*
  4281. * Scale the max load_balance interval with the number of CPUs in the system.
  4282. * This trades load-balance latency on larger machines for less cross talk.
  4283. */
  4284. void update_max_interval(void)
  4285. {
  4286. max_load_balance_interval = HZ*num_online_cpus()/10;
  4287. }
  4288. /*
  4289. * It checks each scheduling domain to see if it is due to be balanced,
  4290. * and initiates a balancing operation if so.
  4291. *
  4292. * Balancing parameters are set up in arch_init_sched_domains.
  4293. */
  4294. static void rebalance_domains(int cpu, enum cpu_idle_type idle)
  4295. {
  4296. int balance = 1;
  4297. struct rq *rq = cpu_rq(cpu);
  4298. unsigned long interval;
  4299. struct sched_domain *sd;
  4300. /* Earliest time when we have to do rebalance again */
  4301. unsigned long next_balance = jiffies + 60*HZ;
  4302. int update_next_balance = 0;
  4303. int need_serialize;
  4304. update_shares(cpu);
  4305. rcu_read_lock();
  4306. for_each_domain(cpu, sd) {
  4307. if (!(sd->flags & SD_LOAD_BALANCE))
  4308. continue;
  4309. interval = sd->balance_interval;
  4310. if (idle != CPU_IDLE)
  4311. interval *= sd->busy_factor;
  4312. /* scale ms to jiffies */
  4313. interval = msecs_to_jiffies(interval);
  4314. interval = clamp(interval, 1UL, max_load_balance_interval);
  4315. need_serialize = sd->flags & SD_SERIALIZE;
  4316. if (need_serialize) {
  4317. if (!spin_trylock(&balancing))
  4318. goto out;
  4319. }
  4320. if (time_after_eq(jiffies, sd->last_balance + interval)) {
  4321. if (load_balance(cpu, rq, sd, idle, &balance)) {
  4322. /*
  4323. * We've pulled tasks over so either we're no
  4324. * longer idle.
  4325. */
  4326. idle = CPU_NOT_IDLE;
  4327. }
  4328. sd->last_balance = jiffies;
  4329. }
  4330. if (need_serialize)
  4331. spin_unlock(&balancing);
  4332. out:
  4333. if (time_after(next_balance, sd->last_balance + interval)) {
  4334. next_balance = sd->last_balance + interval;
  4335. update_next_balance = 1;
  4336. }
  4337. /*
  4338. * Stop the load balance at this level. There is another
  4339. * CPU in our sched group which is doing load balancing more
  4340. * actively.
  4341. */
  4342. if (!balance)
  4343. break;
  4344. }
  4345. rcu_read_unlock();
  4346. /*
  4347. * next_balance will be updated only when there is a need.
  4348. * When the cpu is attached to null domain for ex, it will not be
  4349. * updated.
  4350. */
  4351. if (likely(update_next_balance))
  4352. rq->next_balance = next_balance;
  4353. }
  4354. #ifdef CONFIG_NO_HZ
  4355. /*
  4356. * In CONFIG_NO_HZ case, the idle balance kickee will do the
  4357. * rebalancing for all the cpus for whom scheduler ticks are stopped.
  4358. */
  4359. static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
  4360. {
  4361. struct rq *this_rq = cpu_rq(this_cpu);
  4362. struct rq *rq;
  4363. int balance_cpu;
  4364. if (idle != CPU_IDLE ||
  4365. !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
  4366. goto end;
  4367. for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
  4368. if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
  4369. continue;
  4370. /*
  4371. * If this cpu gets work to do, stop the load balancing
  4372. * work being done for other cpus. Next load
  4373. * balancing owner will pick it up.
  4374. */
  4375. if (need_resched())
  4376. break;
  4377. rq = cpu_rq(balance_cpu);
  4378. raw_spin_lock_irq(&rq->lock);
  4379. update_rq_clock(rq);
  4380. update_idle_cpu_load(rq);
  4381. raw_spin_unlock_irq(&rq->lock);
  4382. rebalance_domains(balance_cpu, CPU_IDLE);
  4383. if (time_after(this_rq->next_balance, rq->next_balance))
  4384. this_rq->next_balance = rq->next_balance;
  4385. }
  4386. nohz.next_balance = this_rq->next_balance;
  4387. end:
  4388. clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
  4389. }
  4390. /*
  4391. * Current heuristic for kicking the idle load balancer in the presence
  4392. * of an idle cpu is the system.
  4393. * - This rq has more than one task.
  4394. * - At any scheduler domain level, this cpu's scheduler group has multiple
  4395. * busy cpu's exceeding the group's power.
  4396. * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
  4397. * domain span are idle.
  4398. */
  4399. static inline int nohz_kick_needed(struct rq *rq, int cpu)
  4400. {
  4401. unsigned long now = jiffies;
  4402. struct sched_domain *sd;
  4403. if (unlikely(idle_cpu(cpu)))
  4404. return 0;
  4405. /*
  4406. * We may be recently in ticked or tickless idle mode. At the first
  4407. * busy tick after returning from idle, we will update the busy stats.
  4408. */
  4409. set_cpu_sd_state_busy();
  4410. nohz_balance_exit_idle(cpu);
  4411. /*
  4412. * None are in tickless mode and hence no need for NOHZ idle load
  4413. * balancing.
  4414. */
  4415. if (likely(!atomic_read(&nohz.nr_cpus)))
  4416. return 0;
  4417. if (time_before(now, nohz.next_balance))
  4418. return 0;
  4419. if (rq->nr_running >= 2)
  4420. goto need_kick;
  4421. rcu_read_lock();
  4422. for_each_domain(cpu, sd) {
  4423. struct sched_group *sg = sd->groups;
  4424. struct sched_group_power *sgp = sg->sgp;
  4425. int nr_busy = atomic_read(&sgp->nr_busy_cpus);
  4426. if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
  4427. goto need_kick_unlock;
  4428. if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
  4429. && (cpumask_first_and(nohz.idle_cpus_mask,
  4430. sched_domain_span(sd)) < cpu))
  4431. goto need_kick_unlock;
  4432. if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
  4433. break;
  4434. }
  4435. rcu_read_unlock();
  4436. return 0;
  4437. need_kick_unlock:
  4438. rcu_read_unlock();
  4439. need_kick:
  4440. return 1;
  4441. }
  4442. #else
  4443. static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
  4444. #endif
  4445. /*
  4446. * run_rebalance_domains is triggered when needed from the scheduler tick.
  4447. * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
  4448. */
  4449. static void run_rebalance_domains(struct softirq_action *h)
  4450. {
  4451. int this_cpu = smp_processor_id();
  4452. struct rq *this_rq = cpu_rq(this_cpu);
  4453. enum cpu_idle_type idle = this_rq->idle_balance ?
  4454. CPU_IDLE : CPU_NOT_IDLE;
  4455. rebalance_domains(this_cpu, idle);
  4456. /*
  4457. * If this cpu has a pending nohz_balance_kick, then do the
  4458. * balancing on behalf of the other idle cpus whose ticks are
  4459. * stopped.
  4460. */
  4461. nohz_idle_balance(this_cpu, idle);
  4462. }
  4463. static inline int on_null_domain(int cpu)
  4464. {
  4465. return !rcu_dereference_sched(cpu_rq(cpu)->sd);
  4466. }
  4467. /*
  4468. * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
  4469. */
  4470. void trigger_load_balance(struct rq *rq, int cpu)
  4471. {
  4472. /* Don't need to rebalance while attached to NULL domain */
  4473. if (time_after_eq(jiffies, rq->next_balance) &&
  4474. likely(!on_null_domain(cpu)))
  4475. raise_softirq(SCHED_SOFTIRQ);
  4476. #ifdef CONFIG_NO_HZ
  4477. if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
  4478. nohz_balancer_kick(cpu);
  4479. #endif
  4480. }
  4481. static void rq_online_fair(struct rq *rq)
  4482. {
  4483. update_sysctl();
  4484. }
  4485. static void rq_offline_fair(struct rq *rq)
  4486. {
  4487. update_sysctl();
  4488. /* Ensure any throttled groups are reachable by pick_next_task */
  4489. unthrottle_offline_cfs_rqs(rq);
  4490. }
  4491. #endif /* CONFIG_SMP */
  4492. /*
  4493. * scheduler tick hitting a task of our scheduling class:
  4494. */
  4495. static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
  4496. {
  4497. struct cfs_rq *cfs_rq;
  4498. struct sched_entity *se = &curr->se;
  4499. for_each_sched_entity(se) {
  4500. cfs_rq = cfs_rq_of(se);
  4501. entity_tick(cfs_rq, se, queued);
  4502. }
  4503. update_rq_runnable_avg(rq, 1);
  4504. }
  4505. /*
  4506. * called on fork with the child task as argument from the parent's context
  4507. * - child not yet on the tasklist
  4508. * - preemption disabled
  4509. */
  4510. static void task_fork_fair(struct task_struct *p)
  4511. {
  4512. struct cfs_rq *cfs_rq;
  4513. struct sched_entity *se = &p->se, *curr;
  4514. int this_cpu = smp_processor_id();
  4515. struct rq *rq = this_rq();
  4516. unsigned long flags;
  4517. raw_spin_lock_irqsave(&rq->lock, flags);
  4518. update_rq_clock(rq);
  4519. cfs_rq = task_cfs_rq(current);
  4520. curr = cfs_rq->curr;
  4521. if (unlikely(task_cpu(p) != this_cpu)) {
  4522. rcu_read_lock();
  4523. __set_task_cpu(p, this_cpu);
  4524. rcu_read_unlock();
  4525. }
  4526. update_curr(cfs_rq);
  4527. if (curr)
  4528. se->vruntime = curr->vruntime;
  4529. place_entity(cfs_rq, se, 1);
  4530. if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
  4531. /*
  4532. * Upon rescheduling, sched_class::put_prev_task() will place
  4533. * 'current' within the tree based on its new key value.
  4534. */
  4535. swap(curr->vruntime, se->vruntime);
  4536. resched_task(rq->curr);
  4537. }
  4538. se->vruntime -= cfs_rq->min_vruntime;
  4539. raw_spin_unlock_irqrestore(&rq->lock, flags);
  4540. }
  4541. /*
  4542. * Priority of the task has changed. Check to see if we preempt
  4543. * the current task.
  4544. */
  4545. static void
  4546. prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
  4547. {
  4548. if (!p->se.on_rq)
  4549. return;
  4550. /*
  4551. * Reschedule if we are currently running on this runqueue and
  4552. * our priority decreased, or if we are not currently running on
  4553. * this runqueue and our priority is higher than the current's
  4554. */
  4555. if (rq->curr == p) {
  4556. if (p->prio > oldprio)
  4557. resched_task(rq->curr);
  4558. } else
  4559. check_preempt_curr(rq, p, 0);
  4560. }
  4561. static void switched_from_fair(struct rq *rq, struct task_struct *p)
  4562. {
  4563. struct sched_entity *se = &p->se;
  4564. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  4565. /*
  4566. * Ensure the task's vruntime is normalized, so that when its
  4567. * switched back to the fair class the enqueue_entity(.flags=0) will
  4568. * do the right thing.
  4569. *
  4570. * If it was on_rq, then the dequeue_entity(.flags=0) will already
  4571. * have normalized the vruntime, if it was !on_rq, then only when
  4572. * the task is sleeping will it still have non-normalized vruntime.
  4573. */
  4574. if (!se->on_rq && p->state != TASK_RUNNING) {
  4575. /*
  4576. * Fix up our vruntime so that the current sleep doesn't
  4577. * cause 'unlimited' sleep bonus.
  4578. */
  4579. place_entity(cfs_rq, se, 0);
  4580. se->vruntime -= cfs_rq->min_vruntime;
  4581. }
  4582. #if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
  4583. /*
  4584. * Remove our load from contribution when we leave sched_fair
  4585. * and ensure we don't carry in an old decay_count if we
  4586. * switch back.
  4587. */
  4588. if (p->se.avg.decay_count) {
  4589. struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
  4590. __synchronize_entity_decay(&p->se);
  4591. subtract_blocked_load_contrib(cfs_rq,
  4592. p->se.avg.load_avg_contrib);
  4593. }
  4594. #endif
  4595. }
  4596. /*
  4597. * We switched to the sched_fair class.
  4598. */
  4599. static void switched_to_fair(struct rq *rq, struct task_struct *p)
  4600. {
  4601. if (!p->se.on_rq)
  4602. return;
  4603. /*
  4604. * We were most likely switched from sched_rt, so
  4605. * kick off the schedule if running, otherwise just see
  4606. * if we can still preempt the current task.
  4607. */
  4608. if (rq->curr == p)
  4609. resched_task(rq->curr);
  4610. else
  4611. check_preempt_curr(rq, p, 0);
  4612. }
  4613. /* Account for a task changing its policy or group.
  4614. *
  4615. * This routine is mostly called to set cfs_rq->curr field when a task
  4616. * migrates between groups/classes.
  4617. */
  4618. static void set_curr_task_fair(struct rq *rq)
  4619. {
  4620. struct sched_entity *se = &rq->curr->se;
  4621. for_each_sched_entity(se) {
  4622. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  4623. set_next_entity(cfs_rq, se);
  4624. /* ensure bandwidth has been allocated on our new cfs_rq */
  4625. account_cfs_rq_runtime(cfs_rq, 0);
  4626. }
  4627. }
  4628. void init_cfs_rq(struct cfs_rq *cfs_rq)
  4629. {
  4630. cfs_rq->tasks_timeline = RB_ROOT;
  4631. cfs_rq->min_vruntime = (u64)(-(1LL << 20));
  4632. #ifndef CONFIG_64BIT
  4633. cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
  4634. #endif
  4635. #if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
  4636. atomic64_set(&cfs_rq->decay_counter, 1);
  4637. atomic64_set(&cfs_rq->removed_load, 0);
  4638. #endif
  4639. }
  4640. #ifdef CONFIG_FAIR_GROUP_SCHED
  4641. static void task_move_group_fair(struct task_struct *p, int on_rq)
  4642. {
  4643. struct cfs_rq *cfs_rq;
  4644. /*
  4645. * If the task was not on the rq at the time of this cgroup movement
  4646. * it must have been asleep, sleeping tasks keep their ->vruntime
  4647. * absolute on their old rq until wakeup (needed for the fair sleeper
  4648. * bonus in place_entity()).
  4649. *
  4650. * If it was on the rq, we've just 'preempted' it, which does convert
  4651. * ->vruntime to a relative base.
  4652. *
  4653. * Make sure both cases convert their relative position when migrating
  4654. * to another cgroup's rq. This does somewhat interfere with the
  4655. * fair sleeper stuff for the first placement, but who cares.
  4656. */
  4657. /*
  4658. * When !on_rq, vruntime of the task has usually NOT been normalized.
  4659. * But there are some cases where it has already been normalized:
  4660. *
  4661. * - Moving a forked child which is waiting for being woken up by
  4662. * wake_up_new_task().
  4663. * - Moving a task which has been woken up by try_to_wake_up() and
  4664. * waiting for actually being woken up by sched_ttwu_pending().
  4665. *
  4666. * To prevent boost or penalty in the new cfs_rq caused by delta
  4667. * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
  4668. */
  4669. if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
  4670. on_rq = 1;
  4671. if (!on_rq)
  4672. p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
  4673. set_task_rq(p, task_cpu(p));
  4674. if (!on_rq) {
  4675. cfs_rq = cfs_rq_of(&p->se);
  4676. p->se.vruntime += cfs_rq->min_vruntime;
  4677. #ifdef CONFIG_SMP
  4678. /*
  4679. * migrate_task_rq_fair() will have removed our previous
  4680. * contribution, but we must synchronize for ongoing future
  4681. * decay.
  4682. */
  4683. p->se.avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
  4684. cfs_rq->blocked_load_avg += p->se.avg.load_avg_contrib;
  4685. #endif
  4686. }
  4687. }
  4688. void free_fair_sched_group(struct task_group *tg)
  4689. {
  4690. int i;
  4691. destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
  4692. for_each_possible_cpu(i) {
  4693. if (tg->cfs_rq)
  4694. kfree(tg->cfs_rq[i]);
  4695. if (tg->se)
  4696. kfree(tg->se[i]);
  4697. }
  4698. kfree(tg->cfs_rq);
  4699. kfree(tg->se);
  4700. }
  4701. int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  4702. {
  4703. struct cfs_rq *cfs_rq;
  4704. struct sched_entity *se;
  4705. int i;
  4706. tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
  4707. if (!tg->cfs_rq)
  4708. goto err;
  4709. tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
  4710. if (!tg->se)
  4711. goto err;
  4712. tg->shares = NICE_0_LOAD;
  4713. init_cfs_bandwidth(tg_cfs_bandwidth(tg));
  4714. for_each_possible_cpu(i) {
  4715. cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
  4716. GFP_KERNEL, cpu_to_node(i));
  4717. if (!cfs_rq)
  4718. goto err;
  4719. se = kzalloc_node(sizeof(struct sched_entity),
  4720. GFP_KERNEL, cpu_to_node(i));
  4721. if (!se)
  4722. goto err_free_rq;
  4723. init_cfs_rq(cfs_rq);
  4724. init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
  4725. }
  4726. return 1;
  4727. err_free_rq:
  4728. kfree(cfs_rq);
  4729. err:
  4730. return 0;
  4731. }
  4732. void unregister_fair_sched_group(struct task_group *tg, int cpu)
  4733. {
  4734. struct rq *rq = cpu_rq(cpu);
  4735. unsigned long flags;
  4736. /*
  4737. * Only empty task groups can be destroyed; so we can speculatively
  4738. * check on_list without danger of it being re-added.
  4739. */
  4740. if (!tg->cfs_rq[cpu]->on_list)
  4741. return;
  4742. raw_spin_lock_irqsave(&rq->lock, flags);
  4743. list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
  4744. raw_spin_unlock_irqrestore(&rq->lock, flags);
  4745. }
  4746. void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
  4747. struct sched_entity *se, int cpu,
  4748. struct sched_entity *parent)
  4749. {
  4750. struct rq *rq = cpu_rq(cpu);
  4751. cfs_rq->tg = tg;
  4752. cfs_rq->rq = rq;
  4753. #ifdef CONFIG_SMP
  4754. /* allow initial update_cfs_load() to truncate */
  4755. cfs_rq->load_stamp = 1;
  4756. #endif
  4757. init_cfs_rq_runtime(cfs_rq);
  4758. tg->cfs_rq[cpu] = cfs_rq;
  4759. tg->se[cpu] = se;
  4760. /* se could be NULL for root_task_group */
  4761. if (!se)
  4762. return;
  4763. if (!parent)
  4764. se->cfs_rq = &rq->cfs;
  4765. else
  4766. se->cfs_rq = parent->my_q;
  4767. se->my_q = cfs_rq;
  4768. update_load_set(&se->load, 0);
  4769. se->parent = parent;
  4770. }
  4771. static DEFINE_MUTEX(shares_mutex);
  4772. int sched_group_set_shares(struct task_group *tg, unsigned long shares)
  4773. {
  4774. int i;
  4775. unsigned long flags;
  4776. /*
  4777. * We can't change the weight of the root cgroup.
  4778. */
  4779. if (!tg->se[0])
  4780. return -EINVAL;
  4781. shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
  4782. mutex_lock(&shares_mutex);
  4783. if (tg->shares == shares)
  4784. goto done;
  4785. tg->shares = shares;
  4786. for_each_possible_cpu(i) {
  4787. struct rq *rq = cpu_rq(i);
  4788. struct sched_entity *se;
  4789. se = tg->se[i];
  4790. /* Propagate contribution to hierarchy */
  4791. raw_spin_lock_irqsave(&rq->lock, flags);
  4792. for_each_sched_entity(se)
  4793. update_cfs_shares(group_cfs_rq(se));
  4794. raw_spin_unlock_irqrestore(&rq->lock, flags);
  4795. }
  4796. done:
  4797. mutex_unlock(&shares_mutex);
  4798. return 0;
  4799. }
  4800. #else /* CONFIG_FAIR_GROUP_SCHED */
  4801. void free_fair_sched_group(struct task_group *tg) { }
  4802. int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  4803. {
  4804. return 1;
  4805. }
  4806. void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
  4807. #endif /* CONFIG_FAIR_GROUP_SCHED */
  4808. static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
  4809. {
  4810. struct sched_entity *se = &task->se;
  4811. unsigned int rr_interval = 0;
  4812. /*
  4813. * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
  4814. * idle runqueue:
  4815. */
  4816. if (rq->cfs.load.weight)
  4817. rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
  4818. return rr_interval;
  4819. }
  4820. /*
  4821. * All the scheduling class methods:
  4822. */
  4823. const struct sched_class fair_sched_class = {
  4824. .next = &idle_sched_class,
  4825. .enqueue_task = enqueue_task_fair,
  4826. .dequeue_task = dequeue_task_fair,
  4827. .yield_task = yield_task_fair,
  4828. .yield_to_task = yield_to_task_fair,
  4829. .check_preempt_curr = check_preempt_wakeup,
  4830. .pick_next_task = pick_next_task_fair,
  4831. .put_prev_task = put_prev_task_fair,
  4832. #ifdef CONFIG_SMP
  4833. .select_task_rq = select_task_rq_fair,
  4834. .migrate_task_rq = migrate_task_rq_fair,
  4835. .rq_online = rq_online_fair,
  4836. .rq_offline = rq_offline_fair,
  4837. .task_waking = task_waking_fair,
  4838. #endif
  4839. .set_curr_task = set_curr_task_fair,
  4840. .task_tick = task_tick_fair,
  4841. .task_fork = task_fork_fair,
  4842. .prio_changed = prio_changed_fair,
  4843. .switched_from = switched_from_fair,
  4844. .switched_to = switched_to_fair,
  4845. .get_rr_interval = get_rr_interval_fair,
  4846. #ifdef CONFIG_FAIR_GROUP_SCHED
  4847. .task_move_group = task_move_group_fair,
  4848. #endif
  4849. };
  4850. #ifdef CONFIG_SCHED_DEBUG
  4851. void print_cfs_stats(struct seq_file *m, int cpu)
  4852. {
  4853. struct cfs_rq *cfs_rq;
  4854. rcu_read_lock();
  4855. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  4856. print_cfs_rq(m, cpu, cfs_rq);
  4857. rcu_read_unlock();
  4858. }
  4859. #endif
  4860. __init void init_sched_fair_class(void)
  4861. {
  4862. #ifdef CONFIG_SMP
  4863. open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
  4864. #ifdef CONFIG_NO_HZ
  4865. nohz.next_balance = jiffies;
  4866. zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
  4867. cpu_notifier(sched_ilb_notifier, 0);
  4868. #endif
  4869. #endif /* SMP */
  4870. }