cgroup.c 153 KB

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