cgroup.c 144 KB

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