cgroup.c 157 KB

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