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