perf_event.c 126 KB

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