cgroup.c 138 KB

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