cgroup.c 108 KB

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