cgroup.c 156 KB

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