perf_counter.c 72 KB

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