cgroup.c 152 KB

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