perf_counter.c 77 KB

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