cgroup.c 148 KB

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