cgroup.c 162 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/cgroup.h>
  30. #include <linux/cred.h>
  31. #include <linux/ctype.h>
  32. #include <linux/errno.h>
  33. #include <linux/init_task.h>
  34. #include <linux/kernel.h>
  35. #include <linux/list.h>
  36. #include <linux/magic.h>
  37. #include <linux/mm.h>
  38. #include <linux/mutex.h>
  39. #include <linux/mount.h>
  40. #include <linux/pagemap.h>
  41. #include <linux/proc_fs.h>
  42. #include <linux/rcupdate.h>
  43. #include <linux/sched.h>
  44. #include <linux/slab.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/percpu-rwsem.h>
  47. #include <linux/string.h>
  48. #include <linux/sort.h>
  49. #include <linux/kmod.h>
  50. #include <linux/delayacct.h>
  51. #include <linux/cgroupstats.h>
  52. #include <linux/hashtable.h>
  53. #include <linux/pid_namespace.h>
  54. #include <linux/idr.h>
  55. #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
  56. #include <linux/kthread.h>
  57. #include <linux/delay.h>
  58. #include <linux/atomic.h>
  59. #include <net/sock.h>
  60. /*
  61. * pidlists linger the following amount before being destroyed. The goal
  62. * is avoiding frequent destruction in the middle of consecutive read calls
  63. * Expiring in the middle is a performance problem not a correctness one.
  64. * 1 sec should be enough.
  65. */
  66. #define CGROUP_PIDLIST_DESTROY_DELAY HZ
  67. #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
  68. MAX_CFTYPE_NAME + 2)
  69. /*
  70. * cgroup_mutex is the master lock. Any modification to cgroup or its
  71. * hierarchy must be performed while holding it.
  72. *
  73. * css_set_lock protects task->cgroups pointer, the list of css_set
  74. * objects, and the chain of tasks off each css_set.
  75. *
  76. * These locks are exported if CONFIG_PROVE_RCU so that accessors in
  77. * cgroup.h can use them for lockdep annotations.
  78. */
  79. #ifdef CONFIG_PROVE_RCU
  80. DEFINE_MUTEX(cgroup_mutex);
  81. DEFINE_SPINLOCK(css_set_lock);
  82. EXPORT_SYMBOL_GPL(cgroup_mutex);
  83. EXPORT_SYMBOL_GPL(css_set_lock);
  84. #else
  85. static DEFINE_MUTEX(cgroup_mutex);
  86. static DEFINE_SPINLOCK(css_set_lock);
  87. #endif
  88. /*
  89. * Protects cgroup_idr and css_idr so that IDs can be released without
  90. * grabbing cgroup_mutex.
  91. */
  92. static DEFINE_SPINLOCK(cgroup_idr_lock);
  93. /*
  94. * Protects cgroup_file->kn for !self csses. It synchronizes notifications
  95. * against file removal/re-creation across css hiding.
  96. */
  97. static DEFINE_SPINLOCK(cgroup_file_kn_lock);
  98. /*
  99. * Protects cgroup_subsys->release_agent_path. Modifying it also requires
  100. * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
  101. */
  102. static DEFINE_SPINLOCK(release_agent_path_lock);
  103. struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
  104. #define cgroup_assert_mutex_or_rcu_locked() \
  105. RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  106. !lockdep_is_held(&cgroup_mutex), \
  107. "cgroup_mutex or RCU read lock required");
  108. /*
  109. * cgroup destruction makes heavy use of work items and there can be a lot
  110. * of concurrent destructions. Use a separate workqueue so that cgroup
  111. * destruction work items don't end up filling up max_active of system_wq
  112. * which may lead to deadlock.
  113. */
  114. static struct workqueue_struct *cgroup_destroy_wq;
  115. /*
  116. * pidlist destructions need to be flushed on cgroup destruction. Use a
  117. * separate workqueue as flush domain.
  118. */
  119. static struct workqueue_struct *cgroup_pidlist_destroy_wq;
  120. /* generate an array of cgroup subsystem pointers */
  121. #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
  122. static struct cgroup_subsys *cgroup_subsys[] = {
  123. #include <linux/cgroup_subsys.h>
  124. };
  125. #undef SUBSYS
  126. /* array of cgroup subsystem names */
  127. #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
  128. static const char *cgroup_subsys_name[] = {
  129. #include <linux/cgroup_subsys.h>
  130. };
  131. #undef SUBSYS
  132. /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
  133. #define SUBSYS(_x) \
  134. DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
  135. DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
  136. EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
  137. EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
  138. #include <linux/cgroup_subsys.h>
  139. #undef SUBSYS
  140. #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
  141. static struct static_key_true *cgroup_subsys_enabled_key[] = {
  142. #include <linux/cgroup_subsys.h>
  143. };
  144. #undef SUBSYS
  145. #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
  146. static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
  147. #include <linux/cgroup_subsys.h>
  148. };
  149. #undef SUBSYS
  150. /*
  151. * The default hierarchy, reserved for the subsystems that are otherwise
  152. * unattached - it never has more than a single cgroup, and all tasks are
  153. * part of that cgroup.
  154. */
  155. struct cgroup_root cgrp_dfl_root;
  156. EXPORT_SYMBOL_GPL(cgrp_dfl_root);
  157. /*
  158. * The default hierarchy always exists but is hidden until mounted for the
  159. * first time. This is for backward compatibility.
  160. */
  161. static bool cgrp_dfl_root_visible;
  162. /* some controllers are not supported in the default hierarchy */
  163. static unsigned long cgrp_dfl_root_inhibit_ss_mask;
  164. /* The list of hierarchy roots */
  165. static LIST_HEAD(cgroup_roots);
  166. static int cgroup_root_count;
  167. /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
  168. static DEFINE_IDR(cgroup_hierarchy_idr);
  169. /*
  170. * Assign a monotonically increasing serial number to csses. It guarantees
  171. * cgroups with bigger numbers are newer than those with smaller numbers.
  172. * Also, as csses are always appended to the parent's ->children list, it
  173. * guarantees that sibling csses are always sorted in the ascending serial
  174. * number order on the list. Protected by cgroup_mutex.
  175. */
  176. static u64 css_serial_nr_next = 1;
  177. /*
  178. * These bitmask flags indicate whether tasks in the fork and exit paths have
  179. * fork/exit handlers to call. This avoids us having to do extra work in the
  180. * fork/exit path to check which subsystems have fork/exit callbacks.
  181. */
  182. static unsigned long have_fork_callback __read_mostly;
  183. static unsigned long have_exit_callback __read_mostly;
  184. static unsigned long have_free_callback __read_mostly;
  185. /* Ditto for the can_fork callback. */
  186. static unsigned long have_canfork_callback __read_mostly;
  187. static struct file_system_type cgroup2_fs_type;
  188. static struct cftype cgroup_dfl_base_files[];
  189. static struct cftype cgroup_legacy_base_files[];
  190. static int rebind_subsystems(struct cgroup_root *dst_root,
  191. unsigned long ss_mask);
  192. static void css_task_iter_advance(struct css_task_iter *it);
  193. static int cgroup_destroy_locked(struct cgroup *cgrp);
  194. static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
  195. bool visible);
  196. static void css_release(struct percpu_ref *ref);
  197. static void kill_css(struct cgroup_subsys_state *css);
  198. static int cgroup_addrm_files(struct cgroup_subsys_state *css,
  199. struct cgroup *cgrp, struct cftype cfts[],
  200. bool is_add);
  201. /**
  202. * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
  203. * @ssid: subsys ID of interest
  204. *
  205. * cgroup_subsys_enabled() can only be used with literal subsys names which
  206. * is fine for individual subsystems but unsuitable for cgroup core. This
  207. * is slower static_key_enabled() based test indexed by @ssid.
  208. */
  209. static bool cgroup_ssid_enabled(int ssid)
  210. {
  211. return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
  212. }
  213. /**
  214. * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
  215. * @cgrp: the cgroup of interest
  216. *
  217. * The default hierarchy is the v2 interface of cgroup and this function
  218. * can be used to test whether a cgroup is on the default hierarchy for
  219. * cases where a subsystem should behave differnetly depending on the
  220. * interface version.
  221. *
  222. * The set of behaviors which change on the default hierarchy are still
  223. * being determined and the mount option is prefixed with __DEVEL__.
  224. *
  225. * List of changed behaviors:
  226. *
  227. * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
  228. * and "name" are disallowed.
  229. *
  230. * - When mounting an existing superblock, mount options should match.
  231. *
  232. * - Remount is disallowed.
  233. *
  234. * - rename(2) is disallowed.
  235. *
  236. * - "tasks" is removed. Everything should be at process granularity. Use
  237. * "cgroup.procs" instead.
  238. *
  239. * - "cgroup.procs" is not sorted. pids will be unique unless they got
  240. * recycled inbetween reads.
  241. *
  242. * - "release_agent" and "notify_on_release" are removed. Replacement
  243. * notification mechanism will be implemented.
  244. *
  245. * - "cgroup.clone_children" is removed.
  246. *
  247. * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
  248. * and its descendants contain no task; otherwise, 1. The file also
  249. * generates kernfs notification which can be monitored through poll and
  250. * [di]notify when the value of the file changes.
  251. *
  252. * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
  253. * take masks of ancestors with non-empty cpus/mems, instead of being
  254. * moved to an ancestor.
  255. *
  256. * - cpuset: a task can be moved into an empty cpuset, and again it takes
  257. * masks of ancestors.
  258. *
  259. * - memcg: use_hierarchy is on by default and the cgroup file for the flag
  260. * is not created.
  261. *
  262. * - blkcg: blk-throttle becomes properly hierarchical.
  263. *
  264. * - debug: disallowed on the default hierarchy.
  265. */
  266. static bool cgroup_on_dfl(const struct cgroup *cgrp)
  267. {
  268. return cgrp->root == &cgrp_dfl_root;
  269. }
  270. /* IDR wrappers which synchronize using cgroup_idr_lock */
  271. static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
  272. gfp_t gfp_mask)
  273. {
  274. int ret;
  275. idr_preload(gfp_mask);
  276. spin_lock_bh(&cgroup_idr_lock);
  277. ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
  278. spin_unlock_bh(&cgroup_idr_lock);
  279. idr_preload_end();
  280. return ret;
  281. }
  282. static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
  283. {
  284. void *ret;
  285. spin_lock_bh(&cgroup_idr_lock);
  286. ret = idr_replace(idr, ptr, id);
  287. spin_unlock_bh(&cgroup_idr_lock);
  288. return ret;
  289. }
  290. static void cgroup_idr_remove(struct idr *idr, int id)
  291. {
  292. spin_lock_bh(&cgroup_idr_lock);
  293. idr_remove(idr, id);
  294. spin_unlock_bh(&cgroup_idr_lock);
  295. }
  296. static struct cgroup *cgroup_parent(struct cgroup *cgrp)
  297. {
  298. struct cgroup_subsys_state *parent_css = cgrp->self.parent;
  299. if (parent_css)
  300. return container_of(parent_css, struct cgroup, self);
  301. return NULL;
  302. }
  303. /**
  304. * cgroup_css - obtain a cgroup's css for the specified subsystem
  305. * @cgrp: the cgroup of interest
  306. * @ss: the subsystem of interest (%NULL returns @cgrp->self)
  307. *
  308. * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
  309. * function must be called either under cgroup_mutex or rcu_read_lock() and
  310. * the caller is responsible for pinning the returned css if it wants to
  311. * keep accessing it outside the said locks. This function may return
  312. * %NULL if @cgrp doesn't have @subsys_id enabled.
  313. */
  314. static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
  315. struct cgroup_subsys *ss)
  316. {
  317. if (ss)
  318. return rcu_dereference_check(cgrp->subsys[ss->id],
  319. lockdep_is_held(&cgroup_mutex));
  320. else
  321. return &cgrp->self;
  322. }
  323. /**
  324. * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
  325. * @cgrp: the cgroup of interest
  326. * @ss: the subsystem of interest (%NULL returns @cgrp->self)
  327. *
  328. * Similar to cgroup_css() but returns the effective css, which is defined
  329. * as the matching css of the nearest ancestor including self which has @ss
  330. * enabled. If @ss is associated with the hierarchy @cgrp is on, this
  331. * function is guaranteed to return non-NULL css.
  332. */
  333. static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
  334. struct cgroup_subsys *ss)
  335. {
  336. lockdep_assert_held(&cgroup_mutex);
  337. if (!ss)
  338. return &cgrp->self;
  339. if (!(cgrp->root->subsys_mask & (1 << ss->id)))
  340. return NULL;
  341. /*
  342. * This function is used while updating css associations and thus
  343. * can't test the csses directly. Use ->child_subsys_mask.
  344. */
  345. while (cgroup_parent(cgrp) &&
  346. !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id)))
  347. cgrp = cgroup_parent(cgrp);
  348. return cgroup_css(cgrp, ss);
  349. }
  350. /**
  351. * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
  352. * @cgrp: the cgroup of interest
  353. * @ss: the subsystem of interest
  354. *
  355. * Find and get the effective css of @cgrp for @ss. The effective css is
  356. * defined as the matching css of the nearest ancestor including self which
  357. * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
  358. * the root css is returned, so this function always returns a valid css.
  359. * The returned css must be put using css_put().
  360. */
  361. struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
  362. struct cgroup_subsys *ss)
  363. {
  364. struct cgroup_subsys_state *css;
  365. rcu_read_lock();
  366. do {
  367. css = cgroup_css(cgrp, ss);
  368. if (css && css_tryget_online(css))
  369. goto out_unlock;
  370. cgrp = cgroup_parent(cgrp);
  371. } while (cgrp);
  372. css = init_css_set.subsys[ss->id];
  373. css_get(css);
  374. out_unlock:
  375. rcu_read_unlock();
  376. return css;
  377. }
  378. /* convenient tests for these bits */
  379. static inline bool cgroup_is_dead(const struct cgroup *cgrp)
  380. {
  381. return !(cgrp->self.flags & CSS_ONLINE);
  382. }
  383. static void cgroup_get(struct cgroup *cgrp)
  384. {
  385. WARN_ON_ONCE(cgroup_is_dead(cgrp));
  386. css_get(&cgrp->self);
  387. }
  388. static bool cgroup_tryget(struct cgroup *cgrp)
  389. {
  390. return css_tryget(&cgrp->self);
  391. }
  392. struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
  393. {
  394. struct cgroup *cgrp = of->kn->parent->priv;
  395. struct cftype *cft = of_cft(of);
  396. /*
  397. * This is open and unprotected implementation of cgroup_css().
  398. * seq_css() is only called from a kernfs file operation which has
  399. * an active reference on the file. Because all the subsystem
  400. * files are drained before a css is disassociated with a cgroup,
  401. * the matching css from the cgroup's subsys table is guaranteed to
  402. * be and stay valid until the enclosing operation is complete.
  403. */
  404. if (cft->ss)
  405. return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
  406. else
  407. return &cgrp->self;
  408. }
  409. EXPORT_SYMBOL_GPL(of_css);
  410. static int notify_on_release(const struct cgroup *cgrp)
  411. {
  412. return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  413. }
  414. /**
  415. * for_each_css - iterate all css's of a cgroup
  416. * @css: the iteration cursor
  417. * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
  418. * @cgrp: the target cgroup to iterate css's of
  419. *
  420. * Should be called under cgroup_[tree_]mutex.
  421. */
  422. #define for_each_css(css, ssid, cgrp) \
  423. for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
  424. if (!((css) = rcu_dereference_check( \
  425. (cgrp)->subsys[(ssid)], \
  426. lockdep_is_held(&cgroup_mutex)))) { } \
  427. else
  428. /**
  429. * for_each_e_css - iterate all effective css's of a cgroup
  430. * @css: the iteration cursor
  431. * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
  432. * @cgrp: the target cgroup to iterate css's of
  433. *
  434. * Should be called under cgroup_[tree_]mutex.
  435. */
  436. #define for_each_e_css(css, ssid, cgrp) \
  437. for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
  438. if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
  439. ; \
  440. else
  441. /**
  442. * for_each_subsys - iterate all enabled cgroup subsystems
  443. * @ss: the iteration cursor
  444. * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
  445. */
  446. #define for_each_subsys(ss, ssid) \
  447. for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
  448. (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
  449. /**
  450. * for_each_subsys_which - filter for_each_subsys with a bitmask
  451. * @ss: the iteration cursor
  452. * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
  453. * @ss_maskp: a pointer to the bitmask
  454. *
  455. * The block will only run for cases where the ssid-th bit (1 << ssid) of
  456. * mask is set to 1.
  457. */
  458. #define for_each_subsys_which(ss, ssid, ss_maskp) \
  459. if (!CGROUP_SUBSYS_COUNT) /* to avoid spurious gcc warning */ \
  460. (ssid) = 0; \
  461. else \
  462. for_each_set_bit(ssid, ss_maskp, CGROUP_SUBSYS_COUNT) \
  463. if (((ss) = cgroup_subsys[ssid]) && false) \
  464. break; \
  465. else
  466. /* iterate across the hierarchies */
  467. #define for_each_root(root) \
  468. list_for_each_entry((root), &cgroup_roots, root_list)
  469. /* iterate over child cgrps, lock should be held throughout iteration */
  470. #define cgroup_for_each_live_child(child, cgrp) \
  471. list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
  472. if (({ lockdep_assert_held(&cgroup_mutex); \
  473. cgroup_is_dead(child); })) \
  474. ; \
  475. else
  476. static void cgroup_release_agent(struct work_struct *work);
  477. static void check_for_release(struct cgroup *cgrp);
  478. /*
  479. * A cgroup can be associated with multiple css_sets as different tasks may
  480. * belong to different cgroups on different hierarchies. In the other
  481. * direction, a css_set is naturally associated with multiple cgroups.
  482. * This M:N relationship is represented by the following link structure
  483. * which exists for each association and allows traversing the associations
  484. * from both sides.
  485. */
  486. struct cgrp_cset_link {
  487. /* the cgroup and css_set this link associates */
  488. struct cgroup *cgrp;
  489. struct css_set *cset;
  490. /* list of cgrp_cset_links anchored at cgrp->cset_links */
  491. struct list_head cset_link;
  492. /* list of cgrp_cset_links anchored at css_set->cgrp_links */
  493. struct list_head cgrp_link;
  494. };
  495. /*
  496. * The default css_set - used by init and its children prior to any
  497. * hierarchies being mounted. It contains a pointer to the root state
  498. * for each subsystem. Also used to anchor the list of css_sets. Not
  499. * reference-counted, to improve performance when child cgroups
  500. * haven't been created.
  501. */
  502. struct css_set init_css_set = {
  503. .refcount = ATOMIC_INIT(1),
  504. .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
  505. .tasks = LIST_HEAD_INIT(init_css_set.tasks),
  506. .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
  507. .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
  508. .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
  509. .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
  510. };
  511. static int css_set_count = 1; /* 1 for init_css_set */
  512. /**
  513. * css_set_populated - does a css_set contain any tasks?
  514. * @cset: target css_set
  515. */
  516. static bool css_set_populated(struct css_set *cset)
  517. {
  518. lockdep_assert_held(&css_set_lock);
  519. return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
  520. }
  521. /**
  522. * cgroup_update_populated - updated populated count of a cgroup
  523. * @cgrp: the target cgroup
  524. * @populated: inc or dec populated count
  525. *
  526. * One of the css_sets associated with @cgrp is either getting its first
  527. * task or losing the last. Update @cgrp->populated_cnt accordingly. The
  528. * count is propagated towards root so that a given cgroup's populated_cnt
  529. * is zero iff the cgroup and all its descendants don't contain any tasks.
  530. *
  531. * @cgrp's interface file "cgroup.populated" is zero if
  532. * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
  533. * changes from or to zero, userland is notified that the content of the
  534. * interface file has changed. This can be used to detect when @cgrp and
  535. * its descendants become populated or empty.
  536. */
  537. static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
  538. {
  539. lockdep_assert_held(&css_set_lock);
  540. do {
  541. bool trigger;
  542. if (populated)
  543. trigger = !cgrp->populated_cnt++;
  544. else
  545. trigger = !--cgrp->populated_cnt;
  546. if (!trigger)
  547. break;
  548. check_for_release(cgrp);
  549. cgroup_file_notify(&cgrp->events_file);
  550. cgrp = cgroup_parent(cgrp);
  551. } while (cgrp);
  552. }
  553. /**
  554. * css_set_update_populated - update populated state of a css_set
  555. * @cset: target css_set
  556. * @populated: whether @cset is populated or depopulated
  557. *
  558. * @cset is either getting the first task or losing the last. Update the
  559. * ->populated_cnt of all associated cgroups accordingly.
  560. */
  561. static void css_set_update_populated(struct css_set *cset, bool populated)
  562. {
  563. struct cgrp_cset_link *link;
  564. lockdep_assert_held(&css_set_lock);
  565. list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
  566. cgroup_update_populated(link->cgrp, populated);
  567. }
  568. /**
  569. * css_set_move_task - move a task from one css_set to another
  570. * @task: task being moved
  571. * @from_cset: css_set @task currently belongs to (may be NULL)
  572. * @to_cset: new css_set @task is being moved to (may be NULL)
  573. * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
  574. *
  575. * Move @task from @from_cset to @to_cset. If @task didn't belong to any
  576. * css_set, @from_cset can be NULL. If @task is being disassociated
  577. * instead of moved, @to_cset can be NULL.
  578. *
  579. * This function automatically handles populated_cnt updates and
  580. * css_task_iter adjustments but the caller is responsible for managing
  581. * @from_cset and @to_cset's reference counts.
  582. */
  583. static void css_set_move_task(struct task_struct *task,
  584. struct css_set *from_cset, struct css_set *to_cset,
  585. bool use_mg_tasks)
  586. {
  587. lockdep_assert_held(&css_set_lock);
  588. if (from_cset) {
  589. struct css_task_iter *it, *pos;
  590. WARN_ON_ONCE(list_empty(&task->cg_list));
  591. /*
  592. * @task is leaving, advance task iterators which are
  593. * pointing to it so that they can resume at the next
  594. * position. Advancing an iterator might remove it from
  595. * the list, use safe walk. See css_task_iter_advance*()
  596. * for details.
  597. */
  598. list_for_each_entry_safe(it, pos, &from_cset->task_iters,
  599. iters_node)
  600. if (it->task_pos == &task->cg_list)
  601. css_task_iter_advance(it);
  602. list_del_init(&task->cg_list);
  603. if (!css_set_populated(from_cset))
  604. css_set_update_populated(from_cset, false);
  605. } else {
  606. WARN_ON_ONCE(!list_empty(&task->cg_list));
  607. }
  608. if (to_cset) {
  609. /*
  610. * We are synchronized through cgroup_threadgroup_rwsem
  611. * against PF_EXITING setting such that we can't race
  612. * against cgroup_exit() changing the css_set to
  613. * init_css_set and dropping the old one.
  614. */
  615. WARN_ON_ONCE(task->flags & PF_EXITING);
  616. if (!css_set_populated(to_cset))
  617. css_set_update_populated(to_cset, true);
  618. rcu_assign_pointer(task->cgroups, to_cset);
  619. list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
  620. &to_cset->tasks);
  621. }
  622. }
  623. /*
  624. * hash table for cgroup groups. This improves the performance to find
  625. * an existing css_set. This hash doesn't (currently) take into
  626. * account cgroups in empty hierarchies.
  627. */
  628. #define CSS_SET_HASH_BITS 7
  629. static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
  630. static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
  631. {
  632. unsigned long key = 0UL;
  633. struct cgroup_subsys *ss;
  634. int i;
  635. for_each_subsys(ss, i)
  636. key += (unsigned long)css[i];
  637. key = (key >> 16) ^ key;
  638. return key;
  639. }
  640. static void put_css_set_locked(struct css_set *cset)
  641. {
  642. struct cgrp_cset_link *link, *tmp_link;
  643. struct cgroup_subsys *ss;
  644. int ssid;
  645. lockdep_assert_held(&css_set_lock);
  646. if (!atomic_dec_and_test(&cset->refcount))
  647. return;
  648. /* This css_set is dead. unlink it and release cgroup and css refs */
  649. for_each_subsys(ss, ssid) {
  650. list_del(&cset->e_cset_node[ssid]);
  651. css_put(cset->subsys[ssid]);
  652. }
  653. hash_del(&cset->hlist);
  654. css_set_count--;
  655. list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
  656. list_del(&link->cset_link);
  657. list_del(&link->cgrp_link);
  658. if (cgroup_parent(link->cgrp))
  659. cgroup_put(link->cgrp);
  660. kfree(link);
  661. }
  662. kfree_rcu(cset, rcu_head);
  663. }
  664. static void put_css_set(struct css_set *cset)
  665. {
  666. /*
  667. * Ensure that the refcount doesn't hit zero while any readers
  668. * can see it. Similar to atomic_dec_and_lock(), but for an
  669. * rwlock
  670. */
  671. if (atomic_add_unless(&cset->refcount, -1, 1))
  672. return;
  673. spin_lock_bh(&css_set_lock);
  674. put_css_set_locked(cset);
  675. spin_unlock_bh(&css_set_lock);
  676. }
  677. /*
  678. * refcounted get/put for css_set objects
  679. */
  680. static inline void get_css_set(struct css_set *cset)
  681. {
  682. atomic_inc(&cset->refcount);
  683. }
  684. /**
  685. * compare_css_sets - helper function for find_existing_css_set().
  686. * @cset: candidate css_set being tested
  687. * @old_cset: existing css_set for a task
  688. * @new_cgrp: cgroup that's being entered by the task
  689. * @template: desired set of css pointers in css_set (pre-calculated)
  690. *
  691. * Returns true if "cset" matches "old_cset" except for the hierarchy
  692. * which "new_cgrp" belongs to, for which it should match "new_cgrp".
  693. */
  694. static bool compare_css_sets(struct css_set *cset,
  695. struct css_set *old_cset,
  696. struct cgroup *new_cgrp,
  697. struct cgroup_subsys_state *template[])
  698. {
  699. struct list_head *l1, *l2;
  700. /*
  701. * On the default hierarchy, there can be csets which are
  702. * associated with the same set of cgroups but different csses.
  703. * Let's first ensure that csses match.
  704. */
  705. if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
  706. return false;
  707. /*
  708. * Compare cgroup pointers in order to distinguish between
  709. * different cgroups in hierarchies. As different cgroups may
  710. * share the same effective css, this comparison is always
  711. * necessary.
  712. */
  713. l1 = &cset->cgrp_links;
  714. l2 = &old_cset->cgrp_links;
  715. while (1) {
  716. struct cgrp_cset_link *link1, *link2;
  717. struct cgroup *cgrp1, *cgrp2;
  718. l1 = l1->next;
  719. l2 = l2->next;
  720. /* See if we reached the end - both lists are equal length. */
  721. if (l1 == &cset->cgrp_links) {
  722. BUG_ON(l2 != &old_cset->cgrp_links);
  723. break;
  724. } else {
  725. BUG_ON(l2 == &old_cset->cgrp_links);
  726. }
  727. /* Locate the cgroups associated with these links. */
  728. link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
  729. link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
  730. cgrp1 = link1->cgrp;
  731. cgrp2 = link2->cgrp;
  732. /* Hierarchies should be linked in the same order. */
  733. BUG_ON(cgrp1->root != cgrp2->root);
  734. /*
  735. * If this hierarchy is the hierarchy of the cgroup
  736. * that's changing, then we need to check that this
  737. * css_set points to the new cgroup; if it's any other
  738. * hierarchy, then this css_set should point to the
  739. * same cgroup as the old css_set.
  740. */
  741. if (cgrp1->root == new_cgrp->root) {
  742. if (cgrp1 != new_cgrp)
  743. return false;
  744. } else {
  745. if (cgrp1 != cgrp2)
  746. return false;
  747. }
  748. }
  749. return true;
  750. }
  751. /**
  752. * find_existing_css_set - init css array and find the matching css_set
  753. * @old_cset: the css_set that we're using before the cgroup transition
  754. * @cgrp: the cgroup that we're moving into
  755. * @template: out param for the new set of csses, should be clear on entry
  756. */
  757. static struct css_set *find_existing_css_set(struct css_set *old_cset,
  758. struct cgroup *cgrp,
  759. struct cgroup_subsys_state *template[])
  760. {
  761. struct cgroup_root *root = cgrp->root;
  762. struct cgroup_subsys *ss;
  763. struct css_set *cset;
  764. unsigned long key;
  765. int i;
  766. /*
  767. * Build the set of subsystem state objects that we want to see in the
  768. * new css_set. while subsystems can change globally, the entries here
  769. * won't change, so no need for locking.
  770. */
  771. for_each_subsys(ss, i) {
  772. if (root->subsys_mask & (1UL << i)) {
  773. /*
  774. * @ss is in this hierarchy, so we want the
  775. * effective css from @cgrp.
  776. */
  777. template[i] = cgroup_e_css(cgrp, ss);
  778. } else {
  779. /*
  780. * @ss is not in this hierarchy, so we don't want
  781. * to change the css.
  782. */
  783. template[i] = old_cset->subsys[i];
  784. }
  785. }
  786. key = css_set_hash(template);
  787. hash_for_each_possible(css_set_table, cset, hlist, key) {
  788. if (!compare_css_sets(cset, old_cset, cgrp, template))
  789. continue;
  790. /* This css_set matches what we need */
  791. return cset;
  792. }
  793. /* No existing cgroup group matched */
  794. return NULL;
  795. }
  796. static void free_cgrp_cset_links(struct list_head *links_to_free)
  797. {
  798. struct cgrp_cset_link *link, *tmp_link;
  799. list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
  800. list_del(&link->cset_link);
  801. kfree(link);
  802. }
  803. }
  804. /**
  805. * allocate_cgrp_cset_links - allocate cgrp_cset_links
  806. * @count: the number of links to allocate
  807. * @tmp_links: list_head the allocated links are put on
  808. *
  809. * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
  810. * through ->cset_link. Returns 0 on success or -errno.
  811. */
  812. static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
  813. {
  814. struct cgrp_cset_link *link;
  815. int i;
  816. INIT_LIST_HEAD(tmp_links);
  817. for (i = 0; i < count; i++) {
  818. link = kzalloc(sizeof(*link), GFP_KERNEL);
  819. if (!link) {
  820. free_cgrp_cset_links(tmp_links);
  821. return -ENOMEM;
  822. }
  823. list_add(&link->cset_link, tmp_links);
  824. }
  825. return 0;
  826. }
  827. /**
  828. * link_css_set - a helper function to link a css_set to a cgroup
  829. * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
  830. * @cset: the css_set to be linked
  831. * @cgrp: the destination cgroup
  832. */
  833. static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
  834. struct cgroup *cgrp)
  835. {
  836. struct cgrp_cset_link *link;
  837. BUG_ON(list_empty(tmp_links));
  838. if (cgroup_on_dfl(cgrp))
  839. cset->dfl_cgrp = cgrp;
  840. link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
  841. link->cset = cset;
  842. link->cgrp = cgrp;
  843. /*
  844. * Always add links to the tail of the lists so that the lists are
  845. * in choronological order.
  846. */
  847. list_move_tail(&link->cset_link, &cgrp->cset_links);
  848. list_add_tail(&link->cgrp_link, &cset->cgrp_links);
  849. if (cgroup_parent(cgrp))
  850. cgroup_get(cgrp);
  851. }
  852. /**
  853. * find_css_set - return a new css_set with one cgroup updated
  854. * @old_cset: the baseline css_set
  855. * @cgrp: the cgroup to be updated
  856. *
  857. * Return a new css_set that's equivalent to @old_cset, but with @cgrp
  858. * substituted into the appropriate hierarchy.
  859. */
  860. static struct css_set *find_css_set(struct css_set *old_cset,
  861. struct cgroup *cgrp)
  862. {
  863. struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
  864. struct css_set *cset;
  865. struct list_head tmp_links;
  866. struct cgrp_cset_link *link;
  867. struct cgroup_subsys *ss;
  868. unsigned long key;
  869. int ssid;
  870. lockdep_assert_held(&cgroup_mutex);
  871. /* First see if we already have a cgroup group that matches
  872. * the desired set */
  873. spin_lock_bh(&css_set_lock);
  874. cset = find_existing_css_set(old_cset, cgrp, template);
  875. if (cset)
  876. get_css_set(cset);
  877. spin_unlock_bh(&css_set_lock);
  878. if (cset)
  879. return cset;
  880. cset = kzalloc(sizeof(*cset), GFP_KERNEL);
  881. if (!cset)
  882. return NULL;
  883. /* Allocate all the cgrp_cset_link objects that we'll need */
  884. if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
  885. kfree(cset);
  886. return NULL;
  887. }
  888. atomic_set(&cset->refcount, 1);
  889. INIT_LIST_HEAD(&cset->cgrp_links);
  890. INIT_LIST_HEAD(&cset->tasks);
  891. INIT_LIST_HEAD(&cset->mg_tasks);
  892. INIT_LIST_HEAD(&cset->mg_preload_node);
  893. INIT_LIST_HEAD(&cset->mg_node);
  894. INIT_LIST_HEAD(&cset->task_iters);
  895. INIT_HLIST_NODE(&cset->hlist);
  896. /* Copy the set of subsystem state objects generated in
  897. * find_existing_css_set() */
  898. memcpy(cset->subsys, template, sizeof(cset->subsys));
  899. spin_lock_bh(&css_set_lock);
  900. /* Add reference counts and links from the new css_set. */
  901. list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
  902. struct cgroup *c = link->cgrp;
  903. if (c->root == cgrp->root)
  904. c = cgrp;
  905. link_css_set(&tmp_links, cset, c);
  906. }
  907. BUG_ON(!list_empty(&tmp_links));
  908. css_set_count++;
  909. /* Add @cset to the hash table */
  910. key = css_set_hash(cset->subsys);
  911. hash_add(css_set_table, &cset->hlist, key);
  912. for_each_subsys(ss, ssid) {
  913. struct cgroup_subsys_state *css = cset->subsys[ssid];
  914. list_add_tail(&cset->e_cset_node[ssid],
  915. &css->cgroup->e_csets[ssid]);
  916. css_get(css);
  917. }
  918. spin_unlock_bh(&css_set_lock);
  919. return cset;
  920. }
  921. static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
  922. {
  923. struct cgroup *root_cgrp = kf_root->kn->priv;
  924. return root_cgrp->root;
  925. }
  926. static int cgroup_init_root_id(struct cgroup_root *root)
  927. {
  928. int id;
  929. lockdep_assert_held(&cgroup_mutex);
  930. id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
  931. if (id < 0)
  932. return id;
  933. root->hierarchy_id = id;
  934. return 0;
  935. }
  936. static void cgroup_exit_root_id(struct cgroup_root *root)
  937. {
  938. lockdep_assert_held(&cgroup_mutex);
  939. if (root->hierarchy_id) {
  940. idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
  941. root->hierarchy_id = 0;
  942. }
  943. }
  944. static void cgroup_free_root(struct cgroup_root *root)
  945. {
  946. if (root) {
  947. /* hierarchy ID should already have been released */
  948. WARN_ON_ONCE(root->hierarchy_id);
  949. idr_destroy(&root->cgroup_idr);
  950. kfree(root);
  951. }
  952. }
  953. static void cgroup_destroy_root(struct cgroup_root *root)
  954. {
  955. struct cgroup *cgrp = &root->cgrp;
  956. struct cgrp_cset_link *link, *tmp_link;
  957. mutex_lock(&cgroup_mutex);
  958. BUG_ON(atomic_read(&root->nr_cgrps));
  959. BUG_ON(!list_empty(&cgrp->self.children));
  960. /* Rebind all subsystems back to the default hierarchy */
  961. rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
  962. /*
  963. * Release all the links from cset_links to this hierarchy's
  964. * root cgroup
  965. */
  966. spin_lock_bh(&css_set_lock);
  967. list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
  968. list_del(&link->cset_link);
  969. list_del(&link->cgrp_link);
  970. kfree(link);
  971. }
  972. spin_unlock_bh(&css_set_lock);
  973. if (!list_empty(&root->root_list)) {
  974. list_del(&root->root_list);
  975. cgroup_root_count--;
  976. }
  977. cgroup_exit_root_id(root);
  978. mutex_unlock(&cgroup_mutex);
  979. kernfs_destroy_root(root->kf_root);
  980. cgroup_free_root(root);
  981. }
  982. /* look up cgroup associated with given css_set on the specified hierarchy */
  983. static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
  984. struct cgroup_root *root)
  985. {
  986. struct cgroup *res = NULL;
  987. lockdep_assert_held(&cgroup_mutex);
  988. lockdep_assert_held(&css_set_lock);
  989. if (cset == &init_css_set) {
  990. res = &root->cgrp;
  991. } else {
  992. struct cgrp_cset_link *link;
  993. list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  994. struct cgroup *c = link->cgrp;
  995. if (c->root == root) {
  996. res = c;
  997. break;
  998. }
  999. }
  1000. }
  1001. BUG_ON(!res);
  1002. return res;
  1003. }
  1004. /*
  1005. * Return the cgroup for "task" from the given hierarchy. Must be
  1006. * called with cgroup_mutex and css_set_lock held.
  1007. */
  1008. static struct cgroup *task_cgroup_from_root(struct task_struct *task,
  1009. struct cgroup_root *root)
  1010. {
  1011. /*
  1012. * No need to lock the task - since we hold cgroup_mutex the
  1013. * task can't change groups, so the only thing that can happen
  1014. * is that it exits and its css is set back to init_css_set.
  1015. */
  1016. return cset_cgroup_from_root(task_css_set(task), root);
  1017. }
  1018. /*
  1019. * A task must hold cgroup_mutex to modify cgroups.
  1020. *
  1021. * Any task can increment and decrement the count field without lock.
  1022. * So in general, code holding cgroup_mutex can't rely on the count
  1023. * field not changing. However, if the count goes to zero, then only
  1024. * cgroup_attach_task() can increment it again. Because a count of zero
  1025. * means that no tasks are currently attached, therefore there is no
  1026. * way a task attached to that cgroup can fork (the other way to
  1027. * increment the count). So code holding cgroup_mutex can safely
  1028. * assume that if the count is zero, it will stay zero. Similarly, if
  1029. * a task holds cgroup_mutex on a cgroup with zero count, it
  1030. * knows that the cgroup won't be removed, as cgroup_rmdir()
  1031. * needs that mutex.
  1032. *
  1033. * A cgroup can only be deleted if both its 'count' of using tasks
  1034. * is zero, and its list of 'children' cgroups is empty. Since all
  1035. * tasks in the system use _some_ cgroup, and since there is always at
  1036. * least one task in the system (init, pid == 1), therefore, root cgroup
  1037. * always has either children cgroups and/or using tasks. So we don't
  1038. * need a special hack to ensure that root cgroup cannot be deleted.
  1039. *
  1040. * P.S. One more locking exception. RCU is used to guard the
  1041. * update of a tasks cgroup pointer by cgroup_attach_task()
  1042. */
  1043. static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
  1044. static const struct file_operations proc_cgroupstats_operations;
  1045. static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
  1046. char *buf)
  1047. {
  1048. struct cgroup_subsys *ss = cft->ss;
  1049. if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
  1050. !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
  1051. snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
  1052. cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
  1053. cft->name);
  1054. else
  1055. strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
  1056. return buf;
  1057. }
  1058. /**
  1059. * cgroup_file_mode - deduce file mode of a control file
  1060. * @cft: the control file in question
  1061. *
  1062. * S_IRUGO for read, S_IWUSR for write.
  1063. */
  1064. static umode_t cgroup_file_mode(const struct cftype *cft)
  1065. {
  1066. umode_t mode = 0;
  1067. if (cft->read_u64 || cft->read_s64 || cft->seq_show)
  1068. mode |= S_IRUGO;
  1069. if (cft->write_u64 || cft->write_s64 || cft->write) {
  1070. if (cft->flags & CFTYPE_WORLD_WRITABLE)
  1071. mode |= S_IWUGO;
  1072. else
  1073. mode |= S_IWUSR;
  1074. }
  1075. return mode;
  1076. }
  1077. /**
  1078. * cgroup_calc_child_subsys_mask - calculate child_subsys_mask
  1079. * @cgrp: the target cgroup
  1080. * @subtree_control: the new subtree_control mask to consider
  1081. *
  1082. * On the default hierarchy, a subsystem may request other subsystems to be
  1083. * enabled together through its ->depends_on mask. In such cases, more
  1084. * subsystems than specified in "cgroup.subtree_control" may be enabled.
  1085. *
  1086. * This function calculates which subsystems need to be enabled if
  1087. * @subtree_control is to be applied to @cgrp. The returned mask is always
  1088. * a superset of @subtree_control and follows the usual hierarchy rules.
  1089. */
  1090. static unsigned long cgroup_calc_child_subsys_mask(struct cgroup *cgrp,
  1091. unsigned long subtree_control)
  1092. {
  1093. struct cgroup *parent = cgroup_parent(cgrp);
  1094. unsigned long cur_ss_mask = subtree_control;
  1095. struct cgroup_subsys *ss;
  1096. int ssid;
  1097. lockdep_assert_held(&cgroup_mutex);
  1098. if (!cgroup_on_dfl(cgrp))
  1099. return cur_ss_mask;
  1100. while (true) {
  1101. unsigned long new_ss_mask = cur_ss_mask;
  1102. for_each_subsys_which(ss, ssid, &cur_ss_mask)
  1103. new_ss_mask |= ss->depends_on;
  1104. /*
  1105. * Mask out subsystems which aren't available. This can
  1106. * happen only if some depended-upon subsystems were bound
  1107. * to non-default hierarchies.
  1108. */
  1109. if (parent)
  1110. new_ss_mask &= parent->child_subsys_mask;
  1111. else
  1112. new_ss_mask &= cgrp->root->subsys_mask;
  1113. if (new_ss_mask == cur_ss_mask)
  1114. break;
  1115. cur_ss_mask = new_ss_mask;
  1116. }
  1117. return cur_ss_mask;
  1118. }
  1119. /**
  1120. * cgroup_refresh_child_subsys_mask - update child_subsys_mask
  1121. * @cgrp: the target cgroup
  1122. *
  1123. * Update @cgrp->child_subsys_mask according to the current
  1124. * @cgrp->subtree_control using cgroup_calc_child_subsys_mask().
  1125. */
  1126. static void cgroup_refresh_child_subsys_mask(struct cgroup *cgrp)
  1127. {
  1128. cgrp->child_subsys_mask =
  1129. cgroup_calc_child_subsys_mask(cgrp, cgrp->subtree_control);
  1130. }
  1131. /**
  1132. * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
  1133. * @kn: the kernfs_node being serviced
  1134. *
  1135. * This helper undoes cgroup_kn_lock_live() and should be invoked before
  1136. * the method finishes if locking succeeded. Note that once this function
  1137. * returns the cgroup returned by cgroup_kn_lock_live() may become
  1138. * inaccessible any time. If the caller intends to continue to access the
  1139. * cgroup, it should pin it before invoking this function.
  1140. */
  1141. static void cgroup_kn_unlock(struct kernfs_node *kn)
  1142. {
  1143. struct cgroup *cgrp;
  1144. if (kernfs_type(kn) == KERNFS_DIR)
  1145. cgrp = kn->priv;
  1146. else
  1147. cgrp = kn->parent->priv;
  1148. mutex_unlock(&cgroup_mutex);
  1149. kernfs_unbreak_active_protection(kn);
  1150. cgroup_put(cgrp);
  1151. }
  1152. /**
  1153. * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
  1154. * @kn: the kernfs_node being serviced
  1155. *
  1156. * This helper is to be used by a cgroup kernfs method currently servicing
  1157. * @kn. It breaks the active protection, performs cgroup locking and
  1158. * verifies that the associated cgroup is alive. Returns the cgroup if
  1159. * alive; otherwise, %NULL. A successful return should be undone by a
  1160. * matching cgroup_kn_unlock() invocation.
  1161. *
  1162. * Any cgroup kernfs method implementation which requires locking the
  1163. * associated cgroup should use this helper. It avoids nesting cgroup
  1164. * locking under kernfs active protection and allows all kernfs operations
  1165. * including self-removal.
  1166. */
  1167. static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
  1168. {
  1169. struct cgroup *cgrp;
  1170. if (kernfs_type(kn) == KERNFS_DIR)
  1171. cgrp = kn->priv;
  1172. else
  1173. cgrp = kn->parent->priv;
  1174. /*
  1175. * We're gonna grab cgroup_mutex which nests outside kernfs
  1176. * active_ref. cgroup liveliness check alone provides enough
  1177. * protection against removal. Ensure @cgrp stays accessible and
  1178. * break the active_ref protection.
  1179. */
  1180. if (!cgroup_tryget(cgrp))
  1181. return NULL;
  1182. kernfs_break_active_protection(kn);
  1183. mutex_lock(&cgroup_mutex);
  1184. if (!cgroup_is_dead(cgrp))
  1185. return cgrp;
  1186. cgroup_kn_unlock(kn);
  1187. return NULL;
  1188. }
  1189. static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
  1190. {
  1191. char name[CGROUP_FILE_NAME_MAX];
  1192. lockdep_assert_held(&cgroup_mutex);
  1193. if (cft->file_offset) {
  1194. struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
  1195. struct cgroup_file *cfile = (void *)css + cft->file_offset;
  1196. spin_lock_irq(&cgroup_file_kn_lock);
  1197. cfile->kn = NULL;
  1198. spin_unlock_irq(&cgroup_file_kn_lock);
  1199. }
  1200. kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
  1201. }
  1202. /**
  1203. * css_clear_dir - remove subsys files in a cgroup directory
  1204. * @css: taget css
  1205. * @cgrp_override: specify if target cgroup is different from css->cgroup
  1206. */
  1207. static void css_clear_dir(struct cgroup_subsys_state *css,
  1208. struct cgroup *cgrp_override)
  1209. {
  1210. struct cgroup *cgrp = cgrp_override ?: css->cgroup;
  1211. struct cftype *cfts;
  1212. list_for_each_entry(cfts, &css->ss->cfts, node)
  1213. cgroup_addrm_files(css, cgrp, cfts, false);
  1214. }
  1215. /**
  1216. * css_populate_dir - create subsys files in a cgroup directory
  1217. * @css: target css
  1218. * @cgrp_overried: specify if target cgroup is different from css->cgroup
  1219. *
  1220. * On failure, no file is added.
  1221. */
  1222. static int css_populate_dir(struct cgroup_subsys_state *css,
  1223. struct cgroup *cgrp_override)
  1224. {
  1225. struct cgroup *cgrp = cgrp_override ?: css->cgroup;
  1226. struct cftype *cfts, *failed_cfts;
  1227. int ret;
  1228. if (!css->ss) {
  1229. if (cgroup_on_dfl(cgrp))
  1230. cfts = cgroup_dfl_base_files;
  1231. else
  1232. cfts = cgroup_legacy_base_files;
  1233. return cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
  1234. }
  1235. list_for_each_entry(cfts, &css->ss->cfts, node) {
  1236. ret = cgroup_addrm_files(css, cgrp, cfts, true);
  1237. if (ret < 0) {
  1238. failed_cfts = cfts;
  1239. goto err;
  1240. }
  1241. }
  1242. return 0;
  1243. err:
  1244. list_for_each_entry(cfts, &css->ss->cfts, node) {
  1245. if (cfts == failed_cfts)
  1246. break;
  1247. cgroup_addrm_files(css, cgrp, cfts, false);
  1248. }
  1249. return ret;
  1250. }
  1251. static int rebind_subsystems(struct cgroup_root *dst_root,
  1252. unsigned long ss_mask)
  1253. {
  1254. struct cgroup *dcgrp = &dst_root->cgrp;
  1255. struct cgroup_subsys *ss;
  1256. unsigned long tmp_ss_mask;
  1257. int ssid, i, ret;
  1258. lockdep_assert_held(&cgroup_mutex);
  1259. for_each_subsys_which(ss, ssid, &ss_mask) {
  1260. /* if @ss has non-root csses attached to it, can't move */
  1261. if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
  1262. return -EBUSY;
  1263. /* can't move between two non-dummy roots either */
  1264. if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
  1265. return -EBUSY;
  1266. }
  1267. /* skip creating root files on dfl_root for inhibited subsystems */
  1268. tmp_ss_mask = ss_mask;
  1269. if (dst_root == &cgrp_dfl_root)
  1270. tmp_ss_mask &= ~cgrp_dfl_root_inhibit_ss_mask;
  1271. for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
  1272. struct cgroup *scgrp = &ss->root->cgrp;
  1273. int tssid;
  1274. ret = css_populate_dir(cgroup_css(scgrp, ss), dcgrp);
  1275. if (!ret)
  1276. continue;
  1277. /*
  1278. * Rebinding back to the default root is not allowed to
  1279. * fail. Using both default and non-default roots should
  1280. * be rare. Moving subsystems back and forth even more so.
  1281. * Just warn about it and continue.
  1282. */
  1283. if (dst_root == &cgrp_dfl_root) {
  1284. if (cgrp_dfl_root_visible) {
  1285. pr_warn("failed to create files (%d) while rebinding 0x%lx to default root\n",
  1286. ret, ss_mask);
  1287. pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
  1288. }
  1289. continue;
  1290. }
  1291. for_each_subsys_which(ss, tssid, &tmp_ss_mask) {
  1292. if (tssid == ssid)
  1293. break;
  1294. css_clear_dir(cgroup_css(scgrp, ss), dcgrp);
  1295. }
  1296. return ret;
  1297. }
  1298. /*
  1299. * Nothing can fail from this point on. Remove files for the
  1300. * removed subsystems and rebind each subsystem.
  1301. */
  1302. for_each_subsys_which(ss, ssid, &ss_mask) {
  1303. struct cgroup_root *src_root = ss->root;
  1304. struct cgroup *scgrp = &src_root->cgrp;
  1305. struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
  1306. struct css_set *cset;
  1307. WARN_ON(!css || cgroup_css(dcgrp, ss));
  1308. css_clear_dir(css, NULL);
  1309. RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
  1310. rcu_assign_pointer(dcgrp->subsys[ssid], css);
  1311. ss->root = dst_root;
  1312. css->cgroup = dcgrp;
  1313. spin_lock_bh(&css_set_lock);
  1314. hash_for_each(css_set_table, i, cset, hlist)
  1315. list_move_tail(&cset->e_cset_node[ss->id],
  1316. &dcgrp->e_csets[ss->id]);
  1317. spin_unlock_bh(&css_set_lock);
  1318. src_root->subsys_mask &= ~(1 << ssid);
  1319. scgrp->subtree_control &= ~(1 << ssid);
  1320. cgroup_refresh_child_subsys_mask(scgrp);
  1321. /* default hierarchy doesn't enable controllers by default */
  1322. dst_root->subsys_mask |= 1 << ssid;
  1323. if (dst_root == &cgrp_dfl_root) {
  1324. static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
  1325. } else {
  1326. dcgrp->subtree_control |= 1 << ssid;
  1327. cgroup_refresh_child_subsys_mask(dcgrp);
  1328. static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
  1329. }
  1330. if (ss->bind)
  1331. ss->bind(css);
  1332. }
  1333. kernfs_activate(dcgrp->kn);
  1334. return 0;
  1335. }
  1336. static int cgroup_show_options(struct seq_file *seq,
  1337. struct kernfs_root *kf_root)
  1338. {
  1339. struct cgroup_root *root = cgroup_root_from_kf(kf_root);
  1340. struct cgroup_subsys *ss;
  1341. int ssid;
  1342. if (root != &cgrp_dfl_root)
  1343. for_each_subsys(ss, ssid)
  1344. if (root->subsys_mask & (1 << ssid))
  1345. seq_show_option(seq, ss->legacy_name, NULL);
  1346. if (root->flags & CGRP_ROOT_NOPREFIX)
  1347. seq_puts(seq, ",noprefix");
  1348. if (root->flags & CGRP_ROOT_XATTR)
  1349. seq_puts(seq, ",xattr");
  1350. spin_lock(&release_agent_path_lock);
  1351. if (strlen(root->release_agent_path))
  1352. seq_show_option(seq, "release_agent",
  1353. root->release_agent_path);
  1354. spin_unlock(&release_agent_path_lock);
  1355. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
  1356. seq_puts(seq, ",clone_children");
  1357. if (strlen(root->name))
  1358. seq_show_option(seq, "name", root->name);
  1359. return 0;
  1360. }
  1361. struct cgroup_sb_opts {
  1362. unsigned long subsys_mask;
  1363. unsigned int flags;
  1364. char *release_agent;
  1365. bool cpuset_clone_children;
  1366. char *name;
  1367. /* User explicitly requested empty subsystem */
  1368. bool none;
  1369. };
  1370. static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
  1371. {
  1372. char *token, *o = data;
  1373. bool all_ss = false, one_ss = false;
  1374. unsigned long mask = -1UL;
  1375. struct cgroup_subsys *ss;
  1376. int nr_opts = 0;
  1377. int i;
  1378. #ifdef CONFIG_CPUSETS
  1379. mask = ~(1U << cpuset_cgrp_id);
  1380. #endif
  1381. memset(opts, 0, sizeof(*opts));
  1382. while ((token = strsep(&o, ",")) != NULL) {
  1383. nr_opts++;
  1384. if (!*token)
  1385. return -EINVAL;
  1386. if (!strcmp(token, "none")) {
  1387. /* Explicitly have no subsystems */
  1388. opts->none = true;
  1389. continue;
  1390. }
  1391. if (!strcmp(token, "all")) {
  1392. /* Mutually exclusive option 'all' + subsystem name */
  1393. if (one_ss)
  1394. return -EINVAL;
  1395. all_ss = true;
  1396. continue;
  1397. }
  1398. if (!strcmp(token, "noprefix")) {
  1399. opts->flags |= CGRP_ROOT_NOPREFIX;
  1400. continue;
  1401. }
  1402. if (!strcmp(token, "clone_children")) {
  1403. opts->cpuset_clone_children = true;
  1404. continue;
  1405. }
  1406. if (!strcmp(token, "xattr")) {
  1407. opts->flags |= CGRP_ROOT_XATTR;
  1408. continue;
  1409. }
  1410. if (!strncmp(token, "release_agent=", 14)) {
  1411. /* Specifying two release agents is forbidden */
  1412. if (opts->release_agent)
  1413. return -EINVAL;
  1414. opts->release_agent =
  1415. kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
  1416. if (!opts->release_agent)
  1417. return -ENOMEM;
  1418. continue;
  1419. }
  1420. if (!strncmp(token, "name=", 5)) {
  1421. const char *name = token + 5;
  1422. /* Can't specify an empty name */
  1423. if (!strlen(name))
  1424. return -EINVAL;
  1425. /* Must match [\w.-]+ */
  1426. for (i = 0; i < strlen(name); i++) {
  1427. char c = name[i];
  1428. if (isalnum(c))
  1429. continue;
  1430. if ((c == '.') || (c == '-') || (c == '_'))
  1431. continue;
  1432. return -EINVAL;
  1433. }
  1434. /* Specifying two names is forbidden */
  1435. if (opts->name)
  1436. return -EINVAL;
  1437. opts->name = kstrndup(name,
  1438. MAX_CGROUP_ROOT_NAMELEN - 1,
  1439. GFP_KERNEL);
  1440. if (!opts->name)
  1441. return -ENOMEM;
  1442. continue;
  1443. }
  1444. for_each_subsys(ss, i) {
  1445. if (strcmp(token, ss->legacy_name))
  1446. continue;
  1447. if (!cgroup_ssid_enabled(i))
  1448. continue;
  1449. /* Mutually exclusive option 'all' + subsystem name */
  1450. if (all_ss)
  1451. return -EINVAL;
  1452. opts->subsys_mask |= (1 << i);
  1453. one_ss = true;
  1454. break;
  1455. }
  1456. if (i == CGROUP_SUBSYS_COUNT)
  1457. return -ENOENT;
  1458. }
  1459. /*
  1460. * If the 'all' option was specified select all the subsystems,
  1461. * otherwise if 'none', 'name=' and a subsystem name options were
  1462. * not specified, let's default to 'all'
  1463. */
  1464. if (all_ss || (!one_ss && !opts->none && !opts->name))
  1465. for_each_subsys(ss, i)
  1466. if (cgroup_ssid_enabled(i))
  1467. opts->subsys_mask |= (1 << i);
  1468. /*
  1469. * We either have to specify by name or by subsystems. (So all
  1470. * empty hierarchies must have a name).
  1471. */
  1472. if (!opts->subsys_mask && !opts->name)
  1473. return -EINVAL;
  1474. /*
  1475. * Option noprefix was introduced just for backward compatibility
  1476. * with the old cpuset, so we allow noprefix only if mounting just
  1477. * the cpuset subsystem.
  1478. */
  1479. if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
  1480. return -EINVAL;
  1481. /* Can't specify "none" and some subsystems */
  1482. if (opts->subsys_mask && opts->none)
  1483. return -EINVAL;
  1484. return 0;
  1485. }
  1486. static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
  1487. {
  1488. int ret = 0;
  1489. struct cgroup_root *root = cgroup_root_from_kf(kf_root);
  1490. struct cgroup_sb_opts opts;
  1491. unsigned long added_mask, removed_mask;
  1492. if (root == &cgrp_dfl_root) {
  1493. pr_err("remount is not allowed\n");
  1494. return -EINVAL;
  1495. }
  1496. mutex_lock(&cgroup_mutex);
  1497. /* See what subsystems are wanted */
  1498. ret = parse_cgroupfs_options(data, &opts);
  1499. if (ret)
  1500. goto out_unlock;
  1501. if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
  1502. pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
  1503. task_tgid_nr(current), current->comm);
  1504. added_mask = opts.subsys_mask & ~root->subsys_mask;
  1505. removed_mask = root->subsys_mask & ~opts.subsys_mask;
  1506. /* Don't allow flags or name to change at remount */
  1507. if ((opts.flags ^ root->flags) ||
  1508. (opts.name && strcmp(opts.name, root->name))) {
  1509. pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
  1510. opts.flags, opts.name ?: "", root->flags, root->name);
  1511. ret = -EINVAL;
  1512. goto out_unlock;
  1513. }
  1514. /* remounting is not allowed for populated hierarchies */
  1515. if (!list_empty(&root->cgrp.self.children)) {
  1516. ret = -EBUSY;
  1517. goto out_unlock;
  1518. }
  1519. ret = rebind_subsystems(root, added_mask);
  1520. if (ret)
  1521. goto out_unlock;
  1522. rebind_subsystems(&cgrp_dfl_root, removed_mask);
  1523. if (opts.release_agent) {
  1524. spin_lock(&release_agent_path_lock);
  1525. strcpy(root->release_agent_path, opts.release_agent);
  1526. spin_unlock(&release_agent_path_lock);
  1527. }
  1528. out_unlock:
  1529. kfree(opts.release_agent);
  1530. kfree(opts.name);
  1531. mutex_unlock(&cgroup_mutex);
  1532. return ret;
  1533. }
  1534. /*
  1535. * To reduce the fork() overhead for systems that are not actually using
  1536. * their cgroups capability, we don't maintain the lists running through
  1537. * each css_set to its tasks until we see the list actually used - in other
  1538. * words after the first mount.
  1539. */
  1540. static bool use_task_css_set_links __read_mostly;
  1541. static void cgroup_enable_task_cg_lists(void)
  1542. {
  1543. struct task_struct *p, *g;
  1544. spin_lock_bh(&css_set_lock);
  1545. if (use_task_css_set_links)
  1546. goto out_unlock;
  1547. use_task_css_set_links = true;
  1548. /*
  1549. * We need tasklist_lock because RCU is not safe against
  1550. * while_each_thread(). Besides, a forking task that has passed
  1551. * cgroup_post_fork() without seeing use_task_css_set_links = 1
  1552. * is not guaranteed to have its child immediately visible in the
  1553. * tasklist if we walk through it with RCU.
  1554. */
  1555. read_lock(&tasklist_lock);
  1556. do_each_thread(g, p) {
  1557. WARN_ON_ONCE(!list_empty(&p->cg_list) ||
  1558. task_css_set(p) != &init_css_set);
  1559. /*
  1560. * We should check if the process is exiting, otherwise
  1561. * it will race with cgroup_exit() in that the list
  1562. * entry won't be deleted though the process has exited.
  1563. * Do it while holding siglock so that we don't end up
  1564. * racing against cgroup_exit().
  1565. */
  1566. spin_lock_irq(&p->sighand->siglock);
  1567. if (!(p->flags & PF_EXITING)) {
  1568. struct css_set *cset = task_css_set(p);
  1569. if (!css_set_populated(cset))
  1570. css_set_update_populated(cset, true);
  1571. list_add_tail(&p->cg_list, &cset->tasks);
  1572. get_css_set(cset);
  1573. }
  1574. spin_unlock_irq(&p->sighand->siglock);
  1575. } while_each_thread(g, p);
  1576. read_unlock(&tasklist_lock);
  1577. out_unlock:
  1578. spin_unlock_bh(&css_set_lock);
  1579. }
  1580. static void init_cgroup_housekeeping(struct cgroup *cgrp)
  1581. {
  1582. struct cgroup_subsys *ss;
  1583. int ssid;
  1584. INIT_LIST_HEAD(&cgrp->self.sibling);
  1585. INIT_LIST_HEAD(&cgrp->self.children);
  1586. INIT_LIST_HEAD(&cgrp->cset_links);
  1587. INIT_LIST_HEAD(&cgrp->pidlists);
  1588. mutex_init(&cgrp->pidlist_mutex);
  1589. cgrp->self.cgroup = cgrp;
  1590. cgrp->self.flags |= CSS_ONLINE;
  1591. for_each_subsys(ss, ssid)
  1592. INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
  1593. init_waitqueue_head(&cgrp->offline_waitq);
  1594. INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
  1595. }
  1596. static void init_cgroup_root(struct cgroup_root *root,
  1597. struct cgroup_sb_opts *opts)
  1598. {
  1599. struct cgroup *cgrp = &root->cgrp;
  1600. INIT_LIST_HEAD(&root->root_list);
  1601. atomic_set(&root->nr_cgrps, 1);
  1602. cgrp->root = root;
  1603. init_cgroup_housekeeping(cgrp);
  1604. idr_init(&root->cgroup_idr);
  1605. root->flags = opts->flags;
  1606. if (opts->release_agent)
  1607. strcpy(root->release_agent_path, opts->release_agent);
  1608. if (opts->name)
  1609. strcpy(root->name, opts->name);
  1610. if (opts->cpuset_clone_children)
  1611. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
  1612. }
  1613. static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
  1614. {
  1615. LIST_HEAD(tmp_links);
  1616. struct cgroup *root_cgrp = &root->cgrp;
  1617. struct css_set *cset;
  1618. int i, ret;
  1619. lockdep_assert_held(&cgroup_mutex);
  1620. ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
  1621. if (ret < 0)
  1622. goto out;
  1623. root_cgrp->id = ret;
  1624. root_cgrp->ancestor_ids[0] = ret;
  1625. ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
  1626. GFP_KERNEL);
  1627. if (ret)
  1628. goto out;
  1629. /*
  1630. * We're accessing css_set_count without locking css_set_lock here,
  1631. * but that's OK - it can only be increased by someone holding
  1632. * cgroup_lock, and that's us. The worst that can happen is that we
  1633. * have some link structures left over
  1634. */
  1635. ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
  1636. if (ret)
  1637. goto cancel_ref;
  1638. ret = cgroup_init_root_id(root);
  1639. if (ret)
  1640. goto cancel_ref;
  1641. root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
  1642. KERNFS_ROOT_CREATE_DEACTIVATED,
  1643. root_cgrp);
  1644. if (IS_ERR(root->kf_root)) {
  1645. ret = PTR_ERR(root->kf_root);
  1646. goto exit_root_id;
  1647. }
  1648. root_cgrp->kn = root->kf_root->kn;
  1649. ret = css_populate_dir(&root_cgrp->self, NULL);
  1650. if (ret)
  1651. goto destroy_root;
  1652. ret = rebind_subsystems(root, ss_mask);
  1653. if (ret)
  1654. goto destroy_root;
  1655. /*
  1656. * There must be no failure case after here, since rebinding takes
  1657. * care of subsystems' refcounts, which are explicitly dropped in
  1658. * the failure exit path.
  1659. */
  1660. list_add(&root->root_list, &cgroup_roots);
  1661. cgroup_root_count++;
  1662. /*
  1663. * Link the root cgroup in this hierarchy into all the css_set
  1664. * objects.
  1665. */
  1666. spin_lock_bh(&css_set_lock);
  1667. hash_for_each(css_set_table, i, cset, hlist) {
  1668. link_css_set(&tmp_links, cset, root_cgrp);
  1669. if (css_set_populated(cset))
  1670. cgroup_update_populated(root_cgrp, true);
  1671. }
  1672. spin_unlock_bh(&css_set_lock);
  1673. BUG_ON(!list_empty(&root_cgrp->self.children));
  1674. BUG_ON(atomic_read(&root->nr_cgrps) != 1);
  1675. kernfs_activate(root_cgrp->kn);
  1676. ret = 0;
  1677. goto out;
  1678. destroy_root:
  1679. kernfs_destroy_root(root->kf_root);
  1680. root->kf_root = NULL;
  1681. exit_root_id:
  1682. cgroup_exit_root_id(root);
  1683. cancel_ref:
  1684. percpu_ref_exit(&root_cgrp->self.refcnt);
  1685. out:
  1686. free_cgrp_cset_links(&tmp_links);
  1687. return ret;
  1688. }
  1689. static struct dentry *cgroup_mount(struct file_system_type *fs_type,
  1690. int flags, const char *unused_dev_name,
  1691. void *data)
  1692. {
  1693. bool is_v2 = fs_type == &cgroup2_fs_type;
  1694. struct super_block *pinned_sb = NULL;
  1695. struct cgroup_subsys *ss;
  1696. struct cgroup_root *root;
  1697. struct cgroup_sb_opts opts;
  1698. struct dentry *dentry;
  1699. int ret;
  1700. int i;
  1701. bool new_sb;
  1702. /*
  1703. * The first time anyone tries to mount a cgroup, enable the list
  1704. * linking each css_set to its tasks and fix up all existing tasks.
  1705. */
  1706. if (!use_task_css_set_links)
  1707. cgroup_enable_task_cg_lists();
  1708. if (is_v2) {
  1709. if (data) {
  1710. pr_err("cgroup2: unknown option \"%s\"\n", (char *)data);
  1711. return ERR_PTR(-EINVAL);
  1712. }
  1713. cgrp_dfl_root_visible = true;
  1714. root = &cgrp_dfl_root;
  1715. cgroup_get(&root->cgrp);
  1716. goto out_mount;
  1717. }
  1718. mutex_lock(&cgroup_mutex);
  1719. /* First find the desired set of subsystems */
  1720. ret = parse_cgroupfs_options(data, &opts);
  1721. if (ret)
  1722. goto out_unlock;
  1723. /*
  1724. * Destruction of cgroup root is asynchronous, so subsystems may
  1725. * still be dying after the previous unmount. Let's drain the
  1726. * dying subsystems. We just need to ensure that the ones
  1727. * unmounted previously finish dying and don't care about new ones
  1728. * starting. Testing ref liveliness is good enough.
  1729. */
  1730. for_each_subsys(ss, i) {
  1731. if (!(opts.subsys_mask & (1 << i)) ||
  1732. ss->root == &cgrp_dfl_root)
  1733. continue;
  1734. if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
  1735. mutex_unlock(&cgroup_mutex);
  1736. msleep(10);
  1737. ret = restart_syscall();
  1738. goto out_free;
  1739. }
  1740. cgroup_put(&ss->root->cgrp);
  1741. }
  1742. for_each_root(root) {
  1743. bool name_match = false;
  1744. if (root == &cgrp_dfl_root)
  1745. continue;
  1746. /*
  1747. * If we asked for a name then it must match. Also, if
  1748. * name matches but sybsys_mask doesn't, we should fail.
  1749. * Remember whether name matched.
  1750. */
  1751. if (opts.name) {
  1752. if (strcmp(opts.name, root->name))
  1753. continue;
  1754. name_match = true;
  1755. }
  1756. /*
  1757. * If we asked for subsystems (or explicitly for no
  1758. * subsystems) then they must match.
  1759. */
  1760. if ((opts.subsys_mask || opts.none) &&
  1761. (opts.subsys_mask != root->subsys_mask)) {
  1762. if (!name_match)
  1763. continue;
  1764. ret = -EBUSY;
  1765. goto out_unlock;
  1766. }
  1767. if (root->flags ^ opts.flags)
  1768. pr_warn("new mount options do not match the existing superblock, will be ignored\n");
  1769. /*
  1770. * We want to reuse @root whose lifetime is governed by its
  1771. * ->cgrp. Let's check whether @root is alive and keep it
  1772. * that way. As cgroup_kill_sb() can happen anytime, we
  1773. * want to block it by pinning the sb so that @root doesn't
  1774. * get killed before mount is complete.
  1775. *
  1776. * With the sb pinned, tryget_live can reliably indicate
  1777. * whether @root can be reused. If it's being killed,
  1778. * drain it. We can use wait_queue for the wait but this
  1779. * path is super cold. Let's just sleep a bit and retry.
  1780. */
  1781. pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
  1782. if (IS_ERR(pinned_sb) ||
  1783. !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
  1784. mutex_unlock(&cgroup_mutex);
  1785. if (!IS_ERR_OR_NULL(pinned_sb))
  1786. deactivate_super(pinned_sb);
  1787. msleep(10);
  1788. ret = restart_syscall();
  1789. goto out_free;
  1790. }
  1791. ret = 0;
  1792. goto out_unlock;
  1793. }
  1794. /*
  1795. * No such thing, create a new one. name= matching without subsys
  1796. * specification is allowed for already existing hierarchies but we
  1797. * can't create new one without subsys specification.
  1798. */
  1799. if (!opts.subsys_mask && !opts.none) {
  1800. ret = -EINVAL;
  1801. goto out_unlock;
  1802. }
  1803. root = kzalloc(sizeof(*root), GFP_KERNEL);
  1804. if (!root) {
  1805. ret = -ENOMEM;
  1806. goto out_unlock;
  1807. }
  1808. init_cgroup_root(root, &opts);
  1809. ret = cgroup_setup_root(root, opts.subsys_mask);
  1810. if (ret)
  1811. cgroup_free_root(root);
  1812. out_unlock:
  1813. mutex_unlock(&cgroup_mutex);
  1814. out_free:
  1815. kfree(opts.release_agent);
  1816. kfree(opts.name);
  1817. if (ret)
  1818. return ERR_PTR(ret);
  1819. out_mount:
  1820. dentry = kernfs_mount(fs_type, flags, root->kf_root,
  1821. is_v2 ? CGROUP2_SUPER_MAGIC : CGROUP_SUPER_MAGIC,
  1822. &new_sb);
  1823. if (IS_ERR(dentry) || !new_sb)
  1824. cgroup_put(&root->cgrp);
  1825. /*
  1826. * If @pinned_sb, we're reusing an existing root and holding an
  1827. * extra ref on its sb. Mount is complete. Put the extra ref.
  1828. */
  1829. if (pinned_sb) {
  1830. WARN_ON(new_sb);
  1831. deactivate_super(pinned_sb);
  1832. }
  1833. return dentry;
  1834. }
  1835. static void cgroup_kill_sb(struct super_block *sb)
  1836. {
  1837. struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
  1838. struct cgroup_root *root = cgroup_root_from_kf(kf_root);
  1839. /*
  1840. * If @root doesn't have any mounts or children, start killing it.
  1841. * This prevents new mounts by disabling percpu_ref_tryget_live().
  1842. * cgroup_mount() may wait for @root's release.
  1843. *
  1844. * And don't kill the default root.
  1845. */
  1846. if (!list_empty(&root->cgrp.self.children) ||
  1847. root == &cgrp_dfl_root)
  1848. cgroup_put(&root->cgrp);
  1849. else
  1850. percpu_ref_kill(&root->cgrp.self.refcnt);
  1851. kernfs_kill_sb(sb);
  1852. }
  1853. static struct file_system_type cgroup_fs_type = {
  1854. .name = "cgroup",
  1855. .mount = cgroup_mount,
  1856. .kill_sb = cgroup_kill_sb,
  1857. };
  1858. static struct file_system_type cgroup2_fs_type = {
  1859. .name = "cgroup2",
  1860. .mount = cgroup_mount,
  1861. .kill_sb = cgroup_kill_sb,
  1862. };
  1863. /**
  1864. * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
  1865. * @task: target task
  1866. * @buf: the buffer to write the path into
  1867. * @buflen: the length of the buffer
  1868. *
  1869. * Determine @task's cgroup on the first (the one with the lowest non-zero
  1870. * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
  1871. * function grabs cgroup_mutex and shouldn't be used inside locks used by
  1872. * cgroup controller callbacks.
  1873. *
  1874. * Return value is the same as kernfs_path().
  1875. */
  1876. char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
  1877. {
  1878. struct cgroup_root *root;
  1879. struct cgroup *cgrp;
  1880. int hierarchy_id = 1;
  1881. char *path = NULL;
  1882. mutex_lock(&cgroup_mutex);
  1883. spin_lock_bh(&css_set_lock);
  1884. root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
  1885. if (root) {
  1886. cgrp = task_cgroup_from_root(task, root);
  1887. path = cgroup_path(cgrp, buf, buflen);
  1888. } else {
  1889. /* if no hierarchy exists, everyone is in "/" */
  1890. if (strlcpy(buf, "/", buflen) < buflen)
  1891. path = buf;
  1892. }
  1893. spin_unlock_bh(&css_set_lock);
  1894. mutex_unlock(&cgroup_mutex);
  1895. return path;
  1896. }
  1897. EXPORT_SYMBOL_GPL(task_cgroup_path);
  1898. /* used to track tasks and other necessary states during migration */
  1899. struct cgroup_taskset {
  1900. /* the src and dst cset list running through cset->mg_node */
  1901. struct list_head src_csets;
  1902. struct list_head dst_csets;
  1903. /* the subsys currently being processed */
  1904. int ssid;
  1905. /*
  1906. * Fields for cgroup_taskset_*() iteration.
  1907. *
  1908. * Before migration is committed, the target migration tasks are on
  1909. * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
  1910. * the csets on ->dst_csets. ->csets point to either ->src_csets
  1911. * or ->dst_csets depending on whether migration is committed.
  1912. *
  1913. * ->cur_csets and ->cur_task point to the current task position
  1914. * during iteration.
  1915. */
  1916. struct list_head *csets;
  1917. struct css_set *cur_cset;
  1918. struct task_struct *cur_task;
  1919. };
  1920. #define CGROUP_TASKSET_INIT(tset) (struct cgroup_taskset){ \
  1921. .src_csets = LIST_HEAD_INIT(tset.src_csets), \
  1922. .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \
  1923. .csets = &tset.src_csets, \
  1924. }
  1925. /**
  1926. * cgroup_taskset_add - try to add a migration target task to a taskset
  1927. * @task: target task
  1928. * @tset: target taskset
  1929. *
  1930. * Add @task, which is a migration target, to @tset. This function becomes
  1931. * noop if @task doesn't need to be migrated. @task's css_set should have
  1932. * been added as a migration source and @task->cg_list will be moved from
  1933. * the css_set's tasks list to mg_tasks one.
  1934. */
  1935. static void cgroup_taskset_add(struct task_struct *task,
  1936. struct cgroup_taskset *tset)
  1937. {
  1938. struct css_set *cset;
  1939. lockdep_assert_held(&css_set_lock);
  1940. /* @task either already exited or can't exit until the end */
  1941. if (task->flags & PF_EXITING)
  1942. return;
  1943. /* leave @task alone if post_fork() hasn't linked it yet */
  1944. if (list_empty(&task->cg_list))
  1945. return;
  1946. cset = task_css_set(task);
  1947. if (!cset->mg_src_cgrp)
  1948. return;
  1949. list_move_tail(&task->cg_list, &cset->mg_tasks);
  1950. if (list_empty(&cset->mg_node))
  1951. list_add_tail(&cset->mg_node, &tset->src_csets);
  1952. if (list_empty(&cset->mg_dst_cset->mg_node))
  1953. list_move_tail(&cset->mg_dst_cset->mg_node,
  1954. &tset->dst_csets);
  1955. }
  1956. /**
  1957. * cgroup_taskset_first - reset taskset and return the first task
  1958. * @tset: taskset of interest
  1959. * @dst_cssp: output variable for the destination css
  1960. *
  1961. * @tset iteration is initialized and the first task is returned.
  1962. */
  1963. struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
  1964. struct cgroup_subsys_state **dst_cssp)
  1965. {
  1966. tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
  1967. tset->cur_task = NULL;
  1968. return cgroup_taskset_next(tset, dst_cssp);
  1969. }
  1970. /**
  1971. * cgroup_taskset_next - iterate to the next task in taskset
  1972. * @tset: taskset of interest
  1973. * @dst_cssp: output variable for the destination css
  1974. *
  1975. * Return the next task in @tset. Iteration must have been initialized
  1976. * with cgroup_taskset_first().
  1977. */
  1978. struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
  1979. struct cgroup_subsys_state **dst_cssp)
  1980. {
  1981. struct css_set *cset = tset->cur_cset;
  1982. struct task_struct *task = tset->cur_task;
  1983. while (&cset->mg_node != tset->csets) {
  1984. if (!task)
  1985. task = list_first_entry(&cset->mg_tasks,
  1986. struct task_struct, cg_list);
  1987. else
  1988. task = list_next_entry(task, cg_list);
  1989. if (&task->cg_list != &cset->mg_tasks) {
  1990. tset->cur_cset = cset;
  1991. tset->cur_task = task;
  1992. /*
  1993. * This function may be called both before and
  1994. * after cgroup_taskset_migrate(). The two cases
  1995. * can be distinguished by looking at whether @cset
  1996. * has its ->mg_dst_cset set.
  1997. */
  1998. if (cset->mg_dst_cset)
  1999. *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
  2000. else
  2001. *dst_cssp = cset->subsys[tset->ssid];
  2002. return task;
  2003. }
  2004. cset = list_next_entry(cset, mg_node);
  2005. task = NULL;
  2006. }
  2007. return NULL;
  2008. }
  2009. /**
  2010. * cgroup_taskset_migrate - migrate a taskset to a cgroup
  2011. * @tset: taget taskset
  2012. * @dst_cgrp: destination cgroup
  2013. *
  2014. * Migrate tasks in @tset to @dst_cgrp. This function fails iff one of the
  2015. * ->can_attach callbacks fails and guarantees that either all or none of
  2016. * the tasks in @tset are migrated. @tset is consumed regardless of
  2017. * success.
  2018. */
  2019. static int cgroup_taskset_migrate(struct cgroup_taskset *tset,
  2020. struct cgroup *dst_cgrp)
  2021. {
  2022. struct cgroup_subsys_state *css, *failed_css = NULL;
  2023. struct task_struct *task, *tmp_task;
  2024. struct css_set *cset, *tmp_cset;
  2025. int i, ret;
  2026. /* methods shouldn't be called if no task is actually migrating */
  2027. if (list_empty(&tset->src_csets))
  2028. return 0;
  2029. /* check that we can legitimately attach to the cgroup */
  2030. for_each_e_css(css, i, dst_cgrp) {
  2031. if (css->ss->can_attach) {
  2032. tset->ssid = i;
  2033. ret = css->ss->can_attach(tset);
  2034. if (ret) {
  2035. failed_css = css;
  2036. goto out_cancel_attach;
  2037. }
  2038. }
  2039. }
  2040. /*
  2041. * Now that we're guaranteed success, proceed to move all tasks to
  2042. * the new cgroup. There are no failure cases after here, so this
  2043. * is the commit point.
  2044. */
  2045. spin_lock_bh(&css_set_lock);
  2046. list_for_each_entry(cset, &tset->src_csets, mg_node) {
  2047. list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
  2048. struct css_set *from_cset = task_css_set(task);
  2049. struct css_set *to_cset = cset->mg_dst_cset;
  2050. get_css_set(to_cset);
  2051. css_set_move_task(task, from_cset, to_cset, true);
  2052. put_css_set_locked(from_cset);
  2053. }
  2054. }
  2055. spin_unlock_bh(&css_set_lock);
  2056. /*
  2057. * Migration is committed, all target tasks are now on dst_csets.
  2058. * Nothing is sensitive to fork() after this point. Notify
  2059. * controllers that migration is complete.
  2060. */
  2061. tset->csets = &tset->dst_csets;
  2062. for_each_e_css(css, i, dst_cgrp) {
  2063. if (css->ss->attach) {
  2064. tset->ssid = i;
  2065. css->ss->attach(tset);
  2066. }
  2067. }
  2068. ret = 0;
  2069. goto out_release_tset;
  2070. out_cancel_attach:
  2071. for_each_e_css(css, i, dst_cgrp) {
  2072. if (css == failed_css)
  2073. break;
  2074. if (css->ss->cancel_attach) {
  2075. tset->ssid = i;
  2076. css->ss->cancel_attach(tset);
  2077. }
  2078. }
  2079. out_release_tset:
  2080. spin_lock_bh(&css_set_lock);
  2081. list_splice_init(&tset->dst_csets, &tset->src_csets);
  2082. list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
  2083. list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
  2084. list_del_init(&cset->mg_node);
  2085. }
  2086. spin_unlock_bh(&css_set_lock);
  2087. return ret;
  2088. }
  2089. /**
  2090. * cgroup_migrate_finish - cleanup after attach
  2091. * @preloaded_csets: list of preloaded css_sets
  2092. *
  2093. * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
  2094. * those functions for details.
  2095. */
  2096. static void cgroup_migrate_finish(struct list_head *preloaded_csets)
  2097. {
  2098. struct css_set *cset, *tmp_cset;
  2099. lockdep_assert_held(&cgroup_mutex);
  2100. spin_lock_bh(&css_set_lock);
  2101. list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
  2102. cset->mg_src_cgrp = NULL;
  2103. cset->mg_dst_cset = NULL;
  2104. list_del_init(&cset->mg_preload_node);
  2105. put_css_set_locked(cset);
  2106. }
  2107. spin_unlock_bh(&css_set_lock);
  2108. }
  2109. /**
  2110. * cgroup_migrate_add_src - add a migration source css_set
  2111. * @src_cset: the source css_set to add
  2112. * @dst_cgrp: the destination cgroup
  2113. * @preloaded_csets: list of preloaded css_sets
  2114. *
  2115. * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
  2116. * @src_cset and add it to @preloaded_csets, which should later be cleaned
  2117. * up by cgroup_migrate_finish().
  2118. *
  2119. * This function may be called without holding cgroup_threadgroup_rwsem
  2120. * even if the target is a process. Threads may be created and destroyed
  2121. * but as long as cgroup_mutex is not dropped, no new css_set can be put
  2122. * into play and the preloaded css_sets are guaranteed to cover all
  2123. * migrations.
  2124. */
  2125. static void cgroup_migrate_add_src(struct css_set *src_cset,
  2126. struct cgroup *dst_cgrp,
  2127. struct list_head *preloaded_csets)
  2128. {
  2129. struct cgroup *src_cgrp;
  2130. lockdep_assert_held(&cgroup_mutex);
  2131. lockdep_assert_held(&css_set_lock);
  2132. src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
  2133. if (!list_empty(&src_cset->mg_preload_node))
  2134. return;
  2135. WARN_ON(src_cset->mg_src_cgrp);
  2136. WARN_ON(!list_empty(&src_cset->mg_tasks));
  2137. WARN_ON(!list_empty(&src_cset->mg_node));
  2138. src_cset->mg_src_cgrp = src_cgrp;
  2139. get_css_set(src_cset);
  2140. list_add(&src_cset->mg_preload_node, preloaded_csets);
  2141. }
  2142. /**
  2143. * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
  2144. * @dst_cgrp: the destination cgroup (may be %NULL)
  2145. * @preloaded_csets: list of preloaded source css_sets
  2146. *
  2147. * Tasks are about to be moved to @dst_cgrp and all the source css_sets
  2148. * have been preloaded to @preloaded_csets. This function looks up and
  2149. * pins all destination css_sets, links each to its source, and append them
  2150. * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
  2151. * source css_set is assumed to be its cgroup on the default hierarchy.
  2152. *
  2153. * This function must be called after cgroup_migrate_add_src() has been
  2154. * called on each migration source css_set. After migration is performed
  2155. * using cgroup_migrate(), cgroup_migrate_finish() must be called on
  2156. * @preloaded_csets.
  2157. */
  2158. static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
  2159. struct list_head *preloaded_csets)
  2160. {
  2161. LIST_HEAD(csets);
  2162. struct css_set *src_cset, *tmp_cset;
  2163. lockdep_assert_held(&cgroup_mutex);
  2164. /*
  2165. * Except for the root, child_subsys_mask must be zero for a cgroup
  2166. * with tasks so that child cgroups don't compete against tasks.
  2167. */
  2168. if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
  2169. dst_cgrp->child_subsys_mask)
  2170. return -EBUSY;
  2171. /* look up the dst cset for each src cset and link it to src */
  2172. list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
  2173. struct css_set *dst_cset;
  2174. dst_cset = find_css_set(src_cset,
  2175. dst_cgrp ?: src_cset->dfl_cgrp);
  2176. if (!dst_cset)
  2177. goto err;
  2178. WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
  2179. /*
  2180. * If src cset equals dst, it's noop. Drop the src.
  2181. * cgroup_migrate() will skip the cset too. Note that we
  2182. * can't handle src == dst as some nodes are used by both.
  2183. */
  2184. if (src_cset == dst_cset) {
  2185. src_cset->mg_src_cgrp = NULL;
  2186. list_del_init(&src_cset->mg_preload_node);
  2187. put_css_set(src_cset);
  2188. put_css_set(dst_cset);
  2189. continue;
  2190. }
  2191. src_cset->mg_dst_cset = dst_cset;
  2192. if (list_empty(&dst_cset->mg_preload_node))
  2193. list_add(&dst_cset->mg_preload_node, &csets);
  2194. else
  2195. put_css_set(dst_cset);
  2196. }
  2197. list_splice_tail(&csets, preloaded_csets);
  2198. return 0;
  2199. err:
  2200. cgroup_migrate_finish(&csets);
  2201. return -ENOMEM;
  2202. }
  2203. /**
  2204. * cgroup_migrate - migrate a process or task to a cgroup
  2205. * @leader: the leader of the process or the task to migrate
  2206. * @threadgroup: whether @leader points to the whole process or a single task
  2207. * @cgrp: the destination cgroup
  2208. *
  2209. * Migrate a process or task denoted by @leader to @cgrp. If migrating a
  2210. * process, the caller must be holding cgroup_threadgroup_rwsem. The
  2211. * caller is also responsible for invoking cgroup_migrate_add_src() and
  2212. * cgroup_migrate_prepare_dst() on the targets before invoking this
  2213. * function and following up with cgroup_migrate_finish().
  2214. *
  2215. * As long as a controller's ->can_attach() doesn't fail, this function is
  2216. * guaranteed to succeed. This means that, excluding ->can_attach()
  2217. * failure, when migrating multiple targets, the success or failure can be
  2218. * decided for all targets by invoking group_migrate_prepare_dst() before
  2219. * actually starting migrating.
  2220. */
  2221. static int cgroup_migrate(struct task_struct *leader, bool threadgroup,
  2222. struct cgroup *cgrp)
  2223. {
  2224. struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
  2225. struct task_struct *task;
  2226. /*
  2227. * Prevent freeing of tasks while we take a snapshot. Tasks that are
  2228. * already PF_EXITING could be freed from underneath us unless we
  2229. * take an rcu_read_lock.
  2230. */
  2231. spin_lock_bh(&css_set_lock);
  2232. rcu_read_lock();
  2233. task = leader;
  2234. do {
  2235. cgroup_taskset_add(task, &tset);
  2236. if (!threadgroup)
  2237. break;
  2238. } while_each_thread(leader, task);
  2239. rcu_read_unlock();
  2240. spin_unlock_bh(&css_set_lock);
  2241. return cgroup_taskset_migrate(&tset, cgrp);
  2242. }
  2243. /**
  2244. * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
  2245. * @dst_cgrp: the cgroup to attach to
  2246. * @leader: the task or the leader of the threadgroup to be attached
  2247. * @threadgroup: attach the whole threadgroup?
  2248. *
  2249. * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
  2250. */
  2251. static int cgroup_attach_task(struct cgroup *dst_cgrp,
  2252. struct task_struct *leader, bool threadgroup)
  2253. {
  2254. LIST_HEAD(preloaded_csets);
  2255. struct task_struct *task;
  2256. int ret;
  2257. /* look up all src csets */
  2258. spin_lock_bh(&css_set_lock);
  2259. rcu_read_lock();
  2260. task = leader;
  2261. do {
  2262. cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
  2263. &preloaded_csets);
  2264. if (!threadgroup)
  2265. break;
  2266. } while_each_thread(leader, task);
  2267. rcu_read_unlock();
  2268. spin_unlock_bh(&css_set_lock);
  2269. /* prepare dst csets and commit */
  2270. ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
  2271. if (!ret)
  2272. ret = cgroup_migrate(leader, threadgroup, dst_cgrp);
  2273. cgroup_migrate_finish(&preloaded_csets);
  2274. return ret;
  2275. }
  2276. static int cgroup_procs_write_permission(struct task_struct *task,
  2277. struct cgroup *dst_cgrp,
  2278. struct kernfs_open_file *of)
  2279. {
  2280. const struct cred *cred = current_cred();
  2281. const struct cred *tcred = get_task_cred(task);
  2282. int ret = 0;
  2283. /*
  2284. * even if we're attaching all tasks in the thread group, we only
  2285. * need to check permissions on one of them.
  2286. */
  2287. if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
  2288. !uid_eq(cred->euid, tcred->uid) &&
  2289. !uid_eq(cred->euid, tcred->suid))
  2290. ret = -EACCES;
  2291. if (!ret && cgroup_on_dfl(dst_cgrp)) {
  2292. struct super_block *sb = of->file->f_path.dentry->d_sb;
  2293. struct cgroup *cgrp;
  2294. struct inode *inode;
  2295. spin_lock_bh(&css_set_lock);
  2296. cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
  2297. spin_unlock_bh(&css_set_lock);
  2298. while (!cgroup_is_descendant(dst_cgrp, cgrp))
  2299. cgrp = cgroup_parent(cgrp);
  2300. ret = -ENOMEM;
  2301. inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
  2302. if (inode) {
  2303. ret = inode_permission(inode, MAY_WRITE);
  2304. iput(inode);
  2305. }
  2306. }
  2307. put_cred(tcred);
  2308. return ret;
  2309. }
  2310. /*
  2311. * Find the task_struct of the task to attach by vpid and pass it along to the
  2312. * function to attach either it or all tasks in its threadgroup. Will lock
  2313. * cgroup_mutex and threadgroup.
  2314. */
  2315. static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
  2316. size_t nbytes, loff_t off, bool threadgroup)
  2317. {
  2318. struct task_struct *tsk;
  2319. struct cgroup *cgrp;
  2320. pid_t pid;
  2321. int ret;
  2322. if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
  2323. return -EINVAL;
  2324. cgrp = cgroup_kn_lock_live(of->kn);
  2325. if (!cgrp)
  2326. return -ENODEV;
  2327. percpu_down_write(&cgroup_threadgroup_rwsem);
  2328. rcu_read_lock();
  2329. if (pid) {
  2330. tsk = find_task_by_vpid(pid);
  2331. if (!tsk) {
  2332. ret = -ESRCH;
  2333. goto out_unlock_rcu;
  2334. }
  2335. } else {
  2336. tsk = current;
  2337. }
  2338. if (threadgroup)
  2339. tsk = tsk->group_leader;
  2340. /*
  2341. * Workqueue threads may acquire PF_NO_SETAFFINITY and become
  2342. * trapped in a cpuset, or RT worker may be born in a cgroup
  2343. * with no rt_runtime allocated. Just say no.
  2344. */
  2345. if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
  2346. ret = -EINVAL;
  2347. goto out_unlock_rcu;
  2348. }
  2349. get_task_struct(tsk);
  2350. rcu_read_unlock();
  2351. ret = cgroup_procs_write_permission(tsk, cgrp, of);
  2352. if (!ret)
  2353. ret = cgroup_attach_task(cgrp, tsk, threadgroup);
  2354. put_task_struct(tsk);
  2355. goto out_unlock_threadgroup;
  2356. out_unlock_rcu:
  2357. rcu_read_unlock();
  2358. out_unlock_threadgroup:
  2359. percpu_up_write(&cgroup_threadgroup_rwsem);
  2360. cgroup_kn_unlock(of->kn);
  2361. return ret ?: nbytes;
  2362. }
  2363. /**
  2364. * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
  2365. * @from: attach to all cgroups of a given task
  2366. * @tsk: the task to be attached
  2367. */
  2368. int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
  2369. {
  2370. struct cgroup_root *root;
  2371. int retval = 0;
  2372. mutex_lock(&cgroup_mutex);
  2373. for_each_root(root) {
  2374. struct cgroup *from_cgrp;
  2375. if (root == &cgrp_dfl_root)
  2376. continue;
  2377. spin_lock_bh(&css_set_lock);
  2378. from_cgrp = task_cgroup_from_root(from, root);
  2379. spin_unlock_bh(&css_set_lock);
  2380. retval = cgroup_attach_task(from_cgrp, tsk, false);
  2381. if (retval)
  2382. break;
  2383. }
  2384. mutex_unlock(&cgroup_mutex);
  2385. return retval;
  2386. }
  2387. EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
  2388. static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
  2389. char *buf, size_t nbytes, loff_t off)
  2390. {
  2391. return __cgroup_procs_write(of, buf, nbytes, off, false);
  2392. }
  2393. static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
  2394. char *buf, size_t nbytes, loff_t off)
  2395. {
  2396. return __cgroup_procs_write(of, buf, nbytes, off, true);
  2397. }
  2398. static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
  2399. char *buf, size_t nbytes, loff_t off)
  2400. {
  2401. struct cgroup *cgrp;
  2402. BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
  2403. cgrp = cgroup_kn_lock_live(of->kn);
  2404. if (!cgrp)
  2405. return -ENODEV;
  2406. spin_lock(&release_agent_path_lock);
  2407. strlcpy(cgrp->root->release_agent_path, strstrip(buf),
  2408. sizeof(cgrp->root->release_agent_path));
  2409. spin_unlock(&release_agent_path_lock);
  2410. cgroup_kn_unlock(of->kn);
  2411. return nbytes;
  2412. }
  2413. static int cgroup_release_agent_show(struct seq_file *seq, void *v)
  2414. {
  2415. struct cgroup *cgrp = seq_css(seq)->cgroup;
  2416. spin_lock(&release_agent_path_lock);
  2417. seq_puts(seq, cgrp->root->release_agent_path);
  2418. spin_unlock(&release_agent_path_lock);
  2419. seq_putc(seq, '\n');
  2420. return 0;
  2421. }
  2422. static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
  2423. {
  2424. seq_puts(seq, "0\n");
  2425. return 0;
  2426. }
  2427. static void cgroup_print_ss_mask(struct seq_file *seq, unsigned long ss_mask)
  2428. {
  2429. struct cgroup_subsys *ss;
  2430. bool printed = false;
  2431. int ssid;
  2432. for_each_subsys_which(ss, ssid, &ss_mask) {
  2433. if (printed)
  2434. seq_putc(seq, ' ');
  2435. seq_printf(seq, "%s", ss->name);
  2436. printed = true;
  2437. }
  2438. if (printed)
  2439. seq_putc(seq, '\n');
  2440. }
  2441. /* show controllers which are currently attached to the default hierarchy */
  2442. static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
  2443. {
  2444. struct cgroup *cgrp = seq_css(seq)->cgroup;
  2445. cgroup_print_ss_mask(seq, cgrp->root->subsys_mask &
  2446. ~cgrp_dfl_root_inhibit_ss_mask);
  2447. return 0;
  2448. }
  2449. /* show controllers which are enabled from the parent */
  2450. static int cgroup_controllers_show(struct seq_file *seq, void *v)
  2451. {
  2452. struct cgroup *cgrp = seq_css(seq)->cgroup;
  2453. cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->subtree_control);
  2454. return 0;
  2455. }
  2456. /* show controllers which are enabled for a given cgroup's children */
  2457. static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
  2458. {
  2459. struct cgroup *cgrp = seq_css(seq)->cgroup;
  2460. cgroup_print_ss_mask(seq, cgrp->subtree_control);
  2461. return 0;
  2462. }
  2463. /**
  2464. * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
  2465. * @cgrp: root of the subtree to update csses for
  2466. *
  2467. * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
  2468. * css associations need to be updated accordingly. This function looks up
  2469. * all css_sets which are attached to the subtree, creates the matching
  2470. * updated css_sets and migrates the tasks to the new ones.
  2471. */
  2472. static int cgroup_update_dfl_csses(struct cgroup *cgrp)
  2473. {
  2474. LIST_HEAD(preloaded_csets);
  2475. struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
  2476. struct cgroup_subsys_state *css;
  2477. struct css_set *src_cset;
  2478. int ret;
  2479. lockdep_assert_held(&cgroup_mutex);
  2480. percpu_down_write(&cgroup_threadgroup_rwsem);
  2481. /* look up all csses currently attached to @cgrp's subtree */
  2482. spin_lock_bh(&css_set_lock);
  2483. css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
  2484. struct cgrp_cset_link *link;
  2485. /* self is not affected by child_subsys_mask change */
  2486. if (css->cgroup == cgrp)
  2487. continue;
  2488. list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
  2489. cgroup_migrate_add_src(link->cset, cgrp,
  2490. &preloaded_csets);
  2491. }
  2492. spin_unlock_bh(&css_set_lock);
  2493. /* NULL dst indicates self on default hierarchy */
  2494. ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
  2495. if (ret)
  2496. goto out_finish;
  2497. spin_lock_bh(&css_set_lock);
  2498. list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
  2499. struct task_struct *task, *ntask;
  2500. /* src_csets precede dst_csets, break on the first dst_cset */
  2501. if (!src_cset->mg_src_cgrp)
  2502. break;
  2503. /* all tasks in src_csets need to be migrated */
  2504. list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
  2505. cgroup_taskset_add(task, &tset);
  2506. }
  2507. spin_unlock_bh(&css_set_lock);
  2508. ret = cgroup_taskset_migrate(&tset, cgrp);
  2509. out_finish:
  2510. cgroup_migrate_finish(&preloaded_csets);
  2511. percpu_up_write(&cgroup_threadgroup_rwsem);
  2512. return ret;
  2513. }
  2514. /* change the enabled child controllers for a cgroup in the default hierarchy */
  2515. static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
  2516. char *buf, size_t nbytes,
  2517. loff_t off)
  2518. {
  2519. unsigned long enable = 0, disable = 0;
  2520. unsigned long css_enable, css_disable, old_sc, new_sc, old_ss, new_ss;
  2521. struct cgroup *cgrp, *child;
  2522. struct cgroup_subsys *ss;
  2523. char *tok;
  2524. int ssid, ret;
  2525. /*
  2526. * Parse input - space separated list of subsystem names prefixed
  2527. * with either + or -.
  2528. */
  2529. buf = strstrip(buf);
  2530. while ((tok = strsep(&buf, " "))) {
  2531. unsigned long tmp_ss_mask = ~cgrp_dfl_root_inhibit_ss_mask;
  2532. if (tok[0] == '\0')
  2533. continue;
  2534. for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
  2535. if (!cgroup_ssid_enabled(ssid) ||
  2536. strcmp(tok + 1, ss->name))
  2537. continue;
  2538. if (*tok == '+') {
  2539. enable |= 1 << ssid;
  2540. disable &= ~(1 << ssid);
  2541. } else if (*tok == '-') {
  2542. disable |= 1 << ssid;
  2543. enable &= ~(1 << ssid);
  2544. } else {
  2545. return -EINVAL;
  2546. }
  2547. break;
  2548. }
  2549. if (ssid == CGROUP_SUBSYS_COUNT)
  2550. return -EINVAL;
  2551. }
  2552. cgrp = cgroup_kn_lock_live(of->kn);
  2553. if (!cgrp)
  2554. return -ENODEV;
  2555. for_each_subsys(ss, ssid) {
  2556. if (enable & (1 << ssid)) {
  2557. if (cgrp->subtree_control & (1 << ssid)) {
  2558. enable &= ~(1 << ssid);
  2559. continue;
  2560. }
  2561. /* unavailable or not enabled on the parent? */
  2562. if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
  2563. (cgroup_parent(cgrp) &&
  2564. !(cgroup_parent(cgrp)->subtree_control & (1 << ssid)))) {
  2565. ret = -ENOENT;
  2566. goto out_unlock;
  2567. }
  2568. } else if (disable & (1 << ssid)) {
  2569. if (!(cgrp->subtree_control & (1 << ssid))) {
  2570. disable &= ~(1 << ssid);
  2571. continue;
  2572. }
  2573. /* a child has it enabled? */
  2574. cgroup_for_each_live_child(child, cgrp) {
  2575. if (child->subtree_control & (1 << ssid)) {
  2576. ret = -EBUSY;
  2577. goto out_unlock;
  2578. }
  2579. }
  2580. }
  2581. }
  2582. if (!enable && !disable) {
  2583. ret = 0;
  2584. goto out_unlock;
  2585. }
  2586. /*
  2587. * Except for the root, subtree_control must be zero for a cgroup
  2588. * with tasks so that child cgroups don't compete against tasks.
  2589. */
  2590. if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
  2591. ret = -EBUSY;
  2592. goto out_unlock;
  2593. }
  2594. /*
  2595. * Update subsys masks and calculate what needs to be done. More
  2596. * subsystems than specified may need to be enabled or disabled
  2597. * depending on subsystem dependencies.
  2598. */
  2599. old_sc = cgrp->subtree_control;
  2600. old_ss = cgrp->child_subsys_mask;
  2601. new_sc = (old_sc | enable) & ~disable;
  2602. new_ss = cgroup_calc_child_subsys_mask(cgrp, new_sc);
  2603. css_enable = ~old_ss & new_ss;
  2604. css_disable = old_ss & ~new_ss;
  2605. enable |= css_enable;
  2606. disable |= css_disable;
  2607. /*
  2608. * Because css offlining is asynchronous, userland might try to
  2609. * re-enable the same controller while the previous instance is
  2610. * still around. In such cases, wait till it's gone using
  2611. * offline_waitq.
  2612. */
  2613. for_each_subsys_which(ss, ssid, &css_enable) {
  2614. cgroup_for_each_live_child(child, cgrp) {
  2615. DEFINE_WAIT(wait);
  2616. if (!cgroup_css(child, ss))
  2617. continue;
  2618. cgroup_get(child);
  2619. prepare_to_wait(&child->offline_waitq, &wait,
  2620. TASK_UNINTERRUPTIBLE);
  2621. cgroup_kn_unlock(of->kn);
  2622. schedule();
  2623. finish_wait(&child->offline_waitq, &wait);
  2624. cgroup_put(child);
  2625. return restart_syscall();
  2626. }
  2627. }
  2628. cgrp->subtree_control = new_sc;
  2629. cgrp->child_subsys_mask = new_ss;
  2630. /*
  2631. * Create new csses or make the existing ones visible. A css is
  2632. * created invisible if it's being implicitly enabled through
  2633. * dependency. An invisible css is made visible when the userland
  2634. * explicitly enables it.
  2635. */
  2636. for_each_subsys(ss, ssid) {
  2637. if (!(enable & (1 << ssid)))
  2638. continue;
  2639. cgroup_for_each_live_child(child, cgrp) {
  2640. if (css_enable & (1 << ssid))
  2641. ret = create_css(child, ss,
  2642. cgrp->subtree_control & (1 << ssid));
  2643. else
  2644. ret = css_populate_dir(cgroup_css(child, ss),
  2645. NULL);
  2646. if (ret)
  2647. goto err_undo_css;
  2648. }
  2649. }
  2650. /*
  2651. * At this point, cgroup_e_css() results reflect the new csses
  2652. * making the following cgroup_update_dfl_csses() properly update
  2653. * css associations of all tasks in the subtree.
  2654. */
  2655. ret = cgroup_update_dfl_csses(cgrp);
  2656. if (ret)
  2657. goto err_undo_css;
  2658. /*
  2659. * All tasks are migrated out of disabled csses. Kill or hide
  2660. * them. A css is hidden when the userland requests it to be
  2661. * disabled while other subsystems are still depending on it. The
  2662. * css must not actively control resources and be in the vanilla
  2663. * state if it's made visible again later. Controllers which may
  2664. * be depended upon should provide ->css_reset() for this purpose.
  2665. */
  2666. for_each_subsys(ss, ssid) {
  2667. if (!(disable & (1 << ssid)))
  2668. continue;
  2669. cgroup_for_each_live_child(child, cgrp) {
  2670. struct cgroup_subsys_state *css = cgroup_css(child, ss);
  2671. if (css_disable & (1 << ssid)) {
  2672. kill_css(css);
  2673. } else {
  2674. css_clear_dir(css, NULL);
  2675. if (ss->css_reset)
  2676. ss->css_reset(css);
  2677. }
  2678. }
  2679. }
  2680. /*
  2681. * The effective csses of all the descendants (excluding @cgrp) may
  2682. * have changed. Subsystems can optionally subscribe to this event
  2683. * by implementing ->css_e_css_changed() which is invoked if any of
  2684. * the effective csses seen from the css's cgroup may have changed.
  2685. */
  2686. for_each_subsys(ss, ssid) {
  2687. struct cgroup_subsys_state *this_css = cgroup_css(cgrp, ss);
  2688. struct cgroup_subsys_state *css;
  2689. if (!ss->css_e_css_changed || !this_css)
  2690. continue;
  2691. css_for_each_descendant_pre(css, this_css)
  2692. if (css != this_css)
  2693. ss->css_e_css_changed(css);
  2694. }
  2695. kernfs_activate(cgrp->kn);
  2696. ret = 0;
  2697. out_unlock:
  2698. cgroup_kn_unlock(of->kn);
  2699. return ret ?: nbytes;
  2700. err_undo_css:
  2701. cgrp->subtree_control = old_sc;
  2702. cgrp->child_subsys_mask = old_ss;
  2703. for_each_subsys(ss, ssid) {
  2704. if (!(enable & (1 << ssid)))
  2705. continue;
  2706. cgroup_for_each_live_child(child, cgrp) {
  2707. struct cgroup_subsys_state *css = cgroup_css(child, ss);
  2708. if (!css)
  2709. continue;
  2710. if (css_enable & (1 << ssid))
  2711. kill_css(css);
  2712. else
  2713. css_clear_dir(css, NULL);
  2714. }
  2715. }
  2716. goto out_unlock;
  2717. }
  2718. static int cgroup_events_show(struct seq_file *seq, void *v)
  2719. {
  2720. seq_printf(seq, "populated %d\n",
  2721. cgroup_is_populated(seq_css(seq)->cgroup));
  2722. return 0;
  2723. }
  2724. static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
  2725. size_t nbytes, loff_t off)
  2726. {
  2727. struct cgroup *cgrp = of->kn->parent->priv;
  2728. struct cftype *cft = of->kn->priv;
  2729. struct cgroup_subsys_state *css;
  2730. int ret;
  2731. if (cft->write)
  2732. return cft->write(of, buf, nbytes, off);
  2733. /*
  2734. * kernfs guarantees that a file isn't deleted with operations in
  2735. * flight, which means that the matching css is and stays alive and
  2736. * doesn't need to be pinned. The RCU locking is not necessary
  2737. * either. It's just for the convenience of using cgroup_css().
  2738. */
  2739. rcu_read_lock();
  2740. css = cgroup_css(cgrp, cft->ss);
  2741. rcu_read_unlock();
  2742. if (cft->write_u64) {
  2743. unsigned long long v;
  2744. ret = kstrtoull(buf, 0, &v);
  2745. if (!ret)
  2746. ret = cft->write_u64(css, cft, v);
  2747. } else if (cft->write_s64) {
  2748. long long v;
  2749. ret = kstrtoll(buf, 0, &v);
  2750. if (!ret)
  2751. ret = cft->write_s64(css, cft, v);
  2752. } else {
  2753. ret = -EINVAL;
  2754. }
  2755. return ret ?: nbytes;
  2756. }
  2757. static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
  2758. {
  2759. return seq_cft(seq)->seq_start(seq, ppos);
  2760. }
  2761. static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
  2762. {
  2763. return seq_cft(seq)->seq_next(seq, v, ppos);
  2764. }
  2765. static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
  2766. {
  2767. seq_cft(seq)->seq_stop(seq, v);
  2768. }
  2769. static int cgroup_seqfile_show(struct seq_file *m, void *arg)
  2770. {
  2771. struct cftype *cft = seq_cft(m);
  2772. struct cgroup_subsys_state *css = seq_css(m);
  2773. if (cft->seq_show)
  2774. return cft->seq_show(m, arg);
  2775. if (cft->read_u64)
  2776. seq_printf(m, "%llu\n", cft->read_u64(css, cft));
  2777. else if (cft->read_s64)
  2778. seq_printf(m, "%lld\n", cft->read_s64(css, cft));
  2779. else
  2780. return -EINVAL;
  2781. return 0;
  2782. }
  2783. static struct kernfs_ops cgroup_kf_single_ops = {
  2784. .atomic_write_len = PAGE_SIZE,
  2785. .write = cgroup_file_write,
  2786. .seq_show = cgroup_seqfile_show,
  2787. };
  2788. static struct kernfs_ops cgroup_kf_ops = {
  2789. .atomic_write_len = PAGE_SIZE,
  2790. .write = cgroup_file_write,
  2791. .seq_start = cgroup_seqfile_start,
  2792. .seq_next = cgroup_seqfile_next,
  2793. .seq_stop = cgroup_seqfile_stop,
  2794. .seq_show = cgroup_seqfile_show,
  2795. };
  2796. /*
  2797. * cgroup_rename - Only allow simple rename of directories in place.
  2798. */
  2799. static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
  2800. const char *new_name_str)
  2801. {
  2802. struct cgroup *cgrp = kn->priv;
  2803. int ret;
  2804. if (kernfs_type(kn) != KERNFS_DIR)
  2805. return -ENOTDIR;
  2806. if (kn->parent != new_parent)
  2807. return -EIO;
  2808. /*
  2809. * This isn't a proper migration and its usefulness is very
  2810. * limited. Disallow on the default hierarchy.
  2811. */
  2812. if (cgroup_on_dfl(cgrp))
  2813. return -EPERM;
  2814. /*
  2815. * We're gonna grab cgroup_mutex which nests outside kernfs
  2816. * active_ref. kernfs_rename() doesn't require active_ref
  2817. * protection. Break them before grabbing cgroup_mutex.
  2818. */
  2819. kernfs_break_active_protection(new_parent);
  2820. kernfs_break_active_protection(kn);
  2821. mutex_lock(&cgroup_mutex);
  2822. ret = kernfs_rename(kn, new_parent, new_name_str);
  2823. mutex_unlock(&cgroup_mutex);
  2824. kernfs_unbreak_active_protection(kn);
  2825. kernfs_unbreak_active_protection(new_parent);
  2826. return ret;
  2827. }
  2828. /* set uid and gid of cgroup dirs and files to that of the creator */
  2829. static int cgroup_kn_set_ugid(struct kernfs_node *kn)
  2830. {
  2831. struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
  2832. .ia_uid = current_fsuid(),
  2833. .ia_gid = current_fsgid(), };
  2834. if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
  2835. gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
  2836. return 0;
  2837. return kernfs_setattr(kn, &iattr);
  2838. }
  2839. static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
  2840. struct cftype *cft)
  2841. {
  2842. char name[CGROUP_FILE_NAME_MAX];
  2843. struct kernfs_node *kn;
  2844. struct lock_class_key *key = NULL;
  2845. int ret;
  2846. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  2847. key = &cft->lockdep_key;
  2848. #endif
  2849. kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
  2850. cgroup_file_mode(cft), 0, cft->kf_ops, cft,
  2851. NULL, key);
  2852. if (IS_ERR(kn))
  2853. return PTR_ERR(kn);
  2854. ret = cgroup_kn_set_ugid(kn);
  2855. if (ret) {
  2856. kernfs_remove(kn);
  2857. return ret;
  2858. }
  2859. if (cft->file_offset) {
  2860. struct cgroup_file *cfile = (void *)css + cft->file_offset;
  2861. spin_lock_irq(&cgroup_file_kn_lock);
  2862. cfile->kn = kn;
  2863. spin_unlock_irq(&cgroup_file_kn_lock);
  2864. }
  2865. return 0;
  2866. }
  2867. /**
  2868. * cgroup_addrm_files - add or remove files to a cgroup directory
  2869. * @css: the target css
  2870. * @cgrp: the target cgroup (usually css->cgroup)
  2871. * @cfts: array of cftypes to be added
  2872. * @is_add: whether to add or remove
  2873. *
  2874. * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
  2875. * For removals, this function never fails.
  2876. */
  2877. static int cgroup_addrm_files(struct cgroup_subsys_state *css,
  2878. struct cgroup *cgrp, struct cftype cfts[],
  2879. bool is_add)
  2880. {
  2881. struct cftype *cft, *cft_end = NULL;
  2882. int ret;
  2883. lockdep_assert_held(&cgroup_mutex);
  2884. restart:
  2885. for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
  2886. /* does cft->flags tell us to skip this file on @cgrp? */
  2887. if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
  2888. continue;
  2889. if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
  2890. continue;
  2891. if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
  2892. continue;
  2893. if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
  2894. continue;
  2895. if (is_add) {
  2896. ret = cgroup_add_file(css, cgrp, cft);
  2897. if (ret) {
  2898. pr_warn("%s: failed to add %s, err=%d\n",
  2899. __func__, cft->name, ret);
  2900. cft_end = cft;
  2901. is_add = false;
  2902. goto restart;
  2903. }
  2904. } else {
  2905. cgroup_rm_file(cgrp, cft);
  2906. }
  2907. }
  2908. return 0;
  2909. }
  2910. static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
  2911. {
  2912. LIST_HEAD(pending);
  2913. struct cgroup_subsys *ss = cfts[0].ss;
  2914. struct cgroup *root = &ss->root->cgrp;
  2915. struct cgroup_subsys_state *css;
  2916. int ret = 0;
  2917. lockdep_assert_held(&cgroup_mutex);
  2918. /* add/rm files for all cgroups created before */
  2919. css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
  2920. struct cgroup *cgrp = css->cgroup;
  2921. if (cgroup_is_dead(cgrp))
  2922. continue;
  2923. ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
  2924. if (ret)
  2925. break;
  2926. }
  2927. if (is_add && !ret)
  2928. kernfs_activate(root->kn);
  2929. return ret;
  2930. }
  2931. static void cgroup_exit_cftypes(struct cftype *cfts)
  2932. {
  2933. struct cftype *cft;
  2934. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  2935. /* free copy for custom atomic_write_len, see init_cftypes() */
  2936. if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
  2937. kfree(cft->kf_ops);
  2938. cft->kf_ops = NULL;
  2939. cft->ss = NULL;
  2940. /* revert flags set by cgroup core while adding @cfts */
  2941. cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
  2942. }
  2943. }
  2944. static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  2945. {
  2946. struct cftype *cft;
  2947. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  2948. struct kernfs_ops *kf_ops;
  2949. WARN_ON(cft->ss || cft->kf_ops);
  2950. if (cft->seq_start)
  2951. kf_ops = &cgroup_kf_ops;
  2952. else
  2953. kf_ops = &cgroup_kf_single_ops;
  2954. /*
  2955. * Ugh... if @cft wants a custom max_write_len, we need to
  2956. * make a copy of kf_ops to set its atomic_write_len.
  2957. */
  2958. if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
  2959. kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
  2960. if (!kf_ops) {
  2961. cgroup_exit_cftypes(cfts);
  2962. return -ENOMEM;
  2963. }
  2964. kf_ops->atomic_write_len = cft->max_write_len;
  2965. }
  2966. cft->kf_ops = kf_ops;
  2967. cft->ss = ss;
  2968. }
  2969. return 0;
  2970. }
  2971. static int cgroup_rm_cftypes_locked(struct cftype *cfts)
  2972. {
  2973. lockdep_assert_held(&cgroup_mutex);
  2974. if (!cfts || !cfts[0].ss)
  2975. return -ENOENT;
  2976. list_del(&cfts->node);
  2977. cgroup_apply_cftypes(cfts, false);
  2978. cgroup_exit_cftypes(cfts);
  2979. return 0;
  2980. }
  2981. /**
  2982. * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
  2983. * @cfts: zero-length name terminated array of cftypes
  2984. *
  2985. * Unregister @cfts. Files described by @cfts are removed from all
  2986. * existing cgroups and all future cgroups won't have them either. This
  2987. * function can be called anytime whether @cfts' subsys is attached or not.
  2988. *
  2989. * Returns 0 on successful unregistration, -ENOENT if @cfts is not
  2990. * registered.
  2991. */
  2992. int cgroup_rm_cftypes(struct cftype *cfts)
  2993. {
  2994. int ret;
  2995. mutex_lock(&cgroup_mutex);
  2996. ret = cgroup_rm_cftypes_locked(cfts);
  2997. mutex_unlock(&cgroup_mutex);
  2998. return ret;
  2999. }
  3000. /**
  3001. * cgroup_add_cftypes - add an array of cftypes to a subsystem
  3002. * @ss: target cgroup subsystem
  3003. * @cfts: zero-length name terminated array of cftypes
  3004. *
  3005. * Register @cfts to @ss. Files described by @cfts are created for all
  3006. * existing cgroups to which @ss is attached and all future cgroups will
  3007. * have them too. This function can be called anytime whether @ss is
  3008. * attached or not.
  3009. *
  3010. * Returns 0 on successful registration, -errno on failure. Note that this
  3011. * function currently returns 0 as long as @cfts registration is successful
  3012. * even if some file creation attempts on existing cgroups fail.
  3013. */
  3014. static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  3015. {
  3016. int ret;
  3017. if (!cgroup_ssid_enabled(ss->id))
  3018. return 0;
  3019. if (!cfts || cfts[0].name[0] == '\0')
  3020. return 0;
  3021. ret = cgroup_init_cftypes(ss, cfts);
  3022. if (ret)
  3023. return ret;
  3024. mutex_lock(&cgroup_mutex);
  3025. list_add_tail(&cfts->node, &ss->cfts);
  3026. ret = cgroup_apply_cftypes(cfts, true);
  3027. if (ret)
  3028. cgroup_rm_cftypes_locked(cfts);
  3029. mutex_unlock(&cgroup_mutex);
  3030. return ret;
  3031. }
  3032. /**
  3033. * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
  3034. * @ss: target cgroup subsystem
  3035. * @cfts: zero-length name terminated array of cftypes
  3036. *
  3037. * Similar to cgroup_add_cftypes() but the added files are only used for
  3038. * the default hierarchy.
  3039. */
  3040. int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  3041. {
  3042. struct cftype *cft;
  3043. for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
  3044. cft->flags |= __CFTYPE_ONLY_ON_DFL;
  3045. return cgroup_add_cftypes(ss, cfts);
  3046. }
  3047. /**
  3048. * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
  3049. * @ss: target cgroup subsystem
  3050. * @cfts: zero-length name terminated array of cftypes
  3051. *
  3052. * Similar to cgroup_add_cftypes() but the added files are only used for
  3053. * the legacy hierarchies.
  3054. */
  3055. int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  3056. {
  3057. struct cftype *cft;
  3058. for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
  3059. cft->flags |= __CFTYPE_NOT_ON_DFL;
  3060. return cgroup_add_cftypes(ss, cfts);
  3061. }
  3062. /**
  3063. * cgroup_file_notify - generate a file modified event for a cgroup_file
  3064. * @cfile: target cgroup_file
  3065. *
  3066. * @cfile must have been obtained by setting cftype->file_offset.
  3067. */
  3068. void cgroup_file_notify(struct cgroup_file *cfile)
  3069. {
  3070. unsigned long flags;
  3071. spin_lock_irqsave(&cgroup_file_kn_lock, flags);
  3072. if (cfile->kn)
  3073. kernfs_notify(cfile->kn);
  3074. spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
  3075. }
  3076. /**
  3077. * cgroup_task_count - count the number of tasks in a cgroup.
  3078. * @cgrp: the cgroup in question
  3079. *
  3080. * Return the number of tasks in the cgroup.
  3081. */
  3082. static int cgroup_task_count(const struct cgroup *cgrp)
  3083. {
  3084. int count = 0;
  3085. struct cgrp_cset_link *link;
  3086. spin_lock_bh(&css_set_lock);
  3087. list_for_each_entry(link, &cgrp->cset_links, cset_link)
  3088. count += atomic_read(&link->cset->refcount);
  3089. spin_unlock_bh(&css_set_lock);
  3090. return count;
  3091. }
  3092. /**
  3093. * css_next_child - find the next child of a given css
  3094. * @pos: the current position (%NULL to initiate traversal)
  3095. * @parent: css whose children to walk
  3096. *
  3097. * This function returns the next child of @parent and should be called
  3098. * under either cgroup_mutex or RCU read lock. The only requirement is
  3099. * that @parent and @pos are accessible. The next sibling is guaranteed to
  3100. * be returned regardless of their states.
  3101. *
  3102. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  3103. * css which finished ->css_online() is guaranteed to be visible in the
  3104. * future iterations and will stay visible until the last reference is put.
  3105. * A css which hasn't finished ->css_online() or already finished
  3106. * ->css_offline() may show up during traversal. It's each subsystem's
  3107. * responsibility to synchronize against on/offlining.
  3108. */
  3109. struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
  3110. struct cgroup_subsys_state *parent)
  3111. {
  3112. struct cgroup_subsys_state *next;
  3113. cgroup_assert_mutex_or_rcu_locked();
  3114. /*
  3115. * @pos could already have been unlinked from the sibling list.
  3116. * Once a cgroup is removed, its ->sibling.next is no longer
  3117. * updated when its next sibling changes. CSS_RELEASED is set when
  3118. * @pos is taken off list, at which time its next pointer is valid,
  3119. * and, as releases are serialized, the one pointed to by the next
  3120. * pointer is guaranteed to not have started release yet. This
  3121. * implies that if we observe !CSS_RELEASED on @pos in this RCU
  3122. * critical section, the one pointed to by its next pointer is
  3123. * guaranteed to not have finished its RCU grace period even if we
  3124. * have dropped rcu_read_lock() inbetween iterations.
  3125. *
  3126. * If @pos has CSS_RELEASED set, its next pointer can't be
  3127. * dereferenced; however, as each css is given a monotonically
  3128. * increasing unique serial number and always appended to the
  3129. * sibling list, the next one can be found by walking the parent's
  3130. * children until the first css with higher serial number than
  3131. * @pos's. While this path can be slower, it happens iff iteration
  3132. * races against release and the race window is very small.
  3133. */
  3134. if (!pos) {
  3135. next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
  3136. } else if (likely(!(pos->flags & CSS_RELEASED))) {
  3137. next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
  3138. } else {
  3139. list_for_each_entry_rcu(next, &parent->children, sibling)
  3140. if (next->serial_nr > pos->serial_nr)
  3141. break;
  3142. }
  3143. /*
  3144. * @next, if not pointing to the head, can be dereferenced and is
  3145. * the next sibling.
  3146. */
  3147. if (&next->sibling != &parent->children)
  3148. return next;
  3149. return NULL;
  3150. }
  3151. /**
  3152. * css_next_descendant_pre - find the next descendant for pre-order walk
  3153. * @pos: the current position (%NULL to initiate traversal)
  3154. * @root: css whose descendants to walk
  3155. *
  3156. * To be used by css_for_each_descendant_pre(). Find the next descendant
  3157. * to visit for pre-order traversal of @root's descendants. @root is
  3158. * included in the iteration and the first node to be visited.
  3159. *
  3160. * While this function requires cgroup_mutex or RCU read locking, it
  3161. * doesn't require the whole traversal to be contained in a single critical
  3162. * section. This function will return the correct next descendant as long
  3163. * as both @pos and @root are accessible and @pos is a descendant of @root.
  3164. *
  3165. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  3166. * css which finished ->css_online() is guaranteed to be visible in the
  3167. * future iterations and will stay visible until the last reference is put.
  3168. * A css which hasn't finished ->css_online() or already finished
  3169. * ->css_offline() may show up during traversal. It's each subsystem's
  3170. * responsibility to synchronize against on/offlining.
  3171. */
  3172. struct cgroup_subsys_state *
  3173. css_next_descendant_pre(struct cgroup_subsys_state *pos,
  3174. struct cgroup_subsys_state *root)
  3175. {
  3176. struct cgroup_subsys_state *next;
  3177. cgroup_assert_mutex_or_rcu_locked();
  3178. /* if first iteration, visit @root */
  3179. if (!pos)
  3180. return root;
  3181. /* visit the first child if exists */
  3182. next = css_next_child(NULL, pos);
  3183. if (next)
  3184. return next;
  3185. /* no child, visit my or the closest ancestor's next sibling */
  3186. while (pos != root) {
  3187. next = css_next_child(pos, pos->parent);
  3188. if (next)
  3189. return next;
  3190. pos = pos->parent;
  3191. }
  3192. return NULL;
  3193. }
  3194. /**
  3195. * css_rightmost_descendant - return the rightmost descendant of a css
  3196. * @pos: css of interest
  3197. *
  3198. * Return the rightmost descendant of @pos. If there's no descendant, @pos
  3199. * is returned. This can be used during pre-order traversal to skip
  3200. * subtree of @pos.
  3201. *
  3202. * While this function requires cgroup_mutex or RCU read locking, it
  3203. * doesn't require the whole traversal to be contained in a single critical
  3204. * section. This function will return the correct rightmost descendant as
  3205. * long as @pos is accessible.
  3206. */
  3207. struct cgroup_subsys_state *
  3208. css_rightmost_descendant(struct cgroup_subsys_state *pos)
  3209. {
  3210. struct cgroup_subsys_state *last, *tmp;
  3211. cgroup_assert_mutex_or_rcu_locked();
  3212. do {
  3213. last = pos;
  3214. /* ->prev isn't RCU safe, walk ->next till the end */
  3215. pos = NULL;
  3216. css_for_each_child(tmp, last)
  3217. pos = tmp;
  3218. } while (pos);
  3219. return last;
  3220. }
  3221. static struct cgroup_subsys_state *
  3222. css_leftmost_descendant(struct cgroup_subsys_state *pos)
  3223. {
  3224. struct cgroup_subsys_state *last;
  3225. do {
  3226. last = pos;
  3227. pos = css_next_child(NULL, pos);
  3228. } while (pos);
  3229. return last;
  3230. }
  3231. /**
  3232. * css_next_descendant_post - find the next descendant for post-order walk
  3233. * @pos: the current position (%NULL to initiate traversal)
  3234. * @root: css whose descendants to walk
  3235. *
  3236. * To be used by css_for_each_descendant_post(). Find the next descendant
  3237. * to visit for post-order traversal of @root's descendants. @root is
  3238. * included in the iteration and the last node to be visited.
  3239. *
  3240. * While this function requires cgroup_mutex or RCU read locking, it
  3241. * doesn't require the whole traversal to be contained in a single critical
  3242. * section. This function will return the correct next descendant as long
  3243. * as both @pos and @cgroup are accessible and @pos is a descendant of
  3244. * @cgroup.
  3245. *
  3246. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  3247. * css which finished ->css_online() is guaranteed to be visible in the
  3248. * future iterations and will stay visible until the last reference is put.
  3249. * A css which hasn't finished ->css_online() or already finished
  3250. * ->css_offline() may show up during traversal. It's each subsystem's
  3251. * responsibility to synchronize against on/offlining.
  3252. */
  3253. struct cgroup_subsys_state *
  3254. css_next_descendant_post(struct cgroup_subsys_state *pos,
  3255. struct cgroup_subsys_state *root)
  3256. {
  3257. struct cgroup_subsys_state *next;
  3258. cgroup_assert_mutex_or_rcu_locked();
  3259. /* if first iteration, visit leftmost descendant which may be @root */
  3260. if (!pos)
  3261. return css_leftmost_descendant(root);
  3262. /* if we visited @root, we're done */
  3263. if (pos == root)
  3264. return NULL;
  3265. /* if there's an unvisited sibling, visit its leftmost descendant */
  3266. next = css_next_child(pos, pos->parent);
  3267. if (next)
  3268. return css_leftmost_descendant(next);
  3269. /* no sibling left, visit parent */
  3270. return pos->parent;
  3271. }
  3272. /**
  3273. * css_has_online_children - does a css have online children
  3274. * @css: the target css
  3275. *
  3276. * Returns %true if @css has any online children; otherwise, %false. This
  3277. * function can be called from any context but the caller is responsible
  3278. * for synchronizing against on/offlining as necessary.
  3279. */
  3280. bool css_has_online_children(struct cgroup_subsys_state *css)
  3281. {
  3282. struct cgroup_subsys_state *child;
  3283. bool ret = false;
  3284. rcu_read_lock();
  3285. css_for_each_child(child, css) {
  3286. if (child->flags & CSS_ONLINE) {
  3287. ret = true;
  3288. break;
  3289. }
  3290. }
  3291. rcu_read_unlock();
  3292. return ret;
  3293. }
  3294. /**
  3295. * css_task_iter_advance_css_set - advance a task itererator to the next css_set
  3296. * @it: the iterator to advance
  3297. *
  3298. * Advance @it to the next css_set to walk.
  3299. */
  3300. static void css_task_iter_advance_css_set(struct css_task_iter *it)
  3301. {
  3302. struct list_head *l = it->cset_pos;
  3303. struct cgrp_cset_link *link;
  3304. struct css_set *cset;
  3305. lockdep_assert_held(&css_set_lock);
  3306. /* Advance to the next non-empty css_set */
  3307. do {
  3308. l = l->next;
  3309. if (l == it->cset_head) {
  3310. it->cset_pos = NULL;
  3311. it->task_pos = NULL;
  3312. return;
  3313. }
  3314. if (it->ss) {
  3315. cset = container_of(l, struct css_set,
  3316. e_cset_node[it->ss->id]);
  3317. } else {
  3318. link = list_entry(l, struct cgrp_cset_link, cset_link);
  3319. cset = link->cset;
  3320. }
  3321. } while (!css_set_populated(cset));
  3322. it->cset_pos = l;
  3323. if (!list_empty(&cset->tasks))
  3324. it->task_pos = cset->tasks.next;
  3325. else
  3326. it->task_pos = cset->mg_tasks.next;
  3327. it->tasks_head = &cset->tasks;
  3328. it->mg_tasks_head = &cset->mg_tasks;
  3329. /*
  3330. * We don't keep css_sets locked across iteration steps and thus
  3331. * need to take steps to ensure that iteration can be resumed after
  3332. * the lock is re-acquired. Iteration is performed at two levels -
  3333. * css_sets and tasks in them.
  3334. *
  3335. * Once created, a css_set never leaves its cgroup lists, so a
  3336. * pinned css_set is guaranteed to stay put and we can resume
  3337. * iteration afterwards.
  3338. *
  3339. * Tasks may leave @cset across iteration steps. This is resolved
  3340. * by registering each iterator with the css_set currently being
  3341. * walked and making css_set_move_task() advance iterators whose
  3342. * next task is leaving.
  3343. */
  3344. if (it->cur_cset) {
  3345. list_del(&it->iters_node);
  3346. put_css_set_locked(it->cur_cset);
  3347. }
  3348. get_css_set(cset);
  3349. it->cur_cset = cset;
  3350. list_add(&it->iters_node, &cset->task_iters);
  3351. }
  3352. static void css_task_iter_advance(struct css_task_iter *it)
  3353. {
  3354. struct list_head *l = it->task_pos;
  3355. lockdep_assert_held(&css_set_lock);
  3356. WARN_ON_ONCE(!l);
  3357. /*
  3358. * Advance iterator to find next entry. cset->tasks is consumed
  3359. * first and then ->mg_tasks. After ->mg_tasks, we move onto the
  3360. * next cset.
  3361. */
  3362. l = l->next;
  3363. if (l == it->tasks_head)
  3364. l = it->mg_tasks_head->next;
  3365. if (l == it->mg_tasks_head)
  3366. css_task_iter_advance_css_set(it);
  3367. else
  3368. it->task_pos = l;
  3369. }
  3370. /**
  3371. * css_task_iter_start - initiate task iteration
  3372. * @css: the css to walk tasks of
  3373. * @it: the task iterator to use
  3374. *
  3375. * Initiate iteration through the tasks of @css. The caller can call
  3376. * css_task_iter_next() to walk through the tasks until the function
  3377. * returns NULL. On completion of iteration, css_task_iter_end() must be
  3378. * called.
  3379. */
  3380. void css_task_iter_start(struct cgroup_subsys_state *css,
  3381. struct css_task_iter *it)
  3382. {
  3383. /* no one should try to iterate before mounting cgroups */
  3384. WARN_ON_ONCE(!use_task_css_set_links);
  3385. memset(it, 0, sizeof(*it));
  3386. spin_lock_bh(&css_set_lock);
  3387. it->ss = css->ss;
  3388. if (it->ss)
  3389. it->cset_pos = &css->cgroup->e_csets[css->ss->id];
  3390. else
  3391. it->cset_pos = &css->cgroup->cset_links;
  3392. it->cset_head = it->cset_pos;
  3393. css_task_iter_advance_css_set(it);
  3394. spin_unlock_bh(&css_set_lock);
  3395. }
  3396. /**
  3397. * css_task_iter_next - return the next task for the iterator
  3398. * @it: the task iterator being iterated
  3399. *
  3400. * The "next" function for task iteration. @it should have been
  3401. * initialized via css_task_iter_start(). Returns NULL when the iteration
  3402. * reaches the end.
  3403. */
  3404. struct task_struct *css_task_iter_next(struct css_task_iter *it)
  3405. {
  3406. if (it->cur_task) {
  3407. put_task_struct(it->cur_task);
  3408. it->cur_task = NULL;
  3409. }
  3410. spin_lock_bh(&css_set_lock);
  3411. if (it->task_pos) {
  3412. it->cur_task = list_entry(it->task_pos, struct task_struct,
  3413. cg_list);
  3414. get_task_struct(it->cur_task);
  3415. css_task_iter_advance(it);
  3416. }
  3417. spin_unlock_bh(&css_set_lock);
  3418. return it->cur_task;
  3419. }
  3420. /**
  3421. * css_task_iter_end - finish task iteration
  3422. * @it: the task iterator to finish
  3423. *
  3424. * Finish task iteration started by css_task_iter_start().
  3425. */
  3426. void css_task_iter_end(struct css_task_iter *it)
  3427. {
  3428. if (it->cur_cset) {
  3429. spin_lock_bh(&css_set_lock);
  3430. list_del(&it->iters_node);
  3431. put_css_set_locked(it->cur_cset);
  3432. spin_unlock_bh(&css_set_lock);
  3433. }
  3434. if (it->cur_task)
  3435. put_task_struct(it->cur_task);
  3436. }
  3437. /**
  3438. * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
  3439. * @to: cgroup to which the tasks will be moved
  3440. * @from: cgroup in which the tasks currently reside
  3441. *
  3442. * Locking rules between cgroup_post_fork() and the migration path
  3443. * guarantee that, if a task is forking while being migrated, the new child
  3444. * is guaranteed to be either visible in the source cgroup after the
  3445. * parent's migration is complete or put into the target cgroup. No task
  3446. * can slip out of migration through forking.
  3447. */
  3448. int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
  3449. {
  3450. LIST_HEAD(preloaded_csets);
  3451. struct cgrp_cset_link *link;
  3452. struct css_task_iter it;
  3453. struct task_struct *task;
  3454. int ret;
  3455. mutex_lock(&cgroup_mutex);
  3456. /* all tasks in @from are being moved, all csets are source */
  3457. spin_lock_bh(&css_set_lock);
  3458. list_for_each_entry(link, &from->cset_links, cset_link)
  3459. cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
  3460. spin_unlock_bh(&css_set_lock);
  3461. ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
  3462. if (ret)
  3463. goto out_err;
  3464. /*
  3465. * Migrate tasks one-by-one until @from is empty. This fails iff
  3466. * ->can_attach() fails.
  3467. */
  3468. do {
  3469. css_task_iter_start(&from->self, &it);
  3470. task = css_task_iter_next(&it);
  3471. if (task)
  3472. get_task_struct(task);
  3473. css_task_iter_end(&it);
  3474. if (task) {
  3475. ret = cgroup_migrate(task, false, to);
  3476. put_task_struct(task);
  3477. }
  3478. } while (task && !ret);
  3479. out_err:
  3480. cgroup_migrate_finish(&preloaded_csets);
  3481. mutex_unlock(&cgroup_mutex);
  3482. return ret;
  3483. }
  3484. /*
  3485. * Stuff for reading the 'tasks'/'procs' files.
  3486. *
  3487. * Reading this file can return large amounts of data if a cgroup has
  3488. * *lots* of attached tasks. So it may need several calls to read(),
  3489. * but we cannot guarantee that the information we produce is correct
  3490. * unless we produce it entirely atomically.
  3491. *
  3492. */
  3493. /* which pidlist file are we talking about? */
  3494. enum cgroup_filetype {
  3495. CGROUP_FILE_PROCS,
  3496. CGROUP_FILE_TASKS,
  3497. };
  3498. /*
  3499. * A pidlist is a list of pids that virtually represents the contents of one
  3500. * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
  3501. * a pair (one each for procs, tasks) for each pid namespace that's relevant
  3502. * to the cgroup.
  3503. */
  3504. struct cgroup_pidlist {
  3505. /*
  3506. * used to find which pidlist is wanted. doesn't change as long as
  3507. * this particular list stays in the list.
  3508. */
  3509. struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
  3510. /* array of xids */
  3511. pid_t *list;
  3512. /* how many elements the above list has */
  3513. int length;
  3514. /* each of these stored in a list by its cgroup */
  3515. struct list_head links;
  3516. /* pointer to the cgroup we belong to, for list removal purposes */
  3517. struct cgroup *owner;
  3518. /* for delayed destruction */
  3519. struct delayed_work destroy_dwork;
  3520. };
  3521. /*
  3522. * The following two functions "fix" the issue where there are more pids
  3523. * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
  3524. * TODO: replace with a kernel-wide solution to this problem
  3525. */
  3526. #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
  3527. static void *pidlist_allocate(int count)
  3528. {
  3529. if (PIDLIST_TOO_LARGE(count))
  3530. return vmalloc(count * sizeof(pid_t));
  3531. else
  3532. return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
  3533. }
  3534. static void pidlist_free(void *p)
  3535. {
  3536. kvfree(p);
  3537. }
  3538. /*
  3539. * Used to destroy all pidlists lingering waiting for destroy timer. None
  3540. * should be left afterwards.
  3541. */
  3542. static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
  3543. {
  3544. struct cgroup_pidlist *l, *tmp_l;
  3545. mutex_lock(&cgrp->pidlist_mutex);
  3546. list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
  3547. mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
  3548. mutex_unlock(&cgrp->pidlist_mutex);
  3549. flush_workqueue(cgroup_pidlist_destroy_wq);
  3550. BUG_ON(!list_empty(&cgrp->pidlists));
  3551. }
  3552. static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
  3553. {
  3554. struct delayed_work *dwork = to_delayed_work(work);
  3555. struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
  3556. destroy_dwork);
  3557. struct cgroup_pidlist *tofree = NULL;
  3558. mutex_lock(&l->owner->pidlist_mutex);
  3559. /*
  3560. * Destroy iff we didn't get queued again. The state won't change
  3561. * as destroy_dwork can only be queued while locked.
  3562. */
  3563. if (!delayed_work_pending(dwork)) {
  3564. list_del(&l->links);
  3565. pidlist_free(l->list);
  3566. put_pid_ns(l->key.ns);
  3567. tofree = l;
  3568. }
  3569. mutex_unlock(&l->owner->pidlist_mutex);
  3570. kfree(tofree);
  3571. }
  3572. /*
  3573. * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
  3574. * Returns the number of unique elements.
  3575. */
  3576. static int pidlist_uniq(pid_t *list, int length)
  3577. {
  3578. int src, dest = 1;
  3579. /*
  3580. * we presume the 0th element is unique, so i starts at 1. trivial
  3581. * edge cases first; no work needs to be done for either
  3582. */
  3583. if (length == 0 || length == 1)
  3584. return length;
  3585. /* src and dest walk down the list; dest counts unique elements */
  3586. for (src = 1; src < length; src++) {
  3587. /* find next unique element */
  3588. while (list[src] == list[src-1]) {
  3589. src++;
  3590. if (src == length)
  3591. goto after;
  3592. }
  3593. /* dest always points to where the next unique element goes */
  3594. list[dest] = list[src];
  3595. dest++;
  3596. }
  3597. after:
  3598. return dest;
  3599. }
  3600. /*
  3601. * The two pid files - task and cgroup.procs - guaranteed that the result
  3602. * is sorted, which forced this whole pidlist fiasco. As pid order is
  3603. * different per namespace, each namespace needs differently sorted list,
  3604. * making it impossible to use, for example, single rbtree of member tasks
  3605. * sorted by task pointer. As pidlists can be fairly large, allocating one
  3606. * per open file is dangerous, so cgroup had to implement shared pool of
  3607. * pidlists keyed by cgroup and namespace.
  3608. *
  3609. * All this extra complexity was caused by the original implementation
  3610. * committing to an entirely unnecessary property. In the long term, we
  3611. * want to do away with it. Explicitly scramble sort order if on the
  3612. * default hierarchy so that no such expectation exists in the new
  3613. * interface.
  3614. *
  3615. * Scrambling is done by swapping every two consecutive bits, which is
  3616. * non-identity one-to-one mapping which disturbs sort order sufficiently.
  3617. */
  3618. static pid_t pid_fry(pid_t pid)
  3619. {
  3620. unsigned a = pid & 0x55555555;
  3621. unsigned b = pid & 0xAAAAAAAA;
  3622. return (a << 1) | (b >> 1);
  3623. }
  3624. static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
  3625. {
  3626. if (cgroup_on_dfl(cgrp))
  3627. return pid_fry(pid);
  3628. else
  3629. return pid;
  3630. }
  3631. static int cmppid(const void *a, const void *b)
  3632. {
  3633. return *(pid_t *)a - *(pid_t *)b;
  3634. }
  3635. static int fried_cmppid(const void *a, const void *b)
  3636. {
  3637. return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
  3638. }
  3639. static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
  3640. enum cgroup_filetype type)
  3641. {
  3642. struct cgroup_pidlist *l;
  3643. /* don't need task_nsproxy() if we're looking at ourself */
  3644. struct pid_namespace *ns = task_active_pid_ns(current);
  3645. lockdep_assert_held(&cgrp->pidlist_mutex);
  3646. list_for_each_entry(l, &cgrp->pidlists, links)
  3647. if (l->key.type == type && l->key.ns == ns)
  3648. return l;
  3649. return NULL;
  3650. }
  3651. /*
  3652. * find the appropriate pidlist for our purpose (given procs vs tasks)
  3653. * returns with the lock on that pidlist already held, and takes care
  3654. * of the use count, or returns NULL with no locks held if we're out of
  3655. * memory.
  3656. */
  3657. static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
  3658. enum cgroup_filetype type)
  3659. {
  3660. struct cgroup_pidlist *l;
  3661. lockdep_assert_held(&cgrp->pidlist_mutex);
  3662. l = cgroup_pidlist_find(cgrp, type);
  3663. if (l)
  3664. return l;
  3665. /* entry not found; create a new one */
  3666. l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
  3667. if (!l)
  3668. return l;
  3669. INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
  3670. l->key.type = type;
  3671. /* don't need task_nsproxy() if we're looking at ourself */
  3672. l->key.ns = get_pid_ns(task_active_pid_ns(current));
  3673. l->owner = cgrp;
  3674. list_add(&l->links, &cgrp->pidlists);
  3675. return l;
  3676. }
  3677. /*
  3678. * Load a cgroup's pidarray with either procs' tgids or tasks' pids
  3679. */
  3680. static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
  3681. struct cgroup_pidlist **lp)
  3682. {
  3683. pid_t *array;
  3684. int length;
  3685. int pid, n = 0; /* used for populating the array */
  3686. struct css_task_iter it;
  3687. struct task_struct *tsk;
  3688. struct cgroup_pidlist *l;
  3689. lockdep_assert_held(&cgrp->pidlist_mutex);
  3690. /*
  3691. * If cgroup gets more users after we read count, we won't have
  3692. * enough space - tough. This race is indistinguishable to the
  3693. * caller from the case that the additional cgroup users didn't
  3694. * show up until sometime later on.
  3695. */
  3696. length = cgroup_task_count(cgrp);
  3697. array = pidlist_allocate(length);
  3698. if (!array)
  3699. return -ENOMEM;
  3700. /* now, populate the array */
  3701. css_task_iter_start(&cgrp->self, &it);
  3702. while ((tsk = css_task_iter_next(&it))) {
  3703. if (unlikely(n == length))
  3704. break;
  3705. /* get tgid or pid for procs or tasks file respectively */
  3706. if (type == CGROUP_FILE_PROCS)
  3707. pid = task_tgid_vnr(tsk);
  3708. else
  3709. pid = task_pid_vnr(tsk);
  3710. if (pid > 0) /* make sure to only use valid results */
  3711. array[n++] = pid;
  3712. }
  3713. css_task_iter_end(&it);
  3714. length = n;
  3715. /* now sort & (if procs) strip out duplicates */
  3716. if (cgroup_on_dfl(cgrp))
  3717. sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
  3718. else
  3719. sort(array, length, sizeof(pid_t), cmppid, NULL);
  3720. if (type == CGROUP_FILE_PROCS)
  3721. length = pidlist_uniq(array, length);
  3722. l = cgroup_pidlist_find_create(cgrp, type);
  3723. if (!l) {
  3724. pidlist_free(array);
  3725. return -ENOMEM;
  3726. }
  3727. /* store array, freeing old if necessary */
  3728. pidlist_free(l->list);
  3729. l->list = array;
  3730. l->length = length;
  3731. *lp = l;
  3732. return 0;
  3733. }
  3734. /**
  3735. * cgroupstats_build - build and fill cgroupstats
  3736. * @stats: cgroupstats to fill information into
  3737. * @dentry: A dentry entry belonging to the cgroup for which stats have
  3738. * been requested.
  3739. *
  3740. * Build and fill cgroupstats so that taskstats can export it to user
  3741. * space.
  3742. */
  3743. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
  3744. {
  3745. struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
  3746. struct cgroup *cgrp;
  3747. struct css_task_iter it;
  3748. struct task_struct *tsk;
  3749. /* it should be kernfs_node belonging to cgroupfs and is a directory */
  3750. if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
  3751. kernfs_type(kn) != KERNFS_DIR)
  3752. return -EINVAL;
  3753. mutex_lock(&cgroup_mutex);
  3754. /*
  3755. * We aren't being called from kernfs and there's no guarantee on
  3756. * @kn->priv's validity. For this and css_tryget_online_from_dir(),
  3757. * @kn->priv is RCU safe. Let's do the RCU dancing.
  3758. */
  3759. rcu_read_lock();
  3760. cgrp = rcu_dereference(kn->priv);
  3761. if (!cgrp || cgroup_is_dead(cgrp)) {
  3762. rcu_read_unlock();
  3763. mutex_unlock(&cgroup_mutex);
  3764. return -ENOENT;
  3765. }
  3766. rcu_read_unlock();
  3767. css_task_iter_start(&cgrp->self, &it);
  3768. while ((tsk = css_task_iter_next(&it))) {
  3769. switch (tsk->state) {
  3770. case TASK_RUNNING:
  3771. stats->nr_running++;
  3772. break;
  3773. case TASK_INTERRUPTIBLE:
  3774. stats->nr_sleeping++;
  3775. break;
  3776. case TASK_UNINTERRUPTIBLE:
  3777. stats->nr_uninterruptible++;
  3778. break;
  3779. case TASK_STOPPED:
  3780. stats->nr_stopped++;
  3781. break;
  3782. default:
  3783. if (delayacct_is_task_waiting_on_io(tsk))
  3784. stats->nr_io_wait++;
  3785. break;
  3786. }
  3787. }
  3788. css_task_iter_end(&it);
  3789. mutex_unlock(&cgroup_mutex);
  3790. return 0;
  3791. }
  3792. /*
  3793. * seq_file methods for the tasks/procs files. The seq_file position is the
  3794. * next pid to display; the seq_file iterator is a pointer to the pid
  3795. * in the cgroup->l->list array.
  3796. */
  3797. static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
  3798. {
  3799. /*
  3800. * Initially we receive a position value that corresponds to
  3801. * one more than the last pid shown (or 0 on the first call or
  3802. * after a seek to the start). Use a binary-search to find the
  3803. * next pid to display, if any
  3804. */
  3805. struct kernfs_open_file *of = s->private;
  3806. struct cgroup *cgrp = seq_css(s)->cgroup;
  3807. struct cgroup_pidlist *l;
  3808. enum cgroup_filetype type = seq_cft(s)->private;
  3809. int index = 0, pid = *pos;
  3810. int *iter, ret;
  3811. mutex_lock(&cgrp->pidlist_mutex);
  3812. /*
  3813. * !NULL @of->priv indicates that this isn't the first start()
  3814. * after open. If the matching pidlist is around, we can use that.
  3815. * Look for it. Note that @of->priv can't be used directly. It
  3816. * could already have been destroyed.
  3817. */
  3818. if (of->priv)
  3819. of->priv = cgroup_pidlist_find(cgrp, type);
  3820. /*
  3821. * Either this is the first start() after open or the matching
  3822. * pidlist has been destroyed inbetween. Create a new one.
  3823. */
  3824. if (!of->priv) {
  3825. ret = pidlist_array_load(cgrp, type,
  3826. (struct cgroup_pidlist **)&of->priv);
  3827. if (ret)
  3828. return ERR_PTR(ret);
  3829. }
  3830. l = of->priv;
  3831. if (pid) {
  3832. int end = l->length;
  3833. while (index < end) {
  3834. int mid = (index + end) / 2;
  3835. if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
  3836. index = mid;
  3837. break;
  3838. } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
  3839. index = mid + 1;
  3840. else
  3841. end = mid;
  3842. }
  3843. }
  3844. /* If we're off the end of the array, we're done */
  3845. if (index >= l->length)
  3846. return NULL;
  3847. /* Update the abstract position to be the actual pid that we found */
  3848. iter = l->list + index;
  3849. *pos = cgroup_pid_fry(cgrp, *iter);
  3850. return iter;
  3851. }
  3852. static void cgroup_pidlist_stop(struct seq_file *s, void *v)
  3853. {
  3854. struct kernfs_open_file *of = s->private;
  3855. struct cgroup_pidlist *l = of->priv;
  3856. if (l)
  3857. mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
  3858. CGROUP_PIDLIST_DESTROY_DELAY);
  3859. mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
  3860. }
  3861. static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
  3862. {
  3863. struct kernfs_open_file *of = s->private;
  3864. struct cgroup_pidlist *l = of->priv;
  3865. pid_t *p = v;
  3866. pid_t *end = l->list + l->length;
  3867. /*
  3868. * Advance to the next pid in the array. If this goes off the
  3869. * end, we're done
  3870. */
  3871. p++;
  3872. if (p >= end) {
  3873. return NULL;
  3874. } else {
  3875. *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
  3876. return p;
  3877. }
  3878. }
  3879. static int cgroup_pidlist_show(struct seq_file *s, void *v)
  3880. {
  3881. seq_printf(s, "%d\n", *(int *)v);
  3882. return 0;
  3883. }
  3884. static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
  3885. struct cftype *cft)
  3886. {
  3887. return notify_on_release(css->cgroup);
  3888. }
  3889. static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
  3890. struct cftype *cft, u64 val)
  3891. {
  3892. if (val)
  3893. set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
  3894. else
  3895. clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
  3896. return 0;
  3897. }
  3898. static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
  3899. struct cftype *cft)
  3900. {
  3901. return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  3902. }
  3903. static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
  3904. struct cftype *cft, u64 val)
  3905. {
  3906. if (val)
  3907. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  3908. else
  3909. clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  3910. return 0;
  3911. }
  3912. /* cgroup core interface files for the default hierarchy */
  3913. static struct cftype cgroup_dfl_base_files[] = {
  3914. {
  3915. .name = "cgroup.procs",
  3916. .file_offset = offsetof(struct cgroup, procs_file),
  3917. .seq_start = cgroup_pidlist_start,
  3918. .seq_next = cgroup_pidlist_next,
  3919. .seq_stop = cgroup_pidlist_stop,
  3920. .seq_show = cgroup_pidlist_show,
  3921. .private = CGROUP_FILE_PROCS,
  3922. .write = cgroup_procs_write,
  3923. },
  3924. {
  3925. .name = "cgroup.controllers",
  3926. .flags = CFTYPE_ONLY_ON_ROOT,
  3927. .seq_show = cgroup_root_controllers_show,
  3928. },
  3929. {
  3930. .name = "cgroup.controllers",
  3931. .flags = CFTYPE_NOT_ON_ROOT,
  3932. .seq_show = cgroup_controllers_show,
  3933. },
  3934. {
  3935. .name = "cgroup.subtree_control",
  3936. .seq_show = cgroup_subtree_control_show,
  3937. .write = cgroup_subtree_control_write,
  3938. },
  3939. {
  3940. .name = "cgroup.events",
  3941. .flags = CFTYPE_NOT_ON_ROOT,
  3942. .file_offset = offsetof(struct cgroup, events_file),
  3943. .seq_show = cgroup_events_show,
  3944. },
  3945. { } /* terminate */
  3946. };
  3947. /* cgroup core interface files for the legacy hierarchies */
  3948. static struct cftype cgroup_legacy_base_files[] = {
  3949. {
  3950. .name = "cgroup.procs",
  3951. .seq_start = cgroup_pidlist_start,
  3952. .seq_next = cgroup_pidlist_next,
  3953. .seq_stop = cgroup_pidlist_stop,
  3954. .seq_show = cgroup_pidlist_show,
  3955. .private = CGROUP_FILE_PROCS,
  3956. .write = cgroup_procs_write,
  3957. },
  3958. {
  3959. .name = "cgroup.clone_children",
  3960. .read_u64 = cgroup_clone_children_read,
  3961. .write_u64 = cgroup_clone_children_write,
  3962. },
  3963. {
  3964. .name = "cgroup.sane_behavior",
  3965. .flags = CFTYPE_ONLY_ON_ROOT,
  3966. .seq_show = cgroup_sane_behavior_show,
  3967. },
  3968. {
  3969. .name = "tasks",
  3970. .seq_start = cgroup_pidlist_start,
  3971. .seq_next = cgroup_pidlist_next,
  3972. .seq_stop = cgroup_pidlist_stop,
  3973. .seq_show = cgroup_pidlist_show,
  3974. .private = CGROUP_FILE_TASKS,
  3975. .write = cgroup_tasks_write,
  3976. },
  3977. {
  3978. .name = "notify_on_release",
  3979. .read_u64 = cgroup_read_notify_on_release,
  3980. .write_u64 = cgroup_write_notify_on_release,
  3981. },
  3982. {
  3983. .name = "release_agent",
  3984. .flags = CFTYPE_ONLY_ON_ROOT,
  3985. .seq_show = cgroup_release_agent_show,
  3986. .write = cgroup_release_agent_write,
  3987. .max_write_len = PATH_MAX - 1,
  3988. },
  3989. { } /* terminate */
  3990. };
  3991. /*
  3992. * css destruction is four-stage process.
  3993. *
  3994. * 1. Destruction starts. Killing of the percpu_ref is initiated.
  3995. * Implemented in kill_css().
  3996. *
  3997. * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
  3998. * and thus css_tryget_online() is guaranteed to fail, the css can be
  3999. * offlined by invoking offline_css(). After offlining, the base ref is
  4000. * put. Implemented in css_killed_work_fn().
  4001. *
  4002. * 3. When the percpu_ref reaches zero, the only possible remaining
  4003. * accessors are inside RCU read sections. css_release() schedules the
  4004. * RCU callback.
  4005. *
  4006. * 4. After the grace period, the css can be freed. Implemented in
  4007. * css_free_work_fn().
  4008. *
  4009. * It is actually hairier because both step 2 and 4 require process context
  4010. * and thus involve punting to css->destroy_work adding two additional
  4011. * steps to the already complex sequence.
  4012. */
  4013. static void css_free_work_fn(struct work_struct *work)
  4014. {
  4015. struct cgroup_subsys_state *css =
  4016. container_of(work, struct cgroup_subsys_state, destroy_work);
  4017. struct cgroup_subsys *ss = css->ss;
  4018. struct cgroup *cgrp = css->cgroup;
  4019. percpu_ref_exit(&css->refcnt);
  4020. if (ss) {
  4021. /* css free path */
  4022. int id = css->id;
  4023. if (css->parent)
  4024. css_put(css->parent);
  4025. ss->css_free(css);
  4026. cgroup_idr_remove(&ss->css_idr, id);
  4027. cgroup_put(cgrp);
  4028. } else {
  4029. /* cgroup free path */
  4030. atomic_dec(&cgrp->root->nr_cgrps);
  4031. cgroup_pidlist_destroy_all(cgrp);
  4032. cancel_work_sync(&cgrp->release_agent_work);
  4033. if (cgroup_parent(cgrp)) {
  4034. /*
  4035. * We get a ref to the parent, and put the ref when
  4036. * this cgroup is being freed, so it's guaranteed
  4037. * that the parent won't be destroyed before its
  4038. * children.
  4039. */
  4040. cgroup_put(cgroup_parent(cgrp));
  4041. kernfs_put(cgrp->kn);
  4042. kfree(cgrp);
  4043. } else {
  4044. /*
  4045. * This is root cgroup's refcnt reaching zero,
  4046. * which indicates that the root should be
  4047. * released.
  4048. */
  4049. cgroup_destroy_root(cgrp->root);
  4050. }
  4051. }
  4052. }
  4053. static void css_free_rcu_fn(struct rcu_head *rcu_head)
  4054. {
  4055. struct cgroup_subsys_state *css =
  4056. container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
  4057. INIT_WORK(&css->destroy_work, css_free_work_fn);
  4058. queue_work(cgroup_destroy_wq, &css->destroy_work);
  4059. }
  4060. static void css_release_work_fn(struct work_struct *work)
  4061. {
  4062. struct cgroup_subsys_state *css =
  4063. container_of(work, struct cgroup_subsys_state, destroy_work);
  4064. struct cgroup_subsys *ss = css->ss;
  4065. struct cgroup *cgrp = css->cgroup;
  4066. mutex_lock(&cgroup_mutex);
  4067. css->flags |= CSS_RELEASED;
  4068. list_del_rcu(&css->sibling);
  4069. if (ss) {
  4070. /* css release path */
  4071. cgroup_idr_replace(&ss->css_idr, NULL, css->id);
  4072. if (ss->css_released)
  4073. ss->css_released(css);
  4074. } else {
  4075. /* cgroup release path */
  4076. cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
  4077. cgrp->id = -1;
  4078. /*
  4079. * There are two control paths which try to determine
  4080. * cgroup from dentry without going through kernfs -
  4081. * cgroupstats_build() and css_tryget_online_from_dir().
  4082. * Those are supported by RCU protecting clearing of
  4083. * cgrp->kn->priv backpointer.
  4084. */
  4085. RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
  4086. }
  4087. mutex_unlock(&cgroup_mutex);
  4088. call_rcu(&css->rcu_head, css_free_rcu_fn);
  4089. }
  4090. static void css_release(struct percpu_ref *ref)
  4091. {
  4092. struct cgroup_subsys_state *css =
  4093. container_of(ref, struct cgroup_subsys_state, refcnt);
  4094. INIT_WORK(&css->destroy_work, css_release_work_fn);
  4095. queue_work(cgroup_destroy_wq, &css->destroy_work);
  4096. }
  4097. static void init_and_link_css(struct cgroup_subsys_state *css,
  4098. struct cgroup_subsys *ss, struct cgroup *cgrp)
  4099. {
  4100. lockdep_assert_held(&cgroup_mutex);
  4101. cgroup_get(cgrp);
  4102. memset(css, 0, sizeof(*css));
  4103. css->cgroup = cgrp;
  4104. css->ss = ss;
  4105. INIT_LIST_HEAD(&css->sibling);
  4106. INIT_LIST_HEAD(&css->children);
  4107. css->serial_nr = css_serial_nr_next++;
  4108. if (cgroup_parent(cgrp)) {
  4109. css->parent = cgroup_css(cgroup_parent(cgrp), ss);
  4110. css_get(css->parent);
  4111. }
  4112. BUG_ON(cgroup_css(cgrp, ss));
  4113. }
  4114. /* invoke ->css_online() on a new CSS and mark it online if successful */
  4115. static int online_css(struct cgroup_subsys_state *css)
  4116. {
  4117. struct cgroup_subsys *ss = css->ss;
  4118. int ret = 0;
  4119. lockdep_assert_held(&cgroup_mutex);
  4120. if (ss->css_online)
  4121. ret = ss->css_online(css);
  4122. if (!ret) {
  4123. css->flags |= CSS_ONLINE;
  4124. rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
  4125. }
  4126. return ret;
  4127. }
  4128. /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
  4129. static void offline_css(struct cgroup_subsys_state *css)
  4130. {
  4131. struct cgroup_subsys *ss = css->ss;
  4132. lockdep_assert_held(&cgroup_mutex);
  4133. if (!(css->flags & CSS_ONLINE))
  4134. return;
  4135. if (ss->css_offline)
  4136. ss->css_offline(css);
  4137. css->flags &= ~CSS_ONLINE;
  4138. RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
  4139. wake_up_all(&css->cgroup->offline_waitq);
  4140. }
  4141. /**
  4142. * create_css - create a cgroup_subsys_state
  4143. * @cgrp: the cgroup new css will be associated with
  4144. * @ss: the subsys of new css
  4145. * @visible: whether to create control knobs for the new css or not
  4146. *
  4147. * Create a new css associated with @cgrp - @ss pair. On success, the new
  4148. * css is online and installed in @cgrp with all interface files created if
  4149. * @visible. Returns 0 on success, -errno on failure.
  4150. */
  4151. static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
  4152. bool visible)
  4153. {
  4154. struct cgroup *parent = cgroup_parent(cgrp);
  4155. struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
  4156. struct cgroup_subsys_state *css;
  4157. int err;
  4158. lockdep_assert_held(&cgroup_mutex);
  4159. css = ss->css_alloc(parent_css);
  4160. if (IS_ERR(css))
  4161. return PTR_ERR(css);
  4162. init_and_link_css(css, ss, cgrp);
  4163. err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
  4164. if (err)
  4165. goto err_free_css;
  4166. err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
  4167. if (err < 0)
  4168. goto err_free_percpu_ref;
  4169. css->id = err;
  4170. if (visible) {
  4171. err = css_populate_dir(css, NULL);
  4172. if (err)
  4173. goto err_free_id;
  4174. }
  4175. /* @css is ready to be brought online now, make it visible */
  4176. list_add_tail_rcu(&css->sibling, &parent_css->children);
  4177. cgroup_idr_replace(&ss->css_idr, css, css->id);
  4178. err = online_css(css);
  4179. if (err)
  4180. goto err_list_del;
  4181. if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
  4182. cgroup_parent(parent)) {
  4183. pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
  4184. current->comm, current->pid, ss->name);
  4185. if (!strcmp(ss->name, "memory"))
  4186. pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
  4187. ss->warned_broken_hierarchy = true;
  4188. }
  4189. return 0;
  4190. err_list_del:
  4191. list_del_rcu(&css->sibling);
  4192. css_clear_dir(css, NULL);
  4193. err_free_id:
  4194. cgroup_idr_remove(&ss->css_idr, css->id);
  4195. err_free_percpu_ref:
  4196. percpu_ref_exit(&css->refcnt);
  4197. err_free_css:
  4198. call_rcu(&css->rcu_head, css_free_rcu_fn);
  4199. return err;
  4200. }
  4201. static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
  4202. umode_t mode)
  4203. {
  4204. struct cgroup *parent, *cgrp, *tcgrp;
  4205. struct cgroup_root *root;
  4206. struct cgroup_subsys *ss;
  4207. struct kernfs_node *kn;
  4208. int level, ssid, ret;
  4209. /* Do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable.
  4210. */
  4211. if (strchr(name, '\n'))
  4212. return -EINVAL;
  4213. parent = cgroup_kn_lock_live(parent_kn);
  4214. if (!parent)
  4215. return -ENODEV;
  4216. root = parent->root;
  4217. level = parent->level + 1;
  4218. /* allocate the cgroup and its ID, 0 is reserved for the root */
  4219. cgrp = kzalloc(sizeof(*cgrp) +
  4220. sizeof(cgrp->ancestor_ids[0]) * (level + 1), GFP_KERNEL);
  4221. if (!cgrp) {
  4222. ret = -ENOMEM;
  4223. goto out_unlock;
  4224. }
  4225. ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
  4226. if (ret)
  4227. goto out_free_cgrp;
  4228. /*
  4229. * Temporarily set the pointer to NULL, so idr_find() won't return
  4230. * a half-baked cgroup.
  4231. */
  4232. cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
  4233. if (cgrp->id < 0) {
  4234. ret = -ENOMEM;
  4235. goto out_cancel_ref;
  4236. }
  4237. init_cgroup_housekeeping(cgrp);
  4238. cgrp->self.parent = &parent->self;
  4239. cgrp->root = root;
  4240. cgrp->level = level;
  4241. for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp))
  4242. cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
  4243. if (notify_on_release(parent))
  4244. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  4245. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
  4246. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
  4247. /* create the directory */
  4248. kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
  4249. if (IS_ERR(kn)) {
  4250. ret = PTR_ERR(kn);
  4251. goto out_free_id;
  4252. }
  4253. cgrp->kn = kn;
  4254. /*
  4255. * This extra ref will be put in cgroup_free_fn() and guarantees
  4256. * that @cgrp->kn is always accessible.
  4257. */
  4258. kernfs_get(kn);
  4259. cgrp->self.serial_nr = css_serial_nr_next++;
  4260. /* allocation complete, commit to creation */
  4261. list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
  4262. atomic_inc(&root->nr_cgrps);
  4263. cgroup_get(parent);
  4264. /*
  4265. * @cgrp is now fully operational. If something fails after this
  4266. * point, it'll be released via the normal destruction path.
  4267. */
  4268. cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
  4269. ret = cgroup_kn_set_ugid(kn);
  4270. if (ret)
  4271. goto out_destroy;
  4272. ret = css_populate_dir(&cgrp->self, NULL);
  4273. if (ret)
  4274. goto out_destroy;
  4275. /* let's create and online css's */
  4276. for_each_subsys(ss, ssid) {
  4277. if (parent->child_subsys_mask & (1 << ssid)) {
  4278. ret = create_css(cgrp, ss,
  4279. parent->subtree_control & (1 << ssid));
  4280. if (ret)
  4281. goto out_destroy;
  4282. }
  4283. }
  4284. /*
  4285. * On the default hierarchy, a child doesn't automatically inherit
  4286. * subtree_control from the parent. Each is configured manually.
  4287. */
  4288. if (!cgroup_on_dfl(cgrp)) {
  4289. cgrp->subtree_control = parent->subtree_control;
  4290. cgroup_refresh_child_subsys_mask(cgrp);
  4291. }
  4292. kernfs_activate(kn);
  4293. ret = 0;
  4294. goto out_unlock;
  4295. out_free_id:
  4296. cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
  4297. out_cancel_ref:
  4298. percpu_ref_exit(&cgrp->self.refcnt);
  4299. out_free_cgrp:
  4300. kfree(cgrp);
  4301. out_unlock:
  4302. cgroup_kn_unlock(parent_kn);
  4303. return ret;
  4304. out_destroy:
  4305. cgroup_destroy_locked(cgrp);
  4306. goto out_unlock;
  4307. }
  4308. /*
  4309. * This is called when the refcnt of a css is confirmed to be killed.
  4310. * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
  4311. * initate destruction and put the css ref from kill_css().
  4312. */
  4313. static void css_killed_work_fn(struct work_struct *work)
  4314. {
  4315. struct cgroup_subsys_state *css =
  4316. container_of(work, struct cgroup_subsys_state, destroy_work);
  4317. mutex_lock(&cgroup_mutex);
  4318. offline_css(css);
  4319. mutex_unlock(&cgroup_mutex);
  4320. css_put(css);
  4321. }
  4322. /* css kill confirmation processing requires process context, bounce */
  4323. static void css_killed_ref_fn(struct percpu_ref *ref)
  4324. {
  4325. struct cgroup_subsys_state *css =
  4326. container_of(ref, struct cgroup_subsys_state, refcnt);
  4327. INIT_WORK(&css->destroy_work, css_killed_work_fn);
  4328. queue_work(cgroup_destroy_wq, &css->destroy_work);
  4329. }
  4330. /**
  4331. * kill_css - destroy a css
  4332. * @css: css to destroy
  4333. *
  4334. * This function initiates destruction of @css by removing cgroup interface
  4335. * files and putting its base reference. ->css_offline() will be invoked
  4336. * asynchronously once css_tryget_online() is guaranteed to fail and when
  4337. * the reference count reaches zero, @css will be released.
  4338. */
  4339. static void kill_css(struct cgroup_subsys_state *css)
  4340. {
  4341. lockdep_assert_held(&cgroup_mutex);
  4342. /*
  4343. * This must happen before css is disassociated with its cgroup.
  4344. * See seq_css() for details.
  4345. */
  4346. css_clear_dir(css, NULL);
  4347. /*
  4348. * Killing would put the base ref, but we need to keep it alive
  4349. * until after ->css_offline().
  4350. */
  4351. css_get(css);
  4352. /*
  4353. * cgroup core guarantees that, by the time ->css_offline() is
  4354. * invoked, no new css reference will be given out via
  4355. * css_tryget_online(). We can't simply call percpu_ref_kill() and
  4356. * proceed to offlining css's because percpu_ref_kill() doesn't
  4357. * guarantee that the ref is seen as killed on all CPUs on return.
  4358. *
  4359. * Use percpu_ref_kill_and_confirm() to get notifications as each
  4360. * css is confirmed to be seen as killed on all CPUs.
  4361. */
  4362. percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
  4363. }
  4364. /**
  4365. * cgroup_destroy_locked - the first stage of cgroup destruction
  4366. * @cgrp: cgroup to be destroyed
  4367. *
  4368. * css's make use of percpu refcnts whose killing latency shouldn't be
  4369. * exposed to userland and are RCU protected. Also, cgroup core needs to
  4370. * guarantee that css_tryget_online() won't succeed by the time
  4371. * ->css_offline() is invoked. To satisfy all the requirements,
  4372. * destruction is implemented in the following two steps.
  4373. *
  4374. * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
  4375. * userland visible parts and start killing the percpu refcnts of
  4376. * css's. Set up so that the next stage will be kicked off once all
  4377. * the percpu refcnts are confirmed to be killed.
  4378. *
  4379. * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
  4380. * rest of destruction. Once all cgroup references are gone, the
  4381. * cgroup is RCU-freed.
  4382. *
  4383. * This function implements s1. After this step, @cgrp is gone as far as
  4384. * the userland is concerned and a new cgroup with the same name may be
  4385. * created. As cgroup doesn't care about the names internally, this
  4386. * doesn't cause any problem.
  4387. */
  4388. static int cgroup_destroy_locked(struct cgroup *cgrp)
  4389. __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
  4390. {
  4391. struct cgroup_subsys_state *css;
  4392. int ssid;
  4393. lockdep_assert_held(&cgroup_mutex);
  4394. /*
  4395. * Only migration can raise populated from zero and we're already
  4396. * holding cgroup_mutex.
  4397. */
  4398. if (cgroup_is_populated(cgrp))
  4399. return -EBUSY;
  4400. /*
  4401. * Make sure there's no live children. We can't test emptiness of
  4402. * ->self.children as dead children linger on it while being
  4403. * drained; otherwise, "rmdir parent/child parent" may fail.
  4404. */
  4405. if (css_has_online_children(&cgrp->self))
  4406. return -EBUSY;
  4407. /*
  4408. * Mark @cgrp dead. This prevents further task migration and child
  4409. * creation by disabling cgroup_lock_live_group().
  4410. */
  4411. cgrp->self.flags &= ~CSS_ONLINE;
  4412. /* initiate massacre of all css's */
  4413. for_each_css(css, ssid, cgrp)
  4414. kill_css(css);
  4415. /*
  4416. * Remove @cgrp directory along with the base files. @cgrp has an
  4417. * extra ref on its kn.
  4418. */
  4419. kernfs_remove(cgrp->kn);
  4420. check_for_release(cgroup_parent(cgrp));
  4421. /* put the base reference */
  4422. percpu_ref_kill(&cgrp->self.refcnt);
  4423. return 0;
  4424. };
  4425. static int cgroup_rmdir(struct kernfs_node *kn)
  4426. {
  4427. struct cgroup *cgrp;
  4428. int ret = 0;
  4429. cgrp = cgroup_kn_lock_live(kn);
  4430. if (!cgrp)
  4431. return 0;
  4432. ret = cgroup_destroy_locked(cgrp);
  4433. cgroup_kn_unlock(kn);
  4434. return ret;
  4435. }
  4436. static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
  4437. .remount_fs = cgroup_remount,
  4438. .show_options = cgroup_show_options,
  4439. .mkdir = cgroup_mkdir,
  4440. .rmdir = cgroup_rmdir,
  4441. .rename = cgroup_rename,
  4442. };
  4443. static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
  4444. {
  4445. struct cgroup_subsys_state *css;
  4446. pr_debug("Initializing cgroup subsys %s\n", ss->name);
  4447. mutex_lock(&cgroup_mutex);
  4448. idr_init(&ss->css_idr);
  4449. INIT_LIST_HEAD(&ss->cfts);
  4450. /* Create the root cgroup state for this subsystem */
  4451. ss->root = &cgrp_dfl_root;
  4452. css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
  4453. /* We don't handle early failures gracefully */
  4454. BUG_ON(IS_ERR(css));
  4455. init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
  4456. /*
  4457. * Root csses are never destroyed and we can't initialize
  4458. * percpu_ref during early init. Disable refcnting.
  4459. */
  4460. css->flags |= CSS_NO_REF;
  4461. if (early) {
  4462. /* allocation can't be done safely during early init */
  4463. css->id = 1;
  4464. } else {
  4465. css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
  4466. BUG_ON(css->id < 0);
  4467. }
  4468. /* Update the init_css_set to contain a subsys
  4469. * pointer to this state - since the subsystem is
  4470. * newly registered, all tasks and hence the
  4471. * init_css_set is in the subsystem's root cgroup. */
  4472. init_css_set.subsys[ss->id] = css;
  4473. have_fork_callback |= (bool)ss->fork << ss->id;
  4474. have_exit_callback |= (bool)ss->exit << ss->id;
  4475. have_free_callback |= (bool)ss->free << ss->id;
  4476. have_canfork_callback |= (bool)ss->can_fork << ss->id;
  4477. /* At system boot, before all subsystems have been
  4478. * registered, no tasks have been forked, so we don't
  4479. * need to invoke fork callbacks here. */
  4480. BUG_ON(!list_empty(&init_task.tasks));
  4481. BUG_ON(online_css(css));
  4482. mutex_unlock(&cgroup_mutex);
  4483. }
  4484. /**
  4485. * cgroup_init_early - cgroup initialization at system boot
  4486. *
  4487. * Initialize cgroups at system boot, and initialize any
  4488. * subsystems that request early init.
  4489. */
  4490. int __init cgroup_init_early(void)
  4491. {
  4492. static struct cgroup_sb_opts __initdata opts;
  4493. struct cgroup_subsys *ss;
  4494. int i;
  4495. init_cgroup_root(&cgrp_dfl_root, &opts);
  4496. cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
  4497. RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
  4498. for_each_subsys(ss, i) {
  4499. WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
  4500. "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
  4501. i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
  4502. ss->id, ss->name);
  4503. WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
  4504. "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
  4505. ss->id = i;
  4506. ss->name = cgroup_subsys_name[i];
  4507. if (!ss->legacy_name)
  4508. ss->legacy_name = cgroup_subsys_name[i];
  4509. if (ss->early_init)
  4510. cgroup_init_subsys(ss, true);
  4511. }
  4512. return 0;
  4513. }
  4514. static unsigned long cgroup_disable_mask __initdata;
  4515. /**
  4516. * cgroup_init - cgroup initialization
  4517. *
  4518. * Register cgroup filesystem and /proc file, and initialize
  4519. * any subsystems that didn't request early init.
  4520. */
  4521. int __init cgroup_init(void)
  4522. {
  4523. struct cgroup_subsys *ss;
  4524. unsigned long key;
  4525. int ssid;
  4526. BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
  4527. BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
  4528. BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
  4529. mutex_lock(&cgroup_mutex);
  4530. /* Add init_css_set to the hash table */
  4531. key = css_set_hash(init_css_set.subsys);
  4532. hash_add(css_set_table, &init_css_set.hlist, key);
  4533. BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
  4534. mutex_unlock(&cgroup_mutex);
  4535. for_each_subsys(ss, ssid) {
  4536. if (ss->early_init) {
  4537. struct cgroup_subsys_state *css =
  4538. init_css_set.subsys[ss->id];
  4539. css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
  4540. GFP_KERNEL);
  4541. BUG_ON(css->id < 0);
  4542. } else {
  4543. cgroup_init_subsys(ss, false);
  4544. }
  4545. list_add_tail(&init_css_set.e_cset_node[ssid],
  4546. &cgrp_dfl_root.cgrp.e_csets[ssid]);
  4547. /*
  4548. * Setting dfl_root subsys_mask needs to consider the
  4549. * disabled flag and cftype registration needs kmalloc,
  4550. * both of which aren't available during early_init.
  4551. */
  4552. if (cgroup_disable_mask & (1 << ssid)) {
  4553. static_branch_disable(cgroup_subsys_enabled_key[ssid]);
  4554. printk(KERN_INFO "Disabling %s control group subsystem\n",
  4555. ss->name);
  4556. continue;
  4557. }
  4558. cgrp_dfl_root.subsys_mask |= 1 << ss->id;
  4559. if (!ss->dfl_cftypes)
  4560. cgrp_dfl_root_inhibit_ss_mask |= 1 << ss->id;
  4561. if (ss->dfl_cftypes == ss->legacy_cftypes) {
  4562. WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
  4563. } else {
  4564. WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
  4565. WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
  4566. }
  4567. if (ss->bind)
  4568. ss->bind(init_css_set.subsys[ssid]);
  4569. }
  4570. WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
  4571. WARN_ON(register_filesystem(&cgroup_fs_type));
  4572. WARN_ON(register_filesystem(&cgroup2_fs_type));
  4573. WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations));
  4574. return 0;
  4575. }
  4576. static int __init cgroup_wq_init(void)
  4577. {
  4578. /*
  4579. * There isn't much point in executing destruction path in
  4580. * parallel. Good chunk is serialized with cgroup_mutex anyway.
  4581. * Use 1 for @max_active.
  4582. *
  4583. * We would prefer to do this in cgroup_init() above, but that
  4584. * is called before init_workqueues(): so leave this until after.
  4585. */
  4586. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  4587. BUG_ON(!cgroup_destroy_wq);
  4588. /*
  4589. * Used to destroy pidlists and separate to serve as flush domain.
  4590. * Cap @max_active to 1 too.
  4591. */
  4592. cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
  4593. 0, 1);
  4594. BUG_ON(!cgroup_pidlist_destroy_wq);
  4595. return 0;
  4596. }
  4597. core_initcall(cgroup_wq_init);
  4598. /*
  4599. * proc_cgroup_show()
  4600. * - Print task's cgroup paths into seq_file, one line for each hierarchy
  4601. * - Used for /proc/<pid>/cgroup.
  4602. */
  4603. int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
  4604. struct pid *pid, struct task_struct *tsk)
  4605. {
  4606. char *buf, *path;
  4607. int retval;
  4608. struct cgroup_root *root;
  4609. retval = -ENOMEM;
  4610. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  4611. if (!buf)
  4612. goto out;
  4613. mutex_lock(&cgroup_mutex);
  4614. spin_lock_bh(&css_set_lock);
  4615. for_each_root(root) {
  4616. struct cgroup_subsys *ss;
  4617. struct cgroup *cgrp;
  4618. int ssid, count = 0;
  4619. if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
  4620. continue;
  4621. seq_printf(m, "%d:", root->hierarchy_id);
  4622. if (root != &cgrp_dfl_root)
  4623. for_each_subsys(ss, ssid)
  4624. if (root->subsys_mask & (1 << ssid))
  4625. seq_printf(m, "%s%s", count++ ? "," : "",
  4626. ss->legacy_name);
  4627. if (strlen(root->name))
  4628. seq_printf(m, "%sname=%s", count ? "," : "",
  4629. root->name);
  4630. seq_putc(m, ':');
  4631. cgrp = task_cgroup_from_root(tsk, root);
  4632. /*
  4633. * On traditional hierarchies, all zombie tasks show up as
  4634. * belonging to the root cgroup. On the default hierarchy,
  4635. * while a zombie doesn't show up in "cgroup.procs" and
  4636. * thus can't be migrated, its /proc/PID/cgroup keeps
  4637. * reporting the cgroup it belonged to before exiting. If
  4638. * the cgroup is removed before the zombie is reaped,
  4639. * " (deleted)" is appended to the cgroup path.
  4640. */
  4641. if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
  4642. path = cgroup_path(cgrp, buf, PATH_MAX);
  4643. if (!path) {
  4644. retval = -ENAMETOOLONG;
  4645. goto out_unlock;
  4646. }
  4647. } else {
  4648. path = "/";
  4649. }
  4650. seq_puts(m, path);
  4651. if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
  4652. seq_puts(m, " (deleted)\n");
  4653. else
  4654. seq_putc(m, '\n');
  4655. }
  4656. retval = 0;
  4657. out_unlock:
  4658. spin_unlock_bh(&css_set_lock);
  4659. mutex_unlock(&cgroup_mutex);
  4660. kfree(buf);
  4661. out:
  4662. return retval;
  4663. }
  4664. /* Display information about each subsystem and each hierarchy */
  4665. static int proc_cgroupstats_show(struct seq_file *m, void *v)
  4666. {
  4667. struct cgroup_subsys *ss;
  4668. int i;
  4669. seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
  4670. /*
  4671. * ideally we don't want subsystems moving around while we do this.
  4672. * cgroup_mutex is also necessary to guarantee an atomic snapshot of
  4673. * subsys/hierarchy state.
  4674. */
  4675. mutex_lock(&cgroup_mutex);
  4676. for_each_subsys(ss, i)
  4677. seq_printf(m, "%s\t%d\t%d\t%d\n",
  4678. ss->legacy_name, ss->root->hierarchy_id,
  4679. atomic_read(&ss->root->nr_cgrps),
  4680. cgroup_ssid_enabled(i));
  4681. mutex_unlock(&cgroup_mutex);
  4682. return 0;
  4683. }
  4684. static int cgroupstats_open(struct inode *inode, struct file *file)
  4685. {
  4686. return single_open(file, proc_cgroupstats_show, NULL);
  4687. }
  4688. static const struct file_operations proc_cgroupstats_operations = {
  4689. .open = cgroupstats_open,
  4690. .read = seq_read,
  4691. .llseek = seq_lseek,
  4692. .release = single_release,
  4693. };
  4694. /**
  4695. * cgroup_fork - initialize cgroup related fields during copy_process()
  4696. * @child: pointer to task_struct of forking parent process.
  4697. *
  4698. * A task is associated with the init_css_set until cgroup_post_fork()
  4699. * attaches it to the parent's css_set. Empty cg_list indicates that
  4700. * @child isn't holding reference to its css_set.
  4701. */
  4702. void cgroup_fork(struct task_struct *child)
  4703. {
  4704. RCU_INIT_POINTER(child->cgroups, &init_css_set);
  4705. INIT_LIST_HEAD(&child->cg_list);
  4706. }
  4707. /**
  4708. * cgroup_can_fork - called on a new task before the process is exposed
  4709. * @child: the task in question.
  4710. *
  4711. * This calls the subsystem can_fork() callbacks. If the can_fork() callback
  4712. * returns an error, the fork aborts with that error code. This allows for
  4713. * a cgroup subsystem to conditionally allow or deny new forks.
  4714. */
  4715. int cgroup_can_fork(struct task_struct *child)
  4716. {
  4717. struct cgroup_subsys *ss;
  4718. int i, j, ret;
  4719. for_each_subsys_which(ss, i, &have_canfork_callback) {
  4720. ret = ss->can_fork(child);
  4721. if (ret)
  4722. goto out_revert;
  4723. }
  4724. return 0;
  4725. out_revert:
  4726. for_each_subsys(ss, j) {
  4727. if (j >= i)
  4728. break;
  4729. if (ss->cancel_fork)
  4730. ss->cancel_fork(child);
  4731. }
  4732. return ret;
  4733. }
  4734. /**
  4735. * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
  4736. * @child: the task in question
  4737. *
  4738. * This calls the cancel_fork() callbacks if a fork failed *after*
  4739. * cgroup_can_fork() succeded.
  4740. */
  4741. void cgroup_cancel_fork(struct task_struct *child)
  4742. {
  4743. struct cgroup_subsys *ss;
  4744. int i;
  4745. for_each_subsys(ss, i)
  4746. if (ss->cancel_fork)
  4747. ss->cancel_fork(child);
  4748. }
  4749. /**
  4750. * cgroup_post_fork - called on a new task after adding it to the task list
  4751. * @child: the task in question
  4752. *
  4753. * Adds the task to the list running through its css_set if necessary and
  4754. * call the subsystem fork() callbacks. Has to be after the task is
  4755. * visible on the task list in case we race with the first call to
  4756. * cgroup_task_iter_start() - to guarantee that the new task ends up on its
  4757. * list.
  4758. */
  4759. void cgroup_post_fork(struct task_struct *child)
  4760. {
  4761. struct cgroup_subsys *ss;
  4762. int i;
  4763. /*
  4764. * This may race against cgroup_enable_task_cg_lists(). As that
  4765. * function sets use_task_css_set_links before grabbing
  4766. * tasklist_lock and we just went through tasklist_lock to add
  4767. * @child, it's guaranteed that either we see the set
  4768. * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
  4769. * @child during its iteration.
  4770. *
  4771. * If we won the race, @child is associated with %current's
  4772. * css_set. Grabbing css_set_lock guarantees both that the
  4773. * association is stable, and, on completion of the parent's
  4774. * migration, @child is visible in the source of migration or
  4775. * already in the destination cgroup. This guarantee is necessary
  4776. * when implementing operations which need to migrate all tasks of
  4777. * a cgroup to another.
  4778. *
  4779. * Note that if we lose to cgroup_enable_task_cg_lists(), @child
  4780. * will remain in init_css_set. This is safe because all tasks are
  4781. * in the init_css_set before cg_links is enabled and there's no
  4782. * operation which transfers all tasks out of init_css_set.
  4783. */
  4784. if (use_task_css_set_links) {
  4785. struct css_set *cset;
  4786. spin_lock_bh(&css_set_lock);
  4787. cset = task_css_set(current);
  4788. if (list_empty(&child->cg_list)) {
  4789. get_css_set(cset);
  4790. css_set_move_task(child, NULL, cset, false);
  4791. }
  4792. spin_unlock_bh(&css_set_lock);
  4793. }
  4794. /*
  4795. * Call ss->fork(). This must happen after @child is linked on
  4796. * css_set; otherwise, @child might change state between ->fork()
  4797. * and addition to css_set.
  4798. */
  4799. for_each_subsys_which(ss, i, &have_fork_callback)
  4800. ss->fork(child);
  4801. }
  4802. /**
  4803. * cgroup_exit - detach cgroup from exiting task
  4804. * @tsk: pointer to task_struct of exiting process
  4805. *
  4806. * Description: Detach cgroup from @tsk and release it.
  4807. *
  4808. * Note that cgroups marked notify_on_release force every task in
  4809. * them to take the global cgroup_mutex mutex when exiting.
  4810. * This could impact scaling on very large systems. Be reluctant to
  4811. * use notify_on_release cgroups where very high task exit scaling
  4812. * is required on large systems.
  4813. *
  4814. * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
  4815. * call cgroup_exit() while the task is still competent to handle
  4816. * notify_on_release(), then leave the task attached to the root cgroup in
  4817. * each hierarchy for the remainder of its exit. No need to bother with
  4818. * init_css_set refcnting. init_css_set never goes away and we can't race
  4819. * with migration path - PF_EXITING is visible to migration path.
  4820. */
  4821. void cgroup_exit(struct task_struct *tsk)
  4822. {
  4823. struct cgroup_subsys *ss;
  4824. struct css_set *cset;
  4825. int i;
  4826. /*
  4827. * Unlink from @tsk from its css_set. As migration path can't race
  4828. * with us, we can check css_set and cg_list without synchronization.
  4829. */
  4830. cset = task_css_set(tsk);
  4831. if (!list_empty(&tsk->cg_list)) {
  4832. spin_lock_bh(&css_set_lock);
  4833. css_set_move_task(tsk, cset, NULL, false);
  4834. spin_unlock_bh(&css_set_lock);
  4835. } else {
  4836. get_css_set(cset);
  4837. }
  4838. /* see cgroup_post_fork() for details */
  4839. for_each_subsys_which(ss, i, &have_exit_callback)
  4840. ss->exit(tsk);
  4841. }
  4842. void cgroup_free(struct task_struct *task)
  4843. {
  4844. struct css_set *cset = task_css_set(task);
  4845. struct cgroup_subsys *ss;
  4846. int ssid;
  4847. for_each_subsys_which(ss, ssid, &have_free_callback)
  4848. ss->free(task);
  4849. put_css_set(cset);
  4850. }
  4851. static void check_for_release(struct cgroup *cgrp)
  4852. {
  4853. if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
  4854. !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
  4855. schedule_work(&cgrp->release_agent_work);
  4856. }
  4857. /*
  4858. * Notify userspace when a cgroup is released, by running the
  4859. * configured release agent with the name of the cgroup (path
  4860. * relative to the root of cgroup file system) as the argument.
  4861. *
  4862. * Most likely, this user command will try to rmdir this cgroup.
  4863. *
  4864. * This races with the possibility that some other task will be
  4865. * attached to this cgroup before it is removed, or that some other
  4866. * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
  4867. * The presumed 'rmdir' will fail quietly if this cgroup is no longer
  4868. * unused, and this cgroup will be reprieved from its death sentence,
  4869. * to continue to serve a useful existence. Next time it's released,
  4870. * we will get notified again, if it still has 'notify_on_release' set.
  4871. *
  4872. * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
  4873. * means only wait until the task is successfully execve()'d. The
  4874. * separate release agent task is forked by call_usermodehelper(),
  4875. * then control in this thread returns here, without waiting for the
  4876. * release agent task. We don't bother to wait because the caller of
  4877. * this routine has no use for the exit status of the release agent
  4878. * task, so no sense holding our caller up for that.
  4879. */
  4880. static void cgroup_release_agent(struct work_struct *work)
  4881. {
  4882. struct cgroup *cgrp =
  4883. container_of(work, struct cgroup, release_agent_work);
  4884. char *pathbuf = NULL, *agentbuf = NULL, *path;
  4885. char *argv[3], *envp[3];
  4886. mutex_lock(&cgroup_mutex);
  4887. pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  4888. agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
  4889. if (!pathbuf || !agentbuf)
  4890. goto out;
  4891. path = cgroup_path(cgrp, pathbuf, PATH_MAX);
  4892. if (!path)
  4893. goto out;
  4894. argv[0] = agentbuf;
  4895. argv[1] = path;
  4896. argv[2] = NULL;
  4897. /* minimal command environment */
  4898. envp[0] = "HOME=/";
  4899. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  4900. envp[2] = NULL;
  4901. mutex_unlock(&cgroup_mutex);
  4902. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  4903. goto out_free;
  4904. out:
  4905. mutex_unlock(&cgroup_mutex);
  4906. out_free:
  4907. kfree(agentbuf);
  4908. kfree(pathbuf);
  4909. }
  4910. static int __init cgroup_disable(char *str)
  4911. {
  4912. struct cgroup_subsys *ss;
  4913. char *token;
  4914. int i;
  4915. while ((token = strsep(&str, ",")) != NULL) {
  4916. if (!*token)
  4917. continue;
  4918. for_each_subsys(ss, i) {
  4919. if (strcmp(token, ss->name) &&
  4920. strcmp(token, ss->legacy_name))
  4921. continue;
  4922. cgroup_disable_mask |= 1 << i;
  4923. }
  4924. }
  4925. return 1;
  4926. }
  4927. __setup("cgroup_disable=", cgroup_disable);
  4928. /**
  4929. * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
  4930. * @dentry: directory dentry of interest
  4931. * @ss: subsystem of interest
  4932. *
  4933. * If @dentry is a directory for a cgroup which has @ss enabled on it, try
  4934. * to get the corresponding css and return it. If such css doesn't exist
  4935. * or can't be pinned, an ERR_PTR value is returned.
  4936. */
  4937. struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
  4938. struct cgroup_subsys *ss)
  4939. {
  4940. struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
  4941. struct cgroup_subsys_state *css = NULL;
  4942. struct cgroup *cgrp;
  4943. /* is @dentry a cgroup dir? */
  4944. if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
  4945. kernfs_type(kn) != KERNFS_DIR)
  4946. return ERR_PTR(-EBADF);
  4947. rcu_read_lock();
  4948. /*
  4949. * This path doesn't originate from kernfs and @kn could already
  4950. * have been or be removed at any point. @kn->priv is RCU
  4951. * protected for this access. See css_release_work_fn() for details.
  4952. */
  4953. cgrp = rcu_dereference(kn->priv);
  4954. if (cgrp)
  4955. css = cgroup_css(cgrp, ss);
  4956. if (!css || !css_tryget_online(css))
  4957. css = ERR_PTR(-ENOENT);
  4958. rcu_read_unlock();
  4959. return css;
  4960. }
  4961. /**
  4962. * css_from_id - lookup css by id
  4963. * @id: the cgroup id
  4964. * @ss: cgroup subsys to be looked into
  4965. *
  4966. * Returns the css if there's valid one with @id, otherwise returns NULL.
  4967. * Should be called under rcu_read_lock().
  4968. */
  4969. struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
  4970. {
  4971. WARN_ON_ONCE(!rcu_read_lock_held());
  4972. return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
  4973. }
  4974. /**
  4975. * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
  4976. * @path: path on the default hierarchy
  4977. *
  4978. * Find the cgroup at @path on the default hierarchy, increment its
  4979. * reference count and return it. Returns pointer to the found cgroup on
  4980. * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
  4981. * if @path points to a non-directory.
  4982. */
  4983. struct cgroup *cgroup_get_from_path(const char *path)
  4984. {
  4985. struct kernfs_node *kn;
  4986. struct cgroup *cgrp;
  4987. mutex_lock(&cgroup_mutex);
  4988. kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
  4989. if (kn) {
  4990. if (kernfs_type(kn) == KERNFS_DIR) {
  4991. cgrp = kn->priv;
  4992. cgroup_get(cgrp);
  4993. } else {
  4994. cgrp = ERR_PTR(-ENOTDIR);
  4995. }
  4996. kernfs_put(kn);
  4997. } else {
  4998. cgrp = ERR_PTR(-ENOENT);
  4999. }
  5000. mutex_unlock(&cgroup_mutex);
  5001. return cgrp;
  5002. }
  5003. EXPORT_SYMBOL_GPL(cgroup_get_from_path);
  5004. /*
  5005. * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
  5006. * definition in cgroup-defs.h.
  5007. */
  5008. #ifdef CONFIG_SOCK_CGROUP_DATA
  5009. #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
  5010. DEFINE_SPINLOCK(cgroup_sk_update_lock);
  5011. static bool cgroup_sk_alloc_disabled __read_mostly;
  5012. void cgroup_sk_alloc_disable(void)
  5013. {
  5014. if (cgroup_sk_alloc_disabled)
  5015. return;
  5016. pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
  5017. cgroup_sk_alloc_disabled = true;
  5018. }
  5019. #else
  5020. #define cgroup_sk_alloc_disabled false
  5021. #endif
  5022. void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
  5023. {
  5024. if (cgroup_sk_alloc_disabled)
  5025. return;
  5026. rcu_read_lock();
  5027. while (true) {
  5028. struct css_set *cset;
  5029. cset = task_css_set(current);
  5030. if (likely(cgroup_tryget(cset->dfl_cgrp))) {
  5031. skcd->val = (unsigned long)cset->dfl_cgrp;
  5032. break;
  5033. }
  5034. cpu_relax();
  5035. }
  5036. rcu_read_unlock();
  5037. }
  5038. void cgroup_sk_free(struct sock_cgroup_data *skcd)
  5039. {
  5040. cgroup_put(sock_cgroup_ptr(skcd));
  5041. }
  5042. #endif /* CONFIG_SOCK_CGROUP_DATA */
  5043. #ifdef CONFIG_CGROUP_DEBUG
  5044. static struct cgroup_subsys_state *
  5045. debug_css_alloc(struct cgroup_subsys_state *parent_css)
  5046. {
  5047. struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
  5048. if (!css)
  5049. return ERR_PTR(-ENOMEM);
  5050. return css;
  5051. }
  5052. static void debug_css_free(struct cgroup_subsys_state *css)
  5053. {
  5054. kfree(css);
  5055. }
  5056. static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
  5057. struct cftype *cft)
  5058. {
  5059. return cgroup_task_count(css->cgroup);
  5060. }
  5061. static u64 current_css_set_read(struct cgroup_subsys_state *css,
  5062. struct cftype *cft)
  5063. {
  5064. return (u64)(unsigned long)current->cgroups;
  5065. }
  5066. static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
  5067. struct cftype *cft)
  5068. {
  5069. u64 count;
  5070. rcu_read_lock();
  5071. count = atomic_read(&task_css_set(current)->refcount);
  5072. rcu_read_unlock();
  5073. return count;
  5074. }
  5075. static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
  5076. {
  5077. struct cgrp_cset_link *link;
  5078. struct css_set *cset;
  5079. char *name_buf;
  5080. name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  5081. if (!name_buf)
  5082. return -ENOMEM;
  5083. spin_lock_bh(&css_set_lock);
  5084. rcu_read_lock();
  5085. cset = rcu_dereference(current->cgroups);
  5086. list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  5087. struct cgroup *c = link->cgrp;
  5088. cgroup_name(c, name_buf, NAME_MAX + 1);
  5089. seq_printf(seq, "Root %d group %s\n",
  5090. c->root->hierarchy_id, name_buf);
  5091. }
  5092. rcu_read_unlock();
  5093. spin_unlock_bh(&css_set_lock);
  5094. kfree(name_buf);
  5095. return 0;
  5096. }
  5097. #define MAX_TASKS_SHOWN_PER_CSS 25
  5098. static int cgroup_css_links_read(struct seq_file *seq, void *v)
  5099. {
  5100. struct cgroup_subsys_state *css = seq_css(seq);
  5101. struct cgrp_cset_link *link;
  5102. spin_lock_bh(&css_set_lock);
  5103. list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
  5104. struct css_set *cset = link->cset;
  5105. struct task_struct *task;
  5106. int count = 0;
  5107. seq_printf(seq, "css_set %p\n", cset);
  5108. list_for_each_entry(task, &cset->tasks, cg_list) {
  5109. if (count++ > MAX_TASKS_SHOWN_PER_CSS)
  5110. goto overflow;
  5111. seq_printf(seq, " task %d\n", task_pid_vnr(task));
  5112. }
  5113. list_for_each_entry(task, &cset->mg_tasks, cg_list) {
  5114. if (count++ > MAX_TASKS_SHOWN_PER_CSS)
  5115. goto overflow;
  5116. seq_printf(seq, " task %d\n", task_pid_vnr(task));
  5117. }
  5118. continue;
  5119. overflow:
  5120. seq_puts(seq, " ...\n");
  5121. }
  5122. spin_unlock_bh(&css_set_lock);
  5123. return 0;
  5124. }
  5125. static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
  5126. {
  5127. return (!cgroup_is_populated(css->cgroup) &&
  5128. !css_has_online_children(&css->cgroup->self));
  5129. }
  5130. static struct cftype debug_files[] = {
  5131. {
  5132. .name = "taskcount",
  5133. .read_u64 = debug_taskcount_read,
  5134. },
  5135. {
  5136. .name = "current_css_set",
  5137. .read_u64 = current_css_set_read,
  5138. },
  5139. {
  5140. .name = "current_css_set_refcount",
  5141. .read_u64 = current_css_set_refcount_read,
  5142. },
  5143. {
  5144. .name = "current_css_set_cg_links",
  5145. .seq_show = current_css_set_cg_links_read,
  5146. },
  5147. {
  5148. .name = "cgroup_css_links",
  5149. .seq_show = cgroup_css_links_read,
  5150. },
  5151. {
  5152. .name = "releasable",
  5153. .read_u64 = releasable_read,
  5154. },
  5155. { } /* terminate */
  5156. };
  5157. struct cgroup_subsys debug_cgrp_subsys = {
  5158. .css_alloc = debug_css_alloc,
  5159. .css_free = debug_css_free,
  5160. .legacy_cftypes = debug_files,
  5161. };
  5162. #endif /* CONFIG_CGROUP_DEBUG */