blob: 51386e84293e3bfb145c3fe5dd641aa4d6235613 [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) {
Peter Zijlstra0da4cf32016-02-24 18:45:51 +010067 /* -EAGAIN */
68 if (task_cpu(p) != smp_processor_id())
69 return;
70
71 /*
72 * Now that we're on right CPU with IRQs disabled, we can test
73 * if we hit the right task without races.
74 */
75
76 tfc->ret = -ESRCH; /* No such (running) process */
77 if (p != current)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010078 return;
79 }
80
81 tfc->ret = tfc->func(tfc->info);
82}
83
84/**
85 * task_function_call - call a function on the cpu on which a task runs
86 * @p: the task to evaluate
87 * @func: the function to be called
88 * @info: the function call argument
89 *
90 * Calls the function @func when the task is currently running. This might
91 * be on the current CPU, which just calls the function directly
92 *
93 * returns: @func return value, or
94 * -ESRCH - when the process isn't running
95 * -EAGAIN - when the process moved away
96 */
97static int
Peter Zijlstra272325c2015-04-15 11:41:58 +020098task_function_call(struct task_struct *p, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010099{
100 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200101 .p = p,
102 .func = func,
103 .info = info,
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100104 .ret = -EAGAIN,
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100105 };
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100106 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100107
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100108 do {
109 ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1);
110 if (!ret)
111 ret = data.ret;
112 } while (ret == -EAGAIN);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100113
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100114 return ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100115}
116
117/**
118 * cpu_function_call - call a function on the cpu
119 * @func: the function to be called
120 * @info: the function call argument
121 *
122 * Calls the function @func on the remote cpu.
123 *
124 * returns: @func return value or -ENXIO when the cpu is offline
125 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200126static int cpu_function_call(int cpu, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100127{
128 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200129 .p = NULL,
130 .func = func,
131 .info = info,
132 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100133 };
134
135 smp_call_function_single(cpu, remote_function, &data, 1);
136
137 return data.ret;
138}
139
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100140static inline struct perf_cpu_context *
141__get_cpu_context(struct perf_event_context *ctx)
142{
143 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
144}
145
146static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
147 struct perf_event_context *ctx)
148{
149 raw_spin_lock(&cpuctx->ctx.lock);
150 if (ctx)
151 raw_spin_lock(&ctx->lock);
152}
153
154static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
155 struct perf_event_context *ctx)
156{
157 if (ctx)
158 raw_spin_unlock(&ctx->lock);
159 raw_spin_unlock(&cpuctx->ctx.lock);
160}
161
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100162#define TASK_TOMBSTONE ((void *)-1L)
163
164static bool is_kernel_event(struct perf_event *event)
165{
Peter Zijlstraf47c02c2016-01-26 12:30:14 +0100166 return READ_ONCE(event->owner) == TASK_TOMBSTONE;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100167}
168
Peter Zijlstra39a43642016-01-11 12:46:35 +0100169/*
170 * On task ctx scheduling...
171 *
172 * When !ctx->nr_events a task context will not be scheduled. This means
173 * we can disable the scheduler hooks (for performance) without leaving
174 * pending task ctx state.
175 *
176 * This however results in two special cases:
177 *
178 * - removing the last event from a task ctx; this is relatively straight
179 * forward and is done in __perf_remove_from_context.
180 *
181 * - adding the first event to a task ctx; this is tricky because we cannot
182 * rely on ctx->is_active and therefore cannot use event_function_call().
183 * See perf_install_in_context().
184 *
Peter Zijlstra39a43642016-01-11 12:46:35 +0100185 * 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) {
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100215 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100216 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;
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100379 tmp = div_u64(tmp, 100);
380 if (!tmp)
381 tmp = 1;
382
383 WRITE_ONCE(perf_sample_allowed_ns, tmp);
Dave Hansen14c63f12013-06-21 08:51:36 -0700384}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100385
Stephane Eranian9e630202013-04-03 14:21:33 +0200386static int perf_rotate_context(struct perf_cpu_context *cpuctx);
387
Peter Zijlstra163ec432011-02-16 11:22:34 +0100388int perf_proc_update_handler(struct ctl_table *table, int write,
389 void __user *buffer, size_t *lenp,
390 loff_t *ppos)
391{
Knut Petersen723478c2013-09-25 14:29:37 +0200392 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100393
394 if (ret || !write)
395 return ret;
396
397 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700398 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
399 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100400
401 return 0;
402}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200403
Dave Hansen14c63f12013-06-21 08:51:36 -0700404int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
405
406int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
407 void __user *buffer, size_t *lenp,
408 loff_t *ppos)
409{
410 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
411
412 if (ret || !write)
413 return ret;
414
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100415 if (sysctl_perf_cpu_time_max_percent == 100) {
416 printk(KERN_WARNING
417 "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
418 WRITE_ONCE(perf_sample_allowed_ns, 0);
419 } else {
420 update_perf_cpu_limits();
421 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700422
423 return 0;
424}
425
426/*
427 * perf samples are done in some very critical code paths (NMIs).
428 * If they take too much CPU time, the system can lock up and not
429 * get any real work done. This will drop the sample rate when
430 * we detect that events are taking too long.
431 */
432#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200433static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700434
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100435static u64 __report_avg;
436static u64 __report_allowed;
437
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100438static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700439{
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100440 printk_ratelimited(KERN_WARNING
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100441 "perf: interrupt took too long (%lld > %lld), lowering "
442 "kernel.perf_event_max_sample_rate to %d\n",
443 __report_avg, __report_allowed,
444 sysctl_perf_event_sample_rate);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100445}
446
447static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
448
449void perf_sample_event_took(u64 sample_len_ns)
450{
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100451 u64 max_len = READ_ONCE(perf_sample_allowed_ns);
452 u64 running_len;
453 u64 avg_len;
454 u32 max;
Dave Hansen14c63f12013-06-21 08:51:36 -0700455
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100456 if (max_len == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700457 return;
458
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100459 /* Decay the counter by 1 average sample. */
460 running_len = __this_cpu_read(running_sample_length);
461 running_len -= running_len/NR_ACCUMULATED_SAMPLES;
462 running_len += sample_len_ns;
463 __this_cpu_write(running_sample_length, running_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700464
465 /*
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100466 * Note: this will be biased artifically low until we have
467 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
Dave Hansen14c63f12013-06-21 08:51:36 -0700468 * from having to maintain a count.
469 */
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100470 avg_len = running_len/NR_ACCUMULATED_SAMPLES;
471 if (avg_len <= max_len)
Dave Hansen14c63f12013-06-21 08:51:36 -0700472 return;
473
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100474 __report_avg = avg_len;
475 __report_allowed = max_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700476
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100477 /*
478 * Compute a throttle threshold 25% below the current duration.
479 */
480 avg_len += avg_len / 4;
481 max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
482 if (avg_len < max)
483 max /= (u32)avg_len;
484 else
485 max = 1;
486
487 WRITE_ONCE(perf_sample_allowed_ns, avg_len);
488 WRITE_ONCE(max_samples_per_tick, max);
489
490 sysctl_perf_event_sample_rate = max * HZ;
Dave Hansen14c63f12013-06-21 08:51:36 -0700491 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
492
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100493 if (!irq_work_queue(&perf_duration_work)) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100494 early_printk("perf: interrupt took too long (%lld > %lld), lowering "
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100495 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100496 __report_avg, __report_allowed,
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100497 sysctl_perf_event_sample_rate);
498 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700499}
500
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200501static atomic64_t perf_event_id;
502
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200503static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
504 enum event_type_t event_type);
505
506static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200507 enum event_type_t event_type,
508 struct task_struct *task);
509
510static void update_context_time(struct perf_event_context *ctx);
511static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200512
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200513void __weak perf_event_print_debug(void) { }
514
Matt Fleming84c79912010-10-03 21:41:13 +0100515extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200516{
Matt Fleming84c79912010-10-03 21:41:13 +0100517 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200518}
519
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200520static inline u64 perf_clock(void)
521{
522 return local_clock();
523}
524
Peter Zijlstra34f43922015-02-20 14:05:38 +0100525static inline u64 perf_event_clock(struct perf_event *event)
526{
527 return event->clock();
528}
529
Stephane Eraniane5d13672011-02-14 11:20:01 +0200530#ifdef CONFIG_CGROUP_PERF
531
Stephane Eraniane5d13672011-02-14 11:20:01 +0200532static inline bool
533perf_cgroup_match(struct perf_event *event)
534{
535 struct perf_event_context *ctx = event->ctx;
536 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
537
Tejun Heoef824fa2013-04-08 19:00:38 -0700538 /* @event doesn't care about cgroup */
539 if (!event->cgrp)
540 return true;
541
542 /* wants specific cgroup scope but @cpuctx isn't associated with any */
543 if (!cpuctx->cgrp)
544 return false;
545
546 /*
547 * Cgroup scoping is recursive. An event enabled for a cgroup is
548 * also enabled for all its descendant cgroups. If @cpuctx's
549 * cgroup is a descendant of @event's (the test covers identity
550 * case), it's a match.
551 */
552 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
553 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200554}
555
Stephane Eraniane5d13672011-02-14 11:20:01 +0200556static inline void perf_detach_cgroup(struct perf_event *event)
557{
Zefan Li4e2ba652014-09-19 16:53:14 +0800558 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200559 event->cgrp = NULL;
560}
561
562static inline int is_cgroup_event(struct perf_event *event)
563{
564 return event->cgrp != NULL;
565}
566
567static inline u64 perf_cgroup_event_time(struct perf_event *event)
568{
569 struct perf_cgroup_info *t;
570
571 t = per_cpu_ptr(event->cgrp->info, event->cpu);
572 return t->time;
573}
574
575static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
576{
577 struct perf_cgroup_info *info;
578 u64 now;
579
580 now = perf_clock();
581
582 info = this_cpu_ptr(cgrp->info);
583
584 info->time += now - info->timestamp;
585 info->timestamp = now;
586}
587
588static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
589{
590 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
591 if (cgrp_out)
592 __update_cgrp_time(cgrp_out);
593}
594
595static inline void update_cgrp_time_from_event(struct perf_event *event)
596{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200597 struct perf_cgroup *cgrp;
598
Stephane Eraniane5d13672011-02-14 11:20:01 +0200599 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200600 * ensure we access cgroup data only when needed and
601 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200602 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200603 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200604 return;
605
Stephane Eranian614e4c42015-11-12 11:00:04 +0100606 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200607 /*
608 * Do not update time when cgroup is not active
609 */
610 if (cgrp == event->cgrp)
611 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200612}
613
614static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200615perf_cgroup_set_timestamp(struct task_struct *task,
616 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200617{
618 struct perf_cgroup *cgrp;
619 struct perf_cgroup_info *info;
620
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200621 /*
622 * ctx->lock held by caller
623 * ensure we do not access cgroup data
624 * unless we have the cgroup pinned (css_get)
625 */
626 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200627 return;
628
Stephane Eranian614e4c42015-11-12 11:00:04 +0100629 cgrp = perf_cgroup_from_task(task, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200630 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200631 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200632}
633
634#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
635#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
636
637/*
638 * reschedule events based on the cgroup constraint of task.
639 *
640 * mode SWOUT : schedule out everything
641 * mode SWIN : schedule in based on cgroup for next
642 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800643static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200644{
645 struct perf_cpu_context *cpuctx;
646 struct pmu *pmu;
647 unsigned long flags;
648
649 /*
650 * disable interrupts to avoid geting nr_cgroup
651 * changes via __perf_event_disable(). Also
652 * avoids preemption.
653 */
654 local_irq_save(flags);
655
656 /*
657 * we reschedule only in the presence of cgroup
658 * constrained events.
659 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200660
661 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200662 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200663 if (cpuctx->unique_pmu != pmu)
664 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200665
Stephane Eraniane5d13672011-02-14 11:20:01 +0200666 /*
667 * perf_cgroup_events says at least one
668 * context on this CPU has cgroup events.
669 *
670 * ctx->nr_cgroups reports the number of cgroup
671 * events for a context.
672 */
673 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200674 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
675 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200676
677 if (mode & PERF_CGROUP_SWOUT) {
678 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
679 /*
680 * must not be done before ctxswout due
681 * to event_filter_match() in event_sched_out()
682 */
683 cpuctx->cgrp = NULL;
684 }
685
686 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200687 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200688 /*
689 * set cgrp before ctxsw in to allow
690 * event_filter_match() to not have to pass
691 * task around
Stephane Eranian614e4c42015-11-12 11:00:04 +0100692 * we pass the cpuctx->ctx to perf_cgroup_from_task()
693 * because cgorup events are only per-cpu
Stephane Eraniane5d13672011-02-14 11:20:01 +0200694 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100695 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200696 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
697 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200698 perf_pmu_enable(cpuctx->ctx.pmu);
699 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200700 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200701 }
702
Stephane Eraniane5d13672011-02-14 11:20:01 +0200703 local_irq_restore(flags);
704}
705
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200706static inline void perf_cgroup_sched_out(struct task_struct *task,
707 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200708{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200709 struct perf_cgroup *cgrp1;
710 struct perf_cgroup *cgrp2 = NULL;
711
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100712 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200713 /*
714 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100715 * we do not need to pass the ctx here because we know
716 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200717 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100718 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100719 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200720
721 /*
722 * only schedule out current cgroup events if we know
723 * that we are switching to a different cgroup. Otherwise,
724 * do no touch the cgroup events.
725 */
726 if (cgrp1 != cgrp2)
727 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100728
729 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200730}
731
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200732static inline void perf_cgroup_sched_in(struct task_struct *prev,
733 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200734{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200735 struct perf_cgroup *cgrp1;
736 struct perf_cgroup *cgrp2 = NULL;
737
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100738 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200739 /*
740 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100741 * we do not need to pass the ctx here because we know
742 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200743 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100744 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100745 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200746
747 /*
748 * only need to schedule in cgroup events if we are changing
749 * cgroup during ctxsw. Cgroup events were not scheduled
750 * out of ctxsw out if that was not the case.
751 */
752 if (cgrp1 != cgrp2)
753 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100754
755 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200756}
757
758static inline int perf_cgroup_connect(int fd, struct perf_event *event,
759 struct perf_event_attr *attr,
760 struct perf_event *group_leader)
761{
762 struct perf_cgroup *cgrp;
763 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400764 struct fd f = fdget(fd);
765 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200766
Al Viro2903ff02012-08-28 12:52:22 -0400767 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200768 return -EBADF;
769
Al Virob5830432014-10-31 01:22:04 -0400770 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400771 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800772 if (IS_ERR(css)) {
773 ret = PTR_ERR(css);
774 goto out;
775 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200776
777 cgrp = container_of(css, struct perf_cgroup, css);
778 event->cgrp = cgrp;
779
780 /*
781 * all events in a group must monitor
782 * the same cgroup because a task belongs
783 * to only one perf cgroup at a time
784 */
785 if (group_leader && group_leader->cgrp != cgrp) {
786 perf_detach_cgroup(event);
787 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200788 }
Li Zefan3db272c2011-03-03 14:25:37 +0800789out:
Al Viro2903ff02012-08-28 12:52:22 -0400790 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200791 return ret;
792}
793
794static inline void
795perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
796{
797 struct perf_cgroup_info *t;
798 t = per_cpu_ptr(event->cgrp->info, event->cpu);
799 event->shadow_ctx_time = now - t->timestamp;
800}
801
802static inline void
803perf_cgroup_defer_enabled(struct perf_event *event)
804{
805 /*
806 * when the current task's perf cgroup does not match
807 * the event's, we need to remember to call the
808 * perf_mark_enable() function the first time a task with
809 * a matching perf cgroup is scheduled in.
810 */
811 if (is_cgroup_event(event) && !perf_cgroup_match(event))
812 event->cgrp_defer_enabled = 1;
813}
814
815static inline void
816perf_cgroup_mark_enabled(struct perf_event *event,
817 struct perf_event_context *ctx)
818{
819 struct perf_event *sub;
820 u64 tstamp = perf_event_time(event);
821
822 if (!event->cgrp_defer_enabled)
823 return;
824
825 event->cgrp_defer_enabled = 0;
826
827 event->tstamp_enabled = tstamp - event->total_time_enabled;
828 list_for_each_entry(sub, &event->sibling_list, group_entry) {
829 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
830 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
831 sub->cgrp_defer_enabled = 0;
832 }
833 }
834}
835#else /* !CONFIG_CGROUP_PERF */
836
837static inline bool
838perf_cgroup_match(struct perf_event *event)
839{
840 return true;
841}
842
843static inline void perf_detach_cgroup(struct perf_event *event)
844{}
845
846static inline int is_cgroup_event(struct perf_event *event)
847{
848 return 0;
849}
850
851static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
852{
853 return 0;
854}
855
856static inline void update_cgrp_time_from_event(struct perf_event *event)
857{
858}
859
860static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
861{
862}
863
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200864static inline void perf_cgroup_sched_out(struct task_struct *task,
865 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200866{
867}
868
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200869static inline void perf_cgroup_sched_in(struct task_struct *prev,
870 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200871{
872}
873
874static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
875 struct perf_event_attr *attr,
876 struct perf_event *group_leader)
877{
878 return -EINVAL;
879}
880
881static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200882perf_cgroup_set_timestamp(struct task_struct *task,
883 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200884{
885}
886
887void
888perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
889{
890}
891
892static inline void
893perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
894{
895}
896
897static inline u64 perf_cgroup_event_time(struct perf_event *event)
898{
899 return 0;
900}
901
902static inline void
903perf_cgroup_defer_enabled(struct perf_event *event)
904{
905}
906
907static inline void
908perf_cgroup_mark_enabled(struct perf_event *event,
909 struct perf_event_context *ctx)
910{
911}
912#endif
913
Stephane Eranian9e630202013-04-03 14:21:33 +0200914/*
915 * set default to be dependent on timer tick just
916 * like original code
917 */
918#define PERF_CPU_HRTIMER (1000 / HZ)
919/*
920 * function must be called with interrupts disbled
921 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200922static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +0200923{
924 struct perf_cpu_context *cpuctx;
Stephane Eranian9e630202013-04-03 14:21:33 +0200925 int rotations = 0;
926
927 WARN_ON(!irqs_disabled());
928
929 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200930 rotations = perf_rotate_context(cpuctx);
931
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200932 raw_spin_lock(&cpuctx->hrtimer_lock);
933 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +0200934 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200935 else
936 cpuctx->hrtimer_active = 0;
937 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +0200938
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200939 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +0200940}
941
Peter Zijlstra272325c2015-04-15 11:41:58 +0200942static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +0200943{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200944 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200945 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +0200946 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +0200947
948 /* no multiplexing needed for SW PMU */
949 if (pmu->task_ctx_nr == perf_sw_context)
950 return;
951
Stephane Eranian62b85632013-04-03 14:21:34 +0200952 /*
953 * check default is sane, if not set then force to
954 * default interval (1/tick)
955 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200956 interval = pmu->hrtimer_interval_ms;
957 if (interval < 1)
958 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +0200959
Peter Zijlstra272325c2015-04-15 11:41:58 +0200960 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +0200961
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200962 raw_spin_lock_init(&cpuctx->hrtimer_lock);
963 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra272325c2015-04-15 11:41:58 +0200964 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +0200965}
966
Peter Zijlstra272325c2015-04-15 11:41:58 +0200967static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +0200968{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200969 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200970 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200971 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +0200972
973 /* not for SW PMU */
974 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +0200975 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +0200976
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200977 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
978 if (!cpuctx->hrtimer_active) {
979 cpuctx->hrtimer_active = 1;
980 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
981 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
982 }
983 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +0200984
Peter Zijlstra272325c2015-04-15 11:41:58 +0200985 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +0200986}
987
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200988void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200989{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200990 int *count = this_cpu_ptr(pmu->pmu_disable_count);
991 if (!(*count)++)
992 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200993}
994
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200995void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200996{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200997 int *count = this_cpu_ptr(pmu->pmu_disable_count);
998 if (!--(*count))
999 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001000}
1001
Mark Rutland2fde4f92015-01-07 15:01:54 +00001002static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001003
1004/*
Mark Rutland2fde4f92015-01-07 15:01:54 +00001005 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1006 * perf_event_task_tick() are fully serialized because they're strictly cpu
1007 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1008 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001009 */
Mark Rutland2fde4f92015-01-07 15:01:54 +00001010static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001011{
Mark Rutland2fde4f92015-01-07 15:01:54 +00001012 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001013
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001014 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001015
Mark Rutland2fde4f92015-01-07 15:01:54 +00001016 WARN_ON(!list_empty(&ctx->active_ctx_list));
1017
1018 list_add(&ctx->active_ctx_list, head);
1019}
1020
1021static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1022{
1023 WARN_ON(!irqs_disabled());
1024
1025 WARN_ON(list_empty(&ctx->active_ctx_list));
1026
1027 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001028}
1029
1030static void get_ctx(struct perf_event_context *ctx)
1031{
1032 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
1033}
1034
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001035static void free_ctx(struct rcu_head *head)
1036{
1037 struct perf_event_context *ctx;
1038
1039 ctx = container_of(head, struct perf_event_context, rcu_head);
1040 kfree(ctx->task_ctx_data);
1041 kfree(ctx);
1042}
1043
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001044static void put_ctx(struct perf_event_context *ctx)
1045{
1046 if (atomic_dec_and_test(&ctx->refcount)) {
1047 if (ctx->parent_ctx)
1048 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001049 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001050 put_task_struct(ctx->task);
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001051 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001052 }
1053}
1054
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001055/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001056 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1057 * perf_pmu_migrate_context() we need some magic.
1058 *
1059 * Those places that change perf_event::ctx will hold both
1060 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1061 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001062 * Lock ordering is by mutex address. There are two other sites where
1063 * perf_event_context::mutex nests and those are:
1064 *
1065 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001066 * perf_event_exit_event()
1067 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001068 *
1069 * - perf_event_init_context() [ parent, 0 ]
1070 * inherit_task_group()
1071 * inherit_group()
1072 * inherit_event()
1073 * perf_event_alloc()
1074 * perf_init_event()
1075 * perf_try_init_event() [ child , 1 ]
1076 *
1077 * While it appears there is an obvious deadlock here -- the parent and child
1078 * nesting levels are inverted between the two. This is in fact safe because
1079 * life-time rules separate them. That is an exiting task cannot fork, and a
1080 * spawning task cannot (yet) exit.
1081 *
1082 * But remember that that these are parent<->child context relations, and
1083 * migration does not affect children, therefore these two orderings should not
1084 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001085 *
1086 * The change in perf_event::ctx does not affect children (as claimed above)
1087 * because the sys_perf_event_open() case will install a new event and break
1088 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1089 * concerned with cpuctx and that doesn't have children.
1090 *
1091 * The places that change perf_event::ctx will issue:
1092 *
1093 * perf_remove_from_context();
1094 * synchronize_rcu();
1095 * perf_install_in_context();
1096 *
1097 * to affect the change. The remove_from_context() + synchronize_rcu() should
1098 * quiesce the event, after which we can install it in the new location. This
1099 * means that only external vectors (perf_fops, prctl) can perturb the event
1100 * while in transit. Therefore all such accessors should also acquire
1101 * perf_event_context::mutex to serialize against this.
1102 *
1103 * However; because event->ctx can change while we're waiting to acquire
1104 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1105 * function.
1106 *
1107 * Lock order:
1108 * task_struct::perf_event_mutex
1109 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001110 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001111 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001112 * perf_event::mmap_mutex
1113 * mmap_sem
1114 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001115static struct perf_event_context *
1116perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001117{
1118 struct perf_event_context *ctx;
1119
1120again:
1121 rcu_read_lock();
1122 ctx = ACCESS_ONCE(event->ctx);
1123 if (!atomic_inc_not_zero(&ctx->refcount)) {
1124 rcu_read_unlock();
1125 goto again;
1126 }
1127 rcu_read_unlock();
1128
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001129 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001130 if (event->ctx != ctx) {
1131 mutex_unlock(&ctx->mutex);
1132 put_ctx(ctx);
1133 goto again;
1134 }
1135
1136 return ctx;
1137}
1138
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001139static inline struct perf_event_context *
1140perf_event_ctx_lock(struct perf_event *event)
1141{
1142 return perf_event_ctx_lock_nested(event, 0);
1143}
1144
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001145static void perf_event_ctx_unlock(struct perf_event *event,
1146 struct perf_event_context *ctx)
1147{
1148 mutex_unlock(&ctx->mutex);
1149 put_ctx(ctx);
1150}
1151
1152/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001153 * This must be done under the ctx->lock, such as to serialize against
1154 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1155 * calling scheduler related locks and ctx->lock nests inside those.
1156 */
1157static __must_check struct perf_event_context *
1158unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001159{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001160 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1161
1162 lockdep_assert_held(&ctx->lock);
1163
1164 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001165 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001166 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001167
1168 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001169}
1170
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001171static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1172{
1173 /*
1174 * only top level events have the pid namespace they were created in
1175 */
1176 if (event->parent)
1177 event = event->parent;
1178
1179 return task_tgid_nr_ns(p, event->ns);
1180}
1181
1182static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1183{
1184 /*
1185 * only top level events have the pid namespace they were created in
1186 */
1187 if (event->parent)
1188 event = event->parent;
1189
1190 return task_pid_nr_ns(p, event->ns);
1191}
1192
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001193/*
1194 * If we inherit events we want to return the parent event id
1195 * to userspace.
1196 */
1197static u64 primary_event_id(struct perf_event *event)
1198{
1199 u64 id = event->id;
1200
1201 if (event->parent)
1202 id = event->parent->id;
1203
1204 return id;
1205}
1206
1207/*
1208 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001209 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001210 * This has to cope with with the fact that until it is locked,
1211 * the context could get moved to another task.
1212 */
1213static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001214perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001215{
1216 struct perf_event_context *ctx;
1217
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001218retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001219 /*
1220 * One of the few rules of preemptible RCU is that one cannot do
1221 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001222 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001223 * rcu_read_unlock_special().
1224 *
1225 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001226 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001227 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001228 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001229 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001230 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001231 if (ctx) {
1232 /*
1233 * If this context is a clone of another, it might
1234 * get swapped for another underneath us by
1235 * perf_event_task_sched_out, though the
1236 * rcu_read_lock() protects us from any context
1237 * getting freed. Lock the context and check if it
1238 * got swapped before we could get the lock, and retry
1239 * if so. If we locked the right context, then it
1240 * can't get swapped on us any more.
1241 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001242 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001243 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001244 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001245 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001246 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001247 goto retry;
1248 }
1249
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001250 if (ctx->task == TASK_TOMBSTONE ||
1251 !atomic_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001252 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001253 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001254 } else {
1255 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001256 }
1257 }
1258 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001259 if (!ctx)
1260 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001261 return ctx;
1262}
1263
1264/*
1265 * Get the context for a task and increment its pin_count so it
1266 * can't get swapped to another task. This also increments its
1267 * reference count so that the context can't get freed.
1268 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001269static struct perf_event_context *
1270perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001271{
1272 struct perf_event_context *ctx;
1273 unsigned long flags;
1274
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001275 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001276 if (ctx) {
1277 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001278 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001279 }
1280 return ctx;
1281}
1282
1283static void perf_unpin_context(struct perf_event_context *ctx)
1284{
1285 unsigned long flags;
1286
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001287 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001288 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001289 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001290}
1291
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001292/*
1293 * Update the record of the current time in a context.
1294 */
1295static void update_context_time(struct perf_event_context *ctx)
1296{
1297 u64 now = perf_clock();
1298
1299 ctx->time += now - ctx->timestamp;
1300 ctx->timestamp = now;
1301}
1302
Stephane Eranian41587552011-01-03 18:20:01 +02001303static u64 perf_event_time(struct perf_event *event)
1304{
1305 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001306
1307 if (is_cgroup_event(event))
1308 return perf_cgroup_event_time(event);
1309
Stephane Eranian41587552011-01-03 18:20:01 +02001310 return ctx ? ctx->time : 0;
1311}
1312
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001313/*
1314 * Update the total_time_enabled and total_time_running fields for a event.
1315 */
1316static void update_event_times(struct perf_event *event)
1317{
1318 struct perf_event_context *ctx = event->ctx;
1319 u64 run_end;
1320
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001321 lockdep_assert_held(&ctx->lock);
1322
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001323 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1324 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1325 return;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001326
Stephane Eraniane5d13672011-02-14 11:20:01 +02001327 /*
1328 * in cgroup mode, time_enabled represents
1329 * the time the event was enabled AND active
1330 * tasks were in the monitored cgroup. This is
1331 * independent of the activity of the context as
1332 * there may be a mix of cgroup and non-cgroup events.
1333 *
1334 * That is why we treat cgroup events differently
1335 * here.
1336 */
1337 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001338 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001339 else if (ctx->is_active)
1340 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001341 else
1342 run_end = event->tstamp_stopped;
1343
1344 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001345
1346 if (event->state == PERF_EVENT_STATE_INACTIVE)
1347 run_end = event->tstamp_stopped;
1348 else
Stephane Eranian41587552011-01-03 18:20:01 +02001349 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001350
1351 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001352
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001353}
1354
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001355/*
1356 * Update total_time_enabled and total_time_running for all events in a group.
1357 */
1358static void update_group_times(struct perf_event *leader)
1359{
1360 struct perf_event *event;
1361
1362 update_event_times(leader);
1363 list_for_each_entry(event, &leader->sibling_list, group_entry)
1364 update_event_times(event);
1365}
1366
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001367static struct list_head *
1368ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1369{
1370 if (event->attr.pinned)
1371 return &ctx->pinned_groups;
1372 else
1373 return &ctx->flexible_groups;
1374}
1375
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001376/*
1377 * Add a event from the lists for its context.
1378 * Must be called with ctx->mutex and ctx->lock held.
1379 */
1380static void
1381list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1382{
Peter Zijlstrac994d612016-01-08 09:20:23 +01001383 lockdep_assert_held(&ctx->lock);
1384
Peter Zijlstra8a495422010-05-27 15:47:49 +02001385 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1386 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001387
1388 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001389 * If we're a stand alone event or group leader, we go to the context
1390 * list, group events are kept attached to the group so that
1391 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001392 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001393 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001394 struct list_head *list;
1395
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001396 if (is_software_event(event))
1397 event->group_flags |= PERF_GROUP_SOFTWARE;
1398
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001399 list = ctx_group_list(event, ctx);
1400 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001401 }
1402
Peter Zijlstra08309372011-03-03 11:31:20 +01001403 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001404 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001405
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001406 list_add_rcu(&event->event_entry, &ctx->event_list);
1407 ctx->nr_events++;
1408 if (event->attr.inherit_stat)
1409 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001410
1411 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001412}
1413
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001414/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001415 * Initialize event state based on the perf_event_attr::disabled.
1416 */
1417static inline void perf_event__state_init(struct perf_event *event)
1418{
1419 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1420 PERF_EVENT_STATE_INACTIVE;
1421}
1422
Peter Zijlstraa7239682015-09-09 19:06:33 +02001423static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001424{
1425 int entry = sizeof(u64); /* value */
1426 int size = 0;
1427 int nr = 1;
1428
1429 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1430 size += sizeof(u64);
1431
1432 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1433 size += sizeof(u64);
1434
1435 if (event->attr.read_format & PERF_FORMAT_ID)
1436 entry += sizeof(u64);
1437
1438 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001439 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001440 size += sizeof(u64);
1441 }
1442
1443 size += entry * nr;
1444 event->read_size = size;
1445}
1446
Peter Zijlstraa7239682015-09-09 19:06:33 +02001447static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001448{
1449 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001450 u16 size = 0;
1451
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001452 if (sample_type & PERF_SAMPLE_IP)
1453 size += sizeof(data->ip);
1454
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001455 if (sample_type & PERF_SAMPLE_ADDR)
1456 size += sizeof(data->addr);
1457
1458 if (sample_type & PERF_SAMPLE_PERIOD)
1459 size += sizeof(data->period);
1460
Andi Kleenc3feedf2013-01-24 16:10:28 +01001461 if (sample_type & PERF_SAMPLE_WEIGHT)
1462 size += sizeof(data->weight);
1463
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001464 if (sample_type & PERF_SAMPLE_READ)
1465 size += event->read_size;
1466
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001467 if (sample_type & PERF_SAMPLE_DATA_SRC)
1468 size += sizeof(data->data_src.val);
1469
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001470 if (sample_type & PERF_SAMPLE_TRANSACTION)
1471 size += sizeof(data->txn);
1472
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001473 event->header_size = size;
1474}
1475
Peter Zijlstraa7239682015-09-09 19:06:33 +02001476/*
1477 * Called at perf_event creation and when events are attached/detached from a
1478 * group.
1479 */
1480static void perf_event__header_size(struct perf_event *event)
1481{
1482 __perf_event_read_size(event,
1483 event->group_leader->nr_siblings);
1484 __perf_event_header_size(event, event->attr.sample_type);
1485}
1486
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001487static void perf_event__id_header_size(struct perf_event *event)
1488{
1489 struct perf_sample_data *data;
1490 u64 sample_type = event->attr.sample_type;
1491 u16 size = 0;
1492
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001493 if (sample_type & PERF_SAMPLE_TID)
1494 size += sizeof(data->tid_entry);
1495
1496 if (sample_type & PERF_SAMPLE_TIME)
1497 size += sizeof(data->time);
1498
Adrian Hunterff3d5272013-08-27 11:23:07 +03001499 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1500 size += sizeof(data->id);
1501
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001502 if (sample_type & PERF_SAMPLE_ID)
1503 size += sizeof(data->id);
1504
1505 if (sample_type & PERF_SAMPLE_STREAM_ID)
1506 size += sizeof(data->stream_id);
1507
1508 if (sample_type & PERF_SAMPLE_CPU)
1509 size += sizeof(data->cpu_entry);
1510
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001511 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001512}
1513
Peter Zijlstraa7239682015-09-09 19:06:33 +02001514static bool perf_event_validate_size(struct perf_event *event)
1515{
1516 /*
1517 * The values computed here will be over-written when we actually
1518 * attach the event.
1519 */
1520 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1521 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1522 perf_event__id_header_size(event);
1523
1524 /*
1525 * Sum the lot; should not exceed the 64k limit we have on records.
1526 * Conservative limit to allow for callchains and other variable fields.
1527 */
1528 if (event->read_size + event->header_size +
1529 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1530 return false;
1531
1532 return true;
1533}
1534
Peter Zijlstra8a495422010-05-27 15:47:49 +02001535static void perf_group_attach(struct perf_event *event)
1536{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001537 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001538
Peter Zijlstra74c33372010-10-15 11:40:29 +02001539 /*
1540 * We can have double attach due to group movement in perf_event_open.
1541 */
1542 if (event->attach_state & PERF_ATTACH_GROUP)
1543 return;
1544
Peter Zijlstra8a495422010-05-27 15:47:49 +02001545 event->attach_state |= PERF_ATTACH_GROUP;
1546
1547 if (group_leader == event)
1548 return;
1549
Peter Zijlstra652884f2015-01-23 11:20:10 +01001550 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1551
Peter Zijlstra8a495422010-05-27 15:47:49 +02001552 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1553 !is_software_event(event))
1554 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1555
1556 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1557 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001558
1559 perf_event__header_size(group_leader);
1560
1561 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1562 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001563}
1564
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001565/*
1566 * Remove a event from the lists for its context.
1567 * Must be called with ctx->mutex and ctx->lock held.
1568 */
1569static void
1570list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1571{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001572 struct perf_cpu_context *cpuctx;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001573
1574 WARN_ON_ONCE(event->ctx != ctx);
1575 lockdep_assert_held(&ctx->lock);
1576
Peter Zijlstra8a495422010-05-27 15:47:49 +02001577 /*
1578 * We can have double detach due to exit/hot-unplug + close.
1579 */
1580 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001581 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001582
1583 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1584
Stephane Eranian68cacd22011-03-23 16:03:06 +01001585 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001586 ctx->nr_cgroups--;
Peter Zijlstra70a01652016-01-08 09:29:16 +01001587 /*
1588 * Because cgroup events are always per-cpu events, this will
1589 * always be called from the right CPU.
1590 */
Stephane Eranian68cacd22011-03-23 16:03:06 +01001591 cpuctx = __get_cpu_context(ctx);
1592 /*
Peter Zijlstra70a01652016-01-08 09:29:16 +01001593 * If there are no more cgroup events then clear cgrp to avoid
1594 * stale pointer in update_cgrp_time_from_cpuctx().
Stephane Eranian68cacd22011-03-23 16:03:06 +01001595 */
1596 if (!ctx->nr_cgroups)
1597 cpuctx->cgrp = NULL;
1598 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001599
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001600 ctx->nr_events--;
1601 if (event->attr.inherit_stat)
1602 ctx->nr_stat--;
1603
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001604 list_del_rcu(&event->event_entry);
1605
Peter Zijlstra8a495422010-05-27 15:47:49 +02001606 if (event->group_leader == event)
1607 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001608
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001609 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001610
1611 /*
1612 * If event was in error state, then keep it
1613 * that way, otherwise bogus counts will be
1614 * returned on read(). The only way to get out
1615 * of error state is by explicit re-enabling
1616 * of the event
1617 */
1618 if (event->state > PERF_EVENT_STATE_OFF)
1619 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001620
1621 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001622}
1623
Peter Zijlstra8a495422010-05-27 15:47:49 +02001624static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001625{
1626 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001627 struct list_head *list = NULL;
1628
1629 /*
1630 * We can have double detach due to exit/hot-unplug + close.
1631 */
1632 if (!(event->attach_state & PERF_ATTACH_GROUP))
1633 return;
1634
1635 event->attach_state &= ~PERF_ATTACH_GROUP;
1636
1637 /*
1638 * If this is a sibling, remove it from its group.
1639 */
1640 if (event->group_leader != event) {
1641 list_del_init(&event->group_entry);
1642 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001643 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001644 }
1645
1646 if (!list_empty(&event->group_entry))
1647 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001648
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001649 /*
1650 * If this was a group event with sibling events then
1651 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001652 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001653 */
1654 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001655 if (list)
1656 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001657 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001658
1659 /* Inherit group flags from the previous leader */
1660 sibling->group_flags = event->group_flags;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001661
1662 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001663 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001664
1665out:
1666 perf_event__header_size(event->group_leader);
1667
1668 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1669 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001670}
1671
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001672static bool is_orphaned_event(struct perf_event *event)
1673{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01001674 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001675}
1676
Mark Rutland66eb5792015-05-13 17:12:23 +01001677static inline int pmu_filter_match(struct perf_event *event)
1678{
1679 struct pmu *pmu = event->pmu;
1680 return pmu->filter_match ? pmu->filter_match(event) : 1;
1681}
1682
Stephane Eranianfa66f072010-08-26 16:40:01 +02001683static inline int
1684event_filter_match(struct perf_event *event)
1685{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001686 return (event->cpu == -1 || event->cpu == smp_processor_id())
Mark Rutland66eb5792015-05-13 17:12:23 +01001687 && perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001688}
1689
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001690static void
1691event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001692 struct perf_cpu_context *cpuctx,
1693 struct perf_event_context *ctx)
1694{
Stephane Eranian41587552011-01-03 18:20:01 +02001695 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001696 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001697
1698 WARN_ON_ONCE(event->ctx != ctx);
1699 lockdep_assert_held(&ctx->lock);
1700
Stephane Eranianfa66f072010-08-26 16:40:01 +02001701 /*
1702 * An event which could not be activated because of
1703 * filter mismatch still needs to have its timings
1704 * maintained, otherwise bogus information is return
1705 * via read() for time_enabled, time_running:
1706 */
1707 if (event->state == PERF_EVENT_STATE_INACTIVE
1708 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001709 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001710 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001711 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001712 }
1713
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001714 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001715 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001716
Alexander Shishkin44377272013-12-16 14:17:36 +02001717 perf_pmu_disable(event->pmu);
1718
Peter Zijlstra28a967c2016-02-24 18:45:46 +01001719 event->tstamp_stopped = tstamp;
1720 event->pmu->del(event, 0);
1721 event->oncpu = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001722 event->state = PERF_EVENT_STATE_INACTIVE;
1723 if (event->pending_disable) {
1724 event->pending_disable = 0;
1725 event->state = PERF_EVENT_STATE_OFF;
1726 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001727
1728 if (!is_software_event(event))
1729 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001730 if (!--ctx->nr_active)
1731 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001732 if (event->attr.freq && event->attr.sample_freq)
1733 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001734 if (event->attr.exclusive || !cpuctx->active_oncpu)
1735 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001736
1737 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001738}
1739
1740static void
1741group_sched_out(struct perf_event *group_event,
1742 struct perf_cpu_context *cpuctx,
1743 struct perf_event_context *ctx)
1744{
1745 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001746 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001747
1748 event_sched_out(group_event, cpuctx, ctx);
1749
1750 /*
1751 * Schedule out siblings (if any):
1752 */
1753 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1754 event_sched_out(event, cpuctx, ctx);
1755
Stephane Eranianfa66f072010-08-26 16:40:01 +02001756 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001757 cpuctx->exclusive = 0;
1758}
1759
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001760#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01001761
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001762/*
1763 * Cross CPU call to remove a performance event
1764 *
1765 * We disable the event on the hardware level first. After that we
1766 * remove it from the context list.
1767 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001768static void
1769__perf_remove_from_context(struct perf_event *event,
1770 struct perf_cpu_context *cpuctx,
1771 struct perf_event_context *ctx,
1772 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001773{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001774 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001775
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001776 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001777 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001778 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001779 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001780
Peter Zijlstra39a43642016-01-11 12:46:35 +01001781 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001782 ctx->is_active = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01001783 if (ctx->task) {
1784 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1785 cpuctx->task_ctx = NULL;
1786 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001787 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001788}
1789
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001790/*
1791 * Remove the event from a task's (or a CPU's) list of events.
1792 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001793 * If event->ctx is a cloned context, callers must make sure that
1794 * every task struct that event->ctx->task could possibly point to
1795 * remains valid. This is OK when called from perf_release since
1796 * that only calls us on the top-level context, which can't be a clone.
1797 * When called from perf_event_exit_task, it's OK because the
1798 * context has been detached from its task.
1799 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001800static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001801{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001802 lockdep_assert_held(&event->ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001803
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001804 event_function_call(event, __perf_remove_from_context, (void *)flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001805}
1806
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001807/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001808 * Cross CPU call to disable a performance event
1809 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001810static void __perf_event_disable(struct perf_event *event,
1811 struct perf_cpu_context *cpuctx,
1812 struct perf_event_context *ctx,
1813 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001814{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001815 if (event->state < PERF_EVENT_STATE_INACTIVE)
1816 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001817
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001818 update_context_time(ctx);
1819 update_cgrp_time_from_event(event);
1820 update_group_times(event);
1821 if (event == event->group_leader)
1822 group_sched_out(event, cpuctx, ctx);
1823 else
1824 event_sched_out(event, cpuctx, ctx);
1825 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra7b648012015-12-03 18:35:21 +01001826}
1827
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001828/*
1829 * Disable a event.
1830 *
1831 * If event->ctx is a cloned context, callers must make sure that
1832 * every task struct that event->ctx->task could possibly point to
1833 * remains valid. This condition is satisifed when called through
1834 * perf_event_for_each_child or perf_event_for_each because they
1835 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001836 * goes to exit will block in perf_event_exit_event().
1837 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001838 * When called from perf_pending_event it's OK because event->ctx
1839 * is the current context on this CPU and preemption is disabled,
1840 * hence we can't get into perf_event_task_sched_out for this context.
1841 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001842static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001843{
1844 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001845
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001846 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001847 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001848 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001849 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001850 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001851 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001852
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001853 event_function_call(event, __perf_event_disable, NULL);
1854}
1855
1856void perf_event_disable_local(struct perf_event *event)
1857{
1858 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001859}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001860
1861/*
1862 * Strictly speaking kernel users cannot create groups and therefore this
1863 * interface does not need the perf_event_ctx_lock() magic.
1864 */
1865void perf_event_disable(struct perf_event *event)
1866{
1867 struct perf_event_context *ctx;
1868
1869 ctx = perf_event_ctx_lock(event);
1870 _perf_event_disable(event);
1871 perf_event_ctx_unlock(event, ctx);
1872}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001873EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001874
Stephane Eraniane5d13672011-02-14 11:20:01 +02001875static void perf_set_shadow_time(struct perf_event *event,
1876 struct perf_event_context *ctx,
1877 u64 tstamp)
1878{
1879 /*
1880 * use the correct time source for the time snapshot
1881 *
1882 * We could get by without this by leveraging the
1883 * fact that to get to this function, the caller
1884 * has most likely already called update_context_time()
1885 * and update_cgrp_time_xx() and thus both timestamp
1886 * are identical (or very close). Given that tstamp is,
1887 * already adjusted for cgroup, we could say that:
1888 * tstamp - ctx->timestamp
1889 * is equivalent to
1890 * tstamp - cgrp->timestamp.
1891 *
1892 * Then, in perf_output_read(), the calculation would
1893 * work with no changes because:
1894 * - event is guaranteed scheduled in
1895 * - no scheduled out in between
1896 * - thus the timestamp would be the same
1897 *
1898 * But this is a bit hairy.
1899 *
1900 * So instead, we have an explicit cgroup call to remain
1901 * within the time time source all along. We believe it
1902 * is cleaner and simpler to understand.
1903 */
1904 if (is_cgroup_event(event))
1905 perf_cgroup_set_shadow_time(event, tstamp);
1906 else
1907 event->shadow_ctx_time = tstamp - ctx->timestamp;
1908}
1909
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001910#define MAX_INTERRUPTS (~0ULL)
1911
1912static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001913static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001914
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001915static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001916event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001917 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001918 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001919{
Stephane Eranian41587552011-01-03 18:20:01 +02001920 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001921 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001922
Peter Zijlstra63342412014-05-05 11:49:16 +02001923 lockdep_assert_held(&ctx->lock);
1924
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001925 if (event->state <= PERF_EVENT_STATE_OFF)
1926 return 0;
1927
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02001928 WRITE_ONCE(event->oncpu, smp_processor_id());
1929 /*
1930 * Order event::oncpu write to happen before the ACTIVE state
1931 * is visible.
1932 */
1933 smp_wmb();
1934 WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001935
1936 /*
1937 * Unthrottle events, since we scheduled we might have missed several
1938 * ticks already, also for a heavily scheduling task there is little
1939 * guarantee it'll get a tick in a timely manner.
1940 */
1941 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1942 perf_log_throttle(event, 1);
1943 event->hw.interrupts = 0;
1944 }
1945
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001946 /*
1947 * The new state must be visible before we turn it on in the hardware:
1948 */
1949 smp_wmb();
1950
Alexander Shishkin44377272013-12-16 14:17:36 +02001951 perf_pmu_disable(event->pmu);
1952
Shaohua Li72f669c2015-02-05 15:55:31 -08001953 perf_set_shadow_time(event, ctx, tstamp);
1954
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001955 perf_log_itrace_start(event);
1956
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001957 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001958 event->state = PERF_EVENT_STATE_INACTIVE;
1959 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001960 ret = -EAGAIN;
1961 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001962 }
1963
Peter Zijlstra00a29162015-07-27 10:35:07 +02001964 event->tstamp_running += tstamp - event->tstamp_stopped;
1965
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001966 if (!is_software_event(event))
1967 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001968 if (!ctx->nr_active++)
1969 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001970 if (event->attr.freq && event->attr.sample_freq)
1971 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001972
1973 if (event->attr.exclusive)
1974 cpuctx->exclusive = 1;
1975
Alexander Shishkin44377272013-12-16 14:17:36 +02001976out:
1977 perf_pmu_enable(event->pmu);
1978
1979 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001980}
1981
1982static int
1983group_sched_in(struct perf_event *group_event,
1984 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001985 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001986{
Lin Ming6bde9b62010-04-23 13:56:00 +08001987 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001988 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001989 u64 now = ctx->time;
1990 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001991
1992 if (group_event->state == PERF_EVENT_STATE_OFF)
1993 return 0;
1994
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001995 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08001996
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001997 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001998 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001999 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002000 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02002001 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002002
2003 /*
2004 * Schedule in siblings as one group (if any):
2005 */
2006 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002007 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002008 partial_group = event;
2009 goto group_error;
2010 }
2011 }
2012
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002013 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002014 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002015
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002016group_error:
2017 /*
2018 * Groups can be scheduled in as one unit only, so undo any
2019 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02002020 * The events up to the failed event are scheduled out normally,
2021 * tstamp_stopped will be updated.
2022 *
2023 * The failed events and the remaining siblings need to have
2024 * their timings updated as if they had gone thru event_sched_in()
2025 * and event_sched_out(). This is required to get consistent timings
2026 * across the group. This also takes care of the case where the group
2027 * could never be scheduled by ensuring tstamp_stopped is set to mark
2028 * the time the event was actually stopped, such that time delta
2029 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002030 */
2031 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2032 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02002033 simulate = true;
2034
2035 if (simulate) {
2036 event->tstamp_running += now - event->tstamp_stopped;
2037 event->tstamp_stopped = now;
2038 } else {
2039 event_sched_out(event, cpuctx, ctx);
2040 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002041 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002042 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002043
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002044 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002045
Peter Zijlstra272325c2015-04-15 11:41:58 +02002046 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002047
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002048 return -EAGAIN;
2049}
2050
2051/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002052 * Work out whether we can put this event group on the CPU now.
2053 */
2054static int group_can_go_on(struct perf_event *event,
2055 struct perf_cpu_context *cpuctx,
2056 int can_add_hw)
2057{
2058 /*
2059 * Groups consisting entirely of software events can always go on.
2060 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01002061 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002062 return 1;
2063 /*
2064 * If an exclusive group is already on, no other hardware
2065 * events can go on.
2066 */
2067 if (cpuctx->exclusive)
2068 return 0;
2069 /*
2070 * If this group is exclusive and there are already
2071 * events on the CPU, it can't go on.
2072 */
2073 if (event->attr.exclusive && cpuctx->active_oncpu)
2074 return 0;
2075 /*
2076 * Otherwise, try to add it if all previous groups were able
2077 * to go on.
2078 */
2079 return can_add_hw;
2080}
2081
2082static void add_event_to_ctx(struct perf_event *event,
2083 struct perf_event_context *ctx)
2084{
Stephane Eranian41587552011-01-03 18:20:01 +02002085 u64 tstamp = perf_event_time(event);
2086
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002087 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002088 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002089 event->tstamp_enabled = tstamp;
2090 event->tstamp_running = tstamp;
2091 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002092}
2093
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002094static void ctx_sched_out(struct perf_event_context *ctx,
2095 struct perf_cpu_context *cpuctx,
2096 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002097static void
2098ctx_sched_in(struct perf_event_context *ctx,
2099 struct perf_cpu_context *cpuctx,
2100 enum event_type_t event_type,
2101 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002102
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002103static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2104 struct perf_event_context *ctx)
2105{
2106 if (!cpuctx->task_ctx)
2107 return;
2108
2109 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2110 return;
2111
2112 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2113}
2114
Peter Zijlstradce58552011-04-09 21:17:46 +02002115static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2116 struct perf_event_context *ctx,
2117 struct task_struct *task)
2118{
2119 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2120 if (ctx)
2121 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2122 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2123 if (ctx)
2124 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2125}
2126
Peter Zijlstra3e349502016-01-08 10:01:18 +01002127static void ctx_resched(struct perf_cpu_context *cpuctx,
2128 struct perf_event_context *task_ctx)
Peter Zijlstra00179602015-11-30 16:26:35 +01002129{
Peter Zijlstra3e349502016-01-08 10:01:18 +01002130 perf_pmu_disable(cpuctx->ctx.pmu);
2131 if (task_ctx)
2132 task_ctx_sched_out(cpuctx, task_ctx);
2133 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2134 perf_event_sched_in(cpuctx, task_ctx, current);
2135 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002136}
2137
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002138/*
2139 * Cross CPU call to install and enable a performance event
2140 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002141 * Very similar to remote_function() + event_function() but cannot assume that
2142 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002143 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002144static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002145{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002146 struct perf_event *event = info;
2147 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002148 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002149 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002150 bool activate = true;
2151 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002152
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002153 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002154 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002155 raw_spin_lock(&ctx->lock);
2156 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002157
2158 /* If we're on the wrong CPU, try again */
2159 if (task_cpu(ctx->task) != smp_processor_id()) {
2160 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002161 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002162 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002163
Peter Zijlstra39a43642016-01-11 12:46:35 +01002164 /*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002165 * If we're on the right CPU, see if the task we target is
2166 * current, if not we don't have to activate the ctx, a future
2167 * context switch will do that for us.
Peter Zijlstra39a43642016-01-11 12:46:35 +01002168 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002169 if (ctx->task != current)
2170 activate = false;
2171 else
2172 WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx);
2173
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002174 } else if (task_ctx) {
2175 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002176 }
2177
Peter Zijlstraa0963092016-02-24 18:45:50 +01002178 if (activate) {
2179 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2180 add_event_to_ctx(event, ctx);
2181 ctx_resched(cpuctx, task_ctx);
2182 } else {
2183 add_event_to_ctx(event, ctx);
2184 }
2185
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002186unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002187 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002188
Peter Zijlstraa0963092016-02-24 18:45:50 +01002189 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002190}
2191
2192/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002193 * Attach a performance event to a context.
2194 *
2195 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002196 */
2197static void
2198perf_install_in_context(struct perf_event_context *ctx,
2199 struct perf_event *event,
2200 int cpu)
2201{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002202 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002203
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002204 lockdep_assert_held(&ctx->mutex);
2205
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002206 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002207 if (event->cpu != -1)
2208 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002209
Peter Zijlstraa0963092016-02-24 18:45:50 +01002210 if (!task) {
2211 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002212 return;
2213 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002214
Peter Zijlstraa0963092016-02-24 18:45:50 +01002215 /*
2216 * Should not happen, we validate the ctx is still alive before calling.
2217 */
2218 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2219 return;
2220
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002221 /*
2222 * Installing events is tricky because we cannot rely on ctx->is_active
2223 * to be set in case this is the nr_events 0 -> 1 transition.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002224 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002225again:
2226 /*
2227 * Cannot use task_function_call() because we need to run on the task's
2228 * CPU regardless of whether its current or not.
2229 */
2230 if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event))
2231 return;
2232
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002233 raw_spin_lock_irq(&ctx->lock);
2234 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002235 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2236 /*
2237 * Cannot happen because we already checked above (which also
2238 * cannot happen), and we hold ctx->mutex, which serializes us
2239 * against perf_event_exit_task_context().
2240 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002241 raw_spin_unlock_irq(&ctx->lock);
2242 return;
2243 }
Peter Zijlstra39a43642016-01-11 12:46:35 +01002244 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstraa0963092016-02-24 18:45:50 +01002245 /*
2246 * Since !ctx->is_active doesn't mean anything, we must IPI
2247 * unconditionally.
2248 */
2249 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002250}
2251
2252/*
2253 * Put a event into inactive state and update time fields.
2254 * Enabling the leader of a group effectively enables all
2255 * the group members that aren't explicitly disabled, so we
2256 * have to update their ->tstamp_enabled also.
2257 * Note: this works for group members as well as group leaders
2258 * since the non-leader members' sibling_lists will be empty.
2259 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002260static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002261{
2262 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002263 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002264
2265 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002266 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002267 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002268 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2269 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002270 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002271}
2272
2273/*
2274 * Cross CPU call to enable a performance event
2275 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002276static void __perf_event_enable(struct perf_event *event,
2277 struct perf_cpu_context *cpuctx,
2278 struct perf_event_context *ctx,
2279 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002280{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002281 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002282 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002283
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002284 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2285 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002286 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002287
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002288 if (ctx->is_active)
2289 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2290
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002291 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002292
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002293 if (!ctx->is_active)
2294 return;
2295
Stephane Eraniane5d13672011-02-14 11:20:01 +02002296 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002297 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02002298 perf_cgroup_defer_enabled(event);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002299 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002300 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002301 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002302
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002303 /*
2304 * If the event is in a group and isn't the group leader,
2305 * then don't put it on unless the group is on.
2306 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002307 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2308 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002309 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002310 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002311
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002312 task_ctx = cpuctx->task_ctx;
2313 if (ctx->task)
2314 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002315
Peter Zijlstraaee7dbc2016-01-08 10:45:11 +01002316 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002317}
2318
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002319/*
2320 * Enable a event.
2321 *
2322 * If event->ctx is a cloned context, callers must make sure that
2323 * every task struct that event->ctx->task could possibly point to
2324 * remains valid. This condition is satisfied when called through
2325 * perf_event_for_each_child or perf_event_for_each as described
2326 * for perf_event_disable.
2327 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002328static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002329{
2330 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002331
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002332 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002333 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2334 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002335 raw_spin_unlock_irq(&ctx->lock);
2336 return;
2337 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002338
2339 /*
2340 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002341 *
2342 * That way, if we see the event in error state below, we know that it
2343 * has gone back into error state, as distinct from the task having
2344 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002345 */
2346 if (event->state == PERF_EVENT_STATE_ERROR)
2347 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002348 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002349
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002350 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002351}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002352
2353/*
2354 * See perf_event_disable();
2355 */
2356void perf_event_enable(struct perf_event *event)
2357{
2358 struct perf_event_context *ctx;
2359
2360 ctx = perf_event_ctx_lock(event);
2361 _perf_event_enable(event);
2362 perf_event_ctx_unlock(event, ctx);
2363}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002364EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002365
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002366static int __perf_event_stop(void *info)
2367{
2368 struct perf_event *event = info;
2369
2370 /* for AUX events, our job is done if the event is already inactive */
2371 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2372 return 0;
2373
2374 /* matches smp_wmb() in event_sched_in() */
2375 smp_rmb();
2376
2377 /*
2378 * There is a window with interrupts enabled before we get here,
2379 * so we need to check again lest we try to stop another CPU's event.
2380 */
2381 if (READ_ONCE(event->oncpu) != smp_processor_id())
2382 return -EAGAIN;
2383
2384 event->pmu->stop(event, PERF_EF_UPDATE);
2385
2386 return 0;
2387}
2388
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002389static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002390{
2391 /*
2392 * not supported on inherited events
2393 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002394 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002395 return -EINVAL;
2396
2397 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002398 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002399
2400 return 0;
2401}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002402
2403/*
2404 * See perf_event_disable()
2405 */
2406int perf_event_refresh(struct perf_event *event, int refresh)
2407{
2408 struct perf_event_context *ctx;
2409 int ret;
2410
2411 ctx = perf_event_ctx_lock(event);
2412 ret = _perf_event_refresh(event, refresh);
2413 perf_event_ctx_unlock(event, ctx);
2414
2415 return ret;
2416}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002417EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002418
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002419static void ctx_sched_out(struct perf_event_context *ctx,
2420 struct perf_cpu_context *cpuctx,
2421 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002422{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002423 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002424 struct perf_event *event;
2425
2426 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002427
Peter Zijlstra39a43642016-01-11 12:46:35 +01002428 if (likely(!ctx->nr_events)) {
2429 /*
2430 * See __perf_remove_from_context().
2431 */
2432 WARN_ON_ONCE(ctx->is_active);
2433 if (ctx->task)
2434 WARN_ON_ONCE(cpuctx->task_ctx);
2435 return;
2436 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002437
Peter Zijlstradb24d332011-04-09 21:17:45 +02002438 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002439 if (!(ctx->is_active & EVENT_ALL))
2440 ctx->is_active = 0;
2441
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002442 if (ctx->task) {
2443 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2444 if (!ctx->is_active)
2445 cpuctx->task_ctx = NULL;
2446 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002447
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002448 /*
2449 * Always update time if it was set; not only when it changes.
2450 * Otherwise we can 'forget' to update time for any but the last
2451 * context we sched out. For example:
2452 *
2453 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2454 * ctx_sched_out(.event_type = EVENT_PINNED)
2455 *
2456 * would only update time for the pinned events.
2457 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002458 if (is_active & EVENT_TIME) {
2459 /* update (and stop) ctx time */
2460 update_context_time(ctx);
2461 update_cgrp_time_from_cpuctx(cpuctx);
2462 }
2463
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002464 is_active ^= ctx->is_active; /* changed bits */
2465
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002466 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002467 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002468
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002469 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002470 if (is_active & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002471 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2472 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002473 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002474
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002475 if (is_active & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002476 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002477 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002478 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002479 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002480}
2481
2482/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002483 * Test whether two contexts are equivalent, i.e. whether they have both been
2484 * cloned from the same version of the same context.
2485 *
2486 * Equivalence is measured using a generation number in the context that is
2487 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2488 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002489 */
2490static int context_equiv(struct perf_event_context *ctx1,
2491 struct perf_event_context *ctx2)
2492{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002493 lockdep_assert_held(&ctx1->lock);
2494 lockdep_assert_held(&ctx2->lock);
2495
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002496 /* Pinning disables the swap optimization */
2497 if (ctx1->pin_count || ctx2->pin_count)
2498 return 0;
2499
2500 /* If ctx1 is the parent of ctx2 */
2501 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2502 return 1;
2503
2504 /* If ctx2 is the parent of ctx1 */
2505 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2506 return 1;
2507
2508 /*
2509 * If ctx1 and ctx2 have the same parent; we flatten the parent
2510 * hierarchy, see perf_event_init_context().
2511 */
2512 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2513 ctx1->parent_gen == ctx2->parent_gen)
2514 return 1;
2515
2516 /* Unmatched */
2517 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002518}
2519
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002520static void __perf_event_sync_stat(struct perf_event *event,
2521 struct perf_event *next_event)
2522{
2523 u64 value;
2524
2525 if (!event->attr.inherit_stat)
2526 return;
2527
2528 /*
2529 * Update the event value, we cannot use perf_event_read()
2530 * because we're in the middle of a context switch and have IRQs
2531 * disabled, which upsets smp_call_function_single(), however
2532 * we know the event must be on the current CPU, therefore we
2533 * don't need to use it.
2534 */
2535 switch (event->state) {
2536 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002537 event->pmu->read(event);
2538 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002539
2540 case PERF_EVENT_STATE_INACTIVE:
2541 update_event_times(event);
2542 break;
2543
2544 default:
2545 break;
2546 }
2547
2548 /*
2549 * In order to keep per-task stats reliable we need to flip the event
2550 * values when we flip the contexts.
2551 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002552 value = local64_read(&next_event->count);
2553 value = local64_xchg(&event->count, value);
2554 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002555
2556 swap(event->total_time_enabled, next_event->total_time_enabled);
2557 swap(event->total_time_running, next_event->total_time_running);
2558
2559 /*
2560 * Since we swizzled the values, update the user visible data too.
2561 */
2562 perf_event_update_userpage(event);
2563 perf_event_update_userpage(next_event);
2564}
2565
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002566static void perf_event_sync_stat(struct perf_event_context *ctx,
2567 struct perf_event_context *next_ctx)
2568{
2569 struct perf_event *event, *next_event;
2570
2571 if (!ctx->nr_stat)
2572 return;
2573
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002574 update_context_time(ctx);
2575
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002576 event = list_first_entry(&ctx->event_list,
2577 struct perf_event, event_entry);
2578
2579 next_event = list_first_entry(&next_ctx->event_list,
2580 struct perf_event, event_entry);
2581
2582 while (&event->event_entry != &ctx->event_list &&
2583 &next_event->event_entry != &next_ctx->event_list) {
2584
2585 __perf_event_sync_stat(event, next_event);
2586
2587 event = list_next_entry(event, event_entry);
2588 next_event = list_next_entry(next_event, event_entry);
2589 }
2590}
2591
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002592static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2593 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002594{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002595 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002596 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002597 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002598 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002599 int do_switch = 1;
2600
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002601 if (likely(!ctx))
2602 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002603
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002604 cpuctx = __get_cpu_context(ctx);
2605 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002606 return;
2607
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002608 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002609 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002610 if (!next_ctx)
2611 goto unlock;
2612
2613 parent = rcu_dereference(ctx->parent_ctx);
2614 next_parent = rcu_dereference(next_ctx->parent_ctx);
2615
2616 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002617 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002618 goto unlock;
2619
2620 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002621 /*
2622 * Looks like the two contexts are clones, so we might be
2623 * able to optimize the context switch. We lock both
2624 * contexts and check that they are clones under the
2625 * lock (including re-checking that neither has been
2626 * uncloned in the meantime). It doesn't matter which
2627 * order we take the locks because no other cpu could
2628 * be trying to lock both of these tasks.
2629 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002630 raw_spin_lock(&ctx->lock);
2631 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002632 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002633 WRITE_ONCE(ctx->task, next);
2634 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05002635
2636 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2637
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002638 /*
2639 * RCU_INIT_POINTER here is safe because we've not
2640 * modified the ctx and the above modification of
2641 * ctx->task and ctx->task_ctx_data are immaterial
2642 * since those values are always verified under
2643 * ctx->lock which we're now holding.
2644 */
2645 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2646 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2647
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002648 do_switch = 0;
2649
2650 perf_event_sync_stat(ctx, next_ctx);
2651 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002652 raw_spin_unlock(&next_ctx->lock);
2653 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002654 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002655unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002656 rcu_read_unlock();
2657
2658 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002659 raw_spin_lock(&ctx->lock);
Peter Zijlstra8833d0e2016-01-08 10:02:37 +01002660 task_ctx_sched_out(cpuctx, ctx);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002661 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002662 }
2663}
2664
Yan, Zhengba532502014-11-04 21:55:58 -05002665void perf_sched_cb_dec(struct pmu *pmu)
2666{
2667 this_cpu_dec(perf_sched_cb_usages);
2668}
2669
2670void perf_sched_cb_inc(struct pmu *pmu)
2671{
2672 this_cpu_inc(perf_sched_cb_usages);
2673}
2674
2675/*
2676 * This function provides the context switch callback to the lower code
2677 * layer. It is invoked ONLY when the context switch callback is enabled.
2678 */
2679static void perf_pmu_sched_task(struct task_struct *prev,
2680 struct task_struct *next,
2681 bool sched_in)
2682{
2683 struct perf_cpu_context *cpuctx;
2684 struct pmu *pmu;
2685 unsigned long flags;
2686
2687 if (prev == next)
2688 return;
2689
2690 local_irq_save(flags);
2691
2692 rcu_read_lock();
2693
2694 list_for_each_entry_rcu(pmu, &pmus, entry) {
2695 if (pmu->sched_task) {
2696 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2697
2698 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2699
2700 perf_pmu_disable(pmu);
2701
2702 pmu->sched_task(cpuctx->task_ctx, sched_in);
2703
2704 perf_pmu_enable(pmu);
2705
2706 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2707 }
2708 }
2709
2710 rcu_read_unlock();
2711
2712 local_irq_restore(flags);
2713}
2714
Adrian Hunter45ac1402015-07-21 12:44:02 +03002715static void perf_event_switch(struct task_struct *task,
2716 struct task_struct *next_prev, bool sched_in);
2717
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002718#define for_each_task_context_nr(ctxn) \
2719 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2720
2721/*
2722 * Called from scheduler to remove the events of the current task,
2723 * with interrupts disabled.
2724 *
2725 * We stop each event and update the event value in event->count.
2726 *
2727 * This does not protect us against NMI, but disable()
2728 * sets the disabled bit in the control field of event _before_
2729 * accessing the event control register. If a NMI hits, then it will
2730 * not restart the event.
2731 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002732void __perf_event_task_sched_out(struct task_struct *task,
2733 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002734{
2735 int ctxn;
2736
Yan, Zhengba532502014-11-04 21:55:58 -05002737 if (__this_cpu_read(perf_sched_cb_usages))
2738 perf_pmu_sched_task(task, next, false);
2739
Adrian Hunter45ac1402015-07-21 12:44:02 +03002740 if (atomic_read(&nr_switch_events))
2741 perf_event_switch(task, next, false);
2742
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002743 for_each_task_context_nr(ctxn)
2744 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002745
2746 /*
2747 * if cgroup events exist on this CPU, then we need
2748 * to check if we have to switch out PMU state.
2749 * cgroup event are system-wide mode only
2750 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002751 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002752 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002753}
2754
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002755/*
2756 * Called with IRQs disabled
2757 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002758static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2759 enum event_type_t event_type)
2760{
2761 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002762}
2763
2764static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002765ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002766 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002767{
2768 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002769
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002770 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2771 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002772 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002773 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002774 continue;
2775
Stephane Eraniane5d13672011-02-14 11:20:01 +02002776 /* may need to reset tstamp_enabled */
2777 if (is_cgroup_event(event))
2778 perf_cgroup_mark_enabled(event, ctx);
2779
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002780 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002781 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002782
2783 /*
2784 * If this pinned group hasn't been scheduled,
2785 * put it in error state.
2786 */
2787 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2788 update_group_times(event);
2789 event->state = PERF_EVENT_STATE_ERROR;
2790 }
2791 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002792}
2793
2794static void
2795ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002796 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002797{
2798 struct perf_event *event;
2799 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002800
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002801 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2802 /* Ignore events in OFF or ERROR state */
2803 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002804 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002805 /*
2806 * Listen to the 'cpu' scheduling filter constraint
2807 * of events:
2808 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002809 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002810 continue;
2811
Stephane Eraniane5d13672011-02-14 11:20:01 +02002812 /* may need to reset tstamp_enabled */
2813 if (is_cgroup_event(event))
2814 perf_cgroup_mark_enabled(event, ctx);
2815
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002816 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002817 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002818 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002819 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002820 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002821}
2822
2823static void
2824ctx_sched_in(struct perf_event_context *ctx,
2825 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002826 enum event_type_t event_type,
2827 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002828{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002829 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002830 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002831
Peter Zijlstrac994d612016-01-08 09:20:23 +01002832 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002833
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002834 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002835 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002836
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002837 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002838 if (ctx->task) {
2839 if (!is_active)
2840 cpuctx->task_ctx = ctx;
2841 else
2842 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2843 }
2844
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002845 is_active ^= ctx->is_active; /* changed bits */
2846
2847 if (is_active & EVENT_TIME) {
2848 /* start ctx time */
2849 now = perf_clock();
2850 ctx->timestamp = now;
2851 perf_cgroup_set_timestamp(task, ctx);
2852 }
2853
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002854 /*
2855 * First go through the list and put on any pinned groups
2856 * in order to give them the best chance of going on.
2857 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002858 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002859 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002860
2861 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002862 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002863 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002864}
2865
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002866static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002867 enum event_type_t event_type,
2868 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002869{
2870 struct perf_event_context *ctx = &cpuctx->ctx;
2871
Stephane Eraniane5d13672011-02-14 11:20:01 +02002872 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002873}
2874
Stephane Eraniane5d13672011-02-14 11:20:01 +02002875static void perf_event_context_sched_in(struct perf_event_context *ctx,
2876 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002877{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002878 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002879
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002880 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002881 if (cpuctx->task_ctx == ctx)
2882 return;
2883
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002884 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002885 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002886 /*
2887 * We want to keep the following priority order:
2888 * cpu pinned (that don't need to move), task pinned,
2889 * cpu flexible, task flexible.
2890 */
2891 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002892 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002893 perf_pmu_enable(ctx->pmu);
2894 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002895}
2896
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002897/*
2898 * Called from scheduler to add the events of the current task
2899 * with interrupts disabled.
2900 *
2901 * We restore the event value and then enable it.
2902 *
2903 * This does not protect us against NMI, but enable()
2904 * sets the enabled bit in the control field of event _before_
2905 * accessing the event control register. If a NMI hits, then it will
2906 * keep the event running.
2907 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002908void __perf_event_task_sched_in(struct task_struct *prev,
2909 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002910{
2911 struct perf_event_context *ctx;
2912 int ctxn;
2913
Peter Zijlstra7e41d172016-01-08 09:21:40 +01002914 /*
2915 * If cgroup events exist on this CPU, then we need to check if we have
2916 * to switch in PMU state; cgroup event are system-wide mode only.
2917 *
2918 * Since cgroup events are CPU events, we must schedule these in before
2919 * we schedule in the task events.
2920 */
2921 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
2922 perf_cgroup_sched_in(prev, task);
2923
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002924 for_each_task_context_nr(ctxn) {
2925 ctx = task->perf_event_ctxp[ctxn];
2926 if (likely(!ctx))
2927 continue;
2928
Stephane Eraniane5d13672011-02-14 11:20:01 +02002929 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002930 }
Stephane Eraniand010b332012-02-09 23:21:00 +01002931
Adrian Hunter45ac1402015-07-21 12:44:02 +03002932 if (atomic_read(&nr_switch_events))
2933 perf_event_switch(task, prev, true);
2934
Yan, Zhengba532502014-11-04 21:55:58 -05002935 if (__this_cpu_read(perf_sched_cb_usages))
2936 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002937}
2938
Peter Zijlstraabd50712010-01-26 18:50:16 +01002939static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2940{
2941 u64 frequency = event->attr.sample_freq;
2942 u64 sec = NSEC_PER_SEC;
2943 u64 divisor, dividend;
2944
2945 int count_fls, nsec_fls, frequency_fls, sec_fls;
2946
2947 count_fls = fls64(count);
2948 nsec_fls = fls64(nsec);
2949 frequency_fls = fls64(frequency);
2950 sec_fls = 30;
2951
2952 /*
2953 * We got @count in @nsec, with a target of sample_freq HZ
2954 * the target period becomes:
2955 *
2956 * @count * 10^9
2957 * period = -------------------
2958 * @nsec * sample_freq
2959 *
2960 */
2961
2962 /*
2963 * Reduce accuracy by one bit such that @a and @b converge
2964 * to a similar magnitude.
2965 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002966#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002967do { \
2968 if (a##_fls > b##_fls) { \
2969 a >>= 1; \
2970 a##_fls--; \
2971 } else { \
2972 b >>= 1; \
2973 b##_fls--; \
2974 } \
2975} while (0)
2976
2977 /*
2978 * Reduce accuracy until either term fits in a u64, then proceed with
2979 * the other, so that finally we can do a u64/u64 division.
2980 */
2981 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2982 REDUCE_FLS(nsec, frequency);
2983 REDUCE_FLS(sec, count);
2984 }
2985
2986 if (count_fls + sec_fls > 64) {
2987 divisor = nsec * frequency;
2988
2989 while (count_fls + sec_fls > 64) {
2990 REDUCE_FLS(count, sec);
2991 divisor >>= 1;
2992 }
2993
2994 dividend = count * sec;
2995 } else {
2996 dividend = count * sec;
2997
2998 while (nsec_fls + frequency_fls > 64) {
2999 REDUCE_FLS(nsec, frequency);
3000 dividend >>= 1;
3001 }
3002
3003 divisor = nsec * frequency;
3004 }
3005
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003006 if (!divisor)
3007 return dividend;
3008
Peter Zijlstraabd50712010-01-26 18:50:16 +01003009 return div64_u64(dividend, divisor);
3010}
3011
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003012static DEFINE_PER_CPU(int, perf_throttled_count);
3013static DEFINE_PER_CPU(u64, perf_throttled_seq);
3014
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003015static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003016{
3017 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003018 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003019 s64 delta;
3020
Peter Zijlstraabd50712010-01-26 18:50:16 +01003021 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003022
3023 delta = (s64)(period - hwc->sample_period);
3024 delta = (delta + 7) / 8; /* low pass filter */
3025
3026 sample_period = hwc->sample_period + delta;
3027
3028 if (!sample_period)
3029 sample_period = 1;
3030
3031 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003032
Peter Zijlstrae7850592010-05-21 14:43:08 +02003033 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003034 if (disable)
3035 event->pmu->stop(event, PERF_EF_UPDATE);
3036
Peter Zijlstrae7850592010-05-21 14:43:08 +02003037 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003038
3039 if (disable)
3040 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003041 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003042}
3043
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003044/*
3045 * combine freq adjustment with unthrottling to avoid two passes over the
3046 * events. At the same time, make sure, having freq events does not change
3047 * the rate of unthrottling as that would introduce bias.
3048 */
3049static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3050 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003051{
3052 struct perf_event *event;
3053 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003054 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003055 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003056
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003057 /*
3058 * only need to iterate over all events iff:
3059 * - context have events in frequency mode (needs freq adjust)
3060 * - there are events to unthrottle on this cpu
3061 */
3062 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003063 return;
3064
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003065 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003066 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003067
Paul Mackerras03541f82009-10-14 16:58:03 +11003068 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003069 if (event->state != PERF_EVENT_STATE_ACTIVE)
3070 continue;
3071
Stephane Eranian5632ab12011-01-03 18:20:01 +02003072 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003073 continue;
3074
Alexander Shishkin44377272013-12-16 14:17:36 +02003075 perf_pmu_disable(event->pmu);
3076
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003077 hwc = &event->hw;
3078
Jiri Olsaae23bff2013-08-24 16:45:54 +02003079 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003080 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003081 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003082 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003083 }
3084
3085 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003086 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003087
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003088 /*
3089 * stop the event and update event->count
3090 */
3091 event->pmu->stop(event, PERF_EF_UPDATE);
3092
Peter Zijlstrae7850592010-05-21 14:43:08 +02003093 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003094 delta = now - hwc->freq_count_stamp;
3095 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003096
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003097 /*
3098 * restart the event
3099 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003100 * we have stopped the event so tell that
3101 * to perf_adjust_period() to avoid stopping it
3102 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003103 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003104 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003105 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003106
3107 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003108 next:
3109 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003110 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003111
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003112 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003113 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003114}
3115
3116/*
3117 * Round-robin a context's events:
3118 */
3119static void rotate_ctx(struct perf_event_context *ctx)
3120{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003121 /*
3122 * Rotate the first entry last of non-pinned groups. Rotation might be
3123 * disabled by the inheritance code.
3124 */
3125 if (!ctx->rotate_disable)
3126 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003127}
3128
Stephane Eranian9e630202013-04-03 14:21:33 +02003129static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003130{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003131 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003132 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003133
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003134 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003135 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3136 rotate = 1;
3137 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003138
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003139 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003140 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003141 if (ctx->nr_events != ctx->nr_active)
3142 rotate = 1;
3143 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003144
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003145 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003146 goto done;
3147
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003148 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003149 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003150
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003151 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3152 if (ctx)
3153 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003154
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003155 rotate_ctx(&cpuctx->ctx);
3156 if (ctx)
3157 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003158
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003159 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003160
3161 perf_pmu_enable(cpuctx->ctx.pmu);
3162 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003163done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003164
3165 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003166}
3167
3168void perf_event_task_tick(void)
3169{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003170 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3171 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003172 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003173
3174 WARN_ON(!irqs_disabled());
3175
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003176 __this_cpu_inc(perf_throttled_seq);
3177 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003178 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003179
Mark Rutland2fde4f92015-01-07 15:01:54 +00003180 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003181 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003182}
3183
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003184static int event_enable_on_exec(struct perf_event *event,
3185 struct perf_event_context *ctx)
3186{
3187 if (!event->attr.enable_on_exec)
3188 return 0;
3189
3190 event->attr.enable_on_exec = 0;
3191 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3192 return 0;
3193
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003194 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003195
3196 return 1;
3197}
3198
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003199/*
3200 * Enable all of a task's events that have been marked enable-on-exec.
3201 * This expects task == current.
3202 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003203static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003204{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003205 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003206 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003207 struct perf_event *event;
3208 unsigned long flags;
3209 int enabled = 0;
3210
3211 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003212 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003213 if (!ctx || !ctx->nr_events)
3214 goto out;
3215
Peter Zijlstra3e349502016-01-08 10:01:18 +01003216 cpuctx = __get_cpu_context(ctx);
3217 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01003218 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003219 list_for_each_entry(event, &ctx->event_list, event_entry)
3220 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003221
3222 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003223 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003224 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003225 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003226 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003227 ctx_resched(cpuctx, ctx);
3228 }
3229 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003230
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003231out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003232 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003233
3234 if (clone_ctx)
3235 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003236}
3237
Peter Zijlstrae041e322014-05-21 17:32:19 +02003238void perf_event_exec(void)
3239{
Peter Zijlstrae041e322014-05-21 17:32:19 +02003240 int ctxn;
3241
3242 rcu_read_lock();
Peter Zijlstrac1274492015-12-10 20:57:40 +01003243 for_each_task_context_nr(ctxn)
3244 perf_event_enable_on_exec(ctxn);
Peter Zijlstrae041e322014-05-21 17:32:19 +02003245 rcu_read_unlock();
3246}
3247
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003248struct perf_read_data {
3249 struct perf_event *event;
3250 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003251 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003252};
3253
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003254/*
3255 * Cross CPU call to read the hardware event
3256 */
3257static void __perf_event_read(void *info)
3258{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003259 struct perf_read_data *data = info;
3260 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003261 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003262 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003263 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003264
3265 /*
3266 * If this is a task context, we need to check whether it is
3267 * the current task context of this cpu. If not it has been
3268 * scheduled out before the smp call arrived. In that case
3269 * event->count would have been updated to a recent sample
3270 * when the event was scheduled out.
3271 */
3272 if (ctx->task && cpuctx->task_ctx != ctx)
3273 return;
3274
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003275 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003276 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003277 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003278 update_cgrp_time_from_event(event);
3279 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003280
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003281 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003282 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003283 goto unlock;
3284
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003285 if (!data->group) {
3286 pmu->read(event);
3287 data->ret = 0;
3288 goto unlock;
3289 }
3290
3291 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3292
3293 pmu->read(event);
3294
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003295 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3296 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003297 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3298 /*
3299 * Use sibling's PMU rather than @event's since
3300 * sibling could be on different (eg: software) PMU.
3301 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003302 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003303 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003304 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003305
3306 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003307
3308unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003309 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003310}
3311
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003312static inline u64 perf_event_count(struct perf_event *event)
3313{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003314 if (event->pmu->count)
3315 return event->pmu->count(event);
3316
3317 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003318}
3319
Kaixu Xiaffe86902015-08-06 07:02:32 +00003320/*
3321 * NMI-safe method to read a local event, that is an event that
3322 * is:
3323 * - either for the current task, or for this CPU
3324 * - does not have inherit set, for inherited task events
3325 * will not be local and we cannot read them atomically
3326 * - must not have a pmu::count method
3327 */
3328u64 perf_event_read_local(struct perf_event *event)
3329{
3330 unsigned long flags;
3331 u64 val;
3332
3333 /*
3334 * Disabling interrupts avoids all counter scheduling (context
3335 * switches, timer based rotation and IPIs).
3336 */
3337 local_irq_save(flags);
3338
3339 /* If this is a per-task event, it must be for current */
3340 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3341 event->hw.target != current);
3342
3343 /* If this is a per-CPU event, it must be for this CPU */
3344 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3345 event->cpu != smp_processor_id());
3346
3347 /*
3348 * It must not be an event with inherit set, we cannot read
3349 * all child counters from atomic context.
3350 */
3351 WARN_ON_ONCE(event->attr.inherit);
3352
3353 /*
3354 * It must not have a pmu::count method, those are not
3355 * NMI safe.
3356 */
3357 WARN_ON_ONCE(event->pmu->count);
3358
3359 /*
3360 * If the event is currently on this CPU, its either a per-task event,
3361 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3362 * oncpu == -1).
3363 */
3364 if (event->oncpu == smp_processor_id())
3365 event->pmu->read(event);
3366
3367 val = local64_read(&event->count);
3368 local_irq_restore(flags);
3369
3370 return val;
3371}
3372
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003373static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003374{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003375 int ret = 0;
3376
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003377 /*
3378 * If event is enabled and currently active on a CPU, update the
3379 * value in the event structure:
3380 */
3381 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003382 struct perf_read_data data = {
3383 .event = event,
3384 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003385 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003386 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003387 smp_call_function_single(event->oncpu,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003388 __perf_event_read, &data, 1);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003389 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003390 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003391 struct perf_event_context *ctx = event->ctx;
3392 unsigned long flags;
3393
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003394 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003395 /*
3396 * may read while context is not active
3397 * (e.g., thread is blocked), in that case
3398 * we cannot update context time
3399 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003400 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003401 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003402 update_cgrp_time_from_event(event);
3403 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003404 if (group)
3405 update_group_times(event);
3406 else
3407 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003408 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003409 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003410
3411 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003412}
3413
3414/*
3415 * Initialize the perf_event context in a task_struct:
3416 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003417static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003418{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003419 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003420 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003421 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003422 INIT_LIST_HEAD(&ctx->pinned_groups);
3423 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003424 INIT_LIST_HEAD(&ctx->event_list);
3425 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003426}
3427
Peter Zijlstraeb184472010-09-07 15:55:13 +02003428static struct perf_event_context *
3429alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003430{
3431 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003432
3433 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3434 if (!ctx)
3435 return NULL;
3436
3437 __perf_event_init_context(ctx);
3438 if (task) {
3439 ctx->task = task;
3440 get_task_struct(task);
3441 }
3442 ctx->pmu = pmu;
3443
3444 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003445}
3446
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003447static struct task_struct *
3448find_lively_task_by_vpid(pid_t vpid)
3449{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003450 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003451 int err;
3452
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003453 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003454 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003455 task = current;
3456 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003457 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003458 if (task)
3459 get_task_struct(task);
3460 rcu_read_unlock();
3461
3462 if (!task)
3463 return ERR_PTR(-ESRCH);
3464
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003465 /* Reuse ptrace permission checks for now. */
3466 err = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08003467 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003468 goto errout;
3469
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003470 return task;
3471errout:
3472 put_task_struct(task);
3473 return ERR_PTR(err);
3474
3475}
3476
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003477/*
3478 * Returns a matching context with refcount and pincount.
3479 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003480static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003481find_get_context(struct pmu *pmu, struct task_struct *task,
3482 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003483{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003484 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003485 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003486 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003487 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003488 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003489 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003490
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003491 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003492 /* Must be root to operate on a CPU event: */
3493 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3494 return ERR_PTR(-EACCES);
3495
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003496 /*
3497 * We could be clever and allow to attach a event to an
3498 * offline CPU and activate it when the CPU comes up, but
3499 * that's for later.
3500 */
3501 if (!cpu_online(cpu))
3502 return ERR_PTR(-ENODEV);
3503
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003504 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003505 ctx = &cpuctx->ctx;
3506 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003507 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003508
3509 return ctx;
3510 }
3511
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003512 err = -EINVAL;
3513 ctxn = pmu->task_ctx_nr;
3514 if (ctxn < 0)
3515 goto errout;
3516
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003517 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3518 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3519 if (!task_ctx_data) {
3520 err = -ENOMEM;
3521 goto errout;
3522 }
3523 }
3524
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003525retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003526 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003527 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003528 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003529 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003530
3531 if (task_ctx_data && !ctx->task_ctx_data) {
3532 ctx->task_ctx_data = task_ctx_data;
3533 task_ctx_data = NULL;
3534 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003535 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003536
3537 if (clone_ctx)
3538 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003539 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003540 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003541 err = -ENOMEM;
3542 if (!ctx)
3543 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003544
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003545 if (task_ctx_data) {
3546 ctx->task_ctx_data = task_ctx_data;
3547 task_ctx_data = NULL;
3548 }
3549
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003550 err = 0;
3551 mutex_lock(&task->perf_event_mutex);
3552 /*
3553 * If it has already passed perf_event_exit_task().
3554 * we must see PF_EXITING, it takes this mutex too.
3555 */
3556 if (task->flags & PF_EXITING)
3557 err = -ESRCH;
3558 else if (task->perf_event_ctxp[ctxn])
3559 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003560 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003561 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003562 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003563 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003564 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003565 mutex_unlock(&task->perf_event_mutex);
3566
3567 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003568 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003569
3570 if (err == -EAGAIN)
3571 goto retry;
3572 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003573 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003574 }
3575
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003576 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003577 return ctx;
3578
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003579errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003580 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003581 return ERR_PTR(err);
3582}
3583
Li Zefan6fb29152009-10-15 11:21:42 +08003584static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003585static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003586
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003587static void free_event_rcu(struct rcu_head *head)
3588{
3589 struct perf_event *event;
3590
3591 event = container_of(head, struct perf_event, rcu_head);
3592 if (event->ns)
3593 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003594 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003595 kfree(event);
3596}
3597
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003598static void ring_buffer_attach(struct perf_event *event,
3599 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003600
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003601static void unaccount_event_cpu(struct perf_event *event, int cpu)
3602{
3603 if (event->parent)
3604 return;
3605
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003606 if (is_cgroup_event(event))
3607 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3608}
3609
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003610#ifdef CONFIG_NO_HZ_FULL
3611static DEFINE_SPINLOCK(nr_freq_lock);
3612#endif
3613
3614static void unaccount_freq_event_nohz(void)
3615{
3616#ifdef CONFIG_NO_HZ_FULL
3617 spin_lock(&nr_freq_lock);
3618 if (atomic_dec_and_test(&nr_freq_events))
3619 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
3620 spin_unlock(&nr_freq_lock);
3621#endif
3622}
3623
3624static void unaccount_freq_event(void)
3625{
3626 if (tick_nohz_full_enabled())
3627 unaccount_freq_event_nohz();
3628 else
3629 atomic_dec(&nr_freq_events);
3630}
3631
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003632static void unaccount_event(struct perf_event *event)
3633{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003634 bool dec = false;
3635
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003636 if (event->parent)
3637 return;
3638
3639 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003640 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003641 if (event->attr.mmap || event->attr.mmap_data)
3642 atomic_dec(&nr_mmap_events);
3643 if (event->attr.comm)
3644 atomic_dec(&nr_comm_events);
3645 if (event->attr.task)
3646 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003647 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003648 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03003649 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003650 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003651 atomic_dec(&nr_switch_events);
3652 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003653 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003654 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003655 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003656 dec = true;
3657
Peter Zijlstra9107c892016-02-24 18:45:45 +01003658 if (dec) {
3659 if (!atomic_add_unless(&perf_sched_count, -1, 1))
3660 schedule_delayed_work(&perf_sched_work, HZ);
3661 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003662
3663 unaccount_event_cpu(event, event->cpu);
3664}
3665
Peter Zijlstra9107c892016-02-24 18:45:45 +01003666static void perf_sched_delayed(struct work_struct *work)
3667{
3668 mutex_lock(&perf_sched_mutex);
3669 if (atomic_dec_and_test(&perf_sched_count))
3670 static_branch_disable(&perf_sched_events);
3671 mutex_unlock(&perf_sched_mutex);
3672}
3673
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003674/*
3675 * The following implement mutual exclusion of events on "exclusive" pmus
3676 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3677 * at a time, so we disallow creating events that might conflict, namely:
3678 *
3679 * 1) cpu-wide events in the presence of per-task events,
3680 * 2) per-task events in the presence of cpu-wide events,
3681 * 3) two matching events on the same context.
3682 *
3683 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003684 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003685 */
3686static int exclusive_event_init(struct perf_event *event)
3687{
3688 struct pmu *pmu = event->pmu;
3689
3690 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3691 return 0;
3692
3693 /*
3694 * Prevent co-existence of per-task and cpu-wide events on the
3695 * same exclusive pmu.
3696 *
3697 * Negative pmu::exclusive_cnt means there are cpu-wide
3698 * events on this "exclusive" pmu, positive means there are
3699 * per-task events.
3700 *
3701 * Since this is called in perf_event_alloc() path, event::ctx
3702 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3703 * to mean "per-task event", because unlike other attach states it
3704 * never gets cleared.
3705 */
3706 if (event->attach_state & PERF_ATTACH_TASK) {
3707 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
3708 return -EBUSY;
3709 } else {
3710 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
3711 return -EBUSY;
3712 }
3713
3714 return 0;
3715}
3716
3717static void exclusive_event_destroy(struct perf_event *event)
3718{
3719 struct pmu *pmu = event->pmu;
3720
3721 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3722 return;
3723
3724 /* see comment in exclusive_event_init() */
3725 if (event->attach_state & PERF_ATTACH_TASK)
3726 atomic_dec(&pmu->exclusive_cnt);
3727 else
3728 atomic_inc(&pmu->exclusive_cnt);
3729}
3730
3731static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
3732{
3733 if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
3734 (e1->cpu == e2->cpu ||
3735 e1->cpu == -1 ||
3736 e2->cpu == -1))
3737 return true;
3738 return false;
3739}
3740
3741/* Called under the same ctx::mutex as perf_install_in_context() */
3742static bool exclusive_event_installable(struct perf_event *event,
3743 struct perf_event_context *ctx)
3744{
3745 struct perf_event *iter_event;
3746 struct pmu *pmu = event->pmu;
3747
3748 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3749 return true;
3750
3751 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
3752 if (exclusive_event_match(iter_event, event))
3753 return false;
3754 }
3755
3756 return true;
3757}
3758
Peter Zijlstra683ede42014-05-05 12:11:24 +02003759static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003760{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003761 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003762
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003763 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003764
Frederic Weisbecker76369132011-05-19 19:55:04 +02003765 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003766 /*
3767 * Can happen when we close an event with re-directed output.
3768 *
3769 * Since we have a 0 refcount, perf_mmap_close() will skip
3770 * over us; possibly making our ring_buffer_put() the last.
3771 */
3772 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003773 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003774 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003775 }
3776
Stephane Eraniane5d13672011-02-14 11:20:01 +02003777 if (is_cgroup_event(event))
3778 perf_detach_cgroup(event);
3779
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003780 if (!event->parent) {
3781 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3782 put_callchain_buffers();
3783 }
3784
3785 perf_event_free_bpf_prog(event);
3786
3787 if (event->destroy)
3788 event->destroy(event);
3789
3790 if (event->ctx)
3791 put_ctx(event->ctx);
3792
3793 if (event->pmu) {
3794 exclusive_event_destroy(event);
3795 module_put(event->pmu->module);
3796 }
3797
3798 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003799}
3800
Peter Zijlstra683ede42014-05-05 12:11:24 +02003801/*
3802 * Used to free events which have a known refcount of 1, such as in error paths
3803 * where the event isn't exposed yet and inherited events.
3804 */
3805static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003806{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003807 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3808 "unexpected event refcount: %ld; ptr=%p\n",
3809 atomic_long_read(&event->refcount), event)) {
3810 /* leak to avoid use-after-free */
3811 return;
3812 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003813
Peter Zijlstra683ede42014-05-05 12:11:24 +02003814 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003815}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003816
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003817/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003818 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003819 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003820static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003821{
Peter Zijlstra88821352010-11-09 19:01:43 +01003822 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003823
Peter Zijlstra88821352010-11-09 19:01:43 +01003824 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01003825 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003826 * Matches the smp_store_release() in perf_event_exit_task(). If we
3827 * observe !owner it means the list deletion is complete and we can
3828 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01003829 * owner->perf_event_mutex.
3830 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003831 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01003832 if (owner) {
3833 /*
3834 * Since delayed_put_task_struct() also drops the last
3835 * task reference we can safely take a new reference
3836 * while holding the rcu_read_lock().
3837 */
3838 get_task_struct(owner);
3839 }
3840 rcu_read_unlock();
3841
3842 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003843 /*
3844 * If we're here through perf_event_exit_task() we're already
3845 * holding ctx->mutex which would be an inversion wrt. the
3846 * normal lock order.
3847 *
3848 * However we can safely take this lock because its the child
3849 * ctx->mutex.
3850 */
3851 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
3852
Peter Zijlstra88821352010-11-09 19:01:43 +01003853 /*
3854 * We have to re-check the event->owner field, if it is cleared
3855 * we raced with perf_event_exit_task(), acquiring the mutex
3856 * ensured they're done, and we can proceed with freeing the
3857 * event.
3858 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003859 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01003860 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003861 smp_store_release(&event->owner, NULL);
3862 }
Peter Zijlstra88821352010-11-09 19:01:43 +01003863 mutex_unlock(&owner->perf_event_mutex);
3864 put_task_struct(owner);
3865 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003866}
3867
Jiri Olsaf8697762014-08-01 14:33:01 +02003868static void put_event(struct perf_event *event)
3869{
Jiri Olsaf8697762014-08-01 14:33:01 +02003870 if (!atomic_long_dec_and_test(&event->refcount))
3871 return;
3872
Peter Zijlstra683ede42014-05-05 12:11:24 +02003873 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003874}
3875
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003876/*
3877 * Kill an event dead; while event:refcount will preserve the event
3878 * object, it will not preserve its functionality. Once the last 'user'
3879 * gives up the object, we'll destroy the thing.
3880 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02003881int perf_event_release_kernel(struct perf_event *event)
3882{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003883 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003884 struct perf_event *child, *tmp;
3885
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003886 /*
3887 * If we got here through err_file: fput(event_file); we will not have
3888 * attached to a context yet.
3889 */
3890 if (!ctx) {
3891 WARN_ON_ONCE(event->attach_state &
3892 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
3893 goto no_ctx;
3894 }
3895
Peter Zijlstra88821352010-11-09 19:01:43 +01003896 if (!is_kernel_event(event))
3897 perf_remove_from_owner(event);
3898
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01003899 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02003900 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003901 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01003902
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003903 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01003904 /*
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003905 * Mark this even as STATE_DEAD, there is no external reference to it
3906 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003907 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003908 * Anybody acquiring event->child_mutex after the below loop _must_
3909 * also see this, most importantly inherit_event() which will avoid
3910 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003911 *
3912 * Thus this guarantees that we will in fact observe and kill _ALL_
3913 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01003914 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003915 event->state = PERF_EVENT_STATE_DEAD;
3916 raw_spin_unlock_irq(&ctx->lock);
3917
3918 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01003919
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003920again:
3921 mutex_lock(&event->child_mutex);
3922 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01003923
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003924 /*
3925 * Cannot change, child events are not migrated, see the
3926 * comment with perf_event_ctx_lock_nested().
3927 */
3928 ctx = lockless_dereference(child->ctx);
3929 /*
3930 * Since child_mutex nests inside ctx::mutex, we must jump
3931 * through hoops. We start by grabbing a reference on the ctx.
3932 *
3933 * Since the event cannot get freed while we hold the
3934 * child_mutex, the context must also exist and have a !0
3935 * reference count.
3936 */
3937 get_ctx(ctx);
3938
3939 /*
3940 * Now that we have a ctx ref, we can drop child_mutex, and
3941 * acquire ctx::mutex without fear of it going away. Then we
3942 * can re-acquire child_mutex.
3943 */
3944 mutex_unlock(&event->child_mutex);
3945 mutex_lock(&ctx->mutex);
3946 mutex_lock(&event->child_mutex);
3947
3948 /*
3949 * Now that we hold ctx::mutex and child_mutex, revalidate our
3950 * state, if child is still the first entry, it didn't get freed
3951 * and we can continue doing so.
3952 */
3953 tmp = list_first_entry_or_null(&event->child_list,
3954 struct perf_event, child_list);
3955 if (tmp == child) {
3956 perf_remove_from_context(child, DETACH_GROUP);
3957 list_del(&child->child_list);
3958 free_event(child);
3959 /*
3960 * This matches the refcount bump in inherit_event();
3961 * this can't be the last reference.
3962 */
3963 put_event(event);
3964 }
3965
3966 mutex_unlock(&event->child_mutex);
3967 mutex_unlock(&ctx->mutex);
3968 put_ctx(ctx);
3969 goto again;
3970 }
3971 mutex_unlock(&event->child_mutex);
3972
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003973no_ctx:
3974 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02003975 return 0;
3976}
3977EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3978
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02003979/*
3980 * Called when the last reference to the file is gone.
3981 */
Al Viroa6fa9412012-08-20 14:59:25 +01003982static int perf_release(struct inode *inode, struct file *file)
3983{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003984 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01003985 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003986}
3987
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003988u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003989{
3990 struct perf_event *child;
3991 u64 total = 0;
3992
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003993 *enabled = 0;
3994 *running = 0;
3995
Peter Zijlstra6f105812009-11-20 22:19:56 +01003996 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003997
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003998 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003999 total += perf_event_count(event);
4000
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004001 *enabled += event->total_time_enabled +
4002 atomic64_read(&event->child_total_time_enabled);
4003 *running += event->total_time_running +
4004 atomic64_read(&event->child_total_time_running);
4005
4006 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004007 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004008 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004009 *enabled += child->total_time_enabled;
4010 *running += child->total_time_running;
4011 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01004012 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004013
4014 return total;
4015}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004016EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004017
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004018static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004019 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004020{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004021 struct perf_event *sub;
4022 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004023 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01004024
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004025 ret = perf_event_read(leader, true);
4026 if (ret)
4027 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004028
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004029 /*
4030 * Since we co-schedule groups, {enabled,running} times of siblings
4031 * will be identical to those of the leader, so we only publish one
4032 * set.
4033 */
4034 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4035 values[n++] += leader->total_time_enabled +
4036 atomic64_read(&leader->child_total_time_enabled);
4037 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004038
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004039 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4040 values[n++] += leader->total_time_running +
4041 atomic64_read(&leader->child_total_time_running);
4042 }
4043
4044 /*
4045 * Write {count,id} tuples for every sibling.
4046 */
4047 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004048 if (read_format & PERF_FORMAT_ID)
4049 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004050
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004051 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004052 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004053 if (read_format & PERF_FORMAT_ID)
4054 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004055 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004056
4057 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004058}
4059
4060static int perf_read_group(struct perf_event *event,
4061 u64 read_format, char __user *buf)
4062{
4063 struct perf_event *leader = event->group_leader, *child;
4064 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004065 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004066 u64 *values;
4067
4068 lockdep_assert_held(&ctx->mutex);
4069
4070 values = kzalloc(event->read_size, GFP_KERNEL);
4071 if (!values)
4072 return -ENOMEM;
4073
4074 values[0] = 1 + leader->nr_siblings;
4075
4076 /*
4077 * By locking the child_mutex of the leader we effectively
4078 * lock the child list of all siblings.. XXX explain how.
4079 */
4080 mutex_lock(&leader->child_mutex);
4081
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004082 ret = __perf_read_group_add(leader, read_format, values);
4083 if (ret)
4084 goto unlock;
4085
4086 list_for_each_entry(child, &leader->child_list, child_list) {
4087 ret = __perf_read_group_add(child, read_format, values);
4088 if (ret)
4089 goto unlock;
4090 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004091
4092 mutex_unlock(&leader->child_mutex);
4093
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004094 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004095 if (copy_to_user(buf, values, event->read_size))
4096 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004097 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004098
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004099unlock:
4100 mutex_unlock(&leader->child_mutex);
4101out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004102 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004103 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004104}
4105
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004106static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004107 u64 read_format, char __user *buf)
4108{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004109 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004110 u64 values[4];
4111 int n = 0;
4112
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004113 values[n++] = perf_event_read_value(event, &enabled, &running);
4114 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4115 values[n++] = enabled;
4116 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4117 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004118 if (read_format & PERF_FORMAT_ID)
4119 values[n++] = primary_event_id(event);
4120
4121 if (copy_to_user(buf, values, n * sizeof(u64)))
4122 return -EFAULT;
4123
4124 return n * sizeof(u64);
4125}
4126
Jiri Olsadc633982014-09-12 13:18:26 +02004127static bool is_event_hup(struct perf_event *event)
4128{
4129 bool no_children;
4130
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004131 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004132 return false;
4133
4134 mutex_lock(&event->child_mutex);
4135 no_children = list_empty(&event->child_list);
4136 mutex_unlock(&event->child_mutex);
4137 return no_children;
4138}
4139
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004140/*
4141 * Read the performance event - simple non blocking version for now
4142 */
4143static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004144__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004145{
4146 u64 read_format = event->attr.read_format;
4147 int ret;
4148
4149 /*
4150 * Return end-of-file for a read on a event that is in
4151 * error state (i.e. because it was pinned but it couldn't be
4152 * scheduled on to the CPU at some point).
4153 */
4154 if (event->state == PERF_EVENT_STATE_ERROR)
4155 return 0;
4156
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004157 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004158 return -ENOSPC;
4159
4160 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004161 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004162 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004163 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004164 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004165
4166 return ret;
4167}
4168
4169static ssize_t
4170perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4171{
4172 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004173 struct perf_event_context *ctx;
4174 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004175
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004176 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004177 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004178 perf_event_ctx_unlock(event, ctx);
4179
4180 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004181}
4182
4183static unsigned int perf_poll(struct file *file, poll_table *wait)
4184{
4185 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004186 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004187 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004188
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004189 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004190
Jiri Olsadc633982014-09-12 13:18:26 +02004191 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004192 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004193
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004194 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004195 * Pin the event->rb by taking event->mmap_mutex; otherwise
4196 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004197 */
4198 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004199 rb = event->rb;
4200 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004201 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004202 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004203 return events;
4204}
4205
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004206static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004207{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004208 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004209 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004210 perf_event_update_userpage(event);
4211}
4212
4213/*
4214 * Holding the top-level event's child_mutex means that any
4215 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004216 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004217 * task existence requirements of perf_event_enable/disable.
4218 */
4219static void perf_event_for_each_child(struct perf_event *event,
4220 void (*func)(struct perf_event *))
4221{
4222 struct perf_event *child;
4223
4224 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004225
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004226 mutex_lock(&event->child_mutex);
4227 func(event);
4228 list_for_each_entry(child, &event->child_list, child_list)
4229 func(child);
4230 mutex_unlock(&event->child_mutex);
4231}
4232
4233static void perf_event_for_each(struct perf_event *event,
4234 void (*func)(struct perf_event *))
4235{
4236 struct perf_event_context *ctx = event->ctx;
4237 struct perf_event *sibling;
4238
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004239 lockdep_assert_held(&ctx->mutex);
4240
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004241 event = event->group_leader;
4242
4243 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004244 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004245 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004246}
4247
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004248static void __perf_event_period(struct perf_event *event,
4249 struct perf_cpu_context *cpuctx,
4250 struct perf_event_context *ctx,
4251 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004252{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004253 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004254 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004255
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004256 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004257 event->attr.sample_freq = value;
4258 } else {
4259 event->attr.sample_period = value;
4260 event->hw.sample_period = value;
4261 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004262
4263 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4264 if (active) {
4265 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01004266 /*
4267 * We could be throttled; unthrottle now to avoid the tick
4268 * trying to unthrottle while we already re-started the event.
4269 */
4270 if (event->hw.interrupts == MAX_INTERRUPTS) {
4271 event->hw.interrupts = 0;
4272 perf_log_throttle(event, 1);
4273 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004274 event->pmu->stop(event, PERF_EF_UPDATE);
4275 }
4276
4277 local64_set(&event->hw.period_left, 0);
4278
4279 if (active) {
4280 event->pmu->start(event, PERF_EF_RELOAD);
4281 perf_pmu_enable(ctx->pmu);
4282 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004283}
4284
4285static int perf_event_period(struct perf_event *event, u64 __user *arg)
4286{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004287 u64 value;
4288
4289 if (!is_sampling_event(event))
4290 return -EINVAL;
4291
4292 if (copy_from_user(&value, arg, sizeof(value)))
4293 return -EFAULT;
4294
4295 if (!value)
4296 return -EINVAL;
4297
4298 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4299 return -EINVAL;
4300
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004301 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004302
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004303 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004304}
4305
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004306static const struct file_operations perf_fops;
4307
Al Viro2903ff02012-08-28 12:52:22 -04004308static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004309{
Al Viro2903ff02012-08-28 12:52:22 -04004310 struct fd f = fdget(fd);
4311 if (!f.file)
4312 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004313
Al Viro2903ff02012-08-28 12:52:22 -04004314 if (f.file->f_op != &perf_fops) {
4315 fdput(f);
4316 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004317 }
Al Viro2903ff02012-08-28 12:52:22 -04004318 *p = f;
4319 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004320}
4321
4322static int perf_event_set_output(struct perf_event *event,
4323 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004324static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004325static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004326
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004327static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004328{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004329 void (*func)(struct perf_event *);
4330 u32 flags = arg;
4331
4332 switch (cmd) {
4333 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004334 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004335 break;
4336 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004337 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004338 break;
4339 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004340 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004341 break;
4342
4343 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004344 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004345
4346 case PERF_EVENT_IOC_PERIOD:
4347 return perf_event_period(event, (u64 __user *)arg);
4348
Jiri Olsacf4957f2012-10-24 13:37:58 +02004349 case PERF_EVENT_IOC_ID:
4350 {
4351 u64 id = primary_event_id(event);
4352
4353 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4354 return -EFAULT;
4355 return 0;
4356 }
4357
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004358 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004359 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004360 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004361 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004362 struct perf_event *output_event;
4363 struct fd output;
4364 ret = perf_fget_light(arg, &output);
4365 if (ret)
4366 return ret;
4367 output_event = output.file->private_data;
4368 ret = perf_event_set_output(event, output_event);
4369 fdput(output);
4370 } else {
4371 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004372 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004373 return ret;
4374 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004375
Li Zefan6fb29152009-10-15 11:21:42 +08004376 case PERF_EVENT_IOC_SET_FILTER:
4377 return perf_event_set_filter(event, (void __user *)arg);
4378
Alexei Starovoitov25415172015-03-25 12:49:20 -07004379 case PERF_EVENT_IOC_SET_BPF:
4380 return perf_event_set_bpf_prog(event, arg);
4381
Wang Nan86e79722016-03-28 06:41:29 +00004382 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
4383 struct ring_buffer *rb;
4384
4385 rcu_read_lock();
4386 rb = rcu_dereference(event->rb);
4387 if (!rb || !rb->nr_pages) {
4388 rcu_read_unlock();
4389 return -EINVAL;
4390 }
4391 rb_toggle_paused(rb, !!arg);
4392 rcu_read_unlock();
4393 return 0;
4394 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004395 default:
4396 return -ENOTTY;
4397 }
4398
4399 if (flags & PERF_IOC_FLAG_GROUP)
4400 perf_event_for_each(event, func);
4401 else
4402 perf_event_for_each_child(event, func);
4403
4404 return 0;
4405}
4406
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004407static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4408{
4409 struct perf_event *event = file->private_data;
4410 struct perf_event_context *ctx;
4411 long ret;
4412
4413 ctx = perf_event_ctx_lock(event);
4414 ret = _perf_ioctl(event, cmd, arg);
4415 perf_event_ctx_unlock(event, ctx);
4416
4417 return ret;
4418}
4419
Pawel Mollb3f20782014-06-13 16:03:32 +01004420#ifdef CONFIG_COMPAT
4421static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4422 unsigned long arg)
4423{
4424 switch (_IOC_NR(cmd)) {
4425 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4426 case _IOC_NR(PERF_EVENT_IOC_ID):
4427 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4428 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4429 cmd &= ~IOCSIZE_MASK;
4430 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4431 }
4432 break;
4433 }
4434 return perf_ioctl(file, cmd, arg);
4435}
4436#else
4437# define perf_compat_ioctl NULL
4438#endif
4439
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004440int perf_event_task_enable(void)
4441{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004442 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004443 struct perf_event *event;
4444
4445 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004446 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4447 ctx = perf_event_ctx_lock(event);
4448 perf_event_for_each_child(event, _perf_event_enable);
4449 perf_event_ctx_unlock(event, ctx);
4450 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004451 mutex_unlock(&current->perf_event_mutex);
4452
4453 return 0;
4454}
4455
4456int perf_event_task_disable(void)
4457{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004458 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004459 struct perf_event *event;
4460
4461 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004462 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4463 ctx = perf_event_ctx_lock(event);
4464 perf_event_for_each_child(event, _perf_event_disable);
4465 perf_event_ctx_unlock(event, ctx);
4466 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004467 mutex_unlock(&current->perf_event_mutex);
4468
4469 return 0;
4470}
4471
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004472static int perf_event_index(struct perf_event *event)
4473{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004474 if (event->hw.state & PERF_HES_STOPPED)
4475 return 0;
4476
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004477 if (event->state != PERF_EVENT_STATE_ACTIVE)
4478 return 0;
4479
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004480 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004481}
4482
Eric B Munsonc4794292011-06-23 16:34:38 -04004483static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004484 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004485 u64 *enabled,
4486 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004487{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004488 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004489
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004490 *now = perf_clock();
4491 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004492 *enabled = ctx_time - event->tstamp_enabled;
4493 *running = ctx_time - event->tstamp_running;
4494}
4495
Peter Zijlstrafa731582013-09-19 10:16:42 +02004496static void perf_event_init_userpage(struct perf_event *event)
4497{
4498 struct perf_event_mmap_page *userpg;
4499 struct ring_buffer *rb;
4500
4501 rcu_read_lock();
4502 rb = rcu_dereference(event->rb);
4503 if (!rb)
4504 goto unlock;
4505
4506 userpg = rb->user_page;
4507
4508 /* Allow new userspace to detect that bit 0 is deprecated */
4509 userpg->cap_bit0_is_deprecated = 1;
4510 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004511 userpg->data_offset = PAGE_SIZE;
4512 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004513
4514unlock:
4515 rcu_read_unlock();
4516}
4517
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004518void __weak arch_perf_update_userpage(
4519 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004520{
4521}
4522
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004523/*
4524 * Callers need to ensure there can be no nesting of this function, otherwise
4525 * the seqlock logic goes bad. We can not serialize this because the arch
4526 * code calls this from NMI context.
4527 */
4528void perf_event_update_userpage(struct perf_event *event)
4529{
4530 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004531 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004532 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004533
4534 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004535 rb = rcu_dereference(event->rb);
4536 if (!rb)
4537 goto unlock;
4538
Eric B Munson0d641202011-06-24 12:26:26 -04004539 /*
4540 * compute total_time_enabled, total_time_running
4541 * based on snapshot values taken when the event
4542 * was last scheduled in.
4543 *
4544 * we cannot simply called update_context_time()
4545 * because of locking issue as we can be called in
4546 * NMI context
4547 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004548 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004549
Frederic Weisbecker76369132011-05-19 19:55:04 +02004550 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004551 /*
4552 * Disable preemption so as to not let the corresponding user-space
4553 * spin too long if we get preempted.
4554 */
4555 preempt_disable();
4556 ++userpg->lock;
4557 barrier();
4558 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004559 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004560 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004561 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004562
Eric B Munson0d641202011-06-24 12:26:26 -04004563 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004564 atomic64_read(&event->child_total_time_enabled);
4565
Eric B Munson0d641202011-06-24 12:26:26 -04004566 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004567 atomic64_read(&event->child_total_time_running);
4568
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004569 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004570
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004571 barrier();
4572 ++userpg->lock;
4573 preempt_enable();
4574unlock:
4575 rcu_read_unlock();
4576}
4577
Peter Zijlstra906010b2009-09-21 16:08:49 +02004578static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4579{
4580 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004581 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004582 int ret = VM_FAULT_SIGBUS;
4583
4584 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4585 if (vmf->pgoff == 0)
4586 ret = 0;
4587 return ret;
4588 }
4589
4590 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004591 rb = rcu_dereference(event->rb);
4592 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004593 goto unlock;
4594
4595 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4596 goto unlock;
4597
Frederic Weisbecker76369132011-05-19 19:55:04 +02004598 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004599 if (!vmf->page)
4600 goto unlock;
4601
4602 get_page(vmf->page);
4603 vmf->page->mapping = vma->vm_file->f_mapping;
4604 vmf->page->index = vmf->pgoff;
4605
4606 ret = 0;
4607unlock:
4608 rcu_read_unlock();
4609
4610 return ret;
4611}
4612
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004613static void ring_buffer_attach(struct perf_event *event,
4614 struct ring_buffer *rb)
4615{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004616 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004617 unsigned long flags;
4618
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004619 if (event->rb) {
4620 /*
4621 * Should be impossible, we set this when removing
4622 * event->rb_entry and wait/clear when adding event->rb_entry.
4623 */
4624 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004625
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004626 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004627 spin_lock_irqsave(&old_rb->event_lock, flags);
4628 list_del_rcu(&event->rb_entry);
4629 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004630
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004631 event->rcu_batches = get_state_synchronize_rcu();
4632 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004633 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004634
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004635 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004636 if (event->rcu_pending) {
4637 cond_synchronize_rcu(event->rcu_batches);
4638 event->rcu_pending = 0;
4639 }
4640
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004641 spin_lock_irqsave(&rb->event_lock, flags);
4642 list_add_rcu(&event->rb_entry, &rb->event_list);
4643 spin_unlock_irqrestore(&rb->event_lock, flags);
4644 }
4645
4646 rcu_assign_pointer(event->rb, rb);
4647
4648 if (old_rb) {
4649 ring_buffer_put(old_rb);
4650 /*
4651 * Since we detached before setting the new rb, so that we
4652 * could attach the new rb, we could have missed a wakeup.
4653 * Provide it now.
4654 */
4655 wake_up_all(&event->waitq);
4656 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004657}
4658
4659static void ring_buffer_wakeup(struct perf_event *event)
4660{
4661 struct ring_buffer *rb;
4662
4663 rcu_read_lock();
4664 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004665 if (rb) {
4666 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4667 wake_up_all(&event->waitq);
4668 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004669 rcu_read_unlock();
4670}
4671
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004672struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004673{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004674 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004675
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004676 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004677 rb = rcu_dereference(event->rb);
4678 if (rb) {
4679 if (!atomic_inc_not_zero(&rb->refcount))
4680 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004681 }
4682 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004683
Frederic Weisbecker76369132011-05-19 19:55:04 +02004684 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004685}
4686
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004687void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004688{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004689 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004690 return;
4691
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004692 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004693
Frederic Weisbecker76369132011-05-19 19:55:04 +02004694 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004695}
4696
4697static void perf_mmap_open(struct vm_area_struct *vma)
4698{
4699 struct perf_event *event = vma->vm_file->private_data;
4700
4701 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004702 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004703
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004704 if (vma->vm_pgoff)
4705 atomic_inc(&event->rb->aux_mmap_count);
4706
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004707 if (event->pmu->event_mapped)
4708 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004709}
4710
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004711static void perf_pmu_output_stop(struct perf_event *event);
4712
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004713/*
4714 * A buffer can be mmap()ed multiple times; either directly through the same
4715 * event, or through other events by use of perf_event_set_output().
4716 *
4717 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4718 * the buffer here, where we still have a VM context. This means we need
4719 * to detach all events redirecting to us.
4720 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004721static void perf_mmap_close(struct vm_area_struct *vma)
4722{
4723 struct perf_event *event = vma->vm_file->private_data;
4724
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004725 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004726 struct user_struct *mmap_user = rb->mmap_user;
4727 int mmap_locked = rb->mmap_locked;
4728 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004729
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004730 if (event->pmu->event_unmapped)
4731 event->pmu->event_unmapped(event);
4732
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004733 /*
4734 * rb->aux_mmap_count will always drop before rb->mmap_count and
4735 * event->mmap_count, so it is ok to use event->mmap_mutex to
4736 * serialize with perf_mmap here.
4737 */
4738 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
4739 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004740 /*
4741 * Stop all AUX events that are writing to this buffer,
4742 * so that we can free its AUX pages and corresponding PMU
4743 * data. Note that after rb::aux_mmap_count dropped to zero,
4744 * they won't start any more (see perf_aux_output_begin()).
4745 */
4746 perf_pmu_output_stop(event);
4747
4748 /* now it's safe to free the pages */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004749 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
4750 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
4751
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004752 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004753 rb_free_aux(rb);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004754 WARN_ON_ONCE(atomic_read(&rb->aux_refcount));
4755
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004756 mutex_unlock(&event->mmap_mutex);
4757 }
4758
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004759 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004760
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004761 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004762 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004763
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004764 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004765 mutex_unlock(&event->mmap_mutex);
4766
4767 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004768 if (atomic_read(&rb->mmap_count))
4769 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004770
4771 /*
4772 * No other mmap()s, detach from all other events that might redirect
4773 * into the now unreachable buffer. Somewhat complicated by the
4774 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4775 */
4776again:
4777 rcu_read_lock();
4778 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4779 if (!atomic_long_inc_not_zero(&event->refcount)) {
4780 /*
4781 * This event is en-route to free_event() which will
4782 * detach it and remove it from the list.
4783 */
4784 continue;
4785 }
4786 rcu_read_unlock();
4787
4788 mutex_lock(&event->mmap_mutex);
4789 /*
4790 * Check we didn't race with perf_event_set_output() which can
4791 * swizzle the rb from under us while we were waiting to
4792 * acquire mmap_mutex.
4793 *
4794 * If we find a different rb; ignore this event, a next
4795 * iteration will no longer find it on the list. We have to
4796 * still restart the iteration to make sure we're not now
4797 * iterating the wrong list.
4798 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004799 if (event->rb == rb)
4800 ring_buffer_attach(event, NULL);
4801
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004802 mutex_unlock(&event->mmap_mutex);
4803 put_event(event);
4804
4805 /*
4806 * Restart the iteration; either we're on the wrong list or
4807 * destroyed its integrity by doing a deletion.
4808 */
4809 goto again;
4810 }
4811 rcu_read_unlock();
4812
4813 /*
4814 * It could be there's still a few 0-ref events on the list; they'll
4815 * get cleaned up by free_event() -- they'll also still have their
4816 * ref on the rb and will free it whenever they are done with it.
4817 *
4818 * Aside from that, this buffer is 'fully' detached and unmapped,
4819 * undo the VM accounting.
4820 */
4821
4822 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4823 vma->vm_mm->pinned_vm -= mmap_locked;
4824 free_uid(mmap_user);
4825
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004826out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004827 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004828}
4829
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004830static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004831 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004832 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004833 .fault = perf_mmap_fault,
4834 .page_mkwrite = perf_mmap_fault,
4835};
4836
4837static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4838{
4839 struct perf_event *event = file->private_data;
4840 unsigned long user_locked, user_lock_limit;
4841 struct user_struct *user = current_user();
4842 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004843 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004844 unsigned long vma_size;
4845 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004846 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004847 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004848
Peter Zijlstrac7920612010-05-18 10:33:24 +02004849 /*
4850 * Don't allow mmap() of inherited per-task counters. This would
4851 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004852 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004853 */
4854 if (event->cpu == -1 && event->attr.inherit)
4855 return -EINVAL;
4856
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004857 if (!(vma->vm_flags & VM_SHARED))
4858 return -EINVAL;
4859
4860 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004861
4862 if (vma->vm_pgoff == 0) {
4863 nr_pages = (vma_size / PAGE_SIZE) - 1;
4864 } else {
4865 /*
4866 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
4867 * mapped, all subsequent mappings should have the same size
4868 * and offset. Must be above the normal perf buffer.
4869 */
4870 u64 aux_offset, aux_size;
4871
4872 if (!event->rb)
4873 return -EINVAL;
4874
4875 nr_pages = vma_size / PAGE_SIZE;
4876
4877 mutex_lock(&event->mmap_mutex);
4878 ret = -EINVAL;
4879
4880 rb = event->rb;
4881 if (!rb)
4882 goto aux_unlock;
4883
4884 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
4885 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
4886
4887 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
4888 goto aux_unlock;
4889
4890 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
4891 goto aux_unlock;
4892
4893 /* already mapped with a different offset */
4894 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
4895 goto aux_unlock;
4896
4897 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
4898 goto aux_unlock;
4899
4900 /* already mapped with a different size */
4901 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
4902 goto aux_unlock;
4903
4904 if (!is_power_of_2(nr_pages))
4905 goto aux_unlock;
4906
4907 if (!atomic_inc_not_zero(&rb->mmap_count))
4908 goto aux_unlock;
4909
4910 if (rb_has_aux(rb)) {
4911 atomic_inc(&rb->aux_mmap_count);
4912 ret = 0;
4913 goto unlock;
4914 }
4915
4916 atomic_set(&rb->aux_mmap_count, 1);
4917 user_extra = nr_pages;
4918
4919 goto accounting;
4920 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004921
4922 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004923 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004924 * can do bitmasks instead of modulo.
4925 */
Kan Liang2ed11312015-03-02 02:14:26 -05004926 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004927 return -EINVAL;
4928
4929 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4930 return -EINVAL;
4931
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004932 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004933again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004934 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004935 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004936 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004937 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004938 goto unlock;
4939 }
4940
4941 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4942 /*
4943 * Raced against perf_mmap_close() through
4944 * perf_event_set_output(). Try again, hope for better
4945 * luck.
4946 */
4947 mutex_unlock(&event->mmap_mutex);
4948 goto again;
4949 }
4950
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004951 goto unlock;
4952 }
4953
4954 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004955
4956accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004957 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4958
4959 /*
4960 * Increase the limit linearly with more CPUs:
4961 */
4962 user_lock_limit *= num_online_cpus();
4963
4964 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4965
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004966 if (user_locked > user_lock_limit)
4967 extra = user_locked - user_lock_limit;
4968
Jiri Slaby78d7d402010-03-05 13:42:54 -08004969 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004970 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004971 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004972
4973 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4974 !capable(CAP_IPC_LOCK)) {
4975 ret = -EPERM;
4976 goto unlock;
4977 }
4978
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004979 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004980
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004981 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004982 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004983
Frederic Weisbecker76369132011-05-19 19:55:04 +02004984 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004985 rb = rb_alloc(nr_pages,
4986 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4987 event->cpu, flags);
4988
4989 if (!rb) {
4990 ret = -ENOMEM;
4991 goto unlock;
4992 }
4993
4994 atomic_set(&rb->mmap_count, 1);
4995 rb->mmap_user = get_current_user();
4996 rb->mmap_locked = extra;
4997
4998 ring_buffer_attach(event, rb);
4999
5000 perf_event_init_userpage(event);
5001 perf_event_update_userpage(event);
5002 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02005003 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
5004 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005005 if (!ret)
5006 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005007 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005008
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005009unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005010 if (!ret) {
5011 atomic_long_add(user_extra, &user->locked_vm);
5012 vma->vm_mm->pinned_vm += extra;
5013
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005014 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005015 } else if (rb) {
5016 atomic_dec(&rb->mmap_count);
5017 }
5018aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005019 mutex_unlock(&event->mmap_mutex);
5020
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005021 /*
5022 * Since pinned accounting is per vm we cannot allow fork() to copy our
5023 * vma.
5024 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005025 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005026 vma->vm_ops = &perf_mmap_vmops;
5027
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005028 if (event->pmu->event_mapped)
5029 event->pmu->event_mapped(event);
5030
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005031 return ret;
5032}
5033
5034static int perf_fasync(int fd, struct file *filp, int on)
5035{
Al Viro496ad9a2013-01-23 17:07:38 -05005036 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005037 struct perf_event *event = filp->private_data;
5038 int retval;
5039
Al Viro59551022016-01-22 15:40:57 -05005040 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005041 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05005042 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005043
5044 if (retval < 0)
5045 return retval;
5046
5047 return 0;
5048}
5049
5050static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01005051 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005052 .release = perf_release,
5053 .read = perf_read,
5054 .poll = perf_poll,
5055 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01005056 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005057 .mmap = perf_mmap,
5058 .fasync = perf_fasync,
5059};
5060
5061/*
5062 * Perf event wakeup
5063 *
5064 * If there's data, ensure we set the poll() state and publish everything
5065 * to user-space before waking everybody up.
5066 */
5067
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005068static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
5069{
5070 /* only the parent has fasync state */
5071 if (event->parent)
5072 event = event->parent;
5073 return &event->fasync;
5074}
5075
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005076void perf_event_wakeup(struct perf_event *event)
5077{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005078 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005079
5080 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005081 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005082 event->pending_kill = 0;
5083 }
5084}
5085
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005086static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005087{
5088 struct perf_event *event = container_of(entry,
5089 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01005090 int rctx;
5091
5092 rctx = perf_swevent_get_recursion_context();
5093 /*
5094 * If we 'fail' here, that's OK, it means recursion is already disabled
5095 * and we won't recurse 'further'.
5096 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005097
5098 if (event->pending_disable) {
5099 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005100 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005101 }
5102
5103 if (event->pending_wakeup) {
5104 event->pending_wakeup = 0;
5105 perf_event_wakeup(event);
5106 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01005107
5108 if (rctx >= 0)
5109 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005110}
5111
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005112/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005113 * We assume there is only KVM supporting the callbacks.
5114 * Later on, we might change it to a list if there is
5115 * another virtualization implementation supporting the callbacks.
5116 */
5117struct perf_guest_info_callbacks *perf_guest_cbs;
5118
5119int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5120{
5121 perf_guest_cbs = cbs;
5122 return 0;
5123}
5124EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5125
5126int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5127{
5128 perf_guest_cbs = NULL;
5129 return 0;
5130}
5131EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5132
Jiri Olsa40189942012-08-07 15:20:37 +02005133static void
5134perf_output_sample_regs(struct perf_output_handle *handle,
5135 struct pt_regs *regs, u64 mask)
5136{
5137 int bit;
5138
5139 for_each_set_bit(bit, (const unsigned long *) &mask,
5140 sizeof(mask) * BITS_PER_BYTE) {
5141 u64 val;
5142
5143 val = perf_reg_value(regs, bit);
5144 perf_output_put(handle, val);
5145 }
5146}
5147
Stephane Eranian60e23642014-09-24 13:48:37 +02005148static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005149 struct pt_regs *regs,
5150 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02005151{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005152 if (user_mode(regs)) {
5153 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02005154 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005155 } else if (current->mm) {
5156 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005157 } else {
5158 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5159 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005160 }
5161}
5162
Stephane Eranian60e23642014-09-24 13:48:37 +02005163static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5164 struct pt_regs *regs)
5165{
5166 regs_intr->regs = regs;
5167 regs_intr->abi = perf_reg_abi(current);
5168}
5169
5170
Jiri Olsac5ebced2012-08-07 15:20:40 +02005171/*
5172 * Get remaining task size from user stack pointer.
5173 *
5174 * It'd be better to take stack vma map and limit this more
5175 * precisly, but there's no way to get it safely under interrupt,
5176 * so using TASK_SIZE as limit.
5177 */
5178static u64 perf_ustack_task_size(struct pt_regs *regs)
5179{
5180 unsigned long addr = perf_user_stack_pointer(regs);
5181
5182 if (!addr || addr >= TASK_SIZE)
5183 return 0;
5184
5185 return TASK_SIZE - addr;
5186}
5187
5188static u16
5189perf_sample_ustack_size(u16 stack_size, u16 header_size,
5190 struct pt_regs *regs)
5191{
5192 u64 task_size;
5193
5194 /* No regs, no stack pointer, no dump. */
5195 if (!regs)
5196 return 0;
5197
5198 /*
5199 * Check if we fit in with the requested stack size into the:
5200 * - TASK_SIZE
5201 * If we don't, we limit the size to the TASK_SIZE.
5202 *
5203 * - remaining sample size
5204 * If we don't, we customize the stack size to
5205 * fit in to the remaining sample size.
5206 */
5207
5208 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5209 stack_size = min(stack_size, (u16) task_size);
5210
5211 /* Current header size plus static size and dynamic size. */
5212 header_size += 2 * sizeof(u64);
5213
5214 /* Do we fit in with the current stack dump size? */
5215 if ((u16) (header_size + stack_size) < header_size) {
5216 /*
5217 * If we overflow the maximum size for the sample,
5218 * we customize the stack dump size to fit in.
5219 */
5220 stack_size = USHRT_MAX - header_size - sizeof(u64);
5221 stack_size = round_up(stack_size, sizeof(u64));
5222 }
5223
5224 return stack_size;
5225}
5226
5227static void
5228perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5229 struct pt_regs *regs)
5230{
5231 /* Case of a kernel thread, nothing to dump */
5232 if (!regs) {
5233 u64 size = 0;
5234 perf_output_put(handle, size);
5235 } else {
5236 unsigned long sp;
5237 unsigned int rem;
5238 u64 dyn_size;
5239
5240 /*
5241 * We dump:
5242 * static size
5243 * - the size requested by user or the best one we can fit
5244 * in to the sample max size
5245 * data
5246 * - user stack dump data
5247 * dynamic size
5248 * - the actual dumped size
5249 */
5250
5251 /* Static size. */
5252 perf_output_put(handle, dump_size);
5253
5254 /* Data. */
5255 sp = perf_user_stack_pointer(regs);
5256 rem = __output_copy_user(handle, (void *) sp, dump_size);
5257 dyn_size = dump_size - rem;
5258
5259 perf_output_skip(handle, rem);
5260
5261 /* Dynamic size. */
5262 perf_output_put(handle, dyn_size);
5263 }
5264}
5265
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005266static void __perf_event_header__init_id(struct perf_event_header *header,
5267 struct perf_sample_data *data,
5268 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005269{
5270 u64 sample_type = event->attr.sample_type;
5271
5272 data->type = sample_type;
5273 header->size += event->id_header_size;
5274
5275 if (sample_type & PERF_SAMPLE_TID) {
5276 /* namespace issues */
5277 data->tid_entry.pid = perf_event_pid(event, current);
5278 data->tid_entry.tid = perf_event_tid(event, current);
5279 }
5280
5281 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005282 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005283
Adrian Hunterff3d5272013-08-27 11:23:07 +03005284 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005285 data->id = primary_event_id(event);
5286
5287 if (sample_type & PERF_SAMPLE_STREAM_ID)
5288 data->stream_id = event->id;
5289
5290 if (sample_type & PERF_SAMPLE_CPU) {
5291 data->cpu_entry.cpu = raw_smp_processor_id();
5292 data->cpu_entry.reserved = 0;
5293 }
5294}
5295
Frederic Weisbecker76369132011-05-19 19:55:04 +02005296void perf_event_header__init_id(struct perf_event_header *header,
5297 struct perf_sample_data *data,
5298 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005299{
5300 if (event->attr.sample_id_all)
5301 __perf_event_header__init_id(header, data, event);
5302}
5303
5304static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5305 struct perf_sample_data *data)
5306{
5307 u64 sample_type = data->type;
5308
5309 if (sample_type & PERF_SAMPLE_TID)
5310 perf_output_put(handle, data->tid_entry);
5311
5312 if (sample_type & PERF_SAMPLE_TIME)
5313 perf_output_put(handle, data->time);
5314
5315 if (sample_type & PERF_SAMPLE_ID)
5316 perf_output_put(handle, data->id);
5317
5318 if (sample_type & PERF_SAMPLE_STREAM_ID)
5319 perf_output_put(handle, data->stream_id);
5320
5321 if (sample_type & PERF_SAMPLE_CPU)
5322 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005323
5324 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5325 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005326}
5327
Frederic Weisbecker76369132011-05-19 19:55:04 +02005328void perf_event__output_id_sample(struct perf_event *event,
5329 struct perf_output_handle *handle,
5330 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005331{
5332 if (event->attr.sample_id_all)
5333 __perf_event__output_id_sample(handle, sample);
5334}
5335
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005336static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005337 struct perf_event *event,
5338 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005339{
5340 u64 read_format = event->attr.read_format;
5341 u64 values[4];
5342 int n = 0;
5343
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005344 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005345 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005346 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005347 atomic64_read(&event->child_total_time_enabled);
5348 }
5349 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005350 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005351 atomic64_read(&event->child_total_time_running);
5352 }
5353 if (read_format & PERF_FORMAT_ID)
5354 values[n++] = primary_event_id(event);
5355
Frederic Weisbecker76369132011-05-19 19:55:04 +02005356 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005357}
5358
5359/*
5360 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
5361 */
5362static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005363 struct perf_event *event,
5364 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005365{
5366 struct perf_event *leader = event->group_leader, *sub;
5367 u64 read_format = event->attr.read_format;
5368 u64 values[5];
5369 int n = 0;
5370
5371 values[n++] = 1 + leader->nr_siblings;
5372
5373 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005374 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005375
5376 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005377 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005378
5379 if (leader != event)
5380 leader->pmu->read(leader);
5381
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005382 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005383 if (read_format & PERF_FORMAT_ID)
5384 values[n++] = primary_event_id(leader);
5385
Frederic Weisbecker76369132011-05-19 19:55:04 +02005386 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005387
5388 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5389 n = 0;
5390
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005391 if ((sub != event) &&
5392 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005393 sub->pmu->read(sub);
5394
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005395 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005396 if (read_format & PERF_FORMAT_ID)
5397 values[n++] = primary_event_id(sub);
5398
Frederic Weisbecker76369132011-05-19 19:55:04 +02005399 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005400 }
5401}
5402
Stephane Eranianeed01522010-10-26 16:08:01 +02005403#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5404 PERF_FORMAT_TOTAL_TIME_RUNNING)
5405
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005406static void perf_output_read(struct perf_output_handle *handle,
5407 struct perf_event *event)
5408{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005409 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005410 u64 read_format = event->attr.read_format;
5411
5412 /*
5413 * compute total_time_enabled, total_time_running
5414 * based on snapshot values taken when the event
5415 * was last scheduled in.
5416 *
5417 * we cannot simply called update_context_time()
5418 * because of locking issue as we are called in
5419 * NMI context
5420 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005421 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005422 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005423
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005424 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005425 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005426 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005427 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005428}
5429
5430void perf_output_sample(struct perf_output_handle *handle,
5431 struct perf_event_header *header,
5432 struct perf_sample_data *data,
5433 struct perf_event *event)
5434{
5435 u64 sample_type = data->type;
5436
5437 perf_output_put(handle, *header);
5438
Adrian Hunterff3d5272013-08-27 11:23:07 +03005439 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5440 perf_output_put(handle, data->id);
5441
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005442 if (sample_type & PERF_SAMPLE_IP)
5443 perf_output_put(handle, data->ip);
5444
5445 if (sample_type & PERF_SAMPLE_TID)
5446 perf_output_put(handle, data->tid_entry);
5447
5448 if (sample_type & PERF_SAMPLE_TIME)
5449 perf_output_put(handle, data->time);
5450
5451 if (sample_type & PERF_SAMPLE_ADDR)
5452 perf_output_put(handle, data->addr);
5453
5454 if (sample_type & PERF_SAMPLE_ID)
5455 perf_output_put(handle, data->id);
5456
5457 if (sample_type & PERF_SAMPLE_STREAM_ID)
5458 perf_output_put(handle, data->stream_id);
5459
5460 if (sample_type & PERF_SAMPLE_CPU)
5461 perf_output_put(handle, data->cpu_entry);
5462
5463 if (sample_type & PERF_SAMPLE_PERIOD)
5464 perf_output_put(handle, data->period);
5465
5466 if (sample_type & PERF_SAMPLE_READ)
5467 perf_output_read(handle, event);
5468
5469 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5470 if (data->callchain) {
5471 int size = 1;
5472
5473 if (data->callchain)
5474 size += data->callchain->nr;
5475
5476 size *= sizeof(u64);
5477
Frederic Weisbecker76369132011-05-19 19:55:04 +02005478 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005479 } else {
5480 u64 nr = 0;
5481 perf_output_put(handle, nr);
5482 }
5483 }
5484
5485 if (sample_type & PERF_SAMPLE_RAW) {
5486 if (data->raw) {
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005487 u32 raw_size = data->raw->size;
5488 u32 real_size = round_up(raw_size + sizeof(u32),
5489 sizeof(u64)) - sizeof(u32);
5490 u64 zero = 0;
5491
5492 perf_output_put(handle, real_size);
5493 __output_copy(handle, data->raw->data, raw_size);
5494 if (real_size - raw_size)
5495 __output_copy(handle, &zero, real_size - raw_size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005496 } else {
5497 struct {
5498 u32 size;
5499 u32 data;
5500 } raw = {
5501 .size = sizeof(u32),
5502 .data = 0,
5503 };
5504 perf_output_put(handle, raw);
5505 }
5506 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005507
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005508 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5509 if (data->br_stack) {
5510 size_t size;
5511
5512 size = data->br_stack->nr
5513 * sizeof(struct perf_branch_entry);
5514
5515 perf_output_put(handle, data->br_stack->nr);
5516 perf_output_copy(handle, data->br_stack->entries, size);
5517 } else {
5518 /*
5519 * we always store at least the value of nr
5520 */
5521 u64 nr = 0;
5522 perf_output_put(handle, nr);
5523 }
5524 }
Jiri Olsa40189942012-08-07 15:20:37 +02005525
5526 if (sample_type & PERF_SAMPLE_REGS_USER) {
5527 u64 abi = data->regs_user.abi;
5528
5529 /*
5530 * If there are no regs to dump, notice it through
5531 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5532 */
5533 perf_output_put(handle, abi);
5534
5535 if (abi) {
5536 u64 mask = event->attr.sample_regs_user;
5537 perf_output_sample_regs(handle,
5538 data->regs_user.regs,
5539 mask);
5540 }
5541 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005542
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005543 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005544 perf_output_sample_ustack(handle,
5545 data->stack_user_size,
5546 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005547 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005548
5549 if (sample_type & PERF_SAMPLE_WEIGHT)
5550 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005551
5552 if (sample_type & PERF_SAMPLE_DATA_SRC)
5553 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005554
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005555 if (sample_type & PERF_SAMPLE_TRANSACTION)
5556 perf_output_put(handle, data->txn);
5557
Stephane Eranian60e23642014-09-24 13:48:37 +02005558 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5559 u64 abi = data->regs_intr.abi;
5560 /*
5561 * If there are no regs to dump, notice it through
5562 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5563 */
5564 perf_output_put(handle, abi);
5565
5566 if (abi) {
5567 u64 mask = event->attr.sample_regs_intr;
5568
5569 perf_output_sample_regs(handle,
5570 data->regs_intr.regs,
5571 mask);
5572 }
5573 }
5574
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005575 if (!event->attr.watermark) {
5576 int wakeup_events = event->attr.wakeup_events;
5577
5578 if (wakeup_events) {
5579 struct ring_buffer *rb = handle->rb;
5580 int events = local_inc_return(&rb->events);
5581
5582 if (events >= wakeup_events) {
5583 local_sub(wakeup_events, &rb->events);
5584 local_inc(&rb->wakeup);
5585 }
5586 }
5587 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005588}
5589
5590void perf_prepare_sample(struct perf_event_header *header,
5591 struct perf_sample_data *data,
5592 struct perf_event *event,
5593 struct pt_regs *regs)
5594{
5595 u64 sample_type = event->attr.sample_type;
5596
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005597 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005598 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005599
5600 header->misc = 0;
5601 header->misc |= perf_misc_flags(regs);
5602
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005603 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005604
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005605 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005606 data->ip = perf_instruction_pointer(regs);
5607
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005608 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5609 int size = 1;
5610
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005611 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005612
5613 if (data->callchain)
5614 size += data->callchain->nr;
5615
5616 header->size += size * sizeof(u64);
5617 }
5618
5619 if (sample_type & PERF_SAMPLE_RAW) {
5620 int size = sizeof(u32);
5621
5622 if (data->raw)
5623 size += data->raw->size;
5624 else
5625 size += sizeof(u32);
5626
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005627 header->size += round_up(size, sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005628 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005629
5630 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5631 int size = sizeof(u64); /* nr */
5632 if (data->br_stack) {
5633 size += data->br_stack->nr
5634 * sizeof(struct perf_branch_entry);
5635 }
5636 header->size += size;
5637 }
Jiri Olsa40189942012-08-07 15:20:37 +02005638
Peter Zijlstra25657112014-09-24 13:48:42 +02005639 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005640 perf_sample_regs_user(&data->regs_user, regs,
5641 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005642
Jiri Olsa40189942012-08-07 15:20:37 +02005643 if (sample_type & PERF_SAMPLE_REGS_USER) {
5644 /* regs dump ABI info */
5645 int size = sizeof(u64);
5646
Jiri Olsa40189942012-08-07 15:20:37 +02005647 if (data->regs_user.regs) {
5648 u64 mask = event->attr.sample_regs_user;
5649 size += hweight64(mask) * sizeof(u64);
5650 }
5651
5652 header->size += size;
5653 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005654
5655 if (sample_type & PERF_SAMPLE_STACK_USER) {
5656 /*
5657 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5658 * processed as the last one or have additional check added
5659 * in case new sample type is added, because we could eat
5660 * up the rest of the sample size.
5661 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005662 u16 stack_size = event->attr.sample_stack_user;
5663 u16 size = sizeof(u64);
5664
Jiri Olsac5ebced2012-08-07 15:20:40 +02005665 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005666 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005667
5668 /*
5669 * If there is something to dump, add space for the dump
5670 * itself and for the field that tells the dynamic size,
5671 * which is how many have been actually dumped.
5672 */
5673 if (stack_size)
5674 size += sizeof(u64) + stack_size;
5675
5676 data->stack_user_size = stack_size;
5677 header->size += size;
5678 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005679
5680 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5681 /* regs dump ABI info */
5682 int size = sizeof(u64);
5683
5684 perf_sample_regs_intr(&data->regs_intr, regs);
5685
5686 if (data->regs_intr.regs) {
5687 u64 mask = event->attr.sample_regs_intr;
5688
5689 size += hweight64(mask) * sizeof(u64);
5690 }
5691
5692 header->size += size;
5693 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005694}
5695
Yan, Zheng21509082015-05-06 15:33:49 -04005696void perf_event_output(struct perf_event *event,
5697 struct perf_sample_data *data,
5698 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005699{
5700 struct perf_output_handle handle;
5701 struct perf_event_header header;
5702
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005703 /* protect the callchain buffers */
5704 rcu_read_lock();
5705
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005706 perf_prepare_sample(&header, data, event, regs);
5707
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005708 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005709 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005710
5711 perf_output_sample(&handle, &header, data, event);
5712
5713 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005714
5715exit:
5716 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005717}
5718
5719/*
5720 * read event_id
5721 */
5722
5723struct perf_read_event {
5724 struct perf_event_header header;
5725
5726 u32 pid;
5727 u32 tid;
5728};
5729
5730static void
5731perf_event_read_event(struct perf_event *event,
5732 struct task_struct *task)
5733{
5734 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005735 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005736 struct perf_read_event read_event = {
5737 .header = {
5738 .type = PERF_RECORD_READ,
5739 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005740 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005741 },
5742 .pid = perf_event_pid(event, task),
5743 .tid = perf_event_tid(event, task),
5744 };
5745 int ret;
5746
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005747 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005748 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005749 if (ret)
5750 return;
5751
5752 perf_output_put(&handle, read_event);
5753 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005754 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005755
5756 perf_output_end(&handle);
5757}
5758
Jiri Olsa52d857a2013-05-06 18:27:18 +02005759typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5760
5761static void
5762perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005763 perf_event_aux_output_cb output,
5764 void *data)
5765{
5766 struct perf_event *event;
5767
5768 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5769 if (event->state < PERF_EVENT_STATE_INACTIVE)
5770 continue;
5771 if (!event_filter_match(event))
5772 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02005773 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005774 }
5775}
5776
5777static void
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005778perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data,
5779 struct perf_event_context *task_ctx)
5780{
5781 rcu_read_lock();
5782 preempt_disable();
5783 perf_event_aux_ctx(task_ctx, output, data);
5784 preempt_enable();
5785 rcu_read_unlock();
5786}
5787
5788static void
Jiri Olsa67516842013-07-09 18:56:31 +02005789perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005790 struct perf_event_context *task_ctx)
5791{
5792 struct perf_cpu_context *cpuctx;
5793 struct perf_event_context *ctx;
5794 struct pmu *pmu;
5795 int ctxn;
5796
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005797 /*
5798 * If we have task_ctx != NULL we only notify
5799 * the task context itself. The task_ctx is set
5800 * only for EXIT events before releasing task
5801 * context.
5802 */
5803 if (task_ctx) {
5804 perf_event_aux_task_ctx(output, data, task_ctx);
5805 return;
5806 }
5807
Jiri Olsa52d857a2013-05-06 18:27:18 +02005808 rcu_read_lock();
5809 list_for_each_entry_rcu(pmu, &pmus, entry) {
5810 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5811 if (cpuctx->unique_pmu != pmu)
5812 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02005813 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005814 ctxn = pmu->task_ctx_nr;
5815 if (ctxn < 0)
5816 goto next;
5817 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5818 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02005819 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005820next:
5821 put_cpu_ptr(pmu->pmu_cpu_context);
5822 }
Jiri Olsa52d857a2013-05-06 18:27:18 +02005823 rcu_read_unlock();
5824}
5825
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005826struct remote_output {
5827 struct ring_buffer *rb;
5828 int err;
5829};
5830
5831static void __perf_event_output_stop(struct perf_event *event, void *data)
5832{
5833 struct perf_event *parent = event->parent;
5834 struct remote_output *ro = data;
5835 struct ring_buffer *rb = ro->rb;
5836
5837 if (!has_aux(event))
5838 return;
5839
5840 if (!parent)
5841 parent = event;
5842
5843 /*
5844 * In case of inheritance, it will be the parent that links to the
5845 * ring-buffer, but it will be the child that's actually using it:
5846 */
5847 if (rcu_dereference(parent->rb) == rb)
5848 ro->err = __perf_event_stop(event);
5849}
5850
5851static int __perf_pmu_output_stop(void *info)
5852{
5853 struct perf_event *event = info;
5854 struct pmu *pmu = event->pmu;
5855 struct perf_cpu_context *cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5856 struct remote_output ro = {
5857 .rb = event->rb,
5858 };
5859
5860 rcu_read_lock();
5861 perf_event_aux_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro);
5862 if (cpuctx->task_ctx)
5863 perf_event_aux_ctx(cpuctx->task_ctx, __perf_event_output_stop,
5864 &ro);
5865 rcu_read_unlock();
5866
5867 return ro.err;
5868}
5869
5870static void perf_pmu_output_stop(struct perf_event *event)
5871{
5872 struct perf_event *iter;
5873 int err, cpu;
5874
5875restart:
5876 rcu_read_lock();
5877 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
5878 /*
5879 * For per-CPU events, we need to make sure that neither they
5880 * nor their children are running; for cpu==-1 events it's
5881 * sufficient to stop the event itself if it's active, since
5882 * it can't have children.
5883 */
5884 cpu = iter->cpu;
5885 if (cpu == -1)
5886 cpu = READ_ONCE(iter->oncpu);
5887
5888 if (cpu == -1)
5889 continue;
5890
5891 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
5892 if (err == -EAGAIN) {
5893 rcu_read_unlock();
5894 goto restart;
5895 }
5896 }
5897 rcu_read_unlock();
5898}
5899
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005900/*
5901 * task tracking -- fork/exit
5902 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02005903 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005904 */
5905
5906struct perf_task_event {
5907 struct task_struct *task;
5908 struct perf_event_context *task_ctx;
5909
5910 struct {
5911 struct perf_event_header header;
5912
5913 u32 pid;
5914 u32 ppid;
5915 u32 tid;
5916 u32 ptid;
5917 u64 time;
5918 } event_id;
5919};
5920
Jiri Olsa67516842013-07-09 18:56:31 +02005921static int perf_event_task_match(struct perf_event *event)
5922{
Stephane Eranian13d7a242013-08-21 12:10:24 +02005923 return event->attr.comm || event->attr.mmap ||
5924 event->attr.mmap2 || event->attr.mmap_data ||
5925 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02005926}
5927
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005928static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005929 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005930{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005931 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005932 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005933 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005934 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005935 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01005936
Jiri Olsa67516842013-07-09 18:56:31 +02005937 if (!perf_event_task_match(event))
5938 return;
5939
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005940 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005941
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005942 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005943 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02005944 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005945 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005946
5947 task_event->event_id.pid = perf_event_pid(event, task);
5948 task_event->event_id.ppid = perf_event_pid(event, current);
5949
5950 task_event->event_id.tid = perf_event_tid(event, task);
5951 task_event->event_id.ptid = perf_event_tid(event, current);
5952
Peter Zijlstra34f43922015-02-20 14:05:38 +01005953 task_event->event_id.time = perf_event_clock(event);
5954
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005955 perf_output_put(&handle, task_event->event_id);
5956
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005957 perf_event__output_id_sample(event, &handle, &sample);
5958
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005959 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005960out:
5961 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005962}
5963
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005964static void perf_event_task(struct task_struct *task,
5965 struct perf_event_context *task_ctx,
5966 int new)
5967{
5968 struct perf_task_event task_event;
5969
5970 if (!atomic_read(&nr_comm_events) &&
5971 !atomic_read(&nr_mmap_events) &&
5972 !atomic_read(&nr_task_events))
5973 return;
5974
5975 task_event = (struct perf_task_event){
5976 .task = task,
5977 .task_ctx = task_ctx,
5978 .event_id = {
5979 .header = {
5980 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5981 .misc = 0,
5982 .size = sizeof(task_event.event_id),
5983 },
5984 /* .pid */
5985 /* .ppid */
5986 /* .tid */
5987 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01005988 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005989 },
5990 };
5991
Jiri Olsa67516842013-07-09 18:56:31 +02005992 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005993 &task_event,
5994 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005995}
5996
5997void perf_event_fork(struct task_struct *task)
5998{
5999 perf_event_task(task, NULL, 1);
6000}
6001
6002/*
6003 * comm tracking
6004 */
6005
6006struct perf_comm_event {
6007 struct task_struct *task;
6008 char *comm;
6009 int comm_size;
6010
6011 struct {
6012 struct perf_event_header header;
6013
6014 u32 pid;
6015 u32 tid;
6016 } event_id;
6017};
6018
Jiri Olsa67516842013-07-09 18:56:31 +02006019static int perf_event_comm_match(struct perf_event *event)
6020{
6021 return event->attr.comm;
6022}
6023
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006024static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006025 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006026{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006027 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006028 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006029 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006030 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006031 int ret;
6032
Jiri Olsa67516842013-07-09 18:56:31 +02006033 if (!perf_event_comm_match(event))
6034 return;
6035
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006036 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
6037 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006038 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006039
6040 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006041 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006042
6043 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
6044 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
6045
6046 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02006047 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006048 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006049
6050 perf_event__output_id_sample(event, &handle, &sample);
6051
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006052 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006053out:
6054 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006055}
6056
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006057static void perf_event_comm_event(struct perf_comm_event *comm_event)
6058{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006059 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006060 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006061
6062 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01006063 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006064 size = ALIGN(strlen(comm)+1, sizeof(u64));
6065
6066 comm_event->comm = comm;
6067 comm_event->comm_size = size;
6068
6069 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006070
Jiri Olsa67516842013-07-09 18:56:31 +02006071 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006072 comm_event,
6073 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006074}
6075
Adrian Hunter82b89772014-05-28 11:45:04 +03006076void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006077{
6078 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006079
6080 if (!atomic_read(&nr_comm_events))
6081 return;
6082
6083 comm_event = (struct perf_comm_event){
6084 .task = task,
6085 /* .comm */
6086 /* .comm_size */
6087 .event_id = {
6088 .header = {
6089 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03006090 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006091 /* .size */
6092 },
6093 /* .pid */
6094 /* .tid */
6095 },
6096 };
6097
6098 perf_event_comm_event(&comm_event);
6099}
6100
6101/*
6102 * mmap tracking
6103 */
6104
6105struct perf_mmap_event {
6106 struct vm_area_struct *vma;
6107
6108 const char *file_name;
6109 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006110 int maj, min;
6111 u64 ino;
6112 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006113 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006114
6115 struct {
6116 struct perf_event_header header;
6117
6118 u32 pid;
6119 u32 tid;
6120 u64 start;
6121 u64 len;
6122 u64 pgoff;
6123 } event_id;
6124};
6125
Jiri Olsa67516842013-07-09 18:56:31 +02006126static int perf_event_mmap_match(struct perf_event *event,
6127 void *data)
6128{
6129 struct perf_mmap_event *mmap_event = data;
6130 struct vm_area_struct *vma = mmap_event->vma;
6131 int executable = vma->vm_flags & VM_EXEC;
6132
6133 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02006134 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02006135}
6136
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006137static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006138 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006139{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006140 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006141 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006142 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006143 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006144 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006145
Jiri Olsa67516842013-07-09 18:56:31 +02006146 if (!perf_event_mmap_match(event, data))
6147 return;
6148
Stephane Eranian13d7a242013-08-21 12:10:24 +02006149 if (event->attr.mmap2) {
6150 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
6151 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
6152 mmap_event->event_id.header.size += sizeof(mmap_event->min);
6153 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03006154 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006155 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
6156 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006157 }
6158
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006159 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
6160 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006161 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006162 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006163 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006164
6165 mmap_event->event_id.pid = perf_event_pid(event, current);
6166 mmap_event->event_id.tid = perf_event_tid(event, current);
6167
6168 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006169
6170 if (event->attr.mmap2) {
6171 perf_output_put(&handle, mmap_event->maj);
6172 perf_output_put(&handle, mmap_event->min);
6173 perf_output_put(&handle, mmap_event->ino);
6174 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006175 perf_output_put(&handle, mmap_event->prot);
6176 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006177 }
6178
Frederic Weisbecker76369132011-05-19 19:55:04 +02006179 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006180 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006181
6182 perf_event__output_id_sample(event, &handle, &sample);
6183
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006184 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006185out:
6186 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006187}
6188
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006189static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
6190{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006191 struct vm_area_struct *vma = mmap_event->vma;
6192 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006193 int maj = 0, min = 0;
6194 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006195 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006196 unsigned int size;
6197 char tmp[16];
6198 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006199 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006200
6201 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02006202 struct inode *inode;
6203 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006204
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006205 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006206 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006207 name = "//enomem";
6208 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006209 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006210 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006211 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006212 * need to add enough zero bytes after the string to handle
6213 * the 64bit alignment we do later.
6214 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02006215 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006216 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006217 name = "//toolong";
6218 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006219 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02006220 inode = file_inode(vma->vm_file);
6221 dev = inode->i_sb->s_dev;
6222 ino = inode->i_ino;
6223 gen = inode->i_generation;
6224 maj = MAJOR(dev);
6225 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006226
6227 if (vma->vm_flags & VM_READ)
6228 prot |= PROT_READ;
6229 if (vma->vm_flags & VM_WRITE)
6230 prot |= PROT_WRITE;
6231 if (vma->vm_flags & VM_EXEC)
6232 prot |= PROT_EXEC;
6233
6234 if (vma->vm_flags & VM_MAYSHARE)
6235 flags = MAP_SHARED;
6236 else
6237 flags = MAP_PRIVATE;
6238
6239 if (vma->vm_flags & VM_DENYWRITE)
6240 flags |= MAP_DENYWRITE;
6241 if (vma->vm_flags & VM_MAYEXEC)
6242 flags |= MAP_EXECUTABLE;
6243 if (vma->vm_flags & VM_LOCKED)
6244 flags |= MAP_LOCKED;
6245 if (vma->vm_flags & VM_HUGETLB)
6246 flags |= MAP_HUGETLB;
6247
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006248 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006249 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006250 if (vma->vm_ops && vma->vm_ops->name) {
6251 name = (char *) vma->vm_ops->name(vma);
6252 if (name)
6253 goto cpy_name;
6254 }
6255
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006256 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006257 if (name)
6258 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006259
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006260 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006261 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006262 name = "[heap]";
6263 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006264 }
6265 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006266 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006267 name = "[stack]";
6268 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006269 }
6270
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006271 name = "//anon";
6272 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006273 }
6274
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006275cpy_name:
6276 strlcpy(tmp, name, sizeof(tmp));
6277 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006278got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006279 /*
6280 * Since our buffer works in 8 byte units we need to align our string
6281 * size to a multiple of 8. However, we must guarantee the tail end is
6282 * zero'd out to avoid leaking random bits to userspace.
6283 */
6284 size = strlen(name)+1;
6285 while (!IS_ALIGNED(size, sizeof(u64)))
6286 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006287
6288 mmap_event->file_name = name;
6289 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006290 mmap_event->maj = maj;
6291 mmap_event->min = min;
6292 mmap_event->ino = ino;
6293 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006294 mmap_event->prot = prot;
6295 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006296
Stephane Eranian2fe85422013-01-24 16:10:39 +01006297 if (!(vma->vm_flags & VM_EXEC))
6298 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6299
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006300 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6301
Jiri Olsa67516842013-07-09 18:56:31 +02006302 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006303 mmap_event,
6304 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006305
6306 kfree(buf);
6307}
6308
Eric B Munson3af9e852010-05-18 15:30:49 +01006309void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006310{
6311 struct perf_mmap_event mmap_event;
6312
6313 if (!atomic_read(&nr_mmap_events))
6314 return;
6315
6316 mmap_event = (struct perf_mmap_event){
6317 .vma = vma,
6318 /* .file_name */
6319 /* .file_size */
6320 .event_id = {
6321 .header = {
6322 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006323 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006324 /* .size */
6325 },
6326 /* .pid */
6327 /* .tid */
6328 .start = vma->vm_start,
6329 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006330 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006331 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006332 /* .maj (attr_mmap2 only) */
6333 /* .min (attr_mmap2 only) */
6334 /* .ino (attr_mmap2 only) */
6335 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006336 /* .prot (attr_mmap2 only) */
6337 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006338 };
6339
6340 perf_event_mmap_event(&mmap_event);
6341}
6342
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006343void perf_event_aux_event(struct perf_event *event, unsigned long head,
6344 unsigned long size, u64 flags)
6345{
6346 struct perf_output_handle handle;
6347 struct perf_sample_data sample;
6348 struct perf_aux_event {
6349 struct perf_event_header header;
6350 u64 offset;
6351 u64 size;
6352 u64 flags;
6353 } rec = {
6354 .header = {
6355 .type = PERF_RECORD_AUX,
6356 .misc = 0,
6357 .size = sizeof(rec),
6358 },
6359 .offset = head,
6360 .size = size,
6361 .flags = flags,
6362 };
6363 int ret;
6364
6365 perf_event_header__init_id(&rec.header, &sample, event);
6366 ret = perf_output_begin(&handle, event, rec.header.size);
6367
6368 if (ret)
6369 return;
6370
6371 perf_output_put(&handle, rec);
6372 perf_event__output_id_sample(event, &handle, &sample);
6373
6374 perf_output_end(&handle);
6375}
6376
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006377/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006378 * Lost/dropped samples logging
6379 */
6380void perf_log_lost_samples(struct perf_event *event, u64 lost)
6381{
6382 struct perf_output_handle handle;
6383 struct perf_sample_data sample;
6384 int ret;
6385
6386 struct {
6387 struct perf_event_header header;
6388 u64 lost;
6389 } lost_samples_event = {
6390 .header = {
6391 .type = PERF_RECORD_LOST_SAMPLES,
6392 .misc = 0,
6393 .size = sizeof(lost_samples_event),
6394 },
6395 .lost = lost,
6396 };
6397
6398 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6399
6400 ret = perf_output_begin(&handle, event,
6401 lost_samples_event.header.size);
6402 if (ret)
6403 return;
6404
6405 perf_output_put(&handle, lost_samples_event);
6406 perf_event__output_id_sample(event, &handle, &sample);
6407 perf_output_end(&handle);
6408}
6409
6410/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006411 * context_switch tracking
6412 */
6413
6414struct perf_switch_event {
6415 struct task_struct *task;
6416 struct task_struct *next_prev;
6417
6418 struct {
6419 struct perf_event_header header;
6420 u32 next_prev_pid;
6421 u32 next_prev_tid;
6422 } event_id;
6423};
6424
6425static int perf_event_switch_match(struct perf_event *event)
6426{
6427 return event->attr.context_switch;
6428}
6429
6430static void perf_event_switch_output(struct perf_event *event, void *data)
6431{
6432 struct perf_switch_event *se = data;
6433 struct perf_output_handle handle;
6434 struct perf_sample_data sample;
6435 int ret;
6436
6437 if (!perf_event_switch_match(event))
6438 return;
6439
6440 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6441 if (event->ctx->task) {
6442 se->event_id.header.type = PERF_RECORD_SWITCH;
6443 se->event_id.header.size = sizeof(se->event_id.header);
6444 } else {
6445 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6446 se->event_id.header.size = sizeof(se->event_id);
6447 se->event_id.next_prev_pid =
6448 perf_event_pid(event, se->next_prev);
6449 se->event_id.next_prev_tid =
6450 perf_event_tid(event, se->next_prev);
6451 }
6452
6453 perf_event_header__init_id(&se->event_id.header, &sample, event);
6454
6455 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6456 if (ret)
6457 return;
6458
6459 if (event->ctx->task)
6460 perf_output_put(&handle, se->event_id.header);
6461 else
6462 perf_output_put(&handle, se->event_id);
6463
6464 perf_event__output_id_sample(event, &handle, &sample);
6465
6466 perf_output_end(&handle);
6467}
6468
6469static void perf_event_switch(struct task_struct *task,
6470 struct task_struct *next_prev, bool sched_in)
6471{
6472 struct perf_switch_event switch_event;
6473
6474 /* N.B. caller checks nr_switch_events != 0 */
6475
6476 switch_event = (struct perf_switch_event){
6477 .task = task,
6478 .next_prev = next_prev,
6479 .event_id = {
6480 .header = {
6481 /* .type */
6482 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
6483 /* .size */
6484 },
6485 /* .next_prev_pid */
6486 /* .next_prev_tid */
6487 },
6488 };
6489
6490 perf_event_aux(perf_event_switch_output,
6491 &switch_event,
6492 NULL);
6493}
6494
6495/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006496 * IRQ throttle logging
6497 */
6498
6499static void perf_log_throttle(struct perf_event *event, int enable)
6500{
6501 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006502 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006503 int ret;
6504
6505 struct {
6506 struct perf_event_header header;
6507 u64 time;
6508 u64 id;
6509 u64 stream_id;
6510 } throttle_event = {
6511 .header = {
6512 .type = PERF_RECORD_THROTTLE,
6513 .misc = 0,
6514 .size = sizeof(throttle_event),
6515 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01006516 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006517 .id = primary_event_id(event),
6518 .stream_id = event->id,
6519 };
6520
6521 if (enable)
6522 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
6523
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006524 perf_event_header__init_id(&throttle_event.header, &sample, event);
6525
6526 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006527 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006528 if (ret)
6529 return;
6530
6531 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006532 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006533 perf_output_end(&handle);
6534}
6535
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006536static void perf_log_itrace_start(struct perf_event *event)
6537{
6538 struct perf_output_handle handle;
6539 struct perf_sample_data sample;
6540 struct perf_aux_event {
6541 struct perf_event_header header;
6542 u32 pid;
6543 u32 tid;
6544 } rec;
6545 int ret;
6546
6547 if (event->parent)
6548 event = event->parent;
6549
6550 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
6551 event->hw.itrace_started)
6552 return;
6553
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006554 rec.header.type = PERF_RECORD_ITRACE_START;
6555 rec.header.misc = 0;
6556 rec.header.size = sizeof(rec);
6557 rec.pid = perf_event_pid(event, current);
6558 rec.tid = perf_event_tid(event, current);
6559
6560 perf_event_header__init_id(&rec.header, &sample, event);
6561 ret = perf_output_begin(&handle, event, rec.header.size);
6562
6563 if (ret)
6564 return;
6565
6566 perf_output_put(&handle, rec);
6567 perf_event__output_id_sample(event, &handle, &sample);
6568
6569 perf_output_end(&handle);
6570}
6571
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006572/*
6573 * Generic event overflow handling, sampling.
6574 */
6575
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006576static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006577 int throttle, struct perf_sample_data *data,
6578 struct pt_regs *regs)
6579{
6580 int events = atomic_read(&event->event_limit);
6581 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006582 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006583 int ret = 0;
6584
Peter Zijlstra96398822010-11-24 18:55:29 +01006585 /*
6586 * Non-sampling counters might still use the PMI to fold short
6587 * hardware counters, ignore those.
6588 */
6589 if (unlikely(!is_sampling_event(event)))
6590 return 0;
6591
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006592 seq = __this_cpu_read(perf_throttled_seq);
6593 if (seq != hwc->interrupts_seq) {
6594 hwc->interrupts_seq = seq;
6595 hwc->interrupts = 1;
6596 } else {
6597 hwc->interrupts++;
6598 if (unlikely(throttle
6599 && hwc->interrupts >= max_samples_per_tick)) {
6600 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02006601 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01006602 hwc->interrupts = MAX_INTERRUPTS;
6603 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006604 ret = 1;
6605 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006606 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006607
6608 if (event->attr.freq) {
6609 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01006610 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006611
Peter Zijlstraabd50712010-01-26 18:50:16 +01006612 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006613
Peter Zijlstraabd50712010-01-26 18:50:16 +01006614 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01006615 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006616 }
6617
6618 /*
6619 * XXX event_limit might not quite work as expected on inherited
6620 * events
6621 */
6622
6623 event->pending_kill = POLL_IN;
6624 if (events && atomic_dec_and_test(&event->event_limit)) {
6625 ret = 1;
6626 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006627 event->pending_disable = 1;
6628 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006629 }
6630
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006631 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006632 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006633 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006634 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006635
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006636 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006637 event->pending_wakeup = 1;
6638 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02006639 }
6640
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006641 return ret;
6642}
6643
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006644int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006645 struct perf_sample_data *data,
6646 struct pt_regs *regs)
6647{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006648 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006649}
6650
6651/*
6652 * Generic software event infrastructure
6653 */
6654
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006655struct swevent_htable {
6656 struct swevent_hlist *swevent_hlist;
6657 struct mutex hlist_mutex;
6658 int hlist_refcount;
6659
6660 /* Recursion avoidance in each contexts */
6661 int recursion[PERF_NR_CONTEXTS];
6662};
6663
6664static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
6665
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006666/*
6667 * We directly increment event->count and keep a second value in
6668 * event->hw.period_left to count intervals. This period event
6669 * is kept in the range [-sample_period, 0] so that we can use the
6670 * sign as trigger.
6671 */
6672
Jiri Olsaab573842013-05-01 17:25:44 +02006673u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006674{
6675 struct hw_perf_event *hwc = &event->hw;
6676 u64 period = hwc->last_period;
6677 u64 nr, offset;
6678 s64 old, val;
6679
6680 hwc->last_period = hwc->sample_period;
6681
6682again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02006683 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006684 if (val < 0)
6685 return 0;
6686
6687 nr = div64_u64(period + val, period);
6688 offset = nr * period;
6689 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02006690 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006691 goto again;
6692
6693 return nr;
6694}
6695
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006696static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006697 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006698 struct pt_regs *regs)
6699{
6700 struct hw_perf_event *hwc = &event->hw;
6701 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006702
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006703 if (!overflow)
6704 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006705
6706 if (hwc->interrupts == MAX_INTERRUPTS)
6707 return;
6708
6709 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006710 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006711 data, regs)) {
6712 /*
6713 * We inhibit the overflow from happening when
6714 * hwc->interrupts == MAX_INTERRUPTS.
6715 */
6716 break;
6717 }
6718 throttle = 1;
6719 }
6720}
6721
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006722static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006723 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006724 struct pt_regs *regs)
6725{
6726 struct hw_perf_event *hwc = &event->hw;
6727
Peter Zijlstrae7850592010-05-21 14:43:08 +02006728 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006729
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006730 if (!regs)
6731 return;
6732
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006733 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006734 return;
6735
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03006736 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
6737 data->period = nr;
6738 return perf_swevent_overflow(event, 1, data, regs);
6739 } else
6740 data->period = event->hw.last_period;
6741
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006742 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006743 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006744
Peter Zijlstrae7850592010-05-21 14:43:08 +02006745 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006746 return;
6747
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006748 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006749}
6750
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006751static int perf_exclude_event(struct perf_event *event,
6752 struct pt_regs *regs)
6753{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006754 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01006755 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006756
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006757 if (regs) {
6758 if (event->attr.exclude_user && user_mode(regs))
6759 return 1;
6760
6761 if (event->attr.exclude_kernel && !user_mode(regs))
6762 return 1;
6763 }
6764
6765 return 0;
6766}
6767
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006768static int perf_swevent_match(struct perf_event *event,
6769 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08006770 u32 event_id,
6771 struct perf_sample_data *data,
6772 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006773{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006774 if (event->attr.type != type)
6775 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006776
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006777 if (event->attr.config != event_id)
6778 return 0;
6779
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006780 if (perf_exclude_event(event, regs))
6781 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006782
6783 return 1;
6784}
6785
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006786static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006787{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006788 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006789
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006790 return hash_64(val, SWEVENT_HLIST_BITS);
6791}
6792
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006793static inline struct hlist_head *
6794__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006795{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006796 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006797
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006798 return &hlist->heads[hash];
6799}
6800
6801/* For the read side: events when they trigger */
6802static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006803find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006804{
6805 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006806
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006807 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006808 if (!hlist)
6809 return NULL;
6810
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006811 return __find_swevent_head(hlist, type, event_id);
6812}
6813
6814/* For the event head insertion and removal in the hlist */
6815static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006816find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006817{
6818 struct swevent_hlist *hlist;
6819 u32 event_id = event->attr.config;
6820 u64 type = event->attr.type;
6821
6822 /*
6823 * Event scheduling is always serialized against hlist allocation
6824 * and release. Which makes the protected version suitable here.
6825 * The context lock guarantees that.
6826 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006827 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006828 lockdep_is_held(&event->ctx->lock));
6829 if (!hlist)
6830 return NULL;
6831
6832 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006833}
6834
6835static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006836 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006837 struct perf_sample_data *data,
6838 struct pt_regs *regs)
6839{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006840 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006841 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006842 struct hlist_head *head;
6843
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006844 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006845 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006846 if (!head)
6847 goto end;
6848
Sasha Levinb67bfe02013-02-27 17:06:00 -08006849 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08006850 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006851 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006852 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006853end:
6854 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006855}
6856
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006857DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
6858
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006859int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006860{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006861 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006862
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006863 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006864}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01006865EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006866
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01006867inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006868{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006869 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006870
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006871 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006872}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006873
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006874void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006875{
Ingo Molnara4234bf2009-11-23 10:57:59 +01006876 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006877
6878 if (WARN_ON_ONCE(!regs))
6879 return;
6880
6881 perf_sample_data_init(&data, addr, 0);
6882 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
6883}
6884
6885void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
6886{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006887 int rctx;
6888
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006889 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006890 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006891 if (unlikely(rctx < 0))
6892 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006893
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006894 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006895
6896 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006897fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006898 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006899}
6900
6901static void perf_swevent_read(struct perf_event *event)
6902{
6903}
6904
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006905static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006906{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006907 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006908 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006909 struct hlist_head *head;
6910
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006911 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006912 hwc->last_period = hwc->sample_period;
6913 perf_swevent_set_period(event);
6914 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006915
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006916 hwc->state = !(flags & PERF_EF_START);
6917
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006918 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01006919 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006920 return -EINVAL;
6921
6922 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08006923 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006924
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006925 return 0;
6926}
6927
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006928static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006929{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006930 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006931}
6932
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006933static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006934{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006935 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006936}
6937
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006938static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006939{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006940 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006941}
6942
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006943/* Deref the hlist from the update side */
6944static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006945swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006946{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006947 return rcu_dereference_protected(swhash->swevent_hlist,
6948 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006949}
6950
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006951static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006952{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006953 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006954
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006955 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006956 return;
6957
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03006958 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08006959 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006960}
6961
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006962static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006963{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006964 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006965
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006966 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006967
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006968 if (!--swhash->hlist_refcount)
6969 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006970
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006971 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006972}
6973
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006974static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006975{
6976 int cpu;
6977
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006978 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006979 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006980}
6981
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006982static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006983{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006984 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006985 int err = 0;
6986
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006987 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006988 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006989 struct swevent_hlist *hlist;
6990
6991 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
6992 if (!hlist) {
6993 err = -ENOMEM;
6994 goto exit;
6995 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006996 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006997 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006998 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006999exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007000 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007001
7002 return err;
7003}
7004
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007005static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007006{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007007 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007008
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007009 get_online_cpus();
7010 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007011 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007012 if (err) {
7013 failed_cpu = cpu;
7014 goto fail;
7015 }
7016 }
7017 put_online_cpus();
7018
7019 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007020fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007021 for_each_possible_cpu(cpu) {
7022 if (cpu == failed_cpu)
7023 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007024 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007025 }
7026
7027 put_online_cpus();
7028 return err;
7029}
7030
Ingo Molnarc5905af2012-02-24 08:31:31 +01007031struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007032
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007033static void sw_perf_event_destroy(struct perf_event *event)
7034{
7035 u64 event_id = event->attr.config;
7036
7037 WARN_ON(event->parent);
7038
Ingo Molnarc5905af2012-02-24 08:31:31 +01007039 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007040 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007041}
7042
7043static int perf_swevent_init(struct perf_event *event)
7044{
Tommi Rantala8176cce2013-04-13 22:49:14 +03007045 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007046
7047 if (event->attr.type != PERF_TYPE_SOFTWARE)
7048 return -ENOENT;
7049
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007050 /*
7051 * no branch sampling for software events
7052 */
7053 if (has_branch_stack(event))
7054 return -EOPNOTSUPP;
7055
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007056 switch (event_id) {
7057 case PERF_COUNT_SW_CPU_CLOCK:
7058 case PERF_COUNT_SW_TASK_CLOCK:
7059 return -ENOENT;
7060
7061 default:
7062 break;
7063 }
7064
Dan Carpenterce677832010-10-24 21:50:42 +02007065 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007066 return -ENOENT;
7067
7068 if (!event->parent) {
7069 int err;
7070
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007071 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007072 if (err)
7073 return err;
7074
Ingo Molnarc5905af2012-02-24 08:31:31 +01007075 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007076 event->destroy = sw_perf_event_destroy;
7077 }
7078
7079 return 0;
7080}
7081
7082static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007083 .task_ctx_nr = perf_sw_context,
7084
Peter Zijlstra34f43922015-02-20 14:05:38 +01007085 .capabilities = PERF_PMU_CAP_NO_NMI,
7086
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007087 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007088 .add = perf_swevent_add,
7089 .del = perf_swevent_del,
7090 .start = perf_swevent_start,
7091 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007092 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007093};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007094
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007095#ifdef CONFIG_EVENT_TRACING
7096
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007097static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007098 struct perf_sample_data *data)
7099{
7100 void *record = data->raw->data;
7101
Peter Zijlstrab71b4372015-11-02 10:50:51 +01007102 /* only top level events have filters set */
7103 if (event->parent)
7104 event = event->parent;
7105
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007106 if (likely(!event->filter) || filter_match_preds(event->filter, record))
7107 return 1;
7108 return 0;
7109}
7110
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007111static int perf_tp_event_match(struct perf_event *event,
7112 struct perf_sample_data *data,
7113 struct pt_regs *regs)
7114{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01007115 if (event->hw.state & PERF_HES_STOPPED)
7116 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02007117 /*
7118 * All tracepoints are from kernel-space.
7119 */
7120 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007121 return 0;
7122
7123 if (!perf_tp_filter_match(event, data))
7124 return 0;
7125
7126 return 1;
7127}
7128
7129void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007130 struct pt_regs *regs, struct hlist_head *head, int rctx,
7131 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007132{
7133 struct perf_sample_data data;
7134 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007135
7136 struct perf_raw_record raw = {
7137 .size = entry_size,
7138 .data = record,
7139 };
7140
Robert Richterfd0d0002012-04-02 20:19:08 +02007141 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007142 data.raw = &raw;
7143
Sasha Levinb67bfe02013-02-27 17:06:00 -08007144 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007145 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007146 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007147 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007148
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007149 /*
7150 * If we got specified a target task, also iterate its context and
7151 * deliver this event there too.
7152 */
7153 if (task && task != current) {
7154 struct perf_event_context *ctx;
7155 struct trace_entry *entry = record;
7156
7157 rcu_read_lock();
7158 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
7159 if (!ctx)
7160 goto unlock;
7161
7162 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
7163 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7164 continue;
7165 if (event->attr.config != entry->type)
7166 continue;
7167 if (perf_tp_event_match(event, &data, regs))
7168 perf_swevent_event(event, count, &data, regs);
7169 }
7170unlock:
7171 rcu_read_unlock();
7172 }
7173
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007174 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007175}
7176EXPORT_SYMBOL_GPL(perf_tp_event);
7177
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007178static void tp_perf_event_destroy(struct perf_event *event)
7179{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007180 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007181}
7182
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007183static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007184{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007185 int err;
7186
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007187 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7188 return -ENOENT;
7189
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007190 /*
7191 * no branch sampling for tracepoint events
7192 */
7193 if (has_branch_stack(event))
7194 return -EOPNOTSUPP;
7195
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007196 err = perf_trace_init(event);
7197 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007198 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007199
7200 event->destroy = tp_perf_event_destroy;
7201
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007202 return 0;
7203}
7204
7205static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007206 .task_ctx_nr = perf_sw_context,
7207
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007208 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007209 .add = perf_trace_add,
7210 .del = perf_trace_del,
7211 .start = perf_swevent_start,
7212 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007213 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007214};
7215
7216static inline void perf_tp_register(void)
7217{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007218 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007219}
Li Zefan6fb29152009-10-15 11:21:42 +08007220
7221static int perf_event_set_filter(struct perf_event *event, void __user *arg)
7222{
7223 char *filter_str;
7224 int ret;
7225
7226 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7227 return -EINVAL;
7228
7229 filter_str = strndup_user(arg, PAGE_SIZE);
7230 if (IS_ERR(filter_str))
7231 return PTR_ERR(filter_str);
7232
7233 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
7234
7235 kfree(filter_str);
7236 return ret;
7237}
7238
7239static void perf_event_free_filter(struct perf_event *event)
7240{
7241 ftrace_profile_free_filter(event);
7242}
7243
Alexei Starovoitov25415172015-03-25 12:49:20 -07007244static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7245{
7246 struct bpf_prog *prog;
7247
7248 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7249 return -EINVAL;
7250
7251 if (event->tp_event->prog)
7252 return -EEXIST;
7253
Wang Nan04a22fa2015-07-01 02:13:50 +00007254 if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE))
7255 /* bpf programs can only be attached to u/kprobes */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007256 return -EINVAL;
7257
7258 prog = bpf_prog_get(prog_fd);
7259 if (IS_ERR(prog))
7260 return PTR_ERR(prog);
7261
Linus Torvalds6c373ca2015-04-15 09:00:47 -07007262 if (prog->type != BPF_PROG_TYPE_KPROBE) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007263 /* valid fd, but invalid bpf program type */
7264 bpf_prog_put(prog);
7265 return -EINVAL;
7266 }
7267
7268 event->tp_event->prog = prog;
7269
7270 return 0;
7271}
7272
7273static void perf_event_free_bpf_prog(struct perf_event *event)
7274{
7275 struct bpf_prog *prog;
7276
7277 if (!event->tp_event)
7278 return;
7279
7280 prog = event->tp_event->prog;
7281 if (prog) {
7282 event->tp_event->prog = NULL;
7283 bpf_prog_put(prog);
7284 }
7285}
7286
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007287#else
Li Zefan6fb29152009-10-15 11:21:42 +08007288
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007289static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007290{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007291}
Li Zefan6fb29152009-10-15 11:21:42 +08007292
7293static int perf_event_set_filter(struct perf_event *event, void __user *arg)
7294{
7295 return -ENOENT;
7296}
7297
7298static void perf_event_free_filter(struct perf_event *event)
7299{
7300}
7301
Alexei Starovoitov25415172015-03-25 12:49:20 -07007302static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7303{
7304 return -ENOENT;
7305}
7306
7307static void perf_event_free_bpf_prog(struct perf_event *event)
7308{
7309}
Li Zefan07b139c2009-12-21 14:27:35 +08007310#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007311
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007312#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007313void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007314{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007315 struct perf_sample_data sample;
7316 struct pt_regs *regs = data;
7317
Robert Richterfd0d0002012-04-02 20:19:08 +02007318 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007319
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007320 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007321 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007322}
7323#endif
7324
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007325/*
7326 * hrtimer based swevent callback
7327 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007328
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007329static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007330{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007331 enum hrtimer_restart ret = HRTIMER_RESTART;
7332 struct perf_sample_data data;
7333 struct pt_regs *regs;
7334 struct perf_event *event;
7335 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007336
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007337 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007338
7339 if (event->state != PERF_EVENT_STATE_ACTIVE)
7340 return HRTIMER_NORESTART;
7341
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007342 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007343
Robert Richterfd0d0002012-04-02 20:19:08 +02007344 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007345 regs = get_irq_regs();
7346
7347 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08007348 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02007349 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007350 ret = HRTIMER_NORESTART;
7351 }
7352
7353 period = max_t(u64, 10000, event->hw.sample_period);
7354 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
7355
7356 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007357}
7358
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007359static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007360{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007361 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007362 s64 period;
7363
7364 if (!is_sampling_event(event))
7365 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007366
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007367 period = local64_read(&hwc->period_left);
7368 if (period) {
7369 if (period < 0)
7370 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007371
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007372 local64_set(&hwc->period_left, 0);
7373 } else {
7374 period = max_t(u64, 10000, hwc->sample_period);
7375 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00007376 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
7377 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007378}
7379
7380static void perf_swevent_cancel_hrtimer(struct perf_event *event)
7381{
7382 struct hw_perf_event *hwc = &event->hw;
7383
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007384 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007385 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007386 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007387
7388 hrtimer_cancel(&hwc->hrtimer);
7389 }
7390}
7391
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007392static void perf_swevent_init_hrtimer(struct perf_event *event)
7393{
7394 struct hw_perf_event *hwc = &event->hw;
7395
7396 if (!is_sampling_event(event))
7397 return;
7398
7399 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
7400 hwc->hrtimer.function = perf_swevent_hrtimer;
7401
7402 /*
7403 * Since hrtimers have a fixed rate, we can do a static freq->period
7404 * mapping and avoid the whole period adjust feedback stuff.
7405 */
7406 if (event->attr.freq) {
7407 long freq = event->attr.sample_freq;
7408
7409 event->attr.sample_period = NSEC_PER_SEC / freq;
7410 hwc->sample_period = event->attr.sample_period;
7411 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09007412 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007413 event->attr.freq = 0;
7414 }
7415}
7416
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007417/*
7418 * Software event: cpu wall time clock
7419 */
7420
7421static void cpu_clock_event_update(struct perf_event *event)
7422{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007423 s64 prev;
7424 u64 now;
7425
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007426 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007427 prev = local64_xchg(&event->hw.prev_count, now);
7428 local64_add(now - prev, &event->count);
7429}
7430
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007431static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007432{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007433 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007434 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007435}
7436
7437static void cpu_clock_event_stop(struct perf_event *event, int flags)
7438{
7439 perf_swevent_cancel_hrtimer(event);
7440 cpu_clock_event_update(event);
7441}
7442
7443static int cpu_clock_event_add(struct perf_event *event, int flags)
7444{
7445 if (flags & PERF_EF_START)
7446 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08007447 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007448
7449 return 0;
7450}
7451
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007452static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007453{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007454 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007455}
7456
7457static void cpu_clock_event_read(struct perf_event *event)
7458{
7459 cpu_clock_event_update(event);
7460}
7461
7462static int cpu_clock_event_init(struct perf_event *event)
7463{
7464 if (event->attr.type != PERF_TYPE_SOFTWARE)
7465 return -ENOENT;
7466
7467 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
7468 return -ENOENT;
7469
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007470 /*
7471 * no branch sampling for software events
7472 */
7473 if (has_branch_stack(event))
7474 return -EOPNOTSUPP;
7475
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007476 perf_swevent_init_hrtimer(event);
7477
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007478 return 0;
7479}
7480
7481static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007482 .task_ctx_nr = perf_sw_context,
7483
Peter Zijlstra34f43922015-02-20 14:05:38 +01007484 .capabilities = PERF_PMU_CAP_NO_NMI,
7485
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007486 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007487 .add = cpu_clock_event_add,
7488 .del = cpu_clock_event_del,
7489 .start = cpu_clock_event_start,
7490 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007491 .read = cpu_clock_event_read,
7492};
7493
7494/*
7495 * Software event: task time clock
7496 */
7497
7498static void task_clock_event_update(struct perf_event *event, u64 now)
7499{
7500 u64 prev;
7501 s64 delta;
7502
7503 prev = local64_xchg(&event->hw.prev_count, now);
7504 delta = now - prev;
7505 local64_add(delta, &event->count);
7506}
7507
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007508static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007509{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007510 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007511 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007512}
7513
7514static void task_clock_event_stop(struct perf_event *event, int flags)
7515{
7516 perf_swevent_cancel_hrtimer(event);
7517 task_clock_event_update(event, event->ctx->time);
7518}
7519
7520static int task_clock_event_add(struct perf_event *event, int flags)
7521{
7522 if (flags & PERF_EF_START)
7523 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08007524 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007525
7526 return 0;
7527}
7528
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007529static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007530{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007531 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007532}
7533
7534static void task_clock_event_read(struct perf_event *event)
7535{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01007536 u64 now = perf_clock();
7537 u64 delta = now - event->ctx->timestamp;
7538 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007539
7540 task_clock_event_update(event, time);
7541}
7542
7543static int task_clock_event_init(struct perf_event *event)
7544{
7545 if (event->attr.type != PERF_TYPE_SOFTWARE)
7546 return -ENOENT;
7547
7548 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
7549 return -ENOENT;
7550
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007551 /*
7552 * no branch sampling for software events
7553 */
7554 if (has_branch_stack(event))
7555 return -EOPNOTSUPP;
7556
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007557 perf_swevent_init_hrtimer(event);
7558
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007559 return 0;
7560}
7561
7562static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007563 .task_ctx_nr = perf_sw_context,
7564
Peter Zijlstra34f43922015-02-20 14:05:38 +01007565 .capabilities = PERF_PMU_CAP_NO_NMI,
7566
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007567 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007568 .add = task_clock_event_add,
7569 .del = task_clock_event_del,
7570 .start = task_clock_event_start,
7571 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007572 .read = task_clock_event_read,
7573};
7574
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007575static void perf_pmu_nop_void(struct pmu *pmu)
7576{
7577}
7578
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007579static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
7580{
7581}
7582
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007583static int perf_pmu_nop_int(struct pmu *pmu)
7584{
7585 return 0;
7586}
7587
Geliang Tang18ab2cd2015-09-27 23:25:50 +08007588static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007589
7590static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007591{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007592 __this_cpu_write(nop_txn_flags, flags);
7593
7594 if (flags & ~PERF_PMU_TXN_ADD)
7595 return;
7596
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007597 perf_pmu_disable(pmu);
7598}
7599
7600static int perf_pmu_commit_txn(struct pmu *pmu)
7601{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007602 unsigned int flags = __this_cpu_read(nop_txn_flags);
7603
7604 __this_cpu_write(nop_txn_flags, 0);
7605
7606 if (flags & ~PERF_PMU_TXN_ADD)
7607 return 0;
7608
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007609 perf_pmu_enable(pmu);
7610 return 0;
7611}
7612
7613static void perf_pmu_cancel_txn(struct pmu *pmu)
7614{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007615 unsigned int flags = __this_cpu_read(nop_txn_flags);
7616
7617 __this_cpu_write(nop_txn_flags, 0);
7618
7619 if (flags & ~PERF_PMU_TXN_ADD)
7620 return;
7621
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007622 perf_pmu_enable(pmu);
7623}
7624
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007625static int perf_event_idx_default(struct perf_event *event)
7626{
Peter Zijlstrac719f562014-10-21 11:10:21 +02007627 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007628}
7629
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007630/*
7631 * Ensures all contexts with the same task_ctx_nr have the same
7632 * pmu_cpu_context too.
7633 */
Mark Rutland9e317042014-02-10 17:44:18 +00007634static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007635{
7636 struct pmu *pmu;
7637
7638 if (ctxn < 0)
7639 return NULL;
7640
7641 list_for_each_entry(pmu, &pmus, entry) {
7642 if (pmu->task_ctx_nr == ctxn)
7643 return pmu->pmu_cpu_context;
7644 }
7645
7646 return NULL;
7647}
7648
Peter Zijlstra51676952010-12-07 14:18:20 +01007649static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007650{
Peter Zijlstra51676952010-12-07 14:18:20 +01007651 int cpu;
7652
7653 for_each_possible_cpu(cpu) {
7654 struct perf_cpu_context *cpuctx;
7655
7656 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
7657
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007658 if (cpuctx->unique_pmu == old_pmu)
7659 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01007660 }
7661}
7662
7663static void free_pmu_context(struct pmu *pmu)
7664{
7665 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007666
7667 mutex_lock(&pmus_lock);
7668 /*
7669 * Like a real lame refcount.
7670 */
Peter Zijlstra51676952010-12-07 14:18:20 +01007671 list_for_each_entry(i, &pmus, entry) {
7672 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
7673 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007674 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01007675 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007676 }
7677
Peter Zijlstra51676952010-12-07 14:18:20 +01007678 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007679out:
7680 mutex_unlock(&pmus_lock);
7681}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007682static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007683
Peter Zijlstraabe43402010-11-17 23:17:37 +01007684static ssize_t
7685type_show(struct device *dev, struct device_attribute *attr, char *page)
7686{
7687 struct pmu *pmu = dev_get_drvdata(dev);
7688
7689 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
7690}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007691static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007692
Stephane Eranian62b85632013-04-03 14:21:34 +02007693static ssize_t
7694perf_event_mux_interval_ms_show(struct device *dev,
7695 struct device_attribute *attr,
7696 char *page)
7697{
7698 struct pmu *pmu = dev_get_drvdata(dev);
7699
7700 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
7701}
7702
Peter Zijlstra272325c2015-04-15 11:41:58 +02007703static DEFINE_MUTEX(mux_interval_mutex);
7704
Stephane Eranian62b85632013-04-03 14:21:34 +02007705static ssize_t
7706perf_event_mux_interval_ms_store(struct device *dev,
7707 struct device_attribute *attr,
7708 const char *buf, size_t count)
7709{
7710 struct pmu *pmu = dev_get_drvdata(dev);
7711 int timer, cpu, ret;
7712
7713 ret = kstrtoint(buf, 0, &timer);
7714 if (ret)
7715 return ret;
7716
7717 if (timer < 1)
7718 return -EINVAL;
7719
7720 /* same value, noting to do */
7721 if (timer == pmu->hrtimer_interval_ms)
7722 return count;
7723
Peter Zijlstra272325c2015-04-15 11:41:58 +02007724 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02007725 pmu->hrtimer_interval_ms = timer;
7726
7727 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02007728 get_online_cpus();
7729 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02007730 struct perf_cpu_context *cpuctx;
7731 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
7732 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
7733
Peter Zijlstra272325c2015-04-15 11:41:58 +02007734 cpu_function_call(cpu,
7735 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02007736 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02007737 put_online_cpus();
7738 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02007739
7740 return count;
7741}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007742static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02007743
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007744static struct attribute *pmu_dev_attrs[] = {
7745 &dev_attr_type.attr,
7746 &dev_attr_perf_event_mux_interval_ms.attr,
7747 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01007748};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007749ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007750
7751static int pmu_bus_running;
7752static struct bus_type pmu_bus = {
7753 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007754 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01007755};
7756
7757static void pmu_dev_release(struct device *dev)
7758{
7759 kfree(dev);
7760}
7761
7762static int pmu_dev_alloc(struct pmu *pmu)
7763{
7764 int ret = -ENOMEM;
7765
7766 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
7767 if (!pmu->dev)
7768 goto out;
7769
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01007770 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01007771 device_initialize(pmu->dev);
7772 ret = dev_set_name(pmu->dev, "%s", pmu->name);
7773 if (ret)
7774 goto free_dev;
7775
7776 dev_set_drvdata(pmu->dev, pmu);
7777 pmu->dev->bus = &pmu_bus;
7778 pmu->dev->release = pmu_dev_release;
7779 ret = device_add(pmu->dev);
7780 if (ret)
7781 goto free_dev;
7782
7783out:
7784 return ret;
7785
7786free_dev:
7787 put_device(pmu->dev);
7788 goto out;
7789}
7790
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007791static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007792static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007793
Mischa Jonker03d8e802013-06-04 11:45:48 +02007794int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007795{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007796 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007797
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007798 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007799 ret = -ENOMEM;
7800 pmu->pmu_disable_count = alloc_percpu(int);
7801 if (!pmu->pmu_disable_count)
7802 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007803
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007804 pmu->type = -1;
7805 if (!name)
7806 goto skip_type;
7807 pmu->name = name;
7808
7809 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08007810 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
7811 if (type < 0) {
7812 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007813 goto free_pdc;
7814 }
7815 }
7816 pmu->type = type;
7817
Peter Zijlstraabe43402010-11-17 23:17:37 +01007818 if (pmu_bus_running) {
7819 ret = pmu_dev_alloc(pmu);
7820 if (ret)
7821 goto free_idr;
7822 }
7823
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007824skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +01007825 if (pmu->task_ctx_nr == perf_hw_context) {
7826 static int hw_context_taken = 0;
7827
7828 if (WARN_ON_ONCE(hw_context_taken))
7829 pmu->task_ctx_nr = perf_invalid_context;
7830
7831 hw_context_taken = 1;
7832 }
7833
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007834 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
7835 if (pmu->pmu_cpu_context)
7836 goto got_cpu_context;
7837
Wei Yongjunc4814202013-04-12 11:05:54 +08007838 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007839 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
7840 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01007841 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007842
7843 for_each_possible_cpu(cpu) {
7844 struct perf_cpu_context *cpuctx;
7845
7846 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02007847 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007848 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007849 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007850 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02007851
Peter Zijlstra272325c2015-04-15 11:41:58 +02007852 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02007853
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007854 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007855 }
7856
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007857got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007858 if (!pmu->start_txn) {
7859 if (pmu->pmu_enable) {
7860 /*
7861 * If we have pmu_enable/pmu_disable calls, install
7862 * transaction stubs that use that to try and batch
7863 * hardware accesses.
7864 */
7865 pmu->start_txn = perf_pmu_start_txn;
7866 pmu->commit_txn = perf_pmu_commit_txn;
7867 pmu->cancel_txn = perf_pmu_cancel_txn;
7868 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007869 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007870 pmu->commit_txn = perf_pmu_nop_int;
7871 pmu->cancel_txn = perf_pmu_nop_void;
7872 }
7873 }
7874
7875 if (!pmu->pmu_enable) {
7876 pmu->pmu_enable = perf_pmu_nop_void;
7877 pmu->pmu_disable = perf_pmu_nop_void;
7878 }
7879
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007880 if (!pmu->event_idx)
7881 pmu->event_idx = perf_event_idx_default;
7882
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007883 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007884 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007885 ret = 0;
7886unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007887 mutex_unlock(&pmus_lock);
7888
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007889 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007890
Peter Zijlstraabe43402010-11-17 23:17:37 +01007891free_dev:
7892 device_del(pmu->dev);
7893 put_device(pmu->dev);
7894
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007895free_idr:
7896 if (pmu->type >= PERF_TYPE_MAX)
7897 idr_remove(&pmu_idr, pmu->type);
7898
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007899free_pdc:
7900 free_percpu(pmu->pmu_disable_count);
7901 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007902}
Yan, Zhengc464c762014-03-18 16:56:41 +08007903EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007904
7905void perf_pmu_unregister(struct pmu *pmu)
7906{
7907 mutex_lock(&pmus_lock);
7908 list_del_rcu(&pmu->entry);
7909 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007910
7911 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02007912 * We dereference the pmu list under both SRCU and regular RCU, so
7913 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007914 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007915 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02007916 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007917
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007918 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007919 if (pmu->type >= PERF_TYPE_MAX)
7920 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007921 device_del(pmu->dev);
7922 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01007923 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007924}
Yan, Zhengc464c762014-03-18 16:56:41 +08007925EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007926
Mark Rutlandcc34b982015-01-07 14:56:51 +00007927static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
7928{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007929 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00007930 int ret;
7931
7932 if (!try_module_get(pmu->module))
7933 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007934
7935 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02007936 /*
7937 * This ctx->mutex can nest when we're called through
7938 * inheritance. See the perf_event_ctx_lock_nested() comment.
7939 */
7940 ctx = perf_event_ctx_lock_nested(event->group_leader,
7941 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007942 BUG_ON(!ctx);
7943 }
7944
Mark Rutlandcc34b982015-01-07 14:56:51 +00007945 event->pmu = pmu;
7946 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007947
7948 if (ctx)
7949 perf_event_ctx_unlock(event->group_leader, ctx);
7950
Mark Rutlandcc34b982015-01-07 14:56:51 +00007951 if (ret)
7952 module_put(pmu->module);
7953
7954 return ret;
7955}
7956
Geliang Tang18ab2cd2015-09-27 23:25:50 +08007957static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007958{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007959 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007960 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08007961 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007962
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007963 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007964
7965 rcu_read_lock();
7966 pmu = idr_find(&pmu_idr, event->attr.type);
7967 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08007968 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007969 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08007970 if (ret)
7971 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007972 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08007973 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007974
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007975 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007976 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007977 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007978 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007979
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007980 if (ret != -ENOENT) {
7981 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007982 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007983 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007984 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007985 pmu = ERR_PTR(-ENOENT);
7986unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007987 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007988
7989 return pmu;
7990}
7991
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007992static void account_event_cpu(struct perf_event *event, int cpu)
7993{
7994 if (event->parent)
7995 return;
7996
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007997 if (is_cgroup_event(event))
7998 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
7999}
8000
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02008001/* Freq events need the tick to stay alive (see perf_event_task_tick). */
8002static void account_freq_event_nohz(void)
8003{
8004#ifdef CONFIG_NO_HZ_FULL
8005 /* Lock so we don't race with concurrent unaccount */
8006 spin_lock(&nr_freq_lock);
8007 if (atomic_inc_return(&nr_freq_events) == 1)
8008 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
8009 spin_unlock(&nr_freq_lock);
8010#endif
8011}
8012
8013static void account_freq_event(void)
8014{
8015 if (tick_nohz_full_enabled())
8016 account_freq_event_nohz();
8017 else
8018 atomic_inc(&nr_freq_events);
8019}
8020
8021
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008022static void account_event(struct perf_event *event)
8023{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008024 bool inc = false;
8025
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008026 if (event->parent)
8027 return;
8028
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008029 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008030 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008031 if (event->attr.mmap || event->attr.mmap_data)
8032 atomic_inc(&nr_mmap_events);
8033 if (event->attr.comm)
8034 atomic_inc(&nr_comm_events);
8035 if (event->attr.task)
8036 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02008037 if (event->attr.freq)
8038 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03008039 if (event->attr.context_switch) {
8040 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008041 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03008042 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008043 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008044 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008045 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008046 inc = true;
8047
Peter Zijlstra9107c892016-02-24 18:45:45 +01008048 if (inc) {
8049 if (atomic_inc_not_zero(&perf_sched_count))
8050 goto enabled;
8051
8052 mutex_lock(&perf_sched_mutex);
8053 if (!atomic_read(&perf_sched_count)) {
8054 static_branch_enable(&perf_sched_events);
8055 /*
8056 * Guarantee that all CPUs observe they key change and
8057 * call the perf scheduling hooks before proceeding to
8058 * install events that need them.
8059 */
8060 synchronize_sched();
8061 }
8062 /*
8063 * Now that we have waited for the sync_sched(), allow further
8064 * increments to by-pass the mutex.
8065 */
8066 atomic_inc(&perf_sched_count);
8067 mutex_unlock(&perf_sched_mutex);
8068 }
8069enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008070
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008071 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008072}
8073
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008074/*
8075 * Allocate and initialize a event structure
8076 */
8077static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008078perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008079 struct task_struct *task,
8080 struct perf_event *group_leader,
8081 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008082 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00008083 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008084{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02008085 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008086 struct perf_event *event;
8087 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008088 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008089
Oleg Nesterov66832eb2011-01-18 17:10:32 +01008090 if ((unsigned)cpu >= nr_cpu_ids) {
8091 if (!task || cpu != -1)
8092 return ERR_PTR(-EINVAL);
8093 }
8094
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008095 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008096 if (!event)
8097 return ERR_PTR(-ENOMEM);
8098
8099 /*
8100 * Single events are their own group leaders, with an
8101 * empty sibling list:
8102 */
8103 if (!group_leader)
8104 group_leader = event;
8105
8106 mutex_init(&event->child_mutex);
8107 INIT_LIST_HEAD(&event->child_list);
8108
8109 INIT_LIST_HEAD(&event->group_entry);
8110 INIT_LIST_HEAD(&event->event_entry);
8111 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01008112 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01008113 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01008114 INIT_HLIST_NODE(&event->hlist_entry);
8115
Peter Zijlstra10c6db12011-11-26 02:47:31 +01008116
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008117 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08008118 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008119
8120 mutex_init(&event->mmap_mutex);
8121
Al Viroa6fa9412012-08-20 14:59:25 +01008122 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008123 event->cpu = cpu;
8124 event->attr = *attr;
8125 event->group_leader = group_leader;
8126 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008127 event->oncpu = -1;
8128
8129 event->parent = parent_event;
8130
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08008131 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008132 event->id = atomic64_inc_return(&perf_event_id);
8133
8134 event->state = PERF_EVENT_STATE_INACTIVE;
8135
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008136 if (task) {
8137 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008138 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01008139 * XXX pmu::event_init needs to know what task to account to
8140 * and we cannot use the ctx information because we need the
8141 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008142 */
Peter Zijlstra50f16a82015-03-05 22:10:19 +01008143 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008144 }
8145
Peter Zijlstra34f43922015-02-20 14:05:38 +01008146 event->clock = &local_clock;
8147 if (parent_event)
8148 event->clock = parent_event->clock;
8149
Avi Kivity4dc0da82011-06-29 18:42:35 +03008150 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01008151 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03008152 context = parent_event->overflow_handler_context;
8153 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01008154
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01008155 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03008156 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02008157
Jiri Olsa0231bb52013-02-01 11:23:45 +01008158 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008159
8160 pmu = NULL;
8161
8162 hwc = &event->hw;
8163 hwc->sample_period = attr->sample_period;
8164 if (attr->freq && attr->sample_freq)
8165 hwc->sample_period = 1;
8166 hwc->last_period = hwc->sample_period;
8167
Peter Zijlstrae7850592010-05-21 14:43:08 +02008168 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008169
8170 /*
8171 * we currently do not support PERF_FORMAT_GROUP on inherited events
8172 */
8173 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008174 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008175
Yan, Zhenga46a2302014-11-04 21:56:06 -05008176 if (!has_branch_stack(event))
8177 event->attr.branch_sample_type = 0;
8178
Matt Fleming79dff512015-01-23 18:45:42 +00008179 if (cgroup_fd != -1) {
8180 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
8181 if (err)
8182 goto err_ns;
8183 }
8184
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008185 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008186 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008187 goto err_ns;
8188 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008189 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008190 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008191 }
8192
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008193 err = exclusive_event_init(event);
8194 if (err)
8195 goto err_pmu;
8196
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008197 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02008198 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
8199 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008200 if (err)
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008201 goto err_per_task;
Stephane Eraniand010b332012-02-09 23:21:00 +01008202 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008203 }
8204
Alexander Shishkin927a5572016-03-02 13:24:14 +02008205 /* symmetric to unaccount_event() in _free_event() */
8206 account_event(event);
8207
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008208 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008209
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008210err_per_task:
8211 exclusive_event_destroy(event);
8212
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008213err_pmu:
8214 if (event->destroy)
8215 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08008216 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008217err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00008218 if (is_cgroup_event(event))
8219 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008220 if (event->ns)
8221 put_pid_ns(event->ns);
8222 kfree(event);
8223
8224 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008225}
8226
8227static int perf_copy_attr(struct perf_event_attr __user *uattr,
8228 struct perf_event_attr *attr)
8229{
8230 u32 size;
8231 int ret;
8232
8233 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
8234 return -EFAULT;
8235
8236 /*
8237 * zero the full structure, so that a short copy will be nice.
8238 */
8239 memset(attr, 0, sizeof(*attr));
8240
8241 ret = get_user(size, &uattr->size);
8242 if (ret)
8243 return ret;
8244
8245 if (size > PAGE_SIZE) /* silly large */
8246 goto err_size;
8247
8248 if (!size) /* abi compat */
8249 size = PERF_ATTR_SIZE_VER0;
8250
8251 if (size < PERF_ATTR_SIZE_VER0)
8252 goto err_size;
8253
8254 /*
8255 * If we're handed a bigger struct than we know of,
8256 * ensure all the unknown bits are 0 - i.e. new
8257 * user-space does not rely on any kernel feature
8258 * extensions we dont know about yet.
8259 */
8260 if (size > sizeof(*attr)) {
8261 unsigned char __user *addr;
8262 unsigned char __user *end;
8263 unsigned char val;
8264
8265 addr = (void __user *)uattr + sizeof(*attr);
8266 end = (void __user *)uattr + size;
8267
8268 for (; addr < end; addr++) {
8269 ret = get_user(val, addr);
8270 if (ret)
8271 return ret;
8272 if (val)
8273 goto err_size;
8274 }
8275 size = sizeof(*attr);
8276 }
8277
8278 ret = copy_from_user(attr, uattr, size);
8279 if (ret)
8280 return -EFAULT;
8281
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05308282 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008283 return -EINVAL;
8284
8285 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
8286 return -EINVAL;
8287
8288 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
8289 return -EINVAL;
8290
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008291 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
8292 u64 mask = attr->branch_sample_type;
8293
8294 /* only using defined bits */
8295 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
8296 return -EINVAL;
8297
8298 /* at least one branch bit must be set */
8299 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
8300 return -EINVAL;
8301
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008302 /* propagate priv level, when not set for branch */
8303 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
8304
8305 /* exclude_kernel checked on syscall entry */
8306 if (!attr->exclude_kernel)
8307 mask |= PERF_SAMPLE_BRANCH_KERNEL;
8308
8309 if (!attr->exclude_user)
8310 mask |= PERF_SAMPLE_BRANCH_USER;
8311
8312 if (!attr->exclude_hv)
8313 mask |= PERF_SAMPLE_BRANCH_HV;
8314 /*
8315 * adjust user setting (for HW filter setup)
8316 */
8317 attr->branch_sample_type = mask;
8318 }
Stephane Eraniane7122092013-06-06 11:02:04 +02008319 /* privileged levels capture (kernel, hv): check permissions */
8320 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02008321 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8322 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008323 }
Jiri Olsa40189942012-08-07 15:20:37 +02008324
Jiri Olsac5ebced2012-08-07 15:20:40 +02008325 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02008326 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02008327 if (ret)
8328 return ret;
8329 }
8330
8331 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
8332 if (!arch_perf_have_user_stack_dump())
8333 return -ENOSYS;
8334
8335 /*
8336 * We have __u32 type for the size, but so far
8337 * we can only use __u16 as maximum due to the
8338 * __u16 sample size limit.
8339 */
8340 if (attr->sample_stack_user >= USHRT_MAX)
8341 ret = -EINVAL;
8342 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
8343 ret = -EINVAL;
8344 }
Jiri Olsa40189942012-08-07 15:20:37 +02008345
Stephane Eranian60e23642014-09-24 13:48:37 +02008346 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
8347 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008348out:
8349 return ret;
8350
8351err_size:
8352 put_user(sizeof(*attr), &uattr->size);
8353 ret = -E2BIG;
8354 goto out;
8355}
8356
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008357static int
8358perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008359{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01008360 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008361 int ret = -EINVAL;
8362
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008363 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008364 goto set;
8365
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008366 /* don't allow circular references */
8367 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008368 goto out;
8369
Peter Zijlstra0f139302010-05-20 14:35:15 +02008370 /*
8371 * Don't allow cross-cpu buffers
8372 */
8373 if (output_event->cpu != event->cpu)
8374 goto out;
8375
8376 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02008377 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02008378 */
8379 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
8380 goto out;
8381
Peter Zijlstra34f43922015-02-20 14:05:38 +01008382 /*
8383 * Mixing clocks in the same buffer is trouble you don't need.
8384 */
8385 if (output_event->clock != event->clock)
8386 goto out;
8387
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02008388 /*
8389 * If both events generate aux data, they must be on the same PMU
8390 */
8391 if (has_aux(event) && has_aux(output_event) &&
8392 event->pmu != output_event->pmu)
8393 goto out;
8394
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008395set:
8396 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008397 /* Can't redirect output if we've got an active mmap() */
8398 if (atomic_read(&event->mmap_count))
8399 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008400
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008401 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02008402 /* get the rb we want to redirect to */
8403 rb = ring_buffer_get(output_event);
8404 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008405 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008406 }
8407
Peter Zijlstrab69cf532014-03-14 10:50:33 +01008408 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02008409
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008410 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008411unlock:
8412 mutex_unlock(&event->mmap_mutex);
8413
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008414out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008415 return ret;
8416}
8417
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008418static void mutex_lock_double(struct mutex *a, struct mutex *b)
8419{
8420 if (b < a)
8421 swap(a, b);
8422
8423 mutex_lock(a);
8424 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
8425}
8426
Peter Zijlstra34f43922015-02-20 14:05:38 +01008427static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
8428{
8429 bool nmi_safe = false;
8430
8431 switch (clk_id) {
8432 case CLOCK_MONOTONIC:
8433 event->clock = &ktime_get_mono_fast_ns;
8434 nmi_safe = true;
8435 break;
8436
8437 case CLOCK_MONOTONIC_RAW:
8438 event->clock = &ktime_get_raw_fast_ns;
8439 nmi_safe = true;
8440 break;
8441
8442 case CLOCK_REALTIME:
8443 event->clock = &ktime_get_real_ns;
8444 break;
8445
8446 case CLOCK_BOOTTIME:
8447 event->clock = &ktime_get_boot_ns;
8448 break;
8449
8450 case CLOCK_TAI:
8451 event->clock = &ktime_get_tai_ns;
8452 break;
8453
8454 default:
8455 return -EINVAL;
8456 }
8457
8458 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
8459 return -EINVAL;
8460
8461 return 0;
8462}
8463
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008464/**
8465 * sys_perf_event_open - open a performance event, associate it to a task/cpu
8466 *
8467 * @attr_uptr: event_id type attributes for monitoring/sampling
8468 * @pid: target pid
8469 * @cpu: target cpu
8470 * @group_fd: group leader event fd
8471 */
8472SYSCALL_DEFINE5(perf_event_open,
8473 struct perf_event_attr __user *, attr_uptr,
8474 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
8475{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008476 struct perf_event *group_leader = NULL, *output_event = NULL;
8477 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008478 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008479 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008480 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04008481 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07008482 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008483 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04008484 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008485 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008486 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01008487 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00008488 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008489
8490 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02008491 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008492 return -EINVAL;
8493
8494 err = perf_copy_attr(attr_uptr, &attr);
8495 if (err)
8496 return err;
8497
8498 if (!attr.exclude_kernel) {
8499 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8500 return -EACCES;
8501 }
8502
8503 if (attr.freq) {
8504 if (attr.sample_freq > sysctl_perf_event_sample_rate)
8505 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02008506 } else {
8507 if (attr.sample_period & (1ULL << 63))
8508 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008509 }
8510
Stephane Eraniane5d13672011-02-14 11:20:01 +02008511 /*
8512 * In cgroup mode, the pid argument is used to pass the fd
8513 * opened to the cgroup directory in cgroupfs. The cpu argument
8514 * designates the cpu on which to monitor threads from that
8515 * cgroup.
8516 */
8517 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
8518 return -EINVAL;
8519
Yann Droneauda21b0b32014-01-05 21:36:33 +01008520 if (flags & PERF_FLAG_FD_CLOEXEC)
8521 f_flags |= O_CLOEXEC;
8522
8523 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04008524 if (event_fd < 0)
8525 return event_fd;
8526
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008527 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04008528 err = perf_fget_light(group_fd, &group);
8529 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008530 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04008531 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008532 if (flags & PERF_FLAG_FD_OUTPUT)
8533 output_event = group_leader;
8534 if (flags & PERF_FLAG_FD_NO_GROUP)
8535 group_leader = NULL;
8536 }
8537
Stephane Eraniane5d13672011-02-14 11:20:01 +02008538 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008539 task = find_lively_task_by_vpid(pid);
8540 if (IS_ERR(task)) {
8541 err = PTR_ERR(task);
8542 goto err_group_fd;
8543 }
8544 }
8545
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008546 if (task && group_leader &&
8547 group_leader->attr.inherit != attr.inherit) {
8548 err = -EINVAL;
8549 goto err_task;
8550 }
8551
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008552 get_online_cpus();
8553
Matt Fleming79dff512015-01-23 18:45:42 +00008554 if (flags & PERF_FLAG_PID_CGROUP)
8555 cgroup_fd = pid;
8556
Avi Kivity4dc0da82011-06-29 18:42:35 +03008557 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00008558 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008559 if (IS_ERR(event)) {
8560 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008561 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008562 }
8563
Vince Weaver53b25332014-05-16 17:12:12 -04008564 if (is_sampling_event(event)) {
8565 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
8566 err = -ENOTSUPP;
8567 goto err_alloc;
8568 }
8569 }
8570
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008571 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008572 * Special case software events and allow them to be part of
8573 * any hardware group.
8574 */
8575 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008576
Peter Zijlstra34f43922015-02-20 14:05:38 +01008577 if (attr.use_clockid) {
8578 err = perf_event_set_clock(event, attr.clockid);
8579 if (err)
8580 goto err_alloc;
8581 }
8582
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008583 if (group_leader &&
8584 (is_software_event(event) != is_software_event(group_leader))) {
8585 if (is_software_event(event)) {
8586 /*
8587 * If event and group_leader are not both a software
8588 * event, and event is, then group leader is not.
8589 *
8590 * Allow the addition of software events to !software
8591 * groups, this is safe because software events never
8592 * fail to schedule.
8593 */
8594 pmu = group_leader->pmu;
8595 } else if (is_software_event(group_leader) &&
8596 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
8597 /*
8598 * In case the group is a pure software group, and we
8599 * try to add a hardware event, move the whole group to
8600 * the hardware context.
8601 */
8602 move_group = 1;
8603 }
8604 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008605
8606 /*
8607 * Get the target context (task or percpu):
8608 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05008609 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008610 if (IS_ERR(ctx)) {
8611 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008612 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008613 }
8614
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008615 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
8616 err = -EBUSY;
8617 goto err_context;
8618 }
8619
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02008620 if (task) {
8621 put_task_struct(task);
8622 task = NULL;
8623 }
8624
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008625 /*
8626 * Look up the group leader (we will attach this event to it):
8627 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008628 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008629 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008630
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008631 /*
8632 * Do not allow a recursive hierarchy (this new sibling
8633 * becoming part of another group-sibling):
8634 */
8635 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008636 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01008637
8638 /* All events in a group should have the same clock */
8639 if (group_leader->clock != event->clock)
8640 goto err_context;
8641
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008642 /*
8643 * Do not allow to attach to a group in a different
8644 * task or CPU context:
8645 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008646 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01008647 /*
8648 * Make sure we're both on the same task, or both
8649 * per-cpu events.
8650 */
8651 if (group_leader->ctx->task != ctx->task)
8652 goto err_context;
8653
8654 /*
8655 * Make sure we're both events for the same CPU;
8656 * grouping events for different CPUs is broken; since
8657 * you can never concurrently schedule them anyhow.
8658 */
8659 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008660 goto err_context;
8661 } else {
8662 if (group_leader->ctx != ctx)
8663 goto err_context;
8664 }
8665
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008666 /*
8667 * Only a group leader can be exclusive or pinned
8668 */
8669 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008670 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008671 }
8672
8673 if (output_event) {
8674 err = perf_event_set_output(event, output_event);
8675 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008676 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008677 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008678
Yann Droneauda21b0b32014-01-05 21:36:33 +01008679 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
8680 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04008681 if (IS_ERR(event_file)) {
8682 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +02008683 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008684 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04008685 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008686
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008687 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008688 gctx = group_leader->ctx;
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008689 mutex_lock_double(&gctx->mutex, &ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008690 if (gctx->task == TASK_TOMBSTONE) {
8691 err = -ESRCH;
8692 goto err_locked;
8693 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008694 } else {
8695 mutex_lock(&ctx->mutex);
8696 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008697
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008698 if (ctx->task == TASK_TOMBSTONE) {
8699 err = -ESRCH;
8700 goto err_locked;
8701 }
8702
Peter Zijlstraa7239682015-09-09 19:06:33 +02008703 if (!perf_event_validate_size(event)) {
8704 err = -E2BIG;
8705 goto err_locked;
8706 }
8707
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008708 /*
8709 * Must be under the same ctx::mutex as perf_install_in_context(),
8710 * because we need to serialize with concurrent event creation.
8711 */
8712 if (!exclusive_event_installable(event, ctx)) {
8713 /* exclusive and group stuff are assumed mutually exclusive */
8714 WARN_ON_ONCE(move_group);
8715
8716 err = -EBUSY;
8717 goto err_locked;
8718 }
8719
8720 WARN_ON_ONCE(ctx->parent_ctx);
8721
8722 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008723 /*
8724 * See perf_event_ctx_lock() for comments on the details
8725 * of swizzling perf_event::ctx.
8726 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008727 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01008728
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008729 list_for_each_entry(sibling, &group_leader->sibling_list,
8730 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008731 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008732 put_ctx(gctx);
8733 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008734
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008735 /*
8736 * Wait for everybody to stop referencing the events through
8737 * the old lists, before installing it on new lists.
8738 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008739 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008740
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008741 /*
8742 * Install the group siblings before the group leader.
8743 *
8744 * Because a group leader will try and install the entire group
8745 * (through the sibling list, which is still in-tact), we can
8746 * end up with siblings installed in the wrong context.
8747 *
8748 * By installing siblings first we NO-OP because they're not
8749 * reachable through the group lists.
8750 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008751 list_for_each_entry(sibling, &group_leader->sibling_list,
8752 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008753 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01008754 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008755 get_ctx(ctx);
8756 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008757
8758 /*
8759 * Removing from the context ends up with disabled
8760 * event. What we want here is event in the initial
8761 * startup state, ready to be add into new context.
8762 */
8763 perf_event__state_init(group_leader);
8764 perf_install_in_context(ctx, group_leader, group_leader->cpu);
8765 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008766
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008767 /*
8768 * Now that all events are installed in @ctx, nothing
8769 * references @gctx anymore, so drop the last reference we have
8770 * on it.
8771 */
8772 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008773 }
8774
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02008775 /*
8776 * Precalculate sample_data sizes; do while holding ctx::mutex such
8777 * that we're serialized against further additions and before
8778 * perf_install_in_context() which is the point the event is active and
8779 * can use these values.
8780 */
8781 perf_event__header_size(event);
8782 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008783
Peter Zijlstra78cd2c72016-01-25 14:08:45 +01008784 event->owner = current;
8785
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08008786 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008787 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008788
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008789 if (move_group)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008790 mutex_unlock(&gctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008791 mutex_unlock(&ctx->mutex);
8792
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008793 put_online_cpus();
8794
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008795 mutex_lock(&current->perf_event_mutex);
8796 list_add_tail(&event->owner_entry, &current->perf_event_list);
8797 mutex_unlock(&current->perf_event_mutex);
8798
Peter Zijlstra8a495422010-05-27 15:47:49 +02008799 /*
8800 * Drop the reference on the group_event after placing the
8801 * new event on the sibling_list. This ensures destruction
8802 * of the group leader will find the pointer to itself in
8803 * perf_group_detach().
8804 */
Al Viro2903ff02012-08-28 12:52:22 -04008805 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04008806 fd_install(event_fd, event_file);
8807 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008808
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008809err_locked:
8810 if (move_group)
8811 mutex_unlock(&gctx->mutex);
8812 mutex_unlock(&ctx->mutex);
8813/* err_file: */
8814 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008815err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008816 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04008817 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008818err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +01008819 /*
8820 * If event_file is set, the fput() above will have called ->release()
8821 * and that will take care of freeing the event.
8822 */
8823 if (!event_file)
8824 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008825err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008826 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008827err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02008828 if (task)
8829 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008830err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04008831 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04008832err_fd:
8833 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008834 return err;
8835}
8836
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008837/**
8838 * perf_event_create_kernel_counter
8839 *
8840 * @attr: attributes of the counter to create
8841 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07008842 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008843 */
8844struct perf_event *
8845perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07008846 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008847 perf_overflow_handler_t overflow_handler,
8848 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008849{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008850 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008851 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008852 int err;
8853
8854 /*
8855 * Get the target context (task or percpu):
8856 */
8857
Avi Kivity4dc0da82011-06-29 18:42:35 +03008858 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00008859 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008860 if (IS_ERR(event)) {
8861 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008862 goto err;
8863 }
8864
Jiri Olsaf8697762014-08-01 14:33:01 +02008865 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008866 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +02008867
Yan, Zheng4af57ef282014-11-04 21:56:01 -05008868 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008869 if (IS_ERR(ctx)) {
8870 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008871 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008872 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008873
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008874 WARN_ON_ONCE(ctx->parent_ctx);
8875 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008876 if (ctx->task == TASK_TOMBSTONE) {
8877 err = -ESRCH;
8878 goto err_unlock;
8879 }
8880
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008881 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008882 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008883 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008884 }
8885
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008886 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008887 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008888 mutex_unlock(&ctx->mutex);
8889
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008890 return event;
8891
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008892err_unlock:
8893 mutex_unlock(&ctx->mutex);
8894 perf_unpin_context(ctx);
8895 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008896err_free:
8897 free_event(event);
8898err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008899 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008900}
8901EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
8902
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008903void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
8904{
8905 struct perf_event_context *src_ctx;
8906 struct perf_event_context *dst_ctx;
8907 struct perf_event *event, *tmp;
8908 LIST_HEAD(events);
8909
8910 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
8911 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
8912
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008913 /*
8914 * See perf_event_ctx_lock() for comments on the details
8915 * of swizzling perf_event::ctx.
8916 */
8917 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008918 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
8919 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008920 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02008921 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008922 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02008923 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008924 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008925
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008926 /*
8927 * Wait for the events to quiesce before re-instating them.
8928 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008929 synchronize_rcu();
8930
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008931 /*
8932 * Re-instate events in 2 passes.
8933 *
8934 * Skip over group leaders and only install siblings on this first
8935 * pass, siblings will not get enabled without a leader, however a
8936 * leader will enable its siblings, even if those are still on the old
8937 * context.
8938 */
8939 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
8940 if (event->group_leader == event)
8941 continue;
8942
8943 list_del(&event->migrate_entry);
8944 if (event->state >= PERF_EVENT_STATE_OFF)
8945 event->state = PERF_EVENT_STATE_INACTIVE;
8946 account_event_cpu(event, dst_cpu);
8947 perf_install_in_context(dst_ctx, event, dst_cpu);
8948 get_ctx(dst_ctx);
8949 }
8950
8951 /*
8952 * Once all the siblings are setup properly, install the group leaders
8953 * to make it go.
8954 */
Peter Zijlstra98861672013-10-03 16:02:23 +02008955 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
8956 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008957 if (event->state >= PERF_EVENT_STATE_OFF)
8958 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02008959 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008960 perf_install_in_context(dst_ctx, event, dst_cpu);
8961 get_ctx(dst_ctx);
8962 }
8963 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008964 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008965}
8966EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
8967
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008968static void sync_child_event(struct perf_event *child_event,
8969 struct task_struct *child)
8970{
8971 struct perf_event *parent_event = child_event->parent;
8972 u64 child_val;
8973
8974 if (child_event->attr.inherit_stat)
8975 perf_event_read_event(child_event, child);
8976
Peter Zijlstrab5e58792010-05-21 14:43:12 +02008977 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008978
8979 /*
8980 * Add back the child's count to the parent's count:
8981 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02008982 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008983 atomic64_add(child_event->total_time_enabled,
8984 &parent_event->child_total_time_enabled);
8985 atomic64_add(child_event->total_time_running,
8986 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008987}
8988
8989static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008990perf_event_exit_event(struct perf_event *child_event,
8991 struct perf_event_context *child_ctx,
8992 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008993{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008994 struct perf_event *parent_event = child_event->parent;
8995
Peter Zijlstra1903d502014-07-15 17:27:27 +02008996 /*
8997 * Do not destroy the 'original' grouping; because of the context
8998 * switch optimization the original events could've ended up in a
8999 * random child task.
9000 *
9001 * If we were to destroy the original group, all group related
9002 * operations would cease to function properly after this random
9003 * child dies.
9004 *
9005 * Do destroy all inherited groups, we don't care about those
9006 * and being thorough is better.
9007 */
Peter Zijlstra32132a32016-01-11 15:40:59 +01009008 raw_spin_lock_irq(&child_ctx->lock);
9009 WARN_ON_ONCE(child_ctx->is_active);
9010
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009011 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +01009012 perf_group_detach(child_event);
9013 list_del_event(child_event, child_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01009014 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +01009015 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009016
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009017 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009018 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009019 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009020 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -04009021 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009022 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009023 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009024 /*
9025 * Child events can be cleaned up.
9026 */
9027
9028 sync_child_event(child_event, child);
9029
9030 /*
9031 * Remove this event from the parent's list
9032 */
9033 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
9034 mutex_lock(&parent_event->child_mutex);
9035 list_del_init(&child_event->child_list);
9036 mutex_unlock(&parent_event->child_mutex);
9037
9038 /*
9039 * Kick perf_poll() for is_event_hup().
9040 */
9041 perf_event_wakeup(parent_event);
9042 free_event(child_event);
9043 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009044}
9045
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009046static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009047{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02009048 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009049 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009050
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009051 WARN_ON_ONCE(child != current);
9052
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009053 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009054 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009055 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009056
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009057 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009058 * In order to reduce the amount of tricky in ctx tear-down, we hold
9059 * ctx::mutex over the entire thing. This serializes against almost
9060 * everything that wants to access the ctx.
9061 *
9062 * The exception is sys_perf_event_open() /
9063 * perf_event_create_kernel_count() which does find_get_context()
9064 * without ctx::mutex (it cannot because of the move_group double mutex
9065 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009066 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009067 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009068
9069 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009070 * In a single ctx::lock section, de-schedule the events and detach the
9071 * context from the task such that we cannot ever get it scheduled back
9072 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009073 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009074 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009075 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02009076
9077 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009078 * Now that the context is inactive, destroy the task <-> ctx relation
9079 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009080 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009081 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
9082 put_ctx(child_ctx); /* cannot be last */
9083 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
9084 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009085
Peter Zijlstra211de6e2014-09-30 19:23:08 +02009086 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009087 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009088
Peter Zijlstra211de6e2014-09-30 19:23:08 +02009089 if (clone_ctx)
9090 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02009091
9092 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009093 * Report the task dead after unscheduling the events so that we
9094 * won't get any samples after PERF_RECORD_EXIT. We can however still
9095 * get a few PERF_RECORD_READ events.
9096 */
9097 perf_event_task(child, child_ctx, 0);
9098
Peter Zijlstraebf905f2014-05-29 19:00:24 +02009099 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009100 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009101
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009102 mutex_unlock(&child_ctx->mutex);
9103
9104 put_ctx(child_ctx);
9105}
9106
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009107/*
9108 * When a child task exits, feed back event values to parent events.
9109 */
9110void perf_event_exit_task(struct task_struct *child)
9111{
Peter Zijlstra88821352010-11-09 19:01:43 +01009112 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009113 int ctxn;
9114
Peter Zijlstra88821352010-11-09 19:01:43 +01009115 mutex_lock(&child->perf_event_mutex);
9116 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
9117 owner_entry) {
9118 list_del_init(&event->owner_entry);
9119
9120 /*
9121 * Ensure the list deletion is visible before we clear
9122 * the owner, closes a race against perf_release() where
9123 * we need to serialize on the owner->perf_event_mutex.
9124 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01009125 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +01009126 }
9127 mutex_unlock(&child->perf_event_mutex);
9128
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009129 for_each_task_context_nr(ctxn)
9130 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +01009131
9132 /*
9133 * The perf_event_exit_task_context calls perf_event_task
9134 * with child's task_ctx, which generates EXIT events for
9135 * child contexts and sets child->perf_event_ctxp[] to NULL.
9136 * At this point we need to send EXIT events to cpu contexts.
9137 */
9138 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009139}
9140
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009141static void perf_free_event(struct perf_event *event,
9142 struct perf_event_context *ctx)
9143{
9144 struct perf_event *parent = event->parent;
9145
9146 if (WARN_ON_ONCE(!parent))
9147 return;
9148
9149 mutex_lock(&parent->child_mutex);
9150 list_del_init(&event->child_list);
9151 mutex_unlock(&parent->child_mutex);
9152
Al Viroa6fa9412012-08-20 14:59:25 +01009153 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009154
Peter Zijlstra652884f2015-01-23 11:20:10 +01009155 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02009156 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009157 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +01009158 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009159 free_event(event);
9160}
9161
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009162/*
Peter Zijlstra652884f2015-01-23 11:20:10 +01009163 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009164 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +01009165 *
9166 * Not all locks are strictly required, but take them anyway to be nice and
9167 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009168 */
9169void perf_event_free_task(struct task_struct *task)
9170{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009171 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009172 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009173 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009174
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009175 for_each_task_context_nr(ctxn) {
9176 ctx = task->perf_event_ctxp[ctxn];
9177 if (!ctx)
9178 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009179
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009180 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009181again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009182 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
9183 group_entry)
9184 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009185
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009186 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
9187 group_entry)
9188 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009189
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009190 if (!list_empty(&ctx->pinned_groups) ||
9191 !list_empty(&ctx->flexible_groups))
9192 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009193
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009194 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009195
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009196 put_ctx(ctx);
9197 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009198}
9199
Peter Zijlstra4e231c72010-09-09 21:01:59 +02009200void perf_event_delayed_put(struct task_struct *task)
9201{
9202 int ctxn;
9203
9204 for_each_task_context_nr(ctxn)
9205 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
9206}
9207
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009208struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +00009209{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009210 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +00009211
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009212 file = fget_raw(fd);
9213 if (!file)
9214 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +00009215
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009216 if (file->f_op != &perf_fops) {
9217 fput(file);
9218 return ERR_PTR(-EBADF);
9219 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00009220
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009221 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +00009222}
9223
9224const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
9225{
9226 if (!event)
9227 return ERR_PTR(-EINVAL);
9228
9229 return &event->attr;
9230}
9231
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009232/*
9233 * inherit a event from parent task to child task:
9234 */
9235static struct perf_event *
9236inherit_event(struct perf_event *parent_event,
9237 struct task_struct *parent,
9238 struct perf_event_context *parent_ctx,
9239 struct task_struct *child,
9240 struct perf_event *group_leader,
9241 struct perf_event_context *child_ctx)
9242{
Jiri Olsa1929def2014-09-12 13:18:27 +02009243 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009244 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02009245 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009246
9247 /*
9248 * Instead of creating recursive hierarchies of events,
9249 * we link inherited events back to the original parent,
9250 * which has a filp for sure, which we use as the reference
9251 * count:
9252 */
9253 if (parent_event->parent)
9254 parent_event = parent_event->parent;
9255
9256 child_event = perf_event_alloc(&parent_event->attr,
9257 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009258 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009259 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +00009260 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009261 if (IS_ERR(child_event))
9262 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01009263
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02009264 /*
9265 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
9266 * must be under the same lock in order to serialize against
9267 * perf_event_release_kernel(), such that either we must observe
9268 * is_orphaned_event() or they will observe us on the child_list.
9269 */
9270 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02009271 if (is_orphaned_event(parent_event) ||
9272 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02009273 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +01009274 free_event(child_event);
9275 return NULL;
9276 }
9277
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009278 get_ctx(child_ctx);
9279
9280 /*
9281 * Make the child state follow the state of the parent event,
9282 * not its attr.disabled bit. We hold the parent's mutex,
9283 * so we won't race with perf_event_{en, dis}able_family.
9284 */
Jiri Olsa1929def2014-09-12 13:18:27 +02009285 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009286 child_event->state = PERF_EVENT_STATE_INACTIVE;
9287 else
9288 child_event->state = PERF_EVENT_STATE_OFF;
9289
9290 if (parent_event->attr.freq) {
9291 u64 sample_period = parent_event->hw.sample_period;
9292 struct hw_perf_event *hwc = &child_event->hw;
9293
9294 hwc->sample_period = sample_period;
9295 hwc->last_period = sample_period;
9296
9297 local64_set(&hwc->period_left, sample_period);
9298 }
9299
9300 child_event->ctx = child_ctx;
9301 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03009302 child_event->overflow_handler_context
9303 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009304
9305 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02009306 * Precalculate sample_data sizes
9307 */
9308 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02009309 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02009310
9311 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009312 * Link it up in the child's context:
9313 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02009314 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009315 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02009316 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009317
9318 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009319 * Link this into the parent event's child list
9320 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009321 list_add_tail(&child_event->child_list, &parent_event->child_list);
9322 mutex_unlock(&parent_event->child_mutex);
9323
9324 return child_event;
9325}
9326
9327static int inherit_group(struct perf_event *parent_event,
9328 struct task_struct *parent,
9329 struct perf_event_context *parent_ctx,
9330 struct task_struct *child,
9331 struct perf_event_context *child_ctx)
9332{
9333 struct perf_event *leader;
9334 struct perf_event *sub;
9335 struct perf_event *child_ctr;
9336
9337 leader = inherit_event(parent_event, parent, parent_ctx,
9338 child, NULL, child_ctx);
9339 if (IS_ERR(leader))
9340 return PTR_ERR(leader);
9341 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
9342 child_ctr = inherit_event(sub, parent, parent_ctx,
9343 child, leader, child_ctx);
9344 if (IS_ERR(child_ctr))
9345 return PTR_ERR(child_ctr);
9346 }
9347 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009348}
9349
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009350static int
9351inherit_task_group(struct perf_event *event, struct task_struct *parent,
9352 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009353 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009354 int *inherited_all)
9355{
9356 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009357 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009358
9359 if (!event->attr.inherit) {
9360 *inherited_all = 0;
9361 return 0;
9362 }
9363
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009364 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009365 if (!child_ctx) {
9366 /*
9367 * This is executed from the parent task context, so
9368 * inherit events that have been marked for cloning.
9369 * First allocate and initialize a context for the
9370 * child.
9371 */
9372
Jiri Olsa734df5a2013-07-09 17:44:10 +02009373 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009374 if (!child_ctx)
9375 return -ENOMEM;
9376
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009377 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009378 }
9379
9380 ret = inherit_group(event, parent, parent_ctx,
9381 child, child_ctx);
9382
9383 if (ret)
9384 *inherited_all = 0;
9385
9386 return ret;
9387}
9388
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009389/*
9390 * Initialize the perf_event context in task_struct
9391 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02009392static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009393{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009394 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009395 struct perf_event_context *cloned_ctx;
9396 struct perf_event *event;
9397 struct task_struct *parent = current;
9398 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009399 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009400 int ret = 0;
9401
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009402 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009403 return 0;
9404
9405 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009406 * If the parent's context is a clone, pin it so it won't get
9407 * swapped under us.
9408 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009409 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02009410 if (!parent_ctx)
9411 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009412
9413 /*
9414 * No need to check if parent_ctx != NULL here; since we saw
9415 * it non-NULL earlier, the only reason for it to become NULL
9416 * is if we exit, and since we're currently in the middle of
9417 * a fork we can't be exiting at the same time.
9418 */
9419
9420 /*
9421 * Lock the parent list. No need to lock the child - not PID
9422 * hashed yet and not running, so nobody can access it.
9423 */
9424 mutex_lock(&parent_ctx->mutex);
9425
9426 /*
9427 * We dont have to disable NMIs - we are only looking at
9428 * the list, not manipulating it:
9429 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009430 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009431 ret = inherit_task_group(event, parent, parent_ctx,
9432 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009433 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009434 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009435 }
9436
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009437 /*
9438 * We can't hold ctx->lock when iterating the ->flexible_group list due
9439 * to allocations, but we need to prevent rotation because
9440 * rotate_ctx() will change the list from interrupt context.
9441 */
9442 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
9443 parent_ctx->rotate_disable = 1;
9444 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
9445
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009446 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009447 ret = inherit_task_group(event, parent, parent_ctx,
9448 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009449 if (ret)
9450 break;
9451 }
9452
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009453 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
9454 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009455
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009456 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009457
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01009458 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009459 /*
9460 * Mark the child context as a clone of the parent
9461 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009462 *
9463 * Note that if the parent is a clone, the holding of
9464 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009465 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009466 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009467 if (cloned_ctx) {
9468 child_ctx->parent_ctx = cloned_ctx;
9469 child_ctx->parent_gen = parent_ctx->parent_gen;
9470 } else {
9471 child_ctx->parent_ctx = parent_ctx;
9472 child_ctx->parent_gen = parent_ctx->generation;
9473 }
9474 get_ctx(child_ctx->parent_ctx);
9475 }
9476
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009477 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009478 mutex_unlock(&parent_ctx->mutex);
9479
9480 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009481 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009482
9483 return ret;
9484}
9485
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009486/*
9487 * Initialize the perf_event context in task_struct
9488 */
9489int perf_event_init_task(struct task_struct *child)
9490{
9491 int ctxn, ret;
9492
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01009493 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
9494 mutex_init(&child->perf_event_mutex);
9495 INIT_LIST_HEAD(&child->perf_event_list);
9496
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009497 for_each_task_context_nr(ctxn) {
9498 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07009499 if (ret) {
9500 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009501 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07009502 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009503 }
9504
9505 return 0;
9506}
9507
Paul Mackerras220b1402010-03-10 20:45:52 +11009508static void __init perf_event_init_all_cpus(void)
9509{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009510 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11009511 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11009512
9513 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009514 swhash = &per_cpu(swevent_htable, cpu);
9515 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00009516 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11009517 }
9518}
9519
Paul Gortmaker0db06282013-06-19 14:53:51 -04009520static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009521{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009522 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009523
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009524 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +00009525 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009526 struct swevent_hlist *hlist;
9527
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009528 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
9529 WARN_ON(!hlist);
9530 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009531 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009532 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009533}
9534
Dave Young2965faa2015-09-09 15:38:55 -07009535#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009536static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009537{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009538 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009539 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
9540 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009541
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009542 raw_spin_lock(&ctx->lock);
9543 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009544 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009545 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009546}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009547
9548static void perf_event_exit_cpu_context(int cpu)
9549{
9550 struct perf_event_context *ctx;
9551 struct pmu *pmu;
9552 int idx;
9553
9554 idx = srcu_read_lock(&pmus_srcu);
9555 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02009556 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009557
9558 mutex_lock(&ctx->mutex);
9559 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
9560 mutex_unlock(&ctx->mutex);
9561 }
9562 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009563}
9564
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009565static void perf_event_exit_cpu(int cpu)
9566{
Peter Zijlstrae3703f82014-02-24 12:06:12 +01009567 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009568}
9569#else
9570static inline void perf_event_exit_cpu(int cpu) { }
9571#endif
9572
Peter Zijlstrac2774432010-12-08 15:29:02 +01009573static int
9574perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
9575{
9576 int cpu;
9577
9578 for_each_online_cpu(cpu)
9579 perf_event_exit_cpu(cpu);
9580
9581 return NOTIFY_OK;
9582}
9583
9584/*
9585 * Run the perf reboot notifier at the very last possible moment so that
9586 * the generic watchdog code runs as long as possible.
9587 */
9588static struct notifier_block perf_reboot_notifier = {
9589 .notifier_call = perf_reboot,
9590 .priority = INT_MIN,
9591};
9592
Paul Gortmaker0db06282013-06-19 14:53:51 -04009593static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009594perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
9595{
9596 unsigned int cpu = (long)hcpu;
9597
Linus Torvalds4536e4d2011-11-03 07:44:04 -07009598 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009599
9600 case CPU_UP_PREPARE:
Peter Zijlstra1dcaac12016-03-08 17:56:05 +01009601 /*
9602 * This must be done before the CPU comes alive, because the
9603 * moment we can run tasks we can encounter (software) events.
9604 *
9605 * Specifically, someone can have inherited events on kthreadd
9606 * or a pre-existing worker thread that gets re-bound.
9607 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009608 perf_event_init_cpu(cpu);
9609 break;
9610
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009611 case CPU_DOWN_PREPARE:
Peter Zijlstra1dcaac12016-03-08 17:56:05 +01009612 /*
9613 * This must be done before the CPU dies because after that an
9614 * active event might want to IPI the CPU and that'll not work
9615 * so great for dead CPUs.
9616 *
9617 * XXX smp_call_function_single() return -ENXIO without a warn
9618 * so we could possibly deal with this.
9619 *
9620 * This is safe against new events arriving because
9621 * sys_perf_event_open() serializes against hotplug using
9622 * get_online_cpus().
9623 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009624 perf_event_exit_cpu(cpu);
9625 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009626 default:
9627 break;
9628 }
9629
9630 return NOTIFY_OK;
9631}
9632
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009633void __init perf_event_init(void)
9634{
Jason Wessel3c502e72010-11-04 17:33:01 -05009635 int ret;
9636
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009637 idr_init(&pmu_idr);
9638
Paul Mackerras220b1402010-03-10 20:45:52 +11009639 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009640 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009641 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
9642 perf_pmu_register(&perf_cpu_clock, NULL, -1);
9643 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009644 perf_tp_register();
9645 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01009646 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05009647
9648 ret = init_hw_breakpoint();
9649 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02009650
Jiri Olsab01c3a02012-03-23 15:41:20 +01009651 /*
9652 * Build time assertion that we keep the data_head at the intended
9653 * location. IOW, validation we got the __reserved[] size right.
9654 */
9655 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
9656 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009657}
Peter Zijlstraabe43402010-11-17 23:17:37 +01009658
Cody P Schaferfd979c02015-01-30 13:45:57 -08009659ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
9660 char *page)
9661{
9662 struct perf_pmu_events_attr *pmu_attr =
9663 container_of(attr, struct perf_pmu_events_attr, attr);
9664
9665 if (pmu_attr->event_str)
9666 return sprintf(page, "%s\n", pmu_attr->event_str);
9667
9668 return 0;
9669}
Thomas Gleixner675965b2016-02-22 22:19:27 +00009670EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -08009671
Peter Zijlstraabe43402010-11-17 23:17:37 +01009672static int __init perf_event_sysfs_init(void)
9673{
9674 struct pmu *pmu;
9675 int ret;
9676
9677 mutex_lock(&pmus_lock);
9678
9679 ret = bus_register(&pmu_bus);
9680 if (ret)
9681 goto unlock;
9682
9683 list_for_each_entry(pmu, &pmus, entry) {
9684 if (!pmu->name || pmu->type < 0)
9685 continue;
9686
9687 ret = pmu_dev_alloc(pmu);
9688 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
9689 }
9690 pmu_bus_running = 1;
9691 ret = 0;
9692
9693unlock:
9694 mutex_unlock(&pmus_lock);
9695
9696 return ret;
9697}
9698device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009699
9700#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04009701static struct cgroup_subsys_state *
9702perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009703{
9704 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02009705
Li Zefan1b15d052011-03-03 14:26:06 +08009706 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009707 if (!jc)
9708 return ERR_PTR(-ENOMEM);
9709
Stephane Eraniane5d13672011-02-14 11:20:01 +02009710 jc->info = alloc_percpu(struct perf_cgroup_info);
9711 if (!jc->info) {
9712 kfree(jc);
9713 return ERR_PTR(-ENOMEM);
9714 }
9715
Stephane Eraniane5d13672011-02-14 11:20:01 +02009716 return &jc->css;
9717}
9718
Tejun Heoeb954192013-08-08 20:11:23 -04009719static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009720{
Tejun Heoeb954192013-08-08 20:11:23 -04009721 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
9722
Stephane Eraniane5d13672011-02-14 11:20:01 +02009723 free_percpu(jc->info);
9724 kfree(jc);
9725}
9726
9727static int __perf_cgroup_move(void *info)
9728{
9729 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +01009730 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +02009731 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +01009732 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +02009733 return 0;
9734}
9735
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009736static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009737{
Tejun Heobb9d97b2011-12-12 18:12:21 -08009738 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009739 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -08009740
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009741 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08009742 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009743}
9744
Tejun Heo073219e2014-02-08 10:36:58 -05009745struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08009746 .css_alloc = perf_cgroup_css_alloc,
9747 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -08009748 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02009749};
9750#endif /* CONFIG_CGROUP_PERF */