cgroup.c 151 KB

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