cgroup.c 151 KB

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