cgroup.c 148 KB

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