cgroup.c 152 KB

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