cgroup.c 82 KB

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