cgroup.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233
  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. list_del(&root->root_list);
  963. root_count--;
  964. mutex_unlock(&cgroup_mutex);
  965. kfree(root);
  966. kill_litter_super(sb);
  967. }
  968. static struct file_system_type cgroup_fs_type = {
  969. .name = "cgroup",
  970. .get_sb = cgroup_get_sb,
  971. .kill_sb = cgroup_kill_sb,
  972. };
  973. static inline struct cgroup *__d_cgrp(struct dentry *dentry)
  974. {
  975. return dentry->d_fsdata;
  976. }
  977. static inline struct cftype *__d_cft(struct dentry *dentry)
  978. {
  979. return dentry->d_fsdata;
  980. }
  981. /**
  982. * cgroup_path - generate the path of a cgroup
  983. * @cgrp: the cgroup in question
  984. * @buf: the buffer to write the path into
  985. * @buflen: the length of the buffer
  986. *
  987. * Called with cgroup_mutex held or else with an RCU-protected cgroup
  988. * reference. Writes path of cgroup into buf. Returns 0 on success,
  989. * -errno on error.
  990. */
  991. int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
  992. {
  993. char *start;
  994. struct dentry *dentry = rcu_dereference(cgrp->dentry);
  995. if (!dentry || cgrp == dummytop) {
  996. /*
  997. * Inactive subsystems have no dentry for their root
  998. * cgroup
  999. */
  1000. strcpy(buf, "/");
  1001. return 0;
  1002. }
  1003. start = buf + buflen;
  1004. *--start = '\0';
  1005. for (;;) {
  1006. int len = dentry->d_name.len;
  1007. if ((start -= len) < buf)
  1008. return -ENAMETOOLONG;
  1009. memcpy(start, cgrp->dentry->d_name.name, len);
  1010. cgrp = cgrp->parent;
  1011. if (!cgrp)
  1012. break;
  1013. dentry = rcu_dereference(cgrp->dentry);
  1014. if (!cgrp->parent)
  1015. continue;
  1016. if (--start < buf)
  1017. return -ENAMETOOLONG;
  1018. *start = '/';
  1019. }
  1020. memmove(buf, start, buf + buflen - start);
  1021. return 0;
  1022. }
  1023. /*
  1024. * Return the first subsystem attached to a cgroup's hierarchy, and
  1025. * its subsystem id.
  1026. */
  1027. static void get_first_subsys(const struct cgroup *cgrp,
  1028. struct cgroup_subsys_state **css, int *subsys_id)
  1029. {
  1030. const struct cgroupfs_root *root = cgrp->root;
  1031. const struct cgroup_subsys *test_ss;
  1032. BUG_ON(list_empty(&root->subsys_list));
  1033. test_ss = list_entry(root->subsys_list.next,
  1034. struct cgroup_subsys, sibling);
  1035. if (css) {
  1036. *css = cgrp->subsys[test_ss->subsys_id];
  1037. BUG_ON(!*css);
  1038. }
  1039. if (subsys_id)
  1040. *subsys_id = test_ss->subsys_id;
  1041. }
  1042. /**
  1043. * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
  1044. * @cgrp: the cgroup the task is attaching to
  1045. * @tsk: the task to be attached
  1046. *
  1047. * Call holding cgroup_mutex. May take task_lock of
  1048. * the task 'tsk' during call.
  1049. */
  1050. int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
  1051. {
  1052. int retval = 0;
  1053. struct cgroup_subsys *ss;
  1054. struct cgroup *oldcgrp;
  1055. struct css_set *cg;
  1056. struct css_set *newcg;
  1057. struct cgroupfs_root *root = cgrp->root;
  1058. int subsys_id;
  1059. get_first_subsys(cgrp, NULL, &subsys_id);
  1060. /* Nothing to do if the task is already in that cgroup */
  1061. oldcgrp = task_cgroup(tsk, subsys_id);
  1062. if (cgrp == oldcgrp)
  1063. return 0;
  1064. for_each_subsys(root, ss) {
  1065. if (ss->can_attach) {
  1066. retval = ss->can_attach(ss, cgrp, tsk);
  1067. if (retval)
  1068. return retval;
  1069. }
  1070. }
  1071. task_lock(tsk);
  1072. cg = tsk->cgroups;
  1073. get_css_set(cg);
  1074. task_unlock(tsk);
  1075. /*
  1076. * Locate or allocate a new css_set for this task,
  1077. * based on its final set of cgroups
  1078. */
  1079. newcg = find_css_set(cg, cgrp);
  1080. put_css_set(cg);
  1081. if (!newcg)
  1082. return -ENOMEM;
  1083. task_lock(tsk);
  1084. if (tsk->flags & PF_EXITING) {
  1085. task_unlock(tsk);
  1086. put_css_set(newcg);
  1087. return -ESRCH;
  1088. }
  1089. rcu_assign_pointer(tsk->cgroups, newcg);
  1090. task_unlock(tsk);
  1091. /* Update the css_set linked lists if we're using them */
  1092. write_lock(&css_set_lock);
  1093. if (!list_empty(&tsk->cg_list)) {
  1094. list_del(&tsk->cg_list);
  1095. list_add(&tsk->cg_list, &newcg->tasks);
  1096. }
  1097. write_unlock(&css_set_lock);
  1098. for_each_subsys(root, ss) {
  1099. if (ss->attach)
  1100. ss->attach(ss, cgrp, oldcgrp, tsk);
  1101. }
  1102. set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
  1103. synchronize_rcu();
  1104. put_css_set(cg);
  1105. return 0;
  1106. }
  1107. /*
  1108. * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
  1109. * held. May take task_lock of task
  1110. */
  1111. static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
  1112. {
  1113. struct task_struct *tsk;
  1114. const struct cred *cred = current_cred(), *tcred;
  1115. int ret;
  1116. if (pid) {
  1117. rcu_read_lock();
  1118. tsk = find_task_by_vpid(pid);
  1119. if (!tsk || tsk->flags & PF_EXITING) {
  1120. rcu_read_unlock();
  1121. return -ESRCH;
  1122. }
  1123. tcred = __task_cred(tsk);
  1124. if (cred->euid &&
  1125. cred->euid != tcred->uid &&
  1126. cred->euid != tcred->suid) {
  1127. rcu_read_unlock();
  1128. return -EACCES;
  1129. }
  1130. get_task_struct(tsk);
  1131. rcu_read_unlock();
  1132. } else {
  1133. tsk = current;
  1134. get_task_struct(tsk);
  1135. }
  1136. ret = cgroup_attach_task(cgrp, tsk);
  1137. put_task_struct(tsk);
  1138. return ret;
  1139. }
  1140. static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
  1141. {
  1142. int ret;
  1143. if (!cgroup_lock_live_group(cgrp))
  1144. return -ENODEV;
  1145. ret = attach_task_by_pid(cgrp, pid);
  1146. cgroup_unlock();
  1147. return ret;
  1148. }
  1149. /* The various types of files and directories in a cgroup file system */
  1150. enum cgroup_filetype {
  1151. FILE_ROOT,
  1152. FILE_DIR,
  1153. FILE_TASKLIST,
  1154. FILE_NOTIFY_ON_RELEASE,
  1155. FILE_RELEASE_AGENT,
  1156. };
  1157. /**
  1158. * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
  1159. * @cgrp: the cgroup to be checked for liveness
  1160. *
  1161. * On success, returns true; the lock should be later released with
  1162. * cgroup_unlock(). On failure returns false with no lock held.
  1163. */
  1164. bool cgroup_lock_live_group(struct cgroup *cgrp)
  1165. {
  1166. mutex_lock(&cgroup_mutex);
  1167. if (cgroup_is_removed(cgrp)) {
  1168. mutex_unlock(&cgroup_mutex);
  1169. return false;
  1170. }
  1171. return true;
  1172. }
  1173. static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
  1174. const char *buffer)
  1175. {
  1176. BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
  1177. if (!cgroup_lock_live_group(cgrp))
  1178. return -ENODEV;
  1179. strcpy(cgrp->root->release_agent_path, buffer);
  1180. cgroup_unlock();
  1181. return 0;
  1182. }
  1183. static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
  1184. struct seq_file *seq)
  1185. {
  1186. if (!cgroup_lock_live_group(cgrp))
  1187. return -ENODEV;
  1188. seq_puts(seq, cgrp->root->release_agent_path);
  1189. seq_putc(seq, '\n');
  1190. cgroup_unlock();
  1191. return 0;
  1192. }
  1193. /* A buffer size big enough for numbers or short strings */
  1194. #define CGROUP_LOCAL_BUFFER_SIZE 64
  1195. static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
  1196. struct file *file,
  1197. const char __user *userbuf,
  1198. size_t nbytes, loff_t *unused_ppos)
  1199. {
  1200. char buffer[CGROUP_LOCAL_BUFFER_SIZE];
  1201. int retval = 0;
  1202. char *end;
  1203. if (!nbytes)
  1204. return -EINVAL;
  1205. if (nbytes >= sizeof(buffer))
  1206. return -E2BIG;
  1207. if (copy_from_user(buffer, userbuf, nbytes))
  1208. return -EFAULT;
  1209. buffer[nbytes] = 0; /* nul-terminate */
  1210. strstrip(buffer);
  1211. if (cft->write_u64) {
  1212. u64 val = simple_strtoull(buffer, &end, 0);
  1213. if (*end)
  1214. return -EINVAL;
  1215. retval = cft->write_u64(cgrp, cft, val);
  1216. } else {
  1217. s64 val = simple_strtoll(buffer, &end, 0);
  1218. if (*end)
  1219. return -EINVAL;
  1220. retval = cft->write_s64(cgrp, cft, val);
  1221. }
  1222. if (!retval)
  1223. retval = nbytes;
  1224. return retval;
  1225. }
  1226. static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
  1227. struct file *file,
  1228. const char __user *userbuf,
  1229. size_t nbytes, loff_t *unused_ppos)
  1230. {
  1231. char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
  1232. int retval = 0;
  1233. size_t max_bytes = cft->max_write_len;
  1234. char *buffer = local_buffer;
  1235. if (!max_bytes)
  1236. max_bytes = sizeof(local_buffer) - 1;
  1237. if (nbytes >= max_bytes)
  1238. return -E2BIG;
  1239. /* Allocate a dynamic buffer if we need one */
  1240. if (nbytes >= sizeof(local_buffer)) {
  1241. buffer = kmalloc(nbytes + 1, GFP_KERNEL);
  1242. if (buffer == NULL)
  1243. return -ENOMEM;
  1244. }
  1245. if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
  1246. retval = -EFAULT;
  1247. goto out;
  1248. }
  1249. buffer[nbytes] = 0; /* nul-terminate */
  1250. strstrip(buffer);
  1251. retval = cft->write_string(cgrp, cft, buffer);
  1252. if (!retval)
  1253. retval = nbytes;
  1254. out:
  1255. if (buffer != local_buffer)
  1256. kfree(buffer);
  1257. return retval;
  1258. }
  1259. static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
  1260. size_t nbytes, loff_t *ppos)
  1261. {
  1262. struct cftype *cft = __d_cft(file->f_dentry);
  1263. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  1264. if (cgroup_is_removed(cgrp))
  1265. return -ENODEV;
  1266. if (cft->write)
  1267. return cft->write(cgrp, cft, file, buf, nbytes, ppos);
  1268. if (cft->write_u64 || cft->write_s64)
  1269. return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
  1270. if (cft->write_string)
  1271. return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
  1272. if (cft->trigger) {
  1273. int ret = cft->trigger(cgrp, (unsigned int)cft->private);
  1274. return ret ? ret : nbytes;
  1275. }
  1276. return -EINVAL;
  1277. }
  1278. static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
  1279. struct file *file,
  1280. char __user *buf, size_t nbytes,
  1281. loff_t *ppos)
  1282. {
  1283. char tmp[CGROUP_LOCAL_BUFFER_SIZE];
  1284. u64 val = cft->read_u64(cgrp, cft);
  1285. int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
  1286. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  1287. }
  1288. static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
  1289. struct file *file,
  1290. char __user *buf, size_t nbytes,
  1291. loff_t *ppos)
  1292. {
  1293. char tmp[CGROUP_LOCAL_BUFFER_SIZE];
  1294. s64 val = cft->read_s64(cgrp, cft);
  1295. int len = sprintf(tmp, "%lld\n", (long long) val);
  1296. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  1297. }
  1298. static ssize_t cgroup_file_read(struct file *file, char __user *buf,
  1299. size_t nbytes, loff_t *ppos)
  1300. {
  1301. struct cftype *cft = __d_cft(file->f_dentry);
  1302. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  1303. if (cgroup_is_removed(cgrp))
  1304. return -ENODEV;
  1305. if (cft->read)
  1306. return cft->read(cgrp, cft, file, buf, nbytes, ppos);
  1307. if (cft->read_u64)
  1308. return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
  1309. if (cft->read_s64)
  1310. return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
  1311. return -EINVAL;
  1312. }
  1313. /*
  1314. * seqfile ops/methods for returning structured data. Currently just
  1315. * supports string->u64 maps, but can be extended in future.
  1316. */
  1317. struct cgroup_seqfile_state {
  1318. struct cftype *cft;
  1319. struct cgroup *cgroup;
  1320. };
  1321. static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
  1322. {
  1323. struct seq_file *sf = cb->state;
  1324. return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
  1325. }
  1326. static int cgroup_seqfile_show(struct seq_file *m, void *arg)
  1327. {
  1328. struct cgroup_seqfile_state *state = m->private;
  1329. struct cftype *cft = state->cft;
  1330. if (cft->read_map) {
  1331. struct cgroup_map_cb cb = {
  1332. .fill = cgroup_map_add,
  1333. .state = m,
  1334. };
  1335. return cft->read_map(state->cgroup, cft, &cb);
  1336. }
  1337. return cft->read_seq_string(state->cgroup, cft, m);
  1338. }
  1339. static int cgroup_seqfile_release(struct inode *inode, struct file *file)
  1340. {
  1341. struct seq_file *seq = file->private_data;
  1342. kfree(seq->private);
  1343. return single_release(inode, file);
  1344. }
  1345. static struct file_operations cgroup_seqfile_operations = {
  1346. .read = seq_read,
  1347. .write = cgroup_file_write,
  1348. .llseek = seq_lseek,
  1349. .release = cgroup_seqfile_release,
  1350. };
  1351. static int cgroup_file_open(struct inode *inode, struct file *file)
  1352. {
  1353. int err;
  1354. struct cftype *cft;
  1355. err = generic_file_open(inode, file);
  1356. if (err)
  1357. return err;
  1358. cft = __d_cft(file->f_dentry);
  1359. if (cft->read_map || cft->read_seq_string) {
  1360. struct cgroup_seqfile_state *state =
  1361. kzalloc(sizeof(*state), GFP_USER);
  1362. if (!state)
  1363. return -ENOMEM;
  1364. state->cft = cft;
  1365. state->cgroup = __d_cgrp(file->f_dentry->d_parent);
  1366. file->f_op = &cgroup_seqfile_operations;
  1367. err = single_open(file, cgroup_seqfile_show, state);
  1368. if (err < 0)
  1369. kfree(state);
  1370. } else if (cft->open)
  1371. err = cft->open(inode, file);
  1372. else
  1373. err = 0;
  1374. return err;
  1375. }
  1376. static int cgroup_file_release(struct inode *inode, struct file *file)
  1377. {
  1378. struct cftype *cft = __d_cft(file->f_dentry);
  1379. if (cft->release)
  1380. return cft->release(inode, file);
  1381. return 0;
  1382. }
  1383. /*
  1384. * cgroup_rename - Only allow simple rename of directories in place.
  1385. */
  1386. static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
  1387. struct inode *new_dir, struct dentry *new_dentry)
  1388. {
  1389. if (!S_ISDIR(old_dentry->d_inode->i_mode))
  1390. return -ENOTDIR;
  1391. if (new_dentry->d_inode)
  1392. return -EEXIST;
  1393. if (old_dir != new_dir)
  1394. return -EIO;
  1395. return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  1396. }
  1397. static struct file_operations cgroup_file_operations = {
  1398. .read = cgroup_file_read,
  1399. .write = cgroup_file_write,
  1400. .llseek = generic_file_llseek,
  1401. .open = cgroup_file_open,
  1402. .release = cgroup_file_release,
  1403. };
  1404. static struct inode_operations cgroup_dir_inode_operations = {
  1405. .lookup = simple_lookup,
  1406. .mkdir = cgroup_mkdir,
  1407. .rmdir = cgroup_rmdir,
  1408. .rename = cgroup_rename,
  1409. };
  1410. static int cgroup_create_file(struct dentry *dentry, int mode,
  1411. struct super_block *sb)
  1412. {
  1413. static struct dentry_operations cgroup_dops = {
  1414. .d_iput = cgroup_diput,
  1415. };
  1416. struct inode *inode;
  1417. if (!dentry)
  1418. return -ENOENT;
  1419. if (dentry->d_inode)
  1420. return -EEXIST;
  1421. inode = cgroup_new_inode(mode, sb);
  1422. if (!inode)
  1423. return -ENOMEM;
  1424. if (S_ISDIR(mode)) {
  1425. inode->i_op = &cgroup_dir_inode_operations;
  1426. inode->i_fop = &simple_dir_operations;
  1427. /* start off with i_nlink == 2 (for "." entry) */
  1428. inc_nlink(inode);
  1429. /* start with the directory inode held, so that we can
  1430. * populate it without racing with another mkdir */
  1431. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1432. } else if (S_ISREG(mode)) {
  1433. inode->i_size = 0;
  1434. inode->i_fop = &cgroup_file_operations;
  1435. }
  1436. dentry->d_op = &cgroup_dops;
  1437. d_instantiate(dentry, inode);
  1438. dget(dentry); /* Extra count - pin the dentry in core */
  1439. return 0;
  1440. }
  1441. /*
  1442. * cgroup_create_dir - create a directory for an object.
  1443. * @cgrp: the cgroup we create the directory for. It must have a valid
  1444. * ->parent field. And we are going to fill its ->dentry field.
  1445. * @dentry: dentry of the new cgroup
  1446. * @mode: mode to set on new directory.
  1447. */
  1448. static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
  1449. int mode)
  1450. {
  1451. struct dentry *parent;
  1452. int error = 0;
  1453. parent = cgrp->parent->dentry;
  1454. error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
  1455. if (!error) {
  1456. dentry->d_fsdata = cgrp;
  1457. inc_nlink(parent->d_inode);
  1458. rcu_assign_pointer(cgrp->dentry, dentry);
  1459. dget(dentry);
  1460. }
  1461. dput(dentry);
  1462. return error;
  1463. }
  1464. int cgroup_add_file(struct cgroup *cgrp,
  1465. struct cgroup_subsys *subsys,
  1466. const struct cftype *cft)
  1467. {
  1468. struct dentry *dir = cgrp->dentry;
  1469. struct dentry *dentry;
  1470. int error;
  1471. char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
  1472. if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
  1473. strcpy(name, subsys->name);
  1474. strcat(name, ".");
  1475. }
  1476. strcat(name, cft->name);
  1477. BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
  1478. dentry = lookup_one_len(name, dir, strlen(name));
  1479. if (!IS_ERR(dentry)) {
  1480. error = cgroup_create_file(dentry, 0644 | S_IFREG,
  1481. cgrp->root->sb);
  1482. if (!error)
  1483. dentry->d_fsdata = (void *)cft;
  1484. dput(dentry);
  1485. } else
  1486. error = PTR_ERR(dentry);
  1487. return error;
  1488. }
  1489. int cgroup_add_files(struct cgroup *cgrp,
  1490. struct cgroup_subsys *subsys,
  1491. const struct cftype cft[],
  1492. int count)
  1493. {
  1494. int i, err;
  1495. for (i = 0; i < count; i++) {
  1496. err = cgroup_add_file(cgrp, subsys, &cft[i]);
  1497. if (err)
  1498. return err;
  1499. }
  1500. return 0;
  1501. }
  1502. /**
  1503. * cgroup_task_count - count the number of tasks in a cgroup.
  1504. * @cgrp: the cgroup in question
  1505. *
  1506. * Return the number of tasks in the cgroup.
  1507. */
  1508. int cgroup_task_count(const struct cgroup *cgrp)
  1509. {
  1510. int count = 0;
  1511. struct cg_cgroup_link *link;
  1512. read_lock(&css_set_lock);
  1513. list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
  1514. count += atomic_read(&link->cg->refcount);
  1515. }
  1516. read_unlock(&css_set_lock);
  1517. return count;
  1518. }
  1519. /*
  1520. * Advance a list_head iterator. The iterator should be positioned at
  1521. * the start of a css_set
  1522. */
  1523. static void cgroup_advance_iter(struct cgroup *cgrp,
  1524. struct cgroup_iter *it)
  1525. {
  1526. struct list_head *l = it->cg_link;
  1527. struct cg_cgroup_link *link;
  1528. struct css_set *cg;
  1529. /* Advance to the next non-empty css_set */
  1530. do {
  1531. l = l->next;
  1532. if (l == &cgrp->css_sets) {
  1533. it->cg_link = NULL;
  1534. return;
  1535. }
  1536. link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
  1537. cg = link->cg;
  1538. } while (list_empty(&cg->tasks));
  1539. it->cg_link = l;
  1540. it->task = cg->tasks.next;
  1541. }
  1542. /*
  1543. * To reduce the fork() overhead for systems that are not actually
  1544. * using their cgroups capability, we don't maintain the lists running
  1545. * through each css_set to its tasks until we see the list actually
  1546. * used - in other words after the first call to cgroup_iter_start().
  1547. *
  1548. * The tasklist_lock is not held here, as do_each_thread() and
  1549. * while_each_thread() are protected by RCU.
  1550. */
  1551. static void cgroup_enable_task_cg_lists(void)
  1552. {
  1553. struct task_struct *p, *g;
  1554. write_lock(&css_set_lock);
  1555. use_task_css_set_links = 1;
  1556. do_each_thread(g, p) {
  1557. task_lock(p);
  1558. /*
  1559. * We should check if the process is exiting, otherwise
  1560. * it will race with cgroup_exit() in that the list
  1561. * entry won't be deleted though the process has exited.
  1562. */
  1563. if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
  1564. list_add(&p->cg_list, &p->cgroups->tasks);
  1565. task_unlock(p);
  1566. } while_each_thread(g, p);
  1567. write_unlock(&css_set_lock);
  1568. }
  1569. void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
  1570. {
  1571. /*
  1572. * The first time anyone tries to iterate across a cgroup,
  1573. * we need to enable the list linking each css_set to its
  1574. * tasks, and fix up all existing tasks.
  1575. */
  1576. if (!use_task_css_set_links)
  1577. cgroup_enable_task_cg_lists();
  1578. read_lock(&css_set_lock);
  1579. it->cg_link = &cgrp->css_sets;
  1580. cgroup_advance_iter(cgrp, it);
  1581. }
  1582. struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
  1583. struct cgroup_iter *it)
  1584. {
  1585. struct task_struct *res;
  1586. struct list_head *l = it->task;
  1587. struct cg_cgroup_link *link;
  1588. /* If the iterator cg is NULL, we have no tasks */
  1589. if (!it->cg_link)
  1590. return NULL;
  1591. res = list_entry(l, struct task_struct, cg_list);
  1592. /* Advance iterator to find next entry */
  1593. l = l->next;
  1594. link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
  1595. if (l == &link->cg->tasks) {
  1596. /* We reached the end of this task list - move on to
  1597. * the next cg_cgroup_link */
  1598. cgroup_advance_iter(cgrp, it);
  1599. } else {
  1600. it->task = l;
  1601. }
  1602. return res;
  1603. }
  1604. void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
  1605. {
  1606. read_unlock(&css_set_lock);
  1607. }
  1608. static inline int started_after_time(struct task_struct *t1,
  1609. struct timespec *time,
  1610. struct task_struct *t2)
  1611. {
  1612. int start_diff = timespec_compare(&t1->start_time, time);
  1613. if (start_diff > 0) {
  1614. return 1;
  1615. } else if (start_diff < 0) {
  1616. return 0;
  1617. } else {
  1618. /*
  1619. * Arbitrarily, if two processes started at the same
  1620. * time, we'll say that the lower pointer value
  1621. * started first. Note that t2 may have exited by now
  1622. * so this may not be a valid pointer any longer, but
  1623. * that's fine - it still serves to distinguish
  1624. * between two tasks started (effectively) simultaneously.
  1625. */
  1626. return t1 > t2;
  1627. }
  1628. }
  1629. /*
  1630. * This function is a callback from heap_insert() and is used to order
  1631. * the heap.
  1632. * In this case we order the heap in descending task start time.
  1633. */
  1634. static inline int started_after(void *p1, void *p2)
  1635. {
  1636. struct task_struct *t1 = p1;
  1637. struct task_struct *t2 = p2;
  1638. return started_after_time(t1, &t2->start_time, t2);
  1639. }
  1640. /**
  1641. * cgroup_scan_tasks - iterate though all the tasks in a cgroup
  1642. * @scan: struct cgroup_scanner containing arguments for the scan
  1643. *
  1644. * Arguments include pointers to callback functions test_task() and
  1645. * process_task().
  1646. * Iterate through all the tasks in a cgroup, calling test_task() for each,
  1647. * and if it returns true, call process_task() for it also.
  1648. * The test_task pointer may be NULL, meaning always true (select all tasks).
  1649. * Effectively duplicates cgroup_iter_{start,next,end}()
  1650. * but does not lock css_set_lock for the call to process_task().
  1651. * The struct cgroup_scanner may be embedded in any structure of the caller's
  1652. * creation.
  1653. * It is guaranteed that process_task() will act on every task that
  1654. * is a member of the cgroup for the duration of this call. This
  1655. * function may or may not call process_task() for tasks that exit
  1656. * or move to a different cgroup during the call, or are forked or
  1657. * move into the cgroup during the call.
  1658. *
  1659. * Note that test_task() may be called with locks held, and may in some
  1660. * situations be called multiple times for the same task, so it should
  1661. * be cheap.
  1662. * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
  1663. * pre-allocated and will be used for heap operations (and its "gt" member will
  1664. * be overwritten), else a temporary heap will be used (allocation of which
  1665. * may cause this function to fail).
  1666. */
  1667. int cgroup_scan_tasks(struct cgroup_scanner *scan)
  1668. {
  1669. int retval, i;
  1670. struct cgroup_iter it;
  1671. struct task_struct *p, *dropped;
  1672. /* Never dereference latest_task, since it's not refcounted */
  1673. struct task_struct *latest_task = NULL;
  1674. struct ptr_heap tmp_heap;
  1675. struct ptr_heap *heap;
  1676. struct timespec latest_time = { 0, 0 };
  1677. if (scan->heap) {
  1678. /* The caller supplied our heap and pre-allocated its memory */
  1679. heap = scan->heap;
  1680. heap->gt = &started_after;
  1681. } else {
  1682. /* We need to allocate our own heap memory */
  1683. heap = &tmp_heap;
  1684. retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
  1685. if (retval)
  1686. /* cannot allocate the heap */
  1687. return retval;
  1688. }
  1689. again:
  1690. /*
  1691. * Scan tasks in the cgroup, using the scanner's "test_task" callback
  1692. * to determine which are of interest, and using the scanner's
  1693. * "process_task" callback to process any of them that need an update.
  1694. * Since we don't want to hold any locks during the task updates,
  1695. * gather tasks to be processed in a heap structure.
  1696. * The heap is sorted by descending task start time.
  1697. * If the statically-sized heap fills up, we overflow tasks that
  1698. * started later, and in future iterations only consider tasks that
  1699. * started after the latest task in the previous pass. This
  1700. * guarantees forward progress and that we don't miss any tasks.
  1701. */
  1702. heap->size = 0;
  1703. cgroup_iter_start(scan->cg, &it);
  1704. while ((p = cgroup_iter_next(scan->cg, &it))) {
  1705. /*
  1706. * Only affect tasks that qualify per the caller's callback,
  1707. * if he provided one
  1708. */
  1709. if (scan->test_task && !scan->test_task(p, scan))
  1710. continue;
  1711. /*
  1712. * Only process tasks that started after the last task
  1713. * we processed
  1714. */
  1715. if (!started_after_time(p, &latest_time, latest_task))
  1716. continue;
  1717. dropped = heap_insert(heap, p);
  1718. if (dropped == NULL) {
  1719. /*
  1720. * The new task was inserted; the heap wasn't
  1721. * previously full
  1722. */
  1723. get_task_struct(p);
  1724. } else if (dropped != p) {
  1725. /*
  1726. * The new task was inserted, and pushed out a
  1727. * different task
  1728. */
  1729. get_task_struct(p);
  1730. put_task_struct(dropped);
  1731. }
  1732. /*
  1733. * Else the new task was newer than anything already in
  1734. * the heap and wasn't inserted
  1735. */
  1736. }
  1737. cgroup_iter_end(scan->cg, &it);
  1738. if (heap->size) {
  1739. for (i = 0; i < heap->size; i++) {
  1740. struct task_struct *q = heap->ptrs[i];
  1741. if (i == 0) {
  1742. latest_time = q->start_time;
  1743. latest_task = q;
  1744. }
  1745. /* Process the task per the caller's callback */
  1746. scan->process_task(q, scan);
  1747. put_task_struct(q);
  1748. }
  1749. /*
  1750. * If we had to process any tasks at all, scan again
  1751. * in case some of them were in the middle of forking
  1752. * children that didn't get processed.
  1753. * Not the most efficient way to do it, but it avoids
  1754. * having to take callback_mutex in the fork path
  1755. */
  1756. goto again;
  1757. }
  1758. if (heap == &tmp_heap)
  1759. heap_free(&tmp_heap);
  1760. return 0;
  1761. }
  1762. /*
  1763. * Stuff for reading the 'tasks' file.
  1764. *
  1765. * Reading this file can return large amounts of data if a cgroup has
  1766. * *lots* of attached tasks. So it may need several calls to read(),
  1767. * but we cannot guarantee that the information we produce is correct
  1768. * unless we produce it entirely atomically.
  1769. *
  1770. */
  1771. /*
  1772. * Load into 'pidarray' up to 'npids' of the tasks using cgroup
  1773. * 'cgrp'. Return actual number of pids loaded. No need to
  1774. * task_lock(p) when reading out p->cgroup, since we're in an RCU
  1775. * read section, so the css_set can't go away, and is
  1776. * immutable after creation.
  1777. */
  1778. static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
  1779. {
  1780. int n = 0, pid;
  1781. struct cgroup_iter it;
  1782. struct task_struct *tsk;
  1783. cgroup_iter_start(cgrp, &it);
  1784. while ((tsk = cgroup_iter_next(cgrp, &it))) {
  1785. if (unlikely(n == npids))
  1786. break;
  1787. pid = task_pid_vnr(tsk);
  1788. if (pid > 0)
  1789. pidarray[n++] = pid;
  1790. }
  1791. cgroup_iter_end(cgrp, &it);
  1792. return n;
  1793. }
  1794. /**
  1795. * cgroupstats_build - build and fill cgroupstats
  1796. * @stats: cgroupstats to fill information into
  1797. * @dentry: A dentry entry belonging to the cgroup for which stats have
  1798. * been requested.
  1799. *
  1800. * Build and fill cgroupstats so that taskstats can export it to user
  1801. * space.
  1802. */
  1803. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
  1804. {
  1805. int ret = -EINVAL;
  1806. struct cgroup *cgrp;
  1807. struct cgroup_iter it;
  1808. struct task_struct *tsk;
  1809. /*
  1810. * Validate dentry by checking the superblock operations,
  1811. * and make sure it's a directory.
  1812. */
  1813. if (dentry->d_sb->s_op != &cgroup_ops ||
  1814. !S_ISDIR(dentry->d_inode->i_mode))
  1815. goto err;
  1816. ret = 0;
  1817. cgrp = dentry->d_fsdata;
  1818. cgroup_iter_start(cgrp, &it);
  1819. while ((tsk = cgroup_iter_next(cgrp, &it))) {
  1820. switch (tsk->state) {
  1821. case TASK_RUNNING:
  1822. stats->nr_running++;
  1823. break;
  1824. case TASK_INTERRUPTIBLE:
  1825. stats->nr_sleeping++;
  1826. break;
  1827. case TASK_UNINTERRUPTIBLE:
  1828. stats->nr_uninterruptible++;
  1829. break;
  1830. case TASK_STOPPED:
  1831. stats->nr_stopped++;
  1832. break;
  1833. default:
  1834. if (delayacct_is_task_waiting_on_io(tsk))
  1835. stats->nr_io_wait++;
  1836. break;
  1837. }
  1838. }
  1839. cgroup_iter_end(cgrp, &it);
  1840. err:
  1841. return ret;
  1842. }
  1843. static int cmppid(const void *a, const void *b)
  1844. {
  1845. return *(pid_t *)a - *(pid_t *)b;
  1846. }
  1847. /*
  1848. * seq_file methods for the "tasks" file. The seq_file position is the
  1849. * next pid to display; the seq_file iterator is a pointer to the pid
  1850. * in the cgroup->tasks_pids array.
  1851. */
  1852. static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
  1853. {
  1854. /*
  1855. * Initially we receive a position value that corresponds to
  1856. * one more than the last pid shown (or 0 on the first call or
  1857. * after a seek to the start). Use a binary-search to find the
  1858. * next pid to display, if any
  1859. */
  1860. struct cgroup *cgrp = s->private;
  1861. int index = 0, pid = *pos;
  1862. int *iter;
  1863. down_read(&cgrp->pids_mutex);
  1864. if (pid) {
  1865. int end = cgrp->pids_length;
  1866. while (index < end) {
  1867. int mid = (index + end) / 2;
  1868. if (cgrp->tasks_pids[mid] == pid) {
  1869. index = mid;
  1870. break;
  1871. } else if (cgrp->tasks_pids[mid] <= pid)
  1872. index = mid + 1;
  1873. else
  1874. end = mid;
  1875. }
  1876. }
  1877. /* If we're off the end of the array, we're done */
  1878. if (index >= cgrp->pids_length)
  1879. return NULL;
  1880. /* Update the abstract position to be the actual pid that we found */
  1881. iter = cgrp->tasks_pids + index;
  1882. *pos = *iter;
  1883. return iter;
  1884. }
  1885. static void cgroup_tasks_stop(struct seq_file *s, void *v)
  1886. {
  1887. struct cgroup *cgrp = s->private;
  1888. up_read(&cgrp->pids_mutex);
  1889. }
  1890. static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos)
  1891. {
  1892. struct cgroup *cgrp = s->private;
  1893. int *p = v;
  1894. int *end = cgrp->tasks_pids + cgrp->pids_length;
  1895. /*
  1896. * Advance to the next pid in the array. If this goes off the
  1897. * end, we're done
  1898. */
  1899. p++;
  1900. if (p >= end) {
  1901. return NULL;
  1902. } else {
  1903. *pos = *p;
  1904. return p;
  1905. }
  1906. }
  1907. static int cgroup_tasks_show(struct seq_file *s, void *v)
  1908. {
  1909. return seq_printf(s, "%d\n", *(int *)v);
  1910. }
  1911. static struct seq_operations cgroup_tasks_seq_operations = {
  1912. .start = cgroup_tasks_start,
  1913. .stop = cgroup_tasks_stop,
  1914. .next = cgroup_tasks_next,
  1915. .show = cgroup_tasks_show,
  1916. };
  1917. static void release_cgroup_pid_array(struct cgroup *cgrp)
  1918. {
  1919. down_write(&cgrp->pids_mutex);
  1920. BUG_ON(!cgrp->pids_use_count);
  1921. if (!--cgrp->pids_use_count) {
  1922. kfree(cgrp->tasks_pids);
  1923. cgrp->tasks_pids = NULL;
  1924. cgrp->pids_length = 0;
  1925. }
  1926. up_write(&cgrp->pids_mutex);
  1927. }
  1928. static int cgroup_tasks_release(struct inode *inode, struct file *file)
  1929. {
  1930. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  1931. if (!(file->f_mode & FMODE_READ))
  1932. return 0;
  1933. release_cgroup_pid_array(cgrp);
  1934. return seq_release(inode, file);
  1935. }
  1936. static struct file_operations cgroup_tasks_operations = {
  1937. .read = seq_read,
  1938. .llseek = seq_lseek,
  1939. .write = cgroup_file_write,
  1940. .release = cgroup_tasks_release,
  1941. };
  1942. /*
  1943. * Handle an open on 'tasks' file. Prepare an array containing the
  1944. * process id's of tasks currently attached to the cgroup being opened.
  1945. */
  1946. static int cgroup_tasks_open(struct inode *unused, struct file *file)
  1947. {
  1948. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  1949. pid_t *pidarray;
  1950. int npids;
  1951. int retval;
  1952. /* Nothing to do for write-only files */
  1953. if (!(file->f_mode & FMODE_READ))
  1954. return 0;
  1955. /*
  1956. * If cgroup gets more users after we read count, we won't have
  1957. * enough space - tough. This race is indistinguishable to the
  1958. * caller from the case that the additional cgroup users didn't
  1959. * show up until sometime later on.
  1960. */
  1961. npids = cgroup_task_count(cgrp);
  1962. pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
  1963. if (!pidarray)
  1964. return -ENOMEM;
  1965. npids = pid_array_load(pidarray, npids, cgrp);
  1966. sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
  1967. /*
  1968. * Store the array in the cgroup, freeing the old
  1969. * array if necessary
  1970. */
  1971. down_write(&cgrp->pids_mutex);
  1972. kfree(cgrp->tasks_pids);
  1973. cgrp->tasks_pids = pidarray;
  1974. cgrp->pids_length = npids;
  1975. cgrp->pids_use_count++;
  1976. up_write(&cgrp->pids_mutex);
  1977. file->f_op = &cgroup_tasks_operations;
  1978. retval = seq_open(file, &cgroup_tasks_seq_operations);
  1979. if (retval) {
  1980. release_cgroup_pid_array(cgrp);
  1981. return retval;
  1982. }
  1983. ((struct seq_file *)file->private_data)->private = cgrp;
  1984. return 0;
  1985. }
  1986. static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
  1987. struct cftype *cft)
  1988. {
  1989. return notify_on_release(cgrp);
  1990. }
  1991. static int cgroup_write_notify_on_release(struct cgroup *cgrp,
  1992. struct cftype *cft,
  1993. u64 val)
  1994. {
  1995. clear_bit(CGRP_RELEASABLE, &cgrp->flags);
  1996. if (val)
  1997. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  1998. else
  1999. clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  2000. return 0;
  2001. }
  2002. /*
  2003. * for the common functions, 'private' gives the type of file
  2004. */
  2005. static struct cftype files[] = {
  2006. {
  2007. .name = "tasks",
  2008. .open = cgroup_tasks_open,
  2009. .write_u64 = cgroup_tasks_write,
  2010. .release = cgroup_tasks_release,
  2011. .private = FILE_TASKLIST,
  2012. },
  2013. {
  2014. .name = "notify_on_release",
  2015. .read_u64 = cgroup_read_notify_on_release,
  2016. .write_u64 = cgroup_write_notify_on_release,
  2017. .private = FILE_NOTIFY_ON_RELEASE,
  2018. },
  2019. };
  2020. static struct cftype cft_release_agent = {
  2021. .name = "release_agent",
  2022. .read_seq_string = cgroup_release_agent_show,
  2023. .write_string = cgroup_release_agent_write,
  2024. .max_write_len = PATH_MAX,
  2025. .private = FILE_RELEASE_AGENT,
  2026. };
  2027. static int cgroup_populate_dir(struct cgroup *cgrp)
  2028. {
  2029. int err;
  2030. struct cgroup_subsys *ss;
  2031. /* First clear out any existing files */
  2032. cgroup_clear_directory(cgrp->dentry);
  2033. err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
  2034. if (err < 0)
  2035. return err;
  2036. if (cgrp == cgrp->top_cgroup) {
  2037. if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
  2038. return err;
  2039. }
  2040. for_each_subsys(cgrp->root, ss) {
  2041. if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
  2042. return err;
  2043. }
  2044. return 0;
  2045. }
  2046. static void init_cgroup_css(struct cgroup_subsys_state *css,
  2047. struct cgroup_subsys *ss,
  2048. struct cgroup *cgrp)
  2049. {
  2050. css->cgroup = cgrp;
  2051. atomic_set(&css->refcnt, 1);
  2052. css->flags = 0;
  2053. if (cgrp == dummytop)
  2054. set_bit(CSS_ROOT, &css->flags);
  2055. BUG_ON(cgrp->subsys[ss->subsys_id]);
  2056. cgrp->subsys[ss->subsys_id] = css;
  2057. }
  2058. static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
  2059. {
  2060. /* We need to take each hierarchy_mutex in a consistent order */
  2061. int i;
  2062. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2063. struct cgroup_subsys *ss = subsys[i];
  2064. if (ss->root == root)
  2065. mutex_lock_nested(&ss->hierarchy_mutex, i);
  2066. }
  2067. }
  2068. static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
  2069. {
  2070. int i;
  2071. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2072. struct cgroup_subsys *ss = subsys[i];
  2073. if (ss->root == root)
  2074. mutex_unlock(&ss->hierarchy_mutex);
  2075. }
  2076. }
  2077. /*
  2078. * cgroup_create - create a cgroup
  2079. * @parent: cgroup that will be parent of the new cgroup
  2080. * @dentry: dentry of the new cgroup
  2081. * @mode: mode to set on new inode
  2082. *
  2083. * Must be called with the mutex on the parent inode held
  2084. */
  2085. static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
  2086. int mode)
  2087. {
  2088. struct cgroup *cgrp;
  2089. struct cgroupfs_root *root = parent->root;
  2090. int err = 0;
  2091. struct cgroup_subsys *ss;
  2092. struct super_block *sb = root->sb;
  2093. cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
  2094. if (!cgrp)
  2095. return -ENOMEM;
  2096. /* Grab a reference on the superblock so the hierarchy doesn't
  2097. * get deleted on unmount if there are child cgroups. This
  2098. * can be done outside cgroup_mutex, since the sb can't
  2099. * disappear while someone has an open control file on the
  2100. * fs */
  2101. atomic_inc(&sb->s_active);
  2102. mutex_lock(&cgroup_mutex);
  2103. init_cgroup_housekeeping(cgrp);
  2104. cgrp->parent = parent;
  2105. cgrp->root = parent->root;
  2106. cgrp->top_cgroup = parent->top_cgroup;
  2107. if (notify_on_release(parent))
  2108. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  2109. for_each_subsys(root, ss) {
  2110. struct cgroup_subsys_state *css = ss->create(ss, cgrp);
  2111. if (IS_ERR(css)) {
  2112. err = PTR_ERR(css);
  2113. goto err_destroy;
  2114. }
  2115. init_cgroup_css(css, ss, cgrp);
  2116. }
  2117. cgroup_lock_hierarchy(root);
  2118. list_add(&cgrp->sibling, &cgrp->parent->children);
  2119. cgroup_unlock_hierarchy(root);
  2120. root->number_of_cgroups++;
  2121. err = cgroup_create_dir(cgrp, dentry, mode);
  2122. if (err < 0)
  2123. goto err_remove;
  2124. /* The cgroup directory was pre-locked for us */
  2125. BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
  2126. err = cgroup_populate_dir(cgrp);
  2127. /* If err < 0, we have a half-filled directory - oh well ;) */
  2128. mutex_unlock(&cgroup_mutex);
  2129. mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
  2130. return 0;
  2131. err_remove:
  2132. list_del(&cgrp->sibling);
  2133. root->number_of_cgroups--;
  2134. err_destroy:
  2135. for_each_subsys(root, ss) {
  2136. if (cgrp->subsys[ss->subsys_id])
  2137. ss->destroy(ss, cgrp);
  2138. }
  2139. mutex_unlock(&cgroup_mutex);
  2140. /* Release the reference count that we took on the superblock */
  2141. deactivate_super(sb);
  2142. kfree(cgrp);
  2143. return err;
  2144. }
  2145. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2146. {
  2147. struct cgroup *c_parent = dentry->d_parent->d_fsdata;
  2148. /* the vfs holds inode->i_mutex already */
  2149. return cgroup_create(c_parent, dentry, mode | S_IFDIR);
  2150. }
  2151. static int cgroup_has_css_refs(struct cgroup *cgrp)
  2152. {
  2153. /* Check the reference count on each subsystem. Since we
  2154. * already established that there are no tasks in the
  2155. * cgroup, if the css refcount is also 1, then there should
  2156. * be no outstanding references, so the subsystem is safe to
  2157. * destroy. We scan across all subsystems rather than using
  2158. * the per-hierarchy linked list of mounted subsystems since
  2159. * we can be called via check_for_release() with no
  2160. * synchronization other than RCU, and the subsystem linked
  2161. * list isn't RCU-safe */
  2162. int i;
  2163. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2164. struct cgroup_subsys *ss = subsys[i];
  2165. struct cgroup_subsys_state *css;
  2166. /* Skip subsystems not in this hierarchy */
  2167. if (ss->root != cgrp->root)
  2168. continue;
  2169. css = cgrp->subsys[ss->subsys_id];
  2170. /* When called from check_for_release() it's possible
  2171. * that by this point the cgroup has been removed
  2172. * and the css deleted. But a false-positive doesn't
  2173. * matter, since it can only happen if the cgroup
  2174. * has been deleted and hence no longer needs the
  2175. * release agent to be called anyway. */
  2176. if (css && (atomic_read(&css->refcnt) > 1))
  2177. return 1;
  2178. }
  2179. return 0;
  2180. }
  2181. /*
  2182. * Atomically mark all (or else none) of the cgroup's CSS objects as
  2183. * CSS_REMOVED. Return true on success, or false if the cgroup has
  2184. * busy subsystems. Call with cgroup_mutex held
  2185. */
  2186. static int cgroup_clear_css_refs(struct cgroup *cgrp)
  2187. {
  2188. struct cgroup_subsys *ss;
  2189. unsigned long flags;
  2190. bool failed = false;
  2191. local_irq_save(flags);
  2192. for_each_subsys(cgrp->root, ss) {
  2193. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  2194. int refcnt;
  2195. do {
  2196. /* We can only remove a CSS with a refcnt==1 */
  2197. refcnt = atomic_read(&css->refcnt);
  2198. if (refcnt > 1) {
  2199. failed = true;
  2200. goto done;
  2201. }
  2202. BUG_ON(!refcnt);
  2203. /*
  2204. * Drop the refcnt to 0 while we check other
  2205. * subsystems. This will cause any racing
  2206. * css_tryget() to spin until we set the
  2207. * CSS_REMOVED bits or abort
  2208. */
  2209. } while (atomic_cmpxchg(&css->refcnt, refcnt, 0) != refcnt);
  2210. }
  2211. done:
  2212. for_each_subsys(cgrp->root, ss) {
  2213. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  2214. if (failed) {
  2215. /*
  2216. * Restore old refcnt if we previously managed
  2217. * to clear it from 1 to 0
  2218. */
  2219. if (!atomic_read(&css->refcnt))
  2220. atomic_set(&css->refcnt, 1);
  2221. } else {
  2222. /* Commit the fact that the CSS is removed */
  2223. set_bit(CSS_REMOVED, &css->flags);
  2224. }
  2225. }
  2226. local_irq_restore(flags);
  2227. return !failed;
  2228. }
  2229. static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
  2230. {
  2231. struct cgroup *cgrp = dentry->d_fsdata;
  2232. struct dentry *d;
  2233. struct cgroup *parent;
  2234. /* the vfs holds both inode->i_mutex already */
  2235. mutex_lock(&cgroup_mutex);
  2236. if (atomic_read(&cgrp->count) != 0) {
  2237. mutex_unlock(&cgroup_mutex);
  2238. return -EBUSY;
  2239. }
  2240. if (!list_empty(&cgrp->children)) {
  2241. mutex_unlock(&cgroup_mutex);
  2242. return -EBUSY;
  2243. }
  2244. mutex_unlock(&cgroup_mutex);
  2245. /*
  2246. * Call pre_destroy handlers of subsys. Notify subsystems
  2247. * that rmdir() request comes.
  2248. */
  2249. cgroup_call_pre_destroy(cgrp);
  2250. mutex_lock(&cgroup_mutex);
  2251. parent = cgrp->parent;
  2252. if (atomic_read(&cgrp->count)
  2253. || !list_empty(&cgrp->children)
  2254. || !cgroup_clear_css_refs(cgrp)) {
  2255. mutex_unlock(&cgroup_mutex);
  2256. return -EBUSY;
  2257. }
  2258. spin_lock(&release_list_lock);
  2259. set_bit(CGRP_REMOVED, &cgrp->flags);
  2260. if (!list_empty(&cgrp->release_list))
  2261. list_del(&cgrp->release_list);
  2262. spin_unlock(&release_list_lock);
  2263. cgroup_lock_hierarchy(cgrp->root);
  2264. /* delete this cgroup from parent->children */
  2265. list_del(&cgrp->sibling);
  2266. cgroup_unlock_hierarchy(cgrp->root);
  2267. spin_lock(&cgrp->dentry->d_lock);
  2268. d = dget(cgrp->dentry);
  2269. spin_unlock(&d->d_lock);
  2270. cgroup_d_remove_dir(d);
  2271. dput(d);
  2272. set_bit(CGRP_RELEASABLE, &parent->flags);
  2273. check_for_release(parent);
  2274. mutex_unlock(&cgroup_mutex);
  2275. return 0;
  2276. }
  2277. static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
  2278. {
  2279. struct cgroup_subsys_state *css;
  2280. printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
  2281. /* Create the top cgroup state for this subsystem */
  2282. list_add(&ss->sibling, &rootnode.subsys_list);
  2283. ss->root = &rootnode;
  2284. css = ss->create(ss, dummytop);
  2285. /* We don't handle early failures gracefully */
  2286. BUG_ON(IS_ERR(css));
  2287. init_cgroup_css(css, ss, dummytop);
  2288. /* Update the init_css_set to contain a subsys
  2289. * pointer to this state - since the subsystem is
  2290. * newly registered, all tasks and hence the
  2291. * init_css_set is in the subsystem's top cgroup. */
  2292. init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
  2293. need_forkexit_callback |= ss->fork || ss->exit;
  2294. /* At system boot, before all subsystems have been
  2295. * registered, no tasks have been forked, so we don't
  2296. * need to invoke fork callbacks here. */
  2297. BUG_ON(!list_empty(&init_task.tasks));
  2298. mutex_init(&ss->hierarchy_mutex);
  2299. ss->active = 1;
  2300. }
  2301. /**
  2302. * cgroup_init_early - cgroup initialization at system boot
  2303. *
  2304. * Initialize cgroups at system boot, and initialize any
  2305. * subsystems that request early init.
  2306. */
  2307. int __init cgroup_init_early(void)
  2308. {
  2309. int i;
  2310. atomic_set(&init_css_set.refcount, 1);
  2311. INIT_LIST_HEAD(&init_css_set.cg_links);
  2312. INIT_LIST_HEAD(&init_css_set.tasks);
  2313. INIT_HLIST_NODE(&init_css_set.hlist);
  2314. css_set_count = 1;
  2315. init_cgroup_root(&rootnode);
  2316. root_count = 1;
  2317. init_task.cgroups = &init_css_set;
  2318. init_css_set_link.cg = &init_css_set;
  2319. list_add(&init_css_set_link.cgrp_link_list,
  2320. &rootnode.top_cgroup.css_sets);
  2321. list_add(&init_css_set_link.cg_link_list,
  2322. &init_css_set.cg_links);
  2323. for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
  2324. INIT_HLIST_HEAD(&css_set_table[i]);
  2325. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2326. struct cgroup_subsys *ss = subsys[i];
  2327. BUG_ON(!ss->name);
  2328. BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
  2329. BUG_ON(!ss->create);
  2330. BUG_ON(!ss->destroy);
  2331. if (ss->subsys_id != i) {
  2332. printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
  2333. ss->name, ss->subsys_id);
  2334. BUG();
  2335. }
  2336. if (ss->early_init)
  2337. cgroup_init_subsys(ss);
  2338. }
  2339. return 0;
  2340. }
  2341. /**
  2342. * cgroup_init - cgroup initialization
  2343. *
  2344. * Register cgroup filesystem and /proc file, and initialize
  2345. * any subsystems that didn't request early init.
  2346. */
  2347. int __init cgroup_init(void)
  2348. {
  2349. int err;
  2350. int i;
  2351. struct hlist_head *hhead;
  2352. err = bdi_init(&cgroup_backing_dev_info);
  2353. if (err)
  2354. return err;
  2355. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2356. struct cgroup_subsys *ss = subsys[i];
  2357. if (!ss->early_init)
  2358. cgroup_init_subsys(ss);
  2359. }
  2360. /* Add init_css_set to the hash table */
  2361. hhead = css_set_hash(init_css_set.subsys);
  2362. hlist_add_head(&init_css_set.hlist, hhead);
  2363. err = register_filesystem(&cgroup_fs_type);
  2364. if (err < 0)
  2365. goto out;
  2366. proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
  2367. out:
  2368. if (err)
  2369. bdi_destroy(&cgroup_backing_dev_info);
  2370. return err;
  2371. }
  2372. /*
  2373. * proc_cgroup_show()
  2374. * - Print task's cgroup paths into seq_file, one line for each hierarchy
  2375. * - Used for /proc/<pid>/cgroup.
  2376. * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
  2377. * doesn't really matter if tsk->cgroup changes after we read it,
  2378. * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
  2379. * anyway. No need to check that tsk->cgroup != NULL, thanks to
  2380. * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
  2381. * cgroup to top_cgroup.
  2382. */
  2383. /* TODO: Use a proper seq_file iterator */
  2384. static int proc_cgroup_show(struct seq_file *m, void *v)
  2385. {
  2386. struct pid *pid;
  2387. struct task_struct *tsk;
  2388. char *buf;
  2389. int retval;
  2390. struct cgroupfs_root *root;
  2391. retval = -ENOMEM;
  2392. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2393. if (!buf)
  2394. goto out;
  2395. retval = -ESRCH;
  2396. pid = m->private;
  2397. tsk = get_pid_task(pid, PIDTYPE_PID);
  2398. if (!tsk)
  2399. goto out_free;
  2400. retval = 0;
  2401. mutex_lock(&cgroup_mutex);
  2402. for_each_active_root(root) {
  2403. struct cgroup_subsys *ss;
  2404. struct cgroup *cgrp;
  2405. int subsys_id;
  2406. int count = 0;
  2407. seq_printf(m, "%lu:", root->subsys_bits);
  2408. for_each_subsys(root, ss)
  2409. seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
  2410. seq_putc(m, ':');
  2411. get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
  2412. cgrp = task_cgroup(tsk, subsys_id);
  2413. retval = cgroup_path(cgrp, buf, PAGE_SIZE);
  2414. if (retval < 0)
  2415. goto out_unlock;
  2416. seq_puts(m, buf);
  2417. seq_putc(m, '\n');
  2418. }
  2419. out_unlock:
  2420. mutex_unlock(&cgroup_mutex);
  2421. put_task_struct(tsk);
  2422. out_free:
  2423. kfree(buf);
  2424. out:
  2425. return retval;
  2426. }
  2427. static int cgroup_open(struct inode *inode, struct file *file)
  2428. {
  2429. struct pid *pid = PROC_I(inode)->pid;
  2430. return single_open(file, proc_cgroup_show, pid);
  2431. }
  2432. struct file_operations proc_cgroup_operations = {
  2433. .open = cgroup_open,
  2434. .read = seq_read,
  2435. .llseek = seq_lseek,
  2436. .release = single_release,
  2437. };
  2438. /* Display information about each subsystem and each hierarchy */
  2439. static int proc_cgroupstats_show(struct seq_file *m, void *v)
  2440. {
  2441. int i;
  2442. seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
  2443. mutex_lock(&cgroup_mutex);
  2444. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2445. struct cgroup_subsys *ss = subsys[i];
  2446. seq_printf(m, "%s\t%lu\t%d\t%d\n",
  2447. ss->name, ss->root->subsys_bits,
  2448. ss->root->number_of_cgroups, !ss->disabled);
  2449. }
  2450. mutex_unlock(&cgroup_mutex);
  2451. return 0;
  2452. }
  2453. static int cgroupstats_open(struct inode *inode, struct file *file)
  2454. {
  2455. return single_open(file, proc_cgroupstats_show, NULL);
  2456. }
  2457. static struct file_operations proc_cgroupstats_operations = {
  2458. .open = cgroupstats_open,
  2459. .read = seq_read,
  2460. .llseek = seq_lseek,
  2461. .release = single_release,
  2462. };
  2463. /**
  2464. * cgroup_fork - attach newly forked task to its parents cgroup.
  2465. * @child: pointer to task_struct of forking parent process.
  2466. *
  2467. * Description: A task inherits its parent's cgroup at fork().
  2468. *
  2469. * A pointer to the shared css_set was automatically copied in
  2470. * fork.c by dup_task_struct(). However, we ignore that copy, since
  2471. * it was not made under the protection of RCU or cgroup_mutex, so
  2472. * might no longer be a valid cgroup pointer. cgroup_attach_task() might
  2473. * have already changed current->cgroups, allowing the previously
  2474. * referenced cgroup group to be removed and freed.
  2475. *
  2476. * At the point that cgroup_fork() is called, 'current' is the parent
  2477. * task, and the passed argument 'child' points to the child task.
  2478. */
  2479. void cgroup_fork(struct task_struct *child)
  2480. {
  2481. task_lock(current);
  2482. child->cgroups = current->cgroups;
  2483. get_css_set(child->cgroups);
  2484. task_unlock(current);
  2485. INIT_LIST_HEAD(&child->cg_list);
  2486. }
  2487. /**
  2488. * cgroup_fork_callbacks - run fork callbacks
  2489. * @child: the new task
  2490. *
  2491. * Called on a new task very soon before adding it to the
  2492. * tasklist. No need to take any locks since no-one can
  2493. * be operating on this task.
  2494. */
  2495. void cgroup_fork_callbacks(struct task_struct *child)
  2496. {
  2497. if (need_forkexit_callback) {
  2498. int i;
  2499. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2500. struct cgroup_subsys *ss = subsys[i];
  2501. if (ss->fork)
  2502. ss->fork(ss, child);
  2503. }
  2504. }
  2505. }
  2506. /**
  2507. * cgroup_post_fork - called on a new task after adding it to the task list
  2508. * @child: the task in question
  2509. *
  2510. * Adds the task to the list running through its css_set if necessary.
  2511. * Has to be after the task is visible on the task list in case we race
  2512. * with the first call to cgroup_iter_start() - to guarantee that the
  2513. * new task ends up on its list.
  2514. */
  2515. void cgroup_post_fork(struct task_struct *child)
  2516. {
  2517. if (use_task_css_set_links) {
  2518. write_lock(&css_set_lock);
  2519. task_lock(child);
  2520. if (list_empty(&child->cg_list))
  2521. list_add(&child->cg_list, &child->cgroups->tasks);
  2522. task_unlock(child);
  2523. write_unlock(&css_set_lock);
  2524. }
  2525. }
  2526. /**
  2527. * cgroup_exit - detach cgroup from exiting task
  2528. * @tsk: pointer to task_struct of exiting process
  2529. * @run_callback: run exit callbacks?
  2530. *
  2531. * Description: Detach cgroup from @tsk and release it.
  2532. *
  2533. * Note that cgroups marked notify_on_release force every task in
  2534. * them to take the global cgroup_mutex mutex when exiting.
  2535. * This could impact scaling on very large systems. Be reluctant to
  2536. * use notify_on_release cgroups where very high task exit scaling
  2537. * is required on large systems.
  2538. *
  2539. * the_top_cgroup_hack:
  2540. *
  2541. * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
  2542. *
  2543. * We call cgroup_exit() while the task is still competent to
  2544. * handle notify_on_release(), then leave the task attached to the
  2545. * root cgroup in each hierarchy for the remainder of its exit.
  2546. *
  2547. * To do this properly, we would increment the reference count on
  2548. * top_cgroup, and near the very end of the kernel/exit.c do_exit()
  2549. * code we would add a second cgroup function call, to drop that
  2550. * reference. This would just create an unnecessary hot spot on
  2551. * the top_cgroup reference count, to no avail.
  2552. *
  2553. * Normally, holding a reference to a cgroup without bumping its
  2554. * count is unsafe. The cgroup could go away, or someone could
  2555. * attach us to a different cgroup, decrementing the count on
  2556. * the first cgroup that we never incremented. But in this case,
  2557. * top_cgroup isn't going away, and either task has PF_EXITING set,
  2558. * which wards off any cgroup_attach_task() attempts, or task is a failed
  2559. * fork, never visible to cgroup_attach_task.
  2560. */
  2561. void cgroup_exit(struct task_struct *tsk, int run_callbacks)
  2562. {
  2563. int i;
  2564. struct css_set *cg;
  2565. if (run_callbacks && need_forkexit_callback) {
  2566. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2567. struct cgroup_subsys *ss = subsys[i];
  2568. if (ss->exit)
  2569. ss->exit(ss, tsk);
  2570. }
  2571. }
  2572. /*
  2573. * Unlink from the css_set task list if necessary.
  2574. * Optimistically check cg_list before taking
  2575. * css_set_lock
  2576. */
  2577. if (!list_empty(&tsk->cg_list)) {
  2578. write_lock(&css_set_lock);
  2579. if (!list_empty(&tsk->cg_list))
  2580. list_del(&tsk->cg_list);
  2581. write_unlock(&css_set_lock);
  2582. }
  2583. /* Reassign the task to the init_css_set. */
  2584. task_lock(tsk);
  2585. cg = tsk->cgroups;
  2586. tsk->cgroups = &init_css_set;
  2587. task_unlock(tsk);
  2588. if (cg)
  2589. put_css_set_taskexit(cg);
  2590. }
  2591. /**
  2592. * cgroup_clone - clone the cgroup the given subsystem is attached to
  2593. * @tsk: the task to be moved
  2594. * @subsys: the given subsystem
  2595. * @nodename: the name for the new cgroup
  2596. *
  2597. * Duplicate the current cgroup in the hierarchy that the given
  2598. * subsystem is attached to, and move this task into the new
  2599. * child.
  2600. */
  2601. int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
  2602. char *nodename)
  2603. {
  2604. struct dentry *dentry;
  2605. int ret = 0;
  2606. struct cgroup *parent, *child;
  2607. struct inode *inode;
  2608. struct css_set *cg;
  2609. struct cgroupfs_root *root;
  2610. struct cgroup_subsys *ss;
  2611. /* We shouldn't be called by an unregistered subsystem */
  2612. BUG_ON(!subsys->active);
  2613. /* First figure out what hierarchy and cgroup we're dealing
  2614. * with, and pin them so we can drop cgroup_mutex */
  2615. mutex_lock(&cgroup_mutex);
  2616. again:
  2617. root = subsys->root;
  2618. if (root == &rootnode) {
  2619. mutex_unlock(&cgroup_mutex);
  2620. return 0;
  2621. }
  2622. task_lock(tsk);
  2623. cg = tsk->cgroups;
  2624. parent = task_cgroup(tsk, subsys->subsys_id);
  2625. /* Pin the hierarchy */
  2626. if (!atomic_inc_not_zero(&parent->root->sb->s_active)) {
  2627. /* We race with the final deactivate_super() */
  2628. mutex_unlock(&cgroup_mutex);
  2629. return 0;
  2630. }
  2631. /* Keep the cgroup alive */
  2632. get_css_set(cg);
  2633. task_unlock(tsk);
  2634. mutex_unlock(&cgroup_mutex);
  2635. /* Now do the VFS work to create a cgroup */
  2636. inode = parent->dentry->d_inode;
  2637. /* Hold the parent directory mutex across this operation to
  2638. * stop anyone else deleting the new cgroup */
  2639. mutex_lock(&inode->i_mutex);
  2640. dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
  2641. if (IS_ERR(dentry)) {
  2642. printk(KERN_INFO
  2643. "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
  2644. PTR_ERR(dentry));
  2645. ret = PTR_ERR(dentry);
  2646. goto out_release;
  2647. }
  2648. /* Create the cgroup directory, which also creates the cgroup */
  2649. ret = vfs_mkdir(inode, dentry, 0755);
  2650. child = __d_cgrp(dentry);
  2651. dput(dentry);
  2652. if (ret) {
  2653. printk(KERN_INFO
  2654. "Failed to create cgroup %s: %d\n", nodename,
  2655. ret);
  2656. goto out_release;
  2657. }
  2658. /* The cgroup now exists. Retake cgroup_mutex and check
  2659. * that we're still in the same state that we thought we
  2660. * were. */
  2661. mutex_lock(&cgroup_mutex);
  2662. if ((root != subsys->root) ||
  2663. (parent != task_cgroup(tsk, subsys->subsys_id))) {
  2664. /* Aargh, we raced ... */
  2665. mutex_unlock(&inode->i_mutex);
  2666. put_css_set(cg);
  2667. deactivate_super(parent->root->sb);
  2668. /* The cgroup is still accessible in the VFS, but
  2669. * we're not going to try to rmdir() it at this
  2670. * point. */
  2671. printk(KERN_INFO
  2672. "Race in cgroup_clone() - leaking cgroup %s\n",
  2673. nodename);
  2674. goto again;
  2675. }
  2676. /* do any required auto-setup */
  2677. for_each_subsys(root, ss) {
  2678. if (ss->post_clone)
  2679. ss->post_clone(ss, child);
  2680. }
  2681. /* All seems fine. Finish by moving the task into the new cgroup */
  2682. ret = cgroup_attach_task(child, tsk);
  2683. mutex_unlock(&cgroup_mutex);
  2684. out_release:
  2685. mutex_unlock(&inode->i_mutex);
  2686. mutex_lock(&cgroup_mutex);
  2687. put_css_set(cg);
  2688. mutex_unlock(&cgroup_mutex);
  2689. deactivate_super(parent->root->sb);
  2690. return ret;
  2691. }
  2692. /**
  2693. * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
  2694. * @cgrp: the cgroup in question
  2695. *
  2696. * See if @cgrp is a descendant of the current task's cgroup in
  2697. * the appropriate hierarchy.
  2698. *
  2699. * If we are sending in dummytop, then presumably we are creating
  2700. * the top cgroup in the subsystem.
  2701. *
  2702. * Called only by the ns (nsproxy) cgroup.
  2703. */
  2704. int cgroup_is_descendant(const struct cgroup *cgrp)
  2705. {
  2706. int ret;
  2707. struct cgroup *target;
  2708. int subsys_id;
  2709. if (cgrp == dummytop)
  2710. return 1;
  2711. get_first_subsys(cgrp, NULL, &subsys_id);
  2712. target = task_cgroup(current, subsys_id);
  2713. while (cgrp != target && cgrp!= cgrp->top_cgroup)
  2714. cgrp = cgrp->parent;
  2715. ret = (cgrp == target);
  2716. return ret;
  2717. }
  2718. static void check_for_release(struct cgroup *cgrp)
  2719. {
  2720. /* All of these checks rely on RCU to keep the cgroup
  2721. * structure alive */
  2722. if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
  2723. && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
  2724. /* Control Group is currently removeable. If it's not
  2725. * already queued for a userspace notification, queue
  2726. * it now */
  2727. int need_schedule_work = 0;
  2728. spin_lock(&release_list_lock);
  2729. if (!cgroup_is_removed(cgrp) &&
  2730. list_empty(&cgrp->release_list)) {
  2731. list_add(&cgrp->release_list, &release_list);
  2732. need_schedule_work = 1;
  2733. }
  2734. spin_unlock(&release_list_lock);
  2735. if (need_schedule_work)
  2736. schedule_work(&release_agent_work);
  2737. }
  2738. }
  2739. void __css_put(struct cgroup_subsys_state *css)
  2740. {
  2741. struct cgroup *cgrp = css->cgroup;
  2742. rcu_read_lock();
  2743. if ((atomic_dec_return(&css->refcnt) == 1) &&
  2744. notify_on_release(cgrp)) {
  2745. set_bit(CGRP_RELEASABLE, &cgrp->flags);
  2746. check_for_release(cgrp);
  2747. }
  2748. rcu_read_unlock();
  2749. }
  2750. /*
  2751. * Notify userspace when a cgroup is released, by running the
  2752. * configured release agent with the name of the cgroup (path
  2753. * relative to the root of cgroup file system) as the argument.
  2754. *
  2755. * Most likely, this user command will try to rmdir this cgroup.
  2756. *
  2757. * This races with the possibility that some other task will be
  2758. * attached to this cgroup before it is removed, or that some other
  2759. * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
  2760. * The presumed 'rmdir' will fail quietly if this cgroup is no longer
  2761. * unused, and this cgroup will be reprieved from its death sentence,
  2762. * to continue to serve a useful existence. Next time it's released,
  2763. * we will get notified again, if it still has 'notify_on_release' set.
  2764. *
  2765. * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
  2766. * means only wait until the task is successfully execve()'d. The
  2767. * separate release agent task is forked by call_usermodehelper(),
  2768. * then control in this thread returns here, without waiting for the
  2769. * release agent task. We don't bother to wait because the caller of
  2770. * this routine has no use for the exit status of the release agent
  2771. * task, so no sense holding our caller up for that.
  2772. */
  2773. static void cgroup_release_agent(struct work_struct *work)
  2774. {
  2775. BUG_ON(work != &release_agent_work);
  2776. mutex_lock(&cgroup_mutex);
  2777. spin_lock(&release_list_lock);
  2778. while (!list_empty(&release_list)) {
  2779. char *argv[3], *envp[3];
  2780. int i;
  2781. char *pathbuf = NULL, *agentbuf = NULL;
  2782. struct cgroup *cgrp = list_entry(release_list.next,
  2783. struct cgroup,
  2784. release_list);
  2785. list_del_init(&cgrp->release_list);
  2786. spin_unlock(&release_list_lock);
  2787. pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2788. if (!pathbuf)
  2789. goto continue_free;
  2790. if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
  2791. goto continue_free;
  2792. agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
  2793. if (!agentbuf)
  2794. goto continue_free;
  2795. i = 0;
  2796. argv[i++] = agentbuf;
  2797. argv[i++] = pathbuf;
  2798. argv[i] = NULL;
  2799. i = 0;
  2800. /* minimal command environment */
  2801. envp[i++] = "HOME=/";
  2802. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  2803. envp[i] = NULL;
  2804. /* Drop the lock while we invoke the usermode helper,
  2805. * since the exec could involve hitting disk and hence
  2806. * be a slow process */
  2807. mutex_unlock(&cgroup_mutex);
  2808. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  2809. mutex_lock(&cgroup_mutex);
  2810. continue_free:
  2811. kfree(pathbuf);
  2812. kfree(agentbuf);
  2813. spin_lock(&release_list_lock);
  2814. }
  2815. spin_unlock(&release_list_lock);
  2816. mutex_unlock(&cgroup_mutex);
  2817. }
  2818. static int __init cgroup_disable(char *str)
  2819. {
  2820. int i;
  2821. char *token;
  2822. while ((token = strsep(&str, ",")) != NULL) {
  2823. if (!*token)
  2824. continue;
  2825. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2826. struct cgroup_subsys *ss = subsys[i];
  2827. if (!strcmp(token, ss->name)) {
  2828. ss->disabled = 1;
  2829. printk(KERN_INFO "Disabling %s control group"
  2830. " subsystem\n", ss->name);
  2831. break;
  2832. }
  2833. }
  2834. }
  2835. return 1;
  2836. }
  2837. __setup("cgroup_disable=", cgroup_disable);