core.c 167 KB

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