cgroup.c 143 KB

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