cgroup.c 190 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244
  1. /*
  2. * Generic process-grouping system.
  3. *
  4. * Based originally on the cpuset system, extracted by Paul Menage
  5. * Copyright (C) 2006 Google, Inc
  6. *
  7. * Notifications support
  8. * Copyright (C) 2009 Nokia Corporation
  9. * Author: Kirill A. Shutemov
  10. *
  11. * Copyright notices from the original cpuset code:
  12. * --------------------------------------------------
  13. * Copyright (C) 2003 BULL SA.
  14. * Copyright (C) 2004-2006 Silicon Graphics, Inc.
  15. *
  16. * Portions derived from Patrick Mochel's sysfs code.
  17. * sysfs is Copyright (c) 2001-3 Patrick Mochel
  18. *
  19. * 2003-10-10 Written by Simon Derr.
  20. * 2003-10-22 Updates by Stephen Hemminger.
  21. * 2004 May-July Rework by Paul Jackson.
  22. * ---------------------------------------------------
  23. *
  24. * This file is subject to the terms and conditions of the GNU General Public
  25. * License. See the file COPYING in the main directory of the Linux
  26. * distribution for more details.
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include "cgroup-internal.h"
  30. #include <linux/bpf-cgroup.h>
  31. #include <linux/cred.h>
  32. #include <linux/errno.h>
  33. #include <linux/init_task.h>
  34. #include <linux/kernel.h>
  35. #include <linux/magic.h>
  36. #include <linux/mutex.h>
  37. #include <linux/mount.h>
  38. #include <linux/pagemap.h>
  39. #include <linux/proc_fs.h>
  40. #include <linux/rcupdate.h>
  41. #include <linux/sched.h>
  42. #include <linux/sched/task.h>
  43. #include <linux/slab.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/percpu-rwsem.h>
  46. #include <linux/string.h>
  47. #include <linux/hashtable.h>
  48. #include <linux/idr.h>
  49. #include <linux/kthread.h>
  50. #include <linux/atomic.h>
  51. #include <linux/cpuset.h>
  52. #include <linux/proc_ns.h>
  53. #include <linux/nsproxy.h>
  54. #include <linux/file.h>
  55. #include <linux/fs_parser.h>
  56. #include <linux/sched/cputime.h>
  57. #include <linux/sched/deadline.h>
  58. #include <linux/psi.h>
  59. #include <net/sock.h>
  60. #define CREATE_TRACE_POINTS
  61. #include <trace/events/cgroup.h>
  62. #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
  63. MAX_CFTYPE_NAME + 2)
  64. /* let's not notify more than 100 times per second */
  65. #define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100)
  66. /*
  67. * To avoid confusing the compiler (and generating warnings) with code
  68. * that attempts to access what would be a 0-element array (i.e. sized
  69. * to a potentially empty array when CGROUP_SUBSYS_COUNT == 0), this
  70. * constant expression can be added.
  71. */
  72. #define CGROUP_HAS_SUBSYS_CONFIG (CGROUP_SUBSYS_COUNT > 0)
  73. /*
  74. * cgroup_mutex is the master lock. Any modification to cgroup or its
  75. * hierarchy must be performed while holding it.
  76. *
  77. * css_set_lock protects task->cgroups pointer, the list of css_set
  78. * objects, and the chain of tasks off each css_set.
  79. *
  80. * These locks are exported if CONFIG_PROVE_RCU so that accessors in
  81. * cgroup.h can use them for lockdep annotations.
  82. */
  83. DEFINE_MUTEX(cgroup_mutex);
  84. DEFINE_SPINLOCK(css_set_lock);
  85. #if (defined CONFIG_PROVE_RCU || defined CONFIG_LOCKDEP)
  86. EXPORT_SYMBOL_GPL(cgroup_mutex);
  87. EXPORT_SYMBOL_GPL(css_set_lock);
  88. #endif
  89. DEFINE_SPINLOCK(trace_cgroup_path_lock);
  90. char trace_cgroup_path[TRACE_CGROUP_PATH_LEN];
  91. static bool cgroup_debug __read_mostly;
  92. /*
  93. * Protects cgroup_idr and css_idr so that IDs can be released without
  94. * grabbing cgroup_mutex.
  95. */
  96. static DEFINE_SPINLOCK(cgroup_idr_lock);
  97. /*
  98. * Protects cgroup_file->kn for !self csses. It synchronizes notifications
  99. * against file removal/re-creation across css hiding.
  100. */
  101. static DEFINE_SPINLOCK(cgroup_file_kn_lock);
  102. DEFINE_PERCPU_RWSEM(cgroup_threadgroup_rwsem);
  103. #define cgroup_assert_mutex_or_rcu_locked() \
  104. RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  105. !lockdep_is_held(&cgroup_mutex), \
  106. "cgroup_mutex or RCU read lock required");
  107. /*
  108. * cgroup destruction makes heavy use of work items and there can be a lot
  109. * of concurrent destructions. Use a separate workqueue so that cgroup
  110. * destruction work items don't end up filling up max_active of system_wq
  111. * which may lead to deadlock.
  112. *
  113. * A cgroup destruction should enqueue work sequentially to:
  114. * cgroup_offline_wq: use for css offline work
  115. * cgroup_release_wq: use for css release work
  116. * cgroup_free_wq: use for free work
  117. *
  118. * Rationale for using separate workqueues:
  119. * The cgroup root free work may depend on completion of other css offline
  120. * operations. If all tasks were enqueued to a single workqueue, this could
  121. * create a deadlock scenario where:
  122. * - Free work waits for other css offline work to complete.
  123. * - But other css offline work is queued after free work in the same queue.
  124. *
  125. * Example deadlock scenario with single workqueue (cgroup_destroy_wq):
  126. * 1. umount net_prio
  127. * 2. net_prio root destruction enqueues work to cgroup_destroy_wq (CPUx)
  128. * 3. perf_event CSS A offline enqueues work to same cgroup_destroy_wq (CPUx)
  129. * 4. net_prio cgroup_destroy_root->cgroup_lock_and_drain_offline.
  130. * 5. net_prio root destruction blocks waiting for perf_event CSS A offline,
  131. * which can never complete as it's behind in the same queue and
  132. * workqueue's max_active is 1.
  133. */
  134. static struct workqueue_struct *cgroup_offline_wq;
  135. static struct workqueue_struct *cgroup_release_wq;
  136. static struct workqueue_struct *cgroup_free_wq;
  137. /* generate an array of cgroup subsystem pointers */
  138. #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
  139. struct cgroup_subsys *cgroup_subsys[] = {
  140. #include <linux/cgroup_subsys.h>
  141. };
  142. #undef SUBSYS
  143. /* array of cgroup subsystem names */
  144. #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
  145. static const char *cgroup_subsys_name[] = {
  146. #include <linux/cgroup_subsys.h>
  147. };
  148. #undef SUBSYS
  149. /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
  150. #define SUBSYS(_x) \
  151. DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
  152. DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
  153. EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
  154. EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
  155. #include <linux/cgroup_subsys.h>
  156. #undef SUBSYS
  157. #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
  158. static struct static_key_true *cgroup_subsys_enabled_key[] = {
  159. #include <linux/cgroup_subsys.h>
  160. };
  161. #undef SUBSYS
  162. #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
  163. static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
  164. #include <linux/cgroup_subsys.h>
  165. };
  166. #undef SUBSYS
  167. static DEFINE_PER_CPU(struct cgroup_rstat_cpu, cgrp_dfl_root_rstat_cpu);
  168. /* the default hierarchy */
  169. struct cgroup_root cgrp_dfl_root = { .cgrp.rstat_cpu = &cgrp_dfl_root_rstat_cpu };
  170. EXPORT_SYMBOL_GPL(cgrp_dfl_root);
  171. /*
  172. * The default hierarchy always exists but is hidden until mounted for the
  173. * first time. This is for backward compatibility.
  174. */
  175. static bool cgrp_dfl_visible;
  176. /* some controllers are not supported in the default hierarchy */
  177. static u16 cgrp_dfl_inhibit_ss_mask;
  178. /* some controllers are implicitly enabled on the default hierarchy */
  179. static u16 cgrp_dfl_implicit_ss_mask;
  180. /* some controllers can be threaded on the default hierarchy */
  181. static u16 cgrp_dfl_threaded_ss_mask;
  182. /* The list of hierarchy roots */
  183. LIST_HEAD(cgroup_roots);
  184. static int cgroup_root_count;
  185. /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
  186. static DEFINE_IDR(cgroup_hierarchy_idr);
  187. /*
  188. * Assign a monotonically increasing serial number to csses. It guarantees
  189. * cgroups with bigger numbers are newer than those with smaller numbers.
  190. * Also, as csses are always appended to the parent's ->children list, it
  191. * guarantees that sibling csses are always sorted in the ascending serial
  192. * number order on the list. Protected by cgroup_mutex.
  193. */
  194. static u64 css_serial_nr_next = 1;
  195. /*
  196. * These bitmasks identify subsystems with specific features to avoid
  197. * having to do iterative checks repeatedly.
  198. */
  199. static u16 have_fork_callback __read_mostly;
  200. static u16 have_exit_callback __read_mostly;
  201. static u16 have_release_callback __read_mostly;
  202. static u16 have_canfork_callback __read_mostly;
  203. static bool have_favordynmods __ro_after_init = IS_ENABLED(CONFIG_CGROUP_FAVOR_DYNMODS);
  204. /* cgroup namespace for init task */
  205. struct cgroup_namespace init_cgroup_ns = {
  206. .ns.count = REFCOUNT_INIT(2),
  207. .user_ns = &init_user_ns,
  208. .ns.ops = &cgroupns_operations,
  209. .ns.inum = PROC_CGROUP_INIT_INO,
  210. .root_cset = &init_css_set,
  211. };
  212. static struct file_system_type cgroup2_fs_type;
  213. static struct cftype cgroup_base_files[];
  214. static struct cftype cgroup_psi_files[];
  215. /* cgroup optional features */
  216. enum cgroup_opt_features {
  217. #ifdef CONFIG_PSI
  218. OPT_FEATURE_PRESSURE,
  219. #endif
  220. OPT_FEATURE_COUNT
  221. };
  222. static const char *cgroup_opt_feature_names[OPT_FEATURE_COUNT] = {
  223. #ifdef CONFIG_PSI
  224. "pressure",
  225. #endif
  226. };
  227. static u16 cgroup_feature_disable_mask __read_mostly;
  228. static int cgroup_apply_control(struct cgroup *cgrp);
  229. static void cgroup_finalize_control(struct cgroup *cgrp, int ret);
  230. static void css_task_iter_skip(struct css_task_iter *it,
  231. struct task_struct *task);
  232. static int cgroup_destroy_locked(struct cgroup *cgrp);
  233. static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
  234. struct cgroup_subsys *ss);
  235. static void css_release(struct percpu_ref *ref);
  236. static void kill_css(struct cgroup_subsys_state *css);
  237. static int cgroup_addrm_files(struct cgroup_subsys_state *css,
  238. struct cgroup *cgrp, struct cftype cfts[],
  239. bool is_add);
  240. #ifdef CONFIG_DEBUG_CGROUP_REF
  241. #define CGROUP_REF_FN_ATTRS noinline
  242. #define CGROUP_REF_EXPORT(fn) EXPORT_SYMBOL_GPL(fn);
  243. #include <linux/cgroup_refcnt.h>
  244. #endif
  245. /**
  246. * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
  247. * @ssid: subsys ID of interest
  248. *
  249. * cgroup_subsys_enabled() can only be used with literal subsys names which
  250. * is fine for individual subsystems but unsuitable for cgroup core. This
  251. * is slower static_key_enabled() based test indexed by @ssid.
  252. */
  253. bool cgroup_ssid_enabled(int ssid)
  254. {
  255. if (!CGROUP_HAS_SUBSYS_CONFIG)
  256. return false;
  257. return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
  258. }
  259. /**
  260. * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
  261. * @cgrp: the cgroup of interest
  262. *
  263. * The default hierarchy is the v2 interface of cgroup and this function
  264. * can be used to test whether a cgroup is on the default hierarchy for
  265. * cases where a subsystem should behave differently depending on the
  266. * interface version.
  267. *
  268. * List of changed behaviors:
  269. *
  270. * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
  271. * and "name" are disallowed.
  272. *
  273. * - When mounting an existing superblock, mount options should match.
  274. *
  275. * - rename(2) is disallowed.
  276. *
  277. * - "tasks" is removed. Everything should be at process granularity. Use
  278. * "cgroup.procs" instead.
  279. *
  280. * - "cgroup.procs" is not sorted. pids will be unique unless they got
  281. * recycled in-between reads.
  282. *
  283. * - "release_agent" and "notify_on_release" are removed. Replacement
  284. * notification mechanism will be implemented.
  285. *
  286. * - "cgroup.clone_children" is removed.
  287. *
  288. * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
  289. * and its descendants contain no task; otherwise, 1. The file also
  290. * generates kernfs notification which can be monitored through poll and
  291. * [di]notify when the value of the file changes.
  292. *
  293. * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
  294. * take masks of ancestors with non-empty cpus/mems, instead of being
  295. * moved to an ancestor.
  296. *
  297. * - cpuset: a task can be moved into an empty cpuset, and again it takes
  298. * masks of ancestors.
  299. *
  300. * - blkcg: blk-throttle becomes properly hierarchical.
  301. */
  302. bool cgroup_on_dfl(const struct cgroup *cgrp)
  303. {
  304. return cgrp->root == &cgrp_dfl_root;
  305. }
  306. /* IDR wrappers which synchronize using cgroup_idr_lock */
  307. static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
  308. gfp_t gfp_mask)
  309. {
  310. int ret;
  311. idr_preload(gfp_mask);
  312. spin_lock_bh(&cgroup_idr_lock);
  313. ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
  314. spin_unlock_bh(&cgroup_idr_lock);
  315. idr_preload_end();
  316. return ret;
  317. }
  318. static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
  319. {
  320. void *ret;
  321. spin_lock_bh(&cgroup_idr_lock);
  322. ret = idr_replace(idr, ptr, id);
  323. spin_unlock_bh(&cgroup_idr_lock);
  324. return ret;
  325. }
  326. static void cgroup_idr_remove(struct idr *idr, int id)
  327. {
  328. spin_lock_bh(&cgroup_idr_lock);
  329. idr_remove(idr, id);
  330. spin_unlock_bh(&cgroup_idr_lock);
  331. }
  332. static bool cgroup_has_tasks(struct cgroup *cgrp)
  333. {
  334. return cgrp->nr_populated_csets;
  335. }
  336. static bool cgroup_is_threaded(struct cgroup *cgrp)
  337. {
  338. return cgrp->dom_cgrp != cgrp;
  339. }
  340. /* can @cgrp host both domain and threaded children? */
  341. static bool cgroup_is_mixable(struct cgroup *cgrp)
  342. {
  343. /*
  344. * Root isn't under domain level resource control exempting it from
  345. * the no-internal-process constraint, so it can serve as a thread
  346. * root and a parent of resource domains at the same time.
  347. */
  348. return !cgroup_parent(cgrp);
  349. }
  350. /* can @cgrp become a thread root? Should always be true for a thread root */
  351. static bool cgroup_can_be_thread_root(struct cgroup *cgrp)
  352. {
  353. /* mixables don't care */
  354. if (cgroup_is_mixable(cgrp))
  355. return true;
  356. /* domain roots can't be nested under threaded */
  357. if (cgroup_is_threaded(cgrp))
  358. return false;
  359. /* can only have either domain or threaded children */
  360. if (cgrp->nr_populated_domain_children)
  361. return false;
  362. /* and no domain controllers can be enabled */
  363. if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
  364. return false;
  365. return true;
  366. }
  367. /* is @cgrp root of a threaded subtree? */
  368. static bool cgroup_is_thread_root(struct cgroup *cgrp)
  369. {
  370. /* thread root should be a domain */
  371. if (cgroup_is_threaded(cgrp))
  372. return false;
  373. /* a domain w/ threaded children is a thread root */
  374. if (cgrp->nr_threaded_children)
  375. return true;
  376. /*
  377. * A domain which has tasks and explicit threaded controllers
  378. * enabled is a thread root.
  379. */
  380. if (cgroup_has_tasks(cgrp) &&
  381. (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask))
  382. return true;
  383. return false;
  384. }
  385. /* a domain which isn't connected to the root w/o brekage can't be used */
  386. static bool cgroup_is_valid_domain(struct cgroup *cgrp)
  387. {
  388. /* the cgroup itself can be a thread root */
  389. if (cgroup_is_threaded(cgrp))
  390. return false;
  391. /* but the ancestors can't be unless mixable */
  392. while ((cgrp = cgroup_parent(cgrp))) {
  393. if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp))
  394. return false;
  395. if (cgroup_is_threaded(cgrp))
  396. return false;
  397. }
  398. return true;
  399. }
  400. /* subsystems visibly enabled on a cgroup */
  401. static u16 cgroup_control(struct cgroup *cgrp)
  402. {
  403. struct cgroup *parent = cgroup_parent(cgrp);
  404. u16 root_ss_mask = cgrp->root->subsys_mask;
  405. if (parent) {
  406. u16 ss_mask = parent->subtree_control;
  407. /* threaded cgroups can only have threaded controllers */
  408. if (cgroup_is_threaded(cgrp))
  409. ss_mask &= cgrp_dfl_threaded_ss_mask;
  410. return ss_mask;
  411. }
  412. if (cgroup_on_dfl(cgrp))
  413. root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask |
  414. cgrp_dfl_implicit_ss_mask);
  415. return root_ss_mask;
  416. }
  417. /* subsystems enabled on a cgroup */
  418. static u16 cgroup_ss_mask(struct cgroup *cgrp)
  419. {
  420. struct cgroup *parent = cgroup_parent(cgrp);
  421. if (parent) {
  422. u16 ss_mask = parent->subtree_ss_mask;
  423. /* threaded cgroups can only have threaded controllers */
  424. if (cgroup_is_threaded(cgrp))
  425. ss_mask &= cgrp_dfl_threaded_ss_mask;
  426. return ss_mask;
  427. }
  428. return cgrp->root->subsys_mask;
  429. }
  430. /**
  431. * cgroup_css - obtain a cgroup's css for the specified subsystem
  432. * @cgrp: the cgroup of interest
  433. * @ss: the subsystem of interest (%NULL returns @cgrp->self)
  434. *
  435. * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
  436. * function must be called either under cgroup_mutex or rcu_read_lock() and
  437. * the caller is responsible for pinning the returned css if it wants to
  438. * keep accessing it outside the said locks. This function may return
  439. * %NULL if @cgrp doesn't have @subsys_id enabled.
  440. */
  441. static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
  442. struct cgroup_subsys *ss)
  443. {
  444. if (CGROUP_HAS_SUBSYS_CONFIG && ss)
  445. return rcu_dereference_check(cgrp->subsys[ss->id],
  446. lockdep_is_held(&cgroup_mutex));
  447. else
  448. return &cgrp->self;
  449. }
  450. /**
  451. * cgroup_e_css_by_mask - obtain a cgroup's effective css for the specified ss
  452. * @cgrp: the cgroup of interest
  453. * @ss: the subsystem of interest (%NULL returns @cgrp->self)
  454. *
  455. * Similar to cgroup_css() but returns the effective css, which is defined
  456. * as the matching css of the nearest ancestor including self which has @ss
  457. * enabled. If @ss is associated with the hierarchy @cgrp is on, this
  458. * function is guaranteed to return non-NULL css.
  459. */
  460. static struct cgroup_subsys_state *cgroup_e_css_by_mask(struct cgroup *cgrp,
  461. struct cgroup_subsys *ss)
  462. {
  463. lockdep_assert_held(&cgroup_mutex);
  464. if (!ss)
  465. return &cgrp->self;
  466. /*
  467. * This function is used while updating css associations and thus
  468. * can't test the csses directly. Test ss_mask.
  469. */
  470. while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) {
  471. cgrp = cgroup_parent(cgrp);
  472. if (!cgrp)
  473. return NULL;
  474. }
  475. return cgroup_css(cgrp, ss);
  476. }
  477. /**
  478. * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
  479. * @cgrp: the cgroup of interest
  480. * @ss: the subsystem of interest
  481. *
  482. * Find and get the effective css of @cgrp for @ss. The effective css is
  483. * defined as the matching css of the nearest ancestor including self which
  484. * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
  485. * the root css is returned, so this function always returns a valid css.
  486. *
  487. * The returned css is not guaranteed to be online, and therefore it is the
  488. * callers responsibility to try get a reference for it.
  489. */
  490. struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
  491. struct cgroup_subsys *ss)
  492. {
  493. struct cgroup_subsys_state *css;
  494. if (!CGROUP_HAS_SUBSYS_CONFIG)
  495. return NULL;
  496. do {
  497. css = cgroup_css(cgrp, ss);
  498. if (css)
  499. return css;
  500. cgrp = cgroup_parent(cgrp);
  501. } while (cgrp);
  502. return init_css_set.subsys[ss->id];
  503. }
  504. /**
  505. * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
  506. * @cgrp: the cgroup of interest
  507. * @ss: the subsystem of interest
  508. *
  509. * Find and get the effective css of @cgrp for @ss. The effective css is
  510. * defined as the matching css of the nearest ancestor including self which
  511. * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
  512. * the root css is returned, so this function always returns a valid css.
  513. * The returned css must be put using css_put().
  514. */
  515. struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
  516. struct cgroup_subsys *ss)
  517. {
  518. struct cgroup_subsys_state *css;
  519. if (!CGROUP_HAS_SUBSYS_CONFIG)
  520. return NULL;
  521. rcu_read_lock();
  522. do {
  523. css = cgroup_css(cgrp, ss);
  524. if (css && css_tryget_online(css))
  525. goto out_unlock;
  526. cgrp = cgroup_parent(cgrp);
  527. } while (cgrp);
  528. css = init_css_set.subsys[ss->id];
  529. css_get(css);
  530. out_unlock:
  531. rcu_read_unlock();
  532. return css;
  533. }
  534. EXPORT_SYMBOL_GPL(cgroup_get_e_css);
  535. static void cgroup_get_live(struct cgroup *cgrp)
  536. {
  537. WARN_ON_ONCE(cgroup_is_dead(cgrp));
  538. cgroup_get(cgrp);
  539. }
  540. /**
  541. * __cgroup_task_count - count the number of tasks in a cgroup. The caller
  542. * is responsible for taking the css_set_lock.
  543. * @cgrp: the cgroup in question
  544. */
  545. int __cgroup_task_count(const struct cgroup *cgrp)
  546. {
  547. int count = 0;
  548. struct cgrp_cset_link *link;
  549. lockdep_assert_held(&css_set_lock);
  550. list_for_each_entry(link, &cgrp->cset_links, cset_link)
  551. count += link->cset->nr_tasks;
  552. return count;
  553. }
  554. /**
  555. * cgroup_task_count - count the number of tasks in a cgroup.
  556. * @cgrp: the cgroup in question
  557. */
  558. int cgroup_task_count(const struct cgroup *cgrp)
  559. {
  560. int count;
  561. spin_lock_irq(&css_set_lock);
  562. count = __cgroup_task_count(cgrp);
  563. spin_unlock_irq(&css_set_lock);
  564. return count;
  565. }
  566. struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
  567. {
  568. struct cgroup *cgrp = of->kn->parent->priv;
  569. struct cftype *cft = of_cft(of);
  570. /*
  571. * This is open and unprotected implementation of cgroup_css().
  572. * seq_css() is only called from a kernfs file operation which has
  573. * an active reference on the file. Because all the subsystem
  574. * files are drained before a css is disassociated with a cgroup,
  575. * the matching css from the cgroup's subsys table is guaranteed to
  576. * be and stay valid until the enclosing operation is complete.
  577. */
  578. if (CGROUP_HAS_SUBSYS_CONFIG && cft->ss)
  579. return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
  580. else
  581. return &cgrp->self;
  582. }
  583. EXPORT_SYMBOL_GPL(of_css);
  584. /**
  585. * for_each_css - iterate all css's of a cgroup
  586. * @css: the iteration cursor
  587. * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
  588. * @cgrp: the target cgroup to iterate css's of
  589. *
  590. * Should be called under cgroup_mutex.
  591. */
  592. #define for_each_css(css, ssid, cgrp) \
  593. for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
  594. if (!((css) = rcu_dereference_check( \
  595. (cgrp)->subsys[(ssid)], \
  596. lockdep_is_held(&cgroup_mutex)))) { } \
  597. else
  598. /**
  599. * do_each_subsys_mask - filter for_each_subsys with a bitmask
  600. * @ss: the iteration cursor
  601. * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
  602. * @ss_mask: the bitmask
  603. *
  604. * The block will only run for cases where the ssid-th bit (1 << ssid) of
  605. * @ss_mask is set.
  606. */
  607. #define do_each_subsys_mask(ss, ssid, ss_mask) do { \
  608. unsigned long __ss_mask = (ss_mask); \
  609. if (!CGROUP_HAS_SUBSYS_CONFIG) { \
  610. (ssid) = 0; \
  611. break; \
  612. } \
  613. for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
  614. (ss) = cgroup_subsys[ssid]; \
  615. {
  616. #define while_each_subsys_mask() \
  617. } \
  618. } \
  619. } while (false)
  620. /* iterate over child cgrps, lock should be held throughout iteration */
  621. #define cgroup_for_each_live_child(child, cgrp) \
  622. list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
  623. if (({ lockdep_assert_held(&cgroup_mutex); \
  624. cgroup_is_dead(child); })) \
  625. ; \
  626. else
  627. /* walk live descendants in pre order */
  628. #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
  629. css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
  630. if (({ lockdep_assert_held(&cgroup_mutex); \
  631. (dsct) = (d_css)->cgroup; \
  632. cgroup_is_dead(dsct); })) \
  633. ; \
  634. else
  635. /* walk live descendants in postorder */
  636. #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
  637. css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
  638. if (({ lockdep_assert_held(&cgroup_mutex); \
  639. (dsct) = (d_css)->cgroup; \
  640. cgroup_is_dead(dsct); })) \
  641. ; \
  642. else
  643. /*
  644. * The default css_set - used by init and its children prior to any
  645. * hierarchies being mounted. It contains a pointer to the root state
  646. * for each subsystem. Also used to anchor the list of css_sets. Not
  647. * reference-counted, to improve performance when child cgroups
  648. * haven't been created.
  649. */
  650. struct css_set init_css_set = {
  651. .refcount = REFCOUNT_INIT(1),
  652. .dom_cset = &init_css_set,
  653. .tasks = LIST_HEAD_INIT(init_css_set.tasks),
  654. .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
  655. .dying_tasks = LIST_HEAD_INIT(init_css_set.dying_tasks),
  656. .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
  657. .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets),
  658. .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
  659. .mg_src_preload_node = LIST_HEAD_INIT(init_css_set.mg_src_preload_node),
  660. .mg_dst_preload_node = LIST_HEAD_INIT(init_css_set.mg_dst_preload_node),
  661. .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
  662. /*
  663. * The following field is re-initialized when this cset gets linked
  664. * in cgroup_init(). However, let's initialize the field
  665. * statically too so that the default cgroup can be accessed safely
  666. * early during boot.
  667. */
  668. .dfl_cgrp = &cgrp_dfl_root.cgrp,
  669. };
  670. static int css_set_count = 1; /* 1 for init_css_set */
  671. static bool css_set_threaded(struct css_set *cset)
  672. {
  673. return cset->dom_cset != cset;
  674. }
  675. /**
  676. * css_set_populated - does a css_set contain any tasks?
  677. * @cset: target css_set
  678. *
  679. * css_set_populated() should be the same as !!cset->nr_tasks at steady
  680. * state. However, css_set_populated() can be called while a task is being
  681. * added to or removed from the linked list before the nr_tasks is
  682. * properly updated. Hence, we can't just look at ->nr_tasks here.
  683. */
  684. static bool css_set_populated(struct css_set *cset)
  685. {
  686. lockdep_assert_held(&css_set_lock);
  687. return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
  688. }
  689. /**
  690. * cgroup_update_populated - update the populated count of a cgroup
  691. * @cgrp: the target cgroup
  692. * @populated: inc or dec populated count
  693. *
  694. * One of the css_sets associated with @cgrp is either getting its first
  695. * task or losing the last. Update @cgrp->nr_populated_* accordingly. The
  696. * count is propagated towards root so that a given cgroup's
  697. * nr_populated_children is zero iff none of its descendants contain any
  698. * tasks.
  699. *
  700. * @cgrp's interface file "cgroup.populated" is zero if both
  701. * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and
  702. * 1 otherwise. When the sum changes from or to zero, userland is notified
  703. * that the content of the interface file has changed. This can be used to
  704. * detect when @cgrp and its descendants become populated or empty.
  705. */
  706. static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
  707. {
  708. struct cgroup *child = NULL;
  709. int adj = populated ? 1 : -1;
  710. lockdep_assert_held(&css_set_lock);
  711. do {
  712. bool was_populated = cgroup_is_populated(cgrp);
  713. if (!child) {
  714. cgrp->nr_populated_csets += adj;
  715. } else {
  716. if (cgroup_is_threaded(child))
  717. cgrp->nr_populated_threaded_children += adj;
  718. else
  719. cgrp->nr_populated_domain_children += adj;
  720. }
  721. if (was_populated == cgroup_is_populated(cgrp))
  722. break;
  723. cgroup1_check_for_release(cgrp);
  724. TRACE_CGROUP_PATH(notify_populated, cgrp,
  725. cgroup_is_populated(cgrp));
  726. cgroup_file_notify(&cgrp->events_file);
  727. child = cgrp;
  728. cgrp = cgroup_parent(cgrp);
  729. } while (cgrp);
  730. }
  731. /**
  732. * css_set_update_populated - update populated state of a css_set
  733. * @cset: target css_set
  734. * @populated: whether @cset is populated or depopulated
  735. *
  736. * @cset is either getting the first task or losing the last. Update the
  737. * populated counters of all associated cgroups accordingly.
  738. */
  739. static void css_set_update_populated(struct css_set *cset, bool populated)
  740. {
  741. struct cgrp_cset_link *link;
  742. lockdep_assert_held(&css_set_lock);
  743. list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
  744. cgroup_update_populated(link->cgrp, populated);
  745. }
  746. /*
  747. * @task is leaving, advance task iterators which are pointing to it so
  748. * that they can resume at the next position. Advancing an iterator might
  749. * remove it from the list, use safe walk. See css_task_iter_skip() for
  750. * details.
  751. */
  752. static void css_set_skip_task_iters(struct css_set *cset,
  753. struct task_struct *task)
  754. {
  755. struct css_task_iter *it, *pos;
  756. list_for_each_entry_safe(it, pos, &cset->task_iters, iters_node)
  757. css_task_iter_skip(it, task);
  758. }
  759. /**
  760. * css_set_move_task - move a task from one css_set to another
  761. * @task: task being moved
  762. * @from_cset: css_set @task currently belongs to (may be NULL)
  763. * @to_cset: new css_set @task is being moved to (may be NULL)
  764. * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
  765. *
  766. * Move @task from @from_cset to @to_cset. If @task didn't belong to any
  767. * css_set, @from_cset can be NULL. If @task is being disassociated
  768. * instead of moved, @to_cset can be NULL.
  769. *
  770. * This function automatically handles populated counter updates and
  771. * css_task_iter adjustments but the caller is responsible for managing
  772. * @from_cset and @to_cset's reference counts.
  773. */
  774. static void css_set_move_task(struct task_struct *task,
  775. struct css_set *from_cset, struct css_set *to_cset,
  776. bool use_mg_tasks)
  777. {
  778. lockdep_assert_held(&css_set_lock);
  779. if (to_cset && !css_set_populated(to_cset))
  780. css_set_update_populated(to_cset, true);
  781. if (from_cset) {
  782. WARN_ON_ONCE(list_empty(&task->cg_list));
  783. css_set_skip_task_iters(from_cset, task);
  784. list_del_init(&task->cg_list);
  785. if (!css_set_populated(from_cset))
  786. css_set_update_populated(from_cset, false);
  787. } else {
  788. WARN_ON_ONCE(!list_empty(&task->cg_list));
  789. }
  790. if (to_cset) {
  791. /*
  792. * We are synchronized through cgroup_threadgroup_rwsem
  793. * against PF_EXITING setting such that we can't race
  794. * against cgroup_exit()/cgroup_free() dropping the css_set.
  795. */
  796. WARN_ON_ONCE(task->flags & PF_EXITING);
  797. cgroup_move_task(task, to_cset);
  798. list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
  799. &to_cset->tasks);
  800. }
  801. }
  802. /*
  803. * hash table for cgroup groups. This improves the performance to find
  804. * an existing css_set. This hash doesn't (currently) take into
  805. * account cgroups in empty hierarchies.
  806. */
  807. #define CSS_SET_HASH_BITS 7
  808. static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
  809. static unsigned long css_set_hash(struct cgroup_subsys_state **css)
  810. {
  811. unsigned long key = 0UL;
  812. struct cgroup_subsys *ss;
  813. int i;
  814. for_each_subsys(ss, i)
  815. key += (unsigned long)css[i];
  816. key = (key >> 16) ^ key;
  817. return key;
  818. }
  819. void put_css_set_locked(struct css_set *cset)
  820. {
  821. struct cgrp_cset_link *link, *tmp_link;
  822. struct cgroup_subsys *ss;
  823. int ssid;
  824. lockdep_assert_held(&css_set_lock);
  825. if (!refcount_dec_and_test(&cset->refcount))
  826. return;
  827. WARN_ON_ONCE(!list_empty(&cset->threaded_csets));
  828. /* This css_set is dead. Unlink it and release cgroup and css refs */
  829. for_each_subsys(ss, ssid) {
  830. list_del(&cset->e_cset_node[ssid]);
  831. css_put(cset->subsys[ssid]);
  832. }
  833. hash_del(&cset->hlist);
  834. css_set_count--;
  835. list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
  836. list_del(&link->cset_link);
  837. list_del(&link->cgrp_link);
  838. if (cgroup_parent(link->cgrp))
  839. cgroup_put(link->cgrp);
  840. kfree(link);
  841. }
  842. if (css_set_threaded(cset)) {
  843. list_del(&cset->threaded_csets_node);
  844. put_css_set_locked(cset->dom_cset);
  845. }
  846. kfree_rcu(cset, rcu_head);
  847. }
  848. /**
  849. * compare_css_sets - helper function for find_existing_css_set().
  850. * @cset: candidate css_set being tested
  851. * @old_cset: existing css_set for a task
  852. * @new_cgrp: cgroup that's being entered by the task
  853. * @template: desired set of css pointers in css_set (pre-calculated)
  854. *
  855. * Returns true if "cset" matches "old_cset" except for the hierarchy
  856. * which "new_cgrp" belongs to, for which it should match "new_cgrp".
  857. */
  858. static bool compare_css_sets(struct css_set *cset,
  859. struct css_set *old_cset,
  860. struct cgroup *new_cgrp,
  861. struct cgroup_subsys_state *template[])
  862. {
  863. struct cgroup *new_dfl_cgrp;
  864. struct list_head *l1, *l2;
  865. /*
  866. * On the default hierarchy, there can be csets which are
  867. * associated with the same set of cgroups but different csses.
  868. * Let's first ensure that csses match.
  869. */
  870. if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
  871. return false;
  872. /* @cset's domain should match the default cgroup's */
  873. if (cgroup_on_dfl(new_cgrp))
  874. new_dfl_cgrp = new_cgrp;
  875. else
  876. new_dfl_cgrp = old_cset->dfl_cgrp;
  877. if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp)
  878. return false;
  879. /*
  880. * Compare cgroup pointers in order to distinguish between
  881. * different cgroups in hierarchies. As different cgroups may
  882. * share the same effective css, this comparison is always
  883. * necessary.
  884. */
  885. l1 = &cset->cgrp_links;
  886. l2 = &old_cset->cgrp_links;
  887. while (1) {
  888. struct cgrp_cset_link *link1, *link2;
  889. struct cgroup *cgrp1, *cgrp2;
  890. l1 = l1->next;
  891. l2 = l2->next;
  892. /* See if we reached the end - both lists are equal length. */
  893. if (l1 == &cset->cgrp_links) {
  894. BUG_ON(l2 != &old_cset->cgrp_links);
  895. break;
  896. } else {
  897. BUG_ON(l2 == &old_cset->cgrp_links);
  898. }
  899. /* Locate the cgroups associated with these links. */
  900. link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
  901. link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
  902. cgrp1 = link1->cgrp;
  903. cgrp2 = link2->cgrp;
  904. /* Hierarchies should be linked in the same order. */
  905. BUG_ON(cgrp1->root != cgrp2->root);
  906. /*
  907. * If this hierarchy is the hierarchy of the cgroup
  908. * that's changing, then we need to check that this
  909. * css_set points to the new cgroup; if it's any other
  910. * hierarchy, then this css_set should point to the
  911. * same cgroup as the old css_set.
  912. */
  913. if (cgrp1->root == new_cgrp->root) {
  914. if (cgrp1 != new_cgrp)
  915. return false;
  916. } else {
  917. if (cgrp1 != cgrp2)
  918. return false;
  919. }
  920. }
  921. return true;
  922. }
  923. /**
  924. * find_existing_css_set - init css array and find the matching css_set
  925. * @old_cset: the css_set that we're using before the cgroup transition
  926. * @cgrp: the cgroup that we're moving into
  927. * @template: out param for the new set of csses, should be clear on entry
  928. */
  929. static struct css_set *find_existing_css_set(struct css_set *old_cset,
  930. struct cgroup *cgrp,
  931. struct cgroup_subsys_state **template)
  932. {
  933. struct cgroup_root *root = cgrp->root;
  934. struct cgroup_subsys *ss;
  935. struct css_set *cset;
  936. unsigned long key;
  937. int i;
  938. /*
  939. * Build the set of subsystem state objects that we want to see in the
  940. * new css_set. While subsystems can change globally, the entries here
  941. * won't change, so no need for locking.
  942. */
  943. for_each_subsys(ss, i) {
  944. if (root->subsys_mask & (1UL << i)) {
  945. /*
  946. * @ss is in this hierarchy, so we want the
  947. * effective css from @cgrp.
  948. */
  949. template[i] = cgroup_e_css_by_mask(cgrp, ss);
  950. } else {
  951. /*
  952. * @ss is not in this hierarchy, so we don't want
  953. * to change the css.
  954. */
  955. template[i] = old_cset->subsys[i];
  956. }
  957. }
  958. key = css_set_hash(template);
  959. hash_for_each_possible(css_set_table, cset, hlist, key) {
  960. if (!compare_css_sets(cset, old_cset, cgrp, template))
  961. continue;
  962. /* This css_set matches what we need */
  963. return cset;
  964. }
  965. /* No existing cgroup group matched */
  966. return NULL;
  967. }
  968. static void free_cgrp_cset_links(struct list_head *links_to_free)
  969. {
  970. struct cgrp_cset_link *link, *tmp_link;
  971. list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
  972. list_del(&link->cset_link);
  973. kfree(link);
  974. }
  975. }
  976. /**
  977. * allocate_cgrp_cset_links - allocate cgrp_cset_links
  978. * @count: the number of links to allocate
  979. * @tmp_links: list_head the allocated links are put on
  980. *
  981. * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
  982. * through ->cset_link. Returns 0 on success or -errno.
  983. */
  984. static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
  985. {
  986. struct cgrp_cset_link *link;
  987. int i;
  988. INIT_LIST_HEAD(tmp_links);
  989. for (i = 0; i < count; i++) {
  990. link = kzalloc(sizeof(*link), GFP_KERNEL);
  991. if (!link) {
  992. free_cgrp_cset_links(tmp_links);
  993. return -ENOMEM;
  994. }
  995. list_add(&link->cset_link, tmp_links);
  996. }
  997. return 0;
  998. }
  999. /**
  1000. * link_css_set - a helper function to link a css_set to a cgroup
  1001. * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
  1002. * @cset: the css_set to be linked
  1003. * @cgrp: the destination cgroup
  1004. */
  1005. static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
  1006. struct cgroup *cgrp)
  1007. {
  1008. struct cgrp_cset_link *link;
  1009. BUG_ON(list_empty(tmp_links));
  1010. if (cgroup_on_dfl(cgrp))
  1011. cset->dfl_cgrp = cgrp;
  1012. link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
  1013. link->cset = cset;
  1014. link->cgrp = cgrp;
  1015. /*
  1016. * Always add links to the tail of the lists so that the lists are
  1017. * in chronological order.
  1018. */
  1019. list_move_tail(&link->cset_link, &cgrp->cset_links);
  1020. list_add_tail(&link->cgrp_link, &cset->cgrp_links);
  1021. if (cgroup_parent(cgrp))
  1022. cgroup_get_live(cgrp);
  1023. }
  1024. /**
  1025. * find_css_set - return a new css_set with one cgroup updated
  1026. * @old_cset: the baseline css_set
  1027. * @cgrp: the cgroup to be updated
  1028. *
  1029. * Return a new css_set that's equivalent to @old_cset, but with @cgrp
  1030. * substituted into the appropriate hierarchy.
  1031. */
  1032. static struct css_set *find_css_set(struct css_set *old_cset,
  1033. struct cgroup *cgrp)
  1034. {
  1035. struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
  1036. struct css_set *cset;
  1037. struct list_head tmp_links;
  1038. struct cgrp_cset_link *link;
  1039. struct cgroup_subsys *ss;
  1040. unsigned long key;
  1041. int ssid;
  1042. lockdep_assert_held(&cgroup_mutex);
  1043. /* First see if we already have a cgroup group that matches
  1044. * the desired set */
  1045. spin_lock_irq(&css_set_lock);
  1046. cset = find_existing_css_set(old_cset, cgrp, template);
  1047. if (cset)
  1048. get_css_set(cset);
  1049. spin_unlock_irq(&css_set_lock);
  1050. if (cset)
  1051. return cset;
  1052. cset = kzalloc(sizeof(*cset), GFP_KERNEL);
  1053. if (!cset)
  1054. return NULL;
  1055. /* Allocate all the cgrp_cset_link objects that we'll need */
  1056. if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
  1057. kfree(cset);
  1058. return NULL;
  1059. }
  1060. refcount_set(&cset->refcount, 1);
  1061. cset->dom_cset = cset;
  1062. INIT_LIST_HEAD(&cset->tasks);
  1063. INIT_LIST_HEAD(&cset->mg_tasks);
  1064. INIT_LIST_HEAD(&cset->dying_tasks);
  1065. INIT_LIST_HEAD(&cset->task_iters);
  1066. INIT_LIST_HEAD(&cset->threaded_csets);
  1067. INIT_HLIST_NODE(&cset->hlist);
  1068. INIT_LIST_HEAD(&cset->cgrp_links);
  1069. INIT_LIST_HEAD(&cset->mg_src_preload_node);
  1070. INIT_LIST_HEAD(&cset->mg_dst_preload_node);
  1071. INIT_LIST_HEAD(&cset->mg_node);
  1072. /* Copy the set of subsystem state objects generated in
  1073. * find_existing_css_set() */
  1074. memcpy(cset->subsys, template, sizeof(cset->subsys));
  1075. spin_lock_irq(&css_set_lock);
  1076. /* Add reference counts and links from the new css_set. */
  1077. list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
  1078. struct cgroup *c = link->cgrp;
  1079. if (c->root == cgrp->root)
  1080. c = cgrp;
  1081. link_css_set(&tmp_links, cset, c);
  1082. }
  1083. BUG_ON(!list_empty(&tmp_links));
  1084. css_set_count++;
  1085. /* Add @cset to the hash table */
  1086. key = css_set_hash(cset->subsys);
  1087. hash_add(css_set_table, &cset->hlist, key);
  1088. for_each_subsys(ss, ssid) {
  1089. struct cgroup_subsys_state *css = cset->subsys[ssid];
  1090. list_add_tail(&cset->e_cset_node[ssid],
  1091. &css->cgroup->e_csets[ssid]);
  1092. css_get(css);
  1093. }
  1094. spin_unlock_irq(&css_set_lock);
  1095. /*
  1096. * If @cset should be threaded, look up the matching dom_cset and
  1097. * link them up. We first fully initialize @cset then look for the
  1098. * dom_cset. It's simpler this way and safe as @cset is guaranteed
  1099. * to stay empty until we return.
  1100. */
  1101. if (cgroup_is_threaded(cset->dfl_cgrp)) {
  1102. struct css_set *dcset;
  1103. dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp);
  1104. if (!dcset) {
  1105. put_css_set(cset);
  1106. return NULL;
  1107. }
  1108. spin_lock_irq(&css_set_lock);
  1109. cset->dom_cset = dcset;
  1110. list_add_tail(&cset->threaded_csets_node,
  1111. &dcset->threaded_csets);
  1112. spin_unlock_irq(&css_set_lock);
  1113. }
  1114. return cset;
  1115. }
  1116. struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
  1117. {
  1118. struct cgroup *root_cgrp = kernfs_root_to_node(kf_root)->priv;
  1119. return root_cgrp->root;
  1120. }
  1121. void cgroup_favor_dynmods(struct cgroup_root *root, bool favor)
  1122. {
  1123. bool favoring = root->flags & CGRP_ROOT_FAVOR_DYNMODS;
  1124. /* see the comment above CGRP_ROOT_FAVOR_DYNMODS definition */
  1125. if (favor && !favoring) {
  1126. rcu_sync_enter(&cgroup_threadgroup_rwsem.rss);
  1127. root->flags |= CGRP_ROOT_FAVOR_DYNMODS;
  1128. } else if (!favor && favoring) {
  1129. rcu_sync_exit(&cgroup_threadgroup_rwsem.rss);
  1130. root->flags &= ~CGRP_ROOT_FAVOR_DYNMODS;
  1131. }
  1132. }
  1133. static int cgroup_init_root_id(struct cgroup_root *root)
  1134. {
  1135. int id;
  1136. lockdep_assert_held(&cgroup_mutex);
  1137. id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
  1138. if (id < 0)
  1139. return id;
  1140. root->hierarchy_id = id;
  1141. return 0;
  1142. }
  1143. static void cgroup_exit_root_id(struct cgroup_root *root)
  1144. {
  1145. lockdep_assert_held(&cgroup_mutex);
  1146. idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
  1147. }
  1148. void cgroup_free_root(struct cgroup_root *root)
  1149. {
  1150. kfree_rcu(root, rcu);
  1151. }
  1152. static void cgroup_destroy_root(struct cgroup_root *root)
  1153. {
  1154. struct cgroup *cgrp = &root->cgrp;
  1155. struct cgrp_cset_link *link, *tmp_link;
  1156. trace_cgroup_destroy_root(root);
  1157. cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
  1158. BUG_ON(atomic_read(&root->nr_cgrps));
  1159. BUG_ON(!list_empty(&cgrp->self.children));
  1160. /* Rebind all subsystems back to the default hierarchy */
  1161. WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask));
  1162. /*
  1163. * Release all the links from cset_links to this hierarchy's
  1164. * root cgroup
  1165. */
  1166. spin_lock_irq(&css_set_lock);
  1167. list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
  1168. list_del(&link->cset_link);
  1169. list_del(&link->cgrp_link);
  1170. kfree(link);
  1171. }
  1172. spin_unlock_irq(&css_set_lock);
  1173. WARN_ON_ONCE(list_empty(&root->root_list));
  1174. list_del_rcu(&root->root_list);
  1175. cgroup_root_count--;
  1176. if (!have_favordynmods)
  1177. cgroup_favor_dynmods(root, false);
  1178. cgroup_exit_root_id(root);
  1179. cgroup_unlock();
  1180. cgroup_rstat_exit(cgrp);
  1181. kernfs_destroy_root(root->kf_root);
  1182. cgroup_free_root(root);
  1183. }
  1184. /*
  1185. * Returned cgroup is without refcount but it's valid as long as cset pins it.
  1186. */
  1187. static inline struct cgroup *__cset_cgroup_from_root(struct css_set *cset,
  1188. struct cgroup_root *root)
  1189. {
  1190. struct cgroup *res_cgroup = NULL;
  1191. if (cset == &init_css_set) {
  1192. res_cgroup = &root->cgrp;
  1193. } else if (root == &cgrp_dfl_root) {
  1194. res_cgroup = cset->dfl_cgrp;
  1195. } else {
  1196. struct cgrp_cset_link *link;
  1197. lockdep_assert_held(&css_set_lock);
  1198. list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  1199. struct cgroup *c = link->cgrp;
  1200. if (c->root == root) {
  1201. res_cgroup = c;
  1202. break;
  1203. }
  1204. }
  1205. }
  1206. /*
  1207. * If cgroup_mutex is not held, the cgrp_cset_link will be freed
  1208. * before we remove the cgroup root from the root_list. Consequently,
  1209. * when accessing a cgroup root, the cset_link may have already been
  1210. * freed, resulting in a NULL res_cgroup. However, by holding the
  1211. * cgroup_mutex, we ensure that res_cgroup can't be NULL.
  1212. * If we don't hold cgroup_mutex in the caller, we must do the NULL
  1213. * check.
  1214. */
  1215. return res_cgroup;
  1216. }
  1217. /*
  1218. * look up cgroup associated with current task's cgroup namespace on the
  1219. * specified hierarchy
  1220. */
  1221. static struct cgroup *
  1222. current_cgns_cgroup_from_root(struct cgroup_root *root)
  1223. {
  1224. struct cgroup *res = NULL;
  1225. struct css_set *cset;
  1226. lockdep_assert_held(&css_set_lock);
  1227. rcu_read_lock();
  1228. cset = current->nsproxy->cgroup_ns->root_cset;
  1229. res = __cset_cgroup_from_root(cset, root);
  1230. rcu_read_unlock();
  1231. /*
  1232. * The namespace_sem is held by current, so the root cgroup can't
  1233. * be umounted. Therefore, we can ensure that the res is non-NULL.
  1234. */
  1235. WARN_ON_ONCE(!res);
  1236. return res;
  1237. }
  1238. /*
  1239. * Look up cgroup associated with current task's cgroup namespace on the default
  1240. * hierarchy.
  1241. *
  1242. * Unlike current_cgns_cgroup_from_root(), this doesn't need locks:
  1243. * - Internal rcu_read_lock is unnecessary because we don't dereference any rcu
  1244. * pointers.
  1245. * - css_set_lock is not needed because we just read cset->dfl_cgrp.
  1246. * - As a bonus returned cgrp is pinned with the current because it cannot
  1247. * switch cgroup_ns asynchronously.
  1248. */
  1249. static struct cgroup *current_cgns_cgroup_dfl(void)
  1250. {
  1251. struct css_set *cset;
  1252. if (current->nsproxy) {
  1253. cset = current->nsproxy->cgroup_ns->root_cset;
  1254. return __cset_cgroup_from_root(cset, &cgrp_dfl_root);
  1255. } else {
  1256. /*
  1257. * NOTE: This function may be called from bpf_cgroup_from_id()
  1258. * on a task which has already passed exit_task_namespaces() and
  1259. * nsproxy == NULL. Fall back to cgrp_dfl_root which will make all
  1260. * cgroups visible for lookups.
  1261. */
  1262. return &cgrp_dfl_root.cgrp;
  1263. }
  1264. }
  1265. /* look up cgroup associated with given css_set on the specified hierarchy */
  1266. static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
  1267. struct cgroup_root *root)
  1268. {
  1269. lockdep_assert_held(&css_set_lock);
  1270. return __cset_cgroup_from_root(cset, root);
  1271. }
  1272. /*
  1273. * Return the cgroup for "task" from the given hierarchy. Must be
  1274. * called with css_set_lock held to prevent task's groups from being modified.
  1275. * Must be called with either cgroup_mutex or rcu read lock to prevent the
  1276. * cgroup root from being destroyed.
  1277. */
  1278. struct cgroup *task_cgroup_from_root(struct task_struct *task,
  1279. struct cgroup_root *root)
  1280. {
  1281. /*
  1282. * No need to lock the task - since we hold css_set_lock the
  1283. * task can't change groups.
  1284. */
  1285. return cset_cgroup_from_root(task_css_set(task), root);
  1286. }
  1287. /*
  1288. * A task must hold cgroup_mutex to modify cgroups.
  1289. *
  1290. * Any task can increment and decrement the count field without lock.
  1291. * So in general, code holding cgroup_mutex can't rely on the count
  1292. * field not changing. However, if the count goes to zero, then only
  1293. * cgroup_attach_task() can increment it again. Because a count of zero
  1294. * means that no tasks are currently attached, therefore there is no
  1295. * way a task attached to that cgroup can fork (the other way to
  1296. * increment the count). So code holding cgroup_mutex can safely
  1297. * assume that if the count is zero, it will stay zero. Similarly, if
  1298. * a task holds cgroup_mutex on a cgroup with zero count, it
  1299. * knows that the cgroup won't be removed, as cgroup_rmdir()
  1300. * needs that mutex.
  1301. *
  1302. * A cgroup can only be deleted if both its 'count' of using tasks
  1303. * is zero, and its list of 'children' cgroups is empty. Since all
  1304. * tasks in the system use _some_ cgroup, and since there is always at
  1305. * least one task in the system (init, pid == 1), therefore, root cgroup
  1306. * always has either children cgroups and/or using tasks. So we don't
  1307. * need a special hack to ensure that root cgroup cannot be deleted.
  1308. *
  1309. * P.S. One more locking exception. RCU is used to guard the
  1310. * update of a tasks cgroup pointer by cgroup_attach_task()
  1311. */
  1312. static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
  1313. static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
  1314. char *buf)
  1315. {
  1316. struct cgroup_subsys *ss = cft->ss;
  1317. if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
  1318. !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
  1319. const char *dbg = (cft->flags & CFTYPE_DEBUG) ? ".__DEBUG__." : "";
  1320. snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s",
  1321. dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
  1322. cft->name);
  1323. } else {
  1324. strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
  1325. }
  1326. return buf;
  1327. }
  1328. /**
  1329. * cgroup_file_mode - deduce file mode of a control file
  1330. * @cft: the control file in question
  1331. *
  1332. * S_IRUGO for read, S_IWUSR for write.
  1333. */
  1334. static umode_t cgroup_file_mode(const struct cftype *cft)
  1335. {
  1336. umode_t mode = 0;
  1337. if (cft->read_u64 || cft->read_s64 || cft->seq_show)
  1338. mode |= S_IRUGO;
  1339. if (cft->write_u64 || cft->write_s64 || cft->write) {
  1340. if (cft->flags & CFTYPE_WORLD_WRITABLE)
  1341. mode |= S_IWUGO;
  1342. else
  1343. mode |= S_IWUSR;
  1344. }
  1345. return mode;
  1346. }
  1347. /**
  1348. * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
  1349. * @subtree_control: the new subtree_control mask to consider
  1350. * @this_ss_mask: available subsystems
  1351. *
  1352. * On the default hierarchy, a subsystem may request other subsystems to be
  1353. * enabled together through its ->depends_on mask. In such cases, more
  1354. * subsystems than specified in "cgroup.subtree_control" may be enabled.
  1355. *
  1356. * This function calculates which subsystems need to be enabled if
  1357. * @subtree_control is to be applied while restricted to @this_ss_mask.
  1358. */
  1359. static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask)
  1360. {
  1361. u16 cur_ss_mask = subtree_control;
  1362. struct cgroup_subsys *ss;
  1363. int ssid;
  1364. lockdep_assert_held(&cgroup_mutex);
  1365. cur_ss_mask |= cgrp_dfl_implicit_ss_mask;
  1366. while (true) {
  1367. u16 new_ss_mask = cur_ss_mask;
  1368. do_each_subsys_mask(ss, ssid, cur_ss_mask) {
  1369. new_ss_mask |= ss->depends_on;
  1370. } while_each_subsys_mask();
  1371. /*
  1372. * Mask out subsystems which aren't available. This can
  1373. * happen only if some depended-upon subsystems were bound
  1374. * to non-default hierarchies.
  1375. */
  1376. new_ss_mask &= this_ss_mask;
  1377. if (new_ss_mask == cur_ss_mask)
  1378. break;
  1379. cur_ss_mask = new_ss_mask;
  1380. }
  1381. return cur_ss_mask;
  1382. }
  1383. /**
  1384. * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
  1385. * @kn: the kernfs_node being serviced
  1386. *
  1387. * This helper undoes cgroup_kn_lock_live() and should be invoked before
  1388. * the method finishes if locking succeeded. Note that once this function
  1389. * returns the cgroup returned by cgroup_kn_lock_live() may become
  1390. * inaccessible any time. If the caller intends to continue to access the
  1391. * cgroup, it should pin it before invoking this function.
  1392. */
  1393. void cgroup_kn_unlock(struct kernfs_node *kn)
  1394. {
  1395. struct cgroup *cgrp;
  1396. if (kernfs_type(kn) == KERNFS_DIR)
  1397. cgrp = kn->priv;
  1398. else
  1399. cgrp = kn->parent->priv;
  1400. cgroup_unlock();
  1401. kernfs_unbreak_active_protection(kn);
  1402. cgroup_put(cgrp);
  1403. }
  1404. /**
  1405. * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
  1406. * @kn: the kernfs_node being serviced
  1407. * @drain_offline: perform offline draining on the cgroup
  1408. *
  1409. * This helper is to be used by a cgroup kernfs method currently servicing
  1410. * @kn. It breaks the active protection, performs cgroup locking and
  1411. * verifies that the associated cgroup is alive. Returns the cgroup if
  1412. * alive; otherwise, %NULL. A successful return should be undone by a
  1413. * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
  1414. * cgroup is drained of offlining csses before return.
  1415. *
  1416. * Any cgroup kernfs method implementation which requires locking the
  1417. * associated cgroup should use this helper. It avoids nesting cgroup
  1418. * locking under kernfs active protection and allows all kernfs operations
  1419. * including self-removal.
  1420. */
  1421. struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline)
  1422. {
  1423. struct cgroup *cgrp;
  1424. if (kernfs_type(kn) == KERNFS_DIR)
  1425. cgrp = kn->priv;
  1426. else
  1427. cgrp = kn->parent->priv;
  1428. /*
  1429. * We're gonna grab cgroup_mutex which nests outside kernfs
  1430. * active_ref. cgroup liveliness check alone provides enough
  1431. * protection against removal. Ensure @cgrp stays accessible and
  1432. * break the active_ref protection.
  1433. */
  1434. if (!cgroup_tryget(cgrp))
  1435. return NULL;
  1436. kernfs_break_active_protection(kn);
  1437. if (drain_offline)
  1438. cgroup_lock_and_drain_offline(cgrp);
  1439. else
  1440. cgroup_lock();
  1441. if (!cgroup_is_dead(cgrp))
  1442. return cgrp;
  1443. cgroup_kn_unlock(kn);
  1444. return NULL;
  1445. }
  1446. static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
  1447. {
  1448. char name[CGROUP_FILE_NAME_MAX];
  1449. lockdep_assert_held(&cgroup_mutex);
  1450. if (cft->file_offset) {
  1451. struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
  1452. struct cgroup_file *cfile = (void *)css + cft->file_offset;
  1453. spin_lock_irq(&cgroup_file_kn_lock);
  1454. cfile->kn = NULL;
  1455. spin_unlock_irq(&cgroup_file_kn_lock);
  1456. del_timer_sync(&cfile->notify_timer);
  1457. }
  1458. kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
  1459. }
  1460. /**
  1461. * css_clear_dir - remove subsys files in a cgroup directory
  1462. * @css: target css
  1463. */
  1464. static void css_clear_dir(struct cgroup_subsys_state *css)
  1465. {
  1466. struct cgroup *cgrp = css->cgroup;
  1467. struct cftype *cfts;
  1468. if (!(css->flags & CSS_VISIBLE))
  1469. return;
  1470. css->flags &= ~CSS_VISIBLE;
  1471. if (!css->ss) {
  1472. if (cgroup_on_dfl(cgrp)) {
  1473. cgroup_addrm_files(css, cgrp,
  1474. cgroup_base_files, false);
  1475. if (cgroup_psi_enabled())
  1476. cgroup_addrm_files(css, cgrp,
  1477. cgroup_psi_files, false);
  1478. } else {
  1479. cgroup_addrm_files(css, cgrp,
  1480. cgroup1_base_files, false);
  1481. }
  1482. } else {
  1483. list_for_each_entry(cfts, &css->ss->cfts, node)
  1484. cgroup_addrm_files(css, cgrp, cfts, false);
  1485. }
  1486. }
  1487. /**
  1488. * css_populate_dir - create subsys files in a cgroup directory
  1489. * @css: target css
  1490. *
  1491. * On failure, no file is added.
  1492. */
  1493. static int css_populate_dir(struct cgroup_subsys_state *css)
  1494. {
  1495. struct cgroup *cgrp = css->cgroup;
  1496. struct cftype *cfts, *failed_cfts;
  1497. int ret;
  1498. if (css->flags & CSS_VISIBLE)
  1499. return 0;
  1500. if (!css->ss) {
  1501. if (cgroup_on_dfl(cgrp)) {
  1502. ret = cgroup_addrm_files(css, cgrp,
  1503. cgroup_base_files, true);
  1504. if (ret < 0)
  1505. return ret;
  1506. if (cgroup_psi_enabled()) {
  1507. ret = cgroup_addrm_files(css, cgrp,
  1508. cgroup_psi_files, true);
  1509. if (ret < 0) {
  1510. cgroup_addrm_files(css, cgrp,
  1511. cgroup_base_files, false);
  1512. return ret;
  1513. }
  1514. }
  1515. } else {
  1516. ret = cgroup_addrm_files(css, cgrp,
  1517. cgroup1_base_files, true);
  1518. if (ret < 0)
  1519. return ret;
  1520. }
  1521. } else {
  1522. list_for_each_entry(cfts, &css->ss->cfts, node) {
  1523. ret = cgroup_addrm_files(css, cgrp, cfts, true);
  1524. if (ret < 0) {
  1525. failed_cfts = cfts;
  1526. goto err;
  1527. }
  1528. }
  1529. }
  1530. css->flags |= CSS_VISIBLE;
  1531. return 0;
  1532. err:
  1533. list_for_each_entry(cfts, &css->ss->cfts, node) {
  1534. if (cfts == failed_cfts)
  1535. break;
  1536. cgroup_addrm_files(css, cgrp, cfts, false);
  1537. }
  1538. return ret;
  1539. }
  1540. int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
  1541. {
  1542. struct cgroup *dcgrp = &dst_root->cgrp;
  1543. struct cgroup_subsys *ss;
  1544. int ssid, ret;
  1545. u16 dfl_disable_ss_mask = 0;
  1546. lockdep_assert_held(&cgroup_mutex);
  1547. do_each_subsys_mask(ss, ssid, ss_mask) {
  1548. /*
  1549. * If @ss has non-root csses attached to it, can't move.
  1550. * If @ss is an implicit controller, it is exempt from this
  1551. * rule and can be stolen.
  1552. */
  1553. if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) &&
  1554. !ss->implicit_on_dfl)
  1555. return -EBUSY;
  1556. /* can't move between two non-dummy roots either */
  1557. if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
  1558. return -EBUSY;
  1559. /*
  1560. * Collect ssid's that need to be disabled from default
  1561. * hierarchy.
  1562. */
  1563. if (ss->root == &cgrp_dfl_root)
  1564. dfl_disable_ss_mask |= 1 << ssid;
  1565. } while_each_subsys_mask();
  1566. if (dfl_disable_ss_mask) {
  1567. struct cgroup *scgrp = &cgrp_dfl_root.cgrp;
  1568. /*
  1569. * Controllers from default hierarchy that need to be rebound
  1570. * are all disabled together in one go.
  1571. */
  1572. cgrp_dfl_root.subsys_mask &= ~dfl_disable_ss_mask;
  1573. WARN_ON(cgroup_apply_control(scgrp));
  1574. cgroup_finalize_control(scgrp, 0);
  1575. }
  1576. do_each_subsys_mask(ss, ssid, ss_mask) {
  1577. struct cgroup_root *src_root = ss->root;
  1578. struct cgroup *scgrp = &src_root->cgrp;
  1579. struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
  1580. struct css_set *cset, *cset_pos;
  1581. struct css_task_iter *it;
  1582. WARN_ON(!css || cgroup_css(dcgrp, ss));
  1583. if (src_root != &cgrp_dfl_root) {
  1584. /* disable from the source */
  1585. src_root->subsys_mask &= ~(1 << ssid);
  1586. WARN_ON(cgroup_apply_control(scgrp));
  1587. cgroup_finalize_control(scgrp, 0);
  1588. }
  1589. /* rebind */
  1590. RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
  1591. rcu_assign_pointer(dcgrp->subsys[ssid], css);
  1592. ss->root = dst_root;
  1593. spin_lock_irq(&css_set_lock);
  1594. css->cgroup = dcgrp;
  1595. WARN_ON(!list_empty(&dcgrp->e_csets[ss->id]));
  1596. list_for_each_entry_safe(cset, cset_pos, &scgrp->e_csets[ss->id],
  1597. e_cset_node[ss->id]) {
  1598. list_move_tail(&cset->e_cset_node[ss->id],
  1599. &dcgrp->e_csets[ss->id]);
  1600. /*
  1601. * all css_sets of scgrp together in same order to dcgrp,
  1602. * patch in-flight iterators to preserve correct iteration.
  1603. * since the iterator is always advanced right away and
  1604. * finished when it->cset_pos meets it->cset_head, so only
  1605. * update it->cset_head is enough here.
  1606. */
  1607. list_for_each_entry(it, &cset->task_iters, iters_node)
  1608. if (it->cset_head == &scgrp->e_csets[ss->id])
  1609. it->cset_head = &dcgrp->e_csets[ss->id];
  1610. }
  1611. spin_unlock_irq(&css_set_lock);
  1612. if (ss->css_rstat_flush) {
  1613. list_del_rcu(&css->rstat_css_node);
  1614. synchronize_rcu();
  1615. list_add_rcu(&css->rstat_css_node,
  1616. &dcgrp->rstat_css_list);
  1617. }
  1618. /* default hierarchy doesn't enable controllers by default */
  1619. dst_root->subsys_mask |= 1 << ssid;
  1620. if (dst_root == &cgrp_dfl_root) {
  1621. static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
  1622. } else {
  1623. dcgrp->subtree_control |= 1 << ssid;
  1624. static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
  1625. }
  1626. ret = cgroup_apply_control(dcgrp);
  1627. if (ret)
  1628. pr_warn("partial failure to rebind %s controller (err=%d)\n",
  1629. ss->name, ret);
  1630. if (ss->bind)
  1631. ss->bind(css);
  1632. } while_each_subsys_mask();
  1633. kernfs_activate(dcgrp->kn);
  1634. return 0;
  1635. }
  1636. int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
  1637. struct kernfs_root *kf_root)
  1638. {
  1639. int len = 0;
  1640. char *buf = NULL;
  1641. struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root);
  1642. struct cgroup *ns_cgroup;
  1643. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  1644. if (!buf)
  1645. return -ENOMEM;
  1646. spin_lock_irq(&css_set_lock);
  1647. ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot);
  1648. len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX);
  1649. spin_unlock_irq(&css_set_lock);
  1650. if (len == -E2BIG)
  1651. len = -ERANGE;
  1652. else if (len > 0) {
  1653. seq_escape(sf, buf, " \t\n\\");
  1654. len = 0;
  1655. }
  1656. kfree(buf);
  1657. return len;
  1658. }
  1659. enum cgroup2_param {
  1660. Opt_nsdelegate,
  1661. Opt_favordynmods,
  1662. Opt_memory_localevents,
  1663. Opt_memory_recursiveprot,
  1664. Opt_memory_hugetlb_accounting,
  1665. Opt_pids_localevents,
  1666. nr__cgroup2_params
  1667. };
  1668. static const struct fs_parameter_spec cgroup2_fs_parameters[] = {
  1669. fsparam_flag("nsdelegate", Opt_nsdelegate),
  1670. fsparam_flag("favordynmods", Opt_favordynmods),
  1671. fsparam_flag("memory_localevents", Opt_memory_localevents),
  1672. fsparam_flag("memory_recursiveprot", Opt_memory_recursiveprot),
  1673. fsparam_flag("memory_hugetlb_accounting", Opt_memory_hugetlb_accounting),
  1674. fsparam_flag("pids_localevents", Opt_pids_localevents),
  1675. {}
  1676. };
  1677. static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *param)
  1678. {
  1679. struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
  1680. struct fs_parse_result result;
  1681. int opt;
  1682. opt = fs_parse(fc, cgroup2_fs_parameters, param, &result);
  1683. if (opt < 0)
  1684. return opt;
  1685. switch (opt) {
  1686. case Opt_nsdelegate:
  1687. ctx->flags |= CGRP_ROOT_NS_DELEGATE;
  1688. return 0;
  1689. case Opt_favordynmods:
  1690. ctx->flags |= CGRP_ROOT_FAVOR_DYNMODS;
  1691. return 0;
  1692. case Opt_memory_localevents:
  1693. ctx->flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS;
  1694. return 0;
  1695. case Opt_memory_recursiveprot:
  1696. ctx->flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT;
  1697. return 0;
  1698. case Opt_memory_hugetlb_accounting:
  1699. ctx->flags |= CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING;
  1700. return 0;
  1701. case Opt_pids_localevents:
  1702. ctx->flags |= CGRP_ROOT_PIDS_LOCAL_EVENTS;
  1703. return 0;
  1704. }
  1705. return -EINVAL;
  1706. }
  1707. struct cgroup_of_peak *of_peak(struct kernfs_open_file *of)
  1708. {
  1709. struct cgroup_file_ctx *ctx = of->priv;
  1710. return &ctx->peak;
  1711. }
  1712. static void apply_cgroup_root_flags(unsigned int root_flags)
  1713. {
  1714. if (current->nsproxy->cgroup_ns == &init_cgroup_ns) {
  1715. if (root_flags & CGRP_ROOT_NS_DELEGATE)
  1716. cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE;
  1717. else
  1718. cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE;
  1719. cgroup_favor_dynmods(&cgrp_dfl_root,
  1720. root_flags & CGRP_ROOT_FAVOR_DYNMODS);
  1721. if (root_flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
  1722. cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS;
  1723. else
  1724. cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_LOCAL_EVENTS;
  1725. if (root_flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT)
  1726. cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT;
  1727. else
  1728. cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_RECURSIVE_PROT;
  1729. if (root_flags & CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING)
  1730. cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING;
  1731. else
  1732. cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING;
  1733. if (root_flags & CGRP_ROOT_PIDS_LOCAL_EVENTS)
  1734. cgrp_dfl_root.flags |= CGRP_ROOT_PIDS_LOCAL_EVENTS;
  1735. else
  1736. cgrp_dfl_root.flags &= ~CGRP_ROOT_PIDS_LOCAL_EVENTS;
  1737. }
  1738. }
  1739. static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root)
  1740. {
  1741. if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE)
  1742. seq_puts(seq, ",nsdelegate");
  1743. if (cgrp_dfl_root.flags & CGRP_ROOT_FAVOR_DYNMODS)
  1744. seq_puts(seq, ",favordynmods");
  1745. if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
  1746. seq_puts(seq, ",memory_localevents");
  1747. if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT)
  1748. seq_puts(seq, ",memory_recursiveprot");
  1749. if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING)
  1750. seq_puts(seq, ",memory_hugetlb_accounting");
  1751. if (cgrp_dfl_root.flags & CGRP_ROOT_PIDS_LOCAL_EVENTS)
  1752. seq_puts(seq, ",pids_localevents");
  1753. return 0;
  1754. }
  1755. static int cgroup_reconfigure(struct fs_context *fc)
  1756. {
  1757. struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
  1758. apply_cgroup_root_flags(ctx->flags);
  1759. return 0;
  1760. }
  1761. static void init_cgroup_housekeeping(struct cgroup *cgrp)
  1762. {
  1763. struct cgroup_subsys *ss;
  1764. int ssid;
  1765. INIT_LIST_HEAD(&cgrp->self.sibling);
  1766. INIT_LIST_HEAD(&cgrp->self.children);
  1767. INIT_LIST_HEAD(&cgrp->cset_links);
  1768. INIT_LIST_HEAD(&cgrp->pidlists);
  1769. mutex_init(&cgrp->pidlist_mutex);
  1770. cgrp->self.cgroup = cgrp;
  1771. cgrp->self.flags |= CSS_ONLINE;
  1772. cgrp->dom_cgrp = cgrp;
  1773. cgrp->max_descendants = INT_MAX;
  1774. cgrp->max_depth = INT_MAX;
  1775. INIT_LIST_HEAD(&cgrp->rstat_css_list);
  1776. prev_cputime_init(&cgrp->prev_cputime);
  1777. for_each_subsys(ss, ssid)
  1778. INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
  1779. init_waitqueue_head(&cgrp->offline_waitq);
  1780. INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent);
  1781. }
  1782. void init_cgroup_root(struct cgroup_fs_context *ctx)
  1783. {
  1784. struct cgroup_root *root = ctx->root;
  1785. struct cgroup *cgrp = &root->cgrp;
  1786. INIT_LIST_HEAD_RCU(&root->root_list);
  1787. atomic_set(&root->nr_cgrps, 1);
  1788. cgrp->root = root;
  1789. init_cgroup_housekeeping(cgrp);
  1790. /* DYNMODS must be modified through cgroup_favor_dynmods() */
  1791. root->flags = ctx->flags & ~CGRP_ROOT_FAVOR_DYNMODS;
  1792. if (ctx->release_agent)
  1793. strscpy(root->release_agent_path, ctx->release_agent, PATH_MAX);
  1794. if (ctx->name)
  1795. strscpy(root->name, ctx->name, MAX_CGROUP_ROOT_NAMELEN);
  1796. if (ctx->cpuset_clone_children)
  1797. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
  1798. }
  1799. int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
  1800. {
  1801. LIST_HEAD(tmp_links);
  1802. struct cgroup *root_cgrp = &root->cgrp;
  1803. struct kernfs_syscall_ops *kf_sops;
  1804. struct css_set *cset;
  1805. int i, ret;
  1806. lockdep_assert_held(&cgroup_mutex);
  1807. ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release,
  1808. 0, GFP_KERNEL);
  1809. if (ret)
  1810. goto out;
  1811. /*
  1812. * We're accessing css_set_count without locking css_set_lock here,
  1813. * but that's OK - it can only be increased by someone holding
  1814. * cgroup_lock, and that's us. Later rebinding may disable
  1815. * controllers on the default hierarchy and thus create new csets,
  1816. * which can't be more than the existing ones. Allocate 2x.
  1817. */
  1818. ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links);
  1819. if (ret)
  1820. goto cancel_ref;
  1821. ret = cgroup_init_root_id(root);
  1822. if (ret)
  1823. goto cancel_ref;
  1824. kf_sops = root == &cgrp_dfl_root ?
  1825. &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops;
  1826. root->kf_root = kernfs_create_root(kf_sops,
  1827. KERNFS_ROOT_CREATE_DEACTIVATED |
  1828. KERNFS_ROOT_SUPPORT_EXPORTOP |
  1829. KERNFS_ROOT_SUPPORT_USER_XATTR,
  1830. root_cgrp);
  1831. if (IS_ERR(root->kf_root)) {
  1832. ret = PTR_ERR(root->kf_root);
  1833. goto exit_root_id;
  1834. }
  1835. root_cgrp->kn = kernfs_root_to_node(root->kf_root);
  1836. WARN_ON_ONCE(cgroup_ino(root_cgrp) != 1);
  1837. root_cgrp->ancestors[0] = root_cgrp;
  1838. ret = css_populate_dir(&root_cgrp->self);
  1839. if (ret)
  1840. goto destroy_root;
  1841. ret = cgroup_rstat_init(root_cgrp);
  1842. if (ret)
  1843. goto destroy_root;
  1844. ret = rebind_subsystems(root, ss_mask);
  1845. if (ret)
  1846. goto exit_stats;
  1847. if (root == &cgrp_dfl_root) {
  1848. ret = cgroup_bpf_inherit(root_cgrp);
  1849. WARN_ON_ONCE(ret);
  1850. }
  1851. trace_cgroup_setup_root(root);
  1852. /*
  1853. * There must be no failure case after here, since rebinding takes
  1854. * care of subsystems' refcounts, which are explicitly dropped in
  1855. * the failure exit path.
  1856. */
  1857. list_add_rcu(&root->root_list, &cgroup_roots);
  1858. cgroup_root_count++;
  1859. /*
  1860. * Link the root cgroup in this hierarchy into all the css_set
  1861. * objects.
  1862. */
  1863. spin_lock_irq(&css_set_lock);
  1864. hash_for_each(css_set_table, i, cset, hlist) {
  1865. link_css_set(&tmp_links, cset, root_cgrp);
  1866. if (css_set_populated(cset))
  1867. cgroup_update_populated(root_cgrp, true);
  1868. }
  1869. spin_unlock_irq(&css_set_lock);
  1870. BUG_ON(!list_empty(&root_cgrp->self.children));
  1871. BUG_ON(atomic_read(&root->nr_cgrps) != 1);
  1872. ret = 0;
  1873. goto out;
  1874. exit_stats:
  1875. cgroup_rstat_exit(root_cgrp);
  1876. destroy_root:
  1877. kernfs_destroy_root(root->kf_root);
  1878. root->kf_root = NULL;
  1879. exit_root_id:
  1880. cgroup_exit_root_id(root);
  1881. cancel_ref:
  1882. percpu_ref_exit(&root_cgrp->self.refcnt);
  1883. out:
  1884. free_cgrp_cset_links(&tmp_links);
  1885. return ret;
  1886. }
  1887. int cgroup_do_get_tree(struct fs_context *fc)
  1888. {
  1889. struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
  1890. int ret;
  1891. ctx->kfc.root = ctx->root->kf_root;
  1892. if (fc->fs_type == &cgroup2_fs_type)
  1893. ctx->kfc.magic = CGROUP2_SUPER_MAGIC;
  1894. else
  1895. ctx->kfc.magic = CGROUP_SUPER_MAGIC;
  1896. ret = kernfs_get_tree(fc);
  1897. /*
  1898. * In non-init cgroup namespace, instead of root cgroup's dentry,
  1899. * we return the dentry corresponding to the cgroupns->root_cgrp.
  1900. */
  1901. if (!ret && ctx->ns != &init_cgroup_ns) {
  1902. struct dentry *nsdentry;
  1903. struct super_block *sb = fc->root->d_sb;
  1904. struct cgroup *cgrp;
  1905. cgroup_lock();
  1906. spin_lock_irq(&css_set_lock);
  1907. cgrp = cset_cgroup_from_root(ctx->ns->root_cset, ctx->root);
  1908. spin_unlock_irq(&css_set_lock);
  1909. cgroup_unlock();
  1910. nsdentry = kernfs_node_dentry(cgrp->kn, sb);
  1911. dput(fc->root);
  1912. if (IS_ERR(nsdentry)) {
  1913. deactivate_locked_super(sb);
  1914. ret = PTR_ERR(nsdentry);
  1915. nsdentry = NULL;
  1916. }
  1917. fc->root = nsdentry;
  1918. }
  1919. if (!ctx->kfc.new_sb_created)
  1920. cgroup_put(&ctx->root->cgrp);
  1921. return ret;
  1922. }
  1923. /*
  1924. * Destroy a cgroup filesystem context.
  1925. */
  1926. static void cgroup_fs_context_free(struct fs_context *fc)
  1927. {
  1928. struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
  1929. kfree(ctx->name);
  1930. kfree(ctx->release_agent);
  1931. put_cgroup_ns(ctx->ns);
  1932. kernfs_free_fs_context(fc);
  1933. kfree(ctx);
  1934. }
  1935. static int cgroup_get_tree(struct fs_context *fc)
  1936. {
  1937. struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
  1938. int ret;
  1939. WRITE_ONCE(cgrp_dfl_visible, true);
  1940. cgroup_get_live(&cgrp_dfl_root.cgrp);
  1941. ctx->root = &cgrp_dfl_root;
  1942. ret = cgroup_do_get_tree(fc);
  1943. if (!ret)
  1944. apply_cgroup_root_flags(ctx->flags);
  1945. return ret;
  1946. }
  1947. static const struct fs_context_operations cgroup_fs_context_ops = {
  1948. .free = cgroup_fs_context_free,
  1949. .parse_param = cgroup2_parse_param,
  1950. .get_tree = cgroup_get_tree,
  1951. .reconfigure = cgroup_reconfigure,
  1952. };
  1953. static const struct fs_context_operations cgroup1_fs_context_ops = {
  1954. .free = cgroup_fs_context_free,
  1955. .parse_param = cgroup1_parse_param,
  1956. .get_tree = cgroup1_get_tree,
  1957. .reconfigure = cgroup1_reconfigure,
  1958. };
  1959. /*
  1960. * Initialise the cgroup filesystem creation/reconfiguration context. Notably,
  1961. * we select the namespace we're going to use.
  1962. */
  1963. static int cgroup_init_fs_context(struct fs_context *fc)
  1964. {
  1965. struct cgroup_fs_context *ctx;
  1966. ctx = kzalloc(sizeof(struct cgroup_fs_context), GFP_KERNEL);
  1967. if (!ctx)
  1968. return -ENOMEM;
  1969. ctx->ns = current->nsproxy->cgroup_ns;
  1970. get_cgroup_ns(ctx->ns);
  1971. fc->fs_private = &ctx->kfc;
  1972. if (fc->fs_type == &cgroup2_fs_type)
  1973. fc->ops = &cgroup_fs_context_ops;
  1974. else
  1975. fc->ops = &cgroup1_fs_context_ops;
  1976. put_user_ns(fc->user_ns);
  1977. fc->user_ns = get_user_ns(ctx->ns->user_ns);
  1978. fc->global = true;
  1979. if (have_favordynmods)
  1980. ctx->flags |= CGRP_ROOT_FAVOR_DYNMODS;
  1981. return 0;
  1982. }
  1983. static void cgroup_kill_sb(struct super_block *sb)
  1984. {
  1985. struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
  1986. struct cgroup_root *root = cgroup_root_from_kf(kf_root);
  1987. /*
  1988. * If @root doesn't have any children, start killing it.
  1989. * This prevents new mounts by disabling percpu_ref_tryget_live().
  1990. *
  1991. * And don't kill the default root.
  1992. */
  1993. if (list_empty(&root->cgrp.self.children) && root != &cgrp_dfl_root &&
  1994. !percpu_ref_is_dying(&root->cgrp.self.refcnt))
  1995. percpu_ref_kill(&root->cgrp.self.refcnt);
  1996. cgroup_put(&root->cgrp);
  1997. kernfs_kill_sb(sb);
  1998. }
  1999. struct file_system_type cgroup_fs_type = {
  2000. .name = "cgroup",
  2001. .init_fs_context = cgroup_init_fs_context,
  2002. .parameters = cgroup1_fs_parameters,
  2003. .kill_sb = cgroup_kill_sb,
  2004. .fs_flags = FS_USERNS_MOUNT,
  2005. };
  2006. static struct file_system_type cgroup2_fs_type = {
  2007. .name = "cgroup2",
  2008. .init_fs_context = cgroup_init_fs_context,
  2009. .parameters = cgroup2_fs_parameters,
  2010. .kill_sb = cgroup_kill_sb,
  2011. .fs_flags = FS_USERNS_MOUNT,
  2012. };
  2013. #ifdef CONFIG_CPUSETS_V1
  2014. enum cpuset_param {
  2015. Opt_cpuset_v2_mode,
  2016. };
  2017. static const struct fs_parameter_spec cpuset_fs_parameters[] = {
  2018. fsparam_flag ("cpuset_v2_mode", Opt_cpuset_v2_mode),
  2019. {}
  2020. };
  2021. static int cpuset_parse_param(struct fs_context *fc, struct fs_parameter *param)
  2022. {
  2023. struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
  2024. struct fs_parse_result result;
  2025. int opt;
  2026. opt = fs_parse(fc, cpuset_fs_parameters, param, &result);
  2027. if (opt < 0)
  2028. return opt;
  2029. switch (opt) {
  2030. case Opt_cpuset_v2_mode:
  2031. ctx->flags |= CGRP_ROOT_CPUSET_V2_MODE;
  2032. return 0;
  2033. }
  2034. return -EINVAL;
  2035. }
  2036. static const struct fs_context_operations cpuset_fs_context_ops = {
  2037. .get_tree = cgroup1_get_tree,
  2038. .free = cgroup_fs_context_free,
  2039. .parse_param = cpuset_parse_param,
  2040. };
  2041. /*
  2042. * This is ugly, but preserves the userspace API for existing cpuset
  2043. * users. If someone tries to mount the "cpuset" filesystem, we
  2044. * silently switch it to mount "cgroup" instead
  2045. */
  2046. static int cpuset_init_fs_context(struct fs_context *fc)
  2047. {
  2048. char *agent = kstrdup("/sbin/cpuset_release_agent", GFP_USER);
  2049. struct cgroup_fs_context *ctx;
  2050. int err;
  2051. err = cgroup_init_fs_context(fc);
  2052. if (err) {
  2053. kfree(agent);
  2054. return err;
  2055. }
  2056. fc->ops = &cpuset_fs_context_ops;
  2057. ctx = cgroup_fc2context(fc);
  2058. ctx->subsys_mask = 1 << cpuset_cgrp_id;
  2059. ctx->flags |= CGRP_ROOT_NOPREFIX;
  2060. ctx->release_agent = agent;
  2061. get_filesystem(&cgroup_fs_type);
  2062. put_filesystem(fc->fs_type);
  2063. fc->fs_type = &cgroup_fs_type;
  2064. return 0;
  2065. }
  2066. static struct file_system_type cpuset_fs_type = {
  2067. .name = "cpuset",
  2068. .init_fs_context = cpuset_init_fs_context,
  2069. .parameters = cpuset_fs_parameters,
  2070. .fs_flags = FS_USERNS_MOUNT,
  2071. };
  2072. #endif
  2073. int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
  2074. struct cgroup_namespace *ns)
  2075. {
  2076. struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root);
  2077. return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen);
  2078. }
  2079. int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
  2080. struct cgroup_namespace *ns)
  2081. {
  2082. int ret;
  2083. cgroup_lock();
  2084. spin_lock_irq(&css_set_lock);
  2085. ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns);
  2086. spin_unlock_irq(&css_set_lock);
  2087. cgroup_unlock();
  2088. return ret;
  2089. }
  2090. EXPORT_SYMBOL_GPL(cgroup_path_ns);
  2091. /**
  2092. * cgroup_attach_lock - Lock for ->attach()
  2093. * @lock_threadgroup: whether to down_write cgroup_threadgroup_rwsem
  2094. *
  2095. * cgroup migration sometimes needs to stabilize threadgroups against forks and
  2096. * exits by write-locking cgroup_threadgroup_rwsem. However, some ->attach()
  2097. * implementations (e.g. cpuset), also need to disable CPU hotplug.
  2098. * Unfortunately, letting ->attach() operations acquire cpus_read_lock() can
  2099. * lead to deadlocks.
  2100. *
  2101. * Bringing up a CPU may involve creating and destroying tasks which requires
  2102. * read-locking threadgroup_rwsem, so threadgroup_rwsem nests inside
  2103. * cpus_read_lock(). If we call an ->attach() which acquires the cpus lock while
  2104. * write-locking threadgroup_rwsem, the locking order is reversed and we end up
  2105. * waiting for an on-going CPU hotplug operation which in turn is waiting for
  2106. * the threadgroup_rwsem to be released to create new tasks. For more details:
  2107. *
  2108. * http://lkml.kernel.org/r/20220711174629.uehfmqegcwn2lqzu@wubuntu
  2109. *
  2110. * Resolve the situation by always acquiring cpus_read_lock() before optionally
  2111. * write-locking cgroup_threadgroup_rwsem. This allows ->attach() to assume that
  2112. * CPU hotplug is disabled on entry.
  2113. */
  2114. void cgroup_attach_lock(bool lock_threadgroup)
  2115. {
  2116. cpus_read_lock();
  2117. if (lock_threadgroup)
  2118. percpu_down_write(&cgroup_threadgroup_rwsem);
  2119. }
  2120. /**
  2121. * cgroup_attach_unlock - Undo cgroup_attach_lock()
  2122. * @lock_threadgroup: whether to up_write cgroup_threadgroup_rwsem
  2123. */
  2124. void cgroup_attach_unlock(bool lock_threadgroup)
  2125. {
  2126. if (lock_threadgroup)
  2127. percpu_up_write(&cgroup_threadgroup_rwsem);
  2128. cpus_read_unlock();
  2129. }
  2130. /**
  2131. * cgroup_migrate_add_task - add a migration target task to a migration context
  2132. * @task: target task
  2133. * @mgctx: target migration context
  2134. *
  2135. * Add @task, which is a migration target, to @mgctx->tset. This function
  2136. * becomes noop if @task doesn't need to be migrated. @task's css_set
  2137. * should have been added as a migration source and @task->cg_list will be
  2138. * moved from the css_set's tasks list to mg_tasks one.
  2139. */
  2140. static void cgroup_migrate_add_task(struct task_struct *task,
  2141. struct cgroup_mgctx *mgctx)
  2142. {
  2143. struct css_set *cset;
  2144. lockdep_assert_held(&css_set_lock);
  2145. /* @task either already exited or can't exit until the end */
  2146. if (task->flags & PF_EXITING)
  2147. return;
  2148. /* cgroup_threadgroup_rwsem protects racing against forks */
  2149. WARN_ON_ONCE(list_empty(&task->cg_list));
  2150. cset = task_css_set(task);
  2151. if (!cset->mg_src_cgrp)
  2152. return;
  2153. mgctx->tset.nr_tasks++;
  2154. list_move_tail(&task->cg_list, &cset->mg_tasks);
  2155. if (list_empty(&cset->mg_node))
  2156. list_add_tail(&cset->mg_node,
  2157. &mgctx->tset.src_csets);
  2158. if (list_empty(&cset->mg_dst_cset->mg_node))
  2159. list_add_tail(&cset->mg_dst_cset->mg_node,
  2160. &mgctx->tset.dst_csets);
  2161. }
  2162. /**
  2163. * cgroup_taskset_first - reset taskset and return the first task
  2164. * @tset: taskset of interest
  2165. * @dst_cssp: output variable for the destination css
  2166. *
  2167. * @tset iteration is initialized and the first task is returned.
  2168. */
  2169. struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
  2170. struct cgroup_subsys_state **dst_cssp)
  2171. {
  2172. tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
  2173. tset->cur_task = NULL;
  2174. return cgroup_taskset_next(tset, dst_cssp);
  2175. }
  2176. /**
  2177. * cgroup_taskset_next - iterate to the next task in taskset
  2178. * @tset: taskset of interest
  2179. * @dst_cssp: output variable for the destination css
  2180. *
  2181. * Return the next task in @tset. Iteration must have been initialized
  2182. * with cgroup_taskset_first().
  2183. */
  2184. struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
  2185. struct cgroup_subsys_state **dst_cssp)
  2186. {
  2187. struct css_set *cset = tset->cur_cset;
  2188. struct task_struct *task = tset->cur_task;
  2189. while (CGROUP_HAS_SUBSYS_CONFIG && &cset->mg_node != tset->csets) {
  2190. if (!task)
  2191. task = list_first_entry(&cset->mg_tasks,
  2192. struct task_struct, cg_list);
  2193. else
  2194. task = list_next_entry(task, cg_list);
  2195. if (&task->cg_list != &cset->mg_tasks) {
  2196. tset->cur_cset = cset;
  2197. tset->cur_task = task;
  2198. /*
  2199. * This function may be called both before and
  2200. * after cgroup_migrate_execute(). The two cases
  2201. * can be distinguished by looking at whether @cset
  2202. * has its ->mg_dst_cset set.
  2203. */
  2204. if (cset->mg_dst_cset)
  2205. *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
  2206. else
  2207. *dst_cssp = cset->subsys[tset->ssid];
  2208. return task;
  2209. }
  2210. cset = list_next_entry(cset, mg_node);
  2211. task = NULL;
  2212. }
  2213. return NULL;
  2214. }
  2215. /**
  2216. * cgroup_migrate_execute - migrate a taskset
  2217. * @mgctx: migration context
  2218. *
  2219. * Migrate tasks in @mgctx as setup by migration preparation functions.
  2220. * This function fails iff one of the ->can_attach callbacks fails and
  2221. * guarantees that either all or none of the tasks in @mgctx are migrated.
  2222. * @mgctx is consumed regardless of success.
  2223. */
  2224. static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
  2225. {
  2226. struct cgroup_taskset *tset = &mgctx->tset;
  2227. struct cgroup_subsys *ss;
  2228. struct task_struct *task, *tmp_task;
  2229. struct css_set *cset, *tmp_cset;
  2230. int ssid, failed_ssid, ret;
  2231. /* check that we can legitimately attach to the cgroup */
  2232. if (tset->nr_tasks) {
  2233. do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
  2234. if (ss->can_attach) {
  2235. tset->ssid = ssid;
  2236. ret = ss->can_attach(tset);
  2237. if (ret) {
  2238. failed_ssid = ssid;
  2239. goto out_cancel_attach;
  2240. }
  2241. }
  2242. } while_each_subsys_mask();
  2243. }
  2244. /*
  2245. * Now that we're guaranteed success, proceed to move all tasks to
  2246. * the new cgroup. There are no failure cases after here, so this
  2247. * is the commit point.
  2248. */
  2249. spin_lock_irq(&css_set_lock);
  2250. list_for_each_entry(cset, &tset->src_csets, mg_node) {
  2251. list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
  2252. struct css_set *from_cset = task_css_set(task);
  2253. struct css_set *to_cset = cset->mg_dst_cset;
  2254. get_css_set(to_cset);
  2255. to_cset->nr_tasks++;
  2256. css_set_move_task(task, from_cset, to_cset, true);
  2257. from_cset->nr_tasks--;
  2258. /*
  2259. * If the source or destination cgroup is frozen,
  2260. * the task might require to change its state.
  2261. */
  2262. cgroup_freezer_migrate_task(task, from_cset->dfl_cgrp,
  2263. to_cset->dfl_cgrp);
  2264. put_css_set_locked(from_cset);
  2265. }
  2266. }
  2267. spin_unlock_irq(&css_set_lock);
  2268. /*
  2269. * Migration is committed, all target tasks are now on dst_csets.
  2270. * Nothing is sensitive to fork() after this point. Notify
  2271. * controllers that migration is complete.
  2272. */
  2273. tset->csets = &tset->dst_csets;
  2274. if (tset->nr_tasks) {
  2275. do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
  2276. if (ss->attach) {
  2277. tset->ssid = ssid;
  2278. ss->attach(tset);
  2279. }
  2280. } while_each_subsys_mask();
  2281. }
  2282. ret = 0;
  2283. goto out_release_tset;
  2284. out_cancel_attach:
  2285. if (tset->nr_tasks) {
  2286. do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
  2287. if (ssid == failed_ssid)
  2288. break;
  2289. if (ss->cancel_attach) {
  2290. tset->ssid = ssid;
  2291. ss->cancel_attach(tset);
  2292. }
  2293. } while_each_subsys_mask();
  2294. }
  2295. out_release_tset:
  2296. spin_lock_irq(&css_set_lock);
  2297. list_splice_init(&tset->dst_csets, &tset->src_csets);
  2298. list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
  2299. list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
  2300. list_del_init(&cset->mg_node);
  2301. }
  2302. spin_unlock_irq(&css_set_lock);
  2303. /*
  2304. * Re-initialize the cgroup_taskset structure in case it is reused
  2305. * again in another cgroup_migrate_add_task()/cgroup_migrate_execute()
  2306. * iteration.
  2307. */
  2308. tset->nr_tasks = 0;
  2309. tset->csets = &tset->src_csets;
  2310. return ret;
  2311. }
  2312. /**
  2313. * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination
  2314. * @dst_cgrp: destination cgroup to test
  2315. *
  2316. * On the default hierarchy, except for the mixable, (possible) thread root
  2317. * and threaded cgroups, subtree_control must be zero for migration
  2318. * destination cgroups with tasks so that child cgroups don't compete
  2319. * against tasks.
  2320. */
  2321. int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp)
  2322. {
  2323. /* v1 doesn't have any restriction */
  2324. if (!cgroup_on_dfl(dst_cgrp))
  2325. return 0;
  2326. /* verify @dst_cgrp can host resources */
  2327. if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp))
  2328. return -EOPNOTSUPP;
  2329. /*
  2330. * If @dst_cgrp is already or can become a thread root or is
  2331. * threaded, it doesn't matter.
  2332. */
  2333. if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp))
  2334. return 0;
  2335. /* apply no-internal-process constraint */
  2336. if (dst_cgrp->subtree_control)
  2337. return -EBUSY;
  2338. return 0;
  2339. }
  2340. /**
  2341. * cgroup_migrate_finish - cleanup after attach
  2342. * @mgctx: migration context
  2343. *
  2344. * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
  2345. * those functions for details.
  2346. */
  2347. void cgroup_migrate_finish(struct cgroup_mgctx *mgctx)
  2348. {
  2349. struct css_set *cset, *tmp_cset;
  2350. lockdep_assert_held(&cgroup_mutex);
  2351. spin_lock_irq(&css_set_lock);
  2352. list_for_each_entry_safe(cset, tmp_cset, &mgctx->preloaded_src_csets,
  2353. mg_src_preload_node) {
  2354. cset->mg_src_cgrp = NULL;
  2355. cset->mg_dst_cgrp = NULL;
  2356. cset->mg_dst_cset = NULL;
  2357. list_del_init(&cset->mg_src_preload_node);
  2358. put_css_set_locked(cset);
  2359. }
  2360. list_for_each_entry_safe(cset, tmp_cset, &mgctx->preloaded_dst_csets,
  2361. mg_dst_preload_node) {
  2362. cset->mg_src_cgrp = NULL;
  2363. cset->mg_dst_cgrp = NULL;
  2364. cset->mg_dst_cset = NULL;
  2365. list_del_init(&cset->mg_dst_preload_node);
  2366. put_css_set_locked(cset);
  2367. }
  2368. spin_unlock_irq(&css_set_lock);
  2369. }
  2370. /**
  2371. * cgroup_migrate_add_src - add a migration source css_set
  2372. * @src_cset: the source css_set to add
  2373. * @dst_cgrp: the destination cgroup
  2374. * @mgctx: migration context
  2375. *
  2376. * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
  2377. * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
  2378. * up by cgroup_migrate_finish().
  2379. *
  2380. * This function may be called without holding cgroup_threadgroup_rwsem
  2381. * even if the target is a process. Threads may be created and destroyed
  2382. * but as long as cgroup_mutex is not dropped, no new css_set can be put
  2383. * into play and the preloaded css_sets are guaranteed to cover all
  2384. * migrations.
  2385. */
  2386. void cgroup_migrate_add_src(struct css_set *src_cset,
  2387. struct cgroup *dst_cgrp,
  2388. struct cgroup_mgctx *mgctx)
  2389. {
  2390. struct cgroup *src_cgrp;
  2391. lockdep_assert_held(&cgroup_mutex);
  2392. lockdep_assert_held(&css_set_lock);
  2393. /*
  2394. * If ->dead, @src_set is associated with one or more dead cgroups
  2395. * and doesn't contain any migratable tasks. Ignore it early so
  2396. * that the rest of migration path doesn't get confused by it.
  2397. */
  2398. if (src_cset->dead)
  2399. return;
  2400. if (!list_empty(&src_cset->mg_src_preload_node))
  2401. return;
  2402. src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
  2403. WARN_ON(src_cset->mg_src_cgrp);
  2404. WARN_ON(src_cset->mg_dst_cgrp);
  2405. WARN_ON(!list_empty(&src_cset->mg_tasks));
  2406. WARN_ON(!list_empty(&src_cset->mg_node));
  2407. src_cset->mg_src_cgrp = src_cgrp;
  2408. src_cset->mg_dst_cgrp = dst_cgrp;
  2409. get_css_set(src_cset);
  2410. list_add_tail(&src_cset->mg_src_preload_node, &mgctx->preloaded_src_csets);
  2411. }
  2412. /**
  2413. * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
  2414. * @mgctx: migration context
  2415. *
  2416. * Tasks are about to be moved and all the source css_sets have been
  2417. * preloaded to @mgctx->preloaded_src_csets. This function looks up and
  2418. * pins all destination css_sets, links each to its source, and append them
  2419. * to @mgctx->preloaded_dst_csets.
  2420. *
  2421. * This function must be called after cgroup_migrate_add_src() has been
  2422. * called on each migration source css_set. After migration is performed
  2423. * using cgroup_migrate(), cgroup_migrate_finish() must be called on
  2424. * @mgctx.
  2425. */
  2426. int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx)
  2427. {
  2428. struct css_set *src_cset, *tmp_cset;
  2429. lockdep_assert_held(&cgroup_mutex);
  2430. /* look up the dst cset for each src cset and link it to src */
  2431. list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets,
  2432. mg_src_preload_node) {
  2433. struct css_set *dst_cset;
  2434. struct cgroup_subsys *ss;
  2435. int ssid;
  2436. dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp);
  2437. if (!dst_cset)
  2438. return -ENOMEM;
  2439. WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
  2440. /*
  2441. * If src cset equals dst, it's noop. Drop the src.
  2442. * cgroup_migrate() will skip the cset too. Note that we
  2443. * can't handle src == dst as some nodes are used by both.
  2444. */
  2445. if (src_cset == dst_cset) {
  2446. src_cset->mg_src_cgrp = NULL;
  2447. src_cset->mg_dst_cgrp = NULL;
  2448. list_del_init(&src_cset->mg_src_preload_node);
  2449. put_css_set(src_cset);
  2450. put_css_set(dst_cset);
  2451. continue;
  2452. }
  2453. src_cset->mg_dst_cset = dst_cset;
  2454. if (list_empty(&dst_cset->mg_dst_preload_node))
  2455. list_add_tail(&dst_cset->mg_dst_preload_node,
  2456. &mgctx->preloaded_dst_csets);
  2457. else
  2458. put_css_set(dst_cset);
  2459. for_each_subsys(ss, ssid)
  2460. if (src_cset->subsys[ssid] != dst_cset->subsys[ssid])
  2461. mgctx->ss_mask |= 1 << ssid;
  2462. }
  2463. return 0;
  2464. }
  2465. /**
  2466. * cgroup_migrate - migrate a process or task to a cgroup
  2467. * @leader: the leader of the process or the task to migrate
  2468. * @threadgroup: whether @leader points to the whole process or a single task
  2469. * @mgctx: migration context
  2470. *
  2471. * Migrate a process or task denoted by @leader. If migrating a process,
  2472. * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
  2473. * responsible for invoking cgroup_migrate_add_src() and
  2474. * cgroup_migrate_prepare_dst() on the targets before invoking this
  2475. * function and following up with cgroup_migrate_finish().
  2476. *
  2477. * As long as a controller's ->can_attach() doesn't fail, this function is
  2478. * guaranteed to succeed. This means that, excluding ->can_attach()
  2479. * failure, when migrating multiple targets, the success or failure can be
  2480. * decided for all targets by invoking group_migrate_prepare_dst() before
  2481. * actually starting migrating.
  2482. */
  2483. int cgroup_migrate(struct task_struct *leader, bool threadgroup,
  2484. struct cgroup_mgctx *mgctx)
  2485. {
  2486. struct task_struct *task;
  2487. /*
  2488. * The following thread iteration should be inside an RCU critical
  2489. * section to prevent tasks from being freed while taking the snapshot.
  2490. * spin_lock_irq() implies RCU critical section here.
  2491. */
  2492. spin_lock_irq(&css_set_lock);
  2493. task = leader;
  2494. do {
  2495. cgroup_migrate_add_task(task, mgctx);
  2496. if (!threadgroup)
  2497. break;
  2498. } while_each_thread(leader, task);
  2499. spin_unlock_irq(&css_set_lock);
  2500. return cgroup_migrate_execute(mgctx);
  2501. }
  2502. /**
  2503. * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
  2504. * @dst_cgrp: the cgroup to attach to
  2505. * @leader: the task or the leader of the threadgroup to be attached
  2506. * @threadgroup: attach the whole threadgroup?
  2507. *
  2508. * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
  2509. */
  2510. int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
  2511. bool threadgroup)
  2512. {
  2513. DEFINE_CGROUP_MGCTX(mgctx);
  2514. struct task_struct *task;
  2515. int ret = 0;
  2516. /* look up all src csets */
  2517. spin_lock_irq(&css_set_lock);
  2518. rcu_read_lock();
  2519. task = leader;
  2520. do {
  2521. cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);
  2522. if (!threadgroup)
  2523. break;
  2524. } while_each_thread(leader, task);
  2525. rcu_read_unlock();
  2526. spin_unlock_irq(&css_set_lock);
  2527. /* prepare dst csets and commit */
  2528. ret = cgroup_migrate_prepare_dst(&mgctx);
  2529. if (!ret)
  2530. ret = cgroup_migrate(leader, threadgroup, &mgctx);
  2531. cgroup_migrate_finish(&mgctx);
  2532. if (!ret)
  2533. TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup);
  2534. return ret;
  2535. }
  2536. struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,
  2537. bool *threadgroup_locked)
  2538. {
  2539. struct task_struct *tsk;
  2540. pid_t pid;
  2541. if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
  2542. return ERR_PTR(-EINVAL);
  2543. /*
  2544. * If we migrate a single thread, we don't care about threadgroup
  2545. * stability. If the thread is `current`, it won't exit(2) under our
  2546. * hands or change PID through exec(2). We exclude
  2547. * cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write
  2548. * callers by cgroup_mutex.
  2549. * Therefore, we can skip the global lock.
  2550. */
  2551. lockdep_assert_held(&cgroup_mutex);
  2552. *threadgroup_locked = pid || threadgroup;
  2553. cgroup_attach_lock(*threadgroup_locked);
  2554. rcu_read_lock();
  2555. if (pid) {
  2556. tsk = find_task_by_vpid(pid);
  2557. if (!tsk) {
  2558. tsk = ERR_PTR(-ESRCH);
  2559. goto out_unlock_threadgroup;
  2560. }
  2561. } else {
  2562. tsk = current;
  2563. }
  2564. if (threadgroup)
  2565. tsk = tsk->group_leader;
  2566. /*
  2567. * kthreads may acquire PF_NO_SETAFFINITY during initialization.
  2568. * If userland migrates such a kthread to a non-root cgroup, it can
  2569. * become trapped in a cpuset, or RT kthread may be born in a
  2570. * cgroup with no rt_runtime allocated. Just say no.
  2571. */
  2572. if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) {
  2573. tsk = ERR_PTR(-EINVAL);
  2574. goto out_unlock_threadgroup;
  2575. }
  2576. get_task_struct(tsk);
  2577. goto out_unlock_rcu;
  2578. out_unlock_threadgroup:
  2579. cgroup_attach_unlock(*threadgroup_locked);
  2580. *threadgroup_locked = false;
  2581. out_unlock_rcu:
  2582. rcu_read_unlock();
  2583. return tsk;
  2584. }
  2585. void cgroup_procs_write_finish(struct task_struct *task, bool threadgroup_locked)
  2586. {
  2587. struct cgroup_subsys *ss;
  2588. int ssid;
  2589. /* release reference from cgroup_procs_write_start() */
  2590. put_task_struct(task);
  2591. cgroup_attach_unlock(threadgroup_locked);
  2592. for_each_subsys(ss, ssid)
  2593. if (ss->post_attach)
  2594. ss->post_attach();
  2595. }
  2596. static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
  2597. {
  2598. struct cgroup_subsys *ss;
  2599. bool printed = false;
  2600. int ssid;
  2601. do_each_subsys_mask(ss, ssid, ss_mask) {
  2602. if (printed)
  2603. seq_putc(seq, ' ');
  2604. seq_puts(seq, ss->name);
  2605. printed = true;
  2606. } while_each_subsys_mask();
  2607. if (printed)
  2608. seq_putc(seq, '\n');
  2609. }
  2610. /* show controllers which are enabled from the parent */
  2611. static int cgroup_controllers_show(struct seq_file *seq, void *v)
  2612. {
  2613. struct cgroup *cgrp = seq_css(seq)->cgroup;
  2614. cgroup_print_ss_mask(seq, cgroup_control(cgrp));
  2615. return 0;
  2616. }
  2617. /* show controllers which are enabled for a given cgroup's children */
  2618. static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
  2619. {
  2620. struct cgroup *cgrp = seq_css(seq)->cgroup;
  2621. cgroup_print_ss_mask(seq, cgrp->subtree_control);
  2622. return 0;
  2623. }
  2624. /**
  2625. * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
  2626. * @cgrp: root of the subtree to update csses for
  2627. *
  2628. * @cgrp's control masks have changed and its subtree's css associations
  2629. * need to be updated accordingly. This function looks up all css_sets
  2630. * which are attached to the subtree, creates the matching updated css_sets
  2631. * and migrates the tasks to the new ones.
  2632. */
  2633. static int cgroup_update_dfl_csses(struct cgroup *cgrp)
  2634. {
  2635. DEFINE_CGROUP_MGCTX(mgctx);
  2636. struct cgroup_subsys_state *d_css;
  2637. struct cgroup *dsct;
  2638. struct css_set *src_cset;
  2639. bool has_tasks;
  2640. int ret;
  2641. lockdep_assert_held(&cgroup_mutex);
  2642. /* look up all csses currently attached to @cgrp's subtree */
  2643. spin_lock_irq(&css_set_lock);
  2644. cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
  2645. struct cgrp_cset_link *link;
  2646. /*
  2647. * As cgroup_update_dfl_csses() is only called by
  2648. * cgroup_apply_control(). The csses associated with the
  2649. * given cgrp will not be affected by changes made to
  2650. * its subtree_control file. We can skip them.
  2651. */
  2652. if (dsct == cgrp)
  2653. continue;
  2654. list_for_each_entry(link, &dsct->cset_links, cset_link)
  2655. cgroup_migrate_add_src(link->cset, dsct, &mgctx);
  2656. }
  2657. spin_unlock_irq(&css_set_lock);
  2658. /*
  2659. * We need to write-lock threadgroup_rwsem while migrating tasks.
  2660. * However, if there are no source csets for @cgrp, changing its
  2661. * controllers isn't gonna produce any task migrations and the
  2662. * write-locking can be skipped safely.
  2663. */
  2664. has_tasks = !list_empty(&mgctx.preloaded_src_csets);
  2665. cgroup_attach_lock(has_tasks);
  2666. /* NULL dst indicates self on default hierarchy */
  2667. ret = cgroup_migrate_prepare_dst(&mgctx);
  2668. if (ret)
  2669. goto out_finish;
  2670. spin_lock_irq(&css_set_lock);
  2671. list_for_each_entry(src_cset, &mgctx.preloaded_src_csets,
  2672. mg_src_preload_node) {
  2673. struct task_struct *task, *ntask;
  2674. /* all tasks in src_csets need to be migrated */
  2675. list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
  2676. cgroup_migrate_add_task(task, &mgctx);
  2677. }
  2678. spin_unlock_irq(&css_set_lock);
  2679. ret = cgroup_migrate_execute(&mgctx);
  2680. out_finish:
  2681. cgroup_migrate_finish(&mgctx);
  2682. cgroup_attach_unlock(has_tasks);
  2683. return ret;
  2684. }
  2685. /**
  2686. * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
  2687. * @cgrp: root of the target subtree
  2688. *
  2689. * Because css offlining is asynchronous, userland may try to re-enable a
  2690. * controller while the previous css is still around. This function grabs
  2691. * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
  2692. */
  2693. void cgroup_lock_and_drain_offline(struct cgroup *cgrp)
  2694. __acquires(&cgroup_mutex)
  2695. {
  2696. struct cgroup *dsct;
  2697. struct cgroup_subsys_state *d_css;
  2698. struct cgroup_subsys *ss;
  2699. int ssid;
  2700. restart:
  2701. cgroup_lock();
  2702. cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
  2703. for_each_subsys(ss, ssid) {
  2704. struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
  2705. DEFINE_WAIT(wait);
  2706. if (!css || !percpu_ref_is_dying(&css->refcnt))
  2707. continue;
  2708. cgroup_get_live(dsct);
  2709. prepare_to_wait(&dsct->offline_waitq, &wait,
  2710. TASK_UNINTERRUPTIBLE);
  2711. cgroup_unlock();
  2712. schedule();
  2713. finish_wait(&dsct->offline_waitq, &wait);
  2714. cgroup_put(dsct);
  2715. goto restart;
  2716. }
  2717. }
  2718. }
  2719. /**
  2720. * cgroup_save_control - save control masks and dom_cgrp of a subtree
  2721. * @cgrp: root of the target subtree
  2722. *
  2723. * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the
  2724. * respective old_ prefixed fields for @cgrp's subtree including @cgrp
  2725. * itself.
  2726. */
  2727. static void cgroup_save_control(struct cgroup *cgrp)
  2728. {
  2729. struct cgroup *dsct;
  2730. struct cgroup_subsys_state *d_css;
  2731. cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
  2732. dsct->old_subtree_control = dsct->subtree_control;
  2733. dsct->old_subtree_ss_mask = dsct->subtree_ss_mask;
  2734. dsct->old_dom_cgrp = dsct->dom_cgrp;
  2735. }
  2736. }
  2737. /**
  2738. * cgroup_propagate_control - refresh control masks of a subtree
  2739. * @cgrp: root of the target subtree
  2740. *
  2741. * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
  2742. * ->subtree_control and propagate controller availability through the
  2743. * subtree so that descendants don't have unavailable controllers enabled.
  2744. */
  2745. static void cgroup_propagate_control(struct cgroup *cgrp)
  2746. {
  2747. struct cgroup *dsct;
  2748. struct cgroup_subsys_state *d_css;
  2749. cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
  2750. dsct->subtree_control &= cgroup_control(dsct);
  2751. dsct->subtree_ss_mask =
  2752. cgroup_calc_subtree_ss_mask(dsct->subtree_control,
  2753. cgroup_ss_mask(dsct));
  2754. }
  2755. }
  2756. /**
  2757. * cgroup_restore_control - restore control masks and dom_cgrp of a subtree
  2758. * @cgrp: root of the target subtree
  2759. *
  2760. * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the
  2761. * respective old_ prefixed fields for @cgrp's subtree including @cgrp
  2762. * itself.
  2763. */
  2764. static void cgroup_restore_control(struct cgroup *cgrp)
  2765. {
  2766. struct cgroup *dsct;
  2767. struct cgroup_subsys_state *d_css;
  2768. cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
  2769. dsct->subtree_control = dsct->old_subtree_control;
  2770. dsct->subtree_ss_mask = dsct->old_subtree_ss_mask;
  2771. dsct->dom_cgrp = dsct->old_dom_cgrp;
  2772. }
  2773. }
  2774. static bool css_visible(struct cgroup_subsys_state *css)
  2775. {
  2776. struct cgroup_subsys *ss = css->ss;
  2777. struct cgroup *cgrp = css->cgroup;
  2778. if (cgroup_control(cgrp) & (1 << ss->id))
  2779. return true;
  2780. if (!(cgroup_ss_mask(cgrp) & (1 << ss->id)))
  2781. return false;
  2782. return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl;
  2783. }
  2784. /**
  2785. * cgroup_apply_control_enable - enable or show csses according to control
  2786. * @cgrp: root of the target subtree
  2787. *
  2788. * Walk @cgrp's subtree and create new csses or make the existing ones
  2789. * visible. A css is created invisible if it's being implicitly enabled
  2790. * through dependency. An invisible css is made visible when the userland
  2791. * explicitly enables it.
  2792. *
  2793. * Returns 0 on success, -errno on failure. On failure, csses which have
  2794. * been processed already aren't cleaned up. The caller is responsible for
  2795. * cleaning up with cgroup_apply_control_disable().
  2796. */
  2797. static int cgroup_apply_control_enable(struct cgroup *cgrp)
  2798. {
  2799. struct cgroup *dsct;
  2800. struct cgroup_subsys_state *d_css;
  2801. struct cgroup_subsys *ss;
  2802. int ssid, ret;
  2803. cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
  2804. for_each_subsys(ss, ssid) {
  2805. struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
  2806. if (!(cgroup_ss_mask(dsct) & (1 << ss->id)))
  2807. continue;
  2808. if (!css) {
  2809. css = css_create(dsct, ss);
  2810. if (IS_ERR(css))
  2811. return PTR_ERR(css);
  2812. }
  2813. WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt));
  2814. if (css_visible(css)) {
  2815. ret = css_populate_dir(css);
  2816. if (ret)
  2817. return ret;
  2818. }
  2819. }
  2820. }
  2821. return 0;
  2822. }
  2823. /**
  2824. * cgroup_apply_control_disable - kill or hide csses according to control
  2825. * @cgrp: root of the target subtree
  2826. *
  2827. * Walk @cgrp's subtree and kill and hide csses so that they match
  2828. * cgroup_ss_mask() and cgroup_visible_mask().
  2829. *
  2830. * A css is hidden when the userland requests it to be disabled while other
  2831. * subsystems are still depending on it. The css must not actively control
  2832. * resources and be in the vanilla state if it's made visible again later.
  2833. * Controllers which may be depended upon should provide ->css_reset() for
  2834. * this purpose.
  2835. */
  2836. static void cgroup_apply_control_disable(struct cgroup *cgrp)
  2837. {
  2838. struct cgroup *dsct;
  2839. struct cgroup_subsys_state *d_css;
  2840. struct cgroup_subsys *ss;
  2841. int ssid;
  2842. cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
  2843. for_each_subsys(ss, ssid) {
  2844. struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
  2845. if (!css)
  2846. continue;
  2847. WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt));
  2848. if (css->parent &&
  2849. !(cgroup_ss_mask(dsct) & (1 << ss->id))) {
  2850. kill_css(css);
  2851. } else if (!css_visible(css)) {
  2852. css_clear_dir(css);
  2853. if (ss->css_reset)
  2854. ss->css_reset(css);
  2855. }
  2856. }
  2857. }
  2858. }
  2859. /**
  2860. * cgroup_apply_control - apply control mask updates to the subtree
  2861. * @cgrp: root of the target subtree
  2862. *
  2863. * subsystems can be enabled and disabled in a subtree using the following
  2864. * steps.
  2865. *
  2866. * 1. Call cgroup_save_control() to stash the current state.
  2867. * 2. Update ->subtree_control masks in the subtree as desired.
  2868. * 3. Call cgroup_apply_control() to apply the changes.
  2869. * 4. Optionally perform other related operations.
  2870. * 5. Call cgroup_finalize_control() to finish up.
  2871. *
  2872. * This function implements step 3 and propagates the mask changes
  2873. * throughout @cgrp's subtree, updates csses accordingly and perform
  2874. * process migrations.
  2875. */
  2876. static int cgroup_apply_control(struct cgroup *cgrp)
  2877. {
  2878. int ret;
  2879. cgroup_propagate_control(cgrp);
  2880. ret = cgroup_apply_control_enable(cgrp);
  2881. if (ret)
  2882. return ret;
  2883. /*
  2884. * At this point, cgroup_e_css_by_mask() results reflect the new csses
  2885. * making the following cgroup_update_dfl_csses() properly update
  2886. * css associations of all tasks in the subtree.
  2887. */
  2888. return cgroup_update_dfl_csses(cgrp);
  2889. }
  2890. /**
  2891. * cgroup_finalize_control - finalize control mask update
  2892. * @cgrp: root of the target subtree
  2893. * @ret: the result of the update
  2894. *
  2895. * Finalize control mask update. See cgroup_apply_control() for more info.
  2896. */
  2897. static void cgroup_finalize_control(struct cgroup *cgrp, int ret)
  2898. {
  2899. if (ret) {
  2900. cgroup_restore_control(cgrp);
  2901. cgroup_propagate_control(cgrp);
  2902. }
  2903. cgroup_apply_control_disable(cgrp);
  2904. }
  2905. static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable)
  2906. {
  2907. u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask;
  2908. /* if nothing is getting enabled, nothing to worry about */
  2909. if (!enable)
  2910. return 0;
  2911. /* can @cgrp host any resources? */
  2912. if (!cgroup_is_valid_domain(cgrp->dom_cgrp))
  2913. return -EOPNOTSUPP;
  2914. /* mixables don't care */
  2915. if (cgroup_is_mixable(cgrp))
  2916. return 0;
  2917. if (domain_enable) {
  2918. /* can't enable domain controllers inside a thread subtree */
  2919. if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp))
  2920. return -EOPNOTSUPP;
  2921. } else {
  2922. /*
  2923. * Threaded controllers can handle internal competitions
  2924. * and are always allowed inside a (prospective) thread
  2925. * subtree.
  2926. */
  2927. if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp))
  2928. return 0;
  2929. }
  2930. /*
  2931. * Controllers can't be enabled for a cgroup with tasks to avoid
  2932. * child cgroups competing against tasks.
  2933. */
  2934. if (cgroup_has_tasks(cgrp))
  2935. return -EBUSY;
  2936. return 0;
  2937. }
  2938. /* change the enabled child controllers for a cgroup in the default hierarchy */
  2939. static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
  2940. char *buf, size_t nbytes,
  2941. loff_t off)
  2942. {
  2943. u16 enable = 0, disable = 0;
  2944. struct cgroup *cgrp, *child;
  2945. struct cgroup_subsys *ss;
  2946. char *tok;
  2947. int ssid, ret;
  2948. /*
  2949. * Parse input - space separated list of subsystem names prefixed
  2950. * with either + or -.
  2951. */
  2952. buf = strstrip(buf);
  2953. while ((tok = strsep(&buf, " "))) {
  2954. if (tok[0] == '\0')
  2955. continue;
  2956. do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
  2957. if (!cgroup_ssid_enabled(ssid) ||
  2958. strcmp(tok + 1, ss->name))
  2959. continue;
  2960. if (*tok == '+') {
  2961. enable |= 1 << ssid;
  2962. disable &= ~(1 << ssid);
  2963. } else if (*tok == '-') {
  2964. disable |= 1 << ssid;
  2965. enable &= ~(1 << ssid);
  2966. } else {
  2967. return -EINVAL;
  2968. }
  2969. break;
  2970. } while_each_subsys_mask();
  2971. if (ssid == CGROUP_SUBSYS_COUNT)
  2972. return -EINVAL;
  2973. }
  2974. cgrp = cgroup_kn_lock_live(of->kn, true);
  2975. if (!cgrp)
  2976. return -ENODEV;
  2977. for_each_subsys(ss, ssid) {
  2978. if (enable & (1 << ssid)) {
  2979. if (cgrp->subtree_control & (1 << ssid)) {
  2980. enable &= ~(1 << ssid);
  2981. continue;
  2982. }
  2983. if (!(cgroup_control(cgrp) & (1 << ssid))) {
  2984. ret = -ENOENT;
  2985. goto out_unlock;
  2986. }
  2987. } else if (disable & (1 << ssid)) {
  2988. if (!(cgrp->subtree_control & (1 << ssid))) {
  2989. disable &= ~(1 << ssid);
  2990. continue;
  2991. }
  2992. /* a child has it enabled? */
  2993. cgroup_for_each_live_child(child, cgrp) {
  2994. if (child->subtree_control & (1 << ssid)) {
  2995. ret = -EBUSY;
  2996. goto out_unlock;
  2997. }
  2998. }
  2999. }
  3000. }
  3001. if (!enable && !disable) {
  3002. ret = 0;
  3003. goto out_unlock;
  3004. }
  3005. ret = cgroup_vet_subtree_control_enable(cgrp, enable);
  3006. if (ret)
  3007. goto out_unlock;
  3008. /* save and update control masks and prepare csses */
  3009. cgroup_save_control(cgrp);
  3010. cgrp->subtree_control |= enable;
  3011. cgrp->subtree_control &= ~disable;
  3012. ret = cgroup_apply_control(cgrp);
  3013. cgroup_finalize_control(cgrp, ret);
  3014. if (ret)
  3015. goto out_unlock;
  3016. kernfs_activate(cgrp->kn);
  3017. out_unlock:
  3018. cgroup_kn_unlock(of->kn);
  3019. return ret ?: nbytes;
  3020. }
  3021. /**
  3022. * cgroup_enable_threaded - make @cgrp threaded
  3023. * @cgrp: the target cgroup
  3024. *
  3025. * Called when "threaded" is written to the cgroup.type interface file and
  3026. * tries to make @cgrp threaded and join the parent's resource domain.
  3027. * This function is never called on the root cgroup as cgroup.type doesn't
  3028. * exist on it.
  3029. */
  3030. static int cgroup_enable_threaded(struct cgroup *cgrp)
  3031. {
  3032. struct cgroup *parent = cgroup_parent(cgrp);
  3033. struct cgroup *dom_cgrp = parent->dom_cgrp;
  3034. struct cgroup *dsct;
  3035. struct cgroup_subsys_state *d_css;
  3036. int ret;
  3037. lockdep_assert_held(&cgroup_mutex);
  3038. /* noop if already threaded */
  3039. if (cgroup_is_threaded(cgrp))
  3040. return 0;
  3041. /*
  3042. * If @cgroup is populated or has domain controllers enabled, it
  3043. * can't be switched. While the below cgroup_can_be_thread_root()
  3044. * test can catch the same conditions, that's only when @parent is
  3045. * not mixable, so let's check it explicitly.
  3046. */
  3047. if (cgroup_is_populated(cgrp) ||
  3048. cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
  3049. return -EOPNOTSUPP;
  3050. /* we're joining the parent's domain, ensure its validity */
  3051. if (!cgroup_is_valid_domain(dom_cgrp) ||
  3052. !cgroup_can_be_thread_root(dom_cgrp))
  3053. return -EOPNOTSUPP;
  3054. /*
  3055. * The following shouldn't cause actual migrations and should
  3056. * always succeed.
  3057. */
  3058. cgroup_save_control(cgrp);
  3059. cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp)
  3060. if (dsct == cgrp || cgroup_is_threaded(dsct))
  3061. dsct->dom_cgrp = dom_cgrp;
  3062. ret = cgroup_apply_control(cgrp);
  3063. if (!ret)
  3064. parent->nr_threaded_children++;
  3065. cgroup_finalize_control(cgrp, ret);
  3066. return ret;
  3067. }
  3068. static int cgroup_type_show(struct seq_file *seq, void *v)
  3069. {
  3070. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3071. if (cgroup_is_threaded(cgrp))
  3072. seq_puts(seq, "threaded\n");
  3073. else if (!cgroup_is_valid_domain(cgrp))
  3074. seq_puts(seq, "domain invalid\n");
  3075. else if (cgroup_is_thread_root(cgrp))
  3076. seq_puts(seq, "domain threaded\n");
  3077. else
  3078. seq_puts(seq, "domain\n");
  3079. return 0;
  3080. }
  3081. static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf,
  3082. size_t nbytes, loff_t off)
  3083. {
  3084. struct cgroup *cgrp;
  3085. int ret;
  3086. /* only switching to threaded mode is supported */
  3087. if (strcmp(strstrip(buf), "threaded"))
  3088. return -EINVAL;
  3089. /* drain dying csses before we re-apply (threaded) subtree control */
  3090. cgrp = cgroup_kn_lock_live(of->kn, true);
  3091. if (!cgrp)
  3092. return -ENOENT;
  3093. /* threaded can only be enabled */
  3094. ret = cgroup_enable_threaded(cgrp);
  3095. cgroup_kn_unlock(of->kn);
  3096. return ret ?: nbytes;
  3097. }
  3098. static int cgroup_max_descendants_show(struct seq_file *seq, void *v)
  3099. {
  3100. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3101. int descendants = READ_ONCE(cgrp->max_descendants);
  3102. if (descendants == INT_MAX)
  3103. seq_puts(seq, "max\n");
  3104. else
  3105. seq_printf(seq, "%d\n", descendants);
  3106. return 0;
  3107. }
  3108. static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of,
  3109. char *buf, size_t nbytes, loff_t off)
  3110. {
  3111. struct cgroup *cgrp;
  3112. int descendants;
  3113. ssize_t ret;
  3114. buf = strstrip(buf);
  3115. if (!strcmp(buf, "max")) {
  3116. descendants = INT_MAX;
  3117. } else {
  3118. ret = kstrtoint(buf, 0, &descendants);
  3119. if (ret)
  3120. return ret;
  3121. }
  3122. if (descendants < 0)
  3123. return -ERANGE;
  3124. cgrp = cgroup_kn_lock_live(of->kn, false);
  3125. if (!cgrp)
  3126. return -ENOENT;
  3127. cgrp->max_descendants = descendants;
  3128. cgroup_kn_unlock(of->kn);
  3129. return nbytes;
  3130. }
  3131. static int cgroup_max_depth_show(struct seq_file *seq, void *v)
  3132. {
  3133. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3134. int depth = READ_ONCE(cgrp->max_depth);
  3135. if (depth == INT_MAX)
  3136. seq_puts(seq, "max\n");
  3137. else
  3138. seq_printf(seq, "%d\n", depth);
  3139. return 0;
  3140. }
  3141. static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of,
  3142. char *buf, size_t nbytes, loff_t off)
  3143. {
  3144. struct cgroup *cgrp;
  3145. ssize_t ret;
  3146. int depth;
  3147. buf = strstrip(buf);
  3148. if (!strcmp(buf, "max")) {
  3149. depth = INT_MAX;
  3150. } else {
  3151. ret = kstrtoint(buf, 0, &depth);
  3152. if (ret)
  3153. return ret;
  3154. }
  3155. if (depth < 0)
  3156. return -ERANGE;
  3157. cgrp = cgroup_kn_lock_live(of->kn, false);
  3158. if (!cgrp)
  3159. return -ENOENT;
  3160. cgrp->max_depth = depth;
  3161. cgroup_kn_unlock(of->kn);
  3162. return nbytes;
  3163. }
  3164. static int cgroup_events_show(struct seq_file *seq, void *v)
  3165. {
  3166. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3167. seq_printf(seq, "populated %d\n", cgroup_is_populated(cgrp));
  3168. seq_printf(seq, "frozen %d\n", test_bit(CGRP_FROZEN, &cgrp->flags));
  3169. return 0;
  3170. }
  3171. static int cgroup_stat_show(struct seq_file *seq, void *v)
  3172. {
  3173. struct cgroup *cgroup = seq_css(seq)->cgroup;
  3174. struct cgroup_subsys_state *css;
  3175. int dying_cnt[CGROUP_SUBSYS_COUNT];
  3176. int ssid;
  3177. seq_printf(seq, "nr_descendants %d\n",
  3178. cgroup->nr_descendants);
  3179. /*
  3180. * Show the number of live and dying csses associated with each of
  3181. * non-inhibited cgroup subsystems that is bound to cgroup v2.
  3182. *
  3183. * Without proper lock protection, racing is possible. So the
  3184. * numbers may not be consistent when that happens.
  3185. */
  3186. rcu_read_lock();
  3187. for (ssid = 0; ssid < CGROUP_SUBSYS_COUNT; ssid++) {
  3188. dying_cnt[ssid] = -1;
  3189. if ((BIT(ssid) & cgrp_dfl_inhibit_ss_mask) ||
  3190. (cgroup_subsys[ssid]->root != &cgrp_dfl_root))
  3191. continue;
  3192. css = rcu_dereference_raw(cgroup->subsys[ssid]);
  3193. dying_cnt[ssid] = cgroup->nr_dying_subsys[ssid];
  3194. seq_printf(seq, "nr_subsys_%s %d\n", cgroup_subsys[ssid]->name,
  3195. css ? (css->nr_descendants + 1) : 0);
  3196. }
  3197. seq_printf(seq, "nr_dying_descendants %d\n",
  3198. cgroup->nr_dying_descendants);
  3199. for (ssid = 0; ssid < CGROUP_SUBSYS_COUNT; ssid++) {
  3200. if (dying_cnt[ssid] >= 0)
  3201. seq_printf(seq, "nr_dying_subsys_%s %d\n",
  3202. cgroup_subsys[ssid]->name, dying_cnt[ssid]);
  3203. }
  3204. rcu_read_unlock();
  3205. return 0;
  3206. }
  3207. #ifdef CONFIG_CGROUP_SCHED
  3208. /**
  3209. * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem
  3210. * @cgrp: the cgroup of interest
  3211. * @ss: the subsystem of interest
  3212. *
  3213. * Find and get @cgrp's css associated with @ss. If the css doesn't exist
  3214. * or is offline, %NULL is returned.
  3215. */
  3216. static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp,
  3217. struct cgroup_subsys *ss)
  3218. {
  3219. struct cgroup_subsys_state *css;
  3220. rcu_read_lock();
  3221. css = cgroup_css(cgrp, ss);
  3222. if (css && !css_tryget_online(css))
  3223. css = NULL;
  3224. rcu_read_unlock();
  3225. return css;
  3226. }
  3227. static int cgroup_extra_stat_show(struct seq_file *seq, int ssid)
  3228. {
  3229. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3230. struct cgroup_subsys *ss = cgroup_subsys[ssid];
  3231. struct cgroup_subsys_state *css;
  3232. int ret;
  3233. if (!ss->css_extra_stat_show)
  3234. return 0;
  3235. css = cgroup_tryget_css(cgrp, ss);
  3236. if (!css)
  3237. return 0;
  3238. ret = ss->css_extra_stat_show(seq, css);
  3239. css_put(css);
  3240. return ret;
  3241. }
  3242. static int cgroup_local_stat_show(struct seq_file *seq,
  3243. struct cgroup *cgrp, int ssid)
  3244. {
  3245. struct cgroup_subsys *ss = cgroup_subsys[ssid];
  3246. struct cgroup_subsys_state *css;
  3247. int ret;
  3248. if (!ss->css_local_stat_show)
  3249. return 0;
  3250. css = cgroup_tryget_css(cgrp, ss);
  3251. if (!css)
  3252. return 0;
  3253. ret = ss->css_local_stat_show(seq, css);
  3254. css_put(css);
  3255. return ret;
  3256. }
  3257. #endif
  3258. static int cpu_stat_show(struct seq_file *seq, void *v)
  3259. {
  3260. int ret = 0;
  3261. cgroup_base_stat_cputime_show(seq);
  3262. #ifdef CONFIG_CGROUP_SCHED
  3263. ret = cgroup_extra_stat_show(seq, cpu_cgrp_id);
  3264. #endif
  3265. return ret;
  3266. }
  3267. static int cpu_local_stat_show(struct seq_file *seq, void *v)
  3268. {
  3269. struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup;
  3270. int ret = 0;
  3271. #ifdef CONFIG_CGROUP_SCHED
  3272. ret = cgroup_local_stat_show(seq, cgrp, cpu_cgrp_id);
  3273. #endif
  3274. return ret;
  3275. }
  3276. #ifdef CONFIG_PSI
  3277. static int cgroup_io_pressure_show(struct seq_file *seq, void *v)
  3278. {
  3279. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3280. struct psi_group *psi = cgroup_psi(cgrp);
  3281. return psi_show(seq, psi, PSI_IO);
  3282. }
  3283. static int cgroup_memory_pressure_show(struct seq_file *seq, void *v)
  3284. {
  3285. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3286. struct psi_group *psi = cgroup_psi(cgrp);
  3287. return psi_show(seq, psi, PSI_MEM);
  3288. }
  3289. static int cgroup_cpu_pressure_show(struct seq_file *seq, void *v)
  3290. {
  3291. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3292. struct psi_group *psi = cgroup_psi(cgrp);
  3293. return psi_show(seq, psi, PSI_CPU);
  3294. }
  3295. static ssize_t pressure_write(struct kernfs_open_file *of, char *buf,
  3296. size_t nbytes, enum psi_res res)
  3297. {
  3298. struct cgroup_file_ctx *ctx = of->priv;
  3299. struct psi_trigger *new;
  3300. struct cgroup *cgrp;
  3301. struct psi_group *psi;
  3302. cgrp = cgroup_kn_lock_live(of->kn, false);
  3303. if (!cgrp)
  3304. return -ENODEV;
  3305. cgroup_get(cgrp);
  3306. cgroup_kn_unlock(of->kn);
  3307. /* Allow only one trigger per file descriptor */
  3308. if (ctx->psi.trigger) {
  3309. cgroup_put(cgrp);
  3310. return -EBUSY;
  3311. }
  3312. psi = cgroup_psi(cgrp);
  3313. new = psi_trigger_create(psi, buf, res, of->file, of);
  3314. if (IS_ERR(new)) {
  3315. cgroup_put(cgrp);
  3316. return PTR_ERR(new);
  3317. }
  3318. smp_store_release(&ctx->psi.trigger, new);
  3319. cgroup_put(cgrp);
  3320. return nbytes;
  3321. }
  3322. static ssize_t cgroup_io_pressure_write(struct kernfs_open_file *of,
  3323. char *buf, size_t nbytes,
  3324. loff_t off)
  3325. {
  3326. return pressure_write(of, buf, nbytes, PSI_IO);
  3327. }
  3328. static ssize_t cgroup_memory_pressure_write(struct kernfs_open_file *of,
  3329. char *buf, size_t nbytes,
  3330. loff_t off)
  3331. {
  3332. return pressure_write(of, buf, nbytes, PSI_MEM);
  3333. }
  3334. static ssize_t cgroup_cpu_pressure_write(struct kernfs_open_file *of,
  3335. char *buf, size_t nbytes,
  3336. loff_t off)
  3337. {
  3338. return pressure_write(of, buf, nbytes, PSI_CPU);
  3339. }
  3340. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  3341. static int cgroup_irq_pressure_show(struct seq_file *seq, void *v)
  3342. {
  3343. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3344. struct psi_group *psi = cgroup_psi(cgrp);
  3345. return psi_show(seq, psi, PSI_IRQ);
  3346. }
  3347. static ssize_t cgroup_irq_pressure_write(struct kernfs_open_file *of,
  3348. char *buf, size_t nbytes,
  3349. loff_t off)
  3350. {
  3351. return pressure_write(of, buf, nbytes, PSI_IRQ);
  3352. }
  3353. #endif
  3354. static int cgroup_pressure_show(struct seq_file *seq, void *v)
  3355. {
  3356. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3357. struct psi_group *psi = cgroup_psi(cgrp);
  3358. seq_printf(seq, "%d\n", psi->enabled);
  3359. return 0;
  3360. }
  3361. static ssize_t cgroup_pressure_write(struct kernfs_open_file *of,
  3362. char *buf, size_t nbytes,
  3363. loff_t off)
  3364. {
  3365. ssize_t ret;
  3366. int enable;
  3367. struct cgroup *cgrp;
  3368. struct psi_group *psi;
  3369. ret = kstrtoint(strstrip(buf), 0, &enable);
  3370. if (ret)
  3371. return ret;
  3372. if (enable < 0 || enable > 1)
  3373. return -ERANGE;
  3374. cgrp = cgroup_kn_lock_live(of->kn, false);
  3375. if (!cgrp)
  3376. return -ENOENT;
  3377. psi = cgroup_psi(cgrp);
  3378. if (psi->enabled != enable) {
  3379. int i;
  3380. /* show or hide {cpu,memory,io,irq}.pressure files */
  3381. for (i = 0; i < NR_PSI_RESOURCES; i++)
  3382. cgroup_file_show(&cgrp->psi_files[i], enable);
  3383. psi->enabled = enable;
  3384. if (enable)
  3385. psi_cgroup_restart(psi);
  3386. }
  3387. cgroup_kn_unlock(of->kn);
  3388. return nbytes;
  3389. }
  3390. static __poll_t cgroup_pressure_poll(struct kernfs_open_file *of,
  3391. poll_table *pt)
  3392. {
  3393. struct cgroup_file_ctx *ctx = of->priv;
  3394. return psi_trigger_poll(&ctx->psi.trigger, of->file, pt);
  3395. }
  3396. static void cgroup_pressure_release(struct kernfs_open_file *of)
  3397. {
  3398. struct cgroup_file_ctx *ctx = of->priv;
  3399. psi_trigger_destroy(ctx->psi.trigger);
  3400. }
  3401. bool cgroup_psi_enabled(void)
  3402. {
  3403. if (static_branch_likely(&psi_disabled))
  3404. return false;
  3405. return (cgroup_feature_disable_mask & (1 << OPT_FEATURE_PRESSURE)) == 0;
  3406. }
  3407. #else /* CONFIG_PSI */
  3408. bool cgroup_psi_enabled(void)
  3409. {
  3410. return false;
  3411. }
  3412. #endif /* CONFIG_PSI */
  3413. static int cgroup_freeze_show(struct seq_file *seq, void *v)
  3414. {
  3415. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3416. seq_printf(seq, "%d\n", cgrp->freezer.freeze);
  3417. return 0;
  3418. }
  3419. static ssize_t cgroup_freeze_write(struct kernfs_open_file *of,
  3420. char *buf, size_t nbytes, loff_t off)
  3421. {
  3422. struct cgroup *cgrp;
  3423. ssize_t ret;
  3424. int freeze;
  3425. ret = kstrtoint(strstrip(buf), 0, &freeze);
  3426. if (ret)
  3427. return ret;
  3428. if (freeze < 0 || freeze > 1)
  3429. return -ERANGE;
  3430. cgrp = cgroup_kn_lock_live(of->kn, false);
  3431. if (!cgrp)
  3432. return -ENOENT;
  3433. cgroup_freeze(cgrp, freeze);
  3434. cgroup_kn_unlock(of->kn);
  3435. return nbytes;
  3436. }
  3437. static void __cgroup_kill(struct cgroup *cgrp)
  3438. {
  3439. struct css_task_iter it;
  3440. struct task_struct *task;
  3441. lockdep_assert_held(&cgroup_mutex);
  3442. spin_lock_irq(&css_set_lock);
  3443. cgrp->kill_seq++;
  3444. spin_unlock_irq(&css_set_lock);
  3445. css_task_iter_start(&cgrp->self, CSS_TASK_ITER_PROCS | CSS_TASK_ITER_THREADED, &it);
  3446. while ((task = css_task_iter_next(&it))) {
  3447. /* Ignore kernel threads here. */
  3448. if (task->flags & PF_KTHREAD)
  3449. continue;
  3450. /* Skip tasks that are already dying. */
  3451. if (__fatal_signal_pending(task))
  3452. continue;
  3453. send_sig(SIGKILL, task, 0);
  3454. }
  3455. css_task_iter_end(&it);
  3456. }
  3457. static void cgroup_kill(struct cgroup *cgrp)
  3458. {
  3459. struct cgroup_subsys_state *css;
  3460. struct cgroup *dsct;
  3461. lockdep_assert_held(&cgroup_mutex);
  3462. cgroup_for_each_live_descendant_pre(dsct, css, cgrp)
  3463. __cgroup_kill(dsct);
  3464. }
  3465. static ssize_t cgroup_kill_write(struct kernfs_open_file *of, char *buf,
  3466. size_t nbytes, loff_t off)
  3467. {
  3468. ssize_t ret = 0;
  3469. int kill;
  3470. struct cgroup *cgrp;
  3471. ret = kstrtoint(strstrip(buf), 0, &kill);
  3472. if (ret)
  3473. return ret;
  3474. if (kill != 1)
  3475. return -ERANGE;
  3476. cgrp = cgroup_kn_lock_live(of->kn, false);
  3477. if (!cgrp)
  3478. return -ENOENT;
  3479. /*
  3480. * Killing is a process directed operation, i.e. the whole thread-group
  3481. * is taken down so act like we do for cgroup.procs and only make this
  3482. * writable in non-threaded cgroups.
  3483. */
  3484. if (cgroup_is_threaded(cgrp))
  3485. ret = -EOPNOTSUPP;
  3486. else
  3487. cgroup_kill(cgrp);
  3488. cgroup_kn_unlock(of->kn);
  3489. return ret ?: nbytes;
  3490. }
  3491. static int cgroup_file_open(struct kernfs_open_file *of)
  3492. {
  3493. struct cftype *cft = of_cft(of);
  3494. struct cgroup_file_ctx *ctx;
  3495. int ret;
  3496. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  3497. if (!ctx)
  3498. return -ENOMEM;
  3499. ctx->ns = current->nsproxy->cgroup_ns;
  3500. get_cgroup_ns(ctx->ns);
  3501. of->priv = ctx;
  3502. if (!cft->open)
  3503. return 0;
  3504. ret = cft->open(of);
  3505. if (ret) {
  3506. put_cgroup_ns(ctx->ns);
  3507. kfree(ctx);
  3508. }
  3509. return ret;
  3510. }
  3511. static void cgroup_file_release(struct kernfs_open_file *of)
  3512. {
  3513. struct cftype *cft = of_cft(of);
  3514. struct cgroup_file_ctx *ctx = of->priv;
  3515. if (cft->release)
  3516. cft->release(of);
  3517. put_cgroup_ns(ctx->ns);
  3518. kfree(ctx);
  3519. }
  3520. static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
  3521. size_t nbytes, loff_t off)
  3522. {
  3523. struct cgroup_file_ctx *ctx = of->priv;
  3524. struct cgroup *cgrp = of->kn->parent->priv;
  3525. struct cftype *cft = of_cft(of);
  3526. struct cgroup_subsys_state *css;
  3527. int ret;
  3528. if (!nbytes)
  3529. return 0;
  3530. /*
  3531. * If namespaces are delegation boundaries, disallow writes to
  3532. * files in an non-init namespace root from inside the namespace
  3533. * except for the files explicitly marked delegatable -
  3534. * eg. cgroup.procs, cgroup.threads and cgroup.subtree_control.
  3535. */
  3536. if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) &&
  3537. !(cft->flags & CFTYPE_NS_DELEGATABLE) &&
  3538. ctx->ns != &init_cgroup_ns && ctx->ns->root_cset->dfl_cgrp == cgrp)
  3539. return -EPERM;
  3540. if (cft->write)
  3541. return cft->write(of, buf, nbytes, off);
  3542. /*
  3543. * kernfs guarantees that a file isn't deleted with operations in
  3544. * flight, which means that the matching css is and stays alive and
  3545. * doesn't need to be pinned. The RCU locking is not necessary
  3546. * either. It's just for the convenience of using cgroup_css().
  3547. */
  3548. rcu_read_lock();
  3549. css = cgroup_css(cgrp, cft->ss);
  3550. rcu_read_unlock();
  3551. if (cft->write_u64) {
  3552. unsigned long long v;
  3553. ret = kstrtoull(buf, 0, &v);
  3554. if (!ret)
  3555. ret = cft->write_u64(css, cft, v);
  3556. } else if (cft->write_s64) {
  3557. long long v;
  3558. ret = kstrtoll(buf, 0, &v);
  3559. if (!ret)
  3560. ret = cft->write_s64(css, cft, v);
  3561. } else {
  3562. ret = -EINVAL;
  3563. }
  3564. return ret ?: nbytes;
  3565. }
  3566. static __poll_t cgroup_file_poll(struct kernfs_open_file *of, poll_table *pt)
  3567. {
  3568. struct cftype *cft = of_cft(of);
  3569. if (cft->poll)
  3570. return cft->poll(of, pt);
  3571. return kernfs_generic_poll(of, pt);
  3572. }
  3573. static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
  3574. {
  3575. return seq_cft(seq)->seq_start(seq, ppos);
  3576. }
  3577. static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
  3578. {
  3579. return seq_cft(seq)->seq_next(seq, v, ppos);
  3580. }
  3581. static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
  3582. {
  3583. if (seq_cft(seq)->seq_stop)
  3584. seq_cft(seq)->seq_stop(seq, v);
  3585. }
  3586. static int cgroup_seqfile_show(struct seq_file *m, void *arg)
  3587. {
  3588. struct cftype *cft = seq_cft(m);
  3589. struct cgroup_subsys_state *css = seq_css(m);
  3590. if (cft->seq_show)
  3591. return cft->seq_show(m, arg);
  3592. if (cft->read_u64)
  3593. seq_printf(m, "%llu\n", cft->read_u64(css, cft));
  3594. else if (cft->read_s64)
  3595. seq_printf(m, "%lld\n", cft->read_s64(css, cft));
  3596. else
  3597. return -EINVAL;
  3598. return 0;
  3599. }
  3600. static struct kernfs_ops cgroup_kf_single_ops = {
  3601. .atomic_write_len = PAGE_SIZE,
  3602. .open = cgroup_file_open,
  3603. .release = cgroup_file_release,
  3604. .write = cgroup_file_write,
  3605. .poll = cgroup_file_poll,
  3606. .seq_show = cgroup_seqfile_show,
  3607. };
  3608. static struct kernfs_ops cgroup_kf_ops = {
  3609. .atomic_write_len = PAGE_SIZE,
  3610. .open = cgroup_file_open,
  3611. .release = cgroup_file_release,
  3612. .write = cgroup_file_write,
  3613. .poll = cgroup_file_poll,
  3614. .seq_start = cgroup_seqfile_start,
  3615. .seq_next = cgroup_seqfile_next,
  3616. .seq_stop = cgroup_seqfile_stop,
  3617. .seq_show = cgroup_seqfile_show,
  3618. };
  3619. static void cgroup_file_notify_timer(struct timer_list *timer)
  3620. {
  3621. cgroup_file_notify(container_of(timer, struct cgroup_file,
  3622. notify_timer));
  3623. }
  3624. static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
  3625. struct cftype *cft)
  3626. {
  3627. char name[CGROUP_FILE_NAME_MAX];
  3628. struct kernfs_node *kn;
  3629. struct lock_class_key *key = NULL;
  3630. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  3631. key = &cft->lockdep_key;
  3632. #endif
  3633. kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
  3634. cgroup_file_mode(cft),
  3635. current_fsuid(), current_fsgid(),
  3636. 0, cft->kf_ops, cft,
  3637. NULL, key);
  3638. if (IS_ERR(kn))
  3639. return PTR_ERR(kn);
  3640. if (cft->file_offset) {
  3641. struct cgroup_file *cfile = (void *)css + cft->file_offset;
  3642. timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
  3643. spin_lock_irq(&cgroup_file_kn_lock);
  3644. cfile->kn = kn;
  3645. spin_unlock_irq(&cgroup_file_kn_lock);
  3646. }
  3647. return 0;
  3648. }
  3649. /**
  3650. * cgroup_addrm_files - add or remove files to a cgroup directory
  3651. * @css: the target css
  3652. * @cgrp: the target cgroup (usually css->cgroup)
  3653. * @cfts: array of cftypes to be added
  3654. * @is_add: whether to add or remove
  3655. *
  3656. * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
  3657. * For removals, this function never fails.
  3658. */
  3659. static int cgroup_addrm_files(struct cgroup_subsys_state *css,
  3660. struct cgroup *cgrp, struct cftype cfts[],
  3661. bool is_add)
  3662. {
  3663. struct cftype *cft, *cft_end = NULL;
  3664. int ret = 0;
  3665. lockdep_assert_held(&cgroup_mutex);
  3666. restart:
  3667. for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
  3668. /* does cft->flags tell us to skip this file on @cgrp? */
  3669. if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
  3670. continue;
  3671. if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
  3672. continue;
  3673. if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
  3674. continue;
  3675. if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
  3676. continue;
  3677. if ((cft->flags & CFTYPE_DEBUG) && !cgroup_debug)
  3678. continue;
  3679. if (is_add) {
  3680. ret = cgroup_add_file(css, cgrp, cft);
  3681. if (ret) {
  3682. pr_warn("%s: failed to add %s, err=%d\n",
  3683. __func__, cft->name, ret);
  3684. cft_end = cft;
  3685. is_add = false;
  3686. goto restart;
  3687. }
  3688. } else {
  3689. cgroup_rm_file(cgrp, cft);
  3690. }
  3691. }
  3692. return ret;
  3693. }
  3694. static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
  3695. {
  3696. struct cgroup_subsys *ss = cfts[0].ss;
  3697. struct cgroup *root = &ss->root->cgrp;
  3698. struct cgroup_subsys_state *css;
  3699. int ret = 0;
  3700. lockdep_assert_held(&cgroup_mutex);
  3701. /* add/rm files for all cgroups created before */
  3702. css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
  3703. struct cgroup *cgrp = css->cgroup;
  3704. if (!(css->flags & CSS_VISIBLE))
  3705. continue;
  3706. ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
  3707. if (ret)
  3708. break;
  3709. }
  3710. if (is_add && !ret)
  3711. kernfs_activate(root->kn);
  3712. return ret;
  3713. }
  3714. static void cgroup_exit_cftypes(struct cftype *cfts)
  3715. {
  3716. struct cftype *cft;
  3717. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  3718. /* free copy for custom atomic_write_len, see init_cftypes() */
  3719. if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
  3720. kfree(cft->kf_ops);
  3721. cft->kf_ops = NULL;
  3722. cft->ss = NULL;
  3723. /* revert flags set by cgroup core while adding @cfts */
  3724. cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL |
  3725. __CFTYPE_ADDED);
  3726. }
  3727. }
  3728. static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  3729. {
  3730. struct cftype *cft;
  3731. int ret = 0;
  3732. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  3733. struct kernfs_ops *kf_ops;
  3734. WARN_ON(cft->ss || cft->kf_ops);
  3735. if (cft->flags & __CFTYPE_ADDED) {
  3736. ret = -EBUSY;
  3737. break;
  3738. }
  3739. if (cft->seq_start)
  3740. kf_ops = &cgroup_kf_ops;
  3741. else
  3742. kf_ops = &cgroup_kf_single_ops;
  3743. /*
  3744. * Ugh... if @cft wants a custom max_write_len, we need to
  3745. * make a copy of kf_ops to set its atomic_write_len.
  3746. */
  3747. if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
  3748. kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
  3749. if (!kf_ops) {
  3750. ret = -ENOMEM;
  3751. break;
  3752. }
  3753. kf_ops->atomic_write_len = cft->max_write_len;
  3754. }
  3755. cft->kf_ops = kf_ops;
  3756. cft->ss = ss;
  3757. cft->flags |= __CFTYPE_ADDED;
  3758. }
  3759. if (ret)
  3760. cgroup_exit_cftypes(cfts);
  3761. return ret;
  3762. }
  3763. static void cgroup_rm_cftypes_locked(struct cftype *cfts)
  3764. {
  3765. lockdep_assert_held(&cgroup_mutex);
  3766. list_del(&cfts->node);
  3767. cgroup_apply_cftypes(cfts, false);
  3768. cgroup_exit_cftypes(cfts);
  3769. }
  3770. /**
  3771. * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
  3772. * @cfts: zero-length name terminated array of cftypes
  3773. *
  3774. * Unregister @cfts. Files described by @cfts are removed from all
  3775. * existing cgroups and all future cgroups won't have them either. This
  3776. * function can be called anytime whether @cfts' subsys is attached or not.
  3777. *
  3778. * Returns 0 on successful unregistration, -ENOENT if @cfts is not
  3779. * registered.
  3780. */
  3781. int cgroup_rm_cftypes(struct cftype *cfts)
  3782. {
  3783. if (!cfts || cfts[0].name[0] == '\0')
  3784. return 0;
  3785. if (!(cfts[0].flags & __CFTYPE_ADDED))
  3786. return -ENOENT;
  3787. cgroup_lock();
  3788. cgroup_rm_cftypes_locked(cfts);
  3789. cgroup_unlock();
  3790. return 0;
  3791. }
  3792. /**
  3793. * cgroup_add_cftypes - add an array of cftypes to a subsystem
  3794. * @ss: target cgroup subsystem
  3795. * @cfts: zero-length name terminated array of cftypes
  3796. *
  3797. * Register @cfts to @ss. Files described by @cfts are created for all
  3798. * existing cgroups to which @ss is attached and all future cgroups will
  3799. * have them too. This function can be called anytime whether @ss is
  3800. * attached or not.
  3801. *
  3802. * Returns 0 on successful registration, -errno on failure. Note that this
  3803. * function currently returns 0 as long as @cfts registration is successful
  3804. * even if some file creation attempts on existing cgroups fail.
  3805. */
  3806. static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  3807. {
  3808. int ret;
  3809. if (!cgroup_ssid_enabled(ss->id))
  3810. return 0;
  3811. if (!cfts || cfts[0].name[0] == '\0')
  3812. return 0;
  3813. ret = cgroup_init_cftypes(ss, cfts);
  3814. if (ret)
  3815. return ret;
  3816. cgroup_lock();
  3817. list_add_tail(&cfts->node, &ss->cfts);
  3818. ret = cgroup_apply_cftypes(cfts, true);
  3819. if (ret)
  3820. cgroup_rm_cftypes_locked(cfts);
  3821. cgroup_unlock();
  3822. return ret;
  3823. }
  3824. /**
  3825. * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
  3826. * @ss: target cgroup subsystem
  3827. * @cfts: zero-length name terminated array of cftypes
  3828. *
  3829. * Similar to cgroup_add_cftypes() but the added files are only used for
  3830. * the default hierarchy.
  3831. */
  3832. int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  3833. {
  3834. struct cftype *cft;
  3835. for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
  3836. cft->flags |= __CFTYPE_ONLY_ON_DFL;
  3837. return cgroup_add_cftypes(ss, cfts);
  3838. }
  3839. /**
  3840. * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
  3841. * @ss: target cgroup subsystem
  3842. * @cfts: zero-length name terminated array of cftypes
  3843. *
  3844. * Similar to cgroup_add_cftypes() but the added files are only used for
  3845. * the legacy hierarchies.
  3846. */
  3847. int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  3848. {
  3849. struct cftype *cft;
  3850. for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
  3851. cft->flags |= __CFTYPE_NOT_ON_DFL;
  3852. return cgroup_add_cftypes(ss, cfts);
  3853. }
  3854. /**
  3855. * cgroup_file_notify - generate a file modified event for a cgroup_file
  3856. * @cfile: target cgroup_file
  3857. *
  3858. * @cfile must have been obtained by setting cftype->file_offset.
  3859. */
  3860. void cgroup_file_notify(struct cgroup_file *cfile)
  3861. {
  3862. unsigned long flags;
  3863. spin_lock_irqsave(&cgroup_file_kn_lock, flags);
  3864. if (cfile->kn) {
  3865. unsigned long last = cfile->notified_at;
  3866. unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
  3867. if (time_in_range(jiffies, last, next)) {
  3868. timer_reduce(&cfile->notify_timer, next);
  3869. } else {
  3870. kernfs_notify(cfile->kn);
  3871. cfile->notified_at = jiffies;
  3872. }
  3873. }
  3874. spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
  3875. }
  3876. /**
  3877. * cgroup_file_show - show or hide a hidden cgroup file
  3878. * @cfile: target cgroup_file obtained by setting cftype->file_offset
  3879. * @show: whether to show or hide
  3880. */
  3881. void cgroup_file_show(struct cgroup_file *cfile, bool show)
  3882. {
  3883. struct kernfs_node *kn;
  3884. spin_lock_irq(&cgroup_file_kn_lock);
  3885. kn = cfile->kn;
  3886. kernfs_get(kn);
  3887. spin_unlock_irq(&cgroup_file_kn_lock);
  3888. if (kn)
  3889. kernfs_show(kn, show);
  3890. kernfs_put(kn);
  3891. }
  3892. /**
  3893. * css_next_child - find the next child of a given css
  3894. * @pos: the current position (%NULL to initiate traversal)
  3895. * @parent: css whose children to walk
  3896. *
  3897. * This function returns the next child of @parent and should be called
  3898. * under either cgroup_mutex or RCU read lock. The only requirement is
  3899. * that @parent and @pos are accessible. The next sibling is guaranteed to
  3900. * be returned regardless of their states.
  3901. *
  3902. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  3903. * css which finished ->css_online() is guaranteed to be visible in the
  3904. * future iterations and will stay visible until the last reference is put.
  3905. * A css which hasn't finished ->css_online() or already finished
  3906. * ->css_offline() may show up during traversal. It's each subsystem's
  3907. * responsibility to synchronize against on/offlining.
  3908. */
  3909. struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
  3910. struct cgroup_subsys_state *parent)
  3911. {
  3912. struct cgroup_subsys_state *next;
  3913. cgroup_assert_mutex_or_rcu_locked();
  3914. /*
  3915. * @pos could already have been unlinked from the sibling list.
  3916. * Once a cgroup is removed, its ->sibling.next is no longer
  3917. * updated when its next sibling changes. CSS_RELEASED is set when
  3918. * @pos is taken off list, at which time its next pointer is valid,
  3919. * and, as releases are serialized, the one pointed to by the next
  3920. * pointer is guaranteed to not have started release yet. This
  3921. * implies that if we observe !CSS_RELEASED on @pos in this RCU
  3922. * critical section, the one pointed to by its next pointer is
  3923. * guaranteed to not have finished its RCU grace period even if we
  3924. * have dropped rcu_read_lock() in-between iterations.
  3925. *
  3926. * If @pos has CSS_RELEASED set, its next pointer can't be
  3927. * dereferenced; however, as each css is given a monotonically
  3928. * increasing unique serial number and always appended to the
  3929. * sibling list, the next one can be found by walking the parent's
  3930. * children until the first css with higher serial number than
  3931. * @pos's. While this path can be slower, it happens iff iteration
  3932. * races against release and the race window is very small.
  3933. */
  3934. if (!pos) {
  3935. next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
  3936. } else if (likely(!(pos->flags & CSS_RELEASED))) {
  3937. next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
  3938. } else {
  3939. list_for_each_entry_rcu(next, &parent->children, sibling,
  3940. lockdep_is_held(&cgroup_mutex))
  3941. if (next->serial_nr > pos->serial_nr)
  3942. break;
  3943. }
  3944. /*
  3945. * @next, if not pointing to the head, can be dereferenced and is
  3946. * the next sibling.
  3947. */
  3948. if (&next->sibling != &parent->children)
  3949. return next;
  3950. return NULL;
  3951. }
  3952. /**
  3953. * css_next_descendant_pre - find the next descendant for pre-order walk
  3954. * @pos: the current position (%NULL to initiate traversal)
  3955. * @root: css whose descendants to walk
  3956. *
  3957. * To be used by css_for_each_descendant_pre(). Find the next descendant
  3958. * to visit for pre-order traversal of @root's descendants. @root is
  3959. * included in the iteration and the first node to be visited.
  3960. *
  3961. * While this function requires cgroup_mutex or RCU read locking, it
  3962. * doesn't require the whole traversal to be contained in a single critical
  3963. * section. Additionally, it isn't necessary to hold onto a reference to @pos.
  3964. * This function will return the correct next descendant as long as both @pos
  3965. * and @root are accessible and @pos is a descendant of @root.
  3966. *
  3967. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  3968. * css which finished ->css_online() is guaranteed to be visible in the
  3969. * future iterations and will stay visible until the last reference is put.
  3970. * A css which hasn't finished ->css_online() or already finished
  3971. * ->css_offline() may show up during traversal. It's each subsystem's
  3972. * responsibility to synchronize against on/offlining.
  3973. */
  3974. struct cgroup_subsys_state *
  3975. css_next_descendant_pre(struct cgroup_subsys_state *pos,
  3976. struct cgroup_subsys_state *root)
  3977. {
  3978. struct cgroup_subsys_state *next;
  3979. cgroup_assert_mutex_or_rcu_locked();
  3980. /* if first iteration, visit @root */
  3981. if (!pos)
  3982. return root;
  3983. /* visit the first child if exists */
  3984. next = css_next_child(NULL, pos);
  3985. if (next)
  3986. return next;
  3987. /* no child, visit my or the closest ancestor's next sibling */
  3988. while (pos != root) {
  3989. next = css_next_child(pos, pos->parent);
  3990. if (next)
  3991. return next;
  3992. pos = pos->parent;
  3993. }
  3994. return NULL;
  3995. }
  3996. EXPORT_SYMBOL_GPL(css_next_descendant_pre);
  3997. /**
  3998. * css_rightmost_descendant - return the rightmost descendant of a css
  3999. * @pos: css of interest
  4000. *
  4001. * Return the rightmost descendant of @pos. If there's no descendant, @pos
  4002. * is returned. This can be used during pre-order traversal to skip
  4003. * subtree of @pos.
  4004. *
  4005. * While this function requires cgroup_mutex or RCU read locking, it
  4006. * doesn't require the whole traversal to be contained in a single critical
  4007. * section. Additionally, it isn't necessary to hold onto a reference to @pos.
  4008. * This function will return the correct rightmost descendant as long as @pos
  4009. * is accessible.
  4010. */
  4011. struct cgroup_subsys_state *
  4012. css_rightmost_descendant(struct cgroup_subsys_state *pos)
  4013. {
  4014. struct cgroup_subsys_state *last, *tmp;
  4015. cgroup_assert_mutex_or_rcu_locked();
  4016. do {
  4017. last = pos;
  4018. /* ->prev isn't RCU safe, walk ->next till the end */
  4019. pos = NULL;
  4020. css_for_each_child(tmp, last)
  4021. pos = tmp;
  4022. } while (pos);
  4023. return last;
  4024. }
  4025. static struct cgroup_subsys_state *
  4026. css_leftmost_descendant(struct cgroup_subsys_state *pos)
  4027. {
  4028. struct cgroup_subsys_state *last;
  4029. do {
  4030. last = pos;
  4031. pos = css_next_child(NULL, pos);
  4032. } while (pos);
  4033. return last;
  4034. }
  4035. /**
  4036. * css_next_descendant_post - find the next descendant for post-order walk
  4037. * @pos: the current position (%NULL to initiate traversal)
  4038. * @root: css whose descendants to walk
  4039. *
  4040. * To be used by css_for_each_descendant_post(). Find the next descendant
  4041. * to visit for post-order traversal of @root's descendants. @root is
  4042. * included in the iteration and the last node to be visited.
  4043. *
  4044. * While this function requires cgroup_mutex or RCU read locking, it
  4045. * doesn't require the whole traversal to be contained in a single critical
  4046. * section. Additionally, it isn't necessary to hold onto a reference to @pos.
  4047. * This function will return the correct next descendant as long as both @pos
  4048. * and @cgroup are accessible and @pos is a descendant of @cgroup.
  4049. *
  4050. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  4051. * css which finished ->css_online() is guaranteed to be visible in the
  4052. * future iterations and will stay visible until the last reference is put.
  4053. * A css which hasn't finished ->css_online() or already finished
  4054. * ->css_offline() may show up during traversal. It's each subsystem's
  4055. * responsibility to synchronize against on/offlining.
  4056. */
  4057. struct cgroup_subsys_state *
  4058. css_next_descendant_post(struct cgroup_subsys_state *pos,
  4059. struct cgroup_subsys_state *root)
  4060. {
  4061. struct cgroup_subsys_state *next;
  4062. cgroup_assert_mutex_or_rcu_locked();
  4063. /* if first iteration, visit leftmost descendant which may be @root */
  4064. if (!pos)
  4065. return css_leftmost_descendant(root);
  4066. /* if we visited @root, we're done */
  4067. if (pos == root)
  4068. return NULL;
  4069. /* if there's an unvisited sibling, visit its leftmost descendant */
  4070. next = css_next_child(pos, pos->parent);
  4071. if (next)
  4072. return css_leftmost_descendant(next);
  4073. /* no sibling left, visit parent */
  4074. return pos->parent;
  4075. }
  4076. /**
  4077. * css_has_online_children - does a css have online children
  4078. * @css: the target css
  4079. *
  4080. * Returns %true if @css has any online children; otherwise, %false. This
  4081. * function can be called from any context but the caller is responsible
  4082. * for synchronizing against on/offlining as necessary.
  4083. */
  4084. bool css_has_online_children(struct cgroup_subsys_state *css)
  4085. {
  4086. struct cgroup_subsys_state *child;
  4087. bool ret = false;
  4088. rcu_read_lock();
  4089. css_for_each_child(child, css) {
  4090. if (child->flags & CSS_ONLINE) {
  4091. ret = true;
  4092. break;
  4093. }
  4094. }
  4095. rcu_read_unlock();
  4096. return ret;
  4097. }
  4098. static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it)
  4099. {
  4100. struct list_head *l;
  4101. struct cgrp_cset_link *link;
  4102. struct css_set *cset;
  4103. lockdep_assert_held(&css_set_lock);
  4104. /* find the next threaded cset */
  4105. if (it->tcset_pos) {
  4106. l = it->tcset_pos->next;
  4107. if (l != it->tcset_head) {
  4108. it->tcset_pos = l;
  4109. return container_of(l, struct css_set,
  4110. threaded_csets_node);
  4111. }
  4112. it->tcset_pos = NULL;
  4113. }
  4114. /* find the next cset */
  4115. l = it->cset_pos;
  4116. l = l->next;
  4117. if (l == it->cset_head) {
  4118. it->cset_pos = NULL;
  4119. return NULL;
  4120. }
  4121. if (it->ss) {
  4122. cset = container_of(l, struct css_set, e_cset_node[it->ss->id]);
  4123. } else {
  4124. link = list_entry(l, struct cgrp_cset_link, cset_link);
  4125. cset = link->cset;
  4126. }
  4127. it->cset_pos = l;
  4128. /* initialize threaded css_set walking */
  4129. if (it->flags & CSS_TASK_ITER_THREADED) {
  4130. if (it->cur_dcset)
  4131. put_css_set_locked(it->cur_dcset);
  4132. it->cur_dcset = cset;
  4133. get_css_set(cset);
  4134. it->tcset_head = &cset->threaded_csets;
  4135. it->tcset_pos = &cset->threaded_csets;
  4136. }
  4137. return cset;
  4138. }
  4139. /**
  4140. * css_task_iter_advance_css_set - advance a task iterator to the next css_set
  4141. * @it: the iterator to advance
  4142. *
  4143. * Advance @it to the next css_set to walk.
  4144. */
  4145. static void css_task_iter_advance_css_set(struct css_task_iter *it)
  4146. {
  4147. struct css_set *cset;
  4148. lockdep_assert_held(&css_set_lock);
  4149. /* Advance to the next non-empty css_set and find first non-empty tasks list*/
  4150. while ((cset = css_task_iter_next_css_set(it))) {
  4151. if (!list_empty(&cset->tasks)) {
  4152. it->cur_tasks_head = &cset->tasks;
  4153. break;
  4154. } else if (!list_empty(&cset->mg_tasks)) {
  4155. it->cur_tasks_head = &cset->mg_tasks;
  4156. break;
  4157. } else if (!list_empty(&cset->dying_tasks)) {
  4158. it->cur_tasks_head = &cset->dying_tasks;
  4159. break;
  4160. }
  4161. }
  4162. if (!cset) {
  4163. it->task_pos = NULL;
  4164. return;
  4165. }
  4166. it->task_pos = it->cur_tasks_head->next;
  4167. /*
  4168. * We don't keep css_sets locked across iteration steps and thus
  4169. * need to take steps to ensure that iteration can be resumed after
  4170. * the lock is re-acquired. Iteration is performed at two levels -
  4171. * css_sets and tasks in them.
  4172. *
  4173. * Once created, a css_set never leaves its cgroup lists, so a
  4174. * pinned css_set is guaranteed to stay put and we can resume
  4175. * iteration afterwards.
  4176. *
  4177. * Tasks may leave @cset across iteration steps. This is resolved
  4178. * by registering each iterator with the css_set currently being
  4179. * walked and making css_set_move_task() advance iterators whose
  4180. * next task is leaving.
  4181. */
  4182. if (it->cur_cset) {
  4183. list_del(&it->iters_node);
  4184. put_css_set_locked(it->cur_cset);
  4185. }
  4186. get_css_set(cset);
  4187. it->cur_cset = cset;
  4188. list_add(&it->iters_node, &cset->task_iters);
  4189. }
  4190. static void css_task_iter_skip(struct css_task_iter *it,
  4191. struct task_struct *task)
  4192. {
  4193. lockdep_assert_held(&css_set_lock);
  4194. if (it->task_pos == &task->cg_list) {
  4195. it->task_pos = it->task_pos->next;
  4196. it->flags |= CSS_TASK_ITER_SKIPPED;
  4197. }
  4198. }
  4199. static void css_task_iter_advance(struct css_task_iter *it)
  4200. {
  4201. struct task_struct *task;
  4202. lockdep_assert_held(&css_set_lock);
  4203. repeat:
  4204. if (it->task_pos) {
  4205. /*
  4206. * Advance iterator to find next entry. We go through cset
  4207. * tasks, mg_tasks and dying_tasks, when consumed we move onto
  4208. * the next cset.
  4209. */
  4210. if (it->flags & CSS_TASK_ITER_SKIPPED)
  4211. it->flags &= ~CSS_TASK_ITER_SKIPPED;
  4212. else
  4213. it->task_pos = it->task_pos->next;
  4214. if (it->task_pos == &it->cur_cset->tasks) {
  4215. it->cur_tasks_head = &it->cur_cset->mg_tasks;
  4216. it->task_pos = it->cur_tasks_head->next;
  4217. }
  4218. if (it->task_pos == &it->cur_cset->mg_tasks) {
  4219. it->cur_tasks_head = &it->cur_cset->dying_tasks;
  4220. it->task_pos = it->cur_tasks_head->next;
  4221. }
  4222. if (it->task_pos == &it->cur_cset->dying_tasks)
  4223. css_task_iter_advance_css_set(it);
  4224. } else {
  4225. /* called from start, proceed to the first cset */
  4226. css_task_iter_advance_css_set(it);
  4227. }
  4228. if (!it->task_pos)
  4229. return;
  4230. task = list_entry(it->task_pos, struct task_struct, cg_list);
  4231. if (it->flags & CSS_TASK_ITER_PROCS) {
  4232. /* if PROCS, skip over tasks which aren't group leaders */
  4233. if (!thread_group_leader(task))
  4234. goto repeat;
  4235. /* and dying leaders w/o live member threads */
  4236. if (it->cur_tasks_head == &it->cur_cset->dying_tasks &&
  4237. !atomic_read(&task->signal->live))
  4238. goto repeat;
  4239. } else {
  4240. /* skip all dying ones */
  4241. if (it->cur_tasks_head == &it->cur_cset->dying_tasks)
  4242. goto repeat;
  4243. }
  4244. }
  4245. /**
  4246. * css_task_iter_start - initiate task iteration
  4247. * @css: the css to walk tasks of
  4248. * @flags: CSS_TASK_ITER_* flags
  4249. * @it: the task iterator to use
  4250. *
  4251. * Initiate iteration through the tasks of @css. The caller can call
  4252. * css_task_iter_next() to walk through the tasks until the function
  4253. * returns NULL. On completion of iteration, css_task_iter_end() must be
  4254. * called.
  4255. */
  4256. void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
  4257. struct css_task_iter *it)
  4258. {
  4259. unsigned long irqflags;
  4260. memset(it, 0, sizeof(*it));
  4261. spin_lock_irqsave(&css_set_lock, irqflags);
  4262. it->ss = css->ss;
  4263. it->flags = flags;
  4264. if (CGROUP_HAS_SUBSYS_CONFIG && it->ss)
  4265. it->cset_pos = &css->cgroup->e_csets[css->ss->id];
  4266. else
  4267. it->cset_pos = &css->cgroup->cset_links;
  4268. it->cset_head = it->cset_pos;
  4269. css_task_iter_advance(it);
  4270. spin_unlock_irqrestore(&css_set_lock, irqflags);
  4271. }
  4272. /**
  4273. * css_task_iter_next - return the next task for the iterator
  4274. * @it: the task iterator being iterated
  4275. *
  4276. * The "next" function for task iteration. @it should have been
  4277. * initialized via css_task_iter_start(). Returns NULL when the iteration
  4278. * reaches the end.
  4279. */
  4280. struct task_struct *css_task_iter_next(struct css_task_iter *it)
  4281. {
  4282. unsigned long irqflags;
  4283. if (it->cur_task) {
  4284. put_task_struct(it->cur_task);
  4285. it->cur_task = NULL;
  4286. }
  4287. spin_lock_irqsave(&css_set_lock, irqflags);
  4288. /* @it may be half-advanced by skips, finish advancing */
  4289. if (it->flags & CSS_TASK_ITER_SKIPPED)
  4290. css_task_iter_advance(it);
  4291. if (it->task_pos) {
  4292. it->cur_task = list_entry(it->task_pos, struct task_struct,
  4293. cg_list);
  4294. get_task_struct(it->cur_task);
  4295. css_task_iter_advance(it);
  4296. }
  4297. spin_unlock_irqrestore(&css_set_lock, irqflags);
  4298. return it->cur_task;
  4299. }
  4300. /**
  4301. * css_task_iter_end - finish task iteration
  4302. * @it: the task iterator to finish
  4303. *
  4304. * Finish task iteration started by css_task_iter_start().
  4305. */
  4306. void css_task_iter_end(struct css_task_iter *it)
  4307. {
  4308. unsigned long irqflags;
  4309. if (it->cur_cset) {
  4310. spin_lock_irqsave(&css_set_lock, irqflags);
  4311. list_del(&it->iters_node);
  4312. put_css_set_locked(it->cur_cset);
  4313. spin_unlock_irqrestore(&css_set_lock, irqflags);
  4314. }
  4315. if (it->cur_dcset)
  4316. put_css_set(it->cur_dcset);
  4317. if (it->cur_task)
  4318. put_task_struct(it->cur_task);
  4319. }
  4320. static void cgroup_procs_release(struct kernfs_open_file *of)
  4321. {
  4322. struct cgroup_file_ctx *ctx = of->priv;
  4323. if (ctx->procs.started)
  4324. css_task_iter_end(&ctx->procs.iter);
  4325. }
  4326. static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos)
  4327. {
  4328. struct kernfs_open_file *of = s->private;
  4329. struct cgroup_file_ctx *ctx = of->priv;
  4330. if (pos)
  4331. (*pos)++;
  4332. return css_task_iter_next(&ctx->procs.iter);
  4333. }
  4334. static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos,
  4335. unsigned int iter_flags)
  4336. {
  4337. struct kernfs_open_file *of = s->private;
  4338. struct cgroup *cgrp = seq_css(s)->cgroup;
  4339. struct cgroup_file_ctx *ctx = of->priv;
  4340. struct css_task_iter *it = &ctx->procs.iter;
  4341. /*
  4342. * When a seq_file is seeked, it's always traversed sequentially
  4343. * from position 0, so we can simply keep iterating on !0 *pos.
  4344. */
  4345. if (!ctx->procs.started) {
  4346. if (WARN_ON_ONCE((*pos)))
  4347. return ERR_PTR(-EINVAL);
  4348. css_task_iter_start(&cgrp->self, iter_flags, it);
  4349. ctx->procs.started = true;
  4350. } else if (!(*pos)) {
  4351. css_task_iter_end(it);
  4352. css_task_iter_start(&cgrp->self, iter_flags, it);
  4353. } else
  4354. return it->cur_task;
  4355. return cgroup_procs_next(s, NULL, NULL);
  4356. }
  4357. static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
  4358. {
  4359. struct cgroup *cgrp = seq_css(s)->cgroup;
  4360. /*
  4361. * All processes of a threaded subtree belong to the domain cgroup
  4362. * of the subtree. Only threads can be distributed across the
  4363. * subtree. Reject reads on cgroup.procs in the subtree proper.
  4364. * They're always empty anyway.
  4365. */
  4366. if (cgroup_is_threaded(cgrp))
  4367. return ERR_PTR(-EOPNOTSUPP);
  4368. return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS |
  4369. CSS_TASK_ITER_THREADED);
  4370. }
  4371. static int cgroup_procs_show(struct seq_file *s, void *v)
  4372. {
  4373. seq_printf(s, "%d\n", task_pid_vnr(v));
  4374. return 0;
  4375. }
  4376. static int cgroup_may_write(const struct cgroup *cgrp, struct super_block *sb)
  4377. {
  4378. int ret;
  4379. struct inode *inode;
  4380. lockdep_assert_held(&cgroup_mutex);
  4381. inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
  4382. if (!inode)
  4383. return -ENOMEM;
  4384. ret = inode_permission(&nop_mnt_idmap, inode, MAY_WRITE);
  4385. iput(inode);
  4386. return ret;
  4387. }
  4388. static int cgroup_procs_write_permission(struct cgroup *src_cgrp,
  4389. struct cgroup *dst_cgrp,
  4390. struct super_block *sb,
  4391. struct cgroup_namespace *ns)
  4392. {
  4393. struct cgroup *com_cgrp = src_cgrp;
  4394. int ret;
  4395. lockdep_assert_held(&cgroup_mutex);
  4396. /* find the common ancestor */
  4397. while (!cgroup_is_descendant(dst_cgrp, com_cgrp))
  4398. com_cgrp = cgroup_parent(com_cgrp);
  4399. /* %current should be authorized to migrate to the common ancestor */
  4400. ret = cgroup_may_write(com_cgrp, sb);
  4401. if (ret)
  4402. return ret;
  4403. /*
  4404. * If namespaces are delegation boundaries, %current must be able
  4405. * to see both source and destination cgroups from its namespace.
  4406. */
  4407. if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) &&
  4408. (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) ||
  4409. !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp)))
  4410. return -ENOENT;
  4411. return 0;
  4412. }
  4413. static int cgroup_attach_permissions(struct cgroup *src_cgrp,
  4414. struct cgroup *dst_cgrp,
  4415. struct super_block *sb, bool threadgroup,
  4416. struct cgroup_namespace *ns)
  4417. {
  4418. int ret = 0;
  4419. ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, sb, ns);
  4420. if (ret)
  4421. return ret;
  4422. ret = cgroup_migrate_vet_dst(dst_cgrp);
  4423. if (ret)
  4424. return ret;
  4425. if (!threadgroup && (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp))
  4426. ret = -EOPNOTSUPP;
  4427. return ret;
  4428. }
  4429. static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
  4430. bool threadgroup)
  4431. {
  4432. struct cgroup_file_ctx *ctx = of->priv;
  4433. struct cgroup *src_cgrp, *dst_cgrp;
  4434. struct task_struct *task;
  4435. const struct cred *saved_cred;
  4436. ssize_t ret;
  4437. bool threadgroup_locked;
  4438. dst_cgrp = cgroup_kn_lock_live(of->kn, false);
  4439. if (!dst_cgrp)
  4440. return -ENODEV;
  4441. task = cgroup_procs_write_start(buf, threadgroup, &threadgroup_locked);
  4442. ret = PTR_ERR_OR_ZERO(task);
  4443. if (ret)
  4444. goto out_unlock;
  4445. /* find the source cgroup */
  4446. spin_lock_irq(&css_set_lock);
  4447. src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
  4448. spin_unlock_irq(&css_set_lock);
  4449. /*
  4450. * Process and thread migrations follow same delegation rule. Check
  4451. * permissions using the credentials from file open to protect against
  4452. * inherited fd attacks.
  4453. */
  4454. saved_cred = override_creds(of->file->f_cred);
  4455. ret = cgroup_attach_permissions(src_cgrp, dst_cgrp,
  4456. of->file->f_path.dentry->d_sb,
  4457. threadgroup, ctx->ns);
  4458. revert_creds(saved_cred);
  4459. if (ret)
  4460. goto out_finish;
  4461. ret = cgroup_attach_task(dst_cgrp, task, threadgroup);
  4462. out_finish:
  4463. cgroup_procs_write_finish(task, threadgroup_locked);
  4464. out_unlock:
  4465. cgroup_kn_unlock(of->kn);
  4466. return ret;
  4467. }
  4468. static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
  4469. char *buf, size_t nbytes, loff_t off)
  4470. {
  4471. return __cgroup_procs_write(of, buf, true) ?: nbytes;
  4472. }
  4473. static void *cgroup_threads_start(struct seq_file *s, loff_t *pos)
  4474. {
  4475. return __cgroup_procs_start(s, pos, 0);
  4476. }
  4477. static ssize_t cgroup_threads_write(struct kernfs_open_file *of,
  4478. char *buf, size_t nbytes, loff_t off)
  4479. {
  4480. return __cgroup_procs_write(of, buf, false) ?: nbytes;
  4481. }
  4482. /* cgroup core interface files for the default hierarchy */
  4483. static struct cftype cgroup_base_files[] = {
  4484. {
  4485. .name = "cgroup.type",
  4486. .flags = CFTYPE_NOT_ON_ROOT,
  4487. .seq_show = cgroup_type_show,
  4488. .write = cgroup_type_write,
  4489. },
  4490. {
  4491. .name = "cgroup.procs",
  4492. .flags = CFTYPE_NS_DELEGATABLE,
  4493. .file_offset = offsetof(struct cgroup, procs_file),
  4494. .release = cgroup_procs_release,
  4495. .seq_start = cgroup_procs_start,
  4496. .seq_next = cgroup_procs_next,
  4497. .seq_show = cgroup_procs_show,
  4498. .write = cgroup_procs_write,
  4499. },
  4500. {
  4501. .name = "cgroup.threads",
  4502. .flags = CFTYPE_NS_DELEGATABLE,
  4503. .release = cgroup_procs_release,
  4504. .seq_start = cgroup_threads_start,
  4505. .seq_next = cgroup_procs_next,
  4506. .seq_show = cgroup_procs_show,
  4507. .write = cgroup_threads_write,
  4508. },
  4509. {
  4510. .name = "cgroup.controllers",
  4511. .seq_show = cgroup_controllers_show,
  4512. },
  4513. {
  4514. .name = "cgroup.subtree_control",
  4515. .flags = CFTYPE_NS_DELEGATABLE,
  4516. .seq_show = cgroup_subtree_control_show,
  4517. .write = cgroup_subtree_control_write,
  4518. },
  4519. {
  4520. .name = "cgroup.events",
  4521. .flags = CFTYPE_NOT_ON_ROOT,
  4522. .file_offset = offsetof(struct cgroup, events_file),
  4523. .seq_show = cgroup_events_show,
  4524. },
  4525. {
  4526. .name = "cgroup.max.descendants",
  4527. .seq_show = cgroup_max_descendants_show,
  4528. .write = cgroup_max_descendants_write,
  4529. },
  4530. {
  4531. .name = "cgroup.max.depth",
  4532. .seq_show = cgroup_max_depth_show,
  4533. .write = cgroup_max_depth_write,
  4534. },
  4535. {
  4536. .name = "cgroup.stat",
  4537. .seq_show = cgroup_stat_show,
  4538. },
  4539. {
  4540. .name = "cgroup.freeze",
  4541. .flags = CFTYPE_NOT_ON_ROOT,
  4542. .seq_show = cgroup_freeze_show,
  4543. .write = cgroup_freeze_write,
  4544. },
  4545. {
  4546. .name = "cgroup.kill",
  4547. .flags = CFTYPE_NOT_ON_ROOT,
  4548. .write = cgroup_kill_write,
  4549. },
  4550. {
  4551. .name = "cpu.stat",
  4552. .seq_show = cpu_stat_show,
  4553. },
  4554. {
  4555. .name = "cpu.stat.local",
  4556. .seq_show = cpu_local_stat_show,
  4557. },
  4558. { } /* terminate */
  4559. };
  4560. static struct cftype cgroup_psi_files[] = {
  4561. #ifdef CONFIG_PSI
  4562. {
  4563. .name = "io.pressure",
  4564. .file_offset = offsetof(struct cgroup, psi_files[PSI_IO]),
  4565. .seq_show = cgroup_io_pressure_show,
  4566. .write = cgroup_io_pressure_write,
  4567. .poll = cgroup_pressure_poll,
  4568. .release = cgroup_pressure_release,
  4569. },
  4570. {
  4571. .name = "memory.pressure",
  4572. .file_offset = offsetof(struct cgroup, psi_files[PSI_MEM]),
  4573. .seq_show = cgroup_memory_pressure_show,
  4574. .write = cgroup_memory_pressure_write,
  4575. .poll = cgroup_pressure_poll,
  4576. .release = cgroup_pressure_release,
  4577. },
  4578. {
  4579. .name = "cpu.pressure",
  4580. .file_offset = offsetof(struct cgroup, psi_files[PSI_CPU]),
  4581. .seq_show = cgroup_cpu_pressure_show,
  4582. .write = cgroup_cpu_pressure_write,
  4583. .poll = cgroup_pressure_poll,
  4584. .release = cgroup_pressure_release,
  4585. },
  4586. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  4587. {
  4588. .name = "irq.pressure",
  4589. .file_offset = offsetof(struct cgroup, psi_files[PSI_IRQ]),
  4590. .seq_show = cgroup_irq_pressure_show,
  4591. .write = cgroup_irq_pressure_write,
  4592. .poll = cgroup_pressure_poll,
  4593. .release = cgroup_pressure_release,
  4594. },
  4595. #endif
  4596. {
  4597. .name = "cgroup.pressure",
  4598. .seq_show = cgroup_pressure_show,
  4599. .write = cgroup_pressure_write,
  4600. },
  4601. #endif /* CONFIG_PSI */
  4602. { } /* terminate */
  4603. };
  4604. /*
  4605. * css destruction is four-stage process.
  4606. *
  4607. * 1. Destruction starts. Killing of the percpu_ref is initiated.
  4608. * Implemented in kill_css().
  4609. *
  4610. * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
  4611. * and thus css_tryget_online() is guaranteed to fail, the css can be
  4612. * offlined by invoking offline_css(). After offlining, the base ref is
  4613. * put. Implemented in css_killed_work_fn().
  4614. *
  4615. * 3. When the percpu_ref reaches zero, the only possible remaining
  4616. * accessors are inside RCU read sections. css_release() schedules the
  4617. * RCU callback.
  4618. *
  4619. * 4. After the grace period, the css can be freed. Implemented in
  4620. * css_free_rwork_fn().
  4621. *
  4622. * It is actually hairier because both step 2 and 4 require process context
  4623. * and thus involve punting to css->destroy_work adding two additional
  4624. * steps to the already complex sequence.
  4625. */
  4626. static void css_free_rwork_fn(struct work_struct *work)
  4627. {
  4628. struct cgroup_subsys_state *css = container_of(to_rcu_work(work),
  4629. struct cgroup_subsys_state, destroy_rwork);
  4630. struct cgroup_subsys *ss = css->ss;
  4631. struct cgroup *cgrp = css->cgroup;
  4632. percpu_ref_exit(&css->refcnt);
  4633. if (ss) {
  4634. /* css free path */
  4635. struct cgroup_subsys_state *parent = css->parent;
  4636. int id = css->id;
  4637. ss->css_free(css);
  4638. cgroup_idr_remove(&ss->css_idr, id);
  4639. cgroup_put(cgrp);
  4640. if (parent)
  4641. css_put(parent);
  4642. } else {
  4643. /* cgroup free path */
  4644. atomic_dec(&cgrp->root->nr_cgrps);
  4645. if (!cgroup_on_dfl(cgrp))
  4646. cgroup1_pidlist_destroy_all(cgrp);
  4647. cancel_work_sync(&cgrp->release_agent_work);
  4648. bpf_cgrp_storage_free(cgrp);
  4649. if (cgroup_parent(cgrp)) {
  4650. /*
  4651. * We get a ref to the parent, and put the ref when
  4652. * this cgroup is being freed, so it's guaranteed
  4653. * that the parent won't be destroyed before its
  4654. * children.
  4655. */
  4656. cgroup_put(cgroup_parent(cgrp));
  4657. kernfs_put(cgrp->kn);
  4658. psi_cgroup_free(cgrp);
  4659. cgroup_rstat_exit(cgrp);
  4660. kfree(cgrp);
  4661. } else {
  4662. /*
  4663. * This is root cgroup's refcnt reaching zero,
  4664. * which indicates that the root should be
  4665. * released.
  4666. */
  4667. cgroup_destroy_root(cgrp->root);
  4668. }
  4669. }
  4670. }
  4671. static void css_release_work_fn(struct work_struct *work)
  4672. {
  4673. struct cgroup_subsys_state *css =
  4674. container_of(work, struct cgroup_subsys_state, destroy_work);
  4675. struct cgroup_subsys *ss = css->ss;
  4676. struct cgroup *cgrp = css->cgroup;
  4677. cgroup_lock();
  4678. css->flags |= CSS_RELEASED;
  4679. list_del_rcu(&css->sibling);
  4680. if (ss) {
  4681. struct cgroup *parent_cgrp;
  4682. /* css release path */
  4683. if (!list_empty(&css->rstat_css_node)) {
  4684. cgroup_rstat_flush(cgrp);
  4685. list_del_rcu(&css->rstat_css_node);
  4686. }
  4687. cgroup_idr_replace(&ss->css_idr, NULL, css->id);
  4688. if (ss->css_released)
  4689. ss->css_released(css);
  4690. cgrp->nr_dying_subsys[ss->id]--;
  4691. /*
  4692. * When a css is released and ready to be freed, its
  4693. * nr_descendants must be zero. However, the corresponding
  4694. * cgrp->nr_dying_subsys[ss->id] may not be 0 if a subsystem
  4695. * is activated and deactivated multiple times with one or
  4696. * more of its previous activation leaving behind dying csses.
  4697. */
  4698. WARN_ON_ONCE(css->nr_descendants);
  4699. parent_cgrp = cgroup_parent(cgrp);
  4700. while (parent_cgrp) {
  4701. parent_cgrp->nr_dying_subsys[ss->id]--;
  4702. parent_cgrp = cgroup_parent(parent_cgrp);
  4703. }
  4704. } else {
  4705. struct cgroup *tcgrp;
  4706. /* cgroup release path */
  4707. TRACE_CGROUP_PATH(release, cgrp);
  4708. cgroup_rstat_flush(cgrp);
  4709. spin_lock_irq(&css_set_lock);
  4710. for (tcgrp = cgroup_parent(cgrp); tcgrp;
  4711. tcgrp = cgroup_parent(tcgrp))
  4712. tcgrp->nr_dying_descendants--;
  4713. spin_unlock_irq(&css_set_lock);
  4714. /*
  4715. * There are two control paths which try to determine
  4716. * cgroup from dentry without going through kernfs -
  4717. * cgroupstats_build() and css_tryget_online_from_dir().
  4718. * Those are supported by RCU protecting clearing of
  4719. * cgrp->kn->priv backpointer.
  4720. */
  4721. if (cgrp->kn)
  4722. RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
  4723. NULL);
  4724. }
  4725. cgroup_unlock();
  4726. INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
  4727. queue_rcu_work(cgroup_free_wq, &css->destroy_rwork);
  4728. }
  4729. static void css_release(struct percpu_ref *ref)
  4730. {
  4731. struct cgroup_subsys_state *css =
  4732. container_of(ref, struct cgroup_subsys_state, refcnt);
  4733. INIT_WORK(&css->destroy_work, css_release_work_fn);
  4734. queue_work(cgroup_release_wq, &css->destroy_work);
  4735. }
  4736. static void init_and_link_css(struct cgroup_subsys_state *css,
  4737. struct cgroup_subsys *ss, struct cgroup *cgrp)
  4738. {
  4739. lockdep_assert_held(&cgroup_mutex);
  4740. cgroup_get_live(cgrp);
  4741. memset(css, 0, sizeof(*css));
  4742. css->cgroup = cgrp;
  4743. css->ss = ss;
  4744. css->id = -1;
  4745. INIT_LIST_HEAD(&css->sibling);
  4746. INIT_LIST_HEAD(&css->children);
  4747. INIT_LIST_HEAD(&css->rstat_css_node);
  4748. css->serial_nr = css_serial_nr_next++;
  4749. atomic_set(&css->online_cnt, 0);
  4750. if (cgroup_parent(cgrp)) {
  4751. css->parent = cgroup_css(cgroup_parent(cgrp), ss);
  4752. css_get(css->parent);
  4753. }
  4754. if (ss->css_rstat_flush)
  4755. list_add_rcu(&css->rstat_css_node, &cgrp->rstat_css_list);
  4756. BUG_ON(cgroup_css(cgrp, ss));
  4757. }
  4758. /* invoke ->css_online() on a new CSS and mark it online if successful */
  4759. static int online_css(struct cgroup_subsys_state *css)
  4760. {
  4761. struct cgroup_subsys *ss = css->ss;
  4762. int ret = 0;
  4763. lockdep_assert_held(&cgroup_mutex);
  4764. if (ss->css_online)
  4765. ret = ss->css_online(css);
  4766. if (!ret) {
  4767. css->flags |= CSS_ONLINE;
  4768. rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
  4769. atomic_inc(&css->online_cnt);
  4770. if (css->parent) {
  4771. atomic_inc(&css->parent->online_cnt);
  4772. while ((css = css->parent))
  4773. css->nr_descendants++;
  4774. }
  4775. }
  4776. return ret;
  4777. }
  4778. /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
  4779. static void offline_css(struct cgroup_subsys_state *css)
  4780. {
  4781. struct cgroup_subsys *ss = css->ss;
  4782. lockdep_assert_held(&cgroup_mutex);
  4783. if (!(css->flags & CSS_ONLINE))
  4784. return;
  4785. if (ss->css_offline)
  4786. ss->css_offline(css);
  4787. css->flags &= ~CSS_ONLINE;
  4788. RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
  4789. wake_up_all(&css->cgroup->offline_waitq);
  4790. css->cgroup->nr_dying_subsys[ss->id]++;
  4791. /*
  4792. * Parent css and cgroup cannot be freed until after the freeing
  4793. * of child css, see css_free_rwork_fn().
  4794. */
  4795. while ((css = css->parent)) {
  4796. css->nr_descendants--;
  4797. css->cgroup->nr_dying_subsys[ss->id]++;
  4798. }
  4799. }
  4800. /**
  4801. * css_create - create a cgroup_subsys_state
  4802. * @cgrp: the cgroup new css will be associated with
  4803. * @ss: the subsys of new css
  4804. *
  4805. * Create a new css associated with @cgrp - @ss pair. On success, the new
  4806. * css is online and installed in @cgrp. This function doesn't create the
  4807. * interface files. Returns 0 on success, -errno on failure.
  4808. */
  4809. static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
  4810. struct cgroup_subsys *ss)
  4811. {
  4812. struct cgroup *parent = cgroup_parent(cgrp);
  4813. struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
  4814. struct cgroup_subsys_state *css;
  4815. int err;
  4816. lockdep_assert_held(&cgroup_mutex);
  4817. css = ss->css_alloc(parent_css);
  4818. if (!css)
  4819. css = ERR_PTR(-ENOMEM);
  4820. if (IS_ERR(css))
  4821. return css;
  4822. init_and_link_css(css, ss, cgrp);
  4823. err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
  4824. if (err)
  4825. goto err_free_css;
  4826. err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
  4827. if (err < 0)
  4828. goto err_free_css;
  4829. css->id = err;
  4830. /* @css is ready to be brought online now, make it visible */
  4831. list_add_tail_rcu(&css->sibling, &parent_css->children);
  4832. cgroup_idr_replace(&ss->css_idr, css, css->id);
  4833. err = online_css(css);
  4834. if (err)
  4835. goto err_list_del;
  4836. return css;
  4837. err_list_del:
  4838. list_del_rcu(&css->sibling);
  4839. err_free_css:
  4840. list_del_rcu(&css->rstat_css_node);
  4841. INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
  4842. queue_rcu_work(cgroup_free_wq, &css->destroy_rwork);
  4843. return ERR_PTR(err);
  4844. }
  4845. /*
  4846. * The returned cgroup is fully initialized including its control mask, but
  4847. * it doesn't have the control mask applied.
  4848. */
  4849. static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,
  4850. umode_t mode)
  4851. {
  4852. struct cgroup_root *root = parent->root;
  4853. struct cgroup *cgrp, *tcgrp;
  4854. struct kernfs_node *kn;
  4855. int level = parent->level + 1;
  4856. int ret;
  4857. /* allocate the cgroup and its ID, 0 is reserved for the root */
  4858. cgrp = kzalloc(struct_size(cgrp, ancestors, (level + 1)), GFP_KERNEL);
  4859. if (!cgrp)
  4860. return ERR_PTR(-ENOMEM);
  4861. ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
  4862. if (ret)
  4863. goto out_free_cgrp;
  4864. ret = cgroup_rstat_init(cgrp);
  4865. if (ret)
  4866. goto out_cancel_ref;
  4867. /* create the directory */
  4868. kn = kernfs_create_dir_ns(parent->kn, name, mode,
  4869. current_fsuid(), current_fsgid(),
  4870. cgrp, NULL);
  4871. if (IS_ERR(kn)) {
  4872. ret = PTR_ERR(kn);
  4873. goto out_stat_exit;
  4874. }
  4875. cgrp->kn = kn;
  4876. init_cgroup_housekeeping(cgrp);
  4877. cgrp->self.parent = &parent->self;
  4878. cgrp->root = root;
  4879. cgrp->level = level;
  4880. ret = psi_cgroup_alloc(cgrp);
  4881. if (ret)
  4882. goto out_kernfs_remove;
  4883. if (cgrp->root == &cgrp_dfl_root) {
  4884. ret = cgroup_bpf_inherit(cgrp);
  4885. if (ret)
  4886. goto out_psi_free;
  4887. }
  4888. /*
  4889. * New cgroup inherits effective freeze counter, and
  4890. * if the parent has to be frozen, the child has too.
  4891. */
  4892. cgrp->freezer.e_freeze = parent->freezer.e_freeze;
  4893. if (cgrp->freezer.e_freeze) {
  4894. /*
  4895. * Set the CGRP_FREEZE flag, so when a process will be
  4896. * attached to the child cgroup, it will become frozen.
  4897. * At this point the new cgroup is unpopulated, so we can
  4898. * consider it frozen immediately.
  4899. */
  4900. set_bit(CGRP_FREEZE, &cgrp->flags);
  4901. set_bit(CGRP_FROZEN, &cgrp->flags);
  4902. }
  4903. spin_lock_irq(&css_set_lock);
  4904. for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) {
  4905. cgrp->ancestors[tcgrp->level] = tcgrp;
  4906. if (tcgrp != cgrp) {
  4907. tcgrp->nr_descendants++;
  4908. /*
  4909. * If the new cgroup is frozen, all ancestor cgroups
  4910. * get a new frozen descendant, but their state can't
  4911. * change because of this.
  4912. */
  4913. if (cgrp->freezer.e_freeze)
  4914. tcgrp->freezer.nr_frozen_descendants++;
  4915. }
  4916. }
  4917. spin_unlock_irq(&css_set_lock);
  4918. if (notify_on_release(parent))
  4919. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  4920. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
  4921. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
  4922. cgrp->self.serial_nr = css_serial_nr_next++;
  4923. /* allocation complete, commit to creation */
  4924. list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
  4925. atomic_inc(&root->nr_cgrps);
  4926. cgroup_get_live(parent);
  4927. /*
  4928. * On the default hierarchy, a child doesn't automatically inherit
  4929. * subtree_control from the parent. Each is configured manually.
  4930. */
  4931. if (!cgroup_on_dfl(cgrp))
  4932. cgrp->subtree_control = cgroup_control(cgrp);
  4933. cgroup_propagate_control(cgrp);
  4934. return cgrp;
  4935. out_psi_free:
  4936. psi_cgroup_free(cgrp);
  4937. out_kernfs_remove:
  4938. kernfs_remove(cgrp->kn);
  4939. out_stat_exit:
  4940. cgroup_rstat_exit(cgrp);
  4941. out_cancel_ref:
  4942. percpu_ref_exit(&cgrp->self.refcnt);
  4943. out_free_cgrp:
  4944. kfree(cgrp);
  4945. return ERR_PTR(ret);
  4946. }
  4947. static bool cgroup_check_hierarchy_limits(struct cgroup *parent)
  4948. {
  4949. struct cgroup *cgroup;
  4950. int ret = false;
  4951. int level = 0;
  4952. lockdep_assert_held(&cgroup_mutex);
  4953. for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) {
  4954. if (cgroup->nr_descendants >= cgroup->max_descendants)
  4955. goto fail;
  4956. if (level >= cgroup->max_depth)
  4957. goto fail;
  4958. level++;
  4959. }
  4960. ret = true;
  4961. fail:
  4962. return ret;
  4963. }
  4964. int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
  4965. {
  4966. struct cgroup *parent, *cgrp;
  4967. int ret;
  4968. /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
  4969. if (strchr(name, '\n'))
  4970. return -EINVAL;
  4971. parent = cgroup_kn_lock_live(parent_kn, false);
  4972. if (!parent)
  4973. return -ENODEV;
  4974. if (!cgroup_check_hierarchy_limits(parent)) {
  4975. ret = -EAGAIN;
  4976. goto out_unlock;
  4977. }
  4978. cgrp = cgroup_create(parent, name, mode);
  4979. if (IS_ERR(cgrp)) {
  4980. ret = PTR_ERR(cgrp);
  4981. goto out_unlock;
  4982. }
  4983. /*
  4984. * This extra ref will be put in cgroup_free_fn() and guarantees
  4985. * that @cgrp->kn is always accessible.
  4986. */
  4987. kernfs_get(cgrp->kn);
  4988. ret = css_populate_dir(&cgrp->self);
  4989. if (ret)
  4990. goto out_destroy;
  4991. ret = cgroup_apply_control_enable(cgrp);
  4992. if (ret)
  4993. goto out_destroy;
  4994. TRACE_CGROUP_PATH(mkdir, cgrp);
  4995. /* let's create and online css's */
  4996. kernfs_activate(cgrp->kn);
  4997. ret = 0;
  4998. goto out_unlock;
  4999. out_destroy:
  5000. cgroup_destroy_locked(cgrp);
  5001. out_unlock:
  5002. cgroup_kn_unlock(parent_kn);
  5003. return ret;
  5004. }
  5005. /*
  5006. * This is called when the refcnt of a css is confirmed to be killed.
  5007. * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
  5008. * initiate destruction and put the css ref from kill_css().
  5009. */
  5010. static void css_killed_work_fn(struct work_struct *work)
  5011. {
  5012. struct cgroup_subsys_state *css =
  5013. container_of(work, struct cgroup_subsys_state, destroy_work);
  5014. cgroup_lock();
  5015. do {
  5016. offline_css(css);
  5017. css_put(css);
  5018. /* @css can't go away while we're holding cgroup_mutex */
  5019. css = css->parent;
  5020. } while (css && atomic_dec_and_test(&css->online_cnt));
  5021. cgroup_unlock();
  5022. }
  5023. /* css kill confirmation processing requires process context, bounce */
  5024. static void css_killed_ref_fn(struct percpu_ref *ref)
  5025. {
  5026. struct cgroup_subsys_state *css =
  5027. container_of(ref, struct cgroup_subsys_state, refcnt);
  5028. if (atomic_dec_and_test(&css->online_cnt)) {
  5029. INIT_WORK(&css->destroy_work, css_killed_work_fn);
  5030. queue_work(cgroup_offline_wq, &css->destroy_work);
  5031. }
  5032. }
  5033. /**
  5034. * kill_css - destroy a css
  5035. * @css: css to destroy
  5036. *
  5037. * This function initiates destruction of @css by removing cgroup interface
  5038. * files and putting its base reference. ->css_offline() will be invoked
  5039. * asynchronously once css_tryget_online() is guaranteed to fail and when
  5040. * the reference count reaches zero, @css will be released.
  5041. */
  5042. static void kill_css(struct cgroup_subsys_state *css)
  5043. {
  5044. lockdep_assert_held(&cgroup_mutex);
  5045. if (css->flags & CSS_DYING)
  5046. return;
  5047. /*
  5048. * Call css_killed(), if defined, before setting the CSS_DYING flag
  5049. */
  5050. if (css->ss->css_killed)
  5051. css->ss->css_killed(css);
  5052. css->flags |= CSS_DYING;
  5053. /*
  5054. * This must happen before css is disassociated with its cgroup.
  5055. * See seq_css() for details.
  5056. */
  5057. css_clear_dir(css);
  5058. /*
  5059. * Killing would put the base ref, but we need to keep it alive
  5060. * until after ->css_offline().
  5061. */
  5062. css_get(css);
  5063. /*
  5064. * cgroup core guarantees that, by the time ->css_offline() is
  5065. * invoked, no new css reference will be given out via
  5066. * css_tryget_online(). We can't simply call percpu_ref_kill() and
  5067. * proceed to offlining css's because percpu_ref_kill() doesn't
  5068. * guarantee that the ref is seen as killed on all CPUs on return.
  5069. *
  5070. * Use percpu_ref_kill_and_confirm() to get notifications as each
  5071. * css is confirmed to be seen as killed on all CPUs.
  5072. */
  5073. percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
  5074. }
  5075. /**
  5076. * cgroup_destroy_locked - the first stage of cgroup destruction
  5077. * @cgrp: cgroup to be destroyed
  5078. *
  5079. * css's make use of percpu refcnts whose killing latency shouldn't be
  5080. * exposed to userland and are RCU protected. Also, cgroup core needs to
  5081. * guarantee that css_tryget_online() won't succeed by the time
  5082. * ->css_offline() is invoked. To satisfy all the requirements,
  5083. * destruction is implemented in the following two steps.
  5084. *
  5085. * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
  5086. * userland visible parts and start killing the percpu refcnts of
  5087. * css's. Set up so that the next stage will be kicked off once all
  5088. * the percpu refcnts are confirmed to be killed.
  5089. *
  5090. * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
  5091. * rest of destruction. Once all cgroup references are gone, the
  5092. * cgroup is RCU-freed.
  5093. *
  5094. * This function implements s1. After this step, @cgrp is gone as far as
  5095. * the userland is concerned and a new cgroup with the same name may be
  5096. * created. As cgroup doesn't care about the names internally, this
  5097. * doesn't cause any problem.
  5098. */
  5099. static int cgroup_destroy_locked(struct cgroup *cgrp)
  5100. __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
  5101. {
  5102. struct cgroup *tcgrp, *parent = cgroup_parent(cgrp);
  5103. struct cgroup_subsys_state *css;
  5104. struct cgrp_cset_link *link;
  5105. int ssid;
  5106. lockdep_assert_held(&cgroup_mutex);
  5107. /*
  5108. * Only migration can raise populated from zero and we're already
  5109. * holding cgroup_mutex.
  5110. */
  5111. if (cgroup_is_populated(cgrp))
  5112. return -EBUSY;
  5113. /*
  5114. * Make sure there's no live children. We can't test emptiness of
  5115. * ->self.children as dead children linger on it while being
  5116. * drained; otherwise, "rmdir parent/child parent" may fail.
  5117. */
  5118. if (css_has_online_children(&cgrp->self))
  5119. return -EBUSY;
  5120. /*
  5121. * Mark @cgrp and the associated csets dead. The former prevents
  5122. * further task migration and child creation by disabling
  5123. * cgroup_kn_lock_live(). The latter makes the csets ignored by
  5124. * the migration path.
  5125. */
  5126. cgrp->self.flags &= ~CSS_ONLINE;
  5127. spin_lock_irq(&css_set_lock);
  5128. list_for_each_entry(link, &cgrp->cset_links, cset_link)
  5129. link->cset->dead = true;
  5130. spin_unlock_irq(&css_set_lock);
  5131. /* initiate massacre of all css's */
  5132. for_each_css(css, ssid, cgrp)
  5133. kill_css(css);
  5134. /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */
  5135. css_clear_dir(&cgrp->self);
  5136. kernfs_remove(cgrp->kn);
  5137. if (cgroup_is_threaded(cgrp))
  5138. parent->nr_threaded_children--;
  5139. spin_lock_irq(&css_set_lock);
  5140. for (tcgrp = parent; tcgrp; tcgrp = cgroup_parent(tcgrp)) {
  5141. tcgrp->nr_descendants--;
  5142. tcgrp->nr_dying_descendants++;
  5143. /*
  5144. * If the dying cgroup is frozen, decrease frozen descendants
  5145. * counters of ancestor cgroups.
  5146. */
  5147. if (test_bit(CGRP_FROZEN, &cgrp->flags))
  5148. tcgrp->freezer.nr_frozen_descendants--;
  5149. }
  5150. spin_unlock_irq(&css_set_lock);
  5151. cgroup1_check_for_release(parent);
  5152. if (cgrp->root == &cgrp_dfl_root)
  5153. cgroup_bpf_offline(cgrp);
  5154. /* put the base reference */
  5155. percpu_ref_kill(&cgrp->self.refcnt);
  5156. return 0;
  5157. };
  5158. int cgroup_rmdir(struct kernfs_node *kn)
  5159. {
  5160. struct cgroup *cgrp;
  5161. int ret = 0;
  5162. cgrp = cgroup_kn_lock_live(kn, false);
  5163. if (!cgrp)
  5164. return 0;
  5165. ret = cgroup_destroy_locked(cgrp);
  5166. if (!ret)
  5167. TRACE_CGROUP_PATH(rmdir, cgrp);
  5168. cgroup_kn_unlock(kn);
  5169. return ret;
  5170. }
  5171. static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
  5172. .show_options = cgroup_show_options,
  5173. .mkdir = cgroup_mkdir,
  5174. .rmdir = cgroup_rmdir,
  5175. .show_path = cgroup_show_path,
  5176. };
  5177. static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
  5178. {
  5179. struct cgroup_subsys_state *css;
  5180. pr_debug("Initializing cgroup subsys %s\n", ss->name);
  5181. cgroup_lock();
  5182. idr_init(&ss->css_idr);
  5183. INIT_LIST_HEAD(&ss->cfts);
  5184. /* Create the root cgroup state for this subsystem */
  5185. ss->root = &cgrp_dfl_root;
  5186. css = ss->css_alloc(NULL);
  5187. /* We don't handle early failures gracefully */
  5188. BUG_ON(IS_ERR(css));
  5189. init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
  5190. /*
  5191. * Root csses are never destroyed and we can't initialize
  5192. * percpu_ref during early init. Disable refcnting.
  5193. */
  5194. css->flags |= CSS_NO_REF;
  5195. if (early) {
  5196. /* allocation can't be done safely during early init */
  5197. css->id = 1;
  5198. } else {
  5199. css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
  5200. BUG_ON(css->id < 0);
  5201. }
  5202. /* Update the init_css_set to contain a subsys
  5203. * pointer to this state - since the subsystem is
  5204. * newly registered, all tasks and hence the
  5205. * init_css_set is in the subsystem's root cgroup. */
  5206. init_css_set.subsys[ss->id] = css;
  5207. have_fork_callback |= (bool)ss->fork << ss->id;
  5208. have_exit_callback |= (bool)ss->exit << ss->id;
  5209. have_release_callback |= (bool)ss->release << ss->id;
  5210. have_canfork_callback |= (bool)ss->can_fork << ss->id;
  5211. /* At system boot, before all subsystems have been
  5212. * registered, no tasks have been forked, so we don't
  5213. * need to invoke fork callbacks here. */
  5214. BUG_ON(!list_empty(&init_task.tasks));
  5215. BUG_ON(online_css(css));
  5216. cgroup_unlock();
  5217. }
  5218. /**
  5219. * cgroup_init_early - cgroup initialization at system boot
  5220. *
  5221. * Initialize cgroups at system boot, and initialize any
  5222. * subsystems that request early init.
  5223. */
  5224. int __init cgroup_init_early(void)
  5225. {
  5226. static struct cgroup_fs_context __initdata ctx;
  5227. struct cgroup_subsys *ss;
  5228. int i;
  5229. ctx.root = &cgrp_dfl_root;
  5230. init_cgroup_root(&ctx);
  5231. cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
  5232. RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
  5233. for_each_subsys(ss, i) {
  5234. WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
  5235. "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
  5236. i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
  5237. ss->id, ss->name);
  5238. WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
  5239. "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
  5240. ss->id = i;
  5241. ss->name = cgroup_subsys_name[i];
  5242. if (!ss->legacy_name)
  5243. ss->legacy_name = cgroup_subsys_name[i];
  5244. if (ss->early_init)
  5245. cgroup_init_subsys(ss, true);
  5246. }
  5247. return 0;
  5248. }
  5249. /**
  5250. * cgroup_init - cgroup initialization
  5251. *
  5252. * Register cgroup filesystem and /proc file, and initialize
  5253. * any subsystems that didn't request early init.
  5254. */
  5255. int __init cgroup_init(void)
  5256. {
  5257. struct cgroup_subsys *ss;
  5258. int ssid;
  5259. BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
  5260. BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
  5261. BUG_ON(cgroup_init_cftypes(NULL, cgroup_psi_files));
  5262. BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files));
  5263. cgroup_rstat_boot();
  5264. get_user_ns(init_cgroup_ns.user_ns);
  5265. cgroup_lock();
  5266. /*
  5267. * Add init_css_set to the hash table so that dfl_root can link to
  5268. * it during init.
  5269. */
  5270. hash_add(css_set_table, &init_css_set.hlist,
  5271. css_set_hash(init_css_set.subsys));
  5272. BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
  5273. cgroup_unlock();
  5274. for_each_subsys(ss, ssid) {
  5275. if (ss->early_init) {
  5276. struct cgroup_subsys_state *css =
  5277. init_css_set.subsys[ss->id];
  5278. css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
  5279. GFP_KERNEL);
  5280. BUG_ON(css->id < 0);
  5281. } else {
  5282. cgroup_init_subsys(ss, false);
  5283. }
  5284. list_add_tail(&init_css_set.e_cset_node[ssid],
  5285. &cgrp_dfl_root.cgrp.e_csets[ssid]);
  5286. /*
  5287. * Setting dfl_root subsys_mask needs to consider the
  5288. * disabled flag and cftype registration needs kmalloc,
  5289. * both of which aren't available during early_init.
  5290. */
  5291. if (!cgroup_ssid_enabled(ssid))
  5292. continue;
  5293. if (cgroup1_ssid_disabled(ssid))
  5294. pr_info("Disabling %s control group subsystem in v1 mounts\n",
  5295. ss->legacy_name);
  5296. cgrp_dfl_root.subsys_mask |= 1 << ss->id;
  5297. /* implicit controllers must be threaded too */
  5298. WARN_ON(ss->implicit_on_dfl && !ss->threaded);
  5299. if (ss->implicit_on_dfl)
  5300. cgrp_dfl_implicit_ss_mask |= 1 << ss->id;
  5301. else if (!ss->dfl_cftypes)
  5302. cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
  5303. if (ss->threaded)
  5304. cgrp_dfl_threaded_ss_mask |= 1 << ss->id;
  5305. if (ss->dfl_cftypes == ss->legacy_cftypes) {
  5306. WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
  5307. } else {
  5308. WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
  5309. WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
  5310. }
  5311. if (ss->bind)
  5312. ss->bind(init_css_set.subsys[ssid]);
  5313. cgroup_lock();
  5314. css_populate_dir(init_css_set.subsys[ssid]);
  5315. cgroup_unlock();
  5316. }
  5317. /* init_css_set.subsys[] has been updated, re-hash */
  5318. hash_del(&init_css_set.hlist);
  5319. hash_add(css_set_table, &init_css_set.hlist,
  5320. css_set_hash(init_css_set.subsys));
  5321. WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
  5322. WARN_ON(register_filesystem(&cgroup_fs_type));
  5323. WARN_ON(register_filesystem(&cgroup2_fs_type));
  5324. WARN_ON(!proc_create_single("cgroups", 0, NULL, proc_cgroupstats_show));
  5325. #ifdef CONFIG_CPUSETS_V1
  5326. WARN_ON(register_filesystem(&cpuset_fs_type));
  5327. #endif
  5328. return 0;
  5329. }
  5330. static int __init cgroup_wq_init(void)
  5331. {
  5332. /*
  5333. * There isn't much point in executing destruction path in
  5334. * parallel. Good chunk is serialized with cgroup_mutex anyway.
  5335. * Use 1 for @max_active.
  5336. *
  5337. * We would prefer to do this in cgroup_init() above, but that
  5338. * is called before init_workqueues(): so leave this until after.
  5339. */
  5340. cgroup_offline_wq = alloc_workqueue("cgroup_offline", 0, 1);
  5341. BUG_ON(!cgroup_offline_wq);
  5342. cgroup_release_wq = alloc_workqueue("cgroup_release", 0, 1);
  5343. BUG_ON(!cgroup_release_wq);
  5344. cgroup_free_wq = alloc_workqueue("cgroup_free", 0, 1);
  5345. BUG_ON(!cgroup_free_wq);
  5346. return 0;
  5347. }
  5348. core_initcall(cgroup_wq_init);
  5349. void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
  5350. {
  5351. struct kernfs_node *kn;
  5352. kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id);
  5353. if (!kn)
  5354. return;
  5355. kernfs_path(kn, buf, buflen);
  5356. kernfs_put(kn);
  5357. }
  5358. /*
  5359. * cgroup_get_from_id : get the cgroup associated with cgroup id
  5360. * @id: cgroup id
  5361. * On success return the cgrp or ERR_PTR on failure
  5362. * Only cgroups within current task's cgroup NS are valid.
  5363. */
  5364. struct cgroup *cgroup_get_from_id(u64 id)
  5365. {
  5366. struct kernfs_node *kn;
  5367. struct cgroup *cgrp, *root_cgrp;
  5368. kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id);
  5369. if (!kn)
  5370. return ERR_PTR(-ENOENT);
  5371. if (kernfs_type(kn) != KERNFS_DIR) {
  5372. kernfs_put(kn);
  5373. return ERR_PTR(-ENOENT);
  5374. }
  5375. rcu_read_lock();
  5376. cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
  5377. if (cgrp && !cgroup_tryget(cgrp))
  5378. cgrp = NULL;
  5379. rcu_read_unlock();
  5380. kernfs_put(kn);
  5381. if (!cgrp)
  5382. return ERR_PTR(-ENOENT);
  5383. root_cgrp = current_cgns_cgroup_dfl();
  5384. if (!cgroup_is_descendant(cgrp, root_cgrp)) {
  5385. cgroup_put(cgrp);
  5386. return ERR_PTR(-ENOENT);
  5387. }
  5388. return cgrp;
  5389. }
  5390. EXPORT_SYMBOL_GPL(cgroup_get_from_id);
  5391. /*
  5392. * proc_cgroup_show()
  5393. * - Print task's cgroup paths into seq_file, one line for each hierarchy
  5394. * - Used for /proc/<pid>/cgroup.
  5395. */
  5396. int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
  5397. struct pid *pid, struct task_struct *tsk)
  5398. {
  5399. char *buf;
  5400. int retval;
  5401. struct cgroup_root *root;
  5402. retval = -ENOMEM;
  5403. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  5404. if (!buf)
  5405. goto out;
  5406. rcu_read_lock();
  5407. spin_lock_irq(&css_set_lock);
  5408. for_each_root(root) {
  5409. struct cgroup_subsys *ss;
  5410. struct cgroup *cgrp;
  5411. int ssid, count = 0;
  5412. if (root == &cgrp_dfl_root && !READ_ONCE(cgrp_dfl_visible))
  5413. continue;
  5414. cgrp = task_cgroup_from_root(tsk, root);
  5415. /* The root has already been unmounted. */
  5416. if (!cgrp)
  5417. continue;
  5418. seq_printf(m, "%d:", root->hierarchy_id);
  5419. if (root != &cgrp_dfl_root)
  5420. for_each_subsys(ss, ssid)
  5421. if (root->subsys_mask & (1 << ssid))
  5422. seq_printf(m, "%s%s", count++ ? "," : "",
  5423. ss->legacy_name);
  5424. if (strlen(root->name))
  5425. seq_printf(m, "%sname=%s", count ? "," : "",
  5426. root->name);
  5427. seq_putc(m, ':');
  5428. /*
  5429. * On traditional hierarchies, all zombie tasks show up as
  5430. * belonging to the root cgroup. On the default hierarchy,
  5431. * while a zombie doesn't show up in "cgroup.procs" and
  5432. * thus can't be migrated, its /proc/PID/cgroup keeps
  5433. * reporting the cgroup it belonged to before exiting. If
  5434. * the cgroup is removed before the zombie is reaped,
  5435. * " (deleted)" is appended to the cgroup path.
  5436. */
  5437. if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
  5438. retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX,
  5439. current->nsproxy->cgroup_ns);
  5440. if (retval == -E2BIG)
  5441. retval = -ENAMETOOLONG;
  5442. if (retval < 0)
  5443. goto out_unlock;
  5444. seq_puts(m, buf);
  5445. } else {
  5446. seq_puts(m, "/");
  5447. }
  5448. if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
  5449. seq_puts(m, " (deleted)\n");
  5450. else
  5451. seq_putc(m, '\n');
  5452. }
  5453. retval = 0;
  5454. out_unlock:
  5455. spin_unlock_irq(&css_set_lock);
  5456. rcu_read_unlock();
  5457. kfree(buf);
  5458. out:
  5459. return retval;
  5460. }
  5461. /**
  5462. * cgroup_fork - initialize cgroup related fields during copy_process()
  5463. * @child: pointer to task_struct of forking parent process.
  5464. *
  5465. * A task is associated with the init_css_set until cgroup_post_fork()
  5466. * attaches it to the target css_set.
  5467. */
  5468. void cgroup_fork(struct task_struct *child)
  5469. {
  5470. RCU_INIT_POINTER(child->cgroups, &init_css_set);
  5471. INIT_LIST_HEAD(&child->cg_list);
  5472. }
  5473. /**
  5474. * cgroup_v1v2_get_from_file - get a cgroup pointer from a file pointer
  5475. * @f: file corresponding to cgroup_dir
  5476. *
  5477. * Find the cgroup from a file pointer associated with a cgroup directory.
  5478. * Returns a pointer to the cgroup on success. ERR_PTR is returned if the
  5479. * cgroup cannot be found.
  5480. */
  5481. static struct cgroup *cgroup_v1v2_get_from_file(struct file *f)
  5482. {
  5483. struct cgroup_subsys_state *css;
  5484. css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
  5485. if (IS_ERR(css))
  5486. return ERR_CAST(css);
  5487. return css->cgroup;
  5488. }
  5489. /**
  5490. * cgroup_get_from_file - same as cgroup_v1v2_get_from_file, but only supports
  5491. * cgroup2.
  5492. * @f: file corresponding to cgroup2_dir
  5493. */
  5494. static struct cgroup *cgroup_get_from_file(struct file *f)
  5495. {
  5496. struct cgroup *cgrp = cgroup_v1v2_get_from_file(f);
  5497. if (IS_ERR(cgrp))
  5498. return ERR_CAST(cgrp);
  5499. if (!cgroup_on_dfl(cgrp)) {
  5500. cgroup_put(cgrp);
  5501. return ERR_PTR(-EBADF);
  5502. }
  5503. return cgrp;
  5504. }
  5505. /**
  5506. * cgroup_css_set_fork - find or create a css_set for a child process
  5507. * @kargs: the arguments passed to create the child process
  5508. *
  5509. * This functions finds or creates a new css_set which the child
  5510. * process will be attached to in cgroup_post_fork(). By default,
  5511. * the child process will be given the same css_set as its parent.
  5512. *
  5513. * If CLONE_INTO_CGROUP is specified this function will try to find an
  5514. * existing css_set which includes the requested cgroup and if not create
  5515. * a new css_set that the child will be attached to later. If this function
  5516. * succeeds it will hold cgroup_threadgroup_rwsem on return. If
  5517. * CLONE_INTO_CGROUP is requested this function will grab cgroup mutex
  5518. * before grabbing cgroup_threadgroup_rwsem and will hold a reference
  5519. * to the target cgroup.
  5520. */
  5521. static int cgroup_css_set_fork(struct kernel_clone_args *kargs)
  5522. __acquires(&cgroup_mutex) __acquires(&cgroup_threadgroup_rwsem)
  5523. {
  5524. int ret;
  5525. struct cgroup *dst_cgrp = NULL;
  5526. struct css_set *cset;
  5527. struct super_block *sb;
  5528. struct file *f;
  5529. if (kargs->flags & CLONE_INTO_CGROUP)
  5530. cgroup_lock();
  5531. cgroup_threadgroup_change_begin(current);
  5532. spin_lock_irq(&css_set_lock);
  5533. cset = task_css_set(current);
  5534. get_css_set(cset);
  5535. if (kargs->cgrp)
  5536. kargs->kill_seq = kargs->cgrp->kill_seq;
  5537. else
  5538. kargs->kill_seq = cset->dfl_cgrp->kill_seq;
  5539. spin_unlock_irq(&css_set_lock);
  5540. if (!(kargs->flags & CLONE_INTO_CGROUP)) {
  5541. kargs->cset = cset;
  5542. return 0;
  5543. }
  5544. f = fget_raw(kargs->cgroup);
  5545. if (!f) {
  5546. ret = -EBADF;
  5547. goto err;
  5548. }
  5549. sb = f->f_path.dentry->d_sb;
  5550. dst_cgrp = cgroup_get_from_file(f);
  5551. if (IS_ERR(dst_cgrp)) {
  5552. ret = PTR_ERR(dst_cgrp);
  5553. dst_cgrp = NULL;
  5554. goto err;
  5555. }
  5556. if (cgroup_is_dead(dst_cgrp)) {
  5557. ret = -ENODEV;
  5558. goto err;
  5559. }
  5560. /*
  5561. * Verify that we the target cgroup is writable for us. This is
  5562. * usually done by the vfs layer but since we're not going through
  5563. * the vfs layer here we need to do it "manually".
  5564. */
  5565. ret = cgroup_may_write(dst_cgrp, sb);
  5566. if (ret)
  5567. goto err;
  5568. /*
  5569. * Spawning a task directly into a cgroup works by passing a file
  5570. * descriptor to the target cgroup directory. This can even be an O_PATH
  5571. * file descriptor. But it can never be a cgroup.procs file descriptor.
  5572. * This was done on purpose so spawning into a cgroup could be
  5573. * conceptualized as an atomic
  5574. *
  5575. * fd = openat(dfd_cgroup, "cgroup.procs", ...);
  5576. * write(fd, <child-pid>, ...);
  5577. *
  5578. * sequence, i.e. it's a shorthand for the caller opening and writing
  5579. * cgroup.procs of the cgroup indicated by @dfd_cgroup. This allows us
  5580. * to always use the caller's credentials.
  5581. */
  5582. ret = cgroup_attach_permissions(cset->dfl_cgrp, dst_cgrp, sb,
  5583. !(kargs->flags & CLONE_THREAD),
  5584. current->nsproxy->cgroup_ns);
  5585. if (ret)
  5586. goto err;
  5587. kargs->cset = find_css_set(cset, dst_cgrp);
  5588. if (!kargs->cset) {
  5589. ret = -ENOMEM;
  5590. goto err;
  5591. }
  5592. put_css_set(cset);
  5593. fput(f);
  5594. kargs->cgrp = dst_cgrp;
  5595. return ret;
  5596. err:
  5597. cgroup_threadgroup_change_end(current);
  5598. cgroup_unlock();
  5599. if (f)
  5600. fput(f);
  5601. if (dst_cgrp)
  5602. cgroup_put(dst_cgrp);
  5603. put_css_set(cset);
  5604. if (kargs->cset)
  5605. put_css_set(kargs->cset);
  5606. return ret;
  5607. }
  5608. /**
  5609. * cgroup_css_set_put_fork - drop references we took during fork
  5610. * @kargs: the arguments passed to create the child process
  5611. *
  5612. * Drop references to the prepared css_set and target cgroup if
  5613. * CLONE_INTO_CGROUP was requested.
  5614. */
  5615. static void cgroup_css_set_put_fork(struct kernel_clone_args *kargs)
  5616. __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex)
  5617. {
  5618. struct cgroup *cgrp = kargs->cgrp;
  5619. struct css_set *cset = kargs->cset;
  5620. cgroup_threadgroup_change_end(current);
  5621. if (cset) {
  5622. put_css_set(cset);
  5623. kargs->cset = NULL;
  5624. }
  5625. if (kargs->flags & CLONE_INTO_CGROUP) {
  5626. cgroup_unlock();
  5627. if (cgrp) {
  5628. cgroup_put(cgrp);
  5629. kargs->cgrp = NULL;
  5630. }
  5631. }
  5632. }
  5633. /**
  5634. * cgroup_can_fork - called on a new task before the process is exposed
  5635. * @child: the child process
  5636. * @kargs: the arguments passed to create the child process
  5637. *
  5638. * This prepares a new css_set for the child process which the child will
  5639. * be attached to in cgroup_post_fork().
  5640. * This calls the subsystem can_fork() callbacks. If the cgroup_can_fork()
  5641. * callback returns an error, the fork aborts with that error code. This
  5642. * allows for a cgroup subsystem to conditionally allow or deny new forks.
  5643. */
  5644. int cgroup_can_fork(struct task_struct *child, struct kernel_clone_args *kargs)
  5645. {
  5646. struct cgroup_subsys *ss;
  5647. int i, j, ret;
  5648. ret = cgroup_css_set_fork(kargs);
  5649. if (ret)
  5650. return ret;
  5651. do_each_subsys_mask(ss, i, have_canfork_callback) {
  5652. ret = ss->can_fork(child, kargs->cset);
  5653. if (ret)
  5654. goto out_revert;
  5655. } while_each_subsys_mask();
  5656. return 0;
  5657. out_revert:
  5658. for_each_subsys(ss, j) {
  5659. if (j >= i)
  5660. break;
  5661. if (ss->cancel_fork)
  5662. ss->cancel_fork(child, kargs->cset);
  5663. }
  5664. cgroup_css_set_put_fork(kargs);
  5665. return ret;
  5666. }
  5667. /**
  5668. * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
  5669. * @child: the child process
  5670. * @kargs: the arguments passed to create the child process
  5671. *
  5672. * This calls the cancel_fork() callbacks if a fork failed *after*
  5673. * cgroup_can_fork() succeeded and cleans up references we took to
  5674. * prepare a new css_set for the child process in cgroup_can_fork().
  5675. */
  5676. void cgroup_cancel_fork(struct task_struct *child,
  5677. struct kernel_clone_args *kargs)
  5678. {
  5679. struct cgroup_subsys *ss;
  5680. int i;
  5681. for_each_subsys(ss, i)
  5682. if (ss->cancel_fork)
  5683. ss->cancel_fork(child, kargs->cset);
  5684. cgroup_css_set_put_fork(kargs);
  5685. }
  5686. /**
  5687. * cgroup_post_fork - finalize cgroup setup for the child process
  5688. * @child: the child process
  5689. * @kargs: the arguments passed to create the child process
  5690. *
  5691. * Attach the child process to its css_set calling the subsystem fork()
  5692. * callbacks.
  5693. */
  5694. void cgroup_post_fork(struct task_struct *child,
  5695. struct kernel_clone_args *kargs)
  5696. __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex)
  5697. {
  5698. unsigned int cgrp_kill_seq = 0;
  5699. unsigned long cgrp_flags = 0;
  5700. bool kill = false;
  5701. struct cgroup_subsys *ss;
  5702. struct css_set *cset;
  5703. int i;
  5704. cset = kargs->cset;
  5705. kargs->cset = NULL;
  5706. spin_lock_irq(&css_set_lock);
  5707. /* init tasks are special, only link regular threads */
  5708. if (likely(child->pid)) {
  5709. if (kargs->cgrp) {
  5710. cgrp_flags = kargs->cgrp->flags;
  5711. cgrp_kill_seq = kargs->cgrp->kill_seq;
  5712. } else {
  5713. cgrp_flags = cset->dfl_cgrp->flags;
  5714. cgrp_kill_seq = cset->dfl_cgrp->kill_seq;
  5715. }
  5716. WARN_ON_ONCE(!list_empty(&child->cg_list));
  5717. cset->nr_tasks++;
  5718. css_set_move_task(child, NULL, cset, false);
  5719. } else {
  5720. put_css_set(cset);
  5721. cset = NULL;
  5722. }
  5723. if (!(child->flags & PF_KTHREAD)) {
  5724. if (unlikely(test_bit(CGRP_FREEZE, &cgrp_flags))) {
  5725. /*
  5726. * If the cgroup has to be frozen, the new task has
  5727. * too. Let's set the JOBCTL_TRAP_FREEZE jobctl bit to
  5728. * get the task into the frozen state.
  5729. */
  5730. spin_lock(&child->sighand->siglock);
  5731. WARN_ON_ONCE(child->frozen);
  5732. child->jobctl |= JOBCTL_TRAP_FREEZE;
  5733. spin_unlock(&child->sighand->siglock);
  5734. /*
  5735. * Calling cgroup_update_frozen() isn't required here,
  5736. * because it will be called anyway a bit later from
  5737. * do_freezer_trap(). So we avoid cgroup's transient
  5738. * switch from the frozen state and back.
  5739. */
  5740. }
  5741. /*
  5742. * If the cgroup is to be killed notice it now and take the
  5743. * child down right after we finished preparing it for
  5744. * userspace.
  5745. */
  5746. kill = kargs->kill_seq != cgrp_kill_seq;
  5747. }
  5748. spin_unlock_irq(&css_set_lock);
  5749. /*
  5750. * Call ss->fork(). This must happen after @child is linked on
  5751. * css_set; otherwise, @child might change state between ->fork()
  5752. * and addition to css_set.
  5753. */
  5754. do_each_subsys_mask(ss, i, have_fork_callback) {
  5755. ss->fork(child);
  5756. } while_each_subsys_mask();
  5757. /* Make the new cset the root_cset of the new cgroup namespace. */
  5758. if (kargs->flags & CLONE_NEWCGROUP) {
  5759. struct css_set *rcset = child->nsproxy->cgroup_ns->root_cset;
  5760. get_css_set(cset);
  5761. child->nsproxy->cgroup_ns->root_cset = cset;
  5762. put_css_set(rcset);
  5763. }
  5764. /* Cgroup has to be killed so take down child immediately. */
  5765. if (unlikely(kill))
  5766. do_send_sig_info(SIGKILL, SEND_SIG_NOINFO, child, PIDTYPE_TGID);
  5767. cgroup_css_set_put_fork(kargs);
  5768. }
  5769. /**
  5770. * cgroup_exit - detach cgroup from exiting task
  5771. * @tsk: pointer to task_struct of exiting process
  5772. *
  5773. * Description: Detach cgroup from @tsk.
  5774. *
  5775. */
  5776. void cgroup_exit(struct task_struct *tsk)
  5777. {
  5778. struct cgroup_subsys *ss;
  5779. struct css_set *cset;
  5780. int i;
  5781. spin_lock_irq(&css_set_lock);
  5782. WARN_ON_ONCE(list_empty(&tsk->cg_list));
  5783. cset = task_css_set(tsk);
  5784. css_set_move_task(tsk, cset, NULL, false);
  5785. cset->nr_tasks--;
  5786. /* matches the signal->live check in css_task_iter_advance() */
  5787. if (thread_group_leader(tsk) && atomic_read(&tsk->signal->live))
  5788. list_add_tail(&tsk->cg_list, &cset->dying_tasks);
  5789. if (dl_task(tsk))
  5790. dec_dl_tasks_cs(tsk);
  5791. WARN_ON_ONCE(cgroup_task_frozen(tsk));
  5792. if (unlikely(!(tsk->flags & PF_KTHREAD) &&
  5793. test_bit(CGRP_FREEZE, &task_dfl_cgroup(tsk)->flags)))
  5794. cgroup_update_frozen(task_dfl_cgroup(tsk));
  5795. spin_unlock_irq(&css_set_lock);
  5796. /* see cgroup_post_fork() for details */
  5797. do_each_subsys_mask(ss, i, have_exit_callback) {
  5798. ss->exit(tsk);
  5799. } while_each_subsys_mask();
  5800. }
  5801. void cgroup_release(struct task_struct *task)
  5802. {
  5803. struct cgroup_subsys *ss;
  5804. int ssid;
  5805. do_each_subsys_mask(ss, ssid, have_release_callback) {
  5806. ss->release(task);
  5807. } while_each_subsys_mask();
  5808. if (!list_empty(&task->cg_list)) {
  5809. spin_lock_irq(&css_set_lock);
  5810. css_set_skip_task_iters(task_css_set(task), task);
  5811. list_del_init(&task->cg_list);
  5812. spin_unlock_irq(&css_set_lock);
  5813. }
  5814. }
  5815. void cgroup_free(struct task_struct *task)
  5816. {
  5817. struct css_set *cset = task_css_set(task);
  5818. put_css_set(cset);
  5819. }
  5820. static int __init cgroup_disable(char *str)
  5821. {
  5822. struct cgroup_subsys *ss;
  5823. char *token;
  5824. int i;
  5825. while ((token = strsep(&str, ",")) != NULL) {
  5826. if (!*token)
  5827. continue;
  5828. for_each_subsys(ss, i) {
  5829. if (strcmp(token, ss->name) &&
  5830. strcmp(token, ss->legacy_name))
  5831. continue;
  5832. static_branch_disable(cgroup_subsys_enabled_key[i]);
  5833. pr_info("Disabling %s control group subsystem\n",
  5834. ss->name);
  5835. }
  5836. for (i = 0; i < OPT_FEATURE_COUNT; i++) {
  5837. if (strcmp(token, cgroup_opt_feature_names[i]))
  5838. continue;
  5839. cgroup_feature_disable_mask |= 1 << i;
  5840. pr_info("Disabling %s control group feature\n",
  5841. cgroup_opt_feature_names[i]);
  5842. break;
  5843. }
  5844. }
  5845. return 1;
  5846. }
  5847. __setup("cgroup_disable=", cgroup_disable);
  5848. void __init __weak enable_debug_cgroup(void) { }
  5849. static int __init enable_cgroup_debug(char *str)
  5850. {
  5851. cgroup_debug = true;
  5852. enable_debug_cgroup();
  5853. return 1;
  5854. }
  5855. __setup("cgroup_debug", enable_cgroup_debug);
  5856. static int __init cgroup_favordynmods_setup(char *str)
  5857. {
  5858. return (kstrtobool(str, &have_favordynmods) == 0);
  5859. }
  5860. __setup("cgroup_favordynmods=", cgroup_favordynmods_setup);
  5861. /**
  5862. * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
  5863. * @dentry: directory dentry of interest
  5864. * @ss: subsystem of interest
  5865. *
  5866. * If @dentry is a directory for a cgroup which has @ss enabled on it, try
  5867. * to get the corresponding css and return it. If such css doesn't exist
  5868. * or can't be pinned, an ERR_PTR value is returned.
  5869. */
  5870. struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
  5871. struct cgroup_subsys *ss)
  5872. {
  5873. struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
  5874. struct file_system_type *s_type = dentry->d_sb->s_type;
  5875. struct cgroup_subsys_state *css = NULL;
  5876. struct cgroup *cgrp;
  5877. /* is @dentry a cgroup dir? */
  5878. if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
  5879. !kn || kernfs_type(kn) != KERNFS_DIR)
  5880. return ERR_PTR(-EBADF);
  5881. rcu_read_lock();
  5882. /*
  5883. * This path doesn't originate from kernfs and @kn could already
  5884. * have been or be removed at any point. @kn->priv is RCU
  5885. * protected for this access. See css_release_work_fn() for details.
  5886. */
  5887. cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
  5888. if (cgrp)
  5889. css = cgroup_css(cgrp, ss);
  5890. if (!css || !css_tryget_online(css))
  5891. css = ERR_PTR(-ENOENT);
  5892. rcu_read_unlock();
  5893. return css;
  5894. }
  5895. /**
  5896. * css_from_id - lookup css by id
  5897. * @id: the cgroup id
  5898. * @ss: cgroup subsys to be looked into
  5899. *
  5900. * Returns the css if there's valid one with @id, otherwise returns NULL.
  5901. * Should be called under rcu_read_lock().
  5902. */
  5903. struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
  5904. {
  5905. WARN_ON_ONCE(!rcu_read_lock_held());
  5906. return idr_find(&ss->css_idr, id);
  5907. }
  5908. /**
  5909. * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
  5910. * @path: path on the default hierarchy
  5911. *
  5912. * Find the cgroup at @path on the default hierarchy, increment its
  5913. * reference count and return it. Returns pointer to the found cgroup on
  5914. * success, ERR_PTR(-ENOENT) if @path doesn't exist or if the cgroup has already
  5915. * been released and ERR_PTR(-ENOTDIR) if @path points to a non-directory.
  5916. */
  5917. struct cgroup *cgroup_get_from_path(const char *path)
  5918. {
  5919. struct kernfs_node *kn;
  5920. struct cgroup *cgrp = ERR_PTR(-ENOENT);
  5921. struct cgroup *root_cgrp;
  5922. root_cgrp = current_cgns_cgroup_dfl();
  5923. kn = kernfs_walk_and_get(root_cgrp->kn, path);
  5924. if (!kn)
  5925. goto out;
  5926. if (kernfs_type(kn) != KERNFS_DIR) {
  5927. cgrp = ERR_PTR(-ENOTDIR);
  5928. goto out_kernfs;
  5929. }
  5930. rcu_read_lock();
  5931. cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
  5932. if (!cgrp || !cgroup_tryget(cgrp))
  5933. cgrp = ERR_PTR(-ENOENT);
  5934. rcu_read_unlock();
  5935. out_kernfs:
  5936. kernfs_put(kn);
  5937. out:
  5938. return cgrp;
  5939. }
  5940. EXPORT_SYMBOL_GPL(cgroup_get_from_path);
  5941. /**
  5942. * cgroup_v1v2_get_from_fd - get a cgroup pointer from a fd
  5943. * @fd: fd obtained by open(cgroup_dir)
  5944. *
  5945. * Find the cgroup from a fd which should be obtained
  5946. * by opening a cgroup directory. Returns a pointer to the
  5947. * cgroup on success. ERR_PTR is returned if the cgroup
  5948. * cannot be found.
  5949. */
  5950. struct cgroup *cgroup_v1v2_get_from_fd(int fd)
  5951. {
  5952. struct cgroup *cgrp;
  5953. struct fd f = fdget_raw(fd);
  5954. if (!fd_file(f))
  5955. return ERR_PTR(-EBADF);
  5956. cgrp = cgroup_v1v2_get_from_file(fd_file(f));
  5957. fdput(f);
  5958. return cgrp;
  5959. }
  5960. /**
  5961. * cgroup_get_from_fd - same as cgroup_v1v2_get_from_fd, but only supports
  5962. * cgroup2.
  5963. * @fd: fd obtained by open(cgroup2_dir)
  5964. */
  5965. struct cgroup *cgroup_get_from_fd(int fd)
  5966. {
  5967. struct cgroup *cgrp = cgroup_v1v2_get_from_fd(fd);
  5968. if (IS_ERR(cgrp))
  5969. return ERR_CAST(cgrp);
  5970. if (!cgroup_on_dfl(cgrp)) {
  5971. cgroup_put(cgrp);
  5972. return ERR_PTR(-EBADF);
  5973. }
  5974. return cgrp;
  5975. }
  5976. EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
  5977. static u64 power_of_ten(int power)
  5978. {
  5979. u64 v = 1;
  5980. while (power--)
  5981. v *= 10;
  5982. return v;
  5983. }
  5984. /**
  5985. * cgroup_parse_float - parse a floating number
  5986. * @input: input string
  5987. * @dec_shift: number of decimal digits to shift
  5988. * @v: output
  5989. *
  5990. * Parse a decimal floating point number in @input and store the result in
  5991. * @v with decimal point right shifted @dec_shift times. For example, if
  5992. * @input is "12.3456" and @dec_shift is 3, *@v will be set to 12345.
  5993. * Returns 0 on success, -errno otherwise.
  5994. *
  5995. * There's nothing cgroup specific about this function except that it's
  5996. * currently the only user.
  5997. */
  5998. int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v)
  5999. {
  6000. s64 whole, frac = 0;
  6001. int fstart = 0, fend = 0, flen;
  6002. if (!sscanf(input, "%lld.%n%lld%n", &whole, &fstart, &frac, &fend))
  6003. return -EINVAL;
  6004. if (frac < 0)
  6005. return -EINVAL;
  6006. flen = fend > fstart ? fend - fstart : 0;
  6007. if (flen < dec_shift)
  6008. frac *= power_of_ten(dec_shift - flen);
  6009. else
  6010. frac = DIV_ROUND_CLOSEST_ULL(frac, power_of_ten(flen - dec_shift));
  6011. *v = whole * power_of_ten(dec_shift) + frac;
  6012. return 0;
  6013. }
  6014. /*
  6015. * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
  6016. * definition in cgroup-defs.h.
  6017. */
  6018. #ifdef CONFIG_SOCK_CGROUP_DATA
  6019. void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
  6020. {
  6021. struct cgroup *cgroup;
  6022. rcu_read_lock();
  6023. /* Don't associate the sock with unrelated interrupted task's cgroup. */
  6024. if (in_interrupt()) {
  6025. cgroup = &cgrp_dfl_root.cgrp;
  6026. cgroup_get(cgroup);
  6027. goto out;
  6028. }
  6029. while (true) {
  6030. struct css_set *cset;
  6031. cset = task_css_set(current);
  6032. if (likely(cgroup_tryget(cset->dfl_cgrp))) {
  6033. cgroup = cset->dfl_cgrp;
  6034. break;
  6035. }
  6036. cpu_relax();
  6037. }
  6038. out:
  6039. skcd->cgroup = cgroup;
  6040. cgroup_bpf_get(cgroup);
  6041. rcu_read_unlock();
  6042. }
  6043. void cgroup_sk_clone(struct sock_cgroup_data *skcd)
  6044. {
  6045. struct cgroup *cgrp = sock_cgroup_ptr(skcd);
  6046. /*
  6047. * We might be cloning a socket which is left in an empty
  6048. * cgroup and the cgroup might have already been rmdir'd.
  6049. * Don't use cgroup_get_live().
  6050. */
  6051. cgroup_get(cgrp);
  6052. cgroup_bpf_get(cgrp);
  6053. }
  6054. void cgroup_sk_free(struct sock_cgroup_data *skcd)
  6055. {
  6056. struct cgroup *cgrp = sock_cgroup_ptr(skcd);
  6057. cgroup_bpf_put(cgrp);
  6058. cgroup_put(cgrp);
  6059. }
  6060. #endif /* CONFIG_SOCK_CGROUP_DATA */
  6061. #ifdef CONFIG_SYSFS
  6062. static ssize_t show_delegatable_files(struct cftype *files, char *buf,
  6063. ssize_t size, const char *prefix)
  6064. {
  6065. struct cftype *cft;
  6066. ssize_t ret = 0;
  6067. for (cft = files; cft && cft->name[0] != '\0'; cft++) {
  6068. if (!(cft->flags & CFTYPE_NS_DELEGATABLE))
  6069. continue;
  6070. if (prefix)
  6071. ret += snprintf(buf + ret, size - ret, "%s.", prefix);
  6072. ret += snprintf(buf + ret, size - ret, "%s\n", cft->name);
  6073. if (WARN_ON(ret >= size))
  6074. break;
  6075. }
  6076. return ret;
  6077. }
  6078. static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr,
  6079. char *buf)
  6080. {
  6081. struct cgroup_subsys *ss;
  6082. int ssid;
  6083. ssize_t ret = 0;
  6084. ret = show_delegatable_files(cgroup_base_files, buf + ret,
  6085. PAGE_SIZE - ret, NULL);
  6086. if (cgroup_psi_enabled())
  6087. ret += show_delegatable_files(cgroup_psi_files, buf + ret,
  6088. PAGE_SIZE - ret, NULL);
  6089. for_each_subsys(ss, ssid)
  6090. ret += show_delegatable_files(ss->dfl_cftypes, buf + ret,
  6091. PAGE_SIZE - ret,
  6092. cgroup_subsys_name[ssid]);
  6093. return ret;
  6094. }
  6095. static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate);
  6096. static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr,
  6097. char *buf)
  6098. {
  6099. return snprintf(buf, PAGE_SIZE,
  6100. "nsdelegate\n"
  6101. "favordynmods\n"
  6102. "memory_localevents\n"
  6103. "memory_recursiveprot\n"
  6104. "memory_hugetlb_accounting\n"
  6105. "pids_localevents\n");
  6106. }
  6107. static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features);
  6108. static struct attribute *cgroup_sysfs_attrs[] = {
  6109. &cgroup_delegate_attr.attr,
  6110. &cgroup_features_attr.attr,
  6111. NULL,
  6112. };
  6113. static const struct attribute_group cgroup_sysfs_attr_group = {
  6114. .attrs = cgroup_sysfs_attrs,
  6115. .name = "cgroup",
  6116. };
  6117. static int __init cgroup_sysfs_init(void)
  6118. {
  6119. return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group);
  6120. }
  6121. subsys_initcall(cgroup_sysfs_init);
  6122. #endif /* CONFIG_SYSFS */