cgroup.c 155 KB

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