cgroup.c 95 KB

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