cgroup.c 93 KB

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