perf_event.c 153 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734
  1. /*
  2. * Performance events core code:
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2008-2009 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/sysfs.h>
  21. #include <linux/dcache.h>
  22. #include <linux/percpu.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/reboot.h>
  25. #include <linux/vmstat.h>
  26. #include <linux/device.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/hardirq.h>
  29. #include <linux/rculist.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/syscalls.h>
  32. #include <linux/anon_inodes.h>
  33. #include <linux/kernel_stat.h>
  34. #include <linux/perf_event.h>
  35. #include <linux/ftrace_event.h>
  36. #include <linux/hw_breakpoint.h>
  37. #include <asm/irq_regs.h>
  38. enum event_type_t {
  39. EVENT_FLEXIBLE = 0x1,
  40. EVENT_PINNED = 0x2,
  41. EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
  42. };
  43. atomic_t perf_task_events __read_mostly;
  44. static atomic_t nr_mmap_events __read_mostly;
  45. static atomic_t nr_comm_events __read_mostly;
  46. static atomic_t nr_task_events __read_mostly;
  47. static LIST_HEAD(pmus);
  48. static DEFINE_MUTEX(pmus_lock);
  49. static struct srcu_struct pmus_srcu;
  50. /*
  51. * perf event paranoia level:
  52. * -1 - not paranoid at all
  53. * 0 - disallow raw tracepoint access for unpriv
  54. * 1 - disallow cpu events for unpriv
  55. * 2 - disallow kernel profiling for unpriv
  56. */
  57. int sysctl_perf_event_paranoid __read_mostly = 1;
  58. int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
  59. /*
  60. * max perf event sample rate
  61. */
  62. int sysctl_perf_event_sample_rate __read_mostly = 100000;
  63. static atomic64_t perf_event_id;
  64. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  65. enum event_type_t event_type);
  66. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  67. enum event_type_t event_type);
  68. void __weak perf_event_print_debug(void) { }
  69. extern __weak const char *perf_pmu_name(void)
  70. {
  71. return "pmu";
  72. }
  73. static inline u64 perf_clock(void)
  74. {
  75. return local_clock();
  76. }
  77. void perf_pmu_disable(struct pmu *pmu)
  78. {
  79. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  80. if (!(*count)++)
  81. pmu->pmu_disable(pmu);
  82. }
  83. void perf_pmu_enable(struct pmu *pmu)
  84. {
  85. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  86. if (!--(*count))
  87. pmu->pmu_enable(pmu);
  88. }
  89. static DEFINE_PER_CPU(struct list_head, rotation_list);
  90. /*
  91. * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
  92. * because they're strictly cpu affine and rotate_start is called with IRQs
  93. * disabled, while rotate_context is called from IRQ context.
  94. */
  95. static void perf_pmu_rotate_start(struct pmu *pmu)
  96. {
  97. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  98. struct list_head *head = &__get_cpu_var(rotation_list);
  99. WARN_ON(!irqs_disabled());
  100. if (list_empty(&cpuctx->rotation_list))
  101. list_add(&cpuctx->rotation_list, head);
  102. }
  103. static void get_ctx(struct perf_event_context *ctx)
  104. {
  105. WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
  106. }
  107. static void free_ctx(struct rcu_head *head)
  108. {
  109. struct perf_event_context *ctx;
  110. ctx = container_of(head, struct perf_event_context, rcu_head);
  111. kfree(ctx);
  112. }
  113. static void put_ctx(struct perf_event_context *ctx)
  114. {
  115. if (atomic_dec_and_test(&ctx->refcount)) {
  116. if (ctx->parent_ctx)
  117. put_ctx(ctx->parent_ctx);
  118. if (ctx->task)
  119. put_task_struct(ctx->task);
  120. call_rcu(&ctx->rcu_head, free_ctx);
  121. }
  122. }
  123. static void unclone_ctx(struct perf_event_context *ctx)
  124. {
  125. if (ctx->parent_ctx) {
  126. put_ctx(ctx->parent_ctx);
  127. ctx->parent_ctx = NULL;
  128. }
  129. }
  130. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  131. {
  132. /*
  133. * only top level events have the pid namespace they were created in
  134. */
  135. if (event->parent)
  136. event = event->parent;
  137. return task_tgid_nr_ns(p, event->ns);
  138. }
  139. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  140. {
  141. /*
  142. * only top level events have the pid namespace they were created in
  143. */
  144. if (event->parent)
  145. event = event->parent;
  146. return task_pid_nr_ns(p, event->ns);
  147. }
  148. /*
  149. * If we inherit events we want to return the parent event id
  150. * to userspace.
  151. */
  152. static u64 primary_event_id(struct perf_event *event)
  153. {
  154. u64 id = event->id;
  155. if (event->parent)
  156. id = event->parent->id;
  157. return id;
  158. }
  159. /*
  160. * Get the perf_event_context for a task and lock it.
  161. * This has to cope with with the fact that until it is locked,
  162. * the context could get moved to another task.
  163. */
  164. static struct perf_event_context *
  165. perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
  166. {
  167. struct perf_event_context *ctx;
  168. rcu_read_lock();
  169. retry:
  170. ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
  171. if (ctx) {
  172. /*
  173. * If this context is a clone of another, it might
  174. * get swapped for another underneath us by
  175. * perf_event_task_sched_out, though the
  176. * rcu_read_lock() protects us from any context
  177. * getting freed. Lock the context and check if it
  178. * got swapped before we could get the lock, and retry
  179. * if so. If we locked the right context, then it
  180. * can't get swapped on us any more.
  181. */
  182. raw_spin_lock_irqsave(&ctx->lock, *flags);
  183. if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
  184. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  185. goto retry;
  186. }
  187. if (!atomic_inc_not_zero(&ctx->refcount)) {
  188. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  189. ctx = NULL;
  190. }
  191. }
  192. rcu_read_unlock();
  193. return ctx;
  194. }
  195. /*
  196. * Get the context for a task and increment its pin_count so it
  197. * can't get swapped to another task. This also increments its
  198. * reference count so that the context can't get freed.
  199. */
  200. static struct perf_event_context *
  201. perf_pin_task_context(struct task_struct *task, int ctxn)
  202. {
  203. struct perf_event_context *ctx;
  204. unsigned long flags;
  205. ctx = perf_lock_task_context(task, ctxn, &flags);
  206. if (ctx) {
  207. ++ctx->pin_count;
  208. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  209. }
  210. return ctx;
  211. }
  212. static void perf_unpin_context(struct perf_event_context *ctx)
  213. {
  214. unsigned long flags;
  215. raw_spin_lock_irqsave(&ctx->lock, flags);
  216. --ctx->pin_count;
  217. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  218. put_ctx(ctx);
  219. }
  220. /*
  221. * Update the record of the current time in a context.
  222. */
  223. static void update_context_time(struct perf_event_context *ctx)
  224. {
  225. u64 now = perf_clock();
  226. ctx->time += now - ctx->timestamp;
  227. ctx->timestamp = now;
  228. }
  229. static u64 perf_event_time(struct perf_event *event)
  230. {
  231. struct perf_event_context *ctx = event->ctx;
  232. return ctx ? ctx->time : 0;
  233. }
  234. /*
  235. * Update the total_time_enabled and total_time_running fields for a event.
  236. */
  237. static void update_event_times(struct perf_event *event)
  238. {
  239. struct perf_event_context *ctx = event->ctx;
  240. u64 run_end;
  241. if (event->state < PERF_EVENT_STATE_INACTIVE ||
  242. event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
  243. return;
  244. if (ctx->is_active)
  245. run_end = perf_event_time(event);
  246. else
  247. run_end = event->tstamp_stopped;
  248. event->total_time_enabled = run_end - event->tstamp_enabled;
  249. if (event->state == PERF_EVENT_STATE_INACTIVE)
  250. run_end = event->tstamp_stopped;
  251. else
  252. run_end = perf_event_time(event);
  253. event->total_time_running = run_end - event->tstamp_running;
  254. }
  255. /*
  256. * Update total_time_enabled and total_time_running for all events in a group.
  257. */
  258. static void update_group_times(struct perf_event *leader)
  259. {
  260. struct perf_event *event;
  261. update_event_times(leader);
  262. list_for_each_entry(event, &leader->sibling_list, group_entry)
  263. update_event_times(event);
  264. }
  265. static struct list_head *
  266. ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
  267. {
  268. if (event->attr.pinned)
  269. return &ctx->pinned_groups;
  270. else
  271. return &ctx->flexible_groups;
  272. }
  273. /*
  274. * Add a event from the lists for its context.
  275. * Must be called with ctx->mutex and ctx->lock held.
  276. */
  277. static void
  278. list_add_event(struct perf_event *event, struct perf_event_context *ctx)
  279. {
  280. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
  281. event->attach_state |= PERF_ATTACH_CONTEXT;
  282. /*
  283. * If we're a stand alone event or group leader, we go to the context
  284. * list, group events are kept attached to the group so that
  285. * perf_group_detach can, at all times, locate all siblings.
  286. */
  287. if (event->group_leader == event) {
  288. struct list_head *list;
  289. if (is_software_event(event))
  290. event->group_flags |= PERF_GROUP_SOFTWARE;
  291. list = ctx_group_list(event, ctx);
  292. list_add_tail(&event->group_entry, list);
  293. }
  294. list_add_rcu(&event->event_entry, &ctx->event_list);
  295. if (!ctx->nr_events)
  296. perf_pmu_rotate_start(ctx->pmu);
  297. ctx->nr_events++;
  298. if (event->attr.inherit_stat)
  299. ctx->nr_stat++;
  300. }
  301. /*
  302. * Called at perf_event creation and when events are attached/detached from a
  303. * group.
  304. */
  305. static void perf_event__read_size(struct perf_event *event)
  306. {
  307. int entry = sizeof(u64); /* value */
  308. int size = 0;
  309. int nr = 1;
  310. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  311. size += sizeof(u64);
  312. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  313. size += sizeof(u64);
  314. if (event->attr.read_format & PERF_FORMAT_ID)
  315. entry += sizeof(u64);
  316. if (event->attr.read_format & PERF_FORMAT_GROUP) {
  317. nr += event->group_leader->nr_siblings;
  318. size += sizeof(u64);
  319. }
  320. size += entry * nr;
  321. event->read_size = size;
  322. }
  323. static void perf_event__header_size(struct perf_event *event)
  324. {
  325. struct perf_sample_data *data;
  326. u64 sample_type = event->attr.sample_type;
  327. u16 size = 0;
  328. perf_event__read_size(event);
  329. if (sample_type & PERF_SAMPLE_IP)
  330. size += sizeof(data->ip);
  331. if (sample_type & PERF_SAMPLE_ADDR)
  332. size += sizeof(data->addr);
  333. if (sample_type & PERF_SAMPLE_PERIOD)
  334. size += sizeof(data->period);
  335. if (sample_type & PERF_SAMPLE_READ)
  336. size += event->read_size;
  337. event->header_size = size;
  338. }
  339. static void perf_event__id_header_size(struct perf_event *event)
  340. {
  341. struct perf_sample_data *data;
  342. u64 sample_type = event->attr.sample_type;
  343. u16 size = 0;
  344. if (sample_type & PERF_SAMPLE_TID)
  345. size += sizeof(data->tid_entry);
  346. if (sample_type & PERF_SAMPLE_TIME)
  347. size += sizeof(data->time);
  348. if (sample_type & PERF_SAMPLE_ID)
  349. size += sizeof(data->id);
  350. if (sample_type & PERF_SAMPLE_STREAM_ID)
  351. size += sizeof(data->stream_id);
  352. if (sample_type & PERF_SAMPLE_CPU)
  353. size += sizeof(data->cpu_entry);
  354. event->id_header_size = size;
  355. }
  356. static void perf_group_attach(struct perf_event *event)
  357. {
  358. struct perf_event *group_leader = event->group_leader, *pos;
  359. /*
  360. * We can have double attach due to group movement in perf_event_open.
  361. */
  362. if (event->attach_state & PERF_ATTACH_GROUP)
  363. return;
  364. event->attach_state |= PERF_ATTACH_GROUP;
  365. if (group_leader == event)
  366. return;
  367. if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
  368. !is_software_event(event))
  369. group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
  370. list_add_tail(&event->group_entry, &group_leader->sibling_list);
  371. group_leader->nr_siblings++;
  372. perf_event__header_size(group_leader);
  373. list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
  374. perf_event__header_size(pos);
  375. }
  376. /*
  377. * Remove a event from the lists for its context.
  378. * Must be called with ctx->mutex and ctx->lock held.
  379. */
  380. static void
  381. list_del_event(struct perf_event *event, struct perf_event_context *ctx)
  382. {
  383. /*
  384. * We can have double detach due to exit/hot-unplug + close.
  385. */
  386. if (!(event->attach_state & PERF_ATTACH_CONTEXT))
  387. return;
  388. event->attach_state &= ~PERF_ATTACH_CONTEXT;
  389. ctx->nr_events--;
  390. if (event->attr.inherit_stat)
  391. ctx->nr_stat--;
  392. list_del_rcu(&event->event_entry);
  393. if (event->group_leader == event)
  394. list_del_init(&event->group_entry);
  395. update_group_times(event);
  396. /*
  397. * If event was in error state, then keep it
  398. * that way, otherwise bogus counts will be
  399. * returned on read(). The only way to get out
  400. * of error state is by explicit re-enabling
  401. * of the event
  402. */
  403. if (event->state > PERF_EVENT_STATE_OFF)
  404. event->state = PERF_EVENT_STATE_OFF;
  405. }
  406. static void perf_group_detach(struct perf_event *event)
  407. {
  408. struct perf_event *sibling, *tmp;
  409. struct list_head *list = NULL;
  410. /*
  411. * We can have double detach due to exit/hot-unplug + close.
  412. */
  413. if (!(event->attach_state & PERF_ATTACH_GROUP))
  414. return;
  415. event->attach_state &= ~PERF_ATTACH_GROUP;
  416. /*
  417. * If this is a sibling, remove it from its group.
  418. */
  419. if (event->group_leader != event) {
  420. list_del_init(&event->group_entry);
  421. event->group_leader->nr_siblings--;
  422. goto out;
  423. }
  424. if (!list_empty(&event->group_entry))
  425. list = &event->group_entry;
  426. /*
  427. * If this was a group event with sibling events then
  428. * upgrade the siblings to singleton events by adding them
  429. * to whatever list we are on.
  430. */
  431. list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
  432. if (list)
  433. list_move_tail(&sibling->group_entry, list);
  434. sibling->group_leader = sibling;
  435. /* Inherit group flags from the previous leader */
  436. sibling->group_flags = event->group_flags;
  437. }
  438. out:
  439. perf_event__header_size(event->group_leader);
  440. list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
  441. perf_event__header_size(tmp);
  442. }
  443. static inline int
  444. event_filter_match(struct perf_event *event)
  445. {
  446. return event->cpu == -1 || event->cpu == smp_processor_id();
  447. }
  448. static void
  449. event_sched_out(struct perf_event *event,
  450. struct perf_cpu_context *cpuctx,
  451. struct perf_event_context *ctx)
  452. {
  453. u64 tstamp = perf_event_time(event);
  454. u64 delta;
  455. /*
  456. * An event which could not be activated because of
  457. * filter mismatch still needs to have its timings
  458. * maintained, otherwise bogus information is return
  459. * via read() for time_enabled, time_running:
  460. */
  461. if (event->state == PERF_EVENT_STATE_INACTIVE
  462. && !event_filter_match(event)) {
  463. delta = ctx->time - event->tstamp_stopped;
  464. event->tstamp_running += delta;
  465. event->tstamp_stopped = tstamp;
  466. }
  467. if (event->state != PERF_EVENT_STATE_ACTIVE)
  468. return;
  469. event->state = PERF_EVENT_STATE_INACTIVE;
  470. if (event->pending_disable) {
  471. event->pending_disable = 0;
  472. event->state = PERF_EVENT_STATE_OFF;
  473. }
  474. event->tstamp_stopped = tstamp;
  475. event->pmu->del(event, 0);
  476. event->oncpu = -1;
  477. if (!is_software_event(event))
  478. cpuctx->active_oncpu--;
  479. ctx->nr_active--;
  480. if (event->attr.exclusive || !cpuctx->active_oncpu)
  481. cpuctx->exclusive = 0;
  482. }
  483. static void
  484. group_sched_out(struct perf_event *group_event,
  485. struct perf_cpu_context *cpuctx,
  486. struct perf_event_context *ctx)
  487. {
  488. struct perf_event *event;
  489. int state = group_event->state;
  490. event_sched_out(group_event, cpuctx, ctx);
  491. /*
  492. * Schedule out siblings (if any):
  493. */
  494. list_for_each_entry(event, &group_event->sibling_list, group_entry)
  495. event_sched_out(event, cpuctx, ctx);
  496. if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
  497. cpuctx->exclusive = 0;
  498. }
  499. static inline struct perf_cpu_context *
  500. __get_cpu_context(struct perf_event_context *ctx)
  501. {
  502. return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
  503. }
  504. /*
  505. * Cross CPU call to remove a performance event
  506. *
  507. * We disable the event on the hardware level first. After that we
  508. * remove it from the context list.
  509. */
  510. static void __perf_event_remove_from_context(void *info)
  511. {
  512. struct perf_event *event = info;
  513. struct perf_event_context *ctx = event->ctx;
  514. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  515. /*
  516. * If this is a task context, we need to check whether it is
  517. * the current task context of this cpu. If not it has been
  518. * scheduled out before the smp call arrived.
  519. */
  520. if (ctx->task && cpuctx->task_ctx != ctx)
  521. return;
  522. raw_spin_lock(&ctx->lock);
  523. event_sched_out(event, cpuctx, ctx);
  524. list_del_event(event, ctx);
  525. raw_spin_unlock(&ctx->lock);
  526. }
  527. /*
  528. * Remove the event from a task's (or a CPU's) list of events.
  529. *
  530. * Must be called with ctx->mutex held.
  531. *
  532. * CPU events are removed with a smp call. For task events we only
  533. * call when the task is on a CPU.
  534. *
  535. * If event->ctx is a cloned context, callers must make sure that
  536. * every task struct that event->ctx->task could possibly point to
  537. * remains valid. This is OK when called from perf_release since
  538. * that only calls us on the top-level context, which can't be a clone.
  539. * When called from perf_event_exit_task, it's OK because the
  540. * context has been detached from its task.
  541. */
  542. static void perf_event_remove_from_context(struct perf_event *event)
  543. {
  544. struct perf_event_context *ctx = event->ctx;
  545. struct task_struct *task = ctx->task;
  546. if (!task) {
  547. /*
  548. * Per cpu events are removed via an smp call and
  549. * the removal is always successful.
  550. */
  551. smp_call_function_single(event->cpu,
  552. __perf_event_remove_from_context,
  553. event, 1);
  554. return;
  555. }
  556. retry:
  557. task_oncpu_function_call(task, __perf_event_remove_from_context,
  558. event);
  559. raw_spin_lock_irq(&ctx->lock);
  560. /*
  561. * If the context is active we need to retry the smp call.
  562. */
  563. if (ctx->nr_active && !list_empty(&event->group_entry)) {
  564. raw_spin_unlock_irq(&ctx->lock);
  565. goto retry;
  566. }
  567. /*
  568. * The lock prevents that this context is scheduled in so we
  569. * can remove the event safely, if the call above did not
  570. * succeed.
  571. */
  572. if (!list_empty(&event->group_entry))
  573. list_del_event(event, ctx);
  574. raw_spin_unlock_irq(&ctx->lock);
  575. }
  576. /*
  577. * Cross CPU call to disable a performance event
  578. */
  579. static void __perf_event_disable(void *info)
  580. {
  581. struct perf_event *event = info;
  582. struct perf_event_context *ctx = event->ctx;
  583. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  584. /*
  585. * If this is a per-task event, need to check whether this
  586. * event's task is the current task on this cpu.
  587. */
  588. if (ctx->task && cpuctx->task_ctx != ctx)
  589. return;
  590. raw_spin_lock(&ctx->lock);
  591. /*
  592. * If the event is on, turn it off.
  593. * If it is in error state, leave it in error state.
  594. */
  595. if (event->state >= PERF_EVENT_STATE_INACTIVE) {
  596. update_context_time(ctx);
  597. update_group_times(event);
  598. if (event == event->group_leader)
  599. group_sched_out(event, cpuctx, ctx);
  600. else
  601. event_sched_out(event, cpuctx, ctx);
  602. event->state = PERF_EVENT_STATE_OFF;
  603. }
  604. raw_spin_unlock(&ctx->lock);
  605. }
  606. /*
  607. * Disable a event.
  608. *
  609. * If event->ctx is a cloned context, callers must make sure that
  610. * every task struct that event->ctx->task could possibly point to
  611. * remains valid. This condition is satisifed when called through
  612. * perf_event_for_each_child or perf_event_for_each because they
  613. * hold the top-level event's child_mutex, so any descendant that
  614. * goes to exit will block in sync_child_event.
  615. * When called from perf_pending_event it's OK because event->ctx
  616. * is the current context on this CPU and preemption is disabled,
  617. * hence we can't get into perf_event_task_sched_out for this context.
  618. */
  619. void perf_event_disable(struct perf_event *event)
  620. {
  621. struct perf_event_context *ctx = event->ctx;
  622. struct task_struct *task = ctx->task;
  623. if (!task) {
  624. /*
  625. * Disable the event on the cpu that it's on
  626. */
  627. smp_call_function_single(event->cpu, __perf_event_disable,
  628. event, 1);
  629. return;
  630. }
  631. retry:
  632. task_oncpu_function_call(task, __perf_event_disable, event);
  633. raw_spin_lock_irq(&ctx->lock);
  634. /*
  635. * If the event is still active, we need to retry the cross-call.
  636. */
  637. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  638. raw_spin_unlock_irq(&ctx->lock);
  639. goto retry;
  640. }
  641. /*
  642. * Since we have the lock this context can't be scheduled
  643. * in, so we can change the state safely.
  644. */
  645. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  646. update_group_times(event);
  647. event->state = PERF_EVENT_STATE_OFF;
  648. }
  649. raw_spin_unlock_irq(&ctx->lock);
  650. }
  651. #define MAX_INTERRUPTS (~0ULL)
  652. static void perf_log_throttle(struct perf_event *event, int enable);
  653. static int
  654. event_sched_in(struct perf_event *event,
  655. struct perf_cpu_context *cpuctx,
  656. struct perf_event_context *ctx)
  657. {
  658. u64 tstamp = perf_event_time(event);
  659. if (event->state <= PERF_EVENT_STATE_OFF)
  660. return 0;
  661. event->state = PERF_EVENT_STATE_ACTIVE;
  662. event->oncpu = smp_processor_id();
  663. /*
  664. * Unthrottle events, since we scheduled we might have missed several
  665. * ticks already, also for a heavily scheduling task there is little
  666. * guarantee it'll get a tick in a timely manner.
  667. */
  668. if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
  669. perf_log_throttle(event, 1);
  670. event->hw.interrupts = 0;
  671. }
  672. /*
  673. * The new state must be visible before we turn it on in the hardware:
  674. */
  675. smp_wmb();
  676. if (event->pmu->add(event, PERF_EF_START)) {
  677. event->state = PERF_EVENT_STATE_INACTIVE;
  678. event->oncpu = -1;
  679. return -EAGAIN;
  680. }
  681. event->tstamp_running += tstamp - event->tstamp_stopped;
  682. event->shadow_ctx_time = tstamp - ctx->timestamp;
  683. if (!is_software_event(event))
  684. cpuctx->active_oncpu++;
  685. ctx->nr_active++;
  686. if (event->attr.exclusive)
  687. cpuctx->exclusive = 1;
  688. return 0;
  689. }
  690. static int
  691. group_sched_in(struct perf_event *group_event,
  692. struct perf_cpu_context *cpuctx,
  693. struct perf_event_context *ctx)
  694. {
  695. struct perf_event *event, *partial_group = NULL;
  696. struct pmu *pmu = group_event->pmu;
  697. u64 now = ctx->time;
  698. bool simulate = false;
  699. if (group_event->state == PERF_EVENT_STATE_OFF)
  700. return 0;
  701. pmu->start_txn(pmu);
  702. if (event_sched_in(group_event, cpuctx, ctx)) {
  703. pmu->cancel_txn(pmu);
  704. return -EAGAIN;
  705. }
  706. /*
  707. * Schedule in siblings as one group (if any):
  708. */
  709. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  710. if (event_sched_in(event, cpuctx, ctx)) {
  711. partial_group = event;
  712. goto group_error;
  713. }
  714. }
  715. if (!pmu->commit_txn(pmu))
  716. return 0;
  717. group_error:
  718. /*
  719. * Groups can be scheduled in as one unit only, so undo any
  720. * partial group before returning:
  721. * The events up to the failed event are scheduled out normally,
  722. * tstamp_stopped will be updated.
  723. *
  724. * The failed events and the remaining siblings need to have
  725. * their timings updated as if they had gone thru event_sched_in()
  726. * and event_sched_out(). This is required to get consistent timings
  727. * across the group. This also takes care of the case where the group
  728. * could never be scheduled by ensuring tstamp_stopped is set to mark
  729. * the time the event was actually stopped, such that time delta
  730. * calculation in update_event_times() is correct.
  731. */
  732. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  733. if (event == partial_group)
  734. simulate = true;
  735. if (simulate) {
  736. event->tstamp_running += now - event->tstamp_stopped;
  737. event->tstamp_stopped = now;
  738. } else {
  739. event_sched_out(event, cpuctx, ctx);
  740. }
  741. }
  742. event_sched_out(group_event, cpuctx, ctx);
  743. pmu->cancel_txn(pmu);
  744. return -EAGAIN;
  745. }
  746. /*
  747. * Work out whether we can put this event group on the CPU now.
  748. */
  749. static int group_can_go_on(struct perf_event *event,
  750. struct perf_cpu_context *cpuctx,
  751. int can_add_hw)
  752. {
  753. /*
  754. * Groups consisting entirely of software events can always go on.
  755. */
  756. if (event->group_flags & PERF_GROUP_SOFTWARE)
  757. return 1;
  758. /*
  759. * If an exclusive group is already on, no other hardware
  760. * events can go on.
  761. */
  762. if (cpuctx->exclusive)
  763. return 0;
  764. /*
  765. * If this group is exclusive and there are already
  766. * events on the CPU, it can't go on.
  767. */
  768. if (event->attr.exclusive && cpuctx->active_oncpu)
  769. return 0;
  770. /*
  771. * Otherwise, try to add it if all previous groups were able
  772. * to go on.
  773. */
  774. return can_add_hw;
  775. }
  776. static void add_event_to_ctx(struct perf_event *event,
  777. struct perf_event_context *ctx)
  778. {
  779. u64 tstamp = perf_event_time(event);
  780. list_add_event(event, ctx);
  781. perf_group_attach(event);
  782. event->tstamp_enabled = tstamp;
  783. event->tstamp_running = tstamp;
  784. event->tstamp_stopped = tstamp;
  785. }
  786. /*
  787. * Cross CPU call to install and enable a performance event
  788. *
  789. * Must be called with ctx->mutex held
  790. */
  791. static void __perf_install_in_context(void *info)
  792. {
  793. struct perf_event *event = info;
  794. struct perf_event_context *ctx = event->ctx;
  795. struct perf_event *leader = event->group_leader;
  796. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  797. int err;
  798. /*
  799. * If this is a task context, we need to check whether it is
  800. * the current task context of this cpu. If not it has been
  801. * scheduled out before the smp call arrived.
  802. * Or possibly this is the right context but it isn't
  803. * on this cpu because it had no events.
  804. */
  805. if (ctx->task && cpuctx->task_ctx != ctx) {
  806. if (cpuctx->task_ctx || ctx->task != current)
  807. return;
  808. cpuctx->task_ctx = ctx;
  809. }
  810. raw_spin_lock(&ctx->lock);
  811. ctx->is_active = 1;
  812. update_context_time(ctx);
  813. add_event_to_ctx(event, ctx);
  814. if (!event_filter_match(event))
  815. goto unlock;
  816. /*
  817. * Don't put the event on if it is disabled or if
  818. * it is in a group and the group isn't on.
  819. */
  820. if (event->state != PERF_EVENT_STATE_INACTIVE ||
  821. (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
  822. goto unlock;
  823. /*
  824. * An exclusive event can't go on if there are already active
  825. * hardware events, and no hardware event can go on if there
  826. * is already an exclusive event on.
  827. */
  828. if (!group_can_go_on(event, cpuctx, 1))
  829. err = -EEXIST;
  830. else
  831. err = event_sched_in(event, cpuctx, ctx);
  832. if (err) {
  833. /*
  834. * This event couldn't go on. If it is in a group
  835. * then we have to pull the whole group off.
  836. * If the event group is pinned then put it in error state.
  837. */
  838. if (leader != event)
  839. group_sched_out(leader, cpuctx, ctx);
  840. if (leader->attr.pinned) {
  841. update_group_times(leader);
  842. leader->state = PERF_EVENT_STATE_ERROR;
  843. }
  844. }
  845. unlock:
  846. raw_spin_unlock(&ctx->lock);
  847. }
  848. /*
  849. * Attach a performance event to a context
  850. *
  851. * First we add the event to the list with the hardware enable bit
  852. * in event->hw_config cleared.
  853. *
  854. * If the event is attached to a task which is on a CPU we use a smp
  855. * call to enable it in the task context. The task might have been
  856. * scheduled away, but we check this in the smp call again.
  857. *
  858. * Must be called with ctx->mutex held.
  859. */
  860. static void
  861. perf_install_in_context(struct perf_event_context *ctx,
  862. struct perf_event *event,
  863. int cpu)
  864. {
  865. struct task_struct *task = ctx->task;
  866. event->ctx = ctx;
  867. if (!task) {
  868. /*
  869. * Per cpu events are installed via an smp call and
  870. * the install is always successful.
  871. */
  872. smp_call_function_single(cpu, __perf_install_in_context,
  873. event, 1);
  874. return;
  875. }
  876. retry:
  877. task_oncpu_function_call(task, __perf_install_in_context,
  878. event);
  879. raw_spin_lock_irq(&ctx->lock);
  880. /*
  881. * we need to retry the smp call.
  882. */
  883. if (ctx->is_active && list_empty(&event->group_entry)) {
  884. raw_spin_unlock_irq(&ctx->lock);
  885. goto retry;
  886. }
  887. /*
  888. * The lock prevents that this context is scheduled in so we
  889. * can add the event safely, if it the call above did not
  890. * succeed.
  891. */
  892. if (list_empty(&event->group_entry))
  893. add_event_to_ctx(event, ctx);
  894. raw_spin_unlock_irq(&ctx->lock);
  895. }
  896. /*
  897. * Put a event into inactive state and update time fields.
  898. * Enabling the leader of a group effectively enables all
  899. * the group members that aren't explicitly disabled, so we
  900. * have to update their ->tstamp_enabled also.
  901. * Note: this works for group members as well as group leaders
  902. * since the non-leader members' sibling_lists will be empty.
  903. */
  904. static void __perf_event_mark_enabled(struct perf_event *event,
  905. struct perf_event_context *ctx)
  906. {
  907. struct perf_event *sub;
  908. u64 tstamp = perf_event_time(event);
  909. event->state = PERF_EVENT_STATE_INACTIVE;
  910. event->tstamp_enabled = tstamp - event->total_time_enabled;
  911. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  912. if (sub->state >= PERF_EVENT_STATE_INACTIVE)
  913. sub->tstamp_enabled = tstamp - sub->total_time_enabled;
  914. }
  915. }
  916. /*
  917. * Cross CPU call to enable a performance event
  918. */
  919. static void __perf_event_enable(void *info)
  920. {
  921. struct perf_event *event = info;
  922. struct perf_event_context *ctx = event->ctx;
  923. struct perf_event *leader = event->group_leader;
  924. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  925. int err;
  926. /*
  927. * If this is a per-task event, need to check whether this
  928. * event's task is the current task on this cpu.
  929. */
  930. if (ctx->task && cpuctx->task_ctx != ctx) {
  931. if (cpuctx->task_ctx || ctx->task != current)
  932. return;
  933. cpuctx->task_ctx = ctx;
  934. }
  935. raw_spin_lock(&ctx->lock);
  936. ctx->is_active = 1;
  937. update_context_time(ctx);
  938. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  939. goto unlock;
  940. __perf_event_mark_enabled(event, ctx);
  941. if (!event_filter_match(event))
  942. goto unlock;
  943. /*
  944. * If the event is in a group and isn't the group leader,
  945. * then don't put it on unless the group is on.
  946. */
  947. if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
  948. goto unlock;
  949. if (!group_can_go_on(event, cpuctx, 1)) {
  950. err = -EEXIST;
  951. } else {
  952. if (event == leader)
  953. err = group_sched_in(event, cpuctx, ctx);
  954. else
  955. err = event_sched_in(event, cpuctx, ctx);
  956. }
  957. if (err) {
  958. /*
  959. * If this event can't go on and it's part of a
  960. * group, then the whole group has to come off.
  961. */
  962. if (leader != event)
  963. group_sched_out(leader, cpuctx, ctx);
  964. if (leader->attr.pinned) {
  965. update_group_times(leader);
  966. leader->state = PERF_EVENT_STATE_ERROR;
  967. }
  968. }
  969. unlock:
  970. raw_spin_unlock(&ctx->lock);
  971. }
  972. /*
  973. * Enable a event.
  974. *
  975. * If event->ctx is a cloned context, callers must make sure that
  976. * every task struct that event->ctx->task could possibly point to
  977. * remains valid. This condition is satisfied when called through
  978. * perf_event_for_each_child or perf_event_for_each as described
  979. * for perf_event_disable.
  980. */
  981. void perf_event_enable(struct perf_event *event)
  982. {
  983. struct perf_event_context *ctx = event->ctx;
  984. struct task_struct *task = ctx->task;
  985. if (!task) {
  986. /*
  987. * Enable the event on the cpu that it's on
  988. */
  989. smp_call_function_single(event->cpu, __perf_event_enable,
  990. event, 1);
  991. return;
  992. }
  993. raw_spin_lock_irq(&ctx->lock);
  994. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  995. goto out;
  996. /*
  997. * If the event is in error state, clear that first.
  998. * That way, if we see the event in error state below, we
  999. * know that it has gone back into error state, as distinct
  1000. * from the task having been scheduled away before the
  1001. * cross-call arrived.
  1002. */
  1003. if (event->state == PERF_EVENT_STATE_ERROR)
  1004. event->state = PERF_EVENT_STATE_OFF;
  1005. retry:
  1006. raw_spin_unlock_irq(&ctx->lock);
  1007. task_oncpu_function_call(task, __perf_event_enable, event);
  1008. raw_spin_lock_irq(&ctx->lock);
  1009. /*
  1010. * If the context is active and the event is still off,
  1011. * we need to retry the cross-call.
  1012. */
  1013. if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
  1014. goto retry;
  1015. /*
  1016. * Since we have the lock this context can't be scheduled
  1017. * in, so we can change the state safely.
  1018. */
  1019. if (event->state == PERF_EVENT_STATE_OFF)
  1020. __perf_event_mark_enabled(event, ctx);
  1021. out:
  1022. raw_spin_unlock_irq(&ctx->lock);
  1023. }
  1024. static int perf_event_refresh(struct perf_event *event, int refresh)
  1025. {
  1026. /*
  1027. * not supported on inherited events
  1028. */
  1029. if (event->attr.inherit || !is_sampling_event(event))
  1030. return -EINVAL;
  1031. atomic_add(refresh, &event->event_limit);
  1032. perf_event_enable(event);
  1033. return 0;
  1034. }
  1035. static void ctx_sched_out(struct perf_event_context *ctx,
  1036. struct perf_cpu_context *cpuctx,
  1037. enum event_type_t event_type)
  1038. {
  1039. struct perf_event *event;
  1040. raw_spin_lock(&ctx->lock);
  1041. perf_pmu_disable(ctx->pmu);
  1042. ctx->is_active = 0;
  1043. if (likely(!ctx->nr_events))
  1044. goto out;
  1045. update_context_time(ctx);
  1046. if (!ctx->nr_active)
  1047. goto out;
  1048. if (event_type & EVENT_PINNED) {
  1049. list_for_each_entry(event, &ctx->pinned_groups, group_entry)
  1050. group_sched_out(event, cpuctx, ctx);
  1051. }
  1052. if (event_type & EVENT_FLEXIBLE) {
  1053. list_for_each_entry(event, &ctx->flexible_groups, group_entry)
  1054. group_sched_out(event, cpuctx, ctx);
  1055. }
  1056. out:
  1057. perf_pmu_enable(ctx->pmu);
  1058. raw_spin_unlock(&ctx->lock);
  1059. }
  1060. /*
  1061. * Test whether two contexts are equivalent, i.e. whether they
  1062. * have both been cloned from the same version of the same context
  1063. * and they both have the same number of enabled events.
  1064. * If the number of enabled events is the same, then the set
  1065. * of enabled events should be the same, because these are both
  1066. * inherited contexts, therefore we can't access individual events
  1067. * in them directly with an fd; we can only enable/disable all
  1068. * events via prctl, or enable/disable all events in a family
  1069. * via ioctl, which will have the same effect on both contexts.
  1070. */
  1071. static int context_equiv(struct perf_event_context *ctx1,
  1072. struct perf_event_context *ctx2)
  1073. {
  1074. return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
  1075. && ctx1->parent_gen == ctx2->parent_gen
  1076. && !ctx1->pin_count && !ctx2->pin_count;
  1077. }
  1078. static void __perf_event_sync_stat(struct perf_event *event,
  1079. struct perf_event *next_event)
  1080. {
  1081. u64 value;
  1082. if (!event->attr.inherit_stat)
  1083. return;
  1084. /*
  1085. * Update the event value, we cannot use perf_event_read()
  1086. * because we're in the middle of a context switch and have IRQs
  1087. * disabled, which upsets smp_call_function_single(), however
  1088. * we know the event must be on the current CPU, therefore we
  1089. * don't need to use it.
  1090. */
  1091. switch (event->state) {
  1092. case PERF_EVENT_STATE_ACTIVE:
  1093. event->pmu->read(event);
  1094. /* fall-through */
  1095. case PERF_EVENT_STATE_INACTIVE:
  1096. update_event_times(event);
  1097. break;
  1098. default:
  1099. break;
  1100. }
  1101. /*
  1102. * In order to keep per-task stats reliable we need to flip the event
  1103. * values when we flip the contexts.
  1104. */
  1105. value = local64_read(&next_event->count);
  1106. value = local64_xchg(&event->count, value);
  1107. local64_set(&next_event->count, value);
  1108. swap(event->total_time_enabled, next_event->total_time_enabled);
  1109. swap(event->total_time_running, next_event->total_time_running);
  1110. /*
  1111. * Since we swizzled the values, update the user visible data too.
  1112. */
  1113. perf_event_update_userpage(event);
  1114. perf_event_update_userpage(next_event);
  1115. }
  1116. #define list_next_entry(pos, member) \
  1117. list_entry(pos->member.next, typeof(*pos), member)
  1118. static void perf_event_sync_stat(struct perf_event_context *ctx,
  1119. struct perf_event_context *next_ctx)
  1120. {
  1121. struct perf_event *event, *next_event;
  1122. if (!ctx->nr_stat)
  1123. return;
  1124. update_context_time(ctx);
  1125. event = list_first_entry(&ctx->event_list,
  1126. struct perf_event, event_entry);
  1127. next_event = list_first_entry(&next_ctx->event_list,
  1128. struct perf_event, event_entry);
  1129. while (&event->event_entry != &ctx->event_list &&
  1130. &next_event->event_entry != &next_ctx->event_list) {
  1131. __perf_event_sync_stat(event, next_event);
  1132. event = list_next_entry(event, event_entry);
  1133. next_event = list_next_entry(next_event, event_entry);
  1134. }
  1135. }
  1136. void perf_event_context_sched_out(struct task_struct *task, int ctxn,
  1137. struct task_struct *next)
  1138. {
  1139. struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
  1140. struct perf_event_context *next_ctx;
  1141. struct perf_event_context *parent;
  1142. struct perf_cpu_context *cpuctx;
  1143. int do_switch = 1;
  1144. if (likely(!ctx))
  1145. return;
  1146. cpuctx = __get_cpu_context(ctx);
  1147. if (!cpuctx->task_ctx)
  1148. return;
  1149. rcu_read_lock();
  1150. parent = rcu_dereference(ctx->parent_ctx);
  1151. next_ctx = next->perf_event_ctxp[ctxn];
  1152. if (parent && next_ctx &&
  1153. rcu_dereference(next_ctx->parent_ctx) == parent) {
  1154. /*
  1155. * Looks like the two contexts are clones, so we might be
  1156. * able to optimize the context switch. We lock both
  1157. * contexts and check that they are clones under the
  1158. * lock (including re-checking that neither has been
  1159. * uncloned in the meantime). It doesn't matter which
  1160. * order we take the locks because no other cpu could
  1161. * be trying to lock both of these tasks.
  1162. */
  1163. raw_spin_lock(&ctx->lock);
  1164. raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
  1165. if (context_equiv(ctx, next_ctx)) {
  1166. /*
  1167. * XXX do we need a memory barrier of sorts
  1168. * wrt to rcu_dereference() of perf_event_ctxp
  1169. */
  1170. task->perf_event_ctxp[ctxn] = next_ctx;
  1171. next->perf_event_ctxp[ctxn] = ctx;
  1172. ctx->task = next;
  1173. next_ctx->task = task;
  1174. do_switch = 0;
  1175. perf_event_sync_stat(ctx, next_ctx);
  1176. }
  1177. raw_spin_unlock(&next_ctx->lock);
  1178. raw_spin_unlock(&ctx->lock);
  1179. }
  1180. rcu_read_unlock();
  1181. if (do_switch) {
  1182. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  1183. cpuctx->task_ctx = NULL;
  1184. }
  1185. }
  1186. #define for_each_task_context_nr(ctxn) \
  1187. for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
  1188. /*
  1189. * Called from scheduler to remove the events of the current task,
  1190. * with interrupts disabled.
  1191. *
  1192. * We stop each event and update the event value in event->count.
  1193. *
  1194. * This does not protect us against NMI, but disable()
  1195. * sets the disabled bit in the control field of event _before_
  1196. * accessing the event control register. If a NMI hits, then it will
  1197. * not restart the event.
  1198. */
  1199. void __perf_event_task_sched_out(struct task_struct *task,
  1200. struct task_struct *next)
  1201. {
  1202. int ctxn;
  1203. for_each_task_context_nr(ctxn)
  1204. perf_event_context_sched_out(task, ctxn, next);
  1205. }
  1206. static void task_ctx_sched_out(struct perf_event_context *ctx,
  1207. enum event_type_t event_type)
  1208. {
  1209. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1210. if (!cpuctx->task_ctx)
  1211. return;
  1212. if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  1213. return;
  1214. ctx_sched_out(ctx, cpuctx, event_type);
  1215. cpuctx->task_ctx = NULL;
  1216. }
  1217. /*
  1218. * Called with IRQs disabled
  1219. */
  1220. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  1221. enum event_type_t event_type)
  1222. {
  1223. ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
  1224. }
  1225. static void
  1226. ctx_pinned_sched_in(struct perf_event_context *ctx,
  1227. struct perf_cpu_context *cpuctx)
  1228. {
  1229. struct perf_event *event;
  1230. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  1231. if (event->state <= PERF_EVENT_STATE_OFF)
  1232. continue;
  1233. if (!event_filter_match(event))
  1234. continue;
  1235. if (group_can_go_on(event, cpuctx, 1))
  1236. group_sched_in(event, cpuctx, ctx);
  1237. /*
  1238. * If this pinned group hasn't been scheduled,
  1239. * put it in error state.
  1240. */
  1241. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1242. update_group_times(event);
  1243. event->state = PERF_EVENT_STATE_ERROR;
  1244. }
  1245. }
  1246. }
  1247. static void
  1248. ctx_flexible_sched_in(struct perf_event_context *ctx,
  1249. struct perf_cpu_context *cpuctx)
  1250. {
  1251. struct perf_event *event;
  1252. int can_add_hw = 1;
  1253. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  1254. /* Ignore events in OFF or ERROR state */
  1255. if (event->state <= PERF_EVENT_STATE_OFF)
  1256. continue;
  1257. /*
  1258. * Listen to the 'cpu' scheduling filter constraint
  1259. * of events:
  1260. */
  1261. if (!event_filter_match(event))
  1262. continue;
  1263. if (group_can_go_on(event, cpuctx, can_add_hw)) {
  1264. if (group_sched_in(event, cpuctx, ctx))
  1265. can_add_hw = 0;
  1266. }
  1267. }
  1268. }
  1269. static void
  1270. ctx_sched_in(struct perf_event_context *ctx,
  1271. struct perf_cpu_context *cpuctx,
  1272. enum event_type_t event_type)
  1273. {
  1274. raw_spin_lock(&ctx->lock);
  1275. ctx->is_active = 1;
  1276. if (likely(!ctx->nr_events))
  1277. goto out;
  1278. ctx->timestamp = perf_clock();
  1279. /*
  1280. * First go through the list and put on any pinned groups
  1281. * in order to give them the best chance of going on.
  1282. */
  1283. if (event_type & EVENT_PINNED)
  1284. ctx_pinned_sched_in(ctx, cpuctx);
  1285. /* Then walk through the lower prio flexible groups */
  1286. if (event_type & EVENT_FLEXIBLE)
  1287. ctx_flexible_sched_in(ctx, cpuctx);
  1288. out:
  1289. raw_spin_unlock(&ctx->lock);
  1290. }
  1291. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  1292. enum event_type_t event_type)
  1293. {
  1294. struct perf_event_context *ctx = &cpuctx->ctx;
  1295. ctx_sched_in(ctx, cpuctx, event_type);
  1296. }
  1297. static void task_ctx_sched_in(struct perf_event_context *ctx,
  1298. enum event_type_t event_type)
  1299. {
  1300. struct perf_cpu_context *cpuctx;
  1301. cpuctx = __get_cpu_context(ctx);
  1302. if (cpuctx->task_ctx == ctx)
  1303. return;
  1304. ctx_sched_in(ctx, cpuctx, event_type);
  1305. cpuctx->task_ctx = ctx;
  1306. }
  1307. void perf_event_context_sched_in(struct perf_event_context *ctx)
  1308. {
  1309. struct perf_cpu_context *cpuctx;
  1310. cpuctx = __get_cpu_context(ctx);
  1311. if (cpuctx->task_ctx == ctx)
  1312. return;
  1313. perf_pmu_disable(ctx->pmu);
  1314. /*
  1315. * We want to keep the following priority order:
  1316. * cpu pinned (that don't need to move), task pinned,
  1317. * cpu flexible, task flexible.
  1318. */
  1319. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1320. ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
  1321. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  1322. ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
  1323. cpuctx->task_ctx = ctx;
  1324. /*
  1325. * Since these rotations are per-cpu, we need to ensure the
  1326. * cpu-context we got scheduled on is actually rotating.
  1327. */
  1328. perf_pmu_rotate_start(ctx->pmu);
  1329. perf_pmu_enable(ctx->pmu);
  1330. }
  1331. /*
  1332. * Called from scheduler to add the events of the current task
  1333. * with interrupts disabled.
  1334. *
  1335. * We restore the event value and then enable it.
  1336. *
  1337. * This does not protect us against NMI, but enable()
  1338. * sets the enabled bit in the control field of event _before_
  1339. * accessing the event control register. If a NMI hits, then it will
  1340. * keep the event running.
  1341. */
  1342. void __perf_event_task_sched_in(struct task_struct *task)
  1343. {
  1344. struct perf_event_context *ctx;
  1345. int ctxn;
  1346. for_each_task_context_nr(ctxn) {
  1347. ctx = task->perf_event_ctxp[ctxn];
  1348. if (likely(!ctx))
  1349. continue;
  1350. perf_event_context_sched_in(ctx);
  1351. }
  1352. }
  1353. static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
  1354. {
  1355. u64 frequency = event->attr.sample_freq;
  1356. u64 sec = NSEC_PER_SEC;
  1357. u64 divisor, dividend;
  1358. int count_fls, nsec_fls, frequency_fls, sec_fls;
  1359. count_fls = fls64(count);
  1360. nsec_fls = fls64(nsec);
  1361. frequency_fls = fls64(frequency);
  1362. sec_fls = 30;
  1363. /*
  1364. * We got @count in @nsec, with a target of sample_freq HZ
  1365. * the target period becomes:
  1366. *
  1367. * @count * 10^9
  1368. * period = -------------------
  1369. * @nsec * sample_freq
  1370. *
  1371. */
  1372. /*
  1373. * Reduce accuracy by one bit such that @a and @b converge
  1374. * to a similar magnitude.
  1375. */
  1376. #define REDUCE_FLS(a, b) \
  1377. do { \
  1378. if (a##_fls > b##_fls) { \
  1379. a >>= 1; \
  1380. a##_fls--; \
  1381. } else { \
  1382. b >>= 1; \
  1383. b##_fls--; \
  1384. } \
  1385. } while (0)
  1386. /*
  1387. * Reduce accuracy until either term fits in a u64, then proceed with
  1388. * the other, so that finally we can do a u64/u64 division.
  1389. */
  1390. while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
  1391. REDUCE_FLS(nsec, frequency);
  1392. REDUCE_FLS(sec, count);
  1393. }
  1394. if (count_fls + sec_fls > 64) {
  1395. divisor = nsec * frequency;
  1396. while (count_fls + sec_fls > 64) {
  1397. REDUCE_FLS(count, sec);
  1398. divisor >>= 1;
  1399. }
  1400. dividend = count * sec;
  1401. } else {
  1402. dividend = count * sec;
  1403. while (nsec_fls + frequency_fls > 64) {
  1404. REDUCE_FLS(nsec, frequency);
  1405. dividend >>= 1;
  1406. }
  1407. divisor = nsec * frequency;
  1408. }
  1409. if (!divisor)
  1410. return dividend;
  1411. return div64_u64(dividend, divisor);
  1412. }
  1413. static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
  1414. {
  1415. struct hw_perf_event *hwc = &event->hw;
  1416. s64 period, sample_period;
  1417. s64 delta;
  1418. period = perf_calculate_period(event, nsec, count);
  1419. delta = (s64)(period - hwc->sample_period);
  1420. delta = (delta + 7) / 8; /* low pass filter */
  1421. sample_period = hwc->sample_period + delta;
  1422. if (!sample_period)
  1423. sample_period = 1;
  1424. hwc->sample_period = sample_period;
  1425. if (local64_read(&hwc->period_left) > 8*sample_period) {
  1426. event->pmu->stop(event, PERF_EF_UPDATE);
  1427. local64_set(&hwc->period_left, 0);
  1428. event->pmu->start(event, PERF_EF_RELOAD);
  1429. }
  1430. }
  1431. static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
  1432. {
  1433. struct perf_event *event;
  1434. struct hw_perf_event *hwc;
  1435. u64 interrupts, now;
  1436. s64 delta;
  1437. raw_spin_lock(&ctx->lock);
  1438. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  1439. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1440. continue;
  1441. if (!event_filter_match(event))
  1442. continue;
  1443. hwc = &event->hw;
  1444. interrupts = hwc->interrupts;
  1445. hwc->interrupts = 0;
  1446. /*
  1447. * unthrottle events on the tick
  1448. */
  1449. if (interrupts == MAX_INTERRUPTS) {
  1450. perf_log_throttle(event, 1);
  1451. event->pmu->start(event, 0);
  1452. }
  1453. if (!event->attr.freq || !event->attr.sample_freq)
  1454. continue;
  1455. event->pmu->read(event);
  1456. now = local64_read(&event->count);
  1457. delta = now - hwc->freq_count_stamp;
  1458. hwc->freq_count_stamp = now;
  1459. if (delta > 0)
  1460. perf_adjust_period(event, period, delta);
  1461. }
  1462. raw_spin_unlock(&ctx->lock);
  1463. }
  1464. /*
  1465. * Round-robin a context's events:
  1466. */
  1467. static void rotate_ctx(struct perf_event_context *ctx)
  1468. {
  1469. raw_spin_lock(&ctx->lock);
  1470. /*
  1471. * Rotate the first entry last of non-pinned groups. Rotation might be
  1472. * disabled by the inheritance code.
  1473. */
  1474. if (!ctx->rotate_disable)
  1475. list_rotate_left(&ctx->flexible_groups);
  1476. raw_spin_unlock(&ctx->lock);
  1477. }
  1478. /*
  1479. * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
  1480. * because they're strictly cpu affine and rotate_start is called with IRQs
  1481. * disabled, while rotate_context is called from IRQ context.
  1482. */
  1483. static void perf_rotate_context(struct perf_cpu_context *cpuctx)
  1484. {
  1485. u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
  1486. struct perf_event_context *ctx = NULL;
  1487. int rotate = 0, remove = 1;
  1488. if (cpuctx->ctx.nr_events) {
  1489. remove = 0;
  1490. if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
  1491. rotate = 1;
  1492. }
  1493. ctx = cpuctx->task_ctx;
  1494. if (ctx && ctx->nr_events) {
  1495. remove = 0;
  1496. if (ctx->nr_events != ctx->nr_active)
  1497. rotate = 1;
  1498. }
  1499. perf_pmu_disable(cpuctx->ctx.pmu);
  1500. perf_ctx_adjust_freq(&cpuctx->ctx, interval);
  1501. if (ctx)
  1502. perf_ctx_adjust_freq(ctx, interval);
  1503. if (!rotate)
  1504. goto done;
  1505. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1506. if (ctx)
  1507. task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
  1508. rotate_ctx(&cpuctx->ctx);
  1509. if (ctx)
  1510. rotate_ctx(ctx);
  1511. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  1512. if (ctx)
  1513. task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
  1514. done:
  1515. if (remove)
  1516. list_del_init(&cpuctx->rotation_list);
  1517. perf_pmu_enable(cpuctx->ctx.pmu);
  1518. }
  1519. void perf_event_task_tick(void)
  1520. {
  1521. struct list_head *head = &__get_cpu_var(rotation_list);
  1522. struct perf_cpu_context *cpuctx, *tmp;
  1523. WARN_ON(!irqs_disabled());
  1524. list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
  1525. if (cpuctx->jiffies_interval == 1 ||
  1526. !(jiffies % cpuctx->jiffies_interval))
  1527. perf_rotate_context(cpuctx);
  1528. }
  1529. }
  1530. static int event_enable_on_exec(struct perf_event *event,
  1531. struct perf_event_context *ctx)
  1532. {
  1533. if (!event->attr.enable_on_exec)
  1534. return 0;
  1535. event->attr.enable_on_exec = 0;
  1536. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1537. return 0;
  1538. __perf_event_mark_enabled(event, ctx);
  1539. return 1;
  1540. }
  1541. /*
  1542. * Enable all of a task's events that have been marked enable-on-exec.
  1543. * This expects task == current.
  1544. */
  1545. static void perf_event_enable_on_exec(struct perf_event_context *ctx)
  1546. {
  1547. struct perf_event *event;
  1548. unsigned long flags;
  1549. int enabled = 0;
  1550. int ret;
  1551. local_irq_save(flags);
  1552. if (!ctx || !ctx->nr_events)
  1553. goto out;
  1554. task_ctx_sched_out(ctx, EVENT_ALL);
  1555. raw_spin_lock(&ctx->lock);
  1556. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  1557. ret = event_enable_on_exec(event, ctx);
  1558. if (ret)
  1559. enabled = 1;
  1560. }
  1561. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  1562. ret = event_enable_on_exec(event, ctx);
  1563. if (ret)
  1564. enabled = 1;
  1565. }
  1566. /*
  1567. * Unclone this context if we enabled any event.
  1568. */
  1569. if (enabled)
  1570. unclone_ctx(ctx);
  1571. raw_spin_unlock(&ctx->lock);
  1572. perf_event_context_sched_in(ctx);
  1573. out:
  1574. local_irq_restore(flags);
  1575. }
  1576. /*
  1577. * Cross CPU call to read the hardware event
  1578. */
  1579. static void __perf_event_read(void *info)
  1580. {
  1581. struct perf_event *event = info;
  1582. struct perf_event_context *ctx = event->ctx;
  1583. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1584. /*
  1585. * If this is a task context, we need to check whether it is
  1586. * the current task context of this cpu. If not it has been
  1587. * scheduled out before the smp call arrived. In that case
  1588. * event->count would have been updated to a recent sample
  1589. * when the event was scheduled out.
  1590. */
  1591. if (ctx->task && cpuctx->task_ctx != ctx)
  1592. return;
  1593. raw_spin_lock(&ctx->lock);
  1594. if (ctx->is_active)
  1595. update_context_time(ctx);
  1596. update_event_times(event);
  1597. if (event->state == PERF_EVENT_STATE_ACTIVE)
  1598. event->pmu->read(event);
  1599. raw_spin_unlock(&ctx->lock);
  1600. }
  1601. static inline u64 perf_event_count(struct perf_event *event)
  1602. {
  1603. return local64_read(&event->count) + atomic64_read(&event->child_count);
  1604. }
  1605. static u64 perf_event_read(struct perf_event *event)
  1606. {
  1607. /*
  1608. * If event is enabled and currently active on a CPU, update the
  1609. * value in the event structure:
  1610. */
  1611. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  1612. smp_call_function_single(event->oncpu,
  1613. __perf_event_read, event, 1);
  1614. } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1615. struct perf_event_context *ctx = event->ctx;
  1616. unsigned long flags;
  1617. raw_spin_lock_irqsave(&ctx->lock, flags);
  1618. /*
  1619. * may read while context is not active
  1620. * (e.g., thread is blocked), in that case
  1621. * we cannot update context time
  1622. */
  1623. if (ctx->is_active)
  1624. update_context_time(ctx);
  1625. update_event_times(event);
  1626. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1627. }
  1628. return perf_event_count(event);
  1629. }
  1630. /*
  1631. * Callchain support
  1632. */
  1633. struct callchain_cpus_entries {
  1634. struct rcu_head rcu_head;
  1635. struct perf_callchain_entry *cpu_entries[0];
  1636. };
  1637. static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
  1638. static atomic_t nr_callchain_events;
  1639. static DEFINE_MUTEX(callchain_mutex);
  1640. struct callchain_cpus_entries *callchain_cpus_entries;
  1641. __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
  1642. struct pt_regs *regs)
  1643. {
  1644. }
  1645. __weak void perf_callchain_user(struct perf_callchain_entry *entry,
  1646. struct pt_regs *regs)
  1647. {
  1648. }
  1649. static void release_callchain_buffers_rcu(struct rcu_head *head)
  1650. {
  1651. struct callchain_cpus_entries *entries;
  1652. int cpu;
  1653. entries = container_of(head, struct callchain_cpus_entries, rcu_head);
  1654. for_each_possible_cpu(cpu)
  1655. kfree(entries->cpu_entries[cpu]);
  1656. kfree(entries);
  1657. }
  1658. static void release_callchain_buffers(void)
  1659. {
  1660. struct callchain_cpus_entries *entries;
  1661. entries = callchain_cpus_entries;
  1662. rcu_assign_pointer(callchain_cpus_entries, NULL);
  1663. call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
  1664. }
  1665. static int alloc_callchain_buffers(void)
  1666. {
  1667. int cpu;
  1668. int size;
  1669. struct callchain_cpus_entries *entries;
  1670. /*
  1671. * We can't use the percpu allocation API for data that can be
  1672. * accessed from NMI. Use a temporary manual per cpu allocation
  1673. * until that gets sorted out.
  1674. */
  1675. size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
  1676. entries = kzalloc(size, GFP_KERNEL);
  1677. if (!entries)
  1678. return -ENOMEM;
  1679. size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
  1680. for_each_possible_cpu(cpu) {
  1681. entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
  1682. cpu_to_node(cpu));
  1683. if (!entries->cpu_entries[cpu])
  1684. goto fail;
  1685. }
  1686. rcu_assign_pointer(callchain_cpus_entries, entries);
  1687. return 0;
  1688. fail:
  1689. for_each_possible_cpu(cpu)
  1690. kfree(entries->cpu_entries[cpu]);
  1691. kfree(entries);
  1692. return -ENOMEM;
  1693. }
  1694. static int get_callchain_buffers(void)
  1695. {
  1696. int err = 0;
  1697. int count;
  1698. mutex_lock(&callchain_mutex);
  1699. count = atomic_inc_return(&nr_callchain_events);
  1700. if (WARN_ON_ONCE(count < 1)) {
  1701. err = -EINVAL;
  1702. goto exit;
  1703. }
  1704. if (count > 1) {
  1705. /* If the allocation failed, give up */
  1706. if (!callchain_cpus_entries)
  1707. err = -ENOMEM;
  1708. goto exit;
  1709. }
  1710. err = alloc_callchain_buffers();
  1711. if (err)
  1712. release_callchain_buffers();
  1713. exit:
  1714. mutex_unlock(&callchain_mutex);
  1715. return err;
  1716. }
  1717. static void put_callchain_buffers(void)
  1718. {
  1719. if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
  1720. release_callchain_buffers();
  1721. mutex_unlock(&callchain_mutex);
  1722. }
  1723. }
  1724. static int get_recursion_context(int *recursion)
  1725. {
  1726. int rctx;
  1727. if (in_nmi())
  1728. rctx = 3;
  1729. else if (in_irq())
  1730. rctx = 2;
  1731. else if (in_softirq())
  1732. rctx = 1;
  1733. else
  1734. rctx = 0;
  1735. if (recursion[rctx])
  1736. return -1;
  1737. recursion[rctx]++;
  1738. barrier();
  1739. return rctx;
  1740. }
  1741. static inline void put_recursion_context(int *recursion, int rctx)
  1742. {
  1743. barrier();
  1744. recursion[rctx]--;
  1745. }
  1746. static struct perf_callchain_entry *get_callchain_entry(int *rctx)
  1747. {
  1748. int cpu;
  1749. struct callchain_cpus_entries *entries;
  1750. *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
  1751. if (*rctx == -1)
  1752. return NULL;
  1753. entries = rcu_dereference(callchain_cpus_entries);
  1754. if (!entries)
  1755. return NULL;
  1756. cpu = smp_processor_id();
  1757. return &entries->cpu_entries[cpu][*rctx];
  1758. }
  1759. static void
  1760. put_callchain_entry(int rctx)
  1761. {
  1762. put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
  1763. }
  1764. static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1765. {
  1766. int rctx;
  1767. struct perf_callchain_entry *entry;
  1768. entry = get_callchain_entry(&rctx);
  1769. if (rctx == -1)
  1770. return NULL;
  1771. if (!entry)
  1772. goto exit_put;
  1773. entry->nr = 0;
  1774. if (!user_mode(regs)) {
  1775. perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
  1776. perf_callchain_kernel(entry, regs);
  1777. if (current->mm)
  1778. regs = task_pt_regs(current);
  1779. else
  1780. regs = NULL;
  1781. }
  1782. if (regs) {
  1783. perf_callchain_store(entry, PERF_CONTEXT_USER);
  1784. perf_callchain_user(entry, regs);
  1785. }
  1786. exit_put:
  1787. put_callchain_entry(rctx);
  1788. return entry;
  1789. }
  1790. /*
  1791. * Initialize the perf_event context in a task_struct:
  1792. */
  1793. static void __perf_event_init_context(struct perf_event_context *ctx)
  1794. {
  1795. raw_spin_lock_init(&ctx->lock);
  1796. mutex_init(&ctx->mutex);
  1797. INIT_LIST_HEAD(&ctx->pinned_groups);
  1798. INIT_LIST_HEAD(&ctx->flexible_groups);
  1799. INIT_LIST_HEAD(&ctx->event_list);
  1800. atomic_set(&ctx->refcount, 1);
  1801. }
  1802. static struct perf_event_context *
  1803. alloc_perf_context(struct pmu *pmu, struct task_struct *task)
  1804. {
  1805. struct perf_event_context *ctx;
  1806. ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  1807. if (!ctx)
  1808. return NULL;
  1809. __perf_event_init_context(ctx);
  1810. if (task) {
  1811. ctx->task = task;
  1812. get_task_struct(task);
  1813. }
  1814. ctx->pmu = pmu;
  1815. return ctx;
  1816. }
  1817. static struct task_struct *
  1818. find_lively_task_by_vpid(pid_t vpid)
  1819. {
  1820. struct task_struct *task;
  1821. int err;
  1822. rcu_read_lock();
  1823. if (!vpid)
  1824. task = current;
  1825. else
  1826. task = find_task_by_vpid(vpid);
  1827. if (task)
  1828. get_task_struct(task);
  1829. rcu_read_unlock();
  1830. if (!task)
  1831. return ERR_PTR(-ESRCH);
  1832. /* Reuse ptrace permission checks for now. */
  1833. err = -EACCES;
  1834. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  1835. goto errout;
  1836. return task;
  1837. errout:
  1838. put_task_struct(task);
  1839. return ERR_PTR(err);
  1840. }
  1841. static struct perf_event_context *
  1842. find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
  1843. {
  1844. struct perf_event_context *ctx;
  1845. struct perf_cpu_context *cpuctx;
  1846. unsigned long flags;
  1847. int ctxn, err;
  1848. if (!task) {
  1849. /* Must be root to operate on a CPU event: */
  1850. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  1851. return ERR_PTR(-EACCES);
  1852. /*
  1853. * We could be clever and allow to attach a event to an
  1854. * offline CPU and activate it when the CPU comes up, but
  1855. * that's for later.
  1856. */
  1857. if (!cpu_online(cpu))
  1858. return ERR_PTR(-ENODEV);
  1859. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  1860. ctx = &cpuctx->ctx;
  1861. get_ctx(ctx);
  1862. return ctx;
  1863. }
  1864. err = -EINVAL;
  1865. ctxn = pmu->task_ctx_nr;
  1866. if (ctxn < 0)
  1867. goto errout;
  1868. retry:
  1869. ctx = perf_lock_task_context(task, ctxn, &flags);
  1870. if (ctx) {
  1871. unclone_ctx(ctx);
  1872. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1873. }
  1874. if (!ctx) {
  1875. ctx = alloc_perf_context(pmu, task);
  1876. err = -ENOMEM;
  1877. if (!ctx)
  1878. goto errout;
  1879. get_ctx(ctx);
  1880. err = 0;
  1881. mutex_lock(&task->perf_event_mutex);
  1882. /*
  1883. * If it has already passed perf_event_exit_task().
  1884. * we must see PF_EXITING, it takes this mutex too.
  1885. */
  1886. if (task->flags & PF_EXITING)
  1887. err = -ESRCH;
  1888. else if (task->perf_event_ctxp[ctxn])
  1889. err = -EAGAIN;
  1890. else
  1891. rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
  1892. mutex_unlock(&task->perf_event_mutex);
  1893. if (unlikely(err)) {
  1894. put_task_struct(task);
  1895. kfree(ctx);
  1896. if (err == -EAGAIN)
  1897. goto retry;
  1898. goto errout;
  1899. }
  1900. }
  1901. return ctx;
  1902. errout:
  1903. return ERR_PTR(err);
  1904. }
  1905. static void perf_event_free_filter(struct perf_event *event);
  1906. static void free_event_rcu(struct rcu_head *head)
  1907. {
  1908. struct perf_event *event;
  1909. event = container_of(head, struct perf_event, rcu_head);
  1910. if (event->ns)
  1911. put_pid_ns(event->ns);
  1912. perf_event_free_filter(event);
  1913. kfree(event);
  1914. }
  1915. static void perf_buffer_put(struct perf_buffer *buffer);
  1916. static void free_event(struct perf_event *event)
  1917. {
  1918. irq_work_sync(&event->pending);
  1919. if (!event->parent) {
  1920. if (event->attach_state & PERF_ATTACH_TASK)
  1921. jump_label_dec(&perf_task_events);
  1922. if (event->attr.mmap || event->attr.mmap_data)
  1923. atomic_dec(&nr_mmap_events);
  1924. if (event->attr.comm)
  1925. atomic_dec(&nr_comm_events);
  1926. if (event->attr.task)
  1927. atomic_dec(&nr_task_events);
  1928. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  1929. put_callchain_buffers();
  1930. }
  1931. if (event->buffer) {
  1932. perf_buffer_put(event->buffer);
  1933. event->buffer = NULL;
  1934. }
  1935. if (event->destroy)
  1936. event->destroy(event);
  1937. if (event->ctx)
  1938. put_ctx(event->ctx);
  1939. call_rcu(&event->rcu_head, free_event_rcu);
  1940. }
  1941. int perf_event_release_kernel(struct perf_event *event)
  1942. {
  1943. struct perf_event_context *ctx = event->ctx;
  1944. /*
  1945. * Remove from the PMU, can't get re-enabled since we got
  1946. * here because the last ref went.
  1947. */
  1948. perf_event_disable(event);
  1949. WARN_ON_ONCE(ctx->parent_ctx);
  1950. /*
  1951. * There are two ways this annotation is useful:
  1952. *
  1953. * 1) there is a lock recursion from perf_event_exit_task
  1954. * see the comment there.
  1955. *
  1956. * 2) there is a lock-inversion with mmap_sem through
  1957. * perf_event_read_group(), which takes faults while
  1958. * holding ctx->mutex, however this is called after
  1959. * the last filedesc died, so there is no possibility
  1960. * to trigger the AB-BA case.
  1961. */
  1962. mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
  1963. raw_spin_lock_irq(&ctx->lock);
  1964. perf_group_detach(event);
  1965. list_del_event(event, ctx);
  1966. raw_spin_unlock_irq(&ctx->lock);
  1967. mutex_unlock(&ctx->mutex);
  1968. free_event(event);
  1969. return 0;
  1970. }
  1971. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  1972. /*
  1973. * Called when the last reference to the file is gone.
  1974. */
  1975. static int perf_release(struct inode *inode, struct file *file)
  1976. {
  1977. struct perf_event *event = file->private_data;
  1978. struct task_struct *owner;
  1979. file->private_data = NULL;
  1980. rcu_read_lock();
  1981. owner = ACCESS_ONCE(event->owner);
  1982. /*
  1983. * Matches the smp_wmb() in perf_event_exit_task(). If we observe
  1984. * !owner it means the list deletion is complete and we can indeed
  1985. * free this event, otherwise we need to serialize on
  1986. * owner->perf_event_mutex.
  1987. */
  1988. smp_read_barrier_depends();
  1989. if (owner) {
  1990. /*
  1991. * Since delayed_put_task_struct() also drops the last
  1992. * task reference we can safely take a new reference
  1993. * while holding the rcu_read_lock().
  1994. */
  1995. get_task_struct(owner);
  1996. }
  1997. rcu_read_unlock();
  1998. if (owner) {
  1999. mutex_lock(&owner->perf_event_mutex);
  2000. /*
  2001. * We have to re-check the event->owner field, if it is cleared
  2002. * we raced with perf_event_exit_task(), acquiring the mutex
  2003. * ensured they're done, and we can proceed with freeing the
  2004. * event.
  2005. */
  2006. if (event->owner)
  2007. list_del_init(&event->owner_entry);
  2008. mutex_unlock(&owner->perf_event_mutex);
  2009. put_task_struct(owner);
  2010. }
  2011. return perf_event_release_kernel(event);
  2012. }
  2013. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  2014. {
  2015. struct perf_event *child;
  2016. u64 total = 0;
  2017. *enabled = 0;
  2018. *running = 0;
  2019. mutex_lock(&event->child_mutex);
  2020. total += perf_event_read(event);
  2021. *enabled += event->total_time_enabled +
  2022. atomic64_read(&event->child_total_time_enabled);
  2023. *running += event->total_time_running +
  2024. atomic64_read(&event->child_total_time_running);
  2025. list_for_each_entry(child, &event->child_list, child_list) {
  2026. total += perf_event_read(child);
  2027. *enabled += child->total_time_enabled;
  2028. *running += child->total_time_running;
  2029. }
  2030. mutex_unlock(&event->child_mutex);
  2031. return total;
  2032. }
  2033. EXPORT_SYMBOL_GPL(perf_event_read_value);
  2034. static int perf_event_read_group(struct perf_event *event,
  2035. u64 read_format, char __user *buf)
  2036. {
  2037. struct perf_event *leader = event->group_leader, *sub;
  2038. int n = 0, size = 0, ret = -EFAULT;
  2039. struct perf_event_context *ctx = leader->ctx;
  2040. u64 values[5];
  2041. u64 count, enabled, running;
  2042. mutex_lock(&ctx->mutex);
  2043. count = perf_event_read_value(leader, &enabled, &running);
  2044. values[n++] = 1 + leader->nr_siblings;
  2045. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2046. values[n++] = enabled;
  2047. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2048. values[n++] = running;
  2049. values[n++] = count;
  2050. if (read_format & PERF_FORMAT_ID)
  2051. values[n++] = primary_event_id(leader);
  2052. size = n * sizeof(u64);
  2053. if (copy_to_user(buf, values, size))
  2054. goto unlock;
  2055. ret = size;
  2056. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2057. n = 0;
  2058. values[n++] = perf_event_read_value(sub, &enabled, &running);
  2059. if (read_format & PERF_FORMAT_ID)
  2060. values[n++] = primary_event_id(sub);
  2061. size = n * sizeof(u64);
  2062. if (copy_to_user(buf + ret, values, size)) {
  2063. ret = -EFAULT;
  2064. goto unlock;
  2065. }
  2066. ret += size;
  2067. }
  2068. unlock:
  2069. mutex_unlock(&ctx->mutex);
  2070. return ret;
  2071. }
  2072. static int perf_event_read_one(struct perf_event *event,
  2073. u64 read_format, char __user *buf)
  2074. {
  2075. u64 enabled, running;
  2076. u64 values[4];
  2077. int n = 0;
  2078. values[n++] = perf_event_read_value(event, &enabled, &running);
  2079. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2080. values[n++] = enabled;
  2081. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2082. values[n++] = running;
  2083. if (read_format & PERF_FORMAT_ID)
  2084. values[n++] = primary_event_id(event);
  2085. if (copy_to_user(buf, values, n * sizeof(u64)))
  2086. return -EFAULT;
  2087. return n * sizeof(u64);
  2088. }
  2089. /*
  2090. * Read the performance event - simple non blocking version for now
  2091. */
  2092. static ssize_t
  2093. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  2094. {
  2095. u64 read_format = event->attr.read_format;
  2096. int ret;
  2097. /*
  2098. * Return end-of-file for a read on a event that is in
  2099. * error state (i.e. because it was pinned but it couldn't be
  2100. * scheduled on to the CPU at some point).
  2101. */
  2102. if (event->state == PERF_EVENT_STATE_ERROR)
  2103. return 0;
  2104. if (count < event->read_size)
  2105. return -ENOSPC;
  2106. WARN_ON_ONCE(event->ctx->parent_ctx);
  2107. if (read_format & PERF_FORMAT_GROUP)
  2108. ret = perf_event_read_group(event, read_format, buf);
  2109. else
  2110. ret = perf_event_read_one(event, read_format, buf);
  2111. return ret;
  2112. }
  2113. static ssize_t
  2114. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  2115. {
  2116. struct perf_event *event = file->private_data;
  2117. return perf_read_hw(event, buf, count);
  2118. }
  2119. static unsigned int perf_poll(struct file *file, poll_table *wait)
  2120. {
  2121. struct perf_event *event = file->private_data;
  2122. struct perf_buffer *buffer;
  2123. unsigned int events = POLL_HUP;
  2124. rcu_read_lock();
  2125. buffer = rcu_dereference(event->buffer);
  2126. if (buffer)
  2127. events = atomic_xchg(&buffer->poll, 0);
  2128. rcu_read_unlock();
  2129. poll_wait(file, &event->waitq, wait);
  2130. return events;
  2131. }
  2132. static void perf_event_reset(struct perf_event *event)
  2133. {
  2134. (void)perf_event_read(event);
  2135. local64_set(&event->count, 0);
  2136. perf_event_update_userpage(event);
  2137. }
  2138. /*
  2139. * Holding the top-level event's child_mutex means that any
  2140. * descendant process that has inherited this event will block
  2141. * in sync_child_event if it goes to exit, thus satisfying the
  2142. * task existence requirements of perf_event_enable/disable.
  2143. */
  2144. static void perf_event_for_each_child(struct perf_event *event,
  2145. void (*func)(struct perf_event *))
  2146. {
  2147. struct perf_event *child;
  2148. WARN_ON_ONCE(event->ctx->parent_ctx);
  2149. mutex_lock(&event->child_mutex);
  2150. func(event);
  2151. list_for_each_entry(child, &event->child_list, child_list)
  2152. func(child);
  2153. mutex_unlock(&event->child_mutex);
  2154. }
  2155. static void perf_event_for_each(struct perf_event *event,
  2156. void (*func)(struct perf_event *))
  2157. {
  2158. struct perf_event_context *ctx = event->ctx;
  2159. struct perf_event *sibling;
  2160. WARN_ON_ONCE(ctx->parent_ctx);
  2161. mutex_lock(&ctx->mutex);
  2162. event = event->group_leader;
  2163. perf_event_for_each_child(event, func);
  2164. func(event);
  2165. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  2166. perf_event_for_each_child(event, func);
  2167. mutex_unlock(&ctx->mutex);
  2168. }
  2169. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  2170. {
  2171. struct perf_event_context *ctx = event->ctx;
  2172. int ret = 0;
  2173. u64 value;
  2174. if (!is_sampling_event(event))
  2175. return -EINVAL;
  2176. if (copy_from_user(&value, arg, sizeof(value)))
  2177. return -EFAULT;
  2178. if (!value)
  2179. return -EINVAL;
  2180. raw_spin_lock_irq(&ctx->lock);
  2181. if (event->attr.freq) {
  2182. if (value > sysctl_perf_event_sample_rate) {
  2183. ret = -EINVAL;
  2184. goto unlock;
  2185. }
  2186. event->attr.sample_freq = value;
  2187. } else {
  2188. event->attr.sample_period = value;
  2189. event->hw.sample_period = value;
  2190. }
  2191. unlock:
  2192. raw_spin_unlock_irq(&ctx->lock);
  2193. return ret;
  2194. }
  2195. static const struct file_operations perf_fops;
  2196. static struct perf_event *perf_fget_light(int fd, int *fput_needed)
  2197. {
  2198. struct file *file;
  2199. file = fget_light(fd, fput_needed);
  2200. if (!file)
  2201. return ERR_PTR(-EBADF);
  2202. if (file->f_op != &perf_fops) {
  2203. fput_light(file, *fput_needed);
  2204. *fput_needed = 0;
  2205. return ERR_PTR(-EBADF);
  2206. }
  2207. return file->private_data;
  2208. }
  2209. static int perf_event_set_output(struct perf_event *event,
  2210. struct perf_event *output_event);
  2211. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  2212. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2213. {
  2214. struct perf_event *event = file->private_data;
  2215. void (*func)(struct perf_event *);
  2216. u32 flags = arg;
  2217. switch (cmd) {
  2218. case PERF_EVENT_IOC_ENABLE:
  2219. func = perf_event_enable;
  2220. break;
  2221. case PERF_EVENT_IOC_DISABLE:
  2222. func = perf_event_disable;
  2223. break;
  2224. case PERF_EVENT_IOC_RESET:
  2225. func = perf_event_reset;
  2226. break;
  2227. case PERF_EVENT_IOC_REFRESH:
  2228. return perf_event_refresh(event, arg);
  2229. case PERF_EVENT_IOC_PERIOD:
  2230. return perf_event_period(event, (u64 __user *)arg);
  2231. case PERF_EVENT_IOC_SET_OUTPUT:
  2232. {
  2233. struct perf_event *output_event = NULL;
  2234. int fput_needed = 0;
  2235. int ret;
  2236. if (arg != -1) {
  2237. output_event = perf_fget_light(arg, &fput_needed);
  2238. if (IS_ERR(output_event))
  2239. return PTR_ERR(output_event);
  2240. }
  2241. ret = perf_event_set_output(event, output_event);
  2242. if (output_event)
  2243. fput_light(output_event->filp, fput_needed);
  2244. return ret;
  2245. }
  2246. case PERF_EVENT_IOC_SET_FILTER:
  2247. return perf_event_set_filter(event, (void __user *)arg);
  2248. default:
  2249. return -ENOTTY;
  2250. }
  2251. if (flags & PERF_IOC_FLAG_GROUP)
  2252. perf_event_for_each(event, func);
  2253. else
  2254. perf_event_for_each_child(event, func);
  2255. return 0;
  2256. }
  2257. int perf_event_task_enable(void)
  2258. {
  2259. struct perf_event *event;
  2260. mutex_lock(&current->perf_event_mutex);
  2261. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  2262. perf_event_for_each_child(event, perf_event_enable);
  2263. mutex_unlock(&current->perf_event_mutex);
  2264. return 0;
  2265. }
  2266. int perf_event_task_disable(void)
  2267. {
  2268. struct perf_event *event;
  2269. mutex_lock(&current->perf_event_mutex);
  2270. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  2271. perf_event_for_each_child(event, perf_event_disable);
  2272. mutex_unlock(&current->perf_event_mutex);
  2273. return 0;
  2274. }
  2275. #ifndef PERF_EVENT_INDEX_OFFSET
  2276. # define PERF_EVENT_INDEX_OFFSET 0
  2277. #endif
  2278. static int perf_event_index(struct perf_event *event)
  2279. {
  2280. if (event->hw.state & PERF_HES_STOPPED)
  2281. return 0;
  2282. if (event->state != PERF_EVENT_STATE_ACTIVE)
  2283. return 0;
  2284. return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
  2285. }
  2286. /*
  2287. * Callers need to ensure there can be no nesting of this function, otherwise
  2288. * the seqlock logic goes bad. We can not serialize this because the arch
  2289. * code calls this from NMI context.
  2290. */
  2291. void perf_event_update_userpage(struct perf_event *event)
  2292. {
  2293. struct perf_event_mmap_page *userpg;
  2294. struct perf_buffer *buffer;
  2295. rcu_read_lock();
  2296. buffer = rcu_dereference(event->buffer);
  2297. if (!buffer)
  2298. goto unlock;
  2299. userpg = buffer->user_page;
  2300. /*
  2301. * Disable preemption so as to not let the corresponding user-space
  2302. * spin too long if we get preempted.
  2303. */
  2304. preempt_disable();
  2305. ++userpg->lock;
  2306. barrier();
  2307. userpg->index = perf_event_index(event);
  2308. userpg->offset = perf_event_count(event);
  2309. if (event->state == PERF_EVENT_STATE_ACTIVE)
  2310. userpg->offset -= local64_read(&event->hw.prev_count);
  2311. userpg->time_enabled = event->total_time_enabled +
  2312. atomic64_read(&event->child_total_time_enabled);
  2313. userpg->time_running = event->total_time_running +
  2314. atomic64_read(&event->child_total_time_running);
  2315. barrier();
  2316. ++userpg->lock;
  2317. preempt_enable();
  2318. unlock:
  2319. rcu_read_unlock();
  2320. }
  2321. static unsigned long perf_data_size(struct perf_buffer *buffer);
  2322. static void
  2323. perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
  2324. {
  2325. long max_size = perf_data_size(buffer);
  2326. if (watermark)
  2327. buffer->watermark = min(max_size, watermark);
  2328. if (!buffer->watermark)
  2329. buffer->watermark = max_size / 2;
  2330. if (flags & PERF_BUFFER_WRITABLE)
  2331. buffer->writable = 1;
  2332. atomic_set(&buffer->refcount, 1);
  2333. }
  2334. #ifndef CONFIG_PERF_USE_VMALLOC
  2335. /*
  2336. * Back perf_mmap() with regular GFP_KERNEL-0 pages.
  2337. */
  2338. static struct page *
  2339. perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
  2340. {
  2341. if (pgoff > buffer->nr_pages)
  2342. return NULL;
  2343. if (pgoff == 0)
  2344. return virt_to_page(buffer->user_page);
  2345. return virt_to_page(buffer->data_pages[pgoff - 1]);
  2346. }
  2347. static void *perf_mmap_alloc_page(int cpu)
  2348. {
  2349. struct page *page;
  2350. int node;
  2351. node = (cpu == -1) ? cpu : cpu_to_node(cpu);
  2352. page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  2353. if (!page)
  2354. return NULL;
  2355. return page_address(page);
  2356. }
  2357. static struct perf_buffer *
  2358. perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
  2359. {
  2360. struct perf_buffer *buffer;
  2361. unsigned long size;
  2362. int i;
  2363. size = sizeof(struct perf_buffer);
  2364. size += nr_pages * sizeof(void *);
  2365. buffer = kzalloc(size, GFP_KERNEL);
  2366. if (!buffer)
  2367. goto fail;
  2368. buffer->user_page = perf_mmap_alloc_page(cpu);
  2369. if (!buffer->user_page)
  2370. goto fail_user_page;
  2371. for (i = 0; i < nr_pages; i++) {
  2372. buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
  2373. if (!buffer->data_pages[i])
  2374. goto fail_data_pages;
  2375. }
  2376. buffer->nr_pages = nr_pages;
  2377. perf_buffer_init(buffer, watermark, flags);
  2378. return buffer;
  2379. fail_data_pages:
  2380. for (i--; i >= 0; i--)
  2381. free_page((unsigned long)buffer->data_pages[i]);
  2382. free_page((unsigned long)buffer->user_page);
  2383. fail_user_page:
  2384. kfree(buffer);
  2385. fail:
  2386. return NULL;
  2387. }
  2388. static void perf_mmap_free_page(unsigned long addr)
  2389. {
  2390. struct page *page = virt_to_page((void *)addr);
  2391. page->mapping = NULL;
  2392. __free_page(page);
  2393. }
  2394. static void perf_buffer_free(struct perf_buffer *buffer)
  2395. {
  2396. int i;
  2397. perf_mmap_free_page((unsigned long)buffer->user_page);
  2398. for (i = 0; i < buffer->nr_pages; i++)
  2399. perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
  2400. kfree(buffer);
  2401. }
  2402. static inline int page_order(struct perf_buffer *buffer)
  2403. {
  2404. return 0;
  2405. }
  2406. #else
  2407. /*
  2408. * Back perf_mmap() with vmalloc memory.
  2409. *
  2410. * Required for architectures that have d-cache aliasing issues.
  2411. */
  2412. static inline int page_order(struct perf_buffer *buffer)
  2413. {
  2414. return buffer->page_order;
  2415. }
  2416. static struct page *
  2417. perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
  2418. {
  2419. if (pgoff > (1UL << page_order(buffer)))
  2420. return NULL;
  2421. return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
  2422. }
  2423. static void perf_mmap_unmark_page(void *addr)
  2424. {
  2425. struct page *page = vmalloc_to_page(addr);
  2426. page->mapping = NULL;
  2427. }
  2428. static void perf_buffer_free_work(struct work_struct *work)
  2429. {
  2430. struct perf_buffer *buffer;
  2431. void *base;
  2432. int i, nr;
  2433. buffer = container_of(work, struct perf_buffer, work);
  2434. nr = 1 << page_order(buffer);
  2435. base = buffer->user_page;
  2436. for (i = 0; i < nr + 1; i++)
  2437. perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  2438. vfree(base);
  2439. kfree(buffer);
  2440. }
  2441. static void perf_buffer_free(struct perf_buffer *buffer)
  2442. {
  2443. schedule_work(&buffer->work);
  2444. }
  2445. static struct perf_buffer *
  2446. perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
  2447. {
  2448. struct perf_buffer *buffer;
  2449. unsigned long size;
  2450. void *all_buf;
  2451. size = sizeof(struct perf_buffer);
  2452. size += sizeof(void *);
  2453. buffer = kzalloc(size, GFP_KERNEL);
  2454. if (!buffer)
  2455. goto fail;
  2456. INIT_WORK(&buffer->work, perf_buffer_free_work);
  2457. all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  2458. if (!all_buf)
  2459. goto fail_all_buf;
  2460. buffer->user_page = all_buf;
  2461. buffer->data_pages[0] = all_buf + PAGE_SIZE;
  2462. buffer->page_order = ilog2(nr_pages);
  2463. buffer->nr_pages = 1;
  2464. perf_buffer_init(buffer, watermark, flags);
  2465. return buffer;
  2466. fail_all_buf:
  2467. kfree(buffer);
  2468. fail:
  2469. return NULL;
  2470. }
  2471. #endif
  2472. static unsigned long perf_data_size(struct perf_buffer *buffer)
  2473. {
  2474. return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
  2475. }
  2476. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  2477. {
  2478. struct perf_event *event = vma->vm_file->private_data;
  2479. struct perf_buffer *buffer;
  2480. int ret = VM_FAULT_SIGBUS;
  2481. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  2482. if (vmf->pgoff == 0)
  2483. ret = 0;
  2484. return ret;
  2485. }
  2486. rcu_read_lock();
  2487. buffer = rcu_dereference(event->buffer);
  2488. if (!buffer)
  2489. goto unlock;
  2490. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  2491. goto unlock;
  2492. vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
  2493. if (!vmf->page)
  2494. goto unlock;
  2495. get_page(vmf->page);
  2496. vmf->page->mapping = vma->vm_file->f_mapping;
  2497. vmf->page->index = vmf->pgoff;
  2498. ret = 0;
  2499. unlock:
  2500. rcu_read_unlock();
  2501. return ret;
  2502. }
  2503. static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
  2504. {
  2505. struct perf_buffer *buffer;
  2506. buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
  2507. perf_buffer_free(buffer);
  2508. }
  2509. static struct perf_buffer *perf_buffer_get(struct perf_event *event)
  2510. {
  2511. struct perf_buffer *buffer;
  2512. rcu_read_lock();
  2513. buffer = rcu_dereference(event->buffer);
  2514. if (buffer) {
  2515. if (!atomic_inc_not_zero(&buffer->refcount))
  2516. buffer = NULL;
  2517. }
  2518. rcu_read_unlock();
  2519. return buffer;
  2520. }
  2521. static void perf_buffer_put(struct perf_buffer *buffer)
  2522. {
  2523. if (!atomic_dec_and_test(&buffer->refcount))
  2524. return;
  2525. call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
  2526. }
  2527. static void perf_mmap_open(struct vm_area_struct *vma)
  2528. {
  2529. struct perf_event *event = vma->vm_file->private_data;
  2530. atomic_inc(&event->mmap_count);
  2531. }
  2532. static void perf_mmap_close(struct vm_area_struct *vma)
  2533. {
  2534. struct perf_event *event = vma->vm_file->private_data;
  2535. if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
  2536. unsigned long size = perf_data_size(event->buffer);
  2537. struct user_struct *user = event->mmap_user;
  2538. struct perf_buffer *buffer = event->buffer;
  2539. atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
  2540. vma->vm_mm->locked_vm -= event->mmap_locked;
  2541. rcu_assign_pointer(event->buffer, NULL);
  2542. mutex_unlock(&event->mmap_mutex);
  2543. perf_buffer_put(buffer);
  2544. free_uid(user);
  2545. }
  2546. }
  2547. static const struct vm_operations_struct perf_mmap_vmops = {
  2548. .open = perf_mmap_open,
  2549. .close = perf_mmap_close,
  2550. .fault = perf_mmap_fault,
  2551. .page_mkwrite = perf_mmap_fault,
  2552. };
  2553. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  2554. {
  2555. struct perf_event *event = file->private_data;
  2556. unsigned long user_locked, user_lock_limit;
  2557. struct user_struct *user = current_user();
  2558. unsigned long locked, lock_limit;
  2559. struct perf_buffer *buffer;
  2560. unsigned long vma_size;
  2561. unsigned long nr_pages;
  2562. long user_extra, extra;
  2563. int ret = 0, flags = 0;
  2564. /*
  2565. * Don't allow mmap() of inherited per-task counters. This would
  2566. * create a performance issue due to all children writing to the
  2567. * same buffer.
  2568. */
  2569. if (event->cpu == -1 && event->attr.inherit)
  2570. return -EINVAL;
  2571. if (!(vma->vm_flags & VM_SHARED))
  2572. return -EINVAL;
  2573. vma_size = vma->vm_end - vma->vm_start;
  2574. nr_pages = (vma_size / PAGE_SIZE) - 1;
  2575. /*
  2576. * If we have buffer pages ensure they're a power-of-two number, so we
  2577. * can do bitmasks instead of modulo.
  2578. */
  2579. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  2580. return -EINVAL;
  2581. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  2582. return -EINVAL;
  2583. if (vma->vm_pgoff != 0)
  2584. return -EINVAL;
  2585. WARN_ON_ONCE(event->ctx->parent_ctx);
  2586. mutex_lock(&event->mmap_mutex);
  2587. if (event->buffer) {
  2588. if (event->buffer->nr_pages == nr_pages)
  2589. atomic_inc(&event->buffer->refcount);
  2590. else
  2591. ret = -EINVAL;
  2592. goto unlock;
  2593. }
  2594. user_extra = nr_pages + 1;
  2595. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  2596. /*
  2597. * Increase the limit linearly with more CPUs:
  2598. */
  2599. user_lock_limit *= num_online_cpus();
  2600. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  2601. extra = 0;
  2602. if (user_locked > user_lock_limit)
  2603. extra = user_locked - user_lock_limit;
  2604. lock_limit = rlimit(RLIMIT_MEMLOCK);
  2605. lock_limit >>= PAGE_SHIFT;
  2606. locked = vma->vm_mm->locked_vm + extra;
  2607. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  2608. !capable(CAP_IPC_LOCK)) {
  2609. ret = -EPERM;
  2610. goto unlock;
  2611. }
  2612. WARN_ON(event->buffer);
  2613. if (vma->vm_flags & VM_WRITE)
  2614. flags |= PERF_BUFFER_WRITABLE;
  2615. buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
  2616. event->cpu, flags);
  2617. if (!buffer) {
  2618. ret = -ENOMEM;
  2619. goto unlock;
  2620. }
  2621. rcu_assign_pointer(event->buffer, buffer);
  2622. atomic_long_add(user_extra, &user->locked_vm);
  2623. event->mmap_locked = extra;
  2624. event->mmap_user = get_current_user();
  2625. vma->vm_mm->locked_vm += event->mmap_locked;
  2626. unlock:
  2627. if (!ret)
  2628. atomic_inc(&event->mmap_count);
  2629. mutex_unlock(&event->mmap_mutex);
  2630. vma->vm_flags |= VM_RESERVED;
  2631. vma->vm_ops = &perf_mmap_vmops;
  2632. return ret;
  2633. }
  2634. static int perf_fasync(int fd, struct file *filp, int on)
  2635. {
  2636. struct inode *inode = filp->f_path.dentry->d_inode;
  2637. struct perf_event *event = filp->private_data;
  2638. int retval;
  2639. mutex_lock(&inode->i_mutex);
  2640. retval = fasync_helper(fd, filp, on, &event->fasync);
  2641. mutex_unlock(&inode->i_mutex);
  2642. if (retval < 0)
  2643. return retval;
  2644. return 0;
  2645. }
  2646. static const struct file_operations perf_fops = {
  2647. .llseek = no_llseek,
  2648. .release = perf_release,
  2649. .read = perf_read,
  2650. .poll = perf_poll,
  2651. .unlocked_ioctl = perf_ioctl,
  2652. .compat_ioctl = perf_ioctl,
  2653. .mmap = perf_mmap,
  2654. .fasync = perf_fasync,
  2655. };
  2656. /*
  2657. * Perf event wakeup
  2658. *
  2659. * If there's data, ensure we set the poll() state and publish everything
  2660. * to user-space before waking everybody up.
  2661. */
  2662. void perf_event_wakeup(struct perf_event *event)
  2663. {
  2664. wake_up_all(&event->waitq);
  2665. if (event->pending_kill) {
  2666. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  2667. event->pending_kill = 0;
  2668. }
  2669. }
  2670. static void perf_pending_event(struct irq_work *entry)
  2671. {
  2672. struct perf_event *event = container_of(entry,
  2673. struct perf_event, pending);
  2674. if (event->pending_disable) {
  2675. event->pending_disable = 0;
  2676. __perf_event_disable(event);
  2677. }
  2678. if (event->pending_wakeup) {
  2679. event->pending_wakeup = 0;
  2680. perf_event_wakeup(event);
  2681. }
  2682. }
  2683. /*
  2684. * We assume there is only KVM supporting the callbacks.
  2685. * Later on, we might change it to a list if there is
  2686. * another virtualization implementation supporting the callbacks.
  2687. */
  2688. struct perf_guest_info_callbacks *perf_guest_cbs;
  2689. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2690. {
  2691. perf_guest_cbs = cbs;
  2692. return 0;
  2693. }
  2694. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  2695. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2696. {
  2697. perf_guest_cbs = NULL;
  2698. return 0;
  2699. }
  2700. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  2701. /*
  2702. * Output
  2703. */
  2704. static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
  2705. unsigned long offset, unsigned long head)
  2706. {
  2707. unsigned long mask;
  2708. if (!buffer->writable)
  2709. return true;
  2710. mask = perf_data_size(buffer) - 1;
  2711. offset = (offset - tail) & mask;
  2712. head = (head - tail) & mask;
  2713. if ((int)(head - offset) < 0)
  2714. return false;
  2715. return true;
  2716. }
  2717. static void perf_output_wakeup(struct perf_output_handle *handle)
  2718. {
  2719. atomic_set(&handle->buffer->poll, POLL_IN);
  2720. if (handle->nmi) {
  2721. handle->event->pending_wakeup = 1;
  2722. irq_work_queue(&handle->event->pending);
  2723. } else
  2724. perf_event_wakeup(handle->event);
  2725. }
  2726. /*
  2727. * We need to ensure a later event_id doesn't publish a head when a former
  2728. * event isn't done writing. However since we need to deal with NMIs we
  2729. * cannot fully serialize things.
  2730. *
  2731. * We only publish the head (and generate a wakeup) when the outer-most
  2732. * event completes.
  2733. */
  2734. static void perf_output_get_handle(struct perf_output_handle *handle)
  2735. {
  2736. struct perf_buffer *buffer = handle->buffer;
  2737. preempt_disable();
  2738. local_inc(&buffer->nest);
  2739. handle->wakeup = local_read(&buffer->wakeup);
  2740. }
  2741. static void perf_output_put_handle(struct perf_output_handle *handle)
  2742. {
  2743. struct perf_buffer *buffer = handle->buffer;
  2744. unsigned long head;
  2745. again:
  2746. head = local_read(&buffer->head);
  2747. /*
  2748. * IRQ/NMI can happen here, which means we can miss a head update.
  2749. */
  2750. if (!local_dec_and_test(&buffer->nest))
  2751. goto out;
  2752. /*
  2753. * Publish the known good head. Rely on the full barrier implied
  2754. * by atomic_dec_and_test() order the buffer->head read and this
  2755. * write.
  2756. */
  2757. buffer->user_page->data_head = head;
  2758. /*
  2759. * Now check if we missed an update, rely on the (compiler)
  2760. * barrier in atomic_dec_and_test() to re-read buffer->head.
  2761. */
  2762. if (unlikely(head != local_read(&buffer->head))) {
  2763. local_inc(&buffer->nest);
  2764. goto again;
  2765. }
  2766. if (handle->wakeup != local_read(&buffer->wakeup))
  2767. perf_output_wakeup(handle);
  2768. out:
  2769. preempt_enable();
  2770. }
  2771. __always_inline void perf_output_copy(struct perf_output_handle *handle,
  2772. const void *buf, unsigned int len)
  2773. {
  2774. do {
  2775. unsigned long size = min_t(unsigned long, handle->size, len);
  2776. memcpy(handle->addr, buf, size);
  2777. len -= size;
  2778. handle->addr += size;
  2779. buf += size;
  2780. handle->size -= size;
  2781. if (!handle->size) {
  2782. struct perf_buffer *buffer = handle->buffer;
  2783. handle->page++;
  2784. handle->page &= buffer->nr_pages - 1;
  2785. handle->addr = buffer->data_pages[handle->page];
  2786. handle->size = PAGE_SIZE << page_order(buffer);
  2787. }
  2788. } while (len);
  2789. }
  2790. static void __perf_event_header__init_id(struct perf_event_header *header,
  2791. struct perf_sample_data *data,
  2792. struct perf_event *event)
  2793. {
  2794. u64 sample_type = event->attr.sample_type;
  2795. data->type = sample_type;
  2796. header->size += event->id_header_size;
  2797. if (sample_type & PERF_SAMPLE_TID) {
  2798. /* namespace issues */
  2799. data->tid_entry.pid = perf_event_pid(event, current);
  2800. data->tid_entry.tid = perf_event_tid(event, current);
  2801. }
  2802. if (sample_type & PERF_SAMPLE_TIME)
  2803. data->time = perf_clock();
  2804. if (sample_type & PERF_SAMPLE_ID)
  2805. data->id = primary_event_id(event);
  2806. if (sample_type & PERF_SAMPLE_STREAM_ID)
  2807. data->stream_id = event->id;
  2808. if (sample_type & PERF_SAMPLE_CPU) {
  2809. data->cpu_entry.cpu = raw_smp_processor_id();
  2810. data->cpu_entry.reserved = 0;
  2811. }
  2812. }
  2813. static void perf_event_header__init_id(struct perf_event_header *header,
  2814. struct perf_sample_data *data,
  2815. struct perf_event *event)
  2816. {
  2817. if (event->attr.sample_id_all)
  2818. __perf_event_header__init_id(header, data, event);
  2819. }
  2820. static void __perf_event__output_id_sample(struct perf_output_handle *handle,
  2821. struct perf_sample_data *data)
  2822. {
  2823. u64 sample_type = data->type;
  2824. if (sample_type & PERF_SAMPLE_TID)
  2825. perf_output_put(handle, data->tid_entry);
  2826. if (sample_type & PERF_SAMPLE_TIME)
  2827. perf_output_put(handle, data->time);
  2828. if (sample_type & PERF_SAMPLE_ID)
  2829. perf_output_put(handle, data->id);
  2830. if (sample_type & PERF_SAMPLE_STREAM_ID)
  2831. perf_output_put(handle, data->stream_id);
  2832. if (sample_type & PERF_SAMPLE_CPU)
  2833. perf_output_put(handle, data->cpu_entry);
  2834. }
  2835. static void perf_event__output_id_sample(struct perf_event *event,
  2836. struct perf_output_handle *handle,
  2837. struct perf_sample_data *sample)
  2838. {
  2839. if (event->attr.sample_id_all)
  2840. __perf_event__output_id_sample(handle, sample);
  2841. }
  2842. int perf_output_begin(struct perf_output_handle *handle,
  2843. struct perf_event *event, unsigned int size,
  2844. int nmi, int sample)
  2845. {
  2846. struct perf_buffer *buffer;
  2847. unsigned long tail, offset, head;
  2848. int have_lost;
  2849. struct perf_sample_data sample_data;
  2850. struct {
  2851. struct perf_event_header header;
  2852. u64 id;
  2853. u64 lost;
  2854. } lost_event;
  2855. rcu_read_lock();
  2856. /*
  2857. * For inherited events we send all the output towards the parent.
  2858. */
  2859. if (event->parent)
  2860. event = event->parent;
  2861. buffer = rcu_dereference(event->buffer);
  2862. if (!buffer)
  2863. goto out;
  2864. handle->buffer = buffer;
  2865. handle->event = event;
  2866. handle->nmi = nmi;
  2867. handle->sample = sample;
  2868. if (!buffer->nr_pages)
  2869. goto out;
  2870. have_lost = local_read(&buffer->lost);
  2871. if (have_lost) {
  2872. lost_event.header.size = sizeof(lost_event);
  2873. perf_event_header__init_id(&lost_event.header, &sample_data,
  2874. event);
  2875. size += lost_event.header.size;
  2876. }
  2877. perf_output_get_handle(handle);
  2878. do {
  2879. /*
  2880. * Userspace could choose to issue a mb() before updating the
  2881. * tail pointer. So that all reads will be completed before the
  2882. * write is issued.
  2883. */
  2884. tail = ACCESS_ONCE(buffer->user_page->data_tail);
  2885. smp_rmb();
  2886. offset = head = local_read(&buffer->head);
  2887. head += size;
  2888. if (unlikely(!perf_output_space(buffer, tail, offset, head)))
  2889. goto fail;
  2890. } while (local_cmpxchg(&buffer->head, offset, head) != offset);
  2891. if (head - local_read(&buffer->wakeup) > buffer->watermark)
  2892. local_add(buffer->watermark, &buffer->wakeup);
  2893. handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
  2894. handle->page &= buffer->nr_pages - 1;
  2895. handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
  2896. handle->addr = buffer->data_pages[handle->page];
  2897. handle->addr += handle->size;
  2898. handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
  2899. if (have_lost) {
  2900. lost_event.header.type = PERF_RECORD_LOST;
  2901. lost_event.header.misc = 0;
  2902. lost_event.id = event->id;
  2903. lost_event.lost = local_xchg(&buffer->lost, 0);
  2904. perf_output_put(handle, lost_event);
  2905. perf_event__output_id_sample(event, handle, &sample_data);
  2906. }
  2907. return 0;
  2908. fail:
  2909. local_inc(&buffer->lost);
  2910. perf_output_put_handle(handle);
  2911. out:
  2912. rcu_read_unlock();
  2913. return -ENOSPC;
  2914. }
  2915. void perf_output_end(struct perf_output_handle *handle)
  2916. {
  2917. struct perf_event *event = handle->event;
  2918. struct perf_buffer *buffer = handle->buffer;
  2919. int wakeup_events = event->attr.wakeup_events;
  2920. if (handle->sample && wakeup_events) {
  2921. int events = local_inc_return(&buffer->events);
  2922. if (events >= wakeup_events) {
  2923. local_sub(wakeup_events, &buffer->events);
  2924. local_inc(&buffer->wakeup);
  2925. }
  2926. }
  2927. perf_output_put_handle(handle);
  2928. rcu_read_unlock();
  2929. }
  2930. static void perf_output_read_one(struct perf_output_handle *handle,
  2931. struct perf_event *event,
  2932. u64 enabled, u64 running)
  2933. {
  2934. u64 read_format = event->attr.read_format;
  2935. u64 values[4];
  2936. int n = 0;
  2937. values[n++] = perf_event_count(event);
  2938. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  2939. values[n++] = enabled +
  2940. atomic64_read(&event->child_total_time_enabled);
  2941. }
  2942. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  2943. values[n++] = running +
  2944. atomic64_read(&event->child_total_time_running);
  2945. }
  2946. if (read_format & PERF_FORMAT_ID)
  2947. values[n++] = primary_event_id(event);
  2948. perf_output_copy(handle, values, n * sizeof(u64));
  2949. }
  2950. /*
  2951. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  2952. */
  2953. static void perf_output_read_group(struct perf_output_handle *handle,
  2954. struct perf_event *event,
  2955. u64 enabled, u64 running)
  2956. {
  2957. struct perf_event *leader = event->group_leader, *sub;
  2958. u64 read_format = event->attr.read_format;
  2959. u64 values[5];
  2960. int n = 0;
  2961. values[n++] = 1 + leader->nr_siblings;
  2962. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2963. values[n++] = enabled;
  2964. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2965. values[n++] = running;
  2966. if (leader != event)
  2967. leader->pmu->read(leader);
  2968. values[n++] = perf_event_count(leader);
  2969. if (read_format & PERF_FORMAT_ID)
  2970. values[n++] = primary_event_id(leader);
  2971. perf_output_copy(handle, values, n * sizeof(u64));
  2972. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2973. n = 0;
  2974. if (sub != event)
  2975. sub->pmu->read(sub);
  2976. values[n++] = perf_event_count(sub);
  2977. if (read_format & PERF_FORMAT_ID)
  2978. values[n++] = primary_event_id(sub);
  2979. perf_output_copy(handle, values, n * sizeof(u64));
  2980. }
  2981. }
  2982. #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
  2983. PERF_FORMAT_TOTAL_TIME_RUNNING)
  2984. static void perf_output_read(struct perf_output_handle *handle,
  2985. struct perf_event *event)
  2986. {
  2987. u64 enabled = 0, running = 0, now, ctx_time;
  2988. u64 read_format = event->attr.read_format;
  2989. /*
  2990. * compute total_time_enabled, total_time_running
  2991. * based on snapshot values taken when the event
  2992. * was last scheduled in.
  2993. *
  2994. * we cannot simply called update_context_time()
  2995. * because of locking issue as we are called in
  2996. * NMI context
  2997. */
  2998. if (read_format & PERF_FORMAT_TOTAL_TIMES) {
  2999. now = perf_clock();
  3000. ctx_time = event->shadow_ctx_time + now;
  3001. enabled = ctx_time - event->tstamp_enabled;
  3002. running = ctx_time - event->tstamp_running;
  3003. }
  3004. if (event->attr.read_format & PERF_FORMAT_GROUP)
  3005. perf_output_read_group(handle, event, enabled, running);
  3006. else
  3007. perf_output_read_one(handle, event, enabled, running);
  3008. }
  3009. void perf_output_sample(struct perf_output_handle *handle,
  3010. struct perf_event_header *header,
  3011. struct perf_sample_data *data,
  3012. struct perf_event *event)
  3013. {
  3014. u64 sample_type = data->type;
  3015. perf_output_put(handle, *header);
  3016. if (sample_type & PERF_SAMPLE_IP)
  3017. perf_output_put(handle, data->ip);
  3018. if (sample_type & PERF_SAMPLE_TID)
  3019. perf_output_put(handle, data->tid_entry);
  3020. if (sample_type & PERF_SAMPLE_TIME)
  3021. perf_output_put(handle, data->time);
  3022. if (sample_type & PERF_SAMPLE_ADDR)
  3023. perf_output_put(handle, data->addr);
  3024. if (sample_type & PERF_SAMPLE_ID)
  3025. perf_output_put(handle, data->id);
  3026. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3027. perf_output_put(handle, data->stream_id);
  3028. if (sample_type & PERF_SAMPLE_CPU)
  3029. perf_output_put(handle, data->cpu_entry);
  3030. if (sample_type & PERF_SAMPLE_PERIOD)
  3031. perf_output_put(handle, data->period);
  3032. if (sample_type & PERF_SAMPLE_READ)
  3033. perf_output_read(handle, event);
  3034. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  3035. if (data->callchain) {
  3036. int size = 1;
  3037. if (data->callchain)
  3038. size += data->callchain->nr;
  3039. size *= sizeof(u64);
  3040. perf_output_copy(handle, data->callchain, size);
  3041. } else {
  3042. u64 nr = 0;
  3043. perf_output_put(handle, nr);
  3044. }
  3045. }
  3046. if (sample_type & PERF_SAMPLE_RAW) {
  3047. if (data->raw) {
  3048. perf_output_put(handle, data->raw->size);
  3049. perf_output_copy(handle, data->raw->data,
  3050. data->raw->size);
  3051. } else {
  3052. struct {
  3053. u32 size;
  3054. u32 data;
  3055. } raw = {
  3056. .size = sizeof(u32),
  3057. .data = 0,
  3058. };
  3059. perf_output_put(handle, raw);
  3060. }
  3061. }
  3062. }
  3063. void perf_prepare_sample(struct perf_event_header *header,
  3064. struct perf_sample_data *data,
  3065. struct perf_event *event,
  3066. struct pt_regs *regs)
  3067. {
  3068. u64 sample_type = event->attr.sample_type;
  3069. header->type = PERF_RECORD_SAMPLE;
  3070. header->size = sizeof(*header) + event->header_size;
  3071. header->misc = 0;
  3072. header->misc |= perf_misc_flags(regs);
  3073. __perf_event_header__init_id(header, data, event);
  3074. if (sample_type & PERF_SAMPLE_IP)
  3075. data->ip = perf_instruction_pointer(regs);
  3076. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  3077. int size = 1;
  3078. data->callchain = perf_callchain(regs);
  3079. if (data->callchain)
  3080. size += data->callchain->nr;
  3081. header->size += size * sizeof(u64);
  3082. }
  3083. if (sample_type & PERF_SAMPLE_RAW) {
  3084. int size = sizeof(u32);
  3085. if (data->raw)
  3086. size += data->raw->size;
  3087. else
  3088. size += sizeof(u32);
  3089. WARN_ON_ONCE(size & (sizeof(u64)-1));
  3090. header->size += size;
  3091. }
  3092. }
  3093. static void perf_event_output(struct perf_event *event, int nmi,
  3094. struct perf_sample_data *data,
  3095. struct pt_regs *regs)
  3096. {
  3097. struct perf_output_handle handle;
  3098. struct perf_event_header header;
  3099. /* protect the callchain buffers */
  3100. rcu_read_lock();
  3101. perf_prepare_sample(&header, data, event, regs);
  3102. if (perf_output_begin(&handle, event, header.size, nmi, 1))
  3103. goto exit;
  3104. perf_output_sample(&handle, &header, data, event);
  3105. perf_output_end(&handle);
  3106. exit:
  3107. rcu_read_unlock();
  3108. }
  3109. /*
  3110. * read event_id
  3111. */
  3112. struct perf_read_event {
  3113. struct perf_event_header header;
  3114. u32 pid;
  3115. u32 tid;
  3116. };
  3117. static void
  3118. perf_event_read_event(struct perf_event *event,
  3119. struct task_struct *task)
  3120. {
  3121. struct perf_output_handle handle;
  3122. struct perf_sample_data sample;
  3123. struct perf_read_event read_event = {
  3124. .header = {
  3125. .type = PERF_RECORD_READ,
  3126. .misc = 0,
  3127. .size = sizeof(read_event) + event->read_size,
  3128. },
  3129. .pid = perf_event_pid(event, task),
  3130. .tid = perf_event_tid(event, task),
  3131. };
  3132. int ret;
  3133. perf_event_header__init_id(&read_event.header, &sample, event);
  3134. ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
  3135. if (ret)
  3136. return;
  3137. perf_output_put(&handle, read_event);
  3138. perf_output_read(&handle, event);
  3139. perf_event__output_id_sample(event, &handle, &sample);
  3140. perf_output_end(&handle);
  3141. }
  3142. /*
  3143. * task tracking -- fork/exit
  3144. *
  3145. * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
  3146. */
  3147. struct perf_task_event {
  3148. struct task_struct *task;
  3149. struct perf_event_context *task_ctx;
  3150. struct {
  3151. struct perf_event_header header;
  3152. u32 pid;
  3153. u32 ppid;
  3154. u32 tid;
  3155. u32 ptid;
  3156. u64 time;
  3157. } event_id;
  3158. };
  3159. static void perf_event_task_output(struct perf_event *event,
  3160. struct perf_task_event *task_event)
  3161. {
  3162. struct perf_output_handle handle;
  3163. struct perf_sample_data sample;
  3164. struct task_struct *task = task_event->task;
  3165. int ret, size = task_event->event_id.header.size;
  3166. perf_event_header__init_id(&task_event->event_id.header, &sample, event);
  3167. ret = perf_output_begin(&handle, event,
  3168. task_event->event_id.header.size, 0, 0);
  3169. if (ret)
  3170. goto out;
  3171. task_event->event_id.pid = perf_event_pid(event, task);
  3172. task_event->event_id.ppid = perf_event_pid(event, current);
  3173. task_event->event_id.tid = perf_event_tid(event, task);
  3174. task_event->event_id.ptid = perf_event_tid(event, current);
  3175. perf_output_put(&handle, task_event->event_id);
  3176. perf_event__output_id_sample(event, &handle, &sample);
  3177. perf_output_end(&handle);
  3178. out:
  3179. task_event->event_id.header.size = size;
  3180. }
  3181. static int perf_event_task_match(struct perf_event *event)
  3182. {
  3183. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3184. return 0;
  3185. if (!event_filter_match(event))
  3186. return 0;
  3187. if (event->attr.comm || event->attr.mmap ||
  3188. event->attr.mmap_data || event->attr.task)
  3189. return 1;
  3190. return 0;
  3191. }
  3192. static void perf_event_task_ctx(struct perf_event_context *ctx,
  3193. struct perf_task_event *task_event)
  3194. {
  3195. struct perf_event *event;
  3196. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3197. if (perf_event_task_match(event))
  3198. perf_event_task_output(event, task_event);
  3199. }
  3200. }
  3201. static void perf_event_task_event(struct perf_task_event *task_event)
  3202. {
  3203. struct perf_cpu_context *cpuctx;
  3204. struct perf_event_context *ctx;
  3205. struct pmu *pmu;
  3206. int ctxn;
  3207. rcu_read_lock();
  3208. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3209. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3210. if (cpuctx->active_pmu != pmu)
  3211. goto next;
  3212. perf_event_task_ctx(&cpuctx->ctx, task_event);
  3213. ctx = task_event->task_ctx;
  3214. if (!ctx) {
  3215. ctxn = pmu->task_ctx_nr;
  3216. if (ctxn < 0)
  3217. goto next;
  3218. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3219. }
  3220. if (ctx)
  3221. perf_event_task_ctx(ctx, task_event);
  3222. next:
  3223. put_cpu_ptr(pmu->pmu_cpu_context);
  3224. }
  3225. rcu_read_unlock();
  3226. }
  3227. static void perf_event_task(struct task_struct *task,
  3228. struct perf_event_context *task_ctx,
  3229. int new)
  3230. {
  3231. struct perf_task_event task_event;
  3232. if (!atomic_read(&nr_comm_events) &&
  3233. !atomic_read(&nr_mmap_events) &&
  3234. !atomic_read(&nr_task_events))
  3235. return;
  3236. task_event = (struct perf_task_event){
  3237. .task = task,
  3238. .task_ctx = task_ctx,
  3239. .event_id = {
  3240. .header = {
  3241. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  3242. .misc = 0,
  3243. .size = sizeof(task_event.event_id),
  3244. },
  3245. /* .pid */
  3246. /* .ppid */
  3247. /* .tid */
  3248. /* .ptid */
  3249. .time = perf_clock(),
  3250. },
  3251. };
  3252. perf_event_task_event(&task_event);
  3253. }
  3254. void perf_event_fork(struct task_struct *task)
  3255. {
  3256. perf_event_task(task, NULL, 1);
  3257. }
  3258. /*
  3259. * comm tracking
  3260. */
  3261. struct perf_comm_event {
  3262. struct task_struct *task;
  3263. char *comm;
  3264. int comm_size;
  3265. struct {
  3266. struct perf_event_header header;
  3267. u32 pid;
  3268. u32 tid;
  3269. } event_id;
  3270. };
  3271. static void perf_event_comm_output(struct perf_event *event,
  3272. struct perf_comm_event *comm_event)
  3273. {
  3274. struct perf_output_handle handle;
  3275. struct perf_sample_data sample;
  3276. int size = comm_event->event_id.header.size;
  3277. int ret;
  3278. perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
  3279. ret = perf_output_begin(&handle, event,
  3280. comm_event->event_id.header.size, 0, 0);
  3281. if (ret)
  3282. goto out;
  3283. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  3284. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  3285. perf_output_put(&handle, comm_event->event_id);
  3286. perf_output_copy(&handle, comm_event->comm,
  3287. comm_event->comm_size);
  3288. perf_event__output_id_sample(event, &handle, &sample);
  3289. perf_output_end(&handle);
  3290. out:
  3291. comm_event->event_id.header.size = size;
  3292. }
  3293. static int perf_event_comm_match(struct perf_event *event)
  3294. {
  3295. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3296. return 0;
  3297. if (!event_filter_match(event))
  3298. return 0;
  3299. if (event->attr.comm)
  3300. return 1;
  3301. return 0;
  3302. }
  3303. static void perf_event_comm_ctx(struct perf_event_context *ctx,
  3304. struct perf_comm_event *comm_event)
  3305. {
  3306. struct perf_event *event;
  3307. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3308. if (perf_event_comm_match(event))
  3309. perf_event_comm_output(event, comm_event);
  3310. }
  3311. }
  3312. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  3313. {
  3314. struct perf_cpu_context *cpuctx;
  3315. struct perf_event_context *ctx;
  3316. char comm[TASK_COMM_LEN];
  3317. unsigned int size;
  3318. struct pmu *pmu;
  3319. int ctxn;
  3320. memset(comm, 0, sizeof(comm));
  3321. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  3322. size = ALIGN(strlen(comm)+1, sizeof(u64));
  3323. comm_event->comm = comm;
  3324. comm_event->comm_size = size;
  3325. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  3326. rcu_read_lock();
  3327. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3328. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3329. if (cpuctx->active_pmu != pmu)
  3330. goto next;
  3331. perf_event_comm_ctx(&cpuctx->ctx, comm_event);
  3332. ctxn = pmu->task_ctx_nr;
  3333. if (ctxn < 0)
  3334. goto next;
  3335. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3336. if (ctx)
  3337. perf_event_comm_ctx(ctx, comm_event);
  3338. next:
  3339. put_cpu_ptr(pmu->pmu_cpu_context);
  3340. }
  3341. rcu_read_unlock();
  3342. }
  3343. void perf_event_comm(struct task_struct *task)
  3344. {
  3345. struct perf_comm_event comm_event;
  3346. struct perf_event_context *ctx;
  3347. int ctxn;
  3348. for_each_task_context_nr(ctxn) {
  3349. ctx = task->perf_event_ctxp[ctxn];
  3350. if (!ctx)
  3351. continue;
  3352. perf_event_enable_on_exec(ctx);
  3353. }
  3354. if (!atomic_read(&nr_comm_events))
  3355. return;
  3356. comm_event = (struct perf_comm_event){
  3357. .task = task,
  3358. /* .comm */
  3359. /* .comm_size */
  3360. .event_id = {
  3361. .header = {
  3362. .type = PERF_RECORD_COMM,
  3363. .misc = 0,
  3364. /* .size */
  3365. },
  3366. /* .pid */
  3367. /* .tid */
  3368. },
  3369. };
  3370. perf_event_comm_event(&comm_event);
  3371. }
  3372. /*
  3373. * mmap tracking
  3374. */
  3375. struct perf_mmap_event {
  3376. struct vm_area_struct *vma;
  3377. const char *file_name;
  3378. int file_size;
  3379. struct {
  3380. struct perf_event_header header;
  3381. u32 pid;
  3382. u32 tid;
  3383. u64 start;
  3384. u64 len;
  3385. u64 pgoff;
  3386. } event_id;
  3387. };
  3388. static void perf_event_mmap_output(struct perf_event *event,
  3389. struct perf_mmap_event *mmap_event)
  3390. {
  3391. struct perf_output_handle handle;
  3392. struct perf_sample_data sample;
  3393. int size = mmap_event->event_id.header.size;
  3394. int ret;
  3395. perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
  3396. ret = perf_output_begin(&handle, event,
  3397. mmap_event->event_id.header.size, 0, 0);
  3398. if (ret)
  3399. goto out;
  3400. mmap_event->event_id.pid = perf_event_pid(event, current);
  3401. mmap_event->event_id.tid = perf_event_tid(event, current);
  3402. perf_output_put(&handle, mmap_event->event_id);
  3403. perf_output_copy(&handle, mmap_event->file_name,
  3404. mmap_event->file_size);
  3405. perf_event__output_id_sample(event, &handle, &sample);
  3406. perf_output_end(&handle);
  3407. out:
  3408. mmap_event->event_id.header.size = size;
  3409. }
  3410. static int perf_event_mmap_match(struct perf_event *event,
  3411. struct perf_mmap_event *mmap_event,
  3412. int executable)
  3413. {
  3414. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3415. return 0;
  3416. if (!event_filter_match(event))
  3417. return 0;
  3418. if ((!executable && event->attr.mmap_data) ||
  3419. (executable && event->attr.mmap))
  3420. return 1;
  3421. return 0;
  3422. }
  3423. static void perf_event_mmap_ctx(struct perf_event_context *ctx,
  3424. struct perf_mmap_event *mmap_event,
  3425. int executable)
  3426. {
  3427. struct perf_event *event;
  3428. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3429. if (perf_event_mmap_match(event, mmap_event, executable))
  3430. perf_event_mmap_output(event, mmap_event);
  3431. }
  3432. }
  3433. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  3434. {
  3435. struct perf_cpu_context *cpuctx;
  3436. struct perf_event_context *ctx;
  3437. struct vm_area_struct *vma = mmap_event->vma;
  3438. struct file *file = vma->vm_file;
  3439. unsigned int size;
  3440. char tmp[16];
  3441. char *buf = NULL;
  3442. const char *name;
  3443. struct pmu *pmu;
  3444. int ctxn;
  3445. memset(tmp, 0, sizeof(tmp));
  3446. if (file) {
  3447. /*
  3448. * d_path works from the end of the buffer backwards, so we
  3449. * need to add enough zero bytes after the string to handle
  3450. * the 64bit alignment we do later.
  3451. */
  3452. buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
  3453. if (!buf) {
  3454. name = strncpy(tmp, "//enomem", sizeof(tmp));
  3455. goto got_name;
  3456. }
  3457. name = d_path(&file->f_path, buf, PATH_MAX);
  3458. if (IS_ERR(name)) {
  3459. name = strncpy(tmp, "//toolong", sizeof(tmp));
  3460. goto got_name;
  3461. }
  3462. } else {
  3463. if (arch_vma_name(mmap_event->vma)) {
  3464. name = strncpy(tmp, arch_vma_name(mmap_event->vma),
  3465. sizeof(tmp));
  3466. goto got_name;
  3467. }
  3468. if (!vma->vm_mm) {
  3469. name = strncpy(tmp, "[vdso]", sizeof(tmp));
  3470. goto got_name;
  3471. } else if (vma->vm_start <= vma->vm_mm->start_brk &&
  3472. vma->vm_end >= vma->vm_mm->brk) {
  3473. name = strncpy(tmp, "[heap]", sizeof(tmp));
  3474. goto got_name;
  3475. } else if (vma->vm_start <= vma->vm_mm->start_stack &&
  3476. vma->vm_end >= vma->vm_mm->start_stack) {
  3477. name = strncpy(tmp, "[stack]", sizeof(tmp));
  3478. goto got_name;
  3479. }
  3480. name = strncpy(tmp, "//anon", sizeof(tmp));
  3481. goto got_name;
  3482. }
  3483. got_name:
  3484. size = ALIGN(strlen(name)+1, sizeof(u64));
  3485. mmap_event->file_name = name;
  3486. mmap_event->file_size = size;
  3487. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  3488. rcu_read_lock();
  3489. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3490. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3491. if (cpuctx->active_pmu != pmu)
  3492. goto next;
  3493. perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
  3494. vma->vm_flags & VM_EXEC);
  3495. ctxn = pmu->task_ctx_nr;
  3496. if (ctxn < 0)
  3497. goto next;
  3498. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3499. if (ctx) {
  3500. perf_event_mmap_ctx(ctx, mmap_event,
  3501. vma->vm_flags & VM_EXEC);
  3502. }
  3503. next:
  3504. put_cpu_ptr(pmu->pmu_cpu_context);
  3505. }
  3506. rcu_read_unlock();
  3507. kfree(buf);
  3508. }
  3509. void perf_event_mmap(struct vm_area_struct *vma)
  3510. {
  3511. struct perf_mmap_event mmap_event;
  3512. if (!atomic_read(&nr_mmap_events))
  3513. return;
  3514. mmap_event = (struct perf_mmap_event){
  3515. .vma = vma,
  3516. /* .file_name */
  3517. /* .file_size */
  3518. .event_id = {
  3519. .header = {
  3520. .type = PERF_RECORD_MMAP,
  3521. .misc = PERF_RECORD_MISC_USER,
  3522. /* .size */
  3523. },
  3524. /* .pid */
  3525. /* .tid */
  3526. .start = vma->vm_start,
  3527. .len = vma->vm_end - vma->vm_start,
  3528. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  3529. },
  3530. };
  3531. perf_event_mmap_event(&mmap_event);
  3532. }
  3533. /*
  3534. * IRQ throttle logging
  3535. */
  3536. static void perf_log_throttle(struct perf_event *event, int enable)
  3537. {
  3538. struct perf_output_handle handle;
  3539. struct perf_sample_data sample;
  3540. int ret;
  3541. struct {
  3542. struct perf_event_header header;
  3543. u64 time;
  3544. u64 id;
  3545. u64 stream_id;
  3546. } throttle_event = {
  3547. .header = {
  3548. .type = PERF_RECORD_THROTTLE,
  3549. .misc = 0,
  3550. .size = sizeof(throttle_event),
  3551. },
  3552. .time = perf_clock(),
  3553. .id = primary_event_id(event),
  3554. .stream_id = event->id,
  3555. };
  3556. if (enable)
  3557. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  3558. perf_event_header__init_id(&throttle_event.header, &sample, event);
  3559. ret = perf_output_begin(&handle, event,
  3560. throttle_event.header.size, 1, 0);
  3561. if (ret)
  3562. return;
  3563. perf_output_put(&handle, throttle_event);
  3564. perf_event__output_id_sample(event, &handle, &sample);
  3565. perf_output_end(&handle);
  3566. }
  3567. /*
  3568. * Generic event overflow handling, sampling.
  3569. */
  3570. static int __perf_event_overflow(struct perf_event *event, int nmi,
  3571. int throttle, struct perf_sample_data *data,
  3572. struct pt_regs *regs)
  3573. {
  3574. int events = atomic_read(&event->event_limit);
  3575. struct hw_perf_event *hwc = &event->hw;
  3576. int ret = 0;
  3577. /*
  3578. * Non-sampling counters might still use the PMI to fold short
  3579. * hardware counters, ignore those.
  3580. */
  3581. if (unlikely(!is_sampling_event(event)))
  3582. return 0;
  3583. if (!throttle) {
  3584. hwc->interrupts++;
  3585. } else {
  3586. if (hwc->interrupts != MAX_INTERRUPTS) {
  3587. hwc->interrupts++;
  3588. if (HZ * hwc->interrupts >
  3589. (u64)sysctl_perf_event_sample_rate) {
  3590. hwc->interrupts = MAX_INTERRUPTS;
  3591. perf_log_throttle(event, 0);
  3592. ret = 1;
  3593. }
  3594. } else {
  3595. /*
  3596. * Keep re-disabling events even though on the previous
  3597. * pass we disabled it - just in case we raced with a
  3598. * sched-in and the event got enabled again:
  3599. */
  3600. ret = 1;
  3601. }
  3602. }
  3603. if (event->attr.freq) {
  3604. u64 now = perf_clock();
  3605. s64 delta = now - hwc->freq_time_stamp;
  3606. hwc->freq_time_stamp = now;
  3607. if (delta > 0 && delta < 2*TICK_NSEC)
  3608. perf_adjust_period(event, delta, hwc->last_period);
  3609. }
  3610. /*
  3611. * XXX event_limit might not quite work as expected on inherited
  3612. * events
  3613. */
  3614. event->pending_kill = POLL_IN;
  3615. if (events && atomic_dec_and_test(&event->event_limit)) {
  3616. ret = 1;
  3617. event->pending_kill = POLL_HUP;
  3618. if (nmi) {
  3619. event->pending_disable = 1;
  3620. irq_work_queue(&event->pending);
  3621. } else
  3622. perf_event_disable(event);
  3623. }
  3624. if (event->overflow_handler)
  3625. event->overflow_handler(event, nmi, data, regs);
  3626. else
  3627. perf_event_output(event, nmi, data, regs);
  3628. return ret;
  3629. }
  3630. int perf_event_overflow(struct perf_event *event, int nmi,
  3631. struct perf_sample_data *data,
  3632. struct pt_regs *regs)
  3633. {
  3634. return __perf_event_overflow(event, nmi, 1, data, regs);
  3635. }
  3636. /*
  3637. * Generic software event infrastructure
  3638. */
  3639. struct swevent_htable {
  3640. struct swevent_hlist *swevent_hlist;
  3641. struct mutex hlist_mutex;
  3642. int hlist_refcount;
  3643. /* Recursion avoidance in each contexts */
  3644. int recursion[PERF_NR_CONTEXTS];
  3645. };
  3646. static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
  3647. /*
  3648. * We directly increment event->count and keep a second value in
  3649. * event->hw.period_left to count intervals. This period event
  3650. * is kept in the range [-sample_period, 0] so that we can use the
  3651. * sign as trigger.
  3652. */
  3653. static u64 perf_swevent_set_period(struct perf_event *event)
  3654. {
  3655. struct hw_perf_event *hwc = &event->hw;
  3656. u64 period = hwc->last_period;
  3657. u64 nr, offset;
  3658. s64 old, val;
  3659. hwc->last_period = hwc->sample_period;
  3660. again:
  3661. old = val = local64_read(&hwc->period_left);
  3662. if (val < 0)
  3663. return 0;
  3664. nr = div64_u64(period + val, period);
  3665. offset = nr * period;
  3666. val -= offset;
  3667. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  3668. goto again;
  3669. return nr;
  3670. }
  3671. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  3672. int nmi, struct perf_sample_data *data,
  3673. struct pt_regs *regs)
  3674. {
  3675. struct hw_perf_event *hwc = &event->hw;
  3676. int throttle = 0;
  3677. data->period = event->hw.last_period;
  3678. if (!overflow)
  3679. overflow = perf_swevent_set_period(event);
  3680. if (hwc->interrupts == MAX_INTERRUPTS)
  3681. return;
  3682. for (; overflow; overflow--) {
  3683. if (__perf_event_overflow(event, nmi, throttle,
  3684. data, regs)) {
  3685. /*
  3686. * We inhibit the overflow from happening when
  3687. * hwc->interrupts == MAX_INTERRUPTS.
  3688. */
  3689. break;
  3690. }
  3691. throttle = 1;
  3692. }
  3693. }
  3694. static void perf_swevent_event(struct perf_event *event, u64 nr,
  3695. int nmi, struct perf_sample_data *data,
  3696. struct pt_regs *regs)
  3697. {
  3698. struct hw_perf_event *hwc = &event->hw;
  3699. local64_add(nr, &event->count);
  3700. if (!regs)
  3701. return;
  3702. if (!is_sampling_event(event))
  3703. return;
  3704. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  3705. return perf_swevent_overflow(event, 1, nmi, data, regs);
  3706. if (local64_add_negative(nr, &hwc->period_left))
  3707. return;
  3708. perf_swevent_overflow(event, 0, nmi, data, regs);
  3709. }
  3710. static int perf_exclude_event(struct perf_event *event,
  3711. struct pt_regs *regs)
  3712. {
  3713. if (event->hw.state & PERF_HES_STOPPED)
  3714. return 0;
  3715. if (regs) {
  3716. if (event->attr.exclude_user && user_mode(regs))
  3717. return 1;
  3718. if (event->attr.exclude_kernel && !user_mode(regs))
  3719. return 1;
  3720. }
  3721. return 0;
  3722. }
  3723. static int perf_swevent_match(struct perf_event *event,
  3724. enum perf_type_id type,
  3725. u32 event_id,
  3726. struct perf_sample_data *data,
  3727. struct pt_regs *regs)
  3728. {
  3729. if (event->attr.type != type)
  3730. return 0;
  3731. if (event->attr.config != event_id)
  3732. return 0;
  3733. if (perf_exclude_event(event, regs))
  3734. return 0;
  3735. return 1;
  3736. }
  3737. static inline u64 swevent_hash(u64 type, u32 event_id)
  3738. {
  3739. u64 val = event_id | (type << 32);
  3740. return hash_64(val, SWEVENT_HLIST_BITS);
  3741. }
  3742. static inline struct hlist_head *
  3743. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  3744. {
  3745. u64 hash = swevent_hash(type, event_id);
  3746. return &hlist->heads[hash];
  3747. }
  3748. /* For the read side: events when they trigger */
  3749. static inline struct hlist_head *
  3750. find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
  3751. {
  3752. struct swevent_hlist *hlist;
  3753. hlist = rcu_dereference(swhash->swevent_hlist);
  3754. if (!hlist)
  3755. return NULL;
  3756. return __find_swevent_head(hlist, type, event_id);
  3757. }
  3758. /* For the event head insertion and removal in the hlist */
  3759. static inline struct hlist_head *
  3760. find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
  3761. {
  3762. struct swevent_hlist *hlist;
  3763. u32 event_id = event->attr.config;
  3764. u64 type = event->attr.type;
  3765. /*
  3766. * Event scheduling is always serialized against hlist allocation
  3767. * and release. Which makes the protected version suitable here.
  3768. * The context lock guarantees that.
  3769. */
  3770. hlist = rcu_dereference_protected(swhash->swevent_hlist,
  3771. lockdep_is_held(&event->ctx->lock));
  3772. if (!hlist)
  3773. return NULL;
  3774. return __find_swevent_head(hlist, type, event_id);
  3775. }
  3776. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  3777. u64 nr, int nmi,
  3778. struct perf_sample_data *data,
  3779. struct pt_regs *regs)
  3780. {
  3781. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3782. struct perf_event *event;
  3783. struct hlist_node *node;
  3784. struct hlist_head *head;
  3785. rcu_read_lock();
  3786. head = find_swevent_head_rcu(swhash, type, event_id);
  3787. if (!head)
  3788. goto end;
  3789. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3790. if (perf_swevent_match(event, type, event_id, data, regs))
  3791. perf_swevent_event(event, nr, nmi, data, regs);
  3792. }
  3793. end:
  3794. rcu_read_unlock();
  3795. }
  3796. int perf_swevent_get_recursion_context(void)
  3797. {
  3798. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3799. return get_recursion_context(swhash->recursion);
  3800. }
  3801. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  3802. inline void perf_swevent_put_recursion_context(int rctx)
  3803. {
  3804. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3805. put_recursion_context(swhash->recursion, rctx);
  3806. }
  3807. void __perf_sw_event(u32 event_id, u64 nr, int nmi,
  3808. struct pt_regs *regs, u64 addr)
  3809. {
  3810. struct perf_sample_data data;
  3811. int rctx;
  3812. preempt_disable_notrace();
  3813. rctx = perf_swevent_get_recursion_context();
  3814. if (rctx < 0)
  3815. return;
  3816. perf_sample_data_init(&data, addr);
  3817. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
  3818. perf_swevent_put_recursion_context(rctx);
  3819. preempt_enable_notrace();
  3820. }
  3821. static void perf_swevent_read(struct perf_event *event)
  3822. {
  3823. }
  3824. static int perf_swevent_add(struct perf_event *event, int flags)
  3825. {
  3826. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3827. struct hw_perf_event *hwc = &event->hw;
  3828. struct hlist_head *head;
  3829. if (is_sampling_event(event)) {
  3830. hwc->last_period = hwc->sample_period;
  3831. perf_swevent_set_period(event);
  3832. }
  3833. hwc->state = !(flags & PERF_EF_START);
  3834. head = find_swevent_head(swhash, event);
  3835. if (WARN_ON_ONCE(!head))
  3836. return -EINVAL;
  3837. hlist_add_head_rcu(&event->hlist_entry, head);
  3838. return 0;
  3839. }
  3840. static void perf_swevent_del(struct perf_event *event, int flags)
  3841. {
  3842. hlist_del_rcu(&event->hlist_entry);
  3843. }
  3844. static void perf_swevent_start(struct perf_event *event, int flags)
  3845. {
  3846. event->hw.state = 0;
  3847. }
  3848. static void perf_swevent_stop(struct perf_event *event, int flags)
  3849. {
  3850. event->hw.state = PERF_HES_STOPPED;
  3851. }
  3852. /* Deref the hlist from the update side */
  3853. static inline struct swevent_hlist *
  3854. swevent_hlist_deref(struct swevent_htable *swhash)
  3855. {
  3856. return rcu_dereference_protected(swhash->swevent_hlist,
  3857. lockdep_is_held(&swhash->hlist_mutex));
  3858. }
  3859. static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
  3860. {
  3861. struct swevent_hlist *hlist;
  3862. hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
  3863. kfree(hlist);
  3864. }
  3865. static void swevent_hlist_release(struct swevent_htable *swhash)
  3866. {
  3867. struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
  3868. if (!hlist)
  3869. return;
  3870. rcu_assign_pointer(swhash->swevent_hlist, NULL);
  3871. call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
  3872. }
  3873. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  3874. {
  3875. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  3876. mutex_lock(&swhash->hlist_mutex);
  3877. if (!--swhash->hlist_refcount)
  3878. swevent_hlist_release(swhash);
  3879. mutex_unlock(&swhash->hlist_mutex);
  3880. }
  3881. static void swevent_hlist_put(struct perf_event *event)
  3882. {
  3883. int cpu;
  3884. if (event->cpu != -1) {
  3885. swevent_hlist_put_cpu(event, event->cpu);
  3886. return;
  3887. }
  3888. for_each_possible_cpu(cpu)
  3889. swevent_hlist_put_cpu(event, cpu);
  3890. }
  3891. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  3892. {
  3893. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  3894. int err = 0;
  3895. mutex_lock(&swhash->hlist_mutex);
  3896. if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
  3897. struct swevent_hlist *hlist;
  3898. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  3899. if (!hlist) {
  3900. err = -ENOMEM;
  3901. goto exit;
  3902. }
  3903. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  3904. }
  3905. swhash->hlist_refcount++;
  3906. exit:
  3907. mutex_unlock(&swhash->hlist_mutex);
  3908. return err;
  3909. }
  3910. static int swevent_hlist_get(struct perf_event *event)
  3911. {
  3912. int err;
  3913. int cpu, failed_cpu;
  3914. if (event->cpu != -1)
  3915. return swevent_hlist_get_cpu(event, event->cpu);
  3916. get_online_cpus();
  3917. for_each_possible_cpu(cpu) {
  3918. err = swevent_hlist_get_cpu(event, cpu);
  3919. if (err) {
  3920. failed_cpu = cpu;
  3921. goto fail;
  3922. }
  3923. }
  3924. put_online_cpus();
  3925. return 0;
  3926. fail:
  3927. for_each_possible_cpu(cpu) {
  3928. if (cpu == failed_cpu)
  3929. break;
  3930. swevent_hlist_put_cpu(event, cpu);
  3931. }
  3932. put_online_cpus();
  3933. return err;
  3934. }
  3935. atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
  3936. static void sw_perf_event_destroy(struct perf_event *event)
  3937. {
  3938. u64 event_id = event->attr.config;
  3939. WARN_ON(event->parent);
  3940. jump_label_dec(&perf_swevent_enabled[event_id]);
  3941. swevent_hlist_put(event);
  3942. }
  3943. static int perf_swevent_init(struct perf_event *event)
  3944. {
  3945. int event_id = event->attr.config;
  3946. if (event->attr.type != PERF_TYPE_SOFTWARE)
  3947. return -ENOENT;
  3948. switch (event_id) {
  3949. case PERF_COUNT_SW_CPU_CLOCK:
  3950. case PERF_COUNT_SW_TASK_CLOCK:
  3951. return -ENOENT;
  3952. default:
  3953. break;
  3954. }
  3955. if (event_id >= PERF_COUNT_SW_MAX)
  3956. return -ENOENT;
  3957. if (!event->parent) {
  3958. int err;
  3959. err = swevent_hlist_get(event);
  3960. if (err)
  3961. return err;
  3962. jump_label_inc(&perf_swevent_enabled[event_id]);
  3963. event->destroy = sw_perf_event_destroy;
  3964. }
  3965. return 0;
  3966. }
  3967. static struct pmu perf_swevent = {
  3968. .task_ctx_nr = perf_sw_context,
  3969. .event_init = perf_swevent_init,
  3970. .add = perf_swevent_add,
  3971. .del = perf_swevent_del,
  3972. .start = perf_swevent_start,
  3973. .stop = perf_swevent_stop,
  3974. .read = perf_swevent_read,
  3975. };
  3976. #ifdef CONFIG_EVENT_TRACING
  3977. static int perf_tp_filter_match(struct perf_event *event,
  3978. struct perf_sample_data *data)
  3979. {
  3980. void *record = data->raw->data;
  3981. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  3982. return 1;
  3983. return 0;
  3984. }
  3985. static int perf_tp_event_match(struct perf_event *event,
  3986. struct perf_sample_data *data,
  3987. struct pt_regs *regs)
  3988. {
  3989. /*
  3990. * All tracepoints are from kernel-space.
  3991. */
  3992. if (event->attr.exclude_kernel)
  3993. return 0;
  3994. if (!perf_tp_filter_match(event, data))
  3995. return 0;
  3996. return 1;
  3997. }
  3998. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  3999. struct pt_regs *regs, struct hlist_head *head, int rctx)
  4000. {
  4001. struct perf_sample_data data;
  4002. struct perf_event *event;
  4003. struct hlist_node *node;
  4004. struct perf_raw_record raw = {
  4005. .size = entry_size,
  4006. .data = record,
  4007. };
  4008. perf_sample_data_init(&data, addr);
  4009. data.raw = &raw;
  4010. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  4011. if (perf_tp_event_match(event, &data, regs))
  4012. perf_swevent_event(event, count, 1, &data, regs);
  4013. }
  4014. perf_swevent_put_recursion_context(rctx);
  4015. }
  4016. EXPORT_SYMBOL_GPL(perf_tp_event);
  4017. static void tp_perf_event_destroy(struct perf_event *event)
  4018. {
  4019. perf_trace_destroy(event);
  4020. }
  4021. static int perf_tp_event_init(struct perf_event *event)
  4022. {
  4023. int err;
  4024. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  4025. return -ENOENT;
  4026. err = perf_trace_init(event);
  4027. if (err)
  4028. return err;
  4029. event->destroy = tp_perf_event_destroy;
  4030. return 0;
  4031. }
  4032. static struct pmu perf_tracepoint = {
  4033. .task_ctx_nr = perf_sw_context,
  4034. .event_init = perf_tp_event_init,
  4035. .add = perf_trace_add,
  4036. .del = perf_trace_del,
  4037. .start = perf_swevent_start,
  4038. .stop = perf_swevent_stop,
  4039. .read = perf_swevent_read,
  4040. };
  4041. static inline void perf_tp_register(void)
  4042. {
  4043. perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
  4044. }
  4045. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  4046. {
  4047. char *filter_str;
  4048. int ret;
  4049. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  4050. return -EINVAL;
  4051. filter_str = strndup_user(arg, PAGE_SIZE);
  4052. if (IS_ERR(filter_str))
  4053. return PTR_ERR(filter_str);
  4054. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  4055. kfree(filter_str);
  4056. return ret;
  4057. }
  4058. static void perf_event_free_filter(struct perf_event *event)
  4059. {
  4060. ftrace_profile_free_filter(event);
  4061. }
  4062. #else
  4063. static inline void perf_tp_register(void)
  4064. {
  4065. }
  4066. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  4067. {
  4068. return -ENOENT;
  4069. }
  4070. static void perf_event_free_filter(struct perf_event *event)
  4071. {
  4072. }
  4073. #endif /* CONFIG_EVENT_TRACING */
  4074. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  4075. void perf_bp_event(struct perf_event *bp, void *data)
  4076. {
  4077. struct perf_sample_data sample;
  4078. struct pt_regs *regs = data;
  4079. perf_sample_data_init(&sample, bp->attr.bp_addr);
  4080. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  4081. perf_swevent_event(bp, 1, 1, &sample, regs);
  4082. }
  4083. #endif
  4084. /*
  4085. * hrtimer based swevent callback
  4086. */
  4087. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  4088. {
  4089. enum hrtimer_restart ret = HRTIMER_RESTART;
  4090. struct perf_sample_data data;
  4091. struct pt_regs *regs;
  4092. struct perf_event *event;
  4093. u64 period;
  4094. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  4095. event->pmu->read(event);
  4096. perf_sample_data_init(&data, 0);
  4097. data.period = event->hw.last_period;
  4098. regs = get_irq_regs();
  4099. if (regs && !perf_exclude_event(event, regs)) {
  4100. if (!(event->attr.exclude_idle && current->pid == 0))
  4101. if (perf_event_overflow(event, 0, &data, regs))
  4102. ret = HRTIMER_NORESTART;
  4103. }
  4104. period = max_t(u64, 10000, event->hw.sample_period);
  4105. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  4106. return ret;
  4107. }
  4108. static void perf_swevent_start_hrtimer(struct perf_event *event)
  4109. {
  4110. struct hw_perf_event *hwc = &event->hw;
  4111. s64 period;
  4112. if (!is_sampling_event(event))
  4113. return;
  4114. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  4115. hwc->hrtimer.function = perf_swevent_hrtimer;
  4116. period = local64_read(&hwc->period_left);
  4117. if (period) {
  4118. if (period < 0)
  4119. period = 10000;
  4120. local64_set(&hwc->period_left, 0);
  4121. } else {
  4122. period = max_t(u64, 10000, hwc->sample_period);
  4123. }
  4124. __hrtimer_start_range_ns(&hwc->hrtimer,
  4125. ns_to_ktime(period), 0,
  4126. HRTIMER_MODE_REL_PINNED, 0);
  4127. }
  4128. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  4129. {
  4130. struct hw_perf_event *hwc = &event->hw;
  4131. if (is_sampling_event(event)) {
  4132. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  4133. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  4134. hrtimer_cancel(&hwc->hrtimer);
  4135. }
  4136. }
  4137. /*
  4138. * Software event: cpu wall time clock
  4139. */
  4140. static void cpu_clock_event_update(struct perf_event *event)
  4141. {
  4142. s64 prev;
  4143. u64 now;
  4144. now = local_clock();
  4145. prev = local64_xchg(&event->hw.prev_count, now);
  4146. local64_add(now - prev, &event->count);
  4147. }
  4148. static void cpu_clock_event_start(struct perf_event *event, int flags)
  4149. {
  4150. local64_set(&event->hw.prev_count, local_clock());
  4151. perf_swevent_start_hrtimer(event);
  4152. }
  4153. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  4154. {
  4155. perf_swevent_cancel_hrtimer(event);
  4156. cpu_clock_event_update(event);
  4157. }
  4158. static int cpu_clock_event_add(struct perf_event *event, int flags)
  4159. {
  4160. if (flags & PERF_EF_START)
  4161. cpu_clock_event_start(event, flags);
  4162. return 0;
  4163. }
  4164. static void cpu_clock_event_del(struct perf_event *event, int flags)
  4165. {
  4166. cpu_clock_event_stop(event, flags);
  4167. }
  4168. static void cpu_clock_event_read(struct perf_event *event)
  4169. {
  4170. cpu_clock_event_update(event);
  4171. }
  4172. static int cpu_clock_event_init(struct perf_event *event)
  4173. {
  4174. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4175. return -ENOENT;
  4176. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  4177. return -ENOENT;
  4178. return 0;
  4179. }
  4180. static struct pmu perf_cpu_clock = {
  4181. .task_ctx_nr = perf_sw_context,
  4182. .event_init = cpu_clock_event_init,
  4183. .add = cpu_clock_event_add,
  4184. .del = cpu_clock_event_del,
  4185. .start = cpu_clock_event_start,
  4186. .stop = cpu_clock_event_stop,
  4187. .read = cpu_clock_event_read,
  4188. };
  4189. /*
  4190. * Software event: task time clock
  4191. */
  4192. static void task_clock_event_update(struct perf_event *event, u64 now)
  4193. {
  4194. u64 prev;
  4195. s64 delta;
  4196. prev = local64_xchg(&event->hw.prev_count, now);
  4197. delta = now - prev;
  4198. local64_add(delta, &event->count);
  4199. }
  4200. static void task_clock_event_start(struct perf_event *event, int flags)
  4201. {
  4202. local64_set(&event->hw.prev_count, event->ctx->time);
  4203. perf_swevent_start_hrtimer(event);
  4204. }
  4205. static void task_clock_event_stop(struct perf_event *event, int flags)
  4206. {
  4207. perf_swevent_cancel_hrtimer(event);
  4208. task_clock_event_update(event, event->ctx->time);
  4209. }
  4210. static int task_clock_event_add(struct perf_event *event, int flags)
  4211. {
  4212. if (flags & PERF_EF_START)
  4213. task_clock_event_start(event, flags);
  4214. return 0;
  4215. }
  4216. static void task_clock_event_del(struct perf_event *event, int flags)
  4217. {
  4218. task_clock_event_stop(event, PERF_EF_UPDATE);
  4219. }
  4220. static void task_clock_event_read(struct perf_event *event)
  4221. {
  4222. u64 time;
  4223. if (!in_nmi()) {
  4224. update_context_time(event->ctx);
  4225. time = event->ctx->time;
  4226. } else {
  4227. u64 now = perf_clock();
  4228. u64 delta = now - event->ctx->timestamp;
  4229. time = event->ctx->time + delta;
  4230. }
  4231. task_clock_event_update(event, time);
  4232. }
  4233. static int task_clock_event_init(struct perf_event *event)
  4234. {
  4235. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4236. return -ENOENT;
  4237. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  4238. return -ENOENT;
  4239. return 0;
  4240. }
  4241. static struct pmu perf_task_clock = {
  4242. .task_ctx_nr = perf_sw_context,
  4243. .event_init = task_clock_event_init,
  4244. .add = task_clock_event_add,
  4245. .del = task_clock_event_del,
  4246. .start = task_clock_event_start,
  4247. .stop = task_clock_event_stop,
  4248. .read = task_clock_event_read,
  4249. };
  4250. static void perf_pmu_nop_void(struct pmu *pmu)
  4251. {
  4252. }
  4253. static int perf_pmu_nop_int(struct pmu *pmu)
  4254. {
  4255. return 0;
  4256. }
  4257. static void perf_pmu_start_txn(struct pmu *pmu)
  4258. {
  4259. perf_pmu_disable(pmu);
  4260. }
  4261. static int perf_pmu_commit_txn(struct pmu *pmu)
  4262. {
  4263. perf_pmu_enable(pmu);
  4264. return 0;
  4265. }
  4266. static void perf_pmu_cancel_txn(struct pmu *pmu)
  4267. {
  4268. perf_pmu_enable(pmu);
  4269. }
  4270. /*
  4271. * Ensures all contexts with the same task_ctx_nr have the same
  4272. * pmu_cpu_context too.
  4273. */
  4274. static void *find_pmu_context(int ctxn)
  4275. {
  4276. struct pmu *pmu;
  4277. if (ctxn < 0)
  4278. return NULL;
  4279. list_for_each_entry(pmu, &pmus, entry) {
  4280. if (pmu->task_ctx_nr == ctxn)
  4281. return pmu->pmu_cpu_context;
  4282. }
  4283. return NULL;
  4284. }
  4285. static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
  4286. {
  4287. int cpu;
  4288. for_each_possible_cpu(cpu) {
  4289. struct perf_cpu_context *cpuctx;
  4290. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  4291. if (cpuctx->active_pmu == old_pmu)
  4292. cpuctx->active_pmu = pmu;
  4293. }
  4294. }
  4295. static void free_pmu_context(struct pmu *pmu)
  4296. {
  4297. struct pmu *i;
  4298. mutex_lock(&pmus_lock);
  4299. /*
  4300. * Like a real lame refcount.
  4301. */
  4302. list_for_each_entry(i, &pmus, entry) {
  4303. if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
  4304. update_pmu_context(i, pmu);
  4305. goto out;
  4306. }
  4307. }
  4308. free_percpu(pmu->pmu_cpu_context);
  4309. out:
  4310. mutex_unlock(&pmus_lock);
  4311. }
  4312. static struct idr pmu_idr;
  4313. static ssize_t
  4314. type_show(struct device *dev, struct device_attribute *attr, char *page)
  4315. {
  4316. struct pmu *pmu = dev_get_drvdata(dev);
  4317. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
  4318. }
  4319. static struct device_attribute pmu_dev_attrs[] = {
  4320. __ATTR_RO(type),
  4321. __ATTR_NULL,
  4322. };
  4323. static int pmu_bus_running;
  4324. static struct bus_type pmu_bus = {
  4325. .name = "event_source",
  4326. .dev_attrs = pmu_dev_attrs,
  4327. };
  4328. static void pmu_dev_release(struct device *dev)
  4329. {
  4330. kfree(dev);
  4331. }
  4332. static int pmu_dev_alloc(struct pmu *pmu)
  4333. {
  4334. int ret = -ENOMEM;
  4335. pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  4336. if (!pmu->dev)
  4337. goto out;
  4338. device_initialize(pmu->dev);
  4339. ret = dev_set_name(pmu->dev, "%s", pmu->name);
  4340. if (ret)
  4341. goto free_dev;
  4342. dev_set_drvdata(pmu->dev, pmu);
  4343. pmu->dev->bus = &pmu_bus;
  4344. pmu->dev->release = pmu_dev_release;
  4345. ret = device_add(pmu->dev);
  4346. if (ret)
  4347. goto free_dev;
  4348. out:
  4349. return ret;
  4350. free_dev:
  4351. put_device(pmu->dev);
  4352. goto out;
  4353. }
  4354. static struct lock_class_key cpuctx_mutex;
  4355. int perf_pmu_register(struct pmu *pmu, char *name, int type)
  4356. {
  4357. int cpu, ret;
  4358. mutex_lock(&pmus_lock);
  4359. ret = -ENOMEM;
  4360. pmu->pmu_disable_count = alloc_percpu(int);
  4361. if (!pmu->pmu_disable_count)
  4362. goto unlock;
  4363. pmu->type = -1;
  4364. if (!name)
  4365. goto skip_type;
  4366. pmu->name = name;
  4367. if (type < 0) {
  4368. int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
  4369. if (!err)
  4370. goto free_pdc;
  4371. err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
  4372. if (err) {
  4373. ret = err;
  4374. goto free_pdc;
  4375. }
  4376. }
  4377. pmu->type = type;
  4378. if (pmu_bus_running) {
  4379. ret = pmu_dev_alloc(pmu);
  4380. if (ret)
  4381. goto free_idr;
  4382. }
  4383. skip_type:
  4384. pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  4385. if (pmu->pmu_cpu_context)
  4386. goto got_cpu_context;
  4387. pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  4388. if (!pmu->pmu_cpu_context)
  4389. goto free_dev;
  4390. for_each_possible_cpu(cpu) {
  4391. struct perf_cpu_context *cpuctx;
  4392. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  4393. __perf_event_init_context(&cpuctx->ctx);
  4394. lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
  4395. cpuctx->ctx.type = cpu_context;
  4396. cpuctx->ctx.pmu = pmu;
  4397. cpuctx->jiffies_interval = 1;
  4398. INIT_LIST_HEAD(&cpuctx->rotation_list);
  4399. cpuctx->active_pmu = pmu;
  4400. }
  4401. got_cpu_context:
  4402. if (!pmu->start_txn) {
  4403. if (pmu->pmu_enable) {
  4404. /*
  4405. * If we have pmu_enable/pmu_disable calls, install
  4406. * transaction stubs that use that to try and batch
  4407. * hardware accesses.
  4408. */
  4409. pmu->start_txn = perf_pmu_start_txn;
  4410. pmu->commit_txn = perf_pmu_commit_txn;
  4411. pmu->cancel_txn = perf_pmu_cancel_txn;
  4412. } else {
  4413. pmu->start_txn = perf_pmu_nop_void;
  4414. pmu->commit_txn = perf_pmu_nop_int;
  4415. pmu->cancel_txn = perf_pmu_nop_void;
  4416. }
  4417. }
  4418. if (!pmu->pmu_enable) {
  4419. pmu->pmu_enable = perf_pmu_nop_void;
  4420. pmu->pmu_disable = perf_pmu_nop_void;
  4421. }
  4422. list_add_rcu(&pmu->entry, &pmus);
  4423. ret = 0;
  4424. unlock:
  4425. mutex_unlock(&pmus_lock);
  4426. return ret;
  4427. free_dev:
  4428. device_del(pmu->dev);
  4429. put_device(pmu->dev);
  4430. free_idr:
  4431. if (pmu->type >= PERF_TYPE_MAX)
  4432. idr_remove(&pmu_idr, pmu->type);
  4433. free_pdc:
  4434. free_percpu(pmu->pmu_disable_count);
  4435. goto unlock;
  4436. }
  4437. void perf_pmu_unregister(struct pmu *pmu)
  4438. {
  4439. mutex_lock(&pmus_lock);
  4440. list_del_rcu(&pmu->entry);
  4441. mutex_unlock(&pmus_lock);
  4442. /*
  4443. * We dereference the pmu list under both SRCU and regular RCU, so
  4444. * synchronize against both of those.
  4445. */
  4446. synchronize_srcu(&pmus_srcu);
  4447. synchronize_rcu();
  4448. free_percpu(pmu->pmu_disable_count);
  4449. if (pmu->type >= PERF_TYPE_MAX)
  4450. idr_remove(&pmu_idr, pmu->type);
  4451. device_del(pmu->dev);
  4452. put_device(pmu->dev);
  4453. free_pmu_context(pmu);
  4454. }
  4455. struct pmu *perf_init_event(struct perf_event *event)
  4456. {
  4457. struct pmu *pmu = NULL;
  4458. int idx;
  4459. idx = srcu_read_lock(&pmus_srcu);
  4460. rcu_read_lock();
  4461. pmu = idr_find(&pmu_idr, event->attr.type);
  4462. rcu_read_unlock();
  4463. if (pmu)
  4464. goto unlock;
  4465. list_for_each_entry_rcu(pmu, &pmus, entry) {
  4466. int ret = pmu->event_init(event);
  4467. if (!ret)
  4468. goto unlock;
  4469. if (ret != -ENOENT) {
  4470. pmu = ERR_PTR(ret);
  4471. goto unlock;
  4472. }
  4473. }
  4474. pmu = ERR_PTR(-ENOENT);
  4475. unlock:
  4476. srcu_read_unlock(&pmus_srcu, idx);
  4477. return pmu;
  4478. }
  4479. /*
  4480. * Allocate and initialize a event structure
  4481. */
  4482. static struct perf_event *
  4483. perf_event_alloc(struct perf_event_attr *attr, int cpu,
  4484. struct task_struct *task,
  4485. struct perf_event *group_leader,
  4486. struct perf_event *parent_event,
  4487. perf_overflow_handler_t overflow_handler)
  4488. {
  4489. struct pmu *pmu;
  4490. struct perf_event *event;
  4491. struct hw_perf_event *hwc;
  4492. long err;
  4493. if ((unsigned)cpu >= nr_cpu_ids) {
  4494. if (!task || cpu != -1)
  4495. return ERR_PTR(-EINVAL);
  4496. }
  4497. event = kzalloc(sizeof(*event), GFP_KERNEL);
  4498. if (!event)
  4499. return ERR_PTR(-ENOMEM);
  4500. /*
  4501. * Single events are their own group leaders, with an
  4502. * empty sibling list:
  4503. */
  4504. if (!group_leader)
  4505. group_leader = event;
  4506. mutex_init(&event->child_mutex);
  4507. INIT_LIST_HEAD(&event->child_list);
  4508. INIT_LIST_HEAD(&event->group_entry);
  4509. INIT_LIST_HEAD(&event->event_entry);
  4510. INIT_LIST_HEAD(&event->sibling_list);
  4511. init_waitqueue_head(&event->waitq);
  4512. init_irq_work(&event->pending, perf_pending_event);
  4513. mutex_init(&event->mmap_mutex);
  4514. event->cpu = cpu;
  4515. event->attr = *attr;
  4516. event->group_leader = group_leader;
  4517. event->pmu = NULL;
  4518. event->oncpu = -1;
  4519. event->parent = parent_event;
  4520. event->ns = get_pid_ns(current->nsproxy->pid_ns);
  4521. event->id = atomic64_inc_return(&perf_event_id);
  4522. event->state = PERF_EVENT_STATE_INACTIVE;
  4523. if (task) {
  4524. event->attach_state = PERF_ATTACH_TASK;
  4525. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  4526. /*
  4527. * hw_breakpoint is a bit difficult here..
  4528. */
  4529. if (attr->type == PERF_TYPE_BREAKPOINT)
  4530. event->hw.bp_target = task;
  4531. #endif
  4532. }
  4533. if (!overflow_handler && parent_event)
  4534. overflow_handler = parent_event->overflow_handler;
  4535. event->overflow_handler = overflow_handler;
  4536. if (attr->disabled)
  4537. event->state = PERF_EVENT_STATE_OFF;
  4538. pmu = NULL;
  4539. hwc = &event->hw;
  4540. hwc->sample_period = attr->sample_period;
  4541. if (attr->freq && attr->sample_freq)
  4542. hwc->sample_period = 1;
  4543. hwc->last_period = hwc->sample_period;
  4544. local64_set(&hwc->period_left, hwc->sample_period);
  4545. /*
  4546. * we currently do not support PERF_FORMAT_GROUP on inherited events
  4547. */
  4548. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  4549. goto done;
  4550. pmu = perf_init_event(event);
  4551. done:
  4552. err = 0;
  4553. if (!pmu)
  4554. err = -EINVAL;
  4555. else if (IS_ERR(pmu))
  4556. err = PTR_ERR(pmu);
  4557. if (err) {
  4558. if (event->ns)
  4559. put_pid_ns(event->ns);
  4560. kfree(event);
  4561. return ERR_PTR(err);
  4562. }
  4563. event->pmu = pmu;
  4564. if (!event->parent) {
  4565. if (event->attach_state & PERF_ATTACH_TASK)
  4566. jump_label_inc(&perf_task_events);
  4567. if (event->attr.mmap || event->attr.mmap_data)
  4568. atomic_inc(&nr_mmap_events);
  4569. if (event->attr.comm)
  4570. atomic_inc(&nr_comm_events);
  4571. if (event->attr.task)
  4572. atomic_inc(&nr_task_events);
  4573. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  4574. err = get_callchain_buffers();
  4575. if (err) {
  4576. free_event(event);
  4577. return ERR_PTR(err);
  4578. }
  4579. }
  4580. }
  4581. return event;
  4582. }
  4583. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  4584. struct perf_event_attr *attr)
  4585. {
  4586. u32 size;
  4587. int ret;
  4588. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  4589. return -EFAULT;
  4590. /*
  4591. * zero the full structure, so that a short copy will be nice.
  4592. */
  4593. memset(attr, 0, sizeof(*attr));
  4594. ret = get_user(size, &uattr->size);
  4595. if (ret)
  4596. return ret;
  4597. if (size > PAGE_SIZE) /* silly large */
  4598. goto err_size;
  4599. if (!size) /* abi compat */
  4600. size = PERF_ATTR_SIZE_VER0;
  4601. if (size < PERF_ATTR_SIZE_VER0)
  4602. goto err_size;
  4603. /*
  4604. * If we're handed a bigger struct than we know of,
  4605. * ensure all the unknown bits are 0 - i.e. new
  4606. * user-space does not rely on any kernel feature
  4607. * extensions we dont know about yet.
  4608. */
  4609. if (size > sizeof(*attr)) {
  4610. unsigned char __user *addr;
  4611. unsigned char __user *end;
  4612. unsigned char val;
  4613. addr = (void __user *)uattr + sizeof(*attr);
  4614. end = (void __user *)uattr + size;
  4615. for (; addr < end; addr++) {
  4616. ret = get_user(val, addr);
  4617. if (ret)
  4618. return ret;
  4619. if (val)
  4620. goto err_size;
  4621. }
  4622. size = sizeof(*attr);
  4623. }
  4624. ret = copy_from_user(attr, uattr, size);
  4625. if (ret)
  4626. return -EFAULT;
  4627. /*
  4628. * If the type exists, the corresponding creation will verify
  4629. * the attr->config.
  4630. */
  4631. if (attr->type >= PERF_TYPE_MAX)
  4632. return -EINVAL;
  4633. if (attr->__reserved_1)
  4634. return -EINVAL;
  4635. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  4636. return -EINVAL;
  4637. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  4638. return -EINVAL;
  4639. out:
  4640. return ret;
  4641. err_size:
  4642. put_user(sizeof(*attr), &uattr->size);
  4643. ret = -E2BIG;
  4644. goto out;
  4645. }
  4646. static int
  4647. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  4648. {
  4649. struct perf_buffer *buffer = NULL, *old_buffer = NULL;
  4650. int ret = -EINVAL;
  4651. if (!output_event)
  4652. goto set;
  4653. /* don't allow circular references */
  4654. if (event == output_event)
  4655. goto out;
  4656. /*
  4657. * Don't allow cross-cpu buffers
  4658. */
  4659. if (output_event->cpu != event->cpu)
  4660. goto out;
  4661. /*
  4662. * If its not a per-cpu buffer, it must be the same task.
  4663. */
  4664. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  4665. goto out;
  4666. set:
  4667. mutex_lock(&event->mmap_mutex);
  4668. /* Can't redirect output if we've got an active mmap() */
  4669. if (atomic_read(&event->mmap_count))
  4670. goto unlock;
  4671. if (output_event) {
  4672. /* get the buffer we want to redirect to */
  4673. buffer = perf_buffer_get(output_event);
  4674. if (!buffer)
  4675. goto unlock;
  4676. }
  4677. old_buffer = event->buffer;
  4678. rcu_assign_pointer(event->buffer, buffer);
  4679. ret = 0;
  4680. unlock:
  4681. mutex_unlock(&event->mmap_mutex);
  4682. if (old_buffer)
  4683. perf_buffer_put(old_buffer);
  4684. out:
  4685. return ret;
  4686. }
  4687. /**
  4688. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  4689. *
  4690. * @attr_uptr: event_id type attributes for monitoring/sampling
  4691. * @pid: target pid
  4692. * @cpu: target cpu
  4693. * @group_fd: group leader event fd
  4694. */
  4695. SYSCALL_DEFINE5(perf_event_open,
  4696. struct perf_event_attr __user *, attr_uptr,
  4697. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  4698. {
  4699. struct perf_event *group_leader = NULL, *output_event = NULL;
  4700. struct perf_event *event, *sibling;
  4701. struct perf_event_attr attr;
  4702. struct perf_event_context *ctx;
  4703. struct file *event_file = NULL;
  4704. struct file *group_file = NULL;
  4705. struct task_struct *task = NULL;
  4706. struct pmu *pmu;
  4707. int event_fd;
  4708. int move_group = 0;
  4709. int fput_needed = 0;
  4710. int err;
  4711. /* for future expandability... */
  4712. if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
  4713. return -EINVAL;
  4714. err = perf_copy_attr(attr_uptr, &attr);
  4715. if (err)
  4716. return err;
  4717. if (!attr.exclude_kernel) {
  4718. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  4719. return -EACCES;
  4720. }
  4721. if (attr.freq) {
  4722. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  4723. return -EINVAL;
  4724. }
  4725. event_fd = get_unused_fd_flags(O_RDWR);
  4726. if (event_fd < 0)
  4727. return event_fd;
  4728. if (group_fd != -1) {
  4729. group_leader = perf_fget_light(group_fd, &fput_needed);
  4730. if (IS_ERR(group_leader)) {
  4731. err = PTR_ERR(group_leader);
  4732. goto err_fd;
  4733. }
  4734. group_file = group_leader->filp;
  4735. if (flags & PERF_FLAG_FD_OUTPUT)
  4736. output_event = group_leader;
  4737. if (flags & PERF_FLAG_FD_NO_GROUP)
  4738. group_leader = NULL;
  4739. }
  4740. if (pid != -1) {
  4741. task = find_lively_task_by_vpid(pid);
  4742. if (IS_ERR(task)) {
  4743. err = PTR_ERR(task);
  4744. goto err_group_fd;
  4745. }
  4746. }
  4747. event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
  4748. if (IS_ERR(event)) {
  4749. err = PTR_ERR(event);
  4750. goto err_task;
  4751. }
  4752. /*
  4753. * Special case software events and allow them to be part of
  4754. * any hardware group.
  4755. */
  4756. pmu = event->pmu;
  4757. if (group_leader &&
  4758. (is_software_event(event) != is_software_event(group_leader))) {
  4759. if (is_software_event(event)) {
  4760. /*
  4761. * If event and group_leader are not both a software
  4762. * event, and event is, then group leader is not.
  4763. *
  4764. * Allow the addition of software events to !software
  4765. * groups, this is safe because software events never
  4766. * fail to schedule.
  4767. */
  4768. pmu = group_leader->pmu;
  4769. } else if (is_software_event(group_leader) &&
  4770. (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  4771. /*
  4772. * In case the group is a pure software group, and we
  4773. * try to add a hardware event, move the whole group to
  4774. * the hardware context.
  4775. */
  4776. move_group = 1;
  4777. }
  4778. }
  4779. /*
  4780. * Get the target context (task or percpu):
  4781. */
  4782. ctx = find_get_context(pmu, task, cpu);
  4783. if (IS_ERR(ctx)) {
  4784. err = PTR_ERR(ctx);
  4785. goto err_alloc;
  4786. }
  4787. /*
  4788. * Look up the group leader (we will attach this event to it):
  4789. */
  4790. if (group_leader) {
  4791. err = -EINVAL;
  4792. /*
  4793. * Do not allow a recursive hierarchy (this new sibling
  4794. * becoming part of another group-sibling):
  4795. */
  4796. if (group_leader->group_leader != group_leader)
  4797. goto err_context;
  4798. /*
  4799. * Do not allow to attach to a group in a different
  4800. * task or CPU context:
  4801. */
  4802. if (move_group) {
  4803. if (group_leader->ctx->type != ctx->type)
  4804. goto err_context;
  4805. } else {
  4806. if (group_leader->ctx != ctx)
  4807. goto err_context;
  4808. }
  4809. /*
  4810. * Only a group leader can be exclusive or pinned
  4811. */
  4812. if (attr.exclusive || attr.pinned)
  4813. goto err_context;
  4814. }
  4815. if (output_event) {
  4816. err = perf_event_set_output(event, output_event);
  4817. if (err)
  4818. goto err_context;
  4819. }
  4820. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
  4821. if (IS_ERR(event_file)) {
  4822. err = PTR_ERR(event_file);
  4823. goto err_context;
  4824. }
  4825. if (move_group) {
  4826. struct perf_event_context *gctx = group_leader->ctx;
  4827. mutex_lock(&gctx->mutex);
  4828. perf_event_remove_from_context(group_leader);
  4829. list_for_each_entry(sibling, &group_leader->sibling_list,
  4830. group_entry) {
  4831. perf_event_remove_from_context(sibling);
  4832. put_ctx(gctx);
  4833. }
  4834. mutex_unlock(&gctx->mutex);
  4835. put_ctx(gctx);
  4836. }
  4837. event->filp = event_file;
  4838. WARN_ON_ONCE(ctx->parent_ctx);
  4839. mutex_lock(&ctx->mutex);
  4840. if (move_group) {
  4841. perf_install_in_context(ctx, group_leader, cpu);
  4842. get_ctx(ctx);
  4843. list_for_each_entry(sibling, &group_leader->sibling_list,
  4844. group_entry) {
  4845. perf_install_in_context(ctx, sibling, cpu);
  4846. get_ctx(ctx);
  4847. }
  4848. }
  4849. perf_install_in_context(ctx, event, cpu);
  4850. ++ctx->generation;
  4851. mutex_unlock(&ctx->mutex);
  4852. event->owner = current;
  4853. mutex_lock(&current->perf_event_mutex);
  4854. list_add_tail(&event->owner_entry, &current->perf_event_list);
  4855. mutex_unlock(&current->perf_event_mutex);
  4856. /*
  4857. * Precalculate sample_data sizes
  4858. */
  4859. perf_event__header_size(event);
  4860. perf_event__id_header_size(event);
  4861. /*
  4862. * Drop the reference on the group_event after placing the
  4863. * new event on the sibling_list. This ensures destruction
  4864. * of the group leader will find the pointer to itself in
  4865. * perf_group_detach().
  4866. */
  4867. fput_light(group_file, fput_needed);
  4868. fd_install(event_fd, event_file);
  4869. return event_fd;
  4870. err_context:
  4871. put_ctx(ctx);
  4872. err_alloc:
  4873. free_event(event);
  4874. err_task:
  4875. if (task)
  4876. put_task_struct(task);
  4877. err_group_fd:
  4878. fput_light(group_file, fput_needed);
  4879. err_fd:
  4880. put_unused_fd(event_fd);
  4881. return err;
  4882. }
  4883. /**
  4884. * perf_event_create_kernel_counter
  4885. *
  4886. * @attr: attributes of the counter to create
  4887. * @cpu: cpu in which the counter is bound
  4888. * @task: task to profile (NULL for percpu)
  4889. */
  4890. struct perf_event *
  4891. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  4892. struct task_struct *task,
  4893. perf_overflow_handler_t overflow_handler)
  4894. {
  4895. struct perf_event_context *ctx;
  4896. struct perf_event *event;
  4897. int err;
  4898. /*
  4899. * Get the target context (task or percpu):
  4900. */
  4901. event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
  4902. if (IS_ERR(event)) {
  4903. err = PTR_ERR(event);
  4904. goto err;
  4905. }
  4906. ctx = find_get_context(event->pmu, task, cpu);
  4907. if (IS_ERR(ctx)) {
  4908. err = PTR_ERR(ctx);
  4909. goto err_free;
  4910. }
  4911. event->filp = NULL;
  4912. WARN_ON_ONCE(ctx->parent_ctx);
  4913. mutex_lock(&ctx->mutex);
  4914. perf_install_in_context(ctx, event, cpu);
  4915. ++ctx->generation;
  4916. mutex_unlock(&ctx->mutex);
  4917. return event;
  4918. err_free:
  4919. free_event(event);
  4920. err:
  4921. return ERR_PTR(err);
  4922. }
  4923. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  4924. static void sync_child_event(struct perf_event *child_event,
  4925. struct task_struct *child)
  4926. {
  4927. struct perf_event *parent_event = child_event->parent;
  4928. u64 child_val;
  4929. if (child_event->attr.inherit_stat)
  4930. perf_event_read_event(child_event, child);
  4931. child_val = perf_event_count(child_event);
  4932. /*
  4933. * Add back the child's count to the parent's count:
  4934. */
  4935. atomic64_add(child_val, &parent_event->child_count);
  4936. atomic64_add(child_event->total_time_enabled,
  4937. &parent_event->child_total_time_enabled);
  4938. atomic64_add(child_event->total_time_running,
  4939. &parent_event->child_total_time_running);
  4940. /*
  4941. * Remove this event from the parent's list
  4942. */
  4943. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4944. mutex_lock(&parent_event->child_mutex);
  4945. list_del_init(&child_event->child_list);
  4946. mutex_unlock(&parent_event->child_mutex);
  4947. /*
  4948. * Release the parent event, if this was the last
  4949. * reference to it.
  4950. */
  4951. fput(parent_event->filp);
  4952. }
  4953. static void
  4954. __perf_event_exit_task(struct perf_event *child_event,
  4955. struct perf_event_context *child_ctx,
  4956. struct task_struct *child)
  4957. {
  4958. struct perf_event *parent_event;
  4959. perf_event_remove_from_context(child_event);
  4960. parent_event = child_event->parent;
  4961. /*
  4962. * It can happen that parent exits first, and has events
  4963. * that are still around due to the child reference. These
  4964. * events need to be zapped - but otherwise linger.
  4965. */
  4966. if (parent_event) {
  4967. sync_child_event(child_event, child);
  4968. free_event(child_event);
  4969. }
  4970. }
  4971. static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
  4972. {
  4973. struct perf_event *child_event, *tmp;
  4974. struct perf_event_context *child_ctx;
  4975. unsigned long flags;
  4976. if (likely(!child->perf_event_ctxp[ctxn])) {
  4977. perf_event_task(child, NULL, 0);
  4978. return;
  4979. }
  4980. local_irq_save(flags);
  4981. /*
  4982. * We can't reschedule here because interrupts are disabled,
  4983. * and either child is current or it is a task that can't be
  4984. * scheduled, so we are now safe from rescheduling changing
  4985. * our context.
  4986. */
  4987. child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
  4988. task_ctx_sched_out(child_ctx, EVENT_ALL);
  4989. /*
  4990. * Take the context lock here so that if find_get_context is
  4991. * reading child->perf_event_ctxp, we wait until it has
  4992. * incremented the context's refcount before we do put_ctx below.
  4993. */
  4994. raw_spin_lock(&child_ctx->lock);
  4995. child->perf_event_ctxp[ctxn] = NULL;
  4996. /*
  4997. * If this context is a clone; unclone it so it can't get
  4998. * swapped to another process while we're removing all
  4999. * the events from it.
  5000. */
  5001. unclone_ctx(child_ctx);
  5002. update_context_time(child_ctx);
  5003. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  5004. /*
  5005. * Report the task dead after unscheduling the events so that we
  5006. * won't get any samples after PERF_RECORD_EXIT. We can however still
  5007. * get a few PERF_RECORD_READ events.
  5008. */
  5009. perf_event_task(child, child_ctx, 0);
  5010. /*
  5011. * We can recurse on the same lock type through:
  5012. *
  5013. * __perf_event_exit_task()
  5014. * sync_child_event()
  5015. * fput(parent_event->filp)
  5016. * perf_release()
  5017. * mutex_lock(&ctx->mutex)
  5018. *
  5019. * But since its the parent context it won't be the same instance.
  5020. */
  5021. mutex_lock(&child_ctx->mutex);
  5022. again:
  5023. list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
  5024. group_entry)
  5025. __perf_event_exit_task(child_event, child_ctx, child);
  5026. list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
  5027. group_entry)
  5028. __perf_event_exit_task(child_event, child_ctx, child);
  5029. /*
  5030. * If the last event was a group event, it will have appended all
  5031. * its siblings to the list, but we obtained 'tmp' before that which
  5032. * will still point to the list head terminating the iteration.
  5033. */
  5034. if (!list_empty(&child_ctx->pinned_groups) ||
  5035. !list_empty(&child_ctx->flexible_groups))
  5036. goto again;
  5037. mutex_unlock(&child_ctx->mutex);
  5038. put_ctx(child_ctx);
  5039. }
  5040. /*
  5041. * When a child task exits, feed back event values to parent events.
  5042. */
  5043. void perf_event_exit_task(struct task_struct *child)
  5044. {
  5045. struct perf_event *event, *tmp;
  5046. int ctxn;
  5047. mutex_lock(&child->perf_event_mutex);
  5048. list_for_each_entry_safe(event, tmp, &child->perf_event_list,
  5049. owner_entry) {
  5050. list_del_init(&event->owner_entry);
  5051. /*
  5052. * Ensure the list deletion is visible before we clear
  5053. * the owner, closes a race against perf_release() where
  5054. * we need to serialize on the owner->perf_event_mutex.
  5055. */
  5056. smp_wmb();
  5057. event->owner = NULL;
  5058. }
  5059. mutex_unlock(&child->perf_event_mutex);
  5060. for_each_task_context_nr(ctxn)
  5061. perf_event_exit_task_context(child, ctxn);
  5062. }
  5063. static void perf_free_event(struct perf_event *event,
  5064. struct perf_event_context *ctx)
  5065. {
  5066. struct perf_event *parent = event->parent;
  5067. if (WARN_ON_ONCE(!parent))
  5068. return;
  5069. mutex_lock(&parent->child_mutex);
  5070. list_del_init(&event->child_list);
  5071. mutex_unlock(&parent->child_mutex);
  5072. fput(parent->filp);
  5073. perf_group_detach(event);
  5074. list_del_event(event, ctx);
  5075. free_event(event);
  5076. }
  5077. /*
  5078. * free an unexposed, unused context as created by inheritance by
  5079. * perf_event_init_task below, used by fork() in case of fail.
  5080. */
  5081. void perf_event_free_task(struct task_struct *task)
  5082. {
  5083. struct perf_event_context *ctx;
  5084. struct perf_event *event, *tmp;
  5085. int ctxn;
  5086. for_each_task_context_nr(ctxn) {
  5087. ctx = task->perf_event_ctxp[ctxn];
  5088. if (!ctx)
  5089. continue;
  5090. mutex_lock(&ctx->mutex);
  5091. again:
  5092. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
  5093. group_entry)
  5094. perf_free_event(event, ctx);
  5095. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  5096. group_entry)
  5097. perf_free_event(event, ctx);
  5098. if (!list_empty(&ctx->pinned_groups) ||
  5099. !list_empty(&ctx->flexible_groups))
  5100. goto again;
  5101. mutex_unlock(&ctx->mutex);
  5102. put_ctx(ctx);
  5103. }
  5104. }
  5105. void perf_event_delayed_put(struct task_struct *task)
  5106. {
  5107. int ctxn;
  5108. for_each_task_context_nr(ctxn)
  5109. WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  5110. }
  5111. /*
  5112. * inherit a event from parent task to child task:
  5113. */
  5114. static struct perf_event *
  5115. inherit_event(struct perf_event *parent_event,
  5116. struct task_struct *parent,
  5117. struct perf_event_context *parent_ctx,
  5118. struct task_struct *child,
  5119. struct perf_event *group_leader,
  5120. struct perf_event_context *child_ctx)
  5121. {
  5122. struct perf_event *child_event;
  5123. unsigned long flags;
  5124. /*
  5125. * Instead of creating recursive hierarchies of events,
  5126. * we link inherited events back to the original parent,
  5127. * which has a filp for sure, which we use as the reference
  5128. * count:
  5129. */
  5130. if (parent_event->parent)
  5131. parent_event = parent_event->parent;
  5132. child_event = perf_event_alloc(&parent_event->attr,
  5133. parent_event->cpu,
  5134. child,
  5135. group_leader, parent_event,
  5136. NULL);
  5137. if (IS_ERR(child_event))
  5138. return child_event;
  5139. get_ctx(child_ctx);
  5140. /*
  5141. * Make the child state follow the state of the parent event,
  5142. * not its attr.disabled bit. We hold the parent's mutex,
  5143. * so we won't race with perf_event_{en, dis}able_family.
  5144. */
  5145. if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  5146. child_event->state = PERF_EVENT_STATE_INACTIVE;
  5147. else
  5148. child_event->state = PERF_EVENT_STATE_OFF;
  5149. if (parent_event->attr.freq) {
  5150. u64 sample_period = parent_event->hw.sample_period;
  5151. struct hw_perf_event *hwc = &child_event->hw;
  5152. hwc->sample_period = sample_period;
  5153. hwc->last_period = sample_period;
  5154. local64_set(&hwc->period_left, sample_period);
  5155. }
  5156. child_event->ctx = child_ctx;
  5157. child_event->overflow_handler = parent_event->overflow_handler;
  5158. /*
  5159. * Precalculate sample_data sizes
  5160. */
  5161. perf_event__header_size(child_event);
  5162. perf_event__id_header_size(child_event);
  5163. /*
  5164. * Link it up in the child's context:
  5165. */
  5166. raw_spin_lock_irqsave(&child_ctx->lock, flags);
  5167. add_event_to_ctx(child_event, child_ctx);
  5168. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  5169. /*
  5170. * Get a reference to the parent filp - we will fput it
  5171. * when the child event exits. This is safe to do because
  5172. * we are in the parent and we know that the filp still
  5173. * exists and has a nonzero count:
  5174. */
  5175. atomic_long_inc(&parent_event->filp->f_count);
  5176. /*
  5177. * Link this into the parent event's child list
  5178. */
  5179. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  5180. mutex_lock(&parent_event->child_mutex);
  5181. list_add_tail(&child_event->child_list, &parent_event->child_list);
  5182. mutex_unlock(&parent_event->child_mutex);
  5183. return child_event;
  5184. }
  5185. static int inherit_group(struct perf_event *parent_event,
  5186. struct task_struct *parent,
  5187. struct perf_event_context *parent_ctx,
  5188. struct task_struct *child,
  5189. struct perf_event_context *child_ctx)
  5190. {
  5191. struct perf_event *leader;
  5192. struct perf_event *sub;
  5193. struct perf_event *child_ctr;
  5194. leader = inherit_event(parent_event, parent, parent_ctx,
  5195. child, NULL, child_ctx);
  5196. if (IS_ERR(leader))
  5197. return PTR_ERR(leader);
  5198. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  5199. child_ctr = inherit_event(sub, parent, parent_ctx,
  5200. child, leader, child_ctx);
  5201. if (IS_ERR(child_ctr))
  5202. return PTR_ERR(child_ctr);
  5203. }
  5204. return 0;
  5205. }
  5206. static int
  5207. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  5208. struct perf_event_context *parent_ctx,
  5209. struct task_struct *child, int ctxn,
  5210. int *inherited_all)
  5211. {
  5212. int ret;
  5213. struct perf_event_context *child_ctx;
  5214. if (!event->attr.inherit) {
  5215. *inherited_all = 0;
  5216. return 0;
  5217. }
  5218. child_ctx = child->perf_event_ctxp[ctxn];
  5219. if (!child_ctx) {
  5220. /*
  5221. * This is executed from the parent task context, so
  5222. * inherit events that have been marked for cloning.
  5223. * First allocate and initialize a context for the
  5224. * child.
  5225. */
  5226. child_ctx = alloc_perf_context(event->pmu, child);
  5227. if (!child_ctx)
  5228. return -ENOMEM;
  5229. child->perf_event_ctxp[ctxn] = child_ctx;
  5230. }
  5231. ret = inherit_group(event, parent, parent_ctx,
  5232. child, child_ctx);
  5233. if (ret)
  5234. *inherited_all = 0;
  5235. return ret;
  5236. }
  5237. /*
  5238. * Initialize the perf_event context in task_struct
  5239. */
  5240. int perf_event_init_context(struct task_struct *child, int ctxn)
  5241. {
  5242. struct perf_event_context *child_ctx, *parent_ctx;
  5243. struct perf_event_context *cloned_ctx;
  5244. struct perf_event *event;
  5245. struct task_struct *parent = current;
  5246. int inherited_all = 1;
  5247. unsigned long flags;
  5248. int ret = 0;
  5249. if (likely(!parent->perf_event_ctxp[ctxn]))
  5250. return 0;
  5251. /*
  5252. * If the parent's context is a clone, pin it so it won't get
  5253. * swapped under us.
  5254. */
  5255. parent_ctx = perf_pin_task_context(parent, ctxn);
  5256. /*
  5257. * No need to check if parent_ctx != NULL here; since we saw
  5258. * it non-NULL earlier, the only reason for it to become NULL
  5259. * is if we exit, and since we're currently in the middle of
  5260. * a fork we can't be exiting at the same time.
  5261. */
  5262. /*
  5263. * Lock the parent list. No need to lock the child - not PID
  5264. * hashed yet and not running, so nobody can access it.
  5265. */
  5266. mutex_lock(&parent_ctx->mutex);
  5267. /*
  5268. * We dont have to disable NMIs - we are only looking at
  5269. * the list, not manipulating it:
  5270. */
  5271. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  5272. ret = inherit_task_group(event, parent, parent_ctx,
  5273. child, ctxn, &inherited_all);
  5274. if (ret)
  5275. break;
  5276. }
  5277. /*
  5278. * We can't hold ctx->lock when iterating the ->flexible_group list due
  5279. * to allocations, but we need to prevent rotation because
  5280. * rotate_ctx() will change the list from interrupt context.
  5281. */
  5282. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  5283. parent_ctx->rotate_disable = 1;
  5284. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  5285. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  5286. ret = inherit_task_group(event, parent, parent_ctx,
  5287. child, ctxn, &inherited_all);
  5288. if (ret)
  5289. break;
  5290. }
  5291. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  5292. parent_ctx->rotate_disable = 0;
  5293. child_ctx = child->perf_event_ctxp[ctxn];
  5294. if (child_ctx && inherited_all) {
  5295. /*
  5296. * Mark the child context as a clone of the parent
  5297. * context, or of whatever the parent is a clone of.
  5298. *
  5299. * Note that if the parent is a clone, the holding of
  5300. * parent_ctx->lock avoids it from being uncloned.
  5301. */
  5302. cloned_ctx = parent_ctx->parent_ctx;
  5303. if (cloned_ctx) {
  5304. child_ctx->parent_ctx = cloned_ctx;
  5305. child_ctx->parent_gen = parent_ctx->parent_gen;
  5306. } else {
  5307. child_ctx->parent_ctx = parent_ctx;
  5308. child_ctx->parent_gen = parent_ctx->generation;
  5309. }
  5310. get_ctx(child_ctx->parent_ctx);
  5311. }
  5312. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  5313. mutex_unlock(&parent_ctx->mutex);
  5314. perf_unpin_context(parent_ctx);
  5315. return ret;
  5316. }
  5317. /*
  5318. * Initialize the perf_event context in task_struct
  5319. */
  5320. int perf_event_init_task(struct task_struct *child)
  5321. {
  5322. int ctxn, ret;
  5323. memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
  5324. mutex_init(&child->perf_event_mutex);
  5325. INIT_LIST_HEAD(&child->perf_event_list);
  5326. for_each_task_context_nr(ctxn) {
  5327. ret = perf_event_init_context(child, ctxn);
  5328. if (ret)
  5329. return ret;
  5330. }
  5331. return 0;
  5332. }
  5333. static void __init perf_event_init_all_cpus(void)
  5334. {
  5335. struct swevent_htable *swhash;
  5336. int cpu;
  5337. for_each_possible_cpu(cpu) {
  5338. swhash = &per_cpu(swevent_htable, cpu);
  5339. mutex_init(&swhash->hlist_mutex);
  5340. INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
  5341. }
  5342. }
  5343. static void __cpuinit perf_event_init_cpu(int cpu)
  5344. {
  5345. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5346. mutex_lock(&swhash->hlist_mutex);
  5347. if (swhash->hlist_refcount > 0) {
  5348. struct swevent_hlist *hlist;
  5349. hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  5350. WARN_ON(!hlist);
  5351. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  5352. }
  5353. mutex_unlock(&swhash->hlist_mutex);
  5354. }
  5355. #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
  5356. static void perf_pmu_rotate_stop(struct pmu *pmu)
  5357. {
  5358. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  5359. WARN_ON(!irqs_disabled());
  5360. list_del_init(&cpuctx->rotation_list);
  5361. }
  5362. static void __perf_event_exit_context(void *__info)
  5363. {
  5364. struct perf_event_context *ctx = __info;
  5365. struct perf_event *event, *tmp;
  5366. perf_pmu_rotate_stop(ctx->pmu);
  5367. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  5368. __perf_event_remove_from_context(event);
  5369. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
  5370. __perf_event_remove_from_context(event);
  5371. }
  5372. static void perf_event_exit_cpu_context(int cpu)
  5373. {
  5374. struct perf_event_context *ctx;
  5375. struct pmu *pmu;
  5376. int idx;
  5377. idx = srcu_read_lock(&pmus_srcu);
  5378. list_for_each_entry_rcu(pmu, &pmus, entry) {
  5379. ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
  5380. mutex_lock(&ctx->mutex);
  5381. smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
  5382. mutex_unlock(&ctx->mutex);
  5383. }
  5384. srcu_read_unlock(&pmus_srcu, idx);
  5385. }
  5386. static void perf_event_exit_cpu(int cpu)
  5387. {
  5388. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5389. mutex_lock(&swhash->hlist_mutex);
  5390. swevent_hlist_release(swhash);
  5391. mutex_unlock(&swhash->hlist_mutex);
  5392. perf_event_exit_cpu_context(cpu);
  5393. }
  5394. #else
  5395. static inline void perf_event_exit_cpu(int cpu) { }
  5396. #endif
  5397. static int
  5398. perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
  5399. {
  5400. int cpu;
  5401. for_each_online_cpu(cpu)
  5402. perf_event_exit_cpu(cpu);
  5403. return NOTIFY_OK;
  5404. }
  5405. /*
  5406. * Run the perf reboot notifier at the very last possible moment so that
  5407. * the generic watchdog code runs as long as possible.
  5408. */
  5409. static struct notifier_block perf_reboot_notifier = {
  5410. .notifier_call = perf_reboot,
  5411. .priority = INT_MIN,
  5412. };
  5413. static int __cpuinit
  5414. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  5415. {
  5416. unsigned int cpu = (long)hcpu;
  5417. switch (action & ~CPU_TASKS_FROZEN) {
  5418. case CPU_UP_PREPARE:
  5419. case CPU_DOWN_FAILED:
  5420. perf_event_init_cpu(cpu);
  5421. break;
  5422. case CPU_UP_CANCELED:
  5423. case CPU_DOWN_PREPARE:
  5424. perf_event_exit_cpu(cpu);
  5425. break;
  5426. default:
  5427. break;
  5428. }
  5429. return NOTIFY_OK;
  5430. }
  5431. void __init perf_event_init(void)
  5432. {
  5433. int ret;
  5434. idr_init(&pmu_idr);
  5435. perf_event_init_all_cpus();
  5436. init_srcu_struct(&pmus_srcu);
  5437. perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
  5438. perf_pmu_register(&perf_cpu_clock, NULL, -1);
  5439. perf_pmu_register(&perf_task_clock, NULL, -1);
  5440. perf_tp_register();
  5441. perf_cpu_notifier(perf_cpu_notify);
  5442. register_reboot_notifier(&perf_reboot_notifier);
  5443. ret = init_hw_breakpoint();
  5444. WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
  5445. }
  5446. static int __init perf_event_sysfs_init(void)
  5447. {
  5448. struct pmu *pmu;
  5449. int ret;
  5450. mutex_lock(&pmus_lock);
  5451. ret = bus_register(&pmu_bus);
  5452. if (ret)
  5453. goto unlock;
  5454. list_for_each_entry(pmu, &pmus, entry) {
  5455. if (!pmu->name || pmu->type < 0)
  5456. continue;
  5457. ret = pmu_dev_alloc(pmu);
  5458. WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
  5459. }
  5460. pmu_bus_running = 1;
  5461. ret = 0;
  5462. unlock:
  5463. mutex_unlock(&pmus_lock);
  5464. return ret;
  5465. }
  5466. device_initcall(perf_event_sysfs_init);