core.c 182 KB

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