cgroup.c 72 KB

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