cgroup.c 150 KB

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