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