perf_counter.c 85 KB

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