cgroup.c 95 KB

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