cgroup.c 147 KB

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