cgroup.c 146 KB

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