perf_counter.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713
  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/ptrace.h>
  19. #include <linux/percpu.h>
  20. #include <linux/vmstat.h>
  21. #include <linux/hardirq.h>
  22. #include <linux/rculist.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/anon_inodes.h>
  26. #include <linux/kernel_stat.h>
  27. #include <linux/perf_counter.h>
  28. #include <linux/dcache.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_tracking __read_mostly;
  39. static atomic_t nr_munmap_tracking __read_mostly;
  40. static atomic_t nr_comm_tracking __read_mostly;
  41. int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
  42. int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
  43. /*
  44. * Lock for (sysadmin-configurable) counter reservations:
  45. */
  46. static DEFINE_SPINLOCK(perf_resource_lock);
  47. /*
  48. * Architecture provided APIs - weak aliases:
  49. */
  50. extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  51. {
  52. return NULL;
  53. }
  54. void __weak hw_perf_disable(void) { barrier(); }
  55. void __weak hw_perf_enable(void) { barrier(); }
  56. void __weak hw_perf_counter_setup(int cpu) { barrier(); }
  57. int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
  58. struct perf_cpu_context *cpuctx,
  59. struct perf_counter_context *ctx, int cpu)
  60. {
  61. return 0;
  62. }
  63. void __weak perf_counter_print_debug(void) { }
  64. static DEFINE_PER_CPU(int, disable_count);
  65. void __perf_disable(void)
  66. {
  67. __get_cpu_var(disable_count)++;
  68. }
  69. bool __perf_enable(void)
  70. {
  71. return !--__get_cpu_var(disable_count);
  72. }
  73. void perf_disable(void)
  74. {
  75. __perf_disable();
  76. hw_perf_disable();
  77. }
  78. void perf_enable(void)
  79. {
  80. if (__perf_enable())
  81. hw_perf_enable();
  82. }
  83. static void get_ctx(struct perf_counter_context *ctx)
  84. {
  85. atomic_inc(&ctx->refcount);
  86. }
  87. static void put_ctx(struct perf_counter_context *ctx)
  88. {
  89. if (atomic_dec_and_test(&ctx->refcount)) {
  90. if (ctx->parent_ctx)
  91. put_ctx(ctx->parent_ctx);
  92. kfree(ctx);
  93. }
  94. }
  95. /*
  96. * Add a counter from the lists for its context.
  97. * Must be called with ctx->mutex and ctx->lock held.
  98. */
  99. static void
  100. list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  101. {
  102. struct perf_counter *group_leader = counter->group_leader;
  103. /*
  104. * Depending on whether it is a standalone or sibling counter,
  105. * add it straight to the context's counter list, or to the group
  106. * leader's sibling list:
  107. */
  108. if (group_leader == counter)
  109. list_add_tail(&counter->list_entry, &ctx->counter_list);
  110. else {
  111. list_add_tail(&counter->list_entry, &group_leader->sibling_list);
  112. group_leader->nr_siblings++;
  113. }
  114. list_add_rcu(&counter->event_entry, &ctx->event_list);
  115. ctx->nr_counters++;
  116. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  117. ctx->nr_enabled++;
  118. }
  119. /*
  120. * Remove a counter from the lists for its context.
  121. * Must be called with ctx->mutex and ctx->lock held.
  122. */
  123. static void
  124. list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  125. {
  126. struct perf_counter *sibling, *tmp;
  127. if (list_empty(&counter->list_entry))
  128. return;
  129. ctx->nr_counters--;
  130. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  131. ctx->nr_enabled--;
  132. list_del_init(&counter->list_entry);
  133. list_del_rcu(&counter->event_entry);
  134. if (counter->group_leader != counter)
  135. counter->group_leader->nr_siblings--;
  136. /*
  137. * If this was a group counter with sibling counters then
  138. * upgrade the siblings to singleton counters by adding them
  139. * to the context list directly:
  140. */
  141. list_for_each_entry_safe(sibling, tmp,
  142. &counter->sibling_list, list_entry) {
  143. list_move_tail(&sibling->list_entry, &ctx->counter_list);
  144. sibling->group_leader = sibling;
  145. }
  146. }
  147. static void
  148. counter_sched_out(struct perf_counter *counter,
  149. struct perf_cpu_context *cpuctx,
  150. struct perf_counter_context *ctx)
  151. {
  152. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  153. return;
  154. counter->state = PERF_COUNTER_STATE_INACTIVE;
  155. counter->tstamp_stopped = ctx->time;
  156. counter->pmu->disable(counter);
  157. counter->oncpu = -1;
  158. if (!is_software_counter(counter))
  159. cpuctx->active_oncpu--;
  160. ctx->nr_active--;
  161. if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
  162. cpuctx->exclusive = 0;
  163. }
  164. static void
  165. group_sched_out(struct perf_counter *group_counter,
  166. struct perf_cpu_context *cpuctx,
  167. struct perf_counter_context *ctx)
  168. {
  169. struct perf_counter *counter;
  170. if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
  171. return;
  172. counter_sched_out(group_counter, cpuctx, ctx);
  173. /*
  174. * Schedule out siblings (if any):
  175. */
  176. list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
  177. counter_sched_out(counter, cpuctx, ctx);
  178. if (group_counter->hw_event.exclusive)
  179. cpuctx->exclusive = 0;
  180. }
  181. /*
  182. * Mark this context as not being a clone of another.
  183. * Called when counters are added to or removed from this context.
  184. * We also increment our generation number so that anything that
  185. * was cloned from this context before this will not match anything
  186. * cloned from this context after this.
  187. */
  188. static void unclone_ctx(struct perf_counter_context *ctx)
  189. {
  190. ++ctx->generation;
  191. if (!ctx->parent_ctx)
  192. return;
  193. put_ctx(ctx->parent_ctx);
  194. ctx->parent_ctx = NULL;
  195. }
  196. /*
  197. * Cross CPU call to remove a performance counter
  198. *
  199. * We disable the counter on the hardware level first. After that we
  200. * remove it from the context list.
  201. */
  202. static void __perf_counter_remove_from_context(void *info)
  203. {
  204. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  205. struct perf_counter *counter = info;
  206. struct perf_counter_context *ctx = counter->ctx;
  207. unsigned long flags;
  208. /*
  209. * If this is a task context, we need to check whether it is
  210. * the current task context of this cpu. If not it has been
  211. * scheduled out before the smp call arrived.
  212. */
  213. if (ctx->task && cpuctx->task_ctx != ctx)
  214. return;
  215. spin_lock_irqsave(&ctx->lock, flags);
  216. /*
  217. * Protect the list operation against NMI by disabling the
  218. * counters on a global level.
  219. */
  220. perf_disable();
  221. counter_sched_out(counter, cpuctx, ctx);
  222. list_del_counter(counter, ctx);
  223. if (!ctx->task) {
  224. /*
  225. * Allow more per task counters with respect to the
  226. * reservation:
  227. */
  228. cpuctx->max_pertask =
  229. min(perf_max_counters - ctx->nr_counters,
  230. perf_max_counters - perf_reserved_percpu);
  231. }
  232. perf_enable();
  233. spin_unlock_irqrestore(&ctx->lock, flags);
  234. }
  235. /*
  236. * Remove the counter from a task's (or a CPU's) list of counters.
  237. *
  238. * Must be called with ctx->mutex held.
  239. *
  240. * CPU counters are removed with a smp call. For task counters we only
  241. * call when the task is on a CPU.
  242. */
  243. static void perf_counter_remove_from_context(struct perf_counter *counter)
  244. {
  245. struct perf_counter_context *ctx = counter->ctx;
  246. struct task_struct *task = ctx->task;
  247. unclone_ctx(ctx);
  248. if (!task) {
  249. /*
  250. * Per cpu counters are removed via an smp call and
  251. * the removal is always sucessful.
  252. */
  253. smp_call_function_single(counter->cpu,
  254. __perf_counter_remove_from_context,
  255. counter, 1);
  256. return;
  257. }
  258. retry:
  259. task_oncpu_function_call(task, __perf_counter_remove_from_context,
  260. counter);
  261. spin_lock_irq(&ctx->lock);
  262. /*
  263. * If the context is active we need to retry the smp call.
  264. */
  265. if (ctx->nr_active && !list_empty(&counter->list_entry)) {
  266. spin_unlock_irq(&ctx->lock);
  267. goto retry;
  268. }
  269. /*
  270. * The lock prevents that this context is scheduled in so we
  271. * can remove the counter safely, if the call above did not
  272. * succeed.
  273. */
  274. if (!list_empty(&counter->list_entry)) {
  275. list_del_counter(counter, ctx);
  276. }
  277. spin_unlock_irq(&ctx->lock);
  278. }
  279. static inline u64 perf_clock(void)
  280. {
  281. return cpu_clock(smp_processor_id());
  282. }
  283. /*
  284. * Update the record of the current time in a context.
  285. */
  286. static void update_context_time(struct perf_counter_context *ctx)
  287. {
  288. u64 now = perf_clock();
  289. ctx->time += now - ctx->timestamp;
  290. ctx->timestamp = now;
  291. }
  292. /*
  293. * Update the total_time_enabled and total_time_running fields for a counter.
  294. */
  295. static void update_counter_times(struct perf_counter *counter)
  296. {
  297. struct perf_counter_context *ctx = counter->ctx;
  298. u64 run_end;
  299. if (counter->state < PERF_COUNTER_STATE_INACTIVE)
  300. return;
  301. counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
  302. if (counter->state == PERF_COUNTER_STATE_INACTIVE)
  303. run_end = counter->tstamp_stopped;
  304. else
  305. run_end = ctx->time;
  306. counter->total_time_running = run_end - counter->tstamp_running;
  307. }
  308. /*
  309. * Update total_time_enabled and total_time_running for all counters in a group.
  310. */
  311. static void update_group_times(struct perf_counter *leader)
  312. {
  313. struct perf_counter *counter;
  314. update_counter_times(leader);
  315. list_for_each_entry(counter, &leader->sibling_list, list_entry)
  316. update_counter_times(counter);
  317. }
  318. /*
  319. * Cross CPU call to disable a performance counter
  320. */
  321. static void __perf_counter_disable(void *info)
  322. {
  323. struct perf_counter *counter = info;
  324. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  325. struct perf_counter_context *ctx = counter->ctx;
  326. unsigned long flags;
  327. /*
  328. * If this is a per-task counter, need to check whether this
  329. * counter's task is the current task on this cpu.
  330. */
  331. if (ctx->task && cpuctx->task_ctx != ctx)
  332. return;
  333. spin_lock_irqsave(&ctx->lock, flags);
  334. /*
  335. * If the counter is on, turn it off.
  336. * If it is in error state, leave it in error state.
  337. */
  338. if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
  339. update_context_time(ctx);
  340. update_counter_times(counter);
  341. if (counter == counter->group_leader)
  342. group_sched_out(counter, cpuctx, ctx);
  343. else
  344. counter_sched_out(counter, cpuctx, ctx);
  345. counter->state = PERF_COUNTER_STATE_OFF;
  346. ctx->nr_enabled--;
  347. }
  348. spin_unlock_irqrestore(&ctx->lock, flags);
  349. }
  350. /*
  351. * Disable a counter.
  352. */
  353. static void perf_counter_disable(struct perf_counter *counter)
  354. {
  355. struct perf_counter_context *ctx = counter->ctx;
  356. struct task_struct *task = ctx->task;
  357. if (!task) {
  358. /*
  359. * Disable the counter on the cpu that it's on
  360. */
  361. smp_call_function_single(counter->cpu, __perf_counter_disable,
  362. counter, 1);
  363. return;
  364. }
  365. retry:
  366. task_oncpu_function_call(task, __perf_counter_disable, counter);
  367. spin_lock_irq(&ctx->lock);
  368. /*
  369. * If the counter is still active, we need to retry the cross-call.
  370. */
  371. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  372. spin_unlock_irq(&ctx->lock);
  373. goto retry;
  374. }
  375. /*
  376. * Since we have the lock this context can't be scheduled
  377. * in, so we can change the state safely.
  378. */
  379. if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
  380. update_counter_times(counter);
  381. counter->state = PERF_COUNTER_STATE_OFF;
  382. ctx->nr_enabled--;
  383. }
  384. spin_unlock_irq(&ctx->lock);
  385. }
  386. static int
  387. counter_sched_in(struct perf_counter *counter,
  388. struct perf_cpu_context *cpuctx,
  389. struct perf_counter_context *ctx,
  390. int cpu)
  391. {
  392. if (counter->state <= PERF_COUNTER_STATE_OFF)
  393. return 0;
  394. counter->state = PERF_COUNTER_STATE_ACTIVE;
  395. counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
  396. /*
  397. * The new state must be visible before we turn it on in the hardware:
  398. */
  399. smp_wmb();
  400. if (counter->pmu->enable(counter)) {
  401. counter->state = PERF_COUNTER_STATE_INACTIVE;
  402. counter->oncpu = -1;
  403. return -EAGAIN;
  404. }
  405. counter->tstamp_running += ctx->time - counter->tstamp_stopped;
  406. if (!is_software_counter(counter))
  407. cpuctx->active_oncpu++;
  408. ctx->nr_active++;
  409. if (counter->hw_event.exclusive)
  410. cpuctx->exclusive = 1;
  411. return 0;
  412. }
  413. static int
  414. group_sched_in(struct perf_counter *group_counter,
  415. struct perf_cpu_context *cpuctx,
  416. struct perf_counter_context *ctx,
  417. int cpu)
  418. {
  419. struct perf_counter *counter, *partial_group;
  420. int ret;
  421. if (group_counter->state == PERF_COUNTER_STATE_OFF)
  422. return 0;
  423. ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
  424. if (ret)
  425. return ret < 0 ? ret : 0;
  426. group_counter->prev_state = group_counter->state;
  427. if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
  428. return -EAGAIN;
  429. /*
  430. * Schedule in siblings as one group (if any):
  431. */
  432. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  433. counter->prev_state = counter->state;
  434. if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
  435. partial_group = counter;
  436. goto group_error;
  437. }
  438. }
  439. return 0;
  440. group_error:
  441. /*
  442. * Groups can be scheduled in as one unit only, so undo any
  443. * partial group before returning:
  444. */
  445. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  446. if (counter == partial_group)
  447. break;
  448. counter_sched_out(counter, cpuctx, ctx);
  449. }
  450. counter_sched_out(group_counter, cpuctx, ctx);
  451. return -EAGAIN;
  452. }
  453. /*
  454. * Return 1 for a group consisting entirely of software counters,
  455. * 0 if the group contains any hardware counters.
  456. */
  457. static int is_software_only_group(struct perf_counter *leader)
  458. {
  459. struct perf_counter *counter;
  460. if (!is_software_counter(leader))
  461. return 0;
  462. list_for_each_entry(counter, &leader->sibling_list, list_entry)
  463. if (!is_software_counter(counter))
  464. return 0;
  465. return 1;
  466. }
  467. /*
  468. * Work out whether we can put this counter group on the CPU now.
  469. */
  470. static int group_can_go_on(struct perf_counter *counter,
  471. struct perf_cpu_context *cpuctx,
  472. int can_add_hw)
  473. {
  474. /*
  475. * Groups consisting entirely of software counters can always go on.
  476. */
  477. if (is_software_only_group(counter))
  478. return 1;
  479. /*
  480. * If an exclusive group is already on, no other hardware
  481. * counters can go on.
  482. */
  483. if (cpuctx->exclusive)
  484. return 0;
  485. /*
  486. * If this group is exclusive and there are already
  487. * counters on the CPU, it can't go on.
  488. */
  489. if (counter->hw_event.exclusive && cpuctx->active_oncpu)
  490. return 0;
  491. /*
  492. * Otherwise, try to add it if all previous groups were able
  493. * to go on.
  494. */
  495. return can_add_hw;
  496. }
  497. static void add_counter_to_ctx(struct perf_counter *counter,
  498. struct perf_counter_context *ctx)
  499. {
  500. list_add_counter(counter, ctx);
  501. counter->prev_state = PERF_COUNTER_STATE_OFF;
  502. counter->tstamp_enabled = ctx->time;
  503. counter->tstamp_running = ctx->time;
  504. counter->tstamp_stopped = ctx->time;
  505. }
  506. /*
  507. * Cross CPU call to install and enable a performance counter
  508. *
  509. * Must be called with ctx->mutex held
  510. */
  511. static void __perf_install_in_context(void *info)
  512. {
  513. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  514. struct perf_counter *counter = info;
  515. struct perf_counter_context *ctx = counter->ctx;
  516. struct perf_counter *leader = counter->group_leader;
  517. int cpu = smp_processor_id();
  518. unsigned long flags;
  519. int err;
  520. /*
  521. * If this is a task context, we need to check whether it is
  522. * the current task context of this cpu. If not it has been
  523. * scheduled out before the smp call arrived.
  524. * Or possibly this is the right context but it isn't
  525. * on this cpu because it had no counters.
  526. */
  527. if (ctx->task && cpuctx->task_ctx != ctx) {
  528. if (cpuctx->task_ctx || ctx->task != current)
  529. return;
  530. cpuctx->task_ctx = ctx;
  531. }
  532. spin_lock_irqsave(&ctx->lock, flags);
  533. ctx->is_active = 1;
  534. update_context_time(ctx);
  535. /*
  536. * Protect the list operation against NMI by disabling the
  537. * counters on a global level. NOP for non NMI based counters.
  538. */
  539. perf_disable();
  540. add_counter_to_ctx(counter, ctx);
  541. /*
  542. * Don't put the counter on if it is disabled or if
  543. * it is in a group and the group isn't on.
  544. */
  545. if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
  546. (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
  547. goto unlock;
  548. /*
  549. * An exclusive counter can't go on if there are already active
  550. * hardware counters, and no hardware counter can go on if there
  551. * is already an exclusive counter on.
  552. */
  553. if (!group_can_go_on(counter, cpuctx, 1))
  554. err = -EEXIST;
  555. else
  556. err = counter_sched_in(counter, cpuctx, ctx, cpu);
  557. if (err) {
  558. /*
  559. * This counter couldn't go on. If it is in a group
  560. * then we have to pull the whole group off.
  561. * If the counter group is pinned then put it in error state.
  562. */
  563. if (leader != counter)
  564. group_sched_out(leader, cpuctx, ctx);
  565. if (leader->hw_event.pinned) {
  566. update_group_times(leader);
  567. leader->state = PERF_COUNTER_STATE_ERROR;
  568. }
  569. }
  570. if (!err && !ctx->task && cpuctx->max_pertask)
  571. cpuctx->max_pertask--;
  572. unlock:
  573. perf_enable();
  574. spin_unlock_irqrestore(&ctx->lock, flags);
  575. }
  576. /*
  577. * Attach a performance counter to a context
  578. *
  579. * First we add the counter to the list with the hardware enable bit
  580. * in counter->hw_config cleared.
  581. *
  582. * If the counter is attached to a task which is on a CPU we use a smp
  583. * call to enable it in the task context. The task might have been
  584. * scheduled away, but we check this in the smp call again.
  585. *
  586. * Must be called with ctx->mutex held.
  587. */
  588. static void
  589. perf_install_in_context(struct perf_counter_context *ctx,
  590. struct perf_counter *counter,
  591. int cpu)
  592. {
  593. struct task_struct *task = ctx->task;
  594. if (!task) {
  595. /*
  596. * Per cpu counters are installed via an smp call and
  597. * the install is always sucessful.
  598. */
  599. smp_call_function_single(cpu, __perf_install_in_context,
  600. counter, 1);
  601. return;
  602. }
  603. retry:
  604. task_oncpu_function_call(task, __perf_install_in_context,
  605. counter);
  606. spin_lock_irq(&ctx->lock);
  607. /*
  608. * we need to retry the smp call.
  609. */
  610. if (ctx->is_active && list_empty(&counter->list_entry)) {
  611. spin_unlock_irq(&ctx->lock);
  612. goto retry;
  613. }
  614. /*
  615. * The lock prevents that this context is scheduled in so we
  616. * can add the counter safely, if it the call above did not
  617. * succeed.
  618. */
  619. if (list_empty(&counter->list_entry))
  620. add_counter_to_ctx(counter, ctx);
  621. spin_unlock_irq(&ctx->lock);
  622. }
  623. /*
  624. * Cross CPU call to enable a performance counter
  625. */
  626. static void __perf_counter_enable(void *info)
  627. {
  628. struct perf_counter *counter = info;
  629. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  630. struct perf_counter_context *ctx = counter->ctx;
  631. struct perf_counter *leader = counter->group_leader;
  632. unsigned long flags;
  633. int err;
  634. /*
  635. * If this is a per-task counter, need to check whether this
  636. * counter's task is the current task on this cpu.
  637. */
  638. if (ctx->task && cpuctx->task_ctx != ctx) {
  639. if (cpuctx->task_ctx || ctx->task != current)
  640. return;
  641. cpuctx->task_ctx = ctx;
  642. }
  643. spin_lock_irqsave(&ctx->lock, flags);
  644. ctx->is_active = 1;
  645. update_context_time(ctx);
  646. counter->prev_state = counter->state;
  647. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  648. goto unlock;
  649. counter->state = PERF_COUNTER_STATE_INACTIVE;
  650. counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
  651. ctx->nr_enabled++;
  652. /*
  653. * If the counter is in a group and isn't the group leader,
  654. * then don't put it on unless the group is on.
  655. */
  656. if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
  657. goto unlock;
  658. if (!group_can_go_on(counter, cpuctx, 1)) {
  659. err = -EEXIST;
  660. } else {
  661. perf_disable();
  662. if (counter == leader)
  663. err = group_sched_in(counter, cpuctx, ctx,
  664. smp_processor_id());
  665. else
  666. err = counter_sched_in(counter, cpuctx, ctx,
  667. smp_processor_id());
  668. perf_enable();
  669. }
  670. if (err) {
  671. /*
  672. * If this counter can't go on and it's part of a
  673. * group, then the whole group has to come off.
  674. */
  675. if (leader != counter)
  676. group_sched_out(leader, cpuctx, ctx);
  677. if (leader->hw_event.pinned) {
  678. update_group_times(leader);
  679. leader->state = PERF_COUNTER_STATE_ERROR;
  680. }
  681. }
  682. unlock:
  683. spin_unlock_irqrestore(&ctx->lock, flags);
  684. }
  685. /*
  686. * Enable a counter.
  687. */
  688. static void perf_counter_enable(struct perf_counter *counter)
  689. {
  690. struct perf_counter_context *ctx = counter->ctx;
  691. struct task_struct *task = ctx->task;
  692. if (!task) {
  693. /*
  694. * Enable the counter on the cpu that it's on
  695. */
  696. smp_call_function_single(counter->cpu, __perf_counter_enable,
  697. counter, 1);
  698. return;
  699. }
  700. spin_lock_irq(&ctx->lock);
  701. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  702. goto out;
  703. /*
  704. * If the counter is in error state, clear that first.
  705. * That way, if we see the counter in error state below, we
  706. * know that it has gone back into error state, as distinct
  707. * from the task having been scheduled away before the
  708. * cross-call arrived.
  709. */
  710. if (counter->state == PERF_COUNTER_STATE_ERROR)
  711. counter->state = PERF_COUNTER_STATE_OFF;
  712. retry:
  713. spin_unlock_irq(&ctx->lock);
  714. task_oncpu_function_call(task, __perf_counter_enable, counter);
  715. spin_lock_irq(&ctx->lock);
  716. /*
  717. * If the context is active and the counter is still off,
  718. * we need to retry the cross-call.
  719. */
  720. if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
  721. goto retry;
  722. /*
  723. * Since we have the lock this context can't be scheduled
  724. * in, so we can change the state safely.
  725. */
  726. if (counter->state == PERF_COUNTER_STATE_OFF) {
  727. counter->state = PERF_COUNTER_STATE_INACTIVE;
  728. counter->tstamp_enabled =
  729. ctx->time - counter->total_time_enabled;
  730. ctx->nr_enabled++;
  731. }
  732. out:
  733. spin_unlock_irq(&ctx->lock);
  734. }
  735. static int perf_counter_refresh(struct perf_counter *counter, int refresh)
  736. {
  737. /*
  738. * not supported on inherited counters
  739. */
  740. if (counter->hw_event.inherit)
  741. return -EINVAL;
  742. atomic_add(refresh, &counter->event_limit);
  743. perf_counter_enable(counter);
  744. return 0;
  745. }
  746. void __perf_counter_sched_out(struct perf_counter_context *ctx,
  747. struct perf_cpu_context *cpuctx)
  748. {
  749. struct perf_counter *counter;
  750. spin_lock(&ctx->lock);
  751. ctx->is_active = 0;
  752. if (likely(!ctx->nr_counters))
  753. goto out;
  754. update_context_time(ctx);
  755. perf_disable();
  756. if (ctx->nr_active) {
  757. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  758. if (counter != counter->group_leader)
  759. counter_sched_out(counter, cpuctx, ctx);
  760. else
  761. group_sched_out(counter, cpuctx, ctx);
  762. }
  763. }
  764. perf_enable();
  765. out:
  766. spin_unlock(&ctx->lock);
  767. }
  768. /*
  769. * Test whether two contexts are equivalent, i.e. whether they
  770. * have both been cloned from the same version of the same context
  771. * and they both have the same number of enabled counters.
  772. * If the number of enabled counters is the same, then the set
  773. * of enabled counters should be the same, because these are both
  774. * inherited contexts, therefore we can't access individual counters
  775. * in them directly with an fd; we can only enable/disable all
  776. * counters via prctl, or enable/disable all counters in a family
  777. * via ioctl, which will have the same effect on both contexts.
  778. */
  779. static int context_equiv(struct perf_counter_context *ctx1,
  780. struct perf_counter_context *ctx2)
  781. {
  782. return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
  783. && ctx1->parent_gen == ctx2->parent_gen
  784. && ctx1->nr_enabled == ctx2->nr_enabled;
  785. }
  786. /*
  787. * Called from scheduler to remove the counters of the current task,
  788. * with interrupts disabled.
  789. *
  790. * We stop each counter and update the counter value in counter->count.
  791. *
  792. * This does not protect us against NMI, but disable()
  793. * sets the disabled bit in the control field of counter _before_
  794. * accessing the counter control register. If a NMI hits, then it will
  795. * not restart the counter.
  796. */
  797. void perf_counter_task_sched_out(struct task_struct *task,
  798. struct task_struct *next, int cpu)
  799. {
  800. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  801. struct perf_counter_context *ctx = task->perf_counter_ctxp;
  802. struct perf_counter_context *next_ctx;
  803. struct pt_regs *regs;
  804. if (likely(!ctx || !cpuctx->task_ctx))
  805. return;
  806. update_context_time(ctx);
  807. regs = task_pt_regs(task);
  808. perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
  809. next_ctx = next->perf_counter_ctxp;
  810. if (next_ctx && context_equiv(ctx, next_ctx)) {
  811. task->perf_counter_ctxp = next_ctx;
  812. next->perf_counter_ctxp = ctx;
  813. ctx->task = next;
  814. next_ctx->task = task;
  815. return;
  816. }
  817. __perf_counter_sched_out(ctx, cpuctx);
  818. cpuctx->task_ctx = NULL;
  819. }
  820. static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
  821. {
  822. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  823. if (!cpuctx->task_ctx)
  824. return;
  825. __perf_counter_sched_out(ctx, cpuctx);
  826. cpuctx->task_ctx = NULL;
  827. }
  828. static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
  829. {
  830. __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
  831. }
  832. static void
  833. __perf_counter_sched_in(struct perf_counter_context *ctx,
  834. struct perf_cpu_context *cpuctx, int cpu)
  835. {
  836. struct perf_counter *counter;
  837. int can_add_hw = 1;
  838. spin_lock(&ctx->lock);
  839. ctx->is_active = 1;
  840. if (likely(!ctx->nr_counters))
  841. goto out;
  842. ctx->timestamp = perf_clock();
  843. perf_disable();
  844. /*
  845. * First go through the list and put on any pinned groups
  846. * in order to give them the best chance of going on.
  847. */
  848. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  849. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  850. !counter->hw_event.pinned)
  851. continue;
  852. if (counter->cpu != -1 && counter->cpu != cpu)
  853. continue;
  854. if (counter != counter->group_leader)
  855. counter_sched_in(counter, cpuctx, ctx, cpu);
  856. else {
  857. if (group_can_go_on(counter, cpuctx, 1))
  858. group_sched_in(counter, cpuctx, ctx, cpu);
  859. }
  860. /*
  861. * If this pinned group hasn't been scheduled,
  862. * put it in error state.
  863. */
  864. if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
  865. update_group_times(counter);
  866. counter->state = PERF_COUNTER_STATE_ERROR;
  867. }
  868. }
  869. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  870. /*
  871. * Ignore counters in OFF or ERROR state, and
  872. * ignore pinned counters since we did them already.
  873. */
  874. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  875. counter->hw_event.pinned)
  876. continue;
  877. /*
  878. * Listen to the 'cpu' scheduling filter constraint
  879. * of counters:
  880. */
  881. if (counter->cpu != -1 && counter->cpu != cpu)
  882. continue;
  883. if (counter != counter->group_leader) {
  884. if (counter_sched_in(counter, cpuctx, ctx, cpu))
  885. can_add_hw = 0;
  886. } else {
  887. if (group_can_go_on(counter, cpuctx, can_add_hw)) {
  888. if (group_sched_in(counter, cpuctx, ctx, cpu))
  889. can_add_hw = 0;
  890. }
  891. }
  892. }
  893. perf_enable();
  894. out:
  895. spin_unlock(&ctx->lock);
  896. }
  897. /*
  898. * Called from scheduler to add the counters of the current task
  899. * with interrupts disabled.
  900. *
  901. * We restore the counter value and then enable it.
  902. *
  903. * This does not protect us against NMI, but enable()
  904. * sets the enabled bit in the control field of counter _before_
  905. * accessing the counter control register. If a NMI hits, then it will
  906. * keep the counter running.
  907. */
  908. void perf_counter_task_sched_in(struct task_struct *task, int cpu)
  909. {
  910. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  911. struct perf_counter_context *ctx = task->perf_counter_ctxp;
  912. if (likely(!ctx))
  913. return;
  914. if (cpuctx->task_ctx == ctx)
  915. return;
  916. __perf_counter_sched_in(ctx, cpuctx, cpu);
  917. cpuctx->task_ctx = ctx;
  918. }
  919. static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
  920. {
  921. struct perf_counter_context *ctx = &cpuctx->ctx;
  922. __perf_counter_sched_in(ctx, cpuctx, cpu);
  923. }
  924. int perf_counter_task_disable(void)
  925. {
  926. struct task_struct *curr = current;
  927. struct perf_counter_context *ctx = curr->perf_counter_ctxp;
  928. struct perf_counter *counter;
  929. unsigned long flags;
  930. if (!ctx || !ctx->nr_counters)
  931. return 0;
  932. local_irq_save(flags);
  933. __perf_counter_task_sched_out(ctx);
  934. spin_lock(&ctx->lock);
  935. /*
  936. * Disable all the counters:
  937. */
  938. perf_disable();
  939. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  940. if (counter->state != PERF_COUNTER_STATE_ERROR) {
  941. update_group_times(counter);
  942. counter->state = PERF_COUNTER_STATE_OFF;
  943. }
  944. }
  945. perf_enable();
  946. spin_unlock_irqrestore(&ctx->lock, flags);
  947. return 0;
  948. }
  949. int perf_counter_task_enable(void)
  950. {
  951. struct task_struct *curr = current;
  952. struct perf_counter_context *ctx = curr->perf_counter_ctxp;
  953. struct perf_counter *counter;
  954. unsigned long flags;
  955. int cpu;
  956. if (!ctx || !ctx->nr_counters)
  957. return 0;
  958. local_irq_save(flags);
  959. cpu = smp_processor_id();
  960. __perf_counter_task_sched_out(ctx);
  961. spin_lock(&ctx->lock);
  962. /*
  963. * Disable all the counters:
  964. */
  965. perf_disable();
  966. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  967. if (counter->state > PERF_COUNTER_STATE_OFF)
  968. continue;
  969. counter->state = PERF_COUNTER_STATE_INACTIVE;
  970. counter->tstamp_enabled =
  971. ctx->time - counter->total_time_enabled;
  972. counter->hw_event.disabled = 0;
  973. }
  974. perf_enable();
  975. spin_unlock(&ctx->lock);
  976. perf_counter_task_sched_in(curr, cpu);
  977. local_irq_restore(flags);
  978. return 0;
  979. }
  980. static void perf_log_period(struct perf_counter *counter, u64 period);
  981. static void perf_adjust_freq(struct perf_counter_context *ctx)
  982. {
  983. struct perf_counter *counter;
  984. u64 irq_period;
  985. u64 events, period;
  986. s64 delta;
  987. spin_lock(&ctx->lock);
  988. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  989. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  990. continue;
  991. if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
  992. continue;
  993. events = HZ * counter->hw.interrupts * counter->hw.irq_period;
  994. period = div64_u64(events, counter->hw_event.irq_freq);
  995. delta = (s64)(1 + period - counter->hw.irq_period);
  996. delta >>= 1;
  997. irq_period = counter->hw.irq_period + delta;
  998. if (!irq_period)
  999. irq_period = 1;
  1000. perf_log_period(counter, irq_period);
  1001. counter->hw.irq_period = irq_period;
  1002. counter->hw.interrupts = 0;
  1003. }
  1004. spin_unlock(&ctx->lock);
  1005. }
  1006. /*
  1007. * Round-robin a context's counters:
  1008. */
  1009. static void rotate_ctx(struct perf_counter_context *ctx)
  1010. {
  1011. struct perf_counter *counter;
  1012. if (!ctx->nr_counters)
  1013. return;
  1014. spin_lock(&ctx->lock);
  1015. /*
  1016. * Rotate the first entry last (works just fine for group counters too):
  1017. */
  1018. perf_disable();
  1019. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  1020. list_move_tail(&counter->list_entry, &ctx->counter_list);
  1021. break;
  1022. }
  1023. perf_enable();
  1024. spin_unlock(&ctx->lock);
  1025. }
  1026. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  1027. {
  1028. struct perf_cpu_context *cpuctx;
  1029. struct perf_counter_context *ctx;
  1030. if (!atomic_read(&nr_counters))
  1031. return;
  1032. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1033. ctx = curr->perf_counter_ctxp;
  1034. perf_adjust_freq(&cpuctx->ctx);
  1035. if (ctx)
  1036. perf_adjust_freq(ctx);
  1037. perf_counter_cpu_sched_out(cpuctx);
  1038. if (ctx)
  1039. __perf_counter_task_sched_out(ctx);
  1040. rotate_ctx(&cpuctx->ctx);
  1041. if (ctx)
  1042. rotate_ctx(ctx);
  1043. perf_counter_cpu_sched_in(cpuctx, cpu);
  1044. if (ctx)
  1045. perf_counter_task_sched_in(curr, cpu);
  1046. }
  1047. /*
  1048. * Cross CPU call to read the hardware counter
  1049. */
  1050. static void __read(void *info)
  1051. {
  1052. struct perf_counter *counter = info;
  1053. struct perf_counter_context *ctx = counter->ctx;
  1054. unsigned long flags;
  1055. local_irq_save(flags);
  1056. if (ctx->is_active)
  1057. update_context_time(ctx);
  1058. counter->pmu->read(counter);
  1059. update_counter_times(counter);
  1060. local_irq_restore(flags);
  1061. }
  1062. static u64 perf_counter_read(struct perf_counter *counter)
  1063. {
  1064. /*
  1065. * If counter is enabled and currently active on a CPU, update the
  1066. * value in the counter structure:
  1067. */
  1068. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  1069. smp_call_function_single(counter->oncpu,
  1070. __read, counter, 1);
  1071. } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
  1072. update_counter_times(counter);
  1073. }
  1074. return atomic64_read(&counter->count);
  1075. }
  1076. /*
  1077. * Initialize the perf_counter context in a task_struct:
  1078. */
  1079. static void
  1080. __perf_counter_init_context(struct perf_counter_context *ctx,
  1081. struct task_struct *task)
  1082. {
  1083. memset(ctx, 0, sizeof(*ctx));
  1084. spin_lock_init(&ctx->lock);
  1085. mutex_init(&ctx->mutex);
  1086. INIT_LIST_HEAD(&ctx->counter_list);
  1087. INIT_LIST_HEAD(&ctx->event_list);
  1088. atomic_set(&ctx->refcount, 1);
  1089. ctx->task = task;
  1090. }
  1091. static void put_context(struct perf_counter_context *ctx)
  1092. {
  1093. if (ctx->task)
  1094. put_task_struct(ctx->task);
  1095. }
  1096. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  1097. {
  1098. struct perf_cpu_context *cpuctx;
  1099. struct perf_counter_context *ctx;
  1100. struct perf_counter_context *tctx;
  1101. struct task_struct *task;
  1102. /*
  1103. * If cpu is not a wildcard then this is a percpu counter:
  1104. */
  1105. if (cpu != -1) {
  1106. /* Must be root to operate on a CPU counter: */
  1107. if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
  1108. return ERR_PTR(-EACCES);
  1109. if (cpu < 0 || cpu > num_possible_cpus())
  1110. return ERR_PTR(-EINVAL);
  1111. /*
  1112. * We could be clever and allow to attach a counter to an
  1113. * offline CPU and activate it when the CPU comes up, but
  1114. * that's for later.
  1115. */
  1116. if (!cpu_isset(cpu, cpu_online_map))
  1117. return ERR_PTR(-ENODEV);
  1118. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1119. ctx = &cpuctx->ctx;
  1120. return ctx;
  1121. }
  1122. rcu_read_lock();
  1123. if (!pid)
  1124. task = current;
  1125. else
  1126. task = find_task_by_vpid(pid);
  1127. if (task)
  1128. get_task_struct(task);
  1129. rcu_read_unlock();
  1130. if (!task)
  1131. return ERR_PTR(-ESRCH);
  1132. /* Reuse ptrace permission checks for now. */
  1133. if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
  1134. put_task_struct(task);
  1135. return ERR_PTR(-EACCES);
  1136. }
  1137. ctx = task->perf_counter_ctxp;
  1138. if (!ctx) {
  1139. ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
  1140. if (!ctx) {
  1141. put_task_struct(task);
  1142. return ERR_PTR(-ENOMEM);
  1143. }
  1144. __perf_counter_init_context(ctx, task);
  1145. /*
  1146. * Make sure other cpus see correct values for *ctx
  1147. * once task->perf_counter_ctxp is visible to them.
  1148. */
  1149. smp_wmb();
  1150. tctx = cmpxchg(&task->perf_counter_ctxp, NULL, ctx);
  1151. if (tctx) {
  1152. /*
  1153. * We raced with some other task; use
  1154. * the context they set.
  1155. */
  1156. kfree(ctx);
  1157. ctx = tctx;
  1158. }
  1159. }
  1160. return ctx;
  1161. }
  1162. static void free_counter_rcu(struct rcu_head *head)
  1163. {
  1164. struct perf_counter *counter;
  1165. counter = container_of(head, struct perf_counter, rcu_head);
  1166. put_ctx(counter->ctx);
  1167. kfree(counter);
  1168. }
  1169. static void perf_pending_sync(struct perf_counter *counter);
  1170. static void free_counter(struct perf_counter *counter)
  1171. {
  1172. perf_pending_sync(counter);
  1173. atomic_dec(&nr_counters);
  1174. if (counter->hw_event.mmap)
  1175. atomic_dec(&nr_mmap_tracking);
  1176. if (counter->hw_event.munmap)
  1177. atomic_dec(&nr_munmap_tracking);
  1178. if (counter->hw_event.comm)
  1179. atomic_dec(&nr_comm_tracking);
  1180. if (counter->destroy)
  1181. counter->destroy(counter);
  1182. call_rcu(&counter->rcu_head, free_counter_rcu);
  1183. }
  1184. /*
  1185. * Called when the last reference to the file is gone.
  1186. */
  1187. static int perf_release(struct inode *inode, struct file *file)
  1188. {
  1189. struct perf_counter *counter = file->private_data;
  1190. struct perf_counter_context *ctx = counter->ctx;
  1191. file->private_data = NULL;
  1192. mutex_lock(&ctx->mutex);
  1193. perf_counter_remove_from_context(counter);
  1194. mutex_unlock(&ctx->mutex);
  1195. free_counter(counter);
  1196. put_context(ctx);
  1197. return 0;
  1198. }
  1199. /*
  1200. * Read the performance counter - simple non blocking version for now
  1201. */
  1202. static ssize_t
  1203. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  1204. {
  1205. u64 values[3];
  1206. int n;
  1207. /*
  1208. * Return end-of-file for a read on a counter that is in
  1209. * error state (i.e. because it was pinned but it couldn't be
  1210. * scheduled on to the CPU at some point).
  1211. */
  1212. if (counter->state == PERF_COUNTER_STATE_ERROR)
  1213. return 0;
  1214. mutex_lock(&counter->child_mutex);
  1215. values[0] = perf_counter_read(counter);
  1216. n = 1;
  1217. if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1218. values[n++] = counter->total_time_enabled +
  1219. atomic64_read(&counter->child_total_time_enabled);
  1220. if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1221. values[n++] = counter->total_time_running +
  1222. atomic64_read(&counter->child_total_time_running);
  1223. mutex_unlock(&counter->child_mutex);
  1224. if (count < n * sizeof(u64))
  1225. return -EINVAL;
  1226. count = n * sizeof(u64);
  1227. if (copy_to_user(buf, values, count))
  1228. return -EFAULT;
  1229. return count;
  1230. }
  1231. static ssize_t
  1232. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1233. {
  1234. struct perf_counter *counter = file->private_data;
  1235. return perf_read_hw(counter, buf, count);
  1236. }
  1237. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1238. {
  1239. struct perf_counter *counter = file->private_data;
  1240. struct perf_mmap_data *data;
  1241. unsigned int events = POLL_HUP;
  1242. rcu_read_lock();
  1243. data = rcu_dereference(counter->data);
  1244. if (data)
  1245. events = atomic_xchg(&data->poll, 0);
  1246. rcu_read_unlock();
  1247. poll_wait(file, &counter->waitq, wait);
  1248. return events;
  1249. }
  1250. static void perf_counter_reset(struct perf_counter *counter)
  1251. {
  1252. (void)perf_counter_read(counter);
  1253. atomic64_set(&counter->count, 0);
  1254. perf_counter_update_userpage(counter);
  1255. }
  1256. static void perf_counter_for_each_sibling(struct perf_counter *counter,
  1257. void (*func)(struct perf_counter *))
  1258. {
  1259. struct perf_counter_context *ctx = counter->ctx;
  1260. struct perf_counter *sibling;
  1261. mutex_lock(&ctx->mutex);
  1262. counter = counter->group_leader;
  1263. func(counter);
  1264. list_for_each_entry(sibling, &counter->sibling_list, list_entry)
  1265. func(sibling);
  1266. mutex_unlock(&ctx->mutex);
  1267. }
  1268. static void perf_counter_for_each_child(struct perf_counter *counter,
  1269. void (*func)(struct perf_counter *))
  1270. {
  1271. struct perf_counter *child;
  1272. mutex_lock(&counter->child_mutex);
  1273. func(counter);
  1274. list_for_each_entry(child, &counter->child_list, child_list)
  1275. func(child);
  1276. mutex_unlock(&counter->child_mutex);
  1277. }
  1278. static void perf_counter_for_each(struct perf_counter *counter,
  1279. void (*func)(struct perf_counter *))
  1280. {
  1281. struct perf_counter *child;
  1282. mutex_lock(&counter->child_mutex);
  1283. perf_counter_for_each_sibling(counter, func);
  1284. list_for_each_entry(child, &counter->child_list, child_list)
  1285. perf_counter_for_each_sibling(child, func);
  1286. mutex_unlock(&counter->child_mutex);
  1287. }
  1288. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1289. {
  1290. struct perf_counter *counter = file->private_data;
  1291. void (*func)(struct perf_counter *);
  1292. u32 flags = arg;
  1293. switch (cmd) {
  1294. case PERF_COUNTER_IOC_ENABLE:
  1295. func = perf_counter_enable;
  1296. break;
  1297. case PERF_COUNTER_IOC_DISABLE:
  1298. func = perf_counter_disable;
  1299. break;
  1300. case PERF_COUNTER_IOC_RESET:
  1301. func = perf_counter_reset;
  1302. break;
  1303. case PERF_COUNTER_IOC_REFRESH:
  1304. return perf_counter_refresh(counter, arg);
  1305. default:
  1306. return -ENOTTY;
  1307. }
  1308. if (flags & PERF_IOC_FLAG_GROUP)
  1309. perf_counter_for_each(counter, func);
  1310. else
  1311. perf_counter_for_each_child(counter, func);
  1312. return 0;
  1313. }
  1314. /*
  1315. * Callers need to ensure there can be no nesting of this function, otherwise
  1316. * the seqlock logic goes bad. We can not serialize this because the arch
  1317. * code calls this from NMI context.
  1318. */
  1319. void perf_counter_update_userpage(struct perf_counter *counter)
  1320. {
  1321. struct perf_mmap_data *data;
  1322. struct perf_counter_mmap_page *userpg;
  1323. rcu_read_lock();
  1324. data = rcu_dereference(counter->data);
  1325. if (!data)
  1326. goto unlock;
  1327. userpg = data->user_page;
  1328. /*
  1329. * Disable preemption so as to not let the corresponding user-space
  1330. * spin too long if we get preempted.
  1331. */
  1332. preempt_disable();
  1333. ++userpg->lock;
  1334. barrier();
  1335. userpg->index = counter->hw.idx;
  1336. userpg->offset = atomic64_read(&counter->count);
  1337. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  1338. userpg->offset -= atomic64_read(&counter->hw.prev_count);
  1339. barrier();
  1340. ++userpg->lock;
  1341. preempt_enable();
  1342. unlock:
  1343. rcu_read_unlock();
  1344. }
  1345. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1346. {
  1347. struct perf_counter *counter = vma->vm_file->private_data;
  1348. struct perf_mmap_data *data;
  1349. int ret = VM_FAULT_SIGBUS;
  1350. rcu_read_lock();
  1351. data = rcu_dereference(counter->data);
  1352. if (!data)
  1353. goto unlock;
  1354. if (vmf->pgoff == 0) {
  1355. vmf->page = virt_to_page(data->user_page);
  1356. } else {
  1357. int nr = vmf->pgoff - 1;
  1358. if ((unsigned)nr > data->nr_pages)
  1359. goto unlock;
  1360. vmf->page = virt_to_page(data->data_pages[nr]);
  1361. }
  1362. get_page(vmf->page);
  1363. ret = 0;
  1364. unlock:
  1365. rcu_read_unlock();
  1366. return ret;
  1367. }
  1368. static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
  1369. {
  1370. struct perf_mmap_data *data;
  1371. unsigned long size;
  1372. int i;
  1373. WARN_ON(atomic_read(&counter->mmap_count));
  1374. size = sizeof(struct perf_mmap_data);
  1375. size += nr_pages * sizeof(void *);
  1376. data = kzalloc(size, GFP_KERNEL);
  1377. if (!data)
  1378. goto fail;
  1379. data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
  1380. if (!data->user_page)
  1381. goto fail_user_page;
  1382. for (i = 0; i < nr_pages; i++) {
  1383. data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
  1384. if (!data->data_pages[i])
  1385. goto fail_data_pages;
  1386. }
  1387. data->nr_pages = nr_pages;
  1388. atomic_set(&data->lock, -1);
  1389. rcu_assign_pointer(counter->data, data);
  1390. return 0;
  1391. fail_data_pages:
  1392. for (i--; i >= 0; i--)
  1393. free_page((unsigned long)data->data_pages[i]);
  1394. free_page((unsigned long)data->user_page);
  1395. fail_user_page:
  1396. kfree(data);
  1397. fail:
  1398. return -ENOMEM;
  1399. }
  1400. static void __perf_mmap_data_free(struct rcu_head *rcu_head)
  1401. {
  1402. struct perf_mmap_data *data = container_of(rcu_head,
  1403. struct perf_mmap_data, rcu_head);
  1404. int i;
  1405. free_page((unsigned long)data->user_page);
  1406. for (i = 0; i < data->nr_pages; i++)
  1407. free_page((unsigned long)data->data_pages[i]);
  1408. kfree(data);
  1409. }
  1410. static void perf_mmap_data_free(struct perf_counter *counter)
  1411. {
  1412. struct perf_mmap_data *data = counter->data;
  1413. WARN_ON(atomic_read(&counter->mmap_count));
  1414. rcu_assign_pointer(counter->data, NULL);
  1415. call_rcu(&data->rcu_head, __perf_mmap_data_free);
  1416. }
  1417. static void perf_mmap_open(struct vm_area_struct *vma)
  1418. {
  1419. struct perf_counter *counter = vma->vm_file->private_data;
  1420. atomic_inc(&counter->mmap_count);
  1421. }
  1422. static void perf_mmap_close(struct vm_area_struct *vma)
  1423. {
  1424. struct perf_counter *counter = vma->vm_file->private_data;
  1425. if (atomic_dec_and_mutex_lock(&counter->mmap_count,
  1426. &counter->mmap_mutex)) {
  1427. struct user_struct *user = current_user();
  1428. atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
  1429. vma->vm_mm->locked_vm -= counter->data->nr_locked;
  1430. perf_mmap_data_free(counter);
  1431. mutex_unlock(&counter->mmap_mutex);
  1432. }
  1433. }
  1434. static struct vm_operations_struct perf_mmap_vmops = {
  1435. .open = perf_mmap_open,
  1436. .close = perf_mmap_close,
  1437. .fault = perf_mmap_fault,
  1438. };
  1439. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  1440. {
  1441. struct perf_counter *counter = file->private_data;
  1442. struct user_struct *user = current_user();
  1443. unsigned long vma_size;
  1444. unsigned long nr_pages;
  1445. unsigned long user_locked, user_lock_limit;
  1446. unsigned long locked, lock_limit;
  1447. long user_extra, extra;
  1448. int ret = 0;
  1449. if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
  1450. return -EINVAL;
  1451. vma_size = vma->vm_end - vma->vm_start;
  1452. nr_pages = (vma_size / PAGE_SIZE) - 1;
  1453. /*
  1454. * If we have data pages ensure they're a power-of-two number, so we
  1455. * can do bitmasks instead of modulo.
  1456. */
  1457. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  1458. return -EINVAL;
  1459. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  1460. return -EINVAL;
  1461. if (vma->vm_pgoff != 0)
  1462. return -EINVAL;
  1463. mutex_lock(&counter->mmap_mutex);
  1464. if (atomic_inc_not_zero(&counter->mmap_count)) {
  1465. if (nr_pages != counter->data->nr_pages)
  1466. ret = -EINVAL;
  1467. goto unlock;
  1468. }
  1469. user_extra = nr_pages + 1;
  1470. user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
  1471. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  1472. extra = 0;
  1473. if (user_locked > user_lock_limit)
  1474. extra = user_locked - user_lock_limit;
  1475. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  1476. lock_limit >>= PAGE_SHIFT;
  1477. locked = vma->vm_mm->locked_vm + extra;
  1478. if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
  1479. ret = -EPERM;
  1480. goto unlock;
  1481. }
  1482. WARN_ON(counter->data);
  1483. ret = perf_mmap_data_alloc(counter, nr_pages);
  1484. if (ret)
  1485. goto unlock;
  1486. atomic_set(&counter->mmap_count, 1);
  1487. atomic_long_add(user_extra, &user->locked_vm);
  1488. vma->vm_mm->locked_vm += extra;
  1489. counter->data->nr_locked = extra;
  1490. unlock:
  1491. mutex_unlock(&counter->mmap_mutex);
  1492. vma->vm_flags &= ~VM_MAYWRITE;
  1493. vma->vm_flags |= VM_RESERVED;
  1494. vma->vm_ops = &perf_mmap_vmops;
  1495. return ret;
  1496. }
  1497. static int perf_fasync(int fd, struct file *filp, int on)
  1498. {
  1499. struct perf_counter *counter = filp->private_data;
  1500. struct inode *inode = filp->f_path.dentry->d_inode;
  1501. int retval;
  1502. mutex_lock(&inode->i_mutex);
  1503. retval = fasync_helper(fd, filp, on, &counter->fasync);
  1504. mutex_unlock(&inode->i_mutex);
  1505. if (retval < 0)
  1506. return retval;
  1507. return 0;
  1508. }
  1509. static const struct file_operations perf_fops = {
  1510. .release = perf_release,
  1511. .read = perf_read,
  1512. .poll = perf_poll,
  1513. .unlocked_ioctl = perf_ioctl,
  1514. .compat_ioctl = perf_ioctl,
  1515. .mmap = perf_mmap,
  1516. .fasync = perf_fasync,
  1517. };
  1518. /*
  1519. * Perf counter wakeup
  1520. *
  1521. * If there's data, ensure we set the poll() state and publish everything
  1522. * to user-space before waking everybody up.
  1523. */
  1524. void perf_counter_wakeup(struct perf_counter *counter)
  1525. {
  1526. wake_up_all(&counter->waitq);
  1527. if (counter->pending_kill) {
  1528. kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
  1529. counter->pending_kill = 0;
  1530. }
  1531. }
  1532. /*
  1533. * Pending wakeups
  1534. *
  1535. * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
  1536. *
  1537. * The NMI bit means we cannot possibly take locks. Therefore, maintain a
  1538. * single linked list and use cmpxchg() to add entries lockless.
  1539. */
  1540. static void perf_pending_counter(struct perf_pending_entry *entry)
  1541. {
  1542. struct perf_counter *counter = container_of(entry,
  1543. struct perf_counter, pending);
  1544. if (counter->pending_disable) {
  1545. counter->pending_disable = 0;
  1546. perf_counter_disable(counter);
  1547. }
  1548. if (counter->pending_wakeup) {
  1549. counter->pending_wakeup = 0;
  1550. perf_counter_wakeup(counter);
  1551. }
  1552. }
  1553. #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
  1554. static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
  1555. PENDING_TAIL,
  1556. };
  1557. static void perf_pending_queue(struct perf_pending_entry *entry,
  1558. void (*func)(struct perf_pending_entry *))
  1559. {
  1560. struct perf_pending_entry **head;
  1561. if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
  1562. return;
  1563. entry->func = func;
  1564. head = &get_cpu_var(perf_pending_head);
  1565. do {
  1566. entry->next = *head;
  1567. } while (cmpxchg(head, entry->next, entry) != entry->next);
  1568. set_perf_counter_pending();
  1569. put_cpu_var(perf_pending_head);
  1570. }
  1571. static int __perf_pending_run(void)
  1572. {
  1573. struct perf_pending_entry *list;
  1574. int nr = 0;
  1575. list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
  1576. while (list != PENDING_TAIL) {
  1577. void (*func)(struct perf_pending_entry *);
  1578. struct perf_pending_entry *entry = list;
  1579. list = list->next;
  1580. func = entry->func;
  1581. entry->next = NULL;
  1582. /*
  1583. * Ensure we observe the unqueue before we issue the wakeup,
  1584. * so that we won't be waiting forever.
  1585. * -- see perf_not_pending().
  1586. */
  1587. smp_wmb();
  1588. func(entry);
  1589. nr++;
  1590. }
  1591. return nr;
  1592. }
  1593. static inline int perf_not_pending(struct perf_counter *counter)
  1594. {
  1595. /*
  1596. * If we flush on whatever cpu we run, there is a chance we don't
  1597. * need to wait.
  1598. */
  1599. get_cpu();
  1600. __perf_pending_run();
  1601. put_cpu();
  1602. /*
  1603. * Ensure we see the proper queue state before going to sleep
  1604. * so that we do not miss the wakeup. -- see perf_pending_handle()
  1605. */
  1606. smp_rmb();
  1607. return counter->pending.next == NULL;
  1608. }
  1609. static void perf_pending_sync(struct perf_counter *counter)
  1610. {
  1611. wait_event(counter->waitq, perf_not_pending(counter));
  1612. }
  1613. void perf_counter_do_pending(void)
  1614. {
  1615. __perf_pending_run();
  1616. }
  1617. /*
  1618. * Callchain support -- arch specific
  1619. */
  1620. __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1621. {
  1622. return NULL;
  1623. }
  1624. /*
  1625. * Output
  1626. */
  1627. struct perf_output_handle {
  1628. struct perf_counter *counter;
  1629. struct perf_mmap_data *data;
  1630. unsigned int offset;
  1631. unsigned int head;
  1632. int nmi;
  1633. int overflow;
  1634. int locked;
  1635. unsigned long flags;
  1636. };
  1637. static void perf_output_wakeup(struct perf_output_handle *handle)
  1638. {
  1639. atomic_set(&handle->data->poll, POLL_IN);
  1640. if (handle->nmi) {
  1641. handle->counter->pending_wakeup = 1;
  1642. perf_pending_queue(&handle->counter->pending,
  1643. perf_pending_counter);
  1644. } else
  1645. perf_counter_wakeup(handle->counter);
  1646. }
  1647. /*
  1648. * Curious locking construct.
  1649. *
  1650. * We need to ensure a later event doesn't publish a head when a former
  1651. * event isn't done writing. However since we need to deal with NMIs we
  1652. * cannot fully serialize things.
  1653. *
  1654. * What we do is serialize between CPUs so we only have to deal with NMI
  1655. * nesting on a single CPU.
  1656. *
  1657. * We only publish the head (and generate a wakeup) when the outer-most
  1658. * event completes.
  1659. */
  1660. static void perf_output_lock(struct perf_output_handle *handle)
  1661. {
  1662. struct perf_mmap_data *data = handle->data;
  1663. int cpu;
  1664. handle->locked = 0;
  1665. local_irq_save(handle->flags);
  1666. cpu = smp_processor_id();
  1667. if (in_nmi() && atomic_read(&data->lock) == cpu)
  1668. return;
  1669. while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
  1670. cpu_relax();
  1671. handle->locked = 1;
  1672. }
  1673. static void perf_output_unlock(struct perf_output_handle *handle)
  1674. {
  1675. struct perf_mmap_data *data = handle->data;
  1676. int head, cpu;
  1677. data->done_head = data->head;
  1678. if (!handle->locked)
  1679. goto out;
  1680. again:
  1681. /*
  1682. * The xchg implies a full barrier that ensures all writes are done
  1683. * before we publish the new head, matched by a rmb() in userspace when
  1684. * reading this position.
  1685. */
  1686. while ((head = atomic_xchg(&data->done_head, 0)))
  1687. data->user_page->data_head = head;
  1688. /*
  1689. * NMI can happen here, which means we can miss a done_head update.
  1690. */
  1691. cpu = atomic_xchg(&data->lock, -1);
  1692. WARN_ON_ONCE(cpu != smp_processor_id());
  1693. /*
  1694. * Therefore we have to validate we did not indeed do so.
  1695. */
  1696. if (unlikely(atomic_read(&data->done_head))) {
  1697. /*
  1698. * Since we had it locked, we can lock it again.
  1699. */
  1700. while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
  1701. cpu_relax();
  1702. goto again;
  1703. }
  1704. if (atomic_xchg(&data->wakeup, 0))
  1705. perf_output_wakeup(handle);
  1706. out:
  1707. local_irq_restore(handle->flags);
  1708. }
  1709. static int perf_output_begin(struct perf_output_handle *handle,
  1710. struct perf_counter *counter, unsigned int size,
  1711. int nmi, int overflow)
  1712. {
  1713. struct perf_mmap_data *data;
  1714. unsigned int offset, head;
  1715. /*
  1716. * For inherited counters we send all the output towards the parent.
  1717. */
  1718. if (counter->parent)
  1719. counter = counter->parent;
  1720. rcu_read_lock();
  1721. data = rcu_dereference(counter->data);
  1722. if (!data)
  1723. goto out;
  1724. handle->data = data;
  1725. handle->counter = counter;
  1726. handle->nmi = nmi;
  1727. handle->overflow = overflow;
  1728. if (!data->nr_pages)
  1729. goto fail;
  1730. perf_output_lock(handle);
  1731. do {
  1732. offset = head = atomic_read(&data->head);
  1733. head += size;
  1734. } while (atomic_cmpxchg(&data->head, offset, head) != offset);
  1735. handle->offset = offset;
  1736. handle->head = head;
  1737. if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
  1738. atomic_set(&data->wakeup, 1);
  1739. return 0;
  1740. fail:
  1741. perf_output_wakeup(handle);
  1742. out:
  1743. rcu_read_unlock();
  1744. return -ENOSPC;
  1745. }
  1746. static void perf_output_copy(struct perf_output_handle *handle,
  1747. void *buf, unsigned int len)
  1748. {
  1749. unsigned int pages_mask;
  1750. unsigned int offset;
  1751. unsigned int size;
  1752. void **pages;
  1753. offset = handle->offset;
  1754. pages_mask = handle->data->nr_pages - 1;
  1755. pages = handle->data->data_pages;
  1756. do {
  1757. unsigned int page_offset;
  1758. int nr;
  1759. nr = (offset >> PAGE_SHIFT) & pages_mask;
  1760. page_offset = offset & (PAGE_SIZE - 1);
  1761. size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
  1762. memcpy(pages[nr] + page_offset, buf, size);
  1763. len -= size;
  1764. buf += size;
  1765. offset += size;
  1766. } while (len);
  1767. handle->offset = offset;
  1768. /*
  1769. * Check we didn't copy past our reservation window, taking the
  1770. * possible unsigned int wrap into account.
  1771. */
  1772. WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
  1773. }
  1774. #define perf_output_put(handle, x) \
  1775. perf_output_copy((handle), &(x), sizeof(x))
  1776. static void perf_output_end(struct perf_output_handle *handle)
  1777. {
  1778. struct perf_counter *counter = handle->counter;
  1779. struct perf_mmap_data *data = handle->data;
  1780. int wakeup_events = counter->hw_event.wakeup_events;
  1781. if (handle->overflow && wakeup_events) {
  1782. int events = atomic_inc_return(&data->events);
  1783. if (events >= wakeup_events) {
  1784. atomic_sub(wakeup_events, &data->events);
  1785. atomic_set(&data->wakeup, 1);
  1786. }
  1787. }
  1788. perf_output_unlock(handle);
  1789. rcu_read_unlock();
  1790. }
  1791. static void perf_counter_output(struct perf_counter *counter,
  1792. int nmi, struct pt_regs *regs, u64 addr)
  1793. {
  1794. int ret;
  1795. u64 record_type = counter->hw_event.record_type;
  1796. struct perf_output_handle handle;
  1797. struct perf_event_header header;
  1798. u64 ip;
  1799. struct {
  1800. u32 pid, tid;
  1801. } tid_entry;
  1802. struct {
  1803. u64 event;
  1804. u64 counter;
  1805. } group_entry;
  1806. struct perf_callchain_entry *callchain = NULL;
  1807. int callchain_size = 0;
  1808. u64 time;
  1809. struct {
  1810. u32 cpu, reserved;
  1811. } cpu_entry;
  1812. header.type = 0;
  1813. header.size = sizeof(header);
  1814. header.misc = PERF_EVENT_MISC_OVERFLOW;
  1815. header.misc |= perf_misc_flags(regs);
  1816. if (record_type & PERF_RECORD_IP) {
  1817. ip = perf_instruction_pointer(regs);
  1818. header.type |= PERF_RECORD_IP;
  1819. header.size += sizeof(ip);
  1820. }
  1821. if (record_type & PERF_RECORD_TID) {
  1822. /* namespace issues */
  1823. tid_entry.pid = current->group_leader->pid;
  1824. tid_entry.tid = current->pid;
  1825. header.type |= PERF_RECORD_TID;
  1826. header.size += sizeof(tid_entry);
  1827. }
  1828. if (record_type & PERF_RECORD_TIME) {
  1829. /*
  1830. * Maybe do better on x86 and provide cpu_clock_nmi()
  1831. */
  1832. time = sched_clock();
  1833. header.type |= PERF_RECORD_TIME;
  1834. header.size += sizeof(u64);
  1835. }
  1836. if (record_type & PERF_RECORD_ADDR) {
  1837. header.type |= PERF_RECORD_ADDR;
  1838. header.size += sizeof(u64);
  1839. }
  1840. if (record_type & PERF_RECORD_CONFIG) {
  1841. header.type |= PERF_RECORD_CONFIG;
  1842. header.size += sizeof(u64);
  1843. }
  1844. if (record_type & PERF_RECORD_CPU) {
  1845. header.type |= PERF_RECORD_CPU;
  1846. header.size += sizeof(cpu_entry);
  1847. cpu_entry.cpu = raw_smp_processor_id();
  1848. }
  1849. if (record_type & PERF_RECORD_GROUP) {
  1850. header.type |= PERF_RECORD_GROUP;
  1851. header.size += sizeof(u64) +
  1852. counter->nr_siblings * sizeof(group_entry);
  1853. }
  1854. if (record_type & PERF_RECORD_CALLCHAIN) {
  1855. callchain = perf_callchain(regs);
  1856. if (callchain) {
  1857. callchain_size = (1 + callchain->nr) * sizeof(u64);
  1858. header.type |= PERF_RECORD_CALLCHAIN;
  1859. header.size += callchain_size;
  1860. }
  1861. }
  1862. ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
  1863. if (ret)
  1864. return;
  1865. perf_output_put(&handle, header);
  1866. if (record_type & PERF_RECORD_IP)
  1867. perf_output_put(&handle, ip);
  1868. if (record_type & PERF_RECORD_TID)
  1869. perf_output_put(&handle, tid_entry);
  1870. if (record_type & PERF_RECORD_TIME)
  1871. perf_output_put(&handle, time);
  1872. if (record_type & PERF_RECORD_ADDR)
  1873. perf_output_put(&handle, addr);
  1874. if (record_type & PERF_RECORD_CONFIG)
  1875. perf_output_put(&handle, counter->hw_event.config);
  1876. if (record_type & PERF_RECORD_CPU)
  1877. perf_output_put(&handle, cpu_entry);
  1878. /*
  1879. * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
  1880. */
  1881. if (record_type & PERF_RECORD_GROUP) {
  1882. struct perf_counter *leader, *sub;
  1883. u64 nr = counter->nr_siblings;
  1884. perf_output_put(&handle, nr);
  1885. leader = counter->group_leader;
  1886. list_for_each_entry(sub, &leader->sibling_list, list_entry) {
  1887. if (sub != counter)
  1888. sub->pmu->read(sub);
  1889. group_entry.event = sub->hw_event.config;
  1890. group_entry.counter = atomic64_read(&sub->count);
  1891. perf_output_put(&handle, group_entry);
  1892. }
  1893. }
  1894. if (callchain)
  1895. perf_output_copy(&handle, callchain, callchain_size);
  1896. perf_output_end(&handle);
  1897. }
  1898. /*
  1899. * comm tracking
  1900. */
  1901. struct perf_comm_event {
  1902. struct task_struct *task;
  1903. char *comm;
  1904. int comm_size;
  1905. struct {
  1906. struct perf_event_header header;
  1907. u32 pid;
  1908. u32 tid;
  1909. } event;
  1910. };
  1911. static void perf_counter_comm_output(struct perf_counter *counter,
  1912. struct perf_comm_event *comm_event)
  1913. {
  1914. struct perf_output_handle handle;
  1915. int size = comm_event->event.header.size;
  1916. int ret = perf_output_begin(&handle, counter, size, 0, 0);
  1917. if (ret)
  1918. return;
  1919. perf_output_put(&handle, comm_event->event);
  1920. perf_output_copy(&handle, comm_event->comm,
  1921. comm_event->comm_size);
  1922. perf_output_end(&handle);
  1923. }
  1924. static int perf_counter_comm_match(struct perf_counter *counter,
  1925. struct perf_comm_event *comm_event)
  1926. {
  1927. if (counter->hw_event.comm &&
  1928. comm_event->event.header.type == PERF_EVENT_COMM)
  1929. return 1;
  1930. return 0;
  1931. }
  1932. static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
  1933. struct perf_comm_event *comm_event)
  1934. {
  1935. struct perf_counter *counter;
  1936. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  1937. return;
  1938. rcu_read_lock();
  1939. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  1940. if (perf_counter_comm_match(counter, comm_event))
  1941. perf_counter_comm_output(counter, comm_event);
  1942. }
  1943. rcu_read_unlock();
  1944. }
  1945. static void perf_counter_comm_event(struct perf_comm_event *comm_event)
  1946. {
  1947. struct perf_cpu_context *cpuctx;
  1948. unsigned int size;
  1949. char *comm = comm_event->task->comm;
  1950. size = ALIGN(strlen(comm)+1, sizeof(u64));
  1951. comm_event->comm = comm;
  1952. comm_event->comm_size = size;
  1953. comm_event->event.header.size = sizeof(comm_event->event) + size;
  1954. cpuctx = &get_cpu_var(perf_cpu_context);
  1955. perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
  1956. put_cpu_var(perf_cpu_context);
  1957. perf_counter_comm_ctx(current->perf_counter_ctxp, comm_event);
  1958. }
  1959. void perf_counter_comm(struct task_struct *task)
  1960. {
  1961. struct perf_comm_event comm_event;
  1962. if (!atomic_read(&nr_comm_tracking))
  1963. return;
  1964. if (!current->perf_counter_ctxp)
  1965. return;
  1966. comm_event = (struct perf_comm_event){
  1967. .task = task,
  1968. .event = {
  1969. .header = { .type = PERF_EVENT_COMM, },
  1970. .pid = task->group_leader->pid,
  1971. .tid = task->pid,
  1972. },
  1973. };
  1974. perf_counter_comm_event(&comm_event);
  1975. }
  1976. /*
  1977. * mmap tracking
  1978. */
  1979. struct perf_mmap_event {
  1980. struct file *file;
  1981. char *file_name;
  1982. int file_size;
  1983. struct {
  1984. struct perf_event_header header;
  1985. u32 pid;
  1986. u32 tid;
  1987. u64 start;
  1988. u64 len;
  1989. u64 pgoff;
  1990. } event;
  1991. };
  1992. static void perf_counter_mmap_output(struct perf_counter *counter,
  1993. struct perf_mmap_event *mmap_event)
  1994. {
  1995. struct perf_output_handle handle;
  1996. int size = mmap_event->event.header.size;
  1997. int ret = perf_output_begin(&handle, counter, size, 0, 0);
  1998. if (ret)
  1999. return;
  2000. perf_output_put(&handle, mmap_event->event);
  2001. perf_output_copy(&handle, mmap_event->file_name,
  2002. mmap_event->file_size);
  2003. perf_output_end(&handle);
  2004. }
  2005. static int perf_counter_mmap_match(struct perf_counter *counter,
  2006. struct perf_mmap_event *mmap_event)
  2007. {
  2008. if (counter->hw_event.mmap &&
  2009. mmap_event->event.header.type == PERF_EVENT_MMAP)
  2010. return 1;
  2011. if (counter->hw_event.munmap &&
  2012. mmap_event->event.header.type == PERF_EVENT_MUNMAP)
  2013. return 1;
  2014. return 0;
  2015. }
  2016. static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
  2017. struct perf_mmap_event *mmap_event)
  2018. {
  2019. struct perf_counter *counter;
  2020. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  2021. return;
  2022. rcu_read_lock();
  2023. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  2024. if (perf_counter_mmap_match(counter, mmap_event))
  2025. perf_counter_mmap_output(counter, mmap_event);
  2026. }
  2027. rcu_read_unlock();
  2028. }
  2029. static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
  2030. {
  2031. struct perf_cpu_context *cpuctx;
  2032. struct file *file = mmap_event->file;
  2033. unsigned int size;
  2034. char tmp[16];
  2035. char *buf = NULL;
  2036. char *name;
  2037. if (file) {
  2038. buf = kzalloc(PATH_MAX, GFP_KERNEL);
  2039. if (!buf) {
  2040. name = strncpy(tmp, "//enomem", sizeof(tmp));
  2041. goto got_name;
  2042. }
  2043. name = d_path(&file->f_path, buf, PATH_MAX);
  2044. if (IS_ERR(name)) {
  2045. name = strncpy(tmp, "//toolong", sizeof(tmp));
  2046. goto got_name;
  2047. }
  2048. } else {
  2049. name = strncpy(tmp, "//anon", sizeof(tmp));
  2050. goto got_name;
  2051. }
  2052. got_name:
  2053. size = ALIGN(strlen(name)+1, sizeof(u64));
  2054. mmap_event->file_name = name;
  2055. mmap_event->file_size = size;
  2056. mmap_event->event.header.size = sizeof(mmap_event->event) + size;
  2057. cpuctx = &get_cpu_var(perf_cpu_context);
  2058. perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
  2059. put_cpu_var(perf_cpu_context);
  2060. perf_counter_mmap_ctx(current->perf_counter_ctxp, mmap_event);
  2061. kfree(buf);
  2062. }
  2063. void perf_counter_mmap(unsigned long addr, unsigned long len,
  2064. unsigned long pgoff, struct file *file)
  2065. {
  2066. struct perf_mmap_event mmap_event;
  2067. if (!atomic_read(&nr_mmap_tracking))
  2068. return;
  2069. if (!current->perf_counter_ctxp)
  2070. return;
  2071. mmap_event = (struct perf_mmap_event){
  2072. .file = file,
  2073. .event = {
  2074. .header = { .type = PERF_EVENT_MMAP, },
  2075. .pid = current->group_leader->pid,
  2076. .tid = current->pid,
  2077. .start = addr,
  2078. .len = len,
  2079. .pgoff = pgoff,
  2080. },
  2081. };
  2082. perf_counter_mmap_event(&mmap_event);
  2083. }
  2084. void perf_counter_munmap(unsigned long addr, unsigned long len,
  2085. unsigned long pgoff, struct file *file)
  2086. {
  2087. struct perf_mmap_event mmap_event;
  2088. if (!atomic_read(&nr_munmap_tracking))
  2089. return;
  2090. mmap_event = (struct perf_mmap_event){
  2091. .file = file,
  2092. .event = {
  2093. .header = { .type = PERF_EVENT_MUNMAP, },
  2094. .pid = current->group_leader->pid,
  2095. .tid = current->pid,
  2096. .start = addr,
  2097. .len = len,
  2098. .pgoff = pgoff,
  2099. },
  2100. };
  2101. perf_counter_mmap_event(&mmap_event);
  2102. }
  2103. /*
  2104. * Log irq_period changes so that analyzing tools can re-normalize the
  2105. * event flow.
  2106. */
  2107. static void perf_log_period(struct perf_counter *counter, u64 period)
  2108. {
  2109. struct perf_output_handle handle;
  2110. int ret;
  2111. struct {
  2112. struct perf_event_header header;
  2113. u64 time;
  2114. u64 period;
  2115. } freq_event = {
  2116. .header = {
  2117. .type = PERF_EVENT_PERIOD,
  2118. .misc = 0,
  2119. .size = sizeof(freq_event),
  2120. },
  2121. .time = sched_clock(),
  2122. .period = period,
  2123. };
  2124. if (counter->hw.irq_period == period)
  2125. return;
  2126. ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
  2127. if (ret)
  2128. return;
  2129. perf_output_put(&handle, freq_event);
  2130. perf_output_end(&handle);
  2131. }
  2132. /*
  2133. * Generic counter overflow handling.
  2134. */
  2135. int perf_counter_overflow(struct perf_counter *counter,
  2136. int nmi, struct pt_regs *regs, u64 addr)
  2137. {
  2138. int events = atomic_read(&counter->event_limit);
  2139. int ret = 0;
  2140. counter->hw.interrupts++;
  2141. /*
  2142. * XXX event_limit might not quite work as expected on inherited
  2143. * counters
  2144. */
  2145. counter->pending_kill = POLL_IN;
  2146. if (events && atomic_dec_and_test(&counter->event_limit)) {
  2147. ret = 1;
  2148. counter->pending_kill = POLL_HUP;
  2149. if (nmi) {
  2150. counter->pending_disable = 1;
  2151. perf_pending_queue(&counter->pending,
  2152. perf_pending_counter);
  2153. } else
  2154. perf_counter_disable(counter);
  2155. }
  2156. perf_counter_output(counter, nmi, regs, addr);
  2157. return ret;
  2158. }
  2159. /*
  2160. * Generic software counter infrastructure
  2161. */
  2162. static void perf_swcounter_update(struct perf_counter *counter)
  2163. {
  2164. struct hw_perf_counter *hwc = &counter->hw;
  2165. u64 prev, now;
  2166. s64 delta;
  2167. again:
  2168. prev = atomic64_read(&hwc->prev_count);
  2169. now = atomic64_read(&hwc->count);
  2170. if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
  2171. goto again;
  2172. delta = now - prev;
  2173. atomic64_add(delta, &counter->count);
  2174. atomic64_sub(delta, &hwc->period_left);
  2175. }
  2176. static void perf_swcounter_set_period(struct perf_counter *counter)
  2177. {
  2178. struct hw_perf_counter *hwc = &counter->hw;
  2179. s64 left = atomic64_read(&hwc->period_left);
  2180. s64 period = hwc->irq_period;
  2181. if (unlikely(left <= -period)) {
  2182. left = period;
  2183. atomic64_set(&hwc->period_left, left);
  2184. }
  2185. if (unlikely(left <= 0)) {
  2186. left += period;
  2187. atomic64_add(period, &hwc->period_left);
  2188. }
  2189. atomic64_set(&hwc->prev_count, -left);
  2190. atomic64_set(&hwc->count, -left);
  2191. }
  2192. static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
  2193. {
  2194. enum hrtimer_restart ret = HRTIMER_RESTART;
  2195. struct perf_counter *counter;
  2196. struct pt_regs *regs;
  2197. u64 period;
  2198. counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
  2199. counter->pmu->read(counter);
  2200. regs = get_irq_regs();
  2201. /*
  2202. * In case we exclude kernel IPs or are somehow not in interrupt
  2203. * context, provide the next best thing, the user IP.
  2204. */
  2205. if ((counter->hw_event.exclude_kernel || !regs) &&
  2206. !counter->hw_event.exclude_user)
  2207. regs = task_pt_regs(current);
  2208. if (regs) {
  2209. if (perf_counter_overflow(counter, 0, regs, 0))
  2210. ret = HRTIMER_NORESTART;
  2211. }
  2212. period = max_t(u64, 10000, counter->hw.irq_period);
  2213. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  2214. return ret;
  2215. }
  2216. static void perf_swcounter_overflow(struct perf_counter *counter,
  2217. int nmi, struct pt_regs *regs, u64 addr)
  2218. {
  2219. perf_swcounter_update(counter);
  2220. perf_swcounter_set_period(counter);
  2221. if (perf_counter_overflow(counter, nmi, regs, addr))
  2222. /* soft-disable the counter */
  2223. ;
  2224. }
  2225. static int perf_swcounter_match(struct perf_counter *counter,
  2226. enum perf_event_types type,
  2227. u32 event, struct pt_regs *regs)
  2228. {
  2229. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  2230. return 0;
  2231. if (perf_event_raw(&counter->hw_event))
  2232. return 0;
  2233. if (perf_event_type(&counter->hw_event) != type)
  2234. return 0;
  2235. if (perf_event_id(&counter->hw_event) != event)
  2236. return 0;
  2237. if (counter->hw_event.exclude_user && user_mode(regs))
  2238. return 0;
  2239. if (counter->hw_event.exclude_kernel && !user_mode(regs))
  2240. return 0;
  2241. return 1;
  2242. }
  2243. static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
  2244. int nmi, struct pt_regs *regs, u64 addr)
  2245. {
  2246. int neg = atomic64_add_negative(nr, &counter->hw.count);
  2247. if (counter->hw.irq_period && !neg)
  2248. perf_swcounter_overflow(counter, nmi, regs, addr);
  2249. }
  2250. static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
  2251. enum perf_event_types type, u32 event,
  2252. u64 nr, int nmi, struct pt_regs *regs,
  2253. u64 addr)
  2254. {
  2255. struct perf_counter *counter;
  2256. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  2257. return;
  2258. rcu_read_lock();
  2259. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  2260. if (perf_swcounter_match(counter, type, event, regs))
  2261. perf_swcounter_add(counter, nr, nmi, regs, addr);
  2262. }
  2263. rcu_read_unlock();
  2264. }
  2265. static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
  2266. {
  2267. if (in_nmi())
  2268. return &cpuctx->recursion[3];
  2269. if (in_irq())
  2270. return &cpuctx->recursion[2];
  2271. if (in_softirq())
  2272. return &cpuctx->recursion[1];
  2273. return &cpuctx->recursion[0];
  2274. }
  2275. static void __perf_swcounter_event(enum perf_event_types type, u32 event,
  2276. u64 nr, int nmi, struct pt_regs *regs,
  2277. u64 addr)
  2278. {
  2279. struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
  2280. int *recursion = perf_swcounter_recursion_context(cpuctx);
  2281. if (*recursion)
  2282. goto out;
  2283. (*recursion)++;
  2284. barrier();
  2285. perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
  2286. nr, nmi, regs, addr);
  2287. if (cpuctx->task_ctx) {
  2288. perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
  2289. nr, nmi, regs, addr);
  2290. }
  2291. barrier();
  2292. (*recursion)--;
  2293. out:
  2294. put_cpu_var(perf_cpu_context);
  2295. }
  2296. void
  2297. perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
  2298. {
  2299. __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
  2300. }
  2301. static void perf_swcounter_read(struct perf_counter *counter)
  2302. {
  2303. perf_swcounter_update(counter);
  2304. }
  2305. static int perf_swcounter_enable(struct perf_counter *counter)
  2306. {
  2307. perf_swcounter_set_period(counter);
  2308. return 0;
  2309. }
  2310. static void perf_swcounter_disable(struct perf_counter *counter)
  2311. {
  2312. perf_swcounter_update(counter);
  2313. }
  2314. static const struct pmu perf_ops_generic = {
  2315. .enable = perf_swcounter_enable,
  2316. .disable = perf_swcounter_disable,
  2317. .read = perf_swcounter_read,
  2318. };
  2319. /*
  2320. * Software counter: cpu wall time clock
  2321. */
  2322. static void cpu_clock_perf_counter_update(struct perf_counter *counter)
  2323. {
  2324. int cpu = raw_smp_processor_id();
  2325. s64 prev;
  2326. u64 now;
  2327. now = cpu_clock(cpu);
  2328. prev = atomic64_read(&counter->hw.prev_count);
  2329. atomic64_set(&counter->hw.prev_count, now);
  2330. atomic64_add(now - prev, &counter->count);
  2331. }
  2332. static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
  2333. {
  2334. struct hw_perf_counter *hwc = &counter->hw;
  2335. int cpu = raw_smp_processor_id();
  2336. atomic64_set(&hwc->prev_count, cpu_clock(cpu));
  2337. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2338. hwc->hrtimer.function = perf_swcounter_hrtimer;
  2339. if (hwc->irq_period) {
  2340. u64 period = max_t(u64, 10000, hwc->irq_period);
  2341. __hrtimer_start_range_ns(&hwc->hrtimer,
  2342. ns_to_ktime(period), 0,
  2343. HRTIMER_MODE_REL, 0);
  2344. }
  2345. return 0;
  2346. }
  2347. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  2348. {
  2349. if (counter->hw.irq_period)
  2350. hrtimer_cancel(&counter->hw.hrtimer);
  2351. cpu_clock_perf_counter_update(counter);
  2352. }
  2353. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  2354. {
  2355. cpu_clock_perf_counter_update(counter);
  2356. }
  2357. static const struct pmu perf_ops_cpu_clock = {
  2358. .enable = cpu_clock_perf_counter_enable,
  2359. .disable = cpu_clock_perf_counter_disable,
  2360. .read = cpu_clock_perf_counter_read,
  2361. };
  2362. /*
  2363. * Software counter: task time clock
  2364. */
  2365. static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
  2366. {
  2367. u64 prev;
  2368. s64 delta;
  2369. prev = atomic64_xchg(&counter->hw.prev_count, now);
  2370. delta = now - prev;
  2371. atomic64_add(delta, &counter->count);
  2372. }
  2373. static int task_clock_perf_counter_enable(struct perf_counter *counter)
  2374. {
  2375. struct hw_perf_counter *hwc = &counter->hw;
  2376. u64 now;
  2377. now = counter->ctx->time;
  2378. atomic64_set(&hwc->prev_count, now);
  2379. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2380. hwc->hrtimer.function = perf_swcounter_hrtimer;
  2381. if (hwc->irq_period) {
  2382. u64 period = max_t(u64, 10000, hwc->irq_period);
  2383. __hrtimer_start_range_ns(&hwc->hrtimer,
  2384. ns_to_ktime(period), 0,
  2385. HRTIMER_MODE_REL, 0);
  2386. }
  2387. return 0;
  2388. }
  2389. static void task_clock_perf_counter_disable(struct perf_counter *counter)
  2390. {
  2391. if (counter->hw.irq_period)
  2392. hrtimer_cancel(&counter->hw.hrtimer);
  2393. task_clock_perf_counter_update(counter, counter->ctx->time);
  2394. }
  2395. static void task_clock_perf_counter_read(struct perf_counter *counter)
  2396. {
  2397. u64 time;
  2398. if (!in_nmi()) {
  2399. update_context_time(counter->ctx);
  2400. time = counter->ctx->time;
  2401. } else {
  2402. u64 now = perf_clock();
  2403. u64 delta = now - counter->ctx->timestamp;
  2404. time = counter->ctx->time + delta;
  2405. }
  2406. task_clock_perf_counter_update(counter, time);
  2407. }
  2408. static const struct pmu perf_ops_task_clock = {
  2409. .enable = task_clock_perf_counter_enable,
  2410. .disable = task_clock_perf_counter_disable,
  2411. .read = task_clock_perf_counter_read,
  2412. };
  2413. /*
  2414. * Software counter: cpu migrations
  2415. */
  2416. static inline u64 get_cpu_migrations(struct perf_counter *counter)
  2417. {
  2418. struct task_struct *curr = counter->ctx->task;
  2419. if (curr)
  2420. return curr->se.nr_migrations;
  2421. return cpu_nr_migrations(smp_processor_id());
  2422. }
  2423. static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
  2424. {
  2425. u64 prev, now;
  2426. s64 delta;
  2427. prev = atomic64_read(&counter->hw.prev_count);
  2428. now = get_cpu_migrations(counter);
  2429. atomic64_set(&counter->hw.prev_count, now);
  2430. delta = now - prev;
  2431. atomic64_add(delta, &counter->count);
  2432. }
  2433. static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
  2434. {
  2435. cpu_migrations_perf_counter_update(counter);
  2436. }
  2437. static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
  2438. {
  2439. if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
  2440. atomic64_set(&counter->hw.prev_count,
  2441. get_cpu_migrations(counter));
  2442. return 0;
  2443. }
  2444. static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
  2445. {
  2446. cpu_migrations_perf_counter_update(counter);
  2447. }
  2448. static const struct pmu perf_ops_cpu_migrations = {
  2449. .enable = cpu_migrations_perf_counter_enable,
  2450. .disable = cpu_migrations_perf_counter_disable,
  2451. .read = cpu_migrations_perf_counter_read,
  2452. };
  2453. #ifdef CONFIG_EVENT_PROFILE
  2454. void perf_tpcounter_event(int event_id)
  2455. {
  2456. struct pt_regs *regs = get_irq_regs();
  2457. if (!regs)
  2458. regs = task_pt_regs(current);
  2459. __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
  2460. }
  2461. EXPORT_SYMBOL_GPL(perf_tpcounter_event);
  2462. extern int ftrace_profile_enable(int);
  2463. extern void ftrace_profile_disable(int);
  2464. static void tp_perf_counter_destroy(struct perf_counter *counter)
  2465. {
  2466. ftrace_profile_disable(perf_event_id(&counter->hw_event));
  2467. }
  2468. static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
  2469. {
  2470. int event_id = perf_event_id(&counter->hw_event);
  2471. int ret;
  2472. ret = ftrace_profile_enable(event_id);
  2473. if (ret)
  2474. return NULL;
  2475. counter->destroy = tp_perf_counter_destroy;
  2476. counter->hw.irq_period = counter->hw_event.irq_period;
  2477. return &perf_ops_generic;
  2478. }
  2479. #else
  2480. static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
  2481. {
  2482. return NULL;
  2483. }
  2484. #endif
  2485. static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
  2486. {
  2487. const struct pmu *pmu = NULL;
  2488. /*
  2489. * Software counters (currently) can't in general distinguish
  2490. * between user, kernel and hypervisor events.
  2491. * However, context switches and cpu migrations are considered
  2492. * to be kernel events, and page faults are never hypervisor
  2493. * events.
  2494. */
  2495. switch (perf_event_id(&counter->hw_event)) {
  2496. case PERF_COUNT_CPU_CLOCK:
  2497. pmu = &perf_ops_cpu_clock;
  2498. break;
  2499. case PERF_COUNT_TASK_CLOCK:
  2500. /*
  2501. * If the user instantiates this as a per-cpu counter,
  2502. * use the cpu_clock counter instead.
  2503. */
  2504. if (counter->ctx->task)
  2505. pmu = &perf_ops_task_clock;
  2506. else
  2507. pmu = &perf_ops_cpu_clock;
  2508. break;
  2509. case PERF_COUNT_PAGE_FAULTS:
  2510. case PERF_COUNT_PAGE_FAULTS_MIN:
  2511. case PERF_COUNT_PAGE_FAULTS_MAJ:
  2512. case PERF_COUNT_CONTEXT_SWITCHES:
  2513. pmu = &perf_ops_generic;
  2514. break;
  2515. case PERF_COUNT_CPU_MIGRATIONS:
  2516. if (!counter->hw_event.exclude_kernel)
  2517. pmu = &perf_ops_cpu_migrations;
  2518. break;
  2519. }
  2520. return pmu;
  2521. }
  2522. /*
  2523. * Allocate and initialize a counter structure
  2524. */
  2525. static struct perf_counter *
  2526. perf_counter_alloc(struct perf_counter_hw_event *hw_event,
  2527. int cpu,
  2528. struct perf_counter_context *ctx,
  2529. struct perf_counter *group_leader,
  2530. gfp_t gfpflags)
  2531. {
  2532. const struct pmu *pmu;
  2533. struct perf_counter *counter;
  2534. struct hw_perf_counter *hwc;
  2535. long err;
  2536. counter = kzalloc(sizeof(*counter), gfpflags);
  2537. if (!counter)
  2538. return ERR_PTR(-ENOMEM);
  2539. /*
  2540. * Single counters are their own group leaders, with an
  2541. * empty sibling list:
  2542. */
  2543. if (!group_leader)
  2544. group_leader = counter;
  2545. mutex_init(&counter->child_mutex);
  2546. INIT_LIST_HEAD(&counter->child_list);
  2547. INIT_LIST_HEAD(&counter->list_entry);
  2548. INIT_LIST_HEAD(&counter->event_entry);
  2549. INIT_LIST_HEAD(&counter->sibling_list);
  2550. init_waitqueue_head(&counter->waitq);
  2551. mutex_init(&counter->mmap_mutex);
  2552. counter->cpu = cpu;
  2553. counter->hw_event = *hw_event;
  2554. counter->group_leader = group_leader;
  2555. counter->pmu = NULL;
  2556. counter->ctx = ctx;
  2557. get_ctx(ctx);
  2558. counter->state = PERF_COUNTER_STATE_INACTIVE;
  2559. if (hw_event->disabled)
  2560. counter->state = PERF_COUNTER_STATE_OFF;
  2561. pmu = NULL;
  2562. hwc = &counter->hw;
  2563. if (hw_event->freq && hw_event->irq_freq)
  2564. hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
  2565. else
  2566. hwc->irq_period = hw_event->irq_period;
  2567. /*
  2568. * we currently do not support PERF_RECORD_GROUP on inherited counters
  2569. */
  2570. if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
  2571. goto done;
  2572. if (perf_event_raw(hw_event)) {
  2573. pmu = hw_perf_counter_init(counter);
  2574. goto done;
  2575. }
  2576. switch (perf_event_type(hw_event)) {
  2577. case PERF_TYPE_HARDWARE:
  2578. pmu = hw_perf_counter_init(counter);
  2579. break;
  2580. case PERF_TYPE_SOFTWARE:
  2581. pmu = sw_perf_counter_init(counter);
  2582. break;
  2583. case PERF_TYPE_TRACEPOINT:
  2584. pmu = tp_perf_counter_init(counter);
  2585. break;
  2586. }
  2587. done:
  2588. err = 0;
  2589. if (!pmu)
  2590. err = -EINVAL;
  2591. else if (IS_ERR(pmu))
  2592. err = PTR_ERR(pmu);
  2593. if (err) {
  2594. kfree(counter);
  2595. return ERR_PTR(err);
  2596. }
  2597. counter->pmu = pmu;
  2598. atomic_inc(&nr_counters);
  2599. if (counter->hw_event.mmap)
  2600. atomic_inc(&nr_mmap_tracking);
  2601. if (counter->hw_event.munmap)
  2602. atomic_inc(&nr_munmap_tracking);
  2603. if (counter->hw_event.comm)
  2604. atomic_inc(&nr_comm_tracking);
  2605. return counter;
  2606. }
  2607. /**
  2608. * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
  2609. *
  2610. * @hw_event_uptr: event type attributes for monitoring/sampling
  2611. * @pid: target pid
  2612. * @cpu: target cpu
  2613. * @group_fd: group leader counter fd
  2614. */
  2615. SYSCALL_DEFINE5(perf_counter_open,
  2616. const struct perf_counter_hw_event __user *, hw_event_uptr,
  2617. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  2618. {
  2619. struct perf_counter *counter, *group_leader;
  2620. struct perf_counter_hw_event hw_event;
  2621. struct perf_counter_context *ctx;
  2622. struct file *counter_file = NULL;
  2623. struct file *group_file = NULL;
  2624. int fput_needed = 0;
  2625. int fput_needed2 = 0;
  2626. int ret;
  2627. /* for future expandability... */
  2628. if (flags)
  2629. return -EINVAL;
  2630. if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
  2631. return -EFAULT;
  2632. /*
  2633. * Get the target context (task or percpu):
  2634. */
  2635. ctx = find_get_context(pid, cpu);
  2636. if (IS_ERR(ctx))
  2637. return PTR_ERR(ctx);
  2638. /*
  2639. * Look up the group leader (we will attach this counter to it):
  2640. */
  2641. group_leader = NULL;
  2642. if (group_fd != -1) {
  2643. ret = -EINVAL;
  2644. group_file = fget_light(group_fd, &fput_needed);
  2645. if (!group_file)
  2646. goto err_put_context;
  2647. if (group_file->f_op != &perf_fops)
  2648. goto err_put_context;
  2649. group_leader = group_file->private_data;
  2650. /*
  2651. * Do not allow a recursive hierarchy (this new sibling
  2652. * becoming part of another group-sibling):
  2653. */
  2654. if (group_leader->group_leader != group_leader)
  2655. goto err_put_context;
  2656. /*
  2657. * Do not allow to attach to a group in a different
  2658. * task or CPU context:
  2659. */
  2660. if (group_leader->ctx != ctx)
  2661. goto err_put_context;
  2662. /*
  2663. * Only a group leader can be exclusive or pinned
  2664. */
  2665. if (hw_event.exclusive || hw_event.pinned)
  2666. goto err_put_context;
  2667. }
  2668. counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
  2669. GFP_KERNEL);
  2670. ret = PTR_ERR(counter);
  2671. if (IS_ERR(counter))
  2672. goto err_put_context;
  2673. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  2674. if (ret < 0)
  2675. goto err_free_put_context;
  2676. counter_file = fget_light(ret, &fput_needed2);
  2677. if (!counter_file)
  2678. goto err_free_put_context;
  2679. counter->filp = counter_file;
  2680. mutex_lock(&ctx->mutex);
  2681. perf_install_in_context(ctx, counter, cpu);
  2682. mutex_unlock(&ctx->mutex);
  2683. fput_light(counter_file, fput_needed2);
  2684. out_fput:
  2685. fput_light(group_file, fput_needed);
  2686. return ret;
  2687. err_free_put_context:
  2688. kfree(counter);
  2689. err_put_context:
  2690. put_context(ctx);
  2691. goto out_fput;
  2692. }
  2693. /*
  2694. * inherit a counter from parent task to child task:
  2695. */
  2696. static struct perf_counter *
  2697. inherit_counter(struct perf_counter *parent_counter,
  2698. struct task_struct *parent,
  2699. struct perf_counter_context *parent_ctx,
  2700. struct task_struct *child,
  2701. struct perf_counter *group_leader,
  2702. struct perf_counter_context *child_ctx)
  2703. {
  2704. struct perf_counter *child_counter;
  2705. /*
  2706. * Instead of creating recursive hierarchies of counters,
  2707. * we link inherited counters back to the original parent,
  2708. * which has a filp for sure, which we use as the reference
  2709. * count:
  2710. */
  2711. if (parent_counter->parent)
  2712. parent_counter = parent_counter->parent;
  2713. child_counter = perf_counter_alloc(&parent_counter->hw_event,
  2714. parent_counter->cpu, child_ctx,
  2715. group_leader, GFP_KERNEL);
  2716. if (IS_ERR(child_counter))
  2717. return child_counter;
  2718. /*
  2719. * Make the child state follow the state of the parent counter,
  2720. * not its hw_event.disabled bit. We hold the parent's mutex,
  2721. * so we won't race with perf_counter_{en,dis}able_family.
  2722. */
  2723. if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
  2724. child_counter->state = PERF_COUNTER_STATE_INACTIVE;
  2725. else
  2726. child_counter->state = PERF_COUNTER_STATE_OFF;
  2727. /*
  2728. * Link it up in the child's context:
  2729. */
  2730. add_counter_to_ctx(child_counter, child_ctx);
  2731. child_counter->parent = parent_counter;
  2732. /*
  2733. * inherit into child's child as well:
  2734. */
  2735. child_counter->hw_event.inherit = 1;
  2736. /*
  2737. * Get a reference to the parent filp - we will fput it
  2738. * when the child counter exits. This is safe to do because
  2739. * we are in the parent and we know that the filp still
  2740. * exists and has a nonzero count:
  2741. */
  2742. atomic_long_inc(&parent_counter->filp->f_count);
  2743. /*
  2744. * Link this into the parent counter's child list
  2745. */
  2746. mutex_lock(&parent_counter->child_mutex);
  2747. list_add_tail(&child_counter->child_list, &parent_counter->child_list);
  2748. mutex_unlock(&parent_counter->child_mutex);
  2749. return child_counter;
  2750. }
  2751. static int inherit_group(struct perf_counter *parent_counter,
  2752. struct task_struct *parent,
  2753. struct perf_counter_context *parent_ctx,
  2754. struct task_struct *child,
  2755. struct perf_counter_context *child_ctx)
  2756. {
  2757. struct perf_counter *leader;
  2758. struct perf_counter *sub;
  2759. struct perf_counter *child_ctr;
  2760. leader = inherit_counter(parent_counter, parent, parent_ctx,
  2761. child, NULL, child_ctx);
  2762. if (IS_ERR(leader))
  2763. return PTR_ERR(leader);
  2764. list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
  2765. child_ctr = inherit_counter(sub, parent, parent_ctx,
  2766. child, leader, child_ctx);
  2767. if (IS_ERR(child_ctr))
  2768. return PTR_ERR(child_ctr);
  2769. }
  2770. return 0;
  2771. }
  2772. static void sync_child_counter(struct perf_counter *child_counter,
  2773. struct perf_counter *parent_counter)
  2774. {
  2775. u64 child_val;
  2776. child_val = atomic64_read(&child_counter->count);
  2777. /*
  2778. * Add back the child's count to the parent's count:
  2779. */
  2780. atomic64_add(child_val, &parent_counter->count);
  2781. atomic64_add(child_counter->total_time_enabled,
  2782. &parent_counter->child_total_time_enabled);
  2783. atomic64_add(child_counter->total_time_running,
  2784. &parent_counter->child_total_time_running);
  2785. /*
  2786. * Remove this counter from the parent's list
  2787. */
  2788. mutex_lock(&parent_counter->child_mutex);
  2789. list_del_init(&child_counter->child_list);
  2790. mutex_unlock(&parent_counter->child_mutex);
  2791. /*
  2792. * Release the parent counter, if this was the last
  2793. * reference to it.
  2794. */
  2795. fput(parent_counter->filp);
  2796. }
  2797. static void
  2798. __perf_counter_exit_task(struct task_struct *child,
  2799. struct perf_counter *child_counter,
  2800. struct perf_counter_context *child_ctx)
  2801. {
  2802. struct perf_counter *parent_counter;
  2803. update_counter_times(child_counter);
  2804. perf_counter_remove_from_context(child_counter);
  2805. parent_counter = child_counter->parent;
  2806. /*
  2807. * It can happen that parent exits first, and has counters
  2808. * that are still around due to the child reference. These
  2809. * counters need to be zapped - but otherwise linger.
  2810. */
  2811. if (parent_counter) {
  2812. sync_child_counter(child_counter, parent_counter);
  2813. free_counter(child_counter);
  2814. }
  2815. }
  2816. /*
  2817. * When a child task exits, feed back counter values to parent counters.
  2818. *
  2819. * Note: we may be running in child context, but the PID is not hashed
  2820. * anymore so new counters will not be added.
  2821. * (XXX not sure that is true when we get called from flush_old_exec.
  2822. * -- paulus)
  2823. */
  2824. void perf_counter_exit_task(struct task_struct *child)
  2825. {
  2826. struct perf_counter *child_counter, *tmp;
  2827. struct perf_counter_context *child_ctx;
  2828. unsigned long flags;
  2829. WARN_ON_ONCE(child != current);
  2830. child_ctx = child->perf_counter_ctxp;
  2831. if (likely(!child_ctx))
  2832. return;
  2833. local_irq_save(flags);
  2834. __perf_counter_task_sched_out(child_ctx);
  2835. child->perf_counter_ctxp = NULL;
  2836. local_irq_restore(flags);
  2837. mutex_lock(&child_ctx->mutex);
  2838. again:
  2839. list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
  2840. list_entry)
  2841. __perf_counter_exit_task(child, child_counter, child_ctx);
  2842. /*
  2843. * If the last counter was a group counter, it will have appended all
  2844. * its siblings to the list, but we obtained 'tmp' before that which
  2845. * will still point to the list head terminating the iteration.
  2846. */
  2847. if (!list_empty(&child_ctx->counter_list))
  2848. goto again;
  2849. mutex_unlock(&child_ctx->mutex);
  2850. put_ctx(child_ctx);
  2851. }
  2852. /*
  2853. * Initialize the perf_counter context in task_struct
  2854. */
  2855. void perf_counter_init_task(struct task_struct *child)
  2856. {
  2857. struct perf_counter_context *child_ctx, *parent_ctx;
  2858. struct perf_counter *counter;
  2859. struct task_struct *parent = current;
  2860. int inherited_all = 1;
  2861. child->perf_counter_ctxp = NULL;
  2862. /*
  2863. * This is executed from the parent task context, so inherit
  2864. * counters that have been marked for cloning.
  2865. * First allocate and initialize a context for the child.
  2866. */
  2867. child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
  2868. if (!child_ctx)
  2869. return;
  2870. parent_ctx = parent->perf_counter_ctxp;
  2871. if (likely(!parent_ctx || !parent_ctx->nr_counters))
  2872. return;
  2873. __perf_counter_init_context(child_ctx, child);
  2874. child->perf_counter_ctxp = child_ctx;
  2875. /*
  2876. * Lock the parent list. No need to lock the child - not PID
  2877. * hashed yet and not running, so nobody can access it.
  2878. */
  2879. mutex_lock(&parent_ctx->mutex);
  2880. /*
  2881. * We dont have to disable NMIs - we are only looking at
  2882. * the list, not manipulating it:
  2883. */
  2884. list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
  2885. if (counter != counter->group_leader)
  2886. continue;
  2887. if (!counter->hw_event.inherit) {
  2888. inherited_all = 0;
  2889. continue;
  2890. }
  2891. if (inherit_group(counter, parent,
  2892. parent_ctx, child, child_ctx)) {
  2893. inherited_all = 0;
  2894. break;
  2895. }
  2896. }
  2897. if (inherited_all) {
  2898. /*
  2899. * Mark the child context as a clone of the parent
  2900. * context, or of whatever the parent is a clone of.
  2901. */
  2902. if (parent_ctx->parent_ctx) {
  2903. child_ctx->parent_ctx = parent_ctx->parent_ctx;
  2904. child_ctx->parent_gen = parent_ctx->parent_gen;
  2905. } else {
  2906. child_ctx->parent_ctx = parent_ctx;
  2907. child_ctx->parent_gen = parent_ctx->generation;
  2908. }
  2909. get_ctx(child_ctx->parent_ctx);
  2910. }
  2911. mutex_unlock(&parent_ctx->mutex);
  2912. }
  2913. static void __cpuinit perf_counter_init_cpu(int cpu)
  2914. {
  2915. struct perf_cpu_context *cpuctx;
  2916. cpuctx = &per_cpu(perf_cpu_context, cpu);
  2917. __perf_counter_init_context(&cpuctx->ctx, NULL);
  2918. spin_lock(&perf_resource_lock);
  2919. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  2920. spin_unlock(&perf_resource_lock);
  2921. hw_perf_counter_setup(cpu);
  2922. }
  2923. #ifdef CONFIG_HOTPLUG_CPU
  2924. static void __perf_counter_exit_cpu(void *info)
  2925. {
  2926. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  2927. struct perf_counter_context *ctx = &cpuctx->ctx;
  2928. struct perf_counter *counter, *tmp;
  2929. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  2930. __perf_counter_remove_from_context(counter);
  2931. }
  2932. static void perf_counter_exit_cpu(int cpu)
  2933. {
  2934. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  2935. struct perf_counter_context *ctx = &cpuctx->ctx;
  2936. mutex_lock(&ctx->mutex);
  2937. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  2938. mutex_unlock(&ctx->mutex);
  2939. }
  2940. #else
  2941. static inline void perf_counter_exit_cpu(int cpu) { }
  2942. #endif
  2943. static int __cpuinit
  2944. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  2945. {
  2946. unsigned int cpu = (long)hcpu;
  2947. switch (action) {
  2948. case CPU_UP_PREPARE:
  2949. case CPU_UP_PREPARE_FROZEN:
  2950. perf_counter_init_cpu(cpu);
  2951. break;
  2952. case CPU_DOWN_PREPARE:
  2953. case CPU_DOWN_PREPARE_FROZEN:
  2954. perf_counter_exit_cpu(cpu);
  2955. break;
  2956. default:
  2957. break;
  2958. }
  2959. return NOTIFY_OK;
  2960. }
  2961. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  2962. .notifier_call = perf_cpu_notify,
  2963. };
  2964. void __init perf_counter_init(void)
  2965. {
  2966. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  2967. (void *)(long)smp_processor_id());
  2968. register_cpu_notifier(&perf_cpu_nb);
  2969. }
  2970. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  2971. {
  2972. return sprintf(buf, "%d\n", perf_reserved_percpu);
  2973. }
  2974. static ssize_t
  2975. perf_set_reserve_percpu(struct sysdev_class *class,
  2976. const char *buf,
  2977. size_t count)
  2978. {
  2979. struct perf_cpu_context *cpuctx;
  2980. unsigned long val;
  2981. int err, cpu, mpt;
  2982. err = strict_strtoul(buf, 10, &val);
  2983. if (err)
  2984. return err;
  2985. if (val > perf_max_counters)
  2986. return -EINVAL;
  2987. spin_lock(&perf_resource_lock);
  2988. perf_reserved_percpu = val;
  2989. for_each_online_cpu(cpu) {
  2990. cpuctx = &per_cpu(perf_cpu_context, cpu);
  2991. spin_lock_irq(&cpuctx->ctx.lock);
  2992. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  2993. perf_max_counters - perf_reserved_percpu);
  2994. cpuctx->max_pertask = mpt;
  2995. spin_unlock_irq(&cpuctx->ctx.lock);
  2996. }
  2997. spin_unlock(&perf_resource_lock);
  2998. return count;
  2999. }
  3000. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  3001. {
  3002. return sprintf(buf, "%d\n", perf_overcommit);
  3003. }
  3004. static ssize_t
  3005. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  3006. {
  3007. unsigned long val;
  3008. int err;
  3009. err = strict_strtoul(buf, 10, &val);
  3010. if (err)
  3011. return err;
  3012. if (val > 1)
  3013. return -EINVAL;
  3014. spin_lock(&perf_resource_lock);
  3015. perf_overcommit = val;
  3016. spin_unlock(&perf_resource_lock);
  3017. return count;
  3018. }
  3019. static SYSDEV_CLASS_ATTR(
  3020. reserve_percpu,
  3021. 0644,
  3022. perf_show_reserve_percpu,
  3023. perf_set_reserve_percpu
  3024. );
  3025. static SYSDEV_CLASS_ATTR(
  3026. overcommit,
  3027. 0644,
  3028. perf_show_overcommit,
  3029. perf_set_overcommit
  3030. );
  3031. static struct attribute *perfclass_attrs[] = {
  3032. &attr_reserve_percpu.attr,
  3033. &attr_overcommit.attr,
  3034. NULL
  3035. };
  3036. static struct attribute_group perfclass_attr_group = {
  3037. .attrs = perfclass_attrs,
  3038. .name = "perf_counters",
  3039. };
  3040. static int __init perf_counter_sysfs_init(void)
  3041. {
  3042. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  3043. &perfclass_attr_group);
  3044. }
  3045. device_initcall(perf_counter_sysfs_init);