cgroup.c 125 KB

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