perf_counter.c 113 KB

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