cgroup.c 143 KB

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