cgroup.c 147 KB

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