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