cgroup.c 148 KB

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