perf_event.c 128 KB

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