cgroup.c 103 KB

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