cgroup.c 103 KB

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