cgroup.c 153 KB

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