cgroup.c 146 KB

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