cgroup.c 109 KB

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