cgroup.c 151 KB

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