cgroup.c 61 KB

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