cgroup.c 151 KB

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