cgroup.c 129 KB

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