perf_counter.c 80 KB

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