core.c 182 KB

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