cgroup.c 142 KB

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