cgroup.c 188 KB

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