cgroup.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  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/rcupdate.h>
  36. #include <linux/sched.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/slab.h>
  39. #include <linux/magic.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/string.h>
  42. #include <linux/sort.h>
  43. #include <asm/atomic.h>
  44. /* Generate an array of cgroup subsystem pointers */
  45. #define SUBSYS(_x) &_x ## _subsys,
  46. static struct cgroup_subsys *subsys[] = {
  47. #include <linux/cgroup_subsys.h>
  48. };
  49. /*
  50. * A cgroupfs_root represents the root of a cgroup hierarchy,
  51. * and may be associated with a superblock to form an active
  52. * hierarchy
  53. */
  54. struct cgroupfs_root {
  55. struct super_block *sb;
  56. /*
  57. * The bitmask of subsystems intended to be attached to this
  58. * hierarchy
  59. */
  60. unsigned long subsys_bits;
  61. /* The bitmask of subsystems currently attached to this hierarchy */
  62. unsigned long actual_subsys_bits;
  63. /* A list running through the attached subsystems */
  64. struct list_head subsys_list;
  65. /* The root cgroup for this hierarchy */
  66. struct cgroup top_cgroup;
  67. /* Tracks how many cgroups are currently defined in hierarchy.*/
  68. int number_of_cgroups;
  69. /* A list running through the mounted hierarchies */
  70. struct list_head root_list;
  71. /* Hierarchy-specific flags */
  72. unsigned long flags;
  73. };
  74. /*
  75. * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
  76. * subsystems that are otherwise unattached - it never has more than a
  77. * single cgroup, and all tasks are part of that cgroup.
  78. */
  79. static struct cgroupfs_root rootnode;
  80. /* The list of hierarchy roots */
  81. static LIST_HEAD(roots);
  82. /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
  83. #define dummytop (&rootnode.top_cgroup)
  84. /* This flag indicates whether tasks in the fork and exit paths should
  85. * take callback_mutex and check for fork/exit handlers to call. This
  86. * avoids us having to do extra work in the fork/exit path if none of the
  87. * subsystems need to be called.
  88. */
  89. static int need_forkexit_callback;
  90. /* bits in struct cgroup flags field */
  91. enum {
  92. CONT_REMOVED,
  93. };
  94. /* convenient tests for these bits */
  95. inline int cgroup_is_removed(const struct cgroup *cont)
  96. {
  97. return test_bit(CONT_REMOVED, &cont->flags);
  98. }
  99. /* bits in struct cgroupfs_root flags field */
  100. enum {
  101. ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
  102. };
  103. /*
  104. * for_each_subsys() allows you to iterate on each subsystem attached to
  105. * an active hierarchy
  106. */
  107. #define for_each_subsys(_root, _ss) \
  108. list_for_each_entry(_ss, &_root->subsys_list, sibling)
  109. /* for_each_root() allows you to iterate across the active hierarchies */
  110. #define for_each_root(_root) \
  111. list_for_each_entry(_root, &roots, root_list)
  112. /* Each task_struct has an embedded css_set, so the get/put
  113. * operation simply takes a reference count on all the cgroups
  114. * referenced by subsystems in this css_set. This can end up
  115. * multiple-counting some cgroups, but that's OK - the ref-count is
  116. * just a busy/not-busy indicator; ensuring that we only count each
  117. * cgroup once would require taking a global lock to ensure that no
  118. * subsystems moved between hierarchies while we were doing so.
  119. *
  120. * Possible TODO: decide at boot time based on the number of
  121. * registered subsystems and the number of CPUs or NUMA nodes whether
  122. * it's better for performance to ref-count every subsystem, or to
  123. * take a global lock and only add one ref count to each hierarchy.
  124. */
  125. static void get_css_set(struct css_set *cg)
  126. {
  127. int i;
  128. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
  129. atomic_inc(&cg->subsys[i]->cgroup->count);
  130. }
  131. static void put_css_set(struct css_set *cg)
  132. {
  133. int i;
  134. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
  135. atomic_dec(&cg->subsys[i]->cgroup->count);
  136. }
  137. /*
  138. * There is one global cgroup mutex. We also require taking
  139. * task_lock() when dereferencing a task's cgroup subsys pointers.
  140. * See "The task_lock() exception", at the end of this comment.
  141. *
  142. * A task must hold cgroup_mutex to modify cgroups.
  143. *
  144. * Any task can increment and decrement the count field without lock.
  145. * So in general, code holding cgroup_mutex can't rely on the count
  146. * field not changing. However, if the count goes to zero, then only
  147. * attach_task() can increment it again. Because a count of zero
  148. * means that no tasks are currently attached, therefore there is no
  149. * way a task attached to that cgroup can fork (the other way to
  150. * increment the count). So code holding cgroup_mutex can safely
  151. * assume that if the count is zero, it will stay zero. Similarly, if
  152. * a task holds cgroup_mutex on a cgroup with zero count, it
  153. * knows that the cgroup won't be removed, as cgroup_rmdir()
  154. * needs that mutex.
  155. *
  156. * The cgroup_common_file_write handler for operations that modify
  157. * the cgroup hierarchy holds cgroup_mutex across the entire operation,
  158. * single threading all such cgroup modifications across the system.
  159. *
  160. * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
  161. * (usually) take cgroup_mutex. These are the two most performance
  162. * critical pieces of code here. The exception occurs on cgroup_exit(),
  163. * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
  164. * is taken, and if the cgroup count is zero, a usermode call made
  165. * to /sbin/cgroup_release_agent with the name of the cgroup (path
  166. * relative to the root of cgroup file system) as the argument.
  167. *
  168. * A cgroup can only be deleted if both its 'count' of using tasks
  169. * is zero, and its list of 'children' cgroups is empty. Since all
  170. * tasks in the system use _some_ cgroup, and since there is always at
  171. * least one task in the system (init, pid == 1), therefore, top_cgroup
  172. * always has either children cgroups and/or using tasks. So we don't
  173. * need a special hack to ensure that top_cgroup cannot be deleted.
  174. *
  175. * The task_lock() exception
  176. *
  177. * The need for this exception arises from the action of
  178. * attach_task(), which overwrites one tasks cgroup pointer with
  179. * another. It does so using cgroup_mutexe, however there are
  180. * several performance critical places that need to reference
  181. * task->cgroup without the expense of grabbing a system global
  182. * mutex. Therefore except as noted below, when dereferencing or, as
  183. * in attach_task(), modifying a task'ss cgroup pointer we use
  184. * task_lock(), which acts on a spinlock (task->alloc_lock) already in
  185. * the task_struct routinely used for such matters.
  186. *
  187. * P.S. One more locking exception. RCU is used to guard the
  188. * update of a tasks cgroup pointer by attach_task()
  189. */
  190. static DEFINE_MUTEX(cgroup_mutex);
  191. /**
  192. * cgroup_lock - lock out any changes to cgroup structures
  193. *
  194. */
  195. void cgroup_lock(void)
  196. {
  197. mutex_lock(&cgroup_mutex);
  198. }
  199. /**
  200. * cgroup_unlock - release lock on cgroup changes
  201. *
  202. * Undo the lock taken in a previous cgroup_lock() call.
  203. */
  204. void cgroup_unlock(void)
  205. {
  206. mutex_unlock(&cgroup_mutex);
  207. }
  208. /*
  209. * A couple of forward declarations required, due to cyclic reference loop:
  210. * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
  211. * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
  212. * -> cgroup_mkdir.
  213. */
  214. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
  215. static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
  216. static int cgroup_populate_dir(struct cgroup *cont);
  217. static struct inode_operations cgroup_dir_inode_operations;
  218. static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
  219. {
  220. struct inode *inode = new_inode(sb);
  221. static struct backing_dev_info cgroup_backing_dev_info = {
  222. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  223. };
  224. if (inode) {
  225. inode->i_mode = mode;
  226. inode->i_uid = current->fsuid;
  227. inode->i_gid = current->fsgid;
  228. inode->i_blocks = 0;
  229. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  230. inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
  231. }
  232. return inode;
  233. }
  234. static void cgroup_diput(struct dentry *dentry, struct inode *inode)
  235. {
  236. /* is dentry a directory ? if so, kfree() associated cgroup */
  237. if (S_ISDIR(inode->i_mode)) {
  238. struct cgroup *cont = dentry->d_fsdata;
  239. BUG_ON(!(cgroup_is_removed(cont)));
  240. kfree(cont);
  241. }
  242. iput(inode);
  243. }
  244. static void remove_dir(struct dentry *d)
  245. {
  246. struct dentry *parent = dget(d->d_parent);
  247. d_delete(d);
  248. simple_rmdir(parent->d_inode, d);
  249. dput(parent);
  250. }
  251. static void cgroup_clear_directory(struct dentry *dentry)
  252. {
  253. struct list_head *node;
  254. BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
  255. spin_lock(&dcache_lock);
  256. node = dentry->d_subdirs.next;
  257. while (node != &dentry->d_subdirs) {
  258. struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
  259. list_del_init(node);
  260. if (d->d_inode) {
  261. /* This should never be called on a cgroup
  262. * directory with child cgroups */
  263. BUG_ON(d->d_inode->i_mode & S_IFDIR);
  264. d = dget_locked(d);
  265. spin_unlock(&dcache_lock);
  266. d_delete(d);
  267. simple_unlink(dentry->d_inode, d);
  268. dput(d);
  269. spin_lock(&dcache_lock);
  270. }
  271. node = dentry->d_subdirs.next;
  272. }
  273. spin_unlock(&dcache_lock);
  274. }
  275. /*
  276. * NOTE : the dentry must have been dget()'ed
  277. */
  278. static void cgroup_d_remove_dir(struct dentry *dentry)
  279. {
  280. cgroup_clear_directory(dentry);
  281. spin_lock(&dcache_lock);
  282. list_del_init(&dentry->d_u.d_child);
  283. spin_unlock(&dcache_lock);
  284. remove_dir(dentry);
  285. }
  286. static int rebind_subsystems(struct cgroupfs_root *root,
  287. unsigned long final_bits)
  288. {
  289. unsigned long added_bits, removed_bits;
  290. struct cgroup *cont = &root->top_cgroup;
  291. int i;
  292. removed_bits = root->actual_subsys_bits & ~final_bits;
  293. added_bits = final_bits & ~root->actual_subsys_bits;
  294. /* Check that any added subsystems are currently free */
  295. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  296. unsigned long long bit = 1ull << i;
  297. struct cgroup_subsys *ss = subsys[i];
  298. if (!(bit & added_bits))
  299. continue;
  300. if (ss->root != &rootnode) {
  301. /* Subsystem isn't free */
  302. return -EBUSY;
  303. }
  304. }
  305. /* Currently we don't handle adding/removing subsystems when
  306. * any child cgroups exist. This is theoretically supportable
  307. * but involves complex error handling, so it's being left until
  308. * later */
  309. if (!list_empty(&cont->children))
  310. return -EBUSY;
  311. /* Process each subsystem */
  312. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  313. struct cgroup_subsys *ss = subsys[i];
  314. unsigned long bit = 1UL << i;
  315. if (bit & added_bits) {
  316. /* We're binding this subsystem to this hierarchy */
  317. BUG_ON(cont->subsys[i]);
  318. BUG_ON(!dummytop->subsys[i]);
  319. BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
  320. cont->subsys[i] = dummytop->subsys[i];
  321. cont->subsys[i]->cgroup = cont;
  322. list_add(&ss->sibling, &root->subsys_list);
  323. rcu_assign_pointer(ss->root, root);
  324. if (ss->bind)
  325. ss->bind(ss, cont);
  326. } else if (bit & removed_bits) {
  327. /* We're removing this subsystem */
  328. BUG_ON(cont->subsys[i] != dummytop->subsys[i]);
  329. BUG_ON(cont->subsys[i]->cgroup != cont);
  330. if (ss->bind)
  331. ss->bind(ss, dummytop);
  332. dummytop->subsys[i]->cgroup = dummytop;
  333. cont->subsys[i] = NULL;
  334. rcu_assign_pointer(subsys[i]->root, &rootnode);
  335. list_del(&ss->sibling);
  336. } else if (bit & final_bits) {
  337. /* Subsystem state should already exist */
  338. BUG_ON(!cont->subsys[i]);
  339. } else {
  340. /* Subsystem state shouldn't exist */
  341. BUG_ON(cont->subsys[i]);
  342. }
  343. }
  344. root->subsys_bits = root->actual_subsys_bits = final_bits;
  345. synchronize_rcu();
  346. return 0;
  347. }
  348. static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
  349. {
  350. struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
  351. struct cgroup_subsys *ss;
  352. mutex_lock(&cgroup_mutex);
  353. for_each_subsys(root, ss)
  354. seq_printf(seq, ",%s", ss->name);
  355. if (test_bit(ROOT_NOPREFIX, &root->flags))
  356. seq_puts(seq, ",noprefix");
  357. mutex_unlock(&cgroup_mutex);
  358. return 0;
  359. }
  360. struct cgroup_sb_opts {
  361. unsigned long subsys_bits;
  362. unsigned long flags;
  363. };
  364. /* Convert a hierarchy specifier into a bitmask of subsystems and
  365. * flags. */
  366. static int parse_cgroupfs_options(char *data,
  367. struct cgroup_sb_opts *opts)
  368. {
  369. char *token, *o = data ?: "all";
  370. opts->subsys_bits = 0;
  371. opts->flags = 0;
  372. while ((token = strsep(&o, ",")) != NULL) {
  373. if (!*token)
  374. return -EINVAL;
  375. if (!strcmp(token, "all")) {
  376. opts->subsys_bits = (1 << CGROUP_SUBSYS_COUNT) - 1;
  377. } else if (!strcmp(token, "noprefix")) {
  378. set_bit(ROOT_NOPREFIX, &opts->flags);
  379. } else {
  380. struct cgroup_subsys *ss;
  381. int i;
  382. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  383. ss = subsys[i];
  384. if (!strcmp(token, ss->name)) {
  385. set_bit(i, &opts->subsys_bits);
  386. break;
  387. }
  388. }
  389. if (i == CGROUP_SUBSYS_COUNT)
  390. return -ENOENT;
  391. }
  392. }
  393. /* We can't have an empty hierarchy */
  394. if (!opts->subsys_bits)
  395. return -EINVAL;
  396. return 0;
  397. }
  398. static int cgroup_remount(struct super_block *sb, int *flags, char *data)
  399. {
  400. int ret = 0;
  401. struct cgroupfs_root *root = sb->s_fs_info;
  402. struct cgroup *cont = &root->top_cgroup;
  403. struct cgroup_sb_opts opts;
  404. mutex_lock(&cont->dentry->d_inode->i_mutex);
  405. mutex_lock(&cgroup_mutex);
  406. /* See what subsystems are wanted */
  407. ret = parse_cgroupfs_options(data, &opts);
  408. if (ret)
  409. goto out_unlock;
  410. /* Don't allow flags to change at remount */
  411. if (opts.flags != root->flags) {
  412. ret = -EINVAL;
  413. goto out_unlock;
  414. }
  415. ret = rebind_subsystems(root, opts.subsys_bits);
  416. /* (re)populate subsystem files */
  417. if (!ret)
  418. cgroup_populate_dir(cont);
  419. out_unlock:
  420. mutex_unlock(&cgroup_mutex);
  421. mutex_unlock(&cont->dentry->d_inode->i_mutex);
  422. return ret;
  423. }
  424. static struct super_operations cgroup_ops = {
  425. .statfs = simple_statfs,
  426. .drop_inode = generic_delete_inode,
  427. .show_options = cgroup_show_options,
  428. .remount_fs = cgroup_remount,
  429. };
  430. static void init_cgroup_root(struct cgroupfs_root *root)
  431. {
  432. struct cgroup *cont = &root->top_cgroup;
  433. INIT_LIST_HEAD(&root->subsys_list);
  434. INIT_LIST_HEAD(&root->root_list);
  435. root->number_of_cgroups = 1;
  436. cont->root = root;
  437. cont->top_cgroup = cont;
  438. INIT_LIST_HEAD(&cont->sibling);
  439. INIT_LIST_HEAD(&cont->children);
  440. }
  441. static int cgroup_test_super(struct super_block *sb, void *data)
  442. {
  443. struct cgroupfs_root *new = data;
  444. struct cgroupfs_root *root = sb->s_fs_info;
  445. /* First check subsystems */
  446. if (new->subsys_bits != root->subsys_bits)
  447. return 0;
  448. /* Next check flags */
  449. if (new->flags != root->flags)
  450. return 0;
  451. return 1;
  452. }
  453. static int cgroup_set_super(struct super_block *sb, void *data)
  454. {
  455. int ret;
  456. struct cgroupfs_root *root = data;
  457. ret = set_anon_super(sb, NULL);
  458. if (ret)
  459. return ret;
  460. sb->s_fs_info = root;
  461. root->sb = sb;
  462. sb->s_blocksize = PAGE_CACHE_SIZE;
  463. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  464. sb->s_magic = CGROUP_SUPER_MAGIC;
  465. sb->s_op = &cgroup_ops;
  466. return 0;
  467. }
  468. static int cgroup_get_rootdir(struct super_block *sb)
  469. {
  470. struct inode *inode =
  471. cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
  472. struct dentry *dentry;
  473. if (!inode)
  474. return -ENOMEM;
  475. inode->i_op = &simple_dir_inode_operations;
  476. inode->i_fop = &simple_dir_operations;
  477. inode->i_op = &cgroup_dir_inode_operations;
  478. /* directories start off with i_nlink == 2 (for "." entry) */
  479. inc_nlink(inode);
  480. dentry = d_alloc_root(inode);
  481. if (!dentry) {
  482. iput(inode);
  483. return -ENOMEM;
  484. }
  485. sb->s_root = dentry;
  486. return 0;
  487. }
  488. static int cgroup_get_sb(struct file_system_type *fs_type,
  489. int flags, const char *unused_dev_name,
  490. void *data, struct vfsmount *mnt)
  491. {
  492. struct cgroup_sb_opts opts;
  493. int ret = 0;
  494. struct super_block *sb;
  495. struct cgroupfs_root *root;
  496. /* First find the desired set of subsystems */
  497. ret = parse_cgroupfs_options(data, &opts);
  498. if (ret)
  499. return ret;
  500. root = kzalloc(sizeof(*root), GFP_KERNEL);
  501. if (!root)
  502. return -ENOMEM;
  503. init_cgroup_root(root);
  504. root->subsys_bits = opts.subsys_bits;
  505. root->flags = opts.flags;
  506. sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
  507. if (IS_ERR(sb)) {
  508. kfree(root);
  509. return PTR_ERR(sb);
  510. }
  511. if (sb->s_fs_info != root) {
  512. /* Reusing an existing superblock */
  513. BUG_ON(sb->s_root == NULL);
  514. kfree(root);
  515. root = NULL;
  516. } else {
  517. /* New superblock */
  518. struct cgroup *cont = &root->top_cgroup;
  519. BUG_ON(sb->s_root != NULL);
  520. ret = cgroup_get_rootdir(sb);
  521. if (ret)
  522. goto drop_new_super;
  523. mutex_lock(&cgroup_mutex);
  524. ret = rebind_subsystems(root, root->subsys_bits);
  525. if (ret == -EBUSY) {
  526. mutex_unlock(&cgroup_mutex);
  527. goto drop_new_super;
  528. }
  529. /* EBUSY should be the only error here */
  530. BUG_ON(ret);
  531. list_add(&root->root_list, &roots);
  532. sb->s_root->d_fsdata = &root->top_cgroup;
  533. root->top_cgroup.dentry = sb->s_root;
  534. BUG_ON(!list_empty(&cont->sibling));
  535. BUG_ON(!list_empty(&cont->children));
  536. BUG_ON(root->number_of_cgroups != 1);
  537. /*
  538. * I believe that it's safe to nest i_mutex inside
  539. * cgroup_mutex in this case, since no-one else can
  540. * be accessing this directory yet. But we still need
  541. * to teach lockdep that this is the case - currently
  542. * a cgroupfs remount triggers a lockdep warning
  543. */
  544. mutex_lock(&cont->dentry->d_inode->i_mutex);
  545. cgroup_populate_dir(cont);
  546. mutex_unlock(&cont->dentry->d_inode->i_mutex);
  547. mutex_unlock(&cgroup_mutex);
  548. }
  549. return simple_set_mnt(mnt, sb);
  550. drop_new_super:
  551. up_write(&sb->s_umount);
  552. deactivate_super(sb);
  553. return ret;
  554. }
  555. static void cgroup_kill_sb(struct super_block *sb) {
  556. struct cgroupfs_root *root = sb->s_fs_info;
  557. struct cgroup *cont = &root->top_cgroup;
  558. int ret;
  559. BUG_ON(!root);
  560. BUG_ON(root->number_of_cgroups != 1);
  561. BUG_ON(!list_empty(&cont->children));
  562. BUG_ON(!list_empty(&cont->sibling));
  563. mutex_lock(&cgroup_mutex);
  564. /* Rebind all subsystems back to the default hierarchy */
  565. ret = rebind_subsystems(root, 0);
  566. /* Shouldn't be able to fail ... */
  567. BUG_ON(ret);
  568. if (!list_empty(&root->root_list))
  569. list_del(&root->root_list);
  570. mutex_unlock(&cgroup_mutex);
  571. kfree(root);
  572. kill_litter_super(sb);
  573. }
  574. static struct file_system_type cgroup_fs_type = {
  575. .name = "cgroup",
  576. .get_sb = cgroup_get_sb,
  577. .kill_sb = cgroup_kill_sb,
  578. };
  579. static inline struct cgroup *__d_cont(struct dentry *dentry)
  580. {
  581. return dentry->d_fsdata;
  582. }
  583. static inline struct cftype *__d_cft(struct dentry *dentry)
  584. {
  585. return dentry->d_fsdata;
  586. }
  587. /*
  588. * Called with cgroup_mutex held. Writes path of cgroup into buf.
  589. * Returns 0 on success, -errno on error.
  590. */
  591. int cgroup_path(const struct cgroup *cont, char *buf, int buflen)
  592. {
  593. char *start;
  594. if (cont == dummytop) {
  595. /*
  596. * Inactive subsystems have no dentry for their root
  597. * cgroup
  598. */
  599. strcpy(buf, "/");
  600. return 0;
  601. }
  602. start = buf + buflen;
  603. *--start = '\0';
  604. for (;;) {
  605. int len = cont->dentry->d_name.len;
  606. if ((start -= len) < buf)
  607. return -ENAMETOOLONG;
  608. memcpy(start, cont->dentry->d_name.name, len);
  609. cont = cont->parent;
  610. if (!cont)
  611. break;
  612. if (!cont->parent)
  613. continue;
  614. if (--start < buf)
  615. return -ENAMETOOLONG;
  616. *start = '/';
  617. }
  618. memmove(buf, start, buf + buflen - start);
  619. return 0;
  620. }
  621. /*
  622. * Return the first subsystem attached to a cgroup's hierarchy, and
  623. * its subsystem id.
  624. */
  625. static void get_first_subsys(const struct cgroup *cont,
  626. struct cgroup_subsys_state **css, int *subsys_id)
  627. {
  628. const struct cgroupfs_root *root = cont->root;
  629. const struct cgroup_subsys *test_ss;
  630. BUG_ON(list_empty(&root->subsys_list));
  631. test_ss = list_entry(root->subsys_list.next,
  632. struct cgroup_subsys, sibling);
  633. if (css) {
  634. *css = cont->subsys[test_ss->subsys_id];
  635. BUG_ON(!*css);
  636. }
  637. if (subsys_id)
  638. *subsys_id = test_ss->subsys_id;
  639. }
  640. /*
  641. * Attach task 'tsk' to cgroup 'cont'
  642. *
  643. * Call holding cgroup_mutex. May take task_lock of
  644. * the task 'pid' during call.
  645. */
  646. static int attach_task(struct cgroup *cont, struct task_struct *tsk)
  647. {
  648. int retval = 0;
  649. struct cgroup_subsys *ss;
  650. struct cgroup *oldcont;
  651. struct css_set *cg = &tsk->cgroups;
  652. struct cgroupfs_root *root = cont->root;
  653. int i;
  654. int subsys_id;
  655. get_first_subsys(cont, NULL, &subsys_id);
  656. /* Nothing to do if the task is already in that cgroup */
  657. oldcont = task_cgroup(tsk, subsys_id);
  658. if (cont == oldcont)
  659. return 0;
  660. for_each_subsys(root, ss) {
  661. if (ss->can_attach) {
  662. retval = ss->can_attach(ss, cont, tsk);
  663. if (retval) {
  664. return retval;
  665. }
  666. }
  667. }
  668. task_lock(tsk);
  669. if (tsk->flags & PF_EXITING) {
  670. task_unlock(tsk);
  671. return -ESRCH;
  672. }
  673. /* Update the css_set pointers for the subsystems in this
  674. * hierarchy */
  675. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  676. if (root->subsys_bits & (1ull << i)) {
  677. /* Subsystem is in this hierarchy. So we want
  678. * the subsystem state from the new
  679. * cgroup. Transfer the refcount from the
  680. * old to the new */
  681. atomic_inc(&cont->count);
  682. atomic_dec(&cg->subsys[i]->cgroup->count);
  683. rcu_assign_pointer(cg->subsys[i], cont->subsys[i]);
  684. }
  685. }
  686. task_unlock(tsk);
  687. for_each_subsys(root, ss) {
  688. if (ss->attach) {
  689. ss->attach(ss, cont, oldcont, tsk);
  690. }
  691. }
  692. synchronize_rcu();
  693. return 0;
  694. }
  695. /*
  696. * Attach task with pid 'pid' to cgroup 'cont'. Call with
  697. * cgroup_mutex, may take task_lock of task
  698. */
  699. static int attach_task_by_pid(struct cgroup *cont, char *pidbuf)
  700. {
  701. pid_t pid;
  702. struct task_struct *tsk;
  703. int ret;
  704. if (sscanf(pidbuf, "%d", &pid) != 1)
  705. return -EIO;
  706. if (pid) {
  707. rcu_read_lock();
  708. tsk = find_task_by_pid(pid);
  709. if (!tsk || tsk->flags & PF_EXITING) {
  710. rcu_read_unlock();
  711. return -ESRCH;
  712. }
  713. get_task_struct(tsk);
  714. rcu_read_unlock();
  715. if ((current->euid) && (current->euid != tsk->uid)
  716. && (current->euid != tsk->suid)) {
  717. put_task_struct(tsk);
  718. return -EACCES;
  719. }
  720. } else {
  721. tsk = current;
  722. get_task_struct(tsk);
  723. }
  724. ret = attach_task(cont, tsk);
  725. put_task_struct(tsk);
  726. return ret;
  727. }
  728. /* The various types of files and directories in a cgroup file system */
  729. enum cgroup_filetype {
  730. FILE_ROOT,
  731. FILE_DIR,
  732. FILE_TASKLIST,
  733. };
  734. static ssize_t cgroup_write_uint(struct cgroup *cont, struct cftype *cft,
  735. struct file *file,
  736. const char __user *userbuf,
  737. size_t nbytes, loff_t *unused_ppos)
  738. {
  739. char buffer[64];
  740. int retval = 0;
  741. u64 val;
  742. char *end;
  743. if (!nbytes)
  744. return -EINVAL;
  745. if (nbytes >= sizeof(buffer))
  746. return -E2BIG;
  747. if (copy_from_user(buffer, userbuf, nbytes))
  748. return -EFAULT;
  749. buffer[nbytes] = 0; /* nul-terminate */
  750. /* strip newline if necessary */
  751. if (nbytes && (buffer[nbytes-1] == '\n'))
  752. buffer[nbytes-1] = 0;
  753. val = simple_strtoull(buffer, &end, 0);
  754. if (*end)
  755. return -EINVAL;
  756. /* Pass to subsystem */
  757. retval = cft->write_uint(cont, cft, val);
  758. if (!retval)
  759. retval = nbytes;
  760. return retval;
  761. }
  762. static ssize_t cgroup_common_file_write(struct cgroup *cont,
  763. struct cftype *cft,
  764. struct file *file,
  765. const char __user *userbuf,
  766. size_t nbytes, loff_t *unused_ppos)
  767. {
  768. enum cgroup_filetype type = cft->private;
  769. char *buffer;
  770. int retval = 0;
  771. if (nbytes >= PATH_MAX)
  772. return -E2BIG;
  773. /* +1 for nul-terminator */
  774. buffer = kmalloc(nbytes + 1, GFP_KERNEL);
  775. if (buffer == NULL)
  776. return -ENOMEM;
  777. if (copy_from_user(buffer, userbuf, nbytes)) {
  778. retval = -EFAULT;
  779. goto out1;
  780. }
  781. buffer[nbytes] = 0; /* nul-terminate */
  782. mutex_lock(&cgroup_mutex);
  783. if (cgroup_is_removed(cont)) {
  784. retval = -ENODEV;
  785. goto out2;
  786. }
  787. switch (type) {
  788. case FILE_TASKLIST:
  789. retval = attach_task_by_pid(cont, buffer);
  790. break;
  791. default:
  792. retval = -EINVAL;
  793. goto out2;
  794. }
  795. if (retval == 0)
  796. retval = nbytes;
  797. out2:
  798. mutex_unlock(&cgroup_mutex);
  799. out1:
  800. kfree(buffer);
  801. return retval;
  802. }
  803. static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
  804. size_t nbytes, loff_t *ppos)
  805. {
  806. struct cftype *cft = __d_cft(file->f_dentry);
  807. struct cgroup *cont = __d_cont(file->f_dentry->d_parent);
  808. if (!cft)
  809. return -ENODEV;
  810. if (cft->write)
  811. return cft->write(cont, cft, file, buf, nbytes, ppos);
  812. if (cft->write_uint)
  813. return cgroup_write_uint(cont, cft, file, buf, nbytes, ppos);
  814. return -EINVAL;
  815. }
  816. static ssize_t cgroup_read_uint(struct cgroup *cont, struct cftype *cft,
  817. struct file *file,
  818. char __user *buf, size_t nbytes,
  819. loff_t *ppos)
  820. {
  821. char tmp[64];
  822. u64 val = cft->read_uint(cont, cft);
  823. int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
  824. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  825. }
  826. static ssize_t cgroup_file_read(struct file *file, char __user *buf,
  827. size_t nbytes, loff_t *ppos)
  828. {
  829. struct cftype *cft = __d_cft(file->f_dentry);
  830. struct cgroup *cont = __d_cont(file->f_dentry->d_parent);
  831. if (!cft)
  832. return -ENODEV;
  833. if (cft->read)
  834. return cft->read(cont, cft, file, buf, nbytes, ppos);
  835. if (cft->read_uint)
  836. return cgroup_read_uint(cont, cft, file, buf, nbytes, ppos);
  837. return -EINVAL;
  838. }
  839. static int cgroup_file_open(struct inode *inode, struct file *file)
  840. {
  841. int err;
  842. struct cftype *cft;
  843. err = generic_file_open(inode, file);
  844. if (err)
  845. return err;
  846. cft = __d_cft(file->f_dentry);
  847. if (!cft)
  848. return -ENODEV;
  849. if (cft->open)
  850. err = cft->open(inode, file);
  851. else
  852. err = 0;
  853. return err;
  854. }
  855. static int cgroup_file_release(struct inode *inode, struct file *file)
  856. {
  857. struct cftype *cft = __d_cft(file->f_dentry);
  858. if (cft->release)
  859. return cft->release(inode, file);
  860. return 0;
  861. }
  862. /*
  863. * cgroup_rename - Only allow simple rename of directories in place.
  864. */
  865. static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
  866. struct inode *new_dir, struct dentry *new_dentry)
  867. {
  868. if (!S_ISDIR(old_dentry->d_inode->i_mode))
  869. return -ENOTDIR;
  870. if (new_dentry->d_inode)
  871. return -EEXIST;
  872. if (old_dir != new_dir)
  873. return -EIO;
  874. return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  875. }
  876. static struct file_operations cgroup_file_operations = {
  877. .read = cgroup_file_read,
  878. .write = cgroup_file_write,
  879. .llseek = generic_file_llseek,
  880. .open = cgroup_file_open,
  881. .release = cgroup_file_release,
  882. };
  883. static struct inode_operations cgroup_dir_inode_operations = {
  884. .lookup = simple_lookup,
  885. .mkdir = cgroup_mkdir,
  886. .rmdir = cgroup_rmdir,
  887. .rename = cgroup_rename,
  888. };
  889. static int cgroup_create_file(struct dentry *dentry, int mode,
  890. struct super_block *sb)
  891. {
  892. static struct dentry_operations cgroup_dops = {
  893. .d_iput = cgroup_diput,
  894. };
  895. struct inode *inode;
  896. if (!dentry)
  897. return -ENOENT;
  898. if (dentry->d_inode)
  899. return -EEXIST;
  900. inode = cgroup_new_inode(mode, sb);
  901. if (!inode)
  902. return -ENOMEM;
  903. if (S_ISDIR(mode)) {
  904. inode->i_op = &cgroup_dir_inode_operations;
  905. inode->i_fop = &simple_dir_operations;
  906. /* start off with i_nlink == 2 (for "." entry) */
  907. inc_nlink(inode);
  908. /* start with the directory inode held, so that we can
  909. * populate it without racing with another mkdir */
  910. mutex_lock(&inode->i_mutex);
  911. } else if (S_ISREG(mode)) {
  912. inode->i_size = 0;
  913. inode->i_fop = &cgroup_file_operations;
  914. }
  915. dentry->d_op = &cgroup_dops;
  916. d_instantiate(dentry, inode);
  917. dget(dentry); /* Extra count - pin the dentry in core */
  918. return 0;
  919. }
  920. /*
  921. * cgroup_create_dir - create a directory for an object.
  922. * cont: the cgroup we create the directory for.
  923. * It must have a valid ->parent field
  924. * And we are going to fill its ->dentry field.
  925. * dentry: dentry of the new container
  926. * mode: mode to set on new directory.
  927. */
  928. static int cgroup_create_dir(struct cgroup *cont, struct dentry *dentry,
  929. int mode)
  930. {
  931. struct dentry *parent;
  932. int error = 0;
  933. parent = cont->parent->dentry;
  934. error = cgroup_create_file(dentry, S_IFDIR | mode, cont->root->sb);
  935. if (!error) {
  936. dentry->d_fsdata = cont;
  937. inc_nlink(parent->d_inode);
  938. cont->dentry = dentry;
  939. dget(dentry);
  940. }
  941. dput(dentry);
  942. return error;
  943. }
  944. int cgroup_add_file(struct cgroup *cont,
  945. struct cgroup_subsys *subsys,
  946. const struct cftype *cft)
  947. {
  948. struct dentry *dir = cont->dentry;
  949. struct dentry *dentry;
  950. int error;
  951. char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
  952. if (subsys && !test_bit(ROOT_NOPREFIX, &cont->root->flags)) {
  953. strcpy(name, subsys->name);
  954. strcat(name, ".");
  955. }
  956. strcat(name, cft->name);
  957. BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
  958. dentry = lookup_one_len(name, dir, strlen(name));
  959. if (!IS_ERR(dentry)) {
  960. error = cgroup_create_file(dentry, 0644 | S_IFREG,
  961. cont->root->sb);
  962. if (!error)
  963. dentry->d_fsdata = (void *)cft;
  964. dput(dentry);
  965. } else
  966. error = PTR_ERR(dentry);
  967. return error;
  968. }
  969. int cgroup_add_files(struct cgroup *cont,
  970. struct cgroup_subsys *subsys,
  971. const struct cftype cft[],
  972. int count)
  973. {
  974. int i, err;
  975. for (i = 0; i < count; i++) {
  976. err = cgroup_add_file(cont, subsys, &cft[i]);
  977. if (err)
  978. return err;
  979. }
  980. return 0;
  981. }
  982. /* Count the number of tasks in a cgroup. Could be made more
  983. * time-efficient but less space-efficient with more linked lists
  984. * running through each cgroup and the css_set structures that
  985. * referenced it. Must be called with tasklist_lock held for read or
  986. * write or in an rcu critical section.
  987. */
  988. int __cgroup_task_count(const struct cgroup *cont)
  989. {
  990. int count = 0;
  991. struct task_struct *g, *p;
  992. struct cgroup_subsys_state *css;
  993. int subsys_id;
  994. get_first_subsys(cont, &css, &subsys_id);
  995. do_each_thread(g, p) {
  996. if (task_subsys_state(p, subsys_id) == css)
  997. count ++;
  998. } while_each_thread(g, p);
  999. return count;
  1000. }
  1001. /*
  1002. * Stuff for reading the 'tasks' file.
  1003. *
  1004. * Reading this file can return large amounts of data if a cgroup has
  1005. * *lots* of attached tasks. So it may need several calls to read(),
  1006. * but we cannot guarantee that the information we produce is correct
  1007. * unless we produce it entirely atomically.
  1008. *
  1009. * Upon tasks file open(), a struct ctr_struct is allocated, that
  1010. * will have a pointer to an array (also allocated here). The struct
  1011. * ctr_struct * is stored in file->private_data. Its resources will
  1012. * be freed by release() when the file is closed. The array is used
  1013. * to sprintf the PIDs and then used by read().
  1014. */
  1015. struct ctr_struct {
  1016. char *buf;
  1017. int bufsz;
  1018. };
  1019. /*
  1020. * Load into 'pidarray' up to 'npids' of the tasks using cgroup
  1021. * 'cont'. Return actual number of pids loaded. No need to
  1022. * task_lock(p) when reading out p->cgroup, since we're in an RCU
  1023. * read section, so the css_set can't go away, and is
  1024. * immutable after creation.
  1025. */
  1026. static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cont)
  1027. {
  1028. int n = 0;
  1029. struct task_struct *g, *p;
  1030. struct cgroup_subsys_state *css;
  1031. int subsys_id;
  1032. get_first_subsys(cont, &css, &subsys_id);
  1033. rcu_read_lock();
  1034. do_each_thread(g, p) {
  1035. if (task_subsys_state(p, subsys_id) == css) {
  1036. pidarray[n++] = pid_nr(task_pid(p));
  1037. if (unlikely(n == npids))
  1038. goto array_full;
  1039. }
  1040. } while_each_thread(g, p);
  1041. array_full:
  1042. rcu_read_unlock();
  1043. return n;
  1044. }
  1045. static int cmppid(const void *a, const void *b)
  1046. {
  1047. return *(pid_t *)a - *(pid_t *)b;
  1048. }
  1049. /*
  1050. * Convert array 'a' of 'npids' pid_t's to a string of newline separated
  1051. * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
  1052. * count 'cnt' of how many chars would be written if buf were large enough.
  1053. */
  1054. static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
  1055. {
  1056. int cnt = 0;
  1057. int i;
  1058. for (i = 0; i < npids; i++)
  1059. cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
  1060. return cnt;
  1061. }
  1062. /*
  1063. * Handle an open on 'tasks' file. Prepare a buffer listing the
  1064. * process id's of tasks currently attached to the cgroup being opened.
  1065. *
  1066. * Does not require any specific cgroup mutexes, and does not take any.
  1067. */
  1068. static int cgroup_tasks_open(struct inode *unused, struct file *file)
  1069. {
  1070. struct cgroup *cont = __d_cont(file->f_dentry->d_parent);
  1071. struct ctr_struct *ctr;
  1072. pid_t *pidarray;
  1073. int npids;
  1074. char c;
  1075. if (!(file->f_mode & FMODE_READ))
  1076. return 0;
  1077. ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
  1078. if (!ctr)
  1079. goto err0;
  1080. /*
  1081. * If cgroup gets more users after we read count, we won't have
  1082. * enough space - tough. This race is indistinguishable to the
  1083. * caller from the case that the additional cgroup users didn't
  1084. * show up until sometime later on.
  1085. */
  1086. npids = cgroup_task_count(cont);
  1087. if (npids) {
  1088. pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
  1089. if (!pidarray)
  1090. goto err1;
  1091. npids = pid_array_load(pidarray, npids, cont);
  1092. sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
  1093. /* Call pid_array_to_buf() twice, first just to get bufsz */
  1094. ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
  1095. ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
  1096. if (!ctr->buf)
  1097. goto err2;
  1098. ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
  1099. kfree(pidarray);
  1100. } else {
  1101. ctr->buf = 0;
  1102. ctr->bufsz = 0;
  1103. }
  1104. file->private_data = ctr;
  1105. return 0;
  1106. err2:
  1107. kfree(pidarray);
  1108. err1:
  1109. kfree(ctr);
  1110. err0:
  1111. return -ENOMEM;
  1112. }
  1113. static ssize_t cgroup_tasks_read(struct cgroup *cont,
  1114. struct cftype *cft,
  1115. struct file *file, char __user *buf,
  1116. size_t nbytes, loff_t *ppos)
  1117. {
  1118. struct ctr_struct *ctr = file->private_data;
  1119. return simple_read_from_buffer(buf, nbytes, ppos, ctr->buf, ctr->bufsz);
  1120. }
  1121. static int cgroup_tasks_release(struct inode *unused_inode,
  1122. struct file *file)
  1123. {
  1124. struct ctr_struct *ctr;
  1125. if (file->f_mode & FMODE_READ) {
  1126. ctr = file->private_data;
  1127. kfree(ctr->buf);
  1128. kfree(ctr);
  1129. }
  1130. return 0;
  1131. }
  1132. /*
  1133. * for the common functions, 'private' gives the type of file
  1134. */
  1135. static struct cftype cft_tasks = {
  1136. .name = "tasks",
  1137. .open = cgroup_tasks_open,
  1138. .read = cgroup_tasks_read,
  1139. .write = cgroup_common_file_write,
  1140. .release = cgroup_tasks_release,
  1141. .private = FILE_TASKLIST,
  1142. };
  1143. static int cgroup_populate_dir(struct cgroup *cont)
  1144. {
  1145. int err;
  1146. struct cgroup_subsys *ss;
  1147. /* First clear out any existing files */
  1148. cgroup_clear_directory(cont->dentry);
  1149. err = cgroup_add_file(cont, NULL, &cft_tasks);
  1150. if (err < 0)
  1151. return err;
  1152. for_each_subsys(cont->root, ss) {
  1153. if (ss->populate && (err = ss->populate(ss, cont)) < 0)
  1154. return err;
  1155. }
  1156. return 0;
  1157. }
  1158. static void init_cgroup_css(struct cgroup_subsys_state *css,
  1159. struct cgroup_subsys *ss,
  1160. struct cgroup *cont)
  1161. {
  1162. css->cgroup = cont;
  1163. atomic_set(&css->refcnt, 0);
  1164. css->flags = 0;
  1165. if (cont == dummytop)
  1166. set_bit(CSS_ROOT, &css->flags);
  1167. BUG_ON(cont->subsys[ss->subsys_id]);
  1168. cont->subsys[ss->subsys_id] = css;
  1169. }
  1170. /*
  1171. * cgroup_create - create a cgroup
  1172. * parent: cgroup that will be parent of the new cgroup.
  1173. * name: name of the new cgroup. Will be strcpy'ed.
  1174. * mode: mode to set on new inode
  1175. *
  1176. * Must be called with the mutex on the parent inode held
  1177. */
  1178. static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
  1179. int mode)
  1180. {
  1181. struct cgroup *cont;
  1182. struct cgroupfs_root *root = parent->root;
  1183. int err = 0;
  1184. struct cgroup_subsys *ss;
  1185. struct super_block *sb = root->sb;
  1186. cont = kzalloc(sizeof(*cont), GFP_KERNEL);
  1187. if (!cont)
  1188. return -ENOMEM;
  1189. /* Grab a reference on the superblock so the hierarchy doesn't
  1190. * get deleted on unmount if there are child cgroups. This
  1191. * can be done outside cgroup_mutex, since the sb can't
  1192. * disappear while someone has an open control file on the
  1193. * fs */
  1194. atomic_inc(&sb->s_active);
  1195. mutex_lock(&cgroup_mutex);
  1196. cont->flags = 0;
  1197. INIT_LIST_HEAD(&cont->sibling);
  1198. INIT_LIST_HEAD(&cont->children);
  1199. cont->parent = parent;
  1200. cont->root = parent->root;
  1201. cont->top_cgroup = parent->top_cgroup;
  1202. for_each_subsys(root, ss) {
  1203. struct cgroup_subsys_state *css = ss->create(ss, cont);
  1204. if (IS_ERR(css)) {
  1205. err = PTR_ERR(css);
  1206. goto err_destroy;
  1207. }
  1208. init_cgroup_css(css, ss, cont);
  1209. }
  1210. list_add(&cont->sibling, &cont->parent->children);
  1211. root->number_of_cgroups++;
  1212. err = cgroup_create_dir(cont, dentry, mode);
  1213. if (err < 0)
  1214. goto err_remove;
  1215. /* The cgroup directory was pre-locked for us */
  1216. BUG_ON(!mutex_is_locked(&cont->dentry->d_inode->i_mutex));
  1217. err = cgroup_populate_dir(cont);
  1218. /* If err < 0, we have a half-filled directory - oh well ;) */
  1219. mutex_unlock(&cgroup_mutex);
  1220. mutex_unlock(&cont->dentry->d_inode->i_mutex);
  1221. return 0;
  1222. err_remove:
  1223. list_del(&cont->sibling);
  1224. root->number_of_cgroups--;
  1225. err_destroy:
  1226. for_each_subsys(root, ss) {
  1227. if (cont->subsys[ss->subsys_id])
  1228. ss->destroy(ss, cont);
  1229. }
  1230. mutex_unlock(&cgroup_mutex);
  1231. /* Release the reference count that we took on the superblock */
  1232. deactivate_super(sb);
  1233. kfree(cont);
  1234. return err;
  1235. }
  1236. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1237. {
  1238. struct cgroup *c_parent = dentry->d_parent->d_fsdata;
  1239. /* the vfs holds inode->i_mutex already */
  1240. return cgroup_create(c_parent, dentry, mode | S_IFDIR);
  1241. }
  1242. static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
  1243. {
  1244. struct cgroup *cont = dentry->d_fsdata;
  1245. struct dentry *d;
  1246. struct cgroup *parent;
  1247. struct cgroup_subsys *ss;
  1248. struct super_block *sb;
  1249. struct cgroupfs_root *root;
  1250. int css_busy = 0;
  1251. /* the vfs holds both inode->i_mutex already */
  1252. mutex_lock(&cgroup_mutex);
  1253. if (atomic_read(&cont->count) != 0) {
  1254. mutex_unlock(&cgroup_mutex);
  1255. return -EBUSY;
  1256. }
  1257. if (!list_empty(&cont->children)) {
  1258. mutex_unlock(&cgroup_mutex);
  1259. return -EBUSY;
  1260. }
  1261. parent = cont->parent;
  1262. root = cont->root;
  1263. sb = root->sb;
  1264. /* Check the reference count on each subsystem. Since we
  1265. * already established that there are no tasks in the
  1266. * cgroup, if the css refcount is also 0, then there should
  1267. * be no outstanding references, so the subsystem is safe to
  1268. * destroy */
  1269. for_each_subsys(root, ss) {
  1270. struct cgroup_subsys_state *css;
  1271. css = cont->subsys[ss->subsys_id];
  1272. if (atomic_read(&css->refcnt)) {
  1273. css_busy = 1;
  1274. break;
  1275. }
  1276. }
  1277. if (css_busy) {
  1278. mutex_unlock(&cgroup_mutex);
  1279. return -EBUSY;
  1280. }
  1281. for_each_subsys(root, ss) {
  1282. if (cont->subsys[ss->subsys_id])
  1283. ss->destroy(ss, cont);
  1284. }
  1285. set_bit(CONT_REMOVED, &cont->flags);
  1286. /* delete my sibling from parent->children */
  1287. list_del(&cont->sibling);
  1288. spin_lock(&cont->dentry->d_lock);
  1289. d = dget(cont->dentry);
  1290. cont->dentry = NULL;
  1291. spin_unlock(&d->d_lock);
  1292. cgroup_d_remove_dir(d);
  1293. dput(d);
  1294. root->number_of_cgroups--;
  1295. mutex_unlock(&cgroup_mutex);
  1296. /* Drop the active superblock reference that we took when we
  1297. * created the cgroup */
  1298. deactivate_super(sb);
  1299. return 0;
  1300. }
  1301. static void cgroup_init_subsys(struct cgroup_subsys *ss)
  1302. {
  1303. struct task_struct *g, *p;
  1304. struct cgroup_subsys_state *css;
  1305. printk(KERN_ERR "Initializing cgroup subsys %s\n", ss->name);
  1306. /* Create the top cgroup state for this subsystem */
  1307. ss->root = &rootnode;
  1308. css = ss->create(ss, dummytop);
  1309. /* We don't handle early failures gracefully */
  1310. BUG_ON(IS_ERR(css));
  1311. init_cgroup_css(css, ss, dummytop);
  1312. /* Update all tasks to contain a subsys pointer to this state
  1313. * - since the subsystem is newly registered, all tasks are in
  1314. * the subsystem's top cgroup. */
  1315. /* If this subsystem requested that it be notified with fork
  1316. * events, we should send it one now for every process in the
  1317. * system */
  1318. read_lock(&tasklist_lock);
  1319. init_task.cgroups.subsys[ss->subsys_id] = css;
  1320. if (ss->fork)
  1321. ss->fork(ss, &init_task);
  1322. do_each_thread(g, p) {
  1323. printk(KERN_INFO "Setting task %p css to %p (%d)\n", css, p, p->pid);
  1324. p->cgroups.subsys[ss->subsys_id] = css;
  1325. if (ss->fork)
  1326. ss->fork(ss, p);
  1327. } while_each_thread(g, p);
  1328. read_unlock(&tasklist_lock);
  1329. need_forkexit_callback |= ss->fork || ss->exit;
  1330. ss->active = 1;
  1331. }
  1332. /**
  1333. * cgroup_init_early - initialize cgroups at system boot, and
  1334. * initialize any subsystems that request early init.
  1335. */
  1336. int __init cgroup_init_early(void)
  1337. {
  1338. int i;
  1339. init_cgroup_root(&rootnode);
  1340. list_add(&rootnode.root_list, &roots);
  1341. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  1342. struct cgroup_subsys *ss = subsys[i];
  1343. BUG_ON(!ss->name);
  1344. BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
  1345. BUG_ON(!ss->create);
  1346. BUG_ON(!ss->destroy);
  1347. if (ss->subsys_id != i) {
  1348. printk(KERN_ERR "Subsys %s id == %d\n",
  1349. ss->name, ss->subsys_id);
  1350. BUG();
  1351. }
  1352. if (ss->early_init)
  1353. cgroup_init_subsys(ss);
  1354. }
  1355. return 0;
  1356. }
  1357. /**
  1358. * cgroup_init - register cgroup filesystem and /proc file, and
  1359. * initialize any subsystems that didn't request early init.
  1360. */
  1361. int __init cgroup_init(void)
  1362. {
  1363. int err;
  1364. int i;
  1365. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  1366. struct cgroup_subsys *ss = subsys[i];
  1367. if (!ss->early_init)
  1368. cgroup_init_subsys(ss);
  1369. }
  1370. err = register_filesystem(&cgroup_fs_type);
  1371. if (err < 0)
  1372. goto out;
  1373. out:
  1374. return err;
  1375. }
  1376. /**
  1377. * cgroup_fork - attach newly forked task to its parents cgroup.
  1378. * @tsk: pointer to task_struct of forking parent process.
  1379. *
  1380. * Description: A task inherits its parent's cgroup at fork().
  1381. *
  1382. * A pointer to the shared css_set was automatically copied in
  1383. * fork.c by dup_task_struct(). However, we ignore that copy, since
  1384. * it was not made under the protection of RCU or cgroup_mutex, so
  1385. * might no longer be a valid cgroup pointer. attach_task() might
  1386. * have already changed current->cgroup, allowing the previously
  1387. * referenced cgroup to be removed and freed.
  1388. *
  1389. * At the point that cgroup_fork() is called, 'current' is the parent
  1390. * task, and the passed argument 'child' points to the child task.
  1391. */
  1392. void cgroup_fork(struct task_struct *child)
  1393. {
  1394. rcu_read_lock();
  1395. child->cgroups = rcu_dereference(current->cgroups);
  1396. get_css_set(&child->cgroups);
  1397. rcu_read_unlock();
  1398. }
  1399. /**
  1400. * cgroup_fork_callbacks - called on a new task very soon before
  1401. * adding it to the tasklist. No need to take any locks since no-one
  1402. * can be operating on this task
  1403. */
  1404. void cgroup_fork_callbacks(struct task_struct *child)
  1405. {
  1406. if (need_forkexit_callback) {
  1407. int i;
  1408. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  1409. struct cgroup_subsys *ss = subsys[i];
  1410. if (ss->fork)
  1411. ss->fork(ss, child);
  1412. }
  1413. }
  1414. }
  1415. /**
  1416. * cgroup_exit - detach cgroup from exiting task
  1417. * @tsk: pointer to task_struct of exiting process
  1418. *
  1419. * Description: Detach cgroup from @tsk and release it.
  1420. *
  1421. * Note that cgroups marked notify_on_release force every task in
  1422. * them to take the global cgroup_mutex mutex when exiting.
  1423. * This could impact scaling on very large systems. Be reluctant to
  1424. * use notify_on_release cgroups where very high task exit scaling
  1425. * is required on large systems.
  1426. *
  1427. * the_top_cgroup_hack:
  1428. *
  1429. * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
  1430. *
  1431. * We call cgroup_exit() while the task is still competent to
  1432. * handle notify_on_release(), then leave the task attached to the
  1433. * root cgroup in each hierarchy for the remainder of its exit.
  1434. *
  1435. * To do this properly, we would increment the reference count on
  1436. * top_cgroup, and near the very end of the kernel/exit.c do_exit()
  1437. * code we would add a second cgroup function call, to drop that
  1438. * reference. This would just create an unnecessary hot spot on
  1439. * the top_cgroup reference count, to no avail.
  1440. *
  1441. * Normally, holding a reference to a cgroup without bumping its
  1442. * count is unsafe. The cgroup could go away, or someone could
  1443. * attach us to a different cgroup, decrementing the count on
  1444. * the first cgroup that we never incremented. But in this case,
  1445. * top_cgroup isn't going away, and either task has PF_EXITING set,
  1446. * which wards off any attach_task() attempts, or task is a failed
  1447. * fork, never visible to attach_task.
  1448. *
  1449. */
  1450. void cgroup_exit(struct task_struct *tsk, int run_callbacks)
  1451. {
  1452. int i;
  1453. if (run_callbacks && need_forkexit_callback) {
  1454. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  1455. struct cgroup_subsys *ss = subsys[i];
  1456. if (ss->exit)
  1457. ss->exit(ss, tsk);
  1458. }
  1459. }
  1460. /* Reassign the task to the init_css_set. */
  1461. task_lock(tsk);
  1462. put_css_set(&tsk->cgroups);
  1463. tsk->cgroups = init_task.cgroups;
  1464. task_unlock(tsk);
  1465. }
  1466. /**
  1467. * cgroup_clone - duplicate the current cgroup in the hierarchy
  1468. * that the given subsystem is attached to, and move this task into
  1469. * the new child
  1470. */
  1471. int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys)
  1472. {
  1473. struct dentry *dentry;
  1474. int ret = 0;
  1475. char nodename[MAX_CGROUP_TYPE_NAMELEN];
  1476. struct cgroup *parent, *child;
  1477. struct inode *inode;
  1478. struct css_set *cg;
  1479. struct cgroupfs_root *root;
  1480. struct cgroup_subsys *ss;
  1481. /* We shouldn't be called by an unregistered subsystem */
  1482. BUG_ON(!subsys->active);
  1483. /* First figure out what hierarchy and cgroup we're dealing
  1484. * with, and pin them so we can drop cgroup_mutex */
  1485. mutex_lock(&cgroup_mutex);
  1486. again:
  1487. root = subsys->root;
  1488. if (root == &rootnode) {
  1489. printk(KERN_INFO
  1490. "Not cloning cgroup for unused subsystem %s\n",
  1491. subsys->name);
  1492. mutex_unlock(&cgroup_mutex);
  1493. return 0;
  1494. }
  1495. cg = &tsk->cgroups;
  1496. parent = task_cgroup(tsk, subsys->subsys_id);
  1497. snprintf(nodename, MAX_CGROUP_TYPE_NAMELEN, "node_%d", tsk->pid);
  1498. /* Pin the hierarchy */
  1499. atomic_inc(&parent->root->sb->s_active);
  1500. mutex_unlock(&cgroup_mutex);
  1501. /* Now do the VFS work to create a cgroup */
  1502. inode = parent->dentry->d_inode;
  1503. /* Hold the parent directory mutex across this operation to
  1504. * stop anyone else deleting the new cgroup */
  1505. mutex_lock(&inode->i_mutex);
  1506. dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
  1507. if (IS_ERR(dentry)) {
  1508. printk(KERN_INFO
  1509. "Couldn't allocate dentry for %s: %ld\n", nodename,
  1510. PTR_ERR(dentry));
  1511. ret = PTR_ERR(dentry);
  1512. goto out_release;
  1513. }
  1514. /* Create the cgroup directory, which also creates the cgroup */
  1515. ret = vfs_mkdir(inode, dentry, S_IFDIR | 0755);
  1516. child = __d_cont(dentry);
  1517. dput(dentry);
  1518. if (ret) {
  1519. printk(KERN_INFO
  1520. "Failed to create cgroup %s: %d\n", nodename,
  1521. ret);
  1522. goto out_release;
  1523. }
  1524. if (!child) {
  1525. printk(KERN_INFO
  1526. "Couldn't find new cgroup %s\n", nodename);
  1527. ret = -ENOMEM;
  1528. goto out_release;
  1529. }
  1530. /* The cgroup now exists. Retake cgroup_mutex and check
  1531. * that we're still in the same state that we thought we
  1532. * were. */
  1533. mutex_lock(&cgroup_mutex);
  1534. if ((root != subsys->root) ||
  1535. (parent != task_cgroup(tsk, subsys->subsys_id))) {
  1536. /* Aargh, we raced ... */
  1537. mutex_unlock(&inode->i_mutex);
  1538. deactivate_super(parent->root->sb);
  1539. /* The cgroup is still accessible in the VFS, but
  1540. * we're not going to try to rmdir() it at this
  1541. * point. */
  1542. printk(KERN_INFO
  1543. "Race in cgroup_clone() - leaking cgroup %s\n",
  1544. nodename);
  1545. goto again;
  1546. }
  1547. /* do any required auto-setup */
  1548. for_each_subsys(root, ss) {
  1549. if (ss->post_clone)
  1550. ss->post_clone(ss, child);
  1551. }
  1552. /* All seems fine. Finish by moving the task into the new cgroup */
  1553. ret = attach_task(child, tsk);
  1554. mutex_unlock(&cgroup_mutex);
  1555. out_release:
  1556. mutex_unlock(&inode->i_mutex);
  1557. deactivate_super(parent->root->sb);
  1558. return ret;
  1559. }
  1560. /*
  1561. * See if "cont" is a descendant of the current task's cgroup in
  1562. * the appropriate hierarchy
  1563. *
  1564. * If we are sending in dummytop, then presumably we are creating
  1565. * the top cgroup in the subsystem.
  1566. *
  1567. * Called only by the ns (nsproxy) cgroup.
  1568. */
  1569. int cgroup_is_descendant(const struct cgroup *cont)
  1570. {
  1571. int ret;
  1572. struct cgroup *target;
  1573. int subsys_id;
  1574. if (cont == dummytop)
  1575. return 1;
  1576. get_first_subsys(cont, NULL, &subsys_id);
  1577. target = task_cgroup(current, subsys_id);
  1578. while (cont != target && cont!= cont->top_cgroup)
  1579. cont = cont->parent;
  1580. ret = (cont == target);
  1581. return ret;
  1582. }