cgroup.c 109 KB

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