perf_event.c 126 KB

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