cgroup.c 82 KB

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