cgroup.c 152 KB

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