core.c 210 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083
  1. /*
  2. * Performance events core code:
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  7. * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  8. *
  9. * For licensing details see kernel-base/COPYING
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/mm.h>
  13. #include <linux/cpu.h>
  14. #include <linux/smp.h>
  15. #include <linux/idr.h>
  16. #include <linux/file.h>
  17. #include <linux/poll.h>
  18. #include <linux/slab.h>
  19. #include <linux/hash.h>
  20. #include <linux/tick.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/dcache.h>
  23. #include <linux/percpu.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/reboot.h>
  26. #include <linux/vmstat.h>
  27. #include <linux/device.h>
  28. #include <linux/export.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/hardirq.h>
  31. #include <linux/rculist.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/anon_inodes.h>
  35. #include <linux/kernel_stat.h>
  36. #include <linux/cgroup.h>
  37. #include <linux/perf_event.h>
  38. #include <linux/ftrace_event.h>
  39. #include <linux/hw_breakpoint.h>
  40. #include <linux/mm_types.h>
  41. #include <linux/module.h>
  42. #include <linux/mman.h>
  43. #include <linux/compat.h>
  44. #include <linux/bpf.h>
  45. #include <linux/filter.h>
  46. #include "internal.h"
  47. #include <asm/irq_regs.h>
  48. static struct workqueue_struct *perf_wq;
  49. struct remote_function_call {
  50. struct task_struct *p;
  51. int (*func)(void *info);
  52. void *info;
  53. int ret;
  54. };
  55. static void remote_function(void *data)
  56. {
  57. struct remote_function_call *tfc = data;
  58. struct task_struct *p = tfc->p;
  59. if (p) {
  60. tfc->ret = -EAGAIN;
  61. if (task_cpu(p) != smp_processor_id() || !task_curr(p))
  62. return;
  63. }
  64. tfc->ret = tfc->func(tfc->info);
  65. }
  66. /**
  67. * task_function_call - call a function on the cpu on which a task runs
  68. * @p: the task to evaluate
  69. * @func: the function to be called
  70. * @info: the function call argument
  71. *
  72. * Calls the function @func when the task is currently running. This might
  73. * be on the current CPU, which just calls the function directly
  74. *
  75. * returns: @func return value, or
  76. * -ESRCH - when the process isn't running
  77. * -EAGAIN - when the process moved away
  78. */
  79. static int
  80. task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
  81. {
  82. struct remote_function_call data = {
  83. .p = p,
  84. .func = func,
  85. .info = info,
  86. .ret = -ESRCH, /* No such (running) process */
  87. };
  88. if (task_curr(p))
  89. smp_call_function_single(task_cpu(p), remote_function, &data, 1);
  90. return data.ret;
  91. }
  92. /**
  93. * cpu_function_call - call a function on the cpu
  94. * @func: the function to be called
  95. * @info: the function call argument
  96. *
  97. * Calls the function @func on the remote cpu.
  98. *
  99. * returns: @func return value or -ENXIO when the cpu is offline
  100. */
  101. static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
  102. {
  103. struct remote_function_call data = {
  104. .p = NULL,
  105. .func = func,
  106. .info = info,
  107. .ret = -ENXIO, /* No such CPU */
  108. };
  109. smp_call_function_single(cpu, remote_function, &data, 1);
  110. return data.ret;
  111. }
  112. #define EVENT_OWNER_KERNEL ((void *) -1)
  113. static bool is_kernel_event(struct perf_event *event)
  114. {
  115. return event->owner == EVENT_OWNER_KERNEL;
  116. }
  117. #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
  118. PERF_FLAG_FD_OUTPUT |\
  119. PERF_FLAG_PID_CGROUP |\
  120. PERF_FLAG_FD_CLOEXEC)
  121. /*
  122. * branch priv levels that need permission checks
  123. */
  124. #define PERF_SAMPLE_BRANCH_PERM_PLM \
  125. (PERF_SAMPLE_BRANCH_KERNEL |\
  126. PERF_SAMPLE_BRANCH_HV)
  127. enum event_type_t {
  128. EVENT_FLEXIBLE = 0x1,
  129. EVENT_PINNED = 0x2,
  130. EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
  131. };
  132. /*
  133. * perf_sched_events : >0 events exist
  134. * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
  135. */
  136. struct static_key_deferred perf_sched_events __read_mostly;
  137. static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
  138. static DEFINE_PER_CPU(int, perf_sched_cb_usages);
  139. static atomic_t nr_mmap_events __read_mostly;
  140. static atomic_t nr_comm_events __read_mostly;
  141. static atomic_t nr_task_events __read_mostly;
  142. static atomic_t nr_freq_events __read_mostly;
  143. static LIST_HEAD(pmus);
  144. static DEFINE_MUTEX(pmus_lock);
  145. static struct srcu_struct pmus_srcu;
  146. /*
  147. * perf event paranoia level:
  148. * -1 - not paranoid at all
  149. * 0 - disallow raw tracepoint access for unpriv
  150. * 1 - disallow cpu events for unpriv
  151. * 2 - disallow kernel profiling for unpriv
  152. */
  153. int sysctl_perf_event_paranoid __read_mostly = 1;
  154. /* Minimum for 512 kiB + 1 user control page */
  155. int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
  156. /*
  157. * max perf event sample rate
  158. */
  159. #define DEFAULT_MAX_SAMPLE_RATE 100000
  160. #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
  161. #define DEFAULT_CPU_TIME_MAX_PERCENT 25
  162. int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
  163. static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
  164. static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
  165. static int perf_sample_allowed_ns __read_mostly =
  166. DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
  167. void update_perf_cpu_limits(void)
  168. {
  169. u64 tmp = perf_sample_period_ns;
  170. tmp *= sysctl_perf_cpu_time_max_percent;
  171. do_div(tmp, 100);
  172. ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
  173. }
  174. static int perf_rotate_context(struct perf_cpu_context *cpuctx);
  175. int perf_proc_update_handler(struct ctl_table *table, int write,
  176. void __user *buffer, size_t *lenp,
  177. loff_t *ppos)
  178. {
  179. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  180. if (ret || !write)
  181. return ret;
  182. max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
  183. perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
  184. update_perf_cpu_limits();
  185. return 0;
  186. }
  187. int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
  188. int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
  189. void __user *buffer, size_t *lenp,
  190. loff_t *ppos)
  191. {
  192. int ret = proc_dointvec(table, write, buffer, lenp, ppos);
  193. if (ret || !write)
  194. return ret;
  195. update_perf_cpu_limits();
  196. return 0;
  197. }
  198. /*
  199. * perf samples are done in some very critical code paths (NMIs).
  200. * If they take too much CPU time, the system can lock up and not
  201. * get any real work done. This will drop the sample rate when
  202. * we detect that events are taking too long.
  203. */
  204. #define NR_ACCUMULATED_SAMPLES 128
  205. static DEFINE_PER_CPU(u64, running_sample_length);
  206. static void perf_duration_warn(struct irq_work *w)
  207. {
  208. u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
  209. u64 avg_local_sample_len;
  210. u64 local_samples_len;
  211. local_samples_len = __this_cpu_read(running_sample_length);
  212. avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
  213. printk_ratelimited(KERN_WARNING
  214. "perf interrupt took too long (%lld > %lld), lowering "
  215. "kernel.perf_event_max_sample_rate to %d\n",
  216. avg_local_sample_len, allowed_ns >> 1,
  217. sysctl_perf_event_sample_rate);
  218. }
  219. static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
  220. void perf_sample_event_took(u64 sample_len_ns)
  221. {
  222. u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
  223. u64 avg_local_sample_len;
  224. u64 local_samples_len;
  225. if (allowed_ns == 0)
  226. return;
  227. /* decay the counter by 1 average sample */
  228. local_samples_len = __this_cpu_read(running_sample_length);
  229. local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
  230. local_samples_len += sample_len_ns;
  231. __this_cpu_write(running_sample_length, local_samples_len);
  232. /*
  233. * note: this will be biased artifically low until we have
  234. * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
  235. * from having to maintain a count.
  236. */
  237. avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
  238. if (avg_local_sample_len <= allowed_ns)
  239. return;
  240. if (max_samples_per_tick <= 1)
  241. return;
  242. max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
  243. sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
  244. perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
  245. update_perf_cpu_limits();
  246. if (!irq_work_queue(&perf_duration_work)) {
  247. early_printk("perf interrupt took too long (%lld > %lld), lowering "
  248. "kernel.perf_event_max_sample_rate to %d\n",
  249. avg_local_sample_len, allowed_ns >> 1,
  250. sysctl_perf_event_sample_rate);
  251. }
  252. }
  253. static atomic64_t perf_event_id;
  254. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  255. enum event_type_t event_type);
  256. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  257. enum event_type_t event_type,
  258. struct task_struct *task);
  259. static void update_context_time(struct perf_event_context *ctx);
  260. static u64 perf_event_time(struct perf_event *event);
  261. void __weak perf_event_print_debug(void) { }
  262. extern __weak const char *perf_pmu_name(void)
  263. {
  264. return "pmu";
  265. }
  266. static inline u64 perf_clock(void)
  267. {
  268. return local_clock();
  269. }
  270. static inline u64 perf_event_clock(struct perf_event *event)
  271. {
  272. return event->clock();
  273. }
  274. static inline struct perf_cpu_context *
  275. __get_cpu_context(struct perf_event_context *ctx)
  276. {
  277. return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
  278. }
  279. static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
  280. struct perf_event_context *ctx)
  281. {
  282. raw_spin_lock(&cpuctx->ctx.lock);
  283. if (ctx)
  284. raw_spin_lock(&ctx->lock);
  285. }
  286. static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
  287. struct perf_event_context *ctx)
  288. {
  289. if (ctx)
  290. raw_spin_unlock(&ctx->lock);
  291. raw_spin_unlock(&cpuctx->ctx.lock);
  292. }
  293. #ifdef CONFIG_CGROUP_PERF
  294. static inline bool
  295. perf_cgroup_match(struct perf_event *event)
  296. {
  297. struct perf_event_context *ctx = event->ctx;
  298. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  299. /* @event doesn't care about cgroup */
  300. if (!event->cgrp)
  301. return true;
  302. /* wants specific cgroup scope but @cpuctx isn't associated with any */
  303. if (!cpuctx->cgrp)
  304. return false;
  305. /*
  306. * Cgroup scoping is recursive. An event enabled for a cgroup is
  307. * also enabled for all its descendant cgroups. If @cpuctx's
  308. * cgroup is a descendant of @event's (the test covers identity
  309. * case), it's a match.
  310. */
  311. return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
  312. event->cgrp->css.cgroup);
  313. }
  314. static inline void perf_detach_cgroup(struct perf_event *event)
  315. {
  316. css_put(&event->cgrp->css);
  317. event->cgrp = NULL;
  318. }
  319. static inline int is_cgroup_event(struct perf_event *event)
  320. {
  321. return event->cgrp != NULL;
  322. }
  323. static inline u64 perf_cgroup_event_time(struct perf_event *event)
  324. {
  325. struct perf_cgroup_info *t;
  326. t = per_cpu_ptr(event->cgrp->info, event->cpu);
  327. return t->time;
  328. }
  329. static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
  330. {
  331. struct perf_cgroup_info *info;
  332. u64 now;
  333. now = perf_clock();
  334. info = this_cpu_ptr(cgrp->info);
  335. info->time += now - info->timestamp;
  336. info->timestamp = now;
  337. }
  338. static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
  339. {
  340. struct perf_cgroup *cgrp_out = cpuctx->cgrp;
  341. if (cgrp_out)
  342. __update_cgrp_time(cgrp_out);
  343. }
  344. static inline void update_cgrp_time_from_event(struct perf_event *event)
  345. {
  346. struct perf_cgroup *cgrp;
  347. /*
  348. * ensure we access cgroup data only when needed and
  349. * when we know the cgroup is pinned (css_get)
  350. */
  351. if (!is_cgroup_event(event))
  352. return;
  353. cgrp = perf_cgroup_from_task(current);
  354. /*
  355. * Do not update time when cgroup is not active
  356. */
  357. if (cgrp == event->cgrp)
  358. __update_cgrp_time(event->cgrp);
  359. }
  360. static inline void
  361. perf_cgroup_set_timestamp(struct task_struct *task,
  362. struct perf_event_context *ctx)
  363. {
  364. struct perf_cgroup *cgrp;
  365. struct perf_cgroup_info *info;
  366. /*
  367. * ctx->lock held by caller
  368. * ensure we do not access cgroup data
  369. * unless we have the cgroup pinned (css_get)
  370. */
  371. if (!task || !ctx->nr_cgroups)
  372. return;
  373. cgrp = perf_cgroup_from_task(task);
  374. info = this_cpu_ptr(cgrp->info);
  375. info->timestamp = ctx->timestamp;
  376. }
  377. #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
  378. #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
  379. /*
  380. * reschedule events based on the cgroup constraint of task.
  381. *
  382. * mode SWOUT : schedule out everything
  383. * mode SWIN : schedule in based on cgroup for next
  384. */
  385. void perf_cgroup_switch(struct task_struct *task, int mode)
  386. {
  387. struct perf_cpu_context *cpuctx;
  388. struct pmu *pmu;
  389. unsigned long flags;
  390. /*
  391. * disable interrupts to avoid geting nr_cgroup
  392. * changes via __perf_event_disable(). Also
  393. * avoids preemption.
  394. */
  395. local_irq_save(flags);
  396. /*
  397. * we reschedule only in the presence of cgroup
  398. * constrained events.
  399. */
  400. rcu_read_lock();
  401. list_for_each_entry_rcu(pmu, &pmus, entry) {
  402. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  403. if (cpuctx->unique_pmu != pmu)
  404. continue; /* ensure we process each cpuctx once */
  405. /*
  406. * perf_cgroup_events says at least one
  407. * context on this CPU has cgroup events.
  408. *
  409. * ctx->nr_cgroups reports the number of cgroup
  410. * events for a context.
  411. */
  412. if (cpuctx->ctx.nr_cgroups > 0) {
  413. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  414. perf_pmu_disable(cpuctx->ctx.pmu);
  415. if (mode & PERF_CGROUP_SWOUT) {
  416. cpu_ctx_sched_out(cpuctx, EVENT_ALL);
  417. /*
  418. * must not be done before ctxswout due
  419. * to event_filter_match() in event_sched_out()
  420. */
  421. cpuctx->cgrp = NULL;
  422. }
  423. if (mode & PERF_CGROUP_SWIN) {
  424. WARN_ON_ONCE(cpuctx->cgrp);
  425. /*
  426. * set cgrp before ctxsw in to allow
  427. * event_filter_match() to not have to pass
  428. * task around
  429. */
  430. cpuctx->cgrp = perf_cgroup_from_task(task);
  431. cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
  432. }
  433. perf_pmu_enable(cpuctx->ctx.pmu);
  434. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  435. }
  436. }
  437. rcu_read_unlock();
  438. local_irq_restore(flags);
  439. }
  440. static inline void perf_cgroup_sched_out(struct task_struct *task,
  441. struct task_struct *next)
  442. {
  443. struct perf_cgroup *cgrp1;
  444. struct perf_cgroup *cgrp2 = NULL;
  445. /*
  446. * we come here when we know perf_cgroup_events > 0
  447. */
  448. cgrp1 = perf_cgroup_from_task(task);
  449. /*
  450. * next is NULL when called from perf_event_enable_on_exec()
  451. * that will systematically cause a cgroup_switch()
  452. */
  453. if (next)
  454. cgrp2 = perf_cgroup_from_task(next);
  455. /*
  456. * only schedule out current cgroup events if we know
  457. * that we are switching to a different cgroup. Otherwise,
  458. * do no touch the cgroup events.
  459. */
  460. if (cgrp1 != cgrp2)
  461. perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
  462. }
  463. static inline void perf_cgroup_sched_in(struct task_struct *prev,
  464. struct task_struct *task)
  465. {
  466. struct perf_cgroup *cgrp1;
  467. struct perf_cgroup *cgrp2 = NULL;
  468. /*
  469. * we come here when we know perf_cgroup_events > 0
  470. */
  471. cgrp1 = perf_cgroup_from_task(task);
  472. /* prev can never be NULL */
  473. cgrp2 = perf_cgroup_from_task(prev);
  474. /*
  475. * only need to schedule in cgroup events if we are changing
  476. * cgroup during ctxsw. Cgroup events were not scheduled
  477. * out of ctxsw out if that was not the case.
  478. */
  479. if (cgrp1 != cgrp2)
  480. perf_cgroup_switch(task, PERF_CGROUP_SWIN);
  481. }
  482. static inline int perf_cgroup_connect(int fd, struct perf_event *event,
  483. struct perf_event_attr *attr,
  484. struct perf_event *group_leader)
  485. {
  486. struct perf_cgroup *cgrp;
  487. struct cgroup_subsys_state *css;
  488. struct fd f = fdget(fd);
  489. int ret = 0;
  490. if (!f.file)
  491. return -EBADF;
  492. css = css_tryget_online_from_dir(f.file->f_path.dentry,
  493. &perf_event_cgrp_subsys);
  494. if (IS_ERR(css)) {
  495. ret = PTR_ERR(css);
  496. goto out;
  497. }
  498. cgrp = container_of(css, struct perf_cgroup, css);
  499. event->cgrp = cgrp;
  500. /*
  501. * all events in a group must monitor
  502. * the same cgroup because a task belongs
  503. * to only one perf cgroup at a time
  504. */
  505. if (group_leader && group_leader->cgrp != cgrp) {
  506. perf_detach_cgroup(event);
  507. ret = -EINVAL;
  508. }
  509. out:
  510. fdput(f);
  511. return ret;
  512. }
  513. static inline void
  514. perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
  515. {
  516. struct perf_cgroup_info *t;
  517. t = per_cpu_ptr(event->cgrp->info, event->cpu);
  518. event->shadow_ctx_time = now - t->timestamp;
  519. }
  520. static inline void
  521. perf_cgroup_defer_enabled(struct perf_event *event)
  522. {
  523. /*
  524. * when the current task's perf cgroup does not match
  525. * the event's, we need to remember to call the
  526. * perf_mark_enable() function the first time a task with
  527. * a matching perf cgroup is scheduled in.
  528. */
  529. if (is_cgroup_event(event) && !perf_cgroup_match(event))
  530. event->cgrp_defer_enabled = 1;
  531. }
  532. static inline void
  533. perf_cgroup_mark_enabled(struct perf_event *event,
  534. struct perf_event_context *ctx)
  535. {
  536. struct perf_event *sub;
  537. u64 tstamp = perf_event_time(event);
  538. if (!event->cgrp_defer_enabled)
  539. return;
  540. event->cgrp_defer_enabled = 0;
  541. event->tstamp_enabled = tstamp - event->total_time_enabled;
  542. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  543. if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
  544. sub->tstamp_enabled = tstamp - sub->total_time_enabled;
  545. sub->cgrp_defer_enabled = 0;
  546. }
  547. }
  548. }
  549. #else /* !CONFIG_CGROUP_PERF */
  550. static inline bool
  551. perf_cgroup_match(struct perf_event *event)
  552. {
  553. return true;
  554. }
  555. static inline void perf_detach_cgroup(struct perf_event *event)
  556. {}
  557. static inline int is_cgroup_event(struct perf_event *event)
  558. {
  559. return 0;
  560. }
  561. static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
  562. {
  563. return 0;
  564. }
  565. static inline void update_cgrp_time_from_event(struct perf_event *event)
  566. {
  567. }
  568. static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
  569. {
  570. }
  571. static inline void perf_cgroup_sched_out(struct task_struct *task,
  572. struct task_struct *next)
  573. {
  574. }
  575. static inline void perf_cgroup_sched_in(struct task_struct *prev,
  576. struct task_struct *task)
  577. {
  578. }
  579. static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
  580. struct perf_event_attr *attr,
  581. struct perf_event *group_leader)
  582. {
  583. return -EINVAL;
  584. }
  585. static inline void
  586. perf_cgroup_set_timestamp(struct task_struct *task,
  587. struct perf_event_context *ctx)
  588. {
  589. }
  590. void
  591. perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
  592. {
  593. }
  594. static inline void
  595. perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
  596. {
  597. }
  598. static inline u64 perf_cgroup_event_time(struct perf_event *event)
  599. {
  600. return 0;
  601. }
  602. static inline void
  603. perf_cgroup_defer_enabled(struct perf_event *event)
  604. {
  605. }
  606. static inline void
  607. perf_cgroup_mark_enabled(struct perf_event *event,
  608. struct perf_event_context *ctx)
  609. {
  610. }
  611. #endif
  612. /*
  613. * set default to be dependent on timer tick just
  614. * like original code
  615. */
  616. #define PERF_CPU_HRTIMER (1000 / HZ)
  617. /*
  618. * function must be called with interrupts disbled
  619. */
  620. static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
  621. {
  622. struct perf_cpu_context *cpuctx;
  623. enum hrtimer_restart ret = HRTIMER_NORESTART;
  624. int rotations = 0;
  625. WARN_ON(!irqs_disabled());
  626. cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
  627. rotations = perf_rotate_context(cpuctx);
  628. /*
  629. * arm timer if needed
  630. */
  631. if (rotations) {
  632. hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
  633. ret = HRTIMER_RESTART;
  634. }
  635. return ret;
  636. }
  637. /* CPU is going down */
  638. void perf_cpu_hrtimer_cancel(int cpu)
  639. {
  640. struct perf_cpu_context *cpuctx;
  641. struct pmu *pmu;
  642. unsigned long flags;
  643. if (WARN_ON(cpu != smp_processor_id()))
  644. return;
  645. local_irq_save(flags);
  646. rcu_read_lock();
  647. list_for_each_entry_rcu(pmu, &pmus, entry) {
  648. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  649. if (pmu->task_ctx_nr == perf_sw_context)
  650. continue;
  651. hrtimer_cancel(&cpuctx->hrtimer);
  652. }
  653. rcu_read_unlock();
  654. local_irq_restore(flags);
  655. }
  656. static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
  657. {
  658. struct hrtimer *hr = &cpuctx->hrtimer;
  659. struct pmu *pmu = cpuctx->ctx.pmu;
  660. int timer;
  661. /* no multiplexing needed for SW PMU */
  662. if (pmu->task_ctx_nr == perf_sw_context)
  663. return;
  664. /*
  665. * check default is sane, if not set then force to
  666. * default interval (1/tick)
  667. */
  668. timer = pmu->hrtimer_interval_ms;
  669. if (timer < 1)
  670. timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
  671. cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
  672. hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
  673. hr->function = perf_cpu_hrtimer_handler;
  674. }
  675. static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
  676. {
  677. struct hrtimer *hr = &cpuctx->hrtimer;
  678. struct pmu *pmu = cpuctx->ctx.pmu;
  679. /* not for SW PMU */
  680. if (pmu->task_ctx_nr == perf_sw_context)
  681. return;
  682. if (hrtimer_active(hr))
  683. return;
  684. if (!hrtimer_callback_running(hr))
  685. __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
  686. 0, HRTIMER_MODE_REL_PINNED, 0);
  687. }
  688. void perf_pmu_disable(struct pmu *pmu)
  689. {
  690. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  691. if (!(*count)++)
  692. pmu->pmu_disable(pmu);
  693. }
  694. void perf_pmu_enable(struct pmu *pmu)
  695. {
  696. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  697. if (!--(*count))
  698. pmu->pmu_enable(pmu);
  699. }
  700. static DEFINE_PER_CPU(struct list_head, active_ctx_list);
  701. /*
  702. * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
  703. * perf_event_task_tick() are fully serialized because they're strictly cpu
  704. * affine and perf_event_ctx{activate,deactivate} are called with IRQs
  705. * disabled, while perf_event_task_tick is called from IRQ context.
  706. */
  707. static void perf_event_ctx_activate(struct perf_event_context *ctx)
  708. {
  709. struct list_head *head = this_cpu_ptr(&active_ctx_list);
  710. WARN_ON(!irqs_disabled());
  711. WARN_ON(!list_empty(&ctx->active_ctx_list));
  712. list_add(&ctx->active_ctx_list, head);
  713. }
  714. static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
  715. {
  716. WARN_ON(!irqs_disabled());
  717. WARN_ON(list_empty(&ctx->active_ctx_list));
  718. list_del_init(&ctx->active_ctx_list);
  719. }
  720. static void get_ctx(struct perf_event_context *ctx)
  721. {
  722. WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
  723. }
  724. static void free_ctx(struct rcu_head *head)
  725. {
  726. struct perf_event_context *ctx;
  727. ctx = container_of(head, struct perf_event_context, rcu_head);
  728. kfree(ctx->task_ctx_data);
  729. kfree(ctx);
  730. }
  731. static void put_ctx(struct perf_event_context *ctx)
  732. {
  733. if (atomic_dec_and_test(&ctx->refcount)) {
  734. if (ctx->parent_ctx)
  735. put_ctx(ctx->parent_ctx);
  736. if (ctx->task)
  737. put_task_struct(ctx->task);
  738. call_rcu(&ctx->rcu_head, free_ctx);
  739. }
  740. }
  741. /*
  742. * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
  743. * perf_pmu_migrate_context() we need some magic.
  744. *
  745. * Those places that change perf_event::ctx will hold both
  746. * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
  747. *
  748. * Lock ordering is by mutex address. There are two other sites where
  749. * perf_event_context::mutex nests and those are:
  750. *
  751. * - perf_event_exit_task_context() [ child , 0 ]
  752. * __perf_event_exit_task()
  753. * sync_child_event()
  754. * put_event() [ parent, 1 ]
  755. *
  756. * - perf_event_init_context() [ parent, 0 ]
  757. * inherit_task_group()
  758. * inherit_group()
  759. * inherit_event()
  760. * perf_event_alloc()
  761. * perf_init_event()
  762. * perf_try_init_event() [ child , 1 ]
  763. *
  764. * While it appears there is an obvious deadlock here -- the parent and child
  765. * nesting levels are inverted between the two. This is in fact safe because
  766. * life-time rules separate them. That is an exiting task cannot fork, and a
  767. * spawning task cannot (yet) exit.
  768. *
  769. * But remember that that these are parent<->child context relations, and
  770. * migration does not affect children, therefore these two orderings should not
  771. * interact.
  772. *
  773. * The change in perf_event::ctx does not affect children (as claimed above)
  774. * because the sys_perf_event_open() case will install a new event and break
  775. * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
  776. * concerned with cpuctx and that doesn't have children.
  777. *
  778. * The places that change perf_event::ctx will issue:
  779. *
  780. * perf_remove_from_context();
  781. * synchronize_rcu();
  782. * perf_install_in_context();
  783. *
  784. * to affect the change. The remove_from_context() + synchronize_rcu() should
  785. * quiesce the event, after which we can install it in the new location. This
  786. * means that only external vectors (perf_fops, prctl) can perturb the event
  787. * while in transit. Therefore all such accessors should also acquire
  788. * perf_event_context::mutex to serialize against this.
  789. *
  790. * However; because event->ctx can change while we're waiting to acquire
  791. * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
  792. * function.
  793. *
  794. * Lock order:
  795. * task_struct::perf_event_mutex
  796. * perf_event_context::mutex
  797. * perf_event_context::lock
  798. * perf_event::child_mutex;
  799. * perf_event::mmap_mutex
  800. * mmap_sem
  801. */
  802. static struct perf_event_context *
  803. perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
  804. {
  805. struct perf_event_context *ctx;
  806. again:
  807. rcu_read_lock();
  808. ctx = ACCESS_ONCE(event->ctx);
  809. if (!atomic_inc_not_zero(&ctx->refcount)) {
  810. rcu_read_unlock();
  811. goto again;
  812. }
  813. rcu_read_unlock();
  814. mutex_lock_nested(&ctx->mutex, nesting);
  815. if (event->ctx != ctx) {
  816. mutex_unlock(&ctx->mutex);
  817. put_ctx(ctx);
  818. goto again;
  819. }
  820. return ctx;
  821. }
  822. static inline struct perf_event_context *
  823. perf_event_ctx_lock(struct perf_event *event)
  824. {
  825. return perf_event_ctx_lock_nested(event, 0);
  826. }
  827. static void perf_event_ctx_unlock(struct perf_event *event,
  828. struct perf_event_context *ctx)
  829. {
  830. mutex_unlock(&ctx->mutex);
  831. put_ctx(ctx);
  832. }
  833. /*
  834. * This must be done under the ctx->lock, such as to serialize against
  835. * context_equiv(), therefore we cannot call put_ctx() since that might end up
  836. * calling scheduler related locks and ctx->lock nests inside those.
  837. */
  838. static __must_check struct perf_event_context *
  839. unclone_ctx(struct perf_event_context *ctx)
  840. {
  841. struct perf_event_context *parent_ctx = ctx->parent_ctx;
  842. lockdep_assert_held(&ctx->lock);
  843. if (parent_ctx)
  844. ctx->parent_ctx = NULL;
  845. ctx->generation++;
  846. return parent_ctx;
  847. }
  848. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  849. {
  850. /*
  851. * only top level events have the pid namespace they were created in
  852. */
  853. if (event->parent)
  854. event = event->parent;
  855. return task_tgid_nr_ns(p, event->ns);
  856. }
  857. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  858. {
  859. /*
  860. * only top level events have the pid namespace they were created in
  861. */
  862. if (event->parent)
  863. event = event->parent;
  864. return task_pid_nr_ns(p, event->ns);
  865. }
  866. /*
  867. * If we inherit events we want to return the parent event id
  868. * to userspace.
  869. */
  870. static u64 primary_event_id(struct perf_event *event)
  871. {
  872. u64 id = event->id;
  873. if (event->parent)
  874. id = event->parent->id;
  875. return id;
  876. }
  877. /*
  878. * Get the perf_event_context for a task and lock it.
  879. * This has to cope with with the fact that until it is locked,
  880. * the context could get moved to another task.
  881. */
  882. static struct perf_event_context *
  883. perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
  884. {
  885. struct perf_event_context *ctx;
  886. retry:
  887. /*
  888. * One of the few rules of preemptible RCU is that one cannot do
  889. * rcu_read_unlock() while holding a scheduler (or nested) lock when
  890. * part of the read side critical section was preemptible -- see
  891. * rcu_read_unlock_special().
  892. *
  893. * Since ctx->lock nests under rq->lock we must ensure the entire read
  894. * side critical section is non-preemptible.
  895. */
  896. preempt_disable();
  897. rcu_read_lock();
  898. ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
  899. if (ctx) {
  900. /*
  901. * If this context is a clone of another, it might
  902. * get swapped for another underneath us by
  903. * perf_event_task_sched_out, though the
  904. * rcu_read_lock() protects us from any context
  905. * getting freed. Lock the context and check if it
  906. * got swapped before we could get the lock, and retry
  907. * if so. If we locked the right context, then it
  908. * can't get swapped on us any more.
  909. */
  910. raw_spin_lock_irqsave(&ctx->lock, *flags);
  911. if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
  912. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  913. rcu_read_unlock();
  914. preempt_enable();
  915. goto retry;
  916. }
  917. if (!atomic_inc_not_zero(&ctx->refcount)) {
  918. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  919. ctx = NULL;
  920. }
  921. }
  922. rcu_read_unlock();
  923. preempt_enable();
  924. return ctx;
  925. }
  926. /*
  927. * Get the context for a task and increment its pin_count so it
  928. * can't get swapped to another task. This also increments its
  929. * reference count so that the context can't get freed.
  930. */
  931. static struct perf_event_context *
  932. perf_pin_task_context(struct task_struct *task, int ctxn)
  933. {
  934. struct perf_event_context *ctx;
  935. unsigned long flags;
  936. ctx = perf_lock_task_context(task, ctxn, &flags);
  937. if (ctx) {
  938. ++ctx->pin_count;
  939. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  940. }
  941. return ctx;
  942. }
  943. static void perf_unpin_context(struct perf_event_context *ctx)
  944. {
  945. unsigned long flags;
  946. raw_spin_lock_irqsave(&ctx->lock, flags);
  947. --ctx->pin_count;
  948. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  949. }
  950. /*
  951. * Update the record of the current time in a context.
  952. */
  953. static void update_context_time(struct perf_event_context *ctx)
  954. {
  955. u64 now = perf_clock();
  956. ctx->time += now - ctx->timestamp;
  957. ctx->timestamp = now;
  958. }
  959. static u64 perf_event_time(struct perf_event *event)
  960. {
  961. struct perf_event_context *ctx = event->ctx;
  962. if (is_cgroup_event(event))
  963. return perf_cgroup_event_time(event);
  964. return ctx ? ctx->time : 0;
  965. }
  966. /*
  967. * Update the total_time_enabled and total_time_running fields for a event.
  968. * The caller of this function needs to hold the ctx->lock.
  969. */
  970. static void update_event_times(struct perf_event *event)
  971. {
  972. struct perf_event_context *ctx = event->ctx;
  973. u64 run_end;
  974. if (event->state < PERF_EVENT_STATE_INACTIVE ||
  975. event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
  976. return;
  977. /*
  978. * in cgroup mode, time_enabled represents
  979. * the time the event was enabled AND active
  980. * tasks were in the monitored cgroup. This is
  981. * independent of the activity of the context as
  982. * there may be a mix of cgroup and non-cgroup events.
  983. *
  984. * That is why we treat cgroup events differently
  985. * here.
  986. */
  987. if (is_cgroup_event(event))
  988. run_end = perf_cgroup_event_time(event);
  989. else if (ctx->is_active)
  990. run_end = ctx->time;
  991. else
  992. run_end = event->tstamp_stopped;
  993. event->total_time_enabled = run_end - event->tstamp_enabled;
  994. if (event->state == PERF_EVENT_STATE_INACTIVE)
  995. run_end = event->tstamp_stopped;
  996. else
  997. run_end = perf_event_time(event);
  998. event->total_time_running = run_end - event->tstamp_running;
  999. }
  1000. /*
  1001. * Update total_time_enabled and total_time_running for all events in a group.
  1002. */
  1003. static void update_group_times(struct perf_event *leader)
  1004. {
  1005. struct perf_event *event;
  1006. update_event_times(leader);
  1007. list_for_each_entry(event, &leader->sibling_list, group_entry)
  1008. update_event_times(event);
  1009. }
  1010. static struct list_head *
  1011. ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
  1012. {
  1013. if (event->attr.pinned)
  1014. return &ctx->pinned_groups;
  1015. else
  1016. return &ctx->flexible_groups;
  1017. }
  1018. /*
  1019. * Add a event from the lists for its context.
  1020. * Must be called with ctx->mutex and ctx->lock held.
  1021. */
  1022. static void
  1023. list_add_event(struct perf_event *event, struct perf_event_context *ctx)
  1024. {
  1025. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
  1026. event->attach_state |= PERF_ATTACH_CONTEXT;
  1027. /*
  1028. * If we're a stand alone event or group leader, we go to the context
  1029. * list, group events are kept attached to the group so that
  1030. * perf_group_detach can, at all times, locate all siblings.
  1031. */
  1032. if (event->group_leader == event) {
  1033. struct list_head *list;
  1034. if (is_software_event(event))
  1035. event->group_flags |= PERF_GROUP_SOFTWARE;
  1036. list = ctx_group_list(event, ctx);
  1037. list_add_tail(&event->group_entry, list);
  1038. }
  1039. if (is_cgroup_event(event))
  1040. ctx->nr_cgroups++;
  1041. list_add_rcu(&event->event_entry, &ctx->event_list);
  1042. ctx->nr_events++;
  1043. if (event->attr.inherit_stat)
  1044. ctx->nr_stat++;
  1045. ctx->generation++;
  1046. }
  1047. /*
  1048. * Initialize event state based on the perf_event_attr::disabled.
  1049. */
  1050. static inline void perf_event__state_init(struct perf_event *event)
  1051. {
  1052. event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
  1053. PERF_EVENT_STATE_INACTIVE;
  1054. }
  1055. /*
  1056. * Called at perf_event creation and when events are attached/detached from a
  1057. * group.
  1058. */
  1059. static void perf_event__read_size(struct perf_event *event)
  1060. {
  1061. int entry = sizeof(u64); /* value */
  1062. int size = 0;
  1063. int nr = 1;
  1064. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1065. size += sizeof(u64);
  1066. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1067. size += sizeof(u64);
  1068. if (event->attr.read_format & PERF_FORMAT_ID)
  1069. entry += sizeof(u64);
  1070. if (event->attr.read_format & PERF_FORMAT_GROUP) {
  1071. nr += event->group_leader->nr_siblings;
  1072. size += sizeof(u64);
  1073. }
  1074. size += entry * nr;
  1075. event->read_size = size;
  1076. }
  1077. static void perf_event__header_size(struct perf_event *event)
  1078. {
  1079. struct perf_sample_data *data;
  1080. u64 sample_type = event->attr.sample_type;
  1081. u16 size = 0;
  1082. perf_event__read_size(event);
  1083. if (sample_type & PERF_SAMPLE_IP)
  1084. size += sizeof(data->ip);
  1085. if (sample_type & PERF_SAMPLE_ADDR)
  1086. size += sizeof(data->addr);
  1087. if (sample_type & PERF_SAMPLE_PERIOD)
  1088. size += sizeof(data->period);
  1089. if (sample_type & PERF_SAMPLE_WEIGHT)
  1090. size += sizeof(data->weight);
  1091. if (sample_type & PERF_SAMPLE_READ)
  1092. size += event->read_size;
  1093. if (sample_type & PERF_SAMPLE_DATA_SRC)
  1094. size += sizeof(data->data_src.val);
  1095. if (sample_type & PERF_SAMPLE_TRANSACTION)
  1096. size += sizeof(data->txn);
  1097. event->header_size = size;
  1098. }
  1099. static void perf_event__id_header_size(struct perf_event *event)
  1100. {
  1101. struct perf_sample_data *data;
  1102. u64 sample_type = event->attr.sample_type;
  1103. u16 size = 0;
  1104. if (sample_type & PERF_SAMPLE_TID)
  1105. size += sizeof(data->tid_entry);
  1106. if (sample_type & PERF_SAMPLE_TIME)
  1107. size += sizeof(data->time);
  1108. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  1109. size += sizeof(data->id);
  1110. if (sample_type & PERF_SAMPLE_ID)
  1111. size += sizeof(data->id);
  1112. if (sample_type & PERF_SAMPLE_STREAM_ID)
  1113. size += sizeof(data->stream_id);
  1114. if (sample_type & PERF_SAMPLE_CPU)
  1115. size += sizeof(data->cpu_entry);
  1116. event->id_header_size = size;
  1117. }
  1118. static void perf_group_attach(struct perf_event *event)
  1119. {
  1120. struct perf_event *group_leader = event->group_leader, *pos;
  1121. /*
  1122. * We can have double attach due to group movement in perf_event_open.
  1123. */
  1124. if (event->attach_state & PERF_ATTACH_GROUP)
  1125. return;
  1126. event->attach_state |= PERF_ATTACH_GROUP;
  1127. if (group_leader == event)
  1128. return;
  1129. WARN_ON_ONCE(group_leader->ctx != event->ctx);
  1130. if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
  1131. !is_software_event(event))
  1132. group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
  1133. list_add_tail(&event->group_entry, &group_leader->sibling_list);
  1134. group_leader->nr_siblings++;
  1135. perf_event__header_size(group_leader);
  1136. list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
  1137. perf_event__header_size(pos);
  1138. }
  1139. /*
  1140. * Remove a event from the lists for its context.
  1141. * Must be called with ctx->mutex and ctx->lock held.
  1142. */
  1143. static void
  1144. list_del_event(struct perf_event *event, struct perf_event_context *ctx)
  1145. {
  1146. struct perf_cpu_context *cpuctx;
  1147. WARN_ON_ONCE(event->ctx != ctx);
  1148. lockdep_assert_held(&ctx->lock);
  1149. /*
  1150. * We can have double detach due to exit/hot-unplug + close.
  1151. */
  1152. if (!(event->attach_state & PERF_ATTACH_CONTEXT))
  1153. return;
  1154. event->attach_state &= ~PERF_ATTACH_CONTEXT;
  1155. if (is_cgroup_event(event)) {
  1156. ctx->nr_cgroups--;
  1157. cpuctx = __get_cpu_context(ctx);
  1158. /*
  1159. * if there are no more cgroup events
  1160. * then cler cgrp to avoid stale pointer
  1161. * in update_cgrp_time_from_cpuctx()
  1162. */
  1163. if (!ctx->nr_cgroups)
  1164. cpuctx->cgrp = NULL;
  1165. }
  1166. ctx->nr_events--;
  1167. if (event->attr.inherit_stat)
  1168. ctx->nr_stat--;
  1169. list_del_rcu(&event->event_entry);
  1170. if (event->group_leader == event)
  1171. list_del_init(&event->group_entry);
  1172. update_group_times(event);
  1173. /*
  1174. * If event was in error state, then keep it
  1175. * that way, otherwise bogus counts will be
  1176. * returned on read(). The only way to get out
  1177. * of error state is by explicit re-enabling
  1178. * of the event
  1179. */
  1180. if (event->state > PERF_EVENT_STATE_OFF)
  1181. event->state = PERF_EVENT_STATE_OFF;
  1182. ctx->generation++;
  1183. }
  1184. static void perf_group_detach(struct perf_event *event)
  1185. {
  1186. struct perf_event *sibling, *tmp;
  1187. struct list_head *list = NULL;
  1188. /*
  1189. * We can have double detach due to exit/hot-unplug + close.
  1190. */
  1191. if (!(event->attach_state & PERF_ATTACH_GROUP))
  1192. return;
  1193. event->attach_state &= ~PERF_ATTACH_GROUP;
  1194. /*
  1195. * If this is a sibling, remove it from its group.
  1196. */
  1197. if (event->group_leader != event) {
  1198. list_del_init(&event->group_entry);
  1199. event->group_leader->nr_siblings--;
  1200. goto out;
  1201. }
  1202. if (!list_empty(&event->group_entry))
  1203. list = &event->group_entry;
  1204. /*
  1205. * If this was a group event with sibling events then
  1206. * upgrade the siblings to singleton events by adding them
  1207. * to whatever list we are on.
  1208. */
  1209. list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
  1210. if (list)
  1211. list_move_tail(&sibling->group_entry, list);
  1212. sibling->group_leader = sibling;
  1213. /* Inherit group flags from the previous leader */
  1214. sibling->group_flags = event->group_flags;
  1215. WARN_ON_ONCE(sibling->ctx != event->ctx);
  1216. }
  1217. out:
  1218. perf_event__header_size(event->group_leader);
  1219. list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
  1220. perf_event__header_size(tmp);
  1221. }
  1222. /*
  1223. * User event without the task.
  1224. */
  1225. static bool is_orphaned_event(struct perf_event *event)
  1226. {
  1227. return event && !is_kernel_event(event) && !event->owner;
  1228. }
  1229. /*
  1230. * Event has a parent but parent's task finished and it's
  1231. * alive only because of children holding refference.
  1232. */
  1233. static bool is_orphaned_child(struct perf_event *event)
  1234. {
  1235. return is_orphaned_event(event->parent);
  1236. }
  1237. static void orphans_remove_work(struct work_struct *work);
  1238. static void schedule_orphans_remove(struct perf_event_context *ctx)
  1239. {
  1240. if (!ctx->task || ctx->orphans_remove_sched || !perf_wq)
  1241. return;
  1242. if (queue_delayed_work(perf_wq, &ctx->orphans_remove, 1)) {
  1243. get_ctx(ctx);
  1244. ctx->orphans_remove_sched = true;
  1245. }
  1246. }
  1247. static int __init perf_workqueue_init(void)
  1248. {
  1249. perf_wq = create_singlethread_workqueue("perf");
  1250. WARN(!perf_wq, "failed to create perf workqueue\n");
  1251. return perf_wq ? 0 : -1;
  1252. }
  1253. core_initcall(perf_workqueue_init);
  1254. static inline int
  1255. event_filter_match(struct perf_event *event)
  1256. {
  1257. return (event->cpu == -1 || event->cpu == smp_processor_id())
  1258. && perf_cgroup_match(event);
  1259. }
  1260. static void
  1261. event_sched_out(struct perf_event *event,
  1262. struct perf_cpu_context *cpuctx,
  1263. struct perf_event_context *ctx)
  1264. {
  1265. u64 tstamp = perf_event_time(event);
  1266. u64 delta;
  1267. WARN_ON_ONCE(event->ctx != ctx);
  1268. lockdep_assert_held(&ctx->lock);
  1269. /*
  1270. * An event which could not be activated because of
  1271. * filter mismatch still needs to have its timings
  1272. * maintained, otherwise bogus information is return
  1273. * via read() for time_enabled, time_running:
  1274. */
  1275. if (event->state == PERF_EVENT_STATE_INACTIVE
  1276. && !event_filter_match(event)) {
  1277. delta = tstamp - event->tstamp_stopped;
  1278. event->tstamp_running += delta;
  1279. event->tstamp_stopped = tstamp;
  1280. }
  1281. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1282. return;
  1283. perf_pmu_disable(event->pmu);
  1284. event->state = PERF_EVENT_STATE_INACTIVE;
  1285. if (event->pending_disable) {
  1286. event->pending_disable = 0;
  1287. event->state = PERF_EVENT_STATE_OFF;
  1288. }
  1289. event->tstamp_stopped = tstamp;
  1290. event->pmu->del(event, 0);
  1291. event->oncpu = -1;
  1292. if (!is_software_event(event))
  1293. cpuctx->active_oncpu--;
  1294. if (!--ctx->nr_active)
  1295. perf_event_ctx_deactivate(ctx);
  1296. if (event->attr.freq && event->attr.sample_freq)
  1297. ctx->nr_freq--;
  1298. if (event->attr.exclusive || !cpuctx->active_oncpu)
  1299. cpuctx->exclusive = 0;
  1300. if (is_orphaned_child(event))
  1301. schedule_orphans_remove(ctx);
  1302. perf_pmu_enable(event->pmu);
  1303. }
  1304. static void
  1305. group_sched_out(struct perf_event *group_event,
  1306. struct perf_cpu_context *cpuctx,
  1307. struct perf_event_context *ctx)
  1308. {
  1309. struct perf_event *event;
  1310. int state = group_event->state;
  1311. event_sched_out(group_event, cpuctx, ctx);
  1312. /*
  1313. * Schedule out siblings (if any):
  1314. */
  1315. list_for_each_entry(event, &group_event->sibling_list, group_entry)
  1316. event_sched_out(event, cpuctx, ctx);
  1317. if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
  1318. cpuctx->exclusive = 0;
  1319. }
  1320. struct remove_event {
  1321. struct perf_event *event;
  1322. bool detach_group;
  1323. };
  1324. /*
  1325. * Cross CPU call to remove a performance event
  1326. *
  1327. * We disable the event on the hardware level first. After that we
  1328. * remove it from the context list.
  1329. */
  1330. static int __perf_remove_from_context(void *info)
  1331. {
  1332. struct remove_event *re = info;
  1333. struct perf_event *event = re->event;
  1334. struct perf_event_context *ctx = event->ctx;
  1335. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1336. raw_spin_lock(&ctx->lock);
  1337. event_sched_out(event, cpuctx, ctx);
  1338. if (re->detach_group)
  1339. perf_group_detach(event);
  1340. list_del_event(event, ctx);
  1341. if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
  1342. ctx->is_active = 0;
  1343. cpuctx->task_ctx = NULL;
  1344. }
  1345. raw_spin_unlock(&ctx->lock);
  1346. return 0;
  1347. }
  1348. /*
  1349. * Remove the event from a task's (or a CPU's) list of events.
  1350. *
  1351. * CPU events are removed with a smp call. For task events we only
  1352. * call when the task is on a CPU.
  1353. *
  1354. * If event->ctx is a cloned context, callers must make sure that
  1355. * every task struct that event->ctx->task could possibly point to
  1356. * remains valid. This is OK when called from perf_release since
  1357. * that only calls us on the top-level context, which can't be a clone.
  1358. * When called from perf_event_exit_task, it's OK because the
  1359. * context has been detached from its task.
  1360. */
  1361. static void perf_remove_from_context(struct perf_event *event, bool detach_group)
  1362. {
  1363. struct perf_event_context *ctx = event->ctx;
  1364. struct task_struct *task = ctx->task;
  1365. struct remove_event re = {
  1366. .event = event,
  1367. .detach_group = detach_group,
  1368. };
  1369. lockdep_assert_held(&ctx->mutex);
  1370. if (!task) {
  1371. /*
  1372. * Per cpu events are removed via an smp call. The removal can
  1373. * fail if the CPU is currently offline, but in that case we
  1374. * already called __perf_remove_from_context from
  1375. * perf_event_exit_cpu.
  1376. */
  1377. cpu_function_call(event->cpu, __perf_remove_from_context, &re);
  1378. return;
  1379. }
  1380. retry:
  1381. if (!task_function_call(task, __perf_remove_from_context, &re))
  1382. return;
  1383. raw_spin_lock_irq(&ctx->lock);
  1384. /*
  1385. * If we failed to find a running task, but find the context active now
  1386. * that we've acquired the ctx->lock, retry.
  1387. */
  1388. if (ctx->is_active) {
  1389. raw_spin_unlock_irq(&ctx->lock);
  1390. /*
  1391. * Reload the task pointer, it might have been changed by
  1392. * a concurrent perf_event_context_sched_out().
  1393. */
  1394. task = ctx->task;
  1395. goto retry;
  1396. }
  1397. /*
  1398. * Since the task isn't running, its safe to remove the event, us
  1399. * holding the ctx->lock ensures the task won't get scheduled in.
  1400. */
  1401. if (detach_group)
  1402. perf_group_detach(event);
  1403. list_del_event(event, ctx);
  1404. raw_spin_unlock_irq(&ctx->lock);
  1405. }
  1406. /*
  1407. * Cross CPU call to disable a performance event
  1408. */
  1409. int __perf_event_disable(void *info)
  1410. {
  1411. struct perf_event *event = info;
  1412. struct perf_event_context *ctx = event->ctx;
  1413. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1414. /*
  1415. * If this is a per-task event, need to check whether this
  1416. * event's task is the current task on this cpu.
  1417. *
  1418. * Can trigger due to concurrent perf_event_context_sched_out()
  1419. * flipping contexts around.
  1420. */
  1421. if (ctx->task && cpuctx->task_ctx != ctx)
  1422. return -EINVAL;
  1423. raw_spin_lock(&ctx->lock);
  1424. /*
  1425. * If the event is on, turn it off.
  1426. * If it is in error state, leave it in error state.
  1427. */
  1428. if (event->state >= PERF_EVENT_STATE_INACTIVE) {
  1429. update_context_time(ctx);
  1430. update_cgrp_time_from_event(event);
  1431. update_group_times(event);
  1432. if (event == event->group_leader)
  1433. group_sched_out(event, cpuctx, ctx);
  1434. else
  1435. event_sched_out(event, cpuctx, ctx);
  1436. event->state = PERF_EVENT_STATE_OFF;
  1437. }
  1438. raw_spin_unlock(&ctx->lock);
  1439. return 0;
  1440. }
  1441. /*
  1442. * Disable a event.
  1443. *
  1444. * If event->ctx is a cloned context, callers must make sure that
  1445. * every task struct that event->ctx->task could possibly point to
  1446. * remains valid. This condition is satisifed when called through
  1447. * perf_event_for_each_child or perf_event_for_each because they
  1448. * hold the top-level event's child_mutex, so any descendant that
  1449. * goes to exit will block in sync_child_event.
  1450. * When called from perf_pending_event it's OK because event->ctx
  1451. * is the current context on this CPU and preemption is disabled,
  1452. * hence we can't get into perf_event_task_sched_out for this context.
  1453. */
  1454. static void _perf_event_disable(struct perf_event *event)
  1455. {
  1456. struct perf_event_context *ctx = event->ctx;
  1457. struct task_struct *task = ctx->task;
  1458. if (!task) {
  1459. /*
  1460. * Disable the event on the cpu that it's on
  1461. */
  1462. cpu_function_call(event->cpu, __perf_event_disable, event);
  1463. return;
  1464. }
  1465. retry:
  1466. if (!task_function_call(task, __perf_event_disable, event))
  1467. return;
  1468. raw_spin_lock_irq(&ctx->lock);
  1469. /*
  1470. * If the event is still active, we need to retry the cross-call.
  1471. */
  1472. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  1473. raw_spin_unlock_irq(&ctx->lock);
  1474. /*
  1475. * Reload the task pointer, it might have been changed by
  1476. * a concurrent perf_event_context_sched_out().
  1477. */
  1478. task = ctx->task;
  1479. goto retry;
  1480. }
  1481. /*
  1482. * Since we have the lock this context can't be scheduled
  1483. * in, so we can change the state safely.
  1484. */
  1485. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1486. update_group_times(event);
  1487. event->state = PERF_EVENT_STATE_OFF;
  1488. }
  1489. raw_spin_unlock_irq(&ctx->lock);
  1490. }
  1491. /*
  1492. * Strictly speaking kernel users cannot create groups and therefore this
  1493. * interface does not need the perf_event_ctx_lock() magic.
  1494. */
  1495. void perf_event_disable(struct perf_event *event)
  1496. {
  1497. struct perf_event_context *ctx;
  1498. ctx = perf_event_ctx_lock(event);
  1499. _perf_event_disable(event);
  1500. perf_event_ctx_unlock(event, ctx);
  1501. }
  1502. EXPORT_SYMBOL_GPL(perf_event_disable);
  1503. static void perf_set_shadow_time(struct perf_event *event,
  1504. struct perf_event_context *ctx,
  1505. u64 tstamp)
  1506. {
  1507. /*
  1508. * use the correct time source for the time snapshot
  1509. *
  1510. * We could get by without this by leveraging the
  1511. * fact that to get to this function, the caller
  1512. * has most likely already called update_context_time()
  1513. * and update_cgrp_time_xx() and thus both timestamp
  1514. * are identical (or very close). Given that tstamp is,
  1515. * already adjusted for cgroup, we could say that:
  1516. * tstamp - ctx->timestamp
  1517. * is equivalent to
  1518. * tstamp - cgrp->timestamp.
  1519. *
  1520. * Then, in perf_output_read(), the calculation would
  1521. * work with no changes because:
  1522. * - event is guaranteed scheduled in
  1523. * - no scheduled out in between
  1524. * - thus the timestamp would be the same
  1525. *
  1526. * But this is a bit hairy.
  1527. *
  1528. * So instead, we have an explicit cgroup call to remain
  1529. * within the time time source all along. We believe it
  1530. * is cleaner and simpler to understand.
  1531. */
  1532. if (is_cgroup_event(event))
  1533. perf_cgroup_set_shadow_time(event, tstamp);
  1534. else
  1535. event->shadow_ctx_time = tstamp - ctx->timestamp;
  1536. }
  1537. #define MAX_INTERRUPTS (~0ULL)
  1538. static void perf_log_throttle(struct perf_event *event, int enable);
  1539. static void perf_log_itrace_start(struct perf_event *event);
  1540. static int
  1541. event_sched_in(struct perf_event *event,
  1542. struct perf_cpu_context *cpuctx,
  1543. struct perf_event_context *ctx)
  1544. {
  1545. u64 tstamp = perf_event_time(event);
  1546. int ret = 0;
  1547. lockdep_assert_held(&ctx->lock);
  1548. if (event->state <= PERF_EVENT_STATE_OFF)
  1549. return 0;
  1550. event->state = PERF_EVENT_STATE_ACTIVE;
  1551. event->oncpu = smp_processor_id();
  1552. /*
  1553. * Unthrottle events, since we scheduled we might have missed several
  1554. * ticks already, also for a heavily scheduling task there is little
  1555. * guarantee it'll get a tick in a timely manner.
  1556. */
  1557. if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
  1558. perf_log_throttle(event, 1);
  1559. event->hw.interrupts = 0;
  1560. }
  1561. /*
  1562. * The new state must be visible before we turn it on in the hardware:
  1563. */
  1564. smp_wmb();
  1565. perf_pmu_disable(event->pmu);
  1566. event->tstamp_running += tstamp - event->tstamp_stopped;
  1567. perf_set_shadow_time(event, ctx, tstamp);
  1568. perf_log_itrace_start(event);
  1569. if (event->pmu->add(event, PERF_EF_START)) {
  1570. event->state = PERF_EVENT_STATE_INACTIVE;
  1571. event->oncpu = -1;
  1572. ret = -EAGAIN;
  1573. goto out;
  1574. }
  1575. if (!is_software_event(event))
  1576. cpuctx->active_oncpu++;
  1577. if (!ctx->nr_active++)
  1578. perf_event_ctx_activate(ctx);
  1579. if (event->attr.freq && event->attr.sample_freq)
  1580. ctx->nr_freq++;
  1581. if (event->attr.exclusive)
  1582. cpuctx->exclusive = 1;
  1583. if (is_orphaned_child(event))
  1584. schedule_orphans_remove(ctx);
  1585. out:
  1586. perf_pmu_enable(event->pmu);
  1587. return ret;
  1588. }
  1589. static int
  1590. group_sched_in(struct perf_event *group_event,
  1591. struct perf_cpu_context *cpuctx,
  1592. struct perf_event_context *ctx)
  1593. {
  1594. struct perf_event *event, *partial_group = NULL;
  1595. struct pmu *pmu = ctx->pmu;
  1596. u64 now = ctx->time;
  1597. bool simulate = false;
  1598. if (group_event->state == PERF_EVENT_STATE_OFF)
  1599. return 0;
  1600. pmu->start_txn(pmu);
  1601. if (event_sched_in(group_event, cpuctx, ctx)) {
  1602. pmu->cancel_txn(pmu);
  1603. perf_cpu_hrtimer_restart(cpuctx);
  1604. return -EAGAIN;
  1605. }
  1606. /*
  1607. * Schedule in siblings as one group (if any):
  1608. */
  1609. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  1610. if (event_sched_in(event, cpuctx, ctx)) {
  1611. partial_group = event;
  1612. goto group_error;
  1613. }
  1614. }
  1615. if (!pmu->commit_txn(pmu))
  1616. return 0;
  1617. group_error:
  1618. /*
  1619. * Groups can be scheduled in as one unit only, so undo any
  1620. * partial group before returning:
  1621. * The events up to the failed event are scheduled out normally,
  1622. * tstamp_stopped will be updated.
  1623. *
  1624. * The failed events and the remaining siblings need to have
  1625. * their timings updated as if they had gone thru event_sched_in()
  1626. * and event_sched_out(). This is required to get consistent timings
  1627. * across the group. This also takes care of the case where the group
  1628. * could never be scheduled by ensuring tstamp_stopped is set to mark
  1629. * the time the event was actually stopped, such that time delta
  1630. * calculation in update_event_times() is correct.
  1631. */
  1632. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  1633. if (event == partial_group)
  1634. simulate = true;
  1635. if (simulate) {
  1636. event->tstamp_running += now - event->tstamp_stopped;
  1637. event->tstamp_stopped = now;
  1638. } else {
  1639. event_sched_out(event, cpuctx, ctx);
  1640. }
  1641. }
  1642. event_sched_out(group_event, cpuctx, ctx);
  1643. pmu->cancel_txn(pmu);
  1644. perf_cpu_hrtimer_restart(cpuctx);
  1645. return -EAGAIN;
  1646. }
  1647. /*
  1648. * Work out whether we can put this event group on the CPU now.
  1649. */
  1650. static int group_can_go_on(struct perf_event *event,
  1651. struct perf_cpu_context *cpuctx,
  1652. int can_add_hw)
  1653. {
  1654. /*
  1655. * Groups consisting entirely of software events can always go on.
  1656. */
  1657. if (event->group_flags & PERF_GROUP_SOFTWARE)
  1658. return 1;
  1659. /*
  1660. * If an exclusive group is already on, no other hardware
  1661. * events can go on.
  1662. */
  1663. if (cpuctx->exclusive)
  1664. return 0;
  1665. /*
  1666. * If this group is exclusive and there are already
  1667. * events on the CPU, it can't go on.
  1668. */
  1669. if (event->attr.exclusive && cpuctx->active_oncpu)
  1670. return 0;
  1671. /*
  1672. * Otherwise, try to add it if all previous groups were able
  1673. * to go on.
  1674. */
  1675. return can_add_hw;
  1676. }
  1677. static void add_event_to_ctx(struct perf_event *event,
  1678. struct perf_event_context *ctx)
  1679. {
  1680. u64 tstamp = perf_event_time(event);
  1681. list_add_event(event, ctx);
  1682. perf_group_attach(event);
  1683. event->tstamp_enabled = tstamp;
  1684. event->tstamp_running = tstamp;
  1685. event->tstamp_stopped = tstamp;
  1686. }
  1687. static void task_ctx_sched_out(struct perf_event_context *ctx);
  1688. static void
  1689. ctx_sched_in(struct perf_event_context *ctx,
  1690. struct perf_cpu_context *cpuctx,
  1691. enum event_type_t event_type,
  1692. struct task_struct *task);
  1693. static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
  1694. struct perf_event_context *ctx,
  1695. struct task_struct *task)
  1696. {
  1697. cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
  1698. if (ctx)
  1699. ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
  1700. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
  1701. if (ctx)
  1702. ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
  1703. }
  1704. /*
  1705. * Cross CPU call to install and enable a performance event
  1706. *
  1707. * Must be called with ctx->mutex held
  1708. */
  1709. static int __perf_install_in_context(void *info)
  1710. {
  1711. struct perf_event *event = info;
  1712. struct perf_event_context *ctx = event->ctx;
  1713. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1714. struct perf_event_context *task_ctx = cpuctx->task_ctx;
  1715. struct task_struct *task = current;
  1716. perf_ctx_lock(cpuctx, task_ctx);
  1717. perf_pmu_disable(cpuctx->ctx.pmu);
  1718. /*
  1719. * If there was an active task_ctx schedule it out.
  1720. */
  1721. if (task_ctx)
  1722. task_ctx_sched_out(task_ctx);
  1723. /*
  1724. * If the context we're installing events in is not the
  1725. * active task_ctx, flip them.
  1726. */
  1727. if (ctx->task && task_ctx != ctx) {
  1728. if (task_ctx)
  1729. raw_spin_unlock(&task_ctx->lock);
  1730. raw_spin_lock(&ctx->lock);
  1731. task_ctx = ctx;
  1732. }
  1733. if (task_ctx) {
  1734. cpuctx->task_ctx = task_ctx;
  1735. task = task_ctx->task;
  1736. }
  1737. cpu_ctx_sched_out(cpuctx, EVENT_ALL);
  1738. update_context_time(ctx);
  1739. /*
  1740. * update cgrp time only if current cgrp
  1741. * matches event->cgrp. Must be done before
  1742. * calling add_event_to_ctx()
  1743. */
  1744. update_cgrp_time_from_event(event);
  1745. add_event_to_ctx(event, ctx);
  1746. /*
  1747. * Schedule everything back in
  1748. */
  1749. perf_event_sched_in(cpuctx, task_ctx, task);
  1750. perf_pmu_enable(cpuctx->ctx.pmu);
  1751. perf_ctx_unlock(cpuctx, task_ctx);
  1752. return 0;
  1753. }
  1754. /*
  1755. * Attach a performance event to a context
  1756. *
  1757. * First we add the event to the list with the hardware enable bit
  1758. * in event->hw_config cleared.
  1759. *
  1760. * If the event is attached to a task which is on a CPU we use a smp
  1761. * call to enable it in the task context. The task might have been
  1762. * scheduled away, but we check this in the smp call again.
  1763. */
  1764. static void
  1765. perf_install_in_context(struct perf_event_context *ctx,
  1766. struct perf_event *event,
  1767. int cpu)
  1768. {
  1769. struct task_struct *task = ctx->task;
  1770. lockdep_assert_held(&ctx->mutex);
  1771. event->ctx = ctx;
  1772. if (event->cpu != -1)
  1773. event->cpu = cpu;
  1774. if (!task) {
  1775. /*
  1776. * Per cpu events are installed via an smp call and
  1777. * the install is always successful.
  1778. */
  1779. cpu_function_call(cpu, __perf_install_in_context, event);
  1780. return;
  1781. }
  1782. retry:
  1783. if (!task_function_call(task, __perf_install_in_context, event))
  1784. return;
  1785. raw_spin_lock_irq(&ctx->lock);
  1786. /*
  1787. * If we failed to find a running task, but find the context active now
  1788. * that we've acquired the ctx->lock, retry.
  1789. */
  1790. if (ctx->is_active) {
  1791. raw_spin_unlock_irq(&ctx->lock);
  1792. /*
  1793. * Reload the task pointer, it might have been changed by
  1794. * a concurrent perf_event_context_sched_out().
  1795. */
  1796. task = ctx->task;
  1797. goto retry;
  1798. }
  1799. /*
  1800. * Since the task isn't running, its safe to add the event, us holding
  1801. * the ctx->lock ensures the task won't get scheduled in.
  1802. */
  1803. add_event_to_ctx(event, ctx);
  1804. raw_spin_unlock_irq(&ctx->lock);
  1805. }
  1806. /*
  1807. * Put a event into inactive state and update time fields.
  1808. * Enabling the leader of a group effectively enables all
  1809. * the group members that aren't explicitly disabled, so we
  1810. * have to update their ->tstamp_enabled also.
  1811. * Note: this works for group members as well as group leaders
  1812. * since the non-leader members' sibling_lists will be empty.
  1813. */
  1814. static void __perf_event_mark_enabled(struct perf_event *event)
  1815. {
  1816. struct perf_event *sub;
  1817. u64 tstamp = perf_event_time(event);
  1818. event->state = PERF_EVENT_STATE_INACTIVE;
  1819. event->tstamp_enabled = tstamp - event->total_time_enabled;
  1820. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  1821. if (sub->state >= PERF_EVENT_STATE_INACTIVE)
  1822. sub->tstamp_enabled = tstamp - sub->total_time_enabled;
  1823. }
  1824. }
  1825. /*
  1826. * Cross CPU call to enable a performance event
  1827. */
  1828. static int __perf_event_enable(void *info)
  1829. {
  1830. struct perf_event *event = info;
  1831. struct perf_event_context *ctx = event->ctx;
  1832. struct perf_event *leader = event->group_leader;
  1833. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1834. int err;
  1835. /*
  1836. * There's a time window between 'ctx->is_active' check
  1837. * in perf_event_enable function and this place having:
  1838. * - IRQs on
  1839. * - ctx->lock unlocked
  1840. *
  1841. * where the task could be killed and 'ctx' deactivated
  1842. * by perf_event_exit_task.
  1843. */
  1844. if (!ctx->is_active)
  1845. return -EINVAL;
  1846. raw_spin_lock(&ctx->lock);
  1847. update_context_time(ctx);
  1848. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1849. goto unlock;
  1850. /*
  1851. * set current task's cgroup time reference point
  1852. */
  1853. perf_cgroup_set_timestamp(current, ctx);
  1854. __perf_event_mark_enabled(event);
  1855. if (!event_filter_match(event)) {
  1856. if (is_cgroup_event(event))
  1857. perf_cgroup_defer_enabled(event);
  1858. goto unlock;
  1859. }
  1860. /*
  1861. * If the event is in a group and isn't the group leader,
  1862. * then don't put it on unless the group is on.
  1863. */
  1864. if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
  1865. goto unlock;
  1866. if (!group_can_go_on(event, cpuctx, 1)) {
  1867. err = -EEXIST;
  1868. } else {
  1869. if (event == leader)
  1870. err = group_sched_in(event, cpuctx, ctx);
  1871. else
  1872. err = event_sched_in(event, cpuctx, ctx);
  1873. }
  1874. if (err) {
  1875. /*
  1876. * If this event can't go on and it's part of a
  1877. * group, then the whole group has to come off.
  1878. */
  1879. if (leader != event) {
  1880. group_sched_out(leader, cpuctx, ctx);
  1881. perf_cpu_hrtimer_restart(cpuctx);
  1882. }
  1883. if (leader->attr.pinned) {
  1884. update_group_times(leader);
  1885. leader->state = PERF_EVENT_STATE_ERROR;
  1886. }
  1887. }
  1888. unlock:
  1889. raw_spin_unlock(&ctx->lock);
  1890. return 0;
  1891. }
  1892. /*
  1893. * Enable a event.
  1894. *
  1895. * If event->ctx is a cloned context, callers must make sure that
  1896. * every task struct that event->ctx->task could possibly point to
  1897. * remains valid. This condition is satisfied when called through
  1898. * perf_event_for_each_child or perf_event_for_each as described
  1899. * for perf_event_disable.
  1900. */
  1901. static void _perf_event_enable(struct perf_event *event)
  1902. {
  1903. struct perf_event_context *ctx = event->ctx;
  1904. struct task_struct *task = ctx->task;
  1905. if (!task) {
  1906. /*
  1907. * Enable the event on the cpu that it's on
  1908. */
  1909. cpu_function_call(event->cpu, __perf_event_enable, event);
  1910. return;
  1911. }
  1912. raw_spin_lock_irq(&ctx->lock);
  1913. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1914. goto out;
  1915. /*
  1916. * If the event is in error state, clear that first.
  1917. * That way, if we see the event in error state below, we
  1918. * know that it has gone back into error state, as distinct
  1919. * from the task having been scheduled away before the
  1920. * cross-call arrived.
  1921. */
  1922. if (event->state == PERF_EVENT_STATE_ERROR)
  1923. event->state = PERF_EVENT_STATE_OFF;
  1924. retry:
  1925. if (!ctx->is_active) {
  1926. __perf_event_mark_enabled(event);
  1927. goto out;
  1928. }
  1929. raw_spin_unlock_irq(&ctx->lock);
  1930. if (!task_function_call(task, __perf_event_enable, event))
  1931. return;
  1932. raw_spin_lock_irq(&ctx->lock);
  1933. /*
  1934. * If the context is active and the event is still off,
  1935. * we need to retry the cross-call.
  1936. */
  1937. if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
  1938. /*
  1939. * task could have been flipped by a concurrent
  1940. * perf_event_context_sched_out()
  1941. */
  1942. task = ctx->task;
  1943. goto retry;
  1944. }
  1945. out:
  1946. raw_spin_unlock_irq(&ctx->lock);
  1947. }
  1948. /*
  1949. * See perf_event_disable();
  1950. */
  1951. void perf_event_enable(struct perf_event *event)
  1952. {
  1953. struct perf_event_context *ctx;
  1954. ctx = perf_event_ctx_lock(event);
  1955. _perf_event_enable(event);
  1956. perf_event_ctx_unlock(event, ctx);
  1957. }
  1958. EXPORT_SYMBOL_GPL(perf_event_enable);
  1959. static int _perf_event_refresh(struct perf_event *event, int refresh)
  1960. {
  1961. /*
  1962. * not supported on inherited events
  1963. */
  1964. if (event->attr.inherit || !is_sampling_event(event))
  1965. return -EINVAL;
  1966. atomic_add(refresh, &event->event_limit);
  1967. _perf_event_enable(event);
  1968. return 0;
  1969. }
  1970. /*
  1971. * See perf_event_disable()
  1972. */
  1973. int perf_event_refresh(struct perf_event *event, int refresh)
  1974. {
  1975. struct perf_event_context *ctx;
  1976. int ret;
  1977. ctx = perf_event_ctx_lock(event);
  1978. ret = _perf_event_refresh(event, refresh);
  1979. perf_event_ctx_unlock(event, ctx);
  1980. return ret;
  1981. }
  1982. EXPORT_SYMBOL_GPL(perf_event_refresh);
  1983. static void ctx_sched_out(struct perf_event_context *ctx,
  1984. struct perf_cpu_context *cpuctx,
  1985. enum event_type_t event_type)
  1986. {
  1987. struct perf_event *event;
  1988. int is_active = ctx->is_active;
  1989. ctx->is_active &= ~event_type;
  1990. if (likely(!ctx->nr_events))
  1991. return;
  1992. update_context_time(ctx);
  1993. update_cgrp_time_from_cpuctx(cpuctx);
  1994. if (!ctx->nr_active)
  1995. return;
  1996. perf_pmu_disable(ctx->pmu);
  1997. if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
  1998. list_for_each_entry(event, &ctx->pinned_groups, group_entry)
  1999. group_sched_out(event, cpuctx, ctx);
  2000. }
  2001. if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
  2002. list_for_each_entry(event, &ctx->flexible_groups, group_entry)
  2003. group_sched_out(event, cpuctx, ctx);
  2004. }
  2005. perf_pmu_enable(ctx->pmu);
  2006. }
  2007. /*
  2008. * Test whether two contexts are equivalent, i.e. whether they have both been
  2009. * cloned from the same version of the same context.
  2010. *
  2011. * Equivalence is measured using a generation number in the context that is
  2012. * incremented on each modification to it; see unclone_ctx(), list_add_event()
  2013. * and list_del_event().
  2014. */
  2015. static int context_equiv(struct perf_event_context *ctx1,
  2016. struct perf_event_context *ctx2)
  2017. {
  2018. lockdep_assert_held(&ctx1->lock);
  2019. lockdep_assert_held(&ctx2->lock);
  2020. /* Pinning disables the swap optimization */
  2021. if (ctx1->pin_count || ctx2->pin_count)
  2022. return 0;
  2023. /* If ctx1 is the parent of ctx2 */
  2024. if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
  2025. return 1;
  2026. /* If ctx2 is the parent of ctx1 */
  2027. if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
  2028. return 1;
  2029. /*
  2030. * If ctx1 and ctx2 have the same parent; we flatten the parent
  2031. * hierarchy, see perf_event_init_context().
  2032. */
  2033. if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
  2034. ctx1->parent_gen == ctx2->parent_gen)
  2035. return 1;
  2036. /* Unmatched */
  2037. return 0;
  2038. }
  2039. static void __perf_event_sync_stat(struct perf_event *event,
  2040. struct perf_event *next_event)
  2041. {
  2042. u64 value;
  2043. if (!event->attr.inherit_stat)
  2044. return;
  2045. /*
  2046. * Update the event value, we cannot use perf_event_read()
  2047. * because we're in the middle of a context switch and have IRQs
  2048. * disabled, which upsets smp_call_function_single(), however
  2049. * we know the event must be on the current CPU, therefore we
  2050. * don't need to use it.
  2051. */
  2052. switch (event->state) {
  2053. case PERF_EVENT_STATE_ACTIVE:
  2054. event->pmu->read(event);
  2055. /* fall-through */
  2056. case PERF_EVENT_STATE_INACTIVE:
  2057. update_event_times(event);
  2058. break;
  2059. default:
  2060. break;
  2061. }
  2062. /*
  2063. * In order to keep per-task stats reliable we need to flip the event
  2064. * values when we flip the contexts.
  2065. */
  2066. value = local64_read(&next_event->count);
  2067. value = local64_xchg(&event->count, value);
  2068. local64_set(&next_event->count, value);
  2069. swap(event->total_time_enabled, next_event->total_time_enabled);
  2070. swap(event->total_time_running, next_event->total_time_running);
  2071. /*
  2072. * Since we swizzled the values, update the user visible data too.
  2073. */
  2074. perf_event_update_userpage(event);
  2075. perf_event_update_userpage(next_event);
  2076. }
  2077. static void perf_event_sync_stat(struct perf_event_context *ctx,
  2078. struct perf_event_context *next_ctx)
  2079. {
  2080. struct perf_event *event, *next_event;
  2081. if (!ctx->nr_stat)
  2082. return;
  2083. update_context_time(ctx);
  2084. event = list_first_entry(&ctx->event_list,
  2085. struct perf_event, event_entry);
  2086. next_event = list_first_entry(&next_ctx->event_list,
  2087. struct perf_event, event_entry);
  2088. while (&event->event_entry != &ctx->event_list &&
  2089. &next_event->event_entry != &next_ctx->event_list) {
  2090. __perf_event_sync_stat(event, next_event);
  2091. event = list_next_entry(event, event_entry);
  2092. next_event = list_next_entry(next_event, event_entry);
  2093. }
  2094. }
  2095. static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
  2096. struct task_struct *next)
  2097. {
  2098. struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
  2099. struct perf_event_context *next_ctx;
  2100. struct perf_event_context *parent, *next_parent;
  2101. struct perf_cpu_context *cpuctx;
  2102. int do_switch = 1;
  2103. if (likely(!ctx))
  2104. return;
  2105. cpuctx = __get_cpu_context(ctx);
  2106. if (!cpuctx->task_ctx)
  2107. return;
  2108. rcu_read_lock();
  2109. next_ctx = next->perf_event_ctxp[ctxn];
  2110. if (!next_ctx)
  2111. goto unlock;
  2112. parent = rcu_dereference(ctx->parent_ctx);
  2113. next_parent = rcu_dereference(next_ctx->parent_ctx);
  2114. /* If neither context have a parent context; they cannot be clones. */
  2115. if (!parent && !next_parent)
  2116. goto unlock;
  2117. if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
  2118. /*
  2119. * Looks like the two contexts are clones, so we might be
  2120. * able to optimize the context switch. We lock both
  2121. * contexts and check that they are clones under the
  2122. * lock (including re-checking that neither has been
  2123. * uncloned in the meantime). It doesn't matter which
  2124. * order we take the locks because no other cpu could
  2125. * be trying to lock both of these tasks.
  2126. */
  2127. raw_spin_lock(&ctx->lock);
  2128. raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
  2129. if (context_equiv(ctx, next_ctx)) {
  2130. /*
  2131. * XXX do we need a memory barrier of sorts
  2132. * wrt to rcu_dereference() of perf_event_ctxp
  2133. */
  2134. task->perf_event_ctxp[ctxn] = next_ctx;
  2135. next->perf_event_ctxp[ctxn] = ctx;
  2136. ctx->task = next;
  2137. next_ctx->task = task;
  2138. swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
  2139. do_switch = 0;
  2140. perf_event_sync_stat(ctx, next_ctx);
  2141. }
  2142. raw_spin_unlock(&next_ctx->lock);
  2143. raw_spin_unlock(&ctx->lock);
  2144. }
  2145. unlock:
  2146. rcu_read_unlock();
  2147. if (do_switch) {
  2148. raw_spin_lock(&ctx->lock);
  2149. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  2150. cpuctx->task_ctx = NULL;
  2151. raw_spin_unlock(&ctx->lock);
  2152. }
  2153. }
  2154. void perf_sched_cb_dec(struct pmu *pmu)
  2155. {
  2156. this_cpu_dec(perf_sched_cb_usages);
  2157. }
  2158. void perf_sched_cb_inc(struct pmu *pmu)
  2159. {
  2160. this_cpu_inc(perf_sched_cb_usages);
  2161. }
  2162. /*
  2163. * This function provides the context switch callback to the lower code
  2164. * layer. It is invoked ONLY when the context switch callback is enabled.
  2165. */
  2166. static void perf_pmu_sched_task(struct task_struct *prev,
  2167. struct task_struct *next,
  2168. bool sched_in)
  2169. {
  2170. struct perf_cpu_context *cpuctx;
  2171. struct pmu *pmu;
  2172. unsigned long flags;
  2173. if (prev == next)
  2174. return;
  2175. local_irq_save(flags);
  2176. rcu_read_lock();
  2177. list_for_each_entry_rcu(pmu, &pmus, entry) {
  2178. if (pmu->sched_task) {
  2179. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  2180. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  2181. perf_pmu_disable(pmu);
  2182. pmu->sched_task(cpuctx->task_ctx, sched_in);
  2183. perf_pmu_enable(pmu);
  2184. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  2185. }
  2186. }
  2187. rcu_read_unlock();
  2188. local_irq_restore(flags);
  2189. }
  2190. #define for_each_task_context_nr(ctxn) \
  2191. for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
  2192. /*
  2193. * Called from scheduler to remove the events of the current task,
  2194. * with interrupts disabled.
  2195. *
  2196. * We stop each event and update the event value in event->count.
  2197. *
  2198. * This does not protect us against NMI, but disable()
  2199. * sets the disabled bit in the control field of event _before_
  2200. * accessing the event control register. If a NMI hits, then it will
  2201. * not restart the event.
  2202. */
  2203. void __perf_event_task_sched_out(struct task_struct *task,
  2204. struct task_struct *next)
  2205. {
  2206. int ctxn;
  2207. if (__this_cpu_read(perf_sched_cb_usages))
  2208. perf_pmu_sched_task(task, next, false);
  2209. for_each_task_context_nr(ctxn)
  2210. perf_event_context_sched_out(task, ctxn, next);
  2211. /*
  2212. * if cgroup events exist on this CPU, then we need
  2213. * to check if we have to switch out PMU state.
  2214. * cgroup event are system-wide mode only
  2215. */
  2216. if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
  2217. perf_cgroup_sched_out(task, next);
  2218. }
  2219. static void task_ctx_sched_out(struct perf_event_context *ctx)
  2220. {
  2221. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  2222. if (!cpuctx->task_ctx)
  2223. return;
  2224. if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  2225. return;
  2226. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  2227. cpuctx->task_ctx = NULL;
  2228. }
  2229. /*
  2230. * Called with IRQs disabled
  2231. */
  2232. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  2233. enum event_type_t event_type)
  2234. {
  2235. ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
  2236. }
  2237. static void
  2238. ctx_pinned_sched_in(struct perf_event_context *ctx,
  2239. struct perf_cpu_context *cpuctx)
  2240. {
  2241. struct perf_event *event;
  2242. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  2243. if (event->state <= PERF_EVENT_STATE_OFF)
  2244. continue;
  2245. if (!event_filter_match(event))
  2246. continue;
  2247. /* may need to reset tstamp_enabled */
  2248. if (is_cgroup_event(event))
  2249. perf_cgroup_mark_enabled(event, ctx);
  2250. if (group_can_go_on(event, cpuctx, 1))
  2251. group_sched_in(event, cpuctx, ctx);
  2252. /*
  2253. * If this pinned group hasn't been scheduled,
  2254. * put it in error state.
  2255. */
  2256. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  2257. update_group_times(event);
  2258. event->state = PERF_EVENT_STATE_ERROR;
  2259. }
  2260. }
  2261. }
  2262. static void
  2263. ctx_flexible_sched_in(struct perf_event_context *ctx,
  2264. struct perf_cpu_context *cpuctx)
  2265. {
  2266. struct perf_event *event;
  2267. int can_add_hw = 1;
  2268. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  2269. /* Ignore events in OFF or ERROR state */
  2270. if (event->state <= PERF_EVENT_STATE_OFF)
  2271. continue;
  2272. /*
  2273. * Listen to the 'cpu' scheduling filter constraint
  2274. * of events:
  2275. */
  2276. if (!event_filter_match(event))
  2277. continue;
  2278. /* may need to reset tstamp_enabled */
  2279. if (is_cgroup_event(event))
  2280. perf_cgroup_mark_enabled(event, ctx);
  2281. if (group_can_go_on(event, cpuctx, can_add_hw)) {
  2282. if (group_sched_in(event, cpuctx, ctx))
  2283. can_add_hw = 0;
  2284. }
  2285. }
  2286. }
  2287. static void
  2288. ctx_sched_in(struct perf_event_context *ctx,
  2289. struct perf_cpu_context *cpuctx,
  2290. enum event_type_t event_type,
  2291. struct task_struct *task)
  2292. {
  2293. u64 now;
  2294. int is_active = ctx->is_active;
  2295. ctx->is_active |= event_type;
  2296. if (likely(!ctx->nr_events))
  2297. return;
  2298. now = perf_clock();
  2299. ctx->timestamp = now;
  2300. perf_cgroup_set_timestamp(task, ctx);
  2301. /*
  2302. * First go through the list and put on any pinned groups
  2303. * in order to give them the best chance of going on.
  2304. */
  2305. if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
  2306. ctx_pinned_sched_in(ctx, cpuctx);
  2307. /* Then walk through the lower prio flexible groups */
  2308. if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
  2309. ctx_flexible_sched_in(ctx, cpuctx);
  2310. }
  2311. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  2312. enum event_type_t event_type,
  2313. struct task_struct *task)
  2314. {
  2315. struct perf_event_context *ctx = &cpuctx->ctx;
  2316. ctx_sched_in(ctx, cpuctx, event_type, task);
  2317. }
  2318. static void perf_event_context_sched_in(struct perf_event_context *ctx,
  2319. struct task_struct *task)
  2320. {
  2321. struct perf_cpu_context *cpuctx;
  2322. cpuctx = __get_cpu_context(ctx);
  2323. if (cpuctx->task_ctx == ctx)
  2324. return;
  2325. perf_ctx_lock(cpuctx, ctx);
  2326. perf_pmu_disable(ctx->pmu);
  2327. /*
  2328. * We want to keep the following priority order:
  2329. * cpu pinned (that don't need to move), task pinned,
  2330. * cpu flexible, task flexible.
  2331. */
  2332. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  2333. if (ctx->nr_events)
  2334. cpuctx->task_ctx = ctx;
  2335. perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
  2336. perf_pmu_enable(ctx->pmu);
  2337. perf_ctx_unlock(cpuctx, ctx);
  2338. }
  2339. /*
  2340. * Called from scheduler to add the events of the current task
  2341. * with interrupts disabled.
  2342. *
  2343. * We restore the event value and then enable it.
  2344. *
  2345. * This does not protect us against NMI, but enable()
  2346. * sets the enabled bit in the control field of event _before_
  2347. * accessing the event control register. If a NMI hits, then it will
  2348. * keep the event running.
  2349. */
  2350. void __perf_event_task_sched_in(struct task_struct *prev,
  2351. struct task_struct *task)
  2352. {
  2353. struct perf_event_context *ctx;
  2354. int ctxn;
  2355. for_each_task_context_nr(ctxn) {
  2356. ctx = task->perf_event_ctxp[ctxn];
  2357. if (likely(!ctx))
  2358. continue;
  2359. perf_event_context_sched_in(ctx, task);
  2360. }
  2361. /*
  2362. * if cgroup events exist on this CPU, then we need
  2363. * to check if we have to switch in PMU state.
  2364. * cgroup event are system-wide mode only
  2365. */
  2366. if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
  2367. perf_cgroup_sched_in(prev, task);
  2368. if (__this_cpu_read(perf_sched_cb_usages))
  2369. perf_pmu_sched_task(prev, task, true);
  2370. }
  2371. static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
  2372. {
  2373. u64 frequency = event->attr.sample_freq;
  2374. u64 sec = NSEC_PER_SEC;
  2375. u64 divisor, dividend;
  2376. int count_fls, nsec_fls, frequency_fls, sec_fls;
  2377. count_fls = fls64(count);
  2378. nsec_fls = fls64(nsec);
  2379. frequency_fls = fls64(frequency);
  2380. sec_fls = 30;
  2381. /*
  2382. * We got @count in @nsec, with a target of sample_freq HZ
  2383. * the target period becomes:
  2384. *
  2385. * @count * 10^9
  2386. * period = -------------------
  2387. * @nsec * sample_freq
  2388. *
  2389. */
  2390. /*
  2391. * Reduce accuracy by one bit such that @a and @b converge
  2392. * to a similar magnitude.
  2393. */
  2394. #define REDUCE_FLS(a, b) \
  2395. do { \
  2396. if (a##_fls > b##_fls) { \
  2397. a >>= 1; \
  2398. a##_fls--; \
  2399. } else { \
  2400. b >>= 1; \
  2401. b##_fls--; \
  2402. } \
  2403. } while (0)
  2404. /*
  2405. * Reduce accuracy until either term fits in a u64, then proceed with
  2406. * the other, so that finally we can do a u64/u64 division.
  2407. */
  2408. while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
  2409. REDUCE_FLS(nsec, frequency);
  2410. REDUCE_FLS(sec, count);
  2411. }
  2412. if (count_fls + sec_fls > 64) {
  2413. divisor = nsec * frequency;
  2414. while (count_fls + sec_fls > 64) {
  2415. REDUCE_FLS(count, sec);
  2416. divisor >>= 1;
  2417. }
  2418. dividend = count * sec;
  2419. } else {
  2420. dividend = count * sec;
  2421. while (nsec_fls + frequency_fls > 64) {
  2422. REDUCE_FLS(nsec, frequency);
  2423. dividend >>= 1;
  2424. }
  2425. divisor = nsec * frequency;
  2426. }
  2427. if (!divisor)
  2428. return dividend;
  2429. return div64_u64(dividend, divisor);
  2430. }
  2431. static DEFINE_PER_CPU(int, perf_throttled_count);
  2432. static DEFINE_PER_CPU(u64, perf_throttled_seq);
  2433. static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
  2434. {
  2435. struct hw_perf_event *hwc = &event->hw;
  2436. s64 period, sample_period;
  2437. s64 delta;
  2438. period = perf_calculate_period(event, nsec, count);
  2439. delta = (s64)(period - hwc->sample_period);
  2440. delta = (delta + 7) / 8; /* low pass filter */
  2441. sample_period = hwc->sample_period + delta;
  2442. if (!sample_period)
  2443. sample_period = 1;
  2444. hwc->sample_period = sample_period;
  2445. if (local64_read(&hwc->period_left) > 8*sample_period) {
  2446. if (disable)
  2447. event->pmu->stop(event, PERF_EF_UPDATE);
  2448. local64_set(&hwc->period_left, 0);
  2449. if (disable)
  2450. event->pmu->start(event, PERF_EF_RELOAD);
  2451. }
  2452. }
  2453. /*
  2454. * combine freq adjustment with unthrottling to avoid two passes over the
  2455. * events. At the same time, make sure, having freq events does not change
  2456. * the rate of unthrottling as that would introduce bias.
  2457. */
  2458. static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
  2459. int needs_unthr)
  2460. {
  2461. struct perf_event *event;
  2462. struct hw_perf_event *hwc;
  2463. u64 now, period = TICK_NSEC;
  2464. s64 delta;
  2465. /*
  2466. * only need to iterate over all events iff:
  2467. * - context have events in frequency mode (needs freq adjust)
  2468. * - there are events to unthrottle on this cpu
  2469. */
  2470. if (!(ctx->nr_freq || needs_unthr))
  2471. return;
  2472. raw_spin_lock(&ctx->lock);
  2473. perf_pmu_disable(ctx->pmu);
  2474. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  2475. if (event->state != PERF_EVENT_STATE_ACTIVE)
  2476. continue;
  2477. if (!event_filter_match(event))
  2478. continue;
  2479. perf_pmu_disable(event->pmu);
  2480. hwc = &event->hw;
  2481. if (hwc->interrupts == MAX_INTERRUPTS) {
  2482. hwc->interrupts = 0;
  2483. perf_log_throttle(event, 1);
  2484. event->pmu->start(event, 0);
  2485. }
  2486. if (!event->attr.freq || !event->attr.sample_freq)
  2487. goto next;
  2488. /*
  2489. * stop the event and update event->count
  2490. */
  2491. event->pmu->stop(event, PERF_EF_UPDATE);
  2492. now = local64_read(&event->count);
  2493. delta = now - hwc->freq_count_stamp;
  2494. hwc->freq_count_stamp = now;
  2495. /*
  2496. * restart the event
  2497. * reload only if value has changed
  2498. * we have stopped the event so tell that
  2499. * to perf_adjust_period() to avoid stopping it
  2500. * twice.
  2501. */
  2502. if (delta > 0)
  2503. perf_adjust_period(event, period, delta, false);
  2504. event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
  2505. next:
  2506. perf_pmu_enable(event->pmu);
  2507. }
  2508. perf_pmu_enable(ctx->pmu);
  2509. raw_spin_unlock(&ctx->lock);
  2510. }
  2511. /*
  2512. * Round-robin a context's events:
  2513. */
  2514. static void rotate_ctx(struct perf_event_context *ctx)
  2515. {
  2516. /*
  2517. * Rotate the first entry last of non-pinned groups. Rotation might be
  2518. * disabled by the inheritance code.
  2519. */
  2520. if (!ctx->rotate_disable)
  2521. list_rotate_left(&ctx->flexible_groups);
  2522. }
  2523. static int perf_rotate_context(struct perf_cpu_context *cpuctx)
  2524. {
  2525. struct perf_event_context *ctx = NULL;
  2526. int rotate = 0;
  2527. if (cpuctx->ctx.nr_events) {
  2528. if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
  2529. rotate = 1;
  2530. }
  2531. ctx = cpuctx->task_ctx;
  2532. if (ctx && ctx->nr_events) {
  2533. if (ctx->nr_events != ctx->nr_active)
  2534. rotate = 1;
  2535. }
  2536. if (!rotate)
  2537. goto done;
  2538. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  2539. perf_pmu_disable(cpuctx->ctx.pmu);
  2540. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  2541. if (ctx)
  2542. ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
  2543. rotate_ctx(&cpuctx->ctx);
  2544. if (ctx)
  2545. rotate_ctx(ctx);
  2546. perf_event_sched_in(cpuctx, ctx, current);
  2547. perf_pmu_enable(cpuctx->ctx.pmu);
  2548. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  2549. done:
  2550. return rotate;
  2551. }
  2552. #ifdef CONFIG_NO_HZ_FULL
  2553. bool perf_event_can_stop_tick(void)
  2554. {
  2555. if (atomic_read(&nr_freq_events) ||
  2556. __this_cpu_read(perf_throttled_count))
  2557. return false;
  2558. else
  2559. return true;
  2560. }
  2561. #endif
  2562. void perf_event_task_tick(void)
  2563. {
  2564. struct list_head *head = this_cpu_ptr(&active_ctx_list);
  2565. struct perf_event_context *ctx, *tmp;
  2566. int throttled;
  2567. WARN_ON(!irqs_disabled());
  2568. __this_cpu_inc(perf_throttled_seq);
  2569. throttled = __this_cpu_xchg(perf_throttled_count, 0);
  2570. list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
  2571. perf_adjust_freq_unthr_context(ctx, throttled);
  2572. }
  2573. static int event_enable_on_exec(struct perf_event *event,
  2574. struct perf_event_context *ctx)
  2575. {
  2576. if (!event->attr.enable_on_exec)
  2577. return 0;
  2578. event->attr.enable_on_exec = 0;
  2579. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  2580. return 0;
  2581. __perf_event_mark_enabled(event);
  2582. return 1;
  2583. }
  2584. /*
  2585. * Enable all of a task's events that have been marked enable-on-exec.
  2586. * This expects task == current.
  2587. */
  2588. static void perf_event_enable_on_exec(struct perf_event_context *ctx)
  2589. {
  2590. struct perf_event_context *clone_ctx = NULL;
  2591. struct perf_event *event;
  2592. unsigned long flags;
  2593. int enabled = 0;
  2594. int ret;
  2595. local_irq_save(flags);
  2596. if (!ctx || !ctx->nr_events)
  2597. goto out;
  2598. /*
  2599. * We must ctxsw out cgroup events to avoid conflict
  2600. * when invoking perf_task_event_sched_in() later on
  2601. * in this function. Otherwise we end up trying to
  2602. * ctxswin cgroup events which are already scheduled
  2603. * in.
  2604. */
  2605. perf_cgroup_sched_out(current, NULL);
  2606. raw_spin_lock(&ctx->lock);
  2607. task_ctx_sched_out(ctx);
  2608. list_for_each_entry(event, &ctx->event_list, event_entry) {
  2609. ret = event_enable_on_exec(event, ctx);
  2610. if (ret)
  2611. enabled = 1;
  2612. }
  2613. /*
  2614. * Unclone this context if we enabled any event.
  2615. */
  2616. if (enabled)
  2617. clone_ctx = unclone_ctx(ctx);
  2618. raw_spin_unlock(&ctx->lock);
  2619. /*
  2620. * Also calls ctxswin for cgroup events, if any:
  2621. */
  2622. perf_event_context_sched_in(ctx, ctx->task);
  2623. out:
  2624. local_irq_restore(flags);
  2625. if (clone_ctx)
  2626. put_ctx(clone_ctx);
  2627. }
  2628. void perf_event_exec(void)
  2629. {
  2630. struct perf_event_context *ctx;
  2631. int ctxn;
  2632. rcu_read_lock();
  2633. for_each_task_context_nr(ctxn) {
  2634. ctx = current->perf_event_ctxp[ctxn];
  2635. if (!ctx)
  2636. continue;
  2637. perf_event_enable_on_exec(ctx);
  2638. }
  2639. rcu_read_unlock();
  2640. }
  2641. /*
  2642. * Cross CPU call to read the hardware event
  2643. */
  2644. static void __perf_event_read(void *info)
  2645. {
  2646. struct perf_event *event = info;
  2647. struct perf_event_context *ctx = event->ctx;
  2648. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  2649. /*
  2650. * If this is a task context, we need to check whether it is
  2651. * the current task context of this cpu. If not it has been
  2652. * scheduled out before the smp call arrived. In that case
  2653. * event->count would have been updated to a recent sample
  2654. * when the event was scheduled out.
  2655. */
  2656. if (ctx->task && cpuctx->task_ctx != ctx)
  2657. return;
  2658. raw_spin_lock(&ctx->lock);
  2659. if (ctx->is_active) {
  2660. update_context_time(ctx);
  2661. update_cgrp_time_from_event(event);
  2662. }
  2663. update_event_times(event);
  2664. if (event->state == PERF_EVENT_STATE_ACTIVE)
  2665. event->pmu->read(event);
  2666. raw_spin_unlock(&ctx->lock);
  2667. }
  2668. static inline u64 perf_event_count(struct perf_event *event)
  2669. {
  2670. if (event->pmu->count)
  2671. return event->pmu->count(event);
  2672. return __perf_event_count(event);
  2673. }
  2674. static u64 perf_event_read(struct perf_event *event)
  2675. {
  2676. /*
  2677. * If event is enabled and currently active on a CPU, update the
  2678. * value in the event structure:
  2679. */
  2680. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  2681. smp_call_function_single(event->oncpu,
  2682. __perf_event_read, event, 1);
  2683. } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
  2684. struct perf_event_context *ctx = event->ctx;
  2685. unsigned long flags;
  2686. raw_spin_lock_irqsave(&ctx->lock, flags);
  2687. /*
  2688. * may read while context is not active
  2689. * (e.g., thread is blocked), in that case
  2690. * we cannot update context time
  2691. */
  2692. if (ctx->is_active) {
  2693. update_context_time(ctx);
  2694. update_cgrp_time_from_event(event);
  2695. }
  2696. update_event_times(event);
  2697. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  2698. }
  2699. return perf_event_count(event);
  2700. }
  2701. /*
  2702. * Initialize the perf_event context in a task_struct:
  2703. */
  2704. static void __perf_event_init_context(struct perf_event_context *ctx)
  2705. {
  2706. raw_spin_lock_init(&ctx->lock);
  2707. mutex_init(&ctx->mutex);
  2708. INIT_LIST_HEAD(&ctx->active_ctx_list);
  2709. INIT_LIST_HEAD(&ctx->pinned_groups);
  2710. INIT_LIST_HEAD(&ctx->flexible_groups);
  2711. INIT_LIST_HEAD(&ctx->event_list);
  2712. atomic_set(&ctx->refcount, 1);
  2713. INIT_DELAYED_WORK(&ctx->orphans_remove, orphans_remove_work);
  2714. }
  2715. static struct perf_event_context *
  2716. alloc_perf_context(struct pmu *pmu, struct task_struct *task)
  2717. {
  2718. struct perf_event_context *ctx;
  2719. ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  2720. if (!ctx)
  2721. return NULL;
  2722. __perf_event_init_context(ctx);
  2723. if (task) {
  2724. ctx->task = task;
  2725. get_task_struct(task);
  2726. }
  2727. ctx->pmu = pmu;
  2728. return ctx;
  2729. }
  2730. static struct task_struct *
  2731. find_lively_task_by_vpid(pid_t vpid)
  2732. {
  2733. struct task_struct *task;
  2734. int err;
  2735. rcu_read_lock();
  2736. if (!vpid)
  2737. task = current;
  2738. else
  2739. task = find_task_by_vpid(vpid);
  2740. if (task)
  2741. get_task_struct(task);
  2742. rcu_read_unlock();
  2743. if (!task)
  2744. return ERR_PTR(-ESRCH);
  2745. /* Reuse ptrace permission checks for now. */
  2746. err = -EACCES;
  2747. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  2748. goto errout;
  2749. return task;
  2750. errout:
  2751. put_task_struct(task);
  2752. return ERR_PTR(err);
  2753. }
  2754. /*
  2755. * Returns a matching context with refcount and pincount.
  2756. */
  2757. static struct perf_event_context *
  2758. find_get_context(struct pmu *pmu, struct task_struct *task,
  2759. struct perf_event *event)
  2760. {
  2761. struct perf_event_context *ctx, *clone_ctx = NULL;
  2762. struct perf_cpu_context *cpuctx;
  2763. void *task_ctx_data = NULL;
  2764. unsigned long flags;
  2765. int ctxn, err;
  2766. int cpu = event->cpu;
  2767. if (!task) {
  2768. /* Must be root to operate on a CPU event: */
  2769. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  2770. return ERR_PTR(-EACCES);
  2771. /*
  2772. * We could be clever and allow to attach a event to an
  2773. * offline CPU and activate it when the CPU comes up, but
  2774. * that's for later.
  2775. */
  2776. if (!cpu_online(cpu))
  2777. return ERR_PTR(-ENODEV);
  2778. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  2779. ctx = &cpuctx->ctx;
  2780. get_ctx(ctx);
  2781. ++ctx->pin_count;
  2782. return ctx;
  2783. }
  2784. err = -EINVAL;
  2785. ctxn = pmu->task_ctx_nr;
  2786. if (ctxn < 0)
  2787. goto errout;
  2788. if (event->attach_state & PERF_ATTACH_TASK_DATA) {
  2789. task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
  2790. if (!task_ctx_data) {
  2791. err = -ENOMEM;
  2792. goto errout;
  2793. }
  2794. }
  2795. retry:
  2796. ctx = perf_lock_task_context(task, ctxn, &flags);
  2797. if (ctx) {
  2798. clone_ctx = unclone_ctx(ctx);
  2799. ++ctx->pin_count;
  2800. if (task_ctx_data && !ctx->task_ctx_data) {
  2801. ctx->task_ctx_data = task_ctx_data;
  2802. task_ctx_data = NULL;
  2803. }
  2804. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  2805. if (clone_ctx)
  2806. put_ctx(clone_ctx);
  2807. } else {
  2808. ctx = alloc_perf_context(pmu, task);
  2809. err = -ENOMEM;
  2810. if (!ctx)
  2811. goto errout;
  2812. if (task_ctx_data) {
  2813. ctx->task_ctx_data = task_ctx_data;
  2814. task_ctx_data = NULL;
  2815. }
  2816. err = 0;
  2817. mutex_lock(&task->perf_event_mutex);
  2818. /*
  2819. * If it has already passed perf_event_exit_task().
  2820. * we must see PF_EXITING, it takes this mutex too.
  2821. */
  2822. if (task->flags & PF_EXITING)
  2823. err = -ESRCH;
  2824. else if (task->perf_event_ctxp[ctxn])
  2825. err = -EAGAIN;
  2826. else {
  2827. get_ctx(ctx);
  2828. ++ctx->pin_count;
  2829. rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
  2830. }
  2831. mutex_unlock(&task->perf_event_mutex);
  2832. if (unlikely(err)) {
  2833. put_ctx(ctx);
  2834. if (err == -EAGAIN)
  2835. goto retry;
  2836. goto errout;
  2837. }
  2838. }
  2839. kfree(task_ctx_data);
  2840. return ctx;
  2841. errout:
  2842. kfree(task_ctx_data);
  2843. return ERR_PTR(err);
  2844. }
  2845. static void perf_event_free_filter(struct perf_event *event);
  2846. static void perf_event_free_bpf_prog(struct perf_event *event);
  2847. static void free_event_rcu(struct rcu_head *head)
  2848. {
  2849. struct perf_event *event;
  2850. event = container_of(head, struct perf_event, rcu_head);
  2851. if (event->ns)
  2852. put_pid_ns(event->ns);
  2853. perf_event_free_filter(event);
  2854. kfree(event);
  2855. }
  2856. static void ring_buffer_attach(struct perf_event *event,
  2857. struct ring_buffer *rb);
  2858. static void unaccount_event_cpu(struct perf_event *event, int cpu)
  2859. {
  2860. if (event->parent)
  2861. return;
  2862. if (is_cgroup_event(event))
  2863. atomic_dec(&per_cpu(perf_cgroup_events, cpu));
  2864. }
  2865. static void unaccount_event(struct perf_event *event)
  2866. {
  2867. if (event->parent)
  2868. return;
  2869. if (event->attach_state & PERF_ATTACH_TASK)
  2870. static_key_slow_dec_deferred(&perf_sched_events);
  2871. if (event->attr.mmap || event->attr.mmap_data)
  2872. atomic_dec(&nr_mmap_events);
  2873. if (event->attr.comm)
  2874. atomic_dec(&nr_comm_events);
  2875. if (event->attr.task)
  2876. atomic_dec(&nr_task_events);
  2877. if (event->attr.freq)
  2878. atomic_dec(&nr_freq_events);
  2879. if (is_cgroup_event(event))
  2880. static_key_slow_dec_deferred(&perf_sched_events);
  2881. if (has_branch_stack(event))
  2882. static_key_slow_dec_deferred(&perf_sched_events);
  2883. unaccount_event_cpu(event, event->cpu);
  2884. }
  2885. /*
  2886. * The following implement mutual exclusion of events on "exclusive" pmus
  2887. * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
  2888. * at a time, so we disallow creating events that might conflict, namely:
  2889. *
  2890. * 1) cpu-wide events in the presence of per-task events,
  2891. * 2) per-task events in the presence of cpu-wide events,
  2892. * 3) two matching events on the same context.
  2893. *
  2894. * The former two cases are handled in the allocation path (perf_event_alloc(),
  2895. * __free_event()), the latter -- before the first perf_install_in_context().
  2896. */
  2897. static int exclusive_event_init(struct perf_event *event)
  2898. {
  2899. struct pmu *pmu = event->pmu;
  2900. if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
  2901. return 0;
  2902. /*
  2903. * Prevent co-existence of per-task and cpu-wide events on the
  2904. * same exclusive pmu.
  2905. *
  2906. * Negative pmu::exclusive_cnt means there are cpu-wide
  2907. * events on this "exclusive" pmu, positive means there are
  2908. * per-task events.
  2909. *
  2910. * Since this is called in perf_event_alloc() path, event::ctx
  2911. * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
  2912. * to mean "per-task event", because unlike other attach states it
  2913. * never gets cleared.
  2914. */
  2915. if (event->attach_state & PERF_ATTACH_TASK) {
  2916. if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
  2917. return -EBUSY;
  2918. } else {
  2919. if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
  2920. return -EBUSY;
  2921. }
  2922. return 0;
  2923. }
  2924. static void exclusive_event_destroy(struct perf_event *event)
  2925. {
  2926. struct pmu *pmu = event->pmu;
  2927. if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
  2928. return;
  2929. /* see comment in exclusive_event_init() */
  2930. if (event->attach_state & PERF_ATTACH_TASK)
  2931. atomic_dec(&pmu->exclusive_cnt);
  2932. else
  2933. atomic_inc(&pmu->exclusive_cnt);
  2934. }
  2935. static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
  2936. {
  2937. if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
  2938. (e1->cpu == e2->cpu ||
  2939. e1->cpu == -1 ||
  2940. e2->cpu == -1))
  2941. return true;
  2942. return false;
  2943. }
  2944. /* Called under the same ctx::mutex as perf_install_in_context() */
  2945. static bool exclusive_event_installable(struct perf_event *event,
  2946. struct perf_event_context *ctx)
  2947. {
  2948. struct perf_event *iter_event;
  2949. struct pmu *pmu = event->pmu;
  2950. if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
  2951. return true;
  2952. list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
  2953. if (exclusive_event_match(iter_event, event))
  2954. return false;
  2955. }
  2956. return true;
  2957. }
  2958. static void __free_event(struct perf_event *event)
  2959. {
  2960. if (!event->parent) {
  2961. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  2962. put_callchain_buffers();
  2963. }
  2964. perf_event_free_bpf_prog(event);
  2965. if (event->destroy)
  2966. event->destroy(event);
  2967. if (event->ctx)
  2968. put_ctx(event->ctx);
  2969. if (event->pmu) {
  2970. exclusive_event_destroy(event);
  2971. module_put(event->pmu->module);
  2972. }
  2973. call_rcu(&event->rcu_head, free_event_rcu);
  2974. }
  2975. static void _free_event(struct perf_event *event)
  2976. {
  2977. irq_work_sync(&event->pending);
  2978. unaccount_event(event);
  2979. if (event->rb) {
  2980. /*
  2981. * Can happen when we close an event with re-directed output.
  2982. *
  2983. * Since we have a 0 refcount, perf_mmap_close() will skip
  2984. * over us; possibly making our ring_buffer_put() the last.
  2985. */
  2986. mutex_lock(&event->mmap_mutex);
  2987. ring_buffer_attach(event, NULL);
  2988. mutex_unlock(&event->mmap_mutex);
  2989. }
  2990. if (is_cgroup_event(event))
  2991. perf_detach_cgroup(event);
  2992. __free_event(event);
  2993. }
  2994. /*
  2995. * Used to free events which have a known refcount of 1, such as in error paths
  2996. * where the event isn't exposed yet and inherited events.
  2997. */
  2998. static void free_event(struct perf_event *event)
  2999. {
  3000. if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
  3001. "unexpected event refcount: %ld; ptr=%p\n",
  3002. atomic_long_read(&event->refcount), event)) {
  3003. /* leak to avoid use-after-free */
  3004. return;
  3005. }
  3006. _free_event(event);
  3007. }
  3008. /*
  3009. * Remove user event from the owner task.
  3010. */
  3011. static void perf_remove_from_owner(struct perf_event *event)
  3012. {
  3013. struct task_struct *owner;
  3014. rcu_read_lock();
  3015. owner = ACCESS_ONCE(event->owner);
  3016. /*
  3017. * Matches the smp_wmb() in perf_event_exit_task(). If we observe
  3018. * !owner it means the list deletion is complete and we can indeed
  3019. * free this event, otherwise we need to serialize on
  3020. * owner->perf_event_mutex.
  3021. */
  3022. smp_read_barrier_depends();
  3023. if (owner) {
  3024. /*
  3025. * Since delayed_put_task_struct() also drops the last
  3026. * task reference we can safely take a new reference
  3027. * while holding the rcu_read_lock().
  3028. */
  3029. get_task_struct(owner);
  3030. }
  3031. rcu_read_unlock();
  3032. if (owner) {
  3033. /*
  3034. * If we're here through perf_event_exit_task() we're already
  3035. * holding ctx->mutex which would be an inversion wrt. the
  3036. * normal lock order.
  3037. *
  3038. * However we can safely take this lock because its the child
  3039. * ctx->mutex.
  3040. */
  3041. mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
  3042. /*
  3043. * We have to re-check the event->owner field, if it is cleared
  3044. * we raced with perf_event_exit_task(), acquiring the mutex
  3045. * ensured they're done, and we can proceed with freeing the
  3046. * event.
  3047. */
  3048. if (event->owner)
  3049. list_del_init(&event->owner_entry);
  3050. mutex_unlock(&owner->perf_event_mutex);
  3051. put_task_struct(owner);
  3052. }
  3053. }
  3054. static void put_event(struct perf_event *event)
  3055. {
  3056. struct perf_event_context *ctx;
  3057. if (!atomic_long_dec_and_test(&event->refcount))
  3058. return;
  3059. if (!is_kernel_event(event))
  3060. perf_remove_from_owner(event);
  3061. /*
  3062. * There are two ways this annotation is useful:
  3063. *
  3064. * 1) there is a lock recursion from perf_event_exit_task
  3065. * see the comment there.
  3066. *
  3067. * 2) there is a lock-inversion with mmap_sem through
  3068. * perf_event_read_group(), which takes faults while
  3069. * holding ctx->mutex, however this is called after
  3070. * the last filedesc died, so there is no possibility
  3071. * to trigger the AB-BA case.
  3072. */
  3073. ctx = perf_event_ctx_lock_nested(event, SINGLE_DEPTH_NESTING);
  3074. WARN_ON_ONCE(ctx->parent_ctx);
  3075. perf_remove_from_context(event, true);
  3076. perf_event_ctx_unlock(event, ctx);
  3077. _free_event(event);
  3078. }
  3079. int perf_event_release_kernel(struct perf_event *event)
  3080. {
  3081. put_event(event);
  3082. return 0;
  3083. }
  3084. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  3085. /*
  3086. * Called when the last reference to the file is gone.
  3087. */
  3088. static int perf_release(struct inode *inode, struct file *file)
  3089. {
  3090. put_event(file->private_data);
  3091. return 0;
  3092. }
  3093. /*
  3094. * Remove all orphanes events from the context.
  3095. */
  3096. static void orphans_remove_work(struct work_struct *work)
  3097. {
  3098. struct perf_event_context *ctx;
  3099. struct perf_event *event, *tmp;
  3100. ctx = container_of(work, struct perf_event_context,
  3101. orphans_remove.work);
  3102. mutex_lock(&ctx->mutex);
  3103. list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) {
  3104. struct perf_event *parent_event = event->parent;
  3105. if (!is_orphaned_child(event))
  3106. continue;
  3107. perf_remove_from_context(event, true);
  3108. mutex_lock(&parent_event->child_mutex);
  3109. list_del_init(&event->child_list);
  3110. mutex_unlock(&parent_event->child_mutex);
  3111. free_event(event);
  3112. put_event(parent_event);
  3113. }
  3114. raw_spin_lock_irq(&ctx->lock);
  3115. ctx->orphans_remove_sched = false;
  3116. raw_spin_unlock_irq(&ctx->lock);
  3117. mutex_unlock(&ctx->mutex);
  3118. put_ctx(ctx);
  3119. }
  3120. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  3121. {
  3122. struct perf_event *child;
  3123. u64 total = 0;
  3124. *enabled = 0;
  3125. *running = 0;
  3126. mutex_lock(&event->child_mutex);
  3127. total += perf_event_read(event);
  3128. *enabled += event->total_time_enabled +
  3129. atomic64_read(&event->child_total_time_enabled);
  3130. *running += event->total_time_running +
  3131. atomic64_read(&event->child_total_time_running);
  3132. list_for_each_entry(child, &event->child_list, child_list) {
  3133. total += perf_event_read(child);
  3134. *enabled += child->total_time_enabled;
  3135. *running += child->total_time_running;
  3136. }
  3137. mutex_unlock(&event->child_mutex);
  3138. return total;
  3139. }
  3140. EXPORT_SYMBOL_GPL(perf_event_read_value);
  3141. static int perf_event_read_group(struct perf_event *event,
  3142. u64 read_format, char __user *buf)
  3143. {
  3144. struct perf_event *leader = event->group_leader, *sub;
  3145. struct perf_event_context *ctx = leader->ctx;
  3146. int n = 0, size = 0, ret;
  3147. u64 count, enabled, running;
  3148. u64 values[5];
  3149. lockdep_assert_held(&ctx->mutex);
  3150. count = perf_event_read_value(leader, &enabled, &running);
  3151. values[n++] = 1 + leader->nr_siblings;
  3152. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  3153. values[n++] = enabled;
  3154. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  3155. values[n++] = running;
  3156. values[n++] = count;
  3157. if (read_format & PERF_FORMAT_ID)
  3158. values[n++] = primary_event_id(leader);
  3159. size = n * sizeof(u64);
  3160. if (copy_to_user(buf, values, size))
  3161. return -EFAULT;
  3162. ret = size;
  3163. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  3164. n = 0;
  3165. values[n++] = perf_event_read_value(sub, &enabled, &running);
  3166. if (read_format & PERF_FORMAT_ID)
  3167. values[n++] = primary_event_id(sub);
  3168. size = n * sizeof(u64);
  3169. if (copy_to_user(buf + ret, values, size)) {
  3170. return -EFAULT;
  3171. }
  3172. ret += size;
  3173. }
  3174. return ret;
  3175. }
  3176. static int perf_event_read_one(struct perf_event *event,
  3177. u64 read_format, char __user *buf)
  3178. {
  3179. u64 enabled, running;
  3180. u64 values[4];
  3181. int n = 0;
  3182. values[n++] = perf_event_read_value(event, &enabled, &running);
  3183. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  3184. values[n++] = enabled;
  3185. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  3186. values[n++] = running;
  3187. if (read_format & PERF_FORMAT_ID)
  3188. values[n++] = primary_event_id(event);
  3189. if (copy_to_user(buf, values, n * sizeof(u64)))
  3190. return -EFAULT;
  3191. return n * sizeof(u64);
  3192. }
  3193. static bool is_event_hup(struct perf_event *event)
  3194. {
  3195. bool no_children;
  3196. if (event->state != PERF_EVENT_STATE_EXIT)
  3197. return false;
  3198. mutex_lock(&event->child_mutex);
  3199. no_children = list_empty(&event->child_list);
  3200. mutex_unlock(&event->child_mutex);
  3201. return no_children;
  3202. }
  3203. /*
  3204. * Read the performance event - simple non blocking version for now
  3205. */
  3206. static ssize_t
  3207. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  3208. {
  3209. u64 read_format = event->attr.read_format;
  3210. int ret;
  3211. /*
  3212. * Return end-of-file for a read on a event that is in
  3213. * error state (i.e. because it was pinned but it couldn't be
  3214. * scheduled on to the CPU at some point).
  3215. */
  3216. if (event->state == PERF_EVENT_STATE_ERROR)
  3217. return 0;
  3218. if (count < event->read_size)
  3219. return -ENOSPC;
  3220. WARN_ON_ONCE(event->ctx->parent_ctx);
  3221. if (read_format & PERF_FORMAT_GROUP)
  3222. ret = perf_event_read_group(event, read_format, buf);
  3223. else
  3224. ret = perf_event_read_one(event, read_format, buf);
  3225. return ret;
  3226. }
  3227. static ssize_t
  3228. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  3229. {
  3230. struct perf_event *event = file->private_data;
  3231. struct perf_event_context *ctx;
  3232. int ret;
  3233. ctx = perf_event_ctx_lock(event);
  3234. ret = perf_read_hw(event, buf, count);
  3235. perf_event_ctx_unlock(event, ctx);
  3236. return ret;
  3237. }
  3238. static unsigned int perf_poll(struct file *file, poll_table *wait)
  3239. {
  3240. struct perf_event *event = file->private_data;
  3241. struct ring_buffer *rb;
  3242. unsigned int events = POLLHUP;
  3243. poll_wait(file, &event->waitq, wait);
  3244. if (is_event_hup(event))
  3245. return events;
  3246. /*
  3247. * Pin the event->rb by taking event->mmap_mutex; otherwise
  3248. * perf_event_set_output() can swizzle our rb and make us miss wakeups.
  3249. */
  3250. mutex_lock(&event->mmap_mutex);
  3251. rb = event->rb;
  3252. if (rb)
  3253. events = atomic_xchg(&rb->poll, 0);
  3254. mutex_unlock(&event->mmap_mutex);
  3255. return events;
  3256. }
  3257. static void _perf_event_reset(struct perf_event *event)
  3258. {
  3259. (void)perf_event_read(event);
  3260. local64_set(&event->count, 0);
  3261. perf_event_update_userpage(event);
  3262. }
  3263. /*
  3264. * Holding the top-level event's child_mutex means that any
  3265. * descendant process that has inherited this event will block
  3266. * in sync_child_event if it goes to exit, thus satisfying the
  3267. * task existence requirements of perf_event_enable/disable.
  3268. */
  3269. static void perf_event_for_each_child(struct perf_event *event,
  3270. void (*func)(struct perf_event *))
  3271. {
  3272. struct perf_event *child;
  3273. WARN_ON_ONCE(event->ctx->parent_ctx);
  3274. mutex_lock(&event->child_mutex);
  3275. func(event);
  3276. list_for_each_entry(child, &event->child_list, child_list)
  3277. func(child);
  3278. mutex_unlock(&event->child_mutex);
  3279. }
  3280. static void perf_event_for_each(struct perf_event *event,
  3281. void (*func)(struct perf_event *))
  3282. {
  3283. struct perf_event_context *ctx = event->ctx;
  3284. struct perf_event *sibling;
  3285. lockdep_assert_held(&ctx->mutex);
  3286. event = event->group_leader;
  3287. perf_event_for_each_child(event, func);
  3288. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  3289. perf_event_for_each_child(sibling, func);
  3290. }
  3291. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  3292. {
  3293. struct perf_event_context *ctx = event->ctx;
  3294. int ret = 0, active;
  3295. u64 value;
  3296. if (!is_sampling_event(event))
  3297. return -EINVAL;
  3298. if (copy_from_user(&value, arg, sizeof(value)))
  3299. return -EFAULT;
  3300. if (!value)
  3301. return -EINVAL;
  3302. raw_spin_lock_irq(&ctx->lock);
  3303. if (event->attr.freq) {
  3304. if (value > sysctl_perf_event_sample_rate) {
  3305. ret = -EINVAL;
  3306. goto unlock;
  3307. }
  3308. event->attr.sample_freq = value;
  3309. } else {
  3310. event->attr.sample_period = value;
  3311. event->hw.sample_period = value;
  3312. }
  3313. active = (event->state == PERF_EVENT_STATE_ACTIVE);
  3314. if (active) {
  3315. perf_pmu_disable(ctx->pmu);
  3316. event->pmu->stop(event, PERF_EF_UPDATE);
  3317. }
  3318. local64_set(&event->hw.period_left, 0);
  3319. if (active) {
  3320. event->pmu->start(event, PERF_EF_RELOAD);
  3321. perf_pmu_enable(ctx->pmu);
  3322. }
  3323. unlock:
  3324. raw_spin_unlock_irq(&ctx->lock);
  3325. return ret;
  3326. }
  3327. static const struct file_operations perf_fops;
  3328. static inline int perf_fget_light(int fd, struct fd *p)
  3329. {
  3330. struct fd f = fdget(fd);
  3331. if (!f.file)
  3332. return -EBADF;
  3333. if (f.file->f_op != &perf_fops) {
  3334. fdput(f);
  3335. return -EBADF;
  3336. }
  3337. *p = f;
  3338. return 0;
  3339. }
  3340. static int perf_event_set_output(struct perf_event *event,
  3341. struct perf_event *output_event);
  3342. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  3343. static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
  3344. static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
  3345. {
  3346. void (*func)(struct perf_event *);
  3347. u32 flags = arg;
  3348. switch (cmd) {
  3349. case PERF_EVENT_IOC_ENABLE:
  3350. func = _perf_event_enable;
  3351. break;
  3352. case PERF_EVENT_IOC_DISABLE:
  3353. func = _perf_event_disable;
  3354. break;
  3355. case PERF_EVENT_IOC_RESET:
  3356. func = _perf_event_reset;
  3357. break;
  3358. case PERF_EVENT_IOC_REFRESH:
  3359. return _perf_event_refresh(event, arg);
  3360. case PERF_EVENT_IOC_PERIOD:
  3361. return perf_event_period(event, (u64 __user *)arg);
  3362. case PERF_EVENT_IOC_ID:
  3363. {
  3364. u64 id = primary_event_id(event);
  3365. if (copy_to_user((void __user *)arg, &id, sizeof(id)))
  3366. return -EFAULT;
  3367. return 0;
  3368. }
  3369. case PERF_EVENT_IOC_SET_OUTPUT:
  3370. {
  3371. int ret;
  3372. if (arg != -1) {
  3373. struct perf_event *output_event;
  3374. struct fd output;
  3375. ret = perf_fget_light(arg, &output);
  3376. if (ret)
  3377. return ret;
  3378. output_event = output.file->private_data;
  3379. ret = perf_event_set_output(event, output_event);
  3380. fdput(output);
  3381. } else {
  3382. ret = perf_event_set_output(event, NULL);
  3383. }
  3384. return ret;
  3385. }
  3386. case PERF_EVENT_IOC_SET_FILTER:
  3387. return perf_event_set_filter(event, (void __user *)arg);
  3388. case PERF_EVENT_IOC_SET_BPF:
  3389. return perf_event_set_bpf_prog(event, arg);
  3390. default:
  3391. return -ENOTTY;
  3392. }
  3393. if (flags & PERF_IOC_FLAG_GROUP)
  3394. perf_event_for_each(event, func);
  3395. else
  3396. perf_event_for_each_child(event, func);
  3397. return 0;
  3398. }
  3399. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  3400. {
  3401. struct perf_event *event = file->private_data;
  3402. struct perf_event_context *ctx;
  3403. long ret;
  3404. ctx = perf_event_ctx_lock(event);
  3405. ret = _perf_ioctl(event, cmd, arg);
  3406. perf_event_ctx_unlock(event, ctx);
  3407. return ret;
  3408. }
  3409. #ifdef CONFIG_COMPAT
  3410. static long perf_compat_ioctl(struct file *file, unsigned int cmd,
  3411. unsigned long arg)
  3412. {
  3413. switch (_IOC_NR(cmd)) {
  3414. case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
  3415. case _IOC_NR(PERF_EVENT_IOC_ID):
  3416. /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
  3417. if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
  3418. cmd &= ~IOCSIZE_MASK;
  3419. cmd |= sizeof(void *) << IOCSIZE_SHIFT;
  3420. }
  3421. break;
  3422. }
  3423. return perf_ioctl(file, cmd, arg);
  3424. }
  3425. #else
  3426. # define perf_compat_ioctl NULL
  3427. #endif
  3428. int perf_event_task_enable(void)
  3429. {
  3430. struct perf_event_context *ctx;
  3431. struct perf_event *event;
  3432. mutex_lock(&current->perf_event_mutex);
  3433. list_for_each_entry(event, &current->perf_event_list, owner_entry) {
  3434. ctx = perf_event_ctx_lock(event);
  3435. perf_event_for_each_child(event, _perf_event_enable);
  3436. perf_event_ctx_unlock(event, ctx);
  3437. }
  3438. mutex_unlock(&current->perf_event_mutex);
  3439. return 0;
  3440. }
  3441. int perf_event_task_disable(void)
  3442. {
  3443. struct perf_event_context *ctx;
  3444. struct perf_event *event;
  3445. mutex_lock(&current->perf_event_mutex);
  3446. list_for_each_entry(event, &current->perf_event_list, owner_entry) {
  3447. ctx = perf_event_ctx_lock(event);
  3448. perf_event_for_each_child(event, _perf_event_disable);
  3449. perf_event_ctx_unlock(event, ctx);
  3450. }
  3451. mutex_unlock(&current->perf_event_mutex);
  3452. return 0;
  3453. }
  3454. static int perf_event_index(struct perf_event *event)
  3455. {
  3456. if (event->hw.state & PERF_HES_STOPPED)
  3457. return 0;
  3458. if (event->state != PERF_EVENT_STATE_ACTIVE)
  3459. return 0;
  3460. return event->pmu->event_idx(event);
  3461. }
  3462. static void calc_timer_values(struct perf_event *event,
  3463. u64 *now,
  3464. u64 *enabled,
  3465. u64 *running)
  3466. {
  3467. u64 ctx_time;
  3468. *now = perf_clock();
  3469. ctx_time = event->shadow_ctx_time + *now;
  3470. *enabled = ctx_time - event->tstamp_enabled;
  3471. *running = ctx_time - event->tstamp_running;
  3472. }
  3473. static void perf_event_init_userpage(struct perf_event *event)
  3474. {
  3475. struct perf_event_mmap_page *userpg;
  3476. struct ring_buffer *rb;
  3477. rcu_read_lock();
  3478. rb = rcu_dereference(event->rb);
  3479. if (!rb)
  3480. goto unlock;
  3481. userpg = rb->user_page;
  3482. /* Allow new userspace to detect that bit 0 is deprecated */
  3483. userpg->cap_bit0_is_deprecated = 1;
  3484. userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
  3485. userpg->data_offset = PAGE_SIZE;
  3486. userpg->data_size = perf_data_size(rb);
  3487. unlock:
  3488. rcu_read_unlock();
  3489. }
  3490. void __weak arch_perf_update_userpage(
  3491. struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
  3492. {
  3493. }
  3494. /*
  3495. * Callers need to ensure there can be no nesting of this function, otherwise
  3496. * the seqlock logic goes bad. We can not serialize this because the arch
  3497. * code calls this from NMI context.
  3498. */
  3499. void perf_event_update_userpage(struct perf_event *event)
  3500. {
  3501. struct perf_event_mmap_page *userpg;
  3502. struct ring_buffer *rb;
  3503. u64 enabled, running, now;
  3504. rcu_read_lock();
  3505. rb = rcu_dereference(event->rb);
  3506. if (!rb)
  3507. goto unlock;
  3508. /*
  3509. * compute total_time_enabled, total_time_running
  3510. * based on snapshot values taken when the event
  3511. * was last scheduled in.
  3512. *
  3513. * we cannot simply called update_context_time()
  3514. * because of locking issue as we can be called in
  3515. * NMI context
  3516. */
  3517. calc_timer_values(event, &now, &enabled, &running);
  3518. userpg = rb->user_page;
  3519. /*
  3520. * Disable preemption so as to not let the corresponding user-space
  3521. * spin too long if we get preempted.
  3522. */
  3523. preempt_disable();
  3524. ++userpg->lock;
  3525. barrier();
  3526. userpg->index = perf_event_index(event);
  3527. userpg->offset = perf_event_count(event);
  3528. if (userpg->index)
  3529. userpg->offset -= local64_read(&event->hw.prev_count);
  3530. userpg->time_enabled = enabled +
  3531. atomic64_read(&event->child_total_time_enabled);
  3532. userpg->time_running = running +
  3533. atomic64_read(&event->child_total_time_running);
  3534. arch_perf_update_userpage(event, userpg, now);
  3535. barrier();
  3536. ++userpg->lock;
  3537. preempt_enable();
  3538. unlock:
  3539. rcu_read_unlock();
  3540. }
  3541. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  3542. {
  3543. struct perf_event *event = vma->vm_file->private_data;
  3544. struct ring_buffer *rb;
  3545. int ret = VM_FAULT_SIGBUS;
  3546. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  3547. if (vmf->pgoff == 0)
  3548. ret = 0;
  3549. return ret;
  3550. }
  3551. rcu_read_lock();
  3552. rb = rcu_dereference(event->rb);
  3553. if (!rb)
  3554. goto unlock;
  3555. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  3556. goto unlock;
  3557. vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
  3558. if (!vmf->page)
  3559. goto unlock;
  3560. get_page(vmf->page);
  3561. vmf->page->mapping = vma->vm_file->f_mapping;
  3562. vmf->page->index = vmf->pgoff;
  3563. ret = 0;
  3564. unlock:
  3565. rcu_read_unlock();
  3566. return ret;
  3567. }
  3568. static void ring_buffer_attach(struct perf_event *event,
  3569. struct ring_buffer *rb)
  3570. {
  3571. struct ring_buffer *old_rb = NULL;
  3572. unsigned long flags;
  3573. if (event->rb) {
  3574. /*
  3575. * Should be impossible, we set this when removing
  3576. * event->rb_entry and wait/clear when adding event->rb_entry.
  3577. */
  3578. WARN_ON_ONCE(event->rcu_pending);
  3579. old_rb = event->rb;
  3580. event->rcu_batches = get_state_synchronize_rcu();
  3581. event->rcu_pending = 1;
  3582. spin_lock_irqsave(&old_rb->event_lock, flags);
  3583. list_del_rcu(&event->rb_entry);
  3584. spin_unlock_irqrestore(&old_rb->event_lock, flags);
  3585. }
  3586. if (event->rcu_pending && rb) {
  3587. cond_synchronize_rcu(event->rcu_batches);
  3588. event->rcu_pending = 0;
  3589. }
  3590. if (rb) {
  3591. spin_lock_irqsave(&rb->event_lock, flags);
  3592. list_add_rcu(&event->rb_entry, &rb->event_list);
  3593. spin_unlock_irqrestore(&rb->event_lock, flags);
  3594. }
  3595. rcu_assign_pointer(event->rb, rb);
  3596. if (old_rb) {
  3597. ring_buffer_put(old_rb);
  3598. /*
  3599. * Since we detached before setting the new rb, so that we
  3600. * could attach the new rb, we could have missed a wakeup.
  3601. * Provide it now.
  3602. */
  3603. wake_up_all(&event->waitq);
  3604. }
  3605. }
  3606. static void ring_buffer_wakeup(struct perf_event *event)
  3607. {
  3608. struct ring_buffer *rb;
  3609. rcu_read_lock();
  3610. rb = rcu_dereference(event->rb);
  3611. if (rb) {
  3612. list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
  3613. wake_up_all(&event->waitq);
  3614. }
  3615. rcu_read_unlock();
  3616. }
  3617. static void rb_free_rcu(struct rcu_head *rcu_head)
  3618. {
  3619. struct ring_buffer *rb;
  3620. rb = container_of(rcu_head, struct ring_buffer, rcu_head);
  3621. rb_free(rb);
  3622. }
  3623. struct ring_buffer *ring_buffer_get(struct perf_event *event)
  3624. {
  3625. struct ring_buffer *rb;
  3626. rcu_read_lock();
  3627. rb = rcu_dereference(event->rb);
  3628. if (rb) {
  3629. if (!atomic_inc_not_zero(&rb->refcount))
  3630. rb = NULL;
  3631. }
  3632. rcu_read_unlock();
  3633. return rb;
  3634. }
  3635. void ring_buffer_put(struct ring_buffer *rb)
  3636. {
  3637. if (!atomic_dec_and_test(&rb->refcount))
  3638. return;
  3639. WARN_ON_ONCE(!list_empty(&rb->event_list));
  3640. call_rcu(&rb->rcu_head, rb_free_rcu);
  3641. }
  3642. static void perf_mmap_open(struct vm_area_struct *vma)
  3643. {
  3644. struct perf_event *event = vma->vm_file->private_data;
  3645. atomic_inc(&event->mmap_count);
  3646. atomic_inc(&event->rb->mmap_count);
  3647. if (vma->vm_pgoff)
  3648. atomic_inc(&event->rb->aux_mmap_count);
  3649. if (event->pmu->event_mapped)
  3650. event->pmu->event_mapped(event);
  3651. }
  3652. /*
  3653. * A buffer can be mmap()ed multiple times; either directly through the same
  3654. * event, or through other events by use of perf_event_set_output().
  3655. *
  3656. * In order to undo the VM accounting done by perf_mmap() we need to destroy
  3657. * the buffer here, where we still have a VM context. This means we need
  3658. * to detach all events redirecting to us.
  3659. */
  3660. static void perf_mmap_close(struct vm_area_struct *vma)
  3661. {
  3662. struct perf_event *event = vma->vm_file->private_data;
  3663. struct ring_buffer *rb = ring_buffer_get(event);
  3664. struct user_struct *mmap_user = rb->mmap_user;
  3665. int mmap_locked = rb->mmap_locked;
  3666. unsigned long size = perf_data_size(rb);
  3667. if (event->pmu->event_unmapped)
  3668. event->pmu->event_unmapped(event);
  3669. /*
  3670. * rb->aux_mmap_count will always drop before rb->mmap_count and
  3671. * event->mmap_count, so it is ok to use event->mmap_mutex to
  3672. * serialize with perf_mmap here.
  3673. */
  3674. if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
  3675. atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
  3676. atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
  3677. vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
  3678. rb_free_aux(rb);
  3679. mutex_unlock(&event->mmap_mutex);
  3680. }
  3681. atomic_dec(&rb->mmap_count);
  3682. if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
  3683. goto out_put;
  3684. ring_buffer_attach(event, NULL);
  3685. mutex_unlock(&event->mmap_mutex);
  3686. /* If there's still other mmap()s of this buffer, we're done. */
  3687. if (atomic_read(&rb->mmap_count))
  3688. goto out_put;
  3689. /*
  3690. * No other mmap()s, detach from all other events that might redirect
  3691. * into the now unreachable buffer. Somewhat complicated by the
  3692. * fact that rb::event_lock otherwise nests inside mmap_mutex.
  3693. */
  3694. again:
  3695. rcu_read_lock();
  3696. list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
  3697. if (!atomic_long_inc_not_zero(&event->refcount)) {
  3698. /*
  3699. * This event is en-route to free_event() which will
  3700. * detach it and remove it from the list.
  3701. */
  3702. continue;
  3703. }
  3704. rcu_read_unlock();
  3705. mutex_lock(&event->mmap_mutex);
  3706. /*
  3707. * Check we didn't race with perf_event_set_output() which can
  3708. * swizzle the rb from under us while we were waiting to
  3709. * acquire mmap_mutex.
  3710. *
  3711. * If we find a different rb; ignore this event, a next
  3712. * iteration will no longer find it on the list. We have to
  3713. * still restart the iteration to make sure we're not now
  3714. * iterating the wrong list.
  3715. */
  3716. if (event->rb == rb)
  3717. ring_buffer_attach(event, NULL);
  3718. mutex_unlock(&event->mmap_mutex);
  3719. put_event(event);
  3720. /*
  3721. * Restart the iteration; either we're on the wrong list or
  3722. * destroyed its integrity by doing a deletion.
  3723. */
  3724. goto again;
  3725. }
  3726. rcu_read_unlock();
  3727. /*
  3728. * It could be there's still a few 0-ref events on the list; they'll
  3729. * get cleaned up by free_event() -- they'll also still have their
  3730. * ref on the rb and will free it whenever they are done with it.
  3731. *
  3732. * Aside from that, this buffer is 'fully' detached and unmapped,
  3733. * undo the VM accounting.
  3734. */
  3735. atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
  3736. vma->vm_mm->pinned_vm -= mmap_locked;
  3737. free_uid(mmap_user);
  3738. out_put:
  3739. ring_buffer_put(rb); /* could be last */
  3740. }
  3741. static const struct vm_operations_struct perf_mmap_vmops = {
  3742. .open = perf_mmap_open,
  3743. .close = perf_mmap_close, /* non mergable */
  3744. .fault = perf_mmap_fault,
  3745. .page_mkwrite = perf_mmap_fault,
  3746. };
  3747. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  3748. {
  3749. struct perf_event *event = file->private_data;
  3750. unsigned long user_locked, user_lock_limit;
  3751. struct user_struct *user = current_user();
  3752. unsigned long locked, lock_limit;
  3753. struct ring_buffer *rb = NULL;
  3754. unsigned long vma_size;
  3755. unsigned long nr_pages;
  3756. long user_extra = 0, extra = 0;
  3757. int ret = 0, flags = 0;
  3758. /*
  3759. * Don't allow mmap() of inherited per-task counters. This would
  3760. * create a performance issue due to all children writing to the
  3761. * same rb.
  3762. */
  3763. if (event->cpu == -1 && event->attr.inherit)
  3764. return -EINVAL;
  3765. if (!(vma->vm_flags & VM_SHARED))
  3766. return -EINVAL;
  3767. vma_size = vma->vm_end - vma->vm_start;
  3768. if (vma->vm_pgoff == 0) {
  3769. nr_pages = (vma_size / PAGE_SIZE) - 1;
  3770. } else {
  3771. /*
  3772. * AUX area mapping: if rb->aux_nr_pages != 0, it's already
  3773. * mapped, all subsequent mappings should have the same size
  3774. * and offset. Must be above the normal perf buffer.
  3775. */
  3776. u64 aux_offset, aux_size;
  3777. if (!event->rb)
  3778. return -EINVAL;
  3779. nr_pages = vma_size / PAGE_SIZE;
  3780. mutex_lock(&event->mmap_mutex);
  3781. ret = -EINVAL;
  3782. rb = event->rb;
  3783. if (!rb)
  3784. goto aux_unlock;
  3785. aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
  3786. aux_size = ACCESS_ONCE(rb->user_page->aux_size);
  3787. if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
  3788. goto aux_unlock;
  3789. if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
  3790. goto aux_unlock;
  3791. /* already mapped with a different offset */
  3792. if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
  3793. goto aux_unlock;
  3794. if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
  3795. goto aux_unlock;
  3796. /* already mapped with a different size */
  3797. if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
  3798. goto aux_unlock;
  3799. if (!is_power_of_2(nr_pages))
  3800. goto aux_unlock;
  3801. if (!atomic_inc_not_zero(&rb->mmap_count))
  3802. goto aux_unlock;
  3803. if (rb_has_aux(rb)) {
  3804. atomic_inc(&rb->aux_mmap_count);
  3805. ret = 0;
  3806. goto unlock;
  3807. }
  3808. atomic_set(&rb->aux_mmap_count, 1);
  3809. user_extra = nr_pages;
  3810. goto accounting;
  3811. }
  3812. /*
  3813. * If we have rb pages ensure they're a power-of-two number, so we
  3814. * can do bitmasks instead of modulo.
  3815. */
  3816. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  3817. return -EINVAL;
  3818. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  3819. return -EINVAL;
  3820. WARN_ON_ONCE(event->ctx->parent_ctx);
  3821. again:
  3822. mutex_lock(&event->mmap_mutex);
  3823. if (event->rb) {
  3824. if (event->rb->nr_pages != nr_pages) {
  3825. ret = -EINVAL;
  3826. goto unlock;
  3827. }
  3828. if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
  3829. /*
  3830. * Raced against perf_mmap_close() through
  3831. * perf_event_set_output(). Try again, hope for better
  3832. * luck.
  3833. */
  3834. mutex_unlock(&event->mmap_mutex);
  3835. goto again;
  3836. }
  3837. goto unlock;
  3838. }
  3839. user_extra = nr_pages + 1;
  3840. accounting:
  3841. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  3842. /*
  3843. * Increase the limit linearly with more CPUs:
  3844. */
  3845. user_lock_limit *= num_online_cpus();
  3846. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  3847. if (user_locked > user_lock_limit)
  3848. extra = user_locked - user_lock_limit;
  3849. lock_limit = rlimit(RLIMIT_MEMLOCK);
  3850. lock_limit >>= PAGE_SHIFT;
  3851. locked = vma->vm_mm->pinned_vm + extra;
  3852. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  3853. !capable(CAP_IPC_LOCK)) {
  3854. ret = -EPERM;
  3855. goto unlock;
  3856. }
  3857. WARN_ON(!rb && event->rb);
  3858. if (vma->vm_flags & VM_WRITE)
  3859. flags |= RING_BUFFER_WRITABLE;
  3860. if (!rb) {
  3861. rb = rb_alloc(nr_pages,
  3862. event->attr.watermark ? event->attr.wakeup_watermark : 0,
  3863. event->cpu, flags);
  3864. if (!rb) {
  3865. ret = -ENOMEM;
  3866. goto unlock;
  3867. }
  3868. atomic_set(&rb->mmap_count, 1);
  3869. rb->mmap_user = get_current_user();
  3870. rb->mmap_locked = extra;
  3871. ring_buffer_attach(event, rb);
  3872. perf_event_init_userpage(event);
  3873. perf_event_update_userpage(event);
  3874. } else {
  3875. ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
  3876. event->attr.aux_watermark, flags);
  3877. if (!ret)
  3878. rb->aux_mmap_locked = extra;
  3879. }
  3880. unlock:
  3881. if (!ret) {
  3882. atomic_long_add(user_extra, &user->locked_vm);
  3883. vma->vm_mm->pinned_vm += extra;
  3884. atomic_inc(&event->mmap_count);
  3885. } else if (rb) {
  3886. atomic_dec(&rb->mmap_count);
  3887. }
  3888. aux_unlock:
  3889. mutex_unlock(&event->mmap_mutex);
  3890. /*
  3891. * Since pinned accounting is per vm we cannot allow fork() to copy our
  3892. * vma.
  3893. */
  3894. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
  3895. vma->vm_ops = &perf_mmap_vmops;
  3896. if (event->pmu->event_mapped)
  3897. event->pmu->event_mapped(event);
  3898. return ret;
  3899. }
  3900. static int perf_fasync(int fd, struct file *filp, int on)
  3901. {
  3902. struct inode *inode = file_inode(filp);
  3903. struct perf_event *event = filp->private_data;
  3904. int retval;
  3905. mutex_lock(&inode->i_mutex);
  3906. retval = fasync_helper(fd, filp, on, &event->fasync);
  3907. mutex_unlock(&inode->i_mutex);
  3908. if (retval < 0)
  3909. return retval;
  3910. return 0;
  3911. }
  3912. static const struct file_operations perf_fops = {
  3913. .llseek = no_llseek,
  3914. .release = perf_release,
  3915. .read = perf_read,
  3916. .poll = perf_poll,
  3917. .unlocked_ioctl = perf_ioctl,
  3918. .compat_ioctl = perf_compat_ioctl,
  3919. .mmap = perf_mmap,
  3920. .fasync = perf_fasync,
  3921. };
  3922. /*
  3923. * Perf event wakeup
  3924. *
  3925. * If there's data, ensure we set the poll() state and publish everything
  3926. * to user-space before waking everybody up.
  3927. */
  3928. void perf_event_wakeup(struct perf_event *event)
  3929. {
  3930. ring_buffer_wakeup(event);
  3931. if (event->pending_kill) {
  3932. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  3933. event->pending_kill = 0;
  3934. }
  3935. }
  3936. static void perf_pending_event(struct irq_work *entry)
  3937. {
  3938. struct perf_event *event = container_of(entry,
  3939. struct perf_event, pending);
  3940. int rctx;
  3941. rctx = perf_swevent_get_recursion_context();
  3942. /*
  3943. * If we 'fail' here, that's OK, it means recursion is already disabled
  3944. * and we won't recurse 'further'.
  3945. */
  3946. if (event->pending_disable) {
  3947. event->pending_disable = 0;
  3948. __perf_event_disable(event);
  3949. }
  3950. if (event->pending_wakeup) {
  3951. event->pending_wakeup = 0;
  3952. perf_event_wakeup(event);
  3953. }
  3954. if (rctx >= 0)
  3955. perf_swevent_put_recursion_context(rctx);
  3956. }
  3957. /*
  3958. * We assume there is only KVM supporting the callbacks.
  3959. * Later on, we might change it to a list if there is
  3960. * another virtualization implementation supporting the callbacks.
  3961. */
  3962. struct perf_guest_info_callbacks *perf_guest_cbs;
  3963. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  3964. {
  3965. perf_guest_cbs = cbs;
  3966. return 0;
  3967. }
  3968. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  3969. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  3970. {
  3971. perf_guest_cbs = NULL;
  3972. return 0;
  3973. }
  3974. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  3975. static void
  3976. perf_output_sample_regs(struct perf_output_handle *handle,
  3977. struct pt_regs *regs, u64 mask)
  3978. {
  3979. int bit;
  3980. for_each_set_bit(bit, (const unsigned long *) &mask,
  3981. sizeof(mask) * BITS_PER_BYTE) {
  3982. u64 val;
  3983. val = perf_reg_value(regs, bit);
  3984. perf_output_put(handle, val);
  3985. }
  3986. }
  3987. static void perf_sample_regs_user(struct perf_regs *regs_user,
  3988. struct pt_regs *regs,
  3989. struct pt_regs *regs_user_copy)
  3990. {
  3991. if (user_mode(regs)) {
  3992. regs_user->abi = perf_reg_abi(current);
  3993. regs_user->regs = regs;
  3994. } else if (current->mm) {
  3995. perf_get_regs_user(regs_user, regs, regs_user_copy);
  3996. } else {
  3997. regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
  3998. regs_user->regs = NULL;
  3999. }
  4000. }
  4001. static void perf_sample_regs_intr(struct perf_regs *regs_intr,
  4002. struct pt_regs *regs)
  4003. {
  4004. regs_intr->regs = regs;
  4005. regs_intr->abi = perf_reg_abi(current);
  4006. }
  4007. /*
  4008. * Get remaining task size from user stack pointer.
  4009. *
  4010. * It'd be better to take stack vma map and limit this more
  4011. * precisly, but there's no way to get it safely under interrupt,
  4012. * so using TASK_SIZE as limit.
  4013. */
  4014. static u64 perf_ustack_task_size(struct pt_regs *regs)
  4015. {
  4016. unsigned long addr = perf_user_stack_pointer(regs);
  4017. if (!addr || addr >= TASK_SIZE)
  4018. return 0;
  4019. return TASK_SIZE - addr;
  4020. }
  4021. static u16
  4022. perf_sample_ustack_size(u16 stack_size, u16 header_size,
  4023. struct pt_regs *regs)
  4024. {
  4025. u64 task_size;
  4026. /* No regs, no stack pointer, no dump. */
  4027. if (!regs)
  4028. return 0;
  4029. /*
  4030. * Check if we fit in with the requested stack size into the:
  4031. * - TASK_SIZE
  4032. * If we don't, we limit the size to the TASK_SIZE.
  4033. *
  4034. * - remaining sample size
  4035. * If we don't, we customize the stack size to
  4036. * fit in to the remaining sample size.
  4037. */
  4038. task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
  4039. stack_size = min(stack_size, (u16) task_size);
  4040. /* Current header size plus static size and dynamic size. */
  4041. header_size += 2 * sizeof(u64);
  4042. /* Do we fit in with the current stack dump size? */
  4043. if ((u16) (header_size + stack_size) < header_size) {
  4044. /*
  4045. * If we overflow the maximum size for the sample,
  4046. * we customize the stack dump size to fit in.
  4047. */
  4048. stack_size = USHRT_MAX - header_size - sizeof(u64);
  4049. stack_size = round_up(stack_size, sizeof(u64));
  4050. }
  4051. return stack_size;
  4052. }
  4053. static void
  4054. perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
  4055. struct pt_regs *regs)
  4056. {
  4057. /* Case of a kernel thread, nothing to dump */
  4058. if (!regs) {
  4059. u64 size = 0;
  4060. perf_output_put(handle, size);
  4061. } else {
  4062. unsigned long sp;
  4063. unsigned int rem;
  4064. u64 dyn_size;
  4065. /*
  4066. * We dump:
  4067. * static size
  4068. * - the size requested by user or the best one we can fit
  4069. * in to the sample max size
  4070. * data
  4071. * - user stack dump data
  4072. * dynamic size
  4073. * - the actual dumped size
  4074. */
  4075. /* Static size. */
  4076. perf_output_put(handle, dump_size);
  4077. /* Data. */
  4078. sp = perf_user_stack_pointer(regs);
  4079. rem = __output_copy_user(handle, (void *) sp, dump_size);
  4080. dyn_size = dump_size - rem;
  4081. perf_output_skip(handle, rem);
  4082. /* Dynamic size. */
  4083. perf_output_put(handle, dyn_size);
  4084. }
  4085. }
  4086. static void __perf_event_header__init_id(struct perf_event_header *header,
  4087. struct perf_sample_data *data,
  4088. struct perf_event *event)
  4089. {
  4090. u64 sample_type = event->attr.sample_type;
  4091. data->type = sample_type;
  4092. header->size += event->id_header_size;
  4093. if (sample_type & PERF_SAMPLE_TID) {
  4094. /* namespace issues */
  4095. data->tid_entry.pid = perf_event_pid(event, current);
  4096. data->tid_entry.tid = perf_event_tid(event, current);
  4097. }
  4098. if (sample_type & PERF_SAMPLE_TIME)
  4099. data->time = perf_event_clock(event);
  4100. if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
  4101. data->id = primary_event_id(event);
  4102. if (sample_type & PERF_SAMPLE_STREAM_ID)
  4103. data->stream_id = event->id;
  4104. if (sample_type & PERF_SAMPLE_CPU) {
  4105. data->cpu_entry.cpu = raw_smp_processor_id();
  4106. data->cpu_entry.reserved = 0;
  4107. }
  4108. }
  4109. void perf_event_header__init_id(struct perf_event_header *header,
  4110. struct perf_sample_data *data,
  4111. struct perf_event *event)
  4112. {
  4113. if (event->attr.sample_id_all)
  4114. __perf_event_header__init_id(header, data, event);
  4115. }
  4116. static void __perf_event__output_id_sample(struct perf_output_handle *handle,
  4117. struct perf_sample_data *data)
  4118. {
  4119. u64 sample_type = data->type;
  4120. if (sample_type & PERF_SAMPLE_TID)
  4121. perf_output_put(handle, data->tid_entry);
  4122. if (sample_type & PERF_SAMPLE_TIME)
  4123. perf_output_put(handle, data->time);
  4124. if (sample_type & PERF_SAMPLE_ID)
  4125. perf_output_put(handle, data->id);
  4126. if (sample_type & PERF_SAMPLE_STREAM_ID)
  4127. perf_output_put(handle, data->stream_id);
  4128. if (sample_type & PERF_SAMPLE_CPU)
  4129. perf_output_put(handle, data->cpu_entry);
  4130. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  4131. perf_output_put(handle, data->id);
  4132. }
  4133. void perf_event__output_id_sample(struct perf_event *event,
  4134. struct perf_output_handle *handle,
  4135. struct perf_sample_data *sample)
  4136. {
  4137. if (event->attr.sample_id_all)
  4138. __perf_event__output_id_sample(handle, sample);
  4139. }
  4140. static void perf_output_read_one(struct perf_output_handle *handle,
  4141. struct perf_event *event,
  4142. u64 enabled, u64 running)
  4143. {
  4144. u64 read_format = event->attr.read_format;
  4145. u64 values[4];
  4146. int n = 0;
  4147. values[n++] = perf_event_count(event);
  4148. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  4149. values[n++] = enabled +
  4150. atomic64_read(&event->child_total_time_enabled);
  4151. }
  4152. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  4153. values[n++] = running +
  4154. atomic64_read(&event->child_total_time_running);
  4155. }
  4156. if (read_format & PERF_FORMAT_ID)
  4157. values[n++] = primary_event_id(event);
  4158. __output_copy(handle, values, n * sizeof(u64));
  4159. }
  4160. /*
  4161. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  4162. */
  4163. static void perf_output_read_group(struct perf_output_handle *handle,
  4164. struct perf_event *event,
  4165. u64 enabled, u64 running)
  4166. {
  4167. struct perf_event *leader = event->group_leader, *sub;
  4168. u64 read_format = event->attr.read_format;
  4169. u64 values[5];
  4170. int n = 0;
  4171. values[n++] = 1 + leader->nr_siblings;
  4172. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  4173. values[n++] = enabled;
  4174. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  4175. values[n++] = running;
  4176. if (leader != event)
  4177. leader->pmu->read(leader);
  4178. values[n++] = perf_event_count(leader);
  4179. if (read_format & PERF_FORMAT_ID)
  4180. values[n++] = primary_event_id(leader);
  4181. __output_copy(handle, values, n * sizeof(u64));
  4182. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  4183. n = 0;
  4184. if ((sub != event) &&
  4185. (sub->state == PERF_EVENT_STATE_ACTIVE))
  4186. sub->pmu->read(sub);
  4187. values[n++] = perf_event_count(sub);
  4188. if (read_format & PERF_FORMAT_ID)
  4189. values[n++] = primary_event_id(sub);
  4190. __output_copy(handle, values, n * sizeof(u64));
  4191. }
  4192. }
  4193. #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
  4194. PERF_FORMAT_TOTAL_TIME_RUNNING)
  4195. static void perf_output_read(struct perf_output_handle *handle,
  4196. struct perf_event *event)
  4197. {
  4198. u64 enabled = 0, running = 0, now;
  4199. u64 read_format = event->attr.read_format;
  4200. /*
  4201. * compute total_time_enabled, total_time_running
  4202. * based on snapshot values taken when the event
  4203. * was last scheduled in.
  4204. *
  4205. * we cannot simply called update_context_time()
  4206. * because of locking issue as we are called in
  4207. * NMI context
  4208. */
  4209. if (read_format & PERF_FORMAT_TOTAL_TIMES)
  4210. calc_timer_values(event, &now, &enabled, &running);
  4211. if (event->attr.read_format & PERF_FORMAT_GROUP)
  4212. perf_output_read_group(handle, event, enabled, running);
  4213. else
  4214. perf_output_read_one(handle, event, enabled, running);
  4215. }
  4216. void perf_output_sample(struct perf_output_handle *handle,
  4217. struct perf_event_header *header,
  4218. struct perf_sample_data *data,
  4219. struct perf_event *event)
  4220. {
  4221. u64 sample_type = data->type;
  4222. perf_output_put(handle, *header);
  4223. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  4224. perf_output_put(handle, data->id);
  4225. if (sample_type & PERF_SAMPLE_IP)
  4226. perf_output_put(handle, data->ip);
  4227. if (sample_type & PERF_SAMPLE_TID)
  4228. perf_output_put(handle, data->tid_entry);
  4229. if (sample_type & PERF_SAMPLE_TIME)
  4230. perf_output_put(handle, data->time);
  4231. if (sample_type & PERF_SAMPLE_ADDR)
  4232. perf_output_put(handle, data->addr);
  4233. if (sample_type & PERF_SAMPLE_ID)
  4234. perf_output_put(handle, data->id);
  4235. if (sample_type & PERF_SAMPLE_STREAM_ID)
  4236. perf_output_put(handle, data->stream_id);
  4237. if (sample_type & PERF_SAMPLE_CPU)
  4238. perf_output_put(handle, data->cpu_entry);
  4239. if (sample_type & PERF_SAMPLE_PERIOD)
  4240. perf_output_put(handle, data->period);
  4241. if (sample_type & PERF_SAMPLE_READ)
  4242. perf_output_read(handle, event);
  4243. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  4244. if (data->callchain) {
  4245. int size = 1;
  4246. if (data->callchain)
  4247. size += data->callchain->nr;
  4248. size *= sizeof(u64);
  4249. __output_copy(handle, data->callchain, size);
  4250. } else {
  4251. u64 nr = 0;
  4252. perf_output_put(handle, nr);
  4253. }
  4254. }
  4255. if (sample_type & PERF_SAMPLE_RAW) {
  4256. if (data->raw) {
  4257. perf_output_put(handle, data->raw->size);
  4258. __output_copy(handle, data->raw->data,
  4259. data->raw->size);
  4260. } else {
  4261. struct {
  4262. u32 size;
  4263. u32 data;
  4264. } raw = {
  4265. .size = sizeof(u32),
  4266. .data = 0,
  4267. };
  4268. perf_output_put(handle, raw);
  4269. }
  4270. }
  4271. if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
  4272. if (data->br_stack) {
  4273. size_t size;
  4274. size = data->br_stack->nr
  4275. * sizeof(struct perf_branch_entry);
  4276. perf_output_put(handle, data->br_stack->nr);
  4277. perf_output_copy(handle, data->br_stack->entries, size);
  4278. } else {
  4279. /*
  4280. * we always store at least the value of nr
  4281. */
  4282. u64 nr = 0;
  4283. perf_output_put(handle, nr);
  4284. }
  4285. }
  4286. if (sample_type & PERF_SAMPLE_REGS_USER) {
  4287. u64 abi = data->regs_user.abi;
  4288. /*
  4289. * If there are no regs to dump, notice it through
  4290. * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
  4291. */
  4292. perf_output_put(handle, abi);
  4293. if (abi) {
  4294. u64 mask = event->attr.sample_regs_user;
  4295. perf_output_sample_regs(handle,
  4296. data->regs_user.regs,
  4297. mask);
  4298. }
  4299. }
  4300. if (sample_type & PERF_SAMPLE_STACK_USER) {
  4301. perf_output_sample_ustack(handle,
  4302. data->stack_user_size,
  4303. data->regs_user.regs);
  4304. }
  4305. if (sample_type & PERF_SAMPLE_WEIGHT)
  4306. perf_output_put(handle, data->weight);
  4307. if (sample_type & PERF_SAMPLE_DATA_SRC)
  4308. perf_output_put(handle, data->data_src.val);
  4309. if (sample_type & PERF_SAMPLE_TRANSACTION)
  4310. perf_output_put(handle, data->txn);
  4311. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  4312. u64 abi = data->regs_intr.abi;
  4313. /*
  4314. * If there are no regs to dump, notice it through
  4315. * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
  4316. */
  4317. perf_output_put(handle, abi);
  4318. if (abi) {
  4319. u64 mask = event->attr.sample_regs_intr;
  4320. perf_output_sample_regs(handle,
  4321. data->regs_intr.regs,
  4322. mask);
  4323. }
  4324. }
  4325. if (!event->attr.watermark) {
  4326. int wakeup_events = event->attr.wakeup_events;
  4327. if (wakeup_events) {
  4328. struct ring_buffer *rb = handle->rb;
  4329. int events = local_inc_return(&rb->events);
  4330. if (events >= wakeup_events) {
  4331. local_sub(wakeup_events, &rb->events);
  4332. local_inc(&rb->wakeup);
  4333. }
  4334. }
  4335. }
  4336. }
  4337. void perf_prepare_sample(struct perf_event_header *header,
  4338. struct perf_sample_data *data,
  4339. struct perf_event *event,
  4340. struct pt_regs *regs)
  4341. {
  4342. u64 sample_type = event->attr.sample_type;
  4343. header->type = PERF_RECORD_SAMPLE;
  4344. header->size = sizeof(*header) + event->header_size;
  4345. header->misc = 0;
  4346. header->misc |= perf_misc_flags(regs);
  4347. __perf_event_header__init_id(header, data, event);
  4348. if (sample_type & PERF_SAMPLE_IP)
  4349. data->ip = perf_instruction_pointer(regs);
  4350. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  4351. int size = 1;
  4352. data->callchain = perf_callchain(event, regs);
  4353. if (data->callchain)
  4354. size += data->callchain->nr;
  4355. header->size += size * sizeof(u64);
  4356. }
  4357. if (sample_type & PERF_SAMPLE_RAW) {
  4358. int size = sizeof(u32);
  4359. if (data->raw)
  4360. size += data->raw->size;
  4361. else
  4362. size += sizeof(u32);
  4363. WARN_ON_ONCE(size & (sizeof(u64)-1));
  4364. header->size += size;
  4365. }
  4366. if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
  4367. int size = sizeof(u64); /* nr */
  4368. if (data->br_stack) {
  4369. size += data->br_stack->nr
  4370. * sizeof(struct perf_branch_entry);
  4371. }
  4372. header->size += size;
  4373. }
  4374. if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
  4375. perf_sample_regs_user(&data->regs_user, regs,
  4376. &data->regs_user_copy);
  4377. if (sample_type & PERF_SAMPLE_REGS_USER) {
  4378. /* regs dump ABI info */
  4379. int size = sizeof(u64);
  4380. if (data->regs_user.regs) {
  4381. u64 mask = event->attr.sample_regs_user;
  4382. size += hweight64(mask) * sizeof(u64);
  4383. }
  4384. header->size += size;
  4385. }
  4386. if (sample_type & PERF_SAMPLE_STACK_USER) {
  4387. /*
  4388. * Either we need PERF_SAMPLE_STACK_USER bit to be allways
  4389. * processed as the last one or have additional check added
  4390. * in case new sample type is added, because we could eat
  4391. * up the rest of the sample size.
  4392. */
  4393. u16 stack_size = event->attr.sample_stack_user;
  4394. u16 size = sizeof(u64);
  4395. stack_size = perf_sample_ustack_size(stack_size, header->size,
  4396. data->regs_user.regs);
  4397. /*
  4398. * If there is something to dump, add space for the dump
  4399. * itself and for the field that tells the dynamic size,
  4400. * which is how many have been actually dumped.
  4401. */
  4402. if (stack_size)
  4403. size += sizeof(u64) + stack_size;
  4404. data->stack_user_size = stack_size;
  4405. header->size += size;
  4406. }
  4407. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  4408. /* regs dump ABI info */
  4409. int size = sizeof(u64);
  4410. perf_sample_regs_intr(&data->regs_intr, regs);
  4411. if (data->regs_intr.regs) {
  4412. u64 mask = event->attr.sample_regs_intr;
  4413. size += hweight64(mask) * sizeof(u64);
  4414. }
  4415. header->size += size;
  4416. }
  4417. }
  4418. static void perf_event_output(struct perf_event *event,
  4419. struct perf_sample_data *data,
  4420. struct pt_regs *regs)
  4421. {
  4422. struct perf_output_handle handle;
  4423. struct perf_event_header header;
  4424. /* protect the callchain buffers */
  4425. rcu_read_lock();
  4426. perf_prepare_sample(&header, data, event, regs);
  4427. if (perf_output_begin(&handle, event, header.size))
  4428. goto exit;
  4429. perf_output_sample(&handle, &header, data, event);
  4430. perf_output_end(&handle);
  4431. exit:
  4432. rcu_read_unlock();
  4433. }
  4434. /*
  4435. * read event_id
  4436. */
  4437. struct perf_read_event {
  4438. struct perf_event_header header;
  4439. u32 pid;
  4440. u32 tid;
  4441. };
  4442. static void
  4443. perf_event_read_event(struct perf_event *event,
  4444. struct task_struct *task)
  4445. {
  4446. struct perf_output_handle handle;
  4447. struct perf_sample_data sample;
  4448. struct perf_read_event read_event = {
  4449. .header = {
  4450. .type = PERF_RECORD_READ,
  4451. .misc = 0,
  4452. .size = sizeof(read_event) + event->read_size,
  4453. },
  4454. .pid = perf_event_pid(event, task),
  4455. .tid = perf_event_tid(event, task),
  4456. };
  4457. int ret;
  4458. perf_event_header__init_id(&read_event.header, &sample, event);
  4459. ret = perf_output_begin(&handle, event, read_event.header.size);
  4460. if (ret)
  4461. return;
  4462. perf_output_put(&handle, read_event);
  4463. perf_output_read(&handle, event);
  4464. perf_event__output_id_sample(event, &handle, &sample);
  4465. perf_output_end(&handle);
  4466. }
  4467. typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
  4468. static void
  4469. perf_event_aux_ctx(struct perf_event_context *ctx,
  4470. perf_event_aux_output_cb output,
  4471. void *data)
  4472. {
  4473. struct perf_event *event;
  4474. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  4475. if (event->state < PERF_EVENT_STATE_INACTIVE)
  4476. continue;
  4477. if (!event_filter_match(event))
  4478. continue;
  4479. output(event, data);
  4480. }
  4481. }
  4482. static void
  4483. perf_event_aux(perf_event_aux_output_cb output, void *data,
  4484. struct perf_event_context *task_ctx)
  4485. {
  4486. struct perf_cpu_context *cpuctx;
  4487. struct perf_event_context *ctx;
  4488. struct pmu *pmu;
  4489. int ctxn;
  4490. rcu_read_lock();
  4491. list_for_each_entry_rcu(pmu, &pmus, entry) {
  4492. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  4493. if (cpuctx->unique_pmu != pmu)
  4494. goto next;
  4495. perf_event_aux_ctx(&cpuctx->ctx, output, data);
  4496. if (task_ctx)
  4497. goto next;
  4498. ctxn = pmu->task_ctx_nr;
  4499. if (ctxn < 0)
  4500. goto next;
  4501. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  4502. if (ctx)
  4503. perf_event_aux_ctx(ctx, output, data);
  4504. next:
  4505. put_cpu_ptr(pmu->pmu_cpu_context);
  4506. }
  4507. if (task_ctx) {
  4508. preempt_disable();
  4509. perf_event_aux_ctx(task_ctx, output, data);
  4510. preempt_enable();
  4511. }
  4512. rcu_read_unlock();
  4513. }
  4514. /*
  4515. * task tracking -- fork/exit
  4516. *
  4517. * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
  4518. */
  4519. struct perf_task_event {
  4520. struct task_struct *task;
  4521. struct perf_event_context *task_ctx;
  4522. struct {
  4523. struct perf_event_header header;
  4524. u32 pid;
  4525. u32 ppid;
  4526. u32 tid;
  4527. u32 ptid;
  4528. u64 time;
  4529. } event_id;
  4530. };
  4531. static int perf_event_task_match(struct perf_event *event)
  4532. {
  4533. return event->attr.comm || event->attr.mmap ||
  4534. event->attr.mmap2 || event->attr.mmap_data ||
  4535. event->attr.task;
  4536. }
  4537. static void perf_event_task_output(struct perf_event *event,
  4538. void *data)
  4539. {
  4540. struct perf_task_event *task_event = data;
  4541. struct perf_output_handle handle;
  4542. struct perf_sample_data sample;
  4543. struct task_struct *task = task_event->task;
  4544. int ret, size = task_event->event_id.header.size;
  4545. if (!perf_event_task_match(event))
  4546. return;
  4547. perf_event_header__init_id(&task_event->event_id.header, &sample, event);
  4548. ret = perf_output_begin(&handle, event,
  4549. task_event->event_id.header.size);
  4550. if (ret)
  4551. goto out;
  4552. task_event->event_id.pid = perf_event_pid(event, task);
  4553. task_event->event_id.ppid = perf_event_pid(event, current);
  4554. task_event->event_id.tid = perf_event_tid(event, task);
  4555. task_event->event_id.ptid = perf_event_tid(event, current);
  4556. task_event->event_id.time = perf_event_clock(event);
  4557. perf_output_put(&handle, task_event->event_id);
  4558. perf_event__output_id_sample(event, &handle, &sample);
  4559. perf_output_end(&handle);
  4560. out:
  4561. task_event->event_id.header.size = size;
  4562. }
  4563. static void perf_event_task(struct task_struct *task,
  4564. struct perf_event_context *task_ctx,
  4565. int new)
  4566. {
  4567. struct perf_task_event task_event;
  4568. if (!atomic_read(&nr_comm_events) &&
  4569. !atomic_read(&nr_mmap_events) &&
  4570. !atomic_read(&nr_task_events))
  4571. return;
  4572. task_event = (struct perf_task_event){
  4573. .task = task,
  4574. .task_ctx = task_ctx,
  4575. .event_id = {
  4576. .header = {
  4577. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  4578. .misc = 0,
  4579. .size = sizeof(task_event.event_id),
  4580. },
  4581. /* .pid */
  4582. /* .ppid */
  4583. /* .tid */
  4584. /* .ptid */
  4585. /* .time */
  4586. },
  4587. };
  4588. perf_event_aux(perf_event_task_output,
  4589. &task_event,
  4590. task_ctx);
  4591. }
  4592. void perf_event_fork(struct task_struct *task)
  4593. {
  4594. perf_event_task(task, NULL, 1);
  4595. }
  4596. /*
  4597. * comm tracking
  4598. */
  4599. struct perf_comm_event {
  4600. struct task_struct *task;
  4601. char *comm;
  4602. int comm_size;
  4603. struct {
  4604. struct perf_event_header header;
  4605. u32 pid;
  4606. u32 tid;
  4607. } event_id;
  4608. };
  4609. static int perf_event_comm_match(struct perf_event *event)
  4610. {
  4611. return event->attr.comm;
  4612. }
  4613. static void perf_event_comm_output(struct perf_event *event,
  4614. void *data)
  4615. {
  4616. struct perf_comm_event *comm_event = data;
  4617. struct perf_output_handle handle;
  4618. struct perf_sample_data sample;
  4619. int size = comm_event->event_id.header.size;
  4620. int ret;
  4621. if (!perf_event_comm_match(event))
  4622. return;
  4623. perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
  4624. ret = perf_output_begin(&handle, event,
  4625. comm_event->event_id.header.size);
  4626. if (ret)
  4627. goto out;
  4628. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  4629. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  4630. perf_output_put(&handle, comm_event->event_id);
  4631. __output_copy(&handle, comm_event->comm,
  4632. comm_event->comm_size);
  4633. perf_event__output_id_sample(event, &handle, &sample);
  4634. perf_output_end(&handle);
  4635. out:
  4636. comm_event->event_id.header.size = size;
  4637. }
  4638. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  4639. {
  4640. char comm[TASK_COMM_LEN];
  4641. unsigned int size;
  4642. memset(comm, 0, sizeof(comm));
  4643. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  4644. size = ALIGN(strlen(comm)+1, sizeof(u64));
  4645. comm_event->comm = comm;
  4646. comm_event->comm_size = size;
  4647. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  4648. perf_event_aux(perf_event_comm_output,
  4649. comm_event,
  4650. NULL);
  4651. }
  4652. void perf_event_comm(struct task_struct *task, bool exec)
  4653. {
  4654. struct perf_comm_event comm_event;
  4655. if (!atomic_read(&nr_comm_events))
  4656. return;
  4657. comm_event = (struct perf_comm_event){
  4658. .task = task,
  4659. /* .comm */
  4660. /* .comm_size */
  4661. .event_id = {
  4662. .header = {
  4663. .type = PERF_RECORD_COMM,
  4664. .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
  4665. /* .size */
  4666. },
  4667. /* .pid */
  4668. /* .tid */
  4669. },
  4670. };
  4671. perf_event_comm_event(&comm_event);
  4672. }
  4673. /*
  4674. * mmap tracking
  4675. */
  4676. struct perf_mmap_event {
  4677. struct vm_area_struct *vma;
  4678. const char *file_name;
  4679. int file_size;
  4680. int maj, min;
  4681. u64 ino;
  4682. u64 ino_generation;
  4683. u32 prot, flags;
  4684. struct {
  4685. struct perf_event_header header;
  4686. u32 pid;
  4687. u32 tid;
  4688. u64 start;
  4689. u64 len;
  4690. u64 pgoff;
  4691. } event_id;
  4692. };
  4693. static int perf_event_mmap_match(struct perf_event *event,
  4694. void *data)
  4695. {
  4696. struct perf_mmap_event *mmap_event = data;
  4697. struct vm_area_struct *vma = mmap_event->vma;
  4698. int executable = vma->vm_flags & VM_EXEC;
  4699. return (!executable && event->attr.mmap_data) ||
  4700. (executable && (event->attr.mmap || event->attr.mmap2));
  4701. }
  4702. static void perf_event_mmap_output(struct perf_event *event,
  4703. void *data)
  4704. {
  4705. struct perf_mmap_event *mmap_event = data;
  4706. struct perf_output_handle handle;
  4707. struct perf_sample_data sample;
  4708. int size = mmap_event->event_id.header.size;
  4709. int ret;
  4710. if (!perf_event_mmap_match(event, data))
  4711. return;
  4712. if (event->attr.mmap2) {
  4713. mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
  4714. mmap_event->event_id.header.size += sizeof(mmap_event->maj);
  4715. mmap_event->event_id.header.size += sizeof(mmap_event->min);
  4716. mmap_event->event_id.header.size += sizeof(mmap_event->ino);
  4717. mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
  4718. mmap_event->event_id.header.size += sizeof(mmap_event->prot);
  4719. mmap_event->event_id.header.size += sizeof(mmap_event->flags);
  4720. }
  4721. perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
  4722. ret = perf_output_begin(&handle, event,
  4723. mmap_event->event_id.header.size);
  4724. if (ret)
  4725. goto out;
  4726. mmap_event->event_id.pid = perf_event_pid(event, current);
  4727. mmap_event->event_id.tid = perf_event_tid(event, current);
  4728. perf_output_put(&handle, mmap_event->event_id);
  4729. if (event->attr.mmap2) {
  4730. perf_output_put(&handle, mmap_event->maj);
  4731. perf_output_put(&handle, mmap_event->min);
  4732. perf_output_put(&handle, mmap_event->ino);
  4733. perf_output_put(&handle, mmap_event->ino_generation);
  4734. perf_output_put(&handle, mmap_event->prot);
  4735. perf_output_put(&handle, mmap_event->flags);
  4736. }
  4737. __output_copy(&handle, mmap_event->file_name,
  4738. mmap_event->file_size);
  4739. perf_event__output_id_sample(event, &handle, &sample);
  4740. perf_output_end(&handle);
  4741. out:
  4742. mmap_event->event_id.header.size = size;
  4743. }
  4744. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  4745. {
  4746. struct vm_area_struct *vma = mmap_event->vma;
  4747. struct file *file = vma->vm_file;
  4748. int maj = 0, min = 0;
  4749. u64 ino = 0, gen = 0;
  4750. u32 prot = 0, flags = 0;
  4751. unsigned int size;
  4752. char tmp[16];
  4753. char *buf = NULL;
  4754. char *name;
  4755. if (file) {
  4756. struct inode *inode;
  4757. dev_t dev;
  4758. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  4759. if (!buf) {
  4760. name = "//enomem";
  4761. goto cpy_name;
  4762. }
  4763. /*
  4764. * d_path() works from the end of the rb backwards, so we
  4765. * need to add enough zero bytes after the string to handle
  4766. * the 64bit alignment we do later.
  4767. */
  4768. name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
  4769. if (IS_ERR(name)) {
  4770. name = "//toolong";
  4771. goto cpy_name;
  4772. }
  4773. inode = file_inode(vma->vm_file);
  4774. dev = inode->i_sb->s_dev;
  4775. ino = inode->i_ino;
  4776. gen = inode->i_generation;
  4777. maj = MAJOR(dev);
  4778. min = MINOR(dev);
  4779. if (vma->vm_flags & VM_READ)
  4780. prot |= PROT_READ;
  4781. if (vma->vm_flags & VM_WRITE)
  4782. prot |= PROT_WRITE;
  4783. if (vma->vm_flags & VM_EXEC)
  4784. prot |= PROT_EXEC;
  4785. if (vma->vm_flags & VM_MAYSHARE)
  4786. flags = MAP_SHARED;
  4787. else
  4788. flags = MAP_PRIVATE;
  4789. if (vma->vm_flags & VM_DENYWRITE)
  4790. flags |= MAP_DENYWRITE;
  4791. if (vma->vm_flags & VM_MAYEXEC)
  4792. flags |= MAP_EXECUTABLE;
  4793. if (vma->vm_flags & VM_LOCKED)
  4794. flags |= MAP_LOCKED;
  4795. if (vma->vm_flags & VM_HUGETLB)
  4796. flags |= MAP_HUGETLB;
  4797. goto got_name;
  4798. } else {
  4799. if (vma->vm_ops && vma->vm_ops->name) {
  4800. name = (char *) vma->vm_ops->name(vma);
  4801. if (name)
  4802. goto cpy_name;
  4803. }
  4804. name = (char *)arch_vma_name(vma);
  4805. if (name)
  4806. goto cpy_name;
  4807. if (vma->vm_start <= vma->vm_mm->start_brk &&
  4808. vma->vm_end >= vma->vm_mm->brk) {
  4809. name = "[heap]";
  4810. goto cpy_name;
  4811. }
  4812. if (vma->vm_start <= vma->vm_mm->start_stack &&
  4813. vma->vm_end >= vma->vm_mm->start_stack) {
  4814. name = "[stack]";
  4815. goto cpy_name;
  4816. }
  4817. name = "//anon";
  4818. goto cpy_name;
  4819. }
  4820. cpy_name:
  4821. strlcpy(tmp, name, sizeof(tmp));
  4822. name = tmp;
  4823. got_name:
  4824. /*
  4825. * Since our buffer works in 8 byte units we need to align our string
  4826. * size to a multiple of 8. However, we must guarantee the tail end is
  4827. * zero'd out to avoid leaking random bits to userspace.
  4828. */
  4829. size = strlen(name)+1;
  4830. while (!IS_ALIGNED(size, sizeof(u64)))
  4831. name[size++] = '\0';
  4832. mmap_event->file_name = name;
  4833. mmap_event->file_size = size;
  4834. mmap_event->maj = maj;
  4835. mmap_event->min = min;
  4836. mmap_event->ino = ino;
  4837. mmap_event->ino_generation = gen;
  4838. mmap_event->prot = prot;
  4839. mmap_event->flags = flags;
  4840. if (!(vma->vm_flags & VM_EXEC))
  4841. mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
  4842. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  4843. perf_event_aux(perf_event_mmap_output,
  4844. mmap_event,
  4845. NULL);
  4846. kfree(buf);
  4847. }
  4848. void perf_event_mmap(struct vm_area_struct *vma)
  4849. {
  4850. struct perf_mmap_event mmap_event;
  4851. if (!atomic_read(&nr_mmap_events))
  4852. return;
  4853. mmap_event = (struct perf_mmap_event){
  4854. .vma = vma,
  4855. /* .file_name */
  4856. /* .file_size */
  4857. .event_id = {
  4858. .header = {
  4859. .type = PERF_RECORD_MMAP,
  4860. .misc = PERF_RECORD_MISC_USER,
  4861. /* .size */
  4862. },
  4863. /* .pid */
  4864. /* .tid */
  4865. .start = vma->vm_start,
  4866. .len = vma->vm_end - vma->vm_start,
  4867. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  4868. },
  4869. /* .maj (attr_mmap2 only) */
  4870. /* .min (attr_mmap2 only) */
  4871. /* .ino (attr_mmap2 only) */
  4872. /* .ino_generation (attr_mmap2 only) */
  4873. /* .prot (attr_mmap2 only) */
  4874. /* .flags (attr_mmap2 only) */
  4875. };
  4876. perf_event_mmap_event(&mmap_event);
  4877. }
  4878. void perf_event_aux_event(struct perf_event *event, unsigned long head,
  4879. unsigned long size, u64 flags)
  4880. {
  4881. struct perf_output_handle handle;
  4882. struct perf_sample_data sample;
  4883. struct perf_aux_event {
  4884. struct perf_event_header header;
  4885. u64 offset;
  4886. u64 size;
  4887. u64 flags;
  4888. } rec = {
  4889. .header = {
  4890. .type = PERF_RECORD_AUX,
  4891. .misc = 0,
  4892. .size = sizeof(rec),
  4893. },
  4894. .offset = head,
  4895. .size = size,
  4896. .flags = flags,
  4897. };
  4898. int ret;
  4899. perf_event_header__init_id(&rec.header, &sample, event);
  4900. ret = perf_output_begin(&handle, event, rec.header.size);
  4901. if (ret)
  4902. return;
  4903. perf_output_put(&handle, rec);
  4904. perf_event__output_id_sample(event, &handle, &sample);
  4905. perf_output_end(&handle);
  4906. }
  4907. /*
  4908. * IRQ throttle logging
  4909. */
  4910. static void perf_log_throttle(struct perf_event *event, int enable)
  4911. {
  4912. struct perf_output_handle handle;
  4913. struct perf_sample_data sample;
  4914. int ret;
  4915. struct {
  4916. struct perf_event_header header;
  4917. u64 time;
  4918. u64 id;
  4919. u64 stream_id;
  4920. } throttle_event = {
  4921. .header = {
  4922. .type = PERF_RECORD_THROTTLE,
  4923. .misc = 0,
  4924. .size = sizeof(throttle_event),
  4925. },
  4926. .time = perf_event_clock(event),
  4927. .id = primary_event_id(event),
  4928. .stream_id = event->id,
  4929. };
  4930. if (enable)
  4931. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  4932. perf_event_header__init_id(&throttle_event.header, &sample, event);
  4933. ret = perf_output_begin(&handle, event,
  4934. throttle_event.header.size);
  4935. if (ret)
  4936. return;
  4937. perf_output_put(&handle, throttle_event);
  4938. perf_event__output_id_sample(event, &handle, &sample);
  4939. perf_output_end(&handle);
  4940. }
  4941. static void perf_log_itrace_start(struct perf_event *event)
  4942. {
  4943. struct perf_output_handle handle;
  4944. struct perf_sample_data sample;
  4945. struct perf_aux_event {
  4946. struct perf_event_header header;
  4947. u32 pid;
  4948. u32 tid;
  4949. } rec;
  4950. int ret;
  4951. if (event->parent)
  4952. event = event->parent;
  4953. if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
  4954. event->hw.itrace_started)
  4955. return;
  4956. event->hw.itrace_started = 1;
  4957. rec.header.type = PERF_RECORD_ITRACE_START;
  4958. rec.header.misc = 0;
  4959. rec.header.size = sizeof(rec);
  4960. rec.pid = perf_event_pid(event, current);
  4961. rec.tid = perf_event_tid(event, current);
  4962. perf_event_header__init_id(&rec.header, &sample, event);
  4963. ret = perf_output_begin(&handle, event, rec.header.size);
  4964. if (ret)
  4965. return;
  4966. perf_output_put(&handle, rec);
  4967. perf_event__output_id_sample(event, &handle, &sample);
  4968. perf_output_end(&handle);
  4969. }
  4970. /*
  4971. * Generic event overflow handling, sampling.
  4972. */
  4973. static int __perf_event_overflow(struct perf_event *event,
  4974. int throttle, struct perf_sample_data *data,
  4975. struct pt_regs *regs)
  4976. {
  4977. int events = atomic_read(&event->event_limit);
  4978. struct hw_perf_event *hwc = &event->hw;
  4979. u64 seq;
  4980. int ret = 0;
  4981. /*
  4982. * Non-sampling counters might still use the PMI to fold short
  4983. * hardware counters, ignore those.
  4984. */
  4985. if (unlikely(!is_sampling_event(event)))
  4986. return 0;
  4987. seq = __this_cpu_read(perf_throttled_seq);
  4988. if (seq != hwc->interrupts_seq) {
  4989. hwc->interrupts_seq = seq;
  4990. hwc->interrupts = 1;
  4991. } else {
  4992. hwc->interrupts++;
  4993. if (unlikely(throttle
  4994. && hwc->interrupts >= max_samples_per_tick)) {
  4995. __this_cpu_inc(perf_throttled_count);
  4996. hwc->interrupts = MAX_INTERRUPTS;
  4997. perf_log_throttle(event, 0);
  4998. tick_nohz_full_kick();
  4999. ret = 1;
  5000. }
  5001. }
  5002. if (event->attr.freq) {
  5003. u64 now = perf_clock();
  5004. s64 delta = now - hwc->freq_time_stamp;
  5005. hwc->freq_time_stamp = now;
  5006. if (delta > 0 && delta < 2*TICK_NSEC)
  5007. perf_adjust_period(event, delta, hwc->last_period, true);
  5008. }
  5009. /*
  5010. * XXX event_limit might not quite work as expected on inherited
  5011. * events
  5012. */
  5013. event->pending_kill = POLL_IN;
  5014. if (events && atomic_dec_and_test(&event->event_limit)) {
  5015. ret = 1;
  5016. event->pending_kill = POLL_HUP;
  5017. event->pending_disable = 1;
  5018. irq_work_queue(&event->pending);
  5019. }
  5020. if (event->overflow_handler)
  5021. event->overflow_handler(event, data, regs);
  5022. else
  5023. perf_event_output(event, data, regs);
  5024. if (event->fasync && event->pending_kill) {
  5025. event->pending_wakeup = 1;
  5026. irq_work_queue(&event->pending);
  5027. }
  5028. return ret;
  5029. }
  5030. int perf_event_overflow(struct perf_event *event,
  5031. struct perf_sample_data *data,
  5032. struct pt_regs *regs)
  5033. {
  5034. return __perf_event_overflow(event, 1, data, regs);
  5035. }
  5036. /*
  5037. * Generic software event infrastructure
  5038. */
  5039. struct swevent_htable {
  5040. struct swevent_hlist *swevent_hlist;
  5041. struct mutex hlist_mutex;
  5042. int hlist_refcount;
  5043. /* Recursion avoidance in each contexts */
  5044. int recursion[PERF_NR_CONTEXTS];
  5045. /* Keeps track of cpu being initialized/exited */
  5046. bool online;
  5047. };
  5048. static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
  5049. /*
  5050. * We directly increment event->count and keep a second value in
  5051. * event->hw.period_left to count intervals. This period event
  5052. * is kept in the range [-sample_period, 0] so that we can use the
  5053. * sign as trigger.
  5054. */
  5055. u64 perf_swevent_set_period(struct perf_event *event)
  5056. {
  5057. struct hw_perf_event *hwc = &event->hw;
  5058. u64 period = hwc->last_period;
  5059. u64 nr, offset;
  5060. s64 old, val;
  5061. hwc->last_period = hwc->sample_period;
  5062. again:
  5063. old = val = local64_read(&hwc->period_left);
  5064. if (val < 0)
  5065. return 0;
  5066. nr = div64_u64(period + val, period);
  5067. offset = nr * period;
  5068. val -= offset;
  5069. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  5070. goto again;
  5071. return nr;
  5072. }
  5073. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  5074. struct perf_sample_data *data,
  5075. struct pt_regs *regs)
  5076. {
  5077. struct hw_perf_event *hwc = &event->hw;
  5078. int throttle = 0;
  5079. if (!overflow)
  5080. overflow = perf_swevent_set_period(event);
  5081. if (hwc->interrupts == MAX_INTERRUPTS)
  5082. return;
  5083. for (; overflow; overflow--) {
  5084. if (__perf_event_overflow(event, throttle,
  5085. data, regs)) {
  5086. /*
  5087. * We inhibit the overflow from happening when
  5088. * hwc->interrupts == MAX_INTERRUPTS.
  5089. */
  5090. break;
  5091. }
  5092. throttle = 1;
  5093. }
  5094. }
  5095. static void perf_swevent_event(struct perf_event *event, u64 nr,
  5096. struct perf_sample_data *data,
  5097. struct pt_regs *regs)
  5098. {
  5099. struct hw_perf_event *hwc = &event->hw;
  5100. local64_add(nr, &event->count);
  5101. if (!regs)
  5102. return;
  5103. if (!is_sampling_event(event))
  5104. return;
  5105. if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
  5106. data->period = nr;
  5107. return perf_swevent_overflow(event, 1, data, regs);
  5108. } else
  5109. data->period = event->hw.last_period;
  5110. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  5111. return perf_swevent_overflow(event, 1, data, regs);
  5112. if (local64_add_negative(nr, &hwc->period_left))
  5113. return;
  5114. perf_swevent_overflow(event, 0, data, regs);
  5115. }
  5116. static int perf_exclude_event(struct perf_event *event,
  5117. struct pt_regs *regs)
  5118. {
  5119. if (event->hw.state & PERF_HES_STOPPED)
  5120. return 1;
  5121. if (regs) {
  5122. if (event->attr.exclude_user && user_mode(regs))
  5123. return 1;
  5124. if (event->attr.exclude_kernel && !user_mode(regs))
  5125. return 1;
  5126. }
  5127. return 0;
  5128. }
  5129. static int perf_swevent_match(struct perf_event *event,
  5130. enum perf_type_id type,
  5131. u32 event_id,
  5132. struct perf_sample_data *data,
  5133. struct pt_regs *regs)
  5134. {
  5135. if (event->attr.type != type)
  5136. return 0;
  5137. if (event->attr.config != event_id)
  5138. return 0;
  5139. if (perf_exclude_event(event, regs))
  5140. return 0;
  5141. return 1;
  5142. }
  5143. static inline u64 swevent_hash(u64 type, u32 event_id)
  5144. {
  5145. u64 val = event_id | (type << 32);
  5146. return hash_64(val, SWEVENT_HLIST_BITS);
  5147. }
  5148. static inline struct hlist_head *
  5149. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  5150. {
  5151. u64 hash = swevent_hash(type, event_id);
  5152. return &hlist->heads[hash];
  5153. }
  5154. /* For the read side: events when they trigger */
  5155. static inline struct hlist_head *
  5156. find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
  5157. {
  5158. struct swevent_hlist *hlist;
  5159. hlist = rcu_dereference(swhash->swevent_hlist);
  5160. if (!hlist)
  5161. return NULL;
  5162. return __find_swevent_head(hlist, type, event_id);
  5163. }
  5164. /* For the event head insertion and removal in the hlist */
  5165. static inline struct hlist_head *
  5166. find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
  5167. {
  5168. struct swevent_hlist *hlist;
  5169. u32 event_id = event->attr.config;
  5170. u64 type = event->attr.type;
  5171. /*
  5172. * Event scheduling is always serialized against hlist allocation
  5173. * and release. Which makes the protected version suitable here.
  5174. * The context lock guarantees that.
  5175. */
  5176. hlist = rcu_dereference_protected(swhash->swevent_hlist,
  5177. lockdep_is_held(&event->ctx->lock));
  5178. if (!hlist)
  5179. return NULL;
  5180. return __find_swevent_head(hlist, type, event_id);
  5181. }
  5182. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  5183. u64 nr,
  5184. struct perf_sample_data *data,
  5185. struct pt_regs *regs)
  5186. {
  5187. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5188. struct perf_event *event;
  5189. struct hlist_head *head;
  5190. rcu_read_lock();
  5191. head = find_swevent_head_rcu(swhash, type, event_id);
  5192. if (!head)
  5193. goto end;
  5194. hlist_for_each_entry_rcu(event, head, hlist_entry) {
  5195. if (perf_swevent_match(event, type, event_id, data, regs))
  5196. perf_swevent_event(event, nr, data, regs);
  5197. }
  5198. end:
  5199. rcu_read_unlock();
  5200. }
  5201. DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
  5202. int perf_swevent_get_recursion_context(void)
  5203. {
  5204. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5205. return get_recursion_context(swhash->recursion);
  5206. }
  5207. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  5208. inline void perf_swevent_put_recursion_context(int rctx)
  5209. {
  5210. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5211. put_recursion_context(swhash->recursion, rctx);
  5212. }
  5213. void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  5214. {
  5215. struct perf_sample_data data;
  5216. if (WARN_ON_ONCE(!regs))
  5217. return;
  5218. perf_sample_data_init(&data, addr, 0);
  5219. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
  5220. }
  5221. void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  5222. {
  5223. int rctx;
  5224. preempt_disable_notrace();
  5225. rctx = perf_swevent_get_recursion_context();
  5226. if (unlikely(rctx < 0))
  5227. goto fail;
  5228. ___perf_sw_event(event_id, nr, regs, addr);
  5229. perf_swevent_put_recursion_context(rctx);
  5230. fail:
  5231. preempt_enable_notrace();
  5232. }
  5233. static void perf_swevent_read(struct perf_event *event)
  5234. {
  5235. }
  5236. static int perf_swevent_add(struct perf_event *event, int flags)
  5237. {
  5238. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5239. struct hw_perf_event *hwc = &event->hw;
  5240. struct hlist_head *head;
  5241. if (is_sampling_event(event)) {
  5242. hwc->last_period = hwc->sample_period;
  5243. perf_swevent_set_period(event);
  5244. }
  5245. hwc->state = !(flags & PERF_EF_START);
  5246. head = find_swevent_head(swhash, event);
  5247. if (!head) {
  5248. /*
  5249. * We can race with cpu hotplug code. Do not
  5250. * WARN if the cpu just got unplugged.
  5251. */
  5252. WARN_ON_ONCE(swhash->online);
  5253. return -EINVAL;
  5254. }
  5255. hlist_add_head_rcu(&event->hlist_entry, head);
  5256. perf_event_update_userpage(event);
  5257. return 0;
  5258. }
  5259. static void perf_swevent_del(struct perf_event *event, int flags)
  5260. {
  5261. hlist_del_rcu(&event->hlist_entry);
  5262. }
  5263. static void perf_swevent_start(struct perf_event *event, int flags)
  5264. {
  5265. event->hw.state = 0;
  5266. }
  5267. static void perf_swevent_stop(struct perf_event *event, int flags)
  5268. {
  5269. event->hw.state = PERF_HES_STOPPED;
  5270. }
  5271. /* Deref the hlist from the update side */
  5272. static inline struct swevent_hlist *
  5273. swevent_hlist_deref(struct swevent_htable *swhash)
  5274. {
  5275. return rcu_dereference_protected(swhash->swevent_hlist,
  5276. lockdep_is_held(&swhash->hlist_mutex));
  5277. }
  5278. static void swevent_hlist_release(struct swevent_htable *swhash)
  5279. {
  5280. struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
  5281. if (!hlist)
  5282. return;
  5283. RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
  5284. kfree_rcu(hlist, rcu_head);
  5285. }
  5286. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  5287. {
  5288. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5289. mutex_lock(&swhash->hlist_mutex);
  5290. if (!--swhash->hlist_refcount)
  5291. swevent_hlist_release(swhash);
  5292. mutex_unlock(&swhash->hlist_mutex);
  5293. }
  5294. static void swevent_hlist_put(struct perf_event *event)
  5295. {
  5296. int cpu;
  5297. for_each_possible_cpu(cpu)
  5298. swevent_hlist_put_cpu(event, cpu);
  5299. }
  5300. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  5301. {
  5302. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5303. int err = 0;
  5304. mutex_lock(&swhash->hlist_mutex);
  5305. if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
  5306. struct swevent_hlist *hlist;
  5307. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  5308. if (!hlist) {
  5309. err = -ENOMEM;
  5310. goto exit;
  5311. }
  5312. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  5313. }
  5314. swhash->hlist_refcount++;
  5315. exit:
  5316. mutex_unlock(&swhash->hlist_mutex);
  5317. return err;
  5318. }
  5319. static int swevent_hlist_get(struct perf_event *event)
  5320. {
  5321. int err;
  5322. int cpu, failed_cpu;
  5323. get_online_cpus();
  5324. for_each_possible_cpu(cpu) {
  5325. err = swevent_hlist_get_cpu(event, cpu);
  5326. if (err) {
  5327. failed_cpu = cpu;
  5328. goto fail;
  5329. }
  5330. }
  5331. put_online_cpus();
  5332. return 0;
  5333. fail:
  5334. for_each_possible_cpu(cpu) {
  5335. if (cpu == failed_cpu)
  5336. break;
  5337. swevent_hlist_put_cpu(event, cpu);
  5338. }
  5339. put_online_cpus();
  5340. return err;
  5341. }
  5342. struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  5343. static void sw_perf_event_destroy(struct perf_event *event)
  5344. {
  5345. u64 event_id = event->attr.config;
  5346. WARN_ON(event->parent);
  5347. static_key_slow_dec(&perf_swevent_enabled[event_id]);
  5348. swevent_hlist_put(event);
  5349. }
  5350. static int perf_swevent_init(struct perf_event *event)
  5351. {
  5352. u64 event_id = event->attr.config;
  5353. if (event->attr.type != PERF_TYPE_SOFTWARE)
  5354. return -ENOENT;
  5355. /*
  5356. * no branch sampling for software events
  5357. */
  5358. if (has_branch_stack(event))
  5359. return -EOPNOTSUPP;
  5360. switch (event_id) {
  5361. case PERF_COUNT_SW_CPU_CLOCK:
  5362. case PERF_COUNT_SW_TASK_CLOCK:
  5363. return -ENOENT;
  5364. default:
  5365. break;
  5366. }
  5367. if (event_id >= PERF_COUNT_SW_MAX)
  5368. return -ENOENT;
  5369. if (!event->parent) {
  5370. int err;
  5371. err = swevent_hlist_get(event);
  5372. if (err)
  5373. return err;
  5374. static_key_slow_inc(&perf_swevent_enabled[event_id]);
  5375. event->destroy = sw_perf_event_destroy;
  5376. }
  5377. return 0;
  5378. }
  5379. static struct pmu perf_swevent = {
  5380. .task_ctx_nr = perf_sw_context,
  5381. .capabilities = PERF_PMU_CAP_NO_NMI,
  5382. .event_init = perf_swevent_init,
  5383. .add = perf_swevent_add,
  5384. .del = perf_swevent_del,
  5385. .start = perf_swevent_start,
  5386. .stop = perf_swevent_stop,
  5387. .read = perf_swevent_read,
  5388. };
  5389. #ifdef CONFIG_EVENT_TRACING
  5390. static int perf_tp_filter_match(struct perf_event *event,
  5391. struct perf_sample_data *data)
  5392. {
  5393. void *record = data->raw->data;
  5394. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  5395. return 1;
  5396. return 0;
  5397. }
  5398. static int perf_tp_event_match(struct perf_event *event,
  5399. struct perf_sample_data *data,
  5400. struct pt_regs *regs)
  5401. {
  5402. if (event->hw.state & PERF_HES_STOPPED)
  5403. return 0;
  5404. /*
  5405. * All tracepoints are from kernel-space.
  5406. */
  5407. if (event->attr.exclude_kernel)
  5408. return 0;
  5409. if (!perf_tp_filter_match(event, data))
  5410. return 0;
  5411. return 1;
  5412. }
  5413. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  5414. struct pt_regs *regs, struct hlist_head *head, int rctx,
  5415. struct task_struct *task)
  5416. {
  5417. struct perf_sample_data data;
  5418. struct perf_event *event;
  5419. struct perf_raw_record raw = {
  5420. .size = entry_size,
  5421. .data = record,
  5422. };
  5423. perf_sample_data_init(&data, addr, 0);
  5424. data.raw = &raw;
  5425. hlist_for_each_entry_rcu(event, head, hlist_entry) {
  5426. if (perf_tp_event_match(event, &data, regs))
  5427. perf_swevent_event(event, count, &data, regs);
  5428. }
  5429. /*
  5430. * If we got specified a target task, also iterate its context and
  5431. * deliver this event there too.
  5432. */
  5433. if (task && task != current) {
  5434. struct perf_event_context *ctx;
  5435. struct trace_entry *entry = record;
  5436. rcu_read_lock();
  5437. ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
  5438. if (!ctx)
  5439. goto unlock;
  5440. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  5441. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5442. continue;
  5443. if (event->attr.config != entry->type)
  5444. continue;
  5445. if (perf_tp_event_match(event, &data, regs))
  5446. perf_swevent_event(event, count, &data, regs);
  5447. }
  5448. unlock:
  5449. rcu_read_unlock();
  5450. }
  5451. perf_swevent_put_recursion_context(rctx);
  5452. }
  5453. EXPORT_SYMBOL_GPL(perf_tp_event);
  5454. static void tp_perf_event_destroy(struct perf_event *event)
  5455. {
  5456. perf_trace_destroy(event);
  5457. }
  5458. static int perf_tp_event_init(struct perf_event *event)
  5459. {
  5460. int err;
  5461. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5462. return -ENOENT;
  5463. /*
  5464. * no branch sampling for tracepoint events
  5465. */
  5466. if (has_branch_stack(event))
  5467. return -EOPNOTSUPP;
  5468. err = perf_trace_init(event);
  5469. if (err)
  5470. return err;
  5471. event->destroy = tp_perf_event_destroy;
  5472. return 0;
  5473. }
  5474. static struct pmu perf_tracepoint = {
  5475. .task_ctx_nr = perf_sw_context,
  5476. .event_init = perf_tp_event_init,
  5477. .add = perf_trace_add,
  5478. .del = perf_trace_del,
  5479. .start = perf_swevent_start,
  5480. .stop = perf_swevent_stop,
  5481. .read = perf_swevent_read,
  5482. };
  5483. static inline void perf_tp_register(void)
  5484. {
  5485. perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
  5486. }
  5487. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  5488. {
  5489. char *filter_str;
  5490. int ret;
  5491. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5492. return -EINVAL;
  5493. filter_str = strndup_user(arg, PAGE_SIZE);
  5494. if (IS_ERR(filter_str))
  5495. return PTR_ERR(filter_str);
  5496. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  5497. kfree(filter_str);
  5498. return ret;
  5499. }
  5500. static void perf_event_free_filter(struct perf_event *event)
  5501. {
  5502. ftrace_profile_free_filter(event);
  5503. }
  5504. static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
  5505. {
  5506. struct bpf_prog *prog;
  5507. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5508. return -EINVAL;
  5509. if (event->tp_event->prog)
  5510. return -EEXIST;
  5511. if (!(event->tp_event->flags & TRACE_EVENT_FL_KPROBE))
  5512. /* bpf programs can only be attached to kprobes */
  5513. return -EINVAL;
  5514. prog = bpf_prog_get(prog_fd);
  5515. if (IS_ERR(prog))
  5516. return PTR_ERR(prog);
  5517. if (prog->type != BPF_PROG_TYPE_KPROBE) {
  5518. /* valid fd, but invalid bpf program type */
  5519. bpf_prog_put(prog);
  5520. return -EINVAL;
  5521. }
  5522. event->tp_event->prog = prog;
  5523. return 0;
  5524. }
  5525. static void perf_event_free_bpf_prog(struct perf_event *event)
  5526. {
  5527. struct bpf_prog *prog;
  5528. if (!event->tp_event)
  5529. return;
  5530. prog = event->tp_event->prog;
  5531. if (prog) {
  5532. event->tp_event->prog = NULL;
  5533. bpf_prog_put(prog);
  5534. }
  5535. }
  5536. #else
  5537. static inline void perf_tp_register(void)
  5538. {
  5539. }
  5540. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  5541. {
  5542. return -ENOENT;
  5543. }
  5544. static void perf_event_free_filter(struct perf_event *event)
  5545. {
  5546. }
  5547. static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
  5548. {
  5549. return -ENOENT;
  5550. }
  5551. static void perf_event_free_bpf_prog(struct perf_event *event)
  5552. {
  5553. }
  5554. #endif /* CONFIG_EVENT_TRACING */
  5555. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  5556. void perf_bp_event(struct perf_event *bp, void *data)
  5557. {
  5558. struct perf_sample_data sample;
  5559. struct pt_regs *regs = data;
  5560. perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
  5561. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  5562. perf_swevent_event(bp, 1, &sample, regs);
  5563. }
  5564. #endif
  5565. /*
  5566. * hrtimer based swevent callback
  5567. */
  5568. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  5569. {
  5570. enum hrtimer_restart ret = HRTIMER_RESTART;
  5571. struct perf_sample_data data;
  5572. struct pt_regs *regs;
  5573. struct perf_event *event;
  5574. u64 period;
  5575. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  5576. if (event->state != PERF_EVENT_STATE_ACTIVE)
  5577. return HRTIMER_NORESTART;
  5578. event->pmu->read(event);
  5579. perf_sample_data_init(&data, 0, event->hw.last_period);
  5580. regs = get_irq_regs();
  5581. if (regs && !perf_exclude_event(event, regs)) {
  5582. if (!(event->attr.exclude_idle && is_idle_task(current)))
  5583. if (__perf_event_overflow(event, 1, &data, regs))
  5584. ret = HRTIMER_NORESTART;
  5585. }
  5586. period = max_t(u64, 10000, event->hw.sample_period);
  5587. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  5588. return ret;
  5589. }
  5590. static void perf_swevent_start_hrtimer(struct perf_event *event)
  5591. {
  5592. struct hw_perf_event *hwc = &event->hw;
  5593. s64 period;
  5594. if (!is_sampling_event(event))
  5595. return;
  5596. period = local64_read(&hwc->period_left);
  5597. if (period) {
  5598. if (period < 0)
  5599. period = 10000;
  5600. local64_set(&hwc->period_left, 0);
  5601. } else {
  5602. period = max_t(u64, 10000, hwc->sample_period);
  5603. }
  5604. __hrtimer_start_range_ns(&hwc->hrtimer,
  5605. ns_to_ktime(period), 0,
  5606. HRTIMER_MODE_REL_PINNED, 0);
  5607. }
  5608. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  5609. {
  5610. struct hw_perf_event *hwc = &event->hw;
  5611. if (is_sampling_event(event)) {
  5612. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  5613. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  5614. hrtimer_cancel(&hwc->hrtimer);
  5615. }
  5616. }
  5617. static void perf_swevent_init_hrtimer(struct perf_event *event)
  5618. {
  5619. struct hw_perf_event *hwc = &event->hw;
  5620. if (!is_sampling_event(event))
  5621. return;
  5622. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  5623. hwc->hrtimer.function = perf_swevent_hrtimer;
  5624. /*
  5625. * Since hrtimers have a fixed rate, we can do a static freq->period
  5626. * mapping and avoid the whole period adjust feedback stuff.
  5627. */
  5628. if (event->attr.freq) {
  5629. long freq = event->attr.sample_freq;
  5630. event->attr.sample_period = NSEC_PER_SEC / freq;
  5631. hwc->sample_period = event->attr.sample_period;
  5632. local64_set(&hwc->period_left, hwc->sample_period);
  5633. hwc->last_period = hwc->sample_period;
  5634. event->attr.freq = 0;
  5635. }
  5636. }
  5637. /*
  5638. * Software event: cpu wall time clock
  5639. */
  5640. static void cpu_clock_event_update(struct perf_event *event)
  5641. {
  5642. s64 prev;
  5643. u64 now;
  5644. now = local_clock();
  5645. prev = local64_xchg(&event->hw.prev_count, now);
  5646. local64_add(now - prev, &event->count);
  5647. }
  5648. static void cpu_clock_event_start(struct perf_event *event, int flags)
  5649. {
  5650. local64_set(&event->hw.prev_count, local_clock());
  5651. perf_swevent_start_hrtimer(event);
  5652. }
  5653. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  5654. {
  5655. perf_swevent_cancel_hrtimer(event);
  5656. cpu_clock_event_update(event);
  5657. }
  5658. static int cpu_clock_event_add(struct perf_event *event, int flags)
  5659. {
  5660. if (flags & PERF_EF_START)
  5661. cpu_clock_event_start(event, flags);
  5662. perf_event_update_userpage(event);
  5663. return 0;
  5664. }
  5665. static void cpu_clock_event_del(struct perf_event *event, int flags)
  5666. {
  5667. cpu_clock_event_stop(event, flags);
  5668. }
  5669. static void cpu_clock_event_read(struct perf_event *event)
  5670. {
  5671. cpu_clock_event_update(event);
  5672. }
  5673. static int cpu_clock_event_init(struct perf_event *event)
  5674. {
  5675. if (event->attr.type != PERF_TYPE_SOFTWARE)
  5676. return -ENOENT;
  5677. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  5678. return -ENOENT;
  5679. /*
  5680. * no branch sampling for software events
  5681. */
  5682. if (has_branch_stack(event))
  5683. return -EOPNOTSUPP;
  5684. perf_swevent_init_hrtimer(event);
  5685. return 0;
  5686. }
  5687. static struct pmu perf_cpu_clock = {
  5688. .task_ctx_nr = perf_sw_context,
  5689. .capabilities = PERF_PMU_CAP_NO_NMI,
  5690. .event_init = cpu_clock_event_init,
  5691. .add = cpu_clock_event_add,
  5692. .del = cpu_clock_event_del,
  5693. .start = cpu_clock_event_start,
  5694. .stop = cpu_clock_event_stop,
  5695. .read = cpu_clock_event_read,
  5696. };
  5697. /*
  5698. * Software event: task time clock
  5699. */
  5700. static void task_clock_event_update(struct perf_event *event, u64 now)
  5701. {
  5702. u64 prev;
  5703. s64 delta;
  5704. prev = local64_xchg(&event->hw.prev_count, now);
  5705. delta = now - prev;
  5706. local64_add(delta, &event->count);
  5707. }
  5708. static void task_clock_event_start(struct perf_event *event, int flags)
  5709. {
  5710. local64_set(&event->hw.prev_count, event->ctx->time);
  5711. perf_swevent_start_hrtimer(event);
  5712. }
  5713. static void task_clock_event_stop(struct perf_event *event, int flags)
  5714. {
  5715. perf_swevent_cancel_hrtimer(event);
  5716. task_clock_event_update(event, event->ctx->time);
  5717. }
  5718. static int task_clock_event_add(struct perf_event *event, int flags)
  5719. {
  5720. if (flags & PERF_EF_START)
  5721. task_clock_event_start(event, flags);
  5722. perf_event_update_userpage(event);
  5723. return 0;
  5724. }
  5725. static void task_clock_event_del(struct perf_event *event, int flags)
  5726. {
  5727. task_clock_event_stop(event, PERF_EF_UPDATE);
  5728. }
  5729. static void task_clock_event_read(struct perf_event *event)
  5730. {
  5731. u64 now = perf_clock();
  5732. u64 delta = now - event->ctx->timestamp;
  5733. u64 time = event->ctx->time + delta;
  5734. task_clock_event_update(event, time);
  5735. }
  5736. static int task_clock_event_init(struct perf_event *event)
  5737. {
  5738. if (event->attr.type != PERF_TYPE_SOFTWARE)
  5739. return -ENOENT;
  5740. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  5741. return -ENOENT;
  5742. /*
  5743. * no branch sampling for software events
  5744. */
  5745. if (has_branch_stack(event))
  5746. return -EOPNOTSUPP;
  5747. perf_swevent_init_hrtimer(event);
  5748. return 0;
  5749. }
  5750. static struct pmu perf_task_clock = {
  5751. .task_ctx_nr = perf_sw_context,
  5752. .capabilities = PERF_PMU_CAP_NO_NMI,
  5753. .event_init = task_clock_event_init,
  5754. .add = task_clock_event_add,
  5755. .del = task_clock_event_del,
  5756. .start = task_clock_event_start,
  5757. .stop = task_clock_event_stop,
  5758. .read = task_clock_event_read,
  5759. };
  5760. static void perf_pmu_nop_void(struct pmu *pmu)
  5761. {
  5762. }
  5763. static int perf_pmu_nop_int(struct pmu *pmu)
  5764. {
  5765. return 0;
  5766. }
  5767. static void perf_pmu_start_txn(struct pmu *pmu)
  5768. {
  5769. perf_pmu_disable(pmu);
  5770. }
  5771. static int perf_pmu_commit_txn(struct pmu *pmu)
  5772. {
  5773. perf_pmu_enable(pmu);
  5774. return 0;
  5775. }
  5776. static void perf_pmu_cancel_txn(struct pmu *pmu)
  5777. {
  5778. perf_pmu_enable(pmu);
  5779. }
  5780. static int perf_event_idx_default(struct perf_event *event)
  5781. {
  5782. return 0;
  5783. }
  5784. /*
  5785. * Ensures all contexts with the same task_ctx_nr have the same
  5786. * pmu_cpu_context too.
  5787. */
  5788. static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
  5789. {
  5790. struct pmu *pmu;
  5791. if (ctxn < 0)
  5792. return NULL;
  5793. list_for_each_entry(pmu, &pmus, entry) {
  5794. if (pmu->task_ctx_nr == ctxn)
  5795. return pmu->pmu_cpu_context;
  5796. }
  5797. return NULL;
  5798. }
  5799. static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
  5800. {
  5801. int cpu;
  5802. for_each_possible_cpu(cpu) {
  5803. struct perf_cpu_context *cpuctx;
  5804. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  5805. if (cpuctx->unique_pmu == old_pmu)
  5806. cpuctx->unique_pmu = pmu;
  5807. }
  5808. }
  5809. static void free_pmu_context(struct pmu *pmu)
  5810. {
  5811. struct pmu *i;
  5812. mutex_lock(&pmus_lock);
  5813. /*
  5814. * Like a real lame refcount.
  5815. */
  5816. list_for_each_entry(i, &pmus, entry) {
  5817. if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
  5818. update_pmu_context(i, pmu);
  5819. goto out;
  5820. }
  5821. }
  5822. free_percpu(pmu->pmu_cpu_context);
  5823. out:
  5824. mutex_unlock(&pmus_lock);
  5825. }
  5826. static struct idr pmu_idr;
  5827. static ssize_t
  5828. type_show(struct device *dev, struct device_attribute *attr, char *page)
  5829. {
  5830. struct pmu *pmu = dev_get_drvdata(dev);
  5831. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
  5832. }
  5833. static DEVICE_ATTR_RO(type);
  5834. static ssize_t
  5835. perf_event_mux_interval_ms_show(struct device *dev,
  5836. struct device_attribute *attr,
  5837. char *page)
  5838. {
  5839. struct pmu *pmu = dev_get_drvdata(dev);
  5840. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
  5841. }
  5842. static ssize_t
  5843. perf_event_mux_interval_ms_store(struct device *dev,
  5844. struct device_attribute *attr,
  5845. const char *buf, size_t count)
  5846. {
  5847. struct pmu *pmu = dev_get_drvdata(dev);
  5848. int timer, cpu, ret;
  5849. ret = kstrtoint(buf, 0, &timer);
  5850. if (ret)
  5851. return ret;
  5852. if (timer < 1)
  5853. return -EINVAL;
  5854. /* same value, noting to do */
  5855. if (timer == pmu->hrtimer_interval_ms)
  5856. return count;
  5857. pmu->hrtimer_interval_ms = timer;
  5858. /* update all cpuctx for this PMU */
  5859. for_each_possible_cpu(cpu) {
  5860. struct perf_cpu_context *cpuctx;
  5861. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  5862. cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
  5863. if (hrtimer_active(&cpuctx->hrtimer))
  5864. hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
  5865. }
  5866. return count;
  5867. }
  5868. static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
  5869. static struct attribute *pmu_dev_attrs[] = {
  5870. &dev_attr_type.attr,
  5871. &dev_attr_perf_event_mux_interval_ms.attr,
  5872. NULL,
  5873. };
  5874. ATTRIBUTE_GROUPS(pmu_dev);
  5875. static int pmu_bus_running;
  5876. static struct bus_type pmu_bus = {
  5877. .name = "event_source",
  5878. .dev_groups = pmu_dev_groups,
  5879. };
  5880. static void pmu_dev_release(struct device *dev)
  5881. {
  5882. kfree(dev);
  5883. }
  5884. static int pmu_dev_alloc(struct pmu *pmu)
  5885. {
  5886. int ret = -ENOMEM;
  5887. pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  5888. if (!pmu->dev)
  5889. goto out;
  5890. pmu->dev->groups = pmu->attr_groups;
  5891. device_initialize(pmu->dev);
  5892. ret = dev_set_name(pmu->dev, "%s", pmu->name);
  5893. if (ret)
  5894. goto free_dev;
  5895. dev_set_drvdata(pmu->dev, pmu);
  5896. pmu->dev->bus = &pmu_bus;
  5897. pmu->dev->release = pmu_dev_release;
  5898. ret = device_add(pmu->dev);
  5899. if (ret)
  5900. goto free_dev;
  5901. out:
  5902. return ret;
  5903. free_dev:
  5904. put_device(pmu->dev);
  5905. goto out;
  5906. }
  5907. static struct lock_class_key cpuctx_mutex;
  5908. static struct lock_class_key cpuctx_lock;
  5909. int perf_pmu_register(struct pmu *pmu, const char *name, int type)
  5910. {
  5911. int cpu, ret;
  5912. mutex_lock(&pmus_lock);
  5913. ret = -ENOMEM;
  5914. pmu->pmu_disable_count = alloc_percpu(int);
  5915. if (!pmu->pmu_disable_count)
  5916. goto unlock;
  5917. pmu->type = -1;
  5918. if (!name)
  5919. goto skip_type;
  5920. pmu->name = name;
  5921. if (type < 0) {
  5922. type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
  5923. if (type < 0) {
  5924. ret = type;
  5925. goto free_pdc;
  5926. }
  5927. }
  5928. pmu->type = type;
  5929. if (pmu_bus_running) {
  5930. ret = pmu_dev_alloc(pmu);
  5931. if (ret)
  5932. goto free_idr;
  5933. }
  5934. skip_type:
  5935. pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  5936. if (pmu->pmu_cpu_context)
  5937. goto got_cpu_context;
  5938. ret = -ENOMEM;
  5939. pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  5940. if (!pmu->pmu_cpu_context)
  5941. goto free_dev;
  5942. for_each_possible_cpu(cpu) {
  5943. struct perf_cpu_context *cpuctx;
  5944. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  5945. __perf_event_init_context(&cpuctx->ctx);
  5946. lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
  5947. lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
  5948. cpuctx->ctx.pmu = pmu;
  5949. __perf_cpu_hrtimer_init(cpuctx, cpu);
  5950. cpuctx->unique_pmu = pmu;
  5951. }
  5952. got_cpu_context:
  5953. if (!pmu->start_txn) {
  5954. if (pmu->pmu_enable) {
  5955. /*
  5956. * If we have pmu_enable/pmu_disable calls, install
  5957. * transaction stubs that use that to try and batch
  5958. * hardware accesses.
  5959. */
  5960. pmu->start_txn = perf_pmu_start_txn;
  5961. pmu->commit_txn = perf_pmu_commit_txn;
  5962. pmu->cancel_txn = perf_pmu_cancel_txn;
  5963. } else {
  5964. pmu->start_txn = perf_pmu_nop_void;
  5965. pmu->commit_txn = perf_pmu_nop_int;
  5966. pmu->cancel_txn = perf_pmu_nop_void;
  5967. }
  5968. }
  5969. if (!pmu->pmu_enable) {
  5970. pmu->pmu_enable = perf_pmu_nop_void;
  5971. pmu->pmu_disable = perf_pmu_nop_void;
  5972. }
  5973. if (!pmu->event_idx)
  5974. pmu->event_idx = perf_event_idx_default;
  5975. list_add_rcu(&pmu->entry, &pmus);
  5976. atomic_set(&pmu->exclusive_cnt, 0);
  5977. ret = 0;
  5978. unlock:
  5979. mutex_unlock(&pmus_lock);
  5980. return ret;
  5981. free_dev:
  5982. device_del(pmu->dev);
  5983. put_device(pmu->dev);
  5984. free_idr:
  5985. if (pmu->type >= PERF_TYPE_MAX)
  5986. idr_remove(&pmu_idr, pmu->type);
  5987. free_pdc:
  5988. free_percpu(pmu->pmu_disable_count);
  5989. goto unlock;
  5990. }
  5991. EXPORT_SYMBOL_GPL(perf_pmu_register);
  5992. void perf_pmu_unregister(struct pmu *pmu)
  5993. {
  5994. mutex_lock(&pmus_lock);
  5995. list_del_rcu(&pmu->entry);
  5996. mutex_unlock(&pmus_lock);
  5997. /*
  5998. * We dereference the pmu list under both SRCU and regular RCU, so
  5999. * synchronize against both of those.
  6000. */
  6001. synchronize_srcu(&pmus_srcu);
  6002. synchronize_rcu();
  6003. free_percpu(pmu->pmu_disable_count);
  6004. if (pmu->type >= PERF_TYPE_MAX)
  6005. idr_remove(&pmu_idr, pmu->type);
  6006. device_del(pmu->dev);
  6007. put_device(pmu->dev);
  6008. free_pmu_context(pmu);
  6009. }
  6010. EXPORT_SYMBOL_GPL(perf_pmu_unregister);
  6011. static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
  6012. {
  6013. struct perf_event_context *ctx = NULL;
  6014. int ret;
  6015. if (!try_module_get(pmu->module))
  6016. return -ENODEV;
  6017. if (event->group_leader != event) {
  6018. /*
  6019. * This ctx->mutex can nest when we're called through
  6020. * inheritance. See the perf_event_ctx_lock_nested() comment.
  6021. */
  6022. ctx = perf_event_ctx_lock_nested(event->group_leader,
  6023. SINGLE_DEPTH_NESTING);
  6024. BUG_ON(!ctx);
  6025. }
  6026. event->pmu = pmu;
  6027. ret = pmu->event_init(event);
  6028. if (ctx)
  6029. perf_event_ctx_unlock(event->group_leader, ctx);
  6030. if (ret)
  6031. module_put(pmu->module);
  6032. return ret;
  6033. }
  6034. struct pmu *perf_init_event(struct perf_event *event)
  6035. {
  6036. struct pmu *pmu = NULL;
  6037. int idx;
  6038. int ret;
  6039. idx = srcu_read_lock(&pmus_srcu);
  6040. rcu_read_lock();
  6041. pmu = idr_find(&pmu_idr, event->attr.type);
  6042. rcu_read_unlock();
  6043. if (pmu) {
  6044. ret = perf_try_init_event(pmu, event);
  6045. if (ret)
  6046. pmu = ERR_PTR(ret);
  6047. goto unlock;
  6048. }
  6049. list_for_each_entry_rcu(pmu, &pmus, entry) {
  6050. ret = perf_try_init_event(pmu, event);
  6051. if (!ret)
  6052. goto unlock;
  6053. if (ret != -ENOENT) {
  6054. pmu = ERR_PTR(ret);
  6055. goto unlock;
  6056. }
  6057. }
  6058. pmu = ERR_PTR(-ENOENT);
  6059. unlock:
  6060. srcu_read_unlock(&pmus_srcu, idx);
  6061. return pmu;
  6062. }
  6063. static void account_event_cpu(struct perf_event *event, int cpu)
  6064. {
  6065. if (event->parent)
  6066. return;
  6067. if (is_cgroup_event(event))
  6068. atomic_inc(&per_cpu(perf_cgroup_events, cpu));
  6069. }
  6070. static void account_event(struct perf_event *event)
  6071. {
  6072. if (event->parent)
  6073. return;
  6074. if (event->attach_state & PERF_ATTACH_TASK)
  6075. static_key_slow_inc(&perf_sched_events.key);
  6076. if (event->attr.mmap || event->attr.mmap_data)
  6077. atomic_inc(&nr_mmap_events);
  6078. if (event->attr.comm)
  6079. atomic_inc(&nr_comm_events);
  6080. if (event->attr.task)
  6081. atomic_inc(&nr_task_events);
  6082. if (event->attr.freq) {
  6083. if (atomic_inc_return(&nr_freq_events) == 1)
  6084. tick_nohz_full_kick_all();
  6085. }
  6086. if (has_branch_stack(event))
  6087. static_key_slow_inc(&perf_sched_events.key);
  6088. if (is_cgroup_event(event))
  6089. static_key_slow_inc(&perf_sched_events.key);
  6090. account_event_cpu(event, event->cpu);
  6091. }
  6092. /*
  6093. * Allocate and initialize a event structure
  6094. */
  6095. static struct perf_event *
  6096. perf_event_alloc(struct perf_event_attr *attr, int cpu,
  6097. struct task_struct *task,
  6098. struct perf_event *group_leader,
  6099. struct perf_event *parent_event,
  6100. perf_overflow_handler_t overflow_handler,
  6101. void *context, int cgroup_fd)
  6102. {
  6103. struct pmu *pmu;
  6104. struct perf_event *event;
  6105. struct hw_perf_event *hwc;
  6106. long err = -EINVAL;
  6107. if ((unsigned)cpu >= nr_cpu_ids) {
  6108. if (!task || cpu != -1)
  6109. return ERR_PTR(-EINVAL);
  6110. }
  6111. event = kzalloc(sizeof(*event), GFP_KERNEL);
  6112. if (!event)
  6113. return ERR_PTR(-ENOMEM);
  6114. /*
  6115. * Single events are their own group leaders, with an
  6116. * empty sibling list:
  6117. */
  6118. if (!group_leader)
  6119. group_leader = event;
  6120. mutex_init(&event->child_mutex);
  6121. INIT_LIST_HEAD(&event->child_list);
  6122. INIT_LIST_HEAD(&event->group_entry);
  6123. INIT_LIST_HEAD(&event->event_entry);
  6124. INIT_LIST_HEAD(&event->sibling_list);
  6125. INIT_LIST_HEAD(&event->rb_entry);
  6126. INIT_LIST_HEAD(&event->active_entry);
  6127. INIT_HLIST_NODE(&event->hlist_entry);
  6128. init_waitqueue_head(&event->waitq);
  6129. init_irq_work(&event->pending, perf_pending_event);
  6130. mutex_init(&event->mmap_mutex);
  6131. atomic_long_set(&event->refcount, 1);
  6132. event->cpu = cpu;
  6133. event->attr = *attr;
  6134. event->group_leader = group_leader;
  6135. event->pmu = NULL;
  6136. event->oncpu = -1;
  6137. event->parent = parent_event;
  6138. event->ns = get_pid_ns(task_active_pid_ns(current));
  6139. event->id = atomic64_inc_return(&perf_event_id);
  6140. event->state = PERF_EVENT_STATE_INACTIVE;
  6141. if (task) {
  6142. event->attach_state = PERF_ATTACH_TASK;
  6143. /*
  6144. * XXX pmu::event_init needs to know what task to account to
  6145. * and we cannot use the ctx information because we need the
  6146. * pmu before we get a ctx.
  6147. */
  6148. event->hw.target = task;
  6149. }
  6150. event->clock = &local_clock;
  6151. if (parent_event)
  6152. event->clock = parent_event->clock;
  6153. if (!overflow_handler && parent_event) {
  6154. overflow_handler = parent_event->overflow_handler;
  6155. context = parent_event->overflow_handler_context;
  6156. }
  6157. event->overflow_handler = overflow_handler;
  6158. event->overflow_handler_context = context;
  6159. perf_event__state_init(event);
  6160. pmu = NULL;
  6161. hwc = &event->hw;
  6162. hwc->sample_period = attr->sample_period;
  6163. if (attr->freq && attr->sample_freq)
  6164. hwc->sample_period = 1;
  6165. hwc->last_period = hwc->sample_period;
  6166. local64_set(&hwc->period_left, hwc->sample_period);
  6167. /*
  6168. * we currently do not support PERF_FORMAT_GROUP on inherited events
  6169. */
  6170. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  6171. goto err_ns;
  6172. if (!has_branch_stack(event))
  6173. event->attr.branch_sample_type = 0;
  6174. if (cgroup_fd != -1) {
  6175. err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
  6176. if (err)
  6177. goto err_ns;
  6178. }
  6179. pmu = perf_init_event(event);
  6180. if (!pmu)
  6181. goto err_ns;
  6182. else if (IS_ERR(pmu)) {
  6183. err = PTR_ERR(pmu);
  6184. goto err_ns;
  6185. }
  6186. err = exclusive_event_init(event);
  6187. if (err)
  6188. goto err_pmu;
  6189. if (!event->parent) {
  6190. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  6191. err = get_callchain_buffers();
  6192. if (err)
  6193. goto err_per_task;
  6194. }
  6195. }
  6196. return event;
  6197. err_per_task:
  6198. exclusive_event_destroy(event);
  6199. err_pmu:
  6200. if (event->destroy)
  6201. event->destroy(event);
  6202. module_put(pmu->module);
  6203. err_ns:
  6204. if (is_cgroup_event(event))
  6205. perf_detach_cgroup(event);
  6206. if (event->ns)
  6207. put_pid_ns(event->ns);
  6208. kfree(event);
  6209. return ERR_PTR(err);
  6210. }
  6211. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  6212. struct perf_event_attr *attr)
  6213. {
  6214. u32 size;
  6215. int ret;
  6216. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  6217. return -EFAULT;
  6218. /*
  6219. * zero the full structure, so that a short copy will be nice.
  6220. */
  6221. memset(attr, 0, sizeof(*attr));
  6222. ret = get_user(size, &uattr->size);
  6223. if (ret)
  6224. return ret;
  6225. if (size > PAGE_SIZE) /* silly large */
  6226. goto err_size;
  6227. if (!size) /* abi compat */
  6228. size = PERF_ATTR_SIZE_VER0;
  6229. if (size < PERF_ATTR_SIZE_VER0)
  6230. goto err_size;
  6231. /*
  6232. * If we're handed a bigger struct than we know of,
  6233. * ensure all the unknown bits are 0 - i.e. new
  6234. * user-space does not rely on any kernel feature
  6235. * extensions we dont know about yet.
  6236. */
  6237. if (size > sizeof(*attr)) {
  6238. unsigned char __user *addr;
  6239. unsigned char __user *end;
  6240. unsigned char val;
  6241. addr = (void __user *)uattr + sizeof(*attr);
  6242. end = (void __user *)uattr + size;
  6243. for (; addr < end; addr++) {
  6244. ret = get_user(val, addr);
  6245. if (ret)
  6246. return ret;
  6247. if (val)
  6248. goto err_size;
  6249. }
  6250. size = sizeof(*attr);
  6251. }
  6252. ret = copy_from_user(attr, uattr, size);
  6253. if (ret)
  6254. return -EFAULT;
  6255. if (attr->__reserved_1)
  6256. return -EINVAL;
  6257. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  6258. return -EINVAL;
  6259. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  6260. return -EINVAL;
  6261. if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
  6262. u64 mask = attr->branch_sample_type;
  6263. /* only using defined bits */
  6264. if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
  6265. return -EINVAL;
  6266. /* at least one branch bit must be set */
  6267. if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
  6268. return -EINVAL;
  6269. /* propagate priv level, when not set for branch */
  6270. if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
  6271. /* exclude_kernel checked on syscall entry */
  6272. if (!attr->exclude_kernel)
  6273. mask |= PERF_SAMPLE_BRANCH_KERNEL;
  6274. if (!attr->exclude_user)
  6275. mask |= PERF_SAMPLE_BRANCH_USER;
  6276. if (!attr->exclude_hv)
  6277. mask |= PERF_SAMPLE_BRANCH_HV;
  6278. /*
  6279. * adjust user setting (for HW filter setup)
  6280. */
  6281. attr->branch_sample_type = mask;
  6282. }
  6283. /* privileged levels capture (kernel, hv): check permissions */
  6284. if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
  6285. && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  6286. return -EACCES;
  6287. }
  6288. if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
  6289. ret = perf_reg_validate(attr->sample_regs_user);
  6290. if (ret)
  6291. return ret;
  6292. }
  6293. if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
  6294. if (!arch_perf_have_user_stack_dump())
  6295. return -ENOSYS;
  6296. /*
  6297. * We have __u32 type for the size, but so far
  6298. * we can only use __u16 as maximum due to the
  6299. * __u16 sample size limit.
  6300. */
  6301. if (attr->sample_stack_user >= USHRT_MAX)
  6302. ret = -EINVAL;
  6303. else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
  6304. ret = -EINVAL;
  6305. }
  6306. if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
  6307. ret = perf_reg_validate(attr->sample_regs_intr);
  6308. out:
  6309. return ret;
  6310. err_size:
  6311. put_user(sizeof(*attr), &uattr->size);
  6312. ret = -E2BIG;
  6313. goto out;
  6314. }
  6315. static int
  6316. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  6317. {
  6318. struct ring_buffer *rb = NULL;
  6319. int ret = -EINVAL;
  6320. if (!output_event)
  6321. goto set;
  6322. /* don't allow circular references */
  6323. if (event == output_event)
  6324. goto out;
  6325. /*
  6326. * Don't allow cross-cpu buffers
  6327. */
  6328. if (output_event->cpu != event->cpu)
  6329. goto out;
  6330. /*
  6331. * If its not a per-cpu rb, it must be the same task.
  6332. */
  6333. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  6334. goto out;
  6335. /*
  6336. * Mixing clocks in the same buffer is trouble you don't need.
  6337. */
  6338. if (output_event->clock != event->clock)
  6339. goto out;
  6340. /*
  6341. * If both events generate aux data, they must be on the same PMU
  6342. */
  6343. if (has_aux(event) && has_aux(output_event) &&
  6344. event->pmu != output_event->pmu)
  6345. goto out;
  6346. set:
  6347. mutex_lock(&event->mmap_mutex);
  6348. /* Can't redirect output if we've got an active mmap() */
  6349. if (atomic_read(&event->mmap_count))
  6350. goto unlock;
  6351. if (output_event) {
  6352. /* get the rb we want to redirect to */
  6353. rb = ring_buffer_get(output_event);
  6354. if (!rb)
  6355. goto unlock;
  6356. }
  6357. ring_buffer_attach(event, rb);
  6358. ret = 0;
  6359. unlock:
  6360. mutex_unlock(&event->mmap_mutex);
  6361. out:
  6362. return ret;
  6363. }
  6364. static void mutex_lock_double(struct mutex *a, struct mutex *b)
  6365. {
  6366. if (b < a)
  6367. swap(a, b);
  6368. mutex_lock(a);
  6369. mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
  6370. }
  6371. static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
  6372. {
  6373. bool nmi_safe = false;
  6374. switch (clk_id) {
  6375. case CLOCK_MONOTONIC:
  6376. event->clock = &ktime_get_mono_fast_ns;
  6377. nmi_safe = true;
  6378. break;
  6379. case CLOCK_MONOTONIC_RAW:
  6380. event->clock = &ktime_get_raw_fast_ns;
  6381. nmi_safe = true;
  6382. break;
  6383. case CLOCK_REALTIME:
  6384. event->clock = &ktime_get_real_ns;
  6385. break;
  6386. case CLOCK_BOOTTIME:
  6387. event->clock = &ktime_get_boot_ns;
  6388. break;
  6389. case CLOCK_TAI:
  6390. event->clock = &ktime_get_tai_ns;
  6391. break;
  6392. default:
  6393. return -EINVAL;
  6394. }
  6395. if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
  6396. return -EINVAL;
  6397. return 0;
  6398. }
  6399. /**
  6400. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  6401. *
  6402. * @attr_uptr: event_id type attributes for monitoring/sampling
  6403. * @pid: target pid
  6404. * @cpu: target cpu
  6405. * @group_fd: group leader event fd
  6406. */
  6407. SYSCALL_DEFINE5(perf_event_open,
  6408. struct perf_event_attr __user *, attr_uptr,
  6409. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  6410. {
  6411. struct perf_event *group_leader = NULL, *output_event = NULL;
  6412. struct perf_event *event, *sibling;
  6413. struct perf_event_attr attr;
  6414. struct perf_event_context *ctx, *uninitialized_var(gctx);
  6415. struct file *event_file = NULL;
  6416. struct fd group = {NULL, 0};
  6417. struct task_struct *task = NULL;
  6418. struct pmu *pmu;
  6419. int event_fd;
  6420. int move_group = 0;
  6421. int err;
  6422. int f_flags = O_RDWR;
  6423. int cgroup_fd = -1;
  6424. /* for future expandability... */
  6425. if (flags & ~PERF_FLAG_ALL)
  6426. return -EINVAL;
  6427. err = perf_copy_attr(attr_uptr, &attr);
  6428. if (err)
  6429. return err;
  6430. if (!attr.exclude_kernel) {
  6431. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  6432. return -EACCES;
  6433. }
  6434. if (attr.freq) {
  6435. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  6436. return -EINVAL;
  6437. } else {
  6438. if (attr.sample_period & (1ULL << 63))
  6439. return -EINVAL;
  6440. }
  6441. /*
  6442. * In cgroup mode, the pid argument is used to pass the fd
  6443. * opened to the cgroup directory in cgroupfs. The cpu argument
  6444. * designates the cpu on which to monitor threads from that
  6445. * cgroup.
  6446. */
  6447. if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
  6448. return -EINVAL;
  6449. if (flags & PERF_FLAG_FD_CLOEXEC)
  6450. f_flags |= O_CLOEXEC;
  6451. event_fd = get_unused_fd_flags(f_flags);
  6452. if (event_fd < 0)
  6453. return event_fd;
  6454. if (group_fd != -1) {
  6455. err = perf_fget_light(group_fd, &group);
  6456. if (err)
  6457. goto err_fd;
  6458. group_leader = group.file->private_data;
  6459. if (flags & PERF_FLAG_FD_OUTPUT)
  6460. output_event = group_leader;
  6461. if (flags & PERF_FLAG_FD_NO_GROUP)
  6462. group_leader = NULL;
  6463. }
  6464. if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
  6465. task = find_lively_task_by_vpid(pid);
  6466. if (IS_ERR(task)) {
  6467. err = PTR_ERR(task);
  6468. goto err_group_fd;
  6469. }
  6470. }
  6471. if (task && group_leader &&
  6472. group_leader->attr.inherit != attr.inherit) {
  6473. err = -EINVAL;
  6474. goto err_task;
  6475. }
  6476. get_online_cpus();
  6477. if (flags & PERF_FLAG_PID_CGROUP)
  6478. cgroup_fd = pid;
  6479. event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
  6480. NULL, NULL, cgroup_fd);
  6481. if (IS_ERR(event)) {
  6482. err = PTR_ERR(event);
  6483. goto err_cpus;
  6484. }
  6485. if (is_sampling_event(event)) {
  6486. if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
  6487. err = -ENOTSUPP;
  6488. goto err_alloc;
  6489. }
  6490. }
  6491. account_event(event);
  6492. /*
  6493. * Special case software events and allow them to be part of
  6494. * any hardware group.
  6495. */
  6496. pmu = event->pmu;
  6497. if (attr.use_clockid) {
  6498. err = perf_event_set_clock(event, attr.clockid);
  6499. if (err)
  6500. goto err_alloc;
  6501. }
  6502. if (group_leader &&
  6503. (is_software_event(event) != is_software_event(group_leader))) {
  6504. if (is_software_event(event)) {
  6505. /*
  6506. * If event and group_leader are not both a software
  6507. * event, and event is, then group leader is not.
  6508. *
  6509. * Allow the addition of software events to !software
  6510. * groups, this is safe because software events never
  6511. * fail to schedule.
  6512. */
  6513. pmu = group_leader->pmu;
  6514. } else if (is_software_event(group_leader) &&
  6515. (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  6516. /*
  6517. * In case the group is a pure software group, and we
  6518. * try to add a hardware event, move the whole group to
  6519. * the hardware context.
  6520. */
  6521. move_group = 1;
  6522. }
  6523. }
  6524. /*
  6525. * Get the target context (task or percpu):
  6526. */
  6527. ctx = find_get_context(pmu, task, event);
  6528. if (IS_ERR(ctx)) {
  6529. err = PTR_ERR(ctx);
  6530. goto err_alloc;
  6531. }
  6532. if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
  6533. err = -EBUSY;
  6534. goto err_context;
  6535. }
  6536. if (task) {
  6537. put_task_struct(task);
  6538. task = NULL;
  6539. }
  6540. /*
  6541. * Look up the group leader (we will attach this event to it):
  6542. */
  6543. if (group_leader) {
  6544. err = -EINVAL;
  6545. /*
  6546. * Do not allow a recursive hierarchy (this new sibling
  6547. * becoming part of another group-sibling):
  6548. */
  6549. if (group_leader->group_leader != group_leader)
  6550. goto err_context;
  6551. /* All events in a group should have the same clock */
  6552. if (group_leader->clock != event->clock)
  6553. goto err_context;
  6554. /*
  6555. * Do not allow to attach to a group in a different
  6556. * task or CPU context:
  6557. */
  6558. if (move_group) {
  6559. /*
  6560. * Make sure we're both on the same task, or both
  6561. * per-cpu events.
  6562. */
  6563. if (group_leader->ctx->task != ctx->task)
  6564. goto err_context;
  6565. /*
  6566. * Make sure we're both events for the same CPU;
  6567. * grouping events for different CPUs is broken; since
  6568. * you can never concurrently schedule them anyhow.
  6569. */
  6570. if (group_leader->cpu != event->cpu)
  6571. goto err_context;
  6572. } else {
  6573. if (group_leader->ctx != ctx)
  6574. goto err_context;
  6575. }
  6576. /*
  6577. * Only a group leader can be exclusive or pinned
  6578. */
  6579. if (attr.exclusive || attr.pinned)
  6580. goto err_context;
  6581. }
  6582. if (output_event) {
  6583. err = perf_event_set_output(event, output_event);
  6584. if (err)
  6585. goto err_context;
  6586. }
  6587. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
  6588. f_flags);
  6589. if (IS_ERR(event_file)) {
  6590. err = PTR_ERR(event_file);
  6591. goto err_context;
  6592. }
  6593. if (move_group) {
  6594. gctx = group_leader->ctx;
  6595. /*
  6596. * See perf_event_ctx_lock() for comments on the details
  6597. * of swizzling perf_event::ctx.
  6598. */
  6599. mutex_lock_double(&gctx->mutex, &ctx->mutex);
  6600. perf_remove_from_context(group_leader, false);
  6601. list_for_each_entry(sibling, &group_leader->sibling_list,
  6602. group_entry) {
  6603. perf_remove_from_context(sibling, false);
  6604. put_ctx(gctx);
  6605. }
  6606. } else {
  6607. mutex_lock(&ctx->mutex);
  6608. }
  6609. WARN_ON_ONCE(ctx->parent_ctx);
  6610. if (move_group) {
  6611. /*
  6612. * Wait for everybody to stop referencing the events through
  6613. * the old lists, before installing it on new lists.
  6614. */
  6615. synchronize_rcu();
  6616. /*
  6617. * Install the group siblings before the group leader.
  6618. *
  6619. * Because a group leader will try and install the entire group
  6620. * (through the sibling list, which is still in-tact), we can
  6621. * end up with siblings installed in the wrong context.
  6622. *
  6623. * By installing siblings first we NO-OP because they're not
  6624. * reachable through the group lists.
  6625. */
  6626. list_for_each_entry(sibling, &group_leader->sibling_list,
  6627. group_entry) {
  6628. perf_event__state_init(sibling);
  6629. perf_install_in_context(ctx, sibling, sibling->cpu);
  6630. get_ctx(ctx);
  6631. }
  6632. /*
  6633. * Removing from the context ends up with disabled
  6634. * event. What we want here is event in the initial
  6635. * startup state, ready to be add into new context.
  6636. */
  6637. perf_event__state_init(group_leader);
  6638. perf_install_in_context(ctx, group_leader, group_leader->cpu);
  6639. get_ctx(ctx);
  6640. }
  6641. if (!exclusive_event_installable(event, ctx)) {
  6642. err = -EBUSY;
  6643. mutex_unlock(&ctx->mutex);
  6644. fput(event_file);
  6645. goto err_context;
  6646. }
  6647. perf_install_in_context(ctx, event, event->cpu);
  6648. perf_unpin_context(ctx);
  6649. if (move_group) {
  6650. mutex_unlock(&gctx->mutex);
  6651. put_ctx(gctx);
  6652. }
  6653. mutex_unlock(&ctx->mutex);
  6654. put_online_cpus();
  6655. event->owner = current;
  6656. mutex_lock(&current->perf_event_mutex);
  6657. list_add_tail(&event->owner_entry, &current->perf_event_list);
  6658. mutex_unlock(&current->perf_event_mutex);
  6659. /*
  6660. * Precalculate sample_data sizes
  6661. */
  6662. perf_event__header_size(event);
  6663. perf_event__id_header_size(event);
  6664. /*
  6665. * Drop the reference on the group_event after placing the
  6666. * new event on the sibling_list. This ensures destruction
  6667. * of the group leader will find the pointer to itself in
  6668. * perf_group_detach().
  6669. */
  6670. fdput(group);
  6671. fd_install(event_fd, event_file);
  6672. return event_fd;
  6673. err_context:
  6674. perf_unpin_context(ctx);
  6675. put_ctx(ctx);
  6676. err_alloc:
  6677. free_event(event);
  6678. err_cpus:
  6679. put_online_cpus();
  6680. err_task:
  6681. if (task)
  6682. put_task_struct(task);
  6683. err_group_fd:
  6684. fdput(group);
  6685. err_fd:
  6686. put_unused_fd(event_fd);
  6687. return err;
  6688. }
  6689. /**
  6690. * perf_event_create_kernel_counter
  6691. *
  6692. * @attr: attributes of the counter to create
  6693. * @cpu: cpu in which the counter is bound
  6694. * @task: task to profile (NULL for percpu)
  6695. */
  6696. struct perf_event *
  6697. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  6698. struct task_struct *task,
  6699. perf_overflow_handler_t overflow_handler,
  6700. void *context)
  6701. {
  6702. struct perf_event_context *ctx;
  6703. struct perf_event *event;
  6704. int err;
  6705. /*
  6706. * Get the target context (task or percpu):
  6707. */
  6708. event = perf_event_alloc(attr, cpu, task, NULL, NULL,
  6709. overflow_handler, context, -1);
  6710. if (IS_ERR(event)) {
  6711. err = PTR_ERR(event);
  6712. goto err;
  6713. }
  6714. /* Mark owner so we could distinguish it from user events. */
  6715. event->owner = EVENT_OWNER_KERNEL;
  6716. account_event(event);
  6717. ctx = find_get_context(event->pmu, task, event);
  6718. if (IS_ERR(ctx)) {
  6719. err = PTR_ERR(ctx);
  6720. goto err_free;
  6721. }
  6722. WARN_ON_ONCE(ctx->parent_ctx);
  6723. mutex_lock(&ctx->mutex);
  6724. if (!exclusive_event_installable(event, ctx)) {
  6725. mutex_unlock(&ctx->mutex);
  6726. perf_unpin_context(ctx);
  6727. put_ctx(ctx);
  6728. err = -EBUSY;
  6729. goto err_free;
  6730. }
  6731. perf_install_in_context(ctx, event, cpu);
  6732. perf_unpin_context(ctx);
  6733. mutex_unlock(&ctx->mutex);
  6734. return event;
  6735. err_free:
  6736. free_event(event);
  6737. err:
  6738. return ERR_PTR(err);
  6739. }
  6740. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  6741. void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
  6742. {
  6743. struct perf_event_context *src_ctx;
  6744. struct perf_event_context *dst_ctx;
  6745. struct perf_event *event, *tmp;
  6746. LIST_HEAD(events);
  6747. src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
  6748. dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
  6749. /*
  6750. * See perf_event_ctx_lock() for comments on the details
  6751. * of swizzling perf_event::ctx.
  6752. */
  6753. mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
  6754. list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
  6755. event_entry) {
  6756. perf_remove_from_context(event, false);
  6757. unaccount_event_cpu(event, src_cpu);
  6758. put_ctx(src_ctx);
  6759. list_add(&event->migrate_entry, &events);
  6760. }
  6761. /*
  6762. * Wait for the events to quiesce before re-instating them.
  6763. */
  6764. synchronize_rcu();
  6765. /*
  6766. * Re-instate events in 2 passes.
  6767. *
  6768. * Skip over group leaders and only install siblings on this first
  6769. * pass, siblings will not get enabled without a leader, however a
  6770. * leader will enable its siblings, even if those are still on the old
  6771. * context.
  6772. */
  6773. list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
  6774. if (event->group_leader == event)
  6775. continue;
  6776. list_del(&event->migrate_entry);
  6777. if (event->state >= PERF_EVENT_STATE_OFF)
  6778. event->state = PERF_EVENT_STATE_INACTIVE;
  6779. account_event_cpu(event, dst_cpu);
  6780. perf_install_in_context(dst_ctx, event, dst_cpu);
  6781. get_ctx(dst_ctx);
  6782. }
  6783. /*
  6784. * Once all the siblings are setup properly, install the group leaders
  6785. * to make it go.
  6786. */
  6787. list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
  6788. list_del(&event->migrate_entry);
  6789. if (event->state >= PERF_EVENT_STATE_OFF)
  6790. event->state = PERF_EVENT_STATE_INACTIVE;
  6791. account_event_cpu(event, dst_cpu);
  6792. perf_install_in_context(dst_ctx, event, dst_cpu);
  6793. get_ctx(dst_ctx);
  6794. }
  6795. mutex_unlock(&dst_ctx->mutex);
  6796. mutex_unlock(&src_ctx->mutex);
  6797. }
  6798. EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
  6799. static void sync_child_event(struct perf_event *child_event,
  6800. struct task_struct *child)
  6801. {
  6802. struct perf_event *parent_event = child_event->parent;
  6803. u64 child_val;
  6804. if (child_event->attr.inherit_stat)
  6805. perf_event_read_event(child_event, child);
  6806. child_val = perf_event_count(child_event);
  6807. /*
  6808. * Add back the child's count to the parent's count:
  6809. */
  6810. atomic64_add(child_val, &parent_event->child_count);
  6811. atomic64_add(child_event->total_time_enabled,
  6812. &parent_event->child_total_time_enabled);
  6813. atomic64_add(child_event->total_time_running,
  6814. &parent_event->child_total_time_running);
  6815. /*
  6816. * Remove this event from the parent's list
  6817. */
  6818. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  6819. mutex_lock(&parent_event->child_mutex);
  6820. list_del_init(&child_event->child_list);
  6821. mutex_unlock(&parent_event->child_mutex);
  6822. /*
  6823. * Make sure user/parent get notified, that we just
  6824. * lost one event.
  6825. */
  6826. perf_event_wakeup(parent_event);
  6827. /*
  6828. * Release the parent event, if this was the last
  6829. * reference to it.
  6830. */
  6831. put_event(parent_event);
  6832. }
  6833. static void
  6834. __perf_event_exit_task(struct perf_event *child_event,
  6835. struct perf_event_context *child_ctx,
  6836. struct task_struct *child)
  6837. {
  6838. /*
  6839. * Do not destroy the 'original' grouping; because of the context
  6840. * switch optimization the original events could've ended up in a
  6841. * random child task.
  6842. *
  6843. * If we were to destroy the original group, all group related
  6844. * operations would cease to function properly after this random
  6845. * child dies.
  6846. *
  6847. * Do destroy all inherited groups, we don't care about those
  6848. * and being thorough is better.
  6849. */
  6850. perf_remove_from_context(child_event, !!child_event->parent);
  6851. /*
  6852. * It can happen that the parent exits first, and has events
  6853. * that are still around due to the child reference. These
  6854. * events need to be zapped.
  6855. */
  6856. if (child_event->parent) {
  6857. sync_child_event(child_event, child);
  6858. free_event(child_event);
  6859. } else {
  6860. child_event->state = PERF_EVENT_STATE_EXIT;
  6861. perf_event_wakeup(child_event);
  6862. }
  6863. }
  6864. static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
  6865. {
  6866. struct perf_event *child_event, *next;
  6867. struct perf_event_context *child_ctx, *clone_ctx = NULL;
  6868. unsigned long flags;
  6869. if (likely(!child->perf_event_ctxp[ctxn])) {
  6870. perf_event_task(child, NULL, 0);
  6871. return;
  6872. }
  6873. local_irq_save(flags);
  6874. /*
  6875. * We can't reschedule here because interrupts are disabled,
  6876. * and either child is current or it is a task that can't be
  6877. * scheduled, so we are now safe from rescheduling changing
  6878. * our context.
  6879. */
  6880. child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
  6881. /*
  6882. * Take the context lock here so that if find_get_context is
  6883. * reading child->perf_event_ctxp, we wait until it has
  6884. * incremented the context's refcount before we do put_ctx below.
  6885. */
  6886. raw_spin_lock(&child_ctx->lock);
  6887. task_ctx_sched_out(child_ctx);
  6888. child->perf_event_ctxp[ctxn] = NULL;
  6889. /*
  6890. * If this context is a clone; unclone it so it can't get
  6891. * swapped to another process while we're removing all
  6892. * the events from it.
  6893. */
  6894. clone_ctx = unclone_ctx(child_ctx);
  6895. update_context_time(child_ctx);
  6896. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  6897. if (clone_ctx)
  6898. put_ctx(clone_ctx);
  6899. /*
  6900. * Report the task dead after unscheduling the events so that we
  6901. * won't get any samples after PERF_RECORD_EXIT. We can however still
  6902. * get a few PERF_RECORD_READ events.
  6903. */
  6904. perf_event_task(child, child_ctx, 0);
  6905. /*
  6906. * We can recurse on the same lock type through:
  6907. *
  6908. * __perf_event_exit_task()
  6909. * sync_child_event()
  6910. * put_event()
  6911. * mutex_lock(&ctx->mutex)
  6912. *
  6913. * But since its the parent context it won't be the same instance.
  6914. */
  6915. mutex_lock(&child_ctx->mutex);
  6916. list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
  6917. __perf_event_exit_task(child_event, child_ctx, child);
  6918. mutex_unlock(&child_ctx->mutex);
  6919. put_ctx(child_ctx);
  6920. }
  6921. /*
  6922. * When a child task exits, feed back event values to parent events.
  6923. */
  6924. void perf_event_exit_task(struct task_struct *child)
  6925. {
  6926. struct perf_event *event, *tmp;
  6927. int ctxn;
  6928. mutex_lock(&child->perf_event_mutex);
  6929. list_for_each_entry_safe(event, tmp, &child->perf_event_list,
  6930. owner_entry) {
  6931. list_del_init(&event->owner_entry);
  6932. /*
  6933. * Ensure the list deletion is visible before we clear
  6934. * the owner, closes a race against perf_release() where
  6935. * we need to serialize on the owner->perf_event_mutex.
  6936. */
  6937. smp_wmb();
  6938. event->owner = NULL;
  6939. }
  6940. mutex_unlock(&child->perf_event_mutex);
  6941. for_each_task_context_nr(ctxn)
  6942. perf_event_exit_task_context(child, ctxn);
  6943. }
  6944. static void perf_free_event(struct perf_event *event,
  6945. struct perf_event_context *ctx)
  6946. {
  6947. struct perf_event *parent = event->parent;
  6948. if (WARN_ON_ONCE(!parent))
  6949. return;
  6950. mutex_lock(&parent->child_mutex);
  6951. list_del_init(&event->child_list);
  6952. mutex_unlock(&parent->child_mutex);
  6953. put_event(parent);
  6954. raw_spin_lock_irq(&ctx->lock);
  6955. perf_group_detach(event);
  6956. list_del_event(event, ctx);
  6957. raw_spin_unlock_irq(&ctx->lock);
  6958. free_event(event);
  6959. }
  6960. /*
  6961. * Free an unexposed, unused context as created by inheritance by
  6962. * perf_event_init_task below, used by fork() in case of fail.
  6963. *
  6964. * Not all locks are strictly required, but take them anyway to be nice and
  6965. * help out with the lockdep assertions.
  6966. */
  6967. void perf_event_free_task(struct task_struct *task)
  6968. {
  6969. struct perf_event_context *ctx;
  6970. struct perf_event *event, *tmp;
  6971. int ctxn;
  6972. for_each_task_context_nr(ctxn) {
  6973. ctx = task->perf_event_ctxp[ctxn];
  6974. if (!ctx)
  6975. continue;
  6976. mutex_lock(&ctx->mutex);
  6977. again:
  6978. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
  6979. group_entry)
  6980. perf_free_event(event, ctx);
  6981. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  6982. group_entry)
  6983. perf_free_event(event, ctx);
  6984. if (!list_empty(&ctx->pinned_groups) ||
  6985. !list_empty(&ctx->flexible_groups))
  6986. goto again;
  6987. mutex_unlock(&ctx->mutex);
  6988. put_ctx(ctx);
  6989. }
  6990. }
  6991. void perf_event_delayed_put(struct task_struct *task)
  6992. {
  6993. int ctxn;
  6994. for_each_task_context_nr(ctxn)
  6995. WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  6996. }
  6997. /*
  6998. * inherit a event from parent task to child task:
  6999. */
  7000. static struct perf_event *
  7001. inherit_event(struct perf_event *parent_event,
  7002. struct task_struct *parent,
  7003. struct perf_event_context *parent_ctx,
  7004. struct task_struct *child,
  7005. struct perf_event *group_leader,
  7006. struct perf_event_context *child_ctx)
  7007. {
  7008. enum perf_event_active_state parent_state = parent_event->state;
  7009. struct perf_event *child_event;
  7010. unsigned long flags;
  7011. /*
  7012. * Instead of creating recursive hierarchies of events,
  7013. * we link inherited events back to the original parent,
  7014. * which has a filp for sure, which we use as the reference
  7015. * count:
  7016. */
  7017. if (parent_event->parent)
  7018. parent_event = parent_event->parent;
  7019. child_event = perf_event_alloc(&parent_event->attr,
  7020. parent_event->cpu,
  7021. child,
  7022. group_leader, parent_event,
  7023. NULL, NULL, -1);
  7024. if (IS_ERR(child_event))
  7025. return child_event;
  7026. if (is_orphaned_event(parent_event) ||
  7027. !atomic_long_inc_not_zero(&parent_event->refcount)) {
  7028. free_event(child_event);
  7029. return NULL;
  7030. }
  7031. get_ctx(child_ctx);
  7032. /*
  7033. * Make the child state follow the state of the parent event,
  7034. * not its attr.disabled bit. We hold the parent's mutex,
  7035. * so we won't race with perf_event_{en, dis}able_family.
  7036. */
  7037. if (parent_state >= PERF_EVENT_STATE_INACTIVE)
  7038. child_event->state = PERF_EVENT_STATE_INACTIVE;
  7039. else
  7040. child_event->state = PERF_EVENT_STATE_OFF;
  7041. if (parent_event->attr.freq) {
  7042. u64 sample_period = parent_event->hw.sample_period;
  7043. struct hw_perf_event *hwc = &child_event->hw;
  7044. hwc->sample_period = sample_period;
  7045. hwc->last_period = sample_period;
  7046. local64_set(&hwc->period_left, sample_period);
  7047. }
  7048. child_event->ctx = child_ctx;
  7049. child_event->overflow_handler = parent_event->overflow_handler;
  7050. child_event->overflow_handler_context
  7051. = parent_event->overflow_handler_context;
  7052. /*
  7053. * Precalculate sample_data sizes
  7054. */
  7055. perf_event__header_size(child_event);
  7056. perf_event__id_header_size(child_event);
  7057. /*
  7058. * Link it up in the child's context:
  7059. */
  7060. raw_spin_lock_irqsave(&child_ctx->lock, flags);
  7061. add_event_to_ctx(child_event, child_ctx);
  7062. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  7063. /*
  7064. * Link this into the parent event's child list
  7065. */
  7066. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  7067. mutex_lock(&parent_event->child_mutex);
  7068. list_add_tail(&child_event->child_list, &parent_event->child_list);
  7069. mutex_unlock(&parent_event->child_mutex);
  7070. return child_event;
  7071. }
  7072. static int inherit_group(struct perf_event *parent_event,
  7073. struct task_struct *parent,
  7074. struct perf_event_context *parent_ctx,
  7075. struct task_struct *child,
  7076. struct perf_event_context *child_ctx)
  7077. {
  7078. struct perf_event *leader;
  7079. struct perf_event *sub;
  7080. struct perf_event *child_ctr;
  7081. leader = inherit_event(parent_event, parent, parent_ctx,
  7082. child, NULL, child_ctx);
  7083. if (IS_ERR(leader))
  7084. return PTR_ERR(leader);
  7085. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  7086. child_ctr = inherit_event(sub, parent, parent_ctx,
  7087. child, leader, child_ctx);
  7088. if (IS_ERR(child_ctr))
  7089. return PTR_ERR(child_ctr);
  7090. }
  7091. return 0;
  7092. }
  7093. static int
  7094. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  7095. struct perf_event_context *parent_ctx,
  7096. struct task_struct *child, int ctxn,
  7097. int *inherited_all)
  7098. {
  7099. int ret;
  7100. struct perf_event_context *child_ctx;
  7101. if (!event->attr.inherit) {
  7102. *inherited_all = 0;
  7103. return 0;
  7104. }
  7105. child_ctx = child->perf_event_ctxp[ctxn];
  7106. if (!child_ctx) {
  7107. /*
  7108. * This is executed from the parent task context, so
  7109. * inherit events that have been marked for cloning.
  7110. * First allocate and initialize a context for the
  7111. * child.
  7112. */
  7113. child_ctx = alloc_perf_context(parent_ctx->pmu, child);
  7114. if (!child_ctx)
  7115. return -ENOMEM;
  7116. child->perf_event_ctxp[ctxn] = child_ctx;
  7117. }
  7118. ret = inherit_group(event, parent, parent_ctx,
  7119. child, child_ctx);
  7120. if (ret)
  7121. *inherited_all = 0;
  7122. return ret;
  7123. }
  7124. /*
  7125. * Initialize the perf_event context in task_struct
  7126. */
  7127. static int perf_event_init_context(struct task_struct *child, int ctxn)
  7128. {
  7129. struct perf_event_context *child_ctx, *parent_ctx;
  7130. struct perf_event_context *cloned_ctx;
  7131. struct perf_event *event;
  7132. struct task_struct *parent = current;
  7133. int inherited_all = 1;
  7134. unsigned long flags;
  7135. int ret = 0;
  7136. if (likely(!parent->perf_event_ctxp[ctxn]))
  7137. return 0;
  7138. /*
  7139. * If the parent's context is a clone, pin it so it won't get
  7140. * swapped under us.
  7141. */
  7142. parent_ctx = perf_pin_task_context(parent, ctxn);
  7143. if (!parent_ctx)
  7144. return 0;
  7145. /*
  7146. * No need to check if parent_ctx != NULL here; since we saw
  7147. * it non-NULL earlier, the only reason for it to become NULL
  7148. * is if we exit, and since we're currently in the middle of
  7149. * a fork we can't be exiting at the same time.
  7150. */
  7151. /*
  7152. * Lock the parent list. No need to lock the child - not PID
  7153. * hashed yet and not running, so nobody can access it.
  7154. */
  7155. mutex_lock(&parent_ctx->mutex);
  7156. /*
  7157. * We dont have to disable NMIs - we are only looking at
  7158. * the list, not manipulating it:
  7159. */
  7160. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  7161. ret = inherit_task_group(event, parent, parent_ctx,
  7162. child, ctxn, &inherited_all);
  7163. if (ret)
  7164. break;
  7165. }
  7166. /*
  7167. * We can't hold ctx->lock when iterating the ->flexible_group list due
  7168. * to allocations, but we need to prevent rotation because
  7169. * rotate_ctx() will change the list from interrupt context.
  7170. */
  7171. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  7172. parent_ctx->rotate_disable = 1;
  7173. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  7174. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  7175. ret = inherit_task_group(event, parent, parent_ctx,
  7176. child, ctxn, &inherited_all);
  7177. if (ret)
  7178. break;
  7179. }
  7180. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  7181. parent_ctx->rotate_disable = 0;
  7182. child_ctx = child->perf_event_ctxp[ctxn];
  7183. if (child_ctx && inherited_all) {
  7184. /*
  7185. * Mark the child context as a clone of the parent
  7186. * context, or of whatever the parent is a clone of.
  7187. *
  7188. * Note that if the parent is a clone, the holding of
  7189. * parent_ctx->lock avoids it from being uncloned.
  7190. */
  7191. cloned_ctx = parent_ctx->parent_ctx;
  7192. if (cloned_ctx) {
  7193. child_ctx->parent_ctx = cloned_ctx;
  7194. child_ctx->parent_gen = parent_ctx->parent_gen;
  7195. } else {
  7196. child_ctx->parent_ctx = parent_ctx;
  7197. child_ctx->parent_gen = parent_ctx->generation;
  7198. }
  7199. get_ctx(child_ctx->parent_ctx);
  7200. }
  7201. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  7202. mutex_unlock(&parent_ctx->mutex);
  7203. perf_unpin_context(parent_ctx);
  7204. put_ctx(parent_ctx);
  7205. return ret;
  7206. }
  7207. /*
  7208. * Initialize the perf_event context in task_struct
  7209. */
  7210. int perf_event_init_task(struct task_struct *child)
  7211. {
  7212. int ctxn, ret;
  7213. memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
  7214. mutex_init(&child->perf_event_mutex);
  7215. INIT_LIST_HEAD(&child->perf_event_list);
  7216. for_each_task_context_nr(ctxn) {
  7217. ret = perf_event_init_context(child, ctxn);
  7218. if (ret) {
  7219. perf_event_free_task(child);
  7220. return ret;
  7221. }
  7222. }
  7223. return 0;
  7224. }
  7225. static void __init perf_event_init_all_cpus(void)
  7226. {
  7227. struct swevent_htable *swhash;
  7228. int cpu;
  7229. for_each_possible_cpu(cpu) {
  7230. swhash = &per_cpu(swevent_htable, cpu);
  7231. mutex_init(&swhash->hlist_mutex);
  7232. INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
  7233. }
  7234. }
  7235. static void perf_event_init_cpu(int cpu)
  7236. {
  7237. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  7238. mutex_lock(&swhash->hlist_mutex);
  7239. swhash->online = true;
  7240. if (swhash->hlist_refcount > 0) {
  7241. struct swevent_hlist *hlist;
  7242. hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  7243. WARN_ON(!hlist);
  7244. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  7245. }
  7246. mutex_unlock(&swhash->hlist_mutex);
  7247. }
  7248. #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
  7249. static void __perf_event_exit_context(void *__info)
  7250. {
  7251. struct remove_event re = { .detach_group = true };
  7252. struct perf_event_context *ctx = __info;
  7253. rcu_read_lock();
  7254. list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
  7255. __perf_remove_from_context(&re);
  7256. rcu_read_unlock();
  7257. }
  7258. static void perf_event_exit_cpu_context(int cpu)
  7259. {
  7260. struct perf_event_context *ctx;
  7261. struct pmu *pmu;
  7262. int idx;
  7263. idx = srcu_read_lock(&pmus_srcu);
  7264. list_for_each_entry_rcu(pmu, &pmus, entry) {
  7265. ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
  7266. mutex_lock(&ctx->mutex);
  7267. smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
  7268. mutex_unlock(&ctx->mutex);
  7269. }
  7270. srcu_read_unlock(&pmus_srcu, idx);
  7271. }
  7272. static void perf_event_exit_cpu(int cpu)
  7273. {
  7274. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  7275. perf_event_exit_cpu_context(cpu);
  7276. mutex_lock(&swhash->hlist_mutex);
  7277. swhash->online = false;
  7278. swevent_hlist_release(swhash);
  7279. mutex_unlock(&swhash->hlist_mutex);
  7280. }
  7281. #else
  7282. static inline void perf_event_exit_cpu(int cpu) { }
  7283. #endif
  7284. static int
  7285. perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
  7286. {
  7287. int cpu;
  7288. for_each_online_cpu(cpu)
  7289. perf_event_exit_cpu(cpu);
  7290. return NOTIFY_OK;
  7291. }
  7292. /*
  7293. * Run the perf reboot notifier at the very last possible moment so that
  7294. * the generic watchdog code runs as long as possible.
  7295. */
  7296. static struct notifier_block perf_reboot_notifier = {
  7297. .notifier_call = perf_reboot,
  7298. .priority = INT_MIN,
  7299. };
  7300. static int
  7301. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  7302. {
  7303. unsigned int cpu = (long)hcpu;
  7304. switch (action & ~CPU_TASKS_FROZEN) {
  7305. case CPU_UP_PREPARE:
  7306. case CPU_DOWN_FAILED:
  7307. perf_event_init_cpu(cpu);
  7308. break;
  7309. case CPU_UP_CANCELED:
  7310. case CPU_DOWN_PREPARE:
  7311. perf_event_exit_cpu(cpu);
  7312. break;
  7313. default:
  7314. break;
  7315. }
  7316. return NOTIFY_OK;
  7317. }
  7318. void __init perf_event_init(void)
  7319. {
  7320. int ret;
  7321. idr_init(&pmu_idr);
  7322. perf_event_init_all_cpus();
  7323. init_srcu_struct(&pmus_srcu);
  7324. perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
  7325. perf_pmu_register(&perf_cpu_clock, NULL, -1);
  7326. perf_pmu_register(&perf_task_clock, NULL, -1);
  7327. perf_tp_register();
  7328. perf_cpu_notifier(perf_cpu_notify);
  7329. register_reboot_notifier(&perf_reboot_notifier);
  7330. ret = init_hw_breakpoint();
  7331. WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
  7332. /* do not patch jump label more than once per second */
  7333. jump_label_rate_limit(&perf_sched_events, HZ);
  7334. /*
  7335. * Build time assertion that we keep the data_head at the intended
  7336. * location. IOW, validation we got the __reserved[] size right.
  7337. */
  7338. BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
  7339. != 1024);
  7340. }
  7341. ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
  7342. char *page)
  7343. {
  7344. struct perf_pmu_events_attr *pmu_attr =
  7345. container_of(attr, struct perf_pmu_events_attr, attr);
  7346. if (pmu_attr->event_str)
  7347. return sprintf(page, "%s\n", pmu_attr->event_str);
  7348. return 0;
  7349. }
  7350. static int __init perf_event_sysfs_init(void)
  7351. {
  7352. struct pmu *pmu;
  7353. int ret;
  7354. mutex_lock(&pmus_lock);
  7355. ret = bus_register(&pmu_bus);
  7356. if (ret)
  7357. goto unlock;
  7358. list_for_each_entry(pmu, &pmus, entry) {
  7359. if (!pmu->name || pmu->type < 0)
  7360. continue;
  7361. ret = pmu_dev_alloc(pmu);
  7362. WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
  7363. }
  7364. pmu_bus_running = 1;
  7365. ret = 0;
  7366. unlock:
  7367. mutex_unlock(&pmus_lock);
  7368. return ret;
  7369. }
  7370. device_initcall(perf_event_sysfs_init);
  7371. #ifdef CONFIG_CGROUP_PERF
  7372. static struct cgroup_subsys_state *
  7373. perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
  7374. {
  7375. struct perf_cgroup *jc;
  7376. jc = kzalloc(sizeof(*jc), GFP_KERNEL);
  7377. if (!jc)
  7378. return ERR_PTR(-ENOMEM);
  7379. jc->info = alloc_percpu(struct perf_cgroup_info);
  7380. if (!jc->info) {
  7381. kfree(jc);
  7382. return ERR_PTR(-ENOMEM);
  7383. }
  7384. return &jc->css;
  7385. }
  7386. static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
  7387. {
  7388. struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
  7389. free_percpu(jc->info);
  7390. kfree(jc);
  7391. }
  7392. static int __perf_cgroup_move(void *info)
  7393. {
  7394. struct task_struct *task = info;
  7395. perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
  7396. return 0;
  7397. }
  7398. static void perf_cgroup_attach(struct cgroup_subsys_state *css,
  7399. struct cgroup_taskset *tset)
  7400. {
  7401. struct task_struct *task;
  7402. cgroup_taskset_for_each(task, tset)
  7403. task_function_call(task, __perf_cgroup_move, task);
  7404. }
  7405. static void perf_cgroup_exit(struct cgroup_subsys_state *css,
  7406. struct cgroup_subsys_state *old_css,
  7407. struct task_struct *task)
  7408. {
  7409. /*
  7410. * cgroup_exit() is called in the copy_process() failure path.
  7411. * Ignore this case since the task hasn't ran yet, this avoids
  7412. * trying to poke a half freed task state from generic code.
  7413. */
  7414. if (!(task->flags & PF_EXITING))
  7415. return;
  7416. task_function_call(task, __perf_cgroup_move, task);
  7417. }
  7418. struct cgroup_subsys perf_event_cgrp_subsys = {
  7419. .css_alloc = perf_cgroup_css_alloc,
  7420. .css_free = perf_cgroup_css_free,
  7421. .exit = perf_cgroup_exit,
  7422. .attach = perf_cgroup_attach,
  7423. };
  7424. #endif /* CONFIG_CGROUP_PERF */