cgroup.c 125 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804
  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, 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,
  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 inline struct cgroup *__d_cgrp(struct dentry *dentry)
  1417. {
  1418. return dentry->d_fsdata;
  1419. }
  1420. static inline struct cftype *__d_cft(struct dentry *dentry)
  1421. {
  1422. return dentry->d_fsdata;
  1423. }
  1424. /**
  1425. * cgroup_path - generate the path of a cgroup
  1426. * @cgrp: the cgroup in question
  1427. * @buf: the buffer to write the path into
  1428. * @buflen: the length of the buffer
  1429. *
  1430. * Called with cgroup_mutex held or else with an RCU-protected cgroup
  1431. * reference. Writes path of cgroup into buf. Returns 0 on success,
  1432. * -errno on error.
  1433. */
  1434. int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
  1435. {
  1436. char *start;
  1437. struct dentry *dentry = rcu_dereference(cgrp->dentry);
  1438. if (!dentry || cgrp == dummytop) {
  1439. /*
  1440. * Inactive subsystems have no dentry for their root
  1441. * cgroup
  1442. */
  1443. strcpy(buf, "/");
  1444. return 0;
  1445. }
  1446. start = buf + buflen;
  1447. *--start = '\0';
  1448. for (;;) {
  1449. int len = dentry->d_name.len;
  1450. if ((start -= len) < buf)
  1451. return -ENAMETOOLONG;
  1452. memcpy(start, cgrp->dentry->d_name.name, len);
  1453. cgrp = cgrp->parent;
  1454. if (!cgrp)
  1455. break;
  1456. dentry = rcu_dereference(cgrp->dentry);
  1457. if (!cgrp->parent)
  1458. continue;
  1459. if (--start < buf)
  1460. return -ENAMETOOLONG;
  1461. *start = '/';
  1462. }
  1463. memmove(buf, start, buf + buflen - start);
  1464. return 0;
  1465. }
  1466. EXPORT_SYMBOL_GPL(cgroup_path);
  1467. /**
  1468. * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
  1469. * @cgrp: the cgroup the task is attaching to
  1470. * @tsk: the task to be attached
  1471. *
  1472. * Call holding cgroup_mutex. May take task_lock of
  1473. * the task 'tsk' during call.
  1474. */
  1475. int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
  1476. {
  1477. int retval = 0;
  1478. struct cgroup_subsys *ss, *failed_ss = NULL;
  1479. struct cgroup *oldcgrp;
  1480. struct css_set *cg;
  1481. struct css_set *newcg;
  1482. struct cgroupfs_root *root = cgrp->root;
  1483. /* Nothing to do if the task is already in that cgroup */
  1484. oldcgrp = task_cgroup_from_root(tsk, root);
  1485. if (cgrp == oldcgrp)
  1486. return 0;
  1487. for_each_subsys(root, ss) {
  1488. if (ss->can_attach) {
  1489. retval = ss->can_attach(ss, cgrp, tsk, false);
  1490. if (retval) {
  1491. /*
  1492. * Remember on which subsystem the can_attach()
  1493. * failed, so that we only call cancel_attach()
  1494. * against the subsystems whose can_attach()
  1495. * succeeded. (See below)
  1496. */
  1497. failed_ss = ss;
  1498. goto out;
  1499. }
  1500. }
  1501. }
  1502. task_lock(tsk);
  1503. cg = tsk->cgroups;
  1504. get_css_set(cg);
  1505. task_unlock(tsk);
  1506. /*
  1507. * Locate or allocate a new css_set for this task,
  1508. * based on its final set of cgroups
  1509. */
  1510. newcg = find_css_set(cg, cgrp);
  1511. put_css_set(cg);
  1512. if (!newcg) {
  1513. retval = -ENOMEM;
  1514. goto out;
  1515. }
  1516. task_lock(tsk);
  1517. if (tsk->flags & PF_EXITING) {
  1518. task_unlock(tsk);
  1519. put_css_set(newcg);
  1520. retval = -ESRCH;
  1521. goto out;
  1522. }
  1523. rcu_assign_pointer(tsk->cgroups, newcg);
  1524. task_unlock(tsk);
  1525. /* Update the css_set linked lists if we're using them */
  1526. write_lock(&css_set_lock);
  1527. if (!list_empty(&tsk->cg_list)) {
  1528. list_del(&tsk->cg_list);
  1529. list_add(&tsk->cg_list, &newcg->tasks);
  1530. }
  1531. write_unlock(&css_set_lock);
  1532. for_each_subsys(root, ss) {
  1533. if (ss->attach)
  1534. ss->attach(ss, cgrp, oldcgrp, tsk, false);
  1535. }
  1536. set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
  1537. synchronize_rcu();
  1538. put_css_set(cg);
  1539. /*
  1540. * wake up rmdir() waiter. the rmdir should fail since the cgroup
  1541. * is no longer empty.
  1542. */
  1543. cgroup_wakeup_rmdir_waiter(cgrp);
  1544. out:
  1545. if (retval) {
  1546. for_each_subsys(root, ss) {
  1547. if (ss == failed_ss)
  1548. /*
  1549. * This subsystem was the one that failed the
  1550. * can_attach() check earlier, so we don't need
  1551. * to call cancel_attach() against it or any
  1552. * remaining subsystems.
  1553. */
  1554. break;
  1555. if (ss->cancel_attach)
  1556. ss->cancel_attach(ss, cgrp, tsk, false);
  1557. }
  1558. }
  1559. return retval;
  1560. }
  1561. /*
  1562. * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
  1563. * held. May take task_lock of task
  1564. */
  1565. static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
  1566. {
  1567. struct task_struct *tsk;
  1568. const struct cred *cred = current_cred(), *tcred;
  1569. int ret;
  1570. if (pid) {
  1571. rcu_read_lock();
  1572. tsk = find_task_by_vpid(pid);
  1573. if (!tsk || tsk->flags & PF_EXITING) {
  1574. rcu_read_unlock();
  1575. return -ESRCH;
  1576. }
  1577. tcred = __task_cred(tsk);
  1578. if (cred->euid &&
  1579. cred->euid != tcred->uid &&
  1580. cred->euid != tcred->suid) {
  1581. rcu_read_unlock();
  1582. return -EACCES;
  1583. }
  1584. get_task_struct(tsk);
  1585. rcu_read_unlock();
  1586. } else {
  1587. tsk = current;
  1588. get_task_struct(tsk);
  1589. }
  1590. ret = cgroup_attach_task(cgrp, tsk);
  1591. put_task_struct(tsk);
  1592. return ret;
  1593. }
  1594. static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
  1595. {
  1596. int ret;
  1597. if (!cgroup_lock_live_group(cgrp))
  1598. return -ENODEV;
  1599. ret = attach_task_by_pid(cgrp, pid);
  1600. cgroup_unlock();
  1601. return ret;
  1602. }
  1603. /**
  1604. * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
  1605. * @cgrp: the cgroup to be checked for liveness
  1606. *
  1607. * On success, returns true; the lock should be later released with
  1608. * cgroup_unlock(). On failure returns false with no lock held.
  1609. */
  1610. bool cgroup_lock_live_group(struct cgroup *cgrp)
  1611. {
  1612. mutex_lock(&cgroup_mutex);
  1613. if (cgroup_is_removed(cgrp)) {
  1614. mutex_unlock(&cgroup_mutex);
  1615. return false;
  1616. }
  1617. return true;
  1618. }
  1619. EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
  1620. static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
  1621. const char *buffer)
  1622. {
  1623. BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
  1624. if (!cgroup_lock_live_group(cgrp))
  1625. return -ENODEV;
  1626. strcpy(cgrp->root->release_agent_path, buffer);
  1627. cgroup_unlock();
  1628. return 0;
  1629. }
  1630. static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
  1631. struct seq_file *seq)
  1632. {
  1633. if (!cgroup_lock_live_group(cgrp))
  1634. return -ENODEV;
  1635. seq_puts(seq, cgrp->root->release_agent_path);
  1636. seq_putc(seq, '\n');
  1637. cgroup_unlock();
  1638. return 0;
  1639. }
  1640. /* A buffer size big enough for numbers or short strings */
  1641. #define CGROUP_LOCAL_BUFFER_SIZE 64
  1642. static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
  1643. struct file *file,
  1644. const char __user *userbuf,
  1645. size_t nbytes, loff_t *unused_ppos)
  1646. {
  1647. char buffer[CGROUP_LOCAL_BUFFER_SIZE];
  1648. int retval = 0;
  1649. char *end;
  1650. if (!nbytes)
  1651. return -EINVAL;
  1652. if (nbytes >= sizeof(buffer))
  1653. return -E2BIG;
  1654. if (copy_from_user(buffer, userbuf, nbytes))
  1655. return -EFAULT;
  1656. buffer[nbytes] = 0; /* nul-terminate */
  1657. if (cft->write_u64) {
  1658. u64 val = simple_strtoull(strstrip(buffer), &end, 0);
  1659. if (*end)
  1660. return -EINVAL;
  1661. retval = cft->write_u64(cgrp, cft, val);
  1662. } else {
  1663. s64 val = simple_strtoll(strstrip(buffer), &end, 0);
  1664. if (*end)
  1665. return -EINVAL;
  1666. retval = cft->write_s64(cgrp, cft, val);
  1667. }
  1668. if (!retval)
  1669. retval = nbytes;
  1670. return retval;
  1671. }
  1672. static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
  1673. struct file *file,
  1674. const char __user *userbuf,
  1675. size_t nbytes, loff_t *unused_ppos)
  1676. {
  1677. char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
  1678. int retval = 0;
  1679. size_t max_bytes = cft->max_write_len;
  1680. char *buffer = local_buffer;
  1681. if (!max_bytes)
  1682. max_bytes = sizeof(local_buffer) - 1;
  1683. if (nbytes >= max_bytes)
  1684. return -E2BIG;
  1685. /* Allocate a dynamic buffer if we need one */
  1686. if (nbytes >= sizeof(local_buffer)) {
  1687. buffer = kmalloc(nbytes + 1, GFP_KERNEL);
  1688. if (buffer == NULL)
  1689. return -ENOMEM;
  1690. }
  1691. if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
  1692. retval = -EFAULT;
  1693. goto out;
  1694. }
  1695. buffer[nbytes] = 0; /* nul-terminate */
  1696. retval = cft->write_string(cgrp, cft, strstrip(buffer));
  1697. if (!retval)
  1698. retval = nbytes;
  1699. out:
  1700. if (buffer != local_buffer)
  1701. kfree(buffer);
  1702. return retval;
  1703. }
  1704. static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
  1705. size_t nbytes, loff_t *ppos)
  1706. {
  1707. struct cftype *cft = __d_cft(file->f_dentry);
  1708. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  1709. if (cgroup_is_removed(cgrp))
  1710. return -ENODEV;
  1711. if (cft->write)
  1712. return cft->write(cgrp, cft, file, buf, nbytes, ppos);
  1713. if (cft->write_u64 || cft->write_s64)
  1714. return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
  1715. if (cft->write_string)
  1716. return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
  1717. if (cft->trigger) {
  1718. int ret = cft->trigger(cgrp, (unsigned int)cft->private);
  1719. return ret ? ret : nbytes;
  1720. }
  1721. return -EINVAL;
  1722. }
  1723. static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
  1724. struct file *file,
  1725. char __user *buf, size_t nbytes,
  1726. loff_t *ppos)
  1727. {
  1728. char tmp[CGROUP_LOCAL_BUFFER_SIZE];
  1729. u64 val = cft->read_u64(cgrp, cft);
  1730. int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
  1731. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  1732. }
  1733. static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
  1734. struct file *file,
  1735. char __user *buf, size_t nbytes,
  1736. loff_t *ppos)
  1737. {
  1738. char tmp[CGROUP_LOCAL_BUFFER_SIZE];
  1739. s64 val = cft->read_s64(cgrp, cft);
  1740. int len = sprintf(tmp, "%lld\n", (long long) val);
  1741. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  1742. }
  1743. static ssize_t cgroup_file_read(struct file *file, char __user *buf,
  1744. size_t nbytes, loff_t *ppos)
  1745. {
  1746. struct cftype *cft = __d_cft(file->f_dentry);
  1747. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  1748. if (cgroup_is_removed(cgrp))
  1749. return -ENODEV;
  1750. if (cft->read)
  1751. return cft->read(cgrp, cft, file, buf, nbytes, ppos);
  1752. if (cft->read_u64)
  1753. return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
  1754. if (cft->read_s64)
  1755. return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
  1756. return -EINVAL;
  1757. }
  1758. /*
  1759. * seqfile ops/methods for returning structured data. Currently just
  1760. * supports string->u64 maps, but can be extended in future.
  1761. */
  1762. struct cgroup_seqfile_state {
  1763. struct cftype *cft;
  1764. struct cgroup *cgroup;
  1765. };
  1766. static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
  1767. {
  1768. struct seq_file *sf = cb->state;
  1769. return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
  1770. }
  1771. static int cgroup_seqfile_show(struct seq_file *m, void *arg)
  1772. {
  1773. struct cgroup_seqfile_state *state = m->private;
  1774. struct cftype *cft = state->cft;
  1775. if (cft->read_map) {
  1776. struct cgroup_map_cb cb = {
  1777. .fill = cgroup_map_add,
  1778. .state = m,
  1779. };
  1780. return cft->read_map(state->cgroup, cft, &cb);
  1781. }
  1782. return cft->read_seq_string(state->cgroup, cft, m);
  1783. }
  1784. static int cgroup_seqfile_release(struct inode *inode, struct file *file)
  1785. {
  1786. struct seq_file *seq = file->private_data;
  1787. kfree(seq->private);
  1788. return single_release(inode, file);
  1789. }
  1790. static const struct file_operations cgroup_seqfile_operations = {
  1791. .read = seq_read,
  1792. .write = cgroup_file_write,
  1793. .llseek = seq_lseek,
  1794. .release = cgroup_seqfile_release,
  1795. };
  1796. static int cgroup_file_open(struct inode *inode, struct file *file)
  1797. {
  1798. int err;
  1799. struct cftype *cft;
  1800. err = generic_file_open(inode, file);
  1801. if (err)
  1802. return err;
  1803. cft = __d_cft(file->f_dentry);
  1804. if (cft->read_map || cft->read_seq_string) {
  1805. struct cgroup_seqfile_state *state =
  1806. kzalloc(sizeof(*state), GFP_USER);
  1807. if (!state)
  1808. return -ENOMEM;
  1809. state->cft = cft;
  1810. state->cgroup = __d_cgrp(file->f_dentry->d_parent);
  1811. file->f_op = &cgroup_seqfile_operations;
  1812. err = single_open(file, cgroup_seqfile_show, state);
  1813. if (err < 0)
  1814. kfree(state);
  1815. } else if (cft->open)
  1816. err = cft->open(inode, file);
  1817. else
  1818. err = 0;
  1819. return err;
  1820. }
  1821. static int cgroup_file_release(struct inode *inode, struct file *file)
  1822. {
  1823. struct cftype *cft = __d_cft(file->f_dentry);
  1824. if (cft->release)
  1825. return cft->release(inode, file);
  1826. return 0;
  1827. }
  1828. /*
  1829. * cgroup_rename - Only allow simple rename of directories in place.
  1830. */
  1831. static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
  1832. struct inode *new_dir, struct dentry *new_dentry)
  1833. {
  1834. if (!S_ISDIR(old_dentry->d_inode->i_mode))
  1835. return -ENOTDIR;
  1836. if (new_dentry->d_inode)
  1837. return -EEXIST;
  1838. if (old_dir != new_dir)
  1839. return -EIO;
  1840. return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  1841. }
  1842. static const struct file_operations cgroup_file_operations = {
  1843. .read = cgroup_file_read,
  1844. .write = cgroup_file_write,
  1845. .llseek = generic_file_llseek,
  1846. .open = cgroup_file_open,
  1847. .release = cgroup_file_release,
  1848. };
  1849. static const struct inode_operations cgroup_dir_inode_operations = {
  1850. .lookup = simple_lookup,
  1851. .mkdir = cgroup_mkdir,
  1852. .rmdir = cgroup_rmdir,
  1853. .rename = cgroup_rename,
  1854. };
  1855. /*
  1856. * Check if a file is a control file
  1857. */
  1858. static inline struct cftype *__file_cft(struct file *file)
  1859. {
  1860. if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
  1861. return ERR_PTR(-EINVAL);
  1862. return __d_cft(file->f_dentry);
  1863. }
  1864. static int cgroup_create_file(struct dentry *dentry, mode_t mode,
  1865. struct super_block *sb)
  1866. {
  1867. static const struct dentry_operations cgroup_dops = {
  1868. .d_iput = cgroup_diput,
  1869. };
  1870. struct inode *inode;
  1871. if (!dentry)
  1872. return -ENOENT;
  1873. if (dentry->d_inode)
  1874. return -EEXIST;
  1875. inode = cgroup_new_inode(mode, sb);
  1876. if (!inode)
  1877. return -ENOMEM;
  1878. if (S_ISDIR(mode)) {
  1879. inode->i_op = &cgroup_dir_inode_operations;
  1880. inode->i_fop = &simple_dir_operations;
  1881. /* start off with i_nlink == 2 (for "." entry) */
  1882. inc_nlink(inode);
  1883. /* start with the directory inode held, so that we can
  1884. * populate it without racing with another mkdir */
  1885. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1886. } else if (S_ISREG(mode)) {
  1887. inode->i_size = 0;
  1888. inode->i_fop = &cgroup_file_operations;
  1889. }
  1890. dentry->d_op = &cgroup_dops;
  1891. d_instantiate(dentry, inode);
  1892. dget(dentry); /* Extra count - pin the dentry in core */
  1893. return 0;
  1894. }
  1895. /*
  1896. * cgroup_create_dir - create a directory for an object.
  1897. * @cgrp: the cgroup we create the directory for. It must have a valid
  1898. * ->parent field. And we are going to fill its ->dentry field.
  1899. * @dentry: dentry of the new cgroup
  1900. * @mode: mode to set on new directory.
  1901. */
  1902. static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
  1903. mode_t mode)
  1904. {
  1905. struct dentry *parent;
  1906. int error = 0;
  1907. parent = cgrp->parent->dentry;
  1908. error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
  1909. if (!error) {
  1910. dentry->d_fsdata = cgrp;
  1911. inc_nlink(parent->d_inode);
  1912. rcu_assign_pointer(cgrp->dentry, dentry);
  1913. dget(dentry);
  1914. }
  1915. dput(dentry);
  1916. return error;
  1917. }
  1918. /**
  1919. * cgroup_file_mode - deduce file mode of a control file
  1920. * @cft: the control file in question
  1921. *
  1922. * returns cft->mode if ->mode is not 0
  1923. * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
  1924. * returns S_IRUGO if it has only a read handler
  1925. * returns S_IWUSR if it has only a write hander
  1926. */
  1927. static mode_t cgroup_file_mode(const struct cftype *cft)
  1928. {
  1929. mode_t mode = 0;
  1930. if (cft->mode)
  1931. return cft->mode;
  1932. if (cft->read || cft->read_u64 || cft->read_s64 ||
  1933. cft->read_map || cft->read_seq_string)
  1934. mode |= S_IRUGO;
  1935. if (cft->write || cft->write_u64 || cft->write_s64 ||
  1936. cft->write_string || cft->trigger)
  1937. mode |= S_IWUSR;
  1938. return mode;
  1939. }
  1940. int cgroup_add_file(struct cgroup *cgrp,
  1941. struct cgroup_subsys *subsys,
  1942. const struct cftype *cft)
  1943. {
  1944. struct dentry *dir = cgrp->dentry;
  1945. struct dentry *dentry;
  1946. int error;
  1947. mode_t mode;
  1948. char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
  1949. if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
  1950. strcpy(name, subsys->name);
  1951. strcat(name, ".");
  1952. }
  1953. strcat(name, cft->name);
  1954. BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
  1955. dentry = lookup_one_len(name, dir, strlen(name));
  1956. if (!IS_ERR(dentry)) {
  1957. mode = cgroup_file_mode(cft);
  1958. error = cgroup_create_file(dentry, mode | S_IFREG,
  1959. cgrp->root->sb);
  1960. if (!error)
  1961. dentry->d_fsdata = (void *)cft;
  1962. dput(dentry);
  1963. } else
  1964. error = PTR_ERR(dentry);
  1965. return error;
  1966. }
  1967. EXPORT_SYMBOL_GPL(cgroup_add_file);
  1968. int cgroup_add_files(struct cgroup *cgrp,
  1969. struct cgroup_subsys *subsys,
  1970. const struct cftype cft[],
  1971. int count)
  1972. {
  1973. int i, err;
  1974. for (i = 0; i < count; i++) {
  1975. err = cgroup_add_file(cgrp, subsys, &cft[i]);
  1976. if (err)
  1977. return err;
  1978. }
  1979. return 0;
  1980. }
  1981. EXPORT_SYMBOL_GPL(cgroup_add_files);
  1982. /**
  1983. * cgroup_task_count - count the number of tasks in a cgroup.
  1984. * @cgrp: the cgroup in question
  1985. *
  1986. * Return the number of tasks in the cgroup.
  1987. */
  1988. int cgroup_task_count(const struct cgroup *cgrp)
  1989. {
  1990. int count = 0;
  1991. struct cg_cgroup_link *link;
  1992. read_lock(&css_set_lock);
  1993. list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
  1994. count += atomic_read(&link->cg->refcount);
  1995. }
  1996. read_unlock(&css_set_lock);
  1997. return count;
  1998. }
  1999. /*
  2000. * Advance a list_head iterator. The iterator should be positioned at
  2001. * the start of a css_set
  2002. */
  2003. static void cgroup_advance_iter(struct cgroup *cgrp,
  2004. struct cgroup_iter *it)
  2005. {
  2006. struct list_head *l = it->cg_link;
  2007. struct cg_cgroup_link *link;
  2008. struct css_set *cg;
  2009. /* Advance to the next non-empty css_set */
  2010. do {
  2011. l = l->next;
  2012. if (l == &cgrp->css_sets) {
  2013. it->cg_link = NULL;
  2014. return;
  2015. }
  2016. link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
  2017. cg = link->cg;
  2018. } while (list_empty(&cg->tasks));
  2019. it->cg_link = l;
  2020. it->task = cg->tasks.next;
  2021. }
  2022. /*
  2023. * To reduce the fork() overhead for systems that are not actually
  2024. * using their cgroups capability, we don't maintain the lists running
  2025. * through each css_set to its tasks until we see the list actually
  2026. * used - in other words after the first call to cgroup_iter_start().
  2027. *
  2028. * The tasklist_lock is not held here, as do_each_thread() and
  2029. * while_each_thread() are protected by RCU.
  2030. */
  2031. static void cgroup_enable_task_cg_lists(void)
  2032. {
  2033. struct task_struct *p, *g;
  2034. write_lock(&css_set_lock);
  2035. use_task_css_set_links = 1;
  2036. do_each_thread(g, p) {
  2037. task_lock(p);
  2038. /*
  2039. * We should check if the process is exiting, otherwise
  2040. * it will race with cgroup_exit() in that the list
  2041. * entry won't be deleted though the process has exited.
  2042. */
  2043. if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
  2044. list_add(&p->cg_list, &p->cgroups->tasks);
  2045. task_unlock(p);
  2046. } while_each_thread(g, p);
  2047. write_unlock(&css_set_lock);
  2048. }
  2049. void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
  2050. {
  2051. /*
  2052. * The first time anyone tries to iterate across a cgroup,
  2053. * we need to enable the list linking each css_set to its
  2054. * tasks, and fix up all existing tasks.
  2055. */
  2056. if (!use_task_css_set_links)
  2057. cgroup_enable_task_cg_lists();
  2058. read_lock(&css_set_lock);
  2059. it->cg_link = &cgrp->css_sets;
  2060. cgroup_advance_iter(cgrp, it);
  2061. }
  2062. struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
  2063. struct cgroup_iter *it)
  2064. {
  2065. struct task_struct *res;
  2066. struct list_head *l = it->task;
  2067. struct cg_cgroup_link *link;
  2068. /* If the iterator cg is NULL, we have no tasks */
  2069. if (!it->cg_link)
  2070. return NULL;
  2071. res = list_entry(l, struct task_struct, cg_list);
  2072. /* Advance iterator to find next entry */
  2073. l = l->next;
  2074. link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
  2075. if (l == &link->cg->tasks) {
  2076. /* We reached the end of this task list - move on to
  2077. * the next cg_cgroup_link */
  2078. cgroup_advance_iter(cgrp, it);
  2079. } else {
  2080. it->task = l;
  2081. }
  2082. return res;
  2083. }
  2084. void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
  2085. {
  2086. read_unlock(&css_set_lock);
  2087. }
  2088. static inline int started_after_time(struct task_struct *t1,
  2089. struct timespec *time,
  2090. struct task_struct *t2)
  2091. {
  2092. int start_diff = timespec_compare(&t1->start_time, time);
  2093. if (start_diff > 0) {
  2094. return 1;
  2095. } else if (start_diff < 0) {
  2096. return 0;
  2097. } else {
  2098. /*
  2099. * Arbitrarily, if two processes started at the same
  2100. * time, we'll say that the lower pointer value
  2101. * started first. Note that t2 may have exited by now
  2102. * so this may not be a valid pointer any longer, but
  2103. * that's fine - it still serves to distinguish
  2104. * between two tasks started (effectively) simultaneously.
  2105. */
  2106. return t1 > t2;
  2107. }
  2108. }
  2109. /*
  2110. * This function is a callback from heap_insert() and is used to order
  2111. * the heap.
  2112. * In this case we order the heap in descending task start time.
  2113. */
  2114. static inline int started_after(void *p1, void *p2)
  2115. {
  2116. struct task_struct *t1 = p1;
  2117. struct task_struct *t2 = p2;
  2118. return started_after_time(t1, &t2->start_time, t2);
  2119. }
  2120. /**
  2121. * cgroup_scan_tasks - iterate though all the tasks in a cgroup
  2122. * @scan: struct cgroup_scanner containing arguments for the scan
  2123. *
  2124. * Arguments include pointers to callback functions test_task() and
  2125. * process_task().
  2126. * Iterate through all the tasks in a cgroup, calling test_task() for each,
  2127. * and if it returns true, call process_task() for it also.
  2128. * The test_task pointer may be NULL, meaning always true (select all tasks).
  2129. * Effectively duplicates cgroup_iter_{start,next,end}()
  2130. * but does not lock css_set_lock for the call to process_task().
  2131. * The struct cgroup_scanner may be embedded in any structure of the caller's
  2132. * creation.
  2133. * It is guaranteed that process_task() will act on every task that
  2134. * is a member of the cgroup for the duration of this call. This
  2135. * function may or may not call process_task() for tasks that exit
  2136. * or move to a different cgroup during the call, or are forked or
  2137. * move into the cgroup during the call.
  2138. *
  2139. * Note that test_task() may be called with locks held, and may in some
  2140. * situations be called multiple times for the same task, so it should
  2141. * be cheap.
  2142. * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
  2143. * pre-allocated and will be used for heap operations (and its "gt" member will
  2144. * be overwritten), else a temporary heap will be used (allocation of which
  2145. * may cause this function to fail).
  2146. */
  2147. int cgroup_scan_tasks(struct cgroup_scanner *scan)
  2148. {
  2149. int retval, i;
  2150. struct cgroup_iter it;
  2151. struct task_struct *p, *dropped;
  2152. /* Never dereference latest_task, since it's not refcounted */
  2153. struct task_struct *latest_task = NULL;
  2154. struct ptr_heap tmp_heap;
  2155. struct ptr_heap *heap;
  2156. struct timespec latest_time = { 0, 0 };
  2157. if (scan->heap) {
  2158. /* The caller supplied our heap and pre-allocated its memory */
  2159. heap = scan->heap;
  2160. heap->gt = &started_after;
  2161. } else {
  2162. /* We need to allocate our own heap memory */
  2163. heap = &tmp_heap;
  2164. retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
  2165. if (retval)
  2166. /* cannot allocate the heap */
  2167. return retval;
  2168. }
  2169. again:
  2170. /*
  2171. * Scan tasks in the cgroup, using the scanner's "test_task" callback
  2172. * to determine which are of interest, and using the scanner's
  2173. * "process_task" callback to process any of them that need an update.
  2174. * Since we don't want to hold any locks during the task updates,
  2175. * gather tasks to be processed in a heap structure.
  2176. * The heap is sorted by descending task start time.
  2177. * If the statically-sized heap fills up, we overflow tasks that
  2178. * started later, and in future iterations only consider tasks that
  2179. * started after the latest task in the previous pass. This
  2180. * guarantees forward progress and that we don't miss any tasks.
  2181. */
  2182. heap->size = 0;
  2183. cgroup_iter_start(scan->cg, &it);
  2184. while ((p = cgroup_iter_next(scan->cg, &it))) {
  2185. /*
  2186. * Only affect tasks that qualify per the caller's callback,
  2187. * if he provided one
  2188. */
  2189. if (scan->test_task && !scan->test_task(p, scan))
  2190. continue;
  2191. /*
  2192. * Only process tasks that started after the last task
  2193. * we processed
  2194. */
  2195. if (!started_after_time(p, &latest_time, latest_task))
  2196. continue;
  2197. dropped = heap_insert(heap, p);
  2198. if (dropped == NULL) {
  2199. /*
  2200. * The new task was inserted; the heap wasn't
  2201. * previously full
  2202. */
  2203. get_task_struct(p);
  2204. } else if (dropped != p) {
  2205. /*
  2206. * The new task was inserted, and pushed out a
  2207. * different task
  2208. */
  2209. get_task_struct(p);
  2210. put_task_struct(dropped);
  2211. }
  2212. /*
  2213. * Else the new task was newer than anything already in
  2214. * the heap and wasn't inserted
  2215. */
  2216. }
  2217. cgroup_iter_end(scan->cg, &it);
  2218. if (heap->size) {
  2219. for (i = 0; i < heap->size; i++) {
  2220. struct task_struct *q = heap->ptrs[i];
  2221. if (i == 0) {
  2222. latest_time = q->start_time;
  2223. latest_task = q;
  2224. }
  2225. /* Process the task per the caller's callback */
  2226. scan->process_task(q, scan);
  2227. put_task_struct(q);
  2228. }
  2229. /*
  2230. * If we had to process any tasks at all, scan again
  2231. * in case some of them were in the middle of forking
  2232. * children that didn't get processed.
  2233. * Not the most efficient way to do it, but it avoids
  2234. * having to take callback_mutex in the fork path
  2235. */
  2236. goto again;
  2237. }
  2238. if (heap == &tmp_heap)
  2239. heap_free(&tmp_heap);
  2240. return 0;
  2241. }
  2242. /*
  2243. * Stuff for reading the 'tasks'/'procs' files.
  2244. *
  2245. * Reading this file can return large amounts of data if a cgroup has
  2246. * *lots* of attached tasks. So it may need several calls to read(),
  2247. * but we cannot guarantee that the information we produce is correct
  2248. * unless we produce it entirely atomically.
  2249. *
  2250. */
  2251. /*
  2252. * The following two functions "fix" the issue where there are more pids
  2253. * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
  2254. * TODO: replace with a kernel-wide solution to this problem
  2255. */
  2256. #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
  2257. static void *pidlist_allocate(int count)
  2258. {
  2259. if (PIDLIST_TOO_LARGE(count))
  2260. return vmalloc(count * sizeof(pid_t));
  2261. else
  2262. return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
  2263. }
  2264. static void pidlist_free(void *p)
  2265. {
  2266. if (is_vmalloc_addr(p))
  2267. vfree(p);
  2268. else
  2269. kfree(p);
  2270. }
  2271. static void *pidlist_resize(void *p, int newcount)
  2272. {
  2273. void *newlist;
  2274. /* note: if new alloc fails, old p will still be valid either way */
  2275. if (is_vmalloc_addr(p)) {
  2276. newlist = vmalloc(newcount * sizeof(pid_t));
  2277. if (!newlist)
  2278. return NULL;
  2279. memcpy(newlist, p, newcount * sizeof(pid_t));
  2280. vfree(p);
  2281. } else {
  2282. newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
  2283. }
  2284. return newlist;
  2285. }
  2286. /*
  2287. * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
  2288. * If the new stripped list is sufficiently smaller and there's enough memory
  2289. * to allocate a new buffer, will let go of the unneeded memory. Returns the
  2290. * number of unique elements.
  2291. */
  2292. /* is the size difference enough that we should re-allocate the array? */
  2293. #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
  2294. static int pidlist_uniq(pid_t **p, int length)
  2295. {
  2296. int src, dest = 1;
  2297. pid_t *list = *p;
  2298. pid_t *newlist;
  2299. /*
  2300. * we presume the 0th element is unique, so i starts at 1. trivial
  2301. * edge cases first; no work needs to be done for either
  2302. */
  2303. if (length == 0 || length == 1)
  2304. return length;
  2305. /* src and dest walk down the list; dest counts unique elements */
  2306. for (src = 1; src < length; src++) {
  2307. /* find next unique element */
  2308. while (list[src] == list[src-1]) {
  2309. src++;
  2310. if (src == length)
  2311. goto after;
  2312. }
  2313. /* dest always points to where the next unique element goes */
  2314. list[dest] = list[src];
  2315. dest++;
  2316. }
  2317. after:
  2318. /*
  2319. * if the length difference is large enough, we want to allocate a
  2320. * smaller buffer to save memory. if this fails due to out of memory,
  2321. * we'll just stay with what we've got.
  2322. */
  2323. if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
  2324. newlist = pidlist_resize(list, dest);
  2325. if (newlist)
  2326. *p = newlist;
  2327. }
  2328. return dest;
  2329. }
  2330. static int cmppid(const void *a, const void *b)
  2331. {
  2332. return *(pid_t *)a - *(pid_t *)b;
  2333. }
  2334. /*
  2335. * find the appropriate pidlist for our purpose (given procs vs tasks)
  2336. * returns with the lock on that pidlist already held, and takes care
  2337. * of the use count, or returns NULL with no locks held if we're out of
  2338. * memory.
  2339. */
  2340. static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
  2341. enum cgroup_filetype type)
  2342. {
  2343. struct cgroup_pidlist *l;
  2344. /* don't need task_nsproxy() if we're looking at ourself */
  2345. struct pid_namespace *ns = current->nsproxy->pid_ns;
  2346. /*
  2347. * We can't drop the pidlist_mutex before taking the l->mutex in case
  2348. * the last ref-holder is trying to remove l from the list at the same
  2349. * time. Holding the pidlist_mutex precludes somebody taking whichever
  2350. * list we find out from under us - compare release_pid_array().
  2351. */
  2352. mutex_lock(&cgrp->pidlist_mutex);
  2353. list_for_each_entry(l, &cgrp->pidlists, links) {
  2354. if (l->key.type == type && l->key.ns == ns) {
  2355. /* make sure l doesn't vanish out from under us */
  2356. down_write(&l->mutex);
  2357. mutex_unlock(&cgrp->pidlist_mutex);
  2358. return l;
  2359. }
  2360. }
  2361. /* entry not found; create a new one */
  2362. l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
  2363. if (!l) {
  2364. mutex_unlock(&cgrp->pidlist_mutex);
  2365. return l;
  2366. }
  2367. init_rwsem(&l->mutex);
  2368. down_write(&l->mutex);
  2369. l->key.type = type;
  2370. l->key.ns = get_pid_ns(ns);
  2371. l->use_count = 0; /* don't increment here */
  2372. l->list = NULL;
  2373. l->owner = cgrp;
  2374. list_add(&l->links, &cgrp->pidlists);
  2375. mutex_unlock(&cgrp->pidlist_mutex);
  2376. return l;
  2377. }
  2378. /*
  2379. * Load a cgroup's pidarray with either procs' tgids or tasks' pids
  2380. */
  2381. static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
  2382. struct cgroup_pidlist **lp)
  2383. {
  2384. pid_t *array;
  2385. int length;
  2386. int pid, n = 0; /* used for populating the array */
  2387. struct cgroup_iter it;
  2388. struct task_struct *tsk;
  2389. struct cgroup_pidlist *l;
  2390. /*
  2391. * If cgroup gets more users after we read count, we won't have
  2392. * enough space - tough. This race is indistinguishable to the
  2393. * caller from the case that the additional cgroup users didn't
  2394. * show up until sometime later on.
  2395. */
  2396. length = cgroup_task_count(cgrp);
  2397. array = pidlist_allocate(length);
  2398. if (!array)
  2399. return -ENOMEM;
  2400. /* now, populate the array */
  2401. cgroup_iter_start(cgrp, &it);
  2402. while ((tsk = cgroup_iter_next(cgrp, &it))) {
  2403. if (unlikely(n == length))
  2404. break;
  2405. /* get tgid or pid for procs or tasks file respectively */
  2406. if (type == CGROUP_FILE_PROCS)
  2407. pid = task_tgid_vnr(tsk);
  2408. else
  2409. pid = task_pid_vnr(tsk);
  2410. if (pid > 0) /* make sure to only use valid results */
  2411. array[n++] = pid;
  2412. }
  2413. cgroup_iter_end(cgrp, &it);
  2414. length = n;
  2415. /* now sort & (if procs) strip out duplicates */
  2416. sort(array, length, sizeof(pid_t), cmppid, NULL);
  2417. if (type == CGROUP_FILE_PROCS)
  2418. length = pidlist_uniq(&array, length);
  2419. l = cgroup_pidlist_find(cgrp, type);
  2420. if (!l) {
  2421. pidlist_free(array);
  2422. return -ENOMEM;
  2423. }
  2424. /* store array, freeing old if necessary - lock already held */
  2425. pidlist_free(l->list);
  2426. l->list = array;
  2427. l->length = length;
  2428. l->use_count++;
  2429. up_write(&l->mutex);
  2430. *lp = l;
  2431. return 0;
  2432. }
  2433. /**
  2434. * cgroupstats_build - build and fill cgroupstats
  2435. * @stats: cgroupstats to fill information into
  2436. * @dentry: A dentry entry belonging to the cgroup for which stats have
  2437. * been requested.
  2438. *
  2439. * Build and fill cgroupstats so that taskstats can export it to user
  2440. * space.
  2441. */
  2442. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
  2443. {
  2444. int ret = -EINVAL;
  2445. struct cgroup *cgrp;
  2446. struct cgroup_iter it;
  2447. struct task_struct *tsk;
  2448. /*
  2449. * Validate dentry by checking the superblock operations,
  2450. * and make sure it's a directory.
  2451. */
  2452. if (dentry->d_sb->s_op != &cgroup_ops ||
  2453. !S_ISDIR(dentry->d_inode->i_mode))
  2454. goto err;
  2455. ret = 0;
  2456. cgrp = dentry->d_fsdata;
  2457. cgroup_iter_start(cgrp, &it);
  2458. while ((tsk = cgroup_iter_next(cgrp, &it))) {
  2459. switch (tsk->state) {
  2460. case TASK_RUNNING:
  2461. stats->nr_running++;
  2462. break;
  2463. case TASK_INTERRUPTIBLE:
  2464. stats->nr_sleeping++;
  2465. break;
  2466. case TASK_UNINTERRUPTIBLE:
  2467. stats->nr_uninterruptible++;
  2468. break;
  2469. case TASK_STOPPED:
  2470. stats->nr_stopped++;
  2471. break;
  2472. default:
  2473. if (delayacct_is_task_waiting_on_io(tsk))
  2474. stats->nr_io_wait++;
  2475. break;
  2476. }
  2477. }
  2478. cgroup_iter_end(cgrp, &it);
  2479. err:
  2480. return ret;
  2481. }
  2482. /*
  2483. * seq_file methods for the tasks/procs files. The seq_file position is the
  2484. * next pid to display; the seq_file iterator is a pointer to the pid
  2485. * in the cgroup->l->list array.
  2486. */
  2487. static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
  2488. {
  2489. /*
  2490. * Initially we receive a position value that corresponds to
  2491. * one more than the last pid shown (or 0 on the first call or
  2492. * after a seek to the start). Use a binary-search to find the
  2493. * next pid to display, if any
  2494. */
  2495. struct cgroup_pidlist *l = s->private;
  2496. int index = 0, pid = *pos;
  2497. int *iter;
  2498. down_read(&l->mutex);
  2499. if (pid) {
  2500. int end = l->length;
  2501. while (index < end) {
  2502. int mid = (index + end) / 2;
  2503. if (l->list[mid] == pid) {
  2504. index = mid;
  2505. break;
  2506. } else if (l->list[mid] <= pid)
  2507. index = mid + 1;
  2508. else
  2509. end = mid;
  2510. }
  2511. }
  2512. /* If we're off the end of the array, we're done */
  2513. if (index >= l->length)
  2514. return NULL;
  2515. /* Update the abstract position to be the actual pid that we found */
  2516. iter = l->list + index;
  2517. *pos = *iter;
  2518. return iter;
  2519. }
  2520. static void cgroup_pidlist_stop(struct seq_file *s, void *v)
  2521. {
  2522. struct cgroup_pidlist *l = s->private;
  2523. up_read(&l->mutex);
  2524. }
  2525. static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
  2526. {
  2527. struct cgroup_pidlist *l = s->private;
  2528. pid_t *p = v;
  2529. pid_t *end = l->list + l->length;
  2530. /*
  2531. * Advance to the next pid in the array. If this goes off the
  2532. * end, we're done
  2533. */
  2534. p++;
  2535. if (p >= end) {
  2536. return NULL;
  2537. } else {
  2538. *pos = *p;
  2539. return p;
  2540. }
  2541. }
  2542. static int cgroup_pidlist_show(struct seq_file *s, void *v)
  2543. {
  2544. return seq_printf(s, "%d\n", *(int *)v);
  2545. }
  2546. /*
  2547. * seq_operations functions for iterating on pidlists through seq_file -
  2548. * independent of whether it's tasks or procs
  2549. */
  2550. static const struct seq_operations cgroup_pidlist_seq_operations = {
  2551. .start = cgroup_pidlist_start,
  2552. .stop = cgroup_pidlist_stop,
  2553. .next = cgroup_pidlist_next,
  2554. .show = cgroup_pidlist_show,
  2555. };
  2556. static void cgroup_release_pid_array(struct cgroup_pidlist *l)
  2557. {
  2558. /*
  2559. * the case where we're the last user of this particular pidlist will
  2560. * have us remove it from the cgroup's list, which entails taking the
  2561. * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
  2562. * pidlist_mutex, we have to take pidlist_mutex first.
  2563. */
  2564. mutex_lock(&l->owner->pidlist_mutex);
  2565. down_write(&l->mutex);
  2566. BUG_ON(!l->use_count);
  2567. if (!--l->use_count) {
  2568. /* we're the last user if refcount is 0; remove and free */
  2569. list_del(&l->links);
  2570. mutex_unlock(&l->owner->pidlist_mutex);
  2571. pidlist_free(l->list);
  2572. put_pid_ns(l->key.ns);
  2573. up_write(&l->mutex);
  2574. kfree(l);
  2575. return;
  2576. }
  2577. mutex_unlock(&l->owner->pidlist_mutex);
  2578. up_write(&l->mutex);
  2579. }
  2580. static int cgroup_pidlist_release(struct inode *inode, struct file *file)
  2581. {
  2582. struct cgroup_pidlist *l;
  2583. if (!(file->f_mode & FMODE_READ))
  2584. return 0;
  2585. /*
  2586. * the seq_file will only be initialized if the file was opened for
  2587. * reading; hence we check if it's not null only in that case.
  2588. */
  2589. l = ((struct seq_file *)file->private_data)->private;
  2590. cgroup_release_pid_array(l);
  2591. return seq_release(inode, file);
  2592. }
  2593. static const struct file_operations cgroup_pidlist_operations = {
  2594. .read = seq_read,
  2595. .llseek = seq_lseek,
  2596. .write = cgroup_file_write,
  2597. .release = cgroup_pidlist_release,
  2598. };
  2599. /*
  2600. * The following functions handle opens on a file that displays a pidlist
  2601. * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
  2602. * in the cgroup.
  2603. */
  2604. /* helper function for the two below it */
  2605. static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
  2606. {
  2607. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  2608. struct cgroup_pidlist *l;
  2609. int retval;
  2610. /* Nothing to do for write-only files */
  2611. if (!(file->f_mode & FMODE_READ))
  2612. return 0;
  2613. /* have the array populated */
  2614. retval = pidlist_array_load(cgrp, type, &l);
  2615. if (retval)
  2616. return retval;
  2617. /* configure file information */
  2618. file->f_op = &cgroup_pidlist_operations;
  2619. retval = seq_open(file, &cgroup_pidlist_seq_operations);
  2620. if (retval) {
  2621. cgroup_release_pid_array(l);
  2622. return retval;
  2623. }
  2624. ((struct seq_file *)file->private_data)->private = l;
  2625. return 0;
  2626. }
  2627. static int cgroup_tasks_open(struct inode *unused, struct file *file)
  2628. {
  2629. return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
  2630. }
  2631. static int cgroup_procs_open(struct inode *unused, struct file *file)
  2632. {
  2633. return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
  2634. }
  2635. static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
  2636. struct cftype *cft)
  2637. {
  2638. return notify_on_release(cgrp);
  2639. }
  2640. static int cgroup_write_notify_on_release(struct cgroup *cgrp,
  2641. struct cftype *cft,
  2642. u64 val)
  2643. {
  2644. clear_bit(CGRP_RELEASABLE, &cgrp->flags);
  2645. if (val)
  2646. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  2647. else
  2648. clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  2649. return 0;
  2650. }
  2651. /*
  2652. * Unregister event and free resources.
  2653. *
  2654. * Gets called from workqueue.
  2655. */
  2656. static void cgroup_event_remove(struct work_struct *work)
  2657. {
  2658. struct cgroup_event *event = container_of(work, struct cgroup_event,
  2659. remove);
  2660. struct cgroup *cgrp = event->cgrp;
  2661. /* TODO: check return code */
  2662. event->cft->unregister_event(cgrp, event->cft, event->eventfd);
  2663. eventfd_ctx_put(event->eventfd);
  2664. kfree(event);
  2665. dput(cgrp->dentry);
  2666. }
  2667. /*
  2668. * Gets called on POLLHUP on eventfd when user closes it.
  2669. *
  2670. * Called with wqh->lock held and interrupts disabled.
  2671. */
  2672. static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
  2673. int sync, void *key)
  2674. {
  2675. struct cgroup_event *event = container_of(wait,
  2676. struct cgroup_event, wait);
  2677. struct cgroup *cgrp = event->cgrp;
  2678. unsigned long flags = (unsigned long)key;
  2679. if (flags & POLLHUP) {
  2680. remove_wait_queue_locked(event->wqh, &event->wait);
  2681. spin_lock(&cgrp->event_list_lock);
  2682. list_del(&event->list);
  2683. spin_unlock(&cgrp->event_list_lock);
  2684. /*
  2685. * We are in atomic context, but cgroup_event_remove() may
  2686. * sleep, so we have to call it in workqueue.
  2687. */
  2688. schedule_work(&event->remove);
  2689. }
  2690. return 0;
  2691. }
  2692. static void cgroup_event_ptable_queue_proc(struct file *file,
  2693. wait_queue_head_t *wqh, poll_table *pt)
  2694. {
  2695. struct cgroup_event *event = container_of(pt,
  2696. struct cgroup_event, pt);
  2697. event->wqh = wqh;
  2698. add_wait_queue(wqh, &event->wait);
  2699. }
  2700. /*
  2701. * Parse input and register new cgroup event handler.
  2702. *
  2703. * Input must be in format '<event_fd> <control_fd> <args>'.
  2704. * Interpretation of args is defined by control file implementation.
  2705. */
  2706. static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
  2707. const char *buffer)
  2708. {
  2709. struct cgroup_event *event = NULL;
  2710. unsigned int efd, cfd;
  2711. struct file *efile = NULL;
  2712. struct file *cfile = NULL;
  2713. char *endp;
  2714. int ret;
  2715. efd = simple_strtoul(buffer, &endp, 10);
  2716. if (*endp != ' ')
  2717. return -EINVAL;
  2718. buffer = endp + 1;
  2719. cfd = simple_strtoul(buffer, &endp, 10);
  2720. if ((*endp != ' ') && (*endp != '\0'))
  2721. return -EINVAL;
  2722. buffer = endp + 1;
  2723. event = kzalloc(sizeof(*event), GFP_KERNEL);
  2724. if (!event)
  2725. return -ENOMEM;
  2726. event->cgrp = cgrp;
  2727. INIT_LIST_HEAD(&event->list);
  2728. init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
  2729. init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
  2730. INIT_WORK(&event->remove, cgroup_event_remove);
  2731. efile = eventfd_fget(efd);
  2732. if (IS_ERR(efile)) {
  2733. ret = PTR_ERR(efile);
  2734. goto fail;
  2735. }
  2736. event->eventfd = eventfd_ctx_fileget(efile);
  2737. if (IS_ERR(event->eventfd)) {
  2738. ret = PTR_ERR(event->eventfd);
  2739. goto fail;
  2740. }
  2741. cfile = fget(cfd);
  2742. if (!cfile) {
  2743. ret = -EBADF;
  2744. goto fail;
  2745. }
  2746. /* the process need read permission on control file */
  2747. ret = file_permission(cfile, MAY_READ);
  2748. if (ret < 0)
  2749. goto fail;
  2750. event->cft = __file_cft(cfile);
  2751. if (IS_ERR(event->cft)) {
  2752. ret = PTR_ERR(event->cft);
  2753. goto fail;
  2754. }
  2755. if (!event->cft->register_event || !event->cft->unregister_event) {
  2756. ret = -EINVAL;
  2757. goto fail;
  2758. }
  2759. ret = event->cft->register_event(cgrp, event->cft,
  2760. event->eventfd, buffer);
  2761. if (ret)
  2762. goto fail;
  2763. if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
  2764. event->cft->unregister_event(cgrp, event->cft, event->eventfd);
  2765. ret = 0;
  2766. goto fail;
  2767. }
  2768. /*
  2769. * Events should be removed after rmdir of cgroup directory, but before
  2770. * destroying subsystem state objects. Let's take reference to cgroup
  2771. * directory dentry to do that.
  2772. */
  2773. dget(cgrp->dentry);
  2774. spin_lock(&cgrp->event_list_lock);
  2775. list_add(&event->list, &cgrp->event_list);
  2776. spin_unlock(&cgrp->event_list_lock);
  2777. fput(cfile);
  2778. fput(efile);
  2779. return 0;
  2780. fail:
  2781. if (cfile)
  2782. fput(cfile);
  2783. if (event && event->eventfd && !IS_ERR(event->eventfd))
  2784. eventfd_ctx_put(event->eventfd);
  2785. if (!IS_ERR_OR_NULL(efile))
  2786. fput(efile);
  2787. kfree(event);
  2788. return ret;
  2789. }
  2790. /*
  2791. * for the common functions, 'private' gives the type of file
  2792. */
  2793. /* for hysterical raisins, we can't put this on the older files */
  2794. #define CGROUP_FILE_GENERIC_PREFIX "cgroup."
  2795. static struct cftype files[] = {
  2796. {
  2797. .name = "tasks",
  2798. .open = cgroup_tasks_open,
  2799. .write_u64 = cgroup_tasks_write,
  2800. .release = cgroup_pidlist_release,
  2801. .mode = S_IRUGO | S_IWUSR,
  2802. },
  2803. {
  2804. .name = CGROUP_FILE_GENERIC_PREFIX "procs",
  2805. .open = cgroup_procs_open,
  2806. /* .write_u64 = cgroup_procs_write, TODO */
  2807. .release = cgroup_pidlist_release,
  2808. .mode = S_IRUGO,
  2809. },
  2810. {
  2811. .name = "notify_on_release",
  2812. .read_u64 = cgroup_read_notify_on_release,
  2813. .write_u64 = cgroup_write_notify_on_release,
  2814. },
  2815. {
  2816. .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
  2817. .write_string = cgroup_write_event_control,
  2818. .mode = S_IWUGO,
  2819. },
  2820. };
  2821. static struct cftype cft_release_agent = {
  2822. .name = "release_agent",
  2823. .read_seq_string = cgroup_release_agent_show,
  2824. .write_string = cgroup_release_agent_write,
  2825. .max_write_len = PATH_MAX,
  2826. };
  2827. static int cgroup_populate_dir(struct cgroup *cgrp)
  2828. {
  2829. int err;
  2830. struct cgroup_subsys *ss;
  2831. /* First clear out any existing files */
  2832. cgroup_clear_directory(cgrp->dentry);
  2833. err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
  2834. if (err < 0)
  2835. return err;
  2836. if (cgrp == cgrp->top_cgroup) {
  2837. if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
  2838. return err;
  2839. }
  2840. for_each_subsys(cgrp->root, ss) {
  2841. if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
  2842. return err;
  2843. }
  2844. /* This cgroup is ready now */
  2845. for_each_subsys(cgrp->root, ss) {
  2846. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  2847. /*
  2848. * Update id->css pointer and make this css visible from
  2849. * CSS ID functions. This pointer will be dereferened
  2850. * from RCU-read-side without locks.
  2851. */
  2852. if (css->id)
  2853. rcu_assign_pointer(css->id->css, css);
  2854. }
  2855. return 0;
  2856. }
  2857. static void init_cgroup_css(struct cgroup_subsys_state *css,
  2858. struct cgroup_subsys *ss,
  2859. struct cgroup *cgrp)
  2860. {
  2861. css->cgroup = cgrp;
  2862. atomic_set(&css->refcnt, 1);
  2863. css->flags = 0;
  2864. css->id = NULL;
  2865. if (cgrp == dummytop)
  2866. set_bit(CSS_ROOT, &css->flags);
  2867. BUG_ON(cgrp->subsys[ss->subsys_id]);
  2868. cgrp->subsys[ss->subsys_id] = css;
  2869. }
  2870. static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
  2871. {
  2872. /* We need to take each hierarchy_mutex in a consistent order */
  2873. int i;
  2874. /*
  2875. * No worry about a race with rebind_subsystems that might mess up the
  2876. * locking order, since both parties are under cgroup_mutex.
  2877. */
  2878. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2879. struct cgroup_subsys *ss = subsys[i];
  2880. if (ss == NULL)
  2881. continue;
  2882. if (ss->root == root)
  2883. mutex_lock(&ss->hierarchy_mutex);
  2884. }
  2885. }
  2886. static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
  2887. {
  2888. int i;
  2889. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2890. struct cgroup_subsys *ss = subsys[i];
  2891. if (ss == NULL)
  2892. continue;
  2893. if (ss->root == root)
  2894. mutex_unlock(&ss->hierarchy_mutex);
  2895. }
  2896. }
  2897. /*
  2898. * cgroup_create - create a cgroup
  2899. * @parent: cgroup that will be parent of the new cgroup
  2900. * @dentry: dentry of the new cgroup
  2901. * @mode: mode to set on new inode
  2902. *
  2903. * Must be called with the mutex on the parent inode held
  2904. */
  2905. static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
  2906. mode_t mode)
  2907. {
  2908. struct cgroup *cgrp;
  2909. struct cgroupfs_root *root = parent->root;
  2910. int err = 0;
  2911. struct cgroup_subsys *ss;
  2912. struct super_block *sb = root->sb;
  2913. cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
  2914. if (!cgrp)
  2915. return -ENOMEM;
  2916. /* Grab a reference on the superblock so the hierarchy doesn't
  2917. * get deleted on unmount if there are child cgroups. This
  2918. * can be done outside cgroup_mutex, since the sb can't
  2919. * disappear while someone has an open control file on the
  2920. * fs */
  2921. atomic_inc(&sb->s_active);
  2922. mutex_lock(&cgroup_mutex);
  2923. init_cgroup_housekeeping(cgrp);
  2924. cgrp->parent = parent;
  2925. cgrp->root = parent->root;
  2926. cgrp->top_cgroup = parent->top_cgroup;
  2927. if (notify_on_release(parent))
  2928. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  2929. for_each_subsys(root, ss) {
  2930. struct cgroup_subsys_state *css = ss->create(ss, cgrp);
  2931. if (IS_ERR(css)) {
  2932. err = PTR_ERR(css);
  2933. goto err_destroy;
  2934. }
  2935. init_cgroup_css(css, ss, cgrp);
  2936. if (ss->use_id) {
  2937. err = alloc_css_id(ss, parent, cgrp);
  2938. if (err)
  2939. goto err_destroy;
  2940. }
  2941. /* At error, ->destroy() callback has to free assigned ID. */
  2942. }
  2943. cgroup_lock_hierarchy(root);
  2944. list_add(&cgrp->sibling, &cgrp->parent->children);
  2945. cgroup_unlock_hierarchy(root);
  2946. root->number_of_cgroups++;
  2947. err = cgroup_create_dir(cgrp, dentry, mode);
  2948. if (err < 0)
  2949. goto err_remove;
  2950. /* The cgroup directory was pre-locked for us */
  2951. BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
  2952. err = cgroup_populate_dir(cgrp);
  2953. /* If err < 0, we have a half-filled directory - oh well ;) */
  2954. mutex_unlock(&cgroup_mutex);
  2955. mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
  2956. return 0;
  2957. err_remove:
  2958. cgroup_lock_hierarchy(root);
  2959. list_del(&cgrp->sibling);
  2960. cgroup_unlock_hierarchy(root);
  2961. root->number_of_cgroups--;
  2962. err_destroy:
  2963. for_each_subsys(root, ss) {
  2964. if (cgrp->subsys[ss->subsys_id])
  2965. ss->destroy(ss, cgrp);
  2966. }
  2967. mutex_unlock(&cgroup_mutex);
  2968. /* Release the reference count that we took on the superblock */
  2969. deactivate_super(sb);
  2970. kfree(cgrp);
  2971. return err;
  2972. }
  2973. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2974. {
  2975. struct cgroup *c_parent = dentry->d_parent->d_fsdata;
  2976. /* the vfs holds inode->i_mutex already */
  2977. return cgroup_create(c_parent, dentry, mode | S_IFDIR);
  2978. }
  2979. static int cgroup_has_css_refs(struct cgroup *cgrp)
  2980. {
  2981. /* Check the reference count on each subsystem. Since we
  2982. * already established that there are no tasks in the
  2983. * cgroup, if the css refcount is also 1, then there should
  2984. * be no outstanding references, so the subsystem is safe to
  2985. * destroy. We scan across all subsystems rather than using
  2986. * the per-hierarchy linked list of mounted subsystems since
  2987. * we can be called via check_for_release() with no
  2988. * synchronization other than RCU, and the subsystem linked
  2989. * list isn't RCU-safe */
  2990. int i;
  2991. /*
  2992. * We won't need to lock the subsys array, because the subsystems
  2993. * we're concerned about aren't going anywhere since our cgroup root
  2994. * has a reference on them.
  2995. */
  2996. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2997. struct cgroup_subsys *ss = subsys[i];
  2998. struct cgroup_subsys_state *css;
  2999. /* Skip subsystems not present or not in this hierarchy */
  3000. if (ss == NULL || ss->root != cgrp->root)
  3001. continue;
  3002. css = cgrp->subsys[ss->subsys_id];
  3003. /* When called from check_for_release() it's possible
  3004. * that by this point the cgroup has been removed
  3005. * and the css deleted. But a false-positive doesn't
  3006. * matter, since it can only happen if the cgroup
  3007. * has been deleted and hence no longer needs the
  3008. * release agent to be called anyway. */
  3009. if (css && (atomic_read(&css->refcnt) > 1))
  3010. return 1;
  3011. }
  3012. return 0;
  3013. }
  3014. /*
  3015. * Atomically mark all (or else none) of the cgroup's CSS objects as
  3016. * CSS_REMOVED. Return true on success, or false if the cgroup has
  3017. * busy subsystems. Call with cgroup_mutex held
  3018. */
  3019. static int cgroup_clear_css_refs(struct cgroup *cgrp)
  3020. {
  3021. struct cgroup_subsys *ss;
  3022. unsigned long flags;
  3023. bool failed = false;
  3024. local_irq_save(flags);
  3025. for_each_subsys(cgrp->root, ss) {
  3026. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  3027. int refcnt;
  3028. while (1) {
  3029. /* We can only remove a CSS with a refcnt==1 */
  3030. refcnt = atomic_read(&css->refcnt);
  3031. if (refcnt > 1) {
  3032. failed = true;
  3033. goto done;
  3034. }
  3035. BUG_ON(!refcnt);
  3036. /*
  3037. * Drop the refcnt to 0 while we check other
  3038. * subsystems. This will cause any racing
  3039. * css_tryget() to spin until we set the
  3040. * CSS_REMOVED bits or abort
  3041. */
  3042. if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
  3043. break;
  3044. cpu_relax();
  3045. }
  3046. }
  3047. done:
  3048. for_each_subsys(cgrp->root, ss) {
  3049. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  3050. if (failed) {
  3051. /*
  3052. * Restore old refcnt if we previously managed
  3053. * to clear it from 1 to 0
  3054. */
  3055. if (!atomic_read(&css->refcnt))
  3056. atomic_set(&css->refcnt, 1);
  3057. } else {
  3058. /* Commit the fact that the CSS is removed */
  3059. set_bit(CSS_REMOVED, &css->flags);
  3060. }
  3061. }
  3062. local_irq_restore(flags);
  3063. return !failed;
  3064. }
  3065. static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
  3066. {
  3067. struct cgroup *cgrp = dentry->d_fsdata;
  3068. struct dentry *d;
  3069. struct cgroup *parent;
  3070. DEFINE_WAIT(wait);
  3071. struct cgroup_event *event, *tmp;
  3072. int ret;
  3073. /* the vfs holds both inode->i_mutex already */
  3074. again:
  3075. mutex_lock(&cgroup_mutex);
  3076. if (atomic_read(&cgrp->count) != 0) {
  3077. mutex_unlock(&cgroup_mutex);
  3078. return -EBUSY;
  3079. }
  3080. if (!list_empty(&cgrp->children)) {
  3081. mutex_unlock(&cgroup_mutex);
  3082. return -EBUSY;
  3083. }
  3084. mutex_unlock(&cgroup_mutex);
  3085. /*
  3086. * In general, subsystem has no css->refcnt after pre_destroy(). But
  3087. * in racy cases, subsystem may have to get css->refcnt after
  3088. * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
  3089. * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
  3090. * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
  3091. * and subsystem's reference count handling. Please see css_get/put
  3092. * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
  3093. */
  3094. set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3095. /*
  3096. * Call pre_destroy handlers of subsys. Notify subsystems
  3097. * that rmdir() request comes.
  3098. */
  3099. ret = cgroup_call_pre_destroy(cgrp);
  3100. if (ret) {
  3101. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3102. return ret;
  3103. }
  3104. mutex_lock(&cgroup_mutex);
  3105. parent = cgrp->parent;
  3106. if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
  3107. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3108. mutex_unlock(&cgroup_mutex);
  3109. return -EBUSY;
  3110. }
  3111. prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
  3112. if (!cgroup_clear_css_refs(cgrp)) {
  3113. mutex_unlock(&cgroup_mutex);
  3114. /*
  3115. * Because someone may call cgroup_wakeup_rmdir_waiter() before
  3116. * prepare_to_wait(), we need to check this flag.
  3117. */
  3118. if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
  3119. schedule();
  3120. finish_wait(&cgroup_rmdir_waitq, &wait);
  3121. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3122. if (signal_pending(current))
  3123. return -EINTR;
  3124. goto again;
  3125. }
  3126. /* NO css_tryget() can success after here. */
  3127. finish_wait(&cgroup_rmdir_waitq, &wait);
  3128. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3129. spin_lock(&release_list_lock);
  3130. set_bit(CGRP_REMOVED, &cgrp->flags);
  3131. if (!list_empty(&cgrp->release_list))
  3132. list_del(&cgrp->release_list);
  3133. spin_unlock(&release_list_lock);
  3134. cgroup_lock_hierarchy(cgrp->root);
  3135. /* delete this cgroup from parent->children */
  3136. list_del(&cgrp->sibling);
  3137. cgroup_unlock_hierarchy(cgrp->root);
  3138. spin_lock(&cgrp->dentry->d_lock);
  3139. d = dget(cgrp->dentry);
  3140. spin_unlock(&d->d_lock);
  3141. cgroup_d_remove_dir(d);
  3142. dput(d);
  3143. set_bit(CGRP_RELEASABLE, &parent->flags);
  3144. check_for_release(parent);
  3145. /*
  3146. * Unregister events and notify userspace.
  3147. * Notify userspace about cgroup removing only after rmdir of cgroup
  3148. * directory to avoid race between userspace and kernelspace
  3149. */
  3150. spin_lock(&cgrp->event_list_lock);
  3151. list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
  3152. list_del(&event->list);
  3153. remove_wait_queue(event->wqh, &event->wait);
  3154. eventfd_signal(event->eventfd, 1);
  3155. schedule_work(&event->remove);
  3156. }
  3157. spin_unlock(&cgrp->event_list_lock);
  3158. mutex_unlock(&cgroup_mutex);
  3159. return 0;
  3160. }
  3161. static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
  3162. {
  3163. struct cgroup_subsys_state *css;
  3164. printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
  3165. /* Create the top cgroup state for this subsystem */
  3166. list_add(&ss->sibling, &rootnode.subsys_list);
  3167. ss->root = &rootnode;
  3168. css = ss->create(ss, dummytop);
  3169. /* We don't handle early failures gracefully */
  3170. BUG_ON(IS_ERR(css));
  3171. init_cgroup_css(css, ss, dummytop);
  3172. /* Update the init_css_set to contain a subsys
  3173. * pointer to this state - since the subsystem is
  3174. * newly registered, all tasks and hence the
  3175. * init_css_set is in the subsystem's top cgroup. */
  3176. init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
  3177. need_forkexit_callback |= ss->fork || ss->exit;
  3178. /* At system boot, before all subsystems have been
  3179. * registered, no tasks have been forked, so we don't
  3180. * need to invoke fork callbacks here. */
  3181. BUG_ON(!list_empty(&init_task.tasks));
  3182. mutex_init(&ss->hierarchy_mutex);
  3183. lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
  3184. ss->active = 1;
  3185. /* this function shouldn't be used with modular subsystems, since they
  3186. * need to register a subsys_id, among other things */
  3187. BUG_ON(ss->module);
  3188. }
  3189. /**
  3190. * cgroup_load_subsys: load and register a modular subsystem at runtime
  3191. * @ss: the subsystem to load
  3192. *
  3193. * This function should be called in a modular subsystem's initcall. If the
  3194. * subsytem is built as a module, it will be assigned a new subsys_id and set
  3195. * up for use. If the subsystem is built-in anyway, work is delegated to the
  3196. * simpler cgroup_init_subsys.
  3197. */
  3198. int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
  3199. {
  3200. int i;
  3201. struct cgroup_subsys_state *css;
  3202. /* check name and function validity */
  3203. if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
  3204. ss->create == NULL || ss->destroy == NULL)
  3205. return -EINVAL;
  3206. /*
  3207. * we don't support callbacks in modular subsystems. this check is
  3208. * before the ss->module check for consistency; a subsystem that could
  3209. * be a module should still have no callbacks even if the user isn't
  3210. * compiling it as one.
  3211. */
  3212. if (ss->fork || ss->exit)
  3213. return -EINVAL;
  3214. /*
  3215. * an optionally modular subsystem is built-in: we want to do nothing,
  3216. * since cgroup_init_subsys will have already taken care of it.
  3217. */
  3218. if (ss->module == NULL) {
  3219. /* a few sanity checks */
  3220. BUG_ON(ss->subsys_id >= CGROUP_BUILTIN_SUBSYS_COUNT);
  3221. BUG_ON(subsys[ss->subsys_id] != ss);
  3222. return 0;
  3223. }
  3224. /*
  3225. * need to register a subsys id before anything else - for example,
  3226. * init_cgroup_css needs it.
  3227. */
  3228. mutex_lock(&cgroup_mutex);
  3229. /* find the first empty slot in the array */
  3230. for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
  3231. if (subsys[i] == NULL)
  3232. break;
  3233. }
  3234. if (i == CGROUP_SUBSYS_COUNT) {
  3235. /* maximum number of subsystems already registered! */
  3236. mutex_unlock(&cgroup_mutex);
  3237. return -EBUSY;
  3238. }
  3239. /* assign ourselves the subsys_id */
  3240. ss->subsys_id = i;
  3241. subsys[i] = ss;
  3242. /*
  3243. * no ss->create seems to need anything important in the ss struct, so
  3244. * this can happen first (i.e. before the rootnode attachment).
  3245. */
  3246. css = ss->create(ss, dummytop);
  3247. if (IS_ERR(css)) {
  3248. /* failure case - need to deassign the subsys[] slot. */
  3249. subsys[i] = NULL;
  3250. mutex_unlock(&cgroup_mutex);
  3251. return PTR_ERR(css);
  3252. }
  3253. list_add(&ss->sibling, &rootnode.subsys_list);
  3254. ss->root = &rootnode;
  3255. /* our new subsystem will be attached to the dummy hierarchy. */
  3256. init_cgroup_css(css, ss, dummytop);
  3257. /* init_idr must be after init_cgroup_css because it sets css->id. */
  3258. if (ss->use_id) {
  3259. int ret = cgroup_init_idr(ss, css);
  3260. if (ret) {
  3261. dummytop->subsys[ss->subsys_id] = NULL;
  3262. ss->destroy(ss, dummytop);
  3263. subsys[i] = NULL;
  3264. mutex_unlock(&cgroup_mutex);
  3265. return ret;
  3266. }
  3267. }
  3268. /*
  3269. * Now we need to entangle the css into the existing css_sets. unlike
  3270. * in cgroup_init_subsys, there are now multiple css_sets, so each one
  3271. * will need a new pointer to it; done by iterating the css_set_table.
  3272. * furthermore, modifying the existing css_sets will corrupt the hash
  3273. * table state, so each changed css_set will need its hash recomputed.
  3274. * this is all done under the css_set_lock.
  3275. */
  3276. write_lock(&css_set_lock);
  3277. for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
  3278. struct css_set *cg;
  3279. struct hlist_node *node, *tmp;
  3280. struct hlist_head *bucket = &css_set_table[i], *new_bucket;
  3281. hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
  3282. /* skip entries that we already rehashed */
  3283. if (cg->subsys[ss->subsys_id])
  3284. continue;
  3285. /* remove existing entry */
  3286. hlist_del(&cg->hlist);
  3287. /* set new value */
  3288. cg->subsys[ss->subsys_id] = css;
  3289. /* recompute hash and restore entry */
  3290. new_bucket = css_set_hash(cg->subsys);
  3291. hlist_add_head(&cg->hlist, new_bucket);
  3292. }
  3293. }
  3294. write_unlock(&css_set_lock);
  3295. mutex_init(&ss->hierarchy_mutex);
  3296. lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
  3297. ss->active = 1;
  3298. /* success! */
  3299. mutex_unlock(&cgroup_mutex);
  3300. return 0;
  3301. }
  3302. EXPORT_SYMBOL_GPL(cgroup_load_subsys);
  3303. /**
  3304. * cgroup_unload_subsys: unload a modular subsystem
  3305. * @ss: the subsystem to unload
  3306. *
  3307. * This function should be called in a modular subsystem's exitcall. When this
  3308. * function is invoked, the refcount on the subsystem's module will be 0, so
  3309. * the subsystem will not be attached to any hierarchy.
  3310. */
  3311. void cgroup_unload_subsys(struct cgroup_subsys *ss)
  3312. {
  3313. struct cg_cgroup_link *link;
  3314. struct hlist_head *hhead;
  3315. BUG_ON(ss->module == NULL);
  3316. /*
  3317. * we shouldn't be called if the subsystem is in use, and the use of
  3318. * try_module_get in parse_cgroupfs_options should ensure that it
  3319. * doesn't start being used while we're killing it off.
  3320. */
  3321. BUG_ON(ss->root != &rootnode);
  3322. mutex_lock(&cgroup_mutex);
  3323. /* deassign the subsys_id */
  3324. BUG_ON(ss->subsys_id < CGROUP_BUILTIN_SUBSYS_COUNT);
  3325. subsys[ss->subsys_id] = NULL;
  3326. /* remove subsystem from rootnode's list of subsystems */
  3327. list_del(&ss->sibling);
  3328. /*
  3329. * disentangle the css from all css_sets attached to the dummytop. as
  3330. * in loading, we need to pay our respects to the hashtable gods.
  3331. */
  3332. write_lock(&css_set_lock);
  3333. list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
  3334. struct css_set *cg = link->cg;
  3335. hlist_del(&cg->hlist);
  3336. BUG_ON(!cg->subsys[ss->subsys_id]);
  3337. cg->subsys[ss->subsys_id] = NULL;
  3338. hhead = css_set_hash(cg->subsys);
  3339. hlist_add_head(&cg->hlist, hhead);
  3340. }
  3341. write_unlock(&css_set_lock);
  3342. /*
  3343. * remove subsystem's css from the dummytop and free it - need to free
  3344. * before marking as null because ss->destroy needs the cgrp->subsys
  3345. * pointer to find their state. note that this also takes care of
  3346. * freeing the css_id.
  3347. */
  3348. ss->destroy(ss, dummytop);
  3349. dummytop->subsys[ss->subsys_id] = NULL;
  3350. mutex_unlock(&cgroup_mutex);
  3351. }
  3352. EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
  3353. /**
  3354. * cgroup_init_early - cgroup initialization at system boot
  3355. *
  3356. * Initialize cgroups at system boot, and initialize any
  3357. * subsystems that request early init.
  3358. */
  3359. int __init cgroup_init_early(void)
  3360. {
  3361. int i;
  3362. atomic_set(&init_css_set.refcount, 1);
  3363. INIT_LIST_HEAD(&init_css_set.cg_links);
  3364. INIT_LIST_HEAD(&init_css_set.tasks);
  3365. INIT_HLIST_NODE(&init_css_set.hlist);
  3366. css_set_count = 1;
  3367. init_cgroup_root(&rootnode);
  3368. root_count = 1;
  3369. init_task.cgroups = &init_css_set;
  3370. init_css_set_link.cg = &init_css_set;
  3371. init_css_set_link.cgrp = dummytop;
  3372. list_add(&init_css_set_link.cgrp_link_list,
  3373. &rootnode.top_cgroup.css_sets);
  3374. list_add(&init_css_set_link.cg_link_list,
  3375. &init_css_set.cg_links);
  3376. for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
  3377. INIT_HLIST_HEAD(&css_set_table[i]);
  3378. /* at bootup time, we don't worry about modular subsystems */
  3379. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3380. struct cgroup_subsys *ss = subsys[i];
  3381. BUG_ON(!ss->name);
  3382. BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
  3383. BUG_ON(!ss->create);
  3384. BUG_ON(!ss->destroy);
  3385. if (ss->subsys_id != i) {
  3386. printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
  3387. ss->name, ss->subsys_id);
  3388. BUG();
  3389. }
  3390. if (ss->early_init)
  3391. cgroup_init_subsys(ss);
  3392. }
  3393. return 0;
  3394. }
  3395. /**
  3396. * cgroup_init - cgroup initialization
  3397. *
  3398. * Register cgroup filesystem and /proc file, and initialize
  3399. * any subsystems that didn't request early init.
  3400. */
  3401. int __init cgroup_init(void)
  3402. {
  3403. int err;
  3404. int i;
  3405. struct hlist_head *hhead;
  3406. err = bdi_init(&cgroup_backing_dev_info);
  3407. if (err)
  3408. return err;
  3409. /* at bootup time, we don't worry about modular subsystems */
  3410. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3411. struct cgroup_subsys *ss = subsys[i];
  3412. if (!ss->early_init)
  3413. cgroup_init_subsys(ss);
  3414. if (ss->use_id)
  3415. cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
  3416. }
  3417. /* Add init_css_set to the hash table */
  3418. hhead = css_set_hash(init_css_set.subsys);
  3419. hlist_add_head(&init_css_set.hlist, hhead);
  3420. BUG_ON(!init_root_id(&rootnode));
  3421. err = register_filesystem(&cgroup_fs_type);
  3422. if (err < 0)
  3423. goto out;
  3424. proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
  3425. out:
  3426. if (err)
  3427. bdi_destroy(&cgroup_backing_dev_info);
  3428. return err;
  3429. }
  3430. /*
  3431. * proc_cgroup_show()
  3432. * - Print task's cgroup paths into seq_file, one line for each hierarchy
  3433. * - Used for /proc/<pid>/cgroup.
  3434. * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
  3435. * doesn't really matter if tsk->cgroup changes after we read it,
  3436. * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
  3437. * anyway. No need to check that tsk->cgroup != NULL, thanks to
  3438. * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
  3439. * cgroup to top_cgroup.
  3440. */
  3441. /* TODO: Use a proper seq_file iterator */
  3442. static int proc_cgroup_show(struct seq_file *m, void *v)
  3443. {
  3444. struct pid *pid;
  3445. struct task_struct *tsk;
  3446. char *buf;
  3447. int retval;
  3448. struct cgroupfs_root *root;
  3449. retval = -ENOMEM;
  3450. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3451. if (!buf)
  3452. goto out;
  3453. retval = -ESRCH;
  3454. pid = m->private;
  3455. tsk = get_pid_task(pid, PIDTYPE_PID);
  3456. if (!tsk)
  3457. goto out_free;
  3458. retval = 0;
  3459. mutex_lock(&cgroup_mutex);
  3460. for_each_active_root(root) {
  3461. struct cgroup_subsys *ss;
  3462. struct cgroup *cgrp;
  3463. int count = 0;
  3464. seq_printf(m, "%d:", root->hierarchy_id);
  3465. for_each_subsys(root, ss)
  3466. seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
  3467. if (strlen(root->name))
  3468. seq_printf(m, "%sname=%s", count ? "," : "",
  3469. root->name);
  3470. seq_putc(m, ':');
  3471. cgrp = task_cgroup_from_root(tsk, root);
  3472. retval = cgroup_path(cgrp, buf, PAGE_SIZE);
  3473. if (retval < 0)
  3474. goto out_unlock;
  3475. seq_puts(m, buf);
  3476. seq_putc(m, '\n');
  3477. }
  3478. out_unlock:
  3479. mutex_unlock(&cgroup_mutex);
  3480. put_task_struct(tsk);
  3481. out_free:
  3482. kfree(buf);
  3483. out:
  3484. return retval;
  3485. }
  3486. static int cgroup_open(struct inode *inode, struct file *file)
  3487. {
  3488. struct pid *pid = PROC_I(inode)->pid;
  3489. return single_open(file, proc_cgroup_show, pid);
  3490. }
  3491. const struct file_operations proc_cgroup_operations = {
  3492. .open = cgroup_open,
  3493. .read = seq_read,
  3494. .llseek = seq_lseek,
  3495. .release = single_release,
  3496. };
  3497. /* Display information about each subsystem and each hierarchy */
  3498. static int proc_cgroupstats_show(struct seq_file *m, void *v)
  3499. {
  3500. int i;
  3501. seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
  3502. /*
  3503. * ideally we don't want subsystems moving around while we do this.
  3504. * cgroup_mutex is also necessary to guarantee an atomic snapshot of
  3505. * subsys/hierarchy state.
  3506. */
  3507. mutex_lock(&cgroup_mutex);
  3508. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  3509. struct cgroup_subsys *ss = subsys[i];
  3510. if (ss == NULL)
  3511. continue;
  3512. seq_printf(m, "%s\t%d\t%d\t%d\n",
  3513. ss->name, ss->root->hierarchy_id,
  3514. ss->root->number_of_cgroups, !ss->disabled);
  3515. }
  3516. mutex_unlock(&cgroup_mutex);
  3517. return 0;
  3518. }
  3519. static int cgroupstats_open(struct inode *inode, struct file *file)
  3520. {
  3521. return single_open(file, proc_cgroupstats_show, NULL);
  3522. }
  3523. static const struct file_operations proc_cgroupstats_operations = {
  3524. .open = cgroupstats_open,
  3525. .read = seq_read,
  3526. .llseek = seq_lseek,
  3527. .release = single_release,
  3528. };
  3529. /**
  3530. * cgroup_fork - attach newly forked task to its parents cgroup.
  3531. * @child: pointer to task_struct of forking parent process.
  3532. *
  3533. * Description: A task inherits its parent's cgroup at fork().
  3534. *
  3535. * A pointer to the shared css_set was automatically copied in
  3536. * fork.c by dup_task_struct(). However, we ignore that copy, since
  3537. * it was not made under the protection of RCU or cgroup_mutex, so
  3538. * might no longer be a valid cgroup pointer. cgroup_attach_task() might
  3539. * have already changed current->cgroups, allowing the previously
  3540. * referenced cgroup group to be removed and freed.
  3541. *
  3542. * At the point that cgroup_fork() is called, 'current' is the parent
  3543. * task, and the passed argument 'child' points to the child task.
  3544. */
  3545. void cgroup_fork(struct task_struct *child)
  3546. {
  3547. task_lock(current);
  3548. child->cgroups = current->cgroups;
  3549. get_css_set(child->cgroups);
  3550. task_unlock(current);
  3551. INIT_LIST_HEAD(&child->cg_list);
  3552. }
  3553. /**
  3554. * cgroup_fork_callbacks - run fork callbacks
  3555. * @child: the new task
  3556. *
  3557. * Called on a new task very soon before adding it to the
  3558. * tasklist. No need to take any locks since no-one can
  3559. * be operating on this task.
  3560. */
  3561. void cgroup_fork_callbacks(struct task_struct *child)
  3562. {
  3563. if (need_forkexit_callback) {
  3564. int i;
  3565. /*
  3566. * forkexit callbacks are only supported for builtin
  3567. * subsystems, and the builtin section of the subsys array is
  3568. * immutable, so we don't need to lock the subsys array here.
  3569. */
  3570. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3571. struct cgroup_subsys *ss = subsys[i];
  3572. if (ss->fork)
  3573. ss->fork(ss, child);
  3574. }
  3575. }
  3576. }
  3577. /**
  3578. * cgroup_post_fork - called on a new task after adding it to the task list
  3579. * @child: the task in question
  3580. *
  3581. * Adds the task to the list running through its css_set if necessary.
  3582. * Has to be after the task is visible on the task list in case we race
  3583. * with the first call to cgroup_iter_start() - to guarantee that the
  3584. * new task ends up on its list.
  3585. */
  3586. void cgroup_post_fork(struct task_struct *child)
  3587. {
  3588. if (use_task_css_set_links) {
  3589. write_lock(&css_set_lock);
  3590. task_lock(child);
  3591. if (list_empty(&child->cg_list))
  3592. list_add(&child->cg_list, &child->cgroups->tasks);
  3593. task_unlock(child);
  3594. write_unlock(&css_set_lock);
  3595. }
  3596. }
  3597. /**
  3598. * cgroup_exit - detach cgroup from exiting task
  3599. * @tsk: pointer to task_struct of exiting process
  3600. * @run_callback: run exit callbacks?
  3601. *
  3602. * Description: Detach cgroup from @tsk and release it.
  3603. *
  3604. * Note that cgroups marked notify_on_release force every task in
  3605. * them to take the global cgroup_mutex mutex when exiting.
  3606. * This could impact scaling on very large systems. Be reluctant to
  3607. * use notify_on_release cgroups where very high task exit scaling
  3608. * is required on large systems.
  3609. *
  3610. * the_top_cgroup_hack:
  3611. *
  3612. * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
  3613. *
  3614. * We call cgroup_exit() while the task is still competent to
  3615. * handle notify_on_release(), then leave the task attached to the
  3616. * root cgroup in each hierarchy for the remainder of its exit.
  3617. *
  3618. * To do this properly, we would increment the reference count on
  3619. * top_cgroup, and near the very end of the kernel/exit.c do_exit()
  3620. * code we would add a second cgroup function call, to drop that
  3621. * reference. This would just create an unnecessary hot spot on
  3622. * the top_cgroup reference count, to no avail.
  3623. *
  3624. * Normally, holding a reference to a cgroup without bumping its
  3625. * count is unsafe. The cgroup could go away, or someone could
  3626. * attach us to a different cgroup, decrementing the count on
  3627. * the first cgroup that we never incremented. But in this case,
  3628. * top_cgroup isn't going away, and either task has PF_EXITING set,
  3629. * which wards off any cgroup_attach_task() attempts, or task is a failed
  3630. * fork, never visible to cgroup_attach_task.
  3631. */
  3632. void cgroup_exit(struct task_struct *tsk, int run_callbacks)
  3633. {
  3634. int i;
  3635. struct css_set *cg;
  3636. if (run_callbacks && need_forkexit_callback) {
  3637. /*
  3638. * modular subsystems can't use callbacks, so no need to lock
  3639. * the subsys array
  3640. */
  3641. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3642. struct cgroup_subsys *ss = subsys[i];
  3643. if (ss->exit)
  3644. ss->exit(ss, tsk);
  3645. }
  3646. }
  3647. /*
  3648. * Unlink from the css_set task list if necessary.
  3649. * Optimistically check cg_list before taking
  3650. * css_set_lock
  3651. */
  3652. if (!list_empty(&tsk->cg_list)) {
  3653. write_lock(&css_set_lock);
  3654. if (!list_empty(&tsk->cg_list))
  3655. list_del(&tsk->cg_list);
  3656. write_unlock(&css_set_lock);
  3657. }
  3658. /* Reassign the task to the init_css_set. */
  3659. task_lock(tsk);
  3660. cg = tsk->cgroups;
  3661. tsk->cgroups = &init_css_set;
  3662. task_unlock(tsk);
  3663. if (cg)
  3664. put_css_set_taskexit(cg);
  3665. }
  3666. /**
  3667. * cgroup_clone - clone the cgroup the given subsystem is attached to
  3668. * @tsk: the task to be moved
  3669. * @subsys: the given subsystem
  3670. * @nodename: the name for the new cgroup
  3671. *
  3672. * Duplicate the current cgroup in the hierarchy that the given
  3673. * subsystem is attached to, and move this task into the new
  3674. * child.
  3675. */
  3676. int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
  3677. char *nodename)
  3678. {
  3679. struct dentry *dentry;
  3680. int ret = 0;
  3681. struct cgroup *parent, *child;
  3682. struct inode *inode;
  3683. struct css_set *cg;
  3684. struct cgroupfs_root *root;
  3685. struct cgroup_subsys *ss;
  3686. /* We shouldn't be called by an unregistered subsystem */
  3687. BUG_ON(!subsys->active);
  3688. /* First figure out what hierarchy and cgroup we're dealing
  3689. * with, and pin them so we can drop cgroup_mutex */
  3690. mutex_lock(&cgroup_mutex);
  3691. again:
  3692. root = subsys->root;
  3693. if (root == &rootnode) {
  3694. mutex_unlock(&cgroup_mutex);
  3695. return 0;
  3696. }
  3697. /* Pin the hierarchy */
  3698. if (!atomic_inc_not_zero(&root->sb->s_active)) {
  3699. /* We race with the final deactivate_super() */
  3700. mutex_unlock(&cgroup_mutex);
  3701. return 0;
  3702. }
  3703. /* Keep the cgroup alive */
  3704. task_lock(tsk);
  3705. parent = task_cgroup(tsk, subsys->subsys_id);
  3706. cg = tsk->cgroups;
  3707. get_css_set(cg);
  3708. task_unlock(tsk);
  3709. mutex_unlock(&cgroup_mutex);
  3710. /* Now do the VFS work to create a cgroup */
  3711. inode = parent->dentry->d_inode;
  3712. /* Hold the parent directory mutex across this operation to
  3713. * stop anyone else deleting the new cgroup */
  3714. mutex_lock(&inode->i_mutex);
  3715. dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
  3716. if (IS_ERR(dentry)) {
  3717. printk(KERN_INFO
  3718. "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
  3719. PTR_ERR(dentry));
  3720. ret = PTR_ERR(dentry);
  3721. goto out_release;
  3722. }
  3723. /* Create the cgroup directory, which also creates the cgroup */
  3724. ret = vfs_mkdir(inode, dentry, 0755);
  3725. child = __d_cgrp(dentry);
  3726. dput(dentry);
  3727. if (ret) {
  3728. printk(KERN_INFO
  3729. "Failed to create cgroup %s: %d\n", nodename,
  3730. ret);
  3731. goto out_release;
  3732. }
  3733. /* The cgroup now exists. Retake cgroup_mutex and check
  3734. * that we're still in the same state that we thought we
  3735. * were. */
  3736. mutex_lock(&cgroup_mutex);
  3737. if ((root != subsys->root) ||
  3738. (parent != task_cgroup(tsk, subsys->subsys_id))) {
  3739. /* Aargh, we raced ... */
  3740. mutex_unlock(&inode->i_mutex);
  3741. put_css_set(cg);
  3742. deactivate_super(root->sb);
  3743. /* The cgroup is still accessible in the VFS, but
  3744. * we're not going to try to rmdir() it at this
  3745. * point. */
  3746. printk(KERN_INFO
  3747. "Race in cgroup_clone() - leaking cgroup %s\n",
  3748. nodename);
  3749. goto again;
  3750. }
  3751. /* do any required auto-setup */
  3752. for_each_subsys(root, ss) {
  3753. if (ss->post_clone)
  3754. ss->post_clone(ss, child);
  3755. }
  3756. /* All seems fine. Finish by moving the task into the new cgroup */
  3757. ret = cgroup_attach_task(child, tsk);
  3758. mutex_unlock(&cgroup_mutex);
  3759. out_release:
  3760. mutex_unlock(&inode->i_mutex);
  3761. mutex_lock(&cgroup_mutex);
  3762. put_css_set(cg);
  3763. mutex_unlock(&cgroup_mutex);
  3764. deactivate_super(root->sb);
  3765. return ret;
  3766. }
  3767. /**
  3768. * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
  3769. * @cgrp: the cgroup in question
  3770. * @task: the task in question
  3771. *
  3772. * See if @cgrp is a descendant of @task's cgroup in the appropriate
  3773. * hierarchy.
  3774. *
  3775. * If we are sending in dummytop, then presumably we are creating
  3776. * the top cgroup in the subsystem.
  3777. *
  3778. * Called only by the ns (nsproxy) cgroup.
  3779. */
  3780. int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
  3781. {
  3782. int ret;
  3783. struct cgroup *target;
  3784. if (cgrp == dummytop)
  3785. return 1;
  3786. target = task_cgroup_from_root(task, cgrp->root);
  3787. while (cgrp != target && cgrp!= cgrp->top_cgroup)
  3788. cgrp = cgrp->parent;
  3789. ret = (cgrp == target);
  3790. return ret;
  3791. }
  3792. static void check_for_release(struct cgroup *cgrp)
  3793. {
  3794. /* All of these checks rely on RCU to keep the cgroup
  3795. * structure alive */
  3796. if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
  3797. && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
  3798. /* Control Group is currently removeable. If it's not
  3799. * already queued for a userspace notification, queue
  3800. * it now */
  3801. int need_schedule_work = 0;
  3802. spin_lock(&release_list_lock);
  3803. if (!cgroup_is_removed(cgrp) &&
  3804. list_empty(&cgrp->release_list)) {
  3805. list_add(&cgrp->release_list, &release_list);
  3806. need_schedule_work = 1;
  3807. }
  3808. spin_unlock(&release_list_lock);
  3809. if (need_schedule_work)
  3810. schedule_work(&release_agent_work);
  3811. }
  3812. }
  3813. /* Caller must verify that the css is not for root cgroup */
  3814. void __css_put(struct cgroup_subsys_state *css, int count)
  3815. {
  3816. struct cgroup *cgrp = css->cgroup;
  3817. int val;
  3818. rcu_read_lock();
  3819. val = atomic_sub_return(count, &css->refcnt);
  3820. if (val == 1) {
  3821. if (notify_on_release(cgrp)) {
  3822. set_bit(CGRP_RELEASABLE, &cgrp->flags);
  3823. check_for_release(cgrp);
  3824. }
  3825. cgroup_wakeup_rmdir_waiter(cgrp);
  3826. }
  3827. rcu_read_unlock();
  3828. WARN_ON_ONCE(val < 1);
  3829. }
  3830. EXPORT_SYMBOL_GPL(__css_put);
  3831. /*
  3832. * Notify userspace when a cgroup is released, by running the
  3833. * configured release agent with the name of the cgroup (path
  3834. * relative to the root of cgroup file system) as the argument.
  3835. *
  3836. * Most likely, this user command will try to rmdir this cgroup.
  3837. *
  3838. * This races with the possibility that some other task will be
  3839. * attached to this cgroup before it is removed, or that some other
  3840. * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
  3841. * The presumed 'rmdir' will fail quietly if this cgroup is no longer
  3842. * unused, and this cgroup will be reprieved from its death sentence,
  3843. * to continue to serve a useful existence. Next time it's released,
  3844. * we will get notified again, if it still has 'notify_on_release' set.
  3845. *
  3846. * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
  3847. * means only wait until the task is successfully execve()'d. The
  3848. * separate release agent task is forked by call_usermodehelper(),
  3849. * then control in this thread returns here, without waiting for the
  3850. * release agent task. We don't bother to wait because the caller of
  3851. * this routine has no use for the exit status of the release agent
  3852. * task, so no sense holding our caller up for that.
  3853. */
  3854. static void cgroup_release_agent(struct work_struct *work)
  3855. {
  3856. BUG_ON(work != &release_agent_work);
  3857. mutex_lock(&cgroup_mutex);
  3858. spin_lock(&release_list_lock);
  3859. while (!list_empty(&release_list)) {
  3860. char *argv[3], *envp[3];
  3861. int i;
  3862. char *pathbuf = NULL, *agentbuf = NULL;
  3863. struct cgroup *cgrp = list_entry(release_list.next,
  3864. struct cgroup,
  3865. release_list);
  3866. list_del_init(&cgrp->release_list);
  3867. spin_unlock(&release_list_lock);
  3868. pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3869. if (!pathbuf)
  3870. goto continue_free;
  3871. if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
  3872. goto continue_free;
  3873. agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
  3874. if (!agentbuf)
  3875. goto continue_free;
  3876. i = 0;
  3877. argv[i++] = agentbuf;
  3878. argv[i++] = pathbuf;
  3879. argv[i] = NULL;
  3880. i = 0;
  3881. /* minimal command environment */
  3882. envp[i++] = "HOME=/";
  3883. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  3884. envp[i] = NULL;
  3885. /* Drop the lock while we invoke the usermode helper,
  3886. * since the exec could involve hitting disk and hence
  3887. * be a slow process */
  3888. mutex_unlock(&cgroup_mutex);
  3889. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  3890. mutex_lock(&cgroup_mutex);
  3891. continue_free:
  3892. kfree(pathbuf);
  3893. kfree(agentbuf);
  3894. spin_lock(&release_list_lock);
  3895. }
  3896. spin_unlock(&release_list_lock);
  3897. mutex_unlock(&cgroup_mutex);
  3898. }
  3899. static int __init cgroup_disable(char *str)
  3900. {
  3901. int i;
  3902. char *token;
  3903. while ((token = strsep(&str, ",")) != NULL) {
  3904. if (!*token)
  3905. continue;
  3906. /*
  3907. * cgroup_disable, being at boot time, can't know about module
  3908. * subsystems, so we don't worry about them.
  3909. */
  3910. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3911. struct cgroup_subsys *ss = subsys[i];
  3912. if (!strcmp(token, ss->name)) {
  3913. ss->disabled = 1;
  3914. printk(KERN_INFO "Disabling %s control group"
  3915. " subsystem\n", ss->name);
  3916. break;
  3917. }
  3918. }
  3919. }
  3920. return 1;
  3921. }
  3922. __setup("cgroup_disable=", cgroup_disable);
  3923. /*
  3924. * Functons for CSS ID.
  3925. */
  3926. /*
  3927. *To get ID other than 0, this should be called when !cgroup_is_removed().
  3928. */
  3929. unsigned short css_id(struct cgroup_subsys_state *css)
  3930. {
  3931. struct css_id *cssid = rcu_dereference(css->id);
  3932. if (cssid)
  3933. return cssid->id;
  3934. return 0;
  3935. }
  3936. EXPORT_SYMBOL_GPL(css_id);
  3937. unsigned short css_depth(struct cgroup_subsys_state *css)
  3938. {
  3939. struct css_id *cssid = rcu_dereference(css->id);
  3940. if (cssid)
  3941. return cssid->depth;
  3942. return 0;
  3943. }
  3944. EXPORT_SYMBOL_GPL(css_depth);
  3945. bool css_is_ancestor(struct cgroup_subsys_state *child,
  3946. const struct cgroup_subsys_state *root)
  3947. {
  3948. struct css_id *child_id = rcu_dereference(child->id);
  3949. struct css_id *root_id = rcu_dereference(root->id);
  3950. if (!child_id || !root_id || (child_id->depth < root_id->depth))
  3951. return false;
  3952. return child_id->stack[root_id->depth] == root_id->id;
  3953. }
  3954. static void __free_css_id_cb(struct rcu_head *head)
  3955. {
  3956. struct css_id *id;
  3957. id = container_of(head, struct css_id, rcu_head);
  3958. kfree(id);
  3959. }
  3960. void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
  3961. {
  3962. struct css_id *id = css->id;
  3963. /* When this is called before css_id initialization, id can be NULL */
  3964. if (!id)
  3965. return;
  3966. BUG_ON(!ss->use_id);
  3967. rcu_assign_pointer(id->css, NULL);
  3968. rcu_assign_pointer(css->id, NULL);
  3969. spin_lock(&ss->id_lock);
  3970. idr_remove(&ss->idr, id->id);
  3971. spin_unlock(&ss->id_lock);
  3972. call_rcu(&id->rcu_head, __free_css_id_cb);
  3973. }
  3974. EXPORT_SYMBOL_GPL(free_css_id);
  3975. /*
  3976. * This is called by init or create(). Then, calls to this function are
  3977. * always serialized (By cgroup_mutex() at create()).
  3978. */
  3979. static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
  3980. {
  3981. struct css_id *newid;
  3982. int myid, error, size;
  3983. BUG_ON(!ss->use_id);
  3984. size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
  3985. newid = kzalloc(size, GFP_KERNEL);
  3986. if (!newid)
  3987. return ERR_PTR(-ENOMEM);
  3988. /* get id */
  3989. if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
  3990. error = -ENOMEM;
  3991. goto err_out;
  3992. }
  3993. spin_lock(&ss->id_lock);
  3994. /* Don't use 0. allocates an ID of 1-65535 */
  3995. error = idr_get_new_above(&ss->idr, newid, 1, &myid);
  3996. spin_unlock(&ss->id_lock);
  3997. /* Returns error when there are no free spaces for new ID.*/
  3998. if (error) {
  3999. error = -ENOSPC;
  4000. goto err_out;
  4001. }
  4002. if (myid > CSS_ID_MAX)
  4003. goto remove_idr;
  4004. newid->id = myid;
  4005. newid->depth = depth;
  4006. return newid;
  4007. remove_idr:
  4008. error = -ENOSPC;
  4009. spin_lock(&ss->id_lock);
  4010. idr_remove(&ss->idr, myid);
  4011. spin_unlock(&ss->id_lock);
  4012. err_out:
  4013. kfree(newid);
  4014. return ERR_PTR(error);
  4015. }
  4016. static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
  4017. struct cgroup_subsys_state *rootcss)
  4018. {
  4019. struct css_id *newid;
  4020. spin_lock_init(&ss->id_lock);
  4021. idr_init(&ss->idr);
  4022. newid = get_new_cssid(ss, 0);
  4023. if (IS_ERR(newid))
  4024. return PTR_ERR(newid);
  4025. newid->stack[0] = newid->id;
  4026. newid->css = rootcss;
  4027. rootcss->id = newid;
  4028. return 0;
  4029. }
  4030. static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
  4031. struct cgroup *child)
  4032. {
  4033. int subsys_id, i, depth = 0;
  4034. struct cgroup_subsys_state *parent_css, *child_css;
  4035. struct css_id *child_id, *parent_id = NULL;
  4036. subsys_id = ss->subsys_id;
  4037. parent_css = parent->subsys[subsys_id];
  4038. child_css = child->subsys[subsys_id];
  4039. depth = css_depth(parent_css) + 1;
  4040. parent_id = parent_css->id;
  4041. child_id = get_new_cssid(ss, depth);
  4042. if (IS_ERR(child_id))
  4043. return PTR_ERR(child_id);
  4044. for (i = 0; i < depth; i++)
  4045. child_id->stack[i] = parent_id->stack[i];
  4046. child_id->stack[depth] = child_id->id;
  4047. /*
  4048. * child_id->css pointer will be set after this cgroup is available
  4049. * see cgroup_populate_dir()
  4050. */
  4051. rcu_assign_pointer(child_css->id, child_id);
  4052. return 0;
  4053. }
  4054. /**
  4055. * css_lookup - lookup css by id
  4056. * @ss: cgroup subsys to be looked into.
  4057. * @id: the id
  4058. *
  4059. * Returns pointer to cgroup_subsys_state if there is valid one with id.
  4060. * NULL if not. Should be called under rcu_read_lock()
  4061. */
  4062. struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
  4063. {
  4064. struct css_id *cssid = NULL;
  4065. BUG_ON(!ss->use_id);
  4066. cssid = idr_find(&ss->idr, id);
  4067. if (unlikely(!cssid))
  4068. return NULL;
  4069. return rcu_dereference(cssid->css);
  4070. }
  4071. EXPORT_SYMBOL_GPL(css_lookup);
  4072. /**
  4073. * css_get_next - lookup next cgroup under specified hierarchy.
  4074. * @ss: pointer to subsystem
  4075. * @id: current position of iteration.
  4076. * @root: pointer to css. search tree under this.
  4077. * @foundid: position of found object.
  4078. *
  4079. * Search next css under the specified hierarchy of rootid. Calling under
  4080. * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
  4081. */
  4082. struct cgroup_subsys_state *
  4083. css_get_next(struct cgroup_subsys *ss, int id,
  4084. struct cgroup_subsys_state *root, int *foundid)
  4085. {
  4086. struct cgroup_subsys_state *ret = NULL;
  4087. struct css_id *tmp;
  4088. int tmpid;
  4089. int rootid = css_id(root);
  4090. int depth = css_depth(root);
  4091. if (!rootid)
  4092. return NULL;
  4093. BUG_ON(!ss->use_id);
  4094. /* fill start point for scan */
  4095. tmpid = id;
  4096. while (1) {
  4097. /*
  4098. * scan next entry from bitmap(tree), tmpid is updated after
  4099. * idr_get_next().
  4100. */
  4101. spin_lock(&ss->id_lock);
  4102. tmp = idr_get_next(&ss->idr, &tmpid);
  4103. spin_unlock(&ss->id_lock);
  4104. if (!tmp)
  4105. break;
  4106. if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
  4107. ret = rcu_dereference(tmp->css);
  4108. if (ret) {
  4109. *foundid = tmpid;
  4110. break;
  4111. }
  4112. }
  4113. /* continue to scan from next id */
  4114. tmpid = tmpid + 1;
  4115. }
  4116. return ret;
  4117. }
  4118. #ifdef CONFIG_CGROUP_DEBUG
  4119. static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
  4120. struct cgroup *cont)
  4121. {
  4122. struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
  4123. if (!css)
  4124. return ERR_PTR(-ENOMEM);
  4125. return css;
  4126. }
  4127. static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
  4128. {
  4129. kfree(cont->subsys[debug_subsys_id]);
  4130. }
  4131. static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
  4132. {
  4133. return atomic_read(&cont->count);
  4134. }
  4135. static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
  4136. {
  4137. return cgroup_task_count(cont);
  4138. }
  4139. static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
  4140. {
  4141. return (u64)(unsigned long)current->cgroups;
  4142. }
  4143. static u64 current_css_set_refcount_read(struct cgroup *cont,
  4144. struct cftype *cft)
  4145. {
  4146. u64 count;
  4147. rcu_read_lock();
  4148. count = atomic_read(&current->cgroups->refcount);
  4149. rcu_read_unlock();
  4150. return count;
  4151. }
  4152. static int current_css_set_cg_links_read(struct cgroup *cont,
  4153. struct cftype *cft,
  4154. struct seq_file *seq)
  4155. {
  4156. struct cg_cgroup_link *link;
  4157. struct css_set *cg;
  4158. read_lock(&css_set_lock);
  4159. rcu_read_lock();
  4160. cg = rcu_dereference(current->cgroups);
  4161. list_for_each_entry(link, &cg->cg_links, cg_link_list) {
  4162. struct cgroup *c = link->cgrp;
  4163. const char *name;
  4164. if (c->dentry)
  4165. name = c->dentry->d_name.name;
  4166. else
  4167. name = "?";
  4168. seq_printf(seq, "Root %d group %s\n",
  4169. c->root->hierarchy_id, name);
  4170. }
  4171. rcu_read_unlock();
  4172. read_unlock(&css_set_lock);
  4173. return 0;
  4174. }
  4175. #define MAX_TASKS_SHOWN_PER_CSS 25
  4176. static int cgroup_css_links_read(struct cgroup *cont,
  4177. struct cftype *cft,
  4178. struct seq_file *seq)
  4179. {
  4180. struct cg_cgroup_link *link;
  4181. read_lock(&css_set_lock);
  4182. list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
  4183. struct css_set *cg = link->cg;
  4184. struct task_struct *task;
  4185. int count = 0;
  4186. seq_printf(seq, "css_set %p\n", cg);
  4187. list_for_each_entry(task, &cg->tasks, cg_list) {
  4188. if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
  4189. seq_puts(seq, " ...\n");
  4190. break;
  4191. } else {
  4192. seq_printf(seq, " task %d\n",
  4193. task_pid_vnr(task));
  4194. }
  4195. }
  4196. }
  4197. read_unlock(&css_set_lock);
  4198. return 0;
  4199. }
  4200. static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
  4201. {
  4202. return test_bit(CGRP_RELEASABLE, &cgrp->flags);
  4203. }
  4204. static struct cftype debug_files[] = {
  4205. {
  4206. .name = "cgroup_refcount",
  4207. .read_u64 = cgroup_refcount_read,
  4208. },
  4209. {
  4210. .name = "taskcount",
  4211. .read_u64 = debug_taskcount_read,
  4212. },
  4213. {
  4214. .name = "current_css_set",
  4215. .read_u64 = current_css_set_read,
  4216. },
  4217. {
  4218. .name = "current_css_set_refcount",
  4219. .read_u64 = current_css_set_refcount_read,
  4220. },
  4221. {
  4222. .name = "current_css_set_cg_links",
  4223. .read_seq_string = current_css_set_cg_links_read,
  4224. },
  4225. {
  4226. .name = "cgroup_css_links",
  4227. .read_seq_string = cgroup_css_links_read,
  4228. },
  4229. {
  4230. .name = "releasable",
  4231. .read_u64 = releasable_read,
  4232. },
  4233. };
  4234. static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
  4235. {
  4236. return cgroup_add_files(cont, ss, debug_files,
  4237. ARRAY_SIZE(debug_files));
  4238. }
  4239. struct cgroup_subsys debug_subsys = {
  4240. .name = "debug",
  4241. .create = debug_create,
  4242. .destroy = debug_destroy,
  4243. .populate = debug_populate,
  4244. .subsys_id = debug_subsys_id,
  4245. };
  4246. #endif /* CONFIG_CGROUP_DEBUG */