cgroup.c 152 KB

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