cgroup.c 142 KB

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