perf_event.c 152 KB

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