core.c 182 KB

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