cgroup.c 83 KB

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