cgroup.c 147 KB

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