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