cgroup.c 177 KB

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