blob: 4291a4d27664224edf52baa041ac6121dfe8d12b [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
Ingo Molnare7e7ee22011-05-04 08:42:29 +02005 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
Peter Zijlstra90eec102015-11-16 11:08:45 +01006 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra
Al Virod36b6912011-12-29 17:09:01 -05007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008 *
Ingo Molnar57c0c152009-09-21 12:20:38 +02009 * For licensing details see kernel-base/COPYING
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010 */
11
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include <linux/cpu.h>
15#include <linux/smp.h>
Peter Zijlstra2e80a822010-11-17 23:17:36 +010016#include <linux/idr.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/file.h>
18#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020020#include <linux/hash.h>
Frederic Weisbecker12351ef2013-04-20 15:48:22 +020021#include <linux/tick.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020022#include <linux/sysfs.h>
23#include <linux/dcache.h>
24#include <linux/percpu.h>
25#include <linux/ptrace.h>
Peter Zijlstrac2774432010-12-08 15:29:02 +010026#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020027#include <linux/vmstat.h>
Peter Zijlstraabe43402010-11-17 23:17:37 +010028#include <linux/device.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040029#include <linux/export.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020030#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020031#include <linux/hardirq.h>
32#include <linux/rculist.h>
33#include <linux/uaccess.h>
34#include <linux/syscalls.h>
35#include <linux/anon_inodes.h>
36#include <linux/kernel_stat.h>
Matt Fleming39bed6c2015-01-23 18:45:40 +000037#include <linux/cgroup.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020038#include <linux/perf_event.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040039#include <linux/trace_events.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050040#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020041#include <linux/mm_types.h>
Yan, Zhengc464c762014-03-18 16:56:41 +080042#include <linux/module.h>
Peter Zijlstraf972eb62014-05-19 15:13:47 -040043#include <linux/mman.h>
Pawel Mollb3f20782014-06-13 16:03:32 +010044#include <linux/compat.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070045#include <linux/bpf.h>
46#include <linux/filter.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020047
Frederic Weisbecker76369132011-05-19 19:55:04 +020048#include "internal.h"
49
Ingo Molnarcdd6c482009-09-21 12:02:48 +020050#include <asm/irq_regs.h>
51
Jiri Olsafadfe7b2014-08-01 14:33:02 +020052static struct workqueue_struct *perf_wq;
53
Peter Zijlstra272325c2015-04-15 11:41:58 +020054typedef int (*remote_function_f)(void *);
55
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010056struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020057 struct task_struct *p;
Peter Zijlstra272325c2015-04-15 11:41:58 +020058 remote_function_f func;
Ingo Molnare7e7ee22011-05-04 08:42:29 +020059 void *info;
60 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010061};
62
63static void remote_function(void *data)
64{
65 struct remote_function_call *tfc = data;
66 struct task_struct *p = tfc->p;
67
68 if (p) {
69 tfc->ret = -EAGAIN;
70 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
71 return;
72 }
73
74 tfc->ret = tfc->func(tfc->info);
75}
76
77/**
78 * task_function_call - call a function on the cpu on which a task runs
79 * @p: the task to evaluate
80 * @func: the function to be called
81 * @info: the function call argument
82 *
83 * Calls the function @func when the task is currently running. This might
84 * be on the current CPU, which just calls the function directly
85 *
86 * returns: @func return value, or
87 * -ESRCH - when the process isn't running
88 * -EAGAIN - when the process moved away
89 */
90static int
Peter Zijlstra272325c2015-04-15 11:41:58 +020091task_function_call(struct task_struct *p, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010092{
93 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020094 .p = p,
95 .func = func,
96 .info = info,
97 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010098 };
99
100 if (task_curr(p))
101 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
102
103 return data.ret;
104}
105
106/**
107 * cpu_function_call - call a function on the cpu
108 * @func: the function to be called
109 * @info: the function call argument
110 *
111 * Calls the function @func on the remote cpu.
112 *
113 * returns: @func return value or -ENXIO when the cpu is offline
114 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200115static int cpu_function_call(int cpu, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100116{
117 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200118 .p = NULL,
119 .func = func,
120 .info = info,
121 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100122 };
123
124 smp_call_function_single(cpu, remote_function, &data, 1);
125
126 return data.ret;
127}
128
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100129static inline struct perf_cpu_context *
130__get_cpu_context(struct perf_event_context *ctx)
131{
132 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
133}
134
135static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
136 struct perf_event_context *ctx)
137{
138 raw_spin_lock(&cpuctx->ctx.lock);
139 if (ctx)
140 raw_spin_lock(&ctx->lock);
141}
142
143static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
144 struct perf_event_context *ctx)
145{
146 if (ctx)
147 raw_spin_unlock(&ctx->lock);
148 raw_spin_unlock(&cpuctx->ctx.lock);
149}
150
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100151#define TASK_TOMBSTONE ((void *)-1L)
152
153static bool is_kernel_event(struct perf_event *event)
154{
Peter Zijlstraf47c02c2016-01-26 12:30:14 +0100155 return READ_ONCE(event->owner) == TASK_TOMBSTONE;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100156}
157
Peter Zijlstra39a43642016-01-11 12:46:35 +0100158/*
159 * On task ctx scheduling...
160 *
161 * When !ctx->nr_events a task context will not be scheduled. This means
162 * we can disable the scheduler hooks (for performance) without leaving
163 * pending task ctx state.
164 *
165 * This however results in two special cases:
166 *
167 * - removing the last event from a task ctx; this is relatively straight
168 * forward and is done in __perf_remove_from_context.
169 *
170 * - adding the first event to a task ctx; this is tricky because we cannot
171 * rely on ctx->is_active and therefore cannot use event_function_call().
172 * See perf_install_in_context().
173 *
174 * This is because we need a ctx->lock serialized variable (ctx->is_active)
175 * to reliably determine if a particular task/context is scheduled in. The
176 * task_curr() use in task_function_call() is racy in that a remote context
177 * switch is not a single atomic operation.
178 *
179 * As is, the situation is 'safe' because we set rq->curr before we do the
180 * actual context switch. This means that task_curr() will fail early, but
181 * we'll continue spinning on ctx->is_active until we've passed
182 * perf_event_task_sched_out().
183 *
184 * Without this ctx->lock serialized variable we could have race where we find
185 * the task (and hence the context) would not be active while in fact they are.
186 *
187 * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
188 */
189
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100190typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *,
191 struct perf_event_context *, void *);
192
193struct event_function_struct {
194 struct perf_event *event;
195 event_f func;
196 void *data;
197};
198
199static int event_function(void *info)
200{
201 struct event_function_struct *efs = info;
202 struct perf_event *event = efs->event;
203 struct perf_event_context *ctx = event->ctx;
204 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
205 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100206 int ret = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100207
208 WARN_ON_ONCE(!irqs_disabled());
209
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100210 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100211 /*
212 * Since we do the IPI call without holding ctx->lock things can have
213 * changed, double check we hit the task we set out to hit.
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100214 */
215 if (ctx->task) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100216 if (ctx->task != current) {
217 ret = -EAGAIN;
218 goto unlock;
219 }
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100220
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100221 /*
222 * We only use event_function_call() on established contexts,
223 * and event_function() is only ever called when active (or
224 * rather, we'll have bailed in task_function_call() or the
225 * above ctx->task != current test), therefore we must have
226 * ctx->is_active here.
227 */
228 WARN_ON_ONCE(!ctx->is_active);
229 /*
230 * And since we have ctx->is_active, cpuctx->task_ctx must
231 * match.
232 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100233 WARN_ON_ONCE(task_ctx != ctx);
234 } else {
235 WARN_ON_ONCE(&cpuctx->ctx != ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100236 }
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100237
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100238 efs->func(event, cpuctx, ctx, efs->data);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100239unlock:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100240 perf_ctx_unlock(cpuctx, task_ctx);
241
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100242 return ret;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100243}
244
245static void event_function_local(struct perf_event *event, event_f func, void *data)
246{
247 struct event_function_struct efs = {
248 .event = event,
249 .func = func,
250 .data = data,
251 };
252
253 int ret = event_function(&efs);
254 WARN_ON_ONCE(ret);
255}
256
257static void event_function_call(struct perf_event *event, event_f func, void *data)
Peter Zijlstra00179602015-11-30 16:26:35 +0100258{
259 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100260 struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100261 struct event_function_struct efs = {
262 .event = event,
263 .func = func,
264 .data = data,
265 };
Peter Zijlstra00179602015-11-30 16:26:35 +0100266
Peter Zijlstrac97f4732016-01-14 10:51:03 +0100267 if (!event->parent) {
268 /*
269 * If this is a !child event, we must hold ctx::mutex to
270 * stabilize the the event->ctx relation. See
271 * perf_event_ctx_lock().
272 */
273 lockdep_assert_held(&ctx->mutex);
274 }
275
Peter Zijlstra00179602015-11-30 16:26:35 +0100276 if (!task) {
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100277 cpu_function_call(event->cpu, event_function, &efs);
Peter Zijlstra00179602015-11-30 16:26:35 +0100278 return;
279 }
280
281again:
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100282 if (task == TASK_TOMBSTONE)
283 return;
284
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100285 if (!task_function_call(task, event_function, &efs))
Peter Zijlstra00179602015-11-30 16:26:35 +0100286 return;
287
288 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100289 /*
290 * Reload the task pointer, it might have been changed by
291 * a concurrent perf_event_context_sched_out().
292 */
293 task = ctx->task;
294 if (task != TASK_TOMBSTONE) {
295 if (ctx->is_active) {
296 raw_spin_unlock_irq(&ctx->lock);
297 goto again;
298 }
299 func(event, NULL, ctx, data);
Peter Zijlstra00179602015-11-30 16:26:35 +0100300 }
Peter Zijlstra00179602015-11-30 16:26:35 +0100301 raw_spin_unlock_irq(&ctx->lock);
302}
303
Stephane Eraniane5d13672011-02-14 11:20:01 +0200304#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
305 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100306 PERF_FLAG_PID_CGROUP |\
307 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200308
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100309/*
310 * branch priv levels that need permission checks
311 */
312#define PERF_SAMPLE_BRANCH_PERM_PLM \
313 (PERF_SAMPLE_BRANCH_KERNEL |\
314 PERF_SAMPLE_BRANCH_HV)
315
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200316enum event_type_t {
317 EVENT_FLEXIBLE = 0x1,
318 EVENT_PINNED = 0x2,
319 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
320};
321
Stephane Eraniane5d13672011-02-14 11:20:01 +0200322/*
323 * perf_sched_events : >0 events exist
324 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
325 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100326struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200327static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Yan, Zhengba532502014-11-04 21:55:58 -0500328static DEFINE_PER_CPU(int, perf_sched_cb_usages);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200329
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200330static atomic_t nr_mmap_events __read_mostly;
331static atomic_t nr_comm_events __read_mostly;
332static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200333static atomic_t nr_freq_events __read_mostly;
Adrian Hunter45ac1402015-07-21 12:44:02 +0300334static atomic_t nr_switch_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200335
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200336static LIST_HEAD(pmus);
337static DEFINE_MUTEX(pmus_lock);
338static struct srcu_struct pmus_srcu;
339
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200340/*
341 * perf event paranoia level:
342 * -1 - not paranoid at all
343 * 0 - disallow raw tracepoint access for unpriv
344 * 1 - disallow cpu events for unpriv
345 * 2 - disallow kernel profiling for unpriv
346 */
347int sysctl_perf_event_paranoid __read_mostly = 1;
348
Frederic Weisbecker20443382011-03-31 03:33:29 +0200349/* Minimum for 512 kiB + 1 user control page */
350int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200351
352/*
353 * max perf event sample rate
354 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700355#define DEFAULT_MAX_SAMPLE_RATE 100000
356#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
357#define DEFAULT_CPU_TIME_MAX_PERCENT 25
358
359int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
360
361static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
362static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
363
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200364static int perf_sample_allowed_ns __read_mostly =
365 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700366
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800367static void update_perf_cpu_limits(void)
Dave Hansen14c63f12013-06-21 08:51:36 -0700368{
369 u64 tmp = perf_sample_period_ns;
370
371 tmp *= sysctl_perf_cpu_time_max_percent;
Stephane Eraniane5302922013-07-05 00:30:11 +0200372 do_div(tmp, 100);
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200373 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
Dave Hansen14c63f12013-06-21 08:51:36 -0700374}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100375
Stephane Eranian9e630202013-04-03 14:21:33 +0200376static int perf_rotate_context(struct perf_cpu_context *cpuctx);
377
Peter Zijlstra163ec432011-02-16 11:22:34 +0100378int perf_proc_update_handler(struct ctl_table *table, int write,
379 void __user *buffer, size_t *lenp,
380 loff_t *ppos)
381{
Knut Petersen723478c2013-09-25 14:29:37 +0200382 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100383
384 if (ret || !write)
385 return ret;
386
387 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700388 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
389 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100390
391 return 0;
392}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200393
Dave Hansen14c63f12013-06-21 08:51:36 -0700394int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
395
396int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
397 void __user *buffer, size_t *lenp,
398 loff_t *ppos)
399{
400 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
401
402 if (ret || !write)
403 return ret;
404
405 update_perf_cpu_limits();
406
407 return 0;
408}
409
410/*
411 * perf samples are done in some very critical code paths (NMIs).
412 * If they take too much CPU time, the system can lock up and not
413 * get any real work done. This will drop the sample rate when
414 * we detect that events are taking too long.
415 */
416#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200417static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700418
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100419static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700420{
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100421 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Dave Hansen14c63f12013-06-21 08:51:36 -0700422 u64 avg_local_sample_len;
Stephane Eraniane5302922013-07-05 00:30:11 +0200423 u64 local_samples_len;
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100424
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500425 local_samples_len = __this_cpu_read(running_sample_length);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100426 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
427
428 printk_ratelimited(KERN_WARNING
429 "perf interrupt took too long (%lld > %lld), lowering "
430 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100431 avg_local_sample_len, allowed_ns >> 1,
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100432 sysctl_perf_event_sample_rate);
433}
434
435static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
436
437void perf_sample_event_took(u64 sample_len_ns)
438{
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200439 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100440 u64 avg_local_sample_len;
441 u64 local_samples_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700442
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200443 if (allowed_ns == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700444 return;
445
446 /* decay the counter by 1 average sample */
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500447 local_samples_len = __this_cpu_read(running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700448 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
449 local_samples_len += sample_len_ns;
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500450 __this_cpu_write(running_sample_length, local_samples_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700451
452 /*
453 * note: this will be biased artifically low until we have
454 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
455 * from having to maintain a count.
456 */
457 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
458
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200459 if (avg_local_sample_len <= allowed_ns)
Dave Hansen14c63f12013-06-21 08:51:36 -0700460 return;
461
462 if (max_samples_per_tick <= 1)
463 return;
464
465 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
466 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
467 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
468
Dave Hansen14c63f12013-06-21 08:51:36 -0700469 update_perf_cpu_limits();
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100470
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100471 if (!irq_work_queue(&perf_duration_work)) {
472 early_printk("perf interrupt took too long (%lld > %lld), lowering "
473 "kernel.perf_event_max_sample_rate to %d\n",
474 avg_local_sample_len, allowed_ns >> 1,
475 sysctl_perf_event_sample_rate);
476 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700477}
478
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200479static atomic64_t perf_event_id;
480
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200481static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
482 enum event_type_t event_type);
483
484static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200485 enum event_type_t event_type,
486 struct task_struct *task);
487
488static void update_context_time(struct perf_event_context *ctx);
489static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200490
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200491void __weak perf_event_print_debug(void) { }
492
Matt Fleming84c79912010-10-03 21:41:13 +0100493extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200494{
Matt Fleming84c79912010-10-03 21:41:13 +0100495 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200496}
497
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200498static inline u64 perf_clock(void)
499{
500 return local_clock();
501}
502
Peter Zijlstra34f43922015-02-20 14:05:38 +0100503static inline u64 perf_event_clock(struct perf_event *event)
504{
505 return event->clock();
506}
507
Stephane Eraniane5d13672011-02-14 11:20:01 +0200508#ifdef CONFIG_CGROUP_PERF
509
Stephane Eraniane5d13672011-02-14 11:20:01 +0200510static inline bool
511perf_cgroup_match(struct perf_event *event)
512{
513 struct perf_event_context *ctx = event->ctx;
514 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
515
Tejun Heoef824fa2013-04-08 19:00:38 -0700516 /* @event doesn't care about cgroup */
517 if (!event->cgrp)
518 return true;
519
520 /* wants specific cgroup scope but @cpuctx isn't associated with any */
521 if (!cpuctx->cgrp)
522 return false;
523
524 /*
525 * Cgroup scoping is recursive. An event enabled for a cgroup is
526 * also enabled for all its descendant cgroups. If @cpuctx's
527 * cgroup is a descendant of @event's (the test covers identity
528 * case), it's a match.
529 */
530 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
531 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200532}
533
Stephane Eraniane5d13672011-02-14 11:20:01 +0200534static inline void perf_detach_cgroup(struct perf_event *event)
535{
Zefan Li4e2ba652014-09-19 16:53:14 +0800536 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200537 event->cgrp = NULL;
538}
539
540static inline int is_cgroup_event(struct perf_event *event)
541{
542 return event->cgrp != NULL;
543}
544
545static inline u64 perf_cgroup_event_time(struct perf_event *event)
546{
547 struct perf_cgroup_info *t;
548
549 t = per_cpu_ptr(event->cgrp->info, event->cpu);
550 return t->time;
551}
552
553static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
554{
555 struct perf_cgroup_info *info;
556 u64 now;
557
558 now = perf_clock();
559
560 info = this_cpu_ptr(cgrp->info);
561
562 info->time += now - info->timestamp;
563 info->timestamp = now;
564}
565
566static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
567{
568 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
569 if (cgrp_out)
570 __update_cgrp_time(cgrp_out);
571}
572
573static inline void update_cgrp_time_from_event(struct perf_event *event)
574{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200575 struct perf_cgroup *cgrp;
576
Stephane Eraniane5d13672011-02-14 11:20:01 +0200577 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200578 * ensure we access cgroup data only when needed and
579 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200580 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200581 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200582 return;
583
Stephane Eranian614e4c42015-11-12 11:00:04 +0100584 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200585 /*
586 * Do not update time when cgroup is not active
587 */
588 if (cgrp == event->cgrp)
589 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200590}
591
592static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200593perf_cgroup_set_timestamp(struct task_struct *task,
594 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200595{
596 struct perf_cgroup *cgrp;
597 struct perf_cgroup_info *info;
598
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200599 /*
600 * ctx->lock held by caller
601 * ensure we do not access cgroup data
602 * unless we have the cgroup pinned (css_get)
603 */
604 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200605 return;
606
Stephane Eranian614e4c42015-11-12 11:00:04 +0100607 cgrp = perf_cgroup_from_task(task, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200608 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200609 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200610}
611
612#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
613#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
614
615/*
616 * reschedule events based on the cgroup constraint of task.
617 *
618 * mode SWOUT : schedule out everything
619 * mode SWIN : schedule in based on cgroup for next
620 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800621static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200622{
623 struct perf_cpu_context *cpuctx;
624 struct pmu *pmu;
625 unsigned long flags;
626
627 /*
628 * disable interrupts to avoid geting nr_cgroup
629 * changes via __perf_event_disable(). Also
630 * avoids preemption.
631 */
632 local_irq_save(flags);
633
634 /*
635 * we reschedule only in the presence of cgroup
636 * constrained events.
637 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200638
639 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200640 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200641 if (cpuctx->unique_pmu != pmu)
642 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200643
Stephane Eraniane5d13672011-02-14 11:20:01 +0200644 /*
645 * perf_cgroup_events says at least one
646 * context on this CPU has cgroup events.
647 *
648 * ctx->nr_cgroups reports the number of cgroup
649 * events for a context.
650 */
651 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200652 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
653 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200654
655 if (mode & PERF_CGROUP_SWOUT) {
656 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
657 /*
658 * must not be done before ctxswout due
659 * to event_filter_match() in event_sched_out()
660 */
661 cpuctx->cgrp = NULL;
662 }
663
664 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200665 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200666 /*
667 * set cgrp before ctxsw in to allow
668 * event_filter_match() to not have to pass
669 * task around
Stephane Eranian614e4c42015-11-12 11:00:04 +0100670 * we pass the cpuctx->ctx to perf_cgroup_from_task()
671 * because cgorup events are only per-cpu
Stephane Eraniane5d13672011-02-14 11:20:01 +0200672 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100673 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200674 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
675 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200676 perf_pmu_enable(cpuctx->ctx.pmu);
677 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200678 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200679 }
680
Stephane Eraniane5d13672011-02-14 11:20:01 +0200681 local_irq_restore(flags);
682}
683
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200684static inline void perf_cgroup_sched_out(struct task_struct *task,
685 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200686{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200687 struct perf_cgroup *cgrp1;
688 struct perf_cgroup *cgrp2 = NULL;
689
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100690 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200691 /*
692 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100693 * we do not need to pass the ctx here because we know
694 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200695 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100696 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100697 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200698
699 /*
700 * only schedule out current cgroup events if we know
701 * that we are switching to a different cgroup. Otherwise,
702 * do no touch the cgroup events.
703 */
704 if (cgrp1 != cgrp2)
705 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100706
707 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200708}
709
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200710static inline void perf_cgroup_sched_in(struct task_struct *prev,
711 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200712{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200713 struct perf_cgroup *cgrp1;
714 struct perf_cgroup *cgrp2 = NULL;
715
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100716 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200717 /*
718 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100719 * we do not need to pass the ctx here because we know
720 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200721 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100722 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100723 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200724
725 /*
726 * only need to schedule in cgroup events if we are changing
727 * cgroup during ctxsw. Cgroup events were not scheduled
728 * out of ctxsw out if that was not the case.
729 */
730 if (cgrp1 != cgrp2)
731 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100732
733 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200734}
735
736static inline int perf_cgroup_connect(int fd, struct perf_event *event,
737 struct perf_event_attr *attr,
738 struct perf_event *group_leader)
739{
740 struct perf_cgroup *cgrp;
741 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400742 struct fd f = fdget(fd);
743 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200744
Al Viro2903ff02012-08-28 12:52:22 -0400745 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200746 return -EBADF;
747
Al Virob5830432014-10-31 01:22:04 -0400748 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400749 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800750 if (IS_ERR(css)) {
751 ret = PTR_ERR(css);
752 goto out;
753 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200754
755 cgrp = container_of(css, struct perf_cgroup, css);
756 event->cgrp = cgrp;
757
758 /*
759 * all events in a group must monitor
760 * the same cgroup because a task belongs
761 * to only one perf cgroup at a time
762 */
763 if (group_leader && group_leader->cgrp != cgrp) {
764 perf_detach_cgroup(event);
765 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200766 }
Li Zefan3db272c2011-03-03 14:25:37 +0800767out:
Al Viro2903ff02012-08-28 12:52:22 -0400768 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200769 return ret;
770}
771
772static inline void
773perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
774{
775 struct perf_cgroup_info *t;
776 t = per_cpu_ptr(event->cgrp->info, event->cpu);
777 event->shadow_ctx_time = now - t->timestamp;
778}
779
780static inline void
781perf_cgroup_defer_enabled(struct perf_event *event)
782{
783 /*
784 * when the current task's perf cgroup does not match
785 * the event's, we need to remember to call the
786 * perf_mark_enable() function the first time a task with
787 * a matching perf cgroup is scheduled in.
788 */
789 if (is_cgroup_event(event) && !perf_cgroup_match(event))
790 event->cgrp_defer_enabled = 1;
791}
792
793static inline void
794perf_cgroup_mark_enabled(struct perf_event *event,
795 struct perf_event_context *ctx)
796{
797 struct perf_event *sub;
798 u64 tstamp = perf_event_time(event);
799
800 if (!event->cgrp_defer_enabled)
801 return;
802
803 event->cgrp_defer_enabled = 0;
804
805 event->tstamp_enabled = tstamp - event->total_time_enabled;
806 list_for_each_entry(sub, &event->sibling_list, group_entry) {
807 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
808 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
809 sub->cgrp_defer_enabled = 0;
810 }
811 }
812}
813#else /* !CONFIG_CGROUP_PERF */
814
815static inline bool
816perf_cgroup_match(struct perf_event *event)
817{
818 return true;
819}
820
821static inline void perf_detach_cgroup(struct perf_event *event)
822{}
823
824static inline int is_cgroup_event(struct perf_event *event)
825{
826 return 0;
827}
828
829static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
830{
831 return 0;
832}
833
834static inline void update_cgrp_time_from_event(struct perf_event *event)
835{
836}
837
838static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
839{
840}
841
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200842static inline void perf_cgroup_sched_out(struct task_struct *task,
843 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200844{
845}
846
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200847static inline void perf_cgroup_sched_in(struct task_struct *prev,
848 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200849{
850}
851
852static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
853 struct perf_event_attr *attr,
854 struct perf_event *group_leader)
855{
856 return -EINVAL;
857}
858
859static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200860perf_cgroup_set_timestamp(struct task_struct *task,
861 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200862{
863}
864
865void
866perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
867{
868}
869
870static inline void
871perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
872{
873}
874
875static inline u64 perf_cgroup_event_time(struct perf_event *event)
876{
877 return 0;
878}
879
880static inline void
881perf_cgroup_defer_enabled(struct perf_event *event)
882{
883}
884
885static inline void
886perf_cgroup_mark_enabled(struct perf_event *event,
887 struct perf_event_context *ctx)
888{
889}
890#endif
891
Stephane Eranian9e630202013-04-03 14:21:33 +0200892/*
893 * set default to be dependent on timer tick just
894 * like original code
895 */
896#define PERF_CPU_HRTIMER (1000 / HZ)
897/*
898 * function must be called with interrupts disbled
899 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200900static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +0200901{
902 struct perf_cpu_context *cpuctx;
Stephane Eranian9e630202013-04-03 14:21:33 +0200903 int rotations = 0;
904
905 WARN_ON(!irqs_disabled());
906
907 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200908 rotations = perf_rotate_context(cpuctx);
909
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200910 raw_spin_lock(&cpuctx->hrtimer_lock);
911 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +0200912 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200913 else
914 cpuctx->hrtimer_active = 0;
915 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +0200916
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200917 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +0200918}
919
Peter Zijlstra272325c2015-04-15 11:41:58 +0200920static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +0200921{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200922 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200923 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +0200924 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +0200925
926 /* no multiplexing needed for SW PMU */
927 if (pmu->task_ctx_nr == perf_sw_context)
928 return;
929
Stephane Eranian62b85632013-04-03 14:21:34 +0200930 /*
931 * check default is sane, if not set then force to
932 * default interval (1/tick)
933 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200934 interval = pmu->hrtimer_interval_ms;
935 if (interval < 1)
936 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +0200937
Peter Zijlstra272325c2015-04-15 11:41:58 +0200938 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +0200939
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200940 raw_spin_lock_init(&cpuctx->hrtimer_lock);
941 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra272325c2015-04-15 11:41:58 +0200942 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +0200943}
944
Peter Zijlstra272325c2015-04-15 11:41:58 +0200945static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +0200946{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200947 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200948 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200949 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +0200950
951 /* not for SW PMU */
952 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +0200953 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +0200954
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200955 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
956 if (!cpuctx->hrtimer_active) {
957 cpuctx->hrtimer_active = 1;
958 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
959 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
960 }
961 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +0200962
Peter Zijlstra272325c2015-04-15 11:41:58 +0200963 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +0200964}
965
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200966void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200967{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200968 int *count = this_cpu_ptr(pmu->pmu_disable_count);
969 if (!(*count)++)
970 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200971}
972
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200973void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200974{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200975 int *count = this_cpu_ptr(pmu->pmu_disable_count);
976 if (!--(*count))
977 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200978}
979
Mark Rutland2fde4f92015-01-07 15:01:54 +0000980static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200981
982/*
Mark Rutland2fde4f92015-01-07 15:01:54 +0000983 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
984 * perf_event_task_tick() are fully serialized because they're strictly cpu
985 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
986 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200987 */
Mark Rutland2fde4f92015-01-07 15:01:54 +0000988static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200989{
Mark Rutland2fde4f92015-01-07 15:01:54 +0000990 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200991
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200992 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200993
Mark Rutland2fde4f92015-01-07 15:01:54 +0000994 WARN_ON(!list_empty(&ctx->active_ctx_list));
995
996 list_add(&ctx->active_ctx_list, head);
997}
998
999static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1000{
1001 WARN_ON(!irqs_disabled());
1002
1003 WARN_ON(list_empty(&ctx->active_ctx_list));
1004
1005 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001006}
1007
1008static void get_ctx(struct perf_event_context *ctx)
1009{
1010 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
1011}
1012
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001013static void free_ctx(struct rcu_head *head)
1014{
1015 struct perf_event_context *ctx;
1016
1017 ctx = container_of(head, struct perf_event_context, rcu_head);
1018 kfree(ctx->task_ctx_data);
1019 kfree(ctx);
1020}
1021
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001022static void put_ctx(struct perf_event_context *ctx)
1023{
1024 if (atomic_dec_and_test(&ctx->refcount)) {
1025 if (ctx->parent_ctx)
1026 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001027 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001028 put_task_struct(ctx->task);
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001029 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001030 }
1031}
1032
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001033/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001034 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1035 * perf_pmu_migrate_context() we need some magic.
1036 *
1037 * Those places that change perf_event::ctx will hold both
1038 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1039 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001040 * Lock ordering is by mutex address. There are two other sites where
1041 * perf_event_context::mutex nests and those are:
1042 *
1043 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001044 * perf_event_exit_event()
1045 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001046 *
1047 * - perf_event_init_context() [ parent, 0 ]
1048 * inherit_task_group()
1049 * inherit_group()
1050 * inherit_event()
1051 * perf_event_alloc()
1052 * perf_init_event()
1053 * perf_try_init_event() [ child , 1 ]
1054 *
1055 * While it appears there is an obvious deadlock here -- the parent and child
1056 * nesting levels are inverted between the two. This is in fact safe because
1057 * life-time rules separate them. That is an exiting task cannot fork, and a
1058 * spawning task cannot (yet) exit.
1059 *
1060 * But remember that that these are parent<->child context relations, and
1061 * migration does not affect children, therefore these two orderings should not
1062 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001063 *
1064 * The change in perf_event::ctx does not affect children (as claimed above)
1065 * because the sys_perf_event_open() case will install a new event and break
1066 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1067 * concerned with cpuctx and that doesn't have children.
1068 *
1069 * The places that change perf_event::ctx will issue:
1070 *
1071 * perf_remove_from_context();
1072 * synchronize_rcu();
1073 * perf_install_in_context();
1074 *
1075 * to affect the change. The remove_from_context() + synchronize_rcu() should
1076 * quiesce the event, after which we can install it in the new location. This
1077 * means that only external vectors (perf_fops, prctl) can perturb the event
1078 * while in transit. Therefore all such accessors should also acquire
1079 * perf_event_context::mutex to serialize against this.
1080 *
1081 * However; because event->ctx can change while we're waiting to acquire
1082 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1083 * function.
1084 *
1085 * Lock order:
1086 * task_struct::perf_event_mutex
1087 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001088 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001089 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001090 * perf_event::mmap_mutex
1091 * mmap_sem
1092 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001093static struct perf_event_context *
1094perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001095{
1096 struct perf_event_context *ctx;
1097
1098again:
1099 rcu_read_lock();
1100 ctx = ACCESS_ONCE(event->ctx);
1101 if (!atomic_inc_not_zero(&ctx->refcount)) {
1102 rcu_read_unlock();
1103 goto again;
1104 }
1105 rcu_read_unlock();
1106
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001107 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001108 if (event->ctx != ctx) {
1109 mutex_unlock(&ctx->mutex);
1110 put_ctx(ctx);
1111 goto again;
1112 }
1113
1114 return ctx;
1115}
1116
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001117static inline struct perf_event_context *
1118perf_event_ctx_lock(struct perf_event *event)
1119{
1120 return perf_event_ctx_lock_nested(event, 0);
1121}
1122
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001123static void perf_event_ctx_unlock(struct perf_event *event,
1124 struct perf_event_context *ctx)
1125{
1126 mutex_unlock(&ctx->mutex);
1127 put_ctx(ctx);
1128}
1129
1130/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001131 * This must be done under the ctx->lock, such as to serialize against
1132 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1133 * calling scheduler related locks and ctx->lock nests inside those.
1134 */
1135static __must_check struct perf_event_context *
1136unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001137{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001138 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1139
1140 lockdep_assert_held(&ctx->lock);
1141
1142 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001143 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001144 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001145
1146 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001147}
1148
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001149static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1150{
1151 /*
1152 * only top level events have the pid namespace they were created in
1153 */
1154 if (event->parent)
1155 event = event->parent;
1156
1157 return task_tgid_nr_ns(p, event->ns);
1158}
1159
1160static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1161{
1162 /*
1163 * only top level events have the pid namespace they were created in
1164 */
1165 if (event->parent)
1166 event = event->parent;
1167
1168 return task_pid_nr_ns(p, event->ns);
1169}
1170
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001171/*
1172 * If we inherit events we want to return the parent event id
1173 * to userspace.
1174 */
1175static u64 primary_event_id(struct perf_event *event)
1176{
1177 u64 id = event->id;
1178
1179 if (event->parent)
1180 id = event->parent->id;
1181
1182 return id;
1183}
1184
1185/*
1186 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001187 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001188 * This has to cope with with the fact that until it is locked,
1189 * the context could get moved to another task.
1190 */
1191static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001192perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001193{
1194 struct perf_event_context *ctx;
1195
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001196retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001197 /*
1198 * One of the few rules of preemptible RCU is that one cannot do
1199 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001200 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001201 * rcu_read_unlock_special().
1202 *
1203 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001204 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001205 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001206 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001207 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001208 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001209 if (ctx) {
1210 /*
1211 * If this context is a clone of another, it might
1212 * get swapped for another underneath us by
1213 * perf_event_task_sched_out, though the
1214 * rcu_read_lock() protects us from any context
1215 * getting freed. Lock the context and check if it
1216 * got swapped before we could get the lock, and retry
1217 * if so. If we locked the right context, then it
1218 * can't get swapped on us any more.
1219 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001220 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001221 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001222 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001223 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001224 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001225 goto retry;
1226 }
1227
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001228 if (ctx->task == TASK_TOMBSTONE ||
1229 !atomic_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001230 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001231 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001232 } else {
1233 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001234 }
1235 }
1236 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001237 if (!ctx)
1238 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001239 return ctx;
1240}
1241
1242/*
1243 * Get the context for a task and increment its pin_count so it
1244 * can't get swapped to another task. This also increments its
1245 * reference count so that the context can't get freed.
1246 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001247static struct perf_event_context *
1248perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001249{
1250 struct perf_event_context *ctx;
1251 unsigned long flags;
1252
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001253 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001254 if (ctx) {
1255 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001256 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001257 }
1258 return ctx;
1259}
1260
1261static void perf_unpin_context(struct perf_event_context *ctx)
1262{
1263 unsigned long flags;
1264
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001265 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001266 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001267 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001268}
1269
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001270/*
1271 * Update the record of the current time in a context.
1272 */
1273static void update_context_time(struct perf_event_context *ctx)
1274{
1275 u64 now = perf_clock();
1276
1277 ctx->time += now - ctx->timestamp;
1278 ctx->timestamp = now;
1279}
1280
Stephane Eranian41587552011-01-03 18:20:01 +02001281static u64 perf_event_time(struct perf_event *event)
1282{
1283 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001284
1285 if (is_cgroup_event(event))
1286 return perf_cgroup_event_time(event);
1287
Stephane Eranian41587552011-01-03 18:20:01 +02001288 return ctx ? ctx->time : 0;
1289}
1290
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001291/*
1292 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001293 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001294 */
1295static void update_event_times(struct perf_event *event)
1296{
1297 struct perf_event_context *ctx = event->ctx;
1298 u64 run_end;
1299
1300 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1301 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1302 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001303 /*
1304 * in cgroup mode, time_enabled represents
1305 * the time the event was enabled AND active
1306 * tasks were in the monitored cgroup. This is
1307 * independent of the activity of the context as
1308 * there may be a mix of cgroup and non-cgroup events.
1309 *
1310 * That is why we treat cgroup events differently
1311 * here.
1312 */
1313 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001314 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001315 else if (ctx->is_active)
1316 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001317 else
1318 run_end = event->tstamp_stopped;
1319
1320 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001321
1322 if (event->state == PERF_EVENT_STATE_INACTIVE)
1323 run_end = event->tstamp_stopped;
1324 else
Stephane Eranian41587552011-01-03 18:20:01 +02001325 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001326
1327 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001328
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001329}
1330
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001331/*
1332 * Update total_time_enabled and total_time_running for all events in a group.
1333 */
1334static void update_group_times(struct perf_event *leader)
1335{
1336 struct perf_event *event;
1337
1338 update_event_times(leader);
1339 list_for_each_entry(event, &leader->sibling_list, group_entry)
1340 update_event_times(event);
1341}
1342
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001343static struct list_head *
1344ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1345{
1346 if (event->attr.pinned)
1347 return &ctx->pinned_groups;
1348 else
1349 return &ctx->flexible_groups;
1350}
1351
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001352/*
1353 * Add a event from the lists for its context.
1354 * Must be called with ctx->mutex and ctx->lock held.
1355 */
1356static void
1357list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1358{
Peter Zijlstrac994d612016-01-08 09:20:23 +01001359 lockdep_assert_held(&ctx->lock);
1360
Peter Zijlstra8a495422010-05-27 15:47:49 +02001361 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1362 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001363
1364 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001365 * If we're a stand alone event or group leader, we go to the context
1366 * list, group events are kept attached to the group so that
1367 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001368 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001369 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001370 struct list_head *list;
1371
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001372 if (is_software_event(event))
1373 event->group_flags |= PERF_GROUP_SOFTWARE;
1374
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001375 list = ctx_group_list(event, ctx);
1376 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001377 }
1378
Peter Zijlstra08309372011-03-03 11:31:20 +01001379 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001380 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001381
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001382 list_add_rcu(&event->event_entry, &ctx->event_list);
1383 ctx->nr_events++;
1384 if (event->attr.inherit_stat)
1385 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001386
1387 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001388}
1389
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001390/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001391 * Initialize event state based on the perf_event_attr::disabled.
1392 */
1393static inline void perf_event__state_init(struct perf_event *event)
1394{
1395 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1396 PERF_EVENT_STATE_INACTIVE;
1397}
1398
Peter Zijlstraa7239682015-09-09 19:06:33 +02001399static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001400{
1401 int entry = sizeof(u64); /* value */
1402 int size = 0;
1403 int nr = 1;
1404
1405 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1406 size += sizeof(u64);
1407
1408 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1409 size += sizeof(u64);
1410
1411 if (event->attr.read_format & PERF_FORMAT_ID)
1412 entry += sizeof(u64);
1413
1414 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001415 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001416 size += sizeof(u64);
1417 }
1418
1419 size += entry * nr;
1420 event->read_size = size;
1421}
1422
Peter Zijlstraa7239682015-09-09 19:06:33 +02001423static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001424{
1425 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001426 u16 size = 0;
1427
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001428 if (sample_type & PERF_SAMPLE_IP)
1429 size += sizeof(data->ip);
1430
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001431 if (sample_type & PERF_SAMPLE_ADDR)
1432 size += sizeof(data->addr);
1433
1434 if (sample_type & PERF_SAMPLE_PERIOD)
1435 size += sizeof(data->period);
1436
Andi Kleenc3feedf2013-01-24 16:10:28 +01001437 if (sample_type & PERF_SAMPLE_WEIGHT)
1438 size += sizeof(data->weight);
1439
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001440 if (sample_type & PERF_SAMPLE_READ)
1441 size += event->read_size;
1442
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001443 if (sample_type & PERF_SAMPLE_DATA_SRC)
1444 size += sizeof(data->data_src.val);
1445
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001446 if (sample_type & PERF_SAMPLE_TRANSACTION)
1447 size += sizeof(data->txn);
1448
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001449 event->header_size = size;
1450}
1451
Peter Zijlstraa7239682015-09-09 19:06:33 +02001452/*
1453 * Called at perf_event creation and when events are attached/detached from a
1454 * group.
1455 */
1456static void perf_event__header_size(struct perf_event *event)
1457{
1458 __perf_event_read_size(event,
1459 event->group_leader->nr_siblings);
1460 __perf_event_header_size(event, event->attr.sample_type);
1461}
1462
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001463static void perf_event__id_header_size(struct perf_event *event)
1464{
1465 struct perf_sample_data *data;
1466 u64 sample_type = event->attr.sample_type;
1467 u16 size = 0;
1468
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001469 if (sample_type & PERF_SAMPLE_TID)
1470 size += sizeof(data->tid_entry);
1471
1472 if (sample_type & PERF_SAMPLE_TIME)
1473 size += sizeof(data->time);
1474
Adrian Hunterff3d5272013-08-27 11:23:07 +03001475 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1476 size += sizeof(data->id);
1477
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001478 if (sample_type & PERF_SAMPLE_ID)
1479 size += sizeof(data->id);
1480
1481 if (sample_type & PERF_SAMPLE_STREAM_ID)
1482 size += sizeof(data->stream_id);
1483
1484 if (sample_type & PERF_SAMPLE_CPU)
1485 size += sizeof(data->cpu_entry);
1486
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001487 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001488}
1489
Peter Zijlstraa7239682015-09-09 19:06:33 +02001490static bool perf_event_validate_size(struct perf_event *event)
1491{
1492 /*
1493 * The values computed here will be over-written when we actually
1494 * attach the event.
1495 */
1496 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1497 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1498 perf_event__id_header_size(event);
1499
1500 /*
1501 * Sum the lot; should not exceed the 64k limit we have on records.
1502 * Conservative limit to allow for callchains and other variable fields.
1503 */
1504 if (event->read_size + event->header_size +
1505 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1506 return false;
1507
1508 return true;
1509}
1510
Peter Zijlstra8a495422010-05-27 15:47:49 +02001511static void perf_group_attach(struct perf_event *event)
1512{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001513 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001514
Peter Zijlstra74c33372010-10-15 11:40:29 +02001515 /*
1516 * We can have double attach due to group movement in perf_event_open.
1517 */
1518 if (event->attach_state & PERF_ATTACH_GROUP)
1519 return;
1520
Peter Zijlstra8a495422010-05-27 15:47:49 +02001521 event->attach_state |= PERF_ATTACH_GROUP;
1522
1523 if (group_leader == event)
1524 return;
1525
Peter Zijlstra652884f2015-01-23 11:20:10 +01001526 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1527
Peter Zijlstra8a495422010-05-27 15:47:49 +02001528 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1529 !is_software_event(event))
1530 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1531
1532 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1533 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001534
1535 perf_event__header_size(group_leader);
1536
1537 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1538 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001539}
1540
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001541/*
1542 * Remove a event from the lists for its context.
1543 * Must be called with ctx->mutex and ctx->lock held.
1544 */
1545static void
1546list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1547{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001548 struct perf_cpu_context *cpuctx;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001549
1550 WARN_ON_ONCE(event->ctx != ctx);
1551 lockdep_assert_held(&ctx->lock);
1552
Peter Zijlstra8a495422010-05-27 15:47:49 +02001553 /*
1554 * We can have double detach due to exit/hot-unplug + close.
1555 */
1556 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001557 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001558
1559 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1560
Stephane Eranian68cacd22011-03-23 16:03:06 +01001561 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001562 ctx->nr_cgroups--;
Peter Zijlstra70a01652016-01-08 09:29:16 +01001563 /*
1564 * Because cgroup events are always per-cpu events, this will
1565 * always be called from the right CPU.
1566 */
Stephane Eranian68cacd22011-03-23 16:03:06 +01001567 cpuctx = __get_cpu_context(ctx);
1568 /*
Peter Zijlstra70a01652016-01-08 09:29:16 +01001569 * If there are no more cgroup events then clear cgrp to avoid
1570 * stale pointer in update_cgrp_time_from_cpuctx().
Stephane Eranian68cacd22011-03-23 16:03:06 +01001571 */
1572 if (!ctx->nr_cgroups)
1573 cpuctx->cgrp = NULL;
1574 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001575
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001576 ctx->nr_events--;
1577 if (event->attr.inherit_stat)
1578 ctx->nr_stat--;
1579
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001580 list_del_rcu(&event->event_entry);
1581
Peter Zijlstra8a495422010-05-27 15:47:49 +02001582 if (event->group_leader == event)
1583 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001584
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001585 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001586
1587 /*
1588 * If event was in error state, then keep it
1589 * that way, otherwise bogus counts will be
1590 * returned on read(). The only way to get out
1591 * of error state is by explicit re-enabling
1592 * of the event
1593 */
1594 if (event->state > PERF_EVENT_STATE_OFF)
1595 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001596
1597 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001598}
1599
Peter Zijlstra8a495422010-05-27 15:47:49 +02001600static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001601{
1602 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001603 struct list_head *list = NULL;
1604
1605 /*
1606 * We can have double detach due to exit/hot-unplug + close.
1607 */
1608 if (!(event->attach_state & PERF_ATTACH_GROUP))
1609 return;
1610
1611 event->attach_state &= ~PERF_ATTACH_GROUP;
1612
1613 /*
1614 * If this is a sibling, remove it from its group.
1615 */
1616 if (event->group_leader != event) {
1617 list_del_init(&event->group_entry);
1618 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001619 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001620 }
1621
1622 if (!list_empty(&event->group_entry))
1623 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001624
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001625 /*
1626 * If this was a group event with sibling events then
1627 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001628 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001629 */
1630 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001631 if (list)
1632 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001633 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001634
1635 /* Inherit group flags from the previous leader */
1636 sibling->group_flags = event->group_flags;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001637
1638 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001639 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001640
1641out:
1642 perf_event__header_size(event->group_leader);
1643
1644 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1645 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001646}
1647
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001648/*
1649 * User event without the task.
1650 */
1651static bool is_orphaned_event(struct perf_event *event)
1652{
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01001653 return event && !is_kernel_event(event) && !READ_ONCE(event->owner);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001654}
1655
1656/*
1657 * Event has a parent but parent's task finished and it's
1658 * alive only because of children holding refference.
1659 */
1660static bool is_orphaned_child(struct perf_event *event)
1661{
1662 return is_orphaned_event(event->parent);
1663}
1664
1665static void orphans_remove_work(struct work_struct *work);
1666
1667static void schedule_orphans_remove(struct perf_event_context *ctx)
1668{
1669 if (!ctx->task || ctx->orphans_remove_sched || !perf_wq)
1670 return;
1671
1672 if (queue_delayed_work(perf_wq, &ctx->orphans_remove, 1)) {
1673 get_ctx(ctx);
1674 ctx->orphans_remove_sched = true;
1675 }
1676}
1677
1678static int __init perf_workqueue_init(void)
1679{
1680 perf_wq = create_singlethread_workqueue("perf");
1681 WARN(!perf_wq, "failed to create perf workqueue\n");
1682 return perf_wq ? 0 : -1;
1683}
1684
1685core_initcall(perf_workqueue_init);
1686
Mark Rutland66eb5792015-05-13 17:12:23 +01001687static inline int pmu_filter_match(struct perf_event *event)
1688{
1689 struct pmu *pmu = event->pmu;
1690 return pmu->filter_match ? pmu->filter_match(event) : 1;
1691}
1692
Stephane Eranianfa66f072010-08-26 16:40:01 +02001693static inline int
1694event_filter_match(struct perf_event *event)
1695{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001696 return (event->cpu == -1 || event->cpu == smp_processor_id())
Mark Rutland66eb5792015-05-13 17:12:23 +01001697 && perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001698}
1699
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001700static void
1701event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001702 struct perf_cpu_context *cpuctx,
1703 struct perf_event_context *ctx)
1704{
Stephane Eranian41587552011-01-03 18:20:01 +02001705 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001706 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001707
1708 WARN_ON_ONCE(event->ctx != ctx);
1709 lockdep_assert_held(&ctx->lock);
1710
Stephane Eranianfa66f072010-08-26 16:40:01 +02001711 /*
1712 * An event which could not be activated because of
1713 * filter mismatch still needs to have its timings
1714 * maintained, otherwise bogus information is return
1715 * via read() for time_enabled, time_running:
1716 */
1717 if (event->state == PERF_EVENT_STATE_INACTIVE
1718 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001719 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001720 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001721 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001722 }
1723
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001724 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001725 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001726
Alexander Shishkin44377272013-12-16 14:17:36 +02001727 perf_pmu_disable(event->pmu);
1728
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001729 event->state = PERF_EVENT_STATE_INACTIVE;
1730 if (event->pending_disable) {
1731 event->pending_disable = 0;
1732 event->state = PERF_EVENT_STATE_OFF;
1733 }
Stephane Eranian41587552011-01-03 18:20:01 +02001734 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001735 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001736 event->oncpu = -1;
1737
1738 if (!is_software_event(event))
1739 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001740 if (!--ctx->nr_active)
1741 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001742 if (event->attr.freq && event->attr.sample_freq)
1743 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001744 if (event->attr.exclusive || !cpuctx->active_oncpu)
1745 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001746
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001747 if (is_orphaned_child(event))
1748 schedule_orphans_remove(ctx);
1749
Alexander Shishkin44377272013-12-16 14:17:36 +02001750 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001751}
1752
1753static void
1754group_sched_out(struct perf_event *group_event,
1755 struct perf_cpu_context *cpuctx,
1756 struct perf_event_context *ctx)
1757{
1758 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001759 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001760
1761 event_sched_out(group_event, cpuctx, ctx);
1762
1763 /*
1764 * Schedule out siblings (if any):
1765 */
1766 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1767 event_sched_out(event, cpuctx, ctx);
1768
Stephane Eranianfa66f072010-08-26 16:40:01 +02001769 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001770 cpuctx->exclusive = 0;
1771}
1772
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001773#define DETACH_GROUP 0x01UL
1774
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001775/*
1776 * Cross CPU call to remove a performance event
1777 *
1778 * We disable the event on the hardware level first. After that we
1779 * remove it from the context list.
1780 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001781static void
1782__perf_remove_from_context(struct perf_event *event,
1783 struct perf_cpu_context *cpuctx,
1784 struct perf_event_context *ctx,
1785 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001786{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001787 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001788
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001789 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001790 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001791 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001792 list_del_event(event, ctx);
Peter Zijlstra39a43642016-01-11 12:46:35 +01001793
1794 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001795 ctx->is_active = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01001796 if (ctx->task) {
1797 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1798 cpuctx->task_ctx = NULL;
1799 }
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001800 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001801}
1802
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001803/*
1804 * Remove the event from a task's (or a CPU's) list of events.
1805 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001806 * If event->ctx is a cloned context, callers must make sure that
1807 * every task struct that event->ctx->task could possibly point to
1808 * remains valid. This is OK when called from perf_release since
1809 * that only calls us on the top-level context, which can't be a clone.
1810 * When called from perf_event_exit_task, it's OK because the
1811 * context has been detached from its task.
1812 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001813static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001814{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001815 lockdep_assert_held(&event->ctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001816
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001817 event_function_call(event, __perf_remove_from_context, (void *)flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001818}
1819
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001820/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001821 * Cross CPU call to disable a performance event
1822 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001823static void __perf_event_disable(struct perf_event *event,
1824 struct perf_cpu_context *cpuctx,
1825 struct perf_event_context *ctx,
1826 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001827{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001828 if (event->state < PERF_EVENT_STATE_INACTIVE)
1829 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001830
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001831 update_context_time(ctx);
1832 update_cgrp_time_from_event(event);
1833 update_group_times(event);
1834 if (event == event->group_leader)
1835 group_sched_out(event, cpuctx, ctx);
1836 else
1837 event_sched_out(event, cpuctx, ctx);
1838 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra7b648012015-12-03 18:35:21 +01001839}
1840
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001841/*
1842 * Disable a event.
1843 *
1844 * If event->ctx is a cloned context, callers must make sure that
1845 * every task struct that event->ctx->task could possibly point to
1846 * remains valid. This condition is satisifed when called through
1847 * perf_event_for_each_child or perf_event_for_each because they
1848 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001849 * goes to exit will block in perf_event_exit_event().
1850 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001851 * When called from perf_pending_event it's OK because event->ctx
1852 * is the current context on this CPU and preemption is disabled,
1853 * hence we can't get into perf_event_task_sched_out for this context.
1854 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001855static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001856{
1857 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001858
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001859 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001860 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001861 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001862 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001863 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001864 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001865
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001866 event_function_call(event, __perf_event_disable, NULL);
1867}
1868
1869void perf_event_disable_local(struct perf_event *event)
1870{
1871 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001872}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001873
1874/*
1875 * Strictly speaking kernel users cannot create groups and therefore this
1876 * interface does not need the perf_event_ctx_lock() magic.
1877 */
1878void perf_event_disable(struct perf_event *event)
1879{
1880 struct perf_event_context *ctx;
1881
1882 ctx = perf_event_ctx_lock(event);
1883 _perf_event_disable(event);
1884 perf_event_ctx_unlock(event, ctx);
1885}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001886EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001887
Stephane Eraniane5d13672011-02-14 11:20:01 +02001888static void perf_set_shadow_time(struct perf_event *event,
1889 struct perf_event_context *ctx,
1890 u64 tstamp)
1891{
1892 /*
1893 * use the correct time source for the time snapshot
1894 *
1895 * We could get by without this by leveraging the
1896 * fact that to get to this function, the caller
1897 * has most likely already called update_context_time()
1898 * and update_cgrp_time_xx() and thus both timestamp
1899 * are identical (or very close). Given that tstamp is,
1900 * already adjusted for cgroup, we could say that:
1901 * tstamp - ctx->timestamp
1902 * is equivalent to
1903 * tstamp - cgrp->timestamp.
1904 *
1905 * Then, in perf_output_read(), the calculation would
1906 * work with no changes because:
1907 * - event is guaranteed scheduled in
1908 * - no scheduled out in between
1909 * - thus the timestamp would be the same
1910 *
1911 * But this is a bit hairy.
1912 *
1913 * So instead, we have an explicit cgroup call to remain
1914 * within the time time source all along. We believe it
1915 * is cleaner and simpler to understand.
1916 */
1917 if (is_cgroup_event(event))
1918 perf_cgroup_set_shadow_time(event, tstamp);
1919 else
1920 event->shadow_ctx_time = tstamp - ctx->timestamp;
1921}
1922
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001923#define MAX_INTERRUPTS (~0ULL)
1924
1925static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001926static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001927
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001928static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001929event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001930 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001931 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001932{
Stephane Eranian41587552011-01-03 18:20:01 +02001933 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001934 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001935
Peter Zijlstra63342412014-05-05 11:49:16 +02001936 lockdep_assert_held(&ctx->lock);
1937
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001938 if (event->state <= PERF_EVENT_STATE_OFF)
1939 return 0;
1940
1941 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001942 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001943
1944 /*
1945 * Unthrottle events, since we scheduled we might have missed several
1946 * ticks already, also for a heavily scheduling task there is little
1947 * guarantee it'll get a tick in a timely manner.
1948 */
1949 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1950 perf_log_throttle(event, 1);
1951 event->hw.interrupts = 0;
1952 }
1953
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001954 /*
1955 * The new state must be visible before we turn it on in the hardware:
1956 */
1957 smp_wmb();
1958
Alexander Shishkin44377272013-12-16 14:17:36 +02001959 perf_pmu_disable(event->pmu);
1960
Shaohua Li72f669c2015-02-05 15:55:31 -08001961 perf_set_shadow_time(event, ctx, tstamp);
1962
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001963 perf_log_itrace_start(event);
1964
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001965 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001966 event->state = PERF_EVENT_STATE_INACTIVE;
1967 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001968 ret = -EAGAIN;
1969 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001970 }
1971
Peter Zijlstra00a29162015-07-27 10:35:07 +02001972 event->tstamp_running += tstamp - event->tstamp_stopped;
1973
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001974 if (!is_software_event(event))
1975 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001976 if (!ctx->nr_active++)
1977 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001978 if (event->attr.freq && event->attr.sample_freq)
1979 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001980
1981 if (event->attr.exclusive)
1982 cpuctx->exclusive = 1;
1983
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001984 if (is_orphaned_child(event))
1985 schedule_orphans_remove(ctx);
1986
Alexander Shishkin44377272013-12-16 14:17:36 +02001987out:
1988 perf_pmu_enable(event->pmu);
1989
1990 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001991}
1992
1993static int
1994group_sched_in(struct perf_event *group_event,
1995 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001996 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001997{
Lin Ming6bde9b62010-04-23 13:56:00 +08001998 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001999 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02002000 u64 now = ctx->time;
2001 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002002
2003 if (group_event->state == PERF_EVENT_STATE_OFF)
2004 return 0;
2005
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07002006 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08002007
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002008 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002009 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02002010 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002011 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02002012 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002013
2014 /*
2015 * Schedule in siblings as one group (if any):
2016 */
2017 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002018 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002019 partial_group = event;
2020 goto group_error;
2021 }
2022 }
2023
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002024 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002025 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002026
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002027group_error:
2028 /*
2029 * Groups can be scheduled in as one unit only, so undo any
2030 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02002031 * The events up to the failed event are scheduled out normally,
2032 * tstamp_stopped will be updated.
2033 *
2034 * The failed events and the remaining siblings need to have
2035 * their timings updated as if they had gone thru event_sched_in()
2036 * and event_sched_out(). This is required to get consistent timings
2037 * across the group. This also takes care of the case where the group
2038 * could never be scheduled by ensuring tstamp_stopped is set to mark
2039 * the time the event was actually stopped, such that time delta
2040 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002041 */
2042 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2043 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02002044 simulate = true;
2045
2046 if (simulate) {
2047 event->tstamp_running += now - event->tstamp_stopped;
2048 event->tstamp_stopped = now;
2049 } else {
2050 event_sched_out(event, cpuctx, ctx);
2051 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002052 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002053 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002054
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002055 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002056
Peter Zijlstra272325c2015-04-15 11:41:58 +02002057 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002058
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002059 return -EAGAIN;
2060}
2061
2062/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002063 * Work out whether we can put this event group on the CPU now.
2064 */
2065static int group_can_go_on(struct perf_event *event,
2066 struct perf_cpu_context *cpuctx,
2067 int can_add_hw)
2068{
2069 /*
2070 * Groups consisting entirely of software events can always go on.
2071 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01002072 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002073 return 1;
2074 /*
2075 * If an exclusive group is already on, no other hardware
2076 * events can go on.
2077 */
2078 if (cpuctx->exclusive)
2079 return 0;
2080 /*
2081 * If this group is exclusive and there are already
2082 * events on the CPU, it can't go on.
2083 */
2084 if (event->attr.exclusive && cpuctx->active_oncpu)
2085 return 0;
2086 /*
2087 * Otherwise, try to add it if all previous groups were able
2088 * to go on.
2089 */
2090 return can_add_hw;
2091}
2092
2093static void add_event_to_ctx(struct perf_event *event,
2094 struct perf_event_context *ctx)
2095{
Stephane Eranian41587552011-01-03 18:20:01 +02002096 u64 tstamp = perf_event_time(event);
2097
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002098 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002099 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002100 event->tstamp_enabled = tstamp;
2101 event->tstamp_running = tstamp;
2102 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002103}
2104
Peter Zijlstra3e349502016-01-08 10:01:18 +01002105static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2106 struct perf_event_context *ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002107static void
2108ctx_sched_in(struct perf_event_context *ctx,
2109 struct perf_cpu_context *cpuctx,
2110 enum event_type_t event_type,
2111 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002112
Peter Zijlstradce58552011-04-09 21:17:46 +02002113static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2114 struct perf_event_context *ctx,
2115 struct task_struct *task)
2116{
2117 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2118 if (ctx)
2119 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2120 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2121 if (ctx)
2122 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2123}
2124
Peter Zijlstra3e349502016-01-08 10:01:18 +01002125static void ctx_resched(struct perf_cpu_context *cpuctx,
2126 struct perf_event_context *task_ctx)
2127{
2128 perf_pmu_disable(cpuctx->ctx.pmu);
2129 if (task_ctx)
2130 task_ctx_sched_out(cpuctx, task_ctx);
2131 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2132 perf_event_sched_in(cpuctx, task_ctx, current);
2133 perf_pmu_enable(cpuctx->ctx.pmu);
2134}
2135
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002136/*
2137 * Cross CPU call to install and enable a performance event
2138 *
2139 * Must be called with ctx->mutex held
2140 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002141static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002142{
Peter Zijlstra39a43642016-01-11 12:46:35 +01002143 struct perf_event_context *ctx = info;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002144 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002145 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002146
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002147 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002148 if (ctx->task) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002149 raw_spin_lock(&ctx->lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002150 /*
2151 * If we hit the 'wrong' task, we've since scheduled and
2152 * everything should be sorted, nothing to do!
2153 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002154 task_ctx = ctx;
Peter Zijlstra39a43642016-01-11 12:46:35 +01002155 if (ctx->task != current)
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002156 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002157
Peter Zijlstra39a43642016-01-11 12:46:35 +01002158 /*
2159 * If task_ctx is set, it had better be to us.
2160 */
2161 WARN_ON_ONCE(cpuctx->task_ctx != ctx && cpuctx->task_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002162 } else if (task_ctx) {
2163 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002164 }
2165
Peter Zijlstra39a43642016-01-11 12:46:35 +01002166 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002167unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002168 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002169
2170 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002171}
2172
2173/*
2174 * Attach a performance event to a context
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002175 */
2176static void
2177perf_install_in_context(struct perf_event_context *ctx,
2178 struct perf_event *event,
2179 int cpu)
2180{
Peter Zijlstra39a43642016-01-11 12:46:35 +01002181 struct task_struct *task = NULL;
2182
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002183 lockdep_assert_held(&ctx->mutex);
2184
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002185 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002186 if (event->cpu != -1)
2187 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002188
Peter Zijlstra39a43642016-01-11 12:46:35 +01002189 /*
2190 * Installing events is tricky because we cannot rely on ctx->is_active
2191 * to be set in case this is the nr_events 0 -> 1 transition.
2192 *
2193 * So what we do is we add the event to the list here, which will allow
2194 * a future context switch to DTRT and then send a racy IPI. If the IPI
2195 * fails to hit the right task, this means a context switch must have
2196 * happened and that will have taken care of business.
2197 */
2198 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002199 task = ctx->task;
2200 /*
2201 * Worse, we cannot even rely on the ctx actually existing anymore. If
2202 * between find_get_context() and perf_install_in_context() the task
2203 * went through perf_event_exit_task() its dead and we should not be
2204 * adding new events.
2205 */
2206 if (task == TASK_TOMBSTONE) {
2207 raw_spin_unlock_irq(&ctx->lock);
2208 return;
2209 }
Peter Zijlstra39a43642016-01-11 12:46:35 +01002210 update_context_time(ctx);
2211 /*
2212 * Update cgrp time only if current cgrp matches event->cgrp.
2213 * Must be done before calling add_event_to_ctx().
2214 */
2215 update_cgrp_time_from_event(event);
2216 add_event_to_ctx(event, ctx);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002217 raw_spin_unlock_irq(&ctx->lock);
2218
2219 if (task)
2220 task_function_call(task, __perf_install_in_context, ctx);
2221 else
2222 cpu_function_call(cpu, __perf_install_in_context, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002223}
2224
2225/*
2226 * Put a event into inactive state and update time fields.
2227 * Enabling the leader of a group effectively enables all
2228 * the group members that aren't explicitly disabled, so we
2229 * have to update their ->tstamp_enabled also.
2230 * Note: this works for group members as well as group leaders
2231 * since the non-leader members' sibling_lists will be empty.
2232 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002233static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002234{
2235 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002236 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002237
2238 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002239 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002240 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002241 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2242 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002243 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002244}
2245
2246/*
2247 * Cross CPU call to enable a performance event
2248 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002249static void __perf_event_enable(struct perf_event *event,
2250 struct perf_cpu_context *cpuctx,
2251 struct perf_event_context *ctx,
2252 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002253{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002254 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002255 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002256
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002257 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2258 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002259 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002260
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002261 update_context_time(ctx);
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002262 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002263
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002264 if (!ctx->is_active)
2265 return;
2266
Stephane Eraniane5d13672011-02-14 11:20:01 +02002267 if (!event_filter_match(event)) {
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002268 if (is_cgroup_event(event)) {
2269 perf_cgroup_set_timestamp(current, ctx); // XXX ?
Stephane Eraniane5d13672011-02-14 11:20:01 +02002270 perf_cgroup_defer_enabled(event);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002271 }
2272 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002273 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002274
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002275 /*
2276 * If the event is in a group and isn't the group leader,
2277 * then don't put it on unless the group is on.
2278 */
2279 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002280 return;
2281
2282 task_ctx = cpuctx->task_ctx;
2283 if (ctx->task)
2284 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002285
Peter Zijlstraaee7dbc2016-01-08 10:45:11 +01002286 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002287}
2288
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002289/*
2290 * Enable a event.
2291 *
2292 * If event->ctx is a cloned context, callers must make sure that
2293 * every task struct that event->ctx->task could possibly point to
2294 * remains valid. This condition is satisfied when called through
2295 * perf_event_for_each_child or perf_event_for_each as described
2296 * for perf_event_disable.
2297 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002298static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002299{
2300 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002301
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002302 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002303 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2304 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002305 raw_spin_unlock_irq(&ctx->lock);
2306 return;
2307 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002308
2309 /*
2310 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002311 *
2312 * That way, if we see the event in error state below, we know that it
2313 * has gone back into error state, as distinct from the task having
2314 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002315 */
2316 if (event->state == PERF_EVENT_STATE_ERROR)
2317 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002318 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002319
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002320 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002321}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002322
2323/*
2324 * See perf_event_disable();
2325 */
2326void perf_event_enable(struct perf_event *event)
2327{
2328 struct perf_event_context *ctx;
2329
2330 ctx = perf_event_ctx_lock(event);
2331 _perf_event_enable(event);
2332 perf_event_ctx_unlock(event, ctx);
2333}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002334EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002335
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002336static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002337{
2338 /*
2339 * not supported on inherited events
2340 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002341 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002342 return -EINVAL;
2343
2344 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002345 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002346
2347 return 0;
2348}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002349
2350/*
2351 * See perf_event_disable()
2352 */
2353int perf_event_refresh(struct perf_event *event, int refresh)
2354{
2355 struct perf_event_context *ctx;
2356 int ret;
2357
2358 ctx = perf_event_ctx_lock(event);
2359 ret = _perf_event_refresh(event, refresh);
2360 perf_event_ctx_unlock(event, ctx);
2361
2362 return ret;
2363}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002364EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002365
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002366static void ctx_sched_out(struct perf_event_context *ctx,
2367 struct perf_cpu_context *cpuctx,
2368 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002369{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002370 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002371 struct perf_event *event;
2372
2373 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002374
Peter Zijlstra39a43642016-01-11 12:46:35 +01002375 if (likely(!ctx->nr_events)) {
2376 /*
2377 * See __perf_remove_from_context().
2378 */
2379 WARN_ON_ONCE(ctx->is_active);
2380 if (ctx->task)
2381 WARN_ON_ONCE(cpuctx->task_ctx);
2382 return;
2383 }
2384
Peter Zijlstradb24d332011-04-09 21:17:45 +02002385 ctx->is_active &= ~event_type;
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002386 if (ctx->task) {
2387 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2388 if (!ctx->is_active)
2389 cpuctx->task_ctx = NULL;
2390 }
2391
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002392 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002393 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002394 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002395 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002396
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002397 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002398 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002399 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2400 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002401 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002402
Peter Zijlstradb24d332011-04-09 21:17:45 +02002403 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002404 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002405 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002406 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002407 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002408}
2409
2410/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002411 * Test whether two contexts are equivalent, i.e. whether they have both been
2412 * cloned from the same version of the same context.
2413 *
2414 * Equivalence is measured using a generation number in the context that is
2415 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2416 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002417 */
2418static int context_equiv(struct perf_event_context *ctx1,
2419 struct perf_event_context *ctx2)
2420{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002421 lockdep_assert_held(&ctx1->lock);
2422 lockdep_assert_held(&ctx2->lock);
2423
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002424 /* Pinning disables the swap optimization */
2425 if (ctx1->pin_count || ctx2->pin_count)
2426 return 0;
2427
2428 /* If ctx1 is the parent of ctx2 */
2429 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2430 return 1;
2431
2432 /* If ctx2 is the parent of ctx1 */
2433 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2434 return 1;
2435
2436 /*
2437 * If ctx1 and ctx2 have the same parent; we flatten the parent
2438 * hierarchy, see perf_event_init_context().
2439 */
2440 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2441 ctx1->parent_gen == ctx2->parent_gen)
2442 return 1;
2443
2444 /* Unmatched */
2445 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002446}
2447
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002448static void __perf_event_sync_stat(struct perf_event *event,
2449 struct perf_event *next_event)
2450{
2451 u64 value;
2452
2453 if (!event->attr.inherit_stat)
2454 return;
2455
2456 /*
2457 * Update the event value, we cannot use perf_event_read()
2458 * because we're in the middle of a context switch and have IRQs
2459 * disabled, which upsets smp_call_function_single(), however
2460 * we know the event must be on the current CPU, therefore we
2461 * don't need to use it.
2462 */
2463 switch (event->state) {
2464 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002465 event->pmu->read(event);
2466 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002467
2468 case PERF_EVENT_STATE_INACTIVE:
2469 update_event_times(event);
2470 break;
2471
2472 default:
2473 break;
2474 }
2475
2476 /*
2477 * In order to keep per-task stats reliable we need to flip the event
2478 * values when we flip the contexts.
2479 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002480 value = local64_read(&next_event->count);
2481 value = local64_xchg(&event->count, value);
2482 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002483
2484 swap(event->total_time_enabled, next_event->total_time_enabled);
2485 swap(event->total_time_running, next_event->total_time_running);
2486
2487 /*
2488 * Since we swizzled the values, update the user visible data too.
2489 */
2490 perf_event_update_userpage(event);
2491 perf_event_update_userpage(next_event);
2492}
2493
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002494static void perf_event_sync_stat(struct perf_event_context *ctx,
2495 struct perf_event_context *next_ctx)
2496{
2497 struct perf_event *event, *next_event;
2498
2499 if (!ctx->nr_stat)
2500 return;
2501
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002502 update_context_time(ctx);
2503
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002504 event = list_first_entry(&ctx->event_list,
2505 struct perf_event, event_entry);
2506
2507 next_event = list_first_entry(&next_ctx->event_list,
2508 struct perf_event, event_entry);
2509
2510 while (&event->event_entry != &ctx->event_list &&
2511 &next_event->event_entry != &next_ctx->event_list) {
2512
2513 __perf_event_sync_stat(event, next_event);
2514
2515 event = list_next_entry(event, event_entry);
2516 next_event = list_next_entry(next_event, event_entry);
2517 }
2518}
2519
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002520static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2521 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002522{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002523 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002524 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002525 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002526 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002527 int do_switch = 1;
2528
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002529 if (likely(!ctx))
2530 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002531
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002532 cpuctx = __get_cpu_context(ctx);
2533 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002534 return;
2535
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002536 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002537 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002538 if (!next_ctx)
2539 goto unlock;
2540
2541 parent = rcu_dereference(ctx->parent_ctx);
2542 next_parent = rcu_dereference(next_ctx->parent_ctx);
2543
2544 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002545 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002546 goto unlock;
2547
2548 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002549 /*
2550 * Looks like the two contexts are clones, so we might be
2551 * able to optimize the context switch. We lock both
2552 * contexts and check that they are clones under the
2553 * lock (including re-checking that neither has been
2554 * uncloned in the meantime). It doesn't matter which
2555 * order we take the locks because no other cpu could
2556 * be trying to lock both of these tasks.
2557 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002558 raw_spin_lock(&ctx->lock);
2559 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002560 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002561 WRITE_ONCE(ctx->task, next);
2562 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05002563
2564 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2565
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002566 /*
2567 * RCU_INIT_POINTER here is safe because we've not
2568 * modified the ctx and the above modification of
2569 * ctx->task and ctx->task_ctx_data are immaterial
2570 * since those values are always verified under
2571 * ctx->lock which we're now holding.
2572 */
2573 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2574 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2575
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002576 do_switch = 0;
2577
2578 perf_event_sync_stat(ctx, next_ctx);
2579 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002580 raw_spin_unlock(&next_ctx->lock);
2581 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002582 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002583unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002584 rcu_read_unlock();
2585
2586 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002587 raw_spin_lock(&ctx->lock);
Peter Zijlstra8833d0e2016-01-08 10:02:37 +01002588 task_ctx_sched_out(cpuctx, ctx);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002589 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002590 }
2591}
2592
Yan, Zhengba532502014-11-04 21:55:58 -05002593void perf_sched_cb_dec(struct pmu *pmu)
2594{
2595 this_cpu_dec(perf_sched_cb_usages);
2596}
2597
2598void perf_sched_cb_inc(struct pmu *pmu)
2599{
2600 this_cpu_inc(perf_sched_cb_usages);
2601}
2602
2603/*
2604 * This function provides the context switch callback to the lower code
2605 * layer. It is invoked ONLY when the context switch callback is enabled.
2606 */
2607static void perf_pmu_sched_task(struct task_struct *prev,
2608 struct task_struct *next,
2609 bool sched_in)
2610{
2611 struct perf_cpu_context *cpuctx;
2612 struct pmu *pmu;
2613 unsigned long flags;
2614
2615 if (prev == next)
2616 return;
2617
2618 local_irq_save(flags);
2619
2620 rcu_read_lock();
2621
2622 list_for_each_entry_rcu(pmu, &pmus, entry) {
2623 if (pmu->sched_task) {
2624 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2625
2626 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2627
2628 perf_pmu_disable(pmu);
2629
2630 pmu->sched_task(cpuctx->task_ctx, sched_in);
2631
2632 perf_pmu_enable(pmu);
2633
2634 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2635 }
2636 }
2637
2638 rcu_read_unlock();
2639
2640 local_irq_restore(flags);
2641}
2642
Adrian Hunter45ac1402015-07-21 12:44:02 +03002643static void perf_event_switch(struct task_struct *task,
2644 struct task_struct *next_prev, bool sched_in);
2645
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002646#define for_each_task_context_nr(ctxn) \
2647 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2648
2649/*
2650 * Called from scheduler to remove the events of the current task,
2651 * with interrupts disabled.
2652 *
2653 * We stop each event and update the event value in event->count.
2654 *
2655 * This does not protect us against NMI, but disable()
2656 * sets the disabled bit in the control field of event _before_
2657 * accessing the event control register. If a NMI hits, then it will
2658 * not restart the event.
2659 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002660void __perf_event_task_sched_out(struct task_struct *task,
2661 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002662{
2663 int ctxn;
2664
Yan, Zhengba532502014-11-04 21:55:58 -05002665 if (__this_cpu_read(perf_sched_cb_usages))
2666 perf_pmu_sched_task(task, next, false);
2667
Adrian Hunter45ac1402015-07-21 12:44:02 +03002668 if (atomic_read(&nr_switch_events))
2669 perf_event_switch(task, next, false);
2670
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002671 for_each_task_context_nr(ctxn)
2672 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002673
2674 /*
2675 * if cgroup events exist on this CPU, then we need
2676 * to check if we have to switch out PMU state.
2677 * cgroup event are system-wide mode only
2678 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002679 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002680 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002681}
2682
Peter Zijlstra3e349502016-01-08 10:01:18 +01002683static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2684 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002685{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002686 if (!cpuctx->task_ctx)
2687 return;
2688
2689 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2690 return;
2691
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002692 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002693}
2694
2695/*
2696 * Called with IRQs disabled
2697 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002698static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2699 enum event_type_t event_type)
2700{
2701 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002702}
2703
2704static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002705ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002706 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002707{
2708 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002709
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002710 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2711 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002712 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002713 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002714 continue;
2715
Stephane Eraniane5d13672011-02-14 11:20:01 +02002716 /* may need to reset tstamp_enabled */
2717 if (is_cgroup_event(event))
2718 perf_cgroup_mark_enabled(event, ctx);
2719
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002720 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002721 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002722
2723 /*
2724 * If this pinned group hasn't been scheduled,
2725 * put it in error state.
2726 */
2727 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2728 update_group_times(event);
2729 event->state = PERF_EVENT_STATE_ERROR;
2730 }
2731 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002732}
2733
2734static void
2735ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002736 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002737{
2738 struct perf_event *event;
2739 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002740
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002741 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2742 /* Ignore events in OFF or ERROR state */
2743 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002744 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002745 /*
2746 * Listen to the 'cpu' scheduling filter constraint
2747 * of events:
2748 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002749 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002750 continue;
2751
Stephane Eraniane5d13672011-02-14 11:20:01 +02002752 /* may need to reset tstamp_enabled */
2753 if (is_cgroup_event(event))
2754 perf_cgroup_mark_enabled(event, ctx);
2755
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002756 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002757 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002758 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002759 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002760 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002761}
2762
2763static void
2764ctx_sched_in(struct perf_event_context *ctx,
2765 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002766 enum event_type_t event_type,
2767 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002768{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002769 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002770 u64 now;
2771
2772 lockdep_assert_held(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002773
Peter Zijlstra39a43642016-01-11 12:46:35 +01002774 if (likely(!ctx->nr_events))
2775 return;
2776
Peter Zijlstradb24d332011-04-09 21:17:45 +02002777 ctx->is_active |= event_type;
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002778 if (ctx->task) {
2779 if (!is_active)
2780 cpuctx->task_ctx = ctx;
2781 else
2782 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2783 }
2784
Stephane Eraniane5d13672011-02-14 11:20:01 +02002785 now = perf_clock();
2786 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002787 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002788 /*
2789 * First go through the list and put on any pinned groups
2790 * in order to give them the best chance of going on.
2791 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002792 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002793 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002794
2795 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002796 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002797 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002798}
2799
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002800static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002801 enum event_type_t event_type,
2802 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002803{
2804 struct perf_event_context *ctx = &cpuctx->ctx;
2805
Stephane Eraniane5d13672011-02-14 11:20:01 +02002806 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002807}
2808
Stephane Eraniane5d13672011-02-14 11:20:01 +02002809static void perf_event_context_sched_in(struct perf_event_context *ctx,
2810 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002811{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002812 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002813
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002814 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002815 if (cpuctx->task_ctx == ctx)
2816 return;
2817
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002818 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002819 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002820 /*
2821 * We want to keep the following priority order:
2822 * cpu pinned (that don't need to move), task pinned,
2823 * cpu flexible, task flexible.
2824 */
2825 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002826 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002827 perf_pmu_enable(ctx->pmu);
2828 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002829}
2830
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002831/*
2832 * Called from scheduler to add the events of the current task
2833 * with interrupts disabled.
2834 *
2835 * We restore the event value and then enable it.
2836 *
2837 * This does not protect us against NMI, but enable()
2838 * sets the enabled bit in the control field of event _before_
2839 * accessing the event control register. If a NMI hits, then it will
2840 * keep the event running.
2841 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002842void __perf_event_task_sched_in(struct task_struct *prev,
2843 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002844{
2845 struct perf_event_context *ctx;
2846 int ctxn;
2847
Peter Zijlstra7e41d172016-01-08 09:21:40 +01002848 /*
2849 * If cgroup events exist on this CPU, then we need to check if we have
2850 * to switch in PMU state; cgroup event are system-wide mode only.
2851 *
2852 * Since cgroup events are CPU events, we must schedule these in before
2853 * we schedule in the task events.
2854 */
2855 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
2856 perf_cgroup_sched_in(prev, task);
2857
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002858 for_each_task_context_nr(ctxn) {
2859 ctx = task->perf_event_ctxp[ctxn];
2860 if (likely(!ctx))
2861 continue;
2862
Stephane Eraniane5d13672011-02-14 11:20:01 +02002863 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002864 }
Stephane Eraniand010b332012-02-09 23:21:00 +01002865
Adrian Hunter45ac1402015-07-21 12:44:02 +03002866 if (atomic_read(&nr_switch_events))
2867 perf_event_switch(task, prev, true);
2868
Yan, Zhengba532502014-11-04 21:55:58 -05002869 if (__this_cpu_read(perf_sched_cb_usages))
2870 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002871}
2872
Peter Zijlstraabd50712010-01-26 18:50:16 +01002873static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2874{
2875 u64 frequency = event->attr.sample_freq;
2876 u64 sec = NSEC_PER_SEC;
2877 u64 divisor, dividend;
2878
2879 int count_fls, nsec_fls, frequency_fls, sec_fls;
2880
2881 count_fls = fls64(count);
2882 nsec_fls = fls64(nsec);
2883 frequency_fls = fls64(frequency);
2884 sec_fls = 30;
2885
2886 /*
2887 * We got @count in @nsec, with a target of sample_freq HZ
2888 * the target period becomes:
2889 *
2890 * @count * 10^9
2891 * period = -------------------
2892 * @nsec * sample_freq
2893 *
2894 */
2895
2896 /*
2897 * Reduce accuracy by one bit such that @a and @b converge
2898 * to a similar magnitude.
2899 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002900#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002901do { \
2902 if (a##_fls > b##_fls) { \
2903 a >>= 1; \
2904 a##_fls--; \
2905 } else { \
2906 b >>= 1; \
2907 b##_fls--; \
2908 } \
2909} while (0)
2910
2911 /*
2912 * Reduce accuracy until either term fits in a u64, then proceed with
2913 * the other, so that finally we can do a u64/u64 division.
2914 */
2915 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2916 REDUCE_FLS(nsec, frequency);
2917 REDUCE_FLS(sec, count);
2918 }
2919
2920 if (count_fls + sec_fls > 64) {
2921 divisor = nsec * frequency;
2922
2923 while (count_fls + sec_fls > 64) {
2924 REDUCE_FLS(count, sec);
2925 divisor >>= 1;
2926 }
2927
2928 dividend = count * sec;
2929 } else {
2930 dividend = count * sec;
2931
2932 while (nsec_fls + frequency_fls > 64) {
2933 REDUCE_FLS(nsec, frequency);
2934 dividend >>= 1;
2935 }
2936
2937 divisor = nsec * frequency;
2938 }
2939
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002940 if (!divisor)
2941 return dividend;
2942
Peter Zijlstraabd50712010-01-26 18:50:16 +01002943 return div64_u64(dividend, divisor);
2944}
2945
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002946static DEFINE_PER_CPU(int, perf_throttled_count);
2947static DEFINE_PER_CPU(u64, perf_throttled_seq);
2948
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002949static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002950{
2951 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002952 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002953 s64 delta;
2954
Peter Zijlstraabd50712010-01-26 18:50:16 +01002955 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002956
2957 delta = (s64)(period - hwc->sample_period);
2958 delta = (delta + 7) / 8; /* low pass filter */
2959
2960 sample_period = hwc->sample_period + delta;
2961
2962 if (!sample_period)
2963 sample_period = 1;
2964
2965 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002966
Peter Zijlstrae7850592010-05-21 14:43:08 +02002967 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002968 if (disable)
2969 event->pmu->stop(event, PERF_EF_UPDATE);
2970
Peter Zijlstrae7850592010-05-21 14:43:08 +02002971 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002972
2973 if (disable)
2974 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002975 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002976}
2977
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002978/*
2979 * combine freq adjustment with unthrottling to avoid two passes over the
2980 * events. At the same time, make sure, having freq events does not change
2981 * the rate of unthrottling as that would introduce bias.
2982 */
2983static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2984 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002985{
2986 struct perf_event *event;
2987 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002988 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002989 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002990
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002991 /*
2992 * only need to iterate over all events iff:
2993 * - context have events in frequency mode (needs freq adjust)
2994 * - there are events to unthrottle on this cpu
2995 */
2996 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002997 return;
2998
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002999 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003000 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003001
Paul Mackerras03541f82009-10-14 16:58:03 +11003002 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003003 if (event->state != PERF_EVENT_STATE_ACTIVE)
3004 continue;
3005
Stephane Eranian5632ab12011-01-03 18:20:01 +02003006 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003007 continue;
3008
Alexander Shishkin44377272013-12-16 14:17:36 +02003009 perf_pmu_disable(event->pmu);
3010
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003011 hwc = &event->hw;
3012
Jiri Olsaae23bff2013-08-24 16:45:54 +02003013 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003014 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003015 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003016 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003017 }
3018
3019 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003020 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003021
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003022 /*
3023 * stop the event and update event->count
3024 */
3025 event->pmu->stop(event, PERF_EF_UPDATE);
3026
Peter Zijlstrae7850592010-05-21 14:43:08 +02003027 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003028 delta = now - hwc->freq_count_stamp;
3029 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003030
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003031 /*
3032 * restart the event
3033 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003034 * we have stopped the event so tell that
3035 * to perf_adjust_period() to avoid stopping it
3036 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003037 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003038 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003039 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003040
3041 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003042 next:
3043 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003044 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003045
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003046 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003047 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003048}
3049
3050/*
3051 * Round-robin a context's events:
3052 */
3053static void rotate_ctx(struct perf_event_context *ctx)
3054{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003055 /*
3056 * Rotate the first entry last of non-pinned groups. Rotation might be
3057 * disabled by the inheritance code.
3058 */
3059 if (!ctx->rotate_disable)
3060 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003061}
3062
Stephane Eranian9e630202013-04-03 14:21:33 +02003063static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003064{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003065 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003066 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003067
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003068 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003069 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3070 rotate = 1;
3071 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003072
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003073 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003074 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003075 if (ctx->nr_events != ctx->nr_active)
3076 rotate = 1;
3077 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003078
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003079 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003080 goto done;
3081
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003082 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003083 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003084
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003085 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3086 if (ctx)
3087 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003088
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003089 rotate_ctx(&cpuctx->ctx);
3090 if (ctx)
3091 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003092
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003093 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003094
3095 perf_pmu_enable(cpuctx->ctx.pmu);
3096 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003097done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003098
3099 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003100}
3101
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003102#ifdef CONFIG_NO_HZ_FULL
3103bool perf_event_can_stop_tick(void)
3104{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003105 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003106 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003107 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003108 else
3109 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003110}
3111#endif
3112
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003113void perf_event_task_tick(void)
3114{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003115 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3116 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003117 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003118
3119 WARN_ON(!irqs_disabled());
3120
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003121 __this_cpu_inc(perf_throttled_seq);
3122 throttled = __this_cpu_xchg(perf_throttled_count, 0);
3123
Mark Rutland2fde4f92015-01-07 15:01:54 +00003124 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003125 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003126}
3127
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003128static int event_enable_on_exec(struct perf_event *event,
3129 struct perf_event_context *ctx)
3130{
3131 if (!event->attr.enable_on_exec)
3132 return 0;
3133
3134 event->attr.enable_on_exec = 0;
3135 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3136 return 0;
3137
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003138 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003139
3140 return 1;
3141}
3142
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003143/*
3144 * Enable all of a task's events that have been marked enable-on-exec.
3145 * This expects task == current.
3146 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003147static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003148{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003149 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003150 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003151 struct perf_event *event;
3152 unsigned long flags;
3153 int enabled = 0;
3154
3155 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003156 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003157 if (!ctx || !ctx->nr_events)
3158 goto out;
3159
Peter Zijlstra3e349502016-01-08 10:01:18 +01003160 cpuctx = __get_cpu_context(ctx);
3161 perf_ctx_lock(cpuctx, ctx);
3162 list_for_each_entry(event, &ctx->event_list, event_entry)
3163 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003164
3165 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003166 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003167 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003168 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003169 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003170 ctx_resched(cpuctx, ctx);
3171 }
3172 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003173
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003174out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003175 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003176
3177 if (clone_ctx)
3178 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003179}
3180
Peter Zijlstrae041e322014-05-21 17:32:19 +02003181void perf_event_exec(void)
3182{
Peter Zijlstrae041e322014-05-21 17:32:19 +02003183 int ctxn;
3184
3185 rcu_read_lock();
Peter Zijlstrac1274492015-12-10 20:57:40 +01003186 for_each_task_context_nr(ctxn)
3187 perf_event_enable_on_exec(ctxn);
Peter Zijlstrae041e322014-05-21 17:32:19 +02003188 rcu_read_unlock();
3189}
3190
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003191struct perf_read_data {
3192 struct perf_event *event;
3193 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003194 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003195};
3196
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003197/*
3198 * Cross CPU call to read the hardware event
3199 */
3200static void __perf_event_read(void *info)
3201{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003202 struct perf_read_data *data = info;
3203 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003204 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003205 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003206 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003207
3208 /*
3209 * If this is a task context, we need to check whether it is
3210 * the current task context of this cpu. If not it has been
3211 * scheduled out before the smp call arrived. In that case
3212 * event->count would have been updated to a recent sample
3213 * when the event was scheduled out.
3214 */
3215 if (ctx->task && cpuctx->task_ctx != ctx)
3216 return;
3217
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003218 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003219 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003220 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003221 update_cgrp_time_from_event(event);
3222 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003223
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003224 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003225 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003226 goto unlock;
3227
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003228 if (!data->group) {
3229 pmu->read(event);
3230 data->ret = 0;
3231 goto unlock;
3232 }
3233
3234 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3235
3236 pmu->read(event);
3237
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003238 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3239 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003240 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3241 /*
3242 * Use sibling's PMU rather than @event's since
3243 * sibling could be on different (eg: software) PMU.
3244 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003245 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003246 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003247 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003248
3249 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003250
3251unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003252 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003253}
3254
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003255static inline u64 perf_event_count(struct perf_event *event)
3256{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003257 if (event->pmu->count)
3258 return event->pmu->count(event);
3259
3260 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003261}
3262
Kaixu Xiaffe86902015-08-06 07:02:32 +00003263/*
3264 * NMI-safe method to read a local event, that is an event that
3265 * is:
3266 * - either for the current task, or for this CPU
3267 * - does not have inherit set, for inherited task events
3268 * will not be local and we cannot read them atomically
3269 * - must not have a pmu::count method
3270 */
3271u64 perf_event_read_local(struct perf_event *event)
3272{
3273 unsigned long flags;
3274 u64 val;
3275
3276 /*
3277 * Disabling interrupts avoids all counter scheduling (context
3278 * switches, timer based rotation and IPIs).
3279 */
3280 local_irq_save(flags);
3281
3282 /* If this is a per-task event, it must be for current */
3283 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3284 event->hw.target != current);
3285
3286 /* If this is a per-CPU event, it must be for this CPU */
3287 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3288 event->cpu != smp_processor_id());
3289
3290 /*
3291 * It must not be an event with inherit set, we cannot read
3292 * all child counters from atomic context.
3293 */
3294 WARN_ON_ONCE(event->attr.inherit);
3295
3296 /*
3297 * It must not have a pmu::count method, those are not
3298 * NMI safe.
3299 */
3300 WARN_ON_ONCE(event->pmu->count);
3301
3302 /*
3303 * If the event is currently on this CPU, its either a per-task event,
3304 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3305 * oncpu == -1).
3306 */
3307 if (event->oncpu == smp_processor_id())
3308 event->pmu->read(event);
3309
3310 val = local64_read(&event->count);
3311 local_irq_restore(flags);
3312
3313 return val;
3314}
3315
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003316static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003317{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003318 int ret = 0;
3319
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003320 /*
3321 * If event is enabled and currently active on a CPU, update the
3322 * value in the event structure:
3323 */
3324 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003325 struct perf_read_data data = {
3326 .event = event,
3327 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003328 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003329 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003330 smp_call_function_single(event->oncpu,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003331 __perf_event_read, &data, 1);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003332 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003333 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003334 struct perf_event_context *ctx = event->ctx;
3335 unsigned long flags;
3336
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003337 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003338 /*
3339 * may read while context is not active
3340 * (e.g., thread is blocked), in that case
3341 * we cannot update context time
3342 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003343 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003344 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003345 update_cgrp_time_from_event(event);
3346 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003347 if (group)
3348 update_group_times(event);
3349 else
3350 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003351 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003352 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003353
3354 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003355}
3356
3357/*
3358 * Initialize the perf_event context in a task_struct:
3359 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003360static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003361{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003362 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003363 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003364 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003365 INIT_LIST_HEAD(&ctx->pinned_groups);
3366 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003367 INIT_LIST_HEAD(&ctx->event_list);
3368 atomic_set(&ctx->refcount, 1);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003369 INIT_DELAYED_WORK(&ctx->orphans_remove, orphans_remove_work);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003370}
3371
Peter Zijlstraeb184472010-09-07 15:55:13 +02003372static struct perf_event_context *
3373alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003374{
3375 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003376
3377 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3378 if (!ctx)
3379 return NULL;
3380
3381 __perf_event_init_context(ctx);
3382 if (task) {
3383 ctx->task = task;
3384 get_task_struct(task);
3385 }
3386 ctx->pmu = pmu;
3387
3388 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003389}
3390
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003391static struct task_struct *
3392find_lively_task_by_vpid(pid_t vpid)
3393{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003394 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003395 int err;
3396
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003397 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003398 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003399 task = current;
3400 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003401 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402 if (task)
3403 get_task_struct(task);
3404 rcu_read_unlock();
3405
3406 if (!task)
3407 return ERR_PTR(-ESRCH);
3408
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003409 /* Reuse ptrace permission checks for now. */
3410 err = -EACCES;
3411 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3412 goto errout;
3413
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003414 return task;
3415errout:
3416 put_task_struct(task);
3417 return ERR_PTR(err);
3418
3419}
3420
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003421/*
3422 * Returns a matching context with refcount and pincount.
3423 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003424static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003425find_get_context(struct pmu *pmu, struct task_struct *task,
3426 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003427{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003428 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003429 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003430 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003431 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003432 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003433 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003434
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003435 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003436 /* Must be root to operate on a CPU event: */
3437 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3438 return ERR_PTR(-EACCES);
3439
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003440 /*
3441 * We could be clever and allow to attach a event to an
3442 * offline CPU and activate it when the CPU comes up, but
3443 * that's for later.
3444 */
3445 if (!cpu_online(cpu))
3446 return ERR_PTR(-ENODEV);
3447
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003448 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003449 ctx = &cpuctx->ctx;
3450 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003451 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003452
3453 return ctx;
3454 }
3455
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003456 err = -EINVAL;
3457 ctxn = pmu->task_ctx_nr;
3458 if (ctxn < 0)
3459 goto errout;
3460
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003461 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3462 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3463 if (!task_ctx_data) {
3464 err = -ENOMEM;
3465 goto errout;
3466 }
3467 }
3468
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003469retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003470 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003471 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003472 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003473 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003474
3475 if (task_ctx_data && !ctx->task_ctx_data) {
3476 ctx->task_ctx_data = task_ctx_data;
3477 task_ctx_data = NULL;
3478 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003479 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003480
3481 if (clone_ctx)
3482 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003483 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003484 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003485 err = -ENOMEM;
3486 if (!ctx)
3487 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003488
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003489 if (task_ctx_data) {
3490 ctx->task_ctx_data = task_ctx_data;
3491 task_ctx_data = NULL;
3492 }
3493
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003494 err = 0;
3495 mutex_lock(&task->perf_event_mutex);
3496 /*
3497 * If it has already passed perf_event_exit_task().
3498 * we must see PF_EXITING, it takes this mutex too.
3499 */
3500 if (task->flags & PF_EXITING)
3501 err = -ESRCH;
3502 else if (task->perf_event_ctxp[ctxn])
3503 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003504 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003505 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003506 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003507 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003508 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003509 mutex_unlock(&task->perf_event_mutex);
3510
3511 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003512 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003513
3514 if (err == -EAGAIN)
3515 goto retry;
3516 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003517 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003518 }
3519
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003520 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003521 return ctx;
3522
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003523errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003524 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003525 return ERR_PTR(err);
3526}
3527
Li Zefan6fb29152009-10-15 11:21:42 +08003528static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003529static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003530
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003531static void free_event_rcu(struct rcu_head *head)
3532{
3533 struct perf_event *event;
3534
3535 event = container_of(head, struct perf_event, rcu_head);
3536 if (event->ns)
3537 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003538 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003539 kfree(event);
3540}
3541
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003542static void ring_buffer_attach(struct perf_event *event,
3543 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003544
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003545static void unaccount_event_cpu(struct perf_event *event, int cpu)
3546{
3547 if (event->parent)
3548 return;
3549
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003550 if (is_cgroup_event(event))
3551 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3552}
3553
3554static void unaccount_event(struct perf_event *event)
3555{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003556 bool dec = false;
3557
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003558 if (event->parent)
3559 return;
3560
3561 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003562 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003563 if (event->attr.mmap || event->attr.mmap_data)
3564 atomic_dec(&nr_mmap_events);
3565 if (event->attr.comm)
3566 atomic_dec(&nr_comm_events);
3567 if (event->attr.task)
3568 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003569 if (event->attr.freq)
3570 atomic_dec(&nr_freq_events);
Adrian Hunter45ac1402015-07-21 12:44:02 +03003571 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003572 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003573 atomic_dec(&nr_switch_events);
3574 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003575 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003576 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003577 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003578 dec = true;
3579
3580 if (dec)
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003581 static_key_slow_dec_deferred(&perf_sched_events);
3582
3583 unaccount_event_cpu(event, event->cpu);
3584}
3585
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003586/*
3587 * The following implement mutual exclusion of events on "exclusive" pmus
3588 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3589 * at a time, so we disallow creating events that might conflict, namely:
3590 *
3591 * 1) cpu-wide events in the presence of per-task events,
3592 * 2) per-task events in the presence of cpu-wide events,
3593 * 3) two matching events on the same context.
3594 *
3595 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003596 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003597 */
3598static int exclusive_event_init(struct perf_event *event)
3599{
3600 struct pmu *pmu = event->pmu;
3601
3602 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3603 return 0;
3604
3605 /*
3606 * Prevent co-existence of per-task and cpu-wide events on the
3607 * same exclusive pmu.
3608 *
3609 * Negative pmu::exclusive_cnt means there are cpu-wide
3610 * events on this "exclusive" pmu, positive means there are
3611 * per-task events.
3612 *
3613 * Since this is called in perf_event_alloc() path, event::ctx
3614 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3615 * to mean "per-task event", because unlike other attach states it
3616 * never gets cleared.
3617 */
3618 if (event->attach_state & PERF_ATTACH_TASK) {
3619 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
3620 return -EBUSY;
3621 } else {
3622 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
3623 return -EBUSY;
3624 }
3625
3626 return 0;
3627}
3628
3629static void exclusive_event_destroy(struct perf_event *event)
3630{
3631 struct pmu *pmu = event->pmu;
3632
3633 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3634 return;
3635
3636 /* see comment in exclusive_event_init() */
3637 if (event->attach_state & PERF_ATTACH_TASK)
3638 atomic_dec(&pmu->exclusive_cnt);
3639 else
3640 atomic_inc(&pmu->exclusive_cnt);
3641}
3642
3643static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
3644{
3645 if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
3646 (e1->cpu == e2->cpu ||
3647 e1->cpu == -1 ||
3648 e2->cpu == -1))
3649 return true;
3650 return false;
3651}
3652
3653/* Called under the same ctx::mutex as perf_install_in_context() */
3654static bool exclusive_event_installable(struct perf_event *event,
3655 struct perf_event_context *ctx)
3656{
3657 struct perf_event *iter_event;
3658 struct pmu *pmu = event->pmu;
3659
3660 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3661 return true;
3662
3663 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
3664 if (exclusive_event_match(iter_event, event))
3665 return false;
3666 }
3667
3668 return true;
3669}
3670
Peter Zijlstra683ede42014-05-05 12:11:24 +02003671static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003672{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003673 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003674
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003675 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003676
Frederic Weisbecker76369132011-05-19 19:55:04 +02003677 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003678 /*
3679 * Can happen when we close an event with re-directed output.
3680 *
3681 * Since we have a 0 refcount, perf_mmap_close() will skip
3682 * over us; possibly making our ring_buffer_put() the last.
3683 */
3684 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003685 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003686 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003687 }
3688
Stephane Eraniane5d13672011-02-14 11:20:01 +02003689 if (is_cgroup_event(event))
3690 perf_detach_cgroup(event);
3691
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003692 if (!event->parent) {
3693 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3694 put_callchain_buffers();
3695 }
3696
3697 perf_event_free_bpf_prog(event);
3698
3699 if (event->destroy)
3700 event->destroy(event);
3701
3702 if (event->ctx)
3703 put_ctx(event->ctx);
3704
3705 if (event->pmu) {
3706 exclusive_event_destroy(event);
3707 module_put(event->pmu->module);
3708 }
3709
3710 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003711}
3712
Peter Zijlstra683ede42014-05-05 12:11:24 +02003713/*
3714 * Used to free events which have a known refcount of 1, such as in error paths
3715 * where the event isn't exposed yet and inherited events.
3716 */
3717static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003718{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003719 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3720 "unexpected event refcount: %ld; ptr=%p\n",
3721 atomic_long_read(&event->refcount), event)) {
3722 /* leak to avoid use-after-free */
3723 return;
3724 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003725
Peter Zijlstra683ede42014-05-05 12:11:24 +02003726 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003727}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003728
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003729/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003730 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003731 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003732static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003733{
Peter Zijlstra88821352010-11-09 19:01:43 +01003734 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003735
Peter Zijlstra88821352010-11-09 19:01:43 +01003736 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01003737 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003738 * Matches the smp_store_release() in perf_event_exit_task(). If we
3739 * observe !owner it means the list deletion is complete and we can
3740 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01003741 * owner->perf_event_mutex.
3742 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003743 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01003744 if (owner) {
3745 /*
3746 * Since delayed_put_task_struct() also drops the last
3747 * task reference we can safely take a new reference
3748 * while holding the rcu_read_lock().
3749 */
3750 get_task_struct(owner);
3751 }
3752 rcu_read_unlock();
3753
3754 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003755 /*
3756 * If we're here through perf_event_exit_task() we're already
3757 * holding ctx->mutex which would be an inversion wrt. the
3758 * normal lock order.
3759 *
3760 * However we can safely take this lock because its the child
3761 * ctx->mutex.
3762 */
3763 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
3764
Peter Zijlstra88821352010-11-09 19:01:43 +01003765 /*
3766 * We have to re-check the event->owner field, if it is cleared
3767 * we raced with perf_event_exit_task(), acquiring the mutex
3768 * ensured they're done, and we can proceed with freeing the
3769 * event.
3770 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003771 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01003772 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003773 smp_store_release(&event->owner, NULL);
3774 }
Peter Zijlstra88821352010-11-09 19:01:43 +01003775 mutex_unlock(&owner->perf_event_mutex);
3776 put_task_struct(owner);
3777 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003778}
3779
Jiri Olsaf8697762014-08-01 14:33:01 +02003780static void put_event(struct perf_event *event)
3781{
Peter Zijlstraa83fe282015-01-29 14:44:34 +01003782 struct perf_event_context *ctx;
Jiri Olsaf8697762014-08-01 14:33:01 +02003783
3784 if (!atomic_long_dec_and_test(&event->refcount))
3785 return;
3786
3787 if (!is_kernel_event(event))
3788 perf_remove_from_owner(event);
Peter Zijlstra88821352010-11-09 19:01:43 +01003789
Peter Zijlstra683ede42014-05-05 12:11:24 +02003790 /*
3791 * There are two ways this annotation is useful:
3792 *
3793 * 1) there is a lock recursion from perf_event_exit_task
3794 * see the comment there.
3795 *
3796 * 2) there is a lock-inversion with mmap_sem through
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07003797 * perf_read_group(), which takes faults while
Peter Zijlstra683ede42014-05-05 12:11:24 +02003798 * holding ctx->mutex, however this is called after
3799 * the last filedesc died, so there is no possibility
3800 * to trigger the AB-BA case.
3801 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01003802 ctx = perf_event_ctx_lock_nested(event, SINGLE_DEPTH_NESTING);
3803 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01003804 perf_remove_from_context(event, DETACH_GROUP);
Leon Yud415a7f2015-02-26 20:43:33 +08003805 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra683ede42014-05-05 12:11:24 +02003806
3807 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003808}
3809
Peter Zijlstra683ede42014-05-05 12:11:24 +02003810int perf_event_release_kernel(struct perf_event *event)
3811{
3812 put_event(event);
3813 return 0;
3814}
3815EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3816
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02003817/*
3818 * Called when the last reference to the file is gone.
3819 */
Al Viroa6fa9412012-08-20 14:59:25 +01003820static int perf_release(struct inode *inode, struct file *file)
3821{
3822 put_event(file->private_data);
3823 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003824}
3825
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003826/*
3827 * Remove all orphanes events from the context.
3828 */
3829static void orphans_remove_work(struct work_struct *work)
3830{
3831 struct perf_event_context *ctx;
3832 struct perf_event *event, *tmp;
3833
3834 ctx = container_of(work, struct perf_event_context,
3835 orphans_remove.work);
3836
3837 mutex_lock(&ctx->mutex);
3838 list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) {
3839 struct perf_event *parent_event = event->parent;
3840
3841 if (!is_orphaned_child(event))
3842 continue;
3843
Peter Zijlstra45a0e072016-01-26 13:09:48 +01003844 perf_remove_from_context(event, DETACH_GROUP);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003845
3846 mutex_lock(&parent_event->child_mutex);
3847 list_del_init(&event->child_list);
3848 mutex_unlock(&parent_event->child_mutex);
3849
3850 free_event(event);
3851 put_event(parent_event);
3852 }
3853
3854 raw_spin_lock_irq(&ctx->lock);
3855 ctx->orphans_remove_sched = false;
3856 raw_spin_unlock_irq(&ctx->lock);
3857 mutex_unlock(&ctx->mutex);
3858
3859 put_ctx(ctx);
3860}
3861
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003862u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003863{
3864 struct perf_event *child;
3865 u64 total = 0;
3866
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003867 *enabled = 0;
3868 *running = 0;
3869
Peter Zijlstra6f105812009-11-20 22:19:56 +01003870 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003871
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003872 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003873 total += perf_event_count(event);
3874
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003875 *enabled += event->total_time_enabled +
3876 atomic64_read(&event->child_total_time_enabled);
3877 *running += event->total_time_running +
3878 atomic64_read(&event->child_total_time_running);
3879
3880 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003881 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003882 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003883 *enabled += child->total_time_enabled;
3884 *running += child->total_time_running;
3885 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003886 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003887
3888 return total;
3889}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003890EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003891
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003892static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003893 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003894{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003895 struct perf_event *sub;
3896 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003897 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003898
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003899 ret = perf_event_read(leader, true);
3900 if (ret)
3901 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003902
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003903 /*
3904 * Since we co-schedule groups, {enabled,running} times of siblings
3905 * will be identical to those of the leader, so we only publish one
3906 * set.
3907 */
3908 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3909 values[n++] += leader->total_time_enabled +
3910 atomic64_read(&leader->child_total_time_enabled);
3911 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003912
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003913 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3914 values[n++] += leader->total_time_running +
3915 atomic64_read(&leader->child_total_time_running);
3916 }
3917
3918 /*
3919 * Write {count,id} tuples for every sibling.
3920 */
3921 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003922 if (read_format & PERF_FORMAT_ID)
3923 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003924
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003925 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003926 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003927 if (read_format & PERF_FORMAT_ID)
3928 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003929 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003930
3931 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003932}
3933
3934static int perf_read_group(struct perf_event *event,
3935 u64 read_format, char __user *buf)
3936{
3937 struct perf_event *leader = event->group_leader, *child;
3938 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003939 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003940 u64 *values;
3941
3942 lockdep_assert_held(&ctx->mutex);
3943
3944 values = kzalloc(event->read_size, GFP_KERNEL);
3945 if (!values)
3946 return -ENOMEM;
3947
3948 values[0] = 1 + leader->nr_siblings;
3949
3950 /*
3951 * By locking the child_mutex of the leader we effectively
3952 * lock the child list of all siblings.. XXX explain how.
3953 */
3954 mutex_lock(&leader->child_mutex);
3955
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003956 ret = __perf_read_group_add(leader, read_format, values);
3957 if (ret)
3958 goto unlock;
3959
3960 list_for_each_entry(child, &leader->child_list, child_list) {
3961 ret = __perf_read_group_add(child, read_format, values);
3962 if (ret)
3963 goto unlock;
3964 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003965
3966 mutex_unlock(&leader->child_mutex);
3967
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003968 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003969 if (copy_to_user(buf, values, event->read_size))
3970 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003971 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003972
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003973unlock:
3974 mutex_unlock(&leader->child_mutex);
3975out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003976 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003977 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003978}
3979
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07003980static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003981 u64 read_format, char __user *buf)
3982{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003983 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003984 u64 values[4];
3985 int n = 0;
3986
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003987 values[n++] = perf_event_read_value(event, &enabled, &running);
3988 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3989 values[n++] = enabled;
3990 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3991 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003992 if (read_format & PERF_FORMAT_ID)
3993 values[n++] = primary_event_id(event);
3994
3995 if (copy_to_user(buf, values, n * sizeof(u64)))
3996 return -EFAULT;
3997
3998 return n * sizeof(u64);
3999}
4000
Jiri Olsadc633982014-09-12 13:18:26 +02004001static bool is_event_hup(struct perf_event *event)
4002{
4003 bool no_children;
4004
4005 if (event->state != PERF_EVENT_STATE_EXIT)
4006 return false;
4007
4008 mutex_lock(&event->child_mutex);
4009 no_children = list_empty(&event->child_list);
4010 mutex_unlock(&event->child_mutex);
4011 return no_children;
4012}
4013
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004014/*
4015 * Read the performance event - simple non blocking version for now
4016 */
4017static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004018__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004019{
4020 u64 read_format = event->attr.read_format;
4021 int ret;
4022
4023 /*
4024 * Return end-of-file for a read on a event that is in
4025 * error state (i.e. because it was pinned but it couldn't be
4026 * scheduled on to the CPU at some point).
4027 */
4028 if (event->state == PERF_EVENT_STATE_ERROR)
4029 return 0;
4030
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004031 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004032 return -ENOSPC;
4033
4034 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004035 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004036 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004037 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004038 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004039
4040 return ret;
4041}
4042
4043static ssize_t
4044perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4045{
4046 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004047 struct perf_event_context *ctx;
4048 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004049
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004050 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004051 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004052 perf_event_ctx_unlock(event, ctx);
4053
4054 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004055}
4056
4057static unsigned int perf_poll(struct file *file, poll_table *wait)
4058{
4059 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004060 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004061 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004062
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004063 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004064
Jiri Olsadc633982014-09-12 13:18:26 +02004065 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004066 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004067
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004068 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004069 * Pin the event->rb by taking event->mmap_mutex; otherwise
4070 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004071 */
4072 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004073 rb = event->rb;
4074 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004075 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004076 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004077 return events;
4078}
4079
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004080static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004081{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004082 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004083 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004084 perf_event_update_userpage(event);
4085}
4086
4087/*
4088 * Holding the top-level event's child_mutex means that any
4089 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004090 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004091 * task existence requirements of perf_event_enable/disable.
4092 */
4093static void perf_event_for_each_child(struct perf_event *event,
4094 void (*func)(struct perf_event *))
4095{
4096 struct perf_event *child;
4097
4098 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004099
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004100 mutex_lock(&event->child_mutex);
4101 func(event);
4102 list_for_each_entry(child, &event->child_list, child_list)
4103 func(child);
4104 mutex_unlock(&event->child_mutex);
4105}
4106
4107static void perf_event_for_each(struct perf_event *event,
4108 void (*func)(struct perf_event *))
4109{
4110 struct perf_event_context *ctx = event->ctx;
4111 struct perf_event *sibling;
4112
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004113 lockdep_assert_held(&ctx->mutex);
4114
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004115 event = event->group_leader;
4116
4117 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004118 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004119 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004120}
4121
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004122static void __perf_event_period(struct perf_event *event,
4123 struct perf_cpu_context *cpuctx,
4124 struct perf_event_context *ctx,
4125 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004126{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004127 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004128 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004129
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004130 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004131 event->attr.sample_freq = value;
4132 } else {
4133 event->attr.sample_period = value;
4134 event->hw.sample_period = value;
4135 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004136
4137 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4138 if (active) {
4139 perf_pmu_disable(ctx->pmu);
4140 event->pmu->stop(event, PERF_EF_UPDATE);
4141 }
4142
4143 local64_set(&event->hw.period_left, 0);
4144
4145 if (active) {
4146 event->pmu->start(event, PERF_EF_RELOAD);
4147 perf_pmu_enable(ctx->pmu);
4148 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004149}
4150
4151static int perf_event_period(struct perf_event *event, u64 __user *arg)
4152{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004153 u64 value;
4154
4155 if (!is_sampling_event(event))
4156 return -EINVAL;
4157
4158 if (copy_from_user(&value, arg, sizeof(value)))
4159 return -EFAULT;
4160
4161 if (!value)
4162 return -EINVAL;
4163
4164 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4165 return -EINVAL;
4166
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004167 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004168
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004169 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004170}
4171
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004172static const struct file_operations perf_fops;
4173
Al Viro2903ff02012-08-28 12:52:22 -04004174static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004175{
Al Viro2903ff02012-08-28 12:52:22 -04004176 struct fd f = fdget(fd);
4177 if (!f.file)
4178 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004179
Al Viro2903ff02012-08-28 12:52:22 -04004180 if (f.file->f_op != &perf_fops) {
4181 fdput(f);
4182 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004183 }
Al Viro2903ff02012-08-28 12:52:22 -04004184 *p = f;
4185 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004186}
4187
4188static int perf_event_set_output(struct perf_event *event,
4189 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004190static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004191static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004192
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004193static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004194{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004195 void (*func)(struct perf_event *);
4196 u32 flags = arg;
4197
4198 switch (cmd) {
4199 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004200 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004201 break;
4202 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004203 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004204 break;
4205 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004206 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004207 break;
4208
4209 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004210 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004211
4212 case PERF_EVENT_IOC_PERIOD:
4213 return perf_event_period(event, (u64 __user *)arg);
4214
Jiri Olsacf4957f2012-10-24 13:37:58 +02004215 case PERF_EVENT_IOC_ID:
4216 {
4217 u64 id = primary_event_id(event);
4218
4219 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4220 return -EFAULT;
4221 return 0;
4222 }
4223
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004224 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004225 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004226 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004227 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004228 struct perf_event *output_event;
4229 struct fd output;
4230 ret = perf_fget_light(arg, &output);
4231 if (ret)
4232 return ret;
4233 output_event = output.file->private_data;
4234 ret = perf_event_set_output(event, output_event);
4235 fdput(output);
4236 } else {
4237 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004238 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004239 return ret;
4240 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004241
Li Zefan6fb29152009-10-15 11:21:42 +08004242 case PERF_EVENT_IOC_SET_FILTER:
4243 return perf_event_set_filter(event, (void __user *)arg);
4244
Alexei Starovoitov25415172015-03-25 12:49:20 -07004245 case PERF_EVENT_IOC_SET_BPF:
4246 return perf_event_set_bpf_prog(event, arg);
4247
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004248 default:
4249 return -ENOTTY;
4250 }
4251
4252 if (flags & PERF_IOC_FLAG_GROUP)
4253 perf_event_for_each(event, func);
4254 else
4255 perf_event_for_each_child(event, func);
4256
4257 return 0;
4258}
4259
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004260static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4261{
4262 struct perf_event *event = file->private_data;
4263 struct perf_event_context *ctx;
4264 long ret;
4265
4266 ctx = perf_event_ctx_lock(event);
4267 ret = _perf_ioctl(event, cmd, arg);
4268 perf_event_ctx_unlock(event, ctx);
4269
4270 return ret;
4271}
4272
Pawel Mollb3f20782014-06-13 16:03:32 +01004273#ifdef CONFIG_COMPAT
4274static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4275 unsigned long arg)
4276{
4277 switch (_IOC_NR(cmd)) {
4278 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4279 case _IOC_NR(PERF_EVENT_IOC_ID):
4280 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4281 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4282 cmd &= ~IOCSIZE_MASK;
4283 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4284 }
4285 break;
4286 }
4287 return perf_ioctl(file, cmd, arg);
4288}
4289#else
4290# define perf_compat_ioctl NULL
4291#endif
4292
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004293int perf_event_task_enable(void)
4294{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004295 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004296 struct perf_event *event;
4297
4298 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004299 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4300 ctx = perf_event_ctx_lock(event);
4301 perf_event_for_each_child(event, _perf_event_enable);
4302 perf_event_ctx_unlock(event, ctx);
4303 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004304 mutex_unlock(&current->perf_event_mutex);
4305
4306 return 0;
4307}
4308
4309int perf_event_task_disable(void)
4310{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004311 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004312 struct perf_event *event;
4313
4314 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004315 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4316 ctx = perf_event_ctx_lock(event);
4317 perf_event_for_each_child(event, _perf_event_disable);
4318 perf_event_ctx_unlock(event, ctx);
4319 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004320 mutex_unlock(&current->perf_event_mutex);
4321
4322 return 0;
4323}
4324
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004325static int perf_event_index(struct perf_event *event)
4326{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004327 if (event->hw.state & PERF_HES_STOPPED)
4328 return 0;
4329
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004330 if (event->state != PERF_EVENT_STATE_ACTIVE)
4331 return 0;
4332
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004333 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004334}
4335
Eric B Munsonc4794292011-06-23 16:34:38 -04004336static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004337 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004338 u64 *enabled,
4339 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004340{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004341 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004342
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004343 *now = perf_clock();
4344 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004345 *enabled = ctx_time - event->tstamp_enabled;
4346 *running = ctx_time - event->tstamp_running;
4347}
4348
Peter Zijlstrafa731582013-09-19 10:16:42 +02004349static void perf_event_init_userpage(struct perf_event *event)
4350{
4351 struct perf_event_mmap_page *userpg;
4352 struct ring_buffer *rb;
4353
4354 rcu_read_lock();
4355 rb = rcu_dereference(event->rb);
4356 if (!rb)
4357 goto unlock;
4358
4359 userpg = rb->user_page;
4360
4361 /* Allow new userspace to detect that bit 0 is deprecated */
4362 userpg->cap_bit0_is_deprecated = 1;
4363 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004364 userpg->data_offset = PAGE_SIZE;
4365 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004366
4367unlock:
4368 rcu_read_unlock();
4369}
4370
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004371void __weak arch_perf_update_userpage(
4372 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004373{
4374}
4375
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004376/*
4377 * Callers need to ensure there can be no nesting of this function, otherwise
4378 * the seqlock logic goes bad. We can not serialize this because the arch
4379 * code calls this from NMI context.
4380 */
4381void perf_event_update_userpage(struct perf_event *event)
4382{
4383 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004384 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004385 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004386
4387 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004388 rb = rcu_dereference(event->rb);
4389 if (!rb)
4390 goto unlock;
4391
Eric B Munson0d641202011-06-24 12:26:26 -04004392 /*
4393 * compute total_time_enabled, total_time_running
4394 * based on snapshot values taken when the event
4395 * was last scheduled in.
4396 *
4397 * we cannot simply called update_context_time()
4398 * because of locking issue as we can be called in
4399 * NMI context
4400 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004401 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004402
Frederic Weisbecker76369132011-05-19 19:55:04 +02004403 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004404 /*
4405 * Disable preemption so as to not let the corresponding user-space
4406 * spin too long if we get preempted.
4407 */
4408 preempt_disable();
4409 ++userpg->lock;
4410 barrier();
4411 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004412 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004413 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004414 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004415
Eric B Munson0d641202011-06-24 12:26:26 -04004416 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004417 atomic64_read(&event->child_total_time_enabled);
4418
Eric B Munson0d641202011-06-24 12:26:26 -04004419 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004420 atomic64_read(&event->child_total_time_running);
4421
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004422 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004423
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004424 barrier();
4425 ++userpg->lock;
4426 preempt_enable();
4427unlock:
4428 rcu_read_unlock();
4429}
4430
Peter Zijlstra906010b2009-09-21 16:08:49 +02004431static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4432{
4433 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004434 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004435 int ret = VM_FAULT_SIGBUS;
4436
4437 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4438 if (vmf->pgoff == 0)
4439 ret = 0;
4440 return ret;
4441 }
4442
4443 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004444 rb = rcu_dereference(event->rb);
4445 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004446 goto unlock;
4447
4448 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4449 goto unlock;
4450
Frederic Weisbecker76369132011-05-19 19:55:04 +02004451 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004452 if (!vmf->page)
4453 goto unlock;
4454
4455 get_page(vmf->page);
4456 vmf->page->mapping = vma->vm_file->f_mapping;
4457 vmf->page->index = vmf->pgoff;
4458
4459 ret = 0;
4460unlock:
4461 rcu_read_unlock();
4462
4463 return ret;
4464}
4465
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004466static void ring_buffer_attach(struct perf_event *event,
4467 struct ring_buffer *rb)
4468{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004469 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004470 unsigned long flags;
4471
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004472 if (event->rb) {
4473 /*
4474 * Should be impossible, we set this when removing
4475 * event->rb_entry and wait/clear when adding event->rb_entry.
4476 */
4477 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004478
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004479 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004480 spin_lock_irqsave(&old_rb->event_lock, flags);
4481 list_del_rcu(&event->rb_entry);
4482 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004483
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004484 event->rcu_batches = get_state_synchronize_rcu();
4485 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004486 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004487
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004488 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004489 if (event->rcu_pending) {
4490 cond_synchronize_rcu(event->rcu_batches);
4491 event->rcu_pending = 0;
4492 }
4493
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004494 spin_lock_irqsave(&rb->event_lock, flags);
4495 list_add_rcu(&event->rb_entry, &rb->event_list);
4496 spin_unlock_irqrestore(&rb->event_lock, flags);
4497 }
4498
4499 rcu_assign_pointer(event->rb, rb);
4500
4501 if (old_rb) {
4502 ring_buffer_put(old_rb);
4503 /*
4504 * Since we detached before setting the new rb, so that we
4505 * could attach the new rb, we could have missed a wakeup.
4506 * Provide it now.
4507 */
4508 wake_up_all(&event->waitq);
4509 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004510}
4511
4512static void ring_buffer_wakeup(struct perf_event *event)
4513{
4514 struct ring_buffer *rb;
4515
4516 rcu_read_lock();
4517 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004518 if (rb) {
4519 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4520 wake_up_all(&event->waitq);
4521 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004522 rcu_read_unlock();
4523}
4524
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004525struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004526{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004527 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004528
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004529 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004530 rb = rcu_dereference(event->rb);
4531 if (rb) {
4532 if (!atomic_inc_not_zero(&rb->refcount))
4533 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004534 }
4535 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004536
Frederic Weisbecker76369132011-05-19 19:55:04 +02004537 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004538}
4539
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004540void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004541{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004542 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004543 return;
4544
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004545 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004546
Frederic Weisbecker76369132011-05-19 19:55:04 +02004547 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004548}
4549
4550static void perf_mmap_open(struct vm_area_struct *vma)
4551{
4552 struct perf_event *event = vma->vm_file->private_data;
4553
4554 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004555 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004556
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004557 if (vma->vm_pgoff)
4558 atomic_inc(&event->rb->aux_mmap_count);
4559
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004560 if (event->pmu->event_mapped)
4561 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004562}
4563
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004564/*
4565 * A buffer can be mmap()ed multiple times; either directly through the same
4566 * event, or through other events by use of perf_event_set_output().
4567 *
4568 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4569 * the buffer here, where we still have a VM context. This means we need
4570 * to detach all events redirecting to us.
4571 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004572static void perf_mmap_close(struct vm_area_struct *vma)
4573{
4574 struct perf_event *event = vma->vm_file->private_data;
4575
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004576 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004577 struct user_struct *mmap_user = rb->mmap_user;
4578 int mmap_locked = rb->mmap_locked;
4579 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004580
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004581 if (event->pmu->event_unmapped)
4582 event->pmu->event_unmapped(event);
4583
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004584 /*
4585 * rb->aux_mmap_count will always drop before rb->mmap_count and
4586 * event->mmap_count, so it is ok to use event->mmap_mutex to
4587 * serialize with perf_mmap here.
4588 */
4589 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
4590 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
4591 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
4592 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
4593
4594 rb_free_aux(rb);
4595 mutex_unlock(&event->mmap_mutex);
4596 }
4597
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004598 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004599
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004600 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004601 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004602
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004603 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004604 mutex_unlock(&event->mmap_mutex);
4605
4606 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004607 if (atomic_read(&rb->mmap_count))
4608 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004609
4610 /*
4611 * No other mmap()s, detach from all other events that might redirect
4612 * into the now unreachable buffer. Somewhat complicated by the
4613 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4614 */
4615again:
4616 rcu_read_lock();
4617 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4618 if (!atomic_long_inc_not_zero(&event->refcount)) {
4619 /*
4620 * This event is en-route to free_event() which will
4621 * detach it and remove it from the list.
4622 */
4623 continue;
4624 }
4625 rcu_read_unlock();
4626
4627 mutex_lock(&event->mmap_mutex);
4628 /*
4629 * Check we didn't race with perf_event_set_output() which can
4630 * swizzle the rb from under us while we were waiting to
4631 * acquire mmap_mutex.
4632 *
4633 * If we find a different rb; ignore this event, a next
4634 * iteration will no longer find it on the list. We have to
4635 * still restart the iteration to make sure we're not now
4636 * iterating the wrong list.
4637 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004638 if (event->rb == rb)
4639 ring_buffer_attach(event, NULL);
4640
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004641 mutex_unlock(&event->mmap_mutex);
4642 put_event(event);
4643
4644 /*
4645 * Restart the iteration; either we're on the wrong list or
4646 * destroyed its integrity by doing a deletion.
4647 */
4648 goto again;
4649 }
4650 rcu_read_unlock();
4651
4652 /*
4653 * It could be there's still a few 0-ref events on the list; they'll
4654 * get cleaned up by free_event() -- they'll also still have their
4655 * ref on the rb and will free it whenever they are done with it.
4656 *
4657 * Aside from that, this buffer is 'fully' detached and unmapped,
4658 * undo the VM accounting.
4659 */
4660
4661 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4662 vma->vm_mm->pinned_vm -= mmap_locked;
4663 free_uid(mmap_user);
4664
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004665out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004666 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004667}
4668
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004669static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004670 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004671 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004672 .fault = perf_mmap_fault,
4673 .page_mkwrite = perf_mmap_fault,
4674};
4675
4676static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4677{
4678 struct perf_event *event = file->private_data;
4679 unsigned long user_locked, user_lock_limit;
4680 struct user_struct *user = current_user();
4681 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004682 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004683 unsigned long vma_size;
4684 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004685 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004686 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004687
Peter Zijlstrac7920612010-05-18 10:33:24 +02004688 /*
4689 * Don't allow mmap() of inherited per-task counters. This would
4690 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004691 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004692 */
4693 if (event->cpu == -1 && event->attr.inherit)
4694 return -EINVAL;
4695
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004696 if (!(vma->vm_flags & VM_SHARED))
4697 return -EINVAL;
4698
4699 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004700
4701 if (vma->vm_pgoff == 0) {
4702 nr_pages = (vma_size / PAGE_SIZE) - 1;
4703 } else {
4704 /*
4705 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
4706 * mapped, all subsequent mappings should have the same size
4707 * and offset. Must be above the normal perf buffer.
4708 */
4709 u64 aux_offset, aux_size;
4710
4711 if (!event->rb)
4712 return -EINVAL;
4713
4714 nr_pages = vma_size / PAGE_SIZE;
4715
4716 mutex_lock(&event->mmap_mutex);
4717 ret = -EINVAL;
4718
4719 rb = event->rb;
4720 if (!rb)
4721 goto aux_unlock;
4722
4723 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
4724 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
4725
4726 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
4727 goto aux_unlock;
4728
4729 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
4730 goto aux_unlock;
4731
4732 /* already mapped with a different offset */
4733 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
4734 goto aux_unlock;
4735
4736 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
4737 goto aux_unlock;
4738
4739 /* already mapped with a different size */
4740 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
4741 goto aux_unlock;
4742
4743 if (!is_power_of_2(nr_pages))
4744 goto aux_unlock;
4745
4746 if (!atomic_inc_not_zero(&rb->mmap_count))
4747 goto aux_unlock;
4748
4749 if (rb_has_aux(rb)) {
4750 atomic_inc(&rb->aux_mmap_count);
4751 ret = 0;
4752 goto unlock;
4753 }
4754
4755 atomic_set(&rb->aux_mmap_count, 1);
4756 user_extra = nr_pages;
4757
4758 goto accounting;
4759 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004760
4761 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004762 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004763 * can do bitmasks instead of modulo.
4764 */
Kan Liang2ed11312015-03-02 02:14:26 -05004765 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004766 return -EINVAL;
4767
4768 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4769 return -EINVAL;
4770
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004771 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004772again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004773 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004774 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004775 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004776 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004777 goto unlock;
4778 }
4779
4780 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4781 /*
4782 * Raced against perf_mmap_close() through
4783 * perf_event_set_output(). Try again, hope for better
4784 * luck.
4785 */
4786 mutex_unlock(&event->mmap_mutex);
4787 goto again;
4788 }
4789
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004790 goto unlock;
4791 }
4792
4793 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004794
4795accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004796 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4797
4798 /*
4799 * Increase the limit linearly with more CPUs:
4800 */
4801 user_lock_limit *= num_online_cpus();
4802
4803 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4804
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004805 if (user_locked > user_lock_limit)
4806 extra = user_locked - user_lock_limit;
4807
Jiri Slaby78d7d402010-03-05 13:42:54 -08004808 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004809 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004810 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004811
4812 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4813 !capable(CAP_IPC_LOCK)) {
4814 ret = -EPERM;
4815 goto unlock;
4816 }
4817
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004818 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004819
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004820 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004821 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004822
Frederic Weisbecker76369132011-05-19 19:55:04 +02004823 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004824 rb = rb_alloc(nr_pages,
4825 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4826 event->cpu, flags);
4827
4828 if (!rb) {
4829 ret = -ENOMEM;
4830 goto unlock;
4831 }
4832
4833 atomic_set(&rb->mmap_count, 1);
4834 rb->mmap_user = get_current_user();
4835 rb->mmap_locked = extra;
4836
4837 ring_buffer_attach(event, rb);
4838
4839 perf_event_init_userpage(event);
4840 perf_event_update_userpage(event);
4841 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02004842 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
4843 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004844 if (!ret)
4845 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004846 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004847
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004848unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004849 if (!ret) {
4850 atomic_long_add(user_extra, &user->locked_vm);
4851 vma->vm_mm->pinned_vm += extra;
4852
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004853 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004854 } else if (rb) {
4855 atomic_dec(&rb->mmap_count);
4856 }
4857aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004858 mutex_unlock(&event->mmap_mutex);
4859
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004860 /*
4861 * Since pinned accounting is per vm we cannot allow fork() to copy our
4862 * vma.
4863 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004864 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004865 vma->vm_ops = &perf_mmap_vmops;
4866
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004867 if (event->pmu->event_mapped)
4868 event->pmu->event_mapped(event);
4869
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004870 return ret;
4871}
4872
4873static int perf_fasync(int fd, struct file *filp, int on)
4874{
Al Viro496ad9a2013-01-23 17:07:38 -05004875 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004876 struct perf_event *event = filp->private_data;
4877 int retval;
4878
4879 mutex_lock(&inode->i_mutex);
4880 retval = fasync_helper(fd, filp, on, &event->fasync);
4881 mutex_unlock(&inode->i_mutex);
4882
4883 if (retval < 0)
4884 return retval;
4885
4886 return 0;
4887}
4888
4889static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004890 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004891 .release = perf_release,
4892 .read = perf_read,
4893 .poll = perf_poll,
4894 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01004895 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004896 .mmap = perf_mmap,
4897 .fasync = perf_fasync,
4898};
4899
4900/*
4901 * Perf event wakeup
4902 *
4903 * If there's data, ensure we set the poll() state and publish everything
4904 * to user-space before waking everybody up.
4905 */
4906
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02004907static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
4908{
4909 /* only the parent has fasync state */
4910 if (event->parent)
4911 event = event->parent;
4912 return &event->fasync;
4913}
4914
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004915void perf_event_wakeup(struct perf_event *event)
4916{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004917 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004918
4919 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02004920 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004921 event->pending_kill = 0;
4922 }
4923}
4924
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004925static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004926{
4927 struct perf_event *event = container_of(entry,
4928 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01004929 int rctx;
4930
4931 rctx = perf_swevent_get_recursion_context();
4932 /*
4933 * If we 'fail' here, that's OK, it means recursion is already disabled
4934 * and we won't recurse 'further'.
4935 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004936
4937 if (event->pending_disable) {
4938 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004939 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004940 }
4941
4942 if (event->pending_wakeup) {
4943 event->pending_wakeup = 0;
4944 perf_event_wakeup(event);
4945 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01004946
4947 if (rctx >= 0)
4948 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004949}
4950
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004951/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004952 * We assume there is only KVM supporting the callbacks.
4953 * Later on, we might change it to a list if there is
4954 * another virtualization implementation supporting the callbacks.
4955 */
4956struct perf_guest_info_callbacks *perf_guest_cbs;
4957
4958int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4959{
4960 perf_guest_cbs = cbs;
4961 return 0;
4962}
4963EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4964
4965int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4966{
4967 perf_guest_cbs = NULL;
4968 return 0;
4969}
4970EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4971
Jiri Olsa40189942012-08-07 15:20:37 +02004972static void
4973perf_output_sample_regs(struct perf_output_handle *handle,
4974 struct pt_regs *regs, u64 mask)
4975{
4976 int bit;
4977
4978 for_each_set_bit(bit, (const unsigned long *) &mask,
4979 sizeof(mask) * BITS_PER_BYTE) {
4980 u64 val;
4981
4982 val = perf_reg_value(regs, bit);
4983 perf_output_put(handle, val);
4984 }
4985}
4986
Stephane Eranian60e23642014-09-24 13:48:37 +02004987static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004988 struct pt_regs *regs,
4989 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02004990{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004991 if (user_mode(regs)) {
4992 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02004993 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004994 } else if (current->mm) {
4995 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02004996 } else {
4997 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
4998 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02004999 }
5000}
5001
Stephane Eranian60e23642014-09-24 13:48:37 +02005002static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5003 struct pt_regs *regs)
5004{
5005 regs_intr->regs = regs;
5006 regs_intr->abi = perf_reg_abi(current);
5007}
5008
5009
Jiri Olsac5ebced2012-08-07 15:20:40 +02005010/*
5011 * Get remaining task size from user stack pointer.
5012 *
5013 * It'd be better to take stack vma map and limit this more
5014 * precisly, but there's no way to get it safely under interrupt,
5015 * so using TASK_SIZE as limit.
5016 */
5017static u64 perf_ustack_task_size(struct pt_regs *regs)
5018{
5019 unsigned long addr = perf_user_stack_pointer(regs);
5020
5021 if (!addr || addr >= TASK_SIZE)
5022 return 0;
5023
5024 return TASK_SIZE - addr;
5025}
5026
5027static u16
5028perf_sample_ustack_size(u16 stack_size, u16 header_size,
5029 struct pt_regs *regs)
5030{
5031 u64 task_size;
5032
5033 /* No regs, no stack pointer, no dump. */
5034 if (!regs)
5035 return 0;
5036
5037 /*
5038 * Check if we fit in with the requested stack size into the:
5039 * - TASK_SIZE
5040 * If we don't, we limit the size to the TASK_SIZE.
5041 *
5042 * - remaining sample size
5043 * If we don't, we customize the stack size to
5044 * fit in to the remaining sample size.
5045 */
5046
5047 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5048 stack_size = min(stack_size, (u16) task_size);
5049
5050 /* Current header size plus static size and dynamic size. */
5051 header_size += 2 * sizeof(u64);
5052
5053 /* Do we fit in with the current stack dump size? */
5054 if ((u16) (header_size + stack_size) < header_size) {
5055 /*
5056 * If we overflow the maximum size for the sample,
5057 * we customize the stack dump size to fit in.
5058 */
5059 stack_size = USHRT_MAX - header_size - sizeof(u64);
5060 stack_size = round_up(stack_size, sizeof(u64));
5061 }
5062
5063 return stack_size;
5064}
5065
5066static void
5067perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5068 struct pt_regs *regs)
5069{
5070 /* Case of a kernel thread, nothing to dump */
5071 if (!regs) {
5072 u64 size = 0;
5073 perf_output_put(handle, size);
5074 } else {
5075 unsigned long sp;
5076 unsigned int rem;
5077 u64 dyn_size;
5078
5079 /*
5080 * We dump:
5081 * static size
5082 * - the size requested by user or the best one we can fit
5083 * in to the sample max size
5084 * data
5085 * - user stack dump data
5086 * dynamic size
5087 * - the actual dumped size
5088 */
5089
5090 /* Static size. */
5091 perf_output_put(handle, dump_size);
5092
5093 /* Data. */
5094 sp = perf_user_stack_pointer(regs);
5095 rem = __output_copy_user(handle, (void *) sp, dump_size);
5096 dyn_size = dump_size - rem;
5097
5098 perf_output_skip(handle, rem);
5099
5100 /* Dynamic size. */
5101 perf_output_put(handle, dyn_size);
5102 }
5103}
5104
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005105static void __perf_event_header__init_id(struct perf_event_header *header,
5106 struct perf_sample_data *data,
5107 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005108{
5109 u64 sample_type = event->attr.sample_type;
5110
5111 data->type = sample_type;
5112 header->size += event->id_header_size;
5113
5114 if (sample_type & PERF_SAMPLE_TID) {
5115 /* namespace issues */
5116 data->tid_entry.pid = perf_event_pid(event, current);
5117 data->tid_entry.tid = perf_event_tid(event, current);
5118 }
5119
5120 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005121 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005122
Adrian Hunterff3d5272013-08-27 11:23:07 +03005123 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005124 data->id = primary_event_id(event);
5125
5126 if (sample_type & PERF_SAMPLE_STREAM_ID)
5127 data->stream_id = event->id;
5128
5129 if (sample_type & PERF_SAMPLE_CPU) {
5130 data->cpu_entry.cpu = raw_smp_processor_id();
5131 data->cpu_entry.reserved = 0;
5132 }
5133}
5134
Frederic Weisbecker76369132011-05-19 19:55:04 +02005135void perf_event_header__init_id(struct perf_event_header *header,
5136 struct perf_sample_data *data,
5137 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005138{
5139 if (event->attr.sample_id_all)
5140 __perf_event_header__init_id(header, data, event);
5141}
5142
5143static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5144 struct perf_sample_data *data)
5145{
5146 u64 sample_type = data->type;
5147
5148 if (sample_type & PERF_SAMPLE_TID)
5149 perf_output_put(handle, data->tid_entry);
5150
5151 if (sample_type & PERF_SAMPLE_TIME)
5152 perf_output_put(handle, data->time);
5153
5154 if (sample_type & PERF_SAMPLE_ID)
5155 perf_output_put(handle, data->id);
5156
5157 if (sample_type & PERF_SAMPLE_STREAM_ID)
5158 perf_output_put(handle, data->stream_id);
5159
5160 if (sample_type & PERF_SAMPLE_CPU)
5161 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005162
5163 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5164 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005165}
5166
Frederic Weisbecker76369132011-05-19 19:55:04 +02005167void perf_event__output_id_sample(struct perf_event *event,
5168 struct perf_output_handle *handle,
5169 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005170{
5171 if (event->attr.sample_id_all)
5172 __perf_event__output_id_sample(handle, sample);
5173}
5174
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005175static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005176 struct perf_event *event,
5177 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005178{
5179 u64 read_format = event->attr.read_format;
5180 u64 values[4];
5181 int n = 0;
5182
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005183 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005184 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005185 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005186 atomic64_read(&event->child_total_time_enabled);
5187 }
5188 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005189 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005190 atomic64_read(&event->child_total_time_running);
5191 }
5192 if (read_format & PERF_FORMAT_ID)
5193 values[n++] = primary_event_id(event);
5194
Frederic Weisbecker76369132011-05-19 19:55:04 +02005195 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005196}
5197
5198/*
5199 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
5200 */
5201static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005202 struct perf_event *event,
5203 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005204{
5205 struct perf_event *leader = event->group_leader, *sub;
5206 u64 read_format = event->attr.read_format;
5207 u64 values[5];
5208 int n = 0;
5209
5210 values[n++] = 1 + leader->nr_siblings;
5211
5212 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005213 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005214
5215 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005216 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005217
5218 if (leader != event)
5219 leader->pmu->read(leader);
5220
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005221 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005222 if (read_format & PERF_FORMAT_ID)
5223 values[n++] = primary_event_id(leader);
5224
Frederic Weisbecker76369132011-05-19 19:55:04 +02005225 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005226
5227 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5228 n = 0;
5229
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005230 if ((sub != event) &&
5231 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005232 sub->pmu->read(sub);
5233
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005234 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005235 if (read_format & PERF_FORMAT_ID)
5236 values[n++] = primary_event_id(sub);
5237
Frederic Weisbecker76369132011-05-19 19:55:04 +02005238 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005239 }
5240}
5241
Stephane Eranianeed01522010-10-26 16:08:01 +02005242#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5243 PERF_FORMAT_TOTAL_TIME_RUNNING)
5244
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005245static void perf_output_read(struct perf_output_handle *handle,
5246 struct perf_event *event)
5247{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005248 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005249 u64 read_format = event->attr.read_format;
5250
5251 /*
5252 * compute total_time_enabled, total_time_running
5253 * based on snapshot values taken when the event
5254 * was last scheduled in.
5255 *
5256 * we cannot simply called update_context_time()
5257 * because of locking issue as we are called in
5258 * NMI context
5259 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005260 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005261 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005262
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005263 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005264 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005265 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005266 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005267}
5268
5269void perf_output_sample(struct perf_output_handle *handle,
5270 struct perf_event_header *header,
5271 struct perf_sample_data *data,
5272 struct perf_event *event)
5273{
5274 u64 sample_type = data->type;
5275
5276 perf_output_put(handle, *header);
5277
Adrian Hunterff3d5272013-08-27 11:23:07 +03005278 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5279 perf_output_put(handle, data->id);
5280
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005281 if (sample_type & PERF_SAMPLE_IP)
5282 perf_output_put(handle, data->ip);
5283
5284 if (sample_type & PERF_SAMPLE_TID)
5285 perf_output_put(handle, data->tid_entry);
5286
5287 if (sample_type & PERF_SAMPLE_TIME)
5288 perf_output_put(handle, data->time);
5289
5290 if (sample_type & PERF_SAMPLE_ADDR)
5291 perf_output_put(handle, data->addr);
5292
5293 if (sample_type & PERF_SAMPLE_ID)
5294 perf_output_put(handle, data->id);
5295
5296 if (sample_type & PERF_SAMPLE_STREAM_ID)
5297 perf_output_put(handle, data->stream_id);
5298
5299 if (sample_type & PERF_SAMPLE_CPU)
5300 perf_output_put(handle, data->cpu_entry);
5301
5302 if (sample_type & PERF_SAMPLE_PERIOD)
5303 perf_output_put(handle, data->period);
5304
5305 if (sample_type & PERF_SAMPLE_READ)
5306 perf_output_read(handle, event);
5307
5308 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5309 if (data->callchain) {
5310 int size = 1;
5311
5312 if (data->callchain)
5313 size += data->callchain->nr;
5314
5315 size *= sizeof(u64);
5316
Frederic Weisbecker76369132011-05-19 19:55:04 +02005317 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005318 } else {
5319 u64 nr = 0;
5320 perf_output_put(handle, nr);
5321 }
5322 }
5323
5324 if (sample_type & PERF_SAMPLE_RAW) {
5325 if (data->raw) {
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005326 u32 raw_size = data->raw->size;
5327 u32 real_size = round_up(raw_size + sizeof(u32),
5328 sizeof(u64)) - sizeof(u32);
5329 u64 zero = 0;
5330
5331 perf_output_put(handle, real_size);
5332 __output_copy(handle, data->raw->data, raw_size);
5333 if (real_size - raw_size)
5334 __output_copy(handle, &zero, real_size - raw_size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005335 } else {
5336 struct {
5337 u32 size;
5338 u32 data;
5339 } raw = {
5340 .size = sizeof(u32),
5341 .data = 0,
5342 };
5343 perf_output_put(handle, raw);
5344 }
5345 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005346
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005347 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5348 if (data->br_stack) {
5349 size_t size;
5350
5351 size = data->br_stack->nr
5352 * sizeof(struct perf_branch_entry);
5353
5354 perf_output_put(handle, data->br_stack->nr);
5355 perf_output_copy(handle, data->br_stack->entries, size);
5356 } else {
5357 /*
5358 * we always store at least the value of nr
5359 */
5360 u64 nr = 0;
5361 perf_output_put(handle, nr);
5362 }
5363 }
Jiri Olsa40189942012-08-07 15:20:37 +02005364
5365 if (sample_type & PERF_SAMPLE_REGS_USER) {
5366 u64 abi = data->regs_user.abi;
5367
5368 /*
5369 * If there are no regs to dump, notice it through
5370 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5371 */
5372 perf_output_put(handle, abi);
5373
5374 if (abi) {
5375 u64 mask = event->attr.sample_regs_user;
5376 perf_output_sample_regs(handle,
5377 data->regs_user.regs,
5378 mask);
5379 }
5380 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005381
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005382 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005383 perf_output_sample_ustack(handle,
5384 data->stack_user_size,
5385 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005386 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005387
5388 if (sample_type & PERF_SAMPLE_WEIGHT)
5389 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005390
5391 if (sample_type & PERF_SAMPLE_DATA_SRC)
5392 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005393
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005394 if (sample_type & PERF_SAMPLE_TRANSACTION)
5395 perf_output_put(handle, data->txn);
5396
Stephane Eranian60e23642014-09-24 13:48:37 +02005397 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5398 u64 abi = data->regs_intr.abi;
5399 /*
5400 * If there are no regs to dump, notice it through
5401 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5402 */
5403 perf_output_put(handle, abi);
5404
5405 if (abi) {
5406 u64 mask = event->attr.sample_regs_intr;
5407
5408 perf_output_sample_regs(handle,
5409 data->regs_intr.regs,
5410 mask);
5411 }
5412 }
5413
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005414 if (!event->attr.watermark) {
5415 int wakeup_events = event->attr.wakeup_events;
5416
5417 if (wakeup_events) {
5418 struct ring_buffer *rb = handle->rb;
5419 int events = local_inc_return(&rb->events);
5420
5421 if (events >= wakeup_events) {
5422 local_sub(wakeup_events, &rb->events);
5423 local_inc(&rb->wakeup);
5424 }
5425 }
5426 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005427}
5428
5429void perf_prepare_sample(struct perf_event_header *header,
5430 struct perf_sample_data *data,
5431 struct perf_event *event,
5432 struct pt_regs *regs)
5433{
5434 u64 sample_type = event->attr.sample_type;
5435
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005436 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005437 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005438
5439 header->misc = 0;
5440 header->misc |= perf_misc_flags(regs);
5441
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005442 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005443
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005444 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005445 data->ip = perf_instruction_pointer(regs);
5446
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005447 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5448 int size = 1;
5449
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005450 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005451
5452 if (data->callchain)
5453 size += data->callchain->nr;
5454
5455 header->size += size * sizeof(u64);
5456 }
5457
5458 if (sample_type & PERF_SAMPLE_RAW) {
5459 int size = sizeof(u32);
5460
5461 if (data->raw)
5462 size += data->raw->size;
5463 else
5464 size += sizeof(u32);
5465
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005466 header->size += round_up(size, sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005467 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005468
5469 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5470 int size = sizeof(u64); /* nr */
5471 if (data->br_stack) {
5472 size += data->br_stack->nr
5473 * sizeof(struct perf_branch_entry);
5474 }
5475 header->size += size;
5476 }
Jiri Olsa40189942012-08-07 15:20:37 +02005477
Peter Zijlstra25657112014-09-24 13:48:42 +02005478 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005479 perf_sample_regs_user(&data->regs_user, regs,
5480 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005481
Jiri Olsa40189942012-08-07 15:20:37 +02005482 if (sample_type & PERF_SAMPLE_REGS_USER) {
5483 /* regs dump ABI info */
5484 int size = sizeof(u64);
5485
Jiri Olsa40189942012-08-07 15:20:37 +02005486 if (data->regs_user.regs) {
5487 u64 mask = event->attr.sample_regs_user;
5488 size += hweight64(mask) * sizeof(u64);
5489 }
5490
5491 header->size += size;
5492 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005493
5494 if (sample_type & PERF_SAMPLE_STACK_USER) {
5495 /*
5496 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5497 * processed as the last one or have additional check added
5498 * in case new sample type is added, because we could eat
5499 * up the rest of the sample size.
5500 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005501 u16 stack_size = event->attr.sample_stack_user;
5502 u16 size = sizeof(u64);
5503
Jiri Olsac5ebced2012-08-07 15:20:40 +02005504 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005505 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005506
5507 /*
5508 * If there is something to dump, add space for the dump
5509 * itself and for the field that tells the dynamic size,
5510 * which is how many have been actually dumped.
5511 */
5512 if (stack_size)
5513 size += sizeof(u64) + stack_size;
5514
5515 data->stack_user_size = stack_size;
5516 header->size += size;
5517 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005518
5519 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5520 /* regs dump ABI info */
5521 int size = sizeof(u64);
5522
5523 perf_sample_regs_intr(&data->regs_intr, regs);
5524
5525 if (data->regs_intr.regs) {
5526 u64 mask = event->attr.sample_regs_intr;
5527
5528 size += hweight64(mask) * sizeof(u64);
5529 }
5530
5531 header->size += size;
5532 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005533}
5534
Yan, Zheng21509082015-05-06 15:33:49 -04005535void perf_event_output(struct perf_event *event,
5536 struct perf_sample_data *data,
5537 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005538{
5539 struct perf_output_handle handle;
5540 struct perf_event_header header;
5541
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005542 /* protect the callchain buffers */
5543 rcu_read_lock();
5544
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005545 perf_prepare_sample(&header, data, event, regs);
5546
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005547 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005548 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005549
5550 perf_output_sample(&handle, &header, data, event);
5551
5552 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005553
5554exit:
5555 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005556}
5557
5558/*
5559 * read event_id
5560 */
5561
5562struct perf_read_event {
5563 struct perf_event_header header;
5564
5565 u32 pid;
5566 u32 tid;
5567};
5568
5569static void
5570perf_event_read_event(struct perf_event *event,
5571 struct task_struct *task)
5572{
5573 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005574 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005575 struct perf_read_event read_event = {
5576 .header = {
5577 .type = PERF_RECORD_READ,
5578 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005579 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005580 },
5581 .pid = perf_event_pid(event, task),
5582 .tid = perf_event_tid(event, task),
5583 };
5584 int ret;
5585
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005586 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005587 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005588 if (ret)
5589 return;
5590
5591 perf_output_put(&handle, read_event);
5592 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005593 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005594
5595 perf_output_end(&handle);
5596}
5597
Jiri Olsa52d857a2013-05-06 18:27:18 +02005598typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5599
5600static void
5601perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005602 perf_event_aux_output_cb output,
5603 void *data)
5604{
5605 struct perf_event *event;
5606
5607 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5608 if (event->state < PERF_EVENT_STATE_INACTIVE)
5609 continue;
5610 if (!event_filter_match(event))
5611 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02005612 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005613 }
5614}
5615
5616static void
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005617perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data,
5618 struct perf_event_context *task_ctx)
5619{
5620 rcu_read_lock();
5621 preempt_disable();
5622 perf_event_aux_ctx(task_ctx, output, data);
5623 preempt_enable();
5624 rcu_read_unlock();
5625}
5626
5627static void
Jiri Olsa67516842013-07-09 18:56:31 +02005628perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005629 struct perf_event_context *task_ctx)
5630{
5631 struct perf_cpu_context *cpuctx;
5632 struct perf_event_context *ctx;
5633 struct pmu *pmu;
5634 int ctxn;
5635
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005636 /*
5637 * If we have task_ctx != NULL we only notify
5638 * the task context itself. The task_ctx is set
5639 * only for EXIT events before releasing task
5640 * context.
5641 */
5642 if (task_ctx) {
5643 perf_event_aux_task_ctx(output, data, task_ctx);
5644 return;
5645 }
5646
Jiri Olsa52d857a2013-05-06 18:27:18 +02005647 rcu_read_lock();
5648 list_for_each_entry_rcu(pmu, &pmus, entry) {
5649 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5650 if (cpuctx->unique_pmu != pmu)
5651 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02005652 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005653 ctxn = pmu->task_ctx_nr;
5654 if (ctxn < 0)
5655 goto next;
5656 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5657 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02005658 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005659next:
5660 put_cpu_ptr(pmu->pmu_cpu_context);
5661 }
Jiri Olsa52d857a2013-05-06 18:27:18 +02005662 rcu_read_unlock();
5663}
5664
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005665/*
5666 * task tracking -- fork/exit
5667 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02005668 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005669 */
5670
5671struct perf_task_event {
5672 struct task_struct *task;
5673 struct perf_event_context *task_ctx;
5674
5675 struct {
5676 struct perf_event_header header;
5677
5678 u32 pid;
5679 u32 ppid;
5680 u32 tid;
5681 u32 ptid;
5682 u64 time;
5683 } event_id;
5684};
5685
Jiri Olsa67516842013-07-09 18:56:31 +02005686static int perf_event_task_match(struct perf_event *event)
5687{
Stephane Eranian13d7a242013-08-21 12:10:24 +02005688 return event->attr.comm || event->attr.mmap ||
5689 event->attr.mmap2 || event->attr.mmap_data ||
5690 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02005691}
5692
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005693static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005694 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005695{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005696 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005697 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005698 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005699 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005700 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01005701
Jiri Olsa67516842013-07-09 18:56:31 +02005702 if (!perf_event_task_match(event))
5703 return;
5704
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005705 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005706
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005707 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005708 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02005709 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005710 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005711
5712 task_event->event_id.pid = perf_event_pid(event, task);
5713 task_event->event_id.ppid = perf_event_pid(event, current);
5714
5715 task_event->event_id.tid = perf_event_tid(event, task);
5716 task_event->event_id.ptid = perf_event_tid(event, current);
5717
Peter Zijlstra34f43922015-02-20 14:05:38 +01005718 task_event->event_id.time = perf_event_clock(event);
5719
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005720 perf_output_put(&handle, task_event->event_id);
5721
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005722 perf_event__output_id_sample(event, &handle, &sample);
5723
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005724 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005725out:
5726 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005727}
5728
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005729static void perf_event_task(struct task_struct *task,
5730 struct perf_event_context *task_ctx,
5731 int new)
5732{
5733 struct perf_task_event task_event;
5734
5735 if (!atomic_read(&nr_comm_events) &&
5736 !atomic_read(&nr_mmap_events) &&
5737 !atomic_read(&nr_task_events))
5738 return;
5739
5740 task_event = (struct perf_task_event){
5741 .task = task,
5742 .task_ctx = task_ctx,
5743 .event_id = {
5744 .header = {
5745 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5746 .misc = 0,
5747 .size = sizeof(task_event.event_id),
5748 },
5749 /* .pid */
5750 /* .ppid */
5751 /* .tid */
5752 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01005753 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005754 },
5755 };
5756
Jiri Olsa67516842013-07-09 18:56:31 +02005757 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005758 &task_event,
5759 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005760}
5761
5762void perf_event_fork(struct task_struct *task)
5763{
5764 perf_event_task(task, NULL, 1);
5765}
5766
5767/*
5768 * comm tracking
5769 */
5770
5771struct perf_comm_event {
5772 struct task_struct *task;
5773 char *comm;
5774 int comm_size;
5775
5776 struct {
5777 struct perf_event_header header;
5778
5779 u32 pid;
5780 u32 tid;
5781 } event_id;
5782};
5783
Jiri Olsa67516842013-07-09 18:56:31 +02005784static int perf_event_comm_match(struct perf_event *event)
5785{
5786 return event->attr.comm;
5787}
5788
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005789static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005790 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005791{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005792 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005793 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005794 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005795 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005796 int ret;
5797
Jiri Olsa67516842013-07-09 18:56:31 +02005798 if (!perf_event_comm_match(event))
5799 return;
5800
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005801 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5802 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005803 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005804
5805 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005806 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005807
5808 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5809 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5810
5811 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005812 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005813 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005814
5815 perf_event__output_id_sample(event, &handle, &sample);
5816
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005817 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005818out:
5819 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005820}
5821
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005822static void perf_event_comm_event(struct perf_comm_event *comm_event)
5823{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005824 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005825 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005826
5827 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005828 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005829 size = ALIGN(strlen(comm)+1, sizeof(u64));
5830
5831 comm_event->comm = comm;
5832 comm_event->comm_size = size;
5833
5834 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005835
Jiri Olsa67516842013-07-09 18:56:31 +02005836 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005837 comm_event,
5838 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005839}
5840
Adrian Hunter82b89772014-05-28 11:45:04 +03005841void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005842{
5843 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005844
5845 if (!atomic_read(&nr_comm_events))
5846 return;
5847
5848 comm_event = (struct perf_comm_event){
5849 .task = task,
5850 /* .comm */
5851 /* .comm_size */
5852 .event_id = {
5853 .header = {
5854 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005855 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005856 /* .size */
5857 },
5858 /* .pid */
5859 /* .tid */
5860 },
5861 };
5862
5863 perf_event_comm_event(&comm_event);
5864}
5865
5866/*
5867 * mmap tracking
5868 */
5869
5870struct perf_mmap_event {
5871 struct vm_area_struct *vma;
5872
5873 const char *file_name;
5874 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005875 int maj, min;
5876 u64 ino;
5877 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005878 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005879
5880 struct {
5881 struct perf_event_header header;
5882
5883 u32 pid;
5884 u32 tid;
5885 u64 start;
5886 u64 len;
5887 u64 pgoff;
5888 } event_id;
5889};
5890
Jiri Olsa67516842013-07-09 18:56:31 +02005891static int perf_event_mmap_match(struct perf_event *event,
5892 void *data)
5893{
5894 struct perf_mmap_event *mmap_event = data;
5895 struct vm_area_struct *vma = mmap_event->vma;
5896 int executable = vma->vm_flags & VM_EXEC;
5897
5898 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005899 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005900}
5901
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005902static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005903 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005904{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005905 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005906 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005907 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005908 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005909 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005910
Jiri Olsa67516842013-07-09 18:56:31 +02005911 if (!perf_event_mmap_match(event, data))
5912 return;
5913
Stephane Eranian13d7a242013-08-21 12:10:24 +02005914 if (event->attr.mmap2) {
5915 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5916 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5917 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5918 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005919 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005920 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
5921 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005922 }
5923
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005924 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5925 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005926 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005927 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005928 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005929
5930 mmap_event->event_id.pid = perf_event_pid(event, current);
5931 mmap_event->event_id.tid = perf_event_tid(event, current);
5932
5933 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005934
5935 if (event->attr.mmap2) {
5936 perf_output_put(&handle, mmap_event->maj);
5937 perf_output_put(&handle, mmap_event->min);
5938 perf_output_put(&handle, mmap_event->ino);
5939 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005940 perf_output_put(&handle, mmap_event->prot);
5941 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005942 }
5943
Frederic Weisbecker76369132011-05-19 19:55:04 +02005944 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005945 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005946
5947 perf_event__output_id_sample(event, &handle, &sample);
5948
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005949 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005950out:
5951 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005952}
5953
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005954static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5955{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005956 struct vm_area_struct *vma = mmap_event->vma;
5957 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005958 int maj = 0, min = 0;
5959 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005960 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005961 unsigned int size;
5962 char tmp[16];
5963 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005964 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005965
5966 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005967 struct inode *inode;
5968 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005969
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005970 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005971 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005972 name = "//enomem";
5973 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005974 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005975 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005976 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005977 * need to add enough zero bytes after the string to handle
5978 * the 64bit alignment we do later.
5979 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02005980 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005981 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005982 name = "//toolong";
5983 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005984 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005985 inode = file_inode(vma->vm_file);
5986 dev = inode->i_sb->s_dev;
5987 ino = inode->i_ino;
5988 gen = inode->i_generation;
5989 maj = MAJOR(dev);
5990 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005991
5992 if (vma->vm_flags & VM_READ)
5993 prot |= PROT_READ;
5994 if (vma->vm_flags & VM_WRITE)
5995 prot |= PROT_WRITE;
5996 if (vma->vm_flags & VM_EXEC)
5997 prot |= PROT_EXEC;
5998
5999 if (vma->vm_flags & VM_MAYSHARE)
6000 flags = MAP_SHARED;
6001 else
6002 flags = MAP_PRIVATE;
6003
6004 if (vma->vm_flags & VM_DENYWRITE)
6005 flags |= MAP_DENYWRITE;
6006 if (vma->vm_flags & VM_MAYEXEC)
6007 flags |= MAP_EXECUTABLE;
6008 if (vma->vm_flags & VM_LOCKED)
6009 flags |= MAP_LOCKED;
6010 if (vma->vm_flags & VM_HUGETLB)
6011 flags |= MAP_HUGETLB;
6012
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006013 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006014 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006015 if (vma->vm_ops && vma->vm_ops->name) {
6016 name = (char *) vma->vm_ops->name(vma);
6017 if (name)
6018 goto cpy_name;
6019 }
6020
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006021 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006022 if (name)
6023 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006024
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006025 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006026 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006027 name = "[heap]";
6028 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006029 }
6030 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006031 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006032 name = "[stack]";
6033 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006034 }
6035
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006036 name = "//anon";
6037 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006038 }
6039
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006040cpy_name:
6041 strlcpy(tmp, name, sizeof(tmp));
6042 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006043got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006044 /*
6045 * Since our buffer works in 8 byte units we need to align our string
6046 * size to a multiple of 8. However, we must guarantee the tail end is
6047 * zero'd out to avoid leaking random bits to userspace.
6048 */
6049 size = strlen(name)+1;
6050 while (!IS_ALIGNED(size, sizeof(u64)))
6051 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006052
6053 mmap_event->file_name = name;
6054 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006055 mmap_event->maj = maj;
6056 mmap_event->min = min;
6057 mmap_event->ino = ino;
6058 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006059 mmap_event->prot = prot;
6060 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006061
Stephane Eranian2fe85422013-01-24 16:10:39 +01006062 if (!(vma->vm_flags & VM_EXEC))
6063 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6064
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006065 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6066
Jiri Olsa67516842013-07-09 18:56:31 +02006067 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006068 mmap_event,
6069 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006070
6071 kfree(buf);
6072}
6073
Eric B Munson3af9e852010-05-18 15:30:49 +01006074void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006075{
6076 struct perf_mmap_event mmap_event;
6077
6078 if (!atomic_read(&nr_mmap_events))
6079 return;
6080
6081 mmap_event = (struct perf_mmap_event){
6082 .vma = vma,
6083 /* .file_name */
6084 /* .file_size */
6085 .event_id = {
6086 .header = {
6087 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006088 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006089 /* .size */
6090 },
6091 /* .pid */
6092 /* .tid */
6093 .start = vma->vm_start,
6094 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006095 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006096 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006097 /* .maj (attr_mmap2 only) */
6098 /* .min (attr_mmap2 only) */
6099 /* .ino (attr_mmap2 only) */
6100 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006101 /* .prot (attr_mmap2 only) */
6102 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006103 };
6104
6105 perf_event_mmap_event(&mmap_event);
6106}
6107
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006108void perf_event_aux_event(struct perf_event *event, unsigned long head,
6109 unsigned long size, u64 flags)
6110{
6111 struct perf_output_handle handle;
6112 struct perf_sample_data sample;
6113 struct perf_aux_event {
6114 struct perf_event_header header;
6115 u64 offset;
6116 u64 size;
6117 u64 flags;
6118 } rec = {
6119 .header = {
6120 .type = PERF_RECORD_AUX,
6121 .misc = 0,
6122 .size = sizeof(rec),
6123 },
6124 .offset = head,
6125 .size = size,
6126 .flags = flags,
6127 };
6128 int ret;
6129
6130 perf_event_header__init_id(&rec.header, &sample, event);
6131 ret = perf_output_begin(&handle, event, rec.header.size);
6132
6133 if (ret)
6134 return;
6135
6136 perf_output_put(&handle, rec);
6137 perf_event__output_id_sample(event, &handle, &sample);
6138
6139 perf_output_end(&handle);
6140}
6141
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006142/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006143 * Lost/dropped samples logging
6144 */
6145void perf_log_lost_samples(struct perf_event *event, u64 lost)
6146{
6147 struct perf_output_handle handle;
6148 struct perf_sample_data sample;
6149 int ret;
6150
6151 struct {
6152 struct perf_event_header header;
6153 u64 lost;
6154 } lost_samples_event = {
6155 .header = {
6156 .type = PERF_RECORD_LOST_SAMPLES,
6157 .misc = 0,
6158 .size = sizeof(lost_samples_event),
6159 },
6160 .lost = lost,
6161 };
6162
6163 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6164
6165 ret = perf_output_begin(&handle, event,
6166 lost_samples_event.header.size);
6167 if (ret)
6168 return;
6169
6170 perf_output_put(&handle, lost_samples_event);
6171 perf_event__output_id_sample(event, &handle, &sample);
6172 perf_output_end(&handle);
6173}
6174
6175/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006176 * context_switch tracking
6177 */
6178
6179struct perf_switch_event {
6180 struct task_struct *task;
6181 struct task_struct *next_prev;
6182
6183 struct {
6184 struct perf_event_header header;
6185 u32 next_prev_pid;
6186 u32 next_prev_tid;
6187 } event_id;
6188};
6189
6190static int perf_event_switch_match(struct perf_event *event)
6191{
6192 return event->attr.context_switch;
6193}
6194
6195static void perf_event_switch_output(struct perf_event *event, void *data)
6196{
6197 struct perf_switch_event *se = data;
6198 struct perf_output_handle handle;
6199 struct perf_sample_data sample;
6200 int ret;
6201
6202 if (!perf_event_switch_match(event))
6203 return;
6204
6205 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6206 if (event->ctx->task) {
6207 se->event_id.header.type = PERF_RECORD_SWITCH;
6208 se->event_id.header.size = sizeof(se->event_id.header);
6209 } else {
6210 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6211 se->event_id.header.size = sizeof(se->event_id);
6212 se->event_id.next_prev_pid =
6213 perf_event_pid(event, se->next_prev);
6214 se->event_id.next_prev_tid =
6215 perf_event_tid(event, se->next_prev);
6216 }
6217
6218 perf_event_header__init_id(&se->event_id.header, &sample, event);
6219
6220 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6221 if (ret)
6222 return;
6223
6224 if (event->ctx->task)
6225 perf_output_put(&handle, se->event_id.header);
6226 else
6227 perf_output_put(&handle, se->event_id);
6228
6229 perf_event__output_id_sample(event, &handle, &sample);
6230
6231 perf_output_end(&handle);
6232}
6233
6234static void perf_event_switch(struct task_struct *task,
6235 struct task_struct *next_prev, bool sched_in)
6236{
6237 struct perf_switch_event switch_event;
6238
6239 /* N.B. caller checks nr_switch_events != 0 */
6240
6241 switch_event = (struct perf_switch_event){
6242 .task = task,
6243 .next_prev = next_prev,
6244 .event_id = {
6245 .header = {
6246 /* .type */
6247 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
6248 /* .size */
6249 },
6250 /* .next_prev_pid */
6251 /* .next_prev_tid */
6252 },
6253 };
6254
6255 perf_event_aux(perf_event_switch_output,
6256 &switch_event,
6257 NULL);
6258}
6259
6260/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006261 * IRQ throttle logging
6262 */
6263
6264static void perf_log_throttle(struct perf_event *event, int enable)
6265{
6266 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006267 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006268 int ret;
6269
6270 struct {
6271 struct perf_event_header header;
6272 u64 time;
6273 u64 id;
6274 u64 stream_id;
6275 } throttle_event = {
6276 .header = {
6277 .type = PERF_RECORD_THROTTLE,
6278 .misc = 0,
6279 .size = sizeof(throttle_event),
6280 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01006281 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006282 .id = primary_event_id(event),
6283 .stream_id = event->id,
6284 };
6285
6286 if (enable)
6287 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
6288
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006289 perf_event_header__init_id(&throttle_event.header, &sample, event);
6290
6291 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006292 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006293 if (ret)
6294 return;
6295
6296 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006297 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006298 perf_output_end(&handle);
6299}
6300
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006301static void perf_log_itrace_start(struct perf_event *event)
6302{
6303 struct perf_output_handle handle;
6304 struct perf_sample_data sample;
6305 struct perf_aux_event {
6306 struct perf_event_header header;
6307 u32 pid;
6308 u32 tid;
6309 } rec;
6310 int ret;
6311
6312 if (event->parent)
6313 event = event->parent;
6314
6315 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
6316 event->hw.itrace_started)
6317 return;
6318
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006319 rec.header.type = PERF_RECORD_ITRACE_START;
6320 rec.header.misc = 0;
6321 rec.header.size = sizeof(rec);
6322 rec.pid = perf_event_pid(event, current);
6323 rec.tid = perf_event_tid(event, current);
6324
6325 perf_event_header__init_id(&rec.header, &sample, event);
6326 ret = perf_output_begin(&handle, event, rec.header.size);
6327
6328 if (ret)
6329 return;
6330
6331 perf_output_put(&handle, rec);
6332 perf_event__output_id_sample(event, &handle, &sample);
6333
6334 perf_output_end(&handle);
6335}
6336
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006337/*
6338 * Generic event overflow handling, sampling.
6339 */
6340
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006341static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006342 int throttle, struct perf_sample_data *data,
6343 struct pt_regs *regs)
6344{
6345 int events = atomic_read(&event->event_limit);
6346 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006347 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006348 int ret = 0;
6349
Peter Zijlstra96398822010-11-24 18:55:29 +01006350 /*
6351 * Non-sampling counters might still use the PMI to fold short
6352 * hardware counters, ignore those.
6353 */
6354 if (unlikely(!is_sampling_event(event)))
6355 return 0;
6356
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006357 seq = __this_cpu_read(perf_throttled_seq);
6358 if (seq != hwc->interrupts_seq) {
6359 hwc->interrupts_seq = seq;
6360 hwc->interrupts = 1;
6361 } else {
6362 hwc->interrupts++;
6363 if (unlikely(throttle
6364 && hwc->interrupts >= max_samples_per_tick)) {
6365 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01006366 hwc->interrupts = MAX_INTERRUPTS;
6367 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02006368 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006369 ret = 1;
6370 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006371 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006372
6373 if (event->attr.freq) {
6374 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01006375 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006376
Peter Zijlstraabd50712010-01-26 18:50:16 +01006377 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006378
Peter Zijlstraabd50712010-01-26 18:50:16 +01006379 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01006380 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006381 }
6382
6383 /*
6384 * XXX event_limit might not quite work as expected on inherited
6385 * events
6386 */
6387
6388 event->pending_kill = POLL_IN;
6389 if (events && atomic_dec_and_test(&event->event_limit)) {
6390 ret = 1;
6391 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006392 event->pending_disable = 1;
6393 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006394 }
6395
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006396 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006397 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006398 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006399 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006400
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006401 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006402 event->pending_wakeup = 1;
6403 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02006404 }
6405
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006406 return ret;
6407}
6408
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006409int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006410 struct perf_sample_data *data,
6411 struct pt_regs *regs)
6412{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006413 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006414}
6415
6416/*
6417 * Generic software event infrastructure
6418 */
6419
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006420struct swevent_htable {
6421 struct swevent_hlist *swevent_hlist;
6422 struct mutex hlist_mutex;
6423 int hlist_refcount;
6424
6425 /* Recursion avoidance in each contexts */
6426 int recursion[PERF_NR_CONTEXTS];
6427};
6428
6429static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
6430
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006431/*
6432 * We directly increment event->count and keep a second value in
6433 * event->hw.period_left to count intervals. This period event
6434 * is kept in the range [-sample_period, 0] so that we can use the
6435 * sign as trigger.
6436 */
6437
Jiri Olsaab573842013-05-01 17:25:44 +02006438u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006439{
6440 struct hw_perf_event *hwc = &event->hw;
6441 u64 period = hwc->last_period;
6442 u64 nr, offset;
6443 s64 old, val;
6444
6445 hwc->last_period = hwc->sample_period;
6446
6447again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02006448 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006449 if (val < 0)
6450 return 0;
6451
6452 nr = div64_u64(period + val, period);
6453 offset = nr * period;
6454 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02006455 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006456 goto again;
6457
6458 return nr;
6459}
6460
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006461static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006462 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006463 struct pt_regs *regs)
6464{
6465 struct hw_perf_event *hwc = &event->hw;
6466 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006467
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006468 if (!overflow)
6469 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006470
6471 if (hwc->interrupts == MAX_INTERRUPTS)
6472 return;
6473
6474 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006475 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006476 data, regs)) {
6477 /*
6478 * We inhibit the overflow from happening when
6479 * hwc->interrupts == MAX_INTERRUPTS.
6480 */
6481 break;
6482 }
6483 throttle = 1;
6484 }
6485}
6486
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006487static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006488 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006489 struct pt_regs *regs)
6490{
6491 struct hw_perf_event *hwc = &event->hw;
6492
Peter Zijlstrae7850592010-05-21 14:43:08 +02006493 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006494
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006495 if (!regs)
6496 return;
6497
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006498 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006499 return;
6500
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03006501 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
6502 data->period = nr;
6503 return perf_swevent_overflow(event, 1, data, regs);
6504 } else
6505 data->period = event->hw.last_period;
6506
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006507 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006508 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006509
Peter Zijlstrae7850592010-05-21 14:43:08 +02006510 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006511 return;
6512
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006513 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006514}
6515
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006516static int perf_exclude_event(struct perf_event *event,
6517 struct pt_regs *regs)
6518{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006519 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01006520 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006521
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006522 if (regs) {
6523 if (event->attr.exclude_user && user_mode(regs))
6524 return 1;
6525
6526 if (event->attr.exclude_kernel && !user_mode(regs))
6527 return 1;
6528 }
6529
6530 return 0;
6531}
6532
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006533static int perf_swevent_match(struct perf_event *event,
6534 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08006535 u32 event_id,
6536 struct perf_sample_data *data,
6537 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006538{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006539 if (event->attr.type != type)
6540 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006541
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006542 if (event->attr.config != event_id)
6543 return 0;
6544
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006545 if (perf_exclude_event(event, regs))
6546 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006547
6548 return 1;
6549}
6550
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006551static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006552{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006553 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006554
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006555 return hash_64(val, SWEVENT_HLIST_BITS);
6556}
6557
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006558static inline struct hlist_head *
6559__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006560{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006561 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006562
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006563 return &hlist->heads[hash];
6564}
6565
6566/* For the read side: events when they trigger */
6567static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006568find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006569{
6570 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006571
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006572 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006573 if (!hlist)
6574 return NULL;
6575
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006576 return __find_swevent_head(hlist, type, event_id);
6577}
6578
6579/* For the event head insertion and removal in the hlist */
6580static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006581find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006582{
6583 struct swevent_hlist *hlist;
6584 u32 event_id = event->attr.config;
6585 u64 type = event->attr.type;
6586
6587 /*
6588 * Event scheduling is always serialized against hlist allocation
6589 * and release. Which makes the protected version suitable here.
6590 * The context lock guarantees that.
6591 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006592 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006593 lockdep_is_held(&event->ctx->lock));
6594 if (!hlist)
6595 return NULL;
6596
6597 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006598}
6599
6600static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006601 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006602 struct perf_sample_data *data,
6603 struct pt_regs *regs)
6604{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006605 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006606 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006607 struct hlist_head *head;
6608
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006609 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006610 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006611 if (!head)
6612 goto end;
6613
Sasha Levinb67bfe02013-02-27 17:06:00 -08006614 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08006615 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006616 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006617 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006618end:
6619 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006620}
6621
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006622DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
6623
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006624int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006625{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006626 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006627
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006628 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006629}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01006630EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006631
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01006632inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006633{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006634 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006635
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006636 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006637}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006638
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006639void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006640{
Ingo Molnara4234bf2009-11-23 10:57:59 +01006641 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006642
6643 if (WARN_ON_ONCE(!regs))
6644 return;
6645
6646 perf_sample_data_init(&data, addr, 0);
6647 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
6648}
6649
6650void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
6651{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006652 int rctx;
6653
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006654 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006655 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006656 if (unlikely(rctx < 0))
6657 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006658
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006659 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006660
6661 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006662fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006663 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006664}
6665
6666static void perf_swevent_read(struct perf_event *event)
6667{
6668}
6669
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006670static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006671{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006672 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006673 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006674 struct hlist_head *head;
6675
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006676 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006677 hwc->last_period = hwc->sample_period;
6678 perf_swevent_set_period(event);
6679 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006680
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006681 hwc->state = !(flags & PERF_EF_START);
6682
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006683 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01006684 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006685 return -EINVAL;
6686
6687 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08006688 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006689
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006690 return 0;
6691}
6692
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006693static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006694{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006695 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006696}
6697
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006698static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006699{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006700 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006701}
6702
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006703static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006704{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006705 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006706}
6707
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006708/* Deref the hlist from the update side */
6709static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006710swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006711{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006712 return rcu_dereference_protected(swhash->swevent_hlist,
6713 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006714}
6715
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006716static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006717{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006718 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006719
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006720 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006721 return;
6722
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03006723 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08006724 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006725}
6726
6727static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
6728{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006729 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006730
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006731 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006732
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006733 if (!--swhash->hlist_refcount)
6734 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006735
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006736 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006737}
6738
6739static void swevent_hlist_put(struct perf_event *event)
6740{
6741 int cpu;
6742
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006743 for_each_possible_cpu(cpu)
6744 swevent_hlist_put_cpu(event, cpu);
6745}
6746
6747static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
6748{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006749 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006750 int err = 0;
6751
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006752 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006753 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006754 struct swevent_hlist *hlist;
6755
6756 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
6757 if (!hlist) {
6758 err = -ENOMEM;
6759 goto exit;
6760 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006761 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006762 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006763 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006764exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006765 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006766
6767 return err;
6768}
6769
6770static int swevent_hlist_get(struct perf_event *event)
6771{
6772 int err;
6773 int cpu, failed_cpu;
6774
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006775 get_online_cpus();
6776 for_each_possible_cpu(cpu) {
6777 err = swevent_hlist_get_cpu(event, cpu);
6778 if (err) {
6779 failed_cpu = cpu;
6780 goto fail;
6781 }
6782 }
6783 put_online_cpus();
6784
6785 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006786fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006787 for_each_possible_cpu(cpu) {
6788 if (cpu == failed_cpu)
6789 break;
6790 swevent_hlist_put_cpu(event, cpu);
6791 }
6792
6793 put_online_cpus();
6794 return err;
6795}
6796
Ingo Molnarc5905af2012-02-24 08:31:31 +01006797struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006798
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006799static void sw_perf_event_destroy(struct perf_event *event)
6800{
6801 u64 event_id = event->attr.config;
6802
6803 WARN_ON(event->parent);
6804
Ingo Molnarc5905af2012-02-24 08:31:31 +01006805 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006806 swevent_hlist_put(event);
6807}
6808
6809static int perf_swevent_init(struct perf_event *event)
6810{
Tommi Rantala8176cce2013-04-13 22:49:14 +03006811 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006812
6813 if (event->attr.type != PERF_TYPE_SOFTWARE)
6814 return -ENOENT;
6815
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006816 /*
6817 * no branch sampling for software events
6818 */
6819 if (has_branch_stack(event))
6820 return -EOPNOTSUPP;
6821
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006822 switch (event_id) {
6823 case PERF_COUNT_SW_CPU_CLOCK:
6824 case PERF_COUNT_SW_TASK_CLOCK:
6825 return -ENOENT;
6826
6827 default:
6828 break;
6829 }
6830
Dan Carpenterce677832010-10-24 21:50:42 +02006831 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006832 return -ENOENT;
6833
6834 if (!event->parent) {
6835 int err;
6836
6837 err = swevent_hlist_get(event);
6838 if (err)
6839 return err;
6840
Ingo Molnarc5905af2012-02-24 08:31:31 +01006841 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006842 event->destroy = sw_perf_event_destroy;
6843 }
6844
6845 return 0;
6846}
6847
6848static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006849 .task_ctx_nr = perf_sw_context,
6850
Peter Zijlstra34f43922015-02-20 14:05:38 +01006851 .capabilities = PERF_PMU_CAP_NO_NMI,
6852
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006853 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006854 .add = perf_swevent_add,
6855 .del = perf_swevent_del,
6856 .start = perf_swevent_start,
6857 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006858 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006859};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006860
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006861#ifdef CONFIG_EVENT_TRACING
6862
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006863static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006864 struct perf_sample_data *data)
6865{
6866 void *record = data->raw->data;
6867
Peter Zijlstrab71b4372015-11-02 10:50:51 +01006868 /* only top level events have filters set */
6869 if (event->parent)
6870 event = event->parent;
6871
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006872 if (likely(!event->filter) || filter_match_preds(event->filter, record))
6873 return 1;
6874 return 0;
6875}
6876
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006877static int perf_tp_event_match(struct perf_event *event,
6878 struct perf_sample_data *data,
6879 struct pt_regs *regs)
6880{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01006881 if (event->hw.state & PERF_HES_STOPPED)
6882 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02006883 /*
6884 * All tracepoints are from kernel-space.
6885 */
6886 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006887 return 0;
6888
6889 if (!perf_tp_filter_match(event, data))
6890 return 0;
6891
6892 return 1;
6893}
6894
6895void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006896 struct pt_regs *regs, struct hlist_head *head, int rctx,
6897 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006898{
6899 struct perf_sample_data data;
6900 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006901
6902 struct perf_raw_record raw = {
6903 .size = entry_size,
6904 .data = record,
6905 };
6906
Robert Richterfd0d0002012-04-02 20:19:08 +02006907 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006908 data.raw = &raw;
6909
Sasha Levinb67bfe02013-02-27 17:06:00 -08006910 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006911 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006912 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006913 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006914
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006915 /*
6916 * If we got specified a target task, also iterate its context and
6917 * deliver this event there too.
6918 */
6919 if (task && task != current) {
6920 struct perf_event_context *ctx;
6921 struct trace_entry *entry = record;
6922
6923 rcu_read_lock();
6924 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
6925 if (!ctx)
6926 goto unlock;
6927
6928 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
6929 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6930 continue;
6931 if (event->attr.config != entry->type)
6932 continue;
6933 if (perf_tp_event_match(event, &data, regs))
6934 perf_swevent_event(event, count, &data, regs);
6935 }
6936unlock:
6937 rcu_read_unlock();
6938 }
6939
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006940 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006941}
6942EXPORT_SYMBOL_GPL(perf_tp_event);
6943
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006944static void tp_perf_event_destroy(struct perf_event *event)
6945{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006946 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006947}
6948
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006949static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006950{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006951 int err;
6952
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006953 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6954 return -ENOENT;
6955
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006956 /*
6957 * no branch sampling for tracepoint events
6958 */
6959 if (has_branch_stack(event))
6960 return -EOPNOTSUPP;
6961
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006962 err = perf_trace_init(event);
6963 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006964 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006965
6966 event->destroy = tp_perf_event_destroy;
6967
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006968 return 0;
6969}
6970
6971static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006972 .task_ctx_nr = perf_sw_context,
6973
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006974 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006975 .add = perf_trace_add,
6976 .del = perf_trace_del,
6977 .start = perf_swevent_start,
6978 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006979 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006980};
6981
6982static inline void perf_tp_register(void)
6983{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006984 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006985}
Li Zefan6fb29152009-10-15 11:21:42 +08006986
6987static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6988{
6989 char *filter_str;
6990 int ret;
6991
6992 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6993 return -EINVAL;
6994
6995 filter_str = strndup_user(arg, PAGE_SIZE);
6996 if (IS_ERR(filter_str))
6997 return PTR_ERR(filter_str);
6998
6999 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
7000
7001 kfree(filter_str);
7002 return ret;
7003}
7004
7005static void perf_event_free_filter(struct perf_event *event)
7006{
7007 ftrace_profile_free_filter(event);
7008}
7009
Alexei Starovoitov25415172015-03-25 12:49:20 -07007010static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7011{
7012 struct bpf_prog *prog;
7013
7014 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7015 return -EINVAL;
7016
7017 if (event->tp_event->prog)
7018 return -EEXIST;
7019
Wang Nan04a22fa2015-07-01 02:13:50 +00007020 if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE))
7021 /* bpf programs can only be attached to u/kprobes */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007022 return -EINVAL;
7023
7024 prog = bpf_prog_get(prog_fd);
7025 if (IS_ERR(prog))
7026 return PTR_ERR(prog);
7027
Linus Torvalds6c373ca2015-04-15 09:00:47 -07007028 if (prog->type != BPF_PROG_TYPE_KPROBE) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007029 /* valid fd, but invalid bpf program type */
7030 bpf_prog_put(prog);
7031 return -EINVAL;
7032 }
7033
7034 event->tp_event->prog = prog;
7035
7036 return 0;
7037}
7038
7039static void perf_event_free_bpf_prog(struct perf_event *event)
7040{
7041 struct bpf_prog *prog;
7042
7043 if (!event->tp_event)
7044 return;
7045
7046 prog = event->tp_event->prog;
7047 if (prog) {
7048 event->tp_event->prog = NULL;
7049 bpf_prog_put(prog);
7050 }
7051}
7052
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007053#else
Li Zefan6fb29152009-10-15 11:21:42 +08007054
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007055static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007056{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007057}
Li Zefan6fb29152009-10-15 11:21:42 +08007058
7059static int perf_event_set_filter(struct perf_event *event, void __user *arg)
7060{
7061 return -ENOENT;
7062}
7063
7064static void perf_event_free_filter(struct perf_event *event)
7065{
7066}
7067
Alexei Starovoitov25415172015-03-25 12:49:20 -07007068static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7069{
7070 return -ENOENT;
7071}
7072
7073static void perf_event_free_bpf_prog(struct perf_event *event)
7074{
7075}
Li Zefan07b139c2009-12-21 14:27:35 +08007076#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007077
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007078#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007079void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007080{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007081 struct perf_sample_data sample;
7082 struct pt_regs *regs = data;
7083
Robert Richterfd0d0002012-04-02 20:19:08 +02007084 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007085
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007086 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007087 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007088}
7089#endif
7090
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007091/*
7092 * hrtimer based swevent callback
7093 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007094
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007095static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007096{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007097 enum hrtimer_restart ret = HRTIMER_RESTART;
7098 struct perf_sample_data data;
7099 struct pt_regs *regs;
7100 struct perf_event *event;
7101 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007102
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007103 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007104
7105 if (event->state != PERF_EVENT_STATE_ACTIVE)
7106 return HRTIMER_NORESTART;
7107
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007108 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007109
Robert Richterfd0d0002012-04-02 20:19:08 +02007110 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007111 regs = get_irq_regs();
7112
7113 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08007114 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02007115 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007116 ret = HRTIMER_NORESTART;
7117 }
7118
7119 period = max_t(u64, 10000, event->hw.sample_period);
7120 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
7121
7122 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007123}
7124
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007125static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007126{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007127 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007128 s64 period;
7129
7130 if (!is_sampling_event(event))
7131 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007132
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007133 period = local64_read(&hwc->period_left);
7134 if (period) {
7135 if (period < 0)
7136 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007137
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007138 local64_set(&hwc->period_left, 0);
7139 } else {
7140 period = max_t(u64, 10000, hwc->sample_period);
7141 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00007142 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
7143 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007144}
7145
7146static void perf_swevent_cancel_hrtimer(struct perf_event *event)
7147{
7148 struct hw_perf_event *hwc = &event->hw;
7149
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007150 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007151 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007152 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007153
7154 hrtimer_cancel(&hwc->hrtimer);
7155 }
7156}
7157
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007158static void perf_swevent_init_hrtimer(struct perf_event *event)
7159{
7160 struct hw_perf_event *hwc = &event->hw;
7161
7162 if (!is_sampling_event(event))
7163 return;
7164
7165 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
7166 hwc->hrtimer.function = perf_swevent_hrtimer;
7167
7168 /*
7169 * Since hrtimers have a fixed rate, we can do a static freq->period
7170 * mapping and avoid the whole period adjust feedback stuff.
7171 */
7172 if (event->attr.freq) {
7173 long freq = event->attr.sample_freq;
7174
7175 event->attr.sample_period = NSEC_PER_SEC / freq;
7176 hwc->sample_period = event->attr.sample_period;
7177 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09007178 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007179 event->attr.freq = 0;
7180 }
7181}
7182
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007183/*
7184 * Software event: cpu wall time clock
7185 */
7186
7187static void cpu_clock_event_update(struct perf_event *event)
7188{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007189 s64 prev;
7190 u64 now;
7191
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007192 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007193 prev = local64_xchg(&event->hw.prev_count, now);
7194 local64_add(now - prev, &event->count);
7195}
7196
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007197static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007198{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007199 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007200 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007201}
7202
7203static void cpu_clock_event_stop(struct perf_event *event, int flags)
7204{
7205 perf_swevent_cancel_hrtimer(event);
7206 cpu_clock_event_update(event);
7207}
7208
7209static int cpu_clock_event_add(struct perf_event *event, int flags)
7210{
7211 if (flags & PERF_EF_START)
7212 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08007213 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007214
7215 return 0;
7216}
7217
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007218static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007219{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007220 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007221}
7222
7223static void cpu_clock_event_read(struct perf_event *event)
7224{
7225 cpu_clock_event_update(event);
7226}
7227
7228static int cpu_clock_event_init(struct perf_event *event)
7229{
7230 if (event->attr.type != PERF_TYPE_SOFTWARE)
7231 return -ENOENT;
7232
7233 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
7234 return -ENOENT;
7235
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007236 /*
7237 * no branch sampling for software events
7238 */
7239 if (has_branch_stack(event))
7240 return -EOPNOTSUPP;
7241
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007242 perf_swevent_init_hrtimer(event);
7243
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007244 return 0;
7245}
7246
7247static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007248 .task_ctx_nr = perf_sw_context,
7249
Peter Zijlstra34f43922015-02-20 14:05:38 +01007250 .capabilities = PERF_PMU_CAP_NO_NMI,
7251
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007252 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007253 .add = cpu_clock_event_add,
7254 .del = cpu_clock_event_del,
7255 .start = cpu_clock_event_start,
7256 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007257 .read = cpu_clock_event_read,
7258};
7259
7260/*
7261 * Software event: task time clock
7262 */
7263
7264static void task_clock_event_update(struct perf_event *event, u64 now)
7265{
7266 u64 prev;
7267 s64 delta;
7268
7269 prev = local64_xchg(&event->hw.prev_count, now);
7270 delta = now - prev;
7271 local64_add(delta, &event->count);
7272}
7273
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007274static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007275{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007276 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007277 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007278}
7279
7280static void task_clock_event_stop(struct perf_event *event, int flags)
7281{
7282 perf_swevent_cancel_hrtimer(event);
7283 task_clock_event_update(event, event->ctx->time);
7284}
7285
7286static int task_clock_event_add(struct perf_event *event, int flags)
7287{
7288 if (flags & PERF_EF_START)
7289 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08007290 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007291
7292 return 0;
7293}
7294
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007295static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007296{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007297 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007298}
7299
7300static void task_clock_event_read(struct perf_event *event)
7301{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01007302 u64 now = perf_clock();
7303 u64 delta = now - event->ctx->timestamp;
7304 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007305
7306 task_clock_event_update(event, time);
7307}
7308
7309static int task_clock_event_init(struct perf_event *event)
7310{
7311 if (event->attr.type != PERF_TYPE_SOFTWARE)
7312 return -ENOENT;
7313
7314 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
7315 return -ENOENT;
7316
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007317 /*
7318 * no branch sampling for software events
7319 */
7320 if (has_branch_stack(event))
7321 return -EOPNOTSUPP;
7322
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007323 perf_swevent_init_hrtimer(event);
7324
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007325 return 0;
7326}
7327
7328static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007329 .task_ctx_nr = perf_sw_context,
7330
Peter Zijlstra34f43922015-02-20 14:05:38 +01007331 .capabilities = PERF_PMU_CAP_NO_NMI,
7332
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007333 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007334 .add = task_clock_event_add,
7335 .del = task_clock_event_del,
7336 .start = task_clock_event_start,
7337 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007338 .read = task_clock_event_read,
7339};
7340
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007341static void perf_pmu_nop_void(struct pmu *pmu)
7342{
7343}
7344
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007345static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
7346{
7347}
7348
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007349static int perf_pmu_nop_int(struct pmu *pmu)
7350{
7351 return 0;
7352}
7353
Geliang Tang18ab2cd2015-09-27 23:25:50 +08007354static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007355
7356static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007357{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007358 __this_cpu_write(nop_txn_flags, flags);
7359
7360 if (flags & ~PERF_PMU_TXN_ADD)
7361 return;
7362
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007363 perf_pmu_disable(pmu);
7364}
7365
7366static int perf_pmu_commit_txn(struct pmu *pmu)
7367{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007368 unsigned int flags = __this_cpu_read(nop_txn_flags);
7369
7370 __this_cpu_write(nop_txn_flags, 0);
7371
7372 if (flags & ~PERF_PMU_TXN_ADD)
7373 return 0;
7374
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007375 perf_pmu_enable(pmu);
7376 return 0;
7377}
7378
7379static void perf_pmu_cancel_txn(struct pmu *pmu)
7380{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007381 unsigned int flags = __this_cpu_read(nop_txn_flags);
7382
7383 __this_cpu_write(nop_txn_flags, 0);
7384
7385 if (flags & ~PERF_PMU_TXN_ADD)
7386 return;
7387
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007388 perf_pmu_enable(pmu);
7389}
7390
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007391static int perf_event_idx_default(struct perf_event *event)
7392{
Peter Zijlstrac719f562014-10-21 11:10:21 +02007393 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007394}
7395
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007396/*
7397 * Ensures all contexts with the same task_ctx_nr have the same
7398 * pmu_cpu_context too.
7399 */
Mark Rutland9e317042014-02-10 17:44:18 +00007400static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007401{
7402 struct pmu *pmu;
7403
7404 if (ctxn < 0)
7405 return NULL;
7406
7407 list_for_each_entry(pmu, &pmus, entry) {
7408 if (pmu->task_ctx_nr == ctxn)
7409 return pmu->pmu_cpu_context;
7410 }
7411
7412 return NULL;
7413}
7414
Peter Zijlstra51676952010-12-07 14:18:20 +01007415static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007416{
Peter Zijlstra51676952010-12-07 14:18:20 +01007417 int cpu;
7418
7419 for_each_possible_cpu(cpu) {
7420 struct perf_cpu_context *cpuctx;
7421
7422 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
7423
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007424 if (cpuctx->unique_pmu == old_pmu)
7425 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01007426 }
7427}
7428
7429static void free_pmu_context(struct pmu *pmu)
7430{
7431 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007432
7433 mutex_lock(&pmus_lock);
7434 /*
7435 * Like a real lame refcount.
7436 */
Peter Zijlstra51676952010-12-07 14:18:20 +01007437 list_for_each_entry(i, &pmus, entry) {
7438 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
7439 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007440 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01007441 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007442 }
7443
Peter Zijlstra51676952010-12-07 14:18:20 +01007444 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007445out:
7446 mutex_unlock(&pmus_lock);
7447}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007448static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007449
Peter Zijlstraabe43402010-11-17 23:17:37 +01007450static ssize_t
7451type_show(struct device *dev, struct device_attribute *attr, char *page)
7452{
7453 struct pmu *pmu = dev_get_drvdata(dev);
7454
7455 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
7456}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007457static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007458
Stephane Eranian62b85632013-04-03 14:21:34 +02007459static ssize_t
7460perf_event_mux_interval_ms_show(struct device *dev,
7461 struct device_attribute *attr,
7462 char *page)
7463{
7464 struct pmu *pmu = dev_get_drvdata(dev);
7465
7466 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
7467}
7468
Peter Zijlstra272325c2015-04-15 11:41:58 +02007469static DEFINE_MUTEX(mux_interval_mutex);
7470
Stephane Eranian62b85632013-04-03 14:21:34 +02007471static ssize_t
7472perf_event_mux_interval_ms_store(struct device *dev,
7473 struct device_attribute *attr,
7474 const char *buf, size_t count)
7475{
7476 struct pmu *pmu = dev_get_drvdata(dev);
7477 int timer, cpu, ret;
7478
7479 ret = kstrtoint(buf, 0, &timer);
7480 if (ret)
7481 return ret;
7482
7483 if (timer < 1)
7484 return -EINVAL;
7485
7486 /* same value, noting to do */
7487 if (timer == pmu->hrtimer_interval_ms)
7488 return count;
7489
Peter Zijlstra272325c2015-04-15 11:41:58 +02007490 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02007491 pmu->hrtimer_interval_ms = timer;
7492
7493 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02007494 get_online_cpus();
7495 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02007496 struct perf_cpu_context *cpuctx;
7497 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
7498 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
7499
Peter Zijlstra272325c2015-04-15 11:41:58 +02007500 cpu_function_call(cpu,
7501 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02007502 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02007503 put_online_cpus();
7504 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02007505
7506 return count;
7507}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007508static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02007509
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007510static struct attribute *pmu_dev_attrs[] = {
7511 &dev_attr_type.attr,
7512 &dev_attr_perf_event_mux_interval_ms.attr,
7513 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01007514};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007515ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007516
7517static int pmu_bus_running;
7518static struct bus_type pmu_bus = {
7519 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007520 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01007521};
7522
7523static void pmu_dev_release(struct device *dev)
7524{
7525 kfree(dev);
7526}
7527
7528static int pmu_dev_alloc(struct pmu *pmu)
7529{
7530 int ret = -ENOMEM;
7531
7532 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
7533 if (!pmu->dev)
7534 goto out;
7535
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01007536 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01007537 device_initialize(pmu->dev);
7538 ret = dev_set_name(pmu->dev, "%s", pmu->name);
7539 if (ret)
7540 goto free_dev;
7541
7542 dev_set_drvdata(pmu->dev, pmu);
7543 pmu->dev->bus = &pmu_bus;
7544 pmu->dev->release = pmu_dev_release;
7545 ret = device_add(pmu->dev);
7546 if (ret)
7547 goto free_dev;
7548
7549out:
7550 return ret;
7551
7552free_dev:
7553 put_device(pmu->dev);
7554 goto out;
7555}
7556
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007557static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007558static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007559
Mischa Jonker03d8e802013-06-04 11:45:48 +02007560int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007561{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007562 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007563
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007564 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007565 ret = -ENOMEM;
7566 pmu->pmu_disable_count = alloc_percpu(int);
7567 if (!pmu->pmu_disable_count)
7568 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007569
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007570 pmu->type = -1;
7571 if (!name)
7572 goto skip_type;
7573 pmu->name = name;
7574
7575 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08007576 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
7577 if (type < 0) {
7578 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007579 goto free_pdc;
7580 }
7581 }
7582 pmu->type = type;
7583
Peter Zijlstraabe43402010-11-17 23:17:37 +01007584 if (pmu_bus_running) {
7585 ret = pmu_dev_alloc(pmu);
7586 if (ret)
7587 goto free_idr;
7588 }
7589
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007590skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007591 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
7592 if (pmu->pmu_cpu_context)
7593 goto got_cpu_context;
7594
Wei Yongjunc4814202013-04-12 11:05:54 +08007595 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007596 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
7597 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01007598 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007599
7600 for_each_possible_cpu(cpu) {
7601 struct perf_cpu_context *cpuctx;
7602
7603 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02007604 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007605 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007606 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007607 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02007608
Peter Zijlstra272325c2015-04-15 11:41:58 +02007609 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02007610
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007611 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007612 }
7613
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007614got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007615 if (!pmu->start_txn) {
7616 if (pmu->pmu_enable) {
7617 /*
7618 * If we have pmu_enable/pmu_disable calls, install
7619 * transaction stubs that use that to try and batch
7620 * hardware accesses.
7621 */
7622 pmu->start_txn = perf_pmu_start_txn;
7623 pmu->commit_txn = perf_pmu_commit_txn;
7624 pmu->cancel_txn = perf_pmu_cancel_txn;
7625 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007626 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007627 pmu->commit_txn = perf_pmu_nop_int;
7628 pmu->cancel_txn = perf_pmu_nop_void;
7629 }
7630 }
7631
7632 if (!pmu->pmu_enable) {
7633 pmu->pmu_enable = perf_pmu_nop_void;
7634 pmu->pmu_disable = perf_pmu_nop_void;
7635 }
7636
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007637 if (!pmu->event_idx)
7638 pmu->event_idx = perf_event_idx_default;
7639
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007640 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007641 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007642 ret = 0;
7643unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007644 mutex_unlock(&pmus_lock);
7645
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007646 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007647
Peter Zijlstraabe43402010-11-17 23:17:37 +01007648free_dev:
7649 device_del(pmu->dev);
7650 put_device(pmu->dev);
7651
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007652free_idr:
7653 if (pmu->type >= PERF_TYPE_MAX)
7654 idr_remove(&pmu_idr, pmu->type);
7655
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007656free_pdc:
7657 free_percpu(pmu->pmu_disable_count);
7658 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007659}
Yan, Zhengc464c762014-03-18 16:56:41 +08007660EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007661
7662void perf_pmu_unregister(struct pmu *pmu)
7663{
7664 mutex_lock(&pmus_lock);
7665 list_del_rcu(&pmu->entry);
7666 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007667
7668 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02007669 * We dereference the pmu list under both SRCU and regular RCU, so
7670 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007671 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007672 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02007673 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007674
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007675 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007676 if (pmu->type >= PERF_TYPE_MAX)
7677 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007678 device_del(pmu->dev);
7679 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01007680 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007681}
Yan, Zhengc464c762014-03-18 16:56:41 +08007682EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007683
Mark Rutlandcc34b982015-01-07 14:56:51 +00007684static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
7685{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007686 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00007687 int ret;
7688
7689 if (!try_module_get(pmu->module))
7690 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007691
7692 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02007693 /*
7694 * This ctx->mutex can nest when we're called through
7695 * inheritance. See the perf_event_ctx_lock_nested() comment.
7696 */
7697 ctx = perf_event_ctx_lock_nested(event->group_leader,
7698 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007699 BUG_ON(!ctx);
7700 }
7701
Mark Rutlandcc34b982015-01-07 14:56:51 +00007702 event->pmu = pmu;
7703 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007704
7705 if (ctx)
7706 perf_event_ctx_unlock(event->group_leader, ctx);
7707
Mark Rutlandcc34b982015-01-07 14:56:51 +00007708 if (ret)
7709 module_put(pmu->module);
7710
7711 return ret;
7712}
7713
Geliang Tang18ab2cd2015-09-27 23:25:50 +08007714static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007715{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007716 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007717 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08007718 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007719
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007720 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007721
7722 rcu_read_lock();
7723 pmu = idr_find(&pmu_idr, event->attr.type);
7724 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08007725 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007726 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08007727 if (ret)
7728 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007729 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08007730 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007731
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007732 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007733 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007734 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007735 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007736
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007737 if (ret != -ENOENT) {
7738 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007739 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007740 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007741 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007742 pmu = ERR_PTR(-ENOENT);
7743unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007744 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007745
7746 return pmu;
7747}
7748
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007749static void account_event_cpu(struct perf_event *event, int cpu)
7750{
7751 if (event->parent)
7752 return;
7753
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007754 if (is_cgroup_event(event))
7755 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
7756}
7757
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007758static void account_event(struct perf_event *event)
7759{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007760 bool inc = false;
7761
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007762 if (event->parent)
7763 return;
7764
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007765 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007766 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007767 if (event->attr.mmap || event->attr.mmap_data)
7768 atomic_inc(&nr_mmap_events);
7769 if (event->attr.comm)
7770 atomic_inc(&nr_comm_events);
7771 if (event->attr.task)
7772 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02007773 if (event->attr.freq) {
7774 if (atomic_inc_return(&nr_freq_events) == 1)
7775 tick_nohz_full_kick_all();
7776 }
Adrian Hunter45ac1402015-07-21 12:44:02 +03007777 if (event->attr.context_switch) {
7778 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007779 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03007780 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007781 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007782 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007783 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007784 inc = true;
7785
7786 if (inc)
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007787 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007788
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007789 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007790}
7791
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007792/*
7793 * Allocate and initialize a event structure
7794 */
7795static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007796perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007797 struct task_struct *task,
7798 struct perf_event *group_leader,
7799 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007800 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00007801 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007802{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007803 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007804 struct perf_event *event;
7805 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007806 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007807
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007808 if ((unsigned)cpu >= nr_cpu_ids) {
7809 if (!task || cpu != -1)
7810 return ERR_PTR(-EINVAL);
7811 }
7812
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007813 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007814 if (!event)
7815 return ERR_PTR(-ENOMEM);
7816
7817 /*
7818 * Single events are their own group leaders, with an
7819 * empty sibling list:
7820 */
7821 if (!group_leader)
7822 group_leader = event;
7823
7824 mutex_init(&event->child_mutex);
7825 INIT_LIST_HEAD(&event->child_list);
7826
7827 INIT_LIST_HEAD(&event->group_entry);
7828 INIT_LIST_HEAD(&event->event_entry);
7829 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007830 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01007831 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01007832 INIT_HLIST_NODE(&event->hlist_entry);
7833
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007834
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007835 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08007836 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007837
7838 mutex_init(&event->mmap_mutex);
7839
Al Viroa6fa9412012-08-20 14:59:25 +01007840 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007841 event->cpu = cpu;
7842 event->attr = *attr;
7843 event->group_leader = group_leader;
7844 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007845 event->oncpu = -1;
7846
7847 event->parent = parent_event;
7848
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08007849 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007850 event->id = atomic64_inc_return(&perf_event_id);
7851
7852 event->state = PERF_EVENT_STATE_INACTIVE;
7853
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007854 if (task) {
7855 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007856 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01007857 * XXX pmu::event_init needs to know what task to account to
7858 * and we cannot use the ctx information because we need the
7859 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007860 */
Peter Zijlstra50f16a82015-03-05 22:10:19 +01007861 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007862 }
7863
Peter Zijlstra34f43922015-02-20 14:05:38 +01007864 event->clock = &local_clock;
7865 if (parent_event)
7866 event->clock = parent_event->clock;
7867
Avi Kivity4dc0da82011-06-29 18:42:35 +03007868 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007869 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007870 context = parent_event->overflow_handler_context;
7871 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007872
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007873 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007874 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02007875
Jiri Olsa0231bb52013-02-01 11:23:45 +01007876 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007877
7878 pmu = NULL;
7879
7880 hwc = &event->hw;
7881 hwc->sample_period = attr->sample_period;
7882 if (attr->freq && attr->sample_freq)
7883 hwc->sample_period = 1;
7884 hwc->last_period = hwc->sample_period;
7885
Peter Zijlstrae7850592010-05-21 14:43:08 +02007886 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007887
7888 /*
7889 * we currently do not support PERF_FORMAT_GROUP on inherited events
7890 */
7891 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007892 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007893
Yan, Zhenga46a2302014-11-04 21:56:06 -05007894 if (!has_branch_stack(event))
7895 event->attr.branch_sample_type = 0;
7896
Matt Fleming79dff512015-01-23 18:45:42 +00007897 if (cgroup_fd != -1) {
7898 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
7899 if (err)
7900 goto err_ns;
7901 }
7902
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007903 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007904 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007905 goto err_ns;
7906 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007907 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007908 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007909 }
7910
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007911 err = exclusive_event_init(event);
7912 if (err)
7913 goto err_pmu;
7914
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007915 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007916 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
7917 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007918 if (err)
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007919 goto err_per_task;
Stephane Eraniand010b332012-02-09 23:21:00 +01007920 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007921 }
7922
7923 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007924
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007925err_per_task:
7926 exclusive_event_destroy(event);
7927
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007928err_pmu:
7929 if (event->destroy)
7930 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08007931 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007932err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00007933 if (is_cgroup_event(event))
7934 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007935 if (event->ns)
7936 put_pid_ns(event->ns);
7937 kfree(event);
7938
7939 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007940}
7941
7942static int perf_copy_attr(struct perf_event_attr __user *uattr,
7943 struct perf_event_attr *attr)
7944{
7945 u32 size;
7946 int ret;
7947
7948 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
7949 return -EFAULT;
7950
7951 /*
7952 * zero the full structure, so that a short copy will be nice.
7953 */
7954 memset(attr, 0, sizeof(*attr));
7955
7956 ret = get_user(size, &uattr->size);
7957 if (ret)
7958 return ret;
7959
7960 if (size > PAGE_SIZE) /* silly large */
7961 goto err_size;
7962
7963 if (!size) /* abi compat */
7964 size = PERF_ATTR_SIZE_VER0;
7965
7966 if (size < PERF_ATTR_SIZE_VER0)
7967 goto err_size;
7968
7969 /*
7970 * If we're handed a bigger struct than we know of,
7971 * ensure all the unknown bits are 0 - i.e. new
7972 * user-space does not rely on any kernel feature
7973 * extensions we dont know about yet.
7974 */
7975 if (size > sizeof(*attr)) {
7976 unsigned char __user *addr;
7977 unsigned char __user *end;
7978 unsigned char val;
7979
7980 addr = (void __user *)uattr + sizeof(*attr);
7981 end = (void __user *)uattr + size;
7982
7983 for (; addr < end; addr++) {
7984 ret = get_user(val, addr);
7985 if (ret)
7986 return ret;
7987 if (val)
7988 goto err_size;
7989 }
7990 size = sizeof(*attr);
7991 }
7992
7993 ret = copy_from_user(attr, uattr, size);
7994 if (ret)
7995 return -EFAULT;
7996
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05307997 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007998 return -EINVAL;
7999
8000 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
8001 return -EINVAL;
8002
8003 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
8004 return -EINVAL;
8005
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008006 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
8007 u64 mask = attr->branch_sample_type;
8008
8009 /* only using defined bits */
8010 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
8011 return -EINVAL;
8012
8013 /* at least one branch bit must be set */
8014 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
8015 return -EINVAL;
8016
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008017 /* propagate priv level, when not set for branch */
8018 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
8019
8020 /* exclude_kernel checked on syscall entry */
8021 if (!attr->exclude_kernel)
8022 mask |= PERF_SAMPLE_BRANCH_KERNEL;
8023
8024 if (!attr->exclude_user)
8025 mask |= PERF_SAMPLE_BRANCH_USER;
8026
8027 if (!attr->exclude_hv)
8028 mask |= PERF_SAMPLE_BRANCH_HV;
8029 /*
8030 * adjust user setting (for HW filter setup)
8031 */
8032 attr->branch_sample_type = mask;
8033 }
Stephane Eraniane7122092013-06-06 11:02:04 +02008034 /* privileged levels capture (kernel, hv): check permissions */
8035 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02008036 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8037 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008038 }
Jiri Olsa40189942012-08-07 15:20:37 +02008039
Jiri Olsac5ebced2012-08-07 15:20:40 +02008040 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02008041 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02008042 if (ret)
8043 return ret;
8044 }
8045
8046 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
8047 if (!arch_perf_have_user_stack_dump())
8048 return -ENOSYS;
8049
8050 /*
8051 * We have __u32 type for the size, but so far
8052 * we can only use __u16 as maximum due to the
8053 * __u16 sample size limit.
8054 */
8055 if (attr->sample_stack_user >= USHRT_MAX)
8056 ret = -EINVAL;
8057 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
8058 ret = -EINVAL;
8059 }
Jiri Olsa40189942012-08-07 15:20:37 +02008060
Stephane Eranian60e23642014-09-24 13:48:37 +02008061 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
8062 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008063out:
8064 return ret;
8065
8066err_size:
8067 put_user(sizeof(*attr), &uattr->size);
8068 ret = -E2BIG;
8069 goto out;
8070}
8071
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008072static int
8073perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008074{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01008075 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008076 int ret = -EINVAL;
8077
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008078 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008079 goto set;
8080
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008081 /* don't allow circular references */
8082 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008083 goto out;
8084
Peter Zijlstra0f139302010-05-20 14:35:15 +02008085 /*
8086 * Don't allow cross-cpu buffers
8087 */
8088 if (output_event->cpu != event->cpu)
8089 goto out;
8090
8091 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02008092 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02008093 */
8094 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
8095 goto out;
8096
Peter Zijlstra34f43922015-02-20 14:05:38 +01008097 /*
8098 * Mixing clocks in the same buffer is trouble you don't need.
8099 */
8100 if (output_event->clock != event->clock)
8101 goto out;
8102
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02008103 /*
8104 * If both events generate aux data, they must be on the same PMU
8105 */
8106 if (has_aux(event) && has_aux(output_event) &&
8107 event->pmu != output_event->pmu)
8108 goto out;
8109
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008110set:
8111 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008112 /* Can't redirect output if we've got an active mmap() */
8113 if (atomic_read(&event->mmap_count))
8114 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008115
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008116 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02008117 /* get the rb we want to redirect to */
8118 rb = ring_buffer_get(output_event);
8119 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008120 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008121 }
8122
Peter Zijlstrab69cf532014-03-14 10:50:33 +01008123 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02008124
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008125 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008126unlock:
8127 mutex_unlock(&event->mmap_mutex);
8128
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008129out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008130 return ret;
8131}
8132
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008133static void mutex_lock_double(struct mutex *a, struct mutex *b)
8134{
8135 if (b < a)
8136 swap(a, b);
8137
8138 mutex_lock(a);
8139 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
8140}
8141
Peter Zijlstra34f43922015-02-20 14:05:38 +01008142static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
8143{
8144 bool nmi_safe = false;
8145
8146 switch (clk_id) {
8147 case CLOCK_MONOTONIC:
8148 event->clock = &ktime_get_mono_fast_ns;
8149 nmi_safe = true;
8150 break;
8151
8152 case CLOCK_MONOTONIC_RAW:
8153 event->clock = &ktime_get_raw_fast_ns;
8154 nmi_safe = true;
8155 break;
8156
8157 case CLOCK_REALTIME:
8158 event->clock = &ktime_get_real_ns;
8159 break;
8160
8161 case CLOCK_BOOTTIME:
8162 event->clock = &ktime_get_boot_ns;
8163 break;
8164
8165 case CLOCK_TAI:
8166 event->clock = &ktime_get_tai_ns;
8167 break;
8168
8169 default:
8170 return -EINVAL;
8171 }
8172
8173 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
8174 return -EINVAL;
8175
8176 return 0;
8177}
8178
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008179/**
8180 * sys_perf_event_open - open a performance event, associate it to a task/cpu
8181 *
8182 * @attr_uptr: event_id type attributes for monitoring/sampling
8183 * @pid: target pid
8184 * @cpu: target cpu
8185 * @group_fd: group leader event fd
8186 */
8187SYSCALL_DEFINE5(perf_event_open,
8188 struct perf_event_attr __user *, attr_uptr,
8189 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
8190{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008191 struct perf_event *group_leader = NULL, *output_event = NULL;
8192 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008193 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008194 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008195 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04008196 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07008197 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008198 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04008199 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008200 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008201 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01008202 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00008203 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008204
8205 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02008206 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008207 return -EINVAL;
8208
8209 err = perf_copy_attr(attr_uptr, &attr);
8210 if (err)
8211 return err;
8212
8213 if (!attr.exclude_kernel) {
8214 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8215 return -EACCES;
8216 }
8217
8218 if (attr.freq) {
8219 if (attr.sample_freq > sysctl_perf_event_sample_rate)
8220 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02008221 } else {
8222 if (attr.sample_period & (1ULL << 63))
8223 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008224 }
8225
Stephane Eraniane5d13672011-02-14 11:20:01 +02008226 /*
8227 * In cgroup mode, the pid argument is used to pass the fd
8228 * opened to the cgroup directory in cgroupfs. The cpu argument
8229 * designates the cpu on which to monitor threads from that
8230 * cgroup.
8231 */
8232 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
8233 return -EINVAL;
8234
Yann Droneauda21b0b32014-01-05 21:36:33 +01008235 if (flags & PERF_FLAG_FD_CLOEXEC)
8236 f_flags |= O_CLOEXEC;
8237
8238 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04008239 if (event_fd < 0)
8240 return event_fd;
8241
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008242 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04008243 err = perf_fget_light(group_fd, &group);
8244 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008245 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04008246 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008247 if (flags & PERF_FLAG_FD_OUTPUT)
8248 output_event = group_leader;
8249 if (flags & PERF_FLAG_FD_NO_GROUP)
8250 group_leader = NULL;
8251 }
8252
Stephane Eraniane5d13672011-02-14 11:20:01 +02008253 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008254 task = find_lively_task_by_vpid(pid);
8255 if (IS_ERR(task)) {
8256 err = PTR_ERR(task);
8257 goto err_group_fd;
8258 }
8259 }
8260
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008261 if (task && group_leader &&
8262 group_leader->attr.inherit != attr.inherit) {
8263 err = -EINVAL;
8264 goto err_task;
8265 }
8266
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008267 get_online_cpus();
8268
Matt Fleming79dff512015-01-23 18:45:42 +00008269 if (flags & PERF_FLAG_PID_CGROUP)
8270 cgroup_fd = pid;
8271
Avi Kivity4dc0da82011-06-29 18:42:35 +03008272 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00008273 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008274 if (IS_ERR(event)) {
8275 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008276 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008277 }
8278
Vince Weaver53b25332014-05-16 17:12:12 -04008279 if (is_sampling_event(event)) {
8280 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
8281 err = -ENOTSUPP;
8282 goto err_alloc;
8283 }
8284 }
8285
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008286 account_event(event);
8287
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008288 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008289 * Special case software events and allow them to be part of
8290 * any hardware group.
8291 */
8292 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008293
Peter Zijlstra34f43922015-02-20 14:05:38 +01008294 if (attr.use_clockid) {
8295 err = perf_event_set_clock(event, attr.clockid);
8296 if (err)
8297 goto err_alloc;
8298 }
8299
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008300 if (group_leader &&
8301 (is_software_event(event) != is_software_event(group_leader))) {
8302 if (is_software_event(event)) {
8303 /*
8304 * If event and group_leader are not both a software
8305 * event, and event is, then group leader is not.
8306 *
8307 * Allow the addition of software events to !software
8308 * groups, this is safe because software events never
8309 * fail to schedule.
8310 */
8311 pmu = group_leader->pmu;
8312 } else if (is_software_event(group_leader) &&
8313 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
8314 /*
8315 * In case the group is a pure software group, and we
8316 * try to add a hardware event, move the whole group to
8317 * the hardware context.
8318 */
8319 move_group = 1;
8320 }
8321 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008322
8323 /*
8324 * Get the target context (task or percpu):
8325 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05008326 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008327 if (IS_ERR(ctx)) {
8328 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008329 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008330 }
8331
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008332 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
8333 err = -EBUSY;
8334 goto err_context;
8335 }
8336
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02008337 if (task) {
8338 put_task_struct(task);
8339 task = NULL;
8340 }
8341
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008342 /*
8343 * Look up the group leader (we will attach this event to it):
8344 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008345 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008346 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008347
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008348 /*
8349 * Do not allow a recursive hierarchy (this new sibling
8350 * becoming part of another group-sibling):
8351 */
8352 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008353 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01008354
8355 /* All events in a group should have the same clock */
8356 if (group_leader->clock != event->clock)
8357 goto err_context;
8358
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008359 /*
8360 * Do not allow to attach to a group in a different
8361 * task or CPU context:
8362 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008363 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01008364 /*
8365 * Make sure we're both on the same task, or both
8366 * per-cpu events.
8367 */
8368 if (group_leader->ctx->task != ctx->task)
8369 goto err_context;
8370
8371 /*
8372 * Make sure we're both events for the same CPU;
8373 * grouping events for different CPUs is broken; since
8374 * you can never concurrently schedule them anyhow.
8375 */
8376 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008377 goto err_context;
8378 } else {
8379 if (group_leader->ctx != ctx)
8380 goto err_context;
8381 }
8382
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008383 /*
8384 * Only a group leader can be exclusive or pinned
8385 */
8386 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008387 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008388 }
8389
8390 if (output_event) {
8391 err = perf_event_set_output(event, output_event);
8392 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008393 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008394 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008395
Yann Droneauda21b0b32014-01-05 21:36:33 +01008396 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
8397 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04008398 if (IS_ERR(event_file)) {
8399 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008400 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04008401 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008402
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008403 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008404 gctx = group_leader->ctx;
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008405 mutex_lock_double(&gctx->mutex, &ctx->mutex);
8406 } else {
8407 mutex_lock(&ctx->mutex);
8408 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008409
Peter Zijlstraa7239682015-09-09 19:06:33 +02008410 if (!perf_event_validate_size(event)) {
8411 err = -E2BIG;
8412 goto err_locked;
8413 }
8414
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008415 /*
8416 * Must be under the same ctx::mutex as perf_install_in_context(),
8417 * because we need to serialize with concurrent event creation.
8418 */
8419 if (!exclusive_event_installable(event, ctx)) {
8420 /* exclusive and group stuff are assumed mutually exclusive */
8421 WARN_ON_ONCE(move_group);
8422
8423 err = -EBUSY;
8424 goto err_locked;
8425 }
8426
8427 WARN_ON_ONCE(ctx->parent_ctx);
8428
8429 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008430 /*
8431 * See perf_event_ctx_lock() for comments on the details
8432 * of swizzling perf_event::ctx.
8433 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008434 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01008435
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008436 list_for_each_entry(sibling, &group_leader->sibling_list,
8437 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008438 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008439 put_ctx(gctx);
8440 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008441
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008442 /*
8443 * Wait for everybody to stop referencing the events through
8444 * the old lists, before installing it on new lists.
8445 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008446 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008447
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008448 /*
8449 * Install the group siblings before the group leader.
8450 *
8451 * Because a group leader will try and install the entire group
8452 * (through the sibling list, which is still in-tact), we can
8453 * end up with siblings installed in the wrong context.
8454 *
8455 * By installing siblings first we NO-OP because they're not
8456 * reachable through the group lists.
8457 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008458 list_for_each_entry(sibling, &group_leader->sibling_list,
8459 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008460 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01008461 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008462 get_ctx(ctx);
8463 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008464
8465 /*
8466 * Removing from the context ends up with disabled
8467 * event. What we want here is event in the initial
8468 * startup state, ready to be add into new context.
8469 */
8470 perf_event__state_init(group_leader);
8471 perf_install_in_context(ctx, group_leader, group_leader->cpu);
8472 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008473
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008474 /*
8475 * Now that all events are installed in @ctx, nothing
8476 * references @gctx anymore, so drop the last reference we have
8477 * on it.
8478 */
8479 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008480 }
8481
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02008482 /*
8483 * Precalculate sample_data sizes; do while holding ctx::mutex such
8484 * that we're serialized against further additions and before
8485 * perf_install_in_context() which is the point the event is active and
8486 * can use these values.
8487 */
8488 perf_event__header_size(event);
8489 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008490
Peter Zijlstra78cd2c72016-01-25 14:08:45 +01008491 event->owner = current;
8492
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08008493 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008494 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008495
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008496 if (move_group)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008497 mutex_unlock(&gctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008498 mutex_unlock(&ctx->mutex);
8499
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008500 put_online_cpus();
8501
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008502 mutex_lock(&current->perf_event_mutex);
8503 list_add_tail(&event->owner_entry, &current->perf_event_list);
8504 mutex_unlock(&current->perf_event_mutex);
8505
Peter Zijlstra8a495422010-05-27 15:47:49 +02008506 /*
8507 * Drop the reference on the group_event after placing the
8508 * new event on the sibling_list. This ensures destruction
8509 * of the group leader will find the pointer to itself in
8510 * perf_group_detach().
8511 */
Al Viro2903ff02012-08-28 12:52:22 -04008512 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04008513 fd_install(event_fd, event_file);
8514 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008515
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008516err_locked:
8517 if (move_group)
8518 mutex_unlock(&gctx->mutex);
8519 mutex_unlock(&ctx->mutex);
8520/* err_file: */
8521 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008522err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008523 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04008524 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008525err_alloc:
8526 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008527err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008528 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008529err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02008530 if (task)
8531 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008532err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04008533 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04008534err_fd:
8535 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008536 return err;
8537}
8538
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008539/**
8540 * perf_event_create_kernel_counter
8541 *
8542 * @attr: attributes of the counter to create
8543 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07008544 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008545 */
8546struct perf_event *
8547perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07008548 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008549 perf_overflow_handler_t overflow_handler,
8550 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008551{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008552 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008553 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008554 int err;
8555
8556 /*
8557 * Get the target context (task or percpu):
8558 */
8559
Avi Kivity4dc0da82011-06-29 18:42:35 +03008560 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00008561 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008562 if (IS_ERR(event)) {
8563 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008564 goto err;
8565 }
8566
Jiri Olsaf8697762014-08-01 14:33:01 +02008567 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008568 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +02008569
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008570 account_event(event);
8571
Yan, Zheng4af57ef282014-11-04 21:56:01 -05008572 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008573 if (IS_ERR(ctx)) {
8574 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008575 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008576 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008577
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008578 WARN_ON_ONCE(ctx->parent_ctx);
8579 mutex_lock(&ctx->mutex);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008580 if (!exclusive_event_installable(event, ctx)) {
8581 mutex_unlock(&ctx->mutex);
8582 perf_unpin_context(ctx);
8583 put_ctx(ctx);
8584 err = -EBUSY;
8585 goto err_free;
8586 }
8587
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008588 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008589 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008590 mutex_unlock(&ctx->mutex);
8591
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008592 return event;
8593
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008594err_free:
8595 free_event(event);
8596err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008597 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008598}
8599EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
8600
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008601void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
8602{
8603 struct perf_event_context *src_ctx;
8604 struct perf_event_context *dst_ctx;
8605 struct perf_event *event, *tmp;
8606 LIST_HEAD(events);
8607
8608 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
8609 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
8610
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008611 /*
8612 * See perf_event_ctx_lock() for comments on the details
8613 * of swizzling perf_event::ctx.
8614 */
8615 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008616 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
8617 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008618 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02008619 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008620 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02008621 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008622 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008623
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008624 /*
8625 * Wait for the events to quiesce before re-instating them.
8626 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008627 synchronize_rcu();
8628
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008629 /*
8630 * Re-instate events in 2 passes.
8631 *
8632 * Skip over group leaders and only install siblings on this first
8633 * pass, siblings will not get enabled without a leader, however a
8634 * leader will enable its siblings, even if those are still on the old
8635 * context.
8636 */
8637 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
8638 if (event->group_leader == event)
8639 continue;
8640
8641 list_del(&event->migrate_entry);
8642 if (event->state >= PERF_EVENT_STATE_OFF)
8643 event->state = PERF_EVENT_STATE_INACTIVE;
8644 account_event_cpu(event, dst_cpu);
8645 perf_install_in_context(dst_ctx, event, dst_cpu);
8646 get_ctx(dst_ctx);
8647 }
8648
8649 /*
8650 * Once all the siblings are setup properly, install the group leaders
8651 * to make it go.
8652 */
Peter Zijlstra98861672013-10-03 16:02:23 +02008653 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
8654 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008655 if (event->state >= PERF_EVENT_STATE_OFF)
8656 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02008657 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008658 perf_install_in_context(dst_ctx, event, dst_cpu);
8659 get_ctx(dst_ctx);
8660 }
8661 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008662 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008663}
8664EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
8665
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008666static void sync_child_event(struct perf_event *child_event,
8667 struct task_struct *child)
8668{
8669 struct perf_event *parent_event = child_event->parent;
8670 u64 child_val;
8671
8672 if (child_event->attr.inherit_stat)
8673 perf_event_read_event(child_event, child);
8674
Peter Zijlstrab5e58792010-05-21 14:43:12 +02008675 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008676
8677 /*
8678 * Add back the child's count to the parent's count:
8679 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02008680 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008681 atomic64_add(child_event->total_time_enabled,
8682 &parent_event->child_total_time_enabled);
8683 atomic64_add(child_event->total_time_running,
8684 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008685}
8686
8687static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008688perf_event_exit_event(struct perf_event *child_event,
8689 struct perf_event_context *child_ctx,
8690 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008691{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008692 struct perf_event *parent_event = child_event->parent;
8693
Peter Zijlstra1903d502014-07-15 17:27:27 +02008694 /*
8695 * Do not destroy the 'original' grouping; because of the context
8696 * switch optimization the original events could've ended up in a
8697 * random child task.
8698 *
8699 * If we were to destroy the original group, all group related
8700 * operations would cease to function properly after this random
8701 * child dies.
8702 *
8703 * Do destroy all inherited groups, we don't care about those
8704 * and being thorough is better.
8705 */
Peter Zijlstra32132a32016-01-11 15:40:59 +01008706 raw_spin_lock_irq(&child_ctx->lock);
8707 WARN_ON_ONCE(child_ctx->is_active);
8708
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008709 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +01008710 perf_group_detach(child_event);
8711 list_del_event(child_event, child_ctx);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008712 child_event->state = PERF_EVENT_STATE_EXIT;
Peter Zijlstra32132a32016-01-11 15:40:59 +01008713 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008714
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008715 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008716 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008717 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008718 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -04008719 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008720 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008721 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008722 /*
8723 * Child events can be cleaned up.
8724 */
8725
8726 sync_child_event(child_event, child);
8727
8728 /*
8729 * Remove this event from the parent's list
8730 */
8731 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
8732 mutex_lock(&parent_event->child_mutex);
8733 list_del_init(&child_event->child_list);
8734 mutex_unlock(&parent_event->child_mutex);
8735
8736 /*
8737 * Kick perf_poll() for is_event_hup().
8738 */
8739 perf_event_wakeup(parent_event);
8740 free_event(child_event);
8741 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008742}
8743
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008744static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008745{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008746 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008747 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008748
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008749 WARN_ON_ONCE(child != current);
8750
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008751 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008752 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008753 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008754
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008755 /*
8756 * In order to reduce the amount of tricky in ctx tear-down, we hold
8757 * ctx::mutex over the entire thing. This serializes against almost
8758 * everything that wants to access the ctx.
8759 *
8760 * The exception is sys_perf_event_open() /
8761 * perf_event_create_kernel_count() which does find_get_context()
8762 * without ctx::mutex (it cannot because of the move_group double mutex
8763 * lock thing). See the comments in perf_install_in_context().
8764 *
8765 * We can recurse on the same lock type through:
8766 *
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008767 * perf_event_exit_event()
8768 * put_event()
8769 * mutex_lock(&ctx->mutex)
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008770 *
8771 * But since its the parent context it won't be the same instance.
8772 */
8773 mutex_lock(&child_ctx->mutex);
8774
8775 /*
8776 * In a single ctx::lock section, de-schedule the events and detach the
8777 * context from the task such that we cannot ever get it scheduled back
8778 * in.
8779 */
8780 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008781 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008782
8783 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008784 * Now that the context is inactive, destroy the task <-> ctx relation
8785 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008786 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008787 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
8788 put_ctx(child_ctx); /* cannot be last */
8789 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
8790 put_task_struct(current); /* cannot be last */
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008791
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008792 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008793 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008794
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008795 if (clone_ctx)
8796 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008797
8798 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008799 * Report the task dead after unscheduling the events so that we
8800 * won't get any samples after PERF_RECORD_EXIT. We can however still
8801 * get a few PERF_RECORD_READ events.
8802 */
8803 perf_event_task(child, child_ctx, 0);
8804
Peter Zijlstraebf905f2014-05-29 19:00:24 +02008805 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008806 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008807
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008808 mutex_unlock(&child_ctx->mutex);
8809
8810 put_ctx(child_ctx);
8811}
8812
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008813/*
8814 * When a child task exits, feed back event values to parent events.
8815 */
8816void perf_event_exit_task(struct task_struct *child)
8817{
Peter Zijlstra88821352010-11-09 19:01:43 +01008818 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008819 int ctxn;
8820
Peter Zijlstra88821352010-11-09 19:01:43 +01008821 mutex_lock(&child->perf_event_mutex);
8822 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
8823 owner_entry) {
8824 list_del_init(&event->owner_entry);
8825
8826 /*
8827 * Ensure the list deletion is visible before we clear
8828 * the owner, closes a race against perf_release() where
8829 * we need to serialize on the owner->perf_event_mutex.
8830 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01008831 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +01008832 }
8833 mutex_unlock(&child->perf_event_mutex);
8834
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008835 for_each_task_context_nr(ctxn)
8836 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +01008837
8838 /*
8839 * The perf_event_exit_task_context calls perf_event_task
8840 * with child's task_ctx, which generates EXIT events for
8841 * child contexts and sets child->perf_event_ctxp[] to NULL.
8842 * At this point we need to send EXIT events to cpu contexts.
8843 */
8844 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008845}
8846
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008847static void perf_free_event(struct perf_event *event,
8848 struct perf_event_context *ctx)
8849{
8850 struct perf_event *parent = event->parent;
8851
8852 if (WARN_ON_ONCE(!parent))
8853 return;
8854
8855 mutex_lock(&parent->child_mutex);
8856 list_del_init(&event->child_list);
8857 mutex_unlock(&parent->child_mutex);
8858
Al Viroa6fa9412012-08-20 14:59:25 +01008859 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008860
Peter Zijlstra652884f2015-01-23 11:20:10 +01008861 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02008862 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008863 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +01008864 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008865 free_event(event);
8866}
8867
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008868/*
Peter Zijlstra652884f2015-01-23 11:20:10 +01008869 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008870 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +01008871 *
8872 * Not all locks are strictly required, but take them anyway to be nice and
8873 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008874 */
8875void perf_event_free_task(struct task_struct *task)
8876{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008877 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008878 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008879 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008880
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008881 for_each_task_context_nr(ctxn) {
8882 ctx = task->perf_event_ctxp[ctxn];
8883 if (!ctx)
8884 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008885
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008886 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008887again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008888 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
8889 group_entry)
8890 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008891
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008892 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
8893 group_entry)
8894 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008895
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008896 if (!list_empty(&ctx->pinned_groups) ||
8897 !list_empty(&ctx->flexible_groups))
8898 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008899
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008900 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008901
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008902 put_ctx(ctx);
8903 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008904}
8905
Peter Zijlstra4e231c72010-09-09 21:01:59 +02008906void perf_event_delayed_put(struct task_struct *task)
8907{
8908 int ctxn;
8909
8910 for_each_task_context_nr(ctxn)
8911 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
8912}
8913
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08008914struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +00008915{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08008916 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +00008917
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08008918 file = fget_raw(fd);
8919 if (!file)
8920 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +00008921
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08008922 if (file->f_op != &perf_fops) {
8923 fput(file);
8924 return ERR_PTR(-EBADF);
8925 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00008926
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08008927 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +00008928}
8929
8930const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
8931{
8932 if (!event)
8933 return ERR_PTR(-EINVAL);
8934
8935 return &event->attr;
8936}
8937
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008938/*
8939 * inherit a event from parent task to child task:
8940 */
8941static struct perf_event *
8942inherit_event(struct perf_event *parent_event,
8943 struct task_struct *parent,
8944 struct perf_event_context *parent_ctx,
8945 struct task_struct *child,
8946 struct perf_event *group_leader,
8947 struct perf_event_context *child_ctx)
8948{
Jiri Olsa1929def2014-09-12 13:18:27 +02008949 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008950 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02008951 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008952
8953 /*
8954 * Instead of creating recursive hierarchies of events,
8955 * we link inherited events back to the original parent,
8956 * which has a filp for sure, which we use as the reference
8957 * count:
8958 */
8959 if (parent_event->parent)
8960 parent_event = parent_event->parent;
8961
8962 child_event = perf_event_alloc(&parent_event->attr,
8963 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008964 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008965 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +00008966 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008967 if (IS_ERR(child_event))
8968 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01008969
Jiri Olsafadfe7b2014-08-01 14:33:02 +02008970 if (is_orphaned_event(parent_event) ||
8971 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Al Viroa6fa9412012-08-20 14:59:25 +01008972 free_event(child_event);
8973 return NULL;
8974 }
8975
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008976 get_ctx(child_ctx);
8977
8978 /*
8979 * Make the child state follow the state of the parent event,
8980 * not its attr.disabled bit. We hold the parent's mutex,
8981 * so we won't race with perf_event_{en, dis}able_family.
8982 */
Jiri Olsa1929def2014-09-12 13:18:27 +02008983 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008984 child_event->state = PERF_EVENT_STATE_INACTIVE;
8985 else
8986 child_event->state = PERF_EVENT_STATE_OFF;
8987
8988 if (parent_event->attr.freq) {
8989 u64 sample_period = parent_event->hw.sample_period;
8990 struct hw_perf_event *hwc = &child_event->hw;
8991
8992 hwc->sample_period = sample_period;
8993 hwc->last_period = sample_period;
8994
8995 local64_set(&hwc->period_left, sample_period);
8996 }
8997
8998 child_event->ctx = child_ctx;
8999 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03009000 child_event->overflow_handler_context
9001 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009002
9003 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02009004 * Precalculate sample_data sizes
9005 */
9006 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02009007 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02009008
9009 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009010 * Link it up in the child's context:
9011 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02009012 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009013 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02009014 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009015
9016 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009017 * Link this into the parent event's child list
9018 */
9019 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
9020 mutex_lock(&parent_event->child_mutex);
9021 list_add_tail(&child_event->child_list, &parent_event->child_list);
9022 mutex_unlock(&parent_event->child_mutex);
9023
9024 return child_event;
9025}
9026
9027static int inherit_group(struct perf_event *parent_event,
9028 struct task_struct *parent,
9029 struct perf_event_context *parent_ctx,
9030 struct task_struct *child,
9031 struct perf_event_context *child_ctx)
9032{
9033 struct perf_event *leader;
9034 struct perf_event *sub;
9035 struct perf_event *child_ctr;
9036
9037 leader = inherit_event(parent_event, parent, parent_ctx,
9038 child, NULL, child_ctx);
9039 if (IS_ERR(leader))
9040 return PTR_ERR(leader);
9041 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
9042 child_ctr = inherit_event(sub, parent, parent_ctx,
9043 child, leader, child_ctx);
9044 if (IS_ERR(child_ctr))
9045 return PTR_ERR(child_ctr);
9046 }
9047 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009048}
9049
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009050static int
9051inherit_task_group(struct perf_event *event, struct task_struct *parent,
9052 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009053 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009054 int *inherited_all)
9055{
9056 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009057 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009058
9059 if (!event->attr.inherit) {
9060 *inherited_all = 0;
9061 return 0;
9062 }
9063
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009064 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009065 if (!child_ctx) {
9066 /*
9067 * This is executed from the parent task context, so
9068 * inherit events that have been marked for cloning.
9069 * First allocate and initialize a context for the
9070 * child.
9071 */
9072
Jiri Olsa734df5a2013-07-09 17:44:10 +02009073 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009074 if (!child_ctx)
9075 return -ENOMEM;
9076
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009077 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009078 }
9079
9080 ret = inherit_group(event, parent, parent_ctx,
9081 child, child_ctx);
9082
9083 if (ret)
9084 *inherited_all = 0;
9085
9086 return ret;
9087}
9088
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009089/*
9090 * Initialize the perf_event context in task_struct
9091 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02009092static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009093{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009094 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009095 struct perf_event_context *cloned_ctx;
9096 struct perf_event *event;
9097 struct task_struct *parent = current;
9098 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009099 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009100 int ret = 0;
9101
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009102 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009103 return 0;
9104
9105 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009106 * If the parent's context is a clone, pin it so it won't get
9107 * swapped under us.
9108 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009109 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02009110 if (!parent_ctx)
9111 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009112
9113 /*
9114 * No need to check if parent_ctx != NULL here; since we saw
9115 * it non-NULL earlier, the only reason for it to become NULL
9116 * is if we exit, and since we're currently in the middle of
9117 * a fork we can't be exiting at the same time.
9118 */
9119
9120 /*
9121 * Lock the parent list. No need to lock the child - not PID
9122 * hashed yet and not running, so nobody can access it.
9123 */
9124 mutex_lock(&parent_ctx->mutex);
9125
9126 /*
9127 * We dont have to disable NMIs - we are only looking at
9128 * the list, not manipulating it:
9129 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009130 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009131 ret = inherit_task_group(event, parent, parent_ctx,
9132 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009133 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009134 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009135 }
9136
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009137 /*
9138 * We can't hold ctx->lock when iterating the ->flexible_group list due
9139 * to allocations, but we need to prevent rotation because
9140 * rotate_ctx() will change the list from interrupt context.
9141 */
9142 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
9143 parent_ctx->rotate_disable = 1;
9144 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
9145
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009146 list_for_each_entry(event, &parent_ctx->flexible_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)
9150 break;
9151 }
9152
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009153 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
9154 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009155
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009156 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009157
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01009158 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009159 /*
9160 * Mark the child context as a clone of the parent
9161 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009162 *
9163 * Note that if the parent is a clone, the holding of
9164 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009165 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009166 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009167 if (cloned_ctx) {
9168 child_ctx->parent_ctx = cloned_ctx;
9169 child_ctx->parent_gen = parent_ctx->parent_gen;
9170 } else {
9171 child_ctx->parent_ctx = parent_ctx;
9172 child_ctx->parent_gen = parent_ctx->generation;
9173 }
9174 get_ctx(child_ctx->parent_ctx);
9175 }
9176
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009177 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009178 mutex_unlock(&parent_ctx->mutex);
9179
9180 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009181 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009182
9183 return ret;
9184}
9185
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009186/*
9187 * Initialize the perf_event context in task_struct
9188 */
9189int perf_event_init_task(struct task_struct *child)
9190{
9191 int ctxn, ret;
9192
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01009193 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
9194 mutex_init(&child->perf_event_mutex);
9195 INIT_LIST_HEAD(&child->perf_event_list);
9196
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009197 for_each_task_context_nr(ctxn) {
9198 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07009199 if (ret) {
9200 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009201 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07009202 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009203 }
9204
9205 return 0;
9206}
9207
Paul Mackerras220b1402010-03-10 20:45:52 +11009208static void __init perf_event_init_all_cpus(void)
9209{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009210 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11009211 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11009212
9213 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009214 swhash = &per_cpu(swevent_htable, cpu);
9215 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00009216 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11009217 }
9218}
9219
Paul Gortmaker0db06282013-06-19 14:53:51 -04009220static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009221{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009222 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009223
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009224 mutex_lock(&swhash->hlist_mutex);
Linus Torvalds4536e4d2011-11-03 07:44:04 -07009225 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009226 struct swevent_hlist *hlist;
9227
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009228 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
9229 WARN_ON(!hlist);
9230 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009231 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009232 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009233}
9234
Dave Young2965faa2015-09-09 15:38:55 -07009235#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009236static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009237{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009238 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009239 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
9240 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009241
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009242 raw_spin_lock(&ctx->lock);
9243 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009244 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009245 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009246}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009247
9248static void perf_event_exit_cpu_context(int cpu)
9249{
9250 struct perf_event_context *ctx;
9251 struct pmu *pmu;
9252 int idx;
9253
9254 idx = srcu_read_lock(&pmus_srcu);
9255 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02009256 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009257
9258 mutex_lock(&ctx->mutex);
9259 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
9260 mutex_unlock(&ctx->mutex);
9261 }
9262 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009263}
9264
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009265static void perf_event_exit_cpu(int cpu)
9266{
Peter Zijlstrae3703f82014-02-24 12:06:12 +01009267 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009268}
9269#else
9270static inline void perf_event_exit_cpu(int cpu) { }
9271#endif
9272
Peter Zijlstrac2774432010-12-08 15:29:02 +01009273static int
9274perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
9275{
9276 int cpu;
9277
9278 for_each_online_cpu(cpu)
9279 perf_event_exit_cpu(cpu);
9280
9281 return NOTIFY_OK;
9282}
9283
9284/*
9285 * Run the perf reboot notifier at the very last possible moment so that
9286 * the generic watchdog code runs as long as possible.
9287 */
9288static struct notifier_block perf_reboot_notifier = {
9289 .notifier_call = perf_reboot,
9290 .priority = INT_MIN,
9291};
9292
Paul Gortmaker0db06282013-06-19 14:53:51 -04009293static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009294perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
9295{
9296 unsigned int cpu = (long)hcpu;
9297
Linus Torvalds4536e4d2011-11-03 07:44:04 -07009298 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009299
9300 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02009301 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009302 perf_event_init_cpu(cpu);
9303 break;
9304
Peter Zijlstra5e116372010-06-11 13:35:08 +02009305 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009306 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009307 perf_event_exit_cpu(cpu);
9308 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009309 default:
9310 break;
9311 }
9312
9313 return NOTIFY_OK;
9314}
9315
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009316void __init perf_event_init(void)
9317{
Jason Wessel3c502e72010-11-04 17:33:01 -05009318 int ret;
9319
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009320 idr_init(&pmu_idr);
9321
Paul Mackerras220b1402010-03-10 20:45:52 +11009322 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009323 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009324 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
9325 perf_pmu_register(&perf_cpu_clock, NULL, -1);
9326 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009327 perf_tp_register();
9328 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01009329 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05009330
9331 ret = init_hw_breakpoint();
9332 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02009333
9334 /* do not patch jump label more than once per second */
9335 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01009336
9337 /*
9338 * Build time assertion that we keep the data_head at the intended
9339 * location. IOW, validation we got the __reserved[] size right.
9340 */
9341 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
9342 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009343}
Peter Zijlstraabe43402010-11-17 23:17:37 +01009344
Cody P Schaferfd979c02015-01-30 13:45:57 -08009345ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
9346 char *page)
9347{
9348 struct perf_pmu_events_attr *pmu_attr =
9349 container_of(attr, struct perf_pmu_events_attr, attr);
9350
9351 if (pmu_attr->event_str)
9352 return sprintf(page, "%s\n", pmu_attr->event_str);
9353
9354 return 0;
9355}
9356
Peter Zijlstraabe43402010-11-17 23:17:37 +01009357static int __init perf_event_sysfs_init(void)
9358{
9359 struct pmu *pmu;
9360 int ret;
9361
9362 mutex_lock(&pmus_lock);
9363
9364 ret = bus_register(&pmu_bus);
9365 if (ret)
9366 goto unlock;
9367
9368 list_for_each_entry(pmu, &pmus, entry) {
9369 if (!pmu->name || pmu->type < 0)
9370 continue;
9371
9372 ret = pmu_dev_alloc(pmu);
9373 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
9374 }
9375 pmu_bus_running = 1;
9376 ret = 0;
9377
9378unlock:
9379 mutex_unlock(&pmus_lock);
9380
9381 return ret;
9382}
9383device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009384
9385#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04009386static struct cgroup_subsys_state *
9387perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009388{
9389 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02009390
Li Zefan1b15d052011-03-03 14:26:06 +08009391 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009392 if (!jc)
9393 return ERR_PTR(-ENOMEM);
9394
Stephane Eraniane5d13672011-02-14 11:20:01 +02009395 jc->info = alloc_percpu(struct perf_cgroup_info);
9396 if (!jc->info) {
9397 kfree(jc);
9398 return ERR_PTR(-ENOMEM);
9399 }
9400
Stephane Eraniane5d13672011-02-14 11:20:01 +02009401 return &jc->css;
9402}
9403
Tejun Heoeb954192013-08-08 20:11:23 -04009404static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009405{
Tejun Heoeb954192013-08-08 20:11:23 -04009406 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
9407
Stephane Eraniane5d13672011-02-14 11:20:01 +02009408 free_percpu(jc->info);
9409 kfree(jc);
9410}
9411
9412static int __perf_cgroup_move(void *info)
9413{
9414 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +01009415 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +02009416 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +01009417 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +02009418 return 0;
9419}
9420
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009421static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009422{
Tejun Heobb9d97b2011-12-12 18:12:21 -08009423 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009424 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -08009425
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009426 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08009427 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009428}
9429
Tejun Heo073219e2014-02-08 10:36:58 -05009430struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08009431 .css_alloc = perf_cgroup_css_alloc,
9432 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -08009433 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02009434};
9435#endif /* CONFIG_CGROUP_PERF */