blob: 64698fbfad9f8bd4feac74da2b1653f1f73f6fce [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
Peter Zijlstra272325c2015-04-15 11:41:58 +020052typedef int (*remote_function_f)(void *);
53
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010054struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020055 struct task_struct *p;
Peter Zijlstra272325c2015-04-15 11:41:58 +020056 remote_function_f func;
Ingo Molnare7e7ee22011-05-04 08:42:29 +020057 void *info;
58 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010059};
60
61static void remote_function(void *data)
62{
63 struct remote_function_call *tfc = data;
64 struct task_struct *p = tfc->p;
65
66 if (p) {
67 tfc->ret = -EAGAIN;
68 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
69 return;
70 }
71
72 tfc->ret = tfc->func(tfc->info);
73}
74
75/**
76 * task_function_call - call a function on the cpu on which a task runs
77 * @p: the task to evaluate
78 * @func: the function to be called
79 * @info: the function call argument
80 *
81 * Calls the function @func when the task is currently running. This might
82 * be on the current CPU, which just calls the function directly
83 *
84 * returns: @func return value, or
85 * -ESRCH - when the process isn't running
86 * -EAGAIN - when the process moved away
87 */
88static int
Peter Zijlstra272325c2015-04-15 11:41:58 +020089task_function_call(struct task_struct *p, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010090{
91 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020092 .p = p,
93 .func = func,
94 .info = info,
95 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010096 };
97
98 if (task_curr(p))
99 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
100
101 return data.ret;
102}
103
104/**
105 * cpu_function_call - call a function on the cpu
106 * @func: the function to be called
107 * @info: the function call argument
108 *
109 * Calls the function @func on the remote cpu.
110 *
111 * returns: @func return value or -ENXIO when the cpu is offline
112 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200113static int cpu_function_call(int cpu, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100114{
115 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200116 .p = NULL,
117 .func = func,
118 .info = info,
119 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100120 };
121
122 smp_call_function_single(cpu, remote_function, &data, 1);
123
124 return data.ret;
125}
126
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100127static inline struct perf_cpu_context *
128__get_cpu_context(struct perf_event_context *ctx)
129{
130 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
131}
132
133static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
134 struct perf_event_context *ctx)
135{
136 raw_spin_lock(&cpuctx->ctx.lock);
137 if (ctx)
138 raw_spin_lock(&ctx->lock);
139}
140
141static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
142 struct perf_event_context *ctx)
143{
144 if (ctx)
145 raw_spin_unlock(&ctx->lock);
146 raw_spin_unlock(&cpuctx->ctx.lock);
147}
148
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100149#define TASK_TOMBSTONE ((void *)-1L)
150
151static bool is_kernel_event(struct perf_event *event)
152{
Peter Zijlstraf47c02c2016-01-26 12:30:14 +0100153 return READ_ONCE(event->owner) == TASK_TOMBSTONE;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100154}
155
Peter Zijlstra39a43642016-01-11 12:46:35 +0100156/*
157 * On task ctx scheduling...
158 *
159 * When !ctx->nr_events a task context will not be scheduled. This means
160 * we can disable the scheduler hooks (for performance) without leaving
161 * pending task ctx state.
162 *
163 * This however results in two special cases:
164 *
165 * - removing the last event from a task ctx; this is relatively straight
166 * forward and is done in __perf_remove_from_context.
167 *
168 * - adding the first event to a task ctx; this is tricky because we cannot
169 * rely on ctx->is_active and therefore cannot use event_function_call().
170 * See perf_install_in_context().
171 *
172 * This is because we need a ctx->lock serialized variable (ctx->is_active)
173 * to reliably determine if a particular task/context is scheduled in. The
174 * task_curr() use in task_function_call() is racy in that a remote context
175 * switch is not a single atomic operation.
176 *
177 * As is, the situation is 'safe' because we set rq->curr before we do the
178 * actual context switch. This means that task_curr() will fail early, but
179 * we'll continue spinning on ctx->is_active until we've passed
180 * perf_event_task_sched_out().
181 *
182 * Without this ctx->lock serialized variable we could have race where we find
183 * the task (and hence the context) would not be active while in fact they are.
184 *
185 * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
186 */
187
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100188typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *,
189 struct perf_event_context *, void *);
190
191struct event_function_struct {
192 struct perf_event *event;
193 event_f func;
194 void *data;
195};
196
197static int event_function(void *info)
198{
199 struct event_function_struct *efs = info;
200 struct perf_event *event = efs->event;
201 struct perf_event_context *ctx = event->ctx;
202 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
203 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100204 int ret = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100205
206 WARN_ON_ONCE(!irqs_disabled());
207
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100208 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100209 /*
210 * Since we do the IPI call without holding ctx->lock things can have
211 * changed, double check we hit the task we set out to hit.
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100212 */
213 if (ctx->task) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100214 if (ctx->task != current) {
215 ret = -EAGAIN;
216 goto unlock;
217 }
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100218
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100219 /*
220 * We only use event_function_call() on established contexts,
221 * and event_function() is only ever called when active (or
222 * rather, we'll have bailed in task_function_call() or the
223 * above ctx->task != current test), therefore we must have
224 * ctx->is_active here.
225 */
226 WARN_ON_ONCE(!ctx->is_active);
227 /*
228 * And since we have ctx->is_active, cpuctx->task_ctx must
229 * match.
230 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100231 WARN_ON_ONCE(task_ctx != ctx);
232 } else {
233 WARN_ON_ONCE(&cpuctx->ctx != ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100234 }
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100235
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100236 efs->func(event, cpuctx, ctx, efs->data);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100237unlock:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100238 perf_ctx_unlock(cpuctx, task_ctx);
239
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100240 return ret;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100241}
242
243static void event_function_local(struct perf_event *event, event_f func, void *data)
244{
245 struct event_function_struct efs = {
246 .event = event,
247 .func = func,
248 .data = data,
249 };
250
251 int ret = event_function(&efs);
252 WARN_ON_ONCE(ret);
253}
254
255static void event_function_call(struct perf_event *event, event_f func, void *data)
Peter Zijlstra00179602015-11-30 16:26:35 +0100256{
257 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100258 struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100259 struct event_function_struct efs = {
260 .event = event,
261 .func = func,
262 .data = data,
263 };
Peter Zijlstra00179602015-11-30 16:26:35 +0100264
Peter Zijlstrac97f4732016-01-14 10:51:03 +0100265 if (!event->parent) {
266 /*
267 * If this is a !child event, we must hold ctx::mutex to
268 * stabilize the the event->ctx relation. See
269 * perf_event_ctx_lock().
270 */
271 lockdep_assert_held(&ctx->mutex);
272 }
Peter Zijlstra00179602015-11-30 16:26:35 +0100273
274 if (!task) {
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100275 cpu_function_call(event->cpu, event_function, &efs);
Peter Zijlstra00179602015-11-30 16:26:35 +0100276 return;
277 }
278
279again:
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100280 if (task == TASK_TOMBSTONE)
281 return;
282
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100283 if (!task_function_call(task, event_function, &efs))
Peter Zijlstra00179602015-11-30 16:26:35 +0100284 return;
285
286 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100287 /*
288 * Reload the task pointer, it might have been changed by
289 * a concurrent perf_event_context_sched_out().
290 */
291 task = ctx->task;
292 if (task != TASK_TOMBSTONE) {
293 if (ctx->is_active) {
294 raw_spin_unlock_irq(&ctx->lock);
295 goto again;
296 }
297 func(event, NULL, ctx, data);
Peter Zijlstra00179602015-11-30 16:26:35 +0100298 }
Peter Zijlstra00179602015-11-30 16:26:35 +0100299 raw_spin_unlock_irq(&ctx->lock);
300}
301
Stephane Eraniane5d13672011-02-14 11:20:01 +0200302#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
303 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100304 PERF_FLAG_PID_CGROUP |\
305 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200306
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100307/*
308 * branch priv levels that need permission checks
309 */
310#define PERF_SAMPLE_BRANCH_PERM_PLM \
311 (PERF_SAMPLE_BRANCH_KERNEL |\
312 PERF_SAMPLE_BRANCH_HV)
313
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200314enum event_type_t {
315 EVENT_FLEXIBLE = 0x1,
316 EVENT_PINNED = 0x2,
317 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
318};
319
Stephane Eraniane5d13672011-02-14 11:20:01 +0200320/*
321 * perf_sched_events : >0 events exist
322 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
323 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100324struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200325static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Yan, Zhengba532502014-11-04 21:55:58 -0500326static DEFINE_PER_CPU(int, perf_sched_cb_usages);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200327
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200328static atomic_t nr_mmap_events __read_mostly;
329static atomic_t nr_comm_events __read_mostly;
330static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200331static atomic_t nr_freq_events __read_mostly;
Adrian Hunter45ac1402015-07-21 12:44:02 +0300332static atomic_t nr_switch_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200333
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200334static LIST_HEAD(pmus);
335static DEFINE_MUTEX(pmus_lock);
336static struct srcu_struct pmus_srcu;
337
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200338/*
339 * perf event paranoia level:
340 * -1 - not paranoid at all
341 * 0 - disallow raw tracepoint access for unpriv
342 * 1 - disallow cpu events for unpriv
343 * 2 - disallow kernel profiling for unpriv
344 */
345int sysctl_perf_event_paranoid __read_mostly = 1;
346
Frederic Weisbecker20443382011-03-31 03:33:29 +0200347/* Minimum for 512 kiB + 1 user control page */
348int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200349
350/*
351 * max perf event sample rate
352 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700353#define DEFAULT_MAX_SAMPLE_RATE 100000
354#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
355#define DEFAULT_CPU_TIME_MAX_PERCENT 25
356
357int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
358
359static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
360static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
361
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200362static int perf_sample_allowed_ns __read_mostly =
363 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700364
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800365static void update_perf_cpu_limits(void)
Dave Hansen14c63f12013-06-21 08:51:36 -0700366{
367 u64 tmp = perf_sample_period_ns;
368
369 tmp *= sysctl_perf_cpu_time_max_percent;
Stephane Eraniane5302922013-07-05 00:30:11 +0200370 do_div(tmp, 100);
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200371 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
Dave Hansen14c63f12013-06-21 08:51:36 -0700372}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100373
Stephane Eranian9e630202013-04-03 14:21:33 +0200374static int perf_rotate_context(struct perf_cpu_context *cpuctx);
375
Peter Zijlstra163ec432011-02-16 11:22:34 +0100376int perf_proc_update_handler(struct ctl_table *table, int write,
377 void __user *buffer, size_t *lenp,
378 loff_t *ppos)
379{
Knut Petersen723478c2013-09-25 14:29:37 +0200380 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100381
382 if (ret || !write)
383 return ret;
384
385 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700386 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
387 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100388
389 return 0;
390}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200391
Dave Hansen14c63f12013-06-21 08:51:36 -0700392int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
393
394int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
395 void __user *buffer, size_t *lenp,
396 loff_t *ppos)
397{
398 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
399
400 if (ret || !write)
401 return ret;
402
403 update_perf_cpu_limits();
404
405 return 0;
406}
407
408/*
409 * perf samples are done in some very critical code paths (NMIs).
410 * If they take too much CPU time, the system can lock up and not
411 * get any real work done. This will drop the sample rate when
412 * we detect that events are taking too long.
413 */
414#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200415static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700416
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100417static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700418{
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100419 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Dave Hansen14c63f12013-06-21 08:51:36 -0700420 u64 avg_local_sample_len;
Stephane Eraniane5302922013-07-05 00:30:11 +0200421 u64 local_samples_len;
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100422
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500423 local_samples_len = __this_cpu_read(running_sample_length);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100424 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
425
426 printk_ratelimited(KERN_WARNING
427 "perf interrupt took too long (%lld > %lld), lowering "
428 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100429 avg_local_sample_len, allowed_ns >> 1,
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100430 sysctl_perf_event_sample_rate);
431}
432
433static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
434
435void perf_sample_event_took(u64 sample_len_ns)
436{
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200437 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100438 u64 avg_local_sample_len;
439 u64 local_samples_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700440
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200441 if (allowed_ns == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700442 return;
443
444 /* decay the counter by 1 average sample */
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500445 local_samples_len = __this_cpu_read(running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700446 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
447 local_samples_len += sample_len_ns;
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500448 __this_cpu_write(running_sample_length, local_samples_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700449
450 /*
451 * note: this will be biased artifically low until we have
452 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
453 * from having to maintain a count.
454 */
455 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
456
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200457 if (avg_local_sample_len <= allowed_ns)
Dave Hansen14c63f12013-06-21 08:51:36 -0700458 return;
459
460 if (max_samples_per_tick <= 1)
461 return;
462
463 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
464 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
465 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
466
Dave Hansen14c63f12013-06-21 08:51:36 -0700467 update_perf_cpu_limits();
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100468
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100469 if (!irq_work_queue(&perf_duration_work)) {
470 early_printk("perf interrupt took too long (%lld > %lld), lowering "
471 "kernel.perf_event_max_sample_rate to %d\n",
472 avg_local_sample_len, allowed_ns >> 1,
473 sysctl_perf_event_sample_rate);
474 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700475}
476
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200477static atomic64_t perf_event_id;
478
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200479static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
480 enum event_type_t event_type);
481
482static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200483 enum event_type_t event_type,
484 struct task_struct *task);
485
486static void update_context_time(struct perf_event_context *ctx);
487static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200488
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200489void __weak perf_event_print_debug(void) { }
490
Matt Fleming84c79912010-10-03 21:41:13 +0100491extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200492{
Matt Fleming84c79912010-10-03 21:41:13 +0100493 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200494}
495
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200496static inline u64 perf_clock(void)
497{
498 return local_clock();
499}
500
Peter Zijlstra34f43922015-02-20 14:05:38 +0100501static inline u64 perf_event_clock(struct perf_event *event)
502{
503 return event->clock();
504}
505
Stephane Eraniane5d13672011-02-14 11:20:01 +0200506#ifdef CONFIG_CGROUP_PERF
507
Stephane Eraniane5d13672011-02-14 11:20:01 +0200508static inline bool
509perf_cgroup_match(struct perf_event *event)
510{
511 struct perf_event_context *ctx = event->ctx;
512 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
513
Tejun Heoef824fa2013-04-08 19:00:38 -0700514 /* @event doesn't care about cgroup */
515 if (!event->cgrp)
516 return true;
517
518 /* wants specific cgroup scope but @cpuctx isn't associated with any */
519 if (!cpuctx->cgrp)
520 return false;
521
522 /*
523 * Cgroup scoping is recursive. An event enabled for a cgroup is
524 * also enabled for all its descendant cgroups. If @cpuctx's
525 * cgroup is a descendant of @event's (the test covers identity
526 * case), it's a match.
527 */
528 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
529 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200530}
531
Stephane Eraniane5d13672011-02-14 11:20:01 +0200532static inline void perf_detach_cgroup(struct perf_event *event)
533{
Zefan Li4e2ba652014-09-19 16:53:14 +0800534 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200535 event->cgrp = NULL;
536}
537
538static inline int is_cgroup_event(struct perf_event *event)
539{
540 return event->cgrp != NULL;
541}
542
543static inline u64 perf_cgroup_event_time(struct perf_event *event)
544{
545 struct perf_cgroup_info *t;
546
547 t = per_cpu_ptr(event->cgrp->info, event->cpu);
548 return t->time;
549}
550
551static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
552{
553 struct perf_cgroup_info *info;
554 u64 now;
555
556 now = perf_clock();
557
558 info = this_cpu_ptr(cgrp->info);
559
560 info->time += now - info->timestamp;
561 info->timestamp = now;
562}
563
564static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
565{
566 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
567 if (cgrp_out)
568 __update_cgrp_time(cgrp_out);
569}
570
571static inline void update_cgrp_time_from_event(struct perf_event *event)
572{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200573 struct perf_cgroup *cgrp;
574
Stephane Eraniane5d13672011-02-14 11:20:01 +0200575 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200576 * ensure we access cgroup data only when needed and
577 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200578 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200579 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200580 return;
581
Stephane Eranian614e4c42015-11-12 11:00:04 +0100582 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200583 /*
584 * Do not update time when cgroup is not active
585 */
586 if (cgrp == event->cgrp)
587 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200588}
589
590static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200591perf_cgroup_set_timestamp(struct task_struct *task,
592 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200593{
594 struct perf_cgroup *cgrp;
595 struct perf_cgroup_info *info;
596
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200597 /*
598 * ctx->lock held by caller
599 * ensure we do not access cgroup data
600 * unless we have the cgroup pinned (css_get)
601 */
602 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200603 return;
604
Stephane Eranian614e4c42015-11-12 11:00:04 +0100605 cgrp = perf_cgroup_from_task(task, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200606 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200607 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200608}
609
610#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
611#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
612
613/*
614 * reschedule events based on the cgroup constraint of task.
615 *
616 * mode SWOUT : schedule out everything
617 * mode SWIN : schedule in based on cgroup for next
618 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800619static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200620{
621 struct perf_cpu_context *cpuctx;
622 struct pmu *pmu;
623 unsigned long flags;
624
625 /*
626 * disable interrupts to avoid geting nr_cgroup
627 * changes via __perf_event_disable(). Also
628 * avoids preemption.
629 */
630 local_irq_save(flags);
631
632 /*
633 * we reschedule only in the presence of cgroup
634 * constrained events.
635 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200636
637 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200638 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200639 if (cpuctx->unique_pmu != pmu)
640 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200641
Stephane Eraniane5d13672011-02-14 11:20:01 +0200642 /*
643 * perf_cgroup_events says at least one
644 * context on this CPU has cgroup events.
645 *
646 * ctx->nr_cgroups reports the number of cgroup
647 * events for a context.
648 */
649 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200650 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
651 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200652
653 if (mode & PERF_CGROUP_SWOUT) {
654 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
655 /*
656 * must not be done before ctxswout due
657 * to event_filter_match() in event_sched_out()
658 */
659 cpuctx->cgrp = NULL;
660 }
661
662 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200663 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200664 /*
665 * set cgrp before ctxsw in to allow
666 * event_filter_match() to not have to pass
667 * task around
Stephane Eranian614e4c42015-11-12 11:00:04 +0100668 * we pass the cpuctx->ctx to perf_cgroup_from_task()
669 * because cgorup events are only per-cpu
Stephane Eraniane5d13672011-02-14 11:20:01 +0200670 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100671 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200672 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
673 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200674 perf_pmu_enable(cpuctx->ctx.pmu);
675 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200676 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200677 }
678
Stephane Eraniane5d13672011-02-14 11:20:01 +0200679 local_irq_restore(flags);
680}
681
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200682static inline void perf_cgroup_sched_out(struct task_struct *task,
683 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200684{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200685 struct perf_cgroup *cgrp1;
686 struct perf_cgroup *cgrp2 = NULL;
687
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100688 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200689 /*
690 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100691 * we do not need to pass the ctx here because we know
692 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200693 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100694 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100695 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200696
697 /*
698 * only schedule out current cgroup events if we know
699 * that we are switching to a different cgroup. Otherwise,
700 * do no touch the cgroup events.
701 */
702 if (cgrp1 != cgrp2)
703 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100704
705 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200706}
707
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200708static inline void perf_cgroup_sched_in(struct task_struct *prev,
709 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200710{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200711 struct perf_cgroup *cgrp1;
712 struct perf_cgroup *cgrp2 = NULL;
713
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100714 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200715 /*
716 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100717 * we do not need to pass the ctx here because we know
718 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200719 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100720 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100721 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200722
723 /*
724 * only need to schedule in cgroup events if we are changing
725 * cgroup during ctxsw. Cgroup events were not scheduled
726 * out of ctxsw out if that was not the case.
727 */
728 if (cgrp1 != cgrp2)
729 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100730
731 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200732}
733
734static inline int perf_cgroup_connect(int fd, struct perf_event *event,
735 struct perf_event_attr *attr,
736 struct perf_event *group_leader)
737{
738 struct perf_cgroup *cgrp;
739 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400740 struct fd f = fdget(fd);
741 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200742
Al Viro2903ff02012-08-28 12:52:22 -0400743 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200744 return -EBADF;
745
Al Virob5830432014-10-31 01:22:04 -0400746 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400747 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800748 if (IS_ERR(css)) {
749 ret = PTR_ERR(css);
750 goto out;
751 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200752
753 cgrp = container_of(css, struct perf_cgroup, css);
754 event->cgrp = cgrp;
755
756 /*
757 * all events in a group must monitor
758 * the same cgroup because a task belongs
759 * to only one perf cgroup at a time
760 */
761 if (group_leader && group_leader->cgrp != cgrp) {
762 perf_detach_cgroup(event);
763 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200764 }
Li Zefan3db272c2011-03-03 14:25:37 +0800765out:
Al Viro2903ff02012-08-28 12:52:22 -0400766 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200767 return ret;
768}
769
770static inline void
771perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
772{
773 struct perf_cgroup_info *t;
774 t = per_cpu_ptr(event->cgrp->info, event->cpu);
775 event->shadow_ctx_time = now - t->timestamp;
776}
777
778static inline void
779perf_cgroup_defer_enabled(struct perf_event *event)
780{
781 /*
782 * when the current task's perf cgroup does not match
783 * the event's, we need to remember to call the
784 * perf_mark_enable() function the first time a task with
785 * a matching perf cgroup is scheduled in.
786 */
787 if (is_cgroup_event(event) && !perf_cgroup_match(event))
788 event->cgrp_defer_enabled = 1;
789}
790
791static inline void
792perf_cgroup_mark_enabled(struct perf_event *event,
793 struct perf_event_context *ctx)
794{
795 struct perf_event *sub;
796 u64 tstamp = perf_event_time(event);
797
798 if (!event->cgrp_defer_enabled)
799 return;
800
801 event->cgrp_defer_enabled = 0;
802
803 event->tstamp_enabled = tstamp - event->total_time_enabled;
804 list_for_each_entry(sub, &event->sibling_list, group_entry) {
805 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
806 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
807 sub->cgrp_defer_enabled = 0;
808 }
809 }
810}
811#else /* !CONFIG_CGROUP_PERF */
812
813static inline bool
814perf_cgroup_match(struct perf_event *event)
815{
816 return true;
817}
818
819static inline void perf_detach_cgroup(struct perf_event *event)
820{}
821
822static inline int is_cgroup_event(struct perf_event *event)
823{
824 return 0;
825}
826
827static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
828{
829 return 0;
830}
831
832static inline void update_cgrp_time_from_event(struct perf_event *event)
833{
834}
835
836static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
837{
838}
839
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200840static inline void perf_cgroup_sched_out(struct task_struct *task,
841 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200842{
843}
844
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200845static inline void perf_cgroup_sched_in(struct task_struct *prev,
846 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200847{
848}
849
850static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
851 struct perf_event_attr *attr,
852 struct perf_event *group_leader)
853{
854 return -EINVAL;
855}
856
857static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200858perf_cgroup_set_timestamp(struct task_struct *task,
859 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200860{
861}
862
863void
864perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
865{
866}
867
868static inline void
869perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
870{
871}
872
873static inline u64 perf_cgroup_event_time(struct perf_event *event)
874{
875 return 0;
876}
877
878static inline void
879perf_cgroup_defer_enabled(struct perf_event *event)
880{
881}
882
883static inline void
884perf_cgroup_mark_enabled(struct perf_event *event,
885 struct perf_event_context *ctx)
886{
887}
888#endif
889
Stephane Eranian9e630202013-04-03 14:21:33 +0200890/*
891 * set default to be dependent on timer tick just
892 * like original code
893 */
894#define PERF_CPU_HRTIMER (1000 / HZ)
895/*
896 * function must be called with interrupts disbled
897 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200898static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +0200899{
900 struct perf_cpu_context *cpuctx;
Stephane Eranian9e630202013-04-03 14:21:33 +0200901 int rotations = 0;
902
903 WARN_ON(!irqs_disabled());
904
905 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200906 rotations = perf_rotate_context(cpuctx);
907
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200908 raw_spin_lock(&cpuctx->hrtimer_lock);
909 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +0200910 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200911 else
912 cpuctx->hrtimer_active = 0;
913 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +0200914
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200915 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +0200916}
917
Peter Zijlstra272325c2015-04-15 11:41:58 +0200918static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +0200919{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200920 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200921 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +0200922 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +0200923
924 /* no multiplexing needed for SW PMU */
925 if (pmu->task_ctx_nr == perf_sw_context)
926 return;
927
Stephane Eranian62b85632013-04-03 14:21:34 +0200928 /*
929 * check default is sane, if not set then force to
930 * default interval (1/tick)
931 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200932 interval = pmu->hrtimer_interval_ms;
933 if (interval < 1)
934 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +0200935
Peter Zijlstra272325c2015-04-15 11:41:58 +0200936 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +0200937
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200938 raw_spin_lock_init(&cpuctx->hrtimer_lock);
939 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra272325c2015-04-15 11:41:58 +0200940 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +0200941}
942
Peter Zijlstra272325c2015-04-15 11:41:58 +0200943static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +0200944{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200945 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200946 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200947 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +0200948
949 /* not for SW PMU */
950 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +0200951 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +0200952
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200953 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
954 if (!cpuctx->hrtimer_active) {
955 cpuctx->hrtimer_active = 1;
956 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
957 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
958 }
959 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +0200960
Peter Zijlstra272325c2015-04-15 11:41:58 +0200961 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +0200962}
963
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200964void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200965{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200966 int *count = this_cpu_ptr(pmu->pmu_disable_count);
967 if (!(*count)++)
968 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200969}
970
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200971void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200972{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200973 int *count = this_cpu_ptr(pmu->pmu_disable_count);
974 if (!--(*count))
975 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200976}
977
Mark Rutland2fde4f92015-01-07 15:01:54 +0000978static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200979
980/*
Mark Rutland2fde4f92015-01-07 15:01:54 +0000981 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
982 * perf_event_task_tick() are fully serialized because they're strictly cpu
983 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
984 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200985 */
Mark Rutland2fde4f92015-01-07 15:01:54 +0000986static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200987{
Mark Rutland2fde4f92015-01-07 15:01:54 +0000988 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200989
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200990 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200991
Mark Rutland2fde4f92015-01-07 15:01:54 +0000992 WARN_ON(!list_empty(&ctx->active_ctx_list));
993
994 list_add(&ctx->active_ctx_list, head);
995}
996
997static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
998{
999 WARN_ON(!irqs_disabled());
1000
1001 WARN_ON(list_empty(&ctx->active_ctx_list));
1002
1003 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001004}
1005
1006static void get_ctx(struct perf_event_context *ctx)
1007{
1008 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
1009}
1010
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001011static void free_ctx(struct rcu_head *head)
1012{
1013 struct perf_event_context *ctx;
1014
1015 ctx = container_of(head, struct perf_event_context, rcu_head);
1016 kfree(ctx->task_ctx_data);
1017 kfree(ctx);
1018}
1019
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001020static void put_ctx(struct perf_event_context *ctx)
1021{
1022 if (atomic_dec_and_test(&ctx->refcount)) {
1023 if (ctx->parent_ctx)
1024 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001025 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001026 put_task_struct(ctx->task);
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001027 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001028 }
1029}
1030
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001031/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001032 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1033 * perf_pmu_migrate_context() we need some magic.
1034 *
1035 * Those places that change perf_event::ctx will hold both
1036 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1037 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001038 * Lock ordering is by mutex address. There are two other sites where
1039 * perf_event_context::mutex nests and those are:
1040 *
1041 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001042 * perf_event_exit_event()
1043 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001044 *
1045 * - perf_event_init_context() [ parent, 0 ]
1046 * inherit_task_group()
1047 * inherit_group()
1048 * inherit_event()
1049 * perf_event_alloc()
1050 * perf_init_event()
1051 * perf_try_init_event() [ child , 1 ]
1052 *
1053 * While it appears there is an obvious deadlock here -- the parent and child
1054 * nesting levels are inverted between the two. This is in fact safe because
1055 * life-time rules separate them. That is an exiting task cannot fork, and a
1056 * spawning task cannot (yet) exit.
1057 *
1058 * But remember that that these are parent<->child context relations, and
1059 * migration does not affect children, therefore these two orderings should not
1060 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001061 *
1062 * The change in perf_event::ctx does not affect children (as claimed above)
1063 * because the sys_perf_event_open() case will install a new event and break
1064 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1065 * concerned with cpuctx and that doesn't have children.
1066 *
1067 * The places that change perf_event::ctx will issue:
1068 *
1069 * perf_remove_from_context();
1070 * synchronize_rcu();
1071 * perf_install_in_context();
1072 *
1073 * to affect the change. The remove_from_context() + synchronize_rcu() should
1074 * quiesce the event, after which we can install it in the new location. This
1075 * means that only external vectors (perf_fops, prctl) can perturb the event
1076 * while in transit. Therefore all such accessors should also acquire
1077 * perf_event_context::mutex to serialize against this.
1078 *
1079 * However; because event->ctx can change while we're waiting to acquire
1080 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1081 * function.
1082 *
1083 * Lock order:
1084 * task_struct::perf_event_mutex
1085 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001086 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001087 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001088 * perf_event::mmap_mutex
1089 * mmap_sem
1090 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001091static struct perf_event_context *
1092perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001093{
1094 struct perf_event_context *ctx;
1095
1096again:
1097 rcu_read_lock();
1098 ctx = ACCESS_ONCE(event->ctx);
1099 if (!atomic_inc_not_zero(&ctx->refcount)) {
1100 rcu_read_unlock();
1101 goto again;
1102 }
1103 rcu_read_unlock();
1104
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001105 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001106 if (event->ctx != ctx) {
1107 mutex_unlock(&ctx->mutex);
1108 put_ctx(ctx);
1109 goto again;
1110 }
1111
1112 return ctx;
1113}
1114
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001115static inline struct perf_event_context *
1116perf_event_ctx_lock(struct perf_event *event)
1117{
1118 return perf_event_ctx_lock_nested(event, 0);
1119}
1120
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001121static void perf_event_ctx_unlock(struct perf_event *event,
1122 struct perf_event_context *ctx)
1123{
1124 mutex_unlock(&ctx->mutex);
1125 put_ctx(ctx);
1126}
1127
1128/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001129 * This must be done under the ctx->lock, such as to serialize against
1130 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1131 * calling scheduler related locks and ctx->lock nests inside those.
1132 */
1133static __must_check struct perf_event_context *
1134unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001135{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001136 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1137
1138 lockdep_assert_held(&ctx->lock);
1139
1140 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001141 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001142 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001143
1144 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001145}
1146
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001147static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1148{
1149 /*
1150 * only top level events have the pid namespace they were created in
1151 */
1152 if (event->parent)
1153 event = event->parent;
1154
1155 return task_tgid_nr_ns(p, event->ns);
1156}
1157
1158static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1159{
1160 /*
1161 * only top level events have the pid namespace they were created in
1162 */
1163 if (event->parent)
1164 event = event->parent;
1165
1166 return task_pid_nr_ns(p, event->ns);
1167}
1168
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001169/*
1170 * If we inherit events we want to return the parent event id
1171 * to userspace.
1172 */
1173static u64 primary_event_id(struct perf_event *event)
1174{
1175 u64 id = event->id;
1176
1177 if (event->parent)
1178 id = event->parent->id;
1179
1180 return id;
1181}
1182
1183/*
1184 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001185 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001186 * This has to cope with with the fact that until it is locked,
1187 * the context could get moved to another task.
1188 */
1189static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001190perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001191{
1192 struct perf_event_context *ctx;
1193
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001194retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001195 /*
1196 * One of the few rules of preemptible RCU is that one cannot do
1197 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001198 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001199 * rcu_read_unlock_special().
1200 *
1201 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001202 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001203 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001204 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001205 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001206 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001207 if (ctx) {
1208 /*
1209 * If this context is a clone of another, it might
1210 * get swapped for another underneath us by
1211 * perf_event_task_sched_out, though the
1212 * rcu_read_lock() protects us from any context
1213 * getting freed. Lock the context and check if it
1214 * got swapped before we could get the lock, and retry
1215 * if so. If we locked the right context, then it
1216 * can't get swapped on us any more.
1217 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001218 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001219 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001220 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001221 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001222 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001223 goto retry;
1224 }
1225
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001226 if (ctx->task == TASK_TOMBSTONE ||
1227 !atomic_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001228 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001229 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001230 } else {
1231 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001232 }
1233 }
1234 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001235 if (!ctx)
1236 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001237 return ctx;
1238}
1239
1240/*
1241 * Get the context for a task and increment its pin_count so it
1242 * can't get swapped to another task. This also increments its
1243 * reference count so that the context can't get freed.
1244 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001245static struct perf_event_context *
1246perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001247{
1248 struct perf_event_context *ctx;
1249 unsigned long flags;
1250
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001251 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001252 if (ctx) {
1253 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001254 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001255 }
1256 return ctx;
1257}
1258
1259static void perf_unpin_context(struct perf_event_context *ctx)
1260{
1261 unsigned long flags;
1262
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001263 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001264 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001265 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001266}
1267
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001268/*
1269 * Update the record of the current time in a context.
1270 */
1271static void update_context_time(struct perf_event_context *ctx)
1272{
1273 u64 now = perf_clock();
1274
1275 ctx->time += now - ctx->timestamp;
1276 ctx->timestamp = now;
1277}
1278
Stephane Eranian41587552011-01-03 18:20:01 +02001279static u64 perf_event_time(struct perf_event *event)
1280{
1281 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001282
1283 if (is_cgroup_event(event))
1284 return perf_cgroup_event_time(event);
1285
Stephane Eranian41587552011-01-03 18:20:01 +02001286 return ctx ? ctx->time : 0;
1287}
1288
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001289/*
1290 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001291 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001292 */
1293static void update_event_times(struct perf_event *event)
1294{
1295 struct perf_event_context *ctx = event->ctx;
1296 u64 run_end;
1297
1298 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1299 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1300 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001301 /*
1302 * in cgroup mode, time_enabled represents
1303 * the time the event was enabled AND active
1304 * tasks were in the monitored cgroup. This is
1305 * independent of the activity of the context as
1306 * there may be a mix of cgroup and non-cgroup events.
1307 *
1308 * That is why we treat cgroup events differently
1309 * here.
1310 */
1311 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001312 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001313 else if (ctx->is_active)
1314 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001315 else
1316 run_end = event->tstamp_stopped;
1317
1318 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001319
1320 if (event->state == PERF_EVENT_STATE_INACTIVE)
1321 run_end = event->tstamp_stopped;
1322 else
Stephane Eranian41587552011-01-03 18:20:01 +02001323 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001324
1325 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001326
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001327}
1328
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001329/*
1330 * Update total_time_enabled and total_time_running for all events in a group.
1331 */
1332static void update_group_times(struct perf_event *leader)
1333{
1334 struct perf_event *event;
1335
1336 update_event_times(leader);
1337 list_for_each_entry(event, &leader->sibling_list, group_entry)
1338 update_event_times(event);
1339}
1340
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001341static struct list_head *
1342ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1343{
1344 if (event->attr.pinned)
1345 return &ctx->pinned_groups;
1346 else
1347 return &ctx->flexible_groups;
1348}
1349
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001350/*
1351 * Add a event from the lists for its context.
1352 * Must be called with ctx->mutex and ctx->lock held.
1353 */
1354static void
1355list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1356{
Peter Zijlstrac994d612016-01-08 09:20:23 +01001357 lockdep_assert_held(&ctx->lock);
1358
Peter Zijlstra8a495422010-05-27 15:47:49 +02001359 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1360 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001361
1362 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001363 * If we're a stand alone event or group leader, we go to the context
1364 * list, group events are kept attached to the group so that
1365 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001366 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001367 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001368 struct list_head *list;
1369
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001370 if (is_software_event(event))
1371 event->group_flags |= PERF_GROUP_SOFTWARE;
1372
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001373 list = ctx_group_list(event, ctx);
1374 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001375 }
1376
Peter Zijlstra08309372011-03-03 11:31:20 +01001377 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001378 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001379
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001380 list_add_rcu(&event->event_entry, &ctx->event_list);
1381 ctx->nr_events++;
1382 if (event->attr.inherit_stat)
1383 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001384
1385 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001386}
1387
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001388/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001389 * Initialize event state based on the perf_event_attr::disabled.
1390 */
1391static inline void perf_event__state_init(struct perf_event *event)
1392{
1393 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1394 PERF_EVENT_STATE_INACTIVE;
1395}
1396
Peter Zijlstraa7239682015-09-09 19:06:33 +02001397static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001398{
1399 int entry = sizeof(u64); /* value */
1400 int size = 0;
1401 int nr = 1;
1402
1403 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1404 size += sizeof(u64);
1405
1406 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1407 size += sizeof(u64);
1408
1409 if (event->attr.read_format & PERF_FORMAT_ID)
1410 entry += sizeof(u64);
1411
1412 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001413 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001414 size += sizeof(u64);
1415 }
1416
1417 size += entry * nr;
1418 event->read_size = size;
1419}
1420
Peter Zijlstraa7239682015-09-09 19:06:33 +02001421static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001422{
1423 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001424 u16 size = 0;
1425
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001426 if (sample_type & PERF_SAMPLE_IP)
1427 size += sizeof(data->ip);
1428
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001429 if (sample_type & PERF_SAMPLE_ADDR)
1430 size += sizeof(data->addr);
1431
1432 if (sample_type & PERF_SAMPLE_PERIOD)
1433 size += sizeof(data->period);
1434
Andi Kleenc3feedf2013-01-24 16:10:28 +01001435 if (sample_type & PERF_SAMPLE_WEIGHT)
1436 size += sizeof(data->weight);
1437
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001438 if (sample_type & PERF_SAMPLE_READ)
1439 size += event->read_size;
1440
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001441 if (sample_type & PERF_SAMPLE_DATA_SRC)
1442 size += sizeof(data->data_src.val);
1443
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001444 if (sample_type & PERF_SAMPLE_TRANSACTION)
1445 size += sizeof(data->txn);
1446
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001447 event->header_size = size;
1448}
1449
Peter Zijlstraa7239682015-09-09 19:06:33 +02001450/*
1451 * Called at perf_event creation and when events are attached/detached from a
1452 * group.
1453 */
1454static void perf_event__header_size(struct perf_event *event)
1455{
1456 __perf_event_read_size(event,
1457 event->group_leader->nr_siblings);
1458 __perf_event_header_size(event, event->attr.sample_type);
1459}
1460
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001461static void perf_event__id_header_size(struct perf_event *event)
1462{
1463 struct perf_sample_data *data;
1464 u64 sample_type = event->attr.sample_type;
1465 u16 size = 0;
1466
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001467 if (sample_type & PERF_SAMPLE_TID)
1468 size += sizeof(data->tid_entry);
1469
1470 if (sample_type & PERF_SAMPLE_TIME)
1471 size += sizeof(data->time);
1472
Adrian Hunterff3d5272013-08-27 11:23:07 +03001473 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1474 size += sizeof(data->id);
1475
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001476 if (sample_type & PERF_SAMPLE_ID)
1477 size += sizeof(data->id);
1478
1479 if (sample_type & PERF_SAMPLE_STREAM_ID)
1480 size += sizeof(data->stream_id);
1481
1482 if (sample_type & PERF_SAMPLE_CPU)
1483 size += sizeof(data->cpu_entry);
1484
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001485 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001486}
1487
Peter Zijlstraa7239682015-09-09 19:06:33 +02001488static bool perf_event_validate_size(struct perf_event *event)
1489{
1490 /*
1491 * The values computed here will be over-written when we actually
1492 * attach the event.
1493 */
1494 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1495 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1496 perf_event__id_header_size(event);
1497
1498 /*
1499 * Sum the lot; should not exceed the 64k limit we have on records.
1500 * Conservative limit to allow for callchains and other variable fields.
1501 */
1502 if (event->read_size + event->header_size +
1503 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1504 return false;
1505
1506 return true;
1507}
1508
Peter Zijlstra8a495422010-05-27 15:47:49 +02001509static void perf_group_attach(struct perf_event *event)
1510{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001511 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001512
Peter Zijlstra74c33372010-10-15 11:40:29 +02001513 /*
1514 * We can have double attach due to group movement in perf_event_open.
1515 */
1516 if (event->attach_state & PERF_ATTACH_GROUP)
1517 return;
1518
Peter Zijlstra8a495422010-05-27 15:47:49 +02001519 event->attach_state |= PERF_ATTACH_GROUP;
1520
1521 if (group_leader == event)
1522 return;
1523
Peter Zijlstra652884f2015-01-23 11:20:10 +01001524 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1525
Peter Zijlstra8a495422010-05-27 15:47:49 +02001526 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1527 !is_software_event(event))
1528 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1529
1530 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1531 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001532
1533 perf_event__header_size(group_leader);
1534
1535 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1536 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001537}
1538
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001539/*
1540 * Remove a event from the lists for its context.
1541 * Must be called with ctx->mutex and ctx->lock held.
1542 */
1543static void
1544list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1545{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001546 struct perf_cpu_context *cpuctx;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001547
1548 WARN_ON_ONCE(event->ctx != ctx);
1549 lockdep_assert_held(&ctx->lock);
1550
Peter Zijlstra8a495422010-05-27 15:47:49 +02001551 /*
1552 * We can have double detach due to exit/hot-unplug + close.
1553 */
1554 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001555 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001556
1557 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1558
Stephane Eranian68cacd22011-03-23 16:03:06 +01001559 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001560 ctx->nr_cgroups--;
Peter Zijlstra70a01652016-01-08 09:29:16 +01001561 /*
1562 * Because cgroup events are always per-cpu events, this will
1563 * always be called from the right CPU.
1564 */
Stephane Eranian68cacd22011-03-23 16:03:06 +01001565 cpuctx = __get_cpu_context(ctx);
1566 /*
Peter Zijlstra70a01652016-01-08 09:29:16 +01001567 * If there are no more cgroup events then clear cgrp to avoid
1568 * stale pointer in update_cgrp_time_from_cpuctx().
Stephane Eranian68cacd22011-03-23 16:03:06 +01001569 */
1570 if (!ctx->nr_cgroups)
1571 cpuctx->cgrp = NULL;
1572 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001573
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001574 ctx->nr_events--;
1575 if (event->attr.inherit_stat)
1576 ctx->nr_stat--;
1577
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001578 list_del_rcu(&event->event_entry);
1579
Peter Zijlstra8a495422010-05-27 15:47:49 +02001580 if (event->group_leader == event)
1581 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001582
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001583 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001584
1585 /*
1586 * If event was in error state, then keep it
1587 * that way, otherwise bogus counts will be
1588 * returned on read(). The only way to get out
1589 * of error state is by explicit re-enabling
1590 * of the event
1591 */
1592 if (event->state > PERF_EVENT_STATE_OFF)
1593 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001594
1595 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001596}
1597
Peter Zijlstra8a495422010-05-27 15:47:49 +02001598static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001599{
1600 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001601 struct list_head *list = NULL;
1602
1603 /*
1604 * We can have double detach due to exit/hot-unplug + close.
1605 */
1606 if (!(event->attach_state & PERF_ATTACH_GROUP))
1607 return;
1608
1609 event->attach_state &= ~PERF_ATTACH_GROUP;
1610
1611 /*
1612 * If this is a sibling, remove it from its group.
1613 */
1614 if (event->group_leader != event) {
1615 list_del_init(&event->group_entry);
1616 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001617 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001618 }
1619
1620 if (!list_empty(&event->group_entry))
1621 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001622
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001623 /*
1624 * If this was a group event with sibling events then
1625 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001626 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001627 */
1628 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001629 if (list)
1630 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001631 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001632
1633 /* Inherit group flags from the previous leader */
1634 sibling->group_flags = event->group_flags;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001635
1636 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001637 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001638
1639out:
1640 perf_event__header_size(event->group_leader);
1641
1642 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1643 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001644}
1645
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001646static bool is_orphaned_event(struct perf_event *event)
1647{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02001648 return event->state == PERF_EVENT_STATE_EXIT;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001649}
1650
Mark Rutland66eb5792015-05-13 17:12:23 +01001651static inline int pmu_filter_match(struct perf_event *event)
1652{
1653 struct pmu *pmu = event->pmu;
1654 return pmu->filter_match ? pmu->filter_match(event) : 1;
1655}
1656
Stephane Eranianfa66f072010-08-26 16:40:01 +02001657static inline int
1658event_filter_match(struct perf_event *event)
1659{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001660 return (event->cpu == -1 || event->cpu == smp_processor_id())
Mark Rutland66eb5792015-05-13 17:12:23 +01001661 && perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001662}
1663
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001664static void
1665event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001666 struct perf_cpu_context *cpuctx,
1667 struct perf_event_context *ctx)
1668{
Stephane Eranian41587552011-01-03 18:20:01 +02001669 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001670 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001671
1672 WARN_ON_ONCE(event->ctx != ctx);
1673 lockdep_assert_held(&ctx->lock);
1674
Stephane Eranianfa66f072010-08-26 16:40:01 +02001675 /*
1676 * An event which could not be activated because of
1677 * filter mismatch still needs to have its timings
1678 * maintained, otherwise bogus information is return
1679 * via read() for time_enabled, time_running:
1680 */
1681 if (event->state == PERF_EVENT_STATE_INACTIVE
1682 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001683 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001684 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001685 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001686 }
1687
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001688 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001689 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001690
Alexander Shishkin44377272013-12-16 14:17:36 +02001691 perf_pmu_disable(event->pmu);
1692
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001693 event->state = PERF_EVENT_STATE_INACTIVE;
1694 if (event->pending_disable) {
1695 event->pending_disable = 0;
1696 event->state = PERF_EVENT_STATE_OFF;
1697 }
Stephane Eranian41587552011-01-03 18:20:01 +02001698 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001699 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001700 event->oncpu = -1;
1701
1702 if (!is_software_event(event))
1703 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001704 if (!--ctx->nr_active)
1705 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001706 if (event->attr.freq && event->attr.sample_freq)
1707 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001708 if (event->attr.exclusive || !cpuctx->active_oncpu)
1709 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001710
1711 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001712}
1713
1714static void
1715group_sched_out(struct perf_event *group_event,
1716 struct perf_cpu_context *cpuctx,
1717 struct perf_event_context *ctx)
1718{
1719 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001720 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001721
1722 event_sched_out(group_event, cpuctx, ctx);
1723
1724 /*
1725 * Schedule out siblings (if any):
1726 */
1727 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1728 event_sched_out(event, cpuctx, ctx);
1729
Stephane Eranianfa66f072010-08-26 16:40:01 +02001730 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001731 cpuctx->exclusive = 0;
1732}
1733
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001734#define DETACH_GROUP 0x01UL
Peter Zijlstra60beda82016-01-26 14:55:02 +01001735#define DETACH_STATE 0x02UL
Peter Zijlstra00179602015-11-30 16:26:35 +01001736
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001737/*
1738 * Cross CPU call to remove a performance event
1739 *
1740 * We disable the event on the hardware level first. After that we
1741 * remove it from the context list.
1742 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001743static void
1744__perf_remove_from_context(struct perf_event *event,
1745 struct perf_cpu_context *cpuctx,
1746 struct perf_event_context *ctx,
1747 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001748{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001749 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001750
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001751 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001752 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001753 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001754 list_del_event(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01001755 if (flags & DETACH_STATE)
1756 event->state = PERF_EVENT_STATE_EXIT;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001757
Peter Zijlstra39a43642016-01-11 12:46:35 +01001758 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001759 ctx->is_active = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01001760 if (ctx->task) {
1761 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1762 cpuctx->task_ctx = NULL;
1763 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001764 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001765}
1766
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001767/*
1768 * Remove the event from a task's (or a CPU's) list of events.
1769 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001770 * If event->ctx is a cloned context, callers must make sure that
1771 * every task struct that event->ctx->task could possibly point to
1772 * remains valid. This is OK when called from perf_release since
1773 * that only calls us on the top-level context, which can't be a clone.
1774 * When called from perf_event_exit_task, it's OK because the
1775 * context has been detached from its task.
1776 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001777static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001778{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001779 lockdep_assert_held(&event->ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001780
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001781 event_function_call(event, __perf_remove_from_context, (void *)flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001782}
1783
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001784/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001785 * Cross CPU call to disable a performance event
1786 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001787static void __perf_event_disable(struct perf_event *event,
1788 struct perf_cpu_context *cpuctx,
1789 struct perf_event_context *ctx,
1790 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001791{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001792 if (event->state < PERF_EVENT_STATE_INACTIVE)
1793 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001794
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001795 update_context_time(ctx);
1796 update_cgrp_time_from_event(event);
1797 update_group_times(event);
1798 if (event == event->group_leader)
1799 group_sched_out(event, cpuctx, ctx);
1800 else
1801 event_sched_out(event, cpuctx, ctx);
1802 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra7b648012015-12-03 18:35:21 +01001803}
1804
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001805/*
1806 * Disable a event.
1807 *
1808 * If event->ctx is a cloned context, callers must make sure that
1809 * every task struct that event->ctx->task could possibly point to
1810 * remains valid. This condition is satisifed when called through
1811 * perf_event_for_each_child or perf_event_for_each because they
1812 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001813 * goes to exit will block in perf_event_exit_event().
1814 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001815 * When called from perf_pending_event it's OK because event->ctx
1816 * is the current context on this CPU and preemption is disabled,
1817 * hence we can't get into perf_event_task_sched_out for this context.
1818 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001819static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001820{
1821 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001822
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001823 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001824 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001825 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001826 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001827 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001828 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001829
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001830 event_function_call(event, __perf_event_disable, NULL);
1831}
1832
1833void perf_event_disable_local(struct perf_event *event)
1834{
1835 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001836}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001837
1838/*
1839 * Strictly speaking kernel users cannot create groups and therefore this
1840 * interface does not need the perf_event_ctx_lock() magic.
1841 */
1842void perf_event_disable(struct perf_event *event)
1843{
1844 struct perf_event_context *ctx;
1845
1846 ctx = perf_event_ctx_lock(event);
1847 _perf_event_disable(event);
1848 perf_event_ctx_unlock(event, ctx);
1849}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001850EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001851
Stephane Eraniane5d13672011-02-14 11:20:01 +02001852static void perf_set_shadow_time(struct perf_event *event,
1853 struct perf_event_context *ctx,
1854 u64 tstamp)
1855{
1856 /*
1857 * use the correct time source for the time snapshot
1858 *
1859 * We could get by without this by leveraging the
1860 * fact that to get to this function, the caller
1861 * has most likely already called update_context_time()
1862 * and update_cgrp_time_xx() and thus both timestamp
1863 * are identical (or very close). Given that tstamp is,
1864 * already adjusted for cgroup, we could say that:
1865 * tstamp - ctx->timestamp
1866 * is equivalent to
1867 * tstamp - cgrp->timestamp.
1868 *
1869 * Then, in perf_output_read(), the calculation would
1870 * work with no changes because:
1871 * - event is guaranteed scheduled in
1872 * - no scheduled out in between
1873 * - thus the timestamp would be the same
1874 *
1875 * But this is a bit hairy.
1876 *
1877 * So instead, we have an explicit cgroup call to remain
1878 * within the time time source all along. We believe it
1879 * is cleaner and simpler to understand.
1880 */
1881 if (is_cgroup_event(event))
1882 perf_cgroup_set_shadow_time(event, tstamp);
1883 else
1884 event->shadow_ctx_time = tstamp - ctx->timestamp;
1885}
1886
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001887#define MAX_INTERRUPTS (~0ULL)
1888
1889static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001890static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001891
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001892static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001893event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001894 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001895 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001896{
Stephane Eranian41587552011-01-03 18:20:01 +02001897 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001898 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001899
Peter Zijlstra63342412014-05-05 11:49:16 +02001900 lockdep_assert_held(&ctx->lock);
1901
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001902 if (event->state <= PERF_EVENT_STATE_OFF)
1903 return 0;
1904
1905 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001906 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001907
1908 /*
1909 * Unthrottle events, since we scheduled we might have missed several
1910 * ticks already, also for a heavily scheduling task there is little
1911 * guarantee it'll get a tick in a timely manner.
1912 */
1913 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1914 perf_log_throttle(event, 1);
1915 event->hw.interrupts = 0;
1916 }
1917
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001918 /*
1919 * The new state must be visible before we turn it on in the hardware:
1920 */
1921 smp_wmb();
1922
Alexander Shishkin44377272013-12-16 14:17:36 +02001923 perf_pmu_disable(event->pmu);
1924
Shaohua Li72f669c2015-02-05 15:55:31 -08001925 perf_set_shadow_time(event, ctx, tstamp);
1926
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001927 perf_log_itrace_start(event);
1928
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001929 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001930 event->state = PERF_EVENT_STATE_INACTIVE;
1931 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001932 ret = -EAGAIN;
1933 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001934 }
1935
Peter Zijlstra00a29162015-07-27 10:35:07 +02001936 event->tstamp_running += tstamp - event->tstamp_stopped;
1937
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001938 if (!is_software_event(event))
1939 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001940 if (!ctx->nr_active++)
1941 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001942 if (event->attr.freq && event->attr.sample_freq)
1943 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001944
1945 if (event->attr.exclusive)
1946 cpuctx->exclusive = 1;
1947
Alexander Shishkin44377272013-12-16 14:17:36 +02001948out:
1949 perf_pmu_enable(event->pmu);
1950
1951 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001952}
1953
1954static int
1955group_sched_in(struct perf_event *group_event,
1956 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001957 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001958{
Lin Ming6bde9b62010-04-23 13:56:00 +08001959 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001960 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001961 u64 now = ctx->time;
1962 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001963
1964 if (group_event->state == PERF_EVENT_STATE_OFF)
1965 return 0;
1966
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001967 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08001968
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001969 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001970 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001971 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001972 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001973 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001974
1975 /*
1976 * Schedule in siblings as one group (if any):
1977 */
1978 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001979 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001980 partial_group = event;
1981 goto group_error;
1982 }
1983 }
1984
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001985 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001986 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001987
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001988group_error:
1989 /*
1990 * Groups can be scheduled in as one unit only, so undo any
1991 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001992 * The events up to the failed event are scheduled out normally,
1993 * tstamp_stopped will be updated.
1994 *
1995 * The failed events and the remaining siblings need to have
1996 * their timings updated as if they had gone thru event_sched_in()
1997 * and event_sched_out(). This is required to get consistent timings
1998 * across the group. This also takes care of the case where the group
1999 * could never be scheduled by ensuring tstamp_stopped is set to mark
2000 * the time the event was actually stopped, such that time delta
2001 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002002 */
2003 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2004 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02002005 simulate = true;
2006
2007 if (simulate) {
2008 event->tstamp_running += now - event->tstamp_stopped;
2009 event->tstamp_stopped = now;
2010 } else {
2011 event_sched_out(event, cpuctx, ctx);
2012 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002013 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002014 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002015
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002016 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002017
Peter Zijlstra272325c2015-04-15 11:41:58 +02002018 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002019
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002020 return -EAGAIN;
2021}
2022
2023/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002024 * Work out whether we can put this event group on the CPU now.
2025 */
2026static int group_can_go_on(struct perf_event *event,
2027 struct perf_cpu_context *cpuctx,
2028 int can_add_hw)
2029{
2030 /*
2031 * Groups consisting entirely of software events can always go on.
2032 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01002033 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002034 return 1;
2035 /*
2036 * If an exclusive group is already on, no other hardware
2037 * events can go on.
2038 */
2039 if (cpuctx->exclusive)
2040 return 0;
2041 /*
2042 * If this group is exclusive and there are already
2043 * events on the CPU, it can't go on.
2044 */
2045 if (event->attr.exclusive && cpuctx->active_oncpu)
2046 return 0;
2047 /*
2048 * Otherwise, try to add it if all previous groups were able
2049 * to go on.
2050 */
2051 return can_add_hw;
2052}
2053
2054static void add_event_to_ctx(struct perf_event *event,
2055 struct perf_event_context *ctx)
2056{
Stephane Eranian41587552011-01-03 18:20:01 +02002057 u64 tstamp = perf_event_time(event);
2058
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002059 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002060 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002061 event->tstamp_enabled = tstamp;
2062 event->tstamp_running = tstamp;
2063 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002064}
2065
Peter Zijlstra3e349502016-01-08 10:01:18 +01002066static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2067 struct perf_event_context *ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002068static void
2069ctx_sched_in(struct perf_event_context *ctx,
2070 struct perf_cpu_context *cpuctx,
2071 enum event_type_t event_type,
2072 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002073
Peter Zijlstradce58552011-04-09 21:17:46 +02002074static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2075 struct perf_event_context *ctx,
2076 struct task_struct *task)
2077{
2078 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2079 if (ctx)
2080 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2081 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2082 if (ctx)
2083 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2084}
2085
Peter Zijlstra3e349502016-01-08 10:01:18 +01002086static void ctx_resched(struct perf_cpu_context *cpuctx,
2087 struct perf_event_context *task_ctx)
Peter Zijlstra00179602015-11-30 16:26:35 +01002088{
Peter Zijlstra3e349502016-01-08 10:01:18 +01002089 perf_pmu_disable(cpuctx->ctx.pmu);
2090 if (task_ctx)
2091 task_ctx_sched_out(cpuctx, task_ctx);
2092 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2093 perf_event_sched_in(cpuctx, task_ctx, current);
2094 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002095}
2096
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002097/*
2098 * Cross CPU call to install and enable a performance event
2099 *
2100 * Must be called with ctx->mutex held
2101 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002102static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002103{
Peter Zijlstra39a43642016-01-11 12:46:35 +01002104 struct perf_event_context *ctx = info;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002105 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002106 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002107
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002108 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002109 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002110 raw_spin_lock(&ctx->lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002111 /*
2112 * If we hit the 'wrong' task, we've since scheduled and
2113 * everything should be sorted, nothing to do!
2114 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002115 task_ctx = ctx;
Peter Zijlstra39a43642016-01-11 12:46:35 +01002116 if (ctx->task != current)
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002117 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002118
Peter Zijlstra39a43642016-01-11 12:46:35 +01002119 /*
2120 * If task_ctx is set, it had better be to us.
2121 */
2122 WARN_ON_ONCE(cpuctx->task_ctx != ctx && cpuctx->task_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002123 } else if (task_ctx) {
2124 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002125 }
2126
Peter Zijlstra39a43642016-01-11 12:46:35 +01002127 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002128unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002129 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002130
2131 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002132}
2133
2134/*
2135 * Attach a performance event to a context
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002136 */
2137static void
2138perf_install_in_context(struct perf_event_context *ctx,
2139 struct perf_event *event,
2140 int cpu)
2141{
Peter Zijlstra39a43642016-01-11 12:46:35 +01002142 struct task_struct *task = NULL;
2143
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002144 lockdep_assert_held(&ctx->mutex);
2145
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002146 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002147 if (event->cpu != -1)
2148 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002149
Peter Zijlstra39a43642016-01-11 12:46:35 +01002150 /*
2151 * Installing events is tricky because we cannot rely on ctx->is_active
2152 * to be set in case this is the nr_events 0 -> 1 transition.
2153 *
2154 * So what we do is we add the event to the list here, which will allow
2155 * a future context switch to DTRT and then send a racy IPI. If the IPI
2156 * fails to hit the right task, this means a context switch must have
2157 * happened and that will have taken care of business.
2158 */
2159 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002160 task = ctx->task;
Peter Zijlstra84c4e622016-02-24 18:45:40 +01002161
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002162 /*
Peter Zijlstra84c4e622016-02-24 18:45:40 +01002163 * If between ctx = find_get_context() and mutex_lock(&ctx->mutex) the
2164 * ctx gets destroyed, we must not install an event into it.
2165 *
2166 * This is normally tested for after we acquire the mutex, so this is
2167 * a sanity check.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002168 */
Peter Zijlstra84c4e622016-02-24 18:45:40 +01002169 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002170 raw_spin_unlock_irq(&ctx->lock);
2171 return;
2172 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002173
2174 if (ctx->is_active) {
2175 update_context_time(ctx);
2176 update_cgrp_time_from_event(event);
2177 }
2178
Peter Zijlstra39a43642016-01-11 12:46:35 +01002179 add_event_to_ctx(event, ctx);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002180 raw_spin_unlock_irq(&ctx->lock);
2181
2182 if (task)
2183 task_function_call(task, __perf_install_in_context, ctx);
2184 else
2185 cpu_function_call(cpu, __perf_install_in_context, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002186}
2187
2188/*
2189 * Put a event into inactive state and update time fields.
2190 * Enabling the leader of a group effectively enables all
2191 * the group members that aren't explicitly disabled, so we
2192 * have to update their ->tstamp_enabled also.
2193 * Note: this works for group members as well as group leaders
2194 * since the non-leader members' sibling_lists will be empty.
2195 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002196static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002197{
2198 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002199 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002200
2201 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002202 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002203 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002204 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2205 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002206 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002207}
2208
2209/*
2210 * Cross CPU call to enable a performance event
2211 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002212static void __perf_event_enable(struct perf_event *event,
2213 struct perf_cpu_context *cpuctx,
2214 struct perf_event_context *ctx,
2215 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002216{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002217 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002218 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002219
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002220 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2221 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002222 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002223
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002224 update_context_time(ctx);
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002225 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002226
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002227 if (!ctx->is_active)
2228 return;
2229
Stephane Eraniane5d13672011-02-14 11:20:01 +02002230 if (!event_filter_match(event)) {
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002231 if (is_cgroup_event(event)) {
2232 perf_cgroup_set_timestamp(current, ctx); // XXX ?
Stephane Eraniane5d13672011-02-14 11:20:01 +02002233 perf_cgroup_defer_enabled(event);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002234 }
2235 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002236 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002237
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002238 /*
2239 * If the event is in a group and isn't the group leader,
2240 * then don't put it on unless the group is on.
2241 */
2242 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002243 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002244
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002245 task_ctx = cpuctx->task_ctx;
2246 if (ctx->task)
2247 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002248
Peter Zijlstraaee7dbc2016-01-08 10:45:11 +01002249 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002250}
2251
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002252/*
2253 * Enable a event.
2254 *
2255 * If event->ctx is a cloned context, callers must make sure that
2256 * every task struct that event->ctx->task could possibly point to
2257 * remains valid. This condition is satisfied when called through
2258 * perf_event_for_each_child or perf_event_for_each as described
2259 * for perf_event_disable.
2260 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002261static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002262{
2263 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002264
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002265 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002266 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2267 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002268 raw_spin_unlock_irq(&ctx->lock);
2269 return;
2270 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002271
2272 /*
2273 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002274 *
2275 * That way, if we see the event in error state below, we know that it
2276 * has gone back into error state, as distinct from the task having
2277 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002278 */
2279 if (event->state == PERF_EVENT_STATE_ERROR)
2280 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002281 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002282
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002283 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002284}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002285
2286/*
2287 * See perf_event_disable();
2288 */
2289void perf_event_enable(struct perf_event *event)
2290{
2291 struct perf_event_context *ctx;
2292
2293 ctx = perf_event_ctx_lock(event);
2294 _perf_event_enable(event);
2295 perf_event_ctx_unlock(event, ctx);
2296}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002297EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002298
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002299static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002300{
2301 /*
2302 * not supported on inherited events
2303 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002304 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002305 return -EINVAL;
2306
2307 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002308 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002309
2310 return 0;
2311}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002312
2313/*
2314 * See perf_event_disable()
2315 */
2316int perf_event_refresh(struct perf_event *event, int refresh)
2317{
2318 struct perf_event_context *ctx;
2319 int ret;
2320
2321 ctx = perf_event_ctx_lock(event);
2322 ret = _perf_event_refresh(event, refresh);
2323 perf_event_ctx_unlock(event, ctx);
2324
2325 return ret;
2326}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002327EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002328
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002329static void ctx_sched_out(struct perf_event_context *ctx,
2330 struct perf_cpu_context *cpuctx,
2331 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002332{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002333 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002334 struct perf_event *event;
2335
2336 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002337
Peter Zijlstra39a43642016-01-11 12:46:35 +01002338 if (likely(!ctx->nr_events)) {
2339 /*
2340 * See __perf_remove_from_context().
2341 */
2342 WARN_ON_ONCE(ctx->is_active);
2343 if (ctx->task)
2344 WARN_ON_ONCE(cpuctx->task_ctx);
2345 return;
2346 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002347
Peter Zijlstradb24d332011-04-09 21:17:45 +02002348 ctx->is_active &= ~event_type;
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002349 if (ctx->task) {
2350 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2351 if (!ctx->is_active)
2352 cpuctx->task_ctx = NULL;
2353 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002354
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002355 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002356 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002357 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002358 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002359
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002360 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002361 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002362 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2363 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002364 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002365
Peter Zijlstradb24d332011-04-09 21:17:45 +02002366 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002367 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002368 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002369 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002370 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002371}
2372
2373/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002374 * Test whether two contexts are equivalent, i.e. whether they have both been
2375 * cloned from the same version of the same context.
2376 *
2377 * Equivalence is measured using a generation number in the context that is
2378 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2379 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002380 */
2381static int context_equiv(struct perf_event_context *ctx1,
2382 struct perf_event_context *ctx2)
2383{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002384 lockdep_assert_held(&ctx1->lock);
2385 lockdep_assert_held(&ctx2->lock);
2386
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002387 /* Pinning disables the swap optimization */
2388 if (ctx1->pin_count || ctx2->pin_count)
2389 return 0;
2390
2391 /* If ctx1 is the parent of ctx2 */
2392 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2393 return 1;
2394
2395 /* If ctx2 is the parent of ctx1 */
2396 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2397 return 1;
2398
2399 /*
2400 * If ctx1 and ctx2 have the same parent; we flatten the parent
2401 * hierarchy, see perf_event_init_context().
2402 */
2403 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2404 ctx1->parent_gen == ctx2->parent_gen)
2405 return 1;
2406
2407 /* Unmatched */
2408 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002409}
2410
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002411static void __perf_event_sync_stat(struct perf_event *event,
2412 struct perf_event *next_event)
2413{
2414 u64 value;
2415
2416 if (!event->attr.inherit_stat)
2417 return;
2418
2419 /*
2420 * Update the event value, we cannot use perf_event_read()
2421 * because we're in the middle of a context switch and have IRQs
2422 * disabled, which upsets smp_call_function_single(), however
2423 * we know the event must be on the current CPU, therefore we
2424 * don't need to use it.
2425 */
2426 switch (event->state) {
2427 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002428 event->pmu->read(event);
2429 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002430
2431 case PERF_EVENT_STATE_INACTIVE:
2432 update_event_times(event);
2433 break;
2434
2435 default:
2436 break;
2437 }
2438
2439 /*
2440 * In order to keep per-task stats reliable we need to flip the event
2441 * values when we flip the contexts.
2442 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002443 value = local64_read(&next_event->count);
2444 value = local64_xchg(&event->count, value);
2445 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002446
2447 swap(event->total_time_enabled, next_event->total_time_enabled);
2448 swap(event->total_time_running, next_event->total_time_running);
2449
2450 /*
2451 * Since we swizzled the values, update the user visible data too.
2452 */
2453 perf_event_update_userpage(event);
2454 perf_event_update_userpage(next_event);
2455}
2456
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002457static void perf_event_sync_stat(struct perf_event_context *ctx,
2458 struct perf_event_context *next_ctx)
2459{
2460 struct perf_event *event, *next_event;
2461
2462 if (!ctx->nr_stat)
2463 return;
2464
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002465 update_context_time(ctx);
2466
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002467 event = list_first_entry(&ctx->event_list,
2468 struct perf_event, event_entry);
2469
2470 next_event = list_first_entry(&next_ctx->event_list,
2471 struct perf_event, event_entry);
2472
2473 while (&event->event_entry != &ctx->event_list &&
2474 &next_event->event_entry != &next_ctx->event_list) {
2475
2476 __perf_event_sync_stat(event, next_event);
2477
2478 event = list_next_entry(event, event_entry);
2479 next_event = list_next_entry(next_event, event_entry);
2480 }
2481}
2482
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002483static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2484 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002485{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002486 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002487 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002488 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002489 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002490 int do_switch = 1;
2491
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002492 if (likely(!ctx))
2493 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002494
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002495 cpuctx = __get_cpu_context(ctx);
2496 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002497 return;
2498
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002499 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002500 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002501 if (!next_ctx)
2502 goto unlock;
2503
2504 parent = rcu_dereference(ctx->parent_ctx);
2505 next_parent = rcu_dereference(next_ctx->parent_ctx);
2506
2507 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002508 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002509 goto unlock;
2510
2511 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002512 /*
2513 * Looks like the two contexts are clones, so we might be
2514 * able to optimize the context switch. We lock both
2515 * contexts and check that they are clones under the
2516 * lock (including re-checking that neither has been
2517 * uncloned in the meantime). It doesn't matter which
2518 * order we take the locks because no other cpu could
2519 * be trying to lock both of these tasks.
2520 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002521 raw_spin_lock(&ctx->lock);
2522 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002523 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002524 WRITE_ONCE(ctx->task, next);
2525 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05002526
2527 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2528
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002529 /*
2530 * RCU_INIT_POINTER here is safe because we've not
2531 * modified the ctx and the above modification of
2532 * ctx->task and ctx->task_ctx_data are immaterial
2533 * since those values are always verified under
2534 * ctx->lock which we're now holding.
2535 */
2536 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2537 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2538
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002539 do_switch = 0;
2540
2541 perf_event_sync_stat(ctx, next_ctx);
2542 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002543 raw_spin_unlock(&next_ctx->lock);
2544 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002545 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002546unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002547 rcu_read_unlock();
2548
2549 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002550 raw_spin_lock(&ctx->lock);
Peter Zijlstra8833d0e2016-01-08 10:02:37 +01002551 task_ctx_sched_out(cpuctx, ctx);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002552 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002553 }
2554}
2555
Yan, Zhengba532502014-11-04 21:55:58 -05002556void perf_sched_cb_dec(struct pmu *pmu)
2557{
2558 this_cpu_dec(perf_sched_cb_usages);
2559}
2560
2561void perf_sched_cb_inc(struct pmu *pmu)
2562{
2563 this_cpu_inc(perf_sched_cb_usages);
2564}
2565
2566/*
2567 * This function provides the context switch callback to the lower code
2568 * layer. It is invoked ONLY when the context switch callback is enabled.
2569 */
2570static void perf_pmu_sched_task(struct task_struct *prev,
2571 struct task_struct *next,
2572 bool sched_in)
2573{
2574 struct perf_cpu_context *cpuctx;
2575 struct pmu *pmu;
2576 unsigned long flags;
2577
2578 if (prev == next)
2579 return;
2580
2581 local_irq_save(flags);
2582
2583 rcu_read_lock();
2584
2585 list_for_each_entry_rcu(pmu, &pmus, entry) {
2586 if (pmu->sched_task) {
2587 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2588
2589 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2590
2591 perf_pmu_disable(pmu);
2592
2593 pmu->sched_task(cpuctx->task_ctx, sched_in);
2594
2595 perf_pmu_enable(pmu);
2596
2597 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2598 }
2599 }
2600
2601 rcu_read_unlock();
2602
2603 local_irq_restore(flags);
2604}
2605
Adrian Hunter45ac1402015-07-21 12:44:02 +03002606static void perf_event_switch(struct task_struct *task,
2607 struct task_struct *next_prev, bool sched_in);
2608
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002609#define for_each_task_context_nr(ctxn) \
2610 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2611
2612/*
2613 * Called from scheduler to remove the events of the current task,
2614 * with interrupts disabled.
2615 *
2616 * We stop each event and update the event value in event->count.
2617 *
2618 * This does not protect us against NMI, but disable()
2619 * sets the disabled bit in the control field of event _before_
2620 * accessing the event control register. If a NMI hits, then it will
2621 * not restart the event.
2622 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002623void __perf_event_task_sched_out(struct task_struct *task,
2624 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002625{
2626 int ctxn;
2627
Yan, Zhengba532502014-11-04 21:55:58 -05002628 if (__this_cpu_read(perf_sched_cb_usages))
2629 perf_pmu_sched_task(task, next, false);
2630
Adrian Hunter45ac1402015-07-21 12:44:02 +03002631 if (atomic_read(&nr_switch_events))
2632 perf_event_switch(task, next, false);
2633
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002634 for_each_task_context_nr(ctxn)
2635 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002636
2637 /*
2638 * if cgroup events exist on this CPU, then we need
2639 * to check if we have to switch out PMU state.
2640 * cgroup event are system-wide mode only
2641 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002642 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002643 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002644}
2645
Peter Zijlstra3e349502016-01-08 10:01:18 +01002646static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2647 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002648{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002649 if (!cpuctx->task_ctx)
2650 return;
2651
2652 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2653 return;
2654
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002655 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002656}
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;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002734
Peter Zijlstrac994d612016-01-08 09:20:23 +01002735 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002736
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002737 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002738 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002739
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002740 ctx->is_active |= event_type;
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002741 if (ctx->task) {
2742 if (!is_active)
2743 cpuctx->task_ctx = ctx;
2744 else
2745 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2746 }
2747
Stephane Eraniane5d13672011-02-14 11:20:01 +02002748 now = perf_clock();
2749 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002750 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002751 /*
2752 * First go through the list and put on any pinned groups
2753 * in order to give them the best chance of going on.
2754 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002755 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002756 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002757
2758 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002759 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002760 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002761}
2762
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002763static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002764 enum event_type_t event_type,
2765 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002766{
2767 struct perf_event_context *ctx = &cpuctx->ctx;
2768
Stephane Eraniane5d13672011-02-14 11:20:01 +02002769 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002770}
2771
Stephane Eraniane5d13672011-02-14 11:20:01 +02002772static void perf_event_context_sched_in(struct perf_event_context *ctx,
2773 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002774{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002775 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002776
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002777 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002778 if (cpuctx->task_ctx == ctx)
2779 return;
2780
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002781 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002782 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002783 /*
2784 * We want to keep the following priority order:
2785 * cpu pinned (that don't need to move), task pinned,
2786 * cpu flexible, task flexible.
2787 */
2788 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002789 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002790 perf_pmu_enable(ctx->pmu);
2791 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002792}
2793
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002794/*
2795 * Called from scheduler to add the events of the current task
2796 * with interrupts disabled.
2797 *
2798 * We restore the event value and then enable it.
2799 *
2800 * This does not protect us against NMI, but enable()
2801 * sets the enabled bit in the control field of event _before_
2802 * accessing the event control register. If a NMI hits, then it will
2803 * keep the event running.
2804 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002805void __perf_event_task_sched_in(struct task_struct *prev,
2806 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002807{
2808 struct perf_event_context *ctx;
2809 int ctxn;
2810
Peter Zijlstra7e41d172016-01-08 09:21:40 +01002811 /*
2812 * If cgroup events exist on this CPU, then we need to check if we have
2813 * to switch in PMU state; cgroup event are system-wide mode only.
2814 *
2815 * Since cgroup events are CPU events, we must schedule these in before
2816 * we schedule in the task events.
2817 */
2818 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
2819 perf_cgroup_sched_in(prev, task);
2820
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002821 for_each_task_context_nr(ctxn) {
2822 ctx = task->perf_event_ctxp[ctxn];
2823 if (likely(!ctx))
2824 continue;
2825
Stephane Eraniane5d13672011-02-14 11:20:01 +02002826 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002827 }
Stephane Eraniand010b332012-02-09 23:21:00 +01002828
Adrian Hunter45ac1402015-07-21 12:44:02 +03002829 if (atomic_read(&nr_switch_events))
2830 perf_event_switch(task, prev, true);
2831
Yan, Zhengba532502014-11-04 21:55:58 -05002832 if (__this_cpu_read(perf_sched_cb_usages))
2833 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002834}
2835
Peter Zijlstraabd50712010-01-26 18:50:16 +01002836static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2837{
2838 u64 frequency = event->attr.sample_freq;
2839 u64 sec = NSEC_PER_SEC;
2840 u64 divisor, dividend;
2841
2842 int count_fls, nsec_fls, frequency_fls, sec_fls;
2843
2844 count_fls = fls64(count);
2845 nsec_fls = fls64(nsec);
2846 frequency_fls = fls64(frequency);
2847 sec_fls = 30;
2848
2849 /*
2850 * We got @count in @nsec, with a target of sample_freq HZ
2851 * the target period becomes:
2852 *
2853 * @count * 10^9
2854 * period = -------------------
2855 * @nsec * sample_freq
2856 *
2857 */
2858
2859 /*
2860 * Reduce accuracy by one bit such that @a and @b converge
2861 * to a similar magnitude.
2862 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002863#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002864do { \
2865 if (a##_fls > b##_fls) { \
2866 a >>= 1; \
2867 a##_fls--; \
2868 } else { \
2869 b >>= 1; \
2870 b##_fls--; \
2871 } \
2872} while (0)
2873
2874 /*
2875 * Reduce accuracy until either term fits in a u64, then proceed with
2876 * the other, so that finally we can do a u64/u64 division.
2877 */
2878 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2879 REDUCE_FLS(nsec, frequency);
2880 REDUCE_FLS(sec, count);
2881 }
2882
2883 if (count_fls + sec_fls > 64) {
2884 divisor = nsec * frequency;
2885
2886 while (count_fls + sec_fls > 64) {
2887 REDUCE_FLS(count, sec);
2888 divisor >>= 1;
2889 }
2890
2891 dividend = count * sec;
2892 } else {
2893 dividend = count * sec;
2894
2895 while (nsec_fls + frequency_fls > 64) {
2896 REDUCE_FLS(nsec, frequency);
2897 dividend >>= 1;
2898 }
2899
2900 divisor = nsec * frequency;
2901 }
2902
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002903 if (!divisor)
2904 return dividend;
2905
Peter Zijlstraabd50712010-01-26 18:50:16 +01002906 return div64_u64(dividend, divisor);
2907}
2908
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002909static DEFINE_PER_CPU(int, perf_throttled_count);
2910static DEFINE_PER_CPU(u64, perf_throttled_seq);
2911
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002912static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002913{
2914 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002915 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002916 s64 delta;
2917
Peter Zijlstraabd50712010-01-26 18:50:16 +01002918 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002919
2920 delta = (s64)(period - hwc->sample_period);
2921 delta = (delta + 7) / 8; /* low pass filter */
2922
2923 sample_period = hwc->sample_period + delta;
2924
2925 if (!sample_period)
2926 sample_period = 1;
2927
2928 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002929
Peter Zijlstrae7850592010-05-21 14:43:08 +02002930 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002931 if (disable)
2932 event->pmu->stop(event, PERF_EF_UPDATE);
2933
Peter Zijlstrae7850592010-05-21 14:43:08 +02002934 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002935
2936 if (disable)
2937 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002938 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002939}
2940
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002941/*
2942 * combine freq adjustment with unthrottling to avoid two passes over the
2943 * events. At the same time, make sure, having freq events does not change
2944 * the rate of unthrottling as that would introduce bias.
2945 */
2946static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2947 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002948{
2949 struct perf_event *event;
2950 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002951 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002952 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002953
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002954 /*
2955 * only need to iterate over all events iff:
2956 * - context have events in frequency mode (needs freq adjust)
2957 * - there are events to unthrottle on this cpu
2958 */
2959 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002960 return;
2961
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002962 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002963 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002964
Paul Mackerras03541f82009-10-14 16:58:03 +11002965 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002966 if (event->state != PERF_EVENT_STATE_ACTIVE)
2967 continue;
2968
Stephane Eranian5632ab12011-01-03 18:20:01 +02002969 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002970 continue;
2971
Alexander Shishkin44377272013-12-16 14:17:36 +02002972 perf_pmu_disable(event->pmu);
2973
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002974 hwc = &event->hw;
2975
Jiri Olsaae23bff2013-08-24 16:45:54 +02002976 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002977 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002978 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002979 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002980 }
2981
2982 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02002983 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002984
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002985 /*
2986 * stop the event and update event->count
2987 */
2988 event->pmu->stop(event, PERF_EF_UPDATE);
2989
Peter Zijlstrae7850592010-05-21 14:43:08 +02002990 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002991 delta = now - hwc->freq_count_stamp;
2992 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002993
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002994 /*
2995 * restart the event
2996 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002997 * we have stopped the event so tell that
2998 * to perf_adjust_period() to avoid stopping it
2999 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003000 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003001 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003002 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003003
3004 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003005 next:
3006 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003007 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003008
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003009 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003010 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003011}
3012
3013/*
3014 * Round-robin a context's events:
3015 */
3016static void rotate_ctx(struct perf_event_context *ctx)
3017{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003018 /*
3019 * Rotate the first entry last of non-pinned groups. Rotation might be
3020 * disabled by the inheritance code.
3021 */
3022 if (!ctx->rotate_disable)
3023 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024}
3025
Stephane Eranian9e630202013-04-03 14:21:33 +02003026static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003027{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003028 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003029 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003030
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003031 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003032 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3033 rotate = 1;
3034 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003035
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003036 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003037 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003038 if (ctx->nr_events != ctx->nr_active)
3039 rotate = 1;
3040 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003041
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003042 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003043 goto done;
3044
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003045 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003046 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003047
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003048 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3049 if (ctx)
3050 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003051
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003052 rotate_ctx(&cpuctx->ctx);
3053 if (ctx)
3054 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003055
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003056 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003057
3058 perf_pmu_enable(cpuctx->ctx.pmu);
3059 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003060done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003061
3062 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003063}
3064
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003065#ifdef CONFIG_NO_HZ_FULL
3066bool perf_event_can_stop_tick(void)
3067{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003068 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003069 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003070 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003071 else
3072 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003073}
3074#endif
3075
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003076void perf_event_task_tick(void)
3077{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003078 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3079 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003080 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003081
3082 WARN_ON(!irqs_disabled());
3083
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003084 __this_cpu_inc(perf_throttled_seq);
3085 throttled = __this_cpu_xchg(perf_throttled_count, 0);
3086
Mark Rutland2fde4f92015-01-07 15:01:54 +00003087 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003088 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003089}
3090
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003091static int event_enable_on_exec(struct perf_event *event,
3092 struct perf_event_context *ctx)
3093{
3094 if (!event->attr.enable_on_exec)
3095 return 0;
3096
3097 event->attr.enable_on_exec = 0;
3098 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3099 return 0;
3100
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003101 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003102
3103 return 1;
3104}
3105
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003106/*
3107 * Enable all of a task's events that have been marked enable-on-exec.
3108 * This expects task == current.
3109 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003110static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003111{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003112 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003113 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003114 struct perf_event *event;
3115 unsigned long flags;
3116 int enabled = 0;
3117
3118 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003119 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003120 if (!ctx || !ctx->nr_events)
3121 goto out;
3122
Peter Zijlstra3e349502016-01-08 10:01:18 +01003123 cpuctx = __get_cpu_context(ctx);
3124 perf_ctx_lock(cpuctx, ctx);
3125 list_for_each_entry(event, &ctx->event_list, event_entry)
3126 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003127
3128 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003129 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003130 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003131 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003132 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003133 ctx_resched(cpuctx, ctx);
3134 }
3135 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003136
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003137out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003138 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003139
3140 if (clone_ctx)
3141 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003142}
3143
Peter Zijlstrae041e322014-05-21 17:32:19 +02003144void perf_event_exec(void)
3145{
Peter Zijlstrae041e322014-05-21 17:32:19 +02003146 int ctxn;
3147
3148 rcu_read_lock();
Peter Zijlstrac1274492015-12-10 20:57:40 +01003149 for_each_task_context_nr(ctxn)
3150 perf_event_enable_on_exec(ctxn);
Peter Zijlstrae041e322014-05-21 17:32:19 +02003151 rcu_read_unlock();
3152}
3153
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003154struct perf_read_data {
3155 struct perf_event *event;
3156 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003157 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003158};
3159
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003160/*
3161 * Cross CPU call to read the hardware event
3162 */
3163static void __perf_event_read(void *info)
3164{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003165 struct perf_read_data *data = info;
3166 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003167 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003168 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003169 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003170
3171 /*
3172 * If this is a task context, we need to check whether it is
3173 * the current task context of this cpu. If not it has been
3174 * scheduled out before the smp call arrived. In that case
3175 * event->count would have been updated to a recent sample
3176 * when the event was scheduled out.
3177 */
3178 if (ctx->task && cpuctx->task_ctx != ctx)
3179 return;
3180
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003181 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003182 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003183 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003184 update_cgrp_time_from_event(event);
3185 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003186
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003187 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003188 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003189 goto unlock;
3190
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003191 if (!data->group) {
3192 pmu->read(event);
3193 data->ret = 0;
3194 goto unlock;
3195 }
3196
3197 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3198
3199 pmu->read(event);
3200
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003201 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3202 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003203 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3204 /*
3205 * Use sibling's PMU rather than @event's since
3206 * sibling could be on different (eg: software) PMU.
3207 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003208 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003209 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003210 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003211
3212 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003213
3214unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003215 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003216}
3217
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003218static inline u64 perf_event_count(struct perf_event *event)
3219{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003220 if (event->pmu->count)
3221 return event->pmu->count(event);
3222
3223 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003224}
3225
Kaixu Xiaffe86902015-08-06 07:02:32 +00003226/*
3227 * NMI-safe method to read a local event, that is an event that
3228 * is:
3229 * - either for the current task, or for this CPU
3230 * - does not have inherit set, for inherited task events
3231 * will not be local and we cannot read them atomically
3232 * - must not have a pmu::count method
3233 */
3234u64 perf_event_read_local(struct perf_event *event)
3235{
3236 unsigned long flags;
3237 u64 val;
3238
3239 /*
3240 * Disabling interrupts avoids all counter scheduling (context
3241 * switches, timer based rotation and IPIs).
3242 */
3243 local_irq_save(flags);
3244
3245 /* If this is a per-task event, it must be for current */
3246 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3247 event->hw.target != current);
3248
3249 /* If this is a per-CPU event, it must be for this CPU */
3250 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3251 event->cpu != smp_processor_id());
3252
3253 /*
3254 * It must not be an event with inherit set, we cannot read
3255 * all child counters from atomic context.
3256 */
3257 WARN_ON_ONCE(event->attr.inherit);
3258
3259 /*
3260 * It must not have a pmu::count method, those are not
3261 * NMI safe.
3262 */
3263 WARN_ON_ONCE(event->pmu->count);
3264
3265 /*
3266 * If the event is currently on this CPU, its either a per-task event,
3267 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3268 * oncpu == -1).
3269 */
3270 if (event->oncpu == smp_processor_id())
3271 event->pmu->read(event);
3272
3273 val = local64_read(&event->count);
3274 local_irq_restore(flags);
3275
3276 return val;
3277}
3278
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003279static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003280{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003281 int ret = 0;
3282
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003283 /*
3284 * If event is enabled and currently active on a CPU, update the
3285 * value in the event structure:
3286 */
3287 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003288 struct perf_read_data data = {
3289 .event = event,
3290 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003291 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003292 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003293 smp_call_function_single(event->oncpu,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003294 __perf_event_read, &data, 1);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003295 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003296 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003297 struct perf_event_context *ctx = event->ctx;
3298 unsigned long flags;
3299
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003300 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003301 /*
3302 * may read while context is not active
3303 * (e.g., thread is blocked), in that case
3304 * we cannot update context time
3305 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003306 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003307 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003308 update_cgrp_time_from_event(event);
3309 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003310 if (group)
3311 update_group_times(event);
3312 else
3313 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003314 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003315 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003316
3317 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003318}
3319
3320/*
3321 * Initialize the perf_event context in a task_struct:
3322 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003323static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003324{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003325 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003326 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003327 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003328 INIT_LIST_HEAD(&ctx->pinned_groups);
3329 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003330 INIT_LIST_HEAD(&ctx->event_list);
3331 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003332}
3333
Peter Zijlstraeb184472010-09-07 15:55:13 +02003334static struct perf_event_context *
3335alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003336{
3337 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003338
3339 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3340 if (!ctx)
3341 return NULL;
3342
3343 __perf_event_init_context(ctx);
3344 if (task) {
3345 ctx->task = task;
3346 get_task_struct(task);
3347 }
3348 ctx->pmu = pmu;
3349
3350 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003351}
3352
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003353static struct task_struct *
3354find_lively_task_by_vpid(pid_t vpid)
3355{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003356 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003357 int err;
3358
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003359 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003360 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003361 task = current;
3362 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003363 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003364 if (task)
3365 get_task_struct(task);
3366 rcu_read_unlock();
3367
3368 if (!task)
3369 return ERR_PTR(-ESRCH);
3370
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003371 /* Reuse ptrace permission checks for now. */
3372 err = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08003373 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003374 goto errout;
3375
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003376 return task;
3377errout:
3378 put_task_struct(task);
3379 return ERR_PTR(err);
3380
3381}
3382
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003383/*
3384 * Returns a matching context with refcount and pincount.
3385 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003386static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003387find_get_context(struct pmu *pmu, struct task_struct *task,
3388 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003389{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003390 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003391 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003392 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003393 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003394 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003395 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003396
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003397 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003398 /* Must be root to operate on a CPU event: */
3399 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3400 return ERR_PTR(-EACCES);
3401
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402 /*
3403 * We could be clever and allow to attach a event to an
3404 * offline CPU and activate it when the CPU comes up, but
3405 * that's for later.
3406 */
3407 if (!cpu_online(cpu))
3408 return ERR_PTR(-ENODEV);
3409
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003410 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003411 ctx = &cpuctx->ctx;
3412 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003413 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003414
3415 return ctx;
3416 }
3417
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003418 err = -EINVAL;
3419 ctxn = pmu->task_ctx_nr;
3420 if (ctxn < 0)
3421 goto errout;
3422
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003423 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3424 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3425 if (!task_ctx_data) {
3426 err = -ENOMEM;
3427 goto errout;
3428 }
3429 }
3430
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003431retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003432 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003433 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003434 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003435 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003436
3437 if (task_ctx_data && !ctx->task_ctx_data) {
3438 ctx->task_ctx_data = task_ctx_data;
3439 task_ctx_data = NULL;
3440 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003441 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003442
3443 if (clone_ctx)
3444 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003445 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003446 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003447 err = -ENOMEM;
3448 if (!ctx)
3449 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003450
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003451 if (task_ctx_data) {
3452 ctx->task_ctx_data = task_ctx_data;
3453 task_ctx_data = NULL;
3454 }
3455
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003456 err = 0;
3457 mutex_lock(&task->perf_event_mutex);
3458 /*
3459 * If it has already passed perf_event_exit_task().
3460 * we must see PF_EXITING, it takes this mutex too.
3461 */
3462 if (task->flags & PF_EXITING)
3463 err = -ESRCH;
3464 else if (task->perf_event_ctxp[ctxn])
3465 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003466 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003467 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003468 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003469 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003470 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003471 mutex_unlock(&task->perf_event_mutex);
3472
3473 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003474 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003475
3476 if (err == -EAGAIN)
3477 goto retry;
3478 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003479 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003480 }
3481
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003482 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003483 return ctx;
3484
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003485errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003486 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003487 return ERR_PTR(err);
3488}
3489
Li Zefan6fb29152009-10-15 11:21:42 +08003490static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003491static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003492
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003493static void free_event_rcu(struct rcu_head *head)
3494{
3495 struct perf_event *event;
3496
3497 event = container_of(head, struct perf_event, rcu_head);
3498 if (event->ns)
3499 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003500 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003501 kfree(event);
3502}
3503
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003504static void ring_buffer_attach(struct perf_event *event,
3505 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003506
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003507static void unaccount_event_cpu(struct perf_event *event, int cpu)
3508{
3509 if (event->parent)
3510 return;
3511
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003512 if (is_cgroup_event(event))
3513 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3514}
3515
3516static void unaccount_event(struct perf_event *event)
3517{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003518 bool dec = false;
3519
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003520 if (event->parent)
3521 return;
3522
3523 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003524 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003525 if (event->attr.mmap || event->attr.mmap_data)
3526 atomic_dec(&nr_mmap_events);
3527 if (event->attr.comm)
3528 atomic_dec(&nr_comm_events);
3529 if (event->attr.task)
3530 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003531 if (event->attr.freq)
3532 atomic_dec(&nr_freq_events);
Adrian Hunter45ac1402015-07-21 12:44:02 +03003533 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003534 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003535 atomic_dec(&nr_switch_events);
3536 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003537 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003538 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003539 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003540 dec = true;
3541
3542 if (dec)
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003543 static_key_slow_dec_deferred(&perf_sched_events);
3544
3545 unaccount_event_cpu(event, event->cpu);
3546}
3547
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003548/*
3549 * The following implement mutual exclusion of events on "exclusive" pmus
3550 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3551 * at a time, so we disallow creating events that might conflict, namely:
3552 *
3553 * 1) cpu-wide events in the presence of per-task events,
3554 * 2) per-task events in the presence of cpu-wide events,
3555 * 3) two matching events on the same context.
3556 *
3557 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003558 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003559 */
3560static int exclusive_event_init(struct perf_event *event)
3561{
3562 struct pmu *pmu = event->pmu;
3563
3564 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3565 return 0;
3566
3567 /*
3568 * Prevent co-existence of per-task and cpu-wide events on the
3569 * same exclusive pmu.
3570 *
3571 * Negative pmu::exclusive_cnt means there are cpu-wide
3572 * events on this "exclusive" pmu, positive means there are
3573 * per-task events.
3574 *
3575 * Since this is called in perf_event_alloc() path, event::ctx
3576 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3577 * to mean "per-task event", because unlike other attach states it
3578 * never gets cleared.
3579 */
3580 if (event->attach_state & PERF_ATTACH_TASK) {
3581 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
3582 return -EBUSY;
3583 } else {
3584 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
3585 return -EBUSY;
3586 }
3587
3588 return 0;
3589}
3590
3591static void exclusive_event_destroy(struct perf_event *event)
3592{
3593 struct pmu *pmu = event->pmu;
3594
3595 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3596 return;
3597
3598 /* see comment in exclusive_event_init() */
3599 if (event->attach_state & PERF_ATTACH_TASK)
3600 atomic_dec(&pmu->exclusive_cnt);
3601 else
3602 atomic_inc(&pmu->exclusive_cnt);
3603}
3604
3605static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
3606{
3607 if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
3608 (e1->cpu == e2->cpu ||
3609 e1->cpu == -1 ||
3610 e2->cpu == -1))
3611 return true;
3612 return false;
3613}
3614
3615/* Called under the same ctx::mutex as perf_install_in_context() */
3616static bool exclusive_event_installable(struct perf_event *event,
3617 struct perf_event_context *ctx)
3618{
3619 struct perf_event *iter_event;
3620 struct pmu *pmu = event->pmu;
3621
3622 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3623 return true;
3624
3625 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
3626 if (exclusive_event_match(iter_event, event))
3627 return false;
3628 }
3629
3630 return true;
3631}
3632
Peter Zijlstra683ede42014-05-05 12:11:24 +02003633static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003634{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003635 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003636
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003637 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003638
Frederic Weisbecker76369132011-05-19 19:55:04 +02003639 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003640 /*
3641 * Can happen when we close an event with re-directed output.
3642 *
3643 * Since we have a 0 refcount, perf_mmap_close() will skip
3644 * over us; possibly making our ring_buffer_put() the last.
3645 */
3646 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003647 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003648 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003649 }
3650
Stephane Eraniane5d13672011-02-14 11:20:01 +02003651 if (is_cgroup_event(event))
3652 perf_detach_cgroup(event);
3653
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003654 if (!event->parent) {
3655 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3656 put_callchain_buffers();
3657 }
3658
3659 perf_event_free_bpf_prog(event);
3660
3661 if (event->destroy)
3662 event->destroy(event);
3663
3664 if (event->ctx)
3665 put_ctx(event->ctx);
3666
3667 if (event->pmu) {
3668 exclusive_event_destroy(event);
3669 module_put(event->pmu->module);
3670 }
3671
3672 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003673}
3674
Peter Zijlstra683ede42014-05-05 12:11:24 +02003675/*
3676 * Used to free events which have a known refcount of 1, such as in error paths
3677 * where the event isn't exposed yet and inherited events.
3678 */
3679static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003680{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003681 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3682 "unexpected event refcount: %ld; ptr=%p\n",
3683 atomic_long_read(&event->refcount), event)) {
3684 /* leak to avoid use-after-free */
3685 return;
3686 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003687
Peter Zijlstra683ede42014-05-05 12:11:24 +02003688 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003689}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003690
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003691/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003692 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003693 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003694static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003695{
Peter Zijlstra88821352010-11-09 19:01:43 +01003696 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003697
Peter Zijlstra88821352010-11-09 19:01:43 +01003698 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01003699 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003700 * Matches the smp_store_release() in perf_event_exit_task(). If we
3701 * observe !owner it means the list deletion is complete and we can
3702 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01003703 * owner->perf_event_mutex.
3704 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003705 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01003706 if (owner) {
3707 /*
3708 * Since delayed_put_task_struct() also drops the last
3709 * task reference we can safely take a new reference
3710 * while holding the rcu_read_lock().
3711 */
3712 get_task_struct(owner);
3713 }
3714 rcu_read_unlock();
3715
3716 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003717 /*
3718 * If we're here through perf_event_exit_task() we're already
3719 * holding ctx->mutex which would be an inversion wrt. the
3720 * normal lock order.
3721 *
3722 * However we can safely take this lock because its the child
3723 * ctx->mutex.
3724 */
3725 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
3726
Peter Zijlstra88821352010-11-09 19:01:43 +01003727 /*
3728 * We have to re-check the event->owner field, if it is cleared
3729 * we raced with perf_event_exit_task(), acquiring the mutex
3730 * ensured they're done, and we can proceed with freeing the
3731 * event.
3732 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003733 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01003734 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003735 smp_store_release(&event->owner, NULL);
3736 }
Peter Zijlstra88821352010-11-09 19:01:43 +01003737 mutex_unlock(&owner->perf_event_mutex);
3738 put_task_struct(owner);
3739 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003740}
3741
Jiri Olsaf8697762014-08-01 14:33:01 +02003742static void put_event(struct perf_event *event)
3743{
Jiri Olsaf8697762014-08-01 14:33:01 +02003744 if (!atomic_long_dec_and_test(&event->refcount))
3745 return;
3746
Peter Zijlstra683ede42014-05-05 12:11:24 +02003747 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003748}
3749
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003750/*
3751 * Kill an event dead; while event:refcount will preserve the event
3752 * object, it will not preserve its functionality. Once the last 'user'
3753 * gives up the object, we'll destroy the thing.
3754 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02003755int perf_event_release_kernel(struct perf_event *event)
3756{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003757 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003758 struct perf_event *child, *tmp;
3759
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003760 /*
3761 * If we got here through err_file: fput(event_file); we will not have
3762 * attached to a context yet.
3763 */
3764 if (!ctx) {
3765 WARN_ON_ONCE(event->attach_state &
3766 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
3767 goto no_ctx;
3768 }
3769
Peter Zijlstra88821352010-11-09 19:01:43 +01003770 if (!is_kernel_event(event))
3771 perf_remove_from_owner(event);
3772
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01003773 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02003774 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01003775 perf_remove_from_context(event, DETACH_GROUP | DETACH_STATE);
Peter Zijlstra88821352010-11-09 19:01:43 +01003776 perf_event_ctx_unlock(event, ctx);
3777
Peter Zijlstra60beda82016-01-26 14:55:02 +01003778 /*
3779 * At this point we must have event->state == PERF_EVENT_STATE_EXIT,
3780 * either from the above perf_remove_from_context() or through
3781 * perf_event_exit_event().
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003782 *
3783 * Therefore, anybody acquiring event->child_mutex after the below
3784 * loop _must_ also see this, most importantly inherit_event() which
3785 * will avoid placing more children on the list.
3786 *
3787 * Thus this guarantees that we will in fact observe and kill _ALL_
3788 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01003789 */
3790 WARN_ON_ONCE(event->state != PERF_EVENT_STATE_EXIT);
3791
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003792again:
3793 mutex_lock(&event->child_mutex);
3794 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01003795
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003796 /*
3797 * Cannot change, child events are not migrated, see the
3798 * comment with perf_event_ctx_lock_nested().
3799 */
3800 ctx = lockless_dereference(child->ctx);
3801 /*
3802 * Since child_mutex nests inside ctx::mutex, we must jump
3803 * through hoops. We start by grabbing a reference on the ctx.
3804 *
3805 * Since the event cannot get freed while we hold the
3806 * child_mutex, the context must also exist and have a !0
3807 * reference count.
3808 */
3809 get_ctx(ctx);
3810
3811 /*
3812 * Now that we have a ctx ref, we can drop child_mutex, and
3813 * acquire ctx::mutex without fear of it going away. Then we
3814 * can re-acquire child_mutex.
3815 */
3816 mutex_unlock(&event->child_mutex);
3817 mutex_lock(&ctx->mutex);
3818 mutex_lock(&event->child_mutex);
3819
3820 /*
3821 * Now that we hold ctx::mutex and child_mutex, revalidate our
3822 * state, if child is still the first entry, it didn't get freed
3823 * and we can continue doing so.
3824 */
3825 tmp = list_first_entry_or_null(&event->child_list,
3826 struct perf_event, child_list);
3827 if (tmp == child) {
3828 perf_remove_from_context(child, DETACH_GROUP);
3829 list_del(&child->child_list);
3830 free_event(child);
3831 /*
3832 * This matches the refcount bump in inherit_event();
3833 * this can't be the last reference.
3834 */
3835 put_event(event);
3836 }
3837
3838 mutex_unlock(&event->child_mutex);
3839 mutex_unlock(&ctx->mutex);
3840 put_ctx(ctx);
3841 goto again;
3842 }
3843 mutex_unlock(&event->child_mutex);
3844
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003845no_ctx:
3846 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02003847 return 0;
3848}
3849EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3850
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02003851/*
3852 * Called when the last reference to the file is gone.
3853 */
Al Viroa6fa9412012-08-20 14:59:25 +01003854static int perf_release(struct inode *inode, struct file *file)
3855{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003856 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01003857 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003858}
3859
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003860u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003861{
3862 struct perf_event *child;
3863 u64 total = 0;
3864
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003865 *enabled = 0;
3866 *running = 0;
3867
Peter Zijlstra6f105812009-11-20 22:19:56 +01003868 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003869
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003870 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003871 total += perf_event_count(event);
3872
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003873 *enabled += event->total_time_enabled +
3874 atomic64_read(&event->child_total_time_enabled);
3875 *running += event->total_time_running +
3876 atomic64_read(&event->child_total_time_running);
3877
3878 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003879 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003880 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003881 *enabled += child->total_time_enabled;
3882 *running += child->total_time_running;
3883 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003884 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003885
3886 return total;
3887}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003888EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003889
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003890static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003891 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003892{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003893 struct perf_event *sub;
3894 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003895 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003896
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003897 ret = perf_event_read(leader, true);
3898 if (ret)
3899 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003900
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003901 /*
3902 * Since we co-schedule groups, {enabled,running} times of siblings
3903 * will be identical to those of the leader, so we only publish one
3904 * set.
3905 */
3906 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3907 values[n++] += leader->total_time_enabled +
3908 atomic64_read(&leader->child_total_time_enabled);
3909 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003910
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003911 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3912 values[n++] += leader->total_time_running +
3913 atomic64_read(&leader->child_total_time_running);
3914 }
3915
3916 /*
3917 * Write {count,id} tuples for every sibling.
3918 */
3919 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003920 if (read_format & PERF_FORMAT_ID)
3921 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003922
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003923 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003924 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003925 if (read_format & PERF_FORMAT_ID)
3926 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003927 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003928
3929 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003930}
3931
3932static int perf_read_group(struct perf_event *event,
3933 u64 read_format, char __user *buf)
3934{
3935 struct perf_event *leader = event->group_leader, *child;
3936 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003937 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003938 u64 *values;
3939
3940 lockdep_assert_held(&ctx->mutex);
3941
3942 values = kzalloc(event->read_size, GFP_KERNEL);
3943 if (!values)
3944 return -ENOMEM;
3945
3946 values[0] = 1 + leader->nr_siblings;
3947
3948 /*
3949 * By locking the child_mutex of the leader we effectively
3950 * lock the child list of all siblings.. XXX explain how.
3951 */
3952 mutex_lock(&leader->child_mutex);
3953
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003954 ret = __perf_read_group_add(leader, read_format, values);
3955 if (ret)
3956 goto unlock;
3957
3958 list_for_each_entry(child, &leader->child_list, child_list) {
3959 ret = __perf_read_group_add(child, read_format, values);
3960 if (ret)
3961 goto unlock;
3962 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003963
3964 mutex_unlock(&leader->child_mutex);
3965
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003966 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003967 if (copy_to_user(buf, values, event->read_size))
3968 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003969 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003970
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003971unlock:
3972 mutex_unlock(&leader->child_mutex);
3973out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003974 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003975 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003976}
3977
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07003978static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003979 u64 read_format, char __user *buf)
3980{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003981 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003982 u64 values[4];
3983 int n = 0;
3984
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003985 values[n++] = perf_event_read_value(event, &enabled, &running);
3986 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3987 values[n++] = enabled;
3988 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3989 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003990 if (read_format & PERF_FORMAT_ID)
3991 values[n++] = primary_event_id(event);
3992
3993 if (copy_to_user(buf, values, n * sizeof(u64)))
3994 return -EFAULT;
3995
3996 return n * sizeof(u64);
3997}
3998
Jiri Olsadc633982014-09-12 13:18:26 +02003999static bool is_event_hup(struct perf_event *event)
4000{
4001 bool no_children;
4002
4003 if (event->state != PERF_EVENT_STATE_EXIT)
4004 return false;
4005
4006 mutex_lock(&event->child_mutex);
4007 no_children = list_empty(&event->child_list);
4008 mutex_unlock(&event->child_mutex);
4009 return no_children;
4010}
4011
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004012/*
4013 * Read the performance event - simple non blocking version for now
4014 */
4015static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004016__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004017{
4018 u64 read_format = event->attr.read_format;
4019 int ret;
4020
4021 /*
4022 * Return end-of-file for a read on a event that is in
4023 * error state (i.e. because it was pinned but it couldn't be
4024 * scheduled on to the CPU at some point).
4025 */
4026 if (event->state == PERF_EVENT_STATE_ERROR)
4027 return 0;
4028
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004029 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004030 return -ENOSPC;
4031
4032 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004033 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004034 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004035 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004036 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004037
4038 return ret;
4039}
4040
4041static ssize_t
4042perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4043{
4044 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004045 struct perf_event_context *ctx;
4046 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004047
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004048 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004049 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004050 perf_event_ctx_unlock(event, ctx);
4051
4052 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004053}
4054
4055static unsigned int perf_poll(struct file *file, poll_table *wait)
4056{
4057 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004058 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004059 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004060
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004061 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004062
Jiri Olsadc633982014-09-12 13:18:26 +02004063 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004064 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004065
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004066 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004067 * Pin the event->rb by taking event->mmap_mutex; otherwise
4068 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004069 */
4070 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004071 rb = event->rb;
4072 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004073 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004074 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004075 return events;
4076}
4077
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004078static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004079{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004080 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004081 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004082 perf_event_update_userpage(event);
4083}
4084
4085/*
4086 * Holding the top-level event's child_mutex means that any
4087 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004088 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004089 * task existence requirements of perf_event_enable/disable.
4090 */
4091static void perf_event_for_each_child(struct perf_event *event,
4092 void (*func)(struct perf_event *))
4093{
4094 struct perf_event *child;
4095
4096 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004097
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004098 mutex_lock(&event->child_mutex);
4099 func(event);
4100 list_for_each_entry(child, &event->child_list, child_list)
4101 func(child);
4102 mutex_unlock(&event->child_mutex);
4103}
4104
4105static void perf_event_for_each(struct perf_event *event,
4106 void (*func)(struct perf_event *))
4107{
4108 struct perf_event_context *ctx = event->ctx;
4109 struct perf_event *sibling;
4110
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004111 lockdep_assert_held(&ctx->mutex);
4112
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004113 event = event->group_leader;
4114
4115 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004116 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004117 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004118}
4119
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004120static void __perf_event_period(struct perf_event *event,
4121 struct perf_cpu_context *cpuctx,
4122 struct perf_event_context *ctx,
4123 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004124{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004125 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004126 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004127
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004128 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004129 event->attr.sample_freq = value;
4130 } else {
4131 event->attr.sample_period = value;
4132 event->hw.sample_period = value;
4133 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004134
4135 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4136 if (active) {
4137 perf_pmu_disable(ctx->pmu);
4138 event->pmu->stop(event, PERF_EF_UPDATE);
4139 }
4140
4141 local64_set(&event->hw.period_left, 0);
4142
4143 if (active) {
4144 event->pmu->start(event, PERF_EF_RELOAD);
4145 perf_pmu_enable(ctx->pmu);
4146 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004147}
4148
4149static int perf_event_period(struct perf_event *event, u64 __user *arg)
4150{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004151 u64 value;
4152
4153 if (!is_sampling_event(event))
4154 return -EINVAL;
4155
4156 if (copy_from_user(&value, arg, sizeof(value)))
4157 return -EFAULT;
4158
4159 if (!value)
4160 return -EINVAL;
4161
4162 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4163 return -EINVAL;
4164
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004165 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004166
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004167 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004168}
4169
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004170static const struct file_operations perf_fops;
4171
Al Viro2903ff02012-08-28 12:52:22 -04004172static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004173{
Al Viro2903ff02012-08-28 12:52:22 -04004174 struct fd f = fdget(fd);
4175 if (!f.file)
4176 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004177
Al Viro2903ff02012-08-28 12:52:22 -04004178 if (f.file->f_op != &perf_fops) {
4179 fdput(f);
4180 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004181 }
Al Viro2903ff02012-08-28 12:52:22 -04004182 *p = f;
4183 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004184}
4185
4186static int perf_event_set_output(struct perf_event *event,
4187 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004188static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004189static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004190
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004191static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004192{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004193 void (*func)(struct perf_event *);
4194 u32 flags = arg;
4195
4196 switch (cmd) {
4197 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004198 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004199 break;
4200 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004201 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004202 break;
4203 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004204 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004205 break;
4206
4207 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004208 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004209
4210 case PERF_EVENT_IOC_PERIOD:
4211 return perf_event_period(event, (u64 __user *)arg);
4212
Jiri Olsacf4957f2012-10-24 13:37:58 +02004213 case PERF_EVENT_IOC_ID:
4214 {
4215 u64 id = primary_event_id(event);
4216
4217 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4218 return -EFAULT;
4219 return 0;
4220 }
4221
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004222 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004223 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004224 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004225 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004226 struct perf_event *output_event;
4227 struct fd output;
4228 ret = perf_fget_light(arg, &output);
4229 if (ret)
4230 return ret;
4231 output_event = output.file->private_data;
4232 ret = perf_event_set_output(event, output_event);
4233 fdput(output);
4234 } else {
4235 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004236 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004237 return ret;
4238 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004239
Li Zefan6fb29152009-10-15 11:21:42 +08004240 case PERF_EVENT_IOC_SET_FILTER:
4241 return perf_event_set_filter(event, (void __user *)arg);
4242
Alexei Starovoitov25415172015-03-25 12:49:20 -07004243 case PERF_EVENT_IOC_SET_BPF:
4244 return perf_event_set_bpf_prog(event, arg);
4245
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004246 default:
4247 return -ENOTTY;
4248 }
4249
4250 if (flags & PERF_IOC_FLAG_GROUP)
4251 perf_event_for_each(event, func);
4252 else
4253 perf_event_for_each_child(event, func);
4254
4255 return 0;
4256}
4257
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004258static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4259{
4260 struct perf_event *event = file->private_data;
4261 struct perf_event_context *ctx;
4262 long ret;
4263
4264 ctx = perf_event_ctx_lock(event);
4265 ret = _perf_ioctl(event, cmd, arg);
4266 perf_event_ctx_unlock(event, ctx);
4267
4268 return ret;
4269}
4270
Pawel Mollb3f20782014-06-13 16:03:32 +01004271#ifdef CONFIG_COMPAT
4272static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4273 unsigned long arg)
4274{
4275 switch (_IOC_NR(cmd)) {
4276 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4277 case _IOC_NR(PERF_EVENT_IOC_ID):
4278 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4279 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4280 cmd &= ~IOCSIZE_MASK;
4281 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4282 }
4283 break;
4284 }
4285 return perf_ioctl(file, cmd, arg);
4286}
4287#else
4288# define perf_compat_ioctl NULL
4289#endif
4290
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004291int perf_event_task_enable(void)
4292{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004293 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004294 struct perf_event *event;
4295
4296 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004297 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4298 ctx = perf_event_ctx_lock(event);
4299 perf_event_for_each_child(event, _perf_event_enable);
4300 perf_event_ctx_unlock(event, ctx);
4301 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004302 mutex_unlock(&current->perf_event_mutex);
4303
4304 return 0;
4305}
4306
4307int perf_event_task_disable(void)
4308{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004309 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004310 struct perf_event *event;
4311
4312 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004313 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4314 ctx = perf_event_ctx_lock(event);
4315 perf_event_for_each_child(event, _perf_event_disable);
4316 perf_event_ctx_unlock(event, ctx);
4317 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004318 mutex_unlock(&current->perf_event_mutex);
4319
4320 return 0;
4321}
4322
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004323static int perf_event_index(struct perf_event *event)
4324{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004325 if (event->hw.state & PERF_HES_STOPPED)
4326 return 0;
4327
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004328 if (event->state != PERF_EVENT_STATE_ACTIVE)
4329 return 0;
4330
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004331 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004332}
4333
Eric B Munsonc4794292011-06-23 16:34:38 -04004334static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004335 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004336 u64 *enabled,
4337 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004338{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004339 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004340
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004341 *now = perf_clock();
4342 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004343 *enabled = ctx_time - event->tstamp_enabled;
4344 *running = ctx_time - event->tstamp_running;
4345}
4346
Peter Zijlstrafa731582013-09-19 10:16:42 +02004347static void perf_event_init_userpage(struct perf_event *event)
4348{
4349 struct perf_event_mmap_page *userpg;
4350 struct ring_buffer *rb;
4351
4352 rcu_read_lock();
4353 rb = rcu_dereference(event->rb);
4354 if (!rb)
4355 goto unlock;
4356
4357 userpg = rb->user_page;
4358
4359 /* Allow new userspace to detect that bit 0 is deprecated */
4360 userpg->cap_bit0_is_deprecated = 1;
4361 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004362 userpg->data_offset = PAGE_SIZE;
4363 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004364
4365unlock:
4366 rcu_read_unlock();
4367}
4368
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004369void __weak arch_perf_update_userpage(
4370 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004371{
4372}
4373
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004374/*
4375 * Callers need to ensure there can be no nesting of this function, otherwise
4376 * the seqlock logic goes bad. We can not serialize this because the arch
4377 * code calls this from NMI context.
4378 */
4379void perf_event_update_userpage(struct perf_event *event)
4380{
4381 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004382 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004383 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004384
4385 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004386 rb = rcu_dereference(event->rb);
4387 if (!rb)
4388 goto unlock;
4389
Eric B Munson0d641202011-06-24 12:26:26 -04004390 /*
4391 * compute total_time_enabled, total_time_running
4392 * based on snapshot values taken when the event
4393 * was last scheduled in.
4394 *
4395 * we cannot simply called update_context_time()
4396 * because of locking issue as we can be called in
4397 * NMI context
4398 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004399 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004400
Frederic Weisbecker76369132011-05-19 19:55:04 +02004401 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004402 /*
4403 * Disable preemption so as to not let the corresponding user-space
4404 * spin too long if we get preempted.
4405 */
4406 preempt_disable();
4407 ++userpg->lock;
4408 barrier();
4409 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004410 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004411 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004412 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004413
Eric B Munson0d641202011-06-24 12:26:26 -04004414 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004415 atomic64_read(&event->child_total_time_enabled);
4416
Eric B Munson0d641202011-06-24 12:26:26 -04004417 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004418 atomic64_read(&event->child_total_time_running);
4419
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004420 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004421
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004422 barrier();
4423 ++userpg->lock;
4424 preempt_enable();
4425unlock:
4426 rcu_read_unlock();
4427}
4428
Peter Zijlstra906010b2009-09-21 16:08:49 +02004429static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4430{
4431 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004432 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004433 int ret = VM_FAULT_SIGBUS;
4434
4435 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4436 if (vmf->pgoff == 0)
4437 ret = 0;
4438 return ret;
4439 }
4440
4441 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004442 rb = rcu_dereference(event->rb);
4443 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004444 goto unlock;
4445
4446 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4447 goto unlock;
4448
Frederic Weisbecker76369132011-05-19 19:55:04 +02004449 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004450 if (!vmf->page)
4451 goto unlock;
4452
4453 get_page(vmf->page);
4454 vmf->page->mapping = vma->vm_file->f_mapping;
4455 vmf->page->index = vmf->pgoff;
4456
4457 ret = 0;
4458unlock:
4459 rcu_read_unlock();
4460
4461 return ret;
4462}
4463
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004464static void ring_buffer_attach(struct perf_event *event,
4465 struct ring_buffer *rb)
4466{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004467 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004468 unsigned long flags;
4469
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004470 if (event->rb) {
4471 /*
4472 * Should be impossible, we set this when removing
4473 * event->rb_entry and wait/clear when adding event->rb_entry.
4474 */
4475 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004476
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004477 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004478 spin_lock_irqsave(&old_rb->event_lock, flags);
4479 list_del_rcu(&event->rb_entry);
4480 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004481
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004482 event->rcu_batches = get_state_synchronize_rcu();
4483 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004484 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004485
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004486 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004487 if (event->rcu_pending) {
4488 cond_synchronize_rcu(event->rcu_batches);
4489 event->rcu_pending = 0;
4490 }
4491
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004492 spin_lock_irqsave(&rb->event_lock, flags);
4493 list_add_rcu(&event->rb_entry, &rb->event_list);
4494 spin_unlock_irqrestore(&rb->event_lock, flags);
4495 }
4496
4497 rcu_assign_pointer(event->rb, rb);
4498
4499 if (old_rb) {
4500 ring_buffer_put(old_rb);
4501 /*
4502 * Since we detached before setting the new rb, so that we
4503 * could attach the new rb, we could have missed a wakeup.
4504 * Provide it now.
4505 */
4506 wake_up_all(&event->waitq);
4507 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004508}
4509
4510static void ring_buffer_wakeup(struct perf_event *event)
4511{
4512 struct ring_buffer *rb;
4513
4514 rcu_read_lock();
4515 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004516 if (rb) {
4517 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4518 wake_up_all(&event->waitq);
4519 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004520 rcu_read_unlock();
4521}
4522
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004523struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004524{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004525 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004526
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004527 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004528 rb = rcu_dereference(event->rb);
4529 if (rb) {
4530 if (!atomic_inc_not_zero(&rb->refcount))
4531 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004532 }
4533 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004534
Frederic Weisbecker76369132011-05-19 19:55:04 +02004535 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004536}
4537
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004538void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004539{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004540 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004541 return;
4542
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004543 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004544
Frederic Weisbecker76369132011-05-19 19:55:04 +02004545 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004546}
4547
4548static void perf_mmap_open(struct vm_area_struct *vma)
4549{
4550 struct perf_event *event = vma->vm_file->private_data;
4551
4552 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004553 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004554
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004555 if (vma->vm_pgoff)
4556 atomic_inc(&event->rb->aux_mmap_count);
4557
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004558 if (event->pmu->event_mapped)
4559 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004560}
4561
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004562/*
4563 * A buffer can be mmap()ed multiple times; either directly through the same
4564 * event, or through other events by use of perf_event_set_output().
4565 *
4566 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4567 * the buffer here, where we still have a VM context. This means we need
4568 * to detach all events redirecting to us.
4569 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004570static void perf_mmap_close(struct vm_area_struct *vma)
4571{
4572 struct perf_event *event = vma->vm_file->private_data;
4573
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004574 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004575 struct user_struct *mmap_user = rb->mmap_user;
4576 int mmap_locked = rb->mmap_locked;
4577 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004578
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004579 if (event->pmu->event_unmapped)
4580 event->pmu->event_unmapped(event);
4581
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004582 /*
4583 * rb->aux_mmap_count will always drop before rb->mmap_count and
4584 * event->mmap_count, so it is ok to use event->mmap_mutex to
4585 * serialize with perf_mmap here.
4586 */
4587 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
4588 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
4589 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
4590 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
4591
4592 rb_free_aux(rb);
4593 mutex_unlock(&event->mmap_mutex);
4594 }
4595
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004596 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004597
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004598 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004599 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004600
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004601 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004602 mutex_unlock(&event->mmap_mutex);
4603
4604 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004605 if (atomic_read(&rb->mmap_count))
4606 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004607
4608 /*
4609 * No other mmap()s, detach from all other events that might redirect
4610 * into the now unreachable buffer. Somewhat complicated by the
4611 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4612 */
4613again:
4614 rcu_read_lock();
4615 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4616 if (!atomic_long_inc_not_zero(&event->refcount)) {
4617 /*
4618 * This event is en-route to free_event() which will
4619 * detach it and remove it from the list.
4620 */
4621 continue;
4622 }
4623 rcu_read_unlock();
4624
4625 mutex_lock(&event->mmap_mutex);
4626 /*
4627 * Check we didn't race with perf_event_set_output() which can
4628 * swizzle the rb from under us while we were waiting to
4629 * acquire mmap_mutex.
4630 *
4631 * If we find a different rb; ignore this event, a next
4632 * iteration will no longer find it on the list. We have to
4633 * still restart the iteration to make sure we're not now
4634 * iterating the wrong list.
4635 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004636 if (event->rb == rb)
4637 ring_buffer_attach(event, NULL);
4638
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004639 mutex_unlock(&event->mmap_mutex);
4640 put_event(event);
4641
4642 /*
4643 * Restart the iteration; either we're on the wrong list or
4644 * destroyed its integrity by doing a deletion.
4645 */
4646 goto again;
4647 }
4648 rcu_read_unlock();
4649
4650 /*
4651 * It could be there's still a few 0-ref events on the list; they'll
4652 * get cleaned up by free_event() -- they'll also still have their
4653 * ref on the rb and will free it whenever they are done with it.
4654 *
4655 * Aside from that, this buffer is 'fully' detached and unmapped,
4656 * undo the VM accounting.
4657 */
4658
4659 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4660 vma->vm_mm->pinned_vm -= mmap_locked;
4661 free_uid(mmap_user);
4662
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004663out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004664 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004665}
4666
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004667static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004668 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004669 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004670 .fault = perf_mmap_fault,
4671 .page_mkwrite = perf_mmap_fault,
4672};
4673
4674static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4675{
4676 struct perf_event *event = file->private_data;
4677 unsigned long user_locked, user_lock_limit;
4678 struct user_struct *user = current_user();
4679 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004680 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004681 unsigned long vma_size;
4682 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004683 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004684 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004685
Peter Zijlstrac7920612010-05-18 10:33:24 +02004686 /*
4687 * Don't allow mmap() of inherited per-task counters. This would
4688 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004689 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004690 */
4691 if (event->cpu == -1 && event->attr.inherit)
4692 return -EINVAL;
4693
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004694 if (!(vma->vm_flags & VM_SHARED))
4695 return -EINVAL;
4696
4697 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004698
4699 if (vma->vm_pgoff == 0) {
4700 nr_pages = (vma_size / PAGE_SIZE) - 1;
4701 } else {
4702 /*
4703 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
4704 * mapped, all subsequent mappings should have the same size
4705 * and offset. Must be above the normal perf buffer.
4706 */
4707 u64 aux_offset, aux_size;
4708
4709 if (!event->rb)
4710 return -EINVAL;
4711
4712 nr_pages = vma_size / PAGE_SIZE;
4713
4714 mutex_lock(&event->mmap_mutex);
4715 ret = -EINVAL;
4716
4717 rb = event->rb;
4718 if (!rb)
4719 goto aux_unlock;
4720
4721 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
4722 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
4723
4724 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
4725 goto aux_unlock;
4726
4727 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
4728 goto aux_unlock;
4729
4730 /* already mapped with a different offset */
4731 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
4732 goto aux_unlock;
4733
4734 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
4735 goto aux_unlock;
4736
4737 /* already mapped with a different size */
4738 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
4739 goto aux_unlock;
4740
4741 if (!is_power_of_2(nr_pages))
4742 goto aux_unlock;
4743
4744 if (!atomic_inc_not_zero(&rb->mmap_count))
4745 goto aux_unlock;
4746
4747 if (rb_has_aux(rb)) {
4748 atomic_inc(&rb->aux_mmap_count);
4749 ret = 0;
4750 goto unlock;
4751 }
4752
4753 atomic_set(&rb->aux_mmap_count, 1);
4754 user_extra = nr_pages;
4755
4756 goto accounting;
4757 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004758
4759 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004760 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004761 * can do bitmasks instead of modulo.
4762 */
Kan Liang2ed11312015-03-02 02:14:26 -05004763 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004764 return -EINVAL;
4765
4766 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4767 return -EINVAL;
4768
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004769 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004770again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004771 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004772 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004773 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004774 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004775 goto unlock;
4776 }
4777
4778 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4779 /*
4780 * Raced against perf_mmap_close() through
4781 * perf_event_set_output(). Try again, hope for better
4782 * luck.
4783 */
4784 mutex_unlock(&event->mmap_mutex);
4785 goto again;
4786 }
4787
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004788 goto unlock;
4789 }
4790
4791 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004792
4793accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004794 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4795
4796 /*
4797 * Increase the limit linearly with more CPUs:
4798 */
4799 user_lock_limit *= num_online_cpus();
4800
4801 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4802
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004803 if (user_locked > user_lock_limit)
4804 extra = user_locked - user_lock_limit;
4805
Jiri Slaby78d7d402010-03-05 13:42:54 -08004806 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004807 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004808 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004809
4810 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4811 !capable(CAP_IPC_LOCK)) {
4812 ret = -EPERM;
4813 goto unlock;
4814 }
4815
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004816 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004817
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004818 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004819 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004820
Frederic Weisbecker76369132011-05-19 19:55:04 +02004821 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004822 rb = rb_alloc(nr_pages,
4823 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4824 event->cpu, flags);
4825
4826 if (!rb) {
4827 ret = -ENOMEM;
4828 goto unlock;
4829 }
4830
4831 atomic_set(&rb->mmap_count, 1);
4832 rb->mmap_user = get_current_user();
4833 rb->mmap_locked = extra;
4834
4835 ring_buffer_attach(event, rb);
4836
4837 perf_event_init_userpage(event);
4838 perf_event_update_userpage(event);
4839 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02004840 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
4841 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004842 if (!ret)
4843 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004844 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004845
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004846unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004847 if (!ret) {
4848 atomic_long_add(user_extra, &user->locked_vm);
4849 vma->vm_mm->pinned_vm += extra;
4850
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004851 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004852 } else if (rb) {
4853 atomic_dec(&rb->mmap_count);
4854 }
4855aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004856 mutex_unlock(&event->mmap_mutex);
4857
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004858 /*
4859 * Since pinned accounting is per vm we cannot allow fork() to copy our
4860 * vma.
4861 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004862 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004863 vma->vm_ops = &perf_mmap_vmops;
4864
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004865 if (event->pmu->event_mapped)
4866 event->pmu->event_mapped(event);
4867
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004868 return ret;
4869}
4870
4871static int perf_fasync(int fd, struct file *filp, int on)
4872{
Al Viro496ad9a2013-01-23 17:07:38 -05004873 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004874 struct perf_event *event = filp->private_data;
4875 int retval;
4876
Al Viro59551022016-01-22 15:40:57 -05004877 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004878 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05004879 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004880
4881 if (retval < 0)
4882 return retval;
4883
4884 return 0;
4885}
4886
4887static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004888 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004889 .release = perf_release,
4890 .read = perf_read,
4891 .poll = perf_poll,
4892 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01004893 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004894 .mmap = perf_mmap,
4895 .fasync = perf_fasync,
4896};
4897
4898/*
4899 * Perf event wakeup
4900 *
4901 * If there's data, ensure we set the poll() state and publish everything
4902 * to user-space before waking everybody up.
4903 */
4904
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02004905static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
4906{
4907 /* only the parent has fasync state */
4908 if (event->parent)
4909 event = event->parent;
4910 return &event->fasync;
4911}
4912
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004913void perf_event_wakeup(struct perf_event *event)
4914{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004915 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004916
4917 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02004918 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004919 event->pending_kill = 0;
4920 }
4921}
4922
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004923static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004924{
4925 struct perf_event *event = container_of(entry,
4926 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01004927 int rctx;
4928
4929 rctx = perf_swevent_get_recursion_context();
4930 /*
4931 * If we 'fail' here, that's OK, it means recursion is already disabled
4932 * and we won't recurse 'further'.
4933 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004934
4935 if (event->pending_disable) {
4936 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004937 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004938 }
4939
4940 if (event->pending_wakeup) {
4941 event->pending_wakeup = 0;
4942 perf_event_wakeup(event);
4943 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01004944
4945 if (rctx >= 0)
4946 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004947}
4948
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004949/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004950 * We assume there is only KVM supporting the callbacks.
4951 * Later on, we might change it to a list if there is
4952 * another virtualization implementation supporting the callbacks.
4953 */
4954struct perf_guest_info_callbacks *perf_guest_cbs;
4955
4956int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4957{
4958 perf_guest_cbs = cbs;
4959 return 0;
4960}
4961EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4962
4963int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4964{
4965 perf_guest_cbs = NULL;
4966 return 0;
4967}
4968EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4969
Jiri Olsa40189942012-08-07 15:20:37 +02004970static void
4971perf_output_sample_regs(struct perf_output_handle *handle,
4972 struct pt_regs *regs, u64 mask)
4973{
4974 int bit;
4975
4976 for_each_set_bit(bit, (const unsigned long *) &mask,
4977 sizeof(mask) * BITS_PER_BYTE) {
4978 u64 val;
4979
4980 val = perf_reg_value(regs, bit);
4981 perf_output_put(handle, val);
4982 }
4983}
4984
Stephane Eranian60e23642014-09-24 13:48:37 +02004985static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004986 struct pt_regs *regs,
4987 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02004988{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004989 if (user_mode(regs)) {
4990 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02004991 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004992 } else if (current->mm) {
4993 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02004994 } else {
4995 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
4996 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02004997 }
4998}
4999
Stephane Eranian60e23642014-09-24 13:48:37 +02005000static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5001 struct pt_regs *regs)
5002{
5003 regs_intr->regs = regs;
5004 regs_intr->abi = perf_reg_abi(current);
5005}
5006
5007
Jiri Olsac5ebced2012-08-07 15:20:40 +02005008/*
5009 * Get remaining task size from user stack pointer.
5010 *
5011 * It'd be better to take stack vma map and limit this more
5012 * precisly, but there's no way to get it safely under interrupt,
5013 * so using TASK_SIZE as limit.
5014 */
5015static u64 perf_ustack_task_size(struct pt_regs *regs)
5016{
5017 unsigned long addr = perf_user_stack_pointer(regs);
5018
5019 if (!addr || addr >= TASK_SIZE)
5020 return 0;
5021
5022 return TASK_SIZE - addr;
5023}
5024
5025static u16
5026perf_sample_ustack_size(u16 stack_size, u16 header_size,
5027 struct pt_regs *regs)
5028{
5029 u64 task_size;
5030
5031 /* No regs, no stack pointer, no dump. */
5032 if (!regs)
5033 return 0;
5034
5035 /*
5036 * Check if we fit in with the requested stack size into the:
5037 * - TASK_SIZE
5038 * If we don't, we limit the size to the TASK_SIZE.
5039 *
5040 * - remaining sample size
5041 * If we don't, we customize the stack size to
5042 * fit in to the remaining sample size.
5043 */
5044
5045 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5046 stack_size = min(stack_size, (u16) task_size);
5047
5048 /* Current header size plus static size and dynamic size. */
5049 header_size += 2 * sizeof(u64);
5050
5051 /* Do we fit in with the current stack dump size? */
5052 if ((u16) (header_size + stack_size) < header_size) {
5053 /*
5054 * If we overflow the maximum size for the sample,
5055 * we customize the stack dump size to fit in.
5056 */
5057 stack_size = USHRT_MAX - header_size - sizeof(u64);
5058 stack_size = round_up(stack_size, sizeof(u64));
5059 }
5060
5061 return stack_size;
5062}
5063
5064static void
5065perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5066 struct pt_regs *regs)
5067{
5068 /* Case of a kernel thread, nothing to dump */
5069 if (!regs) {
5070 u64 size = 0;
5071 perf_output_put(handle, size);
5072 } else {
5073 unsigned long sp;
5074 unsigned int rem;
5075 u64 dyn_size;
5076
5077 /*
5078 * We dump:
5079 * static size
5080 * - the size requested by user or the best one we can fit
5081 * in to the sample max size
5082 * data
5083 * - user stack dump data
5084 * dynamic size
5085 * - the actual dumped size
5086 */
5087
5088 /* Static size. */
5089 perf_output_put(handle, dump_size);
5090
5091 /* Data. */
5092 sp = perf_user_stack_pointer(regs);
5093 rem = __output_copy_user(handle, (void *) sp, dump_size);
5094 dyn_size = dump_size - rem;
5095
5096 perf_output_skip(handle, rem);
5097
5098 /* Dynamic size. */
5099 perf_output_put(handle, dyn_size);
5100 }
5101}
5102
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005103static void __perf_event_header__init_id(struct perf_event_header *header,
5104 struct perf_sample_data *data,
5105 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005106{
5107 u64 sample_type = event->attr.sample_type;
5108
5109 data->type = sample_type;
5110 header->size += event->id_header_size;
5111
5112 if (sample_type & PERF_SAMPLE_TID) {
5113 /* namespace issues */
5114 data->tid_entry.pid = perf_event_pid(event, current);
5115 data->tid_entry.tid = perf_event_tid(event, current);
5116 }
5117
5118 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005119 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005120
Adrian Hunterff3d5272013-08-27 11:23:07 +03005121 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005122 data->id = primary_event_id(event);
5123
5124 if (sample_type & PERF_SAMPLE_STREAM_ID)
5125 data->stream_id = event->id;
5126
5127 if (sample_type & PERF_SAMPLE_CPU) {
5128 data->cpu_entry.cpu = raw_smp_processor_id();
5129 data->cpu_entry.reserved = 0;
5130 }
5131}
5132
Frederic Weisbecker76369132011-05-19 19:55:04 +02005133void perf_event_header__init_id(struct perf_event_header *header,
5134 struct perf_sample_data *data,
5135 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005136{
5137 if (event->attr.sample_id_all)
5138 __perf_event_header__init_id(header, data, event);
5139}
5140
5141static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5142 struct perf_sample_data *data)
5143{
5144 u64 sample_type = data->type;
5145
5146 if (sample_type & PERF_SAMPLE_TID)
5147 perf_output_put(handle, data->tid_entry);
5148
5149 if (sample_type & PERF_SAMPLE_TIME)
5150 perf_output_put(handle, data->time);
5151
5152 if (sample_type & PERF_SAMPLE_ID)
5153 perf_output_put(handle, data->id);
5154
5155 if (sample_type & PERF_SAMPLE_STREAM_ID)
5156 perf_output_put(handle, data->stream_id);
5157
5158 if (sample_type & PERF_SAMPLE_CPU)
5159 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005160
5161 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5162 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005163}
5164
Frederic Weisbecker76369132011-05-19 19:55:04 +02005165void perf_event__output_id_sample(struct perf_event *event,
5166 struct perf_output_handle *handle,
5167 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005168{
5169 if (event->attr.sample_id_all)
5170 __perf_event__output_id_sample(handle, sample);
5171}
5172
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005173static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005174 struct perf_event *event,
5175 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005176{
5177 u64 read_format = event->attr.read_format;
5178 u64 values[4];
5179 int n = 0;
5180
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005181 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005182 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005183 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005184 atomic64_read(&event->child_total_time_enabled);
5185 }
5186 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005187 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005188 atomic64_read(&event->child_total_time_running);
5189 }
5190 if (read_format & PERF_FORMAT_ID)
5191 values[n++] = primary_event_id(event);
5192
Frederic Weisbecker76369132011-05-19 19:55:04 +02005193 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005194}
5195
5196/*
5197 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
5198 */
5199static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005200 struct perf_event *event,
5201 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005202{
5203 struct perf_event *leader = event->group_leader, *sub;
5204 u64 read_format = event->attr.read_format;
5205 u64 values[5];
5206 int n = 0;
5207
5208 values[n++] = 1 + leader->nr_siblings;
5209
5210 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005211 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005212
5213 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005214 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005215
5216 if (leader != event)
5217 leader->pmu->read(leader);
5218
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005219 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005220 if (read_format & PERF_FORMAT_ID)
5221 values[n++] = primary_event_id(leader);
5222
Frederic Weisbecker76369132011-05-19 19:55:04 +02005223 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005224
5225 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5226 n = 0;
5227
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005228 if ((sub != event) &&
5229 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005230 sub->pmu->read(sub);
5231
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005232 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005233 if (read_format & PERF_FORMAT_ID)
5234 values[n++] = primary_event_id(sub);
5235
Frederic Weisbecker76369132011-05-19 19:55:04 +02005236 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005237 }
5238}
5239
Stephane Eranianeed01522010-10-26 16:08:01 +02005240#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5241 PERF_FORMAT_TOTAL_TIME_RUNNING)
5242
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005243static void perf_output_read(struct perf_output_handle *handle,
5244 struct perf_event *event)
5245{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005246 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005247 u64 read_format = event->attr.read_format;
5248
5249 /*
5250 * compute total_time_enabled, total_time_running
5251 * based on snapshot values taken when the event
5252 * was last scheduled in.
5253 *
5254 * we cannot simply called update_context_time()
5255 * because of locking issue as we are called in
5256 * NMI context
5257 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005258 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005259 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005260
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005261 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005262 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005263 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005264 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005265}
5266
5267void perf_output_sample(struct perf_output_handle *handle,
5268 struct perf_event_header *header,
5269 struct perf_sample_data *data,
5270 struct perf_event *event)
5271{
5272 u64 sample_type = data->type;
5273
5274 perf_output_put(handle, *header);
5275
Adrian Hunterff3d5272013-08-27 11:23:07 +03005276 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5277 perf_output_put(handle, data->id);
5278
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005279 if (sample_type & PERF_SAMPLE_IP)
5280 perf_output_put(handle, data->ip);
5281
5282 if (sample_type & PERF_SAMPLE_TID)
5283 perf_output_put(handle, data->tid_entry);
5284
5285 if (sample_type & PERF_SAMPLE_TIME)
5286 perf_output_put(handle, data->time);
5287
5288 if (sample_type & PERF_SAMPLE_ADDR)
5289 perf_output_put(handle, data->addr);
5290
5291 if (sample_type & PERF_SAMPLE_ID)
5292 perf_output_put(handle, data->id);
5293
5294 if (sample_type & PERF_SAMPLE_STREAM_ID)
5295 perf_output_put(handle, data->stream_id);
5296
5297 if (sample_type & PERF_SAMPLE_CPU)
5298 perf_output_put(handle, data->cpu_entry);
5299
5300 if (sample_type & PERF_SAMPLE_PERIOD)
5301 perf_output_put(handle, data->period);
5302
5303 if (sample_type & PERF_SAMPLE_READ)
5304 perf_output_read(handle, event);
5305
5306 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5307 if (data->callchain) {
5308 int size = 1;
5309
5310 if (data->callchain)
5311 size += data->callchain->nr;
5312
5313 size *= sizeof(u64);
5314
Frederic Weisbecker76369132011-05-19 19:55:04 +02005315 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005316 } else {
5317 u64 nr = 0;
5318 perf_output_put(handle, nr);
5319 }
5320 }
5321
5322 if (sample_type & PERF_SAMPLE_RAW) {
5323 if (data->raw) {
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005324 u32 raw_size = data->raw->size;
5325 u32 real_size = round_up(raw_size + sizeof(u32),
5326 sizeof(u64)) - sizeof(u32);
5327 u64 zero = 0;
5328
5329 perf_output_put(handle, real_size);
5330 __output_copy(handle, data->raw->data, raw_size);
5331 if (real_size - raw_size)
5332 __output_copy(handle, &zero, real_size - raw_size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005333 } else {
5334 struct {
5335 u32 size;
5336 u32 data;
5337 } raw = {
5338 .size = sizeof(u32),
5339 .data = 0,
5340 };
5341 perf_output_put(handle, raw);
5342 }
5343 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005344
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005345 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5346 if (data->br_stack) {
5347 size_t size;
5348
5349 size = data->br_stack->nr
5350 * sizeof(struct perf_branch_entry);
5351
5352 perf_output_put(handle, data->br_stack->nr);
5353 perf_output_copy(handle, data->br_stack->entries, size);
5354 } else {
5355 /*
5356 * we always store at least the value of nr
5357 */
5358 u64 nr = 0;
5359 perf_output_put(handle, nr);
5360 }
5361 }
Jiri Olsa40189942012-08-07 15:20:37 +02005362
5363 if (sample_type & PERF_SAMPLE_REGS_USER) {
5364 u64 abi = data->regs_user.abi;
5365
5366 /*
5367 * If there are no regs to dump, notice it through
5368 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5369 */
5370 perf_output_put(handle, abi);
5371
5372 if (abi) {
5373 u64 mask = event->attr.sample_regs_user;
5374 perf_output_sample_regs(handle,
5375 data->regs_user.regs,
5376 mask);
5377 }
5378 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005379
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005380 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005381 perf_output_sample_ustack(handle,
5382 data->stack_user_size,
5383 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005384 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005385
5386 if (sample_type & PERF_SAMPLE_WEIGHT)
5387 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005388
5389 if (sample_type & PERF_SAMPLE_DATA_SRC)
5390 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005391
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005392 if (sample_type & PERF_SAMPLE_TRANSACTION)
5393 perf_output_put(handle, data->txn);
5394
Stephane Eranian60e23642014-09-24 13:48:37 +02005395 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5396 u64 abi = data->regs_intr.abi;
5397 /*
5398 * If there are no regs to dump, notice it through
5399 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5400 */
5401 perf_output_put(handle, abi);
5402
5403 if (abi) {
5404 u64 mask = event->attr.sample_regs_intr;
5405
5406 perf_output_sample_regs(handle,
5407 data->regs_intr.regs,
5408 mask);
5409 }
5410 }
5411
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005412 if (!event->attr.watermark) {
5413 int wakeup_events = event->attr.wakeup_events;
5414
5415 if (wakeup_events) {
5416 struct ring_buffer *rb = handle->rb;
5417 int events = local_inc_return(&rb->events);
5418
5419 if (events >= wakeup_events) {
5420 local_sub(wakeup_events, &rb->events);
5421 local_inc(&rb->wakeup);
5422 }
5423 }
5424 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005425}
5426
5427void perf_prepare_sample(struct perf_event_header *header,
5428 struct perf_sample_data *data,
5429 struct perf_event *event,
5430 struct pt_regs *regs)
5431{
5432 u64 sample_type = event->attr.sample_type;
5433
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005434 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005435 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005436
5437 header->misc = 0;
5438 header->misc |= perf_misc_flags(regs);
5439
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005440 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005441
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005442 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005443 data->ip = perf_instruction_pointer(regs);
5444
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005445 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5446 int size = 1;
5447
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005448 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005449
5450 if (data->callchain)
5451 size += data->callchain->nr;
5452
5453 header->size += size * sizeof(u64);
5454 }
5455
5456 if (sample_type & PERF_SAMPLE_RAW) {
5457 int size = sizeof(u32);
5458
5459 if (data->raw)
5460 size += data->raw->size;
5461 else
5462 size += sizeof(u32);
5463
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005464 header->size += round_up(size, sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005465 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005466
5467 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5468 int size = sizeof(u64); /* nr */
5469 if (data->br_stack) {
5470 size += data->br_stack->nr
5471 * sizeof(struct perf_branch_entry);
5472 }
5473 header->size += size;
5474 }
Jiri Olsa40189942012-08-07 15:20:37 +02005475
Peter Zijlstra25657112014-09-24 13:48:42 +02005476 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005477 perf_sample_regs_user(&data->regs_user, regs,
5478 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005479
Jiri Olsa40189942012-08-07 15:20:37 +02005480 if (sample_type & PERF_SAMPLE_REGS_USER) {
5481 /* regs dump ABI info */
5482 int size = sizeof(u64);
5483
Jiri Olsa40189942012-08-07 15:20:37 +02005484 if (data->regs_user.regs) {
5485 u64 mask = event->attr.sample_regs_user;
5486 size += hweight64(mask) * sizeof(u64);
5487 }
5488
5489 header->size += size;
5490 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005491
5492 if (sample_type & PERF_SAMPLE_STACK_USER) {
5493 /*
5494 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5495 * processed as the last one or have additional check added
5496 * in case new sample type is added, because we could eat
5497 * up the rest of the sample size.
5498 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005499 u16 stack_size = event->attr.sample_stack_user;
5500 u16 size = sizeof(u64);
5501
Jiri Olsac5ebced2012-08-07 15:20:40 +02005502 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005503 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005504
5505 /*
5506 * If there is something to dump, add space for the dump
5507 * itself and for the field that tells the dynamic size,
5508 * which is how many have been actually dumped.
5509 */
5510 if (stack_size)
5511 size += sizeof(u64) + stack_size;
5512
5513 data->stack_user_size = stack_size;
5514 header->size += size;
5515 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005516
5517 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5518 /* regs dump ABI info */
5519 int size = sizeof(u64);
5520
5521 perf_sample_regs_intr(&data->regs_intr, regs);
5522
5523 if (data->regs_intr.regs) {
5524 u64 mask = event->attr.sample_regs_intr;
5525
5526 size += hweight64(mask) * sizeof(u64);
5527 }
5528
5529 header->size += size;
5530 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005531}
5532
Yan, Zheng21509082015-05-06 15:33:49 -04005533void perf_event_output(struct perf_event *event,
5534 struct perf_sample_data *data,
5535 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005536{
5537 struct perf_output_handle handle;
5538 struct perf_event_header header;
5539
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005540 /* protect the callchain buffers */
5541 rcu_read_lock();
5542
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005543 perf_prepare_sample(&header, data, event, regs);
5544
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005545 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005546 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005547
5548 perf_output_sample(&handle, &header, data, event);
5549
5550 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005551
5552exit:
5553 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005554}
5555
5556/*
5557 * read event_id
5558 */
5559
5560struct perf_read_event {
5561 struct perf_event_header header;
5562
5563 u32 pid;
5564 u32 tid;
5565};
5566
5567static void
5568perf_event_read_event(struct perf_event *event,
5569 struct task_struct *task)
5570{
5571 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005572 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005573 struct perf_read_event read_event = {
5574 .header = {
5575 .type = PERF_RECORD_READ,
5576 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005577 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005578 },
5579 .pid = perf_event_pid(event, task),
5580 .tid = perf_event_tid(event, task),
5581 };
5582 int ret;
5583
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005584 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005585 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005586 if (ret)
5587 return;
5588
5589 perf_output_put(&handle, read_event);
5590 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005591 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005592
5593 perf_output_end(&handle);
5594}
5595
Jiri Olsa52d857a2013-05-06 18:27:18 +02005596typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5597
5598static void
5599perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005600 perf_event_aux_output_cb output,
5601 void *data)
5602{
5603 struct perf_event *event;
5604
5605 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5606 if (event->state < PERF_EVENT_STATE_INACTIVE)
5607 continue;
5608 if (!event_filter_match(event))
5609 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02005610 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005611 }
5612}
5613
5614static void
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005615perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data,
5616 struct perf_event_context *task_ctx)
5617{
5618 rcu_read_lock();
5619 preempt_disable();
5620 perf_event_aux_ctx(task_ctx, output, data);
5621 preempt_enable();
5622 rcu_read_unlock();
5623}
5624
5625static void
Jiri Olsa67516842013-07-09 18:56:31 +02005626perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005627 struct perf_event_context *task_ctx)
5628{
5629 struct perf_cpu_context *cpuctx;
5630 struct perf_event_context *ctx;
5631 struct pmu *pmu;
5632 int ctxn;
5633
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005634 /*
5635 * If we have task_ctx != NULL we only notify
5636 * the task context itself. The task_ctx is set
5637 * only for EXIT events before releasing task
5638 * context.
5639 */
5640 if (task_ctx) {
5641 perf_event_aux_task_ctx(output, data, task_ctx);
5642 return;
5643 }
5644
Jiri Olsa52d857a2013-05-06 18:27:18 +02005645 rcu_read_lock();
5646 list_for_each_entry_rcu(pmu, &pmus, entry) {
5647 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5648 if (cpuctx->unique_pmu != pmu)
5649 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02005650 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005651 ctxn = pmu->task_ctx_nr;
5652 if (ctxn < 0)
5653 goto next;
5654 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5655 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02005656 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005657next:
5658 put_cpu_ptr(pmu->pmu_cpu_context);
5659 }
Jiri Olsa52d857a2013-05-06 18:27:18 +02005660 rcu_read_unlock();
5661}
5662
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005663/*
5664 * task tracking -- fork/exit
5665 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02005666 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005667 */
5668
5669struct perf_task_event {
5670 struct task_struct *task;
5671 struct perf_event_context *task_ctx;
5672
5673 struct {
5674 struct perf_event_header header;
5675
5676 u32 pid;
5677 u32 ppid;
5678 u32 tid;
5679 u32 ptid;
5680 u64 time;
5681 } event_id;
5682};
5683
Jiri Olsa67516842013-07-09 18:56:31 +02005684static int perf_event_task_match(struct perf_event *event)
5685{
Stephane Eranian13d7a242013-08-21 12:10:24 +02005686 return event->attr.comm || event->attr.mmap ||
5687 event->attr.mmap2 || event->attr.mmap_data ||
5688 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02005689}
5690
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005691static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005692 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005693{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005694 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005695 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005696 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005697 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005698 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01005699
Jiri Olsa67516842013-07-09 18:56:31 +02005700 if (!perf_event_task_match(event))
5701 return;
5702
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005703 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005704
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005705 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005706 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02005707 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005708 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005709
5710 task_event->event_id.pid = perf_event_pid(event, task);
5711 task_event->event_id.ppid = perf_event_pid(event, current);
5712
5713 task_event->event_id.tid = perf_event_tid(event, task);
5714 task_event->event_id.ptid = perf_event_tid(event, current);
5715
Peter Zijlstra34f43922015-02-20 14:05:38 +01005716 task_event->event_id.time = perf_event_clock(event);
5717
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005718 perf_output_put(&handle, task_event->event_id);
5719
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005720 perf_event__output_id_sample(event, &handle, &sample);
5721
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005722 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005723out:
5724 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005725}
5726
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005727static void perf_event_task(struct task_struct *task,
5728 struct perf_event_context *task_ctx,
5729 int new)
5730{
5731 struct perf_task_event task_event;
5732
5733 if (!atomic_read(&nr_comm_events) &&
5734 !atomic_read(&nr_mmap_events) &&
5735 !atomic_read(&nr_task_events))
5736 return;
5737
5738 task_event = (struct perf_task_event){
5739 .task = task,
5740 .task_ctx = task_ctx,
5741 .event_id = {
5742 .header = {
5743 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5744 .misc = 0,
5745 .size = sizeof(task_event.event_id),
5746 },
5747 /* .pid */
5748 /* .ppid */
5749 /* .tid */
5750 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01005751 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005752 },
5753 };
5754
Jiri Olsa67516842013-07-09 18:56:31 +02005755 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005756 &task_event,
5757 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005758}
5759
5760void perf_event_fork(struct task_struct *task)
5761{
5762 perf_event_task(task, NULL, 1);
5763}
5764
5765/*
5766 * comm tracking
5767 */
5768
5769struct perf_comm_event {
5770 struct task_struct *task;
5771 char *comm;
5772 int comm_size;
5773
5774 struct {
5775 struct perf_event_header header;
5776
5777 u32 pid;
5778 u32 tid;
5779 } event_id;
5780};
5781
Jiri Olsa67516842013-07-09 18:56:31 +02005782static int perf_event_comm_match(struct perf_event *event)
5783{
5784 return event->attr.comm;
5785}
5786
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005787static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005788 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005789{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005790 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005791 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005792 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005793 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005794 int ret;
5795
Jiri Olsa67516842013-07-09 18:56:31 +02005796 if (!perf_event_comm_match(event))
5797 return;
5798
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005799 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5800 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005801 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005802
5803 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005804 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005805
5806 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5807 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5808
5809 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005810 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005811 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005812
5813 perf_event__output_id_sample(event, &handle, &sample);
5814
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005815 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005816out:
5817 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005818}
5819
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005820static void perf_event_comm_event(struct perf_comm_event *comm_event)
5821{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005822 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005823 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005824
5825 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005826 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005827 size = ALIGN(strlen(comm)+1, sizeof(u64));
5828
5829 comm_event->comm = comm;
5830 comm_event->comm_size = size;
5831
5832 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005833
Jiri Olsa67516842013-07-09 18:56:31 +02005834 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005835 comm_event,
5836 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005837}
5838
Adrian Hunter82b89772014-05-28 11:45:04 +03005839void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005840{
5841 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005842
5843 if (!atomic_read(&nr_comm_events))
5844 return;
5845
5846 comm_event = (struct perf_comm_event){
5847 .task = task,
5848 /* .comm */
5849 /* .comm_size */
5850 .event_id = {
5851 .header = {
5852 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005853 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005854 /* .size */
5855 },
5856 /* .pid */
5857 /* .tid */
5858 },
5859 };
5860
5861 perf_event_comm_event(&comm_event);
5862}
5863
5864/*
5865 * mmap tracking
5866 */
5867
5868struct perf_mmap_event {
5869 struct vm_area_struct *vma;
5870
5871 const char *file_name;
5872 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005873 int maj, min;
5874 u64 ino;
5875 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005876 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005877
5878 struct {
5879 struct perf_event_header header;
5880
5881 u32 pid;
5882 u32 tid;
5883 u64 start;
5884 u64 len;
5885 u64 pgoff;
5886 } event_id;
5887};
5888
Jiri Olsa67516842013-07-09 18:56:31 +02005889static int perf_event_mmap_match(struct perf_event *event,
5890 void *data)
5891{
5892 struct perf_mmap_event *mmap_event = data;
5893 struct vm_area_struct *vma = mmap_event->vma;
5894 int executable = vma->vm_flags & VM_EXEC;
5895
5896 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005897 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005898}
5899
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005900static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005901 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005902{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005903 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005904 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005905 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005906 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005907 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005908
Jiri Olsa67516842013-07-09 18:56:31 +02005909 if (!perf_event_mmap_match(event, data))
5910 return;
5911
Stephane Eranian13d7a242013-08-21 12:10:24 +02005912 if (event->attr.mmap2) {
5913 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5914 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5915 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5916 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005917 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005918 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
5919 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005920 }
5921
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005922 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5923 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005924 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005925 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005926 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005927
5928 mmap_event->event_id.pid = perf_event_pid(event, current);
5929 mmap_event->event_id.tid = perf_event_tid(event, current);
5930
5931 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005932
5933 if (event->attr.mmap2) {
5934 perf_output_put(&handle, mmap_event->maj);
5935 perf_output_put(&handle, mmap_event->min);
5936 perf_output_put(&handle, mmap_event->ino);
5937 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005938 perf_output_put(&handle, mmap_event->prot);
5939 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005940 }
5941
Frederic Weisbecker76369132011-05-19 19:55:04 +02005942 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005943 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005944
5945 perf_event__output_id_sample(event, &handle, &sample);
5946
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005947 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005948out:
5949 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005950}
5951
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005952static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5953{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005954 struct vm_area_struct *vma = mmap_event->vma;
5955 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005956 int maj = 0, min = 0;
5957 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005958 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005959 unsigned int size;
5960 char tmp[16];
5961 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005962 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005963
5964 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005965 struct inode *inode;
5966 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005967
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005968 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005969 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005970 name = "//enomem";
5971 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005972 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005973 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005974 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005975 * need to add enough zero bytes after the string to handle
5976 * the 64bit alignment we do later.
5977 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02005978 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005979 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005980 name = "//toolong";
5981 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005982 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005983 inode = file_inode(vma->vm_file);
5984 dev = inode->i_sb->s_dev;
5985 ino = inode->i_ino;
5986 gen = inode->i_generation;
5987 maj = MAJOR(dev);
5988 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005989
5990 if (vma->vm_flags & VM_READ)
5991 prot |= PROT_READ;
5992 if (vma->vm_flags & VM_WRITE)
5993 prot |= PROT_WRITE;
5994 if (vma->vm_flags & VM_EXEC)
5995 prot |= PROT_EXEC;
5996
5997 if (vma->vm_flags & VM_MAYSHARE)
5998 flags = MAP_SHARED;
5999 else
6000 flags = MAP_PRIVATE;
6001
6002 if (vma->vm_flags & VM_DENYWRITE)
6003 flags |= MAP_DENYWRITE;
6004 if (vma->vm_flags & VM_MAYEXEC)
6005 flags |= MAP_EXECUTABLE;
6006 if (vma->vm_flags & VM_LOCKED)
6007 flags |= MAP_LOCKED;
6008 if (vma->vm_flags & VM_HUGETLB)
6009 flags |= MAP_HUGETLB;
6010
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006011 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006012 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006013 if (vma->vm_ops && vma->vm_ops->name) {
6014 name = (char *) vma->vm_ops->name(vma);
6015 if (name)
6016 goto cpy_name;
6017 }
6018
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006019 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006020 if (name)
6021 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006022
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006023 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006024 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006025 name = "[heap]";
6026 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006027 }
6028 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006029 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006030 name = "[stack]";
6031 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006032 }
6033
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006034 name = "//anon";
6035 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006036 }
6037
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006038cpy_name:
6039 strlcpy(tmp, name, sizeof(tmp));
6040 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006041got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006042 /*
6043 * Since our buffer works in 8 byte units we need to align our string
6044 * size to a multiple of 8. However, we must guarantee the tail end is
6045 * zero'd out to avoid leaking random bits to userspace.
6046 */
6047 size = strlen(name)+1;
6048 while (!IS_ALIGNED(size, sizeof(u64)))
6049 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006050
6051 mmap_event->file_name = name;
6052 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006053 mmap_event->maj = maj;
6054 mmap_event->min = min;
6055 mmap_event->ino = ino;
6056 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006057 mmap_event->prot = prot;
6058 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006059
Stephane Eranian2fe85422013-01-24 16:10:39 +01006060 if (!(vma->vm_flags & VM_EXEC))
6061 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6062
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006063 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6064
Jiri Olsa67516842013-07-09 18:56:31 +02006065 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006066 mmap_event,
6067 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006068
6069 kfree(buf);
6070}
6071
Eric B Munson3af9e852010-05-18 15:30:49 +01006072void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006073{
6074 struct perf_mmap_event mmap_event;
6075
6076 if (!atomic_read(&nr_mmap_events))
6077 return;
6078
6079 mmap_event = (struct perf_mmap_event){
6080 .vma = vma,
6081 /* .file_name */
6082 /* .file_size */
6083 .event_id = {
6084 .header = {
6085 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006086 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006087 /* .size */
6088 },
6089 /* .pid */
6090 /* .tid */
6091 .start = vma->vm_start,
6092 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006093 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006094 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006095 /* .maj (attr_mmap2 only) */
6096 /* .min (attr_mmap2 only) */
6097 /* .ino (attr_mmap2 only) */
6098 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006099 /* .prot (attr_mmap2 only) */
6100 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006101 };
6102
6103 perf_event_mmap_event(&mmap_event);
6104}
6105
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006106void perf_event_aux_event(struct perf_event *event, unsigned long head,
6107 unsigned long size, u64 flags)
6108{
6109 struct perf_output_handle handle;
6110 struct perf_sample_data sample;
6111 struct perf_aux_event {
6112 struct perf_event_header header;
6113 u64 offset;
6114 u64 size;
6115 u64 flags;
6116 } rec = {
6117 .header = {
6118 .type = PERF_RECORD_AUX,
6119 .misc = 0,
6120 .size = sizeof(rec),
6121 },
6122 .offset = head,
6123 .size = size,
6124 .flags = flags,
6125 };
6126 int ret;
6127
6128 perf_event_header__init_id(&rec.header, &sample, event);
6129 ret = perf_output_begin(&handle, event, rec.header.size);
6130
6131 if (ret)
6132 return;
6133
6134 perf_output_put(&handle, rec);
6135 perf_event__output_id_sample(event, &handle, &sample);
6136
6137 perf_output_end(&handle);
6138}
6139
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006140/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006141 * Lost/dropped samples logging
6142 */
6143void perf_log_lost_samples(struct perf_event *event, u64 lost)
6144{
6145 struct perf_output_handle handle;
6146 struct perf_sample_data sample;
6147 int ret;
6148
6149 struct {
6150 struct perf_event_header header;
6151 u64 lost;
6152 } lost_samples_event = {
6153 .header = {
6154 .type = PERF_RECORD_LOST_SAMPLES,
6155 .misc = 0,
6156 .size = sizeof(lost_samples_event),
6157 },
6158 .lost = lost,
6159 };
6160
6161 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6162
6163 ret = perf_output_begin(&handle, event,
6164 lost_samples_event.header.size);
6165 if (ret)
6166 return;
6167
6168 perf_output_put(&handle, lost_samples_event);
6169 perf_event__output_id_sample(event, &handle, &sample);
6170 perf_output_end(&handle);
6171}
6172
6173/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006174 * context_switch tracking
6175 */
6176
6177struct perf_switch_event {
6178 struct task_struct *task;
6179 struct task_struct *next_prev;
6180
6181 struct {
6182 struct perf_event_header header;
6183 u32 next_prev_pid;
6184 u32 next_prev_tid;
6185 } event_id;
6186};
6187
6188static int perf_event_switch_match(struct perf_event *event)
6189{
6190 return event->attr.context_switch;
6191}
6192
6193static void perf_event_switch_output(struct perf_event *event, void *data)
6194{
6195 struct perf_switch_event *se = data;
6196 struct perf_output_handle handle;
6197 struct perf_sample_data sample;
6198 int ret;
6199
6200 if (!perf_event_switch_match(event))
6201 return;
6202
6203 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6204 if (event->ctx->task) {
6205 se->event_id.header.type = PERF_RECORD_SWITCH;
6206 se->event_id.header.size = sizeof(se->event_id.header);
6207 } else {
6208 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6209 se->event_id.header.size = sizeof(se->event_id);
6210 se->event_id.next_prev_pid =
6211 perf_event_pid(event, se->next_prev);
6212 se->event_id.next_prev_tid =
6213 perf_event_tid(event, se->next_prev);
6214 }
6215
6216 perf_event_header__init_id(&se->event_id.header, &sample, event);
6217
6218 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6219 if (ret)
6220 return;
6221
6222 if (event->ctx->task)
6223 perf_output_put(&handle, se->event_id.header);
6224 else
6225 perf_output_put(&handle, se->event_id);
6226
6227 perf_event__output_id_sample(event, &handle, &sample);
6228
6229 perf_output_end(&handle);
6230}
6231
6232static void perf_event_switch(struct task_struct *task,
6233 struct task_struct *next_prev, bool sched_in)
6234{
6235 struct perf_switch_event switch_event;
6236
6237 /* N.B. caller checks nr_switch_events != 0 */
6238
6239 switch_event = (struct perf_switch_event){
6240 .task = task,
6241 .next_prev = next_prev,
6242 .event_id = {
6243 .header = {
6244 /* .type */
6245 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
6246 /* .size */
6247 },
6248 /* .next_prev_pid */
6249 /* .next_prev_tid */
6250 },
6251 };
6252
6253 perf_event_aux(perf_event_switch_output,
6254 &switch_event,
6255 NULL);
6256}
6257
6258/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006259 * IRQ throttle logging
6260 */
6261
6262static void perf_log_throttle(struct perf_event *event, int enable)
6263{
6264 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006265 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006266 int ret;
6267
6268 struct {
6269 struct perf_event_header header;
6270 u64 time;
6271 u64 id;
6272 u64 stream_id;
6273 } throttle_event = {
6274 .header = {
6275 .type = PERF_RECORD_THROTTLE,
6276 .misc = 0,
6277 .size = sizeof(throttle_event),
6278 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01006279 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006280 .id = primary_event_id(event),
6281 .stream_id = event->id,
6282 };
6283
6284 if (enable)
6285 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
6286
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006287 perf_event_header__init_id(&throttle_event.header, &sample, event);
6288
6289 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006290 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006291 if (ret)
6292 return;
6293
6294 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006295 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006296 perf_output_end(&handle);
6297}
6298
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006299static void perf_log_itrace_start(struct perf_event *event)
6300{
6301 struct perf_output_handle handle;
6302 struct perf_sample_data sample;
6303 struct perf_aux_event {
6304 struct perf_event_header header;
6305 u32 pid;
6306 u32 tid;
6307 } rec;
6308 int ret;
6309
6310 if (event->parent)
6311 event = event->parent;
6312
6313 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
6314 event->hw.itrace_started)
6315 return;
6316
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006317 rec.header.type = PERF_RECORD_ITRACE_START;
6318 rec.header.misc = 0;
6319 rec.header.size = sizeof(rec);
6320 rec.pid = perf_event_pid(event, current);
6321 rec.tid = perf_event_tid(event, current);
6322
6323 perf_event_header__init_id(&rec.header, &sample, event);
6324 ret = perf_output_begin(&handle, event, rec.header.size);
6325
6326 if (ret)
6327 return;
6328
6329 perf_output_put(&handle, rec);
6330 perf_event__output_id_sample(event, &handle, &sample);
6331
6332 perf_output_end(&handle);
6333}
6334
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006335/*
6336 * Generic event overflow handling, sampling.
6337 */
6338
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006339static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006340 int throttle, struct perf_sample_data *data,
6341 struct pt_regs *regs)
6342{
6343 int events = atomic_read(&event->event_limit);
6344 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006345 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006346 int ret = 0;
6347
Peter Zijlstra96398822010-11-24 18:55:29 +01006348 /*
6349 * Non-sampling counters might still use the PMI to fold short
6350 * hardware counters, ignore those.
6351 */
6352 if (unlikely(!is_sampling_event(event)))
6353 return 0;
6354
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006355 seq = __this_cpu_read(perf_throttled_seq);
6356 if (seq != hwc->interrupts_seq) {
6357 hwc->interrupts_seq = seq;
6358 hwc->interrupts = 1;
6359 } else {
6360 hwc->interrupts++;
6361 if (unlikely(throttle
6362 && hwc->interrupts >= max_samples_per_tick)) {
6363 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01006364 hwc->interrupts = MAX_INTERRUPTS;
6365 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02006366 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006367 ret = 1;
6368 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006369 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006370
6371 if (event->attr.freq) {
6372 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01006373 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006374
Peter Zijlstraabd50712010-01-26 18:50:16 +01006375 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006376
Peter Zijlstraabd50712010-01-26 18:50:16 +01006377 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01006378 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006379 }
6380
6381 /*
6382 * XXX event_limit might not quite work as expected on inherited
6383 * events
6384 */
6385
6386 event->pending_kill = POLL_IN;
6387 if (events && atomic_dec_and_test(&event->event_limit)) {
6388 ret = 1;
6389 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006390 event->pending_disable = 1;
6391 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006392 }
6393
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006394 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006395 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006396 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006397 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006398
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006399 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006400 event->pending_wakeup = 1;
6401 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02006402 }
6403
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006404 return ret;
6405}
6406
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006407int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006408 struct perf_sample_data *data,
6409 struct pt_regs *regs)
6410{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006411 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006412}
6413
6414/*
6415 * Generic software event infrastructure
6416 */
6417
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006418struct swevent_htable {
6419 struct swevent_hlist *swevent_hlist;
6420 struct mutex hlist_mutex;
6421 int hlist_refcount;
6422
6423 /* Recursion avoidance in each contexts */
6424 int recursion[PERF_NR_CONTEXTS];
6425};
6426
6427static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
6428
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006429/*
6430 * We directly increment event->count and keep a second value in
6431 * event->hw.period_left to count intervals. This period event
6432 * is kept in the range [-sample_period, 0] so that we can use the
6433 * sign as trigger.
6434 */
6435
Jiri Olsaab573842013-05-01 17:25:44 +02006436u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006437{
6438 struct hw_perf_event *hwc = &event->hw;
6439 u64 period = hwc->last_period;
6440 u64 nr, offset;
6441 s64 old, val;
6442
6443 hwc->last_period = hwc->sample_period;
6444
6445again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02006446 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006447 if (val < 0)
6448 return 0;
6449
6450 nr = div64_u64(period + val, period);
6451 offset = nr * period;
6452 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02006453 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006454 goto again;
6455
6456 return nr;
6457}
6458
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006459static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006460 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006461 struct pt_regs *regs)
6462{
6463 struct hw_perf_event *hwc = &event->hw;
6464 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006465
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006466 if (!overflow)
6467 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006468
6469 if (hwc->interrupts == MAX_INTERRUPTS)
6470 return;
6471
6472 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006473 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006474 data, regs)) {
6475 /*
6476 * We inhibit the overflow from happening when
6477 * hwc->interrupts == MAX_INTERRUPTS.
6478 */
6479 break;
6480 }
6481 throttle = 1;
6482 }
6483}
6484
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006485static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006486 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006487 struct pt_regs *regs)
6488{
6489 struct hw_perf_event *hwc = &event->hw;
6490
Peter Zijlstrae7850592010-05-21 14:43:08 +02006491 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006492
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006493 if (!regs)
6494 return;
6495
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006496 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006497 return;
6498
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03006499 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
6500 data->period = nr;
6501 return perf_swevent_overflow(event, 1, data, regs);
6502 } else
6503 data->period = event->hw.last_period;
6504
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006505 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006506 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006507
Peter Zijlstrae7850592010-05-21 14:43:08 +02006508 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006509 return;
6510
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006511 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006512}
6513
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006514static int perf_exclude_event(struct perf_event *event,
6515 struct pt_regs *regs)
6516{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006517 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01006518 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006519
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006520 if (regs) {
6521 if (event->attr.exclude_user && user_mode(regs))
6522 return 1;
6523
6524 if (event->attr.exclude_kernel && !user_mode(regs))
6525 return 1;
6526 }
6527
6528 return 0;
6529}
6530
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006531static int perf_swevent_match(struct perf_event *event,
6532 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08006533 u32 event_id,
6534 struct perf_sample_data *data,
6535 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006536{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006537 if (event->attr.type != type)
6538 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006539
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006540 if (event->attr.config != event_id)
6541 return 0;
6542
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006543 if (perf_exclude_event(event, regs))
6544 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006545
6546 return 1;
6547}
6548
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006549static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006550{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006551 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006552
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006553 return hash_64(val, SWEVENT_HLIST_BITS);
6554}
6555
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006556static inline struct hlist_head *
6557__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006558{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006559 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006560
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006561 return &hlist->heads[hash];
6562}
6563
6564/* For the read side: events when they trigger */
6565static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006566find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006567{
6568 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006569
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006570 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006571 if (!hlist)
6572 return NULL;
6573
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006574 return __find_swevent_head(hlist, type, event_id);
6575}
6576
6577/* For the event head insertion and removal in the hlist */
6578static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006579find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006580{
6581 struct swevent_hlist *hlist;
6582 u32 event_id = event->attr.config;
6583 u64 type = event->attr.type;
6584
6585 /*
6586 * Event scheduling is always serialized against hlist allocation
6587 * and release. Which makes the protected version suitable here.
6588 * The context lock guarantees that.
6589 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006590 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006591 lockdep_is_held(&event->ctx->lock));
6592 if (!hlist)
6593 return NULL;
6594
6595 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006596}
6597
6598static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006599 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006600 struct perf_sample_data *data,
6601 struct pt_regs *regs)
6602{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006603 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006604 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006605 struct hlist_head *head;
6606
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006607 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006608 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006609 if (!head)
6610 goto end;
6611
Sasha Levinb67bfe02013-02-27 17:06:00 -08006612 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08006613 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006614 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006615 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006616end:
6617 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006618}
6619
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006620DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
6621
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006622int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006623{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006624 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006625
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006626 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006627}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01006628EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006629
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01006630inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006631{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006632 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006633
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006634 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006635}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006636
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006637void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006638{
Ingo Molnara4234bf2009-11-23 10:57:59 +01006639 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006640
6641 if (WARN_ON_ONCE(!regs))
6642 return;
6643
6644 perf_sample_data_init(&data, addr, 0);
6645 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
6646}
6647
6648void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
6649{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006650 int rctx;
6651
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006652 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006653 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006654 if (unlikely(rctx < 0))
6655 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006656
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006657 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006658
6659 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006660fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006661 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006662}
6663
6664static void perf_swevent_read(struct perf_event *event)
6665{
6666}
6667
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006668static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006669{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006670 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006671 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006672 struct hlist_head *head;
6673
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006674 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006675 hwc->last_period = hwc->sample_period;
6676 perf_swevent_set_period(event);
6677 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006678
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006679 hwc->state = !(flags & PERF_EF_START);
6680
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006681 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01006682 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006683 return -EINVAL;
6684
6685 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08006686 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006687
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006688 return 0;
6689}
6690
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006691static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006692{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006693 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006694}
6695
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006696static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006697{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006698 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006699}
6700
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006701static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006702{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006703 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006704}
6705
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006706/* Deref the hlist from the update side */
6707static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006708swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006709{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006710 return rcu_dereference_protected(swhash->swevent_hlist,
6711 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006712}
6713
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006714static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006715{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006716 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006717
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006718 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006719 return;
6720
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03006721 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08006722 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006723}
6724
6725static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
6726{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006727 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006728
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006729 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006730
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006731 if (!--swhash->hlist_refcount)
6732 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006733
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006734 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006735}
6736
6737static void swevent_hlist_put(struct perf_event *event)
6738{
6739 int cpu;
6740
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006741 for_each_possible_cpu(cpu)
6742 swevent_hlist_put_cpu(event, cpu);
6743}
6744
6745static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
6746{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006747 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006748 int err = 0;
6749
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006750 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006751 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006752 struct swevent_hlist *hlist;
6753
6754 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
6755 if (!hlist) {
6756 err = -ENOMEM;
6757 goto exit;
6758 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006759 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006760 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006761 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006762exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006763 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006764
6765 return err;
6766}
6767
6768static int swevent_hlist_get(struct perf_event *event)
6769{
6770 int err;
6771 int cpu, failed_cpu;
6772
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006773 get_online_cpus();
6774 for_each_possible_cpu(cpu) {
6775 err = swevent_hlist_get_cpu(event, cpu);
6776 if (err) {
6777 failed_cpu = cpu;
6778 goto fail;
6779 }
6780 }
6781 put_online_cpus();
6782
6783 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006784fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006785 for_each_possible_cpu(cpu) {
6786 if (cpu == failed_cpu)
6787 break;
6788 swevent_hlist_put_cpu(event, cpu);
6789 }
6790
6791 put_online_cpus();
6792 return err;
6793}
6794
Ingo Molnarc5905af2012-02-24 08:31:31 +01006795struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006796
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006797static void sw_perf_event_destroy(struct perf_event *event)
6798{
6799 u64 event_id = event->attr.config;
6800
6801 WARN_ON(event->parent);
6802
Ingo Molnarc5905af2012-02-24 08:31:31 +01006803 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006804 swevent_hlist_put(event);
6805}
6806
6807static int perf_swevent_init(struct perf_event *event)
6808{
Tommi Rantala8176cce2013-04-13 22:49:14 +03006809 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006810
6811 if (event->attr.type != PERF_TYPE_SOFTWARE)
6812 return -ENOENT;
6813
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006814 /*
6815 * no branch sampling for software events
6816 */
6817 if (has_branch_stack(event))
6818 return -EOPNOTSUPP;
6819
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006820 switch (event_id) {
6821 case PERF_COUNT_SW_CPU_CLOCK:
6822 case PERF_COUNT_SW_TASK_CLOCK:
6823 return -ENOENT;
6824
6825 default:
6826 break;
6827 }
6828
Dan Carpenterce677832010-10-24 21:50:42 +02006829 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006830 return -ENOENT;
6831
6832 if (!event->parent) {
6833 int err;
6834
6835 err = swevent_hlist_get(event);
6836 if (err)
6837 return err;
6838
Ingo Molnarc5905af2012-02-24 08:31:31 +01006839 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006840 event->destroy = sw_perf_event_destroy;
6841 }
6842
6843 return 0;
6844}
6845
6846static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006847 .task_ctx_nr = perf_sw_context,
6848
Peter Zijlstra34f43922015-02-20 14:05:38 +01006849 .capabilities = PERF_PMU_CAP_NO_NMI,
6850
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006851 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006852 .add = perf_swevent_add,
6853 .del = perf_swevent_del,
6854 .start = perf_swevent_start,
6855 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006856 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006857};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006858
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006859#ifdef CONFIG_EVENT_TRACING
6860
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006861static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006862 struct perf_sample_data *data)
6863{
6864 void *record = data->raw->data;
6865
Peter Zijlstrab71b4372015-11-02 10:50:51 +01006866 /* only top level events have filters set */
6867 if (event->parent)
6868 event = event->parent;
6869
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006870 if (likely(!event->filter) || filter_match_preds(event->filter, record))
6871 return 1;
6872 return 0;
6873}
6874
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006875static int perf_tp_event_match(struct perf_event *event,
6876 struct perf_sample_data *data,
6877 struct pt_regs *regs)
6878{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01006879 if (event->hw.state & PERF_HES_STOPPED)
6880 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02006881 /*
6882 * All tracepoints are from kernel-space.
6883 */
6884 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006885 return 0;
6886
6887 if (!perf_tp_filter_match(event, data))
6888 return 0;
6889
6890 return 1;
6891}
6892
6893void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006894 struct pt_regs *regs, struct hlist_head *head, int rctx,
6895 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006896{
6897 struct perf_sample_data data;
6898 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006899
6900 struct perf_raw_record raw = {
6901 .size = entry_size,
6902 .data = record,
6903 };
6904
Robert Richterfd0d0002012-04-02 20:19:08 +02006905 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006906 data.raw = &raw;
6907
Sasha Levinb67bfe02013-02-27 17:06:00 -08006908 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006909 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006910 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006911 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006912
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006913 /*
6914 * If we got specified a target task, also iterate its context and
6915 * deliver this event there too.
6916 */
6917 if (task && task != current) {
6918 struct perf_event_context *ctx;
6919 struct trace_entry *entry = record;
6920
6921 rcu_read_lock();
6922 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
6923 if (!ctx)
6924 goto unlock;
6925
6926 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
6927 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6928 continue;
6929 if (event->attr.config != entry->type)
6930 continue;
6931 if (perf_tp_event_match(event, &data, regs))
6932 perf_swevent_event(event, count, &data, regs);
6933 }
6934unlock:
6935 rcu_read_unlock();
6936 }
6937
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006938 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006939}
6940EXPORT_SYMBOL_GPL(perf_tp_event);
6941
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006942static void tp_perf_event_destroy(struct perf_event *event)
6943{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006944 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006945}
6946
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006947static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006948{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006949 int err;
6950
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006951 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6952 return -ENOENT;
6953
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006954 /*
6955 * no branch sampling for tracepoint events
6956 */
6957 if (has_branch_stack(event))
6958 return -EOPNOTSUPP;
6959
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006960 err = perf_trace_init(event);
6961 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006962 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006963
6964 event->destroy = tp_perf_event_destroy;
6965
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006966 return 0;
6967}
6968
6969static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006970 .task_ctx_nr = perf_sw_context,
6971
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006972 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006973 .add = perf_trace_add,
6974 .del = perf_trace_del,
6975 .start = perf_swevent_start,
6976 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006977 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006978};
6979
6980static inline void perf_tp_register(void)
6981{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006982 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006983}
Li Zefan6fb29152009-10-15 11:21:42 +08006984
6985static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6986{
6987 char *filter_str;
6988 int ret;
6989
6990 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6991 return -EINVAL;
6992
6993 filter_str = strndup_user(arg, PAGE_SIZE);
6994 if (IS_ERR(filter_str))
6995 return PTR_ERR(filter_str);
6996
6997 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
6998
6999 kfree(filter_str);
7000 return ret;
7001}
7002
7003static void perf_event_free_filter(struct perf_event *event)
7004{
7005 ftrace_profile_free_filter(event);
7006}
7007
Alexei Starovoitov25415172015-03-25 12:49:20 -07007008static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7009{
7010 struct bpf_prog *prog;
7011
7012 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7013 return -EINVAL;
7014
7015 if (event->tp_event->prog)
7016 return -EEXIST;
7017
Wang Nan04a22fa2015-07-01 02:13:50 +00007018 if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE))
7019 /* bpf programs can only be attached to u/kprobes */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007020 return -EINVAL;
7021
7022 prog = bpf_prog_get(prog_fd);
7023 if (IS_ERR(prog))
7024 return PTR_ERR(prog);
7025
Linus Torvalds6c373ca2015-04-15 09:00:47 -07007026 if (prog->type != BPF_PROG_TYPE_KPROBE) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007027 /* valid fd, but invalid bpf program type */
7028 bpf_prog_put(prog);
7029 return -EINVAL;
7030 }
7031
7032 event->tp_event->prog = prog;
7033
7034 return 0;
7035}
7036
7037static void perf_event_free_bpf_prog(struct perf_event *event)
7038{
7039 struct bpf_prog *prog;
7040
7041 if (!event->tp_event)
7042 return;
7043
7044 prog = event->tp_event->prog;
7045 if (prog) {
7046 event->tp_event->prog = NULL;
7047 bpf_prog_put(prog);
7048 }
7049}
7050
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007051#else
Li Zefan6fb29152009-10-15 11:21:42 +08007052
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007053static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007054{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007055}
Li Zefan6fb29152009-10-15 11:21:42 +08007056
7057static int perf_event_set_filter(struct perf_event *event, void __user *arg)
7058{
7059 return -ENOENT;
7060}
7061
7062static void perf_event_free_filter(struct perf_event *event)
7063{
7064}
7065
Alexei Starovoitov25415172015-03-25 12:49:20 -07007066static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7067{
7068 return -ENOENT;
7069}
7070
7071static void perf_event_free_bpf_prog(struct perf_event *event)
7072{
7073}
Li Zefan07b139c2009-12-21 14:27:35 +08007074#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007075
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007076#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007077void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007078{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007079 struct perf_sample_data sample;
7080 struct pt_regs *regs = data;
7081
Robert Richterfd0d0002012-04-02 20:19:08 +02007082 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007083
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007084 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007085 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007086}
7087#endif
7088
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007089/*
7090 * hrtimer based swevent callback
7091 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007092
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007093static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007094{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007095 enum hrtimer_restart ret = HRTIMER_RESTART;
7096 struct perf_sample_data data;
7097 struct pt_regs *regs;
7098 struct perf_event *event;
7099 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007100
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007101 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007102
7103 if (event->state != PERF_EVENT_STATE_ACTIVE)
7104 return HRTIMER_NORESTART;
7105
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007106 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007107
Robert Richterfd0d0002012-04-02 20:19:08 +02007108 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007109 regs = get_irq_regs();
7110
7111 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08007112 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02007113 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007114 ret = HRTIMER_NORESTART;
7115 }
7116
7117 period = max_t(u64, 10000, event->hw.sample_period);
7118 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
7119
7120 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007121}
7122
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007123static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007124{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007125 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007126 s64 period;
7127
7128 if (!is_sampling_event(event))
7129 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007130
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007131 period = local64_read(&hwc->period_left);
7132 if (period) {
7133 if (period < 0)
7134 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007135
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007136 local64_set(&hwc->period_left, 0);
7137 } else {
7138 period = max_t(u64, 10000, hwc->sample_period);
7139 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00007140 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
7141 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007142}
7143
7144static void perf_swevent_cancel_hrtimer(struct perf_event *event)
7145{
7146 struct hw_perf_event *hwc = &event->hw;
7147
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007148 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007149 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007150 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007151
7152 hrtimer_cancel(&hwc->hrtimer);
7153 }
7154}
7155
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007156static void perf_swevent_init_hrtimer(struct perf_event *event)
7157{
7158 struct hw_perf_event *hwc = &event->hw;
7159
7160 if (!is_sampling_event(event))
7161 return;
7162
7163 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
7164 hwc->hrtimer.function = perf_swevent_hrtimer;
7165
7166 /*
7167 * Since hrtimers have a fixed rate, we can do a static freq->period
7168 * mapping and avoid the whole period adjust feedback stuff.
7169 */
7170 if (event->attr.freq) {
7171 long freq = event->attr.sample_freq;
7172
7173 event->attr.sample_period = NSEC_PER_SEC / freq;
7174 hwc->sample_period = event->attr.sample_period;
7175 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09007176 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007177 event->attr.freq = 0;
7178 }
7179}
7180
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007181/*
7182 * Software event: cpu wall time clock
7183 */
7184
7185static void cpu_clock_event_update(struct perf_event *event)
7186{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007187 s64 prev;
7188 u64 now;
7189
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007190 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007191 prev = local64_xchg(&event->hw.prev_count, now);
7192 local64_add(now - prev, &event->count);
7193}
7194
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007195static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007196{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007197 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007198 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007199}
7200
7201static void cpu_clock_event_stop(struct perf_event *event, int flags)
7202{
7203 perf_swevent_cancel_hrtimer(event);
7204 cpu_clock_event_update(event);
7205}
7206
7207static int cpu_clock_event_add(struct perf_event *event, int flags)
7208{
7209 if (flags & PERF_EF_START)
7210 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08007211 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007212
7213 return 0;
7214}
7215
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007216static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007217{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007218 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007219}
7220
7221static void cpu_clock_event_read(struct perf_event *event)
7222{
7223 cpu_clock_event_update(event);
7224}
7225
7226static int cpu_clock_event_init(struct perf_event *event)
7227{
7228 if (event->attr.type != PERF_TYPE_SOFTWARE)
7229 return -ENOENT;
7230
7231 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
7232 return -ENOENT;
7233
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007234 /*
7235 * no branch sampling for software events
7236 */
7237 if (has_branch_stack(event))
7238 return -EOPNOTSUPP;
7239
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007240 perf_swevent_init_hrtimer(event);
7241
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007242 return 0;
7243}
7244
7245static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007246 .task_ctx_nr = perf_sw_context,
7247
Peter Zijlstra34f43922015-02-20 14:05:38 +01007248 .capabilities = PERF_PMU_CAP_NO_NMI,
7249
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007250 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007251 .add = cpu_clock_event_add,
7252 .del = cpu_clock_event_del,
7253 .start = cpu_clock_event_start,
7254 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007255 .read = cpu_clock_event_read,
7256};
7257
7258/*
7259 * Software event: task time clock
7260 */
7261
7262static void task_clock_event_update(struct perf_event *event, u64 now)
7263{
7264 u64 prev;
7265 s64 delta;
7266
7267 prev = local64_xchg(&event->hw.prev_count, now);
7268 delta = now - prev;
7269 local64_add(delta, &event->count);
7270}
7271
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007272static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007273{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007274 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007275 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007276}
7277
7278static void task_clock_event_stop(struct perf_event *event, int flags)
7279{
7280 perf_swevent_cancel_hrtimer(event);
7281 task_clock_event_update(event, event->ctx->time);
7282}
7283
7284static int task_clock_event_add(struct perf_event *event, int flags)
7285{
7286 if (flags & PERF_EF_START)
7287 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08007288 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007289
7290 return 0;
7291}
7292
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007293static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007294{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007295 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007296}
7297
7298static void task_clock_event_read(struct perf_event *event)
7299{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01007300 u64 now = perf_clock();
7301 u64 delta = now - event->ctx->timestamp;
7302 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007303
7304 task_clock_event_update(event, time);
7305}
7306
7307static int task_clock_event_init(struct perf_event *event)
7308{
7309 if (event->attr.type != PERF_TYPE_SOFTWARE)
7310 return -ENOENT;
7311
7312 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
7313 return -ENOENT;
7314
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007315 /*
7316 * no branch sampling for software events
7317 */
7318 if (has_branch_stack(event))
7319 return -EOPNOTSUPP;
7320
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007321 perf_swevent_init_hrtimer(event);
7322
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007323 return 0;
7324}
7325
7326static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007327 .task_ctx_nr = perf_sw_context,
7328
Peter Zijlstra34f43922015-02-20 14:05:38 +01007329 .capabilities = PERF_PMU_CAP_NO_NMI,
7330
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007331 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007332 .add = task_clock_event_add,
7333 .del = task_clock_event_del,
7334 .start = task_clock_event_start,
7335 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007336 .read = task_clock_event_read,
7337};
7338
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007339static void perf_pmu_nop_void(struct pmu *pmu)
7340{
7341}
7342
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007343static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
7344{
7345}
7346
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007347static int perf_pmu_nop_int(struct pmu *pmu)
7348{
7349 return 0;
7350}
7351
Geliang Tang18ab2cd2015-09-27 23:25:50 +08007352static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007353
7354static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007355{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007356 __this_cpu_write(nop_txn_flags, flags);
7357
7358 if (flags & ~PERF_PMU_TXN_ADD)
7359 return;
7360
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007361 perf_pmu_disable(pmu);
7362}
7363
7364static int perf_pmu_commit_txn(struct pmu *pmu)
7365{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007366 unsigned int flags = __this_cpu_read(nop_txn_flags);
7367
7368 __this_cpu_write(nop_txn_flags, 0);
7369
7370 if (flags & ~PERF_PMU_TXN_ADD)
7371 return 0;
7372
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007373 perf_pmu_enable(pmu);
7374 return 0;
7375}
7376
7377static void perf_pmu_cancel_txn(struct pmu *pmu)
7378{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007379 unsigned int flags = __this_cpu_read(nop_txn_flags);
7380
7381 __this_cpu_write(nop_txn_flags, 0);
7382
7383 if (flags & ~PERF_PMU_TXN_ADD)
7384 return;
7385
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007386 perf_pmu_enable(pmu);
7387}
7388
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007389static int perf_event_idx_default(struct perf_event *event)
7390{
Peter Zijlstrac719f562014-10-21 11:10:21 +02007391 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007392}
7393
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007394/*
7395 * Ensures all contexts with the same task_ctx_nr have the same
7396 * pmu_cpu_context too.
7397 */
Mark Rutland9e317042014-02-10 17:44:18 +00007398static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007399{
7400 struct pmu *pmu;
7401
7402 if (ctxn < 0)
7403 return NULL;
7404
7405 list_for_each_entry(pmu, &pmus, entry) {
7406 if (pmu->task_ctx_nr == ctxn)
7407 return pmu->pmu_cpu_context;
7408 }
7409
7410 return NULL;
7411}
7412
Peter Zijlstra51676952010-12-07 14:18:20 +01007413static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007414{
Peter Zijlstra51676952010-12-07 14:18:20 +01007415 int cpu;
7416
7417 for_each_possible_cpu(cpu) {
7418 struct perf_cpu_context *cpuctx;
7419
7420 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
7421
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007422 if (cpuctx->unique_pmu == old_pmu)
7423 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01007424 }
7425}
7426
7427static void free_pmu_context(struct pmu *pmu)
7428{
7429 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007430
7431 mutex_lock(&pmus_lock);
7432 /*
7433 * Like a real lame refcount.
7434 */
Peter Zijlstra51676952010-12-07 14:18:20 +01007435 list_for_each_entry(i, &pmus, entry) {
7436 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
7437 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007438 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01007439 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007440 }
7441
Peter Zijlstra51676952010-12-07 14:18:20 +01007442 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007443out:
7444 mutex_unlock(&pmus_lock);
7445}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007446static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007447
Peter Zijlstraabe43402010-11-17 23:17:37 +01007448static ssize_t
7449type_show(struct device *dev, struct device_attribute *attr, char *page)
7450{
7451 struct pmu *pmu = dev_get_drvdata(dev);
7452
7453 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
7454}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007455static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007456
Stephane Eranian62b85632013-04-03 14:21:34 +02007457static ssize_t
7458perf_event_mux_interval_ms_show(struct device *dev,
7459 struct device_attribute *attr,
7460 char *page)
7461{
7462 struct pmu *pmu = dev_get_drvdata(dev);
7463
7464 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
7465}
7466
Peter Zijlstra272325c2015-04-15 11:41:58 +02007467static DEFINE_MUTEX(mux_interval_mutex);
7468
Stephane Eranian62b85632013-04-03 14:21:34 +02007469static ssize_t
7470perf_event_mux_interval_ms_store(struct device *dev,
7471 struct device_attribute *attr,
7472 const char *buf, size_t count)
7473{
7474 struct pmu *pmu = dev_get_drvdata(dev);
7475 int timer, cpu, ret;
7476
7477 ret = kstrtoint(buf, 0, &timer);
7478 if (ret)
7479 return ret;
7480
7481 if (timer < 1)
7482 return -EINVAL;
7483
7484 /* same value, noting to do */
7485 if (timer == pmu->hrtimer_interval_ms)
7486 return count;
7487
Peter Zijlstra272325c2015-04-15 11:41:58 +02007488 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02007489 pmu->hrtimer_interval_ms = timer;
7490
7491 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02007492 get_online_cpus();
7493 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02007494 struct perf_cpu_context *cpuctx;
7495 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
7496 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
7497
Peter Zijlstra272325c2015-04-15 11:41:58 +02007498 cpu_function_call(cpu,
7499 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02007500 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02007501 put_online_cpus();
7502 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02007503
7504 return count;
7505}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007506static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02007507
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007508static struct attribute *pmu_dev_attrs[] = {
7509 &dev_attr_type.attr,
7510 &dev_attr_perf_event_mux_interval_ms.attr,
7511 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01007512};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007513ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007514
7515static int pmu_bus_running;
7516static struct bus_type pmu_bus = {
7517 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007518 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01007519};
7520
7521static void pmu_dev_release(struct device *dev)
7522{
7523 kfree(dev);
7524}
7525
7526static int pmu_dev_alloc(struct pmu *pmu)
7527{
7528 int ret = -ENOMEM;
7529
7530 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
7531 if (!pmu->dev)
7532 goto out;
7533
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01007534 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01007535 device_initialize(pmu->dev);
7536 ret = dev_set_name(pmu->dev, "%s", pmu->name);
7537 if (ret)
7538 goto free_dev;
7539
7540 dev_set_drvdata(pmu->dev, pmu);
7541 pmu->dev->bus = &pmu_bus;
7542 pmu->dev->release = pmu_dev_release;
7543 ret = device_add(pmu->dev);
7544 if (ret)
7545 goto free_dev;
7546
7547out:
7548 return ret;
7549
7550free_dev:
7551 put_device(pmu->dev);
7552 goto out;
7553}
7554
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007555static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007556static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007557
Mischa Jonker03d8e802013-06-04 11:45:48 +02007558int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007559{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007560 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007561
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007562 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007563 ret = -ENOMEM;
7564 pmu->pmu_disable_count = alloc_percpu(int);
7565 if (!pmu->pmu_disable_count)
7566 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007567
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007568 pmu->type = -1;
7569 if (!name)
7570 goto skip_type;
7571 pmu->name = name;
7572
7573 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08007574 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
7575 if (type < 0) {
7576 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007577 goto free_pdc;
7578 }
7579 }
7580 pmu->type = type;
7581
Peter Zijlstraabe43402010-11-17 23:17:37 +01007582 if (pmu_bus_running) {
7583 ret = pmu_dev_alloc(pmu);
7584 if (ret)
7585 goto free_idr;
7586 }
7587
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007588skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007589 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
7590 if (pmu->pmu_cpu_context)
7591 goto got_cpu_context;
7592
Wei Yongjunc4814202013-04-12 11:05:54 +08007593 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007594 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
7595 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01007596 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007597
7598 for_each_possible_cpu(cpu) {
7599 struct perf_cpu_context *cpuctx;
7600
7601 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02007602 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007603 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007604 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007605 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02007606
Peter Zijlstra272325c2015-04-15 11:41:58 +02007607 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02007608
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007609 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007610 }
7611
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007612got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007613 if (!pmu->start_txn) {
7614 if (pmu->pmu_enable) {
7615 /*
7616 * If we have pmu_enable/pmu_disable calls, install
7617 * transaction stubs that use that to try and batch
7618 * hardware accesses.
7619 */
7620 pmu->start_txn = perf_pmu_start_txn;
7621 pmu->commit_txn = perf_pmu_commit_txn;
7622 pmu->cancel_txn = perf_pmu_cancel_txn;
7623 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007624 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007625 pmu->commit_txn = perf_pmu_nop_int;
7626 pmu->cancel_txn = perf_pmu_nop_void;
7627 }
7628 }
7629
7630 if (!pmu->pmu_enable) {
7631 pmu->pmu_enable = perf_pmu_nop_void;
7632 pmu->pmu_disable = perf_pmu_nop_void;
7633 }
7634
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007635 if (!pmu->event_idx)
7636 pmu->event_idx = perf_event_idx_default;
7637
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007638 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007639 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007640 ret = 0;
7641unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007642 mutex_unlock(&pmus_lock);
7643
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007644 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007645
Peter Zijlstraabe43402010-11-17 23:17:37 +01007646free_dev:
7647 device_del(pmu->dev);
7648 put_device(pmu->dev);
7649
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007650free_idr:
7651 if (pmu->type >= PERF_TYPE_MAX)
7652 idr_remove(&pmu_idr, pmu->type);
7653
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007654free_pdc:
7655 free_percpu(pmu->pmu_disable_count);
7656 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007657}
Yan, Zhengc464c762014-03-18 16:56:41 +08007658EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007659
7660void perf_pmu_unregister(struct pmu *pmu)
7661{
7662 mutex_lock(&pmus_lock);
7663 list_del_rcu(&pmu->entry);
7664 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007665
7666 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02007667 * We dereference the pmu list under both SRCU and regular RCU, so
7668 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007669 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007670 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02007671 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007672
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007673 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007674 if (pmu->type >= PERF_TYPE_MAX)
7675 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007676 device_del(pmu->dev);
7677 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01007678 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007679}
Yan, Zhengc464c762014-03-18 16:56:41 +08007680EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007681
Mark Rutlandcc34b982015-01-07 14:56:51 +00007682static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
7683{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007684 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00007685 int ret;
7686
7687 if (!try_module_get(pmu->module))
7688 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007689
7690 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02007691 /*
7692 * This ctx->mutex can nest when we're called through
7693 * inheritance. See the perf_event_ctx_lock_nested() comment.
7694 */
7695 ctx = perf_event_ctx_lock_nested(event->group_leader,
7696 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007697 BUG_ON(!ctx);
7698 }
7699
Mark Rutlandcc34b982015-01-07 14:56:51 +00007700 event->pmu = pmu;
7701 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007702
7703 if (ctx)
7704 perf_event_ctx_unlock(event->group_leader, ctx);
7705
Mark Rutlandcc34b982015-01-07 14:56:51 +00007706 if (ret)
7707 module_put(pmu->module);
7708
7709 return ret;
7710}
7711
Geliang Tang18ab2cd2015-09-27 23:25:50 +08007712static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007713{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007714 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007715 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08007716 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007717
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007718 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007719
7720 rcu_read_lock();
7721 pmu = idr_find(&pmu_idr, event->attr.type);
7722 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08007723 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007724 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08007725 if (ret)
7726 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007727 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08007728 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007729
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007730 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007731 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007732 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007733 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007734
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007735 if (ret != -ENOENT) {
7736 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007737 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007738 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007739 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007740 pmu = ERR_PTR(-ENOENT);
7741unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007742 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007743
7744 return pmu;
7745}
7746
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007747static void account_event_cpu(struct perf_event *event, int cpu)
7748{
7749 if (event->parent)
7750 return;
7751
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007752 if (is_cgroup_event(event))
7753 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
7754}
7755
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007756static void account_event(struct perf_event *event)
7757{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007758 bool inc = false;
7759
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007760 if (event->parent)
7761 return;
7762
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007763 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007764 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007765 if (event->attr.mmap || event->attr.mmap_data)
7766 atomic_inc(&nr_mmap_events);
7767 if (event->attr.comm)
7768 atomic_inc(&nr_comm_events);
7769 if (event->attr.task)
7770 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02007771 if (event->attr.freq) {
7772 if (atomic_inc_return(&nr_freq_events) == 1)
7773 tick_nohz_full_kick_all();
7774 }
Adrian Hunter45ac1402015-07-21 12:44:02 +03007775 if (event->attr.context_switch) {
7776 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007777 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03007778 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007779 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007780 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007781 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007782 inc = true;
7783
7784 if (inc)
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007785 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007786
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007787 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007788}
7789
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007790/*
7791 * Allocate and initialize a event structure
7792 */
7793static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007794perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007795 struct task_struct *task,
7796 struct perf_event *group_leader,
7797 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007798 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00007799 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007800{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007801 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007802 struct perf_event *event;
7803 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007804 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007805
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007806 if ((unsigned)cpu >= nr_cpu_ids) {
7807 if (!task || cpu != -1)
7808 return ERR_PTR(-EINVAL);
7809 }
7810
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007811 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007812 if (!event)
7813 return ERR_PTR(-ENOMEM);
7814
7815 /*
7816 * Single events are their own group leaders, with an
7817 * empty sibling list:
7818 */
7819 if (!group_leader)
7820 group_leader = event;
7821
7822 mutex_init(&event->child_mutex);
7823 INIT_LIST_HEAD(&event->child_list);
7824
7825 INIT_LIST_HEAD(&event->group_entry);
7826 INIT_LIST_HEAD(&event->event_entry);
7827 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007828 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01007829 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01007830 INIT_HLIST_NODE(&event->hlist_entry);
7831
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007832
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007833 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08007834 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007835
7836 mutex_init(&event->mmap_mutex);
7837
Al Viroa6fa9412012-08-20 14:59:25 +01007838 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007839 event->cpu = cpu;
7840 event->attr = *attr;
7841 event->group_leader = group_leader;
7842 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007843 event->oncpu = -1;
7844
7845 event->parent = parent_event;
7846
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08007847 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007848 event->id = atomic64_inc_return(&perf_event_id);
7849
7850 event->state = PERF_EVENT_STATE_INACTIVE;
7851
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007852 if (task) {
7853 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007854 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01007855 * XXX pmu::event_init needs to know what task to account to
7856 * and we cannot use the ctx information because we need the
7857 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007858 */
Peter Zijlstra50f16a82015-03-05 22:10:19 +01007859 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007860 }
7861
Peter Zijlstra34f43922015-02-20 14:05:38 +01007862 event->clock = &local_clock;
7863 if (parent_event)
7864 event->clock = parent_event->clock;
7865
Avi Kivity4dc0da82011-06-29 18:42:35 +03007866 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007867 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007868 context = parent_event->overflow_handler_context;
7869 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007870
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007871 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007872 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02007873
Jiri Olsa0231bb52013-02-01 11:23:45 +01007874 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007875
7876 pmu = NULL;
7877
7878 hwc = &event->hw;
7879 hwc->sample_period = attr->sample_period;
7880 if (attr->freq && attr->sample_freq)
7881 hwc->sample_period = 1;
7882 hwc->last_period = hwc->sample_period;
7883
Peter Zijlstrae7850592010-05-21 14:43:08 +02007884 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007885
7886 /*
7887 * we currently do not support PERF_FORMAT_GROUP on inherited events
7888 */
7889 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007890 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007891
Yan, Zhenga46a2302014-11-04 21:56:06 -05007892 if (!has_branch_stack(event))
7893 event->attr.branch_sample_type = 0;
7894
Matt Fleming79dff512015-01-23 18:45:42 +00007895 if (cgroup_fd != -1) {
7896 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
7897 if (err)
7898 goto err_ns;
7899 }
7900
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007901 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007902 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007903 goto err_ns;
7904 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007905 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007906 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007907 }
7908
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007909 err = exclusive_event_init(event);
7910 if (err)
7911 goto err_pmu;
7912
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007913 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007914 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
7915 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007916 if (err)
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007917 goto err_per_task;
Stephane Eraniand010b332012-02-09 23:21:00 +01007918 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007919 }
7920
7921 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007922
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007923err_per_task:
7924 exclusive_event_destroy(event);
7925
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007926err_pmu:
7927 if (event->destroy)
7928 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08007929 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007930err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00007931 if (is_cgroup_event(event))
7932 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007933 if (event->ns)
7934 put_pid_ns(event->ns);
7935 kfree(event);
7936
7937 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007938}
7939
7940static int perf_copy_attr(struct perf_event_attr __user *uattr,
7941 struct perf_event_attr *attr)
7942{
7943 u32 size;
7944 int ret;
7945
7946 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
7947 return -EFAULT;
7948
7949 /*
7950 * zero the full structure, so that a short copy will be nice.
7951 */
7952 memset(attr, 0, sizeof(*attr));
7953
7954 ret = get_user(size, &uattr->size);
7955 if (ret)
7956 return ret;
7957
7958 if (size > PAGE_SIZE) /* silly large */
7959 goto err_size;
7960
7961 if (!size) /* abi compat */
7962 size = PERF_ATTR_SIZE_VER0;
7963
7964 if (size < PERF_ATTR_SIZE_VER0)
7965 goto err_size;
7966
7967 /*
7968 * If we're handed a bigger struct than we know of,
7969 * ensure all the unknown bits are 0 - i.e. new
7970 * user-space does not rely on any kernel feature
7971 * extensions we dont know about yet.
7972 */
7973 if (size > sizeof(*attr)) {
7974 unsigned char __user *addr;
7975 unsigned char __user *end;
7976 unsigned char val;
7977
7978 addr = (void __user *)uattr + sizeof(*attr);
7979 end = (void __user *)uattr + size;
7980
7981 for (; addr < end; addr++) {
7982 ret = get_user(val, addr);
7983 if (ret)
7984 return ret;
7985 if (val)
7986 goto err_size;
7987 }
7988 size = sizeof(*attr);
7989 }
7990
7991 ret = copy_from_user(attr, uattr, size);
7992 if (ret)
7993 return -EFAULT;
7994
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05307995 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007996 return -EINVAL;
7997
7998 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
7999 return -EINVAL;
8000
8001 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
8002 return -EINVAL;
8003
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008004 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
8005 u64 mask = attr->branch_sample_type;
8006
8007 /* only using defined bits */
8008 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
8009 return -EINVAL;
8010
8011 /* at least one branch bit must be set */
8012 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
8013 return -EINVAL;
8014
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008015 /* propagate priv level, when not set for branch */
8016 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
8017
8018 /* exclude_kernel checked on syscall entry */
8019 if (!attr->exclude_kernel)
8020 mask |= PERF_SAMPLE_BRANCH_KERNEL;
8021
8022 if (!attr->exclude_user)
8023 mask |= PERF_SAMPLE_BRANCH_USER;
8024
8025 if (!attr->exclude_hv)
8026 mask |= PERF_SAMPLE_BRANCH_HV;
8027 /*
8028 * adjust user setting (for HW filter setup)
8029 */
8030 attr->branch_sample_type = mask;
8031 }
Stephane Eraniane7122092013-06-06 11:02:04 +02008032 /* privileged levels capture (kernel, hv): check permissions */
8033 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02008034 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8035 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008036 }
Jiri Olsa40189942012-08-07 15:20:37 +02008037
Jiri Olsac5ebced2012-08-07 15:20:40 +02008038 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02008039 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02008040 if (ret)
8041 return ret;
8042 }
8043
8044 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
8045 if (!arch_perf_have_user_stack_dump())
8046 return -ENOSYS;
8047
8048 /*
8049 * We have __u32 type for the size, but so far
8050 * we can only use __u16 as maximum due to the
8051 * __u16 sample size limit.
8052 */
8053 if (attr->sample_stack_user >= USHRT_MAX)
8054 ret = -EINVAL;
8055 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
8056 ret = -EINVAL;
8057 }
Jiri Olsa40189942012-08-07 15:20:37 +02008058
Stephane Eranian60e23642014-09-24 13:48:37 +02008059 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
8060 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008061out:
8062 return ret;
8063
8064err_size:
8065 put_user(sizeof(*attr), &uattr->size);
8066 ret = -E2BIG;
8067 goto out;
8068}
8069
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008070static int
8071perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008072{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01008073 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008074 int ret = -EINVAL;
8075
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008076 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008077 goto set;
8078
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008079 /* don't allow circular references */
8080 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008081 goto out;
8082
Peter Zijlstra0f139302010-05-20 14:35:15 +02008083 /*
8084 * Don't allow cross-cpu buffers
8085 */
8086 if (output_event->cpu != event->cpu)
8087 goto out;
8088
8089 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02008090 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02008091 */
8092 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
8093 goto out;
8094
Peter Zijlstra34f43922015-02-20 14:05:38 +01008095 /*
8096 * Mixing clocks in the same buffer is trouble you don't need.
8097 */
8098 if (output_event->clock != event->clock)
8099 goto out;
8100
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02008101 /*
8102 * If both events generate aux data, they must be on the same PMU
8103 */
8104 if (has_aux(event) && has_aux(output_event) &&
8105 event->pmu != output_event->pmu)
8106 goto out;
8107
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008108set:
8109 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008110 /* Can't redirect output if we've got an active mmap() */
8111 if (atomic_read(&event->mmap_count))
8112 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008113
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008114 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02008115 /* get the rb we want to redirect to */
8116 rb = ring_buffer_get(output_event);
8117 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008118 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008119 }
8120
Peter Zijlstrab69cf532014-03-14 10:50:33 +01008121 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02008122
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008123 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008124unlock:
8125 mutex_unlock(&event->mmap_mutex);
8126
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008127out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008128 return ret;
8129}
8130
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008131static void mutex_lock_double(struct mutex *a, struct mutex *b)
8132{
8133 if (b < a)
8134 swap(a, b);
8135
8136 mutex_lock(a);
8137 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
8138}
8139
Peter Zijlstra34f43922015-02-20 14:05:38 +01008140static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
8141{
8142 bool nmi_safe = false;
8143
8144 switch (clk_id) {
8145 case CLOCK_MONOTONIC:
8146 event->clock = &ktime_get_mono_fast_ns;
8147 nmi_safe = true;
8148 break;
8149
8150 case CLOCK_MONOTONIC_RAW:
8151 event->clock = &ktime_get_raw_fast_ns;
8152 nmi_safe = true;
8153 break;
8154
8155 case CLOCK_REALTIME:
8156 event->clock = &ktime_get_real_ns;
8157 break;
8158
8159 case CLOCK_BOOTTIME:
8160 event->clock = &ktime_get_boot_ns;
8161 break;
8162
8163 case CLOCK_TAI:
8164 event->clock = &ktime_get_tai_ns;
8165 break;
8166
8167 default:
8168 return -EINVAL;
8169 }
8170
8171 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
8172 return -EINVAL;
8173
8174 return 0;
8175}
8176
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008177/**
8178 * sys_perf_event_open - open a performance event, associate it to a task/cpu
8179 *
8180 * @attr_uptr: event_id type attributes for monitoring/sampling
8181 * @pid: target pid
8182 * @cpu: target cpu
8183 * @group_fd: group leader event fd
8184 */
8185SYSCALL_DEFINE5(perf_event_open,
8186 struct perf_event_attr __user *, attr_uptr,
8187 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
8188{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008189 struct perf_event *group_leader = NULL, *output_event = NULL;
8190 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008191 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008192 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008193 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04008194 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07008195 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008196 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04008197 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008198 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008199 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01008200 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00008201 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008202
8203 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02008204 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008205 return -EINVAL;
8206
8207 err = perf_copy_attr(attr_uptr, &attr);
8208 if (err)
8209 return err;
8210
8211 if (!attr.exclude_kernel) {
8212 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8213 return -EACCES;
8214 }
8215
8216 if (attr.freq) {
8217 if (attr.sample_freq > sysctl_perf_event_sample_rate)
8218 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02008219 } else {
8220 if (attr.sample_period & (1ULL << 63))
8221 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008222 }
8223
Stephane Eraniane5d13672011-02-14 11:20:01 +02008224 /*
8225 * In cgroup mode, the pid argument is used to pass the fd
8226 * opened to the cgroup directory in cgroupfs. The cpu argument
8227 * designates the cpu on which to monitor threads from that
8228 * cgroup.
8229 */
8230 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
8231 return -EINVAL;
8232
Yann Droneauda21b0b32014-01-05 21:36:33 +01008233 if (flags & PERF_FLAG_FD_CLOEXEC)
8234 f_flags |= O_CLOEXEC;
8235
8236 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04008237 if (event_fd < 0)
8238 return event_fd;
8239
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008240 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04008241 err = perf_fget_light(group_fd, &group);
8242 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008243 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04008244 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008245 if (flags & PERF_FLAG_FD_OUTPUT)
8246 output_event = group_leader;
8247 if (flags & PERF_FLAG_FD_NO_GROUP)
8248 group_leader = NULL;
8249 }
8250
Stephane Eraniane5d13672011-02-14 11:20:01 +02008251 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008252 task = find_lively_task_by_vpid(pid);
8253 if (IS_ERR(task)) {
8254 err = PTR_ERR(task);
8255 goto err_group_fd;
8256 }
8257 }
8258
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008259 if (task && group_leader &&
8260 group_leader->attr.inherit != attr.inherit) {
8261 err = -EINVAL;
8262 goto err_task;
8263 }
8264
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008265 get_online_cpus();
8266
Matt Fleming79dff512015-01-23 18:45:42 +00008267 if (flags & PERF_FLAG_PID_CGROUP)
8268 cgroup_fd = pid;
8269
Avi Kivity4dc0da82011-06-29 18:42:35 +03008270 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00008271 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008272 if (IS_ERR(event)) {
8273 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008274 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008275 }
8276
Vince Weaver53b25332014-05-16 17:12:12 -04008277 if (is_sampling_event(event)) {
8278 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
8279 err = -ENOTSUPP;
8280 goto err_alloc;
8281 }
8282 }
8283
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008284 account_event(event);
8285
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008286 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008287 * Special case software events and allow them to be part of
8288 * any hardware group.
8289 */
8290 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008291
Peter Zijlstra34f43922015-02-20 14:05:38 +01008292 if (attr.use_clockid) {
8293 err = perf_event_set_clock(event, attr.clockid);
8294 if (err)
8295 goto err_alloc;
8296 }
8297
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008298 if (group_leader &&
8299 (is_software_event(event) != is_software_event(group_leader))) {
8300 if (is_software_event(event)) {
8301 /*
8302 * If event and group_leader are not both a software
8303 * event, and event is, then group leader is not.
8304 *
8305 * Allow the addition of software events to !software
8306 * groups, this is safe because software events never
8307 * fail to schedule.
8308 */
8309 pmu = group_leader->pmu;
8310 } else if (is_software_event(group_leader) &&
8311 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
8312 /*
8313 * In case the group is a pure software group, and we
8314 * try to add a hardware event, move the whole group to
8315 * the hardware context.
8316 */
8317 move_group = 1;
8318 }
8319 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008320
8321 /*
8322 * Get the target context (task or percpu):
8323 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05008324 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008325 if (IS_ERR(ctx)) {
8326 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008327 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008328 }
8329
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008330 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
8331 err = -EBUSY;
8332 goto err_context;
8333 }
8334
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02008335 if (task) {
8336 put_task_struct(task);
8337 task = NULL;
8338 }
8339
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008340 /*
8341 * Look up the group leader (we will attach this event to it):
8342 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008343 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008344 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008345
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008346 /*
8347 * Do not allow a recursive hierarchy (this new sibling
8348 * becoming part of another group-sibling):
8349 */
8350 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008351 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01008352
8353 /* All events in a group should have the same clock */
8354 if (group_leader->clock != event->clock)
8355 goto err_context;
8356
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008357 /*
8358 * Do not allow to attach to a group in a different
8359 * task or CPU context:
8360 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008361 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01008362 /*
8363 * Make sure we're both on the same task, or both
8364 * per-cpu events.
8365 */
8366 if (group_leader->ctx->task != ctx->task)
8367 goto err_context;
8368
8369 /*
8370 * Make sure we're both events for the same CPU;
8371 * grouping events for different CPUs is broken; since
8372 * you can never concurrently schedule them anyhow.
8373 */
8374 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008375 goto err_context;
8376 } else {
8377 if (group_leader->ctx != ctx)
8378 goto err_context;
8379 }
8380
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008381 /*
8382 * Only a group leader can be exclusive or pinned
8383 */
8384 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008385 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008386 }
8387
8388 if (output_event) {
8389 err = perf_event_set_output(event, output_event);
8390 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008391 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008392 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008393
Yann Droneauda21b0b32014-01-05 21:36:33 +01008394 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
8395 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04008396 if (IS_ERR(event_file)) {
8397 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008398 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04008399 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008400
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008401 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008402 gctx = group_leader->ctx;
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008403 mutex_lock_double(&gctx->mutex, &ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008404 if (gctx->task == TASK_TOMBSTONE) {
8405 err = -ESRCH;
8406 goto err_locked;
8407 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008408 } else {
8409 mutex_lock(&ctx->mutex);
8410 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008411
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008412 if (ctx->task == TASK_TOMBSTONE) {
8413 err = -ESRCH;
8414 goto err_locked;
8415 }
8416
Peter Zijlstraa7239682015-09-09 19:06:33 +02008417 if (!perf_event_validate_size(event)) {
8418 err = -E2BIG;
8419 goto err_locked;
8420 }
8421
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008422 /*
8423 * Must be under the same ctx::mutex as perf_install_in_context(),
8424 * because we need to serialize with concurrent event creation.
8425 */
8426 if (!exclusive_event_installable(event, ctx)) {
8427 /* exclusive and group stuff are assumed mutually exclusive */
8428 WARN_ON_ONCE(move_group);
8429
8430 err = -EBUSY;
8431 goto err_locked;
8432 }
8433
8434 WARN_ON_ONCE(ctx->parent_ctx);
8435
8436 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008437 /*
8438 * See perf_event_ctx_lock() for comments on the details
8439 * of swizzling perf_event::ctx.
8440 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008441 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01008442
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008443 list_for_each_entry(sibling, &group_leader->sibling_list,
8444 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008445 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008446 put_ctx(gctx);
8447 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008448
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008449 /*
8450 * Wait for everybody to stop referencing the events through
8451 * the old lists, before installing it on new lists.
8452 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008453 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008454
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008455 /*
8456 * Install the group siblings before the group leader.
8457 *
8458 * Because a group leader will try and install the entire group
8459 * (through the sibling list, which is still in-tact), we can
8460 * end up with siblings installed in the wrong context.
8461 *
8462 * By installing siblings first we NO-OP because they're not
8463 * reachable through the group lists.
8464 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008465 list_for_each_entry(sibling, &group_leader->sibling_list,
8466 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008467 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01008468 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008469 get_ctx(ctx);
8470 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008471
8472 /*
8473 * Removing from the context ends up with disabled
8474 * event. What we want here is event in the initial
8475 * startup state, ready to be add into new context.
8476 */
8477 perf_event__state_init(group_leader);
8478 perf_install_in_context(ctx, group_leader, group_leader->cpu);
8479 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008480
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008481 /*
8482 * Now that all events are installed in @ctx, nothing
8483 * references @gctx anymore, so drop the last reference we have
8484 * on it.
8485 */
8486 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008487 }
8488
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02008489 /*
8490 * Precalculate sample_data sizes; do while holding ctx::mutex such
8491 * that we're serialized against further additions and before
8492 * perf_install_in_context() which is the point the event is active and
8493 * can use these values.
8494 */
8495 perf_event__header_size(event);
8496 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008497
Peter Zijlstra78cd2c72016-01-25 14:08:45 +01008498 event->owner = current;
8499
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08008500 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008501 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008502
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008503 if (move_group)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008504 mutex_unlock(&gctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008505 mutex_unlock(&ctx->mutex);
8506
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008507 put_online_cpus();
8508
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008509 mutex_lock(&current->perf_event_mutex);
8510 list_add_tail(&event->owner_entry, &current->perf_event_list);
8511 mutex_unlock(&current->perf_event_mutex);
8512
Peter Zijlstra8a495422010-05-27 15:47:49 +02008513 /*
8514 * Drop the reference on the group_event after placing the
8515 * new event on the sibling_list. This ensures destruction
8516 * of the group leader will find the pointer to itself in
8517 * perf_group_detach().
8518 */
Al Viro2903ff02012-08-28 12:52:22 -04008519 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04008520 fd_install(event_fd, event_file);
8521 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008522
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008523err_locked:
8524 if (move_group)
8525 mutex_unlock(&gctx->mutex);
8526 mutex_unlock(&ctx->mutex);
8527/* err_file: */
8528 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008529err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008530 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04008531 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008532err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +01008533 /*
8534 * If event_file is set, the fput() above will have called ->release()
8535 * and that will take care of freeing the event.
8536 */
8537 if (!event_file)
8538 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008539err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008540 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008541err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02008542 if (task)
8543 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008544err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04008545 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04008546err_fd:
8547 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008548 return err;
8549}
8550
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008551/**
8552 * perf_event_create_kernel_counter
8553 *
8554 * @attr: attributes of the counter to create
8555 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07008556 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008557 */
8558struct perf_event *
8559perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07008560 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008561 perf_overflow_handler_t overflow_handler,
8562 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008563{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008564 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008565 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008566 int err;
8567
8568 /*
8569 * Get the target context (task or percpu):
8570 */
8571
Avi Kivity4dc0da82011-06-29 18:42:35 +03008572 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00008573 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008574 if (IS_ERR(event)) {
8575 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008576 goto err;
8577 }
8578
Jiri Olsaf8697762014-08-01 14:33:01 +02008579 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008580 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +02008581
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008582 account_event(event);
8583
Yan, Zheng4af57ef282014-11-04 21:56:01 -05008584 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008585 if (IS_ERR(ctx)) {
8586 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008587 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008588 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008589
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008590 WARN_ON_ONCE(ctx->parent_ctx);
8591 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008592 if (ctx->task == TASK_TOMBSTONE) {
8593 err = -ESRCH;
8594 goto err_unlock;
8595 }
8596
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008597 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008598 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008599 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008600 }
8601
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008602 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008603 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008604 mutex_unlock(&ctx->mutex);
8605
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008606 return event;
8607
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008608err_unlock:
8609 mutex_unlock(&ctx->mutex);
8610 perf_unpin_context(ctx);
8611 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008612err_free:
8613 free_event(event);
8614err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008615 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008616}
8617EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
8618
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008619void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
8620{
8621 struct perf_event_context *src_ctx;
8622 struct perf_event_context *dst_ctx;
8623 struct perf_event *event, *tmp;
8624 LIST_HEAD(events);
8625
8626 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
8627 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
8628
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008629 /*
8630 * See perf_event_ctx_lock() for comments on the details
8631 * of swizzling perf_event::ctx.
8632 */
8633 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008634 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
8635 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008636 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02008637 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008638 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02008639 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008640 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008641
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008642 /*
8643 * Wait for the events to quiesce before re-instating them.
8644 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008645 synchronize_rcu();
8646
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008647 /*
8648 * Re-instate events in 2 passes.
8649 *
8650 * Skip over group leaders and only install siblings on this first
8651 * pass, siblings will not get enabled without a leader, however a
8652 * leader will enable its siblings, even if those are still on the old
8653 * context.
8654 */
8655 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
8656 if (event->group_leader == event)
8657 continue;
8658
8659 list_del(&event->migrate_entry);
8660 if (event->state >= PERF_EVENT_STATE_OFF)
8661 event->state = PERF_EVENT_STATE_INACTIVE;
8662 account_event_cpu(event, dst_cpu);
8663 perf_install_in_context(dst_ctx, event, dst_cpu);
8664 get_ctx(dst_ctx);
8665 }
8666
8667 /*
8668 * Once all the siblings are setup properly, install the group leaders
8669 * to make it go.
8670 */
Peter Zijlstra98861672013-10-03 16:02:23 +02008671 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
8672 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008673 if (event->state >= PERF_EVENT_STATE_OFF)
8674 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02008675 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008676 perf_install_in_context(dst_ctx, event, dst_cpu);
8677 get_ctx(dst_ctx);
8678 }
8679 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008680 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008681}
8682EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
8683
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008684static void sync_child_event(struct perf_event *child_event,
8685 struct task_struct *child)
8686{
8687 struct perf_event *parent_event = child_event->parent;
8688 u64 child_val;
8689
8690 if (child_event->attr.inherit_stat)
8691 perf_event_read_event(child_event, child);
8692
Peter Zijlstrab5e58792010-05-21 14:43:12 +02008693 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008694
8695 /*
8696 * Add back the child's count to the parent's count:
8697 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02008698 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008699 atomic64_add(child_event->total_time_enabled,
8700 &parent_event->child_total_time_enabled);
8701 atomic64_add(child_event->total_time_running,
8702 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008703}
8704
8705static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008706perf_event_exit_event(struct perf_event *child_event,
8707 struct perf_event_context *child_ctx,
8708 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008709{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008710 struct perf_event *parent_event = child_event->parent;
8711
Peter Zijlstra1903d502014-07-15 17:27:27 +02008712 /*
8713 * Do not destroy the 'original' grouping; because of the context
8714 * switch optimization the original events could've ended up in a
8715 * random child task.
8716 *
8717 * If we were to destroy the original group, all group related
8718 * operations would cease to function properly after this random
8719 * child dies.
8720 *
8721 * Do destroy all inherited groups, we don't care about those
8722 * and being thorough is better.
8723 */
Peter Zijlstra32132a32016-01-11 15:40:59 +01008724 raw_spin_lock_irq(&child_ctx->lock);
8725 WARN_ON_ONCE(child_ctx->is_active);
8726
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008727 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +01008728 perf_group_detach(child_event);
8729 list_del_event(child_event, child_ctx);
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02008730 child_event->state = PERF_EVENT_STATE_EXIT; /* see perf_event_release_kernel() */
Peter Zijlstra32132a32016-01-11 15:40:59 +01008731 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008732
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008733 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008734 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008735 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008736 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -04008737 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008738 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008739 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008740 /*
8741 * Child events can be cleaned up.
8742 */
8743
8744 sync_child_event(child_event, child);
8745
8746 /*
8747 * Remove this event from the parent's list
8748 */
8749 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
8750 mutex_lock(&parent_event->child_mutex);
8751 list_del_init(&child_event->child_list);
8752 mutex_unlock(&parent_event->child_mutex);
8753
8754 /*
8755 * Kick perf_poll() for is_event_hup().
8756 */
8757 perf_event_wakeup(parent_event);
8758 free_event(child_event);
8759 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008760}
8761
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008762static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008763{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008764 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008765 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008766
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008767 WARN_ON_ONCE(child != current);
8768
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008769 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008770 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008771 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008772
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008773 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008774 * In order to reduce the amount of tricky in ctx tear-down, we hold
8775 * ctx::mutex over the entire thing. This serializes against almost
8776 * everything that wants to access the ctx.
8777 *
8778 * The exception is sys_perf_event_open() /
8779 * perf_event_create_kernel_count() which does find_get_context()
8780 * without ctx::mutex (it cannot because of the move_group double mutex
8781 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008782 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008783 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008784
8785 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008786 * In a single ctx::lock section, de-schedule the events and detach the
8787 * context from the task such that we cannot ever get it scheduled back
8788 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008789 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008790 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008791 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008792
8793 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008794 * Now that the context is inactive, destroy the task <-> ctx relation
8795 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008796 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008797 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
8798 put_ctx(child_ctx); /* cannot be last */
8799 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
8800 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008801
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008802 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008803 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008804
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008805 if (clone_ctx)
8806 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008807
8808 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008809 * Report the task dead after unscheduling the events so that we
8810 * won't get any samples after PERF_RECORD_EXIT. We can however still
8811 * get a few PERF_RECORD_READ events.
8812 */
8813 perf_event_task(child, child_ctx, 0);
8814
Peter Zijlstraebf905f2014-05-29 19:00:24 +02008815 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008816 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008817
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008818 mutex_unlock(&child_ctx->mutex);
8819
8820 put_ctx(child_ctx);
8821}
8822
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008823/*
8824 * When a child task exits, feed back event values to parent events.
8825 */
8826void perf_event_exit_task(struct task_struct *child)
8827{
Peter Zijlstra88821352010-11-09 19:01:43 +01008828 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008829 int ctxn;
8830
Peter Zijlstra88821352010-11-09 19:01:43 +01008831 mutex_lock(&child->perf_event_mutex);
8832 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
8833 owner_entry) {
8834 list_del_init(&event->owner_entry);
8835
8836 /*
8837 * Ensure the list deletion is visible before we clear
8838 * the owner, closes a race against perf_release() where
8839 * we need to serialize on the owner->perf_event_mutex.
8840 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01008841 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +01008842 }
8843 mutex_unlock(&child->perf_event_mutex);
8844
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008845 for_each_task_context_nr(ctxn)
8846 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +01008847
8848 /*
8849 * The perf_event_exit_task_context calls perf_event_task
8850 * with child's task_ctx, which generates EXIT events for
8851 * child contexts and sets child->perf_event_ctxp[] to NULL.
8852 * At this point we need to send EXIT events to cpu contexts.
8853 */
8854 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008855}
8856
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008857static void perf_free_event(struct perf_event *event,
8858 struct perf_event_context *ctx)
8859{
8860 struct perf_event *parent = event->parent;
8861
8862 if (WARN_ON_ONCE(!parent))
8863 return;
8864
8865 mutex_lock(&parent->child_mutex);
8866 list_del_init(&event->child_list);
8867 mutex_unlock(&parent->child_mutex);
8868
Al Viroa6fa9412012-08-20 14:59:25 +01008869 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008870
Peter Zijlstra652884f2015-01-23 11:20:10 +01008871 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02008872 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008873 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +01008874 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008875 free_event(event);
8876}
8877
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008878/*
Peter Zijlstra652884f2015-01-23 11:20:10 +01008879 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008880 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +01008881 *
8882 * Not all locks are strictly required, but take them anyway to be nice and
8883 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008884 */
8885void perf_event_free_task(struct task_struct *task)
8886{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008887 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008888 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008889 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008890
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008891 for_each_task_context_nr(ctxn) {
8892 ctx = task->perf_event_ctxp[ctxn];
8893 if (!ctx)
8894 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008895
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008896 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008897again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008898 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
8899 group_entry)
8900 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008901
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008902 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
8903 group_entry)
8904 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008905
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008906 if (!list_empty(&ctx->pinned_groups) ||
8907 !list_empty(&ctx->flexible_groups))
8908 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008909
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008910 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008911
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008912 put_ctx(ctx);
8913 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008914}
8915
Peter Zijlstra4e231c72010-09-09 21:01:59 +02008916void perf_event_delayed_put(struct task_struct *task)
8917{
8918 int ctxn;
8919
8920 for_each_task_context_nr(ctxn)
8921 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
8922}
8923
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08008924struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +00008925{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08008926 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +00008927
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08008928 file = fget_raw(fd);
8929 if (!file)
8930 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +00008931
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08008932 if (file->f_op != &perf_fops) {
8933 fput(file);
8934 return ERR_PTR(-EBADF);
8935 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00008936
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08008937 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +00008938}
8939
8940const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
8941{
8942 if (!event)
8943 return ERR_PTR(-EINVAL);
8944
8945 return &event->attr;
8946}
8947
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008948/*
8949 * inherit a event from parent task to child task:
8950 */
8951static struct perf_event *
8952inherit_event(struct perf_event *parent_event,
8953 struct task_struct *parent,
8954 struct perf_event_context *parent_ctx,
8955 struct task_struct *child,
8956 struct perf_event *group_leader,
8957 struct perf_event_context *child_ctx)
8958{
Jiri Olsa1929def2014-09-12 13:18:27 +02008959 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008960 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02008961 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008962
8963 /*
8964 * Instead of creating recursive hierarchies of events,
8965 * we link inherited events back to the original parent,
8966 * which has a filp for sure, which we use as the reference
8967 * count:
8968 */
8969 if (parent_event->parent)
8970 parent_event = parent_event->parent;
8971
8972 child_event = perf_event_alloc(&parent_event->attr,
8973 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008974 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008975 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +00008976 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008977 if (IS_ERR(child_event))
8978 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01008979
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02008980 /*
8981 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
8982 * must be under the same lock in order to serialize against
8983 * perf_event_release_kernel(), such that either we must observe
8984 * is_orphaned_event() or they will observe us on the child_list.
8985 */
8986 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02008987 if (is_orphaned_event(parent_event) ||
8988 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02008989 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +01008990 free_event(child_event);
8991 return NULL;
8992 }
8993
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008994 get_ctx(child_ctx);
8995
8996 /*
8997 * Make the child state follow the state of the parent event,
8998 * not its attr.disabled bit. We hold the parent's mutex,
8999 * so we won't race with perf_event_{en, dis}able_family.
9000 */
Jiri Olsa1929def2014-09-12 13:18:27 +02009001 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009002 child_event->state = PERF_EVENT_STATE_INACTIVE;
9003 else
9004 child_event->state = PERF_EVENT_STATE_OFF;
9005
9006 if (parent_event->attr.freq) {
9007 u64 sample_period = parent_event->hw.sample_period;
9008 struct hw_perf_event *hwc = &child_event->hw;
9009
9010 hwc->sample_period = sample_period;
9011 hwc->last_period = sample_period;
9012
9013 local64_set(&hwc->period_left, sample_period);
9014 }
9015
9016 child_event->ctx = child_ctx;
9017 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03009018 child_event->overflow_handler_context
9019 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009020
9021 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02009022 * Precalculate sample_data sizes
9023 */
9024 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02009025 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02009026
9027 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009028 * Link it up in the child's context:
9029 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02009030 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009031 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02009032 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009033
9034 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009035 * Link this into the parent event's child list
9036 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009037 list_add_tail(&child_event->child_list, &parent_event->child_list);
9038 mutex_unlock(&parent_event->child_mutex);
9039
9040 return child_event;
9041}
9042
9043static int inherit_group(struct perf_event *parent_event,
9044 struct task_struct *parent,
9045 struct perf_event_context *parent_ctx,
9046 struct task_struct *child,
9047 struct perf_event_context *child_ctx)
9048{
9049 struct perf_event *leader;
9050 struct perf_event *sub;
9051 struct perf_event *child_ctr;
9052
9053 leader = inherit_event(parent_event, parent, parent_ctx,
9054 child, NULL, child_ctx);
9055 if (IS_ERR(leader))
9056 return PTR_ERR(leader);
9057 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
9058 child_ctr = inherit_event(sub, parent, parent_ctx,
9059 child, leader, child_ctx);
9060 if (IS_ERR(child_ctr))
9061 return PTR_ERR(child_ctr);
9062 }
9063 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009064}
9065
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009066static int
9067inherit_task_group(struct perf_event *event, struct task_struct *parent,
9068 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009069 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009070 int *inherited_all)
9071{
9072 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009073 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009074
9075 if (!event->attr.inherit) {
9076 *inherited_all = 0;
9077 return 0;
9078 }
9079
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009080 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009081 if (!child_ctx) {
9082 /*
9083 * This is executed from the parent task context, so
9084 * inherit events that have been marked for cloning.
9085 * First allocate and initialize a context for the
9086 * child.
9087 */
9088
Jiri Olsa734df5a2013-07-09 17:44:10 +02009089 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009090 if (!child_ctx)
9091 return -ENOMEM;
9092
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009093 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009094 }
9095
9096 ret = inherit_group(event, parent, parent_ctx,
9097 child, child_ctx);
9098
9099 if (ret)
9100 *inherited_all = 0;
9101
9102 return ret;
9103}
9104
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009105/*
9106 * Initialize the perf_event context in task_struct
9107 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02009108static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009109{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009110 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009111 struct perf_event_context *cloned_ctx;
9112 struct perf_event *event;
9113 struct task_struct *parent = current;
9114 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009115 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009116 int ret = 0;
9117
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009118 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009119 return 0;
9120
9121 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009122 * If the parent's context is a clone, pin it so it won't get
9123 * swapped under us.
9124 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009125 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02009126 if (!parent_ctx)
9127 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009128
9129 /*
9130 * No need to check if parent_ctx != NULL here; since we saw
9131 * it non-NULL earlier, the only reason for it to become NULL
9132 * is if we exit, and since we're currently in the middle of
9133 * a fork we can't be exiting at the same time.
9134 */
9135
9136 /*
9137 * Lock the parent list. No need to lock the child - not PID
9138 * hashed yet and not running, so nobody can access it.
9139 */
9140 mutex_lock(&parent_ctx->mutex);
9141
9142 /*
9143 * We dont have to disable NMIs - we are only looking at
9144 * the list, not manipulating it:
9145 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009146 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009147 ret = inherit_task_group(event, parent, parent_ctx,
9148 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009149 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009150 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009151 }
9152
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009153 /*
9154 * We can't hold ctx->lock when iterating the ->flexible_group list due
9155 * to allocations, but we need to prevent rotation because
9156 * rotate_ctx() will change the list from interrupt context.
9157 */
9158 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
9159 parent_ctx->rotate_disable = 1;
9160 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
9161
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009162 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009163 ret = inherit_task_group(event, parent, parent_ctx,
9164 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009165 if (ret)
9166 break;
9167 }
9168
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009169 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
9170 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009171
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009172 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009173
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01009174 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009175 /*
9176 * Mark the child context as a clone of the parent
9177 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009178 *
9179 * Note that if the parent is a clone, the holding of
9180 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009181 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009182 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009183 if (cloned_ctx) {
9184 child_ctx->parent_ctx = cloned_ctx;
9185 child_ctx->parent_gen = parent_ctx->parent_gen;
9186 } else {
9187 child_ctx->parent_ctx = parent_ctx;
9188 child_ctx->parent_gen = parent_ctx->generation;
9189 }
9190 get_ctx(child_ctx->parent_ctx);
9191 }
9192
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009193 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009194 mutex_unlock(&parent_ctx->mutex);
9195
9196 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009197 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009198
9199 return ret;
9200}
9201
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009202/*
9203 * Initialize the perf_event context in task_struct
9204 */
9205int perf_event_init_task(struct task_struct *child)
9206{
9207 int ctxn, ret;
9208
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01009209 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
9210 mutex_init(&child->perf_event_mutex);
9211 INIT_LIST_HEAD(&child->perf_event_list);
9212
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009213 for_each_task_context_nr(ctxn) {
9214 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07009215 if (ret) {
9216 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009217 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07009218 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009219 }
9220
9221 return 0;
9222}
9223
Paul Mackerras220b1402010-03-10 20:45:52 +11009224static void __init perf_event_init_all_cpus(void)
9225{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009226 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11009227 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11009228
9229 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009230 swhash = &per_cpu(swevent_htable, cpu);
9231 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00009232 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11009233 }
9234}
9235
Paul Gortmaker0db06282013-06-19 14:53:51 -04009236static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009237{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009238 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009239
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009240 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +00009241 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009242 struct swevent_hlist *hlist;
9243
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009244 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
9245 WARN_ON(!hlist);
9246 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009247 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009248 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009249}
9250
Dave Young2965faa2015-09-09 15:38:55 -07009251#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009252static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009253{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009254 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009255 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
9256 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009257
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009258 raw_spin_lock(&ctx->lock);
9259 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009260 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009261 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009262}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009263
9264static void perf_event_exit_cpu_context(int cpu)
9265{
9266 struct perf_event_context *ctx;
9267 struct pmu *pmu;
9268 int idx;
9269
9270 idx = srcu_read_lock(&pmus_srcu);
9271 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02009272 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009273
9274 mutex_lock(&ctx->mutex);
9275 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
9276 mutex_unlock(&ctx->mutex);
9277 }
9278 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009279}
9280
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009281static void perf_event_exit_cpu(int cpu)
9282{
Peter Zijlstrae3703f82014-02-24 12:06:12 +01009283 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009284}
9285#else
9286static inline void perf_event_exit_cpu(int cpu) { }
9287#endif
9288
Peter Zijlstrac2774432010-12-08 15:29:02 +01009289static int
9290perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
9291{
9292 int cpu;
9293
9294 for_each_online_cpu(cpu)
9295 perf_event_exit_cpu(cpu);
9296
9297 return NOTIFY_OK;
9298}
9299
9300/*
9301 * Run the perf reboot notifier at the very last possible moment so that
9302 * the generic watchdog code runs as long as possible.
9303 */
9304static struct notifier_block perf_reboot_notifier = {
9305 .notifier_call = perf_reboot,
9306 .priority = INT_MIN,
9307};
9308
Paul Gortmaker0db06282013-06-19 14:53:51 -04009309static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009310perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
9311{
9312 unsigned int cpu = (long)hcpu;
9313
Linus Torvalds4536e4d2011-11-03 07:44:04 -07009314 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009315
9316 case CPU_UP_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009317 perf_event_init_cpu(cpu);
9318 break;
9319
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009320 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009321 perf_event_exit_cpu(cpu);
9322 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009323 default:
9324 break;
9325 }
9326
9327 return NOTIFY_OK;
9328}
9329
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009330void __init perf_event_init(void)
9331{
Jason Wessel3c502e72010-11-04 17:33:01 -05009332 int ret;
9333
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009334 idr_init(&pmu_idr);
9335
Paul Mackerras220b1402010-03-10 20:45:52 +11009336 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009337 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009338 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
9339 perf_pmu_register(&perf_cpu_clock, NULL, -1);
9340 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009341 perf_tp_register();
9342 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01009343 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05009344
9345 ret = init_hw_breakpoint();
9346 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02009347
9348 /* do not patch jump label more than once per second */
9349 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01009350
9351 /*
9352 * Build time assertion that we keep the data_head at the intended
9353 * location. IOW, validation we got the __reserved[] size right.
9354 */
9355 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
9356 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009357}
Peter Zijlstraabe43402010-11-17 23:17:37 +01009358
Cody P Schaferfd979c02015-01-30 13:45:57 -08009359ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
9360 char *page)
9361{
9362 struct perf_pmu_events_attr *pmu_attr =
9363 container_of(attr, struct perf_pmu_events_attr, attr);
9364
9365 if (pmu_attr->event_str)
9366 return sprintf(page, "%s\n", pmu_attr->event_str);
9367
9368 return 0;
9369}
9370
Peter Zijlstraabe43402010-11-17 23:17:37 +01009371static int __init perf_event_sysfs_init(void)
9372{
9373 struct pmu *pmu;
9374 int ret;
9375
9376 mutex_lock(&pmus_lock);
9377
9378 ret = bus_register(&pmu_bus);
9379 if (ret)
9380 goto unlock;
9381
9382 list_for_each_entry(pmu, &pmus, entry) {
9383 if (!pmu->name || pmu->type < 0)
9384 continue;
9385
9386 ret = pmu_dev_alloc(pmu);
9387 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
9388 }
9389 pmu_bus_running = 1;
9390 ret = 0;
9391
9392unlock:
9393 mutex_unlock(&pmus_lock);
9394
9395 return ret;
9396}
9397device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009398
9399#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04009400static struct cgroup_subsys_state *
9401perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009402{
9403 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02009404
Li Zefan1b15d052011-03-03 14:26:06 +08009405 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009406 if (!jc)
9407 return ERR_PTR(-ENOMEM);
9408
Stephane Eraniane5d13672011-02-14 11:20:01 +02009409 jc->info = alloc_percpu(struct perf_cgroup_info);
9410 if (!jc->info) {
9411 kfree(jc);
9412 return ERR_PTR(-ENOMEM);
9413 }
9414
Stephane Eraniane5d13672011-02-14 11:20:01 +02009415 return &jc->css;
9416}
9417
Tejun Heoeb954192013-08-08 20:11:23 -04009418static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009419{
Tejun Heoeb954192013-08-08 20:11:23 -04009420 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
9421
Stephane Eraniane5d13672011-02-14 11:20:01 +02009422 free_percpu(jc->info);
9423 kfree(jc);
9424}
9425
9426static int __perf_cgroup_move(void *info)
9427{
9428 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +01009429 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +02009430 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +01009431 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +02009432 return 0;
9433}
9434
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009435static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009436{
Tejun Heobb9d97b2011-12-12 18:12:21 -08009437 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009438 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -08009439
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009440 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08009441 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009442}
9443
Tejun Heo073219e2014-02-08 10:36:58 -05009444struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08009445 .css_alloc = perf_cgroup_css_alloc,
9446 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -08009447 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02009448};
9449#endif /* CONFIG_CGROUP_PERF */