core.c 168 KB

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