perf_event.c 169 KB

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