cgroup.c 158 KB

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