perf_counter.c 86 KB

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