blob: 2c78b6f473395e952706726d9a622d8eff1bc766 [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 Zijlstrab303e7c2016-04-04 09:57:40 +0200415 if (sysctl_perf_cpu_time_max_percent == 100 ||
416 sysctl_perf_cpu_time_max_percent == 0) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100417 printk(KERN_WARNING
418 "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
419 WRITE_ONCE(perf_sample_allowed_ns, 0);
420 } else {
421 update_perf_cpu_limits();
422 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700423
424 return 0;
425}
426
427/*
428 * perf samples are done in some very critical code paths (NMIs).
429 * If they take too much CPU time, the system can lock up and not
430 * get any real work done. This will drop the sample rate when
431 * we detect that events are taking too long.
432 */
433#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200434static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700435
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100436static u64 __report_avg;
437static u64 __report_allowed;
438
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100439static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700440{
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100441 printk_ratelimited(KERN_WARNING
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100442 "perf: interrupt took too long (%lld > %lld), lowering "
443 "kernel.perf_event_max_sample_rate to %d\n",
444 __report_avg, __report_allowed,
445 sysctl_perf_event_sample_rate);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100446}
447
448static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
449
450void perf_sample_event_took(u64 sample_len_ns)
451{
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100452 u64 max_len = READ_ONCE(perf_sample_allowed_ns);
453 u64 running_len;
454 u64 avg_len;
455 u32 max;
Dave Hansen14c63f12013-06-21 08:51:36 -0700456
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100457 if (max_len == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700458 return;
459
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100460 /* Decay the counter by 1 average sample. */
461 running_len = __this_cpu_read(running_sample_length);
462 running_len -= running_len/NR_ACCUMULATED_SAMPLES;
463 running_len += sample_len_ns;
464 __this_cpu_write(running_sample_length, running_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700465
466 /*
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100467 * Note: this will be biased artifically low until we have
468 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
Dave Hansen14c63f12013-06-21 08:51:36 -0700469 * from having to maintain a count.
470 */
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100471 avg_len = running_len/NR_ACCUMULATED_SAMPLES;
472 if (avg_len <= max_len)
Dave Hansen14c63f12013-06-21 08:51:36 -0700473 return;
474
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100475 __report_avg = avg_len;
476 __report_allowed = max_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700477
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100478 /*
479 * Compute a throttle threshold 25% below the current duration.
480 */
481 avg_len += avg_len / 4;
482 max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
483 if (avg_len < max)
484 max /= (u32)avg_len;
485 else
486 max = 1;
487
488 WRITE_ONCE(perf_sample_allowed_ns, avg_len);
489 WRITE_ONCE(max_samples_per_tick, max);
490
491 sysctl_perf_event_sample_rate = max * HZ;
Dave Hansen14c63f12013-06-21 08:51:36 -0700492 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
493
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100494 if (!irq_work_queue(&perf_duration_work)) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100495 early_printk("perf: interrupt took too long (%lld > %lld), lowering "
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100496 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100497 __report_avg, __report_allowed,
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100498 sysctl_perf_event_sample_rate);
499 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700500}
501
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200502static atomic64_t perf_event_id;
503
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200504static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
505 enum event_type_t event_type);
506
507static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200508 enum event_type_t event_type,
509 struct task_struct *task);
510
511static void update_context_time(struct perf_event_context *ctx);
512static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200513
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200514void __weak perf_event_print_debug(void) { }
515
Matt Fleming84c79912010-10-03 21:41:13 +0100516extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200517{
Matt Fleming84c79912010-10-03 21:41:13 +0100518 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200519}
520
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200521static inline u64 perf_clock(void)
522{
523 return local_clock();
524}
525
Peter Zijlstra34f43922015-02-20 14:05:38 +0100526static inline u64 perf_event_clock(struct perf_event *event)
527{
528 return event->clock();
529}
530
Stephane Eraniane5d13672011-02-14 11:20:01 +0200531#ifdef CONFIG_CGROUP_PERF
532
Stephane Eraniane5d13672011-02-14 11:20:01 +0200533static inline bool
534perf_cgroup_match(struct perf_event *event)
535{
536 struct perf_event_context *ctx = event->ctx;
537 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
538
Tejun Heoef824fa2013-04-08 19:00:38 -0700539 /* @event doesn't care about cgroup */
540 if (!event->cgrp)
541 return true;
542
543 /* wants specific cgroup scope but @cpuctx isn't associated with any */
544 if (!cpuctx->cgrp)
545 return false;
546
547 /*
548 * Cgroup scoping is recursive. An event enabled for a cgroup is
549 * also enabled for all its descendant cgroups. If @cpuctx's
550 * cgroup is a descendant of @event's (the test covers identity
551 * case), it's a match.
552 */
553 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
554 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200555}
556
Stephane Eraniane5d13672011-02-14 11:20:01 +0200557static inline void perf_detach_cgroup(struct perf_event *event)
558{
Zefan Li4e2ba652014-09-19 16:53:14 +0800559 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200560 event->cgrp = NULL;
561}
562
563static inline int is_cgroup_event(struct perf_event *event)
564{
565 return event->cgrp != NULL;
566}
567
568static inline u64 perf_cgroup_event_time(struct perf_event *event)
569{
570 struct perf_cgroup_info *t;
571
572 t = per_cpu_ptr(event->cgrp->info, event->cpu);
573 return t->time;
574}
575
576static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
577{
578 struct perf_cgroup_info *info;
579 u64 now;
580
581 now = perf_clock();
582
583 info = this_cpu_ptr(cgrp->info);
584
585 info->time += now - info->timestamp;
586 info->timestamp = now;
587}
588
589static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
590{
591 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
592 if (cgrp_out)
593 __update_cgrp_time(cgrp_out);
594}
595
596static inline void update_cgrp_time_from_event(struct perf_event *event)
597{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200598 struct perf_cgroup *cgrp;
599
Stephane Eraniane5d13672011-02-14 11:20:01 +0200600 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200601 * ensure we access cgroup data only when needed and
602 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200603 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200604 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200605 return;
606
Stephane Eranian614e4c42015-11-12 11:00:04 +0100607 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200608 /*
609 * Do not update time when cgroup is not active
610 */
611 if (cgrp == event->cgrp)
612 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200613}
614
615static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200616perf_cgroup_set_timestamp(struct task_struct *task,
617 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200618{
619 struct perf_cgroup *cgrp;
620 struct perf_cgroup_info *info;
621
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200622 /*
623 * ctx->lock held by caller
624 * ensure we do not access cgroup data
625 * unless we have the cgroup pinned (css_get)
626 */
627 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200628 return;
629
Stephane Eranian614e4c42015-11-12 11:00:04 +0100630 cgrp = perf_cgroup_from_task(task, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200631 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200632 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200633}
634
635#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
636#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
637
638/*
639 * reschedule events based on the cgroup constraint of task.
640 *
641 * mode SWOUT : schedule out everything
642 * mode SWIN : schedule in based on cgroup for next
643 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800644static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200645{
646 struct perf_cpu_context *cpuctx;
647 struct pmu *pmu;
648 unsigned long flags;
649
650 /*
651 * disable interrupts to avoid geting nr_cgroup
652 * changes via __perf_event_disable(). Also
653 * avoids preemption.
654 */
655 local_irq_save(flags);
656
657 /*
658 * we reschedule only in the presence of cgroup
659 * constrained events.
660 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200661
662 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200663 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200664 if (cpuctx->unique_pmu != pmu)
665 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200666
Stephane Eraniane5d13672011-02-14 11:20:01 +0200667 /*
668 * perf_cgroup_events says at least one
669 * context on this CPU has cgroup events.
670 *
671 * ctx->nr_cgroups reports the number of cgroup
672 * events for a context.
673 */
674 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200675 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
676 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200677
678 if (mode & PERF_CGROUP_SWOUT) {
679 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
680 /*
681 * must not be done before ctxswout due
682 * to event_filter_match() in event_sched_out()
683 */
684 cpuctx->cgrp = NULL;
685 }
686
687 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200688 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200689 /*
690 * set cgrp before ctxsw in to allow
691 * event_filter_match() to not have to pass
692 * task around
Stephane Eranian614e4c42015-11-12 11:00:04 +0100693 * we pass the cpuctx->ctx to perf_cgroup_from_task()
694 * because cgorup events are only per-cpu
Stephane Eraniane5d13672011-02-14 11:20:01 +0200695 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100696 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200697 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
698 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200699 perf_pmu_enable(cpuctx->ctx.pmu);
700 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200701 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200702 }
703
Stephane Eraniane5d13672011-02-14 11:20:01 +0200704 local_irq_restore(flags);
705}
706
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200707static inline void perf_cgroup_sched_out(struct task_struct *task,
708 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200709{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200710 struct perf_cgroup *cgrp1;
711 struct perf_cgroup *cgrp2 = NULL;
712
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100713 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200714 /*
715 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100716 * we do not need to pass the ctx here because we know
717 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200718 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100719 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100720 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200721
722 /*
723 * only schedule out current cgroup events if we know
724 * that we are switching to a different cgroup. Otherwise,
725 * do no touch the cgroup events.
726 */
727 if (cgrp1 != cgrp2)
728 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100729
730 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200731}
732
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200733static inline void perf_cgroup_sched_in(struct task_struct *prev,
734 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200735{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200736 struct perf_cgroup *cgrp1;
737 struct perf_cgroup *cgrp2 = NULL;
738
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100739 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200740 /*
741 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100742 * we do not need to pass the ctx here because we know
743 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200744 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100745 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100746 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200747
748 /*
749 * only need to schedule in cgroup events if we are changing
750 * cgroup during ctxsw. Cgroup events were not scheduled
751 * out of ctxsw out if that was not the case.
752 */
753 if (cgrp1 != cgrp2)
754 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100755
756 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200757}
758
759static inline int perf_cgroup_connect(int fd, struct perf_event *event,
760 struct perf_event_attr *attr,
761 struct perf_event *group_leader)
762{
763 struct perf_cgroup *cgrp;
764 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400765 struct fd f = fdget(fd);
766 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200767
Al Viro2903ff02012-08-28 12:52:22 -0400768 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200769 return -EBADF;
770
Al Virob5830432014-10-31 01:22:04 -0400771 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400772 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800773 if (IS_ERR(css)) {
774 ret = PTR_ERR(css);
775 goto out;
776 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200777
778 cgrp = container_of(css, struct perf_cgroup, css);
779 event->cgrp = cgrp;
780
781 /*
782 * all events in a group must monitor
783 * the same cgroup because a task belongs
784 * to only one perf cgroup at a time
785 */
786 if (group_leader && group_leader->cgrp != cgrp) {
787 perf_detach_cgroup(event);
788 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200789 }
Li Zefan3db272c2011-03-03 14:25:37 +0800790out:
Al Viro2903ff02012-08-28 12:52:22 -0400791 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200792 return ret;
793}
794
795static inline void
796perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
797{
798 struct perf_cgroup_info *t;
799 t = per_cpu_ptr(event->cgrp->info, event->cpu);
800 event->shadow_ctx_time = now - t->timestamp;
801}
802
803static inline void
804perf_cgroup_defer_enabled(struct perf_event *event)
805{
806 /*
807 * when the current task's perf cgroup does not match
808 * the event's, we need to remember to call the
809 * perf_mark_enable() function the first time a task with
810 * a matching perf cgroup is scheduled in.
811 */
812 if (is_cgroup_event(event) && !perf_cgroup_match(event))
813 event->cgrp_defer_enabled = 1;
814}
815
816static inline void
817perf_cgroup_mark_enabled(struct perf_event *event,
818 struct perf_event_context *ctx)
819{
820 struct perf_event *sub;
821 u64 tstamp = perf_event_time(event);
822
823 if (!event->cgrp_defer_enabled)
824 return;
825
826 event->cgrp_defer_enabled = 0;
827
828 event->tstamp_enabled = tstamp - event->total_time_enabled;
829 list_for_each_entry(sub, &event->sibling_list, group_entry) {
830 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
831 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
832 sub->cgrp_defer_enabled = 0;
833 }
834 }
835}
836#else /* !CONFIG_CGROUP_PERF */
837
838static inline bool
839perf_cgroup_match(struct perf_event *event)
840{
841 return true;
842}
843
844static inline void perf_detach_cgroup(struct perf_event *event)
845{}
846
847static inline int is_cgroup_event(struct perf_event *event)
848{
849 return 0;
850}
851
852static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
853{
854 return 0;
855}
856
857static inline void update_cgrp_time_from_event(struct perf_event *event)
858{
859}
860
861static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
862{
863}
864
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200865static inline void perf_cgroup_sched_out(struct task_struct *task,
866 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200867{
868}
869
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200870static inline void perf_cgroup_sched_in(struct task_struct *prev,
871 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200872{
873}
874
875static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
876 struct perf_event_attr *attr,
877 struct perf_event *group_leader)
878{
879 return -EINVAL;
880}
881
882static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200883perf_cgroup_set_timestamp(struct task_struct *task,
884 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200885{
886}
887
888void
889perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
890{
891}
892
893static inline void
894perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
895{
896}
897
898static inline u64 perf_cgroup_event_time(struct perf_event *event)
899{
900 return 0;
901}
902
903static inline void
904perf_cgroup_defer_enabled(struct perf_event *event)
905{
906}
907
908static inline void
909perf_cgroup_mark_enabled(struct perf_event *event,
910 struct perf_event_context *ctx)
911{
912}
913#endif
914
Stephane Eranian9e630202013-04-03 14:21:33 +0200915/*
916 * set default to be dependent on timer tick just
917 * like original code
918 */
919#define PERF_CPU_HRTIMER (1000 / HZ)
920/*
921 * function must be called with interrupts disbled
922 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200923static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +0200924{
925 struct perf_cpu_context *cpuctx;
Stephane Eranian9e630202013-04-03 14:21:33 +0200926 int rotations = 0;
927
928 WARN_ON(!irqs_disabled());
929
930 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200931 rotations = perf_rotate_context(cpuctx);
932
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200933 raw_spin_lock(&cpuctx->hrtimer_lock);
934 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +0200935 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200936 else
937 cpuctx->hrtimer_active = 0;
938 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +0200939
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200940 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +0200941}
942
Peter Zijlstra272325c2015-04-15 11:41:58 +0200943static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +0200944{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200945 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200946 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +0200947 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +0200948
949 /* no multiplexing needed for SW PMU */
950 if (pmu->task_ctx_nr == perf_sw_context)
951 return;
952
Stephane Eranian62b85632013-04-03 14:21:34 +0200953 /*
954 * check default is sane, if not set then force to
955 * default interval (1/tick)
956 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200957 interval = pmu->hrtimer_interval_ms;
958 if (interval < 1)
959 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +0200960
Peter Zijlstra272325c2015-04-15 11:41:58 +0200961 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +0200962
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200963 raw_spin_lock_init(&cpuctx->hrtimer_lock);
964 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra272325c2015-04-15 11:41:58 +0200965 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +0200966}
967
Peter Zijlstra272325c2015-04-15 11:41:58 +0200968static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +0200969{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200970 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200971 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200972 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +0200973
974 /* not for SW PMU */
975 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +0200976 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +0200977
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200978 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
979 if (!cpuctx->hrtimer_active) {
980 cpuctx->hrtimer_active = 1;
981 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
982 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
983 }
984 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +0200985
Peter Zijlstra272325c2015-04-15 11:41:58 +0200986 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +0200987}
988
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200989void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200990{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200991 int *count = this_cpu_ptr(pmu->pmu_disable_count);
992 if (!(*count)++)
993 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200994}
995
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200996void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200997{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200998 int *count = this_cpu_ptr(pmu->pmu_disable_count);
999 if (!--(*count))
1000 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001001}
1002
Mark Rutland2fde4f92015-01-07 15:01:54 +00001003static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001004
1005/*
Mark Rutland2fde4f92015-01-07 15:01:54 +00001006 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1007 * perf_event_task_tick() are fully serialized because they're strictly cpu
1008 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1009 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001010 */
Mark Rutland2fde4f92015-01-07 15:01:54 +00001011static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001012{
Mark Rutland2fde4f92015-01-07 15:01:54 +00001013 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001014
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001015 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001016
Mark Rutland2fde4f92015-01-07 15:01:54 +00001017 WARN_ON(!list_empty(&ctx->active_ctx_list));
1018
1019 list_add(&ctx->active_ctx_list, head);
1020}
1021
1022static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1023{
1024 WARN_ON(!irqs_disabled());
1025
1026 WARN_ON(list_empty(&ctx->active_ctx_list));
1027
1028 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001029}
1030
1031static void get_ctx(struct perf_event_context *ctx)
1032{
1033 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
1034}
1035
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001036static void free_ctx(struct rcu_head *head)
1037{
1038 struct perf_event_context *ctx;
1039
1040 ctx = container_of(head, struct perf_event_context, rcu_head);
1041 kfree(ctx->task_ctx_data);
1042 kfree(ctx);
1043}
1044
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001045static void put_ctx(struct perf_event_context *ctx)
1046{
1047 if (atomic_dec_and_test(&ctx->refcount)) {
1048 if (ctx->parent_ctx)
1049 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001050 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001051 put_task_struct(ctx->task);
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001052 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001053 }
1054}
1055
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001056/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001057 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1058 * perf_pmu_migrate_context() we need some magic.
1059 *
1060 * Those places that change perf_event::ctx will hold both
1061 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1062 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001063 * Lock ordering is by mutex address. There are two other sites where
1064 * perf_event_context::mutex nests and those are:
1065 *
1066 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001067 * perf_event_exit_event()
1068 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001069 *
1070 * - perf_event_init_context() [ parent, 0 ]
1071 * inherit_task_group()
1072 * inherit_group()
1073 * inherit_event()
1074 * perf_event_alloc()
1075 * perf_init_event()
1076 * perf_try_init_event() [ child , 1 ]
1077 *
1078 * While it appears there is an obvious deadlock here -- the parent and child
1079 * nesting levels are inverted between the two. This is in fact safe because
1080 * life-time rules separate them. That is an exiting task cannot fork, and a
1081 * spawning task cannot (yet) exit.
1082 *
1083 * But remember that that these are parent<->child context relations, and
1084 * migration does not affect children, therefore these two orderings should not
1085 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001086 *
1087 * The change in perf_event::ctx does not affect children (as claimed above)
1088 * because the sys_perf_event_open() case will install a new event and break
1089 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1090 * concerned with cpuctx and that doesn't have children.
1091 *
1092 * The places that change perf_event::ctx will issue:
1093 *
1094 * perf_remove_from_context();
1095 * synchronize_rcu();
1096 * perf_install_in_context();
1097 *
1098 * to affect the change. The remove_from_context() + synchronize_rcu() should
1099 * quiesce the event, after which we can install it in the new location. This
1100 * means that only external vectors (perf_fops, prctl) can perturb the event
1101 * while in transit. Therefore all such accessors should also acquire
1102 * perf_event_context::mutex to serialize against this.
1103 *
1104 * However; because event->ctx can change while we're waiting to acquire
1105 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1106 * function.
1107 *
1108 * Lock order:
1109 * task_struct::perf_event_mutex
1110 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001111 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001112 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001113 * perf_event::mmap_mutex
1114 * mmap_sem
1115 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001116static struct perf_event_context *
1117perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001118{
1119 struct perf_event_context *ctx;
1120
1121again:
1122 rcu_read_lock();
1123 ctx = ACCESS_ONCE(event->ctx);
1124 if (!atomic_inc_not_zero(&ctx->refcount)) {
1125 rcu_read_unlock();
1126 goto again;
1127 }
1128 rcu_read_unlock();
1129
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001130 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001131 if (event->ctx != ctx) {
1132 mutex_unlock(&ctx->mutex);
1133 put_ctx(ctx);
1134 goto again;
1135 }
1136
1137 return ctx;
1138}
1139
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001140static inline struct perf_event_context *
1141perf_event_ctx_lock(struct perf_event *event)
1142{
1143 return perf_event_ctx_lock_nested(event, 0);
1144}
1145
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001146static void perf_event_ctx_unlock(struct perf_event *event,
1147 struct perf_event_context *ctx)
1148{
1149 mutex_unlock(&ctx->mutex);
1150 put_ctx(ctx);
1151}
1152
1153/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001154 * This must be done under the ctx->lock, such as to serialize against
1155 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1156 * calling scheduler related locks and ctx->lock nests inside those.
1157 */
1158static __must_check struct perf_event_context *
1159unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001160{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001161 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1162
1163 lockdep_assert_held(&ctx->lock);
1164
1165 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001166 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001167 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001168
1169 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001170}
1171
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001172static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1173{
1174 /*
1175 * only top level events have the pid namespace they were created in
1176 */
1177 if (event->parent)
1178 event = event->parent;
1179
1180 return task_tgid_nr_ns(p, event->ns);
1181}
1182
1183static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1184{
1185 /*
1186 * only top level events have the pid namespace they were created in
1187 */
1188 if (event->parent)
1189 event = event->parent;
1190
1191 return task_pid_nr_ns(p, event->ns);
1192}
1193
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001194/*
1195 * If we inherit events we want to return the parent event id
1196 * to userspace.
1197 */
1198static u64 primary_event_id(struct perf_event *event)
1199{
1200 u64 id = event->id;
1201
1202 if (event->parent)
1203 id = event->parent->id;
1204
1205 return id;
1206}
1207
1208/*
1209 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001210 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001211 * This has to cope with with the fact that until it is locked,
1212 * the context could get moved to another task.
1213 */
1214static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001215perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001216{
1217 struct perf_event_context *ctx;
1218
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001219retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001220 /*
1221 * One of the few rules of preemptible RCU is that one cannot do
1222 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001223 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001224 * rcu_read_unlock_special().
1225 *
1226 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001227 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001228 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001229 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001230 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001231 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001232 if (ctx) {
1233 /*
1234 * If this context is a clone of another, it might
1235 * get swapped for another underneath us by
1236 * perf_event_task_sched_out, though the
1237 * rcu_read_lock() protects us from any context
1238 * getting freed. Lock the context and check if it
1239 * got swapped before we could get the lock, and retry
1240 * if so. If we locked the right context, then it
1241 * can't get swapped on us any more.
1242 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001243 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001244 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001245 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001246 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001247 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001248 goto retry;
1249 }
1250
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001251 if (ctx->task == TASK_TOMBSTONE ||
1252 !atomic_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001253 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001254 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001255 } else {
1256 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001257 }
1258 }
1259 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001260 if (!ctx)
1261 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001262 return ctx;
1263}
1264
1265/*
1266 * Get the context for a task and increment its pin_count so it
1267 * can't get swapped to another task. This also increments its
1268 * reference count so that the context can't get freed.
1269 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001270static struct perf_event_context *
1271perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001272{
1273 struct perf_event_context *ctx;
1274 unsigned long flags;
1275
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001276 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001277 if (ctx) {
1278 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001279 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001280 }
1281 return ctx;
1282}
1283
1284static void perf_unpin_context(struct perf_event_context *ctx)
1285{
1286 unsigned long flags;
1287
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001288 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001289 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001290 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001291}
1292
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001293/*
1294 * Update the record of the current time in a context.
1295 */
1296static void update_context_time(struct perf_event_context *ctx)
1297{
1298 u64 now = perf_clock();
1299
1300 ctx->time += now - ctx->timestamp;
1301 ctx->timestamp = now;
1302}
1303
Stephane Eranian41587552011-01-03 18:20:01 +02001304static u64 perf_event_time(struct perf_event *event)
1305{
1306 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001307
1308 if (is_cgroup_event(event))
1309 return perf_cgroup_event_time(event);
1310
Stephane Eranian41587552011-01-03 18:20:01 +02001311 return ctx ? ctx->time : 0;
1312}
1313
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001314/*
1315 * Update the total_time_enabled and total_time_running fields for a event.
1316 */
1317static void update_event_times(struct perf_event *event)
1318{
1319 struct perf_event_context *ctx = event->ctx;
1320 u64 run_end;
1321
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001322 lockdep_assert_held(&ctx->lock);
1323
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001324 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1325 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1326 return;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001327
Stephane Eraniane5d13672011-02-14 11:20:01 +02001328 /*
1329 * in cgroup mode, time_enabled represents
1330 * the time the event was enabled AND active
1331 * tasks were in the monitored cgroup. This is
1332 * independent of the activity of the context as
1333 * there may be a mix of cgroup and non-cgroup events.
1334 *
1335 * That is why we treat cgroup events differently
1336 * here.
1337 */
1338 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001339 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001340 else if (ctx->is_active)
1341 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001342 else
1343 run_end = event->tstamp_stopped;
1344
1345 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001346
1347 if (event->state == PERF_EVENT_STATE_INACTIVE)
1348 run_end = event->tstamp_stopped;
1349 else
Stephane Eranian41587552011-01-03 18:20:01 +02001350 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001351
1352 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001353
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001354}
1355
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001356/*
1357 * Update total_time_enabled and total_time_running for all events in a group.
1358 */
1359static void update_group_times(struct perf_event *leader)
1360{
1361 struct perf_event *event;
1362
1363 update_event_times(leader);
1364 list_for_each_entry(event, &leader->sibling_list, group_entry)
1365 update_event_times(event);
1366}
1367
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001368static struct list_head *
1369ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1370{
1371 if (event->attr.pinned)
1372 return &ctx->pinned_groups;
1373 else
1374 return &ctx->flexible_groups;
1375}
1376
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001377/*
1378 * Add a event from the lists for its context.
1379 * Must be called with ctx->mutex and ctx->lock held.
1380 */
1381static void
1382list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1383{
Peter Zijlstrac994d612016-01-08 09:20:23 +01001384 lockdep_assert_held(&ctx->lock);
1385
Peter Zijlstra8a495422010-05-27 15:47:49 +02001386 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1387 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001388
1389 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001390 * If we're a stand alone event or group leader, we go to the context
1391 * list, group events are kept attached to the group so that
1392 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001393 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001394 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001395 struct list_head *list;
1396
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001397 if (is_software_event(event))
1398 event->group_flags |= PERF_GROUP_SOFTWARE;
1399
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001400 list = ctx_group_list(event, ctx);
1401 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001402 }
1403
Peter Zijlstra08309372011-03-03 11:31:20 +01001404 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001405 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001406
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001407 list_add_rcu(&event->event_entry, &ctx->event_list);
1408 ctx->nr_events++;
1409 if (event->attr.inherit_stat)
1410 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001411
1412 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001413}
1414
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001415/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001416 * Initialize event state based on the perf_event_attr::disabled.
1417 */
1418static inline void perf_event__state_init(struct perf_event *event)
1419{
1420 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1421 PERF_EVENT_STATE_INACTIVE;
1422}
1423
Peter Zijlstraa7239682015-09-09 19:06:33 +02001424static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001425{
1426 int entry = sizeof(u64); /* value */
1427 int size = 0;
1428 int nr = 1;
1429
1430 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1431 size += sizeof(u64);
1432
1433 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1434 size += sizeof(u64);
1435
1436 if (event->attr.read_format & PERF_FORMAT_ID)
1437 entry += sizeof(u64);
1438
1439 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001440 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001441 size += sizeof(u64);
1442 }
1443
1444 size += entry * nr;
1445 event->read_size = size;
1446}
1447
Peter Zijlstraa7239682015-09-09 19:06:33 +02001448static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001449{
1450 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001451 u16 size = 0;
1452
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001453 if (sample_type & PERF_SAMPLE_IP)
1454 size += sizeof(data->ip);
1455
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001456 if (sample_type & PERF_SAMPLE_ADDR)
1457 size += sizeof(data->addr);
1458
1459 if (sample_type & PERF_SAMPLE_PERIOD)
1460 size += sizeof(data->period);
1461
Andi Kleenc3feedf2013-01-24 16:10:28 +01001462 if (sample_type & PERF_SAMPLE_WEIGHT)
1463 size += sizeof(data->weight);
1464
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001465 if (sample_type & PERF_SAMPLE_READ)
1466 size += event->read_size;
1467
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001468 if (sample_type & PERF_SAMPLE_DATA_SRC)
1469 size += sizeof(data->data_src.val);
1470
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001471 if (sample_type & PERF_SAMPLE_TRANSACTION)
1472 size += sizeof(data->txn);
1473
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001474 event->header_size = size;
1475}
1476
Peter Zijlstraa7239682015-09-09 19:06:33 +02001477/*
1478 * Called at perf_event creation and when events are attached/detached from a
1479 * group.
1480 */
1481static void perf_event__header_size(struct perf_event *event)
1482{
1483 __perf_event_read_size(event,
1484 event->group_leader->nr_siblings);
1485 __perf_event_header_size(event, event->attr.sample_type);
1486}
1487
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001488static void perf_event__id_header_size(struct perf_event *event)
1489{
1490 struct perf_sample_data *data;
1491 u64 sample_type = event->attr.sample_type;
1492 u16 size = 0;
1493
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001494 if (sample_type & PERF_SAMPLE_TID)
1495 size += sizeof(data->tid_entry);
1496
1497 if (sample_type & PERF_SAMPLE_TIME)
1498 size += sizeof(data->time);
1499
Adrian Hunterff3d5272013-08-27 11:23:07 +03001500 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1501 size += sizeof(data->id);
1502
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001503 if (sample_type & PERF_SAMPLE_ID)
1504 size += sizeof(data->id);
1505
1506 if (sample_type & PERF_SAMPLE_STREAM_ID)
1507 size += sizeof(data->stream_id);
1508
1509 if (sample_type & PERF_SAMPLE_CPU)
1510 size += sizeof(data->cpu_entry);
1511
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001512 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001513}
1514
Peter Zijlstraa7239682015-09-09 19:06:33 +02001515static bool perf_event_validate_size(struct perf_event *event)
1516{
1517 /*
1518 * The values computed here will be over-written when we actually
1519 * attach the event.
1520 */
1521 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1522 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1523 perf_event__id_header_size(event);
1524
1525 /*
1526 * Sum the lot; should not exceed the 64k limit we have on records.
1527 * Conservative limit to allow for callchains and other variable fields.
1528 */
1529 if (event->read_size + event->header_size +
1530 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1531 return false;
1532
1533 return true;
1534}
1535
Peter Zijlstra8a495422010-05-27 15:47:49 +02001536static void perf_group_attach(struct perf_event *event)
1537{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001538 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001539
Peter Zijlstra74c33372010-10-15 11:40:29 +02001540 /*
1541 * We can have double attach due to group movement in perf_event_open.
1542 */
1543 if (event->attach_state & PERF_ATTACH_GROUP)
1544 return;
1545
Peter Zijlstra8a495422010-05-27 15:47:49 +02001546 event->attach_state |= PERF_ATTACH_GROUP;
1547
1548 if (group_leader == event)
1549 return;
1550
Peter Zijlstra652884f2015-01-23 11:20:10 +01001551 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1552
Peter Zijlstra8a495422010-05-27 15:47:49 +02001553 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1554 !is_software_event(event))
1555 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1556
1557 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1558 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001559
1560 perf_event__header_size(group_leader);
1561
1562 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1563 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001564}
1565
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001566/*
1567 * Remove a event from the lists for its context.
1568 * Must be called with ctx->mutex and ctx->lock held.
1569 */
1570static void
1571list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1572{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001573 struct perf_cpu_context *cpuctx;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001574
1575 WARN_ON_ONCE(event->ctx != ctx);
1576 lockdep_assert_held(&ctx->lock);
1577
Peter Zijlstra8a495422010-05-27 15:47:49 +02001578 /*
1579 * We can have double detach due to exit/hot-unplug + close.
1580 */
1581 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001582 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001583
1584 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1585
Stephane Eranian68cacd22011-03-23 16:03:06 +01001586 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001587 ctx->nr_cgroups--;
Peter Zijlstra70a01652016-01-08 09:29:16 +01001588 /*
1589 * Because cgroup events are always per-cpu events, this will
1590 * always be called from the right CPU.
1591 */
Stephane Eranian68cacd22011-03-23 16:03:06 +01001592 cpuctx = __get_cpu_context(ctx);
1593 /*
Peter Zijlstra70a01652016-01-08 09:29:16 +01001594 * If there are no more cgroup events then clear cgrp to avoid
1595 * stale pointer in update_cgrp_time_from_cpuctx().
Stephane Eranian68cacd22011-03-23 16:03:06 +01001596 */
1597 if (!ctx->nr_cgroups)
1598 cpuctx->cgrp = NULL;
1599 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001600
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001601 ctx->nr_events--;
1602 if (event->attr.inherit_stat)
1603 ctx->nr_stat--;
1604
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001605 list_del_rcu(&event->event_entry);
1606
Peter Zijlstra8a495422010-05-27 15:47:49 +02001607 if (event->group_leader == event)
1608 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001609
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001610 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001611
1612 /*
1613 * If event was in error state, then keep it
1614 * that way, otherwise bogus counts will be
1615 * returned on read(). The only way to get out
1616 * of error state is by explicit re-enabling
1617 * of the event
1618 */
1619 if (event->state > PERF_EVENT_STATE_OFF)
1620 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001621
1622 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001623}
1624
Peter Zijlstra8a495422010-05-27 15:47:49 +02001625static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001626{
1627 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001628 struct list_head *list = NULL;
1629
1630 /*
1631 * We can have double detach due to exit/hot-unplug + close.
1632 */
1633 if (!(event->attach_state & PERF_ATTACH_GROUP))
1634 return;
1635
1636 event->attach_state &= ~PERF_ATTACH_GROUP;
1637
1638 /*
1639 * If this is a sibling, remove it from its group.
1640 */
1641 if (event->group_leader != event) {
1642 list_del_init(&event->group_entry);
1643 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001644 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001645 }
1646
1647 if (!list_empty(&event->group_entry))
1648 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001649
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001650 /*
1651 * If this was a group event with sibling events then
1652 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001653 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001654 */
1655 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001656 if (list)
1657 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001658 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001659
1660 /* Inherit group flags from the previous leader */
1661 sibling->group_flags = event->group_flags;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001662
1663 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001664 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001665
1666out:
1667 perf_event__header_size(event->group_leader);
1668
1669 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1670 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001671}
1672
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001673static bool is_orphaned_event(struct perf_event *event)
1674{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01001675 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001676}
1677
Mark Rutland66eb5792015-05-13 17:12:23 +01001678static inline int pmu_filter_match(struct perf_event *event)
1679{
1680 struct pmu *pmu = event->pmu;
1681 return pmu->filter_match ? pmu->filter_match(event) : 1;
1682}
1683
Stephane Eranianfa66f072010-08-26 16:40:01 +02001684static inline int
1685event_filter_match(struct perf_event *event)
1686{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001687 return (event->cpu == -1 || event->cpu == smp_processor_id())
Mark Rutland66eb5792015-05-13 17:12:23 +01001688 && perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001689}
1690
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001691static void
1692event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001693 struct perf_cpu_context *cpuctx,
1694 struct perf_event_context *ctx)
1695{
Stephane Eranian41587552011-01-03 18:20:01 +02001696 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001697 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001698
1699 WARN_ON_ONCE(event->ctx != ctx);
1700 lockdep_assert_held(&ctx->lock);
1701
Stephane Eranianfa66f072010-08-26 16:40:01 +02001702 /*
1703 * An event which could not be activated because of
1704 * filter mismatch still needs to have its timings
1705 * maintained, otherwise bogus information is return
1706 * via read() for time_enabled, time_running:
1707 */
1708 if (event->state == PERF_EVENT_STATE_INACTIVE
1709 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001710 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001711 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001712 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001713 }
1714
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001715 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001716 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001717
Alexander Shishkin44377272013-12-16 14:17:36 +02001718 perf_pmu_disable(event->pmu);
1719
Peter Zijlstra28a967c2016-02-24 18:45:46 +01001720 event->tstamp_stopped = tstamp;
1721 event->pmu->del(event, 0);
1722 event->oncpu = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001723 event->state = PERF_EVENT_STATE_INACTIVE;
1724 if (event->pending_disable) {
1725 event->pending_disable = 0;
1726 event->state = PERF_EVENT_STATE_OFF;
1727 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001728
1729 if (!is_software_event(event))
1730 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001731 if (!--ctx->nr_active)
1732 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001733 if (event->attr.freq && event->attr.sample_freq)
1734 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001735 if (event->attr.exclusive || !cpuctx->active_oncpu)
1736 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001737
1738 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001739}
1740
1741static void
1742group_sched_out(struct perf_event *group_event,
1743 struct perf_cpu_context *cpuctx,
1744 struct perf_event_context *ctx)
1745{
1746 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001747 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001748
1749 event_sched_out(group_event, cpuctx, ctx);
1750
1751 /*
1752 * Schedule out siblings (if any):
1753 */
1754 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1755 event_sched_out(event, cpuctx, ctx);
1756
Stephane Eranianfa66f072010-08-26 16:40:01 +02001757 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001758 cpuctx->exclusive = 0;
1759}
1760
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001761#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01001762
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001763/*
1764 * Cross CPU call to remove a performance event
1765 *
1766 * We disable the event on the hardware level first. After that we
1767 * remove it from the context list.
1768 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001769static void
1770__perf_remove_from_context(struct perf_event *event,
1771 struct perf_cpu_context *cpuctx,
1772 struct perf_event_context *ctx,
1773 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001774{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001775 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001776
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001777 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001778 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001779 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001780 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001781
Peter Zijlstra39a43642016-01-11 12:46:35 +01001782 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001783 ctx->is_active = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01001784 if (ctx->task) {
1785 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1786 cpuctx->task_ctx = NULL;
1787 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001788 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001789}
1790
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001791/*
1792 * Remove the event from a task's (or a CPU's) list of events.
1793 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001794 * If event->ctx is a cloned context, callers must make sure that
1795 * every task struct that event->ctx->task could possibly point to
1796 * remains valid. This is OK when called from perf_release since
1797 * that only calls us on the top-level context, which can't be a clone.
1798 * When called from perf_event_exit_task, it's OK because the
1799 * context has been detached from its task.
1800 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001801static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001802{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001803 lockdep_assert_held(&event->ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001804
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001805 event_function_call(event, __perf_remove_from_context, (void *)flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001806}
1807
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001808/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001809 * Cross CPU call to disable a performance event
1810 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001811static void __perf_event_disable(struct perf_event *event,
1812 struct perf_cpu_context *cpuctx,
1813 struct perf_event_context *ctx,
1814 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001815{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001816 if (event->state < PERF_EVENT_STATE_INACTIVE)
1817 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001818
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001819 update_context_time(ctx);
1820 update_cgrp_time_from_event(event);
1821 update_group_times(event);
1822 if (event == event->group_leader)
1823 group_sched_out(event, cpuctx, ctx);
1824 else
1825 event_sched_out(event, cpuctx, ctx);
1826 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra7b648012015-12-03 18:35:21 +01001827}
1828
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001829/*
1830 * Disable a event.
1831 *
1832 * If event->ctx is a cloned context, callers must make sure that
1833 * every task struct that event->ctx->task could possibly point to
1834 * remains valid. This condition is satisifed when called through
1835 * perf_event_for_each_child or perf_event_for_each because they
1836 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001837 * goes to exit will block in perf_event_exit_event().
1838 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001839 * When called from perf_pending_event it's OK because event->ctx
1840 * is the current context on this CPU and preemption is disabled,
1841 * hence we can't get into perf_event_task_sched_out for this context.
1842 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001843static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001844{
1845 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001846
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001847 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001848 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001849 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001850 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001851 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001852 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001853
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001854 event_function_call(event, __perf_event_disable, NULL);
1855}
1856
1857void perf_event_disable_local(struct perf_event *event)
1858{
1859 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001860}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001861
1862/*
1863 * Strictly speaking kernel users cannot create groups and therefore this
1864 * interface does not need the perf_event_ctx_lock() magic.
1865 */
1866void perf_event_disable(struct perf_event *event)
1867{
1868 struct perf_event_context *ctx;
1869
1870 ctx = perf_event_ctx_lock(event);
1871 _perf_event_disable(event);
1872 perf_event_ctx_unlock(event, ctx);
1873}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001874EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001875
Stephane Eraniane5d13672011-02-14 11:20:01 +02001876static void perf_set_shadow_time(struct perf_event *event,
1877 struct perf_event_context *ctx,
1878 u64 tstamp)
1879{
1880 /*
1881 * use the correct time source for the time snapshot
1882 *
1883 * We could get by without this by leveraging the
1884 * fact that to get to this function, the caller
1885 * has most likely already called update_context_time()
1886 * and update_cgrp_time_xx() and thus both timestamp
1887 * are identical (or very close). Given that tstamp is,
1888 * already adjusted for cgroup, we could say that:
1889 * tstamp - ctx->timestamp
1890 * is equivalent to
1891 * tstamp - cgrp->timestamp.
1892 *
1893 * Then, in perf_output_read(), the calculation would
1894 * work with no changes because:
1895 * - event is guaranteed scheduled in
1896 * - no scheduled out in between
1897 * - thus the timestamp would be the same
1898 *
1899 * But this is a bit hairy.
1900 *
1901 * So instead, we have an explicit cgroup call to remain
1902 * within the time time source all along. We believe it
1903 * is cleaner and simpler to understand.
1904 */
1905 if (is_cgroup_event(event))
1906 perf_cgroup_set_shadow_time(event, tstamp);
1907 else
1908 event->shadow_ctx_time = tstamp - ctx->timestamp;
1909}
1910
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001911#define MAX_INTERRUPTS (~0ULL)
1912
1913static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001914static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001915
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001916static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001917event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001918 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001919 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001920{
Stephane Eranian41587552011-01-03 18:20:01 +02001921 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001922 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001923
Peter Zijlstra63342412014-05-05 11:49:16 +02001924 lockdep_assert_held(&ctx->lock);
1925
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001926 if (event->state <= PERF_EVENT_STATE_OFF)
1927 return 0;
1928
1929 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001930 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001931
1932 /*
1933 * Unthrottle events, since we scheduled we might have missed several
1934 * ticks already, also for a heavily scheduling task there is little
1935 * guarantee it'll get a tick in a timely manner.
1936 */
1937 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1938 perf_log_throttle(event, 1);
1939 event->hw.interrupts = 0;
1940 }
1941
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001942 /*
1943 * The new state must be visible before we turn it on in the hardware:
1944 */
1945 smp_wmb();
1946
Alexander Shishkin44377272013-12-16 14:17:36 +02001947 perf_pmu_disable(event->pmu);
1948
Shaohua Li72f669c2015-02-05 15:55:31 -08001949 perf_set_shadow_time(event, ctx, tstamp);
1950
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001951 perf_log_itrace_start(event);
1952
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001953 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001954 event->state = PERF_EVENT_STATE_INACTIVE;
1955 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001956 ret = -EAGAIN;
1957 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001958 }
1959
Peter Zijlstra00a29162015-07-27 10:35:07 +02001960 event->tstamp_running += tstamp - event->tstamp_stopped;
1961
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001962 if (!is_software_event(event))
1963 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001964 if (!ctx->nr_active++)
1965 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001966 if (event->attr.freq && event->attr.sample_freq)
1967 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001968
1969 if (event->attr.exclusive)
1970 cpuctx->exclusive = 1;
1971
Alexander Shishkin44377272013-12-16 14:17:36 +02001972out:
1973 perf_pmu_enable(event->pmu);
1974
1975 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001976}
1977
1978static int
1979group_sched_in(struct perf_event *group_event,
1980 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001981 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001982{
Lin Ming6bde9b62010-04-23 13:56:00 +08001983 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001984 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001985 u64 now = ctx->time;
1986 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001987
1988 if (group_event->state == PERF_EVENT_STATE_OFF)
1989 return 0;
1990
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001991 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08001992
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001993 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001994 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001995 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001996 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001997 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001998
1999 /*
2000 * Schedule in siblings as one group (if any):
2001 */
2002 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002003 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002004 partial_group = event;
2005 goto group_error;
2006 }
2007 }
2008
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002009 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002010 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002011
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002012group_error:
2013 /*
2014 * Groups can be scheduled in as one unit only, so undo any
2015 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02002016 * The events up to the failed event are scheduled out normally,
2017 * tstamp_stopped will be updated.
2018 *
2019 * The failed events and the remaining siblings need to have
2020 * their timings updated as if they had gone thru event_sched_in()
2021 * and event_sched_out(). This is required to get consistent timings
2022 * across the group. This also takes care of the case where the group
2023 * could never be scheduled by ensuring tstamp_stopped is set to mark
2024 * the time the event was actually stopped, such that time delta
2025 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002026 */
2027 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2028 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02002029 simulate = true;
2030
2031 if (simulate) {
2032 event->tstamp_running += now - event->tstamp_stopped;
2033 event->tstamp_stopped = now;
2034 } else {
2035 event_sched_out(event, cpuctx, ctx);
2036 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002037 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002038 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002039
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002040 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002041
Peter Zijlstra272325c2015-04-15 11:41:58 +02002042 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002043
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002044 return -EAGAIN;
2045}
2046
2047/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002048 * Work out whether we can put this event group on the CPU now.
2049 */
2050static int group_can_go_on(struct perf_event *event,
2051 struct perf_cpu_context *cpuctx,
2052 int can_add_hw)
2053{
2054 /*
2055 * Groups consisting entirely of software events can always go on.
2056 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01002057 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002058 return 1;
2059 /*
2060 * If an exclusive group is already on, no other hardware
2061 * events can go on.
2062 */
2063 if (cpuctx->exclusive)
2064 return 0;
2065 /*
2066 * If this group is exclusive and there are already
2067 * events on the CPU, it can't go on.
2068 */
2069 if (event->attr.exclusive && cpuctx->active_oncpu)
2070 return 0;
2071 /*
2072 * Otherwise, try to add it if all previous groups were able
2073 * to go on.
2074 */
2075 return can_add_hw;
2076}
2077
2078static void add_event_to_ctx(struct perf_event *event,
2079 struct perf_event_context *ctx)
2080{
Stephane Eranian41587552011-01-03 18:20:01 +02002081 u64 tstamp = perf_event_time(event);
2082
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002083 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002084 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002085 event->tstamp_enabled = tstamp;
2086 event->tstamp_running = tstamp;
2087 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002088}
2089
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002090static void ctx_sched_out(struct perf_event_context *ctx,
2091 struct perf_cpu_context *cpuctx,
2092 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002093static void
2094ctx_sched_in(struct perf_event_context *ctx,
2095 struct perf_cpu_context *cpuctx,
2096 enum event_type_t event_type,
2097 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002098
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002099static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2100 struct perf_event_context *ctx)
2101{
2102 if (!cpuctx->task_ctx)
2103 return;
2104
2105 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2106 return;
2107
2108 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2109}
2110
Peter Zijlstradce58552011-04-09 21:17:46 +02002111static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2112 struct perf_event_context *ctx,
2113 struct task_struct *task)
2114{
2115 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2116 if (ctx)
2117 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2118 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2119 if (ctx)
2120 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2121}
2122
Peter Zijlstra3e349502016-01-08 10:01:18 +01002123static void ctx_resched(struct perf_cpu_context *cpuctx,
2124 struct perf_event_context *task_ctx)
Peter Zijlstra00179602015-11-30 16:26:35 +01002125{
Peter Zijlstra3e349502016-01-08 10:01:18 +01002126 perf_pmu_disable(cpuctx->ctx.pmu);
2127 if (task_ctx)
2128 task_ctx_sched_out(cpuctx, task_ctx);
2129 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2130 perf_event_sched_in(cpuctx, task_ctx, current);
2131 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002132}
2133
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002134/*
2135 * Cross CPU call to install and enable a performance event
2136 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002137 * Very similar to remote_function() + event_function() but cannot assume that
2138 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002139 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002140static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002141{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002142 struct perf_event *event = info;
2143 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002144 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002145 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002146 bool activate = true;
2147 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002148
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002149 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002150 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002151 raw_spin_lock(&ctx->lock);
2152 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002153
2154 /* If we're on the wrong CPU, try again */
2155 if (task_cpu(ctx->task) != smp_processor_id()) {
2156 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002157 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002158 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002159
Peter Zijlstra39a43642016-01-11 12:46:35 +01002160 /*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002161 * If we're on the right CPU, see if the task we target is
2162 * current, if not we don't have to activate the ctx, a future
2163 * context switch will do that for us.
Peter Zijlstra39a43642016-01-11 12:46:35 +01002164 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002165 if (ctx->task != current)
2166 activate = false;
2167 else
2168 WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx);
2169
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002170 } else if (task_ctx) {
2171 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002172 }
2173
Peter Zijlstraa0963092016-02-24 18:45:50 +01002174 if (activate) {
2175 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2176 add_event_to_ctx(event, ctx);
2177 ctx_resched(cpuctx, task_ctx);
2178 } else {
2179 add_event_to_ctx(event, ctx);
2180 }
2181
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002182unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002183 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002184
Peter Zijlstraa0963092016-02-24 18:45:50 +01002185 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002186}
2187
2188/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002189 * Attach a performance event to a context.
2190 *
2191 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002192 */
2193static void
2194perf_install_in_context(struct perf_event_context *ctx,
2195 struct perf_event *event,
2196 int cpu)
2197{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002198 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002199
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002200 lockdep_assert_held(&ctx->mutex);
2201
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002202 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002203 if (event->cpu != -1)
2204 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002205
Peter Zijlstraa0963092016-02-24 18:45:50 +01002206 if (!task) {
2207 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002208 return;
2209 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002210
Peter Zijlstraa0963092016-02-24 18:45:50 +01002211 /*
2212 * Should not happen, we validate the ctx is still alive before calling.
2213 */
2214 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2215 return;
2216
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002217 /*
2218 * Installing events is tricky because we cannot rely on ctx->is_active
2219 * to be set in case this is the nr_events 0 -> 1 transition.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002220 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002221again:
2222 /*
2223 * Cannot use task_function_call() because we need to run on the task's
2224 * CPU regardless of whether its current or not.
2225 */
2226 if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event))
2227 return;
2228
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002229 raw_spin_lock_irq(&ctx->lock);
2230 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002231 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2232 /*
2233 * Cannot happen because we already checked above (which also
2234 * cannot happen), and we hold ctx->mutex, which serializes us
2235 * against perf_event_exit_task_context().
2236 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002237 raw_spin_unlock_irq(&ctx->lock);
2238 return;
2239 }
Peter Zijlstra39a43642016-01-11 12:46:35 +01002240 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstraa0963092016-02-24 18:45:50 +01002241 /*
2242 * Since !ctx->is_active doesn't mean anything, we must IPI
2243 * unconditionally.
2244 */
2245 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002246}
2247
2248/*
2249 * Put a event into inactive state and update time fields.
2250 * Enabling the leader of a group effectively enables all
2251 * the group members that aren't explicitly disabled, so we
2252 * have to update their ->tstamp_enabled also.
2253 * Note: this works for group members as well as group leaders
2254 * since the non-leader members' sibling_lists will be empty.
2255 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002256static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002257{
2258 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002259 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002260
2261 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002262 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002263 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002264 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2265 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002266 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002267}
2268
2269/*
2270 * Cross CPU call to enable a performance event
2271 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002272static void __perf_event_enable(struct perf_event *event,
2273 struct perf_cpu_context *cpuctx,
2274 struct perf_event_context *ctx,
2275 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002276{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002277 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002278 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002279
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002280 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2281 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002282 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002283
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002284 if (ctx->is_active)
2285 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2286
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002287 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002288
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002289 if (!ctx->is_active)
2290 return;
2291
Stephane Eraniane5d13672011-02-14 11:20:01 +02002292 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002293 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02002294 perf_cgroup_defer_enabled(event);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002295 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002296 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002297 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002298
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002299 /*
2300 * If the event is in a group and isn't the group leader,
2301 * then don't put it on unless the group is on.
2302 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002303 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2304 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002305 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002306 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002307
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002308 task_ctx = cpuctx->task_ctx;
2309 if (ctx->task)
2310 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002311
Peter Zijlstraaee7dbc2016-01-08 10:45:11 +01002312 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002313}
2314
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002315/*
2316 * Enable a event.
2317 *
2318 * If event->ctx is a cloned context, callers must make sure that
2319 * every task struct that event->ctx->task could possibly point to
2320 * remains valid. This condition is satisfied when called through
2321 * perf_event_for_each_child or perf_event_for_each as described
2322 * for perf_event_disable.
2323 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002324static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002325{
2326 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002327
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002328 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002329 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2330 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002331 raw_spin_unlock_irq(&ctx->lock);
2332 return;
2333 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002334
2335 /*
2336 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002337 *
2338 * That way, if we see the event in error state below, we know that it
2339 * has gone back into error state, as distinct from the task having
2340 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002341 */
2342 if (event->state == PERF_EVENT_STATE_ERROR)
2343 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002344 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002345
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002346 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002347}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002348
2349/*
2350 * See perf_event_disable();
2351 */
2352void perf_event_enable(struct perf_event *event)
2353{
2354 struct perf_event_context *ctx;
2355
2356 ctx = perf_event_ctx_lock(event);
2357 _perf_event_enable(event);
2358 perf_event_ctx_unlock(event, ctx);
2359}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002360EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002361
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002362static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002363{
2364 /*
2365 * not supported on inherited events
2366 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002367 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002368 return -EINVAL;
2369
2370 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002371 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002372
2373 return 0;
2374}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002375
2376/*
2377 * See perf_event_disable()
2378 */
2379int perf_event_refresh(struct perf_event *event, int refresh)
2380{
2381 struct perf_event_context *ctx;
2382 int ret;
2383
2384 ctx = perf_event_ctx_lock(event);
2385 ret = _perf_event_refresh(event, refresh);
2386 perf_event_ctx_unlock(event, ctx);
2387
2388 return ret;
2389}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002390EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002391
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002392static void ctx_sched_out(struct perf_event_context *ctx,
2393 struct perf_cpu_context *cpuctx,
2394 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002395{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002396 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002397 struct perf_event *event;
2398
2399 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002400
Peter Zijlstra39a43642016-01-11 12:46:35 +01002401 if (likely(!ctx->nr_events)) {
2402 /*
2403 * See __perf_remove_from_context().
2404 */
2405 WARN_ON_ONCE(ctx->is_active);
2406 if (ctx->task)
2407 WARN_ON_ONCE(cpuctx->task_ctx);
2408 return;
2409 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002410
Peter Zijlstradb24d332011-04-09 21:17:45 +02002411 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002412 if (!(ctx->is_active & EVENT_ALL))
2413 ctx->is_active = 0;
2414
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002415 if (ctx->task) {
2416 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2417 if (!ctx->is_active)
2418 cpuctx->task_ctx = NULL;
2419 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002420
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002421 /*
2422 * Always update time if it was set; not only when it changes.
2423 * Otherwise we can 'forget' to update time for any but the last
2424 * context we sched out. For example:
2425 *
2426 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2427 * ctx_sched_out(.event_type = EVENT_PINNED)
2428 *
2429 * would only update time for the pinned events.
2430 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002431 if (is_active & EVENT_TIME) {
2432 /* update (and stop) ctx time */
2433 update_context_time(ctx);
2434 update_cgrp_time_from_cpuctx(cpuctx);
2435 }
2436
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002437 is_active ^= ctx->is_active; /* changed bits */
2438
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002439 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002440 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002441
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002442 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002443 if (is_active & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002444 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2445 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002446 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002447
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002448 if (is_active & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002449 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002450 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002451 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002452 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002453}
2454
2455/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002456 * Test whether two contexts are equivalent, i.e. whether they have both been
2457 * cloned from the same version of the same context.
2458 *
2459 * Equivalence is measured using a generation number in the context that is
2460 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2461 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002462 */
2463static int context_equiv(struct perf_event_context *ctx1,
2464 struct perf_event_context *ctx2)
2465{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002466 lockdep_assert_held(&ctx1->lock);
2467 lockdep_assert_held(&ctx2->lock);
2468
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002469 /* Pinning disables the swap optimization */
2470 if (ctx1->pin_count || ctx2->pin_count)
2471 return 0;
2472
2473 /* If ctx1 is the parent of ctx2 */
2474 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2475 return 1;
2476
2477 /* If ctx2 is the parent of ctx1 */
2478 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2479 return 1;
2480
2481 /*
2482 * If ctx1 and ctx2 have the same parent; we flatten the parent
2483 * hierarchy, see perf_event_init_context().
2484 */
2485 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2486 ctx1->parent_gen == ctx2->parent_gen)
2487 return 1;
2488
2489 /* Unmatched */
2490 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002491}
2492
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002493static void __perf_event_sync_stat(struct perf_event *event,
2494 struct perf_event *next_event)
2495{
2496 u64 value;
2497
2498 if (!event->attr.inherit_stat)
2499 return;
2500
2501 /*
2502 * Update the event value, we cannot use perf_event_read()
2503 * because we're in the middle of a context switch and have IRQs
2504 * disabled, which upsets smp_call_function_single(), however
2505 * we know the event must be on the current CPU, therefore we
2506 * don't need to use it.
2507 */
2508 switch (event->state) {
2509 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002510 event->pmu->read(event);
2511 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002512
2513 case PERF_EVENT_STATE_INACTIVE:
2514 update_event_times(event);
2515 break;
2516
2517 default:
2518 break;
2519 }
2520
2521 /*
2522 * In order to keep per-task stats reliable we need to flip the event
2523 * values when we flip the contexts.
2524 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002525 value = local64_read(&next_event->count);
2526 value = local64_xchg(&event->count, value);
2527 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002528
2529 swap(event->total_time_enabled, next_event->total_time_enabled);
2530 swap(event->total_time_running, next_event->total_time_running);
2531
2532 /*
2533 * Since we swizzled the values, update the user visible data too.
2534 */
2535 perf_event_update_userpage(event);
2536 perf_event_update_userpage(next_event);
2537}
2538
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002539static void perf_event_sync_stat(struct perf_event_context *ctx,
2540 struct perf_event_context *next_ctx)
2541{
2542 struct perf_event *event, *next_event;
2543
2544 if (!ctx->nr_stat)
2545 return;
2546
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002547 update_context_time(ctx);
2548
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002549 event = list_first_entry(&ctx->event_list,
2550 struct perf_event, event_entry);
2551
2552 next_event = list_first_entry(&next_ctx->event_list,
2553 struct perf_event, event_entry);
2554
2555 while (&event->event_entry != &ctx->event_list &&
2556 &next_event->event_entry != &next_ctx->event_list) {
2557
2558 __perf_event_sync_stat(event, next_event);
2559
2560 event = list_next_entry(event, event_entry);
2561 next_event = list_next_entry(next_event, event_entry);
2562 }
2563}
2564
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002565static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2566 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002567{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002568 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002569 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002570 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002571 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002572 int do_switch = 1;
2573
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002574 if (likely(!ctx))
2575 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002576
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002577 cpuctx = __get_cpu_context(ctx);
2578 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002579 return;
2580
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002581 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002582 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002583 if (!next_ctx)
2584 goto unlock;
2585
2586 parent = rcu_dereference(ctx->parent_ctx);
2587 next_parent = rcu_dereference(next_ctx->parent_ctx);
2588
2589 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002590 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002591 goto unlock;
2592
2593 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002594 /*
2595 * Looks like the two contexts are clones, so we might be
2596 * able to optimize the context switch. We lock both
2597 * contexts and check that they are clones under the
2598 * lock (including re-checking that neither has been
2599 * uncloned in the meantime). It doesn't matter which
2600 * order we take the locks because no other cpu could
2601 * be trying to lock both of these tasks.
2602 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002603 raw_spin_lock(&ctx->lock);
2604 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002605 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002606 WRITE_ONCE(ctx->task, next);
2607 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05002608
2609 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2610
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002611 /*
2612 * RCU_INIT_POINTER here is safe because we've not
2613 * modified the ctx and the above modification of
2614 * ctx->task and ctx->task_ctx_data are immaterial
2615 * since those values are always verified under
2616 * ctx->lock which we're now holding.
2617 */
2618 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2619 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2620
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002621 do_switch = 0;
2622
2623 perf_event_sync_stat(ctx, next_ctx);
2624 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002625 raw_spin_unlock(&next_ctx->lock);
2626 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002627 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002628unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002629 rcu_read_unlock();
2630
2631 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002632 raw_spin_lock(&ctx->lock);
Peter Zijlstra8833d0e2016-01-08 10:02:37 +01002633 task_ctx_sched_out(cpuctx, ctx);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002634 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002635 }
2636}
2637
Yan, Zhengba532502014-11-04 21:55:58 -05002638void perf_sched_cb_dec(struct pmu *pmu)
2639{
2640 this_cpu_dec(perf_sched_cb_usages);
2641}
2642
2643void perf_sched_cb_inc(struct pmu *pmu)
2644{
2645 this_cpu_inc(perf_sched_cb_usages);
2646}
2647
2648/*
2649 * This function provides the context switch callback to the lower code
2650 * layer. It is invoked ONLY when the context switch callback is enabled.
2651 */
2652static void perf_pmu_sched_task(struct task_struct *prev,
2653 struct task_struct *next,
2654 bool sched_in)
2655{
2656 struct perf_cpu_context *cpuctx;
2657 struct pmu *pmu;
2658 unsigned long flags;
2659
2660 if (prev == next)
2661 return;
2662
2663 local_irq_save(flags);
2664
2665 rcu_read_lock();
2666
2667 list_for_each_entry_rcu(pmu, &pmus, entry) {
2668 if (pmu->sched_task) {
2669 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2670
2671 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2672
2673 perf_pmu_disable(pmu);
2674
2675 pmu->sched_task(cpuctx->task_ctx, sched_in);
2676
2677 perf_pmu_enable(pmu);
2678
2679 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2680 }
2681 }
2682
2683 rcu_read_unlock();
2684
2685 local_irq_restore(flags);
2686}
2687
Adrian Hunter45ac1402015-07-21 12:44:02 +03002688static void perf_event_switch(struct task_struct *task,
2689 struct task_struct *next_prev, bool sched_in);
2690
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002691#define for_each_task_context_nr(ctxn) \
2692 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2693
2694/*
2695 * Called from scheduler to remove the events of the current task,
2696 * with interrupts disabled.
2697 *
2698 * We stop each event and update the event value in event->count.
2699 *
2700 * This does not protect us against NMI, but disable()
2701 * sets the disabled bit in the control field of event _before_
2702 * accessing the event control register. If a NMI hits, then it will
2703 * not restart the event.
2704 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002705void __perf_event_task_sched_out(struct task_struct *task,
2706 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002707{
2708 int ctxn;
2709
Yan, Zhengba532502014-11-04 21:55:58 -05002710 if (__this_cpu_read(perf_sched_cb_usages))
2711 perf_pmu_sched_task(task, next, false);
2712
Adrian Hunter45ac1402015-07-21 12:44:02 +03002713 if (atomic_read(&nr_switch_events))
2714 perf_event_switch(task, next, false);
2715
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002716 for_each_task_context_nr(ctxn)
2717 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002718
2719 /*
2720 * if cgroup events exist on this CPU, then we need
2721 * to check if we have to switch out PMU state.
2722 * cgroup event are system-wide mode only
2723 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002724 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002725 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002726}
2727
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002728/*
2729 * Called with IRQs disabled
2730 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002731static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2732 enum event_type_t event_type)
2733{
2734 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002735}
2736
2737static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002738ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002739 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002740{
2741 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002742
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002743 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2744 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002745 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002746 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002747 continue;
2748
Stephane Eraniane5d13672011-02-14 11:20:01 +02002749 /* may need to reset tstamp_enabled */
2750 if (is_cgroup_event(event))
2751 perf_cgroup_mark_enabled(event, ctx);
2752
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002753 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002754 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002755
2756 /*
2757 * If this pinned group hasn't been scheduled,
2758 * put it in error state.
2759 */
2760 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2761 update_group_times(event);
2762 event->state = PERF_EVENT_STATE_ERROR;
2763 }
2764 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002765}
2766
2767static void
2768ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002769 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002770{
2771 struct perf_event *event;
2772 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002773
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002774 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2775 /* Ignore events in OFF or ERROR state */
2776 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002777 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002778 /*
2779 * Listen to the 'cpu' scheduling filter constraint
2780 * of events:
2781 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002782 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002783 continue;
2784
Stephane Eraniane5d13672011-02-14 11:20:01 +02002785 /* may need to reset tstamp_enabled */
2786 if (is_cgroup_event(event))
2787 perf_cgroup_mark_enabled(event, ctx);
2788
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002789 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002790 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002791 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002792 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002793 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002794}
2795
2796static void
2797ctx_sched_in(struct perf_event_context *ctx,
2798 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002799 enum event_type_t event_type,
2800 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002801{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002802 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002803 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002804
Peter Zijlstrac994d612016-01-08 09:20:23 +01002805 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002806
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002807 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002808 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002809
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002810 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002811 if (ctx->task) {
2812 if (!is_active)
2813 cpuctx->task_ctx = ctx;
2814 else
2815 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2816 }
2817
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002818 is_active ^= ctx->is_active; /* changed bits */
2819
2820 if (is_active & EVENT_TIME) {
2821 /* start ctx time */
2822 now = perf_clock();
2823 ctx->timestamp = now;
2824 perf_cgroup_set_timestamp(task, ctx);
2825 }
2826
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002827 /*
2828 * First go through the list and put on any pinned groups
2829 * in order to give them the best chance of going on.
2830 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002831 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002832 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002833
2834 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002835 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002836 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002837}
2838
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002839static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002840 enum event_type_t event_type,
2841 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002842{
2843 struct perf_event_context *ctx = &cpuctx->ctx;
2844
Stephane Eraniane5d13672011-02-14 11:20:01 +02002845 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002846}
2847
Stephane Eraniane5d13672011-02-14 11:20:01 +02002848static void perf_event_context_sched_in(struct perf_event_context *ctx,
2849 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002850{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002851 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002852
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002853 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002854 if (cpuctx->task_ctx == ctx)
2855 return;
2856
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002857 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002858 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002859 /*
2860 * We want to keep the following priority order:
2861 * cpu pinned (that don't need to move), task pinned,
2862 * cpu flexible, task flexible.
2863 */
2864 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002865 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002866 perf_pmu_enable(ctx->pmu);
2867 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002868}
2869
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002870/*
2871 * Called from scheduler to add the events of the current task
2872 * with interrupts disabled.
2873 *
2874 * We restore the event value and then enable it.
2875 *
2876 * This does not protect us against NMI, but enable()
2877 * sets the enabled bit in the control field of event _before_
2878 * accessing the event control register. If a NMI hits, then it will
2879 * keep the event running.
2880 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002881void __perf_event_task_sched_in(struct task_struct *prev,
2882 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002883{
2884 struct perf_event_context *ctx;
2885 int ctxn;
2886
Peter Zijlstra7e41d172016-01-08 09:21:40 +01002887 /*
2888 * If cgroup events exist on this CPU, then we need to check if we have
2889 * to switch in PMU state; cgroup event are system-wide mode only.
2890 *
2891 * Since cgroup events are CPU events, we must schedule these in before
2892 * we schedule in the task events.
2893 */
2894 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
2895 perf_cgroup_sched_in(prev, task);
2896
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002897 for_each_task_context_nr(ctxn) {
2898 ctx = task->perf_event_ctxp[ctxn];
2899 if (likely(!ctx))
2900 continue;
2901
Stephane Eraniane5d13672011-02-14 11:20:01 +02002902 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002903 }
Stephane Eraniand010b332012-02-09 23:21:00 +01002904
Adrian Hunter45ac1402015-07-21 12:44:02 +03002905 if (atomic_read(&nr_switch_events))
2906 perf_event_switch(task, prev, true);
2907
Yan, Zhengba532502014-11-04 21:55:58 -05002908 if (__this_cpu_read(perf_sched_cb_usages))
2909 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002910}
2911
Peter Zijlstraabd50712010-01-26 18:50:16 +01002912static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2913{
2914 u64 frequency = event->attr.sample_freq;
2915 u64 sec = NSEC_PER_SEC;
2916 u64 divisor, dividend;
2917
2918 int count_fls, nsec_fls, frequency_fls, sec_fls;
2919
2920 count_fls = fls64(count);
2921 nsec_fls = fls64(nsec);
2922 frequency_fls = fls64(frequency);
2923 sec_fls = 30;
2924
2925 /*
2926 * We got @count in @nsec, with a target of sample_freq HZ
2927 * the target period becomes:
2928 *
2929 * @count * 10^9
2930 * period = -------------------
2931 * @nsec * sample_freq
2932 *
2933 */
2934
2935 /*
2936 * Reduce accuracy by one bit such that @a and @b converge
2937 * to a similar magnitude.
2938 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002939#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002940do { \
2941 if (a##_fls > b##_fls) { \
2942 a >>= 1; \
2943 a##_fls--; \
2944 } else { \
2945 b >>= 1; \
2946 b##_fls--; \
2947 } \
2948} while (0)
2949
2950 /*
2951 * Reduce accuracy until either term fits in a u64, then proceed with
2952 * the other, so that finally we can do a u64/u64 division.
2953 */
2954 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2955 REDUCE_FLS(nsec, frequency);
2956 REDUCE_FLS(sec, count);
2957 }
2958
2959 if (count_fls + sec_fls > 64) {
2960 divisor = nsec * frequency;
2961
2962 while (count_fls + sec_fls > 64) {
2963 REDUCE_FLS(count, sec);
2964 divisor >>= 1;
2965 }
2966
2967 dividend = count * sec;
2968 } else {
2969 dividend = count * sec;
2970
2971 while (nsec_fls + frequency_fls > 64) {
2972 REDUCE_FLS(nsec, frequency);
2973 dividend >>= 1;
2974 }
2975
2976 divisor = nsec * frequency;
2977 }
2978
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002979 if (!divisor)
2980 return dividend;
2981
Peter Zijlstraabd50712010-01-26 18:50:16 +01002982 return div64_u64(dividend, divisor);
2983}
2984
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002985static DEFINE_PER_CPU(int, perf_throttled_count);
2986static DEFINE_PER_CPU(u64, perf_throttled_seq);
2987
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002988static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002989{
2990 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002991 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002992 s64 delta;
2993
Peter Zijlstraabd50712010-01-26 18:50:16 +01002994 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002995
2996 delta = (s64)(period - hwc->sample_period);
2997 delta = (delta + 7) / 8; /* low pass filter */
2998
2999 sample_period = hwc->sample_period + delta;
3000
3001 if (!sample_period)
3002 sample_period = 1;
3003
3004 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003005
Peter Zijlstrae7850592010-05-21 14:43:08 +02003006 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003007 if (disable)
3008 event->pmu->stop(event, PERF_EF_UPDATE);
3009
Peter Zijlstrae7850592010-05-21 14:43:08 +02003010 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003011
3012 if (disable)
3013 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003014 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003015}
3016
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003017/*
3018 * combine freq adjustment with unthrottling to avoid two passes over the
3019 * events. At the same time, make sure, having freq events does not change
3020 * the rate of unthrottling as that would introduce bias.
3021 */
3022static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3023 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024{
3025 struct perf_event *event;
3026 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003027 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003028 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003029
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003030 /*
3031 * only need to iterate over all events iff:
3032 * - context have events in frequency mode (needs freq adjust)
3033 * - there are events to unthrottle on this cpu
3034 */
3035 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003036 return;
3037
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003038 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003039 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003040
Paul Mackerras03541f82009-10-14 16:58:03 +11003041 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003042 if (event->state != PERF_EVENT_STATE_ACTIVE)
3043 continue;
3044
Stephane Eranian5632ab12011-01-03 18:20:01 +02003045 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003046 continue;
3047
Alexander Shishkin44377272013-12-16 14:17:36 +02003048 perf_pmu_disable(event->pmu);
3049
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003050 hwc = &event->hw;
3051
Jiri Olsaae23bff2013-08-24 16:45:54 +02003052 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003053 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003054 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003055 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003056 }
3057
3058 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003059 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003060
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003061 /*
3062 * stop the event and update event->count
3063 */
3064 event->pmu->stop(event, PERF_EF_UPDATE);
3065
Peter Zijlstrae7850592010-05-21 14:43:08 +02003066 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003067 delta = now - hwc->freq_count_stamp;
3068 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003069
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003070 /*
3071 * restart the event
3072 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003073 * we have stopped the event so tell that
3074 * to perf_adjust_period() to avoid stopping it
3075 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003076 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003077 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003078 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003079
3080 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003081 next:
3082 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003083 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003084
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003085 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003086 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003087}
3088
3089/*
3090 * Round-robin a context's events:
3091 */
3092static void rotate_ctx(struct perf_event_context *ctx)
3093{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003094 /*
3095 * Rotate the first entry last of non-pinned groups. Rotation might be
3096 * disabled by the inheritance code.
3097 */
3098 if (!ctx->rotate_disable)
3099 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003100}
3101
Stephane Eranian9e630202013-04-03 14:21:33 +02003102static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003103{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003104 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003105 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003106
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003107 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003108 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3109 rotate = 1;
3110 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003111
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003112 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003113 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003114 if (ctx->nr_events != ctx->nr_active)
3115 rotate = 1;
3116 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003117
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003118 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003119 goto done;
3120
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003121 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003122 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003123
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003124 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3125 if (ctx)
3126 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003127
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003128 rotate_ctx(&cpuctx->ctx);
3129 if (ctx)
3130 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003131
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003132 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003133
3134 perf_pmu_enable(cpuctx->ctx.pmu);
3135 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003136done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003137
3138 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003139}
3140
3141void perf_event_task_tick(void)
3142{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003143 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3144 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003145 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003146
3147 WARN_ON(!irqs_disabled());
3148
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003149 __this_cpu_inc(perf_throttled_seq);
3150 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003151 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003152
Mark Rutland2fde4f92015-01-07 15:01:54 +00003153 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003154 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003155}
3156
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003157static int event_enable_on_exec(struct perf_event *event,
3158 struct perf_event_context *ctx)
3159{
3160 if (!event->attr.enable_on_exec)
3161 return 0;
3162
3163 event->attr.enable_on_exec = 0;
3164 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3165 return 0;
3166
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003167 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003168
3169 return 1;
3170}
3171
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003172/*
3173 * Enable all of a task's events that have been marked enable-on-exec.
3174 * This expects task == current.
3175 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003176static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003177{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003178 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003179 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003180 struct perf_event *event;
3181 unsigned long flags;
3182 int enabled = 0;
3183
3184 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003185 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003186 if (!ctx || !ctx->nr_events)
3187 goto out;
3188
Peter Zijlstra3e349502016-01-08 10:01:18 +01003189 cpuctx = __get_cpu_context(ctx);
3190 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01003191 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003192 list_for_each_entry(event, &ctx->event_list, event_entry)
3193 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003194
3195 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003196 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003197 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003198 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003199 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003200 ctx_resched(cpuctx, ctx);
3201 }
3202 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003203
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003204out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003205 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003206
3207 if (clone_ctx)
3208 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003209}
3210
Peter Zijlstrae041e322014-05-21 17:32:19 +02003211void perf_event_exec(void)
3212{
Peter Zijlstrae041e322014-05-21 17:32:19 +02003213 int ctxn;
3214
3215 rcu_read_lock();
Peter Zijlstrac1274492015-12-10 20:57:40 +01003216 for_each_task_context_nr(ctxn)
3217 perf_event_enable_on_exec(ctxn);
Peter Zijlstrae041e322014-05-21 17:32:19 +02003218 rcu_read_unlock();
3219}
3220
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003221struct perf_read_data {
3222 struct perf_event *event;
3223 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003224 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003225};
3226
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003227/*
3228 * Cross CPU call to read the hardware event
3229 */
3230static void __perf_event_read(void *info)
3231{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003232 struct perf_read_data *data = info;
3233 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003234 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003235 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003236 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003237
3238 /*
3239 * If this is a task context, we need to check whether it is
3240 * the current task context of this cpu. If not it has been
3241 * scheduled out before the smp call arrived. In that case
3242 * event->count would have been updated to a recent sample
3243 * when the event was scheduled out.
3244 */
3245 if (ctx->task && cpuctx->task_ctx != ctx)
3246 return;
3247
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003248 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003249 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003250 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003251 update_cgrp_time_from_event(event);
3252 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003253
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003254 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003255 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003256 goto unlock;
3257
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003258 if (!data->group) {
3259 pmu->read(event);
3260 data->ret = 0;
3261 goto unlock;
3262 }
3263
3264 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3265
3266 pmu->read(event);
3267
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003268 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3269 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003270 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3271 /*
3272 * Use sibling's PMU rather than @event's since
3273 * sibling could be on different (eg: software) PMU.
3274 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003275 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003276 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003277 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003278
3279 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003280
3281unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003282 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003283}
3284
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003285static inline u64 perf_event_count(struct perf_event *event)
3286{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003287 if (event->pmu->count)
3288 return event->pmu->count(event);
3289
3290 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003291}
3292
Kaixu Xiaffe86902015-08-06 07:02:32 +00003293/*
3294 * NMI-safe method to read a local event, that is an event that
3295 * is:
3296 * - either for the current task, or for this CPU
3297 * - does not have inherit set, for inherited task events
3298 * will not be local and we cannot read them atomically
3299 * - must not have a pmu::count method
3300 */
3301u64 perf_event_read_local(struct perf_event *event)
3302{
3303 unsigned long flags;
3304 u64 val;
3305
3306 /*
3307 * Disabling interrupts avoids all counter scheduling (context
3308 * switches, timer based rotation and IPIs).
3309 */
3310 local_irq_save(flags);
3311
3312 /* If this is a per-task event, it must be for current */
3313 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3314 event->hw.target != current);
3315
3316 /* If this is a per-CPU event, it must be for this CPU */
3317 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3318 event->cpu != smp_processor_id());
3319
3320 /*
3321 * It must not be an event with inherit set, we cannot read
3322 * all child counters from atomic context.
3323 */
3324 WARN_ON_ONCE(event->attr.inherit);
3325
3326 /*
3327 * It must not have a pmu::count method, those are not
3328 * NMI safe.
3329 */
3330 WARN_ON_ONCE(event->pmu->count);
3331
3332 /*
3333 * If the event is currently on this CPU, its either a per-task event,
3334 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3335 * oncpu == -1).
3336 */
3337 if (event->oncpu == smp_processor_id())
3338 event->pmu->read(event);
3339
3340 val = local64_read(&event->count);
3341 local_irq_restore(flags);
3342
3343 return val;
3344}
3345
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003346static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003347{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003348 int ret = 0;
3349
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003350 /*
3351 * If event is enabled and currently active on a CPU, update the
3352 * value in the event structure:
3353 */
3354 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003355 struct perf_read_data data = {
3356 .event = event,
3357 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003358 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003359 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003360 smp_call_function_single(event->oncpu,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003361 __perf_event_read, &data, 1);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003362 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003363 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003364 struct perf_event_context *ctx = event->ctx;
3365 unsigned long flags;
3366
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003367 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003368 /*
3369 * may read while context is not active
3370 * (e.g., thread is blocked), in that case
3371 * we cannot update context time
3372 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003373 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003374 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003375 update_cgrp_time_from_event(event);
3376 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003377 if (group)
3378 update_group_times(event);
3379 else
3380 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003381 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003382 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003383
3384 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003385}
3386
3387/*
3388 * Initialize the perf_event context in a task_struct:
3389 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003390static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003391{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003392 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003393 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003394 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003395 INIT_LIST_HEAD(&ctx->pinned_groups);
3396 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003397 INIT_LIST_HEAD(&ctx->event_list);
3398 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003399}
3400
Peter Zijlstraeb184472010-09-07 15:55:13 +02003401static struct perf_event_context *
3402alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003403{
3404 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003405
3406 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3407 if (!ctx)
3408 return NULL;
3409
3410 __perf_event_init_context(ctx);
3411 if (task) {
3412 ctx->task = task;
3413 get_task_struct(task);
3414 }
3415 ctx->pmu = pmu;
3416
3417 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003418}
3419
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003420static struct task_struct *
3421find_lively_task_by_vpid(pid_t vpid)
3422{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003423 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003424 int err;
3425
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003426 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003427 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003428 task = current;
3429 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003430 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003431 if (task)
3432 get_task_struct(task);
3433 rcu_read_unlock();
3434
3435 if (!task)
3436 return ERR_PTR(-ESRCH);
3437
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003438 /* Reuse ptrace permission checks for now. */
3439 err = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08003440 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003441 goto errout;
3442
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003443 return task;
3444errout:
3445 put_task_struct(task);
3446 return ERR_PTR(err);
3447
3448}
3449
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003450/*
3451 * Returns a matching context with refcount and pincount.
3452 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003453static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003454find_get_context(struct pmu *pmu, struct task_struct *task,
3455 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003456{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003457 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003458 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003459 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003460 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003461 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003462 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003463
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003464 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003465 /* Must be root to operate on a CPU event: */
3466 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3467 return ERR_PTR(-EACCES);
3468
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003469 /*
3470 * We could be clever and allow to attach a event to an
3471 * offline CPU and activate it when the CPU comes up, but
3472 * that's for later.
3473 */
3474 if (!cpu_online(cpu))
3475 return ERR_PTR(-ENODEV);
3476
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003477 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003478 ctx = &cpuctx->ctx;
3479 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003480 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003481
3482 return ctx;
3483 }
3484
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003485 err = -EINVAL;
3486 ctxn = pmu->task_ctx_nr;
3487 if (ctxn < 0)
3488 goto errout;
3489
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003490 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3491 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3492 if (!task_ctx_data) {
3493 err = -ENOMEM;
3494 goto errout;
3495 }
3496 }
3497
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003498retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003499 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003500 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003501 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003502 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003503
3504 if (task_ctx_data && !ctx->task_ctx_data) {
3505 ctx->task_ctx_data = task_ctx_data;
3506 task_ctx_data = NULL;
3507 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003508 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003509
3510 if (clone_ctx)
3511 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003512 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003513 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003514 err = -ENOMEM;
3515 if (!ctx)
3516 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003517
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003518 if (task_ctx_data) {
3519 ctx->task_ctx_data = task_ctx_data;
3520 task_ctx_data = NULL;
3521 }
3522
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003523 err = 0;
3524 mutex_lock(&task->perf_event_mutex);
3525 /*
3526 * If it has already passed perf_event_exit_task().
3527 * we must see PF_EXITING, it takes this mutex too.
3528 */
3529 if (task->flags & PF_EXITING)
3530 err = -ESRCH;
3531 else if (task->perf_event_ctxp[ctxn])
3532 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003533 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003534 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003535 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003536 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003537 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003538 mutex_unlock(&task->perf_event_mutex);
3539
3540 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003541 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003542
3543 if (err == -EAGAIN)
3544 goto retry;
3545 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003546 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003547 }
3548
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003549 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003550 return ctx;
3551
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003552errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003553 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003554 return ERR_PTR(err);
3555}
3556
Li Zefan6fb29152009-10-15 11:21:42 +08003557static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003558static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003559
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003560static void free_event_rcu(struct rcu_head *head)
3561{
3562 struct perf_event *event;
3563
3564 event = container_of(head, struct perf_event, rcu_head);
3565 if (event->ns)
3566 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003567 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003568 kfree(event);
3569}
3570
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003571static void ring_buffer_attach(struct perf_event *event,
3572 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003573
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003574static void unaccount_event_cpu(struct perf_event *event, int cpu)
3575{
3576 if (event->parent)
3577 return;
3578
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003579 if (is_cgroup_event(event))
3580 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3581}
3582
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003583#ifdef CONFIG_NO_HZ_FULL
3584static DEFINE_SPINLOCK(nr_freq_lock);
3585#endif
3586
3587static void unaccount_freq_event_nohz(void)
3588{
3589#ifdef CONFIG_NO_HZ_FULL
3590 spin_lock(&nr_freq_lock);
3591 if (atomic_dec_and_test(&nr_freq_events))
3592 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
3593 spin_unlock(&nr_freq_lock);
3594#endif
3595}
3596
3597static void unaccount_freq_event(void)
3598{
3599 if (tick_nohz_full_enabled())
3600 unaccount_freq_event_nohz();
3601 else
3602 atomic_dec(&nr_freq_events);
3603}
3604
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003605static void unaccount_event(struct perf_event *event)
3606{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003607 bool dec = false;
3608
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003609 if (event->parent)
3610 return;
3611
3612 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003613 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003614 if (event->attr.mmap || event->attr.mmap_data)
3615 atomic_dec(&nr_mmap_events);
3616 if (event->attr.comm)
3617 atomic_dec(&nr_comm_events);
3618 if (event->attr.task)
3619 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003620 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003621 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03003622 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003623 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003624 atomic_dec(&nr_switch_events);
3625 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003626 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003627 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003628 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003629 dec = true;
3630
Peter Zijlstra9107c892016-02-24 18:45:45 +01003631 if (dec) {
3632 if (!atomic_add_unless(&perf_sched_count, -1, 1))
3633 schedule_delayed_work(&perf_sched_work, HZ);
3634 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003635
3636 unaccount_event_cpu(event, event->cpu);
3637}
3638
Peter Zijlstra9107c892016-02-24 18:45:45 +01003639static void perf_sched_delayed(struct work_struct *work)
3640{
3641 mutex_lock(&perf_sched_mutex);
3642 if (atomic_dec_and_test(&perf_sched_count))
3643 static_branch_disable(&perf_sched_events);
3644 mutex_unlock(&perf_sched_mutex);
3645}
3646
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003647/*
3648 * The following implement mutual exclusion of events on "exclusive" pmus
3649 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3650 * at a time, so we disallow creating events that might conflict, namely:
3651 *
3652 * 1) cpu-wide events in the presence of per-task events,
3653 * 2) per-task events in the presence of cpu-wide events,
3654 * 3) two matching events on the same context.
3655 *
3656 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003657 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003658 */
3659static int exclusive_event_init(struct perf_event *event)
3660{
3661 struct pmu *pmu = event->pmu;
3662
3663 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3664 return 0;
3665
3666 /*
3667 * Prevent co-existence of per-task and cpu-wide events on the
3668 * same exclusive pmu.
3669 *
3670 * Negative pmu::exclusive_cnt means there are cpu-wide
3671 * events on this "exclusive" pmu, positive means there are
3672 * per-task events.
3673 *
3674 * Since this is called in perf_event_alloc() path, event::ctx
3675 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3676 * to mean "per-task event", because unlike other attach states it
3677 * never gets cleared.
3678 */
3679 if (event->attach_state & PERF_ATTACH_TASK) {
3680 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
3681 return -EBUSY;
3682 } else {
3683 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
3684 return -EBUSY;
3685 }
3686
3687 return 0;
3688}
3689
3690static void exclusive_event_destroy(struct perf_event *event)
3691{
3692 struct pmu *pmu = event->pmu;
3693
3694 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3695 return;
3696
3697 /* see comment in exclusive_event_init() */
3698 if (event->attach_state & PERF_ATTACH_TASK)
3699 atomic_dec(&pmu->exclusive_cnt);
3700 else
3701 atomic_inc(&pmu->exclusive_cnt);
3702}
3703
3704static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
3705{
3706 if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
3707 (e1->cpu == e2->cpu ||
3708 e1->cpu == -1 ||
3709 e2->cpu == -1))
3710 return true;
3711 return false;
3712}
3713
3714/* Called under the same ctx::mutex as perf_install_in_context() */
3715static bool exclusive_event_installable(struct perf_event *event,
3716 struct perf_event_context *ctx)
3717{
3718 struct perf_event *iter_event;
3719 struct pmu *pmu = event->pmu;
3720
3721 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3722 return true;
3723
3724 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
3725 if (exclusive_event_match(iter_event, event))
3726 return false;
3727 }
3728
3729 return true;
3730}
3731
Peter Zijlstra683ede42014-05-05 12:11:24 +02003732static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003733{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003734 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003735
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003736 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003737
Frederic Weisbecker76369132011-05-19 19:55:04 +02003738 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003739 /*
3740 * Can happen when we close an event with re-directed output.
3741 *
3742 * Since we have a 0 refcount, perf_mmap_close() will skip
3743 * over us; possibly making our ring_buffer_put() the last.
3744 */
3745 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003746 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003747 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003748 }
3749
Stephane Eraniane5d13672011-02-14 11:20:01 +02003750 if (is_cgroup_event(event))
3751 perf_detach_cgroup(event);
3752
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003753 if (!event->parent) {
3754 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3755 put_callchain_buffers();
3756 }
3757
3758 perf_event_free_bpf_prog(event);
3759
3760 if (event->destroy)
3761 event->destroy(event);
3762
3763 if (event->ctx)
3764 put_ctx(event->ctx);
3765
3766 if (event->pmu) {
3767 exclusive_event_destroy(event);
3768 module_put(event->pmu->module);
3769 }
3770
3771 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003772}
3773
Peter Zijlstra683ede42014-05-05 12:11:24 +02003774/*
3775 * Used to free events which have a known refcount of 1, such as in error paths
3776 * where the event isn't exposed yet and inherited events.
3777 */
3778static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003779{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003780 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3781 "unexpected event refcount: %ld; ptr=%p\n",
3782 atomic_long_read(&event->refcount), event)) {
3783 /* leak to avoid use-after-free */
3784 return;
3785 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003786
Peter Zijlstra683ede42014-05-05 12:11:24 +02003787 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003788}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003789
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003790/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003791 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003792 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003793static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003794{
Peter Zijlstra88821352010-11-09 19:01:43 +01003795 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003796
Peter Zijlstra88821352010-11-09 19:01:43 +01003797 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01003798 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003799 * Matches the smp_store_release() in perf_event_exit_task(). If we
3800 * observe !owner it means the list deletion is complete and we can
3801 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01003802 * owner->perf_event_mutex.
3803 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003804 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01003805 if (owner) {
3806 /*
3807 * Since delayed_put_task_struct() also drops the last
3808 * task reference we can safely take a new reference
3809 * while holding the rcu_read_lock().
3810 */
3811 get_task_struct(owner);
3812 }
3813 rcu_read_unlock();
3814
3815 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003816 /*
3817 * If we're here through perf_event_exit_task() we're already
3818 * holding ctx->mutex which would be an inversion wrt. the
3819 * normal lock order.
3820 *
3821 * However we can safely take this lock because its the child
3822 * ctx->mutex.
3823 */
3824 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
3825
Peter Zijlstra88821352010-11-09 19:01:43 +01003826 /*
3827 * We have to re-check the event->owner field, if it is cleared
3828 * we raced with perf_event_exit_task(), acquiring the mutex
3829 * ensured they're done, and we can proceed with freeing the
3830 * event.
3831 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003832 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01003833 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003834 smp_store_release(&event->owner, NULL);
3835 }
Peter Zijlstra88821352010-11-09 19:01:43 +01003836 mutex_unlock(&owner->perf_event_mutex);
3837 put_task_struct(owner);
3838 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003839}
3840
Jiri Olsaf8697762014-08-01 14:33:01 +02003841static void put_event(struct perf_event *event)
3842{
Jiri Olsaf8697762014-08-01 14:33:01 +02003843 if (!atomic_long_dec_and_test(&event->refcount))
3844 return;
3845
Peter Zijlstra683ede42014-05-05 12:11:24 +02003846 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003847}
3848
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003849/*
3850 * Kill an event dead; while event:refcount will preserve the event
3851 * object, it will not preserve its functionality. Once the last 'user'
3852 * gives up the object, we'll destroy the thing.
3853 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02003854int perf_event_release_kernel(struct perf_event *event)
3855{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003856 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003857 struct perf_event *child, *tmp;
3858
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003859 /*
3860 * If we got here through err_file: fput(event_file); we will not have
3861 * attached to a context yet.
3862 */
3863 if (!ctx) {
3864 WARN_ON_ONCE(event->attach_state &
3865 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
3866 goto no_ctx;
3867 }
3868
Peter Zijlstra88821352010-11-09 19:01:43 +01003869 if (!is_kernel_event(event))
3870 perf_remove_from_owner(event);
3871
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01003872 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02003873 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003874 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01003875
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003876 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01003877 /*
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003878 * Mark this even as STATE_DEAD, there is no external reference to it
3879 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003880 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003881 * Anybody acquiring event->child_mutex after the below loop _must_
3882 * also see this, most importantly inherit_event() which will avoid
3883 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003884 *
3885 * Thus this guarantees that we will in fact observe and kill _ALL_
3886 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01003887 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003888 event->state = PERF_EVENT_STATE_DEAD;
3889 raw_spin_unlock_irq(&ctx->lock);
3890
3891 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01003892
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003893again:
3894 mutex_lock(&event->child_mutex);
3895 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01003896
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003897 /*
3898 * Cannot change, child events are not migrated, see the
3899 * comment with perf_event_ctx_lock_nested().
3900 */
3901 ctx = lockless_dereference(child->ctx);
3902 /*
3903 * Since child_mutex nests inside ctx::mutex, we must jump
3904 * through hoops. We start by grabbing a reference on the ctx.
3905 *
3906 * Since the event cannot get freed while we hold the
3907 * child_mutex, the context must also exist and have a !0
3908 * reference count.
3909 */
3910 get_ctx(ctx);
3911
3912 /*
3913 * Now that we have a ctx ref, we can drop child_mutex, and
3914 * acquire ctx::mutex without fear of it going away. Then we
3915 * can re-acquire child_mutex.
3916 */
3917 mutex_unlock(&event->child_mutex);
3918 mutex_lock(&ctx->mutex);
3919 mutex_lock(&event->child_mutex);
3920
3921 /*
3922 * Now that we hold ctx::mutex and child_mutex, revalidate our
3923 * state, if child is still the first entry, it didn't get freed
3924 * and we can continue doing so.
3925 */
3926 tmp = list_first_entry_or_null(&event->child_list,
3927 struct perf_event, child_list);
3928 if (tmp == child) {
3929 perf_remove_from_context(child, DETACH_GROUP);
3930 list_del(&child->child_list);
3931 free_event(child);
3932 /*
3933 * This matches the refcount bump in inherit_event();
3934 * this can't be the last reference.
3935 */
3936 put_event(event);
3937 }
3938
3939 mutex_unlock(&event->child_mutex);
3940 mutex_unlock(&ctx->mutex);
3941 put_ctx(ctx);
3942 goto again;
3943 }
3944 mutex_unlock(&event->child_mutex);
3945
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003946no_ctx:
3947 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02003948 return 0;
3949}
3950EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3951
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02003952/*
3953 * Called when the last reference to the file is gone.
3954 */
Al Viroa6fa9412012-08-20 14:59:25 +01003955static int perf_release(struct inode *inode, struct file *file)
3956{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003957 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01003958 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003959}
3960
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003961u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003962{
3963 struct perf_event *child;
3964 u64 total = 0;
3965
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003966 *enabled = 0;
3967 *running = 0;
3968
Peter Zijlstra6f105812009-11-20 22:19:56 +01003969 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003970
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003971 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003972 total += perf_event_count(event);
3973
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003974 *enabled += event->total_time_enabled +
3975 atomic64_read(&event->child_total_time_enabled);
3976 *running += event->total_time_running +
3977 atomic64_read(&event->child_total_time_running);
3978
3979 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003980 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07003981 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003982 *enabled += child->total_time_enabled;
3983 *running += child->total_time_running;
3984 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003985 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003986
3987 return total;
3988}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003989EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003990
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003991static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003992 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003993{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07003994 struct perf_event *sub;
3995 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003996 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003997
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003998 ret = perf_event_read(leader, true);
3999 if (ret)
4000 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004001
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004002 /*
4003 * Since we co-schedule groups, {enabled,running} times of siblings
4004 * will be identical to those of the leader, so we only publish one
4005 * set.
4006 */
4007 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4008 values[n++] += leader->total_time_enabled +
4009 atomic64_read(&leader->child_total_time_enabled);
4010 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004011
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004012 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4013 values[n++] += leader->total_time_running +
4014 atomic64_read(&leader->child_total_time_running);
4015 }
4016
4017 /*
4018 * Write {count,id} tuples for every sibling.
4019 */
4020 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004021 if (read_format & PERF_FORMAT_ID)
4022 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004023
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004024 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004025 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004026 if (read_format & PERF_FORMAT_ID)
4027 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004028 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004029
4030 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004031}
4032
4033static int perf_read_group(struct perf_event *event,
4034 u64 read_format, char __user *buf)
4035{
4036 struct perf_event *leader = event->group_leader, *child;
4037 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004038 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004039 u64 *values;
4040
4041 lockdep_assert_held(&ctx->mutex);
4042
4043 values = kzalloc(event->read_size, GFP_KERNEL);
4044 if (!values)
4045 return -ENOMEM;
4046
4047 values[0] = 1 + leader->nr_siblings;
4048
4049 /*
4050 * By locking the child_mutex of the leader we effectively
4051 * lock the child list of all siblings.. XXX explain how.
4052 */
4053 mutex_lock(&leader->child_mutex);
4054
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004055 ret = __perf_read_group_add(leader, read_format, values);
4056 if (ret)
4057 goto unlock;
4058
4059 list_for_each_entry(child, &leader->child_list, child_list) {
4060 ret = __perf_read_group_add(child, read_format, values);
4061 if (ret)
4062 goto unlock;
4063 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004064
4065 mutex_unlock(&leader->child_mutex);
4066
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004067 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004068 if (copy_to_user(buf, values, event->read_size))
4069 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004070 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004071
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004072unlock:
4073 mutex_unlock(&leader->child_mutex);
4074out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004075 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004076 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004077}
4078
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004079static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004080 u64 read_format, char __user *buf)
4081{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004082 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004083 u64 values[4];
4084 int n = 0;
4085
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004086 values[n++] = perf_event_read_value(event, &enabled, &running);
4087 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4088 values[n++] = enabled;
4089 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4090 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004091 if (read_format & PERF_FORMAT_ID)
4092 values[n++] = primary_event_id(event);
4093
4094 if (copy_to_user(buf, values, n * sizeof(u64)))
4095 return -EFAULT;
4096
4097 return n * sizeof(u64);
4098}
4099
Jiri Olsadc633982014-09-12 13:18:26 +02004100static bool is_event_hup(struct perf_event *event)
4101{
4102 bool no_children;
4103
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004104 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004105 return false;
4106
4107 mutex_lock(&event->child_mutex);
4108 no_children = list_empty(&event->child_list);
4109 mutex_unlock(&event->child_mutex);
4110 return no_children;
4111}
4112
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004113/*
4114 * Read the performance event - simple non blocking version for now
4115 */
4116static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004117__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004118{
4119 u64 read_format = event->attr.read_format;
4120 int ret;
4121
4122 /*
4123 * Return end-of-file for a read on a event that is in
4124 * error state (i.e. because it was pinned but it couldn't be
4125 * scheduled on to the CPU at some point).
4126 */
4127 if (event->state == PERF_EVENT_STATE_ERROR)
4128 return 0;
4129
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004130 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004131 return -ENOSPC;
4132
4133 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004134 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004135 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004136 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004137 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004138
4139 return ret;
4140}
4141
4142static ssize_t
4143perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4144{
4145 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004146 struct perf_event_context *ctx;
4147 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004148
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004149 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004150 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004151 perf_event_ctx_unlock(event, ctx);
4152
4153 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004154}
4155
4156static unsigned int perf_poll(struct file *file, poll_table *wait)
4157{
4158 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004159 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004160 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004161
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004162 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004163
Jiri Olsadc633982014-09-12 13:18:26 +02004164 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004165 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004166
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004167 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004168 * Pin the event->rb by taking event->mmap_mutex; otherwise
4169 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004170 */
4171 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004172 rb = event->rb;
4173 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004174 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004175 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004176 return events;
4177}
4178
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004179static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004180{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004181 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004182 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004183 perf_event_update_userpage(event);
4184}
4185
4186/*
4187 * Holding the top-level event's child_mutex means that any
4188 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004189 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004190 * task existence requirements of perf_event_enable/disable.
4191 */
4192static void perf_event_for_each_child(struct perf_event *event,
4193 void (*func)(struct perf_event *))
4194{
4195 struct perf_event *child;
4196
4197 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004198
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004199 mutex_lock(&event->child_mutex);
4200 func(event);
4201 list_for_each_entry(child, &event->child_list, child_list)
4202 func(child);
4203 mutex_unlock(&event->child_mutex);
4204}
4205
4206static void perf_event_for_each(struct perf_event *event,
4207 void (*func)(struct perf_event *))
4208{
4209 struct perf_event_context *ctx = event->ctx;
4210 struct perf_event *sibling;
4211
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004212 lockdep_assert_held(&ctx->mutex);
4213
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004214 event = event->group_leader;
4215
4216 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004217 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004218 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004219}
4220
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004221static void __perf_event_period(struct perf_event *event,
4222 struct perf_cpu_context *cpuctx,
4223 struct perf_event_context *ctx,
4224 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004225{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004226 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004227 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004228
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004229 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004230 event->attr.sample_freq = value;
4231 } else {
4232 event->attr.sample_period = value;
4233 event->hw.sample_period = value;
4234 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004235
4236 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4237 if (active) {
4238 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01004239 /*
4240 * We could be throttled; unthrottle now to avoid the tick
4241 * trying to unthrottle while we already re-started the event.
4242 */
4243 if (event->hw.interrupts == MAX_INTERRUPTS) {
4244 event->hw.interrupts = 0;
4245 perf_log_throttle(event, 1);
4246 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004247 event->pmu->stop(event, PERF_EF_UPDATE);
4248 }
4249
4250 local64_set(&event->hw.period_left, 0);
4251
4252 if (active) {
4253 event->pmu->start(event, PERF_EF_RELOAD);
4254 perf_pmu_enable(ctx->pmu);
4255 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004256}
4257
4258static int perf_event_period(struct perf_event *event, u64 __user *arg)
4259{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004260 u64 value;
4261
4262 if (!is_sampling_event(event))
4263 return -EINVAL;
4264
4265 if (copy_from_user(&value, arg, sizeof(value)))
4266 return -EFAULT;
4267
4268 if (!value)
4269 return -EINVAL;
4270
4271 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4272 return -EINVAL;
4273
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004274 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004275
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004276 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004277}
4278
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004279static const struct file_operations perf_fops;
4280
Al Viro2903ff02012-08-28 12:52:22 -04004281static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004282{
Al Viro2903ff02012-08-28 12:52:22 -04004283 struct fd f = fdget(fd);
4284 if (!f.file)
4285 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004286
Al Viro2903ff02012-08-28 12:52:22 -04004287 if (f.file->f_op != &perf_fops) {
4288 fdput(f);
4289 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004290 }
Al Viro2903ff02012-08-28 12:52:22 -04004291 *p = f;
4292 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004293}
4294
4295static int perf_event_set_output(struct perf_event *event,
4296 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004297static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004298static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004299
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004300static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004301{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004302 void (*func)(struct perf_event *);
4303 u32 flags = arg;
4304
4305 switch (cmd) {
4306 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004307 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004308 break;
4309 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004310 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004311 break;
4312 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004313 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004314 break;
4315
4316 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004317 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004318
4319 case PERF_EVENT_IOC_PERIOD:
4320 return perf_event_period(event, (u64 __user *)arg);
4321
Jiri Olsacf4957f2012-10-24 13:37:58 +02004322 case PERF_EVENT_IOC_ID:
4323 {
4324 u64 id = primary_event_id(event);
4325
4326 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4327 return -EFAULT;
4328 return 0;
4329 }
4330
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004331 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004332 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004333 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004334 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004335 struct perf_event *output_event;
4336 struct fd output;
4337 ret = perf_fget_light(arg, &output);
4338 if (ret)
4339 return ret;
4340 output_event = output.file->private_data;
4341 ret = perf_event_set_output(event, output_event);
4342 fdput(output);
4343 } else {
4344 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004345 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004346 return ret;
4347 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004348
Li Zefan6fb29152009-10-15 11:21:42 +08004349 case PERF_EVENT_IOC_SET_FILTER:
4350 return perf_event_set_filter(event, (void __user *)arg);
4351
Alexei Starovoitov25415172015-03-25 12:49:20 -07004352 case PERF_EVENT_IOC_SET_BPF:
4353 return perf_event_set_bpf_prog(event, arg);
4354
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004355 default:
4356 return -ENOTTY;
4357 }
4358
4359 if (flags & PERF_IOC_FLAG_GROUP)
4360 perf_event_for_each(event, func);
4361 else
4362 perf_event_for_each_child(event, func);
4363
4364 return 0;
4365}
4366
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004367static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4368{
4369 struct perf_event *event = file->private_data;
4370 struct perf_event_context *ctx;
4371 long ret;
4372
4373 ctx = perf_event_ctx_lock(event);
4374 ret = _perf_ioctl(event, cmd, arg);
4375 perf_event_ctx_unlock(event, ctx);
4376
4377 return ret;
4378}
4379
Pawel Mollb3f20782014-06-13 16:03:32 +01004380#ifdef CONFIG_COMPAT
4381static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4382 unsigned long arg)
4383{
4384 switch (_IOC_NR(cmd)) {
4385 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4386 case _IOC_NR(PERF_EVENT_IOC_ID):
4387 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4388 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4389 cmd &= ~IOCSIZE_MASK;
4390 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4391 }
4392 break;
4393 }
4394 return perf_ioctl(file, cmd, arg);
4395}
4396#else
4397# define perf_compat_ioctl NULL
4398#endif
4399
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004400int perf_event_task_enable(void)
4401{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004402 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004403 struct perf_event *event;
4404
4405 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004406 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4407 ctx = perf_event_ctx_lock(event);
4408 perf_event_for_each_child(event, _perf_event_enable);
4409 perf_event_ctx_unlock(event, ctx);
4410 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004411 mutex_unlock(&current->perf_event_mutex);
4412
4413 return 0;
4414}
4415
4416int perf_event_task_disable(void)
4417{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004418 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004419 struct perf_event *event;
4420
4421 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004422 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4423 ctx = perf_event_ctx_lock(event);
4424 perf_event_for_each_child(event, _perf_event_disable);
4425 perf_event_ctx_unlock(event, ctx);
4426 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004427 mutex_unlock(&current->perf_event_mutex);
4428
4429 return 0;
4430}
4431
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004432static int perf_event_index(struct perf_event *event)
4433{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004434 if (event->hw.state & PERF_HES_STOPPED)
4435 return 0;
4436
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004437 if (event->state != PERF_EVENT_STATE_ACTIVE)
4438 return 0;
4439
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004440 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004441}
4442
Eric B Munsonc4794292011-06-23 16:34:38 -04004443static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004444 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004445 u64 *enabled,
4446 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004447{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004448 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004449
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004450 *now = perf_clock();
4451 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004452 *enabled = ctx_time - event->tstamp_enabled;
4453 *running = ctx_time - event->tstamp_running;
4454}
4455
Peter Zijlstrafa731582013-09-19 10:16:42 +02004456static void perf_event_init_userpage(struct perf_event *event)
4457{
4458 struct perf_event_mmap_page *userpg;
4459 struct ring_buffer *rb;
4460
4461 rcu_read_lock();
4462 rb = rcu_dereference(event->rb);
4463 if (!rb)
4464 goto unlock;
4465
4466 userpg = rb->user_page;
4467
4468 /* Allow new userspace to detect that bit 0 is deprecated */
4469 userpg->cap_bit0_is_deprecated = 1;
4470 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004471 userpg->data_offset = PAGE_SIZE;
4472 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004473
4474unlock:
4475 rcu_read_unlock();
4476}
4477
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004478void __weak arch_perf_update_userpage(
4479 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004480{
4481}
4482
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004483/*
4484 * Callers need to ensure there can be no nesting of this function, otherwise
4485 * the seqlock logic goes bad. We can not serialize this because the arch
4486 * code calls this from NMI context.
4487 */
4488void perf_event_update_userpage(struct perf_event *event)
4489{
4490 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004491 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004492 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004493
4494 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004495 rb = rcu_dereference(event->rb);
4496 if (!rb)
4497 goto unlock;
4498
Eric B Munson0d641202011-06-24 12:26:26 -04004499 /*
4500 * compute total_time_enabled, total_time_running
4501 * based on snapshot values taken when the event
4502 * was last scheduled in.
4503 *
4504 * we cannot simply called update_context_time()
4505 * because of locking issue as we can be called in
4506 * NMI context
4507 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004508 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004509
Frederic Weisbecker76369132011-05-19 19:55:04 +02004510 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004511 /*
4512 * Disable preemption so as to not let the corresponding user-space
4513 * spin too long if we get preempted.
4514 */
4515 preempt_disable();
4516 ++userpg->lock;
4517 barrier();
4518 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004519 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004520 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004521 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004522
Eric B Munson0d641202011-06-24 12:26:26 -04004523 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004524 atomic64_read(&event->child_total_time_enabled);
4525
Eric B Munson0d641202011-06-24 12:26:26 -04004526 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004527 atomic64_read(&event->child_total_time_running);
4528
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004529 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004530
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004531 barrier();
4532 ++userpg->lock;
4533 preempt_enable();
4534unlock:
4535 rcu_read_unlock();
4536}
4537
Peter Zijlstra906010b2009-09-21 16:08:49 +02004538static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4539{
4540 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004541 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004542 int ret = VM_FAULT_SIGBUS;
4543
4544 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4545 if (vmf->pgoff == 0)
4546 ret = 0;
4547 return ret;
4548 }
4549
4550 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004551 rb = rcu_dereference(event->rb);
4552 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004553 goto unlock;
4554
4555 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4556 goto unlock;
4557
Frederic Weisbecker76369132011-05-19 19:55:04 +02004558 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004559 if (!vmf->page)
4560 goto unlock;
4561
4562 get_page(vmf->page);
4563 vmf->page->mapping = vma->vm_file->f_mapping;
4564 vmf->page->index = vmf->pgoff;
4565
4566 ret = 0;
4567unlock:
4568 rcu_read_unlock();
4569
4570 return ret;
4571}
4572
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004573static void ring_buffer_attach(struct perf_event *event,
4574 struct ring_buffer *rb)
4575{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004576 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004577 unsigned long flags;
4578
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004579 if (event->rb) {
4580 /*
4581 * Should be impossible, we set this when removing
4582 * event->rb_entry and wait/clear when adding event->rb_entry.
4583 */
4584 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004585
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004586 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004587 spin_lock_irqsave(&old_rb->event_lock, flags);
4588 list_del_rcu(&event->rb_entry);
4589 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004590
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004591 event->rcu_batches = get_state_synchronize_rcu();
4592 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004593 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004594
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004595 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004596 if (event->rcu_pending) {
4597 cond_synchronize_rcu(event->rcu_batches);
4598 event->rcu_pending = 0;
4599 }
4600
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004601 spin_lock_irqsave(&rb->event_lock, flags);
4602 list_add_rcu(&event->rb_entry, &rb->event_list);
4603 spin_unlock_irqrestore(&rb->event_lock, flags);
4604 }
4605
4606 rcu_assign_pointer(event->rb, rb);
4607
4608 if (old_rb) {
4609 ring_buffer_put(old_rb);
4610 /*
4611 * Since we detached before setting the new rb, so that we
4612 * could attach the new rb, we could have missed a wakeup.
4613 * Provide it now.
4614 */
4615 wake_up_all(&event->waitq);
4616 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004617}
4618
4619static void ring_buffer_wakeup(struct perf_event *event)
4620{
4621 struct ring_buffer *rb;
4622
4623 rcu_read_lock();
4624 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004625 if (rb) {
4626 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4627 wake_up_all(&event->waitq);
4628 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004629 rcu_read_unlock();
4630}
4631
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004632struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004633{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004634 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004635
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004636 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004637 rb = rcu_dereference(event->rb);
4638 if (rb) {
4639 if (!atomic_inc_not_zero(&rb->refcount))
4640 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004641 }
4642 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004643
Frederic Weisbecker76369132011-05-19 19:55:04 +02004644 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004645}
4646
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004647void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004648{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004649 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004650 return;
4651
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004652 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004653
Frederic Weisbecker76369132011-05-19 19:55:04 +02004654 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004655}
4656
4657static void perf_mmap_open(struct vm_area_struct *vma)
4658{
4659 struct perf_event *event = vma->vm_file->private_data;
4660
4661 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004662 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004663
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004664 if (vma->vm_pgoff)
4665 atomic_inc(&event->rb->aux_mmap_count);
4666
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004667 if (event->pmu->event_mapped)
4668 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004669}
4670
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004671/*
4672 * A buffer can be mmap()ed multiple times; either directly through the same
4673 * event, or through other events by use of perf_event_set_output().
4674 *
4675 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4676 * the buffer here, where we still have a VM context. This means we need
4677 * to detach all events redirecting to us.
4678 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004679static void perf_mmap_close(struct vm_area_struct *vma)
4680{
4681 struct perf_event *event = vma->vm_file->private_data;
4682
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004683 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004684 struct user_struct *mmap_user = rb->mmap_user;
4685 int mmap_locked = rb->mmap_locked;
4686 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004687
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004688 if (event->pmu->event_unmapped)
4689 event->pmu->event_unmapped(event);
4690
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004691 /*
4692 * rb->aux_mmap_count will always drop before rb->mmap_count and
4693 * event->mmap_count, so it is ok to use event->mmap_mutex to
4694 * serialize with perf_mmap here.
4695 */
4696 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
4697 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
4698 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
4699 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
4700
4701 rb_free_aux(rb);
4702 mutex_unlock(&event->mmap_mutex);
4703 }
4704
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004705 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004706
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004707 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004708 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004709
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004710 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004711 mutex_unlock(&event->mmap_mutex);
4712
4713 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004714 if (atomic_read(&rb->mmap_count))
4715 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004716
4717 /*
4718 * No other mmap()s, detach from all other events that might redirect
4719 * into the now unreachable buffer. Somewhat complicated by the
4720 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4721 */
4722again:
4723 rcu_read_lock();
4724 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4725 if (!atomic_long_inc_not_zero(&event->refcount)) {
4726 /*
4727 * This event is en-route to free_event() which will
4728 * detach it and remove it from the list.
4729 */
4730 continue;
4731 }
4732 rcu_read_unlock();
4733
4734 mutex_lock(&event->mmap_mutex);
4735 /*
4736 * Check we didn't race with perf_event_set_output() which can
4737 * swizzle the rb from under us while we were waiting to
4738 * acquire mmap_mutex.
4739 *
4740 * If we find a different rb; ignore this event, a next
4741 * iteration will no longer find it on the list. We have to
4742 * still restart the iteration to make sure we're not now
4743 * iterating the wrong list.
4744 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004745 if (event->rb == rb)
4746 ring_buffer_attach(event, NULL);
4747
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004748 mutex_unlock(&event->mmap_mutex);
4749 put_event(event);
4750
4751 /*
4752 * Restart the iteration; either we're on the wrong list or
4753 * destroyed its integrity by doing a deletion.
4754 */
4755 goto again;
4756 }
4757 rcu_read_unlock();
4758
4759 /*
4760 * It could be there's still a few 0-ref events on the list; they'll
4761 * get cleaned up by free_event() -- they'll also still have their
4762 * ref on the rb and will free it whenever they are done with it.
4763 *
4764 * Aside from that, this buffer is 'fully' detached and unmapped,
4765 * undo the VM accounting.
4766 */
4767
4768 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4769 vma->vm_mm->pinned_vm -= mmap_locked;
4770 free_uid(mmap_user);
4771
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004772out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004773 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004774}
4775
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004776static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004777 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004778 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004779 .fault = perf_mmap_fault,
4780 .page_mkwrite = perf_mmap_fault,
4781};
4782
4783static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4784{
4785 struct perf_event *event = file->private_data;
4786 unsigned long user_locked, user_lock_limit;
4787 struct user_struct *user = current_user();
4788 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004789 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004790 unsigned long vma_size;
4791 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004792 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004793 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004794
Peter Zijlstrac7920612010-05-18 10:33:24 +02004795 /*
4796 * Don't allow mmap() of inherited per-task counters. This would
4797 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004798 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004799 */
4800 if (event->cpu == -1 && event->attr.inherit)
4801 return -EINVAL;
4802
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004803 if (!(vma->vm_flags & VM_SHARED))
4804 return -EINVAL;
4805
4806 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004807
4808 if (vma->vm_pgoff == 0) {
4809 nr_pages = (vma_size / PAGE_SIZE) - 1;
4810 } else {
4811 /*
4812 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
4813 * mapped, all subsequent mappings should have the same size
4814 * and offset. Must be above the normal perf buffer.
4815 */
4816 u64 aux_offset, aux_size;
4817
4818 if (!event->rb)
4819 return -EINVAL;
4820
4821 nr_pages = vma_size / PAGE_SIZE;
4822
4823 mutex_lock(&event->mmap_mutex);
4824 ret = -EINVAL;
4825
4826 rb = event->rb;
4827 if (!rb)
4828 goto aux_unlock;
4829
4830 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
4831 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
4832
4833 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
4834 goto aux_unlock;
4835
4836 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
4837 goto aux_unlock;
4838
4839 /* already mapped with a different offset */
4840 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
4841 goto aux_unlock;
4842
4843 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
4844 goto aux_unlock;
4845
4846 /* already mapped with a different size */
4847 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
4848 goto aux_unlock;
4849
4850 if (!is_power_of_2(nr_pages))
4851 goto aux_unlock;
4852
4853 if (!atomic_inc_not_zero(&rb->mmap_count))
4854 goto aux_unlock;
4855
4856 if (rb_has_aux(rb)) {
4857 atomic_inc(&rb->aux_mmap_count);
4858 ret = 0;
4859 goto unlock;
4860 }
4861
4862 atomic_set(&rb->aux_mmap_count, 1);
4863 user_extra = nr_pages;
4864
4865 goto accounting;
4866 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004867
4868 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004869 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004870 * can do bitmasks instead of modulo.
4871 */
Kan Liang2ed11312015-03-02 02:14:26 -05004872 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004873 return -EINVAL;
4874
4875 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4876 return -EINVAL;
4877
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004878 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004879again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004880 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004881 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004882 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004883 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004884 goto unlock;
4885 }
4886
4887 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4888 /*
4889 * Raced against perf_mmap_close() through
4890 * perf_event_set_output(). Try again, hope for better
4891 * luck.
4892 */
4893 mutex_unlock(&event->mmap_mutex);
4894 goto again;
4895 }
4896
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004897 goto unlock;
4898 }
4899
4900 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004901
4902accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004903 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4904
4905 /*
4906 * Increase the limit linearly with more CPUs:
4907 */
4908 user_lock_limit *= num_online_cpus();
4909
4910 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4911
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004912 if (user_locked > user_lock_limit)
4913 extra = user_locked - user_lock_limit;
4914
Jiri Slaby78d7d402010-03-05 13:42:54 -08004915 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004916 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004917 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004918
4919 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4920 !capable(CAP_IPC_LOCK)) {
4921 ret = -EPERM;
4922 goto unlock;
4923 }
4924
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004925 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004926
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004927 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004928 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004929
Frederic Weisbecker76369132011-05-19 19:55:04 +02004930 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004931 rb = rb_alloc(nr_pages,
4932 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4933 event->cpu, flags);
4934
4935 if (!rb) {
4936 ret = -ENOMEM;
4937 goto unlock;
4938 }
4939
4940 atomic_set(&rb->mmap_count, 1);
4941 rb->mmap_user = get_current_user();
4942 rb->mmap_locked = extra;
4943
4944 ring_buffer_attach(event, rb);
4945
4946 perf_event_init_userpage(event);
4947 perf_event_update_userpage(event);
4948 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02004949 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
4950 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004951 if (!ret)
4952 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004953 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004954
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004955unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004956 if (!ret) {
4957 atomic_long_add(user_extra, &user->locked_vm);
4958 vma->vm_mm->pinned_vm += extra;
4959
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004960 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004961 } else if (rb) {
4962 atomic_dec(&rb->mmap_count);
4963 }
4964aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004965 mutex_unlock(&event->mmap_mutex);
4966
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004967 /*
4968 * Since pinned accounting is per vm we cannot allow fork() to copy our
4969 * vma.
4970 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004971 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004972 vma->vm_ops = &perf_mmap_vmops;
4973
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004974 if (event->pmu->event_mapped)
4975 event->pmu->event_mapped(event);
4976
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004977 return ret;
4978}
4979
4980static int perf_fasync(int fd, struct file *filp, int on)
4981{
Al Viro496ad9a2013-01-23 17:07:38 -05004982 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004983 struct perf_event *event = filp->private_data;
4984 int retval;
4985
Al Viro59551022016-01-22 15:40:57 -05004986 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004987 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05004988 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004989
4990 if (retval < 0)
4991 return retval;
4992
4993 return 0;
4994}
4995
4996static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004997 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004998 .release = perf_release,
4999 .read = perf_read,
5000 .poll = perf_poll,
5001 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01005002 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005003 .mmap = perf_mmap,
5004 .fasync = perf_fasync,
5005};
5006
5007/*
5008 * Perf event wakeup
5009 *
5010 * If there's data, ensure we set the poll() state and publish everything
5011 * to user-space before waking everybody up.
5012 */
5013
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005014static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
5015{
5016 /* only the parent has fasync state */
5017 if (event->parent)
5018 event = event->parent;
5019 return &event->fasync;
5020}
5021
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005022void perf_event_wakeup(struct perf_event *event)
5023{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005024 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005025
5026 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005027 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005028 event->pending_kill = 0;
5029 }
5030}
5031
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005032static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005033{
5034 struct perf_event *event = container_of(entry,
5035 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01005036 int rctx;
5037
5038 rctx = perf_swevent_get_recursion_context();
5039 /*
5040 * If we 'fail' here, that's OK, it means recursion is already disabled
5041 * and we won't recurse 'further'.
5042 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005043
5044 if (event->pending_disable) {
5045 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005046 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005047 }
5048
5049 if (event->pending_wakeup) {
5050 event->pending_wakeup = 0;
5051 perf_event_wakeup(event);
5052 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01005053
5054 if (rctx >= 0)
5055 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005056}
5057
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005058/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005059 * We assume there is only KVM supporting the callbacks.
5060 * Later on, we might change it to a list if there is
5061 * another virtualization implementation supporting the callbacks.
5062 */
5063struct perf_guest_info_callbacks *perf_guest_cbs;
5064
5065int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5066{
5067 perf_guest_cbs = cbs;
5068 return 0;
5069}
5070EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5071
5072int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5073{
5074 perf_guest_cbs = NULL;
5075 return 0;
5076}
5077EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5078
Jiri Olsa40189942012-08-07 15:20:37 +02005079static void
5080perf_output_sample_regs(struct perf_output_handle *handle,
5081 struct pt_regs *regs, u64 mask)
5082{
5083 int bit;
5084
5085 for_each_set_bit(bit, (const unsigned long *) &mask,
5086 sizeof(mask) * BITS_PER_BYTE) {
5087 u64 val;
5088
5089 val = perf_reg_value(regs, bit);
5090 perf_output_put(handle, val);
5091 }
5092}
5093
Stephane Eranian60e23642014-09-24 13:48:37 +02005094static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005095 struct pt_regs *regs,
5096 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02005097{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005098 if (user_mode(regs)) {
5099 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02005100 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005101 } else if (current->mm) {
5102 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005103 } else {
5104 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5105 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005106 }
5107}
5108
Stephane Eranian60e23642014-09-24 13:48:37 +02005109static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5110 struct pt_regs *regs)
5111{
5112 regs_intr->regs = regs;
5113 regs_intr->abi = perf_reg_abi(current);
5114}
5115
5116
Jiri Olsac5ebced2012-08-07 15:20:40 +02005117/*
5118 * Get remaining task size from user stack pointer.
5119 *
5120 * It'd be better to take stack vma map and limit this more
5121 * precisly, but there's no way to get it safely under interrupt,
5122 * so using TASK_SIZE as limit.
5123 */
5124static u64 perf_ustack_task_size(struct pt_regs *regs)
5125{
5126 unsigned long addr = perf_user_stack_pointer(regs);
5127
5128 if (!addr || addr >= TASK_SIZE)
5129 return 0;
5130
5131 return TASK_SIZE - addr;
5132}
5133
5134static u16
5135perf_sample_ustack_size(u16 stack_size, u16 header_size,
5136 struct pt_regs *regs)
5137{
5138 u64 task_size;
5139
5140 /* No regs, no stack pointer, no dump. */
5141 if (!regs)
5142 return 0;
5143
5144 /*
5145 * Check if we fit in with the requested stack size into the:
5146 * - TASK_SIZE
5147 * If we don't, we limit the size to the TASK_SIZE.
5148 *
5149 * - remaining sample size
5150 * If we don't, we customize the stack size to
5151 * fit in to the remaining sample size.
5152 */
5153
5154 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5155 stack_size = min(stack_size, (u16) task_size);
5156
5157 /* Current header size plus static size and dynamic size. */
5158 header_size += 2 * sizeof(u64);
5159
5160 /* Do we fit in with the current stack dump size? */
5161 if ((u16) (header_size + stack_size) < header_size) {
5162 /*
5163 * If we overflow the maximum size for the sample,
5164 * we customize the stack dump size to fit in.
5165 */
5166 stack_size = USHRT_MAX - header_size - sizeof(u64);
5167 stack_size = round_up(stack_size, sizeof(u64));
5168 }
5169
5170 return stack_size;
5171}
5172
5173static void
5174perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5175 struct pt_regs *regs)
5176{
5177 /* Case of a kernel thread, nothing to dump */
5178 if (!regs) {
5179 u64 size = 0;
5180 perf_output_put(handle, size);
5181 } else {
5182 unsigned long sp;
5183 unsigned int rem;
5184 u64 dyn_size;
5185
5186 /*
5187 * We dump:
5188 * static size
5189 * - the size requested by user or the best one we can fit
5190 * in to the sample max size
5191 * data
5192 * - user stack dump data
5193 * dynamic size
5194 * - the actual dumped size
5195 */
5196
5197 /* Static size. */
5198 perf_output_put(handle, dump_size);
5199
5200 /* Data. */
5201 sp = perf_user_stack_pointer(regs);
5202 rem = __output_copy_user(handle, (void *) sp, dump_size);
5203 dyn_size = dump_size - rem;
5204
5205 perf_output_skip(handle, rem);
5206
5207 /* Dynamic size. */
5208 perf_output_put(handle, dyn_size);
5209 }
5210}
5211
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005212static void __perf_event_header__init_id(struct perf_event_header *header,
5213 struct perf_sample_data *data,
5214 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005215{
5216 u64 sample_type = event->attr.sample_type;
5217
5218 data->type = sample_type;
5219 header->size += event->id_header_size;
5220
5221 if (sample_type & PERF_SAMPLE_TID) {
5222 /* namespace issues */
5223 data->tid_entry.pid = perf_event_pid(event, current);
5224 data->tid_entry.tid = perf_event_tid(event, current);
5225 }
5226
5227 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005228 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005229
Adrian Hunterff3d5272013-08-27 11:23:07 +03005230 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005231 data->id = primary_event_id(event);
5232
5233 if (sample_type & PERF_SAMPLE_STREAM_ID)
5234 data->stream_id = event->id;
5235
5236 if (sample_type & PERF_SAMPLE_CPU) {
5237 data->cpu_entry.cpu = raw_smp_processor_id();
5238 data->cpu_entry.reserved = 0;
5239 }
5240}
5241
Frederic Weisbecker76369132011-05-19 19:55:04 +02005242void perf_event_header__init_id(struct perf_event_header *header,
5243 struct perf_sample_data *data,
5244 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005245{
5246 if (event->attr.sample_id_all)
5247 __perf_event_header__init_id(header, data, event);
5248}
5249
5250static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5251 struct perf_sample_data *data)
5252{
5253 u64 sample_type = data->type;
5254
5255 if (sample_type & PERF_SAMPLE_TID)
5256 perf_output_put(handle, data->tid_entry);
5257
5258 if (sample_type & PERF_SAMPLE_TIME)
5259 perf_output_put(handle, data->time);
5260
5261 if (sample_type & PERF_SAMPLE_ID)
5262 perf_output_put(handle, data->id);
5263
5264 if (sample_type & PERF_SAMPLE_STREAM_ID)
5265 perf_output_put(handle, data->stream_id);
5266
5267 if (sample_type & PERF_SAMPLE_CPU)
5268 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005269
5270 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5271 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005272}
5273
Frederic Weisbecker76369132011-05-19 19:55:04 +02005274void perf_event__output_id_sample(struct perf_event *event,
5275 struct perf_output_handle *handle,
5276 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005277{
5278 if (event->attr.sample_id_all)
5279 __perf_event__output_id_sample(handle, sample);
5280}
5281
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005282static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005283 struct perf_event *event,
5284 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005285{
5286 u64 read_format = event->attr.read_format;
5287 u64 values[4];
5288 int n = 0;
5289
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005290 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005291 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005292 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005293 atomic64_read(&event->child_total_time_enabled);
5294 }
5295 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005296 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005297 atomic64_read(&event->child_total_time_running);
5298 }
5299 if (read_format & PERF_FORMAT_ID)
5300 values[n++] = primary_event_id(event);
5301
Frederic Weisbecker76369132011-05-19 19:55:04 +02005302 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005303}
5304
5305/*
5306 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
5307 */
5308static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005309 struct perf_event *event,
5310 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005311{
5312 struct perf_event *leader = event->group_leader, *sub;
5313 u64 read_format = event->attr.read_format;
5314 u64 values[5];
5315 int n = 0;
5316
5317 values[n++] = 1 + leader->nr_siblings;
5318
5319 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005320 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005321
5322 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005323 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005324
5325 if (leader != event)
5326 leader->pmu->read(leader);
5327
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005328 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005329 if (read_format & PERF_FORMAT_ID)
5330 values[n++] = primary_event_id(leader);
5331
Frederic Weisbecker76369132011-05-19 19:55:04 +02005332 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005333
5334 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5335 n = 0;
5336
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005337 if ((sub != event) &&
5338 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005339 sub->pmu->read(sub);
5340
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005341 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005342 if (read_format & PERF_FORMAT_ID)
5343 values[n++] = primary_event_id(sub);
5344
Frederic Weisbecker76369132011-05-19 19:55:04 +02005345 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005346 }
5347}
5348
Stephane Eranianeed01522010-10-26 16:08:01 +02005349#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5350 PERF_FORMAT_TOTAL_TIME_RUNNING)
5351
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005352static void perf_output_read(struct perf_output_handle *handle,
5353 struct perf_event *event)
5354{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005355 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005356 u64 read_format = event->attr.read_format;
5357
5358 /*
5359 * compute total_time_enabled, total_time_running
5360 * based on snapshot values taken when the event
5361 * was last scheduled in.
5362 *
5363 * we cannot simply called update_context_time()
5364 * because of locking issue as we are called in
5365 * NMI context
5366 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005367 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005368 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005369
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005370 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005371 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005372 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005373 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005374}
5375
5376void perf_output_sample(struct perf_output_handle *handle,
5377 struct perf_event_header *header,
5378 struct perf_sample_data *data,
5379 struct perf_event *event)
5380{
5381 u64 sample_type = data->type;
5382
5383 perf_output_put(handle, *header);
5384
Adrian Hunterff3d5272013-08-27 11:23:07 +03005385 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5386 perf_output_put(handle, data->id);
5387
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005388 if (sample_type & PERF_SAMPLE_IP)
5389 perf_output_put(handle, data->ip);
5390
5391 if (sample_type & PERF_SAMPLE_TID)
5392 perf_output_put(handle, data->tid_entry);
5393
5394 if (sample_type & PERF_SAMPLE_TIME)
5395 perf_output_put(handle, data->time);
5396
5397 if (sample_type & PERF_SAMPLE_ADDR)
5398 perf_output_put(handle, data->addr);
5399
5400 if (sample_type & PERF_SAMPLE_ID)
5401 perf_output_put(handle, data->id);
5402
5403 if (sample_type & PERF_SAMPLE_STREAM_ID)
5404 perf_output_put(handle, data->stream_id);
5405
5406 if (sample_type & PERF_SAMPLE_CPU)
5407 perf_output_put(handle, data->cpu_entry);
5408
5409 if (sample_type & PERF_SAMPLE_PERIOD)
5410 perf_output_put(handle, data->period);
5411
5412 if (sample_type & PERF_SAMPLE_READ)
5413 perf_output_read(handle, event);
5414
5415 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5416 if (data->callchain) {
5417 int size = 1;
5418
5419 if (data->callchain)
5420 size += data->callchain->nr;
5421
5422 size *= sizeof(u64);
5423
Frederic Weisbecker76369132011-05-19 19:55:04 +02005424 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005425 } else {
5426 u64 nr = 0;
5427 perf_output_put(handle, nr);
5428 }
5429 }
5430
5431 if (sample_type & PERF_SAMPLE_RAW) {
5432 if (data->raw) {
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005433 u32 raw_size = data->raw->size;
5434 u32 real_size = round_up(raw_size + sizeof(u32),
5435 sizeof(u64)) - sizeof(u32);
5436 u64 zero = 0;
5437
5438 perf_output_put(handle, real_size);
5439 __output_copy(handle, data->raw->data, raw_size);
5440 if (real_size - raw_size)
5441 __output_copy(handle, &zero, real_size - raw_size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005442 } else {
5443 struct {
5444 u32 size;
5445 u32 data;
5446 } raw = {
5447 .size = sizeof(u32),
5448 .data = 0,
5449 };
5450 perf_output_put(handle, raw);
5451 }
5452 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005453
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005454 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5455 if (data->br_stack) {
5456 size_t size;
5457
5458 size = data->br_stack->nr
5459 * sizeof(struct perf_branch_entry);
5460
5461 perf_output_put(handle, data->br_stack->nr);
5462 perf_output_copy(handle, data->br_stack->entries, size);
5463 } else {
5464 /*
5465 * we always store at least the value of nr
5466 */
5467 u64 nr = 0;
5468 perf_output_put(handle, nr);
5469 }
5470 }
Jiri Olsa40189942012-08-07 15:20:37 +02005471
5472 if (sample_type & PERF_SAMPLE_REGS_USER) {
5473 u64 abi = data->regs_user.abi;
5474
5475 /*
5476 * If there are no regs to dump, notice it through
5477 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5478 */
5479 perf_output_put(handle, abi);
5480
5481 if (abi) {
5482 u64 mask = event->attr.sample_regs_user;
5483 perf_output_sample_regs(handle,
5484 data->regs_user.regs,
5485 mask);
5486 }
5487 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005488
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005489 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005490 perf_output_sample_ustack(handle,
5491 data->stack_user_size,
5492 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005493 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005494
5495 if (sample_type & PERF_SAMPLE_WEIGHT)
5496 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005497
5498 if (sample_type & PERF_SAMPLE_DATA_SRC)
5499 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005500
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005501 if (sample_type & PERF_SAMPLE_TRANSACTION)
5502 perf_output_put(handle, data->txn);
5503
Stephane Eranian60e23642014-09-24 13:48:37 +02005504 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5505 u64 abi = data->regs_intr.abi;
5506 /*
5507 * If there are no regs to dump, notice it through
5508 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5509 */
5510 perf_output_put(handle, abi);
5511
5512 if (abi) {
5513 u64 mask = event->attr.sample_regs_intr;
5514
5515 perf_output_sample_regs(handle,
5516 data->regs_intr.regs,
5517 mask);
5518 }
5519 }
5520
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005521 if (!event->attr.watermark) {
5522 int wakeup_events = event->attr.wakeup_events;
5523
5524 if (wakeup_events) {
5525 struct ring_buffer *rb = handle->rb;
5526 int events = local_inc_return(&rb->events);
5527
5528 if (events >= wakeup_events) {
5529 local_sub(wakeup_events, &rb->events);
5530 local_inc(&rb->wakeup);
5531 }
5532 }
5533 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005534}
5535
5536void perf_prepare_sample(struct perf_event_header *header,
5537 struct perf_sample_data *data,
5538 struct perf_event *event,
5539 struct pt_regs *regs)
5540{
5541 u64 sample_type = event->attr.sample_type;
5542
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005543 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005544 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005545
5546 header->misc = 0;
5547 header->misc |= perf_misc_flags(regs);
5548
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005549 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005550
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005551 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005552 data->ip = perf_instruction_pointer(regs);
5553
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005554 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5555 int size = 1;
5556
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005557 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005558
5559 if (data->callchain)
5560 size += data->callchain->nr;
5561
5562 header->size += size * sizeof(u64);
5563 }
5564
5565 if (sample_type & PERF_SAMPLE_RAW) {
5566 int size = sizeof(u32);
5567
5568 if (data->raw)
5569 size += data->raw->size;
5570 else
5571 size += sizeof(u32);
5572
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005573 header->size += round_up(size, sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005574 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005575
5576 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5577 int size = sizeof(u64); /* nr */
5578 if (data->br_stack) {
5579 size += data->br_stack->nr
5580 * sizeof(struct perf_branch_entry);
5581 }
5582 header->size += size;
5583 }
Jiri Olsa40189942012-08-07 15:20:37 +02005584
Peter Zijlstra25657112014-09-24 13:48:42 +02005585 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005586 perf_sample_regs_user(&data->regs_user, regs,
5587 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005588
Jiri Olsa40189942012-08-07 15:20:37 +02005589 if (sample_type & PERF_SAMPLE_REGS_USER) {
5590 /* regs dump ABI info */
5591 int size = sizeof(u64);
5592
Jiri Olsa40189942012-08-07 15:20:37 +02005593 if (data->regs_user.regs) {
5594 u64 mask = event->attr.sample_regs_user;
5595 size += hweight64(mask) * sizeof(u64);
5596 }
5597
5598 header->size += size;
5599 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005600
5601 if (sample_type & PERF_SAMPLE_STACK_USER) {
5602 /*
5603 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5604 * processed as the last one or have additional check added
5605 * in case new sample type is added, because we could eat
5606 * up the rest of the sample size.
5607 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005608 u16 stack_size = event->attr.sample_stack_user;
5609 u16 size = sizeof(u64);
5610
Jiri Olsac5ebced2012-08-07 15:20:40 +02005611 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005612 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005613
5614 /*
5615 * If there is something to dump, add space for the dump
5616 * itself and for the field that tells the dynamic size,
5617 * which is how many have been actually dumped.
5618 */
5619 if (stack_size)
5620 size += sizeof(u64) + stack_size;
5621
5622 data->stack_user_size = stack_size;
5623 header->size += size;
5624 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005625
5626 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5627 /* regs dump ABI info */
5628 int size = sizeof(u64);
5629
5630 perf_sample_regs_intr(&data->regs_intr, regs);
5631
5632 if (data->regs_intr.regs) {
5633 u64 mask = event->attr.sample_regs_intr;
5634
5635 size += hweight64(mask) * sizeof(u64);
5636 }
5637
5638 header->size += size;
5639 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005640}
5641
Yan, Zheng21509082015-05-06 15:33:49 -04005642void perf_event_output(struct perf_event *event,
5643 struct perf_sample_data *data,
5644 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005645{
5646 struct perf_output_handle handle;
5647 struct perf_event_header header;
5648
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005649 /* protect the callchain buffers */
5650 rcu_read_lock();
5651
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005652 perf_prepare_sample(&header, data, event, regs);
5653
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005654 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005655 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005656
5657 perf_output_sample(&handle, &header, data, event);
5658
5659 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005660
5661exit:
5662 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005663}
5664
5665/*
5666 * read event_id
5667 */
5668
5669struct perf_read_event {
5670 struct perf_event_header header;
5671
5672 u32 pid;
5673 u32 tid;
5674};
5675
5676static void
5677perf_event_read_event(struct perf_event *event,
5678 struct task_struct *task)
5679{
5680 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005681 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005682 struct perf_read_event read_event = {
5683 .header = {
5684 .type = PERF_RECORD_READ,
5685 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005686 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005687 },
5688 .pid = perf_event_pid(event, task),
5689 .tid = perf_event_tid(event, task),
5690 };
5691 int ret;
5692
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005693 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005694 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005695 if (ret)
5696 return;
5697
5698 perf_output_put(&handle, read_event);
5699 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005700 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005701
5702 perf_output_end(&handle);
5703}
5704
Jiri Olsa52d857a2013-05-06 18:27:18 +02005705typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5706
5707static void
5708perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005709 perf_event_aux_output_cb output,
5710 void *data)
5711{
5712 struct perf_event *event;
5713
5714 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5715 if (event->state < PERF_EVENT_STATE_INACTIVE)
5716 continue;
5717 if (!event_filter_match(event))
5718 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02005719 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005720 }
5721}
5722
5723static void
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005724perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data,
5725 struct perf_event_context *task_ctx)
5726{
5727 rcu_read_lock();
5728 preempt_disable();
5729 perf_event_aux_ctx(task_ctx, output, data);
5730 preempt_enable();
5731 rcu_read_unlock();
5732}
5733
5734static void
Jiri Olsa67516842013-07-09 18:56:31 +02005735perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005736 struct perf_event_context *task_ctx)
5737{
5738 struct perf_cpu_context *cpuctx;
5739 struct perf_event_context *ctx;
5740 struct pmu *pmu;
5741 int ctxn;
5742
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005743 /*
5744 * If we have task_ctx != NULL we only notify
5745 * the task context itself. The task_ctx is set
5746 * only for EXIT events before releasing task
5747 * context.
5748 */
5749 if (task_ctx) {
5750 perf_event_aux_task_ctx(output, data, task_ctx);
5751 return;
5752 }
5753
Jiri Olsa52d857a2013-05-06 18:27:18 +02005754 rcu_read_lock();
5755 list_for_each_entry_rcu(pmu, &pmus, entry) {
5756 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5757 if (cpuctx->unique_pmu != pmu)
5758 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02005759 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005760 ctxn = pmu->task_ctx_nr;
5761 if (ctxn < 0)
5762 goto next;
5763 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5764 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02005765 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005766next:
5767 put_cpu_ptr(pmu->pmu_cpu_context);
5768 }
Jiri Olsa52d857a2013-05-06 18:27:18 +02005769 rcu_read_unlock();
5770}
5771
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005772/*
5773 * task tracking -- fork/exit
5774 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02005775 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005776 */
5777
5778struct perf_task_event {
5779 struct task_struct *task;
5780 struct perf_event_context *task_ctx;
5781
5782 struct {
5783 struct perf_event_header header;
5784
5785 u32 pid;
5786 u32 ppid;
5787 u32 tid;
5788 u32 ptid;
5789 u64 time;
5790 } event_id;
5791};
5792
Jiri Olsa67516842013-07-09 18:56:31 +02005793static int perf_event_task_match(struct perf_event *event)
5794{
Stephane Eranian13d7a242013-08-21 12:10:24 +02005795 return event->attr.comm || event->attr.mmap ||
5796 event->attr.mmap2 || event->attr.mmap_data ||
5797 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02005798}
5799
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005800static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005801 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005802{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005803 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005804 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005805 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005806 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005807 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01005808
Jiri Olsa67516842013-07-09 18:56:31 +02005809 if (!perf_event_task_match(event))
5810 return;
5811
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005812 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005813
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005814 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005815 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02005816 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005817 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005818
5819 task_event->event_id.pid = perf_event_pid(event, task);
5820 task_event->event_id.ppid = perf_event_pid(event, current);
5821
5822 task_event->event_id.tid = perf_event_tid(event, task);
5823 task_event->event_id.ptid = perf_event_tid(event, current);
5824
Peter Zijlstra34f43922015-02-20 14:05:38 +01005825 task_event->event_id.time = perf_event_clock(event);
5826
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005827 perf_output_put(&handle, task_event->event_id);
5828
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005829 perf_event__output_id_sample(event, &handle, &sample);
5830
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005831 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005832out:
5833 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005834}
5835
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005836static void perf_event_task(struct task_struct *task,
5837 struct perf_event_context *task_ctx,
5838 int new)
5839{
5840 struct perf_task_event task_event;
5841
5842 if (!atomic_read(&nr_comm_events) &&
5843 !atomic_read(&nr_mmap_events) &&
5844 !atomic_read(&nr_task_events))
5845 return;
5846
5847 task_event = (struct perf_task_event){
5848 .task = task,
5849 .task_ctx = task_ctx,
5850 .event_id = {
5851 .header = {
5852 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5853 .misc = 0,
5854 .size = sizeof(task_event.event_id),
5855 },
5856 /* .pid */
5857 /* .ppid */
5858 /* .tid */
5859 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01005860 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005861 },
5862 };
5863
Jiri Olsa67516842013-07-09 18:56:31 +02005864 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005865 &task_event,
5866 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005867}
5868
5869void perf_event_fork(struct task_struct *task)
5870{
5871 perf_event_task(task, NULL, 1);
5872}
5873
5874/*
5875 * comm tracking
5876 */
5877
5878struct perf_comm_event {
5879 struct task_struct *task;
5880 char *comm;
5881 int comm_size;
5882
5883 struct {
5884 struct perf_event_header header;
5885
5886 u32 pid;
5887 u32 tid;
5888 } event_id;
5889};
5890
Jiri Olsa67516842013-07-09 18:56:31 +02005891static int perf_event_comm_match(struct perf_event *event)
5892{
5893 return event->attr.comm;
5894}
5895
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005896static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005897 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005898{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005899 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005900 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005901 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005902 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005903 int ret;
5904
Jiri Olsa67516842013-07-09 18:56:31 +02005905 if (!perf_event_comm_match(event))
5906 return;
5907
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005908 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5909 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005910 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005911
5912 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005913 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005914
5915 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5916 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5917
5918 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005919 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005920 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005921
5922 perf_event__output_id_sample(event, &handle, &sample);
5923
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005924 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005925out:
5926 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005927}
5928
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005929static void perf_event_comm_event(struct perf_comm_event *comm_event)
5930{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005931 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005932 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005933
5934 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005935 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005936 size = ALIGN(strlen(comm)+1, sizeof(u64));
5937
5938 comm_event->comm = comm;
5939 comm_event->comm_size = size;
5940
5941 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005942
Jiri Olsa67516842013-07-09 18:56:31 +02005943 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005944 comm_event,
5945 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005946}
5947
Adrian Hunter82b89772014-05-28 11:45:04 +03005948void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005949{
5950 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005951
5952 if (!atomic_read(&nr_comm_events))
5953 return;
5954
5955 comm_event = (struct perf_comm_event){
5956 .task = task,
5957 /* .comm */
5958 /* .comm_size */
5959 .event_id = {
5960 .header = {
5961 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005962 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005963 /* .size */
5964 },
5965 /* .pid */
5966 /* .tid */
5967 },
5968 };
5969
5970 perf_event_comm_event(&comm_event);
5971}
5972
5973/*
5974 * mmap tracking
5975 */
5976
5977struct perf_mmap_event {
5978 struct vm_area_struct *vma;
5979
5980 const char *file_name;
5981 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005982 int maj, min;
5983 u64 ino;
5984 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005985 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005986
5987 struct {
5988 struct perf_event_header header;
5989
5990 u32 pid;
5991 u32 tid;
5992 u64 start;
5993 u64 len;
5994 u64 pgoff;
5995 } event_id;
5996};
5997
Jiri Olsa67516842013-07-09 18:56:31 +02005998static int perf_event_mmap_match(struct perf_event *event,
5999 void *data)
6000{
6001 struct perf_mmap_event *mmap_event = data;
6002 struct vm_area_struct *vma = mmap_event->vma;
6003 int executable = vma->vm_flags & VM_EXEC;
6004
6005 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02006006 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02006007}
6008
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006009static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006010 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006011{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006012 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006013 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006014 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006015 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006016 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006017
Jiri Olsa67516842013-07-09 18:56:31 +02006018 if (!perf_event_mmap_match(event, data))
6019 return;
6020
Stephane Eranian13d7a242013-08-21 12:10:24 +02006021 if (event->attr.mmap2) {
6022 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
6023 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
6024 mmap_event->event_id.header.size += sizeof(mmap_event->min);
6025 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03006026 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006027 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
6028 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006029 }
6030
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006031 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
6032 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006033 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006034 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006035 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006036
6037 mmap_event->event_id.pid = perf_event_pid(event, current);
6038 mmap_event->event_id.tid = perf_event_tid(event, current);
6039
6040 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006041
6042 if (event->attr.mmap2) {
6043 perf_output_put(&handle, mmap_event->maj);
6044 perf_output_put(&handle, mmap_event->min);
6045 perf_output_put(&handle, mmap_event->ino);
6046 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006047 perf_output_put(&handle, mmap_event->prot);
6048 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006049 }
6050
Frederic Weisbecker76369132011-05-19 19:55:04 +02006051 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006052 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006053
6054 perf_event__output_id_sample(event, &handle, &sample);
6055
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006056 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006057out:
6058 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006059}
6060
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006061static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
6062{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006063 struct vm_area_struct *vma = mmap_event->vma;
6064 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006065 int maj = 0, min = 0;
6066 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006067 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006068 unsigned int size;
6069 char tmp[16];
6070 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006071 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006072
6073 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02006074 struct inode *inode;
6075 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006076
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006077 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006078 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006079 name = "//enomem";
6080 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006081 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006082 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006083 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006084 * need to add enough zero bytes after the string to handle
6085 * the 64bit alignment we do later.
6086 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02006087 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006088 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006089 name = "//toolong";
6090 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006091 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02006092 inode = file_inode(vma->vm_file);
6093 dev = inode->i_sb->s_dev;
6094 ino = inode->i_ino;
6095 gen = inode->i_generation;
6096 maj = MAJOR(dev);
6097 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006098
6099 if (vma->vm_flags & VM_READ)
6100 prot |= PROT_READ;
6101 if (vma->vm_flags & VM_WRITE)
6102 prot |= PROT_WRITE;
6103 if (vma->vm_flags & VM_EXEC)
6104 prot |= PROT_EXEC;
6105
6106 if (vma->vm_flags & VM_MAYSHARE)
6107 flags = MAP_SHARED;
6108 else
6109 flags = MAP_PRIVATE;
6110
6111 if (vma->vm_flags & VM_DENYWRITE)
6112 flags |= MAP_DENYWRITE;
6113 if (vma->vm_flags & VM_MAYEXEC)
6114 flags |= MAP_EXECUTABLE;
6115 if (vma->vm_flags & VM_LOCKED)
6116 flags |= MAP_LOCKED;
6117 if (vma->vm_flags & VM_HUGETLB)
6118 flags |= MAP_HUGETLB;
6119
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006120 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006121 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006122 if (vma->vm_ops && vma->vm_ops->name) {
6123 name = (char *) vma->vm_ops->name(vma);
6124 if (name)
6125 goto cpy_name;
6126 }
6127
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006128 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006129 if (name)
6130 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006131
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006132 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006133 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006134 name = "[heap]";
6135 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006136 }
6137 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006138 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006139 name = "[stack]";
6140 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006141 }
6142
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006143 name = "//anon";
6144 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006145 }
6146
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006147cpy_name:
6148 strlcpy(tmp, name, sizeof(tmp));
6149 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006150got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006151 /*
6152 * Since our buffer works in 8 byte units we need to align our string
6153 * size to a multiple of 8. However, we must guarantee the tail end is
6154 * zero'd out to avoid leaking random bits to userspace.
6155 */
6156 size = strlen(name)+1;
6157 while (!IS_ALIGNED(size, sizeof(u64)))
6158 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006159
6160 mmap_event->file_name = name;
6161 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006162 mmap_event->maj = maj;
6163 mmap_event->min = min;
6164 mmap_event->ino = ino;
6165 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006166 mmap_event->prot = prot;
6167 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006168
Stephane Eranian2fe85422013-01-24 16:10:39 +01006169 if (!(vma->vm_flags & VM_EXEC))
6170 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6171
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006172 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6173
Jiri Olsa67516842013-07-09 18:56:31 +02006174 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006175 mmap_event,
6176 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006177
6178 kfree(buf);
6179}
6180
Eric B Munson3af9e852010-05-18 15:30:49 +01006181void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006182{
6183 struct perf_mmap_event mmap_event;
6184
6185 if (!atomic_read(&nr_mmap_events))
6186 return;
6187
6188 mmap_event = (struct perf_mmap_event){
6189 .vma = vma,
6190 /* .file_name */
6191 /* .file_size */
6192 .event_id = {
6193 .header = {
6194 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006195 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006196 /* .size */
6197 },
6198 /* .pid */
6199 /* .tid */
6200 .start = vma->vm_start,
6201 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006202 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006203 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006204 /* .maj (attr_mmap2 only) */
6205 /* .min (attr_mmap2 only) */
6206 /* .ino (attr_mmap2 only) */
6207 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006208 /* .prot (attr_mmap2 only) */
6209 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006210 };
6211
6212 perf_event_mmap_event(&mmap_event);
6213}
6214
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006215void perf_event_aux_event(struct perf_event *event, unsigned long head,
6216 unsigned long size, u64 flags)
6217{
6218 struct perf_output_handle handle;
6219 struct perf_sample_data sample;
6220 struct perf_aux_event {
6221 struct perf_event_header header;
6222 u64 offset;
6223 u64 size;
6224 u64 flags;
6225 } rec = {
6226 .header = {
6227 .type = PERF_RECORD_AUX,
6228 .misc = 0,
6229 .size = sizeof(rec),
6230 },
6231 .offset = head,
6232 .size = size,
6233 .flags = flags,
6234 };
6235 int ret;
6236
6237 perf_event_header__init_id(&rec.header, &sample, event);
6238 ret = perf_output_begin(&handle, event, rec.header.size);
6239
6240 if (ret)
6241 return;
6242
6243 perf_output_put(&handle, rec);
6244 perf_event__output_id_sample(event, &handle, &sample);
6245
6246 perf_output_end(&handle);
6247}
6248
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006249/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006250 * Lost/dropped samples logging
6251 */
6252void perf_log_lost_samples(struct perf_event *event, u64 lost)
6253{
6254 struct perf_output_handle handle;
6255 struct perf_sample_data sample;
6256 int ret;
6257
6258 struct {
6259 struct perf_event_header header;
6260 u64 lost;
6261 } lost_samples_event = {
6262 .header = {
6263 .type = PERF_RECORD_LOST_SAMPLES,
6264 .misc = 0,
6265 .size = sizeof(lost_samples_event),
6266 },
6267 .lost = lost,
6268 };
6269
6270 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6271
6272 ret = perf_output_begin(&handle, event,
6273 lost_samples_event.header.size);
6274 if (ret)
6275 return;
6276
6277 perf_output_put(&handle, lost_samples_event);
6278 perf_event__output_id_sample(event, &handle, &sample);
6279 perf_output_end(&handle);
6280}
6281
6282/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006283 * context_switch tracking
6284 */
6285
6286struct perf_switch_event {
6287 struct task_struct *task;
6288 struct task_struct *next_prev;
6289
6290 struct {
6291 struct perf_event_header header;
6292 u32 next_prev_pid;
6293 u32 next_prev_tid;
6294 } event_id;
6295};
6296
6297static int perf_event_switch_match(struct perf_event *event)
6298{
6299 return event->attr.context_switch;
6300}
6301
6302static void perf_event_switch_output(struct perf_event *event, void *data)
6303{
6304 struct perf_switch_event *se = data;
6305 struct perf_output_handle handle;
6306 struct perf_sample_data sample;
6307 int ret;
6308
6309 if (!perf_event_switch_match(event))
6310 return;
6311
6312 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6313 if (event->ctx->task) {
6314 se->event_id.header.type = PERF_RECORD_SWITCH;
6315 se->event_id.header.size = sizeof(se->event_id.header);
6316 } else {
6317 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6318 se->event_id.header.size = sizeof(se->event_id);
6319 se->event_id.next_prev_pid =
6320 perf_event_pid(event, se->next_prev);
6321 se->event_id.next_prev_tid =
6322 perf_event_tid(event, se->next_prev);
6323 }
6324
6325 perf_event_header__init_id(&se->event_id.header, &sample, event);
6326
6327 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6328 if (ret)
6329 return;
6330
6331 if (event->ctx->task)
6332 perf_output_put(&handle, se->event_id.header);
6333 else
6334 perf_output_put(&handle, se->event_id);
6335
6336 perf_event__output_id_sample(event, &handle, &sample);
6337
6338 perf_output_end(&handle);
6339}
6340
6341static void perf_event_switch(struct task_struct *task,
6342 struct task_struct *next_prev, bool sched_in)
6343{
6344 struct perf_switch_event switch_event;
6345
6346 /* N.B. caller checks nr_switch_events != 0 */
6347
6348 switch_event = (struct perf_switch_event){
6349 .task = task,
6350 .next_prev = next_prev,
6351 .event_id = {
6352 .header = {
6353 /* .type */
6354 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
6355 /* .size */
6356 },
6357 /* .next_prev_pid */
6358 /* .next_prev_tid */
6359 },
6360 };
6361
6362 perf_event_aux(perf_event_switch_output,
6363 &switch_event,
6364 NULL);
6365}
6366
6367/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006368 * IRQ throttle logging
6369 */
6370
6371static void perf_log_throttle(struct perf_event *event, int enable)
6372{
6373 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006374 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006375 int ret;
6376
6377 struct {
6378 struct perf_event_header header;
6379 u64 time;
6380 u64 id;
6381 u64 stream_id;
6382 } throttle_event = {
6383 .header = {
6384 .type = PERF_RECORD_THROTTLE,
6385 .misc = 0,
6386 .size = sizeof(throttle_event),
6387 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01006388 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006389 .id = primary_event_id(event),
6390 .stream_id = event->id,
6391 };
6392
6393 if (enable)
6394 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
6395
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006396 perf_event_header__init_id(&throttle_event.header, &sample, event);
6397
6398 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006399 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006400 if (ret)
6401 return;
6402
6403 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006404 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006405 perf_output_end(&handle);
6406}
6407
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006408static void perf_log_itrace_start(struct perf_event *event)
6409{
6410 struct perf_output_handle handle;
6411 struct perf_sample_data sample;
6412 struct perf_aux_event {
6413 struct perf_event_header header;
6414 u32 pid;
6415 u32 tid;
6416 } rec;
6417 int ret;
6418
6419 if (event->parent)
6420 event = event->parent;
6421
6422 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
6423 event->hw.itrace_started)
6424 return;
6425
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006426 rec.header.type = PERF_RECORD_ITRACE_START;
6427 rec.header.misc = 0;
6428 rec.header.size = sizeof(rec);
6429 rec.pid = perf_event_pid(event, current);
6430 rec.tid = perf_event_tid(event, current);
6431
6432 perf_event_header__init_id(&rec.header, &sample, event);
6433 ret = perf_output_begin(&handle, event, rec.header.size);
6434
6435 if (ret)
6436 return;
6437
6438 perf_output_put(&handle, rec);
6439 perf_event__output_id_sample(event, &handle, &sample);
6440
6441 perf_output_end(&handle);
6442}
6443
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006444/*
6445 * Generic event overflow handling, sampling.
6446 */
6447
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006448static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006449 int throttle, struct perf_sample_data *data,
6450 struct pt_regs *regs)
6451{
6452 int events = atomic_read(&event->event_limit);
6453 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006454 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006455 int ret = 0;
6456
Peter Zijlstra96398822010-11-24 18:55:29 +01006457 /*
6458 * Non-sampling counters might still use the PMI to fold short
6459 * hardware counters, ignore those.
6460 */
6461 if (unlikely(!is_sampling_event(event)))
6462 return 0;
6463
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006464 seq = __this_cpu_read(perf_throttled_seq);
6465 if (seq != hwc->interrupts_seq) {
6466 hwc->interrupts_seq = seq;
6467 hwc->interrupts = 1;
6468 } else {
6469 hwc->interrupts++;
6470 if (unlikely(throttle
6471 && hwc->interrupts >= max_samples_per_tick)) {
6472 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02006473 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01006474 hwc->interrupts = MAX_INTERRUPTS;
6475 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006476 ret = 1;
6477 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006478 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006479
6480 if (event->attr.freq) {
6481 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01006482 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006483
Peter Zijlstraabd50712010-01-26 18:50:16 +01006484 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006485
Peter Zijlstraabd50712010-01-26 18:50:16 +01006486 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01006487 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006488 }
6489
6490 /*
6491 * XXX event_limit might not quite work as expected on inherited
6492 * events
6493 */
6494
6495 event->pending_kill = POLL_IN;
6496 if (events && atomic_dec_and_test(&event->event_limit)) {
6497 ret = 1;
6498 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006499 event->pending_disable = 1;
6500 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006501 }
6502
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006503 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006504 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006505 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006506 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006507
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006508 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006509 event->pending_wakeup = 1;
6510 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02006511 }
6512
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006513 return ret;
6514}
6515
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006516int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006517 struct perf_sample_data *data,
6518 struct pt_regs *regs)
6519{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006520 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006521}
6522
6523/*
6524 * Generic software event infrastructure
6525 */
6526
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006527struct swevent_htable {
6528 struct swevent_hlist *swevent_hlist;
6529 struct mutex hlist_mutex;
6530 int hlist_refcount;
6531
6532 /* Recursion avoidance in each contexts */
6533 int recursion[PERF_NR_CONTEXTS];
6534};
6535
6536static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
6537
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006538/*
6539 * We directly increment event->count and keep a second value in
6540 * event->hw.period_left to count intervals. This period event
6541 * is kept in the range [-sample_period, 0] so that we can use the
6542 * sign as trigger.
6543 */
6544
Jiri Olsaab573842013-05-01 17:25:44 +02006545u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006546{
6547 struct hw_perf_event *hwc = &event->hw;
6548 u64 period = hwc->last_period;
6549 u64 nr, offset;
6550 s64 old, val;
6551
6552 hwc->last_period = hwc->sample_period;
6553
6554again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02006555 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006556 if (val < 0)
6557 return 0;
6558
6559 nr = div64_u64(period + val, period);
6560 offset = nr * period;
6561 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02006562 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006563 goto again;
6564
6565 return nr;
6566}
6567
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006568static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006569 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006570 struct pt_regs *regs)
6571{
6572 struct hw_perf_event *hwc = &event->hw;
6573 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006574
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006575 if (!overflow)
6576 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006577
6578 if (hwc->interrupts == MAX_INTERRUPTS)
6579 return;
6580
6581 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006582 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006583 data, regs)) {
6584 /*
6585 * We inhibit the overflow from happening when
6586 * hwc->interrupts == MAX_INTERRUPTS.
6587 */
6588 break;
6589 }
6590 throttle = 1;
6591 }
6592}
6593
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006594static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006595 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006596 struct pt_regs *regs)
6597{
6598 struct hw_perf_event *hwc = &event->hw;
6599
Peter Zijlstrae7850592010-05-21 14:43:08 +02006600 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006601
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006602 if (!regs)
6603 return;
6604
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006605 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006606 return;
6607
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03006608 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
6609 data->period = nr;
6610 return perf_swevent_overflow(event, 1, data, regs);
6611 } else
6612 data->period = event->hw.last_period;
6613
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006614 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006615 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006616
Peter Zijlstrae7850592010-05-21 14:43:08 +02006617 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006618 return;
6619
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006620 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006621}
6622
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006623static int perf_exclude_event(struct perf_event *event,
6624 struct pt_regs *regs)
6625{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006626 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01006627 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006628
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006629 if (regs) {
6630 if (event->attr.exclude_user && user_mode(regs))
6631 return 1;
6632
6633 if (event->attr.exclude_kernel && !user_mode(regs))
6634 return 1;
6635 }
6636
6637 return 0;
6638}
6639
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006640static int perf_swevent_match(struct perf_event *event,
6641 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08006642 u32 event_id,
6643 struct perf_sample_data *data,
6644 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006645{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006646 if (event->attr.type != type)
6647 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006648
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006649 if (event->attr.config != event_id)
6650 return 0;
6651
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006652 if (perf_exclude_event(event, regs))
6653 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006654
6655 return 1;
6656}
6657
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006658static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006659{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006660 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006661
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006662 return hash_64(val, SWEVENT_HLIST_BITS);
6663}
6664
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006665static inline struct hlist_head *
6666__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006667{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006668 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006669
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006670 return &hlist->heads[hash];
6671}
6672
6673/* For the read side: events when they trigger */
6674static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006675find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006676{
6677 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006678
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006679 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006680 if (!hlist)
6681 return NULL;
6682
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006683 return __find_swevent_head(hlist, type, event_id);
6684}
6685
6686/* For the event head insertion and removal in the hlist */
6687static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006688find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006689{
6690 struct swevent_hlist *hlist;
6691 u32 event_id = event->attr.config;
6692 u64 type = event->attr.type;
6693
6694 /*
6695 * Event scheduling is always serialized against hlist allocation
6696 * and release. Which makes the protected version suitable here.
6697 * The context lock guarantees that.
6698 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006699 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006700 lockdep_is_held(&event->ctx->lock));
6701 if (!hlist)
6702 return NULL;
6703
6704 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006705}
6706
6707static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006708 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006709 struct perf_sample_data *data,
6710 struct pt_regs *regs)
6711{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006712 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006713 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006714 struct hlist_head *head;
6715
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006716 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006717 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006718 if (!head)
6719 goto end;
6720
Sasha Levinb67bfe02013-02-27 17:06:00 -08006721 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08006722 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006723 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006724 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006725end:
6726 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006727}
6728
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006729DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
6730
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006731int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006732{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006733 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006734
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006735 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006736}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01006737EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006738
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01006739inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006740{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006741 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006742
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006743 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006744}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006745
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006746void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006747{
Ingo Molnara4234bf2009-11-23 10:57:59 +01006748 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006749
6750 if (WARN_ON_ONCE(!regs))
6751 return;
6752
6753 perf_sample_data_init(&data, addr, 0);
6754 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
6755}
6756
6757void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
6758{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006759 int rctx;
6760
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006761 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006762 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006763 if (unlikely(rctx < 0))
6764 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006765
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006766 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006767
6768 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006769fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006770 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006771}
6772
6773static void perf_swevent_read(struct perf_event *event)
6774{
6775}
6776
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006777static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006778{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006779 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006780 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006781 struct hlist_head *head;
6782
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006783 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006784 hwc->last_period = hwc->sample_period;
6785 perf_swevent_set_period(event);
6786 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006787
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006788 hwc->state = !(flags & PERF_EF_START);
6789
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006790 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01006791 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006792 return -EINVAL;
6793
6794 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08006795 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006796
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006797 return 0;
6798}
6799
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006800static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006801{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006802 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006803}
6804
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006805static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006806{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006807 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006808}
6809
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006810static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006811{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006812 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006813}
6814
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006815/* Deref the hlist from the update side */
6816static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006817swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006818{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006819 return rcu_dereference_protected(swhash->swevent_hlist,
6820 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006821}
6822
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006823static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006824{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006825 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006826
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006827 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006828 return;
6829
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03006830 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08006831 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006832}
6833
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006834static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006835{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006836 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006837
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006838 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006839
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006840 if (!--swhash->hlist_refcount)
6841 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006842
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006843 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006844}
6845
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006846static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006847{
6848 int cpu;
6849
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006850 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006851 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006852}
6853
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006854static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006855{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006856 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006857 int err = 0;
6858
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006859 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006860 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006861 struct swevent_hlist *hlist;
6862
6863 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
6864 if (!hlist) {
6865 err = -ENOMEM;
6866 goto exit;
6867 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006868 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006869 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006870 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006871exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006872 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006873
6874 return err;
6875}
6876
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006877static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006878{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006879 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006880
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006881 get_online_cpus();
6882 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006883 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006884 if (err) {
6885 failed_cpu = cpu;
6886 goto fail;
6887 }
6888 }
6889 put_online_cpus();
6890
6891 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006892fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006893 for_each_possible_cpu(cpu) {
6894 if (cpu == failed_cpu)
6895 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006896 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006897 }
6898
6899 put_online_cpus();
6900 return err;
6901}
6902
Ingo Molnarc5905af2012-02-24 08:31:31 +01006903struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006904
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006905static void sw_perf_event_destroy(struct perf_event *event)
6906{
6907 u64 event_id = event->attr.config;
6908
6909 WARN_ON(event->parent);
6910
Ingo Molnarc5905af2012-02-24 08:31:31 +01006911 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006912 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006913}
6914
6915static int perf_swevent_init(struct perf_event *event)
6916{
Tommi Rantala8176cce2013-04-13 22:49:14 +03006917 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006918
6919 if (event->attr.type != PERF_TYPE_SOFTWARE)
6920 return -ENOENT;
6921
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006922 /*
6923 * no branch sampling for software events
6924 */
6925 if (has_branch_stack(event))
6926 return -EOPNOTSUPP;
6927
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006928 switch (event_id) {
6929 case PERF_COUNT_SW_CPU_CLOCK:
6930 case PERF_COUNT_SW_TASK_CLOCK:
6931 return -ENOENT;
6932
6933 default:
6934 break;
6935 }
6936
Dan Carpenterce677832010-10-24 21:50:42 +02006937 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006938 return -ENOENT;
6939
6940 if (!event->parent) {
6941 int err;
6942
Thomas Gleixner3b364d72016-02-09 20:11:40 +00006943 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006944 if (err)
6945 return err;
6946
Ingo Molnarc5905af2012-02-24 08:31:31 +01006947 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006948 event->destroy = sw_perf_event_destroy;
6949 }
6950
6951 return 0;
6952}
6953
6954static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006955 .task_ctx_nr = perf_sw_context,
6956
Peter Zijlstra34f43922015-02-20 14:05:38 +01006957 .capabilities = PERF_PMU_CAP_NO_NMI,
6958
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006959 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006960 .add = perf_swevent_add,
6961 .del = perf_swevent_del,
6962 .start = perf_swevent_start,
6963 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006964 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006965};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006966
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006967#ifdef CONFIG_EVENT_TRACING
6968
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006969static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006970 struct perf_sample_data *data)
6971{
6972 void *record = data->raw->data;
6973
Peter Zijlstrab71b4372015-11-02 10:50:51 +01006974 /* only top level events have filters set */
6975 if (event->parent)
6976 event = event->parent;
6977
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006978 if (likely(!event->filter) || filter_match_preds(event->filter, record))
6979 return 1;
6980 return 0;
6981}
6982
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006983static int perf_tp_event_match(struct perf_event *event,
6984 struct perf_sample_data *data,
6985 struct pt_regs *regs)
6986{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01006987 if (event->hw.state & PERF_HES_STOPPED)
6988 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02006989 /*
6990 * All tracepoints are from kernel-space.
6991 */
6992 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006993 return 0;
6994
6995 if (!perf_tp_filter_match(event, data))
6996 return 0;
6997
6998 return 1;
6999}
7000
7001void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007002 struct pt_regs *regs, struct hlist_head *head, int rctx,
7003 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007004{
7005 struct perf_sample_data data;
7006 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007007
7008 struct perf_raw_record raw = {
7009 .size = entry_size,
7010 .data = record,
7011 };
7012
Robert Richterfd0d0002012-04-02 20:19:08 +02007013 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007014 data.raw = &raw;
7015
Sasha Levinb67bfe02013-02-27 17:06:00 -08007016 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007017 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007018 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007019 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007020
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007021 /*
7022 * If we got specified a target task, also iterate its context and
7023 * deliver this event there too.
7024 */
7025 if (task && task != current) {
7026 struct perf_event_context *ctx;
7027 struct trace_entry *entry = record;
7028
7029 rcu_read_lock();
7030 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
7031 if (!ctx)
7032 goto unlock;
7033
7034 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
7035 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7036 continue;
7037 if (event->attr.config != entry->type)
7038 continue;
7039 if (perf_tp_event_match(event, &data, regs))
7040 perf_swevent_event(event, count, &data, regs);
7041 }
7042unlock:
7043 rcu_read_unlock();
7044 }
7045
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007046 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007047}
7048EXPORT_SYMBOL_GPL(perf_tp_event);
7049
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007050static void tp_perf_event_destroy(struct perf_event *event)
7051{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007052 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007053}
7054
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007055static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007056{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007057 int err;
7058
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007059 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7060 return -ENOENT;
7061
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007062 /*
7063 * no branch sampling for tracepoint events
7064 */
7065 if (has_branch_stack(event))
7066 return -EOPNOTSUPP;
7067
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007068 err = perf_trace_init(event);
7069 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007070 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007071
7072 event->destroy = tp_perf_event_destroy;
7073
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007074 return 0;
7075}
7076
7077static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007078 .task_ctx_nr = perf_sw_context,
7079
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007080 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007081 .add = perf_trace_add,
7082 .del = perf_trace_del,
7083 .start = perf_swevent_start,
7084 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007085 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007086};
7087
7088static inline void perf_tp_register(void)
7089{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007090 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007091}
Li Zefan6fb29152009-10-15 11:21:42 +08007092
7093static int perf_event_set_filter(struct perf_event *event, void __user *arg)
7094{
7095 char *filter_str;
7096 int ret;
7097
7098 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7099 return -EINVAL;
7100
7101 filter_str = strndup_user(arg, PAGE_SIZE);
7102 if (IS_ERR(filter_str))
7103 return PTR_ERR(filter_str);
7104
7105 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
7106
7107 kfree(filter_str);
7108 return ret;
7109}
7110
7111static void perf_event_free_filter(struct perf_event *event)
7112{
7113 ftrace_profile_free_filter(event);
7114}
7115
Alexei Starovoitov25415172015-03-25 12:49:20 -07007116static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7117{
7118 struct bpf_prog *prog;
7119
7120 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7121 return -EINVAL;
7122
7123 if (event->tp_event->prog)
7124 return -EEXIST;
7125
Wang Nan04a22fa2015-07-01 02:13:50 +00007126 if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE))
7127 /* bpf programs can only be attached to u/kprobes */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007128 return -EINVAL;
7129
7130 prog = bpf_prog_get(prog_fd);
7131 if (IS_ERR(prog))
7132 return PTR_ERR(prog);
7133
Linus Torvalds6c373ca2015-04-15 09:00:47 -07007134 if (prog->type != BPF_PROG_TYPE_KPROBE) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007135 /* valid fd, but invalid bpf program type */
7136 bpf_prog_put(prog);
7137 return -EINVAL;
7138 }
7139
7140 event->tp_event->prog = prog;
7141
7142 return 0;
7143}
7144
7145static void perf_event_free_bpf_prog(struct perf_event *event)
7146{
7147 struct bpf_prog *prog;
7148
7149 if (!event->tp_event)
7150 return;
7151
7152 prog = event->tp_event->prog;
7153 if (prog) {
7154 event->tp_event->prog = NULL;
7155 bpf_prog_put(prog);
7156 }
7157}
7158
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007159#else
Li Zefan6fb29152009-10-15 11:21:42 +08007160
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007161static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007162{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007163}
Li Zefan6fb29152009-10-15 11:21:42 +08007164
7165static int perf_event_set_filter(struct perf_event *event, void __user *arg)
7166{
7167 return -ENOENT;
7168}
7169
7170static void perf_event_free_filter(struct perf_event *event)
7171{
7172}
7173
Alexei Starovoitov25415172015-03-25 12:49:20 -07007174static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7175{
7176 return -ENOENT;
7177}
7178
7179static void perf_event_free_bpf_prog(struct perf_event *event)
7180{
7181}
Li Zefan07b139c2009-12-21 14:27:35 +08007182#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007183
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007184#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007185void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007186{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007187 struct perf_sample_data sample;
7188 struct pt_regs *regs = data;
7189
Robert Richterfd0d0002012-04-02 20:19:08 +02007190 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007191
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007192 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007193 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007194}
7195#endif
7196
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007197/*
7198 * hrtimer based swevent callback
7199 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007200
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007201static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007202{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007203 enum hrtimer_restart ret = HRTIMER_RESTART;
7204 struct perf_sample_data data;
7205 struct pt_regs *regs;
7206 struct perf_event *event;
7207 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007208
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007209 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007210
7211 if (event->state != PERF_EVENT_STATE_ACTIVE)
7212 return HRTIMER_NORESTART;
7213
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007214 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007215
Robert Richterfd0d0002012-04-02 20:19:08 +02007216 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007217 regs = get_irq_regs();
7218
7219 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08007220 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02007221 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007222 ret = HRTIMER_NORESTART;
7223 }
7224
7225 period = max_t(u64, 10000, event->hw.sample_period);
7226 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
7227
7228 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007229}
7230
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007231static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007232{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007233 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007234 s64 period;
7235
7236 if (!is_sampling_event(event))
7237 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007238
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007239 period = local64_read(&hwc->period_left);
7240 if (period) {
7241 if (period < 0)
7242 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007243
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007244 local64_set(&hwc->period_left, 0);
7245 } else {
7246 period = max_t(u64, 10000, hwc->sample_period);
7247 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00007248 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
7249 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007250}
7251
7252static void perf_swevent_cancel_hrtimer(struct perf_event *event)
7253{
7254 struct hw_perf_event *hwc = &event->hw;
7255
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007256 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007257 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007258 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007259
7260 hrtimer_cancel(&hwc->hrtimer);
7261 }
7262}
7263
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007264static void perf_swevent_init_hrtimer(struct perf_event *event)
7265{
7266 struct hw_perf_event *hwc = &event->hw;
7267
7268 if (!is_sampling_event(event))
7269 return;
7270
7271 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
7272 hwc->hrtimer.function = perf_swevent_hrtimer;
7273
7274 /*
7275 * Since hrtimers have a fixed rate, we can do a static freq->period
7276 * mapping and avoid the whole period adjust feedback stuff.
7277 */
7278 if (event->attr.freq) {
7279 long freq = event->attr.sample_freq;
7280
7281 event->attr.sample_period = NSEC_PER_SEC / freq;
7282 hwc->sample_period = event->attr.sample_period;
7283 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09007284 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007285 event->attr.freq = 0;
7286 }
7287}
7288
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007289/*
7290 * Software event: cpu wall time clock
7291 */
7292
7293static void cpu_clock_event_update(struct perf_event *event)
7294{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007295 s64 prev;
7296 u64 now;
7297
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007298 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007299 prev = local64_xchg(&event->hw.prev_count, now);
7300 local64_add(now - prev, &event->count);
7301}
7302
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007303static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007304{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007305 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007306 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007307}
7308
7309static void cpu_clock_event_stop(struct perf_event *event, int flags)
7310{
7311 perf_swevent_cancel_hrtimer(event);
7312 cpu_clock_event_update(event);
7313}
7314
7315static int cpu_clock_event_add(struct perf_event *event, int flags)
7316{
7317 if (flags & PERF_EF_START)
7318 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08007319 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007320
7321 return 0;
7322}
7323
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007324static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007325{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007326 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007327}
7328
7329static void cpu_clock_event_read(struct perf_event *event)
7330{
7331 cpu_clock_event_update(event);
7332}
7333
7334static int cpu_clock_event_init(struct perf_event *event)
7335{
7336 if (event->attr.type != PERF_TYPE_SOFTWARE)
7337 return -ENOENT;
7338
7339 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
7340 return -ENOENT;
7341
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007342 /*
7343 * no branch sampling for software events
7344 */
7345 if (has_branch_stack(event))
7346 return -EOPNOTSUPP;
7347
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007348 perf_swevent_init_hrtimer(event);
7349
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007350 return 0;
7351}
7352
7353static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007354 .task_ctx_nr = perf_sw_context,
7355
Peter Zijlstra34f43922015-02-20 14:05:38 +01007356 .capabilities = PERF_PMU_CAP_NO_NMI,
7357
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007358 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007359 .add = cpu_clock_event_add,
7360 .del = cpu_clock_event_del,
7361 .start = cpu_clock_event_start,
7362 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007363 .read = cpu_clock_event_read,
7364};
7365
7366/*
7367 * Software event: task time clock
7368 */
7369
7370static void task_clock_event_update(struct perf_event *event, u64 now)
7371{
7372 u64 prev;
7373 s64 delta;
7374
7375 prev = local64_xchg(&event->hw.prev_count, now);
7376 delta = now - prev;
7377 local64_add(delta, &event->count);
7378}
7379
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007380static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007381{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007382 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007383 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007384}
7385
7386static void task_clock_event_stop(struct perf_event *event, int flags)
7387{
7388 perf_swevent_cancel_hrtimer(event);
7389 task_clock_event_update(event, event->ctx->time);
7390}
7391
7392static int task_clock_event_add(struct perf_event *event, int flags)
7393{
7394 if (flags & PERF_EF_START)
7395 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08007396 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007397
7398 return 0;
7399}
7400
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007401static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007402{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007403 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007404}
7405
7406static void task_clock_event_read(struct perf_event *event)
7407{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01007408 u64 now = perf_clock();
7409 u64 delta = now - event->ctx->timestamp;
7410 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007411
7412 task_clock_event_update(event, time);
7413}
7414
7415static int task_clock_event_init(struct perf_event *event)
7416{
7417 if (event->attr.type != PERF_TYPE_SOFTWARE)
7418 return -ENOENT;
7419
7420 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
7421 return -ENOENT;
7422
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007423 /*
7424 * no branch sampling for software events
7425 */
7426 if (has_branch_stack(event))
7427 return -EOPNOTSUPP;
7428
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007429 perf_swevent_init_hrtimer(event);
7430
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007431 return 0;
7432}
7433
7434static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007435 .task_ctx_nr = perf_sw_context,
7436
Peter Zijlstra34f43922015-02-20 14:05:38 +01007437 .capabilities = PERF_PMU_CAP_NO_NMI,
7438
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007439 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007440 .add = task_clock_event_add,
7441 .del = task_clock_event_del,
7442 .start = task_clock_event_start,
7443 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007444 .read = task_clock_event_read,
7445};
7446
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007447static void perf_pmu_nop_void(struct pmu *pmu)
7448{
7449}
7450
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007451static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
7452{
7453}
7454
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007455static int perf_pmu_nop_int(struct pmu *pmu)
7456{
7457 return 0;
7458}
7459
Geliang Tang18ab2cd2015-09-27 23:25:50 +08007460static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007461
7462static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007463{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007464 __this_cpu_write(nop_txn_flags, flags);
7465
7466 if (flags & ~PERF_PMU_TXN_ADD)
7467 return;
7468
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007469 perf_pmu_disable(pmu);
7470}
7471
7472static int perf_pmu_commit_txn(struct pmu *pmu)
7473{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007474 unsigned int flags = __this_cpu_read(nop_txn_flags);
7475
7476 __this_cpu_write(nop_txn_flags, 0);
7477
7478 if (flags & ~PERF_PMU_TXN_ADD)
7479 return 0;
7480
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007481 perf_pmu_enable(pmu);
7482 return 0;
7483}
7484
7485static void perf_pmu_cancel_txn(struct pmu *pmu)
7486{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007487 unsigned int flags = __this_cpu_read(nop_txn_flags);
7488
7489 __this_cpu_write(nop_txn_flags, 0);
7490
7491 if (flags & ~PERF_PMU_TXN_ADD)
7492 return;
7493
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007494 perf_pmu_enable(pmu);
7495}
7496
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007497static int perf_event_idx_default(struct perf_event *event)
7498{
Peter Zijlstrac719f562014-10-21 11:10:21 +02007499 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007500}
7501
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007502/*
7503 * Ensures all contexts with the same task_ctx_nr have the same
7504 * pmu_cpu_context too.
7505 */
Mark Rutland9e317042014-02-10 17:44:18 +00007506static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007507{
7508 struct pmu *pmu;
7509
7510 if (ctxn < 0)
7511 return NULL;
7512
7513 list_for_each_entry(pmu, &pmus, entry) {
7514 if (pmu->task_ctx_nr == ctxn)
7515 return pmu->pmu_cpu_context;
7516 }
7517
7518 return NULL;
7519}
7520
Peter Zijlstra51676952010-12-07 14:18:20 +01007521static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007522{
Peter Zijlstra51676952010-12-07 14:18:20 +01007523 int cpu;
7524
7525 for_each_possible_cpu(cpu) {
7526 struct perf_cpu_context *cpuctx;
7527
7528 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
7529
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007530 if (cpuctx->unique_pmu == old_pmu)
7531 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01007532 }
7533}
7534
7535static void free_pmu_context(struct pmu *pmu)
7536{
7537 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007538
7539 mutex_lock(&pmus_lock);
7540 /*
7541 * Like a real lame refcount.
7542 */
Peter Zijlstra51676952010-12-07 14:18:20 +01007543 list_for_each_entry(i, &pmus, entry) {
7544 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
7545 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007546 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01007547 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007548 }
7549
Peter Zijlstra51676952010-12-07 14:18:20 +01007550 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007551out:
7552 mutex_unlock(&pmus_lock);
7553}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007554static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007555
Peter Zijlstraabe43402010-11-17 23:17:37 +01007556static ssize_t
7557type_show(struct device *dev, struct device_attribute *attr, char *page)
7558{
7559 struct pmu *pmu = dev_get_drvdata(dev);
7560
7561 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
7562}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007563static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007564
Stephane Eranian62b85632013-04-03 14:21:34 +02007565static ssize_t
7566perf_event_mux_interval_ms_show(struct device *dev,
7567 struct device_attribute *attr,
7568 char *page)
7569{
7570 struct pmu *pmu = dev_get_drvdata(dev);
7571
7572 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
7573}
7574
Peter Zijlstra272325c2015-04-15 11:41:58 +02007575static DEFINE_MUTEX(mux_interval_mutex);
7576
Stephane Eranian62b85632013-04-03 14:21:34 +02007577static ssize_t
7578perf_event_mux_interval_ms_store(struct device *dev,
7579 struct device_attribute *attr,
7580 const char *buf, size_t count)
7581{
7582 struct pmu *pmu = dev_get_drvdata(dev);
7583 int timer, cpu, ret;
7584
7585 ret = kstrtoint(buf, 0, &timer);
7586 if (ret)
7587 return ret;
7588
7589 if (timer < 1)
7590 return -EINVAL;
7591
7592 /* same value, noting to do */
7593 if (timer == pmu->hrtimer_interval_ms)
7594 return count;
7595
Peter Zijlstra272325c2015-04-15 11:41:58 +02007596 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02007597 pmu->hrtimer_interval_ms = timer;
7598
7599 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02007600 get_online_cpus();
7601 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02007602 struct perf_cpu_context *cpuctx;
7603 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
7604 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
7605
Peter Zijlstra272325c2015-04-15 11:41:58 +02007606 cpu_function_call(cpu,
7607 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02007608 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02007609 put_online_cpus();
7610 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02007611
7612 return count;
7613}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007614static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02007615
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007616static struct attribute *pmu_dev_attrs[] = {
7617 &dev_attr_type.attr,
7618 &dev_attr_perf_event_mux_interval_ms.attr,
7619 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01007620};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007621ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007622
7623static int pmu_bus_running;
7624static struct bus_type pmu_bus = {
7625 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07007626 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01007627};
7628
7629static void pmu_dev_release(struct device *dev)
7630{
7631 kfree(dev);
7632}
7633
7634static int pmu_dev_alloc(struct pmu *pmu)
7635{
7636 int ret = -ENOMEM;
7637
7638 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
7639 if (!pmu->dev)
7640 goto out;
7641
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01007642 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01007643 device_initialize(pmu->dev);
7644 ret = dev_set_name(pmu->dev, "%s", pmu->name);
7645 if (ret)
7646 goto free_dev;
7647
7648 dev_set_drvdata(pmu->dev, pmu);
7649 pmu->dev->bus = &pmu_bus;
7650 pmu->dev->release = pmu_dev_release;
7651 ret = device_add(pmu->dev);
7652 if (ret)
7653 goto free_dev;
7654
7655out:
7656 return ret;
7657
7658free_dev:
7659 put_device(pmu->dev);
7660 goto out;
7661}
7662
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007663static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007664static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007665
Mischa Jonker03d8e802013-06-04 11:45:48 +02007666int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007667{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007668 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007669
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007670 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007671 ret = -ENOMEM;
7672 pmu->pmu_disable_count = alloc_percpu(int);
7673 if (!pmu->pmu_disable_count)
7674 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007675
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007676 pmu->type = -1;
7677 if (!name)
7678 goto skip_type;
7679 pmu->name = name;
7680
7681 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08007682 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
7683 if (type < 0) {
7684 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007685 goto free_pdc;
7686 }
7687 }
7688 pmu->type = type;
7689
Peter Zijlstraabe43402010-11-17 23:17:37 +01007690 if (pmu_bus_running) {
7691 ret = pmu_dev_alloc(pmu);
7692 if (ret)
7693 goto free_idr;
7694 }
7695
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007696skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007697 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
7698 if (pmu->pmu_cpu_context)
7699 goto got_cpu_context;
7700
Wei Yongjunc4814202013-04-12 11:05:54 +08007701 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007702 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
7703 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01007704 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007705
7706 for_each_possible_cpu(cpu) {
7707 struct perf_cpu_context *cpuctx;
7708
7709 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02007710 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007711 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007712 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007713 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02007714
Peter Zijlstra272325c2015-04-15 11:41:58 +02007715 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02007716
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007717 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007718 }
7719
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007720got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007721 if (!pmu->start_txn) {
7722 if (pmu->pmu_enable) {
7723 /*
7724 * If we have pmu_enable/pmu_disable calls, install
7725 * transaction stubs that use that to try and batch
7726 * hardware accesses.
7727 */
7728 pmu->start_txn = perf_pmu_start_txn;
7729 pmu->commit_txn = perf_pmu_commit_txn;
7730 pmu->cancel_txn = perf_pmu_cancel_txn;
7731 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07007732 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007733 pmu->commit_txn = perf_pmu_nop_int;
7734 pmu->cancel_txn = perf_pmu_nop_void;
7735 }
7736 }
7737
7738 if (!pmu->pmu_enable) {
7739 pmu->pmu_enable = perf_pmu_nop_void;
7740 pmu->pmu_disable = perf_pmu_nop_void;
7741 }
7742
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007743 if (!pmu->event_idx)
7744 pmu->event_idx = perf_event_idx_default;
7745
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007746 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02007747 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007748 ret = 0;
7749unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007750 mutex_unlock(&pmus_lock);
7751
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007752 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007753
Peter Zijlstraabe43402010-11-17 23:17:37 +01007754free_dev:
7755 device_del(pmu->dev);
7756 put_device(pmu->dev);
7757
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007758free_idr:
7759 if (pmu->type >= PERF_TYPE_MAX)
7760 idr_remove(&pmu_idr, pmu->type);
7761
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007762free_pdc:
7763 free_percpu(pmu->pmu_disable_count);
7764 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007765}
Yan, Zhengc464c762014-03-18 16:56:41 +08007766EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007767
7768void perf_pmu_unregister(struct pmu *pmu)
7769{
7770 mutex_lock(&pmus_lock);
7771 list_del_rcu(&pmu->entry);
7772 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007773
7774 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02007775 * We dereference the pmu list under both SRCU and regular RCU, so
7776 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007777 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007778 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02007779 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007780
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007781 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007782 if (pmu->type >= PERF_TYPE_MAX)
7783 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007784 device_del(pmu->dev);
7785 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01007786 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007787}
Yan, Zhengc464c762014-03-18 16:56:41 +08007788EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007789
Mark Rutlandcc34b982015-01-07 14:56:51 +00007790static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
7791{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007792 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00007793 int ret;
7794
7795 if (!try_module_get(pmu->module))
7796 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007797
7798 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02007799 /*
7800 * This ctx->mutex can nest when we're called through
7801 * inheritance. See the perf_event_ctx_lock_nested() comment.
7802 */
7803 ctx = perf_event_ctx_lock_nested(event->group_leader,
7804 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007805 BUG_ON(!ctx);
7806 }
7807
Mark Rutlandcc34b982015-01-07 14:56:51 +00007808 event->pmu = pmu;
7809 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007810
7811 if (ctx)
7812 perf_event_ctx_unlock(event->group_leader, ctx);
7813
Mark Rutlandcc34b982015-01-07 14:56:51 +00007814 if (ret)
7815 module_put(pmu->module);
7816
7817 return ret;
7818}
7819
Geliang Tang18ab2cd2015-09-27 23:25:50 +08007820static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007821{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007822 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007823 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08007824 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007825
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007826 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007827
7828 rcu_read_lock();
7829 pmu = idr_find(&pmu_idr, event->attr.type);
7830 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08007831 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007832 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08007833 if (ret)
7834 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007835 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08007836 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007837
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007838 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007839 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007840 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007841 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007842
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007843 if (ret != -ENOENT) {
7844 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007845 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007846 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007847 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007848 pmu = ERR_PTR(-ENOENT);
7849unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007850 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007851
7852 return pmu;
7853}
7854
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007855static void account_event_cpu(struct perf_event *event, int cpu)
7856{
7857 if (event->parent)
7858 return;
7859
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007860 if (is_cgroup_event(event))
7861 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
7862}
7863
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02007864/* Freq events need the tick to stay alive (see perf_event_task_tick). */
7865static void account_freq_event_nohz(void)
7866{
7867#ifdef CONFIG_NO_HZ_FULL
7868 /* Lock so we don't race with concurrent unaccount */
7869 spin_lock(&nr_freq_lock);
7870 if (atomic_inc_return(&nr_freq_events) == 1)
7871 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
7872 spin_unlock(&nr_freq_lock);
7873#endif
7874}
7875
7876static void account_freq_event(void)
7877{
7878 if (tick_nohz_full_enabled())
7879 account_freq_event_nohz();
7880 else
7881 atomic_inc(&nr_freq_events);
7882}
7883
7884
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007885static void account_event(struct perf_event *event)
7886{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007887 bool inc = false;
7888
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007889 if (event->parent)
7890 return;
7891
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007892 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007893 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007894 if (event->attr.mmap || event->attr.mmap_data)
7895 atomic_inc(&nr_mmap_events);
7896 if (event->attr.comm)
7897 atomic_inc(&nr_comm_events);
7898 if (event->attr.task)
7899 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02007900 if (event->attr.freq)
7901 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03007902 if (event->attr.context_switch) {
7903 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007904 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03007905 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007906 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007907 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007908 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01007909 inc = true;
7910
Peter Zijlstra9107c892016-02-24 18:45:45 +01007911 if (inc) {
7912 if (atomic_inc_not_zero(&perf_sched_count))
7913 goto enabled;
7914
7915 mutex_lock(&perf_sched_mutex);
7916 if (!atomic_read(&perf_sched_count)) {
7917 static_branch_enable(&perf_sched_events);
7918 /*
7919 * Guarantee that all CPUs observe they key change and
7920 * call the perf scheduling hooks before proceeding to
7921 * install events that need them.
7922 */
7923 synchronize_sched();
7924 }
7925 /*
7926 * Now that we have waited for the sync_sched(), allow further
7927 * increments to by-pass the mutex.
7928 */
7929 atomic_inc(&perf_sched_count);
7930 mutex_unlock(&perf_sched_mutex);
7931 }
7932enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007933
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007934 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007935}
7936
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007937/*
7938 * Allocate and initialize a event structure
7939 */
7940static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007941perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007942 struct task_struct *task,
7943 struct perf_event *group_leader,
7944 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007945 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00007946 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007947{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007948 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007949 struct perf_event *event;
7950 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007951 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007952
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007953 if ((unsigned)cpu >= nr_cpu_ids) {
7954 if (!task || cpu != -1)
7955 return ERR_PTR(-EINVAL);
7956 }
7957
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007958 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007959 if (!event)
7960 return ERR_PTR(-ENOMEM);
7961
7962 /*
7963 * Single events are their own group leaders, with an
7964 * empty sibling list:
7965 */
7966 if (!group_leader)
7967 group_leader = event;
7968
7969 mutex_init(&event->child_mutex);
7970 INIT_LIST_HEAD(&event->child_list);
7971
7972 INIT_LIST_HEAD(&event->group_entry);
7973 INIT_LIST_HEAD(&event->event_entry);
7974 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007975 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01007976 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01007977 INIT_HLIST_NODE(&event->hlist_entry);
7978
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007979
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007980 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08007981 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007982
7983 mutex_init(&event->mmap_mutex);
7984
Al Viroa6fa9412012-08-20 14:59:25 +01007985 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007986 event->cpu = cpu;
7987 event->attr = *attr;
7988 event->group_leader = group_leader;
7989 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007990 event->oncpu = -1;
7991
7992 event->parent = parent_event;
7993
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08007994 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007995 event->id = atomic64_inc_return(&perf_event_id);
7996
7997 event->state = PERF_EVENT_STATE_INACTIVE;
7998
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007999 if (task) {
8000 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008001 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01008002 * XXX pmu::event_init needs to know what task to account to
8003 * and we cannot use the ctx information because we need the
8004 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008005 */
Peter Zijlstra50f16a82015-03-05 22:10:19 +01008006 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008007 }
8008
Peter Zijlstra34f43922015-02-20 14:05:38 +01008009 event->clock = &local_clock;
8010 if (parent_event)
8011 event->clock = parent_event->clock;
8012
Avi Kivity4dc0da82011-06-29 18:42:35 +03008013 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01008014 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03008015 context = parent_event->overflow_handler_context;
8016 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01008017
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01008018 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03008019 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02008020
Jiri Olsa0231bb52013-02-01 11:23:45 +01008021 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008022
8023 pmu = NULL;
8024
8025 hwc = &event->hw;
8026 hwc->sample_period = attr->sample_period;
8027 if (attr->freq && attr->sample_freq)
8028 hwc->sample_period = 1;
8029 hwc->last_period = hwc->sample_period;
8030
Peter Zijlstrae7850592010-05-21 14:43:08 +02008031 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008032
8033 /*
8034 * we currently do not support PERF_FORMAT_GROUP on inherited events
8035 */
8036 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008037 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008038
Yan, Zhenga46a2302014-11-04 21:56:06 -05008039 if (!has_branch_stack(event))
8040 event->attr.branch_sample_type = 0;
8041
Matt Fleming79dff512015-01-23 18:45:42 +00008042 if (cgroup_fd != -1) {
8043 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
8044 if (err)
8045 goto err_ns;
8046 }
8047
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008048 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008049 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008050 goto err_ns;
8051 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008052 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008053 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008054 }
8055
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008056 err = exclusive_event_init(event);
8057 if (err)
8058 goto err_pmu;
8059
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008060 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02008061 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
8062 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008063 if (err)
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008064 goto err_per_task;
Stephane Eraniand010b332012-02-09 23:21:00 +01008065 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008066 }
8067
Alexander Shishkin927a5572016-03-02 13:24:14 +02008068 /* symmetric to unaccount_event() in _free_event() */
8069 account_event(event);
8070
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008071 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008072
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008073err_per_task:
8074 exclusive_event_destroy(event);
8075
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008076err_pmu:
8077 if (event->destroy)
8078 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08008079 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008080err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00008081 if (is_cgroup_event(event))
8082 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008083 if (event->ns)
8084 put_pid_ns(event->ns);
8085 kfree(event);
8086
8087 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008088}
8089
8090static int perf_copy_attr(struct perf_event_attr __user *uattr,
8091 struct perf_event_attr *attr)
8092{
8093 u32 size;
8094 int ret;
8095
8096 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
8097 return -EFAULT;
8098
8099 /*
8100 * zero the full structure, so that a short copy will be nice.
8101 */
8102 memset(attr, 0, sizeof(*attr));
8103
8104 ret = get_user(size, &uattr->size);
8105 if (ret)
8106 return ret;
8107
8108 if (size > PAGE_SIZE) /* silly large */
8109 goto err_size;
8110
8111 if (!size) /* abi compat */
8112 size = PERF_ATTR_SIZE_VER0;
8113
8114 if (size < PERF_ATTR_SIZE_VER0)
8115 goto err_size;
8116
8117 /*
8118 * If we're handed a bigger struct than we know of,
8119 * ensure all the unknown bits are 0 - i.e. new
8120 * user-space does not rely on any kernel feature
8121 * extensions we dont know about yet.
8122 */
8123 if (size > sizeof(*attr)) {
8124 unsigned char __user *addr;
8125 unsigned char __user *end;
8126 unsigned char val;
8127
8128 addr = (void __user *)uattr + sizeof(*attr);
8129 end = (void __user *)uattr + size;
8130
8131 for (; addr < end; addr++) {
8132 ret = get_user(val, addr);
8133 if (ret)
8134 return ret;
8135 if (val)
8136 goto err_size;
8137 }
8138 size = sizeof(*attr);
8139 }
8140
8141 ret = copy_from_user(attr, uattr, size);
8142 if (ret)
8143 return -EFAULT;
8144
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05308145 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008146 return -EINVAL;
8147
8148 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
8149 return -EINVAL;
8150
8151 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
8152 return -EINVAL;
8153
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008154 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
8155 u64 mask = attr->branch_sample_type;
8156
8157 /* only using defined bits */
8158 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
8159 return -EINVAL;
8160
8161 /* at least one branch bit must be set */
8162 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
8163 return -EINVAL;
8164
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008165 /* propagate priv level, when not set for branch */
8166 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
8167
8168 /* exclude_kernel checked on syscall entry */
8169 if (!attr->exclude_kernel)
8170 mask |= PERF_SAMPLE_BRANCH_KERNEL;
8171
8172 if (!attr->exclude_user)
8173 mask |= PERF_SAMPLE_BRANCH_USER;
8174
8175 if (!attr->exclude_hv)
8176 mask |= PERF_SAMPLE_BRANCH_HV;
8177 /*
8178 * adjust user setting (for HW filter setup)
8179 */
8180 attr->branch_sample_type = mask;
8181 }
Stephane Eraniane7122092013-06-06 11:02:04 +02008182 /* privileged levels capture (kernel, hv): check permissions */
8183 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02008184 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8185 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008186 }
Jiri Olsa40189942012-08-07 15:20:37 +02008187
Jiri Olsac5ebced2012-08-07 15:20:40 +02008188 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02008189 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02008190 if (ret)
8191 return ret;
8192 }
8193
8194 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
8195 if (!arch_perf_have_user_stack_dump())
8196 return -ENOSYS;
8197
8198 /*
8199 * We have __u32 type for the size, but so far
8200 * we can only use __u16 as maximum due to the
8201 * __u16 sample size limit.
8202 */
8203 if (attr->sample_stack_user >= USHRT_MAX)
8204 ret = -EINVAL;
8205 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
8206 ret = -EINVAL;
8207 }
Jiri Olsa40189942012-08-07 15:20:37 +02008208
Stephane Eranian60e23642014-09-24 13:48:37 +02008209 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
8210 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008211out:
8212 return ret;
8213
8214err_size:
8215 put_user(sizeof(*attr), &uattr->size);
8216 ret = -E2BIG;
8217 goto out;
8218}
8219
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008220static int
8221perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008222{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01008223 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008224 int ret = -EINVAL;
8225
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008226 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008227 goto set;
8228
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008229 /* don't allow circular references */
8230 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008231 goto out;
8232
Peter Zijlstra0f139302010-05-20 14:35:15 +02008233 /*
8234 * Don't allow cross-cpu buffers
8235 */
8236 if (output_event->cpu != event->cpu)
8237 goto out;
8238
8239 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02008240 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02008241 */
8242 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
8243 goto out;
8244
Peter Zijlstra34f43922015-02-20 14:05:38 +01008245 /*
8246 * Mixing clocks in the same buffer is trouble you don't need.
8247 */
8248 if (output_event->clock != event->clock)
8249 goto out;
8250
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02008251 /*
8252 * If both events generate aux data, they must be on the same PMU
8253 */
8254 if (has_aux(event) && has_aux(output_event) &&
8255 event->pmu != output_event->pmu)
8256 goto out;
8257
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008258set:
8259 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008260 /* Can't redirect output if we've got an active mmap() */
8261 if (atomic_read(&event->mmap_count))
8262 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008263
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008264 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02008265 /* get the rb we want to redirect to */
8266 rb = ring_buffer_get(output_event);
8267 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008268 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008269 }
8270
Peter Zijlstrab69cf532014-03-14 10:50:33 +01008271 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02008272
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008273 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008274unlock:
8275 mutex_unlock(&event->mmap_mutex);
8276
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008277out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008278 return ret;
8279}
8280
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008281static void mutex_lock_double(struct mutex *a, struct mutex *b)
8282{
8283 if (b < a)
8284 swap(a, b);
8285
8286 mutex_lock(a);
8287 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
8288}
8289
Peter Zijlstra34f43922015-02-20 14:05:38 +01008290static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
8291{
8292 bool nmi_safe = false;
8293
8294 switch (clk_id) {
8295 case CLOCK_MONOTONIC:
8296 event->clock = &ktime_get_mono_fast_ns;
8297 nmi_safe = true;
8298 break;
8299
8300 case CLOCK_MONOTONIC_RAW:
8301 event->clock = &ktime_get_raw_fast_ns;
8302 nmi_safe = true;
8303 break;
8304
8305 case CLOCK_REALTIME:
8306 event->clock = &ktime_get_real_ns;
8307 break;
8308
8309 case CLOCK_BOOTTIME:
8310 event->clock = &ktime_get_boot_ns;
8311 break;
8312
8313 case CLOCK_TAI:
8314 event->clock = &ktime_get_tai_ns;
8315 break;
8316
8317 default:
8318 return -EINVAL;
8319 }
8320
8321 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
8322 return -EINVAL;
8323
8324 return 0;
8325}
8326
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008327/**
8328 * sys_perf_event_open - open a performance event, associate it to a task/cpu
8329 *
8330 * @attr_uptr: event_id type attributes for monitoring/sampling
8331 * @pid: target pid
8332 * @cpu: target cpu
8333 * @group_fd: group leader event fd
8334 */
8335SYSCALL_DEFINE5(perf_event_open,
8336 struct perf_event_attr __user *, attr_uptr,
8337 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
8338{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008339 struct perf_event *group_leader = NULL, *output_event = NULL;
8340 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008341 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008342 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008343 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04008344 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07008345 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008346 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04008347 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008348 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008349 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01008350 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00008351 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008352
8353 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02008354 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008355 return -EINVAL;
8356
8357 err = perf_copy_attr(attr_uptr, &attr);
8358 if (err)
8359 return err;
8360
8361 if (!attr.exclude_kernel) {
8362 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8363 return -EACCES;
8364 }
8365
8366 if (attr.freq) {
8367 if (attr.sample_freq > sysctl_perf_event_sample_rate)
8368 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02008369 } else {
8370 if (attr.sample_period & (1ULL << 63))
8371 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008372 }
8373
Stephane Eraniane5d13672011-02-14 11:20:01 +02008374 /*
8375 * In cgroup mode, the pid argument is used to pass the fd
8376 * opened to the cgroup directory in cgroupfs. The cpu argument
8377 * designates the cpu on which to monitor threads from that
8378 * cgroup.
8379 */
8380 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
8381 return -EINVAL;
8382
Yann Droneauda21b0b32014-01-05 21:36:33 +01008383 if (flags & PERF_FLAG_FD_CLOEXEC)
8384 f_flags |= O_CLOEXEC;
8385
8386 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04008387 if (event_fd < 0)
8388 return event_fd;
8389
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008390 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04008391 err = perf_fget_light(group_fd, &group);
8392 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008393 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04008394 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008395 if (flags & PERF_FLAG_FD_OUTPUT)
8396 output_event = group_leader;
8397 if (flags & PERF_FLAG_FD_NO_GROUP)
8398 group_leader = NULL;
8399 }
8400
Stephane Eraniane5d13672011-02-14 11:20:01 +02008401 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008402 task = find_lively_task_by_vpid(pid);
8403 if (IS_ERR(task)) {
8404 err = PTR_ERR(task);
8405 goto err_group_fd;
8406 }
8407 }
8408
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008409 if (task && group_leader &&
8410 group_leader->attr.inherit != attr.inherit) {
8411 err = -EINVAL;
8412 goto err_task;
8413 }
8414
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008415 get_online_cpus();
8416
Matt Fleming79dff512015-01-23 18:45:42 +00008417 if (flags & PERF_FLAG_PID_CGROUP)
8418 cgroup_fd = pid;
8419
Avi Kivity4dc0da82011-06-29 18:42:35 +03008420 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00008421 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008422 if (IS_ERR(event)) {
8423 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008424 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02008425 }
8426
Vince Weaver53b25332014-05-16 17:12:12 -04008427 if (is_sampling_event(event)) {
8428 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
8429 err = -ENOTSUPP;
8430 goto err_alloc;
8431 }
8432 }
8433
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008434 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008435 * Special case software events and allow them to be part of
8436 * any hardware group.
8437 */
8438 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008439
Peter Zijlstra34f43922015-02-20 14:05:38 +01008440 if (attr.use_clockid) {
8441 err = perf_event_set_clock(event, attr.clockid);
8442 if (err)
8443 goto err_alloc;
8444 }
8445
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008446 if (group_leader &&
8447 (is_software_event(event) != is_software_event(group_leader))) {
8448 if (is_software_event(event)) {
8449 /*
8450 * If event and group_leader are not both a software
8451 * event, and event is, then group leader is not.
8452 *
8453 * Allow the addition of software events to !software
8454 * groups, this is safe because software events never
8455 * fail to schedule.
8456 */
8457 pmu = group_leader->pmu;
8458 } else if (is_software_event(group_leader) &&
8459 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
8460 /*
8461 * In case the group is a pure software group, and we
8462 * try to add a hardware event, move the whole group to
8463 * the hardware context.
8464 */
8465 move_group = 1;
8466 }
8467 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008468
8469 /*
8470 * Get the target context (task or percpu):
8471 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05008472 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008473 if (IS_ERR(ctx)) {
8474 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008475 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008476 }
8477
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008478 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
8479 err = -EBUSY;
8480 goto err_context;
8481 }
8482
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02008483 if (task) {
8484 put_task_struct(task);
8485 task = NULL;
8486 }
8487
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008488 /*
8489 * Look up the group leader (we will attach this event to it):
8490 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008491 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008492 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008493
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008494 /*
8495 * Do not allow a recursive hierarchy (this new sibling
8496 * becoming part of another group-sibling):
8497 */
8498 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008499 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01008500
8501 /* All events in a group should have the same clock */
8502 if (group_leader->clock != event->clock)
8503 goto err_context;
8504
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008505 /*
8506 * Do not allow to attach to a group in a different
8507 * task or CPU context:
8508 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008509 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01008510 /*
8511 * Make sure we're both on the same task, or both
8512 * per-cpu events.
8513 */
8514 if (group_leader->ctx->task != ctx->task)
8515 goto err_context;
8516
8517 /*
8518 * Make sure we're both events for the same CPU;
8519 * grouping events for different CPUs is broken; since
8520 * you can never concurrently schedule them anyhow.
8521 */
8522 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008523 goto err_context;
8524 } else {
8525 if (group_leader->ctx != ctx)
8526 goto err_context;
8527 }
8528
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008529 /*
8530 * Only a group leader can be exclusive or pinned
8531 */
8532 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008533 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008534 }
8535
8536 if (output_event) {
8537 err = perf_event_set_output(event, output_event);
8538 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008539 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02008540 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008541
Yann Droneauda21b0b32014-01-05 21:36:33 +01008542 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
8543 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04008544 if (IS_ERR(event_file)) {
8545 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +02008546 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008547 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04008548 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008549
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008550 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008551 gctx = group_leader->ctx;
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008552 mutex_lock_double(&gctx->mutex, &ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008553 if (gctx->task == TASK_TOMBSTONE) {
8554 err = -ESRCH;
8555 goto err_locked;
8556 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008557 } else {
8558 mutex_lock(&ctx->mutex);
8559 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008560
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008561 if (ctx->task == TASK_TOMBSTONE) {
8562 err = -ESRCH;
8563 goto err_locked;
8564 }
8565
Peter Zijlstraa7239682015-09-09 19:06:33 +02008566 if (!perf_event_validate_size(event)) {
8567 err = -E2BIG;
8568 goto err_locked;
8569 }
8570
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008571 /*
8572 * Must be under the same ctx::mutex as perf_install_in_context(),
8573 * because we need to serialize with concurrent event creation.
8574 */
8575 if (!exclusive_event_installable(event, ctx)) {
8576 /* exclusive and group stuff are assumed mutually exclusive */
8577 WARN_ON_ONCE(move_group);
8578
8579 err = -EBUSY;
8580 goto err_locked;
8581 }
8582
8583 WARN_ON_ONCE(ctx->parent_ctx);
8584
8585 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008586 /*
8587 * See perf_event_ctx_lock() for comments on the details
8588 * of swizzling perf_event::ctx.
8589 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008590 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01008591
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008592 list_for_each_entry(sibling, &group_leader->sibling_list,
8593 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008594 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008595 put_ctx(gctx);
8596 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008597
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008598 /*
8599 * Wait for everybody to stop referencing the events through
8600 * the old lists, before installing it on new lists.
8601 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008602 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008603
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008604 /*
8605 * Install the group siblings before the group leader.
8606 *
8607 * Because a group leader will try and install the entire group
8608 * (through the sibling list, which is still in-tact), we can
8609 * end up with siblings installed in the wrong context.
8610 *
8611 * By installing siblings first we NO-OP because they're not
8612 * reachable through the group lists.
8613 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008614 list_for_each_entry(sibling, &group_leader->sibling_list,
8615 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008616 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01008617 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008618 get_ctx(ctx);
8619 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008620
8621 /*
8622 * Removing from the context ends up with disabled
8623 * event. What we want here is event in the initial
8624 * startup state, ready to be add into new context.
8625 */
8626 perf_event__state_init(group_leader);
8627 perf_install_in_context(ctx, group_leader, group_leader->cpu);
8628 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02008629
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008630 /*
8631 * Now that all events are installed in @ctx, nothing
8632 * references @gctx anymore, so drop the last reference we have
8633 * on it.
8634 */
8635 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008636 }
8637
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02008638 /*
8639 * Precalculate sample_data sizes; do while holding ctx::mutex such
8640 * that we're serialized against further additions and before
8641 * perf_install_in_context() which is the point the event is active and
8642 * can use these values.
8643 */
8644 perf_event__header_size(event);
8645 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008646
Peter Zijlstra78cd2c72016-01-25 14:08:45 +01008647 event->owner = current;
8648
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08008649 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008650 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008651
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008652 if (move_group)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008653 mutex_unlock(&gctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008654 mutex_unlock(&ctx->mutex);
8655
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008656 put_online_cpus();
8657
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008658 mutex_lock(&current->perf_event_mutex);
8659 list_add_tail(&event->owner_entry, &current->perf_event_list);
8660 mutex_unlock(&current->perf_event_mutex);
8661
Peter Zijlstra8a495422010-05-27 15:47:49 +02008662 /*
8663 * Drop the reference on the group_event after placing the
8664 * new event on the sibling_list. This ensures destruction
8665 * of the group leader will find the pointer to itself in
8666 * perf_group_detach().
8667 */
Al Viro2903ff02012-08-28 12:52:22 -04008668 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04008669 fd_install(event_fd, event_file);
8670 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008671
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02008672err_locked:
8673 if (move_group)
8674 mutex_unlock(&gctx->mutex);
8675 mutex_unlock(&ctx->mutex);
8676/* err_file: */
8677 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008678err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008679 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04008680 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02008681err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +01008682 /*
8683 * If event_file is set, the fput() above will have called ->release()
8684 * and that will take care of freeing the event.
8685 */
8686 if (!event_file)
8687 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008688err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08008689 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02008690err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02008691 if (task)
8692 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008693err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04008694 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04008695err_fd:
8696 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008697 return err;
8698}
8699
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008700/**
8701 * perf_event_create_kernel_counter
8702 *
8703 * @attr: attributes of the counter to create
8704 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07008705 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008706 */
8707struct perf_event *
8708perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07008709 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008710 perf_overflow_handler_t overflow_handler,
8711 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008712{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008713 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008714 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008715 int err;
8716
8717 /*
8718 * Get the target context (task or percpu):
8719 */
8720
Avi Kivity4dc0da82011-06-29 18:42:35 +03008721 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00008722 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008723 if (IS_ERR(event)) {
8724 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008725 goto err;
8726 }
8727
Jiri Olsaf8697762014-08-01 14:33:01 +02008728 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008729 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +02008730
Yan, Zheng4af57ef282014-11-04 21:56:01 -05008731 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008732 if (IS_ERR(ctx)) {
8733 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008734 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008735 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008736
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008737 WARN_ON_ONCE(ctx->parent_ctx);
8738 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008739 if (ctx->task == TASK_TOMBSTONE) {
8740 err = -ESRCH;
8741 goto err_unlock;
8742 }
8743
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008744 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008745 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008746 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008747 }
8748
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008749 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008750 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008751 mutex_unlock(&ctx->mutex);
8752
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008753 return event;
8754
Peter Zijlstra84c4e622016-02-24 18:45:40 +01008755err_unlock:
8756 mutex_unlock(&ctx->mutex);
8757 perf_unpin_context(ctx);
8758 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008759err_free:
8760 free_event(event);
8761err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01008762 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02008763}
8764EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
8765
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008766void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
8767{
8768 struct perf_event_context *src_ctx;
8769 struct perf_event_context *dst_ctx;
8770 struct perf_event *event, *tmp;
8771 LIST_HEAD(events);
8772
8773 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
8774 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
8775
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008776 /*
8777 * See perf_event_ctx_lock() for comments on the details
8778 * of swizzling perf_event::ctx.
8779 */
8780 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008781 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
8782 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01008783 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02008784 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008785 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02008786 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008787 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008788
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008789 /*
8790 * Wait for the events to quiesce before re-instating them.
8791 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008792 synchronize_rcu();
8793
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01008794 /*
8795 * Re-instate events in 2 passes.
8796 *
8797 * Skip over group leaders and only install siblings on this first
8798 * pass, siblings will not get enabled without a leader, however a
8799 * leader will enable its siblings, even if those are still on the old
8800 * context.
8801 */
8802 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
8803 if (event->group_leader == event)
8804 continue;
8805
8806 list_del(&event->migrate_entry);
8807 if (event->state >= PERF_EVENT_STATE_OFF)
8808 event->state = PERF_EVENT_STATE_INACTIVE;
8809 account_event_cpu(event, dst_cpu);
8810 perf_install_in_context(dst_ctx, event, dst_cpu);
8811 get_ctx(dst_ctx);
8812 }
8813
8814 /*
8815 * Once all the siblings are setup properly, install the group leaders
8816 * to make it go.
8817 */
Peter Zijlstra98861672013-10-03 16:02:23 +02008818 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
8819 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008820 if (event->state >= PERF_EVENT_STATE_OFF)
8821 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02008822 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008823 perf_install_in_context(dst_ctx, event, dst_cpu);
8824 get_ctx(dst_ctx);
8825 }
8826 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01008827 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08008828}
8829EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
8830
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008831static void sync_child_event(struct perf_event *child_event,
8832 struct task_struct *child)
8833{
8834 struct perf_event *parent_event = child_event->parent;
8835 u64 child_val;
8836
8837 if (child_event->attr.inherit_stat)
8838 perf_event_read_event(child_event, child);
8839
Peter Zijlstrab5e58792010-05-21 14:43:12 +02008840 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008841
8842 /*
8843 * Add back the child's count to the parent's count:
8844 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02008845 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008846 atomic64_add(child_event->total_time_enabled,
8847 &parent_event->child_total_time_enabled);
8848 atomic64_add(child_event->total_time_running,
8849 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008850}
8851
8852static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008853perf_event_exit_event(struct perf_event *child_event,
8854 struct perf_event_context *child_ctx,
8855 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008856{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008857 struct perf_event *parent_event = child_event->parent;
8858
Peter Zijlstra1903d502014-07-15 17:27:27 +02008859 /*
8860 * Do not destroy the 'original' grouping; because of the context
8861 * switch optimization the original events could've ended up in a
8862 * random child task.
8863 *
8864 * If we were to destroy the original group, all group related
8865 * operations would cease to function properly after this random
8866 * child dies.
8867 *
8868 * Do destroy all inherited groups, we don't care about those
8869 * and being thorough is better.
8870 */
Peter Zijlstra32132a32016-01-11 15:40:59 +01008871 raw_spin_lock_irq(&child_ctx->lock);
8872 WARN_ON_ONCE(child_ctx->is_active);
8873
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008874 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +01008875 perf_group_detach(child_event);
8876 list_del_event(child_event, child_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01008877 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +01008878 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008879
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008880 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008881 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008882 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008883 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -04008884 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008885 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008886 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008887 /*
8888 * Child events can be cleaned up.
8889 */
8890
8891 sync_child_event(child_event, child);
8892
8893 /*
8894 * Remove this event from the parent's list
8895 */
8896 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
8897 mutex_lock(&parent_event->child_mutex);
8898 list_del_init(&child_event->child_list);
8899 mutex_unlock(&parent_event->child_mutex);
8900
8901 /*
8902 * Kick perf_poll() for is_event_hup().
8903 */
8904 perf_event_wakeup(parent_event);
8905 free_event(child_event);
8906 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008907}
8908
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008909static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008910{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008911 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008912 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008913
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008914 WARN_ON_ONCE(child != current);
8915
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008916 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008917 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008918 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008919
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008920 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008921 * In order to reduce the amount of tricky in ctx tear-down, we hold
8922 * ctx::mutex over the entire thing. This serializes against almost
8923 * everything that wants to access the ctx.
8924 *
8925 * The exception is sys_perf_event_open() /
8926 * perf_event_create_kernel_count() which does find_get_context()
8927 * without ctx::mutex (it cannot because of the move_group double mutex
8928 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008929 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008930 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008931
8932 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008933 * In a single ctx::lock section, de-schedule the events and detach the
8934 * context from the task such that we cannot ever get it scheduled back
8935 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008936 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008937 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008938 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008939
8940 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008941 * Now that the context is inactive, destroy the task <-> ctx relation
8942 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008943 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01008944 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
8945 put_ctx(child_ctx); /* cannot be last */
8946 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
8947 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008948
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008949 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01008950 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008951
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008952 if (clone_ctx)
8953 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008954
8955 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008956 * Report the task dead after unscheduling the events so that we
8957 * won't get any samples after PERF_RECORD_EXIT. We can however still
8958 * get a few PERF_RECORD_READ events.
8959 */
8960 perf_event_task(child, child_ctx, 0);
8961
Peter Zijlstraebf905f2014-05-29 19:00:24 +02008962 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01008963 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008964
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008965 mutex_unlock(&child_ctx->mutex);
8966
8967 put_ctx(child_ctx);
8968}
8969
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008970/*
8971 * When a child task exits, feed back event values to parent events.
8972 */
8973void perf_event_exit_task(struct task_struct *child)
8974{
Peter Zijlstra88821352010-11-09 19:01:43 +01008975 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008976 int ctxn;
8977
Peter Zijlstra88821352010-11-09 19:01:43 +01008978 mutex_lock(&child->perf_event_mutex);
8979 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
8980 owner_entry) {
8981 list_del_init(&event->owner_entry);
8982
8983 /*
8984 * Ensure the list deletion is visible before we clear
8985 * the owner, closes a race against perf_release() where
8986 * we need to serialize on the owner->perf_event_mutex.
8987 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01008988 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +01008989 }
8990 mutex_unlock(&child->perf_event_mutex);
8991
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008992 for_each_task_context_nr(ctxn)
8993 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +01008994
8995 /*
8996 * The perf_event_exit_task_context calls perf_event_task
8997 * with child's task_ctx, which generates EXIT events for
8998 * child contexts and sets child->perf_event_ctxp[] to NULL.
8999 * At this point we need to send EXIT events to cpu contexts.
9000 */
9001 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009002}
9003
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009004static void perf_free_event(struct perf_event *event,
9005 struct perf_event_context *ctx)
9006{
9007 struct perf_event *parent = event->parent;
9008
9009 if (WARN_ON_ONCE(!parent))
9010 return;
9011
9012 mutex_lock(&parent->child_mutex);
9013 list_del_init(&event->child_list);
9014 mutex_unlock(&parent->child_mutex);
9015
Al Viroa6fa9412012-08-20 14:59:25 +01009016 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009017
Peter Zijlstra652884f2015-01-23 11:20:10 +01009018 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02009019 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009020 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +01009021 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009022 free_event(event);
9023}
9024
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009025/*
Peter Zijlstra652884f2015-01-23 11:20:10 +01009026 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009027 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +01009028 *
9029 * Not all locks are strictly required, but take them anyway to be nice and
9030 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009031 */
9032void perf_event_free_task(struct task_struct *task)
9033{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009034 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009035 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009036 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009037
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009038 for_each_task_context_nr(ctxn) {
9039 ctx = task->perf_event_ctxp[ctxn];
9040 if (!ctx)
9041 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009042
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009043 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009044again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009045 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
9046 group_entry)
9047 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009048
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009049 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
9050 group_entry)
9051 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009052
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009053 if (!list_empty(&ctx->pinned_groups) ||
9054 !list_empty(&ctx->flexible_groups))
9055 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009056
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009057 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009058
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009059 put_ctx(ctx);
9060 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009061}
9062
Peter Zijlstra4e231c72010-09-09 21:01:59 +02009063void perf_event_delayed_put(struct task_struct *task)
9064{
9065 int ctxn;
9066
9067 for_each_task_context_nr(ctxn)
9068 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
9069}
9070
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009071struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +00009072{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009073 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +00009074
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009075 file = fget_raw(fd);
9076 if (!file)
9077 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +00009078
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009079 if (file->f_op != &perf_fops) {
9080 fput(file);
9081 return ERR_PTR(-EBADF);
9082 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00009083
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009084 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +00009085}
9086
9087const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
9088{
9089 if (!event)
9090 return ERR_PTR(-EINVAL);
9091
9092 return &event->attr;
9093}
9094
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009095/*
9096 * inherit a event from parent task to child task:
9097 */
9098static struct perf_event *
9099inherit_event(struct perf_event *parent_event,
9100 struct task_struct *parent,
9101 struct perf_event_context *parent_ctx,
9102 struct task_struct *child,
9103 struct perf_event *group_leader,
9104 struct perf_event_context *child_ctx)
9105{
Jiri Olsa1929def2014-09-12 13:18:27 +02009106 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009107 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02009108 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009109
9110 /*
9111 * Instead of creating recursive hierarchies of events,
9112 * we link inherited events back to the original parent,
9113 * which has a filp for sure, which we use as the reference
9114 * count:
9115 */
9116 if (parent_event->parent)
9117 parent_event = parent_event->parent;
9118
9119 child_event = perf_event_alloc(&parent_event->attr,
9120 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009121 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009122 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +00009123 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009124 if (IS_ERR(child_event))
9125 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01009126
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02009127 /*
9128 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
9129 * must be under the same lock in order to serialize against
9130 * perf_event_release_kernel(), such that either we must observe
9131 * is_orphaned_event() or they will observe us on the child_list.
9132 */
9133 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02009134 if (is_orphaned_event(parent_event) ||
9135 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02009136 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +01009137 free_event(child_event);
9138 return NULL;
9139 }
9140
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009141 get_ctx(child_ctx);
9142
9143 /*
9144 * Make the child state follow the state of the parent event,
9145 * not its attr.disabled bit. We hold the parent's mutex,
9146 * so we won't race with perf_event_{en, dis}able_family.
9147 */
Jiri Olsa1929def2014-09-12 13:18:27 +02009148 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009149 child_event->state = PERF_EVENT_STATE_INACTIVE;
9150 else
9151 child_event->state = PERF_EVENT_STATE_OFF;
9152
9153 if (parent_event->attr.freq) {
9154 u64 sample_period = parent_event->hw.sample_period;
9155 struct hw_perf_event *hwc = &child_event->hw;
9156
9157 hwc->sample_period = sample_period;
9158 hwc->last_period = sample_period;
9159
9160 local64_set(&hwc->period_left, sample_period);
9161 }
9162
9163 child_event->ctx = child_ctx;
9164 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03009165 child_event->overflow_handler_context
9166 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009167
9168 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02009169 * Precalculate sample_data sizes
9170 */
9171 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02009172 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02009173
9174 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009175 * Link it up in the child's context:
9176 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02009177 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009178 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02009179 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009180
9181 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009182 * Link this into the parent event's child list
9183 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009184 list_add_tail(&child_event->child_list, &parent_event->child_list);
9185 mutex_unlock(&parent_event->child_mutex);
9186
9187 return child_event;
9188}
9189
9190static int inherit_group(struct perf_event *parent_event,
9191 struct task_struct *parent,
9192 struct perf_event_context *parent_ctx,
9193 struct task_struct *child,
9194 struct perf_event_context *child_ctx)
9195{
9196 struct perf_event *leader;
9197 struct perf_event *sub;
9198 struct perf_event *child_ctr;
9199
9200 leader = inherit_event(parent_event, parent, parent_ctx,
9201 child, NULL, child_ctx);
9202 if (IS_ERR(leader))
9203 return PTR_ERR(leader);
9204 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
9205 child_ctr = inherit_event(sub, parent, parent_ctx,
9206 child, leader, child_ctx);
9207 if (IS_ERR(child_ctr))
9208 return PTR_ERR(child_ctr);
9209 }
9210 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009211}
9212
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009213static int
9214inherit_task_group(struct perf_event *event, struct task_struct *parent,
9215 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009216 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009217 int *inherited_all)
9218{
9219 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009220 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009221
9222 if (!event->attr.inherit) {
9223 *inherited_all = 0;
9224 return 0;
9225 }
9226
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009227 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009228 if (!child_ctx) {
9229 /*
9230 * This is executed from the parent task context, so
9231 * inherit events that have been marked for cloning.
9232 * First allocate and initialize a context for the
9233 * child.
9234 */
9235
Jiri Olsa734df5a2013-07-09 17:44:10 +02009236 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009237 if (!child_ctx)
9238 return -ENOMEM;
9239
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009240 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009241 }
9242
9243 ret = inherit_group(event, parent, parent_ctx,
9244 child, child_ctx);
9245
9246 if (ret)
9247 *inherited_all = 0;
9248
9249 return ret;
9250}
9251
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009252/*
9253 * Initialize the perf_event context in task_struct
9254 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02009255static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009256{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009257 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009258 struct perf_event_context *cloned_ctx;
9259 struct perf_event *event;
9260 struct task_struct *parent = current;
9261 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009262 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009263 int ret = 0;
9264
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009265 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009266 return 0;
9267
9268 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009269 * If the parent's context is a clone, pin it so it won't get
9270 * swapped under us.
9271 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009272 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02009273 if (!parent_ctx)
9274 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009275
9276 /*
9277 * No need to check if parent_ctx != NULL here; since we saw
9278 * it non-NULL earlier, the only reason for it to become NULL
9279 * is if we exit, and since we're currently in the middle of
9280 * a fork we can't be exiting at the same time.
9281 */
9282
9283 /*
9284 * Lock the parent list. No need to lock the child - not PID
9285 * hashed yet and not running, so nobody can access it.
9286 */
9287 mutex_lock(&parent_ctx->mutex);
9288
9289 /*
9290 * We dont have to disable NMIs - we are only looking at
9291 * the list, not manipulating it:
9292 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009293 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009294 ret = inherit_task_group(event, parent, parent_ctx,
9295 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009296 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009297 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009298 }
9299
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009300 /*
9301 * We can't hold ctx->lock when iterating the ->flexible_group list due
9302 * to allocations, but we need to prevent rotation because
9303 * rotate_ctx() will change the list from interrupt context.
9304 */
9305 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
9306 parent_ctx->rotate_disable = 1;
9307 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
9308
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009309 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009310 ret = inherit_task_group(event, parent, parent_ctx,
9311 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009312 if (ret)
9313 break;
9314 }
9315
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009316 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
9317 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01009318
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009319 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009320
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01009321 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009322 /*
9323 * Mark the child context as a clone of the parent
9324 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009325 *
9326 * Note that if the parent is a clone, the holding of
9327 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009328 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009329 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009330 if (cloned_ctx) {
9331 child_ctx->parent_ctx = cloned_ctx;
9332 child_ctx->parent_gen = parent_ctx->parent_gen;
9333 } else {
9334 child_ctx->parent_ctx = parent_ctx;
9335 child_ctx->parent_gen = parent_ctx->generation;
9336 }
9337 get_ctx(child_ctx->parent_ctx);
9338 }
9339
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01009340 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009341 mutex_unlock(&parent_ctx->mutex);
9342
9343 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009344 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009345
9346 return ret;
9347}
9348
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009349/*
9350 * Initialize the perf_event context in task_struct
9351 */
9352int perf_event_init_task(struct task_struct *child)
9353{
9354 int ctxn, ret;
9355
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01009356 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
9357 mutex_init(&child->perf_event_mutex);
9358 INIT_LIST_HEAD(&child->perf_event_list);
9359
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009360 for_each_task_context_nr(ctxn) {
9361 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07009362 if (ret) {
9363 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009364 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07009365 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009366 }
9367
9368 return 0;
9369}
9370
Paul Mackerras220b1402010-03-10 20:45:52 +11009371static void __init perf_event_init_all_cpus(void)
9372{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009373 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11009374 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11009375
9376 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009377 swhash = &per_cpu(swevent_htable, cpu);
9378 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00009379 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11009380 }
9381}
9382
Paul Gortmaker0db06282013-06-19 14:53:51 -04009383static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009384{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009385 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009386
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009387 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +00009388 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009389 struct swevent_hlist *hlist;
9390
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009391 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
9392 WARN_ON(!hlist);
9393 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009394 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009395 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009396}
9397
Dave Young2965faa2015-09-09 15:38:55 -07009398#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009399static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009400{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009401 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009402 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
9403 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009404
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009405 raw_spin_lock(&ctx->lock);
9406 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009407 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01009408 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009409}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009410
9411static void perf_event_exit_cpu_context(int cpu)
9412{
9413 struct perf_event_context *ctx;
9414 struct pmu *pmu;
9415 int idx;
9416
9417 idx = srcu_read_lock(&pmus_srcu);
9418 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02009419 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009420
9421 mutex_lock(&ctx->mutex);
9422 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
9423 mutex_unlock(&ctx->mutex);
9424 }
9425 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02009426}
9427
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009428static void perf_event_exit_cpu(int cpu)
9429{
Peter Zijlstrae3703f82014-02-24 12:06:12 +01009430 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009431}
9432#else
9433static inline void perf_event_exit_cpu(int cpu) { }
9434#endif
9435
Peter Zijlstrac2774432010-12-08 15:29:02 +01009436static int
9437perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
9438{
9439 int cpu;
9440
9441 for_each_online_cpu(cpu)
9442 perf_event_exit_cpu(cpu);
9443
9444 return NOTIFY_OK;
9445}
9446
9447/*
9448 * Run the perf reboot notifier at the very last possible moment so that
9449 * the generic watchdog code runs as long as possible.
9450 */
9451static struct notifier_block perf_reboot_notifier = {
9452 .notifier_call = perf_reboot,
9453 .priority = INT_MIN,
9454};
9455
Paul Gortmaker0db06282013-06-19 14:53:51 -04009456static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009457perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
9458{
9459 unsigned int cpu = (long)hcpu;
9460
Linus Torvalds4536e4d2011-11-03 07:44:04 -07009461 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009462
9463 case CPU_UP_PREPARE:
Peter Zijlstra1dcaac12016-03-08 17:56:05 +01009464 /*
9465 * This must be done before the CPU comes alive, because the
9466 * moment we can run tasks we can encounter (software) events.
9467 *
9468 * Specifically, someone can have inherited events on kthreadd
9469 * or a pre-existing worker thread that gets re-bound.
9470 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009471 perf_event_init_cpu(cpu);
9472 break;
9473
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009474 case CPU_DOWN_PREPARE:
Peter Zijlstra1dcaac12016-03-08 17:56:05 +01009475 /*
9476 * This must be done before the CPU dies because after that an
9477 * active event might want to IPI the CPU and that'll not work
9478 * so great for dead CPUs.
9479 *
9480 * XXX smp_call_function_single() return -ENXIO without a warn
9481 * so we could possibly deal with this.
9482 *
9483 * This is safe against new events arriving because
9484 * sys_perf_event_open() serializes against hotplug using
9485 * get_online_cpus().
9486 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009487 perf_event_exit_cpu(cpu);
9488 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009489 default:
9490 break;
9491 }
9492
9493 return NOTIFY_OK;
9494}
9495
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009496void __init perf_event_init(void)
9497{
Jason Wessel3c502e72010-11-04 17:33:01 -05009498 int ret;
9499
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009500 idr_init(&pmu_idr);
9501
Paul Mackerras220b1402010-03-10 20:45:52 +11009502 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009503 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009504 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
9505 perf_pmu_register(&perf_cpu_clock, NULL, -1);
9506 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009507 perf_tp_register();
9508 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01009509 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05009510
9511 ret = init_hw_breakpoint();
9512 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02009513
Jiri Olsab01c3a02012-03-23 15:41:20 +01009514 /*
9515 * Build time assertion that we keep the data_head at the intended
9516 * location. IOW, validation we got the __reserved[] size right.
9517 */
9518 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
9519 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009520}
Peter Zijlstraabe43402010-11-17 23:17:37 +01009521
Cody P Schaferfd979c02015-01-30 13:45:57 -08009522ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
9523 char *page)
9524{
9525 struct perf_pmu_events_attr *pmu_attr =
9526 container_of(attr, struct perf_pmu_events_attr, attr);
9527
9528 if (pmu_attr->event_str)
9529 return sprintf(page, "%s\n", pmu_attr->event_str);
9530
9531 return 0;
9532}
Thomas Gleixner675965b2016-02-22 22:19:27 +00009533EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -08009534
Peter Zijlstraabe43402010-11-17 23:17:37 +01009535static int __init perf_event_sysfs_init(void)
9536{
9537 struct pmu *pmu;
9538 int ret;
9539
9540 mutex_lock(&pmus_lock);
9541
9542 ret = bus_register(&pmu_bus);
9543 if (ret)
9544 goto unlock;
9545
9546 list_for_each_entry(pmu, &pmus, entry) {
9547 if (!pmu->name || pmu->type < 0)
9548 continue;
9549
9550 ret = pmu_dev_alloc(pmu);
9551 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
9552 }
9553 pmu_bus_running = 1;
9554 ret = 0;
9555
9556unlock:
9557 mutex_unlock(&pmus_lock);
9558
9559 return ret;
9560}
9561device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009562
9563#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04009564static struct cgroup_subsys_state *
9565perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009566{
9567 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02009568
Li Zefan1b15d052011-03-03 14:26:06 +08009569 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009570 if (!jc)
9571 return ERR_PTR(-ENOMEM);
9572
Stephane Eraniane5d13672011-02-14 11:20:01 +02009573 jc->info = alloc_percpu(struct perf_cgroup_info);
9574 if (!jc->info) {
9575 kfree(jc);
9576 return ERR_PTR(-ENOMEM);
9577 }
9578
Stephane Eraniane5d13672011-02-14 11:20:01 +02009579 return &jc->css;
9580}
9581
Tejun Heoeb954192013-08-08 20:11:23 -04009582static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009583{
Tejun Heoeb954192013-08-08 20:11:23 -04009584 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
9585
Stephane Eraniane5d13672011-02-14 11:20:01 +02009586 free_percpu(jc->info);
9587 kfree(jc);
9588}
9589
9590static int __perf_cgroup_move(void *info)
9591{
9592 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +01009593 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +02009594 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +01009595 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +02009596 return 0;
9597}
9598
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009599static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02009600{
Tejun Heobb9d97b2011-12-12 18:12:21 -08009601 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009602 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -08009603
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05009604 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08009605 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02009606}
9607
Tejun Heo073219e2014-02-08 10:36:58 -05009608struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08009609 .css_alloc = perf_cgroup_css_alloc,
9610 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -08009611 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02009612};
9613#endif /* CONFIG_CGROUP_PERF */