cgroup.c 82 KB

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