cgroup.c 150 KB

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