core.c 186 KB

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