cgroup.c 130 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825
  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/magic.h>
  36. #include <linux/mm.h>
  37. #include <linux/mutex.h>
  38. #include <linux/mount.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/rcupdate.h>
  42. #include <linux/sched.h>
  43. #include <linux/slab.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/rwsem.h>
  46. #include <linux/string.h>
  47. #include <linux/sort.h>
  48. #include <linux/kmod.h>
  49. #include <linux/delayacct.h>
  50. #include <linux/cgroupstats.h>
  51. #include <linux/hashtable.h>
  52. #include <linux/pid_namespace.h>
  53. #include <linux/idr.h>
  54. #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
  55. #include <linux/kthread.h>
  56. #include <linux/delay.h>
  57. #include <linux/atomic.h>
  58. /*
  59. * pidlists linger the following amount before being destroyed. The goal
  60. * is avoiding frequent destruction in the middle of consecutive read calls
  61. * Expiring in the middle is a performance problem not a correctness one.
  62. * 1 sec should be enough.
  63. */
  64. #define CGROUP_PIDLIST_DESTROY_DELAY HZ
  65. #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
  66. MAX_CFTYPE_NAME + 2)
  67. /*
  68. * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
  69. * creation/removal and hierarchy changing operations including cgroup
  70. * creation, removal, css association and controller rebinding. This outer
  71. * lock is needed mainly to resolve the circular dependency between kernfs
  72. * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
  73. */
  74. static DEFINE_MUTEX(cgroup_tree_mutex);
  75. /*
  76. * cgroup_mutex is the master lock. Any modification to cgroup or its
  77. * hierarchy must be performed while holding it.
  78. *
  79. * css_set_rwsem protects task->cgroups pointer, the list of css_set
  80. * objects, and the chain of tasks off each css_set.
  81. *
  82. * These locks are exported if CONFIG_PROVE_RCU so that accessors in
  83. * cgroup.h can use them for lockdep annotations.
  84. */
  85. #ifdef CONFIG_PROVE_RCU
  86. DEFINE_MUTEX(cgroup_mutex);
  87. DECLARE_RWSEM(css_set_rwsem);
  88. EXPORT_SYMBOL_GPL(cgroup_mutex);
  89. EXPORT_SYMBOL_GPL(css_set_rwsem);
  90. #else
  91. static DEFINE_MUTEX(cgroup_mutex);
  92. static DECLARE_RWSEM(css_set_rwsem);
  93. #endif
  94. /*
  95. * Protects cgroup_subsys->release_agent_path. Modifying it also requires
  96. * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
  97. */
  98. static DEFINE_SPINLOCK(release_agent_path_lock);
  99. #define cgroup_assert_mutexes_or_rcu_locked() \
  100. rcu_lockdep_assert(rcu_read_lock_held() || \
  101. lockdep_is_held(&cgroup_tree_mutex) || \
  102. lockdep_is_held(&cgroup_mutex), \
  103. "cgroup_[tree_]mutex or RCU read lock required");
  104. /*
  105. * cgroup destruction makes heavy use of work items and there can be a lot
  106. * of concurrent destructions. Use a separate workqueue so that cgroup
  107. * destruction work items don't end up filling up max_active of system_wq
  108. * which may lead to deadlock.
  109. */
  110. static struct workqueue_struct *cgroup_destroy_wq;
  111. /*
  112. * pidlist destructions need to be flushed on cgroup destruction. Use a
  113. * separate workqueue as flush domain.
  114. */
  115. static struct workqueue_struct *cgroup_pidlist_destroy_wq;
  116. /* generate an array of cgroup subsystem pointers */
  117. #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
  118. static struct cgroup_subsys *cgroup_subsys[] = {
  119. #include <linux/cgroup_subsys.h>
  120. };
  121. #undef SUBSYS
  122. /* array of cgroup subsystem names */
  123. #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
  124. static const char *cgroup_subsys_name[] = {
  125. #include <linux/cgroup_subsys.h>
  126. };
  127. #undef SUBSYS
  128. /*
  129. * The default hierarchy, reserved for the subsystems that are otherwise
  130. * unattached - it never has more than a single cgroup, and all tasks are
  131. * part of that cgroup.
  132. */
  133. struct cgroup_root cgrp_dfl_root;
  134. /*
  135. * The default hierarchy always exists but is hidden until mounted for the
  136. * first time. This is for backward compatibility.
  137. */
  138. static bool cgrp_dfl_root_visible;
  139. /* The list of hierarchy roots */
  140. static LIST_HEAD(cgroup_roots);
  141. static int cgroup_root_count;
  142. /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
  143. static DEFINE_IDR(cgroup_hierarchy_idr);
  144. /*
  145. * Assign a monotonically increasing serial number to cgroups. It
  146. * guarantees cgroups with bigger numbers are newer than those with smaller
  147. * numbers. Also, as cgroups are always appended to the parent's
  148. * ->children list, it guarantees that sibling cgroups are always sorted in
  149. * the ascending serial number order on the list. Protected by
  150. * cgroup_mutex.
  151. */
  152. static u64 cgroup_serial_nr_next = 1;
  153. /* This flag indicates whether tasks in the fork and exit paths should
  154. * check for fork/exit handlers to call. This avoids us having to do
  155. * extra work in the fork/exit path if none of the subsystems need to
  156. * be called.
  157. */
  158. static int need_forkexit_callback __read_mostly;
  159. static struct cftype cgroup_base_files[];
  160. static void cgroup_put(struct cgroup *cgrp);
  161. static int rebind_subsystems(struct cgroup_root *dst_root,
  162. unsigned long ss_mask);
  163. static void cgroup_destroy_css_killed(struct cgroup *cgrp);
  164. static int cgroup_destroy_locked(struct cgroup *cgrp);
  165. static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
  166. bool is_add);
  167. static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
  168. /**
  169. * cgroup_css - obtain a cgroup's css for the specified subsystem
  170. * @cgrp: the cgroup of interest
  171. * @ss: the subsystem of interest (%NULL returns the dummy_css)
  172. *
  173. * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
  174. * function must be called either under cgroup_mutex or rcu_read_lock() and
  175. * the caller is responsible for pinning the returned css if it wants to
  176. * keep accessing it outside the said locks. This function may return
  177. * %NULL if @cgrp doesn't have @subsys_id enabled.
  178. */
  179. static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
  180. struct cgroup_subsys *ss)
  181. {
  182. if (ss)
  183. return rcu_dereference_check(cgrp->subsys[ss->id],
  184. lockdep_is_held(&cgroup_tree_mutex) ||
  185. lockdep_is_held(&cgroup_mutex));
  186. else
  187. return &cgrp->dummy_css;
  188. }
  189. /* convenient tests for these bits */
  190. static inline bool cgroup_is_dead(const struct cgroup *cgrp)
  191. {
  192. return test_bit(CGRP_DEAD, &cgrp->flags);
  193. }
  194. struct cgroup_subsys_state *seq_css(struct seq_file *seq)
  195. {
  196. struct kernfs_open_file *of = seq->private;
  197. struct cgroup *cgrp = of->kn->parent->priv;
  198. struct cftype *cft = seq_cft(seq);
  199. /*
  200. * This is open and unprotected implementation of cgroup_css().
  201. * seq_css() is only called from a kernfs file operation which has
  202. * an active reference on the file. Because all the subsystem
  203. * files are drained before a css is disassociated with a cgroup,
  204. * the matching css from the cgroup's subsys table is guaranteed to
  205. * be and stay valid until the enclosing operation is complete.
  206. */
  207. if (cft->ss)
  208. return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
  209. else
  210. return &cgrp->dummy_css;
  211. }
  212. EXPORT_SYMBOL_GPL(seq_css);
  213. /**
  214. * cgroup_is_descendant - test ancestry
  215. * @cgrp: the cgroup to be tested
  216. * @ancestor: possible ancestor of @cgrp
  217. *
  218. * Test whether @cgrp is a descendant of @ancestor. It also returns %true
  219. * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
  220. * and @ancestor are accessible.
  221. */
  222. bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
  223. {
  224. while (cgrp) {
  225. if (cgrp == ancestor)
  226. return true;
  227. cgrp = cgrp->parent;
  228. }
  229. return false;
  230. }
  231. static int cgroup_is_releasable(const struct cgroup *cgrp)
  232. {
  233. const int bits =
  234. (1 << CGRP_RELEASABLE) |
  235. (1 << CGRP_NOTIFY_ON_RELEASE);
  236. return (cgrp->flags & bits) == bits;
  237. }
  238. static int notify_on_release(const struct cgroup *cgrp)
  239. {
  240. return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  241. }
  242. /**
  243. * for_each_css - iterate all css's of a cgroup
  244. * @css: the iteration cursor
  245. * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
  246. * @cgrp: the target cgroup to iterate css's of
  247. *
  248. * Should be called under cgroup_mutex.
  249. */
  250. #define for_each_css(css, ssid, cgrp) \
  251. for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
  252. if (!((css) = rcu_dereference_check( \
  253. (cgrp)->subsys[(ssid)], \
  254. lockdep_is_held(&cgroup_tree_mutex) || \
  255. lockdep_is_held(&cgroup_mutex)))) { } \
  256. else
  257. /**
  258. * for_each_subsys - iterate all enabled cgroup subsystems
  259. * @ss: the iteration cursor
  260. * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
  261. */
  262. #define for_each_subsys(ss, ssid) \
  263. for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
  264. (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
  265. /* iterate across the hierarchies */
  266. #define for_each_root(root) \
  267. list_for_each_entry((root), &cgroup_roots, root_list)
  268. /**
  269. * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
  270. * @cgrp: the cgroup to be checked for liveness
  271. *
  272. * On success, returns true; the mutex should be later unlocked. On
  273. * failure returns false with no lock held.
  274. */
  275. static bool cgroup_lock_live_group(struct cgroup *cgrp)
  276. {
  277. mutex_lock(&cgroup_mutex);
  278. if (cgroup_is_dead(cgrp)) {
  279. mutex_unlock(&cgroup_mutex);
  280. return false;
  281. }
  282. return true;
  283. }
  284. /* the list of cgroups eligible for automatic release. Protected by
  285. * release_list_lock */
  286. static LIST_HEAD(release_list);
  287. static DEFINE_RAW_SPINLOCK(release_list_lock);
  288. static void cgroup_release_agent(struct work_struct *work);
  289. static DECLARE_WORK(release_agent_work, cgroup_release_agent);
  290. static void check_for_release(struct cgroup *cgrp);
  291. /*
  292. * A cgroup can be associated with multiple css_sets as different tasks may
  293. * belong to different cgroups on different hierarchies. In the other
  294. * direction, a css_set is naturally associated with multiple cgroups.
  295. * This M:N relationship is represented by the following link structure
  296. * which exists for each association and allows traversing the associations
  297. * from both sides.
  298. */
  299. struct cgrp_cset_link {
  300. /* the cgroup and css_set this link associates */
  301. struct cgroup *cgrp;
  302. struct css_set *cset;
  303. /* list of cgrp_cset_links anchored at cgrp->cset_links */
  304. struct list_head cset_link;
  305. /* list of cgrp_cset_links anchored at css_set->cgrp_links */
  306. struct list_head cgrp_link;
  307. };
  308. /*
  309. * The default css_set - used by init and its children prior to any
  310. * hierarchies being mounted. It contains a pointer to the root state
  311. * for each subsystem. Also used to anchor the list of css_sets. Not
  312. * reference-counted, to improve performance when child cgroups
  313. * haven't been created.
  314. */
  315. struct css_set init_css_set = {
  316. .refcount = ATOMIC_INIT(1),
  317. .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
  318. .tasks = LIST_HEAD_INIT(init_css_set.tasks),
  319. .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
  320. .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
  321. .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
  322. };
  323. static int css_set_count = 1; /* 1 for init_css_set */
  324. /*
  325. * hash table for cgroup groups. This improves the performance to find
  326. * an existing css_set. This hash doesn't (currently) take into
  327. * account cgroups in empty hierarchies.
  328. */
  329. #define CSS_SET_HASH_BITS 7
  330. static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
  331. static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
  332. {
  333. unsigned long key = 0UL;
  334. struct cgroup_subsys *ss;
  335. int i;
  336. for_each_subsys(ss, i)
  337. key += (unsigned long)css[i];
  338. key = (key >> 16) ^ key;
  339. return key;
  340. }
  341. static void put_css_set_locked(struct css_set *cset, bool taskexit)
  342. {
  343. struct cgrp_cset_link *link, *tmp_link;
  344. lockdep_assert_held(&css_set_rwsem);
  345. if (!atomic_dec_and_test(&cset->refcount))
  346. return;
  347. /* This css_set is dead. unlink it and release cgroup refcounts */
  348. hash_del(&cset->hlist);
  349. css_set_count--;
  350. list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
  351. struct cgroup *cgrp = link->cgrp;
  352. list_del(&link->cset_link);
  353. list_del(&link->cgrp_link);
  354. /* @cgrp can't go away while we're holding css_set_rwsem */
  355. if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
  356. if (taskexit)
  357. set_bit(CGRP_RELEASABLE, &cgrp->flags);
  358. check_for_release(cgrp);
  359. }
  360. kfree(link);
  361. }
  362. kfree_rcu(cset, rcu_head);
  363. }
  364. static void put_css_set(struct css_set *cset, bool taskexit)
  365. {
  366. /*
  367. * Ensure that the refcount doesn't hit zero while any readers
  368. * can see it. Similar to atomic_dec_and_lock(), but for an
  369. * rwlock
  370. */
  371. if (atomic_add_unless(&cset->refcount, -1, 1))
  372. return;
  373. down_write(&css_set_rwsem);
  374. put_css_set_locked(cset, taskexit);
  375. up_write(&css_set_rwsem);
  376. }
  377. /*
  378. * refcounted get/put for css_set objects
  379. */
  380. static inline void get_css_set(struct css_set *cset)
  381. {
  382. atomic_inc(&cset->refcount);
  383. }
  384. /**
  385. * compare_css_sets - helper function for find_existing_css_set().
  386. * @cset: candidate css_set being tested
  387. * @old_cset: existing css_set for a task
  388. * @new_cgrp: cgroup that's being entered by the task
  389. * @template: desired set of css pointers in css_set (pre-calculated)
  390. *
  391. * Returns true if "cset" matches "old_cset" except for the hierarchy
  392. * which "new_cgrp" belongs to, for which it should match "new_cgrp".
  393. */
  394. static bool compare_css_sets(struct css_set *cset,
  395. struct css_set *old_cset,
  396. struct cgroup *new_cgrp,
  397. struct cgroup_subsys_state *template[])
  398. {
  399. struct list_head *l1, *l2;
  400. if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
  401. /* Not all subsystems matched */
  402. return false;
  403. }
  404. /*
  405. * Compare cgroup pointers in order to distinguish between
  406. * different cgroups in heirarchies with no subsystems. We
  407. * could get by with just this check alone (and skip the
  408. * memcmp above) but on most setups the memcmp check will
  409. * avoid the need for this more expensive check on almost all
  410. * candidates.
  411. */
  412. l1 = &cset->cgrp_links;
  413. l2 = &old_cset->cgrp_links;
  414. while (1) {
  415. struct cgrp_cset_link *link1, *link2;
  416. struct cgroup *cgrp1, *cgrp2;
  417. l1 = l1->next;
  418. l2 = l2->next;
  419. /* See if we reached the end - both lists are equal length. */
  420. if (l1 == &cset->cgrp_links) {
  421. BUG_ON(l2 != &old_cset->cgrp_links);
  422. break;
  423. } else {
  424. BUG_ON(l2 == &old_cset->cgrp_links);
  425. }
  426. /* Locate the cgroups associated with these links. */
  427. link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
  428. link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
  429. cgrp1 = link1->cgrp;
  430. cgrp2 = link2->cgrp;
  431. /* Hierarchies should be linked in the same order. */
  432. BUG_ON(cgrp1->root != cgrp2->root);
  433. /*
  434. * If this hierarchy is the hierarchy of the cgroup
  435. * that's changing, then we need to check that this
  436. * css_set points to the new cgroup; if it's any other
  437. * hierarchy, then this css_set should point to the
  438. * same cgroup as the old css_set.
  439. */
  440. if (cgrp1->root == new_cgrp->root) {
  441. if (cgrp1 != new_cgrp)
  442. return false;
  443. } else {
  444. if (cgrp1 != cgrp2)
  445. return false;
  446. }
  447. }
  448. return true;
  449. }
  450. /**
  451. * find_existing_css_set - init css array and find the matching css_set
  452. * @old_cset: the css_set that we're using before the cgroup transition
  453. * @cgrp: the cgroup that we're moving into
  454. * @template: out param for the new set of csses, should be clear on entry
  455. */
  456. static struct css_set *find_existing_css_set(struct css_set *old_cset,
  457. struct cgroup *cgrp,
  458. struct cgroup_subsys_state *template[])
  459. {
  460. struct cgroup_root *root = cgrp->root;
  461. struct cgroup_subsys *ss;
  462. struct css_set *cset;
  463. unsigned long key;
  464. int i;
  465. /*
  466. * Build the set of subsystem state objects that we want to see in the
  467. * new css_set. while subsystems can change globally, the entries here
  468. * won't change, so no need for locking.
  469. */
  470. for_each_subsys(ss, i) {
  471. if (root->cgrp.subsys_mask & (1UL << i)) {
  472. /* Subsystem is in this hierarchy. So we want
  473. * the subsystem state from the new
  474. * cgroup */
  475. template[i] = cgroup_css(cgrp, ss);
  476. } else {
  477. /* Subsystem is not in this hierarchy, so we
  478. * don't want to change the subsystem state */
  479. template[i] = old_cset->subsys[i];
  480. }
  481. }
  482. key = css_set_hash(template);
  483. hash_for_each_possible(css_set_table, cset, hlist, key) {
  484. if (!compare_css_sets(cset, old_cset, cgrp, template))
  485. continue;
  486. /* This css_set matches what we need */
  487. return cset;
  488. }
  489. /* No existing cgroup group matched */
  490. return NULL;
  491. }
  492. static void free_cgrp_cset_links(struct list_head *links_to_free)
  493. {
  494. struct cgrp_cset_link *link, *tmp_link;
  495. list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
  496. list_del(&link->cset_link);
  497. kfree(link);
  498. }
  499. }
  500. /**
  501. * allocate_cgrp_cset_links - allocate cgrp_cset_links
  502. * @count: the number of links to allocate
  503. * @tmp_links: list_head the allocated links are put on
  504. *
  505. * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
  506. * through ->cset_link. Returns 0 on success or -errno.
  507. */
  508. static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
  509. {
  510. struct cgrp_cset_link *link;
  511. int i;
  512. INIT_LIST_HEAD(tmp_links);
  513. for (i = 0; i < count; i++) {
  514. link = kzalloc(sizeof(*link), GFP_KERNEL);
  515. if (!link) {
  516. free_cgrp_cset_links(tmp_links);
  517. return -ENOMEM;
  518. }
  519. list_add(&link->cset_link, tmp_links);
  520. }
  521. return 0;
  522. }
  523. /**
  524. * link_css_set - a helper function to link a css_set to a cgroup
  525. * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
  526. * @cset: the css_set to be linked
  527. * @cgrp: the destination cgroup
  528. */
  529. static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
  530. struct cgroup *cgrp)
  531. {
  532. struct cgrp_cset_link *link;
  533. BUG_ON(list_empty(tmp_links));
  534. link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
  535. link->cset = cset;
  536. link->cgrp = cgrp;
  537. list_move(&link->cset_link, &cgrp->cset_links);
  538. /*
  539. * Always add links to the tail of the list so that the list
  540. * is sorted by order of hierarchy creation
  541. */
  542. list_add_tail(&link->cgrp_link, &cset->cgrp_links);
  543. }
  544. /**
  545. * find_css_set - return a new css_set with one cgroup updated
  546. * @old_cset: the baseline css_set
  547. * @cgrp: the cgroup to be updated
  548. *
  549. * Return a new css_set that's equivalent to @old_cset, but with @cgrp
  550. * substituted into the appropriate hierarchy.
  551. */
  552. static struct css_set *find_css_set(struct css_set *old_cset,
  553. struct cgroup *cgrp)
  554. {
  555. struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
  556. struct css_set *cset;
  557. struct list_head tmp_links;
  558. struct cgrp_cset_link *link;
  559. unsigned long key;
  560. lockdep_assert_held(&cgroup_mutex);
  561. /* First see if we already have a cgroup group that matches
  562. * the desired set */
  563. down_read(&css_set_rwsem);
  564. cset = find_existing_css_set(old_cset, cgrp, template);
  565. if (cset)
  566. get_css_set(cset);
  567. up_read(&css_set_rwsem);
  568. if (cset)
  569. return cset;
  570. cset = kzalloc(sizeof(*cset), GFP_KERNEL);
  571. if (!cset)
  572. return NULL;
  573. /* Allocate all the cgrp_cset_link objects that we'll need */
  574. if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
  575. kfree(cset);
  576. return NULL;
  577. }
  578. atomic_set(&cset->refcount, 1);
  579. INIT_LIST_HEAD(&cset->cgrp_links);
  580. INIT_LIST_HEAD(&cset->tasks);
  581. INIT_LIST_HEAD(&cset->mg_tasks);
  582. INIT_LIST_HEAD(&cset->mg_preload_node);
  583. INIT_LIST_HEAD(&cset->mg_node);
  584. INIT_HLIST_NODE(&cset->hlist);
  585. /* Copy the set of subsystem state objects generated in
  586. * find_existing_css_set() */
  587. memcpy(cset->subsys, template, sizeof(cset->subsys));
  588. down_write(&css_set_rwsem);
  589. /* Add reference counts and links from the new css_set. */
  590. list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
  591. struct cgroup *c = link->cgrp;
  592. if (c->root == cgrp->root)
  593. c = cgrp;
  594. link_css_set(&tmp_links, cset, c);
  595. }
  596. BUG_ON(!list_empty(&tmp_links));
  597. css_set_count++;
  598. /* Add this cgroup group to the hash table */
  599. key = css_set_hash(cset->subsys);
  600. hash_add(css_set_table, &cset->hlist, key);
  601. up_write(&css_set_rwsem);
  602. return cset;
  603. }
  604. static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
  605. {
  606. struct cgroup *root_cgrp = kf_root->kn->priv;
  607. return root_cgrp->root;
  608. }
  609. static int cgroup_init_root_id(struct cgroup_root *root)
  610. {
  611. int id;
  612. lockdep_assert_held(&cgroup_mutex);
  613. id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
  614. if (id < 0)
  615. return id;
  616. root->hierarchy_id = id;
  617. return 0;
  618. }
  619. static void cgroup_exit_root_id(struct cgroup_root *root)
  620. {
  621. lockdep_assert_held(&cgroup_mutex);
  622. if (root->hierarchy_id) {
  623. idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
  624. root->hierarchy_id = 0;
  625. }
  626. }
  627. static void cgroup_free_root(struct cgroup_root *root)
  628. {
  629. if (root) {
  630. /* hierarhcy ID shoulid already have been released */
  631. WARN_ON_ONCE(root->hierarchy_id);
  632. idr_destroy(&root->cgroup_idr);
  633. kfree(root);
  634. }
  635. }
  636. static void cgroup_destroy_root(struct cgroup_root *root)
  637. {
  638. struct cgroup *cgrp = &root->cgrp;
  639. struct cgrp_cset_link *link, *tmp_link;
  640. mutex_lock(&cgroup_tree_mutex);
  641. mutex_lock(&cgroup_mutex);
  642. BUG_ON(atomic_read(&root->nr_cgrps));
  643. BUG_ON(!list_empty(&cgrp->children));
  644. /* Rebind all subsystems back to the default hierarchy */
  645. rebind_subsystems(&cgrp_dfl_root, cgrp->subsys_mask);
  646. /*
  647. * Release all the links from cset_links to this hierarchy's
  648. * root cgroup
  649. */
  650. down_write(&css_set_rwsem);
  651. list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
  652. list_del(&link->cset_link);
  653. list_del(&link->cgrp_link);
  654. kfree(link);
  655. }
  656. up_write(&css_set_rwsem);
  657. if (!list_empty(&root->root_list)) {
  658. list_del(&root->root_list);
  659. cgroup_root_count--;
  660. }
  661. cgroup_exit_root_id(root);
  662. mutex_unlock(&cgroup_mutex);
  663. mutex_unlock(&cgroup_tree_mutex);
  664. kernfs_destroy_root(root->kf_root);
  665. cgroup_free_root(root);
  666. }
  667. /* look up cgroup associated with given css_set on the specified hierarchy */
  668. static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
  669. struct cgroup_root *root)
  670. {
  671. struct cgroup *res = NULL;
  672. lockdep_assert_held(&cgroup_mutex);
  673. lockdep_assert_held(&css_set_rwsem);
  674. if (cset == &init_css_set) {
  675. res = &root->cgrp;
  676. } else {
  677. struct cgrp_cset_link *link;
  678. list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  679. struct cgroup *c = link->cgrp;
  680. if (c->root == root) {
  681. res = c;
  682. break;
  683. }
  684. }
  685. }
  686. BUG_ON(!res);
  687. return res;
  688. }
  689. /*
  690. * Return the cgroup for "task" from the given hierarchy. Must be
  691. * called with cgroup_mutex and css_set_rwsem held.
  692. */
  693. static struct cgroup *task_cgroup_from_root(struct task_struct *task,
  694. struct cgroup_root *root)
  695. {
  696. /*
  697. * No need to lock the task - since we hold cgroup_mutex the
  698. * task can't change groups, so the only thing that can happen
  699. * is that it exits and its css is set back to init_css_set.
  700. */
  701. return cset_cgroup_from_root(task_css_set(task), root);
  702. }
  703. /*
  704. * A task must hold cgroup_mutex to modify cgroups.
  705. *
  706. * Any task can increment and decrement the count field without lock.
  707. * So in general, code holding cgroup_mutex can't rely on the count
  708. * field not changing. However, if the count goes to zero, then only
  709. * cgroup_attach_task() can increment it again. Because a count of zero
  710. * means that no tasks are currently attached, therefore there is no
  711. * way a task attached to that cgroup can fork (the other way to
  712. * increment the count). So code holding cgroup_mutex can safely
  713. * assume that if the count is zero, it will stay zero. Similarly, if
  714. * a task holds cgroup_mutex on a cgroup with zero count, it
  715. * knows that the cgroup won't be removed, as cgroup_rmdir()
  716. * needs that mutex.
  717. *
  718. * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
  719. * (usually) take cgroup_mutex. These are the two most performance
  720. * critical pieces of code here. The exception occurs on cgroup_exit(),
  721. * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
  722. * is taken, and if the cgroup count is zero, a usermode call made
  723. * to the release agent with the name of the cgroup (path relative to
  724. * the root of cgroup file system) as the argument.
  725. *
  726. * A cgroup can only be deleted if both its 'count' of using tasks
  727. * is zero, and its list of 'children' cgroups is empty. Since all
  728. * tasks in the system use _some_ cgroup, and since there is always at
  729. * least one task in the system (init, pid == 1), therefore, root cgroup
  730. * always has either children cgroups and/or using tasks. So we don't
  731. * need a special hack to ensure that root cgroup cannot be deleted.
  732. *
  733. * P.S. One more locking exception. RCU is used to guard the
  734. * update of a tasks cgroup pointer by cgroup_attach_task()
  735. */
  736. static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
  737. static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
  738. static const struct file_operations proc_cgroupstats_operations;
  739. static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
  740. char *buf)
  741. {
  742. if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
  743. !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
  744. snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
  745. cft->ss->name, cft->name);
  746. else
  747. strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
  748. return buf;
  749. }
  750. /**
  751. * cgroup_file_mode - deduce file mode of a control file
  752. * @cft: the control file in question
  753. *
  754. * returns cft->mode if ->mode is not 0
  755. * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
  756. * returns S_IRUGO if it has only a read handler
  757. * returns S_IWUSR if it has only a write hander
  758. */
  759. static umode_t cgroup_file_mode(const struct cftype *cft)
  760. {
  761. umode_t mode = 0;
  762. if (cft->mode)
  763. return cft->mode;
  764. if (cft->read_u64 || cft->read_s64 || cft->seq_show)
  765. mode |= S_IRUGO;
  766. if (cft->write_u64 || cft->write_s64 || cft->write_string ||
  767. cft->trigger)
  768. mode |= S_IWUSR;
  769. return mode;
  770. }
  771. static void cgroup_free_fn(struct work_struct *work)
  772. {
  773. struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
  774. atomic_dec(&cgrp->root->nr_cgrps);
  775. cgroup_pidlist_destroy_all(cgrp);
  776. if (cgrp->parent) {
  777. /*
  778. * We get a ref to the parent, and put the ref when this
  779. * cgroup is being freed, so it's guaranteed that the
  780. * parent won't be destroyed before its children.
  781. */
  782. cgroup_put(cgrp->parent);
  783. kernfs_put(cgrp->kn);
  784. kfree(cgrp);
  785. } else {
  786. /*
  787. * This is root cgroup's refcnt reaching zero, which
  788. * indicates that the root should be released.
  789. */
  790. cgroup_destroy_root(cgrp->root);
  791. }
  792. }
  793. static void cgroup_free_rcu(struct rcu_head *head)
  794. {
  795. struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
  796. INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
  797. queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
  798. }
  799. static void cgroup_get(struct cgroup *cgrp)
  800. {
  801. WARN_ON_ONCE(cgroup_is_dead(cgrp));
  802. WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
  803. atomic_inc(&cgrp->refcnt);
  804. }
  805. static void cgroup_put(struct cgroup *cgrp)
  806. {
  807. if (!atomic_dec_and_test(&cgrp->refcnt))
  808. return;
  809. if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
  810. return;
  811. /*
  812. * XXX: cgrp->id is only used to look up css's. As cgroup and
  813. * css's lifetimes will be decoupled, it should be made
  814. * per-subsystem and moved to css->id so that lookups are
  815. * successful until the target css is released.
  816. */
  817. mutex_lock(&cgroup_mutex);
  818. idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
  819. mutex_unlock(&cgroup_mutex);
  820. cgrp->id = -1;
  821. call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
  822. }
  823. static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
  824. {
  825. char name[CGROUP_FILE_NAME_MAX];
  826. lockdep_assert_held(&cgroup_tree_mutex);
  827. kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
  828. }
  829. /**
  830. * cgroup_clear_dir - remove subsys files in a cgroup directory
  831. * @cgrp: target cgroup
  832. * @subsys_mask: mask of the subsystem ids whose files should be removed
  833. */
  834. static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
  835. {
  836. struct cgroup_subsys *ss;
  837. int i;
  838. for_each_subsys(ss, i) {
  839. struct cftype *cfts;
  840. if (!test_bit(i, &subsys_mask))
  841. continue;
  842. list_for_each_entry(cfts, &ss->cfts, node)
  843. cgroup_addrm_files(cgrp, cfts, false);
  844. }
  845. }
  846. static int rebind_subsystems(struct cgroup_root *dst_root,
  847. unsigned long ss_mask)
  848. {
  849. struct cgroup_subsys *ss;
  850. int ssid, ret;
  851. lockdep_assert_held(&cgroup_tree_mutex);
  852. lockdep_assert_held(&cgroup_mutex);
  853. for_each_subsys(ss, ssid) {
  854. if (!(ss_mask & (1 << ssid)))
  855. continue;
  856. /* if @ss is on the dummy_root, we can always move it */
  857. if (ss->root == &cgrp_dfl_root)
  858. continue;
  859. /* if @ss has non-root cgroups attached to it, can't move */
  860. if (!list_empty(&ss->root->cgrp.children))
  861. return -EBUSY;
  862. /* can't move between two non-dummy roots either */
  863. if (dst_root != &cgrp_dfl_root)
  864. return -EBUSY;
  865. }
  866. ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
  867. if (ret) {
  868. if (dst_root != &cgrp_dfl_root)
  869. return ret;
  870. /*
  871. * Rebinding back to the default root is not allowed to
  872. * fail. Using both default and non-default roots should
  873. * be rare. Moving subsystems back and forth even more so.
  874. * Just warn about it and continue.
  875. */
  876. if (cgrp_dfl_root_visible) {
  877. pr_warning("cgroup: failed to create files (%d) while rebinding 0x%lx to default root\n",
  878. ret, ss_mask);
  879. pr_warning("cgroup: you may retry by moving them to a different hierarchy and unbinding\n");
  880. }
  881. }
  882. /*
  883. * Nothing can fail from this point on. Remove files for the
  884. * removed subsystems and rebind each subsystem.
  885. */
  886. mutex_unlock(&cgroup_mutex);
  887. for_each_subsys(ss, ssid)
  888. if (ss_mask & (1 << ssid))
  889. cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
  890. mutex_lock(&cgroup_mutex);
  891. for_each_subsys(ss, ssid) {
  892. struct cgroup_root *src_root;
  893. struct cgroup_subsys_state *css;
  894. if (!(ss_mask & (1 << ssid)))
  895. continue;
  896. src_root = ss->root;
  897. css = cgroup_css(&src_root->cgrp, ss);
  898. WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
  899. RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
  900. rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
  901. ss->root = dst_root;
  902. css->cgroup = &dst_root->cgrp;
  903. src_root->cgrp.subsys_mask &= ~(1 << ssid);
  904. dst_root->cgrp.subsys_mask |= 1 << ssid;
  905. if (ss->bind)
  906. ss->bind(css);
  907. }
  908. kernfs_activate(dst_root->cgrp.kn);
  909. return 0;
  910. }
  911. static int cgroup_show_options(struct seq_file *seq,
  912. struct kernfs_root *kf_root)
  913. {
  914. struct cgroup_root *root = cgroup_root_from_kf(kf_root);
  915. struct cgroup_subsys *ss;
  916. int ssid;
  917. for_each_subsys(ss, ssid)
  918. if (root->cgrp.subsys_mask & (1 << ssid))
  919. seq_printf(seq, ",%s", ss->name);
  920. if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
  921. seq_puts(seq, ",sane_behavior");
  922. if (root->flags & CGRP_ROOT_NOPREFIX)
  923. seq_puts(seq, ",noprefix");
  924. if (root->flags & CGRP_ROOT_XATTR)
  925. seq_puts(seq, ",xattr");
  926. spin_lock(&release_agent_path_lock);
  927. if (strlen(root->release_agent_path))
  928. seq_printf(seq, ",release_agent=%s", root->release_agent_path);
  929. spin_unlock(&release_agent_path_lock);
  930. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
  931. seq_puts(seq, ",clone_children");
  932. if (strlen(root->name))
  933. seq_printf(seq, ",name=%s", root->name);
  934. return 0;
  935. }
  936. struct cgroup_sb_opts {
  937. unsigned long subsys_mask;
  938. unsigned long flags;
  939. char *release_agent;
  940. bool cpuset_clone_children;
  941. char *name;
  942. /* User explicitly requested empty subsystem */
  943. bool none;
  944. };
  945. /*
  946. * Convert a hierarchy specifier into a bitmask of subsystems and
  947. * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
  948. * array. This function takes refcounts on subsystems to be used, unless it
  949. * returns error, in which case no refcounts are taken.
  950. */
  951. static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
  952. {
  953. char *token, *o = data;
  954. bool all_ss = false, one_ss = false;
  955. unsigned long mask = (unsigned long)-1;
  956. struct cgroup_subsys *ss;
  957. int i;
  958. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  959. #ifdef CONFIG_CPUSETS
  960. mask = ~(1UL << cpuset_cgrp_id);
  961. #endif
  962. memset(opts, 0, sizeof(*opts));
  963. while ((token = strsep(&o, ",")) != NULL) {
  964. if (!*token)
  965. return -EINVAL;
  966. if (!strcmp(token, "none")) {
  967. /* Explicitly have no subsystems */
  968. opts->none = true;
  969. continue;
  970. }
  971. if (!strcmp(token, "all")) {
  972. /* Mutually exclusive option 'all' + subsystem name */
  973. if (one_ss)
  974. return -EINVAL;
  975. all_ss = true;
  976. continue;
  977. }
  978. if (!strcmp(token, "__DEVEL__sane_behavior")) {
  979. opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
  980. continue;
  981. }
  982. if (!strcmp(token, "noprefix")) {
  983. opts->flags |= CGRP_ROOT_NOPREFIX;
  984. continue;
  985. }
  986. if (!strcmp(token, "clone_children")) {
  987. opts->cpuset_clone_children = true;
  988. continue;
  989. }
  990. if (!strcmp(token, "xattr")) {
  991. opts->flags |= CGRP_ROOT_XATTR;
  992. continue;
  993. }
  994. if (!strncmp(token, "release_agent=", 14)) {
  995. /* Specifying two release agents is forbidden */
  996. if (opts->release_agent)
  997. return -EINVAL;
  998. opts->release_agent =
  999. kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
  1000. if (!opts->release_agent)
  1001. return -ENOMEM;
  1002. continue;
  1003. }
  1004. if (!strncmp(token, "name=", 5)) {
  1005. const char *name = token + 5;
  1006. /* Can't specify an empty name */
  1007. if (!strlen(name))
  1008. return -EINVAL;
  1009. /* Must match [\w.-]+ */
  1010. for (i = 0; i < strlen(name); i++) {
  1011. char c = name[i];
  1012. if (isalnum(c))
  1013. continue;
  1014. if ((c == '.') || (c == '-') || (c == '_'))
  1015. continue;
  1016. return -EINVAL;
  1017. }
  1018. /* Specifying two names is forbidden */
  1019. if (opts->name)
  1020. return -EINVAL;
  1021. opts->name = kstrndup(name,
  1022. MAX_CGROUP_ROOT_NAMELEN - 1,
  1023. GFP_KERNEL);
  1024. if (!opts->name)
  1025. return -ENOMEM;
  1026. continue;
  1027. }
  1028. for_each_subsys(ss, i) {
  1029. if (strcmp(token, ss->name))
  1030. continue;
  1031. if (ss->disabled)
  1032. continue;
  1033. /* Mutually exclusive option 'all' + subsystem name */
  1034. if (all_ss)
  1035. return -EINVAL;
  1036. set_bit(i, &opts->subsys_mask);
  1037. one_ss = true;
  1038. break;
  1039. }
  1040. if (i == CGROUP_SUBSYS_COUNT)
  1041. return -ENOENT;
  1042. }
  1043. /* Consistency checks */
  1044. if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
  1045. pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
  1046. if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
  1047. opts->cpuset_clone_children || opts->release_agent ||
  1048. opts->name) {
  1049. pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
  1050. return -EINVAL;
  1051. }
  1052. } else {
  1053. /*
  1054. * If the 'all' option was specified select all the
  1055. * subsystems, otherwise if 'none', 'name=' and a subsystem
  1056. * name options were not specified, let's default to 'all'
  1057. */
  1058. if (all_ss || (!one_ss && !opts->none && !opts->name))
  1059. for_each_subsys(ss, i)
  1060. if (!ss->disabled)
  1061. set_bit(i, &opts->subsys_mask);
  1062. /*
  1063. * We either have to specify by name or by subsystems. (So
  1064. * all empty hierarchies must have a name).
  1065. */
  1066. if (!opts->subsys_mask && !opts->name)
  1067. return -EINVAL;
  1068. }
  1069. /*
  1070. * Option noprefix was introduced just for backward compatibility
  1071. * with the old cpuset, so we allow noprefix only if mounting just
  1072. * the cpuset subsystem.
  1073. */
  1074. if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
  1075. return -EINVAL;
  1076. /* Can't specify "none" and some subsystems */
  1077. if (opts->subsys_mask && opts->none)
  1078. return -EINVAL;
  1079. return 0;
  1080. }
  1081. static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
  1082. {
  1083. int ret = 0;
  1084. struct cgroup_root *root = cgroup_root_from_kf(kf_root);
  1085. struct cgroup_sb_opts opts;
  1086. unsigned long added_mask, removed_mask;
  1087. if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
  1088. pr_err("cgroup: sane_behavior: remount is not allowed\n");
  1089. return -EINVAL;
  1090. }
  1091. mutex_lock(&cgroup_tree_mutex);
  1092. mutex_lock(&cgroup_mutex);
  1093. /* See what subsystems are wanted */
  1094. ret = parse_cgroupfs_options(data, &opts);
  1095. if (ret)
  1096. goto out_unlock;
  1097. if (opts.subsys_mask != root->cgrp.subsys_mask || opts.release_agent)
  1098. pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
  1099. task_tgid_nr(current), current->comm);
  1100. added_mask = opts.subsys_mask & ~root->cgrp.subsys_mask;
  1101. removed_mask = root->cgrp.subsys_mask & ~opts.subsys_mask;
  1102. /* Don't allow flags or name to change at remount */
  1103. if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
  1104. (opts.name && strcmp(opts.name, root->name))) {
  1105. pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
  1106. opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
  1107. root->flags & CGRP_ROOT_OPTION_MASK, root->name);
  1108. ret = -EINVAL;
  1109. goto out_unlock;
  1110. }
  1111. /* remounting is not allowed for populated hierarchies */
  1112. if (!list_empty(&root->cgrp.children)) {
  1113. ret = -EBUSY;
  1114. goto out_unlock;
  1115. }
  1116. ret = rebind_subsystems(root, added_mask);
  1117. if (ret)
  1118. goto out_unlock;
  1119. rebind_subsystems(&cgrp_dfl_root, removed_mask);
  1120. if (opts.release_agent) {
  1121. spin_lock(&release_agent_path_lock);
  1122. strcpy(root->release_agent_path, opts.release_agent);
  1123. spin_unlock(&release_agent_path_lock);
  1124. }
  1125. out_unlock:
  1126. kfree(opts.release_agent);
  1127. kfree(opts.name);
  1128. mutex_unlock(&cgroup_mutex);
  1129. mutex_unlock(&cgroup_tree_mutex);
  1130. return ret;
  1131. }
  1132. /*
  1133. * To reduce the fork() overhead for systems that are not actually using
  1134. * their cgroups capability, we don't maintain the lists running through
  1135. * each css_set to its tasks until we see the list actually used - in other
  1136. * words after the first mount.
  1137. */
  1138. static bool use_task_css_set_links __read_mostly;
  1139. static void cgroup_enable_task_cg_lists(void)
  1140. {
  1141. struct task_struct *p, *g;
  1142. down_write(&css_set_rwsem);
  1143. if (use_task_css_set_links)
  1144. goto out_unlock;
  1145. use_task_css_set_links = true;
  1146. /*
  1147. * We need tasklist_lock because RCU is not safe against
  1148. * while_each_thread(). Besides, a forking task that has passed
  1149. * cgroup_post_fork() without seeing use_task_css_set_links = 1
  1150. * is not guaranteed to have its child immediately visible in the
  1151. * tasklist if we walk through it with RCU.
  1152. */
  1153. read_lock(&tasklist_lock);
  1154. do_each_thread(g, p) {
  1155. WARN_ON_ONCE(!list_empty(&p->cg_list) ||
  1156. task_css_set(p) != &init_css_set);
  1157. /*
  1158. * We should check if the process is exiting, otherwise
  1159. * it will race with cgroup_exit() in that the list
  1160. * entry won't be deleted though the process has exited.
  1161. * Do it while holding siglock so that we don't end up
  1162. * racing against cgroup_exit().
  1163. */
  1164. spin_lock_irq(&p->sighand->siglock);
  1165. if (!(p->flags & PF_EXITING)) {
  1166. struct css_set *cset = task_css_set(p);
  1167. list_add(&p->cg_list, &cset->tasks);
  1168. get_css_set(cset);
  1169. }
  1170. spin_unlock_irq(&p->sighand->siglock);
  1171. } while_each_thread(g, p);
  1172. read_unlock(&tasklist_lock);
  1173. out_unlock:
  1174. up_write(&css_set_rwsem);
  1175. }
  1176. static void init_cgroup_housekeeping(struct cgroup *cgrp)
  1177. {
  1178. atomic_set(&cgrp->refcnt, 1);
  1179. INIT_LIST_HEAD(&cgrp->sibling);
  1180. INIT_LIST_HEAD(&cgrp->children);
  1181. INIT_LIST_HEAD(&cgrp->cset_links);
  1182. INIT_LIST_HEAD(&cgrp->release_list);
  1183. INIT_LIST_HEAD(&cgrp->pidlists);
  1184. mutex_init(&cgrp->pidlist_mutex);
  1185. cgrp->dummy_css.cgroup = cgrp;
  1186. }
  1187. static void init_cgroup_root(struct cgroup_root *root,
  1188. struct cgroup_sb_opts *opts)
  1189. {
  1190. struct cgroup *cgrp = &root->cgrp;
  1191. INIT_LIST_HEAD(&root->root_list);
  1192. atomic_set(&root->nr_cgrps, 1);
  1193. cgrp->root = root;
  1194. init_cgroup_housekeeping(cgrp);
  1195. idr_init(&root->cgroup_idr);
  1196. root->flags = opts->flags;
  1197. if (opts->release_agent)
  1198. strcpy(root->release_agent_path, opts->release_agent);
  1199. if (opts->name)
  1200. strcpy(root->name, opts->name);
  1201. if (opts->cpuset_clone_children)
  1202. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
  1203. }
  1204. static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
  1205. {
  1206. LIST_HEAD(tmp_links);
  1207. struct cgroup *root_cgrp = &root->cgrp;
  1208. struct css_set *cset;
  1209. int i, ret;
  1210. lockdep_assert_held(&cgroup_tree_mutex);
  1211. lockdep_assert_held(&cgroup_mutex);
  1212. ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
  1213. if (ret < 0)
  1214. goto out;
  1215. root_cgrp->id = ret;
  1216. /*
  1217. * We're accessing css_set_count without locking css_set_rwsem here,
  1218. * but that's OK - it can only be increased by someone holding
  1219. * cgroup_lock, and that's us. The worst that can happen is that we
  1220. * have some link structures left over
  1221. */
  1222. ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
  1223. if (ret)
  1224. goto out;
  1225. ret = cgroup_init_root_id(root);
  1226. if (ret)
  1227. goto out;
  1228. root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
  1229. KERNFS_ROOT_CREATE_DEACTIVATED,
  1230. root_cgrp);
  1231. if (IS_ERR(root->kf_root)) {
  1232. ret = PTR_ERR(root->kf_root);
  1233. goto exit_root_id;
  1234. }
  1235. root_cgrp->kn = root->kf_root->kn;
  1236. ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
  1237. if (ret)
  1238. goto destroy_root;
  1239. ret = rebind_subsystems(root, ss_mask);
  1240. if (ret)
  1241. goto destroy_root;
  1242. /*
  1243. * There must be no failure case after here, since rebinding takes
  1244. * care of subsystems' refcounts, which are explicitly dropped in
  1245. * the failure exit path.
  1246. */
  1247. list_add(&root->root_list, &cgroup_roots);
  1248. cgroup_root_count++;
  1249. /*
  1250. * Link the root cgroup in this hierarchy into all the css_set
  1251. * objects.
  1252. */
  1253. down_write(&css_set_rwsem);
  1254. hash_for_each(css_set_table, i, cset, hlist)
  1255. link_css_set(&tmp_links, cset, root_cgrp);
  1256. up_write(&css_set_rwsem);
  1257. BUG_ON(!list_empty(&root_cgrp->children));
  1258. BUG_ON(atomic_read(&root->nr_cgrps) != 1);
  1259. kernfs_activate(root_cgrp->kn);
  1260. ret = 0;
  1261. goto out;
  1262. destroy_root:
  1263. kernfs_destroy_root(root->kf_root);
  1264. root->kf_root = NULL;
  1265. exit_root_id:
  1266. cgroup_exit_root_id(root);
  1267. out:
  1268. free_cgrp_cset_links(&tmp_links);
  1269. return ret;
  1270. }
  1271. static struct dentry *cgroup_mount(struct file_system_type *fs_type,
  1272. int flags, const char *unused_dev_name,
  1273. void *data)
  1274. {
  1275. struct cgroup_root *root;
  1276. struct cgroup_sb_opts opts;
  1277. struct dentry *dentry;
  1278. int ret;
  1279. bool new_sb;
  1280. /*
  1281. * The first time anyone tries to mount a cgroup, enable the list
  1282. * linking each css_set to its tasks and fix up all existing tasks.
  1283. */
  1284. if (!use_task_css_set_links)
  1285. cgroup_enable_task_cg_lists();
  1286. mutex_lock(&cgroup_tree_mutex);
  1287. mutex_lock(&cgroup_mutex);
  1288. /* First find the desired set of subsystems */
  1289. ret = parse_cgroupfs_options(data, &opts);
  1290. if (ret)
  1291. goto out_unlock;
  1292. retry:
  1293. /* look for a matching existing root */
  1294. if (!opts.subsys_mask && !opts.none && !opts.name) {
  1295. cgrp_dfl_root_visible = true;
  1296. root = &cgrp_dfl_root;
  1297. cgroup_get(&root->cgrp);
  1298. ret = 0;
  1299. goto out_unlock;
  1300. }
  1301. for_each_root(root) {
  1302. bool name_match = false;
  1303. if (root == &cgrp_dfl_root)
  1304. continue;
  1305. /*
  1306. * If we asked for a name then it must match. Also, if
  1307. * name matches but sybsys_mask doesn't, we should fail.
  1308. * Remember whether name matched.
  1309. */
  1310. if (opts.name) {
  1311. if (strcmp(opts.name, root->name))
  1312. continue;
  1313. name_match = true;
  1314. }
  1315. /*
  1316. * If we asked for subsystems (or explicitly for no
  1317. * subsystems) then they must match.
  1318. */
  1319. if ((opts.subsys_mask || opts.none) &&
  1320. (opts.subsys_mask != root->cgrp.subsys_mask)) {
  1321. if (!name_match)
  1322. continue;
  1323. ret = -EBUSY;
  1324. goto out_unlock;
  1325. }
  1326. if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
  1327. if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
  1328. pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
  1329. ret = -EINVAL;
  1330. goto out_unlock;
  1331. } else {
  1332. pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
  1333. }
  1334. }
  1335. /*
  1336. * A root's lifetime is governed by its root cgroup. Zero
  1337. * ref indicate that the root is being destroyed. Wait for
  1338. * destruction to complete so that the subsystems are free.
  1339. * We can use wait_queue for the wait but this path is
  1340. * super cold. Let's just sleep for a bit and retry.
  1341. */
  1342. if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
  1343. mutex_unlock(&cgroup_mutex);
  1344. mutex_unlock(&cgroup_tree_mutex);
  1345. msleep(10);
  1346. mutex_lock(&cgroup_tree_mutex);
  1347. mutex_lock(&cgroup_mutex);
  1348. goto retry;
  1349. }
  1350. ret = 0;
  1351. goto out_unlock;
  1352. }
  1353. /*
  1354. * No such thing, create a new one. name= matching without subsys
  1355. * specification is allowed for already existing hierarchies but we
  1356. * can't create new one without subsys specification.
  1357. */
  1358. if (!opts.subsys_mask && !opts.none) {
  1359. ret = -EINVAL;
  1360. goto out_unlock;
  1361. }
  1362. root = kzalloc(sizeof(*root), GFP_KERNEL);
  1363. if (!root) {
  1364. ret = -ENOMEM;
  1365. goto out_unlock;
  1366. }
  1367. init_cgroup_root(root, &opts);
  1368. ret = cgroup_setup_root(root, opts.subsys_mask);
  1369. if (ret)
  1370. cgroup_free_root(root);
  1371. out_unlock:
  1372. mutex_unlock(&cgroup_mutex);
  1373. mutex_unlock(&cgroup_tree_mutex);
  1374. kfree(opts.release_agent);
  1375. kfree(opts.name);
  1376. if (ret)
  1377. return ERR_PTR(ret);
  1378. dentry = kernfs_mount(fs_type, flags, root->kf_root,
  1379. CGROUP_SUPER_MAGIC, &new_sb);
  1380. if (IS_ERR(dentry) || !new_sb)
  1381. cgroup_put(&root->cgrp);
  1382. return dentry;
  1383. }
  1384. static void cgroup_kill_sb(struct super_block *sb)
  1385. {
  1386. struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
  1387. struct cgroup_root *root = cgroup_root_from_kf(kf_root);
  1388. cgroup_put(&root->cgrp);
  1389. kernfs_kill_sb(sb);
  1390. }
  1391. static struct file_system_type cgroup_fs_type = {
  1392. .name = "cgroup",
  1393. .mount = cgroup_mount,
  1394. .kill_sb = cgroup_kill_sb,
  1395. };
  1396. static struct kobject *cgroup_kobj;
  1397. /**
  1398. * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
  1399. * @task: target task
  1400. * @buf: the buffer to write the path into
  1401. * @buflen: the length of the buffer
  1402. *
  1403. * Determine @task's cgroup on the first (the one with the lowest non-zero
  1404. * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
  1405. * function grabs cgroup_mutex and shouldn't be used inside locks used by
  1406. * cgroup controller callbacks.
  1407. *
  1408. * Return value is the same as kernfs_path().
  1409. */
  1410. char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
  1411. {
  1412. struct cgroup_root *root;
  1413. struct cgroup *cgrp;
  1414. int hierarchy_id = 1;
  1415. char *path = NULL;
  1416. mutex_lock(&cgroup_mutex);
  1417. down_read(&css_set_rwsem);
  1418. root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
  1419. if (root) {
  1420. cgrp = task_cgroup_from_root(task, root);
  1421. path = cgroup_path(cgrp, buf, buflen);
  1422. } else {
  1423. /* if no hierarchy exists, everyone is in "/" */
  1424. if (strlcpy(buf, "/", buflen) < buflen)
  1425. path = buf;
  1426. }
  1427. up_read(&css_set_rwsem);
  1428. mutex_unlock(&cgroup_mutex);
  1429. return path;
  1430. }
  1431. EXPORT_SYMBOL_GPL(task_cgroup_path);
  1432. /* used to track tasks and other necessary states during migration */
  1433. struct cgroup_taskset {
  1434. /* the src and dst cset list running through cset->mg_node */
  1435. struct list_head src_csets;
  1436. struct list_head dst_csets;
  1437. /*
  1438. * Fields for cgroup_taskset_*() iteration.
  1439. *
  1440. * Before migration is committed, the target migration tasks are on
  1441. * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
  1442. * the csets on ->dst_csets. ->csets point to either ->src_csets
  1443. * or ->dst_csets depending on whether migration is committed.
  1444. *
  1445. * ->cur_csets and ->cur_task point to the current task position
  1446. * during iteration.
  1447. */
  1448. struct list_head *csets;
  1449. struct css_set *cur_cset;
  1450. struct task_struct *cur_task;
  1451. };
  1452. /**
  1453. * cgroup_taskset_first - reset taskset and return the first task
  1454. * @tset: taskset of interest
  1455. *
  1456. * @tset iteration is initialized and the first task is returned.
  1457. */
  1458. struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
  1459. {
  1460. tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
  1461. tset->cur_task = NULL;
  1462. return cgroup_taskset_next(tset);
  1463. }
  1464. /**
  1465. * cgroup_taskset_next - iterate to the next task in taskset
  1466. * @tset: taskset of interest
  1467. *
  1468. * Return the next task in @tset. Iteration must have been initialized
  1469. * with cgroup_taskset_first().
  1470. */
  1471. struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
  1472. {
  1473. struct css_set *cset = tset->cur_cset;
  1474. struct task_struct *task = tset->cur_task;
  1475. while (&cset->mg_node != tset->csets) {
  1476. if (!task)
  1477. task = list_first_entry(&cset->mg_tasks,
  1478. struct task_struct, cg_list);
  1479. else
  1480. task = list_next_entry(task, cg_list);
  1481. if (&task->cg_list != &cset->mg_tasks) {
  1482. tset->cur_cset = cset;
  1483. tset->cur_task = task;
  1484. return task;
  1485. }
  1486. cset = list_next_entry(cset, mg_node);
  1487. task = NULL;
  1488. }
  1489. return NULL;
  1490. }
  1491. /**
  1492. * cgroup_task_migrate - move a task from one cgroup to another.
  1493. * @old_cgrp; the cgroup @tsk is being migrated from
  1494. * @tsk: the task being migrated
  1495. * @new_cset: the new css_set @tsk is being attached to
  1496. *
  1497. * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
  1498. */
  1499. static void cgroup_task_migrate(struct cgroup *old_cgrp,
  1500. struct task_struct *tsk,
  1501. struct css_set *new_cset)
  1502. {
  1503. struct css_set *old_cset;
  1504. lockdep_assert_held(&cgroup_mutex);
  1505. lockdep_assert_held(&css_set_rwsem);
  1506. /*
  1507. * We are synchronized through threadgroup_lock() against PF_EXITING
  1508. * setting such that we can't race against cgroup_exit() changing the
  1509. * css_set to init_css_set and dropping the old one.
  1510. */
  1511. WARN_ON_ONCE(tsk->flags & PF_EXITING);
  1512. old_cset = task_css_set(tsk);
  1513. get_css_set(new_cset);
  1514. rcu_assign_pointer(tsk->cgroups, new_cset);
  1515. /*
  1516. * Use move_tail so that cgroup_taskset_first() still returns the
  1517. * leader after migration. This works because cgroup_migrate()
  1518. * ensures that the dst_cset of the leader is the first on the
  1519. * tset's dst_csets list.
  1520. */
  1521. list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
  1522. /*
  1523. * We just gained a reference on old_cset by taking it from the
  1524. * task. As trading it for new_cset is protected by cgroup_mutex,
  1525. * we're safe to drop it here; it will be freed under RCU.
  1526. */
  1527. set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
  1528. put_css_set_locked(old_cset, false);
  1529. }
  1530. /**
  1531. * cgroup_migrate_finish - cleanup after attach
  1532. * @preloaded_csets: list of preloaded css_sets
  1533. *
  1534. * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
  1535. * those functions for details.
  1536. */
  1537. static void cgroup_migrate_finish(struct list_head *preloaded_csets)
  1538. {
  1539. struct css_set *cset, *tmp_cset;
  1540. lockdep_assert_held(&cgroup_mutex);
  1541. down_write(&css_set_rwsem);
  1542. list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
  1543. cset->mg_src_cgrp = NULL;
  1544. cset->mg_dst_cset = NULL;
  1545. list_del_init(&cset->mg_preload_node);
  1546. put_css_set_locked(cset, false);
  1547. }
  1548. up_write(&css_set_rwsem);
  1549. }
  1550. /**
  1551. * cgroup_migrate_add_src - add a migration source css_set
  1552. * @src_cset: the source css_set to add
  1553. * @dst_cgrp: the destination cgroup
  1554. * @preloaded_csets: list of preloaded css_sets
  1555. *
  1556. * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
  1557. * @src_cset and add it to @preloaded_csets, which should later be cleaned
  1558. * up by cgroup_migrate_finish().
  1559. *
  1560. * This function may be called without holding threadgroup_lock even if the
  1561. * target is a process. Threads may be created and destroyed but as long
  1562. * as cgroup_mutex is not dropped, no new css_set can be put into play and
  1563. * the preloaded css_sets are guaranteed to cover all migrations.
  1564. */
  1565. static void cgroup_migrate_add_src(struct css_set *src_cset,
  1566. struct cgroup *dst_cgrp,
  1567. struct list_head *preloaded_csets)
  1568. {
  1569. struct cgroup *src_cgrp;
  1570. lockdep_assert_held(&cgroup_mutex);
  1571. lockdep_assert_held(&css_set_rwsem);
  1572. src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
  1573. /* nothing to do if this cset already belongs to the cgroup */
  1574. if (src_cgrp == dst_cgrp)
  1575. return;
  1576. if (!list_empty(&src_cset->mg_preload_node))
  1577. return;
  1578. WARN_ON(src_cset->mg_src_cgrp);
  1579. WARN_ON(!list_empty(&src_cset->mg_tasks));
  1580. WARN_ON(!list_empty(&src_cset->mg_node));
  1581. src_cset->mg_src_cgrp = src_cgrp;
  1582. get_css_set(src_cset);
  1583. list_add(&src_cset->mg_preload_node, preloaded_csets);
  1584. }
  1585. /**
  1586. * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
  1587. * @dst_cgrp: the destination cgroup
  1588. * @preloaded_csets: list of preloaded source css_sets
  1589. *
  1590. * Tasks are about to be moved to @dst_cgrp and all the source css_sets
  1591. * have been preloaded to @preloaded_csets. This function looks up and
  1592. * pins all destination css_sets, links each to its source, and put them on
  1593. * @preloaded_csets.
  1594. *
  1595. * This function must be called after cgroup_migrate_add_src() has been
  1596. * called on each migration source css_set. After migration is performed
  1597. * using cgroup_migrate(), cgroup_migrate_finish() must be called on
  1598. * @preloaded_csets.
  1599. */
  1600. static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
  1601. struct list_head *preloaded_csets)
  1602. {
  1603. LIST_HEAD(csets);
  1604. struct css_set *src_cset;
  1605. lockdep_assert_held(&cgroup_mutex);
  1606. /* look up the dst cset for each src cset and link it to src */
  1607. list_for_each_entry(src_cset, preloaded_csets, mg_preload_node) {
  1608. struct css_set *dst_cset;
  1609. dst_cset = find_css_set(src_cset, dst_cgrp);
  1610. if (!dst_cset)
  1611. goto err;
  1612. WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
  1613. src_cset->mg_dst_cset = dst_cset;
  1614. if (list_empty(&dst_cset->mg_preload_node))
  1615. list_add(&dst_cset->mg_preload_node, &csets);
  1616. else
  1617. put_css_set(dst_cset, false);
  1618. }
  1619. list_splice(&csets, preloaded_csets);
  1620. return 0;
  1621. err:
  1622. cgroup_migrate_finish(&csets);
  1623. return -ENOMEM;
  1624. }
  1625. /**
  1626. * cgroup_migrate - migrate a process or task to a cgroup
  1627. * @cgrp: the destination cgroup
  1628. * @leader: the leader of the process or the task to migrate
  1629. * @threadgroup: whether @leader points to the whole process or a single task
  1630. *
  1631. * Migrate a process or task denoted by @leader to @cgrp. If migrating a
  1632. * process, the caller must be holding threadgroup_lock of @leader. The
  1633. * caller is also responsible for invoking cgroup_migrate_add_src() and
  1634. * cgroup_migrate_prepare_dst() on the targets before invoking this
  1635. * function and following up with cgroup_migrate_finish().
  1636. *
  1637. * As long as a controller's ->can_attach() doesn't fail, this function is
  1638. * guaranteed to succeed. This means that, excluding ->can_attach()
  1639. * failure, when migrating multiple targets, the success or failure can be
  1640. * decided for all targets by invoking group_migrate_prepare_dst() before
  1641. * actually starting migrating.
  1642. */
  1643. static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
  1644. bool threadgroup)
  1645. {
  1646. struct cgroup_taskset tset = {
  1647. .src_csets = LIST_HEAD_INIT(tset.src_csets),
  1648. .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
  1649. .csets = &tset.src_csets,
  1650. };
  1651. struct cgroup_subsys_state *css, *failed_css = NULL;
  1652. struct css_set *cset, *tmp_cset;
  1653. struct task_struct *task, *tmp_task;
  1654. int i, ret;
  1655. /*
  1656. * Prevent freeing of tasks while we take a snapshot. Tasks that are
  1657. * already PF_EXITING could be freed from underneath us unless we
  1658. * take an rcu_read_lock.
  1659. */
  1660. down_write(&css_set_rwsem);
  1661. rcu_read_lock();
  1662. task = leader;
  1663. do {
  1664. /* @task either already exited or can't exit until the end */
  1665. if (task->flags & PF_EXITING)
  1666. goto next;
  1667. /* leave @task alone if post_fork() hasn't linked it yet */
  1668. if (list_empty(&task->cg_list))
  1669. goto next;
  1670. cset = task_css_set(task);
  1671. if (!cset->mg_src_cgrp)
  1672. goto next;
  1673. /*
  1674. * cgroup_taskset_first() must always return the leader.
  1675. * Take care to avoid disturbing the ordering.
  1676. */
  1677. list_move_tail(&task->cg_list, &cset->mg_tasks);
  1678. if (list_empty(&cset->mg_node))
  1679. list_add_tail(&cset->mg_node, &tset.src_csets);
  1680. if (list_empty(&cset->mg_dst_cset->mg_node))
  1681. list_move_tail(&cset->mg_dst_cset->mg_node,
  1682. &tset.dst_csets);
  1683. next:
  1684. if (!threadgroup)
  1685. break;
  1686. } while_each_thread(leader, task);
  1687. rcu_read_unlock();
  1688. up_write(&css_set_rwsem);
  1689. /* methods shouldn't be called if no task is actually migrating */
  1690. if (list_empty(&tset.src_csets))
  1691. return 0;
  1692. /* check that we can legitimately attach to the cgroup */
  1693. for_each_css(css, i, cgrp) {
  1694. if (css->ss->can_attach) {
  1695. ret = css->ss->can_attach(css, &tset);
  1696. if (ret) {
  1697. failed_css = css;
  1698. goto out_cancel_attach;
  1699. }
  1700. }
  1701. }
  1702. /*
  1703. * Now that we're guaranteed success, proceed to move all tasks to
  1704. * the new cgroup. There are no failure cases after here, so this
  1705. * is the commit point.
  1706. */
  1707. down_write(&css_set_rwsem);
  1708. list_for_each_entry(cset, &tset.src_csets, mg_node) {
  1709. list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
  1710. cgroup_task_migrate(cset->mg_src_cgrp, task,
  1711. cset->mg_dst_cset);
  1712. }
  1713. up_write(&css_set_rwsem);
  1714. /*
  1715. * Migration is committed, all target tasks are now on dst_csets.
  1716. * Nothing is sensitive to fork() after this point. Notify
  1717. * controllers that migration is complete.
  1718. */
  1719. tset.csets = &tset.dst_csets;
  1720. for_each_css(css, i, cgrp)
  1721. if (css->ss->attach)
  1722. css->ss->attach(css, &tset);
  1723. ret = 0;
  1724. goto out_release_tset;
  1725. out_cancel_attach:
  1726. for_each_css(css, i, cgrp) {
  1727. if (css == failed_css)
  1728. break;
  1729. if (css->ss->cancel_attach)
  1730. css->ss->cancel_attach(css, &tset);
  1731. }
  1732. out_release_tset:
  1733. down_write(&css_set_rwsem);
  1734. list_splice_init(&tset.dst_csets, &tset.src_csets);
  1735. list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
  1736. list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
  1737. list_del_init(&cset->mg_node);
  1738. }
  1739. up_write(&css_set_rwsem);
  1740. return ret;
  1741. }
  1742. /**
  1743. * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
  1744. * @dst_cgrp: the cgroup to attach to
  1745. * @leader: the task or the leader of the threadgroup to be attached
  1746. * @threadgroup: attach the whole threadgroup?
  1747. *
  1748. * Call holding cgroup_mutex and threadgroup_lock of @leader.
  1749. */
  1750. static int cgroup_attach_task(struct cgroup *dst_cgrp,
  1751. struct task_struct *leader, bool threadgroup)
  1752. {
  1753. LIST_HEAD(preloaded_csets);
  1754. struct task_struct *task;
  1755. int ret;
  1756. /* look up all src csets */
  1757. down_read(&css_set_rwsem);
  1758. rcu_read_lock();
  1759. task = leader;
  1760. do {
  1761. cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
  1762. &preloaded_csets);
  1763. if (!threadgroup)
  1764. break;
  1765. } while_each_thread(leader, task);
  1766. rcu_read_unlock();
  1767. up_read(&css_set_rwsem);
  1768. /* prepare dst csets and commit */
  1769. ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
  1770. if (!ret)
  1771. ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
  1772. cgroup_migrate_finish(&preloaded_csets);
  1773. return ret;
  1774. }
  1775. /*
  1776. * Find the task_struct of the task to attach by vpid and pass it along to the
  1777. * function to attach either it or all tasks in its threadgroup. Will lock
  1778. * cgroup_mutex and threadgroup.
  1779. */
  1780. static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
  1781. {
  1782. struct task_struct *tsk;
  1783. const struct cred *cred = current_cred(), *tcred;
  1784. int ret;
  1785. if (!cgroup_lock_live_group(cgrp))
  1786. return -ENODEV;
  1787. retry_find_task:
  1788. rcu_read_lock();
  1789. if (pid) {
  1790. tsk = find_task_by_vpid(pid);
  1791. if (!tsk) {
  1792. rcu_read_unlock();
  1793. ret = -ESRCH;
  1794. goto out_unlock_cgroup;
  1795. }
  1796. /*
  1797. * even if we're attaching all tasks in the thread group, we
  1798. * only need to check permissions on one of them.
  1799. */
  1800. tcred = __task_cred(tsk);
  1801. if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
  1802. !uid_eq(cred->euid, tcred->uid) &&
  1803. !uid_eq(cred->euid, tcred->suid)) {
  1804. rcu_read_unlock();
  1805. ret = -EACCES;
  1806. goto out_unlock_cgroup;
  1807. }
  1808. } else
  1809. tsk = current;
  1810. if (threadgroup)
  1811. tsk = tsk->group_leader;
  1812. /*
  1813. * Workqueue threads may acquire PF_NO_SETAFFINITY and become
  1814. * trapped in a cpuset, or RT worker may be born in a cgroup
  1815. * with no rt_runtime allocated. Just say no.
  1816. */
  1817. if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
  1818. ret = -EINVAL;
  1819. rcu_read_unlock();
  1820. goto out_unlock_cgroup;
  1821. }
  1822. get_task_struct(tsk);
  1823. rcu_read_unlock();
  1824. threadgroup_lock(tsk);
  1825. if (threadgroup) {
  1826. if (!thread_group_leader(tsk)) {
  1827. /*
  1828. * a race with de_thread from another thread's exec()
  1829. * may strip us of our leadership, if this happens,
  1830. * there is no choice but to throw this task away and
  1831. * try again; this is
  1832. * "double-double-toil-and-trouble-check locking".
  1833. */
  1834. threadgroup_unlock(tsk);
  1835. put_task_struct(tsk);
  1836. goto retry_find_task;
  1837. }
  1838. }
  1839. ret = cgroup_attach_task(cgrp, tsk, threadgroup);
  1840. threadgroup_unlock(tsk);
  1841. put_task_struct(tsk);
  1842. out_unlock_cgroup:
  1843. mutex_unlock(&cgroup_mutex);
  1844. return ret;
  1845. }
  1846. /**
  1847. * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
  1848. * @from: attach to all cgroups of a given task
  1849. * @tsk: the task to be attached
  1850. */
  1851. int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
  1852. {
  1853. struct cgroup_root *root;
  1854. int retval = 0;
  1855. mutex_lock(&cgroup_mutex);
  1856. for_each_root(root) {
  1857. struct cgroup *from_cgrp;
  1858. if (root == &cgrp_dfl_root)
  1859. continue;
  1860. down_read(&css_set_rwsem);
  1861. from_cgrp = task_cgroup_from_root(from, root);
  1862. up_read(&css_set_rwsem);
  1863. retval = cgroup_attach_task(from_cgrp, tsk, false);
  1864. if (retval)
  1865. break;
  1866. }
  1867. mutex_unlock(&cgroup_mutex);
  1868. return retval;
  1869. }
  1870. EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
  1871. static int cgroup_tasks_write(struct cgroup_subsys_state *css,
  1872. struct cftype *cft, u64 pid)
  1873. {
  1874. return attach_task_by_pid(css->cgroup, pid, false);
  1875. }
  1876. static int cgroup_procs_write(struct cgroup_subsys_state *css,
  1877. struct cftype *cft, u64 tgid)
  1878. {
  1879. return attach_task_by_pid(css->cgroup, tgid, true);
  1880. }
  1881. static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
  1882. struct cftype *cft, char *buffer)
  1883. {
  1884. struct cgroup_root *root = css->cgroup->root;
  1885. BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
  1886. if (!cgroup_lock_live_group(css->cgroup))
  1887. return -ENODEV;
  1888. spin_lock(&release_agent_path_lock);
  1889. strlcpy(root->release_agent_path, buffer,
  1890. sizeof(root->release_agent_path));
  1891. spin_unlock(&release_agent_path_lock);
  1892. mutex_unlock(&cgroup_mutex);
  1893. return 0;
  1894. }
  1895. static int cgroup_release_agent_show(struct seq_file *seq, void *v)
  1896. {
  1897. struct cgroup *cgrp = seq_css(seq)->cgroup;
  1898. if (!cgroup_lock_live_group(cgrp))
  1899. return -ENODEV;
  1900. seq_puts(seq, cgrp->root->release_agent_path);
  1901. seq_putc(seq, '\n');
  1902. mutex_unlock(&cgroup_mutex);
  1903. return 0;
  1904. }
  1905. static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
  1906. {
  1907. struct cgroup *cgrp = seq_css(seq)->cgroup;
  1908. seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
  1909. return 0;
  1910. }
  1911. static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
  1912. size_t nbytes, loff_t off)
  1913. {
  1914. struct cgroup *cgrp = of->kn->parent->priv;
  1915. struct cftype *cft = of->kn->priv;
  1916. struct cgroup_subsys_state *css;
  1917. int ret;
  1918. /*
  1919. * kernfs guarantees that a file isn't deleted with operations in
  1920. * flight, which means that the matching css is and stays alive and
  1921. * doesn't need to be pinned. The RCU locking is not necessary
  1922. * either. It's just for the convenience of using cgroup_css().
  1923. */
  1924. rcu_read_lock();
  1925. css = cgroup_css(cgrp, cft->ss);
  1926. rcu_read_unlock();
  1927. if (cft->write_string) {
  1928. ret = cft->write_string(css, cft, strstrip(buf));
  1929. } else if (cft->write_u64) {
  1930. unsigned long long v;
  1931. ret = kstrtoull(buf, 0, &v);
  1932. if (!ret)
  1933. ret = cft->write_u64(css, cft, v);
  1934. } else if (cft->write_s64) {
  1935. long long v;
  1936. ret = kstrtoll(buf, 0, &v);
  1937. if (!ret)
  1938. ret = cft->write_s64(css, cft, v);
  1939. } else if (cft->trigger) {
  1940. ret = cft->trigger(css, (unsigned int)cft->private);
  1941. } else {
  1942. ret = -EINVAL;
  1943. }
  1944. return ret ?: nbytes;
  1945. }
  1946. static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
  1947. {
  1948. return seq_cft(seq)->seq_start(seq, ppos);
  1949. }
  1950. static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
  1951. {
  1952. return seq_cft(seq)->seq_next(seq, v, ppos);
  1953. }
  1954. static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
  1955. {
  1956. seq_cft(seq)->seq_stop(seq, v);
  1957. }
  1958. static int cgroup_seqfile_show(struct seq_file *m, void *arg)
  1959. {
  1960. struct cftype *cft = seq_cft(m);
  1961. struct cgroup_subsys_state *css = seq_css(m);
  1962. if (cft->seq_show)
  1963. return cft->seq_show(m, arg);
  1964. if (cft->read_u64)
  1965. seq_printf(m, "%llu\n", cft->read_u64(css, cft));
  1966. else if (cft->read_s64)
  1967. seq_printf(m, "%lld\n", cft->read_s64(css, cft));
  1968. else
  1969. return -EINVAL;
  1970. return 0;
  1971. }
  1972. static struct kernfs_ops cgroup_kf_single_ops = {
  1973. .atomic_write_len = PAGE_SIZE,
  1974. .write = cgroup_file_write,
  1975. .seq_show = cgroup_seqfile_show,
  1976. };
  1977. static struct kernfs_ops cgroup_kf_ops = {
  1978. .atomic_write_len = PAGE_SIZE,
  1979. .write = cgroup_file_write,
  1980. .seq_start = cgroup_seqfile_start,
  1981. .seq_next = cgroup_seqfile_next,
  1982. .seq_stop = cgroup_seqfile_stop,
  1983. .seq_show = cgroup_seqfile_show,
  1984. };
  1985. /*
  1986. * cgroup_rename - Only allow simple rename of directories in place.
  1987. */
  1988. static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
  1989. const char *new_name_str)
  1990. {
  1991. struct cgroup *cgrp = kn->priv;
  1992. int ret;
  1993. if (kernfs_type(kn) != KERNFS_DIR)
  1994. return -ENOTDIR;
  1995. if (kn->parent != new_parent)
  1996. return -EIO;
  1997. /*
  1998. * This isn't a proper migration and its usefulness is very
  1999. * limited. Disallow if sane_behavior.
  2000. */
  2001. if (cgroup_sane_behavior(cgrp))
  2002. return -EPERM;
  2003. /*
  2004. * We're gonna grab cgroup_tree_mutex which nests outside kernfs
  2005. * active_ref. kernfs_rename() doesn't require active_ref
  2006. * protection. Break them before grabbing cgroup_tree_mutex.
  2007. */
  2008. kernfs_break_active_protection(new_parent);
  2009. kernfs_break_active_protection(kn);
  2010. mutex_lock(&cgroup_tree_mutex);
  2011. mutex_lock(&cgroup_mutex);
  2012. ret = kernfs_rename(kn, new_parent, new_name_str);
  2013. mutex_unlock(&cgroup_mutex);
  2014. mutex_unlock(&cgroup_tree_mutex);
  2015. kernfs_unbreak_active_protection(kn);
  2016. kernfs_unbreak_active_protection(new_parent);
  2017. return ret;
  2018. }
  2019. /* set uid and gid of cgroup dirs and files to that of the creator */
  2020. static int cgroup_kn_set_ugid(struct kernfs_node *kn)
  2021. {
  2022. struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
  2023. .ia_uid = current_fsuid(),
  2024. .ia_gid = current_fsgid(), };
  2025. if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
  2026. gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
  2027. return 0;
  2028. return kernfs_setattr(kn, &iattr);
  2029. }
  2030. static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
  2031. {
  2032. char name[CGROUP_FILE_NAME_MAX];
  2033. struct kernfs_node *kn;
  2034. struct lock_class_key *key = NULL;
  2035. int ret;
  2036. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  2037. key = &cft->lockdep_key;
  2038. #endif
  2039. kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
  2040. cgroup_file_mode(cft), 0, cft->kf_ops, cft,
  2041. NULL, false, key);
  2042. if (IS_ERR(kn))
  2043. return PTR_ERR(kn);
  2044. ret = cgroup_kn_set_ugid(kn);
  2045. if (ret)
  2046. kernfs_remove(kn);
  2047. return ret;
  2048. }
  2049. /**
  2050. * cgroup_addrm_files - add or remove files to a cgroup directory
  2051. * @cgrp: the target cgroup
  2052. * @cfts: array of cftypes to be added
  2053. * @is_add: whether to add or remove
  2054. *
  2055. * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
  2056. * For removals, this function never fails. If addition fails, this
  2057. * function doesn't remove files already added. The caller is responsible
  2058. * for cleaning up.
  2059. */
  2060. static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
  2061. bool is_add)
  2062. {
  2063. struct cftype *cft;
  2064. int ret;
  2065. lockdep_assert_held(&cgroup_tree_mutex);
  2066. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  2067. /* does cft->flags tell us to skip this file on @cgrp? */
  2068. if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
  2069. continue;
  2070. if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
  2071. continue;
  2072. if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
  2073. continue;
  2074. if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
  2075. continue;
  2076. if (is_add) {
  2077. ret = cgroup_add_file(cgrp, cft);
  2078. if (ret) {
  2079. pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
  2080. cft->name, ret);
  2081. return ret;
  2082. }
  2083. } else {
  2084. cgroup_rm_file(cgrp, cft);
  2085. }
  2086. }
  2087. return 0;
  2088. }
  2089. static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
  2090. {
  2091. LIST_HEAD(pending);
  2092. struct cgroup_subsys *ss = cfts[0].ss;
  2093. struct cgroup *root = &ss->root->cgrp;
  2094. struct cgroup_subsys_state *css;
  2095. int ret = 0;
  2096. lockdep_assert_held(&cgroup_tree_mutex);
  2097. /* don't bother if @ss isn't attached */
  2098. if (ss->root == &cgrp_dfl_root)
  2099. return 0;
  2100. /* add/rm files for all cgroups created before */
  2101. css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
  2102. struct cgroup *cgrp = css->cgroup;
  2103. if (cgroup_is_dead(cgrp))
  2104. continue;
  2105. ret = cgroup_addrm_files(cgrp, cfts, is_add);
  2106. if (ret)
  2107. break;
  2108. }
  2109. if (is_add && !ret)
  2110. kernfs_activate(root->kn);
  2111. return ret;
  2112. }
  2113. static void cgroup_exit_cftypes(struct cftype *cfts)
  2114. {
  2115. struct cftype *cft;
  2116. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  2117. /* free copy for custom atomic_write_len, see init_cftypes() */
  2118. if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
  2119. kfree(cft->kf_ops);
  2120. cft->kf_ops = NULL;
  2121. cft->ss = NULL;
  2122. }
  2123. }
  2124. static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  2125. {
  2126. struct cftype *cft;
  2127. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  2128. struct kernfs_ops *kf_ops;
  2129. WARN_ON(cft->ss || cft->kf_ops);
  2130. if (cft->seq_start)
  2131. kf_ops = &cgroup_kf_ops;
  2132. else
  2133. kf_ops = &cgroup_kf_single_ops;
  2134. /*
  2135. * Ugh... if @cft wants a custom max_write_len, we need to
  2136. * make a copy of kf_ops to set its atomic_write_len.
  2137. */
  2138. if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
  2139. kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
  2140. if (!kf_ops) {
  2141. cgroup_exit_cftypes(cfts);
  2142. return -ENOMEM;
  2143. }
  2144. kf_ops->atomic_write_len = cft->max_write_len;
  2145. }
  2146. cft->kf_ops = kf_ops;
  2147. cft->ss = ss;
  2148. }
  2149. return 0;
  2150. }
  2151. static int cgroup_rm_cftypes_locked(struct cftype *cfts)
  2152. {
  2153. lockdep_assert_held(&cgroup_tree_mutex);
  2154. if (!cfts || !cfts[0].ss)
  2155. return -ENOENT;
  2156. list_del(&cfts->node);
  2157. cgroup_apply_cftypes(cfts, false);
  2158. cgroup_exit_cftypes(cfts);
  2159. return 0;
  2160. }
  2161. /**
  2162. * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
  2163. * @cfts: zero-length name terminated array of cftypes
  2164. *
  2165. * Unregister @cfts. Files described by @cfts are removed from all
  2166. * existing cgroups and all future cgroups won't have them either. This
  2167. * function can be called anytime whether @cfts' subsys is attached or not.
  2168. *
  2169. * Returns 0 on successful unregistration, -ENOENT if @cfts is not
  2170. * registered.
  2171. */
  2172. int cgroup_rm_cftypes(struct cftype *cfts)
  2173. {
  2174. int ret;
  2175. mutex_lock(&cgroup_tree_mutex);
  2176. ret = cgroup_rm_cftypes_locked(cfts);
  2177. mutex_unlock(&cgroup_tree_mutex);
  2178. return ret;
  2179. }
  2180. /**
  2181. * cgroup_add_cftypes - add an array of cftypes to a subsystem
  2182. * @ss: target cgroup subsystem
  2183. * @cfts: zero-length name terminated array of cftypes
  2184. *
  2185. * Register @cfts to @ss. Files described by @cfts are created for all
  2186. * existing cgroups to which @ss is attached and all future cgroups will
  2187. * have them too. This function can be called anytime whether @ss is
  2188. * attached or not.
  2189. *
  2190. * Returns 0 on successful registration, -errno on failure. Note that this
  2191. * function currently returns 0 as long as @cfts registration is successful
  2192. * even if some file creation attempts on existing cgroups fail.
  2193. */
  2194. int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  2195. {
  2196. int ret;
  2197. if (!cfts || cfts[0].name[0] == '\0')
  2198. return 0;
  2199. ret = cgroup_init_cftypes(ss, cfts);
  2200. if (ret)
  2201. return ret;
  2202. mutex_lock(&cgroup_tree_mutex);
  2203. list_add_tail(&cfts->node, &ss->cfts);
  2204. ret = cgroup_apply_cftypes(cfts, true);
  2205. if (ret)
  2206. cgroup_rm_cftypes_locked(cfts);
  2207. mutex_unlock(&cgroup_tree_mutex);
  2208. return ret;
  2209. }
  2210. /**
  2211. * cgroup_task_count - count the number of tasks in a cgroup.
  2212. * @cgrp: the cgroup in question
  2213. *
  2214. * Return the number of tasks in the cgroup.
  2215. */
  2216. static int cgroup_task_count(const struct cgroup *cgrp)
  2217. {
  2218. int count = 0;
  2219. struct cgrp_cset_link *link;
  2220. down_read(&css_set_rwsem);
  2221. list_for_each_entry(link, &cgrp->cset_links, cset_link)
  2222. count += atomic_read(&link->cset->refcount);
  2223. up_read(&css_set_rwsem);
  2224. return count;
  2225. }
  2226. /**
  2227. * css_next_child - find the next child of a given css
  2228. * @pos_css: the current position (%NULL to initiate traversal)
  2229. * @parent_css: css whose children to walk
  2230. *
  2231. * This function returns the next child of @parent_css and should be called
  2232. * under either cgroup_mutex or RCU read lock. The only requirement is
  2233. * that @parent_css and @pos_css are accessible. The next sibling is
  2234. * guaranteed to be returned regardless of their states.
  2235. */
  2236. struct cgroup_subsys_state *
  2237. css_next_child(struct cgroup_subsys_state *pos_css,
  2238. struct cgroup_subsys_state *parent_css)
  2239. {
  2240. struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
  2241. struct cgroup *cgrp = parent_css->cgroup;
  2242. struct cgroup *next;
  2243. cgroup_assert_mutexes_or_rcu_locked();
  2244. /*
  2245. * @pos could already have been removed. Once a cgroup is removed,
  2246. * its ->sibling.next is no longer updated when its next sibling
  2247. * changes. As CGRP_DEAD assertion is serialized and happens
  2248. * before the cgroup is taken off the ->sibling list, if we see it
  2249. * unasserted, it's guaranteed that the next sibling hasn't
  2250. * finished its grace period even if it's already removed, and thus
  2251. * safe to dereference from this RCU critical section. If
  2252. * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
  2253. * to be visible as %true here.
  2254. *
  2255. * If @pos is dead, its next pointer can't be dereferenced;
  2256. * however, as each cgroup is given a monotonically increasing
  2257. * unique serial number and always appended to the sibling list,
  2258. * the next one can be found by walking the parent's children until
  2259. * we see a cgroup with higher serial number than @pos's. While
  2260. * this path can be slower, it's taken only when either the current
  2261. * cgroup is removed or iteration and removal race.
  2262. */
  2263. if (!pos) {
  2264. next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
  2265. } else if (likely(!cgroup_is_dead(pos))) {
  2266. next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
  2267. } else {
  2268. list_for_each_entry_rcu(next, &cgrp->children, sibling)
  2269. if (next->serial_nr > pos->serial_nr)
  2270. break;
  2271. }
  2272. if (&next->sibling == &cgrp->children)
  2273. return NULL;
  2274. return cgroup_css(next, parent_css->ss);
  2275. }
  2276. /**
  2277. * css_next_descendant_pre - find the next descendant for pre-order walk
  2278. * @pos: the current position (%NULL to initiate traversal)
  2279. * @root: css whose descendants to walk
  2280. *
  2281. * To be used by css_for_each_descendant_pre(). Find the next descendant
  2282. * to visit for pre-order traversal of @root's descendants. @root is
  2283. * included in the iteration and the first node to be visited.
  2284. *
  2285. * While this function requires cgroup_mutex or RCU read locking, it
  2286. * doesn't require the whole traversal to be contained in a single critical
  2287. * section. This function will return the correct next descendant as long
  2288. * as both @pos and @root are accessible and @pos is a descendant of @root.
  2289. */
  2290. struct cgroup_subsys_state *
  2291. css_next_descendant_pre(struct cgroup_subsys_state *pos,
  2292. struct cgroup_subsys_state *root)
  2293. {
  2294. struct cgroup_subsys_state *next;
  2295. cgroup_assert_mutexes_or_rcu_locked();
  2296. /* if first iteration, visit @root */
  2297. if (!pos)
  2298. return root;
  2299. /* visit the first child if exists */
  2300. next = css_next_child(NULL, pos);
  2301. if (next)
  2302. return next;
  2303. /* no child, visit my or the closest ancestor's next sibling */
  2304. while (pos != root) {
  2305. next = css_next_child(pos, css_parent(pos));
  2306. if (next)
  2307. return next;
  2308. pos = css_parent(pos);
  2309. }
  2310. return NULL;
  2311. }
  2312. /**
  2313. * css_rightmost_descendant - return the rightmost descendant of a css
  2314. * @pos: css of interest
  2315. *
  2316. * Return the rightmost descendant of @pos. If there's no descendant, @pos
  2317. * is returned. This can be used during pre-order traversal to skip
  2318. * subtree of @pos.
  2319. *
  2320. * While this function requires cgroup_mutex or RCU read locking, it
  2321. * doesn't require the whole traversal to be contained in a single critical
  2322. * section. This function will return the correct rightmost descendant as
  2323. * long as @pos is accessible.
  2324. */
  2325. struct cgroup_subsys_state *
  2326. css_rightmost_descendant(struct cgroup_subsys_state *pos)
  2327. {
  2328. struct cgroup_subsys_state *last, *tmp;
  2329. cgroup_assert_mutexes_or_rcu_locked();
  2330. do {
  2331. last = pos;
  2332. /* ->prev isn't RCU safe, walk ->next till the end */
  2333. pos = NULL;
  2334. css_for_each_child(tmp, last)
  2335. pos = tmp;
  2336. } while (pos);
  2337. return last;
  2338. }
  2339. static struct cgroup_subsys_state *
  2340. css_leftmost_descendant(struct cgroup_subsys_state *pos)
  2341. {
  2342. struct cgroup_subsys_state *last;
  2343. do {
  2344. last = pos;
  2345. pos = css_next_child(NULL, pos);
  2346. } while (pos);
  2347. return last;
  2348. }
  2349. /**
  2350. * css_next_descendant_post - find the next descendant for post-order walk
  2351. * @pos: the current position (%NULL to initiate traversal)
  2352. * @root: css whose descendants to walk
  2353. *
  2354. * To be used by css_for_each_descendant_post(). Find the next descendant
  2355. * to visit for post-order traversal of @root's descendants. @root is
  2356. * included in the iteration and the last node to be visited.
  2357. *
  2358. * While this function requires cgroup_mutex or RCU read locking, it
  2359. * doesn't require the whole traversal to be contained in a single critical
  2360. * section. This function will return the correct next descendant as long
  2361. * as both @pos and @cgroup are accessible and @pos is a descendant of
  2362. * @cgroup.
  2363. */
  2364. struct cgroup_subsys_state *
  2365. css_next_descendant_post(struct cgroup_subsys_state *pos,
  2366. struct cgroup_subsys_state *root)
  2367. {
  2368. struct cgroup_subsys_state *next;
  2369. cgroup_assert_mutexes_or_rcu_locked();
  2370. /* if first iteration, visit leftmost descendant which may be @root */
  2371. if (!pos)
  2372. return css_leftmost_descendant(root);
  2373. /* if we visited @root, we're done */
  2374. if (pos == root)
  2375. return NULL;
  2376. /* if there's an unvisited sibling, visit its leftmost descendant */
  2377. next = css_next_child(pos, css_parent(pos));
  2378. if (next)
  2379. return css_leftmost_descendant(next);
  2380. /* no sibling left, visit parent */
  2381. return css_parent(pos);
  2382. }
  2383. /**
  2384. * css_advance_task_iter - advance a task itererator to the next css_set
  2385. * @it: the iterator to advance
  2386. *
  2387. * Advance @it to the next css_set to walk.
  2388. */
  2389. static void css_advance_task_iter(struct css_task_iter *it)
  2390. {
  2391. struct list_head *l = it->cset_link;
  2392. struct cgrp_cset_link *link;
  2393. struct css_set *cset;
  2394. /* Advance to the next non-empty css_set */
  2395. do {
  2396. l = l->next;
  2397. if (l == &it->origin_css->cgroup->cset_links) {
  2398. it->cset_link = NULL;
  2399. return;
  2400. }
  2401. link = list_entry(l, struct cgrp_cset_link, cset_link);
  2402. cset = link->cset;
  2403. } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
  2404. it->cset_link = l;
  2405. if (!list_empty(&cset->tasks))
  2406. it->task = cset->tasks.next;
  2407. else
  2408. it->task = cset->mg_tasks.next;
  2409. }
  2410. /**
  2411. * css_task_iter_start - initiate task iteration
  2412. * @css: the css to walk tasks of
  2413. * @it: the task iterator to use
  2414. *
  2415. * Initiate iteration through the tasks of @css. The caller can call
  2416. * css_task_iter_next() to walk through the tasks until the function
  2417. * returns NULL. On completion of iteration, css_task_iter_end() must be
  2418. * called.
  2419. *
  2420. * Note that this function acquires a lock which is released when the
  2421. * iteration finishes. The caller can't sleep while iteration is in
  2422. * progress.
  2423. */
  2424. void css_task_iter_start(struct cgroup_subsys_state *css,
  2425. struct css_task_iter *it)
  2426. __acquires(css_set_rwsem)
  2427. {
  2428. /* no one should try to iterate before mounting cgroups */
  2429. WARN_ON_ONCE(!use_task_css_set_links);
  2430. down_read(&css_set_rwsem);
  2431. it->origin_css = css;
  2432. it->cset_link = &css->cgroup->cset_links;
  2433. css_advance_task_iter(it);
  2434. }
  2435. /**
  2436. * css_task_iter_next - return the next task for the iterator
  2437. * @it: the task iterator being iterated
  2438. *
  2439. * The "next" function for task iteration. @it should have been
  2440. * initialized via css_task_iter_start(). Returns NULL when the iteration
  2441. * reaches the end.
  2442. */
  2443. struct task_struct *css_task_iter_next(struct css_task_iter *it)
  2444. {
  2445. struct task_struct *res;
  2446. struct list_head *l = it->task;
  2447. struct cgrp_cset_link *link = list_entry(it->cset_link,
  2448. struct cgrp_cset_link, cset_link);
  2449. /* If the iterator cg is NULL, we have no tasks */
  2450. if (!it->cset_link)
  2451. return NULL;
  2452. res = list_entry(l, struct task_struct, cg_list);
  2453. /*
  2454. * Advance iterator to find next entry. cset->tasks is consumed
  2455. * first and then ->mg_tasks. After ->mg_tasks, we move onto the
  2456. * next cset.
  2457. */
  2458. l = l->next;
  2459. if (l == &link->cset->tasks)
  2460. l = link->cset->mg_tasks.next;
  2461. if (l == &link->cset->mg_tasks)
  2462. css_advance_task_iter(it);
  2463. else
  2464. it->task = l;
  2465. return res;
  2466. }
  2467. /**
  2468. * css_task_iter_end - finish task iteration
  2469. * @it: the task iterator to finish
  2470. *
  2471. * Finish task iteration started by css_task_iter_start().
  2472. */
  2473. void css_task_iter_end(struct css_task_iter *it)
  2474. __releases(css_set_rwsem)
  2475. {
  2476. up_read(&css_set_rwsem);
  2477. }
  2478. /**
  2479. * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
  2480. * @to: cgroup to which the tasks will be moved
  2481. * @from: cgroup in which the tasks currently reside
  2482. *
  2483. * Locking rules between cgroup_post_fork() and the migration path
  2484. * guarantee that, if a task is forking while being migrated, the new child
  2485. * is guaranteed to be either visible in the source cgroup after the
  2486. * parent's migration is complete or put into the target cgroup. No task
  2487. * can slip out of migration through forking.
  2488. */
  2489. int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
  2490. {
  2491. LIST_HEAD(preloaded_csets);
  2492. struct cgrp_cset_link *link;
  2493. struct css_task_iter it;
  2494. struct task_struct *task;
  2495. int ret;
  2496. mutex_lock(&cgroup_mutex);
  2497. /* all tasks in @from are being moved, all csets are source */
  2498. down_read(&css_set_rwsem);
  2499. list_for_each_entry(link, &from->cset_links, cset_link)
  2500. cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
  2501. up_read(&css_set_rwsem);
  2502. ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
  2503. if (ret)
  2504. goto out_err;
  2505. /*
  2506. * Migrate tasks one-by-one until @form is empty. This fails iff
  2507. * ->can_attach() fails.
  2508. */
  2509. do {
  2510. css_task_iter_start(&from->dummy_css, &it);
  2511. task = css_task_iter_next(&it);
  2512. if (task)
  2513. get_task_struct(task);
  2514. css_task_iter_end(&it);
  2515. if (task) {
  2516. ret = cgroup_migrate(to, task, false);
  2517. put_task_struct(task);
  2518. }
  2519. } while (task && !ret);
  2520. out_err:
  2521. cgroup_migrate_finish(&preloaded_csets);
  2522. mutex_unlock(&cgroup_mutex);
  2523. return ret;
  2524. }
  2525. /*
  2526. * Stuff for reading the 'tasks'/'procs' files.
  2527. *
  2528. * Reading this file can return large amounts of data if a cgroup has
  2529. * *lots* of attached tasks. So it may need several calls to read(),
  2530. * but we cannot guarantee that the information we produce is correct
  2531. * unless we produce it entirely atomically.
  2532. *
  2533. */
  2534. /* which pidlist file are we talking about? */
  2535. enum cgroup_filetype {
  2536. CGROUP_FILE_PROCS,
  2537. CGROUP_FILE_TASKS,
  2538. };
  2539. /*
  2540. * A pidlist is a list of pids that virtually represents the contents of one
  2541. * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
  2542. * a pair (one each for procs, tasks) for each pid namespace that's relevant
  2543. * to the cgroup.
  2544. */
  2545. struct cgroup_pidlist {
  2546. /*
  2547. * used to find which pidlist is wanted. doesn't change as long as
  2548. * this particular list stays in the list.
  2549. */
  2550. struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
  2551. /* array of xids */
  2552. pid_t *list;
  2553. /* how many elements the above list has */
  2554. int length;
  2555. /* each of these stored in a list by its cgroup */
  2556. struct list_head links;
  2557. /* pointer to the cgroup we belong to, for list removal purposes */
  2558. struct cgroup *owner;
  2559. /* for delayed destruction */
  2560. struct delayed_work destroy_dwork;
  2561. };
  2562. /*
  2563. * The following two functions "fix" the issue where there are more pids
  2564. * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
  2565. * TODO: replace with a kernel-wide solution to this problem
  2566. */
  2567. #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
  2568. static void *pidlist_allocate(int count)
  2569. {
  2570. if (PIDLIST_TOO_LARGE(count))
  2571. return vmalloc(count * sizeof(pid_t));
  2572. else
  2573. return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
  2574. }
  2575. static void pidlist_free(void *p)
  2576. {
  2577. if (is_vmalloc_addr(p))
  2578. vfree(p);
  2579. else
  2580. kfree(p);
  2581. }
  2582. /*
  2583. * Used to destroy all pidlists lingering waiting for destroy timer. None
  2584. * should be left afterwards.
  2585. */
  2586. static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
  2587. {
  2588. struct cgroup_pidlist *l, *tmp_l;
  2589. mutex_lock(&cgrp->pidlist_mutex);
  2590. list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
  2591. mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
  2592. mutex_unlock(&cgrp->pidlist_mutex);
  2593. flush_workqueue(cgroup_pidlist_destroy_wq);
  2594. BUG_ON(!list_empty(&cgrp->pidlists));
  2595. }
  2596. static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
  2597. {
  2598. struct delayed_work *dwork = to_delayed_work(work);
  2599. struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
  2600. destroy_dwork);
  2601. struct cgroup_pidlist *tofree = NULL;
  2602. mutex_lock(&l->owner->pidlist_mutex);
  2603. /*
  2604. * Destroy iff we didn't get queued again. The state won't change
  2605. * as destroy_dwork can only be queued while locked.
  2606. */
  2607. if (!delayed_work_pending(dwork)) {
  2608. list_del(&l->links);
  2609. pidlist_free(l->list);
  2610. put_pid_ns(l->key.ns);
  2611. tofree = l;
  2612. }
  2613. mutex_unlock(&l->owner->pidlist_mutex);
  2614. kfree(tofree);
  2615. }
  2616. /*
  2617. * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
  2618. * Returns the number of unique elements.
  2619. */
  2620. static int pidlist_uniq(pid_t *list, int length)
  2621. {
  2622. int src, dest = 1;
  2623. /*
  2624. * we presume the 0th element is unique, so i starts at 1. trivial
  2625. * edge cases first; no work needs to be done for either
  2626. */
  2627. if (length == 0 || length == 1)
  2628. return length;
  2629. /* src and dest walk down the list; dest counts unique elements */
  2630. for (src = 1; src < length; src++) {
  2631. /* find next unique element */
  2632. while (list[src] == list[src-1]) {
  2633. src++;
  2634. if (src == length)
  2635. goto after;
  2636. }
  2637. /* dest always points to where the next unique element goes */
  2638. list[dest] = list[src];
  2639. dest++;
  2640. }
  2641. after:
  2642. return dest;
  2643. }
  2644. /*
  2645. * The two pid files - task and cgroup.procs - guaranteed that the result
  2646. * is sorted, which forced this whole pidlist fiasco. As pid order is
  2647. * different per namespace, each namespace needs differently sorted list,
  2648. * making it impossible to use, for example, single rbtree of member tasks
  2649. * sorted by task pointer. As pidlists can be fairly large, allocating one
  2650. * per open file is dangerous, so cgroup had to implement shared pool of
  2651. * pidlists keyed by cgroup and namespace.
  2652. *
  2653. * All this extra complexity was caused by the original implementation
  2654. * committing to an entirely unnecessary property. In the long term, we
  2655. * want to do away with it. Explicitly scramble sort order if
  2656. * sane_behavior so that no such expectation exists in the new interface.
  2657. *
  2658. * Scrambling is done by swapping every two consecutive bits, which is
  2659. * non-identity one-to-one mapping which disturbs sort order sufficiently.
  2660. */
  2661. static pid_t pid_fry(pid_t pid)
  2662. {
  2663. unsigned a = pid & 0x55555555;
  2664. unsigned b = pid & 0xAAAAAAAA;
  2665. return (a << 1) | (b >> 1);
  2666. }
  2667. static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
  2668. {
  2669. if (cgroup_sane_behavior(cgrp))
  2670. return pid_fry(pid);
  2671. else
  2672. return pid;
  2673. }
  2674. static int cmppid(const void *a, const void *b)
  2675. {
  2676. return *(pid_t *)a - *(pid_t *)b;
  2677. }
  2678. static int fried_cmppid(const void *a, const void *b)
  2679. {
  2680. return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
  2681. }
  2682. static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
  2683. enum cgroup_filetype type)
  2684. {
  2685. struct cgroup_pidlist *l;
  2686. /* don't need task_nsproxy() if we're looking at ourself */
  2687. struct pid_namespace *ns = task_active_pid_ns(current);
  2688. lockdep_assert_held(&cgrp->pidlist_mutex);
  2689. list_for_each_entry(l, &cgrp->pidlists, links)
  2690. if (l->key.type == type && l->key.ns == ns)
  2691. return l;
  2692. return NULL;
  2693. }
  2694. /*
  2695. * find the appropriate pidlist for our purpose (given procs vs tasks)
  2696. * returns with the lock on that pidlist already held, and takes care
  2697. * of the use count, or returns NULL with no locks held if we're out of
  2698. * memory.
  2699. */
  2700. static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
  2701. enum cgroup_filetype type)
  2702. {
  2703. struct cgroup_pidlist *l;
  2704. lockdep_assert_held(&cgrp->pidlist_mutex);
  2705. l = cgroup_pidlist_find(cgrp, type);
  2706. if (l)
  2707. return l;
  2708. /* entry not found; create a new one */
  2709. l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
  2710. if (!l)
  2711. return l;
  2712. INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
  2713. l->key.type = type;
  2714. /* don't need task_nsproxy() if we're looking at ourself */
  2715. l->key.ns = get_pid_ns(task_active_pid_ns(current));
  2716. l->owner = cgrp;
  2717. list_add(&l->links, &cgrp->pidlists);
  2718. return l;
  2719. }
  2720. /*
  2721. * Load a cgroup's pidarray with either procs' tgids or tasks' pids
  2722. */
  2723. static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
  2724. struct cgroup_pidlist **lp)
  2725. {
  2726. pid_t *array;
  2727. int length;
  2728. int pid, n = 0; /* used for populating the array */
  2729. struct css_task_iter it;
  2730. struct task_struct *tsk;
  2731. struct cgroup_pidlist *l;
  2732. lockdep_assert_held(&cgrp->pidlist_mutex);
  2733. /*
  2734. * If cgroup gets more users after we read count, we won't have
  2735. * enough space - tough. This race is indistinguishable to the
  2736. * caller from the case that the additional cgroup users didn't
  2737. * show up until sometime later on.
  2738. */
  2739. length = cgroup_task_count(cgrp);
  2740. array = pidlist_allocate(length);
  2741. if (!array)
  2742. return -ENOMEM;
  2743. /* now, populate the array */
  2744. css_task_iter_start(&cgrp->dummy_css, &it);
  2745. while ((tsk = css_task_iter_next(&it))) {
  2746. if (unlikely(n == length))
  2747. break;
  2748. /* get tgid or pid for procs or tasks file respectively */
  2749. if (type == CGROUP_FILE_PROCS)
  2750. pid = task_tgid_vnr(tsk);
  2751. else
  2752. pid = task_pid_vnr(tsk);
  2753. if (pid > 0) /* make sure to only use valid results */
  2754. array[n++] = pid;
  2755. }
  2756. css_task_iter_end(&it);
  2757. length = n;
  2758. /* now sort & (if procs) strip out duplicates */
  2759. if (cgroup_sane_behavior(cgrp))
  2760. sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
  2761. else
  2762. sort(array, length, sizeof(pid_t), cmppid, NULL);
  2763. if (type == CGROUP_FILE_PROCS)
  2764. length = pidlist_uniq(array, length);
  2765. l = cgroup_pidlist_find_create(cgrp, type);
  2766. if (!l) {
  2767. mutex_unlock(&cgrp->pidlist_mutex);
  2768. pidlist_free(array);
  2769. return -ENOMEM;
  2770. }
  2771. /* store array, freeing old if necessary */
  2772. pidlist_free(l->list);
  2773. l->list = array;
  2774. l->length = length;
  2775. *lp = l;
  2776. return 0;
  2777. }
  2778. /**
  2779. * cgroupstats_build - build and fill cgroupstats
  2780. * @stats: cgroupstats to fill information into
  2781. * @dentry: A dentry entry belonging to the cgroup for which stats have
  2782. * been requested.
  2783. *
  2784. * Build and fill cgroupstats so that taskstats can export it to user
  2785. * space.
  2786. */
  2787. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
  2788. {
  2789. struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
  2790. struct cgroup *cgrp;
  2791. struct css_task_iter it;
  2792. struct task_struct *tsk;
  2793. /* it should be kernfs_node belonging to cgroupfs and is a directory */
  2794. if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
  2795. kernfs_type(kn) != KERNFS_DIR)
  2796. return -EINVAL;
  2797. mutex_lock(&cgroup_mutex);
  2798. /*
  2799. * We aren't being called from kernfs and there's no guarantee on
  2800. * @kn->priv's validity. For this and css_tryget_from_dir(),
  2801. * @kn->priv is RCU safe. Let's do the RCU dancing.
  2802. */
  2803. rcu_read_lock();
  2804. cgrp = rcu_dereference(kn->priv);
  2805. if (!cgrp || cgroup_is_dead(cgrp)) {
  2806. rcu_read_unlock();
  2807. mutex_unlock(&cgroup_mutex);
  2808. return -ENOENT;
  2809. }
  2810. rcu_read_unlock();
  2811. css_task_iter_start(&cgrp->dummy_css, &it);
  2812. while ((tsk = css_task_iter_next(&it))) {
  2813. switch (tsk->state) {
  2814. case TASK_RUNNING:
  2815. stats->nr_running++;
  2816. break;
  2817. case TASK_INTERRUPTIBLE:
  2818. stats->nr_sleeping++;
  2819. break;
  2820. case TASK_UNINTERRUPTIBLE:
  2821. stats->nr_uninterruptible++;
  2822. break;
  2823. case TASK_STOPPED:
  2824. stats->nr_stopped++;
  2825. break;
  2826. default:
  2827. if (delayacct_is_task_waiting_on_io(tsk))
  2828. stats->nr_io_wait++;
  2829. break;
  2830. }
  2831. }
  2832. css_task_iter_end(&it);
  2833. mutex_unlock(&cgroup_mutex);
  2834. return 0;
  2835. }
  2836. /*
  2837. * seq_file methods for the tasks/procs files. The seq_file position is the
  2838. * next pid to display; the seq_file iterator is a pointer to the pid
  2839. * in the cgroup->l->list array.
  2840. */
  2841. static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
  2842. {
  2843. /*
  2844. * Initially we receive a position value that corresponds to
  2845. * one more than the last pid shown (or 0 on the first call or
  2846. * after a seek to the start). Use a binary-search to find the
  2847. * next pid to display, if any
  2848. */
  2849. struct kernfs_open_file *of = s->private;
  2850. struct cgroup *cgrp = seq_css(s)->cgroup;
  2851. struct cgroup_pidlist *l;
  2852. enum cgroup_filetype type = seq_cft(s)->private;
  2853. int index = 0, pid = *pos;
  2854. int *iter, ret;
  2855. mutex_lock(&cgrp->pidlist_mutex);
  2856. /*
  2857. * !NULL @of->priv indicates that this isn't the first start()
  2858. * after open. If the matching pidlist is around, we can use that.
  2859. * Look for it. Note that @of->priv can't be used directly. It
  2860. * could already have been destroyed.
  2861. */
  2862. if (of->priv)
  2863. of->priv = cgroup_pidlist_find(cgrp, type);
  2864. /*
  2865. * Either this is the first start() after open or the matching
  2866. * pidlist has been destroyed inbetween. Create a new one.
  2867. */
  2868. if (!of->priv) {
  2869. ret = pidlist_array_load(cgrp, type,
  2870. (struct cgroup_pidlist **)&of->priv);
  2871. if (ret)
  2872. return ERR_PTR(ret);
  2873. }
  2874. l = of->priv;
  2875. if (pid) {
  2876. int end = l->length;
  2877. while (index < end) {
  2878. int mid = (index + end) / 2;
  2879. if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
  2880. index = mid;
  2881. break;
  2882. } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
  2883. index = mid + 1;
  2884. else
  2885. end = mid;
  2886. }
  2887. }
  2888. /* If we're off the end of the array, we're done */
  2889. if (index >= l->length)
  2890. return NULL;
  2891. /* Update the abstract position to be the actual pid that we found */
  2892. iter = l->list + index;
  2893. *pos = cgroup_pid_fry(cgrp, *iter);
  2894. return iter;
  2895. }
  2896. static void cgroup_pidlist_stop(struct seq_file *s, void *v)
  2897. {
  2898. struct kernfs_open_file *of = s->private;
  2899. struct cgroup_pidlist *l = of->priv;
  2900. if (l)
  2901. mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
  2902. CGROUP_PIDLIST_DESTROY_DELAY);
  2903. mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
  2904. }
  2905. static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
  2906. {
  2907. struct kernfs_open_file *of = s->private;
  2908. struct cgroup_pidlist *l = of->priv;
  2909. pid_t *p = v;
  2910. pid_t *end = l->list + l->length;
  2911. /*
  2912. * Advance to the next pid in the array. If this goes off the
  2913. * end, we're done
  2914. */
  2915. p++;
  2916. if (p >= end) {
  2917. return NULL;
  2918. } else {
  2919. *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
  2920. return p;
  2921. }
  2922. }
  2923. static int cgroup_pidlist_show(struct seq_file *s, void *v)
  2924. {
  2925. return seq_printf(s, "%d\n", *(int *)v);
  2926. }
  2927. /*
  2928. * seq_operations functions for iterating on pidlists through seq_file -
  2929. * independent of whether it's tasks or procs
  2930. */
  2931. static const struct seq_operations cgroup_pidlist_seq_operations = {
  2932. .start = cgroup_pidlist_start,
  2933. .stop = cgroup_pidlist_stop,
  2934. .next = cgroup_pidlist_next,
  2935. .show = cgroup_pidlist_show,
  2936. };
  2937. static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
  2938. struct cftype *cft)
  2939. {
  2940. return notify_on_release(css->cgroup);
  2941. }
  2942. static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
  2943. struct cftype *cft, u64 val)
  2944. {
  2945. clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
  2946. if (val)
  2947. set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
  2948. else
  2949. clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
  2950. return 0;
  2951. }
  2952. static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
  2953. struct cftype *cft)
  2954. {
  2955. return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  2956. }
  2957. static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
  2958. struct cftype *cft, u64 val)
  2959. {
  2960. if (val)
  2961. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  2962. else
  2963. clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  2964. return 0;
  2965. }
  2966. static struct cftype cgroup_base_files[] = {
  2967. {
  2968. .name = "cgroup.procs",
  2969. .seq_start = cgroup_pidlist_start,
  2970. .seq_next = cgroup_pidlist_next,
  2971. .seq_stop = cgroup_pidlist_stop,
  2972. .seq_show = cgroup_pidlist_show,
  2973. .private = CGROUP_FILE_PROCS,
  2974. .write_u64 = cgroup_procs_write,
  2975. .mode = S_IRUGO | S_IWUSR,
  2976. },
  2977. {
  2978. .name = "cgroup.clone_children",
  2979. .flags = CFTYPE_INSANE,
  2980. .read_u64 = cgroup_clone_children_read,
  2981. .write_u64 = cgroup_clone_children_write,
  2982. },
  2983. {
  2984. .name = "cgroup.sane_behavior",
  2985. .flags = CFTYPE_ONLY_ON_ROOT,
  2986. .seq_show = cgroup_sane_behavior_show,
  2987. },
  2988. /*
  2989. * Historical crazy stuff. These don't have "cgroup." prefix and
  2990. * don't exist if sane_behavior. If you're depending on these, be
  2991. * prepared to be burned.
  2992. */
  2993. {
  2994. .name = "tasks",
  2995. .flags = CFTYPE_INSANE, /* use "procs" instead */
  2996. .seq_start = cgroup_pidlist_start,
  2997. .seq_next = cgroup_pidlist_next,
  2998. .seq_stop = cgroup_pidlist_stop,
  2999. .seq_show = cgroup_pidlist_show,
  3000. .private = CGROUP_FILE_TASKS,
  3001. .write_u64 = cgroup_tasks_write,
  3002. .mode = S_IRUGO | S_IWUSR,
  3003. },
  3004. {
  3005. .name = "notify_on_release",
  3006. .flags = CFTYPE_INSANE,
  3007. .read_u64 = cgroup_read_notify_on_release,
  3008. .write_u64 = cgroup_write_notify_on_release,
  3009. },
  3010. {
  3011. .name = "release_agent",
  3012. .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
  3013. .seq_show = cgroup_release_agent_show,
  3014. .write_string = cgroup_release_agent_write,
  3015. .max_write_len = PATH_MAX - 1,
  3016. },
  3017. { } /* terminate */
  3018. };
  3019. /**
  3020. * cgroup_populate_dir - create subsys files in a cgroup directory
  3021. * @cgrp: target cgroup
  3022. * @subsys_mask: mask of the subsystem ids whose files should be added
  3023. *
  3024. * On failure, no file is added.
  3025. */
  3026. static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
  3027. {
  3028. struct cgroup_subsys *ss;
  3029. int i, ret = 0;
  3030. /* process cftsets of each subsystem */
  3031. for_each_subsys(ss, i) {
  3032. struct cftype *cfts;
  3033. if (!test_bit(i, &subsys_mask))
  3034. continue;
  3035. list_for_each_entry(cfts, &ss->cfts, node) {
  3036. ret = cgroup_addrm_files(cgrp, cfts, true);
  3037. if (ret < 0)
  3038. goto err;
  3039. }
  3040. }
  3041. return 0;
  3042. err:
  3043. cgroup_clear_dir(cgrp, subsys_mask);
  3044. return ret;
  3045. }
  3046. /*
  3047. * css destruction is four-stage process.
  3048. *
  3049. * 1. Destruction starts. Killing of the percpu_ref is initiated.
  3050. * Implemented in kill_css().
  3051. *
  3052. * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
  3053. * and thus css_tryget() is guaranteed to fail, the css can be offlined
  3054. * by invoking offline_css(). After offlining, the base ref is put.
  3055. * Implemented in css_killed_work_fn().
  3056. *
  3057. * 3. When the percpu_ref reaches zero, the only possible remaining
  3058. * accessors are inside RCU read sections. css_release() schedules the
  3059. * RCU callback.
  3060. *
  3061. * 4. After the grace period, the css can be freed. Implemented in
  3062. * css_free_work_fn().
  3063. *
  3064. * It is actually hairier because both step 2 and 4 require process context
  3065. * and thus involve punting to css->destroy_work adding two additional
  3066. * steps to the already complex sequence.
  3067. */
  3068. static void css_free_work_fn(struct work_struct *work)
  3069. {
  3070. struct cgroup_subsys_state *css =
  3071. container_of(work, struct cgroup_subsys_state, destroy_work);
  3072. struct cgroup *cgrp = css->cgroup;
  3073. if (css->parent)
  3074. css_put(css->parent);
  3075. css->ss->css_free(css);
  3076. cgroup_put(cgrp);
  3077. }
  3078. static void css_free_rcu_fn(struct rcu_head *rcu_head)
  3079. {
  3080. struct cgroup_subsys_state *css =
  3081. container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
  3082. INIT_WORK(&css->destroy_work, css_free_work_fn);
  3083. queue_work(cgroup_destroy_wq, &css->destroy_work);
  3084. }
  3085. static void css_release(struct percpu_ref *ref)
  3086. {
  3087. struct cgroup_subsys_state *css =
  3088. container_of(ref, struct cgroup_subsys_state, refcnt);
  3089. RCU_INIT_POINTER(css->cgroup->subsys[css->ss->id], NULL);
  3090. call_rcu(&css->rcu_head, css_free_rcu_fn);
  3091. }
  3092. static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
  3093. struct cgroup *cgrp)
  3094. {
  3095. css->cgroup = cgrp;
  3096. css->ss = ss;
  3097. css->flags = 0;
  3098. if (cgrp->parent)
  3099. css->parent = cgroup_css(cgrp->parent, ss);
  3100. else
  3101. css->flags |= CSS_ROOT;
  3102. BUG_ON(cgroup_css(cgrp, ss));
  3103. }
  3104. /* invoke ->css_online() on a new CSS and mark it online if successful */
  3105. static int online_css(struct cgroup_subsys_state *css)
  3106. {
  3107. struct cgroup_subsys *ss = css->ss;
  3108. int ret = 0;
  3109. lockdep_assert_held(&cgroup_tree_mutex);
  3110. lockdep_assert_held(&cgroup_mutex);
  3111. if (ss->css_online)
  3112. ret = ss->css_online(css);
  3113. if (!ret) {
  3114. css->flags |= CSS_ONLINE;
  3115. css->cgroup->nr_css++;
  3116. rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
  3117. }
  3118. return ret;
  3119. }
  3120. /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
  3121. static void offline_css(struct cgroup_subsys_state *css)
  3122. {
  3123. struct cgroup_subsys *ss = css->ss;
  3124. lockdep_assert_held(&cgroup_tree_mutex);
  3125. lockdep_assert_held(&cgroup_mutex);
  3126. if (!(css->flags & CSS_ONLINE))
  3127. return;
  3128. if (ss->css_offline)
  3129. ss->css_offline(css);
  3130. css->flags &= ~CSS_ONLINE;
  3131. css->cgroup->nr_css--;
  3132. RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
  3133. }
  3134. /**
  3135. * create_css - create a cgroup_subsys_state
  3136. * @cgrp: the cgroup new css will be associated with
  3137. * @ss: the subsys of new css
  3138. *
  3139. * Create a new css associated with @cgrp - @ss pair. On success, the new
  3140. * css is online and installed in @cgrp with all interface files created.
  3141. * Returns 0 on success, -errno on failure.
  3142. */
  3143. static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
  3144. {
  3145. struct cgroup *parent = cgrp->parent;
  3146. struct cgroup_subsys_state *css;
  3147. int err;
  3148. lockdep_assert_held(&cgroup_mutex);
  3149. css = ss->css_alloc(cgroup_css(parent, ss));
  3150. if (IS_ERR(css))
  3151. return PTR_ERR(css);
  3152. err = percpu_ref_init(&css->refcnt, css_release);
  3153. if (err)
  3154. goto err_free_css;
  3155. init_css(css, ss, cgrp);
  3156. err = cgroup_populate_dir(cgrp, 1 << ss->id);
  3157. if (err)
  3158. goto err_free_percpu_ref;
  3159. err = online_css(css);
  3160. if (err)
  3161. goto err_clear_dir;
  3162. cgroup_get(cgrp);
  3163. css_get(css->parent);
  3164. cgrp->subsys_mask |= 1 << ss->id;
  3165. if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
  3166. parent->parent) {
  3167. 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",
  3168. current->comm, current->pid, ss->name);
  3169. if (!strcmp(ss->name, "memory"))
  3170. pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
  3171. ss->warned_broken_hierarchy = true;
  3172. }
  3173. return 0;
  3174. err_clear_dir:
  3175. cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
  3176. err_free_percpu_ref:
  3177. percpu_ref_cancel_init(&css->refcnt);
  3178. err_free_css:
  3179. ss->css_free(css);
  3180. return err;
  3181. }
  3182. /**
  3183. * cgroup_create - create a cgroup
  3184. * @parent: cgroup that will be parent of the new cgroup
  3185. * @name: name of the new cgroup
  3186. * @mode: mode to set on new cgroup
  3187. */
  3188. static long cgroup_create(struct cgroup *parent, const char *name,
  3189. umode_t mode)
  3190. {
  3191. struct cgroup *cgrp;
  3192. struct cgroup_root *root = parent->root;
  3193. int ssid, err;
  3194. struct cgroup_subsys *ss;
  3195. struct kernfs_node *kn;
  3196. /*
  3197. * XXX: The default hierarchy isn't fully implemented yet. Block
  3198. * !root cgroup creation on it for now.
  3199. */
  3200. if (root == &cgrp_dfl_root)
  3201. return -EINVAL;
  3202. /* allocate the cgroup and its ID, 0 is reserved for the root */
  3203. cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
  3204. if (!cgrp)
  3205. return -ENOMEM;
  3206. mutex_lock(&cgroup_tree_mutex);
  3207. /*
  3208. * Only live parents can have children. Note that the liveliness
  3209. * check isn't strictly necessary because cgroup_mkdir() and
  3210. * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
  3211. * anyway so that locking is contained inside cgroup proper and we
  3212. * don't get nasty surprises if we ever grow another caller.
  3213. */
  3214. if (!cgroup_lock_live_group(parent)) {
  3215. err = -ENODEV;
  3216. goto err_unlock_tree;
  3217. }
  3218. /*
  3219. * Temporarily set the pointer to NULL, so idr_find() won't return
  3220. * a half-baked cgroup.
  3221. */
  3222. cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
  3223. if (cgrp->id < 0) {
  3224. err = -ENOMEM;
  3225. goto err_unlock;
  3226. }
  3227. init_cgroup_housekeeping(cgrp);
  3228. cgrp->parent = parent;
  3229. cgrp->dummy_css.parent = &parent->dummy_css;
  3230. cgrp->root = parent->root;
  3231. if (notify_on_release(parent))
  3232. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  3233. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
  3234. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
  3235. /* create the directory */
  3236. kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
  3237. if (IS_ERR(kn)) {
  3238. err = PTR_ERR(kn);
  3239. goto err_free_id;
  3240. }
  3241. cgrp->kn = kn;
  3242. /*
  3243. * This extra ref will be put in cgroup_free_fn() and guarantees
  3244. * that @cgrp->kn is always accessible.
  3245. */
  3246. kernfs_get(kn);
  3247. cgrp->serial_nr = cgroup_serial_nr_next++;
  3248. /* allocation complete, commit to creation */
  3249. list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
  3250. atomic_inc(&root->nr_cgrps);
  3251. cgroup_get(parent);
  3252. /*
  3253. * @cgrp is now fully operational. If something fails after this
  3254. * point, it'll be released via the normal destruction path.
  3255. */
  3256. idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
  3257. err = cgroup_kn_set_ugid(kn);
  3258. if (err)
  3259. goto err_destroy;
  3260. err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
  3261. if (err)
  3262. goto err_destroy;
  3263. /* let's create and online css's */
  3264. for_each_subsys(ss, ssid) {
  3265. if (root->cgrp.subsys_mask & (1 << ssid)) {
  3266. err = create_css(cgrp, ss);
  3267. if (err)
  3268. goto err_destroy;
  3269. }
  3270. }
  3271. kernfs_activate(kn);
  3272. mutex_unlock(&cgroup_mutex);
  3273. mutex_unlock(&cgroup_tree_mutex);
  3274. return 0;
  3275. err_free_id:
  3276. idr_remove(&root->cgroup_idr, cgrp->id);
  3277. err_unlock:
  3278. mutex_unlock(&cgroup_mutex);
  3279. err_unlock_tree:
  3280. mutex_unlock(&cgroup_tree_mutex);
  3281. kfree(cgrp);
  3282. return err;
  3283. err_destroy:
  3284. cgroup_destroy_locked(cgrp);
  3285. mutex_unlock(&cgroup_mutex);
  3286. mutex_unlock(&cgroup_tree_mutex);
  3287. return err;
  3288. }
  3289. static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
  3290. umode_t mode)
  3291. {
  3292. struct cgroup *parent = parent_kn->priv;
  3293. int ret;
  3294. /*
  3295. * cgroup_create() grabs cgroup_tree_mutex which nests outside
  3296. * kernfs active_ref and cgroup_create() already synchronizes
  3297. * properly against removal through cgroup_lock_live_group().
  3298. * Break it before calling cgroup_create().
  3299. */
  3300. cgroup_get(parent);
  3301. kernfs_break_active_protection(parent_kn);
  3302. ret = cgroup_create(parent, name, mode);
  3303. kernfs_unbreak_active_protection(parent_kn);
  3304. cgroup_put(parent);
  3305. return ret;
  3306. }
  3307. /*
  3308. * This is called when the refcnt of a css is confirmed to be killed.
  3309. * css_tryget() is now guaranteed to fail.
  3310. */
  3311. static void css_killed_work_fn(struct work_struct *work)
  3312. {
  3313. struct cgroup_subsys_state *css =
  3314. container_of(work, struct cgroup_subsys_state, destroy_work);
  3315. struct cgroup *cgrp = css->cgroup;
  3316. mutex_lock(&cgroup_tree_mutex);
  3317. mutex_lock(&cgroup_mutex);
  3318. /*
  3319. * css_tryget() is guaranteed to fail now. Tell subsystems to
  3320. * initate destruction.
  3321. */
  3322. offline_css(css);
  3323. /*
  3324. * If @cgrp is marked dead, it's waiting for refs of all css's to
  3325. * be disabled before proceeding to the second phase of cgroup
  3326. * destruction. If we are the last one, kick it off.
  3327. */
  3328. if (!cgrp->nr_css && cgroup_is_dead(cgrp))
  3329. cgroup_destroy_css_killed(cgrp);
  3330. mutex_unlock(&cgroup_mutex);
  3331. mutex_unlock(&cgroup_tree_mutex);
  3332. /*
  3333. * Put the css refs from kill_css(). Each css holds an extra
  3334. * reference to the cgroup's dentry and cgroup removal proceeds
  3335. * regardless of css refs. On the last put of each css, whenever
  3336. * that may be, the extra dentry ref is put so that dentry
  3337. * destruction happens only after all css's are released.
  3338. */
  3339. css_put(css);
  3340. }
  3341. /* css kill confirmation processing requires process context, bounce */
  3342. static void css_killed_ref_fn(struct percpu_ref *ref)
  3343. {
  3344. struct cgroup_subsys_state *css =
  3345. container_of(ref, struct cgroup_subsys_state, refcnt);
  3346. INIT_WORK(&css->destroy_work, css_killed_work_fn);
  3347. queue_work(cgroup_destroy_wq, &css->destroy_work);
  3348. }
  3349. static void __kill_css(struct cgroup_subsys_state *css)
  3350. {
  3351. lockdep_assert_held(&cgroup_tree_mutex);
  3352. /*
  3353. * This must happen before css is disassociated with its cgroup.
  3354. * See seq_css() for details.
  3355. */
  3356. cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
  3357. /*
  3358. * Killing would put the base ref, but we need to keep it alive
  3359. * until after ->css_offline().
  3360. */
  3361. css_get(css);
  3362. /*
  3363. * cgroup core guarantees that, by the time ->css_offline() is
  3364. * invoked, no new css reference will be given out via
  3365. * css_tryget(). We can't simply call percpu_ref_kill() and
  3366. * proceed to offlining css's because percpu_ref_kill() doesn't
  3367. * guarantee that the ref is seen as killed on all CPUs on return.
  3368. *
  3369. * Use percpu_ref_kill_and_confirm() to get notifications as each
  3370. * css is confirmed to be seen as killed on all CPUs.
  3371. */
  3372. percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
  3373. }
  3374. /**
  3375. * kill_css - destroy a css
  3376. * @css: css to destroy
  3377. *
  3378. * This function initiates destruction of @css by removing cgroup interface
  3379. * files and putting its base reference. ->css_offline() will be invoked
  3380. * asynchronously once css_tryget() is guaranteed to fail and when the
  3381. * reference count reaches zero, @css will be released.
  3382. */
  3383. static void kill_css(struct cgroup_subsys_state *css)
  3384. {
  3385. struct cgroup *cgrp = css->cgroup;
  3386. lockdep_assert_held(&cgroup_tree_mutex);
  3387. /* if already killed, noop */
  3388. if (cgrp->subsys_mask & (1 << css->ss->id)) {
  3389. cgrp->subsys_mask &= ~(1 << css->ss->id);
  3390. __kill_css(css);
  3391. }
  3392. }
  3393. /**
  3394. * cgroup_destroy_locked - the first stage of cgroup destruction
  3395. * @cgrp: cgroup to be destroyed
  3396. *
  3397. * css's make use of percpu refcnts whose killing latency shouldn't be
  3398. * exposed to userland and are RCU protected. Also, cgroup core needs to
  3399. * guarantee that css_tryget() won't succeed by the time ->css_offline() is
  3400. * invoked. To satisfy all the requirements, destruction is implemented in
  3401. * the following two steps.
  3402. *
  3403. * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
  3404. * userland visible parts and start killing the percpu refcnts of
  3405. * css's. Set up so that the next stage will be kicked off once all
  3406. * the percpu refcnts are confirmed to be killed.
  3407. *
  3408. * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
  3409. * rest of destruction. Once all cgroup references are gone, the
  3410. * cgroup is RCU-freed.
  3411. *
  3412. * This function implements s1. After this step, @cgrp is gone as far as
  3413. * the userland is concerned and a new cgroup with the same name may be
  3414. * created. As cgroup doesn't care about the names internally, this
  3415. * doesn't cause any problem.
  3416. */
  3417. static int cgroup_destroy_locked(struct cgroup *cgrp)
  3418. __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
  3419. {
  3420. struct cgroup *child;
  3421. struct cgroup_subsys_state *css;
  3422. bool empty;
  3423. int ssid;
  3424. lockdep_assert_held(&cgroup_tree_mutex);
  3425. lockdep_assert_held(&cgroup_mutex);
  3426. /*
  3427. * css_set_rwsem synchronizes access to ->cset_links and prevents
  3428. * @cgrp from being removed while put_css_set() is in progress.
  3429. */
  3430. down_read(&css_set_rwsem);
  3431. empty = list_empty(&cgrp->cset_links);
  3432. up_read(&css_set_rwsem);
  3433. if (!empty)
  3434. return -EBUSY;
  3435. /*
  3436. * Make sure there's no live children. We can't test ->children
  3437. * emptiness as dead children linger on it while being destroyed;
  3438. * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
  3439. */
  3440. empty = true;
  3441. rcu_read_lock();
  3442. list_for_each_entry_rcu(child, &cgrp->children, sibling) {
  3443. empty = cgroup_is_dead(child);
  3444. if (!empty)
  3445. break;
  3446. }
  3447. rcu_read_unlock();
  3448. if (!empty)
  3449. return -EBUSY;
  3450. /*
  3451. * Mark @cgrp dead. This prevents further task migration and child
  3452. * creation by disabling cgroup_lock_live_group(). Note that
  3453. * CGRP_DEAD assertion is depended upon by css_next_child() to
  3454. * resume iteration after dropping RCU read lock. See
  3455. * css_next_child() for details.
  3456. */
  3457. set_bit(CGRP_DEAD, &cgrp->flags);
  3458. /*
  3459. * Initiate massacre of all css's. cgroup_destroy_css_killed()
  3460. * will be invoked to perform the rest of destruction once the
  3461. * percpu refs of all css's are confirmed to be killed. This
  3462. * involves removing the subsystem's files, drop cgroup_mutex.
  3463. */
  3464. mutex_unlock(&cgroup_mutex);
  3465. for_each_css(css, ssid, cgrp)
  3466. kill_css(css);
  3467. mutex_lock(&cgroup_mutex);
  3468. /* CGRP_DEAD is set, remove from ->release_list for the last time */
  3469. raw_spin_lock(&release_list_lock);
  3470. if (!list_empty(&cgrp->release_list))
  3471. list_del_init(&cgrp->release_list);
  3472. raw_spin_unlock(&release_list_lock);
  3473. /*
  3474. * If @cgrp has css's attached, the second stage of cgroup
  3475. * destruction is kicked off from css_killed_work_fn() after the
  3476. * refs of all attached css's are killed. If @cgrp doesn't have
  3477. * any css, we kick it off here.
  3478. */
  3479. if (!cgrp->nr_css)
  3480. cgroup_destroy_css_killed(cgrp);
  3481. /* remove @cgrp directory along with the base files */
  3482. mutex_unlock(&cgroup_mutex);
  3483. /*
  3484. * There are two control paths which try to determine cgroup from
  3485. * dentry without going through kernfs - cgroupstats_build() and
  3486. * css_tryget_from_dir(). Those are supported by RCU protecting
  3487. * clearing of cgrp->kn->priv backpointer, which should happen
  3488. * after all files under it have been removed.
  3489. */
  3490. kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
  3491. RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
  3492. mutex_lock(&cgroup_mutex);
  3493. return 0;
  3494. };
  3495. /**
  3496. * cgroup_destroy_css_killed - the second step of cgroup destruction
  3497. * @work: cgroup->destroy_free_work
  3498. *
  3499. * This function is invoked from a work item for a cgroup which is being
  3500. * destroyed after all css's are offlined and performs the rest of
  3501. * destruction. This is the second step of destruction described in the
  3502. * comment above cgroup_destroy_locked().
  3503. */
  3504. static void cgroup_destroy_css_killed(struct cgroup *cgrp)
  3505. {
  3506. struct cgroup *parent = cgrp->parent;
  3507. lockdep_assert_held(&cgroup_tree_mutex);
  3508. lockdep_assert_held(&cgroup_mutex);
  3509. /* delete this cgroup from parent->children */
  3510. list_del_rcu(&cgrp->sibling);
  3511. cgroup_put(cgrp);
  3512. set_bit(CGRP_RELEASABLE, &parent->flags);
  3513. check_for_release(parent);
  3514. }
  3515. static int cgroup_rmdir(struct kernfs_node *kn)
  3516. {
  3517. struct cgroup *cgrp = kn->priv;
  3518. int ret = 0;
  3519. /*
  3520. * This is self-destruction but @kn can't be removed while this
  3521. * callback is in progress. Let's break active protection. Once
  3522. * the protection is broken, @cgrp can be destroyed at any point.
  3523. * Pin it so that it stays accessible.
  3524. */
  3525. cgroup_get(cgrp);
  3526. kernfs_break_active_protection(kn);
  3527. mutex_lock(&cgroup_tree_mutex);
  3528. mutex_lock(&cgroup_mutex);
  3529. /*
  3530. * @cgrp might already have been destroyed while we're trying to
  3531. * grab the mutexes.
  3532. */
  3533. if (!cgroup_is_dead(cgrp))
  3534. ret = cgroup_destroy_locked(cgrp);
  3535. mutex_unlock(&cgroup_mutex);
  3536. mutex_unlock(&cgroup_tree_mutex);
  3537. kernfs_unbreak_active_protection(kn);
  3538. cgroup_put(cgrp);
  3539. return ret;
  3540. }
  3541. static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
  3542. .remount_fs = cgroup_remount,
  3543. .show_options = cgroup_show_options,
  3544. .mkdir = cgroup_mkdir,
  3545. .rmdir = cgroup_rmdir,
  3546. .rename = cgroup_rename,
  3547. };
  3548. static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
  3549. {
  3550. struct cgroup_subsys_state *css;
  3551. printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
  3552. mutex_lock(&cgroup_tree_mutex);
  3553. mutex_lock(&cgroup_mutex);
  3554. INIT_LIST_HEAD(&ss->cfts);
  3555. /* Create the root cgroup state for this subsystem */
  3556. ss->root = &cgrp_dfl_root;
  3557. css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
  3558. /* We don't handle early failures gracefully */
  3559. BUG_ON(IS_ERR(css));
  3560. init_css(css, ss, &cgrp_dfl_root.cgrp);
  3561. /* Update the init_css_set to contain a subsys
  3562. * pointer to this state - since the subsystem is
  3563. * newly registered, all tasks and hence the
  3564. * init_css_set is in the subsystem's root cgroup. */
  3565. init_css_set.subsys[ss->id] = css;
  3566. need_forkexit_callback |= ss->fork || ss->exit;
  3567. /* At system boot, before all subsystems have been
  3568. * registered, no tasks have been forked, so we don't
  3569. * need to invoke fork callbacks here. */
  3570. BUG_ON(!list_empty(&init_task.tasks));
  3571. BUG_ON(online_css(css));
  3572. cgrp_dfl_root.cgrp.subsys_mask |= 1 << ss->id;
  3573. mutex_unlock(&cgroup_mutex);
  3574. mutex_unlock(&cgroup_tree_mutex);
  3575. }
  3576. /**
  3577. * cgroup_init_early - cgroup initialization at system boot
  3578. *
  3579. * Initialize cgroups at system boot, and initialize any
  3580. * subsystems that request early init.
  3581. */
  3582. int __init cgroup_init_early(void)
  3583. {
  3584. static struct cgroup_sb_opts __initdata opts =
  3585. { .flags = CGRP_ROOT_SANE_BEHAVIOR };
  3586. struct cgroup_subsys *ss;
  3587. int i;
  3588. init_cgroup_root(&cgrp_dfl_root, &opts);
  3589. RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
  3590. for_each_subsys(ss, i) {
  3591. WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
  3592. "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
  3593. i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
  3594. ss->id, ss->name);
  3595. WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
  3596. "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
  3597. ss->id = i;
  3598. ss->name = cgroup_subsys_name[i];
  3599. if (ss->early_init)
  3600. cgroup_init_subsys(ss);
  3601. }
  3602. return 0;
  3603. }
  3604. /**
  3605. * cgroup_init - cgroup initialization
  3606. *
  3607. * Register cgroup filesystem and /proc file, and initialize
  3608. * any subsystems that didn't request early init.
  3609. */
  3610. int __init cgroup_init(void)
  3611. {
  3612. struct cgroup_subsys *ss;
  3613. unsigned long key;
  3614. int ssid, err;
  3615. BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
  3616. mutex_lock(&cgroup_tree_mutex);
  3617. mutex_lock(&cgroup_mutex);
  3618. /* Add init_css_set to the hash table */
  3619. key = css_set_hash(init_css_set.subsys);
  3620. hash_add(css_set_table, &init_css_set.hlist, key);
  3621. BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
  3622. mutex_unlock(&cgroup_mutex);
  3623. mutex_unlock(&cgroup_tree_mutex);
  3624. for_each_subsys(ss, ssid) {
  3625. if (!ss->early_init)
  3626. cgroup_init_subsys(ss);
  3627. /*
  3628. * cftype registration needs kmalloc and can't be done
  3629. * during early_init. Register base cftypes separately.
  3630. */
  3631. if (ss->base_cftypes)
  3632. WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
  3633. }
  3634. cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
  3635. if (!cgroup_kobj)
  3636. return -ENOMEM;
  3637. err = register_filesystem(&cgroup_fs_type);
  3638. if (err < 0) {
  3639. kobject_put(cgroup_kobj);
  3640. return err;
  3641. }
  3642. proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
  3643. return 0;
  3644. }
  3645. static int __init cgroup_wq_init(void)
  3646. {
  3647. /*
  3648. * There isn't much point in executing destruction path in
  3649. * parallel. Good chunk is serialized with cgroup_mutex anyway.
  3650. * Use 1 for @max_active.
  3651. *
  3652. * We would prefer to do this in cgroup_init() above, but that
  3653. * is called before init_workqueues(): so leave this until after.
  3654. */
  3655. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  3656. BUG_ON(!cgroup_destroy_wq);
  3657. /*
  3658. * Used to destroy pidlists and separate to serve as flush domain.
  3659. * Cap @max_active to 1 too.
  3660. */
  3661. cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
  3662. 0, 1);
  3663. BUG_ON(!cgroup_pidlist_destroy_wq);
  3664. return 0;
  3665. }
  3666. core_initcall(cgroup_wq_init);
  3667. /*
  3668. * proc_cgroup_show()
  3669. * - Print task's cgroup paths into seq_file, one line for each hierarchy
  3670. * - Used for /proc/<pid>/cgroup.
  3671. */
  3672. /* TODO: Use a proper seq_file iterator */
  3673. int proc_cgroup_show(struct seq_file *m, void *v)
  3674. {
  3675. struct pid *pid;
  3676. struct task_struct *tsk;
  3677. char *buf, *path;
  3678. int retval;
  3679. struct cgroup_root *root;
  3680. retval = -ENOMEM;
  3681. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  3682. if (!buf)
  3683. goto out;
  3684. retval = -ESRCH;
  3685. pid = m->private;
  3686. tsk = get_pid_task(pid, PIDTYPE_PID);
  3687. if (!tsk)
  3688. goto out_free;
  3689. retval = 0;
  3690. mutex_lock(&cgroup_mutex);
  3691. down_read(&css_set_rwsem);
  3692. for_each_root(root) {
  3693. struct cgroup_subsys *ss;
  3694. struct cgroup *cgrp;
  3695. int ssid, count = 0;
  3696. if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
  3697. continue;
  3698. seq_printf(m, "%d:", root->hierarchy_id);
  3699. for_each_subsys(ss, ssid)
  3700. if (root->cgrp.subsys_mask & (1 << ssid))
  3701. seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
  3702. if (strlen(root->name))
  3703. seq_printf(m, "%sname=%s", count ? "," : "",
  3704. root->name);
  3705. seq_putc(m, ':');
  3706. cgrp = task_cgroup_from_root(tsk, root);
  3707. path = cgroup_path(cgrp, buf, PATH_MAX);
  3708. if (!path) {
  3709. retval = -ENAMETOOLONG;
  3710. goto out_unlock;
  3711. }
  3712. seq_puts(m, path);
  3713. seq_putc(m, '\n');
  3714. }
  3715. out_unlock:
  3716. up_read(&css_set_rwsem);
  3717. mutex_unlock(&cgroup_mutex);
  3718. put_task_struct(tsk);
  3719. out_free:
  3720. kfree(buf);
  3721. out:
  3722. return retval;
  3723. }
  3724. /* Display information about each subsystem and each hierarchy */
  3725. static int proc_cgroupstats_show(struct seq_file *m, void *v)
  3726. {
  3727. struct cgroup_subsys *ss;
  3728. int i;
  3729. seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
  3730. /*
  3731. * ideally we don't want subsystems moving around while we do this.
  3732. * cgroup_mutex is also necessary to guarantee an atomic snapshot of
  3733. * subsys/hierarchy state.
  3734. */
  3735. mutex_lock(&cgroup_mutex);
  3736. for_each_subsys(ss, i)
  3737. seq_printf(m, "%s\t%d\t%d\t%d\n",
  3738. ss->name, ss->root->hierarchy_id,
  3739. atomic_read(&ss->root->nr_cgrps), !ss->disabled);
  3740. mutex_unlock(&cgroup_mutex);
  3741. return 0;
  3742. }
  3743. static int cgroupstats_open(struct inode *inode, struct file *file)
  3744. {
  3745. return single_open(file, proc_cgroupstats_show, NULL);
  3746. }
  3747. static const struct file_operations proc_cgroupstats_operations = {
  3748. .open = cgroupstats_open,
  3749. .read = seq_read,
  3750. .llseek = seq_lseek,
  3751. .release = single_release,
  3752. };
  3753. /**
  3754. * cgroup_fork - initialize cgroup related fields during copy_process()
  3755. * @child: pointer to task_struct of forking parent process.
  3756. *
  3757. * A task is associated with the init_css_set until cgroup_post_fork()
  3758. * attaches it to the parent's css_set. Empty cg_list indicates that
  3759. * @child isn't holding reference to its css_set.
  3760. */
  3761. void cgroup_fork(struct task_struct *child)
  3762. {
  3763. RCU_INIT_POINTER(child->cgroups, &init_css_set);
  3764. INIT_LIST_HEAD(&child->cg_list);
  3765. }
  3766. /**
  3767. * cgroup_post_fork - called on a new task after adding it to the task list
  3768. * @child: the task in question
  3769. *
  3770. * Adds the task to the list running through its css_set if necessary and
  3771. * call the subsystem fork() callbacks. Has to be after the task is
  3772. * visible on the task list in case we race with the first call to
  3773. * cgroup_task_iter_start() - to guarantee that the new task ends up on its
  3774. * list.
  3775. */
  3776. void cgroup_post_fork(struct task_struct *child)
  3777. {
  3778. struct cgroup_subsys *ss;
  3779. int i;
  3780. /*
  3781. * This may race against cgroup_enable_task_cg_links(). As that
  3782. * function sets use_task_css_set_links before grabbing
  3783. * tasklist_lock and we just went through tasklist_lock to add
  3784. * @child, it's guaranteed that either we see the set
  3785. * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
  3786. * @child during its iteration.
  3787. *
  3788. * If we won the race, @child is associated with %current's
  3789. * css_set. Grabbing css_set_rwsem guarantees both that the
  3790. * association is stable, and, on completion of the parent's
  3791. * migration, @child is visible in the source of migration or
  3792. * already in the destination cgroup. This guarantee is necessary
  3793. * when implementing operations which need to migrate all tasks of
  3794. * a cgroup to another.
  3795. *
  3796. * Note that if we lose to cgroup_enable_task_cg_links(), @child
  3797. * will remain in init_css_set. This is safe because all tasks are
  3798. * in the init_css_set before cg_links is enabled and there's no
  3799. * operation which transfers all tasks out of init_css_set.
  3800. */
  3801. if (use_task_css_set_links) {
  3802. struct css_set *cset;
  3803. down_write(&css_set_rwsem);
  3804. cset = task_css_set(current);
  3805. if (list_empty(&child->cg_list)) {
  3806. rcu_assign_pointer(child->cgroups, cset);
  3807. list_add(&child->cg_list, &cset->tasks);
  3808. get_css_set(cset);
  3809. }
  3810. up_write(&css_set_rwsem);
  3811. }
  3812. /*
  3813. * Call ss->fork(). This must happen after @child is linked on
  3814. * css_set; otherwise, @child might change state between ->fork()
  3815. * and addition to css_set.
  3816. */
  3817. if (need_forkexit_callback) {
  3818. for_each_subsys(ss, i)
  3819. if (ss->fork)
  3820. ss->fork(child);
  3821. }
  3822. }
  3823. /**
  3824. * cgroup_exit - detach cgroup from exiting task
  3825. * @tsk: pointer to task_struct of exiting process
  3826. *
  3827. * Description: Detach cgroup from @tsk and release it.
  3828. *
  3829. * Note that cgroups marked notify_on_release force every task in
  3830. * them to take the global cgroup_mutex mutex when exiting.
  3831. * This could impact scaling on very large systems. Be reluctant to
  3832. * use notify_on_release cgroups where very high task exit scaling
  3833. * is required on large systems.
  3834. *
  3835. * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
  3836. * call cgroup_exit() while the task is still competent to handle
  3837. * notify_on_release(), then leave the task attached to the root cgroup in
  3838. * each hierarchy for the remainder of its exit. No need to bother with
  3839. * init_css_set refcnting. init_css_set never goes away and we can't race
  3840. * with migration path - PF_EXITING is visible to migration path.
  3841. */
  3842. void cgroup_exit(struct task_struct *tsk)
  3843. {
  3844. struct cgroup_subsys *ss;
  3845. struct css_set *cset;
  3846. bool put_cset = false;
  3847. int i;
  3848. /*
  3849. * Unlink from @tsk from its css_set. As migration path can't race
  3850. * with us, we can check cg_list without grabbing css_set_rwsem.
  3851. */
  3852. if (!list_empty(&tsk->cg_list)) {
  3853. down_write(&css_set_rwsem);
  3854. list_del_init(&tsk->cg_list);
  3855. up_write(&css_set_rwsem);
  3856. put_cset = true;
  3857. }
  3858. /* Reassign the task to the init_css_set. */
  3859. cset = task_css_set(tsk);
  3860. RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
  3861. if (need_forkexit_callback) {
  3862. /* see cgroup_post_fork() for details */
  3863. for_each_subsys(ss, i) {
  3864. if (ss->exit) {
  3865. struct cgroup_subsys_state *old_css = cset->subsys[i];
  3866. struct cgroup_subsys_state *css = task_css(tsk, i);
  3867. ss->exit(css, old_css, tsk);
  3868. }
  3869. }
  3870. }
  3871. if (put_cset)
  3872. put_css_set(cset, true);
  3873. }
  3874. static void check_for_release(struct cgroup *cgrp)
  3875. {
  3876. if (cgroup_is_releasable(cgrp) &&
  3877. list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
  3878. /*
  3879. * Control Group is currently removeable. If it's not
  3880. * already queued for a userspace notification, queue
  3881. * it now
  3882. */
  3883. int need_schedule_work = 0;
  3884. raw_spin_lock(&release_list_lock);
  3885. if (!cgroup_is_dead(cgrp) &&
  3886. list_empty(&cgrp->release_list)) {
  3887. list_add(&cgrp->release_list, &release_list);
  3888. need_schedule_work = 1;
  3889. }
  3890. raw_spin_unlock(&release_list_lock);
  3891. if (need_schedule_work)
  3892. schedule_work(&release_agent_work);
  3893. }
  3894. }
  3895. /*
  3896. * Notify userspace when a cgroup is released, by running the
  3897. * configured release agent with the name of the cgroup (path
  3898. * relative to the root of cgroup file system) as the argument.
  3899. *
  3900. * Most likely, this user command will try to rmdir this cgroup.
  3901. *
  3902. * This races with the possibility that some other task will be
  3903. * attached to this cgroup before it is removed, or that some other
  3904. * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
  3905. * The presumed 'rmdir' will fail quietly if this cgroup is no longer
  3906. * unused, and this cgroup will be reprieved from its death sentence,
  3907. * to continue to serve a useful existence. Next time it's released,
  3908. * we will get notified again, if it still has 'notify_on_release' set.
  3909. *
  3910. * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
  3911. * means only wait until the task is successfully execve()'d. The
  3912. * separate release agent task is forked by call_usermodehelper(),
  3913. * then control in this thread returns here, without waiting for the
  3914. * release agent task. We don't bother to wait because the caller of
  3915. * this routine has no use for the exit status of the release agent
  3916. * task, so no sense holding our caller up for that.
  3917. */
  3918. static void cgroup_release_agent(struct work_struct *work)
  3919. {
  3920. BUG_ON(work != &release_agent_work);
  3921. mutex_lock(&cgroup_mutex);
  3922. raw_spin_lock(&release_list_lock);
  3923. while (!list_empty(&release_list)) {
  3924. char *argv[3], *envp[3];
  3925. int i;
  3926. char *pathbuf = NULL, *agentbuf = NULL, *path;
  3927. struct cgroup *cgrp = list_entry(release_list.next,
  3928. struct cgroup,
  3929. release_list);
  3930. list_del_init(&cgrp->release_list);
  3931. raw_spin_unlock(&release_list_lock);
  3932. pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  3933. if (!pathbuf)
  3934. goto continue_free;
  3935. path = cgroup_path(cgrp, pathbuf, PATH_MAX);
  3936. if (!path)
  3937. goto continue_free;
  3938. agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
  3939. if (!agentbuf)
  3940. goto continue_free;
  3941. i = 0;
  3942. argv[i++] = agentbuf;
  3943. argv[i++] = path;
  3944. argv[i] = NULL;
  3945. i = 0;
  3946. /* minimal command environment */
  3947. envp[i++] = "HOME=/";
  3948. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  3949. envp[i] = NULL;
  3950. /* Drop the lock while we invoke the usermode helper,
  3951. * since the exec could involve hitting disk and hence
  3952. * be a slow process */
  3953. mutex_unlock(&cgroup_mutex);
  3954. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  3955. mutex_lock(&cgroup_mutex);
  3956. continue_free:
  3957. kfree(pathbuf);
  3958. kfree(agentbuf);
  3959. raw_spin_lock(&release_list_lock);
  3960. }
  3961. raw_spin_unlock(&release_list_lock);
  3962. mutex_unlock(&cgroup_mutex);
  3963. }
  3964. static int __init cgroup_disable(char *str)
  3965. {
  3966. struct cgroup_subsys *ss;
  3967. char *token;
  3968. int i;
  3969. while ((token = strsep(&str, ",")) != NULL) {
  3970. if (!*token)
  3971. continue;
  3972. for_each_subsys(ss, i) {
  3973. if (!strcmp(token, ss->name)) {
  3974. ss->disabled = 1;
  3975. printk(KERN_INFO "Disabling %s control group"
  3976. " subsystem\n", ss->name);
  3977. break;
  3978. }
  3979. }
  3980. }
  3981. return 1;
  3982. }
  3983. __setup("cgroup_disable=", cgroup_disable);
  3984. /**
  3985. * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
  3986. * @dentry: directory dentry of interest
  3987. * @ss: subsystem of interest
  3988. *
  3989. * If @dentry is a directory for a cgroup which has @ss enabled on it, try
  3990. * to get the corresponding css and return it. If such css doesn't exist
  3991. * or can't be pinned, an ERR_PTR value is returned.
  3992. */
  3993. struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
  3994. struct cgroup_subsys *ss)
  3995. {
  3996. struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
  3997. struct cgroup_subsys_state *css = NULL;
  3998. struct cgroup *cgrp;
  3999. /* is @dentry a cgroup dir? */
  4000. if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
  4001. kernfs_type(kn) != KERNFS_DIR)
  4002. return ERR_PTR(-EBADF);
  4003. rcu_read_lock();
  4004. /*
  4005. * This path doesn't originate from kernfs and @kn could already
  4006. * have been or be removed at any point. @kn->priv is RCU
  4007. * protected for this access. See destroy_locked() for details.
  4008. */
  4009. cgrp = rcu_dereference(kn->priv);
  4010. if (cgrp)
  4011. css = cgroup_css(cgrp, ss);
  4012. if (!css || !css_tryget(css))
  4013. css = ERR_PTR(-ENOENT);
  4014. rcu_read_unlock();
  4015. return css;
  4016. }
  4017. /**
  4018. * css_from_id - lookup css by id
  4019. * @id: the cgroup id
  4020. * @ss: cgroup subsys to be looked into
  4021. *
  4022. * Returns the css if there's valid one with @id, otherwise returns NULL.
  4023. * Should be called under rcu_read_lock().
  4024. */
  4025. struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
  4026. {
  4027. struct cgroup *cgrp;
  4028. cgroup_assert_mutexes_or_rcu_locked();
  4029. cgrp = idr_find(&ss->root->cgroup_idr, id);
  4030. if (cgrp)
  4031. return cgroup_css(cgrp, ss);
  4032. return NULL;
  4033. }
  4034. #ifdef CONFIG_CGROUP_DEBUG
  4035. static struct cgroup_subsys_state *
  4036. debug_css_alloc(struct cgroup_subsys_state *parent_css)
  4037. {
  4038. struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
  4039. if (!css)
  4040. return ERR_PTR(-ENOMEM);
  4041. return css;
  4042. }
  4043. static void debug_css_free(struct cgroup_subsys_state *css)
  4044. {
  4045. kfree(css);
  4046. }
  4047. static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
  4048. struct cftype *cft)
  4049. {
  4050. return cgroup_task_count(css->cgroup);
  4051. }
  4052. static u64 current_css_set_read(struct cgroup_subsys_state *css,
  4053. struct cftype *cft)
  4054. {
  4055. return (u64)(unsigned long)current->cgroups;
  4056. }
  4057. static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
  4058. struct cftype *cft)
  4059. {
  4060. u64 count;
  4061. rcu_read_lock();
  4062. count = atomic_read(&task_css_set(current)->refcount);
  4063. rcu_read_unlock();
  4064. return count;
  4065. }
  4066. static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
  4067. {
  4068. struct cgrp_cset_link *link;
  4069. struct css_set *cset;
  4070. char *name_buf;
  4071. name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  4072. if (!name_buf)
  4073. return -ENOMEM;
  4074. down_read(&css_set_rwsem);
  4075. rcu_read_lock();
  4076. cset = rcu_dereference(current->cgroups);
  4077. list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  4078. struct cgroup *c = link->cgrp;
  4079. cgroup_name(c, name_buf, NAME_MAX + 1);
  4080. seq_printf(seq, "Root %d group %s\n",
  4081. c->root->hierarchy_id, name_buf);
  4082. }
  4083. rcu_read_unlock();
  4084. up_read(&css_set_rwsem);
  4085. kfree(name_buf);
  4086. return 0;
  4087. }
  4088. #define MAX_TASKS_SHOWN_PER_CSS 25
  4089. static int cgroup_css_links_read(struct seq_file *seq, void *v)
  4090. {
  4091. struct cgroup_subsys_state *css = seq_css(seq);
  4092. struct cgrp_cset_link *link;
  4093. down_read(&css_set_rwsem);
  4094. list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
  4095. struct css_set *cset = link->cset;
  4096. struct task_struct *task;
  4097. int count = 0;
  4098. seq_printf(seq, "css_set %p\n", cset);
  4099. list_for_each_entry(task, &cset->tasks, cg_list) {
  4100. if (count++ > MAX_TASKS_SHOWN_PER_CSS)
  4101. goto overflow;
  4102. seq_printf(seq, " task %d\n", task_pid_vnr(task));
  4103. }
  4104. list_for_each_entry(task, &cset->mg_tasks, cg_list) {
  4105. if (count++ > MAX_TASKS_SHOWN_PER_CSS)
  4106. goto overflow;
  4107. seq_printf(seq, " task %d\n", task_pid_vnr(task));
  4108. }
  4109. continue;
  4110. overflow:
  4111. seq_puts(seq, " ...\n");
  4112. }
  4113. up_read(&css_set_rwsem);
  4114. return 0;
  4115. }
  4116. static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
  4117. {
  4118. return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
  4119. }
  4120. static struct cftype debug_files[] = {
  4121. {
  4122. .name = "taskcount",
  4123. .read_u64 = debug_taskcount_read,
  4124. },
  4125. {
  4126. .name = "current_css_set",
  4127. .read_u64 = current_css_set_read,
  4128. },
  4129. {
  4130. .name = "current_css_set_refcount",
  4131. .read_u64 = current_css_set_refcount_read,
  4132. },
  4133. {
  4134. .name = "current_css_set_cg_links",
  4135. .seq_show = current_css_set_cg_links_read,
  4136. },
  4137. {
  4138. .name = "cgroup_css_links",
  4139. .seq_show = cgroup_css_links_read,
  4140. },
  4141. {
  4142. .name = "releasable",
  4143. .read_u64 = releasable_read,
  4144. },
  4145. { } /* terminate */
  4146. };
  4147. struct cgroup_subsys debug_cgrp_subsys = {
  4148. .css_alloc = debug_css_alloc,
  4149. .css_free = debug_css_free,
  4150. .base_cftypes = debug_files,
  4151. };
  4152. #endif /* CONFIG_CGROUP_DEBUG */