blob: 25edabd207de748f147bdcd41b41a25428725d5e [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
Ingo Molnare7e7ee22011-05-04 08:42:29 +02005 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
Peter Zijlstra90eec102015-11-16 11:08:45 +01006 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra
Al Virod36b6912011-12-29 17:09:01 -05007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008 *
Ingo Molnar57c0c152009-09-21 12:20:38 +02009 * For licensing details see kernel-base/COPYING
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010 */
11
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include <linux/cpu.h>
15#include <linux/smp.h>
Peter Zijlstra2e80a822010-11-17 23:17:36 +010016#include <linux/idr.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/file.h>
18#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020020#include <linux/hash.h>
Frederic Weisbecker12351ef2013-04-20 15:48:22 +020021#include <linux/tick.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020022#include <linux/sysfs.h>
23#include <linux/dcache.h>
24#include <linux/percpu.h>
25#include <linux/ptrace.h>
Peter Zijlstrac2774432010-12-08 15:29:02 +010026#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020027#include <linux/vmstat.h>
Peter Zijlstraabe43402010-11-17 23:17:37 +010028#include <linux/device.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040029#include <linux/export.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020030#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020031#include <linux/hardirq.h>
32#include <linux/rculist.h>
33#include <linux/uaccess.h>
34#include <linux/syscalls.h>
35#include <linux/anon_inodes.h>
36#include <linux/kernel_stat.h>
Matt Fleming39bed6c2015-01-23 18:45:40 +000037#include <linux/cgroup.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020038#include <linux/perf_event.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040039#include <linux/trace_events.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050040#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020041#include <linux/mm_types.h>
Yan, Zhengc464c762014-03-18 16:56:41 +080042#include <linux/module.h>
Peter Zijlstraf972eb62014-05-19 15:13:47 -040043#include <linux/mman.h>
Pawel Mollb3f20782014-06-13 16:03:32 +010044#include <linux/compat.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070045#include <linux/bpf.h>
46#include <linux/filter.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020047
Frederic Weisbecker76369132011-05-19 19:55:04 +020048#include "internal.h"
49
Ingo Molnarcdd6c482009-09-21 12:02:48 +020050#include <asm/irq_regs.h>
51
Peter Zijlstra272325c2015-04-15 11:41:58 +020052typedef int (*remote_function_f)(void *);
53
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010054struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020055 struct task_struct *p;
Peter Zijlstra272325c2015-04-15 11:41:58 +020056 remote_function_f func;
Ingo Molnare7e7ee22011-05-04 08:42:29 +020057 void *info;
58 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010059};
60
61static void remote_function(void *data)
62{
63 struct remote_function_call *tfc = data;
64 struct task_struct *p = tfc->p;
65
66 if (p) {
67 tfc->ret = -EAGAIN;
68 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
69 return;
70 }
71
72 tfc->ret = tfc->func(tfc->info);
73}
74
75/**
76 * task_function_call - call a function on the cpu on which a task runs
77 * @p: the task to evaluate
78 * @func: the function to be called
79 * @info: the function call argument
80 *
81 * Calls the function @func when the task is currently running. This might
82 * be on the current CPU, which just calls the function directly
83 *
84 * returns: @func return value, or
85 * -ESRCH - when the process isn't running
86 * -EAGAIN - when the process moved away
87 */
88static int
Peter Zijlstra272325c2015-04-15 11:41:58 +020089task_function_call(struct task_struct *p, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010090{
91 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020092 .p = p,
93 .func = func,
94 .info = info,
95 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010096 };
97
98 if (task_curr(p))
99 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
100
101 return data.ret;
102}
103
104/**
105 * cpu_function_call - call a function on the cpu
106 * @func: the function to be called
107 * @info: the function call argument
108 *
109 * Calls the function @func on the remote cpu.
110 *
111 * returns: @func return value or -ENXIO when the cpu is offline
112 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200113static int cpu_function_call(int cpu, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100114{
115 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200116 .p = NULL,
117 .func = func,
118 .info = info,
119 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100120 };
121
122 smp_call_function_single(cpu, remote_function, &data, 1);
123
124 return data.ret;
125}
126
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100127static inline struct perf_cpu_context *
128__get_cpu_context(struct perf_event_context *ctx)
129{
130 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
131}
132
133static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
134 struct perf_event_context *ctx)
135{
136 raw_spin_lock(&cpuctx->ctx.lock);
137 if (ctx)
138 raw_spin_lock(&ctx->lock);
139}
140
141static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
142 struct perf_event_context *ctx)
143{
144 if (ctx)
145 raw_spin_unlock(&ctx->lock);
146 raw_spin_unlock(&cpuctx->ctx.lock);
147}
148
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100149#define TASK_TOMBSTONE ((void *)-1L)
150
151static bool is_kernel_event(struct perf_event *event)
152{
Peter Zijlstraf47c02c2016-01-26 12:30:14 +0100153 return READ_ONCE(event->owner) == TASK_TOMBSTONE;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100154}
155
Peter Zijlstra39a43642016-01-11 12:46:35 +0100156/*
157 * On task ctx scheduling...
158 *
159 * When !ctx->nr_events a task context will not be scheduled. This means
160 * we can disable the scheduler hooks (for performance) without leaving
161 * pending task ctx state.
162 *
163 * This however results in two special cases:
164 *
165 * - removing the last event from a task ctx; this is relatively straight
166 * forward and is done in __perf_remove_from_context.
167 *
168 * - adding the first event to a task ctx; this is tricky because we cannot
169 * rely on ctx->is_active and therefore cannot use event_function_call().
170 * See perf_install_in_context().
171 *
172 * This is because we need a ctx->lock serialized variable (ctx->is_active)
173 * to reliably determine if a particular task/context is scheduled in. The
174 * task_curr() use in task_function_call() is racy in that a remote context
175 * switch is not a single atomic operation.
176 *
177 * As is, the situation is 'safe' because we set rq->curr before we do the
178 * actual context switch. This means that task_curr() will fail early, but
179 * we'll continue spinning on ctx->is_active until we've passed
180 * perf_event_task_sched_out().
181 *
182 * Without this ctx->lock serialized variable we could have race where we find
183 * the task (and hence the context) would not be active while in fact they are.
184 *
185 * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
186 */
187
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100188typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *,
189 struct perf_event_context *, void *);
190
191struct event_function_struct {
192 struct perf_event *event;
193 event_f func;
194 void *data;
195};
196
197static int event_function(void *info)
198{
199 struct event_function_struct *efs = info;
200 struct perf_event *event = efs->event;
201 struct perf_event_context *ctx = event->ctx;
202 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
203 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100204 int ret = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100205
206 WARN_ON_ONCE(!irqs_disabled());
207
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100208 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100209 /*
210 * Since we do the IPI call without holding ctx->lock things can have
211 * changed, double check we hit the task we set out to hit.
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100212 */
213 if (ctx->task) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100214 if (ctx->task != current) {
215 ret = -EAGAIN;
216 goto unlock;
217 }
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100218
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100219 /*
220 * We only use event_function_call() on established contexts,
221 * and event_function() is only ever called when active (or
222 * rather, we'll have bailed in task_function_call() or the
223 * above ctx->task != current test), therefore we must have
224 * ctx->is_active here.
225 */
226 WARN_ON_ONCE(!ctx->is_active);
227 /*
228 * And since we have ctx->is_active, cpuctx->task_ctx must
229 * match.
230 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100231 WARN_ON_ONCE(task_ctx != ctx);
232 } else {
233 WARN_ON_ONCE(&cpuctx->ctx != ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100234 }
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100235
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100236 efs->func(event, cpuctx, ctx, efs->data);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100237unlock:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100238 perf_ctx_unlock(cpuctx, task_ctx);
239
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100240 return ret;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100241}
242
243static void event_function_local(struct perf_event *event, event_f func, void *data)
244{
245 struct event_function_struct efs = {
246 .event = event,
247 .func = func,
248 .data = data,
249 };
250
251 int ret = event_function(&efs);
252 WARN_ON_ONCE(ret);
253}
254
255static void event_function_call(struct perf_event *event, event_f func, void *data)
Peter Zijlstra00179602015-11-30 16:26:35 +0100256{
257 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100258 struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100259 struct event_function_struct efs = {
260 .event = event,
261 .func = func,
262 .data = data,
263 };
Peter Zijlstra00179602015-11-30 16:26:35 +0100264
Peter Zijlstrac97f4732016-01-14 10:51:03 +0100265 if (!event->parent) {
266 /*
267 * If this is a !child event, we must hold ctx::mutex to
268 * stabilize the the event->ctx relation. See
269 * perf_event_ctx_lock().
270 */
271 lockdep_assert_held(&ctx->mutex);
272 }
Peter Zijlstra00179602015-11-30 16:26:35 +0100273
274 if (!task) {
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100275 cpu_function_call(event->cpu, event_function, &efs);
Peter Zijlstra00179602015-11-30 16:26:35 +0100276 return;
277 }
278
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100279 if (task == TASK_TOMBSTONE)
280 return;
281
Peter Zijlstraa0963092016-02-24 18:45:50 +0100282again:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100283 if (!task_function_call(task, event_function, &efs))
Peter Zijlstra00179602015-11-30 16:26:35 +0100284 return;
285
286 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100287 /*
288 * Reload the task pointer, it might have been changed by
289 * a concurrent perf_event_context_sched_out().
290 */
291 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +0100292 if (task == TASK_TOMBSTONE) {
293 raw_spin_unlock_irq(&ctx->lock);
294 return;
Peter Zijlstra00179602015-11-30 16:26:35 +0100295 }
Peter Zijlstraa0963092016-02-24 18:45:50 +0100296 if (ctx->is_active) {
297 raw_spin_unlock_irq(&ctx->lock);
298 goto again;
299 }
300 func(event, NULL, ctx, data);
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,
Peter Zijlstra3cbaa592016-02-24 18:45:47 +0100319 EVENT_TIME = 0x4,
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200320 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
321};
322
Stephane Eraniane5d13672011-02-14 11:20:01 +0200323/*
324 * perf_sched_events : >0 events exist
325 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
326 */
Peter Zijlstra9107c892016-02-24 18:45:45 +0100327
328static void perf_sched_delayed(struct work_struct *work);
329DEFINE_STATIC_KEY_FALSE(perf_sched_events);
330static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed);
331static DEFINE_MUTEX(perf_sched_mutex);
332static atomic_t perf_sched_count;
333
Stephane Eraniane5d13672011-02-14 11:20:01 +0200334static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Yan, Zhengba532502014-11-04 21:55:58 -0500335static DEFINE_PER_CPU(int, perf_sched_cb_usages);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200336
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200337static atomic_t nr_mmap_events __read_mostly;
338static atomic_t nr_comm_events __read_mostly;
339static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200340static atomic_t nr_freq_events __read_mostly;
Adrian Hunter45ac1402015-07-21 12:44:02 +0300341static atomic_t nr_switch_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200342
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200343static LIST_HEAD(pmus);
344static DEFINE_MUTEX(pmus_lock);
345static struct srcu_struct pmus_srcu;
346
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200347/*
348 * perf event paranoia level:
349 * -1 - not paranoid at all
350 * 0 - disallow raw tracepoint access for unpriv
351 * 1 - disallow cpu events for unpriv
352 * 2 - disallow kernel profiling for unpriv
353 */
354int sysctl_perf_event_paranoid __read_mostly = 1;
355
Frederic Weisbecker20443382011-03-31 03:33:29 +0200356/* Minimum for 512 kiB + 1 user control page */
357int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200358
359/*
360 * max perf event sample rate
361 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700362#define DEFAULT_MAX_SAMPLE_RATE 100000
363#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
364#define DEFAULT_CPU_TIME_MAX_PERCENT 25
365
366int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
367
368static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
369static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
370
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200371static int perf_sample_allowed_ns __read_mostly =
372 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700373
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800374static void update_perf_cpu_limits(void)
Dave Hansen14c63f12013-06-21 08:51:36 -0700375{
376 u64 tmp = perf_sample_period_ns;
377
378 tmp *= sysctl_perf_cpu_time_max_percent;
Stephane Eraniane5302922013-07-05 00:30:11 +0200379 do_div(tmp, 100);
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200380 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
Dave Hansen14c63f12013-06-21 08:51:36 -0700381}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100382
Stephane Eranian9e630202013-04-03 14:21:33 +0200383static int perf_rotate_context(struct perf_cpu_context *cpuctx);
384
Peter Zijlstra163ec432011-02-16 11:22:34 +0100385int perf_proc_update_handler(struct ctl_table *table, int write,
386 void __user *buffer, size_t *lenp,
387 loff_t *ppos)
388{
Knut Petersen723478c2013-09-25 14:29:37 +0200389 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100390
391 if (ret || !write)
392 return ret;
393
394 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700395 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
396 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100397
398 return 0;
399}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200400
Dave Hansen14c63f12013-06-21 08:51:36 -0700401int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
402
403int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
404 void __user *buffer, size_t *lenp,
405 loff_t *ppos)
406{
407 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
408
409 if (ret || !write)
410 return ret;
411
412 update_perf_cpu_limits();
413
414 return 0;
415}
416
417/*
418 * perf samples are done in some very critical code paths (NMIs).
419 * If they take too much CPU time, the system can lock up and not
420 * get any real work done. This will drop the sample rate when
421 * we detect that events are taking too long.
422 */
423#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200424static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700425
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100426static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700427{
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100428 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Dave Hansen14c63f12013-06-21 08:51:36 -0700429 u64 avg_local_sample_len;
Stephane Eraniane5302922013-07-05 00:30:11 +0200430 u64 local_samples_len;
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100431
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500432 local_samples_len = __this_cpu_read(running_sample_length);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100433 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
434
435 printk_ratelimited(KERN_WARNING
436 "perf interrupt took too long (%lld > %lld), lowering "
437 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100438 avg_local_sample_len, allowed_ns >> 1,
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100439 sysctl_perf_event_sample_rate);
440}
441
442static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
443
444void perf_sample_event_took(u64 sample_len_ns)
445{
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200446 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100447 u64 avg_local_sample_len;
448 u64 local_samples_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700449
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200450 if (allowed_ns == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700451 return;
452
453 /* decay the counter by 1 average sample */
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500454 local_samples_len = __this_cpu_read(running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700455 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
456 local_samples_len += sample_len_ns;
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500457 __this_cpu_write(running_sample_length, local_samples_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700458
459 /*
460 * note: this will be biased artifically low until we have
461 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
462 * from having to maintain a count.
463 */
464 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
465
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200466 if (avg_local_sample_len <= allowed_ns)
Dave Hansen14c63f12013-06-21 08:51:36 -0700467 return;
468
469 if (max_samples_per_tick <= 1)
470 return;
471
472 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
473 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
474 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
475
Dave Hansen14c63f12013-06-21 08:51:36 -0700476 update_perf_cpu_limits();
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100477
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100478 if (!irq_work_queue(&perf_duration_work)) {
479 early_printk("perf interrupt took too long (%lld > %lld), lowering "
480 "kernel.perf_event_max_sample_rate to %d\n",
481 avg_local_sample_len, allowed_ns >> 1,
482 sysctl_perf_event_sample_rate);
483 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700484}
485
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200486static atomic64_t perf_event_id;
487
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200488static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
489 enum event_type_t event_type);
490
491static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200492 enum event_type_t event_type,
493 struct task_struct *task);
494
495static void update_context_time(struct perf_event_context *ctx);
496static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200497
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200498void __weak perf_event_print_debug(void) { }
499
Matt Fleming84c79912010-10-03 21:41:13 +0100500extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200501{
Matt Fleming84c79912010-10-03 21:41:13 +0100502 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200503}
504
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200505static inline u64 perf_clock(void)
506{
507 return local_clock();
508}
509
Peter Zijlstra34f43922015-02-20 14:05:38 +0100510static inline u64 perf_event_clock(struct perf_event *event)
511{
512 return event->clock();
513}
514
Stephane Eraniane5d13672011-02-14 11:20:01 +0200515#ifdef CONFIG_CGROUP_PERF
516
Stephane Eraniane5d13672011-02-14 11:20:01 +0200517static inline bool
518perf_cgroup_match(struct perf_event *event)
519{
520 struct perf_event_context *ctx = event->ctx;
521 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
522
Tejun Heoef824fa2013-04-08 19:00:38 -0700523 /* @event doesn't care about cgroup */
524 if (!event->cgrp)
525 return true;
526
527 /* wants specific cgroup scope but @cpuctx isn't associated with any */
528 if (!cpuctx->cgrp)
529 return false;
530
531 /*
532 * Cgroup scoping is recursive. An event enabled for a cgroup is
533 * also enabled for all its descendant cgroups. If @cpuctx's
534 * cgroup is a descendant of @event's (the test covers identity
535 * case), it's a match.
536 */
537 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
538 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200539}
540
Stephane Eraniane5d13672011-02-14 11:20:01 +0200541static inline void perf_detach_cgroup(struct perf_event *event)
542{
Zefan Li4e2ba652014-09-19 16:53:14 +0800543 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200544 event->cgrp = NULL;
545}
546
547static inline int is_cgroup_event(struct perf_event *event)
548{
549 return event->cgrp != NULL;
550}
551
552static inline u64 perf_cgroup_event_time(struct perf_event *event)
553{
554 struct perf_cgroup_info *t;
555
556 t = per_cpu_ptr(event->cgrp->info, event->cpu);
557 return t->time;
558}
559
560static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
561{
562 struct perf_cgroup_info *info;
563 u64 now;
564
565 now = perf_clock();
566
567 info = this_cpu_ptr(cgrp->info);
568
569 info->time += now - info->timestamp;
570 info->timestamp = now;
571}
572
573static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
574{
575 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
576 if (cgrp_out)
577 __update_cgrp_time(cgrp_out);
578}
579
580static inline void update_cgrp_time_from_event(struct perf_event *event)
581{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200582 struct perf_cgroup *cgrp;
583
Stephane Eraniane5d13672011-02-14 11:20:01 +0200584 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200585 * ensure we access cgroup data only when needed and
586 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200587 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200588 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200589 return;
590
Stephane Eranian614e4c42015-11-12 11:00:04 +0100591 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200592 /*
593 * Do not update time when cgroup is not active
594 */
595 if (cgrp == event->cgrp)
596 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200597}
598
599static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200600perf_cgroup_set_timestamp(struct task_struct *task,
601 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200602{
603 struct perf_cgroup *cgrp;
604 struct perf_cgroup_info *info;
605
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200606 /*
607 * ctx->lock held by caller
608 * ensure we do not access cgroup data
609 * unless we have the cgroup pinned (css_get)
610 */
611 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200612 return;
613
Stephane Eranian614e4c42015-11-12 11:00:04 +0100614 cgrp = perf_cgroup_from_task(task, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200615 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200616 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200617}
618
619#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
620#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
621
622/*
623 * reschedule events based on the cgroup constraint of task.
624 *
625 * mode SWOUT : schedule out everything
626 * mode SWIN : schedule in based on cgroup for next
627 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800628static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200629{
630 struct perf_cpu_context *cpuctx;
631 struct pmu *pmu;
632 unsigned long flags;
633
634 /*
635 * disable interrupts to avoid geting nr_cgroup
636 * changes via __perf_event_disable(). Also
637 * avoids preemption.
638 */
639 local_irq_save(flags);
640
641 /*
642 * we reschedule only in the presence of cgroup
643 * constrained events.
644 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200645
646 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200647 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200648 if (cpuctx->unique_pmu != pmu)
649 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200650
Stephane Eraniane5d13672011-02-14 11:20:01 +0200651 /*
652 * perf_cgroup_events says at least one
653 * context on this CPU has cgroup events.
654 *
655 * ctx->nr_cgroups reports the number of cgroup
656 * events for a context.
657 */
658 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200659 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
660 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200661
662 if (mode & PERF_CGROUP_SWOUT) {
663 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
664 /*
665 * must not be done before ctxswout due
666 * to event_filter_match() in event_sched_out()
667 */
668 cpuctx->cgrp = NULL;
669 }
670
671 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200672 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200673 /*
674 * set cgrp before ctxsw in to allow
675 * event_filter_match() to not have to pass
676 * task around
Stephane Eranian614e4c42015-11-12 11:00:04 +0100677 * we pass the cpuctx->ctx to perf_cgroup_from_task()
678 * because cgorup events are only per-cpu
Stephane Eraniane5d13672011-02-14 11:20:01 +0200679 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100680 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200681 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
682 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200683 perf_pmu_enable(cpuctx->ctx.pmu);
684 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200685 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200686 }
687
Stephane Eraniane5d13672011-02-14 11:20:01 +0200688 local_irq_restore(flags);
689}
690
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200691static inline void perf_cgroup_sched_out(struct task_struct *task,
692 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200693{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200694 struct perf_cgroup *cgrp1;
695 struct perf_cgroup *cgrp2 = NULL;
696
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100697 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200698 /*
699 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100700 * we do not need to pass the ctx here because we know
701 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200702 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100703 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100704 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200705
706 /*
707 * only schedule out current cgroup events if we know
708 * that we are switching to a different cgroup. Otherwise,
709 * do no touch the cgroup events.
710 */
711 if (cgrp1 != cgrp2)
712 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100713
714 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200715}
716
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200717static inline void perf_cgroup_sched_in(struct task_struct *prev,
718 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200719{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200720 struct perf_cgroup *cgrp1;
721 struct perf_cgroup *cgrp2 = NULL;
722
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100723 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200724 /*
725 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100726 * we do not need to pass the ctx here because we know
727 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200728 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100729 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100730 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200731
732 /*
733 * only need to schedule in cgroup events if we are changing
734 * cgroup during ctxsw. Cgroup events were not scheduled
735 * out of ctxsw out if that was not the case.
736 */
737 if (cgrp1 != cgrp2)
738 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100739
740 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200741}
742
743static inline int perf_cgroup_connect(int fd, struct perf_event *event,
744 struct perf_event_attr *attr,
745 struct perf_event *group_leader)
746{
747 struct perf_cgroup *cgrp;
748 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400749 struct fd f = fdget(fd);
750 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200751
Al Viro2903ff02012-08-28 12:52:22 -0400752 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200753 return -EBADF;
754
Al Virob5830432014-10-31 01:22:04 -0400755 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400756 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800757 if (IS_ERR(css)) {
758 ret = PTR_ERR(css);
759 goto out;
760 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200761
762 cgrp = container_of(css, struct perf_cgroup, css);
763 event->cgrp = cgrp;
764
765 /*
766 * all events in a group must monitor
767 * the same cgroup because a task belongs
768 * to only one perf cgroup at a time
769 */
770 if (group_leader && group_leader->cgrp != cgrp) {
771 perf_detach_cgroup(event);
772 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200773 }
Li Zefan3db272c2011-03-03 14:25:37 +0800774out:
Al Viro2903ff02012-08-28 12:52:22 -0400775 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200776 return ret;
777}
778
779static inline void
780perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
781{
782 struct perf_cgroup_info *t;
783 t = per_cpu_ptr(event->cgrp->info, event->cpu);
784 event->shadow_ctx_time = now - t->timestamp;
785}
786
787static inline void
788perf_cgroup_defer_enabled(struct perf_event *event)
789{
790 /*
791 * when the current task's perf cgroup does not match
792 * the event's, we need to remember to call the
793 * perf_mark_enable() function the first time a task with
794 * a matching perf cgroup is scheduled in.
795 */
796 if (is_cgroup_event(event) && !perf_cgroup_match(event))
797 event->cgrp_defer_enabled = 1;
798}
799
800static inline void
801perf_cgroup_mark_enabled(struct perf_event *event,
802 struct perf_event_context *ctx)
803{
804 struct perf_event *sub;
805 u64 tstamp = perf_event_time(event);
806
807 if (!event->cgrp_defer_enabled)
808 return;
809
810 event->cgrp_defer_enabled = 0;
811
812 event->tstamp_enabled = tstamp - event->total_time_enabled;
813 list_for_each_entry(sub, &event->sibling_list, group_entry) {
814 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
815 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
816 sub->cgrp_defer_enabled = 0;
817 }
818 }
819}
820#else /* !CONFIG_CGROUP_PERF */
821
822static inline bool
823perf_cgroup_match(struct perf_event *event)
824{
825 return true;
826}
827
828static inline void perf_detach_cgroup(struct perf_event *event)
829{}
830
831static inline int is_cgroup_event(struct perf_event *event)
832{
833 return 0;
834}
835
836static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
837{
838 return 0;
839}
840
841static inline void update_cgrp_time_from_event(struct perf_event *event)
842{
843}
844
845static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
846{
847}
848
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200849static inline void perf_cgroup_sched_out(struct task_struct *task,
850 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200851{
852}
853
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200854static inline void perf_cgroup_sched_in(struct task_struct *prev,
855 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200856{
857}
858
859static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
860 struct perf_event_attr *attr,
861 struct perf_event *group_leader)
862{
863 return -EINVAL;
864}
865
866static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200867perf_cgroup_set_timestamp(struct task_struct *task,
868 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200869{
870}
871
872void
873perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
874{
875}
876
877static inline void
878perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
879{
880}
881
882static inline u64 perf_cgroup_event_time(struct perf_event *event)
883{
884 return 0;
885}
886
887static inline void
888perf_cgroup_defer_enabled(struct perf_event *event)
889{
890}
891
892static inline void
893perf_cgroup_mark_enabled(struct perf_event *event,
894 struct perf_event_context *ctx)
895{
896}
897#endif
898
Stephane Eranian9e630202013-04-03 14:21:33 +0200899/*
900 * set default to be dependent on timer tick just
901 * like original code
902 */
903#define PERF_CPU_HRTIMER (1000 / HZ)
904/*
905 * function must be called with interrupts disbled
906 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200907static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +0200908{
909 struct perf_cpu_context *cpuctx;
Stephane Eranian9e630202013-04-03 14:21:33 +0200910 int rotations = 0;
911
912 WARN_ON(!irqs_disabled());
913
914 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200915 rotations = perf_rotate_context(cpuctx);
916
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200917 raw_spin_lock(&cpuctx->hrtimer_lock);
918 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +0200919 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200920 else
921 cpuctx->hrtimer_active = 0;
922 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +0200923
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200924 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +0200925}
926
Peter Zijlstra272325c2015-04-15 11:41:58 +0200927static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +0200928{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200929 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200930 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +0200931 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +0200932
933 /* no multiplexing needed for SW PMU */
934 if (pmu->task_ctx_nr == perf_sw_context)
935 return;
936
Stephane Eranian62b85632013-04-03 14:21:34 +0200937 /*
938 * check default is sane, if not set then force to
939 * default interval (1/tick)
940 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200941 interval = pmu->hrtimer_interval_ms;
942 if (interval < 1)
943 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +0200944
Peter Zijlstra272325c2015-04-15 11:41:58 +0200945 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +0200946
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200947 raw_spin_lock_init(&cpuctx->hrtimer_lock);
948 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra272325c2015-04-15 11:41:58 +0200949 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +0200950}
951
Peter Zijlstra272325c2015-04-15 11:41:58 +0200952static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +0200953{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200954 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200955 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200956 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +0200957
958 /* not for SW PMU */
959 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +0200960 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +0200961
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200962 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
963 if (!cpuctx->hrtimer_active) {
964 cpuctx->hrtimer_active = 1;
965 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
966 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
967 }
968 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +0200969
Peter Zijlstra272325c2015-04-15 11:41:58 +0200970 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +0200971}
972
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200973void perf_pmu_disable(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_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200978}
979
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200980void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200981{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200982 int *count = this_cpu_ptr(pmu->pmu_disable_count);
983 if (!--(*count))
984 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200985}
986
Mark Rutland2fde4f92015-01-07 15:01:54 +0000987static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200988
989/*
Mark Rutland2fde4f92015-01-07 15:01:54 +0000990 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
991 * perf_event_task_tick() are fully serialized because they're strictly cpu
992 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
993 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200994 */
Mark Rutland2fde4f92015-01-07 15:01:54 +0000995static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200996{
Mark Rutland2fde4f92015-01-07 15:01:54 +0000997 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200998
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200999 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001000
Mark Rutland2fde4f92015-01-07 15:01:54 +00001001 WARN_ON(!list_empty(&ctx->active_ctx_list));
1002
1003 list_add(&ctx->active_ctx_list, head);
1004}
1005
1006static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1007{
1008 WARN_ON(!irqs_disabled());
1009
1010 WARN_ON(list_empty(&ctx->active_ctx_list));
1011
1012 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001013}
1014
1015static void get_ctx(struct perf_event_context *ctx)
1016{
1017 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
1018}
1019
Yan, Zheng4af57ef2014-11-04 21:56:01 -05001020static void free_ctx(struct rcu_head *head)
1021{
1022 struct perf_event_context *ctx;
1023
1024 ctx = container_of(head, struct perf_event_context, rcu_head);
1025 kfree(ctx->task_ctx_data);
1026 kfree(ctx);
1027}
1028
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001029static void put_ctx(struct perf_event_context *ctx)
1030{
1031 if (atomic_dec_and_test(&ctx->refcount)) {
1032 if (ctx->parent_ctx)
1033 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001034 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001035 put_task_struct(ctx->task);
Yan, Zheng4af57ef2014-11-04 21:56:01 -05001036 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001037 }
1038}
1039
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001040/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001041 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1042 * perf_pmu_migrate_context() we need some magic.
1043 *
1044 * Those places that change perf_event::ctx will hold both
1045 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1046 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001047 * Lock ordering is by mutex address. There are two other sites where
1048 * perf_event_context::mutex nests and those are:
1049 *
1050 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001051 * perf_event_exit_event()
1052 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001053 *
1054 * - perf_event_init_context() [ parent, 0 ]
1055 * inherit_task_group()
1056 * inherit_group()
1057 * inherit_event()
1058 * perf_event_alloc()
1059 * perf_init_event()
1060 * perf_try_init_event() [ child , 1 ]
1061 *
1062 * While it appears there is an obvious deadlock here -- the parent and child
1063 * nesting levels are inverted between the two. This is in fact safe because
1064 * life-time rules separate them. That is an exiting task cannot fork, and a
1065 * spawning task cannot (yet) exit.
1066 *
1067 * But remember that that these are parent<->child context relations, and
1068 * migration does not affect children, therefore these two orderings should not
1069 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001070 *
1071 * The change in perf_event::ctx does not affect children (as claimed above)
1072 * because the sys_perf_event_open() case will install a new event and break
1073 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1074 * concerned with cpuctx and that doesn't have children.
1075 *
1076 * The places that change perf_event::ctx will issue:
1077 *
1078 * perf_remove_from_context();
1079 * synchronize_rcu();
1080 * perf_install_in_context();
1081 *
1082 * to affect the change. The remove_from_context() + synchronize_rcu() should
1083 * quiesce the event, after which we can install it in the new location. This
1084 * means that only external vectors (perf_fops, prctl) can perturb the event
1085 * while in transit. Therefore all such accessors should also acquire
1086 * perf_event_context::mutex to serialize against this.
1087 *
1088 * However; because event->ctx can change while we're waiting to acquire
1089 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1090 * function.
1091 *
1092 * Lock order:
1093 * task_struct::perf_event_mutex
1094 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001095 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001096 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001097 * perf_event::mmap_mutex
1098 * mmap_sem
1099 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001100static struct perf_event_context *
1101perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001102{
1103 struct perf_event_context *ctx;
1104
1105again:
1106 rcu_read_lock();
1107 ctx = ACCESS_ONCE(event->ctx);
1108 if (!atomic_inc_not_zero(&ctx->refcount)) {
1109 rcu_read_unlock();
1110 goto again;
1111 }
1112 rcu_read_unlock();
1113
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001114 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001115 if (event->ctx != ctx) {
1116 mutex_unlock(&ctx->mutex);
1117 put_ctx(ctx);
1118 goto again;
1119 }
1120
1121 return ctx;
1122}
1123
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001124static inline struct perf_event_context *
1125perf_event_ctx_lock(struct perf_event *event)
1126{
1127 return perf_event_ctx_lock_nested(event, 0);
1128}
1129
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001130static void perf_event_ctx_unlock(struct perf_event *event,
1131 struct perf_event_context *ctx)
1132{
1133 mutex_unlock(&ctx->mutex);
1134 put_ctx(ctx);
1135}
1136
1137/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001138 * This must be done under the ctx->lock, such as to serialize against
1139 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1140 * calling scheduler related locks and ctx->lock nests inside those.
1141 */
1142static __must_check struct perf_event_context *
1143unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001144{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001145 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1146
1147 lockdep_assert_held(&ctx->lock);
1148
1149 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001150 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001151 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001152
1153 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001154}
1155
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001156static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1157{
1158 /*
1159 * only top level events have the pid namespace they were created in
1160 */
1161 if (event->parent)
1162 event = event->parent;
1163
1164 return task_tgid_nr_ns(p, event->ns);
1165}
1166
1167static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1168{
1169 /*
1170 * only top level events have the pid namespace they were created in
1171 */
1172 if (event->parent)
1173 event = event->parent;
1174
1175 return task_pid_nr_ns(p, event->ns);
1176}
1177
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001178/*
1179 * If we inherit events we want to return the parent event id
1180 * to userspace.
1181 */
1182static u64 primary_event_id(struct perf_event *event)
1183{
1184 u64 id = event->id;
1185
1186 if (event->parent)
1187 id = event->parent->id;
1188
1189 return id;
1190}
1191
1192/*
1193 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001194 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001195 * This has to cope with with the fact that until it is locked,
1196 * the context could get moved to another task.
1197 */
1198static struct perf_event_context *
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001199perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001200{
1201 struct perf_event_context *ctx;
1202
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001203retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001204 /*
1205 * One of the few rules of preemptible RCU is that one cannot do
1206 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001207 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001208 * rcu_read_unlock_special().
1209 *
1210 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001211 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001212 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001213 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001214 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001215 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001216 if (ctx) {
1217 /*
1218 * If this context is a clone of another, it might
1219 * get swapped for another underneath us by
1220 * perf_event_task_sched_out, though the
1221 * rcu_read_lock() protects us from any context
1222 * getting freed. Lock the context and check if it
1223 * got swapped before we could get the lock, and retry
1224 * if so. If we locked the right context, then it
1225 * can't get swapped on us any more.
1226 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001227 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001228 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001229 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001230 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001231 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001232 goto retry;
1233 }
1234
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001235 if (ctx->task == TASK_TOMBSTONE ||
1236 !atomic_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001237 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001238 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001239 } else {
1240 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001241 }
1242 }
1243 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001244 if (!ctx)
1245 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001246 return ctx;
1247}
1248
1249/*
1250 * Get the context for a task and increment its pin_count so it
1251 * can't get swapped to another task. This also increments its
1252 * reference count so that the context can't get freed.
1253 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001254static struct perf_event_context *
1255perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001256{
1257 struct perf_event_context *ctx;
1258 unsigned long flags;
1259
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001260 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001261 if (ctx) {
1262 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001263 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001264 }
1265 return ctx;
1266}
1267
1268static void perf_unpin_context(struct perf_event_context *ctx)
1269{
1270 unsigned long flags;
1271
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001272 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001273 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001274 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001275}
1276
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001277/*
1278 * Update the record of the current time in a context.
1279 */
1280static void update_context_time(struct perf_event_context *ctx)
1281{
1282 u64 now = perf_clock();
1283
1284 ctx->time += now - ctx->timestamp;
1285 ctx->timestamp = now;
1286}
1287
Stephane Eranian41587552011-01-03 18:20:01 +02001288static u64 perf_event_time(struct perf_event *event)
1289{
1290 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001291
1292 if (is_cgroup_event(event))
1293 return perf_cgroup_event_time(event);
1294
Stephane Eranian41587552011-01-03 18:20:01 +02001295 return ctx ? ctx->time : 0;
1296}
1297
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001298/*
1299 * Update the total_time_enabled and total_time_running fields for a event.
1300 */
1301static void update_event_times(struct perf_event *event)
1302{
1303 struct perf_event_context *ctx = event->ctx;
1304 u64 run_end;
1305
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001306 lockdep_assert_held(&ctx->lock);
1307
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001308 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1309 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1310 return;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001311
Stephane Eraniane5d13672011-02-14 11:20:01 +02001312 /*
1313 * in cgroup mode, time_enabled represents
1314 * the time the event was enabled AND active
1315 * tasks were in the monitored cgroup. This is
1316 * independent of the activity of the context as
1317 * there may be a mix of cgroup and non-cgroup events.
1318 *
1319 * That is why we treat cgroup events differently
1320 * here.
1321 */
1322 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001323 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001324 else if (ctx->is_active)
1325 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001326 else
1327 run_end = event->tstamp_stopped;
1328
1329 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001330
1331 if (event->state == PERF_EVENT_STATE_INACTIVE)
1332 run_end = event->tstamp_stopped;
1333 else
Stephane Eranian41587552011-01-03 18:20:01 +02001334 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001335
1336 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001337
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001338}
1339
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001340/*
1341 * Update total_time_enabled and total_time_running for all events in a group.
1342 */
1343static void update_group_times(struct perf_event *leader)
1344{
1345 struct perf_event *event;
1346
1347 update_event_times(leader);
1348 list_for_each_entry(event, &leader->sibling_list, group_entry)
1349 update_event_times(event);
1350}
1351
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001352static struct list_head *
1353ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1354{
1355 if (event->attr.pinned)
1356 return &ctx->pinned_groups;
1357 else
1358 return &ctx->flexible_groups;
1359}
1360
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001361/*
1362 * Add a event from the lists for its context.
1363 * Must be called with ctx->mutex and ctx->lock held.
1364 */
1365static void
1366list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1367{
Peter Zijlstrac994d612016-01-08 09:20:23 +01001368 lockdep_assert_held(&ctx->lock);
1369
Peter Zijlstra8a495422010-05-27 15:47:49 +02001370 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1371 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001372
1373 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001374 * If we're a stand alone event or group leader, we go to the context
1375 * list, group events are kept attached to the group so that
1376 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001377 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001378 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001379 struct list_head *list;
1380
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001381 if (is_software_event(event))
1382 event->group_flags |= PERF_GROUP_SOFTWARE;
1383
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001384 list = ctx_group_list(event, ctx);
1385 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001386 }
1387
Peter Zijlstra08309372011-03-03 11:31:20 +01001388 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001389 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001390
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001391 list_add_rcu(&event->event_entry, &ctx->event_list);
1392 ctx->nr_events++;
1393 if (event->attr.inherit_stat)
1394 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001395
1396 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001397}
1398
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001399/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001400 * Initialize event state based on the perf_event_attr::disabled.
1401 */
1402static inline void perf_event__state_init(struct perf_event *event)
1403{
1404 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1405 PERF_EVENT_STATE_INACTIVE;
1406}
1407
Peter Zijlstraa7239682015-09-09 19:06:33 +02001408static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001409{
1410 int entry = sizeof(u64); /* value */
1411 int size = 0;
1412 int nr = 1;
1413
1414 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1415 size += sizeof(u64);
1416
1417 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1418 size += sizeof(u64);
1419
1420 if (event->attr.read_format & PERF_FORMAT_ID)
1421 entry += sizeof(u64);
1422
1423 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001424 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001425 size += sizeof(u64);
1426 }
1427
1428 size += entry * nr;
1429 event->read_size = size;
1430}
1431
Peter Zijlstraa7239682015-09-09 19:06:33 +02001432static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001433{
1434 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001435 u16 size = 0;
1436
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001437 if (sample_type & PERF_SAMPLE_IP)
1438 size += sizeof(data->ip);
1439
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001440 if (sample_type & PERF_SAMPLE_ADDR)
1441 size += sizeof(data->addr);
1442
1443 if (sample_type & PERF_SAMPLE_PERIOD)
1444 size += sizeof(data->period);
1445
Andi Kleenc3feedf2013-01-24 16:10:28 +01001446 if (sample_type & PERF_SAMPLE_WEIGHT)
1447 size += sizeof(data->weight);
1448
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001449 if (sample_type & PERF_SAMPLE_READ)
1450 size += event->read_size;
1451
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001452 if (sample_type & PERF_SAMPLE_DATA_SRC)
1453 size += sizeof(data->data_src.val);
1454
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001455 if (sample_type & PERF_SAMPLE_TRANSACTION)
1456 size += sizeof(data->txn);
1457
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001458 event->header_size = size;
1459}
1460
Peter Zijlstraa7239682015-09-09 19:06:33 +02001461/*
1462 * Called at perf_event creation and when events are attached/detached from a
1463 * group.
1464 */
1465static void perf_event__header_size(struct perf_event *event)
1466{
1467 __perf_event_read_size(event,
1468 event->group_leader->nr_siblings);
1469 __perf_event_header_size(event, event->attr.sample_type);
1470}
1471
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001472static void perf_event__id_header_size(struct perf_event *event)
1473{
1474 struct perf_sample_data *data;
1475 u64 sample_type = event->attr.sample_type;
1476 u16 size = 0;
1477
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001478 if (sample_type & PERF_SAMPLE_TID)
1479 size += sizeof(data->tid_entry);
1480
1481 if (sample_type & PERF_SAMPLE_TIME)
1482 size += sizeof(data->time);
1483
Adrian Hunterff3d5272013-08-27 11:23:07 +03001484 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1485 size += sizeof(data->id);
1486
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001487 if (sample_type & PERF_SAMPLE_ID)
1488 size += sizeof(data->id);
1489
1490 if (sample_type & PERF_SAMPLE_STREAM_ID)
1491 size += sizeof(data->stream_id);
1492
1493 if (sample_type & PERF_SAMPLE_CPU)
1494 size += sizeof(data->cpu_entry);
1495
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001496 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001497}
1498
Peter Zijlstraa7239682015-09-09 19:06:33 +02001499static bool perf_event_validate_size(struct perf_event *event)
1500{
1501 /*
1502 * The values computed here will be over-written when we actually
1503 * attach the event.
1504 */
1505 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1506 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1507 perf_event__id_header_size(event);
1508
1509 /*
1510 * Sum the lot; should not exceed the 64k limit we have on records.
1511 * Conservative limit to allow for callchains and other variable fields.
1512 */
1513 if (event->read_size + event->header_size +
1514 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1515 return false;
1516
1517 return true;
1518}
1519
Peter Zijlstra8a495422010-05-27 15:47:49 +02001520static void perf_group_attach(struct perf_event *event)
1521{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001522 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001523
Peter Zijlstra74c33372010-10-15 11:40:29 +02001524 /*
1525 * We can have double attach due to group movement in perf_event_open.
1526 */
1527 if (event->attach_state & PERF_ATTACH_GROUP)
1528 return;
1529
Peter Zijlstra8a495422010-05-27 15:47:49 +02001530 event->attach_state |= PERF_ATTACH_GROUP;
1531
1532 if (group_leader == event)
1533 return;
1534
Peter Zijlstra652884f2015-01-23 11:20:10 +01001535 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1536
Peter Zijlstra8a495422010-05-27 15:47:49 +02001537 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1538 !is_software_event(event))
1539 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1540
1541 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1542 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001543
1544 perf_event__header_size(group_leader);
1545
1546 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1547 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001548}
1549
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001550/*
1551 * Remove a event from the lists for its context.
1552 * Must be called with ctx->mutex and ctx->lock held.
1553 */
1554static void
1555list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1556{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001557 struct perf_cpu_context *cpuctx;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001558
1559 WARN_ON_ONCE(event->ctx != ctx);
1560 lockdep_assert_held(&ctx->lock);
1561
Peter Zijlstra8a495422010-05-27 15:47:49 +02001562 /*
1563 * We can have double detach due to exit/hot-unplug + close.
1564 */
1565 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001566 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001567
1568 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1569
Stephane Eranian68cacd22011-03-23 16:03:06 +01001570 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001571 ctx->nr_cgroups--;
Peter Zijlstra70a01652016-01-08 09:29:16 +01001572 /*
1573 * Because cgroup events are always per-cpu events, this will
1574 * always be called from the right CPU.
1575 */
Stephane Eranian68cacd22011-03-23 16:03:06 +01001576 cpuctx = __get_cpu_context(ctx);
1577 /*
Peter Zijlstra70a01652016-01-08 09:29:16 +01001578 * If there are no more cgroup events then clear cgrp to avoid
1579 * stale pointer in update_cgrp_time_from_cpuctx().
Stephane Eranian68cacd22011-03-23 16:03:06 +01001580 */
1581 if (!ctx->nr_cgroups)
1582 cpuctx->cgrp = NULL;
1583 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001584
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001585 ctx->nr_events--;
1586 if (event->attr.inherit_stat)
1587 ctx->nr_stat--;
1588
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001589 list_del_rcu(&event->event_entry);
1590
Peter Zijlstra8a495422010-05-27 15:47:49 +02001591 if (event->group_leader == event)
1592 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001593
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001594 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001595
1596 /*
1597 * If event was in error state, then keep it
1598 * that way, otherwise bogus counts will be
1599 * returned on read(). The only way to get out
1600 * of error state is by explicit re-enabling
1601 * of the event
1602 */
1603 if (event->state > PERF_EVENT_STATE_OFF)
1604 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001605
1606 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001607}
1608
Peter Zijlstra8a495422010-05-27 15:47:49 +02001609static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001610{
1611 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001612 struct list_head *list = NULL;
1613
1614 /*
1615 * We can have double detach due to exit/hot-unplug + close.
1616 */
1617 if (!(event->attach_state & PERF_ATTACH_GROUP))
1618 return;
1619
1620 event->attach_state &= ~PERF_ATTACH_GROUP;
1621
1622 /*
1623 * If this is a sibling, remove it from its group.
1624 */
1625 if (event->group_leader != event) {
1626 list_del_init(&event->group_entry);
1627 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001628 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001629 }
1630
1631 if (!list_empty(&event->group_entry))
1632 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001633
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001634 /*
1635 * If this was a group event with sibling events then
1636 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001637 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001638 */
1639 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001640 if (list)
1641 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001642 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001643
1644 /* Inherit group flags from the previous leader */
1645 sibling->group_flags = event->group_flags;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001646
1647 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001648 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001649
1650out:
1651 perf_event__header_size(event->group_leader);
1652
1653 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1654 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001655}
1656
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001657static bool is_orphaned_event(struct perf_event *event)
1658{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01001659 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001660}
1661
Mark Rutland66eb5792015-05-13 17:12:23 +01001662static inline int pmu_filter_match(struct perf_event *event)
1663{
1664 struct pmu *pmu = event->pmu;
1665 return pmu->filter_match ? pmu->filter_match(event) : 1;
1666}
1667
Stephane Eranianfa66f072010-08-26 16:40:01 +02001668static inline int
1669event_filter_match(struct perf_event *event)
1670{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001671 return (event->cpu == -1 || event->cpu == smp_processor_id())
Mark Rutland66eb5792015-05-13 17:12:23 +01001672 && perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001673}
1674
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001675static void
1676event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001677 struct perf_cpu_context *cpuctx,
1678 struct perf_event_context *ctx)
1679{
Stephane Eranian41587552011-01-03 18:20:01 +02001680 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001681 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001682
1683 WARN_ON_ONCE(event->ctx != ctx);
1684 lockdep_assert_held(&ctx->lock);
1685
Stephane Eranianfa66f072010-08-26 16:40:01 +02001686 /*
1687 * An event which could not be activated because of
1688 * filter mismatch still needs to have its timings
1689 * maintained, otherwise bogus information is return
1690 * via read() for time_enabled, time_running:
1691 */
1692 if (event->state == PERF_EVENT_STATE_INACTIVE
1693 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001694 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001695 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001696 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001697 }
1698
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001699 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001700 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001701
Alexander Shishkin44377272013-12-16 14:17:36 +02001702 perf_pmu_disable(event->pmu);
1703
Peter Zijlstra28a967c2016-02-24 18:45:46 +01001704 event->tstamp_stopped = tstamp;
1705 event->pmu->del(event, 0);
1706 event->oncpu = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001707 event->state = PERF_EVENT_STATE_INACTIVE;
1708 if (event->pending_disable) {
1709 event->pending_disable = 0;
1710 event->state = PERF_EVENT_STATE_OFF;
1711 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001712
1713 if (!is_software_event(event))
1714 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001715 if (!--ctx->nr_active)
1716 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001717 if (event->attr.freq && event->attr.sample_freq)
1718 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001719 if (event->attr.exclusive || !cpuctx->active_oncpu)
1720 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001721
1722 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001723}
1724
1725static void
1726group_sched_out(struct perf_event *group_event,
1727 struct perf_cpu_context *cpuctx,
1728 struct perf_event_context *ctx)
1729{
1730 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001731 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001732
1733 event_sched_out(group_event, cpuctx, ctx);
1734
1735 /*
1736 * Schedule out siblings (if any):
1737 */
1738 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1739 event_sched_out(event, cpuctx, ctx);
1740
Stephane Eranianfa66f072010-08-26 16:40:01 +02001741 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001742 cpuctx->exclusive = 0;
1743}
1744
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001745#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01001746
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001747/*
1748 * Cross CPU call to remove a performance event
1749 *
1750 * We disable the event on the hardware level first. After that we
1751 * remove it from the context list.
1752 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001753static void
1754__perf_remove_from_context(struct perf_event *event,
1755 struct perf_cpu_context *cpuctx,
1756 struct perf_event_context *ctx,
1757 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001758{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001759 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001760
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001761 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001762 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001763 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001764 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001765
Peter Zijlstra39a43642016-01-11 12:46:35 +01001766 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001767 ctx->is_active = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01001768 if (ctx->task) {
1769 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1770 cpuctx->task_ctx = NULL;
1771 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001772 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001773}
1774
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001775/*
1776 * Remove the event from a task's (or a CPU's) list of events.
1777 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001778 * If event->ctx is a cloned context, callers must make sure that
1779 * every task struct that event->ctx->task could possibly point to
1780 * remains valid. This is OK when called from perf_release since
1781 * that only calls us on the top-level context, which can't be a clone.
1782 * When called from perf_event_exit_task, it's OK because the
1783 * context has been detached from its task.
1784 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001785static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001786{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001787 lockdep_assert_held(&event->ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001788
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001789 event_function_call(event, __perf_remove_from_context, (void *)flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001790}
1791
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001792/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001793 * Cross CPU call to disable a performance event
1794 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001795static void __perf_event_disable(struct perf_event *event,
1796 struct perf_cpu_context *cpuctx,
1797 struct perf_event_context *ctx,
1798 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001799{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001800 if (event->state < PERF_EVENT_STATE_INACTIVE)
1801 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001802
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001803 update_context_time(ctx);
1804 update_cgrp_time_from_event(event);
1805 update_group_times(event);
1806 if (event == event->group_leader)
1807 group_sched_out(event, cpuctx, ctx);
1808 else
1809 event_sched_out(event, cpuctx, ctx);
1810 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra7b648012015-12-03 18:35:21 +01001811}
1812
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001813/*
1814 * Disable a event.
1815 *
1816 * If event->ctx is a cloned context, callers must make sure that
1817 * every task struct that event->ctx->task could possibly point to
1818 * remains valid. This condition is satisifed when called through
1819 * perf_event_for_each_child or perf_event_for_each because they
1820 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001821 * goes to exit will block in perf_event_exit_event().
1822 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001823 * When called from perf_pending_event it's OK because event->ctx
1824 * is the current context on this CPU and preemption is disabled,
1825 * hence we can't get into perf_event_task_sched_out for this context.
1826 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001827static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001828{
1829 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001830
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001831 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001832 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001833 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001834 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001835 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001836 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001837
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001838 event_function_call(event, __perf_event_disable, NULL);
1839}
1840
1841void perf_event_disable_local(struct perf_event *event)
1842{
1843 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001844}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001845
1846/*
1847 * Strictly speaking kernel users cannot create groups and therefore this
1848 * interface does not need the perf_event_ctx_lock() magic.
1849 */
1850void perf_event_disable(struct perf_event *event)
1851{
1852 struct perf_event_context *ctx;
1853
1854 ctx = perf_event_ctx_lock(event);
1855 _perf_event_disable(event);
1856 perf_event_ctx_unlock(event, ctx);
1857}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001858EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001859
Stephane Eraniane5d13672011-02-14 11:20:01 +02001860static void perf_set_shadow_time(struct perf_event *event,
1861 struct perf_event_context *ctx,
1862 u64 tstamp)
1863{
1864 /*
1865 * use the correct time source for the time snapshot
1866 *
1867 * We could get by without this by leveraging the
1868 * fact that to get to this function, the caller
1869 * has most likely already called update_context_time()
1870 * and update_cgrp_time_xx() and thus both timestamp
1871 * are identical (or very close). Given that tstamp is,
1872 * already adjusted for cgroup, we could say that:
1873 * tstamp - ctx->timestamp
1874 * is equivalent to
1875 * tstamp - cgrp->timestamp.
1876 *
1877 * Then, in perf_output_read(), the calculation would
1878 * work with no changes because:
1879 * - event is guaranteed scheduled in
1880 * - no scheduled out in between
1881 * - thus the timestamp would be the same
1882 *
1883 * But this is a bit hairy.
1884 *
1885 * So instead, we have an explicit cgroup call to remain
1886 * within the time time source all along. We believe it
1887 * is cleaner and simpler to understand.
1888 */
1889 if (is_cgroup_event(event))
1890 perf_cgroup_set_shadow_time(event, tstamp);
1891 else
1892 event->shadow_ctx_time = tstamp - ctx->timestamp;
1893}
1894
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001895#define MAX_INTERRUPTS (~0ULL)
1896
1897static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001898static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001899
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001900static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001901event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001902 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001903 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001904{
Stephane Eranian41587552011-01-03 18:20:01 +02001905 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001906 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001907
Peter Zijlstra63342412014-05-05 11:49:16 +02001908 lockdep_assert_held(&ctx->lock);
1909
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001910 if (event->state <= PERF_EVENT_STATE_OFF)
1911 return 0;
1912
1913 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001914 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001915
1916 /*
1917 * Unthrottle events, since we scheduled we might have missed several
1918 * ticks already, also for a heavily scheduling task there is little
1919 * guarantee it'll get a tick in a timely manner.
1920 */
1921 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1922 perf_log_throttle(event, 1);
1923 event->hw.interrupts = 0;
1924 }
1925
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001926 /*
1927 * The new state must be visible before we turn it on in the hardware:
1928 */
1929 smp_wmb();
1930
Alexander Shishkin44377272013-12-16 14:17:36 +02001931 perf_pmu_disable(event->pmu);
1932
Shaohua Li72f669c2015-02-05 15:55:31 -08001933 perf_set_shadow_time(event, ctx, tstamp);
1934
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001935 perf_log_itrace_start(event);
1936
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001937 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001938 event->state = PERF_EVENT_STATE_INACTIVE;
1939 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001940 ret = -EAGAIN;
1941 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001942 }
1943
Peter Zijlstra00a29162015-07-27 10:35:07 +02001944 event->tstamp_running += tstamp - event->tstamp_stopped;
1945
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001946 if (!is_software_event(event))
1947 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001948 if (!ctx->nr_active++)
1949 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001950 if (event->attr.freq && event->attr.sample_freq)
1951 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001952
1953 if (event->attr.exclusive)
1954 cpuctx->exclusive = 1;
1955
Alexander Shishkin44377272013-12-16 14:17:36 +02001956out:
1957 perf_pmu_enable(event->pmu);
1958
1959 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001960}
1961
1962static int
1963group_sched_in(struct perf_event *group_event,
1964 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001965 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001966{
Lin Ming6bde9b62010-04-23 13:56:00 +08001967 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001968 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001969 u64 now = ctx->time;
1970 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001971
1972 if (group_event->state == PERF_EVENT_STATE_OFF)
1973 return 0;
1974
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001975 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08001976
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001977 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001978 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001979 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001980 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001981 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001982
1983 /*
1984 * Schedule in siblings as one group (if any):
1985 */
1986 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001987 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001988 partial_group = event;
1989 goto group_error;
1990 }
1991 }
1992
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001993 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001994 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001995
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001996group_error:
1997 /*
1998 * Groups can be scheduled in as one unit only, so undo any
1999 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02002000 * The events up to the failed event are scheduled out normally,
2001 * tstamp_stopped will be updated.
2002 *
2003 * The failed events and the remaining siblings need to have
2004 * their timings updated as if they had gone thru event_sched_in()
2005 * and event_sched_out(). This is required to get consistent timings
2006 * across the group. This also takes care of the case where the group
2007 * could never be scheduled by ensuring tstamp_stopped is set to mark
2008 * the time the event was actually stopped, such that time delta
2009 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002010 */
2011 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2012 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02002013 simulate = true;
2014
2015 if (simulate) {
2016 event->tstamp_running += now - event->tstamp_stopped;
2017 event->tstamp_stopped = now;
2018 } else {
2019 event_sched_out(event, cpuctx, ctx);
2020 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002021 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002022 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002023
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002024 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002025
Peter Zijlstra272325c2015-04-15 11:41:58 +02002026 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002027
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002028 return -EAGAIN;
2029}
2030
2031/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002032 * Work out whether we can put this event group on the CPU now.
2033 */
2034static int group_can_go_on(struct perf_event *event,
2035 struct perf_cpu_context *cpuctx,
2036 int can_add_hw)
2037{
2038 /*
2039 * Groups consisting entirely of software events can always go on.
2040 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01002041 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002042 return 1;
2043 /*
2044 * If an exclusive group is already on, no other hardware
2045 * events can go on.
2046 */
2047 if (cpuctx->exclusive)
2048 return 0;
2049 /*
2050 * If this group is exclusive and there are already
2051 * events on the CPU, it can't go on.
2052 */
2053 if (event->attr.exclusive && cpuctx->active_oncpu)
2054 return 0;
2055 /*
2056 * Otherwise, try to add it if all previous groups were able
2057 * to go on.
2058 */
2059 return can_add_hw;
2060}
2061
2062static void add_event_to_ctx(struct perf_event *event,
2063 struct perf_event_context *ctx)
2064{
Stephane Eranian41587552011-01-03 18:20:01 +02002065 u64 tstamp = perf_event_time(event);
2066
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002067 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002068 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002069 event->tstamp_enabled = tstamp;
2070 event->tstamp_running = tstamp;
2071 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002072}
2073
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002074static void ctx_sched_out(struct perf_event_context *ctx,
2075 struct perf_cpu_context *cpuctx,
2076 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002077static void
2078ctx_sched_in(struct perf_event_context *ctx,
2079 struct perf_cpu_context *cpuctx,
2080 enum event_type_t event_type,
2081 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002082
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002083static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2084 struct perf_event_context *ctx)
2085{
2086 if (!cpuctx->task_ctx)
2087 return;
2088
2089 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2090 return;
2091
2092 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2093}
2094
Peter Zijlstradce58552011-04-09 21:17:46 +02002095static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2096 struct perf_event_context *ctx,
2097 struct task_struct *task)
2098{
2099 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2100 if (ctx)
2101 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2102 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2103 if (ctx)
2104 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2105}
2106
Peter Zijlstra3e349502016-01-08 10:01:18 +01002107static void ctx_resched(struct perf_cpu_context *cpuctx,
2108 struct perf_event_context *task_ctx)
Peter Zijlstra00179602015-11-30 16:26:35 +01002109{
Peter Zijlstra3e349502016-01-08 10:01:18 +01002110 perf_pmu_disable(cpuctx->ctx.pmu);
2111 if (task_ctx)
2112 task_ctx_sched_out(cpuctx, task_ctx);
2113 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2114 perf_event_sched_in(cpuctx, task_ctx, current);
2115 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002116}
2117
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002118/*
2119 * Cross CPU call to install and enable a performance event
2120 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002121 * Very similar to remote_function() + event_function() but cannot assume that
2122 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002123 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002124static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002125{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002126 struct perf_event *event = info;
2127 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002128 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002129 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002130 bool activate = true;
2131 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002132
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002133 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002134 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002135 raw_spin_lock(&ctx->lock);
2136 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002137
2138 /* If we're on the wrong CPU, try again */
2139 if (task_cpu(ctx->task) != smp_processor_id()) {
2140 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002141 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002142 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002143
Peter Zijlstra39a43642016-01-11 12:46:35 +01002144 /*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002145 * If we're on the right CPU, see if the task we target is
2146 * current, if not we don't have to activate the ctx, a future
2147 * context switch will do that for us.
Peter Zijlstra39a43642016-01-11 12:46:35 +01002148 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002149 if (ctx->task != current)
2150 activate = false;
2151 else
2152 WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx);
2153
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002154 } else if (task_ctx) {
2155 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002156 }
2157
Peter Zijlstraa0963092016-02-24 18:45:50 +01002158 if (activate) {
2159 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2160 add_event_to_ctx(event, ctx);
2161 ctx_resched(cpuctx, task_ctx);
2162 } else {
2163 add_event_to_ctx(event, ctx);
2164 }
2165
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002166unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002167 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002168
Peter Zijlstraa0963092016-02-24 18:45:50 +01002169 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002170}
2171
2172/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002173 * Attach a performance event to a context.
2174 *
2175 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002176 */
2177static void
2178perf_install_in_context(struct perf_event_context *ctx,
2179 struct perf_event *event,
2180 int cpu)
2181{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002182 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002183
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002184 lockdep_assert_held(&ctx->mutex);
2185
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002186 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002187 if (event->cpu != -1)
2188 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002189
Peter Zijlstraa0963092016-02-24 18:45:50 +01002190 if (!task) {
2191 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002192 return;
2193 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002194
Peter Zijlstraa0963092016-02-24 18:45:50 +01002195 /*
2196 * Should not happen, we validate the ctx is still alive before calling.
2197 */
2198 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2199 return;
2200
2201 /*
2202 * Installing events is tricky because we cannot rely on ctx->is_active
2203 * to be set in case this is the nr_events 0 -> 1 transition.
2204 */
2205again:
2206 /*
2207 * Cannot use task_function_call() because we need to run on the task's
2208 * CPU regardless of whether its current or not.
2209 */
2210 if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event))
2211 return;
2212
2213 raw_spin_lock_irq(&ctx->lock);
2214 task = ctx->task;
2215 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2216 /*
2217 * Cannot happen because we already checked above (which also
2218 * cannot happen), and we hold ctx->mutex, which serializes us
2219 * against perf_event_exit_task_context().
2220 */
2221 raw_spin_unlock_irq(&ctx->lock);
2222 return;
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002223 }
Peter Zijlstra39a43642016-01-11 12:46:35 +01002224 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstraa0963092016-02-24 18:45:50 +01002225 /*
2226 * Since !ctx->is_active doesn't mean anything, we must IPI
2227 * unconditionally.
2228 */
2229 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002230}
2231
2232/*
2233 * Put a event into inactive state and update time fields.
2234 * Enabling the leader of a group effectively enables all
2235 * the group members that aren't explicitly disabled, so we
2236 * have to update their ->tstamp_enabled also.
2237 * Note: this works for group members as well as group leaders
2238 * since the non-leader members' sibling_lists will be empty.
2239 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002240static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002241{
2242 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002243 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002244
2245 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002246 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002247 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002248 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2249 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002250 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002251}
2252
2253/*
2254 * Cross CPU call to enable a performance event
2255 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002256static void __perf_event_enable(struct perf_event *event,
2257 struct perf_cpu_context *cpuctx,
2258 struct perf_event_context *ctx,
2259 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002260{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002261 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002262 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002263
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002264 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2265 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002266 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002267
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002268 if (ctx->is_active)
2269 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2270
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002271 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002272
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002273 if (!ctx->is_active)
2274 return;
2275
Stephane Eraniane5d13672011-02-14 11:20:01 +02002276 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002277 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02002278 perf_cgroup_defer_enabled(event);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002279 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002280 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002281 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002282
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002283 /*
2284 * If the event is in a group and isn't the group leader,
2285 * then don't put it on unless the group is on.
2286 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002287 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2288 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002289 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002290 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002291
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002292 task_ctx = cpuctx->task_ctx;
2293 if (ctx->task)
2294 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002295
Peter Zijlstraaee7dbc2016-01-08 10:45:11 +01002296 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002297}
2298
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002299/*
2300 * Enable a event.
2301 *
2302 * If event->ctx is a cloned context, callers must make sure that
2303 * every task struct that event->ctx->task could possibly point to
2304 * remains valid. This condition is satisfied when called through
2305 * perf_event_for_each_child or perf_event_for_each as described
2306 * for perf_event_disable.
2307 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002308static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002309{
2310 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002311
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002312 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002313 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2314 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002315 raw_spin_unlock_irq(&ctx->lock);
2316 return;
2317 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002318
2319 /*
2320 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002321 *
2322 * That way, if we see the event in error state below, we know that it
2323 * has gone back into error state, as distinct from the task having
2324 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002325 */
2326 if (event->state == PERF_EVENT_STATE_ERROR)
2327 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002328 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002329
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002330 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002331}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002332
2333/*
2334 * See perf_event_disable();
2335 */
2336void perf_event_enable(struct perf_event *event)
2337{
2338 struct perf_event_context *ctx;
2339
2340 ctx = perf_event_ctx_lock(event);
2341 _perf_event_enable(event);
2342 perf_event_ctx_unlock(event, ctx);
2343}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002344EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002345
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002346static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002347{
2348 /*
2349 * not supported on inherited events
2350 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002351 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002352 return -EINVAL;
2353
2354 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002355 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002356
2357 return 0;
2358}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002359
2360/*
2361 * See perf_event_disable()
2362 */
2363int perf_event_refresh(struct perf_event *event, int refresh)
2364{
2365 struct perf_event_context *ctx;
2366 int ret;
2367
2368 ctx = perf_event_ctx_lock(event);
2369 ret = _perf_event_refresh(event, refresh);
2370 perf_event_ctx_unlock(event, ctx);
2371
2372 return ret;
2373}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002374EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002375
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002376static void ctx_sched_out(struct perf_event_context *ctx,
2377 struct perf_cpu_context *cpuctx,
2378 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002379{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002380 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002381 struct perf_event *event;
2382
2383 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002384
Peter Zijlstra39a43642016-01-11 12:46:35 +01002385 if (likely(!ctx->nr_events)) {
2386 /*
2387 * See __perf_remove_from_context().
2388 */
2389 WARN_ON_ONCE(ctx->is_active);
2390 if (ctx->task)
2391 WARN_ON_ONCE(cpuctx->task_ctx);
2392 return;
2393 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002394
Peter Zijlstradb24d332011-04-09 21:17:45 +02002395 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002396 if (!(ctx->is_active & EVENT_ALL))
2397 ctx->is_active = 0;
2398
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002399 if (ctx->task) {
2400 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2401 if (!ctx->is_active)
2402 cpuctx->task_ctx = NULL;
2403 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002404
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002405 is_active ^= ctx->is_active; /* changed bits */
2406
2407 if (is_active & EVENT_TIME) {
2408 /* update (and stop) ctx time */
2409 update_context_time(ctx);
2410 update_cgrp_time_from_cpuctx(cpuctx);
2411 }
2412
2413 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002414 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002415
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002416 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002417 if (is_active & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002418 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2419 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002420 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002421
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002422 if (is_active & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002423 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002424 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002425 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002426 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002427}
2428
2429/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002430 * Test whether two contexts are equivalent, i.e. whether they have both been
2431 * cloned from the same version of the same context.
2432 *
2433 * Equivalence is measured using a generation number in the context that is
2434 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2435 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002436 */
2437static int context_equiv(struct perf_event_context *ctx1,
2438 struct perf_event_context *ctx2)
2439{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002440 lockdep_assert_held(&ctx1->lock);
2441 lockdep_assert_held(&ctx2->lock);
2442
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002443 /* Pinning disables the swap optimization */
2444 if (ctx1->pin_count || ctx2->pin_count)
2445 return 0;
2446
2447 /* If ctx1 is the parent of ctx2 */
2448 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2449 return 1;
2450
2451 /* If ctx2 is the parent of ctx1 */
2452 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2453 return 1;
2454
2455 /*
2456 * If ctx1 and ctx2 have the same parent; we flatten the parent
2457 * hierarchy, see perf_event_init_context().
2458 */
2459 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2460 ctx1->parent_gen == ctx2->parent_gen)
2461 return 1;
2462
2463 /* Unmatched */
2464 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002465}
2466
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002467static void __perf_event_sync_stat(struct perf_event *event,
2468 struct perf_event *next_event)
2469{
2470 u64 value;
2471
2472 if (!event->attr.inherit_stat)
2473 return;
2474
2475 /*
2476 * Update the event value, we cannot use perf_event_read()
2477 * because we're in the middle of a context switch and have IRQs
2478 * disabled, which upsets smp_call_function_single(), however
2479 * we know the event must be on the current CPU, therefore we
2480 * don't need to use it.
2481 */
2482 switch (event->state) {
2483 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002484 event->pmu->read(event);
2485 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002486
2487 case PERF_EVENT_STATE_INACTIVE:
2488 update_event_times(event);
2489 break;
2490
2491 default:
2492 break;
2493 }
2494
2495 /*
2496 * In order to keep per-task stats reliable we need to flip the event
2497 * values when we flip the contexts.
2498 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002499 value = local64_read(&next_event->count);
2500 value = local64_xchg(&event->count, value);
2501 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002502
2503 swap(event->total_time_enabled, next_event->total_time_enabled);
2504 swap(event->total_time_running, next_event->total_time_running);
2505
2506 /*
2507 * Since we swizzled the values, update the user visible data too.
2508 */
2509 perf_event_update_userpage(event);
2510 perf_event_update_userpage(next_event);
2511}
2512
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002513static void perf_event_sync_stat(struct perf_event_context *ctx,
2514 struct perf_event_context *next_ctx)
2515{
2516 struct perf_event *event, *next_event;
2517
2518 if (!ctx->nr_stat)
2519 return;
2520
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002521 update_context_time(ctx);
2522
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002523 event = list_first_entry(&ctx->event_list,
2524 struct perf_event, event_entry);
2525
2526 next_event = list_first_entry(&next_ctx->event_list,
2527 struct perf_event, event_entry);
2528
2529 while (&event->event_entry != &ctx->event_list &&
2530 &next_event->event_entry != &next_ctx->event_list) {
2531
2532 __perf_event_sync_stat(event, next_event);
2533
2534 event = list_next_entry(event, event_entry);
2535 next_event = list_next_entry(next_event, event_entry);
2536 }
2537}
2538
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002539static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2540 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002541{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002542 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002543 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002544 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002545 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002546 int do_switch = 1;
2547
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002548 if (likely(!ctx))
2549 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002550
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002551 cpuctx = __get_cpu_context(ctx);
2552 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002553 return;
2554
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002555 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002556 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002557 if (!next_ctx)
2558 goto unlock;
2559
2560 parent = rcu_dereference(ctx->parent_ctx);
2561 next_parent = rcu_dereference(next_ctx->parent_ctx);
2562
2563 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002564 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002565 goto unlock;
2566
2567 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002568 /*
2569 * Looks like the two contexts are clones, so we might be
2570 * able to optimize the context switch. We lock both
2571 * contexts and check that they are clones under the
2572 * lock (including re-checking that neither has been
2573 * uncloned in the meantime). It doesn't matter which
2574 * order we take the locks because no other cpu could
2575 * be trying to lock both of these tasks.
2576 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002577 raw_spin_lock(&ctx->lock);
2578 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002579 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002580 WRITE_ONCE(ctx->task, next);
2581 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05002582
2583 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2584
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002585 /*
2586 * RCU_INIT_POINTER here is safe because we've not
2587 * modified the ctx and the above modification of
2588 * ctx->task and ctx->task_ctx_data are immaterial
2589 * since those values are always verified under
2590 * ctx->lock which we're now holding.
2591 */
2592 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2593 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2594
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002595 do_switch = 0;
2596
2597 perf_event_sync_stat(ctx, next_ctx);
2598 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002599 raw_spin_unlock(&next_ctx->lock);
2600 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002601 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002602unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002603 rcu_read_unlock();
2604
2605 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002606 raw_spin_lock(&ctx->lock);
Peter Zijlstra8833d0e2016-01-08 10:02:37 +01002607 task_ctx_sched_out(cpuctx, ctx);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002608 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002609 }
2610}
2611
Yan, Zhengba532502014-11-04 21:55:58 -05002612void perf_sched_cb_dec(struct pmu *pmu)
2613{
2614 this_cpu_dec(perf_sched_cb_usages);
2615}
2616
2617void perf_sched_cb_inc(struct pmu *pmu)
2618{
2619 this_cpu_inc(perf_sched_cb_usages);
2620}
2621
2622/*
2623 * This function provides the context switch callback to the lower code
2624 * layer. It is invoked ONLY when the context switch callback is enabled.
2625 */
2626static void perf_pmu_sched_task(struct task_struct *prev,
2627 struct task_struct *next,
2628 bool sched_in)
2629{
2630 struct perf_cpu_context *cpuctx;
2631 struct pmu *pmu;
2632 unsigned long flags;
2633
2634 if (prev == next)
2635 return;
2636
2637 local_irq_save(flags);
2638
2639 rcu_read_lock();
2640
2641 list_for_each_entry_rcu(pmu, &pmus, entry) {
2642 if (pmu->sched_task) {
2643 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2644
2645 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2646
2647 perf_pmu_disable(pmu);
2648
2649 pmu->sched_task(cpuctx->task_ctx, sched_in);
2650
2651 perf_pmu_enable(pmu);
2652
2653 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2654 }
2655 }
2656
2657 rcu_read_unlock();
2658
2659 local_irq_restore(flags);
2660}
2661
Adrian Hunter45ac1402015-07-21 12:44:02 +03002662static void perf_event_switch(struct task_struct *task,
2663 struct task_struct *next_prev, bool sched_in);
2664
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002665#define for_each_task_context_nr(ctxn) \
2666 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2667
2668/*
2669 * Called from scheduler to remove the events of the current task,
2670 * with interrupts disabled.
2671 *
2672 * We stop each event and update the event value in event->count.
2673 *
2674 * This does not protect us against NMI, but disable()
2675 * sets the disabled bit in the control field of event _before_
2676 * accessing the event control register. If a NMI hits, then it will
2677 * not restart the event.
2678 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002679void __perf_event_task_sched_out(struct task_struct *task,
2680 struct task_struct *next)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002681{
2682 int ctxn;
2683
Yan, Zhengba532502014-11-04 21:55:58 -05002684 if (__this_cpu_read(perf_sched_cb_usages))
2685 perf_pmu_sched_task(task, next, false);
2686
Adrian Hunter45ac1402015-07-21 12:44:02 +03002687 if (atomic_read(&nr_switch_events))
2688 perf_event_switch(task, next, false);
2689
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002690 for_each_task_context_nr(ctxn)
2691 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002692
2693 /*
2694 * if cgroup events exist on this CPU, then we need
2695 * to check if we have to switch out PMU state.
2696 * cgroup event are system-wide mode only
2697 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002698 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002699 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002700}
2701
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002702/*
2703 * Called with IRQs disabled
2704 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002705static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2706 enum event_type_t event_type)
2707{
2708 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002709}
2710
2711static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002712ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002713 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002714{
2715 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002716
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002717 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2718 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002719 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002720 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002721 continue;
2722
Stephane Eraniane5d13672011-02-14 11:20:01 +02002723 /* may need to reset tstamp_enabled */
2724 if (is_cgroup_event(event))
2725 perf_cgroup_mark_enabled(event, ctx);
2726
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002727 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002728 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002729
2730 /*
2731 * If this pinned group hasn't been scheduled,
2732 * put it in error state.
2733 */
2734 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2735 update_group_times(event);
2736 event->state = PERF_EVENT_STATE_ERROR;
2737 }
2738 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002739}
2740
2741static void
2742ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002743 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002744{
2745 struct perf_event *event;
2746 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002747
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002748 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2749 /* Ignore events in OFF or ERROR state */
2750 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002751 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002752 /*
2753 * Listen to the 'cpu' scheduling filter constraint
2754 * of events:
2755 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002756 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002757 continue;
2758
Stephane Eraniane5d13672011-02-14 11:20:01 +02002759 /* may need to reset tstamp_enabled */
2760 if (is_cgroup_event(event))
2761 perf_cgroup_mark_enabled(event, ctx);
2762
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002763 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002764 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002765 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002766 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002767 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002768}
2769
2770static void
2771ctx_sched_in(struct perf_event_context *ctx,
2772 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002773 enum event_type_t event_type,
2774 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002775{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002776 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002777 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002778
Peter Zijlstrac994d612016-01-08 09:20:23 +01002779 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002780
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002781 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002782 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002783
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002784 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002785 if (ctx->task) {
2786 if (!is_active)
2787 cpuctx->task_ctx = ctx;
2788 else
2789 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2790 }
2791
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002792 is_active ^= ctx->is_active; /* changed bits */
2793
2794 if (is_active & EVENT_TIME) {
2795 /* start ctx time */
2796 now = perf_clock();
2797 ctx->timestamp = now;
2798 perf_cgroup_set_timestamp(task, ctx);
2799 }
2800
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002801 /*
2802 * First go through the list and put on any pinned groups
2803 * in order to give them the best chance of going on.
2804 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002805 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002806 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002807
2808 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002809 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002810 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002811}
2812
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002813static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002814 enum event_type_t event_type,
2815 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002816{
2817 struct perf_event_context *ctx = &cpuctx->ctx;
2818
Stephane Eraniane5d13672011-02-14 11:20:01 +02002819 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002820}
2821
Stephane Eraniane5d13672011-02-14 11:20:01 +02002822static void perf_event_context_sched_in(struct perf_event_context *ctx,
2823 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002824{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002825 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002826
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002827 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002828 if (cpuctx->task_ctx == ctx)
2829 return;
2830
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002831 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002832 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002833 /*
2834 * We want to keep the following priority order:
2835 * cpu pinned (that don't need to move), task pinned,
2836 * cpu flexible, task flexible.
2837 */
2838 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002839 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002840 perf_pmu_enable(ctx->pmu);
2841 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002842}
2843
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002844/*
2845 * Called from scheduler to add the events of the current task
2846 * with interrupts disabled.
2847 *
2848 * We restore the event value and then enable it.
2849 *
2850 * This does not protect us against NMI, but enable()
2851 * sets the enabled bit in the control field of event _before_
2852 * accessing the event control register. If a NMI hits, then it will
2853 * keep the event running.
2854 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002855void __perf_event_task_sched_in(struct task_struct *prev,
2856 struct task_struct *task)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002857{
2858 struct perf_event_context *ctx;
2859 int ctxn;
2860
Peter Zijlstra7e41d172016-01-08 09:21:40 +01002861 /*
2862 * If cgroup events exist on this CPU, then we need to check if we have
2863 * to switch in PMU state; cgroup event are system-wide mode only.
2864 *
2865 * Since cgroup events are CPU events, we must schedule these in before
2866 * we schedule in the task events.
2867 */
2868 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
2869 perf_cgroup_sched_in(prev, task);
2870
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002871 for_each_task_context_nr(ctxn) {
2872 ctx = task->perf_event_ctxp[ctxn];
2873 if (likely(!ctx))
2874 continue;
2875
Stephane Eraniane5d13672011-02-14 11:20:01 +02002876 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002877 }
Stephane Eraniand010b332012-02-09 23:21:00 +01002878
Adrian Hunter45ac1402015-07-21 12:44:02 +03002879 if (atomic_read(&nr_switch_events))
2880 perf_event_switch(task, prev, true);
2881
Yan, Zhengba532502014-11-04 21:55:58 -05002882 if (__this_cpu_read(perf_sched_cb_usages))
2883 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002884}
2885
Peter Zijlstraabd50712010-01-26 18:50:16 +01002886static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2887{
2888 u64 frequency = event->attr.sample_freq;
2889 u64 sec = NSEC_PER_SEC;
2890 u64 divisor, dividend;
2891
2892 int count_fls, nsec_fls, frequency_fls, sec_fls;
2893
2894 count_fls = fls64(count);
2895 nsec_fls = fls64(nsec);
2896 frequency_fls = fls64(frequency);
2897 sec_fls = 30;
2898
2899 /*
2900 * We got @count in @nsec, with a target of sample_freq HZ
2901 * the target period becomes:
2902 *
2903 * @count * 10^9
2904 * period = -------------------
2905 * @nsec * sample_freq
2906 *
2907 */
2908
2909 /*
2910 * Reduce accuracy by one bit such that @a and @b converge
2911 * to a similar magnitude.
2912 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002913#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002914do { \
2915 if (a##_fls > b##_fls) { \
2916 a >>= 1; \
2917 a##_fls--; \
2918 } else { \
2919 b >>= 1; \
2920 b##_fls--; \
2921 } \
2922} while (0)
2923
2924 /*
2925 * Reduce accuracy until either term fits in a u64, then proceed with
2926 * the other, so that finally we can do a u64/u64 division.
2927 */
2928 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2929 REDUCE_FLS(nsec, frequency);
2930 REDUCE_FLS(sec, count);
2931 }
2932
2933 if (count_fls + sec_fls > 64) {
2934 divisor = nsec * frequency;
2935
2936 while (count_fls + sec_fls > 64) {
2937 REDUCE_FLS(count, sec);
2938 divisor >>= 1;
2939 }
2940
2941 dividend = count * sec;
2942 } else {
2943 dividend = count * sec;
2944
2945 while (nsec_fls + frequency_fls > 64) {
2946 REDUCE_FLS(nsec, frequency);
2947 dividend >>= 1;
2948 }
2949
2950 divisor = nsec * frequency;
2951 }
2952
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02002953 if (!divisor)
2954 return dividend;
2955
Peter Zijlstraabd50712010-01-26 18:50:16 +01002956 return div64_u64(dividend, divisor);
2957}
2958
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002959static DEFINE_PER_CPU(int, perf_throttled_count);
2960static DEFINE_PER_CPU(u64, perf_throttled_seq);
2961
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002962static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002963{
2964 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02002965 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002966 s64 delta;
2967
Peter Zijlstraabd50712010-01-26 18:50:16 +01002968 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002969
2970 delta = (s64)(period - hwc->sample_period);
2971 delta = (delta + 7) / 8; /* low pass filter */
2972
2973 sample_period = hwc->sample_period + delta;
2974
2975 if (!sample_period)
2976 sample_period = 1;
2977
2978 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002979
Peter Zijlstrae7850592010-05-21 14:43:08 +02002980 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002981 if (disable)
2982 event->pmu->stop(event, PERF_EF_UPDATE);
2983
Peter Zijlstrae7850592010-05-21 14:43:08 +02002984 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002985
2986 if (disable)
2987 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002988 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002989}
2990
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002991/*
2992 * combine freq adjustment with unthrottling to avoid two passes over the
2993 * events. At the same time, make sure, having freq events does not change
2994 * the rate of unthrottling as that would introduce bias.
2995 */
2996static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2997 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002998{
2999 struct perf_event *event;
3000 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003001 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003002 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003003
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003004 /*
3005 * only need to iterate over all events iff:
3006 * - context have events in frequency mode (needs freq adjust)
3007 * - there are events to unthrottle on this cpu
3008 */
3009 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003010 return;
3011
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003012 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003013 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003014
Paul Mackerras03541f82009-10-14 16:58:03 +11003015 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003016 if (event->state != PERF_EVENT_STATE_ACTIVE)
3017 continue;
3018
Stephane Eranian5632ab12011-01-03 18:20:01 +02003019 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003020 continue;
3021
Alexander Shishkin44377272013-12-16 14:17:36 +02003022 perf_pmu_disable(event->pmu);
3023
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024 hwc = &event->hw;
3025
Jiri Olsaae23bff2013-08-24 16:45:54 +02003026 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003027 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003028 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003029 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003030 }
3031
3032 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003033 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003034
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003035 /*
3036 * stop the event and update event->count
3037 */
3038 event->pmu->stop(event, PERF_EF_UPDATE);
3039
Peter Zijlstrae7850592010-05-21 14:43:08 +02003040 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003041 delta = now - hwc->freq_count_stamp;
3042 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003043
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003044 /*
3045 * restart the event
3046 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003047 * we have stopped the event so tell that
3048 * to perf_adjust_period() to avoid stopping it
3049 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003050 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003051 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003052 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003053
3054 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003055 next:
3056 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003057 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003058
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003059 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003060 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003061}
3062
3063/*
3064 * Round-robin a context's events:
3065 */
3066static void rotate_ctx(struct perf_event_context *ctx)
3067{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003068 /*
3069 * Rotate the first entry last of non-pinned groups. Rotation might be
3070 * disabled by the inheritance code.
3071 */
3072 if (!ctx->rotate_disable)
3073 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003074}
3075
Stephane Eranian9e630202013-04-03 14:21:33 +02003076static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003077{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003078 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003079 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003080
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003081 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003082 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3083 rotate = 1;
3084 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003085
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003086 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003087 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003088 if (ctx->nr_events != ctx->nr_active)
3089 rotate = 1;
3090 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003091
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003092 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003093 goto done;
3094
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003095 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003096 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003097
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003098 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3099 if (ctx)
3100 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003101
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003102 rotate_ctx(&cpuctx->ctx);
3103 if (ctx)
3104 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003105
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003106 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003107
3108 perf_pmu_enable(cpuctx->ctx.pmu);
3109 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003110done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003111
3112 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003113}
3114
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003115#ifdef CONFIG_NO_HZ_FULL
3116bool perf_event_can_stop_tick(void)
3117{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003118 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003119 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003120 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003121 else
3122 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003123}
3124#endif
3125
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003126void perf_event_task_tick(void)
3127{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003128 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3129 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003130 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003131
3132 WARN_ON(!irqs_disabled());
3133
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003134 __this_cpu_inc(perf_throttled_seq);
3135 throttled = __this_cpu_xchg(perf_throttled_count, 0);
3136
Mark Rutland2fde4f92015-01-07 15:01:54 +00003137 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003138 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003139}
3140
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003141static int event_enable_on_exec(struct perf_event *event,
3142 struct perf_event_context *ctx)
3143{
3144 if (!event->attr.enable_on_exec)
3145 return 0;
3146
3147 event->attr.enable_on_exec = 0;
3148 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3149 return 0;
3150
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003151 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003152
3153 return 1;
3154}
3155
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003156/*
3157 * Enable all of a task's events that have been marked enable-on-exec.
3158 * This expects task == current.
3159 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003160static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003161{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003162 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003163 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003164 struct perf_event *event;
3165 unsigned long flags;
3166 int enabled = 0;
3167
3168 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003169 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003170 if (!ctx || !ctx->nr_events)
3171 goto out;
3172
Peter Zijlstra3e349502016-01-08 10:01:18 +01003173 cpuctx = __get_cpu_context(ctx);
3174 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01003175 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003176 list_for_each_entry(event, &ctx->event_list, event_entry)
3177 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003178
3179 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003180 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003181 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003182 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003183 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003184 ctx_resched(cpuctx, ctx);
3185 }
3186 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003187
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003188out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003189 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003190
3191 if (clone_ctx)
3192 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003193}
3194
Peter Zijlstrae041e322014-05-21 17:32:19 +02003195void perf_event_exec(void)
3196{
Peter Zijlstrae041e322014-05-21 17:32:19 +02003197 int ctxn;
3198
3199 rcu_read_lock();
Peter Zijlstrac1274492015-12-10 20:57:40 +01003200 for_each_task_context_nr(ctxn)
3201 perf_event_enable_on_exec(ctxn);
Peter Zijlstrae041e322014-05-21 17:32:19 +02003202 rcu_read_unlock();
3203}
3204
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003205struct perf_read_data {
3206 struct perf_event *event;
3207 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003208 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003209};
3210
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003211/*
3212 * Cross CPU call to read the hardware event
3213 */
3214static void __perf_event_read(void *info)
3215{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003216 struct perf_read_data *data = info;
3217 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003218 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003219 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003220 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003221
3222 /*
3223 * If this is a task context, we need to check whether it is
3224 * the current task context of this cpu. If not it has been
3225 * scheduled out before the smp call arrived. In that case
3226 * event->count would have been updated to a recent sample
3227 * when the event was scheduled out.
3228 */
3229 if (ctx->task && cpuctx->task_ctx != ctx)
3230 return;
3231
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003232 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003233 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003234 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003235 update_cgrp_time_from_event(event);
3236 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003237
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003238 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003239 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003240 goto unlock;
3241
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003242 if (!data->group) {
3243 pmu->read(event);
3244 data->ret = 0;
3245 goto unlock;
3246 }
3247
3248 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3249
3250 pmu->read(event);
3251
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003252 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3253 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003254 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3255 /*
3256 * Use sibling's PMU rather than @event's since
3257 * sibling could be on different (eg: software) PMU.
3258 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003259 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003260 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003261 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003262
3263 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003264
3265unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003266 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003267}
3268
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003269static inline u64 perf_event_count(struct perf_event *event)
3270{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003271 if (event->pmu->count)
3272 return event->pmu->count(event);
3273
3274 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003275}
3276
Kaixu Xiaffe86902015-08-06 07:02:32 +00003277/*
3278 * NMI-safe method to read a local event, that is an event that
3279 * is:
3280 * - either for the current task, or for this CPU
3281 * - does not have inherit set, for inherited task events
3282 * will not be local and we cannot read them atomically
3283 * - must not have a pmu::count method
3284 */
3285u64 perf_event_read_local(struct perf_event *event)
3286{
3287 unsigned long flags;
3288 u64 val;
3289
3290 /*
3291 * Disabling interrupts avoids all counter scheduling (context
3292 * switches, timer based rotation and IPIs).
3293 */
3294 local_irq_save(flags);
3295
3296 /* If this is a per-task event, it must be for current */
3297 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3298 event->hw.target != current);
3299
3300 /* If this is a per-CPU event, it must be for this CPU */
3301 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3302 event->cpu != smp_processor_id());
3303
3304 /*
3305 * It must not be an event with inherit set, we cannot read
3306 * all child counters from atomic context.
3307 */
3308 WARN_ON_ONCE(event->attr.inherit);
3309
3310 /*
3311 * It must not have a pmu::count method, those are not
3312 * NMI safe.
3313 */
3314 WARN_ON_ONCE(event->pmu->count);
3315
3316 /*
3317 * If the event is currently on this CPU, its either a per-task event,
3318 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3319 * oncpu == -1).
3320 */
3321 if (event->oncpu == smp_processor_id())
3322 event->pmu->read(event);
3323
3324 val = local64_read(&event->count);
3325 local_irq_restore(flags);
3326
3327 return val;
3328}
3329
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003330static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003331{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003332 int ret = 0;
3333
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003334 /*
3335 * If event is enabled and currently active on a CPU, update the
3336 * value in the event structure:
3337 */
3338 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003339 struct perf_read_data data = {
3340 .event = event,
3341 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003342 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003343 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003344 smp_call_function_single(event->oncpu,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003345 __perf_event_read, &data, 1);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003346 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003347 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003348 struct perf_event_context *ctx = event->ctx;
3349 unsigned long flags;
3350
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003351 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003352 /*
3353 * may read while context is not active
3354 * (e.g., thread is blocked), in that case
3355 * we cannot update context time
3356 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003357 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003358 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003359 update_cgrp_time_from_event(event);
3360 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003361 if (group)
3362 update_group_times(event);
3363 else
3364 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003365 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003366 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003367
3368 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003369}
3370
3371/*
3372 * Initialize the perf_event context in a task_struct:
3373 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003374static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003375{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003376 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003377 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003378 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003379 INIT_LIST_HEAD(&ctx->pinned_groups);
3380 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003381 INIT_LIST_HEAD(&ctx->event_list);
3382 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003383}
3384
Peter Zijlstraeb184472010-09-07 15:55:13 +02003385static struct perf_event_context *
3386alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003387{
3388 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003389
3390 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3391 if (!ctx)
3392 return NULL;
3393
3394 __perf_event_init_context(ctx);
3395 if (task) {
3396 ctx->task = task;
3397 get_task_struct(task);
3398 }
3399 ctx->pmu = pmu;
3400
3401 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402}
3403
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003404static struct task_struct *
3405find_lively_task_by_vpid(pid_t vpid)
3406{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003407 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003408 int err;
3409
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003410 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003411 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003412 task = current;
3413 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003414 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003415 if (task)
3416 get_task_struct(task);
3417 rcu_read_unlock();
3418
3419 if (!task)
3420 return ERR_PTR(-ESRCH);
3421
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003422 /* Reuse ptrace permission checks for now. */
3423 err = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08003424 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003425 goto errout;
3426
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003427 return task;
3428errout:
3429 put_task_struct(task);
3430 return ERR_PTR(err);
3431
3432}
3433
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003434/*
3435 * Returns a matching context with refcount and pincount.
3436 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003437static struct perf_event_context *
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003438find_get_context(struct pmu *pmu, struct task_struct *task,
3439 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003440{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003441 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003442 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003443 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003444 unsigned long flags;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003445 int ctxn, err;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003446 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003447
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003448 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003449 /* Must be root to operate on a CPU event: */
3450 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3451 return ERR_PTR(-EACCES);
3452
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003453 /*
3454 * We could be clever and allow to attach a event to an
3455 * offline CPU and activate it when the CPU comes up, but
3456 * that's for later.
3457 */
3458 if (!cpu_online(cpu))
3459 return ERR_PTR(-ENODEV);
3460
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003461 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003462 ctx = &cpuctx->ctx;
3463 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003464 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003465
3466 return ctx;
3467 }
3468
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003469 err = -EINVAL;
3470 ctxn = pmu->task_ctx_nr;
3471 if (ctxn < 0)
3472 goto errout;
3473
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003474 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3475 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3476 if (!task_ctx_data) {
3477 err = -ENOMEM;
3478 goto errout;
3479 }
3480 }
3481
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003482retry:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003483 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003484 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003485 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003486 ++ctx->pin_count;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003487
3488 if (task_ctx_data && !ctx->task_ctx_data) {
3489 ctx->task_ctx_data = task_ctx_data;
3490 task_ctx_data = NULL;
3491 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003492 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003493
3494 if (clone_ctx)
3495 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003496 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003497 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003498 err = -ENOMEM;
3499 if (!ctx)
3500 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003501
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003502 if (task_ctx_data) {
3503 ctx->task_ctx_data = task_ctx_data;
3504 task_ctx_data = NULL;
3505 }
3506
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003507 err = 0;
3508 mutex_lock(&task->perf_event_mutex);
3509 /*
3510 * If it has already passed perf_event_exit_task().
3511 * we must see PF_EXITING, it takes this mutex too.
3512 */
3513 if (task->flags & PF_EXITING)
3514 err = -ESRCH;
3515 else if (task->perf_event_ctxp[ctxn])
3516 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003517 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003518 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003519 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003520 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003521 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003522 mutex_unlock(&task->perf_event_mutex);
3523
3524 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003525 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003526
3527 if (err == -EAGAIN)
3528 goto retry;
3529 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003530 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003531 }
3532
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003533 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003534 return ctx;
3535
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003536errout:
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003537 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003538 return ERR_PTR(err);
3539}
3540
Li Zefan6fb29152009-10-15 11:21:42 +08003541static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003542static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003543
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003544static void free_event_rcu(struct rcu_head *head)
3545{
3546 struct perf_event *event;
3547
3548 event = container_of(head, struct perf_event, rcu_head);
3549 if (event->ns)
3550 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003551 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003552 kfree(event);
3553}
3554
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003555static void ring_buffer_attach(struct perf_event *event,
3556 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003557
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003558static void unaccount_event_cpu(struct perf_event *event, int cpu)
3559{
3560 if (event->parent)
3561 return;
3562
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003563 if (is_cgroup_event(event))
3564 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3565}
3566
3567static void unaccount_event(struct perf_event *event)
3568{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003569 bool dec = false;
3570
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003571 if (event->parent)
3572 return;
3573
3574 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003575 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003576 if (event->attr.mmap || event->attr.mmap_data)
3577 atomic_dec(&nr_mmap_events);
3578 if (event->attr.comm)
3579 atomic_dec(&nr_comm_events);
3580 if (event->attr.task)
3581 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003582 if (event->attr.freq)
3583 atomic_dec(&nr_freq_events);
Adrian Hunter45ac1402015-07-21 12:44:02 +03003584 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003585 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003586 atomic_dec(&nr_switch_events);
3587 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003588 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003589 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003590 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003591 dec = true;
3592
Peter Zijlstra9107c892016-02-24 18:45:45 +01003593 if (dec) {
3594 if (!atomic_add_unless(&perf_sched_count, -1, 1))
3595 schedule_delayed_work(&perf_sched_work, HZ);
3596 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003597
3598 unaccount_event_cpu(event, event->cpu);
3599}
3600
Peter Zijlstra9107c892016-02-24 18:45:45 +01003601static void perf_sched_delayed(struct work_struct *work)
3602{
3603 mutex_lock(&perf_sched_mutex);
3604 if (atomic_dec_and_test(&perf_sched_count))
3605 static_branch_disable(&perf_sched_events);
3606 mutex_unlock(&perf_sched_mutex);
3607}
3608
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003609/*
3610 * The following implement mutual exclusion of events on "exclusive" pmus
3611 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3612 * at a time, so we disallow creating events that might conflict, namely:
3613 *
3614 * 1) cpu-wide events in the presence of per-task events,
3615 * 2) per-task events in the presence of cpu-wide events,
3616 * 3) two matching events on the same context.
3617 *
3618 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003619 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003620 */
3621static int exclusive_event_init(struct perf_event *event)
3622{
3623 struct pmu *pmu = event->pmu;
3624
3625 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3626 return 0;
3627
3628 /*
3629 * Prevent co-existence of per-task and cpu-wide events on the
3630 * same exclusive pmu.
3631 *
3632 * Negative pmu::exclusive_cnt means there are cpu-wide
3633 * events on this "exclusive" pmu, positive means there are
3634 * per-task events.
3635 *
3636 * Since this is called in perf_event_alloc() path, event::ctx
3637 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3638 * to mean "per-task event", because unlike other attach states it
3639 * never gets cleared.
3640 */
3641 if (event->attach_state & PERF_ATTACH_TASK) {
3642 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
3643 return -EBUSY;
3644 } else {
3645 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
3646 return -EBUSY;
3647 }
3648
3649 return 0;
3650}
3651
3652static void exclusive_event_destroy(struct perf_event *event)
3653{
3654 struct pmu *pmu = event->pmu;
3655
3656 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3657 return;
3658
3659 /* see comment in exclusive_event_init() */
3660 if (event->attach_state & PERF_ATTACH_TASK)
3661 atomic_dec(&pmu->exclusive_cnt);
3662 else
3663 atomic_inc(&pmu->exclusive_cnt);
3664}
3665
3666static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
3667{
3668 if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
3669 (e1->cpu == e2->cpu ||
3670 e1->cpu == -1 ||
3671 e2->cpu == -1))
3672 return true;
3673 return false;
3674}
3675
3676/* Called under the same ctx::mutex as perf_install_in_context() */
3677static bool exclusive_event_installable(struct perf_event *event,
3678 struct perf_event_context *ctx)
3679{
3680 struct perf_event *iter_event;
3681 struct pmu *pmu = event->pmu;
3682
3683 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3684 return true;
3685
3686 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
3687 if (exclusive_event_match(iter_event, event))
3688 return false;
3689 }
3690
3691 return true;
3692}
3693
Peter Zijlstra683ede42014-05-05 12:11:24 +02003694static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003695{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003696 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003697
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003698 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003699
Frederic Weisbecker76369132011-05-19 19:55:04 +02003700 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003701 /*
3702 * Can happen when we close an event with re-directed output.
3703 *
3704 * Since we have a 0 refcount, perf_mmap_close() will skip
3705 * over us; possibly making our ring_buffer_put() the last.
3706 */
3707 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003708 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003709 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003710 }
3711
Stephane Eraniane5d13672011-02-14 11:20:01 +02003712 if (is_cgroup_event(event))
3713 perf_detach_cgroup(event);
3714
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003715 if (!event->parent) {
3716 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3717 put_callchain_buffers();
3718 }
3719
3720 perf_event_free_bpf_prog(event);
3721
3722 if (event->destroy)
3723 event->destroy(event);
3724
3725 if (event->ctx)
3726 put_ctx(event->ctx);
3727
3728 if (event->pmu) {
3729 exclusive_event_destroy(event);
3730 module_put(event->pmu->module);
3731 }
3732
3733 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003734}
3735
Peter Zijlstra683ede42014-05-05 12:11:24 +02003736/*
3737 * Used to free events which have a known refcount of 1, such as in error paths
3738 * where the event isn't exposed yet and inherited events.
3739 */
3740static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003741{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003742 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3743 "unexpected event refcount: %ld; ptr=%p\n",
3744 atomic_long_read(&event->refcount), event)) {
3745 /* leak to avoid use-after-free */
3746 return;
3747 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003748
Peter Zijlstra683ede42014-05-05 12:11:24 +02003749 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003750}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003751
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003752/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003753 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003754 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003755static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003756{
Peter Zijlstra88821352010-11-09 19:01:43 +01003757 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003758
Peter Zijlstra88821352010-11-09 19:01:43 +01003759 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01003760 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003761 * Matches the smp_store_release() in perf_event_exit_task(). If we
3762 * observe !owner it means the list deletion is complete and we can
3763 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01003764 * owner->perf_event_mutex.
3765 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003766 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01003767 if (owner) {
3768 /*
3769 * Since delayed_put_task_struct() also drops the last
3770 * task reference we can safely take a new reference
3771 * while holding the rcu_read_lock().
3772 */
3773 get_task_struct(owner);
3774 }
3775 rcu_read_unlock();
3776
3777 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003778 /*
3779 * If we're here through perf_event_exit_task() we're already
3780 * holding ctx->mutex which would be an inversion wrt. the
3781 * normal lock order.
3782 *
3783 * However we can safely take this lock because its the child
3784 * ctx->mutex.
3785 */
3786 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
3787
Peter Zijlstra88821352010-11-09 19:01:43 +01003788 /*
3789 * We have to re-check the event->owner field, if it is cleared
3790 * we raced with perf_event_exit_task(), acquiring the mutex
3791 * ensured they're done, and we can proceed with freeing the
3792 * event.
3793 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003794 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01003795 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003796 smp_store_release(&event->owner, NULL);
3797 }
Peter Zijlstra88821352010-11-09 19:01:43 +01003798 mutex_unlock(&owner->perf_event_mutex);
3799 put_task_struct(owner);
3800 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003801}
3802
Jiri Olsaf8697762014-08-01 14:33:01 +02003803static void put_event(struct perf_event *event)
3804{
Jiri Olsaf8697762014-08-01 14:33:01 +02003805 if (!atomic_long_dec_and_test(&event->refcount))
3806 return;
3807
Peter Zijlstra683ede42014-05-05 12:11:24 +02003808 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003809}
3810
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003811/*
3812 * Kill an event dead; while event:refcount will preserve the event
3813 * object, it will not preserve its functionality. Once the last 'user'
3814 * gives up the object, we'll destroy the thing.
3815 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02003816int perf_event_release_kernel(struct perf_event *event)
3817{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003818 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003819 struct perf_event *child, *tmp;
3820
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003821 /*
3822 * If we got here through err_file: fput(event_file); we will not have
3823 * attached to a context yet.
3824 */
3825 if (!ctx) {
3826 WARN_ON_ONCE(event->attach_state &
3827 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
3828 goto no_ctx;
3829 }
3830
Peter Zijlstra88821352010-11-09 19:01:43 +01003831 if (!is_kernel_event(event))
3832 perf_remove_from_owner(event);
3833
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01003834 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02003835 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003836 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01003837
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003838 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01003839 /*
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003840 * Mark this even as STATE_DEAD, there is no external reference to it
3841 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003842 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003843 * Anybody acquiring event->child_mutex after the below loop _must_
3844 * also see this, most importantly inherit_event() which will avoid
3845 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003846 *
3847 * Thus this guarantees that we will in fact observe and kill _ALL_
3848 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01003849 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003850 event->state = PERF_EVENT_STATE_DEAD;
3851 raw_spin_unlock_irq(&ctx->lock);
3852
3853 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01003854
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003855again:
3856 mutex_lock(&event->child_mutex);
3857 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01003858
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003859 /*
3860 * Cannot change, child events are not migrated, see the
3861 * comment with perf_event_ctx_lock_nested().
3862 */
3863 ctx = lockless_dereference(child->ctx);
3864 /*
3865 * Since child_mutex nests inside ctx::mutex, we must jump
3866 * through hoops. We start by grabbing a reference on the ctx.
3867 *
3868 * Since the event cannot get freed while we hold the
3869 * child_mutex, the context must also exist and have a !0
3870 * reference count.
3871 */
3872 get_ctx(ctx);
3873
3874 /*
3875 * Now that we have a ctx ref, we can drop child_mutex, and
3876 * acquire ctx::mutex without fear of it going away. Then we
3877 * can re-acquire child_mutex.
3878 */
3879 mutex_unlock(&event->child_mutex);
3880 mutex_lock(&ctx->mutex);
3881 mutex_lock(&event->child_mutex);
3882
3883 /*
3884 * Now that we hold ctx::mutex and child_mutex, revalidate our
3885 * state, if child is still the first entry, it didn't get freed
3886 * and we can continue doing so.
3887 */
3888 tmp = list_first_entry_or_null(&event->child_list,
3889 struct perf_event, child_list);
3890 if (tmp == child) {
3891 perf_remove_from_context(child, DETACH_GROUP);
3892 list_del(&child->child_list);
3893 free_event(child);
3894 /*
3895 * This matches the refcount bump in inherit_event();
3896 * this can't be the last reference.
3897 */
3898 put_event(event);
3899 }
3900
3901 mutex_unlock(&event->child_mutex);
3902 mutex_unlock(&ctx->mutex);
3903 put_ctx(ctx);
3904 goto again;
3905 }
3906 mutex_unlock(&event->child_mutex);
3907
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003908no_ctx:
3909 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02003910 return 0;
3911}
3912EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3913
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02003914/*
3915 * Called when the last reference to the file is gone.
3916 */
Al Viroa6fa9412012-08-20 14:59:25 +01003917static int perf_release(struct inode *inode, struct file *file)
3918{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003919 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01003920 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003921}
3922
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003923u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003924{
3925 struct perf_event *child;
3926 u64 total = 0;
3927
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003928 *enabled = 0;
3929 *running = 0;
3930
Peter Zijlstra6f105812009-11-20 22:19:56 +01003931 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003932
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003933 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003934 total += perf_event_count(event);
3935
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003936 *enabled += event->total_time_enabled +
3937 atomic64_read(&event->child_total_time_enabled);
3938 *running += event->total_time_running +
3939 atomic64_read(&event->child_total_time_running);
3940
3941 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003942 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003943 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003944 *enabled += child->total_time_enabled;
3945 *running += child->total_time_running;
3946 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003947 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003948
3949 return total;
3950}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003951EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003952
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003953static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003954 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003955{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003956 struct perf_event *sub;
3957 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003958 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003959
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003960 ret = perf_event_read(leader, true);
3961 if (ret)
3962 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003963
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003964 /*
3965 * Since we co-schedule groups, {enabled,running} times of siblings
3966 * will be identical to those of the leader, so we only publish one
3967 * set.
3968 */
3969 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3970 values[n++] += leader->total_time_enabled +
3971 atomic64_read(&leader->child_total_time_enabled);
3972 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003973
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003974 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3975 values[n++] += leader->total_time_running +
3976 atomic64_read(&leader->child_total_time_running);
3977 }
3978
3979 /*
3980 * Write {count,id} tuples for every sibling.
3981 */
3982 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003983 if (read_format & PERF_FORMAT_ID)
3984 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003985
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003986 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003987 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003988 if (read_format & PERF_FORMAT_ID)
3989 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003990 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003991
3992 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003993}
3994
3995static int perf_read_group(struct perf_event *event,
3996 u64 read_format, char __user *buf)
3997{
3998 struct perf_event *leader = event->group_leader, *child;
3999 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004000 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004001 u64 *values;
4002
4003 lockdep_assert_held(&ctx->mutex);
4004
4005 values = kzalloc(event->read_size, GFP_KERNEL);
4006 if (!values)
4007 return -ENOMEM;
4008
4009 values[0] = 1 + leader->nr_siblings;
4010
4011 /*
4012 * By locking the child_mutex of the leader we effectively
4013 * lock the child list of all siblings.. XXX explain how.
4014 */
4015 mutex_lock(&leader->child_mutex);
4016
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004017 ret = __perf_read_group_add(leader, read_format, values);
4018 if (ret)
4019 goto unlock;
4020
4021 list_for_each_entry(child, &leader->child_list, child_list) {
4022 ret = __perf_read_group_add(child, read_format, values);
4023 if (ret)
4024 goto unlock;
4025 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004026
4027 mutex_unlock(&leader->child_mutex);
4028
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004029 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004030 if (copy_to_user(buf, values, event->read_size))
4031 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004032 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004033
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004034unlock:
4035 mutex_unlock(&leader->child_mutex);
4036out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004037 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004038 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004039}
4040
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004041static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004042 u64 read_format, char __user *buf)
4043{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004044 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004045 u64 values[4];
4046 int n = 0;
4047
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004048 values[n++] = perf_event_read_value(event, &enabled, &running);
4049 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4050 values[n++] = enabled;
4051 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4052 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004053 if (read_format & PERF_FORMAT_ID)
4054 values[n++] = primary_event_id(event);
4055
4056 if (copy_to_user(buf, values, n * sizeof(u64)))
4057 return -EFAULT;
4058
4059 return n * sizeof(u64);
4060}
4061
Jiri Olsadc633982014-09-12 13:18:26 +02004062static bool is_event_hup(struct perf_event *event)
4063{
4064 bool no_children;
4065
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004066 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004067 return false;
4068
4069 mutex_lock(&event->child_mutex);
4070 no_children = list_empty(&event->child_list);
4071 mutex_unlock(&event->child_mutex);
4072 return no_children;
4073}
4074
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004075/*
4076 * Read the performance event - simple non blocking version for now
4077 */
4078static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004079__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004080{
4081 u64 read_format = event->attr.read_format;
4082 int ret;
4083
4084 /*
4085 * Return end-of-file for a read on a event that is in
4086 * error state (i.e. because it was pinned but it couldn't be
4087 * scheduled on to the CPU at some point).
4088 */
4089 if (event->state == PERF_EVENT_STATE_ERROR)
4090 return 0;
4091
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004092 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004093 return -ENOSPC;
4094
4095 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004096 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004097 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004098 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004099 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004100
4101 return ret;
4102}
4103
4104static ssize_t
4105perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4106{
4107 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004108 struct perf_event_context *ctx;
4109 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004110
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004111 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004112 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004113 perf_event_ctx_unlock(event, ctx);
4114
4115 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004116}
4117
4118static unsigned int perf_poll(struct file *file, poll_table *wait)
4119{
4120 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004121 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004122 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004123
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004124 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004125
Jiri Olsadc633982014-09-12 13:18:26 +02004126 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004127 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004128
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004129 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004130 * Pin the event->rb by taking event->mmap_mutex; otherwise
4131 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004132 */
4133 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004134 rb = event->rb;
4135 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004136 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004137 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004138 return events;
4139}
4140
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004141static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004142{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004143 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004144 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004145 perf_event_update_userpage(event);
4146}
4147
4148/*
4149 * Holding the top-level event's child_mutex means that any
4150 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004151 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004152 * task existence requirements of perf_event_enable/disable.
4153 */
4154static void perf_event_for_each_child(struct perf_event *event,
4155 void (*func)(struct perf_event *))
4156{
4157 struct perf_event *child;
4158
4159 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004160
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004161 mutex_lock(&event->child_mutex);
4162 func(event);
4163 list_for_each_entry(child, &event->child_list, child_list)
4164 func(child);
4165 mutex_unlock(&event->child_mutex);
4166}
4167
4168static void perf_event_for_each(struct perf_event *event,
4169 void (*func)(struct perf_event *))
4170{
4171 struct perf_event_context *ctx = event->ctx;
4172 struct perf_event *sibling;
4173
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004174 lockdep_assert_held(&ctx->mutex);
4175
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004176 event = event->group_leader;
4177
4178 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004179 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004180 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004181}
4182
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004183static void __perf_event_period(struct perf_event *event,
4184 struct perf_cpu_context *cpuctx,
4185 struct perf_event_context *ctx,
4186 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004187{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004188 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004189 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004190
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004191 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004192 event->attr.sample_freq = value;
4193 } else {
4194 event->attr.sample_period = value;
4195 event->hw.sample_period = value;
4196 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004197
4198 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4199 if (active) {
4200 perf_pmu_disable(ctx->pmu);
4201 event->pmu->stop(event, PERF_EF_UPDATE);
4202 }
4203
4204 local64_set(&event->hw.period_left, 0);
4205
4206 if (active) {
4207 event->pmu->start(event, PERF_EF_RELOAD);
4208 perf_pmu_enable(ctx->pmu);
4209 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004210}
4211
4212static int perf_event_period(struct perf_event *event, u64 __user *arg)
4213{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004214 u64 value;
4215
4216 if (!is_sampling_event(event))
4217 return -EINVAL;
4218
4219 if (copy_from_user(&value, arg, sizeof(value)))
4220 return -EFAULT;
4221
4222 if (!value)
4223 return -EINVAL;
4224
4225 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4226 return -EINVAL;
4227
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004228 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004229
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004230 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004231}
4232
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004233static const struct file_operations perf_fops;
4234
Al Viro2903ff02012-08-28 12:52:22 -04004235static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004236{
Al Viro2903ff02012-08-28 12:52:22 -04004237 struct fd f = fdget(fd);
4238 if (!f.file)
4239 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004240
Al Viro2903ff02012-08-28 12:52:22 -04004241 if (f.file->f_op != &perf_fops) {
4242 fdput(f);
4243 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004244 }
Al Viro2903ff02012-08-28 12:52:22 -04004245 *p = f;
4246 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004247}
4248
4249static int perf_event_set_output(struct perf_event *event,
4250 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004251static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004252static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004253
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004254static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004255{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004256 void (*func)(struct perf_event *);
4257 u32 flags = arg;
4258
4259 switch (cmd) {
4260 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004261 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004262 break;
4263 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004264 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004265 break;
4266 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004267 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004268 break;
4269
4270 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004271 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004272
4273 case PERF_EVENT_IOC_PERIOD:
4274 return perf_event_period(event, (u64 __user *)arg);
4275
Jiri Olsacf4957f2012-10-24 13:37:58 +02004276 case PERF_EVENT_IOC_ID:
4277 {
4278 u64 id = primary_event_id(event);
4279
4280 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4281 return -EFAULT;
4282 return 0;
4283 }
4284
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004285 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004286 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004287 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004288 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004289 struct perf_event *output_event;
4290 struct fd output;
4291 ret = perf_fget_light(arg, &output);
4292 if (ret)
4293 return ret;
4294 output_event = output.file->private_data;
4295 ret = perf_event_set_output(event, output_event);
4296 fdput(output);
4297 } else {
4298 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004299 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004300 return ret;
4301 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004302
Li Zefan6fb29152009-10-15 11:21:42 +08004303 case PERF_EVENT_IOC_SET_FILTER:
4304 return perf_event_set_filter(event, (void __user *)arg);
4305
Alexei Starovoitov25415172015-03-25 12:49:20 -07004306 case PERF_EVENT_IOC_SET_BPF:
4307 return perf_event_set_bpf_prog(event, arg);
4308
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004309 default:
4310 return -ENOTTY;
4311 }
4312
4313 if (flags & PERF_IOC_FLAG_GROUP)
4314 perf_event_for_each(event, func);
4315 else
4316 perf_event_for_each_child(event, func);
4317
4318 return 0;
4319}
4320
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004321static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4322{
4323 struct perf_event *event = file->private_data;
4324 struct perf_event_context *ctx;
4325 long ret;
4326
4327 ctx = perf_event_ctx_lock(event);
4328 ret = _perf_ioctl(event, cmd, arg);
4329 perf_event_ctx_unlock(event, ctx);
4330
4331 return ret;
4332}
4333
Pawel Mollb3f20782014-06-13 16:03:32 +01004334#ifdef CONFIG_COMPAT
4335static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4336 unsigned long arg)
4337{
4338 switch (_IOC_NR(cmd)) {
4339 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4340 case _IOC_NR(PERF_EVENT_IOC_ID):
4341 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4342 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4343 cmd &= ~IOCSIZE_MASK;
4344 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4345 }
4346 break;
4347 }
4348 return perf_ioctl(file, cmd, arg);
4349}
4350#else
4351# define perf_compat_ioctl NULL
4352#endif
4353
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004354int perf_event_task_enable(void)
4355{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004356 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004357 struct perf_event *event;
4358
4359 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004360 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4361 ctx = perf_event_ctx_lock(event);
4362 perf_event_for_each_child(event, _perf_event_enable);
4363 perf_event_ctx_unlock(event, ctx);
4364 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004365 mutex_unlock(&current->perf_event_mutex);
4366
4367 return 0;
4368}
4369
4370int perf_event_task_disable(void)
4371{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004372 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004373 struct perf_event *event;
4374
4375 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004376 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4377 ctx = perf_event_ctx_lock(event);
4378 perf_event_for_each_child(event, _perf_event_disable);
4379 perf_event_ctx_unlock(event, ctx);
4380 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004381 mutex_unlock(&current->perf_event_mutex);
4382
4383 return 0;
4384}
4385
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004386static int perf_event_index(struct perf_event *event)
4387{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004388 if (event->hw.state & PERF_HES_STOPPED)
4389 return 0;
4390
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004391 if (event->state != PERF_EVENT_STATE_ACTIVE)
4392 return 0;
4393
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004394 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004395}
4396
Eric B Munsonc4794292011-06-23 16:34:38 -04004397static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004398 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004399 u64 *enabled,
4400 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004401{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004402 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004403
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004404 *now = perf_clock();
4405 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004406 *enabled = ctx_time - event->tstamp_enabled;
4407 *running = ctx_time - event->tstamp_running;
4408}
4409
Peter Zijlstrafa7315872013-09-19 10:16:42 +02004410static void perf_event_init_userpage(struct perf_event *event)
4411{
4412 struct perf_event_mmap_page *userpg;
4413 struct ring_buffer *rb;
4414
4415 rcu_read_lock();
4416 rb = rcu_dereference(event->rb);
4417 if (!rb)
4418 goto unlock;
4419
4420 userpg = rb->user_page;
4421
4422 /* Allow new userspace to detect that bit 0 is deprecated */
4423 userpg->cap_bit0_is_deprecated = 1;
4424 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004425 userpg->data_offset = PAGE_SIZE;
4426 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa7315872013-09-19 10:16:42 +02004427
4428unlock:
4429 rcu_read_unlock();
4430}
4431
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004432void __weak arch_perf_update_userpage(
4433 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004434{
4435}
4436
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004437/*
4438 * Callers need to ensure there can be no nesting of this function, otherwise
4439 * the seqlock logic goes bad. We can not serialize this because the arch
4440 * code calls this from NMI context.
4441 */
4442void perf_event_update_userpage(struct perf_event *event)
4443{
4444 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004445 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004446 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004447
4448 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004449 rb = rcu_dereference(event->rb);
4450 if (!rb)
4451 goto unlock;
4452
Eric B Munson0d641202011-06-24 12:26:26 -04004453 /*
4454 * compute total_time_enabled, total_time_running
4455 * based on snapshot values taken when the event
4456 * was last scheduled in.
4457 *
4458 * we cannot simply called update_context_time()
4459 * because of locking issue as we can be called in
4460 * NMI context
4461 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004462 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004463
Frederic Weisbecker76369132011-05-19 19:55:04 +02004464 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004465 /*
4466 * Disable preemption so as to not let the corresponding user-space
4467 * spin too long if we get preempted.
4468 */
4469 preempt_disable();
4470 ++userpg->lock;
4471 barrier();
4472 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004473 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004474 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004475 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004476
Eric B Munson0d641202011-06-24 12:26:26 -04004477 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004478 atomic64_read(&event->child_total_time_enabled);
4479
Eric B Munson0d641202011-06-24 12:26:26 -04004480 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004481 atomic64_read(&event->child_total_time_running);
4482
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004483 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004484
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004485 barrier();
4486 ++userpg->lock;
4487 preempt_enable();
4488unlock:
4489 rcu_read_unlock();
4490}
4491
Peter Zijlstra906010b2009-09-21 16:08:49 +02004492static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4493{
4494 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004495 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004496 int ret = VM_FAULT_SIGBUS;
4497
4498 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4499 if (vmf->pgoff == 0)
4500 ret = 0;
4501 return ret;
4502 }
4503
4504 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004505 rb = rcu_dereference(event->rb);
4506 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004507 goto unlock;
4508
4509 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4510 goto unlock;
4511
Frederic Weisbecker76369132011-05-19 19:55:04 +02004512 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004513 if (!vmf->page)
4514 goto unlock;
4515
4516 get_page(vmf->page);
4517 vmf->page->mapping = vma->vm_file->f_mapping;
4518 vmf->page->index = vmf->pgoff;
4519
4520 ret = 0;
4521unlock:
4522 rcu_read_unlock();
4523
4524 return ret;
4525}
4526
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004527static void ring_buffer_attach(struct perf_event *event,
4528 struct ring_buffer *rb)
4529{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004530 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004531 unsigned long flags;
4532
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004533 if (event->rb) {
4534 /*
4535 * Should be impossible, we set this when removing
4536 * event->rb_entry and wait/clear when adding event->rb_entry.
4537 */
4538 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004539
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004540 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004541 spin_lock_irqsave(&old_rb->event_lock, flags);
4542 list_del_rcu(&event->rb_entry);
4543 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004544
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004545 event->rcu_batches = get_state_synchronize_rcu();
4546 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004547 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004548
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004549 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004550 if (event->rcu_pending) {
4551 cond_synchronize_rcu(event->rcu_batches);
4552 event->rcu_pending = 0;
4553 }
4554
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004555 spin_lock_irqsave(&rb->event_lock, flags);
4556 list_add_rcu(&event->rb_entry, &rb->event_list);
4557 spin_unlock_irqrestore(&rb->event_lock, flags);
4558 }
4559
4560 rcu_assign_pointer(event->rb, rb);
4561
4562 if (old_rb) {
4563 ring_buffer_put(old_rb);
4564 /*
4565 * Since we detached before setting the new rb, so that we
4566 * could attach the new rb, we could have missed a wakeup.
4567 * Provide it now.
4568 */
4569 wake_up_all(&event->waitq);
4570 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004571}
4572
4573static void ring_buffer_wakeup(struct perf_event *event)
4574{
4575 struct ring_buffer *rb;
4576
4577 rcu_read_lock();
4578 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004579 if (rb) {
4580 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4581 wake_up_all(&event->waitq);
4582 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004583 rcu_read_unlock();
4584}
4585
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004586struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004587{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004588 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004589
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004590 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004591 rb = rcu_dereference(event->rb);
4592 if (rb) {
4593 if (!atomic_inc_not_zero(&rb->refcount))
4594 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004595 }
4596 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004597
Frederic Weisbecker76369132011-05-19 19:55:04 +02004598 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004599}
4600
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004601void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004602{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004603 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004604 return;
4605
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004606 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004607
Frederic Weisbecker76369132011-05-19 19:55:04 +02004608 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004609}
4610
4611static void perf_mmap_open(struct vm_area_struct *vma)
4612{
4613 struct perf_event *event = vma->vm_file->private_data;
4614
4615 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004616 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004617
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004618 if (vma->vm_pgoff)
4619 atomic_inc(&event->rb->aux_mmap_count);
4620
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004621 if (event->pmu->event_mapped)
4622 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004623}
4624
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004625/*
4626 * A buffer can be mmap()ed multiple times; either directly through the same
4627 * event, or through other events by use of perf_event_set_output().
4628 *
4629 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4630 * the buffer here, where we still have a VM context. This means we need
4631 * to detach all events redirecting to us.
4632 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004633static void perf_mmap_close(struct vm_area_struct *vma)
4634{
4635 struct perf_event *event = vma->vm_file->private_data;
4636
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004637 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004638 struct user_struct *mmap_user = rb->mmap_user;
4639 int mmap_locked = rb->mmap_locked;
4640 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004641
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004642 if (event->pmu->event_unmapped)
4643 event->pmu->event_unmapped(event);
4644
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004645 /*
4646 * rb->aux_mmap_count will always drop before rb->mmap_count and
4647 * event->mmap_count, so it is ok to use event->mmap_mutex to
4648 * serialize with perf_mmap here.
4649 */
4650 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
4651 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
4652 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
4653 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
4654
4655 rb_free_aux(rb);
4656 mutex_unlock(&event->mmap_mutex);
4657 }
4658
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004659 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004660
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004661 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004662 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004663
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004664 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004665 mutex_unlock(&event->mmap_mutex);
4666
4667 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004668 if (atomic_read(&rb->mmap_count))
4669 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004670
4671 /*
4672 * No other mmap()s, detach from all other events that might redirect
4673 * into the now unreachable buffer. Somewhat complicated by the
4674 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4675 */
4676again:
4677 rcu_read_lock();
4678 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4679 if (!atomic_long_inc_not_zero(&event->refcount)) {
4680 /*
4681 * This event is en-route to free_event() which will
4682 * detach it and remove it from the list.
4683 */
4684 continue;
4685 }
4686 rcu_read_unlock();
4687
4688 mutex_lock(&event->mmap_mutex);
4689 /*
4690 * Check we didn't race with perf_event_set_output() which can
4691 * swizzle the rb from under us while we were waiting to
4692 * acquire mmap_mutex.
4693 *
4694 * If we find a different rb; ignore this event, a next
4695 * iteration will no longer find it on the list. We have to
4696 * still restart the iteration to make sure we're not now
4697 * iterating the wrong list.
4698 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004699 if (event->rb == rb)
4700 ring_buffer_attach(event, NULL);
4701
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004702 mutex_unlock(&event->mmap_mutex);
4703 put_event(event);
4704
4705 /*
4706 * Restart the iteration; either we're on the wrong list or
4707 * destroyed its integrity by doing a deletion.
4708 */
4709 goto again;
4710 }
4711 rcu_read_unlock();
4712
4713 /*
4714 * It could be there's still a few 0-ref events on the list; they'll
4715 * get cleaned up by free_event() -- they'll also still have their
4716 * ref on the rb and will free it whenever they are done with it.
4717 *
4718 * Aside from that, this buffer is 'fully' detached and unmapped,
4719 * undo the VM accounting.
4720 */
4721
4722 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4723 vma->vm_mm->pinned_vm -= mmap_locked;
4724 free_uid(mmap_user);
4725
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004726out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004727 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004728}
4729
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04004730static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004731 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004732 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004733 .fault = perf_mmap_fault,
4734 .page_mkwrite = perf_mmap_fault,
4735};
4736
4737static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4738{
4739 struct perf_event *event = file->private_data;
4740 unsigned long user_locked, user_lock_limit;
4741 struct user_struct *user = current_user();
4742 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004743 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004744 unsigned long vma_size;
4745 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004746 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004747 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004748
Peter Zijlstrac7920612010-05-18 10:33:24 +02004749 /*
4750 * Don't allow mmap() of inherited per-task counters. This would
4751 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004752 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004753 */
4754 if (event->cpu == -1 && event->attr.inherit)
4755 return -EINVAL;
4756
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004757 if (!(vma->vm_flags & VM_SHARED))
4758 return -EINVAL;
4759
4760 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004761
4762 if (vma->vm_pgoff == 0) {
4763 nr_pages = (vma_size / PAGE_SIZE) - 1;
4764 } else {
4765 /*
4766 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
4767 * mapped, all subsequent mappings should have the same size
4768 * and offset. Must be above the normal perf buffer.
4769 */
4770 u64 aux_offset, aux_size;
4771
4772 if (!event->rb)
4773 return -EINVAL;
4774
4775 nr_pages = vma_size / PAGE_SIZE;
4776
4777 mutex_lock(&event->mmap_mutex);
4778 ret = -EINVAL;
4779
4780 rb = event->rb;
4781 if (!rb)
4782 goto aux_unlock;
4783
4784 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
4785 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
4786
4787 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
4788 goto aux_unlock;
4789
4790 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
4791 goto aux_unlock;
4792
4793 /* already mapped with a different offset */
4794 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
4795 goto aux_unlock;
4796
4797 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
4798 goto aux_unlock;
4799
4800 /* already mapped with a different size */
4801 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
4802 goto aux_unlock;
4803
4804 if (!is_power_of_2(nr_pages))
4805 goto aux_unlock;
4806
4807 if (!atomic_inc_not_zero(&rb->mmap_count))
4808 goto aux_unlock;
4809
4810 if (rb_has_aux(rb)) {
4811 atomic_inc(&rb->aux_mmap_count);
4812 ret = 0;
4813 goto unlock;
4814 }
4815
4816 atomic_set(&rb->aux_mmap_count, 1);
4817 user_extra = nr_pages;
4818
4819 goto accounting;
4820 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004821
4822 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004823 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004824 * can do bitmasks instead of modulo.
4825 */
Kan Liang2ed11312015-03-02 02:14:26 -05004826 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004827 return -EINVAL;
4828
4829 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4830 return -EINVAL;
4831
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004832 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004833again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004834 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004835 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004836 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004837 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004838 goto unlock;
4839 }
4840
4841 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4842 /*
4843 * Raced against perf_mmap_close() through
4844 * perf_event_set_output(). Try again, hope for better
4845 * luck.
4846 */
4847 mutex_unlock(&event->mmap_mutex);
4848 goto again;
4849 }
4850
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004851 goto unlock;
4852 }
4853
4854 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004855
4856accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004857 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4858
4859 /*
4860 * Increase the limit linearly with more CPUs:
4861 */
4862 user_lock_limit *= num_online_cpus();
4863
4864 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4865
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004866 if (user_locked > user_lock_limit)
4867 extra = user_locked - user_lock_limit;
4868
Jiri Slaby78d7d402010-03-05 13:42:54 -08004869 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004870 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004871 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004872
4873 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4874 !capable(CAP_IPC_LOCK)) {
4875 ret = -EPERM;
4876 goto unlock;
4877 }
4878
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004879 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004880
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004881 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004882 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004883
Frederic Weisbecker76369132011-05-19 19:55:04 +02004884 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004885 rb = rb_alloc(nr_pages,
4886 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4887 event->cpu, flags);
4888
4889 if (!rb) {
4890 ret = -ENOMEM;
4891 goto unlock;
4892 }
4893
4894 atomic_set(&rb->mmap_count, 1);
4895 rb->mmap_user = get_current_user();
4896 rb->mmap_locked = extra;
4897
4898 ring_buffer_attach(event, rb);
4899
4900 perf_event_init_userpage(event);
4901 perf_event_update_userpage(event);
4902 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02004903 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
4904 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004905 if (!ret)
4906 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004907 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004908
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004909unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004910 if (!ret) {
4911 atomic_long_add(user_extra, &user->locked_vm);
4912 vma->vm_mm->pinned_vm += extra;
4913
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004914 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004915 } else if (rb) {
4916 atomic_dec(&rb->mmap_count);
4917 }
4918aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004919 mutex_unlock(&event->mmap_mutex);
4920
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004921 /*
4922 * Since pinned accounting is per vm we cannot allow fork() to copy our
4923 * vma.
4924 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004925 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004926 vma->vm_ops = &perf_mmap_vmops;
4927
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004928 if (event->pmu->event_mapped)
4929 event->pmu->event_mapped(event);
4930
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004931 return ret;
4932}
4933
4934static int perf_fasync(int fd, struct file *filp, int on)
4935{
Al Viro496ad9a2013-01-23 17:07:38 -05004936 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004937 struct perf_event *event = filp->private_data;
4938 int retval;
4939
Al Viro59551022016-01-22 15:40:57 -05004940 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004941 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05004942 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004943
4944 if (retval < 0)
4945 return retval;
4946
4947 return 0;
4948}
4949
4950static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004951 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004952 .release = perf_release,
4953 .read = perf_read,
4954 .poll = perf_poll,
4955 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01004956 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004957 .mmap = perf_mmap,
4958 .fasync = perf_fasync,
4959};
4960
4961/*
4962 * Perf event wakeup
4963 *
4964 * If there's data, ensure we set the poll() state and publish everything
4965 * to user-space before waking everybody up.
4966 */
4967
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02004968static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
4969{
4970 /* only the parent has fasync state */
4971 if (event->parent)
4972 event = event->parent;
4973 return &event->fasync;
4974}
4975
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004976void perf_event_wakeup(struct perf_event *event)
4977{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004978 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004979
4980 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02004981 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004982 event->pending_kill = 0;
4983 }
4984}
4985
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004986static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004987{
4988 struct perf_event *event = container_of(entry,
4989 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01004990 int rctx;
4991
4992 rctx = perf_swevent_get_recursion_context();
4993 /*
4994 * If we 'fail' here, that's OK, it means recursion is already disabled
4995 * and we won't recurse 'further'.
4996 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004997
4998 if (event->pending_disable) {
4999 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005000 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005001 }
5002
5003 if (event->pending_wakeup) {
5004 event->pending_wakeup = 0;
5005 perf_event_wakeup(event);
5006 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01005007
5008 if (rctx >= 0)
5009 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005010}
5011
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005012/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005013 * We assume there is only KVM supporting the callbacks.
5014 * Later on, we might change it to a list if there is
5015 * another virtualization implementation supporting the callbacks.
5016 */
5017struct perf_guest_info_callbacks *perf_guest_cbs;
5018
5019int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5020{
5021 perf_guest_cbs = cbs;
5022 return 0;
5023}
5024EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5025
5026int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5027{
5028 perf_guest_cbs = NULL;
5029 return 0;
5030}
5031EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5032
Jiri Olsa40189942012-08-07 15:20:37 +02005033static void
5034perf_output_sample_regs(struct perf_output_handle *handle,
5035 struct pt_regs *regs, u64 mask)
5036{
5037 int bit;
5038
5039 for_each_set_bit(bit, (const unsigned long *) &mask,
5040 sizeof(mask) * BITS_PER_BYTE) {
5041 u64 val;
5042
5043 val = perf_reg_value(regs, bit);
5044 perf_output_put(handle, val);
5045 }
5046}
5047
Stephane Eranian60e23642014-09-24 13:48:37 +02005048static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005049 struct pt_regs *regs,
5050 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02005051{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005052 if (user_mode(regs)) {
5053 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02005054 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005055 } else if (current->mm) {
5056 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005057 } else {
5058 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5059 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005060 }
5061}
5062
Stephane Eranian60e23642014-09-24 13:48:37 +02005063static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5064 struct pt_regs *regs)
5065{
5066 regs_intr->regs = regs;
5067 regs_intr->abi = perf_reg_abi(current);
5068}
5069
5070
Jiri Olsac5ebced2012-08-07 15:20:40 +02005071/*
5072 * Get remaining task size from user stack pointer.
5073 *
5074 * It'd be better to take stack vma map and limit this more
5075 * precisly, but there's no way to get it safely under interrupt,
5076 * so using TASK_SIZE as limit.
5077 */
5078static u64 perf_ustack_task_size(struct pt_regs *regs)
5079{
5080 unsigned long addr = perf_user_stack_pointer(regs);
5081
5082 if (!addr || addr >= TASK_SIZE)
5083 return 0;
5084
5085 return TASK_SIZE - addr;
5086}
5087
5088static u16
5089perf_sample_ustack_size(u16 stack_size, u16 header_size,
5090 struct pt_regs *regs)
5091{
5092 u64 task_size;
5093
5094 /* No regs, no stack pointer, no dump. */
5095 if (!regs)
5096 return 0;
5097
5098 /*
5099 * Check if we fit in with the requested stack size into the:
5100 * - TASK_SIZE
5101 * If we don't, we limit the size to the TASK_SIZE.
5102 *
5103 * - remaining sample size
5104 * If we don't, we customize the stack size to
5105 * fit in to the remaining sample size.
5106 */
5107
5108 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5109 stack_size = min(stack_size, (u16) task_size);
5110
5111 /* Current header size plus static size and dynamic size. */
5112 header_size += 2 * sizeof(u64);
5113
5114 /* Do we fit in with the current stack dump size? */
5115 if ((u16) (header_size + stack_size) < header_size) {
5116 /*
5117 * If we overflow the maximum size for the sample,
5118 * we customize the stack dump size to fit in.
5119 */
5120 stack_size = USHRT_MAX - header_size - sizeof(u64);
5121 stack_size = round_up(stack_size, sizeof(u64));
5122 }
5123
5124 return stack_size;
5125}
5126
5127static void
5128perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5129 struct pt_regs *regs)
5130{
5131 /* Case of a kernel thread, nothing to dump */
5132 if (!regs) {
5133 u64 size = 0;
5134 perf_output_put(handle, size);
5135 } else {
5136 unsigned long sp;
5137 unsigned int rem;
5138 u64 dyn_size;
5139
5140 /*
5141 * We dump:
5142 * static size
5143 * - the size requested by user or the best one we can fit
5144 * in to the sample max size
5145 * data
5146 * - user stack dump data
5147 * dynamic size
5148 * - the actual dumped size
5149 */
5150
5151 /* Static size. */
5152 perf_output_put(handle, dump_size);
5153
5154 /* Data. */
5155 sp = perf_user_stack_pointer(regs);
5156 rem = __output_copy_user(handle, (void *) sp, dump_size);
5157 dyn_size = dump_size - rem;
5158
5159 perf_output_skip(handle, rem);
5160
5161 /* Dynamic size. */
5162 perf_output_put(handle, dyn_size);
5163 }
5164}
5165
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005166static void __perf_event_header__init_id(struct perf_event_header *header,
5167 struct perf_sample_data *data,
5168 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005169{
5170 u64 sample_type = event->attr.sample_type;
5171
5172 data->type = sample_type;
5173 header->size += event->id_header_size;
5174
5175 if (sample_type & PERF_SAMPLE_TID) {
5176 /* namespace issues */
5177 data->tid_entry.pid = perf_event_pid(event, current);
5178 data->tid_entry.tid = perf_event_tid(event, current);
5179 }
5180
5181 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005182 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005183
Adrian Hunterff3d5272013-08-27 11:23:07 +03005184 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005185 data->id = primary_event_id(event);
5186
5187 if (sample_type & PERF_SAMPLE_STREAM_ID)
5188 data->stream_id = event->id;
5189
5190 if (sample_type & PERF_SAMPLE_CPU) {
5191 data->cpu_entry.cpu = raw_smp_processor_id();
5192 data->cpu_entry.reserved = 0;
5193 }
5194}
5195
Frederic Weisbecker76369132011-05-19 19:55:04 +02005196void perf_event_header__init_id(struct perf_event_header *header,
5197 struct perf_sample_data *data,
5198 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005199{
5200 if (event->attr.sample_id_all)
5201 __perf_event_header__init_id(header, data, event);
5202}
5203
5204static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5205 struct perf_sample_data *data)
5206{
5207 u64 sample_type = data->type;
5208
5209 if (sample_type & PERF_SAMPLE_TID)
5210 perf_output_put(handle, data->tid_entry);
5211
5212 if (sample_type & PERF_SAMPLE_TIME)
5213 perf_output_put(handle, data->time);
5214
5215 if (sample_type & PERF_SAMPLE_ID)
5216 perf_output_put(handle, data->id);
5217
5218 if (sample_type & PERF_SAMPLE_STREAM_ID)
5219 perf_output_put(handle, data->stream_id);
5220
5221 if (sample_type & PERF_SAMPLE_CPU)
5222 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005223
5224 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5225 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005226}
5227
Frederic Weisbecker76369132011-05-19 19:55:04 +02005228void perf_event__output_id_sample(struct perf_event *event,
5229 struct perf_output_handle *handle,
5230 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005231{
5232 if (event->attr.sample_id_all)
5233 __perf_event__output_id_sample(handle, sample);
5234}
5235
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005236static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005237 struct perf_event *event,
5238 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005239{
5240 u64 read_format = event->attr.read_format;
5241 u64 values[4];
5242 int n = 0;
5243
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005244 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005245 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005246 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005247 atomic64_read(&event->child_total_time_enabled);
5248 }
5249 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005250 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005251 atomic64_read(&event->child_total_time_running);
5252 }
5253 if (read_format & PERF_FORMAT_ID)
5254 values[n++] = primary_event_id(event);
5255
Frederic Weisbecker76369132011-05-19 19:55:04 +02005256 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005257}
5258
5259/*
5260 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
5261 */
5262static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005263 struct perf_event *event,
5264 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005265{
5266 struct perf_event *leader = event->group_leader, *sub;
5267 u64 read_format = event->attr.read_format;
5268 u64 values[5];
5269 int n = 0;
5270
5271 values[n++] = 1 + leader->nr_siblings;
5272
5273 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005274 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005275
5276 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005277 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005278
5279 if (leader != event)
5280 leader->pmu->read(leader);
5281
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005282 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005283 if (read_format & PERF_FORMAT_ID)
5284 values[n++] = primary_event_id(leader);
5285
Frederic Weisbecker76369132011-05-19 19:55:04 +02005286 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005287
5288 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5289 n = 0;
5290
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005291 if ((sub != event) &&
5292 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005293 sub->pmu->read(sub);
5294
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005295 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005296 if (read_format & PERF_FORMAT_ID)
5297 values[n++] = primary_event_id(sub);
5298
Frederic Weisbecker76369132011-05-19 19:55:04 +02005299 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005300 }
5301}
5302
Stephane Eranianeed01522010-10-26 16:08:01 +02005303#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5304 PERF_FORMAT_TOTAL_TIME_RUNNING)
5305
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005306static void perf_output_read(struct perf_output_handle *handle,
5307 struct perf_event *event)
5308{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005309 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005310 u64 read_format = event->attr.read_format;
5311
5312 /*
5313 * compute total_time_enabled, total_time_running
5314 * based on snapshot values taken when the event
5315 * was last scheduled in.
5316 *
5317 * we cannot simply called update_context_time()
5318 * because of locking issue as we are called in
5319 * NMI context
5320 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005321 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005322 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005323
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005324 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005325 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005326 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005327 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005328}
5329
5330void perf_output_sample(struct perf_output_handle *handle,
5331 struct perf_event_header *header,
5332 struct perf_sample_data *data,
5333 struct perf_event *event)
5334{
5335 u64 sample_type = data->type;
5336
5337 perf_output_put(handle, *header);
5338
Adrian Hunterff3d5272013-08-27 11:23:07 +03005339 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5340 perf_output_put(handle, data->id);
5341
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005342 if (sample_type & PERF_SAMPLE_IP)
5343 perf_output_put(handle, data->ip);
5344
5345 if (sample_type & PERF_SAMPLE_TID)
5346 perf_output_put(handle, data->tid_entry);
5347
5348 if (sample_type & PERF_SAMPLE_TIME)
5349 perf_output_put(handle, data->time);
5350
5351 if (sample_type & PERF_SAMPLE_ADDR)
5352 perf_output_put(handle, data->addr);
5353
5354 if (sample_type & PERF_SAMPLE_ID)
5355 perf_output_put(handle, data->id);
5356
5357 if (sample_type & PERF_SAMPLE_STREAM_ID)
5358 perf_output_put(handle, data->stream_id);
5359
5360 if (sample_type & PERF_SAMPLE_CPU)
5361 perf_output_put(handle, data->cpu_entry);
5362
5363 if (sample_type & PERF_SAMPLE_PERIOD)
5364 perf_output_put(handle, data->period);
5365
5366 if (sample_type & PERF_SAMPLE_READ)
5367 perf_output_read(handle, event);
5368
5369 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5370 if (data->callchain) {
5371 int size = 1;
5372
5373 if (data->callchain)
5374 size += data->callchain->nr;
5375
5376 size *= sizeof(u64);
5377
Frederic Weisbecker76369132011-05-19 19:55:04 +02005378 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005379 } else {
5380 u64 nr = 0;
5381 perf_output_put(handle, nr);
5382 }
5383 }
5384
5385 if (sample_type & PERF_SAMPLE_RAW) {
5386 if (data->raw) {
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005387 u32 raw_size = data->raw->size;
5388 u32 real_size = round_up(raw_size + sizeof(u32),
5389 sizeof(u64)) - sizeof(u32);
5390 u64 zero = 0;
5391
5392 perf_output_put(handle, real_size);
5393 __output_copy(handle, data->raw->data, raw_size);
5394 if (real_size - raw_size)
5395 __output_copy(handle, &zero, real_size - raw_size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005396 } else {
5397 struct {
5398 u32 size;
5399 u32 data;
5400 } raw = {
5401 .size = sizeof(u32),
5402 .data = 0,
5403 };
5404 perf_output_put(handle, raw);
5405 }
5406 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005407
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005408 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5409 if (data->br_stack) {
5410 size_t size;
5411
5412 size = data->br_stack->nr
5413 * sizeof(struct perf_branch_entry);
5414
5415 perf_output_put(handle, data->br_stack->nr);
5416 perf_output_copy(handle, data->br_stack->entries, size);
5417 } else {
5418 /*
5419 * we always store at least the value of nr
5420 */
5421 u64 nr = 0;
5422 perf_output_put(handle, nr);
5423 }
5424 }
Jiri Olsa40189942012-08-07 15:20:37 +02005425
5426 if (sample_type & PERF_SAMPLE_REGS_USER) {
5427 u64 abi = data->regs_user.abi;
5428
5429 /*
5430 * If there are no regs to dump, notice it through
5431 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5432 */
5433 perf_output_put(handle, abi);
5434
5435 if (abi) {
5436 u64 mask = event->attr.sample_regs_user;
5437 perf_output_sample_regs(handle,
5438 data->regs_user.regs,
5439 mask);
5440 }
5441 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005442
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005443 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005444 perf_output_sample_ustack(handle,
5445 data->stack_user_size,
5446 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005447 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005448
5449 if (sample_type & PERF_SAMPLE_WEIGHT)
5450 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005451
5452 if (sample_type & PERF_SAMPLE_DATA_SRC)
5453 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005454
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005455 if (sample_type & PERF_SAMPLE_TRANSACTION)
5456 perf_output_put(handle, data->txn);
5457
Stephane Eranian60e23642014-09-24 13:48:37 +02005458 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5459 u64 abi = data->regs_intr.abi;
5460 /*
5461 * If there are no regs to dump, notice it through
5462 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5463 */
5464 perf_output_put(handle, abi);
5465
5466 if (abi) {
5467 u64 mask = event->attr.sample_regs_intr;
5468
5469 perf_output_sample_regs(handle,
5470 data->regs_intr.regs,
5471 mask);
5472 }
5473 }
5474
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005475 if (!event->attr.watermark) {
5476 int wakeup_events = event->attr.wakeup_events;
5477
5478 if (wakeup_events) {
5479 struct ring_buffer *rb = handle->rb;
5480 int events = local_inc_return(&rb->events);
5481
5482 if (events >= wakeup_events) {
5483 local_sub(wakeup_events, &rb->events);
5484 local_inc(&rb->wakeup);
5485 }
5486 }
5487 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005488}
5489
5490void perf_prepare_sample(struct perf_event_header *header,
5491 struct perf_sample_data *data,
5492 struct perf_event *event,
5493 struct pt_regs *regs)
5494{
5495 u64 sample_type = event->attr.sample_type;
5496
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005497 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005498 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005499
5500 header->misc = 0;
5501 header->misc |= perf_misc_flags(regs);
5502
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005503 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005504
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005505 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005506 data->ip = perf_instruction_pointer(regs);
5507
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005508 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5509 int size = 1;
5510
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005511 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005512
5513 if (data->callchain)
5514 size += data->callchain->nr;
5515
5516 header->size += size * sizeof(u64);
5517 }
5518
5519 if (sample_type & PERF_SAMPLE_RAW) {
5520 int size = sizeof(u32);
5521
5522 if (data->raw)
5523 size += data->raw->size;
5524 else
5525 size += sizeof(u32);
5526
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005527 header->size += round_up(size, sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005528 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005529
5530 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5531 int size = sizeof(u64); /* nr */
5532 if (data->br_stack) {
5533 size += data->br_stack->nr
5534 * sizeof(struct perf_branch_entry);
5535 }
5536 header->size += size;
5537 }
Jiri Olsa40189942012-08-07 15:20:37 +02005538
Peter Zijlstra25657112014-09-24 13:48:42 +02005539 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005540 perf_sample_regs_user(&data->regs_user, regs,
5541 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005542
Jiri Olsa40189942012-08-07 15:20:37 +02005543 if (sample_type & PERF_SAMPLE_REGS_USER) {
5544 /* regs dump ABI info */
5545 int size = sizeof(u64);
5546
Jiri Olsa40189942012-08-07 15:20:37 +02005547 if (data->regs_user.regs) {
5548 u64 mask = event->attr.sample_regs_user;
5549 size += hweight64(mask) * sizeof(u64);
5550 }
5551
5552 header->size += size;
5553 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005554
5555 if (sample_type & PERF_SAMPLE_STACK_USER) {
5556 /*
5557 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5558 * processed as the last one or have additional check added
5559 * in case new sample type is added, because we could eat
5560 * up the rest of the sample size.
5561 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005562 u16 stack_size = event->attr.sample_stack_user;
5563 u16 size = sizeof(u64);
5564
Jiri Olsac5ebced2012-08-07 15:20:40 +02005565 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005566 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005567
5568 /*
5569 * If there is something to dump, add space for the dump
5570 * itself and for the field that tells the dynamic size,
5571 * which is how many have been actually dumped.
5572 */
5573 if (stack_size)
5574 size += sizeof(u64) + stack_size;
5575
5576 data->stack_user_size = stack_size;
5577 header->size += size;
5578 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005579
5580 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5581 /* regs dump ABI info */
5582 int size = sizeof(u64);
5583
5584 perf_sample_regs_intr(&data->regs_intr, regs);
5585
5586 if (data->regs_intr.regs) {
5587 u64 mask = event->attr.sample_regs_intr;
5588
5589 size += hweight64(mask) * sizeof(u64);
5590 }
5591
5592 header->size += size;
5593 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005594}
5595
Yan, Zheng21509082015-05-06 15:33:49 -04005596void perf_event_output(struct perf_event *event,
5597 struct perf_sample_data *data,
5598 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005599{
5600 struct perf_output_handle handle;
5601 struct perf_event_header header;
5602
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005603 /* protect the callchain buffers */
5604 rcu_read_lock();
5605
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005606 perf_prepare_sample(&header, data, event, regs);
5607
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005608 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005609 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005610
5611 perf_output_sample(&handle, &header, data, event);
5612
5613 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005614
5615exit:
5616 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005617}
5618
5619/*
5620 * read event_id
5621 */
5622
5623struct perf_read_event {
5624 struct perf_event_header header;
5625
5626 u32 pid;
5627 u32 tid;
5628};
5629
5630static void
5631perf_event_read_event(struct perf_event *event,
5632 struct task_struct *task)
5633{
5634 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005635 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005636 struct perf_read_event read_event = {
5637 .header = {
5638 .type = PERF_RECORD_READ,
5639 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005640 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005641 },
5642 .pid = perf_event_pid(event, task),
5643 .tid = perf_event_tid(event, task),
5644 };
5645 int ret;
5646
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005647 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005648 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005649 if (ret)
5650 return;
5651
5652 perf_output_put(&handle, read_event);
5653 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005654 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005655
5656 perf_output_end(&handle);
5657}
5658
Jiri Olsa52d857a2013-05-06 18:27:18 +02005659typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5660
5661static void
5662perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005663 perf_event_aux_output_cb output,
5664 void *data)
5665{
5666 struct perf_event *event;
5667
5668 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5669 if (event->state < PERF_EVENT_STATE_INACTIVE)
5670 continue;
5671 if (!event_filter_match(event))
5672 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02005673 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005674 }
5675}
5676
5677static void
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005678perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data,
5679 struct perf_event_context *task_ctx)
5680{
5681 rcu_read_lock();
5682 preempt_disable();
5683 perf_event_aux_ctx(task_ctx, output, data);
5684 preempt_enable();
5685 rcu_read_unlock();
5686}
5687
5688static void
Jiri Olsa67516842013-07-09 18:56:31 +02005689perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005690 struct perf_event_context *task_ctx)
5691{
5692 struct perf_cpu_context *cpuctx;
5693 struct perf_event_context *ctx;
5694 struct pmu *pmu;
5695 int ctxn;
5696
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005697 /*
5698 * If we have task_ctx != NULL we only notify
5699 * the task context itself. The task_ctx is set
5700 * only for EXIT events before releasing task
5701 * context.
5702 */
5703 if (task_ctx) {
5704 perf_event_aux_task_ctx(output, data, task_ctx);
5705 return;
5706 }
5707
Jiri Olsa52d857a2013-05-06 18:27:18 +02005708 rcu_read_lock();
5709 list_for_each_entry_rcu(pmu, &pmus, entry) {
5710 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5711 if (cpuctx->unique_pmu != pmu)
5712 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02005713 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005714 ctxn = pmu->task_ctx_nr;
5715 if (ctxn < 0)
5716 goto next;
5717 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5718 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02005719 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005720next:
5721 put_cpu_ptr(pmu->pmu_cpu_context);
5722 }
Jiri Olsa52d857a2013-05-06 18:27:18 +02005723 rcu_read_unlock();
5724}
5725
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005726/*
5727 * task tracking -- fork/exit
5728 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02005729 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005730 */
5731
5732struct perf_task_event {
5733 struct task_struct *task;
5734 struct perf_event_context *task_ctx;
5735
5736 struct {
5737 struct perf_event_header header;
5738
5739 u32 pid;
5740 u32 ppid;
5741 u32 tid;
5742 u32 ptid;
5743 u64 time;
5744 } event_id;
5745};
5746
Jiri Olsa67516842013-07-09 18:56:31 +02005747static int perf_event_task_match(struct perf_event *event)
5748{
Stephane Eranian13d7a242013-08-21 12:10:24 +02005749 return event->attr.comm || event->attr.mmap ||
5750 event->attr.mmap2 || event->attr.mmap_data ||
5751 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02005752}
5753
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005754static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005755 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005756{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005757 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005758 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005759 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005760 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005761 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01005762
Jiri Olsa67516842013-07-09 18:56:31 +02005763 if (!perf_event_task_match(event))
5764 return;
5765
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005766 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005767
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005768 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005769 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02005770 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005771 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005772
5773 task_event->event_id.pid = perf_event_pid(event, task);
5774 task_event->event_id.ppid = perf_event_pid(event, current);
5775
5776 task_event->event_id.tid = perf_event_tid(event, task);
5777 task_event->event_id.ptid = perf_event_tid(event, current);
5778
Peter Zijlstra34f43922015-02-20 14:05:38 +01005779 task_event->event_id.time = perf_event_clock(event);
5780
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005781 perf_output_put(&handle, task_event->event_id);
5782
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005783 perf_event__output_id_sample(event, &handle, &sample);
5784
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005785 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005786out:
5787 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005788}
5789
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005790static void perf_event_task(struct task_struct *task,
5791 struct perf_event_context *task_ctx,
5792 int new)
5793{
5794 struct perf_task_event task_event;
5795
5796 if (!atomic_read(&nr_comm_events) &&
5797 !atomic_read(&nr_mmap_events) &&
5798 !atomic_read(&nr_task_events))
5799 return;
5800
5801 task_event = (struct perf_task_event){
5802 .task = task,
5803 .task_ctx = task_ctx,
5804 .event_id = {
5805 .header = {
5806 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5807 .misc = 0,
5808 .size = sizeof(task_event.event_id),
5809 },
5810 /* .pid */
5811 /* .ppid */
5812 /* .tid */
5813 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01005814 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005815 },
5816 };
5817
Jiri Olsa67516842013-07-09 18:56:31 +02005818 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005819 &task_event,
5820 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005821}
5822
5823void perf_event_fork(struct task_struct *task)
5824{
5825 perf_event_task(task, NULL, 1);
5826}
5827
5828/*
5829 * comm tracking
5830 */
5831
5832struct perf_comm_event {
5833 struct task_struct *task;
5834 char *comm;
5835 int comm_size;
5836
5837 struct {
5838 struct perf_event_header header;
5839
5840 u32 pid;
5841 u32 tid;
5842 } event_id;
5843};
5844
Jiri Olsa67516842013-07-09 18:56:31 +02005845static int perf_event_comm_match(struct perf_event *event)
5846{
5847 return event->attr.comm;
5848}
5849
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005850static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005851 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005852{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005853 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005854 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005855 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005856 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005857 int ret;
5858
Jiri Olsa67516842013-07-09 18:56:31 +02005859 if (!perf_event_comm_match(event))
5860 return;
5861
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005862 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5863 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005864 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005865
5866 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005867 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005868
5869 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5870 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5871
5872 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005873 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005874 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005875
5876 perf_event__output_id_sample(event, &handle, &sample);
5877
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005878 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005879out:
5880 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005881}
5882
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005883static void perf_event_comm_event(struct perf_comm_event *comm_event)
5884{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005885 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005886 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005887
5888 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005889 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005890 size = ALIGN(strlen(comm)+1, sizeof(u64));
5891
5892 comm_event->comm = comm;
5893 comm_event->comm_size = size;
5894
5895 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005896
Jiri Olsa67516842013-07-09 18:56:31 +02005897 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005898 comm_event,
5899 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005900}
5901
Adrian Hunter82b89772014-05-28 11:45:04 +03005902void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005903{
5904 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005905
5906 if (!atomic_read(&nr_comm_events))
5907 return;
5908
5909 comm_event = (struct perf_comm_event){
5910 .task = task,
5911 /* .comm */
5912 /* .comm_size */
5913 .event_id = {
5914 .header = {
5915 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005916 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005917 /* .size */
5918 },
5919 /* .pid */
5920 /* .tid */
5921 },
5922 };
5923
5924 perf_event_comm_event(&comm_event);
5925}
5926
5927/*
5928 * mmap tracking
5929 */
5930
5931struct perf_mmap_event {
5932 struct vm_area_struct *vma;
5933
5934 const char *file_name;
5935 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005936 int maj, min;
5937 u64 ino;
5938 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005939 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005940
5941 struct {
5942 struct perf_event_header header;
5943
5944 u32 pid;
5945 u32 tid;
5946 u64 start;
5947 u64 len;
5948 u64 pgoff;
5949 } event_id;
5950};
5951
Jiri Olsa67516842013-07-09 18:56:31 +02005952static int perf_event_mmap_match(struct perf_event *event,
5953 void *data)
5954{
5955 struct perf_mmap_event *mmap_event = data;
5956 struct vm_area_struct *vma = mmap_event->vma;
5957 int executable = vma->vm_flags & VM_EXEC;
5958
5959 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005960 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005961}
5962
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005963static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005964 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005965{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005966 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005967 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005968 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005969 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005970 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005971
Jiri Olsa67516842013-07-09 18:56:31 +02005972 if (!perf_event_mmap_match(event, data))
5973 return;
5974
Stephane Eranian13d7a242013-08-21 12:10:24 +02005975 if (event->attr.mmap2) {
5976 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5977 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5978 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5979 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005980 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005981 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
5982 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005983 }
5984
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005985 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5986 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005987 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005988 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005989 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005990
5991 mmap_event->event_id.pid = perf_event_pid(event, current);
5992 mmap_event->event_id.tid = perf_event_tid(event, current);
5993
5994 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005995
5996 if (event->attr.mmap2) {
5997 perf_output_put(&handle, mmap_event->maj);
5998 perf_output_put(&handle, mmap_event->min);
5999 perf_output_put(&handle, mmap_event->ino);
6000 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006001 perf_output_put(&handle, mmap_event->prot);
6002 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006003 }
6004
Frederic Weisbecker76369132011-05-19 19:55:04 +02006005 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006006 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006007
6008 perf_event__output_id_sample(event, &handle, &sample);
6009
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006010 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006011out:
6012 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006013}
6014
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006015static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
6016{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006017 struct vm_area_struct *vma = mmap_event->vma;
6018 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006019 int maj = 0, min = 0;
6020 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006021 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006022 unsigned int size;
6023 char tmp[16];
6024 char *buf = NULL;
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02006025 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006026
6027 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02006028 struct inode *inode;
6029 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006030
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02006031 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006032 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006033 name = "//enomem";
6034 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006035 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006036 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006037 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006038 * need to add enough zero bytes after the string to handle
6039 * the 64bit alignment we do later.
6040 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02006041 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006042 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006043 name = "//toolong";
6044 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006045 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02006046 inode = file_inode(vma->vm_file);
6047 dev = inode->i_sb->s_dev;
6048 ino = inode->i_ino;
6049 gen = inode->i_generation;
6050 maj = MAJOR(dev);
6051 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006052
6053 if (vma->vm_flags & VM_READ)
6054 prot |= PROT_READ;
6055 if (vma->vm_flags & VM_WRITE)
6056 prot |= PROT_WRITE;
6057 if (vma->vm_flags & VM_EXEC)
6058 prot |= PROT_EXEC;
6059
6060 if (vma->vm_flags & VM_MAYSHARE)
6061 flags = MAP_SHARED;
6062 else
6063 flags = MAP_PRIVATE;
6064
6065 if (vma->vm_flags & VM_DENYWRITE)
6066 flags |= MAP_DENYWRITE;
6067 if (vma->vm_flags & VM_MAYEXEC)
6068 flags |= MAP_EXECUTABLE;
6069 if (vma->vm_flags & VM_LOCKED)
6070 flags |= MAP_LOCKED;
6071 if (vma->vm_flags & VM_HUGETLB)
6072 flags |= MAP_HUGETLB;
6073
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006074 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006075 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006076 if (vma->vm_ops && vma->vm_ops->name) {
6077 name = (char *) vma->vm_ops->name(vma);
6078 if (name)
6079 goto cpy_name;
6080 }
6081
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02006082 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006083 if (name)
6084 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006085
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006086 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006087 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006088 name = "[heap]";
6089 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006090 }
6091 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006092 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006093 name = "[stack]";
6094 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006095 }
6096
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006097 name = "//anon";
6098 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006099 }
6100
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006101cpy_name:
6102 strlcpy(tmp, name, sizeof(tmp));
6103 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006104got_name:
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02006105 /*
6106 * Since our buffer works in 8 byte units we need to align our string
6107 * size to a multiple of 8. However, we must guarantee the tail end is
6108 * zero'd out to avoid leaking random bits to userspace.
6109 */
6110 size = strlen(name)+1;
6111 while (!IS_ALIGNED(size, sizeof(u64)))
6112 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006113
6114 mmap_event->file_name = name;
6115 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006116 mmap_event->maj = maj;
6117 mmap_event->min = min;
6118 mmap_event->ino = ino;
6119 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006120 mmap_event->prot = prot;
6121 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006122
Stephane Eranian2fe85422013-01-24 16:10:39 +01006123 if (!(vma->vm_flags & VM_EXEC))
6124 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6125
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006126 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6127
Jiri Olsa67516842013-07-09 18:56:31 +02006128 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006129 mmap_event,
6130 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006131
6132 kfree(buf);
6133}
6134
Eric B Munson3af9e852010-05-18 15:30:49 +01006135void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006136{
6137 struct perf_mmap_event mmap_event;
6138
6139 if (!atomic_read(&nr_mmap_events))
6140 return;
6141
6142 mmap_event = (struct perf_mmap_event){
6143 .vma = vma,
6144 /* .file_name */
6145 /* .file_size */
6146 .event_id = {
6147 .header = {
6148 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006149 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006150 /* .size */
6151 },
6152 /* .pid */
6153 /* .tid */
6154 .start = vma->vm_start,
6155 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006156 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006157 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006158 /* .maj (attr_mmap2 only) */
6159 /* .min (attr_mmap2 only) */
6160 /* .ino (attr_mmap2 only) */
6161 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006162 /* .prot (attr_mmap2 only) */
6163 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006164 };
6165
6166 perf_event_mmap_event(&mmap_event);
6167}
6168
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006169void perf_event_aux_event(struct perf_event *event, unsigned long head,
6170 unsigned long size, u64 flags)
6171{
6172 struct perf_output_handle handle;
6173 struct perf_sample_data sample;
6174 struct perf_aux_event {
6175 struct perf_event_header header;
6176 u64 offset;
6177 u64 size;
6178 u64 flags;
6179 } rec = {
6180 .header = {
6181 .type = PERF_RECORD_AUX,
6182 .misc = 0,
6183 .size = sizeof(rec),
6184 },
6185 .offset = head,
6186 .size = size,
6187 .flags = flags,
6188 };
6189 int ret;
6190
6191 perf_event_header__init_id(&rec.header, &sample, event);
6192 ret = perf_output_begin(&handle, event, rec.header.size);
6193
6194 if (ret)
6195 return;
6196
6197 perf_output_put(&handle, rec);
6198 perf_event__output_id_sample(event, &handle, &sample);
6199
6200 perf_output_end(&handle);
6201}
6202
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006203/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006204 * Lost/dropped samples logging
6205 */
6206void perf_log_lost_samples(struct perf_event *event, u64 lost)
6207{
6208 struct perf_output_handle handle;
6209 struct perf_sample_data sample;
6210 int ret;
6211
6212 struct {
6213 struct perf_event_header header;
6214 u64 lost;
6215 } lost_samples_event = {
6216 .header = {
6217 .type = PERF_RECORD_LOST_SAMPLES,
6218 .misc = 0,
6219 .size = sizeof(lost_samples_event),
6220 },
6221 .lost = lost,
6222 };
6223
6224 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6225
6226 ret = perf_output_begin(&handle, event,
6227 lost_samples_event.header.size);
6228 if (ret)
6229 return;
6230
6231 perf_output_put(&handle, lost_samples_event);
6232 perf_event__output_id_sample(event, &handle, &sample);
6233 perf_output_end(&handle);
6234}
6235
6236/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006237 * context_switch tracking
6238 */
6239
6240struct perf_switch_event {
6241 struct task_struct *task;
6242 struct task_struct *next_prev;
6243
6244 struct {
6245 struct perf_event_header header;
6246 u32 next_prev_pid;
6247 u32 next_prev_tid;
6248 } event_id;
6249};
6250
6251static int perf_event_switch_match(struct perf_event *event)
6252{
6253 return event->attr.context_switch;
6254}
6255
6256static void perf_event_switch_output(struct perf_event *event, void *data)
6257{
6258 struct perf_switch_event *se = data;
6259 struct perf_output_handle handle;
6260 struct perf_sample_data sample;
6261 int ret;
6262
6263 if (!perf_event_switch_match(event))
6264 return;
6265
6266 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6267 if (event->ctx->task) {
6268 se->event_id.header.type = PERF_RECORD_SWITCH;
6269 se->event_id.header.size = sizeof(se->event_id.header);
6270 } else {
6271 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6272 se->event_id.header.size = sizeof(se->event_id);
6273 se->event_id.next_prev_pid =
6274 perf_event_pid(event, se->next_prev);
6275 se->event_id.next_prev_tid =
6276 perf_event_tid(event, se->next_prev);
6277 }
6278
6279 perf_event_header__init_id(&se->event_id.header, &sample, event);
6280
6281 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6282 if (ret)
6283 return;
6284
6285 if (event->ctx->task)
6286 perf_output_put(&handle, se->event_id.header);
6287 else
6288 perf_output_put(&handle, se->event_id);
6289
6290 perf_event__output_id_sample(event, &handle, &sample);
6291
6292 perf_output_end(&handle);
6293}
6294
6295static void perf_event_switch(struct task_struct *task,
6296 struct task_struct *next_prev, bool sched_in)
6297{
6298 struct perf_switch_event switch_event;
6299
6300 /* N.B. caller checks nr_switch_events != 0 */
6301
6302 switch_event = (struct perf_switch_event){
6303 .task = task,
6304 .next_prev = next_prev,
6305 .event_id = {
6306 .header = {
6307 /* .type */
6308 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
6309 /* .size */
6310 },
6311 /* .next_prev_pid */
6312 /* .next_prev_tid */
6313 },
6314 };
6315
6316 perf_event_aux(perf_event_switch_output,
6317 &switch_event,
6318 NULL);
6319}
6320
6321/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006322 * IRQ throttle logging
6323 */
6324
6325static void perf_log_throttle(struct perf_event *event, int enable)
6326{
6327 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006328 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006329 int ret;
6330
6331 struct {
6332 struct perf_event_header header;
6333 u64 time;
6334 u64 id;
6335 u64 stream_id;
6336 } throttle_event = {
6337 .header = {
6338 .type = PERF_RECORD_THROTTLE,
6339 .misc = 0,
6340 .size = sizeof(throttle_event),
6341 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01006342 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006343 .id = primary_event_id(event),
6344 .stream_id = event->id,
6345 };
6346
6347 if (enable)
6348 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
6349
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006350 perf_event_header__init_id(&throttle_event.header, &sample, event);
6351
6352 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006353 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006354 if (ret)
6355 return;
6356
6357 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006358 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006359 perf_output_end(&handle);
6360}
6361
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006362static void perf_log_itrace_start(struct perf_event *event)
6363{
6364 struct perf_output_handle handle;
6365 struct perf_sample_data sample;
6366 struct perf_aux_event {
6367 struct perf_event_header header;
6368 u32 pid;
6369 u32 tid;
6370 } rec;
6371 int ret;
6372
6373 if (event->parent)
6374 event = event->parent;
6375
6376 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
6377 event->hw.itrace_started)
6378 return;
6379
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006380 rec.header.type = PERF_RECORD_ITRACE_START;
6381 rec.header.misc = 0;
6382 rec.header.size = sizeof(rec);
6383 rec.pid = perf_event_pid(event, current);
6384 rec.tid = perf_event_tid(event, current);
6385
6386 perf_event_header__init_id(&rec.header, &sample, event);
6387 ret = perf_output_begin(&handle, event, rec.header.size);
6388
6389 if (ret)
6390 return;
6391
6392 perf_output_put(&handle, rec);
6393 perf_event__output_id_sample(event, &handle, &sample);
6394
6395 perf_output_end(&handle);
6396}
6397
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006398/*
6399 * Generic event overflow handling, sampling.
6400 */
6401
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006402static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006403 int throttle, struct perf_sample_data *data,
6404 struct pt_regs *regs)
6405{
6406 int events = atomic_read(&event->event_limit);
6407 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006408 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006409 int ret = 0;
6410
Peter Zijlstra96398822010-11-24 18:55:29 +01006411 /*
6412 * Non-sampling counters might still use the PMI to fold short
6413 * hardware counters, ignore those.
6414 */
6415 if (unlikely(!is_sampling_event(event)))
6416 return 0;
6417
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006418 seq = __this_cpu_read(perf_throttled_seq);
6419 if (seq != hwc->interrupts_seq) {
6420 hwc->interrupts_seq = seq;
6421 hwc->interrupts = 1;
6422 } else {
6423 hwc->interrupts++;
6424 if (unlikely(throttle
6425 && hwc->interrupts >= max_samples_per_tick)) {
6426 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01006427 hwc->interrupts = MAX_INTERRUPTS;
6428 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02006429 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006430 ret = 1;
6431 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006432 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006433
6434 if (event->attr.freq) {
6435 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01006436 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006437
Peter Zijlstraabd50712010-01-26 18:50:16 +01006438 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006439
Peter Zijlstraabd50712010-01-26 18:50:16 +01006440 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01006441 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006442 }
6443
6444 /*
6445 * XXX event_limit might not quite work as expected on inherited
6446 * events
6447 */
6448
6449 event->pending_kill = POLL_IN;
6450 if (events && atomic_dec_and_test(&event->event_limit)) {
6451 ret = 1;
6452 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006453 event->pending_disable = 1;
6454 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006455 }
6456
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006457 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006458 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006459 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006460 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006461
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006462 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006463 event->pending_wakeup = 1;
6464 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02006465 }
6466
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006467 return ret;
6468}
6469
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006470int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006471 struct perf_sample_data *data,
6472 struct pt_regs *regs)
6473{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006474 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006475}
6476
6477/*
6478 * Generic software event infrastructure
6479 */
6480
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006481struct swevent_htable {
6482 struct swevent_hlist *swevent_hlist;
6483 struct mutex hlist_mutex;
6484 int hlist_refcount;
6485
6486 /* Recursion avoidance in each contexts */
6487 int recursion[PERF_NR_CONTEXTS];
6488};
6489
6490static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
6491
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006492/*
6493 * We directly increment event->count and keep a second value in
6494 * event->hw.period_left to count intervals. This period event
6495 * is kept in the range [-sample_period, 0] so that we can use the
6496 * sign as trigger.
6497 */
6498
Jiri Olsaab573842013-05-01 17:25:44 +02006499u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006500{
6501 struct hw_perf_event *hwc = &event->hw;
6502 u64 period = hwc->last_period;
6503 u64 nr, offset;
6504 s64 old, val;
6505
6506 hwc->last_period = hwc->sample_period;
6507
6508again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02006509 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006510 if (val < 0)
6511 return 0;
6512
6513 nr = div64_u64(period + val, period);
6514 offset = nr * period;
6515 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02006516 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006517 goto again;
6518
6519 return nr;
6520}
6521
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006522static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006523 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006524 struct pt_regs *regs)
6525{
6526 struct hw_perf_event *hwc = &event->hw;
6527 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006528
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006529 if (!overflow)
6530 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006531
6532 if (hwc->interrupts == MAX_INTERRUPTS)
6533 return;
6534
6535 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006536 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006537 data, regs)) {
6538 /*
6539 * We inhibit the overflow from happening when
6540 * hwc->interrupts == MAX_INTERRUPTS.
6541 */
6542 break;
6543 }
6544 throttle = 1;
6545 }
6546}
6547
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006548static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006549 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006550 struct pt_regs *regs)
6551{
6552 struct hw_perf_event *hwc = &event->hw;
6553
Peter Zijlstrae7850592010-05-21 14:43:08 +02006554 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006555
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006556 if (!regs)
6557 return;
6558
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006559 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006560 return;
6561
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03006562 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
6563 data->period = nr;
6564 return perf_swevent_overflow(event, 1, data, regs);
6565 } else
6566 data->period = event->hw.last_period;
6567
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006568 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006569 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006570
Peter Zijlstrae7850592010-05-21 14:43:08 +02006571 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006572 return;
6573
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006574 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006575}
6576
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006577static int perf_exclude_event(struct perf_event *event,
6578 struct pt_regs *regs)
6579{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006580 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01006581 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006582
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006583 if (regs) {
6584 if (event->attr.exclude_user && user_mode(regs))
6585 return 1;
6586
6587 if (event->attr.exclude_kernel && !user_mode(regs))
6588 return 1;
6589 }
6590
6591 return 0;
6592}
6593
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006594static int perf_swevent_match(struct perf_event *event,
6595 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08006596 u32 event_id,
6597 struct perf_sample_data *data,
6598 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006599{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006600 if (event->attr.type != type)
6601 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006602
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006603 if (event->attr.config != event_id)
6604 return 0;
6605
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006606 if (perf_exclude_event(event, regs))
6607 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006608
6609 return 1;
6610}
6611
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006612static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006613{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006614 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006615
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006616 return hash_64(val, SWEVENT_HLIST_BITS);
6617}
6618
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006619static inline struct hlist_head *
6620__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006621{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006622 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006623
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006624 return &hlist->heads[hash];
6625}
6626
6627/* For the read side: events when they trigger */
6628static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006629find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006630{
6631 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006632
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006633 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006634 if (!hlist)
6635 return NULL;
6636
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006637 return __find_swevent_head(hlist, type, event_id);
6638}
6639
6640/* For the event head insertion and removal in the hlist */
6641static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006642find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006643{
6644 struct swevent_hlist *hlist;
6645 u32 event_id = event->attr.config;
6646 u64 type = event->attr.type;
6647
6648 /*
6649 * Event scheduling is always serialized against hlist allocation
6650 * and release. Which makes the protected version suitable here.
6651 * The context lock guarantees that.
6652 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006653 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006654 lockdep_is_held(&event->ctx->lock));
6655 if (!hlist)
6656 return NULL;
6657
6658 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006659}
6660
6661static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006662 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006663 struct perf_sample_data *data,
6664 struct pt_regs *regs)
6665{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006666 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006667 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006668 struct hlist_head *head;
6669
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006670 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006671 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006672 if (!head)
6673 goto end;
6674
Sasha Levinb67bfe02013-02-27 17:06:00 -08006675 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08006676 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006677 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006678 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006679end:
6680 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006681}
6682
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006683DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
6684
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006685int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006686{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006687 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006688
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006689 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006690}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01006691EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006692
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01006693inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006694{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006695 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006696
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006697 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006698}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006699
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006700void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006701{
Ingo Molnara4234bf2009-11-23 10:57:59 +01006702 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006703
6704 if (WARN_ON_ONCE(!regs))
6705 return;
6706
6707 perf_sample_data_init(&data, addr, 0);
6708 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
6709}
6710
6711void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
6712{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006713 int rctx;
6714
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006715 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006716 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006717 if (unlikely(rctx < 0))
6718 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006719
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006720 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006721
6722 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006723fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006724 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006725}
6726
6727static void perf_swevent_read(struct perf_event *event)
6728{
6729}
6730
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006731static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006732{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006733 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006734 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006735 struct hlist_head *head;
6736
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006737 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006738 hwc->last_period = hwc->sample_period;
6739 perf_swevent_set_period(event);
6740 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006741
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006742 hwc->state = !(flags & PERF_EF_START);
6743
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006744 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01006745 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006746 return -EINVAL;
6747
6748 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08006749 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006750
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006751 return 0;
6752}
6753
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006754static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006755{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006756 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006757}
6758
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006759static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006760{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006761 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006762}
6763
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006764static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006765{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006766 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006767}
6768
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006769/* Deref the hlist from the update side */
6770static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006771swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006772{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006773 return rcu_dereference_protected(swhash->swevent_hlist,
6774 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006775}
6776
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006777static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006778{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006779 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006780
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006781 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006782 return;
6783
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03006784 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08006785 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006786}
6787
6788static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
6789{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006790 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006791
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006792 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006793
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006794 if (!--swhash->hlist_refcount)
6795 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006796
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006797 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006798}
6799
6800static void swevent_hlist_put(struct perf_event *event)
6801{
6802 int cpu;
6803
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006804 for_each_possible_cpu(cpu)
6805 swevent_hlist_put_cpu(event, cpu);
6806}
6807
6808static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
6809{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006810 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006811 int err = 0;
6812
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006813 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006814 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006815 struct swevent_hlist *hlist;
6816
6817 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
6818 if (!hlist) {
6819 err = -ENOMEM;
6820 goto exit;
6821 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006822 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006823 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006824 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006825exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006826 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006827
6828 return err;
6829}
6830
6831static int swevent_hlist_get(struct perf_event *event)
6832{
6833 int err;
6834 int cpu, failed_cpu;
6835
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006836 get_online_cpus();
6837 for_each_possible_cpu(cpu) {
6838 err = swevent_hlist_get_cpu(event, cpu);
6839 if (err) {
6840 failed_cpu = cpu;
6841 goto fail;
6842 }
6843 }
6844 put_online_cpus();
6845
6846 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006847fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006848 for_each_possible_cpu(cpu) {
6849 if (cpu == failed_cpu)
6850 break;
6851 swevent_hlist_put_cpu(event, cpu);
6852 }
6853
6854 put_online_cpus();
6855 return err;
6856}
6857
Ingo Molnarc5905af2012-02-24 08:31:31 +01006858struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006859
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006860static void sw_perf_event_destroy(struct perf_event *event)
6861{
6862 u64 event_id = event->attr.config;
6863
6864 WARN_ON(event->parent);
6865
Ingo Molnarc5905af2012-02-24 08:31:31 +01006866 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006867 swevent_hlist_put(event);
6868}
6869
6870static int perf_swevent_init(struct perf_event *event)
6871{
Tommi Rantala8176cce2013-04-13 22:49:14 +03006872 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006873
6874 if (event->attr.type != PERF_TYPE_SOFTWARE)
6875 return -ENOENT;
6876
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006877 /*
6878 * no branch sampling for software events
6879 */
6880 if (has_branch_stack(event))
6881 return -EOPNOTSUPP;
6882
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006883 switch (event_id) {
6884 case PERF_COUNT_SW_CPU_CLOCK:
6885 case PERF_COUNT_SW_TASK_CLOCK:
6886 return -ENOENT;
6887
6888 default:
6889 break;
6890 }
6891
Dan Carpenterce677832010-10-24 21:50:42 +02006892 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006893 return -ENOENT;
6894
6895 if (!event->parent) {
6896 int err;
6897
6898 err = swevent_hlist_get(event);
6899 if (err)
6900 return err;
6901
Ingo Molnarc5905af2012-02-24 08:31:31 +01006902 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006903 event->destroy = sw_perf_event_destroy;
6904 }
6905
6906 return 0;
6907}
6908
6909static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006910 .task_ctx_nr = perf_sw_context,
6911
Peter Zijlstra34f43922015-02-20 14:05:38 +01006912 .capabilities = PERF_PMU_CAP_NO_NMI,
6913
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006914 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006915 .add = perf_swevent_add,
6916 .del = perf_swevent_del,
6917 .start = perf_swevent_start,
6918 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006919 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006920};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006921
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006922#ifdef CONFIG_EVENT_TRACING
6923
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006924static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006925 struct perf_sample_data *data)
6926{
6927 void *record = data->raw->data;
6928
Peter Zijlstrab71b4372015-11-02 10:50:51 +01006929 /* only top level events have filters set */
6930 if (event->parent)
6931 event = event->parent;
6932
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006933 if (likely(!event->filter) || filter_match_preds(event->filter, record))
6934 return 1;
6935 return 0;
6936}
6937
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006938static int perf_tp_event_match(struct perf_event *event,
6939 struct perf_sample_data *data,
6940 struct pt_regs *regs)
6941{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01006942 if (event->hw.state & PERF_HES_STOPPED)
6943 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02006944 /*
6945 * All tracepoints are from kernel-space.
6946 */
6947 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006948 return 0;
6949
6950 if (!perf_tp_filter_match(event, data))
6951 return 0;
6952
6953 return 1;
6954}
6955
6956void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006957 struct pt_regs *regs, struct hlist_head *head, int rctx,
6958 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006959{
6960 struct perf_sample_data data;
6961 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006962
6963 struct perf_raw_record raw = {
6964 .size = entry_size,
6965 .data = record,
6966 };
6967
Robert Richterfd0d0002012-04-02 20:19:08 +02006968 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006969 data.raw = &raw;
6970
Sasha Levinb67bfe02013-02-27 17:06:00 -08006971 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006972 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006973 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006974 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006975
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006976 /*
6977 * If we got specified a target task, also iterate its context and
6978 * deliver this event there too.
6979 */
6980 if (task && task != current) {
6981 struct perf_event_context *ctx;
6982 struct trace_entry *entry = record;
6983
6984 rcu_read_lock();
6985 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
6986 if (!ctx)
6987 goto unlock;
6988
6989 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
6990 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6991 continue;
6992 if (event->attr.config != entry->type)
6993 continue;
6994 if (perf_tp_event_match(event, &data, regs))
6995 perf_swevent_event(event, count, &data, regs);
6996 }
6997unlock:
6998 rcu_read_unlock();
6999 }
7000
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007001 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007002}
7003EXPORT_SYMBOL_GPL(perf_tp_event);
7004
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007005static void tp_perf_event_destroy(struct perf_event *event)
7006{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007007 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007008}
7009
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007010static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007011{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007012 int err;
7013
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007014 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7015 return -ENOENT;
7016
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007017 /*
7018 * no branch sampling for tracepoint events
7019 */
7020 if (has_branch_stack(event))
7021 return -EOPNOTSUPP;
7022
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007023 err = perf_trace_init(event);
7024 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007025 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007026
7027 event->destroy = tp_perf_event_destroy;
7028
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007029 return 0;
7030}
7031
7032static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007033 .task_ctx_nr = perf_sw_context,
7034
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007035 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007036 .add = perf_trace_add,
7037 .del = perf_trace_del,
7038 .start = perf_swevent_start,
7039 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007040 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007041};
7042
7043static inline void perf_tp_register(void)
7044{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007045 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007046}
Li Zefan6fb29152009-10-15 11:21:42 +08007047
7048static int perf_event_set_filter(struct perf_event *event, void __user *arg)
7049{
7050 char *filter_str;
7051 int ret;
7052
7053 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7054 return -EINVAL;
7055
7056 filter_str = strndup_user(arg, PAGE_SIZE);
7057 if (IS_ERR(filter_str))
7058 return PTR_ERR(filter_str);
7059
7060 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
7061
7062 kfree(filter_str);
7063 return ret;
7064}
7065
7066static void perf_event_free_filter(struct perf_event *event)
7067{
7068 ftrace_profile_free_filter(event);
7069}
7070
Alexei Starovoitov25415172015-03-25 12:49:20 -07007071static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7072{
7073 struct bpf_prog *prog;
7074
7075 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7076 return -EINVAL;
7077
7078 if (event->tp_event->prog)
7079 return -EEXIST;
7080
Wang Nan04a22fa2015-07-01 02:13:50 +00007081 if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE))
7082 /* bpf programs can only be attached to u/kprobes */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007083 return -EINVAL;
7084
7085 prog = bpf_prog_get(prog_fd);
7086 if (IS_ERR(prog))
7087 return PTR_ERR(prog);
7088
Linus Torvalds6c373ca2015-04-15 09:00:47 -07007089 if (prog->type != BPF_PROG_TYPE_KPROBE) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007090 /* valid fd, but invalid bpf program type */
7091 bpf_prog_put(prog);
7092 return -EINVAL;
7093 }
7094
7095 event->tp_event->prog = prog;
7096
7097 return 0;
7098}
7099
7100static void perf_event_free_bpf_prog(struct perf_event *event)
7101{
7102 struct bpf_prog *prog;
7103
7104 if (!event->tp_event)
7105 return;
7106
7107 prog = event->tp_event->prog;
7108 if (prog) {
7109 event->tp_event->prog = NULL;
7110 bpf_prog_put(prog);
7111 }
7112}
7113
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007114#else
Li Zefan6fb29152009-10-15 11:21:42 +08007115
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007116static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007117{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007118}
Li Zefan6fb29152009-10-15 11:21:42 +08007119
7120static int perf_event_set_filter(struct perf_event *event, void __user *arg)
7121{
7122 return -ENOENT;
7123}
7124
7125static void perf_event_free_filter(struct perf_event *event)
7126{
7127}
7128
Alexei Starovoitov25415172015-03-25 12:49:20 -07007129static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7130{
7131 return -ENOENT;
7132}
7133
7134static void perf_event_free_bpf_prog(struct perf_event *event)
7135{
7136}
Li Zefan07b139c2009-12-21 14:27:35 +08007137#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007138
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007139#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007140void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007141{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007142 struct perf_sample_data sample;
7143 struct pt_regs *regs = data;
7144
Robert Richterfd0d0002012-04-02 20:19:08 +02007145 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007146
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007147 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007148 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007149}
7150#endif
7151
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007152/*
7153 * hrtimer based swevent callback
7154 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007155
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007156static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007157{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007158 enum hrtimer_restart ret = HRTIMER_RESTART;
7159 struct perf_sample_data data;
7160 struct pt_regs *regs;
7161 struct perf_event *event;
7162 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007163
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007164 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007165
7166 if (event->state != PERF_EVENT_STATE_ACTIVE)
7167 return HRTIMER_NORESTART;
7168
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007169 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007170
Robert Richterfd0d0002012-04-02 20:19:08 +02007171 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007172 regs = get_irq_regs();
7173
7174 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08007175 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02007176 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007177 ret = HRTIMER_NORESTART;
7178 }
7179
7180 period = max_t(u64, 10000, event->hw.sample_period);
7181 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
7182
7183 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007184}
7185
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007186static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007187{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007188 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007189 s64 period;
7190
7191 if (!is_sampling_event(event))
7192 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007193
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007194 period = local64_read(&hwc->period_left);
7195 if (period) {
7196 if (period < 0)
7197 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007198
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007199 local64_set(&hwc->period_left, 0);
7200 } else {
7201 period = max_t(u64, 10000, hwc->sample_period);
7202 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00007203 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
7204 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007205}
7206
7207static void perf_swevent_cancel_hrtimer(struct perf_event *event)
7208{
7209 struct hw_perf_event *hwc = &event->hw;
7210
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007211 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007212 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007213 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007214
7215 hrtimer_cancel(&hwc->hrtimer);
7216 }
7217}
7218
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007219static void perf_swevent_init_hrtimer(struct perf_event *event)
7220{
7221 struct hw_perf_event *hwc = &event->hw;
7222
7223 if (!is_sampling_event(event))
7224 return;
7225
7226 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
7227 hwc->hrtimer.function = perf_swevent_hrtimer;
7228
7229 /*
7230 * Since hrtimers have a fixed rate, we can do a static freq->period
7231 * mapping and avoid the whole period adjust feedback stuff.
7232 */
7233 if (event->attr.freq) {
7234 long freq = event->attr.sample_freq;
7235
7236 event->attr.sample_period = NSEC_PER_SEC / freq;
7237 hwc->sample_period = event->attr.sample_period;
7238 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09007239 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007240 event->attr.freq = 0;
7241 }
7242}
7243
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007244/*
7245 * Software event: cpu wall time clock
7246 */
7247
7248static void cpu_clock_event_update(struct perf_event *event)
7249{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007250 s64 prev;
7251 u64 now;
7252
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007253 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007254 prev = local64_xchg(&event->hw.prev_count, now);
7255 local64_add(now - prev, &event->count);
7256}
7257
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007258static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007259{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007260 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007261 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007262}
7263
7264static void cpu_clock_event_stop(struct perf_event *event, int flags)
7265{
7266 perf_swevent_cancel_hrtimer(event);
7267 cpu_clock_event_update(event);
7268}
7269
7270static int cpu_clock_event_add(struct perf_event *event, int flags)
7271{
7272 if (flags & PERF_EF_START)
7273 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08007274 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007275
7276 return 0;
7277}
7278
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007279static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007280{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007281 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007282}
7283
7284static void cpu_clock_event_read(struct perf_event *event)
7285{
7286 cpu_clock_event_update(event);
7287}
7288
7289static int cpu_clock_event_init(struct perf_event *event)
7290{
7291 if (event->attr.type != PERF_TYPE_SOFTWARE)
7292 return -ENOENT;
7293
7294 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
7295 return -ENOENT;
7296
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007297 /*
7298 * no branch sampling for software events
7299 */
7300 if (has_branch_stack(event))
7301 return -EOPNOTSUPP;
7302
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007303 perf_swevent_init_hrtimer(event);
7304
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007305 return 0;
7306}
7307
7308static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007309 .task_ctx_nr = perf_sw_context,
7310
Peter Zijlstra34f43922015-02-20 14:05:38 +01007311 .capabilities = PERF_PMU_CAP_NO_NMI,
7312
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007313 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007314 .add = cpu_clock_event_add,
7315 .del = cpu_clock_event_del,
7316 .start = cpu_clock_event_start,
7317 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007318 .read = cpu_clock_event_read,
7319};
7320
7321/*
7322 * Software event: task time clock
7323 */
7324
7325static void task_clock_event_update(struct perf_event *event, u64 now)
7326{
7327 u64 prev;
7328 s64 delta;
7329
7330 prev = local64_xchg(&event->hw.prev_count, now);
7331 delta = now - prev;
7332 local64_add(delta, &event->count);
7333}
7334
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007335static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007336{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007337 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007338 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007339}
7340
7341static void task_clock_event_stop(struct perf_event *event, int flags)
7342{
7343 perf_swevent_cancel_hrtimer(event);
7344 task_clock_event_update(event, event->ctx->time);
7345}
7346
7347static int task_clock_event_add(struct perf_event *event, int flags)
7348{
7349 if (flags & PERF_EF_START)
7350 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08007351 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007352
7353 return 0;
7354}
7355
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007356static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007357{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007358 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007359}
7360
7361static void task_clock_event_read(struct perf_event *event)
7362{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01007363 u64 now = perf_clock();
7364 u64 delta = now - event->ctx->timestamp;
7365 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007366
7367 task_clock_event_update(event, time);
7368}
7369
7370static int task_clock_event_init(struct perf_event *event)
7371{
7372 if (event->attr.type != PERF_TYPE_SOFTWARE)
7373 return -ENOENT;
7374
7375 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
7376 return -ENOENT;
7377
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007378 /*
7379 * no branch sampling for software events
7380 */
7381 if (has_branch_stack(event))
7382 return -EOPNOTSUPP;
7383
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007384 perf_swevent_init_hrtimer(event);
7385
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007386 return 0;
7387}
7388
7389static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007390 .task_ctx_nr = perf_sw_context,
7391
Peter Zijlstra34f43922015-02-20 14:05:38 +01007392 .capabilities = PERF_PMU_CAP_NO_NMI,
7393
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007394 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007395 .add = task_clock_event_add,
7396 .del = task_clock_event_del,
7397 .start = task_clock_event_start,
7398 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007399 .read = task_clock_event_read,
7400};
7401
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007402static void perf_pmu_nop_void(struct pmu *pmu)
7403{
7404}
7405
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007406static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
7407{
7408}
7409
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007410static int perf_pmu_nop_int(struct pmu *pmu)
7411{
7412 return 0;
7413}
7414
Geliang Tang18ab2cd2015-09-27 23:25:50 +08007415static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007416
7417static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007418{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007419 __this_cpu_write(nop_txn_flags, flags);
7420
7421 if (flags & ~PERF_PMU_TXN_ADD)
7422 return;
7423
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007424 perf_pmu_disable(pmu);
7425}
7426
7427static int perf_pmu_commit_txn(struct pmu *pmu)
7428{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007429 unsigned int flags = __this_cpu_read(nop_txn_flags);
7430
7431 __this_cpu_write(nop_txn_flags, 0);
7432
7433 if (flags & ~PERF_PMU_TXN_ADD)
7434 return 0;
7435
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007436 perf_pmu_enable(pmu);
7437 return 0;
7438}
7439
7440static void perf_pmu_cancel_txn(struct pmu *pmu)
7441{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007442 unsigned int flags = __this_cpu_read(nop_txn_flags);
7443
7444 __this_cpu_write(nop_txn_flags, 0);
7445
7446 if (flags & ~PERF_PMU_TXN_ADD)
7447 return;
7448
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007449 perf_pmu_enable(pmu);
7450}
7451
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007452static int perf_event_idx_default(struct perf_event *event)
7453{
Peter Zijlstrac719f562014-10-21 11:10:21 +02007454 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007455}
7456
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007457/*
7458 * Ensures all contexts with the same task_ctx_nr have the same
7459 * pmu_cpu_context too.
7460 */
Mark Rutland9e317042014-02-10 17:44:18 +00007461static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007462{
7463 struct pmu *pmu;
7464
7465 if (ctxn < 0)
7466 return NULL;
7467
7468 list_for_each_entry(pmu, &pmus, entry) {
7469 if (pmu->task_ctx_nr == ctxn)
7470 return pmu->pmu_cpu_context;
7471 }
7472
7473 return NULL;
7474}
7475
Peter Zijlstra51676952010-12-07 14:18:20 +01007476static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007477{
Peter Zijlstra51676952010-12-07 14:18:20 +01007478 int cpu;
7479
7480 for_each_possible_cpu(cpu) {
7481 struct perf_cpu_context *cpuctx;
7482
7483 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
7484
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007485 if (cpuctx->unique_pmu == old_pmu)
7486 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01007487 }
7488}
7489
7490static void free_pmu_context(struct pmu *pmu)
7491{
7492 struct pmu *i;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007493
7494 mutex_lock(&pmus_lock);
7495 /*
7496 * Like a real lame refcount.
7497 */
Peter Zijlstra51676952010-12-07 14:18:20 +01007498 list_for_each_entry(i, &pmus, entry) {
7499 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
7500 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007501 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01007502 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007503 }
7504
Peter Zijlstra51676952010-12-07 14:18:20 +01007505 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007506out:
7507 mutex_unlock(&pmus_lock);
7508}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007509static struct idr pmu_idr;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007510
Peter Zijlstraabe43402010-11-17 23:17:37 +01007511static ssize_t
7512type_show(struct device *dev, struct device_attribute *attr, char *page)
7513{
7514 struct pmu *pmu = dev_get_drvdata(dev);
7515
7516 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
7517}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007518static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007519
Stephane Eranian62b85632013-04-03 14:21:34 +02007520static ssize_t
7521perf_event_mux_interval_ms_show(struct device *dev,
7522 struct device_attribute *attr,
7523 char *page)
7524{
7525 struct pmu *pmu = dev_get_drvdata(dev);
7526
7527 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
7528}
7529
Peter Zijlstra272325c2015-04-15 11:41:58 +02007530static DEFINE_MUTEX(mux_interval_mutex);
7531
Stephane Eranian62b85632013-04-03 14:21:34 +02007532static ssize_t
7533perf_event_mux_interval_ms_store(struct device *dev,
7534 struct device_attribute *attr,
7535 const char *buf, size_t count)
7536{
7537 struct pmu *pmu = dev_get_drvdata(dev);
7538 int timer, cpu, ret;
7539
7540 ret = kstrtoint(buf, 0, &timer);
7541 if (ret)
7542 return ret;
7543
7544 if (timer < 1)
7545 return -EINVAL;
7546
7547 /* same value, noting to do */
7548 if (timer == pmu->hrtimer_interval_ms)
7549 return count;
7550
Peter Zijlstra272325c2015-04-15 11:41:58 +02007551 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02007552 pmu->hrtimer_interval_ms = timer;
7553
7554 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02007555 get_online_cpus();
7556 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02007557 struct perf_cpu_context *cpuctx;
7558 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
7559 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
7560
Peter Zijlstra272325c2015-04-15 11:41:58 +02007561 cpu_function_call(cpu,
7562 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02007563 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02007564 put_online_cpus();
7565 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02007566
7567 return count;
7568}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007569static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02007570
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007571static struct attribute *pmu_dev_attrs[] = {
7572 &dev_attr_type.attr,
7573 &dev_attr_perf_event_mux_interval_ms.attr,
7574 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01007575};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007576ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007577
7578static int pmu_bus_running;
7579static struct bus_type pmu_bus = {
7580 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007581 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01007582};
7583
7584static void pmu_dev_release(struct device *dev)
7585{
7586 kfree(dev);
7587}
7588
7589static int pmu_dev_alloc(struct pmu *pmu)
7590{
7591 int ret = -ENOMEM;
7592
7593 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
7594 if (!pmu->dev)
7595 goto out;
7596
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01007597 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01007598 device_initialize(pmu->dev);
7599 ret = dev_set_name(pmu->dev, "%s", pmu->name);
7600 if (ret)
7601 goto free_dev;
7602
7603 dev_set_drvdata(pmu->dev, pmu);
7604 pmu->dev->bus = &pmu_bus;
7605 pmu->dev->release = pmu_dev_release;
7606 ret = device_add(pmu->dev);
7607 if (ret)
7608 goto free_dev;
7609
7610out:
7611 return ret;
7612
7613free_dev:
7614 put_device(pmu->dev);
7615 goto out;
7616}
7617
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007618static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007619static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007620
Mischa Jonker03d8e802013-06-04 11:45:48 +02007621int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007622{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007623 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007624
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007625 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007626 ret = -ENOMEM;
7627 pmu->pmu_disable_count = alloc_percpu(int);
7628 if (!pmu->pmu_disable_count)
7629 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007630
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007631 pmu->type = -1;
7632 if (!name)
7633 goto skip_type;
7634 pmu->name = name;
7635
7636 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08007637 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
7638 if (type < 0) {
7639 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007640 goto free_pdc;
7641 }
7642 }
7643 pmu->type = type;
7644
Peter Zijlstraabe43402010-11-17 23:17:37 +01007645 if (pmu_bus_running) {
7646 ret = pmu_dev_alloc(pmu);
7647 if (ret)
7648 goto free_idr;
7649 }
7650
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007651skip_type:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007652 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
7653 if (pmu->pmu_cpu_context)
7654 goto got_cpu_context;
7655
Wei Yongjunc4814202013-04-12 11:05:54 +08007656 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007657 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
7658 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01007659 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007660
7661 for_each_possible_cpu(cpu) {
7662 struct perf_cpu_context *cpuctx;
7663
7664 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02007665 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007666 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007667 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007668 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02007669
Peter Zijlstra272325c2015-04-15 11:41:58 +02007670 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02007671
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007672 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007673 }
7674
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007675got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007676 if (!pmu->start_txn) {
7677 if (pmu->pmu_enable) {
7678 /*
7679 * If we have pmu_enable/pmu_disable calls, install
7680 * transaction stubs that use that to try and batch
7681 * hardware accesses.
7682 */
7683 pmu->start_txn = perf_pmu_start_txn;
7684 pmu->commit_txn = perf_pmu_commit_txn;
7685 pmu->cancel_txn = perf_pmu_cancel_txn;
7686 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007687 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007688 pmu->commit_txn = perf_pmu_nop_int;
7689 pmu->cancel_txn = perf_pmu_nop_void;
7690 }
7691 }
7692
7693 if (!pmu->pmu_enable) {
7694 pmu->pmu_enable = perf_pmu_nop_void;
7695 pmu->pmu_disable = perf_pmu_nop_void;
7696 }
7697
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007698 if (!pmu->event_idx)
7699 pmu->event_idx = perf_event_idx_default;
7700
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007701 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007702 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007703 ret = 0;
7704unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007705 mutex_unlock(&pmus_lock);
7706
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007707 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007708
Peter Zijlstraabe43402010-11-17 23:17:37 +01007709free_dev:
7710 device_del(pmu->dev);
7711 put_device(pmu->dev);
7712
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007713free_idr:
7714 if (pmu->type >= PERF_TYPE_MAX)
7715 idr_remove(&pmu_idr, pmu->type);
7716
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007717free_pdc:
7718 free_percpu(pmu->pmu_disable_count);
7719 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007720}
Yan, Zhengc464c762014-03-18 16:56:41 +08007721EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007722
7723void perf_pmu_unregister(struct pmu *pmu)
7724{
7725 mutex_lock(&pmus_lock);
7726 list_del_rcu(&pmu->entry);
7727 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007728
7729 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02007730 * We dereference the pmu list under both SRCU and regular RCU, so
7731 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007732 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007733 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02007734 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007735
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007736 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007737 if (pmu->type >= PERF_TYPE_MAX)
7738 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007739 device_del(pmu->dev);
7740 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01007741 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007742}
Yan, Zhengc464c762014-03-18 16:56:41 +08007743EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007744
Mark Rutlandcc34b982015-01-07 14:56:51 +00007745static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
7746{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007747 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00007748 int ret;
7749
7750 if (!try_module_get(pmu->module))
7751 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007752
7753 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02007754 /*
7755 * This ctx->mutex can nest when we're called through
7756 * inheritance. See the perf_event_ctx_lock_nested() comment.
7757 */
7758 ctx = perf_event_ctx_lock_nested(event->group_leader,
7759 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007760 BUG_ON(!ctx);
7761 }
7762
Mark Rutlandcc34b982015-01-07 14:56:51 +00007763 event->pmu = pmu;
7764 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007765
7766 if (ctx)
7767 perf_event_ctx_unlock(event->group_leader, ctx);
7768
Mark Rutlandcc34b982015-01-07 14:56:51 +00007769 if (ret)
7770 module_put(pmu->module);
7771
7772 return ret;
7773}
7774
Geliang Tang18ab2cd2015-09-27 23:25:50 +08007775static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007776{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007777 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007778 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08007779 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007780
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007781 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007782
7783 rcu_read_lock();
7784 pmu = idr_find(&pmu_idr, event->attr.type);
7785 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08007786 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007787 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08007788 if (ret)
7789 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007790 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08007791 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007792
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007793 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007794 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007795 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007796 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007797
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007798 if (ret != -ENOENT) {
7799 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007800 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007801 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007802 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007803 pmu = ERR_PTR(-ENOENT);
7804unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007805 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007806
7807 return pmu;
7808}
7809
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007810static void account_event_cpu(struct perf_event *event, int cpu)
7811{
7812 if (event->parent)
7813 return;
7814
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007815 if (is_cgroup_event(event))
7816 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
7817}
7818
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007819static void account_event(struct perf_event *event)
7820{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007821 bool inc = false;
7822
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007823 if (event->parent)
7824 return;
7825
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007826 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007827 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007828 if (event->attr.mmap || event->attr.mmap_data)
7829 atomic_inc(&nr_mmap_events);
7830 if (event->attr.comm)
7831 atomic_inc(&nr_comm_events);
7832 if (event->attr.task)
7833 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02007834 if (event->attr.freq) {
7835 if (atomic_inc_return(&nr_freq_events) == 1)
7836 tick_nohz_full_kick_all();
7837 }
Adrian Hunter45ac1402015-07-21 12:44:02 +03007838 if (event->attr.context_switch) {
7839 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007840 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03007841 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007842 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007843 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007844 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007845 inc = true;
7846
Peter Zijlstra9107c892016-02-24 18:45:45 +01007847 if (inc) {
7848 if (atomic_inc_not_zero(&perf_sched_count))
7849 goto enabled;
7850
7851 mutex_lock(&perf_sched_mutex);
7852 if (!atomic_read(&perf_sched_count)) {
7853 static_branch_enable(&perf_sched_events);
7854 /*
7855 * Guarantee that all CPUs observe they key change and
7856 * call the perf scheduling hooks before proceeding to
7857 * install events that need them.
7858 */
7859 synchronize_sched();
7860 }
7861 /*
7862 * Now that we have waited for the sync_sched(), allow further
7863 * increments to by-pass the mutex.
7864 */
7865 atomic_inc(&perf_sched_count);
7866 mutex_unlock(&perf_sched_mutex);
7867 }
7868enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007869
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007870 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007871}
7872
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007873/*
7874 * Allocate and initialize a event structure
7875 */
7876static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007877perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007878 struct task_struct *task,
7879 struct perf_event *group_leader,
7880 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007881 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00007882 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007883{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007884 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007885 struct perf_event *event;
7886 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007887 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007888
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007889 if ((unsigned)cpu >= nr_cpu_ids) {
7890 if (!task || cpu != -1)
7891 return ERR_PTR(-EINVAL);
7892 }
7893
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007894 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007895 if (!event)
7896 return ERR_PTR(-ENOMEM);
7897
7898 /*
7899 * Single events are their own group leaders, with an
7900 * empty sibling list:
7901 */
7902 if (!group_leader)
7903 group_leader = event;
7904
7905 mutex_init(&event->child_mutex);
7906 INIT_LIST_HEAD(&event->child_list);
7907
7908 INIT_LIST_HEAD(&event->group_entry);
7909 INIT_LIST_HEAD(&event->event_entry);
7910 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007911 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01007912 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01007913 INIT_HLIST_NODE(&event->hlist_entry);
7914
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007915
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007916 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08007917 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007918
7919 mutex_init(&event->mmap_mutex);
7920
Al Viroa6fa9412012-08-20 14:59:25 +01007921 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007922 event->cpu = cpu;
7923 event->attr = *attr;
7924 event->group_leader = group_leader;
7925 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007926 event->oncpu = -1;
7927
7928 event->parent = parent_event;
7929
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08007930 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007931 event->id = atomic64_inc_return(&perf_event_id);
7932
7933 event->state = PERF_EVENT_STATE_INACTIVE;
7934
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007935 if (task) {
7936 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007937 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01007938 * XXX pmu::event_init needs to know what task to account to
7939 * and we cannot use the ctx information because we need the
7940 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007941 */
Peter Zijlstra50f16a82015-03-05 22:10:19 +01007942 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007943 }
7944
Peter Zijlstra34f43922015-02-20 14:05:38 +01007945 event->clock = &local_clock;
7946 if (parent_event)
7947 event->clock = parent_event->clock;
7948
Avi Kivity4dc0da82011-06-29 18:42:35 +03007949 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007950 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007951 context = parent_event->overflow_handler_context;
7952 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007953
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007954 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007955 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02007956
Jiri Olsa0231bb52013-02-01 11:23:45 +01007957 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007958
7959 pmu = NULL;
7960
7961 hwc = &event->hw;
7962 hwc->sample_period = attr->sample_period;
7963 if (attr->freq && attr->sample_freq)
7964 hwc->sample_period = 1;
7965 hwc->last_period = hwc->sample_period;
7966
Peter Zijlstrae7850592010-05-21 14:43:08 +02007967 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007968
7969 /*
7970 * we currently do not support PERF_FORMAT_GROUP on inherited events
7971 */
7972 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007973 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007974
Yan, Zhenga46a2302014-11-04 21:56:06 -05007975 if (!has_branch_stack(event))
7976 event->attr.branch_sample_type = 0;
7977
Matt Fleming79dff512015-01-23 18:45:42 +00007978 if (cgroup_fd != -1) {
7979 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
7980 if (err)
7981 goto err_ns;
7982 }
7983
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007984 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007985 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007986 goto err_ns;
7987 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007988 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007989 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007990 }
7991
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007992 err = exclusive_event_init(event);
7993 if (err)
7994 goto err_pmu;
7995
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007996 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007997 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
7998 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007999 if (err)
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008000 goto err_per_task;
Stephane Eraniand010b332012-02-09 23:21:00 +01008001 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008002 }
8003
8004 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008005
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008006err_per_task:
8007 exclusive_event_destroy(event);
8008
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008009err_pmu:
8010 if (event->destroy)
8011 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08008012 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008013err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00008014 if (is_cgroup_event(event))
8015 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008016 if (event->ns)
8017 put_pid_ns(event->ns);
8018 kfree(event);
8019
8020 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008021}
8022
8023static int perf_copy_attr(struct perf_event_attr __user *uattr,
8024 struct perf_event_attr *attr)
8025{
8026 u32 size;
8027 int ret;
8028
8029 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
8030 return -EFAULT;
8031
8032 /*
8033 * zero the full structure, so that a short copy will be nice.
8034 */
8035 memset(attr, 0, sizeof(*attr));
8036
8037 ret = get_user(size, &uattr->size);
8038 if (ret)
8039 return ret;
8040
8041 if (size > PAGE_SIZE) /* silly large */
8042 goto err_size;
8043
8044 if (!size) /* abi compat */
8045 size = PERF_ATTR_SIZE_VER0;
8046
8047 if (size < PERF_ATTR_SIZE_VER0)
8048 goto err_size;
8049
8050 /*
8051 * If we're handed a bigger struct than we know of,
8052 * ensure all the unknown bits are 0 - i.e. new
8053 * user-space does not rely on any kernel feature
8054 * extensions we dont know about yet.
8055 */
8056 if (size > sizeof(*attr)) {
8057 unsigned char __user *addr;
8058 unsigned char __user *end;
8059 unsigned char val;
8060
8061 addr = (void __user *)uattr + sizeof(*attr);
8062 end = (void __user *)uattr + size;
8063
8064 for (; addr < end; addr++) {
8065 ret = get_user(val, addr);
8066 if (ret)
8067 return ret;
8068 if (val)
8069 goto err_size;
8070 }
8071 size = sizeof(*attr);
8072 }
8073
8074 ret = copy_from_user(attr, uattr, size);
8075 if (ret)
8076 return -EFAULT;
8077
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05308078 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008079 return -EINVAL;
8080
8081 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
8082 return -EINVAL;
8083
8084 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
8085 return -EINVAL;
8086
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008087 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
8088 u64 mask = attr->branch_sample_type;
8089
8090 /* only using defined bits */
8091 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
8092 return -EINVAL;
8093
8094 /* at least one branch bit must be set */
8095 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
8096 return -EINVAL;
8097
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008098 /* propagate priv level, when not set for branch */
8099 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
8100
8101 /* exclude_kernel checked on syscall entry */
8102 if (!attr->exclude_kernel)
8103 mask |= PERF_SAMPLE_BRANCH_KERNEL;
8104
8105 if (!attr->exclude_user)
8106 mask |= PERF_SAMPLE_BRANCH_USER;
8107
8108 if (!attr->exclude_hv)
8109 mask |= PERF_SAMPLE_BRANCH_HV;
8110 /*
8111 * adjust user setting (for HW filter setup)
8112 */
8113 attr->branch_sample_type = mask;
8114 }
Stephane Eraniane7122092013-06-06 11:02:04 +02008115 /* privileged levels capture (kernel, hv): check permissions */
8116 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02008117 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8118 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008119 }
Jiri Olsa40189942012-08-07 15:20:37 +02008120
Jiri Olsac5ebced2012-08-07 15:20:40 +02008121 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02008122 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02008123 if (ret)
8124 return ret;
8125 }
8126
8127 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
8128 if (!arch_perf_have_user_stack_dump())
8129 return -ENOSYS;
8130
8131 /*
8132 * We have __u32 type for the size, but so far
8133 * we can only use __u16 as maximum due to the
8134 * __u16 sample size limit.
8135 */
8136 if (attr->sample_stack_user >= USHRT_MAX)
8137 ret = -EINVAL;
8138 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
8139 ret = -EINVAL;
8140 }
Jiri Olsa40189942012-08-07 15:20:37 +02008141
Stephane Eranian60e23642014-09-24 13:48:37 +02008142 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
8143 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008144out:
8145 return ret;
8146
8147err_size:
8148 put_user(sizeof(*attr), &uattr->size);
8149 ret = -E2BIG;
8150 goto out;
8151}
8152
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008153static int
8154perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008155{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01008156 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008157 int ret = -EINVAL;
8158
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008159 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008160 goto set;
8161
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008162 /* don't allow circular references */
8163 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008164 goto out;
8165
Peter Zijlstra0f139302010-05-20 14:35:15 +02008166 /*
8167 * Don't allow cross-cpu buffers
8168 */
8169 if (output_event->cpu != event->cpu)
8170 goto out;
8171
8172 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02008173 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02008174 */
8175 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
8176 goto out;
8177
Peter Zijlstra34f43922015-02-20 14:05:38 +01008178 /*
8179 * Mixing clocks in the same buffer is trouble you don't need.
8180 */
8181 if (output_event->clock != event->clock)
8182 goto out;
8183
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02008184 /*
8185 * If both events generate aux data, they must be on the same PMU
8186 */
8187 if (has_aux(event) && has_aux(output_event) &&
8188 event->pmu != output_event->pmu)
8189 goto out;
8190
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008191set:
8192 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008193 /* Can't redirect output if we've got an active mmap() */
8194 if (atomic_read(&event->mmap_count))
8195 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008196
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008197 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02008198 /* get the rb we want to redirect to */
8199 rb = ring_buffer_get(output_event);
8200 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008201 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008202 }
8203
Peter Zijlstrab69cf532014-03-14 10:50:33 +01008204 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02008205
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008206 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008207unlock:
8208 mutex_unlock(&event->mmap_mutex);
8209
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008210out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008211 return ret;
8212}
8213
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008214static void mutex_lock_double(struct mutex *a, struct mutex *b)
8215{
8216 if (b < a)
8217 swap(a, b);
8218
8219 mutex_lock(a);
8220 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
8221}
8222
Peter Zijlstra34f43922015-02-20 14:05:38 +01008223static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
8224{
8225 bool nmi_safe = false;
8226
8227 switch (clk_id) {
8228 case CLOCK_MONOTONIC:
8229 event->clock = &ktime_get_mono_fast_ns;
8230 nmi_safe = true;
8231 break;
8232
8233 case CLOCK_MONOTONIC_RAW:
8234 event->clock = &ktime_get_raw_fast_ns;
8235 nmi_safe = true;
8236 break;
8237
8238 case CLOCK_REALTIME:
8239 event->clock = &ktime_get_real_ns;
8240 break;
8241
8242 case CLOCK_BOOTTIME:
8243 event->clock = &ktime_get_boot_ns;
8244 break;
8245
8246 case CLOCK_TAI:
8247 event->clock = &ktime_get_tai_ns;
8248 break;
8249
8250 default:
8251 return -EINVAL;
8252 }
8253
8254 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
8255 return -EINVAL;
8256
8257 return 0;
8258}
8259
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008260/**
8261 * sys_perf_event_open - open a performance event, associate it to a task/cpu
8262 *
8263 * @attr_uptr: event_id type attributes for monitoring/sampling
8264 * @pid: target pid
8265 * @cpu: target cpu
8266 * @group_fd: group leader event fd
8267 */
8268SYSCALL_DEFINE5(perf_event_open,
8269 struct perf_event_attr __user *, attr_uptr,
8270 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
8271{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008272 struct perf_event *group_leader = NULL, *output_event = NULL;
8273 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008274 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008275 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008276 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04008277 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07008278 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008279 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04008280 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008281 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008282 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01008283 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00008284 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008285
8286 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02008287 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008288 return -EINVAL;
8289
8290 err = perf_copy_attr(attr_uptr, &attr);
8291 if (err)
8292 return err;
8293
8294 if (!attr.exclude_kernel) {
8295 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8296 return -EACCES;
8297 }
8298
8299 if (attr.freq) {
8300 if (attr.sample_freq > sysctl_perf_event_sample_rate)
8301 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02008302 } else {
8303 if (attr.sample_period & (1ULL << 63))
8304 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008305 }
8306
Stephane Eraniane5d13672011-02-14 11:20:01 +02008307 /*
8308 * In cgroup mode, the pid argument is used to pass the fd
8309 * opened to the cgroup directory in cgroupfs. The cpu argument
8310 * designates the cpu on which to monitor threads from that
8311 * cgroup.
8312 */
8313 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
8314 return -EINVAL;
8315
Yann Droneauda21b0b32014-01-05 21:36:33 +01008316 if (flags & PERF_FLAG_FD_CLOEXEC)
8317 f_flags |= O_CLOEXEC;
8318
8319 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04008320 if (event_fd < 0)
8321 return event_fd;
8322
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008323 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04008324 err = perf_fget_light(group_fd, &group);
8325 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008326 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04008327 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008328 if (flags & PERF_FLAG_FD_OUTPUT)
8329 output_event = group_leader;
8330 if (flags & PERF_FLAG_FD_NO_GROUP)
8331 group_leader = NULL;
8332 }
8333
Stephane Eraniane5d13672011-02-14 11:20:01 +02008334 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008335 task = find_lively_task_by_vpid(pid);
8336 if (IS_ERR(task)) {
8337 err = PTR_ERR(task);
8338 goto err_group_fd;
8339 }
8340 }
8341
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008342 if (task && group_leader &&
8343 group_leader->attr.inherit != attr.inherit) {
8344 err = -EINVAL;
8345 goto err_task;
8346 }
8347
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008348 get_online_cpus();
8349
Matt Fleming79dff512015-01-23 18:45:42 +00008350 if (flags & PERF_FLAG_PID_CGROUP)
8351 cgroup_fd = pid;
8352
Avi Kivity4dc0da82011-06-29 18:42:35 +03008353 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00008354 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008355 if (IS_ERR(event)) {
8356 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008357 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008358 }
8359
Vince Weaver53b25332014-05-16 17:12:12 -04008360 if (is_sampling_event(event)) {
8361 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
8362 err = -ENOTSUPP;
8363 goto err_alloc;
8364 }
8365 }
8366
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008367 account_event(event);
8368
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008369 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008370 * Special case software events and allow them to be part of
8371 * any hardware group.
8372 */
8373 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008374
Peter Zijlstra34f43922015-02-20 14:05:38 +01008375 if (attr.use_clockid) {
8376 err = perf_event_set_clock(event, attr.clockid);
8377 if (err)
8378 goto err_alloc;
8379 }
8380
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008381 if (group_leader &&
8382 (is_software_event(event) != is_software_event(group_leader))) {
8383 if (is_software_event(event)) {
8384 /*
8385 * If event and group_leader are not both a software
8386 * event, and event is, then group leader is not.
8387 *
8388 * Allow the addition of software events to !software
8389 * groups, this is safe because software events never
8390 * fail to schedule.
8391 */
8392 pmu = group_leader->pmu;
8393 } else if (is_software_event(group_leader) &&
8394 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
8395 /*
8396 * In case the group is a pure software group, and we
8397 * try to add a hardware event, move the whole group to
8398 * the hardware context.
8399 */
8400 move_group = 1;
8401 }
8402 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008403
8404 /*
8405 * Get the target context (task or percpu):
8406 */
Yan, Zheng4af57ef2014-11-04 21:56:01 -05008407 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008408 if (IS_ERR(ctx)) {
8409 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008410 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008411 }
8412
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008413 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
8414 err = -EBUSY;
8415 goto err_context;
8416 }
8417
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02008418 if (task) {
8419 put_task_struct(task);
8420 task = NULL;
8421 }
8422
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008423 /*
8424 * Look up the group leader (we will attach this event to it):
8425 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008426 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008427 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008428
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008429 /*
8430 * Do not allow a recursive hierarchy (this new sibling
8431 * becoming part of another group-sibling):
8432 */
8433 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008434 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01008435
8436 /* All events in a group should have the same clock */
8437 if (group_leader->clock != event->clock)
8438 goto err_context;
8439
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008440 /*
8441 * Do not allow to attach to a group in a different
8442 * task or CPU context:
8443 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008444 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01008445 /*
8446 * Make sure we're both on the same task, or both
8447 * per-cpu events.
8448 */
8449 if (group_leader->ctx->task != ctx->task)
8450 goto err_context;
8451
8452 /*
8453 * Make sure we're both events for the same CPU;
8454 * grouping events for different CPUs is broken; since
8455 * you can never concurrently schedule them anyhow.
8456 */
8457 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008458 goto err_context;
8459 } else {
8460 if (group_leader->ctx != ctx)
8461 goto err_context;
8462 }
8463
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008464 /*
8465 * Only a group leader can be exclusive or pinned
8466 */
8467 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008468 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008469 }
8470
8471 if (output_event) {
8472 err = perf_event_set_output(event, output_event);
8473 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008474 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008475 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008476
Yann Droneauda21b0b32014-01-05 21:36:33 +01008477 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
8478 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04008479 if (IS_ERR(event_file)) {
8480 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008481 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04008482 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008483
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008484 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008485 gctx = group_leader->ctx;
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008486 mutex_lock_double(&gctx->mutex, &ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008487 if (gctx->task == TASK_TOMBSTONE) {
8488 err = -ESRCH;
8489 goto err_locked;
8490 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008491 } else {
8492 mutex_lock(&ctx->mutex);
8493 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008494
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008495 if (ctx->task == TASK_TOMBSTONE) {
8496 err = -ESRCH;
8497 goto err_locked;
8498 }
8499
Peter Zijlstraa7239682015-09-09 19:06:33 +02008500 if (!perf_event_validate_size(event)) {
8501 err = -E2BIG;
8502 goto err_locked;
8503 }
8504
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008505 /*
8506 * Must be under the same ctx::mutex as perf_install_in_context(),
8507 * because we need to serialize with concurrent event creation.
8508 */
8509 if (!exclusive_event_installable(event, ctx)) {
8510 /* exclusive and group stuff are assumed mutually exclusive */
8511 WARN_ON_ONCE(move_group);
8512
8513 err = -EBUSY;
8514 goto err_locked;
8515 }
8516
8517 WARN_ON_ONCE(ctx->parent_ctx);
8518
8519 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008520 /*
8521 * See perf_event_ctx_lock() for comments on the details
8522 * of swizzling perf_event::ctx.
8523 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008524 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01008525
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008526 list_for_each_entry(sibling, &group_leader->sibling_list,
8527 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008528 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008529 put_ctx(gctx);
8530 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008531
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008532 /*
8533 * Wait for everybody to stop referencing the events through
8534 * the old lists, before installing it on new lists.
8535 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008536 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008537
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008538 /*
8539 * Install the group siblings before the group leader.
8540 *
8541 * Because a group leader will try and install the entire group
8542 * (through the sibling list, which is still in-tact), we can
8543 * end up with siblings installed in the wrong context.
8544 *
8545 * By installing siblings first we NO-OP because they're not
8546 * reachable through the group lists.
8547 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008548 list_for_each_entry(sibling, &group_leader->sibling_list,
8549 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008550 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01008551 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008552 get_ctx(ctx);
8553 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008554
8555 /*
8556 * Removing from the context ends up with disabled
8557 * event. What we want here is event in the initial
8558 * startup state, ready to be add into new context.
8559 */
8560 perf_event__state_init(group_leader);
8561 perf_install_in_context(ctx, group_leader, group_leader->cpu);
8562 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008563
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008564 /*
8565 * Now that all events are installed in @ctx, nothing
8566 * references @gctx anymore, so drop the last reference we have
8567 * on it.
8568 */
8569 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008570 }
8571
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02008572 /*
8573 * Precalculate sample_data sizes; do while holding ctx::mutex such
8574 * that we're serialized against further additions and before
8575 * perf_install_in_context() which is the point the event is active and
8576 * can use these values.
8577 */
8578 perf_event__header_size(event);
8579 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008580
Peter Zijlstra78cd2c72016-01-25 14:08:45 +01008581 event->owner = current;
8582
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08008583 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008584 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008585
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008586 if (move_group)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008587 mutex_unlock(&gctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008588 mutex_unlock(&ctx->mutex);
8589
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008590 put_online_cpus();
8591
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008592 mutex_lock(&current->perf_event_mutex);
8593 list_add_tail(&event->owner_entry, &current->perf_event_list);
8594 mutex_unlock(&current->perf_event_mutex);
8595
Peter Zijlstra8a495422010-05-27 15:47:49 +02008596 /*
8597 * Drop the reference on the group_event after placing the
8598 * new event on the sibling_list. This ensures destruction
8599 * of the group leader will find the pointer to itself in
8600 * perf_group_detach().
8601 */
Al Viro2903ff02012-08-28 12:52:22 -04008602 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04008603 fd_install(event_fd, event_file);
8604 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008605
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008606err_locked:
8607 if (move_group)
8608 mutex_unlock(&gctx->mutex);
8609 mutex_unlock(&ctx->mutex);
8610/* err_file: */
8611 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008612err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008613 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04008614 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008615err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +01008616 /*
8617 * If event_file is set, the fput() above will have called ->release()
8618 * and that will take care of freeing the event.
8619 */
8620 if (!event_file)
8621 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008622err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008623 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008624err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02008625 if (task)
8626 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008627err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04008628 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04008629err_fd:
8630 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008631 return err;
8632}
8633
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008634/**
8635 * perf_event_create_kernel_counter
8636 *
8637 * @attr: attributes of the counter to create
8638 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07008639 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008640 */
8641struct perf_event *
8642perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07008643 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008644 perf_overflow_handler_t overflow_handler,
8645 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008646{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008647 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008648 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008649 int err;
8650
8651 /*
8652 * Get the target context (task or percpu):
8653 */
8654
Avi Kivity4dc0da82011-06-29 18:42:35 +03008655 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00008656 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008657 if (IS_ERR(event)) {
8658 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008659 goto err;
8660 }
8661
Jiri Olsaf8697762014-08-01 14:33:01 +02008662 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008663 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +02008664
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008665 account_event(event);
8666
Yan, Zheng4af57ef2014-11-04 21:56:01 -05008667 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008668 if (IS_ERR(ctx)) {
8669 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008670 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008671 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008672
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008673 WARN_ON_ONCE(ctx->parent_ctx);
8674 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008675 if (ctx->task == TASK_TOMBSTONE) {
8676 err = -ESRCH;
8677 goto err_unlock;
8678 }
8679
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008680 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008681 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008682 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008683 }
8684
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008685 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008686 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008687 mutex_unlock(&ctx->mutex);
8688
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008689 return event;
8690
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008691err_unlock:
8692 mutex_unlock(&ctx->mutex);
8693 perf_unpin_context(ctx);
8694 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008695err_free:
8696 free_event(event);
8697err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008698 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008699}
8700EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
8701
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008702void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
8703{
8704 struct perf_event_context *src_ctx;
8705 struct perf_event_context *dst_ctx;
8706 struct perf_event *event, *tmp;
8707 LIST_HEAD(events);
8708
8709 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
8710 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
8711
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008712 /*
8713 * See perf_event_ctx_lock() for comments on the details
8714 * of swizzling perf_event::ctx.
8715 */
8716 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008717 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
8718 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008719 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02008720 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008721 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02008722 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008723 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008724
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008725 /*
8726 * Wait for the events to quiesce before re-instating them.
8727 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008728 synchronize_rcu();
8729
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008730 /*
8731 * Re-instate events in 2 passes.
8732 *
8733 * Skip over group leaders and only install siblings on this first
8734 * pass, siblings will not get enabled without a leader, however a
8735 * leader will enable its siblings, even if those are still on the old
8736 * context.
8737 */
8738 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
8739 if (event->group_leader == event)
8740 continue;
8741
8742 list_del(&event->migrate_entry);
8743 if (event->state >= PERF_EVENT_STATE_OFF)
8744 event->state = PERF_EVENT_STATE_INACTIVE;
8745 account_event_cpu(event, dst_cpu);
8746 perf_install_in_context(dst_ctx, event, dst_cpu);
8747 get_ctx(dst_ctx);
8748 }
8749
8750 /*
8751 * Once all the siblings are setup properly, install the group leaders
8752 * to make it go.
8753 */
Peter Zijlstra98861672013-10-03 16:02:23 +02008754 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
8755 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008756 if (event->state >= PERF_EVENT_STATE_OFF)
8757 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02008758 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008759 perf_install_in_context(dst_ctx, event, dst_cpu);
8760 get_ctx(dst_ctx);
8761 }
8762 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008763 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008764}
8765EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
8766
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008767static void sync_child_event(struct perf_event *child_event,
8768 struct task_struct *child)
8769{
8770 struct perf_event *parent_event = child_event->parent;
8771 u64 child_val;
8772
8773 if (child_event->attr.inherit_stat)
8774 perf_event_read_event(child_event, child);
8775
Peter Zijlstrab5e58792010-05-21 14:43:12 +02008776 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008777
8778 /*
8779 * Add back the child's count to the parent's count:
8780 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02008781 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008782 atomic64_add(child_event->total_time_enabled,
8783 &parent_event->child_total_time_enabled);
8784 atomic64_add(child_event->total_time_running,
8785 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008786}
8787
8788static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008789perf_event_exit_event(struct perf_event *child_event,
8790 struct perf_event_context *child_ctx,
8791 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008792{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008793 struct perf_event *parent_event = child_event->parent;
8794
Peter Zijlstra1903d502014-07-15 17:27:27 +02008795 /*
8796 * Do not destroy the 'original' grouping; because of the context
8797 * switch optimization the original events could've ended up in a
8798 * random child task.
8799 *
8800 * If we were to destroy the original group, all group related
8801 * operations would cease to function properly after this random
8802 * child dies.
8803 *
8804 * Do destroy all inherited groups, we don't care about those
8805 * and being thorough is better.
8806 */
Peter Zijlstra32132a32016-01-11 15:40:59 +01008807 raw_spin_lock_irq(&child_ctx->lock);
8808 WARN_ON_ONCE(child_ctx->is_active);
8809
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008810 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +01008811 perf_group_detach(child_event);
8812 list_del_event(child_event, child_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01008813 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +01008814 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008815
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008816 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008817 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008818 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008819 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -04008820 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008821 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008822 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008823 /*
8824 * Child events can be cleaned up.
8825 */
8826
8827 sync_child_event(child_event, child);
8828
8829 /*
8830 * Remove this event from the parent's list
8831 */
8832 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
8833 mutex_lock(&parent_event->child_mutex);
8834 list_del_init(&child_event->child_list);
8835 mutex_unlock(&parent_event->child_mutex);
8836
8837 /*
8838 * Kick perf_poll() for is_event_hup().
8839 */
8840 perf_event_wakeup(parent_event);
8841 free_event(child_event);
8842 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008843}
8844
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008845static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008846{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008847 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008848 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008849
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008850 WARN_ON_ONCE(child != current);
8851
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008852 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008853 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008854 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008855
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008856 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008857 * In order to reduce the amount of tricky in ctx tear-down, we hold
8858 * ctx::mutex over the entire thing. This serializes against almost
8859 * everything that wants to access the ctx.
8860 *
8861 * The exception is sys_perf_event_open() /
8862 * perf_event_create_kernel_count() which does find_get_context()
8863 * without ctx::mutex (it cannot because of the move_group double mutex
8864 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008865 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008866 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008867
8868 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008869 * In a single ctx::lock section, de-schedule the events and detach the
8870 * context from the task such that we cannot ever get it scheduled back
8871 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008872 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008873 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008874 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008875
8876 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008877 * Now that the context is inactive, destroy the task <-> ctx relation
8878 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008879 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008880 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
8881 put_ctx(child_ctx); /* cannot be last */
8882 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
8883 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008884
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008885 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008886 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008887
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008888 if (clone_ctx)
8889 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008890
8891 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008892 * Report the task dead after unscheduling the events so that we
8893 * won't get any samples after PERF_RECORD_EXIT. We can however still
8894 * get a few PERF_RECORD_READ events.
8895 */
8896 perf_event_task(child, child_ctx, 0);
8897
Peter Zijlstraebf905f2014-05-29 19:00:24 +02008898 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008899 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008900
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008901 mutex_unlock(&child_ctx->mutex);
8902
8903 put_ctx(child_ctx);
8904}
8905
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008906/*
8907 * When a child task exits, feed back event values to parent events.
8908 */
8909void perf_event_exit_task(struct task_struct *child)
8910{
Peter Zijlstra88821352010-11-09 19:01:43 +01008911 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008912 int ctxn;
8913
Peter Zijlstra88821352010-11-09 19:01:43 +01008914 mutex_lock(&child->perf_event_mutex);
8915 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
8916 owner_entry) {
8917 list_del_init(&event->owner_entry);
8918
8919 /*
8920 * Ensure the list deletion is visible before we clear
8921 * the owner, closes a race against perf_release() where
8922 * we need to serialize on the owner->perf_event_mutex.
8923 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01008924 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +01008925 }
8926 mutex_unlock(&child->perf_event_mutex);
8927
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008928 for_each_task_context_nr(ctxn)
8929 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +01008930
8931 /*
8932 * The perf_event_exit_task_context calls perf_event_task
8933 * with child's task_ctx, which generates EXIT events for
8934 * child contexts and sets child->perf_event_ctxp[] to NULL.
8935 * At this point we need to send EXIT events to cpu contexts.
8936 */
8937 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008938}
8939
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008940static void perf_free_event(struct perf_event *event,
8941 struct perf_event_context *ctx)
8942{
8943 struct perf_event *parent = event->parent;
8944
8945 if (WARN_ON_ONCE(!parent))
8946 return;
8947
8948 mutex_lock(&parent->child_mutex);
8949 list_del_init(&event->child_list);
8950 mutex_unlock(&parent->child_mutex);
8951
Al Viroa6fa9412012-08-20 14:59:25 +01008952 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008953
Peter Zijlstra652884f2015-01-23 11:20:10 +01008954 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02008955 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008956 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +01008957 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008958 free_event(event);
8959}
8960
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008961/*
Peter Zijlstra652884f2015-01-23 11:20:10 +01008962 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008963 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +01008964 *
8965 * Not all locks are strictly required, but take them anyway to be nice and
8966 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008967 */
8968void perf_event_free_task(struct task_struct *task)
8969{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008970 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008971 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008972 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008973
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008974 for_each_task_context_nr(ctxn) {
8975 ctx = task->perf_event_ctxp[ctxn];
8976 if (!ctx)
8977 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008978
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008979 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008980again:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008981 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
8982 group_entry)
8983 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008984
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008985 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
8986 group_entry)
8987 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008988
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008989 if (!list_empty(&ctx->pinned_groups) ||
8990 !list_empty(&ctx->flexible_groups))
8991 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008992
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008993 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008994
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008995 put_ctx(ctx);
8996 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008997}
8998
Peter Zijlstra4e231c72010-09-09 21:01:59 +02008999void perf_event_delayed_put(struct task_struct *task)
9000{
9001 int ctxn;
9002
9003 for_each_task_context_nr(ctxn)
9004 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
9005}
9006
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009007struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +00009008{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009009 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +00009010
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009011 file = fget_raw(fd);
9012 if (!file)
9013 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +00009014
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009015 if (file->f_op != &perf_fops) {
9016 fput(file);
9017 return ERR_PTR(-EBADF);
9018 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00009019
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009020 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +00009021}
9022
9023const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
9024{
9025 if (!event)
9026 return ERR_PTR(-EINVAL);
9027
9028 return &event->attr;
9029}
9030
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009031/*
9032 * inherit a event from parent task to child task:
9033 */
9034static struct perf_event *
9035inherit_event(struct perf_event *parent_event,
9036 struct task_struct *parent,
9037 struct perf_event_context *parent_ctx,
9038 struct task_struct *child,
9039 struct perf_event *group_leader,
9040 struct perf_event_context *child_ctx)
9041{
Jiri Olsa1929def2014-09-12 13:18:27 +02009042 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009043 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02009044 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009045
9046 /*
9047 * Instead of creating recursive hierarchies of events,
9048 * we link inherited events back to the original parent,
9049 * which has a filp for sure, which we use as the reference
9050 * count:
9051 */
9052 if (parent_event->parent)
9053 parent_event = parent_event->parent;
9054
9055 child_event = perf_event_alloc(&parent_event->attr,
9056 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009057 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009058 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +00009059 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009060 if (IS_ERR(child_event))
9061 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01009062
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02009063 /*
9064 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
9065 * must be under the same lock in order to serialize against
9066 * perf_event_release_kernel(), such that either we must observe
9067 * is_orphaned_event() or they will observe us on the child_list.
9068 */
9069 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02009070 if (is_orphaned_event(parent_event) ||
9071 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02009072 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +01009073 free_event(child_event);
9074 return NULL;
9075 }
9076
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009077 get_ctx(child_ctx);
9078
9079 /*
9080 * Make the child state follow the state of the parent event,
9081 * not its attr.disabled bit. We hold the parent's mutex,
9082 * so we won't race with perf_event_{en, dis}able_family.
9083 */
Jiri Olsa1929def2014-09-12 13:18:27 +02009084 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009085 child_event->state = PERF_EVENT_STATE_INACTIVE;
9086 else
9087 child_event->state = PERF_EVENT_STATE_OFF;
9088
9089 if (parent_event->attr.freq) {
9090 u64 sample_period = parent_event->hw.sample_period;
9091 struct hw_perf_event *hwc = &child_event->hw;
9092
9093 hwc->sample_period = sample_period;
9094 hwc->last_period = sample_period;
9095
9096 local64_set(&hwc->period_left, sample_period);
9097 }
9098
9099 child_event->ctx = child_ctx;
9100 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03009101 child_event->overflow_handler_context
9102 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009103
9104 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02009105 * Precalculate sample_data sizes
9106 */
9107 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02009108 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02009109
9110 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009111 * Link it up in the child's context:
9112 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02009113 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009114 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02009115 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009116
9117 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009118 * Link this into the parent event's child list
9119 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009120 list_add_tail(&child_event->child_list, &parent_event->child_list);
9121 mutex_unlock(&parent_event->child_mutex);
9122
9123 return child_event;
9124}
9125
9126static int inherit_group(struct perf_event *parent_event,
9127 struct task_struct *parent,
9128 struct perf_event_context *parent_ctx,
9129 struct task_struct *child,
9130 struct perf_event_context *child_ctx)
9131{
9132 struct perf_event *leader;
9133 struct perf_event *sub;
9134 struct perf_event *child_ctr;
9135
9136 leader = inherit_event(parent_event, parent, parent_ctx,
9137 child, NULL, child_ctx);
9138 if (IS_ERR(leader))
9139 return PTR_ERR(leader);
9140 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
9141 child_ctr = inherit_event(sub, parent, parent_ctx,
9142 child, leader, child_ctx);
9143 if (IS_ERR(child_ctr))
9144 return PTR_ERR(child_ctr);
9145 }
9146 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009147}
9148
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009149static int
9150inherit_task_group(struct perf_event *event, struct task_struct *parent,
9151 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009152 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009153 int *inherited_all)
9154{
9155 int ret;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009156 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009157
9158 if (!event->attr.inherit) {
9159 *inherited_all = 0;
9160 return 0;
9161 }
9162
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009163 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009164 if (!child_ctx) {
9165 /*
9166 * This is executed from the parent task context, so
9167 * inherit events that have been marked for cloning.
9168 * First allocate and initialize a context for the
9169 * child.
9170 */
9171
Jiri Olsa734df5a2013-07-09 17:44:10 +02009172 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009173 if (!child_ctx)
9174 return -ENOMEM;
9175
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009176 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009177 }
9178
9179 ret = inherit_group(event, parent, parent_ctx,
9180 child, child_ctx);
9181
9182 if (ret)
9183 *inherited_all = 0;
9184
9185 return ret;
9186}
9187
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009188/*
9189 * Initialize the perf_event context in task_struct
9190 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02009191static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009192{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009193 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009194 struct perf_event_context *cloned_ctx;
9195 struct perf_event *event;
9196 struct task_struct *parent = current;
9197 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009198 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009199 int ret = 0;
9200
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009201 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009202 return 0;
9203
9204 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009205 * If the parent's context is a clone, pin it so it won't get
9206 * swapped under us.
9207 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009208 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02009209 if (!parent_ctx)
9210 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009211
9212 /*
9213 * No need to check if parent_ctx != NULL here; since we saw
9214 * it non-NULL earlier, the only reason for it to become NULL
9215 * is if we exit, and since we're currently in the middle of
9216 * a fork we can't be exiting at the same time.
9217 */
9218
9219 /*
9220 * Lock the parent list. No need to lock the child - not PID
9221 * hashed yet and not running, so nobody can access it.
9222 */
9223 mutex_lock(&parent_ctx->mutex);
9224
9225 /*
9226 * We dont have to disable NMIs - we are only looking at
9227 * the list, not manipulating it:
9228 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009229 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009230 ret = inherit_task_group(event, parent, parent_ctx,
9231 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009232 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009233 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009234 }
9235
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009236 /*
9237 * We can't hold ctx->lock when iterating the ->flexible_group list due
9238 * to allocations, but we need to prevent rotation because
9239 * rotate_ctx() will change the list from interrupt context.
9240 */
9241 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
9242 parent_ctx->rotate_disable = 1;
9243 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
9244
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009245 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009246 ret = inherit_task_group(event, parent, parent_ctx,
9247 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009248 if (ret)
9249 break;
9250 }
9251
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009252 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
9253 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009254
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009255 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009256
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01009257 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009258 /*
9259 * Mark the child context as a clone of the parent
9260 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009261 *
9262 * Note that if the parent is a clone, the holding of
9263 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009264 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009265 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009266 if (cloned_ctx) {
9267 child_ctx->parent_ctx = cloned_ctx;
9268 child_ctx->parent_gen = parent_ctx->parent_gen;
9269 } else {
9270 child_ctx->parent_ctx = parent_ctx;
9271 child_ctx->parent_gen = parent_ctx->generation;
9272 }
9273 get_ctx(child_ctx->parent_ctx);
9274 }
9275
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009276 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009277 mutex_unlock(&parent_ctx->mutex);
9278
9279 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009280 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009281
9282 return ret;
9283}
9284
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009285/*
9286 * Initialize the perf_event context in task_struct
9287 */
9288int perf_event_init_task(struct task_struct *child)
9289{
9290 int ctxn, ret;
9291
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01009292 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
9293 mutex_init(&child->perf_event_mutex);
9294 INIT_LIST_HEAD(&child->perf_event_list);
9295
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009296 for_each_task_context_nr(ctxn) {
9297 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07009298 if (ret) {
9299 perf_event_free_task(child);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009300 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07009301 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009302 }
9303
9304 return 0;
9305}
9306
Paul Mackerras220b1402010-03-10 20:45:52 +11009307static void __init perf_event_init_all_cpus(void)
9308{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009309 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11009310 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11009311
9312 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009313 swhash = &per_cpu(swevent_htable, cpu);
9314 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00009315 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11009316 }
9317}
9318
Paul Gortmaker0db06282013-06-19 14:53:51 -04009319static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009320{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009321 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009322
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009323 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +00009324 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009325 struct swevent_hlist *hlist;
9326
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009327 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
9328 WARN_ON(!hlist);
9329 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009330 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009331 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009332}
9333
Dave Young2965faa2015-09-09 15:38:55 -07009334#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009335static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009336{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009337 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009338 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
9339 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009340
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009341 raw_spin_lock(&ctx->lock);
9342 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009343 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009344 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009345}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009346
9347static void perf_event_exit_cpu_context(int cpu)
9348{
9349 struct perf_event_context *ctx;
9350 struct pmu *pmu;
9351 int idx;
9352
9353 idx = srcu_read_lock(&pmus_srcu);
9354 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02009355 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009356
9357 mutex_lock(&ctx->mutex);
9358 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
9359 mutex_unlock(&ctx->mutex);
9360 }
9361 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009362}
9363
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009364static void perf_event_exit_cpu(int cpu)
9365{
Peter Zijlstrae3703f82014-02-24 12:06:12 +01009366 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009367}
9368#else
9369static inline void perf_event_exit_cpu(int cpu) { }
9370#endif
9371
Peter Zijlstrac2774432010-12-08 15:29:02 +01009372static int
9373perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
9374{
9375 int cpu;
9376
9377 for_each_online_cpu(cpu)
9378 perf_event_exit_cpu(cpu);
9379
9380 return NOTIFY_OK;
9381}
9382
9383/*
9384 * Run the perf reboot notifier at the very last possible moment so that
9385 * the generic watchdog code runs as long as possible.
9386 */
9387static struct notifier_block perf_reboot_notifier = {
9388 .notifier_call = perf_reboot,
9389 .priority = INT_MIN,
9390};
9391
Paul Gortmaker0db06282013-06-19 14:53:51 -04009392static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009393perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
9394{
9395 unsigned int cpu = (long)hcpu;
9396
Linus Torvalds4536e4d2011-11-03 07:44:04 -07009397 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009398
9399 case CPU_UP_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009400 perf_event_init_cpu(cpu);
9401 break;
9402
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009403 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009404 perf_event_exit_cpu(cpu);
9405 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009406 default:
9407 break;
9408 }
9409
9410 return NOTIFY_OK;
9411}
9412
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009413void __init perf_event_init(void)
9414{
Jason Wessel3c502e72010-11-04 17:33:01 -05009415 int ret;
9416
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009417 idr_init(&pmu_idr);
9418
Paul Mackerras220b1402010-03-10 20:45:52 +11009419 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009420 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009421 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
9422 perf_pmu_register(&perf_cpu_clock, NULL, -1);
9423 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009424 perf_tp_register();
9425 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01009426 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05009427
9428 ret = init_hw_breakpoint();
9429 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02009430
Jiri Olsab01c3a02012-03-23 15:41:20 +01009431 /*
9432 * Build time assertion that we keep the data_head at the intended
9433 * location. IOW, validation we got the __reserved[] size right.
9434 */
9435 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
9436 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009437}
Peter Zijlstraabe43402010-11-17 23:17:37 +01009438
Cody P Schaferfd979c02015-01-30 13:45:57 -08009439ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
9440 char *page)
9441{
9442 struct perf_pmu_events_attr *pmu_attr =
9443 container_of(attr, struct perf_pmu_events_attr, attr);
9444
9445 if (pmu_attr->event_str)
9446 return sprintf(page, "%s\n", pmu_attr->event_str);
9447
9448 return 0;
9449}
9450
Peter Zijlstraabe43402010-11-17 23:17:37 +01009451static int __init perf_event_sysfs_init(void)
9452{
9453 struct pmu *pmu;
9454 int ret;
9455
9456 mutex_lock(&pmus_lock);
9457
9458 ret = bus_register(&pmu_bus);
9459 if (ret)
9460 goto unlock;
9461
9462 list_for_each_entry(pmu, &pmus, entry) {
9463 if (!pmu->name || pmu->type < 0)
9464 continue;
9465
9466 ret = pmu_dev_alloc(pmu);
9467 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
9468 }
9469 pmu_bus_running = 1;
9470 ret = 0;
9471
9472unlock:
9473 mutex_unlock(&pmus_lock);
9474
9475 return ret;
9476}
9477device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009478
9479#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04009480static struct cgroup_subsys_state *
9481perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009482{
9483 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02009484
Li Zefan1b15d052011-03-03 14:26:06 +08009485 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009486 if (!jc)
9487 return ERR_PTR(-ENOMEM);
9488
Stephane Eraniane5d13672011-02-14 11:20:01 +02009489 jc->info = alloc_percpu(struct perf_cgroup_info);
9490 if (!jc->info) {
9491 kfree(jc);
9492 return ERR_PTR(-ENOMEM);
9493 }
9494
Stephane Eraniane5d13672011-02-14 11:20:01 +02009495 return &jc->css;
9496}
9497
Tejun Heoeb954192013-08-08 20:11:23 -04009498static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009499{
Tejun Heoeb954192013-08-08 20:11:23 -04009500 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
9501
Stephane Eraniane5d13672011-02-14 11:20:01 +02009502 free_percpu(jc->info);
9503 kfree(jc);
9504}
9505
9506static int __perf_cgroup_move(void *info)
9507{
9508 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +01009509 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +02009510 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +01009511 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +02009512 return 0;
9513}
9514
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009515static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009516{
Tejun Heobb9d97b2011-12-12 18:12:21 -08009517 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009518 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -08009519
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009520 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08009521 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009522}
9523
Tejun Heo073219e2014-02-08 10:36:58 -05009524struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08009525 .css_alloc = perf_cgroup_css_alloc,
9526 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -08009527 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02009528};
9529#endif /* CONFIG_CGROUP_PERF */