cgroup.c 93 KB

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