cgroup.c 146 KB

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