cgroup.c 150 KB

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