blob: 87e945d6ebb8f542daf4559a3946fb304056f1ed [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>
Alexander Shishkin375637b2016-04-27 18:44:46 +030047#include <linux/namei.h>
48#include <linux/parser.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020049
Frederic Weisbecker76369132011-05-19 19:55:04 +020050#include "internal.h"
51
Ingo Molnarcdd6c482009-09-21 12:02:48 +020052#include <asm/irq_regs.h>
53
Peter Zijlstra272325c2015-04-15 11:41:58 +020054typedef int (*remote_function_f)(void *);
55
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010056struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020057 struct task_struct *p;
Peter Zijlstra272325c2015-04-15 11:41:58 +020058 remote_function_f func;
Ingo Molnare7e7ee22011-05-04 08:42:29 +020059 void *info;
60 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010061};
62
63static void remote_function(void *data)
64{
65 struct remote_function_call *tfc = data;
66 struct task_struct *p = tfc->p;
67
68 if (p) {
Peter Zijlstra0da4cf32016-02-24 18:45:51 +010069 /* -EAGAIN */
70 if (task_cpu(p) != smp_processor_id())
71 return;
72
73 /*
74 * Now that we're on right CPU with IRQs disabled, we can test
75 * if we hit the right task without races.
76 */
77
78 tfc->ret = -ESRCH; /* No such (running) process */
79 if (p != current)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010080 return;
81 }
82
83 tfc->ret = tfc->func(tfc->info);
84}
85
86/**
87 * task_function_call - call a function on the cpu on which a task runs
88 * @p: the task to evaluate
89 * @func: the function to be called
90 * @info: the function call argument
91 *
92 * Calls the function @func when the task is currently running. This might
93 * be on the current CPU, which just calls the function directly
94 *
95 * returns: @func return value, or
96 * -ESRCH - when the process isn't running
97 * -EAGAIN - when the process moved away
98 */
99static int
Peter Zijlstra272325c2015-04-15 11:41:58 +0200100task_function_call(struct task_struct *p, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100101{
102 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200103 .p = p,
104 .func = func,
105 .info = info,
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100106 .ret = -EAGAIN,
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100107 };
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100108 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100109
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100110 do {
111 ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1);
112 if (!ret)
113 ret = data.ret;
114 } while (ret == -EAGAIN);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100115
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100116 return ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100117}
118
119/**
120 * cpu_function_call - call a function on the cpu
121 * @func: the function to be called
122 * @info: the function call argument
123 *
124 * Calls the function @func on the remote cpu.
125 *
126 * returns: @func return value or -ENXIO when the cpu is offline
127 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200128static int cpu_function_call(int cpu, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100129{
130 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200131 .p = NULL,
132 .func = func,
133 .info = info,
134 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100135 };
136
137 smp_call_function_single(cpu, remote_function, &data, 1);
138
139 return data.ret;
140}
141
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100142static inline struct perf_cpu_context *
143__get_cpu_context(struct perf_event_context *ctx)
144{
145 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
146}
147
148static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
149 struct perf_event_context *ctx)
150{
151 raw_spin_lock(&cpuctx->ctx.lock);
152 if (ctx)
153 raw_spin_lock(&ctx->lock);
154}
155
156static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
157 struct perf_event_context *ctx)
158{
159 if (ctx)
160 raw_spin_unlock(&ctx->lock);
161 raw_spin_unlock(&cpuctx->ctx.lock);
162}
163
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100164#define TASK_TOMBSTONE ((void *)-1L)
165
166static bool is_kernel_event(struct perf_event *event)
167{
Peter Zijlstraf47c02c2016-01-26 12:30:14 +0100168 return READ_ONCE(event->owner) == TASK_TOMBSTONE;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100169}
170
Peter Zijlstra39a43642016-01-11 12:46:35 +0100171/*
172 * On task ctx scheduling...
173 *
174 * When !ctx->nr_events a task context will not be scheduled. This means
175 * we can disable the scheduler hooks (for performance) without leaving
176 * pending task ctx state.
177 *
178 * This however results in two special cases:
179 *
180 * - removing the last event from a task ctx; this is relatively straight
181 * forward and is done in __perf_remove_from_context.
182 *
183 * - adding the first event to a task ctx; this is tricky because we cannot
184 * rely on ctx->is_active and therefore cannot use event_function_call().
185 * See perf_install_in_context().
186 *
Peter Zijlstra39a43642016-01-11 12:46:35 +0100187 * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
188 */
189
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100190typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *,
191 struct perf_event_context *, void *);
192
193struct event_function_struct {
194 struct perf_event *event;
195 event_f func;
196 void *data;
197};
198
199static int event_function(void *info)
200{
201 struct event_function_struct *efs = info;
202 struct perf_event *event = efs->event;
203 struct perf_event_context *ctx = event->ctx;
204 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
205 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100206 int ret = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100207
208 WARN_ON_ONCE(!irqs_disabled());
209
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100210 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100211 /*
212 * Since we do the IPI call without holding ctx->lock things can have
213 * changed, double check we hit the task we set out to hit.
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100214 */
215 if (ctx->task) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100216 if (ctx->task != current) {
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100217 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100218 goto unlock;
219 }
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100220
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100221 /*
222 * We only use event_function_call() on established contexts,
223 * and event_function() is only ever called when active (or
224 * rather, we'll have bailed in task_function_call() or the
225 * above ctx->task != current test), therefore we must have
226 * ctx->is_active here.
227 */
228 WARN_ON_ONCE(!ctx->is_active);
229 /*
230 * And since we have ctx->is_active, cpuctx->task_ctx must
231 * match.
232 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100233 WARN_ON_ONCE(task_ctx != ctx);
234 } else {
235 WARN_ON_ONCE(&cpuctx->ctx != ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100236 }
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100237
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100238 efs->func(event, cpuctx, ctx, efs->data);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100239unlock:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100240 perf_ctx_unlock(cpuctx, task_ctx);
241
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100242 return ret;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100243}
244
245static void event_function_local(struct perf_event *event, event_f func, void *data)
246{
247 struct event_function_struct efs = {
248 .event = event,
249 .func = func,
250 .data = data,
251 };
252
253 int ret = event_function(&efs);
254 WARN_ON_ONCE(ret);
255}
256
257static void event_function_call(struct perf_event *event, event_f func, void *data)
Peter Zijlstra00179602015-11-30 16:26:35 +0100258{
259 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100260 struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100261 struct event_function_struct efs = {
262 .event = event,
263 .func = func,
264 .data = data,
265 };
Peter Zijlstra00179602015-11-30 16:26:35 +0100266
Peter Zijlstrac97f4732016-01-14 10:51:03 +0100267 if (!event->parent) {
268 /*
269 * If this is a !child event, we must hold ctx::mutex to
270 * stabilize the the event->ctx relation. See
271 * perf_event_ctx_lock().
272 */
273 lockdep_assert_held(&ctx->mutex);
274 }
Peter Zijlstra00179602015-11-30 16:26:35 +0100275
276 if (!task) {
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100277 cpu_function_call(event->cpu, event_function, &efs);
Peter Zijlstra00179602015-11-30 16:26:35 +0100278 return;
279 }
280
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100281 if (task == TASK_TOMBSTONE)
282 return;
283
Peter Zijlstraa0963092016-02-24 18:45:50 +0100284again:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100285 if (!task_function_call(task, event_function, &efs))
Peter Zijlstra00179602015-11-30 16:26:35 +0100286 return;
287
288 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100289 /*
290 * Reload the task pointer, it might have been changed by
291 * a concurrent perf_event_context_sched_out().
292 */
293 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +0100294 if (task == TASK_TOMBSTONE) {
295 raw_spin_unlock_irq(&ctx->lock);
296 return;
Peter Zijlstra00179602015-11-30 16:26:35 +0100297 }
Peter Zijlstraa0963092016-02-24 18:45:50 +0100298 if (ctx->is_active) {
299 raw_spin_unlock_irq(&ctx->lock);
300 goto again;
301 }
302 func(event, NULL, ctx, data);
Peter Zijlstra00179602015-11-30 16:26:35 +0100303 raw_spin_unlock_irq(&ctx->lock);
304}
305
Stephane Eraniane5d13672011-02-14 11:20:01 +0200306#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
307 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100308 PERF_FLAG_PID_CGROUP |\
309 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200310
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100311/*
312 * branch priv levels that need permission checks
313 */
314#define PERF_SAMPLE_BRANCH_PERM_PLM \
315 (PERF_SAMPLE_BRANCH_KERNEL |\
316 PERF_SAMPLE_BRANCH_HV)
317
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200318enum event_type_t {
319 EVENT_FLEXIBLE = 0x1,
320 EVENT_PINNED = 0x2,
Peter Zijlstra3cbaa592016-02-24 18:45:47 +0100321 EVENT_TIME = 0x4,
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200322 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
323};
324
Stephane Eraniane5d13672011-02-14 11:20:01 +0200325/*
326 * perf_sched_events : >0 events exist
327 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
328 */
Peter Zijlstra9107c892016-02-24 18:45:45 +0100329
330static void perf_sched_delayed(struct work_struct *work);
331DEFINE_STATIC_KEY_FALSE(perf_sched_events);
332static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed);
333static DEFINE_MUTEX(perf_sched_mutex);
334static atomic_t perf_sched_count;
335
Stephane Eraniane5d13672011-02-14 11:20:01 +0200336static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Yan, Zhengba532502014-11-04 21:55:58 -0500337static DEFINE_PER_CPU(int, perf_sched_cb_usages);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200338
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200339static atomic_t nr_mmap_events __read_mostly;
340static atomic_t nr_comm_events __read_mostly;
341static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200342static atomic_t nr_freq_events __read_mostly;
Adrian Hunter45ac1402015-07-21 12:44:02 +0300343static atomic_t nr_switch_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200344
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200345static LIST_HEAD(pmus);
346static DEFINE_MUTEX(pmus_lock);
347static struct srcu_struct pmus_srcu;
348
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200349/*
350 * perf event paranoia level:
351 * -1 - not paranoid at all
352 * 0 - disallow raw tracepoint access for unpriv
353 * 1 - disallow cpu events for unpriv
354 * 2 - disallow kernel profiling for unpriv
355 */
Andy Lutomirski01610282016-05-09 15:48:51 -0700356int sysctl_perf_event_paranoid __read_mostly = 2;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200357
Frederic Weisbecker20443382011-03-31 03:33:29 +0200358/* Minimum for 512 kiB + 1 user control page */
359int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200360
361/*
362 * max perf event sample rate
363 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700364#define DEFAULT_MAX_SAMPLE_RATE 100000
365#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
366#define DEFAULT_CPU_TIME_MAX_PERCENT 25
367
368int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
369
370static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
371static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
372
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200373static int perf_sample_allowed_ns __read_mostly =
374 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700375
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800376static void update_perf_cpu_limits(void)
Dave Hansen14c63f12013-06-21 08:51:36 -0700377{
378 u64 tmp = perf_sample_period_ns;
379
380 tmp *= sysctl_perf_cpu_time_max_percent;
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100381 tmp = div_u64(tmp, 100);
382 if (!tmp)
383 tmp = 1;
384
385 WRITE_ONCE(perf_sample_allowed_ns, tmp);
Dave Hansen14c63f12013-06-21 08:51:36 -0700386}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100387
Stephane Eranian9e630202013-04-03 14:21:33 +0200388static int perf_rotate_context(struct perf_cpu_context *cpuctx);
389
Peter Zijlstra163ec432011-02-16 11:22:34 +0100390int perf_proc_update_handler(struct ctl_table *table, int write,
391 void __user *buffer, size_t *lenp,
392 loff_t *ppos)
393{
Knut Petersen723478c2013-09-25 14:29:37 +0200394 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100395
396 if (ret || !write)
397 return ret;
398
399 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700400 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
401 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100402
403 return 0;
404}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200405
Dave Hansen14c63f12013-06-21 08:51:36 -0700406int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
407
408int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
409 void __user *buffer, size_t *lenp,
410 loff_t *ppos)
411{
412 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
413
414 if (ret || !write)
415 return ret;
416
Peter Zijlstrab303e7c2016-04-04 09:57:40 +0200417 if (sysctl_perf_cpu_time_max_percent == 100 ||
418 sysctl_perf_cpu_time_max_percent == 0) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100419 printk(KERN_WARNING
420 "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
421 WRITE_ONCE(perf_sample_allowed_ns, 0);
422 } else {
423 update_perf_cpu_limits();
424 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700425
426 return 0;
427}
428
429/*
430 * perf samples are done in some very critical code paths (NMIs).
431 * If they take too much CPU time, the system can lock up and not
432 * get any real work done. This will drop the sample rate when
433 * we detect that events are taking too long.
434 */
435#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200436static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700437
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100438static u64 __report_avg;
439static u64 __report_allowed;
440
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100441static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700442{
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100443 printk_ratelimited(KERN_WARNING
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100444 "perf: interrupt took too long (%lld > %lld), lowering "
445 "kernel.perf_event_max_sample_rate to %d\n",
446 __report_avg, __report_allowed,
447 sysctl_perf_event_sample_rate);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100448}
449
450static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
451
452void perf_sample_event_took(u64 sample_len_ns)
453{
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100454 u64 max_len = READ_ONCE(perf_sample_allowed_ns);
455 u64 running_len;
456 u64 avg_len;
457 u32 max;
Dave Hansen14c63f12013-06-21 08:51:36 -0700458
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100459 if (max_len == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700460 return;
461
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100462 /* Decay the counter by 1 average sample. */
463 running_len = __this_cpu_read(running_sample_length);
464 running_len -= running_len/NR_ACCUMULATED_SAMPLES;
465 running_len += sample_len_ns;
466 __this_cpu_write(running_sample_length, running_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700467
468 /*
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100469 * Note: this will be biased artifically low until we have
470 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
Dave Hansen14c63f12013-06-21 08:51:36 -0700471 * from having to maintain a count.
472 */
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100473 avg_len = running_len/NR_ACCUMULATED_SAMPLES;
474 if (avg_len <= max_len)
Dave Hansen14c63f12013-06-21 08:51:36 -0700475 return;
476
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100477 __report_avg = avg_len;
478 __report_allowed = max_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700479
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100480 /*
481 * Compute a throttle threshold 25% below the current duration.
482 */
483 avg_len += avg_len / 4;
484 max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
485 if (avg_len < max)
486 max /= (u32)avg_len;
487 else
488 max = 1;
489
490 WRITE_ONCE(perf_sample_allowed_ns, avg_len);
491 WRITE_ONCE(max_samples_per_tick, max);
492
493 sysctl_perf_event_sample_rate = max * HZ;
Dave Hansen14c63f12013-06-21 08:51:36 -0700494 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
495
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100496 if (!irq_work_queue(&perf_duration_work)) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100497 early_printk("perf: interrupt took too long (%lld > %lld), lowering "
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100498 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100499 __report_avg, __report_allowed,
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100500 sysctl_perf_event_sample_rate);
501 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700502}
503
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200504static atomic64_t perf_event_id;
505
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200506static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
507 enum event_type_t event_type);
508
509static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200510 enum event_type_t event_type,
511 struct task_struct *task);
512
513static void update_context_time(struct perf_event_context *ctx);
514static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200515
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200516void __weak perf_event_print_debug(void) { }
517
Matt Fleming84c79912010-10-03 21:41:13 +0100518extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200519{
Matt Fleming84c79912010-10-03 21:41:13 +0100520 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200521}
522
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200523static inline u64 perf_clock(void)
524{
525 return local_clock();
526}
527
Peter Zijlstra34f43922015-02-20 14:05:38 +0100528static inline u64 perf_event_clock(struct perf_event *event)
529{
530 return event->clock();
531}
532
Stephane Eraniane5d13672011-02-14 11:20:01 +0200533#ifdef CONFIG_CGROUP_PERF
534
Stephane Eraniane5d13672011-02-14 11:20:01 +0200535static inline bool
536perf_cgroup_match(struct perf_event *event)
537{
538 struct perf_event_context *ctx = event->ctx;
539 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
540
Tejun Heoef824fa2013-04-08 19:00:38 -0700541 /* @event doesn't care about cgroup */
542 if (!event->cgrp)
543 return true;
544
545 /* wants specific cgroup scope but @cpuctx isn't associated with any */
546 if (!cpuctx->cgrp)
547 return false;
548
549 /*
550 * Cgroup scoping is recursive. An event enabled for a cgroup is
551 * also enabled for all its descendant cgroups. If @cpuctx's
552 * cgroup is a descendant of @event's (the test covers identity
553 * case), it's a match.
554 */
555 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
556 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200557}
558
Stephane Eraniane5d13672011-02-14 11:20:01 +0200559static inline void perf_detach_cgroup(struct perf_event *event)
560{
Zefan Li4e2ba652014-09-19 16:53:14 +0800561 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200562 event->cgrp = NULL;
563}
564
565static inline int is_cgroup_event(struct perf_event *event)
566{
567 return event->cgrp != NULL;
568}
569
570static inline u64 perf_cgroup_event_time(struct perf_event *event)
571{
572 struct perf_cgroup_info *t;
573
574 t = per_cpu_ptr(event->cgrp->info, event->cpu);
575 return t->time;
576}
577
578static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
579{
580 struct perf_cgroup_info *info;
581 u64 now;
582
583 now = perf_clock();
584
585 info = this_cpu_ptr(cgrp->info);
586
587 info->time += now - info->timestamp;
588 info->timestamp = now;
589}
590
591static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
592{
593 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
594 if (cgrp_out)
595 __update_cgrp_time(cgrp_out);
596}
597
598static inline void update_cgrp_time_from_event(struct perf_event *event)
599{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200600 struct perf_cgroup *cgrp;
601
Stephane Eraniane5d13672011-02-14 11:20:01 +0200602 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200603 * ensure we access cgroup data only when needed and
604 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200605 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200606 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200607 return;
608
Stephane Eranian614e4c42015-11-12 11:00:04 +0100609 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200610 /*
611 * Do not update time when cgroup is not active
612 */
613 if (cgrp == event->cgrp)
614 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200615}
616
617static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200618perf_cgroup_set_timestamp(struct task_struct *task,
619 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200620{
621 struct perf_cgroup *cgrp;
622 struct perf_cgroup_info *info;
623
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200624 /*
625 * ctx->lock held by caller
626 * ensure we do not access cgroup data
627 * unless we have the cgroup pinned (css_get)
628 */
629 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200630 return;
631
Stephane Eranian614e4c42015-11-12 11:00:04 +0100632 cgrp = perf_cgroup_from_task(task, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200633 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200634 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200635}
636
637#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
638#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
639
640/*
641 * reschedule events based on the cgroup constraint of task.
642 *
643 * mode SWOUT : schedule out everything
644 * mode SWIN : schedule in based on cgroup for next
645 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800646static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200647{
648 struct perf_cpu_context *cpuctx;
649 struct pmu *pmu;
650 unsigned long flags;
651
652 /*
653 * disable interrupts to avoid geting nr_cgroup
654 * changes via __perf_event_disable(). Also
655 * avoids preemption.
656 */
657 local_irq_save(flags);
658
659 /*
660 * we reschedule only in the presence of cgroup
661 * constrained events.
662 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200663
664 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200665 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200666 if (cpuctx->unique_pmu != pmu)
667 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200668
Stephane Eraniane5d13672011-02-14 11:20:01 +0200669 /*
670 * perf_cgroup_events says at least one
671 * context on this CPU has cgroup events.
672 *
673 * ctx->nr_cgroups reports the number of cgroup
674 * events for a context.
675 */
676 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200677 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
678 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200679
680 if (mode & PERF_CGROUP_SWOUT) {
681 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
682 /*
683 * must not be done before ctxswout due
684 * to event_filter_match() in event_sched_out()
685 */
686 cpuctx->cgrp = NULL;
687 }
688
689 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200690 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200691 /*
692 * set cgrp before ctxsw in to allow
693 * event_filter_match() to not have to pass
694 * task around
Stephane Eranian614e4c42015-11-12 11:00:04 +0100695 * we pass the cpuctx->ctx to perf_cgroup_from_task()
696 * because cgorup events are only per-cpu
Stephane Eraniane5d13672011-02-14 11:20:01 +0200697 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100698 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200699 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
700 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200701 perf_pmu_enable(cpuctx->ctx.pmu);
702 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200703 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200704 }
705
Stephane Eraniane5d13672011-02-14 11:20:01 +0200706 local_irq_restore(flags);
707}
708
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200709static inline void perf_cgroup_sched_out(struct task_struct *task,
710 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200711{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200712 struct perf_cgroup *cgrp1;
713 struct perf_cgroup *cgrp2 = NULL;
714
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100715 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200716 /*
717 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100718 * we do not need to pass the ctx here because we know
719 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200720 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100721 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100722 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200723
724 /*
725 * only schedule out current cgroup events if we know
726 * that we are switching to a different cgroup. Otherwise,
727 * do no touch the cgroup events.
728 */
729 if (cgrp1 != cgrp2)
730 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100731
732 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200733}
734
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200735static inline void perf_cgroup_sched_in(struct task_struct *prev,
736 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200737{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200738 struct perf_cgroup *cgrp1;
739 struct perf_cgroup *cgrp2 = NULL;
740
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100741 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200742 /*
743 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100744 * we do not need to pass the ctx here because we know
745 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200746 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100747 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100748 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200749
750 /*
751 * only need to schedule in cgroup events if we are changing
752 * cgroup during ctxsw. Cgroup events were not scheduled
753 * out of ctxsw out if that was not the case.
754 */
755 if (cgrp1 != cgrp2)
756 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100757
758 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200759}
760
761static inline int perf_cgroup_connect(int fd, struct perf_event *event,
762 struct perf_event_attr *attr,
763 struct perf_event *group_leader)
764{
765 struct perf_cgroup *cgrp;
766 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400767 struct fd f = fdget(fd);
768 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200769
Al Viro2903ff02012-08-28 12:52:22 -0400770 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200771 return -EBADF;
772
Al Virob5830432014-10-31 01:22:04 -0400773 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400774 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800775 if (IS_ERR(css)) {
776 ret = PTR_ERR(css);
777 goto out;
778 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200779
780 cgrp = container_of(css, struct perf_cgroup, css);
781 event->cgrp = cgrp;
782
783 /*
784 * all events in a group must monitor
785 * the same cgroup because a task belongs
786 * to only one perf cgroup at a time
787 */
788 if (group_leader && group_leader->cgrp != cgrp) {
789 perf_detach_cgroup(event);
790 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200791 }
Li Zefan3db272c2011-03-03 14:25:37 +0800792out:
Al Viro2903ff02012-08-28 12:52:22 -0400793 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200794 return ret;
795}
796
797static inline void
798perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
799{
800 struct perf_cgroup_info *t;
801 t = per_cpu_ptr(event->cgrp->info, event->cpu);
802 event->shadow_ctx_time = now - t->timestamp;
803}
804
805static inline void
806perf_cgroup_defer_enabled(struct perf_event *event)
807{
808 /*
809 * when the current task's perf cgroup does not match
810 * the event's, we need to remember to call the
811 * perf_mark_enable() function the first time a task with
812 * a matching perf cgroup is scheduled in.
813 */
814 if (is_cgroup_event(event) && !perf_cgroup_match(event))
815 event->cgrp_defer_enabled = 1;
816}
817
818static inline void
819perf_cgroup_mark_enabled(struct perf_event *event,
820 struct perf_event_context *ctx)
821{
822 struct perf_event *sub;
823 u64 tstamp = perf_event_time(event);
824
825 if (!event->cgrp_defer_enabled)
826 return;
827
828 event->cgrp_defer_enabled = 0;
829
830 event->tstamp_enabled = tstamp - event->total_time_enabled;
831 list_for_each_entry(sub, &event->sibling_list, group_entry) {
832 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
833 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
834 sub->cgrp_defer_enabled = 0;
835 }
836 }
837}
838#else /* !CONFIG_CGROUP_PERF */
839
840static inline bool
841perf_cgroup_match(struct perf_event *event)
842{
843 return true;
844}
845
846static inline void perf_detach_cgroup(struct perf_event *event)
847{}
848
849static inline int is_cgroup_event(struct perf_event *event)
850{
851 return 0;
852}
853
854static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
855{
856 return 0;
857}
858
859static inline void update_cgrp_time_from_event(struct perf_event *event)
860{
861}
862
863static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
864{
865}
866
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200867static inline void perf_cgroup_sched_out(struct task_struct *task,
868 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200869{
870}
871
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200872static inline void perf_cgroup_sched_in(struct task_struct *prev,
873 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200874{
875}
876
877static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
878 struct perf_event_attr *attr,
879 struct perf_event *group_leader)
880{
881 return -EINVAL;
882}
883
884static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200885perf_cgroup_set_timestamp(struct task_struct *task,
886 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200887{
888}
889
890void
891perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
892{
893}
894
895static inline void
896perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
897{
898}
899
900static inline u64 perf_cgroup_event_time(struct perf_event *event)
901{
902 return 0;
903}
904
905static inline void
906perf_cgroup_defer_enabled(struct perf_event *event)
907{
908}
909
910static inline void
911perf_cgroup_mark_enabled(struct perf_event *event,
912 struct perf_event_context *ctx)
913{
914}
915#endif
916
Stephane Eranian9e630202013-04-03 14:21:33 +0200917/*
918 * set default to be dependent on timer tick just
919 * like original code
920 */
921#define PERF_CPU_HRTIMER (1000 / HZ)
922/*
923 * function must be called with interrupts disbled
924 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200925static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +0200926{
927 struct perf_cpu_context *cpuctx;
Stephane Eranian9e630202013-04-03 14:21:33 +0200928 int rotations = 0;
929
930 WARN_ON(!irqs_disabled());
931
932 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200933 rotations = perf_rotate_context(cpuctx);
934
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200935 raw_spin_lock(&cpuctx->hrtimer_lock);
936 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +0200937 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200938 else
939 cpuctx->hrtimer_active = 0;
940 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +0200941
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200942 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +0200943}
944
Peter Zijlstra272325c2015-04-15 11:41:58 +0200945static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +0200946{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200947 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200948 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +0200949 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +0200950
951 /* no multiplexing needed for SW PMU */
952 if (pmu->task_ctx_nr == perf_sw_context)
953 return;
954
Stephane Eranian62b85632013-04-03 14:21:34 +0200955 /*
956 * check default is sane, if not set then force to
957 * default interval (1/tick)
958 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200959 interval = pmu->hrtimer_interval_ms;
960 if (interval < 1)
961 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +0200962
Peter Zijlstra272325c2015-04-15 11:41:58 +0200963 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +0200964
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200965 raw_spin_lock_init(&cpuctx->hrtimer_lock);
966 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra272325c2015-04-15 11:41:58 +0200967 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +0200968}
969
Peter Zijlstra272325c2015-04-15 11:41:58 +0200970static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +0200971{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200972 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200973 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200974 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +0200975
976 /* not for SW PMU */
977 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +0200978 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +0200979
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200980 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
981 if (!cpuctx->hrtimer_active) {
982 cpuctx->hrtimer_active = 1;
983 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
984 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
985 }
986 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +0200987
Peter Zijlstra272325c2015-04-15 11:41:58 +0200988 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +0200989}
990
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200991void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200992{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200993 int *count = this_cpu_ptr(pmu->pmu_disable_count);
994 if (!(*count)++)
995 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200996}
997
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200998void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200999{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001000 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1001 if (!--(*count))
1002 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001003}
1004
Mark Rutland2fde4f92015-01-07 15:01:54 +00001005static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001006
1007/*
Mark Rutland2fde4f92015-01-07 15:01:54 +00001008 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1009 * perf_event_task_tick() are fully serialized because they're strictly cpu
1010 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1011 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001012 */
Mark Rutland2fde4f92015-01-07 15:01:54 +00001013static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001014{
Mark Rutland2fde4f92015-01-07 15:01:54 +00001015 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001016
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001017 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001018
Mark Rutland2fde4f92015-01-07 15:01:54 +00001019 WARN_ON(!list_empty(&ctx->active_ctx_list));
1020
1021 list_add(&ctx->active_ctx_list, head);
1022}
1023
1024static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1025{
1026 WARN_ON(!irqs_disabled());
1027
1028 WARN_ON(list_empty(&ctx->active_ctx_list));
1029
1030 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001031}
1032
1033static void get_ctx(struct perf_event_context *ctx)
1034{
1035 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
1036}
1037
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001038static void free_ctx(struct rcu_head *head)
1039{
1040 struct perf_event_context *ctx;
1041
1042 ctx = container_of(head, struct perf_event_context, rcu_head);
1043 kfree(ctx->task_ctx_data);
1044 kfree(ctx);
1045}
1046
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001047static void put_ctx(struct perf_event_context *ctx)
1048{
1049 if (atomic_dec_and_test(&ctx->refcount)) {
1050 if (ctx->parent_ctx)
1051 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001052 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001053 put_task_struct(ctx->task);
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001054 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001055 }
1056}
1057
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001058/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001059 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1060 * perf_pmu_migrate_context() we need some magic.
1061 *
1062 * Those places that change perf_event::ctx will hold both
1063 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1064 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001065 * Lock ordering is by mutex address. There are two other sites where
1066 * perf_event_context::mutex nests and those are:
1067 *
1068 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001069 * perf_event_exit_event()
1070 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001071 *
1072 * - perf_event_init_context() [ parent, 0 ]
1073 * inherit_task_group()
1074 * inherit_group()
1075 * inherit_event()
1076 * perf_event_alloc()
1077 * perf_init_event()
1078 * perf_try_init_event() [ child , 1 ]
1079 *
1080 * While it appears there is an obvious deadlock here -- the parent and child
1081 * nesting levels are inverted between the two. This is in fact safe because
1082 * life-time rules separate them. That is an exiting task cannot fork, and a
1083 * spawning task cannot (yet) exit.
1084 *
1085 * But remember that that these are parent<->child context relations, and
1086 * migration does not affect children, therefore these two orderings should not
1087 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001088 *
1089 * The change in perf_event::ctx does not affect children (as claimed above)
1090 * because the sys_perf_event_open() case will install a new event and break
1091 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1092 * concerned with cpuctx and that doesn't have children.
1093 *
1094 * The places that change perf_event::ctx will issue:
1095 *
1096 * perf_remove_from_context();
1097 * synchronize_rcu();
1098 * perf_install_in_context();
1099 *
1100 * to affect the change. The remove_from_context() + synchronize_rcu() should
1101 * quiesce the event, after which we can install it in the new location. This
1102 * means that only external vectors (perf_fops, prctl) can perturb the event
1103 * while in transit. Therefore all such accessors should also acquire
1104 * perf_event_context::mutex to serialize against this.
1105 *
1106 * However; because event->ctx can change while we're waiting to acquire
1107 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1108 * function.
1109 *
1110 * Lock order:
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02001111 * cred_guard_mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001112 * task_struct::perf_event_mutex
1113 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001114 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001115 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001116 * perf_event::mmap_mutex
1117 * mmap_sem
1118 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001119static struct perf_event_context *
1120perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001121{
1122 struct perf_event_context *ctx;
1123
1124again:
1125 rcu_read_lock();
1126 ctx = ACCESS_ONCE(event->ctx);
1127 if (!atomic_inc_not_zero(&ctx->refcount)) {
1128 rcu_read_unlock();
1129 goto again;
1130 }
1131 rcu_read_unlock();
1132
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001133 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001134 if (event->ctx != ctx) {
1135 mutex_unlock(&ctx->mutex);
1136 put_ctx(ctx);
1137 goto again;
1138 }
1139
1140 return ctx;
1141}
1142
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001143static inline struct perf_event_context *
1144perf_event_ctx_lock(struct perf_event *event)
1145{
1146 return perf_event_ctx_lock_nested(event, 0);
1147}
1148
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001149static void perf_event_ctx_unlock(struct perf_event *event,
1150 struct perf_event_context *ctx)
1151{
1152 mutex_unlock(&ctx->mutex);
1153 put_ctx(ctx);
1154}
1155
1156/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001157 * This must be done under the ctx->lock, such as to serialize against
1158 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1159 * calling scheduler related locks and ctx->lock nests inside those.
1160 */
1161static __must_check struct perf_event_context *
1162unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001163{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001164 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1165
1166 lockdep_assert_held(&ctx->lock);
1167
1168 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001169 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001170 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001171
1172 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001173}
1174
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001175static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1176{
1177 /*
1178 * only top level events have the pid namespace they were created in
1179 */
1180 if (event->parent)
1181 event = event->parent;
1182
1183 return task_tgid_nr_ns(p, event->ns);
1184}
1185
1186static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1187{
1188 /*
1189 * only top level events have the pid namespace they were created in
1190 */
1191 if (event->parent)
1192 event = event->parent;
1193
1194 return task_pid_nr_ns(p, event->ns);
1195}
1196
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001197/*
1198 * If we inherit events we want to return the parent event id
1199 * to userspace.
1200 */
1201static u64 primary_event_id(struct perf_event *event)
1202{
1203 u64 id = event->id;
1204
1205 if (event->parent)
1206 id = event->parent->id;
1207
1208 return id;
1209}
1210
1211/*
1212 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001213 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001214 * This has to cope with with the fact that until it is locked,
1215 * the context could get moved to another task.
1216 */
1217static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001218perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001219{
1220 struct perf_event_context *ctx;
1221
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001222retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001223 /*
1224 * One of the few rules of preemptible RCU is that one cannot do
1225 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001226 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001227 * rcu_read_unlock_special().
1228 *
1229 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001230 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001231 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001232 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001233 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001234 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001235 if (ctx) {
1236 /*
1237 * If this context is a clone of another, it might
1238 * get swapped for another underneath us by
1239 * perf_event_task_sched_out, though the
1240 * rcu_read_lock() protects us from any context
1241 * getting freed. Lock the context and check if it
1242 * got swapped before we could get the lock, and retry
1243 * if so. If we locked the right context, then it
1244 * can't get swapped on us any more.
1245 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001246 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001247 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001248 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001249 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001250 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001251 goto retry;
1252 }
1253
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001254 if (ctx->task == TASK_TOMBSTONE ||
1255 !atomic_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001256 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001257 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001258 } else {
1259 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001260 }
1261 }
1262 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001263 if (!ctx)
1264 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001265 return ctx;
1266}
1267
1268/*
1269 * Get the context for a task and increment its pin_count so it
1270 * can't get swapped to another task. This also increments its
1271 * reference count so that the context can't get freed.
1272 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001273static struct perf_event_context *
1274perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001275{
1276 struct perf_event_context *ctx;
1277 unsigned long flags;
1278
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001279 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001280 if (ctx) {
1281 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001282 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001283 }
1284 return ctx;
1285}
1286
1287static void perf_unpin_context(struct perf_event_context *ctx)
1288{
1289 unsigned long flags;
1290
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001291 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001292 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001293 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001294}
1295
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001296/*
1297 * Update the record of the current time in a context.
1298 */
1299static void update_context_time(struct perf_event_context *ctx)
1300{
1301 u64 now = perf_clock();
1302
1303 ctx->time += now - ctx->timestamp;
1304 ctx->timestamp = now;
1305}
1306
Stephane Eranian41587552011-01-03 18:20:01 +02001307static u64 perf_event_time(struct perf_event *event)
1308{
1309 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001310
1311 if (is_cgroup_event(event))
1312 return perf_cgroup_event_time(event);
1313
Stephane Eranian41587552011-01-03 18:20:01 +02001314 return ctx ? ctx->time : 0;
1315}
1316
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001317/*
1318 * Update the total_time_enabled and total_time_running fields for a event.
1319 */
1320static void update_event_times(struct perf_event *event)
1321{
1322 struct perf_event_context *ctx = event->ctx;
1323 u64 run_end;
1324
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001325 lockdep_assert_held(&ctx->lock);
1326
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001327 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1328 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1329 return;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001330
Stephane Eraniane5d13672011-02-14 11:20:01 +02001331 /*
1332 * in cgroup mode, time_enabled represents
1333 * the time the event was enabled AND active
1334 * tasks were in the monitored cgroup. This is
1335 * independent of the activity of the context as
1336 * there may be a mix of cgroup and non-cgroup events.
1337 *
1338 * That is why we treat cgroup events differently
1339 * here.
1340 */
1341 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001342 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001343 else if (ctx->is_active)
1344 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001345 else
1346 run_end = event->tstamp_stopped;
1347
1348 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001349
1350 if (event->state == PERF_EVENT_STATE_INACTIVE)
1351 run_end = event->tstamp_stopped;
1352 else
Stephane Eranian41587552011-01-03 18:20:01 +02001353 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001354
1355 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001356
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001357}
1358
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001359/*
1360 * Update total_time_enabled and total_time_running for all events in a group.
1361 */
1362static void update_group_times(struct perf_event *leader)
1363{
1364 struct perf_event *event;
1365
1366 update_event_times(leader);
1367 list_for_each_entry(event, &leader->sibling_list, group_entry)
1368 update_event_times(event);
1369}
1370
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001371static struct list_head *
1372ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1373{
1374 if (event->attr.pinned)
1375 return &ctx->pinned_groups;
1376 else
1377 return &ctx->flexible_groups;
1378}
1379
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001380/*
1381 * Add a event from the lists for its context.
1382 * Must be called with ctx->mutex and ctx->lock held.
1383 */
1384static void
1385list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1386{
Peter Zijlstrac994d612016-01-08 09:20:23 +01001387 lockdep_assert_held(&ctx->lock);
1388
Peter Zijlstra8a495422010-05-27 15:47:49 +02001389 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1390 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001391
1392 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001393 * If we're a stand alone event or group leader, we go to the context
1394 * list, group events are kept attached to the group so that
1395 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001396 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001397 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001398 struct list_head *list;
1399
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001400 if (is_software_event(event))
1401 event->group_flags |= PERF_GROUP_SOFTWARE;
1402
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001403 list = ctx_group_list(event, ctx);
1404 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001405 }
1406
Peter Zijlstra08309372011-03-03 11:31:20 +01001407 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001408 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001409
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001410 list_add_rcu(&event->event_entry, &ctx->event_list);
1411 ctx->nr_events++;
1412 if (event->attr.inherit_stat)
1413 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001414
1415 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001416}
1417
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001418/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001419 * Initialize event state based on the perf_event_attr::disabled.
1420 */
1421static inline void perf_event__state_init(struct perf_event *event)
1422{
1423 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1424 PERF_EVENT_STATE_INACTIVE;
1425}
1426
Peter Zijlstraa7239682015-09-09 19:06:33 +02001427static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001428{
1429 int entry = sizeof(u64); /* value */
1430 int size = 0;
1431 int nr = 1;
1432
1433 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1434 size += sizeof(u64);
1435
1436 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1437 size += sizeof(u64);
1438
1439 if (event->attr.read_format & PERF_FORMAT_ID)
1440 entry += sizeof(u64);
1441
1442 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001443 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001444 size += sizeof(u64);
1445 }
1446
1447 size += entry * nr;
1448 event->read_size = size;
1449}
1450
Peter Zijlstraa7239682015-09-09 19:06:33 +02001451static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001452{
1453 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001454 u16 size = 0;
1455
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001456 if (sample_type & PERF_SAMPLE_IP)
1457 size += sizeof(data->ip);
1458
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001459 if (sample_type & PERF_SAMPLE_ADDR)
1460 size += sizeof(data->addr);
1461
1462 if (sample_type & PERF_SAMPLE_PERIOD)
1463 size += sizeof(data->period);
1464
Andi Kleenc3feedf2013-01-24 16:10:28 +01001465 if (sample_type & PERF_SAMPLE_WEIGHT)
1466 size += sizeof(data->weight);
1467
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001468 if (sample_type & PERF_SAMPLE_READ)
1469 size += event->read_size;
1470
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001471 if (sample_type & PERF_SAMPLE_DATA_SRC)
1472 size += sizeof(data->data_src.val);
1473
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001474 if (sample_type & PERF_SAMPLE_TRANSACTION)
1475 size += sizeof(data->txn);
1476
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001477 event->header_size = size;
1478}
1479
Peter Zijlstraa7239682015-09-09 19:06:33 +02001480/*
1481 * Called at perf_event creation and when events are attached/detached from a
1482 * group.
1483 */
1484static void perf_event__header_size(struct perf_event *event)
1485{
1486 __perf_event_read_size(event,
1487 event->group_leader->nr_siblings);
1488 __perf_event_header_size(event, event->attr.sample_type);
1489}
1490
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001491static void perf_event__id_header_size(struct perf_event *event)
1492{
1493 struct perf_sample_data *data;
1494 u64 sample_type = event->attr.sample_type;
1495 u16 size = 0;
1496
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001497 if (sample_type & PERF_SAMPLE_TID)
1498 size += sizeof(data->tid_entry);
1499
1500 if (sample_type & PERF_SAMPLE_TIME)
1501 size += sizeof(data->time);
1502
Adrian Hunterff3d5272013-08-27 11:23:07 +03001503 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1504 size += sizeof(data->id);
1505
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001506 if (sample_type & PERF_SAMPLE_ID)
1507 size += sizeof(data->id);
1508
1509 if (sample_type & PERF_SAMPLE_STREAM_ID)
1510 size += sizeof(data->stream_id);
1511
1512 if (sample_type & PERF_SAMPLE_CPU)
1513 size += sizeof(data->cpu_entry);
1514
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001515 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001516}
1517
Peter Zijlstraa7239682015-09-09 19:06:33 +02001518static bool perf_event_validate_size(struct perf_event *event)
1519{
1520 /*
1521 * The values computed here will be over-written when we actually
1522 * attach the event.
1523 */
1524 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1525 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1526 perf_event__id_header_size(event);
1527
1528 /*
1529 * Sum the lot; should not exceed the 64k limit we have on records.
1530 * Conservative limit to allow for callchains and other variable fields.
1531 */
1532 if (event->read_size + event->header_size +
1533 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1534 return false;
1535
1536 return true;
1537}
1538
Peter Zijlstra8a495422010-05-27 15:47:49 +02001539static void perf_group_attach(struct perf_event *event)
1540{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001541 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001542
Peter Zijlstra74c33372010-10-15 11:40:29 +02001543 /*
1544 * We can have double attach due to group movement in perf_event_open.
1545 */
1546 if (event->attach_state & PERF_ATTACH_GROUP)
1547 return;
1548
Peter Zijlstra8a495422010-05-27 15:47:49 +02001549 event->attach_state |= PERF_ATTACH_GROUP;
1550
1551 if (group_leader == event)
1552 return;
1553
Peter Zijlstra652884f2015-01-23 11:20:10 +01001554 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1555
Peter Zijlstra8a495422010-05-27 15:47:49 +02001556 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1557 !is_software_event(event))
1558 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1559
1560 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1561 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001562
1563 perf_event__header_size(group_leader);
1564
1565 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1566 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001567}
1568
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001569/*
1570 * Remove a event from the lists for its context.
1571 * Must be called with ctx->mutex and ctx->lock held.
1572 */
1573static void
1574list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1575{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001576 struct perf_cpu_context *cpuctx;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001577
1578 WARN_ON_ONCE(event->ctx != ctx);
1579 lockdep_assert_held(&ctx->lock);
1580
Peter Zijlstra8a495422010-05-27 15:47:49 +02001581 /*
1582 * We can have double detach due to exit/hot-unplug + close.
1583 */
1584 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001585 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001586
1587 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1588
Stephane Eranian68cacd22011-03-23 16:03:06 +01001589 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001590 ctx->nr_cgroups--;
Peter Zijlstra70a01652016-01-08 09:29:16 +01001591 /*
1592 * Because cgroup events are always per-cpu events, this will
1593 * always be called from the right CPU.
1594 */
Stephane Eranian68cacd22011-03-23 16:03:06 +01001595 cpuctx = __get_cpu_context(ctx);
1596 /*
Peter Zijlstra70a01652016-01-08 09:29:16 +01001597 * If there are no more cgroup events then clear cgrp to avoid
1598 * stale pointer in update_cgrp_time_from_cpuctx().
Stephane Eranian68cacd22011-03-23 16:03:06 +01001599 */
1600 if (!ctx->nr_cgroups)
1601 cpuctx->cgrp = NULL;
1602 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001603
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001604 ctx->nr_events--;
1605 if (event->attr.inherit_stat)
1606 ctx->nr_stat--;
1607
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001608 list_del_rcu(&event->event_entry);
1609
Peter Zijlstra8a495422010-05-27 15:47:49 +02001610 if (event->group_leader == event)
1611 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001612
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001613 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001614
1615 /*
1616 * If event was in error state, then keep it
1617 * that way, otherwise bogus counts will be
1618 * returned on read(). The only way to get out
1619 * of error state is by explicit re-enabling
1620 * of the event
1621 */
1622 if (event->state > PERF_EVENT_STATE_OFF)
1623 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001624
1625 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001626}
1627
Peter Zijlstra8a495422010-05-27 15:47:49 +02001628static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001629{
1630 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001631 struct list_head *list = NULL;
1632
1633 /*
1634 * We can have double detach due to exit/hot-unplug + close.
1635 */
1636 if (!(event->attach_state & PERF_ATTACH_GROUP))
1637 return;
1638
1639 event->attach_state &= ~PERF_ATTACH_GROUP;
1640
1641 /*
1642 * If this is a sibling, remove it from its group.
1643 */
1644 if (event->group_leader != event) {
1645 list_del_init(&event->group_entry);
1646 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001647 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001648 }
1649
1650 if (!list_empty(&event->group_entry))
1651 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001652
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001653 /*
1654 * If this was a group event with sibling events then
1655 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001656 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001657 */
1658 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001659 if (list)
1660 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001661 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001662
1663 /* Inherit group flags from the previous leader */
1664 sibling->group_flags = event->group_flags;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001665
1666 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001667 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001668
1669out:
1670 perf_event__header_size(event->group_leader);
1671
1672 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1673 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001674}
1675
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001676static bool is_orphaned_event(struct perf_event *event)
1677{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01001678 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001679}
1680
Mark Rutland66eb5792015-05-13 17:12:23 +01001681static inline int pmu_filter_match(struct perf_event *event)
1682{
1683 struct pmu *pmu = event->pmu;
1684 return pmu->filter_match ? pmu->filter_match(event) : 1;
1685}
1686
Stephane Eranianfa66f072010-08-26 16:40:01 +02001687static inline int
1688event_filter_match(struct perf_event *event)
1689{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001690 return (event->cpu == -1 || event->cpu == smp_processor_id())
Mark Rutland66eb5792015-05-13 17:12:23 +01001691 && perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001692}
1693
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001694static void
1695event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001696 struct perf_cpu_context *cpuctx,
1697 struct perf_event_context *ctx)
1698{
Stephane Eranian41587552011-01-03 18:20:01 +02001699 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001700 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001701
1702 WARN_ON_ONCE(event->ctx != ctx);
1703 lockdep_assert_held(&ctx->lock);
1704
Stephane Eranianfa66f072010-08-26 16:40:01 +02001705 /*
1706 * An event which could not be activated because of
1707 * filter mismatch still needs to have its timings
1708 * maintained, otherwise bogus information is return
1709 * via read() for time_enabled, time_running:
1710 */
1711 if (event->state == PERF_EVENT_STATE_INACTIVE
1712 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001713 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001714 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001715 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001716 }
1717
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001718 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001719 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001720
Alexander Shishkin44377272013-12-16 14:17:36 +02001721 perf_pmu_disable(event->pmu);
1722
Peter Zijlstra28a967c2016-02-24 18:45:46 +01001723 event->tstamp_stopped = tstamp;
1724 event->pmu->del(event, 0);
1725 event->oncpu = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001726 event->state = PERF_EVENT_STATE_INACTIVE;
1727 if (event->pending_disable) {
1728 event->pending_disable = 0;
1729 event->state = PERF_EVENT_STATE_OFF;
1730 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001731
1732 if (!is_software_event(event))
1733 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001734 if (!--ctx->nr_active)
1735 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001736 if (event->attr.freq && event->attr.sample_freq)
1737 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001738 if (event->attr.exclusive || !cpuctx->active_oncpu)
1739 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001740
1741 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001742}
1743
1744static void
1745group_sched_out(struct perf_event *group_event,
1746 struct perf_cpu_context *cpuctx,
1747 struct perf_event_context *ctx)
1748{
1749 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001750 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001751
1752 event_sched_out(group_event, cpuctx, ctx);
1753
1754 /*
1755 * Schedule out siblings (if any):
1756 */
1757 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1758 event_sched_out(event, cpuctx, ctx);
1759
Stephane Eranianfa66f072010-08-26 16:40:01 +02001760 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001761 cpuctx->exclusive = 0;
1762}
1763
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001764#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01001765
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001766/*
1767 * Cross CPU call to remove a performance event
1768 *
1769 * We disable the event on the hardware level first. After that we
1770 * remove it from the context list.
1771 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001772static void
1773__perf_remove_from_context(struct perf_event *event,
1774 struct perf_cpu_context *cpuctx,
1775 struct perf_event_context *ctx,
1776 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001777{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001778 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001779
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001780 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001781 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001782 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001783 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001784
Peter Zijlstra39a43642016-01-11 12:46:35 +01001785 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001786 ctx->is_active = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01001787 if (ctx->task) {
1788 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1789 cpuctx->task_ctx = NULL;
1790 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001791 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001792}
1793
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001794/*
1795 * Remove the event from a task's (or a CPU's) list of events.
1796 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001797 * If event->ctx is a cloned context, callers must make sure that
1798 * every task struct that event->ctx->task could possibly point to
1799 * remains valid. This is OK when called from perf_release since
1800 * that only calls us on the top-level context, which can't be a clone.
1801 * When called from perf_event_exit_task, it's OK because the
1802 * context has been detached from its task.
1803 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001804static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001805{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001806 lockdep_assert_held(&event->ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001807
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001808 event_function_call(event, __perf_remove_from_context, (void *)flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001809}
1810
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001811/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001812 * Cross CPU call to disable a performance event
1813 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001814static void __perf_event_disable(struct perf_event *event,
1815 struct perf_cpu_context *cpuctx,
1816 struct perf_event_context *ctx,
1817 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001818{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001819 if (event->state < PERF_EVENT_STATE_INACTIVE)
1820 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001821
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001822 update_context_time(ctx);
1823 update_cgrp_time_from_event(event);
1824 update_group_times(event);
1825 if (event == event->group_leader)
1826 group_sched_out(event, cpuctx, ctx);
1827 else
1828 event_sched_out(event, cpuctx, ctx);
1829 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra7b648012015-12-03 18:35:21 +01001830}
1831
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001832/*
1833 * Disable a event.
1834 *
1835 * If event->ctx is a cloned context, callers must make sure that
1836 * every task struct that event->ctx->task could possibly point to
1837 * remains valid. This condition is satisifed when called through
1838 * perf_event_for_each_child or perf_event_for_each because they
1839 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001840 * goes to exit will block in perf_event_exit_event().
1841 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001842 * When called from perf_pending_event it's OK because event->ctx
1843 * is the current context on this CPU and preemption is disabled,
1844 * hence we can't get into perf_event_task_sched_out for this context.
1845 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001846static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001847{
1848 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001849
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001850 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001851 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001852 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001853 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001854 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001855 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001856
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001857 event_function_call(event, __perf_event_disable, NULL);
1858}
1859
1860void perf_event_disable_local(struct perf_event *event)
1861{
1862 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001863}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001864
1865/*
1866 * Strictly speaking kernel users cannot create groups and therefore this
1867 * interface does not need the perf_event_ctx_lock() magic.
1868 */
1869void perf_event_disable(struct perf_event *event)
1870{
1871 struct perf_event_context *ctx;
1872
1873 ctx = perf_event_ctx_lock(event);
1874 _perf_event_disable(event);
1875 perf_event_ctx_unlock(event, ctx);
1876}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001877EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001878
Stephane Eraniane5d13672011-02-14 11:20:01 +02001879static void perf_set_shadow_time(struct perf_event *event,
1880 struct perf_event_context *ctx,
1881 u64 tstamp)
1882{
1883 /*
1884 * use the correct time source for the time snapshot
1885 *
1886 * We could get by without this by leveraging the
1887 * fact that to get to this function, the caller
1888 * has most likely already called update_context_time()
1889 * and update_cgrp_time_xx() and thus both timestamp
1890 * are identical (or very close). Given that tstamp is,
1891 * already adjusted for cgroup, we could say that:
1892 * tstamp - ctx->timestamp
1893 * is equivalent to
1894 * tstamp - cgrp->timestamp.
1895 *
1896 * Then, in perf_output_read(), the calculation would
1897 * work with no changes because:
1898 * - event is guaranteed scheduled in
1899 * - no scheduled out in between
1900 * - thus the timestamp would be the same
1901 *
1902 * But this is a bit hairy.
1903 *
1904 * So instead, we have an explicit cgroup call to remain
1905 * within the time time source all along. We believe it
1906 * is cleaner and simpler to understand.
1907 */
1908 if (is_cgroup_event(event))
1909 perf_cgroup_set_shadow_time(event, tstamp);
1910 else
1911 event->shadow_ctx_time = tstamp - ctx->timestamp;
1912}
1913
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001914#define MAX_INTERRUPTS (~0ULL)
1915
1916static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001917static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001918
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001919static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001920event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001921 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001922 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001923{
Stephane Eranian41587552011-01-03 18:20:01 +02001924 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001925 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001926
Peter Zijlstra63342412014-05-05 11:49:16 +02001927 lockdep_assert_held(&ctx->lock);
1928
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001929 if (event->state <= PERF_EVENT_STATE_OFF)
1930 return 0;
1931
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02001932 WRITE_ONCE(event->oncpu, smp_processor_id());
1933 /*
1934 * Order event::oncpu write to happen before the ACTIVE state
1935 * is visible.
1936 */
1937 smp_wmb();
1938 WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001939
1940 /*
1941 * Unthrottle events, since we scheduled we might have missed several
1942 * ticks already, also for a heavily scheduling task there is little
1943 * guarantee it'll get a tick in a timely manner.
1944 */
1945 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1946 perf_log_throttle(event, 1);
1947 event->hw.interrupts = 0;
1948 }
1949
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001950 /*
1951 * The new state must be visible before we turn it on in the hardware:
1952 */
1953 smp_wmb();
1954
Alexander Shishkin44377272013-12-16 14:17:36 +02001955 perf_pmu_disable(event->pmu);
1956
Shaohua Li72f669c2015-02-05 15:55:31 -08001957 perf_set_shadow_time(event, ctx, tstamp);
1958
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001959 perf_log_itrace_start(event);
1960
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001961 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001962 event->state = PERF_EVENT_STATE_INACTIVE;
1963 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001964 ret = -EAGAIN;
1965 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001966 }
1967
Peter Zijlstra00a29162015-07-27 10:35:07 +02001968 event->tstamp_running += tstamp - event->tstamp_stopped;
1969
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001970 if (!is_software_event(event))
1971 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001972 if (!ctx->nr_active++)
1973 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001974 if (event->attr.freq && event->attr.sample_freq)
1975 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001976
1977 if (event->attr.exclusive)
1978 cpuctx->exclusive = 1;
1979
Alexander Shishkin44377272013-12-16 14:17:36 +02001980out:
1981 perf_pmu_enable(event->pmu);
1982
1983 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001984}
1985
1986static int
1987group_sched_in(struct perf_event *group_event,
1988 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001989 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001990{
Lin Ming6bde9b62010-04-23 13:56:00 +08001991 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001992 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001993 u64 now = ctx->time;
1994 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001995
1996 if (group_event->state == PERF_EVENT_STATE_OFF)
1997 return 0;
1998
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001999 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08002000
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002001 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002002 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02002003 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002004 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02002005 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002006
2007 /*
2008 * Schedule in siblings as one group (if any):
2009 */
2010 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002011 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002012 partial_group = event;
2013 goto group_error;
2014 }
2015 }
2016
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002017 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002018 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002019
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002020group_error:
2021 /*
2022 * Groups can be scheduled in as one unit only, so undo any
2023 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02002024 * The events up to the failed event are scheduled out normally,
2025 * tstamp_stopped will be updated.
2026 *
2027 * The failed events and the remaining siblings need to have
2028 * their timings updated as if they had gone thru event_sched_in()
2029 * and event_sched_out(). This is required to get consistent timings
2030 * across the group. This also takes care of the case where the group
2031 * could never be scheduled by ensuring tstamp_stopped is set to mark
2032 * the time the event was actually stopped, such that time delta
2033 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002034 */
2035 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2036 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02002037 simulate = true;
2038
2039 if (simulate) {
2040 event->tstamp_running += now - event->tstamp_stopped;
2041 event->tstamp_stopped = now;
2042 } else {
2043 event_sched_out(event, cpuctx, ctx);
2044 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002045 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002046 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002047
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002048 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002049
Peter Zijlstra272325c2015-04-15 11:41:58 +02002050 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002051
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002052 return -EAGAIN;
2053}
2054
2055/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002056 * Work out whether we can put this event group on the CPU now.
2057 */
2058static int group_can_go_on(struct perf_event *event,
2059 struct perf_cpu_context *cpuctx,
2060 int can_add_hw)
2061{
2062 /*
2063 * Groups consisting entirely of software events can always go on.
2064 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01002065 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002066 return 1;
2067 /*
2068 * If an exclusive group is already on, no other hardware
2069 * events can go on.
2070 */
2071 if (cpuctx->exclusive)
2072 return 0;
2073 /*
2074 * If this group is exclusive and there are already
2075 * events on the CPU, it can't go on.
2076 */
2077 if (event->attr.exclusive && cpuctx->active_oncpu)
2078 return 0;
2079 /*
2080 * Otherwise, try to add it if all previous groups were able
2081 * to go on.
2082 */
2083 return can_add_hw;
2084}
2085
2086static void add_event_to_ctx(struct perf_event *event,
2087 struct perf_event_context *ctx)
2088{
Stephane Eranian41587552011-01-03 18:20:01 +02002089 u64 tstamp = perf_event_time(event);
2090
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002091 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002092 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002093 event->tstamp_enabled = tstamp;
2094 event->tstamp_running = tstamp;
2095 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002096}
2097
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002098static void ctx_sched_out(struct perf_event_context *ctx,
2099 struct perf_cpu_context *cpuctx,
2100 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002101static void
2102ctx_sched_in(struct perf_event_context *ctx,
2103 struct perf_cpu_context *cpuctx,
2104 enum event_type_t event_type,
2105 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002106
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002107static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2108 struct perf_event_context *ctx)
2109{
2110 if (!cpuctx->task_ctx)
2111 return;
2112
2113 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2114 return;
2115
2116 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2117}
2118
Peter Zijlstradce58552011-04-09 21:17:46 +02002119static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2120 struct perf_event_context *ctx,
2121 struct task_struct *task)
2122{
2123 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2124 if (ctx)
2125 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2126 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2127 if (ctx)
2128 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2129}
2130
Peter Zijlstra3e349502016-01-08 10:01:18 +01002131static void ctx_resched(struct perf_cpu_context *cpuctx,
2132 struct perf_event_context *task_ctx)
Peter Zijlstra00179602015-11-30 16:26:35 +01002133{
Peter Zijlstra3e349502016-01-08 10:01:18 +01002134 perf_pmu_disable(cpuctx->ctx.pmu);
2135 if (task_ctx)
2136 task_ctx_sched_out(cpuctx, task_ctx);
2137 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2138 perf_event_sched_in(cpuctx, task_ctx, current);
2139 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002140}
2141
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002142/*
2143 * Cross CPU call to install and enable a performance event
2144 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002145 * Very similar to remote_function() + event_function() but cannot assume that
2146 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002147 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002148static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002149{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002150 struct perf_event *event = info;
2151 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002152 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002153 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002154 bool activate = true;
2155 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002156
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002157 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002158 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002159 raw_spin_lock(&ctx->lock);
2160 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002161
2162 /* If we're on the wrong CPU, try again */
2163 if (task_cpu(ctx->task) != smp_processor_id()) {
2164 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002165 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002166 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002167
Peter Zijlstra39a43642016-01-11 12:46:35 +01002168 /*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002169 * If we're on the right CPU, see if the task we target is
2170 * current, if not we don't have to activate the ctx, a future
2171 * context switch will do that for us.
Peter Zijlstra39a43642016-01-11 12:46:35 +01002172 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002173 if (ctx->task != current)
2174 activate = false;
2175 else
2176 WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx);
2177
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002178 } else if (task_ctx) {
2179 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002180 }
2181
Peter Zijlstraa0963092016-02-24 18:45:50 +01002182 if (activate) {
2183 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2184 add_event_to_ctx(event, ctx);
2185 ctx_resched(cpuctx, task_ctx);
2186 } else {
2187 add_event_to_ctx(event, ctx);
2188 }
2189
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002190unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002191 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002192
Peter Zijlstraa0963092016-02-24 18:45:50 +01002193 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002194}
2195
2196/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002197 * Attach a performance event to a context.
2198 *
2199 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002200 */
2201static void
2202perf_install_in_context(struct perf_event_context *ctx,
2203 struct perf_event *event,
2204 int cpu)
2205{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002206 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002207
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002208 lockdep_assert_held(&ctx->mutex);
2209
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002210 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002211 if (event->cpu != -1)
2212 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002213
Peter Zijlstraa0963092016-02-24 18:45:50 +01002214 if (!task) {
2215 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002216 return;
2217 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002218
Peter Zijlstraa0963092016-02-24 18:45:50 +01002219 /*
2220 * Should not happen, we validate the ctx is still alive before calling.
2221 */
2222 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2223 return;
2224
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002225 /*
2226 * Installing events is tricky because we cannot rely on ctx->is_active
2227 * to be set in case this is the nr_events 0 -> 1 transition.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002228 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002229again:
2230 /*
2231 * Cannot use task_function_call() because we need to run on the task's
2232 * CPU regardless of whether its current or not.
2233 */
2234 if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event))
2235 return;
2236
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002237 raw_spin_lock_irq(&ctx->lock);
2238 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002239 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2240 /*
2241 * Cannot happen because we already checked above (which also
2242 * cannot happen), and we hold ctx->mutex, which serializes us
2243 * against perf_event_exit_task_context().
2244 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002245 raw_spin_unlock_irq(&ctx->lock);
2246 return;
2247 }
Peter Zijlstra39a43642016-01-11 12:46:35 +01002248 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstraa0963092016-02-24 18:45:50 +01002249 /*
2250 * Since !ctx->is_active doesn't mean anything, we must IPI
2251 * unconditionally.
2252 */
2253 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002254}
2255
2256/*
2257 * Put a event into inactive state and update time fields.
2258 * Enabling the leader of a group effectively enables all
2259 * the group members that aren't explicitly disabled, so we
2260 * have to update their ->tstamp_enabled also.
2261 * Note: this works for group members as well as group leaders
2262 * since the non-leader members' sibling_lists will be empty.
2263 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002264static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002265{
2266 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002267 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002268
2269 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002270 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002271 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002272 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2273 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002274 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002275}
2276
2277/*
2278 * Cross CPU call to enable a performance event
2279 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002280static void __perf_event_enable(struct perf_event *event,
2281 struct perf_cpu_context *cpuctx,
2282 struct perf_event_context *ctx,
2283 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002284{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002285 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002286 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002287
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002288 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2289 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002290 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002291
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002292 if (ctx->is_active)
2293 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2294
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002295 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002296
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002297 if (!ctx->is_active)
2298 return;
2299
Stephane Eraniane5d13672011-02-14 11:20:01 +02002300 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002301 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02002302 perf_cgroup_defer_enabled(event);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002303 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002304 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002305 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002306
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002307 /*
2308 * If the event is in a group and isn't the group leader,
2309 * then don't put it on unless the group is on.
2310 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002311 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2312 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002313 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002314 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002315
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002316 task_ctx = cpuctx->task_ctx;
2317 if (ctx->task)
2318 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002319
Peter Zijlstraaee7dbc2016-01-08 10:45:11 +01002320 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002321}
2322
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002323/*
2324 * Enable a event.
2325 *
2326 * If event->ctx is a cloned context, callers must make sure that
2327 * every task struct that event->ctx->task could possibly point to
2328 * remains valid. This condition is satisfied when called through
2329 * perf_event_for_each_child or perf_event_for_each as described
2330 * for perf_event_disable.
2331 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002332static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002333{
2334 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002335
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002336 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002337 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2338 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002339 raw_spin_unlock_irq(&ctx->lock);
2340 return;
2341 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002342
2343 /*
2344 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002345 *
2346 * That way, if we see the event in error state below, we know that it
2347 * has gone back into error state, as distinct from the task having
2348 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002349 */
2350 if (event->state == PERF_EVENT_STATE_ERROR)
2351 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002352 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002353
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002354 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002355}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002356
2357/*
2358 * See perf_event_disable();
2359 */
2360void perf_event_enable(struct perf_event *event)
2361{
2362 struct perf_event_context *ctx;
2363
2364 ctx = perf_event_ctx_lock(event);
2365 _perf_event_enable(event);
2366 perf_event_ctx_unlock(event, ctx);
2367}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002368EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002369
Alexander Shishkin375637b2016-04-27 18:44:46 +03002370struct stop_event_data {
2371 struct perf_event *event;
2372 unsigned int restart;
2373};
2374
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002375static int __perf_event_stop(void *info)
2376{
Alexander Shishkin375637b2016-04-27 18:44:46 +03002377 struct stop_event_data *sd = info;
2378 struct perf_event *event = sd->event;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002379
Alexander Shishkin375637b2016-04-27 18:44:46 +03002380 /* if it's already INACTIVE, do nothing */
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002381 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2382 return 0;
2383
2384 /* matches smp_wmb() in event_sched_in() */
2385 smp_rmb();
2386
2387 /*
2388 * There is a window with interrupts enabled before we get here,
2389 * so we need to check again lest we try to stop another CPU's event.
2390 */
2391 if (READ_ONCE(event->oncpu) != smp_processor_id())
2392 return -EAGAIN;
2393
2394 event->pmu->stop(event, PERF_EF_UPDATE);
2395
Alexander Shishkin375637b2016-04-27 18:44:46 +03002396 /*
2397 * May race with the actual stop (through perf_pmu_output_stop()),
2398 * but it is only used for events with AUX ring buffer, and such
2399 * events will refuse to restart because of rb::aux_mmap_count==0,
2400 * see comments in perf_aux_output_begin().
2401 *
2402 * Since this is happening on a event-local CPU, no trace is lost
2403 * while restarting.
2404 */
2405 if (sd->restart)
2406 event->pmu->start(event, PERF_EF_START);
2407
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002408 return 0;
2409}
2410
Alexander Shishkin375637b2016-04-27 18:44:46 +03002411static int perf_event_restart(struct perf_event *event)
2412{
2413 struct stop_event_data sd = {
2414 .event = event,
2415 .restart = 1,
2416 };
2417 int ret = 0;
2418
2419 do {
2420 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2421 return 0;
2422
2423 /* matches smp_wmb() in event_sched_in() */
2424 smp_rmb();
2425
2426 /*
2427 * We only want to restart ACTIVE events, so if the event goes
2428 * inactive here (event->oncpu==-1), there's nothing more to do;
2429 * fall through with ret==-ENXIO.
2430 */
2431 ret = cpu_function_call(READ_ONCE(event->oncpu),
2432 __perf_event_stop, &sd);
2433 } while (ret == -EAGAIN);
2434
2435 return ret;
2436}
2437
2438/*
2439 * In order to contain the amount of racy and tricky in the address filter
2440 * configuration management, it is a two part process:
2441 *
2442 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
2443 * we update the addresses of corresponding vmas in
2444 * event::addr_filters_offs array and bump the event::addr_filters_gen;
2445 * (p2) when an event is scheduled in (pmu::add), it calls
2446 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
2447 * if the generation has changed since the previous call.
2448 *
2449 * If (p1) happens while the event is active, we restart it to force (p2).
2450 *
2451 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
2452 * pre-existing mappings, called once when new filters arrive via SET_FILTER
2453 * ioctl;
2454 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
2455 * registered mapping, called for every new mmap(), with mm::mmap_sem down
2456 * for reading;
2457 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
2458 * of exec.
2459 */
2460void perf_event_addr_filters_sync(struct perf_event *event)
2461{
2462 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
2463
2464 if (!has_addr_filter(event))
2465 return;
2466
2467 raw_spin_lock(&ifh->lock);
2468 if (event->addr_filters_gen != event->hw.addr_filters_gen) {
2469 event->pmu->addr_filters_sync(event);
2470 event->hw.addr_filters_gen = event->addr_filters_gen;
2471 }
2472 raw_spin_unlock(&ifh->lock);
2473}
2474EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
2475
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002476static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002477{
2478 /*
2479 * not supported on inherited events
2480 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002481 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002482 return -EINVAL;
2483
2484 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002485 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002486
2487 return 0;
2488}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002489
2490/*
2491 * See perf_event_disable()
2492 */
2493int perf_event_refresh(struct perf_event *event, int refresh)
2494{
2495 struct perf_event_context *ctx;
2496 int ret;
2497
2498 ctx = perf_event_ctx_lock(event);
2499 ret = _perf_event_refresh(event, refresh);
2500 perf_event_ctx_unlock(event, ctx);
2501
2502 return ret;
2503}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002504EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002505
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002506static void ctx_sched_out(struct perf_event_context *ctx,
2507 struct perf_cpu_context *cpuctx,
2508 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002509{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002510 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002511 struct perf_event *event;
2512
2513 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002514
Peter Zijlstra39a43642016-01-11 12:46:35 +01002515 if (likely(!ctx->nr_events)) {
2516 /*
2517 * See __perf_remove_from_context().
2518 */
2519 WARN_ON_ONCE(ctx->is_active);
2520 if (ctx->task)
2521 WARN_ON_ONCE(cpuctx->task_ctx);
2522 return;
2523 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002524
Peter Zijlstradb24d332011-04-09 21:17:45 +02002525 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002526 if (!(ctx->is_active & EVENT_ALL))
2527 ctx->is_active = 0;
2528
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002529 if (ctx->task) {
2530 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2531 if (!ctx->is_active)
2532 cpuctx->task_ctx = NULL;
2533 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002534
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002535 /*
2536 * Always update time if it was set; not only when it changes.
2537 * Otherwise we can 'forget' to update time for any but the last
2538 * context we sched out. For example:
2539 *
2540 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2541 * ctx_sched_out(.event_type = EVENT_PINNED)
2542 *
2543 * would only update time for the pinned events.
2544 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002545 if (is_active & EVENT_TIME) {
2546 /* update (and stop) ctx time */
2547 update_context_time(ctx);
2548 update_cgrp_time_from_cpuctx(cpuctx);
2549 }
2550
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002551 is_active ^= ctx->is_active; /* changed bits */
2552
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002553 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002554 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002555
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002556 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002557 if (is_active & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002558 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2559 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002560 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002561
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002562 if (is_active & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002563 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002564 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002565 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002566 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002567}
2568
2569/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002570 * Test whether two contexts are equivalent, i.e. whether they have both been
2571 * cloned from the same version of the same context.
2572 *
2573 * Equivalence is measured using a generation number in the context that is
2574 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2575 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002576 */
2577static int context_equiv(struct perf_event_context *ctx1,
2578 struct perf_event_context *ctx2)
2579{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002580 lockdep_assert_held(&ctx1->lock);
2581 lockdep_assert_held(&ctx2->lock);
2582
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002583 /* Pinning disables the swap optimization */
2584 if (ctx1->pin_count || ctx2->pin_count)
2585 return 0;
2586
2587 /* If ctx1 is the parent of ctx2 */
2588 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2589 return 1;
2590
2591 /* If ctx2 is the parent of ctx1 */
2592 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2593 return 1;
2594
2595 /*
2596 * If ctx1 and ctx2 have the same parent; we flatten the parent
2597 * hierarchy, see perf_event_init_context().
2598 */
2599 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2600 ctx1->parent_gen == ctx2->parent_gen)
2601 return 1;
2602
2603 /* Unmatched */
2604 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002605}
2606
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002607static void __perf_event_sync_stat(struct perf_event *event,
2608 struct perf_event *next_event)
2609{
2610 u64 value;
2611
2612 if (!event->attr.inherit_stat)
2613 return;
2614
2615 /*
2616 * Update the event value, we cannot use perf_event_read()
2617 * because we're in the middle of a context switch and have IRQs
2618 * disabled, which upsets smp_call_function_single(), however
2619 * we know the event must be on the current CPU, therefore we
2620 * don't need to use it.
2621 */
2622 switch (event->state) {
2623 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002624 event->pmu->read(event);
2625 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002626
2627 case PERF_EVENT_STATE_INACTIVE:
2628 update_event_times(event);
2629 break;
2630
2631 default:
2632 break;
2633 }
2634
2635 /*
2636 * In order to keep per-task stats reliable we need to flip the event
2637 * values when we flip the contexts.
2638 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002639 value = local64_read(&next_event->count);
2640 value = local64_xchg(&event->count, value);
2641 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002642
2643 swap(event->total_time_enabled, next_event->total_time_enabled);
2644 swap(event->total_time_running, next_event->total_time_running);
2645
2646 /*
2647 * Since we swizzled the values, update the user visible data too.
2648 */
2649 perf_event_update_userpage(event);
2650 perf_event_update_userpage(next_event);
2651}
2652
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002653static void perf_event_sync_stat(struct perf_event_context *ctx,
2654 struct perf_event_context *next_ctx)
2655{
2656 struct perf_event *event, *next_event;
2657
2658 if (!ctx->nr_stat)
2659 return;
2660
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002661 update_context_time(ctx);
2662
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002663 event = list_first_entry(&ctx->event_list,
2664 struct perf_event, event_entry);
2665
2666 next_event = list_first_entry(&next_ctx->event_list,
2667 struct perf_event, event_entry);
2668
2669 while (&event->event_entry != &ctx->event_list &&
2670 &next_event->event_entry != &next_ctx->event_list) {
2671
2672 __perf_event_sync_stat(event, next_event);
2673
2674 event = list_next_entry(event, event_entry);
2675 next_event = list_next_entry(next_event, event_entry);
2676 }
2677}
2678
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002679static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2680 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002681{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002682 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002683 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002684 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002685 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002686 int do_switch = 1;
2687
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002688 if (likely(!ctx))
2689 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002690
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002691 cpuctx = __get_cpu_context(ctx);
2692 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002693 return;
2694
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002695 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002696 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002697 if (!next_ctx)
2698 goto unlock;
2699
2700 parent = rcu_dereference(ctx->parent_ctx);
2701 next_parent = rcu_dereference(next_ctx->parent_ctx);
2702
2703 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002704 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002705 goto unlock;
2706
2707 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002708 /*
2709 * Looks like the two contexts are clones, so we might be
2710 * able to optimize the context switch. We lock both
2711 * contexts and check that they are clones under the
2712 * lock (including re-checking that neither has been
2713 * uncloned in the meantime). It doesn't matter which
2714 * order we take the locks because no other cpu could
2715 * be trying to lock both of these tasks.
2716 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002717 raw_spin_lock(&ctx->lock);
2718 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002719 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002720 WRITE_ONCE(ctx->task, next);
2721 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05002722
2723 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2724
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002725 /*
2726 * RCU_INIT_POINTER here is safe because we've not
2727 * modified the ctx and the above modification of
2728 * ctx->task and ctx->task_ctx_data are immaterial
2729 * since those values are always verified under
2730 * ctx->lock which we're now holding.
2731 */
2732 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2733 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2734
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002735 do_switch = 0;
2736
2737 perf_event_sync_stat(ctx, next_ctx);
2738 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002739 raw_spin_unlock(&next_ctx->lock);
2740 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002741 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002742unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002743 rcu_read_unlock();
2744
2745 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002746 raw_spin_lock(&ctx->lock);
Peter Zijlstra8833d0e2016-01-08 10:02:37 +01002747 task_ctx_sched_out(cpuctx, ctx);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002748 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002749 }
2750}
2751
Yan, Zhengba532502014-11-04 21:55:58 -05002752void perf_sched_cb_dec(struct pmu *pmu)
2753{
2754 this_cpu_dec(perf_sched_cb_usages);
2755}
2756
2757void perf_sched_cb_inc(struct pmu *pmu)
2758{
2759 this_cpu_inc(perf_sched_cb_usages);
2760}
2761
2762/*
2763 * This function provides the context switch callback to the lower code
2764 * layer. It is invoked ONLY when the context switch callback is enabled.
2765 */
2766static void perf_pmu_sched_task(struct task_struct *prev,
2767 struct task_struct *next,
2768 bool sched_in)
2769{
2770 struct perf_cpu_context *cpuctx;
2771 struct pmu *pmu;
2772 unsigned long flags;
2773
2774 if (prev == next)
2775 return;
2776
2777 local_irq_save(flags);
2778
2779 rcu_read_lock();
2780
2781 list_for_each_entry_rcu(pmu, &pmus, entry) {
2782 if (pmu->sched_task) {
2783 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2784
2785 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2786
2787 perf_pmu_disable(pmu);
2788
2789 pmu->sched_task(cpuctx->task_ctx, sched_in);
2790
2791 perf_pmu_enable(pmu);
2792
2793 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2794 }
2795 }
2796
2797 rcu_read_unlock();
2798
2799 local_irq_restore(flags);
2800}
2801
Adrian Hunter45ac1402015-07-21 12:44:02 +03002802static void perf_event_switch(struct task_struct *task,
2803 struct task_struct *next_prev, bool sched_in);
2804
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002805#define for_each_task_context_nr(ctxn) \
2806 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2807
2808/*
2809 * Called from scheduler to remove the events of the current task,
2810 * with interrupts disabled.
2811 *
2812 * We stop each event and update the event value in event->count.
2813 *
2814 * This does not protect us against NMI, but disable()
2815 * sets the disabled bit in the control field of event _before_
2816 * accessing the event control register. If a NMI hits, then it will
2817 * not restart the event.
2818 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002819void __perf_event_task_sched_out(struct task_struct *task,
2820 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002821{
2822 int ctxn;
2823
Yan, Zhengba532502014-11-04 21:55:58 -05002824 if (__this_cpu_read(perf_sched_cb_usages))
2825 perf_pmu_sched_task(task, next, false);
2826
Adrian Hunter45ac1402015-07-21 12:44:02 +03002827 if (atomic_read(&nr_switch_events))
2828 perf_event_switch(task, next, false);
2829
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002830 for_each_task_context_nr(ctxn)
2831 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002832
2833 /*
2834 * if cgroup events exist on this CPU, then we need
2835 * to check if we have to switch out PMU state.
2836 * cgroup event are system-wide mode only
2837 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002838 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002839 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002840}
2841
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002842/*
2843 * Called with IRQs disabled
2844 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002845static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2846 enum event_type_t event_type)
2847{
2848 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002849}
2850
2851static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002852ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002853 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002854{
2855 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002856
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002857 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2858 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002859 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002860 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002861 continue;
2862
Stephane Eraniane5d13672011-02-14 11:20:01 +02002863 /* may need to reset tstamp_enabled */
2864 if (is_cgroup_event(event))
2865 perf_cgroup_mark_enabled(event, ctx);
2866
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002867 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002868 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002869
2870 /*
2871 * If this pinned group hasn't been scheduled,
2872 * put it in error state.
2873 */
2874 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2875 update_group_times(event);
2876 event->state = PERF_EVENT_STATE_ERROR;
2877 }
2878 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002879}
2880
2881static void
2882ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002883 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002884{
2885 struct perf_event *event;
2886 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002887
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002888 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2889 /* Ignore events in OFF or ERROR state */
2890 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002891 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002892 /*
2893 * Listen to the 'cpu' scheduling filter constraint
2894 * of events:
2895 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002896 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002897 continue;
2898
Stephane Eraniane5d13672011-02-14 11:20:01 +02002899 /* may need to reset tstamp_enabled */
2900 if (is_cgroup_event(event))
2901 perf_cgroup_mark_enabled(event, ctx);
2902
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002903 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002904 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002905 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002906 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002907 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002908}
2909
2910static void
2911ctx_sched_in(struct perf_event_context *ctx,
2912 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002913 enum event_type_t event_type,
2914 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002915{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002916 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002917 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002918
Peter Zijlstrac994d612016-01-08 09:20:23 +01002919 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002920
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002921 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002922 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002923
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002924 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002925 if (ctx->task) {
2926 if (!is_active)
2927 cpuctx->task_ctx = ctx;
2928 else
2929 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2930 }
2931
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002932 is_active ^= ctx->is_active; /* changed bits */
2933
2934 if (is_active & EVENT_TIME) {
2935 /* start ctx time */
2936 now = perf_clock();
2937 ctx->timestamp = now;
2938 perf_cgroup_set_timestamp(task, ctx);
2939 }
2940
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002941 /*
2942 * First go through the list and put on any pinned groups
2943 * in order to give them the best chance of going on.
2944 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002945 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002946 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002947
2948 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002949 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002950 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002951}
2952
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002953static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002954 enum event_type_t event_type,
2955 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002956{
2957 struct perf_event_context *ctx = &cpuctx->ctx;
2958
Stephane Eraniane5d13672011-02-14 11:20:01 +02002959 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002960}
2961
Stephane Eraniane5d13672011-02-14 11:20:01 +02002962static void perf_event_context_sched_in(struct perf_event_context *ctx,
2963 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002964{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002965 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002966
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002967 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002968 if (cpuctx->task_ctx == ctx)
2969 return;
2970
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002971 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002972 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002973 /*
2974 * We want to keep the following priority order:
2975 * cpu pinned (that don't need to move), task pinned,
2976 * cpu flexible, task flexible.
2977 */
2978 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002979 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002980 perf_pmu_enable(ctx->pmu);
2981 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002982}
2983
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002984/*
2985 * Called from scheduler to add the events of the current task
2986 * with interrupts disabled.
2987 *
2988 * We restore the event value and then enable it.
2989 *
2990 * This does not protect us against NMI, but enable()
2991 * sets the enabled bit in the control field of event _before_
2992 * accessing the event control register. If a NMI hits, then it will
2993 * keep the event running.
2994 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002995void __perf_event_task_sched_in(struct task_struct *prev,
2996 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002997{
2998 struct perf_event_context *ctx;
2999 int ctxn;
3000
Peter Zijlstra7e41d172016-01-08 09:21:40 +01003001 /*
3002 * If cgroup events exist on this CPU, then we need to check if we have
3003 * to switch in PMU state; cgroup event are system-wide mode only.
3004 *
3005 * Since cgroup events are CPU events, we must schedule these in before
3006 * we schedule in the task events.
3007 */
3008 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3009 perf_cgroup_sched_in(prev, task);
3010
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003011 for_each_task_context_nr(ctxn) {
3012 ctx = task->perf_event_ctxp[ctxn];
3013 if (likely(!ctx))
3014 continue;
3015
Stephane Eraniane5d13672011-02-14 11:20:01 +02003016 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003017 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003018
Adrian Hunter45ac1402015-07-21 12:44:02 +03003019 if (atomic_read(&nr_switch_events))
3020 perf_event_switch(task, prev, true);
3021
Yan, Zhengba532502014-11-04 21:55:58 -05003022 if (__this_cpu_read(perf_sched_cb_usages))
3023 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024}
3025
Peter Zijlstraabd50712010-01-26 18:50:16 +01003026static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3027{
3028 u64 frequency = event->attr.sample_freq;
3029 u64 sec = NSEC_PER_SEC;
3030 u64 divisor, dividend;
3031
3032 int count_fls, nsec_fls, frequency_fls, sec_fls;
3033
3034 count_fls = fls64(count);
3035 nsec_fls = fls64(nsec);
3036 frequency_fls = fls64(frequency);
3037 sec_fls = 30;
3038
3039 /*
3040 * We got @count in @nsec, with a target of sample_freq HZ
3041 * the target period becomes:
3042 *
3043 * @count * 10^9
3044 * period = -------------------
3045 * @nsec * sample_freq
3046 *
3047 */
3048
3049 /*
3050 * Reduce accuracy by one bit such that @a and @b converge
3051 * to a similar magnitude.
3052 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003053#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01003054do { \
3055 if (a##_fls > b##_fls) { \
3056 a >>= 1; \
3057 a##_fls--; \
3058 } else { \
3059 b >>= 1; \
3060 b##_fls--; \
3061 } \
3062} while (0)
3063
3064 /*
3065 * Reduce accuracy until either term fits in a u64, then proceed with
3066 * the other, so that finally we can do a u64/u64 division.
3067 */
3068 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3069 REDUCE_FLS(nsec, frequency);
3070 REDUCE_FLS(sec, count);
3071 }
3072
3073 if (count_fls + sec_fls > 64) {
3074 divisor = nsec * frequency;
3075
3076 while (count_fls + sec_fls > 64) {
3077 REDUCE_FLS(count, sec);
3078 divisor >>= 1;
3079 }
3080
3081 dividend = count * sec;
3082 } else {
3083 dividend = count * sec;
3084
3085 while (nsec_fls + frequency_fls > 64) {
3086 REDUCE_FLS(nsec, frequency);
3087 dividend >>= 1;
3088 }
3089
3090 divisor = nsec * frequency;
3091 }
3092
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003093 if (!divisor)
3094 return dividend;
3095
Peter Zijlstraabd50712010-01-26 18:50:16 +01003096 return div64_u64(dividend, divisor);
3097}
3098
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003099static DEFINE_PER_CPU(int, perf_throttled_count);
3100static DEFINE_PER_CPU(u64, perf_throttled_seq);
3101
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003102static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003103{
3104 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003105 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003106 s64 delta;
3107
Peter Zijlstraabd50712010-01-26 18:50:16 +01003108 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003109
3110 delta = (s64)(period - hwc->sample_period);
3111 delta = (delta + 7) / 8; /* low pass filter */
3112
3113 sample_period = hwc->sample_period + delta;
3114
3115 if (!sample_period)
3116 sample_period = 1;
3117
3118 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003119
Peter Zijlstrae7850592010-05-21 14:43:08 +02003120 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003121 if (disable)
3122 event->pmu->stop(event, PERF_EF_UPDATE);
3123
Peter Zijlstrae7850592010-05-21 14:43:08 +02003124 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003125
3126 if (disable)
3127 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003128 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003129}
3130
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003131/*
3132 * combine freq adjustment with unthrottling to avoid two passes over the
3133 * events. At the same time, make sure, having freq events does not change
3134 * the rate of unthrottling as that would introduce bias.
3135 */
3136static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3137 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003138{
3139 struct perf_event *event;
3140 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003141 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003142 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003143
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003144 /*
3145 * only need to iterate over all events iff:
3146 * - context have events in frequency mode (needs freq adjust)
3147 * - there are events to unthrottle on this cpu
3148 */
3149 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003150 return;
3151
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003152 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003153 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003154
Paul Mackerras03541f82009-10-14 16:58:03 +11003155 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003156 if (event->state != PERF_EVENT_STATE_ACTIVE)
3157 continue;
3158
Stephane Eranian5632ab12011-01-03 18:20:01 +02003159 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003160 continue;
3161
Alexander Shishkin44377272013-12-16 14:17:36 +02003162 perf_pmu_disable(event->pmu);
3163
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003164 hwc = &event->hw;
3165
Jiri Olsaae23bff2013-08-24 16:45:54 +02003166 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003167 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003168 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003169 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003170 }
3171
3172 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003173 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003174
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003175 /*
3176 * stop the event and update event->count
3177 */
3178 event->pmu->stop(event, PERF_EF_UPDATE);
3179
Peter Zijlstrae7850592010-05-21 14:43:08 +02003180 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003181 delta = now - hwc->freq_count_stamp;
3182 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003183
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003184 /*
3185 * restart the event
3186 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003187 * we have stopped the event so tell that
3188 * to perf_adjust_period() to avoid stopping it
3189 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003190 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003191 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003192 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003193
3194 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003195 next:
3196 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003197 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003198
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003199 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003200 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003201}
3202
3203/*
3204 * Round-robin a context's events:
3205 */
3206static void rotate_ctx(struct perf_event_context *ctx)
3207{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003208 /*
3209 * Rotate the first entry last of non-pinned groups. Rotation might be
3210 * disabled by the inheritance code.
3211 */
3212 if (!ctx->rotate_disable)
3213 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003214}
3215
Stephane Eranian9e630202013-04-03 14:21:33 +02003216static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003217{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003218 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003219 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003220
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003221 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003222 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3223 rotate = 1;
3224 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003225
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003226 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003227 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003228 if (ctx->nr_events != ctx->nr_active)
3229 rotate = 1;
3230 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003231
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003232 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003233 goto done;
3234
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003235 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003236 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003237
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003238 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3239 if (ctx)
3240 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003241
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003242 rotate_ctx(&cpuctx->ctx);
3243 if (ctx)
3244 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003245
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003246 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003247
3248 perf_pmu_enable(cpuctx->ctx.pmu);
3249 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003250done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003251
3252 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003253}
3254
3255void perf_event_task_tick(void)
3256{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003257 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3258 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003259 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003260
3261 WARN_ON(!irqs_disabled());
3262
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003263 __this_cpu_inc(perf_throttled_seq);
3264 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003265 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003266
Mark Rutland2fde4f92015-01-07 15:01:54 +00003267 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003268 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003269}
3270
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003271static int event_enable_on_exec(struct perf_event *event,
3272 struct perf_event_context *ctx)
3273{
3274 if (!event->attr.enable_on_exec)
3275 return 0;
3276
3277 event->attr.enable_on_exec = 0;
3278 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3279 return 0;
3280
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003281 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003282
3283 return 1;
3284}
3285
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003286/*
3287 * Enable all of a task's events that have been marked enable-on-exec.
3288 * This expects task == current.
3289 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003290static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003291{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003292 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003293 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003294 struct perf_event *event;
3295 unsigned long flags;
3296 int enabled = 0;
3297
3298 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003299 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003300 if (!ctx || !ctx->nr_events)
3301 goto out;
3302
Peter Zijlstra3e349502016-01-08 10:01:18 +01003303 cpuctx = __get_cpu_context(ctx);
3304 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01003305 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003306 list_for_each_entry(event, &ctx->event_list, event_entry)
3307 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003308
3309 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003310 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003311 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003312 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003313 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003314 ctx_resched(cpuctx, ctx);
3315 }
3316 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003317
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003318out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003319 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003320
3321 if (clone_ctx)
3322 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003323}
3324
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003325struct perf_read_data {
3326 struct perf_event *event;
3327 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003328 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003329};
3330
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003331/*
3332 * Cross CPU call to read the hardware event
3333 */
3334static void __perf_event_read(void *info)
3335{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003336 struct perf_read_data *data = info;
3337 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003338 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003339 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003340 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003341
3342 /*
3343 * If this is a task context, we need to check whether it is
3344 * the current task context of this cpu. If not it has been
3345 * scheduled out before the smp call arrived. In that case
3346 * event->count would have been updated to a recent sample
3347 * when the event was scheduled out.
3348 */
3349 if (ctx->task && cpuctx->task_ctx != ctx)
3350 return;
3351
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003352 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003353 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003354 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003355 update_cgrp_time_from_event(event);
3356 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003357
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003358 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003359 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003360 goto unlock;
3361
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003362 if (!data->group) {
3363 pmu->read(event);
3364 data->ret = 0;
3365 goto unlock;
3366 }
3367
3368 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3369
3370 pmu->read(event);
3371
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003372 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3373 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003374 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3375 /*
3376 * Use sibling's PMU rather than @event's since
3377 * sibling could be on different (eg: software) PMU.
3378 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003379 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003380 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003381 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003382
3383 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003384
3385unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003386 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003387}
3388
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003389static inline u64 perf_event_count(struct perf_event *event)
3390{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003391 if (event->pmu->count)
3392 return event->pmu->count(event);
3393
3394 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003395}
3396
Kaixu Xiaffe86902015-08-06 07:02:32 +00003397/*
3398 * NMI-safe method to read a local event, that is an event that
3399 * is:
3400 * - either for the current task, or for this CPU
3401 * - does not have inherit set, for inherited task events
3402 * will not be local and we cannot read them atomically
3403 * - must not have a pmu::count method
3404 */
3405u64 perf_event_read_local(struct perf_event *event)
3406{
3407 unsigned long flags;
3408 u64 val;
3409
3410 /*
3411 * Disabling interrupts avoids all counter scheduling (context
3412 * switches, timer based rotation and IPIs).
3413 */
3414 local_irq_save(flags);
3415
3416 /* If this is a per-task event, it must be for current */
3417 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3418 event->hw.target != current);
3419
3420 /* If this is a per-CPU event, it must be for this CPU */
3421 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3422 event->cpu != smp_processor_id());
3423
3424 /*
3425 * It must not be an event with inherit set, we cannot read
3426 * all child counters from atomic context.
3427 */
3428 WARN_ON_ONCE(event->attr.inherit);
3429
3430 /*
3431 * It must not have a pmu::count method, those are not
3432 * NMI safe.
3433 */
3434 WARN_ON_ONCE(event->pmu->count);
3435
3436 /*
3437 * If the event is currently on this CPU, its either a per-task event,
3438 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3439 * oncpu == -1).
3440 */
3441 if (event->oncpu == smp_processor_id())
3442 event->pmu->read(event);
3443
3444 val = local64_read(&event->count);
3445 local_irq_restore(flags);
3446
3447 return val;
3448}
3449
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003450static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003451{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003452 int ret = 0;
3453
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003454 /*
3455 * If event is enabled and currently active on a CPU, update the
3456 * value in the event structure:
3457 */
3458 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003459 struct perf_read_data data = {
3460 .event = event,
3461 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003462 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003463 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003464 smp_call_function_single(event->oncpu,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003465 __perf_event_read, &data, 1);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003466 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003467 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003468 struct perf_event_context *ctx = event->ctx;
3469 unsigned long flags;
3470
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003471 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003472 /*
3473 * may read while context is not active
3474 * (e.g., thread is blocked), in that case
3475 * we cannot update context time
3476 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003477 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003478 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003479 update_cgrp_time_from_event(event);
3480 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003481 if (group)
3482 update_group_times(event);
3483 else
3484 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003485 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003486 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003487
3488 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003489}
3490
3491/*
3492 * Initialize the perf_event context in a task_struct:
3493 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003494static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003495{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003496 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003497 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003498 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003499 INIT_LIST_HEAD(&ctx->pinned_groups);
3500 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003501 INIT_LIST_HEAD(&ctx->event_list);
3502 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003503}
3504
Peter Zijlstraeb184472010-09-07 15:55:13 +02003505static struct perf_event_context *
3506alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003507{
3508 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003509
3510 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3511 if (!ctx)
3512 return NULL;
3513
3514 __perf_event_init_context(ctx);
3515 if (task) {
3516 ctx->task = task;
3517 get_task_struct(task);
3518 }
3519 ctx->pmu = pmu;
3520
3521 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003522}
3523
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003524static struct task_struct *
3525find_lively_task_by_vpid(pid_t vpid)
3526{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003527 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003528
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003529 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003530 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003531 task = current;
3532 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003533 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003534 if (task)
3535 get_task_struct(task);
3536 rcu_read_unlock();
3537
3538 if (!task)
3539 return ERR_PTR(-ESRCH);
3540
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003541 return task;
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003542}
3543
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003544/*
3545 * Returns a matching context with refcount and pincount.
3546 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003547static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003548find_get_context(struct pmu *pmu, struct task_struct *task,
3549 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003550{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003551 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003552 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003553 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003554 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003555 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003556 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003557
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003558 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003559 /* Must be root to operate on a CPU event: */
3560 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3561 return ERR_PTR(-EACCES);
3562
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003563 /*
3564 * We could be clever and allow to attach a event to an
3565 * offline CPU and activate it when the CPU comes up, but
3566 * that's for later.
3567 */
3568 if (!cpu_online(cpu))
3569 return ERR_PTR(-ENODEV);
3570
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003571 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003572 ctx = &cpuctx->ctx;
3573 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003574 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003575
3576 return ctx;
3577 }
3578
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003579 err = -EINVAL;
3580 ctxn = pmu->task_ctx_nr;
3581 if (ctxn < 0)
3582 goto errout;
3583
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003584 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3585 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3586 if (!task_ctx_data) {
3587 err = -ENOMEM;
3588 goto errout;
3589 }
3590 }
3591
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003592retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003593 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003594 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003595 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003596 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003597
3598 if (task_ctx_data && !ctx->task_ctx_data) {
3599 ctx->task_ctx_data = task_ctx_data;
3600 task_ctx_data = NULL;
3601 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003602 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003603
3604 if (clone_ctx)
3605 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003606 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003607 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003608 err = -ENOMEM;
3609 if (!ctx)
3610 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003611
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003612 if (task_ctx_data) {
3613 ctx->task_ctx_data = task_ctx_data;
3614 task_ctx_data = NULL;
3615 }
3616
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003617 err = 0;
3618 mutex_lock(&task->perf_event_mutex);
3619 /*
3620 * If it has already passed perf_event_exit_task().
3621 * we must see PF_EXITING, it takes this mutex too.
3622 */
3623 if (task->flags & PF_EXITING)
3624 err = -ESRCH;
3625 else if (task->perf_event_ctxp[ctxn])
3626 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003627 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003628 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003629 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003630 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003631 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003632 mutex_unlock(&task->perf_event_mutex);
3633
3634 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003635 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003636
3637 if (err == -EAGAIN)
3638 goto retry;
3639 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003640 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003641 }
3642
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003643 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003644 return ctx;
3645
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003646errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003647 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003648 return ERR_PTR(err);
3649}
3650
Li Zefan6fb29152009-10-15 11:21:42 +08003651static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003652static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003653
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003654static void free_event_rcu(struct rcu_head *head)
3655{
3656 struct perf_event *event;
3657
3658 event = container_of(head, struct perf_event, rcu_head);
3659 if (event->ns)
3660 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003661 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003662 kfree(event);
3663}
3664
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003665static void ring_buffer_attach(struct perf_event *event,
3666 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003667
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003668static void unaccount_event_cpu(struct perf_event *event, int cpu)
3669{
3670 if (event->parent)
3671 return;
3672
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003673 if (is_cgroup_event(event))
3674 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3675}
3676
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003677#ifdef CONFIG_NO_HZ_FULL
3678static DEFINE_SPINLOCK(nr_freq_lock);
3679#endif
3680
3681static void unaccount_freq_event_nohz(void)
3682{
3683#ifdef CONFIG_NO_HZ_FULL
3684 spin_lock(&nr_freq_lock);
3685 if (atomic_dec_and_test(&nr_freq_events))
3686 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
3687 spin_unlock(&nr_freq_lock);
3688#endif
3689}
3690
3691static void unaccount_freq_event(void)
3692{
3693 if (tick_nohz_full_enabled())
3694 unaccount_freq_event_nohz();
3695 else
3696 atomic_dec(&nr_freq_events);
3697}
3698
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003699static void unaccount_event(struct perf_event *event)
3700{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003701 bool dec = false;
3702
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003703 if (event->parent)
3704 return;
3705
3706 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003707 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003708 if (event->attr.mmap || event->attr.mmap_data)
3709 atomic_dec(&nr_mmap_events);
3710 if (event->attr.comm)
3711 atomic_dec(&nr_comm_events);
3712 if (event->attr.task)
3713 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003714 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003715 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03003716 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003717 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003718 atomic_dec(&nr_switch_events);
3719 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003720 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003721 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003722 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003723 dec = true;
3724
Peter Zijlstra9107c892016-02-24 18:45:45 +01003725 if (dec) {
3726 if (!atomic_add_unless(&perf_sched_count, -1, 1))
3727 schedule_delayed_work(&perf_sched_work, HZ);
3728 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003729
3730 unaccount_event_cpu(event, event->cpu);
3731}
3732
Peter Zijlstra9107c892016-02-24 18:45:45 +01003733static void perf_sched_delayed(struct work_struct *work)
3734{
3735 mutex_lock(&perf_sched_mutex);
3736 if (atomic_dec_and_test(&perf_sched_count))
3737 static_branch_disable(&perf_sched_events);
3738 mutex_unlock(&perf_sched_mutex);
3739}
3740
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003741/*
3742 * The following implement mutual exclusion of events on "exclusive" pmus
3743 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3744 * at a time, so we disallow creating events that might conflict, namely:
3745 *
3746 * 1) cpu-wide events in the presence of per-task events,
3747 * 2) per-task events in the presence of cpu-wide events,
3748 * 3) two matching events on the same context.
3749 *
3750 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003751 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003752 */
3753static int exclusive_event_init(struct perf_event *event)
3754{
3755 struct pmu *pmu = event->pmu;
3756
3757 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3758 return 0;
3759
3760 /*
3761 * Prevent co-existence of per-task and cpu-wide events on the
3762 * same exclusive pmu.
3763 *
3764 * Negative pmu::exclusive_cnt means there are cpu-wide
3765 * events on this "exclusive" pmu, positive means there are
3766 * per-task events.
3767 *
3768 * Since this is called in perf_event_alloc() path, event::ctx
3769 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3770 * to mean "per-task event", because unlike other attach states it
3771 * never gets cleared.
3772 */
3773 if (event->attach_state & PERF_ATTACH_TASK) {
3774 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
3775 return -EBUSY;
3776 } else {
3777 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
3778 return -EBUSY;
3779 }
3780
3781 return 0;
3782}
3783
3784static void exclusive_event_destroy(struct perf_event *event)
3785{
3786 struct pmu *pmu = event->pmu;
3787
3788 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3789 return;
3790
3791 /* see comment in exclusive_event_init() */
3792 if (event->attach_state & PERF_ATTACH_TASK)
3793 atomic_dec(&pmu->exclusive_cnt);
3794 else
3795 atomic_inc(&pmu->exclusive_cnt);
3796}
3797
3798static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
3799{
3800 if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
3801 (e1->cpu == e2->cpu ||
3802 e1->cpu == -1 ||
3803 e2->cpu == -1))
3804 return true;
3805 return false;
3806}
3807
3808/* Called under the same ctx::mutex as perf_install_in_context() */
3809static bool exclusive_event_installable(struct perf_event *event,
3810 struct perf_event_context *ctx)
3811{
3812 struct perf_event *iter_event;
3813 struct pmu *pmu = event->pmu;
3814
3815 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3816 return true;
3817
3818 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
3819 if (exclusive_event_match(iter_event, event))
3820 return false;
3821 }
3822
3823 return true;
3824}
3825
Alexander Shishkin375637b2016-04-27 18:44:46 +03003826static void perf_addr_filters_splice(struct perf_event *event,
3827 struct list_head *head);
3828
Peter Zijlstra683ede42014-05-05 12:11:24 +02003829static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003830{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003831 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003832
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003833 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003834
Frederic Weisbecker76369132011-05-19 19:55:04 +02003835 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003836 /*
3837 * Can happen when we close an event with re-directed output.
3838 *
3839 * Since we have a 0 refcount, perf_mmap_close() will skip
3840 * over us; possibly making our ring_buffer_put() the last.
3841 */
3842 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003843 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003844 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003845 }
3846
Stephane Eraniane5d13672011-02-14 11:20:01 +02003847 if (is_cgroup_event(event))
3848 perf_detach_cgroup(event);
3849
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003850 if (!event->parent) {
3851 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3852 put_callchain_buffers();
3853 }
3854
3855 perf_event_free_bpf_prog(event);
Alexander Shishkin375637b2016-04-27 18:44:46 +03003856 perf_addr_filters_splice(event, NULL);
3857 kfree(event->addr_filters_offs);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003858
3859 if (event->destroy)
3860 event->destroy(event);
3861
3862 if (event->ctx)
3863 put_ctx(event->ctx);
3864
Alexander Shishkin62a92c82016-06-07 15:44:15 +03003865 exclusive_event_destroy(event);
3866 module_put(event->pmu->module);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003867
3868 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003869}
3870
Peter Zijlstra683ede42014-05-05 12:11:24 +02003871/*
3872 * Used to free events which have a known refcount of 1, such as in error paths
3873 * where the event isn't exposed yet and inherited events.
3874 */
3875static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003876{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003877 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3878 "unexpected event refcount: %ld; ptr=%p\n",
3879 atomic_long_read(&event->refcount), event)) {
3880 /* leak to avoid use-after-free */
3881 return;
3882 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003883
Peter Zijlstra683ede42014-05-05 12:11:24 +02003884 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003885}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003886
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003887/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003888 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003889 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003890static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003891{
Peter Zijlstra88821352010-11-09 19:01:43 +01003892 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003893
Peter Zijlstra88821352010-11-09 19:01:43 +01003894 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01003895 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003896 * Matches the smp_store_release() in perf_event_exit_task(). If we
3897 * observe !owner it means the list deletion is complete and we can
3898 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01003899 * owner->perf_event_mutex.
3900 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003901 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01003902 if (owner) {
3903 /*
3904 * Since delayed_put_task_struct() also drops the last
3905 * task reference we can safely take a new reference
3906 * while holding the rcu_read_lock().
3907 */
3908 get_task_struct(owner);
3909 }
3910 rcu_read_unlock();
3911
3912 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003913 /*
3914 * If we're here through perf_event_exit_task() we're already
3915 * holding ctx->mutex which would be an inversion wrt. the
3916 * normal lock order.
3917 *
3918 * However we can safely take this lock because its the child
3919 * ctx->mutex.
3920 */
3921 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
3922
Peter Zijlstra88821352010-11-09 19:01:43 +01003923 /*
3924 * We have to re-check the event->owner field, if it is cleared
3925 * we raced with perf_event_exit_task(), acquiring the mutex
3926 * ensured they're done, and we can proceed with freeing the
3927 * event.
3928 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003929 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01003930 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003931 smp_store_release(&event->owner, NULL);
3932 }
Peter Zijlstra88821352010-11-09 19:01:43 +01003933 mutex_unlock(&owner->perf_event_mutex);
3934 put_task_struct(owner);
3935 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003936}
3937
Jiri Olsaf8697762014-08-01 14:33:01 +02003938static void put_event(struct perf_event *event)
3939{
Jiri Olsaf8697762014-08-01 14:33:01 +02003940 if (!atomic_long_dec_and_test(&event->refcount))
3941 return;
3942
Peter Zijlstra683ede42014-05-05 12:11:24 +02003943 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003944}
3945
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003946/*
3947 * Kill an event dead; while event:refcount will preserve the event
3948 * object, it will not preserve its functionality. Once the last 'user'
3949 * gives up the object, we'll destroy the thing.
3950 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02003951int perf_event_release_kernel(struct perf_event *event)
3952{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003953 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003954 struct perf_event *child, *tmp;
3955
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01003956 /*
3957 * If we got here through err_file: fput(event_file); we will not have
3958 * attached to a context yet.
3959 */
3960 if (!ctx) {
3961 WARN_ON_ONCE(event->attach_state &
3962 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
3963 goto no_ctx;
3964 }
3965
Peter Zijlstra88821352010-11-09 19:01:43 +01003966 if (!is_kernel_event(event))
3967 perf_remove_from_owner(event);
3968
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01003969 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02003970 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003971 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01003972
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003973 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01003974 /*
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003975 * Mark this even as STATE_DEAD, there is no external reference to it
3976 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003977 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003978 * Anybody acquiring event->child_mutex after the below loop _must_
3979 * also see this, most importantly inherit_event() which will avoid
3980 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003981 *
3982 * Thus this guarantees that we will in fact observe and kill _ALL_
3983 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01003984 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01003985 event->state = PERF_EVENT_STATE_DEAD;
3986 raw_spin_unlock_irq(&ctx->lock);
3987
3988 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01003989
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003990again:
3991 mutex_lock(&event->child_mutex);
3992 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01003993
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02003994 /*
3995 * Cannot change, child events are not migrated, see the
3996 * comment with perf_event_ctx_lock_nested().
3997 */
3998 ctx = lockless_dereference(child->ctx);
3999 /*
4000 * Since child_mutex nests inside ctx::mutex, we must jump
4001 * through hoops. We start by grabbing a reference on the ctx.
4002 *
4003 * Since the event cannot get freed while we hold the
4004 * child_mutex, the context must also exist and have a !0
4005 * reference count.
4006 */
4007 get_ctx(ctx);
4008
4009 /*
4010 * Now that we have a ctx ref, we can drop child_mutex, and
4011 * acquire ctx::mutex without fear of it going away. Then we
4012 * can re-acquire child_mutex.
4013 */
4014 mutex_unlock(&event->child_mutex);
4015 mutex_lock(&ctx->mutex);
4016 mutex_lock(&event->child_mutex);
4017
4018 /*
4019 * Now that we hold ctx::mutex and child_mutex, revalidate our
4020 * state, if child is still the first entry, it didn't get freed
4021 * and we can continue doing so.
4022 */
4023 tmp = list_first_entry_or_null(&event->child_list,
4024 struct perf_event, child_list);
4025 if (tmp == child) {
4026 perf_remove_from_context(child, DETACH_GROUP);
4027 list_del(&child->child_list);
4028 free_event(child);
4029 /*
4030 * This matches the refcount bump in inherit_event();
4031 * this can't be the last reference.
4032 */
4033 put_event(event);
4034 }
4035
4036 mutex_unlock(&event->child_mutex);
4037 mutex_unlock(&ctx->mutex);
4038 put_ctx(ctx);
4039 goto again;
4040 }
4041 mutex_unlock(&event->child_mutex);
4042
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004043no_ctx:
4044 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004045 return 0;
4046}
4047EXPORT_SYMBOL_GPL(perf_event_release_kernel);
4048
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02004049/*
4050 * Called when the last reference to the file is gone.
4051 */
Al Viroa6fa9412012-08-20 14:59:25 +01004052static int perf_release(struct inode *inode, struct file *file)
4053{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004054 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01004055 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004056}
4057
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004058u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004059{
4060 struct perf_event *child;
4061 u64 total = 0;
4062
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004063 *enabled = 0;
4064 *running = 0;
4065
Peter Zijlstra6f105812009-11-20 22:19:56 +01004066 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004067
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004068 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004069 total += perf_event_count(event);
4070
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004071 *enabled += event->total_time_enabled +
4072 atomic64_read(&event->child_total_time_enabled);
4073 *running += event->total_time_running +
4074 atomic64_read(&event->child_total_time_running);
4075
4076 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004077 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004078 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004079 *enabled += child->total_time_enabled;
4080 *running += child->total_time_running;
4081 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01004082 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004083
4084 return total;
4085}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004086EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004087
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004088static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004089 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004090{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004091 struct perf_event *sub;
4092 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004093 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01004094
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004095 ret = perf_event_read(leader, true);
4096 if (ret)
4097 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004098
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004099 /*
4100 * Since we co-schedule groups, {enabled,running} times of siblings
4101 * will be identical to those of the leader, so we only publish one
4102 * set.
4103 */
4104 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4105 values[n++] += leader->total_time_enabled +
4106 atomic64_read(&leader->child_total_time_enabled);
4107 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004108
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004109 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4110 values[n++] += leader->total_time_running +
4111 atomic64_read(&leader->child_total_time_running);
4112 }
4113
4114 /*
4115 * Write {count,id} tuples for every sibling.
4116 */
4117 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004118 if (read_format & PERF_FORMAT_ID)
4119 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004120
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004121 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004122 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004123 if (read_format & PERF_FORMAT_ID)
4124 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004125 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004126
4127 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004128}
4129
4130static int perf_read_group(struct perf_event *event,
4131 u64 read_format, char __user *buf)
4132{
4133 struct perf_event *leader = event->group_leader, *child;
4134 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004135 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004136 u64 *values;
4137
4138 lockdep_assert_held(&ctx->mutex);
4139
4140 values = kzalloc(event->read_size, GFP_KERNEL);
4141 if (!values)
4142 return -ENOMEM;
4143
4144 values[0] = 1 + leader->nr_siblings;
4145
4146 /*
4147 * By locking the child_mutex of the leader we effectively
4148 * lock the child list of all siblings.. XXX explain how.
4149 */
4150 mutex_lock(&leader->child_mutex);
4151
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004152 ret = __perf_read_group_add(leader, read_format, values);
4153 if (ret)
4154 goto unlock;
4155
4156 list_for_each_entry(child, &leader->child_list, child_list) {
4157 ret = __perf_read_group_add(child, read_format, values);
4158 if (ret)
4159 goto unlock;
4160 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004161
4162 mutex_unlock(&leader->child_mutex);
4163
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004164 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004165 if (copy_to_user(buf, values, event->read_size))
4166 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004167 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004168
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004169unlock:
4170 mutex_unlock(&leader->child_mutex);
4171out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004172 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004173 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004174}
4175
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004176static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004177 u64 read_format, char __user *buf)
4178{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004179 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004180 u64 values[4];
4181 int n = 0;
4182
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004183 values[n++] = perf_event_read_value(event, &enabled, &running);
4184 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4185 values[n++] = enabled;
4186 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4187 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004188 if (read_format & PERF_FORMAT_ID)
4189 values[n++] = primary_event_id(event);
4190
4191 if (copy_to_user(buf, values, n * sizeof(u64)))
4192 return -EFAULT;
4193
4194 return n * sizeof(u64);
4195}
4196
Jiri Olsadc633982014-09-12 13:18:26 +02004197static bool is_event_hup(struct perf_event *event)
4198{
4199 bool no_children;
4200
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004201 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004202 return false;
4203
4204 mutex_lock(&event->child_mutex);
4205 no_children = list_empty(&event->child_list);
4206 mutex_unlock(&event->child_mutex);
4207 return no_children;
4208}
4209
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004210/*
4211 * Read the performance event - simple non blocking version for now
4212 */
4213static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004214__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004215{
4216 u64 read_format = event->attr.read_format;
4217 int ret;
4218
4219 /*
4220 * Return end-of-file for a read on a event that is in
4221 * error state (i.e. because it was pinned but it couldn't be
4222 * scheduled on to the CPU at some point).
4223 */
4224 if (event->state == PERF_EVENT_STATE_ERROR)
4225 return 0;
4226
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004227 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004228 return -ENOSPC;
4229
4230 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004231 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004232 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004233 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004234 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004235
4236 return ret;
4237}
4238
4239static ssize_t
4240perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4241{
4242 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004243 struct perf_event_context *ctx;
4244 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004245
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004246 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004247 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004248 perf_event_ctx_unlock(event, ctx);
4249
4250 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004251}
4252
4253static unsigned int perf_poll(struct file *file, poll_table *wait)
4254{
4255 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004256 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004257 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004258
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004259 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004260
Jiri Olsadc633982014-09-12 13:18:26 +02004261 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004262 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004263
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004264 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004265 * Pin the event->rb by taking event->mmap_mutex; otherwise
4266 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004267 */
4268 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004269 rb = event->rb;
4270 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004271 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004272 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004273 return events;
4274}
4275
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004276static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004277{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004278 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004279 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004280 perf_event_update_userpage(event);
4281}
4282
4283/*
4284 * Holding the top-level event's child_mutex means that any
4285 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004286 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004287 * task existence requirements of perf_event_enable/disable.
4288 */
4289static void perf_event_for_each_child(struct perf_event *event,
4290 void (*func)(struct perf_event *))
4291{
4292 struct perf_event *child;
4293
4294 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004295
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004296 mutex_lock(&event->child_mutex);
4297 func(event);
4298 list_for_each_entry(child, &event->child_list, child_list)
4299 func(child);
4300 mutex_unlock(&event->child_mutex);
4301}
4302
4303static void perf_event_for_each(struct perf_event *event,
4304 void (*func)(struct perf_event *))
4305{
4306 struct perf_event_context *ctx = event->ctx;
4307 struct perf_event *sibling;
4308
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004309 lockdep_assert_held(&ctx->mutex);
4310
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004311 event = event->group_leader;
4312
4313 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004314 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004315 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004316}
4317
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004318static void __perf_event_period(struct perf_event *event,
4319 struct perf_cpu_context *cpuctx,
4320 struct perf_event_context *ctx,
4321 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004322{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004323 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004324 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004325
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004326 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004327 event->attr.sample_freq = value;
4328 } else {
4329 event->attr.sample_period = value;
4330 event->hw.sample_period = value;
4331 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004332
4333 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4334 if (active) {
4335 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01004336 /*
4337 * We could be throttled; unthrottle now to avoid the tick
4338 * trying to unthrottle while we already re-started the event.
4339 */
4340 if (event->hw.interrupts == MAX_INTERRUPTS) {
4341 event->hw.interrupts = 0;
4342 perf_log_throttle(event, 1);
4343 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004344 event->pmu->stop(event, PERF_EF_UPDATE);
4345 }
4346
4347 local64_set(&event->hw.period_left, 0);
4348
4349 if (active) {
4350 event->pmu->start(event, PERF_EF_RELOAD);
4351 perf_pmu_enable(ctx->pmu);
4352 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004353}
4354
4355static int perf_event_period(struct perf_event *event, u64 __user *arg)
4356{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004357 u64 value;
4358
4359 if (!is_sampling_event(event))
4360 return -EINVAL;
4361
4362 if (copy_from_user(&value, arg, sizeof(value)))
4363 return -EFAULT;
4364
4365 if (!value)
4366 return -EINVAL;
4367
4368 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4369 return -EINVAL;
4370
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004371 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004372
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004373 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004374}
4375
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004376static const struct file_operations perf_fops;
4377
Al Viro2903ff02012-08-28 12:52:22 -04004378static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004379{
Al Viro2903ff02012-08-28 12:52:22 -04004380 struct fd f = fdget(fd);
4381 if (!f.file)
4382 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004383
Al Viro2903ff02012-08-28 12:52:22 -04004384 if (f.file->f_op != &perf_fops) {
4385 fdput(f);
4386 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004387 }
Al Viro2903ff02012-08-28 12:52:22 -04004388 *p = f;
4389 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004390}
4391
4392static int perf_event_set_output(struct perf_event *event,
4393 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004394static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004395static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004396
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004397static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004398{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004399 void (*func)(struct perf_event *);
4400 u32 flags = arg;
4401
4402 switch (cmd) {
4403 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004404 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004405 break;
4406 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004407 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004408 break;
4409 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004410 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004411 break;
4412
4413 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004414 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004415
4416 case PERF_EVENT_IOC_PERIOD:
4417 return perf_event_period(event, (u64 __user *)arg);
4418
Jiri Olsacf4957f2012-10-24 13:37:58 +02004419 case PERF_EVENT_IOC_ID:
4420 {
4421 u64 id = primary_event_id(event);
4422
4423 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4424 return -EFAULT;
4425 return 0;
4426 }
4427
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004428 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004429 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004430 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004431 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004432 struct perf_event *output_event;
4433 struct fd output;
4434 ret = perf_fget_light(arg, &output);
4435 if (ret)
4436 return ret;
4437 output_event = output.file->private_data;
4438 ret = perf_event_set_output(event, output_event);
4439 fdput(output);
4440 } else {
4441 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004442 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004443 return ret;
4444 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004445
Li Zefan6fb29152009-10-15 11:21:42 +08004446 case PERF_EVENT_IOC_SET_FILTER:
4447 return perf_event_set_filter(event, (void __user *)arg);
4448
Alexei Starovoitov25415172015-03-25 12:49:20 -07004449 case PERF_EVENT_IOC_SET_BPF:
4450 return perf_event_set_bpf_prog(event, arg);
4451
Wang Nan86e79722016-03-28 06:41:29 +00004452 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
4453 struct ring_buffer *rb;
4454
4455 rcu_read_lock();
4456 rb = rcu_dereference(event->rb);
4457 if (!rb || !rb->nr_pages) {
4458 rcu_read_unlock();
4459 return -EINVAL;
4460 }
4461 rb_toggle_paused(rb, !!arg);
4462 rcu_read_unlock();
4463 return 0;
4464 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004465 default:
4466 return -ENOTTY;
4467 }
4468
4469 if (flags & PERF_IOC_FLAG_GROUP)
4470 perf_event_for_each(event, func);
4471 else
4472 perf_event_for_each_child(event, func);
4473
4474 return 0;
4475}
4476
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004477static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4478{
4479 struct perf_event *event = file->private_data;
4480 struct perf_event_context *ctx;
4481 long ret;
4482
4483 ctx = perf_event_ctx_lock(event);
4484 ret = _perf_ioctl(event, cmd, arg);
4485 perf_event_ctx_unlock(event, ctx);
4486
4487 return ret;
4488}
4489
Pawel Mollb3f20782014-06-13 16:03:32 +01004490#ifdef CONFIG_COMPAT
4491static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4492 unsigned long arg)
4493{
4494 switch (_IOC_NR(cmd)) {
4495 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4496 case _IOC_NR(PERF_EVENT_IOC_ID):
4497 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4498 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4499 cmd &= ~IOCSIZE_MASK;
4500 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4501 }
4502 break;
4503 }
4504 return perf_ioctl(file, cmd, arg);
4505}
4506#else
4507# define perf_compat_ioctl NULL
4508#endif
4509
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004510int perf_event_task_enable(void)
4511{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004512 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004513 struct perf_event *event;
4514
4515 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004516 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4517 ctx = perf_event_ctx_lock(event);
4518 perf_event_for_each_child(event, _perf_event_enable);
4519 perf_event_ctx_unlock(event, ctx);
4520 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004521 mutex_unlock(&current->perf_event_mutex);
4522
4523 return 0;
4524}
4525
4526int perf_event_task_disable(void)
4527{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004528 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004529 struct perf_event *event;
4530
4531 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004532 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4533 ctx = perf_event_ctx_lock(event);
4534 perf_event_for_each_child(event, _perf_event_disable);
4535 perf_event_ctx_unlock(event, ctx);
4536 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004537 mutex_unlock(&current->perf_event_mutex);
4538
4539 return 0;
4540}
4541
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004542static int perf_event_index(struct perf_event *event)
4543{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004544 if (event->hw.state & PERF_HES_STOPPED)
4545 return 0;
4546
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004547 if (event->state != PERF_EVENT_STATE_ACTIVE)
4548 return 0;
4549
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004550 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004551}
4552
Eric B Munsonc4794292011-06-23 16:34:38 -04004553static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004554 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004555 u64 *enabled,
4556 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004557{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004558 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004559
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004560 *now = perf_clock();
4561 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004562 *enabled = ctx_time - event->tstamp_enabled;
4563 *running = ctx_time - event->tstamp_running;
4564}
4565
Peter Zijlstrafa731582013-09-19 10:16:42 +02004566static void perf_event_init_userpage(struct perf_event *event)
4567{
4568 struct perf_event_mmap_page *userpg;
4569 struct ring_buffer *rb;
4570
4571 rcu_read_lock();
4572 rb = rcu_dereference(event->rb);
4573 if (!rb)
4574 goto unlock;
4575
4576 userpg = rb->user_page;
4577
4578 /* Allow new userspace to detect that bit 0 is deprecated */
4579 userpg->cap_bit0_is_deprecated = 1;
4580 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004581 userpg->data_offset = PAGE_SIZE;
4582 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004583
4584unlock:
4585 rcu_read_unlock();
4586}
4587
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004588void __weak arch_perf_update_userpage(
4589 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004590{
4591}
4592
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004593/*
4594 * Callers need to ensure there can be no nesting of this function, otherwise
4595 * the seqlock logic goes bad. We can not serialize this because the arch
4596 * code calls this from NMI context.
4597 */
4598void perf_event_update_userpage(struct perf_event *event)
4599{
4600 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004601 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004602 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004603
4604 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004605 rb = rcu_dereference(event->rb);
4606 if (!rb)
4607 goto unlock;
4608
Eric B Munson0d641202011-06-24 12:26:26 -04004609 /*
4610 * compute total_time_enabled, total_time_running
4611 * based on snapshot values taken when the event
4612 * was last scheduled in.
4613 *
4614 * we cannot simply called update_context_time()
4615 * because of locking issue as we can be called in
4616 * NMI context
4617 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004618 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004619
Frederic Weisbecker76369132011-05-19 19:55:04 +02004620 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004621 /*
4622 * Disable preemption so as to not let the corresponding user-space
4623 * spin too long if we get preempted.
4624 */
4625 preempt_disable();
4626 ++userpg->lock;
4627 barrier();
4628 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004629 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004630 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004631 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004632
Eric B Munson0d641202011-06-24 12:26:26 -04004633 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004634 atomic64_read(&event->child_total_time_enabled);
4635
Eric B Munson0d641202011-06-24 12:26:26 -04004636 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004637 atomic64_read(&event->child_total_time_running);
4638
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004639 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004640
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004641 barrier();
4642 ++userpg->lock;
4643 preempt_enable();
4644unlock:
4645 rcu_read_unlock();
4646}
4647
Peter Zijlstra906010b2009-09-21 16:08:49 +02004648static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4649{
4650 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004651 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004652 int ret = VM_FAULT_SIGBUS;
4653
4654 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4655 if (vmf->pgoff == 0)
4656 ret = 0;
4657 return ret;
4658 }
4659
4660 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004661 rb = rcu_dereference(event->rb);
4662 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004663 goto unlock;
4664
4665 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4666 goto unlock;
4667
Frederic Weisbecker76369132011-05-19 19:55:04 +02004668 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004669 if (!vmf->page)
4670 goto unlock;
4671
4672 get_page(vmf->page);
4673 vmf->page->mapping = vma->vm_file->f_mapping;
4674 vmf->page->index = vmf->pgoff;
4675
4676 ret = 0;
4677unlock:
4678 rcu_read_unlock();
4679
4680 return ret;
4681}
4682
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004683static void ring_buffer_attach(struct perf_event *event,
4684 struct ring_buffer *rb)
4685{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004686 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004687 unsigned long flags;
4688
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004689 if (event->rb) {
4690 /*
4691 * Should be impossible, we set this when removing
4692 * event->rb_entry and wait/clear when adding event->rb_entry.
4693 */
4694 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004695
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004696 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004697 spin_lock_irqsave(&old_rb->event_lock, flags);
4698 list_del_rcu(&event->rb_entry);
4699 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004700
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004701 event->rcu_batches = get_state_synchronize_rcu();
4702 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004703 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004704
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004705 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004706 if (event->rcu_pending) {
4707 cond_synchronize_rcu(event->rcu_batches);
4708 event->rcu_pending = 0;
4709 }
4710
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004711 spin_lock_irqsave(&rb->event_lock, flags);
4712 list_add_rcu(&event->rb_entry, &rb->event_list);
4713 spin_unlock_irqrestore(&rb->event_lock, flags);
4714 }
4715
4716 rcu_assign_pointer(event->rb, rb);
4717
4718 if (old_rb) {
4719 ring_buffer_put(old_rb);
4720 /*
4721 * Since we detached before setting the new rb, so that we
4722 * could attach the new rb, we could have missed a wakeup.
4723 * Provide it now.
4724 */
4725 wake_up_all(&event->waitq);
4726 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004727}
4728
4729static void ring_buffer_wakeup(struct perf_event *event)
4730{
4731 struct ring_buffer *rb;
4732
4733 rcu_read_lock();
4734 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004735 if (rb) {
4736 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4737 wake_up_all(&event->waitq);
4738 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004739 rcu_read_unlock();
4740}
4741
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004742struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004743{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004744 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004745
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004746 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004747 rb = rcu_dereference(event->rb);
4748 if (rb) {
4749 if (!atomic_inc_not_zero(&rb->refcount))
4750 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004751 }
4752 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004753
Frederic Weisbecker76369132011-05-19 19:55:04 +02004754 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004755}
4756
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004757void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004758{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004759 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004760 return;
4761
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004762 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004763
Frederic Weisbecker76369132011-05-19 19:55:04 +02004764 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004765}
4766
4767static void perf_mmap_open(struct vm_area_struct *vma)
4768{
4769 struct perf_event *event = vma->vm_file->private_data;
4770
4771 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004772 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004773
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004774 if (vma->vm_pgoff)
4775 atomic_inc(&event->rb->aux_mmap_count);
4776
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004777 if (event->pmu->event_mapped)
4778 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004779}
4780
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004781static void perf_pmu_output_stop(struct perf_event *event);
4782
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004783/*
4784 * A buffer can be mmap()ed multiple times; either directly through the same
4785 * event, or through other events by use of perf_event_set_output().
4786 *
4787 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4788 * the buffer here, where we still have a VM context. This means we need
4789 * to detach all events redirecting to us.
4790 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004791static void perf_mmap_close(struct vm_area_struct *vma)
4792{
4793 struct perf_event *event = vma->vm_file->private_data;
4794
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004795 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004796 struct user_struct *mmap_user = rb->mmap_user;
4797 int mmap_locked = rb->mmap_locked;
4798 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004799
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004800 if (event->pmu->event_unmapped)
4801 event->pmu->event_unmapped(event);
4802
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004803 /*
4804 * rb->aux_mmap_count will always drop before rb->mmap_count and
4805 * event->mmap_count, so it is ok to use event->mmap_mutex to
4806 * serialize with perf_mmap here.
4807 */
4808 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
4809 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004810 /*
4811 * Stop all AUX events that are writing to this buffer,
4812 * so that we can free its AUX pages and corresponding PMU
4813 * data. Note that after rb::aux_mmap_count dropped to zero,
4814 * they won't start any more (see perf_aux_output_begin()).
4815 */
4816 perf_pmu_output_stop(event);
4817
4818 /* now it's safe to free the pages */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004819 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
4820 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
4821
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004822 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004823 rb_free_aux(rb);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004824 WARN_ON_ONCE(atomic_read(&rb->aux_refcount));
4825
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004826 mutex_unlock(&event->mmap_mutex);
4827 }
4828
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004829 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004830
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004831 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004832 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004833
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004834 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004835 mutex_unlock(&event->mmap_mutex);
4836
4837 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004838 if (atomic_read(&rb->mmap_count))
4839 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004840
4841 /*
4842 * No other mmap()s, detach from all other events that might redirect
4843 * into the now unreachable buffer. Somewhat complicated by the
4844 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4845 */
4846again:
4847 rcu_read_lock();
4848 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4849 if (!atomic_long_inc_not_zero(&event->refcount)) {
4850 /*
4851 * This event is en-route to free_event() which will
4852 * detach it and remove it from the list.
4853 */
4854 continue;
4855 }
4856 rcu_read_unlock();
4857
4858 mutex_lock(&event->mmap_mutex);
4859 /*
4860 * Check we didn't race with perf_event_set_output() which can
4861 * swizzle the rb from under us while we were waiting to
4862 * acquire mmap_mutex.
4863 *
4864 * If we find a different rb; ignore this event, a next
4865 * iteration will no longer find it on the list. We have to
4866 * still restart the iteration to make sure we're not now
4867 * iterating the wrong list.
4868 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004869 if (event->rb == rb)
4870 ring_buffer_attach(event, NULL);
4871
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004872 mutex_unlock(&event->mmap_mutex);
4873 put_event(event);
4874
4875 /*
4876 * Restart the iteration; either we're on the wrong list or
4877 * destroyed its integrity by doing a deletion.
4878 */
4879 goto again;
4880 }
4881 rcu_read_unlock();
4882
4883 /*
4884 * It could be there's still a few 0-ref events on the list; they'll
4885 * get cleaned up by free_event() -- they'll also still have their
4886 * ref on the rb and will free it whenever they are done with it.
4887 *
4888 * Aside from that, this buffer is 'fully' detached and unmapped,
4889 * undo the VM accounting.
4890 */
4891
4892 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4893 vma->vm_mm->pinned_vm -= mmap_locked;
4894 free_uid(mmap_user);
4895
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004896out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004897 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004898}
4899
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004900static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004901 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004902 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004903 .fault = perf_mmap_fault,
4904 .page_mkwrite = perf_mmap_fault,
4905};
4906
4907static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4908{
4909 struct perf_event *event = file->private_data;
4910 unsigned long user_locked, user_lock_limit;
4911 struct user_struct *user = current_user();
4912 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004913 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004914 unsigned long vma_size;
4915 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004916 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004917 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004918
Peter Zijlstrac7920612010-05-18 10:33:24 +02004919 /*
4920 * Don't allow mmap() of inherited per-task counters. This would
4921 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004922 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004923 */
4924 if (event->cpu == -1 && event->attr.inherit)
4925 return -EINVAL;
4926
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004927 if (!(vma->vm_flags & VM_SHARED))
4928 return -EINVAL;
4929
4930 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004931
4932 if (vma->vm_pgoff == 0) {
4933 nr_pages = (vma_size / PAGE_SIZE) - 1;
4934 } else {
4935 /*
4936 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
4937 * mapped, all subsequent mappings should have the same size
4938 * and offset. Must be above the normal perf buffer.
4939 */
4940 u64 aux_offset, aux_size;
4941
4942 if (!event->rb)
4943 return -EINVAL;
4944
4945 nr_pages = vma_size / PAGE_SIZE;
4946
4947 mutex_lock(&event->mmap_mutex);
4948 ret = -EINVAL;
4949
4950 rb = event->rb;
4951 if (!rb)
4952 goto aux_unlock;
4953
4954 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
4955 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
4956
4957 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
4958 goto aux_unlock;
4959
4960 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
4961 goto aux_unlock;
4962
4963 /* already mapped with a different offset */
4964 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
4965 goto aux_unlock;
4966
4967 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
4968 goto aux_unlock;
4969
4970 /* already mapped with a different size */
4971 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
4972 goto aux_unlock;
4973
4974 if (!is_power_of_2(nr_pages))
4975 goto aux_unlock;
4976
4977 if (!atomic_inc_not_zero(&rb->mmap_count))
4978 goto aux_unlock;
4979
4980 if (rb_has_aux(rb)) {
4981 atomic_inc(&rb->aux_mmap_count);
4982 ret = 0;
4983 goto unlock;
4984 }
4985
4986 atomic_set(&rb->aux_mmap_count, 1);
4987 user_extra = nr_pages;
4988
4989 goto accounting;
4990 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004991
4992 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004993 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004994 * can do bitmasks instead of modulo.
4995 */
Kan Liang2ed11312015-03-02 02:14:26 -05004996 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004997 return -EINVAL;
4998
4999 if (vma_size != PAGE_SIZE * (1 + nr_pages))
5000 return -EINVAL;
5001
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005002 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005003again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005004 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005005 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005006 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005007 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005008 goto unlock;
5009 }
5010
5011 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
5012 /*
5013 * Raced against perf_mmap_close() through
5014 * perf_event_set_output(). Try again, hope for better
5015 * luck.
5016 */
5017 mutex_unlock(&event->mmap_mutex);
5018 goto again;
5019 }
5020
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005021 goto unlock;
5022 }
5023
5024 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005025
5026accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005027 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
5028
5029 /*
5030 * Increase the limit linearly with more CPUs:
5031 */
5032 user_lock_limit *= num_online_cpus();
5033
5034 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
5035
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005036 if (user_locked > user_lock_limit)
5037 extra = user_locked - user_lock_limit;
5038
Jiri Slaby78d7d402010-03-05 13:42:54 -08005039 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005040 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07005041 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005042
5043 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
5044 !capable(CAP_IPC_LOCK)) {
5045 ret = -EPERM;
5046 goto unlock;
5047 }
5048
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005049 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005050
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005051 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005052 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005053
Frederic Weisbecker76369132011-05-19 19:55:04 +02005054 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005055 rb = rb_alloc(nr_pages,
5056 event->attr.watermark ? event->attr.wakeup_watermark : 0,
5057 event->cpu, flags);
5058
5059 if (!rb) {
5060 ret = -ENOMEM;
5061 goto unlock;
5062 }
5063
5064 atomic_set(&rb->mmap_count, 1);
5065 rb->mmap_user = get_current_user();
5066 rb->mmap_locked = extra;
5067
5068 ring_buffer_attach(event, rb);
5069
5070 perf_event_init_userpage(event);
5071 perf_event_update_userpage(event);
5072 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02005073 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
5074 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005075 if (!ret)
5076 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005077 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005078
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005079unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005080 if (!ret) {
5081 atomic_long_add(user_extra, &user->locked_vm);
5082 vma->vm_mm->pinned_vm += extra;
5083
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005084 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005085 } else if (rb) {
5086 atomic_dec(&rb->mmap_count);
5087 }
5088aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005089 mutex_unlock(&event->mmap_mutex);
5090
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005091 /*
5092 * Since pinned accounting is per vm we cannot allow fork() to copy our
5093 * vma.
5094 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005095 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005096 vma->vm_ops = &perf_mmap_vmops;
5097
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005098 if (event->pmu->event_mapped)
5099 event->pmu->event_mapped(event);
5100
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005101 return ret;
5102}
5103
5104static int perf_fasync(int fd, struct file *filp, int on)
5105{
Al Viro496ad9a2013-01-23 17:07:38 -05005106 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005107 struct perf_event *event = filp->private_data;
5108 int retval;
5109
Al Viro59551022016-01-22 15:40:57 -05005110 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005111 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05005112 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005113
5114 if (retval < 0)
5115 return retval;
5116
5117 return 0;
5118}
5119
5120static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01005121 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005122 .release = perf_release,
5123 .read = perf_read,
5124 .poll = perf_poll,
5125 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01005126 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005127 .mmap = perf_mmap,
5128 .fasync = perf_fasync,
5129};
5130
5131/*
5132 * Perf event wakeup
5133 *
5134 * If there's data, ensure we set the poll() state and publish everything
5135 * to user-space before waking everybody up.
5136 */
5137
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005138static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
5139{
5140 /* only the parent has fasync state */
5141 if (event->parent)
5142 event = event->parent;
5143 return &event->fasync;
5144}
5145
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005146void perf_event_wakeup(struct perf_event *event)
5147{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005148 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005149
5150 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005151 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005152 event->pending_kill = 0;
5153 }
5154}
5155
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005156static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005157{
5158 struct perf_event *event = container_of(entry,
5159 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01005160 int rctx;
5161
5162 rctx = perf_swevent_get_recursion_context();
5163 /*
5164 * If we 'fail' here, that's OK, it means recursion is already disabled
5165 * and we won't recurse 'further'.
5166 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005167
5168 if (event->pending_disable) {
5169 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005170 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005171 }
5172
5173 if (event->pending_wakeup) {
5174 event->pending_wakeup = 0;
5175 perf_event_wakeup(event);
5176 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01005177
5178 if (rctx >= 0)
5179 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005180}
5181
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005182/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005183 * We assume there is only KVM supporting the callbacks.
5184 * Later on, we might change it to a list if there is
5185 * another virtualization implementation supporting the callbacks.
5186 */
5187struct perf_guest_info_callbacks *perf_guest_cbs;
5188
5189int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5190{
5191 perf_guest_cbs = cbs;
5192 return 0;
5193}
5194EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5195
5196int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5197{
5198 perf_guest_cbs = NULL;
5199 return 0;
5200}
5201EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5202
Jiri Olsa40189942012-08-07 15:20:37 +02005203static void
5204perf_output_sample_regs(struct perf_output_handle *handle,
5205 struct pt_regs *regs, u64 mask)
5206{
5207 int bit;
5208
5209 for_each_set_bit(bit, (const unsigned long *) &mask,
5210 sizeof(mask) * BITS_PER_BYTE) {
5211 u64 val;
5212
5213 val = perf_reg_value(regs, bit);
5214 perf_output_put(handle, val);
5215 }
5216}
5217
Stephane Eranian60e23642014-09-24 13:48:37 +02005218static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005219 struct pt_regs *regs,
5220 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02005221{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005222 if (user_mode(regs)) {
5223 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02005224 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005225 } else if (current->mm) {
5226 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005227 } else {
5228 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5229 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005230 }
5231}
5232
Stephane Eranian60e23642014-09-24 13:48:37 +02005233static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5234 struct pt_regs *regs)
5235{
5236 regs_intr->regs = regs;
5237 regs_intr->abi = perf_reg_abi(current);
5238}
5239
5240
Jiri Olsac5ebced2012-08-07 15:20:40 +02005241/*
5242 * Get remaining task size from user stack pointer.
5243 *
5244 * It'd be better to take stack vma map and limit this more
5245 * precisly, but there's no way to get it safely under interrupt,
5246 * so using TASK_SIZE as limit.
5247 */
5248static u64 perf_ustack_task_size(struct pt_regs *regs)
5249{
5250 unsigned long addr = perf_user_stack_pointer(regs);
5251
5252 if (!addr || addr >= TASK_SIZE)
5253 return 0;
5254
5255 return TASK_SIZE - addr;
5256}
5257
5258static u16
5259perf_sample_ustack_size(u16 stack_size, u16 header_size,
5260 struct pt_regs *regs)
5261{
5262 u64 task_size;
5263
5264 /* No regs, no stack pointer, no dump. */
5265 if (!regs)
5266 return 0;
5267
5268 /*
5269 * Check if we fit in with the requested stack size into the:
5270 * - TASK_SIZE
5271 * If we don't, we limit the size to the TASK_SIZE.
5272 *
5273 * - remaining sample size
5274 * If we don't, we customize the stack size to
5275 * fit in to the remaining sample size.
5276 */
5277
5278 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5279 stack_size = min(stack_size, (u16) task_size);
5280
5281 /* Current header size plus static size and dynamic size. */
5282 header_size += 2 * sizeof(u64);
5283
5284 /* Do we fit in with the current stack dump size? */
5285 if ((u16) (header_size + stack_size) < header_size) {
5286 /*
5287 * If we overflow the maximum size for the sample,
5288 * we customize the stack dump size to fit in.
5289 */
5290 stack_size = USHRT_MAX - header_size - sizeof(u64);
5291 stack_size = round_up(stack_size, sizeof(u64));
5292 }
5293
5294 return stack_size;
5295}
5296
5297static void
5298perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5299 struct pt_regs *regs)
5300{
5301 /* Case of a kernel thread, nothing to dump */
5302 if (!regs) {
5303 u64 size = 0;
5304 perf_output_put(handle, size);
5305 } else {
5306 unsigned long sp;
5307 unsigned int rem;
5308 u64 dyn_size;
5309
5310 /*
5311 * We dump:
5312 * static size
5313 * - the size requested by user or the best one we can fit
5314 * in to the sample max size
5315 * data
5316 * - user stack dump data
5317 * dynamic size
5318 * - the actual dumped size
5319 */
5320
5321 /* Static size. */
5322 perf_output_put(handle, dump_size);
5323
5324 /* Data. */
5325 sp = perf_user_stack_pointer(regs);
5326 rem = __output_copy_user(handle, (void *) sp, dump_size);
5327 dyn_size = dump_size - rem;
5328
5329 perf_output_skip(handle, rem);
5330
5331 /* Dynamic size. */
5332 perf_output_put(handle, dyn_size);
5333 }
5334}
5335
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005336static void __perf_event_header__init_id(struct perf_event_header *header,
5337 struct perf_sample_data *data,
5338 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005339{
5340 u64 sample_type = event->attr.sample_type;
5341
5342 data->type = sample_type;
5343 header->size += event->id_header_size;
5344
5345 if (sample_type & PERF_SAMPLE_TID) {
5346 /* namespace issues */
5347 data->tid_entry.pid = perf_event_pid(event, current);
5348 data->tid_entry.tid = perf_event_tid(event, current);
5349 }
5350
5351 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005352 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005353
Adrian Hunterff3d5272013-08-27 11:23:07 +03005354 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005355 data->id = primary_event_id(event);
5356
5357 if (sample_type & PERF_SAMPLE_STREAM_ID)
5358 data->stream_id = event->id;
5359
5360 if (sample_type & PERF_SAMPLE_CPU) {
5361 data->cpu_entry.cpu = raw_smp_processor_id();
5362 data->cpu_entry.reserved = 0;
5363 }
5364}
5365
Frederic Weisbecker76369132011-05-19 19:55:04 +02005366void perf_event_header__init_id(struct perf_event_header *header,
5367 struct perf_sample_data *data,
5368 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005369{
5370 if (event->attr.sample_id_all)
5371 __perf_event_header__init_id(header, data, event);
5372}
5373
5374static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5375 struct perf_sample_data *data)
5376{
5377 u64 sample_type = data->type;
5378
5379 if (sample_type & PERF_SAMPLE_TID)
5380 perf_output_put(handle, data->tid_entry);
5381
5382 if (sample_type & PERF_SAMPLE_TIME)
5383 perf_output_put(handle, data->time);
5384
5385 if (sample_type & PERF_SAMPLE_ID)
5386 perf_output_put(handle, data->id);
5387
5388 if (sample_type & PERF_SAMPLE_STREAM_ID)
5389 perf_output_put(handle, data->stream_id);
5390
5391 if (sample_type & PERF_SAMPLE_CPU)
5392 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005393
5394 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5395 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005396}
5397
Frederic Weisbecker76369132011-05-19 19:55:04 +02005398void perf_event__output_id_sample(struct perf_event *event,
5399 struct perf_output_handle *handle,
5400 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005401{
5402 if (event->attr.sample_id_all)
5403 __perf_event__output_id_sample(handle, sample);
5404}
5405
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005406static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005407 struct perf_event *event,
5408 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005409{
5410 u64 read_format = event->attr.read_format;
5411 u64 values[4];
5412 int n = 0;
5413
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005414 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005415 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005416 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005417 atomic64_read(&event->child_total_time_enabled);
5418 }
5419 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005420 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005421 atomic64_read(&event->child_total_time_running);
5422 }
5423 if (read_format & PERF_FORMAT_ID)
5424 values[n++] = primary_event_id(event);
5425
Frederic Weisbecker76369132011-05-19 19:55:04 +02005426 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005427}
5428
5429/*
5430 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
5431 */
5432static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005433 struct perf_event *event,
5434 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005435{
5436 struct perf_event *leader = event->group_leader, *sub;
5437 u64 read_format = event->attr.read_format;
5438 u64 values[5];
5439 int n = 0;
5440
5441 values[n++] = 1 + leader->nr_siblings;
5442
5443 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005444 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005445
5446 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005447 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005448
5449 if (leader != event)
5450 leader->pmu->read(leader);
5451
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005452 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005453 if (read_format & PERF_FORMAT_ID)
5454 values[n++] = primary_event_id(leader);
5455
Frederic Weisbecker76369132011-05-19 19:55:04 +02005456 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005457
5458 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5459 n = 0;
5460
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005461 if ((sub != event) &&
5462 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005463 sub->pmu->read(sub);
5464
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005465 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005466 if (read_format & PERF_FORMAT_ID)
5467 values[n++] = primary_event_id(sub);
5468
Frederic Weisbecker76369132011-05-19 19:55:04 +02005469 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005470 }
5471}
5472
Stephane Eranianeed01522010-10-26 16:08:01 +02005473#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5474 PERF_FORMAT_TOTAL_TIME_RUNNING)
5475
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005476static void perf_output_read(struct perf_output_handle *handle,
5477 struct perf_event *event)
5478{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005479 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005480 u64 read_format = event->attr.read_format;
5481
5482 /*
5483 * compute total_time_enabled, total_time_running
5484 * based on snapshot values taken when the event
5485 * was last scheduled in.
5486 *
5487 * we cannot simply called update_context_time()
5488 * because of locking issue as we are called in
5489 * NMI context
5490 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005491 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005492 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005493
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005494 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005495 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005496 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005497 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005498}
5499
5500void perf_output_sample(struct perf_output_handle *handle,
5501 struct perf_event_header *header,
5502 struct perf_sample_data *data,
5503 struct perf_event *event)
5504{
5505 u64 sample_type = data->type;
5506
5507 perf_output_put(handle, *header);
5508
Adrian Hunterff3d5272013-08-27 11:23:07 +03005509 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5510 perf_output_put(handle, data->id);
5511
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005512 if (sample_type & PERF_SAMPLE_IP)
5513 perf_output_put(handle, data->ip);
5514
5515 if (sample_type & PERF_SAMPLE_TID)
5516 perf_output_put(handle, data->tid_entry);
5517
5518 if (sample_type & PERF_SAMPLE_TIME)
5519 perf_output_put(handle, data->time);
5520
5521 if (sample_type & PERF_SAMPLE_ADDR)
5522 perf_output_put(handle, data->addr);
5523
5524 if (sample_type & PERF_SAMPLE_ID)
5525 perf_output_put(handle, data->id);
5526
5527 if (sample_type & PERF_SAMPLE_STREAM_ID)
5528 perf_output_put(handle, data->stream_id);
5529
5530 if (sample_type & PERF_SAMPLE_CPU)
5531 perf_output_put(handle, data->cpu_entry);
5532
5533 if (sample_type & PERF_SAMPLE_PERIOD)
5534 perf_output_put(handle, data->period);
5535
5536 if (sample_type & PERF_SAMPLE_READ)
5537 perf_output_read(handle, event);
5538
5539 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5540 if (data->callchain) {
5541 int size = 1;
5542
5543 if (data->callchain)
5544 size += data->callchain->nr;
5545
5546 size *= sizeof(u64);
5547
Frederic Weisbecker76369132011-05-19 19:55:04 +02005548 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005549 } else {
5550 u64 nr = 0;
5551 perf_output_put(handle, nr);
5552 }
5553 }
5554
5555 if (sample_type & PERF_SAMPLE_RAW) {
5556 if (data->raw) {
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005557 u32 raw_size = data->raw->size;
5558 u32 real_size = round_up(raw_size + sizeof(u32),
5559 sizeof(u64)) - sizeof(u32);
5560 u64 zero = 0;
5561
5562 perf_output_put(handle, real_size);
5563 __output_copy(handle, data->raw->data, raw_size);
5564 if (real_size - raw_size)
5565 __output_copy(handle, &zero, real_size - raw_size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005566 } else {
5567 struct {
5568 u32 size;
5569 u32 data;
5570 } raw = {
5571 .size = sizeof(u32),
5572 .data = 0,
5573 };
5574 perf_output_put(handle, raw);
5575 }
5576 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005577
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005578 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5579 if (data->br_stack) {
5580 size_t size;
5581
5582 size = data->br_stack->nr
5583 * sizeof(struct perf_branch_entry);
5584
5585 perf_output_put(handle, data->br_stack->nr);
5586 perf_output_copy(handle, data->br_stack->entries, size);
5587 } else {
5588 /*
5589 * we always store at least the value of nr
5590 */
5591 u64 nr = 0;
5592 perf_output_put(handle, nr);
5593 }
5594 }
Jiri Olsa40189942012-08-07 15:20:37 +02005595
5596 if (sample_type & PERF_SAMPLE_REGS_USER) {
5597 u64 abi = data->regs_user.abi;
5598
5599 /*
5600 * If there are no regs to dump, notice it through
5601 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5602 */
5603 perf_output_put(handle, abi);
5604
5605 if (abi) {
5606 u64 mask = event->attr.sample_regs_user;
5607 perf_output_sample_regs(handle,
5608 data->regs_user.regs,
5609 mask);
5610 }
5611 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005612
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005613 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005614 perf_output_sample_ustack(handle,
5615 data->stack_user_size,
5616 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005617 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005618
5619 if (sample_type & PERF_SAMPLE_WEIGHT)
5620 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005621
5622 if (sample_type & PERF_SAMPLE_DATA_SRC)
5623 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005624
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005625 if (sample_type & PERF_SAMPLE_TRANSACTION)
5626 perf_output_put(handle, data->txn);
5627
Stephane Eranian60e23642014-09-24 13:48:37 +02005628 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5629 u64 abi = data->regs_intr.abi;
5630 /*
5631 * If there are no regs to dump, notice it through
5632 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5633 */
5634 perf_output_put(handle, abi);
5635
5636 if (abi) {
5637 u64 mask = event->attr.sample_regs_intr;
5638
5639 perf_output_sample_regs(handle,
5640 data->regs_intr.regs,
5641 mask);
5642 }
5643 }
5644
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005645 if (!event->attr.watermark) {
5646 int wakeup_events = event->attr.wakeup_events;
5647
5648 if (wakeup_events) {
5649 struct ring_buffer *rb = handle->rb;
5650 int events = local_inc_return(&rb->events);
5651
5652 if (events >= wakeup_events) {
5653 local_sub(wakeup_events, &rb->events);
5654 local_inc(&rb->wakeup);
5655 }
5656 }
5657 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005658}
5659
5660void perf_prepare_sample(struct perf_event_header *header,
5661 struct perf_sample_data *data,
5662 struct perf_event *event,
5663 struct pt_regs *regs)
5664{
5665 u64 sample_type = event->attr.sample_type;
5666
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005667 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005668 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005669
5670 header->misc = 0;
5671 header->misc |= perf_misc_flags(regs);
5672
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005673 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005674
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005675 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005676 data->ip = perf_instruction_pointer(regs);
5677
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005678 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5679 int size = 1;
5680
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005681 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005682
5683 if (data->callchain)
5684 size += data->callchain->nr;
5685
5686 header->size += size * sizeof(u64);
5687 }
5688
5689 if (sample_type & PERF_SAMPLE_RAW) {
5690 int size = sizeof(u32);
5691
5692 if (data->raw)
5693 size += data->raw->size;
5694 else
5695 size += sizeof(u32);
5696
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005697 header->size += round_up(size, sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005698 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005699
5700 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5701 int size = sizeof(u64); /* nr */
5702 if (data->br_stack) {
5703 size += data->br_stack->nr
5704 * sizeof(struct perf_branch_entry);
5705 }
5706 header->size += size;
5707 }
Jiri Olsa40189942012-08-07 15:20:37 +02005708
Peter Zijlstra25657112014-09-24 13:48:42 +02005709 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005710 perf_sample_regs_user(&data->regs_user, regs,
5711 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005712
Jiri Olsa40189942012-08-07 15:20:37 +02005713 if (sample_type & PERF_SAMPLE_REGS_USER) {
5714 /* regs dump ABI info */
5715 int size = sizeof(u64);
5716
Jiri Olsa40189942012-08-07 15:20:37 +02005717 if (data->regs_user.regs) {
5718 u64 mask = event->attr.sample_regs_user;
5719 size += hweight64(mask) * sizeof(u64);
5720 }
5721
5722 header->size += size;
5723 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005724
5725 if (sample_type & PERF_SAMPLE_STACK_USER) {
5726 /*
5727 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5728 * processed as the last one or have additional check added
5729 * in case new sample type is added, because we could eat
5730 * up the rest of the sample size.
5731 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005732 u16 stack_size = event->attr.sample_stack_user;
5733 u16 size = sizeof(u64);
5734
Jiri Olsac5ebced2012-08-07 15:20:40 +02005735 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005736 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005737
5738 /*
5739 * If there is something to dump, add space for the dump
5740 * itself and for the field that tells the dynamic size,
5741 * which is how many have been actually dumped.
5742 */
5743 if (stack_size)
5744 size += sizeof(u64) + stack_size;
5745
5746 data->stack_user_size = stack_size;
5747 header->size += size;
5748 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005749
5750 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5751 /* regs dump ABI info */
5752 int size = sizeof(u64);
5753
5754 perf_sample_regs_intr(&data->regs_intr, regs);
5755
5756 if (data->regs_intr.regs) {
5757 u64 mask = event->attr.sample_regs_intr;
5758
5759 size += hweight64(mask) * sizeof(u64);
5760 }
5761
5762 header->size += size;
5763 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005764}
5765
Wang Nan9ecda412016-04-05 14:11:18 +00005766static void __always_inline
5767__perf_event_output(struct perf_event *event,
5768 struct perf_sample_data *data,
5769 struct pt_regs *regs,
5770 int (*output_begin)(struct perf_output_handle *,
5771 struct perf_event *,
5772 unsigned int))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005773{
5774 struct perf_output_handle handle;
5775 struct perf_event_header header;
5776
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005777 /* protect the callchain buffers */
5778 rcu_read_lock();
5779
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005780 perf_prepare_sample(&header, data, event, regs);
5781
Wang Nan9ecda412016-04-05 14:11:18 +00005782 if (output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005783 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005784
5785 perf_output_sample(&handle, &header, data, event);
5786
5787 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005788
5789exit:
5790 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005791}
5792
Wang Nan9ecda412016-04-05 14:11:18 +00005793void
5794perf_event_output_forward(struct perf_event *event,
5795 struct perf_sample_data *data,
5796 struct pt_regs *regs)
5797{
5798 __perf_event_output(event, data, regs, perf_output_begin_forward);
5799}
5800
5801void
5802perf_event_output_backward(struct perf_event *event,
5803 struct perf_sample_data *data,
5804 struct pt_regs *regs)
5805{
5806 __perf_event_output(event, data, regs, perf_output_begin_backward);
5807}
5808
5809void
5810perf_event_output(struct perf_event *event,
5811 struct perf_sample_data *data,
5812 struct pt_regs *regs)
5813{
5814 __perf_event_output(event, data, regs, perf_output_begin);
5815}
5816
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005817/*
5818 * read event_id
5819 */
5820
5821struct perf_read_event {
5822 struct perf_event_header header;
5823
5824 u32 pid;
5825 u32 tid;
5826};
5827
5828static void
5829perf_event_read_event(struct perf_event *event,
5830 struct task_struct *task)
5831{
5832 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005833 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005834 struct perf_read_event read_event = {
5835 .header = {
5836 .type = PERF_RECORD_READ,
5837 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005838 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005839 },
5840 .pid = perf_event_pid(event, task),
5841 .tid = perf_event_tid(event, task),
5842 };
5843 int ret;
5844
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005845 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005846 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005847 if (ret)
5848 return;
5849
5850 perf_output_put(&handle, read_event);
5851 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005852 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005853
5854 perf_output_end(&handle);
5855}
5856
Jiri Olsa52d857a2013-05-06 18:27:18 +02005857typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5858
5859static void
5860perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005861 perf_event_aux_output_cb output,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03005862 void *data, bool all)
Jiri Olsa52d857a2013-05-06 18:27:18 +02005863{
5864 struct perf_event *event;
5865
5866 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03005867 if (!all) {
5868 if (event->state < PERF_EVENT_STATE_INACTIVE)
5869 continue;
5870 if (!event_filter_match(event))
5871 continue;
5872 }
5873
Jiri Olsa67516842013-07-09 18:56:31 +02005874 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005875 }
5876}
5877
5878static void
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005879perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data,
5880 struct perf_event_context *task_ctx)
5881{
5882 rcu_read_lock();
5883 preempt_disable();
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03005884 perf_event_aux_ctx(task_ctx, output, data, false);
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005885 preempt_enable();
5886 rcu_read_unlock();
5887}
5888
5889static void
Jiri Olsa67516842013-07-09 18:56:31 +02005890perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005891 struct perf_event_context *task_ctx)
5892{
5893 struct perf_cpu_context *cpuctx;
5894 struct perf_event_context *ctx;
5895 struct pmu *pmu;
5896 int ctxn;
5897
Jiri Olsa4e93ad62015-11-04 16:00:05 +01005898 /*
5899 * If we have task_ctx != NULL we only notify
5900 * the task context itself. The task_ctx is set
5901 * only for EXIT events before releasing task
5902 * context.
5903 */
5904 if (task_ctx) {
5905 perf_event_aux_task_ctx(output, data, task_ctx);
5906 return;
5907 }
5908
Jiri Olsa52d857a2013-05-06 18:27:18 +02005909 rcu_read_lock();
5910 list_for_each_entry_rcu(pmu, &pmus, entry) {
5911 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5912 if (cpuctx->unique_pmu != pmu)
5913 goto next;
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03005914 perf_event_aux_ctx(&cpuctx->ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005915 ctxn = pmu->task_ctx_nr;
5916 if (ctxn < 0)
5917 goto next;
5918 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5919 if (ctx)
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03005920 perf_event_aux_ctx(ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005921next:
5922 put_cpu_ptr(pmu->pmu_cpu_context);
5923 }
Jiri Olsa52d857a2013-05-06 18:27:18 +02005924 rcu_read_unlock();
5925}
5926
Alexander Shishkin375637b2016-04-27 18:44:46 +03005927/*
5928 * Clear all file-based filters at exec, they'll have to be
5929 * re-instated when/if these objects are mmapped again.
5930 */
5931static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
5932{
5933 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
5934 struct perf_addr_filter *filter;
5935 unsigned int restart = 0, count = 0;
5936 unsigned long flags;
5937
5938 if (!has_addr_filter(event))
5939 return;
5940
5941 raw_spin_lock_irqsave(&ifh->lock, flags);
5942 list_for_each_entry(filter, &ifh->list, entry) {
5943 if (filter->inode) {
5944 event->addr_filters_offs[count] = 0;
5945 restart++;
5946 }
5947
5948 count++;
5949 }
5950
5951 if (restart)
5952 event->addr_filters_gen++;
5953 raw_spin_unlock_irqrestore(&ifh->lock, flags);
5954
5955 if (restart)
5956 perf_event_restart(event);
5957}
5958
5959void perf_event_exec(void)
5960{
5961 struct perf_event_context *ctx;
5962 int ctxn;
5963
5964 rcu_read_lock();
5965 for_each_task_context_nr(ctxn) {
5966 ctx = current->perf_event_ctxp[ctxn];
5967 if (!ctx)
5968 continue;
5969
5970 perf_event_enable_on_exec(ctxn);
5971
5972 perf_event_aux_ctx(ctx, perf_event_addr_filters_exec, NULL,
5973 true);
5974 }
5975 rcu_read_unlock();
5976}
5977
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005978struct remote_output {
5979 struct ring_buffer *rb;
5980 int err;
5981};
5982
5983static void __perf_event_output_stop(struct perf_event *event, void *data)
5984{
5985 struct perf_event *parent = event->parent;
5986 struct remote_output *ro = data;
5987 struct ring_buffer *rb = ro->rb;
Alexander Shishkin375637b2016-04-27 18:44:46 +03005988 struct stop_event_data sd = {
5989 .event = event,
5990 };
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005991
5992 if (!has_aux(event))
5993 return;
5994
5995 if (!parent)
5996 parent = event;
5997
5998 /*
5999 * In case of inheritance, it will be the parent that links to the
6000 * ring-buffer, but it will be the child that's actually using it:
6001 */
6002 if (rcu_dereference(parent->rb) == rb)
Alexander Shishkin375637b2016-04-27 18:44:46 +03006003 ro->err = __perf_event_stop(&sd);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006004}
6005
6006static int __perf_pmu_output_stop(void *info)
6007{
6008 struct perf_event *event = info;
6009 struct pmu *pmu = event->pmu;
6010 struct perf_cpu_context *cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
6011 struct remote_output ro = {
6012 .rb = event->rb,
6013 };
6014
6015 rcu_read_lock();
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006016 perf_event_aux_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006017 if (cpuctx->task_ctx)
6018 perf_event_aux_ctx(cpuctx->task_ctx, __perf_event_output_stop,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006019 &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006020 rcu_read_unlock();
6021
6022 return ro.err;
6023}
6024
6025static void perf_pmu_output_stop(struct perf_event *event)
6026{
6027 struct perf_event *iter;
6028 int err, cpu;
6029
6030restart:
6031 rcu_read_lock();
6032 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
6033 /*
6034 * For per-CPU events, we need to make sure that neither they
6035 * nor their children are running; for cpu==-1 events it's
6036 * sufficient to stop the event itself if it's active, since
6037 * it can't have children.
6038 */
6039 cpu = iter->cpu;
6040 if (cpu == -1)
6041 cpu = READ_ONCE(iter->oncpu);
6042
6043 if (cpu == -1)
6044 continue;
6045
6046 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
6047 if (err == -EAGAIN) {
6048 rcu_read_unlock();
6049 goto restart;
6050 }
6051 }
6052 rcu_read_unlock();
6053}
6054
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006055/*
6056 * task tracking -- fork/exit
6057 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02006058 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006059 */
6060
6061struct perf_task_event {
6062 struct task_struct *task;
6063 struct perf_event_context *task_ctx;
6064
6065 struct {
6066 struct perf_event_header header;
6067
6068 u32 pid;
6069 u32 ppid;
6070 u32 tid;
6071 u32 ptid;
6072 u64 time;
6073 } event_id;
6074};
6075
Jiri Olsa67516842013-07-09 18:56:31 +02006076static int perf_event_task_match(struct perf_event *event)
6077{
Stephane Eranian13d7a242013-08-21 12:10:24 +02006078 return event->attr.comm || event->attr.mmap ||
6079 event->attr.mmap2 || event->attr.mmap_data ||
6080 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02006081}
6082
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006083static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006084 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006085{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006086 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006087 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006088 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006089 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006090 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01006091
Jiri Olsa67516842013-07-09 18:56:31 +02006092 if (!perf_event_task_match(event))
6093 return;
6094
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006095 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006096
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006097 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006098 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02006099 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006100 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006101
6102 task_event->event_id.pid = perf_event_pid(event, task);
6103 task_event->event_id.ppid = perf_event_pid(event, current);
6104
6105 task_event->event_id.tid = perf_event_tid(event, task);
6106 task_event->event_id.ptid = perf_event_tid(event, current);
6107
Peter Zijlstra34f43922015-02-20 14:05:38 +01006108 task_event->event_id.time = perf_event_clock(event);
6109
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006110 perf_output_put(&handle, task_event->event_id);
6111
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006112 perf_event__output_id_sample(event, &handle, &sample);
6113
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006114 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006115out:
6116 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006117}
6118
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006119static void perf_event_task(struct task_struct *task,
6120 struct perf_event_context *task_ctx,
6121 int new)
6122{
6123 struct perf_task_event task_event;
6124
6125 if (!atomic_read(&nr_comm_events) &&
6126 !atomic_read(&nr_mmap_events) &&
6127 !atomic_read(&nr_task_events))
6128 return;
6129
6130 task_event = (struct perf_task_event){
6131 .task = task,
6132 .task_ctx = task_ctx,
6133 .event_id = {
6134 .header = {
6135 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
6136 .misc = 0,
6137 .size = sizeof(task_event.event_id),
6138 },
6139 /* .pid */
6140 /* .ppid */
6141 /* .tid */
6142 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01006143 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006144 },
6145 };
6146
Jiri Olsa67516842013-07-09 18:56:31 +02006147 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006148 &task_event,
6149 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006150}
6151
6152void perf_event_fork(struct task_struct *task)
6153{
6154 perf_event_task(task, NULL, 1);
6155}
6156
6157/*
6158 * comm tracking
6159 */
6160
6161struct perf_comm_event {
6162 struct task_struct *task;
6163 char *comm;
6164 int comm_size;
6165
6166 struct {
6167 struct perf_event_header header;
6168
6169 u32 pid;
6170 u32 tid;
6171 } event_id;
6172};
6173
Jiri Olsa67516842013-07-09 18:56:31 +02006174static int perf_event_comm_match(struct perf_event *event)
6175{
6176 return event->attr.comm;
6177}
6178
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006179static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006180 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006181{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006182 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006183 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006184 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006185 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006186 int ret;
6187
Jiri Olsa67516842013-07-09 18:56:31 +02006188 if (!perf_event_comm_match(event))
6189 return;
6190
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006191 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
6192 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006193 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006194
6195 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006196 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006197
6198 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
6199 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
6200
6201 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02006202 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006203 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006204
6205 perf_event__output_id_sample(event, &handle, &sample);
6206
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006207 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006208out:
6209 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006210}
6211
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006212static void perf_event_comm_event(struct perf_comm_event *comm_event)
6213{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006214 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006215 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006216
6217 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01006218 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006219 size = ALIGN(strlen(comm)+1, sizeof(u64));
6220
6221 comm_event->comm = comm;
6222 comm_event->comm_size = size;
6223
6224 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006225
Jiri Olsa67516842013-07-09 18:56:31 +02006226 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006227 comm_event,
6228 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006229}
6230
Adrian Hunter82b89772014-05-28 11:45:04 +03006231void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006232{
6233 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006234
6235 if (!atomic_read(&nr_comm_events))
6236 return;
6237
6238 comm_event = (struct perf_comm_event){
6239 .task = task,
6240 /* .comm */
6241 /* .comm_size */
6242 .event_id = {
6243 .header = {
6244 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03006245 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006246 /* .size */
6247 },
6248 /* .pid */
6249 /* .tid */
6250 },
6251 };
6252
6253 perf_event_comm_event(&comm_event);
6254}
6255
6256/*
6257 * mmap tracking
6258 */
6259
6260struct perf_mmap_event {
6261 struct vm_area_struct *vma;
6262
6263 const char *file_name;
6264 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006265 int maj, min;
6266 u64 ino;
6267 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006268 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006269
6270 struct {
6271 struct perf_event_header header;
6272
6273 u32 pid;
6274 u32 tid;
6275 u64 start;
6276 u64 len;
6277 u64 pgoff;
6278 } event_id;
6279};
6280
Jiri Olsa67516842013-07-09 18:56:31 +02006281static int perf_event_mmap_match(struct perf_event *event,
6282 void *data)
6283{
6284 struct perf_mmap_event *mmap_event = data;
6285 struct vm_area_struct *vma = mmap_event->vma;
6286 int executable = vma->vm_flags & VM_EXEC;
6287
6288 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02006289 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02006290}
6291
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006292static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006293 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006294{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006295 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006296 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006297 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006298 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006299 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006300
Jiri Olsa67516842013-07-09 18:56:31 +02006301 if (!perf_event_mmap_match(event, data))
6302 return;
6303
Stephane Eranian13d7a242013-08-21 12:10:24 +02006304 if (event->attr.mmap2) {
6305 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
6306 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
6307 mmap_event->event_id.header.size += sizeof(mmap_event->min);
6308 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03006309 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006310 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
6311 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006312 }
6313
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006314 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
6315 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006316 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006317 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006318 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006319
6320 mmap_event->event_id.pid = perf_event_pid(event, current);
6321 mmap_event->event_id.tid = perf_event_tid(event, current);
6322
6323 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006324
6325 if (event->attr.mmap2) {
6326 perf_output_put(&handle, mmap_event->maj);
6327 perf_output_put(&handle, mmap_event->min);
6328 perf_output_put(&handle, mmap_event->ino);
6329 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006330 perf_output_put(&handle, mmap_event->prot);
6331 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006332 }
6333
Frederic Weisbecker76369132011-05-19 19:55:04 +02006334 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006335 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006336
6337 perf_event__output_id_sample(event, &handle, &sample);
6338
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006339 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006340out:
6341 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006342}
6343
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006344static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
6345{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006346 struct vm_area_struct *vma = mmap_event->vma;
6347 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006348 int maj = 0, min = 0;
6349 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006350 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006351 unsigned int size;
6352 char tmp[16];
6353 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006354 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006355
6356 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02006357 struct inode *inode;
6358 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006359
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006360 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006361 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006362 name = "//enomem";
6363 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006364 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006365 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006366 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006367 * need to add enough zero bytes after the string to handle
6368 * the 64bit alignment we do later.
6369 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02006370 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006371 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006372 name = "//toolong";
6373 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006374 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02006375 inode = file_inode(vma->vm_file);
6376 dev = inode->i_sb->s_dev;
6377 ino = inode->i_ino;
6378 gen = inode->i_generation;
6379 maj = MAJOR(dev);
6380 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006381
6382 if (vma->vm_flags & VM_READ)
6383 prot |= PROT_READ;
6384 if (vma->vm_flags & VM_WRITE)
6385 prot |= PROT_WRITE;
6386 if (vma->vm_flags & VM_EXEC)
6387 prot |= PROT_EXEC;
6388
6389 if (vma->vm_flags & VM_MAYSHARE)
6390 flags = MAP_SHARED;
6391 else
6392 flags = MAP_PRIVATE;
6393
6394 if (vma->vm_flags & VM_DENYWRITE)
6395 flags |= MAP_DENYWRITE;
6396 if (vma->vm_flags & VM_MAYEXEC)
6397 flags |= MAP_EXECUTABLE;
6398 if (vma->vm_flags & VM_LOCKED)
6399 flags |= MAP_LOCKED;
6400 if (vma->vm_flags & VM_HUGETLB)
6401 flags |= MAP_HUGETLB;
6402
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006403 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006404 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006405 if (vma->vm_ops && vma->vm_ops->name) {
6406 name = (char *) vma->vm_ops->name(vma);
6407 if (name)
6408 goto cpy_name;
6409 }
6410
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006411 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006412 if (name)
6413 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006414
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006415 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006416 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006417 name = "[heap]";
6418 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006419 }
6420 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006421 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006422 name = "[stack]";
6423 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006424 }
6425
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006426 name = "//anon";
6427 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006428 }
6429
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006430cpy_name:
6431 strlcpy(tmp, name, sizeof(tmp));
6432 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006433got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006434 /*
6435 * Since our buffer works in 8 byte units we need to align our string
6436 * size to a multiple of 8. However, we must guarantee the tail end is
6437 * zero'd out to avoid leaking random bits to userspace.
6438 */
6439 size = strlen(name)+1;
6440 while (!IS_ALIGNED(size, sizeof(u64)))
6441 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006442
6443 mmap_event->file_name = name;
6444 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006445 mmap_event->maj = maj;
6446 mmap_event->min = min;
6447 mmap_event->ino = ino;
6448 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006449 mmap_event->prot = prot;
6450 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006451
Stephane Eranian2fe85422013-01-24 16:10:39 +01006452 if (!(vma->vm_flags & VM_EXEC))
6453 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6454
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006455 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6456
Jiri Olsa67516842013-07-09 18:56:31 +02006457 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006458 mmap_event,
6459 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006460
6461 kfree(buf);
6462}
6463
Alexander Shishkin375637b2016-04-27 18:44:46 +03006464/*
6465 * Whether this @filter depends on a dynamic object which is not loaded
6466 * yet or its load addresses are not known.
6467 */
6468static bool perf_addr_filter_needs_mmap(struct perf_addr_filter *filter)
6469{
6470 return filter->filter && filter->inode;
6471}
6472
6473/*
6474 * Check whether inode and address range match filter criteria.
6475 */
6476static bool perf_addr_filter_match(struct perf_addr_filter *filter,
6477 struct file *file, unsigned long offset,
6478 unsigned long size)
6479{
6480 if (filter->inode != file->f_inode)
6481 return false;
6482
6483 if (filter->offset > offset + size)
6484 return false;
6485
6486 if (filter->offset + filter->size < offset)
6487 return false;
6488
6489 return true;
6490}
6491
6492static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
6493{
6494 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6495 struct vm_area_struct *vma = data;
6496 unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags;
6497 struct file *file = vma->vm_file;
6498 struct perf_addr_filter *filter;
6499 unsigned int restart = 0, count = 0;
6500
6501 if (!has_addr_filter(event))
6502 return;
6503
6504 if (!file)
6505 return;
6506
6507 raw_spin_lock_irqsave(&ifh->lock, flags);
6508 list_for_each_entry(filter, &ifh->list, entry) {
6509 if (perf_addr_filter_match(filter, file, off,
6510 vma->vm_end - vma->vm_start)) {
6511 event->addr_filters_offs[count] = vma->vm_start;
6512 restart++;
6513 }
6514
6515 count++;
6516 }
6517
6518 if (restart)
6519 event->addr_filters_gen++;
6520 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6521
6522 if (restart)
6523 perf_event_restart(event);
6524}
6525
6526/*
6527 * Adjust all task's events' filters to the new vma
6528 */
6529static void perf_addr_filters_adjust(struct vm_area_struct *vma)
6530{
6531 struct perf_event_context *ctx;
6532 int ctxn;
6533
6534 rcu_read_lock();
6535 for_each_task_context_nr(ctxn) {
6536 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6537 if (!ctx)
6538 continue;
6539
6540 perf_event_aux_ctx(ctx, __perf_addr_filters_adjust, vma, true);
6541 }
6542 rcu_read_unlock();
6543}
6544
Eric B Munson3af9e852010-05-18 15:30:49 +01006545void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006546{
6547 struct perf_mmap_event mmap_event;
6548
6549 if (!atomic_read(&nr_mmap_events))
6550 return;
6551
6552 mmap_event = (struct perf_mmap_event){
6553 .vma = vma,
6554 /* .file_name */
6555 /* .file_size */
6556 .event_id = {
6557 .header = {
6558 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006559 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006560 /* .size */
6561 },
6562 /* .pid */
6563 /* .tid */
6564 .start = vma->vm_start,
6565 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006566 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006567 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006568 /* .maj (attr_mmap2 only) */
6569 /* .min (attr_mmap2 only) */
6570 /* .ino (attr_mmap2 only) */
6571 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006572 /* .prot (attr_mmap2 only) */
6573 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006574 };
6575
Alexander Shishkin375637b2016-04-27 18:44:46 +03006576 perf_addr_filters_adjust(vma);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006577 perf_event_mmap_event(&mmap_event);
6578}
6579
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006580void perf_event_aux_event(struct perf_event *event, unsigned long head,
6581 unsigned long size, u64 flags)
6582{
6583 struct perf_output_handle handle;
6584 struct perf_sample_data sample;
6585 struct perf_aux_event {
6586 struct perf_event_header header;
6587 u64 offset;
6588 u64 size;
6589 u64 flags;
6590 } rec = {
6591 .header = {
6592 .type = PERF_RECORD_AUX,
6593 .misc = 0,
6594 .size = sizeof(rec),
6595 },
6596 .offset = head,
6597 .size = size,
6598 .flags = flags,
6599 };
6600 int ret;
6601
6602 perf_event_header__init_id(&rec.header, &sample, event);
6603 ret = perf_output_begin(&handle, event, rec.header.size);
6604
6605 if (ret)
6606 return;
6607
6608 perf_output_put(&handle, rec);
6609 perf_event__output_id_sample(event, &handle, &sample);
6610
6611 perf_output_end(&handle);
6612}
6613
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006614/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006615 * Lost/dropped samples logging
6616 */
6617void perf_log_lost_samples(struct perf_event *event, u64 lost)
6618{
6619 struct perf_output_handle handle;
6620 struct perf_sample_data sample;
6621 int ret;
6622
6623 struct {
6624 struct perf_event_header header;
6625 u64 lost;
6626 } lost_samples_event = {
6627 .header = {
6628 .type = PERF_RECORD_LOST_SAMPLES,
6629 .misc = 0,
6630 .size = sizeof(lost_samples_event),
6631 },
6632 .lost = lost,
6633 };
6634
6635 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6636
6637 ret = perf_output_begin(&handle, event,
6638 lost_samples_event.header.size);
6639 if (ret)
6640 return;
6641
6642 perf_output_put(&handle, lost_samples_event);
6643 perf_event__output_id_sample(event, &handle, &sample);
6644 perf_output_end(&handle);
6645}
6646
6647/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006648 * context_switch tracking
6649 */
6650
6651struct perf_switch_event {
6652 struct task_struct *task;
6653 struct task_struct *next_prev;
6654
6655 struct {
6656 struct perf_event_header header;
6657 u32 next_prev_pid;
6658 u32 next_prev_tid;
6659 } event_id;
6660};
6661
6662static int perf_event_switch_match(struct perf_event *event)
6663{
6664 return event->attr.context_switch;
6665}
6666
6667static void perf_event_switch_output(struct perf_event *event, void *data)
6668{
6669 struct perf_switch_event *se = data;
6670 struct perf_output_handle handle;
6671 struct perf_sample_data sample;
6672 int ret;
6673
6674 if (!perf_event_switch_match(event))
6675 return;
6676
6677 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6678 if (event->ctx->task) {
6679 se->event_id.header.type = PERF_RECORD_SWITCH;
6680 se->event_id.header.size = sizeof(se->event_id.header);
6681 } else {
6682 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6683 se->event_id.header.size = sizeof(se->event_id);
6684 se->event_id.next_prev_pid =
6685 perf_event_pid(event, se->next_prev);
6686 se->event_id.next_prev_tid =
6687 perf_event_tid(event, se->next_prev);
6688 }
6689
6690 perf_event_header__init_id(&se->event_id.header, &sample, event);
6691
6692 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6693 if (ret)
6694 return;
6695
6696 if (event->ctx->task)
6697 perf_output_put(&handle, se->event_id.header);
6698 else
6699 perf_output_put(&handle, se->event_id);
6700
6701 perf_event__output_id_sample(event, &handle, &sample);
6702
6703 perf_output_end(&handle);
6704}
6705
6706static void perf_event_switch(struct task_struct *task,
6707 struct task_struct *next_prev, bool sched_in)
6708{
6709 struct perf_switch_event switch_event;
6710
6711 /* N.B. caller checks nr_switch_events != 0 */
6712
6713 switch_event = (struct perf_switch_event){
6714 .task = task,
6715 .next_prev = next_prev,
6716 .event_id = {
6717 .header = {
6718 /* .type */
6719 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
6720 /* .size */
6721 },
6722 /* .next_prev_pid */
6723 /* .next_prev_tid */
6724 },
6725 };
6726
6727 perf_event_aux(perf_event_switch_output,
6728 &switch_event,
6729 NULL);
6730}
6731
6732/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006733 * IRQ throttle logging
6734 */
6735
6736static void perf_log_throttle(struct perf_event *event, int enable)
6737{
6738 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006739 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006740 int ret;
6741
6742 struct {
6743 struct perf_event_header header;
6744 u64 time;
6745 u64 id;
6746 u64 stream_id;
6747 } throttle_event = {
6748 .header = {
6749 .type = PERF_RECORD_THROTTLE,
6750 .misc = 0,
6751 .size = sizeof(throttle_event),
6752 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01006753 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006754 .id = primary_event_id(event),
6755 .stream_id = event->id,
6756 };
6757
6758 if (enable)
6759 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
6760
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006761 perf_event_header__init_id(&throttle_event.header, &sample, event);
6762
6763 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006764 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006765 if (ret)
6766 return;
6767
6768 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006769 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006770 perf_output_end(&handle);
6771}
6772
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006773static void perf_log_itrace_start(struct perf_event *event)
6774{
6775 struct perf_output_handle handle;
6776 struct perf_sample_data sample;
6777 struct perf_aux_event {
6778 struct perf_event_header header;
6779 u32 pid;
6780 u32 tid;
6781 } rec;
6782 int ret;
6783
6784 if (event->parent)
6785 event = event->parent;
6786
6787 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
6788 event->hw.itrace_started)
6789 return;
6790
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006791 rec.header.type = PERF_RECORD_ITRACE_START;
6792 rec.header.misc = 0;
6793 rec.header.size = sizeof(rec);
6794 rec.pid = perf_event_pid(event, current);
6795 rec.tid = perf_event_tid(event, current);
6796
6797 perf_event_header__init_id(&rec.header, &sample, event);
6798 ret = perf_output_begin(&handle, event, rec.header.size);
6799
6800 if (ret)
6801 return;
6802
6803 perf_output_put(&handle, rec);
6804 perf_event__output_id_sample(event, &handle, &sample);
6805
6806 perf_output_end(&handle);
6807}
6808
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006809/*
6810 * Generic event overflow handling, sampling.
6811 */
6812
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006813static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006814 int throttle, struct perf_sample_data *data,
6815 struct pt_regs *regs)
6816{
6817 int events = atomic_read(&event->event_limit);
6818 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006819 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006820 int ret = 0;
6821
Peter Zijlstra96398822010-11-24 18:55:29 +01006822 /*
6823 * Non-sampling counters might still use the PMI to fold short
6824 * hardware counters, ignore those.
6825 */
6826 if (unlikely(!is_sampling_event(event)))
6827 return 0;
6828
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006829 seq = __this_cpu_read(perf_throttled_seq);
6830 if (seq != hwc->interrupts_seq) {
6831 hwc->interrupts_seq = seq;
6832 hwc->interrupts = 1;
6833 } else {
6834 hwc->interrupts++;
6835 if (unlikely(throttle
6836 && hwc->interrupts >= max_samples_per_tick)) {
6837 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02006838 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01006839 hwc->interrupts = MAX_INTERRUPTS;
6840 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006841 ret = 1;
6842 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006843 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006844
6845 if (event->attr.freq) {
6846 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01006847 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006848
Peter Zijlstraabd50712010-01-26 18:50:16 +01006849 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006850
Peter Zijlstraabd50712010-01-26 18:50:16 +01006851 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01006852 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006853 }
6854
6855 /*
6856 * XXX event_limit might not quite work as expected on inherited
6857 * events
6858 */
6859
6860 event->pending_kill = POLL_IN;
6861 if (events && atomic_dec_and_test(&event->event_limit)) {
6862 ret = 1;
6863 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006864 event->pending_disable = 1;
6865 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006866 }
6867
Wang Nan18794452016-03-28 06:41:30 +00006868 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006869
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006870 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006871 event->pending_wakeup = 1;
6872 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02006873 }
6874
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006875 return ret;
6876}
6877
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006878int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006879 struct perf_sample_data *data,
6880 struct pt_regs *regs)
6881{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006882 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006883}
6884
6885/*
6886 * Generic software event infrastructure
6887 */
6888
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006889struct swevent_htable {
6890 struct swevent_hlist *swevent_hlist;
6891 struct mutex hlist_mutex;
6892 int hlist_refcount;
6893
6894 /* Recursion avoidance in each contexts */
6895 int recursion[PERF_NR_CONTEXTS];
6896};
6897
6898static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
6899
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006900/*
6901 * We directly increment event->count and keep a second value in
6902 * event->hw.period_left to count intervals. This period event
6903 * is kept in the range [-sample_period, 0] so that we can use the
6904 * sign as trigger.
6905 */
6906
Jiri Olsaab573842013-05-01 17:25:44 +02006907u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006908{
6909 struct hw_perf_event *hwc = &event->hw;
6910 u64 period = hwc->last_period;
6911 u64 nr, offset;
6912 s64 old, val;
6913
6914 hwc->last_period = hwc->sample_period;
6915
6916again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02006917 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006918 if (val < 0)
6919 return 0;
6920
6921 nr = div64_u64(period + val, period);
6922 offset = nr * period;
6923 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02006924 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006925 goto again;
6926
6927 return nr;
6928}
6929
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006930static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006931 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006932 struct pt_regs *regs)
6933{
6934 struct hw_perf_event *hwc = &event->hw;
6935 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006936
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006937 if (!overflow)
6938 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006939
6940 if (hwc->interrupts == MAX_INTERRUPTS)
6941 return;
6942
6943 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006944 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006945 data, regs)) {
6946 /*
6947 * We inhibit the overflow from happening when
6948 * hwc->interrupts == MAX_INTERRUPTS.
6949 */
6950 break;
6951 }
6952 throttle = 1;
6953 }
6954}
6955
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006956static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006957 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006958 struct pt_regs *regs)
6959{
6960 struct hw_perf_event *hwc = &event->hw;
6961
Peter Zijlstrae7850592010-05-21 14:43:08 +02006962 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006963
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006964 if (!regs)
6965 return;
6966
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006967 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006968 return;
6969
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03006970 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
6971 data->period = nr;
6972 return perf_swevent_overflow(event, 1, data, regs);
6973 } else
6974 data->period = event->hw.last_period;
6975
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006976 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006977 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006978
Peter Zijlstrae7850592010-05-21 14:43:08 +02006979 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006980 return;
6981
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006982 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006983}
6984
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006985static int perf_exclude_event(struct perf_event *event,
6986 struct pt_regs *regs)
6987{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006988 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01006989 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006990
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006991 if (regs) {
6992 if (event->attr.exclude_user && user_mode(regs))
6993 return 1;
6994
6995 if (event->attr.exclude_kernel && !user_mode(regs))
6996 return 1;
6997 }
6998
6999 return 0;
7000}
7001
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007002static int perf_swevent_match(struct perf_event *event,
7003 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08007004 u32 event_id,
7005 struct perf_sample_data *data,
7006 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007007{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007008 if (event->attr.type != type)
7009 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007010
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007011 if (event->attr.config != event_id)
7012 return 0;
7013
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007014 if (perf_exclude_event(event, regs))
7015 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007016
7017 return 1;
7018}
7019
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007020static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007021{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007022 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007023
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007024 return hash_64(val, SWEVENT_HLIST_BITS);
7025}
7026
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007027static inline struct hlist_head *
7028__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007029{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007030 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007031
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007032 return &hlist->heads[hash];
7033}
7034
7035/* For the read side: events when they trigger */
7036static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007037find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007038{
7039 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007040
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007041 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007042 if (!hlist)
7043 return NULL;
7044
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007045 return __find_swevent_head(hlist, type, event_id);
7046}
7047
7048/* For the event head insertion and removal in the hlist */
7049static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007050find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007051{
7052 struct swevent_hlist *hlist;
7053 u32 event_id = event->attr.config;
7054 u64 type = event->attr.type;
7055
7056 /*
7057 * Event scheduling is always serialized against hlist allocation
7058 * and release. Which makes the protected version suitable here.
7059 * The context lock guarantees that.
7060 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007061 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007062 lockdep_is_held(&event->ctx->lock));
7063 if (!hlist)
7064 return NULL;
7065
7066 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007067}
7068
7069static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007070 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007071 struct perf_sample_data *data,
7072 struct pt_regs *regs)
7073{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007074 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007075 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007076 struct hlist_head *head;
7077
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007078 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007079 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007080 if (!head)
7081 goto end;
7082
Sasha Levinb67bfe02013-02-27 17:06:00 -08007083 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08007084 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007085 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007086 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007087end:
7088 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007089}
7090
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007091DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
7092
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007093int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007094{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007095 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007096
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007097 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007098}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01007099EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007100
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01007101inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007102{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007103 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007104
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007105 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007106}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007107
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007108void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007109{
Ingo Molnara4234bf2009-11-23 10:57:59 +01007110 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007111
7112 if (WARN_ON_ONCE(!regs))
7113 return;
7114
7115 perf_sample_data_init(&data, addr, 0);
7116 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
7117}
7118
7119void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
7120{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007121 int rctx;
7122
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007123 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007124 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007125 if (unlikely(rctx < 0))
7126 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007127
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007128 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007129
7130 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007131fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007132 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007133}
7134
7135static void perf_swevent_read(struct perf_event *event)
7136{
7137}
7138
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007139static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007140{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007141 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007142 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007143 struct hlist_head *head;
7144
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007145 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007146 hwc->last_period = hwc->sample_period;
7147 perf_swevent_set_period(event);
7148 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007149
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007150 hwc->state = !(flags & PERF_EF_START);
7151
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007152 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01007153 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007154 return -EINVAL;
7155
7156 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08007157 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007158
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007159 return 0;
7160}
7161
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007162static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007163{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007164 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007165}
7166
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007167static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007168{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007169 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007170}
7171
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007172static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007173{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007174 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007175}
7176
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007177/* Deref the hlist from the update side */
7178static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007179swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007180{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007181 return rcu_dereference_protected(swhash->swevent_hlist,
7182 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007183}
7184
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007185static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007186{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007187 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007188
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007189 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007190 return;
7191
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03007192 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08007193 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007194}
7195
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007196static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007197{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007198 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007199
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007200 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007201
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007202 if (!--swhash->hlist_refcount)
7203 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007204
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007205 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007206}
7207
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007208static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007209{
7210 int cpu;
7211
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007212 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007213 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007214}
7215
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007216static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007217{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007218 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007219 int err = 0;
7220
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007221 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007222 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007223 struct swevent_hlist *hlist;
7224
7225 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
7226 if (!hlist) {
7227 err = -ENOMEM;
7228 goto exit;
7229 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007230 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007231 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007232 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007233exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007234 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007235
7236 return err;
7237}
7238
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007239static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007240{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007241 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007242
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007243 get_online_cpus();
7244 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007245 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007246 if (err) {
7247 failed_cpu = cpu;
7248 goto fail;
7249 }
7250 }
7251 put_online_cpus();
7252
7253 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007254fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007255 for_each_possible_cpu(cpu) {
7256 if (cpu == failed_cpu)
7257 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007258 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007259 }
7260
7261 put_online_cpus();
7262 return err;
7263}
7264
Ingo Molnarc5905af2012-02-24 08:31:31 +01007265struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007266
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007267static void sw_perf_event_destroy(struct perf_event *event)
7268{
7269 u64 event_id = event->attr.config;
7270
7271 WARN_ON(event->parent);
7272
Ingo Molnarc5905af2012-02-24 08:31:31 +01007273 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007274 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007275}
7276
7277static int perf_swevent_init(struct perf_event *event)
7278{
Tommi Rantala8176cce2013-04-13 22:49:14 +03007279 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007280
7281 if (event->attr.type != PERF_TYPE_SOFTWARE)
7282 return -ENOENT;
7283
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007284 /*
7285 * no branch sampling for software events
7286 */
7287 if (has_branch_stack(event))
7288 return -EOPNOTSUPP;
7289
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007290 switch (event_id) {
7291 case PERF_COUNT_SW_CPU_CLOCK:
7292 case PERF_COUNT_SW_TASK_CLOCK:
7293 return -ENOENT;
7294
7295 default:
7296 break;
7297 }
7298
Dan Carpenterce677832010-10-24 21:50:42 +02007299 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007300 return -ENOENT;
7301
7302 if (!event->parent) {
7303 int err;
7304
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007305 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007306 if (err)
7307 return err;
7308
Ingo Molnarc5905af2012-02-24 08:31:31 +01007309 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007310 event->destroy = sw_perf_event_destroy;
7311 }
7312
7313 return 0;
7314}
7315
7316static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007317 .task_ctx_nr = perf_sw_context,
7318
Peter Zijlstra34f43922015-02-20 14:05:38 +01007319 .capabilities = PERF_PMU_CAP_NO_NMI,
7320
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007321 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007322 .add = perf_swevent_add,
7323 .del = perf_swevent_del,
7324 .start = perf_swevent_start,
7325 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007326 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007327};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007328
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007329#ifdef CONFIG_EVENT_TRACING
7330
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007331static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007332 struct perf_sample_data *data)
7333{
7334 void *record = data->raw->data;
7335
Peter Zijlstrab71b4372015-11-02 10:50:51 +01007336 /* only top level events have filters set */
7337 if (event->parent)
7338 event = event->parent;
7339
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007340 if (likely(!event->filter) || filter_match_preds(event->filter, record))
7341 return 1;
7342 return 0;
7343}
7344
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007345static int perf_tp_event_match(struct perf_event *event,
7346 struct perf_sample_data *data,
7347 struct pt_regs *regs)
7348{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01007349 if (event->hw.state & PERF_HES_STOPPED)
7350 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02007351 /*
7352 * All tracepoints are from kernel-space.
7353 */
7354 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007355 return 0;
7356
7357 if (!perf_tp_filter_match(event, data))
7358 return 0;
7359
7360 return 1;
7361}
7362
7363void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007364 struct pt_regs *regs, struct hlist_head *head, int rctx,
7365 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007366{
7367 struct perf_sample_data data;
7368 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007369
7370 struct perf_raw_record raw = {
7371 .size = entry_size,
7372 .data = record,
7373 };
7374
Robert Richterfd0d0002012-04-02 20:19:08 +02007375 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007376 data.raw = &raw;
7377
Sasha Levinb67bfe02013-02-27 17:06:00 -08007378 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007379 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007380 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007381 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007382
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007383 /*
7384 * If we got specified a target task, also iterate its context and
7385 * deliver this event there too.
7386 */
7387 if (task && task != current) {
7388 struct perf_event_context *ctx;
7389 struct trace_entry *entry = record;
7390
7391 rcu_read_lock();
7392 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
7393 if (!ctx)
7394 goto unlock;
7395
7396 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
7397 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7398 continue;
7399 if (event->attr.config != entry->type)
7400 continue;
7401 if (perf_tp_event_match(event, &data, regs))
7402 perf_swevent_event(event, count, &data, regs);
7403 }
7404unlock:
7405 rcu_read_unlock();
7406 }
7407
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007408 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007409}
7410EXPORT_SYMBOL_GPL(perf_tp_event);
7411
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007412static void tp_perf_event_destroy(struct perf_event *event)
7413{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007414 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007415}
7416
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007417static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007418{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007419 int err;
7420
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007421 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7422 return -ENOENT;
7423
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007424 /*
7425 * no branch sampling for tracepoint events
7426 */
7427 if (has_branch_stack(event))
7428 return -EOPNOTSUPP;
7429
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007430 err = perf_trace_init(event);
7431 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007432 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007433
7434 event->destroy = tp_perf_event_destroy;
7435
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007436 return 0;
7437}
7438
7439static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007440 .task_ctx_nr = perf_sw_context,
7441
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007442 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007443 .add = perf_trace_add,
7444 .del = perf_trace_del,
7445 .start = perf_swevent_start,
7446 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007447 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007448};
7449
7450static inline void perf_tp_register(void)
7451{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007452 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007453}
Li Zefan6fb29152009-10-15 11:21:42 +08007454
Li Zefan6fb29152009-10-15 11:21:42 +08007455static void perf_event_free_filter(struct perf_event *event)
7456{
7457 ftrace_profile_free_filter(event);
7458}
7459
Alexei Starovoitov25415172015-03-25 12:49:20 -07007460static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7461{
7462 struct bpf_prog *prog;
7463
7464 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7465 return -EINVAL;
7466
7467 if (event->tp_event->prog)
7468 return -EEXIST;
7469
Wang Nan04a22fa2015-07-01 02:13:50 +00007470 if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE))
7471 /* bpf programs can only be attached to u/kprobes */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007472 return -EINVAL;
7473
7474 prog = bpf_prog_get(prog_fd);
7475 if (IS_ERR(prog))
7476 return PTR_ERR(prog);
7477
Linus Torvalds6c373ca2015-04-15 09:00:47 -07007478 if (prog->type != BPF_PROG_TYPE_KPROBE) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007479 /* valid fd, but invalid bpf program type */
7480 bpf_prog_put(prog);
7481 return -EINVAL;
7482 }
7483
7484 event->tp_event->prog = prog;
7485
7486 return 0;
7487}
7488
7489static void perf_event_free_bpf_prog(struct perf_event *event)
7490{
7491 struct bpf_prog *prog;
7492
7493 if (!event->tp_event)
7494 return;
7495
7496 prog = event->tp_event->prog;
7497 if (prog) {
7498 event->tp_event->prog = NULL;
7499 bpf_prog_put(prog);
7500 }
7501}
7502
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007503#else
Li Zefan6fb29152009-10-15 11:21:42 +08007504
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007505static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007506{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007507}
Li Zefan6fb29152009-10-15 11:21:42 +08007508
Li Zefan6fb29152009-10-15 11:21:42 +08007509static void perf_event_free_filter(struct perf_event *event)
7510{
7511}
7512
Alexei Starovoitov25415172015-03-25 12:49:20 -07007513static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7514{
7515 return -ENOENT;
7516}
7517
7518static void perf_event_free_bpf_prog(struct perf_event *event)
7519{
7520}
Li Zefan07b139c2009-12-21 14:27:35 +08007521#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007522
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007523#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007524void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007525{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007526 struct perf_sample_data sample;
7527 struct pt_regs *regs = data;
7528
Robert Richterfd0d0002012-04-02 20:19:08 +02007529 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007530
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007531 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007532 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007533}
7534#endif
7535
Alexander Shishkin375637b2016-04-27 18:44:46 +03007536/*
7537 * Allocate a new address filter
7538 */
7539static struct perf_addr_filter *
7540perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
7541{
7542 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
7543 struct perf_addr_filter *filter;
7544
7545 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
7546 if (!filter)
7547 return NULL;
7548
7549 INIT_LIST_HEAD(&filter->entry);
7550 list_add_tail(&filter->entry, filters);
7551
7552 return filter;
7553}
7554
7555static void free_filters_list(struct list_head *filters)
7556{
7557 struct perf_addr_filter *filter, *iter;
7558
7559 list_for_each_entry_safe(filter, iter, filters, entry) {
7560 if (filter->inode)
7561 iput(filter->inode);
7562 list_del(&filter->entry);
7563 kfree(filter);
7564 }
7565}
7566
7567/*
7568 * Free existing address filters and optionally install new ones
7569 */
7570static void perf_addr_filters_splice(struct perf_event *event,
7571 struct list_head *head)
7572{
7573 unsigned long flags;
7574 LIST_HEAD(list);
7575
7576 if (!has_addr_filter(event))
7577 return;
7578
7579 /* don't bother with children, they don't have their own filters */
7580 if (event->parent)
7581 return;
7582
7583 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
7584
7585 list_splice_init(&event->addr_filters.list, &list);
7586 if (head)
7587 list_splice(head, &event->addr_filters.list);
7588
7589 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
7590
7591 free_filters_list(&list);
7592}
7593
7594/*
7595 * Scan through mm's vmas and see if one of them matches the
7596 * @filter; if so, adjust filter's address range.
7597 * Called with mm::mmap_sem down for reading.
7598 */
7599static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter,
7600 struct mm_struct *mm)
7601{
7602 struct vm_area_struct *vma;
7603
7604 for (vma = mm->mmap; vma; vma = vma->vm_next) {
7605 struct file *file = vma->vm_file;
7606 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
7607 unsigned long vma_size = vma->vm_end - vma->vm_start;
7608
7609 if (!file)
7610 continue;
7611
7612 if (!perf_addr_filter_match(filter, file, off, vma_size))
7613 continue;
7614
7615 return vma->vm_start;
7616 }
7617
7618 return 0;
7619}
7620
7621/*
7622 * Update event's address range filters based on the
7623 * task's existing mappings, if any.
7624 */
7625static void perf_event_addr_filters_apply(struct perf_event *event)
7626{
7627 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
7628 struct task_struct *task = READ_ONCE(event->ctx->task);
7629 struct perf_addr_filter *filter;
7630 struct mm_struct *mm = NULL;
7631 unsigned int count = 0;
7632 unsigned long flags;
7633
7634 /*
7635 * We may observe TASK_TOMBSTONE, which means that the event tear-down
7636 * will stop on the parent's child_mutex that our caller is also holding
7637 */
7638 if (task == TASK_TOMBSTONE)
7639 return;
7640
7641 mm = get_task_mm(event->ctx->task);
7642 if (!mm)
7643 goto restart;
7644
7645 down_read(&mm->mmap_sem);
7646
7647 raw_spin_lock_irqsave(&ifh->lock, flags);
7648 list_for_each_entry(filter, &ifh->list, entry) {
7649 event->addr_filters_offs[count] = 0;
7650
7651 if (perf_addr_filter_needs_mmap(filter))
7652 event->addr_filters_offs[count] =
7653 perf_addr_filter_apply(filter, mm);
7654
7655 count++;
7656 }
7657
7658 event->addr_filters_gen++;
7659 raw_spin_unlock_irqrestore(&ifh->lock, flags);
7660
7661 up_read(&mm->mmap_sem);
7662
7663 mmput(mm);
7664
7665restart:
7666 perf_event_restart(event);
7667}
7668
7669/*
7670 * Address range filtering: limiting the data to certain
7671 * instruction address ranges. Filters are ioctl()ed to us from
7672 * userspace as ascii strings.
7673 *
7674 * Filter string format:
7675 *
7676 * ACTION RANGE_SPEC
7677 * where ACTION is one of the
7678 * * "filter": limit the trace to this region
7679 * * "start": start tracing from this address
7680 * * "stop": stop tracing at this address/region;
7681 * RANGE_SPEC is
7682 * * for kernel addresses: <start address>[/<size>]
7683 * * for object files: <start address>[/<size>]@</path/to/object/file>
7684 *
7685 * if <size> is not specified, the range is treated as a single address.
7686 */
7687enum {
7688 IF_ACT_FILTER,
7689 IF_ACT_START,
7690 IF_ACT_STOP,
7691 IF_SRC_FILE,
7692 IF_SRC_KERNEL,
7693 IF_SRC_FILEADDR,
7694 IF_SRC_KERNELADDR,
7695};
7696
7697enum {
7698 IF_STATE_ACTION = 0,
7699 IF_STATE_SOURCE,
7700 IF_STATE_END,
7701};
7702
7703static const match_table_t if_tokens = {
7704 { IF_ACT_FILTER, "filter" },
7705 { IF_ACT_START, "start" },
7706 { IF_ACT_STOP, "stop" },
7707 { IF_SRC_FILE, "%u/%u@%s" },
7708 { IF_SRC_KERNEL, "%u/%u" },
7709 { IF_SRC_FILEADDR, "%u@%s" },
7710 { IF_SRC_KERNELADDR, "%u" },
7711};
7712
7713/*
7714 * Address filter string parser
7715 */
7716static int
7717perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
7718 struct list_head *filters)
7719{
7720 struct perf_addr_filter *filter = NULL;
7721 char *start, *orig, *filename = NULL;
7722 struct path path;
7723 substring_t args[MAX_OPT_ARGS];
7724 int state = IF_STATE_ACTION, token;
7725 unsigned int kernel = 0;
7726 int ret = -EINVAL;
7727
7728 orig = fstr = kstrdup(fstr, GFP_KERNEL);
7729 if (!fstr)
7730 return -ENOMEM;
7731
7732 while ((start = strsep(&fstr, " ,\n")) != NULL) {
7733 ret = -EINVAL;
7734
7735 if (!*start)
7736 continue;
7737
7738 /* filter definition begins */
7739 if (state == IF_STATE_ACTION) {
7740 filter = perf_addr_filter_new(event, filters);
7741 if (!filter)
7742 goto fail;
7743 }
7744
7745 token = match_token(start, if_tokens, args);
7746 switch (token) {
7747 case IF_ACT_FILTER:
7748 case IF_ACT_START:
7749 filter->filter = 1;
7750
7751 case IF_ACT_STOP:
7752 if (state != IF_STATE_ACTION)
7753 goto fail;
7754
7755 state = IF_STATE_SOURCE;
7756 break;
7757
7758 case IF_SRC_KERNELADDR:
7759 case IF_SRC_KERNEL:
7760 kernel = 1;
7761
7762 case IF_SRC_FILEADDR:
7763 case IF_SRC_FILE:
7764 if (state != IF_STATE_SOURCE)
7765 goto fail;
7766
7767 if (token == IF_SRC_FILE || token == IF_SRC_KERNEL)
7768 filter->range = 1;
7769
7770 *args[0].to = 0;
7771 ret = kstrtoul(args[0].from, 0, &filter->offset);
7772 if (ret)
7773 goto fail;
7774
7775 if (filter->range) {
7776 *args[1].to = 0;
7777 ret = kstrtoul(args[1].from, 0, &filter->size);
7778 if (ret)
7779 goto fail;
7780 }
7781
7782 if (token == IF_SRC_FILE) {
7783 filename = match_strdup(&args[2]);
7784 if (!filename) {
7785 ret = -ENOMEM;
7786 goto fail;
7787 }
7788 }
7789
7790 state = IF_STATE_END;
7791 break;
7792
7793 default:
7794 goto fail;
7795 }
7796
7797 /*
7798 * Filter definition is fully parsed, validate and install it.
7799 * Make sure that it doesn't contradict itself or the event's
7800 * attribute.
7801 */
7802 if (state == IF_STATE_END) {
7803 if (kernel && event->attr.exclude_kernel)
7804 goto fail;
7805
7806 if (!kernel) {
7807 if (!filename)
7808 goto fail;
7809
7810 /* look up the path and grab its inode */
7811 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
7812 if (ret)
7813 goto fail_free_name;
7814
7815 filter->inode = igrab(d_inode(path.dentry));
7816 path_put(&path);
7817 kfree(filename);
7818 filename = NULL;
7819
7820 ret = -EINVAL;
7821 if (!filter->inode ||
7822 !S_ISREG(filter->inode->i_mode))
7823 /* free_filters_list() will iput() */
7824 goto fail;
7825 }
7826
7827 /* ready to consume more filters */
7828 state = IF_STATE_ACTION;
7829 filter = NULL;
7830 }
7831 }
7832
7833 if (state != IF_STATE_ACTION)
7834 goto fail;
7835
7836 kfree(orig);
7837
7838 return 0;
7839
7840fail_free_name:
7841 kfree(filename);
7842fail:
7843 free_filters_list(filters);
7844 kfree(orig);
7845
7846 return ret;
7847}
7848
7849static int
7850perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
7851{
7852 LIST_HEAD(filters);
7853 int ret;
7854
7855 /*
7856 * Since this is called in perf_ioctl() path, we're already holding
7857 * ctx::mutex.
7858 */
7859 lockdep_assert_held(&event->ctx->mutex);
7860
7861 if (WARN_ON_ONCE(event->parent))
7862 return -EINVAL;
7863
7864 /*
7865 * For now, we only support filtering in per-task events; doing so
7866 * for CPU-wide events requires additional context switching trickery,
7867 * since same object code will be mapped at different virtual
7868 * addresses in different processes.
7869 */
7870 if (!event->ctx->task)
7871 return -EOPNOTSUPP;
7872
7873 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
7874 if (ret)
7875 return ret;
7876
7877 ret = event->pmu->addr_filters_validate(&filters);
7878 if (ret) {
7879 free_filters_list(&filters);
7880 return ret;
7881 }
7882
7883 /* remove existing filters, if any */
7884 perf_addr_filters_splice(event, &filters);
7885
7886 /* install new filters */
7887 perf_event_for_each_child(event, perf_event_addr_filters_apply);
7888
7889 return ret;
7890}
7891
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03007892static int perf_event_set_filter(struct perf_event *event, void __user *arg)
7893{
7894 char *filter_str;
7895 int ret = -EINVAL;
7896
Alexander Shishkin375637b2016-04-27 18:44:46 +03007897 if ((event->attr.type != PERF_TYPE_TRACEPOINT ||
7898 !IS_ENABLED(CONFIG_EVENT_TRACING)) &&
7899 !has_addr_filter(event))
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03007900 return -EINVAL;
7901
7902 filter_str = strndup_user(arg, PAGE_SIZE);
7903 if (IS_ERR(filter_str))
7904 return PTR_ERR(filter_str);
7905
7906 if (IS_ENABLED(CONFIG_EVENT_TRACING) &&
7907 event->attr.type == PERF_TYPE_TRACEPOINT)
7908 ret = ftrace_profile_set_filter(event, event->attr.config,
7909 filter_str);
Alexander Shishkin375637b2016-04-27 18:44:46 +03007910 else if (has_addr_filter(event))
7911 ret = perf_event_set_addr_filter(event, filter_str);
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03007912
7913 kfree(filter_str);
7914 return ret;
7915}
7916
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007917/*
7918 * hrtimer based swevent callback
7919 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007920
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007921static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007922{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007923 enum hrtimer_restart ret = HRTIMER_RESTART;
7924 struct perf_sample_data data;
7925 struct pt_regs *regs;
7926 struct perf_event *event;
7927 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007928
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007929 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007930
7931 if (event->state != PERF_EVENT_STATE_ACTIVE)
7932 return HRTIMER_NORESTART;
7933
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007934 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007935
Robert Richterfd0d0002012-04-02 20:19:08 +02007936 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007937 regs = get_irq_regs();
7938
7939 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08007940 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02007941 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007942 ret = HRTIMER_NORESTART;
7943 }
7944
7945 period = max_t(u64, 10000, event->hw.sample_period);
7946 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
7947
7948 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007949}
7950
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007951static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007952{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007953 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007954 s64 period;
7955
7956 if (!is_sampling_event(event))
7957 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007958
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007959 period = local64_read(&hwc->period_left);
7960 if (period) {
7961 if (period < 0)
7962 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007963
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01007964 local64_set(&hwc->period_left, 0);
7965 } else {
7966 period = max_t(u64, 10000, hwc->sample_period);
7967 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00007968 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
7969 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007970}
7971
7972static void perf_swevent_cancel_hrtimer(struct perf_event *event)
7973{
7974 struct hw_perf_event *hwc = &event->hw;
7975
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007976 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007977 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02007978 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007979
7980 hrtimer_cancel(&hwc->hrtimer);
7981 }
7982}
7983
Peter Zijlstraba3dd362011-02-15 12:41:46 +01007984static void perf_swevent_init_hrtimer(struct perf_event *event)
7985{
7986 struct hw_perf_event *hwc = &event->hw;
7987
7988 if (!is_sampling_event(event))
7989 return;
7990
7991 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
7992 hwc->hrtimer.function = perf_swevent_hrtimer;
7993
7994 /*
7995 * Since hrtimers have a fixed rate, we can do a static freq->period
7996 * mapping and avoid the whole period adjust feedback stuff.
7997 */
7998 if (event->attr.freq) {
7999 long freq = event->attr.sample_freq;
8000
8001 event->attr.sample_period = NSEC_PER_SEC / freq;
8002 hwc->sample_period = event->attr.sample_period;
8003 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09008004 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008005 event->attr.freq = 0;
8006 }
8007}
8008
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008009/*
8010 * Software event: cpu wall time clock
8011 */
8012
8013static void cpu_clock_event_update(struct perf_event *event)
8014{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008015 s64 prev;
8016 u64 now;
8017
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008018 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008019 prev = local64_xchg(&event->hw.prev_count, now);
8020 local64_add(now - prev, &event->count);
8021}
8022
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008023static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008024{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008025 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008026 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008027}
8028
8029static void cpu_clock_event_stop(struct perf_event *event, int flags)
8030{
8031 perf_swevent_cancel_hrtimer(event);
8032 cpu_clock_event_update(event);
8033}
8034
8035static int cpu_clock_event_add(struct perf_event *event, int flags)
8036{
8037 if (flags & PERF_EF_START)
8038 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008039 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008040
8041 return 0;
8042}
8043
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008044static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008045{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008046 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008047}
8048
8049static void cpu_clock_event_read(struct perf_event *event)
8050{
8051 cpu_clock_event_update(event);
8052}
8053
8054static int cpu_clock_event_init(struct perf_event *event)
8055{
8056 if (event->attr.type != PERF_TYPE_SOFTWARE)
8057 return -ENOENT;
8058
8059 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
8060 return -ENOENT;
8061
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008062 /*
8063 * no branch sampling for software events
8064 */
8065 if (has_branch_stack(event))
8066 return -EOPNOTSUPP;
8067
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008068 perf_swevent_init_hrtimer(event);
8069
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008070 return 0;
8071}
8072
8073static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008074 .task_ctx_nr = perf_sw_context,
8075
Peter Zijlstra34f43922015-02-20 14:05:38 +01008076 .capabilities = PERF_PMU_CAP_NO_NMI,
8077
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008078 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008079 .add = cpu_clock_event_add,
8080 .del = cpu_clock_event_del,
8081 .start = cpu_clock_event_start,
8082 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008083 .read = cpu_clock_event_read,
8084};
8085
8086/*
8087 * Software event: task time clock
8088 */
8089
8090static void task_clock_event_update(struct perf_event *event, u64 now)
8091{
8092 u64 prev;
8093 s64 delta;
8094
8095 prev = local64_xchg(&event->hw.prev_count, now);
8096 delta = now - prev;
8097 local64_add(delta, &event->count);
8098}
8099
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008100static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008101{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008102 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008103 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008104}
8105
8106static void task_clock_event_stop(struct perf_event *event, int flags)
8107{
8108 perf_swevent_cancel_hrtimer(event);
8109 task_clock_event_update(event, event->ctx->time);
8110}
8111
8112static int task_clock_event_add(struct perf_event *event, int flags)
8113{
8114 if (flags & PERF_EF_START)
8115 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008116 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008117
8118 return 0;
8119}
8120
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008121static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008122{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008123 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008124}
8125
8126static void task_clock_event_read(struct perf_event *event)
8127{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01008128 u64 now = perf_clock();
8129 u64 delta = now - event->ctx->timestamp;
8130 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008131
8132 task_clock_event_update(event, time);
8133}
8134
8135static int task_clock_event_init(struct perf_event *event)
8136{
8137 if (event->attr.type != PERF_TYPE_SOFTWARE)
8138 return -ENOENT;
8139
8140 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
8141 return -ENOENT;
8142
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008143 /*
8144 * no branch sampling for software events
8145 */
8146 if (has_branch_stack(event))
8147 return -EOPNOTSUPP;
8148
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008149 perf_swevent_init_hrtimer(event);
8150
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008151 return 0;
8152}
8153
8154static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008155 .task_ctx_nr = perf_sw_context,
8156
Peter Zijlstra34f43922015-02-20 14:05:38 +01008157 .capabilities = PERF_PMU_CAP_NO_NMI,
8158
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008159 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008160 .add = task_clock_event_add,
8161 .del = task_clock_event_del,
8162 .start = task_clock_event_start,
8163 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008164 .read = task_clock_event_read,
8165};
8166
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008167static void perf_pmu_nop_void(struct pmu *pmu)
8168{
8169}
8170
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008171static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
8172{
8173}
8174
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008175static int perf_pmu_nop_int(struct pmu *pmu)
8176{
8177 return 0;
8178}
8179
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008180static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008181
8182static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008183{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008184 __this_cpu_write(nop_txn_flags, flags);
8185
8186 if (flags & ~PERF_PMU_TXN_ADD)
8187 return;
8188
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008189 perf_pmu_disable(pmu);
8190}
8191
8192static int perf_pmu_commit_txn(struct pmu *pmu)
8193{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008194 unsigned int flags = __this_cpu_read(nop_txn_flags);
8195
8196 __this_cpu_write(nop_txn_flags, 0);
8197
8198 if (flags & ~PERF_PMU_TXN_ADD)
8199 return 0;
8200
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008201 perf_pmu_enable(pmu);
8202 return 0;
8203}
8204
8205static void perf_pmu_cancel_txn(struct pmu *pmu)
8206{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008207 unsigned int flags = __this_cpu_read(nop_txn_flags);
8208
8209 __this_cpu_write(nop_txn_flags, 0);
8210
8211 if (flags & ~PERF_PMU_TXN_ADD)
8212 return;
8213
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008214 perf_pmu_enable(pmu);
8215}
8216
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008217static int perf_event_idx_default(struct perf_event *event)
8218{
Peter Zijlstrac719f562014-10-21 11:10:21 +02008219 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008220}
8221
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008222/*
8223 * Ensures all contexts with the same task_ctx_nr have the same
8224 * pmu_cpu_context too.
8225 */
Mark Rutland9e317042014-02-10 17:44:18 +00008226static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008227{
8228 struct pmu *pmu;
8229
8230 if (ctxn < 0)
8231 return NULL;
8232
8233 list_for_each_entry(pmu, &pmus, entry) {
8234 if (pmu->task_ctx_nr == ctxn)
8235 return pmu->pmu_cpu_context;
8236 }
8237
8238 return NULL;
8239}
8240
Peter Zijlstra51676952010-12-07 14:18:20 +01008241static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008242{
Peter Zijlstra51676952010-12-07 14:18:20 +01008243 int cpu;
8244
8245 for_each_possible_cpu(cpu) {
8246 struct perf_cpu_context *cpuctx;
8247
8248 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8249
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008250 if (cpuctx->unique_pmu == old_pmu)
8251 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01008252 }
8253}
8254
8255static void free_pmu_context(struct pmu *pmu)
8256{
8257 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008258
8259 mutex_lock(&pmus_lock);
8260 /*
8261 * Like a real lame refcount.
8262 */
Peter Zijlstra51676952010-12-07 14:18:20 +01008263 list_for_each_entry(i, &pmus, entry) {
8264 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
8265 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008266 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01008267 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008268 }
8269
Peter Zijlstra51676952010-12-07 14:18:20 +01008270 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008271out:
8272 mutex_unlock(&pmus_lock);
8273}
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008274
8275/*
8276 * Let userspace know that this PMU supports address range filtering:
8277 */
8278static ssize_t nr_addr_filters_show(struct device *dev,
8279 struct device_attribute *attr,
8280 char *page)
8281{
8282 struct pmu *pmu = dev_get_drvdata(dev);
8283
8284 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
8285}
8286DEVICE_ATTR_RO(nr_addr_filters);
8287
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008288static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008289
Peter Zijlstraabe43402010-11-17 23:17:37 +01008290static ssize_t
8291type_show(struct device *dev, struct device_attribute *attr, char *page)
8292{
8293 struct pmu *pmu = dev_get_drvdata(dev);
8294
8295 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
8296}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008297static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008298
Stephane Eranian62b85632013-04-03 14:21:34 +02008299static ssize_t
8300perf_event_mux_interval_ms_show(struct device *dev,
8301 struct device_attribute *attr,
8302 char *page)
8303{
8304 struct pmu *pmu = dev_get_drvdata(dev);
8305
8306 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
8307}
8308
Peter Zijlstra272325c2015-04-15 11:41:58 +02008309static DEFINE_MUTEX(mux_interval_mutex);
8310
Stephane Eranian62b85632013-04-03 14:21:34 +02008311static ssize_t
8312perf_event_mux_interval_ms_store(struct device *dev,
8313 struct device_attribute *attr,
8314 const char *buf, size_t count)
8315{
8316 struct pmu *pmu = dev_get_drvdata(dev);
8317 int timer, cpu, ret;
8318
8319 ret = kstrtoint(buf, 0, &timer);
8320 if (ret)
8321 return ret;
8322
8323 if (timer < 1)
8324 return -EINVAL;
8325
8326 /* same value, noting to do */
8327 if (timer == pmu->hrtimer_interval_ms)
8328 return count;
8329
Peter Zijlstra272325c2015-04-15 11:41:58 +02008330 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008331 pmu->hrtimer_interval_ms = timer;
8332
8333 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02008334 get_online_cpus();
8335 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02008336 struct perf_cpu_context *cpuctx;
8337 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8338 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
8339
Peter Zijlstra272325c2015-04-15 11:41:58 +02008340 cpu_function_call(cpu,
8341 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02008342 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02008343 put_online_cpus();
8344 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008345
8346 return count;
8347}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008348static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02008349
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008350static struct attribute *pmu_dev_attrs[] = {
8351 &dev_attr_type.attr,
8352 &dev_attr_perf_event_mux_interval_ms.attr,
8353 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008354};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008355ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008356
8357static int pmu_bus_running;
8358static struct bus_type pmu_bus = {
8359 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008360 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008361};
8362
8363static void pmu_dev_release(struct device *dev)
8364{
8365 kfree(dev);
8366}
8367
8368static int pmu_dev_alloc(struct pmu *pmu)
8369{
8370 int ret = -ENOMEM;
8371
8372 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
8373 if (!pmu->dev)
8374 goto out;
8375
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01008376 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01008377 device_initialize(pmu->dev);
8378 ret = dev_set_name(pmu->dev, "%s", pmu->name);
8379 if (ret)
8380 goto free_dev;
8381
8382 dev_set_drvdata(pmu->dev, pmu);
8383 pmu->dev->bus = &pmu_bus;
8384 pmu->dev->release = pmu_dev_release;
8385 ret = device_add(pmu->dev);
8386 if (ret)
8387 goto free_dev;
8388
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008389 /* For PMUs with address filters, throw in an extra attribute: */
8390 if (pmu->nr_addr_filters)
8391 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
8392
8393 if (ret)
8394 goto del_dev;
8395
Peter Zijlstraabe43402010-11-17 23:17:37 +01008396out:
8397 return ret;
8398
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008399del_dev:
8400 device_del(pmu->dev);
8401
Peter Zijlstraabe43402010-11-17 23:17:37 +01008402free_dev:
8403 put_device(pmu->dev);
8404 goto out;
8405}
8406
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008407static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008408static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008409
Mischa Jonker03d8e802013-06-04 11:45:48 +02008410int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008411{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008412 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008413
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008414 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008415 ret = -ENOMEM;
8416 pmu->pmu_disable_count = alloc_percpu(int);
8417 if (!pmu->pmu_disable_count)
8418 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008419
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008420 pmu->type = -1;
8421 if (!name)
8422 goto skip_type;
8423 pmu->name = name;
8424
8425 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08008426 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
8427 if (type < 0) {
8428 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008429 goto free_pdc;
8430 }
8431 }
8432 pmu->type = type;
8433
Peter Zijlstraabe43402010-11-17 23:17:37 +01008434 if (pmu_bus_running) {
8435 ret = pmu_dev_alloc(pmu);
8436 if (ret)
8437 goto free_idr;
8438 }
8439
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008440skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +01008441 if (pmu->task_ctx_nr == perf_hw_context) {
8442 static int hw_context_taken = 0;
8443
Mark Rutland5101ef22016-04-26 11:33:46 +01008444 /*
8445 * Other than systems with heterogeneous CPUs, it never makes
8446 * sense for two PMUs to share perf_hw_context. PMUs which are
8447 * uncore must use perf_invalid_context.
8448 */
8449 if (WARN_ON_ONCE(hw_context_taken &&
8450 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
Peter Zijlstra26657842016-03-22 22:09:18 +01008451 pmu->task_ctx_nr = perf_invalid_context;
8452
8453 hw_context_taken = 1;
8454 }
8455
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008456 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
8457 if (pmu->pmu_cpu_context)
8458 goto got_cpu_context;
8459
Wei Yongjunc4814202013-04-12 11:05:54 +08008460 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008461 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
8462 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01008463 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008464
8465 for_each_possible_cpu(cpu) {
8466 struct perf_cpu_context *cpuctx;
8467
8468 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02008469 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008470 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008471 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008472 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02008473
Peter Zijlstra272325c2015-04-15 11:41:58 +02008474 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02008475
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008476 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008477 }
8478
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008479got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008480 if (!pmu->start_txn) {
8481 if (pmu->pmu_enable) {
8482 /*
8483 * If we have pmu_enable/pmu_disable calls, install
8484 * transaction stubs that use that to try and batch
8485 * hardware accesses.
8486 */
8487 pmu->start_txn = perf_pmu_start_txn;
8488 pmu->commit_txn = perf_pmu_commit_txn;
8489 pmu->cancel_txn = perf_pmu_cancel_txn;
8490 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008491 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008492 pmu->commit_txn = perf_pmu_nop_int;
8493 pmu->cancel_txn = perf_pmu_nop_void;
8494 }
8495 }
8496
8497 if (!pmu->pmu_enable) {
8498 pmu->pmu_enable = perf_pmu_nop_void;
8499 pmu->pmu_disable = perf_pmu_nop_void;
8500 }
8501
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008502 if (!pmu->event_idx)
8503 pmu->event_idx = perf_event_idx_default;
8504
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008505 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008506 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008507 ret = 0;
8508unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008509 mutex_unlock(&pmus_lock);
8510
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008511 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008512
Peter Zijlstraabe43402010-11-17 23:17:37 +01008513free_dev:
8514 device_del(pmu->dev);
8515 put_device(pmu->dev);
8516
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008517free_idr:
8518 if (pmu->type >= PERF_TYPE_MAX)
8519 idr_remove(&pmu_idr, pmu->type);
8520
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008521free_pdc:
8522 free_percpu(pmu->pmu_disable_count);
8523 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008524}
Yan, Zhengc464c762014-03-18 16:56:41 +08008525EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008526
8527void perf_pmu_unregister(struct pmu *pmu)
8528{
8529 mutex_lock(&pmus_lock);
8530 list_del_rcu(&pmu->entry);
8531 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008532
8533 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02008534 * We dereference the pmu list under both SRCU and regular RCU, so
8535 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008536 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008537 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02008538 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008539
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008540 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008541 if (pmu->type >= PERF_TYPE_MAX)
8542 idr_remove(&pmu_idr, pmu->type);
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008543 if (pmu->nr_addr_filters)
8544 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008545 device_del(pmu->dev);
8546 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01008547 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008548}
Yan, Zhengc464c762014-03-18 16:56:41 +08008549EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008550
Mark Rutlandcc34b982015-01-07 14:56:51 +00008551static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
8552{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008553 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00008554 int ret;
8555
8556 if (!try_module_get(pmu->module))
8557 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008558
8559 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02008560 /*
8561 * This ctx->mutex can nest when we're called through
8562 * inheritance. See the perf_event_ctx_lock_nested() comment.
8563 */
8564 ctx = perf_event_ctx_lock_nested(event->group_leader,
8565 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008566 BUG_ON(!ctx);
8567 }
8568
Mark Rutlandcc34b982015-01-07 14:56:51 +00008569 event->pmu = pmu;
8570 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008571
8572 if (ctx)
8573 perf_event_ctx_unlock(event->group_leader, ctx);
8574
Mark Rutlandcc34b982015-01-07 14:56:51 +00008575 if (ret)
8576 module_put(pmu->module);
8577
8578 return ret;
8579}
8580
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008581static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008582{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02008583 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008584 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08008585 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008586
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008587 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008588
8589 rcu_read_lock();
8590 pmu = idr_find(&pmu_idr, event->attr.type);
8591 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08008592 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00008593 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08008594 if (ret)
8595 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008596 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08008597 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008598
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008599 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00008600 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008601 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008602 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008603
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008604 if (ret != -ENOENT) {
8605 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008606 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008607 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008608 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008609 pmu = ERR_PTR(-ENOENT);
8610unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008611 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008612
8613 return pmu;
8614}
8615
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008616static void account_event_cpu(struct perf_event *event, int cpu)
8617{
8618 if (event->parent)
8619 return;
8620
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008621 if (is_cgroup_event(event))
8622 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
8623}
8624
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02008625/* Freq events need the tick to stay alive (see perf_event_task_tick). */
8626static void account_freq_event_nohz(void)
8627{
8628#ifdef CONFIG_NO_HZ_FULL
8629 /* Lock so we don't race with concurrent unaccount */
8630 spin_lock(&nr_freq_lock);
8631 if (atomic_inc_return(&nr_freq_events) == 1)
8632 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
8633 spin_unlock(&nr_freq_lock);
8634#endif
8635}
8636
8637static void account_freq_event(void)
8638{
8639 if (tick_nohz_full_enabled())
8640 account_freq_event_nohz();
8641 else
8642 atomic_inc(&nr_freq_events);
8643}
8644
8645
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008646static void account_event(struct perf_event *event)
8647{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008648 bool inc = false;
8649
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008650 if (event->parent)
8651 return;
8652
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008653 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008654 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008655 if (event->attr.mmap || event->attr.mmap_data)
8656 atomic_inc(&nr_mmap_events);
8657 if (event->attr.comm)
8658 atomic_inc(&nr_comm_events);
8659 if (event->attr.task)
8660 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02008661 if (event->attr.freq)
8662 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03008663 if (event->attr.context_switch) {
8664 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008665 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03008666 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008667 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008668 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008669 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008670 inc = true;
8671
Peter Zijlstra9107c892016-02-24 18:45:45 +01008672 if (inc) {
8673 if (atomic_inc_not_zero(&perf_sched_count))
8674 goto enabled;
8675
8676 mutex_lock(&perf_sched_mutex);
8677 if (!atomic_read(&perf_sched_count)) {
8678 static_branch_enable(&perf_sched_events);
8679 /*
8680 * Guarantee that all CPUs observe they key change and
8681 * call the perf scheduling hooks before proceeding to
8682 * install events that need them.
8683 */
8684 synchronize_sched();
8685 }
8686 /*
8687 * Now that we have waited for the sync_sched(), allow further
8688 * increments to by-pass the mutex.
8689 */
8690 atomic_inc(&perf_sched_count);
8691 mutex_unlock(&perf_sched_mutex);
8692 }
8693enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008694
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008695 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008696}
8697
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008698/*
8699 * Allocate and initialize a event structure
8700 */
8701static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008702perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008703 struct task_struct *task,
8704 struct perf_event *group_leader,
8705 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008706 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00008707 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008708{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02008709 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008710 struct perf_event *event;
8711 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008712 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008713
Oleg Nesterov66832eb2011-01-18 17:10:32 +01008714 if ((unsigned)cpu >= nr_cpu_ids) {
8715 if (!task || cpu != -1)
8716 return ERR_PTR(-EINVAL);
8717 }
8718
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008719 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008720 if (!event)
8721 return ERR_PTR(-ENOMEM);
8722
8723 /*
8724 * Single events are their own group leaders, with an
8725 * empty sibling list:
8726 */
8727 if (!group_leader)
8728 group_leader = event;
8729
8730 mutex_init(&event->child_mutex);
8731 INIT_LIST_HEAD(&event->child_list);
8732
8733 INIT_LIST_HEAD(&event->group_entry);
8734 INIT_LIST_HEAD(&event->event_entry);
8735 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01008736 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01008737 INIT_LIST_HEAD(&event->active_entry);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008738 INIT_LIST_HEAD(&event->addr_filters.list);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01008739 INIT_HLIST_NODE(&event->hlist_entry);
8740
Peter Zijlstra10c6db12011-11-26 02:47:31 +01008741
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008742 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08008743 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008744
8745 mutex_init(&event->mmap_mutex);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008746 raw_spin_lock_init(&event->addr_filters.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008747
Al Viroa6fa9412012-08-20 14:59:25 +01008748 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008749 event->cpu = cpu;
8750 event->attr = *attr;
8751 event->group_leader = group_leader;
8752 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008753 event->oncpu = -1;
8754
8755 event->parent = parent_event;
8756
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08008757 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008758 event->id = atomic64_inc_return(&perf_event_id);
8759
8760 event->state = PERF_EVENT_STATE_INACTIVE;
8761
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008762 if (task) {
8763 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008764 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01008765 * XXX pmu::event_init needs to know what task to account to
8766 * and we cannot use the ctx information because we need the
8767 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008768 */
Peter Zijlstra50f16a82015-03-05 22:10:19 +01008769 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008770 }
8771
Peter Zijlstra34f43922015-02-20 14:05:38 +01008772 event->clock = &local_clock;
8773 if (parent_event)
8774 event->clock = parent_event->clock;
8775
Avi Kivity4dc0da82011-06-29 18:42:35 +03008776 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01008777 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03008778 context = parent_event->overflow_handler_context;
8779 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01008780
Wang Nan18794452016-03-28 06:41:30 +00008781 if (overflow_handler) {
8782 event->overflow_handler = overflow_handler;
8783 event->overflow_handler_context = context;
Wang Nan9ecda412016-04-05 14:11:18 +00008784 } else if (is_write_backward(event)){
8785 event->overflow_handler = perf_event_output_backward;
8786 event->overflow_handler_context = NULL;
Wang Nan18794452016-03-28 06:41:30 +00008787 } else {
Wang Nan9ecda412016-04-05 14:11:18 +00008788 event->overflow_handler = perf_event_output_forward;
Wang Nan18794452016-03-28 06:41:30 +00008789 event->overflow_handler_context = NULL;
8790 }
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02008791
Jiri Olsa0231bb52013-02-01 11:23:45 +01008792 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008793
8794 pmu = NULL;
8795
8796 hwc = &event->hw;
8797 hwc->sample_period = attr->sample_period;
8798 if (attr->freq && attr->sample_freq)
8799 hwc->sample_period = 1;
8800 hwc->last_period = hwc->sample_period;
8801
Peter Zijlstrae7850592010-05-21 14:43:08 +02008802 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008803
8804 /*
8805 * we currently do not support PERF_FORMAT_GROUP on inherited events
8806 */
8807 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008808 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008809
Yan, Zhenga46a2302014-11-04 21:56:06 -05008810 if (!has_branch_stack(event))
8811 event->attr.branch_sample_type = 0;
8812
Matt Fleming79dff512015-01-23 18:45:42 +00008813 if (cgroup_fd != -1) {
8814 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
8815 if (err)
8816 goto err_ns;
8817 }
8818
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008819 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008820 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008821 goto err_ns;
8822 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008823 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008824 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008825 }
8826
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008827 err = exclusive_event_init(event);
8828 if (err)
8829 goto err_pmu;
8830
Alexander Shishkin375637b2016-04-27 18:44:46 +03008831 if (has_addr_filter(event)) {
8832 event->addr_filters_offs = kcalloc(pmu->nr_addr_filters,
8833 sizeof(unsigned long),
8834 GFP_KERNEL);
8835 if (!event->addr_filters_offs)
8836 goto err_per_task;
8837
8838 /* force hw sync on the address filters */
8839 event->addr_filters_gen = 1;
8840 }
8841
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008842 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02008843 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
8844 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008845 if (err)
Alexander Shishkin375637b2016-04-27 18:44:46 +03008846 goto err_addr_filters;
Stephane Eraniand010b332012-02-09 23:21:00 +01008847 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008848 }
8849
Alexander Shishkin927a5572016-03-02 13:24:14 +02008850 /* symmetric to unaccount_event() in _free_event() */
8851 account_event(event);
8852
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008853 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008854
Alexander Shishkin375637b2016-04-27 18:44:46 +03008855err_addr_filters:
8856 kfree(event->addr_filters_offs);
8857
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008858err_per_task:
8859 exclusive_event_destroy(event);
8860
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008861err_pmu:
8862 if (event->destroy)
8863 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08008864 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008865err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00008866 if (is_cgroup_event(event))
8867 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008868 if (event->ns)
8869 put_pid_ns(event->ns);
8870 kfree(event);
8871
8872 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008873}
8874
8875static int perf_copy_attr(struct perf_event_attr __user *uattr,
8876 struct perf_event_attr *attr)
8877{
8878 u32 size;
8879 int ret;
8880
8881 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
8882 return -EFAULT;
8883
8884 /*
8885 * zero the full structure, so that a short copy will be nice.
8886 */
8887 memset(attr, 0, sizeof(*attr));
8888
8889 ret = get_user(size, &uattr->size);
8890 if (ret)
8891 return ret;
8892
8893 if (size > PAGE_SIZE) /* silly large */
8894 goto err_size;
8895
8896 if (!size) /* abi compat */
8897 size = PERF_ATTR_SIZE_VER0;
8898
8899 if (size < PERF_ATTR_SIZE_VER0)
8900 goto err_size;
8901
8902 /*
8903 * If we're handed a bigger struct than we know of,
8904 * ensure all the unknown bits are 0 - i.e. new
8905 * user-space does not rely on any kernel feature
8906 * extensions we dont know about yet.
8907 */
8908 if (size > sizeof(*attr)) {
8909 unsigned char __user *addr;
8910 unsigned char __user *end;
8911 unsigned char val;
8912
8913 addr = (void __user *)uattr + sizeof(*attr);
8914 end = (void __user *)uattr + size;
8915
8916 for (; addr < end; addr++) {
8917 ret = get_user(val, addr);
8918 if (ret)
8919 return ret;
8920 if (val)
8921 goto err_size;
8922 }
8923 size = sizeof(*attr);
8924 }
8925
8926 ret = copy_from_user(attr, uattr, size);
8927 if (ret)
8928 return -EFAULT;
8929
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05308930 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008931 return -EINVAL;
8932
8933 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
8934 return -EINVAL;
8935
8936 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
8937 return -EINVAL;
8938
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008939 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
8940 u64 mask = attr->branch_sample_type;
8941
8942 /* only using defined bits */
8943 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
8944 return -EINVAL;
8945
8946 /* at least one branch bit must be set */
8947 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
8948 return -EINVAL;
8949
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008950 /* propagate priv level, when not set for branch */
8951 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
8952
8953 /* exclude_kernel checked on syscall entry */
8954 if (!attr->exclude_kernel)
8955 mask |= PERF_SAMPLE_BRANCH_KERNEL;
8956
8957 if (!attr->exclude_user)
8958 mask |= PERF_SAMPLE_BRANCH_USER;
8959
8960 if (!attr->exclude_hv)
8961 mask |= PERF_SAMPLE_BRANCH_HV;
8962 /*
8963 * adjust user setting (for HW filter setup)
8964 */
8965 attr->branch_sample_type = mask;
8966 }
Stephane Eraniane7122092013-06-06 11:02:04 +02008967 /* privileged levels capture (kernel, hv): check permissions */
8968 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02008969 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
8970 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01008971 }
Jiri Olsa40189942012-08-07 15:20:37 +02008972
Jiri Olsac5ebced2012-08-07 15:20:40 +02008973 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02008974 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02008975 if (ret)
8976 return ret;
8977 }
8978
8979 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
8980 if (!arch_perf_have_user_stack_dump())
8981 return -ENOSYS;
8982
8983 /*
8984 * We have __u32 type for the size, but so far
8985 * we can only use __u16 as maximum due to the
8986 * __u16 sample size limit.
8987 */
8988 if (attr->sample_stack_user >= USHRT_MAX)
8989 ret = -EINVAL;
8990 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
8991 ret = -EINVAL;
8992 }
Jiri Olsa40189942012-08-07 15:20:37 +02008993
Stephane Eranian60e23642014-09-24 13:48:37 +02008994 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
8995 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008996out:
8997 return ret;
8998
8999err_size:
9000 put_user(sizeof(*attr), &uattr->size);
9001 ret = -E2BIG;
9002 goto out;
9003}
9004
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009005static int
9006perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009007{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009008 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009009 int ret = -EINVAL;
9010
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009011 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009012 goto set;
9013
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009014 /* don't allow circular references */
9015 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009016 goto out;
9017
Peter Zijlstra0f139302010-05-20 14:35:15 +02009018 /*
9019 * Don't allow cross-cpu buffers
9020 */
9021 if (output_event->cpu != event->cpu)
9022 goto out;
9023
9024 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02009025 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02009026 */
9027 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
9028 goto out;
9029
Peter Zijlstra34f43922015-02-20 14:05:38 +01009030 /*
9031 * Mixing clocks in the same buffer is trouble you don't need.
9032 */
9033 if (output_event->clock != event->clock)
9034 goto out;
9035
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009036 /*
Wang Nan9ecda412016-04-05 14:11:18 +00009037 * Either writing ring buffer from beginning or from end.
9038 * Mixing is not allowed.
9039 */
9040 if (is_write_backward(output_event) != is_write_backward(event))
9041 goto out;
9042
9043 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009044 * If both events generate aux data, they must be on the same PMU
9045 */
9046 if (has_aux(event) && has_aux(output_event) &&
9047 event->pmu != output_event->pmu)
9048 goto out;
9049
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009050set:
9051 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009052 /* Can't redirect output if we've got an active mmap() */
9053 if (atomic_read(&event->mmap_count))
9054 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009055
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009056 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02009057 /* get the rb we want to redirect to */
9058 rb = ring_buffer_get(output_event);
9059 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009060 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009061 }
9062
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009063 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02009064
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009065 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009066unlock:
9067 mutex_unlock(&event->mmap_mutex);
9068
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009069out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009070 return ret;
9071}
9072
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009073static void mutex_lock_double(struct mutex *a, struct mutex *b)
9074{
9075 if (b < a)
9076 swap(a, b);
9077
9078 mutex_lock(a);
9079 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
9080}
9081
Peter Zijlstra34f43922015-02-20 14:05:38 +01009082static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
9083{
9084 bool nmi_safe = false;
9085
9086 switch (clk_id) {
9087 case CLOCK_MONOTONIC:
9088 event->clock = &ktime_get_mono_fast_ns;
9089 nmi_safe = true;
9090 break;
9091
9092 case CLOCK_MONOTONIC_RAW:
9093 event->clock = &ktime_get_raw_fast_ns;
9094 nmi_safe = true;
9095 break;
9096
9097 case CLOCK_REALTIME:
9098 event->clock = &ktime_get_real_ns;
9099 break;
9100
9101 case CLOCK_BOOTTIME:
9102 event->clock = &ktime_get_boot_ns;
9103 break;
9104
9105 case CLOCK_TAI:
9106 event->clock = &ktime_get_tai_ns;
9107 break;
9108
9109 default:
9110 return -EINVAL;
9111 }
9112
9113 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
9114 return -EINVAL;
9115
9116 return 0;
9117}
9118
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009119/**
9120 * sys_perf_event_open - open a performance event, associate it to a task/cpu
9121 *
9122 * @attr_uptr: event_id type attributes for monitoring/sampling
9123 * @pid: target pid
9124 * @cpu: target cpu
9125 * @group_fd: group leader event fd
9126 */
9127SYSCALL_DEFINE5(perf_event_open,
9128 struct perf_event_attr __user *, attr_uptr,
9129 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
9130{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009131 struct perf_event *group_leader = NULL, *output_event = NULL;
9132 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009133 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009134 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009135 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04009136 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07009137 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009138 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04009139 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009140 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009141 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01009142 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00009143 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009144
9145 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02009146 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009147 return -EINVAL;
9148
9149 err = perf_copy_attr(attr_uptr, &attr);
9150 if (err)
9151 return err;
9152
9153 if (!attr.exclude_kernel) {
9154 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9155 return -EACCES;
9156 }
9157
9158 if (attr.freq) {
9159 if (attr.sample_freq > sysctl_perf_event_sample_rate)
9160 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02009161 } else {
9162 if (attr.sample_period & (1ULL << 63))
9163 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009164 }
9165
Stephane Eraniane5d13672011-02-14 11:20:01 +02009166 /*
9167 * In cgroup mode, the pid argument is used to pass the fd
9168 * opened to the cgroup directory in cgroupfs. The cpu argument
9169 * designates the cpu on which to monitor threads from that
9170 * cgroup.
9171 */
9172 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
9173 return -EINVAL;
9174
Yann Droneauda21b0b32014-01-05 21:36:33 +01009175 if (flags & PERF_FLAG_FD_CLOEXEC)
9176 f_flags |= O_CLOEXEC;
9177
9178 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009179 if (event_fd < 0)
9180 return event_fd;
9181
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009182 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04009183 err = perf_fget_light(group_fd, &group);
9184 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009185 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04009186 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009187 if (flags & PERF_FLAG_FD_OUTPUT)
9188 output_event = group_leader;
9189 if (flags & PERF_FLAG_FD_NO_GROUP)
9190 group_leader = NULL;
9191 }
9192
Stephane Eraniane5d13672011-02-14 11:20:01 +02009193 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009194 task = find_lively_task_by_vpid(pid);
9195 if (IS_ERR(task)) {
9196 err = PTR_ERR(task);
9197 goto err_group_fd;
9198 }
9199 }
9200
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009201 if (task && group_leader &&
9202 group_leader->attr.inherit != attr.inherit) {
9203 err = -EINVAL;
9204 goto err_task;
9205 }
9206
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009207 get_online_cpus();
9208
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009209 if (task) {
9210 err = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
9211 if (err)
9212 goto err_cpus;
9213
9214 /*
9215 * Reuse ptrace permission checks for now.
9216 *
9217 * We must hold cred_guard_mutex across this and any potential
9218 * perf_install_in_context() call for this new event to
9219 * serialize against exec() altering our credentials (and the
9220 * perf_event_exit_task() that could imply).
9221 */
9222 err = -EACCES;
9223 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
9224 goto err_cred;
9225 }
9226
Matt Fleming79dff512015-01-23 18:45:42 +00009227 if (flags & PERF_FLAG_PID_CGROUP)
9228 cgroup_fd = pid;
9229
Avi Kivity4dc0da82011-06-29 18:42:35 +03009230 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009231 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009232 if (IS_ERR(event)) {
9233 err = PTR_ERR(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009234 goto err_cred;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009235 }
9236
Vince Weaver53b25332014-05-16 17:12:12 -04009237 if (is_sampling_event(event)) {
9238 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
9239 err = -ENOTSUPP;
9240 goto err_alloc;
9241 }
9242 }
9243
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009244 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009245 * Special case software events and allow them to be part of
9246 * any hardware group.
9247 */
9248 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009249
Peter Zijlstra34f43922015-02-20 14:05:38 +01009250 if (attr.use_clockid) {
9251 err = perf_event_set_clock(event, attr.clockid);
9252 if (err)
9253 goto err_alloc;
9254 }
9255
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009256 if (group_leader &&
9257 (is_software_event(event) != is_software_event(group_leader))) {
9258 if (is_software_event(event)) {
9259 /*
9260 * If event and group_leader are not both a software
9261 * event, and event is, then group leader is not.
9262 *
9263 * Allow the addition of software events to !software
9264 * groups, this is safe because software events never
9265 * fail to schedule.
9266 */
9267 pmu = group_leader->pmu;
9268 } else if (is_software_event(group_leader) &&
9269 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
9270 /*
9271 * In case the group is a pure software group, and we
9272 * try to add a hardware event, move the whole group to
9273 * the hardware context.
9274 */
9275 move_group = 1;
9276 }
9277 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009278
9279 /*
9280 * Get the target context (task or percpu):
9281 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009282 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009283 if (IS_ERR(ctx)) {
9284 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009285 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009286 }
9287
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009288 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
9289 err = -EBUSY;
9290 goto err_context;
9291 }
9292
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009293 /*
9294 * Look up the group leader (we will attach this event to it):
9295 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009296 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009297 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009298
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009299 /*
9300 * Do not allow a recursive hierarchy (this new sibling
9301 * becoming part of another group-sibling):
9302 */
9303 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009304 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01009305
9306 /* All events in a group should have the same clock */
9307 if (group_leader->clock != event->clock)
9308 goto err_context;
9309
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009310 /*
9311 * Do not allow to attach to a group in a different
9312 * task or CPU context:
9313 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009314 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01009315 /*
9316 * Make sure we're both on the same task, or both
9317 * per-cpu events.
9318 */
9319 if (group_leader->ctx->task != ctx->task)
9320 goto err_context;
9321
9322 /*
9323 * Make sure we're both events for the same CPU;
9324 * grouping events for different CPUs is broken; since
9325 * you can never concurrently schedule them anyhow.
9326 */
9327 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009328 goto err_context;
9329 } else {
9330 if (group_leader->ctx != ctx)
9331 goto err_context;
9332 }
9333
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009334 /*
9335 * Only a group leader can be exclusive or pinned
9336 */
9337 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009338 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009339 }
9340
9341 if (output_event) {
9342 err = perf_event_set_output(event, output_event);
9343 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009344 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009345 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009346
Yann Droneauda21b0b32014-01-05 21:36:33 +01009347 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
9348 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009349 if (IS_ERR(event_file)) {
9350 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +02009351 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009352 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04009353 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009354
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009355 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009356 gctx = group_leader->ctx;
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009357 mutex_lock_double(&gctx->mutex, &ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009358 if (gctx->task == TASK_TOMBSTONE) {
9359 err = -ESRCH;
9360 goto err_locked;
9361 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009362 } else {
9363 mutex_lock(&ctx->mutex);
9364 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009365
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009366 if (ctx->task == TASK_TOMBSTONE) {
9367 err = -ESRCH;
9368 goto err_locked;
9369 }
9370
Peter Zijlstraa7239682015-09-09 19:06:33 +02009371 if (!perf_event_validate_size(event)) {
9372 err = -E2BIG;
9373 goto err_locked;
9374 }
9375
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009376 /*
9377 * Must be under the same ctx::mutex as perf_install_in_context(),
9378 * because we need to serialize with concurrent event creation.
9379 */
9380 if (!exclusive_event_installable(event, ctx)) {
9381 /* exclusive and group stuff are assumed mutually exclusive */
9382 WARN_ON_ONCE(move_group);
9383
9384 err = -EBUSY;
9385 goto err_locked;
9386 }
9387
9388 WARN_ON_ONCE(ctx->parent_ctx);
9389
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009390 /*
9391 * This is the point on no return; we cannot fail hereafter. This is
9392 * where we start modifying current state.
9393 */
9394
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009395 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009396 /*
9397 * See perf_event_ctx_lock() for comments on the details
9398 * of swizzling perf_event::ctx.
9399 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009400 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01009401
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009402 list_for_each_entry(sibling, &group_leader->sibling_list,
9403 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009404 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009405 put_ctx(gctx);
9406 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009407
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009408 /*
9409 * Wait for everybody to stop referencing the events through
9410 * the old lists, before installing it on new lists.
9411 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009412 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009413
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009414 /*
9415 * Install the group siblings before the group leader.
9416 *
9417 * Because a group leader will try and install the entire group
9418 * (through the sibling list, which is still in-tact), we can
9419 * end up with siblings installed in the wrong context.
9420 *
9421 * By installing siblings first we NO-OP because they're not
9422 * reachable through the group lists.
9423 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009424 list_for_each_entry(sibling, &group_leader->sibling_list,
9425 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009426 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01009427 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009428 get_ctx(ctx);
9429 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009430
9431 /*
9432 * Removing from the context ends up with disabled
9433 * event. What we want here is event in the initial
9434 * startup state, ready to be add into new context.
9435 */
9436 perf_event__state_init(group_leader);
9437 perf_install_in_context(ctx, group_leader, group_leader->cpu);
9438 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009439
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009440 /*
9441 * Now that all events are installed in @ctx, nothing
9442 * references @gctx anymore, so drop the last reference we have
9443 * on it.
9444 */
9445 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009446 }
9447
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02009448 /*
9449 * Precalculate sample_data sizes; do while holding ctx::mutex such
9450 * that we're serialized against further additions and before
9451 * perf_install_in_context() which is the point the event is active and
9452 * can use these values.
9453 */
9454 perf_event__header_size(event);
9455 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009456
Peter Zijlstra78cd2c72016-01-25 14:08:45 +01009457 event->owner = current;
9458
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08009459 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009460 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009461
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009462 if (move_group)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009463 mutex_unlock(&gctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009464 mutex_unlock(&ctx->mutex);
9465
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009466 if (task) {
9467 mutex_unlock(&task->signal->cred_guard_mutex);
9468 put_task_struct(task);
9469 }
9470
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009471 put_online_cpus();
9472
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009473 mutex_lock(&current->perf_event_mutex);
9474 list_add_tail(&event->owner_entry, &current->perf_event_list);
9475 mutex_unlock(&current->perf_event_mutex);
9476
Peter Zijlstra8a495422010-05-27 15:47:49 +02009477 /*
9478 * Drop the reference on the group_event after placing the
9479 * new event on the sibling_list. This ensures destruction
9480 * of the group leader will find the pointer to itself in
9481 * perf_group_detach().
9482 */
Al Viro2903ff02012-08-28 12:52:22 -04009483 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04009484 fd_install(event_fd, event_file);
9485 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009486
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009487err_locked:
9488 if (move_group)
9489 mutex_unlock(&gctx->mutex);
9490 mutex_unlock(&ctx->mutex);
9491/* err_file: */
9492 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009493err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009494 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04009495 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009496err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +01009497 /*
9498 * If event_file is set, the fput() above will have called ->release()
9499 * and that will take care of freeing the event.
9500 */
9501 if (!event_file)
9502 free_event(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009503err_cred:
9504 if (task)
9505 mutex_unlock(&task->signal->cred_guard_mutex);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009506err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009507 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009508err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02009509 if (task)
9510 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009511err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04009512 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04009513err_fd:
9514 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009515 return err;
9516}
9517
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009518/**
9519 * perf_event_create_kernel_counter
9520 *
9521 * @attr: attributes of the counter to create
9522 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07009523 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009524 */
9525struct perf_event *
9526perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07009527 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03009528 perf_overflow_handler_t overflow_handler,
9529 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009530{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009531 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009532 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009533 int err;
9534
9535 /*
9536 * Get the target context (task or percpu):
9537 */
9538
Avi Kivity4dc0da82011-06-29 18:42:35 +03009539 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009540 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009541 if (IS_ERR(event)) {
9542 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009543 goto err;
9544 }
9545
Jiri Olsaf8697762014-08-01 14:33:01 +02009546 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009547 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +02009548
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009549 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009550 if (IS_ERR(ctx)) {
9551 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009552 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009553 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009554
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009555 WARN_ON_ONCE(ctx->parent_ctx);
9556 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009557 if (ctx->task == TASK_TOMBSTONE) {
9558 err = -ESRCH;
9559 goto err_unlock;
9560 }
9561
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009562 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009563 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009564 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009565 }
9566
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009567 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009568 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009569 mutex_unlock(&ctx->mutex);
9570
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009571 return event;
9572
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009573err_unlock:
9574 mutex_unlock(&ctx->mutex);
9575 perf_unpin_context(ctx);
9576 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009577err_free:
9578 free_event(event);
9579err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009580 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009581}
9582EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
9583
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009584void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
9585{
9586 struct perf_event_context *src_ctx;
9587 struct perf_event_context *dst_ctx;
9588 struct perf_event *event, *tmp;
9589 LIST_HEAD(events);
9590
9591 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
9592 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
9593
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009594 /*
9595 * See perf_event_ctx_lock() for comments on the details
9596 * of swizzling perf_event::ctx.
9597 */
9598 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009599 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
9600 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009601 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02009602 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009603 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02009604 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009605 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009606
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009607 /*
9608 * Wait for the events to quiesce before re-instating them.
9609 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009610 synchronize_rcu();
9611
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009612 /*
9613 * Re-instate events in 2 passes.
9614 *
9615 * Skip over group leaders and only install siblings on this first
9616 * pass, siblings will not get enabled without a leader, however a
9617 * leader will enable its siblings, even if those are still on the old
9618 * context.
9619 */
9620 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
9621 if (event->group_leader == event)
9622 continue;
9623
9624 list_del(&event->migrate_entry);
9625 if (event->state >= PERF_EVENT_STATE_OFF)
9626 event->state = PERF_EVENT_STATE_INACTIVE;
9627 account_event_cpu(event, dst_cpu);
9628 perf_install_in_context(dst_ctx, event, dst_cpu);
9629 get_ctx(dst_ctx);
9630 }
9631
9632 /*
9633 * Once all the siblings are setup properly, install the group leaders
9634 * to make it go.
9635 */
Peter Zijlstra98861672013-10-03 16:02:23 +02009636 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
9637 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009638 if (event->state >= PERF_EVENT_STATE_OFF)
9639 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02009640 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009641 perf_install_in_context(dst_ctx, event, dst_cpu);
9642 get_ctx(dst_ctx);
9643 }
9644 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009645 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009646}
9647EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
9648
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009649static void sync_child_event(struct perf_event *child_event,
9650 struct task_struct *child)
9651{
9652 struct perf_event *parent_event = child_event->parent;
9653 u64 child_val;
9654
9655 if (child_event->attr.inherit_stat)
9656 perf_event_read_event(child_event, child);
9657
Peter Zijlstrab5e58792010-05-21 14:43:12 +02009658 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009659
9660 /*
9661 * Add back the child's count to the parent's count:
9662 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02009663 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009664 atomic64_add(child_event->total_time_enabled,
9665 &parent_event->child_total_time_enabled);
9666 atomic64_add(child_event->total_time_running,
9667 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009668}
9669
9670static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009671perf_event_exit_event(struct perf_event *child_event,
9672 struct perf_event_context *child_ctx,
9673 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009674{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009675 struct perf_event *parent_event = child_event->parent;
9676
Peter Zijlstra1903d502014-07-15 17:27:27 +02009677 /*
9678 * Do not destroy the 'original' grouping; because of the context
9679 * switch optimization the original events could've ended up in a
9680 * random child task.
9681 *
9682 * If we were to destroy the original group, all group related
9683 * operations would cease to function properly after this random
9684 * child dies.
9685 *
9686 * Do destroy all inherited groups, we don't care about those
9687 * and being thorough is better.
9688 */
Peter Zijlstra32132a32016-01-11 15:40:59 +01009689 raw_spin_lock_irq(&child_ctx->lock);
9690 WARN_ON_ONCE(child_ctx->is_active);
9691
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009692 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +01009693 perf_group_detach(child_event);
9694 list_del_event(child_event, child_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01009695 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +01009696 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009697
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009698 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009699 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009700 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009701 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -04009702 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009703 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009704 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009705 /*
9706 * Child events can be cleaned up.
9707 */
9708
9709 sync_child_event(child_event, child);
9710
9711 /*
9712 * Remove this event from the parent's list
9713 */
9714 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
9715 mutex_lock(&parent_event->child_mutex);
9716 list_del_init(&child_event->child_list);
9717 mutex_unlock(&parent_event->child_mutex);
9718
9719 /*
9720 * Kick perf_poll() for is_event_hup().
9721 */
9722 perf_event_wakeup(parent_event);
9723 free_event(child_event);
9724 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009725}
9726
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009727static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009728{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02009729 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009730 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009731
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009732 WARN_ON_ONCE(child != current);
9733
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009734 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009735 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009736 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009737
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009738 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009739 * In order to reduce the amount of tricky in ctx tear-down, we hold
9740 * ctx::mutex over the entire thing. This serializes against almost
9741 * everything that wants to access the ctx.
9742 *
9743 * The exception is sys_perf_event_open() /
9744 * perf_event_create_kernel_count() which does find_get_context()
9745 * without ctx::mutex (it cannot because of the move_group double mutex
9746 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009747 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009748 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009749
9750 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009751 * In a single ctx::lock section, de-schedule the events and detach the
9752 * context from the task such that we cannot ever get it scheduled back
9753 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009754 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009755 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009756 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02009757
9758 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009759 * Now that the context is inactive, destroy the task <-> ctx relation
9760 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009761 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009762 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
9763 put_ctx(child_ctx); /* cannot be last */
9764 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
9765 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009766
Peter Zijlstra211de6e2014-09-30 19:23:08 +02009767 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009768 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009769
Peter Zijlstra211de6e2014-09-30 19:23:08 +02009770 if (clone_ctx)
9771 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02009772
9773 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009774 * Report the task dead after unscheduling the events so that we
9775 * won't get any samples after PERF_RECORD_EXIT. We can however still
9776 * get a few PERF_RECORD_READ events.
9777 */
9778 perf_event_task(child, child_ctx, 0);
9779
Peter Zijlstraebf905f2014-05-29 19:00:24 +02009780 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009781 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009782
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009783 mutex_unlock(&child_ctx->mutex);
9784
9785 put_ctx(child_ctx);
9786}
9787
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009788/*
9789 * When a child task exits, feed back event values to parent events.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009790 *
9791 * Can be called with cred_guard_mutex held when called from
9792 * install_exec_creds().
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009793 */
9794void perf_event_exit_task(struct task_struct *child)
9795{
Peter Zijlstra88821352010-11-09 19:01:43 +01009796 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009797 int ctxn;
9798
Peter Zijlstra88821352010-11-09 19:01:43 +01009799 mutex_lock(&child->perf_event_mutex);
9800 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
9801 owner_entry) {
9802 list_del_init(&event->owner_entry);
9803
9804 /*
9805 * Ensure the list deletion is visible before we clear
9806 * the owner, closes a race against perf_release() where
9807 * we need to serialize on the owner->perf_event_mutex.
9808 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01009809 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +01009810 }
9811 mutex_unlock(&child->perf_event_mutex);
9812
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009813 for_each_task_context_nr(ctxn)
9814 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +01009815
9816 /*
9817 * The perf_event_exit_task_context calls perf_event_task
9818 * with child's task_ctx, which generates EXIT events for
9819 * child contexts and sets child->perf_event_ctxp[] to NULL.
9820 * At this point we need to send EXIT events to cpu contexts.
9821 */
9822 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009823}
9824
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009825static void perf_free_event(struct perf_event *event,
9826 struct perf_event_context *ctx)
9827{
9828 struct perf_event *parent = event->parent;
9829
9830 if (WARN_ON_ONCE(!parent))
9831 return;
9832
9833 mutex_lock(&parent->child_mutex);
9834 list_del_init(&event->child_list);
9835 mutex_unlock(&parent->child_mutex);
9836
Al Viroa6fa9412012-08-20 14:59:25 +01009837 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009838
Peter Zijlstra652884f2015-01-23 11:20:10 +01009839 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02009840 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009841 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +01009842 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009843 free_event(event);
9844}
9845
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009846/*
Peter Zijlstra652884f2015-01-23 11:20:10 +01009847 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009848 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +01009849 *
9850 * Not all locks are strictly required, but take them anyway to be nice and
9851 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009852 */
9853void perf_event_free_task(struct task_struct *task)
9854{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009855 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009856 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009857 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009858
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009859 for_each_task_context_nr(ctxn) {
9860 ctx = task->perf_event_ctxp[ctxn];
9861 if (!ctx)
9862 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009863
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009864 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009865again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009866 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
9867 group_entry)
9868 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009869
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009870 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
9871 group_entry)
9872 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009873
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009874 if (!list_empty(&ctx->pinned_groups) ||
9875 !list_empty(&ctx->flexible_groups))
9876 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009877
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009878 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009879
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009880 put_ctx(ctx);
9881 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009882}
9883
Peter Zijlstra4e231c72010-09-09 21:01:59 +02009884void perf_event_delayed_put(struct task_struct *task)
9885{
9886 int ctxn;
9887
9888 for_each_task_context_nr(ctxn)
9889 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
9890}
9891
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009892struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +00009893{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009894 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +00009895
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009896 file = fget_raw(fd);
9897 if (!file)
9898 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +00009899
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009900 if (file->f_op != &perf_fops) {
9901 fput(file);
9902 return ERR_PTR(-EBADF);
9903 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00009904
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08009905 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +00009906}
9907
9908const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
9909{
9910 if (!event)
9911 return ERR_PTR(-EINVAL);
9912
9913 return &event->attr;
9914}
9915
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009916/*
9917 * inherit a event from parent task to child task:
9918 */
9919static struct perf_event *
9920inherit_event(struct perf_event *parent_event,
9921 struct task_struct *parent,
9922 struct perf_event_context *parent_ctx,
9923 struct task_struct *child,
9924 struct perf_event *group_leader,
9925 struct perf_event_context *child_ctx)
9926{
Jiri Olsa1929def2014-09-12 13:18:27 +02009927 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009928 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02009929 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009930
9931 /*
9932 * Instead of creating recursive hierarchies of events,
9933 * we link inherited events back to the original parent,
9934 * which has a filp for sure, which we use as the reference
9935 * count:
9936 */
9937 if (parent_event->parent)
9938 parent_event = parent_event->parent;
9939
9940 child_event = perf_event_alloc(&parent_event->attr,
9941 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009942 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009943 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +00009944 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009945 if (IS_ERR(child_event))
9946 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01009947
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02009948 /*
9949 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
9950 * must be under the same lock in order to serialize against
9951 * perf_event_release_kernel(), such that either we must observe
9952 * is_orphaned_event() or they will observe us on the child_list.
9953 */
9954 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02009955 if (is_orphaned_event(parent_event) ||
9956 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02009957 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +01009958 free_event(child_event);
9959 return NULL;
9960 }
9961
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009962 get_ctx(child_ctx);
9963
9964 /*
9965 * Make the child state follow the state of the parent event,
9966 * not its attr.disabled bit. We hold the parent's mutex,
9967 * so we won't race with perf_event_{en, dis}able_family.
9968 */
Jiri Olsa1929def2014-09-12 13:18:27 +02009969 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009970 child_event->state = PERF_EVENT_STATE_INACTIVE;
9971 else
9972 child_event->state = PERF_EVENT_STATE_OFF;
9973
9974 if (parent_event->attr.freq) {
9975 u64 sample_period = parent_event->hw.sample_period;
9976 struct hw_perf_event *hwc = &child_event->hw;
9977
9978 hwc->sample_period = sample_period;
9979 hwc->last_period = sample_period;
9980
9981 local64_set(&hwc->period_left, sample_period);
9982 }
9983
9984 child_event->ctx = child_ctx;
9985 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03009986 child_event->overflow_handler_context
9987 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009988
9989 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02009990 * Precalculate sample_data sizes
9991 */
9992 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02009993 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02009994
9995 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009996 * Link it up in the child's context:
9997 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02009998 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02009999 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +020010000 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010001
10002 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010003 * Link this into the parent event's child list
10004 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010005 list_add_tail(&child_event->child_list, &parent_event->child_list);
10006 mutex_unlock(&parent_event->child_mutex);
10007
10008 return child_event;
10009}
10010
10011static int inherit_group(struct perf_event *parent_event,
10012 struct task_struct *parent,
10013 struct perf_event_context *parent_ctx,
10014 struct task_struct *child,
10015 struct perf_event_context *child_ctx)
10016{
10017 struct perf_event *leader;
10018 struct perf_event *sub;
10019 struct perf_event *child_ctr;
10020
10021 leader = inherit_event(parent_event, parent, parent_ctx,
10022 child, NULL, child_ctx);
10023 if (IS_ERR(leader))
10024 return PTR_ERR(leader);
10025 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
10026 child_ctr = inherit_event(sub, parent, parent_ctx,
10027 child, leader, child_ctx);
10028 if (IS_ERR(child_ctr))
10029 return PTR_ERR(child_ctr);
10030 }
10031 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010032}
10033
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010034static int
10035inherit_task_group(struct perf_event *event, struct task_struct *parent,
10036 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010037 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010038 int *inherited_all)
10039{
10040 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010041 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010042
10043 if (!event->attr.inherit) {
10044 *inherited_all = 0;
10045 return 0;
10046 }
10047
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010048 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010049 if (!child_ctx) {
10050 /*
10051 * This is executed from the parent task context, so
10052 * inherit events that have been marked for cloning.
10053 * First allocate and initialize a context for the
10054 * child.
10055 */
10056
Jiri Olsa734df5a2013-07-09 17:44:10 +020010057 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010058 if (!child_ctx)
10059 return -ENOMEM;
10060
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010061 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010062 }
10063
10064 ret = inherit_group(event, parent, parent_ctx,
10065 child, child_ctx);
10066
10067 if (ret)
10068 *inherited_all = 0;
10069
10070 return ret;
10071}
10072
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010073/*
10074 * Initialize the perf_event context in task_struct
10075 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +020010076static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010077{
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010078 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010079 struct perf_event_context *cloned_ctx;
10080 struct perf_event *event;
10081 struct task_struct *parent = current;
10082 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010083 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010084 int ret = 0;
10085
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010086 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010087 return 0;
10088
10089 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010090 * If the parent's context is a clone, pin it so it won't get
10091 * swapped under us.
10092 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010093 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +020010094 if (!parent_ctx)
10095 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010096
10097 /*
10098 * No need to check if parent_ctx != NULL here; since we saw
10099 * it non-NULL earlier, the only reason for it to become NULL
10100 * is if we exit, and since we're currently in the middle of
10101 * a fork we can't be exiting at the same time.
10102 */
10103
10104 /*
10105 * Lock the parent list. No need to lock the child - not PID
10106 * hashed yet and not running, so nobody can access it.
10107 */
10108 mutex_lock(&parent_ctx->mutex);
10109
10110 /*
10111 * We dont have to disable NMIs - we are only looking at
10112 * the list, not manipulating it:
10113 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010114 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010115 ret = inherit_task_group(event, parent, parent_ctx,
10116 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010117 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010118 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010119 }
10120
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010121 /*
10122 * We can't hold ctx->lock when iterating the ->flexible_group list due
10123 * to allocations, but we need to prevent rotation because
10124 * rotate_ctx() will change the list from interrupt context.
10125 */
10126 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10127 parent_ctx->rotate_disable = 1;
10128 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
10129
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010130 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010131 ret = inherit_task_group(event, parent, parent_ctx,
10132 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010133 if (ret)
10134 break;
10135 }
10136
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010137 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10138 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010139
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010140 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010141
Peter Zijlstra05cbaa22009-12-30 16:00:35 +010010142 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010143 /*
10144 * Mark the child context as a clone of the parent
10145 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010146 *
10147 * Note that if the parent is a clone, the holding of
10148 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010149 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010150 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010151 if (cloned_ctx) {
10152 child_ctx->parent_ctx = cloned_ctx;
10153 child_ctx->parent_gen = parent_ctx->parent_gen;
10154 } else {
10155 child_ctx->parent_ctx = parent_ctx;
10156 child_ctx->parent_gen = parent_ctx->generation;
10157 }
10158 get_ctx(child_ctx->parent_ctx);
10159 }
10160
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010161 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010162 mutex_unlock(&parent_ctx->mutex);
10163
10164 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010165 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010166
10167 return ret;
10168}
10169
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010170/*
10171 * Initialize the perf_event context in task_struct
10172 */
10173int perf_event_init_task(struct task_struct *child)
10174{
10175 int ctxn, ret;
10176
Oleg Nesterov8550d7c2011-01-19 19:22:28 +010010177 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
10178 mutex_init(&child->perf_event_mutex);
10179 INIT_LIST_HEAD(&child->perf_event_list);
10180
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010181 for_each_task_context_nr(ctxn) {
10182 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010183 if (ret) {
10184 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010185 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010186 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010187 }
10188
10189 return 0;
10190}
10191
Paul Mackerras220b1402010-03-10 20:45:52 +110010192static void __init perf_event_init_all_cpus(void)
10193{
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010194 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +110010195 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +110010196
10197 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010198 swhash = &per_cpu(swevent_htable, cpu);
10199 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +000010200 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +110010201 }
10202}
10203
Paul Gortmaker0db06282013-06-19 14:53:51 -040010204static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010205{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010206 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010207
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010208 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +000010209 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010210 struct swevent_hlist *hlist;
10211
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010212 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
10213 WARN_ON(!hlist);
10214 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010215 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010216 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010217}
10218
Dave Young2965faa2015-09-09 15:38:55 -070010219#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010220static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010221{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010222 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010223 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
10224 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010225
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010226 raw_spin_lock(&ctx->lock);
10227 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010228 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010229 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010230}
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010231
10232static void perf_event_exit_cpu_context(int cpu)
10233{
10234 struct perf_event_context *ctx;
10235 struct pmu *pmu;
10236 int idx;
10237
10238 idx = srcu_read_lock(&pmus_srcu);
10239 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +020010240 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010241
10242 mutex_lock(&ctx->mutex);
10243 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
10244 mutex_unlock(&ctx->mutex);
10245 }
10246 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010247}
10248
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010249static void perf_event_exit_cpu(int cpu)
10250{
Peter Zijlstrae3703f82014-02-24 12:06:12 +010010251 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010252}
10253#else
10254static inline void perf_event_exit_cpu(int cpu) { }
10255#endif
10256
Peter Zijlstrac2774432010-12-08 15:29:02 +010010257static int
10258perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
10259{
10260 int cpu;
10261
10262 for_each_online_cpu(cpu)
10263 perf_event_exit_cpu(cpu);
10264
10265 return NOTIFY_OK;
10266}
10267
10268/*
10269 * Run the perf reboot notifier at the very last possible moment so that
10270 * the generic watchdog code runs as long as possible.
10271 */
10272static struct notifier_block perf_reboot_notifier = {
10273 .notifier_call = perf_reboot,
10274 .priority = INT_MIN,
10275};
10276
Paul Gortmaker0db06282013-06-19 14:53:51 -040010277static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010278perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
10279{
10280 unsigned int cpu = (long)hcpu;
10281
Linus Torvalds4536e4d2011-11-03 07:44:04 -070010282 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010283
10284 case CPU_UP_PREPARE:
Peter Zijlstra1dcaac12016-03-08 17:56:05 +010010285 /*
10286 * This must be done before the CPU comes alive, because the
10287 * moment we can run tasks we can encounter (software) events.
10288 *
10289 * Specifically, someone can have inherited events on kthreadd
10290 * or a pre-existing worker thread that gets re-bound.
10291 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010292 perf_event_init_cpu(cpu);
10293 break;
10294
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010295 case CPU_DOWN_PREPARE:
Peter Zijlstra1dcaac12016-03-08 17:56:05 +010010296 /*
10297 * This must be done before the CPU dies because after that an
10298 * active event might want to IPI the CPU and that'll not work
10299 * so great for dead CPUs.
10300 *
10301 * XXX smp_call_function_single() return -ENXIO without a warn
10302 * so we could possibly deal with this.
10303 *
10304 * This is safe against new events arriving because
10305 * sys_perf_event_open() serializes against hotplug using
10306 * get_online_cpus().
10307 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010308 perf_event_exit_cpu(cpu);
10309 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010310 default:
10311 break;
10312 }
10313
10314 return NOTIFY_OK;
10315}
10316
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010317void __init perf_event_init(void)
10318{
Jason Wessel3c502e72010-11-04 17:33:01 -050010319 int ret;
10320
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010321 idr_init(&pmu_idr);
10322
Paul Mackerras220b1402010-03-10 20:45:52 +110010323 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010324 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010325 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
10326 perf_pmu_register(&perf_cpu_clock, NULL, -1);
10327 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010328 perf_tp_register();
10329 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +010010330 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -050010331
10332 ret = init_hw_breakpoint();
10333 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +020010334
Jiri Olsab01c3a02012-03-23 15:41:20 +010010335 /*
10336 * Build time assertion that we keep the data_head at the intended
10337 * location. IOW, validation we got the __reserved[] size right.
10338 */
10339 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
10340 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010341}
Peter Zijlstraabe43402010-11-17 23:17:37 +010010342
Cody P Schaferfd979c02015-01-30 13:45:57 -080010343ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
10344 char *page)
10345{
10346 struct perf_pmu_events_attr *pmu_attr =
10347 container_of(attr, struct perf_pmu_events_attr, attr);
10348
10349 if (pmu_attr->event_str)
10350 return sprintf(page, "%s\n", pmu_attr->event_str);
10351
10352 return 0;
10353}
Thomas Gleixner675965b2016-02-22 22:19:27 +000010354EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -080010355
Peter Zijlstraabe43402010-11-17 23:17:37 +010010356static int __init perf_event_sysfs_init(void)
10357{
10358 struct pmu *pmu;
10359 int ret;
10360
10361 mutex_lock(&pmus_lock);
10362
10363 ret = bus_register(&pmu_bus);
10364 if (ret)
10365 goto unlock;
10366
10367 list_for_each_entry(pmu, &pmus, entry) {
10368 if (!pmu->name || pmu->type < 0)
10369 continue;
10370
10371 ret = pmu_dev_alloc(pmu);
10372 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
10373 }
10374 pmu_bus_running = 1;
10375 ret = 0;
10376
10377unlock:
10378 mutex_unlock(&pmus_lock);
10379
10380 return ret;
10381}
10382device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010383
10384#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -040010385static struct cgroup_subsys_state *
10386perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010387{
10388 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +020010389
Li Zefan1b15d052011-03-03 14:26:06 +080010390 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010391 if (!jc)
10392 return ERR_PTR(-ENOMEM);
10393
Stephane Eraniane5d13672011-02-14 11:20:01 +020010394 jc->info = alloc_percpu(struct perf_cgroup_info);
10395 if (!jc->info) {
10396 kfree(jc);
10397 return ERR_PTR(-ENOMEM);
10398 }
10399
Stephane Eraniane5d13672011-02-14 11:20:01 +020010400 return &jc->css;
10401}
10402
Tejun Heoeb954192013-08-08 20:11:23 -040010403static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010404{
Tejun Heoeb954192013-08-08 20:11:23 -040010405 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
10406
Stephane Eraniane5d13672011-02-14 11:20:01 +020010407 free_percpu(jc->info);
10408 kfree(jc);
10409}
10410
10411static int __perf_cgroup_move(void *info)
10412{
10413 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010414 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010415 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010416 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010417 return 0;
10418}
10419
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010420static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010421{
Tejun Heobb9d97b2011-12-12 18:12:21 -080010422 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010423 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -080010424
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010425 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -080010426 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010427}
10428
Tejun Heo073219e2014-02-08 10:36:58 -050010429struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -080010430 .css_alloc = perf_cgroup_css_alloc,
10431 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -080010432 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +020010433};
10434#endif /* CONFIG_CGROUP_PERF */