cgroup.c 139 KB

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