perf_event.c 134 KB

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