cgroup.c 82 KB

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