cgroup.c 84 KB

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