cgroup.c 160 KB

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