perf_event.c 126 KB

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