cgroup.c 153 KB

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