cgroup.c 176 KB

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