blob: c77b05d9a37d00956704a4810c45c3141f24fffd [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
Ingo Molnare7e7ee22011-05-04 08:42:29 +02005 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
Peter Zijlstra90eec102015-11-16 11:08:45 +01006 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra
Al Virod36b6912011-12-29 17:09:01 -05007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008 *
Ingo Molnar57c0c152009-09-21 12:20:38 +02009 * For licensing details see kernel-base/COPYING
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010 */
11
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include <linux/cpu.h>
15#include <linux/smp.h>
Peter Zijlstra2e80a822010-11-17 23:17:36 +010016#include <linux/idr.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/file.h>
18#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020020#include <linux/hash.h>
Frederic Weisbecker12351ef2013-04-20 15:48:22 +020021#include <linux/tick.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020022#include <linux/sysfs.h>
23#include <linux/dcache.h>
24#include <linux/percpu.h>
25#include <linux/ptrace.h>
Peter Zijlstrac2774432010-12-08 15:29:02 +010026#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020027#include <linux/vmstat.h>
Peter Zijlstraabe43402010-11-17 23:17:37 +010028#include <linux/device.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040029#include <linux/export.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020030#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020031#include <linux/hardirq.h>
32#include <linux/rculist.h>
33#include <linux/uaccess.h>
34#include <linux/syscalls.h>
35#include <linux/anon_inodes.h>
36#include <linux/kernel_stat.h>
Matt Fleming39bed6c2015-01-23 18:45:40 +000037#include <linux/cgroup.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020038#include <linux/perf_event.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040039#include <linux/trace_events.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050040#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020041#include <linux/mm_types.h>
Yan, Zhengc464c762014-03-18 16:56:41 +080042#include <linux/module.h>
Peter Zijlstraf972eb62014-05-19 15:13:47 -040043#include <linux/mman.h>
Pawel Mollb3f20782014-06-13 16:03:32 +010044#include <linux/compat.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070045#include <linux/bpf.h>
46#include <linux/filter.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020047
Frederic Weisbecker76369132011-05-19 19:55:04 +020048#include "internal.h"
49
Ingo Molnarcdd6c482009-09-21 12:02:48 +020050#include <asm/irq_regs.h>
51
Jiri Olsafadfe7b2014-08-01 14:33:02 +020052static struct workqueue_struct *perf_wq;
53
Peter Zijlstra272325c2015-04-15 11:41:58 +020054typedef int (*remote_function_f)(void *);
55
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010056struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020057 struct task_struct *p;
Peter Zijlstra272325c2015-04-15 11:41:58 +020058 remote_function_f func;
Ingo Molnare7e7ee22011-05-04 08:42:29 +020059 void *info;
60 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010061};
62
63static void remote_function(void *data)
64{
65 struct remote_function_call *tfc = data;
66 struct task_struct *p = tfc->p;
67
68 if (p) {
69 tfc->ret = -EAGAIN;
70 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
71 return;
72 }
73
74 tfc->ret = tfc->func(tfc->info);
75}
76
77/**
78 * task_function_call - call a function on the cpu on which a task runs
79 * @p: the task to evaluate
80 * @func: the function to be called
81 * @info: the function call argument
82 *
83 * Calls the function @func when the task is currently running. This might
84 * be on the current CPU, which just calls the function directly
85 *
86 * returns: @func return value, or
87 * -ESRCH - when the process isn't running
88 * -EAGAIN - when the process moved away
89 */
90static int
Peter Zijlstra272325c2015-04-15 11:41:58 +020091task_function_call(struct task_struct *p, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010092{
93 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020094 .p = p,
95 .func = func,
96 .info = info,
97 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010098 };
99
100 if (task_curr(p))
101 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
102
103 return data.ret;
104}
105
106/**
107 * cpu_function_call - call a function on the cpu
108 * @func: the function to be called
109 * @info: the function call argument
110 *
111 * Calls the function @func on the remote cpu.
112 *
113 * returns: @func return value or -ENXIO when the cpu is offline
114 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200115static int cpu_function_call(int cpu, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100116{
117 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200118 .p = NULL,
119 .func = func,
120 .info = info,
121 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100122 };
123
124 smp_call_function_single(cpu, remote_function, &data, 1);
125
126 return data.ret;
127}
128
Peter Zijlstra00179602015-11-30 16:26:35 +0100129static void event_function_call(struct perf_event *event,
130 int (*active)(void *),
131 void (*inactive)(void *),
132 void *data)
133{
134 struct perf_event_context *ctx = event->ctx;
135 struct task_struct *task = ctx->task;
136
137 if (!task) {
138 cpu_function_call(event->cpu, active, data);
139 return;
140 }
141
142again:
143 if (!task_function_call(task, active, data))
144 return;
145
146 raw_spin_lock_irq(&ctx->lock);
147 if (ctx->is_active) {
148 /*
149 * Reload the task pointer, it might have been changed by
150 * a concurrent perf_event_context_sched_out().
151 */
152 task = ctx->task;
153 raw_spin_unlock_irq(&ctx->lock);
154 goto again;
155 }
156 inactive(data);
157 raw_spin_unlock_irq(&ctx->lock);
158}
159
Jiri Olsaf8697762014-08-01 14:33:01 +0200160#define EVENT_OWNER_KERNEL ((void *) -1)
161
162static bool is_kernel_event(struct perf_event *event)
163{
164 return event->owner == EVENT_OWNER_KERNEL;
165}
166
Stephane Eraniane5d13672011-02-14 11:20:01 +0200167#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
168 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100169 PERF_FLAG_PID_CGROUP |\
170 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200171
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100172/*
173 * branch priv levels that need permission checks
174 */
175#define PERF_SAMPLE_BRANCH_PERM_PLM \
176 (PERF_SAMPLE_BRANCH_KERNEL |\
177 PERF_SAMPLE_BRANCH_HV)
178
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200179enum event_type_t {
180 EVENT_FLEXIBLE = 0x1,
181 EVENT_PINNED = 0x2,
182 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
183};
184
Stephane Eraniane5d13672011-02-14 11:20:01 +0200185/*
186 * perf_sched_events : >0 events exist
187 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
188 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100189struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200190static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Yan, Zhengba532502014-11-04 21:55:58 -0500191static DEFINE_PER_CPU(int, perf_sched_cb_usages);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200192
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200193static atomic_t nr_mmap_events __read_mostly;
194static atomic_t nr_comm_events __read_mostly;
195static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200196static atomic_t nr_freq_events __read_mostly;
Adrian Hunter45ac1402015-07-21 12:44:02 +0300197static atomic_t nr_switch_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200198
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200199static LIST_HEAD(pmus);
200static DEFINE_MUTEX(pmus_lock);
201static struct srcu_struct pmus_srcu;
202
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200203/*
204 * perf event paranoia level:
205 * -1 - not paranoid at all
206 * 0 - disallow raw tracepoint access for unpriv
207 * 1 - disallow cpu events for unpriv
208 * 2 - disallow kernel profiling for unpriv
209 */
210int sysctl_perf_event_paranoid __read_mostly = 1;
211
Frederic Weisbecker20443382011-03-31 03:33:29 +0200212/* Minimum for 512 kiB + 1 user control page */
213int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200214
215/*
216 * max perf event sample rate
217 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700218#define DEFAULT_MAX_SAMPLE_RATE 100000
219#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
220#define DEFAULT_CPU_TIME_MAX_PERCENT 25
221
222int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
223
224static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
225static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
226
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200227static int perf_sample_allowed_ns __read_mostly =
228 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700229
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800230static void update_perf_cpu_limits(void)
Dave Hansen14c63f12013-06-21 08:51:36 -0700231{
232 u64 tmp = perf_sample_period_ns;
233
234 tmp *= sysctl_perf_cpu_time_max_percent;
Stephane Eraniane5302922013-07-05 00:30:11 +0200235 do_div(tmp, 100);
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200236 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
Dave Hansen14c63f12013-06-21 08:51:36 -0700237}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100238
Stephane Eranian9e630202013-04-03 14:21:33 +0200239static int perf_rotate_context(struct perf_cpu_context *cpuctx);
240
Peter Zijlstra163ec432011-02-16 11:22:34 +0100241int perf_proc_update_handler(struct ctl_table *table, int write,
242 void __user *buffer, size_t *lenp,
243 loff_t *ppos)
244{
Knut Petersen723478c2013-09-25 14:29:37 +0200245 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100246
247 if (ret || !write)
248 return ret;
249
250 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700251 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
252 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100253
254 return 0;
255}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200256
Dave Hansen14c63f12013-06-21 08:51:36 -0700257int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
258
259int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
260 void __user *buffer, size_t *lenp,
261 loff_t *ppos)
262{
263 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
264
265 if (ret || !write)
266 return ret;
267
268 update_perf_cpu_limits();
269
270 return 0;
271}
272
273/*
274 * perf samples are done in some very critical code paths (NMIs).
275 * If they take too much CPU time, the system can lock up and not
276 * get any real work done. This will drop the sample rate when
277 * we detect that events are taking too long.
278 */
279#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200280static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700281
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100282static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700283{
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100284 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Dave Hansen14c63f12013-06-21 08:51:36 -0700285 u64 avg_local_sample_len;
Stephane Eraniane5302922013-07-05 00:30:11 +0200286 u64 local_samples_len;
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100287
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500288 local_samples_len = __this_cpu_read(running_sample_length);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100289 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
290
291 printk_ratelimited(KERN_WARNING
292 "perf interrupt took too long (%lld > %lld), lowering "
293 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100294 avg_local_sample_len, allowed_ns >> 1,
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100295 sysctl_perf_event_sample_rate);
296}
297
298static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
299
300void perf_sample_event_took(u64 sample_len_ns)
301{
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200302 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100303 u64 avg_local_sample_len;
304 u64 local_samples_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700305
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200306 if (allowed_ns == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700307 return;
308
309 /* decay the counter by 1 average sample */
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500310 local_samples_len = __this_cpu_read(running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700311 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
312 local_samples_len += sample_len_ns;
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500313 __this_cpu_write(running_sample_length, local_samples_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700314
315 /*
316 * note: this will be biased artifically low until we have
317 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
318 * from having to maintain a count.
319 */
320 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
321
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200322 if (avg_local_sample_len <= allowed_ns)
Dave Hansen14c63f12013-06-21 08:51:36 -0700323 return;
324
325 if (max_samples_per_tick <= 1)
326 return;
327
328 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
329 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
330 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
331
Dave Hansen14c63f12013-06-21 08:51:36 -0700332 update_perf_cpu_limits();
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100333
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100334 if (!irq_work_queue(&perf_duration_work)) {
335 early_printk("perf interrupt took too long (%lld > %lld), lowering "
336 "kernel.perf_event_max_sample_rate to %d\n",
337 avg_local_sample_len, allowed_ns >> 1,
338 sysctl_perf_event_sample_rate);
339 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700340}
341
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200342static atomic64_t perf_event_id;
343
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200344static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
345 enum event_type_t event_type);
346
347static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200348 enum event_type_t event_type,
349 struct task_struct *task);
350
351static void update_context_time(struct perf_event_context *ctx);
352static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200353
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200354void __weak perf_event_print_debug(void) { }
355
Matt Fleming84c79912010-10-03 21:41:13 +0100356extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200357{
Matt Fleming84c79912010-10-03 21:41:13 +0100358 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200359}
360
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200361static inline u64 perf_clock(void)
362{
363 return local_clock();
364}
365
Peter Zijlstra34f43922015-02-20 14:05:38 +0100366static inline u64 perf_event_clock(struct perf_event *event)
367{
368 return event->clock();
369}
370
Stephane Eraniane5d13672011-02-14 11:20:01 +0200371static inline struct perf_cpu_context *
372__get_cpu_context(struct perf_event_context *ctx)
373{
374 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
375}
376
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200377static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
378 struct perf_event_context *ctx)
379{
380 raw_spin_lock(&cpuctx->ctx.lock);
381 if (ctx)
382 raw_spin_lock(&ctx->lock);
383}
384
385static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
386 struct perf_event_context *ctx)
387{
388 if (ctx)
389 raw_spin_unlock(&ctx->lock);
390 raw_spin_unlock(&cpuctx->ctx.lock);
391}
392
Stephane Eraniane5d13672011-02-14 11:20:01 +0200393#ifdef CONFIG_CGROUP_PERF
394
Stephane Eraniane5d13672011-02-14 11:20:01 +0200395static inline bool
396perf_cgroup_match(struct perf_event *event)
397{
398 struct perf_event_context *ctx = event->ctx;
399 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
400
Tejun Heoef824fa2013-04-08 19:00:38 -0700401 /* @event doesn't care about cgroup */
402 if (!event->cgrp)
403 return true;
404
405 /* wants specific cgroup scope but @cpuctx isn't associated with any */
406 if (!cpuctx->cgrp)
407 return false;
408
409 /*
410 * Cgroup scoping is recursive. An event enabled for a cgroup is
411 * also enabled for all its descendant cgroups. If @cpuctx's
412 * cgroup is a descendant of @event's (the test covers identity
413 * case), it's a match.
414 */
415 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
416 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200417}
418
Stephane Eraniane5d13672011-02-14 11:20:01 +0200419static inline void perf_detach_cgroup(struct perf_event *event)
420{
Zefan Li4e2ba652014-09-19 16:53:14 +0800421 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200422 event->cgrp = NULL;
423}
424
425static inline int is_cgroup_event(struct perf_event *event)
426{
427 return event->cgrp != NULL;
428}
429
430static inline u64 perf_cgroup_event_time(struct perf_event *event)
431{
432 struct perf_cgroup_info *t;
433
434 t = per_cpu_ptr(event->cgrp->info, event->cpu);
435 return t->time;
436}
437
438static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
439{
440 struct perf_cgroup_info *info;
441 u64 now;
442
443 now = perf_clock();
444
445 info = this_cpu_ptr(cgrp->info);
446
447 info->time += now - info->timestamp;
448 info->timestamp = now;
449}
450
451static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
452{
453 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
454 if (cgrp_out)
455 __update_cgrp_time(cgrp_out);
456}
457
458static inline void update_cgrp_time_from_event(struct perf_event *event)
459{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200460 struct perf_cgroup *cgrp;
461
Stephane Eraniane5d13672011-02-14 11:20:01 +0200462 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200463 * ensure we access cgroup data only when needed and
464 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200465 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200466 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200467 return;
468
Stephane Eranian614e4c42015-11-12 11:00:04 +0100469 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200470 /*
471 * Do not update time when cgroup is not active
472 */
473 if (cgrp == event->cgrp)
474 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200475}
476
477static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200478perf_cgroup_set_timestamp(struct task_struct *task,
479 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200480{
481 struct perf_cgroup *cgrp;
482 struct perf_cgroup_info *info;
483
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200484 /*
485 * ctx->lock held by caller
486 * ensure we do not access cgroup data
487 * unless we have the cgroup pinned (css_get)
488 */
489 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200490 return;
491
Stephane Eranian614e4c42015-11-12 11:00:04 +0100492 cgrp = perf_cgroup_from_task(task, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200493 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200494 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200495}
496
497#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
498#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
499
500/*
501 * reschedule events based on the cgroup constraint of task.
502 *
503 * mode SWOUT : schedule out everything
504 * mode SWIN : schedule in based on cgroup for next
505 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800506static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200507{
508 struct perf_cpu_context *cpuctx;
509 struct pmu *pmu;
510 unsigned long flags;
511
512 /*
513 * disable interrupts to avoid geting nr_cgroup
514 * changes via __perf_event_disable(). Also
515 * avoids preemption.
516 */
517 local_irq_save(flags);
518
519 /*
520 * we reschedule only in the presence of cgroup
521 * constrained events.
522 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200523
524 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200525 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200526 if (cpuctx->unique_pmu != pmu)
527 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200528
Stephane Eraniane5d13672011-02-14 11:20:01 +0200529 /*
530 * perf_cgroup_events says at least one
531 * context on this CPU has cgroup events.
532 *
533 * ctx->nr_cgroups reports the number of cgroup
534 * events for a context.
535 */
536 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200537 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
538 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200539
540 if (mode & PERF_CGROUP_SWOUT) {
541 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
542 /*
543 * must not be done before ctxswout due
544 * to event_filter_match() in event_sched_out()
545 */
546 cpuctx->cgrp = NULL;
547 }
548
549 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200550 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200551 /*
552 * set cgrp before ctxsw in to allow
553 * event_filter_match() to not have to pass
554 * task around
Stephane Eranian614e4c42015-11-12 11:00:04 +0100555 * we pass the cpuctx->ctx to perf_cgroup_from_task()
556 * because cgorup events are only per-cpu
Stephane Eraniane5d13672011-02-14 11:20:01 +0200557 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100558 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200559 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
560 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200561 perf_pmu_enable(cpuctx->ctx.pmu);
562 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200563 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200564 }
565
Stephane Eraniane5d13672011-02-14 11:20:01 +0200566 local_irq_restore(flags);
567}
568
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200569static inline void perf_cgroup_sched_out(struct task_struct *task,
570 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200571{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200572 struct perf_cgroup *cgrp1;
573 struct perf_cgroup *cgrp2 = NULL;
574
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100575 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200576 /*
577 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100578 * we do not need to pass the ctx here because we know
579 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200580 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100581 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200582
583 /*
584 * next is NULL when called from perf_event_enable_on_exec()
585 * that will systematically cause a cgroup_switch()
586 */
587 if (next)
Stephane Eranian614e4c42015-11-12 11:00:04 +0100588 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200589
590 /*
591 * only schedule out current cgroup events if we know
592 * that we are switching to a different cgroup. Otherwise,
593 * do no touch the cgroup events.
594 */
595 if (cgrp1 != cgrp2)
596 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100597
598 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200599}
600
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200601static inline void perf_cgroup_sched_in(struct task_struct *prev,
602 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200603{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200604 struct perf_cgroup *cgrp1;
605 struct perf_cgroup *cgrp2 = NULL;
606
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100607 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200608 /*
609 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100610 * we do not need to pass the ctx here because we know
611 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200612 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100613 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200614
615 /* prev can never be NULL */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100616 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200617
618 /*
619 * only need to schedule in cgroup events if we are changing
620 * cgroup during ctxsw. Cgroup events were not scheduled
621 * out of ctxsw out if that was not the case.
622 */
623 if (cgrp1 != cgrp2)
624 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100625
626 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200627}
628
629static inline int perf_cgroup_connect(int fd, struct perf_event *event,
630 struct perf_event_attr *attr,
631 struct perf_event *group_leader)
632{
633 struct perf_cgroup *cgrp;
634 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400635 struct fd f = fdget(fd);
636 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200637
Al Viro2903ff02012-08-28 12:52:22 -0400638 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200639 return -EBADF;
640
Al Virob5830432014-10-31 01:22:04 -0400641 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400642 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800643 if (IS_ERR(css)) {
644 ret = PTR_ERR(css);
645 goto out;
646 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200647
648 cgrp = container_of(css, struct perf_cgroup, css);
649 event->cgrp = cgrp;
650
651 /*
652 * all events in a group must monitor
653 * the same cgroup because a task belongs
654 * to only one perf cgroup at a time
655 */
656 if (group_leader && group_leader->cgrp != cgrp) {
657 perf_detach_cgroup(event);
658 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200659 }
Li Zefan3db272c2011-03-03 14:25:37 +0800660out:
Al Viro2903ff02012-08-28 12:52:22 -0400661 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200662 return ret;
663}
664
665static inline void
666perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
667{
668 struct perf_cgroup_info *t;
669 t = per_cpu_ptr(event->cgrp->info, event->cpu);
670 event->shadow_ctx_time = now - t->timestamp;
671}
672
673static inline void
674perf_cgroup_defer_enabled(struct perf_event *event)
675{
676 /*
677 * when the current task's perf cgroup does not match
678 * the event's, we need to remember to call the
679 * perf_mark_enable() function the first time a task with
680 * a matching perf cgroup is scheduled in.
681 */
682 if (is_cgroup_event(event) && !perf_cgroup_match(event))
683 event->cgrp_defer_enabled = 1;
684}
685
686static inline void
687perf_cgroup_mark_enabled(struct perf_event *event,
688 struct perf_event_context *ctx)
689{
690 struct perf_event *sub;
691 u64 tstamp = perf_event_time(event);
692
693 if (!event->cgrp_defer_enabled)
694 return;
695
696 event->cgrp_defer_enabled = 0;
697
698 event->tstamp_enabled = tstamp - event->total_time_enabled;
699 list_for_each_entry(sub, &event->sibling_list, group_entry) {
700 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
701 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
702 sub->cgrp_defer_enabled = 0;
703 }
704 }
705}
706#else /* !CONFIG_CGROUP_PERF */
707
708static inline bool
709perf_cgroup_match(struct perf_event *event)
710{
711 return true;
712}
713
714static inline void perf_detach_cgroup(struct perf_event *event)
715{}
716
717static inline int is_cgroup_event(struct perf_event *event)
718{
719 return 0;
720}
721
722static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
723{
724 return 0;
725}
726
727static inline void update_cgrp_time_from_event(struct perf_event *event)
728{
729}
730
731static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
732{
733}
734
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200735static inline void perf_cgroup_sched_out(struct task_struct *task,
736 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200737{
738}
739
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200740static inline void perf_cgroup_sched_in(struct task_struct *prev,
741 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200742{
743}
744
745static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
746 struct perf_event_attr *attr,
747 struct perf_event *group_leader)
748{
749 return -EINVAL;
750}
751
752static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200753perf_cgroup_set_timestamp(struct task_struct *task,
754 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200755{
756}
757
758void
759perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
760{
761}
762
763static inline void
764perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
765{
766}
767
768static inline u64 perf_cgroup_event_time(struct perf_event *event)
769{
770 return 0;
771}
772
773static inline void
774perf_cgroup_defer_enabled(struct perf_event *event)
775{
776}
777
778static inline void
779perf_cgroup_mark_enabled(struct perf_event *event,
780 struct perf_event_context *ctx)
781{
782}
783#endif
784
Stephane Eranian9e630202013-04-03 14:21:33 +0200785/*
786 * set default to be dependent on timer tick just
787 * like original code
788 */
789#define PERF_CPU_HRTIMER (1000 / HZ)
790/*
791 * function must be called with interrupts disbled
792 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200793static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +0200794{
795 struct perf_cpu_context *cpuctx;
Stephane Eranian9e630202013-04-03 14:21:33 +0200796 int rotations = 0;
797
798 WARN_ON(!irqs_disabled());
799
800 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200801 rotations = perf_rotate_context(cpuctx);
802
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200803 raw_spin_lock(&cpuctx->hrtimer_lock);
804 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +0200805 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200806 else
807 cpuctx->hrtimer_active = 0;
808 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +0200809
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200810 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +0200811}
812
Peter Zijlstra272325c2015-04-15 11:41:58 +0200813static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +0200814{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200815 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200816 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +0200817 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +0200818
819 /* no multiplexing needed for SW PMU */
820 if (pmu->task_ctx_nr == perf_sw_context)
821 return;
822
Stephane Eranian62b85632013-04-03 14:21:34 +0200823 /*
824 * check default is sane, if not set then force to
825 * default interval (1/tick)
826 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200827 interval = pmu->hrtimer_interval_ms;
828 if (interval < 1)
829 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +0200830
Peter Zijlstra272325c2015-04-15 11:41:58 +0200831 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +0200832
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200833 raw_spin_lock_init(&cpuctx->hrtimer_lock);
834 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra272325c2015-04-15 11:41:58 +0200835 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +0200836}
837
Peter Zijlstra272325c2015-04-15 11:41:58 +0200838static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +0200839{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200840 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200841 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200842 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +0200843
844 /* not for SW PMU */
845 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +0200846 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +0200847
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200848 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
849 if (!cpuctx->hrtimer_active) {
850 cpuctx->hrtimer_active = 1;
851 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
852 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
853 }
854 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +0200855
Peter Zijlstra272325c2015-04-15 11:41:58 +0200856 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +0200857}
858
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200859void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200860{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200861 int *count = this_cpu_ptr(pmu->pmu_disable_count);
862 if (!(*count)++)
863 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200864}
865
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200866void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200867{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200868 int *count = this_cpu_ptr(pmu->pmu_disable_count);
869 if (!--(*count))
870 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200871}
872
Mark Rutland2fde4f92015-01-07 15:01:54 +0000873static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200874
875/*
Mark Rutland2fde4f92015-01-07 15:01:54 +0000876 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
877 * perf_event_task_tick() are fully serialized because they're strictly cpu
878 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
879 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200880 */
Mark Rutland2fde4f92015-01-07 15:01:54 +0000881static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200882{
Mark Rutland2fde4f92015-01-07 15:01:54 +0000883 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200884
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200885 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200886
Mark Rutland2fde4f92015-01-07 15:01:54 +0000887 WARN_ON(!list_empty(&ctx->active_ctx_list));
888
889 list_add(&ctx->active_ctx_list, head);
890}
891
892static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
893{
894 WARN_ON(!irqs_disabled());
895
896 WARN_ON(list_empty(&ctx->active_ctx_list));
897
898 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200899}
900
901static void get_ctx(struct perf_event_context *ctx)
902{
903 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
904}
905
Yan, Zheng4af57ef282014-11-04 21:56:01 -0500906static void free_ctx(struct rcu_head *head)
907{
908 struct perf_event_context *ctx;
909
910 ctx = container_of(head, struct perf_event_context, rcu_head);
911 kfree(ctx->task_ctx_data);
912 kfree(ctx);
913}
914
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200915static void put_ctx(struct perf_event_context *ctx)
916{
917 if (atomic_dec_and_test(&ctx->refcount)) {
918 if (ctx->parent_ctx)
919 put_ctx(ctx->parent_ctx);
920 if (ctx->task)
921 put_task_struct(ctx->task);
Yan, Zheng4af57ef282014-11-04 21:56:01 -0500922 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200923 }
924}
925
Peter Zijlstra211de6e2014-09-30 19:23:08 +0200926/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100927 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
928 * perf_pmu_migrate_context() we need some magic.
929 *
930 * Those places that change perf_event::ctx will hold both
931 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
932 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +0200933 * Lock ordering is by mutex address. There are two other sites where
934 * perf_event_context::mutex nests and those are:
935 *
936 * - perf_event_exit_task_context() [ child , 0 ]
937 * __perf_event_exit_task()
938 * sync_child_event()
939 * put_event() [ parent, 1 ]
940 *
941 * - perf_event_init_context() [ parent, 0 ]
942 * inherit_task_group()
943 * inherit_group()
944 * inherit_event()
945 * perf_event_alloc()
946 * perf_init_event()
947 * perf_try_init_event() [ child , 1 ]
948 *
949 * While it appears there is an obvious deadlock here -- the parent and child
950 * nesting levels are inverted between the two. This is in fact safe because
951 * life-time rules separate them. That is an exiting task cannot fork, and a
952 * spawning task cannot (yet) exit.
953 *
954 * But remember that that these are parent<->child context relations, and
955 * migration does not affect children, therefore these two orderings should not
956 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100957 *
958 * The change in perf_event::ctx does not affect children (as claimed above)
959 * because the sys_perf_event_open() case will install a new event and break
960 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
961 * concerned with cpuctx and that doesn't have children.
962 *
963 * The places that change perf_event::ctx will issue:
964 *
965 * perf_remove_from_context();
966 * synchronize_rcu();
967 * perf_install_in_context();
968 *
969 * to affect the change. The remove_from_context() + synchronize_rcu() should
970 * quiesce the event, after which we can install it in the new location. This
971 * means that only external vectors (perf_fops, prctl) can perturb the event
972 * while in transit. Therefore all such accessors should also acquire
973 * perf_event_context::mutex to serialize against this.
974 *
975 * However; because event->ctx can change while we're waiting to acquire
976 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
977 * function.
978 *
979 * Lock order:
980 * task_struct::perf_event_mutex
981 * perf_event_context::mutex
982 * perf_event_context::lock
983 * perf_event::child_mutex;
984 * perf_event::mmap_mutex
985 * mmap_sem
986 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +0100987static struct perf_event_context *
988perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100989{
990 struct perf_event_context *ctx;
991
992again:
993 rcu_read_lock();
994 ctx = ACCESS_ONCE(event->ctx);
995 if (!atomic_inc_not_zero(&ctx->refcount)) {
996 rcu_read_unlock();
997 goto again;
998 }
999 rcu_read_unlock();
1000
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001001 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001002 if (event->ctx != ctx) {
1003 mutex_unlock(&ctx->mutex);
1004 put_ctx(ctx);
1005 goto again;
1006 }
1007
1008 return ctx;
1009}
1010
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001011static inline struct perf_event_context *
1012perf_event_ctx_lock(struct perf_event *event)
1013{
1014 return perf_event_ctx_lock_nested(event, 0);
1015}
1016
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001017static void perf_event_ctx_unlock(struct perf_event *event,
1018 struct perf_event_context *ctx)
1019{
1020 mutex_unlock(&ctx->mutex);
1021 put_ctx(ctx);
1022}
1023
1024/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001025 * This must be done under the ctx->lock, such as to serialize against
1026 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1027 * calling scheduler related locks and ctx->lock nests inside those.
1028 */
1029static __must_check struct perf_event_context *
1030unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001031{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001032 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1033
1034 lockdep_assert_held(&ctx->lock);
1035
1036 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001037 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001038 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001039
1040 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001041}
1042
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001043static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1044{
1045 /*
1046 * only top level events have the pid namespace they were created in
1047 */
1048 if (event->parent)
1049 event = event->parent;
1050
1051 return task_tgid_nr_ns(p, event->ns);
1052}
1053
1054static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1055{
1056 /*
1057 * only top level events have the pid namespace they were created in
1058 */
1059 if (event->parent)
1060 event = event->parent;
1061
1062 return task_pid_nr_ns(p, event->ns);
1063}
1064
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001065/*
1066 * If we inherit events we want to return the parent event id
1067 * to userspace.
1068 */
1069static u64 primary_event_id(struct perf_event *event)
1070{
1071 u64 id = event->id;
1072
1073 if (event->parent)
1074 id = event->parent->id;
1075
1076 return id;
1077}
1078
1079/*
1080 * Get the perf_event_context for a task and lock it.
1081 * This has to cope with with the fact that until it is locked,
1082 * the context could get moved to another task.
1083 */
1084static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001085perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001086{
1087 struct perf_event_context *ctx;
1088
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001089retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001090 /*
1091 * One of the few rules of preemptible RCU is that one cannot do
1092 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001093 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001094 * rcu_read_unlock_special().
1095 *
1096 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001097 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001098 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001099 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001100 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001101 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001102 if (ctx) {
1103 /*
1104 * If this context is a clone of another, it might
1105 * get swapped for another underneath us by
1106 * perf_event_task_sched_out, though the
1107 * rcu_read_lock() protects us from any context
1108 * getting freed. Lock the context and check if it
1109 * got swapped before we could get the lock, and retry
1110 * if so. If we locked the right context, then it
1111 * can't get swapped on us any more.
1112 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001113 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001114 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001115 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001116 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001117 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001118 goto retry;
1119 }
1120
1121 if (!atomic_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001122 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001123 ctx = NULL;
1124 }
1125 }
1126 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001127 if (!ctx)
1128 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001129 return ctx;
1130}
1131
1132/*
1133 * Get the context for a task and increment its pin_count so it
1134 * can't get swapped to another task. This also increments its
1135 * reference count so that the context can't get freed.
1136 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001137static struct perf_event_context *
1138perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001139{
1140 struct perf_event_context *ctx;
1141 unsigned long flags;
1142
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001143 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001144 if (ctx) {
1145 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001146 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001147 }
1148 return ctx;
1149}
1150
1151static void perf_unpin_context(struct perf_event_context *ctx)
1152{
1153 unsigned long flags;
1154
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001155 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001156 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001157 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001158}
1159
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001160/*
1161 * Update the record of the current time in a context.
1162 */
1163static void update_context_time(struct perf_event_context *ctx)
1164{
1165 u64 now = perf_clock();
1166
1167 ctx->time += now - ctx->timestamp;
1168 ctx->timestamp = now;
1169}
1170
Stephane Eranian41587552011-01-03 18:20:01 +02001171static u64 perf_event_time(struct perf_event *event)
1172{
1173 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001174
1175 if (is_cgroup_event(event))
1176 return perf_cgroup_event_time(event);
1177
Stephane Eranian41587552011-01-03 18:20:01 +02001178 return ctx ? ctx->time : 0;
1179}
1180
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001181/*
1182 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001183 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001184 */
1185static void update_event_times(struct perf_event *event)
1186{
1187 struct perf_event_context *ctx = event->ctx;
1188 u64 run_end;
1189
1190 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1191 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1192 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001193 /*
1194 * in cgroup mode, time_enabled represents
1195 * the time the event was enabled AND active
1196 * tasks were in the monitored cgroup. This is
1197 * independent of the activity of the context as
1198 * there may be a mix of cgroup and non-cgroup events.
1199 *
1200 * That is why we treat cgroup events differently
1201 * here.
1202 */
1203 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001204 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001205 else if (ctx->is_active)
1206 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001207 else
1208 run_end = event->tstamp_stopped;
1209
1210 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001211
1212 if (event->state == PERF_EVENT_STATE_INACTIVE)
1213 run_end = event->tstamp_stopped;
1214 else
Stephane Eranian41587552011-01-03 18:20:01 +02001215 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001216
1217 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001218
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001219}
1220
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001221/*
1222 * Update total_time_enabled and total_time_running for all events in a group.
1223 */
1224static void update_group_times(struct perf_event *leader)
1225{
1226 struct perf_event *event;
1227
1228 update_event_times(leader);
1229 list_for_each_entry(event, &leader->sibling_list, group_entry)
1230 update_event_times(event);
1231}
1232
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001233static struct list_head *
1234ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1235{
1236 if (event->attr.pinned)
1237 return &ctx->pinned_groups;
1238 else
1239 return &ctx->flexible_groups;
1240}
1241
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001242/*
1243 * Add a event from the lists for its context.
1244 * Must be called with ctx->mutex and ctx->lock held.
1245 */
1246static void
1247list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1248{
Peter Zijlstrac994d612016-01-08 09:20:23 +01001249 lockdep_assert_held(&ctx->lock);
1250
Peter Zijlstra8a495422010-05-27 15:47:49 +02001251 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1252 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001253
1254 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001255 * If we're a stand alone event or group leader, we go to the context
1256 * list, group events are kept attached to the group so that
1257 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001258 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001259 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001260 struct list_head *list;
1261
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001262 if (is_software_event(event))
1263 event->group_flags |= PERF_GROUP_SOFTWARE;
1264
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001265 list = ctx_group_list(event, ctx);
1266 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001267 }
1268
Peter Zijlstra08309372011-03-03 11:31:20 +01001269 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001270 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001271
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001272 list_add_rcu(&event->event_entry, &ctx->event_list);
1273 ctx->nr_events++;
1274 if (event->attr.inherit_stat)
1275 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001276
1277 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001278}
1279
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001280/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001281 * Initialize event state based on the perf_event_attr::disabled.
1282 */
1283static inline void perf_event__state_init(struct perf_event *event)
1284{
1285 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1286 PERF_EVENT_STATE_INACTIVE;
1287}
1288
Peter Zijlstraa7239682015-09-09 19:06:33 +02001289static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001290{
1291 int entry = sizeof(u64); /* value */
1292 int size = 0;
1293 int nr = 1;
1294
1295 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1296 size += sizeof(u64);
1297
1298 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1299 size += sizeof(u64);
1300
1301 if (event->attr.read_format & PERF_FORMAT_ID)
1302 entry += sizeof(u64);
1303
1304 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001305 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001306 size += sizeof(u64);
1307 }
1308
1309 size += entry * nr;
1310 event->read_size = size;
1311}
1312
Peter Zijlstraa7239682015-09-09 19:06:33 +02001313static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001314{
1315 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001316 u16 size = 0;
1317
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001318 if (sample_type & PERF_SAMPLE_IP)
1319 size += sizeof(data->ip);
1320
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001321 if (sample_type & PERF_SAMPLE_ADDR)
1322 size += sizeof(data->addr);
1323
1324 if (sample_type & PERF_SAMPLE_PERIOD)
1325 size += sizeof(data->period);
1326
Andi Kleenc3feedf2013-01-24 16:10:28 +01001327 if (sample_type & PERF_SAMPLE_WEIGHT)
1328 size += sizeof(data->weight);
1329
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001330 if (sample_type & PERF_SAMPLE_READ)
1331 size += event->read_size;
1332
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001333 if (sample_type & PERF_SAMPLE_DATA_SRC)
1334 size += sizeof(data->data_src.val);
1335
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001336 if (sample_type & PERF_SAMPLE_TRANSACTION)
1337 size += sizeof(data->txn);
1338
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001339 event->header_size = size;
1340}
1341
Peter Zijlstraa7239682015-09-09 19:06:33 +02001342/*
1343 * Called at perf_event creation and when events are attached/detached from a
1344 * group.
1345 */
1346static void perf_event__header_size(struct perf_event *event)
1347{
1348 __perf_event_read_size(event,
1349 event->group_leader->nr_siblings);
1350 __perf_event_header_size(event, event->attr.sample_type);
1351}
1352
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001353static void perf_event__id_header_size(struct perf_event *event)
1354{
1355 struct perf_sample_data *data;
1356 u64 sample_type = event->attr.sample_type;
1357 u16 size = 0;
1358
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001359 if (sample_type & PERF_SAMPLE_TID)
1360 size += sizeof(data->tid_entry);
1361
1362 if (sample_type & PERF_SAMPLE_TIME)
1363 size += sizeof(data->time);
1364
Adrian Hunterff3d5272013-08-27 11:23:07 +03001365 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1366 size += sizeof(data->id);
1367
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001368 if (sample_type & PERF_SAMPLE_ID)
1369 size += sizeof(data->id);
1370
1371 if (sample_type & PERF_SAMPLE_STREAM_ID)
1372 size += sizeof(data->stream_id);
1373
1374 if (sample_type & PERF_SAMPLE_CPU)
1375 size += sizeof(data->cpu_entry);
1376
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001377 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001378}
1379
Peter Zijlstraa7239682015-09-09 19:06:33 +02001380static bool perf_event_validate_size(struct perf_event *event)
1381{
1382 /*
1383 * The values computed here will be over-written when we actually
1384 * attach the event.
1385 */
1386 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1387 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1388 perf_event__id_header_size(event);
1389
1390 /*
1391 * Sum the lot; should not exceed the 64k limit we have on records.
1392 * Conservative limit to allow for callchains and other variable fields.
1393 */
1394 if (event->read_size + event->header_size +
1395 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1396 return false;
1397
1398 return true;
1399}
1400
Peter Zijlstra8a495422010-05-27 15:47:49 +02001401static void perf_group_attach(struct perf_event *event)
1402{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001403 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001404
Peter Zijlstra74c33372010-10-15 11:40:29 +02001405 /*
1406 * We can have double attach due to group movement in perf_event_open.
1407 */
1408 if (event->attach_state & PERF_ATTACH_GROUP)
1409 return;
1410
Peter Zijlstra8a495422010-05-27 15:47:49 +02001411 event->attach_state |= PERF_ATTACH_GROUP;
1412
1413 if (group_leader == event)
1414 return;
1415
Peter Zijlstra652884f2015-01-23 11:20:10 +01001416 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1417
Peter Zijlstra8a495422010-05-27 15:47:49 +02001418 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1419 !is_software_event(event))
1420 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1421
1422 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1423 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001424
1425 perf_event__header_size(group_leader);
1426
1427 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1428 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001429}
1430
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001431/*
1432 * Remove a event from the lists for its context.
1433 * Must be called with ctx->mutex and ctx->lock held.
1434 */
1435static void
1436list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1437{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001438 struct perf_cpu_context *cpuctx;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001439
1440 WARN_ON_ONCE(event->ctx != ctx);
1441 lockdep_assert_held(&ctx->lock);
1442
Peter Zijlstra8a495422010-05-27 15:47:49 +02001443 /*
1444 * We can have double detach due to exit/hot-unplug + close.
1445 */
1446 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001447 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001448
1449 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1450
Stephane Eranian68cacd22011-03-23 16:03:06 +01001451 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001452 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001453 cpuctx = __get_cpu_context(ctx);
1454 /*
1455 * if there are no more cgroup events
1456 * then cler cgrp to avoid stale pointer
1457 * in update_cgrp_time_from_cpuctx()
1458 */
1459 if (!ctx->nr_cgroups)
1460 cpuctx->cgrp = NULL;
1461 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001462
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001463 ctx->nr_events--;
1464 if (event->attr.inherit_stat)
1465 ctx->nr_stat--;
1466
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001467 list_del_rcu(&event->event_entry);
1468
Peter Zijlstra8a495422010-05-27 15:47:49 +02001469 if (event->group_leader == event)
1470 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001471
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001472 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001473
1474 /*
1475 * If event was in error state, then keep it
1476 * that way, otherwise bogus counts will be
1477 * returned on read(). The only way to get out
1478 * of error state is by explicit re-enabling
1479 * of the event
1480 */
1481 if (event->state > PERF_EVENT_STATE_OFF)
1482 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001483
1484 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001485}
1486
Peter Zijlstra8a495422010-05-27 15:47:49 +02001487static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001488{
1489 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001490 struct list_head *list = NULL;
1491
1492 /*
1493 * We can have double detach due to exit/hot-unplug + close.
1494 */
1495 if (!(event->attach_state & PERF_ATTACH_GROUP))
1496 return;
1497
1498 event->attach_state &= ~PERF_ATTACH_GROUP;
1499
1500 /*
1501 * If this is a sibling, remove it from its group.
1502 */
1503 if (event->group_leader != event) {
1504 list_del_init(&event->group_entry);
1505 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001506 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001507 }
1508
1509 if (!list_empty(&event->group_entry))
1510 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001511
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001512 /*
1513 * If this was a group event with sibling events then
1514 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001515 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001516 */
1517 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001518 if (list)
1519 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001520 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001521
1522 /* Inherit group flags from the previous leader */
1523 sibling->group_flags = event->group_flags;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001524
1525 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001526 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001527
1528out:
1529 perf_event__header_size(event->group_leader);
1530
1531 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1532 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001533}
1534
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001535/*
1536 * User event without the task.
1537 */
1538static bool is_orphaned_event(struct perf_event *event)
1539{
1540 return event && !is_kernel_event(event) && !event->owner;
1541}
1542
1543/*
1544 * Event has a parent but parent's task finished and it's
1545 * alive only because of children holding refference.
1546 */
1547static bool is_orphaned_child(struct perf_event *event)
1548{
1549 return is_orphaned_event(event->parent);
1550}
1551
1552static void orphans_remove_work(struct work_struct *work);
1553
1554static void schedule_orphans_remove(struct perf_event_context *ctx)
1555{
1556 if (!ctx->task || ctx->orphans_remove_sched || !perf_wq)
1557 return;
1558
1559 if (queue_delayed_work(perf_wq, &ctx->orphans_remove, 1)) {
1560 get_ctx(ctx);
1561 ctx->orphans_remove_sched = true;
1562 }
1563}
1564
1565static int __init perf_workqueue_init(void)
1566{
1567 perf_wq = create_singlethread_workqueue("perf");
1568 WARN(!perf_wq, "failed to create perf workqueue\n");
1569 return perf_wq ? 0 : -1;
1570}
1571
1572core_initcall(perf_workqueue_init);
1573
Mark Rutland66eb5792015-05-13 17:12:23 +01001574static inline int pmu_filter_match(struct perf_event *event)
1575{
1576 struct pmu *pmu = event->pmu;
1577 return pmu->filter_match ? pmu->filter_match(event) : 1;
1578}
1579
Stephane Eranianfa66f072010-08-26 16:40:01 +02001580static inline int
1581event_filter_match(struct perf_event *event)
1582{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001583 return (event->cpu == -1 || event->cpu == smp_processor_id())
Mark Rutland66eb5792015-05-13 17:12:23 +01001584 && perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001585}
1586
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001587static void
1588event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001589 struct perf_cpu_context *cpuctx,
1590 struct perf_event_context *ctx)
1591{
Stephane Eranian41587552011-01-03 18:20:01 +02001592 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001593 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001594
1595 WARN_ON_ONCE(event->ctx != ctx);
1596 lockdep_assert_held(&ctx->lock);
1597
Stephane Eranianfa66f072010-08-26 16:40:01 +02001598 /*
1599 * An event which could not be activated because of
1600 * filter mismatch still needs to have its timings
1601 * maintained, otherwise bogus information is return
1602 * via read() for time_enabled, time_running:
1603 */
1604 if (event->state == PERF_EVENT_STATE_INACTIVE
1605 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001606 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001607 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001608 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001609 }
1610
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001611 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001612 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001613
Alexander Shishkin44377272013-12-16 14:17:36 +02001614 perf_pmu_disable(event->pmu);
1615
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001616 event->state = PERF_EVENT_STATE_INACTIVE;
1617 if (event->pending_disable) {
1618 event->pending_disable = 0;
1619 event->state = PERF_EVENT_STATE_OFF;
1620 }
Stephane Eranian41587552011-01-03 18:20:01 +02001621 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001622 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001623 event->oncpu = -1;
1624
1625 if (!is_software_event(event))
1626 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001627 if (!--ctx->nr_active)
1628 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001629 if (event->attr.freq && event->attr.sample_freq)
1630 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001631 if (event->attr.exclusive || !cpuctx->active_oncpu)
1632 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001633
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001634 if (is_orphaned_child(event))
1635 schedule_orphans_remove(ctx);
1636
Alexander Shishkin44377272013-12-16 14:17:36 +02001637 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001638}
1639
1640static void
1641group_sched_out(struct perf_event *group_event,
1642 struct perf_cpu_context *cpuctx,
1643 struct perf_event_context *ctx)
1644{
1645 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001646 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001647
1648 event_sched_out(group_event, cpuctx, ctx);
1649
1650 /*
1651 * Schedule out siblings (if any):
1652 */
1653 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1654 event_sched_out(event, cpuctx, ctx);
1655
Stephane Eranianfa66f072010-08-26 16:40:01 +02001656 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001657 cpuctx->exclusive = 0;
1658}
1659
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001660struct remove_event {
1661 struct perf_event *event;
1662 bool detach_group;
1663};
1664
Peter Zijlstra00179602015-11-30 16:26:35 +01001665static void ___perf_remove_from_context(void *info)
1666{
1667 struct remove_event *re = info;
1668 struct perf_event *event = re->event;
1669 struct perf_event_context *ctx = event->ctx;
1670
1671 if (re->detach_group)
1672 perf_group_detach(event);
1673 list_del_event(event, ctx);
1674}
1675
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001676/*
1677 * Cross CPU call to remove a performance event
1678 *
1679 * We disable the event on the hardware level first. After that we
1680 * remove it from the context list.
1681 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001682static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001683{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001684 struct remove_event *re = info;
1685 struct perf_event *event = re->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001686 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001687 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001688
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001689 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001690 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001691 if (re->detach_group)
1692 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001693 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001694 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1695 ctx->is_active = 0;
1696 cpuctx->task_ctx = NULL;
1697 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001698 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001699
1700 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001701}
1702
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001703/*
1704 * Remove the event from a task's (or a CPU's) list of events.
1705 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001706 * CPU events are removed with a smp call. For task events we only
1707 * call when the task is on a CPU.
1708 *
1709 * If event->ctx is a cloned context, callers must make sure that
1710 * every task struct that event->ctx->task could possibly point to
1711 * remains valid. This is OK when called from perf_release since
1712 * that only calls us on the top-level context, which can't be a clone.
1713 * When called from perf_event_exit_task, it's OK because the
1714 * context has been detached from its task.
1715 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001716static void perf_remove_from_context(struct perf_event *event, bool detach_group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001717{
1718 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001719 struct remove_event re = {
1720 .event = event,
1721 .detach_group = detach_group,
1722 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001723
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001724 lockdep_assert_held(&ctx->mutex);
1725
Peter Zijlstra00179602015-11-30 16:26:35 +01001726 event_function_call(event, __perf_remove_from_context,
1727 ___perf_remove_from_context, &re);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001728}
1729
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001730/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001731 * Cross CPU call to disable a performance event
1732 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301733int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001734{
1735 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001736 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001737 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001738
1739 /*
1740 * If this is a per-task event, need to check whether this
1741 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001742 *
1743 * Can trigger due to concurrent perf_event_context_sched_out()
1744 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001745 */
1746 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001747 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001748
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001749 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001750
1751 /*
1752 * If the event is on, turn it off.
1753 * If it is in error state, leave it in error state.
1754 */
1755 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1756 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001757 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001758 update_group_times(event);
1759 if (event == event->group_leader)
1760 group_sched_out(event, cpuctx, ctx);
1761 else
1762 event_sched_out(event, cpuctx, ctx);
1763 event->state = PERF_EVENT_STATE_OFF;
1764 }
1765
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001766 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001767
1768 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001769}
1770
Peter Zijlstra7b648012015-12-03 18:35:21 +01001771void ___perf_event_disable(void *info)
1772{
1773 struct perf_event *event = info;
1774
1775 /*
1776 * Since we have the lock this context can't be scheduled
1777 * in, so we can change the state safely.
1778 */
1779 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1780 update_group_times(event);
1781 event->state = PERF_EVENT_STATE_OFF;
1782 }
1783}
1784
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001785/*
1786 * Disable a event.
1787 *
1788 * If event->ctx is a cloned context, callers must make sure that
1789 * every task struct that event->ctx->task could possibly point to
1790 * remains valid. This condition is satisifed when called through
1791 * perf_event_for_each_child or perf_event_for_each because they
1792 * hold the top-level event's child_mutex, so any descendant that
1793 * goes to exit will block in sync_child_event.
1794 * When called from perf_pending_event it's OK because event->ctx
1795 * is the current context on this CPU and preemption is disabled,
1796 * hence we can't get into perf_event_task_sched_out for this context.
1797 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001798static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001799{
1800 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001801
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001802 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001803 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001804 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001805 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001806 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001807 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001808
1809 event_function_call(event, __perf_event_disable,
1810 ___perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001811}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001812
1813/*
1814 * Strictly speaking kernel users cannot create groups and therefore this
1815 * interface does not need the perf_event_ctx_lock() magic.
1816 */
1817void perf_event_disable(struct perf_event *event)
1818{
1819 struct perf_event_context *ctx;
1820
1821 ctx = perf_event_ctx_lock(event);
1822 _perf_event_disable(event);
1823 perf_event_ctx_unlock(event, ctx);
1824}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001825EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001826
Stephane Eraniane5d13672011-02-14 11:20:01 +02001827static void perf_set_shadow_time(struct perf_event *event,
1828 struct perf_event_context *ctx,
1829 u64 tstamp)
1830{
1831 /*
1832 * use the correct time source for the time snapshot
1833 *
1834 * We could get by without this by leveraging the
1835 * fact that to get to this function, the caller
1836 * has most likely already called update_context_time()
1837 * and update_cgrp_time_xx() and thus both timestamp
1838 * are identical (or very close). Given that tstamp is,
1839 * already adjusted for cgroup, we could say that:
1840 * tstamp - ctx->timestamp
1841 * is equivalent to
1842 * tstamp - cgrp->timestamp.
1843 *
1844 * Then, in perf_output_read(), the calculation would
1845 * work with no changes because:
1846 * - event is guaranteed scheduled in
1847 * - no scheduled out in between
1848 * - thus the timestamp would be the same
1849 *
1850 * But this is a bit hairy.
1851 *
1852 * So instead, we have an explicit cgroup call to remain
1853 * within the time time source all along. We believe it
1854 * is cleaner and simpler to understand.
1855 */
1856 if (is_cgroup_event(event))
1857 perf_cgroup_set_shadow_time(event, tstamp);
1858 else
1859 event->shadow_ctx_time = tstamp - ctx->timestamp;
1860}
1861
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001862#define MAX_INTERRUPTS (~0ULL)
1863
1864static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001865static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001866
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001867static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001868event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001869 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001870 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001871{
Stephane Eranian41587552011-01-03 18:20:01 +02001872 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001873 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001874
Peter Zijlstra63342412014-05-05 11:49:16 +02001875 lockdep_assert_held(&ctx->lock);
1876
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001877 if (event->state <= PERF_EVENT_STATE_OFF)
1878 return 0;
1879
1880 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001881 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001882
1883 /*
1884 * Unthrottle events, since we scheduled we might have missed several
1885 * ticks already, also for a heavily scheduling task there is little
1886 * guarantee it'll get a tick in a timely manner.
1887 */
1888 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1889 perf_log_throttle(event, 1);
1890 event->hw.interrupts = 0;
1891 }
1892
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001893 /*
1894 * The new state must be visible before we turn it on in the hardware:
1895 */
1896 smp_wmb();
1897
Alexander Shishkin44377272013-12-16 14:17:36 +02001898 perf_pmu_disable(event->pmu);
1899
Shaohua Li72f669c2015-02-05 15:55:31 -08001900 perf_set_shadow_time(event, ctx, tstamp);
1901
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001902 perf_log_itrace_start(event);
1903
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001904 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001905 event->state = PERF_EVENT_STATE_INACTIVE;
1906 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001907 ret = -EAGAIN;
1908 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001909 }
1910
Peter Zijlstra00a29162015-07-27 10:35:07 +02001911 event->tstamp_running += tstamp - event->tstamp_stopped;
1912
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001913 if (!is_software_event(event))
1914 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001915 if (!ctx->nr_active++)
1916 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001917 if (event->attr.freq && event->attr.sample_freq)
1918 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001919
1920 if (event->attr.exclusive)
1921 cpuctx->exclusive = 1;
1922
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001923 if (is_orphaned_child(event))
1924 schedule_orphans_remove(ctx);
1925
Alexander Shishkin44377272013-12-16 14:17:36 +02001926out:
1927 perf_pmu_enable(event->pmu);
1928
1929 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001930}
1931
1932static int
1933group_sched_in(struct perf_event *group_event,
1934 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001935 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001936{
Lin Ming6bde9b62010-04-23 13:56:00 +08001937 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001938 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001939 u64 now = ctx->time;
1940 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001941
1942 if (group_event->state == PERF_EVENT_STATE_OFF)
1943 return 0;
1944
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001945 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08001946
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001947 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001948 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001949 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001950 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001951 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001952
1953 /*
1954 * Schedule in siblings as one group (if any):
1955 */
1956 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001957 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001958 partial_group = event;
1959 goto group_error;
1960 }
1961 }
1962
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001963 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001964 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001965
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001966group_error:
1967 /*
1968 * Groups can be scheduled in as one unit only, so undo any
1969 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001970 * The events up to the failed event are scheduled out normally,
1971 * tstamp_stopped will be updated.
1972 *
1973 * The failed events and the remaining siblings need to have
1974 * their timings updated as if they had gone thru event_sched_in()
1975 * and event_sched_out(). This is required to get consistent timings
1976 * across the group. This also takes care of the case where the group
1977 * could never be scheduled by ensuring tstamp_stopped is set to mark
1978 * the time the event was actually stopped, such that time delta
1979 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001980 */
1981 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1982 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001983 simulate = true;
1984
1985 if (simulate) {
1986 event->tstamp_running += now - event->tstamp_stopped;
1987 event->tstamp_stopped = now;
1988 } else {
1989 event_sched_out(event, cpuctx, ctx);
1990 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001991 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001992 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001993
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001994 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001995
Peter Zijlstra272325c2015-04-15 11:41:58 +02001996 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02001997
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001998 return -EAGAIN;
1999}
2000
2001/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002002 * Work out whether we can put this event group on the CPU now.
2003 */
2004static int group_can_go_on(struct perf_event *event,
2005 struct perf_cpu_context *cpuctx,
2006 int can_add_hw)
2007{
2008 /*
2009 * Groups consisting entirely of software events can always go on.
2010 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01002011 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002012 return 1;
2013 /*
2014 * If an exclusive group is already on, no other hardware
2015 * events can go on.
2016 */
2017 if (cpuctx->exclusive)
2018 return 0;
2019 /*
2020 * If this group is exclusive and there are already
2021 * events on the CPU, it can't go on.
2022 */
2023 if (event->attr.exclusive && cpuctx->active_oncpu)
2024 return 0;
2025 /*
2026 * Otherwise, try to add it if all previous groups were able
2027 * to go on.
2028 */
2029 return can_add_hw;
2030}
2031
2032static void add_event_to_ctx(struct perf_event *event,
2033 struct perf_event_context *ctx)
2034{
Stephane Eranian41587552011-01-03 18:20:01 +02002035 u64 tstamp = perf_event_time(event);
2036
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002037 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002038 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002039 event->tstamp_enabled = tstamp;
2040 event->tstamp_running = tstamp;
2041 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002042}
2043
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002044static void task_ctx_sched_out(struct perf_event_context *ctx);
2045static void
2046ctx_sched_in(struct perf_event_context *ctx,
2047 struct perf_cpu_context *cpuctx,
2048 enum event_type_t event_type,
2049 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002050
Peter Zijlstradce58552011-04-09 21:17:46 +02002051static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2052 struct perf_event_context *ctx,
2053 struct task_struct *task)
2054{
2055 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2056 if (ctx)
2057 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2058 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2059 if (ctx)
2060 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2061}
2062
Peter Zijlstra00179602015-11-30 16:26:35 +01002063static void ___perf_install_in_context(void *info)
2064{
2065 struct perf_event *event = info;
2066 struct perf_event_context *ctx = event->ctx;
2067
2068 /*
2069 * Since the task isn't running, its safe to add the event, us holding
2070 * the ctx->lock ensures the task won't get scheduled in.
2071 */
2072 add_event_to_ctx(event, ctx);
2073}
2074
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002075/*
2076 * Cross CPU call to install and enable a performance event
2077 *
2078 * Must be called with ctx->mutex held
2079 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002080static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002081{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002082 struct perf_event *event = info;
2083 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002084 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002085 struct perf_event_context *task_ctx = cpuctx->task_ctx;
2086 struct task_struct *task = current;
2087
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002088 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002089 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002090
2091 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002092 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002093 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002094 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002095 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002096
2097 /*
2098 * If the context we're installing events in is not the
2099 * active task_ctx, flip them.
2100 */
2101 if (ctx->task && task_ctx != ctx) {
2102 if (task_ctx)
2103 raw_spin_unlock(&task_ctx->lock);
2104 raw_spin_lock(&ctx->lock);
2105 task_ctx = ctx;
2106 }
2107
2108 if (task_ctx) {
2109 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002110 task = task_ctx->task;
2111 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002112
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002113 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002114
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002115 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002116 /*
2117 * update cgrp time only if current cgrp
2118 * matches event->cgrp. Must be done before
2119 * calling add_event_to_ctx()
2120 */
2121 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002122
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002123 add_event_to_ctx(event, ctx);
2124
2125 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002126 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002127 */
Peter Zijlstradce58552011-04-09 21:17:46 +02002128 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002129
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002130 perf_pmu_enable(cpuctx->ctx.pmu);
2131 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002132
2133 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002134}
2135
2136/*
2137 * Attach a performance event to a context
2138 *
2139 * First we add the event to the list with the hardware enable bit
2140 * in event->hw_config cleared.
2141 *
2142 * If the event is attached to a task which is on a CPU we use a smp
2143 * call to enable it in the task context. The task might have been
2144 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002145 */
2146static void
2147perf_install_in_context(struct perf_event_context *ctx,
2148 struct perf_event *event,
2149 int cpu)
2150{
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002151 lockdep_assert_held(&ctx->mutex);
2152
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002153 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002154 if (event->cpu != -1)
2155 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002156
Peter Zijlstra00179602015-11-30 16:26:35 +01002157 event_function_call(event, __perf_install_in_context,
2158 ___perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002159}
2160
2161/*
2162 * Put a event into inactive state and update time fields.
2163 * Enabling the leader of a group effectively enables all
2164 * the group members that aren't explicitly disabled, so we
2165 * have to update their ->tstamp_enabled also.
2166 * Note: this works for group members as well as group leaders
2167 * since the non-leader members' sibling_lists will be empty.
2168 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002169static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002170{
2171 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002172 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002173
2174 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002175 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002176 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002177 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2178 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002179 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002180}
2181
2182/*
2183 * Cross CPU call to enable a performance event
2184 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002185static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002186{
2187 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002188 struct perf_event_context *ctx = event->ctx;
2189 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002190 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002191 int err;
2192
Jiri Olsa06f41792013-07-09 17:44:11 +02002193 /*
2194 * There's a time window between 'ctx->is_active' check
2195 * in perf_event_enable function and this place having:
2196 * - IRQs on
2197 * - ctx->lock unlocked
2198 *
2199 * where the task could be killed and 'ctx' deactivated
2200 * by perf_event_exit_task.
2201 */
2202 if (!ctx->is_active)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002203 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002204
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002205 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002206 update_context_time(ctx);
2207
2208 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2209 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002210
2211 /*
2212 * set current task's cgroup time reference point
2213 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002214 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002215
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002216 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002217
Stephane Eraniane5d13672011-02-14 11:20:01 +02002218 if (!event_filter_match(event)) {
2219 if (is_cgroup_event(event))
2220 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002221 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002222 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002223
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002224 /*
2225 * If the event is in a group and isn't the group leader,
2226 * then don't put it on unless the group is on.
2227 */
2228 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2229 goto unlock;
2230
2231 if (!group_can_go_on(event, cpuctx, 1)) {
2232 err = -EEXIST;
2233 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002234 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002235 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002236 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01002237 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002238 }
2239
2240 if (err) {
2241 /*
2242 * If this event can't go on and it's part of a
2243 * group, then the whole group has to come off.
2244 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002245 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002246 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra272325c2015-04-15 11:41:58 +02002247 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002248 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002249 if (leader->attr.pinned) {
2250 update_group_times(leader);
2251 leader->state = PERF_EVENT_STATE_ERROR;
2252 }
2253 }
2254
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002255unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002256 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002257
2258 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002259}
2260
Peter Zijlstra7b648012015-12-03 18:35:21 +01002261void ___perf_event_enable(void *info)
2262{
2263 __perf_event_mark_enabled((struct perf_event *)info);
2264}
2265
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002266/*
2267 * Enable a event.
2268 *
2269 * If event->ctx is a cloned context, callers must make sure that
2270 * every task struct that event->ctx->task could possibly point to
2271 * remains valid. This condition is satisfied when called through
2272 * perf_event_for_each_child or perf_event_for_each as described
2273 * for perf_event_disable.
2274 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002275static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002276{
2277 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002278
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002279 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002280 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
2281 raw_spin_unlock_irq(&ctx->lock);
2282 return;
2283 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002284
2285 /*
2286 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002287 *
2288 * That way, if we see the event in error state below, we know that it
2289 * has gone back into error state, as distinct from the task having
2290 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002291 */
2292 if (event->state == PERF_EVENT_STATE_ERROR)
2293 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002294 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002295
Peter Zijlstra7b648012015-12-03 18:35:21 +01002296 event_function_call(event, __perf_event_enable,
2297 ___perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002298}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002299
2300/*
2301 * See perf_event_disable();
2302 */
2303void perf_event_enable(struct perf_event *event)
2304{
2305 struct perf_event_context *ctx;
2306
2307 ctx = perf_event_ctx_lock(event);
2308 _perf_event_enable(event);
2309 perf_event_ctx_unlock(event, ctx);
2310}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002311EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002312
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002313static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002314{
2315 /*
2316 * not supported on inherited events
2317 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002318 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002319 return -EINVAL;
2320
2321 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002322 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002323
2324 return 0;
2325}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002326
2327/*
2328 * See perf_event_disable()
2329 */
2330int perf_event_refresh(struct perf_event *event, int refresh)
2331{
2332 struct perf_event_context *ctx;
2333 int ret;
2334
2335 ctx = perf_event_ctx_lock(event);
2336 ret = _perf_event_refresh(event, refresh);
2337 perf_event_ctx_unlock(event, ctx);
2338
2339 return ret;
2340}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002341EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002342
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002343static void ctx_sched_out(struct perf_event_context *ctx,
2344 struct perf_cpu_context *cpuctx,
2345 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002346{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002347 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002348 struct perf_event *event;
2349
2350 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002351
Peter Zijlstradb24d332011-04-09 21:17:45 +02002352 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002353 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002354 return;
2355
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002356 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002357 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002358 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002359 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002360
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002361 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002362 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002363 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2364 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002365 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002366
Peter Zijlstradb24d332011-04-09 21:17:45 +02002367 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002368 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002369 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002370 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002371 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002372}
2373
2374/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002375 * Test whether two contexts are equivalent, i.e. whether they have both been
2376 * cloned from the same version of the same context.
2377 *
2378 * Equivalence is measured using a generation number in the context that is
2379 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2380 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002381 */
2382static int context_equiv(struct perf_event_context *ctx1,
2383 struct perf_event_context *ctx2)
2384{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002385 lockdep_assert_held(&ctx1->lock);
2386 lockdep_assert_held(&ctx2->lock);
2387
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002388 /* Pinning disables the swap optimization */
2389 if (ctx1->pin_count || ctx2->pin_count)
2390 return 0;
2391
2392 /* If ctx1 is the parent of ctx2 */
2393 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2394 return 1;
2395
2396 /* If ctx2 is the parent of ctx1 */
2397 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2398 return 1;
2399
2400 /*
2401 * If ctx1 and ctx2 have the same parent; we flatten the parent
2402 * hierarchy, see perf_event_init_context().
2403 */
2404 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2405 ctx1->parent_gen == ctx2->parent_gen)
2406 return 1;
2407
2408 /* Unmatched */
2409 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002410}
2411
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002412static void __perf_event_sync_stat(struct perf_event *event,
2413 struct perf_event *next_event)
2414{
2415 u64 value;
2416
2417 if (!event->attr.inherit_stat)
2418 return;
2419
2420 /*
2421 * Update the event value, we cannot use perf_event_read()
2422 * because we're in the middle of a context switch and have IRQs
2423 * disabled, which upsets smp_call_function_single(), however
2424 * we know the event must be on the current CPU, therefore we
2425 * don't need to use it.
2426 */
2427 switch (event->state) {
2428 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002429 event->pmu->read(event);
2430 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002431
2432 case PERF_EVENT_STATE_INACTIVE:
2433 update_event_times(event);
2434 break;
2435
2436 default:
2437 break;
2438 }
2439
2440 /*
2441 * In order to keep per-task stats reliable we need to flip the event
2442 * values when we flip the contexts.
2443 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002444 value = local64_read(&next_event->count);
2445 value = local64_xchg(&event->count, value);
2446 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002447
2448 swap(event->total_time_enabled, next_event->total_time_enabled);
2449 swap(event->total_time_running, next_event->total_time_running);
2450
2451 /*
2452 * Since we swizzled the values, update the user visible data too.
2453 */
2454 perf_event_update_userpage(event);
2455 perf_event_update_userpage(next_event);
2456}
2457
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002458static void perf_event_sync_stat(struct perf_event_context *ctx,
2459 struct perf_event_context *next_ctx)
2460{
2461 struct perf_event *event, *next_event;
2462
2463 if (!ctx->nr_stat)
2464 return;
2465
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002466 update_context_time(ctx);
2467
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002468 event = list_first_entry(&ctx->event_list,
2469 struct perf_event, event_entry);
2470
2471 next_event = list_first_entry(&next_ctx->event_list,
2472 struct perf_event, event_entry);
2473
2474 while (&event->event_entry != &ctx->event_list &&
2475 &next_event->event_entry != &next_ctx->event_list) {
2476
2477 __perf_event_sync_stat(event, next_event);
2478
2479 event = list_next_entry(event, event_entry);
2480 next_event = list_next_entry(next_event, event_entry);
2481 }
2482}
2483
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002484static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2485 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002486{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002487 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002488 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002489 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002490 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002491 int do_switch = 1;
2492
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002493 if (likely(!ctx))
2494 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002495
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002496 cpuctx = __get_cpu_context(ctx);
2497 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002498 return;
2499
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002500 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002501 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002502 if (!next_ctx)
2503 goto unlock;
2504
2505 parent = rcu_dereference(ctx->parent_ctx);
2506 next_parent = rcu_dereference(next_ctx->parent_ctx);
2507
2508 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002509 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002510 goto unlock;
2511
2512 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002513 /*
2514 * Looks like the two contexts are clones, so we might be
2515 * able to optimize the context switch. We lock both
2516 * contexts and check that they are clones under the
2517 * lock (including re-checking that neither has been
2518 * uncloned in the meantime). It doesn't matter which
2519 * order we take the locks because no other cpu could
2520 * be trying to lock both of these tasks.
2521 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002522 raw_spin_lock(&ctx->lock);
2523 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002524 if (context_equiv(ctx, next_ctx)) {
2525 /*
2526 * XXX do we need a memory barrier of sorts
2527 * wrt to rcu_dereference() of perf_event_ctxp
2528 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002529 task->perf_event_ctxp[ctxn] = next_ctx;
2530 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002531 ctx->task = next;
2532 next_ctx->task = task;
Yan, Zheng5a158c32014-11-04 21:56:02 -05002533
2534 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2535
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002536 do_switch = 0;
2537
2538 perf_event_sync_stat(ctx, next_ctx);
2539 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002540 raw_spin_unlock(&next_ctx->lock);
2541 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002542 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002543unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002544 rcu_read_unlock();
2545
2546 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002547 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002548 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002549 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002550 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002551 }
2552}
2553
Yan, Zhengba532502014-11-04 21:55:58 -05002554void perf_sched_cb_dec(struct pmu *pmu)
2555{
2556 this_cpu_dec(perf_sched_cb_usages);
2557}
2558
2559void perf_sched_cb_inc(struct pmu *pmu)
2560{
2561 this_cpu_inc(perf_sched_cb_usages);
2562}
2563
2564/*
2565 * This function provides the context switch callback to the lower code
2566 * layer. It is invoked ONLY when the context switch callback is enabled.
2567 */
2568static void perf_pmu_sched_task(struct task_struct *prev,
2569 struct task_struct *next,
2570 bool sched_in)
2571{
2572 struct perf_cpu_context *cpuctx;
2573 struct pmu *pmu;
2574 unsigned long flags;
2575
2576 if (prev == next)
2577 return;
2578
2579 local_irq_save(flags);
2580
2581 rcu_read_lock();
2582
2583 list_for_each_entry_rcu(pmu, &pmus, entry) {
2584 if (pmu->sched_task) {
2585 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2586
2587 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2588
2589 perf_pmu_disable(pmu);
2590
2591 pmu->sched_task(cpuctx->task_ctx, sched_in);
2592
2593 perf_pmu_enable(pmu);
2594
2595 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2596 }
2597 }
2598
2599 rcu_read_unlock();
2600
2601 local_irq_restore(flags);
2602}
2603
Adrian Hunter45ac1402015-07-21 12:44:02 +03002604static void perf_event_switch(struct task_struct *task,
2605 struct task_struct *next_prev, bool sched_in);
2606
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002607#define for_each_task_context_nr(ctxn) \
2608 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2609
2610/*
2611 * Called from scheduler to remove the events of the current task,
2612 * with interrupts disabled.
2613 *
2614 * We stop each event and update the event value in event->count.
2615 *
2616 * This does not protect us against NMI, but disable()
2617 * sets the disabled bit in the control field of event _before_
2618 * accessing the event control register. If a NMI hits, then it will
2619 * not restart the event.
2620 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002621void __perf_event_task_sched_out(struct task_struct *task,
2622 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002623{
2624 int ctxn;
2625
Yan, Zhengba532502014-11-04 21:55:58 -05002626 if (__this_cpu_read(perf_sched_cb_usages))
2627 perf_pmu_sched_task(task, next, false);
2628
Adrian Hunter45ac1402015-07-21 12:44:02 +03002629 if (atomic_read(&nr_switch_events))
2630 perf_event_switch(task, next, false);
2631
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002632 for_each_task_context_nr(ctxn)
2633 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002634
2635 /*
2636 * if cgroup events exist on this CPU, then we need
2637 * to check if we have to switch out PMU state.
2638 * cgroup event are system-wide mode only
2639 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002640 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002641 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002642}
2643
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002644static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002645{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002646 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002647
2648 if (!cpuctx->task_ctx)
2649 return;
2650
2651 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2652 return;
2653
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002654 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002655 cpuctx->task_ctx = NULL;
2656}
2657
2658/*
2659 * Called with IRQs disabled
2660 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002661static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2662 enum event_type_t event_type)
2663{
2664 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002665}
2666
2667static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002668ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002669 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002670{
2671 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002672
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002673 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2674 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002675 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002676 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002677 continue;
2678
Stephane Eraniane5d13672011-02-14 11:20:01 +02002679 /* may need to reset tstamp_enabled */
2680 if (is_cgroup_event(event))
2681 perf_cgroup_mark_enabled(event, ctx);
2682
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002683 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002684 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002685
2686 /*
2687 * If this pinned group hasn't been scheduled,
2688 * put it in error state.
2689 */
2690 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2691 update_group_times(event);
2692 event->state = PERF_EVENT_STATE_ERROR;
2693 }
2694 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002695}
2696
2697static void
2698ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002699 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002700{
2701 struct perf_event *event;
2702 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002703
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002704 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2705 /* Ignore events in OFF or ERROR state */
2706 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002707 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002708 /*
2709 * Listen to the 'cpu' scheduling filter constraint
2710 * of events:
2711 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002712 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002713 continue;
2714
Stephane Eraniane5d13672011-02-14 11:20:01 +02002715 /* may need to reset tstamp_enabled */
2716 if (is_cgroup_event(event))
2717 perf_cgroup_mark_enabled(event, ctx);
2718
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002719 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002720 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002721 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002722 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002723 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002724}
2725
2726static void
2727ctx_sched_in(struct perf_event_context *ctx,
2728 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002729 enum event_type_t event_type,
2730 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002731{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002732 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002733 u64 now;
2734
2735 lockdep_assert_held(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002736
Peter Zijlstradb24d332011-04-09 21:17:45 +02002737 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002738 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002739 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002740
Stephane Eraniane5d13672011-02-14 11:20:01 +02002741 now = perf_clock();
2742 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002743 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002744 /*
2745 * First go through the list and put on any pinned groups
2746 * in order to give them the best chance of going on.
2747 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002748 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002749 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002750
2751 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002752 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002753 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002754}
2755
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002756static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002757 enum event_type_t event_type,
2758 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002759{
2760 struct perf_event_context *ctx = &cpuctx->ctx;
2761
Stephane Eraniane5d13672011-02-14 11:20:01 +02002762 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002763}
2764
Stephane Eraniane5d13672011-02-14 11:20:01 +02002765static void perf_event_context_sched_in(struct perf_event_context *ctx,
2766 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002767{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002768 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002769
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002770 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002771 if (cpuctx->task_ctx == ctx)
2772 return;
2773
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002774 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002775 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002776 /*
2777 * We want to keep the following priority order:
2778 * cpu pinned (that don't need to move), task pinned,
2779 * cpu flexible, task flexible.
2780 */
2781 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2782
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002783 if (ctx->nr_events)
2784 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002785
Gleb Natapov86b47c22011-11-22 16:08:21 +02002786 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2787
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002788 perf_pmu_enable(ctx->pmu);
2789 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002790}
2791
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002792/*
2793 * Called from scheduler to add the events of the current task
2794 * with interrupts disabled.
2795 *
2796 * We restore the event value and then enable it.
2797 *
2798 * This does not protect us against NMI, but enable()
2799 * sets the enabled bit in the control field of event _before_
2800 * accessing the event control register. If a NMI hits, then it will
2801 * keep the event running.
2802 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002803void __perf_event_task_sched_in(struct task_struct *prev,
2804 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002805{
2806 struct perf_event_context *ctx;
2807 int ctxn;
2808
2809 for_each_task_context_nr(ctxn) {
2810 ctx = task->perf_event_ctxp[ctxn];
2811 if (likely(!ctx))
2812 continue;
2813
Stephane Eraniane5d13672011-02-14 11:20:01 +02002814 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002815 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002816 /*
2817 * if cgroup events exist on this CPU, then we need
2818 * to check if we have to switch in PMU state.
2819 * cgroup event are system-wide mode only
2820 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002821 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002822 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002823
Adrian Hunter45ac1402015-07-21 12:44:02 +03002824 if (atomic_read(&nr_switch_events))
2825 perf_event_switch(task, prev, true);
2826
Yan, Zhengba532502014-11-04 21:55:58 -05002827 if (__this_cpu_read(perf_sched_cb_usages))
2828 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002829}
2830
Peter Zijlstraabd50712010-01-26 18:50:16 +01002831static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2832{
2833 u64 frequency = event->attr.sample_freq;
2834 u64 sec = NSEC_PER_SEC;
2835 u64 divisor, dividend;
2836
2837 int count_fls, nsec_fls, frequency_fls, sec_fls;
2838
2839 count_fls = fls64(count);
2840 nsec_fls = fls64(nsec);
2841 frequency_fls = fls64(frequency);
2842 sec_fls = 30;
2843
2844 /*
2845 * We got @count in @nsec, with a target of sample_freq HZ
2846 * the target period becomes:
2847 *
2848 * @count * 10^9
2849 * period = -------------------
2850 * @nsec * sample_freq
2851 *
2852 */
2853
2854 /*
2855 * Reduce accuracy by one bit such that @a and @b converge
2856 * to a similar magnitude.
2857 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002858#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002859do { \
2860 if (a##_fls > b##_fls) { \
2861 a >>= 1; \
2862 a##_fls--; \
2863 } else { \
2864 b >>= 1; \
2865 b##_fls--; \
2866 } \
2867} while (0)
2868
2869 /*
2870 * Reduce accuracy until either term fits in a u64, then proceed with
2871 * the other, so that finally we can do a u64/u64 division.
2872 */
2873 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2874 REDUCE_FLS(nsec, frequency);
2875 REDUCE_FLS(sec, count);
2876 }
2877
2878 if (count_fls + sec_fls > 64) {
2879 divisor = nsec * frequency;
2880
2881 while (count_fls + sec_fls > 64) {
2882 REDUCE_FLS(count, sec);
2883 divisor >>= 1;
2884 }
2885
2886 dividend = count * sec;
2887 } else {
2888 dividend = count * sec;
2889
2890 while (nsec_fls + frequency_fls > 64) {
2891 REDUCE_FLS(nsec, frequency);
2892 dividend >>= 1;
2893 }
2894
2895 divisor = nsec * frequency;
2896 }
2897
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002898 if (!divisor)
2899 return dividend;
2900
Peter Zijlstraabd50712010-01-26 18:50:16 +01002901 return div64_u64(dividend, divisor);
2902}
2903
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002904static DEFINE_PER_CPU(int, perf_throttled_count);
2905static DEFINE_PER_CPU(u64, perf_throttled_seq);
2906
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002907static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002908{
2909 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002910 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002911 s64 delta;
2912
Peter Zijlstraabd50712010-01-26 18:50:16 +01002913 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002914
2915 delta = (s64)(period - hwc->sample_period);
2916 delta = (delta + 7) / 8; /* low pass filter */
2917
2918 sample_period = hwc->sample_period + delta;
2919
2920 if (!sample_period)
2921 sample_period = 1;
2922
2923 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002924
Peter Zijlstrae7850592010-05-21 14:43:08 +02002925 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002926 if (disable)
2927 event->pmu->stop(event, PERF_EF_UPDATE);
2928
Peter Zijlstrae7850592010-05-21 14:43:08 +02002929 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002930
2931 if (disable)
2932 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002933 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002934}
2935
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002936/*
2937 * combine freq adjustment with unthrottling to avoid two passes over the
2938 * events. At the same time, make sure, having freq events does not change
2939 * the rate of unthrottling as that would introduce bias.
2940 */
2941static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2942 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002943{
2944 struct perf_event *event;
2945 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002946 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002947 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002948
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002949 /*
2950 * only need to iterate over all events iff:
2951 * - context have events in frequency mode (needs freq adjust)
2952 * - there are events to unthrottle on this cpu
2953 */
2954 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002955 return;
2956
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002957 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002958 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002959
Paul Mackerras03541f82009-10-14 16:58:03 +11002960 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002961 if (event->state != PERF_EVENT_STATE_ACTIVE)
2962 continue;
2963
Stephane Eranian5632ab12011-01-03 18:20:01 +02002964 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002965 continue;
2966
Alexander Shishkin44377272013-12-16 14:17:36 +02002967 perf_pmu_disable(event->pmu);
2968
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002969 hwc = &event->hw;
2970
Jiri Olsaae23bff2013-08-24 16:45:54 +02002971 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002972 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002973 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002974 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002975 }
2976
2977 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02002978 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002979
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002980 /*
2981 * stop the event and update event->count
2982 */
2983 event->pmu->stop(event, PERF_EF_UPDATE);
2984
Peter Zijlstrae7850592010-05-21 14:43:08 +02002985 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002986 delta = now - hwc->freq_count_stamp;
2987 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002988
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002989 /*
2990 * restart the event
2991 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002992 * we have stopped the event so tell that
2993 * to perf_adjust_period() to avoid stopping it
2994 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002995 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002996 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002997 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002998
2999 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003000 next:
3001 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003002 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003003
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003004 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003005 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003006}
3007
3008/*
3009 * Round-robin a context's events:
3010 */
3011static void rotate_ctx(struct perf_event_context *ctx)
3012{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003013 /*
3014 * Rotate the first entry last of non-pinned groups. Rotation might be
3015 * disabled by the inheritance code.
3016 */
3017 if (!ctx->rotate_disable)
3018 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003019}
3020
Stephane Eranian9e630202013-04-03 14:21:33 +02003021static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003022{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003023 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003024 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003025
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003026 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003027 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3028 rotate = 1;
3029 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003030
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003031 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003032 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003033 if (ctx->nr_events != ctx->nr_active)
3034 rotate = 1;
3035 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003036
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003037 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003038 goto done;
3039
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003040 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003041 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003042
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003043 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3044 if (ctx)
3045 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003046
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003047 rotate_ctx(&cpuctx->ctx);
3048 if (ctx)
3049 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003050
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003051 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003052
3053 perf_pmu_enable(cpuctx->ctx.pmu);
3054 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003055done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003056
3057 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003058}
3059
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003060#ifdef CONFIG_NO_HZ_FULL
3061bool perf_event_can_stop_tick(void)
3062{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003063 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003064 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003065 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003066 else
3067 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003068}
3069#endif
3070
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003071void perf_event_task_tick(void)
3072{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003073 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3074 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003075 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003076
3077 WARN_ON(!irqs_disabled());
3078
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003079 __this_cpu_inc(perf_throttled_seq);
3080 throttled = __this_cpu_xchg(perf_throttled_count, 0);
3081
Mark Rutland2fde4f92015-01-07 15:01:54 +00003082 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003083 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003084}
3085
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003086static int event_enable_on_exec(struct perf_event *event,
3087 struct perf_event_context *ctx)
3088{
3089 if (!event->attr.enable_on_exec)
3090 return 0;
3091
3092 event->attr.enable_on_exec = 0;
3093 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3094 return 0;
3095
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003096 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003097
3098 return 1;
3099}
3100
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003101/*
3102 * Enable all of a task's events that have been marked enable-on-exec.
3103 * This expects task == current.
3104 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003105static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003106{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003107 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003108 struct perf_event *event;
3109 unsigned long flags;
3110 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003111 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003112
3113 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003114 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003115 if (!ctx || !ctx->nr_events)
3116 goto out;
3117
Stephane Eraniane566b762011-04-06 02:54:54 +02003118 /*
3119 * We must ctxsw out cgroup events to avoid conflict
3120 * when invoking perf_task_event_sched_in() later on
3121 * in this function. Otherwise we end up trying to
3122 * ctxswin cgroup events which are already scheduled
3123 * in.
3124 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003125 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003126
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003127 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02003128 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003129
Peter Zijlstrab79387e2011-11-22 11:25:43 +01003130 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003131 ret = event_enable_on_exec(event, ctx);
3132 if (ret)
3133 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003134 }
3135
3136 /*
3137 * Unclone this context if we enabled any event.
3138 */
3139 if (enabled)
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003140 clone_ctx = unclone_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003141
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003142 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003143
Stephane Eraniane566b762011-04-06 02:54:54 +02003144 /*
3145 * Also calls ctxswin for cgroup events, if any:
3146 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003147 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003148out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003149 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003150
3151 if (clone_ctx)
3152 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003153}
3154
Peter Zijlstrae041e322014-05-21 17:32:19 +02003155void perf_event_exec(void)
3156{
Peter Zijlstrae041e322014-05-21 17:32:19 +02003157 int ctxn;
3158
3159 rcu_read_lock();
Peter Zijlstrac1274492015-12-10 20:57:40 +01003160 for_each_task_context_nr(ctxn)
3161 perf_event_enable_on_exec(ctxn);
Peter Zijlstrae041e322014-05-21 17:32:19 +02003162 rcu_read_unlock();
3163}
3164
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003165struct perf_read_data {
3166 struct perf_event *event;
3167 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003168 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003169};
3170
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003171/*
3172 * Cross CPU call to read the hardware event
3173 */
3174static void __perf_event_read(void *info)
3175{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003176 struct perf_read_data *data = info;
3177 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003178 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003179 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003180 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003181
3182 /*
3183 * If this is a task context, we need to check whether it is
3184 * the current task context of this cpu. If not it has been
3185 * scheduled out before the smp call arrived. In that case
3186 * event->count would have been updated to a recent sample
3187 * when the event was scheduled out.
3188 */
3189 if (ctx->task && cpuctx->task_ctx != ctx)
3190 return;
3191
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003192 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003193 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003194 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003195 update_cgrp_time_from_event(event);
3196 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003197
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003198 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003199 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003200 goto unlock;
3201
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003202 if (!data->group) {
3203 pmu->read(event);
3204 data->ret = 0;
3205 goto unlock;
3206 }
3207
3208 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3209
3210 pmu->read(event);
3211
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003212 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3213 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003214 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3215 /*
3216 * Use sibling's PMU rather than @event's since
3217 * sibling could be on different (eg: software) PMU.
3218 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003219 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003220 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003221 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003222
3223 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003224
3225unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003226 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003227}
3228
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003229static inline u64 perf_event_count(struct perf_event *event)
3230{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003231 if (event->pmu->count)
3232 return event->pmu->count(event);
3233
3234 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003235}
3236
Kaixu Xiaffe86902015-08-06 07:02:32 +00003237/*
3238 * NMI-safe method to read a local event, that is an event that
3239 * is:
3240 * - either for the current task, or for this CPU
3241 * - does not have inherit set, for inherited task events
3242 * will not be local and we cannot read them atomically
3243 * - must not have a pmu::count method
3244 */
3245u64 perf_event_read_local(struct perf_event *event)
3246{
3247 unsigned long flags;
3248 u64 val;
3249
3250 /*
3251 * Disabling interrupts avoids all counter scheduling (context
3252 * switches, timer based rotation and IPIs).
3253 */
3254 local_irq_save(flags);
3255
3256 /* If this is a per-task event, it must be for current */
3257 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3258 event->hw.target != current);
3259
3260 /* If this is a per-CPU event, it must be for this CPU */
3261 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3262 event->cpu != smp_processor_id());
3263
3264 /*
3265 * It must not be an event with inherit set, we cannot read
3266 * all child counters from atomic context.
3267 */
3268 WARN_ON_ONCE(event->attr.inherit);
3269
3270 /*
3271 * It must not have a pmu::count method, those are not
3272 * NMI safe.
3273 */
3274 WARN_ON_ONCE(event->pmu->count);
3275
3276 /*
3277 * If the event is currently on this CPU, its either a per-task event,
3278 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3279 * oncpu == -1).
3280 */
3281 if (event->oncpu == smp_processor_id())
3282 event->pmu->read(event);
3283
3284 val = local64_read(&event->count);
3285 local_irq_restore(flags);
3286
3287 return val;
3288}
3289
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003290static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003291{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003292 int ret = 0;
3293
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003294 /*
3295 * If event is enabled and currently active on a CPU, update the
3296 * value in the event structure:
3297 */
3298 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003299 struct perf_read_data data = {
3300 .event = event,
3301 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003302 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003303 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003304 smp_call_function_single(event->oncpu,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003305 __perf_event_read, &data, 1);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003306 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003307 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003308 struct perf_event_context *ctx = event->ctx;
3309 unsigned long flags;
3310
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003311 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003312 /*
3313 * may read while context is not active
3314 * (e.g., thread is blocked), in that case
3315 * we cannot update context time
3316 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003317 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003318 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003319 update_cgrp_time_from_event(event);
3320 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003321 if (group)
3322 update_group_times(event);
3323 else
3324 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003325 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003326 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003327
3328 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003329}
3330
3331/*
3332 * Initialize the perf_event context in a task_struct:
3333 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003334static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003335{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003336 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003337 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003338 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003339 INIT_LIST_HEAD(&ctx->pinned_groups);
3340 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003341 INIT_LIST_HEAD(&ctx->event_list);
3342 atomic_set(&ctx->refcount, 1);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003343 INIT_DELAYED_WORK(&ctx->orphans_remove, orphans_remove_work);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003344}
3345
Peter Zijlstraeb184472010-09-07 15:55:13 +02003346static struct perf_event_context *
3347alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003348{
3349 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003350
3351 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3352 if (!ctx)
3353 return NULL;
3354
3355 __perf_event_init_context(ctx);
3356 if (task) {
3357 ctx->task = task;
3358 get_task_struct(task);
3359 }
3360 ctx->pmu = pmu;
3361
3362 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003363}
3364
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003365static struct task_struct *
3366find_lively_task_by_vpid(pid_t vpid)
3367{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003368 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003369 int err;
3370
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003371 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003372 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003373 task = current;
3374 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003375 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003376 if (task)
3377 get_task_struct(task);
3378 rcu_read_unlock();
3379
3380 if (!task)
3381 return ERR_PTR(-ESRCH);
3382
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003383 /* Reuse ptrace permission checks for now. */
3384 err = -EACCES;
3385 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3386 goto errout;
3387
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003388 return task;
3389errout:
3390 put_task_struct(task);
3391 return ERR_PTR(err);
3392
3393}
3394
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003395/*
3396 * Returns a matching context with refcount and pincount.
3397 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003398static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003399find_get_context(struct pmu *pmu, struct task_struct *task,
3400 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003401{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003402 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003403 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003404 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003405 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003406 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003407 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003408
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003409 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003410 /* Must be root to operate on a CPU event: */
3411 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3412 return ERR_PTR(-EACCES);
3413
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003414 /*
3415 * We could be clever and allow to attach a event to an
3416 * offline CPU and activate it when the CPU comes up, but
3417 * that's for later.
3418 */
3419 if (!cpu_online(cpu))
3420 return ERR_PTR(-ENODEV);
3421
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003422 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003423 ctx = &cpuctx->ctx;
3424 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003425 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003426
3427 return ctx;
3428 }
3429
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003430 err = -EINVAL;
3431 ctxn = pmu->task_ctx_nr;
3432 if (ctxn < 0)
3433 goto errout;
3434
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003435 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3436 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3437 if (!task_ctx_data) {
3438 err = -ENOMEM;
3439 goto errout;
3440 }
3441 }
3442
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003443retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003444 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003445 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003446 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003447 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003448
3449 if (task_ctx_data && !ctx->task_ctx_data) {
3450 ctx->task_ctx_data = task_ctx_data;
3451 task_ctx_data = NULL;
3452 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003453 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003454
3455 if (clone_ctx)
3456 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003457 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003458 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003459 err = -ENOMEM;
3460 if (!ctx)
3461 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003462
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003463 if (task_ctx_data) {
3464 ctx->task_ctx_data = task_ctx_data;
3465 task_ctx_data = NULL;
3466 }
3467
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003468 err = 0;
3469 mutex_lock(&task->perf_event_mutex);
3470 /*
3471 * If it has already passed perf_event_exit_task().
3472 * we must see PF_EXITING, it takes this mutex too.
3473 */
3474 if (task->flags & PF_EXITING)
3475 err = -ESRCH;
3476 else if (task->perf_event_ctxp[ctxn])
3477 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003478 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003479 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003480 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003481 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003482 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003483 mutex_unlock(&task->perf_event_mutex);
3484
3485 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003486 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003487
3488 if (err == -EAGAIN)
3489 goto retry;
3490 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003491 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003492 }
3493
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003494 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003495 return ctx;
3496
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003497errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003498 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003499 return ERR_PTR(err);
3500}
3501
Li Zefan6fb29152009-10-15 11:21:42 +08003502static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003503static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003504
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003505static void free_event_rcu(struct rcu_head *head)
3506{
3507 struct perf_event *event;
3508
3509 event = container_of(head, struct perf_event, rcu_head);
3510 if (event->ns)
3511 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003512 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003513 kfree(event);
3514}
3515
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003516static void ring_buffer_attach(struct perf_event *event,
3517 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003518
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003519static void unaccount_event_cpu(struct perf_event *event, int cpu)
3520{
3521 if (event->parent)
3522 return;
3523
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003524 if (is_cgroup_event(event))
3525 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3526}
3527
3528static void unaccount_event(struct perf_event *event)
3529{
3530 if (event->parent)
3531 return;
3532
3533 if (event->attach_state & PERF_ATTACH_TASK)
3534 static_key_slow_dec_deferred(&perf_sched_events);
3535 if (event->attr.mmap || event->attr.mmap_data)
3536 atomic_dec(&nr_mmap_events);
3537 if (event->attr.comm)
3538 atomic_dec(&nr_comm_events);
3539 if (event->attr.task)
3540 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003541 if (event->attr.freq)
3542 atomic_dec(&nr_freq_events);
Adrian Hunter45ac1402015-07-21 12:44:02 +03003543 if (event->attr.context_switch) {
3544 static_key_slow_dec_deferred(&perf_sched_events);
3545 atomic_dec(&nr_switch_events);
3546 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003547 if (is_cgroup_event(event))
3548 static_key_slow_dec_deferred(&perf_sched_events);
3549 if (has_branch_stack(event))
3550 static_key_slow_dec_deferred(&perf_sched_events);
3551
3552 unaccount_event_cpu(event, event->cpu);
3553}
3554
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003555/*
3556 * The following implement mutual exclusion of events on "exclusive" pmus
3557 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3558 * at a time, so we disallow creating events that might conflict, namely:
3559 *
3560 * 1) cpu-wide events in the presence of per-task events,
3561 * 2) per-task events in the presence of cpu-wide events,
3562 * 3) two matching events on the same context.
3563 *
3564 * The former two cases are handled in the allocation path (perf_event_alloc(),
3565 * __free_event()), the latter -- before the first perf_install_in_context().
3566 */
3567static int exclusive_event_init(struct perf_event *event)
3568{
3569 struct pmu *pmu = event->pmu;
3570
3571 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3572 return 0;
3573
3574 /*
3575 * Prevent co-existence of per-task and cpu-wide events on the
3576 * same exclusive pmu.
3577 *
3578 * Negative pmu::exclusive_cnt means there are cpu-wide
3579 * events on this "exclusive" pmu, positive means there are
3580 * per-task events.
3581 *
3582 * Since this is called in perf_event_alloc() path, event::ctx
3583 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3584 * to mean "per-task event", because unlike other attach states it
3585 * never gets cleared.
3586 */
3587 if (event->attach_state & PERF_ATTACH_TASK) {
3588 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
3589 return -EBUSY;
3590 } else {
3591 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
3592 return -EBUSY;
3593 }
3594
3595 return 0;
3596}
3597
3598static void exclusive_event_destroy(struct perf_event *event)
3599{
3600 struct pmu *pmu = event->pmu;
3601
3602 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3603 return;
3604
3605 /* see comment in exclusive_event_init() */
3606 if (event->attach_state & PERF_ATTACH_TASK)
3607 atomic_dec(&pmu->exclusive_cnt);
3608 else
3609 atomic_inc(&pmu->exclusive_cnt);
3610}
3611
3612static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
3613{
3614 if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
3615 (e1->cpu == e2->cpu ||
3616 e1->cpu == -1 ||
3617 e2->cpu == -1))
3618 return true;
3619 return false;
3620}
3621
3622/* Called under the same ctx::mutex as perf_install_in_context() */
3623static bool exclusive_event_installable(struct perf_event *event,
3624 struct perf_event_context *ctx)
3625{
3626 struct perf_event *iter_event;
3627 struct pmu *pmu = event->pmu;
3628
3629 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3630 return true;
3631
3632 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
3633 if (exclusive_event_match(iter_event, event))
3634 return false;
3635 }
3636
3637 return true;
3638}
3639
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003640static void __free_event(struct perf_event *event)
3641{
3642 if (!event->parent) {
3643 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3644 put_callchain_buffers();
3645 }
3646
Alexei Starovoitovdead9f22015-05-15 12:15:21 -07003647 perf_event_free_bpf_prog(event);
3648
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003649 if (event->destroy)
3650 event->destroy(event);
3651
3652 if (event->ctx)
3653 put_ctx(event->ctx);
3654
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003655 if (event->pmu) {
3656 exclusive_event_destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08003657 module_put(event->pmu->module);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003658 }
Yan, Zhengc464c762014-03-18 16:56:41 +08003659
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003660 call_rcu(&event->rcu_head, free_event_rcu);
3661}
Peter Zijlstra683ede42014-05-05 12:11:24 +02003662
3663static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003664{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003665 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003666
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003667 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003668
Frederic Weisbecker76369132011-05-19 19:55:04 +02003669 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003670 /*
3671 * Can happen when we close an event with re-directed output.
3672 *
3673 * Since we have a 0 refcount, perf_mmap_close() will skip
3674 * over us; possibly making our ring_buffer_put() the last.
3675 */
3676 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003677 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003678 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003679 }
3680
Stephane Eraniane5d13672011-02-14 11:20:01 +02003681 if (is_cgroup_event(event))
3682 perf_detach_cgroup(event);
3683
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003684 __free_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003685}
3686
Peter Zijlstra683ede42014-05-05 12:11:24 +02003687/*
3688 * Used to free events which have a known refcount of 1, such as in error paths
3689 * where the event isn't exposed yet and inherited events.
3690 */
3691static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003692{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003693 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3694 "unexpected event refcount: %ld; ptr=%p\n",
3695 atomic_long_read(&event->refcount), event)) {
3696 /* leak to avoid use-after-free */
3697 return;
3698 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003699
Peter Zijlstra683ede42014-05-05 12:11:24 +02003700 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003701}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003702
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003703/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003704 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003705 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003706static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003707{
Peter Zijlstra88821352010-11-09 19:01:43 +01003708 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003709
Peter Zijlstra88821352010-11-09 19:01:43 +01003710 rcu_read_lock();
3711 owner = ACCESS_ONCE(event->owner);
3712 /*
3713 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3714 * !owner it means the list deletion is complete and we can indeed
3715 * free this event, otherwise we need to serialize on
3716 * owner->perf_event_mutex.
3717 */
3718 smp_read_barrier_depends();
3719 if (owner) {
3720 /*
3721 * Since delayed_put_task_struct() also drops the last
3722 * task reference we can safely take a new reference
3723 * while holding the rcu_read_lock().
3724 */
3725 get_task_struct(owner);
3726 }
3727 rcu_read_unlock();
3728
3729 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003730 /*
3731 * If we're here through perf_event_exit_task() we're already
3732 * holding ctx->mutex which would be an inversion wrt. the
3733 * normal lock order.
3734 *
3735 * However we can safely take this lock because its the child
3736 * ctx->mutex.
3737 */
3738 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
3739
Peter Zijlstra88821352010-11-09 19:01:43 +01003740 /*
3741 * We have to re-check the event->owner field, if it is cleared
3742 * we raced with perf_event_exit_task(), acquiring the mutex
3743 * ensured they're done, and we can proceed with freeing the
3744 * event.
3745 */
3746 if (event->owner)
3747 list_del_init(&event->owner_entry);
3748 mutex_unlock(&owner->perf_event_mutex);
3749 put_task_struct(owner);
3750 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003751}
3752
Jiri Olsaf8697762014-08-01 14:33:01 +02003753static void put_event(struct perf_event *event)
3754{
Peter Zijlstraa83fe282015-01-29 14:44:34 +01003755 struct perf_event_context *ctx;
Jiri Olsaf8697762014-08-01 14:33:01 +02003756
3757 if (!atomic_long_dec_and_test(&event->refcount))
3758 return;
3759
3760 if (!is_kernel_event(event))
3761 perf_remove_from_owner(event);
Peter Zijlstra88821352010-11-09 19:01:43 +01003762
Peter Zijlstra683ede42014-05-05 12:11:24 +02003763 /*
3764 * There are two ways this annotation is useful:
3765 *
3766 * 1) there is a lock recursion from perf_event_exit_task
3767 * see the comment there.
3768 *
3769 * 2) there is a lock-inversion with mmap_sem through
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07003770 * perf_read_group(), which takes faults while
Peter Zijlstra683ede42014-05-05 12:11:24 +02003771 * holding ctx->mutex, however this is called after
3772 * the last filedesc died, so there is no possibility
3773 * to trigger the AB-BA case.
3774 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01003775 ctx = perf_event_ctx_lock_nested(event, SINGLE_DEPTH_NESTING);
3776 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra683ede42014-05-05 12:11:24 +02003777 perf_remove_from_context(event, true);
Leon Yud415a7f2015-02-26 20:43:33 +08003778 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra683ede42014-05-05 12:11:24 +02003779
3780 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003781}
3782
Peter Zijlstra683ede42014-05-05 12:11:24 +02003783int perf_event_release_kernel(struct perf_event *event)
3784{
3785 put_event(event);
3786 return 0;
3787}
3788EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3789
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02003790/*
3791 * Called when the last reference to the file is gone.
3792 */
Al Viroa6fa9412012-08-20 14:59:25 +01003793static int perf_release(struct inode *inode, struct file *file)
3794{
3795 put_event(file->private_data);
3796 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003797}
3798
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003799/*
3800 * Remove all orphanes events from the context.
3801 */
3802static void orphans_remove_work(struct work_struct *work)
3803{
3804 struct perf_event_context *ctx;
3805 struct perf_event *event, *tmp;
3806
3807 ctx = container_of(work, struct perf_event_context,
3808 orphans_remove.work);
3809
3810 mutex_lock(&ctx->mutex);
3811 list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) {
3812 struct perf_event *parent_event = event->parent;
3813
3814 if (!is_orphaned_child(event))
3815 continue;
3816
3817 perf_remove_from_context(event, true);
3818
3819 mutex_lock(&parent_event->child_mutex);
3820 list_del_init(&event->child_list);
3821 mutex_unlock(&parent_event->child_mutex);
3822
3823 free_event(event);
3824 put_event(parent_event);
3825 }
3826
3827 raw_spin_lock_irq(&ctx->lock);
3828 ctx->orphans_remove_sched = false;
3829 raw_spin_unlock_irq(&ctx->lock);
3830 mutex_unlock(&ctx->mutex);
3831
3832 put_ctx(ctx);
3833}
3834
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003835u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003836{
3837 struct perf_event *child;
3838 u64 total = 0;
3839
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003840 *enabled = 0;
3841 *running = 0;
3842
Peter Zijlstra6f105812009-11-20 22:19:56 +01003843 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003844
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003845 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003846 total += perf_event_count(event);
3847
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003848 *enabled += event->total_time_enabled +
3849 atomic64_read(&event->child_total_time_enabled);
3850 *running += event->total_time_running +
3851 atomic64_read(&event->child_total_time_running);
3852
3853 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003854 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003855 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003856 *enabled += child->total_time_enabled;
3857 *running += child->total_time_running;
3858 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003859 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003860
3861 return total;
3862}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003863EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003864
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003865static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003866 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003867{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003868 struct perf_event *sub;
3869 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003870 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003871
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003872 ret = perf_event_read(leader, true);
3873 if (ret)
3874 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003875
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003876 /*
3877 * Since we co-schedule groups, {enabled,running} times of siblings
3878 * will be identical to those of the leader, so we only publish one
3879 * set.
3880 */
3881 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3882 values[n++] += leader->total_time_enabled +
3883 atomic64_read(&leader->child_total_time_enabled);
3884 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003885
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003886 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3887 values[n++] += leader->total_time_running +
3888 atomic64_read(&leader->child_total_time_running);
3889 }
3890
3891 /*
3892 * Write {count,id} tuples for every sibling.
3893 */
3894 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003895 if (read_format & PERF_FORMAT_ID)
3896 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003897
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003898 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003899 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003900 if (read_format & PERF_FORMAT_ID)
3901 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003902 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003903
3904 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003905}
3906
3907static int perf_read_group(struct perf_event *event,
3908 u64 read_format, char __user *buf)
3909{
3910 struct perf_event *leader = event->group_leader, *child;
3911 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003912 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003913 u64 *values;
3914
3915 lockdep_assert_held(&ctx->mutex);
3916
3917 values = kzalloc(event->read_size, GFP_KERNEL);
3918 if (!values)
3919 return -ENOMEM;
3920
3921 values[0] = 1 + leader->nr_siblings;
3922
3923 /*
3924 * By locking the child_mutex of the leader we effectively
3925 * lock the child list of all siblings.. XXX explain how.
3926 */
3927 mutex_lock(&leader->child_mutex);
3928
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003929 ret = __perf_read_group_add(leader, read_format, values);
3930 if (ret)
3931 goto unlock;
3932
3933 list_for_each_entry(child, &leader->child_list, child_list) {
3934 ret = __perf_read_group_add(child, read_format, values);
3935 if (ret)
3936 goto unlock;
3937 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003938
3939 mutex_unlock(&leader->child_mutex);
3940
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003941 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003942 if (copy_to_user(buf, values, event->read_size))
3943 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003944 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003945
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003946unlock:
3947 mutex_unlock(&leader->child_mutex);
3948out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003949 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003950 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003951}
3952
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07003953static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003954 u64 read_format, char __user *buf)
3955{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003956 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003957 u64 values[4];
3958 int n = 0;
3959
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003960 values[n++] = perf_event_read_value(event, &enabled, &running);
3961 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3962 values[n++] = enabled;
3963 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3964 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003965 if (read_format & PERF_FORMAT_ID)
3966 values[n++] = primary_event_id(event);
3967
3968 if (copy_to_user(buf, values, n * sizeof(u64)))
3969 return -EFAULT;
3970
3971 return n * sizeof(u64);
3972}
3973
Jiri Olsadc633982014-09-12 13:18:26 +02003974static bool is_event_hup(struct perf_event *event)
3975{
3976 bool no_children;
3977
3978 if (event->state != PERF_EVENT_STATE_EXIT)
3979 return false;
3980
3981 mutex_lock(&event->child_mutex);
3982 no_children = list_empty(&event->child_list);
3983 mutex_unlock(&event->child_mutex);
3984 return no_children;
3985}
3986
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003987/*
3988 * Read the performance event - simple non blocking version for now
3989 */
3990static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07003991__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003992{
3993 u64 read_format = event->attr.read_format;
3994 int ret;
3995
3996 /*
3997 * Return end-of-file for a read on a event that is in
3998 * error state (i.e. because it was pinned but it couldn't be
3999 * scheduled on to the CPU at some point).
4000 */
4001 if (event->state == PERF_EVENT_STATE_ERROR)
4002 return 0;
4003
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004004 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004005 return -ENOSPC;
4006
4007 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004008 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004009 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004010 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004011 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004012
4013 return ret;
4014}
4015
4016static ssize_t
4017perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4018{
4019 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004020 struct perf_event_context *ctx;
4021 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004022
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004023 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004024 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004025 perf_event_ctx_unlock(event, ctx);
4026
4027 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004028}
4029
4030static unsigned int perf_poll(struct file *file, poll_table *wait)
4031{
4032 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004033 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004034 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004035
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004036 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004037
Jiri Olsadc633982014-09-12 13:18:26 +02004038 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004039 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004040
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004041 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004042 * Pin the event->rb by taking event->mmap_mutex; otherwise
4043 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004044 */
4045 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004046 rb = event->rb;
4047 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004048 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004049 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004050 return events;
4051}
4052
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004053static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004054{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004055 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004056 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004057 perf_event_update_userpage(event);
4058}
4059
4060/*
4061 * Holding the top-level event's child_mutex means that any
4062 * descendant process that has inherited this event will block
4063 * in sync_child_event if it goes to exit, thus satisfying the
4064 * task existence requirements of perf_event_enable/disable.
4065 */
4066static void perf_event_for_each_child(struct perf_event *event,
4067 void (*func)(struct perf_event *))
4068{
4069 struct perf_event *child;
4070
4071 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004072
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004073 mutex_lock(&event->child_mutex);
4074 func(event);
4075 list_for_each_entry(child, &event->child_list, child_list)
4076 func(child);
4077 mutex_unlock(&event->child_mutex);
4078}
4079
4080static void perf_event_for_each(struct perf_event *event,
4081 void (*func)(struct perf_event *))
4082{
4083 struct perf_event_context *ctx = event->ctx;
4084 struct perf_event *sibling;
4085
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004086 lockdep_assert_held(&ctx->mutex);
4087
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004088 event = event->group_leader;
4089
4090 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004091 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004092 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004093}
4094
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004095struct period_event {
4096 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004097 u64 value;
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004098};
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004099
Peter Zijlstra00179602015-11-30 16:26:35 +01004100static void ___perf_event_period(void *info)
4101{
4102 struct period_event *pe = info;
4103 struct perf_event *event = pe->event;
4104 u64 value = pe->value;
4105
4106 if (event->attr.freq) {
4107 event->attr.sample_freq = value;
4108 } else {
4109 event->attr.sample_period = value;
4110 event->hw.sample_period = value;
4111 }
4112
4113 local64_set(&event->hw.period_left, 0);
4114}
4115
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004116static int __perf_event_period(void *info)
4117{
4118 struct period_event *pe = info;
4119 struct perf_event *event = pe->event;
4120 struct perf_event_context *ctx = event->ctx;
4121 u64 value = pe->value;
4122 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004123
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004124 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004125 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004126 event->attr.sample_freq = value;
4127 } else {
4128 event->attr.sample_period = value;
4129 event->hw.sample_period = value;
4130 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004131
4132 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4133 if (active) {
4134 perf_pmu_disable(ctx->pmu);
4135 event->pmu->stop(event, PERF_EF_UPDATE);
4136 }
4137
4138 local64_set(&event->hw.period_left, 0);
4139
4140 if (active) {
4141 event->pmu->start(event, PERF_EF_RELOAD);
4142 perf_pmu_enable(ctx->pmu);
4143 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004144 raw_spin_unlock(&ctx->lock);
Peter Zijlstrabad71922013-11-27 13:54:38 +00004145
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004146 return 0;
4147}
4148
4149static int perf_event_period(struct perf_event *event, u64 __user *arg)
4150{
4151 struct period_event pe = { .event = event, };
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004152 u64 value;
4153
4154 if (!is_sampling_event(event))
4155 return -EINVAL;
4156
4157 if (copy_from_user(&value, arg, sizeof(value)))
4158 return -EFAULT;
4159
4160 if (!value)
4161 return -EINVAL;
4162
4163 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4164 return -EINVAL;
4165
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004166 pe.value = value;
4167
Peter Zijlstra00179602015-11-30 16:26:35 +01004168 event_function_call(event, __perf_event_period,
4169 ___perf_event_period, &pe);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004170
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004171 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004172}
4173
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004174static const struct file_operations perf_fops;
4175
Al Viro2903ff02012-08-28 12:52:22 -04004176static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004177{
Al Viro2903ff02012-08-28 12:52:22 -04004178 struct fd f = fdget(fd);
4179 if (!f.file)
4180 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004181
Al Viro2903ff02012-08-28 12:52:22 -04004182 if (f.file->f_op != &perf_fops) {
4183 fdput(f);
4184 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004185 }
Al Viro2903ff02012-08-28 12:52:22 -04004186 *p = f;
4187 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004188}
4189
4190static int perf_event_set_output(struct perf_event *event,
4191 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004192static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004193static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004194
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004195static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004196{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004197 void (*func)(struct perf_event *);
4198 u32 flags = arg;
4199
4200 switch (cmd) {
4201 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004202 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004203 break;
4204 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004205 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004206 break;
4207 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004208 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004209 break;
4210
4211 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004212 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004213
4214 case PERF_EVENT_IOC_PERIOD:
4215 return perf_event_period(event, (u64 __user *)arg);
4216
Jiri Olsacf4957f2012-10-24 13:37:58 +02004217 case PERF_EVENT_IOC_ID:
4218 {
4219 u64 id = primary_event_id(event);
4220
4221 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4222 return -EFAULT;
4223 return 0;
4224 }
4225
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004226 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004227 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004228 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004229 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004230 struct perf_event *output_event;
4231 struct fd output;
4232 ret = perf_fget_light(arg, &output);
4233 if (ret)
4234 return ret;
4235 output_event = output.file->private_data;
4236 ret = perf_event_set_output(event, output_event);
4237 fdput(output);
4238 } else {
4239 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004240 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004241 return ret;
4242 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004243
Li Zefan6fb29152009-10-15 11:21:42 +08004244 case PERF_EVENT_IOC_SET_FILTER:
4245 return perf_event_set_filter(event, (void __user *)arg);
4246
Alexei Starovoitov25415172015-03-25 12:49:20 -07004247 case PERF_EVENT_IOC_SET_BPF:
4248 return perf_event_set_bpf_prog(event, arg);
4249
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004250 default:
4251 return -ENOTTY;
4252 }
4253
4254 if (flags & PERF_IOC_FLAG_GROUP)
4255 perf_event_for_each(event, func);
4256 else
4257 perf_event_for_each_child(event, func);
4258
4259 return 0;
4260}
4261
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004262static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4263{
4264 struct perf_event *event = file->private_data;
4265 struct perf_event_context *ctx;
4266 long ret;
4267
4268 ctx = perf_event_ctx_lock(event);
4269 ret = _perf_ioctl(event, cmd, arg);
4270 perf_event_ctx_unlock(event, ctx);
4271
4272 return ret;
4273}
4274
Pawel Mollb3f20782014-06-13 16:03:32 +01004275#ifdef CONFIG_COMPAT
4276static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4277 unsigned long arg)
4278{
4279 switch (_IOC_NR(cmd)) {
4280 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4281 case _IOC_NR(PERF_EVENT_IOC_ID):
4282 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4283 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4284 cmd &= ~IOCSIZE_MASK;
4285 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4286 }
4287 break;
4288 }
4289 return perf_ioctl(file, cmd, arg);
4290}
4291#else
4292# define perf_compat_ioctl NULL
4293#endif
4294
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004295int perf_event_task_enable(void)
4296{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004297 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004298 struct perf_event *event;
4299
4300 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004301 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4302 ctx = perf_event_ctx_lock(event);
4303 perf_event_for_each_child(event, _perf_event_enable);
4304 perf_event_ctx_unlock(event, ctx);
4305 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004306 mutex_unlock(&current->perf_event_mutex);
4307
4308 return 0;
4309}
4310
4311int perf_event_task_disable(void)
4312{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004313 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004314 struct perf_event *event;
4315
4316 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004317 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4318 ctx = perf_event_ctx_lock(event);
4319 perf_event_for_each_child(event, _perf_event_disable);
4320 perf_event_ctx_unlock(event, ctx);
4321 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004322 mutex_unlock(&current->perf_event_mutex);
4323
4324 return 0;
4325}
4326
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004327static int perf_event_index(struct perf_event *event)
4328{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004329 if (event->hw.state & PERF_HES_STOPPED)
4330 return 0;
4331
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004332 if (event->state != PERF_EVENT_STATE_ACTIVE)
4333 return 0;
4334
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004335 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004336}
4337
Eric B Munsonc4794292011-06-23 16:34:38 -04004338static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004339 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004340 u64 *enabled,
4341 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004342{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004343 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004344
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004345 *now = perf_clock();
4346 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004347 *enabled = ctx_time - event->tstamp_enabled;
4348 *running = ctx_time - event->tstamp_running;
4349}
4350
Peter Zijlstrafa731582013-09-19 10:16:42 +02004351static void perf_event_init_userpage(struct perf_event *event)
4352{
4353 struct perf_event_mmap_page *userpg;
4354 struct ring_buffer *rb;
4355
4356 rcu_read_lock();
4357 rb = rcu_dereference(event->rb);
4358 if (!rb)
4359 goto unlock;
4360
4361 userpg = rb->user_page;
4362
4363 /* Allow new userspace to detect that bit 0 is deprecated */
4364 userpg->cap_bit0_is_deprecated = 1;
4365 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004366 userpg->data_offset = PAGE_SIZE;
4367 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004368
4369unlock:
4370 rcu_read_unlock();
4371}
4372
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004373void __weak arch_perf_update_userpage(
4374 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004375{
4376}
4377
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004378/*
4379 * Callers need to ensure there can be no nesting of this function, otherwise
4380 * the seqlock logic goes bad. We can not serialize this because the arch
4381 * code calls this from NMI context.
4382 */
4383void perf_event_update_userpage(struct perf_event *event)
4384{
4385 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004386 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004387 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004388
4389 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004390 rb = rcu_dereference(event->rb);
4391 if (!rb)
4392 goto unlock;
4393
Eric B Munson0d641202011-06-24 12:26:26 -04004394 /*
4395 * compute total_time_enabled, total_time_running
4396 * based on snapshot values taken when the event
4397 * was last scheduled in.
4398 *
4399 * we cannot simply called update_context_time()
4400 * because of locking issue as we can be called in
4401 * NMI context
4402 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004403 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004404
Frederic Weisbecker76369132011-05-19 19:55:04 +02004405 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004406 /*
4407 * Disable preemption so as to not let the corresponding user-space
4408 * spin too long if we get preempted.
4409 */
4410 preempt_disable();
4411 ++userpg->lock;
4412 barrier();
4413 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004414 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004415 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004416 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004417
Eric B Munson0d641202011-06-24 12:26:26 -04004418 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004419 atomic64_read(&event->child_total_time_enabled);
4420
Eric B Munson0d641202011-06-24 12:26:26 -04004421 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004422 atomic64_read(&event->child_total_time_running);
4423
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004424 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004425
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004426 barrier();
4427 ++userpg->lock;
4428 preempt_enable();
4429unlock:
4430 rcu_read_unlock();
4431}
4432
Peter Zijlstra906010b2009-09-21 16:08:49 +02004433static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4434{
4435 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004436 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004437 int ret = VM_FAULT_SIGBUS;
4438
4439 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4440 if (vmf->pgoff == 0)
4441 ret = 0;
4442 return ret;
4443 }
4444
4445 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004446 rb = rcu_dereference(event->rb);
4447 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004448 goto unlock;
4449
4450 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4451 goto unlock;
4452
Frederic Weisbecker76369132011-05-19 19:55:04 +02004453 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004454 if (!vmf->page)
4455 goto unlock;
4456
4457 get_page(vmf->page);
4458 vmf->page->mapping = vma->vm_file->f_mapping;
4459 vmf->page->index = vmf->pgoff;
4460
4461 ret = 0;
4462unlock:
4463 rcu_read_unlock();
4464
4465 return ret;
4466}
4467
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004468static void ring_buffer_attach(struct perf_event *event,
4469 struct ring_buffer *rb)
4470{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004471 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004472 unsigned long flags;
4473
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004474 if (event->rb) {
4475 /*
4476 * Should be impossible, we set this when removing
4477 * event->rb_entry and wait/clear when adding event->rb_entry.
4478 */
4479 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004480
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004481 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004482 spin_lock_irqsave(&old_rb->event_lock, flags);
4483 list_del_rcu(&event->rb_entry);
4484 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004485
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004486 event->rcu_batches = get_state_synchronize_rcu();
4487 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004488 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004489
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004490 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004491 if (event->rcu_pending) {
4492 cond_synchronize_rcu(event->rcu_batches);
4493 event->rcu_pending = 0;
4494 }
4495
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004496 spin_lock_irqsave(&rb->event_lock, flags);
4497 list_add_rcu(&event->rb_entry, &rb->event_list);
4498 spin_unlock_irqrestore(&rb->event_lock, flags);
4499 }
4500
4501 rcu_assign_pointer(event->rb, rb);
4502
4503 if (old_rb) {
4504 ring_buffer_put(old_rb);
4505 /*
4506 * Since we detached before setting the new rb, so that we
4507 * could attach the new rb, we could have missed a wakeup.
4508 * Provide it now.
4509 */
4510 wake_up_all(&event->waitq);
4511 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004512}
4513
4514static void ring_buffer_wakeup(struct perf_event *event)
4515{
4516 struct ring_buffer *rb;
4517
4518 rcu_read_lock();
4519 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004520 if (rb) {
4521 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4522 wake_up_all(&event->waitq);
4523 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004524 rcu_read_unlock();
4525}
4526
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004527struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004528{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004529 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004530
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004531 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004532 rb = rcu_dereference(event->rb);
4533 if (rb) {
4534 if (!atomic_inc_not_zero(&rb->refcount))
4535 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004536 }
4537 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004538
Frederic Weisbecker76369132011-05-19 19:55:04 +02004539 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004540}
4541
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004542void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004543{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004544 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004545 return;
4546
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004547 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004548
Frederic Weisbecker76369132011-05-19 19:55:04 +02004549 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004550}
4551
4552static void perf_mmap_open(struct vm_area_struct *vma)
4553{
4554 struct perf_event *event = vma->vm_file->private_data;
4555
4556 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004557 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004558
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004559 if (vma->vm_pgoff)
4560 atomic_inc(&event->rb->aux_mmap_count);
4561
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004562 if (event->pmu->event_mapped)
4563 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004564}
4565
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004566/*
4567 * A buffer can be mmap()ed multiple times; either directly through the same
4568 * event, or through other events by use of perf_event_set_output().
4569 *
4570 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4571 * the buffer here, where we still have a VM context. This means we need
4572 * to detach all events redirecting to us.
4573 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004574static void perf_mmap_close(struct vm_area_struct *vma)
4575{
4576 struct perf_event *event = vma->vm_file->private_data;
4577
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004578 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004579 struct user_struct *mmap_user = rb->mmap_user;
4580 int mmap_locked = rb->mmap_locked;
4581 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004582
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004583 if (event->pmu->event_unmapped)
4584 event->pmu->event_unmapped(event);
4585
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004586 /*
4587 * rb->aux_mmap_count will always drop before rb->mmap_count and
4588 * event->mmap_count, so it is ok to use event->mmap_mutex to
4589 * serialize with perf_mmap here.
4590 */
4591 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
4592 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
4593 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
4594 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
4595
4596 rb_free_aux(rb);
4597 mutex_unlock(&event->mmap_mutex);
4598 }
4599
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004600 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004601
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004602 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004603 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004604
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004605 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004606 mutex_unlock(&event->mmap_mutex);
4607
4608 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004609 if (atomic_read(&rb->mmap_count))
4610 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004611
4612 /*
4613 * No other mmap()s, detach from all other events that might redirect
4614 * into the now unreachable buffer. Somewhat complicated by the
4615 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4616 */
4617again:
4618 rcu_read_lock();
4619 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4620 if (!atomic_long_inc_not_zero(&event->refcount)) {
4621 /*
4622 * This event is en-route to free_event() which will
4623 * detach it and remove it from the list.
4624 */
4625 continue;
4626 }
4627 rcu_read_unlock();
4628
4629 mutex_lock(&event->mmap_mutex);
4630 /*
4631 * Check we didn't race with perf_event_set_output() which can
4632 * swizzle the rb from under us while we were waiting to
4633 * acquire mmap_mutex.
4634 *
4635 * If we find a different rb; ignore this event, a next
4636 * iteration will no longer find it on the list. We have to
4637 * still restart the iteration to make sure we're not now
4638 * iterating the wrong list.
4639 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004640 if (event->rb == rb)
4641 ring_buffer_attach(event, NULL);
4642
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004643 mutex_unlock(&event->mmap_mutex);
4644 put_event(event);
4645
4646 /*
4647 * Restart the iteration; either we're on the wrong list or
4648 * destroyed its integrity by doing a deletion.
4649 */
4650 goto again;
4651 }
4652 rcu_read_unlock();
4653
4654 /*
4655 * It could be there's still a few 0-ref events on the list; they'll
4656 * get cleaned up by free_event() -- they'll also still have their
4657 * ref on the rb and will free it whenever they are done with it.
4658 *
4659 * Aside from that, this buffer is 'fully' detached and unmapped,
4660 * undo the VM accounting.
4661 */
4662
4663 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4664 vma->vm_mm->pinned_vm -= mmap_locked;
4665 free_uid(mmap_user);
4666
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004667out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004668 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004669}
4670
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004671static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004672 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004673 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004674 .fault = perf_mmap_fault,
4675 .page_mkwrite = perf_mmap_fault,
4676};
4677
4678static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4679{
4680 struct perf_event *event = file->private_data;
4681 unsigned long user_locked, user_lock_limit;
4682 struct user_struct *user = current_user();
4683 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004684 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004685 unsigned long vma_size;
4686 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004687 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004688 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004689
Peter Zijlstrac7920612010-05-18 10:33:24 +02004690 /*
4691 * Don't allow mmap() of inherited per-task counters. This would
4692 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004693 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004694 */
4695 if (event->cpu == -1 && event->attr.inherit)
4696 return -EINVAL;
4697
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004698 if (!(vma->vm_flags & VM_SHARED))
4699 return -EINVAL;
4700
4701 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004702
4703 if (vma->vm_pgoff == 0) {
4704 nr_pages = (vma_size / PAGE_SIZE) - 1;
4705 } else {
4706 /*
4707 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
4708 * mapped, all subsequent mappings should have the same size
4709 * and offset. Must be above the normal perf buffer.
4710 */
4711 u64 aux_offset, aux_size;
4712
4713 if (!event->rb)
4714 return -EINVAL;
4715
4716 nr_pages = vma_size / PAGE_SIZE;
4717
4718 mutex_lock(&event->mmap_mutex);
4719 ret = -EINVAL;
4720
4721 rb = event->rb;
4722 if (!rb)
4723 goto aux_unlock;
4724
4725 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
4726 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
4727
4728 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
4729 goto aux_unlock;
4730
4731 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
4732 goto aux_unlock;
4733
4734 /* already mapped with a different offset */
4735 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
4736 goto aux_unlock;
4737
4738 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
4739 goto aux_unlock;
4740
4741 /* already mapped with a different size */
4742 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
4743 goto aux_unlock;
4744
4745 if (!is_power_of_2(nr_pages))
4746 goto aux_unlock;
4747
4748 if (!atomic_inc_not_zero(&rb->mmap_count))
4749 goto aux_unlock;
4750
4751 if (rb_has_aux(rb)) {
4752 atomic_inc(&rb->aux_mmap_count);
4753 ret = 0;
4754 goto unlock;
4755 }
4756
4757 atomic_set(&rb->aux_mmap_count, 1);
4758 user_extra = nr_pages;
4759
4760 goto accounting;
4761 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004762
4763 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004764 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004765 * can do bitmasks instead of modulo.
4766 */
Kan Liang2ed11312015-03-02 02:14:26 -05004767 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004768 return -EINVAL;
4769
4770 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4771 return -EINVAL;
4772
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004773 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004774again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004775 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004776 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004777 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004778 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004779 goto unlock;
4780 }
4781
4782 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4783 /*
4784 * Raced against perf_mmap_close() through
4785 * perf_event_set_output(). Try again, hope for better
4786 * luck.
4787 */
4788 mutex_unlock(&event->mmap_mutex);
4789 goto again;
4790 }
4791
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004792 goto unlock;
4793 }
4794
4795 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004796
4797accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004798 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4799
4800 /*
4801 * Increase the limit linearly with more CPUs:
4802 */
4803 user_lock_limit *= num_online_cpus();
4804
4805 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4806
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004807 if (user_locked > user_lock_limit)
4808 extra = user_locked - user_lock_limit;
4809
Jiri Slaby78d7d402010-03-05 13:42:54 -08004810 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004811 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004812 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004813
4814 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4815 !capable(CAP_IPC_LOCK)) {
4816 ret = -EPERM;
4817 goto unlock;
4818 }
4819
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004820 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004821
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004822 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004823 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004824
Frederic Weisbecker76369132011-05-19 19:55:04 +02004825 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004826 rb = rb_alloc(nr_pages,
4827 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4828 event->cpu, flags);
4829
4830 if (!rb) {
4831 ret = -ENOMEM;
4832 goto unlock;
4833 }
4834
4835 atomic_set(&rb->mmap_count, 1);
4836 rb->mmap_user = get_current_user();
4837 rb->mmap_locked = extra;
4838
4839 ring_buffer_attach(event, rb);
4840
4841 perf_event_init_userpage(event);
4842 perf_event_update_userpage(event);
4843 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02004844 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
4845 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004846 if (!ret)
4847 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004848 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004849
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004850unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004851 if (!ret) {
4852 atomic_long_add(user_extra, &user->locked_vm);
4853 vma->vm_mm->pinned_vm += extra;
4854
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004855 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004856 } else if (rb) {
4857 atomic_dec(&rb->mmap_count);
4858 }
4859aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004860 mutex_unlock(&event->mmap_mutex);
4861
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004862 /*
4863 * Since pinned accounting is per vm we cannot allow fork() to copy our
4864 * vma.
4865 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004866 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004867 vma->vm_ops = &perf_mmap_vmops;
4868
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004869 if (event->pmu->event_mapped)
4870 event->pmu->event_mapped(event);
4871
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004872 return ret;
4873}
4874
4875static int perf_fasync(int fd, struct file *filp, int on)
4876{
Al Viro496ad9a2013-01-23 17:07:38 -05004877 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004878 struct perf_event *event = filp->private_data;
4879 int retval;
4880
4881 mutex_lock(&inode->i_mutex);
4882 retval = fasync_helper(fd, filp, on, &event->fasync);
4883 mutex_unlock(&inode->i_mutex);
4884
4885 if (retval < 0)
4886 return retval;
4887
4888 return 0;
4889}
4890
4891static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004892 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004893 .release = perf_release,
4894 .read = perf_read,
4895 .poll = perf_poll,
4896 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01004897 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004898 .mmap = perf_mmap,
4899 .fasync = perf_fasync,
4900};
4901
4902/*
4903 * Perf event wakeup
4904 *
4905 * If there's data, ensure we set the poll() state and publish everything
4906 * to user-space before waking everybody up.
4907 */
4908
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02004909static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
4910{
4911 /* only the parent has fasync state */
4912 if (event->parent)
4913 event = event->parent;
4914 return &event->fasync;
4915}
4916
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004917void perf_event_wakeup(struct perf_event *event)
4918{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004919 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004920
4921 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02004922 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004923 event->pending_kill = 0;
4924 }
4925}
4926
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004927static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004928{
4929 struct perf_event *event = container_of(entry,
4930 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01004931 int rctx;
4932
4933 rctx = perf_swevent_get_recursion_context();
4934 /*
4935 * If we 'fail' here, that's OK, it means recursion is already disabled
4936 * and we won't recurse 'further'.
4937 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004938
4939 if (event->pending_disable) {
4940 event->pending_disable = 0;
4941 __perf_event_disable(event);
4942 }
4943
4944 if (event->pending_wakeup) {
4945 event->pending_wakeup = 0;
4946 perf_event_wakeup(event);
4947 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01004948
4949 if (rctx >= 0)
4950 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004951}
4952
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004953/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004954 * We assume there is only KVM supporting the callbacks.
4955 * Later on, we might change it to a list if there is
4956 * another virtualization implementation supporting the callbacks.
4957 */
4958struct perf_guest_info_callbacks *perf_guest_cbs;
4959
4960int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4961{
4962 perf_guest_cbs = cbs;
4963 return 0;
4964}
4965EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4966
4967int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4968{
4969 perf_guest_cbs = NULL;
4970 return 0;
4971}
4972EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4973
Jiri Olsa40189942012-08-07 15:20:37 +02004974static void
4975perf_output_sample_regs(struct perf_output_handle *handle,
4976 struct pt_regs *regs, u64 mask)
4977{
4978 int bit;
4979
4980 for_each_set_bit(bit, (const unsigned long *) &mask,
4981 sizeof(mask) * BITS_PER_BYTE) {
4982 u64 val;
4983
4984 val = perf_reg_value(regs, bit);
4985 perf_output_put(handle, val);
4986 }
4987}
4988
Stephane Eranian60e23642014-09-24 13:48:37 +02004989static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004990 struct pt_regs *regs,
4991 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02004992{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004993 if (user_mode(regs)) {
4994 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02004995 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004996 } else if (current->mm) {
4997 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02004998 } else {
4999 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5000 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005001 }
5002}
5003
Stephane Eranian60e23642014-09-24 13:48:37 +02005004static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5005 struct pt_regs *regs)
5006{
5007 regs_intr->regs = regs;
5008 regs_intr->abi = perf_reg_abi(current);
5009}
5010
5011
Jiri Olsac5ebced2012-08-07 15:20:40 +02005012/*
5013 * Get remaining task size from user stack pointer.
5014 *
5015 * It'd be better to take stack vma map and limit this more
5016 * precisly, but there's no way to get it safely under interrupt,
5017 * so using TASK_SIZE as limit.
5018 */
5019static u64 perf_ustack_task_size(struct pt_regs *regs)
5020{
5021 unsigned long addr = perf_user_stack_pointer(regs);
5022
5023 if (!addr || addr >= TASK_SIZE)
5024 return 0;
5025
5026 return TASK_SIZE - addr;
5027}
5028
5029static u16
5030perf_sample_ustack_size(u16 stack_size, u16 header_size,
5031 struct pt_regs *regs)
5032{
5033 u64 task_size;
5034
5035 /* No regs, no stack pointer, no dump. */
5036 if (!regs)
5037 return 0;
5038
5039 /*
5040 * Check if we fit in with the requested stack size into the:
5041 * - TASK_SIZE
5042 * If we don't, we limit the size to the TASK_SIZE.
5043 *
5044 * - remaining sample size
5045 * If we don't, we customize the stack size to
5046 * fit in to the remaining sample size.
5047 */
5048
5049 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5050 stack_size = min(stack_size, (u16) task_size);
5051
5052 /* Current header size plus static size and dynamic size. */
5053 header_size += 2 * sizeof(u64);
5054
5055 /* Do we fit in with the current stack dump size? */
5056 if ((u16) (header_size + stack_size) < header_size) {
5057 /*
5058 * If we overflow the maximum size for the sample,
5059 * we customize the stack dump size to fit in.
5060 */
5061 stack_size = USHRT_MAX - header_size - sizeof(u64);
5062 stack_size = round_up(stack_size, sizeof(u64));
5063 }
5064
5065 return stack_size;
5066}
5067
5068static void
5069perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5070 struct pt_regs *regs)
5071{
5072 /* Case of a kernel thread, nothing to dump */
5073 if (!regs) {
5074 u64 size = 0;
5075 perf_output_put(handle, size);
5076 } else {
5077 unsigned long sp;
5078 unsigned int rem;
5079 u64 dyn_size;
5080
5081 /*
5082 * We dump:
5083 * static size
5084 * - the size requested by user or the best one we can fit
5085 * in to the sample max size
5086 * data
5087 * - user stack dump data
5088 * dynamic size
5089 * - the actual dumped size
5090 */
5091
5092 /* Static size. */
5093 perf_output_put(handle, dump_size);
5094
5095 /* Data. */
5096 sp = perf_user_stack_pointer(regs);
5097 rem = __output_copy_user(handle, (void *) sp, dump_size);
5098 dyn_size = dump_size - rem;
5099
5100 perf_output_skip(handle, rem);
5101
5102 /* Dynamic size. */
5103 perf_output_put(handle, dyn_size);
5104 }
5105}
5106
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005107static void __perf_event_header__init_id(struct perf_event_header *header,
5108 struct perf_sample_data *data,
5109 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005110{
5111 u64 sample_type = event->attr.sample_type;
5112
5113 data->type = sample_type;
5114 header->size += event->id_header_size;
5115
5116 if (sample_type & PERF_SAMPLE_TID) {
5117 /* namespace issues */
5118 data->tid_entry.pid = perf_event_pid(event, current);
5119 data->tid_entry.tid = perf_event_tid(event, current);
5120 }
5121
5122 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005123 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005124
Adrian Hunterff3d5272013-08-27 11:23:07 +03005125 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005126 data->id = primary_event_id(event);
5127
5128 if (sample_type & PERF_SAMPLE_STREAM_ID)
5129 data->stream_id = event->id;
5130
5131 if (sample_type & PERF_SAMPLE_CPU) {
5132 data->cpu_entry.cpu = raw_smp_processor_id();
5133 data->cpu_entry.reserved = 0;
5134 }
5135}
5136
Frederic Weisbecker76369132011-05-19 19:55:04 +02005137void perf_event_header__init_id(struct perf_event_header *header,
5138 struct perf_sample_data *data,
5139 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005140{
5141 if (event->attr.sample_id_all)
5142 __perf_event_header__init_id(header, data, event);
5143}
5144
5145static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5146 struct perf_sample_data *data)
5147{
5148 u64 sample_type = data->type;
5149
5150 if (sample_type & PERF_SAMPLE_TID)
5151 perf_output_put(handle, data->tid_entry);
5152
5153 if (sample_type & PERF_SAMPLE_TIME)
5154 perf_output_put(handle, data->time);
5155
5156 if (sample_type & PERF_SAMPLE_ID)
5157 perf_output_put(handle, data->id);
5158
5159 if (sample_type & PERF_SAMPLE_STREAM_ID)
5160 perf_output_put(handle, data->stream_id);
5161
5162 if (sample_type & PERF_SAMPLE_CPU)
5163 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005164
5165 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5166 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005167}
5168
Frederic Weisbecker76369132011-05-19 19:55:04 +02005169void perf_event__output_id_sample(struct perf_event *event,
5170 struct perf_output_handle *handle,
5171 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005172{
5173 if (event->attr.sample_id_all)
5174 __perf_event__output_id_sample(handle, sample);
5175}
5176
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005177static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005178 struct perf_event *event,
5179 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005180{
5181 u64 read_format = event->attr.read_format;
5182 u64 values[4];
5183 int n = 0;
5184
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005185 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005186 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005187 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005188 atomic64_read(&event->child_total_time_enabled);
5189 }
5190 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005191 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005192 atomic64_read(&event->child_total_time_running);
5193 }
5194 if (read_format & PERF_FORMAT_ID)
5195 values[n++] = primary_event_id(event);
5196
Frederic Weisbecker76369132011-05-19 19:55:04 +02005197 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005198}
5199
5200/*
5201 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
5202 */
5203static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005204 struct perf_event *event,
5205 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005206{
5207 struct perf_event *leader = event->group_leader, *sub;
5208 u64 read_format = event->attr.read_format;
5209 u64 values[5];
5210 int n = 0;
5211
5212 values[n++] = 1 + leader->nr_siblings;
5213
5214 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005215 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005216
5217 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005218 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005219
5220 if (leader != event)
5221 leader->pmu->read(leader);
5222
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005223 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005224 if (read_format & PERF_FORMAT_ID)
5225 values[n++] = primary_event_id(leader);
5226
Frederic Weisbecker76369132011-05-19 19:55:04 +02005227 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005228
5229 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5230 n = 0;
5231
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005232 if ((sub != event) &&
5233 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005234 sub->pmu->read(sub);
5235
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005236 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005237 if (read_format & PERF_FORMAT_ID)
5238 values[n++] = primary_event_id(sub);
5239
Frederic Weisbecker76369132011-05-19 19:55:04 +02005240 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005241 }
5242}
5243
Stephane Eranianeed01522010-10-26 16:08:01 +02005244#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5245 PERF_FORMAT_TOTAL_TIME_RUNNING)
5246
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005247static void perf_output_read(struct perf_output_handle *handle,
5248 struct perf_event *event)
5249{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005250 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005251 u64 read_format = event->attr.read_format;
5252
5253 /*
5254 * compute total_time_enabled, total_time_running
5255 * based on snapshot values taken when the event
5256 * was last scheduled in.
5257 *
5258 * we cannot simply called update_context_time()
5259 * because of locking issue as we are called in
5260 * NMI context
5261 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005262 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005263 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005264
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005265 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005266 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005267 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005268 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005269}
5270
5271void perf_output_sample(struct perf_output_handle *handle,
5272 struct perf_event_header *header,
5273 struct perf_sample_data *data,
5274 struct perf_event *event)
5275{
5276 u64 sample_type = data->type;
5277
5278 perf_output_put(handle, *header);
5279
Adrian Hunterff3d5272013-08-27 11:23:07 +03005280 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5281 perf_output_put(handle, data->id);
5282
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005283 if (sample_type & PERF_SAMPLE_IP)
5284 perf_output_put(handle, data->ip);
5285
5286 if (sample_type & PERF_SAMPLE_TID)
5287 perf_output_put(handle, data->tid_entry);
5288
5289 if (sample_type & PERF_SAMPLE_TIME)
5290 perf_output_put(handle, data->time);
5291
5292 if (sample_type & PERF_SAMPLE_ADDR)
5293 perf_output_put(handle, data->addr);
5294
5295 if (sample_type & PERF_SAMPLE_ID)
5296 perf_output_put(handle, data->id);
5297
5298 if (sample_type & PERF_SAMPLE_STREAM_ID)
5299 perf_output_put(handle, data->stream_id);
5300
5301 if (sample_type & PERF_SAMPLE_CPU)
5302 perf_output_put(handle, data->cpu_entry);
5303
5304 if (sample_type & PERF_SAMPLE_PERIOD)
5305 perf_output_put(handle, data->period);
5306
5307 if (sample_type & PERF_SAMPLE_READ)
5308 perf_output_read(handle, event);
5309
5310 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5311 if (data->callchain) {
5312 int size = 1;
5313
5314 if (data->callchain)
5315 size += data->callchain->nr;
5316
5317 size *= sizeof(u64);
5318
Frederic Weisbecker76369132011-05-19 19:55:04 +02005319 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005320 } else {
5321 u64 nr = 0;
5322 perf_output_put(handle, nr);
5323 }
5324 }
5325
5326 if (sample_type & PERF_SAMPLE_RAW) {
5327 if (data->raw) {
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005328 u32 raw_size = data->raw->size;
5329 u32 real_size = round_up(raw_size + sizeof(u32),
5330 sizeof(u64)) - sizeof(u32);
5331 u64 zero = 0;
5332
5333 perf_output_put(handle, real_size);
5334 __output_copy(handle, data->raw->data, raw_size);
5335 if (real_size - raw_size)
5336 __output_copy(handle, &zero, real_size - raw_size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005337 } else {
5338 struct {
5339 u32 size;
5340 u32 data;
5341 } raw = {
5342 .size = sizeof(u32),
5343 .data = 0,
5344 };
5345 perf_output_put(handle, raw);
5346 }
5347 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005348
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005349 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5350 if (data->br_stack) {
5351 size_t size;
5352
5353 size = data->br_stack->nr
5354 * sizeof(struct perf_branch_entry);
5355
5356 perf_output_put(handle, data->br_stack->nr);
5357 perf_output_copy(handle, data->br_stack->entries, size);
5358 } else {
5359 /*
5360 * we always store at least the value of nr
5361 */
5362 u64 nr = 0;
5363 perf_output_put(handle, nr);
5364 }
5365 }
Jiri Olsa40189942012-08-07 15:20:37 +02005366
5367 if (sample_type & PERF_SAMPLE_REGS_USER) {
5368 u64 abi = data->regs_user.abi;
5369
5370 /*
5371 * If there are no regs to dump, notice it through
5372 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5373 */
5374 perf_output_put(handle, abi);
5375
5376 if (abi) {
5377 u64 mask = event->attr.sample_regs_user;
5378 perf_output_sample_regs(handle,
5379 data->regs_user.regs,
5380 mask);
5381 }
5382 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005383
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005384 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005385 perf_output_sample_ustack(handle,
5386 data->stack_user_size,
5387 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005388 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005389
5390 if (sample_type & PERF_SAMPLE_WEIGHT)
5391 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005392
5393 if (sample_type & PERF_SAMPLE_DATA_SRC)
5394 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005395
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005396 if (sample_type & PERF_SAMPLE_TRANSACTION)
5397 perf_output_put(handle, data->txn);
5398
Stephane Eranian60e23642014-09-24 13:48:37 +02005399 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5400 u64 abi = data->regs_intr.abi;
5401 /*
5402 * If there are no regs to dump, notice it through
5403 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5404 */
5405 perf_output_put(handle, abi);
5406
5407 if (abi) {
5408 u64 mask = event->attr.sample_regs_intr;
5409
5410 perf_output_sample_regs(handle,
5411 data->regs_intr.regs,
5412 mask);
5413 }
5414 }
5415
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005416 if (!event->attr.watermark) {
5417 int wakeup_events = event->attr.wakeup_events;
5418
5419 if (wakeup_events) {
5420 struct ring_buffer *rb = handle->rb;
5421 int events = local_inc_return(&rb->events);
5422
5423 if (events >= wakeup_events) {
5424 local_sub(wakeup_events, &rb->events);
5425 local_inc(&rb->wakeup);
5426 }
5427 }
5428 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005429}
5430
5431void perf_prepare_sample(struct perf_event_header *header,
5432 struct perf_sample_data *data,
5433 struct perf_event *event,
5434 struct pt_regs *regs)
5435{
5436 u64 sample_type = event->attr.sample_type;
5437
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005438 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005439 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005440
5441 header->misc = 0;
5442 header->misc |= perf_misc_flags(regs);
5443
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005444 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005445
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005446 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005447 data->ip = perf_instruction_pointer(regs);
5448
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005449 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5450 int size = 1;
5451
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005452 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005453
5454 if (data->callchain)
5455 size += data->callchain->nr;
5456
5457 header->size += size * sizeof(u64);
5458 }
5459
5460 if (sample_type & PERF_SAMPLE_RAW) {
5461 int size = sizeof(u32);
5462
5463 if (data->raw)
5464 size += data->raw->size;
5465 else
5466 size += sizeof(u32);
5467
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005468 header->size += round_up(size, sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005469 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005470
5471 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5472 int size = sizeof(u64); /* nr */
5473 if (data->br_stack) {
5474 size += data->br_stack->nr
5475 * sizeof(struct perf_branch_entry);
5476 }
5477 header->size += size;
5478 }
Jiri Olsa40189942012-08-07 15:20:37 +02005479
Peter Zijlstra25657112014-09-24 13:48:42 +02005480 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005481 perf_sample_regs_user(&data->regs_user, regs,
5482 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005483
Jiri Olsa40189942012-08-07 15:20:37 +02005484 if (sample_type & PERF_SAMPLE_REGS_USER) {
5485 /* regs dump ABI info */
5486 int size = sizeof(u64);
5487
Jiri Olsa40189942012-08-07 15:20:37 +02005488 if (data->regs_user.regs) {
5489 u64 mask = event->attr.sample_regs_user;
5490 size += hweight64(mask) * sizeof(u64);
5491 }
5492
5493 header->size += size;
5494 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005495
5496 if (sample_type & PERF_SAMPLE_STACK_USER) {
5497 /*
5498 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5499 * processed as the last one or have additional check added
5500 * in case new sample type is added, because we could eat
5501 * up the rest of the sample size.
5502 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005503 u16 stack_size = event->attr.sample_stack_user;
5504 u16 size = sizeof(u64);
5505
Jiri Olsac5ebced2012-08-07 15:20:40 +02005506 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005507 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005508
5509 /*
5510 * If there is something to dump, add space for the dump
5511 * itself and for the field that tells the dynamic size,
5512 * which is how many have been actually dumped.
5513 */
5514 if (stack_size)
5515 size += sizeof(u64) + stack_size;
5516
5517 data->stack_user_size = stack_size;
5518 header->size += size;
5519 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005520
5521 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5522 /* regs dump ABI info */
5523 int size = sizeof(u64);
5524
5525 perf_sample_regs_intr(&data->regs_intr, regs);
5526
5527 if (data->regs_intr.regs) {
5528 u64 mask = event->attr.sample_regs_intr;
5529
5530 size += hweight64(mask) * sizeof(u64);
5531 }
5532
5533 header->size += size;
5534 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005535}
5536
Yan, Zheng21509082015-05-06 15:33:49 -04005537void perf_event_output(struct perf_event *event,
5538 struct perf_sample_data *data,
5539 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005540{
5541 struct perf_output_handle handle;
5542 struct perf_event_header header;
5543
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005544 /* protect the callchain buffers */
5545 rcu_read_lock();
5546
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005547 perf_prepare_sample(&header, data, event, regs);
5548
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005549 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005550 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005551
5552 perf_output_sample(&handle, &header, data, event);
5553
5554 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005555
5556exit:
5557 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005558}
5559
5560/*
5561 * read event_id
5562 */
5563
5564struct perf_read_event {
5565 struct perf_event_header header;
5566
5567 u32 pid;
5568 u32 tid;
5569};
5570
5571static void
5572perf_event_read_event(struct perf_event *event,
5573 struct task_struct *task)
5574{
5575 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005576 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005577 struct perf_read_event read_event = {
5578 .header = {
5579 .type = PERF_RECORD_READ,
5580 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005581 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005582 },
5583 .pid = perf_event_pid(event, task),
5584 .tid = perf_event_tid(event, task),
5585 };
5586 int ret;
5587
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005588 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005589 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005590 if (ret)
5591 return;
5592
5593 perf_output_put(&handle, read_event);
5594 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005595 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005596
5597 perf_output_end(&handle);
5598}
5599
Jiri Olsa52d857a2013-05-06 18:27:18 +02005600typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5601
5602static void
5603perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005604 perf_event_aux_output_cb output,
5605 void *data)
5606{
5607 struct perf_event *event;
5608
5609 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5610 if (event->state < PERF_EVENT_STATE_INACTIVE)
5611 continue;
5612 if (!event_filter_match(event))
5613 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02005614 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005615 }
5616}
5617
5618static void
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005619perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data,
5620 struct perf_event_context *task_ctx)
5621{
5622 rcu_read_lock();
5623 preempt_disable();
5624 perf_event_aux_ctx(task_ctx, output, data);
5625 preempt_enable();
5626 rcu_read_unlock();
5627}
5628
5629static void
Jiri Olsa67516842013-07-09 18:56:31 +02005630perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005631 struct perf_event_context *task_ctx)
5632{
5633 struct perf_cpu_context *cpuctx;
5634 struct perf_event_context *ctx;
5635 struct pmu *pmu;
5636 int ctxn;
5637
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005638 /*
5639 * If we have task_ctx != NULL we only notify
5640 * the task context itself. The task_ctx is set
5641 * only for EXIT events before releasing task
5642 * context.
5643 */
5644 if (task_ctx) {
5645 perf_event_aux_task_ctx(output, data, task_ctx);
5646 return;
5647 }
5648
Jiri Olsa52d857a2013-05-06 18:27:18 +02005649 rcu_read_lock();
5650 list_for_each_entry_rcu(pmu, &pmus, entry) {
5651 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5652 if (cpuctx->unique_pmu != pmu)
5653 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02005654 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005655 ctxn = pmu->task_ctx_nr;
5656 if (ctxn < 0)
5657 goto next;
5658 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5659 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02005660 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005661next:
5662 put_cpu_ptr(pmu->pmu_cpu_context);
5663 }
Jiri Olsa52d857a2013-05-06 18:27:18 +02005664 rcu_read_unlock();
5665}
5666
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005667/*
5668 * task tracking -- fork/exit
5669 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02005670 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005671 */
5672
5673struct perf_task_event {
5674 struct task_struct *task;
5675 struct perf_event_context *task_ctx;
5676
5677 struct {
5678 struct perf_event_header header;
5679
5680 u32 pid;
5681 u32 ppid;
5682 u32 tid;
5683 u32 ptid;
5684 u64 time;
5685 } event_id;
5686};
5687
Jiri Olsa67516842013-07-09 18:56:31 +02005688static int perf_event_task_match(struct perf_event *event)
5689{
Stephane Eranian13d7a242013-08-21 12:10:24 +02005690 return event->attr.comm || event->attr.mmap ||
5691 event->attr.mmap2 || event->attr.mmap_data ||
5692 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02005693}
5694
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005695static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005696 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005697{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005698 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005699 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005700 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005701 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005702 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01005703
Jiri Olsa67516842013-07-09 18:56:31 +02005704 if (!perf_event_task_match(event))
5705 return;
5706
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005707 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005708
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005709 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005710 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02005711 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005712 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005713
5714 task_event->event_id.pid = perf_event_pid(event, task);
5715 task_event->event_id.ppid = perf_event_pid(event, current);
5716
5717 task_event->event_id.tid = perf_event_tid(event, task);
5718 task_event->event_id.ptid = perf_event_tid(event, current);
5719
Peter Zijlstra34f43922015-02-20 14:05:38 +01005720 task_event->event_id.time = perf_event_clock(event);
5721
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005722 perf_output_put(&handle, task_event->event_id);
5723
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005724 perf_event__output_id_sample(event, &handle, &sample);
5725
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005726 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005727out:
5728 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005729}
5730
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005731static void perf_event_task(struct task_struct *task,
5732 struct perf_event_context *task_ctx,
5733 int new)
5734{
5735 struct perf_task_event task_event;
5736
5737 if (!atomic_read(&nr_comm_events) &&
5738 !atomic_read(&nr_mmap_events) &&
5739 !atomic_read(&nr_task_events))
5740 return;
5741
5742 task_event = (struct perf_task_event){
5743 .task = task,
5744 .task_ctx = task_ctx,
5745 .event_id = {
5746 .header = {
5747 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5748 .misc = 0,
5749 .size = sizeof(task_event.event_id),
5750 },
5751 /* .pid */
5752 /* .ppid */
5753 /* .tid */
5754 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01005755 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005756 },
5757 };
5758
Jiri Olsa67516842013-07-09 18:56:31 +02005759 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005760 &task_event,
5761 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005762}
5763
5764void perf_event_fork(struct task_struct *task)
5765{
5766 perf_event_task(task, NULL, 1);
5767}
5768
5769/*
5770 * comm tracking
5771 */
5772
5773struct perf_comm_event {
5774 struct task_struct *task;
5775 char *comm;
5776 int comm_size;
5777
5778 struct {
5779 struct perf_event_header header;
5780
5781 u32 pid;
5782 u32 tid;
5783 } event_id;
5784};
5785
Jiri Olsa67516842013-07-09 18:56:31 +02005786static int perf_event_comm_match(struct perf_event *event)
5787{
5788 return event->attr.comm;
5789}
5790
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005791static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005792 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005793{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005794 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005795 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005796 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005797 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005798 int ret;
5799
Jiri Olsa67516842013-07-09 18:56:31 +02005800 if (!perf_event_comm_match(event))
5801 return;
5802
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005803 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5804 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005805 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005806
5807 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005808 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005809
5810 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5811 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5812
5813 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005814 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005815 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005816
5817 perf_event__output_id_sample(event, &handle, &sample);
5818
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005819 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005820out:
5821 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005822}
5823
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005824static void perf_event_comm_event(struct perf_comm_event *comm_event)
5825{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005826 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005827 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005828
5829 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005830 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005831 size = ALIGN(strlen(comm)+1, sizeof(u64));
5832
5833 comm_event->comm = comm;
5834 comm_event->comm_size = size;
5835
5836 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005837
Jiri Olsa67516842013-07-09 18:56:31 +02005838 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005839 comm_event,
5840 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005841}
5842
Adrian Hunter82b89772014-05-28 11:45:04 +03005843void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005844{
5845 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005846
5847 if (!atomic_read(&nr_comm_events))
5848 return;
5849
5850 comm_event = (struct perf_comm_event){
5851 .task = task,
5852 /* .comm */
5853 /* .comm_size */
5854 .event_id = {
5855 .header = {
5856 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005857 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005858 /* .size */
5859 },
5860 /* .pid */
5861 /* .tid */
5862 },
5863 };
5864
5865 perf_event_comm_event(&comm_event);
5866}
5867
5868/*
5869 * mmap tracking
5870 */
5871
5872struct perf_mmap_event {
5873 struct vm_area_struct *vma;
5874
5875 const char *file_name;
5876 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005877 int maj, min;
5878 u64 ino;
5879 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005880 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005881
5882 struct {
5883 struct perf_event_header header;
5884
5885 u32 pid;
5886 u32 tid;
5887 u64 start;
5888 u64 len;
5889 u64 pgoff;
5890 } event_id;
5891};
5892
Jiri Olsa67516842013-07-09 18:56:31 +02005893static int perf_event_mmap_match(struct perf_event *event,
5894 void *data)
5895{
5896 struct perf_mmap_event *mmap_event = data;
5897 struct vm_area_struct *vma = mmap_event->vma;
5898 int executable = vma->vm_flags & VM_EXEC;
5899
5900 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005901 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005902}
5903
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005904static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005905 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005906{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005907 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005908 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005909 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005910 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005911 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005912
Jiri Olsa67516842013-07-09 18:56:31 +02005913 if (!perf_event_mmap_match(event, data))
5914 return;
5915
Stephane Eranian13d7a242013-08-21 12:10:24 +02005916 if (event->attr.mmap2) {
5917 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5918 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5919 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5920 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005921 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005922 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
5923 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005924 }
5925
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005926 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5927 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005928 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005929 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005930 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005931
5932 mmap_event->event_id.pid = perf_event_pid(event, current);
5933 mmap_event->event_id.tid = perf_event_tid(event, current);
5934
5935 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005936
5937 if (event->attr.mmap2) {
5938 perf_output_put(&handle, mmap_event->maj);
5939 perf_output_put(&handle, mmap_event->min);
5940 perf_output_put(&handle, mmap_event->ino);
5941 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005942 perf_output_put(&handle, mmap_event->prot);
5943 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005944 }
5945
Frederic Weisbecker76369132011-05-19 19:55:04 +02005946 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005947 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005948
5949 perf_event__output_id_sample(event, &handle, &sample);
5950
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005951 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005952out:
5953 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005954}
5955
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005956static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5957{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005958 struct vm_area_struct *vma = mmap_event->vma;
5959 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005960 int maj = 0, min = 0;
5961 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005962 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005963 unsigned int size;
5964 char tmp[16];
5965 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005966 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005967
5968 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005969 struct inode *inode;
5970 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005971
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005972 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005973 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005974 name = "//enomem";
5975 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005976 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005977 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005978 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005979 * need to add enough zero bytes after the string to handle
5980 * the 64bit alignment we do later.
5981 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02005982 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005983 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005984 name = "//toolong";
5985 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005986 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005987 inode = file_inode(vma->vm_file);
5988 dev = inode->i_sb->s_dev;
5989 ino = inode->i_ino;
5990 gen = inode->i_generation;
5991 maj = MAJOR(dev);
5992 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005993
5994 if (vma->vm_flags & VM_READ)
5995 prot |= PROT_READ;
5996 if (vma->vm_flags & VM_WRITE)
5997 prot |= PROT_WRITE;
5998 if (vma->vm_flags & VM_EXEC)
5999 prot |= PROT_EXEC;
6000
6001 if (vma->vm_flags & VM_MAYSHARE)
6002 flags = MAP_SHARED;
6003 else
6004 flags = MAP_PRIVATE;
6005
6006 if (vma->vm_flags & VM_DENYWRITE)
6007 flags |= MAP_DENYWRITE;
6008 if (vma->vm_flags & VM_MAYEXEC)
6009 flags |= MAP_EXECUTABLE;
6010 if (vma->vm_flags & VM_LOCKED)
6011 flags |= MAP_LOCKED;
6012 if (vma->vm_flags & VM_HUGETLB)
6013 flags |= MAP_HUGETLB;
6014
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006015 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006016 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006017 if (vma->vm_ops && vma->vm_ops->name) {
6018 name = (char *) vma->vm_ops->name(vma);
6019 if (name)
6020 goto cpy_name;
6021 }
6022
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006023 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006024 if (name)
6025 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006026
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006027 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006028 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006029 name = "[heap]";
6030 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006031 }
6032 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006033 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006034 name = "[stack]";
6035 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006036 }
6037
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006038 name = "//anon";
6039 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006040 }
6041
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006042cpy_name:
6043 strlcpy(tmp, name, sizeof(tmp));
6044 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006045got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006046 /*
6047 * Since our buffer works in 8 byte units we need to align our string
6048 * size to a multiple of 8. However, we must guarantee the tail end is
6049 * zero'd out to avoid leaking random bits to userspace.
6050 */
6051 size = strlen(name)+1;
6052 while (!IS_ALIGNED(size, sizeof(u64)))
6053 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006054
6055 mmap_event->file_name = name;
6056 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006057 mmap_event->maj = maj;
6058 mmap_event->min = min;
6059 mmap_event->ino = ino;
6060 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006061 mmap_event->prot = prot;
6062 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006063
Stephane Eranian2fe85422013-01-24 16:10:39 +01006064 if (!(vma->vm_flags & VM_EXEC))
6065 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6066
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006067 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6068
Jiri Olsa67516842013-07-09 18:56:31 +02006069 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006070 mmap_event,
6071 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006072
6073 kfree(buf);
6074}
6075
Eric B Munson3af9e852010-05-18 15:30:49 +01006076void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006077{
6078 struct perf_mmap_event mmap_event;
6079
6080 if (!atomic_read(&nr_mmap_events))
6081 return;
6082
6083 mmap_event = (struct perf_mmap_event){
6084 .vma = vma,
6085 /* .file_name */
6086 /* .file_size */
6087 .event_id = {
6088 .header = {
6089 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006090 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006091 /* .size */
6092 },
6093 /* .pid */
6094 /* .tid */
6095 .start = vma->vm_start,
6096 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006097 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006098 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006099 /* .maj (attr_mmap2 only) */
6100 /* .min (attr_mmap2 only) */
6101 /* .ino (attr_mmap2 only) */
6102 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006103 /* .prot (attr_mmap2 only) */
6104 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006105 };
6106
6107 perf_event_mmap_event(&mmap_event);
6108}
6109
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006110void perf_event_aux_event(struct perf_event *event, unsigned long head,
6111 unsigned long size, u64 flags)
6112{
6113 struct perf_output_handle handle;
6114 struct perf_sample_data sample;
6115 struct perf_aux_event {
6116 struct perf_event_header header;
6117 u64 offset;
6118 u64 size;
6119 u64 flags;
6120 } rec = {
6121 .header = {
6122 .type = PERF_RECORD_AUX,
6123 .misc = 0,
6124 .size = sizeof(rec),
6125 },
6126 .offset = head,
6127 .size = size,
6128 .flags = flags,
6129 };
6130 int ret;
6131
6132 perf_event_header__init_id(&rec.header, &sample, event);
6133 ret = perf_output_begin(&handle, event, rec.header.size);
6134
6135 if (ret)
6136 return;
6137
6138 perf_output_put(&handle, rec);
6139 perf_event__output_id_sample(event, &handle, &sample);
6140
6141 perf_output_end(&handle);
6142}
6143
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006144/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006145 * Lost/dropped samples logging
6146 */
6147void perf_log_lost_samples(struct perf_event *event, u64 lost)
6148{
6149 struct perf_output_handle handle;
6150 struct perf_sample_data sample;
6151 int ret;
6152
6153 struct {
6154 struct perf_event_header header;
6155 u64 lost;
6156 } lost_samples_event = {
6157 .header = {
6158 .type = PERF_RECORD_LOST_SAMPLES,
6159 .misc = 0,
6160 .size = sizeof(lost_samples_event),
6161 },
6162 .lost = lost,
6163 };
6164
6165 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6166
6167 ret = perf_output_begin(&handle, event,
6168 lost_samples_event.header.size);
6169 if (ret)
6170 return;
6171
6172 perf_output_put(&handle, lost_samples_event);
6173 perf_event__output_id_sample(event, &handle, &sample);
6174 perf_output_end(&handle);
6175}
6176
6177/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006178 * context_switch tracking
6179 */
6180
6181struct perf_switch_event {
6182 struct task_struct *task;
6183 struct task_struct *next_prev;
6184
6185 struct {
6186 struct perf_event_header header;
6187 u32 next_prev_pid;
6188 u32 next_prev_tid;
6189 } event_id;
6190};
6191
6192static int perf_event_switch_match(struct perf_event *event)
6193{
6194 return event->attr.context_switch;
6195}
6196
6197static void perf_event_switch_output(struct perf_event *event, void *data)
6198{
6199 struct perf_switch_event *se = data;
6200 struct perf_output_handle handle;
6201 struct perf_sample_data sample;
6202 int ret;
6203
6204 if (!perf_event_switch_match(event))
6205 return;
6206
6207 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6208 if (event->ctx->task) {
6209 se->event_id.header.type = PERF_RECORD_SWITCH;
6210 se->event_id.header.size = sizeof(se->event_id.header);
6211 } else {
6212 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6213 se->event_id.header.size = sizeof(se->event_id);
6214 se->event_id.next_prev_pid =
6215 perf_event_pid(event, se->next_prev);
6216 se->event_id.next_prev_tid =
6217 perf_event_tid(event, se->next_prev);
6218 }
6219
6220 perf_event_header__init_id(&se->event_id.header, &sample, event);
6221
6222 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6223 if (ret)
6224 return;
6225
6226 if (event->ctx->task)
6227 perf_output_put(&handle, se->event_id.header);
6228 else
6229 perf_output_put(&handle, se->event_id);
6230
6231 perf_event__output_id_sample(event, &handle, &sample);
6232
6233 perf_output_end(&handle);
6234}
6235
6236static void perf_event_switch(struct task_struct *task,
6237 struct task_struct *next_prev, bool sched_in)
6238{
6239 struct perf_switch_event switch_event;
6240
6241 /* N.B. caller checks nr_switch_events != 0 */
6242
6243 switch_event = (struct perf_switch_event){
6244 .task = task,
6245 .next_prev = next_prev,
6246 .event_id = {
6247 .header = {
6248 /* .type */
6249 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
6250 /* .size */
6251 },
6252 /* .next_prev_pid */
6253 /* .next_prev_tid */
6254 },
6255 };
6256
6257 perf_event_aux(perf_event_switch_output,
6258 &switch_event,
6259 NULL);
6260}
6261
6262/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006263 * IRQ throttle logging
6264 */
6265
6266static void perf_log_throttle(struct perf_event *event, int enable)
6267{
6268 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006269 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006270 int ret;
6271
6272 struct {
6273 struct perf_event_header header;
6274 u64 time;
6275 u64 id;
6276 u64 stream_id;
6277 } throttle_event = {
6278 .header = {
6279 .type = PERF_RECORD_THROTTLE,
6280 .misc = 0,
6281 .size = sizeof(throttle_event),
6282 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01006283 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006284 .id = primary_event_id(event),
6285 .stream_id = event->id,
6286 };
6287
6288 if (enable)
6289 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
6290
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006291 perf_event_header__init_id(&throttle_event.header, &sample, event);
6292
6293 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006294 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006295 if (ret)
6296 return;
6297
6298 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006299 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006300 perf_output_end(&handle);
6301}
6302
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006303static void perf_log_itrace_start(struct perf_event *event)
6304{
6305 struct perf_output_handle handle;
6306 struct perf_sample_data sample;
6307 struct perf_aux_event {
6308 struct perf_event_header header;
6309 u32 pid;
6310 u32 tid;
6311 } rec;
6312 int ret;
6313
6314 if (event->parent)
6315 event = event->parent;
6316
6317 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
6318 event->hw.itrace_started)
6319 return;
6320
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006321 rec.header.type = PERF_RECORD_ITRACE_START;
6322 rec.header.misc = 0;
6323 rec.header.size = sizeof(rec);
6324 rec.pid = perf_event_pid(event, current);
6325 rec.tid = perf_event_tid(event, current);
6326
6327 perf_event_header__init_id(&rec.header, &sample, event);
6328 ret = perf_output_begin(&handle, event, rec.header.size);
6329
6330 if (ret)
6331 return;
6332
6333 perf_output_put(&handle, rec);
6334 perf_event__output_id_sample(event, &handle, &sample);
6335
6336 perf_output_end(&handle);
6337}
6338
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006339/*
6340 * Generic event overflow handling, sampling.
6341 */
6342
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006343static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006344 int throttle, struct perf_sample_data *data,
6345 struct pt_regs *regs)
6346{
6347 int events = atomic_read(&event->event_limit);
6348 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006349 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006350 int ret = 0;
6351
Peter Zijlstra96398822010-11-24 18:55:29 +01006352 /*
6353 * Non-sampling counters might still use the PMI to fold short
6354 * hardware counters, ignore those.
6355 */
6356 if (unlikely(!is_sampling_event(event)))
6357 return 0;
6358
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006359 seq = __this_cpu_read(perf_throttled_seq);
6360 if (seq != hwc->interrupts_seq) {
6361 hwc->interrupts_seq = seq;
6362 hwc->interrupts = 1;
6363 } else {
6364 hwc->interrupts++;
6365 if (unlikely(throttle
6366 && hwc->interrupts >= max_samples_per_tick)) {
6367 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01006368 hwc->interrupts = MAX_INTERRUPTS;
6369 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02006370 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006371 ret = 1;
6372 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006373 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006374
6375 if (event->attr.freq) {
6376 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01006377 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006378
Peter Zijlstraabd50712010-01-26 18:50:16 +01006379 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006380
Peter Zijlstraabd50712010-01-26 18:50:16 +01006381 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01006382 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006383 }
6384
6385 /*
6386 * XXX event_limit might not quite work as expected on inherited
6387 * events
6388 */
6389
6390 event->pending_kill = POLL_IN;
6391 if (events && atomic_dec_and_test(&event->event_limit)) {
6392 ret = 1;
6393 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006394 event->pending_disable = 1;
6395 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006396 }
6397
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006398 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006399 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006400 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006401 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006402
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006403 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006404 event->pending_wakeup = 1;
6405 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02006406 }
6407
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006408 return ret;
6409}
6410
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006411int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006412 struct perf_sample_data *data,
6413 struct pt_regs *regs)
6414{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006415 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006416}
6417
6418/*
6419 * Generic software event infrastructure
6420 */
6421
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006422struct swevent_htable {
6423 struct swevent_hlist *swevent_hlist;
6424 struct mutex hlist_mutex;
6425 int hlist_refcount;
6426
6427 /* Recursion avoidance in each contexts */
6428 int recursion[PERF_NR_CONTEXTS];
6429};
6430
6431static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
6432
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006433/*
6434 * We directly increment event->count and keep a second value in
6435 * event->hw.period_left to count intervals. This period event
6436 * is kept in the range [-sample_period, 0] so that we can use the
6437 * sign as trigger.
6438 */
6439
Jiri Olsaab573842013-05-01 17:25:44 +02006440u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006441{
6442 struct hw_perf_event *hwc = &event->hw;
6443 u64 period = hwc->last_period;
6444 u64 nr, offset;
6445 s64 old, val;
6446
6447 hwc->last_period = hwc->sample_period;
6448
6449again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02006450 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006451 if (val < 0)
6452 return 0;
6453
6454 nr = div64_u64(period + val, period);
6455 offset = nr * period;
6456 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02006457 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006458 goto again;
6459
6460 return nr;
6461}
6462
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006463static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006464 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006465 struct pt_regs *regs)
6466{
6467 struct hw_perf_event *hwc = &event->hw;
6468 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006469
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006470 if (!overflow)
6471 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006472
6473 if (hwc->interrupts == MAX_INTERRUPTS)
6474 return;
6475
6476 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006477 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006478 data, regs)) {
6479 /*
6480 * We inhibit the overflow from happening when
6481 * hwc->interrupts == MAX_INTERRUPTS.
6482 */
6483 break;
6484 }
6485 throttle = 1;
6486 }
6487}
6488
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006489static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006490 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006491 struct pt_regs *regs)
6492{
6493 struct hw_perf_event *hwc = &event->hw;
6494
Peter Zijlstrae7850592010-05-21 14:43:08 +02006495 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006496
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006497 if (!regs)
6498 return;
6499
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006500 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006501 return;
6502
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03006503 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
6504 data->period = nr;
6505 return perf_swevent_overflow(event, 1, data, regs);
6506 } else
6507 data->period = event->hw.last_period;
6508
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006509 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006510 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006511
Peter Zijlstrae7850592010-05-21 14:43:08 +02006512 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006513 return;
6514
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006515 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006516}
6517
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006518static int perf_exclude_event(struct perf_event *event,
6519 struct pt_regs *regs)
6520{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006521 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01006522 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006523
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006524 if (regs) {
6525 if (event->attr.exclude_user && user_mode(regs))
6526 return 1;
6527
6528 if (event->attr.exclude_kernel && !user_mode(regs))
6529 return 1;
6530 }
6531
6532 return 0;
6533}
6534
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006535static int perf_swevent_match(struct perf_event *event,
6536 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08006537 u32 event_id,
6538 struct perf_sample_data *data,
6539 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006540{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006541 if (event->attr.type != type)
6542 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006543
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006544 if (event->attr.config != event_id)
6545 return 0;
6546
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006547 if (perf_exclude_event(event, regs))
6548 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006549
6550 return 1;
6551}
6552
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006553static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006554{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006555 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006556
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006557 return hash_64(val, SWEVENT_HLIST_BITS);
6558}
6559
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006560static inline struct hlist_head *
6561__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006562{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006563 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006564
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006565 return &hlist->heads[hash];
6566}
6567
6568/* For the read side: events when they trigger */
6569static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006570find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006571{
6572 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006573
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006574 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006575 if (!hlist)
6576 return NULL;
6577
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006578 return __find_swevent_head(hlist, type, event_id);
6579}
6580
6581/* For the event head insertion and removal in the hlist */
6582static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006583find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006584{
6585 struct swevent_hlist *hlist;
6586 u32 event_id = event->attr.config;
6587 u64 type = event->attr.type;
6588
6589 /*
6590 * Event scheduling is always serialized against hlist allocation
6591 * and release. Which makes the protected version suitable here.
6592 * The context lock guarantees that.
6593 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006594 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006595 lockdep_is_held(&event->ctx->lock));
6596 if (!hlist)
6597 return NULL;
6598
6599 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006600}
6601
6602static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006603 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006604 struct perf_sample_data *data,
6605 struct pt_regs *regs)
6606{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006607 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006608 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006609 struct hlist_head *head;
6610
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006611 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006612 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006613 if (!head)
6614 goto end;
6615
Sasha Levinb67bfe02013-02-27 17:06:00 -08006616 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08006617 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006618 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006619 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006620end:
6621 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006622}
6623
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006624DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
6625
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006626int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006627{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006628 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006629
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006630 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006631}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01006632EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006633
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01006634inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006635{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006636 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006637
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006638 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006639}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006640
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006641void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006642{
Ingo Molnara4234bf2009-11-23 10:57:59 +01006643 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006644
6645 if (WARN_ON_ONCE(!regs))
6646 return;
6647
6648 perf_sample_data_init(&data, addr, 0);
6649 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
6650}
6651
6652void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
6653{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006654 int rctx;
6655
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006656 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006657 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006658 if (unlikely(rctx < 0))
6659 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006660
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006661 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006662
6663 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006664fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006665 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006666}
6667
6668static void perf_swevent_read(struct perf_event *event)
6669{
6670}
6671
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006672static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006673{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006674 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006675 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006676 struct hlist_head *head;
6677
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006678 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006679 hwc->last_period = hwc->sample_period;
6680 perf_swevent_set_period(event);
6681 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006682
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006683 hwc->state = !(flags & PERF_EF_START);
6684
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006685 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01006686 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006687 return -EINVAL;
6688
6689 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08006690 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006691
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006692 return 0;
6693}
6694
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006695static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006696{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006697 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006698}
6699
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006700static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006701{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006702 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006703}
6704
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006705static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006706{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006707 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006708}
6709
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006710/* Deref the hlist from the update side */
6711static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006712swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006713{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006714 return rcu_dereference_protected(swhash->swevent_hlist,
6715 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006716}
6717
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006718static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006719{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006720 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006721
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006722 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006723 return;
6724
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03006725 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08006726 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006727}
6728
6729static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
6730{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006731 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006732
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006733 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006734
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006735 if (!--swhash->hlist_refcount)
6736 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006737
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006738 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006739}
6740
6741static void swevent_hlist_put(struct perf_event *event)
6742{
6743 int cpu;
6744
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006745 for_each_possible_cpu(cpu)
6746 swevent_hlist_put_cpu(event, cpu);
6747}
6748
6749static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
6750{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006751 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006752 int err = 0;
6753
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006754 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006755 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006756 struct swevent_hlist *hlist;
6757
6758 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
6759 if (!hlist) {
6760 err = -ENOMEM;
6761 goto exit;
6762 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006763 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006764 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006765 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006766exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006767 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006768
6769 return err;
6770}
6771
6772static int swevent_hlist_get(struct perf_event *event)
6773{
6774 int err;
6775 int cpu, failed_cpu;
6776
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006777 get_online_cpus();
6778 for_each_possible_cpu(cpu) {
6779 err = swevent_hlist_get_cpu(event, cpu);
6780 if (err) {
6781 failed_cpu = cpu;
6782 goto fail;
6783 }
6784 }
6785 put_online_cpus();
6786
6787 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006788fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006789 for_each_possible_cpu(cpu) {
6790 if (cpu == failed_cpu)
6791 break;
6792 swevent_hlist_put_cpu(event, cpu);
6793 }
6794
6795 put_online_cpus();
6796 return err;
6797}
6798
Ingo Molnarc5905af2012-02-24 08:31:31 +01006799struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006800
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006801static void sw_perf_event_destroy(struct perf_event *event)
6802{
6803 u64 event_id = event->attr.config;
6804
6805 WARN_ON(event->parent);
6806
Ingo Molnarc5905af2012-02-24 08:31:31 +01006807 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006808 swevent_hlist_put(event);
6809}
6810
6811static int perf_swevent_init(struct perf_event *event)
6812{
Tommi Rantala8176cce2013-04-13 22:49:14 +03006813 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006814
6815 if (event->attr.type != PERF_TYPE_SOFTWARE)
6816 return -ENOENT;
6817
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006818 /*
6819 * no branch sampling for software events
6820 */
6821 if (has_branch_stack(event))
6822 return -EOPNOTSUPP;
6823
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006824 switch (event_id) {
6825 case PERF_COUNT_SW_CPU_CLOCK:
6826 case PERF_COUNT_SW_TASK_CLOCK:
6827 return -ENOENT;
6828
6829 default:
6830 break;
6831 }
6832
Dan Carpenterce677832010-10-24 21:50:42 +02006833 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006834 return -ENOENT;
6835
6836 if (!event->parent) {
6837 int err;
6838
6839 err = swevent_hlist_get(event);
6840 if (err)
6841 return err;
6842
Ingo Molnarc5905af2012-02-24 08:31:31 +01006843 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006844 event->destroy = sw_perf_event_destroy;
6845 }
6846
6847 return 0;
6848}
6849
6850static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006851 .task_ctx_nr = perf_sw_context,
6852
Peter Zijlstra34f43922015-02-20 14:05:38 +01006853 .capabilities = PERF_PMU_CAP_NO_NMI,
6854
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006855 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006856 .add = perf_swevent_add,
6857 .del = perf_swevent_del,
6858 .start = perf_swevent_start,
6859 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006860 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006861};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006862
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006863#ifdef CONFIG_EVENT_TRACING
6864
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006865static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006866 struct perf_sample_data *data)
6867{
6868 void *record = data->raw->data;
6869
Peter Zijlstrab71b4372015-11-02 10:50:51 +01006870 /* only top level events have filters set */
6871 if (event->parent)
6872 event = event->parent;
6873
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006874 if (likely(!event->filter) || filter_match_preds(event->filter, record))
6875 return 1;
6876 return 0;
6877}
6878
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006879static int perf_tp_event_match(struct perf_event *event,
6880 struct perf_sample_data *data,
6881 struct pt_regs *regs)
6882{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01006883 if (event->hw.state & PERF_HES_STOPPED)
6884 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02006885 /*
6886 * All tracepoints are from kernel-space.
6887 */
6888 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006889 return 0;
6890
6891 if (!perf_tp_filter_match(event, data))
6892 return 0;
6893
6894 return 1;
6895}
6896
6897void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006898 struct pt_regs *regs, struct hlist_head *head, int rctx,
6899 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006900{
6901 struct perf_sample_data data;
6902 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006903
6904 struct perf_raw_record raw = {
6905 .size = entry_size,
6906 .data = record,
6907 };
6908
Robert Richterfd0d0002012-04-02 20:19:08 +02006909 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006910 data.raw = &raw;
6911
Sasha Levinb67bfe02013-02-27 17:06:00 -08006912 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006913 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006914 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006915 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006916
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006917 /*
6918 * If we got specified a target task, also iterate its context and
6919 * deliver this event there too.
6920 */
6921 if (task && task != current) {
6922 struct perf_event_context *ctx;
6923 struct trace_entry *entry = record;
6924
6925 rcu_read_lock();
6926 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
6927 if (!ctx)
6928 goto unlock;
6929
6930 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
6931 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6932 continue;
6933 if (event->attr.config != entry->type)
6934 continue;
6935 if (perf_tp_event_match(event, &data, regs))
6936 perf_swevent_event(event, count, &data, regs);
6937 }
6938unlock:
6939 rcu_read_unlock();
6940 }
6941
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006942 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006943}
6944EXPORT_SYMBOL_GPL(perf_tp_event);
6945
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006946static void tp_perf_event_destroy(struct perf_event *event)
6947{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006948 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006949}
6950
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006951static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006952{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006953 int err;
6954
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006955 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6956 return -ENOENT;
6957
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006958 /*
6959 * no branch sampling for tracepoint events
6960 */
6961 if (has_branch_stack(event))
6962 return -EOPNOTSUPP;
6963
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006964 err = perf_trace_init(event);
6965 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006966 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006967
6968 event->destroy = tp_perf_event_destroy;
6969
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006970 return 0;
6971}
6972
6973static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006974 .task_ctx_nr = perf_sw_context,
6975
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006976 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006977 .add = perf_trace_add,
6978 .del = perf_trace_del,
6979 .start = perf_swevent_start,
6980 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006981 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006982};
6983
6984static inline void perf_tp_register(void)
6985{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006986 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006987}
Li Zefan6fb29152009-10-15 11:21:42 +08006988
6989static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6990{
6991 char *filter_str;
6992 int ret;
6993
6994 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6995 return -EINVAL;
6996
6997 filter_str = strndup_user(arg, PAGE_SIZE);
6998 if (IS_ERR(filter_str))
6999 return PTR_ERR(filter_str);
7000
7001 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
7002
7003 kfree(filter_str);
7004 return ret;
7005}
7006
7007static void perf_event_free_filter(struct perf_event *event)
7008{
7009 ftrace_profile_free_filter(event);
7010}
7011
Alexei Starovoitov25415172015-03-25 12:49:20 -07007012static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7013{
7014 struct bpf_prog *prog;
7015
7016 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7017 return -EINVAL;
7018
7019 if (event->tp_event->prog)
7020 return -EEXIST;
7021
Wang Nan04a22fa2015-07-01 02:13:50 +00007022 if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE))
7023 /* bpf programs can only be attached to u/kprobes */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007024 return -EINVAL;
7025
7026 prog = bpf_prog_get(prog_fd);
7027 if (IS_ERR(prog))
7028 return PTR_ERR(prog);
7029
Linus Torvalds6c373ca2015-04-15 09:00:47 -07007030 if (prog->type != BPF_PROG_TYPE_KPROBE) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007031 /* valid fd, but invalid bpf program type */
7032 bpf_prog_put(prog);
7033 return -EINVAL;
7034 }
7035
7036 event->tp_event->prog = prog;
7037
7038 return 0;
7039}
7040
7041static void perf_event_free_bpf_prog(struct perf_event *event)
7042{
7043 struct bpf_prog *prog;
7044
7045 if (!event->tp_event)
7046 return;
7047
7048 prog = event->tp_event->prog;
7049 if (prog) {
7050 event->tp_event->prog = NULL;
7051 bpf_prog_put(prog);
7052 }
7053}
7054
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007055#else
Li Zefan6fb29152009-10-15 11:21:42 +08007056
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007057static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007058{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007059}
Li Zefan6fb29152009-10-15 11:21:42 +08007060
7061static int perf_event_set_filter(struct perf_event *event, void __user *arg)
7062{
7063 return -ENOENT;
7064}
7065
7066static void perf_event_free_filter(struct perf_event *event)
7067{
7068}
7069
Alexei Starovoitov25415172015-03-25 12:49:20 -07007070static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7071{
7072 return -ENOENT;
7073}
7074
7075static void perf_event_free_bpf_prog(struct perf_event *event)
7076{
7077}
Li Zefan07b139c2009-12-21 14:27:35 +08007078#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007079
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007080#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007081void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007082{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007083 struct perf_sample_data sample;
7084 struct pt_regs *regs = data;
7085
Robert Richterfd0d0002012-04-02 20:19:08 +02007086 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007087
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007088 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007089 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007090}
7091#endif
7092
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007093/*
7094 * hrtimer based swevent callback
7095 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007096
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007097static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007098{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007099 enum hrtimer_restart ret = HRTIMER_RESTART;
7100 struct perf_sample_data data;
7101 struct pt_regs *regs;
7102 struct perf_event *event;
7103 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007104
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007105 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007106
7107 if (event->state != PERF_EVENT_STATE_ACTIVE)
7108 return HRTIMER_NORESTART;
7109
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007110 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007111
Robert Richterfd0d0002012-04-02 20:19:08 +02007112 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007113 regs = get_irq_regs();
7114
7115 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08007116 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02007117 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007118 ret = HRTIMER_NORESTART;
7119 }
7120
7121 period = max_t(u64, 10000, event->hw.sample_period);
7122 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
7123
7124 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007125}
7126
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007127static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007128{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007129 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007130 s64 period;
7131
7132 if (!is_sampling_event(event))
7133 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007134
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007135 period = local64_read(&hwc->period_left);
7136 if (period) {
7137 if (period < 0)
7138 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007139
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007140 local64_set(&hwc->period_left, 0);
7141 } else {
7142 period = max_t(u64, 10000, hwc->sample_period);
7143 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00007144 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
7145 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007146}
7147
7148static void perf_swevent_cancel_hrtimer(struct perf_event *event)
7149{
7150 struct hw_perf_event *hwc = &event->hw;
7151
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007152 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007153 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007154 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007155
7156 hrtimer_cancel(&hwc->hrtimer);
7157 }
7158}
7159
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007160static void perf_swevent_init_hrtimer(struct perf_event *event)
7161{
7162 struct hw_perf_event *hwc = &event->hw;
7163
7164 if (!is_sampling_event(event))
7165 return;
7166
7167 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
7168 hwc->hrtimer.function = perf_swevent_hrtimer;
7169
7170 /*
7171 * Since hrtimers have a fixed rate, we can do a static freq->period
7172 * mapping and avoid the whole period adjust feedback stuff.
7173 */
7174 if (event->attr.freq) {
7175 long freq = event->attr.sample_freq;
7176
7177 event->attr.sample_period = NSEC_PER_SEC / freq;
7178 hwc->sample_period = event->attr.sample_period;
7179 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09007180 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007181 event->attr.freq = 0;
7182 }
7183}
7184
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007185/*
7186 * Software event: cpu wall time clock
7187 */
7188
7189static void cpu_clock_event_update(struct perf_event *event)
7190{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007191 s64 prev;
7192 u64 now;
7193
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007194 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007195 prev = local64_xchg(&event->hw.prev_count, now);
7196 local64_add(now - prev, &event->count);
7197}
7198
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007199static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007200{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007201 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007202 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007203}
7204
7205static void cpu_clock_event_stop(struct perf_event *event, int flags)
7206{
7207 perf_swevent_cancel_hrtimer(event);
7208 cpu_clock_event_update(event);
7209}
7210
7211static int cpu_clock_event_add(struct perf_event *event, int flags)
7212{
7213 if (flags & PERF_EF_START)
7214 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08007215 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007216
7217 return 0;
7218}
7219
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007220static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007221{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007222 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007223}
7224
7225static void cpu_clock_event_read(struct perf_event *event)
7226{
7227 cpu_clock_event_update(event);
7228}
7229
7230static int cpu_clock_event_init(struct perf_event *event)
7231{
7232 if (event->attr.type != PERF_TYPE_SOFTWARE)
7233 return -ENOENT;
7234
7235 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
7236 return -ENOENT;
7237
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007238 /*
7239 * no branch sampling for software events
7240 */
7241 if (has_branch_stack(event))
7242 return -EOPNOTSUPP;
7243
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007244 perf_swevent_init_hrtimer(event);
7245
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007246 return 0;
7247}
7248
7249static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007250 .task_ctx_nr = perf_sw_context,
7251
Peter Zijlstra34f43922015-02-20 14:05:38 +01007252 .capabilities = PERF_PMU_CAP_NO_NMI,
7253
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007254 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007255 .add = cpu_clock_event_add,
7256 .del = cpu_clock_event_del,
7257 .start = cpu_clock_event_start,
7258 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007259 .read = cpu_clock_event_read,
7260};
7261
7262/*
7263 * Software event: task time clock
7264 */
7265
7266static void task_clock_event_update(struct perf_event *event, u64 now)
7267{
7268 u64 prev;
7269 s64 delta;
7270
7271 prev = local64_xchg(&event->hw.prev_count, now);
7272 delta = now - prev;
7273 local64_add(delta, &event->count);
7274}
7275
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007276static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007277{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007278 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007279 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007280}
7281
7282static void task_clock_event_stop(struct perf_event *event, int flags)
7283{
7284 perf_swevent_cancel_hrtimer(event);
7285 task_clock_event_update(event, event->ctx->time);
7286}
7287
7288static int task_clock_event_add(struct perf_event *event, int flags)
7289{
7290 if (flags & PERF_EF_START)
7291 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08007292 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007293
7294 return 0;
7295}
7296
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007297static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007298{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007299 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007300}
7301
7302static void task_clock_event_read(struct perf_event *event)
7303{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01007304 u64 now = perf_clock();
7305 u64 delta = now - event->ctx->timestamp;
7306 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007307
7308 task_clock_event_update(event, time);
7309}
7310
7311static int task_clock_event_init(struct perf_event *event)
7312{
7313 if (event->attr.type != PERF_TYPE_SOFTWARE)
7314 return -ENOENT;
7315
7316 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
7317 return -ENOENT;
7318
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007319 /*
7320 * no branch sampling for software events
7321 */
7322 if (has_branch_stack(event))
7323 return -EOPNOTSUPP;
7324
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007325 perf_swevent_init_hrtimer(event);
7326
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007327 return 0;
7328}
7329
7330static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007331 .task_ctx_nr = perf_sw_context,
7332
Peter Zijlstra34f43922015-02-20 14:05:38 +01007333 .capabilities = PERF_PMU_CAP_NO_NMI,
7334
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007335 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007336 .add = task_clock_event_add,
7337 .del = task_clock_event_del,
7338 .start = task_clock_event_start,
7339 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007340 .read = task_clock_event_read,
7341};
7342
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007343static void perf_pmu_nop_void(struct pmu *pmu)
7344{
7345}
7346
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007347static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
7348{
7349}
7350
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007351static int perf_pmu_nop_int(struct pmu *pmu)
7352{
7353 return 0;
7354}
7355
Geliang Tang18ab2cd2015-09-27 23:25:50 +08007356static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007357
7358static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007359{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007360 __this_cpu_write(nop_txn_flags, flags);
7361
7362 if (flags & ~PERF_PMU_TXN_ADD)
7363 return;
7364
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007365 perf_pmu_disable(pmu);
7366}
7367
7368static int perf_pmu_commit_txn(struct pmu *pmu)
7369{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007370 unsigned int flags = __this_cpu_read(nop_txn_flags);
7371
7372 __this_cpu_write(nop_txn_flags, 0);
7373
7374 if (flags & ~PERF_PMU_TXN_ADD)
7375 return 0;
7376
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007377 perf_pmu_enable(pmu);
7378 return 0;
7379}
7380
7381static void perf_pmu_cancel_txn(struct pmu *pmu)
7382{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007383 unsigned int flags = __this_cpu_read(nop_txn_flags);
7384
7385 __this_cpu_write(nop_txn_flags, 0);
7386
7387 if (flags & ~PERF_PMU_TXN_ADD)
7388 return;
7389
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007390 perf_pmu_enable(pmu);
7391}
7392
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007393static int perf_event_idx_default(struct perf_event *event)
7394{
Peter Zijlstrac719f562014-10-21 11:10:21 +02007395 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007396}
7397
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007398/*
7399 * Ensures all contexts with the same task_ctx_nr have the same
7400 * pmu_cpu_context too.
7401 */
Mark Rutland9e317042014-02-10 17:44:18 +00007402static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007403{
7404 struct pmu *pmu;
7405
7406 if (ctxn < 0)
7407 return NULL;
7408
7409 list_for_each_entry(pmu, &pmus, entry) {
7410 if (pmu->task_ctx_nr == ctxn)
7411 return pmu->pmu_cpu_context;
7412 }
7413
7414 return NULL;
7415}
7416
Peter Zijlstra51676952010-12-07 14:18:20 +01007417static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007418{
Peter Zijlstra51676952010-12-07 14:18:20 +01007419 int cpu;
7420
7421 for_each_possible_cpu(cpu) {
7422 struct perf_cpu_context *cpuctx;
7423
7424 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
7425
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007426 if (cpuctx->unique_pmu == old_pmu)
7427 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01007428 }
7429}
7430
7431static void free_pmu_context(struct pmu *pmu)
7432{
7433 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007434
7435 mutex_lock(&pmus_lock);
7436 /*
7437 * Like a real lame refcount.
7438 */
Peter Zijlstra51676952010-12-07 14:18:20 +01007439 list_for_each_entry(i, &pmus, entry) {
7440 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
7441 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007442 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01007443 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007444 }
7445
Peter Zijlstra51676952010-12-07 14:18:20 +01007446 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007447out:
7448 mutex_unlock(&pmus_lock);
7449}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007450static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007451
Peter Zijlstraabe43402010-11-17 23:17:37 +01007452static ssize_t
7453type_show(struct device *dev, struct device_attribute *attr, char *page)
7454{
7455 struct pmu *pmu = dev_get_drvdata(dev);
7456
7457 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
7458}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007459static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007460
Stephane Eranian62b85632013-04-03 14:21:34 +02007461static ssize_t
7462perf_event_mux_interval_ms_show(struct device *dev,
7463 struct device_attribute *attr,
7464 char *page)
7465{
7466 struct pmu *pmu = dev_get_drvdata(dev);
7467
7468 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
7469}
7470
Peter Zijlstra272325c2015-04-15 11:41:58 +02007471static DEFINE_MUTEX(mux_interval_mutex);
7472
Stephane Eranian62b85632013-04-03 14:21:34 +02007473static ssize_t
7474perf_event_mux_interval_ms_store(struct device *dev,
7475 struct device_attribute *attr,
7476 const char *buf, size_t count)
7477{
7478 struct pmu *pmu = dev_get_drvdata(dev);
7479 int timer, cpu, ret;
7480
7481 ret = kstrtoint(buf, 0, &timer);
7482 if (ret)
7483 return ret;
7484
7485 if (timer < 1)
7486 return -EINVAL;
7487
7488 /* same value, noting to do */
7489 if (timer == pmu->hrtimer_interval_ms)
7490 return count;
7491
Peter Zijlstra272325c2015-04-15 11:41:58 +02007492 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02007493 pmu->hrtimer_interval_ms = timer;
7494
7495 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02007496 get_online_cpus();
7497 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02007498 struct perf_cpu_context *cpuctx;
7499 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
7500 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
7501
Peter Zijlstra272325c2015-04-15 11:41:58 +02007502 cpu_function_call(cpu,
7503 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02007504 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02007505 put_online_cpus();
7506 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02007507
7508 return count;
7509}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007510static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02007511
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007512static struct attribute *pmu_dev_attrs[] = {
7513 &dev_attr_type.attr,
7514 &dev_attr_perf_event_mux_interval_ms.attr,
7515 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01007516};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007517ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007518
7519static int pmu_bus_running;
7520static struct bus_type pmu_bus = {
7521 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007522 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01007523};
7524
7525static void pmu_dev_release(struct device *dev)
7526{
7527 kfree(dev);
7528}
7529
7530static int pmu_dev_alloc(struct pmu *pmu)
7531{
7532 int ret = -ENOMEM;
7533
7534 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
7535 if (!pmu->dev)
7536 goto out;
7537
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01007538 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01007539 device_initialize(pmu->dev);
7540 ret = dev_set_name(pmu->dev, "%s", pmu->name);
7541 if (ret)
7542 goto free_dev;
7543
7544 dev_set_drvdata(pmu->dev, pmu);
7545 pmu->dev->bus = &pmu_bus;
7546 pmu->dev->release = pmu_dev_release;
7547 ret = device_add(pmu->dev);
7548 if (ret)
7549 goto free_dev;
7550
7551out:
7552 return ret;
7553
7554free_dev:
7555 put_device(pmu->dev);
7556 goto out;
7557}
7558
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007559static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007560static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007561
Mischa Jonker03d8e802013-06-04 11:45:48 +02007562int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007563{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007564 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007565
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007566 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007567 ret = -ENOMEM;
7568 pmu->pmu_disable_count = alloc_percpu(int);
7569 if (!pmu->pmu_disable_count)
7570 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007571
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007572 pmu->type = -1;
7573 if (!name)
7574 goto skip_type;
7575 pmu->name = name;
7576
7577 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08007578 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
7579 if (type < 0) {
7580 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007581 goto free_pdc;
7582 }
7583 }
7584 pmu->type = type;
7585
Peter Zijlstraabe43402010-11-17 23:17:37 +01007586 if (pmu_bus_running) {
7587 ret = pmu_dev_alloc(pmu);
7588 if (ret)
7589 goto free_idr;
7590 }
7591
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007592skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007593 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
7594 if (pmu->pmu_cpu_context)
7595 goto got_cpu_context;
7596
Wei Yongjunc4814202013-04-12 11:05:54 +08007597 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007598 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
7599 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01007600 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007601
7602 for_each_possible_cpu(cpu) {
7603 struct perf_cpu_context *cpuctx;
7604
7605 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02007606 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007607 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007608 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007609 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02007610
Peter Zijlstra272325c2015-04-15 11:41:58 +02007611 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02007612
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007613 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007614 }
7615
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007616got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007617 if (!pmu->start_txn) {
7618 if (pmu->pmu_enable) {
7619 /*
7620 * If we have pmu_enable/pmu_disable calls, install
7621 * transaction stubs that use that to try and batch
7622 * hardware accesses.
7623 */
7624 pmu->start_txn = perf_pmu_start_txn;
7625 pmu->commit_txn = perf_pmu_commit_txn;
7626 pmu->cancel_txn = perf_pmu_cancel_txn;
7627 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007628 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007629 pmu->commit_txn = perf_pmu_nop_int;
7630 pmu->cancel_txn = perf_pmu_nop_void;
7631 }
7632 }
7633
7634 if (!pmu->pmu_enable) {
7635 pmu->pmu_enable = perf_pmu_nop_void;
7636 pmu->pmu_disable = perf_pmu_nop_void;
7637 }
7638
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007639 if (!pmu->event_idx)
7640 pmu->event_idx = perf_event_idx_default;
7641
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007642 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007643 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007644 ret = 0;
7645unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007646 mutex_unlock(&pmus_lock);
7647
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007648 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007649
Peter Zijlstraabe43402010-11-17 23:17:37 +01007650free_dev:
7651 device_del(pmu->dev);
7652 put_device(pmu->dev);
7653
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007654free_idr:
7655 if (pmu->type >= PERF_TYPE_MAX)
7656 idr_remove(&pmu_idr, pmu->type);
7657
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007658free_pdc:
7659 free_percpu(pmu->pmu_disable_count);
7660 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007661}
Yan, Zhengc464c762014-03-18 16:56:41 +08007662EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007663
7664void perf_pmu_unregister(struct pmu *pmu)
7665{
7666 mutex_lock(&pmus_lock);
7667 list_del_rcu(&pmu->entry);
7668 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007669
7670 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02007671 * We dereference the pmu list under both SRCU and regular RCU, so
7672 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007673 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007674 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02007675 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007676
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007677 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007678 if (pmu->type >= PERF_TYPE_MAX)
7679 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007680 device_del(pmu->dev);
7681 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01007682 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007683}
Yan, Zhengc464c762014-03-18 16:56:41 +08007684EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007685
Mark Rutlandcc34b982015-01-07 14:56:51 +00007686static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
7687{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007688 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00007689 int ret;
7690
7691 if (!try_module_get(pmu->module))
7692 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007693
7694 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02007695 /*
7696 * This ctx->mutex can nest when we're called through
7697 * inheritance. See the perf_event_ctx_lock_nested() comment.
7698 */
7699 ctx = perf_event_ctx_lock_nested(event->group_leader,
7700 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007701 BUG_ON(!ctx);
7702 }
7703
Mark Rutlandcc34b982015-01-07 14:56:51 +00007704 event->pmu = pmu;
7705 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007706
7707 if (ctx)
7708 perf_event_ctx_unlock(event->group_leader, ctx);
7709
Mark Rutlandcc34b982015-01-07 14:56:51 +00007710 if (ret)
7711 module_put(pmu->module);
7712
7713 return ret;
7714}
7715
Geliang Tang18ab2cd2015-09-27 23:25:50 +08007716static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007717{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007718 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007719 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08007720 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007721
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007722 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007723
7724 rcu_read_lock();
7725 pmu = idr_find(&pmu_idr, event->attr.type);
7726 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08007727 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007728 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08007729 if (ret)
7730 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007731 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08007732 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007733
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007734 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007735 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007736 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007737 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007738
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007739 if (ret != -ENOENT) {
7740 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007741 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007742 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007743 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007744 pmu = ERR_PTR(-ENOENT);
7745unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007746 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007747
7748 return pmu;
7749}
7750
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007751static void account_event_cpu(struct perf_event *event, int cpu)
7752{
7753 if (event->parent)
7754 return;
7755
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007756 if (is_cgroup_event(event))
7757 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
7758}
7759
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007760static void account_event(struct perf_event *event)
7761{
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007762 if (event->parent)
7763 return;
7764
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007765 if (event->attach_state & PERF_ATTACH_TASK)
7766 static_key_slow_inc(&perf_sched_events.key);
7767 if (event->attr.mmap || event->attr.mmap_data)
7768 atomic_inc(&nr_mmap_events);
7769 if (event->attr.comm)
7770 atomic_inc(&nr_comm_events);
7771 if (event->attr.task)
7772 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02007773 if (event->attr.freq) {
7774 if (atomic_inc_return(&nr_freq_events) == 1)
7775 tick_nohz_full_kick_all();
7776 }
Adrian Hunter45ac1402015-07-21 12:44:02 +03007777 if (event->attr.context_switch) {
7778 atomic_inc(&nr_switch_events);
7779 static_key_slow_inc(&perf_sched_events.key);
7780 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007781 if (has_branch_stack(event))
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007782 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007783 if (is_cgroup_event(event))
7784 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007785
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007786 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007787}
7788
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007789/*
7790 * Allocate and initialize a event structure
7791 */
7792static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007793perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007794 struct task_struct *task,
7795 struct perf_event *group_leader,
7796 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007797 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00007798 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007799{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007800 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007801 struct perf_event *event;
7802 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007803 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007804
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007805 if ((unsigned)cpu >= nr_cpu_ids) {
7806 if (!task || cpu != -1)
7807 return ERR_PTR(-EINVAL);
7808 }
7809
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007810 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007811 if (!event)
7812 return ERR_PTR(-ENOMEM);
7813
7814 /*
7815 * Single events are their own group leaders, with an
7816 * empty sibling list:
7817 */
7818 if (!group_leader)
7819 group_leader = event;
7820
7821 mutex_init(&event->child_mutex);
7822 INIT_LIST_HEAD(&event->child_list);
7823
7824 INIT_LIST_HEAD(&event->group_entry);
7825 INIT_LIST_HEAD(&event->event_entry);
7826 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007827 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01007828 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01007829 INIT_HLIST_NODE(&event->hlist_entry);
7830
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007831
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007832 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08007833 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007834
7835 mutex_init(&event->mmap_mutex);
7836
Al Viroa6fa9412012-08-20 14:59:25 +01007837 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007838 event->cpu = cpu;
7839 event->attr = *attr;
7840 event->group_leader = group_leader;
7841 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007842 event->oncpu = -1;
7843
7844 event->parent = parent_event;
7845
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08007846 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007847 event->id = atomic64_inc_return(&perf_event_id);
7848
7849 event->state = PERF_EVENT_STATE_INACTIVE;
7850
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007851 if (task) {
7852 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007853 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01007854 * XXX pmu::event_init needs to know what task to account to
7855 * and we cannot use the ctx information because we need the
7856 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007857 */
Peter Zijlstra50f16a82015-03-05 22:10:19 +01007858 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007859 }
7860
Peter Zijlstra34f43922015-02-20 14:05:38 +01007861 event->clock = &local_clock;
7862 if (parent_event)
7863 event->clock = parent_event->clock;
7864
Avi Kivity4dc0da82011-06-29 18:42:35 +03007865 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007866 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007867 context = parent_event->overflow_handler_context;
7868 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007869
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007870 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007871 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02007872
Jiri Olsa0231bb52013-02-01 11:23:45 +01007873 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007874
7875 pmu = NULL;
7876
7877 hwc = &event->hw;
7878 hwc->sample_period = attr->sample_period;
7879 if (attr->freq && attr->sample_freq)
7880 hwc->sample_period = 1;
7881 hwc->last_period = hwc->sample_period;
7882
Peter Zijlstrae7850592010-05-21 14:43:08 +02007883 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007884
7885 /*
7886 * we currently do not support PERF_FORMAT_GROUP on inherited events
7887 */
7888 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007889 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007890
Yan, Zhenga46a2302014-11-04 21:56:06 -05007891 if (!has_branch_stack(event))
7892 event->attr.branch_sample_type = 0;
7893
Matt Fleming79dff512015-01-23 18:45:42 +00007894 if (cgroup_fd != -1) {
7895 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
7896 if (err)
7897 goto err_ns;
7898 }
7899
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007900 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007901 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007902 goto err_ns;
7903 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007904 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007905 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007906 }
7907
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007908 err = exclusive_event_init(event);
7909 if (err)
7910 goto err_pmu;
7911
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007912 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007913 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
7914 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007915 if (err)
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007916 goto err_per_task;
Stephane Eraniand010b332012-02-09 23:21:00 +01007917 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007918 }
7919
7920 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007921
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007922err_per_task:
7923 exclusive_event_destroy(event);
7924
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007925err_pmu:
7926 if (event->destroy)
7927 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08007928 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007929err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00007930 if (is_cgroup_event(event))
7931 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007932 if (event->ns)
7933 put_pid_ns(event->ns);
7934 kfree(event);
7935
7936 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007937}
7938
7939static int perf_copy_attr(struct perf_event_attr __user *uattr,
7940 struct perf_event_attr *attr)
7941{
7942 u32 size;
7943 int ret;
7944
7945 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
7946 return -EFAULT;
7947
7948 /*
7949 * zero the full structure, so that a short copy will be nice.
7950 */
7951 memset(attr, 0, sizeof(*attr));
7952
7953 ret = get_user(size, &uattr->size);
7954 if (ret)
7955 return ret;
7956
7957 if (size > PAGE_SIZE) /* silly large */
7958 goto err_size;
7959
7960 if (!size) /* abi compat */
7961 size = PERF_ATTR_SIZE_VER0;
7962
7963 if (size < PERF_ATTR_SIZE_VER0)
7964 goto err_size;
7965
7966 /*
7967 * If we're handed a bigger struct than we know of,
7968 * ensure all the unknown bits are 0 - i.e. new
7969 * user-space does not rely on any kernel feature
7970 * extensions we dont know about yet.
7971 */
7972 if (size > sizeof(*attr)) {
7973 unsigned char __user *addr;
7974 unsigned char __user *end;
7975 unsigned char val;
7976
7977 addr = (void __user *)uattr + sizeof(*attr);
7978 end = (void __user *)uattr + size;
7979
7980 for (; addr < end; addr++) {
7981 ret = get_user(val, addr);
7982 if (ret)
7983 return ret;
7984 if (val)
7985 goto err_size;
7986 }
7987 size = sizeof(*attr);
7988 }
7989
7990 ret = copy_from_user(attr, uattr, size);
7991 if (ret)
7992 return -EFAULT;
7993
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05307994 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007995 return -EINVAL;
7996
7997 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
7998 return -EINVAL;
7999
8000 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
8001 return -EINVAL;
8002
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008003 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
8004 u64 mask = attr->branch_sample_type;
8005
8006 /* only using defined bits */
8007 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
8008 return -EINVAL;
8009
8010 /* at least one branch bit must be set */
8011 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
8012 return -EINVAL;
8013
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008014 /* propagate priv level, when not set for branch */
8015 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
8016
8017 /* exclude_kernel checked on syscall entry */
8018 if (!attr->exclude_kernel)
8019 mask |= PERF_SAMPLE_BRANCH_KERNEL;
8020
8021 if (!attr->exclude_user)
8022 mask |= PERF_SAMPLE_BRANCH_USER;
8023
8024 if (!attr->exclude_hv)
8025 mask |= PERF_SAMPLE_BRANCH_HV;
8026 /*
8027 * adjust user setting (for HW filter setup)
8028 */
8029 attr->branch_sample_type = mask;
8030 }
Stephane Eraniane7122092013-06-06 11:02:04 +02008031 /* privileged levels capture (kernel, hv): check permissions */
8032 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02008033 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8034 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008035 }
Jiri Olsa40189942012-08-07 15:20:37 +02008036
Jiri Olsac5ebced2012-08-07 15:20:40 +02008037 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02008038 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02008039 if (ret)
8040 return ret;
8041 }
8042
8043 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
8044 if (!arch_perf_have_user_stack_dump())
8045 return -ENOSYS;
8046
8047 /*
8048 * We have __u32 type for the size, but so far
8049 * we can only use __u16 as maximum due to the
8050 * __u16 sample size limit.
8051 */
8052 if (attr->sample_stack_user >= USHRT_MAX)
8053 ret = -EINVAL;
8054 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
8055 ret = -EINVAL;
8056 }
Jiri Olsa40189942012-08-07 15:20:37 +02008057
Stephane Eranian60e23642014-09-24 13:48:37 +02008058 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
8059 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008060out:
8061 return ret;
8062
8063err_size:
8064 put_user(sizeof(*attr), &uattr->size);
8065 ret = -E2BIG;
8066 goto out;
8067}
8068
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008069static int
8070perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008071{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01008072 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008073 int ret = -EINVAL;
8074
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008075 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008076 goto set;
8077
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008078 /* don't allow circular references */
8079 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008080 goto out;
8081
Peter Zijlstra0f139302010-05-20 14:35:15 +02008082 /*
8083 * Don't allow cross-cpu buffers
8084 */
8085 if (output_event->cpu != event->cpu)
8086 goto out;
8087
8088 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02008089 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02008090 */
8091 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
8092 goto out;
8093
Peter Zijlstra34f43922015-02-20 14:05:38 +01008094 /*
8095 * Mixing clocks in the same buffer is trouble you don't need.
8096 */
8097 if (output_event->clock != event->clock)
8098 goto out;
8099
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02008100 /*
8101 * If both events generate aux data, they must be on the same PMU
8102 */
8103 if (has_aux(event) && has_aux(output_event) &&
8104 event->pmu != output_event->pmu)
8105 goto out;
8106
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008107set:
8108 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008109 /* Can't redirect output if we've got an active mmap() */
8110 if (atomic_read(&event->mmap_count))
8111 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008112
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008113 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02008114 /* get the rb we want to redirect to */
8115 rb = ring_buffer_get(output_event);
8116 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008117 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008118 }
8119
Peter Zijlstrab69cf532014-03-14 10:50:33 +01008120 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02008121
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008122 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008123unlock:
8124 mutex_unlock(&event->mmap_mutex);
8125
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008126out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008127 return ret;
8128}
8129
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008130static void mutex_lock_double(struct mutex *a, struct mutex *b)
8131{
8132 if (b < a)
8133 swap(a, b);
8134
8135 mutex_lock(a);
8136 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
8137}
8138
Peter Zijlstra34f43922015-02-20 14:05:38 +01008139static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
8140{
8141 bool nmi_safe = false;
8142
8143 switch (clk_id) {
8144 case CLOCK_MONOTONIC:
8145 event->clock = &ktime_get_mono_fast_ns;
8146 nmi_safe = true;
8147 break;
8148
8149 case CLOCK_MONOTONIC_RAW:
8150 event->clock = &ktime_get_raw_fast_ns;
8151 nmi_safe = true;
8152 break;
8153
8154 case CLOCK_REALTIME:
8155 event->clock = &ktime_get_real_ns;
8156 break;
8157
8158 case CLOCK_BOOTTIME:
8159 event->clock = &ktime_get_boot_ns;
8160 break;
8161
8162 case CLOCK_TAI:
8163 event->clock = &ktime_get_tai_ns;
8164 break;
8165
8166 default:
8167 return -EINVAL;
8168 }
8169
8170 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
8171 return -EINVAL;
8172
8173 return 0;
8174}
8175
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008176/**
8177 * sys_perf_event_open - open a performance event, associate it to a task/cpu
8178 *
8179 * @attr_uptr: event_id type attributes for monitoring/sampling
8180 * @pid: target pid
8181 * @cpu: target cpu
8182 * @group_fd: group leader event fd
8183 */
8184SYSCALL_DEFINE5(perf_event_open,
8185 struct perf_event_attr __user *, attr_uptr,
8186 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
8187{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008188 struct perf_event *group_leader = NULL, *output_event = NULL;
8189 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008190 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008191 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008192 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04008193 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07008194 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008195 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04008196 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008197 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008198 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01008199 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00008200 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008201
8202 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02008203 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008204 return -EINVAL;
8205
8206 err = perf_copy_attr(attr_uptr, &attr);
8207 if (err)
8208 return err;
8209
8210 if (!attr.exclude_kernel) {
8211 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8212 return -EACCES;
8213 }
8214
8215 if (attr.freq) {
8216 if (attr.sample_freq > sysctl_perf_event_sample_rate)
8217 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02008218 } else {
8219 if (attr.sample_period & (1ULL << 63))
8220 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008221 }
8222
Stephane Eraniane5d13672011-02-14 11:20:01 +02008223 /*
8224 * In cgroup mode, the pid argument is used to pass the fd
8225 * opened to the cgroup directory in cgroupfs. The cpu argument
8226 * designates the cpu on which to monitor threads from that
8227 * cgroup.
8228 */
8229 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
8230 return -EINVAL;
8231
Yann Droneauda21b0b32014-01-05 21:36:33 +01008232 if (flags & PERF_FLAG_FD_CLOEXEC)
8233 f_flags |= O_CLOEXEC;
8234
8235 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04008236 if (event_fd < 0)
8237 return event_fd;
8238
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008239 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04008240 err = perf_fget_light(group_fd, &group);
8241 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008242 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04008243 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008244 if (flags & PERF_FLAG_FD_OUTPUT)
8245 output_event = group_leader;
8246 if (flags & PERF_FLAG_FD_NO_GROUP)
8247 group_leader = NULL;
8248 }
8249
Stephane Eraniane5d13672011-02-14 11:20:01 +02008250 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008251 task = find_lively_task_by_vpid(pid);
8252 if (IS_ERR(task)) {
8253 err = PTR_ERR(task);
8254 goto err_group_fd;
8255 }
8256 }
8257
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008258 if (task && group_leader &&
8259 group_leader->attr.inherit != attr.inherit) {
8260 err = -EINVAL;
8261 goto err_task;
8262 }
8263
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008264 get_online_cpus();
8265
Matt Fleming79dff512015-01-23 18:45:42 +00008266 if (flags & PERF_FLAG_PID_CGROUP)
8267 cgroup_fd = pid;
8268
Avi Kivity4dc0da82011-06-29 18:42:35 +03008269 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00008270 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008271 if (IS_ERR(event)) {
8272 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008273 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008274 }
8275
Vince Weaver53b25332014-05-16 17:12:12 -04008276 if (is_sampling_event(event)) {
8277 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
8278 err = -ENOTSUPP;
8279 goto err_alloc;
8280 }
8281 }
8282
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008283 account_event(event);
8284
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008285 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008286 * Special case software events and allow them to be part of
8287 * any hardware group.
8288 */
8289 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008290
Peter Zijlstra34f43922015-02-20 14:05:38 +01008291 if (attr.use_clockid) {
8292 err = perf_event_set_clock(event, attr.clockid);
8293 if (err)
8294 goto err_alloc;
8295 }
8296
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008297 if (group_leader &&
8298 (is_software_event(event) != is_software_event(group_leader))) {
8299 if (is_software_event(event)) {
8300 /*
8301 * If event and group_leader are not both a software
8302 * event, and event is, then group leader is not.
8303 *
8304 * Allow the addition of software events to !software
8305 * groups, this is safe because software events never
8306 * fail to schedule.
8307 */
8308 pmu = group_leader->pmu;
8309 } else if (is_software_event(group_leader) &&
8310 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
8311 /*
8312 * In case the group is a pure software group, and we
8313 * try to add a hardware event, move the whole group to
8314 * the hardware context.
8315 */
8316 move_group = 1;
8317 }
8318 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008319
8320 /*
8321 * Get the target context (task or percpu):
8322 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05008323 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008324 if (IS_ERR(ctx)) {
8325 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008326 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008327 }
8328
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008329 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
8330 err = -EBUSY;
8331 goto err_context;
8332 }
8333
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02008334 if (task) {
8335 put_task_struct(task);
8336 task = NULL;
8337 }
8338
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008339 /*
8340 * Look up the group leader (we will attach this event to it):
8341 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008342 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008343 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008344
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008345 /*
8346 * Do not allow a recursive hierarchy (this new sibling
8347 * becoming part of another group-sibling):
8348 */
8349 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008350 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01008351
8352 /* All events in a group should have the same clock */
8353 if (group_leader->clock != event->clock)
8354 goto err_context;
8355
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008356 /*
8357 * Do not allow to attach to a group in a different
8358 * task or CPU context:
8359 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008360 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01008361 /*
8362 * Make sure we're both on the same task, or both
8363 * per-cpu events.
8364 */
8365 if (group_leader->ctx->task != ctx->task)
8366 goto err_context;
8367
8368 /*
8369 * Make sure we're both events for the same CPU;
8370 * grouping events for different CPUs is broken; since
8371 * you can never concurrently schedule them anyhow.
8372 */
8373 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008374 goto err_context;
8375 } else {
8376 if (group_leader->ctx != ctx)
8377 goto err_context;
8378 }
8379
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008380 /*
8381 * Only a group leader can be exclusive or pinned
8382 */
8383 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008384 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008385 }
8386
8387 if (output_event) {
8388 err = perf_event_set_output(event, output_event);
8389 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008390 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008391 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008392
Yann Droneauda21b0b32014-01-05 21:36:33 +01008393 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
8394 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04008395 if (IS_ERR(event_file)) {
8396 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008397 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04008398 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008399
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008400 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008401 gctx = group_leader->ctx;
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008402 mutex_lock_double(&gctx->mutex, &ctx->mutex);
8403 } else {
8404 mutex_lock(&ctx->mutex);
8405 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008406
Peter Zijlstraa7239682015-09-09 19:06:33 +02008407 if (!perf_event_validate_size(event)) {
8408 err = -E2BIG;
8409 goto err_locked;
8410 }
8411
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008412 /*
8413 * Must be under the same ctx::mutex as perf_install_in_context(),
8414 * because we need to serialize with concurrent event creation.
8415 */
8416 if (!exclusive_event_installable(event, ctx)) {
8417 /* exclusive and group stuff are assumed mutually exclusive */
8418 WARN_ON_ONCE(move_group);
8419
8420 err = -EBUSY;
8421 goto err_locked;
8422 }
8423
8424 WARN_ON_ONCE(ctx->parent_ctx);
8425
8426 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008427 /*
8428 * See perf_event_ctx_lock() for comments on the details
8429 * of swizzling perf_event::ctx.
8430 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02008431 perf_remove_from_context(group_leader, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01008432
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008433 list_for_each_entry(sibling, &group_leader->sibling_list,
8434 group_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02008435 perf_remove_from_context(sibling, false);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008436 put_ctx(gctx);
8437 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008438
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008439 /*
8440 * Wait for everybody to stop referencing the events through
8441 * the old lists, before installing it on new lists.
8442 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008443 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008444
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008445 /*
8446 * Install the group siblings before the group leader.
8447 *
8448 * Because a group leader will try and install the entire group
8449 * (through the sibling list, which is still in-tact), we can
8450 * end up with siblings installed in the wrong context.
8451 *
8452 * By installing siblings first we NO-OP because they're not
8453 * reachable through the group lists.
8454 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008455 list_for_each_entry(sibling, &group_leader->sibling_list,
8456 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008457 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01008458 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008459 get_ctx(ctx);
8460 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008461
8462 /*
8463 * Removing from the context ends up with disabled
8464 * event. What we want here is event in the initial
8465 * startup state, ready to be add into new context.
8466 */
8467 perf_event__state_init(group_leader);
8468 perf_install_in_context(ctx, group_leader, group_leader->cpu);
8469 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008470
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008471 /*
8472 * Now that all events are installed in @ctx, nothing
8473 * references @gctx anymore, so drop the last reference we have
8474 * on it.
8475 */
8476 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008477 }
8478
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02008479 /*
8480 * Precalculate sample_data sizes; do while holding ctx::mutex such
8481 * that we're serialized against further additions and before
8482 * perf_install_in_context() which is the point the event is active and
8483 * can use these values.
8484 */
8485 perf_event__header_size(event);
8486 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008487
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08008488 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008489 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008490
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008491 if (move_group)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008492 mutex_unlock(&gctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008493 mutex_unlock(&ctx->mutex);
8494
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008495 put_online_cpus();
8496
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008497 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01008498
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008499 mutex_lock(&current->perf_event_mutex);
8500 list_add_tail(&event->owner_entry, &current->perf_event_list);
8501 mutex_unlock(&current->perf_event_mutex);
8502
Peter Zijlstra8a495422010-05-27 15:47:49 +02008503 /*
8504 * Drop the reference on the group_event after placing the
8505 * new event on the sibling_list. This ensures destruction
8506 * of the group leader will find the pointer to itself in
8507 * perf_group_detach().
8508 */
Al Viro2903ff02012-08-28 12:52:22 -04008509 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04008510 fd_install(event_fd, event_file);
8511 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008512
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008513err_locked:
8514 if (move_group)
8515 mutex_unlock(&gctx->mutex);
8516 mutex_unlock(&ctx->mutex);
8517/* err_file: */
8518 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008519err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008520 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04008521 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008522err_alloc:
8523 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008524err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008525 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008526err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02008527 if (task)
8528 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008529err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04008530 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04008531err_fd:
8532 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008533 return err;
8534}
8535
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008536/**
8537 * perf_event_create_kernel_counter
8538 *
8539 * @attr: attributes of the counter to create
8540 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07008541 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008542 */
8543struct perf_event *
8544perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07008545 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008546 perf_overflow_handler_t overflow_handler,
8547 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008548{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008549 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008550 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008551 int err;
8552
8553 /*
8554 * Get the target context (task or percpu):
8555 */
8556
Avi Kivity4dc0da82011-06-29 18:42:35 +03008557 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00008558 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008559 if (IS_ERR(event)) {
8560 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008561 goto err;
8562 }
8563
Jiri Olsaf8697762014-08-01 14:33:01 +02008564 /* Mark owner so we could distinguish it from user events. */
8565 event->owner = EVENT_OWNER_KERNEL;
8566
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008567 account_event(event);
8568
Yan, Zheng4af57ef282014-11-04 21:56:01 -05008569 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008570 if (IS_ERR(ctx)) {
8571 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008572 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008573 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008574
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008575 WARN_ON_ONCE(ctx->parent_ctx);
8576 mutex_lock(&ctx->mutex);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008577 if (!exclusive_event_installable(event, ctx)) {
8578 mutex_unlock(&ctx->mutex);
8579 perf_unpin_context(ctx);
8580 put_ctx(ctx);
8581 err = -EBUSY;
8582 goto err_free;
8583 }
8584
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008585 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008586 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008587 mutex_unlock(&ctx->mutex);
8588
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008589 return event;
8590
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008591err_free:
8592 free_event(event);
8593err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008594 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008595}
8596EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
8597
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008598void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
8599{
8600 struct perf_event_context *src_ctx;
8601 struct perf_event_context *dst_ctx;
8602 struct perf_event *event, *tmp;
8603 LIST_HEAD(events);
8604
8605 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
8606 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
8607
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008608 /*
8609 * See perf_event_ctx_lock() for comments on the details
8610 * of swizzling perf_event::ctx.
8611 */
8612 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008613 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
8614 event_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02008615 perf_remove_from_context(event, false);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02008616 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008617 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02008618 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008619 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008620
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008621 /*
8622 * Wait for the events to quiesce before re-instating them.
8623 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008624 synchronize_rcu();
8625
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008626 /*
8627 * Re-instate events in 2 passes.
8628 *
8629 * Skip over group leaders and only install siblings on this first
8630 * pass, siblings will not get enabled without a leader, however a
8631 * leader will enable its siblings, even if those are still on the old
8632 * context.
8633 */
8634 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
8635 if (event->group_leader == event)
8636 continue;
8637
8638 list_del(&event->migrate_entry);
8639 if (event->state >= PERF_EVENT_STATE_OFF)
8640 event->state = PERF_EVENT_STATE_INACTIVE;
8641 account_event_cpu(event, dst_cpu);
8642 perf_install_in_context(dst_ctx, event, dst_cpu);
8643 get_ctx(dst_ctx);
8644 }
8645
8646 /*
8647 * Once all the siblings are setup properly, install the group leaders
8648 * to make it go.
8649 */
Peter Zijlstra98861672013-10-03 16:02:23 +02008650 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
8651 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008652 if (event->state >= PERF_EVENT_STATE_OFF)
8653 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02008654 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008655 perf_install_in_context(dst_ctx, event, dst_cpu);
8656 get_ctx(dst_ctx);
8657 }
8658 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008659 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008660}
8661EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
8662
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008663static void sync_child_event(struct perf_event *child_event,
8664 struct task_struct *child)
8665{
8666 struct perf_event *parent_event = child_event->parent;
8667 u64 child_val;
8668
8669 if (child_event->attr.inherit_stat)
8670 perf_event_read_event(child_event, child);
8671
Peter Zijlstrab5e58792010-05-21 14:43:12 +02008672 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008673
8674 /*
8675 * Add back the child's count to the parent's count:
8676 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02008677 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008678 atomic64_add(child_event->total_time_enabled,
8679 &parent_event->child_total_time_enabled);
8680 atomic64_add(child_event->total_time_running,
8681 &parent_event->child_total_time_running);
8682
8683 /*
8684 * Remove this event from the parent's list
8685 */
8686 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
8687 mutex_lock(&parent_event->child_mutex);
8688 list_del_init(&child_event->child_list);
8689 mutex_unlock(&parent_event->child_mutex);
8690
8691 /*
Jiri Olsadc633982014-09-12 13:18:26 +02008692 * Make sure user/parent get notified, that we just
8693 * lost one event.
8694 */
8695 perf_event_wakeup(parent_event);
8696
8697 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008698 * Release the parent event, if this was the last
8699 * reference to it.
8700 */
Al Viroa6fa9412012-08-20 14:59:25 +01008701 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008702}
8703
8704static void
8705__perf_event_exit_task(struct perf_event *child_event,
8706 struct perf_event_context *child_ctx,
8707 struct task_struct *child)
8708{
Peter Zijlstra1903d502014-07-15 17:27:27 +02008709 /*
8710 * Do not destroy the 'original' grouping; because of the context
8711 * switch optimization the original events could've ended up in a
8712 * random child task.
8713 *
8714 * If we were to destroy the original group, all group related
8715 * operations would cease to function properly after this random
8716 * child dies.
8717 *
8718 * Do destroy all inherited groups, we don't care about those
8719 * and being thorough is better.
8720 */
8721 perf_remove_from_context(child_event, !!child_event->parent);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008722
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008723 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01008724 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008725 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01008726 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008727 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01008728 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008729 sync_child_event(child_event, child);
8730 free_event(child_event);
Jiri Olsa179033b2014-08-07 11:48:26 -04008731 } else {
8732 child_event->state = PERF_EVENT_STATE_EXIT;
8733 perf_event_wakeup(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008734 }
8735}
8736
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008737static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008738{
Peter Zijlstraebf905f2014-05-29 19:00:24 +02008739 struct perf_event *child_event, *next;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008740 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008741 unsigned long flags;
8742
Jiri Olsa4e93ad62015-11-04 16:00:05 +01008743 if (likely(!child->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008744 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008745
8746 local_irq_save(flags);
8747 /*
8748 * We can't reschedule here because interrupts are disabled,
8749 * and either child is current or it is a task that can't be
8750 * scheduled, so we are now safe from rescheduling changing
8751 * our context.
8752 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01008753 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008754
8755 /*
8756 * Take the context lock here so that if find_get_context is
8757 * reading child->perf_event_ctxp, we wait until it has
8758 * incremented the context's refcount before we do put_ctx below.
8759 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01008760 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02008761 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008762 child->perf_event_ctxp[ctxn] = NULL;
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008763
8764 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008765 * If this context is a clone; unclone it so it can't get
8766 * swapped to another process while we're removing all
8767 * the events from it.
8768 */
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008769 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01008770 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01008771 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008772
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008773 if (clone_ctx)
8774 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008775
8776 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008777 * Report the task dead after unscheduling the events so that we
8778 * won't get any samples after PERF_RECORD_EXIT. We can however still
8779 * get a few PERF_RECORD_READ events.
8780 */
8781 perf_event_task(child, child_ctx, 0);
8782
8783 /*
8784 * We can recurse on the same lock type through:
8785 *
8786 * __perf_event_exit_task()
8787 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01008788 * put_event()
8789 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008790 *
8791 * But since its the parent context it won't be the same instance.
8792 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02008793 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008794
Peter Zijlstraebf905f2014-05-29 19:00:24 +02008795 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008796 __perf_event_exit_task(child_event, child_ctx, child);
8797
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008798 mutex_unlock(&child_ctx->mutex);
8799
8800 put_ctx(child_ctx);
8801}
8802
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008803/*
8804 * When a child task exits, feed back event values to parent events.
8805 */
8806void perf_event_exit_task(struct task_struct *child)
8807{
Peter Zijlstra88821352010-11-09 19:01:43 +01008808 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008809 int ctxn;
8810
Peter Zijlstra88821352010-11-09 19:01:43 +01008811 mutex_lock(&child->perf_event_mutex);
8812 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
8813 owner_entry) {
8814 list_del_init(&event->owner_entry);
8815
8816 /*
8817 * Ensure the list deletion is visible before we clear
8818 * the owner, closes a race against perf_release() where
8819 * we need to serialize on the owner->perf_event_mutex.
8820 */
8821 smp_wmb();
8822 event->owner = NULL;
8823 }
8824 mutex_unlock(&child->perf_event_mutex);
8825
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008826 for_each_task_context_nr(ctxn)
8827 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +01008828
8829 /*
8830 * The perf_event_exit_task_context calls perf_event_task
8831 * with child's task_ctx, which generates EXIT events for
8832 * child contexts and sets child->perf_event_ctxp[] to NULL.
8833 * At this point we need to send EXIT events to cpu contexts.
8834 */
8835 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008836}
8837
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008838static void perf_free_event(struct perf_event *event,
8839 struct perf_event_context *ctx)
8840{
8841 struct perf_event *parent = event->parent;
8842
8843 if (WARN_ON_ONCE(!parent))
8844 return;
8845
8846 mutex_lock(&parent->child_mutex);
8847 list_del_init(&event->child_list);
8848 mutex_unlock(&parent->child_mutex);
8849
Al Viroa6fa9412012-08-20 14:59:25 +01008850 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008851
Peter Zijlstra652884f2015-01-23 11:20:10 +01008852 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02008853 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008854 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +01008855 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008856 free_event(event);
8857}
8858
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008859/*
Peter Zijlstra652884f2015-01-23 11:20:10 +01008860 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008861 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +01008862 *
8863 * Not all locks are strictly required, but take them anyway to be nice and
8864 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008865 */
8866void perf_event_free_task(struct task_struct *task)
8867{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008868 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008869 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008870 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008871
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008872 for_each_task_context_nr(ctxn) {
8873 ctx = task->perf_event_ctxp[ctxn];
8874 if (!ctx)
8875 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008876
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008877 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008878again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008879 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
8880 group_entry)
8881 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008882
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008883 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
8884 group_entry)
8885 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008886
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008887 if (!list_empty(&ctx->pinned_groups) ||
8888 !list_empty(&ctx->flexible_groups))
8889 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008890
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008891 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008892
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008893 put_ctx(ctx);
8894 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008895}
8896
Peter Zijlstra4e231c72010-09-09 21:01:59 +02008897void perf_event_delayed_put(struct task_struct *task)
8898{
8899 int ctxn;
8900
8901 for_each_task_context_nr(ctxn)
8902 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
8903}
8904
Kaixu Xiaffe86902015-08-06 07:02:32 +00008905struct perf_event *perf_event_get(unsigned int fd)
8906{
8907 int err;
8908 struct fd f;
8909 struct perf_event *event;
8910
8911 err = perf_fget_light(fd, &f);
8912 if (err)
8913 return ERR_PTR(err);
8914
8915 event = f.file->private_data;
8916 atomic_long_inc(&event->refcount);
8917 fdput(f);
8918
8919 return event;
8920}
8921
8922const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
8923{
8924 if (!event)
8925 return ERR_PTR(-EINVAL);
8926
8927 return &event->attr;
8928}
8929
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008930/*
8931 * inherit a event from parent task to child task:
8932 */
8933static struct perf_event *
8934inherit_event(struct perf_event *parent_event,
8935 struct task_struct *parent,
8936 struct perf_event_context *parent_ctx,
8937 struct task_struct *child,
8938 struct perf_event *group_leader,
8939 struct perf_event_context *child_ctx)
8940{
Jiri Olsa1929def2014-09-12 13:18:27 +02008941 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008942 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02008943 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008944
8945 /*
8946 * Instead of creating recursive hierarchies of events,
8947 * we link inherited events back to the original parent,
8948 * which has a filp for sure, which we use as the reference
8949 * count:
8950 */
8951 if (parent_event->parent)
8952 parent_event = parent_event->parent;
8953
8954 child_event = perf_event_alloc(&parent_event->attr,
8955 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008956 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008957 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +00008958 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008959 if (IS_ERR(child_event))
8960 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01008961
Jiri Olsafadfe7b2014-08-01 14:33:02 +02008962 if (is_orphaned_event(parent_event) ||
8963 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Al Viroa6fa9412012-08-20 14:59:25 +01008964 free_event(child_event);
8965 return NULL;
8966 }
8967
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008968 get_ctx(child_ctx);
8969
8970 /*
8971 * Make the child state follow the state of the parent event,
8972 * not its attr.disabled bit. We hold the parent's mutex,
8973 * so we won't race with perf_event_{en, dis}able_family.
8974 */
Jiri Olsa1929def2014-09-12 13:18:27 +02008975 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008976 child_event->state = PERF_EVENT_STATE_INACTIVE;
8977 else
8978 child_event->state = PERF_EVENT_STATE_OFF;
8979
8980 if (parent_event->attr.freq) {
8981 u64 sample_period = parent_event->hw.sample_period;
8982 struct hw_perf_event *hwc = &child_event->hw;
8983
8984 hwc->sample_period = sample_period;
8985 hwc->last_period = sample_period;
8986
8987 local64_set(&hwc->period_left, sample_period);
8988 }
8989
8990 child_event->ctx = child_ctx;
8991 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03008992 child_event->overflow_handler_context
8993 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008994
8995 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02008996 * Precalculate sample_data sizes
8997 */
8998 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02008999 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02009000
9001 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009002 * Link it up in the child's context:
9003 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02009004 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009005 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02009006 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009007
9008 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009009 * Link this into the parent event's child list
9010 */
9011 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
9012 mutex_lock(&parent_event->child_mutex);
9013 list_add_tail(&child_event->child_list, &parent_event->child_list);
9014 mutex_unlock(&parent_event->child_mutex);
9015
9016 return child_event;
9017}
9018
9019static int inherit_group(struct perf_event *parent_event,
9020 struct task_struct *parent,
9021 struct perf_event_context *parent_ctx,
9022 struct task_struct *child,
9023 struct perf_event_context *child_ctx)
9024{
9025 struct perf_event *leader;
9026 struct perf_event *sub;
9027 struct perf_event *child_ctr;
9028
9029 leader = inherit_event(parent_event, parent, parent_ctx,
9030 child, NULL, child_ctx);
9031 if (IS_ERR(leader))
9032 return PTR_ERR(leader);
9033 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
9034 child_ctr = inherit_event(sub, parent, parent_ctx,
9035 child, leader, child_ctx);
9036 if (IS_ERR(child_ctr))
9037 return PTR_ERR(child_ctr);
9038 }
9039 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009040}
9041
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009042static int
9043inherit_task_group(struct perf_event *event, struct task_struct *parent,
9044 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009045 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009046 int *inherited_all)
9047{
9048 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009049 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009050
9051 if (!event->attr.inherit) {
9052 *inherited_all = 0;
9053 return 0;
9054 }
9055
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009056 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009057 if (!child_ctx) {
9058 /*
9059 * This is executed from the parent task context, so
9060 * inherit events that have been marked for cloning.
9061 * First allocate and initialize a context for the
9062 * child.
9063 */
9064
Jiri Olsa734df5a2013-07-09 17:44:10 +02009065 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009066 if (!child_ctx)
9067 return -ENOMEM;
9068
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009069 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009070 }
9071
9072 ret = inherit_group(event, parent, parent_ctx,
9073 child, child_ctx);
9074
9075 if (ret)
9076 *inherited_all = 0;
9077
9078 return ret;
9079}
9080
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009081/*
9082 * Initialize the perf_event context in task_struct
9083 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02009084static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009085{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009086 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009087 struct perf_event_context *cloned_ctx;
9088 struct perf_event *event;
9089 struct task_struct *parent = current;
9090 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009091 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009092 int ret = 0;
9093
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009094 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009095 return 0;
9096
9097 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009098 * If the parent's context is a clone, pin it so it won't get
9099 * swapped under us.
9100 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009101 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02009102 if (!parent_ctx)
9103 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009104
9105 /*
9106 * No need to check if parent_ctx != NULL here; since we saw
9107 * it non-NULL earlier, the only reason for it to become NULL
9108 * is if we exit, and since we're currently in the middle of
9109 * a fork we can't be exiting at the same time.
9110 */
9111
9112 /*
9113 * Lock the parent list. No need to lock the child - not PID
9114 * hashed yet and not running, so nobody can access it.
9115 */
9116 mutex_lock(&parent_ctx->mutex);
9117
9118 /*
9119 * We dont have to disable NMIs - we are only looking at
9120 * the list, not manipulating it:
9121 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009122 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009123 ret = inherit_task_group(event, parent, parent_ctx,
9124 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009125 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009126 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009127 }
9128
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009129 /*
9130 * We can't hold ctx->lock when iterating the ->flexible_group list due
9131 * to allocations, but we need to prevent rotation because
9132 * rotate_ctx() will change the list from interrupt context.
9133 */
9134 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
9135 parent_ctx->rotate_disable = 1;
9136 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
9137
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009138 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009139 ret = inherit_task_group(event, parent, parent_ctx,
9140 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009141 if (ret)
9142 break;
9143 }
9144
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009145 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
9146 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009147
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009148 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009149
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01009150 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009151 /*
9152 * Mark the child context as a clone of the parent
9153 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009154 *
9155 * Note that if the parent is a clone, the holding of
9156 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009157 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009158 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009159 if (cloned_ctx) {
9160 child_ctx->parent_ctx = cloned_ctx;
9161 child_ctx->parent_gen = parent_ctx->parent_gen;
9162 } else {
9163 child_ctx->parent_ctx = parent_ctx;
9164 child_ctx->parent_gen = parent_ctx->generation;
9165 }
9166 get_ctx(child_ctx->parent_ctx);
9167 }
9168
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009169 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009170 mutex_unlock(&parent_ctx->mutex);
9171
9172 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009173 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009174
9175 return ret;
9176}
9177
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009178/*
9179 * Initialize the perf_event context in task_struct
9180 */
9181int perf_event_init_task(struct task_struct *child)
9182{
9183 int ctxn, ret;
9184
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01009185 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
9186 mutex_init(&child->perf_event_mutex);
9187 INIT_LIST_HEAD(&child->perf_event_list);
9188
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009189 for_each_task_context_nr(ctxn) {
9190 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07009191 if (ret) {
9192 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009193 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07009194 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009195 }
9196
9197 return 0;
9198}
9199
Paul Mackerras220b1402010-03-10 20:45:52 +11009200static void __init perf_event_init_all_cpus(void)
9201{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009202 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11009203 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11009204
9205 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009206 swhash = &per_cpu(swevent_htable, cpu);
9207 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00009208 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11009209 }
9210}
9211
Paul Gortmaker0db06282013-06-19 14:53:51 -04009212static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009213{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009214 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009215
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009216 mutex_lock(&swhash->hlist_mutex);
Linus Torvalds4536e4d2011-11-03 07:44:04 -07009217 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009218 struct swevent_hlist *hlist;
9219
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009220 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
9221 WARN_ON(!hlist);
9222 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009223 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009224 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009225}
9226
Dave Young2965faa2015-09-09 15:38:55 -07009227#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009228static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009229{
Mark Rutland226424e2014-11-05 16:11:44 +00009230 struct remove_event re = { .detach_group = true };
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009231 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009232
Peter Zijlstrae3703f82014-02-24 12:06:12 +01009233 rcu_read_lock();
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02009234 list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
9235 __perf_remove_from_context(&re);
Peter Zijlstrae3703f82014-02-24 12:06:12 +01009236 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009237}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009238
9239static void perf_event_exit_cpu_context(int cpu)
9240{
9241 struct perf_event_context *ctx;
9242 struct pmu *pmu;
9243 int idx;
9244
9245 idx = srcu_read_lock(&pmus_srcu);
9246 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02009247 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009248
9249 mutex_lock(&ctx->mutex);
9250 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
9251 mutex_unlock(&ctx->mutex);
9252 }
9253 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009254}
9255
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009256static void perf_event_exit_cpu(int cpu)
9257{
Peter Zijlstrae3703f82014-02-24 12:06:12 +01009258 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009259}
9260#else
9261static inline void perf_event_exit_cpu(int cpu) { }
9262#endif
9263
Peter Zijlstrac2774432010-12-08 15:29:02 +01009264static int
9265perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
9266{
9267 int cpu;
9268
9269 for_each_online_cpu(cpu)
9270 perf_event_exit_cpu(cpu);
9271
9272 return NOTIFY_OK;
9273}
9274
9275/*
9276 * Run the perf reboot notifier at the very last possible moment so that
9277 * the generic watchdog code runs as long as possible.
9278 */
9279static struct notifier_block perf_reboot_notifier = {
9280 .notifier_call = perf_reboot,
9281 .priority = INT_MIN,
9282};
9283
Paul Gortmaker0db06282013-06-19 14:53:51 -04009284static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009285perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
9286{
9287 unsigned int cpu = (long)hcpu;
9288
Linus Torvalds4536e4d2011-11-03 07:44:04 -07009289 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009290
9291 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02009292 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009293 perf_event_init_cpu(cpu);
9294 break;
9295
Peter Zijlstra5e116372010-06-11 13:35:08 +02009296 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009297 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009298 perf_event_exit_cpu(cpu);
9299 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009300 default:
9301 break;
9302 }
9303
9304 return NOTIFY_OK;
9305}
9306
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009307void __init perf_event_init(void)
9308{
Jason Wessel3c502e72010-11-04 17:33:01 -05009309 int ret;
9310
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009311 idr_init(&pmu_idr);
9312
Paul Mackerras220b1402010-03-10 20:45:52 +11009313 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009314 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009315 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
9316 perf_pmu_register(&perf_cpu_clock, NULL, -1);
9317 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009318 perf_tp_register();
9319 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01009320 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05009321
9322 ret = init_hw_breakpoint();
9323 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02009324
9325 /* do not patch jump label more than once per second */
9326 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01009327
9328 /*
9329 * Build time assertion that we keep the data_head at the intended
9330 * location. IOW, validation we got the __reserved[] size right.
9331 */
9332 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
9333 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009334}
Peter Zijlstraabe43402010-11-17 23:17:37 +01009335
Cody P Schaferfd979c02015-01-30 13:45:57 -08009336ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
9337 char *page)
9338{
9339 struct perf_pmu_events_attr *pmu_attr =
9340 container_of(attr, struct perf_pmu_events_attr, attr);
9341
9342 if (pmu_attr->event_str)
9343 return sprintf(page, "%s\n", pmu_attr->event_str);
9344
9345 return 0;
9346}
9347
Peter Zijlstraabe43402010-11-17 23:17:37 +01009348static int __init perf_event_sysfs_init(void)
9349{
9350 struct pmu *pmu;
9351 int ret;
9352
9353 mutex_lock(&pmus_lock);
9354
9355 ret = bus_register(&pmu_bus);
9356 if (ret)
9357 goto unlock;
9358
9359 list_for_each_entry(pmu, &pmus, entry) {
9360 if (!pmu->name || pmu->type < 0)
9361 continue;
9362
9363 ret = pmu_dev_alloc(pmu);
9364 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
9365 }
9366 pmu_bus_running = 1;
9367 ret = 0;
9368
9369unlock:
9370 mutex_unlock(&pmus_lock);
9371
9372 return ret;
9373}
9374device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009375
9376#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04009377static struct cgroup_subsys_state *
9378perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009379{
9380 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02009381
Li Zefan1b15d052011-03-03 14:26:06 +08009382 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009383 if (!jc)
9384 return ERR_PTR(-ENOMEM);
9385
Stephane Eraniane5d13672011-02-14 11:20:01 +02009386 jc->info = alloc_percpu(struct perf_cgroup_info);
9387 if (!jc->info) {
9388 kfree(jc);
9389 return ERR_PTR(-ENOMEM);
9390 }
9391
Stephane Eraniane5d13672011-02-14 11:20:01 +02009392 return &jc->css;
9393}
9394
Tejun Heoeb954192013-08-08 20:11:23 -04009395static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009396{
Tejun Heoeb954192013-08-08 20:11:23 -04009397 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
9398
Stephane Eraniane5d13672011-02-14 11:20:01 +02009399 free_percpu(jc->info);
9400 kfree(jc);
9401}
9402
9403static int __perf_cgroup_move(void *info)
9404{
9405 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +01009406 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +02009407 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +01009408 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +02009409 return 0;
9410}
9411
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009412static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009413{
Tejun Heobb9d97b2011-12-12 18:12:21 -08009414 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009415 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -08009416
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009417 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08009418 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009419}
9420
Tejun Heo073219e2014-02-08 10:36:58 -05009421struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08009422 .css_alloc = perf_cgroup_css_alloc,
9423 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -08009424 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02009425};
9426#endif /* CONFIG_CGROUP_PERF */