perf_event.c 169 KB

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