cgroup.c 147 KB

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