perf_event.c 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918
  1. /*
  2. * Performance events core code:
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  7. * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  8. *
  9. * For licensing details see kernel-base/COPYING
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/mm.h>
  13. #include <linux/cpu.h>
  14. #include <linux/smp.h>
  15. #include <linux/file.h>
  16. #include <linux/poll.h>
  17. #include <linux/slab.h>
  18. #include <linux/hash.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/dcache.h>
  21. #include <linux/percpu.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/vmstat.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/hardirq.h>
  26. #include <linux/rculist.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/anon_inodes.h>
  30. #include <linux/kernel_stat.h>
  31. #include <linux/perf_event.h>
  32. #include <linux/ftrace_event.h>
  33. #include <linux/hw_breakpoint.h>
  34. #include <asm/irq_regs.h>
  35. /*
  36. * Each CPU has a list of per CPU events:
  37. */
  38. static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  39. int perf_max_events __read_mostly = 1;
  40. static int perf_reserved_percpu __read_mostly;
  41. static int perf_overcommit __read_mostly = 1;
  42. static atomic_t nr_events __read_mostly;
  43. static atomic_t nr_mmap_events __read_mostly;
  44. static atomic_t nr_comm_events __read_mostly;
  45. static atomic_t nr_task_events __read_mostly;
  46. /*
  47. * perf event paranoia level:
  48. * -1 - not paranoid at all
  49. * 0 - disallow raw tracepoint access for unpriv
  50. * 1 - disallow cpu events for unpriv
  51. * 2 - disallow kernel profiling for unpriv
  52. */
  53. int sysctl_perf_event_paranoid __read_mostly = 1;
  54. int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
  55. /*
  56. * max perf event sample rate
  57. */
  58. int sysctl_perf_event_sample_rate __read_mostly = 100000;
  59. static atomic64_t perf_event_id;
  60. /*
  61. * Lock for (sysadmin-configurable) event reservations:
  62. */
  63. static DEFINE_SPINLOCK(perf_resource_lock);
  64. /*
  65. * Architecture provided APIs - weak aliases:
  66. */
  67. extern __weak const struct pmu *hw_perf_event_init(struct perf_event *event)
  68. {
  69. return NULL;
  70. }
  71. void __weak hw_perf_disable(void) { barrier(); }
  72. void __weak hw_perf_enable(void) { barrier(); }
  73. void __weak perf_event_print_debug(void) { }
  74. static DEFINE_PER_CPU(int, perf_disable_count);
  75. void perf_disable(void)
  76. {
  77. if (!__get_cpu_var(perf_disable_count)++)
  78. hw_perf_disable();
  79. }
  80. void perf_enable(void)
  81. {
  82. if (!--__get_cpu_var(perf_disable_count))
  83. hw_perf_enable();
  84. }
  85. static void get_ctx(struct perf_event_context *ctx)
  86. {
  87. WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
  88. }
  89. static void free_ctx(struct rcu_head *head)
  90. {
  91. struct perf_event_context *ctx;
  92. ctx = container_of(head, struct perf_event_context, rcu_head);
  93. kfree(ctx);
  94. }
  95. static void put_ctx(struct perf_event_context *ctx)
  96. {
  97. if (atomic_dec_and_test(&ctx->refcount)) {
  98. if (ctx->parent_ctx)
  99. put_ctx(ctx->parent_ctx);
  100. if (ctx->task)
  101. put_task_struct(ctx->task);
  102. call_rcu(&ctx->rcu_head, free_ctx);
  103. }
  104. }
  105. static void unclone_ctx(struct perf_event_context *ctx)
  106. {
  107. if (ctx->parent_ctx) {
  108. put_ctx(ctx->parent_ctx);
  109. ctx->parent_ctx = NULL;
  110. }
  111. }
  112. /*
  113. * If we inherit events we want to return the parent event id
  114. * to userspace.
  115. */
  116. static u64 primary_event_id(struct perf_event *event)
  117. {
  118. u64 id = event->id;
  119. if (event->parent)
  120. id = event->parent->id;
  121. return id;
  122. }
  123. /*
  124. * Get the perf_event_context for a task and lock it.
  125. * This has to cope with with the fact that until it is locked,
  126. * the context could get moved to another task.
  127. */
  128. static struct perf_event_context *
  129. perf_lock_task_context(struct task_struct *task, unsigned long *flags)
  130. {
  131. struct perf_event_context *ctx;
  132. rcu_read_lock();
  133. retry:
  134. ctx = rcu_dereference(task->perf_event_ctxp);
  135. if (ctx) {
  136. /*
  137. * If this context is a clone of another, it might
  138. * get swapped for another underneath us by
  139. * perf_event_task_sched_out, though the
  140. * rcu_read_lock() protects us from any context
  141. * getting freed. Lock the context and check if it
  142. * got swapped before we could get the lock, and retry
  143. * if so. If we locked the right context, then it
  144. * can't get swapped on us any more.
  145. */
  146. raw_spin_lock_irqsave(&ctx->lock, *flags);
  147. if (ctx != rcu_dereference(task->perf_event_ctxp)) {
  148. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  149. goto retry;
  150. }
  151. if (!atomic_inc_not_zero(&ctx->refcount)) {
  152. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  153. ctx = NULL;
  154. }
  155. }
  156. rcu_read_unlock();
  157. return ctx;
  158. }
  159. /*
  160. * Get the context for a task and increment its pin_count so it
  161. * can't get swapped to another task. This also increments its
  162. * reference count so that the context can't get freed.
  163. */
  164. static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
  165. {
  166. struct perf_event_context *ctx;
  167. unsigned long flags;
  168. ctx = perf_lock_task_context(task, &flags);
  169. if (ctx) {
  170. ++ctx->pin_count;
  171. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  172. }
  173. return ctx;
  174. }
  175. static void perf_unpin_context(struct perf_event_context *ctx)
  176. {
  177. unsigned long flags;
  178. raw_spin_lock_irqsave(&ctx->lock, flags);
  179. --ctx->pin_count;
  180. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  181. put_ctx(ctx);
  182. }
  183. static inline u64 perf_clock(void)
  184. {
  185. return cpu_clock(raw_smp_processor_id());
  186. }
  187. /*
  188. * Update the record of the current time in a context.
  189. */
  190. static void update_context_time(struct perf_event_context *ctx)
  191. {
  192. u64 now = perf_clock();
  193. ctx->time += now - ctx->timestamp;
  194. ctx->timestamp = now;
  195. }
  196. /*
  197. * Update the total_time_enabled and total_time_running fields for a event.
  198. */
  199. static void update_event_times(struct perf_event *event)
  200. {
  201. struct perf_event_context *ctx = event->ctx;
  202. u64 run_end;
  203. if (event->state < PERF_EVENT_STATE_INACTIVE ||
  204. event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
  205. return;
  206. if (ctx->is_active)
  207. run_end = ctx->time;
  208. else
  209. run_end = event->tstamp_stopped;
  210. event->total_time_enabled = run_end - event->tstamp_enabled;
  211. if (event->state == PERF_EVENT_STATE_INACTIVE)
  212. run_end = event->tstamp_stopped;
  213. else
  214. run_end = ctx->time;
  215. event->total_time_running = run_end - event->tstamp_running;
  216. }
  217. /*
  218. * Update total_time_enabled and total_time_running for all events in a group.
  219. */
  220. static void update_group_times(struct perf_event *leader)
  221. {
  222. struct perf_event *event;
  223. update_event_times(leader);
  224. list_for_each_entry(event, &leader->sibling_list, group_entry)
  225. update_event_times(event);
  226. }
  227. static struct list_head *
  228. ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
  229. {
  230. if (event->attr.pinned)
  231. return &ctx->pinned_groups;
  232. else
  233. return &ctx->flexible_groups;
  234. }
  235. /*
  236. * Add a event from the lists for its context.
  237. * Must be called with ctx->mutex and ctx->lock held.
  238. */
  239. static void
  240. list_add_event(struct perf_event *event, struct perf_event_context *ctx)
  241. {
  242. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
  243. event->attach_state |= PERF_ATTACH_CONTEXT;
  244. /*
  245. * If we're a stand alone event or group leader, we go to the context
  246. * list, group events are kept attached to the group so that
  247. * perf_group_detach can, at all times, locate all siblings.
  248. */
  249. if (event->group_leader == event) {
  250. struct list_head *list;
  251. if (is_software_event(event))
  252. event->group_flags |= PERF_GROUP_SOFTWARE;
  253. list = ctx_group_list(event, ctx);
  254. list_add_tail(&event->group_entry, list);
  255. }
  256. list_add_rcu(&event->event_entry, &ctx->event_list);
  257. ctx->nr_events++;
  258. if (event->attr.inherit_stat)
  259. ctx->nr_stat++;
  260. }
  261. static void perf_group_attach(struct perf_event *event)
  262. {
  263. struct perf_event *group_leader = event->group_leader;
  264. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP);
  265. event->attach_state |= PERF_ATTACH_GROUP;
  266. if (group_leader == event)
  267. return;
  268. if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
  269. !is_software_event(event))
  270. group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
  271. list_add_tail(&event->group_entry, &group_leader->sibling_list);
  272. group_leader->nr_siblings++;
  273. }
  274. /*
  275. * Remove a event from the lists for its context.
  276. * Must be called with ctx->mutex and ctx->lock held.
  277. */
  278. static void
  279. list_del_event(struct perf_event *event, struct perf_event_context *ctx)
  280. {
  281. /*
  282. * We can have double detach due to exit/hot-unplug + close.
  283. */
  284. if (!(event->attach_state & PERF_ATTACH_CONTEXT))
  285. return;
  286. event->attach_state &= ~PERF_ATTACH_CONTEXT;
  287. ctx->nr_events--;
  288. if (event->attr.inherit_stat)
  289. ctx->nr_stat--;
  290. list_del_rcu(&event->event_entry);
  291. if (event->group_leader == event)
  292. list_del_init(&event->group_entry);
  293. update_group_times(event);
  294. /*
  295. * If event was in error state, then keep it
  296. * that way, otherwise bogus counts will be
  297. * returned on read(). The only way to get out
  298. * of error state is by explicit re-enabling
  299. * of the event
  300. */
  301. if (event->state > PERF_EVENT_STATE_OFF)
  302. event->state = PERF_EVENT_STATE_OFF;
  303. }
  304. static void perf_group_detach(struct perf_event *event)
  305. {
  306. struct perf_event *sibling, *tmp;
  307. struct list_head *list = NULL;
  308. /*
  309. * We can have double detach due to exit/hot-unplug + close.
  310. */
  311. if (!(event->attach_state & PERF_ATTACH_GROUP))
  312. return;
  313. event->attach_state &= ~PERF_ATTACH_GROUP;
  314. /*
  315. * If this is a sibling, remove it from its group.
  316. */
  317. if (event->group_leader != event) {
  318. list_del_init(&event->group_entry);
  319. event->group_leader->nr_siblings--;
  320. return;
  321. }
  322. if (!list_empty(&event->group_entry))
  323. list = &event->group_entry;
  324. /*
  325. * If this was a group event with sibling events then
  326. * upgrade the siblings to singleton events by adding them
  327. * to whatever list we are on.
  328. */
  329. list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
  330. if (list)
  331. list_move_tail(&sibling->group_entry, list);
  332. sibling->group_leader = sibling;
  333. /* Inherit group flags from the previous leader */
  334. sibling->group_flags = event->group_flags;
  335. }
  336. }
  337. static void
  338. event_sched_out(struct perf_event *event,
  339. struct perf_cpu_context *cpuctx,
  340. struct perf_event_context *ctx)
  341. {
  342. if (event->state != PERF_EVENT_STATE_ACTIVE)
  343. return;
  344. event->state = PERF_EVENT_STATE_INACTIVE;
  345. if (event->pending_disable) {
  346. event->pending_disable = 0;
  347. event->state = PERF_EVENT_STATE_OFF;
  348. }
  349. event->tstamp_stopped = ctx->time;
  350. event->pmu->disable(event);
  351. event->oncpu = -1;
  352. if (!is_software_event(event))
  353. cpuctx->active_oncpu--;
  354. ctx->nr_active--;
  355. if (event->attr.exclusive || !cpuctx->active_oncpu)
  356. cpuctx->exclusive = 0;
  357. }
  358. static void
  359. group_sched_out(struct perf_event *group_event,
  360. struct perf_cpu_context *cpuctx,
  361. struct perf_event_context *ctx)
  362. {
  363. struct perf_event *event;
  364. if (group_event->state != PERF_EVENT_STATE_ACTIVE)
  365. return;
  366. event_sched_out(group_event, cpuctx, ctx);
  367. /*
  368. * Schedule out siblings (if any):
  369. */
  370. list_for_each_entry(event, &group_event->sibling_list, group_entry)
  371. event_sched_out(event, cpuctx, ctx);
  372. if (group_event->attr.exclusive)
  373. cpuctx->exclusive = 0;
  374. }
  375. /*
  376. * Cross CPU call to remove a performance event
  377. *
  378. * We disable the event on the hardware level first. After that we
  379. * remove it from the context list.
  380. */
  381. static void __perf_event_remove_from_context(void *info)
  382. {
  383. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  384. struct perf_event *event = info;
  385. struct perf_event_context *ctx = event->ctx;
  386. /*
  387. * If this is a task context, we need to check whether it is
  388. * the current task context of this cpu. If not it has been
  389. * scheduled out before the smp call arrived.
  390. */
  391. if (ctx->task && cpuctx->task_ctx != ctx)
  392. return;
  393. raw_spin_lock(&ctx->lock);
  394. /*
  395. * Protect the list operation against NMI by disabling the
  396. * events on a global level.
  397. */
  398. perf_disable();
  399. event_sched_out(event, cpuctx, ctx);
  400. list_del_event(event, ctx);
  401. if (!ctx->task) {
  402. /*
  403. * Allow more per task events with respect to the
  404. * reservation:
  405. */
  406. cpuctx->max_pertask =
  407. min(perf_max_events - ctx->nr_events,
  408. perf_max_events - perf_reserved_percpu);
  409. }
  410. perf_enable();
  411. raw_spin_unlock(&ctx->lock);
  412. }
  413. /*
  414. * Remove the event from a task's (or a CPU's) list of events.
  415. *
  416. * Must be called with ctx->mutex held.
  417. *
  418. * CPU events are removed with a smp call. For task events we only
  419. * call when the task is on a CPU.
  420. *
  421. * If event->ctx is a cloned context, callers must make sure that
  422. * every task struct that event->ctx->task could possibly point to
  423. * remains valid. This is OK when called from perf_release since
  424. * that only calls us on the top-level context, which can't be a clone.
  425. * When called from perf_event_exit_task, it's OK because the
  426. * context has been detached from its task.
  427. */
  428. static void perf_event_remove_from_context(struct perf_event *event)
  429. {
  430. struct perf_event_context *ctx = event->ctx;
  431. struct task_struct *task = ctx->task;
  432. if (!task) {
  433. /*
  434. * Per cpu events are removed via an smp call and
  435. * the removal is always successful.
  436. */
  437. smp_call_function_single(event->cpu,
  438. __perf_event_remove_from_context,
  439. event, 1);
  440. return;
  441. }
  442. retry:
  443. task_oncpu_function_call(task, __perf_event_remove_from_context,
  444. event);
  445. raw_spin_lock_irq(&ctx->lock);
  446. /*
  447. * If the context is active we need to retry the smp call.
  448. */
  449. if (ctx->nr_active && !list_empty(&event->group_entry)) {
  450. raw_spin_unlock_irq(&ctx->lock);
  451. goto retry;
  452. }
  453. /*
  454. * The lock prevents that this context is scheduled in so we
  455. * can remove the event safely, if the call above did not
  456. * succeed.
  457. */
  458. if (!list_empty(&event->group_entry))
  459. list_del_event(event, ctx);
  460. raw_spin_unlock_irq(&ctx->lock);
  461. }
  462. /*
  463. * Cross CPU call to disable a performance event
  464. */
  465. static void __perf_event_disable(void *info)
  466. {
  467. struct perf_event *event = info;
  468. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  469. struct perf_event_context *ctx = event->ctx;
  470. /*
  471. * If this is a per-task event, need to check whether this
  472. * event's task is the current task on this cpu.
  473. */
  474. if (ctx->task && cpuctx->task_ctx != ctx)
  475. return;
  476. raw_spin_lock(&ctx->lock);
  477. /*
  478. * If the event is on, turn it off.
  479. * If it is in error state, leave it in error state.
  480. */
  481. if (event->state >= PERF_EVENT_STATE_INACTIVE) {
  482. update_context_time(ctx);
  483. update_group_times(event);
  484. if (event == event->group_leader)
  485. group_sched_out(event, cpuctx, ctx);
  486. else
  487. event_sched_out(event, cpuctx, ctx);
  488. event->state = PERF_EVENT_STATE_OFF;
  489. }
  490. raw_spin_unlock(&ctx->lock);
  491. }
  492. /*
  493. * Disable a event.
  494. *
  495. * If event->ctx is a cloned context, callers must make sure that
  496. * every task struct that event->ctx->task could possibly point to
  497. * remains valid. This condition is satisifed when called through
  498. * perf_event_for_each_child or perf_event_for_each because they
  499. * hold the top-level event's child_mutex, so any descendant that
  500. * goes to exit will block in sync_child_event.
  501. * When called from perf_pending_event it's OK because event->ctx
  502. * is the current context on this CPU and preemption is disabled,
  503. * hence we can't get into perf_event_task_sched_out for this context.
  504. */
  505. void perf_event_disable(struct perf_event *event)
  506. {
  507. struct perf_event_context *ctx = event->ctx;
  508. struct task_struct *task = ctx->task;
  509. if (!task) {
  510. /*
  511. * Disable the event on the cpu that it's on
  512. */
  513. smp_call_function_single(event->cpu, __perf_event_disable,
  514. event, 1);
  515. return;
  516. }
  517. retry:
  518. task_oncpu_function_call(task, __perf_event_disable, event);
  519. raw_spin_lock_irq(&ctx->lock);
  520. /*
  521. * If the event is still active, we need to retry the cross-call.
  522. */
  523. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  524. raw_spin_unlock_irq(&ctx->lock);
  525. goto retry;
  526. }
  527. /*
  528. * Since we have the lock this context can't be scheduled
  529. * in, so we can change the state safely.
  530. */
  531. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  532. update_group_times(event);
  533. event->state = PERF_EVENT_STATE_OFF;
  534. }
  535. raw_spin_unlock_irq(&ctx->lock);
  536. }
  537. static int
  538. event_sched_in(struct perf_event *event,
  539. struct perf_cpu_context *cpuctx,
  540. struct perf_event_context *ctx)
  541. {
  542. if (event->state <= PERF_EVENT_STATE_OFF)
  543. return 0;
  544. event->state = PERF_EVENT_STATE_ACTIVE;
  545. event->oncpu = smp_processor_id();
  546. /*
  547. * The new state must be visible before we turn it on in the hardware:
  548. */
  549. smp_wmb();
  550. if (event->pmu->enable(event)) {
  551. event->state = PERF_EVENT_STATE_INACTIVE;
  552. event->oncpu = -1;
  553. return -EAGAIN;
  554. }
  555. event->tstamp_running += ctx->time - event->tstamp_stopped;
  556. if (!is_software_event(event))
  557. cpuctx->active_oncpu++;
  558. ctx->nr_active++;
  559. if (event->attr.exclusive)
  560. cpuctx->exclusive = 1;
  561. return 0;
  562. }
  563. static int
  564. group_sched_in(struct perf_event *group_event,
  565. struct perf_cpu_context *cpuctx,
  566. struct perf_event_context *ctx)
  567. {
  568. struct perf_event *event, *partial_group = NULL;
  569. const struct pmu *pmu = group_event->pmu;
  570. bool txn = false;
  571. if (group_event->state == PERF_EVENT_STATE_OFF)
  572. return 0;
  573. /* Check if group transaction availabe */
  574. if (pmu->start_txn)
  575. txn = true;
  576. if (txn)
  577. pmu->start_txn(pmu);
  578. if (event_sched_in(group_event, cpuctx, ctx)) {
  579. if (txn)
  580. pmu->cancel_txn(pmu);
  581. return -EAGAIN;
  582. }
  583. /*
  584. * Schedule in siblings as one group (if any):
  585. */
  586. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  587. if (event_sched_in(event, cpuctx, ctx)) {
  588. partial_group = event;
  589. goto group_error;
  590. }
  591. }
  592. if (!txn || !pmu->commit_txn(pmu))
  593. return 0;
  594. group_error:
  595. /*
  596. * Groups can be scheduled in as one unit only, so undo any
  597. * partial group before returning:
  598. */
  599. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  600. if (event == partial_group)
  601. break;
  602. event_sched_out(event, cpuctx, ctx);
  603. }
  604. event_sched_out(group_event, cpuctx, ctx);
  605. if (txn)
  606. pmu->cancel_txn(pmu);
  607. return -EAGAIN;
  608. }
  609. /*
  610. * Work out whether we can put this event group on the CPU now.
  611. */
  612. static int group_can_go_on(struct perf_event *event,
  613. struct perf_cpu_context *cpuctx,
  614. int can_add_hw)
  615. {
  616. /*
  617. * Groups consisting entirely of software events can always go on.
  618. */
  619. if (event->group_flags & PERF_GROUP_SOFTWARE)
  620. return 1;
  621. /*
  622. * If an exclusive group is already on, no other hardware
  623. * events can go on.
  624. */
  625. if (cpuctx->exclusive)
  626. return 0;
  627. /*
  628. * If this group is exclusive and there are already
  629. * events on the CPU, it can't go on.
  630. */
  631. if (event->attr.exclusive && cpuctx->active_oncpu)
  632. return 0;
  633. /*
  634. * Otherwise, try to add it if all previous groups were able
  635. * to go on.
  636. */
  637. return can_add_hw;
  638. }
  639. static void add_event_to_ctx(struct perf_event *event,
  640. struct perf_event_context *ctx)
  641. {
  642. list_add_event(event, ctx);
  643. perf_group_attach(event);
  644. event->tstamp_enabled = ctx->time;
  645. event->tstamp_running = ctx->time;
  646. event->tstamp_stopped = ctx->time;
  647. }
  648. /*
  649. * Cross CPU call to install and enable a performance event
  650. *
  651. * Must be called with ctx->mutex held
  652. */
  653. static void __perf_install_in_context(void *info)
  654. {
  655. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  656. struct perf_event *event = info;
  657. struct perf_event_context *ctx = event->ctx;
  658. struct perf_event *leader = event->group_leader;
  659. int err;
  660. /*
  661. * If this is a task context, we need to check whether it is
  662. * the current task context of this cpu. If not it has been
  663. * scheduled out before the smp call arrived.
  664. * Or possibly this is the right context but it isn't
  665. * on this cpu because it had no events.
  666. */
  667. if (ctx->task && cpuctx->task_ctx != ctx) {
  668. if (cpuctx->task_ctx || ctx->task != current)
  669. return;
  670. cpuctx->task_ctx = ctx;
  671. }
  672. raw_spin_lock(&ctx->lock);
  673. ctx->is_active = 1;
  674. update_context_time(ctx);
  675. /*
  676. * Protect the list operation against NMI by disabling the
  677. * events on a global level. NOP for non NMI based events.
  678. */
  679. perf_disable();
  680. add_event_to_ctx(event, ctx);
  681. if (event->cpu != -1 && event->cpu != smp_processor_id())
  682. goto unlock;
  683. /*
  684. * Don't put the event on if it is disabled or if
  685. * it is in a group and the group isn't on.
  686. */
  687. if (event->state != PERF_EVENT_STATE_INACTIVE ||
  688. (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
  689. goto unlock;
  690. /*
  691. * An exclusive event can't go on if there are already active
  692. * hardware events, and no hardware event can go on if there
  693. * is already an exclusive event on.
  694. */
  695. if (!group_can_go_on(event, cpuctx, 1))
  696. err = -EEXIST;
  697. else
  698. err = event_sched_in(event, cpuctx, ctx);
  699. if (err) {
  700. /*
  701. * This event couldn't go on. If it is in a group
  702. * then we have to pull the whole group off.
  703. * If the event group is pinned then put it in error state.
  704. */
  705. if (leader != event)
  706. group_sched_out(leader, cpuctx, ctx);
  707. if (leader->attr.pinned) {
  708. update_group_times(leader);
  709. leader->state = PERF_EVENT_STATE_ERROR;
  710. }
  711. }
  712. if (!err && !ctx->task && cpuctx->max_pertask)
  713. cpuctx->max_pertask--;
  714. unlock:
  715. perf_enable();
  716. raw_spin_unlock(&ctx->lock);
  717. }
  718. /*
  719. * Attach a performance event to a context
  720. *
  721. * First we add the event to the list with the hardware enable bit
  722. * in event->hw_config cleared.
  723. *
  724. * If the event is attached to a task which is on a CPU we use a smp
  725. * call to enable it in the task context. The task might have been
  726. * scheduled away, but we check this in the smp call again.
  727. *
  728. * Must be called with ctx->mutex held.
  729. */
  730. static void
  731. perf_install_in_context(struct perf_event_context *ctx,
  732. struct perf_event *event,
  733. int cpu)
  734. {
  735. struct task_struct *task = ctx->task;
  736. if (!task) {
  737. /*
  738. * Per cpu events are installed via an smp call and
  739. * the install is always successful.
  740. */
  741. smp_call_function_single(cpu, __perf_install_in_context,
  742. event, 1);
  743. return;
  744. }
  745. retry:
  746. task_oncpu_function_call(task, __perf_install_in_context,
  747. event);
  748. raw_spin_lock_irq(&ctx->lock);
  749. /*
  750. * we need to retry the smp call.
  751. */
  752. if (ctx->is_active && list_empty(&event->group_entry)) {
  753. raw_spin_unlock_irq(&ctx->lock);
  754. goto retry;
  755. }
  756. /*
  757. * The lock prevents that this context is scheduled in so we
  758. * can add the event safely, if it the call above did not
  759. * succeed.
  760. */
  761. if (list_empty(&event->group_entry))
  762. add_event_to_ctx(event, ctx);
  763. raw_spin_unlock_irq(&ctx->lock);
  764. }
  765. /*
  766. * Put a event into inactive state and update time fields.
  767. * Enabling the leader of a group effectively enables all
  768. * the group members that aren't explicitly disabled, so we
  769. * have to update their ->tstamp_enabled also.
  770. * Note: this works for group members as well as group leaders
  771. * since the non-leader members' sibling_lists will be empty.
  772. */
  773. static void __perf_event_mark_enabled(struct perf_event *event,
  774. struct perf_event_context *ctx)
  775. {
  776. struct perf_event *sub;
  777. event->state = PERF_EVENT_STATE_INACTIVE;
  778. event->tstamp_enabled = ctx->time - event->total_time_enabled;
  779. list_for_each_entry(sub, &event->sibling_list, group_entry)
  780. if (sub->state >= PERF_EVENT_STATE_INACTIVE)
  781. sub->tstamp_enabled =
  782. ctx->time - sub->total_time_enabled;
  783. }
  784. /*
  785. * Cross CPU call to enable a performance event
  786. */
  787. static void __perf_event_enable(void *info)
  788. {
  789. struct perf_event *event = info;
  790. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  791. struct perf_event_context *ctx = event->ctx;
  792. struct perf_event *leader = event->group_leader;
  793. int err;
  794. /*
  795. * If this is a per-task event, need to check whether this
  796. * event's task is the current task on this cpu.
  797. */
  798. if (ctx->task && cpuctx->task_ctx != ctx) {
  799. if (cpuctx->task_ctx || ctx->task != current)
  800. return;
  801. cpuctx->task_ctx = ctx;
  802. }
  803. raw_spin_lock(&ctx->lock);
  804. ctx->is_active = 1;
  805. update_context_time(ctx);
  806. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  807. goto unlock;
  808. __perf_event_mark_enabled(event, ctx);
  809. if (event->cpu != -1 && event->cpu != smp_processor_id())
  810. goto unlock;
  811. /*
  812. * If the event is in a group and isn't the group leader,
  813. * then don't put it on unless the group is on.
  814. */
  815. if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
  816. goto unlock;
  817. if (!group_can_go_on(event, cpuctx, 1)) {
  818. err = -EEXIST;
  819. } else {
  820. perf_disable();
  821. if (event == leader)
  822. err = group_sched_in(event, cpuctx, ctx);
  823. else
  824. err = event_sched_in(event, cpuctx, ctx);
  825. perf_enable();
  826. }
  827. if (err) {
  828. /*
  829. * If this event can't go on and it's part of a
  830. * group, then the whole group has to come off.
  831. */
  832. if (leader != event)
  833. group_sched_out(leader, cpuctx, ctx);
  834. if (leader->attr.pinned) {
  835. update_group_times(leader);
  836. leader->state = PERF_EVENT_STATE_ERROR;
  837. }
  838. }
  839. unlock:
  840. raw_spin_unlock(&ctx->lock);
  841. }
  842. /*
  843. * Enable a event.
  844. *
  845. * If event->ctx is a cloned context, callers must make sure that
  846. * every task struct that event->ctx->task could possibly point to
  847. * remains valid. This condition is satisfied when called through
  848. * perf_event_for_each_child or perf_event_for_each as described
  849. * for perf_event_disable.
  850. */
  851. void perf_event_enable(struct perf_event *event)
  852. {
  853. struct perf_event_context *ctx = event->ctx;
  854. struct task_struct *task = ctx->task;
  855. if (!task) {
  856. /*
  857. * Enable the event on the cpu that it's on
  858. */
  859. smp_call_function_single(event->cpu, __perf_event_enable,
  860. event, 1);
  861. return;
  862. }
  863. raw_spin_lock_irq(&ctx->lock);
  864. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  865. goto out;
  866. /*
  867. * If the event is in error state, clear that first.
  868. * That way, if we see the event in error state below, we
  869. * know that it has gone back into error state, as distinct
  870. * from the task having been scheduled away before the
  871. * cross-call arrived.
  872. */
  873. if (event->state == PERF_EVENT_STATE_ERROR)
  874. event->state = PERF_EVENT_STATE_OFF;
  875. retry:
  876. raw_spin_unlock_irq(&ctx->lock);
  877. task_oncpu_function_call(task, __perf_event_enable, event);
  878. raw_spin_lock_irq(&ctx->lock);
  879. /*
  880. * If the context is active and the event is still off,
  881. * we need to retry the cross-call.
  882. */
  883. if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
  884. goto retry;
  885. /*
  886. * Since we have the lock this context can't be scheduled
  887. * in, so we can change the state safely.
  888. */
  889. if (event->state == PERF_EVENT_STATE_OFF)
  890. __perf_event_mark_enabled(event, ctx);
  891. out:
  892. raw_spin_unlock_irq(&ctx->lock);
  893. }
  894. static int perf_event_refresh(struct perf_event *event, int refresh)
  895. {
  896. /*
  897. * not supported on inherited events
  898. */
  899. if (event->attr.inherit)
  900. return -EINVAL;
  901. atomic_add(refresh, &event->event_limit);
  902. perf_event_enable(event);
  903. return 0;
  904. }
  905. enum event_type_t {
  906. EVENT_FLEXIBLE = 0x1,
  907. EVENT_PINNED = 0x2,
  908. EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
  909. };
  910. static void ctx_sched_out(struct perf_event_context *ctx,
  911. struct perf_cpu_context *cpuctx,
  912. enum event_type_t event_type)
  913. {
  914. struct perf_event *event;
  915. raw_spin_lock(&ctx->lock);
  916. ctx->is_active = 0;
  917. if (likely(!ctx->nr_events))
  918. goto out;
  919. update_context_time(ctx);
  920. perf_disable();
  921. if (!ctx->nr_active)
  922. goto out_enable;
  923. if (event_type & EVENT_PINNED)
  924. list_for_each_entry(event, &ctx->pinned_groups, group_entry)
  925. group_sched_out(event, cpuctx, ctx);
  926. if (event_type & EVENT_FLEXIBLE)
  927. list_for_each_entry(event, &ctx->flexible_groups, group_entry)
  928. group_sched_out(event, cpuctx, ctx);
  929. out_enable:
  930. perf_enable();
  931. out:
  932. raw_spin_unlock(&ctx->lock);
  933. }
  934. /*
  935. * Test whether two contexts are equivalent, i.e. whether they
  936. * have both been cloned from the same version of the same context
  937. * and they both have the same number of enabled events.
  938. * If the number of enabled events is the same, then the set
  939. * of enabled events should be the same, because these are both
  940. * inherited contexts, therefore we can't access individual events
  941. * in them directly with an fd; we can only enable/disable all
  942. * events via prctl, or enable/disable all events in a family
  943. * via ioctl, which will have the same effect on both contexts.
  944. */
  945. static int context_equiv(struct perf_event_context *ctx1,
  946. struct perf_event_context *ctx2)
  947. {
  948. return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
  949. && ctx1->parent_gen == ctx2->parent_gen
  950. && !ctx1->pin_count && !ctx2->pin_count;
  951. }
  952. static void __perf_event_sync_stat(struct perf_event *event,
  953. struct perf_event *next_event)
  954. {
  955. u64 value;
  956. if (!event->attr.inherit_stat)
  957. return;
  958. /*
  959. * Update the event value, we cannot use perf_event_read()
  960. * because we're in the middle of a context switch and have IRQs
  961. * disabled, which upsets smp_call_function_single(), however
  962. * we know the event must be on the current CPU, therefore we
  963. * don't need to use it.
  964. */
  965. switch (event->state) {
  966. case PERF_EVENT_STATE_ACTIVE:
  967. event->pmu->read(event);
  968. /* fall-through */
  969. case PERF_EVENT_STATE_INACTIVE:
  970. update_event_times(event);
  971. break;
  972. default:
  973. break;
  974. }
  975. /*
  976. * In order to keep per-task stats reliable we need to flip the event
  977. * values when we flip the contexts.
  978. */
  979. value = local64_read(&next_event->count);
  980. value = local64_xchg(&event->count, value);
  981. local64_set(&next_event->count, value);
  982. swap(event->total_time_enabled, next_event->total_time_enabled);
  983. swap(event->total_time_running, next_event->total_time_running);
  984. /*
  985. * Since we swizzled the values, update the user visible data too.
  986. */
  987. perf_event_update_userpage(event);
  988. perf_event_update_userpage(next_event);
  989. }
  990. #define list_next_entry(pos, member) \
  991. list_entry(pos->member.next, typeof(*pos), member)
  992. static void perf_event_sync_stat(struct perf_event_context *ctx,
  993. struct perf_event_context *next_ctx)
  994. {
  995. struct perf_event *event, *next_event;
  996. if (!ctx->nr_stat)
  997. return;
  998. update_context_time(ctx);
  999. event = list_first_entry(&ctx->event_list,
  1000. struct perf_event, event_entry);
  1001. next_event = list_first_entry(&next_ctx->event_list,
  1002. struct perf_event, event_entry);
  1003. while (&event->event_entry != &ctx->event_list &&
  1004. &next_event->event_entry != &next_ctx->event_list) {
  1005. __perf_event_sync_stat(event, next_event);
  1006. event = list_next_entry(event, event_entry);
  1007. next_event = list_next_entry(next_event, event_entry);
  1008. }
  1009. }
  1010. /*
  1011. * Called from scheduler to remove the events of the current task,
  1012. * with interrupts disabled.
  1013. *
  1014. * We stop each event and update the event value in event->count.
  1015. *
  1016. * This does not protect us against NMI, but disable()
  1017. * sets the disabled bit in the control field of event _before_
  1018. * accessing the event control register. If a NMI hits, then it will
  1019. * not restart the event.
  1020. */
  1021. void perf_event_task_sched_out(struct task_struct *task,
  1022. struct task_struct *next)
  1023. {
  1024. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1025. struct perf_event_context *ctx = task->perf_event_ctxp;
  1026. struct perf_event_context *next_ctx;
  1027. struct perf_event_context *parent;
  1028. int do_switch = 1;
  1029. perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
  1030. if (likely(!ctx || !cpuctx->task_ctx))
  1031. return;
  1032. rcu_read_lock();
  1033. parent = rcu_dereference(ctx->parent_ctx);
  1034. next_ctx = next->perf_event_ctxp;
  1035. if (parent && next_ctx &&
  1036. rcu_dereference(next_ctx->parent_ctx) == parent) {
  1037. /*
  1038. * Looks like the two contexts are clones, so we might be
  1039. * able to optimize the context switch. We lock both
  1040. * contexts and check that they are clones under the
  1041. * lock (including re-checking that neither has been
  1042. * uncloned in the meantime). It doesn't matter which
  1043. * order we take the locks because no other cpu could
  1044. * be trying to lock both of these tasks.
  1045. */
  1046. raw_spin_lock(&ctx->lock);
  1047. raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
  1048. if (context_equiv(ctx, next_ctx)) {
  1049. /*
  1050. * XXX do we need a memory barrier of sorts
  1051. * wrt to rcu_dereference() of perf_event_ctxp
  1052. */
  1053. task->perf_event_ctxp = next_ctx;
  1054. next->perf_event_ctxp = ctx;
  1055. ctx->task = next;
  1056. next_ctx->task = task;
  1057. do_switch = 0;
  1058. perf_event_sync_stat(ctx, next_ctx);
  1059. }
  1060. raw_spin_unlock(&next_ctx->lock);
  1061. raw_spin_unlock(&ctx->lock);
  1062. }
  1063. rcu_read_unlock();
  1064. if (do_switch) {
  1065. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  1066. cpuctx->task_ctx = NULL;
  1067. }
  1068. }
  1069. static void task_ctx_sched_out(struct perf_event_context *ctx,
  1070. enum event_type_t event_type)
  1071. {
  1072. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1073. if (!cpuctx->task_ctx)
  1074. return;
  1075. if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  1076. return;
  1077. ctx_sched_out(ctx, cpuctx, event_type);
  1078. cpuctx->task_ctx = NULL;
  1079. }
  1080. /*
  1081. * Called with IRQs disabled
  1082. */
  1083. static void __perf_event_task_sched_out(struct perf_event_context *ctx)
  1084. {
  1085. task_ctx_sched_out(ctx, EVENT_ALL);
  1086. }
  1087. /*
  1088. * Called with IRQs disabled
  1089. */
  1090. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  1091. enum event_type_t event_type)
  1092. {
  1093. ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
  1094. }
  1095. static void
  1096. ctx_pinned_sched_in(struct perf_event_context *ctx,
  1097. struct perf_cpu_context *cpuctx)
  1098. {
  1099. struct perf_event *event;
  1100. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  1101. if (event->state <= PERF_EVENT_STATE_OFF)
  1102. continue;
  1103. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1104. continue;
  1105. if (group_can_go_on(event, cpuctx, 1))
  1106. group_sched_in(event, cpuctx, ctx);
  1107. /*
  1108. * If this pinned group hasn't been scheduled,
  1109. * put it in error state.
  1110. */
  1111. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1112. update_group_times(event);
  1113. event->state = PERF_EVENT_STATE_ERROR;
  1114. }
  1115. }
  1116. }
  1117. static void
  1118. ctx_flexible_sched_in(struct perf_event_context *ctx,
  1119. struct perf_cpu_context *cpuctx)
  1120. {
  1121. struct perf_event *event;
  1122. int can_add_hw = 1;
  1123. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  1124. /* Ignore events in OFF or ERROR state */
  1125. if (event->state <= PERF_EVENT_STATE_OFF)
  1126. continue;
  1127. /*
  1128. * Listen to the 'cpu' scheduling filter constraint
  1129. * of events:
  1130. */
  1131. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1132. continue;
  1133. if (group_can_go_on(event, cpuctx, can_add_hw))
  1134. if (group_sched_in(event, cpuctx, ctx))
  1135. can_add_hw = 0;
  1136. }
  1137. }
  1138. static void
  1139. ctx_sched_in(struct perf_event_context *ctx,
  1140. struct perf_cpu_context *cpuctx,
  1141. enum event_type_t event_type)
  1142. {
  1143. raw_spin_lock(&ctx->lock);
  1144. ctx->is_active = 1;
  1145. if (likely(!ctx->nr_events))
  1146. goto out;
  1147. ctx->timestamp = perf_clock();
  1148. perf_disable();
  1149. /*
  1150. * First go through the list and put on any pinned groups
  1151. * in order to give them the best chance of going on.
  1152. */
  1153. if (event_type & EVENT_PINNED)
  1154. ctx_pinned_sched_in(ctx, cpuctx);
  1155. /* Then walk through the lower prio flexible groups */
  1156. if (event_type & EVENT_FLEXIBLE)
  1157. ctx_flexible_sched_in(ctx, cpuctx);
  1158. perf_enable();
  1159. out:
  1160. raw_spin_unlock(&ctx->lock);
  1161. }
  1162. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  1163. enum event_type_t event_type)
  1164. {
  1165. struct perf_event_context *ctx = &cpuctx->ctx;
  1166. ctx_sched_in(ctx, cpuctx, event_type);
  1167. }
  1168. static void task_ctx_sched_in(struct task_struct *task,
  1169. enum event_type_t event_type)
  1170. {
  1171. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1172. struct perf_event_context *ctx = task->perf_event_ctxp;
  1173. if (likely(!ctx))
  1174. return;
  1175. if (cpuctx->task_ctx == ctx)
  1176. return;
  1177. ctx_sched_in(ctx, cpuctx, event_type);
  1178. cpuctx->task_ctx = ctx;
  1179. }
  1180. /*
  1181. * Called from scheduler to add the events of the current task
  1182. * with interrupts disabled.
  1183. *
  1184. * We restore the event value and then enable it.
  1185. *
  1186. * This does not protect us against NMI, but enable()
  1187. * sets the enabled bit in the control field of event _before_
  1188. * accessing the event control register. If a NMI hits, then it will
  1189. * keep the event running.
  1190. */
  1191. void perf_event_task_sched_in(struct task_struct *task)
  1192. {
  1193. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1194. struct perf_event_context *ctx = task->perf_event_ctxp;
  1195. if (likely(!ctx))
  1196. return;
  1197. if (cpuctx->task_ctx == ctx)
  1198. return;
  1199. perf_disable();
  1200. /*
  1201. * We want to keep the following priority order:
  1202. * cpu pinned (that don't need to move), task pinned,
  1203. * cpu flexible, task flexible.
  1204. */
  1205. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1206. ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
  1207. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  1208. ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
  1209. cpuctx->task_ctx = ctx;
  1210. perf_enable();
  1211. }
  1212. #define MAX_INTERRUPTS (~0ULL)
  1213. static void perf_log_throttle(struct perf_event *event, int enable);
  1214. static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
  1215. {
  1216. u64 frequency = event->attr.sample_freq;
  1217. u64 sec = NSEC_PER_SEC;
  1218. u64 divisor, dividend;
  1219. int count_fls, nsec_fls, frequency_fls, sec_fls;
  1220. count_fls = fls64(count);
  1221. nsec_fls = fls64(nsec);
  1222. frequency_fls = fls64(frequency);
  1223. sec_fls = 30;
  1224. /*
  1225. * We got @count in @nsec, with a target of sample_freq HZ
  1226. * the target period becomes:
  1227. *
  1228. * @count * 10^9
  1229. * period = -------------------
  1230. * @nsec * sample_freq
  1231. *
  1232. */
  1233. /*
  1234. * Reduce accuracy by one bit such that @a and @b converge
  1235. * to a similar magnitude.
  1236. */
  1237. #define REDUCE_FLS(a, b) \
  1238. do { \
  1239. if (a##_fls > b##_fls) { \
  1240. a >>= 1; \
  1241. a##_fls--; \
  1242. } else { \
  1243. b >>= 1; \
  1244. b##_fls--; \
  1245. } \
  1246. } while (0)
  1247. /*
  1248. * Reduce accuracy until either term fits in a u64, then proceed with
  1249. * the other, so that finally we can do a u64/u64 division.
  1250. */
  1251. while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
  1252. REDUCE_FLS(nsec, frequency);
  1253. REDUCE_FLS(sec, count);
  1254. }
  1255. if (count_fls + sec_fls > 64) {
  1256. divisor = nsec * frequency;
  1257. while (count_fls + sec_fls > 64) {
  1258. REDUCE_FLS(count, sec);
  1259. divisor >>= 1;
  1260. }
  1261. dividend = count * sec;
  1262. } else {
  1263. dividend = count * sec;
  1264. while (nsec_fls + frequency_fls > 64) {
  1265. REDUCE_FLS(nsec, frequency);
  1266. dividend >>= 1;
  1267. }
  1268. divisor = nsec * frequency;
  1269. }
  1270. if (!divisor)
  1271. return dividend;
  1272. return div64_u64(dividend, divisor);
  1273. }
  1274. static void perf_event_stop(struct perf_event *event)
  1275. {
  1276. if (!event->pmu->stop)
  1277. return event->pmu->disable(event);
  1278. return event->pmu->stop(event);
  1279. }
  1280. static int perf_event_start(struct perf_event *event)
  1281. {
  1282. if (!event->pmu->start)
  1283. return event->pmu->enable(event);
  1284. return event->pmu->start(event);
  1285. }
  1286. static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
  1287. {
  1288. struct hw_perf_event *hwc = &event->hw;
  1289. s64 period, sample_period;
  1290. s64 delta;
  1291. period = perf_calculate_period(event, nsec, count);
  1292. delta = (s64)(period - hwc->sample_period);
  1293. delta = (delta + 7) / 8; /* low pass filter */
  1294. sample_period = hwc->sample_period + delta;
  1295. if (!sample_period)
  1296. sample_period = 1;
  1297. hwc->sample_period = sample_period;
  1298. if (local64_read(&hwc->period_left) > 8*sample_period) {
  1299. perf_disable();
  1300. perf_event_stop(event);
  1301. local64_set(&hwc->period_left, 0);
  1302. perf_event_start(event);
  1303. perf_enable();
  1304. }
  1305. }
  1306. static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
  1307. {
  1308. struct perf_event *event;
  1309. struct hw_perf_event *hwc;
  1310. u64 interrupts, now;
  1311. s64 delta;
  1312. raw_spin_lock(&ctx->lock);
  1313. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  1314. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1315. continue;
  1316. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1317. continue;
  1318. hwc = &event->hw;
  1319. interrupts = hwc->interrupts;
  1320. hwc->interrupts = 0;
  1321. /*
  1322. * unthrottle events on the tick
  1323. */
  1324. if (interrupts == MAX_INTERRUPTS) {
  1325. perf_log_throttle(event, 1);
  1326. perf_disable();
  1327. event->pmu->unthrottle(event);
  1328. perf_enable();
  1329. }
  1330. if (!event->attr.freq || !event->attr.sample_freq)
  1331. continue;
  1332. perf_disable();
  1333. event->pmu->read(event);
  1334. now = local64_read(&event->count);
  1335. delta = now - hwc->freq_count_stamp;
  1336. hwc->freq_count_stamp = now;
  1337. if (delta > 0)
  1338. perf_adjust_period(event, TICK_NSEC, delta);
  1339. perf_enable();
  1340. }
  1341. raw_spin_unlock(&ctx->lock);
  1342. }
  1343. /*
  1344. * Round-robin a context's events:
  1345. */
  1346. static void rotate_ctx(struct perf_event_context *ctx)
  1347. {
  1348. raw_spin_lock(&ctx->lock);
  1349. /* Rotate the first entry last of non-pinned groups */
  1350. list_rotate_left(&ctx->flexible_groups);
  1351. raw_spin_unlock(&ctx->lock);
  1352. }
  1353. void perf_event_task_tick(struct task_struct *curr)
  1354. {
  1355. struct perf_cpu_context *cpuctx;
  1356. struct perf_event_context *ctx;
  1357. int rotate = 0;
  1358. if (!atomic_read(&nr_events))
  1359. return;
  1360. cpuctx = &__get_cpu_var(perf_cpu_context);
  1361. if (cpuctx->ctx.nr_events &&
  1362. cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
  1363. rotate = 1;
  1364. ctx = curr->perf_event_ctxp;
  1365. if (ctx && ctx->nr_events && ctx->nr_events != ctx->nr_active)
  1366. rotate = 1;
  1367. perf_ctx_adjust_freq(&cpuctx->ctx);
  1368. if (ctx)
  1369. perf_ctx_adjust_freq(ctx);
  1370. if (!rotate)
  1371. return;
  1372. perf_disable();
  1373. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1374. if (ctx)
  1375. task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
  1376. rotate_ctx(&cpuctx->ctx);
  1377. if (ctx)
  1378. rotate_ctx(ctx);
  1379. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  1380. if (ctx)
  1381. task_ctx_sched_in(curr, EVENT_FLEXIBLE);
  1382. perf_enable();
  1383. }
  1384. static int event_enable_on_exec(struct perf_event *event,
  1385. struct perf_event_context *ctx)
  1386. {
  1387. if (!event->attr.enable_on_exec)
  1388. return 0;
  1389. event->attr.enable_on_exec = 0;
  1390. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1391. return 0;
  1392. __perf_event_mark_enabled(event, ctx);
  1393. return 1;
  1394. }
  1395. /*
  1396. * Enable all of a task's events that have been marked enable-on-exec.
  1397. * This expects task == current.
  1398. */
  1399. static void perf_event_enable_on_exec(struct task_struct *task)
  1400. {
  1401. struct perf_event_context *ctx;
  1402. struct perf_event *event;
  1403. unsigned long flags;
  1404. int enabled = 0;
  1405. int ret;
  1406. local_irq_save(flags);
  1407. ctx = task->perf_event_ctxp;
  1408. if (!ctx || !ctx->nr_events)
  1409. goto out;
  1410. __perf_event_task_sched_out(ctx);
  1411. raw_spin_lock(&ctx->lock);
  1412. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  1413. ret = event_enable_on_exec(event, ctx);
  1414. if (ret)
  1415. enabled = 1;
  1416. }
  1417. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  1418. ret = event_enable_on_exec(event, ctx);
  1419. if (ret)
  1420. enabled = 1;
  1421. }
  1422. /*
  1423. * Unclone this context if we enabled any event.
  1424. */
  1425. if (enabled)
  1426. unclone_ctx(ctx);
  1427. raw_spin_unlock(&ctx->lock);
  1428. perf_event_task_sched_in(task);
  1429. out:
  1430. local_irq_restore(flags);
  1431. }
  1432. /*
  1433. * Cross CPU call to read the hardware event
  1434. */
  1435. static void __perf_event_read(void *info)
  1436. {
  1437. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1438. struct perf_event *event = info;
  1439. struct perf_event_context *ctx = event->ctx;
  1440. /*
  1441. * If this is a task context, we need to check whether it is
  1442. * the current task context of this cpu. If not it has been
  1443. * scheduled out before the smp call arrived. In that case
  1444. * event->count would have been updated to a recent sample
  1445. * when the event was scheduled out.
  1446. */
  1447. if (ctx->task && cpuctx->task_ctx != ctx)
  1448. return;
  1449. raw_spin_lock(&ctx->lock);
  1450. update_context_time(ctx);
  1451. update_event_times(event);
  1452. raw_spin_unlock(&ctx->lock);
  1453. event->pmu->read(event);
  1454. }
  1455. static inline u64 perf_event_count(struct perf_event *event)
  1456. {
  1457. return local64_read(&event->count) + atomic64_read(&event->child_count);
  1458. }
  1459. static u64 perf_event_read(struct perf_event *event)
  1460. {
  1461. /*
  1462. * If event is enabled and currently active on a CPU, update the
  1463. * value in the event structure:
  1464. */
  1465. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  1466. smp_call_function_single(event->oncpu,
  1467. __perf_event_read, event, 1);
  1468. } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1469. struct perf_event_context *ctx = event->ctx;
  1470. unsigned long flags;
  1471. raw_spin_lock_irqsave(&ctx->lock, flags);
  1472. update_context_time(ctx);
  1473. update_event_times(event);
  1474. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1475. }
  1476. return perf_event_count(event);
  1477. }
  1478. /*
  1479. * Initialize the perf_event context in a task_struct:
  1480. */
  1481. static void
  1482. __perf_event_init_context(struct perf_event_context *ctx,
  1483. struct task_struct *task)
  1484. {
  1485. raw_spin_lock_init(&ctx->lock);
  1486. mutex_init(&ctx->mutex);
  1487. INIT_LIST_HEAD(&ctx->pinned_groups);
  1488. INIT_LIST_HEAD(&ctx->flexible_groups);
  1489. INIT_LIST_HEAD(&ctx->event_list);
  1490. atomic_set(&ctx->refcount, 1);
  1491. ctx->task = task;
  1492. }
  1493. static struct perf_event_context *find_get_context(pid_t pid, int cpu)
  1494. {
  1495. struct perf_event_context *ctx;
  1496. struct perf_cpu_context *cpuctx;
  1497. struct task_struct *task;
  1498. unsigned long flags;
  1499. int err;
  1500. if (pid == -1 && cpu != -1) {
  1501. /* Must be root to operate on a CPU event: */
  1502. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  1503. return ERR_PTR(-EACCES);
  1504. if (cpu < 0 || cpu >= nr_cpumask_bits)
  1505. return ERR_PTR(-EINVAL);
  1506. /*
  1507. * We could be clever and allow to attach a event to an
  1508. * offline CPU and activate it when the CPU comes up, but
  1509. * that's for later.
  1510. */
  1511. if (!cpu_online(cpu))
  1512. return ERR_PTR(-ENODEV);
  1513. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1514. ctx = &cpuctx->ctx;
  1515. get_ctx(ctx);
  1516. return ctx;
  1517. }
  1518. rcu_read_lock();
  1519. if (!pid)
  1520. task = current;
  1521. else
  1522. task = find_task_by_vpid(pid);
  1523. if (task)
  1524. get_task_struct(task);
  1525. rcu_read_unlock();
  1526. if (!task)
  1527. return ERR_PTR(-ESRCH);
  1528. /*
  1529. * Can't attach events to a dying task.
  1530. */
  1531. err = -ESRCH;
  1532. if (task->flags & PF_EXITING)
  1533. goto errout;
  1534. /* Reuse ptrace permission checks for now. */
  1535. err = -EACCES;
  1536. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  1537. goto errout;
  1538. retry:
  1539. ctx = perf_lock_task_context(task, &flags);
  1540. if (ctx) {
  1541. unclone_ctx(ctx);
  1542. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1543. }
  1544. if (!ctx) {
  1545. ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  1546. err = -ENOMEM;
  1547. if (!ctx)
  1548. goto errout;
  1549. __perf_event_init_context(ctx, task);
  1550. get_ctx(ctx);
  1551. if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
  1552. /*
  1553. * We raced with some other task; use
  1554. * the context they set.
  1555. */
  1556. kfree(ctx);
  1557. goto retry;
  1558. }
  1559. get_task_struct(task);
  1560. }
  1561. put_task_struct(task);
  1562. return ctx;
  1563. errout:
  1564. put_task_struct(task);
  1565. return ERR_PTR(err);
  1566. }
  1567. static void perf_event_free_filter(struct perf_event *event);
  1568. static void free_event_rcu(struct rcu_head *head)
  1569. {
  1570. struct perf_event *event;
  1571. event = container_of(head, struct perf_event, rcu_head);
  1572. if (event->ns)
  1573. put_pid_ns(event->ns);
  1574. perf_event_free_filter(event);
  1575. kfree(event);
  1576. }
  1577. static void perf_pending_sync(struct perf_event *event);
  1578. static void perf_buffer_put(struct perf_buffer *buffer);
  1579. static void free_event(struct perf_event *event)
  1580. {
  1581. perf_pending_sync(event);
  1582. if (!event->parent) {
  1583. atomic_dec(&nr_events);
  1584. if (event->attr.mmap || event->attr.mmap_data)
  1585. atomic_dec(&nr_mmap_events);
  1586. if (event->attr.comm)
  1587. atomic_dec(&nr_comm_events);
  1588. if (event->attr.task)
  1589. atomic_dec(&nr_task_events);
  1590. }
  1591. if (event->buffer) {
  1592. perf_buffer_put(event->buffer);
  1593. event->buffer = NULL;
  1594. }
  1595. if (event->destroy)
  1596. event->destroy(event);
  1597. put_ctx(event->ctx);
  1598. call_rcu(&event->rcu_head, free_event_rcu);
  1599. }
  1600. int perf_event_release_kernel(struct perf_event *event)
  1601. {
  1602. struct perf_event_context *ctx = event->ctx;
  1603. /*
  1604. * Remove from the PMU, can't get re-enabled since we got
  1605. * here because the last ref went.
  1606. */
  1607. perf_event_disable(event);
  1608. WARN_ON_ONCE(ctx->parent_ctx);
  1609. /*
  1610. * There are two ways this annotation is useful:
  1611. *
  1612. * 1) there is a lock recursion from perf_event_exit_task
  1613. * see the comment there.
  1614. *
  1615. * 2) there is a lock-inversion with mmap_sem through
  1616. * perf_event_read_group(), which takes faults while
  1617. * holding ctx->mutex, however this is called after
  1618. * the last filedesc died, so there is no possibility
  1619. * to trigger the AB-BA case.
  1620. */
  1621. mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
  1622. raw_spin_lock_irq(&ctx->lock);
  1623. perf_group_detach(event);
  1624. list_del_event(event, ctx);
  1625. raw_spin_unlock_irq(&ctx->lock);
  1626. mutex_unlock(&ctx->mutex);
  1627. mutex_lock(&event->owner->perf_event_mutex);
  1628. list_del_init(&event->owner_entry);
  1629. mutex_unlock(&event->owner->perf_event_mutex);
  1630. put_task_struct(event->owner);
  1631. free_event(event);
  1632. return 0;
  1633. }
  1634. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  1635. /*
  1636. * Called when the last reference to the file is gone.
  1637. */
  1638. static int perf_release(struct inode *inode, struct file *file)
  1639. {
  1640. struct perf_event *event = file->private_data;
  1641. file->private_data = NULL;
  1642. return perf_event_release_kernel(event);
  1643. }
  1644. static int perf_event_read_size(struct perf_event *event)
  1645. {
  1646. int entry = sizeof(u64); /* value */
  1647. int size = 0;
  1648. int nr = 1;
  1649. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1650. size += sizeof(u64);
  1651. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1652. size += sizeof(u64);
  1653. if (event->attr.read_format & PERF_FORMAT_ID)
  1654. entry += sizeof(u64);
  1655. if (event->attr.read_format & PERF_FORMAT_GROUP) {
  1656. nr += event->group_leader->nr_siblings;
  1657. size += sizeof(u64);
  1658. }
  1659. size += entry * nr;
  1660. return size;
  1661. }
  1662. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  1663. {
  1664. struct perf_event *child;
  1665. u64 total = 0;
  1666. *enabled = 0;
  1667. *running = 0;
  1668. mutex_lock(&event->child_mutex);
  1669. total += perf_event_read(event);
  1670. *enabled += event->total_time_enabled +
  1671. atomic64_read(&event->child_total_time_enabled);
  1672. *running += event->total_time_running +
  1673. atomic64_read(&event->child_total_time_running);
  1674. list_for_each_entry(child, &event->child_list, child_list) {
  1675. total += perf_event_read(child);
  1676. *enabled += child->total_time_enabled;
  1677. *running += child->total_time_running;
  1678. }
  1679. mutex_unlock(&event->child_mutex);
  1680. return total;
  1681. }
  1682. EXPORT_SYMBOL_GPL(perf_event_read_value);
  1683. static int perf_event_read_group(struct perf_event *event,
  1684. u64 read_format, char __user *buf)
  1685. {
  1686. struct perf_event *leader = event->group_leader, *sub;
  1687. int n = 0, size = 0, ret = -EFAULT;
  1688. struct perf_event_context *ctx = leader->ctx;
  1689. u64 values[5];
  1690. u64 count, enabled, running;
  1691. mutex_lock(&ctx->mutex);
  1692. count = perf_event_read_value(leader, &enabled, &running);
  1693. values[n++] = 1 + leader->nr_siblings;
  1694. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1695. values[n++] = enabled;
  1696. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1697. values[n++] = running;
  1698. values[n++] = count;
  1699. if (read_format & PERF_FORMAT_ID)
  1700. values[n++] = primary_event_id(leader);
  1701. size = n * sizeof(u64);
  1702. if (copy_to_user(buf, values, size))
  1703. goto unlock;
  1704. ret = size;
  1705. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  1706. n = 0;
  1707. values[n++] = perf_event_read_value(sub, &enabled, &running);
  1708. if (read_format & PERF_FORMAT_ID)
  1709. values[n++] = primary_event_id(sub);
  1710. size = n * sizeof(u64);
  1711. if (copy_to_user(buf + ret, values, size)) {
  1712. ret = -EFAULT;
  1713. goto unlock;
  1714. }
  1715. ret += size;
  1716. }
  1717. unlock:
  1718. mutex_unlock(&ctx->mutex);
  1719. return ret;
  1720. }
  1721. static int perf_event_read_one(struct perf_event *event,
  1722. u64 read_format, char __user *buf)
  1723. {
  1724. u64 enabled, running;
  1725. u64 values[4];
  1726. int n = 0;
  1727. values[n++] = perf_event_read_value(event, &enabled, &running);
  1728. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1729. values[n++] = enabled;
  1730. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1731. values[n++] = running;
  1732. if (read_format & PERF_FORMAT_ID)
  1733. values[n++] = primary_event_id(event);
  1734. if (copy_to_user(buf, values, n * sizeof(u64)))
  1735. return -EFAULT;
  1736. return n * sizeof(u64);
  1737. }
  1738. /*
  1739. * Read the performance event - simple non blocking version for now
  1740. */
  1741. static ssize_t
  1742. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  1743. {
  1744. u64 read_format = event->attr.read_format;
  1745. int ret;
  1746. /*
  1747. * Return end-of-file for a read on a event that is in
  1748. * error state (i.e. because it was pinned but it couldn't be
  1749. * scheduled on to the CPU at some point).
  1750. */
  1751. if (event->state == PERF_EVENT_STATE_ERROR)
  1752. return 0;
  1753. if (count < perf_event_read_size(event))
  1754. return -ENOSPC;
  1755. WARN_ON_ONCE(event->ctx->parent_ctx);
  1756. if (read_format & PERF_FORMAT_GROUP)
  1757. ret = perf_event_read_group(event, read_format, buf);
  1758. else
  1759. ret = perf_event_read_one(event, read_format, buf);
  1760. return ret;
  1761. }
  1762. static ssize_t
  1763. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1764. {
  1765. struct perf_event *event = file->private_data;
  1766. return perf_read_hw(event, buf, count);
  1767. }
  1768. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1769. {
  1770. struct perf_event *event = file->private_data;
  1771. struct perf_buffer *buffer;
  1772. unsigned int events = POLL_HUP;
  1773. rcu_read_lock();
  1774. buffer = rcu_dereference(event->buffer);
  1775. if (buffer)
  1776. events = atomic_xchg(&buffer->poll, 0);
  1777. rcu_read_unlock();
  1778. poll_wait(file, &event->waitq, wait);
  1779. return events;
  1780. }
  1781. static void perf_event_reset(struct perf_event *event)
  1782. {
  1783. (void)perf_event_read(event);
  1784. local64_set(&event->count, 0);
  1785. perf_event_update_userpage(event);
  1786. }
  1787. /*
  1788. * Holding the top-level event's child_mutex means that any
  1789. * descendant process that has inherited this event will block
  1790. * in sync_child_event if it goes to exit, thus satisfying the
  1791. * task existence requirements of perf_event_enable/disable.
  1792. */
  1793. static void perf_event_for_each_child(struct perf_event *event,
  1794. void (*func)(struct perf_event *))
  1795. {
  1796. struct perf_event *child;
  1797. WARN_ON_ONCE(event->ctx->parent_ctx);
  1798. mutex_lock(&event->child_mutex);
  1799. func(event);
  1800. list_for_each_entry(child, &event->child_list, child_list)
  1801. func(child);
  1802. mutex_unlock(&event->child_mutex);
  1803. }
  1804. static void perf_event_for_each(struct perf_event *event,
  1805. void (*func)(struct perf_event *))
  1806. {
  1807. struct perf_event_context *ctx = event->ctx;
  1808. struct perf_event *sibling;
  1809. WARN_ON_ONCE(ctx->parent_ctx);
  1810. mutex_lock(&ctx->mutex);
  1811. event = event->group_leader;
  1812. perf_event_for_each_child(event, func);
  1813. func(event);
  1814. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  1815. perf_event_for_each_child(event, func);
  1816. mutex_unlock(&ctx->mutex);
  1817. }
  1818. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  1819. {
  1820. struct perf_event_context *ctx = event->ctx;
  1821. unsigned long size;
  1822. int ret = 0;
  1823. u64 value;
  1824. if (!event->attr.sample_period)
  1825. return -EINVAL;
  1826. size = copy_from_user(&value, arg, sizeof(value));
  1827. if (size != sizeof(value))
  1828. return -EFAULT;
  1829. if (!value)
  1830. return -EINVAL;
  1831. raw_spin_lock_irq(&ctx->lock);
  1832. if (event->attr.freq) {
  1833. if (value > sysctl_perf_event_sample_rate) {
  1834. ret = -EINVAL;
  1835. goto unlock;
  1836. }
  1837. event->attr.sample_freq = value;
  1838. } else {
  1839. event->attr.sample_period = value;
  1840. event->hw.sample_period = value;
  1841. }
  1842. unlock:
  1843. raw_spin_unlock_irq(&ctx->lock);
  1844. return ret;
  1845. }
  1846. static const struct file_operations perf_fops;
  1847. static struct perf_event *perf_fget_light(int fd, int *fput_needed)
  1848. {
  1849. struct file *file;
  1850. file = fget_light(fd, fput_needed);
  1851. if (!file)
  1852. return ERR_PTR(-EBADF);
  1853. if (file->f_op != &perf_fops) {
  1854. fput_light(file, *fput_needed);
  1855. *fput_needed = 0;
  1856. return ERR_PTR(-EBADF);
  1857. }
  1858. return file->private_data;
  1859. }
  1860. static int perf_event_set_output(struct perf_event *event,
  1861. struct perf_event *output_event);
  1862. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  1863. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1864. {
  1865. struct perf_event *event = file->private_data;
  1866. void (*func)(struct perf_event *);
  1867. u32 flags = arg;
  1868. switch (cmd) {
  1869. case PERF_EVENT_IOC_ENABLE:
  1870. func = perf_event_enable;
  1871. break;
  1872. case PERF_EVENT_IOC_DISABLE:
  1873. func = perf_event_disable;
  1874. break;
  1875. case PERF_EVENT_IOC_RESET:
  1876. func = perf_event_reset;
  1877. break;
  1878. case PERF_EVENT_IOC_REFRESH:
  1879. return perf_event_refresh(event, arg);
  1880. case PERF_EVENT_IOC_PERIOD:
  1881. return perf_event_period(event, (u64 __user *)arg);
  1882. case PERF_EVENT_IOC_SET_OUTPUT:
  1883. {
  1884. struct perf_event *output_event = NULL;
  1885. int fput_needed = 0;
  1886. int ret;
  1887. if (arg != -1) {
  1888. output_event = perf_fget_light(arg, &fput_needed);
  1889. if (IS_ERR(output_event))
  1890. return PTR_ERR(output_event);
  1891. }
  1892. ret = perf_event_set_output(event, output_event);
  1893. if (output_event)
  1894. fput_light(output_event->filp, fput_needed);
  1895. return ret;
  1896. }
  1897. case PERF_EVENT_IOC_SET_FILTER:
  1898. return perf_event_set_filter(event, (void __user *)arg);
  1899. default:
  1900. return -ENOTTY;
  1901. }
  1902. if (flags & PERF_IOC_FLAG_GROUP)
  1903. perf_event_for_each(event, func);
  1904. else
  1905. perf_event_for_each_child(event, func);
  1906. return 0;
  1907. }
  1908. int perf_event_task_enable(void)
  1909. {
  1910. struct perf_event *event;
  1911. mutex_lock(&current->perf_event_mutex);
  1912. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  1913. perf_event_for_each_child(event, perf_event_enable);
  1914. mutex_unlock(&current->perf_event_mutex);
  1915. return 0;
  1916. }
  1917. int perf_event_task_disable(void)
  1918. {
  1919. struct perf_event *event;
  1920. mutex_lock(&current->perf_event_mutex);
  1921. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  1922. perf_event_for_each_child(event, perf_event_disable);
  1923. mutex_unlock(&current->perf_event_mutex);
  1924. return 0;
  1925. }
  1926. #ifndef PERF_EVENT_INDEX_OFFSET
  1927. # define PERF_EVENT_INDEX_OFFSET 0
  1928. #endif
  1929. static int perf_event_index(struct perf_event *event)
  1930. {
  1931. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1932. return 0;
  1933. return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
  1934. }
  1935. /*
  1936. * Callers need to ensure there can be no nesting of this function, otherwise
  1937. * the seqlock logic goes bad. We can not serialize this because the arch
  1938. * code calls this from NMI context.
  1939. */
  1940. void perf_event_update_userpage(struct perf_event *event)
  1941. {
  1942. struct perf_event_mmap_page *userpg;
  1943. struct perf_buffer *buffer;
  1944. rcu_read_lock();
  1945. buffer = rcu_dereference(event->buffer);
  1946. if (!buffer)
  1947. goto unlock;
  1948. userpg = buffer->user_page;
  1949. /*
  1950. * Disable preemption so as to not let the corresponding user-space
  1951. * spin too long if we get preempted.
  1952. */
  1953. preempt_disable();
  1954. ++userpg->lock;
  1955. barrier();
  1956. userpg->index = perf_event_index(event);
  1957. userpg->offset = perf_event_count(event);
  1958. if (event->state == PERF_EVENT_STATE_ACTIVE)
  1959. userpg->offset -= local64_read(&event->hw.prev_count);
  1960. userpg->time_enabled = event->total_time_enabled +
  1961. atomic64_read(&event->child_total_time_enabled);
  1962. userpg->time_running = event->total_time_running +
  1963. atomic64_read(&event->child_total_time_running);
  1964. barrier();
  1965. ++userpg->lock;
  1966. preempt_enable();
  1967. unlock:
  1968. rcu_read_unlock();
  1969. }
  1970. static unsigned long perf_data_size(struct perf_buffer *buffer);
  1971. static void
  1972. perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
  1973. {
  1974. long max_size = perf_data_size(buffer);
  1975. if (watermark)
  1976. buffer->watermark = min(max_size, watermark);
  1977. if (!buffer->watermark)
  1978. buffer->watermark = max_size / 2;
  1979. if (flags & PERF_BUFFER_WRITABLE)
  1980. buffer->writable = 1;
  1981. atomic_set(&buffer->refcount, 1);
  1982. }
  1983. #ifndef CONFIG_PERF_USE_VMALLOC
  1984. /*
  1985. * Back perf_mmap() with regular GFP_KERNEL-0 pages.
  1986. */
  1987. static struct page *
  1988. perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
  1989. {
  1990. if (pgoff > buffer->nr_pages)
  1991. return NULL;
  1992. if (pgoff == 0)
  1993. return virt_to_page(buffer->user_page);
  1994. return virt_to_page(buffer->data_pages[pgoff - 1]);
  1995. }
  1996. static void *perf_mmap_alloc_page(int cpu)
  1997. {
  1998. struct page *page;
  1999. int node;
  2000. node = (cpu == -1) ? cpu : cpu_to_node(cpu);
  2001. page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  2002. if (!page)
  2003. return NULL;
  2004. return page_address(page);
  2005. }
  2006. static struct perf_buffer *
  2007. perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
  2008. {
  2009. struct perf_buffer *buffer;
  2010. unsigned long size;
  2011. int i;
  2012. size = sizeof(struct perf_buffer);
  2013. size += nr_pages * sizeof(void *);
  2014. buffer = kzalloc(size, GFP_KERNEL);
  2015. if (!buffer)
  2016. goto fail;
  2017. buffer->user_page = perf_mmap_alloc_page(cpu);
  2018. if (!buffer->user_page)
  2019. goto fail_user_page;
  2020. for (i = 0; i < nr_pages; i++) {
  2021. buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
  2022. if (!buffer->data_pages[i])
  2023. goto fail_data_pages;
  2024. }
  2025. buffer->nr_pages = nr_pages;
  2026. perf_buffer_init(buffer, watermark, flags);
  2027. return buffer;
  2028. fail_data_pages:
  2029. for (i--; i >= 0; i--)
  2030. free_page((unsigned long)buffer->data_pages[i]);
  2031. free_page((unsigned long)buffer->user_page);
  2032. fail_user_page:
  2033. kfree(buffer);
  2034. fail:
  2035. return NULL;
  2036. }
  2037. static void perf_mmap_free_page(unsigned long addr)
  2038. {
  2039. struct page *page = virt_to_page((void *)addr);
  2040. page->mapping = NULL;
  2041. __free_page(page);
  2042. }
  2043. static void perf_buffer_free(struct perf_buffer *buffer)
  2044. {
  2045. int i;
  2046. perf_mmap_free_page((unsigned long)buffer->user_page);
  2047. for (i = 0; i < buffer->nr_pages; i++)
  2048. perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
  2049. kfree(buffer);
  2050. }
  2051. static inline int page_order(struct perf_buffer *buffer)
  2052. {
  2053. return 0;
  2054. }
  2055. #else
  2056. /*
  2057. * Back perf_mmap() with vmalloc memory.
  2058. *
  2059. * Required for architectures that have d-cache aliasing issues.
  2060. */
  2061. static inline int page_order(struct perf_buffer *buffer)
  2062. {
  2063. return buffer->page_order;
  2064. }
  2065. static struct page *
  2066. perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
  2067. {
  2068. if (pgoff > (1UL << page_order(buffer)))
  2069. return NULL;
  2070. return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
  2071. }
  2072. static void perf_mmap_unmark_page(void *addr)
  2073. {
  2074. struct page *page = vmalloc_to_page(addr);
  2075. page->mapping = NULL;
  2076. }
  2077. static void perf_buffer_free_work(struct work_struct *work)
  2078. {
  2079. struct perf_buffer *buffer;
  2080. void *base;
  2081. int i, nr;
  2082. buffer = container_of(work, struct perf_buffer, work);
  2083. nr = 1 << page_order(buffer);
  2084. base = buffer->user_page;
  2085. for (i = 0; i < nr + 1; i++)
  2086. perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  2087. vfree(base);
  2088. kfree(buffer);
  2089. }
  2090. static void perf_buffer_free(struct perf_buffer *buffer)
  2091. {
  2092. schedule_work(&buffer->work);
  2093. }
  2094. static struct perf_buffer *
  2095. perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
  2096. {
  2097. struct perf_buffer *buffer;
  2098. unsigned long size;
  2099. void *all_buf;
  2100. size = sizeof(struct perf_buffer);
  2101. size += sizeof(void *);
  2102. buffer = kzalloc(size, GFP_KERNEL);
  2103. if (!buffer)
  2104. goto fail;
  2105. INIT_WORK(&buffer->work, perf_buffer_free_work);
  2106. all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  2107. if (!all_buf)
  2108. goto fail_all_buf;
  2109. buffer->user_page = all_buf;
  2110. buffer->data_pages[0] = all_buf + PAGE_SIZE;
  2111. buffer->page_order = ilog2(nr_pages);
  2112. buffer->nr_pages = 1;
  2113. perf_buffer_init(buffer, watermark, flags);
  2114. return buffer;
  2115. fail_all_buf:
  2116. kfree(buffer);
  2117. fail:
  2118. return NULL;
  2119. }
  2120. #endif
  2121. static unsigned long perf_data_size(struct perf_buffer *buffer)
  2122. {
  2123. return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
  2124. }
  2125. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  2126. {
  2127. struct perf_event *event = vma->vm_file->private_data;
  2128. struct perf_buffer *buffer;
  2129. int ret = VM_FAULT_SIGBUS;
  2130. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  2131. if (vmf->pgoff == 0)
  2132. ret = 0;
  2133. return ret;
  2134. }
  2135. rcu_read_lock();
  2136. buffer = rcu_dereference(event->buffer);
  2137. if (!buffer)
  2138. goto unlock;
  2139. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  2140. goto unlock;
  2141. vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
  2142. if (!vmf->page)
  2143. goto unlock;
  2144. get_page(vmf->page);
  2145. vmf->page->mapping = vma->vm_file->f_mapping;
  2146. vmf->page->index = vmf->pgoff;
  2147. ret = 0;
  2148. unlock:
  2149. rcu_read_unlock();
  2150. return ret;
  2151. }
  2152. static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
  2153. {
  2154. struct perf_buffer *buffer;
  2155. buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
  2156. perf_buffer_free(buffer);
  2157. }
  2158. static struct perf_buffer *perf_buffer_get(struct perf_event *event)
  2159. {
  2160. struct perf_buffer *buffer;
  2161. rcu_read_lock();
  2162. buffer = rcu_dereference(event->buffer);
  2163. if (buffer) {
  2164. if (!atomic_inc_not_zero(&buffer->refcount))
  2165. buffer = NULL;
  2166. }
  2167. rcu_read_unlock();
  2168. return buffer;
  2169. }
  2170. static void perf_buffer_put(struct perf_buffer *buffer)
  2171. {
  2172. if (!atomic_dec_and_test(&buffer->refcount))
  2173. return;
  2174. call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
  2175. }
  2176. static void perf_mmap_open(struct vm_area_struct *vma)
  2177. {
  2178. struct perf_event *event = vma->vm_file->private_data;
  2179. atomic_inc(&event->mmap_count);
  2180. }
  2181. static void perf_mmap_close(struct vm_area_struct *vma)
  2182. {
  2183. struct perf_event *event = vma->vm_file->private_data;
  2184. if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
  2185. unsigned long size = perf_data_size(event->buffer);
  2186. struct user_struct *user = event->mmap_user;
  2187. struct perf_buffer *buffer = event->buffer;
  2188. atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
  2189. vma->vm_mm->locked_vm -= event->mmap_locked;
  2190. rcu_assign_pointer(event->buffer, NULL);
  2191. mutex_unlock(&event->mmap_mutex);
  2192. perf_buffer_put(buffer);
  2193. free_uid(user);
  2194. }
  2195. }
  2196. static const struct vm_operations_struct perf_mmap_vmops = {
  2197. .open = perf_mmap_open,
  2198. .close = perf_mmap_close,
  2199. .fault = perf_mmap_fault,
  2200. .page_mkwrite = perf_mmap_fault,
  2201. };
  2202. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  2203. {
  2204. struct perf_event *event = file->private_data;
  2205. unsigned long user_locked, user_lock_limit;
  2206. struct user_struct *user = current_user();
  2207. unsigned long locked, lock_limit;
  2208. struct perf_buffer *buffer;
  2209. unsigned long vma_size;
  2210. unsigned long nr_pages;
  2211. long user_extra, extra;
  2212. int ret = 0, flags = 0;
  2213. /*
  2214. * Don't allow mmap() of inherited per-task counters. This would
  2215. * create a performance issue due to all children writing to the
  2216. * same buffer.
  2217. */
  2218. if (event->cpu == -1 && event->attr.inherit)
  2219. return -EINVAL;
  2220. if (!(vma->vm_flags & VM_SHARED))
  2221. return -EINVAL;
  2222. vma_size = vma->vm_end - vma->vm_start;
  2223. nr_pages = (vma_size / PAGE_SIZE) - 1;
  2224. /*
  2225. * If we have buffer pages ensure they're a power-of-two number, so we
  2226. * can do bitmasks instead of modulo.
  2227. */
  2228. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  2229. return -EINVAL;
  2230. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  2231. return -EINVAL;
  2232. if (vma->vm_pgoff != 0)
  2233. return -EINVAL;
  2234. WARN_ON_ONCE(event->ctx->parent_ctx);
  2235. mutex_lock(&event->mmap_mutex);
  2236. if (event->buffer) {
  2237. if (event->buffer->nr_pages == nr_pages)
  2238. atomic_inc(&event->buffer->refcount);
  2239. else
  2240. ret = -EINVAL;
  2241. goto unlock;
  2242. }
  2243. user_extra = nr_pages + 1;
  2244. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  2245. /*
  2246. * Increase the limit linearly with more CPUs:
  2247. */
  2248. user_lock_limit *= num_online_cpus();
  2249. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  2250. extra = 0;
  2251. if (user_locked > user_lock_limit)
  2252. extra = user_locked - user_lock_limit;
  2253. lock_limit = rlimit(RLIMIT_MEMLOCK);
  2254. lock_limit >>= PAGE_SHIFT;
  2255. locked = vma->vm_mm->locked_vm + extra;
  2256. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  2257. !capable(CAP_IPC_LOCK)) {
  2258. ret = -EPERM;
  2259. goto unlock;
  2260. }
  2261. WARN_ON(event->buffer);
  2262. if (vma->vm_flags & VM_WRITE)
  2263. flags |= PERF_BUFFER_WRITABLE;
  2264. buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
  2265. event->cpu, flags);
  2266. if (!buffer) {
  2267. ret = -ENOMEM;
  2268. goto unlock;
  2269. }
  2270. rcu_assign_pointer(event->buffer, buffer);
  2271. atomic_long_add(user_extra, &user->locked_vm);
  2272. event->mmap_locked = extra;
  2273. event->mmap_user = get_current_user();
  2274. vma->vm_mm->locked_vm += event->mmap_locked;
  2275. unlock:
  2276. if (!ret)
  2277. atomic_inc(&event->mmap_count);
  2278. mutex_unlock(&event->mmap_mutex);
  2279. vma->vm_flags |= VM_RESERVED;
  2280. vma->vm_ops = &perf_mmap_vmops;
  2281. return ret;
  2282. }
  2283. static int perf_fasync(int fd, struct file *filp, int on)
  2284. {
  2285. struct inode *inode = filp->f_path.dentry->d_inode;
  2286. struct perf_event *event = filp->private_data;
  2287. int retval;
  2288. mutex_lock(&inode->i_mutex);
  2289. retval = fasync_helper(fd, filp, on, &event->fasync);
  2290. mutex_unlock(&inode->i_mutex);
  2291. if (retval < 0)
  2292. return retval;
  2293. return 0;
  2294. }
  2295. static const struct file_operations perf_fops = {
  2296. .llseek = no_llseek,
  2297. .release = perf_release,
  2298. .read = perf_read,
  2299. .poll = perf_poll,
  2300. .unlocked_ioctl = perf_ioctl,
  2301. .compat_ioctl = perf_ioctl,
  2302. .mmap = perf_mmap,
  2303. .fasync = perf_fasync,
  2304. };
  2305. /*
  2306. * Perf event wakeup
  2307. *
  2308. * If there's data, ensure we set the poll() state and publish everything
  2309. * to user-space before waking everybody up.
  2310. */
  2311. void perf_event_wakeup(struct perf_event *event)
  2312. {
  2313. wake_up_all(&event->waitq);
  2314. if (event->pending_kill) {
  2315. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  2316. event->pending_kill = 0;
  2317. }
  2318. }
  2319. /*
  2320. * Pending wakeups
  2321. *
  2322. * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
  2323. *
  2324. * The NMI bit means we cannot possibly take locks. Therefore, maintain a
  2325. * single linked list and use cmpxchg() to add entries lockless.
  2326. */
  2327. static void perf_pending_event(struct perf_pending_entry *entry)
  2328. {
  2329. struct perf_event *event = container_of(entry,
  2330. struct perf_event, pending);
  2331. if (event->pending_disable) {
  2332. event->pending_disable = 0;
  2333. __perf_event_disable(event);
  2334. }
  2335. if (event->pending_wakeup) {
  2336. event->pending_wakeup = 0;
  2337. perf_event_wakeup(event);
  2338. }
  2339. }
  2340. #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
  2341. static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
  2342. PENDING_TAIL,
  2343. };
  2344. static void perf_pending_queue(struct perf_pending_entry *entry,
  2345. void (*func)(struct perf_pending_entry *))
  2346. {
  2347. struct perf_pending_entry **head;
  2348. if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
  2349. return;
  2350. entry->func = func;
  2351. head = &get_cpu_var(perf_pending_head);
  2352. do {
  2353. entry->next = *head;
  2354. } while (cmpxchg(head, entry->next, entry) != entry->next);
  2355. set_perf_event_pending();
  2356. put_cpu_var(perf_pending_head);
  2357. }
  2358. static int __perf_pending_run(void)
  2359. {
  2360. struct perf_pending_entry *list;
  2361. int nr = 0;
  2362. list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
  2363. while (list != PENDING_TAIL) {
  2364. void (*func)(struct perf_pending_entry *);
  2365. struct perf_pending_entry *entry = list;
  2366. list = list->next;
  2367. func = entry->func;
  2368. entry->next = NULL;
  2369. /*
  2370. * Ensure we observe the unqueue before we issue the wakeup,
  2371. * so that we won't be waiting forever.
  2372. * -- see perf_not_pending().
  2373. */
  2374. smp_wmb();
  2375. func(entry);
  2376. nr++;
  2377. }
  2378. return nr;
  2379. }
  2380. static inline int perf_not_pending(struct perf_event *event)
  2381. {
  2382. /*
  2383. * If we flush on whatever cpu we run, there is a chance we don't
  2384. * need to wait.
  2385. */
  2386. get_cpu();
  2387. __perf_pending_run();
  2388. put_cpu();
  2389. /*
  2390. * Ensure we see the proper queue state before going to sleep
  2391. * so that we do not miss the wakeup. -- see perf_pending_handle()
  2392. */
  2393. smp_rmb();
  2394. return event->pending.next == NULL;
  2395. }
  2396. static void perf_pending_sync(struct perf_event *event)
  2397. {
  2398. wait_event(event->waitq, perf_not_pending(event));
  2399. }
  2400. void perf_event_do_pending(void)
  2401. {
  2402. __perf_pending_run();
  2403. }
  2404. DEFINE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
  2405. /*
  2406. * Callchain support -- arch specific
  2407. */
  2408. __weak struct perf_callchain_entry *perf_callchain_buffer(void)
  2409. {
  2410. return &__get_cpu_var(perf_callchain_entry);
  2411. }
  2412. __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
  2413. struct pt_regs *regs)
  2414. {
  2415. }
  2416. __weak void perf_callchain_user(struct perf_callchain_entry *entry,
  2417. struct pt_regs *regs)
  2418. {
  2419. }
  2420. static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  2421. {
  2422. struct perf_callchain_entry *entry;
  2423. entry = perf_callchain_buffer();
  2424. if (!entry)
  2425. return NULL;
  2426. entry->nr = 0;
  2427. if (!user_mode(regs)) {
  2428. perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
  2429. perf_callchain_kernel(entry, regs);
  2430. if (current->mm)
  2431. regs = task_pt_regs(current);
  2432. else
  2433. regs = NULL;
  2434. }
  2435. if (regs) {
  2436. perf_callchain_store(entry, PERF_CONTEXT_USER);
  2437. perf_callchain_user(entry, regs);
  2438. }
  2439. return entry;
  2440. }
  2441. /*
  2442. * We assume there is only KVM supporting the callbacks.
  2443. * Later on, we might change it to a list if there is
  2444. * another virtualization implementation supporting the callbacks.
  2445. */
  2446. struct perf_guest_info_callbacks *perf_guest_cbs;
  2447. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2448. {
  2449. perf_guest_cbs = cbs;
  2450. return 0;
  2451. }
  2452. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  2453. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2454. {
  2455. perf_guest_cbs = NULL;
  2456. return 0;
  2457. }
  2458. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  2459. /*
  2460. * Output
  2461. */
  2462. static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
  2463. unsigned long offset, unsigned long head)
  2464. {
  2465. unsigned long mask;
  2466. if (!buffer->writable)
  2467. return true;
  2468. mask = perf_data_size(buffer) - 1;
  2469. offset = (offset - tail) & mask;
  2470. head = (head - tail) & mask;
  2471. if ((int)(head - offset) < 0)
  2472. return false;
  2473. return true;
  2474. }
  2475. static void perf_output_wakeup(struct perf_output_handle *handle)
  2476. {
  2477. atomic_set(&handle->buffer->poll, POLL_IN);
  2478. if (handle->nmi) {
  2479. handle->event->pending_wakeup = 1;
  2480. perf_pending_queue(&handle->event->pending,
  2481. perf_pending_event);
  2482. } else
  2483. perf_event_wakeup(handle->event);
  2484. }
  2485. /*
  2486. * We need to ensure a later event_id doesn't publish a head when a former
  2487. * event isn't done writing. However since we need to deal with NMIs we
  2488. * cannot fully serialize things.
  2489. *
  2490. * We only publish the head (and generate a wakeup) when the outer-most
  2491. * event completes.
  2492. */
  2493. static void perf_output_get_handle(struct perf_output_handle *handle)
  2494. {
  2495. struct perf_buffer *buffer = handle->buffer;
  2496. preempt_disable();
  2497. local_inc(&buffer->nest);
  2498. handle->wakeup = local_read(&buffer->wakeup);
  2499. }
  2500. static void perf_output_put_handle(struct perf_output_handle *handle)
  2501. {
  2502. struct perf_buffer *buffer = handle->buffer;
  2503. unsigned long head;
  2504. again:
  2505. head = local_read(&buffer->head);
  2506. /*
  2507. * IRQ/NMI can happen here, which means we can miss a head update.
  2508. */
  2509. if (!local_dec_and_test(&buffer->nest))
  2510. goto out;
  2511. /*
  2512. * Publish the known good head. Rely on the full barrier implied
  2513. * by atomic_dec_and_test() order the buffer->head read and this
  2514. * write.
  2515. */
  2516. buffer->user_page->data_head = head;
  2517. /*
  2518. * Now check if we missed an update, rely on the (compiler)
  2519. * barrier in atomic_dec_and_test() to re-read buffer->head.
  2520. */
  2521. if (unlikely(head != local_read(&buffer->head))) {
  2522. local_inc(&buffer->nest);
  2523. goto again;
  2524. }
  2525. if (handle->wakeup != local_read(&buffer->wakeup))
  2526. perf_output_wakeup(handle);
  2527. out:
  2528. preempt_enable();
  2529. }
  2530. __always_inline void perf_output_copy(struct perf_output_handle *handle,
  2531. const void *buf, unsigned int len)
  2532. {
  2533. do {
  2534. unsigned long size = min_t(unsigned long, handle->size, len);
  2535. memcpy(handle->addr, buf, size);
  2536. len -= size;
  2537. handle->addr += size;
  2538. buf += size;
  2539. handle->size -= size;
  2540. if (!handle->size) {
  2541. struct perf_buffer *buffer = handle->buffer;
  2542. handle->page++;
  2543. handle->page &= buffer->nr_pages - 1;
  2544. handle->addr = buffer->data_pages[handle->page];
  2545. handle->size = PAGE_SIZE << page_order(buffer);
  2546. }
  2547. } while (len);
  2548. }
  2549. int perf_output_begin(struct perf_output_handle *handle,
  2550. struct perf_event *event, unsigned int size,
  2551. int nmi, int sample)
  2552. {
  2553. struct perf_buffer *buffer;
  2554. unsigned long tail, offset, head;
  2555. int have_lost;
  2556. struct {
  2557. struct perf_event_header header;
  2558. u64 id;
  2559. u64 lost;
  2560. } lost_event;
  2561. rcu_read_lock();
  2562. /*
  2563. * For inherited events we send all the output towards the parent.
  2564. */
  2565. if (event->parent)
  2566. event = event->parent;
  2567. buffer = rcu_dereference(event->buffer);
  2568. if (!buffer)
  2569. goto out;
  2570. handle->buffer = buffer;
  2571. handle->event = event;
  2572. handle->nmi = nmi;
  2573. handle->sample = sample;
  2574. if (!buffer->nr_pages)
  2575. goto out;
  2576. have_lost = local_read(&buffer->lost);
  2577. if (have_lost)
  2578. size += sizeof(lost_event);
  2579. perf_output_get_handle(handle);
  2580. do {
  2581. /*
  2582. * Userspace could choose to issue a mb() before updating the
  2583. * tail pointer. So that all reads will be completed before the
  2584. * write is issued.
  2585. */
  2586. tail = ACCESS_ONCE(buffer->user_page->data_tail);
  2587. smp_rmb();
  2588. offset = head = local_read(&buffer->head);
  2589. head += size;
  2590. if (unlikely(!perf_output_space(buffer, tail, offset, head)))
  2591. goto fail;
  2592. } while (local_cmpxchg(&buffer->head, offset, head) != offset);
  2593. if (head - local_read(&buffer->wakeup) > buffer->watermark)
  2594. local_add(buffer->watermark, &buffer->wakeup);
  2595. handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
  2596. handle->page &= buffer->nr_pages - 1;
  2597. handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
  2598. handle->addr = buffer->data_pages[handle->page];
  2599. handle->addr += handle->size;
  2600. handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
  2601. if (have_lost) {
  2602. lost_event.header.type = PERF_RECORD_LOST;
  2603. lost_event.header.misc = 0;
  2604. lost_event.header.size = sizeof(lost_event);
  2605. lost_event.id = event->id;
  2606. lost_event.lost = local_xchg(&buffer->lost, 0);
  2607. perf_output_put(handle, lost_event);
  2608. }
  2609. return 0;
  2610. fail:
  2611. local_inc(&buffer->lost);
  2612. perf_output_put_handle(handle);
  2613. out:
  2614. rcu_read_unlock();
  2615. return -ENOSPC;
  2616. }
  2617. void perf_output_end(struct perf_output_handle *handle)
  2618. {
  2619. struct perf_event *event = handle->event;
  2620. struct perf_buffer *buffer = handle->buffer;
  2621. int wakeup_events = event->attr.wakeup_events;
  2622. if (handle->sample && wakeup_events) {
  2623. int events = local_inc_return(&buffer->events);
  2624. if (events >= wakeup_events) {
  2625. local_sub(wakeup_events, &buffer->events);
  2626. local_inc(&buffer->wakeup);
  2627. }
  2628. }
  2629. perf_output_put_handle(handle);
  2630. rcu_read_unlock();
  2631. }
  2632. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  2633. {
  2634. /*
  2635. * only top level events have the pid namespace they were created in
  2636. */
  2637. if (event->parent)
  2638. event = event->parent;
  2639. return task_tgid_nr_ns(p, event->ns);
  2640. }
  2641. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  2642. {
  2643. /*
  2644. * only top level events have the pid namespace they were created in
  2645. */
  2646. if (event->parent)
  2647. event = event->parent;
  2648. return task_pid_nr_ns(p, event->ns);
  2649. }
  2650. static void perf_output_read_one(struct perf_output_handle *handle,
  2651. struct perf_event *event)
  2652. {
  2653. u64 read_format = event->attr.read_format;
  2654. u64 values[4];
  2655. int n = 0;
  2656. values[n++] = perf_event_count(event);
  2657. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  2658. values[n++] = event->total_time_enabled +
  2659. atomic64_read(&event->child_total_time_enabled);
  2660. }
  2661. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  2662. values[n++] = event->total_time_running +
  2663. atomic64_read(&event->child_total_time_running);
  2664. }
  2665. if (read_format & PERF_FORMAT_ID)
  2666. values[n++] = primary_event_id(event);
  2667. perf_output_copy(handle, values, n * sizeof(u64));
  2668. }
  2669. /*
  2670. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  2671. */
  2672. static void perf_output_read_group(struct perf_output_handle *handle,
  2673. struct perf_event *event)
  2674. {
  2675. struct perf_event *leader = event->group_leader, *sub;
  2676. u64 read_format = event->attr.read_format;
  2677. u64 values[5];
  2678. int n = 0;
  2679. values[n++] = 1 + leader->nr_siblings;
  2680. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2681. values[n++] = leader->total_time_enabled;
  2682. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2683. values[n++] = leader->total_time_running;
  2684. if (leader != event)
  2685. leader->pmu->read(leader);
  2686. values[n++] = perf_event_count(leader);
  2687. if (read_format & PERF_FORMAT_ID)
  2688. values[n++] = primary_event_id(leader);
  2689. perf_output_copy(handle, values, n * sizeof(u64));
  2690. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2691. n = 0;
  2692. if (sub != event)
  2693. sub->pmu->read(sub);
  2694. values[n++] = perf_event_count(sub);
  2695. if (read_format & PERF_FORMAT_ID)
  2696. values[n++] = primary_event_id(sub);
  2697. perf_output_copy(handle, values, n * sizeof(u64));
  2698. }
  2699. }
  2700. static void perf_output_read(struct perf_output_handle *handle,
  2701. struct perf_event *event)
  2702. {
  2703. if (event->attr.read_format & PERF_FORMAT_GROUP)
  2704. perf_output_read_group(handle, event);
  2705. else
  2706. perf_output_read_one(handle, event);
  2707. }
  2708. void perf_output_sample(struct perf_output_handle *handle,
  2709. struct perf_event_header *header,
  2710. struct perf_sample_data *data,
  2711. struct perf_event *event)
  2712. {
  2713. u64 sample_type = data->type;
  2714. perf_output_put(handle, *header);
  2715. if (sample_type & PERF_SAMPLE_IP)
  2716. perf_output_put(handle, data->ip);
  2717. if (sample_type & PERF_SAMPLE_TID)
  2718. perf_output_put(handle, data->tid_entry);
  2719. if (sample_type & PERF_SAMPLE_TIME)
  2720. perf_output_put(handle, data->time);
  2721. if (sample_type & PERF_SAMPLE_ADDR)
  2722. perf_output_put(handle, data->addr);
  2723. if (sample_type & PERF_SAMPLE_ID)
  2724. perf_output_put(handle, data->id);
  2725. if (sample_type & PERF_SAMPLE_STREAM_ID)
  2726. perf_output_put(handle, data->stream_id);
  2727. if (sample_type & PERF_SAMPLE_CPU)
  2728. perf_output_put(handle, data->cpu_entry);
  2729. if (sample_type & PERF_SAMPLE_PERIOD)
  2730. perf_output_put(handle, data->period);
  2731. if (sample_type & PERF_SAMPLE_READ)
  2732. perf_output_read(handle, event);
  2733. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2734. if (data->callchain) {
  2735. int size = 1;
  2736. if (data->callchain)
  2737. size += data->callchain->nr;
  2738. size *= sizeof(u64);
  2739. perf_output_copy(handle, data->callchain, size);
  2740. } else {
  2741. u64 nr = 0;
  2742. perf_output_put(handle, nr);
  2743. }
  2744. }
  2745. if (sample_type & PERF_SAMPLE_RAW) {
  2746. if (data->raw) {
  2747. perf_output_put(handle, data->raw->size);
  2748. perf_output_copy(handle, data->raw->data,
  2749. data->raw->size);
  2750. } else {
  2751. struct {
  2752. u32 size;
  2753. u32 data;
  2754. } raw = {
  2755. .size = sizeof(u32),
  2756. .data = 0,
  2757. };
  2758. perf_output_put(handle, raw);
  2759. }
  2760. }
  2761. }
  2762. void perf_prepare_sample(struct perf_event_header *header,
  2763. struct perf_sample_data *data,
  2764. struct perf_event *event,
  2765. struct pt_regs *regs)
  2766. {
  2767. u64 sample_type = event->attr.sample_type;
  2768. data->type = sample_type;
  2769. header->type = PERF_RECORD_SAMPLE;
  2770. header->size = sizeof(*header);
  2771. header->misc = 0;
  2772. header->misc |= perf_misc_flags(regs);
  2773. if (sample_type & PERF_SAMPLE_IP) {
  2774. data->ip = perf_instruction_pointer(regs);
  2775. header->size += sizeof(data->ip);
  2776. }
  2777. if (sample_type & PERF_SAMPLE_TID) {
  2778. /* namespace issues */
  2779. data->tid_entry.pid = perf_event_pid(event, current);
  2780. data->tid_entry.tid = perf_event_tid(event, current);
  2781. header->size += sizeof(data->tid_entry);
  2782. }
  2783. if (sample_type & PERF_SAMPLE_TIME) {
  2784. data->time = perf_clock();
  2785. header->size += sizeof(data->time);
  2786. }
  2787. if (sample_type & PERF_SAMPLE_ADDR)
  2788. header->size += sizeof(data->addr);
  2789. if (sample_type & PERF_SAMPLE_ID) {
  2790. data->id = primary_event_id(event);
  2791. header->size += sizeof(data->id);
  2792. }
  2793. if (sample_type & PERF_SAMPLE_STREAM_ID) {
  2794. data->stream_id = event->id;
  2795. header->size += sizeof(data->stream_id);
  2796. }
  2797. if (sample_type & PERF_SAMPLE_CPU) {
  2798. data->cpu_entry.cpu = raw_smp_processor_id();
  2799. data->cpu_entry.reserved = 0;
  2800. header->size += sizeof(data->cpu_entry);
  2801. }
  2802. if (sample_type & PERF_SAMPLE_PERIOD)
  2803. header->size += sizeof(data->period);
  2804. if (sample_type & PERF_SAMPLE_READ)
  2805. header->size += perf_event_read_size(event);
  2806. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2807. int size = 1;
  2808. data->callchain = perf_callchain(regs);
  2809. if (data->callchain)
  2810. size += data->callchain->nr;
  2811. header->size += size * sizeof(u64);
  2812. }
  2813. if (sample_type & PERF_SAMPLE_RAW) {
  2814. int size = sizeof(u32);
  2815. if (data->raw)
  2816. size += data->raw->size;
  2817. else
  2818. size += sizeof(u32);
  2819. WARN_ON_ONCE(size & (sizeof(u64)-1));
  2820. header->size += size;
  2821. }
  2822. }
  2823. static void perf_event_output(struct perf_event *event, int nmi,
  2824. struct perf_sample_data *data,
  2825. struct pt_regs *regs)
  2826. {
  2827. struct perf_output_handle handle;
  2828. struct perf_event_header header;
  2829. perf_prepare_sample(&header, data, event, regs);
  2830. if (perf_output_begin(&handle, event, header.size, nmi, 1))
  2831. return;
  2832. perf_output_sample(&handle, &header, data, event);
  2833. perf_output_end(&handle);
  2834. }
  2835. /*
  2836. * read event_id
  2837. */
  2838. struct perf_read_event {
  2839. struct perf_event_header header;
  2840. u32 pid;
  2841. u32 tid;
  2842. };
  2843. static void
  2844. perf_event_read_event(struct perf_event *event,
  2845. struct task_struct *task)
  2846. {
  2847. struct perf_output_handle handle;
  2848. struct perf_read_event read_event = {
  2849. .header = {
  2850. .type = PERF_RECORD_READ,
  2851. .misc = 0,
  2852. .size = sizeof(read_event) + perf_event_read_size(event),
  2853. },
  2854. .pid = perf_event_pid(event, task),
  2855. .tid = perf_event_tid(event, task),
  2856. };
  2857. int ret;
  2858. ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
  2859. if (ret)
  2860. return;
  2861. perf_output_put(&handle, read_event);
  2862. perf_output_read(&handle, event);
  2863. perf_output_end(&handle);
  2864. }
  2865. /*
  2866. * task tracking -- fork/exit
  2867. *
  2868. * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
  2869. */
  2870. struct perf_task_event {
  2871. struct task_struct *task;
  2872. struct perf_event_context *task_ctx;
  2873. struct {
  2874. struct perf_event_header header;
  2875. u32 pid;
  2876. u32 ppid;
  2877. u32 tid;
  2878. u32 ptid;
  2879. u64 time;
  2880. } event_id;
  2881. };
  2882. static void perf_event_task_output(struct perf_event *event,
  2883. struct perf_task_event *task_event)
  2884. {
  2885. struct perf_output_handle handle;
  2886. struct task_struct *task = task_event->task;
  2887. int size, ret;
  2888. size = task_event->event_id.header.size;
  2889. ret = perf_output_begin(&handle, event, size, 0, 0);
  2890. if (ret)
  2891. return;
  2892. task_event->event_id.pid = perf_event_pid(event, task);
  2893. task_event->event_id.ppid = perf_event_pid(event, current);
  2894. task_event->event_id.tid = perf_event_tid(event, task);
  2895. task_event->event_id.ptid = perf_event_tid(event, current);
  2896. perf_output_put(&handle, task_event->event_id);
  2897. perf_output_end(&handle);
  2898. }
  2899. static int perf_event_task_match(struct perf_event *event)
  2900. {
  2901. if (event->state < PERF_EVENT_STATE_INACTIVE)
  2902. return 0;
  2903. if (event->cpu != -1 && event->cpu != smp_processor_id())
  2904. return 0;
  2905. if (event->attr.comm || event->attr.mmap ||
  2906. event->attr.mmap_data || event->attr.task)
  2907. return 1;
  2908. return 0;
  2909. }
  2910. static void perf_event_task_ctx(struct perf_event_context *ctx,
  2911. struct perf_task_event *task_event)
  2912. {
  2913. struct perf_event *event;
  2914. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  2915. if (perf_event_task_match(event))
  2916. perf_event_task_output(event, task_event);
  2917. }
  2918. }
  2919. static void perf_event_task_event(struct perf_task_event *task_event)
  2920. {
  2921. struct perf_cpu_context *cpuctx;
  2922. struct perf_event_context *ctx = task_event->task_ctx;
  2923. rcu_read_lock();
  2924. cpuctx = &get_cpu_var(perf_cpu_context);
  2925. perf_event_task_ctx(&cpuctx->ctx, task_event);
  2926. if (!ctx)
  2927. ctx = rcu_dereference(current->perf_event_ctxp);
  2928. if (ctx)
  2929. perf_event_task_ctx(ctx, task_event);
  2930. put_cpu_var(perf_cpu_context);
  2931. rcu_read_unlock();
  2932. }
  2933. static void perf_event_task(struct task_struct *task,
  2934. struct perf_event_context *task_ctx,
  2935. int new)
  2936. {
  2937. struct perf_task_event task_event;
  2938. if (!atomic_read(&nr_comm_events) &&
  2939. !atomic_read(&nr_mmap_events) &&
  2940. !atomic_read(&nr_task_events))
  2941. return;
  2942. task_event = (struct perf_task_event){
  2943. .task = task,
  2944. .task_ctx = task_ctx,
  2945. .event_id = {
  2946. .header = {
  2947. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  2948. .misc = 0,
  2949. .size = sizeof(task_event.event_id),
  2950. },
  2951. /* .pid */
  2952. /* .ppid */
  2953. /* .tid */
  2954. /* .ptid */
  2955. .time = perf_clock(),
  2956. },
  2957. };
  2958. perf_event_task_event(&task_event);
  2959. }
  2960. void perf_event_fork(struct task_struct *task)
  2961. {
  2962. perf_event_task(task, NULL, 1);
  2963. }
  2964. /*
  2965. * comm tracking
  2966. */
  2967. struct perf_comm_event {
  2968. struct task_struct *task;
  2969. char *comm;
  2970. int comm_size;
  2971. struct {
  2972. struct perf_event_header header;
  2973. u32 pid;
  2974. u32 tid;
  2975. } event_id;
  2976. };
  2977. static void perf_event_comm_output(struct perf_event *event,
  2978. struct perf_comm_event *comm_event)
  2979. {
  2980. struct perf_output_handle handle;
  2981. int size = comm_event->event_id.header.size;
  2982. int ret = perf_output_begin(&handle, event, size, 0, 0);
  2983. if (ret)
  2984. return;
  2985. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  2986. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  2987. perf_output_put(&handle, comm_event->event_id);
  2988. perf_output_copy(&handle, comm_event->comm,
  2989. comm_event->comm_size);
  2990. perf_output_end(&handle);
  2991. }
  2992. static int perf_event_comm_match(struct perf_event *event)
  2993. {
  2994. if (event->state < PERF_EVENT_STATE_INACTIVE)
  2995. return 0;
  2996. if (event->cpu != -1 && event->cpu != smp_processor_id())
  2997. return 0;
  2998. if (event->attr.comm)
  2999. return 1;
  3000. return 0;
  3001. }
  3002. static void perf_event_comm_ctx(struct perf_event_context *ctx,
  3003. struct perf_comm_event *comm_event)
  3004. {
  3005. struct perf_event *event;
  3006. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3007. if (perf_event_comm_match(event))
  3008. perf_event_comm_output(event, comm_event);
  3009. }
  3010. }
  3011. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  3012. {
  3013. struct perf_cpu_context *cpuctx;
  3014. struct perf_event_context *ctx;
  3015. unsigned int size;
  3016. char comm[TASK_COMM_LEN];
  3017. memset(comm, 0, sizeof(comm));
  3018. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  3019. size = ALIGN(strlen(comm)+1, sizeof(u64));
  3020. comm_event->comm = comm;
  3021. comm_event->comm_size = size;
  3022. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  3023. rcu_read_lock();
  3024. cpuctx = &get_cpu_var(perf_cpu_context);
  3025. perf_event_comm_ctx(&cpuctx->ctx, comm_event);
  3026. ctx = rcu_dereference(current->perf_event_ctxp);
  3027. if (ctx)
  3028. perf_event_comm_ctx(ctx, comm_event);
  3029. put_cpu_var(perf_cpu_context);
  3030. rcu_read_unlock();
  3031. }
  3032. void perf_event_comm(struct task_struct *task)
  3033. {
  3034. struct perf_comm_event comm_event;
  3035. if (task->perf_event_ctxp)
  3036. perf_event_enable_on_exec(task);
  3037. if (!atomic_read(&nr_comm_events))
  3038. return;
  3039. comm_event = (struct perf_comm_event){
  3040. .task = task,
  3041. /* .comm */
  3042. /* .comm_size */
  3043. .event_id = {
  3044. .header = {
  3045. .type = PERF_RECORD_COMM,
  3046. .misc = 0,
  3047. /* .size */
  3048. },
  3049. /* .pid */
  3050. /* .tid */
  3051. },
  3052. };
  3053. perf_event_comm_event(&comm_event);
  3054. }
  3055. /*
  3056. * mmap tracking
  3057. */
  3058. struct perf_mmap_event {
  3059. struct vm_area_struct *vma;
  3060. const char *file_name;
  3061. int file_size;
  3062. struct {
  3063. struct perf_event_header header;
  3064. u32 pid;
  3065. u32 tid;
  3066. u64 start;
  3067. u64 len;
  3068. u64 pgoff;
  3069. } event_id;
  3070. };
  3071. static void perf_event_mmap_output(struct perf_event *event,
  3072. struct perf_mmap_event *mmap_event)
  3073. {
  3074. struct perf_output_handle handle;
  3075. int size = mmap_event->event_id.header.size;
  3076. int ret = perf_output_begin(&handle, event, size, 0, 0);
  3077. if (ret)
  3078. return;
  3079. mmap_event->event_id.pid = perf_event_pid(event, current);
  3080. mmap_event->event_id.tid = perf_event_tid(event, current);
  3081. perf_output_put(&handle, mmap_event->event_id);
  3082. perf_output_copy(&handle, mmap_event->file_name,
  3083. mmap_event->file_size);
  3084. perf_output_end(&handle);
  3085. }
  3086. static int perf_event_mmap_match(struct perf_event *event,
  3087. struct perf_mmap_event *mmap_event,
  3088. int executable)
  3089. {
  3090. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3091. return 0;
  3092. if (event->cpu != -1 && event->cpu != smp_processor_id())
  3093. return 0;
  3094. if ((!executable && event->attr.mmap_data) ||
  3095. (executable && event->attr.mmap))
  3096. return 1;
  3097. return 0;
  3098. }
  3099. static void perf_event_mmap_ctx(struct perf_event_context *ctx,
  3100. struct perf_mmap_event *mmap_event,
  3101. int executable)
  3102. {
  3103. struct perf_event *event;
  3104. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3105. if (perf_event_mmap_match(event, mmap_event, executable))
  3106. perf_event_mmap_output(event, mmap_event);
  3107. }
  3108. }
  3109. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  3110. {
  3111. struct perf_cpu_context *cpuctx;
  3112. struct perf_event_context *ctx;
  3113. struct vm_area_struct *vma = mmap_event->vma;
  3114. struct file *file = vma->vm_file;
  3115. unsigned int size;
  3116. char tmp[16];
  3117. char *buf = NULL;
  3118. const char *name;
  3119. memset(tmp, 0, sizeof(tmp));
  3120. if (file) {
  3121. /*
  3122. * d_path works from the end of the buffer backwards, so we
  3123. * need to add enough zero bytes after the string to handle
  3124. * the 64bit alignment we do later.
  3125. */
  3126. buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
  3127. if (!buf) {
  3128. name = strncpy(tmp, "//enomem", sizeof(tmp));
  3129. goto got_name;
  3130. }
  3131. name = d_path(&file->f_path, buf, PATH_MAX);
  3132. if (IS_ERR(name)) {
  3133. name = strncpy(tmp, "//toolong", sizeof(tmp));
  3134. goto got_name;
  3135. }
  3136. } else {
  3137. if (arch_vma_name(mmap_event->vma)) {
  3138. name = strncpy(tmp, arch_vma_name(mmap_event->vma),
  3139. sizeof(tmp));
  3140. goto got_name;
  3141. }
  3142. if (!vma->vm_mm) {
  3143. name = strncpy(tmp, "[vdso]", sizeof(tmp));
  3144. goto got_name;
  3145. } else if (vma->vm_start <= vma->vm_mm->start_brk &&
  3146. vma->vm_end >= vma->vm_mm->brk) {
  3147. name = strncpy(tmp, "[heap]", sizeof(tmp));
  3148. goto got_name;
  3149. } else if (vma->vm_start <= vma->vm_mm->start_stack &&
  3150. vma->vm_end >= vma->vm_mm->start_stack) {
  3151. name = strncpy(tmp, "[stack]", sizeof(tmp));
  3152. goto got_name;
  3153. }
  3154. name = strncpy(tmp, "//anon", sizeof(tmp));
  3155. goto got_name;
  3156. }
  3157. got_name:
  3158. size = ALIGN(strlen(name)+1, sizeof(u64));
  3159. mmap_event->file_name = name;
  3160. mmap_event->file_size = size;
  3161. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  3162. rcu_read_lock();
  3163. cpuctx = &get_cpu_var(perf_cpu_context);
  3164. perf_event_mmap_ctx(&cpuctx->ctx, mmap_event, vma->vm_flags & VM_EXEC);
  3165. ctx = rcu_dereference(current->perf_event_ctxp);
  3166. if (ctx)
  3167. perf_event_mmap_ctx(ctx, mmap_event, vma->vm_flags & VM_EXEC);
  3168. put_cpu_var(perf_cpu_context);
  3169. rcu_read_unlock();
  3170. kfree(buf);
  3171. }
  3172. void perf_event_mmap(struct vm_area_struct *vma)
  3173. {
  3174. struct perf_mmap_event mmap_event;
  3175. if (!atomic_read(&nr_mmap_events))
  3176. return;
  3177. mmap_event = (struct perf_mmap_event){
  3178. .vma = vma,
  3179. /* .file_name */
  3180. /* .file_size */
  3181. .event_id = {
  3182. .header = {
  3183. .type = PERF_RECORD_MMAP,
  3184. .misc = PERF_RECORD_MISC_USER,
  3185. /* .size */
  3186. },
  3187. /* .pid */
  3188. /* .tid */
  3189. .start = vma->vm_start,
  3190. .len = vma->vm_end - vma->vm_start,
  3191. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  3192. },
  3193. };
  3194. perf_event_mmap_event(&mmap_event);
  3195. }
  3196. /*
  3197. * IRQ throttle logging
  3198. */
  3199. static void perf_log_throttle(struct perf_event *event, int enable)
  3200. {
  3201. struct perf_output_handle handle;
  3202. int ret;
  3203. struct {
  3204. struct perf_event_header header;
  3205. u64 time;
  3206. u64 id;
  3207. u64 stream_id;
  3208. } throttle_event = {
  3209. .header = {
  3210. .type = PERF_RECORD_THROTTLE,
  3211. .misc = 0,
  3212. .size = sizeof(throttle_event),
  3213. },
  3214. .time = perf_clock(),
  3215. .id = primary_event_id(event),
  3216. .stream_id = event->id,
  3217. };
  3218. if (enable)
  3219. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  3220. ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
  3221. if (ret)
  3222. return;
  3223. perf_output_put(&handle, throttle_event);
  3224. perf_output_end(&handle);
  3225. }
  3226. /*
  3227. * Generic event overflow handling, sampling.
  3228. */
  3229. static int __perf_event_overflow(struct perf_event *event, int nmi,
  3230. int throttle, struct perf_sample_data *data,
  3231. struct pt_regs *regs)
  3232. {
  3233. int events = atomic_read(&event->event_limit);
  3234. struct hw_perf_event *hwc = &event->hw;
  3235. int ret = 0;
  3236. throttle = (throttle && event->pmu->unthrottle != NULL);
  3237. if (!throttle) {
  3238. hwc->interrupts++;
  3239. } else {
  3240. if (hwc->interrupts != MAX_INTERRUPTS) {
  3241. hwc->interrupts++;
  3242. if (HZ * hwc->interrupts >
  3243. (u64)sysctl_perf_event_sample_rate) {
  3244. hwc->interrupts = MAX_INTERRUPTS;
  3245. perf_log_throttle(event, 0);
  3246. ret = 1;
  3247. }
  3248. } else {
  3249. /*
  3250. * Keep re-disabling events even though on the previous
  3251. * pass we disabled it - just in case we raced with a
  3252. * sched-in and the event got enabled again:
  3253. */
  3254. ret = 1;
  3255. }
  3256. }
  3257. if (event->attr.freq) {
  3258. u64 now = perf_clock();
  3259. s64 delta = now - hwc->freq_time_stamp;
  3260. hwc->freq_time_stamp = now;
  3261. if (delta > 0 && delta < 2*TICK_NSEC)
  3262. perf_adjust_period(event, delta, hwc->last_period);
  3263. }
  3264. /*
  3265. * XXX event_limit might not quite work as expected on inherited
  3266. * events
  3267. */
  3268. event->pending_kill = POLL_IN;
  3269. if (events && atomic_dec_and_test(&event->event_limit)) {
  3270. ret = 1;
  3271. event->pending_kill = POLL_HUP;
  3272. if (nmi) {
  3273. event->pending_disable = 1;
  3274. perf_pending_queue(&event->pending,
  3275. perf_pending_event);
  3276. } else
  3277. perf_event_disable(event);
  3278. }
  3279. if (event->overflow_handler)
  3280. event->overflow_handler(event, nmi, data, regs);
  3281. else
  3282. perf_event_output(event, nmi, data, regs);
  3283. return ret;
  3284. }
  3285. int perf_event_overflow(struct perf_event *event, int nmi,
  3286. struct perf_sample_data *data,
  3287. struct pt_regs *regs)
  3288. {
  3289. return __perf_event_overflow(event, nmi, 1, data, regs);
  3290. }
  3291. /*
  3292. * Generic software event infrastructure
  3293. */
  3294. /*
  3295. * We directly increment event->count and keep a second value in
  3296. * event->hw.period_left to count intervals. This period event
  3297. * is kept in the range [-sample_period, 0] so that we can use the
  3298. * sign as trigger.
  3299. */
  3300. static u64 perf_swevent_set_period(struct perf_event *event)
  3301. {
  3302. struct hw_perf_event *hwc = &event->hw;
  3303. u64 period = hwc->last_period;
  3304. u64 nr, offset;
  3305. s64 old, val;
  3306. hwc->last_period = hwc->sample_period;
  3307. again:
  3308. old = val = local64_read(&hwc->period_left);
  3309. if (val < 0)
  3310. return 0;
  3311. nr = div64_u64(period + val, period);
  3312. offset = nr * period;
  3313. val -= offset;
  3314. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  3315. goto again;
  3316. return nr;
  3317. }
  3318. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  3319. int nmi, struct perf_sample_data *data,
  3320. struct pt_regs *regs)
  3321. {
  3322. struct hw_perf_event *hwc = &event->hw;
  3323. int throttle = 0;
  3324. data->period = event->hw.last_period;
  3325. if (!overflow)
  3326. overflow = perf_swevent_set_period(event);
  3327. if (hwc->interrupts == MAX_INTERRUPTS)
  3328. return;
  3329. for (; overflow; overflow--) {
  3330. if (__perf_event_overflow(event, nmi, throttle,
  3331. data, regs)) {
  3332. /*
  3333. * We inhibit the overflow from happening when
  3334. * hwc->interrupts == MAX_INTERRUPTS.
  3335. */
  3336. break;
  3337. }
  3338. throttle = 1;
  3339. }
  3340. }
  3341. static void perf_swevent_add(struct perf_event *event, u64 nr,
  3342. int nmi, struct perf_sample_data *data,
  3343. struct pt_regs *regs)
  3344. {
  3345. struct hw_perf_event *hwc = &event->hw;
  3346. local64_add(nr, &event->count);
  3347. if (!regs)
  3348. return;
  3349. if (!hwc->sample_period)
  3350. return;
  3351. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  3352. return perf_swevent_overflow(event, 1, nmi, data, regs);
  3353. if (local64_add_negative(nr, &hwc->period_left))
  3354. return;
  3355. perf_swevent_overflow(event, 0, nmi, data, regs);
  3356. }
  3357. static int perf_exclude_event(struct perf_event *event,
  3358. struct pt_regs *regs)
  3359. {
  3360. if (regs) {
  3361. if (event->attr.exclude_user && user_mode(regs))
  3362. return 1;
  3363. if (event->attr.exclude_kernel && !user_mode(regs))
  3364. return 1;
  3365. }
  3366. return 0;
  3367. }
  3368. static int perf_swevent_match(struct perf_event *event,
  3369. enum perf_type_id type,
  3370. u32 event_id,
  3371. struct perf_sample_data *data,
  3372. struct pt_regs *regs)
  3373. {
  3374. if (event->attr.type != type)
  3375. return 0;
  3376. if (event->attr.config != event_id)
  3377. return 0;
  3378. if (perf_exclude_event(event, regs))
  3379. return 0;
  3380. return 1;
  3381. }
  3382. static inline u64 swevent_hash(u64 type, u32 event_id)
  3383. {
  3384. u64 val = event_id | (type << 32);
  3385. return hash_64(val, SWEVENT_HLIST_BITS);
  3386. }
  3387. static inline struct hlist_head *
  3388. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  3389. {
  3390. u64 hash = swevent_hash(type, event_id);
  3391. return &hlist->heads[hash];
  3392. }
  3393. /* For the read side: events when they trigger */
  3394. static inline struct hlist_head *
  3395. find_swevent_head_rcu(struct perf_cpu_context *ctx, u64 type, u32 event_id)
  3396. {
  3397. struct swevent_hlist *hlist;
  3398. hlist = rcu_dereference(ctx->swevent_hlist);
  3399. if (!hlist)
  3400. return NULL;
  3401. return __find_swevent_head(hlist, type, event_id);
  3402. }
  3403. /* For the event head insertion and removal in the hlist */
  3404. static inline struct hlist_head *
  3405. find_swevent_head(struct perf_cpu_context *ctx, struct perf_event *event)
  3406. {
  3407. struct swevent_hlist *hlist;
  3408. u32 event_id = event->attr.config;
  3409. u64 type = event->attr.type;
  3410. /*
  3411. * Event scheduling is always serialized against hlist allocation
  3412. * and release. Which makes the protected version suitable here.
  3413. * The context lock guarantees that.
  3414. */
  3415. hlist = rcu_dereference_protected(ctx->swevent_hlist,
  3416. lockdep_is_held(&event->ctx->lock));
  3417. if (!hlist)
  3418. return NULL;
  3419. return __find_swevent_head(hlist, type, event_id);
  3420. }
  3421. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  3422. u64 nr, int nmi,
  3423. struct perf_sample_data *data,
  3424. struct pt_regs *regs)
  3425. {
  3426. struct perf_cpu_context *cpuctx;
  3427. struct perf_event *event;
  3428. struct hlist_node *node;
  3429. struct hlist_head *head;
  3430. cpuctx = &__get_cpu_var(perf_cpu_context);
  3431. rcu_read_lock();
  3432. head = find_swevent_head_rcu(cpuctx, type, event_id);
  3433. if (!head)
  3434. goto end;
  3435. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3436. if (perf_swevent_match(event, type, event_id, data, regs))
  3437. perf_swevent_add(event, nr, nmi, data, regs);
  3438. }
  3439. end:
  3440. rcu_read_unlock();
  3441. }
  3442. int perf_swevent_get_recursion_context(void)
  3443. {
  3444. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  3445. int rctx;
  3446. if (in_nmi())
  3447. rctx = 3;
  3448. else if (in_irq())
  3449. rctx = 2;
  3450. else if (in_softirq())
  3451. rctx = 1;
  3452. else
  3453. rctx = 0;
  3454. if (cpuctx->recursion[rctx])
  3455. return -1;
  3456. cpuctx->recursion[rctx]++;
  3457. barrier();
  3458. return rctx;
  3459. }
  3460. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  3461. void inline perf_swevent_put_recursion_context(int rctx)
  3462. {
  3463. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  3464. barrier();
  3465. cpuctx->recursion[rctx]--;
  3466. }
  3467. void __perf_sw_event(u32 event_id, u64 nr, int nmi,
  3468. struct pt_regs *regs, u64 addr)
  3469. {
  3470. struct perf_sample_data data;
  3471. int rctx;
  3472. preempt_disable_notrace();
  3473. rctx = perf_swevent_get_recursion_context();
  3474. if (rctx < 0)
  3475. return;
  3476. perf_sample_data_init(&data, addr);
  3477. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
  3478. perf_swevent_put_recursion_context(rctx);
  3479. preempt_enable_notrace();
  3480. }
  3481. static void perf_swevent_read(struct perf_event *event)
  3482. {
  3483. }
  3484. static int perf_swevent_enable(struct perf_event *event)
  3485. {
  3486. struct hw_perf_event *hwc = &event->hw;
  3487. struct perf_cpu_context *cpuctx;
  3488. struct hlist_head *head;
  3489. cpuctx = &__get_cpu_var(perf_cpu_context);
  3490. if (hwc->sample_period) {
  3491. hwc->last_period = hwc->sample_period;
  3492. perf_swevent_set_period(event);
  3493. }
  3494. head = find_swevent_head(cpuctx, event);
  3495. if (WARN_ON_ONCE(!head))
  3496. return -EINVAL;
  3497. hlist_add_head_rcu(&event->hlist_entry, head);
  3498. return 0;
  3499. }
  3500. static void perf_swevent_disable(struct perf_event *event)
  3501. {
  3502. hlist_del_rcu(&event->hlist_entry);
  3503. }
  3504. static void perf_swevent_void(struct perf_event *event)
  3505. {
  3506. }
  3507. static int perf_swevent_int(struct perf_event *event)
  3508. {
  3509. return 0;
  3510. }
  3511. static const struct pmu perf_ops_generic = {
  3512. .enable = perf_swevent_enable,
  3513. .disable = perf_swevent_disable,
  3514. .start = perf_swevent_int,
  3515. .stop = perf_swevent_void,
  3516. .read = perf_swevent_read,
  3517. .unthrottle = perf_swevent_void, /* hwc->interrupts already reset */
  3518. };
  3519. /*
  3520. * hrtimer based swevent callback
  3521. */
  3522. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  3523. {
  3524. enum hrtimer_restart ret = HRTIMER_RESTART;
  3525. struct perf_sample_data data;
  3526. struct pt_regs *regs;
  3527. struct perf_event *event;
  3528. u64 period;
  3529. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  3530. event->pmu->read(event);
  3531. perf_sample_data_init(&data, 0);
  3532. data.period = event->hw.last_period;
  3533. regs = get_irq_regs();
  3534. if (regs && !perf_exclude_event(event, regs)) {
  3535. if (!(event->attr.exclude_idle && current->pid == 0))
  3536. if (perf_event_overflow(event, 0, &data, regs))
  3537. ret = HRTIMER_NORESTART;
  3538. }
  3539. period = max_t(u64, 10000, event->hw.sample_period);
  3540. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  3541. return ret;
  3542. }
  3543. static void perf_swevent_start_hrtimer(struct perf_event *event)
  3544. {
  3545. struct hw_perf_event *hwc = &event->hw;
  3546. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3547. hwc->hrtimer.function = perf_swevent_hrtimer;
  3548. if (hwc->sample_period) {
  3549. u64 period;
  3550. if (hwc->remaining) {
  3551. if (hwc->remaining < 0)
  3552. period = 10000;
  3553. else
  3554. period = hwc->remaining;
  3555. hwc->remaining = 0;
  3556. } else {
  3557. period = max_t(u64, 10000, hwc->sample_period);
  3558. }
  3559. __hrtimer_start_range_ns(&hwc->hrtimer,
  3560. ns_to_ktime(period), 0,
  3561. HRTIMER_MODE_REL, 0);
  3562. }
  3563. }
  3564. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  3565. {
  3566. struct hw_perf_event *hwc = &event->hw;
  3567. if (hwc->sample_period) {
  3568. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  3569. hwc->remaining = ktime_to_ns(remaining);
  3570. hrtimer_cancel(&hwc->hrtimer);
  3571. }
  3572. }
  3573. /*
  3574. * Software event: cpu wall time clock
  3575. */
  3576. static void cpu_clock_perf_event_update(struct perf_event *event)
  3577. {
  3578. int cpu = raw_smp_processor_id();
  3579. s64 prev;
  3580. u64 now;
  3581. now = cpu_clock(cpu);
  3582. prev = local64_xchg(&event->hw.prev_count, now);
  3583. local64_add(now - prev, &event->count);
  3584. }
  3585. static int cpu_clock_perf_event_enable(struct perf_event *event)
  3586. {
  3587. struct hw_perf_event *hwc = &event->hw;
  3588. int cpu = raw_smp_processor_id();
  3589. local64_set(&hwc->prev_count, cpu_clock(cpu));
  3590. perf_swevent_start_hrtimer(event);
  3591. return 0;
  3592. }
  3593. static void cpu_clock_perf_event_disable(struct perf_event *event)
  3594. {
  3595. perf_swevent_cancel_hrtimer(event);
  3596. cpu_clock_perf_event_update(event);
  3597. }
  3598. static void cpu_clock_perf_event_read(struct perf_event *event)
  3599. {
  3600. cpu_clock_perf_event_update(event);
  3601. }
  3602. static const struct pmu perf_ops_cpu_clock = {
  3603. .enable = cpu_clock_perf_event_enable,
  3604. .disable = cpu_clock_perf_event_disable,
  3605. .read = cpu_clock_perf_event_read,
  3606. };
  3607. /*
  3608. * Software event: task time clock
  3609. */
  3610. static void task_clock_perf_event_update(struct perf_event *event, u64 now)
  3611. {
  3612. u64 prev;
  3613. s64 delta;
  3614. prev = local64_xchg(&event->hw.prev_count, now);
  3615. delta = now - prev;
  3616. local64_add(delta, &event->count);
  3617. }
  3618. static int task_clock_perf_event_enable(struct perf_event *event)
  3619. {
  3620. struct hw_perf_event *hwc = &event->hw;
  3621. u64 now;
  3622. now = event->ctx->time;
  3623. local64_set(&hwc->prev_count, now);
  3624. perf_swevent_start_hrtimer(event);
  3625. return 0;
  3626. }
  3627. static void task_clock_perf_event_disable(struct perf_event *event)
  3628. {
  3629. perf_swevent_cancel_hrtimer(event);
  3630. task_clock_perf_event_update(event, event->ctx->time);
  3631. }
  3632. static void task_clock_perf_event_read(struct perf_event *event)
  3633. {
  3634. u64 time;
  3635. if (!in_nmi()) {
  3636. update_context_time(event->ctx);
  3637. time = event->ctx->time;
  3638. } else {
  3639. u64 now = perf_clock();
  3640. u64 delta = now - event->ctx->timestamp;
  3641. time = event->ctx->time + delta;
  3642. }
  3643. task_clock_perf_event_update(event, time);
  3644. }
  3645. static const struct pmu perf_ops_task_clock = {
  3646. .enable = task_clock_perf_event_enable,
  3647. .disable = task_clock_perf_event_disable,
  3648. .read = task_clock_perf_event_read,
  3649. };
  3650. /* Deref the hlist from the update side */
  3651. static inline struct swevent_hlist *
  3652. swevent_hlist_deref(struct perf_cpu_context *cpuctx)
  3653. {
  3654. return rcu_dereference_protected(cpuctx->swevent_hlist,
  3655. lockdep_is_held(&cpuctx->hlist_mutex));
  3656. }
  3657. static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
  3658. {
  3659. struct swevent_hlist *hlist;
  3660. hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
  3661. kfree(hlist);
  3662. }
  3663. static void swevent_hlist_release(struct perf_cpu_context *cpuctx)
  3664. {
  3665. struct swevent_hlist *hlist = swevent_hlist_deref(cpuctx);
  3666. if (!hlist)
  3667. return;
  3668. rcu_assign_pointer(cpuctx->swevent_hlist, NULL);
  3669. call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
  3670. }
  3671. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  3672. {
  3673. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  3674. mutex_lock(&cpuctx->hlist_mutex);
  3675. if (!--cpuctx->hlist_refcount)
  3676. swevent_hlist_release(cpuctx);
  3677. mutex_unlock(&cpuctx->hlist_mutex);
  3678. }
  3679. static void swevent_hlist_put(struct perf_event *event)
  3680. {
  3681. int cpu;
  3682. if (event->cpu != -1) {
  3683. swevent_hlist_put_cpu(event, event->cpu);
  3684. return;
  3685. }
  3686. for_each_possible_cpu(cpu)
  3687. swevent_hlist_put_cpu(event, cpu);
  3688. }
  3689. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  3690. {
  3691. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  3692. int err = 0;
  3693. mutex_lock(&cpuctx->hlist_mutex);
  3694. if (!swevent_hlist_deref(cpuctx) && cpu_online(cpu)) {
  3695. struct swevent_hlist *hlist;
  3696. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  3697. if (!hlist) {
  3698. err = -ENOMEM;
  3699. goto exit;
  3700. }
  3701. rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
  3702. }
  3703. cpuctx->hlist_refcount++;
  3704. exit:
  3705. mutex_unlock(&cpuctx->hlist_mutex);
  3706. return err;
  3707. }
  3708. static int swevent_hlist_get(struct perf_event *event)
  3709. {
  3710. int err;
  3711. int cpu, failed_cpu;
  3712. if (event->cpu != -1)
  3713. return swevent_hlist_get_cpu(event, event->cpu);
  3714. get_online_cpus();
  3715. for_each_possible_cpu(cpu) {
  3716. err = swevent_hlist_get_cpu(event, cpu);
  3717. if (err) {
  3718. failed_cpu = cpu;
  3719. goto fail;
  3720. }
  3721. }
  3722. put_online_cpus();
  3723. return 0;
  3724. fail:
  3725. for_each_possible_cpu(cpu) {
  3726. if (cpu == failed_cpu)
  3727. break;
  3728. swevent_hlist_put_cpu(event, cpu);
  3729. }
  3730. put_online_cpus();
  3731. return err;
  3732. }
  3733. #ifdef CONFIG_EVENT_TRACING
  3734. static const struct pmu perf_ops_tracepoint = {
  3735. .enable = perf_trace_enable,
  3736. .disable = perf_trace_disable,
  3737. .start = perf_swevent_int,
  3738. .stop = perf_swevent_void,
  3739. .read = perf_swevent_read,
  3740. .unthrottle = perf_swevent_void,
  3741. };
  3742. static int perf_tp_filter_match(struct perf_event *event,
  3743. struct perf_sample_data *data)
  3744. {
  3745. void *record = data->raw->data;
  3746. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  3747. return 1;
  3748. return 0;
  3749. }
  3750. static int perf_tp_event_match(struct perf_event *event,
  3751. struct perf_sample_data *data,
  3752. struct pt_regs *regs)
  3753. {
  3754. /*
  3755. * All tracepoints are from kernel-space.
  3756. */
  3757. if (event->attr.exclude_kernel)
  3758. return 0;
  3759. if (!perf_tp_filter_match(event, data))
  3760. return 0;
  3761. return 1;
  3762. }
  3763. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  3764. struct pt_regs *regs, struct hlist_head *head, int rctx)
  3765. {
  3766. struct perf_sample_data data;
  3767. struct perf_event *event;
  3768. struct hlist_node *node;
  3769. struct perf_raw_record raw = {
  3770. .size = entry_size,
  3771. .data = record,
  3772. };
  3773. perf_sample_data_init(&data, addr);
  3774. data.raw = &raw;
  3775. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3776. if (perf_tp_event_match(event, &data, regs))
  3777. perf_swevent_add(event, count, 1, &data, regs);
  3778. }
  3779. perf_swevent_put_recursion_context(rctx);
  3780. }
  3781. EXPORT_SYMBOL_GPL(perf_tp_event);
  3782. static void tp_perf_event_destroy(struct perf_event *event)
  3783. {
  3784. perf_trace_destroy(event);
  3785. }
  3786. static const struct pmu *tp_perf_event_init(struct perf_event *event)
  3787. {
  3788. int err;
  3789. /*
  3790. * Raw tracepoint data is a severe data leak, only allow root to
  3791. * have these.
  3792. */
  3793. if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
  3794. perf_paranoid_tracepoint_raw() &&
  3795. !capable(CAP_SYS_ADMIN))
  3796. return ERR_PTR(-EPERM);
  3797. err = perf_trace_init(event);
  3798. if (err)
  3799. return NULL;
  3800. event->destroy = tp_perf_event_destroy;
  3801. return &perf_ops_tracepoint;
  3802. }
  3803. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  3804. {
  3805. char *filter_str;
  3806. int ret;
  3807. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  3808. return -EINVAL;
  3809. filter_str = strndup_user(arg, PAGE_SIZE);
  3810. if (IS_ERR(filter_str))
  3811. return PTR_ERR(filter_str);
  3812. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  3813. kfree(filter_str);
  3814. return ret;
  3815. }
  3816. static void perf_event_free_filter(struct perf_event *event)
  3817. {
  3818. ftrace_profile_free_filter(event);
  3819. }
  3820. #else
  3821. static const struct pmu *tp_perf_event_init(struct perf_event *event)
  3822. {
  3823. return NULL;
  3824. }
  3825. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  3826. {
  3827. return -ENOENT;
  3828. }
  3829. static void perf_event_free_filter(struct perf_event *event)
  3830. {
  3831. }
  3832. #endif /* CONFIG_EVENT_TRACING */
  3833. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  3834. static void bp_perf_event_destroy(struct perf_event *event)
  3835. {
  3836. release_bp_slot(event);
  3837. }
  3838. static const struct pmu *bp_perf_event_init(struct perf_event *bp)
  3839. {
  3840. int err;
  3841. err = register_perf_hw_breakpoint(bp);
  3842. if (err)
  3843. return ERR_PTR(err);
  3844. bp->destroy = bp_perf_event_destroy;
  3845. return &perf_ops_bp;
  3846. }
  3847. void perf_bp_event(struct perf_event *bp, void *data)
  3848. {
  3849. struct perf_sample_data sample;
  3850. struct pt_regs *regs = data;
  3851. perf_sample_data_init(&sample, bp->attr.bp_addr);
  3852. if (!perf_exclude_event(bp, regs))
  3853. perf_swevent_add(bp, 1, 1, &sample, regs);
  3854. }
  3855. #else
  3856. static const struct pmu *bp_perf_event_init(struct perf_event *bp)
  3857. {
  3858. return NULL;
  3859. }
  3860. void perf_bp_event(struct perf_event *bp, void *regs)
  3861. {
  3862. }
  3863. #endif
  3864. atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
  3865. static void sw_perf_event_destroy(struct perf_event *event)
  3866. {
  3867. u64 event_id = event->attr.config;
  3868. WARN_ON(event->parent);
  3869. atomic_dec(&perf_swevent_enabled[event_id]);
  3870. swevent_hlist_put(event);
  3871. }
  3872. static const struct pmu *sw_perf_event_init(struct perf_event *event)
  3873. {
  3874. const struct pmu *pmu = NULL;
  3875. u64 event_id = event->attr.config;
  3876. /*
  3877. * Software events (currently) can't in general distinguish
  3878. * between user, kernel and hypervisor events.
  3879. * However, context switches and cpu migrations are considered
  3880. * to be kernel events, and page faults are never hypervisor
  3881. * events.
  3882. */
  3883. switch (event_id) {
  3884. case PERF_COUNT_SW_CPU_CLOCK:
  3885. pmu = &perf_ops_cpu_clock;
  3886. break;
  3887. case PERF_COUNT_SW_TASK_CLOCK:
  3888. /*
  3889. * If the user instantiates this as a per-cpu event,
  3890. * use the cpu_clock event instead.
  3891. */
  3892. if (event->ctx->task)
  3893. pmu = &perf_ops_task_clock;
  3894. else
  3895. pmu = &perf_ops_cpu_clock;
  3896. break;
  3897. case PERF_COUNT_SW_PAGE_FAULTS:
  3898. case PERF_COUNT_SW_PAGE_FAULTS_MIN:
  3899. case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
  3900. case PERF_COUNT_SW_CONTEXT_SWITCHES:
  3901. case PERF_COUNT_SW_CPU_MIGRATIONS:
  3902. case PERF_COUNT_SW_ALIGNMENT_FAULTS:
  3903. case PERF_COUNT_SW_EMULATION_FAULTS:
  3904. if (!event->parent) {
  3905. int err;
  3906. err = swevent_hlist_get(event);
  3907. if (err)
  3908. return ERR_PTR(err);
  3909. atomic_inc(&perf_swevent_enabled[event_id]);
  3910. event->destroy = sw_perf_event_destroy;
  3911. }
  3912. pmu = &perf_ops_generic;
  3913. break;
  3914. }
  3915. return pmu;
  3916. }
  3917. /*
  3918. * Allocate and initialize a event structure
  3919. */
  3920. static struct perf_event *
  3921. perf_event_alloc(struct perf_event_attr *attr,
  3922. int cpu,
  3923. struct perf_event_context *ctx,
  3924. struct perf_event *group_leader,
  3925. struct perf_event *parent_event,
  3926. perf_overflow_handler_t overflow_handler,
  3927. gfp_t gfpflags)
  3928. {
  3929. const struct pmu *pmu;
  3930. struct perf_event *event;
  3931. struct hw_perf_event *hwc;
  3932. long err;
  3933. event = kzalloc(sizeof(*event), gfpflags);
  3934. if (!event)
  3935. return ERR_PTR(-ENOMEM);
  3936. /*
  3937. * Single events are their own group leaders, with an
  3938. * empty sibling list:
  3939. */
  3940. if (!group_leader)
  3941. group_leader = event;
  3942. mutex_init(&event->child_mutex);
  3943. INIT_LIST_HEAD(&event->child_list);
  3944. INIT_LIST_HEAD(&event->group_entry);
  3945. INIT_LIST_HEAD(&event->event_entry);
  3946. INIT_LIST_HEAD(&event->sibling_list);
  3947. init_waitqueue_head(&event->waitq);
  3948. mutex_init(&event->mmap_mutex);
  3949. event->cpu = cpu;
  3950. event->attr = *attr;
  3951. event->group_leader = group_leader;
  3952. event->pmu = NULL;
  3953. event->ctx = ctx;
  3954. event->oncpu = -1;
  3955. event->parent = parent_event;
  3956. event->ns = get_pid_ns(current->nsproxy->pid_ns);
  3957. event->id = atomic64_inc_return(&perf_event_id);
  3958. event->state = PERF_EVENT_STATE_INACTIVE;
  3959. if (!overflow_handler && parent_event)
  3960. overflow_handler = parent_event->overflow_handler;
  3961. event->overflow_handler = overflow_handler;
  3962. if (attr->disabled)
  3963. event->state = PERF_EVENT_STATE_OFF;
  3964. pmu = NULL;
  3965. hwc = &event->hw;
  3966. hwc->sample_period = attr->sample_period;
  3967. if (attr->freq && attr->sample_freq)
  3968. hwc->sample_period = 1;
  3969. hwc->last_period = hwc->sample_period;
  3970. local64_set(&hwc->period_left, hwc->sample_period);
  3971. /*
  3972. * we currently do not support PERF_FORMAT_GROUP on inherited events
  3973. */
  3974. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  3975. goto done;
  3976. switch (attr->type) {
  3977. case PERF_TYPE_RAW:
  3978. case PERF_TYPE_HARDWARE:
  3979. case PERF_TYPE_HW_CACHE:
  3980. pmu = hw_perf_event_init(event);
  3981. break;
  3982. case PERF_TYPE_SOFTWARE:
  3983. pmu = sw_perf_event_init(event);
  3984. break;
  3985. case PERF_TYPE_TRACEPOINT:
  3986. pmu = tp_perf_event_init(event);
  3987. break;
  3988. case PERF_TYPE_BREAKPOINT:
  3989. pmu = bp_perf_event_init(event);
  3990. break;
  3991. default:
  3992. break;
  3993. }
  3994. done:
  3995. err = 0;
  3996. if (!pmu)
  3997. err = -EINVAL;
  3998. else if (IS_ERR(pmu))
  3999. err = PTR_ERR(pmu);
  4000. if (err) {
  4001. if (event->ns)
  4002. put_pid_ns(event->ns);
  4003. kfree(event);
  4004. return ERR_PTR(err);
  4005. }
  4006. event->pmu = pmu;
  4007. if (!event->parent) {
  4008. atomic_inc(&nr_events);
  4009. if (event->attr.mmap || event->attr.mmap_data)
  4010. atomic_inc(&nr_mmap_events);
  4011. if (event->attr.comm)
  4012. atomic_inc(&nr_comm_events);
  4013. if (event->attr.task)
  4014. atomic_inc(&nr_task_events);
  4015. }
  4016. return event;
  4017. }
  4018. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  4019. struct perf_event_attr *attr)
  4020. {
  4021. u32 size;
  4022. int ret;
  4023. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  4024. return -EFAULT;
  4025. /*
  4026. * zero the full structure, so that a short copy will be nice.
  4027. */
  4028. memset(attr, 0, sizeof(*attr));
  4029. ret = get_user(size, &uattr->size);
  4030. if (ret)
  4031. return ret;
  4032. if (size > PAGE_SIZE) /* silly large */
  4033. goto err_size;
  4034. if (!size) /* abi compat */
  4035. size = PERF_ATTR_SIZE_VER0;
  4036. if (size < PERF_ATTR_SIZE_VER0)
  4037. goto err_size;
  4038. /*
  4039. * If we're handed a bigger struct than we know of,
  4040. * ensure all the unknown bits are 0 - i.e. new
  4041. * user-space does not rely on any kernel feature
  4042. * extensions we dont know about yet.
  4043. */
  4044. if (size > sizeof(*attr)) {
  4045. unsigned char __user *addr;
  4046. unsigned char __user *end;
  4047. unsigned char val;
  4048. addr = (void __user *)uattr + sizeof(*attr);
  4049. end = (void __user *)uattr + size;
  4050. for (; addr < end; addr++) {
  4051. ret = get_user(val, addr);
  4052. if (ret)
  4053. return ret;
  4054. if (val)
  4055. goto err_size;
  4056. }
  4057. size = sizeof(*attr);
  4058. }
  4059. ret = copy_from_user(attr, uattr, size);
  4060. if (ret)
  4061. return -EFAULT;
  4062. /*
  4063. * If the type exists, the corresponding creation will verify
  4064. * the attr->config.
  4065. */
  4066. if (attr->type >= PERF_TYPE_MAX)
  4067. return -EINVAL;
  4068. if (attr->__reserved_1)
  4069. return -EINVAL;
  4070. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  4071. return -EINVAL;
  4072. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  4073. return -EINVAL;
  4074. out:
  4075. return ret;
  4076. err_size:
  4077. put_user(sizeof(*attr), &uattr->size);
  4078. ret = -E2BIG;
  4079. goto out;
  4080. }
  4081. static int
  4082. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  4083. {
  4084. struct perf_buffer *buffer = NULL, *old_buffer = NULL;
  4085. int ret = -EINVAL;
  4086. if (!output_event)
  4087. goto set;
  4088. /* don't allow circular references */
  4089. if (event == output_event)
  4090. goto out;
  4091. /*
  4092. * Don't allow cross-cpu buffers
  4093. */
  4094. if (output_event->cpu != event->cpu)
  4095. goto out;
  4096. /*
  4097. * If its not a per-cpu buffer, it must be the same task.
  4098. */
  4099. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  4100. goto out;
  4101. set:
  4102. mutex_lock(&event->mmap_mutex);
  4103. /* Can't redirect output if we've got an active mmap() */
  4104. if (atomic_read(&event->mmap_count))
  4105. goto unlock;
  4106. if (output_event) {
  4107. /* get the buffer we want to redirect to */
  4108. buffer = perf_buffer_get(output_event);
  4109. if (!buffer)
  4110. goto unlock;
  4111. }
  4112. old_buffer = event->buffer;
  4113. rcu_assign_pointer(event->buffer, buffer);
  4114. ret = 0;
  4115. unlock:
  4116. mutex_unlock(&event->mmap_mutex);
  4117. if (old_buffer)
  4118. perf_buffer_put(old_buffer);
  4119. out:
  4120. return ret;
  4121. }
  4122. /**
  4123. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  4124. *
  4125. * @attr_uptr: event_id type attributes for monitoring/sampling
  4126. * @pid: target pid
  4127. * @cpu: target cpu
  4128. * @group_fd: group leader event fd
  4129. */
  4130. SYSCALL_DEFINE5(perf_event_open,
  4131. struct perf_event_attr __user *, attr_uptr,
  4132. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  4133. {
  4134. struct perf_event *event, *group_leader = NULL, *output_event = NULL;
  4135. struct perf_event_attr attr;
  4136. struct perf_event_context *ctx;
  4137. struct file *event_file = NULL;
  4138. struct file *group_file = NULL;
  4139. int event_fd;
  4140. int fput_needed = 0;
  4141. int err;
  4142. /* for future expandability... */
  4143. if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
  4144. return -EINVAL;
  4145. err = perf_copy_attr(attr_uptr, &attr);
  4146. if (err)
  4147. return err;
  4148. if (!attr.exclude_kernel) {
  4149. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  4150. return -EACCES;
  4151. }
  4152. if (attr.freq) {
  4153. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  4154. return -EINVAL;
  4155. }
  4156. event_fd = get_unused_fd_flags(O_RDWR);
  4157. if (event_fd < 0)
  4158. return event_fd;
  4159. /*
  4160. * Get the target context (task or percpu):
  4161. */
  4162. ctx = find_get_context(pid, cpu);
  4163. if (IS_ERR(ctx)) {
  4164. err = PTR_ERR(ctx);
  4165. goto err_fd;
  4166. }
  4167. if (group_fd != -1) {
  4168. group_leader = perf_fget_light(group_fd, &fput_needed);
  4169. if (IS_ERR(group_leader)) {
  4170. err = PTR_ERR(group_leader);
  4171. goto err_put_context;
  4172. }
  4173. group_file = group_leader->filp;
  4174. if (flags & PERF_FLAG_FD_OUTPUT)
  4175. output_event = group_leader;
  4176. if (flags & PERF_FLAG_FD_NO_GROUP)
  4177. group_leader = NULL;
  4178. }
  4179. /*
  4180. * Look up the group leader (we will attach this event to it):
  4181. */
  4182. if (group_leader) {
  4183. err = -EINVAL;
  4184. /*
  4185. * Do not allow a recursive hierarchy (this new sibling
  4186. * becoming part of another group-sibling):
  4187. */
  4188. if (group_leader->group_leader != group_leader)
  4189. goto err_put_context;
  4190. /*
  4191. * Do not allow to attach to a group in a different
  4192. * task or CPU context:
  4193. */
  4194. if (group_leader->ctx != ctx)
  4195. goto err_put_context;
  4196. /*
  4197. * Only a group leader can be exclusive or pinned
  4198. */
  4199. if (attr.exclusive || attr.pinned)
  4200. goto err_put_context;
  4201. }
  4202. event = perf_event_alloc(&attr, cpu, ctx, group_leader,
  4203. NULL, NULL, GFP_KERNEL);
  4204. if (IS_ERR(event)) {
  4205. err = PTR_ERR(event);
  4206. goto err_put_context;
  4207. }
  4208. if (output_event) {
  4209. err = perf_event_set_output(event, output_event);
  4210. if (err)
  4211. goto err_free_put_context;
  4212. }
  4213. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
  4214. if (IS_ERR(event_file)) {
  4215. err = PTR_ERR(event_file);
  4216. goto err_free_put_context;
  4217. }
  4218. event->filp = event_file;
  4219. WARN_ON_ONCE(ctx->parent_ctx);
  4220. mutex_lock(&ctx->mutex);
  4221. perf_install_in_context(ctx, event, cpu);
  4222. ++ctx->generation;
  4223. mutex_unlock(&ctx->mutex);
  4224. event->owner = current;
  4225. get_task_struct(current);
  4226. mutex_lock(&current->perf_event_mutex);
  4227. list_add_tail(&event->owner_entry, &current->perf_event_list);
  4228. mutex_unlock(&current->perf_event_mutex);
  4229. /*
  4230. * Drop the reference on the group_event after placing the
  4231. * new event on the sibling_list. This ensures destruction
  4232. * of the group leader will find the pointer to itself in
  4233. * perf_group_detach().
  4234. */
  4235. fput_light(group_file, fput_needed);
  4236. fd_install(event_fd, event_file);
  4237. return event_fd;
  4238. err_free_put_context:
  4239. free_event(event);
  4240. err_put_context:
  4241. fput_light(group_file, fput_needed);
  4242. put_ctx(ctx);
  4243. err_fd:
  4244. put_unused_fd(event_fd);
  4245. return err;
  4246. }
  4247. /**
  4248. * perf_event_create_kernel_counter
  4249. *
  4250. * @attr: attributes of the counter to create
  4251. * @cpu: cpu in which the counter is bound
  4252. * @pid: task to profile
  4253. */
  4254. struct perf_event *
  4255. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  4256. pid_t pid,
  4257. perf_overflow_handler_t overflow_handler)
  4258. {
  4259. struct perf_event *event;
  4260. struct perf_event_context *ctx;
  4261. int err;
  4262. /*
  4263. * Get the target context (task or percpu):
  4264. */
  4265. ctx = find_get_context(pid, cpu);
  4266. if (IS_ERR(ctx)) {
  4267. err = PTR_ERR(ctx);
  4268. goto err_exit;
  4269. }
  4270. event = perf_event_alloc(attr, cpu, ctx, NULL,
  4271. NULL, overflow_handler, GFP_KERNEL);
  4272. if (IS_ERR(event)) {
  4273. err = PTR_ERR(event);
  4274. goto err_put_context;
  4275. }
  4276. event->filp = NULL;
  4277. WARN_ON_ONCE(ctx->parent_ctx);
  4278. mutex_lock(&ctx->mutex);
  4279. perf_install_in_context(ctx, event, cpu);
  4280. ++ctx->generation;
  4281. mutex_unlock(&ctx->mutex);
  4282. event->owner = current;
  4283. get_task_struct(current);
  4284. mutex_lock(&current->perf_event_mutex);
  4285. list_add_tail(&event->owner_entry, &current->perf_event_list);
  4286. mutex_unlock(&current->perf_event_mutex);
  4287. return event;
  4288. err_put_context:
  4289. put_ctx(ctx);
  4290. err_exit:
  4291. return ERR_PTR(err);
  4292. }
  4293. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  4294. /*
  4295. * inherit a event from parent task to child task:
  4296. */
  4297. static struct perf_event *
  4298. inherit_event(struct perf_event *parent_event,
  4299. struct task_struct *parent,
  4300. struct perf_event_context *parent_ctx,
  4301. struct task_struct *child,
  4302. struct perf_event *group_leader,
  4303. struct perf_event_context *child_ctx)
  4304. {
  4305. struct perf_event *child_event;
  4306. /*
  4307. * Instead of creating recursive hierarchies of events,
  4308. * we link inherited events back to the original parent,
  4309. * which has a filp for sure, which we use as the reference
  4310. * count:
  4311. */
  4312. if (parent_event->parent)
  4313. parent_event = parent_event->parent;
  4314. child_event = perf_event_alloc(&parent_event->attr,
  4315. parent_event->cpu, child_ctx,
  4316. group_leader, parent_event,
  4317. NULL, GFP_KERNEL);
  4318. if (IS_ERR(child_event))
  4319. return child_event;
  4320. get_ctx(child_ctx);
  4321. /*
  4322. * Make the child state follow the state of the parent event,
  4323. * not its attr.disabled bit. We hold the parent's mutex,
  4324. * so we won't race with perf_event_{en, dis}able_family.
  4325. */
  4326. if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  4327. child_event->state = PERF_EVENT_STATE_INACTIVE;
  4328. else
  4329. child_event->state = PERF_EVENT_STATE_OFF;
  4330. if (parent_event->attr.freq) {
  4331. u64 sample_period = parent_event->hw.sample_period;
  4332. struct hw_perf_event *hwc = &child_event->hw;
  4333. hwc->sample_period = sample_period;
  4334. hwc->last_period = sample_period;
  4335. local64_set(&hwc->period_left, sample_period);
  4336. }
  4337. child_event->overflow_handler = parent_event->overflow_handler;
  4338. /*
  4339. * Link it up in the child's context:
  4340. */
  4341. add_event_to_ctx(child_event, child_ctx);
  4342. /*
  4343. * Get a reference to the parent filp - we will fput it
  4344. * when the child event exits. This is safe to do because
  4345. * we are in the parent and we know that the filp still
  4346. * exists and has a nonzero count:
  4347. */
  4348. atomic_long_inc(&parent_event->filp->f_count);
  4349. /*
  4350. * Link this into the parent event's child list
  4351. */
  4352. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4353. mutex_lock(&parent_event->child_mutex);
  4354. list_add_tail(&child_event->child_list, &parent_event->child_list);
  4355. mutex_unlock(&parent_event->child_mutex);
  4356. return child_event;
  4357. }
  4358. static int inherit_group(struct perf_event *parent_event,
  4359. struct task_struct *parent,
  4360. struct perf_event_context *parent_ctx,
  4361. struct task_struct *child,
  4362. struct perf_event_context *child_ctx)
  4363. {
  4364. struct perf_event *leader;
  4365. struct perf_event *sub;
  4366. struct perf_event *child_ctr;
  4367. leader = inherit_event(parent_event, parent, parent_ctx,
  4368. child, NULL, child_ctx);
  4369. if (IS_ERR(leader))
  4370. return PTR_ERR(leader);
  4371. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  4372. child_ctr = inherit_event(sub, parent, parent_ctx,
  4373. child, leader, child_ctx);
  4374. if (IS_ERR(child_ctr))
  4375. return PTR_ERR(child_ctr);
  4376. }
  4377. return 0;
  4378. }
  4379. static void sync_child_event(struct perf_event *child_event,
  4380. struct task_struct *child)
  4381. {
  4382. struct perf_event *parent_event = child_event->parent;
  4383. u64 child_val;
  4384. if (child_event->attr.inherit_stat)
  4385. perf_event_read_event(child_event, child);
  4386. child_val = perf_event_count(child_event);
  4387. /*
  4388. * Add back the child's count to the parent's count:
  4389. */
  4390. atomic64_add(child_val, &parent_event->child_count);
  4391. atomic64_add(child_event->total_time_enabled,
  4392. &parent_event->child_total_time_enabled);
  4393. atomic64_add(child_event->total_time_running,
  4394. &parent_event->child_total_time_running);
  4395. /*
  4396. * Remove this event from the parent's list
  4397. */
  4398. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4399. mutex_lock(&parent_event->child_mutex);
  4400. list_del_init(&child_event->child_list);
  4401. mutex_unlock(&parent_event->child_mutex);
  4402. /*
  4403. * Release the parent event, if this was the last
  4404. * reference to it.
  4405. */
  4406. fput(parent_event->filp);
  4407. }
  4408. static void
  4409. __perf_event_exit_task(struct perf_event *child_event,
  4410. struct perf_event_context *child_ctx,
  4411. struct task_struct *child)
  4412. {
  4413. struct perf_event *parent_event;
  4414. perf_event_remove_from_context(child_event);
  4415. parent_event = child_event->parent;
  4416. /*
  4417. * It can happen that parent exits first, and has events
  4418. * that are still around due to the child reference. These
  4419. * events need to be zapped - but otherwise linger.
  4420. */
  4421. if (parent_event) {
  4422. sync_child_event(child_event, child);
  4423. free_event(child_event);
  4424. }
  4425. }
  4426. /*
  4427. * When a child task exits, feed back event values to parent events.
  4428. */
  4429. void perf_event_exit_task(struct task_struct *child)
  4430. {
  4431. struct perf_event *child_event, *tmp;
  4432. struct perf_event_context *child_ctx;
  4433. unsigned long flags;
  4434. if (likely(!child->perf_event_ctxp)) {
  4435. perf_event_task(child, NULL, 0);
  4436. return;
  4437. }
  4438. local_irq_save(flags);
  4439. /*
  4440. * We can't reschedule here because interrupts are disabled,
  4441. * and either child is current or it is a task that can't be
  4442. * scheduled, so we are now safe from rescheduling changing
  4443. * our context.
  4444. */
  4445. child_ctx = child->perf_event_ctxp;
  4446. __perf_event_task_sched_out(child_ctx);
  4447. /*
  4448. * Take the context lock here so that if find_get_context is
  4449. * reading child->perf_event_ctxp, we wait until it has
  4450. * incremented the context's refcount before we do put_ctx below.
  4451. */
  4452. raw_spin_lock(&child_ctx->lock);
  4453. child->perf_event_ctxp = NULL;
  4454. /*
  4455. * If this context is a clone; unclone it so it can't get
  4456. * swapped to another process while we're removing all
  4457. * the events from it.
  4458. */
  4459. unclone_ctx(child_ctx);
  4460. update_context_time(child_ctx);
  4461. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  4462. /*
  4463. * Report the task dead after unscheduling the events so that we
  4464. * won't get any samples after PERF_RECORD_EXIT. We can however still
  4465. * get a few PERF_RECORD_READ events.
  4466. */
  4467. perf_event_task(child, child_ctx, 0);
  4468. /*
  4469. * We can recurse on the same lock type through:
  4470. *
  4471. * __perf_event_exit_task()
  4472. * sync_child_event()
  4473. * fput(parent_event->filp)
  4474. * perf_release()
  4475. * mutex_lock(&ctx->mutex)
  4476. *
  4477. * But since its the parent context it won't be the same instance.
  4478. */
  4479. mutex_lock(&child_ctx->mutex);
  4480. again:
  4481. list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
  4482. group_entry)
  4483. __perf_event_exit_task(child_event, child_ctx, child);
  4484. list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
  4485. group_entry)
  4486. __perf_event_exit_task(child_event, child_ctx, child);
  4487. /*
  4488. * If the last event was a group event, it will have appended all
  4489. * its siblings to the list, but we obtained 'tmp' before that which
  4490. * will still point to the list head terminating the iteration.
  4491. */
  4492. if (!list_empty(&child_ctx->pinned_groups) ||
  4493. !list_empty(&child_ctx->flexible_groups))
  4494. goto again;
  4495. mutex_unlock(&child_ctx->mutex);
  4496. put_ctx(child_ctx);
  4497. }
  4498. static void perf_free_event(struct perf_event *event,
  4499. struct perf_event_context *ctx)
  4500. {
  4501. struct perf_event *parent = event->parent;
  4502. if (WARN_ON_ONCE(!parent))
  4503. return;
  4504. mutex_lock(&parent->child_mutex);
  4505. list_del_init(&event->child_list);
  4506. mutex_unlock(&parent->child_mutex);
  4507. fput(parent->filp);
  4508. perf_group_detach(event);
  4509. list_del_event(event, ctx);
  4510. free_event(event);
  4511. }
  4512. /*
  4513. * free an unexposed, unused context as created by inheritance by
  4514. * init_task below, used by fork() in case of fail.
  4515. */
  4516. void perf_event_free_task(struct task_struct *task)
  4517. {
  4518. struct perf_event_context *ctx = task->perf_event_ctxp;
  4519. struct perf_event *event, *tmp;
  4520. if (!ctx)
  4521. return;
  4522. mutex_lock(&ctx->mutex);
  4523. again:
  4524. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  4525. perf_free_event(event, ctx);
  4526. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  4527. group_entry)
  4528. perf_free_event(event, ctx);
  4529. if (!list_empty(&ctx->pinned_groups) ||
  4530. !list_empty(&ctx->flexible_groups))
  4531. goto again;
  4532. mutex_unlock(&ctx->mutex);
  4533. put_ctx(ctx);
  4534. }
  4535. static int
  4536. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  4537. struct perf_event_context *parent_ctx,
  4538. struct task_struct *child,
  4539. int *inherited_all)
  4540. {
  4541. int ret;
  4542. struct perf_event_context *child_ctx = child->perf_event_ctxp;
  4543. if (!event->attr.inherit) {
  4544. *inherited_all = 0;
  4545. return 0;
  4546. }
  4547. if (!child_ctx) {
  4548. /*
  4549. * This is executed from the parent task context, so
  4550. * inherit events that have been marked for cloning.
  4551. * First allocate and initialize a context for the
  4552. * child.
  4553. */
  4554. child_ctx = kzalloc(sizeof(struct perf_event_context),
  4555. GFP_KERNEL);
  4556. if (!child_ctx)
  4557. return -ENOMEM;
  4558. __perf_event_init_context(child_ctx, child);
  4559. child->perf_event_ctxp = child_ctx;
  4560. get_task_struct(child);
  4561. }
  4562. ret = inherit_group(event, parent, parent_ctx,
  4563. child, child_ctx);
  4564. if (ret)
  4565. *inherited_all = 0;
  4566. return ret;
  4567. }
  4568. /*
  4569. * Initialize the perf_event context in task_struct
  4570. */
  4571. int perf_event_init_task(struct task_struct *child)
  4572. {
  4573. struct perf_event_context *child_ctx, *parent_ctx;
  4574. struct perf_event_context *cloned_ctx;
  4575. struct perf_event *event;
  4576. struct task_struct *parent = current;
  4577. int inherited_all = 1;
  4578. int ret = 0;
  4579. child->perf_event_ctxp = NULL;
  4580. mutex_init(&child->perf_event_mutex);
  4581. INIT_LIST_HEAD(&child->perf_event_list);
  4582. if (likely(!parent->perf_event_ctxp))
  4583. return 0;
  4584. /*
  4585. * If the parent's context is a clone, pin it so it won't get
  4586. * swapped under us.
  4587. */
  4588. parent_ctx = perf_pin_task_context(parent);
  4589. /*
  4590. * No need to check if parent_ctx != NULL here; since we saw
  4591. * it non-NULL earlier, the only reason for it to become NULL
  4592. * is if we exit, and since we're currently in the middle of
  4593. * a fork we can't be exiting at the same time.
  4594. */
  4595. /*
  4596. * Lock the parent list. No need to lock the child - not PID
  4597. * hashed yet and not running, so nobody can access it.
  4598. */
  4599. mutex_lock(&parent_ctx->mutex);
  4600. /*
  4601. * We dont have to disable NMIs - we are only looking at
  4602. * the list, not manipulating it:
  4603. */
  4604. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  4605. ret = inherit_task_group(event, parent, parent_ctx, child,
  4606. &inherited_all);
  4607. if (ret)
  4608. break;
  4609. }
  4610. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  4611. ret = inherit_task_group(event, parent, parent_ctx, child,
  4612. &inherited_all);
  4613. if (ret)
  4614. break;
  4615. }
  4616. child_ctx = child->perf_event_ctxp;
  4617. if (child_ctx && inherited_all) {
  4618. /*
  4619. * Mark the child context as a clone of the parent
  4620. * context, or of whatever the parent is a clone of.
  4621. * Note that if the parent is a clone, it could get
  4622. * uncloned at any point, but that doesn't matter
  4623. * because the list of events and the generation
  4624. * count can't have changed since we took the mutex.
  4625. */
  4626. cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
  4627. if (cloned_ctx) {
  4628. child_ctx->parent_ctx = cloned_ctx;
  4629. child_ctx->parent_gen = parent_ctx->parent_gen;
  4630. } else {
  4631. child_ctx->parent_ctx = parent_ctx;
  4632. child_ctx->parent_gen = parent_ctx->generation;
  4633. }
  4634. get_ctx(child_ctx->parent_ctx);
  4635. }
  4636. mutex_unlock(&parent_ctx->mutex);
  4637. perf_unpin_context(parent_ctx);
  4638. return ret;
  4639. }
  4640. static void __init perf_event_init_all_cpus(void)
  4641. {
  4642. int cpu;
  4643. struct perf_cpu_context *cpuctx;
  4644. for_each_possible_cpu(cpu) {
  4645. cpuctx = &per_cpu(perf_cpu_context, cpu);
  4646. mutex_init(&cpuctx->hlist_mutex);
  4647. __perf_event_init_context(&cpuctx->ctx, NULL);
  4648. }
  4649. }
  4650. static void __cpuinit perf_event_init_cpu(int cpu)
  4651. {
  4652. struct perf_cpu_context *cpuctx;
  4653. cpuctx = &per_cpu(perf_cpu_context, cpu);
  4654. spin_lock(&perf_resource_lock);
  4655. cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
  4656. spin_unlock(&perf_resource_lock);
  4657. mutex_lock(&cpuctx->hlist_mutex);
  4658. if (cpuctx->hlist_refcount > 0) {
  4659. struct swevent_hlist *hlist;
  4660. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  4661. WARN_ON_ONCE(!hlist);
  4662. rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
  4663. }
  4664. mutex_unlock(&cpuctx->hlist_mutex);
  4665. }
  4666. #ifdef CONFIG_HOTPLUG_CPU
  4667. static void __perf_event_exit_cpu(void *info)
  4668. {
  4669. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  4670. struct perf_event_context *ctx = &cpuctx->ctx;
  4671. struct perf_event *event, *tmp;
  4672. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  4673. __perf_event_remove_from_context(event);
  4674. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
  4675. __perf_event_remove_from_context(event);
  4676. }
  4677. static void perf_event_exit_cpu(int cpu)
  4678. {
  4679. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  4680. struct perf_event_context *ctx = &cpuctx->ctx;
  4681. mutex_lock(&cpuctx->hlist_mutex);
  4682. swevent_hlist_release(cpuctx);
  4683. mutex_unlock(&cpuctx->hlist_mutex);
  4684. mutex_lock(&ctx->mutex);
  4685. smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
  4686. mutex_unlock(&ctx->mutex);
  4687. }
  4688. #else
  4689. static inline void perf_event_exit_cpu(int cpu) { }
  4690. #endif
  4691. static int __cpuinit
  4692. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  4693. {
  4694. unsigned int cpu = (long)hcpu;
  4695. switch (action) {
  4696. case CPU_UP_PREPARE:
  4697. case CPU_UP_PREPARE_FROZEN:
  4698. perf_event_init_cpu(cpu);
  4699. break;
  4700. case CPU_DOWN_PREPARE:
  4701. case CPU_DOWN_PREPARE_FROZEN:
  4702. perf_event_exit_cpu(cpu);
  4703. break;
  4704. default:
  4705. break;
  4706. }
  4707. return NOTIFY_OK;
  4708. }
  4709. /*
  4710. * This has to have a higher priority than migration_notifier in sched.c.
  4711. */
  4712. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  4713. .notifier_call = perf_cpu_notify,
  4714. .priority = 20,
  4715. };
  4716. void __init perf_event_init(void)
  4717. {
  4718. perf_event_init_all_cpus();
  4719. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  4720. (void *)(long)smp_processor_id());
  4721. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
  4722. (void *)(long)smp_processor_id());
  4723. register_cpu_notifier(&perf_cpu_nb);
  4724. }
  4725. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class,
  4726. struct sysdev_class_attribute *attr,
  4727. char *buf)
  4728. {
  4729. return sprintf(buf, "%d\n", perf_reserved_percpu);
  4730. }
  4731. static ssize_t
  4732. perf_set_reserve_percpu(struct sysdev_class *class,
  4733. struct sysdev_class_attribute *attr,
  4734. const char *buf,
  4735. size_t count)
  4736. {
  4737. struct perf_cpu_context *cpuctx;
  4738. unsigned long val;
  4739. int err, cpu, mpt;
  4740. err = strict_strtoul(buf, 10, &val);
  4741. if (err)
  4742. return err;
  4743. if (val > perf_max_events)
  4744. return -EINVAL;
  4745. spin_lock(&perf_resource_lock);
  4746. perf_reserved_percpu = val;
  4747. for_each_online_cpu(cpu) {
  4748. cpuctx = &per_cpu(perf_cpu_context, cpu);
  4749. raw_spin_lock_irq(&cpuctx->ctx.lock);
  4750. mpt = min(perf_max_events - cpuctx->ctx.nr_events,
  4751. perf_max_events - perf_reserved_percpu);
  4752. cpuctx->max_pertask = mpt;
  4753. raw_spin_unlock_irq(&cpuctx->ctx.lock);
  4754. }
  4755. spin_unlock(&perf_resource_lock);
  4756. return count;
  4757. }
  4758. static ssize_t perf_show_overcommit(struct sysdev_class *class,
  4759. struct sysdev_class_attribute *attr,
  4760. char *buf)
  4761. {
  4762. return sprintf(buf, "%d\n", perf_overcommit);
  4763. }
  4764. static ssize_t
  4765. perf_set_overcommit(struct sysdev_class *class,
  4766. struct sysdev_class_attribute *attr,
  4767. const char *buf, size_t count)
  4768. {
  4769. unsigned long val;
  4770. int err;
  4771. err = strict_strtoul(buf, 10, &val);
  4772. if (err)
  4773. return err;
  4774. if (val > 1)
  4775. return -EINVAL;
  4776. spin_lock(&perf_resource_lock);
  4777. perf_overcommit = val;
  4778. spin_unlock(&perf_resource_lock);
  4779. return count;
  4780. }
  4781. static SYSDEV_CLASS_ATTR(
  4782. reserve_percpu,
  4783. 0644,
  4784. perf_show_reserve_percpu,
  4785. perf_set_reserve_percpu
  4786. );
  4787. static SYSDEV_CLASS_ATTR(
  4788. overcommit,
  4789. 0644,
  4790. perf_show_overcommit,
  4791. perf_set_overcommit
  4792. );
  4793. static struct attribute *perfclass_attrs[] = {
  4794. &attr_reserve_percpu.attr,
  4795. &attr_overcommit.attr,
  4796. NULL
  4797. };
  4798. static struct attribute_group perfclass_attr_group = {
  4799. .attrs = perfclass_attrs,
  4800. .name = "perf_events",
  4801. };
  4802. static int __init perf_event_sysfs_init(void)
  4803. {
  4804. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  4805. &perfclass_attr_group);
  4806. }
  4807. device_initcall(perf_event_sysfs_init);