cgroup.c 147 KB

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