cgroup.c 146 KB

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