cgroup.c 152 KB

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