perf_event.c 168 KB

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