cgroup.c 82 KB

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