perf_event.c 152 KB

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