cgroup.c 147 KB

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