cgroup.c 71 KB

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