cgroup.c 147 KB

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