cgroup.c 151 KB

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