perf_counter.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212
  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. * For licencing details see kernel-base/COPYING
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/cpu.h>
  11. #include <linux/smp.h>
  12. #include <linux/file.h>
  13. #include <linux/poll.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/percpu.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/anon_inodes.h>
  20. #include <linux/kernel_stat.h>
  21. #include <linux/perf_counter.h>
  22. #include <linux/mm.h>
  23. #include <linux/vmstat.h>
  24. /*
  25. * Each CPU has a list of per CPU counters:
  26. */
  27. DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  28. int perf_max_counters __read_mostly = 1;
  29. static int perf_reserved_percpu __read_mostly;
  30. static int perf_overcommit __read_mostly = 1;
  31. /*
  32. * Mutex for (sysadmin-configurable) counter reservations:
  33. */
  34. static DEFINE_MUTEX(perf_resource_mutex);
  35. /*
  36. * Architecture provided APIs - weak aliases:
  37. */
  38. extern __weak const struct hw_perf_counter_ops *
  39. hw_perf_counter_init(struct perf_counter *counter)
  40. {
  41. return NULL;
  42. }
  43. u64 __weak hw_perf_save_disable(void) { return 0; }
  44. void __weak hw_perf_restore(u64 ctrl) { barrier(); }
  45. void __weak hw_perf_counter_setup(int cpu) { barrier(); }
  46. int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
  47. struct perf_cpu_context *cpuctx,
  48. struct perf_counter_context *ctx, int cpu)
  49. {
  50. return 0;
  51. }
  52. void __weak perf_counter_print_debug(void) { }
  53. static void
  54. list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  55. {
  56. struct perf_counter *group_leader = counter->group_leader;
  57. /*
  58. * Depending on whether it is a standalone or sibling counter,
  59. * add it straight to the context's counter list, or to the group
  60. * leader's sibling list:
  61. */
  62. if (counter->group_leader == counter)
  63. list_add_tail(&counter->list_entry, &ctx->counter_list);
  64. else
  65. list_add_tail(&counter->list_entry, &group_leader->sibling_list);
  66. }
  67. static void
  68. list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  69. {
  70. struct perf_counter *sibling, *tmp;
  71. list_del_init(&counter->list_entry);
  72. /*
  73. * If this was a group counter with sibling counters then
  74. * upgrade the siblings to singleton counters by adding them
  75. * to the context list directly:
  76. */
  77. list_for_each_entry_safe(sibling, tmp,
  78. &counter->sibling_list, list_entry) {
  79. list_del_init(&sibling->list_entry);
  80. list_add_tail(&sibling->list_entry, &ctx->counter_list);
  81. sibling->group_leader = sibling;
  82. }
  83. }
  84. static void
  85. counter_sched_out(struct perf_counter *counter,
  86. struct perf_cpu_context *cpuctx,
  87. struct perf_counter_context *ctx)
  88. {
  89. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  90. return;
  91. counter->state = PERF_COUNTER_STATE_INACTIVE;
  92. counter->hw_ops->disable(counter);
  93. counter->oncpu = -1;
  94. if (!is_software_counter(counter))
  95. cpuctx->active_oncpu--;
  96. ctx->nr_active--;
  97. if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
  98. cpuctx->exclusive = 0;
  99. }
  100. static void
  101. group_sched_out(struct perf_counter *group_counter,
  102. struct perf_cpu_context *cpuctx,
  103. struct perf_counter_context *ctx)
  104. {
  105. struct perf_counter *counter;
  106. if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
  107. return;
  108. counter_sched_out(group_counter, cpuctx, ctx);
  109. /*
  110. * Schedule out siblings (if any):
  111. */
  112. list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
  113. counter_sched_out(counter, cpuctx, ctx);
  114. if (group_counter->hw_event.exclusive)
  115. cpuctx->exclusive = 0;
  116. }
  117. /*
  118. * Cross CPU call to remove a performance counter
  119. *
  120. * We disable the counter on the hardware level first. After that we
  121. * remove it from the context list.
  122. */
  123. static void __perf_counter_remove_from_context(void *info)
  124. {
  125. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  126. struct perf_counter *counter = info;
  127. struct perf_counter_context *ctx = counter->ctx;
  128. unsigned long flags;
  129. u64 perf_flags;
  130. /*
  131. * If this is a task context, we need to check whether it is
  132. * the current task context of this cpu. If not it has been
  133. * scheduled out before the smp call arrived.
  134. */
  135. if (ctx->task && cpuctx->task_ctx != ctx)
  136. return;
  137. curr_rq_lock_irq_save(&flags);
  138. spin_lock(&ctx->lock);
  139. counter_sched_out(counter, cpuctx, ctx);
  140. counter->task = NULL;
  141. ctx->nr_counters--;
  142. /*
  143. * Protect the list operation against NMI by disabling the
  144. * counters on a global level. NOP for non NMI based counters.
  145. */
  146. perf_flags = hw_perf_save_disable();
  147. list_del_counter(counter, ctx);
  148. hw_perf_restore(perf_flags);
  149. if (!ctx->task) {
  150. /*
  151. * Allow more per task counters with respect to the
  152. * reservation:
  153. */
  154. cpuctx->max_pertask =
  155. min(perf_max_counters - ctx->nr_counters,
  156. perf_max_counters - perf_reserved_percpu);
  157. }
  158. spin_unlock(&ctx->lock);
  159. curr_rq_unlock_irq_restore(&flags);
  160. }
  161. /*
  162. * Remove the counter from a task's (or a CPU's) list of counters.
  163. *
  164. * Must be called with counter->mutex and ctx->mutex held.
  165. *
  166. * CPU counters are removed with a smp call. For task counters we only
  167. * call when the task is on a CPU.
  168. */
  169. static void perf_counter_remove_from_context(struct perf_counter *counter)
  170. {
  171. struct perf_counter_context *ctx = counter->ctx;
  172. struct task_struct *task = ctx->task;
  173. if (!task) {
  174. /*
  175. * Per cpu counters are removed via an smp call and
  176. * the removal is always sucessful.
  177. */
  178. smp_call_function_single(counter->cpu,
  179. __perf_counter_remove_from_context,
  180. counter, 1);
  181. return;
  182. }
  183. retry:
  184. task_oncpu_function_call(task, __perf_counter_remove_from_context,
  185. counter);
  186. spin_lock_irq(&ctx->lock);
  187. /*
  188. * If the context is active we need to retry the smp call.
  189. */
  190. if (ctx->nr_active && !list_empty(&counter->list_entry)) {
  191. spin_unlock_irq(&ctx->lock);
  192. goto retry;
  193. }
  194. /*
  195. * The lock prevents that this context is scheduled in so we
  196. * can remove the counter safely, if the call above did not
  197. * succeed.
  198. */
  199. if (!list_empty(&counter->list_entry)) {
  200. ctx->nr_counters--;
  201. list_del_counter(counter, ctx);
  202. counter->task = NULL;
  203. }
  204. spin_unlock_irq(&ctx->lock);
  205. }
  206. /*
  207. * Cross CPU call to disable a performance counter
  208. */
  209. static void __perf_counter_disable(void *info)
  210. {
  211. struct perf_counter *counter = info;
  212. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  213. struct perf_counter_context *ctx = counter->ctx;
  214. unsigned long flags;
  215. /*
  216. * If this is a per-task counter, need to check whether this
  217. * counter's task is the current task on this cpu.
  218. */
  219. if (ctx->task && cpuctx->task_ctx != ctx)
  220. return;
  221. curr_rq_lock_irq_save(&flags);
  222. spin_lock(&ctx->lock);
  223. /*
  224. * If the counter is on, turn it off.
  225. * If it is in error state, leave it in error state.
  226. */
  227. if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
  228. if (counter == counter->group_leader)
  229. group_sched_out(counter, cpuctx, ctx);
  230. else
  231. counter_sched_out(counter, cpuctx, ctx);
  232. counter->state = PERF_COUNTER_STATE_OFF;
  233. }
  234. spin_unlock(&ctx->lock);
  235. curr_rq_unlock_irq_restore(&flags);
  236. }
  237. /*
  238. * Disable a counter.
  239. */
  240. static void perf_counter_disable(struct perf_counter *counter)
  241. {
  242. struct perf_counter_context *ctx = counter->ctx;
  243. struct task_struct *task = ctx->task;
  244. if (!task) {
  245. /*
  246. * Disable the counter on the cpu that it's on
  247. */
  248. smp_call_function_single(counter->cpu, __perf_counter_disable,
  249. counter, 1);
  250. return;
  251. }
  252. retry:
  253. task_oncpu_function_call(task, __perf_counter_disable, counter);
  254. spin_lock_irq(&ctx->lock);
  255. /*
  256. * If the counter is still active, we need to retry the cross-call.
  257. */
  258. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  259. spin_unlock_irq(&ctx->lock);
  260. goto retry;
  261. }
  262. /*
  263. * Since we have the lock this context can't be scheduled
  264. * in, so we can change the state safely.
  265. */
  266. if (counter->state == PERF_COUNTER_STATE_INACTIVE)
  267. counter->state = PERF_COUNTER_STATE_OFF;
  268. spin_unlock_irq(&ctx->lock);
  269. }
  270. /*
  271. * Disable a counter and all its children.
  272. */
  273. static void perf_counter_disable_family(struct perf_counter *counter)
  274. {
  275. struct perf_counter *child;
  276. perf_counter_disable(counter);
  277. /*
  278. * Lock the mutex to protect the list of children
  279. */
  280. mutex_lock(&counter->mutex);
  281. list_for_each_entry(child, &counter->child_list, child_list)
  282. perf_counter_disable(child);
  283. mutex_unlock(&counter->mutex);
  284. }
  285. static int
  286. counter_sched_in(struct perf_counter *counter,
  287. struct perf_cpu_context *cpuctx,
  288. struct perf_counter_context *ctx,
  289. int cpu)
  290. {
  291. if (counter->state <= PERF_COUNTER_STATE_OFF)
  292. return 0;
  293. counter->state = PERF_COUNTER_STATE_ACTIVE;
  294. counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
  295. /*
  296. * The new state must be visible before we turn it on in the hardware:
  297. */
  298. smp_wmb();
  299. if (counter->hw_ops->enable(counter)) {
  300. counter->state = PERF_COUNTER_STATE_INACTIVE;
  301. counter->oncpu = -1;
  302. return -EAGAIN;
  303. }
  304. if (!is_software_counter(counter))
  305. cpuctx->active_oncpu++;
  306. ctx->nr_active++;
  307. if (counter->hw_event.exclusive)
  308. cpuctx->exclusive = 1;
  309. return 0;
  310. }
  311. /*
  312. * Return 1 for a group consisting entirely of software counters,
  313. * 0 if the group contains any hardware counters.
  314. */
  315. static int is_software_only_group(struct perf_counter *leader)
  316. {
  317. struct perf_counter *counter;
  318. if (!is_software_counter(leader))
  319. return 0;
  320. list_for_each_entry(counter, &leader->sibling_list, list_entry)
  321. if (!is_software_counter(counter))
  322. return 0;
  323. return 1;
  324. }
  325. /*
  326. * Work out whether we can put this counter group on the CPU now.
  327. */
  328. static int group_can_go_on(struct perf_counter *counter,
  329. struct perf_cpu_context *cpuctx,
  330. int can_add_hw)
  331. {
  332. /*
  333. * Groups consisting entirely of software counters can always go on.
  334. */
  335. if (is_software_only_group(counter))
  336. return 1;
  337. /*
  338. * If an exclusive group is already on, no other hardware
  339. * counters can go on.
  340. */
  341. if (cpuctx->exclusive)
  342. return 0;
  343. /*
  344. * If this group is exclusive and there are already
  345. * counters on the CPU, it can't go on.
  346. */
  347. if (counter->hw_event.exclusive && cpuctx->active_oncpu)
  348. return 0;
  349. /*
  350. * Otherwise, try to add it if all previous groups were able
  351. * to go on.
  352. */
  353. return can_add_hw;
  354. }
  355. /*
  356. * Cross CPU call to install and enable a performance counter
  357. */
  358. static void __perf_install_in_context(void *info)
  359. {
  360. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  361. struct perf_counter *counter = info;
  362. struct perf_counter_context *ctx = counter->ctx;
  363. struct perf_counter *leader = counter->group_leader;
  364. int cpu = smp_processor_id();
  365. unsigned long flags;
  366. u64 perf_flags;
  367. int err;
  368. /*
  369. * If this is a task context, we need to check whether it is
  370. * the current task context of this cpu. If not it has been
  371. * scheduled out before the smp call arrived.
  372. */
  373. if (ctx->task && cpuctx->task_ctx != ctx)
  374. return;
  375. curr_rq_lock_irq_save(&flags);
  376. spin_lock(&ctx->lock);
  377. /*
  378. * Protect the list operation against NMI by disabling the
  379. * counters on a global level. NOP for non NMI based counters.
  380. */
  381. perf_flags = hw_perf_save_disable();
  382. list_add_counter(counter, ctx);
  383. ctx->nr_counters++;
  384. counter->prev_state = PERF_COUNTER_STATE_OFF;
  385. /*
  386. * Don't put the counter on if it is disabled or if
  387. * it is in a group and the group isn't on.
  388. */
  389. if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
  390. (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
  391. goto unlock;
  392. /*
  393. * An exclusive counter can't go on if there are already active
  394. * hardware counters, and no hardware counter can go on if there
  395. * is already an exclusive counter on.
  396. */
  397. if (!group_can_go_on(counter, cpuctx, 1))
  398. err = -EEXIST;
  399. else
  400. err = counter_sched_in(counter, cpuctx, ctx, cpu);
  401. if (err) {
  402. /*
  403. * This counter couldn't go on. If it is in a group
  404. * then we have to pull the whole group off.
  405. * If the counter group is pinned then put it in error state.
  406. */
  407. if (leader != counter)
  408. group_sched_out(leader, cpuctx, ctx);
  409. if (leader->hw_event.pinned)
  410. leader->state = PERF_COUNTER_STATE_ERROR;
  411. }
  412. if (!err && !ctx->task && cpuctx->max_pertask)
  413. cpuctx->max_pertask--;
  414. unlock:
  415. hw_perf_restore(perf_flags);
  416. spin_unlock(&ctx->lock);
  417. curr_rq_unlock_irq_restore(&flags);
  418. }
  419. /*
  420. * Attach a performance counter to a context
  421. *
  422. * First we add the counter to the list with the hardware enable bit
  423. * in counter->hw_config cleared.
  424. *
  425. * If the counter is attached to a task which is on a CPU we use a smp
  426. * call to enable it in the task context. The task might have been
  427. * scheduled away, but we check this in the smp call again.
  428. *
  429. * Must be called with ctx->mutex held.
  430. */
  431. static void
  432. perf_install_in_context(struct perf_counter_context *ctx,
  433. struct perf_counter *counter,
  434. int cpu)
  435. {
  436. struct task_struct *task = ctx->task;
  437. if (!task) {
  438. /*
  439. * Per cpu counters are installed via an smp call and
  440. * the install is always sucessful.
  441. */
  442. smp_call_function_single(cpu, __perf_install_in_context,
  443. counter, 1);
  444. return;
  445. }
  446. counter->task = task;
  447. retry:
  448. task_oncpu_function_call(task, __perf_install_in_context,
  449. counter);
  450. spin_lock_irq(&ctx->lock);
  451. /*
  452. * we need to retry the smp call.
  453. */
  454. if (ctx->is_active && list_empty(&counter->list_entry)) {
  455. spin_unlock_irq(&ctx->lock);
  456. goto retry;
  457. }
  458. /*
  459. * The lock prevents that this context is scheduled in so we
  460. * can add the counter safely, if it the call above did not
  461. * succeed.
  462. */
  463. if (list_empty(&counter->list_entry)) {
  464. list_add_counter(counter, ctx);
  465. ctx->nr_counters++;
  466. }
  467. spin_unlock_irq(&ctx->lock);
  468. }
  469. /*
  470. * Cross CPU call to enable a performance counter
  471. */
  472. static void __perf_counter_enable(void *info)
  473. {
  474. struct perf_counter *counter = info;
  475. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  476. struct perf_counter_context *ctx = counter->ctx;
  477. struct perf_counter *leader = counter->group_leader;
  478. unsigned long flags;
  479. int err;
  480. /*
  481. * If this is a per-task counter, need to check whether this
  482. * counter's task is the current task on this cpu.
  483. */
  484. if (ctx->task && cpuctx->task_ctx != ctx)
  485. return;
  486. curr_rq_lock_irq_save(&flags);
  487. spin_lock(&ctx->lock);
  488. counter->prev_state = counter->state;
  489. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  490. goto unlock;
  491. counter->state = PERF_COUNTER_STATE_INACTIVE;
  492. /*
  493. * If the counter is in a group and isn't the group leader,
  494. * then don't put it on unless the group is on.
  495. */
  496. if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
  497. goto unlock;
  498. if (!group_can_go_on(counter, cpuctx, 1))
  499. err = -EEXIST;
  500. else
  501. err = counter_sched_in(counter, cpuctx, ctx,
  502. smp_processor_id());
  503. if (err) {
  504. /*
  505. * If this counter can't go on and it's part of a
  506. * group, then the whole group has to come off.
  507. */
  508. if (leader != counter)
  509. group_sched_out(leader, cpuctx, ctx);
  510. if (leader->hw_event.pinned)
  511. leader->state = PERF_COUNTER_STATE_ERROR;
  512. }
  513. unlock:
  514. spin_unlock(&ctx->lock);
  515. curr_rq_unlock_irq_restore(&flags);
  516. }
  517. /*
  518. * Enable a counter.
  519. */
  520. static void perf_counter_enable(struct perf_counter *counter)
  521. {
  522. struct perf_counter_context *ctx = counter->ctx;
  523. struct task_struct *task = ctx->task;
  524. if (!task) {
  525. /*
  526. * Enable the counter on the cpu that it's on
  527. */
  528. smp_call_function_single(counter->cpu, __perf_counter_enable,
  529. counter, 1);
  530. return;
  531. }
  532. spin_lock_irq(&ctx->lock);
  533. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  534. goto out;
  535. /*
  536. * If the counter is in error state, clear that first.
  537. * That way, if we see the counter in error state below, we
  538. * know that it has gone back into error state, as distinct
  539. * from the task having been scheduled away before the
  540. * cross-call arrived.
  541. */
  542. if (counter->state == PERF_COUNTER_STATE_ERROR)
  543. counter->state = PERF_COUNTER_STATE_OFF;
  544. retry:
  545. spin_unlock_irq(&ctx->lock);
  546. task_oncpu_function_call(task, __perf_counter_enable, counter);
  547. spin_lock_irq(&ctx->lock);
  548. /*
  549. * If the context is active and the counter is still off,
  550. * we need to retry the cross-call.
  551. */
  552. if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
  553. goto retry;
  554. /*
  555. * Since we have the lock this context can't be scheduled
  556. * in, so we can change the state safely.
  557. */
  558. if (counter->state == PERF_COUNTER_STATE_OFF)
  559. counter->state = PERF_COUNTER_STATE_INACTIVE;
  560. out:
  561. spin_unlock_irq(&ctx->lock);
  562. }
  563. /*
  564. * Enable a counter and all its children.
  565. */
  566. static void perf_counter_enable_family(struct perf_counter *counter)
  567. {
  568. struct perf_counter *child;
  569. perf_counter_enable(counter);
  570. /*
  571. * Lock the mutex to protect the list of children
  572. */
  573. mutex_lock(&counter->mutex);
  574. list_for_each_entry(child, &counter->child_list, child_list)
  575. perf_counter_enable(child);
  576. mutex_unlock(&counter->mutex);
  577. }
  578. void __perf_counter_sched_out(struct perf_counter_context *ctx,
  579. struct perf_cpu_context *cpuctx)
  580. {
  581. struct perf_counter *counter;
  582. u64 flags;
  583. spin_lock(&ctx->lock);
  584. ctx->is_active = 0;
  585. if (likely(!ctx->nr_counters))
  586. goto out;
  587. flags = hw_perf_save_disable();
  588. if (ctx->nr_active) {
  589. list_for_each_entry(counter, &ctx->counter_list, list_entry)
  590. group_sched_out(counter, cpuctx, ctx);
  591. }
  592. hw_perf_restore(flags);
  593. out:
  594. spin_unlock(&ctx->lock);
  595. }
  596. /*
  597. * Called from scheduler to remove the counters of the current task,
  598. * with interrupts disabled.
  599. *
  600. * We stop each counter and update the counter value in counter->count.
  601. *
  602. * This does not protect us against NMI, but disable()
  603. * sets the disabled bit in the control field of counter _before_
  604. * accessing the counter control register. If a NMI hits, then it will
  605. * not restart the counter.
  606. */
  607. void perf_counter_task_sched_out(struct task_struct *task, int cpu)
  608. {
  609. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  610. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  611. if (likely(!cpuctx->task_ctx))
  612. return;
  613. __perf_counter_sched_out(ctx, cpuctx);
  614. cpuctx->task_ctx = NULL;
  615. }
  616. static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
  617. {
  618. __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
  619. }
  620. static int
  621. group_sched_in(struct perf_counter *group_counter,
  622. struct perf_cpu_context *cpuctx,
  623. struct perf_counter_context *ctx,
  624. int cpu)
  625. {
  626. struct perf_counter *counter, *partial_group;
  627. int ret;
  628. if (group_counter->state == PERF_COUNTER_STATE_OFF)
  629. return 0;
  630. ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
  631. if (ret)
  632. return ret < 0 ? ret : 0;
  633. group_counter->prev_state = group_counter->state;
  634. if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
  635. return -EAGAIN;
  636. /*
  637. * Schedule in siblings as one group (if any):
  638. */
  639. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  640. counter->prev_state = counter->state;
  641. if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
  642. partial_group = counter;
  643. goto group_error;
  644. }
  645. }
  646. return 0;
  647. group_error:
  648. /*
  649. * Groups can be scheduled in as one unit only, so undo any
  650. * partial group before returning:
  651. */
  652. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  653. if (counter == partial_group)
  654. break;
  655. counter_sched_out(counter, cpuctx, ctx);
  656. }
  657. counter_sched_out(group_counter, cpuctx, ctx);
  658. return -EAGAIN;
  659. }
  660. static void
  661. __perf_counter_sched_in(struct perf_counter_context *ctx,
  662. struct perf_cpu_context *cpuctx, int cpu)
  663. {
  664. struct perf_counter *counter;
  665. u64 flags;
  666. int can_add_hw = 1;
  667. spin_lock(&ctx->lock);
  668. ctx->is_active = 1;
  669. if (likely(!ctx->nr_counters))
  670. goto out;
  671. flags = hw_perf_save_disable();
  672. /*
  673. * First go through the list and put on any pinned groups
  674. * in order to give them the best chance of going on.
  675. */
  676. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  677. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  678. !counter->hw_event.pinned)
  679. continue;
  680. if (counter->cpu != -1 && counter->cpu != cpu)
  681. continue;
  682. if (group_can_go_on(counter, cpuctx, 1))
  683. group_sched_in(counter, cpuctx, ctx, cpu);
  684. /*
  685. * If this pinned group hasn't been scheduled,
  686. * put it in error state.
  687. */
  688. if (counter->state == PERF_COUNTER_STATE_INACTIVE)
  689. counter->state = PERF_COUNTER_STATE_ERROR;
  690. }
  691. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  692. /*
  693. * Ignore counters in OFF or ERROR state, and
  694. * ignore pinned counters since we did them already.
  695. */
  696. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  697. counter->hw_event.pinned)
  698. continue;
  699. /*
  700. * Listen to the 'cpu' scheduling filter constraint
  701. * of counters:
  702. */
  703. if (counter->cpu != -1 && counter->cpu != cpu)
  704. continue;
  705. if (group_can_go_on(counter, cpuctx, can_add_hw)) {
  706. if (group_sched_in(counter, cpuctx, ctx, cpu))
  707. can_add_hw = 0;
  708. }
  709. }
  710. hw_perf_restore(flags);
  711. out:
  712. spin_unlock(&ctx->lock);
  713. }
  714. /*
  715. * Called from scheduler to add the counters of the current task
  716. * with interrupts disabled.
  717. *
  718. * We restore the counter value and then enable it.
  719. *
  720. * This does not protect us against NMI, but enable()
  721. * sets the enabled bit in the control field of counter _before_
  722. * accessing the counter control register. If a NMI hits, then it will
  723. * keep the counter running.
  724. */
  725. void perf_counter_task_sched_in(struct task_struct *task, int cpu)
  726. {
  727. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  728. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  729. __perf_counter_sched_in(ctx, cpuctx, cpu);
  730. cpuctx->task_ctx = ctx;
  731. }
  732. static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
  733. {
  734. struct perf_counter_context *ctx = &cpuctx->ctx;
  735. __perf_counter_sched_in(ctx, cpuctx, cpu);
  736. }
  737. int perf_counter_task_disable(void)
  738. {
  739. struct task_struct *curr = current;
  740. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  741. struct perf_counter *counter;
  742. unsigned long flags;
  743. u64 perf_flags;
  744. int cpu;
  745. if (likely(!ctx->nr_counters))
  746. return 0;
  747. curr_rq_lock_irq_save(&flags);
  748. cpu = smp_processor_id();
  749. /* force the update of the task clock: */
  750. __task_delta_exec(curr, 1);
  751. perf_counter_task_sched_out(curr, cpu);
  752. spin_lock(&ctx->lock);
  753. /*
  754. * Disable all the counters:
  755. */
  756. perf_flags = hw_perf_save_disable();
  757. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  758. if (counter->state != PERF_COUNTER_STATE_ERROR)
  759. counter->state = PERF_COUNTER_STATE_OFF;
  760. }
  761. hw_perf_restore(perf_flags);
  762. spin_unlock(&ctx->lock);
  763. curr_rq_unlock_irq_restore(&flags);
  764. return 0;
  765. }
  766. int perf_counter_task_enable(void)
  767. {
  768. struct task_struct *curr = current;
  769. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  770. struct perf_counter *counter;
  771. unsigned long flags;
  772. u64 perf_flags;
  773. int cpu;
  774. if (likely(!ctx->nr_counters))
  775. return 0;
  776. curr_rq_lock_irq_save(&flags);
  777. cpu = smp_processor_id();
  778. /* force the update of the task clock: */
  779. __task_delta_exec(curr, 1);
  780. perf_counter_task_sched_out(curr, cpu);
  781. spin_lock(&ctx->lock);
  782. /*
  783. * Disable all the counters:
  784. */
  785. perf_flags = hw_perf_save_disable();
  786. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  787. if (counter->state > PERF_COUNTER_STATE_OFF)
  788. continue;
  789. counter->state = PERF_COUNTER_STATE_INACTIVE;
  790. counter->hw_event.disabled = 0;
  791. }
  792. hw_perf_restore(perf_flags);
  793. spin_unlock(&ctx->lock);
  794. perf_counter_task_sched_in(curr, cpu);
  795. curr_rq_unlock_irq_restore(&flags);
  796. return 0;
  797. }
  798. /*
  799. * Round-robin a context's counters:
  800. */
  801. static void rotate_ctx(struct perf_counter_context *ctx)
  802. {
  803. struct perf_counter *counter;
  804. u64 perf_flags;
  805. if (!ctx->nr_counters)
  806. return;
  807. spin_lock(&ctx->lock);
  808. /*
  809. * Rotate the first entry last (works just fine for group counters too):
  810. */
  811. perf_flags = hw_perf_save_disable();
  812. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  813. list_del(&counter->list_entry);
  814. list_add_tail(&counter->list_entry, &ctx->counter_list);
  815. break;
  816. }
  817. hw_perf_restore(perf_flags);
  818. spin_unlock(&ctx->lock);
  819. }
  820. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  821. {
  822. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  823. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  824. const int rotate_percpu = 0;
  825. if (rotate_percpu)
  826. perf_counter_cpu_sched_out(cpuctx);
  827. perf_counter_task_sched_out(curr, cpu);
  828. if (rotate_percpu)
  829. rotate_ctx(&cpuctx->ctx);
  830. rotate_ctx(ctx);
  831. if (rotate_percpu)
  832. perf_counter_cpu_sched_in(cpuctx, cpu);
  833. perf_counter_task_sched_in(curr, cpu);
  834. }
  835. /*
  836. * Cross CPU call to read the hardware counter
  837. */
  838. static void __read(void *info)
  839. {
  840. struct perf_counter *counter = info;
  841. unsigned long flags;
  842. curr_rq_lock_irq_save(&flags);
  843. counter->hw_ops->read(counter);
  844. curr_rq_unlock_irq_restore(&flags);
  845. }
  846. static u64 perf_counter_read(struct perf_counter *counter)
  847. {
  848. /*
  849. * If counter is enabled and currently active on a CPU, update the
  850. * value in the counter structure:
  851. */
  852. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  853. smp_call_function_single(counter->oncpu,
  854. __read, counter, 1);
  855. }
  856. return atomic64_read(&counter->count);
  857. }
  858. /*
  859. * Cross CPU call to switch performance data pointers
  860. */
  861. static void __perf_switch_irq_data(void *info)
  862. {
  863. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  864. struct perf_counter *counter = info;
  865. struct perf_counter_context *ctx = counter->ctx;
  866. struct perf_data *oldirqdata = counter->irqdata;
  867. /*
  868. * If this is a task context, we need to check whether it is
  869. * the current task context of this cpu. If not it has been
  870. * scheduled out before the smp call arrived.
  871. */
  872. if (ctx->task) {
  873. if (cpuctx->task_ctx != ctx)
  874. return;
  875. spin_lock(&ctx->lock);
  876. }
  877. /* Change the pointer NMI safe */
  878. atomic_long_set((atomic_long_t *)&counter->irqdata,
  879. (unsigned long) counter->usrdata);
  880. counter->usrdata = oldirqdata;
  881. if (ctx->task)
  882. spin_unlock(&ctx->lock);
  883. }
  884. static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
  885. {
  886. struct perf_counter_context *ctx = counter->ctx;
  887. struct perf_data *oldirqdata = counter->irqdata;
  888. struct task_struct *task = ctx->task;
  889. if (!task) {
  890. smp_call_function_single(counter->cpu,
  891. __perf_switch_irq_data,
  892. counter, 1);
  893. return counter->usrdata;
  894. }
  895. retry:
  896. spin_lock_irq(&ctx->lock);
  897. if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
  898. counter->irqdata = counter->usrdata;
  899. counter->usrdata = oldirqdata;
  900. spin_unlock_irq(&ctx->lock);
  901. return oldirqdata;
  902. }
  903. spin_unlock_irq(&ctx->lock);
  904. task_oncpu_function_call(task, __perf_switch_irq_data, counter);
  905. /* Might have failed, because task was scheduled out */
  906. if (counter->irqdata == oldirqdata)
  907. goto retry;
  908. return counter->usrdata;
  909. }
  910. static void put_context(struct perf_counter_context *ctx)
  911. {
  912. if (ctx->task)
  913. put_task_struct(ctx->task);
  914. }
  915. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  916. {
  917. struct perf_cpu_context *cpuctx;
  918. struct perf_counter_context *ctx;
  919. struct task_struct *task;
  920. /*
  921. * If cpu is not a wildcard then this is a percpu counter:
  922. */
  923. if (cpu != -1) {
  924. /* Must be root to operate on a CPU counter: */
  925. if (!capable(CAP_SYS_ADMIN))
  926. return ERR_PTR(-EACCES);
  927. if (cpu < 0 || cpu > num_possible_cpus())
  928. return ERR_PTR(-EINVAL);
  929. /*
  930. * We could be clever and allow to attach a counter to an
  931. * offline CPU and activate it when the CPU comes up, but
  932. * that's for later.
  933. */
  934. if (!cpu_isset(cpu, cpu_online_map))
  935. return ERR_PTR(-ENODEV);
  936. cpuctx = &per_cpu(perf_cpu_context, cpu);
  937. ctx = &cpuctx->ctx;
  938. return ctx;
  939. }
  940. rcu_read_lock();
  941. if (!pid)
  942. task = current;
  943. else
  944. task = find_task_by_vpid(pid);
  945. if (task)
  946. get_task_struct(task);
  947. rcu_read_unlock();
  948. if (!task)
  949. return ERR_PTR(-ESRCH);
  950. ctx = &task->perf_counter_ctx;
  951. ctx->task = task;
  952. /* Reuse ptrace permission checks for now. */
  953. if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
  954. put_context(ctx);
  955. return ERR_PTR(-EACCES);
  956. }
  957. return ctx;
  958. }
  959. /*
  960. * Called when the last reference to the file is gone.
  961. */
  962. static int perf_release(struct inode *inode, struct file *file)
  963. {
  964. struct perf_counter *counter = file->private_data;
  965. struct perf_counter_context *ctx = counter->ctx;
  966. file->private_data = NULL;
  967. mutex_lock(&ctx->mutex);
  968. mutex_lock(&counter->mutex);
  969. perf_counter_remove_from_context(counter);
  970. mutex_unlock(&counter->mutex);
  971. mutex_unlock(&ctx->mutex);
  972. kfree(counter);
  973. put_context(ctx);
  974. return 0;
  975. }
  976. /*
  977. * Read the performance counter - simple non blocking version for now
  978. */
  979. static ssize_t
  980. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  981. {
  982. u64 cntval;
  983. if (count != sizeof(cntval))
  984. return -EINVAL;
  985. /*
  986. * Return end-of-file for a read on a counter that is in
  987. * error state (i.e. because it was pinned but it couldn't be
  988. * scheduled on to the CPU at some point).
  989. */
  990. if (counter->state == PERF_COUNTER_STATE_ERROR)
  991. return 0;
  992. mutex_lock(&counter->mutex);
  993. cntval = perf_counter_read(counter);
  994. mutex_unlock(&counter->mutex);
  995. return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
  996. }
  997. static ssize_t
  998. perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
  999. {
  1000. if (!usrdata->len)
  1001. return 0;
  1002. count = min(count, (size_t)usrdata->len);
  1003. if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
  1004. return -EFAULT;
  1005. /* Adjust the counters */
  1006. usrdata->len -= count;
  1007. if (!usrdata->len)
  1008. usrdata->rd_idx = 0;
  1009. else
  1010. usrdata->rd_idx += count;
  1011. return count;
  1012. }
  1013. static ssize_t
  1014. perf_read_irq_data(struct perf_counter *counter,
  1015. char __user *buf,
  1016. size_t count,
  1017. int nonblocking)
  1018. {
  1019. struct perf_data *irqdata, *usrdata;
  1020. DECLARE_WAITQUEUE(wait, current);
  1021. ssize_t res, res2;
  1022. irqdata = counter->irqdata;
  1023. usrdata = counter->usrdata;
  1024. if (usrdata->len + irqdata->len >= count)
  1025. goto read_pending;
  1026. if (nonblocking)
  1027. return -EAGAIN;
  1028. spin_lock_irq(&counter->waitq.lock);
  1029. __add_wait_queue(&counter->waitq, &wait);
  1030. for (;;) {
  1031. set_current_state(TASK_INTERRUPTIBLE);
  1032. if (usrdata->len + irqdata->len >= count)
  1033. break;
  1034. if (signal_pending(current))
  1035. break;
  1036. if (counter->state == PERF_COUNTER_STATE_ERROR)
  1037. break;
  1038. spin_unlock_irq(&counter->waitq.lock);
  1039. schedule();
  1040. spin_lock_irq(&counter->waitq.lock);
  1041. }
  1042. __remove_wait_queue(&counter->waitq, &wait);
  1043. __set_current_state(TASK_RUNNING);
  1044. spin_unlock_irq(&counter->waitq.lock);
  1045. if (usrdata->len + irqdata->len < count &&
  1046. counter->state != PERF_COUNTER_STATE_ERROR)
  1047. return -ERESTARTSYS;
  1048. read_pending:
  1049. mutex_lock(&counter->mutex);
  1050. /* Drain pending data first: */
  1051. res = perf_copy_usrdata(usrdata, buf, count);
  1052. if (res < 0 || res == count)
  1053. goto out;
  1054. /* Switch irq buffer: */
  1055. usrdata = perf_switch_irq_data(counter);
  1056. res2 = perf_copy_usrdata(usrdata, buf + res, count - res);
  1057. if (res2 < 0) {
  1058. if (!res)
  1059. res = -EFAULT;
  1060. } else {
  1061. res += res2;
  1062. }
  1063. out:
  1064. mutex_unlock(&counter->mutex);
  1065. return res;
  1066. }
  1067. static ssize_t
  1068. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1069. {
  1070. struct perf_counter *counter = file->private_data;
  1071. switch (counter->hw_event.record_type) {
  1072. case PERF_RECORD_SIMPLE:
  1073. return perf_read_hw(counter, buf, count);
  1074. case PERF_RECORD_IRQ:
  1075. case PERF_RECORD_GROUP:
  1076. return perf_read_irq_data(counter, buf, count,
  1077. file->f_flags & O_NONBLOCK);
  1078. }
  1079. return -EINVAL;
  1080. }
  1081. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1082. {
  1083. struct perf_counter *counter = file->private_data;
  1084. unsigned int events = 0;
  1085. unsigned long flags;
  1086. poll_wait(file, &counter->waitq, wait);
  1087. spin_lock_irqsave(&counter->waitq.lock, flags);
  1088. if (counter->usrdata->len || counter->irqdata->len)
  1089. events |= POLLIN;
  1090. spin_unlock_irqrestore(&counter->waitq.lock, flags);
  1091. return events;
  1092. }
  1093. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1094. {
  1095. struct perf_counter *counter = file->private_data;
  1096. int err = 0;
  1097. switch (cmd) {
  1098. case PERF_COUNTER_IOC_ENABLE:
  1099. perf_counter_enable_family(counter);
  1100. break;
  1101. case PERF_COUNTER_IOC_DISABLE:
  1102. perf_counter_disable_family(counter);
  1103. break;
  1104. default:
  1105. err = -ENOTTY;
  1106. }
  1107. return err;
  1108. }
  1109. static const struct file_operations perf_fops = {
  1110. .release = perf_release,
  1111. .read = perf_read,
  1112. .poll = perf_poll,
  1113. .unlocked_ioctl = perf_ioctl,
  1114. .compat_ioctl = perf_ioctl,
  1115. };
  1116. static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
  1117. {
  1118. int cpu = raw_smp_processor_id();
  1119. atomic64_set(&counter->hw.prev_count, cpu_clock(cpu));
  1120. return 0;
  1121. }
  1122. static void cpu_clock_perf_counter_update(struct perf_counter *counter)
  1123. {
  1124. int cpu = raw_smp_processor_id();
  1125. s64 prev;
  1126. u64 now;
  1127. now = cpu_clock(cpu);
  1128. prev = atomic64_read(&counter->hw.prev_count);
  1129. atomic64_set(&counter->hw.prev_count, now);
  1130. atomic64_add(now - prev, &counter->count);
  1131. }
  1132. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  1133. {
  1134. cpu_clock_perf_counter_update(counter);
  1135. }
  1136. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  1137. {
  1138. cpu_clock_perf_counter_update(counter);
  1139. }
  1140. static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
  1141. .enable = cpu_clock_perf_counter_enable,
  1142. .disable = cpu_clock_perf_counter_disable,
  1143. .read = cpu_clock_perf_counter_read,
  1144. };
  1145. /*
  1146. * Called from within the scheduler:
  1147. */
  1148. static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
  1149. {
  1150. struct task_struct *curr = counter->task;
  1151. u64 delta;
  1152. delta = __task_delta_exec(curr, update);
  1153. return curr->se.sum_exec_runtime + delta;
  1154. }
  1155. static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
  1156. {
  1157. u64 prev;
  1158. s64 delta;
  1159. prev = atomic64_read(&counter->hw.prev_count);
  1160. atomic64_set(&counter->hw.prev_count, now);
  1161. delta = now - prev;
  1162. atomic64_add(delta, &counter->count);
  1163. }
  1164. static void task_clock_perf_counter_read(struct perf_counter *counter)
  1165. {
  1166. u64 now = task_clock_perf_counter_val(counter, 1);
  1167. task_clock_perf_counter_update(counter, now);
  1168. }
  1169. static int task_clock_perf_counter_enable(struct perf_counter *counter)
  1170. {
  1171. if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
  1172. atomic64_set(&counter->hw.prev_count,
  1173. task_clock_perf_counter_val(counter, 0));
  1174. return 0;
  1175. }
  1176. static void task_clock_perf_counter_disable(struct perf_counter *counter)
  1177. {
  1178. u64 now = task_clock_perf_counter_val(counter, 0);
  1179. task_clock_perf_counter_update(counter, now);
  1180. }
  1181. static const struct hw_perf_counter_ops perf_ops_task_clock = {
  1182. .enable = task_clock_perf_counter_enable,
  1183. .disable = task_clock_perf_counter_disable,
  1184. .read = task_clock_perf_counter_read,
  1185. };
  1186. #ifdef CONFIG_VM_EVENT_COUNTERS
  1187. #define cpu_page_faults() __get_cpu_var(vm_event_states).event[PGFAULT]
  1188. #else
  1189. #define cpu_page_faults() 0
  1190. #endif
  1191. static u64 get_page_faults(struct perf_counter *counter)
  1192. {
  1193. struct task_struct *curr = counter->ctx->task;
  1194. if (curr)
  1195. return curr->maj_flt + curr->min_flt;
  1196. return cpu_page_faults();
  1197. }
  1198. static void page_faults_perf_counter_update(struct perf_counter *counter)
  1199. {
  1200. u64 prev, now;
  1201. s64 delta;
  1202. prev = atomic64_read(&counter->hw.prev_count);
  1203. now = get_page_faults(counter);
  1204. atomic64_set(&counter->hw.prev_count, now);
  1205. delta = now - prev;
  1206. atomic64_add(delta, &counter->count);
  1207. }
  1208. static void page_faults_perf_counter_read(struct perf_counter *counter)
  1209. {
  1210. page_faults_perf_counter_update(counter);
  1211. }
  1212. static int page_faults_perf_counter_enable(struct perf_counter *counter)
  1213. {
  1214. if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
  1215. atomic64_set(&counter->hw.prev_count, get_page_faults(counter));
  1216. return 0;
  1217. }
  1218. static void page_faults_perf_counter_disable(struct perf_counter *counter)
  1219. {
  1220. page_faults_perf_counter_update(counter);
  1221. }
  1222. static const struct hw_perf_counter_ops perf_ops_page_faults = {
  1223. .enable = page_faults_perf_counter_enable,
  1224. .disable = page_faults_perf_counter_disable,
  1225. .read = page_faults_perf_counter_read,
  1226. };
  1227. static u64 get_context_switches(struct perf_counter *counter)
  1228. {
  1229. struct task_struct *curr = counter->ctx->task;
  1230. if (curr)
  1231. return curr->nvcsw + curr->nivcsw;
  1232. return cpu_nr_switches(smp_processor_id());
  1233. }
  1234. static void context_switches_perf_counter_update(struct perf_counter *counter)
  1235. {
  1236. u64 prev, now;
  1237. s64 delta;
  1238. prev = atomic64_read(&counter->hw.prev_count);
  1239. now = get_context_switches(counter);
  1240. atomic64_set(&counter->hw.prev_count, now);
  1241. delta = now - prev;
  1242. atomic64_add(delta, &counter->count);
  1243. }
  1244. static void context_switches_perf_counter_read(struct perf_counter *counter)
  1245. {
  1246. context_switches_perf_counter_update(counter);
  1247. }
  1248. static int context_switches_perf_counter_enable(struct perf_counter *counter)
  1249. {
  1250. if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
  1251. atomic64_set(&counter->hw.prev_count,
  1252. get_context_switches(counter));
  1253. return 0;
  1254. }
  1255. static void context_switches_perf_counter_disable(struct perf_counter *counter)
  1256. {
  1257. context_switches_perf_counter_update(counter);
  1258. }
  1259. static const struct hw_perf_counter_ops perf_ops_context_switches = {
  1260. .enable = context_switches_perf_counter_enable,
  1261. .disable = context_switches_perf_counter_disable,
  1262. .read = context_switches_perf_counter_read,
  1263. };
  1264. static inline u64 get_cpu_migrations(struct perf_counter *counter)
  1265. {
  1266. struct task_struct *curr = counter->ctx->task;
  1267. if (curr)
  1268. return curr->se.nr_migrations;
  1269. return cpu_nr_migrations(smp_processor_id());
  1270. }
  1271. static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
  1272. {
  1273. u64 prev, now;
  1274. s64 delta;
  1275. prev = atomic64_read(&counter->hw.prev_count);
  1276. now = get_cpu_migrations(counter);
  1277. atomic64_set(&counter->hw.prev_count, now);
  1278. delta = now - prev;
  1279. atomic64_add(delta, &counter->count);
  1280. }
  1281. static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
  1282. {
  1283. cpu_migrations_perf_counter_update(counter);
  1284. }
  1285. static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
  1286. {
  1287. if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
  1288. atomic64_set(&counter->hw.prev_count,
  1289. get_cpu_migrations(counter));
  1290. return 0;
  1291. }
  1292. static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
  1293. {
  1294. cpu_migrations_perf_counter_update(counter);
  1295. }
  1296. static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
  1297. .enable = cpu_migrations_perf_counter_enable,
  1298. .disable = cpu_migrations_perf_counter_disable,
  1299. .read = cpu_migrations_perf_counter_read,
  1300. };
  1301. static const struct hw_perf_counter_ops *
  1302. sw_perf_counter_init(struct perf_counter *counter)
  1303. {
  1304. const struct hw_perf_counter_ops *hw_ops = NULL;
  1305. /*
  1306. * Software counters (currently) can't in general distinguish
  1307. * between user, kernel and hypervisor events.
  1308. * However, context switches and cpu migrations are considered
  1309. * to be kernel events, and page faults are never hypervisor
  1310. * events.
  1311. */
  1312. switch (counter->hw_event.type) {
  1313. case PERF_COUNT_CPU_CLOCK:
  1314. if (!(counter->hw_event.exclude_user ||
  1315. counter->hw_event.exclude_kernel ||
  1316. counter->hw_event.exclude_hv))
  1317. hw_ops = &perf_ops_cpu_clock;
  1318. break;
  1319. case PERF_COUNT_TASK_CLOCK:
  1320. if (counter->hw_event.exclude_user ||
  1321. counter->hw_event.exclude_kernel ||
  1322. counter->hw_event.exclude_hv)
  1323. break;
  1324. /*
  1325. * If the user instantiates this as a per-cpu counter,
  1326. * use the cpu_clock counter instead.
  1327. */
  1328. if (counter->ctx->task)
  1329. hw_ops = &perf_ops_task_clock;
  1330. else
  1331. hw_ops = &perf_ops_cpu_clock;
  1332. break;
  1333. case PERF_COUNT_PAGE_FAULTS:
  1334. if (!(counter->hw_event.exclude_user ||
  1335. counter->hw_event.exclude_kernel))
  1336. hw_ops = &perf_ops_page_faults;
  1337. break;
  1338. case PERF_COUNT_CONTEXT_SWITCHES:
  1339. if (!counter->hw_event.exclude_kernel)
  1340. hw_ops = &perf_ops_context_switches;
  1341. break;
  1342. case PERF_COUNT_CPU_MIGRATIONS:
  1343. if (!counter->hw_event.exclude_kernel)
  1344. hw_ops = &perf_ops_cpu_migrations;
  1345. break;
  1346. default:
  1347. break;
  1348. }
  1349. return hw_ops;
  1350. }
  1351. /*
  1352. * Allocate and initialize a counter structure
  1353. */
  1354. static struct perf_counter *
  1355. perf_counter_alloc(struct perf_counter_hw_event *hw_event,
  1356. int cpu,
  1357. struct perf_counter_context *ctx,
  1358. struct perf_counter *group_leader,
  1359. gfp_t gfpflags)
  1360. {
  1361. const struct hw_perf_counter_ops *hw_ops;
  1362. struct perf_counter *counter;
  1363. counter = kzalloc(sizeof(*counter), gfpflags);
  1364. if (!counter)
  1365. return NULL;
  1366. /*
  1367. * Single counters are their own group leaders, with an
  1368. * empty sibling list:
  1369. */
  1370. if (!group_leader)
  1371. group_leader = counter;
  1372. mutex_init(&counter->mutex);
  1373. INIT_LIST_HEAD(&counter->list_entry);
  1374. INIT_LIST_HEAD(&counter->sibling_list);
  1375. init_waitqueue_head(&counter->waitq);
  1376. INIT_LIST_HEAD(&counter->child_list);
  1377. counter->irqdata = &counter->data[0];
  1378. counter->usrdata = &counter->data[1];
  1379. counter->cpu = cpu;
  1380. counter->hw_event = *hw_event;
  1381. counter->wakeup_pending = 0;
  1382. counter->group_leader = group_leader;
  1383. counter->hw_ops = NULL;
  1384. counter->ctx = ctx;
  1385. counter->state = PERF_COUNTER_STATE_INACTIVE;
  1386. if (hw_event->disabled)
  1387. counter->state = PERF_COUNTER_STATE_OFF;
  1388. hw_ops = NULL;
  1389. if (!hw_event->raw && hw_event->type < 0)
  1390. hw_ops = sw_perf_counter_init(counter);
  1391. else
  1392. hw_ops = hw_perf_counter_init(counter);
  1393. if (!hw_ops) {
  1394. kfree(counter);
  1395. return NULL;
  1396. }
  1397. counter->hw_ops = hw_ops;
  1398. return counter;
  1399. }
  1400. /**
  1401. * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
  1402. *
  1403. * @hw_event_uptr: event type attributes for monitoring/sampling
  1404. * @pid: target pid
  1405. * @cpu: target cpu
  1406. * @group_fd: group leader counter fd
  1407. */
  1408. SYSCALL_DEFINE5(perf_counter_open,
  1409. const struct perf_counter_hw_event __user *, hw_event_uptr,
  1410. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  1411. {
  1412. struct perf_counter *counter, *group_leader;
  1413. struct perf_counter_hw_event hw_event;
  1414. struct perf_counter_context *ctx;
  1415. struct file *counter_file = NULL;
  1416. struct file *group_file = NULL;
  1417. int fput_needed = 0;
  1418. int fput_needed2 = 0;
  1419. int ret;
  1420. /* for future expandability... */
  1421. if (flags)
  1422. return -EINVAL;
  1423. if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
  1424. return -EFAULT;
  1425. /*
  1426. * Get the target context (task or percpu):
  1427. */
  1428. ctx = find_get_context(pid, cpu);
  1429. if (IS_ERR(ctx))
  1430. return PTR_ERR(ctx);
  1431. /*
  1432. * Look up the group leader (we will attach this counter to it):
  1433. */
  1434. group_leader = NULL;
  1435. if (group_fd != -1) {
  1436. ret = -EINVAL;
  1437. group_file = fget_light(group_fd, &fput_needed);
  1438. if (!group_file)
  1439. goto err_put_context;
  1440. if (group_file->f_op != &perf_fops)
  1441. goto err_put_context;
  1442. group_leader = group_file->private_data;
  1443. /*
  1444. * Do not allow a recursive hierarchy (this new sibling
  1445. * becoming part of another group-sibling):
  1446. */
  1447. if (group_leader->group_leader != group_leader)
  1448. goto err_put_context;
  1449. /*
  1450. * Do not allow to attach to a group in a different
  1451. * task or CPU context:
  1452. */
  1453. if (group_leader->ctx != ctx)
  1454. goto err_put_context;
  1455. /*
  1456. * Only a group leader can be exclusive or pinned
  1457. */
  1458. if (hw_event.exclusive || hw_event.pinned)
  1459. goto err_put_context;
  1460. }
  1461. ret = -EINVAL;
  1462. counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
  1463. GFP_KERNEL);
  1464. if (!counter)
  1465. goto err_put_context;
  1466. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  1467. if (ret < 0)
  1468. goto err_free_put_context;
  1469. counter_file = fget_light(ret, &fput_needed2);
  1470. if (!counter_file)
  1471. goto err_free_put_context;
  1472. counter->filp = counter_file;
  1473. mutex_lock(&ctx->mutex);
  1474. perf_install_in_context(ctx, counter, cpu);
  1475. mutex_unlock(&ctx->mutex);
  1476. fput_light(counter_file, fput_needed2);
  1477. out_fput:
  1478. fput_light(group_file, fput_needed);
  1479. return ret;
  1480. err_free_put_context:
  1481. kfree(counter);
  1482. err_put_context:
  1483. put_context(ctx);
  1484. goto out_fput;
  1485. }
  1486. /*
  1487. * Initialize the perf_counter context in a task_struct:
  1488. */
  1489. static void
  1490. __perf_counter_init_context(struct perf_counter_context *ctx,
  1491. struct task_struct *task)
  1492. {
  1493. memset(ctx, 0, sizeof(*ctx));
  1494. spin_lock_init(&ctx->lock);
  1495. mutex_init(&ctx->mutex);
  1496. INIT_LIST_HEAD(&ctx->counter_list);
  1497. ctx->task = task;
  1498. }
  1499. /*
  1500. * inherit a counter from parent task to child task:
  1501. */
  1502. static struct perf_counter *
  1503. inherit_counter(struct perf_counter *parent_counter,
  1504. struct task_struct *parent,
  1505. struct perf_counter_context *parent_ctx,
  1506. struct task_struct *child,
  1507. struct perf_counter *group_leader,
  1508. struct perf_counter_context *child_ctx)
  1509. {
  1510. struct perf_counter *child_counter;
  1511. /*
  1512. * Instead of creating recursive hierarchies of counters,
  1513. * we link inherited counters back to the original parent,
  1514. * which has a filp for sure, which we use as the reference
  1515. * count:
  1516. */
  1517. if (parent_counter->parent)
  1518. parent_counter = parent_counter->parent;
  1519. child_counter = perf_counter_alloc(&parent_counter->hw_event,
  1520. parent_counter->cpu, child_ctx,
  1521. group_leader, GFP_KERNEL);
  1522. if (!child_counter)
  1523. return NULL;
  1524. /*
  1525. * Link it up in the child's context:
  1526. */
  1527. child_counter->task = child;
  1528. list_add_counter(child_counter, child_ctx);
  1529. child_ctx->nr_counters++;
  1530. child_counter->parent = parent_counter;
  1531. /*
  1532. * inherit into child's child as well:
  1533. */
  1534. child_counter->hw_event.inherit = 1;
  1535. /*
  1536. * Get a reference to the parent filp - we will fput it
  1537. * when the child counter exits. This is safe to do because
  1538. * we are in the parent and we know that the filp still
  1539. * exists and has a nonzero count:
  1540. */
  1541. atomic_long_inc(&parent_counter->filp->f_count);
  1542. /*
  1543. * Link this into the parent counter's child list
  1544. */
  1545. mutex_lock(&parent_counter->mutex);
  1546. list_add_tail(&child_counter->child_list, &parent_counter->child_list);
  1547. /*
  1548. * Make the child state follow the state of the parent counter,
  1549. * not its hw_event.disabled bit. We hold the parent's mutex,
  1550. * so we won't race with perf_counter_{en,dis}able_family.
  1551. */
  1552. if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
  1553. child_counter->state = PERF_COUNTER_STATE_INACTIVE;
  1554. else
  1555. child_counter->state = PERF_COUNTER_STATE_OFF;
  1556. mutex_unlock(&parent_counter->mutex);
  1557. return child_counter;
  1558. }
  1559. static int inherit_group(struct perf_counter *parent_counter,
  1560. struct task_struct *parent,
  1561. struct perf_counter_context *parent_ctx,
  1562. struct task_struct *child,
  1563. struct perf_counter_context *child_ctx)
  1564. {
  1565. struct perf_counter *leader;
  1566. struct perf_counter *sub;
  1567. leader = inherit_counter(parent_counter, parent, parent_ctx,
  1568. child, NULL, child_ctx);
  1569. if (!leader)
  1570. return -ENOMEM;
  1571. list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
  1572. if (!inherit_counter(sub, parent, parent_ctx,
  1573. child, leader, child_ctx))
  1574. return -ENOMEM;
  1575. }
  1576. return 0;
  1577. }
  1578. static void sync_child_counter(struct perf_counter *child_counter,
  1579. struct perf_counter *parent_counter)
  1580. {
  1581. u64 parent_val, child_val;
  1582. parent_val = atomic64_read(&parent_counter->count);
  1583. child_val = atomic64_read(&child_counter->count);
  1584. /*
  1585. * Add back the child's count to the parent's count:
  1586. */
  1587. atomic64_add(child_val, &parent_counter->count);
  1588. /*
  1589. * Remove this counter from the parent's list
  1590. */
  1591. mutex_lock(&parent_counter->mutex);
  1592. list_del_init(&child_counter->child_list);
  1593. mutex_unlock(&parent_counter->mutex);
  1594. /*
  1595. * Release the parent counter, if this was the last
  1596. * reference to it.
  1597. */
  1598. fput(parent_counter->filp);
  1599. }
  1600. static void
  1601. __perf_counter_exit_task(struct task_struct *child,
  1602. struct perf_counter *child_counter,
  1603. struct perf_counter_context *child_ctx)
  1604. {
  1605. struct perf_counter *parent_counter;
  1606. struct perf_counter *sub, *tmp;
  1607. /*
  1608. * If we do not self-reap then we have to wait for the
  1609. * child task to unschedule (it will happen for sure),
  1610. * so that its counter is at its final count. (This
  1611. * condition triggers rarely - child tasks usually get
  1612. * off their CPU before the parent has a chance to
  1613. * get this far into the reaping action)
  1614. */
  1615. if (child != current) {
  1616. wait_task_inactive(child, 0);
  1617. list_del_init(&child_counter->list_entry);
  1618. } else {
  1619. struct perf_cpu_context *cpuctx;
  1620. unsigned long flags;
  1621. u64 perf_flags;
  1622. /*
  1623. * Disable and unlink this counter.
  1624. *
  1625. * Be careful about zapping the list - IRQ/NMI context
  1626. * could still be processing it:
  1627. */
  1628. curr_rq_lock_irq_save(&flags);
  1629. perf_flags = hw_perf_save_disable();
  1630. cpuctx = &__get_cpu_var(perf_cpu_context);
  1631. group_sched_out(child_counter, cpuctx, child_ctx);
  1632. list_del_init(&child_counter->list_entry);
  1633. child_ctx->nr_counters--;
  1634. hw_perf_restore(perf_flags);
  1635. curr_rq_unlock_irq_restore(&flags);
  1636. }
  1637. parent_counter = child_counter->parent;
  1638. /*
  1639. * It can happen that parent exits first, and has counters
  1640. * that are still around due to the child reference. These
  1641. * counters need to be zapped - but otherwise linger.
  1642. */
  1643. if (parent_counter) {
  1644. sync_child_counter(child_counter, parent_counter);
  1645. list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
  1646. list_entry) {
  1647. if (sub->parent) {
  1648. sync_child_counter(sub, sub->parent);
  1649. kfree(sub);
  1650. }
  1651. }
  1652. kfree(child_counter);
  1653. }
  1654. }
  1655. /*
  1656. * When a child task exits, feed back counter values to parent counters.
  1657. *
  1658. * Note: we may be running in child context, but the PID is not hashed
  1659. * anymore so new counters will not be added.
  1660. */
  1661. void perf_counter_exit_task(struct task_struct *child)
  1662. {
  1663. struct perf_counter *child_counter, *tmp;
  1664. struct perf_counter_context *child_ctx;
  1665. child_ctx = &child->perf_counter_ctx;
  1666. if (likely(!child_ctx->nr_counters))
  1667. return;
  1668. list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
  1669. list_entry)
  1670. __perf_counter_exit_task(child, child_counter, child_ctx);
  1671. }
  1672. /*
  1673. * Initialize the perf_counter context in task_struct
  1674. */
  1675. void perf_counter_init_task(struct task_struct *child)
  1676. {
  1677. struct perf_counter_context *child_ctx, *parent_ctx;
  1678. struct perf_counter *counter;
  1679. struct task_struct *parent = current;
  1680. child_ctx = &child->perf_counter_ctx;
  1681. parent_ctx = &parent->perf_counter_ctx;
  1682. __perf_counter_init_context(child_ctx, child);
  1683. /*
  1684. * This is executed from the parent task context, so inherit
  1685. * counters that have been marked for cloning:
  1686. */
  1687. if (likely(!parent_ctx->nr_counters))
  1688. return;
  1689. /*
  1690. * Lock the parent list. No need to lock the child - not PID
  1691. * hashed yet and not running, so nobody can access it.
  1692. */
  1693. mutex_lock(&parent_ctx->mutex);
  1694. /*
  1695. * We dont have to disable NMIs - we are only looking at
  1696. * the list, not manipulating it:
  1697. */
  1698. list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
  1699. if (!counter->hw_event.inherit)
  1700. continue;
  1701. if (inherit_group(counter, parent,
  1702. parent_ctx, child, child_ctx))
  1703. break;
  1704. }
  1705. mutex_unlock(&parent_ctx->mutex);
  1706. }
  1707. static void __cpuinit perf_counter_init_cpu(int cpu)
  1708. {
  1709. struct perf_cpu_context *cpuctx;
  1710. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1711. __perf_counter_init_context(&cpuctx->ctx, NULL);
  1712. mutex_lock(&perf_resource_mutex);
  1713. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  1714. mutex_unlock(&perf_resource_mutex);
  1715. hw_perf_counter_setup(cpu);
  1716. }
  1717. #ifdef CONFIG_HOTPLUG_CPU
  1718. static void __perf_counter_exit_cpu(void *info)
  1719. {
  1720. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1721. struct perf_counter_context *ctx = &cpuctx->ctx;
  1722. struct perf_counter *counter, *tmp;
  1723. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  1724. __perf_counter_remove_from_context(counter);
  1725. }
  1726. static void perf_counter_exit_cpu(int cpu)
  1727. {
  1728. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  1729. struct perf_counter_context *ctx = &cpuctx->ctx;
  1730. mutex_lock(&ctx->mutex);
  1731. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  1732. mutex_unlock(&ctx->mutex);
  1733. }
  1734. #else
  1735. static inline void perf_counter_exit_cpu(int cpu) { }
  1736. #endif
  1737. static int __cpuinit
  1738. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  1739. {
  1740. unsigned int cpu = (long)hcpu;
  1741. switch (action) {
  1742. case CPU_UP_PREPARE:
  1743. case CPU_UP_PREPARE_FROZEN:
  1744. perf_counter_init_cpu(cpu);
  1745. break;
  1746. case CPU_DOWN_PREPARE:
  1747. case CPU_DOWN_PREPARE_FROZEN:
  1748. perf_counter_exit_cpu(cpu);
  1749. break;
  1750. default:
  1751. break;
  1752. }
  1753. return NOTIFY_OK;
  1754. }
  1755. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  1756. .notifier_call = perf_cpu_notify,
  1757. };
  1758. static int __init perf_counter_init(void)
  1759. {
  1760. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  1761. (void *)(long)smp_processor_id());
  1762. register_cpu_notifier(&perf_cpu_nb);
  1763. return 0;
  1764. }
  1765. early_initcall(perf_counter_init);
  1766. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  1767. {
  1768. return sprintf(buf, "%d\n", perf_reserved_percpu);
  1769. }
  1770. static ssize_t
  1771. perf_set_reserve_percpu(struct sysdev_class *class,
  1772. const char *buf,
  1773. size_t count)
  1774. {
  1775. struct perf_cpu_context *cpuctx;
  1776. unsigned long val;
  1777. int err, cpu, mpt;
  1778. err = strict_strtoul(buf, 10, &val);
  1779. if (err)
  1780. return err;
  1781. if (val > perf_max_counters)
  1782. return -EINVAL;
  1783. mutex_lock(&perf_resource_mutex);
  1784. perf_reserved_percpu = val;
  1785. for_each_online_cpu(cpu) {
  1786. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1787. spin_lock_irq(&cpuctx->ctx.lock);
  1788. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  1789. perf_max_counters - perf_reserved_percpu);
  1790. cpuctx->max_pertask = mpt;
  1791. spin_unlock_irq(&cpuctx->ctx.lock);
  1792. }
  1793. mutex_unlock(&perf_resource_mutex);
  1794. return count;
  1795. }
  1796. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  1797. {
  1798. return sprintf(buf, "%d\n", perf_overcommit);
  1799. }
  1800. static ssize_t
  1801. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  1802. {
  1803. unsigned long val;
  1804. int err;
  1805. err = strict_strtoul(buf, 10, &val);
  1806. if (err)
  1807. return err;
  1808. if (val > 1)
  1809. return -EINVAL;
  1810. mutex_lock(&perf_resource_mutex);
  1811. perf_overcommit = val;
  1812. mutex_unlock(&perf_resource_mutex);
  1813. return count;
  1814. }
  1815. static SYSDEV_CLASS_ATTR(
  1816. reserve_percpu,
  1817. 0644,
  1818. perf_show_reserve_percpu,
  1819. perf_set_reserve_percpu
  1820. );
  1821. static SYSDEV_CLASS_ATTR(
  1822. overcommit,
  1823. 0644,
  1824. perf_show_overcommit,
  1825. perf_set_overcommit
  1826. );
  1827. static struct attribute *perfclass_attrs[] = {
  1828. &attr_reserve_percpu.attr,
  1829. &attr_overcommit.attr,
  1830. NULL
  1831. };
  1832. static struct attribute_group perfclass_attr_group = {
  1833. .attrs = perfclass_attrs,
  1834. .name = "perf_counters",
  1835. };
  1836. static int __init perf_counter_sysfs_init(void)
  1837. {
  1838. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  1839. &perfclass_attr_group);
  1840. }
  1841. device_initcall(perf_counter_sysfs_init);