cgroup.c 148 KB

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