cgroup.c 127 KB

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