cgroup.c 158 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893
  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. #include <linux/cgroup.h>
  29. #include <linux/cred.h>
  30. #include <linux/ctype.h>
  31. #include <linux/errno.h>
  32. #include <linux/init_task.h>
  33. #include <linux/kernel.h>
  34. #include <linux/list.h>
  35. #include <linux/mm.h>
  36. #include <linux/mutex.h>
  37. #include <linux/mount.h>
  38. #include <linux/pagemap.h>
  39. #include <linux/proc_fs.h>
  40. #include <linux/rcupdate.h>
  41. #include <linux/sched.h>
  42. #include <linux/backing-dev.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/slab.h>
  45. #include <linux/magic.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/string.h>
  48. #include <linux/sort.h>
  49. #include <linux/kmod.h>
  50. #include <linux/module.h>
  51. #include <linux/delayacct.h>
  52. #include <linux/cgroupstats.h>
  53. #include <linux/hashtable.h>
  54. #include <linux/namei.h>
  55. #include <linux/pid_namespace.h>
  56. #include <linux/idr.h>
  57. #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
  58. #include <linux/eventfd.h>
  59. #include <linux/poll.h>
  60. #include <linux/flex_array.h> /* used in cgroup_attach_task */
  61. #include <linux/kthread.h>
  62. #include <linux/atomic.h>
  63. /*
  64. * cgroup_mutex is the master lock. Any modification to cgroup or its
  65. * hierarchy must be performed while holding it.
  66. *
  67. * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
  68. * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
  69. * release_agent_path and so on. Modifying requires both cgroup_mutex and
  70. * cgroup_root_mutex. Readers can acquire either of the two. This is to
  71. * break the following locking order cycle.
  72. *
  73. * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
  74. * B. namespace_sem -> cgroup_mutex
  75. *
  76. * B happens only through cgroup_show_options() and using cgroup_root_mutex
  77. * breaks it.
  78. */
  79. #ifdef CONFIG_PROVE_RCU
  80. DEFINE_MUTEX(cgroup_mutex);
  81. EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
  82. #else
  83. static DEFINE_MUTEX(cgroup_mutex);
  84. #endif
  85. static DEFINE_MUTEX(cgroup_root_mutex);
  86. /*
  87. * Generate an array of cgroup subsystem pointers. At boot time, this is
  88. * populated with the built in subsystems, and modular subsystems are
  89. * registered after that. The mutable section of this array is protected by
  90. * cgroup_mutex.
  91. */
  92. #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
  93. #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
  94. static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
  95. #include <linux/cgroup_subsys.h>
  96. };
  97. /*
  98. * The dummy hierarchy, reserved for the subsystems that are otherwise
  99. * unattached - it never has more than a single cgroup, and all tasks are
  100. * part of that cgroup.
  101. */
  102. static struct cgroupfs_root cgroup_dummy_root;
  103. /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
  104. static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
  105. /*
  106. * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
  107. */
  108. struct cfent {
  109. struct list_head node;
  110. struct dentry *dentry;
  111. struct cftype *type;
  112. struct cgroup_subsys_state *css;
  113. /* file xattrs */
  114. struct simple_xattrs xattrs;
  115. };
  116. /*
  117. * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
  118. * cgroup_subsys->use_id != 0.
  119. */
  120. #define CSS_ID_MAX (65535)
  121. struct css_id {
  122. /*
  123. * The css to which this ID points. This pointer is set to valid value
  124. * after cgroup is populated. If cgroup is removed, this will be NULL.
  125. * This pointer is expected to be RCU-safe because destroy()
  126. * is called after synchronize_rcu(). But for safe use, css_tryget()
  127. * should be used for avoiding race.
  128. */
  129. struct cgroup_subsys_state __rcu *css;
  130. /*
  131. * ID of this css.
  132. */
  133. unsigned short id;
  134. /*
  135. * Depth in hierarchy which this ID belongs to.
  136. */
  137. unsigned short depth;
  138. /*
  139. * ID is freed by RCU. (and lookup routine is RCU safe.)
  140. */
  141. struct rcu_head rcu_head;
  142. /*
  143. * Hierarchy of CSS ID belongs to.
  144. */
  145. unsigned short stack[0]; /* Array of Length (depth+1) */
  146. };
  147. /*
  148. * cgroup_event represents events which userspace want to receive.
  149. */
  150. struct cgroup_event {
  151. /*
  152. * css which the event belongs to.
  153. */
  154. struct cgroup_subsys_state *css;
  155. /*
  156. * Control file which the event associated.
  157. */
  158. struct cftype *cft;
  159. /*
  160. * eventfd to signal userspace about the event.
  161. */
  162. struct eventfd_ctx *eventfd;
  163. /*
  164. * Each of these stored in a list by the cgroup.
  165. */
  166. struct list_head list;
  167. /*
  168. * All fields below needed to unregister event when
  169. * userspace closes eventfd.
  170. */
  171. poll_table pt;
  172. wait_queue_head_t *wqh;
  173. wait_queue_t wait;
  174. struct work_struct remove;
  175. };
  176. /* The list of hierarchy roots */
  177. static LIST_HEAD(cgroup_roots);
  178. static int cgroup_root_count;
  179. /*
  180. * Hierarchy ID allocation and mapping. It follows the same exclusion
  181. * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
  182. * writes, either for reads.
  183. */
  184. static DEFINE_IDR(cgroup_hierarchy_idr);
  185. static struct cgroup_name root_cgroup_name = { .name = "/" };
  186. /*
  187. * Assign a monotonically increasing serial number to cgroups. It
  188. * guarantees cgroups with bigger numbers are newer than those with smaller
  189. * numbers. Also, as cgroups are always appended to the parent's
  190. * ->children list, it guarantees that sibling cgroups are always sorted in
  191. * the ascending serial number order on the list. Protected by
  192. * cgroup_mutex.
  193. */
  194. static u64 cgroup_serial_nr_next = 1;
  195. /* This flag indicates whether tasks in the fork and exit paths should
  196. * check for fork/exit handlers to call. This avoids us having to do
  197. * extra work in the fork/exit path if none of the subsystems need to
  198. * be called.
  199. */
  200. static int need_forkexit_callback __read_mostly;
  201. static struct cftype cgroup_base_files[];
  202. static void cgroup_destroy_css_killed(struct cgroup *cgrp);
  203. static int cgroup_destroy_locked(struct cgroup *cgrp);
  204. static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
  205. bool is_add);
  206. /**
  207. * cgroup_css - obtain a cgroup's css for the specified subsystem
  208. * @cgrp: the cgroup of interest
  209. * @subsys_id: the subsystem of interest
  210. *
  211. * Return @cgrp's css (cgroup_subsys_state) associated with @subsys_id.
  212. * This function must be called either under cgroup_mutex or
  213. * rcu_read_lock() and the caller is responsible for pinning the returned
  214. * css if it wants to keep accessing it outside the said locks. This
  215. * function may return %NULL if @cgrp doesn't have @subsys_id enabled.
  216. */
  217. static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
  218. int subsys_id)
  219. {
  220. return rcu_dereference_check(cgrp->subsys[subsys_id],
  221. lockdep_is_held(&cgroup_mutex));
  222. }
  223. /* convenient tests for these bits */
  224. static inline bool cgroup_is_dead(const struct cgroup *cgrp)
  225. {
  226. return test_bit(CGRP_DEAD, &cgrp->flags);
  227. }
  228. /**
  229. * cgroup_is_descendant - test ancestry
  230. * @cgrp: the cgroup to be tested
  231. * @ancestor: possible ancestor of @cgrp
  232. *
  233. * Test whether @cgrp is a descendant of @ancestor. It also returns %true
  234. * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
  235. * and @ancestor are accessible.
  236. */
  237. bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
  238. {
  239. while (cgrp) {
  240. if (cgrp == ancestor)
  241. return true;
  242. cgrp = cgrp->parent;
  243. }
  244. return false;
  245. }
  246. EXPORT_SYMBOL_GPL(cgroup_is_descendant);
  247. static int cgroup_is_releasable(const struct cgroup *cgrp)
  248. {
  249. const int bits =
  250. (1 << CGRP_RELEASABLE) |
  251. (1 << CGRP_NOTIFY_ON_RELEASE);
  252. return (cgrp->flags & bits) == bits;
  253. }
  254. static int notify_on_release(const struct cgroup *cgrp)
  255. {
  256. return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  257. }
  258. /**
  259. * for_each_subsys - iterate all loaded cgroup subsystems
  260. * @ss: the iteration cursor
  261. * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
  262. *
  263. * Should be called under cgroup_mutex.
  264. */
  265. #define for_each_subsys(ss, i) \
  266. for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
  267. if (({ lockdep_assert_held(&cgroup_mutex); \
  268. !((ss) = cgroup_subsys[i]); })) { } \
  269. else
  270. /**
  271. * for_each_builtin_subsys - iterate all built-in cgroup subsystems
  272. * @ss: the iteration cursor
  273. * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
  274. *
  275. * Bulit-in subsystems are always present and iteration itself doesn't
  276. * require any synchronization.
  277. */
  278. #define for_each_builtin_subsys(ss, i) \
  279. for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
  280. (((ss) = cgroup_subsys[i]) || true); (i)++)
  281. /* iterate each subsystem attached to a hierarchy */
  282. #define for_each_root_subsys(root, ss) \
  283. list_for_each_entry((ss), &(root)->subsys_list, sibling)
  284. /* iterate across the active hierarchies */
  285. #define for_each_active_root(root) \
  286. list_for_each_entry((root), &cgroup_roots, root_list)
  287. static inline struct cgroup *__d_cgrp(struct dentry *dentry)
  288. {
  289. return dentry->d_fsdata;
  290. }
  291. static inline struct cfent *__d_cfe(struct dentry *dentry)
  292. {
  293. return dentry->d_fsdata;
  294. }
  295. static inline struct cftype *__d_cft(struct dentry *dentry)
  296. {
  297. return __d_cfe(dentry)->type;
  298. }
  299. /**
  300. * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
  301. * @cgrp: the cgroup to be checked for liveness
  302. *
  303. * On success, returns true; the mutex should be later unlocked. On
  304. * failure returns false with no lock held.
  305. */
  306. static bool cgroup_lock_live_group(struct cgroup *cgrp)
  307. {
  308. mutex_lock(&cgroup_mutex);
  309. if (cgroup_is_dead(cgrp)) {
  310. mutex_unlock(&cgroup_mutex);
  311. return false;
  312. }
  313. return true;
  314. }
  315. /* the list of cgroups eligible for automatic release. Protected by
  316. * release_list_lock */
  317. static LIST_HEAD(release_list);
  318. static DEFINE_RAW_SPINLOCK(release_list_lock);
  319. static void cgroup_release_agent(struct work_struct *work);
  320. static DECLARE_WORK(release_agent_work, cgroup_release_agent);
  321. static void check_for_release(struct cgroup *cgrp);
  322. /*
  323. * A cgroup can be associated with multiple css_sets as different tasks may
  324. * belong to different cgroups on different hierarchies. In the other
  325. * direction, a css_set is naturally associated with multiple cgroups.
  326. * This M:N relationship is represented by the following link structure
  327. * which exists for each association and allows traversing the associations
  328. * from both sides.
  329. */
  330. struct cgrp_cset_link {
  331. /* the cgroup and css_set this link associates */
  332. struct cgroup *cgrp;
  333. struct css_set *cset;
  334. /* list of cgrp_cset_links anchored at cgrp->cset_links */
  335. struct list_head cset_link;
  336. /* list of cgrp_cset_links anchored at css_set->cgrp_links */
  337. struct list_head cgrp_link;
  338. };
  339. /* The default css_set - used by init and its children prior to any
  340. * hierarchies being mounted. It contains a pointer to the root state
  341. * for each subsystem. Also used to anchor the list of css_sets. Not
  342. * reference-counted, to improve performance when child cgroups
  343. * haven't been created.
  344. */
  345. static struct css_set init_css_set;
  346. static struct cgrp_cset_link init_cgrp_cset_link;
  347. static int cgroup_init_idr(struct cgroup_subsys *ss,
  348. struct cgroup_subsys_state *css);
  349. /*
  350. * css_set_lock protects the list of css_set objects, and the chain of
  351. * tasks off each css_set. Nests outside task->alloc_lock due to
  352. * css_task_iter_start().
  353. */
  354. static DEFINE_RWLOCK(css_set_lock);
  355. static int css_set_count;
  356. /*
  357. * hash table for cgroup groups. This improves the performance to find
  358. * an existing css_set. This hash doesn't (currently) take into
  359. * account cgroups in empty hierarchies.
  360. */
  361. #define CSS_SET_HASH_BITS 7
  362. static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
  363. static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
  364. {
  365. unsigned long key = 0UL;
  366. struct cgroup_subsys *ss;
  367. int i;
  368. for_each_subsys(ss, i)
  369. key += (unsigned long)css[i];
  370. key = (key >> 16) ^ key;
  371. return key;
  372. }
  373. /*
  374. * We don't maintain the lists running through each css_set to its task
  375. * until after the first call to css_task_iter_start(). This reduces the
  376. * fork()/exit() overhead for people who have cgroups compiled into their
  377. * kernel but not actually in use.
  378. */
  379. static int use_task_css_set_links __read_mostly;
  380. static void __put_css_set(struct css_set *cset, int taskexit)
  381. {
  382. struct cgrp_cset_link *link, *tmp_link;
  383. /*
  384. * Ensure that the refcount doesn't hit zero while any readers
  385. * can see it. Similar to atomic_dec_and_lock(), but for an
  386. * rwlock
  387. */
  388. if (atomic_add_unless(&cset->refcount, -1, 1))
  389. return;
  390. write_lock(&css_set_lock);
  391. if (!atomic_dec_and_test(&cset->refcount)) {
  392. write_unlock(&css_set_lock);
  393. return;
  394. }
  395. /* This css_set is dead. unlink it and release cgroup refcounts */
  396. hash_del(&cset->hlist);
  397. css_set_count--;
  398. list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
  399. struct cgroup *cgrp = link->cgrp;
  400. list_del(&link->cset_link);
  401. list_del(&link->cgrp_link);
  402. /* @cgrp can't go away while we're holding css_set_lock */
  403. if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
  404. if (taskexit)
  405. set_bit(CGRP_RELEASABLE, &cgrp->flags);
  406. check_for_release(cgrp);
  407. }
  408. kfree(link);
  409. }
  410. write_unlock(&css_set_lock);
  411. kfree_rcu(cset, rcu_head);
  412. }
  413. /*
  414. * refcounted get/put for css_set objects
  415. */
  416. static inline void get_css_set(struct css_set *cset)
  417. {
  418. atomic_inc(&cset->refcount);
  419. }
  420. static inline void put_css_set(struct css_set *cset)
  421. {
  422. __put_css_set(cset, 0);
  423. }
  424. static inline void put_css_set_taskexit(struct css_set *cset)
  425. {
  426. __put_css_set(cset, 1);
  427. }
  428. /**
  429. * compare_css_sets - helper function for find_existing_css_set().
  430. * @cset: candidate css_set being tested
  431. * @old_cset: existing css_set for a task
  432. * @new_cgrp: cgroup that's being entered by the task
  433. * @template: desired set of css pointers in css_set (pre-calculated)
  434. *
  435. * Returns true if "cset" matches "old_cset" except for the hierarchy
  436. * which "new_cgrp" belongs to, for which it should match "new_cgrp".
  437. */
  438. static bool compare_css_sets(struct css_set *cset,
  439. struct css_set *old_cset,
  440. struct cgroup *new_cgrp,
  441. struct cgroup_subsys_state *template[])
  442. {
  443. struct list_head *l1, *l2;
  444. if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
  445. /* Not all subsystems matched */
  446. return false;
  447. }
  448. /*
  449. * Compare cgroup pointers in order to distinguish between
  450. * different cgroups in heirarchies with no subsystems. We
  451. * could get by with just this check alone (and skip the
  452. * memcmp above) but on most setups the memcmp check will
  453. * avoid the need for this more expensive check on almost all
  454. * candidates.
  455. */
  456. l1 = &cset->cgrp_links;
  457. l2 = &old_cset->cgrp_links;
  458. while (1) {
  459. struct cgrp_cset_link *link1, *link2;
  460. struct cgroup *cgrp1, *cgrp2;
  461. l1 = l1->next;
  462. l2 = l2->next;
  463. /* See if we reached the end - both lists are equal length. */
  464. if (l1 == &cset->cgrp_links) {
  465. BUG_ON(l2 != &old_cset->cgrp_links);
  466. break;
  467. } else {
  468. BUG_ON(l2 == &old_cset->cgrp_links);
  469. }
  470. /* Locate the cgroups associated with these links. */
  471. link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
  472. link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
  473. cgrp1 = link1->cgrp;
  474. cgrp2 = link2->cgrp;
  475. /* Hierarchies should be linked in the same order. */
  476. BUG_ON(cgrp1->root != cgrp2->root);
  477. /*
  478. * If this hierarchy is the hierarchy of the cgroup
  479. * that's changing, then we need to check that this
  480. * css_set points to the new cgroup; if it's any other
  481. * hierarchy, then this css_set should point to the
  482. * same cgroup as the old css_set.
  483. */
  484. if (cgrp1->root == new_cgrp->root) {
  485. if (cgrp1 != new_cgrp)
  486. return false;
  487. } else {
  488. if (cgrp1 != cgrp2)
  489. return false;
  490. }
  491. }
  492. return true;
  493. }
  494. /**
  495. * find_existing_css_set - init css array and find the matching css_set
  496. * @old_cset: the css_set that we're using before the cgroup transition
  497. * @cgrp: the cgroup that we're moving into
  498. * @template: out param for the new set of csses, should be clear on entry
  499. */
  500. static struct css_set *find_existing_css_set(struct css_set *old_cset,
  501. struct cgroup *cgrp,
  502. struct cgroup_subsys_state *template[])
  503. {
  504. struct cgroupfs_root *root = cgrp->root;
  505. struct cgroup_subsys *ss;
  506. struct css_set *cset;
  507. unsigned long key;
  508. int i;
  509. /*
  510. * Build the set of subsystem state objects that we want to see in the
  511. * new css_set. while subsystems can change globally, the entries here
  512. * won't change, so no need for locking.
  513. */
  514. for_each_subsys(ss, i) {
  515. if (root->subsys_mask & (1UL << i)) {
  516. /* Subsystem is in this hierarchy. So we want
  517. * the subsystem state from the new
  518. * cgroup */
  519. template[i] = cgroup_css(cgrp, i);
  520. } else {
  521. /* Subsystem is not in this hierarchy, so we
  522. * don't want to change the subsystem state */
  523. template[i] = old_cset->subsys[i];
  524. }
  525. }
  526. key = css_set_hash(template);
  527. hash_for_each_possible(css_set_table, cset, hlist, key) {
  528. if (!compare_css_sets(cset, old_cset, cgrp, template))
  529. continue;
  530. /* This css_set matches what we need */
  531. return cset;
  532. }
  533. /* No existing cgroup group matched */
  534. return NULL;
  535. }
  536. static void free_cgrp_cset_links(struct list_head *links_to_free)
  537. {
  538. struct cgrp_cset_link *link, *tmp_link;
  539. list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
  540. list_del(&link->cset_link);
  541. kfree(link);
  542. }
  543. }
  544. /**
  545. * allocate_cgrp_cset_links - allocate cgrp_cset_links
  546. * @count: the number of links to allocate
  547. * @tmp_links: list_head the allocated links are put on
  548. *
  549. * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
  550. * through ->cset_link. Returns 0 on success or -errno.
  551. */
  552. static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
  553. {
  554. struct cgrp_cset_link *link;
  555. int i;
  556. INIT_LIST_HEAD(tmp_links);
  557. for (i = 0; i < count; i++) {
  558. link = kzalloc(sizeof(*link), GFP_KERNEL);
  559. if (!link) {
  560. free_cgrp_cset_links(tmp_links);
  561. return -ENOMEM;
  562. }
  563. list_add(&link->cset_link, tmp_links);
  564. }
  565. return 0;
  566. }
  567. /**
  568. * link_css_set - a helper function to link a css_set to a cgroup
  569. * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
  570. * @cset: the css_set to be linked
  571. * @cgrp: the destination cgroup
  572. */
  573. static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
  574. struct cgroup *cgrp)
  575. {
  576. struct cgrp_cset_link *link;
  577. BUG_ON(list_empty(tmp_links));
  578. link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
  579. link->cset = cset;
  580. link->cgrp = cgrp;
  581. list_move(&link->cset_link, &cgrp->cset_links);
  582. /*
  583. * Always add links to the tail of the list so that the list
  584. * is sorted by order of hierarchy creation
  585. */
  586. list_add_tail(&link->cgrp_link, &cset->cgrp_links);
  587. }
  588. /**
  589. * find_css_set - return a new css_set with one cgroup updated
  590. * @old_cset: the baseline css_set
  591. * @cgrp: the cgroup to be updated
  592. *
  593. * Return a new css_set that's equivalent to @old_cset, but with @cgrp
  594. * substituted into the appropriate hierarchy.
  595. */
  596. static struct css_set *find_css_set(struct css_set *old_cset,
  597. struct cgroup *cgrp)
  598. {
  599. struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
  600. struct css_set *cset;
  601. struct list_head tmp_links;
  602. struct cgrp_cset_link *link;
  603. unsigned long key;
  604. lockdep_assert_held(&cgroup_mutex);
  605. /* First see if we already have a cgroup group that matches
  606. * the desired set */
  607. read_lock(&css_set_lock);
  608. cset = find_existing_css_set(old_cset, cgrp, template);
  609. if (cset)
  610. get_css_set(cset);
  611. read_unlock(&css_set_lock);
  612. if (cset)
  613. return cset;
  614. cset = kzalloc(sizeof(*cset), GFP_KERNEL);
  615. if (!cset)
  616. return NULL;
  617. /* Allocate all the cgrp_cset_link objects that we'll need */
  618. if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
  619. kfree(cset);
  620. return NULL;
  621. }
  622. atomic_set(&cset->refcount, 1);
  623. INIT_LIST_HEAD(&cset->cgrp_links);
  624. INIT_LIST_HEAD(&cset->tasks);
  625. INIT_HLIST_NODE(&cset->hlist);
  626. /* Copy the set of subsystem state objects generated in
  627. * find_existing_css_set() */
  628. memcpy(cset->subsys, template, sizeof(cset->subsys));
  629. write_lock(&css_set_lock);
  630. /* Add reference counts and links from the new css_set. */
  631. list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
  632. struct cgroup *c = link->cgrp;
  633. if (c->root == cgrp->root)
  634. c = cgrp;
  635. link_css_set(&tmp_links, cset, c);
  636. }
  637. BUG_ON(!list_empty(&tmp_links));
  638. css_set_count++;
  639. /* Add this cgroup group to the hash table */
  640. key = css_set_hash(cset->subsys);
  641. hash_add(css_set_table, &cset->hlist, key);
  642. write_unlock(&css_set_lock);
  643. return cset;
  644. }
  645. /*
  646. * Return the cgroup for "task" from the given hierarchy. Must be
  647. * called with cgroup_mutex held.
  648. */
  649. static struct cgroup *task_cgroup_from_root(struct task_struct *task,
  650. struct cgroupfs_root *root)
  651. {
  652. struct css_set *cset;
  653. struct cgroup *res = NULL;
  654. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  655. read_lock(&css_set_lock);
  656. /*
  657. * No need to lock the task - since we hold cgroup_mutex the
  658. * task can't change groups, so the only thing that can happen
  659. * is that it exits and its css is set back to init_css_set.
  660. */
  661. cset = task_css_set(task);
  662. if (cset == &init_css_set) {
  663. res = &root->top_cgroup;
  664. } else {
  665. struct cgrp_cset_link *link;
  666. list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  667. struct cgroup *c = link->cgrp;
  668. if (c->root == root) {
  669. res = c;
  670. break;
  671. }
  672. }
  673. }
  674. read_unlock(&css_set_lock);
  675. BUG_ON(!res);
  676. return res;
  677. }
  678. /*
  679. * There is one global cgroup mutex. We also require taking
  680. * task_lock() when dereferencing a task's cgroup subsys pointers.
  681. * See "The task_lock() exception", at the end of this comment.
  682. *
  683. * A task must hold cgroup_mutex to modify cgroups.
  684. *
  685. * Any task can increment and decrement the count field without lock.
  686. * So in general, code holding cgroup_mutex can't rely on the count
  687. * field not changing. However, if the count goes to zero, then only
  688. * cgroup_attach_task() can increment it again. Because a count of zero
  689. * means that no tasks are currently attached, therefore there is no
  690. * way a task attached to that cgroup can fork (the other way to
  691. * increment the count). So code holding cgroup_mutex can safely
  692. * assume that if the count is zero, it will stay zero. Similarly, if
  693. * a task holds cgroup_mutex on a cgroup with zero count, it
  694. * knows that the cgroup won't be removed, as cgroup_rmdir()
  695. * needs that mutex.
  696. *
  697. * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
  698. * (usually) take cgroup_mutex. These are the two most performance
  699. * critical pieces of code here. The exception occurs on cgroup_exit(),
  700. * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
  701. * is taken, and if the cgroup count is zero, a usermode call made
  702. * to the release agent with the name of the cgroup (path relative to
  703. * the root of cgroup file system) as the argument.
  704. *
  705. * A cgroup can only be deleted if both its 'count' of using tasks
  706. * is zero, and its list of 'children' cgroups is empty. Since all
  707. * tasks in the system use _some_ cgroup, and since there is always at
  708. * least one task in the system (init, pid == 1), therefore, top_cgroup
  709. * always has either children cgroups and/or using tasks. So we don't
  710. * need a special hack to ensure that top_cgroup cannot be deleted.
  711. *
  712. * The task_lock() exception
  713. *
  714. * The need for this exception arises from the action of
  715. * cgroup_attach_task(), which overwrites one task's cgroup pointer with
  716. * another. It does so using cgroup_mutex, however there are
  717. * several performance critical places that need to reference
  718. * task->cgroup without the expense of grabbing a system global
  719. * mutex. Therefore except as noted below, when dereferencing or, as
  720. * in cgroup_attach_task(), modifying a task's cgroup pointer we use
  721. * task_lock(), which acts on a spinlock (task->alloc_lock) already in
  722. * the task_struct routinely used for such matters.
  723. *
  724. * P.S. One more locking exception. RCU is used to guard the
  725. * update of a tasks cgroup pointer by cgroup_attach_task()
  726. */
  727. /*
  728. * A couple of forward declarations required, due to cyclic reference loop:
  729. * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
  730. * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
  731. * -> cgroup_mkdir.
  732. */
  733. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
  734. static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
  735. static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
  736. static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
  737. static const struct inode_operations cgroup_dir_inode_operations;
  738. static const struct file_operations proc_cgroupstats_operations;
  739. static struct backing_dev_info cgroup_backing_dev_info = {
  740. .name = "cgroup",
  741. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
  742. };
  743. static int alloc_css_id(struct cgroup_subsys_state *child_css);
  744. static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
  745. {
  746. struct inode *inode = new_inode(sb);
  747. if (inode) {
  748. inode->i_ino = get_next_ino();
  749. inode->i_mode = mode;
  750. inode->i_uid = current_fsuid();
  751. inode->i_gid = current_fsgid();
  752. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  753. inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
  754. }
  755. return inode;
  756. }
  757. static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
  758. {
  759. struct cgroup_name *name;
  760. name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
  761. if (!name)
  762. return NULL;
  763. strcpy(name->name, dentry->d_name.name);
  764. return name;
  765. }
  766. static void cgroup_free_fn(struct work_struct *work)
  767. {
  768. struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
  769. mutex_lock(&cgroup_mutex);
  770. cgrp->root->number_of_cgroups--;
  771. mutex_unlock(&cgroup_mutex);
  772. /*
  773. * We get a ref to the parent's dentry, and put the ref when
  774. * this cgroup is being freed, so it's guaranteed that the
  775. * parent won't be destroyed before its children.
  776. */
  777. dput(cgrp->parent->dentry);
  778. /*
  779. * Drop the active superblock reference that we took when we
  780. * created the cgroup. This will free cgrp->root, if we are
  781. * holding the last reference to @sb.
  782. */
  783. deactivate_super(cgrp->root->sb);
  784. /*
  785. * if we're getting rid of the cgroup, refcount should ensure
  786. * that there are no pidlists left.
  787. */
  788. BUG_ON(!list_empty(&cgrp->pidlists));
  789. simple_xattrs_free(&cgrp->xattrs);
  790. kfree(rcu_dereference_raw(cgrp->name));
  791. kfree(cgrp);
  792. }
  793. static void cgroup_free_rcu(struct rcu_head *head)
  794. {
  795. struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
  796. INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
  797. schedule_work(&cgrp->destroy_work);
  798. }
  799. static void cgroup_diput(struct dentry *dentry, struct inode *inode)
  800. {
  801. /* is dentry a directory ? if so, kfree() associated cgroup */
  802. if (S_ISDIR(inode->i_mode)) {
  803. struct cgroup *cgrp = dentry->d_fsdata;
  804. BUG_ON(!(cgroup_is_dead(cgrp)));
  805. call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
  806. } else {
  807. struct cfent *cfe = __d_cfe(dentry);
  808. struct cgroup *cgrp = dentry->d_parent->d_fsdata;
  809. WARN_ONCE(!list_empty(&cfe->node) &&
  810. cgrp != &cgrp->root->top_cgroup,
  811. "cfe still linked for %s\n", cfe->type->name);
  812. simple_xattrs_free(&cfe->xattrs);
  813. kfree(cfe);
  814. }
  815. iput(inode);
  816. }
  817. static int cgroup_delete(const struct dentry *d)
  818. {
  819. return 1;
  820. }
  821. static void remove_dir(struct dentry *d)
  822. {
  823. struct dentry *parent = dget(d->d_parent);
  824. d_delete(d);
  825. simple_rmdir(parent->d_inode, d);
  826. dput(parent);
  827. }
  828. static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
  829. {
  830. struct cfent *cfe;
  831. lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
  832. lockdep_assert_held(&cgroup_mutex);
  833. /*
  834. * If we're doing cleanup due to failure of cgroup_create(),
  835. * the corresponding @cfe may not exist.
  836. */
  837. list_for_each_entry(cfe, &cgrp->files, node) {
  838. struct dentry *d = cfe->dentry;
  839. if (cft && cfe->type != cft)
  840. continue;
  841. dget(d);
  842. d_delete(d);
  843. simple_unlink(cgrp->dentry->d_inode, d);
  844. list_del_init(&cfe->node);
  845. dput(d);
  846. break;
  847. }
  848. }
  849. /**
  850. * cgroup_clear_dir - remove subsys files in a cgroup directory
  851. * @cgrp: target cgroup
  852. * @subsys_mask: mask of the subsystem ids whose files should be removed
  853. */
  854. static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
  855. {
  856. struct cgroup_subsys *ss;
  857. int i;
  858. for_each_subsys(ss, i) {
  859. struct cftype_set *set;
  860. if (!test_bit(i, &subsys_mask))
  861. continue;
  862. list_for_each_entry(set, &ss->cftsets, node)
  863. cgroup_addrm_files(cgrp, set->cfts, false);
  864. }
  865. }
  866. /*
  867. * NOTE : the dentry must have been dget()'ed
  868. */
  869. static void cgroup_d_remove_dir(struct dentry *dentry)
  870. {
  871. struct dentry *parent;
  872. parent = dentry->d_parent;
  873. spin_lock(&parent->d_lock);
  874. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  875. list_del_init(&dentry->d_u.d_child);
  876. spin_unlock(&dentry->d_lock);
  877. spin_unlock(&parent->d_lock);
  878. remove_dir(dentry);
  879. }
  880. /*
  881. * Call with cgroup_mutex held. Drops reference counts on modules, including
  882. * any duplicate ones that parse_cgroupfs_options took. If this function
  883. * returns an error, no reference counts are touched.
  884. */
  885. static int rebind_subsystems(struct cgroupfs_root *root,
  886. unsigned long added_mask, unsigned removed_mask)
  887. {
  888. struct cgroup *cgrp = &root->top_cgroup;
  889. struct cgroup_subsys *ss;
  890. unsigned long pinned = 0;
  891. int i, ret;
  892. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  893. BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
  894. /* Check that any added subsystems are currently free */
  895. for_each_subsys(ss, i) {
  896. if (!(added_mask & (1 << i)))
  897. continue;
  898. /* is the subsystem mounted elsewhere? */
  899. if (ss->root != &cgroup_dummy_root) {
  900. ret = -EBUSY;
  901. goto out_put;
  902. }
  903. /* pin the module */
  904. if (!try_module_get(ss->module)) {
  905. ret = -ENOENT;
  906. goto out_put;
  907. }
  908. pinned |= 1 << i;
  909. }
  910. /* subsys could be missing if unloaded between parsing and here */
  911. if (added_mask != pinned) {
  912. ret = -ENOENT;
  913. goto out_put;
  914. }
  915. ret = cgroup_populate_dir(cgrp, added_mask);
  916. if (ret)
  917. goto out_put;
  918. /*
  919. * Nothing can fail from this point on. Remove files for the
  920. * removed subsystems and rebind each subsystem.
  921. */
  922. cgroup_clear_dir(cgrp, removed_mask);
  923. for_each_subsys(ss, i) {
  924. unsigned long bit = 1UL << i;
  925. if (bit & added_mask) {
  926. /* We're binding this subsystem to this hierarchy */
  927. BUG_ON(cgroup_css(cgrp, i));
  928. BUG_ON(!cgroup_css(cgroup_dummy_top, i));
  929. BUG_ON(cgroup_css(cgroup_dummy_top, i)->cgroup != cgroup_dummy_top);
  930. rcu_assign_pointer(cgrp->subsys[i],
  931. cgroup_css(cgroup_dummy_top, i));
  932. cgroup_css(cgrp, i)->cgroup = cgrp;
  933. list_move(&ss->sibling, &root->subsys_list);
  934. ss->root = root;
  935. if (ss->bind)
  936. ss->bind(cgroup_css(cgrp, i));
  937. /* refcount was already taken, and we're keeping it */
  938. root->subsys_mask |= bit;
  939. } else if (bit & removed_mask) {
  940. /* We're removing this subsystem */
  941. BUG_ON(cgroup_css(cgrp, i) != cgroup_css(cgroup_dummy_top, i));
  942. BUG_ON(cgroup_css(cgrp, i)->cgroup != cgrp);
  943. if (ss->bind)
  944. ss->bind(cgroup_css(cgroup_dummy_top, i));
  945. cgroup_css(cgroup_dummy_top, i)->cgroup = cgroup_dummy_top;
  946. RCU_INIT_POINTER(cgrp->subsys[i], NULL);
  947. cgroup_subsys[i]->root = &cgroup_dummy_root;
  948. list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
  949. /* subsystem is now free - drop reference on module */
  950. module_put(ss->module);
  951. root->subsys_mask &= ~bit;
  952. }
  953. }
  954. /*
  955. * Mark @root has finished binding subsystems. @root->subsys_mask
  956. * now matches the bound subsystems.
  957. */
  958. root->flags |= CGRP_ROOT_SUBSYS_BOUND;
  959. return 0;
  960. out_put:
  961. for_each_subsys(ss, i)
  962. if (pinned & (1 << i))
  963. module_put(ss->module);
  964. return ret;
  965. }
  966. static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
  967. {
  968. struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
  969. struct cgroup_subsys *ss;
  970. mutex_lock(&cgroup_root_mutex);
  971. for_each_root_subsys(root, ss)
  972. seq_printf(seq, ",%s", ss->name);
  973. if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
  974. seq_puts(seq, ",sane_behavior");
  975. if (root->flags & CGRP_ROOT_NOPREFIX)
  976. seq_puts(seq, ",noprefix");
  977. if (root->flags & CGRP_ROOT_XATTR)
  978. seq_puts(seq, ",xattr");
  979. if (strlen(root->release_agent_path))
  980. seq_printf(seq, ",release_agent=%s", root->release_agent_path);
  981. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
  982. seq_puts(seq, ",clone_children");
  983. if (strlen(root->name))
  984. seq_printf(seq, ",name=%s", root->name);
  985. mutex_unlock(&cgroup_root_mutex);
  986. return 0;
  987. }
  988. struct cgroup_sb_opts {
  989. unsigned long subsys_mask;
  990. unsigned long flags;
  991. char *release_agent;
  992. bool cpuset_clone_children;
  993. char *name;
  994. /* User explicitly requested empty subsystem */
  995. bool none;
  996. struct cgroupfs_root *new_root;
  997. };
  998. /*
  999. * Convert a hierarchy specifier into a bitmask of subsystems and
  1000. * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
  1001. * array. This function takes refcounts on subsystems to be used, unless it
  1002. * returns error, in which case no refcounts are taken.
  1003. */
  1004. static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
  1005. {
  1006. char *token, *o = data;
  1007. bool all_ss = false, one_ss = false;
  1008. unsigned long mask = (unsigned long)-1;
  1009. struct cgroup_subsys *ss;
  1010. int i;
  1011. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  1012. #ifdef CONFIG_CPUSETS
  1013. mask = ~(1UL << cpuset_subsys_id);
  1014. #endif
  1015. memset(opts, 0, sizeof(*opts));
  1016. while ((token = strsep(&o, ",")) != NULL) {
  1017. if (!*token)
  1018. return -EINVAL;
  1019. if (!strcmp(token, "none")) {
  1020. /* Explicitly have no subsystems */
  1021. opts->none = true;
  1022. continue;
  1023. }
  1024. if (!strcmp(token, "all")) {
  1025. /* Mutually exclusive option 'all' + subsystem name */
  1026. if (one_ss)
  1027. return -EINVAL;
  1028. all_ss = true;
  1029. continue;
  1030. }
  1031. if (!strcmp(token, "__DEVEL__sane_behavior")) {
  1032. opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
  1033. continue;
  1034. }
  1035. if (!strcmp(token, "noprefix")) {
  1036. opts->flags |= CGRP_ROOT_NOPREFIX;
  1037. continue;
  1038. }
  1039. if (!strcmp(token, "clone_children")) {
  1040. opts->cpuset_clone_children = true;
  1041. continue;
  1042. }
  1043. if (!strcmp(token, "xattr")) {
  1044. opts->flags |= CGRP_ROOT_XATTR;
  1045. continue;
  1046. }
  1047. if (!strncmp(token, "release_agent=", 14)) {
  1048. /* Specifying two release agents is forbidden */
  1049. if (opts->release_agent)
  1050. return -EINVAL;
  1051. opts->release_agent =
  1052. kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
  1053. if (!opts->release_agent)
  1054. return -ENOMEM;
  1055. continue;
  1056. }
  1057. if (!strncmp(token, "name=", 5)) {
  1058. const char *name = token + 5;
  1059. /* Can't specify an empty name */
  1060. if (!strlen(name))
  1061. return -EINVAL;
  1062. /* Must match [\w.-]+ */
  1063. for (i = 0; i < strlen(name); i++) {
  1064. char c = name[i];
  1065. if (isalnum(c))
  1066. continue;
  1067. if ((c == '.') || (c == '-') || (c == '_'))
  1068. continue;
  1069. return -EINVAL;
  1070. }
  1071. /* Specifying two names is forbidden */
  1072. if (opts->name)
  1073. return -EINVAL;
  1074. opts->name = kstrndup(name,
  1075. MAX_CGROUP_ROOT_NAMELEN - 1,
  1076. GFP_KERNEL);
  1077. if (!opts->name)
  1078. return -ENOMEM;
  1079. continue;
  1080. }
  1081. for_each_subsys(ss, i) {
  1082. if (strcmp(token, ss->name))
  1083. continue;
  1084. if (ss->disabled)
  1085. continue;
  1086. /* Mutually exclusive option 'all' + subsystem name */
  1087. if (all_ss)
  1088. return -EINVAL;
  1089. set_bit(i, &opts->subsys_mask);
  1090. one_ss = true;
  1091. break;
  1092. }
  1093. if (i == CGROUP_SUBSYS_COUNT)
  1094. return -ENOENT;
  1095. }
  1096. /*
  1097. * If the 'all' option was specified select all the subsystems,
  1098. * otherwise if 'none', 'name=' and a subsystem name options
  1099. * were not specified, let's default to 'all'
  1100. */
  1101. if (all_ss || (!one_ss && !opts->none && !opts->name))
  1102. for_each_subsys(ss, i)
  1103. if (!ss->disabled)
  1104. set_bit(i, &opts->subsys_mask);
  1105. /* Consistency checks */
  1106. if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
  1107. pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
  1108. if (opts->flags & CGRP_ROOT_NOPREFIX) {
  1109. pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
  1110. return -EINVAL;
  1111. }
  1112. if (opts->cpuset_clone_children) {
  1113. pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
  1114. return -EINVAL;
  1115. }
  1116. }
  1117. /*
  1118. * Option noprefix was introduced just for backward compatibility
  1119. * with the old cpuset, so we allow noprefix only if mounting just
  1120. * the cpuset subsystem.
  1121. */
  1122. if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
  1123. return -EINVAL;
  1124. /* Can't specify "none" and some subsystems */
  1125. if (opts->subsys_mask && opts->none)
  1126. return -EINVAL;
  1127. /*
  1128. * We either have to specify by name or by subsystems. (So all
  1129. * empty hierarchies must have a name).
  1130. */
  1131. if (!opts->subsys_mask && !opts->name)
  1132. return -EINVAL;
  1133. return 0;
  1134. }
  1135. static int cgroup_remount(struct super_block *sb, int *flags, char *data)
  1136. {
  1137. int ret = 0;
  1138. struct cgroupfs_root *root = sb->s_fs_info;
  1139. struct cgroup *cgrp = &root->top_cgroup;
  1140. struct cgroup_sb_opts opts;
  1141. unsigned long added_mask, removed_mask;
  1142. if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
  1143. pr_err("cgroup: sane_behavior: remount is not allowed\n");
  1144. return -EINVAL;
  1145. }
  1146. mutex_lock(&cgrp->dentry->d_inode->i_mutex);
  1147. mutex_lock(&cgroup_mutex);
  1148. mutex_lock(&cgroup_root_mutex);
  1149. /* See what subsystems are wanted */
  1150. ret = parse_cgroupfs_options(data, &opts);
  1151. if (ret)
  1152. goto out_unlock;
  1153. if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
  1154. pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
  1155. task_tgid_nr(current), current->comm);
  1156. added_mask = opts.subsys_mask & ~root->subsys_mask;
  1157. removed_mask = root->subsys_mask & ~opts.subsys_mask;
  1158. /* Don't allow flags or name to change at remount */
  1159. if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
  1160. (opts.name && strcmp(opts.name, root->name))) {
  1161. pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
  1162. opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
  1163. root->flags & CGRP_ROOT_OPTION_MASK, root->name);
  1164. ret = -EINVAL;
  1165. goto out_unlock;
  1166. }
  1167. /* remounting is not allowed for populated hierarchies */
  1168. if (root->number_of_cgroups > 1) {
  1169. ret = -EBUSY;
  1170. goto out_unlock;
  1171. }
  1172. ret = rebind_subsystems(root, added_mask, removed_mask);
  1173. if (ret)
  1174. goto out_unlock;
  1175. if (opts.release_agent)
  1176. strcpy(root->release_agent_path, opts.release_agent);
  1177. out_unlock:
  1178. kfree(opts.release_agent);
  1179. kfree(opts.name);
  1180. mutex_unlock(&cgroup_root_mutex);
  1181. mutex_unlock(&cgroup_mutex);
  1182. mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
  1183. return ret;
  1184. }
  1185. static const struct super_operations cgroup_ops = {
  1186. .statfs = simple_statfs,
  1187. .drop_inode = generic_delete_inode,
  1188. .show_options = cgroup_show_options,
  1189. .remount_fs = cgroup_remount,
  1190. };
  1191. static void init_cgroup_housekeeping(struct cgroup *cgrp)
  1192. {
  1193. INIT_LIST_HEAD(&cgrp->sibling);
  1194. INIT_LIST_HEAD(&cgrp->children);
  1195. INIT_LIST_HEAD(&cgrp->files);
  1196. INIT_LIST_HEAD(&cgrp->cset_links);
  1197. INIT_LIST_HEAD(&cgrp->release_list);
  1198. INIT_LIST_HEAD(&cgrp->pidlists);
  1199. mutex_init(&cgrp->pidlist_mutex);
  1200. cgrp->dummy_css.cgroup = cgrp;
  1201. INIT_LIST_HEAD(&cgrp->event_list);
  1202. spin_lock_init(&cgrp->event_list_lock);
  1203. simple_xattrs_init(&cgrp->xattrs);
  1204. }
  1205. static void init_cgroup_root(struct cgroupfs_root *root)
  1206. {
  1207. struct cgroup *cgrp = &root->top_cgroup;
  1208. INIT_LIST_HEAD(&root->subsys_list);
  1209. INIT_LIST_HEAD(&root->root_list);
  1210. root->number_of_cgroups = 1;
  1211. cgrp->root = root;
  1212. RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
  1213. init_cgroup_housekeeping(cgrp);
  1214. idr_init(&root->cgroup_idr);
  1215. }
  1216. static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
  1217. {
  1218. int id;
  1219. lockdep_assert_held(&cgroup_mutex);
  1220. lockdep_assert_held(&cgroup_root_mutex);
  1221. id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
  1222. GFP_KERNEL);
  1223. if (id < 0)
  1224. return id;
  1225. root->hierarchy_id = id;
  1226. return 0;
  1227. }
  1228. static void cgroup_exit_root_id(struct cgroupfs_root *root)
  1229. {
  1230. lockdep_assert_held(&cgroup_mutex);
  1231. lockdep_assert_held(&cgroup_root_mutex);
  1232. if (root->hierarchy_id) {
  1233. idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
  1234. root->hierarchy_id = 0;
  1235. }
  1236. }
  1237. static int cgroup_test_super(struct super_block *sb, void *data)
  1238. {
  1239. struct cgroup_sb_opts *opts = data;
  1240. struct cgroupfs_root *root = sb->s_fs_info;
  1241. /* If we asked for a name then it must match */
  1242. if (opts->name && strcmp(opts->name, root->name))
  1243. return 0;
  1244. /*
  1245. * If we asked for subsystems (or explicitly for no
  1246. * subsystems) then they must match
  1247. */
  1248. if ((opts->subsys_mask || opts->none)
  1249. && (opts->subsys_mask != root->subsys_mask))
  1250. return 0;
  1251. return 1;
  1252. }
  1253. static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
  1254. {
  1255. struct cgroupfs_root *root;
  1256. if (!opts->subsys_mask && !opts->none)
  1257. return NULL;
  1258. root = kzalloc(sizeof(*root), GFP_KERNEL);
  1259. if (!root)
  1260. return ERR_PTR(-ENOMEM);
  1261. init_cgroup_root(root);
  1262. /*
  1263. * We need to set @root->subsys_mask now so that @root can be
  1264. * matched by cgroup_test_super() before it finishes
  1265. * initialization; otherwise, competing mounts with the same
  1266. * options may try to bind the same subsystems instead of waiting
  1267. * for the first one leading to unexpected mount errors.
  1268. * SUBSYS_BOUND will be set once actual binding is complete.
  1269. */
  1270. root->subsys_mask = opts->subsys_mask;
  1271. root->flags = opts->flags;
  1272. if (opts->release_agent)
  1273. strcpy(root->release_agent_path, opts->release_agent);
  1274. if (opts->name)
  1275. strcpy(root->name, opts->name);
  1276. if (opts->cpuset_clone_children)
  1277. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
  1278. return root;
  1279. }
  1280. static void cgroup_free_root(struct cgroupfs_root *root)
  1281. {
  1282. if (root) {
  1283. /* hierarhcy ID shoulid already have been released */
  1284. WARN_ON_ONCE(root->hierarchy_id);
  1285. idr_destroy(&root->cgroup_idr);
  1286. kfree(root);
  1287. }
  1288. }
  1289. static int cgroup_set_super(struct super_block *sb, void *data)
  1290. {
  1291. int ret;
  1292. struct cgroup_sb_opts *opts = data;
  1293. /* If we don't have a new root, we can't set up a new sb */
  1294. if (!opts->new_root)
  1295. return -EINVAL;
  1296. BUG_ON(!opts->subsys_mask && !opts->none);
  1297. ret = set_anon_super(sb, NULL);
  1298. if (ret)
  1299. return ret;
  1300. sb->s_fs_info = opts->new_root;
  1301. opts->new_root->sb = sb;
  1302. sb->s_blocksize = PAGE_CACHE_SIZE;
  1303. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1304. sb->s_magic = CGROUP_SUPER_MAGIC;
  1305. sb->s_op = &cgroup_ops;
  1306. return 0;
  1307. }
  1308. static int cgroup_get_rootdir(struct super_block *sb)
  1309. {
  1310. static const struct dentry_operations cgroup_dops = {
  1311. .d_iput = cgroup_diput,
  1312. .d_delete = cgroup_delete,
  1313. };
  1314. struct inode *inode =
  1315. cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
  1316. if (!inode)
  1317. return -ENOMEM;
  1318. inode->i_fop = &simple_dir_operations;
  1319. inode->i_op = &cgroup_dir_inode_operations;
  1320. /* directories start off with i_nlink == 2 (for "." entry) */
  1321. inc_nlink(inode);
  1322. sb->s_root = d_make_root(inode);
  1323. if (!sb->s_root)
  1324. return -ENOMEM;
  1325. /* for everything else we want ->d_op set */
  1326. sb->s_d_op = &cgroup_dops;
  1327. return 0;
  1328. }
  1329. static struct dentry *cgroup_mount(struct file_system_type *fs_type,
  1330. int flags, const char *unused_dev_name,
  1331. void *data)
  1332. {
  1333. struct cgroup_sb_opts opts;
  1334. struct cgroupfs_root *root;
  1335. int ret = 0;
  1336. struct super_block *sb;
  1337. struct cgroupfs_root *new_root;
  1338. struct list_head tmp_links;
  1339. struct inode *inode;
  1340. const struct cred *cred;
  1341. /* First find the desired set of subsystems */
  1342. mutex_lock(&cgroup_mutex);
  1343. ret = parse_cgroupfs_options(data, &opts);
  1344. mutex_unlock(&cgroup_mutex);
  1345. if (ret)
  1346. goto out_err;
  1347. /*
  1348. * Allocate a new cgroup root. We may not need it if we're
  1349. * reusing an existing hierarchy.
  1350. */
  1351. new_root = cgroup_root_from_opts(&opts);
  1352. if (IS_ERR(new_root)) {
  1353. ret = PTR_ERR(new_root);
  1354. goto out_err;
  1355. }
  1356. opts.new_root = new_root;
  1357. /* Locate an existing or new sb for this hierarchy */
  1358. sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
  1359. if (IS_ERR(sb)) {
  1360. ret = PTR_ERR(sb);
  1361. cgroup_free_root(opts.new_root);
  1362. goto out_err;
  1363. }
  1364. root = sb->s_fs_info;
  1365. BUG_ON(!root);
  1366. if (root == opts.new_root) {
  1367. /* We used the new root structure, so this is a new hierarchy */
  1368. struct cgroup *root_cgrp = &root->top_cgroup;
  1369. struct cgroupfs_root *existing_root;
  1370. int i;
  1371. struct css_set *cset;
  1372. BUG_ON(sb->s_root != NULL);
  1373. ret = cgroup_get_rootdir(sb);
  1374. if (ret)
  1375. goto drop_new_super;
  1376. inode = sb->s_root->d_inode;
  1377. mutex_lock(&inode->i_mutex);
  1378. mutex_lock(&cgroup_mutex);
  1379. mutex_lock(&cgroup_root_mutex);
  1380. root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
  1381. 0, 1, GFP_KERNEL);
  1382. if (root_cgrp->id < 0)
  1383. goto unlock_drop;
  1384. /* Check for name clashes with existing mounts */
  1385. ret = -EBUSY;
  1386. if (strlen(root->name))
  1387. for_each_active_root(existing_root)
  1388. if (!strcmp(existing_root->name, root->name))
  1389. goto unlock_drop;
  1390. /*
  1391. * We're accessing css_set_count without locking
  1392. * css_set_lock here, but that's OK - it can only be
  1393. * increased by someone holding cgroup_lock, and
  1394. * that's us. The worst that can happen is that we
  1395. * have some link structures left over
  1396. */
  1397. ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
  1398. if (ret)
  1399. goto unlock_drop;
  1400. /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
  1401. ret = cgroup_init_root_id(root, 2, 0);
  1402. if (ret)
  1403. goto unlock_drop;
  1404. sb->s_root->d_fsdata = root_cgrp;
  1405. root_cgrp->dentry = sb->s_root;
  1406. /*
  1407. * We're inside get_sb() and will call lookup_one_len() to
  1408. * create the root files, which doesn't work if SELinux is
  1409. * in use. The following cred dancing somehow works around
  1410. * it. See 2ce9738ba ("cgroupfs: use init_cred when
  1411. * populating new cgroupfs mount") for more details.
  1412. */
  1413. cred = override_creds(&init_cred);
  1414. ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
  1415. if (ret)
  1416. goto rm_base_files;
  1417. ret = rebind_subsystems(root, root->subsys_mask, 0);
  1418. if (ret)
  1419. goto rm_base_files;
  1420. revert_creds(cred);
  1421. /*
  1422. * There must be no failure case after here, since rebinding
  1423. * takes care of subsystems' refcounts, which are explicitly
  1424. * dropped in the failure exit path.
  1425. */
  1426. list_add(&root->root_list, &cgroup_roots);
  1427. cgroup_root_count++;
  1428. /* Link the top cgroup in this hierarchy into all
  1429. * the css_set objects */
  1430. write_lock(&css_set_lock);
  1431. hash_for_each(css_set_table, i, cset, hlist)
  1432. link_css_set(&tmp_links, cset, root_cgrp);
  1433. write_unlock(&css_set_lock);
  1434. free_cgrp_cset_links(&tmp_links);
  1435. BUG_ON(!list_empty(&root_cgrp->children));
  1436. BUG_ON(root->number_of_cgroups != 1);
  1437. mutex_unlock(&cgroup_root_mutex);
  1438. mutex_unlock(&cgroup_mutex);
  1439. mutex_unlock(&inode->i_mutex);
  1440. } else {
  1441. /*
  1442. * We re-used an existing hierarchy - the new root (if
  1443. * any) is not needed
  1444. */
  1445. cgroup_free_root(opts.new_root);
  1446. if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
  1447. if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
  1448. pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
  1449. ret = -EINVAL;
  1450. goto drop_new_super;
  1451. } else {
  1452. pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
  1453. }
  1454. }
  1455. }
  1456. kfree(opts.release_agent);
  1457. kfree(opts.name);
  1458. return dget(sb->s_root);
  1459. rm_base_files:
  1460. free_cgrp_cset_links(&tmp_links);
  1461. cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
  1462. revert_creds(cred);
  1463. unlock_drop:
  1464. cgroup_exit_root_id(root);
  1465. mutex_unlock(&cgroup_root_mutex);
  1466. mutex_unlock(&cgroup_mutex);
  1467. mutex_unlock(&inode->i_mutex);
  1468. drop_new_super:
  1469. deactivate_locked_super(sb);
  1470. out_err:
  1471. kfree(opts.release_agent);
  1472. kfree(opts.name);
  1473. return ERR_PTR(ret);
  1474. }
  1475. static void cgroup_kill_sb(struct super_block *sb) {
  1476. struct cgroupfs_root *root = sb->s_fs_info;
  1477. struct cgroup *cgrp = &root->top_cgroup;
  1478. struct cgrp_cset_link *link, *tmp_link;
  1479. int ret;
  1480. BUG_ON(!root);
  1481. BUG_ON(root->number_of_cgroups != 1);
  1482. BUG_ON(!list_empty(&cgrp->children));
  1483. mutex_lock(&cgrp->dentry->d_inode->i_mutex);
  1484. mutex_lock(&cgroup_mutex);
  1485. mutex_lock(&cgroup_root_mutex);
  1486. /* Rebind all subsystems back to the default hierarchy */
  1487. if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
  1488. ret = rebind_subsystems(root, 0, root->subsys_mask);
  1489. /* Shouldn't be able to fail ... */
  1490. BUG_ON(ret);
  1491. }
  1492. /*
  1493. * Release all the links from cset_links to this hierarchy's
  1494. * root cgroup
  1495. */
  1496. write_lock(&css_set_lock);
  1497. list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
  1498. list_del(&link->cset_link);
  1499. list_del(&link->cgrp_link);
  1500. kfree(link);
  1501. }
  1502. write_unlock(&css_set_lock);
  1503. if (!list_empty(&root->root_list)) {
  1504. list_del(&root->root_list);
  1505. cgroup_root_count--;
  1506. }
  1507. cgroup_exit_root_id(root);
  1508. mutex_unlock(&cgroup_root_mutex);
  1509. mutex_unlock(&cgroup_mutex);
  1510. mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
  1511. simple_xattrs_free(&cgrp->xattrs);
  1512. kill_litter_super(sb);
  1513. cgroup_free_root(root);
  1514. }
  1515. static struct file_system_type cgroup_fs_type = {
  1516. .name = "cgroup",
  1517. .mount = cgroup_mount,
  1518. .kill_sb = cgroup_kill_sb,
  1519. };
  1520. static struct kobject *cgroup_kobj;
  1521. /**
  1522. * cgroup_path - generate the path of a cgroup
  1523. * @cgrp: the cgroup in question
  1524. * @buf: the buffer to write the path into
  1525. * @buflen: the length of the buffer
  1526. *
  1527. * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
  1528. *
  1529. * We can't generate cgroup path using dentry->d_name, as accessing
  1530. * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
  1531. * inode's i_mutex, while on the other hand cgroup_path() can be called
  1532. * with some irq-safe spinlocks held.
  1533. */
  1534. int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
  1535. {
  1536. int ret = -ENAMETOOLONG;
  1537. char *start;
  1538. if (!cgrp->parent) {
  1539. if (strlcpy(buf, "/", buflen) >= buflen)
  1540. return -ENAMETOOLONG;
  1541. return 0;
  1542. }
  1543. start = buf + buflen - 1;
  1544. *start = '\0';
  1545. rcu_read_lock();
  1546. do {
  1547. const char *name = cgroup_name(cgrp);
  1548. int len;
  1549. len = strlen(name);
  1550. if ((start -= len) < buf)
  1551. goto out;
  1552. memcpy(start, name, len);
  1553. if (--start < buf)
  1554. goto out;
  1555. *start = '/';
  1556. cgrp = cgrp->parent;
  1557. } while (cgrp->parent);
  1558. ret = 0;
  1559. memmove(buf, start, buf + buflen - start);
  1560. out:
  1561. rcu_read_unlock();
  1562. return ret;
  1563. }
  1564. EXPORT_SYMBOL_GPL(cgroup_path);
  1565. /**
  1566. * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
  1567. * @task: target task
  1568. * @buf: the buffer to write the path into
  1569. * @buflen: the length of the buffer
  1570. *
  1571. * Determine @task's cgroup on the first (the one with the lowest non-zero
  1572. * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
  1573. * function grabs cgroup_mutex and shouldn't be used inside locks used by
  1574. * cgroup controller callbacks.
  1575. *
  1576. * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
  1577. */
  1578. int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
  1579. {
  1580. struct cgroupfs_root *root;
  1581. struct cgroup *cgrp;
  1582. int hierarchy_id = 1, ret = 0;
  1583. if (buflen < 2)
  1584. return -ENAMETOOLONG;
  1585. mutex_lock(&cgroup_mutex);
  1586. root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
  1587. if (root) {
  1588. cgrp = task_cgroup_from_root(task, root);
  1589. ret = cgroup_path(cgrp, buf, buflen);
  1590. } else {
  1591. /* if no hierarchy exists, everyone is in "/" */
  1592. memcpy(buf, "/", 2);
  1593. }
  1594. mutex_unlock(&cgroup_mutex);
  1595. return ret;
  1596. }
  1597. EXPORT_SYMBOL_GPL(task_cgroup_path);
  1598. /*
  1599. * Control Group taskset
  1600. */
  1601. struct task_and_cgroup {
  1602. struct task_struct *task;
  1603. struct cgroup *cgrp;
  1604. struct css_set *cset;
  1605. };
  1606. struct cgroup_taskset {
  1607. struct task_and_cgroup single;
  1608. struct flex_array *tc_array;
  1609. int tc_array_len;
  1610. int idx;
  1611. struct cgroup *cur_cgrp;
  1612. };
  1613. /**
  1614. * cgroup_taskset_first - reset taskset and return the first task
  1615. * @tset: taskset of interest
  1616. *
  1617. * @tset iteration is initialized and the first task is returned.
  1618. */
  1619. struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
  1620. {
  1621. if (tset->tc_array) {
  1622. tset->idx = 0;
  1623. return cgroup_taskset_next(tset);
  1624. } else {
  1625. tset->cur_cgrp = tset->single.cgrp;
  1626. return tset->single.task;
  1627. }
  1628. }
  1629. EXPORT_SYMBOL_GPL(cgroup_taskset_first);
  1630. /**
  1631. * cgroup_taskset_next - iterate to the next task in taskset
  1632. * @tset: taskset of interest
  1633. *
  1634. * Return the next task in @tset. Iteration must have been initialized
  1635. * with cgroup_taskset_first().
  1636. */
  1637. struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
  1638. {
  1639. struct task_and_cgroup *tc;
  1640. if (!tset->tc_array || tset->idx >= tset->tc_array_len)
  1641. return NULL;
  1642. tc = flex_array_get(tset->tc_array, tset->idx++);
  1643. tset->cur_cgrp = tc->cgrp;
  1644. return tc->task;
  1645. }
  1646. EXPORT_SYMBOL_GPL(cgroup_taskset_next);
  1647. /**
  1648. * cgroup_taskset_cur_css - return the matching css for the current task
  1649. * @tset: taskset of interest
  1650. * @subsys_id: the ID of the target subsystem
  1651. *
  1652. * Return the css for the current (last returned) task of @tset for
  1653. * subsystem specified by @subsys_id. This function must be preceded by
  1654. * either cgroup_taskset_first() or cgroup_taskset_next().
  1655. */
  1656. struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
  1657. int subsys_id)
  1658. {
  1659. return cgroup_css(tset->cur_cgrp, subsys_id);
  1660. }
  1661. EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
  1662. /**
  1663. * cgroup_taskset_size - return the number of tasks in taskset
  1664. * @tset: taskset of interest
  1665. */
  1666. int cgroup_taskset_size(struct cgroup_taskset *tset)
  1667. {
  1668. return tset->tc_array ? tset->tc_array_len : 1;
  1669. }
  1670. EXPORT_SYMBOL_GPL(cgroup_taskset_size);
  1671. /*
  1672. * cgroup_task_migrate - move a task from one cgroup to another.
  1673. *
  1674. * Must be called with cgroup_mutex and threadgroup locked.
  1675. */
  1676. static void cgroup_task_migrate(struct cgroup *old_cgrp,
  1677. struct task_struct *tsk,
  1678. struct css_set *new_cset)
  1679. {
  1680. struct css_set *old_cset;
  1681. /*
  1682. * We are synchronized through threadgroup_lock() against PF_EXITING
  1683. * setting such that we can't race against cgroup_exit() changing the
  1684. * css_set to init_css_set and dropping the old one.
  1685. */
  1686. WARN_ON_ONCE(tsk->flags & PF_EXITING);
  1687. old_cset = task_css_set(tsk);
  1688. task_lock(tsk);
  1689. rcu_assign_pointer(tsk->cgroups, new_cset);
  1690. task_unlock(tsk);
  1691. /* Update the css_set linked lists if we're using them */
  1692. write_lock(&css_set_lock);
  1693. if (!list_empty(&tsk->cg_list))
  1694. list_move(&tsk->cg_list, &new_cset->tasks);
  1695. write_unlock(&css_set_lock);
  1696. /*
  1697. * We just gained a reference on old_cset by taking it from the
  1698. * task. As trading it for new_cset is protected by cgroup_mutex,
  1699. * we're safe to drop it here; it will be freed under RCU.
  1700. */
  1701. set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
  1702. put_css_set(old_cset);
  1703. }
  1704. /**
  1705. * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
  1706. * @cgrp: the cgroup to attach to
  1707. * @tsk: the task or the leader of the threadgroup to be attached
  1708. * @threadgroup: attach the whole threadgroup?
  1709. *
  1710. * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
  1711. * task_lock of @tsk or each thread in the threadgroup individually in turn.
  1712. */
  1713. static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
  1714. bool threadgroup)
  1715. {
  1716. int retval, i, group_size;
  1717. struct cgroup_subsys *ss, *failed_ss = NULL;
  1718. struct cgroupfs_root *root = cgrp->root;
  1719. /* threadgroup list cursor and array */
  1720. struct task_struct *leader = tsk;
  1721. struct task_and_cgroup *tc;
  1722. struct flex_array *group;
  1723. struct cgroup_taskset tset = { };
  1724. /*
  1725. * step 0: in order to do expensive, possibly blocking operations for
  1726. * every thread, we cannot iterate the thread group list, since it needs
  1727. * rcu or tasklist locked. instead, build an array of all threads in the
  1728. * group - group_rwsem prevents new threads from appearing, and if
  1729. * threads exit, this will just be an over-estimate.
  1730. */
  1731. if (threadgroup)
  1732. group_size = get_nr_threads(tsk);
  1733. else
  1734. group_size = 1;
  1735. /* flex_array supports very large thread-groups better than kmalloc. */
  1736. group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
  1737. if (!group)
  1738. return -ENOMEM;
  1739. /* pre-allocate to guarantee space while iterating in rcu read-side. */
  1740. retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
  1741. if (retval)
  1742. goto out_free_group_list;
  1743. i = 0;
  1744. /*
  1745. * Prevent freeing of tasks while we take a snapshot. Tasks that are
  1746. * already PF_EXITING could be freed from underneath us unless we
  1747. * take an rcu_read_lock.
  1748. */
  1749. rcu_read_lock();
  1750. do {
  1751. struct task_and_cgroup ent;
  1752. /* @tsk either already exited or can't exit until the end */
  1753. if (tsk->flags & PF_EXITING)
  1754. continue;
  1755. /* as per above, nr_threads may decrease, but not increase. */
  1756. BUG_ON(i >= group_size);
  1757. ent.task = tsk;
  1758. ent.cgrp = task_cgroup_from_root(tsk, root);
  1759. /* nothing to do if this task is already in the cgroup */
  1760. if (ent.cgrp == cgrp)
  1761. continue;
  1762. /*
  1763. * saying GFP_ATOMIC has no effect here because we did prealloc
  1764. * earlier, but it's good form to communicate our expectations.
  1765. */
  1766. retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
  1767. BUG_ON(retval != 0);
  1768. i++;
  1769. if (!threadgroup)
  1770. break;
  1771. } while_each_thread(leader, tsk);
  1772. rcu_read_unlock();
  1773. /* remember the number of threads in the array for later. */
  1774. group_size = i;
  1775. tset.tc_array = group;
  1776. tset.tc_array_len = group_size;
  1777. /* methods shouldn't be called if no task is actually migrating */
  1778. retval = 0;
  1779. if (!group_size)
  1780. goto out_free_group_list;
  1781. /*
  1782. * step 1: check that we can legitimately attach to the cgroup.
  1783. */
  1784. for_each_root_subsys(root, ss) {
  1785. struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
  1786. if (ss->can_attach) {
  1787. retval = ss->can_attach(css, &tset);
  1788. if (retval) {
  1789. failed_ss = ss;
  1790. goto out_cancel_attach;
  1791. }
  1792. }
  1793. }
  1794. /*
  1795. * step 2: make sure css_sets exist for all threads to be migrated.
  1796. * we use find_css_set, which allocates a new one if necessary.
  1797. */
  1798. for (i = 0; i < group_size; i++) {
  1799. struct css_set *old_cset;
  1800. tc = flex_array_get(group, i);
  1801. old_cset = task_css_set(tc->task);
  1802. tc->cset = find_css_set(old_cset, cgrp);
  1803. if (!tc->cset) {
  1804. retval = -ENOMEM;
  1805. goto out_put_css_set_refs;
  1806. }
  1807. }
  1808. /*
  1809. * step 3: now that we're guaranteed success wrt the css_sets,
  1810. * proceed to move all tasks to the new cgroup. There are no
  1811. * failure cases after here, so this is the commit point.
  1812. */
  1813. for (i = 0; i < group_size; i++) {
  1814. tc = flex_array_get(group, i);
  1815. cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
  1816. }
  1817. /* nothing is sensitive to fork() after this point. */
  1818. /*
  1819. * step 4: do subsystem attach callbacks.
  1820. */
  1821. for_each_root_subsys(root, ss) {
  1822. struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
  1823. if (ss->attach)
  1824. ss->attach(css, &tset);
  1825. }
  1826. /*
  1827. * step 5: success! and cleanup
  1828. */
  1829. retval = 0;
  1830. out_put_css_set_refs:
  1831. if (retval) {
  1832. for (i = 0; i < group_size; i++) {
  1833. tc = flex_array_get(group, i);
  1834. if (!tc->cset)
  1835. break;
  1836. put_css_set(tc->cset);
  1837. }
  1838. }
  1839. out_cancel_attach:
  1840. if (retval) {
  1841. for_each_root_subsys(root, ss) {
  1842. struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
  1843. if (ss == failed_ss)
  1844. break;
  1845. if (ss->cancel_attach)
  1846. ss->cancel_attach(css, &tset);
  1847. }
  1848. }
  1849. out_free_group_list:
  1850. flex_array_free(group);
  1851. return retval;
  1852. }
  1853. /*
  1854. * Find the task_struct of the task to attach by vpid and pass it along to the
  1855. * function to attach either it or all tasks in its threadgroup. Will lock
  1856. * cgroup_mutex and threadgroup; may take task_lock of task.
  1857. */
  1858. static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
  1859. {
  1860. struct task_struct *tsk;
  1861. const struct cred *cred = current_cred(), *tcred;
  1862. int ret;
  1863. if (!cgroup_lock_live_group(cgrp))
  1864. return -ENODEV;
  1865. retry_find_task:
  1866. rcu_read_lock();
  1867. if (pid) {
  1868. tsk = find_task_by_vpid(pid);
  1869. if (!tsk) {
  1870. rcu_read_unlock();
  1871. ret= -ESRCH;
  1872. goto out_unlock_cgroup;
  1873. }
  1874. /*
  1875. * even if we're attaching all tasks in the thread group, we
  1876. * only need to check permissions on one of them.
  1877. */
  1878. tcred = __task_cred(tsk);
  1879. if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
  1880. !uid_eq(cred->euid, tcred->uid) &&
  1881. !uid_eq(cred->euid, tcred->suid)) {
  1882. rcu_read_unlock();
  1883. ret = -EACCES;
  1884. goto out_unlock_cgroup;
  1885. }
  1886. } else
  1887. tsk = current;
  1888. if (threadgroup)
  1889. tsk = tsk->group_leader;
  1890. /*
  1891. * Workqueue threads may acquire PF_NO_SETAFFINITY and become
  1892. * trapped in a cpuset, or RT worker may be born in a cgroup
  1893. * with no rt_runtime allocated. Just say no.
  1894. */
  1895. if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
  1896. ret = -EINVAL;
  1897. rcu_read_unlock();
  1898. goto out_unlock_cgroup;
  1899. }
  1900. get_task_struct(tsk);
  1901. rcu_read_unlock();
  1902. threadgroup_lock(tsk);
  1903. if (threadgroup) {
  1904. if (!thread_group_leader(tsk)) {
  1905. /*
  1906. * a race with de_thread from another thread's exec()
  1907. * may strip us of our leadership, if this happens,
  1908. * there is no choice but to throw this task away and
  1909. * try again; this is
  1910. * "double-double-toil-and-trouble-check locking".
  1911. */
  1912. threadgroup_unlock(tsk);
  1913. put_task_struct(tsk);
  1914. goto retry_find_task;
  1915. }
  1916. }
  1917. ret = cgroup_attach_task(cgrp, tsk, threadgroup);
  1918. threadgroup_unlock(tsk);
  1919. put_task_struct(tsk);
  1920. out_unlock_cgroup:
  1921. mutex_unlock(&cgroup_mutex);
  1922. return ret;
  1923. }
  1924. /**
  1925. * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
  1926. * @from: attach to all cgroups of a given task
  1927. * @tsk: the task to be attached
  1928. */
  1929. int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
  1930. {
  1931. struct cgroupfs_root *root;
  1932. int retval = 0;
  1933. mutex_lock(&cgroup_mutex);
  1934. for_each_active_root(root) {
  1935. struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
  1936. retval = cgroup_attach_task(from_cgrp, tsk, false);
  1937. if (retval)
  1938. break;
  1939. }
  1940. mutex_unlock(&cgroup_mutex);
  1941. return retval;
  1942. }
  1943. EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
  1944. static int cgroup_tasks_write(struct cgroup_subsys_state *css,
  1945. struct cftype *cft, u64 pid)
  1946. {
  1947. return attach_task_by_pid(css->cgroup, pid, false);
  1948. }
  1949. static int cgroup_procs_write(struct cgroup_subsys_state *css,
  1950. struct cftype *cft, u64 tgid)
  1951. {
  1952. return attach_task_by_pid(css->cgroup, tgid, true);
  1953. }
  1954. static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
  1955. struct cftype *cft, const char *buffer)
  1956. {
  1957. BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
  1958. if (strlen(buffer) >= PATH_MAX)
  1959. return -EINVAL;
  1960. if (!cgroup_lock_live_group(css->cgroup))
  1961. return -ENODEV;
  1962. mutex_lock(&cgroup_root_mutex);
  1963. strcpy(css->cgroup->root->release_agent_path, buffer);
  1964. mutex_unlock(&cgroup_root_mutex);
  1965. mutex_unlock(&cgroup_mutex);
  1966. return 0;
  1967. }
  1968. static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
  1969. struct cftype *cft, struct seq_file *seq)
  1970. {
  1971. struct cgroup *cgrp = css->cgroup;
  1972. if (!cgroup_lock_live_group(cgrp))
  1973. return -ENODEV;
  1974. seq_puts(seq, cgrp->root->release_agent_path);
  1975. seq_putc(seq, '\n');
  1976. mutex_unlock(&cgroup_mutex);
  1977. return 0;
  1978. }
  1979. static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
  1980. struct cftype *cft, struct seq_file *seq)
  1981. {
  1982. seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
  1983. return 0;
  1984. }
  1985. /* A buffer size big enough for numbers or short strings */
  1986. #define CGROUP_LOCAL_BUFFER_SIZE 64
  1987. static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
  1988. struct cftype *cft, struct file *file,
  1989. const char __user *userbuf, size_t nbytes,
  1990. loff_t *unused_ppos)
  1991. {
  1992. char buffer[CGROUP_LOCAL_BUFFER_SIZE];
  1993. int retval = 0;
  1994. char *end;
  1995. if (!nbytes)
  1996. return -EINVAL;
  1997. if (nbytes >= sizeof(buffer))
  1998. return -E2BIG;
  1999. if (copy_from_user(buffer, userbuf, nbytes))
  2000. return -EFAULT;
  2001. buffer[nbytes] = 0; /* nul-terminate */
  2002. if (cft->write_u64) {
  2003. u64 val = simple_strtoull(strstrip(buffer), &end, 0);
  2004. if (*end)
  2005. return -EINVAL;
  2006. retval = cft->write_u64(css, cft, val);
  2007. } else {
  2008. s64 val = simple_strtoll(strstrip(buffer), &end, 0);
  2009. if (*end)
  2010. return -EINVAL;
  2011. retval = cft->write_s64(css, cft, val);
  2012. }
  2013. if (!retval)
  2014. retval = nbytes;
  2015. return retval;
  2016. }
  2017. static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
  2018. struct cftype *cft, struct file *file,
  2019. const char __user *userbuf, size_t nbytes,
  2020. loff_t *unused_ppos)
  2021. {
  2022. char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
  2023. int retval = 0;
  2024. size_t max_bytes = cft->max_write_len;
  2025. char *buffer = local_buffer;
  2026. if (!max_bytes)
  2027. max_bytes = sizeof(local_buffer) - 1;
  2028. if (nbytes >= max_bytes)
  2029. return -E2BIG;
  2030. /* Allocate a dynamic buffer if we need one */
  2031. if (nbytes >= sizeof(local_buffer)) {
  2032. buffer = kmalloc(nbytes + 1, GFP_KERNEL);
  2033. if (buffer == NULL)
  2034. return -ENOMEM;
  2035. }
  2036. if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
  2037. retval = -EFAULT;
  2038. goto out;
  2039. }
  2040. buffer[nbytes] = 0; /* nul-terminate */
  2041. retval = cft->write_string(css, cft, strstrip(buffer));
  2042. if (!retval)
  2043. retval = nbytes;
  2044. out:
  2045. if (buffer != local_buffer)
  2046. kfree(buffer);
  2047. return retval;
  2048. }
  2049. static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
  2050. size_t nbytes, loff_t *ppos)
  2051. {
  2052. struct cfent *cfe = __d_cfe(file->f_dentry);
  2053. struct cftype *cft = __d_cft(file->f_dentry);
  2054. struct cgroup_subsys_state *css = cfe->css;
  2055. if (cft->write)
  2056. return cft->write(css, cft, file, buf, nbytes, ppos);
  2057. if (cft->write_u64 || cft->write_s64)
  2058. return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
  2059. if (cft->write_string)
  2060. return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
  2061. if (cft->trigger) {
  2062. int ret = cft->trigger(css, (unsigned int)cft->private);
  2063. return ret ? ret : nbytes;
  2064. }
  2065. return -EINVAL;
  2066. }
  2067. static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
  2068. struct cftype *cft, struct file *file,
  2069. char __user *buf, size_t nbytes, loff_t *ppos)
  2070. {
  2071. char tmp[CGROUP_LOCAL_BUFFER_SIZE];
  2072. u64 val = cft->read_u64(css, cft);
  2073. int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
  2074. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  2075. }
  2076. static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
  2077. struct cftype *cft, struct file *file,
  2078. char __user *buf, size_t nbytes, loff_t *ppos)
  2079. {
  2080. char tmp[CGROUP_LOCAL_BUFFER_SIZE];
  2081. s64 val = cft->read_s64(css, cft);
  2082. int len = sprintf(tmp, "%lld\n", (long long) val);
  2083. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  2084. }
  2085. static ssize_t cgroup_file_read(struct file *file, char __user *buf,
  2086. size_t nbytes, loff_t *ppos)
  2087. {
  2088. struct cfent *cfe = __d_cfe(file->f_dentry);
  2089. struct cftype *cft = __d_cft(file->f_dentry);
  2090. struct cgroup_subsys_state *css = cfe->css;
  2091. if (cft->read)
  2092. return cft->read(css, cft, file, buf, nbytes, ppos);
  2093. if (cft->read_u64)
  2094. return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
  2095. if (cft->read_s64)
  2096. return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
  2097. return -EINVAL;
  2098. }
  2099. /*
  2100. * seqfile ops/methods for returning structured data. Currently just
  2101. * supports string->u64 maps, but can be extended in future.
  2102. */
  2103. static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
  2104. {
  2105. struct seq_file *sf = cb->state;
  2106. return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
  2107. }
  2108. static int cgroup_seqfile_show(struct seq_file *m, void *arg)
  2109. {
  2110. struct cfent *cfe = m->private;
  2111. struct cftype *cft = cfe->type;
  2112. struct cgroup_subsys_state *css = cfe->css;
  2113. if (cft->read_map) {
  2114. struct cgroup_map_cb cb = {
  2115. .fill = cgroup_map_add,
  2116. .state = m,
  2117. };
  2118. return cft->read_map(css, cft, &cb);
  2119. }
  2120. return cft->read_seq_string(css, cft, m);
  2121. }
  2122. static const struct file_operations cgroup_seqfile_operations = {
  2123. .read = seq_read,
  2124. .write = cgroup_file_write,
  2125. .llseek = seq_lseek,
  2126. .release = single_release,
  2127. };
  2128. static int cgroup_file_open(struct inode *inode, struct file *file)
  2129. {
  2130. struct cfent *cfe = __d_cfe(file->f_dentry);
  2131. struct cftype *cft = __d_cft(file->f_dentry);
  2132. struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
  2133. struct cgroup_subsys_state *css;
  2134. int err;
  2135. err = generic_file_open(inode, file);
  2136. if (err)
  2137. return err;
  2138. /*
  2139. * If the file belongs to a subsystem, pin the css. Will be
  2140. * unpinned either on open failure or release. This ensures that
  2141. * @css stays alive for all file operations.
  2142. */
  2143. rcu_read_lock();
  2144. if (cft->ss) {
  2145. css = cgroup_css(cgrp, cft->ss->subsys_id);
  2146. if (!css_tryget(css))
  2147. css = NULL;
  2148. } else {
  2149. css = &cgrp->dummy_css;
  2150. }
  2151. rcu_read_unlock();
  2152. if (!css)
  2153. return -ENODEV;
  2154. /*
  2155. * @cfe->css is used by read/write/close to determine the
  2156. * associated css. @file->private_data would be a better place but
  2157. * that's already used by seqfile. Multiple accessors may use it
  2158. * simultaneously which is okay as the association never changes.
  2159. */
  2160. WARN_ON_ONCE(cfe->css && cfe->css != css);
  2161. cfe->css = css;
  2162. if (cft->read_map || cft->read_seq_string) {
  2163. file->f_op = &cgroup_seqfile_operations;
  2164. err = single_open(file, cgroup_seqfile_show, cfe);
  2165. } else if (cft->open) {
  2166. err = cft->open(inode, file);
  2167. }
  2168. if (css->ss && err)
  2169. css_put(css);
  2170. return err;
  2171. }
  2172. static int cgroup_file_release(struct inode *inode, struct file *file)
  2173. {
  2174. struct cfent *cfe = __d_cfe(file->f_dentry);
  2175. struct cftype *cft = __d_cft(file->f_dentry);
  2176. struct cgroup_subsys_state *css = cfe->css;
  2177. int ret = 0;
  2178. if (cft->release)
  2179. ret = cft->release(inode, file);
  2180. if (css->ss)
  2181. css_put(css);
  2182. return ret;
  2183. }
  2184. /*
  2185. * cgroup_rename - Only allow simple rename of directories in place.
  2186. */
  2187. static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
  2188. struct inode *new_dir, struct dentry *new_dentry)
  2189. {
  2190. int ret;
  2191. struct cgroup_name *name, *old_name;
  2192. struct cgroup *cgrp;
  2193. /*
  2194. * It's convinient to use parent dir's i_mutex to protected
  2195. * cgrp->name.
  2196. */
  2197. lockdep_assert_held(&old_dir->i_mutex);
  2198. if (!S_ISDIR(old_dentry->d_inode->i_mode))
  2199. return -ENOTDIR;
  2200. if (new_dentry->d_inode)
  2201. return -EEXIST;
  2202. if (old_dir != new_dir)
  2203. return -EIO;
  2204. cgrp = __d_cgrp(old_dentry);
  2205. /*
  2206. * This isn't a proper migration and its usefulness is very
  2207. * limited. Disallow if sane_behavior.
  2208. */
  2209. if (cgroup_sane_behavior(cgrp))
  2210. return -EPERM;
  2211. name = cgroup_alloc_name(new_dentry);
  2212. if (!name)
  2213. return -ENOMEM;
  2214. ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  2215. if (ret) {
  2216. kfree(name);
  2217. return ret;
  2218. }
  2219. old_name = rcu_dereference_protected(cgrp->name, true);
  2220. rcu_assign_pointer(cgrp->name, name);
  2221. kfree_rcu(old_name, rcu_head);
  2222. return 0;
  2223. }
  2224. static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
  2225. {
  2226. if (S_ISDIR(dentry->d_inode->i_mode))
  2227. return &__d_cgrp(dentry)->xattrs;
  2228. else
  2229. return &__d_cfe(dentry)->xattrs;
  2230. }
  2231. static inline int xattr_enabled(struct dentry *dentry)
  2232. {
  2233. struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
  2234. return root->flags & CGRP_ROOT_XATTR;
  2235. }
  2236. static bool is_valid_xattr(const char *name)
  2237. {
  2238. if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
  2239. !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
  2240. return true;
  2241. return false;
  2242. }
  2243. static int cgroup_setxattr(struct dentry *dentry, const char *name,
  2244. const void *val, size_t size, int flags)
  2245. {
  2246. if (!xattr_enabled(dentry))
  2247. return -EOPNOTSUPP;
  2248. if (!is_valid_xattr(name))
  2249. return -EINVAL;
  2250. return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
  2251. }
  2252. static int cgroup_removexattr(struct dentry *dentry, const char *name)
  2253. {
  2254. if (!xattr_enabled(dentry))
  2255. return -EOPNOTSUPP;
  2256. if (!is_valid_xattr(name))
  2257. return -EINVAL;
  2258. return simple_xattr_remove(__d_xattrs(dentry), name);
  2259. }
  2260. static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
  2261. void *buf, size_t size)
  2262. {
  2263. if (!xattr_enabled(dentry))
  2264. return -EOPNOTSUPP;
  2265. if (!is_valid_xattr(name))
  2266. return -EINVAL;
  2267. return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
  2268. }
  2269. static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
  2270. {
  2271. if (!xattr_enabled(dentry))
  2272. return -EOPNOTSUPP;
  2273. return simple_xattr_list(__d_xattrs(dentry), buf, size);
  2274. }
  2275. static const struct file_operations cgroup_file_operations = {
  2276. .read = cgroup_file_read,
  2277. .write = cgroup_file_write,
  2278. .llseek = generic_file_llseek,
  2279. .open = cgroup_file_open,
  2280. .release = cgroup_file_release,
  2281. };
  2282. static const struct inode_operations cgroup_file_inode_operations = {
  2283. .setxattr = cgroup_setxattr,
  2284. .getxattr = cgroup_getxattr,
  2285. .listxattr = cgroup_listxattr,
  2286. .removexattr = cgroup_removexattr,
  2287. };
  2288. static const struct inode_operations cgroup_dir_inode_operations = {
  2289. .lookup = cgroup_lookup,
  2290. .mkdir = cgroup_mkdir,
  2291. .rmdir = cgroup_rmdir,
  2292. .rename = cgroup_rename,
  2293. .setxattr = cgroup_setxattr,
  2294. .getxattr = cgroup_getxattr,
  2295. .listxattr = cgroup_listxattr,
  2296. .removexattr = cgroup_removexattr,
  2297. };
  2298. static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  2299. {
  2300. if (dentry->d_name.len > NAME_MAX)
  2301. return ERR_PTR(-ENAMETOOLONG);
  2302. d_add(dentry, NULL);
  2303. return NULL;
  2304. }
  2305. /*
  2306. * Check if a file is a control file
  2307. */
  2308. static inline struct cftype *__file_cft(struct file *file)
  2309. {
  2310. if (file_inode(file)->i_fop != &cgroup_file_operations)
  2311. return ERR_PTR(-EINVAL);
  2312. return __d_cft(file->f_dentry);
  2313. }
  2314. static int cgroup_create_file(struct dentry *dentry, umode_t mode,
  2315. struct super_block *sb)
  2316. {
  2317. struct inode *inode;
  2318. if (!dentry)
  2319. return -ENOENT;
  2320. if (dentry->d_inode)
  2321. return -EEXIST;
  2322. inode = cgroup_new_inode(mode, sb);
  2323. if (!inode)
  2324. return -ENOMEM;
  2325. if (S_ISDIR(mode)) {
  2326. inode->i_op = &cgroup_dir_inode_operations;
  2327. inode->i_fop = &simple_dir_operations;
  2328. /* start off with i_nlink == 2 (for "." entry) */
  2329. inc_nlink(inode);
  2330. inc_nlink(dentry->d_parent->d_inode);
  2331. /*
  2332. * Control reaches here with cgroup_mutex held.
  2333. * @inode->i_mutex should nest outside cgroup_mutex but we
  2334. * want to populate it immediately without releasing
  2335. * cgroup_mutex. As @inode isn't visible to anyone else
  2336. * yet, trylock will always succeed without affecting
  2337. * lockdep checks.
  2338. */
  2339. WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
  2340. } else if (S_ISREG(mode)) {
  2341. inode->i_size = 0;
  2342. inode->i_fop = &cgroup_file_operations;
  2343. inode->i_op = &cgroup_file_inode_operations;
  2344. }
  2345. d_instantiate(dentry, inode);
  2346. dget(dentry); /* Extra count - pin the dentry in core */
  2347. return 0;
  2348. }
  2349. /**
  2350. * cgroup_file_mode - deduce file mode of a control file
  2351. * @cft: the control file in question
  2352. *
  2353. * returns cft->mode if ->mode is not 0
  2354. * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
  2355. * returns S_IRUGO if it has only a read handler
  2356. * returns S_IWUSR if it has only a write hander
  2357. */
  2358. static umode_t cgroup_file_mode(const struct cftype *cft)
  2359. {
  2360. umode_t mode = 0;
  2361. if (cft->mode)
  2362. return cft->mode;
  2363. if (cft->read || cft->read_u64 || cft->read_s64 ||
  2364. cft->read_map || cft->read_seq_string)
  2365. mode |= S_IRUGO;
  2366. if (cft->write || cft->write_u64 || cft->write_s64 ||
  2367. cft->write_string || cft->trigger)
  2368. mode |= S_IWUSR;
  2369. return mode;
  2370. }
  2371. static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
  2372. {
  2373. struct dentry *dir = cgrp->dentry;
  2374. struct cgroup *parent = __d_cgrp(dir);
  2375. struct dentry *dentry;
  2376. struct cfent *cfe;
  2377. int error;
  2378. umode_t mode;
  2379. char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
  2380. if (cft->ss && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
  2381. strcpy(name, cft->ss->name);
  2382. strcat(name, ".");
  2383. }
  2384. strcat(name, cft->name);
  2385. BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
  2386. cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
  2387. if (!cfe)
  2388. return -ENOMEM;
  2389. dentry = lookup_one_len(name, dir, strlen(name));
  2390. if (IS_ERR(dentry)) {
  2391. error = PTR_ERR(dentry);
  2392. goto out;
  2393. }
  2394. cfe->type = (void *)cft;
  2395. cfe->dentry = dentry;
  2396. dentry->d_fsdata = cfe;
  2397. simple_xattrs_init(&cfe->xattrs);
  2398. mode = cgroup_file_mode(cft);
  2399. error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
  2400. if (!error) {
  2401. list_add_tail(&cfe->node, &parent->files);
  2402. cfe = NULL;
  2403. }
  2404. dput(dentry);
  2405. out:
  2406. kfree(cfe);
  2407. return error;
  2408. }
  2409. /**
  2410. * cgroup_addrm_files - add or remove files to a cgroup directory
  2411. * @cgrp: the target cgroup
  2412. * @cfts: array of cftypes to be added
  2413. * @is_add: whether to add or remove
  2414. *
  2415. * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
  2416. * For removals, this function never fails. If addition fails, this
  2417. * function doesn't remove files already added. The caller is responsible
  2418. * for cleaning up.
  2419. */
  2420. static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
  2421. bool is_add)
  2422. {
  2423. struct cftype *cft;
  2424. int ret;
  2425. lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
  2426. lockdep_assert_held(&cgroup_mutex);
  2427. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  2428. /* does cft->flags tell us to skip this file on @cgrp? */
  2429. if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
  2430. continue;
  2431. if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
  2432. continue;
  2433. if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
  2434. continue;
  2435. if (is_add) {
  2436. ret = cgroup_add_file(cgrp, cft);
  2437. if (ret) {
  2438. pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
  2439. cft->name, ret);
  2440. return ret;
  2441. }
  2442. } else {
  2443. cgroup_rm_file(cgrp, cft);
  2444. }
  2445. }
  2446. return 0;
  2447. }
  2448. static void cgroup_cfts_prepare(void)
  2449. __acquires(&cgroup_mutex)
  2450. {
  2451. /*
  2452. * Thanks to the entanglement with vfs inode locking, we can't walk
  2453. * the existing cgroups under cgroup_mutex and create files.
  2454. * Instead, we use css_for_each_descendant_pre() and drop RCU read
  2455. * lock before calling cgroup_addrm_files().
  2456. */
  2457. mutex_lock(&cgroup_mutex);
  2458. }
  2459. static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
  2460. __releases(&cgroup_mutex)
  2461. {
  2462. LIST_HEAD(pending);
  2463. struct cgroup_subsys *ss = cfts[0].ss;
  2464. struct cgroup *root = &ss->root->top_cgroup;
  2465. struct super_block *sb = ss->root->sb;
  2466. struct dentry *prev = NULL;
  2467. struct inode *inode;
  2468. struct cgroup_subsys_state *css;
  2469. u64 update_before;
  2470. int ret = 0;
  2471. /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
  2472. if (!cfts || ss->root == &cgroup_dummy_root ||
  2473. !atomic_inc_not_zero(&sb->s_active)) {
  2474. mutex_unlock(&cgroup_mutex);
  2475. return 0;
  2476. }
  2477. /*
  2478. * All cgroups which are created after we drop cgroup_mutex will
  2479. * have the updated set of files, so we only need to update the
  2480. * cgroups created before the current @cgroup_serial_nr_next.
  2481. */
  2482. update_before = cgroup_serial_nr_next;
  2483. mutex_unlock(&cgroup_mutex);
  2484. /* add/rm files for all cgroups created before */
  2485. rcu_read_lock();
  2486. css_for_each_descendant_pre(css, cgroup_css(root, ss->subsys_id)) {
  2487. struct cgroup *cgrp = css->cgroup;
  2488. if (cgroup_is_dead(cgrp))
  2489. continue;
  2490. inode = cgrp->dentry->d_inode;
  2491. dget(cgrp->dentry);
  2492. rcu_read_unlock();
  2493. dput(prev);
  2494. prev = cgrp->dentry;
  2495. mutex_lock(&inode->i_mutex);
  2496. mutex_lock(&cgroup_mutex);
  2497. if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
  2498. ret = cgroup_addrm_files(cgrp, cfts, is_add);
  2499. mutex_unlock(&cgroup_mutex);
  2500. mutex_unlock(&inode->i_mutex);
  2501. rcu_read_lock();
  2502. if (ret)
  2503. break;
  2504. }
  2505. rcu_read_unlock();
  2506. dput(prev);
  2507. deactivate_super(sb);
  2508. return ret;
  2509. }
  2510. /**
  2511. * cgroup_add_cftypes - add an array of cftypes to a subsystem
  2512. * @ss: target cgroup subsystem
  2513. * @cfts: zero-length name terminated array of cftypes
  2514. *
  2515. * Register @cfts to @ss. Files described by @cfts are created for all
  2516. * existing cgroups to which @ss is attached and all future cgroups will
  2517. * have them too. This function can be called anytime whether @ss is
  2518. * attached or not.
  2519. *
  2520. * Returns 0 on successful registration, -errno on failure. Note that this
  2521. * function currently returns 0 as long as @cfts registration is successful
  2522. * even if some file creation attempts on existing cgroups fail.
  2523. */
  2524. int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  2525. {
  2526. struct cftype_set *set;
  2527. struct cftype *cft;
  2528. int ret;
  2529. set = kzalloc(sizeof(*set), GFP_KERNEL);
  2530. if (!set)
  2531. return -ENOMEM;
  2532. for (cft = cfts; cft->name[0] != '\0'; cft++)
  2533. cft->ss = ss;
  2534. cgroup_cfts_prepare();
  2535. set->cfts = cfts;
  2536. list_add_tail(&set->node, &ss->cftsets);
  2537. ret = cgroup_cfts_commit(cfts, true);
  2538. if (ret)
  2539. cgroup_rm_cftypes(cfts);
  2540. return ret;
  2541. }
  2542. EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
  2543. /**
  2544. * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
  2545. * @cfts: zero-length name terminated array of cftypes
  2546. *
  2547. * Unregister @cfts. Files described by @cfts are removed from all
  2548. * existing cgroups and all future cgroups won't have them either. This
  2549. * function can be called anytime whether @cfts' subsys is attached or not.
  2550. *
  2551. * Returns 0 on successful unregistration, -ENOENT if @cfts is not
  2552. * registered.
  2553. */
  2554. int cgroup_rm_cftypes(struct cftype *cfts)
  2555. {
  2556. struct cftype_set *set;
  2557. if (!cfts || !cfts[0].ss)
  2558. return -ENOENT;
  2559. cgroup_cfts_prepare();
  2560. list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
  2561. if (set->cfts == cfts) {
  2562. list_del(&set->node);
  2563. kfree(set);
  2564. cgroup_cfts_commit(cfts, false);
  2565. return 0;
  2566. }
  2567. }
  2568. cgroup_cfts_commit(NULL, false);
  2569. return -ENOENT;
  2570. }
  2571. /**
  2572. * cgroup_task_count - count the number of tasks in a cgroup.
  2573. * @cgrp: the cgroup in question
  2574. *
  2575. * Return the number of tasks in the cgroup.
  2576. */
  2577. int cgroup_task_count(const struct cgroup *cgrp)
  2578. {
  2579. int count = 0;
  2580. struct cgrp_cset_link *link;
  2581. read_lock(&css_set_lock);
  2582. list_for_each_entry(link, &cgrp->cset_links, cset_link)
  2583. count += atomic_read(&link->cset->refcount);
  2584. read_unlock(&css_set_lock);
  2585. return count;
  2586. }
  2587. /*
  2588. * To reduce the fork() overhead for systems that are not actually using
  2589. * their cgroups capability, we don't maintain the lists running through
  2590. * each css_set to its tasks until we see the list actually used - in other
  2591. * words after the first call to css_task_iter_start().
  2592. */
  2593. static void cgroup_enable_task_cg_lists(void)
  2594. {
  2595. struct task_struct *p, *g;
  2596. write_lock(&css_set_lock);
  2597. use_task_css_set_links = 1;
  2598. /*
  2599. * We need tasklist_lock because RCU is not safe against
  2600. * while_each_thread(). Besides, a forking task that has passed
  2601. * cgroup_post_fork() without seeing use_task_css_set_links = 1
  2602. * is not guaranteed to have its child immediately visible in the
  2603. * tasklist if we walk through it with RCU.
  2604. */
  2605. read_lock(&tasklist_lock);
  2606. do_each_thread(g, p) {
  2607. task_lock(p);
  2608. /*
  2609. * We should check if the process is exiting, otherwise
  2610. * it will race with cgroup_exit() in that the list
  2611. * entry won't be deleted though the process has exited.
  2612. */
  2613. if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
  2614. list_add(&p->cg_list, &task_css_set(p)->tasks);
  2615. task_unlock(p);
  2616. } while_each_thread(g, p);
  2617. read_unlock(&tasklist_lock);
  2618. write_unlock(&css_set_lock);
  2619. }
  2620. /**
  2621. * css_next_child - find the next child of a given css
  2622. * @pos_css: the current position (%NULL to initiate traversal)
  2623. * @parent_css: css whose children to walk
  2624. *
  2625. * This function returns the next child of @parent_css and should be called
  2626. * under RCU read lock. The only requirement is that @parent_css and
  2627. * @pos_css are accessible. The next sibling is guaranteed to be returned
  2628. * regardless of their states.
  2629. */
  2630. struct cgroup_subsys_state *
  2631. css_next_child(struct cgroup_subsys_state *pos_css,
  2632. struct cgroup_subsys_state *parent_css)
  2633. {
  2634. struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
  2635. struct cgroup *cgrp = parent_css->cgroup;
  2636. struct cgroup *next;
  2637. WARN_ON_ONCE(!rcu_read_lock_held());
  2638. /*
  2639. * @pos could already have been removed. Once a cgroup is removed,
  2640. * its ->sibling.next is no longer updated when its next sibling
  2641. * changes. As CGRP_DEAD assertion is serialized and happens
  2642. * before the cgroup is taken off the ->sibling list, if we see it
  2643. * unasserted, it's guaranteed that the next sibling hasn't
  2644. * finished its grace period even if it's already removed, and thus
  2645. * safe to dereference from this RCU critical section. If
  2646. * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
  2647. * to be visible as %true here.
  2648. *
  2649. * If @pos is dead, its next pointer can't be dereferenced;
  2650. * however, as each cgroup is given a monotonically increasing
  2651. * unique serial number and always appended to the sibling list,
  2652. * the next one can be found by walking the parent's children until
  2653. * we see a cgroup with higher serial number than @pos's. While
  2654. * this path can be slower, it's taken only when either the current
  2655. * cgroup is removed or iteration and removal race.
  2656. */
  2657. if (!pos) {
  2658. next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
  2659. } else if (likely(!cgroup_is_dead(pos))) {
  2660. next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
  2661. } else {
  2662. list_for_each_entry_rcu(next, &cgrp->children, sibling)
  2663. if (next->serial_nr > pos->serial_nr)
  2664. break;
  2665. }
  2666. if (&next->sibling == &cgrp->children)
  2667. return NULL;
  2668. if (parent_css->ss)
  2669. return cgroup_css(next, parent_css->ss->subsys_id);
  2670. else
  2671. return &next->dummy_css;
  2672. }
  2673. EXPORT_SYMBOL_GPL(css_next_child);
  2674. /**
  2675. * css_next_descendant_pre - find the next descendant for pre-order walk
  2676. * @pos: the current position (%NULL to initiate traversal)
  2677. * @root: css whose descendants to walk
  2678. *
  2679. * To be used by css_for_each_descendant_pre(). Find the next descendant
  2680. * to visit for pre-order traversal of @root's descendants. @root is
  2681. * included in the iteration and the first node to be visited.
  2682. *
  2683. * While this function requires RCU read locking, it doesn't require the
  2684. * whole traversal to be contained in a single RCU critical section. This
  2685. * function will return the correct next descendant as long as both @pos
  2686. * and @root are accessible and @pos is a descendant of @root.
  2687. */
  2688. struct cgroup_subsys_state *
  2689. css_next_descendant_pre(struct cgroup_subsys_state *pos,
  2690. struct cgroup_subsys_state *root)
  2691. {
  2692. struct cgroup_subsys_state *next;
  2693. WARN_ON_ONCE(!rcu_read_lock_held());
  2694. /* if first iteration, visit @root */
  2695. if (!pos)
  2696. return root;
  2697. /* visit the first child if exists */
  2698. next = css_next_child(NULL, pos);
  2699. if (next)
  2700. return next;
  2701. /* no child, visit my or the closest ancestor's next sibling */
  2702. while (pos != root) {
  2703. next = css_next_child(pos, css_parent(pos));
  2704. if (next)
  2705. return next;
  2706. pos = css_parent(pos);
  2707. }
  2708. return NULL;
  2709. }
  2710. EXPORT_SYMBOL_GPL(css_next_descendant_pre);
  2711. /**
  2712. * css_rightmost_descendant - return the rightmost descendant of a css
  2713. * @pos: css of interest
  2714. *
  2715. * Return the rightmost descendant of @pos. If there's no descendant, @pos
  2716. * is returned. This can be used during pre-order traversal to skip
  2717. * subtree of @pos.
  2718. *
  2719. * While this function requires RCU read locking, it doesn't require the
  2720. * whole traversal to be contained in a single RCU critical section. This
  2721. * function will return the correct rightmost descendant as long as @pos is
  2722. * accessible.
  2723. */
  2724. struct cgroup_subsys_state *
  2725. css_rightmost_descendant(struct cgroup_subsys_state *pos)
  2726. {
  2727. struct cgroup_subsys_state *last, *tmp;
  2728. WARN_ON_ONCE(!rcu_read_lock_held());
  2729. do {
  2730. last = pos;
  2731. /* ->prev isn't RCU safe, walk ->next till the end */
  2732. pos = NULL;
  2733. css_for_each_child(tmp, last)
  2734. pos = tmp;
  2735. } while (pos);
  2736. return last;
  2737. }
  2738. EXPORT_SYMBOL_GPL(css_rightmost_descendant);
  2739. static struct cgroup_subsys_state *
  2740. css_leftmost_descendant(struct cgroup_subsys_state *pos)
  2741. {
  2742. struct cgroup_subsys_state *last;
  2743. do {
  2744. last = pos;
  2745. pos = css_next_child(NULL, pos);
  2746. } while (pos);
  2747. return last;
  2748. }
  2749. /**
  2750. * css_next_descendant_post - find the next descendant for post-order walk
  2751. * @pos: the current position (%NULL to initiate traversal)
  2752. * @root: css whose descendants to walk
  2753. *
  2754. * To be used by css_for_each_descendant_post(). Find the next descendant
  2755. * to visit for post-order traversal of @root's descendants. @root is
  2756. * included in the iteration and the last node to be visited.
  2757. *
  2758. * While this function requires RCU read locking, it doesn't require the
  2759. * whole traversal to be contained in a single RCU critical section. This
  2760. * function will return the correct next descendant as long as both @pos
  2761. * and @cgroup are accessible and @pos is a descendant of @cgroup.
  2762. */
  2763. struct cgroup_subsys_state *
  2764. css_next_descendant_post(struct cgroup_subsys_state *pos,
  2765. struct cgroup_subsys_state *root)
  2766. {
  2767. struct cgroup_subsys_state *next;
  2768. WARN_ON_ONCE(!rcu_read_lock_held());
  2769. /* if first iteration, visit the leftmost descendant */
  2770. if (!pos) {
  2771. next = css_leftmost_descendant(root);
  2772. return next != root ? next : NULL;
  2773. }
  2774. /* if we visited @root, we're done */
  2775. if (pos == root)
  2776. return NULL;
  2777. /* if there's an unvisited sibling, visit its leftmost descendant */
  2778. next = css_next_child(pos, css_parent(pos));
  2779. if (next)
  2780. return css_leftmost_descendant(next);
  2781. /* no sibling left, visit parent */
  2782. return css_parent(pos);
  2783. }
  2784. EXPORT_SYMBOL_GPL(css_next_descendant_post);
  2785. /**
  2786. * css_advance_task_iter - advance a task itererator to the next css_set
  2787. * @it: the iterator to advance
  2788. *
  2789. * Advance @it to the next css_set to walk.
  2790. */
  2791. static void css_advance_task_iter(struct css_task_iter *it)
  2792. {
  2793. struct list_head *l = it->cset_link;
  2794. struct cgrp_cset_link *link;
  2795. struct css_set *cset;
  2796. /* Advance to the next non-empty css_set */
  2797. do {
  2798. l = l->next;
  2799. if (l == &it->origin_css->cgroup->cset_links) {
  2800. it->cset_link = NULL;
  2801. return;
  2802. }
  2803. link = list_entry(l, struct cgrp_cset_link, cset_link);
  2804. cset = link->cset;
  2805. } while (list_empty(&cset->tasks));
  2806. it->cset_link = l;
  2807. it->task = cset->tasks.next;
  2808. }
  2809. /**
  2810. * css_task_iter_start - initiate task iteration
  2811. * @css: the css to walk tasks of
  2812. * @it: the task iterator to use
  2813. *
  2814. * Initiate iteration through the tasks of @css. The caller can call
  2815. * css_task_iter_next() to walk through the tasks until the function
  2816. * returns NULL. On completion of iteration, css_task_iter_end() must be
  2817. * called.
  2818. *
  2819. * Note that this function acquires a lock which is released when the
  2820. * iteration finishes. The caller can't sleep while iteration is in
  2821. * progress.
  2822. */
  2823. void css_task_iter_start(struct cgroup_subsys_state *css,
  2824. struct css_task_iter *it)
  2825. __acquires(css_set_lock)
  2826. {
  2827. /*
  2828. * The first time anyone tries to iterate across a css, we need to
  2829. * enable the list linking each css_set to its tasks, and fix up
  2830. * all existing tasks.
  2831. */
  2832. if (!use_task_css_set_links)
  2833. cgroup_enable_task_cg_lists();
  2834. read_lock(&css_set_lock);
  2835. it->origin_css = css;
  2836. it->cset_link = &css->cgroup->cset_links;
  2837. css_advance_task_iter(it);
  2838. }
  2839. /**
  2840. * css_task_iter_next - return the next task for the iterator
  2841. * @it: the task iterator being iterated
  2842. *
  2843. * The "next" function for task iteration. @it should have been
  2844. * initialized via css_task_iter_start(). Returns NULL when the iteration
  2845. * reaches the end.
  2846. */
  2847. struct task_struct *css_task_iter_next(struct css_task_iter *it)
  2848. {
  2849. struct task_struct *res;
  2850. struct list_head *l = it->task;
  2851. struct cgrp_cset_link *link;
  2852. /* If the iterator cg is NULL, we have no tasks */
  2853. if (!it->cset_link)
  2854. return NULL;
  2855. res = list_entry(l, struct task_struct, cg_list);
  2856. /* Advance iterator to find next entry */
  2857. l = l->next;
  2858. link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
  2859. if (l == &link->cset->tasks) {
  2860. /*
  2861. * We reached the end of this task list - move on to the
  2862. * next cgrp_cset_link.
  2863. */
  2864. css_advance_task_iter(it);
  2865. } else {
  2866. it->task = l;
  2867. }
  2868. return res;
  2869. }
  2870. /**
  2871. * css_task_iter_end - finish task iteration
  2872. * @it: the task iterator to finish
  2873. *
  2874. * Finish task iteration started by css_task_iter_start().
  2875. */
  2876. void css_task_iter_end(struct css_task_iter *it)
  2877. __releases(css_set_lock)
  2878. {
  2879. read_unlock(&css_set_lock);
  2880. }
  2881. static inline int started_after_time(struct task_struct *t1,
  2882. struct timespec *time,
  2883. struct task_struct *t2)
  2884. {
  2885. int start_diff = timespec_compare(&t1->start_time, time);
  2886. if (start_diff > 0) {
  2887. return 1;
  2888. } else if (start_diff < 0) {
  2889. return 0;
  2890. } else {
  2891. /*
  2892. * Arbitrarily, if two processes started at the same
  2893. * time, we'll say that the lower pointer value
  2894. * started first. Note that t2 may have exited by now
  2895. * so this may not be a valid pointer any longer, but
  2896. * that's fine - it still serves to distinguish
  2897. * between two tasks started (effectively) simultaneously.
  2898. */
  2899. return t1 > t2;
  2900. }
  2901. }
  2902. /*
  2903. * This function is a callback from heap_insert() and is used to order
  2904. * the heap.
  2905. * In this case we order the heap in descending task start time.
  2906. */
  2907. static inline int started_after(void *p1, void *p2)
  2908. {
  2909. struct task_struct *t1 = p1;
  2910. struct task_struct *t2 = p2;
  2911. return started_after_time(t1, &t2->start_time, t2);
  2912. }
  2913. /**
  2914. * css_scan_tasks - iterate though all the tasks in a css
  2915. * @css: the css to iterate tasks of
  2916. * @test: optional test callback
  2917. * @process: process callback
  2918. * @data: data passed to @test and @process
  2919. * @heap: optional pre-allocated heap used for task iteration
  2920. *
  2921. * Iterate through all the tasks in @css, calling @test for each, and if it
  2922. * returns %true, call @process for it also.
  2923. *
  2924. * @test may be NULL, meaning always true (select all tasks), which
  2925. * effectively duplicates css_task_iter_{start,next,end}() but does not
  2926. * lock css_set_lock for the call to @process.
  2927. *
  2928. * It is guaranteed that @process will act on every task that is a member
  2929. * of @css for the duration of this call. This function may or may not
  2930. * call @process for tasks that exit or move to a different css during the
  2931. * call, or are forked or move into the css during the call.
  2932. *
  2933. * Note that @test may be called with locks held, and may in some
  2934. * situations be called multiple times for the same task, so it should be
  2935. * cheap.
  2936. *
  2937. * If @heap is non-NULL, a heap has been pre-allocated and will be used for
  2938. * heap operations (and its "gt" member will be overwritten), else a
  2939. * temporary heap will be used (allocation of which may cause this function
  2940. * to fail).
  2941. */
  2942. int css_scan_tasks(struct cgroup_subsys_state *css,
  2943. bool (*test)(struct task_struct *, void *),
  2944. void (*process)(struct task_struct *, void *),
  2945. void *data, struct ptr_heap *heap)
  2946. {
  2947. int retval, i;
  2948. struct css_task_iter it;
  2949. struct task_struct *p, *dropped;
  2950. /* Never dereference latest_task, since it's not refcounted */
  2951. struct task_struct *latest_task = NULL;
  2952. struct ptr_heap tmp_heap;
  2953. struct timespec latest_time = { 0, 0 };
  2954. if (heap) {
  2955. /* The caller supplied our heap and pre-allocated its memory */
  2956. heap->gt = &started_after;
  2957. } else {
  2958. /* We need to allocate our own heap memory */
  2959. heap = &tmp_heap;
  2960. retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
  2961. if (retval)
  2962. /* cannot allocate the heap */
  2963. return retval;
  2964. }
  2965. again:
  2966. /*
  2967. * Scan tasks in the css, using the @test callback to determine
  2968. * which are of interest, and invoking @process callback on the
  2969. * ones which need an update. Since we don't want to hold any
  2970. * locks during the task updates, gather tasks to be processed in a
  2971. * heap structure. The heap is sorted by descending task start
  2972. * time. If the statically-sized heap fills up, we overflow tasks
  2973. * that started later, and in future iterations only consider tasks
  2974. * that started after the latest task in the previous pass. This
  2975. * guarantees forward progress and that we don't miss any tasks.
  2976. */
  2977. heap->size = 0;
  2978. css_task_iter_start(css, &it);
  2979. while ((p = css_task_iter_next(&it))) {
  2980. /*
  2981. * Only affect tasks that qualify per the caller's callback,
  2982. * if he provided one
  2983. */
  2984. if (test && !test(p, data))
  2985. continue;
  2986. /*
  2987. * Only process tasks that started after the last task
  2988. * we processed
  2989. */
  2990. if (!started_after_time(p, &latest_time, latest_task))
  2991. continue;
  2992. dropped = heap_insert(heap, p);
  2993. if (dropped == NULL) {
  2994. /*
  2995. * The new task was inserted; the heap wasn't
  2996. * previously full
  2997. */
  2998. get_task_struct(p);
  2999. } else if (dropped != p) {
  3000. /*
  3001. * The new task was inserted, and pushed out a
  3002. * different task
  3003. */
  3004. get_task_struct(p);
  3005. put_task_struct(dropped);
  3006. }
  3007. /*
  3008. * Else the new task was newer than anything already in
  3009. * the heap and wasn't inserted
  3010. */
  3011. }
  3012. css_task_iter_end(&it);
  3013. if (heap->size) {
  3014. for (i = 0; i < heap->size; i++) {
  3015. struct task_struct *q = heap->ptrs[i];
  3016. if (i == 0) {
  3017. latest_time = q->start_time;
  3018. latest_task = q;
  3019. }
  3020. /* Process the task per the caller's callback */
  3021. process(q, data);
  3022. put_task_struct(q);
  3023. }
  3024. /*
  3025. * If we had to process any tasks at all, scan again
  3026. * in case some of them were in the middle of forking
  3027. * children that didn't get processed.
  3028. * Not the most efficient way to do it, but it avoids
  3029. * having to take callback_mutex in the fork path
  3030. */
  3031. goto again;
  3032. }
  3033. if (heap == &tmp_heap)
  3034. heap_free(&tmp_heap);
  3035. return 0;
  3036. }
  3037. static void cgroup_transfer_one_task(struct task_struct *task, void *data)
  3038. {
  3039. struct cgroup *new_cgroup = data;
  3040. mutex_lock(&cgroup_mutex);
  3041. cgroup_attach_task(new_cgroup, task, false);
  3042. mutex_unlock(&cgroup_mutex);
  3043. }
  3044. /**
  3045. * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
  3046. * @to: cgroup to which the tasks will be moved
  3047. * @from: cgroup in which the tasks currently reside
  3048. */
  3049. int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
  3050. {
  3051. return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
  3052. to, NULL);
  3053. }
  3054. /*
  3055. * Stuff for reading the 'tasks'/'procs' files.
  3056. *
  3057. * Reading this file can return large amounts of data if a cgroup has
  3058. * *lots* of attached tasks. So it may need several calls to read(),
  3059. * but we cannot guarantee that the information we produce is correct
  3060. * unless we produce it entirely atomically.
  3061. *
  3062. */
  3063. /* which pidlist file are we talking about? */
  3064. enum cgroup_filetype {
  3065. CGROUP_FILE_PROCS,
  3066. CGROUP_FILE_TASKS,
  3067. };
  3068. /*
  3069. * A pidlist is a list of pids that virtually represents the contents of one
  3070. * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
  3071. * a pair (one each for procs, tasks) for each pid namespace that's relevant
  3072. * to the cgroup.
  3073. */
  3074. struct cgroup_pidlist {
  3075. /*
  3076. * used to find which pidlist is wanted. doesn't change as long as
  3077. * this particular list stays in the list.
  3078. */
  3079. struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
  3080. /* array of xids */
  3081. pid_t *list;
  3082. /* how many elements the above list has */
  3083. int length;
  3084. /* how many files are using the current array */
  3085. int use_count;
  3086. /* each of these stored in a list by its cgroup */
  3087. struct list_head links;
  3088. /* pointer to the cgroup we belong to, for list removal purposes */
  3089. struct cgroup *owner;
  3090. /* protects the other fields */
  3091. struct rw_semaphore rwsem;
  3092. };
  3093. /*
  3094. * The following two functions "fix" the issue where there are more pids
  3095. * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
  3096. * TODO: replace with a kernel-wide solution to this problem
  3097. */
  3098. #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
  3099. static void *pidlist_allocate(int count)
  3100. {
  3101. if (PIDLIST_TOO_LARGE(count))
  3102. return vmalloc(count * sizeof(pid_t));
  3103. else
  3104. return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
  3105. }
  3106. static void pidlist_free(void *p)
  3107. {
  3108. if (is_vmalloc_addr(p))
  3109. vfree(p);
  3110. else
  3111. kfree(p);
  3112. }
  3113. /*
  3114. * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
  3115. * Returns the number of unique elements.
  3116. */
  3117. static int pidlist_uniq(pid_t *list, int length)
  3118. {
  3119. int src, dest = 1;
  3120. /*
  3121. * we presume the 0th element is unique, so i starts at 1. trivial
  3122. * edge cases first; no work needs to be done for either
  3123. */
  3124. if (length == 0 || length == 1)
  3125. return length;
  3126. /* src and dest walk down the list; dest counts unique elements */
  3127. for (src = 1; src < length; src++) {
  3128. /* find next unique element */
  3129. while (list[src] == list[src-1]) {
  3130. src++;
  3131. if (src == length)
  3132. goto after;
  3133. }
  3134. /* dest always points to where the next unique element goes */
  3135. list[dest] = list[src];
  3136. dest++;
  3137. }
  3138. after:
  3139. return dest;
  3140. }
  3141. static int cmppid(const void *a, const void *b)
  3142. {
  3143. return *(pid_t *)a - *(pid_t *)b;
  3144. }
  3145. /*
  3146. * find the appropriate pidlist for our purpose (given procs vs tasks)
  3147. * returns with the lock on that pidlist already held, and takes care
  3148. * of the use count, or returns NULL with no locks held if we're out of
  3149. * memory.
  3150. */
  3151. static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
  3152. enum cgroup_filetype type)
  3153. {
  3154. struct cgroup_pidlist *l;
  3155. /* don't need task_nsproxy() if we're looking at ourself */
  3156. struct pid_namespace *ns = task_active_pid_ns(current);
  3157. /*
  3158. * We can't drop the pidlist_mutex before taking the l->rwsem in case
  3159. * the last ref-holder is trying to remove l from the list at the same
  3160. * time. Holding the pidlist_mutex precludes somebody taking whichever
  3161. * list we find out from under us - compare release_pid_array().
  3162. */
  3163. mutex_lock(&cgrp->pidlist_mutex);
  3164. list_for_each_entry(l, &cgrp->pidlists, links) {
  3165. if (l->key.type == type && l->key.ns == ns) {
  3166. /* make sure l doesn't vanish out from under us */
  3167. down_write(&l->rwsem);
  3168. mutex_unlock(&cgrp->pidlist_mutex);
  3169. return l;
  3170. }
  3171. }
  3172. /* entry not found; create a new one */
  3173. l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
  3174. if (!l) {
  3175. mutex_unlock(&cgrp->pidlist_mutex);
  3176. return l;
  3177. }
  3178. init_rwsem(&l->rwsem);
  3179. down_write(&l->rwsem);
  3180. l->key.type = type;
  3181. l->key.ns = get_pid_ns(ns);
  3182. l->owner = cgrp;
  3183. list_add(&l->links, &cgrp->pidlists);
  3184. mutex_unlock(&cgrp->pidlist_mutex);
  3185. return l;
  3186. }
  3187. /*
  3188. * Load a cgroup's pidarray with either procs' tgids or tasks' pids
  3189. */
  3190. static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
  3191. struct cgroup_pidlist **lp)
  3192. {
  3193. pid_t *array;
  3194. int length;
  3195. int pid, n = 0; /* used for populating the array */
  3196. struct css_task_iter it;
  3197. struct task_struct *tsk;
  3198. struct cgroup_pidlist *l;
  3199. /*
  3200. * If cgroup gets more users after we read count, we won't have
  3201. * enough space - tough. This race is indistinguishable to the
  3202. * caller from the case that the additional cgroup users didn't
  3203. * show up until sometime later on.
  3204. */
  3205. length = cgroup_task_count(cgrp);
  3206. array = pidlist_allocate(length);
  3207. if (!array)
  3208. return -ENOMEM;
  3209. /* now, populate the array */
  3210. css_task_iter_start(&cgrp->dummy_css, &it);
  3211. while ((tsk = css_task_iter_next(&it))) {
  3212. if (unlikely(n == length))
  3213. break;
  3214. /* get tgid or pid for procs or tasks file respectively */
  3215. if (type == CGROUP_FILE_PROCS)
  3216. pid = task_tgid_vnr(tsk);
  3217. else
  3218. pid = task_pid_vnr(tsk);
  3219. if (pid > 0) /* make sure to only use valid results */
  3220. array[n++] = pid;
  3221. }
  3222. css_task_iter_end(&it);
  3223. length = n;
  3224. /* now sort & (if procs) strip out duplicates */
  3225. sort(array, length, sizeof(pid_t), cmppid, NULL);
  3226. if (type == CGROUP_FILE_PROCS)
  3227. length = pidlist_uniq(array, length);
  3228. l = cgroup_pidlist_find(cgrp, type);
  3229. if (!l) {
  3230. pidlist_free(array);
  3231. return -ENOMEM;
  3232. }
  3233. /* store array, freeing old if necessary - lock already held */
  3234. pidlist_free(l->list);
  3235. l->list = array;
  3236. l->length = length;
  3237. l->use_count++;
  3238. up_write(&l->rwsem);
  3239. *lp = l;
  3240. return 0;
  3241. }
  3242. /**
  3243. * cgroupstats_build - build and fill cgroupstats
  3244. * @stats: cgroupstats to fill information into
  3245. * @dentry: A dentry entry belonging to the cgroup for which stats have
  3246. * been requested.
  3247. *
  3248. * Build and fill cgroupstats so that taskstats can export it to user
  3249. * space.
  3250. */
  3251. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
  3252. {
  3253. int ret = -EINVAL;
  3254. struct cgroup *cgrp;
  3255. struct css_task_iter it;
  3256. struct task_struct *tsk;
  3257. /*
  3258. * Validate dentry by checking the superblock operations,
  3259. * and make sure it's a directory.
  3260. */
  3261. if (dentry->d_sb->s_op != &cgroup_ops ||
  3262. !S_ISDIR(dentry->d_inode->i_mode))
  3263. goto err;
  3264. ret = 0;
  3265. cgrp = dentry->d_fsdata;
  3266. css_task_iter_start(&cgrp->dummy_css, &it);
  3267. while ((tsk = css_task_iter_next(&it))) {
  3268. switch (tsk->state) {
  3269. case TASK_RUNNING:
  3270. stats->nr_running++;
  3271. break;
  3272. case TASK_INTERRUPTIBLE:
  3273. stats->nr_sleeping++;
  3274. break;
  3275. case TASK_UNINTERRUPTIBLE:
  3276. stats->nr_uninterruptible++;
  3277. break;
  3278. case TASK_STOPPED:
  3279. stats->nr_stopped++;
  3280. break;
  3281. default:
  3282. if (delayacct_is_task_waiting_on_io(tsk))
  3283. stats->nr_io_wait++;
  3284. break;
  3285. }
  3286. }
  3287. css_task_iter_end(&it);
  3288. err:
  3289. return ret;
  3290. }
  3291. /*
  3292. * seq_file methods for the tasks/procs files. The seq_file position is the
  3293. * next pid to display; the seq_file iterator is a pointer to the pid
  3294. * in the cgroup->l->list array.
  3295. */
  3296. static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
  3297. {
  3298. /*
  3299. * Initially we receive a position value that corresponds to
  3300. * one more than the last pid shown (or 0 on the first call or
  3301. * after a seek to the start). Use a binary-search to find the
  3302. * next pid to display, if any
  3303. */
  3304. struct cgroup_pidlist *l = s->private;
  3305. int index = 0, pid = *pos;
  3306. int *iter;
  3307. down_read(&l->rwsem);
  3308. if (pid) {
  3309. int end = l->length;
  3310. while (index < end) {
  3311. int mid = (index + end) / 2;
  3312. if (l->list[mid] == pid) {
  3313. index = mid;
  3314. break;
  3315. } else if (l->list[mid] <= pid)
  3316. index = mid + 1;
  3317. else
  3318. end = mid;
  3319. }
  3320. }
  3321. /* If we're off the end of the array, we're done */
  3322. if (index >= l->length)
  3323. return NULL;
  3324. /* Update the abstract position to be the actual pid that we found */
  3325. iter = l->list + index;
  3326. *pos = *iter;
  3327. return iter;
  3328. }
  3329. static void cgroup_pidlist_stop(struct seq_file *s, void *v)
  3330. {
  3331. struct cgroup_pidlist *l = s->private;
  3332. up_read(&l->rwsem);
  3333. }
  3334. static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
  3335. {
  3336. struct cgroup_pidlist *l = s->private;
  3337. pid_t *p = v;
  3338. pid_t *end = l->list + l->length;
  3339. /*
  3340. * Advance to the next pid in the array. If this goes off the
  3341. * end, we're done
  3342. */
  3343. p++;
  3344. if (p >= end) {
  3345. return NULL;
  3346. } else {
  3347. *pos = *p;
  3348. return p;
  3349. }
  3350. }
  3351. static int cgroup_pidlist_show(struct seq_file *s, void *v)
  3352. {
  3353. return seq_printf(s, "%d\n", *(int *)v);
  3354. }
  3355. /*
  3356. * seq_operations functions for iterating on pidlists through seq_file -
  3357. * independent of whether it's tasks or procs
  3358. */
  3359. static const struct seq_operations cgroup_pidlist_seq_operations = {
  3360. .start = cgroup_pidlist_start,
  3361. .stop = cgroup_pidlist_stop,
  3362. .next = cgroup_pidlist_next,
  3363. .show = cgroup_pidlist_show,
  3364. };
  3365. static void cgroup_release_pid_array(struct cgroup_pidlist *l)
  3366. {
  3367. /*
  3368. * the case where we're the last user of this particular pidlist will
  3369. * have us remove it from the cgroup's list, which entails taking the
  3370. * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
  3371. * pidlist_mutex, we have to take pidlist_mutex first.
  3372. */
  3373. mutex_lock(&l->owner->pidlist_mutex);
  3374. down_write(&l->rwsem);
  3375. BUG_ON(!l->use_count);
  3376. if (!--l->use_count) {
  3377. /* we're the last user if refcount is 0; remove and free */
  3378. list_del(&l->links);
  3379. mutex_unlock(&l->owner->pidlist_mutex);
  3380. pidlist_free(l->list);
  3381. put_pid_ns(l->key.ns);
  3382. up_write(&l->rwsem);
  3383. kfree(l);
  3384. return;
  3385. }
  3386. mutex_unlock(&l->owner->pidlist_mutex);
  3387. up_write(&l->rwsem);
  3388. }
  3389. static int cgroup_pidlist_release(struct inode *inode, struct file *file)
  3390. {
  3391. struct cgroup_pidlist *l;
  3392. if (!(file->f_mode & FMODE_READ))
  3393. return 0;
  3394. /*
  3395. * the seq_file will only be initialized if the file was opened for
  3396. * reading; hence we check if it's not null only in that case.
  3397. */
  3398. l = ((struct seq_file *)file->private_data)->private;
  3399. cgroup_release_pid_array(l);
  3400. return seq_release(inode, file);
  3401. }
  3402. static const struct file_operations cgroup_pidlist_operations = {
  3403. .read = seq_read,
  3404. .llseek = seq_lseek,
  3405. .write = cgroup_file_write,
  3406. .release = cgroup_pidlist_release,
  3407. };
  3408. /*
  3409. * The following functions handle opens on a file that displays a pidlist
  3410. * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
  3411. * in the cgroup.
  3412. */
  3413. /* helper function for the two below it */
  3414. static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
  3415. {
  3416. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  3417. struct cgroup_pidlist *l;
  3418. int retval;
  3419. /* Nothing to do for write-only files */
  3420. if (!(file->f_mode & FMODE_READ))
  3421. return 0;
  3422. /* have the array populated */
  3423. retval = pidlist_array_load(cgrp, type, &l);
  3424. if (retval)
  3425. return retval;
  3426. /* configure file information */
  3427. file->f_op = &cgroup_pidlist_operations;
  3428. retval = seq_open(file, &cgroup_pidlist_seq_operations);
  3429. if (retval) {
  3430. cgroup_release_pid_array(l);
  3431. return retval;
  3432. }
  3433. ((struct seq_file *)file->private_data)->private = l;
  3434. return 0;
  3435. }
  3436. static int cgroup_tasks_open(struct inode *unused, struct file *file)
  3437. {
  3438. return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
  3439. }
  3440. static int cgroup_procs_open(struct inode *unused, struct file *file)
  3441. {
  3442. return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
  3443. }
  3444. static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
  3445. struct cftype *cft)
  3446. {
  3447. return notify_on_release(css->cgroup);
  3448. }
  3449. static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
  3450. struct cftype *cft, u64 val)
  3451. {
  3452. clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
  3453. if (val)
  3454. set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
  3455. else
  3456. clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
  3457. return 0;
  3458. }
  3459. /*
  3460. * When dput() is called asynchronously, if umount has been done and
  3461. * then deactivate_super() in cgroup_free_fn() kills the superblock,
  3462. * there's a small window that vfs will see the root dentry with non-zero
  3463. * refcnt and trigger BUG().
  3464. *
  3465. * That's why we hold a reference before dput() and drop it right after.
  3466. */
  3467. static void cgroup_dput(struct cgroup *cgrp)
  3468. {
  3469. struct super_block *sb = cgrp->root->sb;
  3470. atomic_inc(&sb->s_active);
  3471. dput(cgrp->dentry);
  3472. deactivate_super(sb);
  3473. }
  3474. /*
  3475. * Unregister event and free resources.
  3476. *
  3477. * Gets called from workqueue.
  3478. */
  3479. static void cgroup_event_remove(struct work_struct *work)
  3480. {
  3481. struct cgroup_event *event = container_of(work, struct cgroup_event,
  3482. remove);
  3483. struct cgroup_subsys_state *css = event->css;
  3484. struct cgroup *cgrp = css->cgroup;
  3485. remove_wait_queue(event->wqh, &event->wait);
  3486. event->cft->unregister_event(css, event->cft, event->eventfd);
  3487. /* Notify userspace the event is going away. */
  3488. eventfd_signal(event->eventfd, 1);
  3489. eventfd_ctx_put(event->eventfd);
  3490. kfree(event);
  3491. cgroup_dput(cgrp);
  3492. }
  3493. /*
  3494. * Gets called on POLLHUP on eventfd when user closes it.
  3495. *
  3496. * Called with wqh->lock held and interrupts disabled.
  3497. */
  3498. static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
  3499. int sync, void *key)
  3500. {
  3501. struct cgroup_event *event = container_of(wait,
  3502. struct cgroup_event, wait);
  3503. struct cgroup *cgrp = event->css->cgroup;
  3504. unsigned long flags = (unsigned long)key;
  3505. if (flags & POLLHUP) {
  3506. /*
  3507. * If the event has been detached at cgroup removal, we
  3508. * can simply return knowing the other side will cleanup
  3509. * for us.
  3510. *
  3511. * We can't race against event freeing since the other
  3512. * side will require wqh->lock via remove_wait_queue(),
  3513. * which we hold.
  3514. */
  3515. spin_lock(&cgrp->event_list_lock);
  3516. if (!list_empty(&event->list)) {
  3517. list_del_init(&event->list);
  3518. /*
  3519. * We are in atomic context, but cgroup_event_remove()
  3520. * may sleep, so we have to call it in workqueue.
  3521. */
  3522. schedule_work(&event->remove);
  3523. }
  3524. spin_unlock(&cgrp->event_list_lock);
  3525. }
  3526. return 0;
  3527. }
  3528. static void cgroup_event_ptable_queue_proc(struct file *file,
  3529. wait_queue_head_t *wqh, poll_table *pt)
  3530. {
  3531. struct cgroup_event *event = container_of(pt,
  3532. struct cgroup_event, pt);
  3533. event->wqh = wqh;
  3534. add_wait_queue(wqh, &event->wait);
  3535. }
  3536. /*
  3537. * Parse input and register new cgroup event handler.
  3538. *
  3539. * Input must be in format '<event_fd> <control_fd> <args>'.
  3540. * Interpretation of args is defined by control file implementation.
  3541. */
  3542. static int cgroup_write_event_control(struct cgroup_subsys_state *dummy_css,
  3543. struct cftype *cft, const char *buffer)
  3544. {
  3545. struct cgroup *cgrp = dummy_css->cgroup;
  3546. struct cgroup_event *event;
  3547. struct cgroup *cgrp_cfile;
  3548. unsigned int efd, cfd;
  3549. struct file *efile;
  3550. struct file *cfile;
  3551. char *endp;
  3552. int ret;
  3553. efd = simple_strtoul(buffer, &endp, 10);
  3554. if (*endp != ' ')
  3555. return -EINVAL;
  3556. buffer = endp + 1;
  3557. cfd = simple_strtoul(buffer, &endp, 10);
  3558. if ((*endp != ' ') && (*endp != '\0'))
  3559. return -EINVAL;
  3560. buffer = endp + 1;
  3561. event = kzalloc(sizeof(*event), GFP_KERNEL);
  3562. if (!event)
  3563. return -ENOMEM;
  3564. INIT_LIST_HEAD(&event->list);
  3565. init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
  3566. init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
  3567. INIT_WORK(&event->remove, cgroup_event_remove);
  3568. efile = eventfd_fget(efd);
  3569. if (IS_ERR(efile)) {
  3570. ret = PTR_ERR(efile);
  3571. goto out_kfree;
  3572. }
  3573. event->eventfd = eventfd_ctx_fileget(efile);
  3574. if (IS_ERR(event->eventfd)) {
  3575. ret = PTR_ERR(event->eventfd);
  3576. goto out_put_efile;
  3577. }
  3578. cfile = fget(cfd);
  3579. if (!cfile) {
  3580. ret = -EBADF;
  3581. goto out_put_eventfd;
  3582. }
  3583. /* the process need read permission on control file */
  3584. /* AV: shouldn't we check that it's been opened for read instead? */
  3585. ret = inode_permission(file_inode(cfile), MAY_READ);
  3586. if (ret < 0)
  3587. goto out_put_cfile;
  3588. event->cft = __file_cft(cfile);
  3589. if (IS_ERR(event->cft)) {
  3590. ret = PTR_ERR(event->cft);
  3591. goto out_put_cfile;
  3592. }
  3593. if (!event->cft->ss) {
  3594. ret = -EBADF;
  3595. goto out_put_cfile;
  3596. }
  3597. /* determine the css of @cfile and associate @event with it */
  3598. rcu_read_lock();
  3599. ret = -EINVAL;
  3600. event->css = cgroup_css(cgrp, event->cft->ss->subsys_id);
  3601. if (event->css)
  3602. ret = 0;
  3603. rcu_read_unlock();
  3604. if (ret)
  3605. goto out_put_cfile;
  3606. /*
  3607. * The file to be monitored must be in the same cgroup as
  3608. * cgroup.event_control is.
  3609. */
  3610. cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
  3611. if (cgrp_cfile != cgrp) {
  3612. ret = -EINVAL;
  3613. goto out_put_cfile;
  3614. }
  3615. if (!event->cft->register_event || !event->cft->unregister_event) {
  3616. ret = -EINVAL;
  3617. goto out_put_cfile;
  3618. }
  3619. ret = event->cft->register_event(event->css, event->cft,
  3620. event->eventfd, buffer);
  3621. if (ret)
  3622. goto out_put_cfile;
  3623. efile->f_op->poll(efile, &event->pt);
  3624. /*
  3625. * Events should be removed after rmdir of cgroup directory, but before
  3626. * destroying subsystem state objects. Let's take reference to cgroup
  3627. * directory dentry to do that.
  3628. */
  3629. dget(cgrp->dentry);
  3630. spin_lock(&cgrp->event_list_lock);
  3631. list_add(&event->list, &cgrp->event_list);
  3632. spin_unlock(&cgrp->event_list_lock);
  3633. fput(cfile);
  3634. fput(efile);
  3635. return 0;
  3636. out_put_cfile:
  3637. fput(cfile);
  3638. out_put_eventfd:
  3639. eventfd_ctx_put(event->eventfd);
  3640. out_put_efile:
  3641. fput(efile);
  3642. out_kfree:
  3643. kfree(event);
  3644. return ret;
  3645. }
  3646. static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
  3647. struct cftype *cft)
  3648. {
  3649. return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  3650. }
  3651. static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
  3652. struct cftype *cft, u64 val)
  3653. {
  3654. if (val)
  3655. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  3656. else
  3657. clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  3658. return 0;
  3659. }
  3660. static struct cftype cgroup_base_files[] = {
  3661. {
  3662. .name = "cgroup.procs",
  3663. .open = cgroup_procs_open,
  3664. .write_u64 = cgroup_procs_write,
  3665. .release = cgroup_pidlist_release,
  3666. .mode = S_IRUGO | S_IWUSR,
  3667. },
  3668. {
  3669. .name = "cgroup.event_control",
  3670. .write_string = cgroup_write_event_control,
  3671. .mode = S_IWUGO,
  3672. },
  3673. {
  3674. .name = "cgroup.clone_children",
  3675. .flags = CFTYPE_INSANE,
  3676. .read_u64 = cgroup_clone_children_read,
  3677. .write_u64 = cgroup_clone_children_write,
  3678. },
  3679. {
  3680. .name = "cgroup.sane_behavior",
  3681. .flags = CFTYPE_ONLY_ON_ROOT,
  3682. .read_seq_string = cgroup_sane_behavior_show,
  3683. },
  3684. /*
  3685. * Historical crazy stuff. These don't have "cgroup." prefix and
  3686. * don't exist if sane_behavior. If you're depending on these, be
  3687. * prepared to be burned.
  3688. */
  3689. {
  3690. .name = "tasks",
  3691. .flags = CFTYPE_INSANE, /* use "procs" instead */
  3692. .open = cgroup_tasks_open,
  3693. .write_u64 = cgroup_tasks_write,
  3694. .release = cgroup_pidlist_release,
  3695. .mode = S_IRUGO | S_IWUSR,
  3696. },
  3697. {
  3698. .name = "notify_on_release",
  3699. .flags = CFTYPE_INSANE,
  3700. .read_u64 = cgroup_read_notify_on_release,
  3701. .write_u64 = cgroup_write_notify_on_release,
  3702. },
  3703. {
  3704. .name = "release_agent",
  3705. .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
  3706. .read_seq_string = cgroup_release_agent_show,
  3707. .write_string = cgroup_release_agent_write,
  3708. .max_write_len = PATH_MAX,
  3709. },
  3710. { } /* terminate */
  3711. };
  3712. /**
  3713. * cgroup_populate_dir - create subsys files in a cgroup directory
  3714. * @cgrp: target cgroup
  3715. * @subsys_mask: mask of the subsystem ids whose files should be added
  3716. *
  3717. * On failure, no file is added.
  3718. */
  3719. static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
  3720. {
  3721. struct cgroup_subsys *ss;
  3722. int i, ret = 0;
  3723. /* process cftsets of each subsystem */
  3724. for_each_subsys(ss, i) {
  3725. struct cftype_set *set;
  3726. if (!test_bit(i, &subsys_mask))
  3727. continue;
  3728. list_for_each_entry(set, &ss->cftsets, node) {
  3729. ret = cgroup_addrm_files(cgrp, set->cfts, true);
  3730. if (ret < 0)
  3731. goto err;
  3732. }
  3733. }
  3734. /* This cgroup is ready now */
  3735. for_each_root_subsys(cgrp->root, ss) {
  3736. struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
  3737. struct css_id *id = rcu_dereference_protected(css->id, true);
  3738. /*
  3739. * Update id->css pointer and make this css visible from
  3740. * CSS ID functions. This pointer will be dereferened
  3741. * from RCU-read-side without locks.
  3742. */
  3743. if (id)
  3744. rcu_assign_pointer(id->css, css);
  3745. }
  3746. return 0;
  3747. err:
  3748. cgroup_clear_dir(cgrp, subsys_mask);
  3749. return ret;
  3750. }
  3751. /*
  3752. * css destruction is four-stage process.
  3753. *
  3754. * 1. Destruction starts. Killing of the percpu_ref is initiated.
  3755. * Implemented in kill_css().
  3756. *
  3757. * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
  3758. * and thus css_tryget() is guaranteed to fail, the css can be offlined
  3759. * by invoking offline_css(). After offlining, the base ref is put.
  3760. * Implemented in css_killed_work_fn().
  3761. *
  3762. * 3. When the percpu_ref reaches zero, the only possible remaining
  3763. * accessors are inside RCU read sections. css_release() schedules the
  3764. * RCU callback.
  3765. *
  3766. * 4. After the grace period, the css can be freed. Implemented in
  3767. * css_free_work_fn().
  3768. *
  3769. * It is actually hairier because both step 2 and 4 require process context
  3770. * and thus involve punting to css->destroy_work adding two additional
  3771. * steps to the already complex sequence.
  3772. */
  3773. static void css_free_work_fn(struct work_struct *work)
  3774. {
  3775. struct cgroup_subsys_state *css =
  3776. container_of(work, struct cgroup_subsys_state, destroy_work);
  3777. struct cgroup *cgrp = css->cgroup;
  3778. if (css->parent)
  3779. css_put(css->parent);
  3780. css->ss->css_free(css);
  3781. cgroup_dput(cgrp);
  3782. }
  3783. static void css_free_rcu_fn(struct rcu_head *rcu_head)
  3784. {
  3785. struct cgroup_subsys_state *css =
  3786. container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
  3787. /*
  3788. * css holds an extra ref to @cgrp->dentry which is put on the last
  3789. * css_put(). dput() requires process context which we don't have.
  3790. */
  3791. INIT_WORK(&css->destroy_work, css_free_work_fn);
  3792. schedule_work(&css->destroy_work);
  3793. }
  3794. static void css_release(struct percpu_ref *ref)
  3795. {
  3796. struct cgroup_subsys_state *css =
  3797. container_of(ref, struct cgroup_subsys_state, refcnt);
  3798. call_rcu(&css->rcu_head, css_free_rcu_fn);
  3799. }
  3800. static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
  3801. struct cgroup *cgrp)
  3802. {
  3803. css->cgroup = cgrp;
  3804. css->ss = ss;
  3805. css->flags = 0;
  3806. css->id = NULL;
  3807. if (cgrp->parent)
  3808. css->parent = cgroup_css(cgrp->parent, ss->subsys_id);
  3809. else
  3810. css->flags |= CSS_ROOT;
  3811. BUG_ON(cgroup_css(cgrp, ss->subsys_id));
  3812. }
  3813. /* invoke ->css_online() on a new CSS and mark it online if successful */
  3814. static int online_css(struct cgroup_subsys_state *css)
  3815. {
  3816. struct cgroup_subsys *ss = css->ss;
  3817. int ret = 0;
  3818. lockdep_assert_held(&cgroup_mutex);
  3819. if (ss->css_online)
  3820. ret = ss->css_online(css);
  3821. if (!ret) {
  3822. css->flags |= CSS_ONLINE;
  3823. css->cgroup->nr_css++;
  3824. rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
  3825. }
  3826. return ret;
  3827. }
  3828. /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
  3829. static void offline_css(struct cgroup_subsys_state *css)
  3830. {
  3831. struct cgroup_subsys *ss = css->ss;
  3832. lockdep_assert_held(&cgroup_mutex);
  3833. if (!(css->flags & CSS_ONLINE))
  3834. return;
  3835. if (ss->css_offline)
  3836. ss->css_offline(css);
  3837. css->flags &= ~CSS_ONLINE;
  3838. css->cgroup->nr_css--;
  3839. RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
  3840. }
  3841. /*
  3842. * cgroup_create - create a cgroup
  3843. * @parent: cgroup that will be parent of the new cgroup
  3844. * @dentry: dentry of the new cgroup
  3845. * @mode: mode to set on new inode
  3846. *
  3847. * Must be called with the mutex on the parent inode held
  3848. */
  3849. static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
  3850. umode_t mode)
  3851. {
  3852. struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
  3853. struct cgroup *cgrp;
  3854. struct cgroup_name *name;
  3855. struct cgroupfs_root *root = parent->root;
  3856. int err = 0;
  3857. struct cgroup_subsys *ss;
  3858. struct super_block *sb = root->sb;
  3859. /* allocate the cgroup and its ID, 0 is reserved for the root */
  3860. cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
  3861. if (!cgrp)
  3862. return -ENOMEM;
  3863. name = cgroup_alloc_name(dentry);
  3864. if (!name)
  3865. goto err_free_cgrp;
  3866. rcu_assign_pointer(cgrp->name, name);
  3867. /*
  3868. * Temporarily set the pointer to NULL, so idr_find() won't return
  3869. * a half-baked cgroup.
  3870. */
  3871. cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
  3872. if (cgrp->id < 0)
  3873. goto err_free_name;
  3874. /*
  3875. * Only live parents can have children. Note that the liveliness
  3876. * check isn't strictly necessary because cgroup_mkdir() and
  3877. * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
  3878. * anyway so that locking is contained inside cgroup proper and we
  3879. * don't get nasty surprises if we ever grow another caller.
  3880. */
  3881. if (!cgroup_lock_live_group(parent)) {
  3882. err = -ENODEV;
  3883. goto err_free_id;
  3884. }
  3885. /* Grab a reference on the superblock so the hierarchy doesn't
  3886. * get deleted on unmount if there are child cgroups. This
  3887. * can be done outside cgroup_mutex, since the sb can't
  3888. * disappear while someone has an open control file on the
  3889. * fs */
  3890. atomic_inc(&sb->s_active);
  3891. init_cgroup_housekeeping(cgrp);
  3892. dentry->d_fsdata = cgrp;
  3893. cgrp->dentry = dentry;
  3894. cgrp->parent = parent;
  3895. cgrp->dummy_css.parent = &parent->dummy_css;
  3896. cgrp->root = parent->root;
  3897. if (notify_on_release(parent))
  3898. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  3899. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
  3900. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
  3901. for_each_root_subsys(root, ss) {
  3902. struct cgroup_subsys_state *css;
  3903. css = ss->css_alloc(cgroup_css(parent, ss->subsys_id));
  3904. if (IS_ERR(css)) {
  3905. err = PTR_ERR(css);
  3906. goto err_free_all;
  3907. }
  3908. css_ar[ss->subsys_id] = css;
  3909. err = percpu_ref_init(&css->refcnt, css_release);
  3910. if (err)
  3911. goto err_free_all;
  3912. init_css(css, ss, cgrp);
  3913. if (ss->use_id) {
  3914. err = alloc_css_id(css);
  3915. if (err)
  3916. goto err_free_all;
  3917. }
  3918. }
  3919. /*
  3920. * Create directory. cgroup_create_file() returns with the new
  3921. * directory locked on success so that it can be populated without
  3922. * dropping cgroup_mutex.
  3923. */
  3924. err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
  3925. if (err < 0)
  3926. goto err_free_all;
  3927. lockdep_assert_held(&dentry->d_inode->i_mutex);
  3928. cgrp->serial_nr = cgroup_serial_nr_next++;
  3929. /* allocation complete, commit to creation */
  3930. list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
  3931. root->number_of_cgroups++;
  3932. /* each css holds a ref to the cgroup's dentry and the parent css */
  3933. for_each_root_subsys(root, ss) {
  3934. struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
  3935. dget(dentry);
  3936. css_get(css->parent);
  3937. }
  3938. /* hold a ref to the parent's dentry */
  3939. dget(parent->dentry);
  3940. /* creation succeeded, notify subsystems */
  3941. for_each_root_subsys(root, ss) {
  3942. struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
  3943. err = online_css(css);
  3944. if (err)
  3945. goto err_destroy;
  3946. if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
  3947. parent->parent) {
  3948. pr_warning("cgroup: %s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
  3949. current->comm, current->pid, ss->name);
  3950. if (!strcmp(ss->name, "memory"))
  3951. pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
  3952. ss->warned_broken_hierarchy = true;
  3953. }
  3954. }
  3955. idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
  3956. err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
  3957. if (err)
  3958. goto err_destroy;
  3959. err = cgroup_populate_dir(cgrp, root->subsys_mask);
  3960. if (err)
  3961. goto err_destroy;
  3962. mutex_unlock(&cgroup_mutex);
  3963. mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
  3964. return 0;
  3965. err_free_all:
  3966. for_each_root_subsys(root, ss) {
  3967. struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
  3968. if (css) {
  3969. percpu_ref_cancel_init(&css->refcnt);
  3970. ss->css_free(css);
  3971. }
  3972. }
  3973. mutex_unlock(&cgroup_mutex);
  3974. /* Release the reference count that we took on the superblock */
  3975. deactivate_super(sb);
  3976. err_free_id:
  3977. idr_remove(&root->cgroup_idr, cgrp->id);
  3978. err_free_name:
  3979. kfree(rcu_dereference_raw(cgrp->name));
  3980. err_free_cgrp:
  3981. kfree(cgrp);
  3982. return err;
  3983. err_destroy:
  3984. cgroup_destroy_locked(cgrp);
  3985. mutex_unlock(&cgroup_mutex);
  3986. mutex_unlock(&dentry->d_inode->i_mutex);
  3987. return err;
  3988. }
  3989. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  3990. {
  3991. struct cgroup *c_parent = dentry->d_parent->d_fsdata;
  3992. /* the vfs holds inode->i_mutex already */
  3993. return cgroup_create(c_parent, dentry, mode | S_IFDIR);
  3994. }
  3995. /*
  3996. * This is called when the refcnt of a css is confirmed to be killed.
  3997. * css_tryget() is now guaranteed to fail.
  3998. */
  3999. static void css_killed_work_fn(struct work_struct *work)
  4000. {
  4001. struct cgroup_subsys_state *css =
  4002. container_of(work, struct cgroup_subsys_state, destroy_work);
  4003. struct cgroup *cgrp = css->cgroup;
  4004. mutex_lock(&cgroup_mutex);
  4005. /*
  4006. * css_tryget() is guaranteed to fail now. Tell subsystems to
  4007. * initate destruction.
  4008. */
  4009. offline_css(css);
  4010. /*
  4011. * If @cgrp is marked dead, it's waiting for refs of all css's to
  4012. * be disabled before proceeding to the second phase of cgroup
  4013. * destruction. If we are the last one, kick it off.
  4014. */
  4015. if (!cgrp->nr_css && cgroup_is_dead(cgrp))
  4016. cgroup_destroy_css_killed(cgrp);
  4017. mutex_unlock(&cgroup_mutex);
  4018. /*
  4019. * Put the css refs from kill_css(). Each css holds an extra
  4020. * reference to the cgroup's dentry and cgroup removal proceeds
  4021. * regardless of css refs. On the last put of each css, whenever
  4022. * that may be, the extra dentry ref is put so that dentry
  4023. * destruction happens only after all css's are released.
  4024. */
  4025. css_put(css);
  4026. }
  4027. /* css kill confirmation processing requires process context, bounce */
  4028. static void css_killed_ref_fn(struct percpu_ref *ref)
  4029. {
  4030. struct cgroup_subsys_state *css =
  4031. container_of(ref, struct cgroup_subsys_state, refcnt);
  4032. INIT_WORK(&css->destroy_work, css_killed_work_fn);
  4033. schedule_work(&css->destroy_work);
  4034. }
  4035. /**
  4036. * kill_css - destroy a css
  4037. * @css: css to destroy
  4038. *
  4039. * This function initiates destruction of @css by removing cgroup interface
  4040. * files and putting its base reference. ->css_offline() will be invoked
  4041. * asynchronously once css_tryget() is guaranteed to fail and when the
  4042. * reference count reaches zero, @css will be released.
  4043. */
  4044. static void kill_css(struct cgroup_subsys_state *css)
  4045. {
  4046. cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
  4047. /*
  4048. * Killing would put the base ref, but we need to keep it alive
  4049. * until after ->css_offline().
  4050. */
  4051. css_get(css);
  4052. /*
  4053. * cgroup core guarantees that, by the time ->css_offline() is
  4054. * invoked, no new css reference will be given out via
  4055. * css_tryget(). We can't simply call percpu_ref_kill() and
  4056. * proceed to offlining css's because percpu_ref_kill() doesn't
  4057. * guarantee that the ref is seen as killed on all CPUs on return.
  4058. *
  4059. * Use percpu_ref_kill_and_confirm() to get notifications as each
  4060. * css is confirmed to be seen as killed on all CPUs.
  4061. */
  4062. percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
  4063. }
  4064. /**
  4065. * cgroup_destroy_locked - the first stage of cgroup destruction
  4066. * @cgrp: cgroup to be destroyed
  4067. *
  4068. * css's make use of percpu refcnts whose killing latency shouldn't be
  4069. * exposed to userland and are RCU protected. Also, cgroup core needs to
  4070. * guarantee that css_tryget() won't succeed by the time ->css_offline() is
  4071. * invoked. To satisfy all the requirements, destruction is implemented in
  4072. * the following two steps.
  4073. *
  4074. * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
  4075. * userland visible parts and start killing the percpu refcnts of
  4076. * css's. Set up so that the next stage will be kicked off once all
  4077. * the percpu refcnts are confirmed to be killed.
  4078. *
  4079. * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
  4080. * rest of destruction. Once all cgroup references are gone, the
  4081. * cgroup is RCU-freed.
  4082. *
  4083. * This function implements s1. After this step, @cgrp is gone as far as
  4084. * the userland is concerned and a new cgroup with the same name may be
  4085. * created. As cgroup doesn't care about the names internally, this
  4086. * doesn't cause any problem.
  4087. */
  4088. static int cgroup_destroy_locked(struct cgroup *cgrp)
  4089. __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
  4090. {
  4091. struct dentry *d = cgrp->dentry;
  4092. struct cgroup_event *event, *tmp;
  4093. struct cgroup_subsys *ss;
  4094. bool empty;
  4095. lockdep_assert_held(&d->d_inode->i_mutex);
  4096. lockdep_assert_held(&cgroup_mutex);
  4097. /*
  4098. * css_set_lock synchronizes access to ->cset_links and prevents
  4099. * @cgrp from being removed while __put_css_set() is in progress.
  4100. */
  4101. read_lock(&css_set_lock);
  4102. empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
  4103. read_unlock(&css_set_lock);
  4104. if (!empty)
  4105. return -EBUSY;
  4106. /*
  4107. * Initiate massacre of all css's. cgroup_destroy_css_killed()
  4108. * will be invoked to perform the rest of destruction once the
  4109. * percpu refs of all css's are confirmed to be killed.
  4110. */
  4111. for_each_root_subsys(cgrp->root, ss)
  4112. kill_css(cgroup_css(cgrp, ss->subsys_id));
  4113. /*
  4114. * Mark @cgrp dead. This prevents further task migration and child
  4115. * creation by disabling cgroup_lock_live_group(). Note that
  4116. * CGRP_DEAD assertion is depended upon by css_next_child() to
  4117. * resume iteration after dropping RCU read lock. See
  4118. * css_next_child() for details.
  4119. */
  4120. set_bit(CGRP_DEAD, &cgrp->flags);
  4121. /* CGRP_DEAD is set, remove from ->release_list for the last time */
  4122. raw_spin_lock(&release_list_lock);
  4123. if (!list_empty(&cgrp->release_list))
  4124. list_del_init(&cgrp->release_list);
  4125. raw_spin_unlock(&release_list_lock);
  4126. /*
  4127. * If @cgrp has css's attached, the second stage of cgroup
  4128. * destruction is kicked off from css_killed_work_fn() after the
  4129. * refs of all attached css's are killed. If @cgrp doesn't have
  4130. * any css, we kick it off here.
  4131. */
  4132. if (!cgrp->nr_css)
  4133. cgroup_destroy_css_killed(cgrp);
  4134. /*
  4135. * Clear the base files and remove @cgrp directory. The removal
  4136. * puts the base ref but we aren't quite done with @cgrp yet, so
  4137. * hold onto it.
  4138. */
  4139. cgroup_addrm_files(cgrp, cgroup_base_files, false);
  4140. dget(d);
  4141. cgroup_d_remove_dir(d);
  4142. /*
  4143. * Unregister events and notify userspace.
  4144. * Notify userspace about cgroup removing only after rmdir of cgroup
  4145. * directory to avoid race between userspace and kernelspace.
  4146. */
  4147. spin_lock(&cgrp->event_list_lock);
  4148. list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
  4149. list_del_init(&event->list);
  4150. schedule_work(&event->remove);
  4151. }
  4152. spin_unlock(&cgrp->event_list_lock);
  4153. return 0;
  4154. };
  4155. /**
  4156. * cgroup_destroy_css_killed - the second step of cgroup destruction
  4157. * @work: cgroup->destroy_free_work
  4158. *
  4159. * This function is invoked from a work item for a cgroup which is being
  4160. * destroyed after all css's are offlined and performs the rest of
  4161. * destruction. This is the second step of destruction described in the
  4162. * comment above cgroup_destroy_locked().
  4163. */
  4164. static void cgroup_destroy_css_killed(struct cgroup *cgrp)
  4165. {
  4166. struct cgroup *parent = cgrp->parent;
  4167. struct dentry *d = cgrp->dentry;
  4168. lockdep_assert_held(&cgroup_mutex);
  4169. /* delete this cgroup from parent->children */
  4170. list_del_rcu(&cgrp->sibling);
  4171. /*
  4172. * We should remove the cgroup object from idr before its grace
  4173. * period starts, so we won't be looking up a cgroup while the
  4174. * cgroup is being freed.
  4175. */
  4176. idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
  4177. cgrp->id = -1;
  4178. dput(d);
  4179. set_bit(CGRP_RELEASABLE, &parent->flags);
  4180. check_for_release(parent);
  4181. }
  4182. static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
  4183. {
  4184. int ret;
  4185. mutex_lock(&cgroup_mutex);
  4186. ret = cgroup_destroy_locked(dentry->d_fsdata);
  4187. mutex_unlock(&cgroup_mutex);
  4188. return ret;
  4189. }
  4190. static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
  4191. {
  4192. INIT_LIST_HEAD(&ss->cftsets);
  4193. /*
  4194. * base_cftset is embedded in subsys itself, no need to worry about
  4195. * deregistration.
  4196. */
  4197. if (ss->base_cftypes) {
  4198. struct cftype *cft;
  4199. for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
  4200. cft->ss = ss;
  4201. ss->base_cftset.cfts = ss->base_cftypes;
  4202. list_add_tail(&ss->base_cftset.node, &ss->cftsets);
  4203. }
  4204. }
  4205. static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
  4206. {
  4207. struct cgroup_subsys_state *css;
  4208. printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
  4209. mutex_lock(&cgroup_mutex);
  4210. /* init base cftset */
  4211. cgroup_init_cftsets(ss);
  4212. /* Create the top cgroup state for this subsystem */
  4213. list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
  4214. ss->root = &cgroup_dummy_root;
  4215. css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss->subsys_id));
  4216. /* We don't handle early failures gracefully */
  4217. BUG_ON(IS_ERR(css));
  4218. init_css(css, ss, cgroup_dummy_top);
  4219. /* Update the init_css_set to contain a subsys
  4220. * pointer to this state - since the subsystem is
  4221. * newly registered, all tasks and hence the
  4222. * init_css_set is in the subsystem's top cgroup. */
  4223. init_css_set.subsys[ss->subsys_id] = css;
  4224. need_forkexit_callback |= ss->fork || ss->exit;
  4225. /* At system boot, before all subsystems have been
  4226. * registered, no tasks have been forked, so we don't
  4227. * need to invoke fork callbacks here. */
  4228. BUG_ON(!list_empty(&init_task.tasks));
  4229. BUG_ON(online_css(css));
  4230. mutex_unlock(&cgroup_mutex);
  4231. /* this function shouldn't be used with modular subsystems, since they
  4232. * need to register a subsys_id, among other things */
  4233. BUG_ON(ss->module);
  4234. }
  4235. /**
  4236. * cgroup_load_subsys: load and register a modular subsystem at runtime
  4237. * @ss: the subsystem to load
  4238. *
  4239. * This function should be called in a modular subsystem's initcall. If the
  4240. * subsystem is built as a module, it will be assigned a new subsys_id and set
  4241. * up for use. If the subsystem is built-in anyway, work is delegated to the
  4242. * simpler cgroup_init_subsys.
  4243. */
  4244. int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
  4245. {
  4246. struct cgroup_subsys_state *css;
  4247. int i, ret;
  4248. struct hlist_node *tmp;
  4249. struct css_set *cset;
  4250. unsigned long key;
  4251. /* check name and function validity */
  4252. if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
  4253. ss->css_alloc == NULL || ss->css_free == NULL)
  4254. return -EINVAL;
  4255. /*
  4256. * we don't support callbacks in modular subsystems. this check is
  4257. * before the ss->module check for consistency; a subsystem that could
  4258. * be a module should still have no callbacks even if the user isn't
  4259. * compiling it as one.
  4260. */
  4261. if (ss->fork || ss->exit)
  4262. return -EINVAL;
  4263. /*
  4264. * an optionally modular subsystem is built-in: we want to do nothing,
  4265. * since cgroup_init_subsys will have already taken care of it.
  4266. */
  4267. if (ss->module == NULL) {
  4268. /* a sanity check */
  4269. BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
  4270. return 0;
  4271. }
  4272. /* init base cftset */
  4273. cgroup_init_cftsets(ss);
  4274. mutex_lock(&cgroup_mutex);
  4275. cgroup_subsys[ss->subsys_id] = ss;
  4276. /*
  4277. * no ss->css_alloc seems to need anything important in the ss
  4278. * struct, so this can happen first (i.e. before the dummy root
  4279. * attachment).
  4280. */
  4281. css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss->subsys_id));
  4282. if (IS_ERR(css)) {
  4283. /* failure case - need to deassign the cgroup_subsys[] slot. */
  4284. cgroup_subsys[ss->subsys_id] = NULL;
  4285. mutex_unlock(&cgroup_mutex);
  4286. return PTR_ERR(css);
  4287. }
  4288. list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
  4289. ss->root = &cgroup_dummy_root;
  4290. /* our new subsystem will be attached to the dummy hierarchy. */
  4291. init_css(css, ss, cgroup_dummy_top);
  4292. /* init_idr must be after init_css() because it sets css->id. */
  4293. if (ss->use_id) {
  4294. ret = cgroup_init_idr(ss, css);
  4295. if (ret)
  4296. goto err_unload;
  4297. }
  4298. /*
  4299. * Now we need to entangle the css into the existing css_sets. unlike
  4300. * in cgroup_init_subsys, there are now multiple css_sets, so each one
  4301. * will need a new pointer to it; done by iterating the css_set_table.
  4302. * furthermore, modifying the existing css_sets will corrupt the hash
  4303. * table state, so each changed css_set will need its hash recomputed.
  4304. * this is all done under the css_set_lock.
  4305. */
  4306. write_lock(&css_set_lock);
  4307. hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
  4308. /* skip entries that we already rehashed */
  4309. if (cset->subsys[ss->subsys_id])
  4310. continue;
  4311. /* remove existing entry */
  4312. hash_del(&cset->hlist);
  4313. /* set new value */
  4314. cset->subsys[ss->subsys_id] = css;
  4315. /* recompute hash and restore entry */
  4316. key = css_set_hash(cset->subsys);
  4317. hash_add(css_set_table, &cset->hlist, key);
  4318. }
  4319. write_unlock(&css_set_lock);
  4320. ret = online_css(css);
  4321. if (ret)
  4322. goto err_unload;
  4323. /* success! */
  4324. mutex_unlock(&cgroup_mutex);
  4325. return 0;
  4326. err_unload:
  4327. mutex_unlock(&cgroup_mutex);
  4328. /* @ss can't be mounted here as try_module_get() would fail */
  4329. cgroup_unload_subsys(ss);
  4330. return ret;
  4331. }
  4332. EXPORT_SYMBOL_GPL(cgroup_load_subsys);
  4333. /**
  4334. * cgroup_unload_subsys: unload a modular subsystem
  4335. * @ss: the subsystem to unload
  4336. *
  4337. * This function should be called in a modular subsystem's exitcall. When this
  4338. * function is invoked, the refcount on the subsystem's module will be 0, so
  4339. * the subsystem will not be attached to any hierarchy.
  4340. */
  4341. void cgroup_unload_subsys(struct cgroup_subsys *ss)
  4342. {
  4343. struct cgrp_cset_link *link;
  4344. BUG_ON(ss->module == NULL);
  4345. /*
  4346. * we shouldn't be called if the subsystem is in use, and the use of
  4347. * try_module_get() in rebind_subsystems() should ensure that it
  4348. * doesn't start being used while we're killing it off.
  4349. */
  4350. BUG_ON(ss->root != &cgroup_dummy_root);
  4351. mutex_lock(&cgroup_mutex);
  4352. offline_css(cgroup_css(cgroup_dummy_top, ss->subsys_id));
  4353. if (ss->use_id)
  4354. idr_destroy(&ss->idr);
  4355. /* deassign the subsys_id */
  4356. cgroup_subsys[ss->subsys_id] = NULL;
  4357. /* remove subsystem from the dummy root's list of subsystems */
  4358. list_del_init(&ss->sibling);
  4359. /*
  4360. * disentangle the css from all css_sets attached to the dummy
  4361. * top. as in loading, we need to pay our respects to the hashtable
  4362. * gods.
  4363. */
  4364. write_lock(&css_set_lock);
  4365. list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
  4366. struct css_set *cset = link->cset;
  4367. unsigned long key;
  4368. hash_del(&cset->hlist);
  4369. cset->subsys[ss->subsys_id] = NULL;
  4370. key = css_set_hash(cset->subsys);
  4371. hash_add(css_set_table, &cset->hlist, key);
  4372. }
  4373. write_unlock(&css_set_lock);
  4374. /*
  4375. * remove subsystem's css from the cgroup_dummy_top and free it -
  4376. * need to free before marking as null because ss->css_free needs
  4377. * the cgrp->subsys pointer to find their state. note that this
  4378. * also takes care of freeing the css_id.
  4379. */
  4380. ss->css_free(cgroup_css(cgroup_dummy_top, ss->subsys_id));
  4381. RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
  4382. mutex_unlock(&cgroup_mutex);
  4383. }
  4384. EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
  4385. /**
  4386. * cgroup_init_early - cgroup initialization at system boot
  4387. *
  4388. * Initialize cgroups at system boot, and initialize any
  4389. * subsystems that request early init.
  4390. */
  4391. int __init cgroup_init_early(void)
  4392. {
  4393. struct cgroup_subsys *ss;
  4394. int i;
  4395. atomic_set(&init_css_set.refcount, 1);
  4396. INIT_LIST_HEAD(&init_css_set.cgrp_links);
  4397. INIT_LIST_HEAD(&init_css_set.tasks);
  4398. INIT_HLIST_NODE(&init_css_set.hlist);
  4399. css_set_count = 1;
  4400. init_cgroup_root(&cgroup_dummy_root);
  4401. cgroup_root_count = 1;
  4402. RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
  4403. init_cgrp_cset_link.cset = &init_css_set;
  4404. init_cgrp_cset_link.cgrp = cgroup_dummy_top;
  4405. list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
  4406. list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
  4407. /* at bootup time, we don't worry about modular subsystems */
  4408. for_each_builtin_subsys(ss, i) {
  4409. BUG_ON(!ss->name);
  4410. BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
  4411. BUG_ON(!ss->css_alloc);
  4412. BUG_ON(!ss->css_free);
  4413. if (ss->subsys_id != i) {
  4414. printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
  4415. ss->name, ss->subsys_id);
  4416. BUG();
  4417. }
  4418. if (ss->early_init)
  4419. cgroup_init_subsys(ss);
  4420. }
  4421. return 0;
  4422. }
  4423. /**
  4424. * cgroup_init - cgroup initialization
  4425. *
  4426. * Register cgroup filesystem and /proc file, and initialize
  4427. * any subsystems that didn't request early init.
  4428. */
  4429. int __init cgroup_init(void)
  4430. {
  4431. struct cgroup_subsys *ss;
  4432. unsigned long key;
  4433. int i, err;
  4434. err = bdi_init(&cgroup_backing_dev_info);
  4435. if (err)
  4436. return err;
  4437. for_each_builtin_subsys(ss, i) {
  4438. if (!ss->early_init)
  4439. cgroup_init_subsys(ss);
  4440. if (ss->use_id)
  4441. cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
  4442. }
  4443. /* allocate id for the dummy hierarchy */
  4444. mutex_lock(&cgroup_mutex);
  4445. mutex_lock(&cgroup_root_mutex);
  4446. /* Add init_css_set to the hash table */
  4447. key = css_set_hash(init_css_set.subsys);
  4448. hash_add(css_set_table, &init_css_set.hlist, key);
  4449. BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
  4450. err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
  4451. 0, 1, GFP_KERNEL);
  4452. BUG_ON(err < 0);
  4453. mutex_unlock(&cgroup_root_mutex);
  4454. mutex_unlock(&cgroup_mutex);
  4455. cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
  4456. if (!cgroup_kobj) {
  4457. err = -ENOMEM;
  4458. goto out;
  4459. }
  4460. err = register_filesystem(&cgroup_fs_type);
  4461. if (err < 0) {
  4462. kobject_put(cgroup_kobj);
  4463. goto out;
  4464. }
  4465. proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
  4466. out:
  4467. if (err)
  4468. bdi_destroy(&cgroup_backing_dev_info);
  4469. return err;
  4470. }
  4471. /*
  4472. * proc_cgroup_show()
  4473. * - Print task's cgroup paths into seq_file, one line for each hierarchy
  4474. * - Used for /proc/<pid>/cgroup.
  4475. * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
  4476. * doesn't really matter if tsk->cgroup changes after we read it,
  4477. * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
  4478. * anyway. No need to check that tsk->cgroup != NULL, thanks to
  4479. * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
  4480. * cgroup to top_cgroup.
  4481. */
  4482. /* TODO: Use a proper seq_file iterator */
  4483. int proc_cgroup_show(struct seq_file *m, void *v)
  4484. {
  4485. struct pid *pid;
  4486. struct task_struct *tsk;
  4487. char *buf;
  4488. int retval;
  4489. struct cgroupfs_root *root;
  4490. retval = -ENOMEM;
  4491. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  4492. if (!buf)
  4493. goto out;
  4494. retval = -ESRCH;
  4495. pid = m->private;
  4496. tsk = get_pid_task(pid, PIDTYPE_PID);
  4497. if (!tsk)
  4498. goto out_free;
  4499. retval = 0;
  4500. mutex_lock(&cgroup_mutex);
  4501. for_each_active_root(root) {
  4502. struct cgroup_subsys *ss;
  4503. struct cgroup *cgrp;
  4504. int count = 0;
  4505. seq_printf(m, "%d:", root->hierarchy_id);
  4506. for_each_root_subsys(root, ss)
  4507. seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
  4508. if (strlen(root->name))
  4509. seq_printf(m, "%sname=%s", count ? "," : "",
  4510. root->name);
  4511. seq_putc(m, ':');
  4512. cgrp = task_cgroup_from_root(tsk, root);
  4513. retval = cgroup_path(cgrp, buf, PAGE_SIZE);
  4514. if (retval < 0)
  4515. goto out_unlock;
  4516. seq_puts(m, buf);
  4517. seq_putc(m, '\n');
  4518. }
  4519. out_unlock:
  4520. mutex_unlock(&cgroup_mutex);
  4521. put_task_struct(tsk);
  4522. out_free:
  4523. kfree(buf);
  4524. out:
  4525. return retval;
  4526. }
  4527. /* Display information about each subsystem and each hierarchy */
  4528. static int proc_cgroupstats_show(struct seq_file *m, void *v)
  4529. {
  4530. struct cgroup_subsys *ss;
  4531. int i;
  4532. seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
  4533. /*
  4534. * ideally we don't want subsystems moving around while we do this.
  4535. * cgroup_mutex is also necessary to guarantee an atomic snapshot of
  4536. * subsys/hierarchy state.
  4537. */
  4538. mutex_lock(&cgroup_mutex);
  4539. for_each_subsys(ss, i)
  4540. seq_printf(m, "%s\t%d\t%d\t%d\n",
  4541. ss->name, ss->root->hierarchy_id,
  4542. ss->root->number_of_cgroups, !ss->disabled);
  4543. mutex_unlock(&cgroup_mutex);
  4544. return 0;
  4545. }
  4546. static int cgroupstats_open(struct inode *inode, struct file *file)
  4547. {
  4548. return single_open(file, proc_cgroupstats_show, NULL);
  4549. }
  4550. static const struct file_operations proc_cgroupstats_operations = {
  4551. .open = cgroupstats_open,
  4552. .read = seq_read,
  4553. .llseek = seq_lseek,
  4554. .release = single_release,
  4555. };
  4556. /**
  4557. * cgroup_fork - attach newly forked task to its parents cgroup.
  4558. * @child: pointer to task_struct of forking parent process.
  4559. *
  4560. * Description: A task inherits its parent's cgroup at fork().
  4561. *
  4562. * A pointer to the shared css_set was automatically copied in
  4563. * fork.c by dup_task_struct(). However, we ignore that copy, since
  4564. * it was not made under the protection of RCU or cgroup_mutex, so
  4565. * might no longer be a valid cgroup pointer. cgroup_attach_task() might
  4566. * have already changed current->cgroups, allowing the previously
  4567. * referenced cgroup group to be removed and freed.
  4568. *
  4569. * At the point that cgroup_fork() is called, 'current' is the parent
  4570. * task, and the passed argument 'child' points to the child task.
  4571. */
  4572. void cgroup_fork(struct task_struct *child)
  4573. {
  4574. task_lock(current);
  4575. get_css_set(task_css_set(current));
  4576. child->cgroups = current->cgroups;
  4577. task_unlock(current);
  4578. INIT_LIST_HEAD(&child->cg_list);
  4579. }
  4580. /**
  4581. * cgroup_post_fork - called on a new task after adding it to the task list
  4582. * @child: the task in question
  4583. *
  4584. * Adds the task to the list running through its css_set if necessary and
  4585. * call the subsystem fork() callbacks. Has to be after the task is
  4586. * visible on the task list in case we race with the first call to
  4587. * cgroup_task_iter_start() - to guarantee that the new task ends up on its
  4588. * list.
  4589. */
  4590. void cgroup_post_fork(struct task_struct *child)
  4591. {
  4592. struct cgroup_subsys *ss;
  4593. int i;
  4594. /*
  4595. * use_task_css_set_links is set to 1 before we walk the tasklist
  4596. * under the tasklist_lock and we read it here after we added the child
  4597. * to the tasklist under the tasklist_lock as well. If the child wasn't
  4598. * yet in the tasklist when we walked through it from
  4599. * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
  4600. * should be visible now due to the paired locking and barriers implied
  4601. * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
  4602. * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
  4603. * lock on fork.
  4604. */
  4605. if (use_task_css_set_links) {
  4606. write_lock(&css_set_lock);
  4607. task_lock(child);
  4608. if (list_empty(&child->cg_list))
  4609. list_add(&child->cg_list, &task_css_set(child)->tasks);
  4610. task_unlock(child);
  4611. write_unlock(&css_set_lock);
  4612. }
  4613. /*
  4614. * Call ss->fork(). This must happen after @child is linked on
  4615. * css_set; otherwise, @child might change state between ->fork()
  4616. * and addition to css_set.
  4617. */
  4618. if (need_forkexit_callback) {
  4619. /*
  4620. * fork/exit callbacks are supported only for builtin
  4621. * subsystems, and the builtin section of the subsys
  4622. * array is immutable, so we don't need to lock the
  4623. * subsys array here. On the other hand, modular section
  4624. * of the array can be freed at module unload, so we
  4625. * can't touch that.
  4626. */
  4627. for_each_builtin_subsys(ss, i)
  4628. if (ss->fork)
  4629. ss->fork(child);
  4630. }
  4631. }
  4632. /**
  4633. * cgroup_exit - detach cgroup from exiting task
  4634. * @tsk: pointer to task_struct of exiting process
  4635. * @run_callback: run exit callbacks?
  4636. *
  4637. * Description: Detach cgroup from @tsk and release it.
  4638. *
  4639. * Note that cgroups marked notify_on_release force every task in
  4640. * them to take the global cgroup_mutex mutex when exiting.
  4641. * This could impact scaling on very large systems. Be reluctant to
  4642. * use notify_on_release cgroups where very high task exit scaling
  4643. * is required on large systems.
  4644. *
  4645. * the_top_cgroup_hack:
  4646. *
  4647. * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
  4648. *
  4649. * We call cgroup_exit() while the task is still competent to
  4650. * handle notify_on_release(), then leave the task attached to the
  4651. * root cgroup in each hierarchy for the remainder of its exit.
  4652. *
  4653. * To do this properly, we would increment the reference count on
  4654. * top_cgroup, and near the very end of the kernel/exit.c do_exit()
  4655. * code we would add a second cgroup function call, to drop that
  4656. * reference. This would just create an unnecessary hot spot on
  4657. * the top_cgroup reference count, to no avail.
  4658. *
  4659. * Normally, holding a reference to a cgroup without bumping its
  4660. * count is unsafe. The cgroup could go away, or someone could
  4661. * attach us to a different cgroup, decrementing the count on
  4662. * the first cgroup that we never incremented. But in this case,
  4663. * top_cgroup isn't going away, and either task has PF_EXITING set,
  4664. * which wards off any cgroup_attach_task() attempts, or task is a failed
  4665. * fork, never visible to cgroup_attach_task.
  4666. */
  4667. void cgroup_exit(struct task_struct *tsk, int run_callbacks)
  4668. {
  4669. struct cgroup_subsys *ss;
  4670. struct css_set *cset;
  4671. int i;
  4672. /*
  4673. * Unlink from the css_set task list if necessary.
  4674. * Optimistically check cg_list before taking
  4675. * css_set_lock
  4676. */
  4677. if (!list_empty(&tsk->cg_list)) {
  4678. write_lock(&css_set_lock);
  4679. if (!list_empty(&tsk->cg_list))
  4680. list_del_init(&tsk->cg_list);
  4681. write_unlock(&css_set_lock);
  4682. }
  4683. /* Reassign the task to the init_css_set. */
  4684. task_lock(tsk);
  4685. cset = task_css_set(tsk);
  4686. RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
  4687. if (run_callbacks && need_forkexit_callback) {
  4688. /*
  4689. * fork/exit callbacks are supported only for builtin
  4690. * subsystems, see cgroup_post_fork() for details.
  4691. */
  4692. for_each_builtin_subsys(ss, i) {
  4693. if (ss->exit) {
  4694. struct cgroup_subsys_state *old_css = cset->subsys[i];
  4695. struct cgroup_subsys_state *css = task_css(tsk, i);
  4696. ss->exit(css, old_css, tsk);
  4697. }
  4698. }
  4699. }
  4700. task_unlock(tsk);
  4701. put_css_set_taskexit(cset);
  4702. }
  4703. static void check_for_release(struct cgroup *cgrp)
  4704. {
  4705. if (cgroup_is_releasable(cgrp) &&
  4706. list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
  4707. /*
  4708. * Control Group is currently removeable. If it's not
  4709. * already queued for a userspace notification, queue
  4710. * it now
  4711. */
  4712. int need_schedule_work = 0;
  4713. raw_spin_lock(&release_list_lock);
  4714. if (!cgroup_is_dead(cgrp) &&
  4715. list_empty(&cgrp->release_list)) {
  4716. list_add(&cgrp->release_list, &release_list);
  4717. need_schedule_work = 1;
  4718. }
  4719. raw_spin_unlock(&release_list_lock);
  4720. if (need_schedule_work)
  4721. schedule_work(&release_agent_work);
  4722. }
  4723. }
  4724. /*
  4725. * Notify userspace when a cgroup is released, by running the
  4726. * configured release agent with the name of the cgroup (path
  4727. * relative to the root of cgroup file system) as the argument.
  4728. *
  4729. * Most likely, this user command will try to rmdir this cgroup.
  4730. *
  4731. * This races with the possibility that some other task will be
  4732. * attached to this cgroup before it is removed, or that some other
  4733. * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
  4734. * The presumed 'rmdir' will fail quietly if this cgroup is no longer
  4735. * unused, and this cgroup will be reprieved from its death sentence,
  4736. * to continue to serve a useful existence. Next time it's released,
  4737. * we will get notified again, if it still has 'notify_on_release' set.
  4738. *
  4739. * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
  4740. * means only wait until the task is successfully execve()'d. The
  4741. * separate release agent task is forked by call_usermodehelper(),
  4742. * then control in this thread returns here, without waiting for the
  4743. * release agent task. We don't bother to wait because the caller of
  4744. * this routine has no use for the exit status of the release agent
  4745. * task, so no sense holding our caller up for that.
  4746. */
  4747. static void cgroup_release_agent(struct work_struct *work)
  4748. {
  4749. BUG_ON(work != &release_agent_work);
  4750. mutex_lock(&cgroup_mutex);
  4751. raw_spin_lock(&release_list_lock);
  4752. while (!list_empty(&release_list)) {
  4753. char *argv[3], *envp[3];
  4754. int i;
  4755. char *pathbuf = NULL, *agentbuf = NULL;
  4756. struct cgroup *cgrp = list_entry(release_list.next,
  4757. struct cgroup,
  4758. release_list);
  4759. list_del_init(&cgrp->release_list);
  4760. raw_spin_unlock(&release_list_lock);
  4761. pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  4762. if (!pathbuf)
  4763. goto continue_free;
  4764. if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
  4765. goto continue_free;
  4766. agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
  4767. if (!agentbuf)
  4768. goto continue_free;
  4769. i = 0;
  4770. argv[i++] = agentbuf;
  4771. argv[i++] = pathbuf;
  4772. argv[i] = NULL;
  4773. i = 0;
  4774. /* minimal command environment */
  4775. envp[i++] = "HOME=/";
  4776. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  4777. envp[i] = NULL;
  4778. /* Drop the lock while we invoke the usermode helper,
  4779. * since the exec could involve hitting disk and hence
  4780. * be a slow process */
  4781. mutex_unlock(&cgroup_mutex);
  4782. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  4783. mutex_lock(&cgroup_mutex);
  4784. continue_free:
  4785. kfree(pathbuf);
  4786. kfree(agentbuf);
  4787. raw_spin_lock(&release_list_lock);
  4788. }
  4789. raw_spin_unlock(&release_list_lock);
  4790. mutex_unlock(&cgroup_mutex);
  4791. }
  4792. static int __init cgroup_disable(char *str)
  4793. {
  4794. struct cgroup_subsys *ss;
  4795. char *token;
  4796. int i;
  4797. while ((token = strsep(&str, ",")) != NULL) {
  4798. if (!*token)
  4799. continue;
  4800. /*
  4801. * cgroup_disable, being at boot time, can't know about
  4802. * module subsystems, so we don't worry about them.
  4803. */
  4804. for_each_builtin_subsys(ss, i) {
  4805. if (!strcmp(token, ss->name)) {
  4806. ss->disabled = 1;
  4807. printk(KERN_INFO "Disabling %s control group"
  4808. " subsystem\n", ss->name);
  4809. break;
  4810. }
  4811. }
  4812. }
  4813. return 1;
  4814. }
  4815. __setup("cgroup_disable=", cgroup_disable);
  4816. /*
  4817. * Functons for CSS ID.
  4818. */
  4819. /* to get ID other than 0, this should be called when !cgroup_is_dead() */
  4820. unsigned short css_id(struct cgroup_subsys_state *css)
  4821. {
  4822. struct css_id *cssid;
  4823. /*
  4824. * This css_id() can return correct value when somone has refcnt
  4825. * on this or this is under rcu_read_lock(). Once css->id is allocated,
  4826. * it's unchanged until freed.
  4827. */
  4828. cssid = rcu_dereference_raw(css->id);
  4829. if (cssid)
  4830. return cssid->id;
  4831. return 0;
  4832. }
  4833. EXPORT_SYMBOL_GPL(css_id);
  4834. /**
  4835. * css_is_ancestor - test "root" css is an ancestor of "child"
  4836. * @child: the css to be tested.
  4837. * @root: the css supporsed to be an ancestor of the child.
  4838. *
  4839. * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
  4840. * this function reads css->id, the caller must hold rcu_read_lock().
  4841. * But, considering usual usage, the csses should be valid objects after test.
  4842. * Assuming that the caller will do some action to the child if this returns
  4843. * returns true, the caller must take "child";s reference count.
  4844. * If "child" is valid object and this returns true, "root" is valid, too.
  4845. */
  4846. bool css_is_ancestor(struct cgroup_subsys_state *child,
  4847. const struct cgroup_subsys_state *root)
  4848. {
  4849. struct css_id *child_id;
  4850. struct css_id *root_id;
  4851. child_id = rcu_dereference(child->id);
  4852. if (!child_id)
  4853. return false;
  4854. root_id = rcu_dereference(root->id);
  4855. if (!root_id)
  4856. return false;
  4857. if (child_id->depth < root_id->depth)
  4858. return false;
  4859. if (child_id->stack[root_id->depth] != root_id->id)
  4860. return false;
  4861. return true;
  4862. }
  4863. void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
  4864. {
  4865. struct css_id *id = rcu_dereference_protected(css->id, true);
  4866. /* When this is called before css_id initialization, id can be NULL */
  4867. if (!id)
  4868. return;
  4869. BUG_ON(!ss->use_id);
  4870. rcu_assign_pointer(id->css, NULL);
  4871. rcu_assign_pointer(css->id, NULL);
  4872. spin_lock(&ss->id_lock);
  4873. idr_remove(&ss->idr, id->id);
  4874. spin_unlock(&ss->id_lock);
  4875. kfree_rcu(id, rcu_head);
  4876. }
  4877. EXPORT_SYMBOL_GPL(free_css_id);
  4878. /*
  4879. * This is called by init or create(). Then, calls to this function are
  4880. * always serialized (By cgroup_mutex() at create()).
  4881. */
  4882. static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
  4883. {
  4884. struct css_id *newid;
  4885. int ret, size;
  4886. BUG_ON(!ss->use_id);
  4887. size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
  4888. newid = kzalloc(size, GFP_KERNEL);
  4889. if (!newid)
  4890. return ERR_PTR(-ENOMEM);
  4891. idr_preload(GFP_KERNEL);
  4892. spin_lock(&ss->id_lock);
  4893. /* Don't use 0. allocates an ID of 1-65535 */
  4894. ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
  4895. spin_unlock(&ss->id_lock);
  4896. idr_preload_end();
  4897. /* Returns error when there are no free spaces for new ID.*/
  4898. if (ret < 0)
  4899. goto err_out;
  4900. newid->id = ret;
  4901. newid->depth = depth;
  4902. return newid;
  4903. err_out:
  4904. kfree(newid);
  4905. return ERR_PTR(ret);
  4906. }
  4907. static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
  4908. struct cgroup_subsys_state *rootcss)
  4909. {
  4910. struct css_id *newid;
  4911. spin_lock_init(&ss->id_lock);
  4912. idr_init(&ss->idr);
  4913. newid = get_new_cssid(ss, 0);
  4914. if (IS_ERR(newid))
  4915. return PTR_ERR(newid);
  4916. newid->stack[0] = newid->id;
  4917. RCU_INIT_POINTER(newid->css, rootcss);
  4918. RCU_INIT_POINTER(rootcss->id, newid);
  4919. return 0;
  4920. }
  4921. static int alloc_css_id(struct cgroup_subsys_state *child_css)
  4922. {
  4923. struct cgroup_subsys_state *parent_css = css_parent(child_css);
  4924. struct css_id *child_id, *parent_id;
  4925. int i, depth;
  4926. parent_id = rcu_dereference_protected(parent_css->id, true);
  4927. depth = parent_id->depth + 1;
  4928. child_id = get_new_cssid(child_css->ss, depth);
  4929. if (IS_ERR(child_id))
  4930. return PTR_ERR(child_id);
  4931. for (i = 0; i < depth; i++)
  4932. child_id->stack[i] = parent_id->stack[i];
  4933. child_id->stack[depth] = child_id->id;
  4934. /*
  4935. * child_id->css pointer will be set after this cgroup is available
  4936. * see cgroup_populate_dir()
  4937. */
  4938. rcu_assign_pointer(child_css->id, child_id);
  4939. return 0;
  4940. }
  4941. /**
  4942. * css_lookup - lookup css by id
  4943. * @ss: cgroup subsys to be looked into.
  4944. * @id: the id
  4945. *
  4946. * Returns pointer to cgroup_subsys_state if there is valid one with id.
  4947. * NULL if not. Should be called under rcu_read_lock()
  4948. */
  4949. struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
  4950. {
  4951. struct css_id *cssid = NULL;
  4952. BUG_ON(!ss->use_id);
  4953. cssid = idr_find(&ss->idr, id);
  4954. if (unlikely(!cssid))
  4955. return NULL;
  4956. return rcu_dereference(cssid->css);
  4957. }
  4958. EXPORT_SYMBOL_GPL(css_lookup);
  4959. /**
  4960. * cgroup_css_from_dir - get corresponding css from file open on cgroup dir
  4961. * @f: directory file of interest
  4962. * @id: subsystem id of interest
  4963. *
  4964. * Must be called under RCU read lock. The caller is responsible for
  4965. * pinning the returned css if it needs to be accessed outside the RCU
  4966. * critical section.
  4967. */
  4968. struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
  4969. {
  4970. struct cgroup *cgrp;
  4971. struct inode *inode;
  4972. struct cgroup_subsys_state *css;
  4973. WARN_ON_ONCE(!rcu_read_lock_held());
  4974. inode = file_inode(f);
  4975. /* check in cgroup filesystem dir */
  4976. if (inode->i_op != &cgroup_dir_inode_operations)
  4977. return ERR_PTR(-EBADF);
  4978. if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
  4979. return ERR_PTR(-EINVAL);
  4980. /* get cgroup */
  4981. cgrp = __d_cgrp(f->f_dentry);
  4982. css = cgroup_css(cgrp, id);
  4983. return css ? css : ERR_PTR(-ENOENT);
  4984. }
  4985. /**
  4986. * css_from_id - lookup css by id
  4987. * @id: the cgroup id
  4988. * @ss: cgroup subsys to be looked into
  4989. *
  4990. * Returns the css if there's valid one with @id, otherwise returns NULL.
  4991. * Should be called under rcu_read_lock().
  4992. */
  4993. struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
  4994. {
  4995. struct cgroup *cgrp;
  4996. rcu_lockdep_assert(rcu_read_lock_held() ||
  4997. lockdep_is_held(&cgroup_mutex),
  4998. "css_from_id() needs proper protection");
  4999. cgrp = idr_find(&ss->root->cgroup_idr, id);
  5000. if (cgrp)
  5001. return cgroup_css(cgrp, ss->subsys_id);
  5002. return NULL;
  5003. }
  5004. #ifdef CONFIG_CGROUP_DEBUG
  5005. static struct cgroup_subsys_state *
  5006. debug_css_alloc(struct cgroup_subsys_state *parent_css)
  5007. {
  5008. struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
  5009. if (!css)
  5010. return ERR_PTR(-ENOMEM);
  5011. return css;
  5012. }
  5013. static void debug_css_free(struct cgroup_subsys_state *css)
  5014. {
  5015. kfree(css);
  5016. }
  5017. static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
  5018. struct cftype *cft)
  5019. {
  5020. return cgroup_task_count(css->cgroup);
  5021. }
  5022. static u64 current_css_set_read(struct cgroup_subsys_state *css,
  5023. struct cftype *cft)
  5024. {
  5025. return (u64)(unsigned long)current->cgroups;
  5026. }
  5027. static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
  5028. struct cftype *cft)
  5029. {
  5030. u64 count;
  5031. rcu_read_lock();
  5032. count = atomic_read(&task_css_set(current)->refcount);
  5033. rcu_read_unlock();
  5034. return count;
  5035. }
  5036. static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
  5037. struct cftype *cft,
  5038. struct seq_file *seq)
  5039. {
  5040. struct cgrp_cset_link *link;
  5041. struct css_set *cset;
  5042. read_lock(&css_set_lock);
  5043. rcu_read_lock();
  5044. cset = rcu_dereference(current->cgroups);
  5045. list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  5046. struct cgroup *c = link->cgrp;
  5047. const char *name;
  5048. if (c->dentry)
  5049. name = c->dentry->d_name.name;
  5050. else
  5051. name = "?";
  5052. seq_printf(seq, "Root %d group %s\n",
  5053. c->root->hierarchy_id, name);
  5054. }
  5055. rcu_read_unlock();
  5056. read_unlock(&css_set_lock);
  5057. return 0;
  5058. }
  5059. #define MAX_TASKS_SHOWN_PER_CSS 25
  5060. static int cgroup_css_links_read(struct cgroup_subsys_state *css,
  5061. struct cftype *cft, struct seq_file *seq)
  5062. {
  5063. struct cgrp_cset_link *link;
  5064. read_lock(&css_set_lock);
  5065. list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
  5066. struct css_set *cset = link->cset;
  5067. struct task_struct *task;
  5068. int count = 0;
  5069. seq_printf(seq, "css_set %p\n", cset);
  5070. list_for_each_entry(task, &cset->tasks, cg_list) {
  5071. if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
  5072. seq_puts(seq, " ...\n");
  5073. break;
  5074. } else {
  5075. seq_printf(seq, " task %d\n",
  5076. task_pid_vnr(task));
  5077. }
  5078. }
  5079. }
  5080. read_unlock(&css_set_lock);
  5081. return 0;
  5082. }
  5083. static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
  5084. {
  5085. return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
  5086. }
  5087. static struct cftype debug_files[] = {
  5088. {
  5089. .name = "taskcount",
  5090. .read_u64 = debug_taskcount_read,
  5091. },
  5092. {
  5093. .name = "current_css_set",
  5094. .read_u64 = current_css_set_read,
  5095. },
  5096. {
  5097. .name = "current_css_set_refcount",
  5098. .read_u64 = current_css_set_refcount_read,
  5099. },
  5100. {
  5101. .name = "current_css_set_cg_links",
  5102. .read_seq_string = current_css_set_cg_links_read,
  5103. },
  5104. {
  5105. .name = "cgroup_css_links",
  5106. .read_seq_string = cgroup_css_links_read,
  5107. },
  5108. {
  5109. .name = "releasable",
  5110. .read_u64 = releasable_read,
  5111. },
  5112. { } /* terminate */
  5113. };
  5114. struct cgroup_subsys debug_subsys = {
  5115. .name = "debug",
  5116. .css_alloc = debug_css_alloc,
  5117. .css_free = debug_css_free,
  5118. .subsys_id = debug_subsys_id,
  5119. .base_cftypes = debug_files,
  5120. };
  5121. #endif /* CONFIG_CGROUP_DEBUG */