blob: 52bedc5a5aaa190be3545d7534ce065ce735e473 [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, Zheng4af57ef2014-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, Zheng4af57ef2014-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 Zijlstra8dc85d5472010-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 Zijlstra8dc85d5472010-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 Zijlstra8dc85d5472010-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 Zijlstra8dc85d5472010-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 Zijlstra8dc85d5472010-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
1928 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001929 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001930
1931 /*
1932 * Unthrottle events, since we scheduled we might have missed several
1933 * ticks already, also for a heavily scheduling task there is little
1934 * guarantee it'll get a tick in a timely manner.
1935 */
1936 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1937 perf_log_throttle(event, 1);
1938 event->hw.interrupts = 0;
1939 }
1940
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001941 /*
1942 * The new state must be visible before we turn it on in the hardware:
1943 */
1944 smp_wmb();
1945
Alexander Shishkin44377272013-12-16 14:17:36 +02001946 perf_pmu_disable(event->pmu);
1947
Shaohua Li72f669c2015-02-05 15:55:31 -08001948 perf_set_shadow_time(event, ctx, tstamp);
1949
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001950 perf_log_itrace_start(event);
1951
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001952 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001953 event->state = PERF_EVENT_STATE_INACTIVE;
1954 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001955 ret = -EAGAIN;
1956 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001957 }
1958
Peter Zijlstra00a29162015-07-27 10:35:07 +02001959 event->tstamp_running += tstamp - event->tstamp_stopped;
1960
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001961 if (!is_software_event(event))
1962 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001963 if (!ctx->nr_active++)
1964 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001965 if (event->attr.freq && event->attr.sample_freq)
1966 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001967
1968 if (event->attr.exclusive)
1969 cpuctx->exclusive = 1;
1970
Alexander Shishkin44377272013-12-16 14:17:36 +02001971out:
1972 perf_pmu_enable(event->pmu);
1973
1974 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001975}
1976
1977static int
1978group_sched_in(struct perf_event *group_event,
1979 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001980 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001981{
Lin Ming6bde9b62010-04-23 13:56:00 +08001982 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001983 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001984 u64 now = ctx->time;
1985 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001986
1987 if (group_event->state == PERF_EVENT_STATE_OFF)
1988 return 0;
1989
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001990 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08001991
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001992 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001993 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001994 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001995 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001996 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001997
1998 /*
1999 * Schedule in siblings as one group (if any):
2000 */
2001 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002002 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002003 partial_group = event;
2004 goto group_error;
2005 }
2006 }
2007
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002008 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002009 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002010
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002011group_error:
2012 /*
2013 * Groups can be scheduled in as one unit only, so undo any
2014 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02002015 * The events up to the failed event are scheduled out normally,
2016 * tstamp_stopped will be updated.
2017 *
2018 * The failed events and the remaining siblings need to have
2019 * their timings updated as if they had gone thru event_sched_in()
2020 * and event_sched_out(). This is required to get consistent timings
2021 * across the group. This also takes care of the case where the group
2022 * could never be scheduled by ensuring tstamp_stopped is set to mark
2023 * the time the event was actually stopped, such that time delta
2024 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002025 */
2026 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2027 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02002028 simulate = true;
2029
2030 if (simulate) {
2031 event->tstamp_running += now - event->tstamp_stopped;
2032 event->tstamp_stopped = now;
2033 } else {
2034 event_sched_out(event, cpuctx, ctx);
2035 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002036 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002037 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002038
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002039 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002040
Peter Zijlstra272325c2015-04-15 11:41:58 +02002041 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002042
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002043 return -EAGAIN;
2044}
2045
2046/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002047 * Work out whether we can put this event group on the CPU now.
2048 */
2049static int group_can_go_on(struct perf_event *event,
2050 struct perf_cpu_context *cpuctx,
2051 int can_add_hw)
2052{
2053 /*
2054 * Groups consisting entirely of software events can always go on.
2055 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01002056 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002057 return 1;
2058 /*
2059 * If an exclusive group is already on, no other hardware
2060 * events can go on.
2061 */
2062 if (cpuctx->exclusive)
2063 return 0;
2064 /*
2065 * If this group is exclusive and there are already
2066 * events on the CPU, it can't go on.
2067 */
2068 if (event->attr.exclusive && cpuctx->active_oncpu)
2069 return 0;
2070 /*
2071 * Otherwise, try to add it if all previous groups were able
2072 * to go on.
2073 */
2074 return can_add_hw;
2075}
2076
2077static void add_event_to_ctx(struct perf_event *event,
2078 struct perf_event_context *ctx)
2079{
Stephane Eranian41587552011-01-03 18:20:01 +02002080 u64 tstamp = perf_event_time(event);
2081
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002082 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002083 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002084 event->tstamp_enabled = tstamp;
2085 event->tstamp_running = tstamp;
2086 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002087}
2088
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002089static void ctx_sched_out(struct perf_event_context *ctx,
2090 struct perf_cpu_context *cpuctx,
2091 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002092static void
2093ctx_sched_in(struct perf_event_context *ctx,
2094 struct perf_cpu_context *cpuctx,
2095 enum event_type_t event_type,
2096 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002097
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002098static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2099 struct perf_event_context *ctx)
2100{
2101 if (!cpuctx->task_ctx)
2102 return;
2103
2104 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2105 return;
2106
2107 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2108}
2109
Peter Zijlstradce58552011-04-09 21:17:46 +02002110static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2111 struct perf_event_context *ctx,
2112 struct task_struct *task)
2113{
2114 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2115 if (ctx)
2116 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2117 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2118 if (ctx)
2119 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2120}
2121
Peter Zijlstra3e349502016-01-08 10:01:18 +01002122static void ctx_resched(struct perf_cpu_context *cpuctx,
2123 struct perf_event_context *task_ctx)
Peter Zijlstra00179602015-11-30 16:26:35 +01002124{
Peter Zijlstra3e349502016-01-08 10:01:18 +01002125 perf_pmu_disable(cpuctx->ctx.pmu);
2126 if (task_ctx)
2127 task_ctx_sched_out(cpuctx, task_ctx);
2128 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2129 perf_event_sched_in(cpuctx, task_ctx, current);
2130 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002131}
2132
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002133/*
2134 * Cross CPU call to install and enable a performance event
2135 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002136 * Very similar to remote_function() + event_function() but cannot assume that
2137 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002138 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002139static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002140{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002141 struct perf_event *event = info;
2142 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002143 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002144 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002145 bool activate = true;
2146 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002147
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002148 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002149 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002150 raw_spin_lock(&ctx->lock);
2151 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002152
2153 /* If we're on the wrong CPU, try again */
2154 if (task_cpu(ctx->task) != smp_processor_id()) {
2155 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002156 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002157 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002158
Peter Zijlstra39a43642016-01-11 12:46:35 +01002159 /*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002160 * If we're on the right CPU, see if the task we target is
2161 * current, if not we don't have to activate the ctx, a future
2162 * context switch will do that for us.
Peter Zijlstra39a43642016-01-11 12:46:35 +01002163 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002164 if (ctx->task != current)
2165 activate = false;
2166 else
2167 WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx);
2168
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002169 } else if (task_ctx) {
2170 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002171 }
2172
Peter Zijlstraa0963092016-02-24 18:45:50 +01002173 if (activate) {
2174 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2175 add_event_to_ctx(event, ctx);
2176 ctx_resched(cpuctx, task_ctx);
2177 } else {
2178 add_event_to_ctx(event, ctx);
2179 }
2180
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002181unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002182 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002183
Peter Zijlstraa0963092016-02-24 18:45:50 +01002184 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002185}
2186
2187/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002188 * Attach a performance event to a context.
2189 *
2190 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002191 */
2192static void
2193perf_install_in_context(struct perf_event_context *ctx,
2194 struct perf_event *event,
2195 int cpu)
2196{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002197 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002198
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002199 lockdep_assert_held(&ctx->mutex);
2200
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002201 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002202 if (event->cpu != -1)
2203 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002204
Peter Zijlstraa0963092016-02-24 18:45:50 +01002205 if (!task) {
2206 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002207 return;
2208 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002209
Peter Zijlstraa0963092016-02-24 18:45:50 +01002210 /*
2211 * Should not happen, we validate the ctx is still alive before calling.
2212 */
2213 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2214 return;
2215
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002216 /*
2217 * Installing events is tricky because we cannot rely on ctx->is_active
2218 * to be set in case this is the nr_events 0 -> 1 transition.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002219 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002220again:
2221 /*
2222 * Cannot use task_function_call() because we need to run on the task's
2223 * CPU regardless of whether its current or not.
2224 */
2225 if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event))
2226 return;
2227
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002228 raw_spin_lock_irq(&ctx->lock);
2229 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002230 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2231 /*
2232 * Cannot happen because we already checked above (which also
2233 * cannot happen), and we hold ctx->mutex, which serializes us
2234 * against perf_event_exit_task_context().
2235 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002236 raw_spin_unlock_irq(&ctx->lock);
2237 return;
2238 }
Peter Zijlstra39a43642016-01-11 12:46:35 +01002239 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstraa0963092016-02-24 18:45:50 +01002240 /*
2241 * Since !ctx->is_active doesn't mean anything, we must IPI
2242 * unconditionally.
2243 */
2244 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002245}
2246
2247/*
2248 * Put a event into inactive state and update time fields.
2249 * Enabling the leader of a group effectively enables all
2250 * the group members that aren't explicitly disabled, so we
2251 * have to update their ->tstamp_enabled also.
2252 * Note: this works for group members as well as group leaders
2253 * since the non-leader members' sibling_lists will be empty.
2254 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002255static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002256{
2257 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002258 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002259
2260 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002261 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002262 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002263 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2264 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002265 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002266}
2267
2268/*
2269 * Cross CPU call to enable a performance event
2270 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002271static void __perf_event_enable(struct perf_event *event,
2272 struct perf_cpu_context *cpuctx,
2273 struct perf_event_context *ctx,
2274 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002275{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002276 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002277 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002278
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002279 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2280 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002281 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002282
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002283 if (ctx->is_active)
2284 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2285
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002286 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002287
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002288 if (!ctx->is_active)
2289 return;
2290
Stephane Eraniane5d13672011-02-14 11:20:01 +02002291 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002292 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02002293 perf_cgroup_defer_enabled(event);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002294 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002295 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002296 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002297
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002298 /*
2299 * If the event is in a group and isn't the group leader,
2300 * then don't put it on unless the group is on.
2301 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002302 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2303 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002304 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002305 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002306
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002307 task_ctx = cpuctx->task_ctx;
2308 if (ctx->task)
2309 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002310
Peter Zijlstraaee7dbc2016-01-08 10:45:11 +01002311 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002312}
2313
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002314/*
2315 * Enable a event.
2316 *
2317 * If event->ctx is a cloned context, callers must make sure that
2318 * every task struct that event->ctx->task could possibly point to
2319 * remains valid. This condition is satisfied when called through
2320 * perf_event_for_each_child or perf_event_for_each as described
2321 * for perf_event_disable.
2322 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002323static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002324{
2325 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002326
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002327 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002328 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2329 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002330 raw_spin_unlock_irq(&ctx->lock);
2331 return;
2332 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002333
2334 /*
2335 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002336 *
2337 * That way, if we see the event in error state below, we know that it
2338 * has gone back into error state, as distinct from the task having
2339 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002340 */
2341 if (event->state == PERF_EVENT_STATE_ERROR)
2342 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002343 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002344
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002345 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002346}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002347
2348/*
2349 * See perf_event_disable();
2350 */
2351void perf_event_enable(struct perf_event *event)
2352{
2353 struct perf_event_context *ctx;
2354
2355 ctx = perf_event_ctx_lock(event);
2356 _perf_event_enable(event);
2357 perf_event_ctx_unlock(event, ctx);
2358}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002359EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002360
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002361static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002362{
2363 /*
2364 * not supported on inherited events
2365 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002366 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002367 return -EINVAL;
2368
2369 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002370 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002371
2372 return 0;
2373}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002374
2375/*
2376 * See perf_event_disable()
2377 */
2378int perf_event_refresh(struct perf_event *event, int refresh)
2379{
2380 struct perf_event_context *ctx;
2381 int ret;
2382
2383 ctx = perf_event_ctx_lock(event);
2384 ret = _perf_event_refresh(event, refresh);
2385 perf_event_ctx_unlock(event, ctx);
2386
2387 return ret;
2388}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002389EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002390
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002391static void ctx_sched_out(struct perf_event_context *ctx,
2392 struct perf_cpu_context *cpuctx,
2393 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002394{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002395 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002396 struct perf_event *event;
2397
2398 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002399
Peter Zijlstra39a43642016-01-11 12:46:35 +01002400 if (likely(!ctx->nr_events)) {
2401 /*
2402 * See __perf_remove_from_context().
2403 */
2404 WARN_ON_ONCE(ctx->is_active);
2405 if (ctx->task)
2406 WARN_ON_ONCE(cpuctx->task_ctx);
2407 return;
2408 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002409
Peter Zijlstradb24d332011-04-09 21:17:45 +02002410 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002411 if (!(ctx->is_active & EVENT_ALL))
2412 ctx->is_active = 0;
2413
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002414 if (ctx->task) {
2415 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2416 if (!ctx->is_active)
2417 cpuctx->task_ctx = NULL;
2418 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002419
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002420 /*
2421 * Always update time if it was set; not only when it changes.
2422 * Otherwise we can 'forget' to update time for any but the last
2423 * context we sched out. For example:
2424 *
2425 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2426 * ctx_sched_out(.event_type = EVENT_PINNED)
2427 *
2428 * would only update time for the pinned events.
2429 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002430 if (is_active & EVENT_TIME) {
2431 /* update (and stop) ctx time */
2432 update_context_time(ctx);
2433 update_cgrp_time_from_cpuctx(cpuctx);
2434 }
2435
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002436 is_active ^= ctx->is_active; /* changed bits */
2437
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002438 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002439 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002440
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002441 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002442 if (is_active & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002443 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2444 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002445 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002446
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002447 if (is_active & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002448 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002449 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002450 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002451 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002452}
2453
2454/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002455 * Test whether two contexts are equivalent, i.e. whether they have both been
2456 * cloned from the same version of the same context.
2457 *
2458 * Equivalence is measured using a generation number in the context that is
2459 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2460 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002461 */
2462static int context_equiv(struct perf_event_context *ctx1,
2463 struct perf_event_context *ctx2)
2464{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002465 lockdep_assert_held(&ctx1->lock);
2466 lockdep_assert_held(&ctx2->lock);
2467
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002468 /* Pinning disables the swap optimization */
2469 if (ctx1->pin_count || ctx2->pin_count)
2470 return 0;
2471
2472 /* If ctx1 is the parent of ctx2 */
2473 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2474 return 1;
2475
2476 /* If ctx2 is the parent of ctx1 */
2477 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2478 return 1;
2479
2480 /*
2481 * If ctx1 and ctx2 have the same parent; we flatten the parent
2482 * hierarchy, see perf_event_init_context().
2483 */
2484 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2485 ctx1->parent_gen == ctx2->parent_gen)
2486 return 1;
2487
2488 /* Unmatched */
2489 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002490}
2491
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002492static void __perf_event_sync_stat(struct perf_event *event,
2493 struct perf_event *next_event)
2494{
2495 u64 value;
2496
2497 if (!event->attr.inherit_stat)
2498 return;
2499
2500 /*
2501 * Update the event value, we cannot use perf_event_read()
2502 * because we're in the middle of a context switch and have IRQs
2503 * disabled, which upsets smp_call_function_single(), however
2504 * we know the event must be on the current CPU, therefore we
2505 * don't need to use it.
2506 */
2507 switch (event->state) {
2508 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002509 event->pmu->read(event);
2510 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002511
2512 case PERF_EVENT_STATE_INACTIVE:
2513 update_event_times(event);
2514 break;
2515
2516 default:
2517 break;
2518 }
2519
2520 /*
2521 * In order to keep per-task stats reliable we need to flip the event
2522 * values when we flip the contexts.
2523 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002524 value = local64_read(&next_event->count);
2525 value = local64_xchg(&event->count, value);
2526 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002527
2528 swap(event->total_time_enabled, next_event->total_time_enabled);
2529 swap(event->total_time_running, next_event->total_time_running);
2530
2531 /*
2532 * Since we swizzled the values, update the user visible data too.
2533 */
2534 perf_event_update_userpage(event);
2535 perf_event_update_userpage(next_event);
2536}
2537
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002538static void perf_event_sync_stat(struct perf_event_context *ctx,
2539 struct perf_event_context *next_ctx)
2540{
2541 struct perf_event *event, *next_event;
2542
2543 if (!ctx->nr_stat)
2544 return;
2545
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002546 update_context_time(ctx);
2547
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002548 event = list_first_entry(&ctx->event_list,
2549 struct perf_event, event_entry);
2550
2551 next_event = list_first_entry(&next_ctx->event_list,
2552 struct perf_event, event_entry);
2553
2554 while (&event->event_entry != &ctx->event_list &&
2555 &next_event->event_entry != &next_ctx->event_list) {
2556
2557 __perf_event_sync_stat(event, next_event);
2558
2559 event = list_next_entry(event, event_entry);
2560 next_event = list_next_entry(next_event, event_entry);
2561 }
2562}
2563
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002564static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2565 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002566{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002567 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002568 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002569 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002570 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002571 int do_switch = 1;
2572
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002573 if (likely(!ctx))
2574 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002575
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002576 cpuctx = __get_cpu_context(ctx);
2577 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002578 return;
2579
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002580 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002581 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002582 if (!next_ctx)
2583 goto unlock;
2584
2585 parent = rcu_dereference(ctx->parent_ctx);
2586 next_parent = rcu_dereference(next_ctx->parent_ctx);
2587
2588 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002589 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002590 goto unlock;
2591
2592 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002593 /*
2594 * Looks like the two contexts are clones, so we might be
2595 * able to optimize the context switch. We lock both
2596 * contexts and check that they are clones under the
2597 * lock (including re-checking that neither has been
2598 * uncloned in the meantime). It doesn't matter which
2599 * order we take the locks because no other cpu could
2600 * be trying to lock both of these tasks.
2601 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002602 raw_spin_lock(&ctx->lock);
2603 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002604 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002605 WRITE_ONCE(ctx->task, next);
2606 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05002607
2608 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2609
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002610 /*
2611 * RCU_INIT_POINTER here is safe because we've not
2612 * modified the ctx and the above modification of
2613 * ctx->task and ctx->task_ctx_data are immaterial
2614 * since those values are always verified under
2615 * ctx->lock which we're now holding.
2616 */
2617 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2618 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2619
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002620 do_switch = 0;
2621
2622 perf_event_sync_stat(ctx, next_ctx);
2623 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002624 raw_spin_unlock(&next_ctx->lock);
2625 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002626 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002627unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002628 rcu_read_unlock();
2629
2630 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002631 raw_spin_lock(&ctx->lock);
Peter Zijlstra8833d0e2016-01-08 10:02:37 +01002632 task_ctx_sched_out(cpuctx, ctx);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002633 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002634 }
2635}
2636
Yan, Zhengba532502014-11-04 21:55:58 -05002637void perf_sched_cb_dec(struct pmu *pmu)
2638{
2639 this_cpu_dec(perf_sched_cb_usages);
2640}
2641
2642void perf_sched_cb_inc(struct pmu *pmu)
2643{
2644 this_cpu_inc(perf_sched_cb_usages);
2645}
2646
2647/*
2648 * This function provides the context switch callback to the lower code
2649 * layer. It is invoked ONLY when the context switch callback is enabled.
2650 */
2651static void perf_pmu_sched_task(struct task_struct *prev,
2652 struct task_struct *next,
2653 bool sched_in)
2654{
2655 struct perf_cpu_context *cpuctx;
2656 struct pmu *pmu;
2657 unsigned long flags;
2658
2659 if (prev == next)
2660 return;
2661
2662 local_irq_save(flags);
2663
2664 rcu_read_lock();
2665
2666 list_for_each_entry_rcu(pmu, &pmus, entry) {
2667 if (pmu->sched_task) {
2668 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2669
2670 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2671
2672 perf_pmu_disable(pmu);
2673
2674 pmu->sched_task(cpuctx->task_ctx, sched_in);
2675
2676 perf_pmu_enable(pmu);
2677
2678 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2679 }
2680 }
2681
2682 rcu_read_unlock();
2683
2684 local_irq_restore(flags);
2685}
2686
Adrian Hunter45ac1402015-07-21 12:44:02 +03002687static void perf_event_switch(struct task_struct *task,
2688 struct task_struct *next_prev, bool sched_in);
2689
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002690#define for_each_task_context_nr(ctxn) \
2691 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2692
2693/*
2694 * Called from scheduler to remove the events of the current task,
2695 * with interrupts disabled.
2696 *
2697 * We stop each event and update the event value in event->count.
2698 *
2699 * This does not protect us against NMI, but disable()
2700 * sets the disabled bit in the control field of event _before_
2701 * accessing the event control register. If a NMI hits, then it will
2702 * not restart the event.
2703 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002704void __perf_event_task_sched_out(struct task_struct *task,
2705 struct task_struct *next)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002706{
2707 int ctxn;
2708
Yan, Zhengba532502014-11-04 21:55:58 -05002709 if (__this_cpu_read(perf_sched_cb_usages))
2710 perf_pmu_sched_task(task, next, false);
2711
Adrian Hunter45ac1402015-07-21 12:44:02 +03002712 if (atomic_read(&nr_switch_events))
2713 perf_event_switch(task, next, false);
2714
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002715 for_each_task_context_nr(ctxn)
2716 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002717
2718 /*
2719 * if cgroup events exist on this CPU, then we need
2720 * to check if we have to switch out PMU state.
2721 * cgroup event are system-wide mode only
2722 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002723 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002724 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002725}
2726
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002727/*
2728 * Called with IRQs disabled
2729 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002730static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2731 enum event_type_t event_type)
2732{
2733 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002734}
2735
2736static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002737ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002738 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002739{
2740 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002741
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002742 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2743 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002744 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002745 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002746 continue;
2747
Stephane Eraniane5d13672011-02-14 11:20:01 +02002748 /* may need to reset tstamp_enabled */
2749 if (is_cgroup_event(event))
2750 perf_cgroup_mark_enabled(event, ctx);
2751
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002752 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002753 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002754
2755 /*
2756 * If this pinned group hasn't been scheduled,
2757 * put it in error state.
2758 */
2759 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2760 update_group_times(event);
2761 event->state = PERF_EVENT_STATE_ERROR;
2762 }
2763 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002764}
2765
2766static void
2767ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002768 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002769{
2770 struct perf_event *event;
2771 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002772
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002773 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2774 /* Ignore events in OFF or ERROR state */
2775 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002776 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002777 /*
2778 * Listen to the 'cpu' scheduling filter constraint
2779 * of events:
2780 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002781 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002782 continue;
2783
Stephane Eraniane5d13672011-02-14 11:20:01 +02002784 /* may need to reset tstamp_enabled */
2785 if (is_cgroup_event(event))
2786 perf_cgroup_mark_enabled(event, ctx);
2787
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002788 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002789 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002790 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002791 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002792 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002793}
2794
2795static void
2796ctx_sched_in(struct perf_event_context *ctx,
2797 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002798 enum event_type_t event_type,
2799 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002800{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002801 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002802 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002803
Peter Zijlstrac994d612016-01-08 09:20:23 +01002804 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002805
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002806 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002807 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002808
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002809 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002810 if (ctx->task) {
2811 if (!is_active)
2812 cpuctx->task_ctx = ctx;
2813 else
2814 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2815 }
2816
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002817 is_active ^= ctx->is_active; /* changed bits */
2818
2819 if (is_active & EVENT_TIME) {
2820 /* start ctx time */
2821 now = perf_clock();
2822 ctx->timestamp = now;
2823 perf_cgroup_set_timestamp(task, ctx);
2824 }
2825
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002826 /*
2827 * First go through the list and put on any pinned groups
2828 * in order to give them the best chance of going on.
2829 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002830 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002831 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002832
2833 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002834 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002835 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002836}
2837
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002838static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002839 enum event_type_t event_type,
2840 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002841{
2842 struct perf_event_context *ctx = &cpuctx->ctx;
2843
Stephane Eraniane5d13672011-02-14 11:20:01 +02002844 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002845}
2846
Stephane Eraniane5d13672011-02-14 11:20:01 +02002847static void perf_event_context_sched_in(struct perf_event_context *ctx,
2848 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002849{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002850 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002851
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002852 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002853 if (cpuctx->task_ctx == ctx)
2854 return;
2855
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002856 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002857 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002858 /*
2859 * We want to keep the following priority order:
2860 * cpu pinned (that don't need to move), task pinned,
2861 * cpu flexible, task flexible.
2862 */
2863 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002864 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002865 perf_pmu_enable(ctx->pmu);
2866 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002867}
2868
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002869/*
2870 * Called from scheduler to add the events of the current task
2871 * with interrupts disabled.
2872 *
2873 * We restore the event value and then enable it.
2874 *
2875 * This does not protect us against NMI, but enable()
2876 * sets the enabled bit in the control field of event _before_
2877 * accessing the event control register. If a NMI hits, then it will
2878 * keep the event running.
2879 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002880void __perf_event_task_sched_in(struct task_struct *prev,
2881 struct task_struct *task)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002882{
2883 struct perf_event_context *ctx;
2884 int ctxn;
2885
Peter Zijlstra7e41d172016-01-08 09:21:40 +01002886 /*
2887 * If cgroup events exist on this CPU, then we need to check if we have
2888 * to switch in PMU state; cgroup event are system-wide mode only.
2889 *
2890 * Since cgroup events are CPU events, we must schedule these in before
2891 * we schedule in the task events.
2892 */
2893 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
2894 perf_cgroup_sched_in(prev, task);
2895
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002896 for_each_task_context_nr(ctxn) {
2897 ctx = task->perf_event_ctxp[ctxn];
2898 if (likely(!ctx))
2899 continue;
2900
Stephane Eraniane5d13672011-02-14 11:20:01 +02002901 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002902 }
Stephane Eraniand010b332012-02-09 23:21:00 +01002903
Adrian Hunter45ac1402015-07-21 12:44:02 +03002904 if (atomic_read(&nr_switch_events))
2905 perf_event_switch(task, prev, true);
2906
Yan, Zhengba532502014-11-04 21:55:58 -05002907 if (__this_cpu_read(perf_sched_cb_usages))
2908 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002909}
2910
Peter Zijlstraabd50712010-01-26 18:50:16 +01002911static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2912{
2913 u64 frequency = event->attr.sample_freq;
2914 u64 sec = NSEC_PER_SEC;
2915 u64 divisor, dividend;
2916
2917 int count_fls, nsec_fls, frequency_fls, sec_fls;
2918
2919 count_fls = fls64(count);
2920 nsec_fls = fls64(nsec);
2921 frequency_fls = fls64(frequency);
2922 sec_fls = 30;
2923
2924 /*
2925 * We got @count in @nsec, with a target of sample_freq HZ
2926 * the target period becomes:
2927 *
2928 * @count * 10^9
2929 * period = -------------------
2930 * @nsec * sample_freq
2931 *
2932 */
2933
2934 /*
2935 * Reduce accuracy by one bit such that @a and @b converge
2936 * to a similar magnitude.
2937 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002938#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002939do { \
2940 if (a##_fls > b##_fls) { \
2941 a >>= 1; \
2942 a##_fls--; \
2943 } else { \
2944 b >>= 1; \
2945 b##_fls--; \
2946 } \
2947} while (0)
2948
2949 /*
2950 * Reduce accuracy until either term fits in a u64, then proceed with
2951 * the other, so that finally we can do a u64/u64 division.
2952 */
2953 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2954 REDUCE_FLS(nsec, frequency);
2955 REDUCE_FLS(sec, count);
2956 }
2957
2958 if (count_fls + sec_fls > 64) {
2959 divisor = nsec * frequency;
2960
2961 while (count_fls + sec_fls > 64) {
2962 REDUCE_FLS(count, sec);
2963 divisor >>= 1;
2964 }
2965
2966 dividend = count * sec;
2967 } else {
2968 dividend = count * sec;
2969
2970 while (nsec_fls + frequency_fls > 64) {
2971 REDUCE_FLS(nsec, frequency);
2972 dividend >>= 1;
2973 }
2974
2975 divisor = nsec * frequency;
2976 }
2977
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02002978 if (!divisor)
2979 return dividend;
2980
Peter Zijlstraabd50712010-01-26 18:50:16 +01002981 return div64_u64(dividend, divisor);
2982}
2983
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002984static DEFINE_PER_CPU(int, perf_throttled_count);
2985static DEFINE_PER_CPU(u64, perf_throttled_seq);
2986
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002987static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002988{
2989 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02002990 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002991 s64 delta;
2992
Peter Zijlstraabd50712010-01-26 18:50:16 +01002993 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002994
2995 delta = (s64)(period - hwc->sample_period);
2996 delta = (delta + 7) / 8; /* low pass filter */
2997
2998 sample_period = hwc->sample_period + delta;
2999
3000 if (!sample_period)
3001 sample_period = 1;
3002
3003 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003004
Peter Zijlstrae7850592010-05-21 14:43:08 +02003005 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003006 if (disable)
3007 event->pmu->stop(event, PERF_EF_UPDATE);
3008
Peter Zijlstrae7850592010-05-21 14:43:08 +02003009 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003010
3011 if (disable)
3012 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003013 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003014}
3015
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003016/*
3017 * combine freq adjustment with unthrottling to avoid two passes over the
3018 * events. At the same time, make sure, having freq events does not change
3019 * the rate of unthrottling as that would introduce bias.
3020 */
3021static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3022 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003023{
3024 struct perf_event *event;
3025 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003026 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003027 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003028
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003029 /*
3030 * only need to iterate over all events iff:
3031 * - context have events in frequency mode (needs freq adjust)
3032 * - there are events to unthrottle on this cpu
3033 */
3034 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003035 return;
3036
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003037 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003038 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003039
Paul Mackerras03541f82009-10-14 16:58:03 +11003040 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003041 if (event->state != PERF_EVENT_STATE_ACTIVE)
3042 continue;
3043
Stephane Eranian5632ab12011-01-03 18:20:01 +02003044 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003045 continue;
3046
Alexander Shishkin44377272013-12-16 14:17:36 +02003047 perf_pmu_disable(event->pmu);
3048
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003049 hwc = &event->hw;
3050
Jiri Olsaae23bff2013-08-24 16:45:54 +02003051 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003052 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003053 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003054 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003055 }
3056
3057 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003058 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003059
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003060 /*
3061 * stop the event and update event->count
3062 */
3063 event->pmu->stop(event, PERF_EF_UPDATE);
3064
Peter Zijlstrae7850592010-05-21 14:43:08 +02003065 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003066 delta = now - hwc->freq_count_stamp;
3067 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003068
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003069 /*
3070 * restart the event
3071 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003072 * we have stopped the event so tell that
3073 * to perf_adjust_period() to avoid stopping it
3074 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003075 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003076 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003077 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003078
3079 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003080 next:
3081 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003082 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003083
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003084 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003085 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003086}
3087
3088/*
3089 * Round-robin a context's events:
3090 */
3091static void rotate_ctx(struct perf_event_context *ctx)
3092{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003093 /*
3094 * Rotate the first entry last of non-pinned groups. Rotation might be
3095 * disabled by the inheritance code.
3096 */
3097 if (!ctx->rotate_disable)
3098 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003099}
3100
Stephane Eranian9e630202013-04-03 14:21:33 +02003101static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003102{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003103 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003104 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003105
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003106 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003107 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3108 rotate = 1;
3109 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003110
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003111 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003112 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003113 if (ctx->nr_events != ctx->nr_active)
3114 rotate = 1;
3115 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003116
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003117 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003118 goto done;
3119
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003120 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003121 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003122
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003123 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3124 if (ctx)
3125 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003126
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003127 rotate_ctx(&cpuctx->ctx);
3128 if (ctx)
3129 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003130
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003131 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003132
3133 perf_pmu_enable(cpuctx->ctx.pmu);
3134 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003135done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003136
3137 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003138}
3139
3140void perf_event_task_tick(void)
3141{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003142 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3143 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003144 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003145
3146 WARN_ON(!irqs_disabled());
3147
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003148 __this_cpu_inc(perf_throttled_seq);
3149 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003150 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003151
Mark Rutland2fde4f92015-01-07 15:01:54 +00003152 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003153 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003154}
3155
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003156static int event_enable_on_exec(struct perf_event *event,
3157 struct perf_event_context *ctx)
3158{
3159 if (!event->attr.enable_on_exec)
3160 return 0;
3161
3162 event->attr.enable_on_exec = 0;
3163 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3164 return 0;
3165
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003166 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003167
3168 return 1;
3169}
3170
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003171/*
3172 * Enable all of a task's events that have been marked enable-on-exec.
3173 * This expects task == current.
3174 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003175static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003176{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003177 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003178 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003179 struct perf_event *event;
3180 unsigned long flags;
3181 int enabled = 0;
3182
3183 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003184 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003185 if (!ctx || !ctx->nr_events)
3186 goto out;
3187
Peter Zijlstra3e349502016-01-08 10:01:18 +01003188 cpuctx = __get_cpu_context(ctx);
3189 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01003190 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003191 list_for_each_entry(event, &ctx->event_list, event_entry)
3192 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003193
3194 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003195 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003196 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003197 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003198 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003199 ctx_resched(cpuctx, ctx);
3200 }
3201 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003202
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003203out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003204 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003205
3206 if (clone_ctx)
3207 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003208}
3209
Peter Zijlstrae041e322014-05-21 17:32:19 +02003210void perf_event_exec(void)
3211{
Peter Zijlstrae041e322014-05-21 17:32:19 +02003212 int ctxn;
3213
3214 rcu_read_lock();
Peter Zijlstrac1274492015-12-10 20:57:40 +01003215 for_each_task_context_nr(ctxn)
3216 perf_event_enable_on_exec(ctxn);
Peter Zijlstrae041e322014-05-21 17:32:19 +02003217 rcu_read_unlock();
3218}
3219
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003220struct perf_read_data {
3221 struct perf_event *event;
3222 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003223 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003224};
3225
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003226/*
3227 * Cross CPU call to read the hardware event
3228 */
3229static void __perf_event_read(void *info)
3230{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003231 struct perf_read_data *data = info;
3232 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003233 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003234 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003235 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003236
3237 /*
3238 * If this is a task context, we need to check whether it is
3239 * the current task context of this cpu. If not it has been
3240 * scheduled out before the smp call arrived. In that case
3241 * event->count would have been updated to a recent sample
3242 * when the event was scheduled out.
3243 */
3244 if (ctx->task && cpuctx->task_ctx != ctx)
3245 return;
3246
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003247 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003248 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003249 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003250 update_cgrp_time_from_event(event);
3251 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003252
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003253 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003254 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003255 goto unlock;
3256
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003257 if (!data->group) {
3258 pmu->read(event);
3259 data->ret = 0;
3260 goto unlock;
3261 }
3262
3263 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3264
3265 pmu->read(event);
3266
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003267 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3268 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003269 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3270 /*
3271 * Use sibling's PMU rather than @event's since
3272 * sibling could be on different (eg: software) PMU.
3273 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003274 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003275 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003276 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003277
3278 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003279
3280unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003281 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003282}
3283
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003284static inline u64 perf_event_count(struct perf_event *event)
3285{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003286 if (event->pmu->count)
3287 return event->pmu->count(event);
3288
3289 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003290}
3291
Kaixu Xiaffe86902015-08-06 07:02:32 +00003292/*
3293 * NMI-safe method to read a local event, that is an event that
3294 * is:
3295 * - either for the current task, or for this CPU
3296 * - does not have inherit set, for inherited task events
3297 * will not be local and we cannot read them atomically
3298 * - must not have a pmu::count method
3299 */
3300u64 perf_event_read_local(struct perf_event *event)
3301{
3302 unsigned long flags;
3303 u64 val;
3304
3305 /*
3306 * Disabling interrupts avoids all counter scheduling (context
3307 * switches, timer based rotation and IPIs).
3308 */
3309 local_irq_save(flags);
3310
3311 /* If this is a per-task event, it must be for current */
3312 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3313 event->hw.target != current);
3314
3315 /* If this is a per-CPU event, it must be for this CPU */
3316 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3317 event->cpu != smp_processor_id());
3318
3319 /*
3320 * It must not be an event with inherit set, we cannot read
3321 * all child counters from atomic context.
3322 */
3323 WARN_ON_ONCE(event->attr.inherit);
3324
3325 /*
3326 * It must not have a pmu::count method, those are not
3327 * NMI safe.
3328 */
3329 WARN_ON_ONCE(event->pmu->count);
3330
3331 /*
3332 * If the event is currently on this CPU, its either a per-task event,
3333 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3334 * oncpu == -1).
3335 */
3336 if (event->oncpu == smp_processor_id())
3337 event->pmu->read(event);
3338
3339 val = local64_read(&event->count);
3340 local_irq_restore(flags);
3341
3342 return val;
3343}
3344
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003345static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003346{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003347 int ret = 0;
3348
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003349 /*
3350 * If event is enabled and currently active on a CPU, update the
3351 * value in the event structure:
3352 */
3353 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003354 struct perf_read_data data = {
3355 .event = event,
3356 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003357 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003358 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003359 smp_call_function_single(event->oncpu,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003360 __perf_event_read, &data, 1);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003361 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003362 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003363 struct perf_event_context *ctx = event->ctx;
3364 unsigned long flags;
3365
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003366 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003367 /*
3368 * may read while context is not active
3369 * (e.g., thread is blocked), in that case
3370 * we cannot update context time
3371 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003372 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003373 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003374 update_cgrp_time_from_event(event);
3375 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003376 if (group)
3377 update_group_times(event);
3378 else
3379 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003380 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003381 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003382
3383 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003384}
3385
3386/*
3387 * Initialize the perf_event context in a task_struct:
3388 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003389static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003390{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003391 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003392 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003393 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003394 INIT_LIST_HEAD(&ctx->pinned_groups);
3395 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003396 INIT_LIST_HEAD(&ctx->event_list);
3397 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003398}
3399
Peter Zijlstraeb184472010-09-07 15:55:13 +02003400static struct perf_event_context *
3401alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402{
3403 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003404
3405 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3406 if (!ctx)
3407 return NULL;
3408
3409 __perf_event_init_context(ctx);
3410 if (task) {
3411 ctx->task = task;
3412 get_task_struct(task);
3413 }
3414 ctx->pmu = pmu;
3415
3416 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003417}
3418
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003419static struct task_struct *
3420find_lively_task_by_vpid(pid_t vpid)
3421{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003422 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003423 int err;
3424
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003425 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003426 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003427 task = current;
3428 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003429 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003430 if (task)
3431 get_task_struct(task);
3432 rcu_read_unlock();
3433
3434 if (!task)
3435 return ERR_PTR(-ESRCH);
3436
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003437 /* Reuse ptrace permission checks for now. */
3438 err = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08003439 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003440 goto errout;
3441
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003442 return task;
3443errout:
3444 put_task_struct(task);
3445 return ERR_PTR(err);
3446
3447}
3448
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003449/*
3450 * Returns a matching context with refcount and pincount.
3451 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003452static struct perf_event_context *
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003453find_get_context(struct pmu *pmu, struct task_struct *task,
3454 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003455{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003456 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003457 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003458 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003459 unsigned long flags;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003460 int ctxn, err;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003461 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003462
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003463 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003464 /* Must be root to operate on a CPU event: */
3465 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3466 return ERR_PTR(-EACCES);
3467
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003468 /*
3469 * We could be clever and allow to attach a event to an
3470 * offline CPU and activate it when the CPU comes up, but
3471 * that's for later.
3472 */
3473 if (!cpu_online(cpu))
3474 return ERR_PTR(-ENODEV);
3475
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003476 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003477 ctx = &cpuctx->ctx;
3478 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003479 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003480
3481 return ctx;
3482 }
3483
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003484 err = -EINVAL;
3485 ctxn = pmu->task_ctx_nr;
3486 if (ctxn < 0)
3487 goto errout;
3488
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003489 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3490 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3491 if (!task_ctx_data) {
3492 err = -ENOMEM;
3493 goto errout;
3494 }
3495 }
3496
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003497retry:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003498 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003499 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003500 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003501 ++ctx->pin_count;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003502
3503 if (task_ctx_data && !ctx->task_ctx_data) {
3504 ctx->task_ctx_data = task_ctx_data;
3505 task_ctx_data = NULL;
3506 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003507 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003508
3509 if (clone_ctx)
3510 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003511 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003512 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003513 err = -ENOMEM;
3514 if (!ctx)
3515 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003516
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003517 if (task_ctx_data) {
3518 ctx->task_ctx_data = task_ctx_data;
3519 task_ctx_data = NULL;
3520 }
3521
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003522 err = 0;
3523 mutex_lock(&task->perf_event_mutex);
3524 /*
3525 * If it has already passed perf_event_exit_task().
3526 * we must see PF_EXITING, it takes this mutex too.
3527 */
3528 if (task->flags & PF_EXITING)
3529 err = -ESRCH;
3530 else if (task->perf_event_ctxp[ctxn])
3531 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003532 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003533 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003534 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003535 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003536 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003537 mutex_unlock(&task->perf_event_mutex);
3538
3539 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003540 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003541
3542 if (err == -EAGAIN)
3543 goto retry;
3544 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003545 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003546 }
3547
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003548 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003549 return ctx;
3550
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003551errout:
Yan, Zheng4af57ef2014-11-04 21:56:01 -05003552 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003553 return ERR_PTR(err);
3554}
3555
Li Zefan6fb29152009-10-15 11:21:42 +08003556static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003557static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003558
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003559static void free_event_rcu(struct rcu_head *head)
3560{
3561 struct perf_event *event;
3562
3563 event = container_of(head, struct perf_event, rcu_head);
3564 if (event->ns)
3565 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003566 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003567 kfree(event);
3568}
3569
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003570static void ring_buffer_attach(struct perf_event *event,
3571 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003572
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003573static void unaccount_event_cpu(struct perf_event *event, int cpu)
3574{
3575 if (event->parent)
3576 return;
3577
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003578 if (is_cgroup_event(event))
3579 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3580}
3581
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003582#ifdef CONFIG_NO_HZ_FULL
3583static DEFINE_SPINLOCK(nr_freq_lock);
3584#endif
3585
3586static void unaccount_freq_event_nohz(void)
3587{
3588#ifdef CONFIG_NO_HZ_FULL
3589 spin_lock(&nr_freq_lock);
3590 if (atomic_dec_and_test(&nr_freq_events))
3591 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
3592 spin_unlock(&nr_freq_lock);
3593#endif
3594}
3595
3596static void unaccount_freq_event(void)
3597{
3598 if (tick_nohz_full_enabled())
3599 unaccount_freq_event_nohz();
3600 else
3601 atomic_dec(&nr_freq_events);
3602}
3603
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003604static void unaccount_event(struct perf_event *event)
3605{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003606 bool dec = false;
3607
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003608 if (event->parent)
3609 return;
3610
3611 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003612 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003613 if (event->attr.mmap || event->attr.mmap_data)
3614 atomic_dec(&nr_mmap_events);
3615 if (event->attr.comm)
3616 atomic_dec(&nr_comm_events);
3617 if (event->attr.task)
3618 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003619 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003620 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03003621 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003622 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003623 atomic_dec(&nr_switch_events);
3624 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003625 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003626 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003627 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003628 dec = true;
3629
Peter Zijlstra9107c892016-02-24 18:45:45 +01003630 if (dec) {
3631 if (!atomic_add_unless(&perf_sched_count, -1, 1))
3632 schedule_delayed_work(&perf_sched_work, HZ);
3633 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003634
3635 unaccount_event_cpu(event, event->cpu);
3636}
3637
Peter Zijlstra9107c892016-02-24 18:45:45 +01003638static void perf_sched_delayed(struct work_struct *work)
3639{
3640 mutex_lock(&perf_sched_mutex);
3641 if (atomic_dec_and_test(&perf_sched_count))
3642 static_branch_disable(&perf_sched_events);
3643 mutex_unlock(&perf_sched_mutex);
3644}
3645
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003646/*
3647 * The following implement mutual exclusion of events on "exclusive" pmus
3648 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3649 * at a time, so we disallow creating events that might conflict, namely:
3650 *
3651 * 1) cpu-wide events in the presence of per-task events,
3652 * 2) per-task events in the presence of cpu-wide events,
3653 * 3) two matching events on the same context.
3654 *
3655 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003656 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003657 */
3658static int exclusive_event_init(struct perf_event *event)
3659{
3660 struct pmu *pmu = event->pmu;
3661
3662 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3663 return 0;
3664
3665 /*
3666 * Prevent co-existence of per-task and cpu-wide events on the
3667 * same exclusive pmu.
3668 *
3669 * Negative pmu::exclusive_cnt means there are cpu-wide
3670 * events on this "exclusive" pmu, positive means there are
3671 * per-task events.
3672 *
3673 * Since this is called in perf_event_alloc() path, event::ctx
3674 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3675 * to mean "per-task event", because unlike other attach states it
3676 * never gets cleared.
3677 */
3678 if (event->attach_state & PERF_ATTACH_TASK) {
3679 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
3680 return -EBUSY;
3681 } else {
3682 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
3683 return -EBUSY;
3684 }
3685
3686 return 0;
3687}
3688
3689static void exclusive_event_destroy(struct perf_event *event)
3690{
3691 struct pmu *pmu = event->pmu;
3692
3693 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3694 return;
3695
3696 /* see comment in exclusive_event_init() */
3697 if (event->attach_state & PERF_ATTACH_TASK)
3698 atomic_dec(&pmu->exclusive_cnt);
3699 else
3700 atomic_inc(&pmu->exclusive_cnt);
3701}
3702
3703static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
3704{
3705 if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
3706 (e1->cpu == e2->cpu ||
3707 e1->cpu == -1 ||
3708 e2->cpu == -1))
3709 return true;
3710 return false;
3711}
3712
3713/* Called under the same ctx::mutex as perf_install_in_context() */
3714static bool exclusive_event_installable(struct perf_event *event,
3715 struct perf_event_context *ctx)
3716{
3717 struct perf_event *iter_event;
3718 struct pmu *pmu = event->pmu;
3719
3720 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3721 return true;
3722
3723 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
3724 if (exclusive_event_match(iter_event, event))
3725 return false;
3726 }
3727
3728 return true;
3729}
3730
Peter Zijlstra683ede42014-05-05 12:11:24 +02003731static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003732{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003733 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003734
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003735 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003736
Frederic Weisbecker76369132011-05-19 19:55:04 +02003737 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003738 /*
3739 * Can happen when we close an event with re-directed output.
3740 *
3741 * Since we have a 0 refcount, perf_mmap_close() will skip
3742 * over us; possibly making our ring_buffer_put() the last.
3743 */
3744 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003745 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003746 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003747 }
3748
Stephane Eraniane5d13672011-02-14 11:20:01 +02003749 if (is_cgroup_event(event))
3750 perf_detach_cgroup(event);
3751
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003752 if (!event->parent) {
3753 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3754 put_callchain_buffers();
3755 }
3756
3757 perf_event_free_bpf_prog(event);
3758
3759 if (event->destroy)
3760 event->destroy(event);
3761
3762 if (event->ctx)
3763 put_ctx(event->ctx);
3764
3765 if (event->pmu) {
3766 exclusive_event_destroy(event);
3767 module_put(event->pmu->module);
3768 }
3769
3770 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003771}
3772
Peter Zijlstra683ede42014-05-05 12:11:24 +02003773/*
3774 * Used to free events which have a known refcount of 1, such as in error paths
3775 * where the event isn't exposed yet and inherited events.
3776 */
3777static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003778{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003779 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3780 "unexpected event refcount: %ld; ptr=%p\n",
3781 atomic_long_read(&event->refcount), event)) {
3782 /* leak to avoid use-after-free */
3783 return;
3784 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003785
Peter Zijlstra683ede42014-05-05 12:11:24 +02003786 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003787}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003788
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003789/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003790 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003791 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003792static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003793{
Peter Zijlstra88821352010-11-09 19:01:43 +01003794 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003795
Peter Zijlstra88821352010-11-09 19:01:43 +01003796 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01003797 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003798 * Matches the smp_store_release() in perf_event_exit_task(). If we
3799 * observe !owner it means the list deletion is complete and we can
3800 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01003801 * owner->perf_event_mutex.
3802 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003803 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01003804 if (owner) {
3805 /*
3806 * Since delayed_put_task_struct() also drops the last
3807 * task reference we can safely take a new reference
3808 * while holding the rcu_read_lock().
3809 */
3810 get_task_struct(owner);
3811 }
3812 rcu_read_unlock();
3813
3814 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003815 /*
3816 * If we're here through perf_event_exit_task() we're already
3817 * holding ctx->mutex which would be an inversion wrt. the
3818 * normal lock order.
3819 *
3820 * However we can safely take this lock because its the child
3821 * ctx->mutex.
3822 */
3823 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
3824
Peter Zijlstra88821352010-11-09 19:01:43 +01003825 /*
3826 * We have to re-check the event->owner field, if it is cleared
3827 * we raced with perf_event_exit_task(), acquiring the mutex
3828 * ensured they're done, and we can proceed with freeing the
3829 * event.
3830 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003831 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01003832 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003833 smp_store_release(&event->owner, NULL);
3834 }
Peter Zijlstra88821352010-11-09 19:01:43 +01003835 mutex_unlock(&owner->perf_event_mutex);
3836 put_task_struct(owner);
3837 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003838}
3839
Jiri Olsaf8697762014-08-01 14:33:01 +02003840static void put_event(struct perf_event *event)
3841{
Jiri Olsaf8697762014-08-01 14:33:01 +02003842 if (!atomic_long_dec_and_test(&event->refcount))
3843 return;
3844
Peter Zijlstra683ede42014-05-05 12:11:24 +02003845 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003846}
3847
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003848/*
3849 * Kill an event dead; while event:refcount will preserve the event
3850 * object, it will not preserve its functionality. Once the last 'user'
3851 * gives up the object, we'll destroy the thing.
3852 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02003853int perf_event_release_kernel(struct perf_event *event)
3854{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003855 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003856 struct perf_event *child, *tmp;
3857
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003858 /*
3859 * If we got here through err_file: fput(event_file); we will not have
3860 * attached to a context yet.
3861 */
3862 if (!ctx) {
3863 WARN_ON_ONCE(event->attach_state &
3864 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
3865 goto no_ctx;
3866 }
3867
Peter Zijlstra88821352010-11-09 19:01:43 +01003868 if (!is_kernel_event(event))
3869 perf_remove_from_owner(event);
3870
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01003871 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02003872 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003873 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01003874
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003875 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01003876 /*
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003877 * Mark this even as STATE_DEAD, there is no external reference to it
3878 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003879 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003880 * Anybody acquiring event->child_mutex after the below loop _must_
3881 * also see this, most importantly inherit_event() which will avoid
3882 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003883 *
3884 * Thus this guarantees that we will in fact observe and kill _ALL_
3885 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01003886 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003887 event->state = PERF_EVENT_STATE_DEAD;
3888 raw_spin_unlock_irq(&ctx->lock);
3889
3890 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01003891
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003892again:
3893 mutex_lock(&event->child_mutex);
3894 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01003895
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003896 /*
3897 * Cannot change, child events are not migrated, see the
3898 * comment with perf_event_ctx_lock_nested().
3899 */
3900 ctx = lockless_dereference(child->ctx);
3901 /*
3902 * Since child_mutex nests inside ctx::mutex, we must jump
3903 * through hoops. We start by grabbing a reference on the ctx.
3904 *
3905 * Since the event cannot get freed while we hold the
3906 * child_mutex, the context must also exist and have a !0
3907 * reference count.
3908 */
3909 get_ctx(ctx);
3910
3911 /*
3912 * Now that we have a ctx ref, we can drop child_mutex, and
3913 * acquire ctx::mutex without fear of it going away. Then we
3914 * can re-acquire child_mutex.
3915 */
3916 mutex_unlock(&event->child_mutex);
3917 mutex_lock(&ctx->mutex);
3918 mutex_lock(&event->child_mutex);
3919
3920 /*
3921 * Now that we hold ctx::mutex and child_mutex, revalidate our
3922 * state, if child is still the first entry, it didn't get freed
3923 * and we can continue doing so.
3924 */
3925 tmp = list_first_entry_or_null(&event->child_list,
3926 struct perf_event, child_list);
3927 if (tmp == child) {
3928 perf_remove_from_context(child, DETACH_GROUP);
3929 list_del(&child->child_list);
3930 free_event(child);
3931 /*
3932 * This matches the refcount bump in inherit_event();
3933 * this can't be the last reference.
3934 */
3935 put_event(event);
3936 }
3937
3938 mutex_unlock(&event->child_mutex);
3939 mutex_unlock(&ctx->mutex);
3940 put_ctx(ctx);
3941 goto again;
3942 }
3943 mutex_unlock(&event->child_mutex);
3944
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003945no_ctx:
3946 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02003947 return 0;
3948}
3949EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3950
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02003951/*
3952 * Called when the last reference to the file is gone.
3953 */
Al Viroa6fa9412012-08-20 14:59:25 +01003954static int perf_release(struct inode *inode, struct file *file)
3955{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003956 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01003957 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003958}
3959
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003960u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003961{
3962 struct perf_event *child;
3963 u64 total = 0;
3964
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003965 *enabled = 0;
3966 *running = 0;
3967
Peter Zijlstra6f105812009-11-20 22:19:56 +01003968 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003969
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003970 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003971 total += perf_event_count(event);
3972
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003973 *enabled += event->total_time_enabled +
3974 atomic64_read(&event->child_total_time_enabled);
3975 *running += event->total_time_running +
3976 atomic64_read(&event->child_total_time_running);
3977
3978 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003979 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003980 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003981 *enabled += child->total_time_enabled;
3982 *running += child->total_time_running;
3983 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003984 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003985
3986 return total;
3987}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003988EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003989
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003990static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003991 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003992{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003993 struct perf_event *sub;
3994 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003995 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003996
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003997 ret = perf_event_read(leader, true);
3998 if (ret)
3999 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004000
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004001 /*
4002 * Since we co-schedule groups, {enabled,running} times of siblings
4003 * will be identical to those of the leader, so we only publish one
4004 * set.
4005 */
4006 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4007 values[n++] += leader->total_time_enabled +
4008 atomic64_read(&leader->child_total_time_enabled);
4009 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004010
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004011 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4012 values[n++] += leader->total_time_running +
4013 atomic64_read(&leader->child_total_time_running);
4014 }
4015
4016 /*
4017 * Write {count,id} tuples for every sibling.
4018 */
4019 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004020 if (read_format & PERF_FORMAT_ID)
4021 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004022
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004023 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004024 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004025 if (read_format & PERF_FORMAT_ID)
4026 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004027 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004028
4029 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004030}
4031
4032static int perf_read_group(struct perf_event *event,
4033 u64 read_format, char __user *buf)
4034{
4035 struct perf_event *leader = event->group_leader, *child;
4036 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004037 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004038 u64 *values;
4039
4040 lockdep_assert_held(&ctx->mutex);
4041
4042 values = kzalloc(event->read_size, GFP_KERNEL);
4043 if (!values)
4044 return -ENOMEM;
4045
4046 values[0] = 1 + leader->nr_siblings;
4047
4048 /*
4049 * By locking the child_mutex of the leader we effectively
4050 * lock the child list of all siblings.. XXX explain how.
4051 */
4052 mutex_lock(&leader->child_mutex);
4053
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004054 ret = __perf_read_group_add(leader, read_format, values);
4055 if (ret)
4056 goto unlock;
4057
4058 list_for_each_entry(child, &leader->child_list, child_list) {
4059 ret = __perf_read_group_add(child, read_format, values);
4060 if (ret)
4061 goto unlock;
4062 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004063
4064 mutex_unlock(&leader->child_mutex);
4065
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004066 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004067 if (copy_to_user(buf, values, event->read_size))
4068 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004069 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004070
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004071unlock:
4072 mutex_unlock(&leader->child_mutex);
4073out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004074 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004075 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004076}
4077
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004078static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004079 u64 read_format, char __user *buf)
4080{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004081 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004082 u64 values[4];
4083 int n = 0;
4084
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004085 values[n++] = perf_event_read_value(event, &enabled, &running);
4086 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4087 values[n++] = enabled;
4088 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4089 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004090 if (read_format & PERF_FORMAT_ID)
4091 values[n++] = primary_event_id(event);
4092
4093 if (copy_to_user(buf, values, n * sizeof(u64)))
4094 return -EFAULT;
4095
4096 return n * sizeof(u64);
4097}
4098
Jiri Olsadc633982014-09-12 13:18:26 +02004099static bool is_event_hup(struct perf_event *event)
4100{
4101 bool no_children;
4102
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004103 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004104 return false;
4105
4106 mutex_lock(&event->child_mutex);
4107 no_children = list_empty(&event->child_list);
4108 mutex_unlock(&event->child_mutex);
4109 return no_children;
4110}
4111
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004112/*
4113 * Read the performance event - simple non blocking version for now
4114 */
4115static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004116__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004117{
4118 u64 read_format = event->attr.read_format;
4119 int ret;
4120
4121 /*
4122 * Return end-of-file for a read on a event that is in
4123 * error state (i.e. because it was pinned but it couldn't be
4124 * scheduled on to the CPU at some point).
4125 */
4126 if (event->state == PERF_EVENT_STATE_ERROR)
4127 return 0;
4128
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004129 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004130 return -ENOSPC;
4131
4132 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004133 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004134 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004135 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004136 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004137
4138 return ret;
4139}
4140
4141static ssize_t
4142perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4143{
4144 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004145 struct perf_event_context *ctx;
4146 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004147
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004148 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004149 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004150 perf_event_ctx_unlock(event, ctx);
4151
4152 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004153}
4154
4155static unsigned int perf_poll(struct file *file, poll_table *wait)
4156{
4157 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004158 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004159 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004160
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004161 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004162
Jiri Olsadc633982014-09-12 13:18:26 +02004163 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004164 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004165
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004166 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004167 * Pin the event->rb by taking event->mmap_mutex; otherwise
4168 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004169 */
4170 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004171 rb = event->rb;
4172 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004173 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004174 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004175 return events;
4176}
4177
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004178static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004179{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004180 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004181 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004182 perf_event_update_userpage(event);
4183}
4184
4185/*
4186 * Holding the top-level event's child_mutex means that any
4187 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004188 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004189 * task existence requirements of perf_event_enable/disable.
4190 */
4191static void perf_event_for_each_child(struct perf_event *event,
4192 void (*func)(struct perf_event *))
4193{
4194 struct perf_event *child;
4195
4196 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004197
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004198 mutex_lock(&event->child_mutex);
4199 func(event);
4200 list_for_each_entry(child, &event->child_list, child_list)
4201 func(child);
4202 mutex_unlock(&event->child_mutex);
4203}
4204
4205static void perf_event_for_each(struct perf_event *event,
4206 void (*func)(struct perf_event *))
4207{
4208 struct perf_event_context *ctx = event->ctx;
4209 struct perf_event *sibling;
4210
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004211 lockdep_assert_held(&ctx->mutex);
4212
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004213 event = event->group_leader;
4214
4215 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004216 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004217 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004218}
4219
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004220static void __perf_event_period(struct perf_event *event,
4221 struct perf_cpu_context *cpuctx,
4222 struct perf_event_context *ctx,
4223 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004224{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004225 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004226 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004227
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004228 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004229 event->attr.sample_freq = value;
4230 } else {
4231 event->attr.sample_period = value;
4232 event->hw.sample_period = value;
4233 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004234
4235 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4236 if (active) {
4237 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01004238 /*
4239 * We could be throttled; unthrottle now to avoid the tick
4240 * trying to unthrottle while we already re-started the event.
4241 */
4242 if (event->hw.interrupts == MAX_INTERRUPTS) {
4243 event->hw.interrupts = 0;
4244 perf_log_throttle(event, 1);
4245 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004246 event->pmu->stop(event, PERF_EF_UPDATE);
4247 }
4248
4249 local64_set(&event->hw.period_left, 0);
4250
4251 if (active) {
4252 event->pmu->start(event, PERF_EF_RELOAD);
4253 perf_pmu_enable(ctx->pmu);
4254 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004255}
4256
4257static int perf_event_period(struct perf_event *event, u64 __user *arg)
4258{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004259 u64 value;
4260
4261 if (!is_sampling_event(event))
4262 return -EINVAL;
4263
4264 if (copy_from_user(&value, arg, sizeof(value)))
4265 return -EFAULT;
4266
4267 if (!value)
4268 return -EINVAL;
4269
4270 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4271 return -EINVAL;
4272
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004273 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004274
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004275 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004276}
4277
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004278static const struct file_operations perf_fops;
4279
Al Viro2903ff02012-08-28 12:52:22 -04004280static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004281{
Al Viro2903ff02012-08-28 12:52:22 -04004282 struct fd f = fdget(fd);
4283 if (!f.file)
4284 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004285
Al Viro2903ff02012-08-28 12:52:22 -04004286 if (f.file->f_op != &perf_fops) {
4287 fdput(f);
4288 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004289 }
Al Viro2903ff02012-08-28 12:52:22 -04004290 *p = f;
4291 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004292}
4293
4294static int perf_event_set_output(struct perf_event *event,
4295 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004296static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004297static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004298
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004299static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004300{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004301 void (*func)(struct perf_event *);
4302 u32 flags = arg;
4303
4304 switch (cmd) {
4305 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004306 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004307 break;
4308 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004309 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004310 break;
4311 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004312 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004313 break;
4314
4315 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004316 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004317
4318 case PERF_EVENT_IOC_PERIOD:
4319 return perf_event_period(event, (u64 __user *)arg);
4320
Jiri Olsacf4957f2012-10-24 13:37:58 +02004321 case PERF_EVENT_IOC_ID:
4322 {
4323 u64 id = primary_event_id(event);
4324
4325 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4326 return -EFAULT;
4327 return 0;
4328 }
4329
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004330 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004331 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004332 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004333 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004334 struct perf_event *output_event;
4335 struct fd output;
4336 ret = perf_fget_light(arg, &output);
4337 if (ret)
4338 return ret;
4339 output_event = output.file->private_data;
4340 ret = perf_event_set_output(event, output_event);
4341 fdput(output);
4342 } else {
4343 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004344 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004345 return ret;
4346 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004347
Li Zefan6fb29152009-10-15 11:21:42 +08004348 case PERF_EVENT_IOC_SET_FILTER:
4349 return perf_event_set_filter(event, (void __user *)arg);
4350
Alexei Starovoitov25415172015-03-25 12:49:20 -07004351 case PERF_EVENT_IOC_SET_BPF:
4352 return perf_event_set_bpf_prog(event, arg);
4353
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004354 default:
4355 return -ENOTTY;
4356 }
4357
4358 if (flags & PERF_IOC_FLAG_GROUP)
4359 perf_event_for_each(event, func);
4360 else
4361 perf_event_for_each_child(event, func);
4362
4363 return 0;
4364}
4365
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004366static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4367{
4368 struct perf_event *event = file->private_data;
4369 struct perf_event_context *ctx;
4370 long ret;
4371
4372 ctx = perf_event_ctx_lock(event);
4373 ret = _perf_ioctl(event, cmd, arg);
4374 perf_event_ctx_unlock(event, ctx);
4375
4376 return ret;
4377}
4378
Pawel Mollb3f20782014-06-13 16:03:32 +01004379#ifdef CONFIG_COMPAT
4380static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4381 unsigned long arg)
4382{
4383 switch (_IOC_NR(cmd)) {
4384 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4385 case _IOC_NR(PERF_EVENT_IOC_ID):
4386 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4387 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4388 cmd &= ~IOCSIZE_MASK;
4389 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4390 }
4391 break;
4392 }
4393 return perf_ioctl(file, cmd, arg);
4394}
4395#else
4396# define perf_compat_ioctl NULL
4397#endif
4398
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004399int perf_event_task_enable(void)
4400{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004401 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004402 struct perf_event *event;
4403
4404 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004405 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4406 ctx = perf_event_ctx_lock(event);
4407 perf_event_for_each_child(event, _perf_event_enable);
4408 perf_event_ctx_unlock(event, ctx);
4409 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004410 mutex_unlock(&current->perf_event_mutex);
4411
4412 return 0;
4413}
4414
4415int perf_event_task_disable(void)
4416{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004417 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004418 struct perf_event *event;
4419
4420 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004421 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4422 ctx = perf_event_ctx_lock(event);
4423 perf_event_for_each_child(event, _perf_event_disable);
4424 perf_event_ctx_unlock(event, ctx);
4425 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004426 mutex_unlock(&current->perf_event_mutex);
4427
4428 return 0;
4429}
4430
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004431static int perf_event_index(struct perf_event *event)
4432{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004433 if (event->hw.state & PERF_HES_STOPPED)
4434 return 0;
4435
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004436 if (event->state != PERF_EVENT_STATE_ACTIVE)
4437 return 0;
4438
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004439 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004440}
4441
Eric B Munsonc4794292011-06-23 16:34:38 -04004442static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004443 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004444 u64 *enabled,
4445 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004446{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004447 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004448
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004449 *now = perf_clock();
4450 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004451 *enabled = ctx_time - event->tstamp_enabled;
4452 *running = ctx_time - event->tstamp_running;
4453}
4454
Peter Zijlstrafa7315872013-09-19 10:16:42 +02004455static void perf_event_init_userpage(struct perf_event *event)
4456{
4457 struct perf_event_mmap_page *userpg;
4458 struct ring_buffer *rb;
4459
4460 rcu_read_lock();
4461 rb = rcu_dereference(event->rb);
4462 if (!rb)
4463 goto unlock;
4464
4465 userpg = rb->user_page;
4466
4467 /* Allow new userspace to detect that bit 0 is deprecated */
4468 userpg->cap_bit0_is_deprecated = 1;
4469 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004470 userpg->data_offset = PAGE_SIZE;
4471 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa7315872013-09-19 10:16:42 +02004472
4473unlock:
4474 rcu_read_unlock();
4475}
4476
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004477void __weak arch_perf_update_userpage(
4478 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004479{
4480}
4481
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004482/*
4483 * Callers need to ensure there can be no nesting of this function, otherwise
4484 * the seqlock logic goes bad. We can not serialize this because the arch
4485 * code calls this from NMI context.
4486 */
4487void perf_event_update_userpage(struct perf_event *event)
4488{
4489 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004490 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004491 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004492
4493 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004494 rb = rcu_dereference(event->rb);
4495 if (!rb)
4496 goto unlock;
4497
Eric B Munson0d641202011-06-24 12:26:26 -04004498 /*
4499 * compute total_time_enabled, total_time_running
4500 * based on snapshot values taken when the event
4501 * was last scheduled in.
4502 *
4503 * we cannot simply called update_context_time()
4504 * because of locking issue as we can be called in
4505 * NMI context
4506 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004507 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004508
Frederic Weisbecker76369132011-05-19 19:55:04 +02004509 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004510 /*
4511 * Disable preemption so as to not let the corresponding user-space
4512 * spin too long if we get preempted.
4513 */
4514 preempt_disable();
4515 ++userpg->lock;
4516 barrier();
4517 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004518 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004519 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004520 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004521
Eric B Munson0d641202011-06-24 12:26:26 -04004522 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004523 atomic64_read(&event->child_total_time_enabled);
4524
Eric B Munson0d641202011-06-24 12:26:26 -04004525 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004526 atomic64_read(&event->child_total_time_running);
4527
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004528 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004529
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004530 barrier();
4531 ++userpg->lock;
4532 preempt_enable();
4533unlock:
4534 rcu_read_unlock();
4535}
4536
Peter Zijlstra906010b2009-09-21 16:08:49 +02004537static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4538{
4539 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004540 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004541 int ret = VM_FAULT_SIGBUS;
4542
4543 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4544 if (vmf->pgoff == 0)
4545 ret = 0;
4546 return ret;
4547 }
4548
4549 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004550 rb = rcu_dereference(event->rb);
4551 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004552 goto unlock;
4553
4554 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4555 goto unlock;
4556
Frederic Weisbecker76369132011-05-19 19:55:04 +02004557 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004558 if (!vmf->page)
4559 goto unlock;
4560
4561 get_page(vmf->page);
4562 vmf->page->mapping = vma->vm_file->f_mapping;
4563 vmf->page->index = vmf->pgoff;
4564
4565 ret = 0;
4566unlock:
4567 rcu_read_unlock();
4568
4569 return ret;
4570}
4571
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004572static void ring_buffer_attach(struct perf_event *event,
4573 struct ring_buffer *rb)
4574{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004575 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004576 unsigned long flags;
4577
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004578 if (event->rb) {
4579 /*
4580 * Should be impossible, we set this when removing
4581 * event->rb_entry and wait/clear when adding event->rb_entry.
4582 */
4583 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004584
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004585 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004586 spin_lock_irqsave(&old_rb->event_lock, flags);
4587 list_del_rcu(&event->rb_entry);
4588 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004589
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004590 event->rcu_batches = get_state_synchronize_rcu();
4591 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004592 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004593
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004594 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004595 if (event->rcu_pending) {
4596 cond_synchronize_rcu(event->rcu_batches);
4597 event->rcu_pending = 0;
4598 }
4599
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004600 spin_lock_irqsave(&rb->event_lock, flags);
4601 list_add_rcu(&event->rb_entry, &rb->event_list);
4602 spin_unlock_irqrestore(&rb->event_lock, flags);
4603 }
4604
4605 rcu_assign_pointer(event->rb, rb);
4606
4607 if (old_rb) {
4608 ring_buffer_put(old_rb);
4609 /*
4610 * Since we detached before setting the new rb, so that we
4611 * could attach the new rb, we could have missed a wakeup.
4612 * Provide it now.
4613 */
4614 wake_up_all(&event->waitq);
4615 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004616}
4617
4618static void ring_buffer_wakeup(struct perf_event *event)
4619{
4620 struct ring_buffer *rb;
4621
4622 rcu_read_lock();
4623 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004624 if (rb) {
4625 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4626 wake_up_all(&event->waitq);
4627 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004628 rcu_read_unlock();
4629}
4630
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004631struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004632{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004633 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004634
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004635 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004636 rb = rcu_dereference(event->rb);
4637 if (rb) {
4638 if (!atomic_inc_not_zero(&rb->refcount))
4639 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004640 }
4641 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004642
Frederic Weisbecker76369132011-05-19 19:55:04 +02004643 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004644}
4645
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004646void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004647{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004648 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004649 return;
4650
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004651 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004652
Frederic Weisbecker76369132011-05-19 19:55:04 +02004653 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004654}
4655
4656static void perf_mmap_open(struct vm_area_struct *vma)
4657{
4658 struct perf_event *event = vma->vm_file->private_data;
4659
4660 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004661 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004662
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004663 if (vma->vm_pgoff)
4664 atomic_inc(&event->rb->aux_mmap_count);
4665
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004666 if (event->pmu->event_mapped)
4667 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004668}
4669
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004670/*
4671 * A buffer can be mmap()ed multiple times; either directly through the same
4672 * event, or through other events by use of perf_event_set_output().
4673 *
4674 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4675 * the buffer here, where we still have a VM context. This means we need
4676 * to detach all events redirecting to us.
4677 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004678static void perf_mmap_close(struct vm_area_struct *vma)
4679{
4680 struct perf_event *event = vma->vm_file->private_data;
4681
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004682 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004683 struct user_struct *mmap_user = rb->mmap_user;
4684 int mmap_locked = rb->mmap_locked;
4685 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004686
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004687 if (event->pmu->event_unmapped)
4688 event->pmu->event_unmapped(event);
4689
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004690 /*
4691 * rb->aux_mmap_count will always drop before rb->mmap_count and
4692 * event->mmap_count, so it is ok to use event->mmap_mutex to
4693 * serialize with perf_mmap here.
4694 */
4695 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
4696 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
4697 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
4698 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
4699
4700 rb_free_aux(rb);
4701 mutex_unlock(&event->mmap_mutex);
4702 }
4703
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004704 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004705
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004706 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004707 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004708
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004709 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004710 mutex_unlock(&event->mmap_mutex);
4711
4712 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004713 if (atomic_read(&rb->mmap_count))
4714 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004715
4716 /*
4717 * No other mmap()s, detach from all other events that might redirect
4718 * into the now unreachable buffer. Somewhat complicated by the
4719 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4720 */
4721again:
4722 rcu_read_lock();
4723 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4724 if (!atomic_long_inc_not_zero(&event->refcount)) {
4725 /*
4726 * This event is en-route to free_event() which will
4727 * detach it and remove it from the list.
4728 */
4729 continue;
4730 }
4731 rcu_read_unlock();
4732
4733 mutex_lock(&event->mmap_mutex);
4734 /*
4735 * Check we didn't race with perf_event_set_output() which can
4736 * swizzle the rb from under us while we were waiting to
4737 * acquire mmap_mutex.
4738 *
4739 * If we find a different rb; ignore this event, a next
4740 * iteration will no longer find it on the list. We have to
4741 * still restart the iteration to make sure we're not now
4742 * iterating the wrong list.
4743 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004744 if (event->rb == rb)
4745 ring_buffer_attach(event, NULL);
4746
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004747 mutex_unlock(&event->mmap_mutex);
4748 put_event(event);
4749
4750 /*
4751 * Restart the iteration; either we're on the wrong list or
4752 * destroyed its integrity by doing a deletion.
4753 */
4754 goto again;
4755 }
4756 rcu_read_unlock();
4757
4758 /*
4759 * It could be there's still a few 0-ref events on the list; they'll
4760 * get cleaned up by free_event() -- they'll also still have their
4761 * ref on the rb and will free it whenever they are done with it.
4762 *
4763 * Aside from that, this buffer is 'fully' detached and unmapped,
4764 * undo the VM accounting.
4765 */
4766
4767 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4768 vma->vm_mm->pinned_vm -= mmap_locked;
4769 free_uid(mmap_user);
4770
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004771out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004772 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004773}
4774
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04004775static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004776 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004777 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004778 .fault = perf_mmap_fault,
4779 .page_mkwrite = perf_mmap_fault,
4780};
4781
4782static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4783{
4784 struct perf_event *event = file->private_data;
4785 unsigned long user_locked, user_lock_limit;
4786 struct user_struct *user = current_user();
4787 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004788 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004789 unsigned long vma_size;
4790 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004791 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004792 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004793
Peter Zijlstrac7920612010-05-18 10:33:24 +02004794 /*
4795 * Don't allow mmap() of inherited per-task counters. This would
4796 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004797 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004798 */
4799 if (event->cpu == -1 && event->attr.inherit)
4800 return -EINVAL;
4801
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004802 if (!(vma->vm_flags & VM_SHARED))
4803 return -EINVAL;
4804
4805 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004806
4807 if (vma->vm_pgoff == 0) {
4808 nr_pages = (vma_size / PAGE_SIZE) - 1;
4809 } else {
4810 /*
4811 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
4812 * mapped, all subsequent mappings should have the same size
4813 * and offset. Must be above the normal perf buffer.
4814 */
4815 u64 aux_offset, aux_size;
4816
4817 if (!event->rb)
4818 return -EINVAL;
4819
4820 nr_pages = vma_size / PAGE_SIZE;
4821
4822 mutex_lock(&event->mmap_mutex);
4823 ret = -EINVAL;
4824
4825 rb = event->rb;
4826 if (!rb)
4827 goto aux_unlock;
4828
4829 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
4830 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
4831
4832 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
4833 goto aux_unlock;
4834
4835 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
4836 goto aux_unlock;
4837
4838 /* already mapped with a different offset */
4839 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
4840 goto aux_unlock;
4841
4842 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
4843 goto aux_unlock;
4844
4845 /* already mapped with a different size */
4846 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
4847 goto aux_unlock;
4848
4849 if (!is_power_of_2(nr_pages))
4850 goto aux_unlock;
4851
4852 if (!atomic_inc_not_zero(&rb->mmap_count))
4853 goto aux_unlock;
4854
4855 if (rb_has_aux(rb)) {
4856 atomic_inc(&rb->aux_mmap_count);
4857 ret = 0;
4858 goto unlock;
4859 }
4860
4861 atomic_set(&rb->aux_mmap_count, 1);
4862 user_extra = nr_pages;
4863
4864 goto accounting;
4865 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004866
4867 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004868 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004869 * can do bitmasks instead of modulo.
4870 */
Kan Liang2ed11312015-03-02 02:14:26 -05004871 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004872 return -EINVAL;
4873
4874 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4875 return -EINVAL;
4876
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004877 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004878again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004879 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004880 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004881 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004882 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004883 goto unlock;
4884 }
4885
4886 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4887 /*
4888 * Raced against perf_mmap_close() through
4889 * perf_event_set_output(). Try again, hope for better
4890 * luck.
4891 */
4892 mutex_unlock(&event->mmap_mutex);
4893 goto again;
4894 }
4895
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004896 goto unlock;
4897 }
4898
4899 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004900
4901accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004902 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4903
4904 /*
4905 * Increase the limit linearly with more CPUs:
4906 */
4907 user_lock_limit *= num_online_cpus();
4908
4909 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4910
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004911 if (user_locked > user_lock_limit)
4912 extra = user_locked - user_lock_limit;
4913
Jiri Slaby78d7d402010-03-05 13:42:54 -08004914 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004915 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004916 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004917
4918 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4919 !capable(CAP_IPC_LOCK)) {
4920 ret = -EPERM;
4921 goto unlock;
4922 }
4923
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004924 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004925
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004926 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004927 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004928
Frederic Weisbecker76369132011-05-19 19:55:04 +02004929 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004930 rb = rb_alloc(nr_pages,
4931 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4932 event->cpu, flags);
4933
4934 if (!rb) {
4935 ret = -ENOMEM;
4936 goto unlock;
4937 }
4938
4939 atomic_set(&rb->mmap_count, 1);
4940 rb->mmap_user = get_current_user();
4941 rb->mmap_locked = extra;
4942
4943 ring_buffer_attach(event, rb);
4944
4945 perf_event_init_userpage(event);
4946 perf_event_update_userpage(event);
4947 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02004948 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
4949 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004950 if (!ret)
4951 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004952 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004953
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004954unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004955 if (!ret) {
4956 atomic_long_add(user_extra, &user->locked_vm);
4957 vma->vm_mm->pinned_vm += extra;
4958
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004959 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004960 } else if (rb) {
4961 atomic_dec(&rb->mmap_count);
4962 }
4963aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004964 mutex_unlock(&event->mmap_mutex);
4965
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004966 /*
4967 * Since pinned accounting is per vm we cannot allow fork() to copy our
4968 * vma.
4969 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004970 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004971 vma->vm_ops = &perf_mmap_vmops;
4972
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004973 if (event->pmu->event_mapped)
4974 event->pmu->event_mapped(event);
4975
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004976 return ret;
4977}
4978
4979static int perf_fasync(int fd, struct file *filp, int on)
4980{
Al Viro496ad9a2013-01-23 17:07:38 -05004981 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004982 struct perf_event *event = filp->private_data;
4983 int retval;
4984
Al Viro59551022016-01-22 15:40:57 -05004985 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004986 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05004987 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004988
4989 if (retval < 0)
4990 return retval;
4991
4992 return 0;
4993}
4994
4995static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004996 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004997 .release = perf_release,
4998 .read = perf_read,
4999 .poll = perf_poll,
5000 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01005001 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005002 .mmap = perf_mmap,
5003 .fasync = perf_fasync,
5004};
5005
5006/*
5007 * Perf event wakeup
5008 *
5009 * If there's data, ensure we set the poll() state and publish everything
5010 * to user-space before waking everybody up.
5011 */
5012
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005013static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
5014{
5015 /* only the parent has fasync state */
5016 if (event->parent)
5017 event = event->parent;
5018 return &event->fasync;
5019}
5020
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005021void perf_event_wakeup(struct perf_event *event)
5022{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005023 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005024
5025 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005026 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005027 event->pending_kill = 0;
5028 }
5029}
5030
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005031static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005032{
5033 struct perf_event *event = container_of(entry,
5034 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01005035 int rctx;
5036
5037 rctx = perf_swevent_get_recursion_context();
5038 /*
5039 * If we 'fail' here, that's OK, it means recursion is already disabled
5040 * and we won't recurse 'further'.
5041 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005042
5043 if (event->pending_disable) {
5044 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005045 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005046 }
5047
5048 if (event->pending_wakeup) {
5049 event->pending_wakeup = 0;
5050 perf_event_wakeup(event);
5051 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01005052
5053 if (rctx >= 0)
5054 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005055}
5056
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005057/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005058 * We assume there is only KVM supporting the callbacks.
5059 * Later on, we might change it to a list if there is
5060 * another virtualization implementation supporting the callbacks.
5061 */
5062struct perf_guest_info_callbacks *perf_guest_cbs;
5063
5064int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5065{
5066 perf_guest_cbs = cbs;
5067 return 0;
5068}
5069EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5070
5071int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5072{
5073 perf_guest_cbs = NULL;
5074 return 0;
5075}
5076EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5077
Jiri Olsa40189942012-08-07 15:20:37 +02005078static void
5079perf_output_sample_regs(struct perf_output_handle *handle,
5080 struct pt_regs *regs, u64 mask)
5081{
5082 int bit;
5083
5084 for_each_set_bit(bit, (const unsigned long *) &mask,
5085 sizeof(mask) * BITS_PER_BYTE) {
5086 u64 val;
5087
5088 val = perf_reg_value(regs, bit);
5089 perf_output_put(handle, val);
5090 }
5091}
5092
Stephane Eranian60e23642014-09-24 13:48:37 +02005093static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005094 struct pt_regs *regs,
5095 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02005096{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005097 if (user_mode(regs)) {
5098 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02005099 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005100 } else if (current->mm) {
5101 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005102 } else {
5103 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5104 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005105 }
5106}
5107
Stephane Eranian60e23642014-09-24 13:48:37 +02005108static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5109 struct pt_regs *regs)
5110{
5111 regs_intr->regs = regs;
5112 regs_intr->abi = perf_reg_abi(current);
5113}
5114
5115
Jiri Olsac5ebced2012-08-07 15:20:40 +02005116/*
5117 * Get remaining task size from user stack pointer.
5118 *
5119 * It'd be better to take stack vma map and limit this more
5120 * precisly, but there's no way to get it safely under interrupt,
5121 * so using TASK_SIZE as limit.
5122 */
5123static u64 perf_ustack_task_size(struct pt_regs *regs)
5124{
5125 unsigned long addr = perf_user_stack_pointer(regs);
5126
5127 if (!addr || addr >= TASK_SIZE)
5128 return 0;
5129
5130 return TASK_SIZE - addr;
5131}
5132
5133static u16
5134perf_sample_ustack_size(u16 stack_size, u16 header_size,
5135 struct pt_regs *regs)
5136{
5137 u64 task_size;
5138
5139 /* No regs, no stack pointer, no dump. */
5140 if (!regs)
5141 return 0;
5142
5143 /*
5144 * Check if we fit in with the requested stack size into the:
5145 * - TASK_SIZE
5146 * If we don't, we limit the size to the TASK_SIZE.
5147 *
5148 * - remaining sample size
5149 * If we don't, we customize the stack size to
5150 * fit in to the remaining sample size.
5151 */
5152
5153 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5154 stack_size = min(stack_size, (u16) task_size);
5155
5156 /* Current header size plus static size and dynamic size. */
5157 header_size += 2 * sizeof(u64);
5158
5159 /* Do we fit in with the current stack dump size? */
5160 if ((u16) (header_size + stack_size) < header_size) {
5161 /*
5162 * If we overflow the maximum size for the sample,
5163 * we customize the stack dump size to fit in.
5164 */
5165 stack_size = USHRT_MAX - header_size - sizeof(u64);
5166 stack_size = round_up(stack_size, sizeof(u64));
5167 }
5168
5169 return stack_size;
5170}
5171
5172static void
5173perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5174 struct pt_regs *regs)
5175{
5176 /* Case of a kernel thread, nothing to dump */
5177 if (!regs) {
5178 u64 size = 0;
5179 perf_output_put(handle, size);
5180 } else {
5181 unsigned long sp;
5182 unsigned int rem;
5183 u64 dyn_size;
5184
5185 /*
5186 * We dump:
5187 * static size
5188 * - the size requested by user or the best one we can fit
5189 * in to the sample max size
5190 * data
5191 * - user stack dump data
5192 * dynamic size
5193 * - the actual dumped size
5194 */
5195
5196 /* Static size. */
5197 perf_output_put(handle, dump_size);
5198
5199 /* Data. */
5200 sp = perf_user_stack_pointer(regs);
5201 rem = __output_copy_user(handle, (void *) sp, dump_size);
5202 dyn_size = dump_size - rem;
5203
5204 perf_output_skip(handle, rem);
5205
5206 /* Dynamic size. */
5207 perf_output_put(handle, dyn_size);
5208 }
5209}
5210
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005211static void __perf_event_header__init_id(struct perf_event_header *header,
5212 struct perf_sample_data *data,
5213 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005214{
5215 u64 sample_type = event->attr.sample_type;
5216
5217 data->type = sample_type;
5218 header->size += event->id_header_size;
5219
5220 if (sample_type & PERF_SAMPLE_TID) {
5221 /* namespace issues */
5222 data->tid_entry.pid = perf_event_pid(event, current);
5223 data->tid_entry.tid = perf_event_tid(event, current);
5224 }
5225
5226 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005227 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005228
Adrian Hunterff3d5272013-08-27 11:23:07 +03005229 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005230 data->id = primary_event_id(event);
5231
5232 if (sample_type & PERF_SAMPLE_STREAM_ID)
5233 data->stream_id = event->id;
5234
5235 if (sample_type & PERF_SAMPLE_CPU) {
5236 data->cpu_entry.cpu = raw_smp_processor_id();
5237 data->cpu_entry.reserved = 0;
5238 }
5239}
5240
Frederic Weisbecker76369132011-05-19 19:55:04 +02005241void perf_event_header__init_id(struct perf_event_header *header,
5242 struct perf_sample_data *data,
5243 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005244{
5245 if (event->attr.sample_id_all)
5246 __perf_event_header__init_id(header, data, event);
5247}
5248
5249static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5250 struct perf_sample_data *data)
5251{
5252 u64 sample_type = data->type;
5253
5254 if (sample_type & PERF_SAMPLE_TID)
5255 perf_output_put(handle, data->tid_entry);
5256
5257 if (sample_type & PERF_SAMPLE_TIME)
5258 perf_output_put(handle, data->time);
5259
5260 if (sample_type & PERF_SAMPLE_ID)
5261 perf_output_put(handle, data->id);
5262
5263 if (sample_type & PERF_SAMPLE_STREAM_ID)
5264 perf_output_put(handle, data->stream_id);
5265
5266 if (sample_type & PERF_SAMPLE_CPU)
5267 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005268
5269 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5270 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005271}
5272
Frederic Weisbecker76369132011-05-19 19:55:04 +02005273void perf_event__output_id_sample(struct perf_event *event,
5274 struct perf_output_handle *handle,
5275 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005276{
5277 if (event->attr.sample_id_all)
5278 __perf_event__output_id_sample(handle, sample);
5279}
5280
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005281static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005282 struct perf_event *event,
5283 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005284{
5285 u64 read_format = event->attr.read_format;
5286 u64 values[4];
5287 int n = 0;
5288
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005289 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005290 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005291 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005292 atomic64_read(&event->child_total_time_enabled);
5293 }
5294 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005295 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005296 atomic64_read(&event->child_total_time_running);
5297 }
5298 if (read_format & PERF_FORMAT_ID)
5299 values[n++] = primary_event_id(event);
5300
Frederic Weisbecker76369132011-05-19 19:55:04 +02005301 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005302}
5303
5304/*
5305 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
5306 */
5307static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005308 struct perf_event *event,
5309 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005310{
5311 struct perf_event *leader = event->group_leader, *sub;
5312 u64 read_format = event->attr.read_format;
5313 u64 values[5];
5314 int n = 0;
5315
5316 values[n++] = 1 + leader->nr_siblings;
5317
5318 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005319 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005320
5321 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005322 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005323
5324 if (leader != event)
5325 leader->pmu->read(leader);
5326
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005327 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005328 if (read_format & PERF_FORMAT_ID)
5329 values[n++] = primary_event_id(leader);
5330
Frederic Weisbecker76369132011-05-19 19:55:04 +02005331 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005332
5333 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5334 n = 0;
5335
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005336 if ((sub != event) &&
5337 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005338 sub->pmu->read(sub);
5339
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005340 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005341 if (read_format & PERF_FORMAT_ID)
5342 values[n++] = primary_event_id(sub);
5343
Frederic Weisbecker76369132011-05-19 19:55:04 +02005344 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005345 }
5346}
5347
Stephane Eranianeed01522010-10-26 16:08:01 +02005348#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5349 PERF_FORMAT_TOTAL_TIME_RUNNING)
5350
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005351static void perf_output_read(struct perf_output_handle *handle,
5352 struct perf_event *event)
5353{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005354 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005355 u64 read_format = event->attr.read_format;
5356
5357 /*
5358 * compute total_time_enabled, total_time_running
5359 * based on snapshot values taken when the event
5360 * was last scheduled in.
5361 *
5362 * we cannot simply called update_context_time()
5363 * because of locking issue as we are called in
5364 * NMI context
5365 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005366 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005367 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005368
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005369 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005370 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005371 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005372 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005373}
5374
5375void perf_output_sample(struct perf_output_handle *handle,
5376 struct perf_event_header *header,
5377 struct perf_sample_data *data,
5378 struct perf_event *event)
5379{
5380 u64 sample_type = data->type;
5381
5382 perf_output_put(handle, *header);
5383
Adrian Hunterff3d5272013-08-27 11:23:07 +03005384 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5385 perf_output_put(handle, data->id);
5386
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005387 if (sample_type & PERF_SAMPLE_IP)
5388 perf_output_put(handle, data->ip);
5389
5390 if (sample_type & PERF_SAMPLE_TID)
5391 perf_output_put(handle, data->tid_entry);
5392
5393 if (sample_type & PERF_SAMPLE_TIME)
5394 perf_output_put(handle, data->time);
5395
5396 if (sample_type & PERF_SAMPLE_ADDR)
5397 perf_output_put(handle, data->addr);
5398
5399 if (sample_type & PERF_SAMPLE_ID)
5400 perf_output_put(handle, data->id);
5401
5402 if (sample_type & PERF_SAMPLE_STREAM_ID)
5403 perf_output_put(handle, data->stream_id);
5404
5405 if (sample_type & PERF_SAMPLE_CPU)
5406 perf_output_put(handle, data->cpu_entry);
5407
5408 if (sample_type & PERF_SAMPLE_PERIOD)
5409 perf_output_put(handle, data->period);
5410
5411 if (sample_type & PERF_SAMPLE_READ)
5412 perf_output_read(handle, event);
5413
5414 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5415 if (data->callchain) {
5416 int size = 1;
5417
5418 if (data->callchain)
5419 size += data->callchain->nr;
5420
5421 size *= sizeof(u64);
5422
Frederic Weisbecker76369132011-05-19 19:55:04 +02005423 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005424 } else {
5425 u64 nr = 0;
5426 perf_output_put(handle, nr);
5427 }
5428 }
5429
5430 if (sample_type & PERF_SAMPLE_RAW) {
5431 if (data->raw) {
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005432 u32 raw_size = data->raw->size;
5433 u32 real_size = round_up(raw_size + sizeof(u32),
5434 sizeof(u64)) - sizeof(u32);
5435 u64 zero = 0;
5436
5437 perf_output_put(handle, real_size);
5438 __output_copy(handle, data->raw->data, raw_size);
5439 if (real_size - raw_size)
5440 __output_copy(handle, &zero, real_size - raw_size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005441 } else {
5442 struct {
5443 u32 size;
5444 u32 data;
5445 } raw = {
5446 .size = sizeof(u32),
5447 .data = 0,
5448 };
5449 perf_output_put(handle, raw);
5450 }
5451 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005452
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005453 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5454 if (data->br_stack) {
5455 size_t size;
5456
5457 size = data->br_stack->nr
5458 * sizeof(struct perf_branch_entry);
5459
5460 perf_output_put(handle, data->br_stack->nr);
5461 perf_output_copy(handle, data->br_stack->entries, size);
5462 } else {
5463 /*
5464 * we always store at least the value of nr
5465 */
5466 u64 nr = 0;
5467 perf_output_put(handle, nr);
5468 }
5469 }
Jiri Olsa40189942012-08-07 15:20:37 +02005470
5471 if (sample_type & PERF_SAMPLE_REGS_USER) {
5472 u64 abi = data->regs_user.abi;
5473
5474 /*
5475 * If there are no regs to dump, notice it through
5476 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5477 */
5478 perf_output_put(handle, abi);
5479
5480 if (abi) {
5481 u64 mask = event->attr.sample_regs_user;
5482 perf_output_sample_regs(handle,
5483 data->regs_user.regs,
5484 mask);
5485 }
5486 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005487
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005488 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005489 perf_output_sample_ustack(handle,
5490 data->stack_user_size,
5491 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005492 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005493
5494 if (sample_type & PERF_SAMPLE_WEIGHT)
5495 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005496
5497 if (sample_type & PERF_SAMPLE_DATA_SRC)
5498 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005499
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005500 if (sample_type & PERF_SAMPLE_TRANSACTION)
5501 perf_output_put(handle, data->txn);
5502
Stephane Eranian60e23642014-09-24 13:48:37 +02005503 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5504 u64 abi = data->regs_intr.abi;
5505 /*
5506 * If there are no regs to dump, notice it through
5507 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5508 */
5509 perf_output_put(handle, abi);
5510
5511 if (abi) {
5512 u64 mask = event->attr.sample_regs_intr;
5513
5514 perf_output_sample_regs(handle,
5515 data->regs_intr.regs,
5516 mask);
5517 }
5518 }
5519
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005520 if (!event->attr.watermark) {
5521 int wakeup_events = event->attr.wakeup_events;
5522
5523 if (wakeup_events) {
5524 struct ring_buffer *rb = handle->rb;
5525 int events = local_inc_return(&rb->events);
5526
5527 if (events >= wakeup_events) {
5528 local_sub(wakeup_events, &rb->events);
5529 local_inc(&rb->wakeup);
5530 }
5531 }
5532 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005533}
5534
5535void perf_prepare_sample(struct perf_event_header *header,
5536 struct perf_sample_data *data,
5537 struct perf_event *event,
5538 struct pt_regs *regs)
5539{
5540 u64 sample_type = event->attr.sample_type;
5541
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005542 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005543 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005544
5545 header->misc = 0;
5546 header->misc |= perf_misc_flags(regs);
5547
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005548 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005549
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005550 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005551 data->ip = perf_instruction_pointer(regs);
5552
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005553 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5554 int size = 1;
5555
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005556 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005557
5558 if (data->callchain)
5559 size += data->callchain->nr;
5560
5561 header->size += size * sizeof(u64);
5562 }
5563
5564 if (sample_type & PERF_SAMPLE_RAW) {
5565 int size = sizeof(u32);
5566
5567 if (data->raw)
5568 size += data->raw->size;
5569 else
5570 size += sizeof(u32);
5571
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005572 header->size += round_up(size, sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005573 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005574
5575 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5576 int size = sizeof(u64); /* nr */
5577 if (data->br_stack) {
5578 size += data->br_stack->nr
5579 * sizeof(struct perf_branch_entry);
5580 }
5581 header->size += size;
5582 }
Jiri Olsa40189942012-08-07 15:20:37 +02005583
Peter Zijlstra25657112014-09-24 13:48:42 +02005584 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005585 perf_sample_regs_user(&data->regs_user, regs,
5586 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005587
Jiri Olsa40189942012-08-07 15:20:37 +02005588 if (sample_type & PERF_SAMPLE_REGS_USER) {
5589 /* regs dump ABI info */
5590 int size = sizeof(u64);
5591
Jiri Olsa40189942012-08-07 15:20:37 +02005592 if (data->regs_user.regs) {
5593 u64 mask = event->attr.sample_regs_user;
5594 size += hweight64(mask) * sizeof(u64);
5595 }
5596
5597 header->size += size;
5598 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005599
5600 if (sample_type & PERF_SAMPLE_STACK_USER) {
5601 /*
5602 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5603 * processed as the last one or have additional check added
5604 * in case new sample type is added, because we could eat
5605 * up the rest of the sample size.
5606 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005607 u16 stack_size = event->attr.sample_stack_user;
5608 u16 size = sizeof(u64);
5609
Jiri Olsac5ebced2012-08-07 15:20:40 +02005610 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005611 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005612
5613 /*
5614 * If there is something to dump, add space for the dump
5615 * itself and for the field that tells the dynamic size,
5616 * which is how many have been actually dumped.
5617 */
5618 if (stack_size)
5619 size += sizeof(u64) + stack_size;
5620
5621 data->stack_user_size = stack_size;
5622 header->size += size;
5623 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005624
5625 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5626 /* regs dump ABI info */
5627 int size = sizeof(u64);
5628
5629 perf_sample_regs_intr(&data->regs_intr, regs);
5630
5631 if (data->regs_intr.regs) {
5632 u64 mask = event->attr.sample_regs_intr;
5633
5634 size += hweight64(mask) * sizeof(u64);
5635 }
5636
5637 header->size += size;
5638 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005639}
5640
Yan, Zheng21509082015-05-06 15:33:49 -04005641void perf_event_output(struct perf_event *event,
5642 struct perf_sample_data *data,
5643 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005644{
5645 struct perf_output_handle handle;
5646 struct perf_event_header header;
5647
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005648 /* protect the callchain buffers */
5649 rcu_read_lock();
5650
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005651 perf_prepare_sample(&header, data, event, regs);
5652
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005653 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005654 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005655
5656 perf_output_sample(&handle, &header, data, event);
5657
5658 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005659
5660exit:
5661 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005662}
5663
5664/*
5665 * read event_id
5666 */
5667
5668struct perf_read_event {
5669 struct perf_event_header header;
5670
5671 u32 pid;
5672 u32 tid;
5673};
5674
5675static void
5676perf_event_read_event(struct perf_event *event,
5677 struct task_struct *task)
5678{
5679 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005680 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005681 struct perf_read_event read_event = {
5682 .header = {
5683 .type = PERF_RECORD_READ,
5684 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005685 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005686 },
5687 .pid = perf_event_pid(event, task),
5688 .tid = perf_event_tid(event, task),
5689 };
5690 int ret;
5691
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005692 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005693 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005694 if (ret)
5695 return;
5696
5697 perf_output_put(&handle, read_event);
5698 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005699 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005700
5701 perf_output_end(&handle);
5702}
5703
Jiri Olsa52d857a2013-05-06 18:27:18 +02005704typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5705
5706static void
5707perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005708 perf_event_aux_output_cb output,
5709 void *data)
5710{
5711 struct perf_event *event;
5712
5713 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5714 if (event->state < PERF_EVENT_STATE_INACTIVE)
5715 continue;
5716 if (!event_filter_match(event))
5717 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02005718 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005719 }
5720}
5721
5722static void
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005723perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data,
5724 struct perf_event_context *task_ctx)
5725{
5726 rcu_read_lock();
5727 preempt_disable();
5728 perf_event_aux_ctx(task_ctx, output, data);
5729 preempt_enable();
5730 rcu_read_unlock();
5731}
5732
5733static void
Jiri Olsa67516842013-07-09 18:56:31 +02005734perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005735 struct perf_event_context *task_ctx)
5736{
5737 struct perf_cpu_context *cpuctx;
5738 struct perf_event_context *ctx;
5739 struct pmu *pmu;
5740 int ctxn;
5741
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005742 /*
5743 * If we have task_ctx != NULL we only notify
5744 * the task context itself. The task_ctx is set
5745 * only for EXIT events before releasing task
5746 * context.
5747 */
5748 if (task_ctx) {
5749 perf_event_aux_task_ctx(output, data, task_ctx);
5750 return;
5751 }
5752
Jiri Olsa52d857a2013-05-06 18:27:18 +02005753 rcu_read_lock();
5754 list_for_each_entry_rcu(pmu, &pmus, entry) {
5755 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5756 if (cpuctx->unique_pmu != pmu)
5757 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02005758 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005759 ctxn = pmu->task_ctx_nr;
5760 if (ctxn < 0)
5761 goto next;
5762 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5763 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02005764 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005765next:
5766 put_cpu_ptr(pmu->pmu_cpu_context);
5767 }
Jiri Olsa52d857a2013-05-06 18:27:18 +02005768 rcu_read_unlock();
5769}
5770
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005771/*
5772 * task tracking -- fork/exit
5773 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02005774 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005775 */
5776
5777struct perf_task_event {
5778 struct task_struct *task;
5779 struct perf_event_context *task_ctx;
5780
5781 struct {
5782 struct perf_event_header header;
5783
5784 u32 pid;
5785 u32 ppid;
5786 u32 tid;
5787 u32 ptid;
5788 u64 time;
5789 } event_id;
5790};
5791
Jiri Olsa67516842013-07-09 18:56:31 +02005792static int perf_event_task_match(struct perf_event *event)
5793{
Stephane Eranian13d7a242013-08-21 12:10:24 +02005794 return event->attr.comm || event->attr.mmap ||
5795 event->attr.mmap2 || event->attr.mmap_data ||
5796 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02005797}
5798
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005799static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005800 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005801{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005802 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005803 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005804 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005805 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005806 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01005807
Jiri Olsa67516842013-07-09 18:56:31 +02005808 if (!perf_event_task_match(event))
5809 return;
5810
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005811 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005812
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005813 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005814 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02005815 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005816 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005817
5818 task_event->event_id.pid = perf_event_pid(event, task);
5819 task_event->event_id.ppid = perf_event_pid(event, current);
5820
5821 task_event->event_id.tid = perf_event_tid(event, task);
5822 task_event->event_id.ptid = perf_event_tid(event, current);
5823
Peter Zijlstra34f43922015-02-20 14:05:38 +01005824 task_event->event_id.time = perf_event_clock(event);
5825
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005826 perf_output_put(&handle, task_event->event_id);
5827
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005828 perf_event__output_id_sample(event, &handle, &sample);
5829
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005830 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005831out:
5832 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005833}
5834
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005835static void perf_event_task(struct task_struct *task,
5836 struct perf_event_context *task_ctx,
5837 int new)
5838{
5839 struct perf_task_event task_event;
5840
5841 if (!atomic_read(&nr_comm_events) &&
5842 !atomic_read(&nr_mmap_events) &&
5843 !atomic_read(&nr_task_events))
5844 return;
5845
5846 task_event = (struct perf_task_event){
5847 .task = task,
5848 .task_ctx = task_ctx,
5849 .event_id = {
5850 .header = {
5851 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5852 .misc = 0,
5853 .size = sizeof(task_event.event_id),
5854 },
5855 /* .pid */
5856 /* .ppid */
5857 /* .tid */
5858 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01005859 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005860 },
5861 };
5862
Jiri Olsa67516842013-07-09 18:56:31 +02005863 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005864 &task_event,
5865 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005866}
5867
5868void perf_event_fork(struct task_struct *task)
5869{
5870 perf_event_task(task, NULL, 1);
5871}
5872
5873/*
5874 * comm tracking
5875 */
5876
5877struct perf_comm_event {
5878 struct task_struct *task;
5879 char *comm;
5880 int comm_size;
5881
5882 struct {
5883 struct perf_event_header header;
5884
5885 u32 pid;
5886 u32 tid;
5887 } event_id;
5888};
5889
Jiri Olsa67516842013-07-09 18:56:31 +02005890static int perf_event_comm_match(struct perf_event *event)
5891{
5892 return event->attr.comm;
5893}
5894
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005895static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005896 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005897{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005898 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005899 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005900 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005901 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005902 int ret;
5903
Jiri Olsa67516842013-07-09 18:56:31 +02005904 if (!perf_event_comm_match(event))
5905 return;
5906
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005907 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5908 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005909 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005910
5911 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005912 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005913
5914 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5915 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5916
5917 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005918 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005919 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005920
5921 perf_event__output_id_sample(event, &handle, &sample);
5922
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005923 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005924out:
5925 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005926}
5927
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005928static void perf_event_comm_event(struct perf_comm_event *comm_event)
5929{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005930 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005931 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005932
5933 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005934 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005935 size = ALIGN(strlen(comm)+1, sizeof(u64));
5936
5937 comm_event->comm = comm;
5938 comm_event->comm_size = size;
5939
5940 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005941
Jiri Olsa67516842013-07-09 18:56:31 +02005942 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005943 comm_event,
5944 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005945}
5946
Adrian Hunter82b89772014-05-28 11:45:04 +03005947void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005948{
5949 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005950
5951 if (!atomic_read(&nr_comm_events))
5952 return;
5953
5954 comm_event = (struct perf_comm_event){
5955 .task = task,
5956 /* .comm */
5957 /* .comm_size */
5958 .event_id = {
5959 .header = {
5960 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005961 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005962 /* .size */
5963 },
5964 /* .pid */
5965 /* .tid */
5966 },
5967 };
5968
5969 perf_event_comm_event(&comm_event);
5970}
5971
5972/*
5973 * mmap tracking
5974 */
5975
5976struct perf_mmap_event {
5977 struct vm_area_struct *vma;
5978
5979 const char *file_name;
5980 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005981 int maj, min;
5982 u64 ino;
5983 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005984 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005985
5986 struct {
5987 struct perf_event_header header;
5988
5989 u32 pid;
5990 u32 tid;
5991 u64 start;
5992 u64 len;
5993 u64 pgoff;
5994 } event_id;
5995};
5996
Jiri Olsa67516842013-07-09 18:56:31 +02005997static int perf_event_mmap_match(struct perf_event *event,
5998 void *data)
5999{
6000 struct perf_mmap_event *mmap_event = data;
6001 struct vm_area_struct *vma = mmap_event->vma;
6002 int executable = vma->vm_flags & VM_EXEC;
6003
6004 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02006005 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02006006}
6007
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006008static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006009 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006010{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006011 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006012 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006013 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006014 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006015 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006016
Jiri Olsa67516842013-07-09 18:56:31 +02006017 if (!perf_event_mmap_match(event, data))
6018 return;
6019
Stephane Eranian13d7a242013-08-21 12:10:24 +02006020 if (event->attr.mmap2) {
6021 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
6022 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
6023 mmap_event->event_id.header.size += sizeof(mmap_event->min);
6024 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03006025 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006026 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
6027 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006028 }
6029
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006030 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
6031 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006032 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006033 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006034 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006035
6036 mmap_event->event_id.pid = perf_event_pid(event, current);
6037 mmap_event->event_id.tid = perf_event_tid(event, current);
6038
6039 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006040
6041 if (event->attr.mmap2) {
6042 perf_output_put(&handle, mmap_event->maj);
6043 perf_output_put(&handle, mmap_event->min);
6044 perf_output_put(&handle, mmap_event->ino);
6045 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006046 perf_output_put(&handle, mmap_event->prot);
6047 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006048 }
6049
Frederic Weisbecker76369132011-05-19 19:55:04 +02006050 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006051 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006052
6053 perf_event__output_id_sample(event, &handle, &sample);
6054
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006055 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006056out:
6057 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006058}
6059
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006060static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
6061{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006062 struct vm_area_struct *vma = mmap_event->vma;
6063 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006064 int maj = 0, min = 0;
6065 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006066 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006067 unsigned int size;
6068 char tmp[16];
6069 char *buf = NULL;
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02006070 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006071
6072 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02006073 struct inode *inode;
6074 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006075
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02006076 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006077 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006078 name = "//enomem";
6079 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006080 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006081 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006082 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006083 * need to add enough zero bytes after the string to handle
6084 * the 64bit alignment we do later.
6085 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02006086 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006087 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006088 name = "//toolong";
6089 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006090 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02006091 inode = file_inode(vma->vm_file);
6092 dev = inode->i_sb->s_dev;
6093 ino = inode->i_ino;
6094 gen = inode->i_generation;
6095 maj = MAJOR(dev);
6096 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006097
6098 if (vma->vm_flags & VM_READ)
6099 prot |= PROT_READ;
6100 if (vma->vm_flags & VM_WRITE)
6101 prot |= PROT_WRITE;
6102 if (vma->vm_flags & VM_EXEC)
6103 prot |= PROT_EXEC;
6104
6105 if (vma->vm_flags & VM_MAYSHARE)
6106 flags = MAP_SHARED;
6107 else
6108 flags = MAP_PRIVATE;
6109
6110 if (vma->vm_flags & VM_DENYWRITE)
6111 flags |= MAP_DENYWRITE;
6112 if (vma->vm_flags & VM_MAYEXEC)
6113 flags |= MAP_EXECUTABLE;
6114 if (vma->vm_flags & VM_LOCKED)
6115 flags |= MAP_LOCKED;
6116 if (vma->vm_flags & VM_HUGETLB)
6117 flags |= MAP_HUGETLB;
6118
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006119 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006120 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006121 if (vma->vm_ops && vma->vm_ops->name) {
6122 name = (char *) vma->vm_ops->name(vma);
6123 if (name)
6124 goto cpy_name;
6125 }
6126
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02006127 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006128 if (name)
6129 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006130
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006131 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006132 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006133 name = "[heap]";
6134 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006135 }
6136 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006137 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006138 name = "[stack]";
6139 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006140 }
6141
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006142 name = "//anon";
6143 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006144 }
6145
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006146cpy_name:
6147 strlcpy(tmp, name, sizeof(tmp));
6148 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006149got_name:
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02006150 /*
6151 * Since our buffer works in 8 byte units we need to align our string
6152 * size to a multiple of 8. However, we must guarantee the tail end is
6153 * zero'd out to avoid leaking random bits to userspace.
6154 */
6155 size = strlen(name)+1;
6156 while (!IS_ALIGNED(size, sizeof(u64)))
6157 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006158
6159 mmap_event->file_name = name;
6160 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006161 mmap_event->maj = maj;
6162 mmap_event->min = min;
6163 mmap_event->ino = ino;
6164 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006165 mmap_event->prot = prot;
6166 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006167
Stephane Eranian2fe85422013-01-24 16:10:39 +01006168 if (!(vma->vm_flags & VM_EXEC))
6169 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6170
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006171 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6172
Jiri Olsa67516842013-07-09 18:56:31 +02006173 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006174 mmap_event,
6175 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006176
6177 kfree(buf);
6178}
6179
Eric B Munson3af9e852010-05-18 15:30:49 +01006180void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006181{
6182 struct perf_mmap_event mmap_event;
6183
6184 if (!atomic_read(&nr_mmap_events))
6185 return;
6186
6187 mmap_event = (struct perf_mmap_event){
6188 .vma = vma,
6189 /* .file_name */
6190 /* .file_size */
6191 .event_id = {
6192 .header = {
6193 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006194 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006195 /* .size */
6196 },
6197 /* .pid */
6198 /* .tid */
6199 .start = vma->vm_start,
6200 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006201 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006202 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006203 /* .maj (attr_mmap2 only) */
6204 /* .min (attr_mmap2 only) */
6205 /* .ino (attr_mmap2 only) */
6206 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006207 /* .prot (attr_mmap2 only) */
6208 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006209 };
6210
6211 perf_event_mmap_event(&mmap_event);
6212}
6213
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006214void perf_event_aux_event(struct perf_event *event, unsigned long head,
6215 unsigned long size, u64 flags)
6216{
6217 struct perf_output_handle handle;
6218 struct perf_sample_data sample;
6219 struct perf_aux_event {
6220 struct perf_event_header header;
6221 u64 offset;
6222 u64 size;
6223 u64 flags;
6224 } rec = {
6225 .header = {
6226 .type = PERF_RECORD_AUX,
6227 .misc = 0,
6228 .size = sizeof(rec),
6229 },
6230 .offset = head,
6231 .size = size,
6232 .flags = flags,
6233 };
6234 int ret;
6235
6236 perf_event_header__init_id(&rec.header, &sample, event);
6237 ret = perf_output_begin(&handle, event, rec.header.size);
6238
6239 if (ret)
6240 return;
6241
6242 perf_output_put(&handle, rec);
6243 perf_event__output_id_sample(event, &handle, &sample);
6244
6245 perf_output_end(&handle);
6246}
6247
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006248/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006249 * Lost/dropped samples logging
6250 */
6251void perf_log_lost_samples(struct perf_event *event, u64 lost)
6252{
6253 struct perf_output_handle handle;
6254 struct perf_sample_data sample;
6255 int ret;
6256
6257 struct {
6258 struct perf_event_header header;
6259 u64 lost;
6260 } lost_samples_event = {
6261 .header = {
6262 .type = PERF_RECORD_LOST_SAMPLES,
6263 .misc = 0,
6264 .size = sizeof(lost_samples_event),
6265 },
6266 .lost = lost,
6267 };
6268
6269 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6270
6271 ret = perf_output_begin(&handle, event,
6272 lost_samples_event.header.size);
6273 if (ret)
6274 return;
6275
6276 perf_output_put(&handle, lost_samples_event);
6277 perf_event__output_id_sample(event, &handle, &sample);
6278 perf_output_end(&handle);
6279}
6280
6281/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006282 * context_switch tracking
6283 */
6284
6285struct perf_switch_event {
6286 struct task_struct *task;
6287 struct task_struct *next_prev;
6288
6289 struct {
6290 struct perf_event_header header;
6291 u32 next_prev_pid;
6292 u32 next_prev_tid;
6293 } event_id;
6294};
6295
6296static int perf_event_switch_match(struct perf_event *event)
6297{
6298 return event->attr.context_switch;
6299}
6300
6301static void perf_event_switch_output(struct perf_event *event, void *data)
6302{
6303 struct perf_switch_event *se = data;
6304 struct perf_output_handle handle;
6305 struct perf_sample_data sample;
6306 int ret;
6307
6308 if (!perf_event_switch_match(event))
6309 return;
6310
6311 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6312 if (event->ctx->task) {
6313 se->event_id.header.type = PERF_RECORD_SWITCH;
6314 se->event_id.header.size = sizeof(se->event_id.header);
6315 } else {
6316 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6317 se->event_id.header.size = sizeof(se->event_id);
6318 se->event_id.next_prev_pid =
6319 perf_event_pid(event, se->next_prev);
6320 se->event_id.next_prev_tid =
6321 perf_event_tid(event, se->next_prev);
6322 }
6323
6324 perf_event_header__init_id(&se->event_id.header, &sample, event);
6325
6326 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6327 if (ret)
6328 return;
6329
6330 if (event->ctx->task)
6331 perf_output_put(&handle, se->event_id.header);
6332 else
6333 perf_output_put(&handle, se->event_id);
6334
6335 perf_event__output_id_sample(event, &handle, &sample);
6336
6337 perf_output_end(&handle);
6338}
6339
6340static void perf_event_switch(struct task_struct *task,
6341 struct task_struct *next_prev, bool sched_in)
6342{
6343 struct perf_switch_event switch_event;
6344
6345 /* N.B. caller checks nr_switch_events != 0 */
6346
6347 switch_event = (struct perf_switch_event){
6348 .task = task,
6349 .next_prev = next_prev,
6350 .event_id = {
6351 .header = {
6352 /* .type */
6353 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
6354 /* .size */
6355 },
6356 /* .next_prev_pid */
6357 /* .next_prev_tid */
6358 },
6359 };
6360
6361 perf_event_aux(perf_event_switch_output,
6362 &switch_event,
6363 NULL);
6364}
6365
6366/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006367 * IRQ throttle logging
6368 */
6369
6370static void perf_log_throttle(struct perf_event *event, int enable)
6371{
6372 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006373 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006374 int ret;
6375
6376 struct {
6377 struct perf_event_header header;
6378 u64 time;
6379 u64 id;
6380 u64 stream_id;
6381 } throttle_event = {
6382 .header = {
6383 .type = PERF_RECORD_THROTTLE,
6384 .misc = 0,
6385 .size = sizeof(throttle_event),
6386 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01006387 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006388 .id = primary_event_id(event),
6389 .stream_id = event->id,
6390 };
6391
6392 if (enable)
6393 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
6394
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006395 perf_event_header__init_id(&throttle_event.header, &sample, event);
6396
6397 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006398 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006399 if (ret)
6400 return;
6401
6402 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006403 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006404 perf_output_end(&handle);
6405}
6406
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006407static void perf_log_itrace_start(struct perf_event *event)
6408{
6409 struct perf_output_handle handle;
6410 struct perf_sample_data sample;
6411 struct perf_aux_event {
6412 struct perf_event_header header;
6413 u32 pid;
6414 u32 tid;
6415 } rec;
6416 int ret;
6417
6418 if (event->parent)
6419 event = event->parent;
6420
6421 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
6422 event->hw.itrace_started)
6423 return;
6424
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006425 rec.header.type = PERF_RECORD_ITRACE_START;
6426 rec.header.misc = 0;
6427 rec.header.size = sizeof(rec);
6428 rec.pid = perf_event_pid(event, current);
6429 rec.tid = perf_event_tid(event, current);
6430
6431 perf_event_header__init_id(&rec.header, &sample, event);
6432 ret = perf_output_begin(&handle, event, rec.header.size);
6433
6434 if (ret)
6435 return;
6436
6437 perf_output_put(&handle, rec);
6438 perf_event__output_id_sample(event, &handle, &sample);
6439
6440 perf_output_end(&handle);
6441}
6442
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006443/*
6444 * Generic event overflow handling, sampling.
6445 */
6446
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006447static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006448 int throttle, struct perf_sample_data *data,
6449 struct pt_regs *regs)
6450{
6451 int events = atomic_read(&event->event_limit);
6452 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006453 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006454 int ret = 0;
6455
Peter Zijlstra96398822010-11-24 18:55:29 +01006456 /*
6457 * Non-sampling counters might still use the PMI to fold short
6458 * hardware counters, ignore those.
6459 */
6460 if (unlikely(!is_sampling_event(event)))
6461 return 0;
6462
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006463 seq = __this_cpu_read(perf_throttled_seq);
6464 if (seq != hwc->interrupts_seq) {
6465 hwc->interrupts_seq = seq;
6466 hwc->interrupts = 1;
6467 } else {
6468 hwc->interrupts++;
6469 if (unlikely(throttle
6470 && hwc->interrupts >= max_samples_per_tick)) {
6471 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02006472 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01006473 hwc->interrupts = MAX_INTERRUPTS;
6474 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006475 ret = 1;
6476 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006477 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006478
6479 if (event->attr.freq) {
6480 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01006481 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006482
Peter Zijlstraabd50712010-01-26 18:50:16 +01006483 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006484
Peter Zijlstraabd50712010-01-26 18:50:16 +01006485 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01006486 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006487 }
6488
6489 /*
6490 * XXX event_limit might not quite work as expected on inherited
6491 * events
6492 */
6493
6494 event->pending_kill = POLL_IN;
6495 if (events && atomic_dec_and_test(&event->event_limit)) {
6496 ret = 1;
6497 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006498 event->pending_disable = 1;
6499 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006500 }
6501
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006502 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006503 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006504 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006505 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006506
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006507 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006508 event->pending_wakeup = 1;
6509 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02006510 }
6511
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006512 return ret;
6513}
6514
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006515int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006516 struct perf_sample_data *data,
6517 struct pt_regs *regs)
6518{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006519 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006520}
6521
6522/*
6523 * Generic software event infrastructure
6524 */
6525
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006526struct swevent_htable {
6527 struct swevent_hlist *swevent_hlist;
6528 struct mutex hlist_mutex;
6529 int hlist_refcount;
6530
6531 /* Recursion avoidance in each contexts */
6532 int recursion[PERF_NR_CONTEXTS];
6533};
6534
6535static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
6536
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006537/*
6538 * We directly increment event->count and keep a second value in
6539 * event->hw.period_left to count intervals. This period event
6540 * is kept in the range [-sample_period, 0] so that we can use the
6541 * sign as trigger.
6542 */
6543
Jiri Olsaab573842013-05-01 17:25:44 +02006544u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006545{
6546 struct hw_perf_event *hwc = &event->hw;
6547 u64 period = hwc->last_period;
6548 u64 nr, offset;
6549 s64 old, val;
6550
6551 hwc->last_period = hwc->sample_period;
6552
6553again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02006554 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006555 if (val < 0)
6556 return 0;
6557
6558 nr = div64_u64(period + val, period);
6559 offset = nr * period;
6560 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02006561 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006562 goto again;
6563
6564 return nr;
6565}
6566
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006567static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006568 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006569 struct pt_regs *regs)
6570{
6571 struct hw_perf_event *hwc = &event->hw;
6572 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006573
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006574 if (!overflow)
6575 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006576
6577 if (hwc->interrupts == MAX_INTERRUPTS)
6578 return;
6579
6580 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006581 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006582 data, regs)) {
6583 /*
6584 * We inhibit the overflow from happening when
6585 * hwc->interrupts == MAX_INTERRUPTS.
6586 */
6587 break;
6588 }
6589 throttle = 1;
6590 }
6591}
6592
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006593static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006594 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006595 struct pt_regs *regs)
6596{
6597 struct hw_perf_event *hwc = &event->hw;
6598
Peter Zijlstrae7850592010-05-21 14:43:08 +02006599 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006600
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006601 if (!regs)
6602 return;
6603
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006604 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006605 return;
6606
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03006607 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
6608 data->period = nr;
6609 return perf_swevent_overflow(event, 1, data, regs);
6610 } else
6611 data->period = event->hw.last_period;
6612
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006613 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006614 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006615
Peter Zijlstrae7850592010-05-21 14:43:08 +02006616 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006617 return;
6618
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006619 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006620}
6621
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006622static int perf_exclude_event(struct perf_event *event,
6623 struct pt_regs *regs)
6624{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006625 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01006626 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006627
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006628 if (regs) {
6629 if (event->attr.exclude_user && user_mode(regs))
6630 return 1;
6631
6632 if (event->attr.exclude_kernel && !user_mode(regs))
6633 return 1;
6634 }
6635
6636 return 0;
6637}
6638
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006639static int perf_swevent_match(struct perf_event *event,
6640 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08006641 u32 event_id,
6642 struct perf_sample_data *data,
6643 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006644{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006645 if (event->attr.type != type)
6646 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006647
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006648 if (event->attr.config != event_id)
6649 return 0;
6650
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006651 if (perf_exclude_event(event, regs))
6652 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006653
6654 return 1;
6655}
6656
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006657static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006658{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006659 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006660
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006661 return hash_64(val, SWEVENT_HLIST_BITS);
6662}
6663
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006664static inline struct hlist_head *
6665__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006666{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006667 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006668
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006669 return &hlist->heads[hash];
6670}
6671
6672/* For the read side: events when they trigger */
6673static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006674find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006675{
6676 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006677
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006678 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006679 if (!hlist)
6680 return NULL;
6681
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006682 return __find_swevent_head(hlist, type, event_id);
6683}
6684
6685/* For the event head insertion and removal in the hlist */
6686static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006687find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006688{
6689 struct swevent_hlist *hlist;
6690 u32 event_id = event->attr.config;
6691 u64 type = event->attr.type;
6692
6693 /*
6694 * Event scheduling is always serialized against hlist allocation
6695 * and release. Which makes the protected version suitable here.
6696 * The context lock guarantees that.
6697 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006698 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006699 lockdep_is_held(&event->ctx->lock));
6700 if (!hlist)
6701 return NULL;
6702
6703 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006704}
6705
6706static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006707 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006708 struct perf_sample_data *data,
6709 struct pt_regs *regs)
6710{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006711 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006712 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006713 struct hlist_head *head;
6714
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006715 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006716 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006717 if (!head)
6718 goto end;
6719
Sasha Levinb67bfe02013-02-27 17:06:00 -08006720 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08006721 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006722 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006723 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006724end:
6725 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006726}
6727
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006728DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
6729
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006730int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006731{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006732 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006733
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006734 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006735}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01006736EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006737
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01006738inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006739{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006740 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006741
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006742 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006743}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006744
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006745void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006746{
Ingo Molnara4234bf2009-11-23 10:57:59 +01006747 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006748
6749 if (WARN_ON_ONCE(!regs))
6750 return;
6751
6752 perf_sample_data_init(&data, addr, 0);
6753 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
6754}
6755
6756void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
6757{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006758 int rctx;
6759
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006760 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006761 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006762 if (unlikely(rctx < 0))
6763 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006764
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006765 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006766
6767 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006768fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006769 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006770}
6771
6772static void perf_swevent_read(struct perf_event *event)
6773{
6774}
6775
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006776static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006777{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006778 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006779 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006780 struct hlist_head *head;
6781
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006782 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006783 hwc->last_period = hwc->sample_period;
6784 perf_swevent_set_period(event);
6785 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006786
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006787 hwc->state = !(flags & PERF_EF_START);
6788
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006789 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01006790 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006791 return -EINVAL;
6792
6793 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08006794 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006795
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006796 return 0;
6797}
6798
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006799static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006800{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006801 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006802}
6803
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006804static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006805{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006806 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006807}
6808
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006809static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006810{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006811 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006812}
6813
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006814/* Deref the hlist from the update side */
6815static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006816swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006817{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006818 return rcu_dereference_protected(swhash->swevent_hlist,
6819 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006820}
6821
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006822static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006823{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006824 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006825
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006826 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006827 return;
6828
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03006829 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08006830 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006831}
6832
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006833static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006834{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006835 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006836
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006837 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006838
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006839 if (!--swhash->hlist_refcount)
6840 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006841
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006842 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006843}
6844
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006845static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006846{
6847 int cpu;
6848
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006849 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006850 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006851}
6852
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006853static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006854{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006855 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006856 int err = 0;
6857
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006858 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006859 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006860 struct swevent_hlist *hlist;
6861
6862 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
6863 if (!hlist) {
6864 err = -ENOMEM;
6865 goto exit;
6866 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006867 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006868 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006869 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006870exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006871 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006872
6873 return err;
6874}
6875
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006876static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006877{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006878 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006879
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006880 get_online_cpus();
6881 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006882 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006883 if (err) {
6884 failed_cpu = cpu;
6885 goto fail;
6886 }
6887 }
6888 put_online_cpus();
6889
6890 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006891fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006892 for_each_possible_cpu(cpu) {
6893 if (cpu == failed_cpu)
6894 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006895 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006896 }
6897
6898 put_online_cpus();
6899 return err;
6900}
6901
Ingo Molnarc5905af2012-02-24 08:31:31 +01006902struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006903
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006904static void sw_perf_event_destroy(struct perf_event *event)
6905{
6906 u64 event_id = event->attr.config;
6907
6908 WARN_ON(event->parent);
6909
Ingo Molnarc5905af2012-02-24 08:31:31 +01006910 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006911 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006912}
6913
6914static int perf_swevent_init(struct perf_event *event)
6915{
Tommi Rantala8176cce2013-04-13 22:49:14 +03006916 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006917
6918 if (event->attr.type != PERF_TYPE_SOFTWARE)
6919 return -ENOENT;
6920
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006921 /*
6922 * no branch sampling for software events
6923 */
6924 if (has_branch_stack(event))
6925 return -EOPNOTSUPP;
6926
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006927 switch (event_id) {
6928 case PERF_COUNT_SW_CPU_CLOCK:
6929 case PERF_COUNT_SW_TASK_CLOCK:
6930 return -ENOENT;
6931
6932 default:
6933 break;
6934 }
6935
Dan Carpenterce677832010-10-24 21:50:42 +02006936 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006937 return -ENOENT;
6938
6939 if (!event->parent) {
6940 int err;
6941
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006942 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006943 if (err)
6944 return err;
6945
Ingo Molnarc5905af2012-02-24 08:31:31 +01006946 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006947 event->destroy = sw_perf_event_destroy;
6948 }
6949
6950 return 0;
6951}
6952
6953static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006954 .task_ctx_nr = perf_sw_context,
6955
Peter Zijlstra34f43922015-02-20 14:05:38 +01006956 .capabilities = PERF_PMU_CAP_NO_NMI,
6957
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006958 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006959 .add = perf_swevent_add,
6960 .del = perf_swevent_del,
6961 .start = perf_swevent_start,
6962 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006963 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006964};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006965
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006966#ifdef CONFIG_EVENT_TRACING
6967
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006968static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006969 struct perf_sample_data *data)
6970{
6971 void *record = data->raw->data;
6972
Peter Zijlstrab71b4372015-11-02 10:50:51 +01006973 /* only top level events have filters set */
6974 if (event->parent)
6975 event = event->parent;
6976
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006977 if (likely(!event->filter) || filter_match_preds(event->filter, record))
6978 return 1;
6979 return 0;
6980}
6981
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006982static int perf_tp_event_match(struct perf_event *event,
6983 struct perf_sample_data *data,
6984 struct pt_regs *regs)
6985{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01006986 if (event->hw.state & PERF_HES_STOPPED)
6987 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02006988 /*
6989 * All tracepoints are from kernel-space.
6990 */
6991 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006992 return 0;
6993
6994 if (!perf_tp_filter_match(event, data))
6995 return 0;
6996
6997 return 1;
6998}
6999
7000void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007001 struct pt_regs *regs, struct hlist_head *head, int rctx,
7002 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007003{
7004 struct perf_sample_data data;
7005 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007006
7007 struct perf_raw_record raw = {
7008 .size = entry_size,
7009 .data = record,
7010 };
7011
Robert Richterfd0d0002012-04-02 20:19:08 +02007012 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007013 data.raw = &raw;
7014
Sasha Levinb67bfe02013-02-27 17:06:00 -08007015 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007016 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007017 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007018 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007019
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007020 /*
7021 * If we got specified a target task, also iterate its context and
7022 * deliver this event there too.
7023 */
7024 if (task && task != current) {
7025 struct perf_event_context *ctx;
7026 struct trace_entry *entry = record;
7027
7028 rcu_read_lock();
7029 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
7030 if (!ctx)
7031 goto unlock;
7032
7033 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
7034 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7035 continue;
7036 if (event->attr.config != entry->type)
7037 continue;
7038 if (perf_tp_event_match(event, &data, regs))
7039 perf_swevent_event(event, count, &data, regs);
7040 }
7041unlock:
7042 rcu_read_unlock();
7043 }
7044
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007045 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007046}
7047EXPORT_SYMBOL_GPL(perf_tp_event);
7048
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007049static void tp_perf_event_destroy(struct perf_event *event)
7050{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007051 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007052}
7053
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007054static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007055{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007056 int err;
7057
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007058 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7059 return -ENOENT;
7060
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007061 /*
7062 * no branch sampling for tracepoint events
7063 */
7064 if (has_branch_stack(event))
7065 return -EOPNOTSUPP;
7066
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007067 err = perf_trace_init(event);
7068 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007069 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007070
7071 event->destroy = tp_perf_event_destroy;
7072
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007073 return 0;
7074}
7075
7076static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007077 .task_ctx_nr = perf_sw_context,
7078
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007079 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007080 .add = perf_trace_add,
7081 .del = perf_trace_del,
7082 .start = perf_swevent_start,
7083 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007084 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007085};
7086
7087static inline void perf_tp_register(void)
7088{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007089 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007090}
Li Zefan6fb29152009-10-15 11:21:42 +08007091
7092static int perf_event_set_filter(struct perf_event *event, void __user *arg)
7093{
7094 char *filter_str;
7095 int ret;
7096
7097 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7098 return -EINVAL;
7099
7100 filter_str = strndup_user(arg, PAGE_SIZE);
7101 if (IS_ERR(filter_str))
7102 return PTR_ERR(filter_str);
7103
7104 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
7105
7106 kfree(filter_str);
7107 return ret;
7108}
7109
7110static void perf_event_free_filter(struct perf_event *event)
7111{
7112 ftrace_profile_free_filter(event);
7113}
7114
Alexei Starovoitov25415172015-03-25 12:49:20 -07007115static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7116{
7117 struct bpf_prog *prog;
7118
7119 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7120 return -EINVAL;
7121
7122 if (event->tp_event->prog)
7123 return -EEXIST;
7124
Wang Nan04a22fa2015-07-01 02:13:50 +00007125 if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE))
7126 /* bpf programs can only be attached to u/kprobes */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007127 return -EINVAL;
7128
7129 prog = bpf_prog_get(prog_fd);
7130 if (IS_ERR(prog))
7131 return PTR_ERR(prog);
7132
Linus Torvalds6c373ca2015-04-15 09:00:47 -07007133 if (prog->type != BPF_PROG_TYPE_KPROBE) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007134 /* valid fd, but invalid bpf program type */
7135 bpf_prog_put(prog);
7136 return -EINVAL;
7137 }
7138
7139 event->tp_event->prog = prog;
7140
7141 return 0;
7142}
7143
7144static void perf_event_free_bpf_prog(struct perf_event *event)
7145{
7146 struct bpf_prog *prog;
7147
7148 if (!event->tp_event)
7149 return;
7150
7151 prog = event->tp_event->prog;
7152 if (prog) {
7153 event->tp_event->prog = NULL;
7154 bpf_prog_put(prog);
7155 }
7156}
7157
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007158#else
Li Zefan6fb29152009-10-15 11:21:42 +08007159
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007160static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007161{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007162}
Li Zefan6fb29152009-10-15 11:21:42 +08007163
7164static int perf_event_set_filter(struct perf_event *event, void __user *arg)
7165{
7166 return -ENOENT;
7167}
7168
7169static void perf_event_free_filter(struct perf_event *event)
7170{
7171}
7172
Alexei Starovoitov25415172015-03-25 12:49:20 -07007173static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7174{
7175 return -ENOENT;
7176}
7177
7178static void perf_event_free_bpf_prog(struct perf_event *event)
7179{
7180}
Li Zefan07b139c2009-12-21 14:27:35 +08007181#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007182
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007183#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007184void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007185{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007186 struct perf_sample_data sample;
7187 struct pt_regs *regs = data;
7188
Robert Richterfd0d0002012-04-02 20:19:08 +02007189 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007190
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007191 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007192 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007193}
7194#endif
7195
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007196/*
7197 * hrtimer based swevent callback
7198 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007199
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007200static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007201{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007202 enum hrtimer_restart ret = HRTIMER_RESTART;
7203 struct perf_sample_data data;
7204 struct pt_regs *regs;
7205 struct perf_event *event;
7206 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007207
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007208 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007209
7210 if (event->state != PERF_EVENT_STATE_ACTIVE)
7211 return HRTIMER_NORESTART;
7212
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007213 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007214
Robert Richterfd0d0002012-04-02 20:19:08 +02007215 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007216 regs = get_irq_regs();
7217
7218 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08007219 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02007220 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007221 ret = HRTIMER_NORESTART;
7222 }
7223
7224 period = max_t(u64, 10000, event->hw.sample_period);
7225 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
7226
7227 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007228}
7229
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007230static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007231{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007232 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007233 s64 period;
7234
7235 if (!is_sampling_event(event))
7236 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007237
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007238 period = local64_read(&hwc->period_left);
7239 if (period) {
7240 if (period < 0)
7241 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007242
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007243 local64_set(&hwc->period_left, 0);
7244 } else {
7245 period = max_t(u64, 10000, hwc->sample_period);
7246 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00007247 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
7248 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007249}
7250
7251static void perf_swevent_cancel_hrtimer(struct perf_event *event)
7252{
7253 struct hw_perf_event *hwc = &event->hw;
7254
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007255 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007256 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007257 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007258
7259 hrtimer_cancel(&hwc->hrtimer);
7260 }
7261}
7262
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007263static void perf_swevent_init_hrtimer(struct perf_event *event)
7264{
7265 struct hw_perf_event *hwc = &event->hw;
7266
7267 if (!is_sampling_event(event))
7268 return;
7269
7270 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
7271 hwc->hrtimer.function = perf_swevent_hrtimer;
7272
7273 /*
7274 * Since hrtimers have a fixed rate, we can do a static freq->period
7275 * mapping and avoid the whole period adjust feedback stuff.
7276 */
7277 if (event->attr.freq) {
7278 long freq = event->attr.sample_freq;
7279
7280 event->attr.sample_period = NSEC_PER_SEC / freq;
7281 hwc->sample_period = event->attr.sample_period;
7282 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09007283 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007284 event->attr.freq = 0;
7285 }
7286}
7287
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007288/*
7289 * Software event: cpu wall time clock
7290 */
7291
7292static void cpu_clock_event_update(struct perf_event *event)
7293{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007294 s64 prev;
7295 u64 now;
7296
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007297 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007298 prev = local64_xchg(&event->hw.prev_count, now);
7299 local64_add(now - prev, &event->count);
7300}
7301
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007302static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007303{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007304 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007305 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007306}
7307
7308static void cpu_clock_event_stop(struct perf_event *event, int flags)
7309{
7310 perf_swevent_cancel_hrtimer(event);
7311 cpu_clock_event_update(event);
7312}
7313
7314static int cpu_clock_event_add(struct perf_event *event, int flags)
7315{
7316 if (flags & PERF_EF_START)
7317 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08007318 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007319
7320 return 0;
7321}
7322
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007323static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007324{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007325 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007326}
7327
7328static void cpu_clock_event_read(struct perf_event *event)
7329{
7330 cpu_clock_event_update(event);
7331}
7332
7333static int cpu_clock_event_init(struct perf_event *event)
7334{
7335 if (event->attr.type != PERF_TYPE_SOFTWARE)
7336 return -ENOENT;
7337
7338 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
7339 return -ENOENT;
7340
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007341 /*
7342 * no branch sampling for software events
7343 */
7344 if (has_branch_stack(event))
7345 return -EOPNOTSUPP;
7346
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007347 perf_swevent_init_hrtimer(event);
7348
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007349 return 0;
7350}
7351
7352static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007353 .task_ctx_nr = perf_sw_context,
7354
Peter Zijlstra34f43922015-02-20 14:05:38 +01007355 .capabilities = PERF_PMU_CAP_NO_NMI,
7356
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007357 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007358 .add = cpu_clock_event_add,
7359 .del = cpu_clock_event_del,
7360 .start = cpu_clock_event_start,
7361 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007362 .read = cpu_clock_event_read,
7363};
7364
7365/*
7366 * Software event: task time clock
7367 */
7368
7369static void task_clock_event_update(struct perf_event *event, u64 now)
7370{
7371 u64 prev;
7372 s64 delta;
7373
7374 prev = local64_xchg(&event->hw.prev_count, now);
7375 delta = now - prev;
7376 local64_add(delta, &event->count);
7377}
7378
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007379static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007380{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007381 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007382 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007383}
7384
7385static void task_clock_event_stop(struct perf_event *event, int flags)
7386{
7387 perf_swevent_cancel_hrtimer(event);
7388 task_clock_event_update(event, event->ctx->time);
7389}
7390
7391static int task_clock_event_add(struct perf_event *event, int flags)
7392{
7393 if (flags & PERF_EF_START)
7394 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08007395 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007396
7397 return 0;
7398}
7399
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007400static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007401{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007402 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007403}
7404
7405static void task_clock_event_read(struct perf_event *event)
7406{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01007407 u64 now = perf_clock();
7408 u64 delta = now - event->ctx->timestamp;
7409 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007410
7411 task_clock_event_update(event, time);
7412}
7413
7414static int task_clock_event_init(struct perf_event *event)
7415{
7416 if (event->attr.type != PERF_TYPE_SOFTWARE)
7417 return -ENOENT;
7418
7419 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
7420 return -ENOENT;
7421
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007422 /*
7423 * no branch sampling for software events
7424 */
7425 if (has_branch_stack(event))
7426 return -EOPNOTSUPP;
7427
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007428 perf_swevent_init_hrtimer(event);
7429
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007430 return 0;
7431}
7432
7433static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007434 .task_ctx_nr = perf_sw_context,
7435
Peter Zijlstra34f43922015-02-20 14:05:38 +01007436 .capabilities = PERF_PMU_CAP_NO_NMI,
7437
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007438 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007439 .add = task_clock_event_add,
7440 .del = task_clock_event_del,
7441 .start = task_clock_event_start,
7442 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007443 .read = task_clock_event_read,
7444};
7445
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007446static void perf_pmu_nop_void(struct pmu *pmu)
7447{
7448}
7449
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007450static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
7451{
7452}
7453
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007454static int perf_pmu_nop_int(struct pmu *pmu)
7455{
7456 return 0;
7457}
7458
Geliang Tang18ab2cd2015-09-27 23:25:50 +08007459static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007460
7461static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007462{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007463 __this_cpu_write(nop_txn_flags, flags);
7464
7465 if (flags & ~PERF_PMU_TXN_ADD)
7466 return;
7467
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007468 perf_pmu_disable(pmu);
7469}
7470
7471static int perf_pmu_commit_txn(struct pmu *pmu)
7472{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007473 unsigned int flags = __this_cpu_read(nop_txn_flags);
7474
7475 __this_cpu_write(nop_txn_flags, 0);
7476
7477 if (flags & ~PERF_PMU_TXN_ADD)
7478 return 0;
7479
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007480 perf_pmu_enable(pmu);
7481 return 0;
7482}
7483
7484static void perf_pmu_cancel_txn(struct pmu *pmu)
7485{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007486 unsigned int flags = __this_cpu_read(nop_txn_flags);
7487
7488 __this_cpu_write(nop_txn_flags, 0);
7489
7490 if (flags & ~PERF_PMU_TXN_ADD)
7491 return;
7492
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007493 perf_pmu_enable(pmu);
7494}
7495
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007496static int perf_event_idx_default(struct perf_event *event)
7497{
Peter Zijlstrac719f562014-10-21 11:10:21 +02007498 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007499}
7500
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007501/*
7502 * Ensures all contexts with the same task_ctx_nr have the same
7503 * pmu_cpu_context too.
7504 */
Mark Rutland9e317042014-02-10 17:44:18 +00007505static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007506{
7507 struct pmu *pmu;
7508
7509 if (ctxn < 0)
7510 return NULL;
7511
7512 list_for_each_entry(pmu, &pmus, entry) {
7513 if (pmu->task_ctx_nr == ctxn)
7514 return pmu->pmu_cpu_context;
7515 }
7516
7517 return NULL;
7518}
7519
Peter Zijlstra51676952010-12-07 14:18:20 +01007520static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007521{
Peter Zijlstra51676952010-12-07 14:18:20 +01007522 int cpu;
7523
7524 for_each_possible_cpu(cpu) {
7525 struct perf_cpu_context *cpuctx;
7526
7527 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
7528
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007529 if (cpuctx->unique_pmu == old_pmu)
7530 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01007531 }
7532}
7533
7534static void free_pmu_context(struct pmu *pmu)
7535{
7536 struct pmu *i;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007537
7538 mutex_lock(&pmus_lock);
7539 /*
7540 * Like a real lame refcount.
7541 */
Peter Zijlstra51676952010-12-07 14:18:20 +01007542 list_for_each_entry(i, &pmus, entry) {
7543 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
7544 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007545 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01007546 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007547 }
7548
Peter Zijlstra51676952010-12-07 14:18:20 +01007549 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007550out:
7551 mutex_unlock(&pmus_lock);
7552}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007553static struct idr pmu_idr;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007554
Peter Zijlstraabe43402010-11-17 23:17:37 +01007555static ssize_t
7556type_show(struct device *dev, struct device_attribute *attr, char *page)
7557{
7558 struct pmu *pmu = dev_get_drvdata(dev);
7559
7560 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
7561}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007562static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007563
Stephane Eranian62b85632013-04-03 14:21:34 +02007564static ssize_t
7565perf_event_mux_interval_ms_show(struct device *dev,
7566 struct device_attribute *attr,
7567 char *page)
7568{
7569 struct pmu *pmu = dev_get_drvdata(dev);
7570
7571 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
7572}
7573
Peter Zijlstra272325c2015-04-15 11:41:58 +02007574static DEFINE_MUTEX(mux_interval_mutex);
7575
Stephane Eranian62b85632013-04-03 14:21:34 +02007576static ssize_t
7577perf_event_mux_interval_ms_store(struct device *dev,
7578 struct device_attribute *attr,
7579 const char *buf, size_t count)
7580{
7581 struct pmu *pmu = dev_get_drvdata(dev);
7582 int timer, cpu, ret;
7583
7584 ret = kstrtoint(buf, 0, &timer);
7585 if (ret)
7586 return ret;
7587
7588 if (timer < 1)
7589 return -EINVAL;
7590
7591 /* same value, noting to do */
7592 if (timer == pmu->hrtimer_interval_ms)
7593 return count;
7594
Peter Zijlstra272325c2015-04-15 11:41:58 +02007595 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02007596 pmu->hrtimer_interval_ms = timer;
7597
7598 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02007599 get_online_cpus();
7600 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02007601 struct perf_cpu_context *cpuctx;
7602 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
7603 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
7604
Peter Zijlstra272325c2015-04-15 11:41:58 +02007605 cpu_function_call(cpu,
7606 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02007607 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02007608 put_online_cpus();
7609 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02007610
7611 return count;
7612}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007613static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02007614
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007615static struct attribute *pmu_dev_attrs[] = {
7616 &dev_attr_type.attr,
7617 &dev_attr_perf_event_mux_interval_ms.attr,
7618 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01007619};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007620ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007621
7622static int pmu_bus_running;
7623static struct bus_type pmu_bus = {
7624 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007625 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01007626};
7627
7628static void pmu_dev_release(struct device *dev)
7629{
7630 kfree(dev);
7631}
7632
7633static int pmu_dev_alloc(struct pmu *pmu)
7634{
7635 int ret = -ENOMEM;
7636
7637 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
7638 if (!pmu->dev)
7639 goto out;
7640
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01007641 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01007642 device_initialize(pmu->dev);
7643 ret = dev_set_name(pmu->dev, "%s", pmu->name);
7644 if (ret)
7645 goto free_dev;
7646
7647 dev_set_drvdata(pmu->dev, pmu);
7648 pmu->dev->bus = &pmu_bus;
7649 pmu->dev->release = pmu_dev_release;
7650 ret = device_add(pmu->dev);
7651 if (ret)
7652 goto free_dev;
7653
7654out:
7655 return ret;
7656
7657free_dev:
7658 put_device(pmu->dev);
7659 goto out;
7660}
7661
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007662static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007663static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007664
Mischa Jonker03d8e802013-06-04 11:45:48 +02007665int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007666{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007667 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007668
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007669 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007670 ret = -ENOMEM;
7671 pmu->pmu_disable_count = alloc_percpu(int);
7672 if (!pmu->pmu_disable_count)
7673 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007674
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007675 pmu->type = -1;
7676 if (!name)
7677 goto skip_type;
7678 pmu->name = name;
7679
7680 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08007681 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
7682 if (type < 0) {
7683 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007684 goto free_pdc;
7685 }
7686 }
7687 pmu->type = type;
7688
Peter Zijlstraabe43402010-11-17 23:17:37 +01007689 if (pmu_bus_running) {
7690 ret = pmu_dev_alloc(pmu);
7691 if (ret)
7692 goto free_idr;
7693 }
7694
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007695skip_type:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007696 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
7697 if (pmu->pmu_cpu_context)
7698 goto got_cpu_context;
7699
Wei Yongjunc4814202013-04-12 11:05:54 +08007700 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007701 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
7702 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01007703 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007704
7705 for_each_possible_cpu(cpu) {
7706 struct perf_cpu_context *cpuctx;
7707
7708 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02007709 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007710 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007711 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007712 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02007713
Peter Zijlstra272325c2015-04-15 11:41:58 +02007714 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02007715
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007716 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007717 }
7718
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007719got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007720 if (!pmu->start_txn) {
7721 if (pmu->pmu_enable) {
7722 /*
7723 * If we have pmu_enable/pmu_disable calls, install
7724 * transaction stubs that use that to try and batch
7725 * hardware accesses.
7726 */
7727 pmu->start_txn = perf_pmu_start_txn;
7728 pmu->commit_txn = perf_pmu_commit_txn;
7729 pmu->cancel_txn = perf_pmu_cancel_txn;
7730 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007731 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007732 pmu->commit_txn = perf_pmu_nop_int;
7733 pmu->cancel_txn = perf_pmu_nop_void;
7734 }
7735 }
7736
7737 if (!pmu->pmu_enable) {
7738 pmu->pmu_enable = perf_pmu_nop_void;
7739 pmu->pmu_disable = perf_pmu_nop_void;
7740 }
7741
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007742 if (!pmu->event_idx)
7743 pmu->event_idx = perf_event_idx_default;
7744
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007745 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007746 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007747 ret = 0;
7748unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007749 mutex_unlock(&pmus_lock);
7750
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007751 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007752
Peter Zijlstraabe43402010-11-17 23:17:37 +01007753free_dev:
7754 device_del(pmu->dev);
7755 put_device(pmu->dev);
7756
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007757free_idr:
7758 if (pmu->type >= PERF_TYPE_MAX)
7759 idr_remove(&pmu_idr, pmu->type);
7760
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007761free_pdc:
7762 free_percpu(pmu->pmu_disable_count);
7763 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007764}
Yan, Zhengc464c762014-03-18 16:56:41 +08007765EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007766
7767void perf_pmu_unregister(struct pmu *pmu)
7768{
7769 mutex_lock(&pmus_lock);
7770 list_del_rcu(&pmu->entry);
7771 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007772
7773 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02007774 * We dereference the pmu list under both SRCU and regular RCU, so
7775 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007776 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007777 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02007778 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007779
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007780 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007781 if (pmu->type >= PERF_TYPE_MAX)
7782 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007783 device_del(pmu->dev);
7784 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01007785 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007786}
Yan, Zhengc464c762014-03-18 16:56:41 +08007787EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007788
Mark Rutlandcc34b982015-01-07 14:56:51 +00007789static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
7790{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007791 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00007792 int ret;
7793
7794 if (!try_module_get(pmu->module))
7795 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007796
7797 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02007798 /*
7799 * This ctx->mutex can nest when we're called through
7800 * inheritance. See the perf_event_ctx_lock_nested() comment.
7801 */
7802 ctx = perf_event_ctx_lock_nested(event->group_leader,
7803 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007804 BUG_ON(!ctx);
7805 }
7806
Mark Rutlandcc34b982015-01-07 14:56:51 +00007807 event->pmu = pmu;
7808 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007809
7810 if (ctx)
7811 perf_event_ctx_unlock(event->group_leader, ctx);
7812
Mark Rutlandcc34b982015-01-07 14:56:51 +00007813 if (ret)
7814 module_put(pmu->module);
7815
7816 return ret;
7817}
7818
Geliang Tang18ab2cd2015-09-27 23:25:50 +08007819static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007820{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007821 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007822 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08007823 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007824
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007825 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007826
7827 rcu_read_lock();
7828 pmu = idr_find(&pmu_idr, event->attr.type);
7829 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08007830 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007831 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08007832 if (ret)
7833 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007834 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08007835 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007836
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007837 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007838 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007839 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007840 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007841
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007842 if (ret != -ENOENT) {
7843 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007844 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007845 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007846 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007847 pmu = ERR_PTR(-ENOENT);
7848unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007849 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007850
7851 return pmu;
7852}
7853
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007854static void account_event_cpu(struct perf_event *event, int cpu)
7855{
7856 if (event->parent)
7857 return;
7858
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007859 if (is_cgroup_event(event))
7860 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
7861}
7862
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02007863/* Freq events need the tick to stay alive (see perf_event_task_tick). */
7864static void account_freq_event_nohz(void)
7865{
7866#ifdef CONFIG_NO_HZ_FULL
7867 /* Lock so we don't race with concurrent unaccount */
7868 spin_lock(&nr_freq_lock);
7869 if (atomic_inc_return(&nr_freq_events) == 1)
7870 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
7871 spin_unlock(&nr_freq_lock);
7872#endif
7873}
7874
7875static void account_freq_event(void)
7876{
7877 if (tick_nohz_full_enabled())
7878 account_freq_event_nohz();
7879 else
7880 atomic_inc(&nr_freq_events);
7881}
7882
7883
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007884static void account_event(struct perf_event *event)
7885{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007886 bool inc = false;
7887
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007888 if (event->parent)
7889 return;
7890
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007891 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007892 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007893 if (event->attr.mmap || event->attr.mmap_data)
7894 atomic_inc(&nr_mmap_events);
7895 if (event->attr.comm)
7896 atomic_inc(&nr_comm_events);
7897 if (event->attr.task)
7898 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02007899 if (event->attr.freq)
7900 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03007901 if (event->attr.context_switch) {
7902 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007903 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03007904 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007905 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007906 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007907 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007908 inc = true;
7909
Peter Zijlstra9107c892016-02-24 18:45:45 +01007910 if (inc) {
7911 if (atomic_inc_not_zero(&perf_sched_count))
7912 goto enabled;
7913
7914 mutex_lock(&perf_sched_mutex);
7915 if (!atomic_read(&perf_sched_count)) {
7916 static_branch_enable(&perf_sched_events);
7917 /*
7918 * Guarantee that all CPUs observe they key change and
7919 * call the perf scheduling hooks before proceeding to
7920 * install events that need them.
7921 */
7922 synchronize_sched();
7923 }
7924 /*
7925 * Now that we have waited for the sync_sched(), allow further
7926 * increments to by-pass the mutex.
7927 */
7928 atomic_inc(&perf_sched_count);
7929 mutex_unlock(&perf_sched_mutex);
7930 }
7931enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007932
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007933 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007934}
7935
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007936/*
7937 * Allocate and initialize a event structure
7938 */
7939static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007940perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007941 struct task_struct *task,
7942 struct perf_event *group_leader,
7943 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007944 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00007945 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007946{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007947 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007948 struct perf_event *event;
7949 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007950 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007951
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007952 if ((unsigned)cpu >= nr_cpu_ids) {
7953 if (!task || cpu != -1)
7954 return ERR_PTR(-EINVAL);
7955 }
7956
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007957 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007958 if (!event)
7959 return ERR_PTR(-ENOMEM);
7960
7961 /*
7962 * Single events are their own group leaders, with an
7963 * empty sibling list:
7964 */
7965 if (!group_leader)
7966 group_leader = event;
7967
7968 mutex_init(&event->child_mutex);
7969 INIT_LIST_HEAD(&event->child_list);
7970
7971 INIT_LIST_HEAD(&event->group_entry);
7972 INIT_LIST_HEAD(&event->event_entry);
7973 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007974 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01007975 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01007976 INIT_HLIST_NODE(&event->hlist_entry);
7977
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007978
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007979 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08007980 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007981
7982 mutex_init(&event->mmap_mutex);
7983
Al Viroa6fa9412012-08-20 14:59:25 +01007984 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007985 event->cpu = cpu;
7986 event->attr = *attr;
7987 event->group_leader = group_leader;
7988 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007989 event->oncpu = -1;
7990
7991 event->parent = parent_event;
7992
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08007993 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007994 event->id = atomic64_inc_return(&perf_event_id);
7995
7996 event->state = PERF_EVENT_STATE_INACTIVE;
7997
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007998 if (task) {
7999 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008000 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01008001 * XXX pmu::event_init needs to know what task to account to
8002 * and we cannot use the ctx information because we need the
8003 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008004 */
Peter Zijlstra50f16a82015-03-05 22:10:19 +01008005 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008006 }
8007
Peter Zijlstra34f43922015-02-20 14:05:38 +01008008 event->clock = &local_clock;
8009 if (parent_event)
8010 event->clock = parent_event->clock;
8011
Avi Kivity4dc0da82011-06-29 18:42:35 +03008012 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01008013 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03008014 context = parent_event->overflow_handler_context;
8015 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01008016
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01008017 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03008018 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02008019
Jiri Olsa0231bb52013-02-01 11:23:45 +01008020 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008021
8022 pmu = NULL;
8023
8024 hwc = &event->hw;
8025 hwc->sample_period = attr->sample_period;
8026 if (attr->freq && attr->sample_freq)
8027 hwc->sample_period = 1;
8028 hwc->last_period = hwc->sample_period;
8029
Peter Zijlstrae7850592010-05-21 14:43:08 +02008030 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008031
8032 /*
8033 * we currently do not support PERF_FORMAT_GROUP on inherited events
8034 */
8035 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008036 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008037
Yan, Zhenga46a2302014-11-04 21:56:06 -05008038 if (!has_branch_stack(event))
8039 event->attr.branch_sample_type = 0;
8040
Matt Fleming79dff512015-01-23 18:45:42 +00008041 if (cgroup_fd != -1) {
8042 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
8043 if (err)
8044 goto err_ns;
8045 }
8046
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008047 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008048 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008049 goto err_ns;
8050 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008051 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008052 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008053 }
8054
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008055 err = exclusive_event_init(event);
8056 if (err)
8057 goto err_pmu;
8058
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008059 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02008060 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
8061 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008062 if (err)
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008063 goto err_per_task;
Stephane Eraniand010b332012-02-09 23:21:00 +01008064 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008065 }
8066
Alexander Shishkin927a5572016-03-02 13:24:14 +02008067 /* symmetric to unaccount_event() in _free_event() */
8068 account_event(event);
8069
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008070 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008071
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008072err_per_task:
8073 exclusive_event_destroy(event);
8074
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008075err_pmu:
8076 if (event->destroy)
8077 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08008078 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008079err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00008080 if (is_cgroup_event(event))
8081 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008082 if (event->ns)
8083 put_pid_ns(event->ns);
8084 kfree(event);
8085
8086 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008087}
8088
8089static int perf_copy_attr(struct perf_event_attr __user *uattr,
8090 struct perf_event_attr *attr)
8091{
8092 u32 size;
8093 int ret;
8094
8095 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
8096 return -EFAULT;
8097
8098 /*
8099 * zero the full structure, so that a short copy will be nice.
8100 */
8101 memset(attr, 0, sizeof(*attr));
8102
8103 ret = get_user(size, &uattr->size);
8104 if (ret)
8105 return ret;
8106
8107 if (size > PAGE_SIZE) /* silly large */
8108 goto err_size;
8109
8110 if (!size) /* abi compat */
8111 size = PERF_ATTR_SIZE_VER0;
8112
8113 if (size < PERF_ATTR_SIZE_VER0)
8114 goto err_size;
8115
8116 /*
8117 * If we're handed a bigger struct than we know of,
8118 * ensure all the unknown bits are 0 - i.e. new
8119 * user-space does not rely on any kernel feature
8120 * extensions we dont know about yet.
8121 */
8122 if (size > sizeof(*attr)) {
8123 unsigned char __user *addr;
8124 unsigned char __user *end;
8125 unsigned char val;
8126
8127 addr = (void __user *)uattr + sizeof(*attr);
8128 end = (void __user *)uattr + size;
8129
8130 for (; addr < end; addr++) {
8131 ret = get_user(val, addr);
8132 if (ret)
8133 return ret;
8134 if (val)
8135 goto err_size;
8136 }
8137 size = sizeof(*attr);
8138 }
8139
8140 ret = copy_from_user(attr, uattr, size);
8141 if (ret)
8142 return -EFAULT;
8143
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05308144 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008145 return -EINVAL;
8146
8147 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
8148 return -EINVAL;
8149
8150 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
8151 return -EINVAL;
8152
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008153 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
8154 u64 mask = attr->branch_sample_type;
8155
8156 /* only using defined bits */
8157 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
8158 return -EINVAL;
8159
8160 /* at least one branch bit must be set */
8161 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
8162 return -EINVAL;
8163
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008164 /* propagate priv level, when not set for branch */
8165 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
8166
8167 /* exclude_kernel checked on syscall entry */
8168 if (!attr->exclude_kernel)
8169 mask |= PERF_SAMPLE_BRANCH_KERNEL;
8170
8171 if (!attr->exclude_user)
8172 mask |= PERF_SAMPLE_BRANCH_USER;
8173
8174 if (!attr->exclude_hv)
8175 mask |= PERF_SAMPLE_BRANCH_HV;
8176 /*
8177 * adjust user setting (for HW filter setup)
8178 */
8179 attr->branch_sample_type = mask;
8180 }
Stephane Eraniane7122092013-06-06 11:02:04 +02008181 /* privileged levels capture (kernel, hv): check permissions */
8182 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02008183 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8184 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008185 }
Jiri Olsa40189942012-08-07 15:20:37 +02008186
Jiri Olsac5ebced2012-08-07 15:20:40 +02008187 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02008188 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02008189 if (ret)
8190 return ret;
8191 }
8192
8193 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
8194 if (!arch_perf_have_user_stack_dump())
8195 return -ENOSYS;
8196
8197 /*
8198 * We have __u32 type for the size, but so far
8199 * we can only use __u16 as maximum due to the
8200 * __u16 sample size limit.
8201 */
8202 if (attr->sample_stack_user >= USHRT_MAX)
8203 ret = -EINVAL;
8204 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
8205 ret = -EINVAL;
8206 }
Jiri Olsa40189942012-08-07 15:20:37 +02008207
Stephane Eranian60e23642014-09-24 13:48:37 +02008208 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
8209 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008210out:
8211 return ret;
8212
8213err_size:
8214 put_user(sizeof(*attr), &uattr->size);
8215 ret = -E2BIG;
8216 goto out;
8217}
8218
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008219static int
8220perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008221{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01008222 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008223 int ret = -EINVAL;
8224
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008225 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008226 goto set;
8227
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008228 /* don't allow circular references */
8229 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008230 goto out;
8231
Peter Zijlstra0f139302010-05-20 14:35:15 +02008232 /*
8233 * Don't allow cross-cpu buffers
8234 */
8235 if (output_event->cpu != event->cpu)
8236 goto out;
8237
8238 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02008239 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02008240 */
8241 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
8242 goto out;
8243
Peter Zijlstra34f43922015-02-20 14:05:38 +01008244 /*
8245 * Mixing clocks in the same buffer is trouble you don't need.
8246 */
8247 if (output_event->clock != event->clock)
8248 goto out;
8249
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02008250 /*
8251 * If both events generate aux data, they must be on the same PMU
8252 */
8253 if (has_aux(event) && has_aux(output_event) &&
8254 event->pmu != output_event->pmu)
8255 goto out;
8256
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008257set:
8258 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008259 /* Can't redirect output if we've got an active mmap() */
8260 if (atomic_read(&event->mmap_count))
8261 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008262
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008263 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02008264 /* get the rb we want to redirect to */
8265 rb = ring_buffer_get(output_event);
8266 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008267 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008268 }
8269
Peter Zijlstrab69cf532014-03-14 10:50:33 +01008270 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02008271
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008272 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008273unlock:
8274 mutex_unlock(&event->mmap_mutex);
8275
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008276out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008277 return ret;
8278}
8279
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008280static void mutex_lock_double(struct mutex *a, struct mutex *b)
8281{
8282 if (b < a)
8283 swap(a, b);
8284
8285 mutex_lock(a);
8286 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
8287}
8288
Peter Zijlstra34f43922015-02-20 14:05:38 +01008289static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
8290{
8291 bool nmi_safe = false;
8292
8293 switch (clk_id) {
8294 case CLOCK_MONOTONIC:
8295 event->clock = &ktime_get_mono_fast_ns;
8296 nmi_safe = true;
8297 break;
8298
8299 case CLOCK_MONOTONIC_RAW:
8300 event->clock = &ktime_get_raw_fast_ns;
8301 nmi_safe = true;
8302 break;
8303
8304 case CLOCK_REALTIME:
8305 event->clock = &ktime_get_real_ns;
8306 break;
8307
8308 case CLOCK_BOOTTIME:
8309 event->clock = &ktime_get_boot_ns;
8310 break;
8311
8312 case CLOCK_TAI:
8313 event->clock = &ktime_get_tai_ns;
8314 break;
8315
8316 default:
8317 return -EINVAL;
8318 }
8319
8320 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
8321 return -EINVAL;
8322
8323 return 0;
8324}
8325
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008326/**
8327 * sys_perf_event_open - open a performance event, associate it to a task/cpu
8328 *
8329 * @attr_uptr: event_id type attributes for monitoring/sampling
8330 * @pid: target pid
8331 * @cpu: target cpu
8332 * @group_fd: group leader event fd
8333 */
8334SYSCALL_DEFINE5(perf_event_open,
8335 struct perf_event_attr __user *, attr_uptr,
8336 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
8337{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008338 struct perf_event *group_leader = NULL, *output_event = NULL;
8339 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008340 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008341 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008342 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04008343 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07008344 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008345 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04008346 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008347 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008348 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01008349 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00008350 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008351
8352 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02008353 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008354 return -EINVAL;
8355
8356 err = perf_copy_attr(attr_uptr, &attr);
8357 if (err)
8358 return err;
8359
8360 if (!attr.exclude_kernel) {
8361 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8362 return -EACCES;
8363 }
8364
8365 if (attr.freq) {
8366 if (attr.sample_freq > sysctl_perf_event_sample_rate)
8367 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02008368 } else {
8369 if (attr.sample_period & (1ULL << 63))
8370 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008371 }
8372
Stephane Eraniane5d13672011-02-14 11:20:01 +02008373 /*
8374 * In cgroup mode, the pid argument is used to pass the fd
8375 * opened to the cgroup directory in cgroupfs. The cpu argument
8376 * designates the cpu on which to monitor threads from that
8377 * cgroup.
8378 */
8379 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
8380 return -EINVAL;
8381
Yann Droneauda21b0b32014-01-05 21:36:33 +01008382 if (flags & PERF_FLAG_FD_CLOEXEC)
8383 f_flags |= O_CLOEXEC;
8384
8385 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04008386 if (event_fd < 0)
8387 return event_fd;
8388
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008389 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04008390 err = perf_fget_light(group_fd, &group);
8391 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008392 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04008393 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008394 if (flags & PERF_FLAG_FD_OUTPUT)
8395 output_event = group_leader;
8396 if (flags & PERF_FLAG_FD_NO_GROUP)
8397 group_leader = NULL;
8398 }
8399
Stephane Eraniane5d13672011-02-14 11:20:01 +02008400 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008401 task = find_lively_task_by_vpid(pid);
8402 if (IS_ERR(task)) {
8403 err = PTR_ERR(task);
8404 goto err_group_fd;
8405 }
8406 }
8407
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008408 if (task && group_leader &&
8409 group_leader->attr.inherit != attr.inherit) {
8410 err = -EINVAL;
8411 goto err_task;
8412 }
8413
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008414 get_online_cpus();
8415
Matt Fleming79dff512015-01-23 18:45:42 +00008416 if (flags & PERF_FLAG_PID_CGROUP)
8417 cgroup_fd = pid;
8418
Avi Kivity4dc0da82011-06-29 18:42:35 +03008419 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00008420 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008421 if (IS_ERR(event)) {
8422 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008423 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008424 }
8425
Vince Weaver53b25332014-05-16 17:12:12 -04008426 if (is_sampling_event(event)) {
8427 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
8428 err = -ENOTSUPP;
8429 goto err_alloc;
8430 }
8431 }
8432
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008433 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008434 * Special case software events and allow them to be part of
8435 * any hardware group.
8436 */
8437 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008438
Peter Zijlstra34f43922015-02-20 14:05:38 +01008439 if (attr.use_clockid) {
8440 err = perf_event_set_clock(event, attr.clockid);
8441 if (err)
8442 goto err_alloc;
8443 }
8444
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008445 if (group_leader &&
8446 (is_software_event(event) != is_software_event(group_leader))) {
8447 if (is_software_event(event)) {
8448 /*
8449 * If event and group_leader are not both a software
8450 * event, and event is, then group leader is not.
8451 *
8452 * Allow the addition of software events to !software
8453 * groups, this is safe because software events never
8454 * fail to schedule.
8455 */
8456 pmu = group_leader->pmu;
8457 } else if (is_software_event(group_leader) &&
8458 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
8459 /*
8460 * In case the group is a pure software group, and we
8461 * try to add a hardware event, move the whole group to
8462 * the hardware context.
8463 */
8464 move_group = 1;
8465 }
8466 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008467
8468 /*
8469 * Get the target context (task or percpu):
8470 */
Yan, Zheng4af57ef2014-11-04 21:56:01 -05008471 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008472 if (IS_ERR(ctx)) {
8473 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008474 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008475 }
8476
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008477 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
8478 err = -EBUSY;
8479 goto err_context;
8480 }
8481
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02008482 if (task) {
8483 put_task_struct(task);
8484 task = NULL;
8485 }
8486
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008487 /*
8488 * Look up the group leader (we will attach this event to it):
8489 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008490 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008491 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008492
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008493 /*
8494 * Do not allow a recursive hierarchy (this new sibling
8495 * becoming part of another group-sibling):
8496 */
8497 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008498 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01008499
8500 /* All events in a group should have the same clock */
8501 if (group_leader->clock != event->clock)
8502 goto err_context;
8503
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008504 /*
8505 * Do not allow to attach to a group in a different
8506 * task or CPU context:
8507 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008508 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01008509 /*
8510 * Make sure we're both on the same task, or both
8511 * per-cpu events.
8512 */
8513 if (group_leader->ctx->task != ctx->task)
8514 goto err_context;
8515
8516 /*
8517 * Make sure we're both events for the same CPU;
8518 * grouping events for different CPUs is broken; since
8519 * you can never concurrently schedule them anyhow.
8520 */
8521 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008522 goto err_context;
8523 } else {
8524 if (group_leader->ctx != ctx)
8525 goto err_context;
8526 }
8527
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008528 /*
8529 * Only a group leader can be exclusive or pinned
8530 */
8531 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008532 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008533 }
8534
8535 if (output_event) {
8536 err = perf_event_set_output(event, output_event);
8537 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008538 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008539 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008540
Yann Droneauda21b0b32014-01-05 21:36:33 +01008541 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
8542 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04008543 if (IS_ERR(event_file)) {
8544 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +02008545 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008546 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04008547 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008548
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008549 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008550 gctx = group_leader->ctx;
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008551 mutex_lock_double(&gctx->mutex, &ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008552 if (gctx->task == TASK_TOMBSTONE) {
8553 err = -ESRCH;
8554 goto err_locked;
8555 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008556 } else {
8557 mutex_lock(&ctx->mutex);
8558 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008559
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008560 if (ctx->task == TASK_TOMBSTONE) {
8561 err = -ESRCH;
8562 goto err_locked;
8563 }
8564
Peter Zijlstraa7239682015-09-09 19:06:33 +02008565 if (!perf_event_validate_size(event)) {
8566 err = -E2BIG;
8567 goto err_locked;
8568 }
8569
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008570 /*
8571 * Must be under the same ctx::mutex as perf_install_in_context(),
8572 * because we need to serialize with concurrent event creation.
8573 */
8574 if (!exclusive_event_installable(event, ctx)) {
8575 /* exclusive and group stuff are assumed mutually exclusive */
8576 WARN_ON_ONCE(move_group);
8577
8578 err = -EBUSY;
8579 goto err_locked;
8580 }
8581
8582 WARN_ON_ONCE(ctx->parent_ctx);
8583
8584 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008585 /*
8586 * See perf_event_ctx_lock() for comments on the details
8587 * of swizzling perf_event::ctx.
8588 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008589 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01008590
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008591 list_for_each_entry(sibling, &group_leader->sibling_list,
8592 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008593 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008594 put_ctx(gctx);
8595 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008596
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008597 /*
8598 * Wait for everybody to stop referencing the events through
8599 * the old lists, before installing it on new lists.
8600 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008601 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008602
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008603 /*
8604 * Install the group siblings before the group leader.
8605 *
8606 * Because a group leader will try and install the entire group
8607 * (through the sibling list, which is still in-tact), we can
8608 * end up with siblings installed in the wrong context.
8609 *
8610 * By installing siblings first we NO-OP because they're not
8611 * reachable through the group lists.
8612 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008613 list_for_each_entry(sibling, &group_leader->sibling_list,
8614 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008615 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01008616 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008617 get_ctx(ctx);
8618 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008619
8620 /*
8621 * Removing from the context ends up with disabled
8622 * event. What we want here is event in the initial
8623 * startup state, ready to be add into new context.
8624 */
8625 perf_event__state_init(group_leader);
8626 perf_install_in_context(ctx, group_leader, group_leader->cpu);
8627 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008628
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008629 /*
8630 * Now that all events are installed in @ctx, nothing
8631 * references @gctx anymore, so drop the last reference we have
8632 * on it.
8633 */
8634 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008635 }
8636
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02008637 /*
8638 * Precalculate sample_data sizes; do while holding ctx::mutex such
8639 * that we're serialized against further additions and before
8640 * perf_install_in_context() which is the point the event is active and
8641 * can use these values.
8642 */
8643 perf_event__header_size(event);
8644 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008645
Peter Zijlstra78cd2c72016-01-25 14:08:45 +01008646 event->owner = current;
8647
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08008648 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008649 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008650
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008651 if (move_group)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008652 mutex_unlock(&gctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008653 mutex_unlock(&ctx->mutex);
8654
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008655 put_online_cpus();
8656
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008657 mutex_lock(&current->perf_event_mutex);
8658 list_add_tail(&event->owner_entry, &current->perf_event_list);
8659 mutex_unlock(&current->perf_event_mutex);
8660
Peter Zijlstra8a495422010-05-27 15:47:49 +02008661 /*
8662 * Drop the reference on the group_event after placing the
8663 * new event on the sibling_list. This ensures destruction
8664 * of the group leader will find the pointer to itself in
8665 * perf_group_detach().
8666 */
Al Viro2903ff02012-08-28 12:52:22 -04008667 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04008668 fd_install(event_fd, event_file);
8669 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008670
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008671err_locked:
8672 if (move_group)
8673 mutex_unlock(&gctx->mutex);
8674 mutex_unlock(&ctx->mutex);
8675/* err_file: */
8676 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008677err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008678 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04008679 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008680err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +01008681 /*
8682 * If event_file is set, the fput() above will have called ->release()
8683 * and that will take care of freeing the event.
8684 */
8685 if (!event_file)
8686 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008687err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008688 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008689err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02008690 if (task)
8691 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008692err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04008693 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04008694err_fd:
8695 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008696 return err;
8697}
8698
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008699/**
8700 * perf_event_create_kernel_counter
8701 *
8702 * @attr: attributes of the counter to create
8703 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07008704 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008705 */
8706struct perf_event *
8707perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07008708 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008709 perf_overflow_handler_t overflow_handler,
8710 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008711{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008712 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008713 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008714 int err;
8715
8716 /*
8717 * Get the target context (task or percpu):
8718 */
8719
Avi Kivity4dc0da82011-06-29 18:42:35 +03008720 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00008721 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008722 if (IS_ERR(event)) {
8723 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008724 goto err;
8725 }
8726
Jiri Olsaf8697762014-08-01 14:33:01 +02008727 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008728 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +02008729
Yan, Zheng4af57ef2014-11-04 21:56:01 -05008730 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008731 if (IS_ERR(ctx)) {
8732 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008733 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008734 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008735
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008736 WARN_ON_ONCE(ctx->parent_ctx);
8737 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008738 if (ctx->task == TASK_TOMBSTONE) {
8739 err = -ESRCH;
8740 goto err_unlock;
8741 }
8742
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008743 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008744 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008745 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008746 }
8747
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008748 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008749 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008750 mutex_unlock(&ctx->mutex);
8751
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008752 return event;
8753
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008754err_unlock:
8755 mutex_unlock(&ctx->mutex);
8756 perf_unpin_context(ctx);
8757 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008758err_free:
8759 free_event(event);
8760err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008761 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008762}
8763EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
8764
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008765void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
8766{
8767 struct perf_event_context *src_ctx;
8768 struct perf_event_context *dst_ctx;
8769 struct perf_event *event, *tmp;
8770 LIST_HEAD(events);
8771
8772 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
8773 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
8774
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008775 /*
8776 * See perf_event_ctx_lock() for comments on the details
8777 * of swizzling perf_event::ctx.
8778 */
8779 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008780 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
8781 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008782 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02008783 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008784 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02008785 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008786 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008787
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008788 /*
8789 * Wait for the events to quiesce before re-instating them.
8790 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008791 synchronize_rcu();
8792
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008793 /*
8794 * Re-instate events in 2 passes.
8795 *
8796 * Skip over group leaders and only install siblings on this first
8797 * pass, siblings will not get enabled without a leader, however a
8798 * leader will enable its siblings, even if those are still on the old
8799 * context.
8800 */
8801 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
8802 if (event->group_leader == event)
8803 continue;
8804
8805 list_del(&event->migrate_entry);
8806 if (event->state >= PERF_EVENT_STATE_OFF)
8807 event->state = PERF_EVENT_STATE_INACTIVE;
8808 account_event_cpu(event, dst_cpu);
8809 perf_install_in_context(dst_ctx, event, dst_cpu);
8810 get_ctx(dst_ctx);
8811 }
8812
8813 /*
8814 * Once all the siblings are setup properly, install the group leaders
8815 * to make it go.
8816 */
Peter Zijlstra98861672013-10-03 16:02:23 +02008817 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
8818 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008819 if (event->state >= PERF_EVENT_STATE_OFF)
8820 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02008821 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008822 perf_install_in_context(dst_ctx, event, dst_cpu);
8823 get_ctx(dst_ctx);
8824 }
8825 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008826 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008827}
8828EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
8829
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008830static void sync_child_event(struct perf_event *child_event,
8831 struct task_struct *child)
8832{
8833 struct perf_event *parent_event = child_event->parent;
8834 u64 child_val;
8835
8836 if (child_event->attr.inherit_stat)
8837 perf_event_read_event(child_event, child);
8838
Peter Zijlstrab5e58792010-05-21 14:43:12 +02008839 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008840
8841 /*
8842 * Add back the child's count to the parent's count:
8843 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02008844 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008845 atomic64_add(child_event->total_time_enabled,
8846 &parent_event->child_total_time_enabled);
8847 atomic64_add(child_event->total_time_running,
8848 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008849}
8850
8851static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008852perf_event_exit_event(struct perf_event *child_event,
8853 struct perf_event_context *child_ctx,
8854 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008855{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008856 struct perf_event *parent_event = child_event->parent;
8857
Peter Zijlstra1903d502014-07-15 17:27:27 +02008858 /*
8859 * Do not destroy the 'original' grouping; because of the context
8860 * switch optimization the original events could've ended up in a
8861 * random child task.
8862 *
8863 * If we were to destroy the original group, all group related
8864 * operations would cease to function properly after this random
8865 * child dies.
8866 *
8867 * Do destroy all inherited groups, we don't care about those
8868 * and being thorough is better.
8869 */
Peter Zijlstra32132a32016-01-11 15:40:59 +01008870 raw_spin_lock_irq(&child_ctx->lock);
8871 WARN_ON_ONCE(child_ctx->is_active);
8872
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008873 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +01008874 perf_group_detach(child_event);
8875 list_del_event(child_event, child_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01008876 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +01008877 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008878
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008879 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008880 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008881 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008882 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -04008883 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008884 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008885 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008886 /*
8887 * Child events can be cleaned up.
8888 */
8889
8890 sync_child_event(child_event, child);
8891
8892 /*
8893 * Remove this event from the parent's list
8894 */
8895 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
8896 mutex_lock(&parent_event->child_mutex);
8897 list_del_init(&child_event->child_list);
8898 mutex_unlock(&parent_event->child_mutex);
8899
8900 /*
8901 * Kick perf_poll() for is_event_hup().
8902 */
8903 perf_event_wakeup(parent_event);
8904 free_event(child_event);
8905 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008906}
8907
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008908static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008909{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008910 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008911 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008912
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008913 WARN_ON_ONCE(child != current);
8914
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008915 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008916 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008917 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008918
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008919 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008920 * In order to reduce the amount of tricky in ctx tear-down, we hold
8921 * ctx::mutex over the entire thing. This serializes against almost
8922 * everything that wants to access the ctx.
8923 *
8924 * The exception is sys_perf_event_open() /
8925 * perf_event_create_kernel_count() which does find_get_context()
8926 * without ctx::mutex (it cannot because of the move_group double mutex
8927 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008928 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008929 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008930
8931 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008932 * In a single ctx::lock section, de-schedule the events and detach the
8933 * context from the task such that we cannot ever get it scheduled back
8934 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008935 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008936 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008937 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008938
8939 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008940 * Now that the context is inactive, destroy the task <-> ctx relation
8941 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008942 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008943 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
8944 put_ctx(child_ctx); /* cannot be last */
8945 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
8946 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008947
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008948 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008949 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008950
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008951 if (clone_ctx)
8952 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008953
8954 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008955 * Report the task dead after unscheduling the events so that we
8956 * won't get any samples after PERF_RECORD_EXIT. We can however still
8957 * get a few PERF_RECORD_READ events.
8958 */
8959 perf_event_task(child, child_ctx, 0);
8960
Peter Zijlstraebf905f2014-05-29 19:00:24 +02008961 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008962 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008963
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008964 mutex_unlock(&child_ctx->mutex);
8965
8966 put_ctx(child_ctx);
8967}
8968
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008969/*
8970 * When a child task exits, feed back event values to parent events.
8971 */
8972void perf_event_exit_task(struct task_struct *child)
8973{
Peter Zijlstra88821352010-11-09 19:01:43 +01008974 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008975 int ctxn;
8976
Peter Zijlstra88821352010-11-09 19:01:43 +01008977 mutex_lock(&child->perf_event_mutex);
8978 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
8979 owner_entry) {
8980 list_del_init(&event->owner_entry);
8981
8982 /*
8983 * Ensure the list deletion is visible before we clear
8984 * the owner, closes a race against perf_release() where
8985 * we need to serialize on the owner->perf_event_mutex.
8986 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01008987 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +01008988 }
8989 mutex_unlock(&child->perf_event_mutex);
8990
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008991 for_each_task_context_nr(ctxn)
8992 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +01008993
8994 /*
8995 * The perf_event_exit_task_context calls perf_event_task
8996 * with child's task_ctx, which generates EXIT events for
8997 * child contexts and sets child->perf_event_ctxp[] to NULL.
8998 * At this point we need to send EXIT events to cpu contexts.
8999 */
9000 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009001}
9002
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009003static void perf_free_event(struct perf_event *event,
9004 struct perf_event_context *ctx)
9005{
9006 struct perf_event *parent = event->parent;
9007
9008 if (WARN_ON_ONCE(!parent))
9009 return;
9010
9011 mutex_lock(&parent->child_mutex);
9012 list_del_init(&event->child_list);
9013 mutex_unlock(&parent->child_mutex);
9014
Al Viroa6fa9412012-08-20 14:59:25 +01009015 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009016
Peter Zijlstra652884f2015-01-23 11:20:10 +01009017 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02009018 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009019 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +01009020 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009021 free_event(event);
9022}
9023
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009024/*
Peter Zijlstra652884f2015-01-23 11:20:10 +01009025 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009026 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +01009027 *
9028 * Not all locks are strictly required, but take them anyway to be nice and
9029 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009030 */
9031void perf_event_free_task(struct task_struct *task)
9032{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009033 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009034 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009035 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009036
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009037 for_each_task_context_nr(ctxn) {
9038 ctx = task->perf_event_ctxp[ctxn];
9039 if (!ctx)
9040 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009041
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009042 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009043again:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009044 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
9045 group_entry)
9046 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009047
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009048 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
9049 group_entry)
9050 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009051
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009052 if (!list_empty(&ctx->pinned_groups) ||
9053 !list_empty(&ctx->flexible_groups))
9054 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009055
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009056 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009057
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009058 put_ctx(ctx);
9059 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009060}
9061
Peter Zijlstra4e231c72010-09-09 21:01:59 +02009062void perf_event_delayed_put(struct task_struct *task)
9063{
9064 int ctxn;
9065
9066 for_each_task_context_nr(ctxn)
9067 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
9068}
9069
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009070struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +00009071{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009072 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +00009073
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009074 file = fget_raw(fd);
9075 if (!file)
9076 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +00009077
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009078 if (file->f_op != &perf_fops) {
9079 fput(file);
9080 return ERR_PTR(-EBADF);
9081 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00009082
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009083 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +00009084}
9085
9086const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
9087{
9088 if (!event)
9089 return ERR_PTR(-EINVAL);
9090
9091 return &event->attr;
9092}
9093
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009094/*
9095 * inherit a event from parent task to child task:
9096 */
9097static struct perf_event *
9098inherit_event(struct perf_event *parent_event,
9099 struct task_struct *parent,
9100 struct perf_event_context *parent_ctx,
9101 struct task_struct *child,
9102 struct perf_event *group_leader,
9103 struct perf_event_context *child_ctx)
9104{
Jiri Olsa1929def2014-09-12 13:18:27 +02009105 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009106 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02009107 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009108
9109 /*
9110 * Instead of creating recursive hierarchies of events,
9111 * we link inherited events back to the original parent,
9112 * which has a filp for sure, which we use as the reference
9113 * count:
9114 */
9115 if (parent_event->parent)
9116 parent_event = parent_event->parent;
9117
9118 child_event = perf_event_alloc(&parent_event->attr,
9119 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009120 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009121 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +00009122 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009123 if (IS_ERR(child_event))
9124 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01009125
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02009126 /*
9127 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
9128 * must be under the same lock in order to serialize against
9129 * perf_event_release_kernel(), such that either we must observe
9130 * is_orphaned_event() or they will observe us on the child_list.
9131 */
9132 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02009133 if (is_orphaned_event(parent_event) ||
9134 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02009135 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +01009136 free_event(child_event);
9137 return NULL;
9138 }
9139
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009140 get_ctx(child_ctx);
9141
9142 /*
9143 * Make the child state follow the state of the parent event,
9144 * not its attr.disabled bit. We hold the parent's mutex,
9145 * so we won't race with perf_event_{en, dis}able_family.
9146 */
Jiri Olsa1929def2014-09-12 13:18:27 +02009147 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009148 child_event->state = PERF_EVENT_STATE_INACTIVE;
9149 else
9150 child_event->state = PERF_EVENT_STATE_OFF;
9151
9152 if (parent_event->attr.freq) {
9153 u64 sample_period = parent_event->hw.sample_period;
9154 struct hw_perf_event *hwc = &child_event->hw;
9155
9156 hwc->sample_period = sample_period;
9157 hwc->last_period = sample_period;
9158
9159 local64_set(&hwc->period_left, sample_period);
9160 }
9161
9162 child_event->ctx = child_ctx;
9163 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03009164 child_event->overflow_handler_context
9165 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009166
9167 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02009168 * Precalculate sample_data sizes
9169 */
9170 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02009171 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02009172
9173 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009174 * Link it up in the child's context:
9175 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02009176 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009177 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02009178 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009179
9180 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009181 * Link this into the parent event's child list
9182 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009183 list_add_tail(&child_event->child_list, &parent_event->child_list);
9184 mutex_unlock(&parent_event->child_mutex);
9185
9186 return child_event;
9187}
9188
9189static int inherit_group(struct perf_event *parent_event,
9190 struct task_struct *parent,
9191 struct perf_event_context *parent_ctx,
9192 struct task_struct *child,
9193 struct perf_event_context *child_ctx)
9194{
9195 struct perf_event *leader;
9196 struct perf_event *sub;
9197 struct perf_event *child_ctr;
9198
9199 leader = inherit_event(parent_event, parent, parent_ctx,
9200 child, NULL, child_ctx);
9201 if (IS_ERR(leader))
9202 return PTR_ERR(leader);
9203 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
9204 child_ctr = inherit_event(sub, parent, parent_ctx,
9205 child, leader, child_ctx);
9206 if (IS_ERR(child_ctr))
9207 return PTR_ERR(child_ctr);
9208 }
9209 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009210}
9211
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009212static int
9213inherit_task_group(struct perf_event *event, struct task_struct *parent,
9214 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009215 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009216 int *inherited_all)
9217{
9218 int ret;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009219 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009220
9221 if (!event->attr.inherit) {
9222 *inherited_all = 0;
9223 return 0;
9224 }
9225
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009226 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009227 if (!child_ctx) {
9228 /*
9229 * This is executed from the parent task context, so
9230 * inherit events that have been marked for cloning.
9231 * First allocate and initialize a context for the
9232 * child.
9233 */
9234
Jiri Olsa734df5a2013-07-09 17:44:10 +02009235 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009236 if (!child_ctx)
9237 return -ENOMEM;
9238
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009239 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009240 }
9241
9242 ret = inherit_group(event, parent, parent_ctx,
9243 child, child_ctx);
9244
9245 if (ret)
9246 *inherited_all = 0;
9247
9248 return ret;
9249}
9250
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009251/*
9252 * Initialize the perf_event context in task_struct
9253 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02009254static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009255{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009256 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009257 struct perf_event_context *cloned_ctx;
9258 struct perf_event *event;
9259 struct task_struct *parent = current;
9260 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009261 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009262 int ret = 0;
9263
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009264 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009265 return 0;
9266
9267 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009268 * If the parent's context is a clone, pin it so it won't get
9269 * swapped under us.
9270 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009271 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02009272 if (!parent_ctx)
9273 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009274
9275 /*
9276 * No need to check if parent_ctx != NULL here; since we saw
9277 * it non-NULL earlier, the only reason for it to become NULL
9278 * is if we exit, and since we're currently in the middle of
9279 * a fork we can't be exiting at the same time.
9280 */
9281
9282 /*
9283 * Lock the parent list. No need to lock the child - not PID
9284 * hashed yet and not running, so nobody can access it.
9285 */
9286 mutex_lock(&parent_ctx->mutex);
9287
9288 /*
9289 * We dont have to disable NMIs - we are only looking at
9290 * the list, not manipulating it:
9291 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009292 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009293 ret = inherit_task_group(event, parent, parent_ctx,
9294 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009295 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009296 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009297 }
9298
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009299 /*
9300 * We can't hold ctx->lock when iterating the ->flexible_group list due
9301 * to allocations, but we need to prevent rotation because
9302 * rotate_ctx() will change the list from interrupt context.
9303 */
9304 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
9305 parent_ctx->rotate_disable = 1;
9306 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
9307
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009308 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009309 ret = inherit_task_group(event, parent, parent_ctx,
9310 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009311 if (ret)
9312 break;
9313 }
9314
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009315 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
9316 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009317
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009318 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009319
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01009320 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009321 /*
9322 * Mark the child context as a clone of the parent
9323 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009324 *
9325 * Note that if the parent is a clone, the holding of
9326 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009327 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009328 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009329 if (cloned_ctx) {
9330 child_ctx->parent_ctx = cloned_ctx;
9331 child_ctx->parent_gen = parent_ctx->parent_gen;
9332 } else {
9333 child_ctx->parent_ctx = parent_ctx;
9334 child_ctx->parent_gen = parent_ctx->generation;
9335 }
9336 get_ctx(child_ctx->parent_ctx);
9337 }
9338
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009339 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009340 mutex_unlock(&parent_ctx->mutex);
9341
9342 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009343 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009344
9345 return ret;
9346}
9347
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009348/*
9349 * Initialize the perf_event context in task_struct
9350 */
9351int perf_event_init_task(struct task_struct *child)
9352{
9353 int ctxn, ret;
9354
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01009355 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
9356 mutex_init(&child->perf_event_mutex);
9357 INIT_LIST_HEAD(&child->perf_event_list);
9358
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009359 for_each_task_context_nr(ctxn) {
9360 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07009361 if (ret) {
9362 perf_event_free_task(child);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009363 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07009364 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02009365 }
9366
9367 return 0;
9368}
9369
Paul Mackerras220b1402010-03-10 20:45:52 +11009370static void __init perf_event_init_all_cpus(void)
9371{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009372 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11009373 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11009374
9375 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009376 swhash = &per_cpu(swevent_htable, cpu);
9377 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00009378 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11009379 }
9380}
9381
Paul Gortmaker0db06282013-06-19 14:53:51 -04009382static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009383{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009384 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009385
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009386 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +00009387 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009388 struct swevent_hlist *hlist;
9389
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009390 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
9391 WARN_ON(!hlist);
9392 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009393 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009394 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009395}
9396
Dave Young2965faa2015-09-09 15:38:55 -07009397#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009398static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009399{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009400 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009401 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
9402 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009403
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009404 raw_spin_lock(&ctx->lock);
9405 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009406 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009407 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009408}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009409
9410static void perf_event_exit_cpu_context(int cpu)
9411{
9412 struct perf_event_context *ctx;
9413 struct pmu *pmu;
9414 int idx;
9415
9416 idx = srcu_read_lock(&pmus_srcu);
9417 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02009418 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009419
9420 mutex_lock(&ctx->mutex);
9421 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
9422 mutex_unlock(&ctx->mutex);
9423 }
9424 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009425}
9426
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009427static void perf_event_exit_cpu(int cpu)
9428{
Peter Zijlstrae3703f82014-02-24 12:06:12 +01009429 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009430}
9431#else
9432static inline void perf_event_exit_cpu(int cpu) { }
9433#endif
9434
Peter Zijlstrac2774432010-12-08 15:29:02 +01009435static int
9436perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
9437{
9438 int cpu;
9439
9440 for_each_online_cpu(cpu)
9441 perf_event_exit_cpu(cpu);
9442
9443 return NOTIFY_OK;
9444}
9445
9446/*
9447 * Run the perf reboot notifier at the very last possible moment so that
9448 * the generic watchdog code runs as long as possible.
9449 */
9450static struct notifier_block perf_reboot_notifier = {
9451 .notifier_call = perf_reboot,
9452 .priority = INT_MIN,
9453};
9454
Paul Gortmaker0db06282013-06-19 14:53:51 -04009455static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009456perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
9457{
9458 unsigned int cpu = (long)hcpu;
9459
Linus Torvalds4536e4d2011-11-03 07:44:04 -07009460 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009461
9462 case CPU_UP_PREPARE:
Peter Zijlstra1dcaac12016-03-08 17:56:05 +01009463 /*
9464 * This must be done before the CPU comes alive, because the
9465 * moment we can run tasks we can encounter (software) events.
9466 *
9467 * Specifically, someone can have inherited events on kthreadd
9468 * or a pre-existing worker thread that gets re-bound.
9469 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009470 perf_event_init_cpu(cpu);
9471 break;
9472
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009473 case CPU_DOWN_PREPARE:
Peter Zijlstra1dcaac12016-03-08 17:56:05 +01009474 /*
9475 * This must be done before the CPU dies because after that an
9476 * active event might want to IPI the CPU and that'll not work
9477 * so great for dead CPUs.
9478 *
9479 * XXX smp_call_function_single() return -ENXIO without a warn
9480 * so we could possibly deal with this.
9481 *
9482 * This is safe against new events arriving because
9483 * sys_perf_event_open() serializes against hotplug using
9484 * get_online_cpus().
9485 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009486 perf_event_exit_cpu(cpu);
9487 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009488 default:
9489 break;
9490 }
9491
9492 return NOTIFY_OK;
9493}
9494
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009495void __init perf_event_init(void)
9496{
Jason Wessel3c502e72010-11-04 17:33:01 -05009497 int ret;
9498
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009499 idr_init(&pmu_idr);
9500
Paul Mackerras220b1402010-03-10 20:45:52 +11009501 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009502 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009503 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
9504 perf_pmu_register(&perf_cpu_clock, NULL, -1);
9505 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009506 perf_tp_register();
9507 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01009508 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05009509
9510 ret = init_hw_breakpoint();
9511 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02009512
Jiri Olsab01c3a02012-03-23 15:41:20 +01009513 /*
9514 * Build time assertion that we keep the data_head at the intended
9515 * location. IOW, validation we got the __reserved[] size right.
9516 */
9517 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
9518 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009519}
Peter Zijlstraabe43402010-11-17 23:17:37 +01009520
Cody P Schaferfd979c02015-01-30 13:45:57 -08009521ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
9522 char *page)
9523{
9524 struct perf_pmu_events_attr *pmu_attr =
9525 container_of(attr, struct perf_pmu_events_attr, attr);
9526
9527 if (pmu_attr->event_str)
9528 return sprintf(page, "%s\n", pmu_attr->event_str);
9529
9530 return 0;
9531}
Thomas Gleixner675965b2016-02-22 22:19:27 +00009532EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -08009533
Peter Zijlstraabe43402010-11-17 23:17:37 +01009534static int __init perf_event_sysfs_init(void)
9535{
9536 struct pmu *pmu;
9537 int ret;
9538
9539 mutex_lock(&pmus_lock);
9540
9541 ret = bus_register(&pmu_bus);
9542 if (ret)
9543 goto unlock;
9544
9545 list_for_each_entry(pmu, &pmus, entry) {
9546 if (!pmu->name || pmu->type < 0)
9547 continue;
9548
9549 ret = pmu_dev_alloc(pmu);
9550 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
9551 }
9552 pmu_bus_running = 1;
9553 ret = 0;
9554
9555unlock:
9556 mutex_unlock(&pmus_lock);
9557
9558 return ret;
9559}
9560device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009561
9562#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04009563static struct cgroup_subsys_state *
9564perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009565{
9566 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02009567
Li Zefan1b15d052011-03-03 14:26:06 +08009568 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009569 if (!jc)
9570 return ERR_PTR(-ENOMEM);
9571
Stephane Eraniane5d13672011-02-14 11:20:01 +02009572 jc->info = alloc_percpu(struct perf_cgroup_info);
9573 if (!jc->info) {
9574 kfree(jc);
9575 return ERR_PTR(-ENOMEM);
9576 }
9577
Stephane Eraniane5d13672011-02-14 11:20:01 +02009578 return &jc->css;
9579}
9580
Tejun Heoeb954192013-08-08 20:11:23 -04009581static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009582{
Tejun Heoeb954192013-08-08 20:11:23 -04009583 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
9584
Stephane Eraniane5d13672011-02-14 11:20:01 +02009585 free_percpu(jc->info);
9586 kfree(jc);
9587}
9588
9589static int __perf_cgroup_move(void *info)
9590{
9591 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +01009592 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +02009593 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +01009594 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +02009595 return 0;
9596}
9597
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009598static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009599{
Tejun Heobb9d97b2011-12-12 18:12:21 -08009600 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009601 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -08009602
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009603 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08009604 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009605}
9606
Tejun Heo073219e2014-02-08 10:36:58 -05009607struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08009608 .css_alloc = perf_cgroup_css_alloc,
9609 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -08009610 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02009611};
9612#endif /* CONFIG_CGROUP_PERF */