perf_counter.c 82 KB

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