perf_counter.c 106 KB

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