cgroup.c 157 KB

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