cgroup.c 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604
  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. #include <linux/cgroup.h>
  29. #include <linux/cred.h>
  30. #include <linux/ctype.h>
  31. #include <linux/errno.h>
  32. #include <linux/fs.h>
  33. #include <linux/init_task.h>
  34. #include <linux/kernel.h>
  35. #include <linux/list.h>
  36. #include <linux/mm.h>
  37. #include <linux/mutex.h>
  38. #include <linux/mount.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/rcupdate.h>
  42. #include <linux/sched.h>
  43. #include <linux/backing-dev.h>
  44. #include <linux/seq_file.h>
  45. #include <linux/slab.h>
  46. #include <linux/magic.h>
  47. #include <linux/spinlock.h>
  48. #include <linux/string.h>
  49. #include <linux/sort.h>
  50. #include <linux/kmod.h>
  51. #include <linux/module.h>
  52. #include <linux/delayacct.h>
  53. #include <linux/cgroupstats.h>
  54. #include <linux/hash.h>
  55. #include <linux/namei.h>
  56. #include <linux/pid_namespace.h>
  57. #include <linux/idr.h>
  58. #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
  59. #include <linux/eventfd.h>
  60. #include <linux/poll.h>
  61. #include <linux/flex_array.h> /* used in cgroup_attach_proc */
  62. #include <linux/kthread.h>
  63. #include <linux/atomic.h>
  64. /* css deactivation bias, makes css->refcnt negative to deny new trygets */
  65. #define CSS_DEACT_BIAS INT_MIN
  66. /*
  67. * cgroup_mutex is the master lock. Any modification to cgroup or its
  68. * hierarchy must be performed while holding it.
  69. *
  70. * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
  71. * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
  72. * release_agent_path and so on. Modifying requires both cgroup_mutex and
  73. * cgroup_root_mutex. Readers can acquire either of the two. This is to
  74. * break the following locking order cycle.
  75. *
  76. * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
  77. * B. namespace_sem -> cgroup_mutex
  78. *
  79. * B happens only through cgroup_show_options() and using cgroup_root_mutex
  80. * breaks it.
  81. */
  82. static DEFINE_MUTEX(cgroup_mutex);
  83. static DEFINE_MUTEX(cgroup_root_mutex);
  84. /*
  85. * Generate an array of cgroup subsystem pointers. At boot time, this is
  86. * populated with the built in subsystems, and modular subsystems are
  87. * registered after that. The mutable section of this array is protected by
  88. * cgroup_mutex.
  89. */
  90. #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
  91. #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
  92. static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
  93. #include <linux/cgroup_subsys.h>
  94. };
  95. #define MAX_CGROUP_ROOT_NAMELEN 64
  96. /*
  97. * A cgroupfs_root represents the root of a cgroup hierarchy,
  98. * and may be associated with a superblock to form an active
  99. * hierarchy
  100. */
  101. struct cgroupfs_root {
  102. struct super_block *sb;
  103. /*
  104. * The bitmask of subsystems intended to be attached to this
  105. * hierarchy
  106. */
  107. unsigned long subsys_mask;
  108. /* Unique id for this hierarchy. */
  109. int hierarchy_id;
  110. /* The bitmask of subsystems currently attached to this hierarchy */
  111. unsigned long actual_subsys_mask;
  112. /* A list running through the attached subsystems */
  113. struct list_head subsys_list;
  114. /* The root cgroup for this hierarchy */
  115. struct cgroup top_cgroup;
  116. /* Tracks how many cgroups are currently defined in hierarchy.*/
  117. int number_of_cgroups;
  118. /* A list running through the active hierarchies */
  119. struct list_head root_list;
  120. /* All cgroups on this root, cgroup_mutex protected */
  121. struct list_head allcg_list;
  122. /* Hierarchy-specific flags */
  123. unsigned long flags;
  124. /* IDs for cgroups in this hierarchy */
  125. struct ida cgroup_ida;
  126. /* The path to use for release notifications. */
  127. char release_agent_path[PATH_MAX];
  128. /* The name for this hierarchy - may be empty */
  129. char name[MAX_CGROUP_ROOT_NAMELEN];
  130. };
  131. /*
  132. * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
  133. * subsystems that are otherwise unattached - it never has more than a
  134. * single cgroup, and all tasks are part of that cgroup.
  135. */
  136. static struct cgroupfs_root rootnode;
  137. /*
  138. * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
  139. */
  140. struct cfent {
  141. struct list_head node;
  142. struct dentry *dentry;
  143. struct cftype *type;
  144. };
  145. /*
  146. * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
  147. * cgroup_subsys->use_id != 0.
  148. */
  149. #define CSS_ID_MAX (65535)
  150. struct css_id {
  151. /*
  152. * The css to which this ID points. This pointer is set to valid value
  153. * after cgroup is populated. If cgroup is removed, this will be NULL.
  154. * This pointer is expected to be RCU-safe because destroy()
  155. * is called after synchronize_rcu(). But for safe use, css_tryget()
  156. * should be used for avoiding race.
  157. */
  158. struct cgroup_subsys_state __rcu *css;
  159. /*
  160. * ID of this css.
  161. */
  162. unsigned short id;
  163. /*
  164. * Depth in hierarchy which this ID belongs to.
  165. */
  166. unsigned short depth;
  167. /*
  168. * ID is freed by RCU. (and lookup routine is RCU safe.)
  169. */
  170. struct rcu_head rcu_head;
  171. /*
  172. * Hierarchy of CSS ID belongs to.
  173. */
  174. unsigned short stack[0]; /* Array of Length (depth+1) */
  175. };
  176. /*
  177. * cgroup_event represents events which userspace want to receive.
  178. */
  179. struct cgroup_event {
  180. /*
  181. * Cgroup which the event belongs to.
  182. */
  183. struct cgroup *cgrp;
  184. /*
  185. * Control file which the event associated.
  186. */
  187. struct cftype *cft;
  188. /*
  189. * eventfd to signal userspace about the event.
  190. */
  191. struct eventfd_ctx *eventfd;
  192. /*
  193. * Each of these stored in a list by the cgroup.
  194. */
  195. struct list_head list;
  196. /*
  197. * All fields below needed to unregister event when
  198. * userspace closes eventfd.
  199. */
  200. poll_table pt;
  201. wait_queue_head_t *wqh;
  202. wait_queue_t wait;
  203. struct work_struct remove;
  204. };
  205. /* The list of hierarchy roots */
  206. static LIST_HEAD(roots);
  207. static int root_count;
  208. static DEFINE_IDA(hierarchy_ida);
  209. static int next_hierarchy_id;
  210. static DEFINE_SPINLOCK(hierarchy_id_lock);
  211. /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
  212. #define dummytop (&rootnode.top_cgroup)
  213. /* This flag indicates whether tasks in the fork and exit paths should
  214. * check for fork/exit handlers to call. This avoids us having to do
  215. * extra work in the fork/exit path if none of the subsystems need to
  216. * be called.
  217. */
  218. static int need_forkexit_callback __read_mostly;
  219. static int cgroup_destroy_locked(struct cgroup *cgrp);
  220. static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
  221. struct cftype cfts[], bool is_add);
  222. #ifdef CONFIG_PROVE_LOCKING
  223. int cgroup_lock_is_held(void)
  224. {
  225. return lockdep_is_held(&cgroup_mutex);
  226. }
  227. #else /* #ifdef CONFIG_PROVE_LOCKING */
  228. int cgroup_lock_is_held(void)
  229. {
  230. return mutex_is_locked(&cgroup_mutex);
  231. }
  232. #endif /* #else #ifdef CONFIG_PROVE_LOCKING */
  233. EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
  234. static int css_unbias_refcnt(int refcnt)
  235. {
  236. return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
  237. }
  238. /* the current nr of refs, always >= 0 whether @css is deactivated or not */
  239. static int css_refcnt(struct cgroup_subsys_state *css)
  240. {
  241. int v = atomic_read(&css->refcnt);
  242. return css_unbias_refcnt(v);
  243. }
  244. /* convenient tests for these bits */
  245. inline int cgroup_is_removed(const struct cgroup *cgrp)
  246. {
  247. return test_bit(CGRP_REMOVED, &cgrp->flags);
  248. }
  249. /* bits in struct cgroupfs_root flags field */
  250. enum {
  251. ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
  252. ROOT_XATTR, /* supports extended attributes */
  253. };
  254. static int cgroup_is_releasable(const struct cgroup *cgrp)
  255. {
  256. const int bits =
  257. (1 << CGRP_RELEASABLE) |
  258. (1 << CGRP_NOTIFY_ON_RELEASE);
  259. return (cgrp->flags & bits) == bits;
  260. }
  261. static int notify_on_release(const struct cgroup *cgrp)
  262. {
  263. return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  264. }
  265. /*
  266. * for_each_subsys() allows you to iterate on each subsystem attached to
  267. * an active hierarchy
  268. */
  269. #define for_each_subsys(_root, _ss) \
  270. list_for_each_entry(_ss, &_root->subsys_list, sibling)
  271. /* for_each_active_root() allows you to iterate across the active hierarchies */
  272. #define for_each_active_root(_root) \
  273. list_for_each_entry(_root, &roots, root_list)
  274. static inline struct cgroup *__d_cgrp(struct dentry *dentry)
  275. {
  276. return dentry->d_fsdata;
  277. }
  278. static inline struct cfent *__d_cfe(struct dentry *dentry)
  279. {
  280. return dentry->d_fsdata;
  281. }
  282. static inline struct cftype *__d_cft(struct dentry *dentry)
  283. {
  284. return __d_cfe(dentry)->type;
  285. }
  286. /* the list of cgroups eligible for automatic release. Protected by
  287. * release_list_lock */
  288. static LIST_HEAD(release_list);
  289. static DEFINE_RAW_SPINLOCK(release_list_lock);
  290. static void cgroup_release_agent(struct work_struct *work);
  291. static DECLARE_WORK(release_agent_work, cgroup_release_agent);
  292. static void check_for_release(struct cgroup *cgrp);
  293. /* Link structure for associating css_set objects with cgroups */
  294. struct cg_cgroup_link {
  295. /*
  296. * List running through cg_cgroup_links associated with a
  297. * cgroup, anchored on cgroup->css_sets
  298. */
  299. struct list_head cgrp_link_list;
  300. struct cgroup *cgrp;
  301. /*
  302. * List running through cg_cgroup_links pointing at a
  303. * single css_set object, anchored on css_set->cg_links
  304. */
  305. struct list_head cg_link_list;
  306. struct css_set *cg;
  307. };
  308. /* The default css_set - used by init and its children prior to any
  309. * hierarchies being mounted. It contains a pointer to the root state
  310. * for each subsystem. Also used to anchor the list of css_sets. Not
  311. * reference-counted, to improve performance when child cgroups
  312. * haven't been created.
  313. */
  314. static struct css_set init_css_set;
  315. static struct cg_cgroup_link init_css_set_link;
  316. static int cgroup_init_idr(struct cgroup_subsys *ss,
  317. struct cgroup_subsys_state *css);
  318. /* css_set_lock protects the list of css_set objects, and the
  319. * chain of tasks off each css_set. Nests outside task->alloc_lock
  320. * due to cgroup_iter_start() */
  321. static DEFINE_RWLOCK(css_set_lock);
  322. static int css_set_count;
  323. /*
  324. * hash table for cgroup groups. This improves the performance to find
  325. * an existing css_set. This hash doesn't (currently) take into
  326. * account cgroups in empty hierarchies.
  327. */
  328. #define CSS_SET_HASH_BITS 7
  329. #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
  330. static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
  331. static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
  332. {
  333. int i;
  334. int index;
  335. unsigned long tmp = 0UL;
  336. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
  337. tmp += (unsigned long)css[i];
  338. tmp = (tmp >> 16) ^ tmp;
  339. index = hash_long(tmp, CSS_SET_HASH_BITS);
  340. return &css_set_table[index];
  341. }
  342. /* We don't maintain the lists running through each css_set to its
  343. * task until after the first call to cgroup_iter_start(). This
  344. * reduces the fork()/exit() overhead for people who have cgroups
  345. * compiled into their kernel but not actually in use */
  346. static int use_task_css_set_links __read_mostly;
  347. static void __put_css_set(struct css_set *cg, int taskexit)
  348. {
  349. struct cg_cgroup_link *link;
  350. struct cg_cgroup_link *saved_link;
  351. /*
  352. * Ensure that the refcount doesn't hit zero while any readers
  353. * can see it. Similar to atomic_dec_and_lock(), but for an
  354. * rwlock
  355. */
  356. if (atomic_add_unless(&cg->refcount, -1, 1))
  357. return;
  358. write_lock(&css_set_lock);
  359. if (!atomic_dec_and_test(&cg->refcount)) {
  360. write_unlock(&css_set_lock);
  361. return;
  362. }
  363. /* This css_set is dead. unlink it and release cgroup refcounts */
  364. hlist_del(&cg->hlist);
  365. css_set_count--;
  366. list_for_each_entry_safe(link, saved_link, &cg->cg_links,
  367. cg_link_list) {
  368. struct cgroup *cgrp = link->cgrp;
  369. list_del(&link->cg_link_list);
  370. list_del(&link->cgrp_link_list);
  371. if (atomic_dec_and_test(&cgrp->count) &&
  372. notify_on_release(cgrp)) {
  373. if (taskexit)
  374. set_bit(CGRP_RELEASABLE, &cgrp->flags);
  375. check_for_release(cgrp);
  376. }
  377. kfree(link);
  378. }
  379. write_unlock(&css_set_lock);
  380. kfree_rcu(cg, rcu_head);
  381. }
  382. /*
  383. * refcounted get/put for css_set objects
  384. */
  385. static inline void get_css_set(struct css_set *cg)
  386. {
  387. atomic_inc(&cg->refcount);
  388. }
  389. static inline void put_css_set(struct css_set *cg)
  390. {
  391. __put_css_set(cg, 0);
  392. }
  393. static inline void put_css_set_taskexit(struct css_set *cg)
  394. {
  395. __put_css_set(cg, 1);
  396. }
  397. /*
  398. * compare_css_sets - helper function for find_existing_css_set().
  399. * @cg: candidate css_set being tested
  400. * @old_cg: existing css_set for a task
  401. * @new_cgrp: cgroup that's being entered by the task
  402. * @template: desired set of css pointers in css_set (pre-calculated)
  403. *
  404. * Returns true if "cg" matches "old_cg" except for the hierarchy
  405. * which "new_cgrp" belongs to, for which it should match "new_cgrp".
  406. */
  407. static bool compare_css_sets(struct css_set *cg,
  408. struct css_set *old_cg,
  409. struct cgroup *new_cgrp,
  410. struct cgroup_subsys_state *template[])
  411. {
  412. struct list_head *l1, *l2;
  413. if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
  414. /* Not all subsystems matched */
  415. return false;
  416. }
  417. /*
  418. * Compare cgroup pointers in order to distinguish between
  419. * different cgroups in heirarchies with no subsystems. We
  420. * could get by with just this check alone (and skip the
  421. * memcmp above) but on most setups the memcmp check will
  422. * avoid the need for this more expensive check on almost all
  423. * candidates.
  424. */
  425. l1 = &cg->cg_links;
  426. l2 = &old_cg->cg_links;
  427. while (1) {
  428. struct cg_cgroup_link *cgl1, *cgl2;
  429. struct cgroup *cg1, *cg2;
  430. l1 = l1->next;
  431. l2 = l2->next;
  432. /* See if we reached the end - both lists are equal length. */
  433. if (l1 == &cg->cg_links) {
  434. BUG_ON(l2 != &old_cg->cg_links);
  435. break;
  436. } else {
  437. BUG_ON(l2 == &old_cg->cg_links);
  438. }
  439. /* Locate the cgroups associated with these links. */
  440. cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
  441. cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
  442. cg1 = cgl1->cgrp;
  443. cg2 = cgl2->cgrp;
  444. /* Hierarchies should be linked in the same order. */
  445. BUG_ON(cg1->root != cg2->root);
  446. /*
  447. * If this hierarchy is the hierarchy of the cgroup
  448. * that's changing, then we need to check that this
  449. * css_set points to the new cgroup; if it's any other
  450. * hierarchy, then this css_set should point to the
  451. * same cgroup as the old css_set.
  452. */
  453. if (cg1->root == new_cgrp->root) {
  454. if (cg1 != new_cgrp)
  455. return false;
  456. } else {
  457. if (cg1 != cg2)
  458. return false;
  459. }
  460. }
  461. return true;
  462. }
  463. /*
  464. * find_existing_css_set() is a helper for
  465. * find_css_set(), and checks to see whether an existing
  466. * css_set is suitable.
  467. *
  468. * oldcg: the cgroup group that we're using before the cgroup
  469. * transition
  470. *
  471. * cgrp: the cgroup that we're moving into
  472. *
  473. * template: location in which to build the desired set of subsystem
  474. * state objects for the new cgroup group
  475. */
  476. static struct css_set *find_existing_css_set(
  477. struct css_set *oldcg,
  478. struct cgroup *cgrp,
  479. struct cgroup_subsys_state *template[])
  480. {
  481. int i;
  482. struct cgroupfs_root *root = cgrp->root;
  483. struct hlist_head *hhead;
  484. struct hlist_node *node;
  485. struct css_set *cg;
  486. /*
  487. * Build the set of subsystem state objects that we want to see in the
  488. * new css_set. while subsystems can change globally, the entries here
  489. * won't change, so no need for locking.
  490. */
  491. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  492. if (root->subsys_mask & (1UL << i)) {
  493. /* Subsystem is in this hierarchy. So we want
  494. * the subsystem state from the new
  495. * cgroup */
  496. template[i] = cgrp->subsys[i];
  497. } else {
  498. /* Subsystem is not in this hierarchy, so we
  499. * don't want to change the subsystem state */
  500. template[i] = oldcg->subsys[i];
  501. }
  502. }
  503. hhead = css_set_hash(template);
  504. hlist_for_each_entry(cg, node, hhead, hlist) {
  505. if (!compare_css_sets(cg, oldcg, cgrp, template))
  506. continue;
  507. /* This css_set matches what we need */
  508. return cg;
  509. }
  510. /* No existing cgroup group matched */
  511. return NULL;
  512. }
  513. static void free_cg_links(struct list_head *tmp)
  514. {
  515. struct cg_cgroup_link *link;
  516. struct cg_cgroup_link *saved_link;
  517. list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
  518. list_del(&link->cgrp_link_list);
  519. kfree(link);
  520. }
  521. }
  522. /*
  523. * allocate_cg_links() allocates "count" cg_cgroup_link structures
  524. * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
  525. * success or a negative error
  526. */
  527. static int allocate_cg_links(int count, struct list_head *tmp)
  528. {
  529. struct cg_cgroup_link *link;
  530. int i;
  531. INIT_LIST_HEAD(tmp);
  532. for (i = 0; i < count; i++) {
  533. link = kmalloc(sizeof(*link), GFP_KERNEL);
  534. if (!link) {
  535. free_cg_links(tmp);
  536. return -ENOMEM;
  537. }
  538. list_add(&link->cgrp_link_list, tmp);
  539. }
  540. return 0;
  541. }
  542. /**
  543. * link_css_set - a helper function to link a css_set to a cgroup
  544. * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
  545. * @cg: the css_set to be linked
  546. * @cgrp: the destination cgroup
  547. */
  548. static void link_css_set(struct list_head *tmp_cg_links,
  549. struct css_set *cg, struct cgroup *cgrp)
  550. {
  551. struct cg_cgroup_link *link;
  552. BUG_ON(list_empty(tmp_cg_links));
  553. link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
  554. cgrp_link_list);
  555. link->cg = cg;
  556. link->cgrp = cgrp;
  557. atomic_inc(&cgrp->count);
  558. list_move(&link->cgrp_link_list, &cgrp->css_sets);
  559. /*
  560. * Always add links to the tail of the list so that the list
  561. * is sorted by order of hierarchy creation
  562. */
  563. list_add_tail(&link->cg_link_list, &cg->cg_links);
  564. }
  565. /*
  566. * find_css_set() takes an existing cgroup group and a
  567. * cgroup object, and returns a css_set object that's
  568. * equivalent to the old group, but with the given cgroup
  569. * substituted into the appropriate hierarchy. Must be called with
  570. * cgroup_mutex held
  571. */
  572. static struct css_set *find_css_set(
  573. struct css_set *oldcg, struct cgroup *cgrp)
  574. {
  575. struct css_set *res;
  576. struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
  577. struct list_head tmp_cg_links;
  578. struct hlist_head *hhead;
  579. struct cg_cgroup_link *link;
  580. /* First see if we already have a cgroup group that matches
  581. * the desired set */
  582. read_lock(&css_set_lock);
  583. res = find_existing_css_set(oldcg, cgrp, template);
  584. if (res)
  585. get_css_set(res);
  586. read_unlock(&css_set_lock);
  587. if (res)
  588. return res;
  589. res = kmalloc(sizeof(*res), GFP_KERNEL);
  590. if (!res)
  591. return NULL;
  592. /* Allocate all the cg_cgroup_link objects that we'll need */
  593. if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
  594. kfree(res);
  595. return NULL;
  596. }
  597. atomic_set(&res->refcount, 1);
  598. INIT_LIST_HEAD(&res->cg_links);
  599. INIT_LIST_HEAD(&res->tasks);
  600. INIT_HLIST_NODE(&res->hlist);
  601. /* Copy the set of subsystem state objects generated in
  602. * find_existing_css_set() */
  603. memcpy(res->subsys, template, sizeof(res->subsys));
  604. write_lock(&css_set_lock);
  605. /* Add reference counts and links from the new css_set. */
  606. list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
  607. struct cgroup *c = link->cgrp;
  608. if (c->root == cgrp->root)
  609. c = cgrp;
  610. link_css_set(&tmp_cg_links, res, c);
  611. }
  612. BUG_ON(!list_empty(&tmp_cg_links));
  613. css_set_count++;
  614. /* Add this cgroup group to the hash table */
  615. hhead = css_set_hash(res->subsys);
  616. hlist_add_head(&res->hlist, hhead);
  617. write_unlock(&css_set_lock);
  618. return res;
  619. }
  620. /*
  621. * Return the cgroup for "task" from the given hierarchy. Must be
  622. * called with cgroup_mutex held.
  623. */
  624. static struct cgroup *task_cgroup_from_root(struct task_struct *task,
  625. struct cgroupfs_root *root)
  626. {
  627. struct css_set *css;
  628. struct cgroup *res = NULL;
  629. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  630. read_lock(&css_set_lock);
  631. /*
  632. * No need to lock the task - since we hold cgroup_mutex the
  633. * task can't change groups, so the only thing that can happen
  634. * is that it exits and its css is set back to init_css_set.
  635. */
  636. css = task->cgroups;
  637. if (css == &init_css_set) {
  638. res = &root->top_cgroup;
  639. } else {
  640. struct cg_cgroup_link *link;
  641. list_for_each_entry(link, &css->cg_links, cg_link_list) {
  642. struct cgroup *c = link->cgrp;
  643. if (c->root == root) {
  644. res = c;
  645. break;
  646. }
  647. }
  648. }
  649. read_unlock(&css_set_lock);
  650. BUG_ON(!res);
  651. return res;
  652. }
  653. /*
  654. * There is one global cgroup mutex. We also require taking
  655. * task_lock() when dereferencing a task's cgroup subsys pointers.
  656. * See "The task_lock() exception", at the end of this comment.
  657. *
  658. * A task must hold cgroup_mutex to modify cgroups.
  659. *
  660. * Any task can increment and decrement the count field without lock.
  661. * So in general, code holding cgroup_mutex can't rely on the count
  662. * field not changing. However, if the count goes to zero, then only
  663. * cgroup_attach_task() can increment it again. Because a count of zero
  664. * means that no tasks are currently attached, therefore there is no
  665. * way a task attached to that cgroup can fork (the other way to
  666. * increment the count). So code holding cgroup_mutex can safely
  667. * assume that if the count is zero, it will stay zero. Similarly, if
  668. * a task holds cgroup_mutex on a cgroup with zero count, it
  669. * knows that the cgroup won't be removed, as cgroup_rmdir()
  670. * needs that mutex.
  671. *
  672. * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
  673. * (usually) take cgroup_mutex. These are the two most performance
  674. * critical pieces of code here. The exception occurs on cgroup_exit(),
  675. * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
  676. * is taken, and if the cgroup count is zero, a usermode call made
  677. * to the release agent with the name of the cgroup (path relative to
  678. * the root of cgroup file system) as the argument.
  679. *
  680. * A cgroup can only be deleted if both its 'count' of using tasks
  681. * is zero, and its list of 'children' cgroups is empty. Since all
  682. * tasks in the system use _some_ cgroup, and since there is always at
  683. * least one task in the system (init, pid == 1), therefore, top_cgroup
  684. * always has either children cgroups and/or using tasks. So we don't
  685. * need a special hack to ensure that top_cgroup cannot be deleted.
  686. *
  687. * The task_lock() exception
  688. *
  689. * The need for this exception arises from the action of
  690. * cgroup_attach_task(), which overwrites one task's cgroup pointer with
  691. * another. It does so using cgroup_mutex, however there are
  692. * several performance critical places that need to reference
  693. * task->cgroup without the expense of grabbing a system global
  694. * mutex. Therefore except as noted below, when dereferencing or, as
  695. * in cgroup_attach_task(), modifying a task's cgroup pointer we use
  696. * task_lock(), which acts on a spinlock (task->alloc_lock) already in
  697. * the task_struct routinely used for such matters.
  698. *
  699. * P.S. One more locking exception. RCU is used to guard the
  700. * update of a tasks cgroup pointer by cgroup_attach_task()
  701. */
  702. /**
  703. * cgroup_lock - lock out any changes to cgroup structures
  704. *
  705. */
  706. void cgroup_lock(void)
  707. {
  708. mutex_lock(&cgroup_mutex);
  709. }
  710. EXPORT_SYMBOL_GPL(cgroup_lock);
  711. /**
  712. * cgroup_unlock - release lock on cgroup changes
  713. *
  714. * Undo the lock taken in a previous cgroup_lock() call.
  715. */
  716. void cgroup_unlock(void)
  717. {
  718. mutex_unlock(&cgroup_mutex);
  719. }
  720. EXPORT_SYMBOL_GPL(cgroup_unlock);
  721. /*
  722. * A couple of forward declarations required, due to cyclic reference loop:
  723. * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
  724. * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
  725. * -> cgroup_mkdir.
  726. */
  727. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
  728. static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
  729. static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
  730. static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
  731. unsigned long subsys_mask);
  732. static const struct inode_operations cgroup_dir_inode_operations;
  733. static const struct file_operations proc_cgroupstats_operations;
  734. static struct backing_dev_info cgroup_backing_dev_info = {
  735. .name = "cgroup",
  736. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
  737. };
  738. static int alloc_css_id(struct cgroup_subsys *ss,
  739. struct cgroup *parent, struct cgroup *child);
  740. static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
  741. {
  742. struct inode *inode = new_inode(sb);
  743. if (inode) {
  744. inode->i_ino = get_next_ino();
  745. inode->i_mode = mode;
  746. inode->i_uid = current_fsuid();
  747. inode->i_gid = current_fsgid();
  748. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  749. inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
  750. }
  751. return inode;
  752. }
  753. static void cgroup_diput(struct dentry *dentry, struct inode *inode)
  754. {
  755. /* is dentry a directory ? if so, kfree() associated cgroup */
  756. if (S_ISDIR(inode->i_mode)) {
  757. struct cgroup *cgrp = dentry->d_fsdata;
  758. struct cgroup_subsys *ss;
  759. BUG_ON(!(cgroup_is_removed(cgrp)));
  760. /* It's possible for external users to be holding css
  761. * reference counts on a cgroup; css_put() needs to
  762. * be able to access the cgroup after decrementing
  763. * the reference count in order to know if it needs to
  764. * queue the cgroup to be handled by the release
  765. * agent */
  766. synchronize_rcu();
  767. mutex_lock(&cgroup_mutex);
  768. /*
  769. * Release the subsystem state objects.
  770. */
  771. for_each_subsys(cgrp->root, ss)
  772. ss->css_free(cgrp);
  773. cgrp->root->number_of_cgroups--;
  774. mutex_unlock(&cgroup_mutex);
  775. /*
  776. * Drop the active superblock reference that we took when we
  777. * created the cgroup
  778. */
  779. deactivate_super(cgrp->root->sb);
  780. /*
  781. * if we're getting rid of the cgroup, refcount should ensure
  782. * that there are no pidlists left.
  783. */
  784. BUG_ON(!list_empty(&cgrp->pidlists));
  785. simple_xattrs_free(&cgrp->xattrs);
  786. ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
  787. kfree_rcu(cgrp, rcu_head);
  788. } else {
  789. struct cfent *cfe = __d_cfe(dentry);
  790. struct cgroup *cgrp = dentry->d_parent->d_fsdata;
  791. struct cftype *cft = cfe->type;
  792. WARN_ONCE(!list_empty(&cfe->node) &&
  793. cgrp != &cgrp->root->top_cgroup,
  794. "cfe still linked for %s\n", cfe->type->name);
  795. kfree(cfe);
  796. simple_xattrs_free(&cft->xattrs);
  797. }
  798. iput(inode);
  799. }
  800. static int cgroup_delete(const struct dentry *d)
  801. {
  802. return 1;
  803. }
  804. static void remove_dir(struct dentry *d)
  805. {
  806. struct dentry *parent = dget(d->d_parent);
  807. d_delete(d);
  808. simple_rmdir(parent->d_inode, d);
  809. dput(parent);
  810. }
  811. static int cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
  812. {
  813. struct cfent *cfe;
  814. lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
  815. lockdep_assert_held(&cgroup_mutex);
  816. list_for_each_entry(cfe, &cgrp->files, node) {
  817. struct dentry *d = cfe->dentry;
  818. if (cft && cfe->type != cft)
  819. continue;
  820. dget(d);
  821. d_delete(d);
  822. simple_unlink(cgrp->dentry->d_inode, d);
  823. list_del_init(&cfe->node);
  824. dput(d);
  825. return 0;
  826. }
  827. return -ENOENT;
  828. }
  829. /**
  830. * cgroup_clear_directory - selective removal of base and subsystem files
  831. * @dir: directory containing the files
  832. * @base_files: true if the base files should be removed
  833. * @subsys_mask: mask of the subsystem ids whose files should be removed
  834. */
  835. static void cgroup_clear_directory(struct dentry *dir, bool base_files,
  836. unsigned long subsys_mask)
  837. {
  838. struct cgroup *cgrp = __d_cgrp(dir);
  839. struct cgroup_subsys *ss;
  840. for_each_subsys(cgrp->root, ss) {
  841. struct cftype_set *set;
  842. if (!test_bit(ss->subsys_id, &subsys_mask))
  843. continue;
  844. list_for_each_entry(set, &ss->cftsets, node)
  845. cgroup_addrm_files(cgrp, NULL, set->cfts, false);
  846. }
  847. if (base_files) {
  848. while (!list_empty(&cgrp->files))
  849. cgroup_rm_file(cgrp, NULL);
  850. }
  851. }
  852. /*
  853. * NOTE : the dentry must have been dget()'ed
  854. */
  855. static void cgroup_d_remove_dir(struct dentry *dentry)
  856. {
  857. struct dentry *parent;
  858. struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
  859. cgroup_clear_directory(dentry, true, root->subsys_mask);
  860. parent = dentry->d_parent;
  861. spin_lock(&parent->d_lock);
  862. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  863. list_del_init(&dentry->d_u.d_child);
  864. spin_unlock(&dentry->d_lock);
  865. spin_unlock(&parent->d_lock);
  866. remove_dir(dentry);
  867. }
  868. /*
  869. * Call with cgroup_mutex held. Drops reference counts on modules, including
  870. * any duplicate ones that parse_cgroupfs_options took. If this function
  871. * returns an error, no reference counts are touched.
  872. */
  873. static int rebind_subsystems(struct cgroupfs_root *root,
  874. unsigned long final_subsys_mask)
  875. {
  876. unsigned long added_mask, removed_mask;
  877. struct cgroup *cgrp = &root->top_cgroup;
  878. int i;
  879. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  880. BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
  881. removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
  882. added_mask = final_subsys_mask & ~root->actual_subsys_mask;
  883. /* Check that any added subsystems are currently free */
  884. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  885. unsigned long bit = 1UL << i;
  886. struct cgroup_subsys *ss = subsys[i];
  887. if (!(bit & added_mask))
  888. continue;
  889. /*
  890. * Nobody should tell us to do a subsys that doesn't exist:
  891. * parse_cgroupfs_options should catch that case and refcounts
  892. * ensure that subsystems won't disappear once selected.
  893. */
  894. BUG_ON(ss == NULL);
  895. if (ss->root != &rootnode) {
  896. /* Subsystem isn't free */
  897. return -EBUSY;
  898. }
  899. }
  900. /* Currently we don't handle adding/removing subsystems when
  901. * any child cgroups exist. This is theoretically supportable
  902. * but involves complex error handling, so it's being left until
  903. * later */
  904. if (root->number_of_cgroups > 1)
  905. return -EBUSY;
  906. /* Process each subsystem */
  907. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  908. struct cgroup_subsys *ss = subsys[i];
  909. unsigned long bit = 1UL << i;
  910. if (bit & added_mask) {
  911. /* We're binding this subsystem to this hierarchy */
  912. BUG_ON(ss == NULL);
  913. BUG_ON(cgrp->subsys[i]);
  914. BUG_ON(!dummytop->subsys[i]);
  915. BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
  916. cgrp->subsys[i] = dummytop->subsys[i];
  917. cgrp->subsys[i]->cgroup = cgrp;
  918. list_move(&ss->sibling, &root->subsys_list);
  919. ss->root = root;
  920. if (ss->bind)
  921. ss->bind(cgrp);
  922. /* refcount was already taken, and we're keeping it */
  923. } else if (bit & removed_mask) {
  924. /* We're removing this subsystem */
  925. BUG_ON(ss == NULL);
  926. BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
  927. BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
  928. if (ss->bind)
  929. ss->bind(dummytop);
  930. dummytop->subsys[i]->cgroup = dummytop;
  931. cgrp->subsys[i] = NULL;
  932. subsys[i]->root = &rootnode;
  933. list_move(&ss->sibling, &rootnode.subsys_list);
  934. /* subsystem is now free - drop reference on module */
  935. module_put(ss->module);
  936. } else if (bit & final_subsys_mask) {
  937. /* Subsystem state should already exist */
  938. BUG_ON(ss == NULL);
  939. BUG_ON(!cgrp->subsys[i]);
  940. /*
  941. * a refcount was taken, but we already had one, so
  942. * drop the extra reference.
  943. */
  944. module_put(ss->module);
  945. #ifdef CONFIG_MODULE_UNLOAD
  946. BUG_ON(ss->module && !module_refcount(ss->module));
  947. #endif
  948. } else {
  949. /* Subsystem state shouldn't exist */
  950. BUG_ON(cgrp->subsys[i]);
  951. }
  952. }
  953. root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
  954. synchronize_rcu();
  955. return 0;
  956. }
  957. static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
  958. {
  959. struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
  960. struct cgroup_subsys *ss;
  961. mutex_lock(&cgroup_root_mutex);
  962. for_each_subsys(root, ss)
  963. seq_printf(seq, ",%s", ss->name);
  964. if (test_bit(ROOT_NOPREFIX, &root->flags))
  965. seq_puts(seq, ",noprefix");
  966. if (test_bit(ROOT_XATTR, &root->flags))
  967. seq_puts(seq, ",xattr");
  968. if (strlen(root->release_agent_path))
  969. seq_printf(seq, ",release_agent=%s", root->release_agent_path);
  970. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
  971. seq_puts(seq, ",clone_children");
  972. if (strlen(root->name))
  973. seq_printf(seq, ",name=%s", root->name);
  974. mutex_unlock(&cgroup_root_mutex);
  975. return 0;
  976. }
  977. struct cgroup_sb_opts {
  978. unsigned long subsys_mask;
  979. unsigned long flags;
  980. char *release_agent;
  981. bool cpuset_clone_children;
  982. char *name;
  983. /* User explicitly requested empty subsystem */
  984. bool none;
  985. struct cgroupfs_root *new_root;
  986. };
  987. /*
  988. * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
  989. * with cgroup_mutex held to protect the subsys[] array. This function takes
  990. * refcounts on subsystems to be used, unless it returns error, in which case
  991. * no refcounts are taken.
  992. */
  993. static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
  994. {
  995. char *token, *o = data;
  996. bool all_ss = false, one_ss = false;
  997. unsigned long mask = (unsigned long)-1;
  998. int i;
  999. bool module_pin_failed = false;
  1000. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  1001. #ifdef CONFIG_CPUSETS
  1002. mask = ~(1UL << cpuset_subsys_id);
  1003. #endif
  1004. memset(opts, 0, sizeof(*opts));
  1005. while ((token = strsep(&o, ",")) != NULL) {
  1006. if (!*token)
  1007. return -EINVAL;
  1008. if (!strcmp(token, "none")) {
  1009. /* Explicitly have no subsystems */
  1010. opts->none = true;
  1011. continue;
  1012. }
  1013. if (!strcmp(token, "all")) {
  1014. /* Mutually exclusive option 'all' + subsystem name */
  1015. if (one_ss)
  1016. return -EINVAL;
  1017. all_ss = true;
  1018. continue;
  1019. }
  1020. if (!strcmp(token, "noprefix")) {
  1021. set_bit(ROOT_NOPREFIX, &opts->flags);
  1022. continue;
  1023. }
  1024. if (!strcmp(token, "clone_children")) {
  1025. opts->cpuset_clone_children = true;
  1026. continue;
  1027. }
  1028. if (!strcmp(token, "xattr")) {
  1029. set_bit(ROOT_XATTR, &opts->flags);
  1030. continue;
  1031. }
  1032. if (!strncmp(token, "release_agent=", 14)) {
  1033. /* Specifying two release agents is forbidden */
  1034. if (opts->release_agent)
  1035. return -EINVAL;
  1036. opts->release_agent =
  1037. kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
  1038. if (!opts->release_agent)
  1039. return -ENOMEM;
  1040. continue;
  1041. }
  1042. if (!strncmp(token, "name=", 5)) {
  1043. const char *name = token + 5;
  1044. /* Can't specify an empty name */
  1045. if (!strlen(name))
  1046. return -EINVAL;
  1047. /* Must match [\w.-]+ */
  1048. for (i = 0; i < strlen(name); i++) {
  1049. char c = name[i];
  1050. if (isalnum(c))
  1051. continue;
  1052. if ((c == '.') || (c == '-') || (c == '_'))
  1053. continue;
  1054. return -EINVAL;
  1055. }
  1056. /* Specifying two names is forbidden */
  1057. if (opts->name)
  1058. return -EINVAL;
  1059. opts->name = kstrndup(name,
  1060. MAX_CGROUP_ROOT_NAMELEN - 1,
  1061. GFP_KERNEL);
  1062. if (!opts->name)
  1063. return -ENOMEM;
  1064. continue;
  1065. }
  1066. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  1067. struct cgroup_subsys *ss = subsys[i];
  1068. if (ss == NULL)
  1069. continue;
  1070. if (strcmp(token, ss->name))
  1071. continue;
  1072. if (ss->disabled)
  1073. continue;
  1074. /* Mutually exclusive option 'all' + subsystem name */
  1075. if (all_ss)
  1076. return -EINVAL;
  1077. set_bit(i, &opts->subsys_mask);
  1078. one_ss = true;
  1079. break;
  1080. }
  1081. if (i == CGROUP_SUBSYS_COUNT)
  1082. return -ENOENT;
  1083. }
  1084. /*
  1085. * If the 'all' option was specified select all the subsystems,
  1086. * otherwise if 'none', 'name=' and a subsystem name options
  1087. * were not specified, let's default to 'all'
  1088. */
  1089. if (all_ss || (!one_ss && !opts->none && !opts->name)) {
  1090. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  1091. struct cgroup_subsys *ss = subsys[i];
  1092. if (ss == NULL)
  1093. continue;
  1094. if (ss->disabled)
  1095. continue;
  1096. set_bit(i, &opts->subsys_mask);
  1097. }
  1098. }
  1099. /* Consistency checks */
  1100. /*
  1101. * Option noprefix was introduced just for backward compatibility
  1102. * with the old cpuset, so we allow noprefix only if mounting just
  1103. * the cpuset subsystem.
  1104. */
  1105. if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
  1106. (opts->subsys_mask & mask))
  1107. return -EINVAL;
  1108. /* Can't specify "none" and some subsystems */
  1109. if (opts->subsys_mask && opts->none)
  1110. return -EINVAL;
  1111. /*
  1112. * We either have to specify by name or by subsystems. (So all
  1113. * empty hierarchies must have a name).
  1114. */
  1115. if (!opts->subsys_mask && !opts->name)
  1116. return -EINVAL;
  1117. /*
  1118. * Grab references on all the modules we'll need, so the subsystems
  1119. * don't dance around before rebind_subsystems attaches them. This may
  1120. * take duplicate reference counts on a subsystem that's already used,
  1121. * but rebind_subsystems handles this case.
  1122. */
  1123. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  1124. unsigned long bit = 1UL << i;
  1125. if (!(bit & opts->subsys_mask))
  1126. continue;
  1127. if (!try_module_get(subsys[i]->module)) {
  1128. module_pin_failed = true;
  1129. break;
  1130. }
  1131. }
  1132. if (module_pin_failed) {
  1133. /*
  1134. * oops, one of the modules was going away. this means that we
  1135. * raced with a module_delete call, and to the user this is
  1136. * essentially a "subsystem doesn't exist" case.
  1137. */
  1138. for (i--; i >= 0; i--) {
  1139. /* drop refcounts only on the ones we took */
  1140. unsigned long bit = 1UL << i;
  1141. if (!(bit & opts->subsys_mask))
  1142. continue;
  1143. module_put(subsys[i]->module);
  1144. }
  1145. return -ENOENT;
  1146. }
  1147. return 0;
  1148. }
  1149. static void drop_parsed_module_refcounts(unsigned long subsys_mask)
  1150. {
  1151. int i;
  1152. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  1153. unsigned long bit = 1UL << i;
  1154. if (!(bit & subsys_mask))
  1155. continue;
  1156. module_put(subsys[i]->module);
  1157. }
  1158. }
  1159. static int cgroup_remount(struct super_block *sb, int *flags, char *data)
  1160. {
  1161. int ret = 0;
  1162. struct cgroupfs_root *root = sb->s_fs_info;
  1163. struct cgroup *cgrp = &root->top_cgroup;
  1164. struct cgroup_sb_opts opts;
  1165. unsigned long added_mask, removed_mask;
  1166. mutex_lock(&cgrp->dentry->d_inode->i_mutex);
  1167. mutex_lock(&cgroup_mutex);
  1168. mutex_lock(&cgroup_root_mutex);
  1169. /* See what subsystems are wanted */
  1170. ret = parse_cgroupfs_options(data, &opts);
  1171. if (ret)
  1172. goto out_unlock;
  1173. /* See feature-removal-schedule.txt */
  1174. if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
  1175. pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
  1176. task_tgid_nr(current), current->comm);
  1177. added_mask = opts.subsys_mask & ~root->subsys_mask;
  1178. removed_mask = root->subsys_mask & ~opts.subsys_mask;
  1179. /* Don't allow flags or name to change at remount */
  1180. if (opts.flags != root->flags ||
  1181. (opts.name && strcmp(opts.name, root->name))) {
  1182. ret = -EINVAL;
  1183. drop_parsed_module_refcounts(opts.subsys_mask);
  1184. goto out_unlock;
  1185. }
  1186. /*
  1187. * Clear out the files of subsystems that should be removed, do
  1188. * this before rebind_subsystems, since rebind_subsystems may
  1189. * change this hierarchy's subsys_list.
  1190. */
  1191. cgroup_clear_directory(cgrp->dentry, false, removed_mask);
  1192. ret = rebind_subsystems(root, opts.subsys_mask);
  1193. if (ret) {
  1194. /* rebind_subsystems failed, re-populate the removed files */
  1195. cgroup_populate_dir(cgrp, false, removed_mask);
  1196. drop_parsed_module_refcounts(opts.subsys_mask);
  1197. goto out_unlock;
  1198. }
  1199. /* re-populate subsystem files */
  1200. cgroup_populate_dir(cgrp, false, added_mask);
  1201. if (opts.release_agent)
  1202. strcpy(root->release_agent_path, opts.release_agent);
  1203. out_unlock:
  1204. kfree(opts.release_agent);
  1205. kfree(opts.name);
  1206. mutex_unlock(&cgroup_root_mutex);
  1207. mutex_unlock(&cgroup_mutex);
  1208. mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
  1209. return ret;
  1210. }
  1211. static const struct super_operations cgroup_ops = {
  1212. .statfs = simple_statfs,
  1213. .drop_inode = generic_delete_inode,
  1214. .show_options = cgroup_show_options,
  1215. .remount_fs = cgroup_remount,
  1216. };
  1217. static void init_cgroup_housekeeping(struct cgroup *cgrp)
  1218. {
  1219. INIT_LIST_HEAD(&cgrp->sibling);
  1220. INIT_LIST_HEAD(&cgrp->children);
  1221. INIT_LIST_HEAD(&cgrp->files);
  1222. INIT_LIST_HEAD(&cgrp->css_sets);
  1223. INIT_LIST_HEAD(&cgrp->allcg_node);
  1224. INIT_LIST_HEAD(&cgrp->release_list);
  1225. INIT_LIST_HEAD(&cgrp->pidlists);
  1226. mutex_init(&cgrp->pidlist_mutex);
  1227. INIT_LIST_HEAD(&cgrp->event_list);
  1228. spin_lock_init(&cgrp->event_list_lock);
  1229. simple_xattrs_init(&cgrp->xattrs);
  1230. }
  1231. static void init_cgroup_root(struct cgroupfs_root *root)
  1232. {
  1233. struct cgroup *cgrp = &root->top_cgroup;
  1234. INIT_LIST_HEAD(&root->subsys_list);
  1235. INIT_LIST_HEAD(&root->root_list);
  1236. INIT_LIST_HEAD(&root->allcg_list);
  1237. root->number_of_cgroups = 1;
  1238. cgrp->root = root;
  1239. cgrp->top_cgroup = cgrp;
  1240. init_cgroup_housekeeping(cgrp);
  1241. list_add_tail(&cgrp->allcg_node, &root->allcg_list);
  1242. }
  1243. static bool init_root_id(struct cgroupfs_root *root)
  1244. {
  1245. int ret = 0;
  1246. do {
  1247. if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
  1248. return false;
  1249. spin_lock(&hierarchy_id_lock);
  1250. /* Try to allocate the next unused ID */
  1251. ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
  1252. &root->hierarchy_id);
  1253. if (ret == -ENOSPC)
  1254. /* Try again starting from 0 */
  1255. ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
  1256. if (!ret) {
  1257. next_hierarchy_id = root->hierarchy_id + 1;
  1258. } else if (ret != -EAGAIN) {
  1259. /* Can only get here if the 31-bit IDR is full ... */
  1260. BUG_ON(ret);
  1261. }
  1262. spin_unlock(&hierarchy_id_lock);
  1263. } while (ret);
  1264. return true;
  1265. }
  1266. static int cgroup_test_super(struct super_block *sb, void *data)
  1267. {
  1268. struct cgroup_sb_opts *opts = data;
  1269. struct cgroupfs_root *root = sb->s_fs_info;
  1270. /* If we asked for a name then it must match */
  1271. if (opts->name && strcmp(opts->name, root->name))
  1272. return 0;
  1273. /*
  1274. * If we asked for subsystems (or explicitly for no
  1275. * subsystems) then they must match
  1276. */
  1277. if ((opts->subsys_mask || opts->none)
  1278. && (opts->subsys_mask != root->subsys_mask))
  1279. return 0;
  1280. return 1;
  1281. }
  1282. static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
  1283. {
  1284. struct cgroupfs_root *root;
  1285. if (!opts->subsys_mask && !opts->none)
  1286. return NULL;
  1287. root = kzalloc(sizeof(*root), GFP_KERNEL);
  1288. if (!root)
  1289. return ERR_PTR(-ENOMEM);
  1290. if (!init_root_id(root)) {
  1291. kfree(root);
  1292. return ERR_PTR(-ENOMEM);
  1293. }
  1294. init_cgroup_root(root);
  1295. root->subsys_mask = opts->subsys_mask;
  1296. root->flags = opts->flags;
  1297. ida_init(&root->cgroup_ida);
  1298. if (opts->release_agent)
  1299. strcpy(root->release_agent_path, opts->release_agent);
  1300. if (opts->name)
  1301. strcpy(root->name, opts->name);
  1302. if (opts->cpuset_clone_children)
  1303. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
  1304. return root;
  1305. }
  1306. static void cgroup_drop_root(struct cgroupfs_root *root)
  1307. {
  1308. if (!root)
  1309. return;
  1310. BUG_ON(!root->hierarchy_id);
  1311. spin_lock(&hierarchy_id_lock);
  1312. ida_remove(&hierarchy_ida, root->hierarchy_id);
  1313. spin_unlock(&hierarchy_id_lock);
  1314. ida_destroy(&root->cgroup_ida);
  1315. kfree(root);
  1316. }
  1317. static int cgroup_set_super(struct super_block *sb, void *data)
  1318. {
  1319. int ret;
  1320. struct cgroup_sb_opts *opts = data;
  1321. /* If we don't have a new root, we can't set up a new sb */
  1322. if (!opts->new_root)
  1323. return -EINVAL;
  1324. BUG_ON(!opts->subsys_mask && !opts->none);
  1325. ret = set_anon_super(sb, NULL);
  1326. if (ret)
  1327. return ret;
  1328. sb->s_fs_info = opts->new_root;
  1329. opts->new_root->sb = sb;
  1330. sb->s_blocksize = PAGE_CACHE_SIZE;
  1331. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1332. sb->s_magic = CGROUP_SUPER_MAGIC;
  1333. sb->s_op = &cgroup_ops;
  1334. return 0;
  1335. }
  1336. static int cgroup_get_rootdir(struct super_block *sb)
  1337. {
  1338. static const struct dentry_operations cgroup_dops = {
  1339. .d_iput = cgroup_diput,
  1340. .d_delete = cgroup_delete,
  1341. };
  1342. struct inode *inode =
  1343. cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
  1344. if (!inode)
  1345. return -ENOMEM;
  1346. inode->i_fop = &simple_dir_operations;
  1347. inode->i_op = &cgroup_dir_inode_operations;
  1348. /* directories start off with i_nlink == 2 (for "." entry) */
  1349. inc_nlink(inode);
  1350. sb->s_root = d_make_root(inode);
  1351. if (!sb->s_root)
  1352. return -ENOMEM;
  1353. /* for everything else we want ->d_op set */
  1354. sb->s_d_op = &cgroup_dops;
  1355. return 0;
  1356. }
  1357. static struct dentry *cgroup_mount(struct file_system_type *fs_type,
  1358. int flags, const char *unused_dev_name,
  1359. void *data)
  1360. {
  1361. struct cgroup_sb_opts opts;
  1362. struct cgroupfs_root *root;
  1363. int ret = 0;
  1364. struct super_block *sb;
  1365. struct cgroupfs_root *new_root;
  1366. struct inode *inode;
  1367. /* First find the desired set of subsystems */
  1368. mutex_lock(&cgroup_mutex);
  1369. ret = parse_cgroupfs_options(data, &opts);
  1370. mutex_unlock(&cgroup_mutex);
  1371. if (ret)
  1372. goto out_err;
  1373. /*
  1374. * Allocate a new cgroup root. We may not need it if we're
  1375. * reusing an existing hierarchy.
  1376. */
  1377. new_root = cgroup_root_from_opts(&opts);
  1378. if (IS_ERR(new_root)) {
  1379. ret = PTR_ERR(new_root);
  1380. goto drop_modules;
  1381. }
  1382. opts.new_root = new_root;
  1383. /* Locate an existing or new sb for this hierarchy */
  1384. sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
  1385. if (IS_ERR(sb)) {
  1386. ret = PTR_ERR(sb);
  1387. cgroup_drop_root(opts.new_root);
  1388. goto drop_modules;
  1389. }
  1390. root = sb->s_fs_info;
  1391. BUG_ON(!root);
  1392. if (root == opts.new_root) {
  1393. /* We used the new root structure, so this is a new hierarchy */
  1394. struct list_head tmp_cg_links;
  1395. struct cgroup *root_cgrp = &root->top_cgroup;
  1396. struct cgroupfs_root *existing_root;
  1397. const struct cred *cred;
  1398. int i;
  1399. BUG_ON(sb->s_root != NULL);
  1400. ret = cgroup_get_rootdir(sb);
  1401. if (ret)
  1402. goto drop_new_super;
  1403. inode = sb->s_root->d_inode;
  1404. mutex_lock(&inode->i_mutex);
  1405. mutex_lock(&cgroup_mutex);
  1406. mutex_lock(&cgroup_root_mutex);
  1407. /* Check for name clashes with existing mounts */
  1408. ret = -EBUSY;
  1409. if (strlen(root->name))
  1410. for_each_active_root(existing_root)
  1411. if (!strcmp(existing_root->name, root->name))
  1412. goto unlock_drop;
  1413. /*
  1414. * We're accessing css_set_count without locking
  1415. * css_set_lock here, but that's OK - it can only be
  1416. * increased by someone holding cgroup_lock, and
  1417. * that's us. The worst that can happen is that we
  1418. * have some link structures left over
  1419. */
  1420. ret = allocate_cg_links(css_set_count, &tmp_cg_links);
  1421. if (ret)
  1422. goto unlock_drop;
  1423. ret = rebind_subsystems(root, root->subsys_mask);
  1424. if (ret == -EBUSY) {
  1425. free_cg_links(&tmp_cg_links);
  1426. goto unlock_drop;
  1427. }
  1428. /*
  1429. * There must be no failure case after here, since rebinding
  1430. * takes care of subsystems' refcounts, which are explicitly
  1431. * dropped in the failure exit path.
  1432. */
  1433. /* EBUSY should be the only error here */
  1434. BUG_ON(ret);
  1435. list_add(&root->root_list, &roots);
  1436. root_count++;
  1437. sb->s_root->d_fsdata = root_cgrp;
  1438. root->top_cgroup.dentry = sb->s_root;
  1439. /* Link the top cgroup in this hierarchy into all
  1440. * the css_set objects */
  1441. write_lock(&css_set_lock);
  1442. for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
  1443. struct hlist_head *hhead = &css_set_table[i];
  1444. struct hlist_node *node;
  1445. struct css_set *cg;
  1446. hlist_for_each_entry(cg, node, hhead, hlist)
  1447. link_css_set(&tmp_cg_links, cg, root_cgrp);
  1448. }
  1449. write_unlock(&css_set_lock);
  1450. free_cg_links(&tmp_cg_links);
  1451. BUG_ON(!list_empty(&root_cgrp->children));
  1452. BUG_ON(root->number_of_cgroups != 1);
  1453. cred = override_creds(&init_cred);
  1454. cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
  1455. revert_creds(cred);
  1456. mutex_unlock(&cgroup_root_mutex);
  1457. mutex_unlock(&cgroup_mutex);
  1458. mutex_unlock(&inode->i_mutex);
  1459. } else {
  1460. /*
  1461. * We re-used an existing hierarchy - the new root (if
  1462. * any) is not needed
  1463. */
  1464. cgroup_drop_root(opts.new_root);
  1465. /* no subsys rebinding, so refcounts don't change */
  1466. drop_parsed_module_refcounts(opts.subsys_mask);
  1467. }
  1468. kfree(opts.release_agent);
  1469. kfree(opts.name);
  1470. return dget(sb->s_root);
  1471. unlock_drop:
  1472. mutex_unlock(&cgroup_root_mutex);
  1473. mutex_unlock(&cgroup_mutex);
  1474. mutex_unlock(&inode->i_mutex);
  1475. drop_new_super:
  1476. deactivate_locked_super(sb);
  1477. drop_modules:
  1478. drop_parsed_module_refcounts(opts.subsys_mask);
  1479. out_err:
  1480. kfree(opts.release_agent);
  1481. kfree(opts.name);
  1482. return ERR_PTR(ret);
  1483. }
  1484. static void cgroup_kill_sb(struct super_block *sb) {
  1485. struct cgroupfs_root *root = sb->s_fs_info;
  1486. struct cgroup *cgrp = &root->top_cgroup;
  1487. int ret;
  1488. struct cg_cgroup_link *link;
  1489. struct cg_cgroup_link *saved_link;
  1490. BUG_ON(!root);
  1491. BUG_ON(root->number_of_cgroups != 1);
  1492. BUG_ON(!list_empty(&cgrp->children));
  1493. mutex_lock(&cgroup_mutex);
  1494. mutex_lock(&cgroup_root_mutex);
  1495. /* Rebind all subsystems back to the default hierarchy */
  1496. ret = rebind_subsystems(root, 0);
  1497. /* Shouldn't be able to fail ... */
  1498. BUG_ON(ret);
  1499. /*
  1500. * Release all the links from css_sets to this hierarchy's
  1501. * root cgroup
  1502. */
  1503. write_lock(&css_set_lock);
  1504. list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
  1505. cgrp_link_list) {
  1506. list_del(&link->cg_link_list);
  1507. list_del(&link->cgrp_link_list);
  1508. kfree(link);
  1509. }
  1510. write_unlock(&css_set_lock);
  1511. if (!list_empty(&root->root_list)) {
  1512. list_del(&root->root_list);
  1513. root_count--;
  1514. }
  1515. mutex_unlock(&cgroup_root_mutex);
  1516. mutex_unlock(&cgroup_mutex);
  1517. simple_xattrs_free(&cgrp->xattrs);
  1518. kill_litter_super(sb);
  1519. cgroup_drop_root(root);
  1520. }
  1521. static struct file_system_type cgroup_fs_type = {
  1522. .name = "cgroup",
  1523. .mount = cgroup_mount,
  1524. .kill_sb = cgroup_kill_sb,
  1525. };
  1526. static struct kobject *cgroup_kobj;
  1527. /**
  1528. * cgroup_path - generate the path of a cgroup
  1529. * @cgrp: the cgroup in question
  1530. * @buf: the buffer to write the path into
  1531. * @buflen: the length of the buffer
  1532. *
  1533. * Called with cgroup_mutex held or else with an RCU-protected cgroup
  1534. * reference. Writes path of cgroup into buf. Returns 0 on success,
  1535. * -errno on error.
  1536. */
  1537. int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
  1538. {
  1539. struct dentry *dentry = cgrp->dentry;
  1540. char *start;
  1541. rcu_lockdep_assert(rcu_read_lock_held() || cgroup_lock_is_held(),
  1542. "cgroup_path() called without proper locking");
  1543. if (!dentry || cgrp == dummytop) {
  1544. /*
  1545. * Inactive subsystems have no dentry for their root
  1546. * cgroup
  1547. */
  1548. strcpy(buf, "/");
  1549. return 0;
  1550. }
  1551. start = buf + buflen - 1;
  1552. *start = '\0';
  1553. for (;;) {
  1554. int len = dentry->d_name.len;
  1555. if ((start -= len) < buf)
  1556. return -ENAMETOOLONG;
  1557. memcpy(start, dentry->d_name.name, len);
  1558. cgrp = cgrp->parent;
  1559. if (!cgrp)
  1560. break;
  1561. dentry = cgrp->dentry;
  1562. if (!cgrp->parent)
  1563. continue;
  1564. if (--start < buf)
  1565. return -ENAMETOOLONG;
  1566. *start = '/';
  1567. }
  1568. memmove(buf, start, buf + buflen - start);
  1569. return 0;
  1570. }
  1571. EXPORT_SYMBOL_GPL(cgroup_path);
  1572. /*
  1573. * Control Group taskset
  1574. */
  1575. struct task_and_cgroup {
  1576. struct task_struct *task;
  1577. struct cgroup *cgrp;
  1578. struct css_set *cg;
  1579. };
  1580. struct cgroup_taskset {
  1581. struct task_and_cgroup single;
  1582. struct flex_array *tc_array;
  1583. int tc_array_len;
  1584. int idx;
  1585. struct cgroup *cur_cgrp;
  1586. };
  1587. /**
  1588. * cgroup_taskset_first - reset taskset and return the first task
  1589. * @tset: taskset of interest
  1590. *
  1591. * @tset iteration is initialized and the first task is returned.
  1592. */
  1593. struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
  1594. {
  1595. if (tset->tc_array) {
  1596. tset->idx = 0;
  1597. return cgroup_taskset_next(tset);
  1598. } else {
  1599. tset->cur_cgrp = tset->single.cgrp;
  1600. return tset->single.task;
  1601. }
  1602. }
  1603. EXPORT_SYMBOL_GPL(cgroup_taskset_first);
  1604. /**
  1605. * cgroup_taskset_next - iterate to the next task in taskset
  1606. * @tset: taskset of interest
  1607. *
  1608. * Return the next task in @tset. Iteration must have been initialized
  1609. * with cgroup_taskset_first().
  1610. */
  1611. struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
  1612. {
  1613. struct task_and_cgroup *tc;
  1614. if (!tset->tc_array || tset->idx >= tset->tc_array_len)
  1615. return NULL;
  1616. tc = flex_array_get(tset->tc_array, tset->idx++);
  1617. tset->cur_cgrp = tc->cgrp;
  1618. return tc->task;
  1619. }
  1620. EXPORT_SYMBOL_GPL(cgroup_taskset_next);
  1621. /**
  1622. * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
  1623. * @tset: taskset of interest
  1624. *
  1625. * Return the cgroup for the current (last returned) task of @tset. This
  1626. * function must be preceded by either cgroup_taskset_first() or
  1627. * cgroup_taskset_next().
  1628. */
  1629. struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
  1630. {
  1631. return tset->cur_cgrp;
  1632. }
  1633. EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
  1634. /**
  1635. * cgroup_taskset_size - return the number of tasks in taskset
  1636. * @tset: taskset of interest
  1637. */
  1638. int cgroup_taskset_size(struct cgroup_taskset *tset)
  1639. {
  1640. return tset->tc_array ? tset->tc_array_len : 1;
  1641. }
  1642. EXPORT_SYMBOL_GPL(cgroup_taskset_size);
  1643. /*
  1644. * cgroup_task_migrate - move a task from one cgroup to another.
  1645. *
  1646. * Must be called with cgroup_mutex and threadgroup locked.
  1647. */
  1648. static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
  1649. struct task_struct *tsk, struct css_set *newcg)
  1650. {
  1651. struct css_set *oldcg;
  1652. /*
  1653. * We are synchronized through threadgroup_lock() against PF_EXITING
  1654. * setting such that we can't race against cgroup_exit() changing the
  1655. * css_set to init_css_set and dropping the old one.
  1656. */
  1657. WARN_ON_ONCE(tsk->flags & PF_EXITING);
  1658. oldcg = tsk->cgroups;
  1659. task_lock(tsk);
  1660. rcu_assign_pointer(tsk->cgroups, newcg);
  1661. task_unlock(tsk);
  1662. /* Update the css_set linked lists if we're using them */
  1663. write_lock(&css_set_lock);
  1664. if (!list_empty(&tsk->cg_list))
  1665. list_move(&tsk->cg_list, &newcg->tasks);
  1666. write_unlock(&css_set_lock);
  1667. /*
  1668. * We just gained a reference on oldcg by taking it from the task. As
  1669. * trading it for newcg is protected by cgroup_mutex, we're safe to drop
  1670. * it here; it will be freed under RCU.
  1671. */
  1672. set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
  1673. put_css_set(oldcg);
  1674. }
  1675. /**
  1676. * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
  1677. * @cgrp: the cgroup the task is attaching to
  1678. * @tsk: the task to be attached
  1679. *
  1680. * Call with cgroup_mutex and threadgroup locked. May take task_lock of
  1681. * @tsk during call.
  1682. */
  1683. int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
  1684. {
  1685. int retval = 0;
  1686. struct cgroup_subsys *ss, *failed_ss = NULL;
  1687. struct cgroup *oldcgrp;
  1688. struct cgroupfs_root *root = cgrp->root;
  1689. struct cgroup_taskset tset = { };
  1690. struct css_set *newcg;
  1691. /* @tsk either already exited or can't exit until the end */
  1692. if (tsk->flags & PF_EXITING)
  1693. return -ESRCH;
  1694. /* Nothing to do if the task is already in that cgroup */
  1695. oldcgrp = task_cgroup_from_root(tsk, root);
  1696. if (cgrp == oldcgrp)
  1697. return 0;
  1698. tset.single.task = tsk;
  1699. tset.single.cgrp = oldcgrp;
  1700. for_each_subsys(root, ss) {
  1701. if (ss->can_attach) {
  1702. retval = ss->can_attach(cgrp, &tset);
  1703. if (retval) {
  1704. /*
  1705. * Remember on which subsystem the can_attach()
  1706. * failed, so that we only call cancel_attach()
  1707. * against the subsystems whose can_attach()
  1708. * succeeded. (See below)
  1709. */
  1710. failed_ss = ss;
  1711. goto out;
  1712. }
  1713. }
  1714. }
  1715. newcg = find_css_set(tsk->cgroups, cgrp);
  1716. if (!newcg) {
  1717. retval = -ENOMEM;
  1718. goto out;
  1719. }
  1720. cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
  1721. for_each_subsys(root, ss) {
  1722. if (ss->attach)
  1723. ss->attach(cgrp, &tset);
  1724. }
  1725. synchronize_rcu();
  1726. out:
  1727. if (retval) {
  1728. for_each_subsys(root, ss) {
  1729. if (ss == failed_ss)
  1730. /*
  1731. * This subsystem was the one that failed the
  1732. * can_attach() check earlier, so we don't need
  1733. * to call cancel_attach() against it or any
  1734. * remaining subsystems.
  1735. */
  1736. break;
  1737. if (ss->cancel_attach)
  1738. ss->cancel_attach(cgrp, &tset);
  1739. }
  1740. }
  1741. return retval;
  1742. }
  1743. /**
  1744. * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
  1745. * @from: attach to all cgroups of a given task
  1746. * @tsk: the task to be attached
  1747. */
  1748. int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
  1749. {
  1750. struct cgroupfs_root *root;
  1751. int retval = 0;
  1752. cgroup_lock();
  1753. for_each_active_root(root) {
  1754. struct cgroup *from_cg = task_cgroup_from_root(from, root);
  1755. retval = cgroup_attach_task(from_cg, tsk);
  1756. if (retval)
  1757. break;
  1758. }
  1759. cgroup_unlock();
  1760. return retval;
  1761. }
  1762. EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
  1763. /**
  1764. * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
  1765. * @cgrp: the cgroup to attach to
  1766. * @leader: the threadgroup leader task_struct of the group to be attached
  1767. *
  1768. * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
  1769. * task_lock of each thread in leader's threadgroup individually in turn.
  1770. */
  1771. static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
  1772. {
  1773. int retval, i, group_size;
  1774. struct cgroup_subsys *ss, *failed_ss = NULL;
  1775. /* guaranteed to be initialized later, but the compiler needs this */
  1776. struct cgroupfs_root *root = cgrp->root;
  1777. /* threadgroup list cursor and array */
  1778. struct task_struct *tsk;
  1779. struct task_and_cgroup *tc;
  1780. struct flex_array *group;
  1781. struct cgroup_taskset tset = { };
  1782. /*
  1783. * step 0: in order to do expensive, possibly blocking operations for
  1784. * every thread, we cannot iterate the thread group list, since it needs
  1785. * rcu or tasklist locked. instead, build an array of all threads in the
  1786. * group - group_rwsem prevents new threads from appearing, and if
  1787. * threads exit, this will just be an over-estimate.
  1788. */
  1789. group_size = get_nr_threads(leader);
  1790. /* flex_array supports very large thread-groups better than kmalloc. */
  1791. group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
  1792. if (!group)
  1793. return -ENOMEM;
  1794. /* pre-allocate to guarantee space while iterating in rcu read-side. */
  1795. retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
  1796. if (retval)
  1797. goto out_free_group_list;
  1798. tsk = leader;
  1799. i = 0;
  1800. /*
  1801. * Prevent freeing of tasks while we take a snapshot. Tasks that are
  1802. * already PF_EXITING could be freed from underneath us unless we
  1803. * take an rcu_read_lock.
  1804. */
  1805. rcu_read_lock();
  1806. do {
  1807. struct task_and_cgroup ent;
  1808. /* @tsk either already exited or can't exit until the end */
  1809. if (tsk->flags & PF_EXITING)
  1810. continue;
  1811. /* as per above, nr_threads may decrease, but not increase. */
  1812. BUG_ON(i >= group_size);
  1813. ent.task = tsk;
  1814. ent.cgrp = task_cgroup_from_root(tsk, root);
  1815. /* nothing to do if this task is already in the cgroup */
  1816. if (ent.cgrp == cgrp)
  1817. continue;
  1818. /*
  1819. * saying GFP_ATOMIC has no effect here because we did prealloc
  1820. * earlier, but it's good form to communicate our expectations.
  1821. */
  1822. retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
  1823. BUG_ON(retval != 0);
  1824. i++;
  1825. } while_each_thread(leader, tsk);
  1826. rcu_read_unlock();
  1827. /* remember the number of threads in the array for later. */
  1828. group_size = i;
  1829. tset.tc_array = group;
  1830. tset.tc_array_len = group_size;
  1831. /* methods shouldn't be called if no task is actually migrating */
  1832. retval = 0;
  1833. if (!group_size)
  1834. goto out_free_group_list;
  1835. /*
  1836. * step 1: check that we can legitimately attach to the cgroup.
  1837. */
  1838. for_each_subsys(root, ss) {
  1839. if (ss->can_attach) {
  1840. retval = ss->can_attach(cgrp, &tset);
  1841. if (retval) {
  1842. failed_ss = ss;
  1843. goto out_cancel_attach;
  1844. }
  1845. }
  1846. }
  1847. /*
  1848. * step 2: make sure css_sets exist for all threads to be migrated.
  1849. * we use find_css_set, which allocates a new one if necessary.
  1850. */
  1851. for (i = 0; i < group_size; i++) {
  1852. tc = flex_array_get(group, i);
  1853. tc->cg = find_css_set(tc->task->cgroups, cgrp);
  1854. if (!tc->cg) {
  1855. retval = -ENOMEM;
  1856. goto out_put_css_set_refs;
  1857. }
  1858. }
  1859. /*
  1860. * step 3: now that we're guaranteed success wrt the css_sets,
  1861. * proceed to move all tasks to the new cgroup. There are no
  1862. * failure cases after here, so this is the commit point.
  1863. */
  1864. for (i = 0; i < group_size; i++) {
  1865. tc = flex_array_get(group, i);
  1866. cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
  1867. }
  1868. /* nothing is sensitive to fork() after this point. */
  1869. /*
  1870. * step 4: do subsystem attach callbacks.
  1871. */
  1872. for_each_subsys(root, ss) {
  1873. if (ss->attach)
  1874. ss->attach(cgrp, &tset);
  1875. }
  1876. /*
  1877. * step 5: success! and cleanup
  1878. */
  1879. synchronize_rcu();
  1880. retval = 0;
  1881. out_put_css_set_refs:
  1882. if (retval) {
  1883. for (i = 0; i < group_size; i++) {
  1884. tc = flex_array_get(group, i);
  1885. if (!tc->cg)
  1886. break;
  1887. put_css_set(tc->cg);
  1888. }
  1889. }
  1890. out_cancel_attach:
  1891. if (retval) {
  1892. for_each_subsys(root, ss) {
  1893. if (ss == failed_ss)
  1894. break;
  1895. if (ss->cancel_attach)
  1896. ss->cancel_attach(cgrp, &tset);
  1897. }
  1898. }
  1899. out_free_group_list:
  1900. flex_array_free(group);
  1901. return retval;
  1902. }
  1903. /*
  1904. * Find the task_struct of the task to attach by vpid and pass it along to the
  1905. * function to attach either it or all tasks in its threadgroup. Will lock
  1906. * cgroup_mutex and threadgroup; may take task_lock of task.
  1907. */
  1908. static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
  1909. {
  1910. struct task_struct *tsk;
  1911. const struct cred *cred = current_cred(), *tcred;
  1912. int ret;
  1913. if (!cgroup_lock_live_group(cgrp))
  1914. return -ENODEV;
  1915. retry_find_task:
  1916. rcu_read_lock();
  1917. if (pid) {
  1918. tsk = find_task_by_vpid(pid);
  1919. if (!tsk) {
  1920. rcu_read_unlock();
  1921. ret= -ESRCH;
  1922. goto out_unlock_cgroup;
  1923. }
  1924. /*
  1925. * even if we're attaching all tasks in the thread group, we
  1926. * only need to check permissions on one of them.
  1927. */
  1928. tcred = __task_cred(tsk);
  1929. if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
  1930. !uid_eq(cred->euid, tcred->uid) &&
  1931. !uid_eq(cred->euid, tcred->suid)) {
  1932. rcu_read_unlock();
  1933. ret = -EACCES;
  1934. goto out_unlock_cgroup;
  1935. }
  1936. } else
  1937. tsk = current;
  1938. if (threadgroup)
  1939. tsk = tsk->group_leader;
  1940. /*
  1941. * Workqueue threads may acquire PF_THREAD_BOUND and become
  1942. * trapped in a cpuset, or RT worker may be born in a cgroup
  1943. * with no rt_runtime allocated. Just say no.
  1944. */
  1945. if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
  1946. ret = -EINVAL;
  1947. rcu_read_unlock();
  1948. goto out_unlock_cgroup;
  1949. }
  1950. get_task_struct(tsk);
  1951. rcu_read_unlock();
  1952. threadgroup_lock(tsk);
  1953. if (threadgroup) {
  1954. if (!thread_group_leader(tsk)) {
  1955. /*
  1956. * a race with de_thread from another thread's exec()
  1957. * may strip us of our leadership, if this happens,
  1958. * there is no choice but to throw this task away and
  1959. * try again; this is
  1960. * "double-double-toil-and-trouble-check locking".
  1961. */
  1962. threadgroup_unlock(tsk);
  1963. put_task_struct(tsk);
  1964. goto retry_find_task;
  1965. }
  1966. ret = cgroup_attach_proc(cgrp, tsk);
  1967. } else
  1968. ret = cgroup_attach_task(cgrp, tsk);
  1969. threadgroup_unlock(tsk);
  1970. put_task_struct(tsk);
  1971. out_unlock_cgroup:
  1972. cgroup_unlock();
  1973. return ret;
  1974. }
  1975. static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
  1976. {
  1977. return attach_task_by_pid(cgrp, pid, false);
  1978. }
  1979. static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
  1980. {
  1981. return attach_task_by_pid(cgrp, tgid, true);
  1982. }
  1983. /**
  1984. * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
  1985. * @cgrp: the cgroup to be checked for liveness
  1986. *
  1987. * On success, returns true; the lock should be later released with
  1988. * cgroup_unlock(). On failure returns false with no lock held.
  1989. */
  1990. bool cgroup_lock_live_group(struct cgroup *cgrp)
  1991. {
  1992. mutex_lock(&cgroup_mutex);
  1993. if (cgroup_is_removed(cgrp)) {
  1994. mutex_unlock(&cgroup_mutex);
  1995. return false;
  1996. }
  1997. return true;
  1998. }
  1999. EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
  2000. static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
  2001. const char *buffer)
  2002. {
  2003. BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
  2004. if (strlen(buffer) >= PATH_MAX)
  2005. return -EINVAL;
  2006. if (!cgroup_lock_live_group(cgrp))
  2007. return -ENODEV;
  2008. mutex_lock(&cgroup_root_mutex);
  2009. strcpy(cgrp->root->release_agent_path, buffer);
  2010. mutex_unlock(&cgroup_root_mutex);
  2011. cgroup_unlock();
  2012. return 0;
  2013. }
  2014. static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
  2015. struct seq_file *seq)
  2016. {
  2017. if (!cgroup_lock_live_group(cgrp))
  2018. return -ENODEV;
  2019. seq_puts(seq, cgrp->root->release_agent_path);
  2020. seq_putc(seq, '\n');
  2021. cgroup_unlock();
  2022. return 0;
  2023. }
  2024. /* A buffer size big enough for numbers or short strings */
  2025. #define CGROUP_LOCAL_BUFFER_SIZE 64
  2026. static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
  2027. struct file *file,
  2028. const char __user *userbuf,
  2029. size_t nbytes, loff_t *unused_ppos)
  2030. {
  2031. char buffer[CGROUP_LOCAL_BUFFER_SIZE];
  2032. int retval = 0;
  2033. char *end;
  2034. if (!nbytes)
  2035. return -EINVAL;
  2036. if (nbytes >= sizeof(buffer))
  2037. return -E2BIG;
  2038. if (copy_from_user(buffer, userbuf, nbytes))
  2039. return -EFAULT;
  2040. buffer[nbytes] = 0; /* nul-terminate */
  2041. if (cft->write_u64) {
  2042. u64 val = simple_strtoull(strstrip(buffer), &end, 0);
  2043. if (*end)
  2044. return -EINVAL;
  2045. retval = cft->write_u64(cgrp, cft, val);
  2046. } else {
  2047. s64 val = simple_strtoll(strstrip(buffer), &end, 0);
  2048. if (*end)
  2049. return -EINVAL;
  2050. retval = cft->write_s64(cgrp, cft, val);
  2051. }
  2052. if (!retval)
  2053. retval = nbytes;
  2054. return retval;
  2055. }
  2056. static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
  2057. struct file *file,
  2058. const char __user *userbuf,
  2059. size_t nbytes, loff_t *unused_ppos)
  2060. {
  2061. char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
  2062. int retval = 0;
  2063. size_t max_bytes = cft->max_write_len;
  2064. char *buffer = local_buffer;
  2065. if (!max_bytes)
  2066. max_bytes = sizeof(local_buffer) - 1;
  2067. if (nbytes >= max_bytes)
  2068. return -E2BIG;
  2069. /* Allocate a dynamic buffer if we need one */
  2070. if (nbytes >= sizeof(local_buffer)) {
  2071. buffer = kmalloc(nbytes + 1, GFP_KERNEL);
  2072. if (buffer == NULL)
  2073. return -ENOMEM;
  2074. }
  2075. if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
  2076. retval = -EFAULT;
  2077. goto out;
  2078. }
  2079. buffer[nbytes] = 0; /* nul-terminate */
  2080. retval = cft->write_string(cgrp, cft, strstrip(buffer));
  2081. if (!retval)
  2082. retval = nbytes;
  2083. out:
  2084. if (buffer != local_buffer)
  2085. kfree(buffer);
  2086. return retval;
  2087. }
  2088. static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
  2089. size_t nbytes, loff_t *ppos)
  2090. {
  2091. struct cftype *cft = __d_cft(file->f_dentry);
  2092. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  2093. if (cgroup_is_removed(cgrp))
  2094. return -ENODEV;
  2095. if (cft->write)
  2096. return cft->write(cgrp, cft, file, buf, nbytes, ppos);
  2097. if (cft->write_u64 || cft->write_s64)
  2098. return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
  2099. if (cft->write_string)
  2100. return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
  2101. if (cft->trigger) {
  2102. int ret = cft->trigger(cgrp, (unsigned int)cft->private);
  2103. return ret ? ret : nbytes;
  2104. }
  2105. return -EINVAL;
  2106. }
  2107. static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
  2108. struct file *file,
  2109. char __user *buf, size_t nbytes,
  2110. loff_t *ppos)
  2111. {
  2112. char tmp[CGROUP_LOCAL_BUFFER_SIZE];
  2113. u64 val = cft->read_u64(cgrp, cft);
  2114. int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
  2115. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  2116. }
  2117. static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
  2118. struct file *file,
  2119. char __user *buf, size_t nbytes,
  2120. loff_t *ppos)
  2121. {
  2122. char tmp[CGROUP_LOCAL_BUFFER_SIZE];
  2123. s64 val = cft->read_s64(cgrp, cft);
  2124. int len = sprintf(tmp, "%lld\n", (long long) val);
  2125. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  2126. }
  2127. static ssize_t cgroup_file_read(struct file *file, char __user *buf,
  2128. size_t nbytes, loff_t *ppos)
  2129. {
  2130. struct cftype *cft = __d_cft(file->f_dentry);
  2131. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  2132. if (cgroup_is_removed(cgrp))
  2133. return -ENODEV;
  2134. if (cft->read)
  2135. return cft->read(cgrp, cft, file, buf, nbytes, ppos);
  2136. if (cft->read_u64)
  2137. return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
  2138. if (cft->read_s64)
  2139. return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
  2140. return -EINVAL;
  2141. }
  2142. /*
  2143. * seqfile ops/methods for returning structured data. Currently just
  2144. * supports string->u64 maps, but can be extended in future.
  2145. */
  2146. struct cgroup_seqfile_state {
  2147. struct cftype *cft;
  2148. struct cgroup *cgroup;
  2149. };
  2150. static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
  2151. {
  2152. struct seq_file *sf = cb->state;
  2153. return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
  2154. }
  2155. static int cgroup_seqfile_show(struct seq_file *m, void *arg)
  2156. {
  2157. struct cgroup_seqfile_state *state = m->private;
  2158. struct cftype *cft = state->cft;
  2159. if (cft->read_map) {
  2160. struct cgroup_map_cb cb = {
  2161. .fill = cgroup_map_add,
  2162. .state = m,
  2163. };
  2164. return cft->read_map(state->cgroup, cft, &cb);
  2165. }
  2166. return cft->read_seq_string(state->cgroup, cft, m);
  2167. }
  2168. static int cgroup_seqfile_release(struct inode *inode, struct file *file)
  2169. {
  2170. struct seq_file *seq = file->private_data;
  2171. kfree(seq->private);
  2172. return single_release(inode, file);
  2173. }
  2174. static const struct file_operations cgroup_seqfile_operations = {
  2175. .read = seq_read,
  2176. .write = cgroup_file_write,
  2177. .llseek = seq_lseek,
  2178. .release = cgroup_seqfile_release,
  2179. };
  2180. static int cgroup_file_open(struct inode *inode, struct file *file)
  2181. {
  2182. int err;
  2183. struct cftype *cft;
  2184. err = generic_file_open(inode, file);
  2185. if (err)
  2186. return err;
  2187. cft = __d_cft(file->f_dentry);
  2188. if (cft->read_map || cft->read_seq_string) {
  2189. struct cgroup_seqfile_state *state =
  2190. kzalloc(sizeof(*state), GFP_USER);
  2191. if (!state)
  2192. return -ENOMEM;
  2193. state->cft = cft;
  2194. state->cgroup = __d_cgrp(file->f_dentry->d_parent);
  2195. file->f_op = &cgroup_seqfile_operations;
  2196. err = single_open(file, cgroup_seqfile_show, state);
  2197. if (err < 0)
  2198. kfree(state);
  2199. } else if (cft->open)
  2200. err = cft->open(inode, file);
  2201. else
  2202. err = 0;
  2203. return err;
  2204. }
  2205. static int cgroup_file_release(struct inode *inode, struct file *file)
  2206. {
  2207. struct cftype *cft = __d_cft(file->f_dentry);
  2208. if (cft->release)
  2209. return cft->release(inode, file);
  2210. return 0;
  2211. }
  2212. /*
  2213. * cgroup_rename - Only allow simple rename of directories in place.
  2214. */
  2215. static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
  2216. struct inode *new_dir, struct dentry *new_dentry)
  2217. {
  2218. if (!S_ISDIR(old_dentry->d_inode->i_mode))
  2219. return -ENOTDIR;
  2220. if (new_dentry->d_inode)
  2221. return -EEXIST;
  2222. if (old_dir != new_dir)
  2223. return -EIO;
  2224. return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  2225. }
  2226. static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
  2227. {
  2228. if (S_ISDIR(dentry->d_inode->i_mode))
  2229. return &__d_cgrp(dentry)->xattrs;
  2230. else
  2231. return &__d_cft(dentry)->xattrs;
  2232. }
  2233. static inline int xattr_enabled(struct dentry *dentry)
  2234. {
  2235. struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
  2236. return test_bit(ROOT_XATTR, &root->flags);
  2237. }
  2238. static bool is_valid_xattr(const char *name)
  2239. {
  2240. if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
  2241. !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
  2242. return true;
  2243. return false;
  2244. }
  2245. static int cgroup_setxattr(struct dentry *dentry, const char *name,
  2246. const void *val, size_t size, int flags)
  2247. {
  2248. if (!xattr_enabled(dentry))
  2249. return -EOPNOTSUPP;
  2250. if (!is_valid_xattr(name))
  2251. return -EINVAL;
  2252. return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
  2253. }
  2254. static int cgroup_removexattr(struct dentry *dentry, const char *name)
  2255. {
  2256. if (!xattr_enabled(dentry))
  2257. return -EOPNOTSUPP;
  2258. if (!is_valid_xattr(name))
  2259. return -EINVAL;
  2260. return simple_xattr_remove(__d_xattrs(dentry), name);
  2261. }
  2262. static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
  2263. void *buf, size_t size)
  2264. {
  2265. if (!xattr_enabled(dentry))
  2266. return -EOPNOTSUPP;
  2267. if (!is_valid_xattr(name))
  2268. return -EINVAL;
  2269. return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
  2270. }
  2271. static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
  2272. {
  2273. if (!xattr_enabled(dentry))
  2274. return -EOPNOTSUPP;
  2275. return simple_xattr_list(__d_xattrs(dentry), buf, size);
  2276. }
  2277. static const struct file_operations cgroup_file_operations = {
  2278. .read = cgroup_file_read,
  2279. .write = cgroup_file_write,
  2280. .llseek = generic_file_llseek,
  2281. .open = cgroup_file_open,
  2282. .release = cgroup_file_release,
  2283. };
  2284. static const struct inode_operations cgroup_file_inode_operations = {
  2285. .setxattr = cgroup_setxattr,
  2286. .getxattr = cgroup_getxattr,
  2287. .listxattr = cgroup_listxattr,
  2288. .removexattr = cgroup_removexattr,
  2289. };
  2290. static const struct inode_operations cgroup_dir_inode_operations = {
  2291. .lookup = cgroup_lookup,
  2292. .mkdir = cgroup_mkdir,
  2293. .rmdir = cgroup_rmdir,
  2294. .rename = cgroup_rename,
  2295. .setxattr = cgroup_setxattr,
  2296. .getxattr = cgroup_getxattr,
  2297. .listxattr = cgroup_listxattr,
  2298. .removexattr = cgroup_removexattr,
  2299. };
  2300. static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  2301. {
  2302. if (dentry->d_name.len > NAME_MAX)
  2303. return ERR_PTR(-ENAMETOOLONG);
  2304. d_add(dentry, NULL);
  2305. return NULL;
  2306. }
  2307. /*
  2308. * Check if a file is a control file
  2309. */
  2310. static inline struct cftype *__file_cft(struct file *file)
  2311. {
  2312. if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
  2313. return ERR_PTR(-EINVAL);
  2314. return __d_cft(file->f_dentry);
  2315. }
  2316. static int cgroup_create_file(struct dentry *dentry, umode_t mode,
  2317. struct super_block *sb)
  2318. {
  2319. struct inode *inode;
  2320. if (!dentry)
  2321. return -ENOENT;
  2322. if (dentry->d_inode)
  2323. return -EEXIST;
  2324. inode = cgroup_new_inode(mode, sb);
  2325. if (!inode)
  2326. return -ENOMEM;
  2327. if (S_ISDIR(mode)) {
  2328. inode->i_op = &cgroup_dir_inode_operations;
  2329. inode->i_fop = &simple_dir_operations;
  2330. /* start off with i_nlink == 2 (for "." entry) */
  2331. inc_nlink(inode);
  2332. inc_nlink(dentry->d_parent->d_inode);
  2333. /*
  2334. * Control reaches here with cgroup_mutex held.
  2335. * @inode->i_mutex should nest outside cgroup_mutex but we
  2336. * want to populate it immediately without releasing
  2337. * cgroup_mutex. As @inode isn't visible to anyone else
  2338. * yet, trylock will always succeed without affecting
  2339. * lockdep checks.
  2340. */
  2341. WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
  2342. } else if (S_ISREG(mode)) {
  2343. inode->i_size = 0;
  2344. inode->i_fop = &cgroup_file_operations;
  2345. inode->i_op = &cgroup_file_inode_operations;
  2346. }
  2347. d_instantiate(dentry, inode);
  2348. dget(dentry); /* Extra count - pin the dentry in core */
  2349. return 0;
  2350. }
  2351. /**
  2352. * cgroup_file_mode - deduce file mode of a control file
  2353. * @cft: the control file in question
  2354. *
  2355. * returns cft->mode if ->mode is not 0
  2356. * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
  2357. * returns S_IRUGO if it has only a read handler
  2358. * returns S_IWUSR if it has only a write hander
  2359. */
  2360. static umode_t cgroup_file_mode(const struct cftype *cft)
  2361. {
  2362. umode_t mode = 0;
  2363. if (cft->mode)
  2364. return cft->mode;
  2365. if (cft->read || cft->read_u64 || cft->read_s64 ||
  2366. cft->read_map || cft->read_seq_string)
  2367. mode |= S_IRUGO;
  2368. if (cft->write || cft->write_u64 || cft->write_s64 ||
  2369. cft->write_string || cft->trigger)
  2370. mode |= S_IWUSR;
  2371. return mode;
  2372. }
  2373. static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
  2374. struct cftype *cft)
  2375. {
  2376. struct dentry *dir = cgrp->dentry;
  2377. struct cgroup *parent = __d_cgrp(dir);
  2378. struct dentry *dentry;
  2379. struct cfent *cfe;
  2380. int error;
  2381. umode_t mode;
  2382. char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
  2383. simple_xattrs_init(&cft->xattrs);
  2384. /* does @cft->flags tell us to skip creation on @cgrp? */
  2385. if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
  2386. return 0;
  2387. if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
  2388. return 0;
  2389. if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
  2390. strcpy(name, subsys->name);
  2391. strcat(name, ".");
  2392. }
  2393. strcat(name, cft->name);
  2394. BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
  2395. cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
  2396. if (!cfe)
  2397. return -ENOMEM;
  2398. dentry = lookup_one_len(name, dir, strlen(name));
  2399. if (IS_ERR(dentry)) {
  2400. error = PTR_ERR(dentry);
  2401. goto out;
  2402. }
  2403. mode = cgroup_file_mode(cft);
  2404. error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
  2405. if (!error) {
  2406. cfe->type = (void *)cft;
  2407. cfe->dentry = dentry;
  2408. dentry->d_fsdata = cfe;
  2409. list_add_tail(&cfe->node, &parent->files);
  2410. cfe = NULL;
  2411. }
  2412. dput(dentry);
  2413. out:
  2414. kfree(cfe);
  2415. return error;
  2416. }
  2417. static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
  2418. struct cftype cfts[], bool is_add)
  2419. {
  2420. struct cftype *cft;
  2421. int err, ret = 0;
  2422. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  2423. if (is_add)
  2424. err = cgroup_add_file(cgrp, subsys, cft);
  2425. else
  2426. err = cgroup_rm_file(cgrp, cft);
  2427. if (err) {
  2428. pr_warning("cgroup_addrm_files: failed to %s %s, err=%d\n",
  2429. is_add ? "add" : "remove", cft->name, err);
  2430. ret = err;
  2431. }
  2432. }
  2433. return ret;
  2434. }
  2435. static DEFINE_MUTEX(cgroup_cft_mutex);
  2436. static void cgroup_cfts_prepare(void)
  2437. __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
  2438. {
  2439. /*
  2440. * Thanks to the entanglement with vfs inode locking, we can't walk
  2441. * the existing cgroups under cgroup_mutex and create files.
  2442. * Instead, we increment reference on all cgroups and build list of
  2443. * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
  2444. * exclusive access to the field.
  2445. */
  2446. mutex_lock(&cgroup_cft_mutex);
  2447. mutex_lock(&cgroup_mutex);
  2448. }
  2449. static void cgroup_cfts_commit(struct cgroup_subsys *ss,
  2450. struct cftype *cfts, bool is_add)
  2451. __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
  2452. {
  2453. LIST_HEAD(pending);
  2454. struct cgroup *cgrp, *n;
  2455. /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
  2456. if (cfts && ss->root != &rootnode) {
  2457. list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
  2458. dget(cgrp->dentry);
  2459. list_add_tail(&cgrp->cft_q_node, &pending);
  2460. }
  2461. }
  2462. mutex_unlock(&cgroup_mutex);
  2463. /*
  2464. * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
  2465. * files for all cgroups which were created before.
  2466. */
  2467. list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
  2468. struct inode *inode = cgrp->dentry->d_inode;
  2469. mutex_lock(&inode->i_mutex);
  2470. mutex_lock(&cgroup_mutex);
  2471. if (!cgroup_is_removed(cgrp))
  2472. cgroup_addrm_files(cgrp, ss, cfts, is_add);
  2473. mutex_unlock(&cgroup_mutex);
  2474. mutex_unlock(&inode->i_mutex);
  2475. list_del_init(&cgrp->cft_q_node);
  2476. dput(cgrp->dentry);
  2477. }
  2478. mutex_unlock(&cgroup_cft_mutex);
  2479. }
  2480. /**
  2481. * cgroup_add_cftypes - add an array of cftypes to a subsystem
  2482. * @ss: target cgroup subsystem
  2483. * @cfts: zero-length name terminated array of cftypes
  2484. *
  2485. * Register @cfts to @ss. Files described by @cfts are created for all
  2486. * existing cgroups to which @ss is attached and all future cgroups will
  2487. * have them too. This function can be called anytime whether @ss is
  2488. * attached or not.
  2489. *
  2490. * Returns 0 on successful registration, -errno on failure. Note that this
  2491. * function currently returns 0 as long as @cfts registration is successful
  2492. * even if some file creation attempts on existing cgroups fail.
  2493. */
  2494. int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  2495. {
  2496. struct cftype_set *set;
  2497. set = kzalloc(sizeof(*set), GFP_KERNEL);
  2498. if (!set)
  2499. return -ENOMEM;
  2500. cgroup_cfts_prepare();
  2501. set->cfts = cfts;
  2502. list_add_tail(&set->node, &ss->cftsets);
  2503. cgroup_cfts_commit(ss, cfts, true);
  2504. return 0;
  2505. }
  2506. EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
  2507. /**
  2508. * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
  2509. * @ss: target cgroup subsystem
  2510. * @cfts: zero-length name terminated array of cftypes
  2511. *
  2512. * Unregister @cfts from @ss. Files described by @cfts are removed from
  2513. * all existing cgroups to which @ss is attached and all future cgroups
  2514. * won't have them either. This function can be called anytime whether @ss
  2515. * is attached or not.
  2516. *
  2517. * Returns 0 on successful unregistration, -ENOENT if @cfts is not
  2518. * registered with @ss.
  2519. */
  2520. int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  2521. {
  2522. struct cftype_set *set;
  2523. cgroup_cfts_prepare();
  2524. list_for_each_entry(set, &ss->cftsets, node) {
  2525. if (set->cfts == cfts) {
  2526. list_del_init(&set->node);
  2527. cgroup_cfts_commit(ss, cfts, false);
  2528. return 0;
  2529. }
  2530. }
  2531. cgroup_cfts_commit(ss, NULL, false);
  2532. return -ENOENT;
  2533. }
  2534. /**
  2535. * cgroup_task_count - count the number of tasks in a cgroup.
  2536. * @cgrp: the cgroup in question
  2537. *
  2538. * Return the number of tasks in the cgroup.
  2539. */
  2540. int cgroup_task_count(const struct cgroup *cgrp)
  2541. {
  2542. int count = 0;
  2543. struct cg_cgroup_link *link;
  2544. read_lock(&css_set_lock);
  2545. list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
  2546. count += atomic_read(&link->cg->refcount);
  2547. }
  2548. read_unlock(&css_set_lock);
  2549. return count;
  2550. }
  2551. /*
  2552. * Advance a list_head iterator. The iterator should be positioned at
  2553. * the start of a css_set
  2554. */
  2555. static void cgroup_advance_iter(struct cgroup *cgrp,
  2556. struct cgroup_iter *it)
  2557. {
  2558. struct list_head *l = it->cg_link;
  2559. struct cg_cgroup_link *link;
  2560. struct css_set *cg;
  2561. /* Advance to the next non-empty css_set */
  2562. do {
  2563. l = l->next;
  2564. if (l == &cgrp->css_sets) {
  2565. it->cg_link = NULL;
  2566. return;
  2567. }
  2568. link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
  2569. cg = link->cg;
  2570. } while (list_empty(&cg->tasks));
  2571. it->cg_link = l;
  2572. it->task = cg->tasks.next;
  2573. }
  2574. /*
  2575. * To reduce the fork() overhead for systems that are not actually
  2576. * using their cgroups capability, we don't maintain the lists running
  2577. * through each css_set to its tasks until we see the list actually
  2578. * used - in other words after the first call to cgroup_iter_start().
  2579. */
  2580. static void cgroup_enable_task_cg_lists(void)
  2581. {
  2582. struct task_struct *p, *g;
  2583. write_lock(&css_set_lock);
  2584. use_task_css_set_links = 1;
  2585. /*
  2586. * We need tasklist_lock because RCU is not safe against
  2587. * while_each_thread(). Besides, a forking task that has passed
  2588. * cgroup_post_fork() without seeing use_task_css_set_links = 1
  2589. * is not guaranteed to have its child immediately visible in the
  2590. * tasklist if we walk through it with RCU.
  2591. */
  2592. read_lock(&tasklist_lock);
  2593. do_each_thread(g, p) {
  2594. task_lock(p);
  2595. /*
  2596. * We should check if the process is exiting, otherwise
  2597. * it will race with cgroup_exit() in that the list
  2598. * entry won't be deleted though the process has exited.
  2599. */
  2600. if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
  2601. list_add(&p->cg_list, &p->cgroups->tasks);
  2602. task_unlock(p);
  2603. } while_each_thread(g, p);
  2604. read_unlock(&tasklist_lock);
  2605. write_unlock(&css_set_lock);
  2606. }
  2607. /**
  2608. * cgroup_next_descendant_pre - find the next descendant for pre-order walk
  2609. * @pos: the current position (%NULL to initiate traversal)
  2610. * @cgroup: cgroup whose descendants to walk
  2611. *
  2612. * To be used by cgroup_for_each_descendant_pre(). Find the next
  2613. * descendant to visit for pre-order traversal of @cgroup's descendants.
  2614. */
  2615. struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
  2616. struct cgroup *cgroup)
  2617. {
  2618. struct cgroup *next;
  2619. WARN_ON_ONCE(!rcu_read_lock_held());
  2620. /* if first iteration, pretend we just visited @cgroup */
  2621. if (!pos) {
  2622. if (list_empty(&cgroup->children))
  2623. return NULL;
  2624. pos = cgroup;
  2625. }
  2626. /* visit the first child if exists */
  2627. next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
  2628. if (next)
  2629. return next;
  2630. /* no child, visit my or the closest ancestor's next sibling */
  2631. do {
  2632. next = list_entry_rcu(pos->sibling.next, struct cgroup,
  2633. sibling);
  2634. if (&next->sibling != &pos->parent->children)
  2635. return next;
  2636. pos = pos->parent;
  2637. } while (pos != cgroup);
  2638. return NULL;
  2639. }
  2640. EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
  2641. static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
  2642. {
  2643. struct cgroup *last;
  2644. do {
  2645. last = pos;
  2646. pos = list_first_or_null_rcu(&pos->children, struct cgroup,
  2647. sibling);
  2648. } while (pos);
  2649. return last;
  2650. }
  2651. /**
  2652. * cgroup_next_descendant_post - find the next descendant for post-order walk
  2653. * @pos: the current position (%NULL to initiate traversal)
  2654. * @cgroup: cgroup whose descendants to walk
  2655. *
  2656. * To be used by cgroup_for_each_descendant_post(). Find the next
  2657. * descendant to visit for post-order traversal of @cgroup's descendants.
  2658. */
  2659. struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
  2660. struct cgroup *cgroup)
  2661. {
  2662. struct cgroup *next;
  2663. WARN_ON_ONCE(!rcu_read_lock_held());
  2664. /* if first iteration, visit the leftmost descendant */
  2665. if (!pos) {
  2666. next = cgroup_leftmost_descendant(cgroup);
  2667. return next != cgroup ? next : NULL;
  2668. }
  2669. /* if there's an unvisited sibling, visit its leftmost descendant */
  2670. next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
  2671. if (&next->sibling != &pos->parent->children)
  2672. return cgroup_leftmost_descendant(next);
  2673. /* no sibling left, visit parent */
  2674. next = pos->parent;
  2675. return next != cgroup ? next : NULL;
  2676. }
  2677. EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
  2678. void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
  2679. __acquires(css_set_lock)
  2680. {
  2681. /*
  2682. * The first time anyone tries to iterate across a cgroup,
  2683. * we need to enable the list linking each css_set to its
  2684. * tasks, and fix up all existing tasks.
  2685. */
  2686. if (!use_task_css_set_links)
  2687. cgroup_enable_task_cg_lists();
  2688. read_lock(&css_set_lock);
  2689. it->cg_link = &cgrp->css_sets;
  2690. cgroup_advance_iter(cgrp, it);
  2691. }
  2692. struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
  2693. struct cgroup_iter *it)
  2694. {
  2695. struct task_struct *res;
  2696. struct list_head *l = it->task;
  2697. struct cg_cgroup_link *link;
  2698. /* If the iterator cg is NULL, we have no tasks */
  2699. if (!it->cg_link)
  2700. return NULL;
  2701. res = list_entry(l, struct task_struct, cg_list);
  2702. /* Advance iterator to find next entry */
  2703. l = l->next;
  2704. link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
  2705. if (l == &link->cg->tasks) {
  2706. /* We reached the end of this task list - move on to
  2707. * the next cg_cgroup_link */
  2708. cgroup_advance_iter(cgrp, it);
  2709. } else {
  2710. it->task = l;
  2711. }
  2712. return res;
  2713. }
  2714. void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
  2715. __releases(css_set_lock)
  2716. {
  2717. read_unlock(&css_set_lock);
  2718. }
  2719. static inline int started_after_time(struct task_struct *t1,
  2720. struct timespec *time,
  2721. struct task_struct *t2)
  2722. {
  2723. int start_diff = timespec_compare(&t1->start_time, time);
  2724. if (start_diff > 0) {
  2725. return 1;
  2726. } else if (start_diff < 0) {
  2727. return 0;
  2728. } else {
  2729. /*
  2730. * Arbitrarily, if two processes started at the same
  2731. * time, we'll say that the lower pointer value
  2732. * started first. Note that t2 may have exited by now
  2733. * so this may not be a valid pointer any longer, but
  2734. * that's fine - it still serves to distinguish
  2735. * between two tasks started (effectively) simultaneously.
  2736. */
  2737. return t1 > t2;
  2738. }
  2739. }
  2740. /*
  2741. * This function is a callback from heap_insert() and is used to order
  2742. * the heap.
  2743. * In this case we order the heap in descending task start time.
  2744. */
  2745. static inline int started_after(void *p1, void *p2)
  2746. {
  2747. struct task_struct *t1 = p1;
  2748. struct task_struct *t2 = p2;
  2749. return started_after_time(t1, &t2->start_time, t2);
  2750. }
  2751. /**
  2752. * cgroup_scan_tasks - iterate though all the tasks in a cgroup
  2753. * @scan: struct cgroup_scanner containing arguments for the scan
  2754. *
  2755. * Arguments include pointers to callback functions test_task() and
  2756. * process_task().
  2757. * Iterate through all the tasks in a cgroup, calling test_task() for each,
  2758. * and if it returns true, call process_task() for it also.
  2759. * The test_task pointer may be NULL, meaning always true (select all tasks).
  2760. * Effectively duplicates cgroup_iter_{start,next,end}()
  2761. * but does not lock css_set_lock for the call to process_task().
  2762. * The struct cgroup_scanner may be embedded in any structure of the caller's
  2763. * creation.
  2764. * It is guaranteed that process_task() will act on every task that
  2765. * is a member of the cgroup for the duration of this call. This
  2766. * function may or may not call process_task() for tasks that exit
  2767. * or move to a different cgroup during the call, or are forked or
  2768. * move into the cgroup during the call.
  2769. *
  2770. * Note that test_task() may be called with locks held, and may in some
  2771. * situations be called multiple times for the same task, so it should
  2772. * be cheap.
  2773. * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
  2774. * pre-allocated and will be used for heap operations (and its "gt" member will
  2775. * be overwritten), else a temporary heap will be used (allocation of which
  2776. * may cause this function to fail).
  2777. */
  2778. int cgroup_scan_tasks(struct cgroup_scanner *scan)
  2779. {
  2780. int retval, i;
  2781. struct cgroup_iter it;
  2782. struct task_struct *p, *dropped;
  2783. /* Never dereference latest_task, since it's not refcounted */
  2784. struct task_struct *latest_task = NULL;
  2785. struct ptr_heap tmp_heap;
  2786. struct ptr_heap *heap;
  2787. struct timespec latest_time = { 0, 0 };
  2788. if (scan->heap) {
  2789. /* The caller supplied our heap and pre-allocated its memory */
  2790. heap = scan->heap;
  2791. heap->gt = &started_after;
  2792. } else {
  2793. /* We need to allocate our own heap memory */
  2794. heap = &tmp_heap;
  2795. retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
  2796. if (retval)
  2797. /* cannot allocate the heap */
  2798. return retval;
  2799. }
  2800. again:
  2801. /*
  2802. * Scan tasks in the cgroup, using the scanner's "test_task" callback
  2803. * to determine which are of interest, and using the scanner's
  2804. * "process_task" callback to process any of them that need an update.
  2805. * Since we don't want to hold any locks during the task updates,
  2806. * gather tasks to be processed in a heap structure.
  2807. * The heap is sorted by descending task start time.
  2808. * If the statically-sized heap fills up, we overflow tasks that
  2809. * started later, and in future iterations only consider tasks that
  2810. * started after the latest task in the previous pass. This
  2811. * guarantees forward progress and that we don't miss any tasks.
  2812. */
  2813. heap->size = 0;
  2814. cgroup_iter_start(scan->cg, &it);
  2815. while ((p = cgroup_iter_next(scan->cg, &it))) {
  2816. /*
  2817. * Only affect tasks that qualify per the caller's callback,
  2818. * if he provided one
  2819. */
  2820. if (scan->test_task && !scan->test_task(p, scan))
  2821. continue;
  2822. /*
  2823. * Only process tasks that started after the last task
  2824. * we processed
  2825. */
  2826. if (!started_after_time(p, &latest_time, latest_task))
  2827. continue;
  2828. dropped = heap_insert(heap, p);
  2829. if (dropped == NULL) {
  2830. /*
  2831. * The new task was inserted; the heap wasn't
  2832. * previously full
  2833. */
  2834. get_task_struct(p);
  2835. } else if (dropped != p) {
  2836. /*
  2837. * The new task was inserted, and pushed out a
  2838. * different task
  2839. */
  2840. get_task_struct(p);
  2841. put_task_struct(dropped);
  2842. }
  2843. /*
  2844. * Else the new task was newer than anything already in
  2845. * the heap and wasn't inserted
  2846. */
  2847. }
  2848. cgroup_iter_end(scan->cg, &it);
  2849. if (heap->size) {
  2850. for (i = 0; i < heap->size; i++) {
  2851. struct task_struct *q = heap->ptrs[i];
  2852. if (i == 0) {
  2853. latest_time = q->start_time;
  2854. latest_task = q;
  2855. }
  2856. /* Process the task per the caller's callback */
  2857. scan->process_task(q, scan);
  2858. put_task_struct(q);
  2859. }
  2860. /*
  2861. * If we had to process any tasks at all, scan again
  2862. * in case some of them were in the middle of forking
  2863. * children that didn't get processed.
  2864. * Not the most efficient way to do it, but it avoids
  2865. * having to take callback_mutex in the fork path
  2866. */
  2867. goto again;
  2868. }
  2869. if (heap == &tmp_heap)
  2870. heap_free(&tmp_heap);
  2871. return 0;
  2872. }
  2873. /*
  2874. * Stuff for reading the 'tasks'/'procs' files.
  2875. *
  2876. * Reading this file can return large amounts of data if a cgroup has
  2877. * *lots* of attached tasks. So it may need several calls to read(),
  2878. * but we cannot guarantee that the information we produce is correct
  2879. * unless we produce it entirely atomically.
  2880. *
  2881. */
  2882. /* which pidlist file are we talking about? */
  2883. enum cgroup_filetype {
  2884. CGROUP_FILE_PROCS,
  2885. CGROUP_FILE_TASKS,
  2886. };
  2887. /*
  2888. * A pidlist is a list of pids that virtually represents the contents of one
  2889. * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
  2890. * a pair (one each for procs, tasks) for each pid namespace that's relevant
  2891. * to the cgroup.
  2892. */
  2893. struct cgroup_pidlist {
  2894. /*
  2895. * used to find which pidlist is wanted. doesn't change as long as
  2896. * this particular list stays in the list.
  2897. */
  2898. struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
  2899. /* array of xids */
  2900. pid_t *list;
  2901. /* how many elements the above list has */
  2902. int length;
  2903. /* how many files are using the current array */
  2904. int use_count;
  2905. /* each of these stored in a list by its cgroup */
  2906. struct list_head links;
  2907. /* pointer to the cgroup we belong to, for list removal purposes */
  2908. struct cgroup *owner;
  2909. /* protects the other fields */
  2910. struct rw_semaphore mutex;
  2911. };
  2912. /*
  2913. * The following two functions "fix" the issue where there are more pids
  2914. * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
  2915. * TODO: replace with a kernel-wide solution to this problem
  2916. */
  2917. #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
  2918. static void *pidlist_allocate(int count)
  2919. {
  2920. if (PIDLIST_TOO_LARGE(count))
  2921. return vmalloc(count * sizeof(pid_t));
  2922. else
  2923. return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
  2924. }
  2925. static void pidlist_free(void *p)
  2926. {
  2927. if (is_vmalloc_addr(p))
  2928. vfree(p);
  2929. else
  2930. kfree(p);
  2931. }
  2932. static void *pidlist_resize(void *p, int newcount)
  2933. {
  2934. void *newlist;
  2935. /* note: if new alloc fails, old p will still be valid either way */
  2936. if (is_vmalloc_addr(p)) {
  2937. newlist = vmalloc(newcount * sizeof(pid_t));
  2938. if (!newlist)
  2939. return NULL;
  2940. memcpy(newlist, p, newcount * sizeof(pid_t));
  2941. vfree(p);
  2942. } else {
  2943. newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
  2944. }
  2945. return newlist;
  2946. }
  2947. /*
  2948. * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
  2949. * If the new stripped list is sufficiently smaller and there's enough memory
  2950. * to allocate a new buffer, will let go of the unneeded memory. Returns the
  2951. * number of unique elements.
  2952. */
  2953. /* is the size difference enough that we should re-allocate the array? */
  2954. #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
  2955. static int pidlist_uniq(pid_t **p, int length)
  2956. {
  2957. int src, dest = 1;
  2958. pid_t *list = *p;
  2959. pid_t *newlist;
  2960. /*
  2961. * we presume the 0th element is unique, so i starts at 1. trivial
  2962. * edge cases first; no work needs to be done for either
  2963. */
  2964. if (length == 0 || length == 1)
  2965. return length;
  2966. /* src and dest walk down the list; dest counts unique elements */
  2967. for (src = 1; src < length; src++) {
  2968. /* find next unique element */
  2969. while (list[src] == list[src-1]) {
  2970. src++;
  2971. if (src == length)
  2972. goto after;
  2973. }
  2974. /* dest always points to where the next unique element goes */
  2975. list[dest] = list[src];
  2976. dest++;
  2977. }
  2978. after:
  2979. /*
  2980. * if the length difference is large enough, we want to allocate a
  2981. * smaller buffer to save memory. if this fails due to out of memory,
  2982. * we'll just stay with what we've got.
  2983. */
  2984. if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
  2985. newlist = pidlist_resize(list, dest);
  2986. if (newlist)
  2987. *p = newlist;
  2988. }
  2989. return dest;
  2990. }
  2991. static int cmppid(const void *a, const void *b)
  2992. {
  2993. return *(pid_t *)a - *(pid_t *)b;
  2994. }
  2995. /*
  2996. * find the appropriate pidlist for our purpose (given procs vs tasks)
  2997. * returns with the lock on that pidlist already held, and takes care
  2998. * of the use count, or returns NULL with no locks held if we're out of
  2999. * memory.
  3000. */
  3001. static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
  3002. enum cgroup_filetype type)
  3003. {
  3004. struct cgroup_pidlist *l;
  3005. /* don't need task_nsproxy() if we're looking at ourself */
  3006. struct pid_namespace *ns = current->nsproxy->pid_ns;
  3007. /*
  3008. * We can't drop the pidlist_mutex before taking the l->mutex in case
  3009. * the last ref-holder is trying to remove l from the list at the same
  3010. * time. Holding the pidlist_mutex precludes somebody taking whichever
  3011. * list we find out from under us - compare release_pid_array().
  3012. */
  3013. mutex_lock(&cgrp->pidlist_mutex);
  3014. list_for_each_entry(l, &cgrp->pidlists, links) {
  3015. if (l->key.type == type && l->key.ns == ns) {
  3016. /* make sure l doesn't vanish out from under us */
  3017. down_write(&l->mutex);
  3018. mutex_unlock(&cgrp->pidlist_mutex);
  3019. return l;
  3020. }
  3021. }
  3022. /* entry not found; create a new one */
  3023. l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
  3024. if (!l) {
  3025. mutex_unlock(&cgrp->pidlist_mutex);
  3026. return l;
  3027. }
  3028. init_rwsem(&l->mutex);
  3029. down_write(&l->mutex);
  3030. l->key.type = type;
  3031. l->key.ns = get_pid_ns(ns);
  3032. l->use_count = 0; /* don't increment here */
  3033. l->list = NULL;
  3034. l->owner = cgrp;
  3035. list_add(&l->links, &cgrp->pidlists);
  3036. mutex_unlock(&cgrp->pidlist_mutex);
  3037. return l;
  3038. }
  3039. /*
  3040. * Load a cgroup's pidarray with either procs' tgids or tasks' pids
  3041. */
  3042. static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
  3043. struct cgroup_pidlist **lp)
  3044. {
  3045. pid_t *array;
  3046. int length;
  3047. int pid, n = 0; /* used for populating the array */
  3048. struct cgroup_iter it;
  3049. struct task_struct *tsk;
  3050. struct cgroup_pidlist *l;
  3051. /*
  3052. * If cgroup gets more users after we read count, we won't have
  3053. * enough space - tough. This race is indistinguishable to the
  3054. * caller from the case that the additional cgroup users didn't
  3055. * show up until sometime later on.
  3056. */
  3057. length = cgroup_task_count(cgrp);
  3058. array = pidlist_allocate(length);
  3059. if (!array)
  3060. return -ENOMEM;
  3061. /* now, populate the array */
  3062. cgroup_iter_start(cgrp, &it);
  3063. while ((tsk = cgroup_iter_next(cgrp, &it))) {
  3064. if (unlikely(n == length))
  3065. break;
  3066. /* get tgid or pid for procs or tasks file respectively */
  3067. if (type == CGROUP_FILE_PROCS)
  3068. pid = task_tgid_vnr(tsk);
  3069. else
  3070. pid = task_pid_vnr(tsk);
  3071. if (pid > 0) /* make sure to only use valid results */
  3072. array[n++] = pid;
  3073. }
  3074. cgroup_iter_end(cgrp, &it);
  3075. length = n;
  3076. /* now sort & (if procs) strip out duplicates */
  3077. sort(array, length, sizeof(pid_t), cmppid, NULL);
  3078. if (type == CGROUP_FILE_PROCS)
  3079. length = pidlist_uniq(&array, length);
  3080. l = cgroup_pidlist_find(cgrp, type);
  3081. if (!l) {
  3082. pidlist_free(array);
  3083. return -ENOMEM;
  3084. }
  3085. /* store array, freeing old if necessary - lock already held */
  3086. pidlist_free(l->list);
  3087. l->list = array;
  3088. l->length = length;
  3089. l->use_count++;
  3090. up_write(&l->mutex);
  3091. *lp = l;
  3092. return 0;
  3093. }
  3094. /**
  3095. * cgroupstats_build - build and fill cgroupstats
  3096. * @stats: cgroupstats to fill information into
  3097. * @dentry: A dentry entry belonging to the cgroup for which stats have
  3098. * been requested.
  3099. *
  3100. * Build and fill cgroupstats so that taskstats can export it to user
  3101. * space.
  3102. */
  3103. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
  3104. {
  3105. int ret = -EINVAL;
  3106. struct cgroup *cgrp;
  3107. struct cgroup_iter it;
  3108. struct task_struct *tsk;
  3109. /*
  3110. * Validate dentry by checking the superblock operations,
  3111. * and make sure it's a directory.
  3112. */
  3113. if (dentry->d_sb->s_op != &cgroup_ops ||
  3114. !S_ISDIR(dentry->d_inode->i_mode))
  3115. goto err;
  3116. ret = 0;
  3117. cgrp = dentry->d_fsdata;
  3118. cgroup_iter_start(cgrp, &it);
  3119. while ((tsk = cgroup_iter_next(cgrp, &it))) {
  3120. switch (tsk->state) {
  3121. case TASK_RUNNING:
  3122. stats->nr_running++;
  3123. break;
  3124. case TASK_INTERRUPTIBLE:
  3125. stats->nr_sleeping++;
  3126. break;
  3127. case TASK_UNINTERRUPTIBLE:
  3128. stats->nr_uninterruptible++;
  3129. break;
  3130. case TASK_STOPPED:
  3131. stats->nr_stopped++;
  3132. break;
  3133. default:
  3134. if (delayacct_is_task_waiting_on_io(tsk))
  3135. stats->nr_io_wait++;
  3136. break;
  3137. }
  3138. }
  3139. cgroup_iter_end(cgrp, &it);
  3140. err:
  3141. return ret;
  3142. }
  3143. /*
  3144. * seq_file methods for the tasks/procs files. The seq_file position is the
  3145. * next pid to display; the seq_file iterator is a pointer to the pid
  3146. * in the cgroup->l->list array.
  3147. */
  3148. static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
  3149. {
  3150. /*
  3151. * Initially we receive a position value that corresponds to
  3152. * one more than the last pid shown (or 0 on the first call or
  3153. * after a seek to the start). Use a binary-search to find the
  3154. * next pid to display, if any
  3155. */
  3156. struct cgroup_pidlist *l = s->private;
  3157. int index = 0, pid = *pos;
  3158. int *iter;
  3159. down_read(&l->mutex);
  3160. if (pid) {
  3161. int end = l->length;
  3162. while (index < end) {
  3163. int mid = (index + end) / 2;
  3164. if (l->list[mid] == pid) {
  3165. index = mid;
  3166. break;
  3167. } else if (l->list[mid] <= pid)
  3168. index = mid + 1;
  3169. else
  3170. end = mid;
  3171. }
  3172. }
  3173. /* If we're off the end of the array, we're done */
  3174. if (index >= l->length)
  3175. return NULL;
  3176. /* Update the abstract position to be the actual pid that we found */
  3177. iter = l->list + index;
  3178. *pos = *iter;
  3179. return iter;
  3180. }
  3181. static void cgroup_pidlist_stop(struct seq_file *s, void *v)
  3182. {
  3183. struct cgroup_pidlist *l = s->private;
  3184. up_read(&l->mutex);
  3185. }
  3186. static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
  3187. {
  3188. struct cgroup_pidlist *l = s->private;
  3189. pid_t *p = v;
  3190. pid_t *end = l->list + l->length;
  3191. /*
  3192. * Advance to the next pid in the array. If this goes off the
  3193. * end, we're done
  3194. */
  3195. p++;
  3196. if (p >= end) {
  3197. return NULL;
  3198. } else {
  3199. *pos = *p;
  3200. return p;
  3201. }
  3202. }
  3203. static int cgroup_pidlist_show(struct seq_file *s, void *v)
  3204. {
  3205. return seq_printf(s, "%d\n", *(int *)v);
  3206. }
  3207. /*
  3208. * seq_operations functions for iterating on pidlists through seq_file -
  3209. * independent of whether it's tasks or procs
  3210. */
  3211. static const struct seq_operations cgroup_pidlist_seq_operations = {
  3212. .start = cgroup_pidlist_start,
  3213. .stop = cgroup_pidlist_stop,
  3214. .next = cgroup_pidlist_next,
  3215. .show = cgroup_pidlist_show,
  3216. };
  3217. static void cgroup_release_pid_array(struct cgroup_pidlist *l)
  3218. {
  3219. /*
  3220. * the case where we're the last user of this particular pidlist will
  3221. * have us remove it from the cgroup's list, which entails taking the
  3222. * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
  3223. * pidlist_mutex, we have to take pidlist_mutex first.
  3224. */
  3225. mutex_lock(&l->owner->pidlist_mutex);
  3226. down_write(&l->mutex);
  3227. BUG_ON(!l->use_count);
  3228. if (!--l->use_count) {
  3229. /* we're the last user if refcount is 0; remove and free */
  3230. list_del(&l->links);
  3231. mutex_unlock(&l->owner->pidlist_mutex);
  3232. pidlist_free(l->list);
  3233. put_pid_ns(l->key.ns);
  3234. up_write(&l->mutex);
  3235. kfree(l);
  3236. return;
  3237. }
  3238. mutex_unlock(&l->owner->pidlist_mutex);
  3239. up_write(&l->mutex);
  3240. }
  3241. static int cgroup_pidlist_release(struct inode *inode, struct file *file)
  3242. {
  3243. struct cgroup_pidlist *l;
  3244. if (!(file->f_mode & FMODE_READ))
  3245. return 0;
  3246. /*
  3247. * the seq_file will only be initialized if the file was opened for
  3248. * reading; hence we check if it's not null only in that case.
  3249. */
  3250. l = ((struct seq_file *)file->private_data)->private;
  3251. cgroup_release_pid_array(l);
  3252. return seq_release(inode, file);
  3253. }
  3254. static const struct file_operations cgroup_pidlist_operations = {
  3255. .read = seq_read,
  3256. .llseek = seq_lseek,
  3257. .write = cgroup_file_write,
  3258. .release = cgroup_pidlist_release,
  3259. };
  3260. /*
  3261. * The following functions handle opens on a file that displays a pidlist
  3262. * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
  3263. * in the cgroup.
  3264. */
  3265. /* helper function for the two below it */
  3266. static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
  3267. {
  3268. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  3269. struct cgroup_pidlist *l;
  3270. int retval;
  3271. /* Nothing to do for write-only files */
  3272. if (!(file->f_mode & FMODE_READ))
  3273. return 0;
  3274. /* have the array populated */
  3275. retval = pidlist_array_load(cgrp, type, &l);
  3276. if (retval)
  3277. return retval;
  3278. /* configure file information */
  3279. file->f_op = &cgroup_pidlist_operations;
  3280. retval = seq_open(file, &cgroup_pidlist_seq_operations);
  3281. if (retval) {
  3282. cgroup_release_pid_array(l);
  3283. return retval;
  3284. }
  3285. ((struct seq_file *)file->private_data)->private = l;
  3286. return 0;
  3287. }
  3288. static int cgroup_tasks_open(struct inode *unused, struct file *file)
  3289. {
  3290. return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
  3291. }
  3292. static int cgroup_procs_open(struct inode *unused, struct file *file)
  3293. {
  3294. return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
  3295. }
  3296. static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
  3297. struct cftype *cft)
  3298. {
  3299. return notify_on_release(cgrp);
  3300. }
  3301. static int cgroup_write_notify_on_release(struct cgroup *cgrp,
  3302. struct cftype *cft,
  3303. u64 val)
  3304. {
  3305. clear_bit(CGRP_RELEASABLE, &cgrp->flags);
  3306. if (val)
  3307. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  3308. else
  3309. clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  3310. return 0;
  3311. }
  3312. /*
  3313. * Unregister event and free resources.
  3314. *
  3315. * Gets called from workqueue.
  3316. */
  3317. static void cgroup_event_remove(struct work_struct *work)
  3318. {
  3319. struct cgroup_event *event = container_of(work, struct cgroup_event,
  3320. remove);
  3321. struct cgroup *cgrp = event->cgrp;
  3322. event->cft->unregister_event(cgrp, event->cft, event->eventfd);
  3323. eventfd_ctx_put(event->eventfd);
  3324. kfree(event);
  3325. dput(cgrp->dentry);
  3326. }
  3327. /*
  3328. * Gets called on POLLHUP on eventfd when user closes it.
  3329. *
  3330. * Called with wqh->lock held and interrupts disabled.
  3331. */
  3332. static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
  3333. int sync, void *key)
  3334. {
  3335. struct cgroup_event *event = container_of(wait,
  3336. struct cgroup_event, wait);
  3337. struct cgroup *cgrp = event->cgrp;
  3338. unsigned long flags = (unsigned long)key;
  3339. if (flags & POLLHUP) {
  3340. __remove_wait_queue(event->wqh, &event->wait);
  3341. spin_lock(&cgrp->event_list_lock);
  3342. list_del_init(&event->list);
  3343. spin_unlock(&cgrp->event_list_lock);
  3344. /*
  3345. * We are in atomic context, but cgroup_event_remove() may
  3346. * sleep, so we have to call it in workqueue.
  3347. */
  3348. schedule_work(&event->remove);
  3349. }
  3350. return 0;
  3351. }
  3352. static void cgroup_event_ptable_queue_proc(struct file *file,
  3353. wait_queue_head_t *wqh, poll_table *pt)
  3354. {
  3355. struct cgroup_event *event = container_of(pt,
  3356. struct cgroup_event, pt);
  3357. event->wqh = wqh;
  3358. add_wait_queue(wqh, &event->wait);
  3359. }
  3360. /*
  3361. * Parse input and register new cgroup event handler.
  3362. *
  3363. * Input must be in format '<event_fd> <control_fd> <args>'.
  3364. * Interpretation of args is defined by control file implementation.
  3365. */
  3366. static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
  3367. const char *buffer)
  3368. {
  3369. struct cgroup_event *event = NULL;
  3370. unsigned int efd, cfd;
  3371. struct file *efile = NULL;
  3372. struct file *cfile = NULL;
  3373. char *endp;
  3374. int ret;
  3375. efd = simple_strtoul(buffer, &endp, 10);
  3376. if (*endp != ' ')
  3377. return -EINVAL;
  3378. buffer = endp + 1;
  3379. cfd = simple_strtoul(buffer, &endp, 10);
  3380. if ((*endp != ' ') && (*endp != '\0'))
  3381. return -EINVAL;
  3382. buffer = endp + 1;
  3383. event = kzalloc(sizeof(*event), GFP_KERNEL);
  3384. if (!event)
  3385. return -ENOMEM;
  3386. event->cgrp = cgrp;
  3387. INIT_LIST_HEAD(&event->list);
  3388. init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
  3389. init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
  3390. INIT_WORK(&event->remove, cgroup_event_remove);
  3391. efile = eventfd_fget(efd);
  3392. if (IS_ERR(efile)) {
  3393. ret = PTR_ERR(efile);
  3394. goto fail;
  3395. }
  3396. event->eventfd = eventfd_ctx_fileget(efile);
  3397. if (IS_ERR(event->eventfd)) {
  3398. ret = PTR_ERR(event->eventfd);
  3399. goto fail;
  3400. }
  3401. cfile = fget(cfd);
  3402. if (!cfile) {
  3403. ret = -EBADF;
  3404. goto fail;
  3405. }
  3406. /* the process need read permission on control file */
  3407. /* AV: shouldn't we check that it's been opened for read instead? */
  3408. ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ);
  3409. if (ret < 0)
  3410. goto fail;
  3411. event->cft = __file_cft(cfile);
  3412. if (IS_ERR(event->cft)) {
  3413. ret = PTR_ERR(event->cft);
  3414. goto fail;
  3415. }
  3416. if (!event->cft->register_event || !event->cft->unregister_event) {
  3417. ret = -EINVAL;
  3418. goto fail;
  3419. }
  3420. ret = event->cft->register_event(cgrp, event->cft,
  3421. event->eventfd, buffer);
  3422. if (ret)
  3423. goto fail;
  3424. if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
  3425. event->cft->unregister_event(cgrp, event->cft, event->eventfd);
  3426. ret = 0;
  3427. goto fail;
  3428. }
  3429. /*
  3430. * Events should be removed after rmdir of cgroup directory, but before
  3431. * destroying subsystem state objects. Let's take reference to cgroup
  3432. * directory dentry to do that.
  3433. */
  3434. dget(cgrp->dentry);
  3435. spin_lock(&cgrp->event_list_lock);
  3436. list_add(&event->list, &cgrp->event_list);
  3437. spin_unlock(&cgrp->event_list_lock);
  3438. fput(cfile);
  3439. fput(efile);
  3440. return 0;
  3441. fail:
  3442. if (cfile)
  3443. fput(cfile);
  3444. if (event && event->eventfd && !IS_ERR(event->eventfd))
  3445. eventfd_ctx_put(event->eventfd);
  3446. if (!IS_ERR_OR_NULL(efile))
  3447. fput(efile);
  3448. kfree(event);
  3449. return ret;
  3450. }
  3451. static u64 cgroup_clone_children_read(struct cgroup *cgrp,
  3452. struct cftype *cft)
  3453. {
  3454. return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
  3455. }
  3456. static int cgroup_clone_children_write(struct cgroup *cgrp,
  3457. struct cftype *cft,
  3458. u64 val)
  3459. {
  3460. if (val)
  3461. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
  3462. else
  3463. clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
  3464. return 0;
  3465. }
  3466. /*
  3467. * for the common functions, 'private' gives the type of file
  3468. */
  3469. /* for hysterical raisins, we can't put this on the older files */
  3470. #define CGROUP_FILE_GENERIC_PREFIX "cgroup."
  3471. static struct cftype files[] = {
  3472. {
  3473. .name = "tasks",
  3474. .open = cgroup_tasks_open,
  3475. .write_u64 = cgroup_tasks_write,
  3476. .release = cgroup_pidlist_release,
  3477. .mode = S_IRUGO | S_IWUSR,
  3478. },
  3479. {
  3480. .name = CGROUP_FILE_GENERIC_PREFIX "procs",
  3481. .open = cgroup_procs_open,
  3482. .write_u64 = cgroup_procs_write,
  3483. .release = cgroup_pidlist_release,
  3484. .mode = S_IRUGO | S_IWUSR,
  3485. },
  3486. {
  3487. .name = "notify_on_release",
  3488. .read_u64 = cgroup_read_notify_on_release,
  3489. .write_u64 = cgroup_write_notify_on_release,
  3490. },
  3491. {
  3492. .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
  3493. .write_string = cgroup_write_event_control,
  3494. .mode = S_IWUGO,
  3495. },
  3496. {
  3497. .name = "cgroup.clone_children",
  3498. .read_u64 = cgroup_clone_children_read,
  3499. .write_u64 = cgroup_clone_children_write,
  3500. },
  3501. {
  3502. .name = "release_agent",
  3503. .flags = CFTYPE_ONLY_ON_ROOT,
  3504. .read_seq_string = cgroup_release_agent_show,
  3505. .write_string = cgroup_release_agent_write,
  3506. .max_write_len = PATH_MAX,
  3507. },
  3508. { } /* terminate */
  3509. };
  3510. /**
  3511. * cgroup_populate_dir - selectively creation of files in a directory
  3512. * @cgrp: target cgroup
  3513. * @base_files: true if the base files should be added
  3514. * @subsys_mask: mask of the subsystem ids whose files should be added
  3515. */
  3516. static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
  3517. unsigned long subsys_mask)
  3518. {
  3519. int err;
  3520. struct cgroup_subsys *ss;
  3521. if (base_files) {
  3522. err = cgroup_addrm_files(cgrp, NULL, files, true);
  3523. if (err < 0)
  3524. return err;
  3525. }
  3526. /* process cftsets of each subsystem */
  3527. for_each_subsys(cgrp->root, ss) {
  3528. struct cftype_set *set;
  3529. if (!test_bit(ss->subsys_id, &subsys_mask))
  3530. continue;
  3531. list_for_each_entry(set, &ss->cftsets, node)
  3532. cgroup_addrm_files(cgrp, ss, set->cfts, true);
  3533. }
  3534. /* This cgroup is ready now */
  3535. for_each_subsys(cgrp->root, ss) {
  3536. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  3537. /*
  3538. * Update id->css pointer and make this css visible from
  3539. * CSS ID functions. This pointer will be dereferened
  3540. * from RCU-read-side without locks.
  3541. */
  3542. if (css->id)
  3543. rcu_assign_pointer(css->id->css, css);
  3544. }
  3545. return 0;
  3546. }
  3547. static void css_dput_fn(struct work_struct *work)
  3548. {
  3549. struct cgroup_subsys_state *css =
  3550. container_of(work, struct cgroup_subsys_state, dput_work);
  3551. struct dentry *dentry = css->cgroup->dentry;
  3552. struct super_block *sb = dentry->d_sb;
  3553. atomic_inc(&sb->s_active);
  3554. dput(dentry);
  3555. deactivate_super(sb);
  3556. }
  3557. static void init_cgroup_css(struct cgroup_subsys_state *css,
  3558. struct cgroup_subsys *ss,
  3559. struct cgroup *cgrp)
  3560. {
  3561. css->cgroup = cgrp;
  3562. atomic_set(&css->refcnt, 1);
  3563. css->flags = 0;
  3564. css->id = NULL;
  3565. if (cgrp == dummytop)
  3566. css->flags |= CSS_ROOT;
  3567. BUG_ON(cgrp->subsys[ss->subsys_id]);
  3568. cgrp->subsys[ss->subsys_id] = css;
  3569. /*
  3570. * css holds an extra ref to @cgrp->dentry which is put on the last
  3571. * css_put(). dput() requires process context, which css_put() may
  3572. * be called without. @css->dput_work will be used to invoke
  3573. * dput() asynchronously from css_put().
  3574. */
  3575. INIT_WORK(&css->dput_work, css_dput_fn);
  3576. }
  3577. /* invoke ->post_create() on a new CSS and mark it online if successful */
  3578. static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
  3579. {
  3580. int ret = 0;
  3581. lockdep_assert_held(&cgroup_mutex);
  3582. if (ss->css_online)
  3583. ret = ss->css_online(cgrp);
  3584. if (!ret)
  3585. cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
  3586. return ret;
  3587. }
  3588. /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
  3589. static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
  3590. __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
  3591. {
  3592. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  3593. lockdep_assert_held(&cgroup_mutex);
  3594. if (!(css->flags & CSS_ONLINE))
  3595. return;
  3596. /*
  3597. * css_offline() should be called with cgroup_mutex unlocked. See
  3598. * 3fa59dfbc3 ("cgroup: fix potential deadlock in pre_destroy") for
  3599. * details. This temporary unlocking should go away once
  3600. * cgroup_mutex is unexported from controllers.
  3601. */
  3602. if (ss->css_offline) {
  3603. mutex_unlock(&cgroup_mutex);
  3604. ss->css_offline(cgrp);
  3605. mutex_lock(&cgroup_mutex);
  3606. }
  3607. cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
  3608. }
  3609. /*
  3610. * cgroup_create - create a cgroup
  3611. * @parent: cgroup that will be parent of the new cgroup
  3612. * @dentry: dentry of the new cgroup
  3613. * @mode: mode to set on new inode
  3614. *
  3615. * Must be called with the mutex on the parent inode held
  3616. */
  3617. static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
  3618. umode_t mode)
  3619. {
  3620. struct cgroup *cgrp;
  3621. struct cgroupfs_root *root = parent->root;
  3622. int err = 0;
  3623. struct cgroup_subsys *ss;
  3624. struct super_block *sb = root->sb;
  3625. /* allocate the cgroup and its ID, 0 is reserved for the root */
  3626. cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
  3627. if (!cgrp)
  3628. return -ENOMEM;
  3629. cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
  3630. if (cgrp->id < 0)
  3631. goto err_free_cgrp;
  3632. /*
  3633. * Only live parents can have children. Note that the liveliness
  3634. * check isn't strictly necessary because cgroup_mkdir() and
  3635. * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
  3636. * anyway so that locking is contained inside cgroup proper and we
  3637. * don't get nasty surprises if we ever grow another caller.
  3638. */
  3639. if (!cgroup_lock_live_group(parent)) {
  3640. err = -ENODEV;
  3641. goto err_free_id;
  3642. }
  3643. /* Grab a reference on the superblock so the hierarchy doesn't
  3644. * get deleted on unmount if there are child cgroups. This
  3645. * can be done outside cgroup_mutex, since the sb can't
  3646. * disappear while someone has an open control file on the
  3647. * fs */
  3648. atomic_inc(&sb->s_active);
  3649. init_cgroup_housekeeping(cgrp);
  3650. cgrp->parent = parent;
  3651. cgrp->root = parent->root;
  3652. cgrp->top_cgroup = parent->top_cgroup;
  3653. if (notify_on_release(parent))
  3654. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  3655. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
  3656. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
  3657. for_each_subsys(root, ss) {
  3658. struct cgroup_subsys_state *css;
  3659. css = ss->css_alloc(cgrp);
  3660. if (IS_ERR(css)) {
  3661. err = PTR_ERR(css);
  3662. goto err_free_all;
  3663. }
  3664. init_cgroup_css(css, ss, cgrp);
  3665. if (ss->use_id) {
  3666. err = alloc_css_id(ss, parent, cgrp);
  3667. if (err)
  3668. goto err_free_all;
  3669. }
  3670. }
  3671. /*
  3672. * Create directory. cgroup_create_file() returns with the new
  3673. * directory locked on success so that it can be populated without
  3674. * dropping cgroup_mutex.
  3675. */
  3676. err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
  3677. if (err < 0)
  3678. goto err_free_all;
  3679. lockdep_assert_held(&dentry->d_inode->i_mutex);
  3680. /* allocation complete, commit to creation */
  3681. dentry->d_fsdata = cgrp;
  3682. cgrp->dentry = dentry;
  3683. list_add_tail(&cgrp->allcg_node, &root->allcg_list);
  3684. list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
  3685. root->number_of_cgroups++;
  3686. /* each css holds a ref to the cgroup's dentry */
  3687. for_each_subsys(root, ss)
  3688. dget(dentry);
  3689. /* creation succeeded, notify subsystems */
  3690. for_each_subsys(root, ss) {
  3691. err = online_css(ss, cgrp);
  3692. if (err)
  3693. goto err_destroy;
  3694. if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
  3695. parent->parent) {
  3696. pr_warning("cgroup: %s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
  3697. current->comm, current->pid, ss->name);
  3698. if (!strcmp(ss->name, "memory"))
  3699. pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
  3700. ss->warned_broken_hierarchy = true;
  3701. }
  3702. }
  3703. err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
  3704. if (err)
  3705. goto err_destroy;
  3706. mutex_unlock(&cgroup_mutex);
  3707. mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
  3708. return 0;
  3709. err_free_all:
  3710. for_each_subsys(root, ss) {
  3711. if (cgrp->subsys[ss->subsys_id])
  3712. ss->css_free(cgrp);
  3713. }
  3714. mutex_unlock(&cgroup_mutex);
  3715. /* Release the reference count that we took on the superblock */
  3716. deactivate_super(sb);
  3717. err_free_id:
  3718. ida_simple_remove(&root->cgroup_ida, cgrp->id);
  3719. err_free_cgrp:
  3720. kfree(cgrp);
  3721. return err;
  3722. err_destroy:
  3723. cgroup_destroy_locked(cgrp);
  3724. mutex_unlock(&cgroup_mutex);
  3725. mutex_unlock(&dentry->d_inode->i_mutex);
  3726. return err;
  3727. }
  3728. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  3729. {
  3730. struct cgroup *c_parent = dentry->d_parent->d_fsdata;
  3731. /* the vfs holds inode->i_mutex already */
  3732. return cgroup_create(c_parent, dentry, mode | S_IFDIR);
  3733. }
  3734. /*
  3735. * Check the reference count on each subsystem. Since we already
  3736. * established that there are no tasks in the cgroup, if the css refcount
  3737. * is also 1, then there should be no outstanding references, so the
  3738. * subsystem is safe to destroy. We scan across all subsystems rather than
  3739. * using the per-hierarchy linked list of mounted subsystems since we can
  3740. * be called via check_for_release() with no synchronization other than
  3741. * RCU, and the subsystem linked list isn't RCU-safe.
  3742. */
  3743. static int cgroup_has_css_refs(struct cgroup *cgrp)
  3744. {
  3745. int i;
  3746. /*
  3747. * We won't need to lock the subsys array, because the subsystems
  3748. * we're concerned about aren't going anywhere since our cgroup root
  3749. * has a reference on them.
  3750. */
  3751. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  3752. struct cgroup_subsys *ss = subsys[i];
  3753. struct cgroup_subsys_state *css;
  3754. /* Skip subsystems not present or not in this hierarchy */
  3755. if (ss == NULL || ss->root != cgrp->root)
  3756. continue;
  3757. css = cgrp->subsys[ss->subsys_id];
  3758. /*
  3759. * When called from check_for_release() it's possible
  3760. * that by this point the cgroup has been removed
  3761. * and the css deleted. But a false-positive doesn't
  3762. * matter, since it can only happen if the cgroup
  3763. * has been deleted and hence no longer needs the
  3764. * release agent to be called anyway.
  3765. */
  3766. if (css && css_refcnt(css) > 1)
  3767. return 1;
  3768. }
  3769. return 0;
  3770. }
  3771. static int cgroup_destroy_locked(struct cgroup *cgrp)
  3772. __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
  3773. {
  3774. struct dentry *d = cgrp->dentry;
  3775. struct cgroup *parent = cgrp->parent;
  3776. DEFINE_WAIT(wait);
  3777. struct cgroup_event *event, *tmp;
  3778. struct cgroup_subsys *ss;
  3779. LIST_HEAD(tmp_list);
  3780. lockdep_assert_held(&d->d_inode->i_mutex);
  3781. lockdep_assert_held(&cgroup_mutex);
  3782. if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
  3783. return -EBUSY;
  3784. /*
  3785. * Block new css_tryget() by deactivating refcnt and mark @cgrp
  3786. * removed. This makes future css_tryget() and child creation
  3787. * attempts fail thus maintaining the removal conditions verified
  3788. * above.
  3789. */
  3790. for_each_subsys(cgrp->root, ss) {
  3791. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  3792. WARN_ON(atomic_read(&css->refcnt) < 0);
  3793. atomic_add(CSS_DEACT_BIAS, &css->refcnt);
  3794. }
  3795. set_bit(CGRP_REMOVED, &cgrp->flags);
  3796. /* tell subsystems to initate destruction */
  3797. for_each_subsys(cgrp->root, ss)
  3798. offline_css(ss, cgrp);
  3799. /*
  3800. * Put all the base refs. Each css holds an extra reference to the
  3801. * cgroup's dentry and cgroup removal proceeds regardless of css
  3802. * refs. On the last put of each css, whenever that may be, the
  3803. * extra dentry ref is put so that dentry destruction happens only
  3804. * after all css's are released.
  3805. */
  3806. for_each_subsys(cgrp->root, ss)
  3807. css_put(cgrp->subsys[ss->subsys_id]);
  3808. raw_spin_lock(&release_list_lock);
  3809. if (!list_empty(&cgrp->release_list))
  3810. list_del_init(&cgrp->release_list);
  3811. raw_spin_unlock(&release_list_lock);
  3812. /* delete this cgroup from parent->children */
  3813. list_del_rcu(&cgrp->sibling);
  3814. list_del_init(&cgrp->allcg_node);
  3815. dget(d);
  3816. cgroup_d_remove_dir(d);
  3817. dput(d);
  3818. set_bit(CGRP_RELEASABLE, &parent->flags);
  3819. check_for_release(parent);
  3820. /*
  3821. * Unregister events and notify userspace.
  3822. * Notify userspace about cgroup removing only after rmdir of cgroup
  3823. * directory to avoid race between userspace and kernelspace. Use
  3824. * a temporary list to avoid a deadlock with cgroup_event_wake(). Since
  3825. * cgroup_event_wake() is called with the wait queue head locked,
  3826. * remove_wait_queue() cannot be called while holding event_list_lock.
  3827. */
  3828. spin_lock(&cgrp->event_list_lock);
  3829. list_splice_init(&cgrp->event_list, &tmp_list);
  3830. spin_unlock(&cgrp->event_list_lock);
  3831. list_for_each_entry_safe(event, tmp, &tmp_list, list) {
  3832. list_del_init(&event->list);
  3833. remove_wait_queue(event->wqh, &event->wait);
  3834. eventfd_signal(event->eventfd, 1);
  3835. schedule_work(&event->remove);
  3836. }
  3837. return 0;
  3838. }
  3839. static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
  3840. {
  3841. int ret;
  3842. mutex_lock(&cgroup_mutex);
  3843. ret = cgroup_destroy_locked(dentry->d_fsdata);
  3844. mutex_unlock(&cgroup_mutex);
  3845. return ret;
  3846. }
  3847. static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
  3848. {
  3849. INIT_LIST_HEAD(&ss->cftsets);
  3850. /*
  3851. * base_cftset is embedded in subsys itself, no need to worry about
  3852. * deregistration.
  3853. */
  3854. if (ss->base_cftypes) {
  3855. ss->base_cftset.cfts = ss->base_cftypes;
  3856. list_add_tail(&ss->base_cftset.node, &ss->cftsets);
  3857. }
  3858. }
  3859. static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
  3860. {
  3861. struct cgroup_subsys_state *css;
  3862. printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
  3863. mutex_lock(&cgroup_mutex);
  3864. /* init base cftset */
  3865. cgroup_init_cftsets(ss);
  3866. /* Create the top cgroup state for this subsystem */
  3867. list_add(&ss->sibling, &rootnode.subsys_list);
  3868. ss->root = &rootnode;
  3869. css = ss->css_alloc(dummytop);
  3870. /* We don't handle early failures gracefully */
  3871. BUG_ON(IS_ERR(css));
  3872. init_cgroup_css(css, ss, dummytop);
  3873. /* Update the init_css_set to contain a subsys
  3874. * pointer to this state - since the subsystem is
  3875. * newly registered, all tasks and hence the
  3876. * init_css_set is in the subsystem's top cgroup. */
  3877. init_css_set.subsys[ss->subsys_id] = css;
  3878. need_forkexit_callback |= ss->fork || ss->exit;
  3879. /* At system boot, before all subsystems have been
  3880. * registered, no tasks have been forked, so we don't
  3881. * need to invoke fork callbacks here. */
  3882. BUG_ON(!list_empty(&init_task.tasks));
  3883. ss->active = 1;
  3884. BUG_ON(online_css(ss, dummytop));
  3885. mutex_unlock(&cgroup_mutex);
  3886. /* this function shouldn't be used with modular subsystems, since they
  3887. * need to register a subsys_id, among other things */
  3888. BUG_ON(ss->module);
  3889. }
  3890. /**
  3891. * cgroup_load_subsys: load and register a modular subsystem at runtime
  3892. * @ss: the subsystem to load
  3893. *
  3894. * This function should be called in a modular subsystem's initcall. If the
  3895. * subsystem is built as a module, it will be assigned a new subsys_id and set
  3896. * up for use. If the subsystem is built-in anyway, work is delegated to the
  3897. * simpler cgroup_init_subsys.
  3898. */
  3899. int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
  3900. {
  3901. struct cgroup_subsys_state *css;
  3902. int i, ret;
  3903. /* check name and function validity */
  3904. if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
  3905. ss->css_alloc == NULL || ss->css_free == NULL)
  3906. return -EINVAL;
  3907. /*
  3908. * we don't support callbacks in modular subsystems. this check is
  3909. * before the ss->module check for consistency; a subsystem that could
  3910. * be a module should still have no callbacks even if the user isn't
  3911. * compiling it as one.
  3912. */
  3913. if (ss->fork || ss->exit)
  3914. return -EINVAL;
  3915. /*
  3916. * an optionally modular subsystem is built-in: we want to do nothing,
  3917. * since cgroup_init_subsys will have already taken care of it.
  3918. */
  3919. if (ss->module == NULL) {
  3920. /* a sanity check */
  3921. BUG_ON(subsys[ss->subsys_id] != ss);
  3922. return 0;
  3923. }
  3924. /* init base cftset */
  3925. cgroup_init_cftsets(ss);
  3926. mutex_lock(&cgroup_mutex);
  3927. subsys[ss->subsys_id] = ss;
  3928. /*
  3929. * no ss->css_alloc seems to need anything important in the ss
  3930. * struct, so this can happen first (i.e. before the rootnode
  3931. * attachment).
  3932. */
  3933. css = ss->css_alloc(dummytop);
  3934. if (IS_ERR(css)) {
  3935. /* failure case - need to deassign the subsys[] slot. */
  3936. subsys[ss->subsys_id] = NULL;
  3937. mutex_unlock(&cgroup_mutex);
  3938. return PTR_ERR(css);
  3939. }
  3940. list_add(&ss->sibling, &rootnode.subsys_list);
  3941. ss->root = &rootnode;
  3942. /* our new subsystem will be attached to the dummy hierarchy. */
  3943. init_cgroup_css(css, ss, dummytop);
  3944. /* init_idr must be after init_cgroup_css because it sets css->id. */
  3945. if (ss->use_id) {
  3946. ret = cgroup_init_idr(ss, css);
  3947. if (ret)
  3948. goto err_unload;
  3949. }
  3950. /*
  3951. * Now we need to entangle the css into the existing css_sets. unlike
  3952. * in cgroup_init_subsys, there are now multiple css_sets, so each one
  3953. * will need a new pointer to it; done by iterating the css_set_table.
  3954. * furthermore, modifying the existing css_sets will corrupt the hash
  3955. * table state, so each changed css_set will need its hash recomputed.
  3956. * this is all done under the css_set_lock.
  3957. */
  3958. write_lock(&css_set_lock);
  3959. for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
  3960. struct css_set *cg;
  3961. struct hlist_node *node, *tmp;
  3962. struct hlist_head *bucket = &css_set_table[i], *new_bucket;
  3963. hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
  3964. /* skip entries that we already rehashed */
  3965. if (cg->subsys[ss->subsys_id])
  3966. continue;
  3967. /* remove existing entry */
  3968. hlist_del(&cg->hlist);
  3969. /* set new value */
  3970. cg->subsys[ss->subsys_id] = css;
  3971. /* recompute hash and restore entry */
  3972. new_bucket = css_set_hash(cg->subsys);
  3973. hlist_add_head(&cg->hlist, new_bucket);
  3974. }
  3975. }
  3976. write_unlock(&css_set_lock);
  3977. ss->active = 1;
  3978. ret = online_css(ss, dummytop);
  3979. if (ret)
  3980. goto err_unload;
  3981. /* success! */
  3982. mutex_unlock(&cgroup_mutex);
  3983. return 0;
  3984. err_unload:
  3985. mutex_unlock(&cgroup_mutex);
  3986. /* @ss can't be mounted here as try_module_get() would fail */
  3987. cgroup_unload_subsys(ss);
  3988. return ret;
  3989. }
  3990. EXPORT_SYMBOL_GPL(cgroup_load_subsys);
  3991. /**
  3992. * cgroup_unload_subsys: unload a modular subsystem
  3993. * @ss: the subsystem to unload
  3994. *
  3995. * This function should be called in a modular subsystem's exitcall. When this
  3996. * function is invoked, the refcount on the subsystem's module will be 0, so
  3997. * the subsystem will not be attached to any hierarchy.
  3998. */
  3999. void cgroup_unload_subsys(struct cgroup_subsys *ss)
  4000. {
  4001. struct cg_cgroup_link *link;
  4002. struct hlist_head *hhead;
  4003. BUG_ON(ss->module == NULL);
  4004. /*
  4005. * we shouldn't be called if the subsystem is in use, and the use of
  4006. * try_module_get in parse_cgroupfs_options should ensure that it
  4007. * doesn't start being used while we're killing it off.
  4008. */
  4009. BUG_ON(ss->root != &rootnode);
  4010. mutex_lock(&cgroup_mutex);
  4011. offline_css(ss, dummytop);
  4012. ss->active = 0;
  4013. if (ss->use_id) {
  4014. idr_remove_all(&ss->idr);
  4015. idr_destroy(&ss->idr);
  4016. }
  4017. /* deassign the subsys_id */
  4018. subsys[ss->subsys_id] = NULL;
  4019. /* remove subsystem from rootnode's list of subsystems */
  4020. list_del_init(&ss->sibling);
  4021. /*
  4022. * disentangle the css from all css_sets attached to the dummytop. as
  4023. * in loading, we need to pay our respects to the hashtable gods.
  4024. */
  4025. write_lock(&css_set_lock);
  4026. list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
  4027. struct css_set *cg = link->cg;
  4028. hlist_del(&cg->hlist);
  4029. cg->subsys[ss->subsys_id] = NULL;
  4030. hhead = css_set_hash(cg->subsys);
  4031. hlist_add_head(&cg->hlist, hhead);
  4032. }
  4033. write_unlock(&css_set_lock);
  4034. /*
  4035. * remove subsystem's css from the dummytop and free it - need to
  4036. * free before marking as null because ss->css_free needs the
  4037. * cgrp->subsys pointer to find their state. note that this also
  4038. * takes care of freeing the css_id.
  4039. */
  4040. ss->css_free(dummytop);
  4041. dummytop->subsys[ss->subsys_id] = NULL;
  4042. mutex_unlock(&cgroup_mutex);
  4043. }
  4044. EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
  4045. /**
  4046. * cgroup_init_early - cgroup initialization at system boot
  4047. *
  4048. * Initialize cgroups at system boot, and initialize any
  4049. * subsystems that request early init.
  4050. */
  4051. int __init cgroup_init_early(void)
  4052. {
  4053. int i;
  4054. atomic_set(&init_css_set.refcount, 1);
  4055. INIT_LIST_HEAD(&init_css_set.cg_links);
  4056. INIT_LIST_HEAD(&init_css_set.tasks);
  4057. INIT_HLIST_NODE(&init_css_set.hlist);
  4058. css_set_count = 1;
  4059. init_cgroup_root(&rootnode);
  4060. root_count = 1;
  4061. init_task.cgroups = &init_css_set;
  4062. init_css_set_link.cg = &init_css_set;
  4063. init_css_set_link.cgrp = dummytop;
  4064. list_add(&init_css_set_link.cgrp_link_list,
  4065. &rootnode.top_cgroup.css_sets);
  4066. list_add(&init_css_set_link.cg_link_list,
  4067. &init_css_set.cg_links);
  4068. for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
  4069. INIT_HLIST_HEAD(&css_set_table[i]);
  4070. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  4071. struct cgroup_subsys *ss = subsys[i];
  4072. /* at bootup time, we don't worry about modular subsystems */
  4073. if (!ss || ss->module)
  4074. continue;
  4075. BUG_ON(!ss->name);
  4076. BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
  4077. BUG_ON(!ss->css_alloc);
  4078. BUG_ON(!ss->css_free);
  4079. if (ss->subsys_id != i) {
  4080. printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
  4081. ss->name, ss->subsys_id);
  4082. BUG();
  4083. }
  4084. if (ss->early_init)
  4085. cgroup_init_subsys(ss);
  4086. }
  4087. return 0;
  4088. }
  4089. /**
  4090. * cgroup_init - cgroup initialization
  4091. *
  4092. * Register cgroup filesystem and /proc file, and initialize
  4093. * any subsystems that didn't request early init.
  4094. */
  4095. int __init cgroup_init(void)
  4096. {
  4097. int err;
  4098. int i;
  4099. struct hlist_head *hhead;
  4100. err = bdi_init(&cgroup_backing_dev_info);
  4101. if (err)
  4102. return err;
  4103. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  4104. struct cgroup_subsys *ss = subsys[i];
  4105. /* at bootup time, we don't worry about modular subsystems */
  4106. if (!ss || ss->module)
  4107. continue;
  4108. if (!ss->early_init)
  4109. cgroup_init_subsys(ss);
  4110. if (ss->use_id)
  4111. cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
  4112. }
  4113. /* Add init_css_set to the hash table */
  4114. hhead = css_set_hash(init_css_set.subsys);
  4115. hlist_add_head(&init_css_set.hlist, hhead);
  4116. BUG_ON(!init_root_id(&rootnode));
  4117. cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
  4118. if (!cgroup_kobj) {
  4119. err = -ENOMEM;
  4120. goto out;
  4121. }
  4122. err = register_filesystem(&cgroup_fs_type);
  4123. if (err < 0) {
  4124. kobject_put(cgroup_kobj);
  4125. goto out;
  4126. }
  4127. proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
  4128. out:
  4129. if (err)
  4130. bdi_destroy(&cgroup_backing_dev_info);
  4131. return err;
  4132. }
  4133. /*
  4134. * proc_cgroup_show()
  4135. * - Print task's cgroup paths into seq_file, one line for each hierarchy
  4136. * - Used for /proc/<pid>/cgroup.
  4137. * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
  4138. * doesn't really matter if tsk->cgroup changes after we read it,
  4139. * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
  4140. * anyway. No need to check that tsk->cgroup != NULL, thanks to
  4141. * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
  4142. * cgroup to top_cgroup.
  4143. */
  4144. /* TODO: Use a proper seq_file iterator */
  4145. static int proc_cgroup_show(struct seq_file *m, void *v)
  4146. {
  4147. struct pid *pid;
  4148. struct task_struct *tsk;
  4149. char *buf;
  4150. int retval;
  4151. struct cgroupfs_root *root;
  4152. retval = -ENOMEM;
  4153. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  4154. if (!buf)
  4155. goto out;
  4156. retval = -ESRCH;
  4157. pid = m->private;
  4158. tsk = get_pid_task(pid, PIDTYPE_PID);
  4159. if (!tsk)
  4160. goto out_free;
  4161. retval = 0;
  4162. mutex_lock(&cgroup_mutex);
  4163. for_each_active_root(root) {
  4164. struct cgroup_subsys *ss;
  4165. struct cgroup *cgrp;
  4166. int count = 0;
  4167. seq_printf(m, "%d:", root->hierarchy_id);
  4168. for_each_subsys(root, ss)
  4169. seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
  4170. if (strlen(root->name))
  4171. seq_printf(m, "%sname=%s", count ? "," : "",
  4172. root->name);
  4173. seq_putc(m, ':');
  4174. cgrp = task_cgroup_from_root(tsk, root);
  4175. retval = cgroup_path(cgrp, buf, PAGE_SIZE);
  4176. if (retval < 0)
  4177. goto out_unlock;
  4178. seq_puts(m, buf);
  4179. seq_putc(m, '\n');
  4180. }
  4181. out_unlock:
  4182. mutex_unlock(&cgroup_mutex);
  4183. put_task_struct(tsk);
  4184. out_free:
  4185. kfree(buf);
  4186. out:
  4187. return retval;
  4188. }
  4189. static int cgroup_open(struct inode *inode, struct file *file)
  4190. {
  4191. struct pid *pid = PROC_I(inode)->pid;
  4192. return single_open(file, proc_cgroup_show, pid);
  4193. }
  4194. const struct file_operations proc_cgroup_operations = {
  4195. .open = cgroup_open,
  4196. .read = seq_read,
  4197. .llseek = seq_lseek,
  4198. .release = single_release,
  4199. };
  4200. /* Display information about each subsystem and each hierarchy */
  4201. static int proc_cgroupstats_show(struct seq_file *m, void *v)
  4202. {
  4203. int i;
  4204. seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
  4205. /*
  4206. * ideally we don't want subsystems moving around while we do this.
  4207. * cgroup_mutex is also necessary to guarantee an atomic snapshot of
  4208. * subsys/hierarchy state.
  4209. */
  4210. mutex_lock(&cgroup_mutex);
  4211. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  4212. struct cgroup_subsys *ss = subsys[i];
  4213. if (ss == NULL)
  4214. continue;
  4215. seq_printf(m, "%s\t%d\t%d\t%d\n",
  4216. ss->name, ss->root->hierarchy_id,
  4217. ss->root->number_of_cgroups, !ss->disabled);
  4218. }
  4219. mutex_unlock(&cgroup_mutex);
  4220. return 0;
  4221. }
  4222. static int cgroupstats_open(struct inode *inode, struct file *file)
  4223. {
  4224. return single_open(file, proc_cgroupstats_show, NULL);
  4225. }
  4226. static const struct file_operations proc_cgroupstats_operations = {
  4227. .open = cgroupstats_open,
  4228. .read = seq_read,
  4229. .llseek = seq_lseek,
  4230. .release = single_release,
  4231. };
  4232. /**
  4233. * cgroup_fork - attach newly forked task to its parents cgroup.
  4234. * @child: pointer to task_struct of forking parent process.
  4235. *
  4236. * Description: A task inherits its parent's cgroup at fork().
  4237. *
  4238. * A pointer to the shared css_set was automatically copied in
  4239. * fork.c by dup_task_struct(). However, we ignore that copy, since
  4240. * it was not made under the protection of RCU or cgroup_mutex, so
  4241. * might no longer be a valid cgroup pointer. cgroup_attach_task() might
  4242. * have already changed current->cgroups, allowing the previously
  4243. * referenced cgroup group to be removed and freed.
  4244. *
  4245. * At the point that cgroup_fork() is called, 'current' is the parent
  4246. * task, and the passed argument 'child' points to the child task.
  4247. */
  4248. void cgroup_fork(struct task_struct *child)
  4249. {
  4250. task_lock(current);
  4251. child->cgroups = current->cgroups;
  4252. get_css_set(child->cgroups);
  4253. task_unlock(current);
  4254. INIT_LIST_HEAD(&child->cg_list);
  4255. }
  4256. /**
  4257. * cgroup_post_fork - called on a new task after adding it to the task list
  4258. * @child: the task in question
  4259. *
  4260. * Adds the task to the list running through its css_set if necessary and
  4261. * call the subsystem fork() callbacks. Has to be after the task is
  4262. * visible on the task list in case we race with the first call to
  4263. * cgroup_iter_start() - to guarantee that the new task ends up on its
  4264. * list.
  4265. */
  4266. void cgroup_post_fork(struct task_struct *child)
  4267. {
  4268. int i;
  4269. /*
  4270. * use_task_css_set_links is set to 1 before we walk the tasklist
  4271. * under the tasklist_lock and we read it here after we added the child
  4272. * to the tasklist under the tasklist_lock as well. If the child wasn't
  4273. * yet in the tasklist when we walked through it from
  4274. * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
  4275. * should be visible now due to the paired locking and barriers implied
  4276. * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
  4277. * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
  4278. * lock on fork.
  4279. */
  4280. if (use_task_css_set_links) {
  4281. write_lock(&css_set_lock);
  4282. task_lock(child);
  4283. if (list_empty(&child->cg_list))
  4284. list_add(&child->cg_list, &child->cgroups->tasks);
  4285. task_unlock(child);
  4286. write_unlock(&css_set_lock);
  4287. }
  4288. /*
  4289. * Call ss->fork(). This must happen after @child is linked on
  4290. * css_set; otherwise, @child might change state between ->fork()
  4291. * and addition to css_set.
  4292. */
  4293. if (need_forkexit_callback) {
  4294. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  4295. struct cgroup_subsys *ss = subsys[i];
  4296. /*
  4297. * fork/exit callbacks are supported only for
  4298. * builtin subsystems and we don't need further
  4299. * synchronization as they never go away.
  4300. */
  4301. if (!ss || ss->module)
  4302. continue;
  4303. if (ss->fork)
  4304. ss->fork(child);
  4305. }
  4306. }
  4307. }
  4308. /**
  4309. * cgroup_exit - detach cgroup from exiting task
  4310. * @tsk: pointer to task_struct of exiting process
  4311. * @run_callback: run exit callbacks?
  4312. *
  4313. * Description: Detach cgroup from @tsk and release it.
  4314. *
  4315. * Note that cgroups marked notify_on_release force every task in
  4316. * them to take the global cgroup_mutex mutex when exiting.
  4317. * This could impact scaling on very large systems. Be reluctant to
  4318. * use notify_on_release cgroups where very high task exit scaling
  4319. * is required on large systems.
  4320. *
  4321. * the_top_cgroup_hack:
  4322. *
  4323. * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
  4324. *
  4325. * We call cgroup_exit() while the task is still competent to
  4326. * handle notify_on_release(), then leave the task attached to the
  4327. * root cgroup in each hierarchy for the remainder of its exit.
  4328. *
  4329. * To do this properly, we would increment the reference count on
  4330. * top_cgroup, and near the very end of the kernel/exit.c do_exit()
  4331. * code we would add a second cgroup function call, to drop that
  4332. * reference. This would just create an unnecessary hot spot on
  4333. * the top_cgroup reference count, to no avail.
  4334. *
  4335. * Normally, holding a reference to a cgroup without bumping its
  4336. * count is unsafe. The cgroup could go away, or someone could
  4337. * attach us to a different cgroup, decrementing the count on
  4338. * the first cgroup that we never incremented. But in this case,
  4339. * top_cgroup isn't going away, and either task has PF_EXITING set,
  4340. * which wards off any cgroup_attach_task() attempts, or task is a failed
  4341. * fork, never visible to cgroup_attach_task.
  4342. */
  4343. void cgroup_exit(struct task_struct *tsk, int run_callbacks)
  4344. {
  4345. struct css_set *cg;
  4346. int i;
  4347. /*
  4348. * Unlink from the css_set task list if necessary.
  4349. * Optimistically check cg_list before taking
  4350. * css_set_lock
  4351. */
  4352. if (!list_empty(&tsk->cg_list)) {
  4353. write_lock(&css_set_lock);
  4354. if (!list_empty(&tsk->cg_list))
  4355. list_del_init(&tsk->cg_list);
  4356. write_unlock(&css_set_lock);
  4357. }
  4358. /* Reassign the task to the init_css_set. */
  4359. task_lock(tsk);
  4360. cg = tsk->cgroups;
  4361. tsk->cgroups = &init_css_set;
  4362. if (run_callbacks && need_forkexit_callback) {
  4363. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  4364. struct cgroup_subsys *ss = subsys[i];
  4365. /* modular subsystems can't use callbacks */
  4366. if (!ss || ss->module)
  4367. continue;
  4368. if (ss->exit) {
  4369. struct cgroup *old_cgrp =
  4370. rcu_dereference_raw(cg->subsys[i])->cgroup;
  4371. struct cgroup *cgrp = task_cgroup(tsk, i);
  4372. ss->exit(cgrp, old_cgrp, tsk);
  4373. }
  4374. }
  4375. }
  4376. task_unlock(tsk);
  4377. if (cg)
  4378. put_css_set_taskexit(cg);
  4379. }
  4380. /**
  4381. * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
  4382. * @cgrp: the cgroup in question
  4383. * @task: the task in question
  4384. *
  4385. * See if @cgrp is a descendant of @task's cgroup in the appropriate
  4386. * hierarchy.
  4387. *
  4388. * If we are sending in dummytop, then presumably we are creating
  4389. * the top cgroup in the subsystem.
  4390. *
  4391. * Called only by the ns (nsproxy) cgroup.
  4392. */
  4393. int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
  4394. {
  4395. int ret;
  4396. struct cgroup *target;
  4397. if (cgrp == dummytop)
  4398. return 1;
  4399. target = task_cgroup_from_root(task, cgrp->root);
  4400. while (cgrp != target && cgrp!= cgrp->top_cgroup)
  4401. cgrp = cgrp->parent;
  4402. ret = (cgrp == target);
  4403. return ret;
  4404. }
  4405. static void check_for_release(struct cgroup *cgrp)
  4406. {
  4407. /* All of these checks rely on RCU to keep the cgroup
  4408. * structure alive */
  4409. if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
  4410. && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
  4411. /* Control Group is currently removeable. If it's not
  4412. * already queued for a userspace notification, queue
  4413. * it now */
  4414. int need_schedule_work = 0;
  4415. raw_spin_lock(&release_list_lock);
  4416. if (!cgroup_is_removed(cgrp) &&
  4417. list_empty(&cgrp->release_list)) {
  4418. list_add(&cgrp->release_list, &release_list);
  4419. need_schedule_work = 1;
  4420. }
  4421. raw_spin_unlock(&release_list_lock);
  4422. if (need_schedule_work)
  4423. schedule_work(&release_agent_work);
  4424. }
  4425. }
  4426. /* Caller must verify that the css is not for root cgroup */
  4427. bool __css_tryget(struct cgroup_subsys_state *css)
  4428. {
  4429. while (true) {
  4430. int t, v;
  4431. v = css_refcnt(css);
  4432. t = atomic_cmpxchg(&css->refcnt, v, v + 1);
  4433. if (likely(t == v))
  4434. return true;
  4435. else if (t < 0)
  4436. return false;
  4437. cpu_relax();
  4438. }
  4439. }
  4440. EXPORT_SYMBOL_GPL(__css_tryget);
  4441. /* Caller must verify that the css is not for root cgroup */
  4442. void __css_put(struct cgroup_subsys_state *css)
  4443. {
  4444. struct cgroup *cgrp = css->cgroup;
  4445. int v;
  4446. rcu_read_lock();
  4447. v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
  4448. switch (v) {
  4449. case 1:
  4450. if (notify_on_release(cgrp)) {
  4451. set_bit(CGRP_RELEASABLE, &cgrp->flags);
  4452. check_for_release(cgrp);
  4453. }
  4454. break;
  4455. case 0:
  4456. schedule_work(&css->dput_work);
  4457. break;
  4458. }
  4459. rcu_read_unlock();
  4460. }
  4461. EXPORT_SYMBOL_GPL(__css_put);
  4462. /*
  4463. * Notify userspace when a cgroup is released, by running the
  4464. * configured release agent with the name of the cgroup (path
  4465. * relative to the root of cgroup file system) as the argument.
  4466. *
  4467. * Most likely, this user command will try to rmdir this cgroup.
  4468. *
  4469. * This races with the possibility that some other task will be
  4470. * attached to this cgroup before it is removed, or that some other
  4471. * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
  4472. * The presumed 'rmdir' will fail quietly if this cgroup is no longer
  4473. * unused, and this cgroup will be reprieved from its death sentence,
  4474. * to continue to serve a useful existence. Next time it's released,
  4475. * we will get notified again, if it still has 'notify_on_release' set.
  4476. *
  4477. * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
  4478. * means only wait until the task is successfully execve()'d. The
  4479. * separate release agent task is forked by call_usermodehelper(),
  4480. * then control in this thread returns here, without waiting for the
  4481. * release agent task. We don't bother to wait because the caller of
  4482. * this routine has no use for the exit status of the release agent
  4483. * task, so no sense holding our caller up for that.
  4484. */
  4485. static void cgroup_release_agent(struct work_struct *work)
  4486. {
  4487. BUG_ON(work != &release_agent_work);
  4488. mutex_lock(&cgroup_mutex);
  4489. raw_spin_lock(&release_list_lock);
  4490. while (!list_empty(&release_list)) {
  4491. char *argv[3], *envp[3];
  4492. int i;
  4493. char *pathbuf = NULL, *agentbuf = NULL;
  4494. struct cgroup *cgrp = list_entry(release_list.next,
  4495. struct cgroup,
  4496. release_list);
  4497. list_del_init(&cgrp->release_list);
  4498. raw_spin_unlock(&release_list_lock);
  4499. pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  4500. if (!pathbuf)
  4501. goto continue_free;
  4502. if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
  4503. goto continue_free;
  4504. agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
  4505. if (!agentbuf)
  4506. goto continue_free;
  4507. i = 0;
  4508. argv[i++] = agentbuf;
  4509. argv[i++] = pathbuf;
  4510. argv[i] = NULL;
  4511. i = 0;
  4512. /* minimal command environment */
  4513. envp[i++] = "HOME=/";
  4514. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  4515. envp[i] = NULL;
  4516. /* Drop the lock while we invoke the usermode helper,
  4517. * since the exec could involve hitting disk and hence
  4518. * be a slow process */
  4519. mutex_unlock(&cgroup_mutex);
  4520. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  4521. mutex_lock(&cgroup_mutex);
  4522. continue_free:
  4523. kfree(pathbuf);
  4524. kfree(agentbuf);
  4525. raw_spin_lock(&release_list_lock);
  4526. }
  4527. raw_spin_unlock(&release_list_lock);
  4528. mutex_unlock(&cgroup_mutex);
  4529. }
  4530. static int __init cgroup_disable(char *str)
  4531. {
  4532. int i;
  4533. char *token;
  4534. while ((token = strsep(&str, ",")) != NULL) {
  4535. if (!*token)
  4536. continue;
  4537. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  4538. struct cgroup_subsys *ss = subsys[i];
  4539. /*
  4540. * cgroup_disable, being at boot time, can't
  4541. * know about module subsystems, so we don't
  4542. * worry about them.
  4543. */
  4544. if (!ss || ss->module)
  4545. continue;
  4546. if (!strcmp(token, ss->name)) {
  4547. ss->disabled = 1;
  4548. printk(KERN_INFO "Disabling %s control group"
  4549. " subsystem\n", ss->name);
  4550. break;
  4551. }
  4552. }
  4553. }
  4554. return 1;
  4555. }
  4556. __setup("cgroup_disable=", cgroup_disable);
  4557. /*
  4558. * Functons for CSS ID.
  4559. */
  4560. /*
  4561. *To get ID other than 0, this should be called when !cgroup_is_removed().
  4562. */
  4563. unsigned short css_id(struct cgroup_subsys_state *css)
  4564. {
  4565. struct css_id *cssid;
  4566. /*
  4567. * This css_id() can return correct value when somone has refcnt
  4568. * on this or this is under rcu_read_lock(). Once css->id is allocated,
  4569. * it's unchanged until freed.
  4570. */
  4571. cssid = rcu_dereference_check(css->id, css_refcnt(css));
  4572. if (cssid)
  4573. return cssid->id;
  4574. return 0;
  4575. }
  4576. EXPORT_SYMBOL_GPL(css_id);
  4577. unsigned short css_depth(struct cgroup_subsys_state *css)
  4578. {
  4579. struct css_id *cssid;
  4580. cssid = rcu_dereference_check(css->id, css_refcnt(css));
  4581. if (cssid)
  4582. return cssid->depth;
  4583. return 0;
  4584. }
  4585. EXPORT_SYMBOL_GPL(css_depth);
  4586. /**
  4587. * css_is_ancestor - test "root" css is an ancestor of "child"
  4588. * @child: the css to be tested.
  4589. * @root: the css supporsed to be an ancestor of the child.
  4590. *
  4591. * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
  4592. * this function reads css->id, the caller must hold rcu_read_lock().
  4593. * But, considering usual usage, the csses should be valid objects after test.
  4594. * Assuming that the caller will do some action to the child if this returns
  4595. * returns true, the caller must take "child";s reference count.
  4596. * If "child" is valid object and this returns true, "root" is valid, too.
  4597. */
  4598. bool css_is_ancestor(struct cgroup_subsys_state *child,
  4599. const struct cgroup_subsys_state *root)
  4600. {
  4601. struct css_id *child_id;
  4602. struct css_id *root_id;
  4603. child_id = rcu_dereference(child->id);
  4604. if (!child_id)
  4605. return false;
  4606. root_id = rcu_dereference(root->id);
  4607. if (!root_id)
  4608. return false;
  4609. if (child_id->depth < root_id->depth)
  4610. return false;
  4611. if (child_id->stack[root_id->depth] != root_id->id)
  4612. return false;
  4613. return true;
  4614. }
  4615. void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
  4616. {
  4617. struct css_id *id = css->id;
  4618. /* When this is called before css_id initialization, id can be NULL */
  4619. if (!id)
  4620. return;
  4621. BUG_ON(!ss->use_id);
  4622. rcu_assign_pointer(id->css, NULL);
  4623. rcu_assign_pointer(css->id, NULL);
  4624. spin_lock(&ss->id_lock);
  4625. idr_remove(&ss->idr, id->id);
  4626. spin_unlock(&ss->id_lock);
  4627. kfree_rcu(id, rcu_head);
  4628. }
  4629. EXPORT_SYMBOL_GPL(free_css_id);
  4630. /*
  4631. * This is called by init or create(). Then, calls to this function are
  4632. * always serialized (By cgroup_mutex() at create()).
  4633. */
  4634. static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
  4635. {
  4636. struct css_id *newid;
  4637. int myid, error, size;
  4638. BUG_ON(!ss->use_id);
  4639. size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
  4640. newid = kzalloc(size, GFP_KERNEL);
  4641. if (!newid)
  4642. return ERR_PTR(-ENOMEM);
  4643. /* get id */
  4644. if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
  4645. error = -ENOMEM;
  4646. goto err_out;
  4647. }
  4648. spin_lock(&ss->id_lock);
  4649. /* Don't use 0. allocates an ID of 1-65535 */
  4650. error = idr_get_new_above(&ss->idr, newid, 1, &myid);
  4651. spin_unlock(&ss->id_lock);
  4652. /* Returns error when there are no free spaces for new ID.*/
  4653. if (error) {
  4654. error = -ENOSPC;
  4655. goto err_out;
  4656. }
  4657. if (myid > CSS_ID_MAX)
  4658. goto remove_idr;
  4659. newid->id = myid;
  4660. newid->depth = depth;
  4661. return newid;
  4662. remove_idr:
  4663. error = -ENOSPC;
  4664. spin_lock(&ss->id_lock);
  4665. idr_remove(&ss->idr, myid);
  4666. spin_unlock(&ss->id_lock);
  4667. err_out:
  4668. kfree(newid);
  4669. return ERR_PTR(error);
  4670. }
  4671. static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
  4672. struct cgroup_subsys_state *rootcss)
  4673. {
  4674. struct css_id *newid;
  4675. spin_lock_init(&ss->id_lock);
  4676. idr_init(&ss->idr);
  4677. newid = get_new_cssid(ss, 0);
  4678. if (IS_ERR(newid))
  4679. return PTR_ERR(newid);
  4680. newid->stack[0] = newid->id;
  4681. newid->css = rootcss;
  4682. rootcss->id = newid;
  4683. return 0;
  4684. }
  4685. static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
  4686. struct cgroup *child)
  4687. {
  4688. int subsys_id, i, depth = 0;
  4689. struct cgroup_subsys_state *parent_css, *child_css;
  4690. struct css_id *child_id, *parent_id;
  4691. subsys_id = ss->subsys_id;
  4692. parent_css = parent->subsys[subsys_id];
  4693. child_css = child->subsys[subsys_id];
  4694. parent_id = parent_css->id;
  4695. depth = parent_id->depth + 1;
  4696. child_id = get_new_cssid(ss, depth);
  4697. if (IS_ERR(child_id))
  4698. return PTR_ERR(child_id);
  4699. for (i = 0; i < depth; i++)
  4700. child_id->stack[i] = parent_id->stack[i];
  4701. child_id->stack[depth] = child_id->id;
  4702. /*
  4703. * child_id->css pointer will be set after this cgroup is available
  4704. * see cgroup_populate_dir()
  4705. */
  4706. rcu_assign_pointer(child_css->id, child_id);
  4707. return 0;
  4708. }
  4709. /**
  4710. * css_lookup - lookup css by id
  4711. * @ss: cgroup subsys to be looked into.
  4712. * @id: the id
  4713. *
  4714. * Returns pointer to cgroup_subsys_state if there is valid one with id.
  4715. * NULL if not. Should be called under rcu_read_lock()
  4716. */
  4717. struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
  4718. {
  4719. struct css_id *cssid = NULL;
  4720. BUG_ON(!ss->use_id);
  4721. cssid = idr_find(&ss->idr, id);
  4722. if (unlikely(!cssid))
  4723. return NULL;
  4724. return rcu_dereference(cssid->css);
  4725. }
  4726. EXPORT_SYMBOL_GPL(css_lookup);
  4727. /**
  4728. * css_get_next - lookup next cgroup under specified hierarchy.
  4729. * @ss: pointer to subsystem
  4730. * @id: current position of iteration.
  4731. * @root: pointer to css. search tree under this.
  4732. * @foundid: position of found object.
  4733. *
  4734. * Search next css under the specified hierarchy of rootid. Calling under
  4735. * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
  4736. */
  4737. struct cgroup_subsys_state *
  4738. css_get_next(struct cgroup_subsys *ss, int id,
  4739. struct cgroup_subsys_state *root, int *foundid)
  4740. {
  4741. struct cgroup_subsys_state *ret = NULL;
  4742. struct css_id *tmp;
  4743. int tmpid;
  4744. int rootid = css_id(root);
  4745. int depth = css_depth(root);
  4746. if (!rootid)
  4747. return NULL;
  4748. BUG_ON(!ss->use_id);
  4749. WARN_ON_ONCE(!rcu_read_lock_held());
  4750. /* fill start point for scan */
  4751. tmpid = id;
  4752. while (1) {
  4753. /*
  4754. * scan next entry from bitmap(tree), tmpid is updated after
  4755. * idr_get_next().
  4756. */
  4757. tmp = idr_get_next(&ss->idr, &tmpid);
  4758. if (!tmp)
  4759. break;
  4760. if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
  4761. ret = rcu_dereference(tmp->css);
  4762. if (ret) {
  4763. *foundid = tmpid;
  4764. break;
  4765. }
  4766. }
  4767. /* continue to scan from next id */
  4768. tmpid = tmpid + 1;
  4769. }
  4770. return ret;
  4771. }
  4772. /*
  4773. * get corresponding css from file open on cgroupfs directory
  4774. */
  4775. struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
  4776. {
  4777. struct cgroup *cgrp;
  4778. struct inode *inode;
  4779. struct cgroup_subsys_state *css;
  4780. inode = f->f_dentry->d_inode;
  4781. /* check in cgroup filesystem dir */
  4782. if (inode->i_op != &cgroup_dir_inode_operations)
  4783. return ERR_PTR(-EBADF);
  4784. if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
  4785. return ERR_PTR(-EINVAL);
  4786. /* get cgroup */
  4787. cgrp = __d_cgrp(f->f_dentry);
  4788. css = cgrp->subsys[id];
  4789. return css ? css : ERR_PTR(-ENOENT);
  4790. }
  4791. #ifdef CONFIG_CGROUP_DEBUG
  4792. static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
  4793. {
  4794. struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
  4795. if (!css)
  4796. return ERR_PTR(-ENOMEM);
  4797. return css;
  4798. }
  4799. static void debug_css_free(struct cgroup *cont)
  4800. {
  4801. kfree(cont->subsys[debug_subsys_id]);
  4802. }
  4803. static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
  4804. {
  4805. return atomic_read(&cont->count);
  4806. }
  4807. static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
  4808. {
  4809. return cgroup_task_count(cont);
  4810. }
  4811. static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
  4812. {
  4813. return (u64)(unsigned long)current->cgroups;
  4814. }
  4815. static u64 current_css_set_refcount_read(struct cgroup *cont,
  4816. struct cftype *cft)
  4817. {
  4818. u64 count;
  4819. rcu_read_lock();
  4820. count = atomic_read(&current->cgroups->refcount);
  4821. rcu_read_unlock();
  4822. return count;
  4823. }
  4824. static int current_css_set_cg_links_read(struct cgroup *cont,
  4825. struct cftype *cft,
  4826. struct seq_file *seq)
  4827. {
  4828. struct cg_cgroup_link *link;
  4829. struct css_set *cg;
  4830. read_lock(&css_set_lock);
  4831. rcu_read_lock();
  4832. cg = rcu_dereference(current->cgroups);
  4833. list_for_each_entry(link, &cg->cg_links, cg_link_list) {
  4834. struct cgroup *c = link->cgrp;
  4835. const char *name;
  4836. if (c->dentry)
  4837. name = c->dentry->d_name.name;
  4838. else
  4839. name = "?";
  4840. seq_printf(seq, "Root %d group %s\n",
  4841. c->root->hierarchy_id, name);
  4842. }
  4843. rcu_read_unlock();
  4844. read_unlock(&css_set_lock);
  4845. return 0;
  4846. }
  4847. #define MAX_TASKS_SHOWN_PER_CSS 25
  4848. static int cgroup_css_links_read(struct cgroup *cont,
  4849. struct cftype *cft,
  4850. struct seq_file *seq)
  4851. {
  4852. struct cg_cgroup_link *link;
  4853. read_lock(&css_set_lock);
  4854. list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
  4855. struct css_set *cg = link->cg;
  4856. struct task_struct *task;
  4857. int count = 0;
  4858. seq_printf(seq, "css_set %p\n", cg);
  4859. list_for_each_entry(task, &cg->tasks, cg_list) {
  4860. if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
  4861. seq_puts(seq, " ...\n");
  4862. break;
  4863. } else {
  4864. seq_printf(seq, " task %d\n",
  4865. task_pid_vnr(task));
  4866. }
  4867. }
  4868. }
  4869. read_unlock(&css_set_lock);
  4870. return 0;
  4871. }
  4872. static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
  4873. {
  4874. return test_bit(CGRP_RELEASABLE, &cgrp->flags);
  4875. }
  4876. static struct cftype debug_files[] = {
  4877. {
  4878. .name = "cgroup_refcount",
  4879. .read_u64 = cgroup_refcount_read,
  4880. },
  4881. {
  4882. .name = "taskcount",
  4883. .read_u64 = debug_taskcount_read,
  4884. },
  4885. {
  4886. .name = "current_css_set",
  4887. .read_u64 = current_css_set_read,
  4888. },
  4889. {
  4890. .name = "current_css_set_refcount",
  4891. .read_u64 = current_css_set_refcount_read,
  4892. },
  4893. {
  4894. .name = "current_css_set_cg_links",
  4895. .read_seq_string = current_css_set_cg_links_read,
  4896. },
  4897. {
  4898. .name = "cgroup_css_links",
  4899. .read_seq_string = cgroup_css_links_read,
  4900. },
  4901. {
  4902. .name = "releasable",
  4903. .read_u64 = releasable_read,
  4904. },
  4905. { } /* terminate */
  4906. };
  4907. struct cgroup_subsys debug_subsys = {
  4908. .name = "debug",
  4909. .css_alloc = debug_css_alloc,
  4910. .css_free = debug_css_free,
  4911. .subsys_id = debug_subsys_id,
  4912. .base_cftypes = debug_files,
  4913. };
  4914. #endif /* CONFIG_CGROUP_DEBUG */