cgroup.c 136 KB

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