cgroup.c 149 KB

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