perf_counter.c 110 KB

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