cgroup.c 152 KB

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