perf_event.c 169 KB

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