blob: 1447c04c651d87a2625054e80e8345c970f6c2e8 [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
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100245static void event_function_call(struct perf_event *event, event_f func, void *data)
Peter Zijlstra00179602015-11-30 16:26:35 +0100246{
247 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100248 struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100249 struct event_function_struct efs = {
250 .event = event,
251 .func = func,
252 .data = data,
253 };
Peter Zijlstra00179602015-11-30 16:26:35 +0100254
Peter Zijlstrac97f4732016-01-14 10:51:03 +0100255 if (!event->parent) {
256 /*
257 * If this is a !child event, we must hold ctx::mutex to
258 * stabilize the the event->ctx relation. See
259 * perf_event_ctx_lock().
260 */
261 lockdep_assert_held(&ctx->mutex);
262 }
Peter Zijlstra00179602015-11-30 16:26:35 +0100263
264 if (!task) {
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100265 cpu_function_call(event->cpu, event_function, &efs);
Peter Zijlstra00179602015-11-30 16:26:35 +0100266 return;
267 }
268
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100269 if (task == TASK_TOMBSTONE)
270 return;
271
Peter Zijlstraa0963092016-02-24 18:45:50 +0100272again:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100273 if (!task_function_call(task, event_function, &efs))
Peter Zijlstra00179602015-11-30 16:26:35 +0100274 return;
275
276 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100277 /*
278 * Reload the task pointer, it might have been changed by
279 * a concurrent perf_event_context_sched_out().
280 */
281 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +0100282 if (task == TASK_TOMBSTONE) {
283 raw_spin_unlock_irq(&ctx->lock);
284 return;
Peter Zijlstra00179602015-11-30 16:26:35 +0100285 }
Peter Zijlstraa0963092016-02-24 18:45:50 +0100286 if (ctx->is_active) {
287 raw_spin_unlock_irq(&ctx->lock);
288 goto again;
289 }
290 func(event, NULL, ctx, data);
Peter Zijlstra00179602015-11-30 16:26:35 +0100291 raw_spin_unlock_irq(&ctx->lock);
292}
293
Peter Zijlstracca20942016-08-16 13:33:26 +0200294/*
295 * Similar to event_function_call() + event_function(), but hard assumes IRQs
296 * are already disabled and we're on the right CPU.
297 */
298static void event_function_local(struct perf_event *event, event_f func, void *data)
299{
300 struct perf_event_context *ctx = event->ctx;
301 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
302 struct task_struct *task = READ_ONCE(ctx->task);
303 struct perf_event_context *task_ctx = NULL;
304
305 WARN_ON_ONCE(!irqs_disabled());
306
307 if (task) {
308 if (task == TASK_TOMBSTONE)
309 return;
310
311 task_ctx = ctx;
312 }
313
314 perf_ctx_lock(cpuctx, task_ctx);
315
316 task = ctx->task;
317 if (task == TASK_TOMBSTONE)
318 goto unlock;
319
320 if (task) {
321 /*
322 * We must be either inactive or active and the right task,
323 * otherwise we're screwed, since we cannot IPI to somewhere
324 * else.
325 */
326 if (ctx->is_active) {
327 if (WARN_ON_ONCE(task != current))
328 goto unlock;
329
330 if (WARN_ON_ONCE(cpuctx->task_ctx != ctx))
331 goto unlock;
332 }
333 } else {
334 WARN_ON_ONCE(&cpuctx->ctx != ctx);
335 }
336
337 func(event, cpuctx, ctx, data);
338unlock:
339 perf_ctx_unlock(cpuctx, task_ctx);
340}
341
Stephane Eraniane5d13672011-02-14 11:20:01 +0200342#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
343 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100344 PERF_FLAG_PID_CGROUP |\
345 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200346
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100347/*
348 * branch priv levels that need permission checks
349 */
350#define PERF_SAMPLE_BRANCH_PERM_PLM \
351 (PERF_SAMPLE_BRANCH_KERNEL |\
352 PERF_SAMPLE_BRANCH_HV)
353
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200354enum event_type_t {
355 EVENT_FLEXIBLE = 0x1,
356 EVENT_PINNED = 0x2,
Peter Zijlstra3cbaa592016-02-24 18:45:47 +0100357 EVENT_TIME = 0x4,
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200358 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
359};
360
Stephane Eraniane5d13672011-02-14 11:20:01 +0200361/*
362 * perf_sched_events : >0 events exist
363 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
364 */
Peter Zijlstra9107c892016-02-24 18:45:45 +0100365
366static void perf_sched_delayed(struct work_struct *work);
367DEFINE_STATIC_KEY_FALSE(perf_sched_events);
368static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed);
369static DEFINE_MUTEX(perf_sched_mutex);
370static atomic_t perf_sched_count;
371
Stephane Eraniane5d13672011-02-14 11:20:01 +0200372static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Yan, Zhengba532502014-11-04 21:55:58 -0500373static DEFINE_PER_CPU(int, perf_sched_cb_usages);
Kan Liangf2fb6be2016-03-23 11:24:37 -0700374static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200375
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200376static atomic_t nr_mmap_events __read_mostly;
377static atomic_t nr_comm_events __read_mostly;
378static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200379static atomic_t nr_freq_events __read_mostly;
Adrian Hunter45ac1402015-07-21 12:44:02 +0300380static atomic_t nr_switch_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200381
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200382static LIST_HEAD(pmus);
383static DEFINE_MUTEX(pmus_lock);
384static struct srcu_struct pmus_srcu;
385
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200386/*
387 * perf event paranoia level:
388 * -1 - not paranoid at all
389 * 0 - disallow raw tracepoint access for unpriv
390 * 1 - disallow cpu events for unpriv
391 * 2 - disallow kernel profiling for unpriv
Jeff Vander Stoepd28f8562016-05-29 14:22:32 -0700392 * 3 - disallow all unpriv perf event use
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200393 */
Jeff Vander Stoepd28f8562016-05-29 14:22:32 -0700394#ifdef CONFIG_SECURITY_PERF_EVENTS_RESTRICT
395int sysctl_perf_event_paranoid __read_mostly = 3;
396#else
Andy Lutomirski01610282016-05-09 15:48:51 -0700397int sysctl_perf_event_paranoid __read_mostly = 2;
Jeff Vander Stoepd28f8562016-05-29 14:22:32 -0700398#endif
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200399
Frederic Weisbecker20443382011-03-31 03:33:29 +0200400/* Minimum for 512 kiB + 1 user control page */
401int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200402
403/*
404 * max perf event sample rate
405 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700406#define DEFAULT_MAX_SAMPLE_RATE 100000
407#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
408#define DEFAULT_CPU_TIME_MAX_PERCENT 25
409
410int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
411
412static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
413static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
414
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200415static int perf_sample_allowed_ns __read_mostly =
416 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700417
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800418static void update_perf_cpu_limits(void)
Dave Hansen14c63f12013-06-21 08:51:36 -0700419{
420 u64 tmp = perf_sample_period_ns;
421
422 tmp *= sysctl_perf_cpu_time_max_percent;
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100423 tmp = div_u64(tmp, 100);
424 if (!tmp)
425 tmp = 1;
426
427 WRITE_ONCE(perf_sample_allowed_ns, tmp);
Dave Hansen14c63f12013-06-21 08:51:36 -0700428}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100429
Stephane Eranian9e630202013-04-03 14:21:33 +0200430static int perf_rotate_context(struct perf_cpu_context *cpuctx);
431
Peter Zijlstra163ec432011-02-16 11:22:34 +0100432int perf_proc_update_handler(struct ctl_table *table, int write,
433 void __user *buffer, size_t *lenp,
434 loff_t *ppos)
435{
Stephane Eranian3d3916f2019-01-10 17:17:16 -0800436 int ret;
437 int perf_cpu = sysctl_perf_cpu_time_max_percent;
Kan Liangab7fdef2016-05-03 00:26:06 -0700438 /*
439 * If throttling is disabled don't allow the write:
440 */
Stephane Eranian3d3916f2019-01-10 17:17:16 -0800441 if (write && (perf_cpu == 100 || perf_cpu == 0))
Kan Liangab7fdef2016-05-03 00:26:06 -0700442 return -EINVAL;
443
Stephane Eranian3d3916f2019-01-10 17:17:16 -0800444 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
445 if (ret || !write)
446 return ret;
447
Peter Zijlstra163ec432011-02-16 11:22:34 +0100448 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700449 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
450 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100451
452 return 0;
453}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200454
Dave Hansen14c63f12013-06-21 08:51:36 -0700455int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
456
457int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
458 void __user *buffer, size_t *lenp,
459 loff_t *ppos)
460{
Tan Xiaojun0f8a75e2017-02-23 14:04:39 +0800461 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Dave Hansen14c63f12013-06-21 08:51:36 -0700462
463 if (ret || !write)
464 return ret;
465
Peter Zijlstrab303e7c2016-04-04 09:57:40 +0200466 if (sysctl_perf_cpu_time_max_percent == 100 ||
467 sysctl_perf_cpu_time_max_percent == 0) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100468 printk(KERN_WARNING
469 "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
470 WRITE_ONCE(perf_sample_allowed_ns, 0);
471 } else {
472 update_perf_cpu_limits();
473 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700474
475 return 0;
476}
477
478/*
479 * perf samples are done in some very critical code paths (NMIs).
480 * If they take too much CPU time, the system can lock up and not
481 * get any real work done. This will drop the sample rate when
482 * we detect that events are taking too long.
483 */
484#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200485static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700486
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100487static u64 __report_avg;
488static u64 __report_allowed;
489
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100490static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700491{
David Ahern0d87d7e2016-08-01 13:49:29 -0700492 printk_ratelimited(KERN_INFO
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100493 "perf: interrupt took too long (%lld > %lld), lowering "
494 "kernel.perf_event_max_sample_rate to %d\n",
495 __report_avg, __report_allowed,
496 sysctl_perf_event_sample_rate);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100497}
498
499static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
500
501void perf_sample_event_took(u64 sample_len_ns)
502{
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100503 u64 max_len = READ_ONCE(perf_sample_allowed_ns);
504 u64 running_len;
505 u64 avg_len;
506 u32 max;
Dave Hansen14c63f12013-06-21 08:51:36 -0700507
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100508 if (max_len == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700509 return;
510
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100511 /* Decay the counter by 1 average sample. */
512 running_len = __this_cpu_read(running_sample_length);
513 running_len -= running_len/NR_ACCUMULATED_SAMPLES;
514 running_len += sample_len_ns;
515 __this_cpu_write(running_sample_length, running_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700516
517 /*
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100518 * Note: this will be biased artifically low until we have
519 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
Dave Hansen14c63f12013-06-21 08:51:36 -0700520 * from having to maintain a count.
521 */
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100522 avg_len = running_len/NR_ACCUMULATED_SAMPLES;
523 if (avg_len <= max_len)
Dave Hansen14c63f12013-06-21 08:51:36 -0700524 return;
525
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100526 __report_avg = avg_len;
527 __report_allowed = max_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700528
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100529 /*
530 * Compute a throttle threshold 25% below the current duration.
531 */
532 avg_len += avg_len / 4;
533 max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
534 if (avg_len < max)
535 max /= (u32)avg_len;
536 else
537 max = 1;
538
539 WRITE_ONCE(perf_sample_allowed_ns, avg_len);
540 WRITE_ONCE(max_samples_per_tick, max);
541
542 sysctl_perf_event_sample_rate = max * HZ;
Dave Hansen14c63f12013-06-21 08:51:36 -0700543 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
544
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100545 if (!irq_work_queue(&perf_duration_work)) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100546 early_printk("perf: interrupt took too long (%lld > %lld), lowering "
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100547 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100548 __report_avg, __report_allowed,
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100549 sysctl_perf_event_sample_rate);
550 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700551}
552
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200553static atomic64_t perf_event_id;
554
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200555static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
556 enum event_type_t event_type);
557
558static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200559 enum event_type_t event_type,
560 struct task_struct *task);
561
562static void update_context_time(struct perf_event_context *ctx);
563static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200564
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200565void __weak perf_event_print_debug(void) { }
566
Matt Fleming84c79912010-10-03 21:41:13 +0100567extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200568{
Matt Fleming84c79912010-10-03 21:41:13 +0100569 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200570}
571
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200572static inline u64 perf_clock(void)
573{
574 return local_clock();
575}
576
Peter Zijlstra34f43922015-02-20 14:05:38 +0100577static inline u64 perf_event_clock(struct perf_event *event)
578{
579 return event->clock();
580}
581
Stephane Eraniane5d13672011-02-14 11:20:01 +0200582#ifdef CONFIG_CGROUP_PERF
583
Stephane Eraniane5d13672011-02-14 11:20:01 +0200584static inline bool
585perf_cgroup_match(struct perf_event *event)
586{
587 struct perf_event_context *ctx = event->ctx;
588 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
589
Tejun Heoef824fa2013-04-08 19:00:38 -0700590 /* @event doesn't care about cgroup */
591 if (!event->cgrp)
592 return true;
593
594 /* wants specific cgroup scope but @cpuctx isn't associated with any */
595 if (!cpuctx->cgrp)
596 return false;
597
598 /*
599 * Cgroup scoping is recursive. An event enabled for a cgroup is
600 * also enabled for all its descendant cgroups. If @cpuctx's
601 * cgroup is a descendant of @event's (the test covers identity
602 * case), it's a match.
603 */
604 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
605 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200606}
607
Stephane Eraniane5d13672011-02-14 11:20:01 +0200608static inline void perf_detach_cgroup(struct perf_event *event)
609{
Zefan Li4e2ba652014-09-19 16:53:14 +0800610 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200611 event->cgrp = NULL;
612}
613
614static inline int is_cgroup_event(struct perf_event *event)
615{
616 return event->cgrp != NULL;
617}
618
619static inline u64 perf_cgroup_event_time(struct perf_event *event)
620{
621 struct perf_cgroup_info *t;
622
623 t = per_cpu_ptr(event->cgrp->info, event->cpu);
624 return t->time;
625}
626
627static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
628{
629 struct perf_cgroup_info *info;
630 u64 now;
631
632 now = perf_clock();
633
634 info = this_cpu_ptr(cgrp->info);
635
636 info->time += now - info->timestamp;
637 info->timestamp = now;
638}
639
640static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
641{
Song Liud4271d82018-03-12 09:59:43 -0700642 struct perf_cgroup *cgrp = cpuctx->cgrp;
643 struct cgroup_subsys_state *css;
644
645 if (cgrp) {
646 for (css = &cgrp->css; css; css = css->parent) {
647 cgrp = container_of(css, struct perf_cgroup, css);
648 __update_cgrp_time(cgrp);
649 }
650 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200651}
652
653static inline void update_cgrp_time_from_event(struct perf_event *event)
654{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200655 struct perf_cgroup *cgrp;
656
Stephane Eraniane5d13672011-02-14 11:20:01 +0200657 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200658 * ensure we access cgroup data only when needed and
659 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200660 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200661 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200662 return;
663
Stephane Eranian614e4c42015-11-12 11:00:04 +0100664 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200665 /*
666 * Do not update time when cgroup is not active
667 */
668 if (cgrp == event->cgrp)
669 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200670}
671
672static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200673perf_cgroup_set_timestamp(struct task_struct *task,
674 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200675{
676 struct perf_cgroup *cgrp;
677 struct perf_cgroup_info *info;
Song Liud4271d82018-03-12 09:59:43 -0700678 struct cgroup_subsys_state *css;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200679
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200680 /*
681 * ctx->lock held by caller
682 * ensure we do not access cgroup data
683 * unless we have the cgroup pinned (css_get)
684 */
685 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200686 return;
687
Stephane Eranian614e4c42015-11-12 11:00:04 +0100688 cgrp = perf_cgroup_from_task(task, ctx);
Song Liud4271d82018-03-12 09:59:43 -0700689
690 for (css = &cgrp->css; css; css = css->parent) {
691 cgrp = container_of(css, struct perf_cgroup, css);
692 info = this_cpu_ptr(cgrp->info);
693 info->timestamp = ctx->timestamp;
694 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200695}
696
697#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
698#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
699
700/*
701 * reschedule events based on the cgroup constraint of task.
702 *
703 * mode SWOUT : schedule out everything
704 * mode SWIN : schedule in based on cgroup for next
705 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800706static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200707{
708 struct perf_cpu_context *cpuctx;
709 struct pmu *pmu;
710 unsigned long flags;
711
712 /*
713 * disable interrupts to avoid geting nr_cgroup
714 * changes via __perf_event_disable(). Also
715 * avoids preemption.
716 */
717 local_irq_save(flags);
718
719 /*
720 * we reschedule only in the presence of cgroup
721 * constrained events.
722 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200723
724 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200725 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200726 if (cpuctx->unique_pmu != pmu)
727 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200728
Stephane Eraniane5d13672011-02-14 11:20:01 +0200729 /*
730 * perf_cgroup_events says at least one
731 * context on this CPU has cgroup events.
732 *
733 * ctx->nr_cgroups reports the number of cgroup
734 * events for a context.
735 */
736 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200737 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
738 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200739
740 if (mode & PERF_CGROUP_SWOUT) {
741 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
742 /*
743 * must not be done before ctxswout due
744 * to event_filter_match() in event_sched_out()
745 */
746 cpuctx->cgrp = NULL;
747 }
748
749 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200750 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200751 /*
752 * set cgrp before ctxsw in to allow
753 * event_filter_match() to not have to pass
754 * task around
Stephane Eranian614e4c42015-11-12 11:00:04 +0100755 * we pass the cpuctx->ctx to perf_cgroup_from_task()
756 * because cgorup events are only per-cpu
Stephane Eraniane5d13672011-02-14 11:20:01 +0200757 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100758 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200759 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
760 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200761 perf_pmu_enable(cpuctx->ctx.pmu);
762 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200763 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200764 }
765
Stephane Eraniane5d13672011-02-14 11:20:01 +0200766 local_irq_restore(flags);
767}
768
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200769static inline void perf_cgroup_sched_out(struct task_struct *task,
770 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200771{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200772 struct perf_cgroup *cgrp1;
773 struct perf_cgroup *cgrp2 = NULL;
774
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100775 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200776 /*
777 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100778 * we do not need to pass the ctx here because we know
779 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200780 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100781 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100782 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200783
784 /*
785 * only schedule out current cgroup events if we know
786 * that we are switching to a different cgroup. Otherwise,
787 * do no touch the cgroup events.
788 */
789 if (cgrp1 != cgrp2)
790 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100791
792 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200793}
794
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200795static inline void perf_cgroup_sched_in(struct task_struct *prev,
796 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200797{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200798 struct perf_cgroup *cgrp1;
799 struct perf_cgroup *cgrp2 = NULL;
800
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100801 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200802 /*
803 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100804 * we do not need to pass the ctx here because we know
805 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200806 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100807 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100808 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200809
810 /*
811 * only need to schedule in cgroup events if we are changing
812 * cgroup during ctxsw. Cgroup events were not scheduled
813 * out of ctxsw out if that was not the case.
814 */
815 if (cgrp1 != cgrp2)
816 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100817
818 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200819}
820
821static inline int perf_cgroup_connect(int fd, struct perf_event *event,
822 struct perf_event_attr *attr,
823 struct perf_event *group_leader)
824{
825 struct perf_cgroup *cgrp;
826 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400827 struct fd f = fdget(fd);
828 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200829
Al Viro2903ff02012-08-28 12:52:22 -0400830 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200831 return -EBADF;
832
Al Virob5830432014-10-31 01:22:04 -0400833 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400834 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800835 if (IS_ERR(css)) {
836 ret = PTR_ERR(css);
837 goto out;
838 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200839
840 cgrp = container_of(css, struct perf_cgroup, css);
841 event->cgrp = cgrp;
842
843 /*
844 * all events in a group must monitor
845 * the same cgroup because a task belongs
846 * to only one perf cgroup at a time
847 */
848 if (group_leader && group_leader->cgrp != cgrp) {
849 perf_detach_cgroup(event);
850 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200851 }
Li Zefan3db272c2011-03-03 14:25:37 +0800852out:
Al Viro2903ff02012-08-28 12:52:22 -0400853 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200854 return ret;
855}
856
857static inline void
858perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
859{
860 struct perf_cgroup_info *t;
861 t = per_cpu_ptr(event->cgrp->info, event->cpu);
862 event->shadow_ctx_time = now - t->timestamp;
863}
864
865static inline void
866perf_cgroup_defer_enabled(struct perf_event *event)
867{
868 /*
869 * when the current task's perf cgroup does not match
870 * the event's, we need to remember to call the
871 * perf_mark_enable() function the first time a task with
872 * a matching perf cgroup is scheduled in.
873 */
874 if (is_cgroup_event(event) && !perf_cgroup_match(event))
875 event->cgrp_defer_enabled = 1;
876}
877
878static inline void
879perf_cgroup_mark_enabled(struct perf_event *event,
880 struct perf_event_context *ctx)
881{
882 struct perf_event *sub;
883 u64 tstamp = perf_event_time(event);
884
885 if (!event->cgrp_defer_enabled)
886 return;
887
888 event->cgrp_defer_enabled = 0;
889
890 event->tstamp_enabled = tstamp - event->total_time_enabled;
891 list_for_each_entry(sub, &event->sibling_list, group_entry) {
892 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
893 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
894 sub->cgrp_defer_enabled = 0;
895 }
896 }
897}
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700898
899/*
900 * Update cpuctx->cgrp so that it is set when first cgroup event is added and
901 * cleared when last cgroup event is removed.
902 */
903static inline void
904list_update_cgroup_event(struct perf_event *event,
905 struct perf_event_context *ctx, bool add)
906{
907 struct perf_cpu_context *cpuctx;
908
909 if (!is_cgroup_event(event))
910 return;
911
912 if (add && ctx->nr_cgroups++)
913 return;
914 else if (!add && --ctx->nr_cgroups)
915 return;
916 /*
917 * Because cgroup events are always per-cpu events,
918 * this will always be called from the right CPU.
919 */
920 cpuctx = __get_cpu_context(ctx);
David Carrillo-Cisneros864c2352016-11-01 11:52:58 -0700921
David Carrillo-Cisneros8fc31ce2016-12-04 00:46:17 -0800922 /*
923 * cpuctx->cgrp is NULL until a cgroup event is sched in or
924 * ctx->nr_cgroup == 0 .
925 */
926 if (add && perf_cgroup_from_task(current, ctx) == event->cgrp)
927 cpuctx->cgrp = event->cgrp;
928 else if (!add)
929 cpuctx->cgrp = NULL;
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700930}
931
Stephane Eraniane5d13672011-02-14 11:20:01 +0200932#else /* !CONFIG_CGROUP_PERF */
933
934static inline bool
935perf_cgroup_match(struct perf_event *event)
936{
937 return true;
938}
939
940static inline void perf_detach_cgroup(struct perf_event *event)
941{}
942
943static inline int is_cgroup_event(struct perf_event *event)
944{
945 return 0;
946}
947
948static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
949{
950 return 0;
951}
952
953static inline void update_cgrp_time_from_event(struct perf_event *event)
954{
955}
956
957static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
958{
959}
960
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200961static inline void perf_cgroup_sched_out(struct task_struct *task,
962 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200963{
964}
965
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200966static inline void perf_cgroup_sched_in(struct task_struct *prev,
967 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200968{
969}
970
971static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
972 struct perf_event_attr *attr,
973 struct perf_event *group_leader)
974{
975 return -EINVAL;
976}
977
978static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200979perf_cgroup_set_timestamp(struct task_struct *task,
980 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200981{
982}
983
984void
985perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
986{
987}
988
989static inline void
990perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
991{
992}
993
994static inline u64 perf_cgroup_event_time(struct perf_event *event)
995{
996 return 0;
997}
998
999static inline void
1000perf_cgroup_defer_enabled(struct perf_event *event)
1001{
1002}
1003
1004static inline void
1005perf_cgroup_mark_enabled(struct perf_event *event,
1006 struct perf_event_context *ctx)
1007{
1008}
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001009
1010static inline void
1011list_update_cgroup_event(struct perf_event *event,
1012 struct perf_event_context *ctx, bool add)
1013{
1014}
1015
Stephane Eraniane5d13672011-02-14 11:20:01 +02001016#endif
1017
Stephane Eranian9e630202013-04-03 14:21:33 +02001018/*
1019 * set default to be dependent on timer tick just
1020 * like original code
1021 */
1022#define PERF_CPU_HRTIMER (1000 / HZ)
1023/*
1024 * function must be called with interrupts disbled
1025 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001026static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +02001027{
1028 struct perf_cpu_context *cpuctx;
Stephane Eranian9e630202013-04-03 14:21:33 +02001029 int rotations = 0;
1030
1031 WARN_ON(!irqs_disabled());
1032
1033 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +02001034 rotations = perf_rotate_context(cpuctx);
1035
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001036 raw_spin_lock(&cpuctx->hrtimer_lock);
1037 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +02001038 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001039 else
1040 cpuctx->hrtimer_active = 0;
1041 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +02001042
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001043 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +02001044}
1045
Peter Zijlstra272325c2015-04-15 11:41:58 +02001046static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +02001047{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001048 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001049 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +02001050 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +02001051
1052 /* no multiplexing needed for SW PMU */
1053 if (pmu->task_ctx_nr == perf_sw_context)
1054 return;
1055
Stephane Eranian62b85632013-04-03 14:21:34 +02001056 /*
1057 * check default is sane, if not set then force to
1058 * default interval (1/tick)
1059 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001060 interval = pmu->hrtimer_interval_ms;
1061 if (interval < 1)
1062 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +02001063
Peter Zijlstra272325c2015-04-15 11:41:58 +02001064 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +02001065
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001066 raw_spin_lock_init(&cpuctx->hrtimer_lock);
1067 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001068 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +02001069}
1070
Peter Zijlstra272325c2015-04-15 11:41:58 +02001071static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +02001072{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001073 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001074 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001075 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +02001076
1077 /* not for SW PMU */
1078 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +02001079 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001080
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001081 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
1082 if (!cpuctx->hrtimer_active) {
1083 cpuctx->hrtimer_active = 1;
1084 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
1085 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
1086 }
1087 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +02001088
Peter Zijlstra272325c2015-04-15 11:41:58 +02001089 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001090}
1091
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001092void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001093{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001094 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1095 if (!(*count)++)
1096 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001097}
1098
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001099void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001100{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001101 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1102 if (!--(*count))
1103 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001104}
1105
Mark Rutland2fde4f92015-01-07 15:01:54 +00001106static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001107
1108/*
Mark Rutland2fde4f92015-01-07 15:01:54 +00001109 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1110 * perf_event_task_tick() are fully serialized because they're strictly cpu
1111 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1112 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001113 */
Mark Rutland2fde4f92015-01-07 15:01:54 +00001114static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001115{
Mark Rutland2fde4f92015-01-07 15:01:54 +00001116 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001117
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001118 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001119
Mark Rutland2fde4f92015-01-07 15:01:54 +00001120 WARN_ON(!list_empty(&ctx->active_ctx_list));
1121
1122 list_add(&ctx->active_ctx_list, head);
1123}
1124
1125static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1126{
1127 WARN_ON(!irqs_disabled());
1128
1129 WARN_ON(list_empty(&ctx->active_ctx_list));
1130
1131 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001132}
1133
1134static void get_ctx(struct perf_event_context *ctx)
1135{
1136 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
1137}
1138
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001139static void free_ctx(struct rcu_head *head)
1140{
1141 struct perf_event_context *ctx;
1142
1143 ctx = container_of(head, struct perf_event_context, rcu_head);
1144 kfree(ctx->task_ctx_data);
1145 kfree(ctx);
1146}
1147
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001148static void put_ctx(struct perf_event_context *ctx)
1149{
1150 if (atomic_dec_and_test(&ctx->refcount)) {
1151 if (ctx->parent_ctx)
1152 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001153 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001154 put_task_struct(ctx->task);
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001155 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001156 }
1157}
1158
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001159/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001160 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1161 * perf_pmu_migrate_context() we need some magic.
1162 *
1163 * Those places that change perf_event::ctx will hold both
1164 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1165 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001166 * Lock ordering is by mutex address. There are two other sites where
1167 * perf_event_context::mutex nests and those are:
1168 *
1169 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001170 * perf_event_exit_event()
1171 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001172 *
1173 * - perf_event_init_context() [ parent, 0 ]
1174 * inherit_task_group()
1175 * inherit_group()
1176 * inherit_event()
1177 * perf_event_alloc()
1178 * perf_init_event()
1179 * perf_try_init_event() [ child , 1 ]
1180 *
1181 * While it appears there is an obvious deadlock here -- the parent and child
1182 * nesting levels are inverted between the two. This is in fact safe because
1183 * life-time rules separate them. That is an exiting task cannot fork, and a
1184 * spawning task cannot (yet) exit.
1185 *
1186 * But remember that that these are parent<->child context relations, and
1187 * migration does not affect children, therefore these two orderings should not
1188 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001189 *
1190 * The change in perf_event::ctx does not affect children (as claimed above)
1191 * because the sys_perf_event_open() case will install a new event and break
1192 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1193 * concerned with cpuctx and that doesn't have children.
1194 *
1195 * The places that change perf_event::ctx will issue:
1196 *
1197 * perf_remove_from_context();
1198 * synchronize_rcu();
1199 * perf_install_in_context();
1200 *
1201 * to affect the change. The remove_from_context() + synchronize_rcu() should
1202 * quiesce the event, after which we can install it in the new location. This
1203 * means that only external vectors (perf_fops, prctl) can perturb the event
1204 * while in transit. Therefore all such accessors should also acquire
1205 * perf_event_context::mutex to serialize against this.
1206 *
1207 * However; because event->ctx can change while we're waiting to acquire
1208 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1209 * function.
1210 *
1211 * Lock order:
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02001212 * cred_guard_mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001213 * task_struct::perf_event_mutex
1214 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001215 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001216 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001217 * perf_event::mmap_mutex
1218 * mmap_sem
1219 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001220static struct perf_event_context *
1221perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001222{
1223 struct perf_event_context *ctx;
1224
1225again:
1226 rcu_read_lock();
1227 ctx = ACCESS_ONCE(event->ctx);
1228 if (!atomic_inc_not_zero(&ctx->refcount)) {
1229 rcu_read_unlock();
1230 goto again;
1231 }
1232 rcu_read_unlock();
1233
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001234 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001235 if (event->ctx != ctx) {
1236 mutex_unlock(&ctx->mutex);
1237 put_ctx(ctx);
1238 goto again;
1239 }
1240
1241 return ctx;
1242}
1243
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001244static inline struct perf_event_context *
1245perf_event_ctx_lock(struct perf_event *event)
1246{
1247 return perf_event_ctx_lock_nested(event, 0);
1248}
1249
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001250static void perf_event_ctx_unlock(struct perf_event *event,
1251 struct perf_event_context *ctx)
1252{
1253 mutex_unlock(&ctx->mutex);
1254 put_ctx(ctx);
1255}
1256
1257/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001258 * This must be done under the ctx->lock, such as to serialize against
1259 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1260 * calling scheduler related locks and ctx->lock nests inside those.
1261 */
1262static __must_check struct perf_event_context *
1263unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001264{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001265 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1266
1267 lockdep_assert_held(&ctx->lock);
1268
1269 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001270 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001271 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001272
1273 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001274}
1275
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001276static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1277{
1278 /*
1279 * only top level events have the pid namespace they were created in
1280 */
1281 if (event->parent)
1282 event = event->parent;
1283
1284 return task_tgid_nr_ns(p, event->ns);
1285}
1286
1287static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1288{
1289 /*
1290 * only top level events have the pid namespace they were created in
1291 */
1292 if (event->parent)
1293 event = event->parent;
1294
1295 return task_pid_nr_ns(p, event->ns);
1296}
1297
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001298/*
1299 * If we inherit events we want to return the parent event id
1300 * to userspace.
1301 */
1302static u64 primary_event_id(struct perf_event *event)
1303{
1304 u64 id = event->id;
1305
1306 if (event->parent)
1307 id = event->parent->id;
1308
1309 return id;
1310}
1311
1312/*
1313 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001314 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001315 * This has to cope with with the fact that until it is locked,
1316 * the context could get moved to another task.
1317 */
1318static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001319perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001320{
1321 struct perf_event_context *ctx;
1322
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001323retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001324 /*
1325 * One of the few rules of preemptible RCU is that one cannot do
1326 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001327 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001328 * rcu_read_unlock_special().
1329 *
1330 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001331 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001332 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001333 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001334 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001335 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001336 if (ctx) {
1337 /*
1338 * If this context is a clone of another, it might
1339 * get swapped for another underneath us by
1340 * perf_event_task_sched_out, though the
1341 * rcu_read_lock() protects us from any context
1342 * getting freed. Lock the context and check if it
1343 * got swapped before we could get the lock, and retry
1344 * if so. If we locked the right context, then it
1345 * can't get swapped on us any more.
1346 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001347 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001348 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001349 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001350 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001351 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001352 goto retry;
1353 }
1354
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001355 if (ctx->task == TASK_TOMBSTONE ||
1356 !atomic_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001357 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001358 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001359 } else {
1360 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001361 }
1362 }
1363 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001364 if (!ctx)
1365 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001366 return ctx;
1367}
1368
1369/*
1370 * Get the context for a task and increment its pin_count so it
1371 * can't get swapped to another task. This also increments its
1372 * reference count so that the context can't get freed.
1373 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001374static struct perf_event_context *
1375perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001376{
1377 struct perf_event_context *ctx;
1378 unsigned long flags;
1379
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001380 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001381 if (ctx) {
1382 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001383 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001384 }
1385 return ctx;
1386}
1387
1388static void perf_unpin_context(struct perf_event_context *ctx)
1389{
1390 unsigned long flags;
1391
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001392 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001393 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001394 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001395}
1396
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001397/*
1398 * Update the record of the current time in a context.
1399 */
1400static void update_context_time(struct perf_event_context *ctx)
1401{
1402 u64 now = perf_clock();
1403
1404 ctx->time += now - ctx->timestamp;
1405 ctx->timestamp = now;
1406}
1407
Stephane Eranian41587552011-01-03 18:20:01 +02001408static u64 perf_event_time(struct perf_event *event)
1409{
1410 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001411
1412 if (is_cgroup_event(event))
1413 return perf_cgroup_event_time(event);
1414
Stephane Eranian41587552011-01-03 18:20:01 +02001415 return ctx ? ctx->time : 0;
1416}
1417
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001418/*
1419 * Update the total_time_enabled and total_time_running fields for a event.
1420 */
1421static void update_event_times(struct perf_event *event)
1422{
1423 struct perf_event_context *ctx = event->ctx;
1424 u64 run_end;
1425
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001426 lockdep_assert_held(&ctx->lock);
1427
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001428 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1429 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1430 return;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001431
Stephane Eraniane5d13672011-02-14 11:20:01 +02001432 /*
1433 * in cgroup mode, time_enabled represents
1434 * the time the event was enabled AND active
1435 * tasks were in the monitored cgroup. This is
1436 * independent of the activity of the context as
1437 * there may be a mix of cgroup and non-cgroup events.
1438 *
1439 * That is why we treat cgroup events differently
1440 * here.
1441 */
1442 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001443 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001444 else if (ctx->is_active)
1445 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001446 else
1447 run_end = event->tstamp_stopped;
1448
1449 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001450
1451 if (event->state == PERF_EVENT_STATE_INACTIVE)
1452 run_end = event->tstamp_stopped;
1453 else
Stephane Eranian41587552011-01-03 18:20:01 +02001454 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001455
1456 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001457
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001458}
1459
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001460/*
1461 * Update total_time_enabled and total_time_running for all events in a group.
1462 */
1463static void update_group_times(struct perf_event *leader)
1464{
1465 struct perf_event *event;
1466
1467 update_event_times(leader);
1468 list_for_each_entry(event, &leader->sibling_list, group_entry)
1469 update_event_times(event);
1470}
1471
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001472static struct list_head *
1473ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1474{
1475 if (event->attr.pinned)
1476 return &ctx->pinned_groups;
1477 else
1478 return &ctx->flexible_groups;
1479}
1480
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001481/*
1482 * Add a event from the lists for its context.
1483 * Must be called with ctx->mutex and ctx->lock held.
1484 */
1485static void
1486list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1487{
Peter Zijlstrac994d612016-01-08 09:20:23 +01001488 lockdep_assert_held(&ctx->lock);
1489
Peter Zijlstra8a495422010-05-27 15:47:49 +02001490 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1491 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001492
1493 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001494 * If we're a stand alone event or group leader, we go to the context
1495 * list, group events are kept attached to the group so that
1496 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001497 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001498 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001499 struct list_head *list;
1500
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001501 event->group_caps = event->event_caps;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001502
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001503 list = ctx_group_list(event, ctx);
1504 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001505 }
1506
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001507 list_update_cgroup_event(event, ctx, true);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001508
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001509 list_add_rcu(&event->event_entry, &ctx->event_list);
1510 ctx->nr_events++;
1511 if (event->attr.inherit_stat)
1512 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001513
1514 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001515}
1516
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001517/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001518 * Initialize event state based on the perf_event_attr::disabled.
1519 */
1520static inline void perf_event__state_init(struct perf_event *event)
1521{
1522 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1523 PERF_EVENT_STATE_INACTIVE;
1524}
1525
Peter Zijlstraa7239682015-09-09 19:06:33 +02001526static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001527{
1528 int entry = sizeof(u64); /* value */
1529 int size = 0;
1530 int nr = 1;
1531
1532 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1533 size += sizeof(u64);
1534
1535 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1536 size += sizeof(u64);
1537
1538 if (event->attr.read_format & PERF_FORMAT_ID)
1539 entry += sizeof(u64);
1540
1541 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001542 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001543 size += sizeof(u64);
1544 }
1545
1546 size += entry * nr;
1547 event->read_size = size;
1548}
1549
Peter Zijlstraa7239682015-09-09 19:06:33 +02001550static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001551{
1552 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001553 u16 size = 0;
1554
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001555 if (sample_type & PERF_SAMPLE_IP)
1556 size += sizeof(data->ip);
1557
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001558 if (sample_type & PERF_SAMPLE_ADDR)
1559 size += sizeof(data->addr);
1560
1561 if (sample_type & PERF_SAMPLE_PERIOD)
1562 size += sizeof(data->period);
1563
Andi Kleenc3feedf2013-01-24 16:10:28 +01001564 if (sample_type & PERF_SAMPLE_WEIGHT)
1565 size += sizeof(data->weight);
1566
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001567 if (sample_type & PERF_SAMPLE_READ)
1568 size += event->read_size;
1569
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001570 if (sample_type & PERF_SAMPLE_DATA_SRC)
1571 size += sizeof(data->data_src.val);
1572
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001573 if (sample_type & PERF_SAMPLE_TRANSACTION)
1574 size += sizeof(data->txn);
1575
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001576 event->header_size = size;
1577}
1578
Peter Zijlstraa7239682015-09-09 19:06:33 +02001579/*
1580 * Called at perf_event creation and when events are attached/detached from a
1581 * group.
1582 */
1583static void perf_event__header_size(struct perf_event *event)
1584{
1585 __perf_event_read_size(event,
1586 event->group_leader->nr_siblings);
1587 __perf_event_header_size(event, event->attr.sample_type);
1588}
1589
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001590static void perf_event__id_header_size(struct perf_event *event)
1591{
1592 struct perf_sample_data *data;
1593 u64 sample_type = event->attr.sample_type;
1594 u16 size = 0;
1595
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001596 if (sample_type & PERF_SAMPLE_TID)
1597 size += sizeof(data->tid_entry);
1598
1599 if (sample_type & PERF_SAMPLE_TIME)
1600 size += sizeof(data->time);
1601
Adrian Hunterff3d5272013-08-27 11:23:07 +03001602 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1603 size += sizeof(data->id);
1604
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001605 if (sample_type & PERF_SAMPLE_ID)
1606 size += sizeof(data->id);
1607
1608 if (sample_type & PERF_SAMPLE_STREAM_ID)
1609 size += sizeof(data->stream_id);
1610
1611 if (sample_type & PERF_SAMPLE_CPU)
1612 size += sizeof(data->cpu_entry);
1613
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001614 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001615}
1616
Peter Zijlstraa7239682015-09-09 19:06:33 +02001617static bool perf_event_validate_size(struct perf_event *event)
1618{
1619 /*
1620 * The values computed here will be over-written when we actually
1621 * attach the event.
1622 */
1623 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1624 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1625 perf_event__id_header_size(event);
1626
1627 /*
1628 * Sum the lot; should not exceed the 64k limit we have on records.
1629 * Conservative limit to allow for callchains and other variable fields.
1630 */
1631 if (event->read_size + event->header_size +
1632 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1633 return false;
1634
1635 return true;
1636}
1637
Peter Zijlstra8a495422010-05-27 15:47:49 +02001638static void perf_group_attach(struct perf_event *event)
1639{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001640 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001641
Peter Zijlstra3996a912017-01-26 16:39:55 +01001642 lockdep_assert_held(&event->ctx->lock);
1643
Peter Zijlstra74c33372010-10-15 11:40:29 +02001644 /*
1645 * We can have double attach due to group movement in perf_event_open.
1646 */
1647 if (event->attach_state & PERF_ATTACH_GROUP)
1648 return;
1649
Peter Zijlstra8a495422010-05-27 15:47:49 +02001650 event->attach_state |= PERF_ATTACH_GROUP;
1651
1652 if (group_leader == event)
1653 return;
1654
Peter Zijlstra652884f2015-01-23 11:20:10 +01001655 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1656
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001657 group_leader->group_caps &= event->event_caps;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001658
1659 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1660 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001661
1662 perf_event__header_size(group_leader);
1663
1664 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1665 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001666}
1667
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001668/*
1669 * Remove a event from the lists for its context.
1670 * Must be called with ctx->mutex and ctx->lock held.
1671 */
1672static void
1673list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1674{
Peter Zijlstra652884f2015-01-23 11:20:10 +01001675 WARN_ON_ONCE(event->ctx != ctx);
1676 lockdep_assert_held(&ctx->lock);
1677
Peter Zijlstra8a495422010-05-27 15:47:49 +02001678 /*
1679 * We can have double detach due to exit/hot-unplug + close.
1680 */
1681 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001682 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001683
1684 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1685
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001686 list_update_cgroup_event(event, ctx, false);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001687
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001688 ctx->nr_events--;
1689 if (event->attr.inherit_stat)
1690 ctx->nr_stat--;
1691
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001692 list_del_rcu(&event->event_entry);
1693
Peter Zijlstra8a495422010-05-27 15:47:49 +02001694 if (event->group_leader == event)
1695 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001696
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001697 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001698
1699 /*
1700 * If event was in error state, then keep it
1701 * that way, otherwise bogus counts will be
1702 * returned on read(). The only way to get out
1703 * of error state is by explicit re-enabling
1704 * of the event
1705 */
1706 if (event->state > PERF_EVENT_STATE_OFF)
1707 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001708
1709 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001710}
1711
Peter Zijlstra8a495422010-05-27 15:47:49 +02001712static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001713{
1714 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001715 struct list_head *list = NULL;
1716
Peter Zijlstra3996a912017-01-26 16:39:55 +01001717 lockdep_assert_held(&event->ctx->lock);
1718
Peter Zijlstra8a495422010-05-27 15:47:49 +02001719 /*
1720 * We can have double detach due to exit/hot-unplug + close.
1721 */
1722 if (!(event->attach_state & PERF_ATTACH_GROUP))
1723 return;
1724
1725 event->attach_state &= ~PERF_ATTACH_GROUP;
1726
1727 /*
1728 * If this is a sibling, remove it from its group.
1729 */
1730 if (event->group_leader != event) {
1731 list_del_init(&event->group_entry);
1732 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001733 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001734 }
1735
1736 if (!list_empty(&event->group_entry))
1737 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001738
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001739 /*
1740 * If this was a group event with sibling events then
1741 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001742 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001743 */
1744 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001745 if (list)
1746 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001747 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001748
1749 /* Inherit group flags from the previous leader */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001750 sibling->group_caps = event->group_caps;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001751
1752 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001753 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001754
1755out:
1756 perf_event__header_size(event->group_leader);
1757
1758 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1759 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001760}
1761
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001762static bool is_orphaned_event(struct perf_event *event)
1763{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01001764 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001765}
1766
Mark Rutland2c81a642016-06-14 16:10:41 +01001767static inline int __pmu_filter_match(struct perf_event *event)
Mark Rutland66eb5792015-05-13 17:12:23 +01001768{
1769 struct pmu *pmu = event->pmu;
1770 return pmu->filter_match ? pmu->filter_match(event) : 1;
1771}
1772
Mark Rutland2c81a642016-06-14 16:10:41 +01001773/*
1774 * Check whether we should attempt to schedule an event group based on
1775 * PMU-specific filtering. An event group can consist of HW and SW events,
1776 * potentially with a SW leader, so we must check all the filters, to
1777 * determine whether a group is schedulable:
1778 */
1779static inline int pmu_filter_match(struct perf_event *event)
1780{
1781 struct perf_event *child;
1782
1783 if (!__pmu_filter_match(event))
1784 return 0;
1785
1786 list_for_each_entry(child, &event->sibling_list, group_entry) {
1787 if (!__pmu_filter_match(child))
1788 return 0;
1789 }
1790
1791 return 1;
1792}
1793
Stephane Eranianfa66f072010-08-26 16:40:01 +02001794static inline int
1795event_filter_match(struct perf_event *event)
1796{
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02001797 return (event->cpu == -1 || event->cpu == smp_processor_id()) &&
1798 perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001799}
1800
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001801static void
1802event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001803 struct perf_cpu_context *cpuctx,
1804 struct perf_event_context *ctx)
1805{
Stephane Eranian41587552011-01-03 18:20:01 +02001806 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001807 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001808
1809 WARN_ON_ONCE(event->ctx != ctx);
1810 lockdep_assert_held(&ctx->lock);
1811
Stephane Eranianfa66f072010-08-26 16:40:01 +02001812 /*
1813 * An event which could not be activated because of
1814 * filter mismatch still needs to have its timings
1815 * maintained, otherwise bogus information is return
1816 * via read() for time_enabled, time_running:
1817 */
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02001818 if (event->state == PERF_EVENT_STATE_INACTIVE &&
1819 !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001820 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001821 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001822 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001823 }
1824
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001825 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001826 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001827
Alexander Shishkin44377272013-12-16 14:17:36 +02001828 perf_pmu_disable(event->pmu);
1829
Peter Zijlstra28a967c2016-02-24 18:45:46 +01001830 event->tstamp_stopped = tstamp;
1831 event->pmu->del(event, 0);
1832 event->oncpu = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001833 event->state = PERF_EVENT_STATE_INACTIVE;
1834 if (event->pending_disable) {
1835 event->pending_disable = 0;
1836 event->state = PERF_EVENT_STATE_OFF;
1837 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001838
1839 if (!is_software_event(event))
1840 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001841 if (!--ctx->nr_active)
1842 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001843 if (event->attr.freq && event->attr.sample_freq)
1844 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001845 if (event->attr.exclusive || !cpuctx->active_oncpu)
1846 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001847
1848 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001849}
1850
1851static void
1852group_sched_out(struct perf_event *group_event,
1853 struct perf_cpu_context *cpuctx,
1854 struct perf_event_context *ctx)
1855{
1856 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001857 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001858
Mark Rutland3f005e72016-07-26 18:12:21 +01001859 perf_pmu_disable(ctx->pmu);
1860
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001861 event_sched_out(group_event, cpuctx, ctx);
1862
1863 /*
1864 * Schedule out siblings (if any):
1865 */
1866 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1867 event_sched_out(event, cpuctx, ctx);
1868
Mark Rutland3f005e72016-07-26 18:12:21 +01001869 perf_pmu_enable(ctx->pmu);
1870
Stephane Eranianfa66f072010-08-26 16:40:01 +02001871 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001872 cpuctx->exclusive = 0;
1873}
1874
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001875#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01001876
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001877/*
1878 * Cross CPU call to remove a performance event
1879 *
1880 * We disable the event on the hardware level first. After that we
1881 * remove it from the context list.
1882 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001883static void
1884__perf_remove_from_context(struct perf_event *event,
1885 struct perf_cpu_context *cpuctx,
1886 struct perf_event_context *ctx,
1887 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001888{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001889 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001890
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001891 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001892 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001893 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001894 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001895
Peter Zijlstra39a43642016-01-11 12:46:35 +01001896 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001897 ctx->is_active = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01001898 if (ctx->task) {
1899 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1900 cpuctx->task_ctx = NULL;
1901 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001902 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001903}
1904
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001905/*
1906 * Remove the event from a task's (or a CPU's) list of events.
1907 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001908 * If event->ctx is a cloned context, callers must make sure that
1909 * every task struct that event->ctx->task could possibly point to
1910 * remains valid. This is OK when called from perf_release since
1911 * that only calls us on the top-level context, which can't be a clone.
1912 * When called from perf_event_exit_task, it's OK because the
1913 * context has been detached from its task.
1914 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001915static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001916{
Peter Zijlstra3996a912017-01-26 16:39:55 +01001917 struct perf_event_context *ctx = event->ctx;
1918
1919 lockdep_assert_held(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001920
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001921 event_function_call(event, __perf_remove_from_context, (void *)flags);
Peter Zijlstra3996a912017-01-26 16:39:55 +01001922
1923 /*
1924 * The above event_function_call() can NO-OP when it hits
1925 * TASK_TOMBSTONE. In that case we must already have been detached
1926 * from the context (by perf_event_exit_event()) but the grouping
1927 * might still be in-tact.
1928 */
1929 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1930 if ((flags & DETACH_GROUP) &&
1931 (event->attach_state & PERF_ATTACH_GROUP)) {
1932 /*
1933 * Since in that case we cannot possibly be scheduled, simply
1934 * detach now.
1935 */
1936 raw_spin_lock_irq(&ctx->lock);
1937 perf_group_detach(event);
1938 raw_spin_unlock_irq(&ctx->lock);
1939 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001940}
1941
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001942/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001943 * Cross CPU call to disable a performance event
1944 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001945static void __perf_event_disable(struct perf_event *event,
1946 struct perf_cpu_context *cpuctx,
1947 struct perf_event_context *ctx,
1948 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001949{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001950 if (event->state < PERF_EVENT_STATE_INACTIVE)
1951 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001952
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001953 update_context_time(ctx);
1954 update_cgrp_time_from_event(event);
1955 update_group_times(event);
1956 if (event == event->group_leader)
1957 group_sched_out(event, cpuctx, ctx);
1958 else
1959 event_sched_out(event, cpuctx, ctx);
1960 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra7b648012015-12-03 18:35:21 +01001961}
1962
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001963/*
1964 * Disable a event.
1965 *
1966 * If event->ctx is a cloned context, callers must make sure that
1967 * every task struct that event->ctx->task could possibly point to
1968 * remains valid. This condition is satisifed when called through
1969 * perf_event_for_each_child or perf_event_for_each because they
1970 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001971 * goes to exit will block in perf_event_exit_event().
1972 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001973 * When called from perf_pending_event it's OK because event->ctx
1974 * is the current context on this CPU and preemption is disabled,
1975 * hence we can't get into perf_event_task_sched_out for this context.
1976 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001977static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001978{
1979 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001980
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001981 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001982 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001983 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001984 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001985 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001986 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001987
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001988 event_function_call(event, __perf_event_disable, NULL);
1989}
1990
1991void perf_event_disable_local(struct perf_event *event)
1992{
1993 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001994}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001995
1996/*
1997 * Strictly speaking kernel users cannot create groups and therefore this
1998 * interface does not need the perf_event_ctx_lock() magic.
1999 */
2000void perf_event_disable(struct perf_event *event)
2001{
2002 struct perf_event_context *ctx;
2003
2004 ctx = perf_event_ctx_lock(event);
2005 _perf_event_disable(event);
2006 perf_event_ctx_unlock(event, ctx);
2007}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002008EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002009
Jiri Olsa5aab90c2016-10-26 11:48:24 +02002010void perf_event_disable_inatomic(struct perf_event *event)
2011{
2012 event->pending_disable = 1;
2013 irq_work_queue(&event->pending);
2014}
2015
Stephane Eraniane5d13672011-02-14 11:20:01 +02002016static void perf_set_shadow_time(struct perf_event *event,
2017 struct perf_event_context *ctx,
2018 u64 tstamp)
2019{
2020 /*
2021 * use the correct time source for the time snapshot
2022 *
2023 * We could get by without this by leveraging the
2024 * fact that to get to this function, the caller
2025 * has most likely already called update_context_time()
2026 * and update_cgrp_time_xx() and thus both timestamp
2027 * are identical (or very close). Given that tstamp is,
2028 * already adjusted for cgroup, we could say that:
2029 * tstamp - ctx->timestamp
2030 * is equivalent to
2031 * tstamp - cgrp->timestamp.
2032 *
2033 * Then, in perf_output_read(), the calculation would
2034 * work with no changes because:
2035 * - event is guaranteed scheduled in
2036 * - no scheduled out in between
2037 * - thus the timestamp would be the same
2038 *
2039 * But this is a bit hairy.
2040 *
2041 * So instead, we have an explicit cgroup call to remain
2042 * within the time time source all along. We believe it
2043 * is cleaner and simpler to understand.
2044 */
2045 if (is_cgroup_event(event))
2046 perf_cgroup_set_shadow_time(event, tstamp);
2047 else
2048 event->shadow_ctx_time = tstamp - ctx->timestamp;
2049}
2050
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002051#define MAX_INTERRUPTS (~0ULL)
2052
2053static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002054static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002055
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002056static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002057event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002058 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002059 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002060{
Stephane Eranian41587552011-01-03 18:20:01 +02002061 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02002062 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02002063
Peter Zijlstra63342412014-05-05 11:49:16 +02002064 lockdep_assert_held(&ctx->lock);
2065
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002066 if (event->state <= PERF_EVENT_STATE_OFF)
2067 return 0;
2068
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002069 WRITE_ONCE(event->oncpu, smp_processor_id());
2070 /*
2071 * Order event::oncpu write to happen before the ACTIVE state
2072 * is visible.
2073 */
2074 smp_wmb();
2075 WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002076
2077 /*
2078 * Unthrottle events, since we scheduled we might have missed several
2079 * ticks already, also for a heavily scheduling task there is little
2080 * guarantee it'll get a tick in a timely manner.
2081 */
2082 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
2083 perf_log_throttle(event, 1);
2084 event->hw.interrupts = 0;
2085 }
2086
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002087 /*
2088 * The new state must be visible before we turn it on in the hardware:
2089 */
2090 smp_wmb();
2091
Alexander Shishkin44377272013-12-16 14:17:36 +02002092 perf_pmu_disable(event->pmu);
2093
Shaohua Li72f669c2015-02-05 15:55:31 -08002094 perf_set_shadow_time(event, ctx, tstamp);
2095
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002096 perf_log_itrace_start(event);
2097
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002098 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002099 event->state = PERF_EVENT_STATE_INACTIVE;
2100 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02002101 ret = -EAGAIN;
2102 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002103 }
2104
Peter Zijlstra00a29162015-07-27 10:35:07 +02002105 event->tstamp_running += tstamp - event->tstamp_stopped;
2106
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002107 if (!is_software_event(event))
2108 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002109 if (!ctx->nr_active++)
2110 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002111 if (event->attr.freq && event->attr.sample_freq)
2112 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002113
2114 if (event->attr.exclusive)
2115 cpuctx->exclusive = 1;
2116
Alexander Shishkin44377272013-12-16 14:17:36 +02002117out:
2118 perf_pmu_enable(event->pmu);
2119
2120 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002121}
2122
2123static int
2124group_sched_in(struct perf_event *group_event,
2125 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002126 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002127{
Lin Ming6bde9b62010-04-23 13:56:00 +08002128 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01002129 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02002130 u64 now = ctx->time;
2131 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002132
2133 if (group_event->state == PERF_EVENT_STATE_OFF)
2134 return 0;
2135
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07002136 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08002137
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002138 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002139 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02002140 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002141 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02002142 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002143
2144 /*
2145 * Schedule in siblings as one group (if any):
2146 */
2147 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002148 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002149 partial_group = event;
2150 goto group_error;
2151 }
2152 }
2153
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002154 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002155 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002156
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002157group_error:
2158 /*
2159 * Groups can be scheduled in as one unit only, so undo any
2160 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02002161 * The events up to the failed event are scheduled out normally,
2162 * tstamp_stopped will be updated.
2163 *
2164 * The failed events and the remaining siblings need to have
2165 * their timings updated as if they had gone thru event_sched_in()
2166 * and event_sched_out(). This is required to get consistent timings
2167 * across the group. This also takes care of the case where the group
2168 * could never be scheduled by ensuring tstamp_stopped is set to mark
2169 * the time the event was actually stopped, such that time delta
2170 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002171 */
2172 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2173 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02002174 simulate = true;
2175
2176 if (simulate) {
2177 event->tstamp_running += now - event->tstamp_stopped;
2178 event->tstamp_stopped = now;
2179 } else {
2180 event_sched_out(event, cpuctx, ctx);
2181 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002182 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002183 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002184
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002185 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002186
Peter Zijlstra272325c2015-04-15 11:41:58 +02002187 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002188
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002189 return -EAGAIN;
2190}
2191
2192/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002193 * Work out whether we can put this event group on the CPU now.
2194 */
2195static int group_can_go_on(struct perf_event *event,
2196 struct perf_cpu_context *cpuctx,
2197 int can_add_hw)
2198{
2199 /*
2200 * Groups consisting entirely of software events can always go on.
2201 */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07002202 if (event->group_caps & PERF_EV_CAP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002203 return 1;
2204 /*
2205 * If an exclusive group is already on, no other hardware
2206 * events can go on.
2207 */
2208 if (cpuctx->exclusive)
2209 return 0;
2210 /*
2211 * If this group is exclusive and there are already
2212 * events on the CPU, it can't go on.
2213 */
2214 if (event->attr.exclusive && cpuctx->active_oncpu)
2215 return 0;
2216 /*
2217 * Otherwise, try to add it if all previous groups were able
2218 * to go on.
2219 */
2220 return can_add_hw;
2221}
2222
2223static void add_event_to_ctx(struct perf_event *event,
2224 struct perf_event_context *ctx)
2225{
Stephane Eranian41587552011-01-03 18:20:01 +02002226 u64 tstamp = perf_event_time(event);
2227
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002228 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002229 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002230 event->tstamp_enabled = tstamp;
2231 event->tstamp_running = tstamp;
2232 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002233}
2234
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002235static void ctx_sched_out(struct perf_event_context *ctx,
2236 struct perf_cpu_context *cpuctx,
2237 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002238static void
2239ctx_sched_in(struct perf_event_context *ctx,
2240 struct perf_cpu_context *cpuctx,
2241 enum event_type_t event_type,
2242 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002243
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002244static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2245 struct perf_event_context *ctx)
2246{
2247 if (!cpuctx->task_ctx)
2248 return;
2249
2250 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2251 return;
2252
2253 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2254}
2255
Peter Zijlstradce58552011-04-09 21:17:46 +02002256static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2257 struct perf_event_context *ctx,
2258 struct task_struct *task)
2259{
2260 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2261 if (ctx)
2262 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2263 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2264 if (ctx)
2265 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2266}
2267
Peter Zijlstra3e349502016-01-08 10:01:18 +01002268static void ctx_resched(struct perf_cpu_context *cpuctx,
2269 struct perf_event_context *task_ctx)
Peter Zijlstra00179602015-11-30 16:26:35 +01002270{
Peter Zijlstra3e349502016-01-08 10:01:18 +01002271 perf_pmu_disable(cpuctx->ctx.pmu);
2272 if (task_ctx)
2273 task_ctx_sched_out(cpuctx, task_ctx);
2274 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2275 perf_event_sched_in(cpuctx, task_ctx, current);
2276 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002277}
2278
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002279/*
2280 * Cross CPU call to install and enable a performance event
2281 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002282 * Very similar to remote_function() + event_function() but cannot assume that
2283 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002284 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002285static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002286{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002287 struct perf_event *event = info;
2288 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002289 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002290 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra1c686332016-12-09 14:59:00 +01002291 bool reprogram = true;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002292 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002293
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002294 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002295 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002296 raw_spin_lock(&ctx->lock);
2297 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002298
Peter Zijlstra1c686332016-12-09 14:59:00 +01002299 reprogram = (ctx->task == current);
2300
2301 /*
2302 * If the task is running, it must be running on this CPU,
2303 * otherwise we cannot reprogram things.
2304 *
2305 * If its not running, we don't care, ctx->lock will
2306 * serialize against it becoming runnable.
2307 */
2308 if (task_curr(ctx->task) && !reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002309 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002310 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002311 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002312
Peter Zijlstra1c686332016-12-09 14:59:00 +01002313 WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002314 } else if (task_ctx) {
2315 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002316 }
2317
Peter Zijlstra1c686332016-12-09 14:59:00 +01002318 if (reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002319 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2320 add_event_to_ctx(event, ctx);
2321 ctx_resched(cpuctx, task_ctx);
2322 } else {
2323 add_event_to_ctx(event, ctx);
2324 }
2325
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002326unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002327 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002328
Peter Zijlstraa0963092016-02-24 18:45:50 +01002329 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002330}
2331
2332/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002333 * Attach a performance event to a context.
2334 *
2335 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002336 */
2337static void
2338perf_install_in_context(struct perf_event_context *ctx,
2339 struct perf_event *event,
2340 int cpu)
2341{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002342 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002343
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002344 lockdep_assert_held(&ctx->mutex);
2345
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002346 if (event->cpu != -1)
2347 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002348
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002349 /*
2350 * Ensures that if we can observe event->ctx, both the event and ctx
2351 * will be 'complete'. See perf_iterate_sb_cpu().
2352 */
2353 smp_store_release(&event->ctx, ctx);
2354
Peter Zijlstraa0963092016-02-24 18:45:50 +01002355 if (!task) {
2356 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002357 return;
2358 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002359
Peter Zijlstraa0963092016-02-24 18:45:50 +01002360 /*
2361 * Should not happen, we validate the ctx is still alive before calling.
2362 */
2363 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2364 return;
2365
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002366 /*
2367 * Installing events is tricky because we cannot rely on ctx->is_active
2368 * to be set in case this is the nr_events 0 -> 1 transition.
Peter Zijlstra1c686332016-12-09 14:59:00 +01002369 *
2370 * Instead we use task_curr(), which tells us if the task is running.
2371 * However, since we use task_curr() outside of rq::lock, we can race
2372 * against the actual state. This means the result can be wrong.
2373 *
2374 * If we get a false positive, we retry, this is harmless.
2375 *
2376 * If we get a false negative, things are complicated. If we are after
2377 * perf_event_context_sched_in() ctx::lock will serialize us, and the
2378 * value must be correct. If we're before, it doesn't matter since
2379 * perf_event_context_sched_in() will program the counter.
2380 *
2381 * However, this hinges on the remote context switch having observed
2382 * our task->perf_event_ctxp[] store, such that it will in fact take
2383 * ctx::lock in perf_event_context_sched_in().
2384 *
2385 * We do this by task_function_call(), if the IPI fails to hit the task
2386 * we know any future context switch of task must see the
2387 * perf_event_ctpx[] store.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002388 */
Peter Zijlstra1c686332016-12-09 14:59:00 +01002389
Peter Zijlstraa0963092016-02-24 18:45:50 +01002390 /*
Peter Zijlstra1c686332016-12-09 14:59:00 +01002391 * This smp_mb() orders the task->perf_event_ctxp[] store with the
2392 * task_cpu() load, such that if the IPI then does not find the task
2393 * running, a future context switch of that task must observe the
2394 * store.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002395 */
Peter Zijlstra1c686332016-12-09 14:59:00 +01002396 smp_mb();
2397again:
2398 if (!task_function_call(task, __perf_install_in_context, event))
Peter Zijlstraa0963092016-02-24 18:45:50 +01002399 return;
2400
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002401 raw_spin_lock_irq(&ctx->lock);
2402 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002403 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2404 /*
2405 * Cannot happen because we already checked above (which also
2406 * cannot happen), and we hold ctx->mutex, which serializes us
2407 * against perf_event_exit_task_context().
2408 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002409 raw_spin_unlock_irq(&ctx->lock);
2410 return;
2411 }
Peter Zijlstraa0963092016-02-24 18:45:50 +01002412 /*
Peter Zijlstra1c686332016-12-09 14:59:00 +01002413 * If the task is not running, ctx->lock will avoid it becoming so,
2414 * thus we can safely install the event.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002415 */
Peter Zijlstra1c686332016-12-09 14:59:00 +01002416 if (task_curr(task)) {
2417 raw_spin_unlock_irq(&ctx->lock);
2418 goto again;
2419 }
2420 add_event_to_ctx(event, ctx);
2421 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002422}
2423
2424/*
2425 * Put a event into inactive state and update time fields.
2426 * Enabling the leader of a group effectively enables all
2427 * the group members that aren't explicitly disabled, so we
2428 * have to update their ->tstamp_enabled also.
2429 * Note: this works for group members as well as group leaders
2430 * since the non-leader members' sibling_lists will be empty.
2431 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002432static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002433{
2434 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002435 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002436
2437 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002438 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002439 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002440 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2441 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002442 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002443}
2444
2445/*
2446 * Cross CPU call to enable a performance event
2447 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002448static void __perf_event_enable(struct perf_event *event,
2449 struct perf_cpu_context *cpuctx,
2450 struct perf_event_context *ctx,
2451 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002452{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002453 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002454 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002455
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002456 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2457 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002458 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002459
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002460 if (ctx->is_active)
2461 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2462
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002463 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002464
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002465 if (!ctx->is_active)
2466 return;
2467
Stephane Eraniane5d13672011-02-14 11:20:01 +02002468 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002469 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02002470 perf_cgroup_defer_enabled(event);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002471 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002472 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002473 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002474
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002475 /*
2476 * If the event is in a group and isn't the group leader,
2477 * then don't put it on unless the group is on.
2478 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002479 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2480 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002481 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002482 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002483
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002484 task_ctx = cpuctx->task_ctx;
2485 if (ctx->task)
2486 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002487
Peter Zijlstraaee7dbc2016-01-08 10:45:11 +01002488 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002489}
2490
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002491/*
2492 * Enable a event.
2493 *
2494 * If event->ctx is a cloned context, callers must make sure that
2495 * every task struct that event->ctx->task could possibly point to
2496 * remains valid. This condition is satisfied when called through
2497 * perf_event_for_each_child or perf_event_for_each as described
2498 * for perf_event_disable.
2499 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002500static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002501{
2502 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002503
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002504 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002505 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2506 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002507 raw_spin_unlock_irq(&ctx->lock);
2508 return;
2509 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002510
2511 /*
2512 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002513 *
2514 * That way, if we see the event in error state below, we know that it
2515 * has gone back into error state, as distinct from the task having
2516 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002517 */
2518 if (event->state == PERF_EVENT_STATE_ERROR)
2519 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002520 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002521
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002522 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002523}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002524
2525/*
2526 * See perf_event_disable();
2527 */
2528void perf_event_enable(struct perf_event *event)
2529{
2530 struct perf_event_context *ctx;
2531
2532 ctx = perf_event_ctx_lock(event);
2533 _perf_event_enable(event);
2534 perf_event_ctx_unlock(event, ctx);
2535}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002536EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002537
Alexander Shishkin375637b2016-04-27 18:44:46 +03002538struct stop_event_data {
2539 struct perf_event *event;
2540 unsigned int restart;
2541};
2542
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002543static int __perf_event_stop(void *info)
2544{
Alexander Shishkin375637b2016-04-27 18:44:46 +03002545 struct stop_event_data *sd = info;
2546 struct perf_event *event = sd->event;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002547
Alexander Shishkin375637b2016-04-27 18:44:46 +03002548 /* if it's already INACTIVE, do nothing */
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002549 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2550 return 0;
2551
2552 /* matches smp_wmb() in event_sched_in() */
2553 smp_rmb();
2554
2555 /*
2556 * There is a window with interrupts enabled before we get here,
2557 * so we need to check again lest we try to stop another CPU's event.
2558 */
2559 if (READ_ONCE(event->oncpu) != smp_processor_id())
2560 return -EAGAIN;
2561
2562 event->pmu->stop(event, PERF_EF_UPDATE);
2563
Alexander Shishkin375637b2016-04-27 18:44:46 +03002564 /*
2565 * May race with the actual stop (through perf_pmu_output_stop()),
2566 * but it is only used for events with AUX ring buffer, and such
2567 * events will refuse to restart because of rb::aux_mmap_count==0,
2568 * see comments in perf_aux_output_begin().
2569 *
2570 * Since this is happening on a event-local CPU, no trace is lost
2571 * while restarting.
2572 */
2573 if (sd->restart)
Will Deaconc9bbdd42016-08-15 11:42:45 +01002574 event->pmu->start(event, 0);
Alexander Shishkin375637b2016-04-27 18:44:46 +03002575
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002576 return 0;
2577}
2578
Alexander Shishkin767ae082016-09-06 16:23:49 +03002579static int perf_event_stop(struct perf_event *event, int restart)
Alexander Shishkin375637b2016-04-27 18:44:46 +03002580{
2581 struct stop_event_data sd = {
2582 .event = event,
Alexander Shishkin767ae082016-09-06 16:23:49 +03002583 .restart = restart,
Alexander Shishkin375637b2016-04-27 18:44:46 +03002584 };
2585 int ret = 0;
2586
2587 do {
2588 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2589 return 0;
2590
2591 /* matches smp_wmb() in event_sched_in() */
2592 smp_rmb();
2593
2594 /*
2595 * We only want to restart ACTIVE events, so if the event goes
2596 * inactive here (event->oncpu==-1), there's nothing more to do;
2597 * fall through with ret==-ENXIO.
2598 */
2599 ret = cpu_function_call(READ_ONCE(event->oncpu),
2600 __perf_event_stop, &sd);
2601 } while (ret == -EAGAIN);
2602
2603 return ret;
2604}
2605
2606/*
2607 * In order to contain the amount of racy and tricky in the address filter
2608 * configuration management, it is a two part process:
2609 *
2610 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
2611 * we update the addresses of corresponding vmas in
2612 * event::addr_filters_offs array and bump the event::addr_filters_gen;
2613 * (p2) when an event is scheduled in (pmu::add), it calls
2614 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
2615 * if the generation has changed since the previous call.
2616 *
2617 * If (p1) happens while the event is active, we restart it to force (p2).
2618 *
2619 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
2620 * pre-existing mappings, called once when new filters arrive via SET_FILTER
2621 * ioctl;
2622 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
2623 * registered mapping, called for every new mmap(), with mm::mmap_sem down
2624 * for reading;
2625 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
2626 * of exec.
2627 */
2628void perf_event_addr_filters_sync(struct perf_event *event)
2629{
2630 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
2631
2632 if (!has_addr_filter(event))
2633 return;
2634
2635 raw_spin_lock(&ifh->lock);
2636 if (event->addr_filters_gen != event->hw.addr_filters_gen) {
2637 event->pmu->addr_filters_sync(event);
2638 event->hw.addr_filters_gen = event->addr_filters_gen;
2639 }
2640 raw_spin_unlock(&ifh->lock);
2641}
2642EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
2643
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002644static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002645{
2646 /*
2647 * not supported on inherited events
2648 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002649 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002650 return -EINVAL;
2651
2652 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002653 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002654
2655 return 0;
2656}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002657
2658/*
2659 * See perf_event_disable()
2660 */
2661int perf_event_refresh(struct perf_event *event, int refresh)
2662{
2663 struct perf_event_context *ctx;
2664 int ret;
2665
2666 ctx = perf_event_ctx_lock(event);
2667 ret = _perf_event_refresh(event, refresh);
2668 perf_event_ctx_unlock(event, ctx);
2669
2670 return ret;
2671}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002672EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002673
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002674static void ctx_sched_out(struct perf_event_context *ctx,
2675 struct perf_cpu_context *cpuctx,
2676 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002677{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002678 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002679 struct perf_event *event;
2680
2681 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002682
Peter Zijlstra39a43642016-01-11 12:46:35 +01002683 if (likely(!ctx->nr_events)) {
2684 /*
2685 * See __perf_remove_from_context().
2686 */
2687 WARN_ON_ONCE(ctx->is_active);
2688 if (ctx->task)
2689 WARN_ON_ONCE(cpuctx->task_ctx);
2690 return;
2691 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002692
Peter Zijlstradb24d332011-04-09 21:17:45 +02002693 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002694 if (!(ctx->is_active & EVENT_ALL))
2695 ctx->is_active = 0;
2696
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002697 if (ctx->task) {
2698 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2699 if (!ctx->is_active)
2700 cpuctx->task_ctx = NULL;
2701 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002702
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002703 /*
2704 * Always update time if it was set; not only when it changes.
2705 * Otherwise we can 'forget' to update time for any but the last
2706 * context we sched out. For example:
2707 *
2708 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2709 * ctx_sched_out(.event_type = EVENT_PINNED)
2710 *
2711 * would only update time for the pinned events.
2712 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002713 if (is_active & EVENT_TIME) {
2714 /* update (and stop) ctx time */
2715 update_context_time(ctx);
2716 update_cgrp_time_from_cpuctx(cpuctx);
2717 }
2718
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002719 is_active ^= ctx->is_active; /* changed bits */
2720
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002721 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002722 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002723
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002724 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002725 if (is_active & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002726 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2727 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002728 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002729
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002730 if (is_active & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002731 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002732 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002733 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002734 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002735}
2736
2737/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002738 * Test whether two contexts are equivalent, i.e. whether they have both been
2739 * cloned from the same version of the same context.
2740 *
2741 * Equivalence is measured using a generation number in the context that is
2742 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2743 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002744 */
2745static int context_equiv(struct perf_event_context *ctx1,
2746 struct perf_event_context *ctx2)
2747{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002748 lockdep_assert_held(&ctx1->lock);
2749 lockdep_assert_held(&ctx2->lock);
2750
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002751 /* Pinning disables the swap optimization */
2752 if (ctx1->pin_count || ctx2->pin_count)
2753 return 0;
2754
2755 /* If ctx1 is the parent of ctx2 */
2756 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2757 return 1;
2758
2759 /* If ctx2 is the parent of ctx1 */
2760 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2761 return 1;
2762
2763 /*
2764 * If ctx1 and ctx2 have the same parent; we flatten the parent
2765 * hierarchy, see perf_event_init_context().
2766 */
2767 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2768 ctx1->parent_gen == ctx2->parent_gen)
2769 return 1;
2770
2771 /* Unmatched */
2772 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002773}
2774
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002775static void __perf_event_sync_stat(struct perf_event *event,
2776 struct perf_event *next_event)
2777{
2778 u64 value;
2779
2780 if (!event->attr.inherit_stat)
2781 return;
2782
2783 /*
2784 * Update the event value, we cannot use perf_event_read()
2785 * because we're in the middle of a context switch and have IRQs
2786 * disabled, which upsets smp_call_function_single(), however
2787 * we know the event must be on the current CPU, therefore we
2788 * don't need to use it.
2789 */
2790 switch (event->state) {
2791 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002792 event->pmu->read(event);
2793 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002794
2795 case PERF_EVENT_STATE_INACTIVE:
2796 update_event_times(event);
2797 break;
2798
2799 default:
2800 break;
2801 }
2802
2803 /*
2804 * In order to keep per-task stats reliable we need to flip the event
2805 * values when we flip the contexts.
2806 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002807 value = local64_read(&next_event->count);
2808 value = local64_xchg(&event->count, value);
2809 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002810
2811 swap(event->total_time_enabled, next_event->total_time_enabled);
2812 swap(event->total_time_running, next_event->total_time_running);
2813
2814 /*
2815 * Since we swizzled the values, update the user visible data too.
2816 */
2817 perf_event_update_userpage(event);
2818 perf_event_update_userpage(next_event);
2819}
2820
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002821static void perf_event_sync_stat(struct perf_event_context *ctx,
2822 struct perf_event_context *next_ctx)
2823{
2824 struct perf_event *event, *next_event;
2825
2826 if (!ctx->nr_stat)
2827 return;
2828
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002829 update_context_time(ctx);
2830
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002831 event = list_first_entry(&ctx->event_list,
2832 struct perf_event, event_entry);
2833
2834 next_event = list_first_entry(&next_ctx->event_list,
2835 struct perf_event, event_entry);
2836
2837 while (&event->event_entry != &ctx->event_list &&
2838 &next_event->event_entry != &next_ctx->event_list) {
2839
2840 __perf_event_sync_stat(event, next_event);
2841
2842 event = list_next_entry(event, event_entry);
2843 next_event = list_next_entry(next_event, event_entry);
2844 }
2845}
2846
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002847static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2848 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002849{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002850 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002851 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002852 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002853 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002854 int do_switch = 1;
2855
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002856 if (likely(!ctx))
2857 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002858
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002859 cpuctx = __get_cpu_context(ctx);
2860 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002861 return;
2862
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002863 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002864 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002865 if (!next_ctx)
2866 goto unlock;
2867
2868 parent = rcu_dereference(ctx->parent_ctx);
2869 next_parent = rcu_dereference(next_ctx->parent_ctx);
2870
2871 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002872 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002873 goto unlock;
2874
2875 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002876 /*
2877 * Looks like the two contexts are clones, so we might be
2878 * able to optimize the context switch. We lock both
2879 * contexts and check that they are clones under the
2880 * lock (including re-checking that neither has been
2881 * uncloned in the meantime). It doesn't matter which
2882 * order we take the locks because no other cpu could
2883 * be trying to lock both of these tasks.
2884 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002885 raw_spin_lock(&ctx->lock);
2886 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002887 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002888 WRITE_ONCE(ctx->task, next);
2889 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05002890
2891 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2892
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002893 /*
2894 * RCU_INIT_POINTER here is safe because we've not
2895 * modified the ctx and the above modification of
2896 * ctx->task and ctx->task_ctx_data are immaterial
2897 * since those values are always verified under
2898 * ctx->lock which we're now holding.
2899 */
2900 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2901 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2902
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002903 do_switch = 0;
2904
2905 perf_event_sync_stat(ctx, next_ctx);
2906 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002907 raw_spin_unlock(&next_ctx->lock);
2908 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002909 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002910unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002911 rcu_read_unlock();
2912
2913 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002914 raw_spin_lock(&ctx->lock);
Peter Zijlstra8833d0e2016-01-08 10:02:37 +01002915 task_ctx_sched_out(cpuctx, ctx);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002916 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002917 }
2918}
2919
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002920static DEFINE_PER_CPU(struct list_head, sched_cb_list);
2921
Yan, Zhengba532502014-11-04 21:55:58 -05002922void perf_sched_cb_dec(struct pmu *pmu)
2923{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002924 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2925
Yan, Zhengba532502014-11-04 21:55:58 -05002926 this_cpu_dec(perf_sched_cb_usages);
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002927
2928 if (!--cpuctx->sched_cb_usage)
2929 list_del(&cpuctx->sched_cb_entry);
Yan, Zhengba532502014-11-04 21:55:58 -05002930}
2931
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002932
Yan, Zhengba532502014-11-04 21:55:58 -05002933void perf_sched_cb_inc(struct pmu *pmu)
2934{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002935 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2936
2937 if (!cpuctx->sched_cb_usage++)
2938 list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list));
2939
Yan, Zhengba532502014-11-04 21:55:58 -05002940 this_cpu_inc(perf_sched_cb_usages);
2941}
2942
2943/*
2944 * This function provides the context switch callback to the lower code
2945 * layer. It is invoked ONLY when the context switch callback is enabled.
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +02002946 *
2947 * This callback is relevant even to per-cpu events; for example multi event
2948 * PEBS requires this to provide PID/TID information. This requires we flush
2949 * all queued PEBS records before we context switch to a new task.
Yan, Zhengba532502014-11-04 21:55:58 -05002950 */
2951static void perf_pmu_sched_task(struct task_struct *prev,
2952 struct task_struct *next,
2953 bool sched_in)
2954{
2955 struct perf_cpu_context *cpuctx;
2956 struct pmu *pmu;
Yan, Zhengba532502014-11-04 21:55:58 -05002957
2958 if (prev == next)
2959 return;
2960
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002961 list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) {
2962 pmu = cpuctx->unique_pmu; /* software PMUs will not have sched_task */
Yan, Zhengba532502014-11-04 21:55:58 -05002963
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002964 if (WARN_ON_ONCE(!pmu->sched_task))
2965 continue;
Yan, Zhengba532502014-11-04 21:55:58 -05002966
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002967 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2968 perf_pmu_disable(pmu);
Yan, Zhengba532502014-11-04 21:55:58 -05002969
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002970 pmu->sched_task(cpuctx->task_ctx, sched_in);
Yan, Zhengba532502014-11-04 21:55:58 -05002971
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002972 perf_pmu_enable(pmu);
2973 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Yan, Zhengba532502014-11-04 21:55:58 -05002974 }
Yan, Zhengba532502014-11-04 21:55:58 -05002975}
2976
Adrian Hunter45ac1402015-07-21 12:44:02 +03002977static void perf_event_switch(struct task_struct *task,
2978 struct task_struct *next_prev, bool sched_in);
2979
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002980#define for_each_task_context_nr(ctxn) \
2981 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2982
2983/*
2984 * Called from scheduler to remove the events of the current task,
2985 * with interrupts disabled.
2986 *
2987 * We stop each event and update the event value in event->count.
2988 *
2989 * This does not protect us against NMI, but disable()
2990 * sets the disabled bit in the control field of event _before_
2991 * accessing the event control register. If a NMI hits, then it will
2992 * not restart the event.
2993 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002994void __perf_event_task_sched_out(struct task_struct *task,
2995 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002996{
2997 int ctxn;
2998
Yan, Zhengba532502014-11-04 21:55:58 -05002999 if (__this_cpu_read(perf_sched_cb_usages))
3000 perf_pmu_sched_task(task, next, false);
3001
Adrian Hunter45ac1402015-07-21 12:44:02 +03003002 if (atomic_read(&nr_switch_events))
3003 perf_event_switch(task, next, false);
3004
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003005 for_each_task_context_nr(ctxn)
3006 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003007
3008 /*
3009 * if cgroup events exist on this CPU, then we need
3010 * to check if we have to switch out PMU state.
3011 * cgroup event are system-wide mode only
3012 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05003013 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003014 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003015}
3016
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003017/*
3018 * Called with IRQs disabled
3019 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003020static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
3021 enum event_type_t event_type)
3022{
3023 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024}
3025
3026static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003027ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003028 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003029{
3030 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003031
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003032 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
3033 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003034 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02003035 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003036 continue;
3037
Stephane Eraniane5d13672011-02-14 11:20:01 +02003038 /* may need to reset tstamp_enabled */
3039 if (is_cgroup_event(event))
3040 perf_cgroup_mark_enabled(event, ctx);
3041
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08003042 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01003043 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003044
3045 /*
3046 * If this pinned group hasn't been scheduled,
3047 * put it in error state.
3048 */
3049 if (event->state == PERF_EVENT_STATE_INACTIVE) {
3050 update_group_times(event);
3051 event->state = PERF_EVENT_STATE_ERROR;
3052 }
3053 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003054}
3055
3056static void
3057ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003058 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003059{
3060 struct perf_event *event;
3061 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003062
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003063 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
3064 /* Ignore events in OFF or ERROR state */
3065 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003066 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003067 /*
3068 * Listen to the 'cpu' scheduling filter constraint
3069 * of events:
3070 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02003071 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003072 continue;
3073
Stephane Eraniane5d13672011-02-14 11:20:01 +02003074 /* may need to reset tstamp_enabled */
3075 if (is_cgroup_event(event))
3076 perf_cgroup_mark_enabled(event, ctx);
3077
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003078 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01003079 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003080 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003081 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003082 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003083}
3084
3085static void
3086ctx_sched_in(struct perf_event_context *ctx,
3087 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003088 enum event_type_t event_type,
3089 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003090{
Peter Zijlstradb24d332011-04-09 21:17:45 +02003091 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01003092 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02003093
Peter Zijlstrac994d612016-01-08 09:20:23 +01003094 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003095
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003096 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003097 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003098
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003099 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003100 if (ctx->task) {
3101 if (!is_active)
3102 cpuctx->task_ctx = ctx;
3103 else
3104 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3105 }
3106
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003107 is_active ^= ctx->is_active; /* changed bits */
3108
3109 if (is_active & EVENT_TIME) {
3110 /* start ctx time */
3111 now = perf_clock();
3112 ctx->timestamp = now;
3113 perf_cgroup_set_timestamp(task, ctx);
3114 }
3115
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003116 /*
3117 * First go through the list and put on any pinned groups
3118 * in order to give them the best chance of going on.
3119 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003120 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003121 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003122
3123 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003124 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003125 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003126}
3127
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003128static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003129 enum event_type_t event_type,
3130 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003131{
3132 struct perf_event_context *ctx = &cpuctx->ctx;
3133
Stephane Eraniane5d13672011-02-14 11:20:01 +02003134 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003135}
3136
Stephane Eraniane5d13672011-02-14 11:20:01 +02003137static void perf_event_context_sched_in(struct perf_event_context *ctx,
3138 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003139{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003140 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003141
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003142 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003143 if (cpuctx->task_ctx == ctx)
3144 return;
3145
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003146 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003147 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003148 /*
3149 * We want to keep the following priority order:
3150 * cpu pinned (that don't need to move), task pinned,
3151 * cpu flexible, task flexible.
3152 */
3153 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003154 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003155 perf_pmu_enable(ctx->pmu);
3156 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003157}
3158
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003159/*
3160 * Called from scheduler to add the events of the current task
3161 * with interrupts disabled.
3162 *
3163 * We restore the event value and then enable it.
3164 *
3165 * This does not protect us against NMI, but enable()
3166 * sets the enabled bit in the control field of event _before_
3167 * accessing the event control register. If a NMI hits, then it will
3168 * keep the event running.
3169 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003170void __perf_event_task_sched_in(struct task_struct *prev,
3171 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003172{
3173 struct perf_event_context *ctx;
3174 int ctxn;
3175
Peter Zijlstra7e41d172016-01-08 09:21:40 +01003176 /*
3177 * If cgroup events exist on this CPU, then we need to check if we have
3178 * to switch in PMU state; cgroup event are system-wide mode only.
3179 *
3180 * Since cgroup events are CPU events, we must schedule these in before
3181 * we schedule in the task events.
3182 */
3183 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3184 perf_cgroup_sched_in(prev, task);
3185
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003186 for_each_task_context_nr(ctxn) {
3187 ctx = task->perf_event_ctxp[ctxn];
3188 if (likely(!ctx))
3189 continue;
3190
Stephane Eraniane5d13672011-02-14 11:20:01 +02003191 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003192 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003193
Adrian Hunter45ac1402015-07-21 12:44:02 +03003194 if (atomic_read(&nr_switch_events))
3195 perf_event_switch(task, prev, true);
3196
Yan, Zhengba532502014-11-04 21:55:58 -05003197 if (__this_cpu_read(perf_sched_cb_usages))
3198 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003199}
3200
Peter Zijlstraabd50712010-01-26 18:50:16 +01003201static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3202{
3203 u64 frequency = event->attr.sample_freq;
3204 u64 sec = NSEC_PER_SEC;
3205 u64 divisor, dividend;
3206
3207 int count_fls, nsec_fls, frequency_fls, sec_fls;
3208
3209 count_fls = fls64(count);
3210 nsec_fls = fls64(nsec);
3211 frequency_fls = fls64(frequency);
3212 sec_fls = 30;
3213
3214 /*
3215 * We got @count in @nsec, with a target of sample_freq HZ
3216 * the target period becomes:
3217 *
3218 * @count * 10^9
3219 * period = -------------------
3220 * @nsec * sample_freq
3221 *
3222 */
3223
3224 /*
3225 * Reduce accuracy by one bit such that @a and @b converge
3226 * to a similar magnitude.
3227 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003228#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01003229do { \
3230 if (a##_fls > b##_fls) { \
3231 a >>= 1; \
3232 a##_fls--; \
3233 } else { \
3234 b >>= 1; \
3235 b##_fls--; \
3236 } \
3237} while (0)
3238
3239 /*
3240 * Reduce accuracy until either term fits in a u64, then proceed with
3241 * the other, so that finally we can do a u64/u64 division.
3242 */
3243 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3244 REDUCE_FLS(nsec, frequency);
3245 REDUCE_FLS(sec, count);
3246 }
3247
3248 if (count_fls + sec_fls > 64) {
3249 divisor = nsec * frequency;
3250
3251 while (count_fls + sec_fls > 64) {
3252 REDUCE_FLS(count, sec);
3253 divisor >>= 1;
3254 }
3255
3256 dividend = count * sec;
3257 } else {
3258 dividend = count * sec;
3259
3260 while (nsec_fls + frequency_fls > 64) {
3261 REDUCE_FLS(nsec, frequency);
3262 dividend >>= 1;
3263 }
3264
3265 divisor = nsec * frequency;
3266 }
3267
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003268 if (!divisor)
3269 return dividend;
3270
Peter Zijlstraabd50712010-01-26 18:50:16 +01003271 return div64_u64(dividend, divisor);
3272}
3273
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003274static DEFINE_PER_CPU(int, perf_throttled_count);
3275static DEFINE_PER_CPU(u64, perf_throttled_seq);
3276
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003277static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003278{
3279 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003280 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003281 s64 delta;
3282
Peter Zijlstraabd50712010-01-26 18:50:16 +01003283 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003284
3285 delta = (s64)(period - hwc->sample_period);
3286 delta = (delta + 7) / 8; /* low pass filter */
3287
3288 sample_period = hwc->sample_period + delta;
3289
3290 if (!sample_period)
3291 sample_period = 1;
3292
3293 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003294
Peter Zijlstrae7850592010-05-21 14:43:08 +02003295 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003296 if (disable)
3297 event->pmu->stop(event, PERF_EF_UPDATE);
3298
Peter Zijlstrae7850592010-05-21 14:43:08 +02003299 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003300
3301 if (disable)
3302 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003303 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003304}
3305
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003306/*
3307 * combine freq adjustment with unthrottling to avoid two passes over the
3308 * events. At the same time, make sure, having freq events does not change
3309 * the rate of unthrottling as that would introduce bias.
3310 */
3311static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3312 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003313{
3314 struct perf_event *event;
3315 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003316 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003317 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003318
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003319 /*
3320 * only need to iterate over all events iff:
3321 * - context have events in frequency mode (needs freq adjust)
3322 * - there are events to unthrottle on this cpu
3323 */
3324 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003325 return;
3326
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003327 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003328 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003329
Paul Mackerras03541f82009-10-14 16:58:03 +11003330 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003331 if (event->state != PERF_EVENT_STATE_ACTIVE)
3332 continue;
3333
Stephane Eranian5632ab12011-01-03 18:20:01 +02003334 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003335 continue;
3336
Alexander Shishkin44377272013-12-16 14:17:36 +02003337 perf_pmu_disable(event->pmu);
3338
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003339 hwc = &event->hw;
3340
Jiri Olsaae23bff2013-08-24 16:45:54 +02003341 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003342 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003343 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003344 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003345 }
3346
3347 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003348 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003349
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003350 /*
3351 * stop the event and update event->count
3352 */
3353 event->pmu->stop(event, PERF_EF_UPDATE);
3354
Peter Zijlstrae7850592010-05-21 14:43:08 +02003355 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003356 delta = now - hwc->freq_count_stamp;
3357 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003358
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003359 /*
3360 * restart the event
3361 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003362 * we have stopped the event so tell that
3363 * to perf_adjust_period() to avoid stopping it
3364 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003365 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003366 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003367 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003368
3369 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003370 next:
3371 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003372 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003373
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003374 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003375 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003376}
3377
3378/*
3379 * Round-robin a context's events:
3380 */
3381static void rotate_ctx(struct perf_event_context *ctx)
3382{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003383 /*
3384 * Rotate the first entry last of non-pinned groups. Rotation might be
3385 * disabled by the inheritance code.
3386 */
3387 if (!ctx->rotate_disable)
3388 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003389}
3390
Stephane Eranian9e630202013-04-03 14:21:33 +02003391static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003392{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003393 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003394 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003395
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003396 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003397 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3398 rotate = 1;
3399 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003400
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003401 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003402 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003403 if (ctx->nr_events != ctx->nr_active)
3404 rotate = 1;
3405 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003406
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003407 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003408 goto done;
3409
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003410 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003411 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003412
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003413 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3414 if (ctx)
3415 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003416
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003417 rotate_ctx(&cpuctx->ctx);
3418 if (ctx)
3419 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003420
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003421 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003422
3423 perf_pmu_enable(cpuctx->ctx.pmu);
3424 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003425done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003426
3427 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003428}
3429
3430void perf_event_task_tick(void)
3431{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003432 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3433 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003434 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003435
3436 WARN_ON(!irqs_disabled());
3437
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003438 __this_cpu_inc(perf_throttled_seq);
3439 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003440 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003441
Mark Rutland2fde4f92015-01-07 15:01:54 +00003442 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003443 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003444}
3445
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003446static int event_enable_on_exec(struct perf_event *event,
3447 struct perf_event_context *ctx)
3448{
3449 if (!event->attr.enable_on_exec)
3450 return 0;
3451
3452 event->attr.enable_on_exec = 0;
3453 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3454 return 0;
3455
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003456 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003457
3458 return 1;
3459}
3460
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003461/*
3462 * Enable all of a task's events that have been marked enable-on-exec.
3463 * This expects task == current.
3464 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003465static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003466{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003467 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003468 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003469 struct perf_event *event;
3470 unsigned long flags;
3471 int enabled = 0;
3472
3473 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003474 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003475 if (!ctx || !ctx->nr_events)
3476 goto out;
3477
Peter Zijlstra3e349502016-01-08 10:01:18 +01003478 cpuctx = __get_cpu_context(ctx);
3479 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01003480 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003481 list_for_each_entry(event, &ctx->event_list, event_entry)
3482 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003483
3484 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003485 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003486 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003487 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003488 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003489 ctx_resched(cpuctx, ctx);
3490 }
3491 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003492
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003493out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003494 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003495
3496 if (clone_ctx)
3497 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003498}
3499
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003500struct perf_read_data {
3501 struct perf_event *event;
3502 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003503 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003504};
3505
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003506static int __perf_event_read_cpu(struct perf_event *event, int event_cpu)
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003507{
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003508 u16 local_pkg, event_pkg;
3509
3510 if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) {
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003511 int local_cpu = smp_processor_id();
3512
3513 event_pkg = topology_physical_package_id(event_cpu);
3514 local_pkg = topology_physical_package_id(local_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003515
3516 if (event_pkg == local_pkg)
3517 return local_cpu;
3518 }
3519
3520 return event_cpu;
3521}
3522
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003523/*
3524 * Cross CPU call to read the hardware event
3525 */
3526static void __perf_event_read(void *info)
3527{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003528 struct perf_read_data *data = info;
3529 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003530 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003531 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003532 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003533
3534 /*
3535 * If this is a task context, we need to check whether it is
3536 * the current task context of this cpu. If not it has been
3537 * scheduled out before the smp call arrived. In that case
3538 * event->count would have been updated to a recent sample
3539 * when the event was scheduled out.
3540 */
3541 if (ctx->task && cpuctx->task_ctx != ctx)
3542 return;
3543
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003544 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003545 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003546 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003547 update_cgrp_time_from_event(event);
3548 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003549
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003550 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003551 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003552 goto unlock;
3553
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003554 if (!data->group) {
3555 pmu->read(event);
3556 data->ret = 0;
3557 goto unlock;
3558 }
3559
3560 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3561
3562 pmu->read(event);
3563
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003564 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3565 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003566 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3567 /*
3568 * Use sibling's PMU rather than @event's since
3569 * sibling could be on different (eg: software) PMU.
3570 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003571 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003572 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003573 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003574
3575 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003576
3577unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003578 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003579}
3580
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003581static inline u64 perf_event_count(struct perf_event *event)
3582{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003583 if (event->pmu->count)
3584 return event->pmu->count(event);
3585
3586 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003587}
3588
Kaixu Xiaffe86902015-08-06 07:02:32 +00003589/*
3590 * NMI-safe method to read a local event, that is an event that
3591 * is:
3592 * - either for the current task, or for this CPU
3593 * - does not have inherit set, for inherited task events
3594 * will not be local and we cannot read them atomically
3595 * - must not have a pmu::count method
3596 */
3597u64 perf_event_read_local(struct perf_event *event)
3598{
3599 unsigned long flags;
3600 u64 val;
3601
3602 /*
3603 * Disabling interrupts avoids all counter scheduling (context
3604 * switches, timer based rotation and IPIs).
3605 */
3606 local_irq_save(flags);
3607
3608 /* If this is a per-task event, it must be for current */
3609 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3610 event->hw.target != current);
3611
3612 /* If this is a per-CPU event, it must be for this CPU */
3613 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3614 event->cpu != smp_processor_id());
3615
3616 /*
3617 * It must not be an event with inherit set, we cannot read
3618 * all child counters from atomic context.
3619 */
3620 WARN_ON_ONCE(event->attr.inherit);
3621
3622 /*
3623 * It must not have a pmu::count method, those are not
3624 * NMI safe.
3625 */
3626 WARN_ON_ONCE(event->pmu->count);
3627
3628 /*
3629 * If the event is currently on this CPU, its either a per-task event,
3630 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3631 * oncpu == -1).
3632 */
3633 if (event->oncpu == smp_processor_id())
3634 event->pmu->read(event);
3635
3636 val = local64_read(&event->count);
3637 local_irq_restore(flags);
3638
3639 return val;
3640}
3641
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003642static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003643{
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003644 int event_cpu, ret = 0;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003645
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003646 /*
3647 * If event is enabled and currently active on a CPU, update the
3648 * value in the event structure:
3649 */
3650 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003651 struct perf_read_data data = {
3652 .event = event,
3653 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003654 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003655 };
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003656
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003657 event_cpu = READ_ONCE(event->oncpu);
3658 if ((unsigned)event_cpu >= nr_cpu_ids)
3659 return 0;
3660
3661 preempt_disable();
3662 event_cpu = __perf_event_read_cpu(event, event_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003663
Peter Zijlstra58763142016-08-30 10:15:03 +02003664 /*
3665 * Purposely ignore the smp_call_function_single() return
3666 * value.
3667 *
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003668 * If event_cpu isn't a valid CPU it means the event got
Peter Zijlstra58763142016-08-30 10:15:03 +02003669 * scheduled out and that will have updated the event count.
3670 *
3671 * Therefore, either way, we'll have an up-to-date event count
3672 * after this.
3673 */
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003674 (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1);
3675 preempt_enable();
Peter Zijlstra58763142016-08-30 10:15:03 +02003676 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003677 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003678 struct perf_event_context *ctx = event->ctx;
3679 unsigned long flags;
3680
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003681 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003682 /*
3683 * may read while context is not active
3684 * (e.g., thread is blocked), in that case
3685 * we cannot update context time
3686 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003687 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003688 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003689 update_cgrp_time_from_event(event);
3690 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003691 if (group)
3692 update_group_times(event);
3693 else
3694 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003695 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003696 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003697
3698 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003699}
3700
3701/*
3702 * Initialize the perf_event context in a task_struct:
3703 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003704static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003705{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003706 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003707 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003708 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003709 INIT_LIST_HEAD(&ctx->pinned_groups);
3710 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003711 INIT_LIST_HEAD(&ctx->event_list);
3712 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003713}
3714
Peter Zijlstraeb184472010-09-07 15:55:13 +02003715static struct perf_event_context *
3716alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003717{
3718 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003719
3720 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3721 if (!ctx)
3722 return NULL;
3723
3724 __perf_event_init_context(ctx);
3725 if (task) {
3726 ctx->task = task;
3727 get_task_struct(task);
3728 }
3729 ctx->pmu = pmu;
3730
3731 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003732}
3733
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003734static struct task_struct *
3735find_lively_task_by_vpid(pid_t vpid)
3736{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003737 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003738
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003739 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003740 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003741 task = current;
3742 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003743 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003744 if (task)
3745 get_task_struct(task);
3746 rcu_read_unlock();
3747
3748 if (!task)
3749 return ERR_PTR(-ESRCH);
3750
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003751 return task;
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003752}
3753
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003754/*
3755 * Returns a matching context with refcount and pincount.
3756 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003757static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003758find_get_context(struct pmu *pmu, struct task_struct *task,
3759 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003760{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003761 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003762 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003763 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003764 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003765 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003766 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003767
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003768 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003769 /* Must be root to operate on a CPU event: */
3770 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3771 return ERR_PTR(-EACCES);
3772
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003773 /*
3774 * We could be clever and allow to attach a event to an
3775 * offline CPU and activate it when the CPU comes up, but
3776 * that's for later.
3777 */
3778 if (!cpu_online(cpu))
3779 return ERR_PTR(-ENODEV);
3780
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003781 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003782 ctx = &cpuctx->ctx;
3783 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003784 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003785
3786 return ctx;
3787 }
3788
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003789 err = -EINVAL;
3790 ctxn = pmu->task_ctx_nr;
3791 if (ctxn < 0)
3792 goto errout;
3793
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003794 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3795 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3796 if (!task_ctx_data) {
3797 err = -ENOMEM;
3798 goto errout;
3799 }
3800 }
3801
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003802retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003803 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003804 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003805 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003806 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003807
3808 if (task_ctx_data && !ctx->task_ctx_data) {
3809 ctx->task_ctx_data = task_ctx_data;
3810 task_ctx_data = NULL;
3811 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003812 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003813
3814 if (clone_ctx)
3815 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003816 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003817 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003818 err = -ENOMEM;
3819 if (!ctx)
3820 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003821
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003822 if (task_ctx_data) {
3823 ctx->task_ctx_data = task_ctx_data;
3824 task_ctx_data = NULL;
3825 }
3826
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003827 err = 0;
3828 mutex_lock(&task->perf_event_mutex);
3829 /*
3830 * If it has already passed perf_event_exit_task().
3831 * we must see PF_EXITING, it takes this mutex too.
3832 */
3833 if (task->flags & PF_EXITING)
3834 err = -ESRCH;
3835 else if (task->perf_event_ctxp[ctxn])
3836 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003837 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003838 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003839 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003840 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003841 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003842 mutex_unlock(&task->perf_event_mutex);
3843
3844 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003845 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003846
3847 if (err == -EAGAIN)
3848 goto retry;
3849 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003850 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003851 }
3852
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003853 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003854 return ctx;
3855
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003856errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003857 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003858 return ERR_PTR(err);
3859}
3860
Li Zefan6fb29152009-10-15 11:21:42 +08003861static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003862static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003863
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003864static void free_event_rcu(struct rcu_head *head)
3865{
3866 struct perf_event *event;
3867
3868 event = container_of(head, struct perf_event, rcu_head);
3869 if (event->ns)
3870 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003871 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003872 kfree(event);
3873}
3874
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003875static void ring_buffer_attach(struct perf_event *event,
3876 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003877
Kan Liangf2fb6be2016-03-23 11:24:37 -07003878static void detach_sb_event(struct perf_event *event)
3879{
3880 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
3881
3882 raw_spin_lock(&pel->lock);
3883 list_del_rcu(&event->sb_list);
3884 raw_spin_unlock(&pel->lock);
3885}
3886
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003887static bool is_sb_event(struct perf_event *event)
Kan Liangf2fb6be2016-03-23 11:24:37 -07003888{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003889 struct perf_event_attr *attr = &event->attr;
3890
Kan Liangf2fb6be2016-03-23 11:24:37 -07003891 if (event->parent)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003892 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003893
3894 if (event->attach_state & PERF_ATTACH_TASK)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003895 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003896
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003897 if (attr->mmap || attr->mmap_data || attr->mmap2 ||
3898 attr->comm || attr->comm_exec ||
3899 attr->task ||
3900 attr->context_switch)
3901 return true;
3902 return false;
3903}
3904
3905static void unaccount_pmu_sb_event(struct perf_event *event)
3906{
3907 if (is_sb_event(event))
3908 detach_sb_event(event);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003909}
3910
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003911static void unaccount_event_cpu(struct perf_event *event, int cpu)
3912{
3913 if (event->parent)
3914 return;
3915
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003916 if (is_cgroup_event(event))
3917 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3918}
3919
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003920#ifdef CONFIG_NO_HZ_FULL
3921static DEFINE_SPINLOCK(nr_freq_lock);
3922#endif
3923
3924static void unaccount_freq_event_nohz(void)
3925{
3926#ifdef CONFIG_NO_HZ_FULL
3927 spin_lock(&nr_freq_lock);
3928 if (atomic_dec_and_test(&nr_freq_events))
3929 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
3930 spin_unlock(&nr_freq_lock);
3931#endif
3932}
3933
3934static void unaccount_freq_event(void)
3935{
3936 if (tick_nohz_full_enabled())
3937 unaccount_freq_event_nohz();
3938 else
3939 atomic_dec(&nr_freq_events);
3940}
3941
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003942static void unaccount_event(struct perf_event *event)
3943{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003944 bool dec = false;
3945
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003946 if (event->parent)
3947 return;
3948
3949 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003950 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003951 if (event->attr.mmap || event->attr.mmap_data)
3952 atomic_dec(&nr_mmap_events);
3953 if (event->attr.comm)
3954 atomic_dec(&nr_comm_events);
3955 if (event->attr.task)
3956 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003957 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003958 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03003959 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003960 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003961 atomic_dec(&nr_switch_events);
3962 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003963 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003964 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003965 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003966 dec = true;
3967
Peter Zijlstra9107c892016-02-24 18:45:45 +01003968 if (dec) {
3969 if (!atomic_add_unless(&perf_sched_count, -1, 1))
3970 schedule_delayed_work(&perf_sched_work, HZ);
3971 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003972
3973 unaccount_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003974
3975 unaccount_pmu_sb_event(event);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003976}
3977
Peter Zijlstra9107c892016-02-24 18:45:45 +01003978static void perf_sched_delayed(struct work_struct *work)
3979{
3980 mutex_lock(&perf_sched_mutex);
3981 if (atomic_dec_and_test(&perf_sched_count))
3982 static_branch_disable(&perf_sched_events);
3983 mutex_unlock(&perf_sched_mutex);
3984}
3985
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003986/*
3987 * The following implement mutual exclusion of events on "exclusive" pmus
3988 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3989 * at a time, so we disallow creating events that might conflict, namely:
3990 *
3991 * 1) cpu-wide events in the presence of per-task events,
3992 * 2) per-task events in the presence of cpu-wide events,
3993 * 3) two matching events on the same context.
3994 *
3995 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003996 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003997 */
3998static int exclusive_event_init(struct perf_event *event)
3999{
4000 struct pmu *pmu = event->pmu;
4001
4002 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
4003 return 0;
4004
4005 /*
4006 * Prevent co-existence of per-task and cpu-wide events on the
4007 * same exclusive pmu.
4008 *
4009 * Negative pmu::exclusive_cnt means there are cpu-wide
4010 * events on this "exclusive" pmu, positive means there are
4011 * per-task events.
4012 *
4013 * Since this is called in perf_event_alloc() path, event::ctx
4014 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
4015 * to mean "per-task event", because unlike other attach states it
4016 * never gets cleared.
4017 */
4018 if (event->attach_state & PERF_ATTACH_TASK) {
4019 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
4020 return -EBUSY;
4021 } else {
4022 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
4023 return -EBUSY;
4024 }
4025
4026 return 0;
4027}
4028
4029static void exclusive_event_destroy(struct perf_event *event)
4030{
4031 struct pmu *pmu = event->pmu;
4032
4033 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
4034 return;
4035
4036 /* see comment in exclusive_event_init() */
4037 if (event->attach_state & PERF_ATTACH_TASK)
4038 atomic_dec(&pmu->exclusive_cnt);
4039 else
4040 atomic_inc(&pmu->exclusive_cnt);
4041}
4042
4043static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
4044{
Alexander Shishkin3bf62152016-09-20 18:48:11 +03004045 if ((e1->pmu == e2->pmu) &&
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004046 (e1->cpu == e2->cpu ||
4047 e1->cpu == -1 ||
4048 e2->cpu == -1))
4049 return true;
4050 return false;
4051}
4052
4053/* Called under the same ctx::mutex as perf_install_in_context() */
4054static bool exclusive_event_installable(struct perf_event *event,
4055 struct perf_event_context *ctx)
4056{
4057 struct perf_event *iter_event;
4058 struct pmu *pmu = event->pmu;
4059
4060 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
4061 return true;
4062
4063 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
4064 if (exclusive_event_match(iter_event, event))
4065 return false;
4066 }
4067
4068 return true;
4069}
4070
Alexander Shishkin375637b2016-04-27 18:44:46 +03004071static void perf_addr_filters_splice(struct perf_event *event,
4072 struct list_head *head);
4073
Peter Zijlstra683ede42014-05-05 12:11:24 +02004074static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004075{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004076 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004077
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004078 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004079
Frederic Weisbecker76369132011-05-19 19:55:04 +02004080 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004081 /*
4082 * Can happen when we close an event with re-directed output.
4083 *
4084 * Since we have a 0 refcount, perf_mmap_close() will skip
4085 * over us; possibly making our ring_buffer_put() the last.
4086 */
4087 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004088 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004089 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004090 }
4091
Stephane Eraniane5d13672011-02-14 11:20:01 +02004092 if (is_cgroup_event(event))
4093 perf_detach_cgroup(event);
4094
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004095 if (!event->parent) {
4096 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
4097 put_callchain_buffers();
4098 }
4099
4100 perf_event_free_bpf_prog(event);
Alexander Shishkin375637b2016-04-27 18:44:46 +03004101 perf_addr_filters_splice(event, NULL);
4102 kfree(event->addr_filters_offs);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004103
4104 if (event->destroy)
4105 event->destroy(event);
4106
4107 if (event->ctx)
4108 put_ctx(event->ctx);
4109
Prashant Bholeb951ffb2018-04-09 19:03:46 +09004110 if (event->hw.target)
4111 put_task_struct(event->hw.target);
4112
Alexander Shishkin62a92c82016-06-07 15:44:15 +03004113 exclusive_event_destroy(event);
4114 module_put(event->pmu->module);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004115
4116 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004117}
4118
Peter Zijlstra683ede42014-05-05 12:11:24 +02004119/*
4120 * Used to free events which have a known refcount of 1, such as in error paths
4121 * where the event isn't exposed yet and inherited events.
4122 */
4123static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004124{
Peter Zijlstra683ede42014-05-05 12:11:24 +02004125 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
4126 "unexpected event refcount: %ld; ptr=%p\n",
4127 atomic_long_read(&event->refcount), event)) {
4128 /* leak to avoid use-after-free */
4129 return;
4130 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004131
Peter Zijlstra683ede42014-05-05 12:11:24 +02004132 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004133}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004134
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004135/*
Jiri Olsaf8697762014-08-01 14:33:01 +02004136 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004137 */
Jiri Olsaf8697762014-08-01 14:33:01 +02004138static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004139{
Peter Zijlstra88821352010-11-09 19:01:43 +01004140 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004141
Peter Zijlstra88821352010-11-09 19:01:43 +01004142 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01004143 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004144 * Matches the smp_store_release() in perf_event_exit_task(). If we
4145 * observe !owner it means the list deletion is complete and we can
4146 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01004147 * owner->perf_event_mutex.
4148 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004149 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01004150 if (owner) {
4151 /*
4152 * Since delayed_put_task_struct() also drops the last
4153 * task reference we can safely take a new reference
4154 * while holding the rcu_read_lock().
4155 */
4156 get_task_struct(owner);
4157 }
4158 rcu_read_unlock();
4159
4160 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004161 /*
4162 * If we're here through perf_event_exit_task() we're already
4163 * holding ctx->mutex which would be an inversion wrt. the
4164 * normal lock order.
4165 *
4166 * However we can safely take this lock because its the child
4167 * ctx->mutex.
4168 */
4169 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
4170
Peter Zijlstra88821352010-11-09 19:01:43 +01004171 /*
4172 * We have to re-check the event->owner field, if it is cleared
4173 * we raced with perf_event_exit_task(), acquiring the mutex
4174 * ensured they're done, and we can proceed with freeing the
4175 * event.
4176 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004177 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01004178 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004179 smp_store_release(&event->owner, NULL);
4180 }
Peter Zijlstra88821352010-11-09 19:01:43 +01004181 mutex_unlock(&owner->perf_event_mutex);
4182 put_task_struct(owner);
4183 }
Jiri Olsaf8697762014-08-01 14:33:01 +02004184}
4185
Jiri Olsaf8697762014-08-01 14:33:01 +02004186static void put_event(struct perf_event *event)
4187{
Jiri Olsaf8697762014-08-01 14:33:01 +02004188 if (!atomic_long_dec_and_test(&event->refcount))
4189 return;
4190
Peter Zijlstra683ede42014-05-05 12:11:24 +02004191 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01004192}
4193
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004194/*
4195 * Kill an event dead; while event:refcount will preserve the event
4196 * object, it will not preserve its functionality. Once the last 'user'
4197 * gives up the object, we'll destroy the thing.
4198 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004199int perf_event_release_kernel(struct perf_event *event)
4200{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004201 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004202 struct perf_event *child, *tmp;
4203
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004204 /*
4205 * If we got here through err_file: fput(event_file); we will not have
4206 * attached to a context yet.
4207 */
4208 if (!ctx) {
4209 WARN_ON_ONCE(event->attach_state &
4210 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
4211 goto no_ctx;
4212 }
4213
Peter Zijlstra88821352010-11-09 19:01:43 +01004214 if (!is_kernel_event(event))
4215 perf_remove_from_owner(event);
4216
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01004217 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02004218 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004219 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01004220
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004221 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004222 /*
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004223 * Mark this even as STATE_DEAD, there is no external reference to it
4224 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004225 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004226 * Anybody acquiring event->child_mutex after the below loop _must_
4227 * also see this, most importantly inherit_event() which will avoid
4228 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004229 *
4230 * Thus this guarantees that we will in fact observe and kill _ALL_
4231 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01004232 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004233 event->state = PERF_EVENT_STATE_DEAD;
4234 raw_spin_unlock_irq(&ctx->lock);
4235
4236 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004237
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004238again:
4239 mutex_lock(&event->child_mutex);
4240 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01004241
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004242 /*
4243 * Cannot change, child events are not migrated, see the
4244 * comment with perf_event_ctx_lock_nested().
4245 */
4246 ctx = lockless_dereference(child->ctx);
4247 /*
4248 * Since child_mutex nests inside ctx::mutex, we must jump
4249 * through hoops. We start by grabbing a reference on the ctx.
4250 *
4251 * Since the event cannot get freed while we hold the
4252 * child_mutex, the context must also exist and have a !0
4253 * reference count.
4254 */
4255 get_ctx(ctx);
4256
4257 /*
4258 * Now that we have a ctx ref, we can drop child_mutex, and
4259 * acquire ctx::mutex without fear of it going away. Then we
4260 * can re-acquire child_mutex.
4261 */
4262 mutex_unlock(&event->child_mutex);
4263 mutex_lock(&ctx->mutex);
4264 mutex_lock(&event->child_mutex);
4265
4266 /*
4267 * Now that we hold ctx::mutex and child_mutex, revalidate our
4268 * state, if child is still the first entry, it didn't get freed
4269 * and we can continue doing so.
4270 */
4271 tmp = list_first_entry_or_null(&event->child_list,
4272 struct perf_event, child_list);
4273 if (tmp == child) {
4274 perf_remove_from_context(child, DETACH_GROUP);
4275 list_del(&child->child_list);
4276 free_event(child);
4277 /*
4278 * This matches the refcount bump in inherit_event();
4279 * this can't be the last reference.
4280 */
4281 put_event(event);
4282 }
4283
4284 mutex_unlock(&event->child_mutex);
4285 mutex_unlock(&ctx->mutex);
4286 put_ctx(ctx);
4287 goto again;
4288 }
4289 mutex_unlock(&event->child_mutex);
4290
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004291no_ctx:
4292 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004293 return 0;
4294}
4295EXPORT_SYMBOL_GPL(perf_event_release_kernel);
4296
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02004297/*
4298 * Called when the last reference to the file is gone.
4299 */
Al Viroa6fa9412012-08-20 14:59:25 +01004300static int perf_release(struct inode *inode, struct file *file)
4301{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004302 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01004303 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004304}
4305
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004306u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004307{
4308 struct perf_event *child;
4309 u64 total = 0;
4310
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004311 *enabled = 0;
4312 *running = 0;
4313
Peter Zijlstra6f105812009-11-20 22:19:56 +01004314 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004315
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004316 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004317 total += perf_event_count(event);
4318
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004319 *enabled += event->total_time_enabled +
4320 atomic64_read(&event->child_total_time_enabled);
4321 *running += event->total_time_running +
4322 atomic64_read(&event->child_total_time_running);
4323
4324 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004325 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004326 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004327 *enabled += child->total_time_enabled;
4328 *running += child->total_time_running;
4329 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01004330 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004331
4332 return total;
4333}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004334EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004335
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004336static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004337 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004338{
Jiri Olsa6279fc52017-07-20 16:14:55 +02004339 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004340 struct perf_event *sub;
Jiri Olsa6279fc52017-07-20 16:14:55 +02004341 unsigned long flags;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004342 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004343 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01004344
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004345 ret = perf_event_read(leader, true);
4346 if (ret)
4347 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004348
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004349 /*
4350 * Since we co-schedule groups, {enabled,running} times of siblings
4351 * will be identical to those of the leader, so we only publish one
4352 * set.
4353 */
4354 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4355 values[n++] += leader->total_time_enabled +
4356 atomic64_read(&leader->child_total_time_enabled);
4357 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004358
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004359 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4360 values[n++] += leader->total_time_running +
4361 atomic64_read(&leader->child_total_time_running);
4362 }
4363
4364 /*
4365 * Write {count,id} tuples for every sibling.
4366 */
4367 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004368 if (read_format & PERF_FORMAT_ID)
4369 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004370
Jiri Olsa6279fc52017-07-20 16:14:55 +02004371 raw_spin_lock_irqsave(&ctx->lock, flags);
4372
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004373 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004374 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004375 if (read_format & PERF_FORMAT_ID)
4376 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004377 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004378
Jiri Olsa6279fc52017-07-20 16:14:55 +02004379 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004380 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004381}
4382
4383static int perf_read_group(struct perf_event *event,
4384 u64 read_format, char __user *buf)
4385{
4386 struct perf_event *leader = event->group_leader, *child;
4387 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004388 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004389 u64 *values;
4390
4391 lockdep_assert_held(&ctx->mutex);
4392
4393 values = kzalloc(event->read_size, GFP_KERNEL);
4394 if (!values)
4395 return -ENOMEM;
4396
4397 values[0] = 1 + leader->nr_siblings;
4398
4399 /*
4400 * By locking the child_mutex of the leader we effectively
4401 * lock the child list of all siblings.. XXX explain how.
4402 */
4403 mutex_lock(&leader->child_mutex);
4404
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004405 ret = __perf_read_group_add(leader, read_format, values);
4406 if (ret)
4407 goto unlock;
4408
4409 list_for_each_entry(child, &leader->child_list, child_list) {
4410 ret = __perf_read_group_add(child, read_format, values);
4411 if (ret)
4412 goto unlock;
4413 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004414
4415 mutex_unlock(&leader->child_mutex);
4416
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004417 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004418 if (copy_to_user(buf, values, event->read_size))
4419 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004420 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004421
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004422unlock:
4423 mutex_unlock(&leader->child_mutex);
4424out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004425 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004426 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004427}
4428
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004429static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004430 u64 read_format, char __user *buf)
4431{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004432 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004433 u64 values[4];
4434 int n = 0;
4435
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004436 values[n++] = perf_event_read_value(event, &enabled, &running);
4437 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4438 values[n++] = enabled;
4439 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4440 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004441 if (read_format & PERF_FORMAT_ID)
4442 values[n++] = primary_event_id(event);
4443
4444 if (copy_to_user(buf, values, n * sizeof(u64)))
4445 return -EFAULT;
4446
4447 return n * sizeof(u64);
4448}
4449
Jiri Olsadc633982014-09-12 13:18:26 +02004450static bool is_event_hup(struct perf_event *event)
4451{
4452 bool no_children;
4453
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004454 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004455 return false;
4456
4457 mutex_lock(&event->child_mutex);
4458 no_children = list_empty(&event->child_list);
4459 mutex_unlock(&event->child_mutex);
4460 return no_children;
4461}
4462
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004463/*
4464 * Read the performance event - simple non blocking version for now
4465 */
4466static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004467__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004468{
4469 u64 read_format = event->attr.read_format;
4470 int ret;
4471
4472 /*
4473 * Return end-of-file for a read on a event that is in
4474 * error state (i.e. because it was pinned but it couldn't be
4475 * scheduled on to the CPU at some point).
4476 */
4477 if (event->state == PERF_EVENT_STATE_ERROR)
4478 return 0;
4479
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004480 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004481 return -ENOSPC;
4482
4483 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004484 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004485 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004486 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004487 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004488
4489 return ret;
4490}
4491
4492static ssize_t
4493perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4494{
4495 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004496 struct perf_event_context *ctx;
4497 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004498
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004499 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004500 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004501 perf_event_ctx_unlock(event, ctx);
4502
4503 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004504}
4505
4506static unsigned int perf_poll(struct file *file, poll_table *wait)
4507{
4508 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004509 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004510 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004511
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004512 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004513
Jiri Olsadc633982014-09-12 13:18:26 +02004514 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004515 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004516
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004517 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004518 * Pin the event->rb by taking event->mmap_mutex; otherwise
4519 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004520 */
4521 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004522 rb = event->rb;
4523 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004524 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004525 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004526 return events;
4527}
4528
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004529static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004530{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004531 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004532 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004533 perf_event_update_userpage(event);
4534}
4535
4536/*
4537 * Holding the top-level event's child_mutex means that any
4538 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004539 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004540 * task existence requirements of perf_event_enable/disable.
4541 */
4542static void perf_event_for_each_child(struct perf_event *event,
4543 void (*func)(struct perf_event *))
4544{
4545 struct perf_event *child;
4546
4547 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004548
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004549 mutex_lock(&event->child_mutex);
4550 func(event);
4551 list_for_each_entry(child, &event->child_list, child_list)
4552 func(child);
4553 mutex_unlock(&event->child_mutex);
4554}
4555
4556static void perf_event_for_each(struct perf_event *event,
4557 void (*func)(struct perf_event *))
4558{
4559 struct perf_event_context *ctx = event->ctx;
4560 struct perf_event *sibling;
4561
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004562 lockdep_assert_held(&ctx->mutex);
4563
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004564 event = event->group_leader;
4565
4566 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004567 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004568 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004569}
4570
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004571static void __perf_event_period(struct perf_event *event,
4572 struct perf_cpu_context *cpuctx,
4573 struct perf_event_context *ctx,
4574 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004575{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004576 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004577 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004578
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004579 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004580 event->attr.sample_freq = value;
4581 } else {
4582 event->attr.sample_period = value;
4583 event->hw.sample_period = value;
4584 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004585
4586 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4587 if (active) {
4588 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01004589 /*
4590 * We could be throttled; unthrottle now to avoid the tick
4591 * trying to unthrottle while we already re-started the event.
4592 */
4593 if (event->hw.interrupts == MAX_INTERRUPTS) {
4594 event->hw.interrupts = 0;
4595 perf_log_throttle(event, 1);
4596 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004597 event->pmu->stop(event, PERF_EF_UPDATE);
4598 }
4599
4600 local64_set(&event->hw.period_left, 0);
4601
4602 if (active) {
4603 event->pmu->start(event, PERF_EF_RELOAD);
4604 perf_pmu_enable(ctx->pmu);
4605 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004606}
4607
Jiri Olsae62e3b62019-02-04 13:35:32 +01004608static int perf_event_check_period(struct perf_event *event, u64 value)
4609{
4610 return event->pmu->check_period(event, value);
4611}
4612
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004613static int perf_event_period(struct perf_event *event, u64 __user *arg)
4614{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004615 u64 value;
4616
4617 if (!is_sampling_event(event))
4618 return -EINVAL;
4619
4620 if (copy_from_user(&value, arg, sizeof(value)))
4621 return -EFAULT;
4622
4623 if (!value)
4624 return -EINVAL;
4625
4626 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4627 return -EINVAL;
4628
Jiri Olsae62e3b62019-02-04 13:35:32 +01004629 if (perf_event_check_period(event, value))
4630 return -EINVAL;
4631
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004632 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004633
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004634 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004635}
4636
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004637static const struct file_operations perf_fops;
4638
Al Viro2903ff02012-08-28 12:52:22 -04004639static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004640{
Al Viro2903ff02012-08-28 12:52:22 -04004641 struct fd f = fdget(fd);
4642 if (!f.file)
4643 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004644
Al Viro2903ff02012-08-28 12:52:22 -04004645 if (f.file->f_op != &perf_fops) {
4646 fdput(f);
4647 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004648 }
Al Viro2903ff02012-08-28 12:52:22 -04004649 *p = f;
4650 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004651}
4652
4653static int perf_event_set_output(struct perf_event *event,
4654 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004655static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004656static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004657
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004658static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004659{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004660 void (*func)(struct perf_event *);
4661 u32 flags = arg;
4662
4663 switch (cmd) {
4664 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004665 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004666 break;
4667 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004668 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004669 break;
4670 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004671 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004672 break;
4673
4674 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004675 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004676
4677 case PERF_EVENT_IOC_PERIOD:
4678 return perf_event_period(event, (u64 __user *)arg);
4679
Jiri Olsacf4957f2012-10-24 13:37:58 +02004680 case PERF_EVENT_IOC_ID:
4681 {
4682 u64 id = primary_event_id(event);
4683
4684 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4685 return -EFAULT;
4686 return 0;
4687 }
4688
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004689 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004690 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004691 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004692 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004693 struct perf_event *output_event;
4694 struct fd output;
4695 ret = perf_fget_light(arg, &output);
4696 if (ret)
4697 return ret;
4698 output_event = output.file->private_data;
4699 ret = perf_event_set_output(event, output_event);
4700 fdput(output);
4701 } else {
4702 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004703 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004704 return ret;
4705 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004706
Li Zefan6fb29152009-10-15 11:21:42 +08004707 case PERF_EVENT_IOC_SET_FILTER:
4708 return perf_event_set_filter(event, (void __user *)arg);
4709
Alexei Starovoitov25415172015-03-25 12:49:20 -07004710 case PERF_EVENT_IOC_SET_BPF:
4711 return perf_event_set_bpf_prog(event, arg);
4712
Wang Nan86e79722016-03-28 06:41:29 +00004713 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
4714 struct ring_buffer *rb;
4715
4716 rcu_read_lock();
4717 rb = rcu_dereference(event->rb);
4718 if (!rb || !rb->nr_pages) {
4719 rcu_read_unlock();
4720 return -EINVAL;
4721 }
4722 rb_toggle_paused(rb, !!arg);
4723 rcu_read_unlock();
4724 return 0;
4725 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004726 default:
4727 return -ENOTTY;
4728 }
4729
4730 if (flags & PERF_IOC_FLAG_GROUP)
4731 perf_event_for_each(event, func);
4732 else
4733 perf_event_for_each_child(event, func);
4734
4735 return 0;
4736}
4737
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004738static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4739{
4740 struct perf_event *event = file->private_data;
4741 struct perf_event_context *ctx;
4742 long ret;
4743
4744 ctx = perf_event_ctx_lock(event);
4745 ret = _perf_ioctl(event, cmd, arg);
4746 perf_event_ctx_unlock(event, ctx);
4747
4748 return ret;
4749}
4750
Pawel Mollb3f20782014-06-13 16:03:32 +01004751#ifdef CONFIG_COMPAT
4752static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4753 unsigned long arg)
4754{
4755 switch (_IOC_NR(cmd)) {
4756 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4757 case _IOC_NR(PERF_EVENT_IOC_ID):
4758 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4759 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4760 cmd &= ~IOCSIZE_MASK;
4761 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4762 }
4763 break;
4764 }
4765 return perf_ioctl(file, cmd, arg);
4766}
4767#else
4768# define perf_compat_ioctl NULL
4769#endif
4770
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004771int perf_event_task_enable(void)
4772{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004773 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004774 struct perf_event *event;
4775
4776 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004777 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4778 ctx = perf_event_ctx_lock(event);
4779 perf_event_for_each_child(event, _perf_event_enable);
4780 perf_event_ctx_unlock(event, ctx);
4781 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004782 mutex_unlock(&current->perf_event_mutex);
4783
4784 return 0;
4785}
4786
4787int perf_event_task_disable(void)
4788{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004789 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004790 struct perf_event *event;
4791
4792 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004793 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4794 ctx = perf_event_ctx_lock(event);
4795 perf_event_for_each_child(event, _perf_event_disable);
4796 perf_event_ctx_unlock(event, ctx);
4797 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004798 mutex_unlock(&current->perf_event_mutex);
4799
4800 return 0;
4801}
4802
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004803static int perf_event_index(struct perf_event *event)
4804{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004805 if (event->hw.state & PERF_HES_STOPPED)
4806 return 0;
4807
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004808 if (event->state != PERF_EVENT_STATE_ACTIVE)
4809 return 0;
4810
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004811 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004812}
4813
Eric B Munsonc4794292011-06-23 16:34:38 -04004814static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004815 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004816 u64 *enabled,
4817 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004818{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004819 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004820
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004821 *now = perf_clock();
4822 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004823 *enabled = ctx_time - event->tstamp_enabled;
4824 *running = ctx_time - event->tstamp_running;
4825}
4826
Peter Zijlstrafa731582013-09-19 10:16:42 +02004827static void perf_event_init_userpage(struct perf_event *event)
4828{
4829 struct perf_event_mmap_page *userpg;
4830 struct ring_buffer *rb;
4831
4832 rcu_read_lock();
4833 rb = rcu_dereference(event->rb);
4834 if (!rb)
4835 goto unlock;
4836
4837 userpg = rb->user_page;
4838
4839 /* Allow new userspace to detect that bit 0 is deprecated */
4840 userpg->cap_bit0_is_deprecated = 1;
4841 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004842 userpg->data_offset = PAGE_SIZE;
4843 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004844
4845unlock:
4846 rcu_read_unlock();
4847}
4848
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004849void __weak arch_perf_update_userpage(
4850 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004851{
4852}
4853
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004854/*
4855 * Callers need to ensure there can be no nesting of this function, otherwise
4856 * the seqlock logic goes bad. We can not serialize this because the arch
4857 * code calls this from NMI context.
4858 */
4859void perf_event_update_userpage(struct perf_event *event)
4860{
4861 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004862 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004863 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004864
4865 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004866 rb = rcu_dereference(event->rb);
4867 if (!rb)
4868 goto unlock;
4869
Eric B Munson0d641202011-06-24 12:26:26 -04004870 /*
4871 * compute total_time_enabled, total_time_running
4872 * based on snapshot values taken when the event
4873 * was last scheduled in.
4874 *
4875 * we cannot simply called update_context_time()
4876 * because of locking issue as we can be called in
4877 * NMI context
4878 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004879 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004880
Frederic Weisbecker76369132011-05-19 19:55:04 +02004881 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004882 /*
4883 * Disable preemption so as to not let the corresponding user-space
4884 * spin too long if we get preempted.
4885 */
4886 preempt_disable();
4887 ++userpg->lock;
4888 barrier();
4889 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004890 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004891 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004892 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004893
Eric B Munson0d641202011-06-24 12:26:26 -04004894 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004895 atomic64_read(&event->child_total_time_enabled);
4896
Eric B Munson0d641202011-06-24 12:26:26 -04004897 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004898 atomic64_read(&event->child_total_time_running);
4899
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004900 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004901
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004902 barrier();
4903 ++userpg->lock;
4904 preempt_enable();
4905unlock:
4906 rcu_read_unlock();
4907}
4908
Peter Zijlstra906010b2009-09-21 16:08:49 +02004909static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4910{
4911 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004912 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004913 int ret = VM_FAULT_SIGBUS;
4914
4915 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4916 if (vmf->pgoff == 0)
4917 ret = 0;
4918 return ret;
4919 }
4920
4921 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004922 rb = rcu_dereference(event->rb);
4923 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004924 goto unlock;
4925
4926 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4927 goto unlock;
4928
Frederic Weisbecker76369132011-05-19 19:55:04 +02004929 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004930 if (!vmf->page)
4931 goto unlock;
4932
4933 get_page(vmf->page);
4934 vmf->page->mapping = vma->vm_file->f_mapping;
4935 vmf->page->index = vmf->pgoff;
4936
4937 ret = 0;
4938unlock:
4939 rcu_read_unlock();
4940
4941 return ret;
4942}
4943
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004944static void ring_buffer_attach(struct perf_event *event,
4945 struct ring_buffer *rb)
4946{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004947 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004948 unsigned long flags;
4949
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004950 if (event->rb) {
4951 /*
4952 * Should be impossible, we set this when removing
4953 * event->rb_entry and wait/clear when adding event->rb_entry.
4954 */
4955 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004956
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004957 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004958 spin_lock_irqsave(&old_rb->event_lock, flags);
4959 list_del_rcu(&event->rb_entry);
4960 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004961
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004962 event->rcu_batches = get_state_synchronize_rcu();
4963 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004964 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004965
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004966 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004967 if (event->rcu_pending) {
4968 cond_synchronize_rcu(event->rcu_batches);
4969 event->rcu_pending = 0;
4970 }
4971
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004972 spin_lock_irqsave(&rb->event_lock, flags);
4973 list_add_rcu(&event->rb_entry, &rb->event_list);
4974 spin_unlock_irqrestore(&rb->event_lock, flags);
4975 }
4976
Alexander Shishkin767ae082016-09-06 16:23:49 +03004977 /*
4978 * Avoid racing with perf_mmap_close(AUX): stop the event
4979 * before swizzling the event::rb pointer; if it's getting
4980 * unmapped, its aux_mmap_count will be 0 and it won't
4981 * restart. See the comment in __perf_pmu_output_stop().
4982 *
4983 * Data will inevitably be lost when set_output is done in
4984 * mid-air, but then again, whoever does it like this is
4985 * not in for the data anyway.
4986 */
4987 if (has_aux(event))
4988 perf_event_stop(event, 0);
4989
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004990 rcu_assign_pointer(event->rb, rb);
4991
4992 if (old_rb) {
4993 ring_buffer_put(old_rb);
4994 /*
4995 * Since we detached before setting the new rb, so that we
4996 * could attach the new rb, we could have missed a wakeup.
4997 * Provide it now.
4998 */
4999 wake_up_all(&event->waitq);
5000 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005001}
5002
5003static void ring_buffer_wakeup(struct perf_event *event)
5004{
5005 struct ring_buffer *rb;
5006
5007 rcu_read_lock();
5008 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005009 if (rb) {
5010 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
5011 wake_up_all(&event->waitq);
5012 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005013 rcu_read_unlock();
5014}
5015
Alexander Shishkinfdc26702015-01-14 14:18:16 +02005016struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005017{
Frederic Weisbecker76369132011-05-19 19:55:04 +02005018 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005019
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005020 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02005021 rb = rcu_dereference(event->rb);
5022 if (rb) {
5023 if (!atomic_inc_not_zero(&rb->refcount))
5024 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005025 }
5026 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005027
Frederic Weisbecker76369132011-05-19 19:55:04 +02005028 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005029}
5030
Alexander Shishkinfdc26702015-01-14 14:18:16 +02005031void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005032{
Frederic Weisbecker76369132011-05-19 19:55:04 +02005033 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005034 return;
5035
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005036 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005037
Frederic Weisbecker76369132011-05-19 19:55:04 +02005038 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005039}
5040
5041static void perf_mmap_open(struct vm_area_struct *vma)
5042{
5043 struct perf_event *event = vma->vm_file->private_data;
5044
5045 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005046 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005047
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005048 if (vma->vm_pgoff)
5049 atomic_inc(&event->rb->aux_mmap_count);
5050
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005051 if (event->pmu->event_mapped)
5052 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005053}
5054
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005055static void perf_pmu_output_stop(struct perf_event *event);
5056
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005057/*
5058 * A buffer can be mmap()ed multiple times; either directly through the same
5059 * event, or through other events by use of perf_event_set_output().
5060 *
5061 * In order to undo the VM accounting done by perf_mmap() we need to destroy
5062 * the buffer here, where we still have a VM context. This means we need
5063 * to detach all events redirecting to us.
5064 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005065static void perf_mmap_close(struct vm_area_struct *vma)
5066{
5067 struct perf_event *event = vma->vm_file->private_data;
5068
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005069 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005070 struct user_struct *mmap_user = rb->mmap_user;
5071 int mmap_locked = rb->mmap_locked;
5072 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005073
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005074 if (event->pmu->event_unmapped)
5075 event->pmu->event_unmapped(event);
5076
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005077 /*
5078 * rb->aux_mmap_count will always drop before rb->mmap_count and
5079 * event->mmap_count, so it is ok to use event->mmap_mutex to
5080 * serialize with perf_mmap here.
5081 */
5082 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
5083 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005084 /*
5085 * Stop all AUX events that are writing to this buffer,
5086 * so that we can free its AUX pages and corresponding PMU
5087 * data. Note that after rb::aux_mmap_count dropped to zero,
5088 * they won't start any more (see perf_aux_output_begin()).
5089 */
5090 perf_pmu_output_stop(event);
5091
5092 /* now it's safe to free the pages */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005093 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
5094 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
5095
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005096 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005097 rb_free_aux(rb);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005098 WARN_ON_ONCE(atomic_read(&rb->aux_refcount));
5099
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005100 mutex_unlock(&event->mmap_mutex);
5101 }
5102
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005103 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005104
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005105 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005106 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005107
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005108 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005109 mutex_unlock(&event->mmap_mutex);
5110
5111 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005112 if (atomic_read(&rb->mmap_count))
5113 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005114
5115 /*
5116 * No other mmap()s, detach from all other events that might redirect
5117 * into the now unreachable buffer. Somewhat complicated by the
5118 * fact that rb::event_lock otherwise nests inside mmap_mutex.
5119 */
5120again:
5121 rcu_read_lock();
5122 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
5123 if (!atomic_long_inc_not_zero(&event->refcount)) {
5124 /*
5125 * This event is en-route to free_event() which will
5126 * detach it and remove it from the list.
5127 */
5128 continue;
5129 }
5130 rcu_read_unlock();
5131
5132 mutex_lock(&event->mmap_mutex);
5133 /*
5134 * Check we didn't race with perf_event_set_output() which can
5135 * swizzle the rb from under us while we were waiting to
5136 * acquire mmap_mutex.
5137 *
5138 * If we find a different rb; ignore this event, a next
5139 * iteration will no longer find it on the list. We have to
5140 * still restart the iteration to make sure we're not now
5141 * iterating the wrong list.
5142 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005143 if (event->rb == rb)
5144 ring_buffer_attach(event, NULL);
5145
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005146 mutex_unlock(&event->mmap_mutex);
5147 put_event(event);
5148
5149 /*
5150 * Restart the iteration; either we're on the wrong list or
5151 * destroyed its integrity by doing a deletion.
5152 */
5153 goto again;
5154 }
5155 rcu_read_unlock();
5156
5157 /*
5158 * It could be there's still a few 0-ref events on the list; they'll
5159 * get cleaned up by free_event() -- they'll also still have their
5160 * ref on the rb and will free it whenever they are done with it.
5161 *
5162 * Aside from that, this buffer is 'fully' detached and unmapped,
5163 * undo the VM accounting.
5164 */
5165
5166 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
5167 vma->vm_mm->pinned_vm -= mmap_locked;
5168 free_uid(mmap_user);
5169
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005170out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005171 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005172}
5173
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04005174static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005175 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005176 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005177 .fault = perf_mmap_fault,
5178 .page_mkwrite = perf_mmap_fault,
5179};
5180
5181static int perf_mmap(struct file *file, struct vm_area_struct *vma)
5182{
5183 struct perf_event *event = file->private_data;
5184 unsigned long user_locked, user_lock_limit;
5185 struct user_struct *user = current_user();
5186 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005187 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005188 unsigned long vma_size;
5189 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005190 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005191 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005192
Peter Zijlstrac7920612010-05-18 10:33:24 +02005193 /*
5194 * Don't allow mmap() of inherited per-task counters. This would
5195 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02005196 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02005197 */
5198 if (event->cpu == -1 && event->attr.inherit)
5199 return -EINVAL;
5200
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005201 if (!(vma->vm_flags & VM_SHARED))
5202 return -EINVAL;
5203
5204 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005205
5206 if (vma->vm_pgoff == 0) {
5207 nr_pages = (vma_size / PAGE_SIZE) - 1;
5208 } else {
5209 /*
5210 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
5211 * mapped, all subsequent mappings should have the same size
5212 * and offset. Must be above the normal perf buffer.
5213 */
5214 u64 aux_offset, aux_size;
5215
5216 if (!event->rb)
5217 return -EINVAL;
5218
5219 nr_pages = vma_size / PAGE_SIZE;
5220
5221 mutex_lock(&event->mmap_mutex);
5222 ret = -EINVAL;
5223
5224 rb = event->rb;
5225 if (!rb)
5226 goto aux_unlock;
5227
5228 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
5229 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
5230
5231 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
5232 goto aux_unlock;
5233
5234 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
5235 goto aux_unlock;
5236
5237 /* already mapped with a different offset */
5238 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
5239 goto aux_unlock;
5240
5241 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
5242 goto aux_unlock;
5243
5244 /* already mapped with a different size */
5245 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
5246 goto aux_unlock;
5247
5248 if (!is_power_of_2(nr_pages))
5249 goto aux_unlock;
5250
5251 if (!atomic_inc_not_zero(&rb->mmap_count))
5252 goto aux_unlock;
5253
5254 if (rb_has_aux(rb)) {
5255 atomic_inc(&rb->aux_mmap_count);
5256 ret = 0;
5257 goto unlock;
5258 }
5259
5260 atomic_set(&rb->aux_mmap_count, 1);
5261 user_extra = nr_pages;
5262
5263 goto accounting;
5264 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005265
5266 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02005267 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005268 * can do bitmasks instead of modulo.
5269 */
Kan Liang2ed11312015-03-02 02:14:26 -05005270 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005271 return -EINVAL;
5272
5273 if (vma_size != PAGE_SIZE * (1 + nr_pages))
5274 return -EINVAL;
5275
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005276 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005277again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005278 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005279 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005280 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005281 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005282 goto unlock;
5283 }
5284
5285 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
5286 /*
5287 * Raced against perf_mmap_close() through
5288 * perf_event_set_output(). Try again, hope for better
5289 * luck.
5290 */
5291 mutex_unlock(&event->mmap_mutex);
5292 goto again;
5293 }
5294
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005295 goto unlock;
5296 }
5297
5298 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005299
5300accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005301 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
5302
5303 /*
5304 * Increase the limit linearly with more CPUs:
5305 */
5306 user_lock_limit *= num_online_cpus();
5307
5308 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
5309
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005310 if (user_locked > user_lock_limit)
5311 extra = user_locked - user_lock_limit;
5312
Jiri Slaby78d7d402010-03-05 13:42:54 -08005313 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005314 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07005315 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005316
5317 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
5318 !capable(CAP_IPC_LOCK)) {
5319 ret = -EPERM;
5320 goto unlock;
5321 }
5322
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005323 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005324
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005325 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005326 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005327
Frederic Weisbecker76369132011-05-19 19:55:04 +02005328 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005329 rb = rb_alloc(nr_pages,
5330 event->attr.watermark ? event->attr.wakeup_watermark : 0,
5331 event->cpu, flags);
5332
5333 if (!rb) {
5334 ret = -ENOMEM;
5335 goto unlock;
5336 }
5337
5338 atomic_set(&rb->mmap_count, 1);
5339 rb->mmap_user = get_current_user();
5340 rb->mmap_locked = extra;
5341
5342 ring_buffer_attach(event, rb);
5343
5344 perf_event_init_userpage(event);
5345 perf_event_update_userpage(event);
5346 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02005347 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
5348 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005349 if (!ret)
5350 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005351 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005352
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005353unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005354 if (!ret) {
5355 atomic_long_add(user_extra, &user->locked_vm);
5356 vma->vm_mm->pinned_vm += extra;
5357
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005358 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005359 } else if (rb) {
5360 atomic_dec(&rb->mmap_count);
5361 }
5362aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005363 mutex_unlock(&event->mmap_mutex);
5364
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005365 /*
5366 * Since pinned accounting is per vm we cannot allow fork() to copy our
5367 * vma.
5368 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005369 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005370 vma->vm_ops = &perf_mmap_vmops;
5371
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005372 if (event->pmu->event_mapped)
5373 event->pmu->event_mapped(event);
5374
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005375 return ret;
5376}
5377
5378static int perf_fasync(int fd, struct file *filp, int on)
5379{
Al Viro496ad9a2013-01-23 17:07:38 -05005380 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005381 struct perf_event *event = filp->private_data;
5382 int retval;
5383
Al Viro59551022016-01-22 15:40:57 -05005384 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005385 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05005386 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005387
5388 if (retval < 0)
5389 return retval;
5390
5391 return 0;
5392}
5393
5394static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01005395 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005396 .release = perf_release,
5397 .read = perf_read,
5398 .poll = perf_poll,
5399 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01005400 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005401 .mmap = perf_mmap,
5402 .fasync = perf_fasync,
5403};
5404
5405/*
5406 * Perf event wakeup
5407 *
5408 * If there's data, ensure we set the poll() state and publish everything
5409 * to user-space before waking everybody up.
5410 */
5411
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005412static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
5413{
5414 /* only the parent has fasync state */
5415 if (event->parent)
5416 event = event->parent;
5417 return &event->fasync;
5418}
5419
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005420void perf_event_wakeup(struct perf_event *event)
5421{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005422 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005423
5424 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005425 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005426 event->pending_kill = 0;
5427 }
5428}
5429
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005430static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005431{
5432 struct perf_event *event = container_of(entry,
5433 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01005434 int rctx;
5435
5436 rctx = perf_swevent_get_recursion_context();
5437 /*
5438 * If we 'fail' here, that's OK, it means recursion is already disabled
5439 * and we won't recurse 'further'.
5440 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005441
5442 if (event->pending_disable) {
5443 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005444 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005445 }
5446
5447 if (event->pending_wakeup) {
5448 event->pending_wakeup = 0;
5449 perf_event_wakeup(event);
5450 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01005451
5452 if (rctx >= 0)
5453 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005454}
5455
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005456/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005457 * We assume there is only KVM supporting the callbacks.
5458 * Later on, we might change it to a list if there is
5459 * another virtualization implementation supporting the callbacks.
5460 */
5461struct perf_guest_info_callbacks *perf_guest_cbs;
5462
5463int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5464{
5465 perf_guest_cbs = cbs;
5466 return 0;
5467}
5468EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5469
5470int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5471{
5472 perf_guest_cbs = NULL;
5473 return 0;
5474}
5475EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5476
Jiri Olsa40189942012-08-07 15:20:37 +02005477static void
5478perf_output_sample_regs(struct perf_output_handle *handle,
5479 struct pt_regs *regs, u64 mask)
5480{
5481 int bit;
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05305482 DECLARE_BITMAP(_mask, 64);
Jiri Olsa40189942012-08-07 15:20:37 +02005483
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05305484 bitmap_from_u64(_mask, mask);
5485 for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) {
Jiri Olsa40189942012-08-07 15:20:37 +02005486 u64 val;
5487
5488 val = perf_reg_value(regs, bit);
5489 perf_output_put(handle, val);
5490 }
5491}
5492
Stephane Eranian60e23642014-09-24 13:48:37 +02005493static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005494 struct pt_regs *regs,
5495 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02005496{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005497 if (user_mode(regs)) {
5498 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02005499 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005500 } else if (current->mm) {
5501 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005502 } else {
5503 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5504 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005505 }
5506}
5507
Stephane Eranian60e23642014-09-24 13:48:37 +02005508static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5509 struct pt_regs *regs)
5510{
5511 regs_intr->regs = regs;
5512 regs_intr->abi = perf_reg_abi(current);
5513}
5514
5515
Jiri Olsac5ebced2012-08-07 15:20:40 +02005516/*
5517 * Get remaining task size from user stack pointer.
5518 *
5519 * It'd be better to take stack vma map and limit this more
5520 * precisly, but there's no way to get it safely under interrupt,
5521 * so using TASK_SIZE as limit.
5522 */
5523static u64 perf_ustack_task_size(struct pt_regs *regs)
5524{
5525 unsigned long addr = perf_user_stack_pointer(regs);
5526
5527 if (!addr || addr >= TASK_SIZE)
5528 return 0;
5529
5530 return TASK_SIZE - addr;
5531}
5532
5533static u16
5534perf_sample_ustack_size(u16 stack_size, u16 header_size,
5535 struct pt_regs *regs)
5536{
5537 u64 task_size;
5538
5539 /* No regs, no stack pointer, no dump. */
5540 if (!regs)
5541 return 0;
5542
5543 /*
5544 * Check if we fit in with the requested stack size into the:
5545 * - TASK_SIZE
5546 * If we don't, we limit the size to the TASK_SIZE.
5547 *
5548 * - remaining sample size
5549 * If we don't, we customize the stack size to
5550 * fit in to the remaining sample size.
5551 */
5552
5553 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5554 stack_size = min(stack_size, (u16) task_size);
5555
5556 /* Current header size plus static size and dynamic size. */
5557 header_size += 2 * sizeof(u64);
5558
5559 /* Do we fit in with the current stack dump size? */
5560 if ((u16) (header_size + stack_size) < header_size) {
5561 /*
5562 * If we overflow the maximum size for the sample,
5563 * we customize the stack dump size to fit in.
5564 */
5565 stack_size = USHRT_MAX - header_size - sizeof(u64);
5566 stack_size = round_up(stack_size, sizeof(u64));
5567 }
5568
5569 return stack_size;
5570}
5571
5572static void
5573perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5574 struct pt_regs *regs)
5575{
5576 /* Case of a kernel thread, nothing to dump */
5577 if (!regs) {
5578 u64 size = 0;
5579 perf_output_put(handle, size);
5580 } else {
5581 unsigned long sp;
5582 unsigned int rem;
5583 u64 dyn_size;
Yabin Cuia64fbec2018-08-23 15:59:35 -07005584 mm_segment_t fs;
Jiri Olsac5ebced2012-08-07 15:20:40 +02005585
5586 /*
5587 * We dump:
5588 * static size
5589 * - the size requested by user or the best one we can fit
5590 * in to the sample max size
5591 * data
5592 * - user stack dump data
5593 * dynamic size
5594 * - the actual dumped size
5595 */
5596
5597 /* Static size. */
5598 perf_output_put(handle, dump_size);
5599
5600 /* Data. */
5601 sp = perf_user_stack_pointer(regs);
Yabin Cuia64fbec2018-08-23 15:59:35 -07005602 fs = get_fs();
5603 set_fs(USER_DS);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005604 rem = __output_copy_user(handle, (void *) sp, dump_size);
Yabin Cuia64fbec2018-08-23 15:59:35 -07005605 set_fs(fs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005606 dyn_size = dump_size - rem;
5607
5608 perf_output_skip(handle, rem);
5609
5610 /* Dynamic size. */
5611 perf_output_put(handle, dyn_size);
5612 }
5613}
5614
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005615static void __perf_event_header__init_id(struct perf_event_header *header,
5616 struct perf_sample_data *data,
5617 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005618{
5619 u64 sample_type = event->attr.sample_type;
5620
5621 data->type = sample_type;
5622 header->size += event->id_header_size;
5623
5624 if (sample_type & PERF_SAMPLE_TID) {
5625 /* namespace issues */
5626 data->tid_entry.pid = perf_event_pid(event, current);
5627 data->tid_entry.tid = perf_event_tid(event, current);
5628 }
5629
5630 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005631 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005632
Adrian Hunterff3d5272013-08-27 11:23:07 +03005633 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005634 data->id = primary_event_id(event);
5635
5636 if (sample_type & PERF_SAMPLE_STREAM_ID)
5637 data->stream_id = event->id;
5638
5639 if (sample_type & PERF_SAMPLE_CPU) {
5640 data->cpu_entry.cpu = raw_smp_processor_id();
5641 data->cpu_entry.reserved = 0;
5642 }
5643}
5644
Frederic Weisbecker76369132011-05-19 19:55:04 +02005645void perf_event_header__init_id(struct perf_event_header *header,
5646 struct perf_sample_data *data,
5647 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005648{
5649 if (event->attr.sample_id_all)
5650 __perf_event_header__init_id(header, data, event);
5651}
5652
5653static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5654 struct perf_sample_data *data)
5655{
5656 u64 sample_type = data->type;
5657
5658 if (sample_type & PERF_SAMPLE_TID)
5659 perf_output_put(handle, data->tid_entry);
5660
5661 if (sample_type & PERF_SAMPLE_TIME)
5662 perf_output_put(handle, data->time);
5663
5664 if (sample_type & PERF_SAMPLE_ID)
5665 perf_output_put(handle, data->id);
5666
5667 if (sample_type & PERF_SAMPLE_STREAM_ID)
5668 perf_output_put(handle, data->stream_id);
5669
5670 if (sample_type & PERF_SAMPLE_CPU)
5671 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005672
5673 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5674 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005675}
5676
Frederic Weisbecker76369132011-05-19 19:55:04 +02005677void perf_event__output_id_sample(struct perf_event *event,
5678 struct perf_output_handle *handle,
5679 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005680{
5681 if (event->attr.sample_id_all)
5682 __perf_event__output_id_sample(handle, sample);
5683}
5684
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005685static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005686 struct perf_event *event,
5687 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005688{
5689 u64 read_format = event->attr.read_format;
5690 u64 values[4];
5691 int n = 0;
5692
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005693 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005694 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005695 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005696 atomic64_read(&event->child_total_time_enabled);
5697 }
5698 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005699 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005700 atomic64_read(&event->child_total_time_running);
5701 }
5702 if (read_format & PERF_FORMAT_ID)
5703 values[n++] = primary_event_id(event);
5704
Frederic Weisbecker76369132011-05-19 19:55:04 +02005705 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005706}
5707
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005708static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005709 struct perf_event *event,
5710 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005711{
5712 struct perf_event *leader = event->group_leader, *sub;
5713 u64 read_format = event->attr.read_format;
5714 u64 values[5];
5715 int n = 0;
5716
5717 values[n++] = 1 + leader->nr_siblings;
5718
5719 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005720 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005721
5722 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005723 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005724
Peter Zijlstrabc09bf82018-03-09 12:52:04 +01005725 if ((leader != event) &&
5726 (leader->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005727 leader->pmu->read(leader);
5728
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005729 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005730 if (read_format & PERF_FORMAT_ID)
5731 values[n++] = primary_event_id(leader);
5732
Frederic Weisbecker76369132011-05-19 19:55:04 +02005733 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005734
5735 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5736 n = 0;
5737
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005738 if ((sub != event) &&
5739 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005740 sub->pmu->read(sub);
5741
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005742 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005743 if (read_format & PERF_FORMAT_ID)
5744 values[n++] = primary_event_id(sub);
5745
Frederic Weisbecker76369132011-05-19 19:55:04 +02005746 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005747 }
5748}
5749
Stephane Eranianeed01522010-10-26 16:08:01 +02005750#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5751 PERF_FORMAT_TOTAL_TIME_RUNNING)
5752
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02005753/*
5754 * XXX PERF_SAMPLE_READ vs inherited events seems difficult.
5755 *
5756 * The problem is that its both hard and excessively expensive to iterate the
5757 * child list, not to mention that its impossible to IPI the children running
5758 * on another CPU, from interrupt/NMI context.
5759 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005760static void perf_output_read(struct perf_output_handle *handle,
5761 struct perf_event *event)
5762{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005763 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005764 u64 read_format = event->attr.read_format;
5765
5766 /*
5767 * compute total_time_enabled, total_time_running
5768 * based on snapshot values taken when the event
5769 * was last scheduled in.
5770 *
5771 * we cannot simply called update_context_time()
5772 * because of locking issue as we are called in
5773 * NMI context
5774 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005775 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005776 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005777
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005778 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005779 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005780 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005781 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005782}
5783
5784void perf_output_sample(struct perf_output_handle *handle,
5785 struct perf_event_header *header,
5786 struct perf_sample_data *data,
5787 struct perf_event *event)
5788{
5789 u64 sample_type = data->type;
5790
5791 perf_output_put(handle, *header);
5792
Adrian Hunterff3d5272013-08-27 11:23:07 +03005793 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5794 perf_output_put(handle, data->id);
5795
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005796 if (sample_type & PERF_SAMPLE_IP)
5797 perf_output_put(handle, data->ip);
5798
5799 if (sample_type & PERF_SAMPLE_TID)
5800 perf_output_put(handle, data->tid_entry);
5801
5802 if (sample_type & PERF_SAMPLE_TIME)
5803 perf_output_put(handle, data->time);
5804
5805 if (sample_type & PERF_SAMPLE_ADDR)
5806 perf_output_put(handle, data->addr);
5807
5808 if (sample_type & PERF_SAMPLE_ID)
5809 perf_output_put(handle, data->id);
5810
5811 if (sample_type & PERF_SAMPLE_STREAM_ID)
5812 perf_output_put(handle, data->stream_id);
5813
5814 if (sample_type & PERF_SAMPLE_CPU)
5815 perf_output_put(handle, data->cpu_entry);
5816
5817 if (sample_type & PERF_SAMPLE_PERIOD)
5818 perf_output_put(handle, data->period);
5819
5820 if (sample_type & PERF_SAMPLE_READ)
5821 perf_output_read(handle, event);
5822
5823 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5824 if (data->callchain) {
5825 int size = 1;
5826
5827 if (data->callchain)
5828 size += data->callchain->nr;
5829
5830 size *= sizeof(u64);
5831
Frederic Weisbecker76369132011-05-19 19:55:04 +02005832 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005833 } else {
5834 u64 nr = 0;
5835 perf_output_put(handle, nr);
5836 }
5837 }
5838
5839 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005840 struct perf_raw_record *raw = data->raw;
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005841
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005842 if (raw) {
5843 struct perf_raw_frag *frag = &raw->frag;
5844
5845 perf_output_put(handle, raw->size);
5846 do {
5847 if (frag->copy) {
5848 __output_custom(handle, frag->copy,
5849 frag->data, frag->size);
5850 } else {
5851 __output_copy(handle, frag->data,
5852 frag->size);
5853 }
5854 if (perf_raw_frag_last(frag))
5855 break;
5856 frag = frag->next;
5857 } while (1);
5858 if (frag->pad)
5859 __output_skip(handle, NULL, frag->pad);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005860 } else {
5861 struct {
5862 u32 size;
5863 u32 data;
5864 } raw = {
5865 .size = sizeof(u32),
5866 .data = 0,
5867 };
5868 perf_output_put(handle, raw);
5869 }
5870 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005871
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005872 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5873 if (data->br_stack) {
5874 size_t size;
5875
5876 size = data->br_stack->nr
5877 * sizeof(struct perf_branch_entry);
5878
5879 perf_output_put(handle, data->br_stack->nr);
5880 perf_output_copy(handle, data->br_stack->entries, size);
5881 } else {
5882 /*
5883 * we always store at least the value of nr
5884 */
5885 u64 nr = 0;
5886 perf_output_put(handle, nr);
5887 }
5888 }
Jiri Olsa40189942012-08-07 15:20:37 +02005889
5890 if (sample_type & PERF_SAMPLE_REGS_USER) {
5891 u64 abi = data->regs_user.abi;
5892
5893 /*
5894 * If there are no regs to dump, notice it through
5895 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5896 */
5897 perf_output_put(handle, abi);
5898
5899 if (abi) {
5900 u64 mask = event->attr.sample_regs_user;
5901 perf_output_sample_regs(handle,
5902 data->regs_user.regs,
5903 mask);
5904 }
5905 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005906
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005907 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005908 perf_output_sample_ustack(handle,
5909 data->stack_user_size,
5910 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005911 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005912
5913 if (sample_type & PERF_SAMPLE_WEIGHT)
5914 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005915
5916 if (sample_type & PERF_SAMPLE_DATA_SRC)
5917 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005918
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005919 if (sample_type & PERF_SAMPLE_TRANSACTION)
5920 perf_output_put(handle, data->txn);
5921
Stephane Eranian60e23642014-09-24 13:48:37 +02005922 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5923 u64 abi = data->regs_intr.abi;
5924 /*
5925 * If there are no regs to dump, notice it through
5926 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5927 */
5928 perf_output_put(handle, abi);
5929
5930 if (abi) {
5931 u64 mask = event->attr.sample_regs_intr;
5932
5933 perf_output_sample_regs(handle,
5934 data->regs_intr.regs,
5935 mask);
5936 }
5937 }
5938
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005939 if (!event->attr.watermark) {
5940 int wakeup_events = event->attr.wakeup_events;
5941
5942 if (wakeup_events) {
5943 struct ring_buffer *rb = handle->rb;
5944 int events = local_inc_return(&rb->events);
5945
5946 if (events >= wakeup_events) {
5947 local_sub(wakeup_events, &rb->events);
5948 local_inc(&rb->wakeup);
5949 }
5950 }
5951 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005952}
5953
5954void perf_prepare_sample(struct perf_event_header *header,
5955 struct perf_sample_data *data,
5956 struct perf_event *event,
5957 struct pt_regs *regs)
5958{
5959 u64 sample_type = event->attr.sample_type;
5960
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005961 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005962 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005963
5964 header->misc = 0;
5965 header->misc |= perf_misc_flags(regs);
5966
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005967 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005968
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005969 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005970 data->ip = perf_instruction_pointer(regs);
5971
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005972 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5973 int size = 1;
5974
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005975 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005976
5977 if (data->callchain)
5978 size += data->callchain->nr;
5979
5980 header->size += size * sizeof(u64);
5981 }
5982
5983 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005984 struct perf_raw_record *raw = data->raw;
5985 int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005986
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005987 if (raw) {
5988 struct perf_raw_frag *frag = &raw->frag;
5989 u32 sum = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005990
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005991 do {
5992 sum += frag->size;
5993 if (perf_raw_frag_last(frag))
5994 break;
5995 frag = frag->next;
5996 } while (1);
5997
5998 size = round_up(sum + sizeof(u32), sizeof(u64));
5999 raw->size = size - sizeof(u32);
6000 frag->pad = raw->size - sum;
6001 } else {
6002 size = sizeof(u64);
6003 }
6004
6005 header->size += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006006 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006007
6008 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
6009 int size = sizeof(u64); /* nr */
6010 if (data->br_stack) {
6011 size += data->br_stack->nr
6012 * sizeof(struct perf_branch_entry);
6013 }
6014 header->size += size;
6015 }
Jiri Olsa40189942012-08-07 15:20:37 +02006016
Peter Zijlstra25657112014-09-24 13:48:42 +02006017 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006018 perf_sample_regs_user(&data->regs_user, regs,
6019 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02006020
Jiri Olsa40189942012-08-07 15:20:37 +02006021 if (sample_type & PERF_SAMPLE_REGS_USER) {
6022 /* regs dump ABI info */
6023 int size = sizeof(u64);
6024
Jiri Olsa40189942012-08-07 15:20:37 +02006025 if (data->regs_user.regs) {
6026 u64 mask = event->attr.sample_regs_user;
6027 size += hweight64(mask) * sizeof(u64);
6028 }
6029
6030 header->size += size;
6031 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02006032
6033 if (sample_type & PERF_SAMPLE_STACK_USER) {
6034 /*
6035 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
6036 * processed as the last one or have additional check added
6037 * in case new sample type is added, because we could eat
6038 * up the rest of the sample size.
6039 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02006040 u16 stack_size = event->attr.sample_stack_user;
6041 u16 size = sizeof(u64);
6042
Jiri Olsac5ebced2012-08-07 15:20:40 +02006043 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02006044 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006045
6046 /*
6047 * If there is something to dump, add space for the dump
6048 * itself and for the field that tells the dynamic size,
6049 * which is how many have been actually dumped.
6050 */
6051 if (stack_size)
6052 size += sizeof(u64) + stack_size;
6053
6054 data->stack_user_size = stack_size;
6055 header->size += size;
6056 }
Stephane Eranian60e23642014-09-24 13:48:37 +02006057
6058 if (sample_type & PERF_SAMPLE_REGS_INTR) {
6059 /* regs dump ABI info */
6060 int size = sizeof(u64);
6061
6062 perf_sample_regs_intr(&data->regs_intr, regs);
6063
6064 if (data->regs_intr.regs) {
6065 u64 mask = event->attr.sample_regs_intr;
6066
6067 size += hweight64(mask) * sizeof(u64);
6068 }
6069
6070 header->size += size;
6071 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006072}
6073
Wang Nan9ecda412016-04-05 14:11:18 +00006074static void __always_inline
6075__perf_event_output(struct perf_event *event,
6076 struct perf_sample_data *data,
6077 struct pt_regs *regs,
6078 int (*output_begin)(struct perf_output_handle *,
6079 struct perf_event *,
6080 unsigned int))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006081{
6082 struct perf_output_handle handle;
6083 struct perf_event_header header;
6084
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006085 /* protect the callchain buffers */
6086 rcu_read_lock();
6087
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006088 perf_prepare_sample(&header, data, event, regs);
6089
Wang Nan9ecda412016-04-05 14:11:18 +00006090 if (output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006091 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006092
6093 perf_output_sample(&handle, &header, data, event);
6094
6095 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006096
6097exit:
6098 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006099}
6100
Wang Nan9ecda412016-04-05 14:11:18 +00006101void
6102perf_event_output_forward(struct perf_event *event,
6103 struct perf_sample_data *data,
6104 struct pt_regs *regs)
6105{
6106 __perf_event_output(event, data, regs, perf_output_begin_forward);
6107}
6108
6109void
6110perf_event_output_backward(struct perf_event *event,
6111 struct perf_sample_data *data,
6112 struct pt_regs *regs)
6113{
6114 __perf_event_output(event, data, regs, perf_output_begin_backward);
6115}
6116
6117void
6118perf_event_output(struct perf_event *event,
6119 struct perf_sample_data *data,
6120 struct pt_regs *regs)
6121{
6122 __perf_event_output(event, data, regs, perf_output_begin);
6123}
6124
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006125/*
6126 * read event_id
6127 */
6128
6129struct perf_read_event {
6130 struct perf_event_header header;
6131
6132 u32 pid;
6133 u32 tid;
6134};
6135
6136static void
6137perf_event_read_event(struct perf_event *event,
6138 struct task_struct *task)
6139{
6140 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006141 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006142 struct perf_read_event read_event = {
6143 .header = {
6144 .type = PERF_RECORD_READ,
6145 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006146 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006147 },
6148 .pid = perf_event_pid(event, task),
6149 .tid = perf_event_tid(event, task),
6150 };
6151 int ret;
6152
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006153 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006154 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006155 if (ret)
6156 return;
6157
6158 perf_output_put(&handle, read_event);
6159 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006160 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006161
6162 perf_output_end(&handle);
6163}
6164
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006165typedef void (perf_iterate_f)(struct perf_event *event, void *data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006166
6167static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006168perf_iterate_ctx(struct perf_event_context *ctx,
6169 perf_iterate_f output,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006170 void *data, bool all)
Jiri Olsa52d857a2013-05-06 18:27:18 +02006171{
6172 struct perf_event *event;
6173
6174 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006175 if (!all) {
6176 if (event->state < PERF_EVENT_STATE_INACTIVE)
6177 continue;
6178 if (!event_filter_match(event))
6179 continue;
6180 }
6181
Jiri Olsa67516842013-07-09 18:56:31 +02006182 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006183 }
6184}
6185
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006186static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
Kan Liangf2fb6be2016-03-23 11:24:37 -07006187{
6188 struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events);
6189 struct perf_event *event;
6190
6191 list_for_each_entry_rcu(event, &pel->list, sb_list) {
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02006192 /*
6193 * Skip events that are not fully formed yet; ensure that
6194 * if we observe event->ctx, both event and ctx will be
6195 * complete enough. See perf_install_in_context().
6196 */
6197 if (!smp_load_acquire(&event->ctx))
6198 continue;
6199
Kan Liangf2fb6be2016-03-23 11:24:37 -07006200 if (event->state < PERF_EVENT_STATE_INACTIVE)
6201 continue;
6202 if (!event_filter_match(event))
6203 continue;
6204 output(event, data);
6205 }
6206}
6207
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006208/*
6209 * Iterate all events that need to receive side-band events.
6210 *
6211 * For new callers; ensure that account_pmu_sb_event() includes
6212 * your event, otherwise it might not get delivered.
6213 */
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006214static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006215perf_iterate_sb(perf_iterate_f output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006216 struct perf_event_context *task_ctx)
6217{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006218 struct perf_event_context *ctx;
Jiri Olsa52d857a2013-05-06 18:27:18 +02006219 int ctxn;
6220
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006221 rcu_read_lock();
6222 preempt_disable();
6223
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006224 /*
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006225 * If we have task_ctx != NULL we only notify the task context itself.
6226 * The task_ctx is set only for EXIT events before releasing task
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006227 * context.
6228 */
6229 if (task_ctx) {
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006230 perf_iterate_ctx(task_ctx, output, data, false);
6231 goto done;
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006232 }
6233
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006234 perf_iterate_sb_cpu(output, data);
Kan Liangf2fb6be2016-03-23 11:24:37 -07006235
6236 for_each_task_context_nr(ctxn) {
Jiri Olsa52d857a2013-05-06 18:27:18 +02006237 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6238 if (ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006239 perf_iterate_ctx(ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006240 }
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006241done:
Kan Liangf2fb6be2016-03-23 11:24:37 -07006242 preempt_enable();
Jiri Olsa52d857a2013-05-06 18:27:18 +02006243 rcu_read_unlock();
6244}
6245
Alexander Shishkin375637b2016-04-27 18:44:46 +03006246/*
6247 * Clear all file-based filters at exec, they'll have to be
6248 * re-instated when/if these objects are mmapped again.
6249 */
6250static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
6251{
6252 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6253 struct perf_addr_filter *filter;
6254 unsigned int restart = 0, count = 0;
6255 unsigned long flags;
6256
6257 if (!has_addr_filter(event))
6258 return;
6259
6260 raw_spin_lock_irqsave(&ifh->lock, flags);
6261 list_for_each_entry(filter, &ifh->list, entry) {
6262 if (filter->inode) {
6263 event->addr_filters_offs[count] = 0;
6264 restart++;
6265 }
6266
6267 count++;
6268 }
6269
6270 if (restart)
6271 event->addr_filters_gen++;
6272 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6273
6274 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03006275 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006276}
6277
6278void perf_event_exec(void)
6279{
6280 struct perf_event_context *ctx;
6281 int ctxn;
6282
6283 rcu_read_lock();
6284 for_each_task_context_nr(ctxn) {
6285 ctx = current->perf_event_ctxp[ctxn];
6286 if (!ctx)
6287 continue;
6288
6289 perf_event_enable_on_exec(ctxn);
6290
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006291 perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL,
Alexander Shishkin375637b2016-04-27 18:44:46 +03006292 true);
6293 }
6294 rcu_read_unlock();
6295}
6296
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006297struct remote_output {
6298 struct ring_buffer *rb;
6299 int err;
6300};
6301
6302static void __perf_event_output_stop(struct perf_event *event, void *data)
6303{
6304 struct perf_event *parent = event->parent;
6305 struct remote_output *ro = data;
6306 struct ring_buffer *rb = ro->rb;
Alexander Shishkin375637b2016-04-27 18:44:46 +03006307 struct stop_event_data sd = {
6308 .event = event,
6309 };
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006310
6311 if (!has_aux(event))
6312 return;
6313
6314 if (!parent)
6315 parent = event;
6316
6317 /*
6318 * In case of inheritance, it will be the parent that links to the
Alexander Shishkin767ae082016-09-06 16:23:49 +03006319 * ring-buffer, but it will be the child that's actually using it.
6320 *
6321 * We are using event::rb to determine if the event should be stopped,
6322 * however this may race with ring_buffer_attach() (through set_output),
6323 * which will make us skip the event that actually needs to be stopped.
6324 * So ring_buffer_attach() has to stop an aux event before re-assigning
6325 * its rb pointer.
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006326 */
6327 if (rcu_dereference(parent->rb) == rb)
Alexander Shishkin375637b2016-04-27 18:44:46 +03006328 ro->err = __perf_event_stop(&sd);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006329}
6330
6331static int __perf_pmu_output_stop(void *info)
6332{
6333 struct perf_event *event = info;
6334 struct pmu *pmu = event->pmu;
Will Deacon8b6a3fe2016-08-24 10:07:14 +01006335 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006336 struct remote_output ro = {
6337 .rb = event->rb,
6338 };
6339
6340 rcu_read_lock();
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006341 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006342 if (cpuctx->task_ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006343 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006344 &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006345 rcu_read_unlock();
6346
6347 return ro.err;
6348}
6349
6350static void perf_pmu_output_stop(struct perf_event *event)
6351{
6352 struct perf_event *iter;
6353 int err, cpu;
6354
6355restart:
6356 rcu_read_lock();
6357 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
6358 /*
6359 * For per-CPU events, we need to make sure that neither they
6360 * nor their children are running; for cpu==-1 events it's
6361 * sufficient to stop the event itself if it's active, since
6362 * it can't have children.
6363 */
6364 cpu = iter->cpu;
6365 if (cpu == -1)
6366 cpu = READ_ONCE(iter->oncpu);
6367
6368 if (cpu == -1)
6369 continue;
6370
6371 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
6372 if (err == -EAGAIN) {
6373 rcu_read_unlock();
6374 goto restart;
6375 }
6376 }
6377 rcu_read_unlock();
6378}
6379
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006380/*
6381 * task tracking -- fork/exit
6382 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02006383 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006384 */
6385
6386struct perf_task_event {
6387 struct task_struct *task;
6388 struct perf_event_context *task_ctx;
6389
6390 struct {
6391 struct perf_event_header header;
6392
6393 u32 pid;
6394 u32 ppid;
6395 u32 tid;
6396 u32 ptid;
6397 u64 time;
6398 } event_id;
6399};
6400
Jiri Olsa67516842013-07-09 18:56:31 +02006401static int perf_event_task_match(struct perf_event *event)
6402{
Stephane Eranian13d7a242013-08-21 12:10:24 +02006403 return event->attr.comm || event->attr.mmap ||
6404 event->attr.mmap2 || event->attr.mmap_data ||
6405 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02006406}
6407
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006408static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006409 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006410{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006411 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006412 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006413 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006414 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006415 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01006416
Jiri Olsa67516842013-07-09 18:56:31 +02006417 if (!perf_event_task_match(event))
6418 return;
6419
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006420 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006421
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006422 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006423 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02006424 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006425 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006426
6427 task_event->event_id.pid = perf_event_pid(event, task);
6428 task_event->event_id.ppid = perf_event_pid(event, current);
6429
6430 task_event->event_id.tid = perf_event_tid(event, task);
6431 task_event->event_id.ptid = perf_event_tid(event, current);
6432
Peter Zijlstra34f43922015-02-20 14:05:38 +01006433 task_event->event_id.time = perf_event_clock(event);
6434
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006435 perf_output_put(&handle, task_event->event_id);
6436
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006437 perf_event__output_id_sample(event, &handle, &sample);
6438
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006439 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006440out:
6441 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006442}
6443
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006444static void perf_event_task(struct task_struct *task,
6445 struct perf_event_context *task_ctx,
6446 int new)
6447{
6448 struct perf_task_event task_event;
6449
6450 if (!atomic_read(&nr_comm_events) &&
6451 !atomic_read(&nr_mmap_events) &&
6452 !atomic_read(&nr_task_events))
6453 return;
6454
6455 task_event = (struct perf_task_event){
6456 .task = task,
6457 .task_ctx = task_ctx,
6458 .event_id = {
6459 .header = {
6460 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
6461 .misc = 0,
6462 .size = sizeof(task_event.event_id),
6463 },
6464 /* .pid */
6465 /* .ppid */
6466 /* .tid */
6467 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01006468 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006469 },
6470 };
6471
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006472 perf_iterate_sb(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006473 &task_event,
6474 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006475}
6476
6477void perf_event_fork(struct task_struct *task)
6478{
6479 perf_event_task(task, NULL, 1);
6480}
6481
6482/*
6483 * comm tracking
6484 */
6485
6486struct perf_comm_event {
6487 struct task_struct *task;
6488 char *comm;
6489 int comm_size;
6490
6491 struct {
6492 struct perf_event_header header;
6493
6494 u32 pid;
6495 u32 tid;
6496 } event_id;
6497};
6498
Jiri Olsa67516842013-07-09 18:56:31 +02006499static int perf_event_comm_match(struct perf_event *event)
6500{
6501 return event->attr.comm;
6502}
6503
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006504static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006505 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006506{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006507 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006508 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006509 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006510 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006511 int ret;
6512
Jiri Olsa67516842013-07-09 18:56:31 +02006513 if (!perf_event_comm_match(event))
6514 return;
6515
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006516 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
6517 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006518 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006519
6520 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006521 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006522
6523 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
6524 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
6525
6526 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02006527 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006528 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006529
6530 perf_event__output_id_sample(event, &handle, &sample);
6531
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006532 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006533out:
6534 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006535}
6536
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006537static void perf_event_comm_event(struct perf_comm_event *comm_event)
6538{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006539 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006540 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006541
6542 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01006543 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006544 size = ALIGN(strlen(comm)+1, sizeof(u64));
6545
6546 comm_event->comm = comm;
6547 comm_event->comm_size = size;
6548
6549 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006550
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006551 perf_iterate_sb(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006552 comm_event,
6553 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006554}
6555
Adrian Hunter82b89772014-05-28 11:45:04 +03006556void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006557{
6558 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006559
6560 if (!atomic_read(&nr_comm_events))
6561 return;
6562
6563 comm_event = (struct perf_comm_event){
6564 .task = task,
6565 /* .comm */
6566 /* .comm_size */
6567 .event_id = {
6568 .header = {
6569 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03006570 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006571 /* .size */
6572 },
6573 /* .pid */
6574 /* .tid */
6575 },
6576 };
6577
6578 perf_event_comm_event(&comm_event);
6579}
6580
6581/*
6582 * mmap tracking
6583 */
6584
6585struct perf_mmap_event {
6586 struct vm_area_struct *vma;
6587
6588 const char *file_name;
6589 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006590 int maj, min;
6591 u64 ino;
6592 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006593 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006594
6595 struct {
6596 struct perf_event_header header;
6597
6598 u32 pid;
6599 u32 tid;
6600 u64 start;
6601 u64 len;
6602 u64 pgoff;
6603 } event_id;
6604};
6605
Jiri Olsa67516842013-07-09 18:56:31 +02006606static int perf_event_mmap_match(struct perf_event *event,
6607 void *data)
6608{
6609 struct perf_mmap_event *mmap_event = data;
6610 struct vm_area_struct *vma = mmap_event->vma;
6611 int executable = vma->vm_flags & VM_EXEC;
6612
6613 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02006614 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02006615}
6616
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006617static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006618 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006619{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006620 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006621 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006622 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006623 int size = mmap_event->event_id.header.size;
Stephane Eranian8fb8f972019-03-07 10:52:33 -08006624 u32 type = mmap_event->event_id.header.type;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006625 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006626
Jiri Olsa67516842013-07-09 18:56:31 +02006627 if (!perf_event_mmap_match(event, data))
6628 return;
6629
Stephane Eranian13d7a242013-08-21 12:10:24 +02006630 if (event->attr.mmap2) {
6631 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
6632 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
6633 mmap_event->event_id.header.size += sizeof(mmap_event->min);
6634 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03006635 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006636 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
6637 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006638 }
6639
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006640 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
6641 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006642 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006643 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006644 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006645
6646 mmap_event->event_id.pid = perf_event_pid(event, current);
6647 mmap_event->event_id.tid = perf_event_tid(event, current);
6648
6649 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006650
6651 if (event->attr.mmap2) {
6652 perf_output_put(&handle, mmap_event->maj);
6653 perf_output_put(&handle, mmap_event->min);
6654 perf_output_put(&handle, mmap_event->ino);
6655 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006656 perf_output_put(&handle, mmap_event->prot);
6657 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006658 }
6659
Frederic Weisbecker76369132011-05-19 19:55:04 +02006660 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006661 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006662
6663 perf_event__output_id_sample(event, &handle, &sample);
6664
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006665 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006666out:
6667 mmap_event->event_id.header.size = size;
Stephane Eranian8fb8f972019-03-07 10:52:33 -08006668 mmap_event->event_id.header.type = type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006669}
6670
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006671static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
6672{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006673 struct vm_area_struct *vma = mmap_event->vma;
6674 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006675 int maj = 0, min = 0;
6676 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006677 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006678 unsigned int size;
6679 char tmp[16];
6680 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006681 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006682
Peter Zijlstrab41615a2017-01-26 23:15:08 +01006683 if (vma->vm_flags & VM_READ)
6684 prot |= PROT_READ;
6685 if (vma->vm_flags & VM_WRITE)
6686 prot |= PROT_WRITE;
6687 if (vma->vm_flags & VM_EXEC)
6688 prot |= PROT_EXEC;
6689
6690 if (vma->vm_flags & VM_MAYSHARE)
6691 flags = MAP_SHARED;
6692 else
6693 flags = MAP_PRIVATE;
6694
6695 if (vma->vm_flags & VM_DENYWRITE)
6696 flags |= MAP_DENYWRITE;
6697 if (vma->vm_flags & VM_MAYEXEC)
6698 flags |= MAP_EXECUTABLE;
6699 if (vma->vm_flags & VM_LOCKED)
6700 flags |= MAP_LOCKED;
6701 if (vma->vm_flags & VM_HUGETLB)
6702 flags |= MAP_HUGETLB;
6703
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006704 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02006705 struct inode *inode;
6706 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006707
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006708 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006709 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006710 name = "//enomem";
6711 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006712 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006713 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006714 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006715 * need to add enough zero bytes after the string to handle
6716 * the 64bit alignment we do later.
6717 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02006718 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006719 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006720 name = "//toolong";
6721 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006722 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02006723 inode = file_inode(vma->vm_file);
6724 dev = inode->i_sb->s_dev;
6725 ino = inode->i_ino;
6726 gen = inode->i_generation;
6727 maj = MAJOR(dev);
6728 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006729
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006730 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006731 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006732 if (vma->vm_ops && vma->vm_ops->name) {
6733 name = (char *) vma->vm_ops->name(vma);
6734 if (name)
6735 goto cpy_name;
6736 }
6737
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006738 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006739 if (name)
6740 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006741
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006742 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006743 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006744 name = "[heap]";
6745 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006746 }
6747 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006748 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006749 name = "[stack]";
6750 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006751 }
6752
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006753 name = "//anon";
6754 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006755 }
6756
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006757cpy_name:
6758 strlcpy(tmp, name, sizeof(tmp));
6759 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006760got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006761 /*
6762 * Since our buffer works in 8 byte units we need to align our string
6763 * size to a multiple of 8. However, we must guarantee the tail end is
6764 * zero'd out to avoid leaking random bits to userspace.
6765 */
6766 size = strlen(name)+1;
6767 while (!IS_ALIGNED(size, sizeof(u64)))
6768 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006769
6770 mmap_event->file_name = name;
6771 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006772 mmap_event->maj = maj;
6773 mmap_event->min = min;
6774 mmap_event->ino = ino;
6775 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006776 mmap_event->prot = prot;
6777 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006778
Stephane Eranian2fe85422013-01-24 16:10:39 +01006779 if (!(vma->vm_flags & VM_EXEC))
6780 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6781
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006782 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6783
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006784 perf_iterate_sb(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006785 mmap_event,
6786 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006787
6788 kfree(buf);
6789}
6790
Alexander Shishkin375637b2016-04-27 18:44:46 +03006791/*
Alexander Shishkin375637b2016-04-27 18:44:46 +03006792 * Check whether inode and address range match filter criteria.
6793 */
6794static bool perf_addr_filter_match(struct perf_addr_filter *filter,
6795 struct file *file, unsigned long offset,
6796 unsigned long size)
6797{
6798 if (filter->inode != file->f_inode)
6799 return false;
6800
6801 if (filter->offset > offset + size)
6802 return false;
6803
6804 if (filter->offset + filter->size < offset)
6805 return false;
6806
6807 return true;
6808}
6809
6810static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
6811{
6812 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6813 struct vm_area_struct *vma = data;
6814 unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags;
6815 struct file *file = vma->vm_file;
6816 struct perf_addr_filter *filter;
6817 unsigned int restart = 0, count = 0;
6818
6819 if (!has_addr_filter(event))
6820 return;
6821
6822 if (!file)
6823 return;
6824
6825 raw_spin_lock_irqsave(&ifh->lock, flags);
6826 list_for_each_entry(filter, &ifh->list, entry) {
6827 if (perf_addr_filter_match(filter, file, off,
6828 vma->vm_end - vma->vm_start)) {
6829 event->addr_filters_offs[count] = vma->vm_start;
6830 restart++;
6831 }
6832
6833 count++;
6834 }
6835
6836 if (restart)
6837 event->addr_filters_gen++;
6838 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6839
6840 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03006841 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006842}
6843
6844/*
6845 * Adjust all task's events' filters to the new vma
6846 */
6847static void perf_addr_filters_adjust(struct vm_area_struct *vma)
6848{
6849 struct perf_event_context *ctx;
6850 int ctxn;
6851
Mathieu Poirier12b40a22016-07-18 10:43:06 -06006852 /*
6853 * Data tracing isn't supported yet and as such there is no need
6854 * to keep track of anything that isn't related to executable code:
6855 */
6856 if (!(vma->vm_flags & VM_EXEC))
6857 return;
6858
Alexander Shishkin375637b2016-04-27 18:44:46 +03006859 rcu_read_lock();
6860 for_each_task_context_nr(ctxn) {
6861 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6862 if (!ctx)
6863 continue;
6864
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006865 perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006866 }
6867 rcu_read_unlock();
6868}
6869
Eric B Munson3af9e852010-05-18 15:30:49 +01006870void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006871{
6872 struct perf_mmap_event mmap_event;
6873
6874 if (!atomic_read(&nr_mmap_events))
6875 return;
6876
6877 mmap_event = (struct perf_mmap_event){
6878 .vma = vma,
6879 /* .file_name */
6880 /* .file_size */
6881 .event_id = {
6882 .header = {
6883 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006884 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006885 /* .size */
6886 },
6887 /* .pid */
6888 /* .tid */
6889 .start = vma->vm_start,
6890 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006891 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006892 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006893 /* .maj (attr_mmap2 only) */
6894 /* .min (attr_mmap2 only) */
6895 /* .ino (attr_mmap2 only) */
6896 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006897 /* .prot (attr_mmap2 only) */
6898 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006899 };
6900
Alexander Shishkin375637b2016-04-27 18:44:46 +03006901 perf_addr_filters_adjust(vma);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006902 perf_event_mmap_event(&mmap_event);
6903}
6904
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006905void perf_event_aux_event(struct perf_event *event, unsigned long head,
6906 unsigned long size, u64 flags)
6907{
6908 struct perf_output_handle handle;
6909 struct perf_sample_data sample;
6910 struct perf_aux_event {
6911 struct perf_event_header header;
6912 u64 offset;
6913 u64 size;
6914 u64 flags;
6915 } rec = {
6916 .header = {
6917 .type = PERF_RECORD_AUX,
6918 .misc = 0,
6919 .size = sizeof(rec),
6920 },
6921 .offset = head,
6922 .size = size,
6923 .flags = flags,
6924 };
6925 int ret;
6926
6927 perf_event_header__init_id(&rec.header, &sample, event);
6928 ret = perf_output_begin(&handle, event, rec.header.size);
6929
6930 if (ret)
6931 return;
6932
6933 perf_output_put(&handle, rec);
6934 perf_event__output_id_sample(event, &handle, &sample);
6935
6936 perf_output_end(&handle);
6937}
6938
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006939/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006940 * Lost/dropped samples logging
6941 */
6942void perf_log_lost_samples(struct perf_event *event, u64 lost)
6943{
6944 struct perf_output_handle handle;
6945 struct perf_sample_data sample;
6946 int ret;
6947
6948 struct {
6949 struct perf_event_header header;
6950 u64 lost;
6951 } lost_samples_event = {
6952 .header = {
6953 .type = PERF_RECORD_LOST_SAMPLES,
6954 .misc = 0,
6955 .size = sizeof(lost_samples_event),
6956 },
6957 .lost = lost,
6958 };
6959
6960 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6961
6962 ret = perf_output_begin(&handle, event,
6963 lost_samples_event.header.size);
6964 if (ret)
6965 return;
6966
6967 perf_output_put(&handle, lost_samples_event);
6968 perf_event__output_id_sample(event, &handle, &sample);
6969 perf_output_end(&handle);
6970}
6971
6972/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006973 * context_switch tracking
6974 */
6975
6976struct perf_switch_event {
6977 struct task_struct *task;
6978 struct task_struct *next_prev;
6979
6980 struct {
6981 struct perf_event_header header;
6982 u32 next_prev_pid;
6983 u32 next_prev_tid;
6984 } event_id;
6985};
6986
6987static int perf_event_switch_match(struct perf_event *event)
6988{
6989 return event->attr.context_switch;
6990}
6991
6992static void perf_event_switch_output(struct perf_event *event, void *data)
6993{
6994 struct perf_switch_event *se = data;
6995 struct perf_output_handle handle;
6996 struct perf_sample_data sample;
6997 int ret;
6998
6999 if (!perf_event_switch_match(event))
7000 return;
7001
7002 /* Only CPU-wide events are allowed to see next/prev pid/tid */
7003 if (event->ctx->task) {
7004 se->event_id.header.type = PERF_RECORD_SWITCH;
7005 se->event_id.header.size = sizeof(se->event_id.header);
7006 } else {
7007 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
7008 se->event_id.header.size = sizeof(se->event_id);
7009 se->event_id.next_prev_pid =
7010 perf_event_pid(event, se->next_prev);
7011 se->event_id.next_prev_tid =
7012 perf_event_tid(event, se->next_prev);
7013 }
7014
7015 perf_event_header__init_id(&se->event_id.header, &sample, event);
7016
7017 ret = perf_output_begin(&handle, event, se->event_id.header.size);
7018 if (ret)
7019 return;
7020
7021 if (event->ctx->task)
7022 perf_output_put(&handle, se->event_id.header);
7023 else
7024 perf_output_put(&handle, se->event_id);
7025
7026 perf_event__output_id_sample(event, &handle, &sample);
7027
7028 perf_output_end(&handle);
7029}
7030
7031static void perf_event_switch(struct task_struct *task,
7032 struct task_struct *next_prev, bool sched_in)
7033{
7034 struct perf_switch_event switch_event;
7035
7036 /* N.B. caller checks nr_switch_events != 0 */
7037
7038 switch_event = (struct perf_switch_event){
7039 .task = task,
7040 .next_prev = next_prev,
7041 .event_id = {
7042 .header = {
7043 /* .type */
7044 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
7045 /* .size */
7046 },
7047 /* .next_prev_pid */
7048 /* .next_prev_tid */
7049 },
7050 };
7051
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007052 perf_iterate_sb(perf_event_switch_output,
Adrian Hunter45ac1402015-07-21 12:44:02 +03007053 &switch_event,
7054 NULL);
7055}
7056
7057/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007058 * IRQ throttle logging
7059 */
7060
7061static void perf_log_throttle(struct perf_event *event, int enable)
7062{
7063 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007064 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007065 int ret;
7066
7067 struct {
7068 struct perf_event_header header;
7069 u64 time;
7070 u64 id;
7071 u64 stream_id;
7072 } throttle_event = {
7073 .header = {
7074 .type = PERF_RECORD_THROTTLE,
7075 .misc = 0,
7076 .size = sizeof(throttle_event),
7077 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01007078 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007079 .id = primary_event_id(event),
7080 .stream_id = event->id,
7081 };
7082
7083 if (enable)
7084 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
7085
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007086 perf_event_header__init_id(&throttle_event.header, &sample, event);
7087
7088 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007089 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007090 if (ret)
7091 return;
7092
7093 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007094 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007095 perf_output_end(&handle);
7096}
7097
Alexander Shishkinec0d7722015-01-14 14:18:23 +02007098static void perf_log_itrace_start(struct perf_event *event)
7099{
7100 struct perf_output_handle handle;
7101 struct perf_sample_data sample;
7102 struct perf_aux_event {
7103 struct perf_event_header header;
7104 u32 pid;
7105 u32 tid;
7106 } rec;
7107 int ret;
7108
7109 if (event->parent)
7110 event = event->parent;
7111
7112 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
7113 event->hw.itrace_started)
7114 return;
7115
Alexander Shishkinec0d7722015-01-14 14:18:23 +02007116 rec.header.type = PERF_RECORD_ITRACE_START;
7117 rec.header.misc = 0;
7118 rec.header.size = sizeof(rec);
7119 rec.pid = perf_event_pid(event, current);
7120 rec.tid = perf_event_tid(event, current);
7121
7122 perf_event_header__init_id(&rec.header, &sample, event);
7123 ret = perf_output_begin(&handle, event, rec.header.size);
7124
7125 if (ret)
7126 return;
7127
7128 perf_output_put(&handle, rec);
7129 perf_event__output_id_sample(event, &handle, &sample);
7130
7131 perf_output_end(&handle);
7132}
7133
Jiri Olsaa88ff232016-12-28 14:31:03 +01007134static int
7135__perf_event_account_interrupt(struct perf_event *event, int throttle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007136{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007137 struct hw_perf_event *hwc = &event->hw;
7138 int ret = 0;
Jiri Olsaa88ff232016-12-28 14:31:03 +01007139 u64 seq;
Peter Zijlstra96398822010-11-24 18:55:29 +01007140
Stephane Eraniane050e3f2012-01-26 17:03:19 +01007141 seq = __this_cpu_read(perf_throttled_seq);
7142 if (seq != hwc->interrupts_seq) {
7143 hwc->interrupts_seq = seq;
7144 hwc->interrupts = 1;
7145 } else {
7146 hwc->interrupts++;
7147 if (unlikely(throttle
7148 && hwc->interrupts >= max_samples_per_tick)) {
7149 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02007150 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01007151 hwc->interrupts = MAX_INTERRUPTS;
7152 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007153 ret = 1;
7154 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01007155 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007156
7157 if (event->attr.freq) {
7158 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01007159 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007160
Peter Zijlstraabd50712010-01-26 18:50:16 +01007161 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007162
Peter Zijlstraabd50712010-01-26 18:50:16 +01007163 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01007164 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007165 }
7166
Jiri Olsaa88ff232016-12-28 14:31:03 +01007167 return ret;
7168}
7169
7170int perf_event_account_interrupt(struct perf_event *event)
7171{
7172 return __perf_event_account_interrupt(event, 1);
7173}
7174
7175/*
7176 * Generic event overflow handling, sampling.
7177 */
7178
7179static int __perf_event_overflow(struct perf_event *event,
7180 int throttle, struct perf_sample_data *data,
7181 struct pt_regs *regs)
7182{
7183 int events = atomic_read(&event->event_limit);
7184 int ret = 0;
7185
7186 /*
7187 * Non-sampling counters might still use the PMI to fold short
7188 * hardware counters, ignore those.
7189 */
7190 if (unlikely(!is_sampling_event(event)))
7191 return 0;
7192
7193 ret = __perf_event_account_interrupt(event, throttle);
7194
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007195 /*
7196 * XXX event_limit might not quite work as expected on inherited
7197 * events
7198 */
7199
7200 event->pending_kill = POLL_IN;
7201 if (events && atomic_dec_and_test(&event->event_limit)) {
7202 ret = 1;
7203 event->pending_kill = POLL_HUP;
Jiri Olsa5aab90c2016-10-26 11:48:24 +02007204
7205 perf_event_disable_inatomic(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007206 }
7207
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007208 READ_ONCE(event->overflow_handler)(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01007209
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02007210 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007211 event->pending_wakeup = 1;
7212 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02007213 }
7214
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007215 return ret;
7216}
7217
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007218int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007219 struct perf_sample_data *data,
7220 struct pt_regs *regs)
7221{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007222 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007223}
7224
7225/*
7226 * Generic software event infrastructure
7227 */
7228
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007229struct swevent_htable {
7230 struct swevent_hlist *swevent_hlist;
7231 struct mutex hlist_mutex;
7232 int hlist_refcount;
7233
7234 /* Recursion avoidance in each contexts */
7235 int recursion[PERF_NR_CONTEXTS];
7236};
7237
7238static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
7239
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007240/*
7241 * We directly increment event->count and keep a second value in
7242 * event->hw.period_left to count intervals. This period event
7243 * is kept in the range [-sample_period, 0] so that we can use the
7244 * sign as trigger.
7245 */
7246
Jiri Olsaab573842013-05-01 17:25:44 +02007247u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007248{
7249 struct hw_perf_event *hwc = &event->hw;
7250 u64 period = hwc->last_period;
7251 u64 nr, offset;
7252 s64 old, val;
7253
7254 hwc->last_period = hwc->sample_period;
7255
7256again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02007257 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007258 if (val < 0)
7259 return 0;
7260
7261 nr = div64_u64(period + val, period);
7262 offset = nr * period;
7263 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02007264 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007265 goto again;
7266
7267 return nr;
7268}
7269
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007270static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007271 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007272 struct pt_regs *regs)
7273{
7274 struct hw_perf_event *hwc = &event->hw;
7275 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007276
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007277 if (!overflow)
7278 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007279
7280 if (hwc->interrupts == MAX_INTERRUPTS)
7281 return;
7282
7283 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007284 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007285 data, regs)) {
7286 /*
7287 * We inhibit the overflow from happening when
7288 * hwc->interrupts == MAX_INTERRUPTS.
7289 */
7290 break;
7291 }
7292 throttle = 1;
7293 }
7294}
7295
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007296static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007297 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007298 struct pt_regs *regs)
7299{
7300 struct hw_perf_event *hwc = &event->hw;
7301
Peter Zijlstrae7850592010-05-21 14:43:08 +02007302 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007303
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007304 if (!regs)
7305 return;
7306
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007307 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007308 return;
7309
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03007310 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
7311 data->period = nr;
7312 return perf_swevent_overflow(event, 1, data, regs);
7313 } else
7314 data->period = event->hw.last_period;
7315
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007316 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007317 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007318
Peter Zijlstrae7850592010-05-21 14:43:08 +02007319 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007320 return;
7321
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007322 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007323}
7324
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007325static int perf_exclude_event(struct perf_event *event,
7326 struct pt_regs *regs)
7327{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007328 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01007329 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007330
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007331 if (regs) {
7332 if (event->attr.exclude_user && user_mode(regs))
7333 return 1;
7334
7335 if (event->attr.exclude_kernel && !user_mode(regs))
7336 return 1;
7337 }
7338
7339 return 0;
7340}
7341
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007342static int perf_swevent_match(struct perf_event *event,
7343 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08007344 u32 event_id,
7345 struct perf_sample_data *data,
7346 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007347{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007348 if (event->attr.type != type)
7349 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007350
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007351 if (event->attr.config != event_id)
7352 return 0;
7353
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007354 if (perf_exclude_event(event, regs))
7355 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007356
7357 return 1;
7358}
7359
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007360static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007361{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007362 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007363
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007364 return hash_64(val, SWEVENT_HLIST_BITS);
7365}
7366
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007367static inline struct hlist_head *
7368__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007369{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007370 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007371
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007372 return &hlist->heads[hash];
7373}
7374
7375/* For the read side: events when they trigger */
7376static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007377find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007378{
7379 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007380
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007381 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007382 if (!hlist)
7383 return NULL;
7384
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007385 return __find_swevent_head(hlist, type, event_id);
7386}
7387
7388/* For the event head insertion and removal in the hlist */
7389static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007390find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007391{
7392 struct swevent_hlist *hlist;
7393 u32 event_id = event->attr.config;
7394 u64 type = event->attr.type;
7395
7396 /*
7397 * Event scheduling is always serialized against hlist allocation
7398 * and release. Which makes the protected version suitable here.
7399 * The context lock guarantees that.
7400 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007401 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007402 lockdep_is_held(&event->ctx->lock));
7403 if (!hlist)
7404 return NULL;
7405
7406 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007407}
7408
7409static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007410 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007411 struct perf_sample_data *data,
7412 struct pt_regs *regs)
7413{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007414 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007415 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007416 struct hlist_head *head;
7417
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007418 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007419 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007420 if (!head)
7421 goto end;
7422
Sasha Levinb67bfe02013-02-27 17:06:00 -08007423 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08007424 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007425 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007426 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007427end:
7428 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007429}
7430
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007431DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
7432
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007433int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007434{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007435 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007436
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007437 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007438}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01007439EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007440
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007441void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007442{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007443 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007444
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007445 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007446}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007447
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007448void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007449{
Ingo Molnara4234bf2009-11-23 10:57:59 +01007450 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007451
7452 if (WARN_ON_ONCE(!regs))
7453 return;
7454
7455 perf_sample_data_init(&data, addr, 0);
7456 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
7457}
7458
7459void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
7460{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007461 int rctx;
7462
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007463 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007464 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007465 if (unlikely(rctx < 0))
7466 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007467
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007468 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007469
7470 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007471fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007472 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007473}
7474
7475static void perf_swevent_read(struct perf_event *event)
7476{
7477}
7478
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007479static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007480{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007481 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007482 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007483 struct hlist_head *head;
7484
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007485 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007486 hwc->last_period = hwc->sample_period;
7487 perf_swevent_set_period(event);
7488 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007489
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007490 hwc->state = !(flags & PERF_EF_START);
7491
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007492 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01007493 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007494 return -EINVAL;
7495
7496 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08007497 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007498
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007499 return 0;
7500}
7501
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007502static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007503{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007504 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007505}
7506
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007507static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007508{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007509 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007510}
7511
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007512static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007513{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007514 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007515}
7516
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007517/* Deref the hlist from the update side */
7518static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007519swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007520{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007521 return rcu_dereference_protected(swhash->swevent_hlist,
7522 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007523}
7524
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007525static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007526{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007527 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007528
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007529 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007530 return;
7531
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03007532 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08007533 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007534}
7535
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007536static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007537{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007538 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007539
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007540 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007541
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007542 if (!--swhash->hlist_refcount)
7543 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007544
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007545 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007546}
7547
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007548static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007549{
7550 int cpu;
7551
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007552 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007553 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007554}
7555
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007556static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007557{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007558 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007559 int err = 0;
7560
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007561 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007562 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007563 struct swevent_hlist *hlist;
7564
7565 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
7566 if (!hlist) {
7567 err = -ENOMEM;
7568 goto exit;
7569 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007570 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007571 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007572 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007573exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007574 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007575
7576 return err;
7577}
7578
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007579static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007580{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007581 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007582
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007583 get_online_cpus();
7584 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007585 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007586 if (err) {
7587 failed_cpu = cpu;
7588 goto fail;
7589 }
7590 }
7591 put_online_cpus();
7592
7593 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007594fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007595 for_each_possible_cpu(cpu) {
7596 if (cpu == failed_cpu)
7597 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007598 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007599 }
7600
7601 put_online_cpus();
7602 return err;
7603}
7604
Ingo Molnarc5905af2012-02-24 08:31:31 +01007605struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007606
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007607static void sw_perf_event_destroy(struct perf_event *event)
7608{
7609 u64 event_id = event->attr.config;
7610
7611 WARN_ON(event->parent);
7612
Ingo Molnarc5905af2012-02-24 08:31:31 +01007613 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007614 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007615}
7616
7617static int perf_swevent_init(struct perf_event *event)
7618{
Tommi Rantala8176cce2013-04-13 22:49:14 +03007619 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007620
7621 if (event->attr.type != PERF_TYPE_SOFTWARE)
7622 return -ENOENT;
7623
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007624 /*
7625 * no branch sampling for software events
7626 */
7627 if (has_branch_stack(event))
7628 return -EOPNOTSUPP;
7629
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007630 switch (event_id) {
7631 case PERF_COUNT_SW_CPU_CLOCK:
7632 case PERF_COUNT_SW_TASK_CLOCK:
7633 return -ENOENT;
7634
7635 default:
7636 break;
7637 }
7638
Dan Carpenterce677832010-10-24 21:50:42 +02007639 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007640 return -ENOENT;
7641
7642 if (!event->parent) {
7643 int err;
7644
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007645 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007646 if (err)
7647 return err;
7648
Ingo Molnarc5905af2012-02-24 08:31:31 +01007649 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007650 event->destroy = sw_perf_event_destroy;
7651 }
7652
7653 return 0;
7654}
7655
7656static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007657 .task_ctx_nr = perf_sw_context,
7658
Peter Zijlstra34f43922015-02-20 14:05:38 +01007659 .capabilities = PERF_PMU_CAP_NO_NMI,
7660
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007661 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007662 .add = perf_swevent_add,
7663 .del = perf_swevent_del,
7664 .start = perf_swevent_start,
7665 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007666 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007667};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007668
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007669#ifdef CONFIG_EVENT_TRACING
7670
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007671static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007672 struct perf_sample_data *data)
7673{
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007674 void *record = data->raw->frag.data;
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007675
Peter Zijlstrab71b4372015-11-02 10:50:51 +01007676 /* only top level events have filters set */
7677 if (event->parent)
7678 event = event->parent;
7679
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007680 if (likely(!event->filter) || filter_match_preds(event->filter, record))
7681 return 1;
7682 return 0;
7683}
7684
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007685static int perf_tp_event_match(struct perf_event *event,
7686 struct perf_sample_data *data,
7687 struct pt_regs *regs)
7688{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01007689 if (event->hw.state & PERF_HES_STOPPED)
7690 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02007691 /*
7692 * All tracepoints are from kernel-space.
7693 */
7694 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007695 return 0;
7696
7697 if (!perf_tp_filter_match(event, data))
7698 return 0;
7699
7700 return 1;
7701}
7702
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07007703void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
7704 struct trace_event_call *call, u64 count,
7705 struct pt_regs *regs, struct hlist_head *head,
7706 struct task_struct *task)
7707{
7708 struct bpf_prog *prog = call->prog;
7709
7710 if (prog) {
7711 *(struct pt_regs **)raw_data = regs;
7712 if (!trace_call_bpf(prog, raw_data) || hlist_empty(head)) {
7713 perf_swevent_put_recursion_context(rctx);
7714 return;
7715 }
7716 }
7717 perf_tp_event(call->event.type, count, raw_data, size, regs, head,
7718 rctx, task);
7719}
7720EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
7721
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007722void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007723 struct pt_regs *regs, struct hlist_head *head, int rctx,
7724 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007725{
7726 struct perf_sample_data data;
7727 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007728
7729 struct perf_raw_record raw = {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007730 .frag = {
7731 .size = entry_size,
7732 .data = record,
7733 },
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007734 };
7735
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007736 perf_sample_data_init(&data, 0, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007737 data.raw = &raw;
7738
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007739 perf_trace_buf_update(record, event_type);
7740
Sasha Levinb67bfe02013-02-27 17:06:00 -08007741 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007742 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007743 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007744 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007745
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007746 /*
7747 * If we got specified a target task, also iterate its context and
7748 * deliver this event there too.
7749 */
7750 if (task && task != current) {
7751 struct perf_event_context *ctx;
7752 struct trace_entry *entry = record;
7753
7754 rcu_read_lock();
7755 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
7756 if (!ctx)
7757 goto unlock;
7758
7759 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Jiri Olsa4d083662018-09-23 18:13:43 +02007760 if (event->cpu != smp_processor_id())
7761 continue;
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007762 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7763 continue;
7764 if (event->attr.config != entry->type)
7765 continue;
7766 if (perf_tp_event_match(event, &data, regs))
7767 perf_swevent_event(event, count, &data, regs);
7768 }
7769unlock:
7770 rcu_read_unlock();
7771 }
7772
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007773 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007774}
7775EXPORT_SYMBOL_GPL(perf_tp_event);
7776
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007777static void tp_perf_event_destroy(struct perf_event *event)
7778{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007779 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007780}
7781
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007782static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007783{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007784 int err;
7785
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007786 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7787 return -ENOENT;
7788
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007789 /*
7790 * no branch sampling for tracepoint events
7791 */
7792 if (has_branch_stack(event))
7793 return -EOPNOTSUPP;
7794
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007795 err = perf_trace_init(event);
7796 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007797 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007798
7799 event->destroy = tp_perf_event_destroy;
7800
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007801 return 0;
7802}
7803
7804static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007805 .task_ctx_nr = perf_sw_context,
7806
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007807 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007808 .add = perf_trace_add,
7809 .del = perf_trace_del,
7810 .start = perf_swevent_start,
7811 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007812 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007813};
7814
7815static inline void perf_tp_register(void)
7816{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007817 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007818}
Li Zefan6fb29152009-10-15 11:21:42 +08007819
Li Zefan6fb29152009-10-15 11:21:42 +08007820static void perf_event_free_filter(struct perf_event *event)
7821{
7822 ftrace_profile_free_filter(event);
7823}
7824
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007825#ifdef CONFIG_BPF_SYSCALL
7826static void bpf_overflow_handler(struct perf_event *event,
7827 struct perf_sample_data *data,
7828 struct pt_regs *regs)
7829{
7830 struct bpf_perf_event_data_kern ctx = {
7831 .data = data,
7832 .regs = regs,
7833 };
7834 int ret = 0;
7835
7836 preempt_disable();
7837 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
7838 goto out;
7839 rcu_read_lock();
7840 ret = BPF_PROG_RUN(event->prog, (void *)&ctx);
7841 rcu_read_unlock();
7842out:
7843 __this_cpu_dec(bpf_prog_active);
7844 preempt_enable();
7845 if (!ret)
7846 return;
7847
7848 event->orig_overflow_handler(event, data, regs);
7849}
7850
7851static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
7852{
7853 struct bpf_prog *prog;
7854
7855 if (event->overflow_handler_context)
7856 /* hw breakpoint or kernel counter */
7857 return -EINVAL;
7858
7859 if (event->prog)
7860 return -EEXIST;
7861
7862 prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT);
7863 if (IS_ERR(prog))
7864 return PTR_ERR(prog);
7865
7866 event->prog = prog;
7867 event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
7868 WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
7869 return 0;
7870}
7871
7872static void perf_event_free_bpf_handler(struct perf_event *event)
7873{
7874 struct bpf_prog *prog = event->prog;
7875
7876 if (!prog)
7877 return;
7878
7879 WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
7880 event->prog = NULL;
7881 bpf_prog_put(prog);
7882}
7883#else
7884static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
7885{
7886 return -EOPNOTSUPP;
7887}
7888static void perf_event_free_bpf_handler(struct perf_event *event)
7889{
7890}
7891#endif
7892
Alexei Starovoitov25415172015-03-25 12:49:20 -07007893static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7894{
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007895 bool is_kprobe, is_tracepoint;
Alexei Starovoitov25415172015-03-25 12:49:20 -07007896 struct bpf_prog *prog;
7897
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007898 if (event->attr.type == PERF_TYPE_HARDWARE ||
7899 event->attr.type == PERF_TYPE_SOFTWARE)
7900 return perf_event_set_bpf_handler(event, prog_fd);
7901
Alexei Starovoitov25415172015-03-25 12:49:20 -07007902 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7903 return -EINVAL;
7904
7905 if (event->tp_event->prog)
7906 return -EEXIST;
7907
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007908 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
7909 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
7910 if (!is_kprobe && !is_tracepoint)
7911 /* bpf programs can only be attached to u/kprobe or tracepoint */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007912 return -EINVAL;
7913
7914 prog = bpf_prog_get(prog_fd);
7915 if (IS_ERR(prog))
7916 return PTR_ERR(prog);
7917
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007918 if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
7919 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007920 /* valid fd, but invalid bpf program type */
7921 bpf_prog_put(prog);
7922 return -EINVAL;
7923 }
7924
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07007925 if (is_tracepoint) {
7926 int off = trace_event_get_offsets(event->tp_event);
7927
7928 if (prog->aux->max_ctx_offset > off) {
7929 bpf_prog_put(prog);
7930 return -EACCES;
7931 }
7932 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07007933 event->tp_event->prog = prog;
Yonghong Song0dee5492017-09-18 16:38:36 -07007934 event->tp_event->bpf_prog_owner = event;
Alexei Starovoitov25415172015-03-25 12:49:20 -07007935
7936 return 0;
7937}
7938
7939static void perf_event_free_bpf_prog(struct perf_event *event)
7940{
7941 struct bpf_prog *prog;
7942
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007943 perf_event_free_bpf_handler(event);
7944
Alexei Starovoitov25415172015-03-25 12:49:20 -07007945 if (!event->tp_event)
7946 return;
7947
7948 prog = event->tp_event->prog;
Yonghong Song0dee5492017-09-18 16:38:36 -07007949 if (prog && event->tp_event->bpf_prog_owner == event) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007950 event->tp_event->prog = NULL;
Daniel Borkmann1aacde32016-06-30 17:24:43 +02007951 bpf_prog_put(prog);
Alexei Starovoitov25415172015-03-25 12:49:20 -07007952 }
7953}
7954
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007955#else
Li Zefan6fb29152009-10-15 11:21:42 +08007956
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007957static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007958{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007959}
Li Zefan6fb29152009-10-15 11:21:42 +08007960
Li Zefan6fb29152009-10-15 11:21:42 +08007961static void perf_event_free_filter(struct perf_event *event)
7962{
7963}
7964
Alexei Starovoitov25415172015-03-25 12:49:20 -07007965static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7966{
7967 return -ENOENT;
7968}
7969
7970static void perf_event_free_bpf_prog(struct perf_event *event)
7971{
7972}
Li Zefan07b139c2009-12-21 14:27:35 +08007973#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007974
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007975#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007976void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007977{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007978 struct perf_sample_data sample;
7979 struct pt_regs *regs = data;
7980
Robert Richterfd0d0002012-04-02 20:19:08 +02007981 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007982
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007983 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007984 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007985}
7986#endif
7987
Alexander Shishkin375637b2016-04-27 18:44:46 +03007988/*
7989 * Allocate a new address filter
7990 */
7991static struct perf_addr_filter *
7992perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
7993{
7994 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
7995 struct perf_addr_filter *filter;
7996
7997 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
7998 if (!filter)
7999 return NULL;
8000
8001 INIT_LIST_HEAD(&filter->entry);
8002 list_add_tail(&filter->entry, filters);
8003
8004 return filter;
8005}
8006
8007static void free_filters_list(struct list_head *filters)
8008{
8009 struct perf_addr_filter *filter, *iter;
8010
8011 list_for_each_entry_safe(filter, iter, filters, entry) {
8012 if (filter->inode)
8013 iput(filter->inode);
8014 list_del(&filter->entry);
8015 kfree(filter);
8016 }
8017}
8018
8019/*
8020 * Free existing address filters and optionally install new ones
8021 */
8022static void perf_addr_filters_splice(struct perf_event *event,
8023 struct list_head *head)
8024{
8025 unsigned long flags;
8026 LIST_HEAD(list);
8027
8028 if (!has_addr_filter(event))
8029 return;
8030
8031 /* don't bother with children, they don't have their own filters */
8032 if (event->parent)
8033 return;
8034
8035 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
8036
8037 list_splice_init(&event->addr_filters.list, &list);
8038 if (head)
8039 list_splice(head, &event->addr_filters.list);
8040
8041 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
8042
8043 free_filters_list(&list);
8044}
8045
8046/*
8047 * Scan through mm's vmas and see if one of them matches the
8048 * @filter; if so, adjust filter's address range.
8049 * Called with mm::mmap_sem down for reading.
8050 */
8051static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter,
8052 struct mm_struct *mm)
8053{
8054 struct vm_area_struct *vma;
8055
8056 for (vma = mm->mmap; vma; vma = vma->vm_next) {
8057 struct file *file = vma->vm_file;
8058 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
8059 unsigned long vma_size = vma->vm_end - vma->vm_start;
8060
8061 if (!file)
8062 continue;
8063
8064 if (!perf_addr_filter_match(filter, file, off, vma_size))
8065 continue;
8066
8067 return vma->vm_start;
8068 }
8069
8070 return 0;
8071}
8072
8073/*
8074 * Update event's address range filters based on the
8075 * task's existing mappings, if any.
8076 */
8077static void perf_event_addr_filters_apply(struct perf_event *event)
8078{
8079 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
8080 struct task_struct *task = READ_ONCE(event->ctx->task);
8081 struct perf_addr_filter *filter;
8082 struct mm_struct *mm = NULL;
8083 unsigned int count = 0;
8084 unsigned long flags;
8085
8086 /*
8087 * We may observe TASK_TOMBSTONE, which means that the event tear-down
8088 * will stop on the parent's child_mutex that our caller is also holding
8089 */
8090 if (task == TASK_TOMBSTONE)
8091 return;
8092
8093 mm = get_task_mm(event->ctx->task);
8094 if (!mm)
8095 goto restart;
8096
8097 down_read(&mm->mmap_sem);
8098
8099 raw_spin_lock_irqsave(&ifh->lock, flags);
8100 list_for_each_entry(filter, &ifh->list, entry) {
8101 event->addr_filters_offs[count] = 0;
8102
Mathieu Poirier99f5bc92016-07-18 10:43:07 -06008103 /*
8104 * Adjust base offset if the filter is associated to a binary
8105 * that needs to be mapped:
8106 */
8107 if (filter->inode)
Alexander Shishkin375637b2016-04-27 18:44:46 +03008108 event->addr_filters_offs[count] =
8109 perf_addr_filter_apply(filter, mm);
8110
8111 count++;
8112 }
8113
8114 event->addr_filters_gen++;
8115 raw_spin_unlock_irqrestore(&ifh->lock, flags);
8116
8117 up_read(&mm->mmap_sem);
8118
8119 mmput(mm);
8120
8121restart:
Alexander Shishkin767ae082016-09-06 16:23:49 +03008122 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008123}
8124
8125/*
8126 * Address range filtering: limiting the data to certain
8127 * instruction address ranges. Filters are ioctl()ed to us from
8128 * userspace as ascii strings.
8129 *
8130 * Filter string format:
8131 *
8132 * ACTION RANGE_SPEC
8133 * where ACTION is one of the
8134 * * "filter": limit the trace to this region
8135 * * "start": start tracing from this address
8136 * * "stop": stop tracing at this address/region;
8137 * RANGE_SPEC is
8138 * * for kernel addresses: <start address>[/<size>]
8139 * * for object files: <start address>[/<size>]@</path/to/object/file>
8140 *
8141 * if <size> is not specified, the range is treated as a single address.
8142 */
8143enum {
Alexander Shishkine96271f2016-11-18 13:38:43 +02008144 IF_ACT_NONE = -1,
Alexander Shishkin375637b2016-04-27 18:44:46 +03008145 IF_ACT_FILTER,
8146 IF_ACT_START,
8147 IF_ACT_STOP,
8148 IF_SRC_FILE,
8149 IF_SRC_KERNEL,
8150 IF_SRC_FILEADDR,
8151 IF_SRC_KERNELADDR,
8152};
8153
8154enum {
8155 IF_STATE_ACTION = 0,
8156 IF_STATE_SOURCE,
8157 IF_STATE_END,
8158};
8159
8160static const match_table_t if_tokens = {
8161 { IF_ACT_FILTER, "filter" },
8162 { IF_ACT_START, "start" },
8163 { IF_ACT_STOP, "stop" },
8164 { IF_SRC_FILE, "%u/%u@%s" },
8165 { IF_SRC_KERNEL, "%u/%u" },
8166 { IF_SRC_FILEADDR, "%u@%s" },
8167 { IF_SRC_KERNELADDR, "%u" },
Alexander Shishkine96271f2016-11-18 13:38:43 +02008168 { IF_ACT_NONE, NULL },
Alexander Shishkin375637b2016-04-27 18:44:46 +03008169};
8170
8171/*
8172 * Address filter string parser
8173 */
8174static int
8175perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
8176 struct list_head *filters)
8177{
8178 struct perf_addr_filter *filter = NULL;
8179 char *start, *orig, *filename = NULL;
8180 struct path path;
8181 substring_t args[MAX_OPT_ARGS];
8182 int state = IF_STATE_ACTION, token;
8183 unsigned int kernel = 0;
8184 int ret = -EINVAL;
8185
8186 orig = fstr = kstrdup(fstr, GFP_KERNEL);
8187 if (!fstr)
8188 return -ENOMEM;
8189
8190 while ((start = strsep(&fstr, " ,\n")) != NULL) {
8191 ret = -EINVAL;
8192
8193 if (!*start)
8194 continue;
8195
8196 /* filter definition begins */
8197 if (state == IF_STATE_ACTION) {
8198 filter = perf_addr_filter_new(event, filters);
8199 if (!filter)
8200 goto fail;
8201 }
8202
8203 token = match_token(start, if_tokens, args);
8204 switch (token) {
8205 case IF_ACT_FILTER:
8206 case IF_ACT_START:
8207 filter->filter = 1;
8208
8209 case IF_ACT_STOP:
8210 if (state != IF_STATE_ACTION)
8211 goto fail;
8212
8213 state = IF_STATE_SOURCE;
8214 break;
8215
8216 case IF_SRC_KERNELADDR:
8217 case IF_SRC_KERNEL:
8218 kernel = 1;
8219
8220 case IF_SRC_FILEADDR:
8221 case IF_SRC_FILE:
8222 if (state != IF_STATE_SOURCE)
8223 goto fail;
8224
8225 if (token == IF_SRC_FILE || token == IF_SRC_KERNEL)
8226 filter->range = 1;
8227
8228 *args[0].to = 0;
8229 ret = kstrtoul(args[0].from, 0, &filter->offset);
8230 if (ret)
8231 goto fail;
8232
8233 if (filter->range) {
8234 *args[1].to = 0;
8235 ret = kstrtoul(args[1].from, 0, &filter->size);
8236 if (ret)
8237 goto fail;
8238 }
8239
Mathieu Poirier4059ffd2016-07-18 10:43:05 -06008240 if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) {
8241 int fpos = filter->range ? 2 : 1;
8242
8243 filename = match_strdup(&args[fpos]);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008244 if (!filename) {
8245 ret = -ENOMEM;
8246 goto fail;
8247 }
8248 }
8249
8250 state = IF_STATE_END;
8251 break;
8252
8253 default:
8254 goto fail;
8255 }
8256
8257 /*
8258 * Filter definition is fully parsed, validate and install it.
8259 * Make sure that it doesn't contradict itself or the event's
8260 * attribute.
8261 */
8262 if (state == IF_STATE_END) {
8263 if (kernel && event->attr.exclude_kernel)
8264 goto fail;
8265
8266 if (!kernel) {
8267 if (!filename)
8268 goto fail;
8269
8270 /* look up the path and grab its inode */
8271 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
8272 if (ret)
8273 goto fail_free_name;
8274
8275 filter->inode = igrab(d_inode(path.dentry));
8276 path_put(&path);
8277 kfree(filename);
8278 filename = NULL;
8279
8280 ret = -EINVAL;
8281 if (!filter->inode ||
8282 !S_ISREG(filter->inode->i_mode))
8283 /* free_filters_list() will iput() */
8284 goto fail;
8285 }
8286
8287 /* ready to consume more filters */
8288 state = IF_STATE_ACTION;
8289 filter = NULL;
8290 }
8291 }
8292
8293 if (state != IF_STATE_ACTION)
8294 goto fail;
8295
8296 kfree(orig);
8297
8298 return 0;
8299
8300fail_free_name:
8301 kfree(filename);
8302fail:
8303 free_filters_list(filters);
8304 kfree(orig);
8305
8306 return ret;
8307}
8308
8309static int
8310perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
8311{
8312 LIST_HEAD(filters);
8313 int ret;
8314
8315 /*
8316 * Since this is called in perf_ioctl() path, we're already holding
8317 * ctx::mutex.
8318 */
8319 lockdep_assert_held(&event->ctx->mutex);
8320
8321 if (WARN_ON_ONCE(event->parent))
8322 return -EINVAL;
8323
8324 /*
8325 * For now, we only support filtering in per-task events; doing so
8326 * for CPU-wide events requires additional context switching trickery,
8327 * since same object code will be mapped at different virtual
8328 * addresses in different processes.
8329 */
8330 if (!event->ctx->task)
8331 return -EOPNOTSUPP;
8332
8333 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
8334 if (ret)
8335 return ret;
8336
8337 ret = event->pmu->addr_filters_validate(&filters);
8338 if (ret) {
8339 free_filters_list(&filters);
8340 return ret;
8341 }
8342
8343 /* remove existing filters, if any */
8344 perf_addr_filters_splice(event, &filters);
8345
8346 /* install new filters */
8347 perf_event_for_each_child(event, perf_event_addr_filters_apply);
8348
8349 return ret;
8350}
8351
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008352static int perf_event_set_filter(struct perf_event *event, void __user *arg)
8353{
8354 char *filter_str;
8355 int ret = -EINVAL;
8356
Alexander Shishkin375637b2016-04-27 18:44:46 +03008357 if ((event->attr.type != PERF_TYPE_TRACEPOINT ||
8358 !IS_ENABLED(CONFIG_EVENT_TRACING)) &&
8359 !has_addr_filter(event))
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008360 return -EINVAL;
8361
8362 filter_str = strndup_user(arg, PAGE_SIZE);
8363 if (IS_ERR(filter_str))
8364 return PTR_ERR(filter_str);
8365
8366 if (IS_ENABLED(CONFIG_EVENT_TRACING) &&
8367 event->attr.type == PERF_TYPE_TRACEPOINT)
8368 ret = ftrace_profile_set_filter(event, event->attr.config,
8369 filter_str);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008370 else if (has_addr_filter(event))
8371 ret = perf_event_set_addr_filter(event, filter_str);
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008372
8373 kfree(filter_str);
8374 return ret;
8375}
8376
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008377/*
8378 * hrtimer based swevent callback
8379 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008380
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008381static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008382{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008383 enum hrtimer_restart ret = HRTIMER_RESTART;
8384 struct perf_sample_data data;
8385 struct pt_regs *regs;
8386 struct perf_event *event;
8387 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008388
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008389 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008390
8391 if (event->state != PERF_EVENT_STATE_ACTIVE)
8392 return HRTIMER_NORESTART;
8393
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008394 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008395
Robert Richterfd0d0002012-04-02 20:19:08 +02008396 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008397 regs = get_irq_regs();
8398
8399 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08008400 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02008401 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008402 ret = HRTIMER_NORESTART;
8403 }
8404
8405 period = max_t(u64, 10000, event->hw.sample_period);
8406 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
8407
8408 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008409}
8410
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008411static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008412{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008413 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008414 s64 period;
8415
8416 if (!is_sampling_event(event))
8417 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008418
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008419 period = local64_read(&hwc->period_left);
8420 if (period) {
8421 if (period < 0)
8422 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008423
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008424 local64_set(&hwc->period_left, 0);
8425 } else {
8426 period = max_t(u64, 10000, hwc->sample_period);
8427 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00008428 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
8429 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008430}
8431
8432static void perf_swevent_cancel_hrtimer(struct perf_event *event)
8433{
8434 struct hw_perf_event *hwc = &event->hw;
8435
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01008436 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008437 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008438 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008439
8440 hrtimer_cancel(&hwc->hrtimer);
8441 }
8442}
8443
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008444static void perf_swevent_init_hrtimer(struct perf_event *event)
8445{
8446 struct hw_perf_event *hwc = &event->hw;
8447
8448 if (!is_sampling_event(event))
8449 return;
8450
8451 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
8452 hwc->hrtimer.function = perf_swevent_hrtimer;
8453
8454 /*
8455 * Since hrtimers have a fixed rate, we can do a static freq->period
8456 * mapping and avoid the whole period adjust feedback stuff.
8457 */
8458 if (event->attr.freq) {
8459 long freq = event->attr.sample_freq;
8460
8461 event->attr.sample_period = NSEC_PER_SEC / freq;
8462 hwc->sample_period = event->attr.sample_period;
8463 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09008464 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008465 event->attr.freq = 0;
8466 }
8467}
8468
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008469/*
8470 * Software event: cpu wall time clock
8471 */
8472
8473static void cpu_clock_event_update(struct perf_event *event)
8474{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008475 s64 prev;
8476 u64 now;
8477
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008478 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008479 prev = local64_xchg(&event->hw.prev_count, now);
8480 local64_add(now - prev, &event->count);
8481}
8482
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008483static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008484{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008485 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008486 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008487}
8488
8489static void cpu_clock_event_stop(struct perf_event *event, int flags)
8490{
8491 perf_swevent_cancel_hrtimer(event);
8492 cpu_clock_event_update(event);
8493}
8494
8495static int cpu_clock_event_add(struct perf_event *event, int flags)
8496{
8497 if (flags & PERF_EF_START)
8498 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008499 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008500
8501 return 0;
8502}
8503
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008504static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008505{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008506 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008507}
8508
8509static void cpu_clock_event_read(struct perf_event *event)
8510{
8511 cpu_clock_event_update(event);
8512}
8513
8514static int cpu_clock_event_init(struct perf_event *event)
8515{
8516 if (event->attr.type != PERF_TYPE_SOFTWARE)
8517 return -ENOENT;
8518
8519 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
8520 return -ENOENT;
8521
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008522 /*
8523 * no branch sampling for software events
8524 */
8525 if (has_branch_stack(event))
8526 return -EOPNOTSUPP;
8527
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008528 perf_swevent_init_hrtimer(event);
8529
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008530 return 0;
8531}
8532
8533static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008534 .task_ctx_nr = perf_sw_context,
8535
Peter Zijlstra34f43922015-02-20 14:05:38 +01008536 .capabilities = PERF_PMU_CAP_NO_NMI,
8537
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008538 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008539 .add = cpu_clock_event_add,
8540 .del = cpu_clock_event_del,
8541 .start = cpu_clock_event_start,
8542 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008543 .read = cpu_clock_event_read,
8544};
8545
8546/*
8547 * Software event: task time clock
8548 */
8549
8550static void task_clock_event_update(struct perf_event *event, u64 now)
8551{
8552 u64 prev;
8553 s64 delta;
8554
8555 prev = local64_xchg(&event->hw.prev_count, now);
8556 delta = now - prev;
8557 local64_add(delta, &event->count);
8558}
8559
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008560static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008561{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008562 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008563 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008564}
8565
8566static void task_clock_event_stop(struct perf_event *event, int flags)
8567{
8568 perf_swevent_cancel_hrtimer(event);
8569 task_clock_event_update(event, event->ctx->time);
8570}
8571
8572static int task_clock_event_add(struct perf_event *event, int flags)
8573{
8574 if (flags & PERF_EF_START)
8575 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008576 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008577
8578 return 0;
8579}
8580
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008581static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008582{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008583 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008584}
8585
8586static void task_clock_event_read(struct perf_event *event)
8587{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01008588 u64 now = perf_clock();
8589 u64 delta = now - event->ctx->timestamp;
8590 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008591
8592 task_clock_event_update(event, time);
8593}
8594
8595static int task_clock_event_init(struct perf_event *event)
8596{
8597 if (event->attr.type != PERF_TYPE_SOFTWARE)
8598 return -ENOENT;
8599
8600 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
8601 return -ENOENT;
8602
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008603 /*
8604 * no branch sampling for software events
8605 */
8606 if (has_branch_stack(event))
8607 return -EOPNOTSUPP;
8608
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008609 perf_swevent_init_hrtimer(event);
8610
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008611 return 0;
8612}
8613
8614static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008615 .task_ctx_nr = perf_sw_context,
8616
Peter Zijlstra34f43922015-02-20 14:05:38 +01008617 .capabilities = PERF_PMU_CAP_NO_NMI,
8618
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008619 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008620 .add = task_clock_event_add,
8621 .del = task_clock_event_del,
8622 .start = task_clock_event_start,
8623 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008624 .read = task_clock_event_read,
8625};
8626
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008627static void perf_pmu_nop_void(struct pmu *pmu)
8628{
8629}
8630
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008631static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
8632{
8633}
8634
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008635static int perf_pmu_nop_int(struct pmu *pmu)
8636{
8637 return 0;
8638}
8639
Jiri Olsae62e3b62019-02-04 13:35:32 +01008640static int perf_event_nop_int(struct perf_event *event, u64 value)
8641{
8642 return 0;
8643}
8644
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008645static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008646
8647static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008648{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008649 __this_cpu_write(nop_txn_flags, flags);
8650
8651 if (flags & ~PERF_PMU_TXN_ADD)
8652 return;
8653
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008654 perf_pmu_disable(pmu);
8655}
8656
8657static int perf_pmu_commit_txn(struct pmu *pmu)
8658{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008659 unsigned int flags = __this_cpu_read(nop_txn_flags);
8660
8661 __this_cpu_write(nop_txn_flags, 0);
8662
8663 if (flags & ~PERF_PMU_TXN_ADD)
8664 return 0;
8665
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008666 perf_pmu_enable(pmu);
8667 return 0;
8668}
8669
8670static void perf_pmu_cancel_txn(struct pmu *pmu)
8671{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008672 unsigned int flags = __this_cpu_read(nop_txn_flags);
8673
8674 __this_cpu_write(nop_txn_flags, 0);
8675
8676 if (flags & ~PERF_PMU_TXN_ADD)
8677 return;
8678
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008679 perf_pmu_enable(pmu);
8680}
8681
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008682static int perf_event_idx_default(struct perf_event *event)
8683{
Peter Zijlstrac719f562014-10-21 11:10:21 +02008684 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008685}
8686
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008687/*
8688 * Ensures all contexts with the same task_ctx_nr have the same
8689 * pmu_cpu_context too.
8690 */
Mark Rutland9e317042014-02-10 17:44:18 +00008691static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008692{
8693 struct pmu *pmu;
8694
8695 if (ctxn < 0)
8696 return NULL;
8697
8698 list_for_each_entry(pmu, &pmus, entry) {
8699 if (pmu->task_ctx_nr == ctxn)
8700 return pmu->pmu_cpu_context;
8701 }
8702
8703 return NULL;
8704}
8705
Peter Zijlstra51676952010-12-07 14:18:20 +01008706static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008707{
Peter Zijlstra51676952010-12-07 14:18:20 +01008708 int cpu;
8709
8710 for_each_possible_cpu(cpu) {
8711 struct perf_cpu_context *cpuctx;
8712
8713 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8714
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008715 if (cpuctx->unique_pmu == old_pmu)
8716 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01008717 }
8718}
8719
8720static void free_pmu_context(struct pmu *pmu)
8721{
8722 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008723
8724 mutex_lock(&pmus_lock);
8725 /*
8726 * Like a real lame refcount.
8727 */
Peter Zijlstra51676952010-12-07 14:18:20 +01008728 list_for_each_entry(i, &pmus, entry) {
8729 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
8730 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008731 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01008732 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008733 }
8734
Peter Zijlstra51676952010-12-07 14:18:20 +01008735 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008736out:
8737 mutex_unlock(&pmus_lock);
8738}
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008739
8740/*
8741 * Let userspace know that this PMU supports address range filtering:
8742 */
8743static ssize_t nr_addr_filters_show(struct device *dev,
8744 struct device_attribute *attr,
8745 char *page)
8746{
8747 struct pmu *pmu = dev_get_drvdata(dev);
8748
8749 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
8750}
8751DEVICE_ATTR_RO(nr_addr_filters);
8752
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008753static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008754
Peter Zijlstraabe43402010-11-17 23:17:37 +01008755static ssize_t
8756type_show(struct device *dev, struct device_attribute *attr, char *page)
8757{
8758 struct pmu *pmu = dev_get_drvdata(dev);
8759
8760 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
8761}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008762static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008763
Stephane Eranian62b85632013-04-03 14:21:34 +02008764static ssize_t
8765perf_event_mux_interval_ms_show(struct device *dev,
8766 struct device_attribute *attr,
8767 char *page)
8768{
8769 struct pmu *pmu = dev_get_drvdata(dev);
8770
8771 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
8772}
8773
Peter Zijlstra272325c2015-04-15 11:41:58 +02008774static DEFINE_MUTEX(mux_interval_mutex);
8775
Stephane Eranian62b85632013-04-03 14:21:34 +02008776static ssize_t
8777perf_event_mux_interval_ms_store(struct device *dev,
8778 struct device_attribute *attr,
8779 const char *buf, size_t count)
8780{
8781 struct pmu *pmu = dev_get_drvdata(dev);
8782 int timer, cpu, ret;
8783
8784 ret = kstrtoint(buf, 0, &timer);
8785 if (ret)
8786 return ret;
8787
8788 if (timer < 1)
8789 return -EINVAL;
8790
8791 /* same value, noting to do */
8792 if (timer == pmu->hrtimer_interval_ms)
8793 return count;
8794
Peter Zijlstra272325c2015-04-15 11:41:58 +02008795 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008796 pmu->hrtimer_interval_ms = timer;
8797
8798 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02008799 get_online_cpus();
8800 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02008801 struct perf_cpu_context *cpuctx;
8802 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8803 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
8804
Peter Zijlstra272325c2015-04-15 11:41:58 +02008805 cpu_function_call(cpu,
8806 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02008807 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02008808 put_online_cpus();
8809 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008810
8811 return count;
8812}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008813static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02008814
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008815static struct attribute *pmu_dev_attrs[] = {
8816 &dev_attr_type.attr,
8817 &dev_attr_perf_event_mux_interval_ms.attr,
8818 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008819};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008820ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008821
8822static int pmu_bus_running;
8823static struct bus_type pmu_bus = {
8824 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008825 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008826};
8827
8828static void pmu_dev_release(struct device *dev)
8829{
8830 kfree(dev);
8831}
8832
8833static int pmu_dev_alloc(struct pmu *pmu)
8834{
8835 int ret = -ENOMEM;
8836
8837 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
8838 if (!pmu->dev)
8839 goto out;
8840
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01008841 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01008842 device_initialize(pmu->dev);
8843 ret = dev_set_name(pmu->dev, "%s", pmu->name);
8844 if (ret)
8845 goto free_dev;
8846
8847 dev_set_drvdata(pmu->dev, pmu);
8848 pmu->dev->bus = &pmu_bus;
8849 pmu->dev->release = pmu_dev_release;
8850 ret = device_add(pmu->dev);
8851 if (ret)
8852 goto free_dev;
8853
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008854 /* For PMUs with address filters, throw in an extra attribute: */
8855 if (pmu->nr_addr_filters)
8856 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
8857
8858 if (ret)
8859 goto del_dev;
8860
Peter Zijlstraabe43402010-11-17 23:17:37 +01008861out:
8862 return ret;
8863
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008864del_dev:
8865 device_del(pmu->dev);
8866
Peter Zijlstraabe43402010-11-17 23:17:37 +01008867free_dev:
8868 put_device(pmu->dev);
8869 goto out;
8870}
8871
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008872static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008873static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008874
Mischa Jonker03d8e802013-06-04 11:45:48 +02008875int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008876{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008877 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008878
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008879 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008880 ret = -ENOMEM;
8881 pmu->pmu_disable_count = alloc_percpu(int);
8882 if (!pmu->pmu_disable_count)
8883 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008884
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008885 pmu->type = -1;
8886 if (!name)
8887 goto skip_type;
8888 pmu->name = name;
8889
8890 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08008891 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
8892 if (type < 0) {
8893 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008894 goto free_pdc;
8895 }
8896 }
8897 pmu->type = type;
8898
Peter Zijlstraabe43402010-11-17 23:17:37 +01008899 if (pmu_bus_running) {
8900 ret = pmu_dev_alloc(pmu);
8901 if (ret)
8902 goto free_idr;
8903 }
8904
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008905skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +01008906 if (pmu->task_ctx_nr == perf_hw_context) {
8907 static int hw_context_taken = 0;
8908
Mark Rutland5101ef22016-04-26 11:33:46 +01008909 /*
8910 * Other than systems with heterogeneous CPUs, it never makes
8911 * sense for two PMUs to share perf_hw_context. PMUs which are
8912 * uncore must use perf_invalid_context.
8913 */
8914 if (WARN_ON_ONCE(hw_context_taken &&
8915 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
Peter Zijlstra26657842016-03-22 22:09:18 +01008916 pmu->task_ctx_nr = perf_invalid_context;
8917
8918 hw_context_taken = 1;
8919 }
8920
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008921 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
8922 if (pmu->pmu_cpu_context)
8923 goto got_cpu_context;
8924
Wei Yongjunc4814202013-04-12 11:05:54 +08008925 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008926 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
8927 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01008928 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008929
8930 for_each_possible_cpu(cpu) {
8931 struct perf_cpu_context *cpuctx;
8932
8933 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02008934 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008935 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008936 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008937 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02008938
Peter Zijlstra272325c2015-04-15 11:41:58 +02008939 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02008940
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008941 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008942 }
8943
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008944got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008945 if (!pmu->start_txn) {
8946 if (pmu->pmu_enable) {
8947 /*
8948 * If we have pmu_enable/pmu_disable calls, install
8949 * transaction stubs that use that to try and batch
8950 * hardware accesses.
8951 */
8952 pmu->start_txn = perf_pmu_start_txn;
8953 pmu->commit_txn = perf_pmu_commit_txn;
8954 pmu->cancel_txn = perf_pmu_cancel_txn;
8955 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008956 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008957 pmu->commit_txn = perf_pmu_nop_int;
8958 pmu->cancel_txn = perf_pmu_nop_void;
8959 }
8960 }
8961
8962 if (!pmu->pmu_enable) {
8963 pmu->pmu_enable = perf_pmu_nop_void;
8964 pmu->pmu_disable = perf_pmu_nop_void;
8965 }
8966
Jiri Olsae62e3b62019-02-04 13:35:32 +01008967 if (!pmu->check_period)
8968 pmu->check_period = perf_event_nop_int;
8969
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008970 if (!pmu->event_idx)
8971 pmu->event_idx = perf_event_idx_default;
8972
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008973 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008974 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008975 ret = 0;
8976unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008977 mutex_unlock(&pmus_lock);
8978
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008979 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008980
Peter Zijlstraabe43402010-11-17 23:17:37 +01008981free_dev:
8982 device_del(pmu->dev);
8983 put_device(pmu->dev);
8984
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008985free_idr:
8986 if (pmu->type >= PERF_TYPE_MAX)
8987 idr_remove(&pmu_idr, pmu->type);
8988
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008989free_pdc:
8990 free_percpu(pmu->pmu_disable_count);
8991 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008992}
Yan, Zhengc464c762014-03-18 16:56:41 +08008993EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008994
8995void perf_pmu_unregister(struct pmu *pmu)
8996{
Jiri Olsa09338402016-10-20 13:10:11 +02008997 int remove_device;
8998
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008999 mutex_lock(&pmus_lock);
Jiri Olsa09338402016-10-20 13:10:11 +02009000 remove_device = pmu_bus_running;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009001 list_del_rcu(&pmu->entry);
9002 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009003
9004 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02009005 * We dereference the pmu list under both SRCU and regular RCU, so
9006 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009007 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009008 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02009009 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009010
Peter Zijlstra33696fc2010-06-14 08:49:00 +02009011 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009012 if (pmu->type >= PERF_TYPE_MAX)
9013 idr_remove(&pmu_idr, pmu->type);
Jiri Olsa09338402016-10-20 13:10:11 +02009014 if (remove_device) {
9015 if (pmu->nr_addr_filters)
9016 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
9017 device_del(pmu->dev);
9018 put_device(pmu->dev);
9019 }
Peter Zijlstra51676952010-12-07 14:18:20 +01009020 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009021}
Yan, Zhengc464c762014-03-18 16:56:41 +08009022EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009023
Mark Rutlandcc34b982015-01-07 14:56:51 +00009024static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
9025{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009026 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00009027 int ret;
9028
9029 if (!try_module_get(pmu->module))
9030 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009031
9032 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02009033 /*
9034 * This ctx->mutex can nest when we're called through
9035 * inheritance. See the perf_event_ctx_lock_nested() comment.
9036 */
9037 ctx = perf_event_ctx_lock_nested(event->group_leader,
9038 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009039 BUG_ON(!ctx);
9040 }
9041
Mark Rutlandcc34b982015-01-07 14:56:51 +00009042 event->pmu = pmu;
9043 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009044
9045 if (ctx)
9046 perf_event_ctx_unlock(event->group_leader, ctx);
9047
Mark Rutlandcc34b982015-01-07 14:56:51 +00009048 if (ret)
9049 module_put(pmu->module);
9050
9051 return ret;
9052}
9053
Geliang Tang18ab2cd2015-09-27 23:25:50 +08009054static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009055{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02009056 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009057 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08009058 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009059
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009060 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009061
9062 rcu_read_lock();
9063 pmu = idr_find(&pmu_idr, event->attr.type);
9064 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08009065 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00009066 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08009067 if (ret)
9068 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009069 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08009070 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009071
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009072 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00009073 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009074 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009075 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009076
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009077 if (ret != -ENOENT) {
9078 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009079 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009080 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009081 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009082 pmu = ERR_PTR(-ENOENT);
9083unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009084 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009085
9086 return pmu;
9087}
9088
Kan Liangf2fb6be2016-03-23 11:24:37 -07009089static void attach_sb_event(struct perf_event *event)
9090{
9091 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
9092
9093 raw_spin_lock(&pel->lock);
9094 list_add_rcu(&event->sb_list, &pel->list);
9095 raw_spin_unlock(&pel->lock);
9096}
9097
Peter Zijlstraaab5b712016-05-12 17:26:46 +02009098/*
9099 * We keep a list of all !task (and therefore per-cpu) events
9100 * that need to receive side-band records.
9101 *
9102 * This avoids having to scan all the various PMU per-cpu contexts
9103 * looking for them.
9104 */
Kan Liangf2fb6be2016-03-23 11:24:37 -07009105static void account_pmu_sb_event(struct perf_event *event)
9106{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07009107 if (is_sb_event(event))
Kan Liangf2fb6be2016-03-23 11:24:37 -07009108 attach_sb_event(event);
9109}
9110
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009111static void account_event_cpu(struct perf_event *event, int cpu)
9112{
9113 if (event->parent)
9114 return;
9115
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009116 if (is_cgroup_event(event))
9117 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
9118}
9119
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02009120/* Freq events need the tick to stay alive (see perf_event_task_tick). */
9121static void account_freq_event_nohz(void)
9122{
9123#ifdef CONFIG_NO_HZ_FULL
9124 /* Lock so we don't race with concurrent unaccount */
9125 spin_lock(&nr_freq_lock);
9126 if (atomic_inc_return(&nr_freq_events) == 1)
9127 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
9128 spin_unlock(&nr_freq_lock);
9129#endif
9130}
9131
9132static void account_freq_event(void)
9133{
9134 if (tick_nohz_full_enabled())
9135 account_freq_event_nohz();
9136 else
9137 atomic_inc(&nr_freq_events);
9138}
9139
9140
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009141static void account_event(struct perf_event *event)
9142{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009143 bool inc = false;
9144
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009145 if (event->parent)
9146 return;
9147
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009148 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009149 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009150 if (event->attr.mmap || event->attr.mmap_data)
9151 atomic_inc(&nr_mmap_events);
9152 if (event->attr.comm)
9153 atomic_inc(&nr_comm_events);
9154 if (event->attr.task)
9155 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02009156 if (event->attr.freq)
9157 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03009158 if (event->attr.context_switch) {
9159 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009160 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03009161 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009162 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009163 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009164 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009165 inc = true;
9166
Peter Zijlstra9107c892016-02-24 18:45:45 +01009167 if (inc) {
9168 if (atomic_inc_not_zero(&perf_sched_count))
9169 goto enabled;
9170
9171 mutex_lock(&perf_sched_mutex);
9172 if (!atomic_read(&perf_sched_count)) {
9173 static_branch_enable(&perf_sched_events);
9174 /*
9175 * Guarantee that all CPUs observe they key change and
9176 * call the perf scheduling hooks before proceeding to
9177 * install events that need them.
9178 */
9179 synchronize_sched();
9180 }
9181 /*
9182 * Now that we have waited for the sync_sched(), allow further
9183 * increments to by-pass the mutex.
9184 */
9185 atomic_inc(&perf_sched_count);
9186 mutex_unlock(&perf_sched_mutex);
9187 }
9188enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009189
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009190 account_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07009191
9192 account_pmu_sb_event(event);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009193}
9194
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009195/*
9196 * Allocate and initialize a event structure
9197 */
9198static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009199perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009200 struct task_struct *task,
9201 struct perf_event *group_leader,
9202 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03009203 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00009204 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009205{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02009206 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009207 struct perf_event *event;
9208 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009209 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009210
Oleg Nesterov66832eb2011-01-18 17:10:32 +01009211 if ((unsigned)cpu >= nr_cpu_ids) {
9212 if (!task || cpu != -1)
9213 return ERR_PTR(-EINVAL);
9214 }
9215
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009216 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009217 if (!event)
9218 return ERR_PTR(-ENOMEM);
9219
9220 /*
9221 * Single events are their own group leaders, with an
9222 * empty sibling list:
9223 */
9224 if (!group_leader)
9225 group_leader = event;
9226
9227 mutex_init(&event->child_mutex);
9228 INIT_LIST_HEAD(&event->child_list);
9229
9230 INIT_LIST_HEAD(&event->group_entry);
9231 INIT_LIST_HEAD(&event->event_entry);
9232 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01009233 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01009234 INIT_LIST_HEAD(&event->active_entry);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009235 INIT_LIST_HEAD(&event->addr_filters.list);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01009236 INIT_HLIST_NODE(&event->hlist_entry);
9237
Peter Zijlstra10c6db12011-11-26 02:47:31 +01009238
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009239 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08009240 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009241
9242 mutex_init(&event->mmap_mutex);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009243 raw_spin_lock_init(&event->addr_filters.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009244
Al Viroa6fa9412012-08-20 14:59:25 +01009245 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009246 event->cpu = cpu;
9247 event->attr = *attr;
9248 event->group_leader = group_leader;
9249 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009250 event->oncpu = -1;
9251
9252 event->parent = parent_event;
9253
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08009254 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009255 event->id = atomic64_inc_return(&perf_event_id);
9256
9257 event->state = PERF_EVENT_STATE_INACTIVE;
9258
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009259 if (task) {
9260 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009261 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01009262 * XXX pmu::event_init needs to know what task to account to
9263 * and we cannot use the ctx information because we need the
9264 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009265 */
Prashant Bholeb951ffb2018-04-09 19:03:46 +09009266 get_task_struct(task);
Peter Zijlstra50f16a82015-03-05 22:10:19 +01009267 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009268 }
9269
Peter Zijlstra34f43922015-02-20 14:05:38 +01009270 event->clock = &local_clock;
9271 if (parent_event)
9272 event->clock = parent_event->clock;
9273
Avi Kivity4dc0da82011-06-29 18:42:35 +03009274 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01009275 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03009276 context = parent_event->overflow_handler_context;
Arnd Bergmannf1e4ba52016-09-06 15:10:22 +02009277#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009278 if (overflow_handler == bpf_overflow_handler) {
9279 struct bpf_prog *prog = bpf_prog_inc(parent_event->prog);
9280
9281 if (IS_ERR(prog)) {
9282 err = PTR_ERR(prog);
9283 goto err_ns;
9284 }
9285 event->prog = prog;
9286 event->orig_overflow_handler =
9287 parent_event->orig_overflow_handler;
9288 }
9289#endif
Avi Kivity4dc0da82011-06-29 18:42:35 +03009290 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01009291
Wang Nan18794452016-03-28 06:41:30 +00009292 if (overflow_handler) {
9293 event->overflow_handler = overflow_handler;
9294 event->overflow_handler_context = context;
Wang Nan9ecda412016-04-05 14:11:18 +00009295 } else if (is_write_backward(event)){
9296 event->overflow_handler = perf_event_output_backward;
9297 event->overflow_handler_context = NULL;
Wang Nan18794452016-03-28 06:41:30 +00009298 } else {
Wang Nan9ecda412016-04-05 14:11:18 +00009299 event->overflow_handler = perf_event_output_forward;
Wang Nan18794452016-03-28 06:41:30 +00009300 event->overflow_handler_context = NULL;
9301 }
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02009302
Jiri Olsa0231bb52013-02-01 11:23:45 +01009303 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009304
9305 pmu = NULL;
9306
9307 hwc = &event->hw;
9308 hwc->sample_period = attr->sample_period;
9309 if (attr->freq && attr->sample_freq)
9310 hwc->sample_period = 1;
9311 hwc->last_period = hwc->sample_period;
9312
Peter Zijlstrae7850592010-05-21 14:43:08 +02009313 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009314
9315 /*
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02009316 * We currently do not support PERF_SAMPLE_READ on inherited events.
9317 * See perf_output_read().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009318 */
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02009319 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009320 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009321
Yan, Zhenga46a2302014-11-04 21:56:06 -05009322 if (!has_branch_stack(event))
9323 event->attr.branch_sample_type = 0;
9324
Matt Fleming79dff512015-01-23 18:45:42 +00009325 if (cgroup_fd != -1) {
9326 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
9327 if (err)
9328 goto err_ns;
9329 }
9330
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009331 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009332 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009333 goto err_ns;
9334 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009335 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009336 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009337 }
9338
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009339 err = exclusive_event_init(event);
9340 if (err)
9341 goto err_pmu;
9342
Alexander Shishkin375637b2016-04-27 18:44:46 +03009343 if (has_addr_filter(event)) {
9344 event->addr_filters_offs = kcalloc(pmu->nr_addr_filters,
9345 sizeof(unsigned long),
9346 GFP_KERNEL);
Dan Carpenterf02bfec2017-05-22 12:04:18 +03009347 if (!event->addr_filters_offs) {
9348 err = -ENOMEM;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009349 goto err_per_task;
Dan Carpenterf02bfec2017-05-22 12:04:18 +03009350 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009351
9352 /* force hw sync on the address filters */
9353 event->addr_filters_gen = 1;
9354 }
9355
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009356 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02009357 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009358 err = get_callchain_buffers(attr->sample_max_stack);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009359 if (err)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009360 goto err_addr_filters;
Stephane Eraniand010b332012-02-09 23:21:00 +01009361 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009362 }
9363
Alexander Shishkin927a5572016-03-02 13:24:14 +02009364 /* symmetric to unaccount_event() in _free_event() */
9365 account_event(event);
9366
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009367 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009368
Alexander Shishkin375637b2016-04-27 18:44:46 +03009369err_addr_filters:
9370 kfree(event->addr_filters_offs);
9371
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009372err_per_task:
9373 exclusive_event_destroy(event);
9374
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009375err_pmu:
9376 if (event->destroy)
9377 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08009378 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009379err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00009380 if (is_cgroup_event(event))
9381 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009382 if (event->ns)
9383 put_pid_ns(event->ns);
Prashant Bholeb951ffb2018-04-09 19:03:46 +09009384 if (event->hw.target)
9385 put_task_struct(event->hw.target);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009386 kfree(event);
9387
9388 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009389}
9390
9391static int perf_copy_attr(struct perf_event_attr __user *uattr,
9392 struct perf_event_attr *attr)
9393{
9394 u32 size;
9395 int ret;
9396
9397 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
9398 return -EFAULT;
9399
9400 /*
9401 * zero the full structure, so that a short copy will be nice.
9402 */
9403 memset(attr, 0, sizeof(*attr));
9404
9405 ret = get_user(size, &uattr->size);
9406 if (ret)
9407 return ret;
9408
9409 if (size > PAGE_SIZE) /* silly large */
9410 goto err_size;
9411
9412 if (!size) /* abi compat */
9413 size = PERF_ATTR_SIZE_VER0;
9414
9415 if (size < PERF_ATTR_SIZE_VER0)
9416 goto err_size;
9417
9418 /*
9419 * If we're handed a bigger struct than we know of,
9420 * ensure all the unknown bits are 0 - i.e. new
9421 * user-space does not rely on any kernel feature
9422 * extensions we dont know about yet.
9423 */
9424 if (size > sizeof(*attr)) {
9425 unsigned char __user *addr;
9426 unsigned char __user *end;
9427 unsigned char val;
9428
9429 addr = (void __user *)uattr + sizeof(*attr);
9430 end = (void __user *)uattr + size;
9431
9432 for (; addr < end; addr++) {
9433 ret = get_user(val, addr);
9434 if (ret)
9435 return ret;
9436 if (val)
9437 goto err_size;
9438 }
9439 size = sizeof(*attr);
9440 }
9441
9442 ret = copy_from_user(attr, uattr, size);
9443 if (ret)
9444 return -EFAULT;
9445
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05309446 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009447 return -EINVAL;
9448
9449 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
9450 return -EINVAL;
9451
9452 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
9453 return -EINVAL;
9454
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009455 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
9456 u64 mask = attr->branch_sample_type;
9457
9458 /* only using defined bits */
9459 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
9460 return -EINVAL;
9461
9462 /* at least one branch bit must be set */
9463 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
9464 return -EINVAL;
9465
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009466 /* propagate priv level, when not set for branch */
9467 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
9468
9469 /* exclude_kernel checked on syscall entry */
9470 if (!attr->exclude_kernel)
9471 mask |= PERF_SAMPLE_BRANCH_KERNEL;
9472
9473 if (!attr->exclude_user)
9474 mask |= PERF_SAMPLE_BRANCH_USER;
9475
9476 if (!attr->exclude_hv)
9477 mask |= PERF_SAMPLE_BRANCH_HV;
9478 /*
9479 * adjust user setting (for HW filter setup)
9480 */
9481 attr->branch_sample_type = mask;
9482 }
Stephane Eraniane7122092013-06-06 11:02:04 +02009483 /* privileged levels capture (kernel, hv): check permissions */
9484 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02009485 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9486 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009487 }
Jiri Olsa40189942012-08-07 15:20:37 +02009488
Jiri Olsac5ebced2012-08-07 15:20:40 +02009489 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02009490 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02009491 if (ret)
9492 return ret;
9493 }
9494
9495 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
9496 if (!arch_perf_have_user_stack_dump())
9497 return -ENOSYS;
9498
9499 /*
9500 * We have __u32 type for the size, but so far
9501 * we can only use __u16 as maximum due to the
9502 * __u16 sample size limit.
9503 */
9504 if (attr->sample_stack_user >= USHRT_MAX)
Jiri Olsa9acdfe42018-04-15 11:23:50 +02009505 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +02009506 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
Jiri Olsa9acdfe42018-04-15 11:23:50 +02009507 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +02009508 }
Jiri Olsa40189942012-08-07 15:20:37 +02009509
Stephane Eranian60e23642014-09-24 13:48:37 +02009510 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
9511 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009512out:
9513 return ret;
9514
9515err_size:
9516 put_user(sizeof(*attr), &uattr->size);
9517 ret = -E2BIG;
9518 goto out;
9519}
9520
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009521static int
9522perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009523{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009524 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009525 int ret = -EINVAL;
9526
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009527 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009528 goto set;
9529
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009530 /* don't allow circular references */
9531 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009532 goto out;
9533
Peter Zijlstra0f139302010-05-20 14:35:15 +02009534 /*
9535 * Don't allow cross-cpu buffers
9536 */
9537 if (output_event->cpu != event->cpu)
9538 goto out;
9539
9540 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02009541 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02009542 */
9543 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
9544 goto out;
9545
Peter Zijlstra34f43922015-02-20 14:05:38 +01009546 /*
9547 * Mixing clocks in the same buffer is trouble you don't need.
9548 */
9549 if (output_event->clock != event->clock)
9550 goto out;
9551
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009552 /*
Wang Nan9ecda412016-04-05 14:11:18 +00009553 * Either writing ring buffer from beginning or from end.
9554 * Mixing is not allowed.
9555 */
9556 if (is_write_backward(output_event) != is_write_backward(event))
9557 goto out;
9558
9559 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009560 * If both events generate aux data, they must be on the same PMU
9561 */
9562 if (has_aux(event) && has_aux(output_event) &&
9563 event->pmu != output_event->pmu)
9564 goto out;
9565
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009566set:
9567 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009568 /* Can't redirect output if we've got an active mmap() */
9569 if (atomic_read(&event->mmap_count))
9570 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009571
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009572 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02009573 /* get the rb we want to redirect to */
9574 rb = ring_buffer_get(output_event);
9575 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009576 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009577 }
9578
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009579 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02009580
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009581 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009582unlock:
9583 mutex_unlock(&event->mmap_mutex);
9584
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009585out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009586 return ret;
9587}
9588
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009589static void mutex_lock_double(struct mutex *a, struct mutex *b)
9590{
9591 if (b < a)
9592 swap(a, b);
9593
9594 mutex_lock(a);
9595 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
9596}
9597
Peter Zijlstra34f43922015-02-20 14:05:38 +01009598static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
9599{
9600 bool nmi_safe = false;
9601
9602 switch (clk_id) {
9603 case CLOCK_MONOTONIC:
9604 event->clock = &ktime_get_mono_fast_ns;
9605 nmi_safe = true;
9606 break;
9607
9608 case CLOCK_MONOTONIC_RAW:
9609 event->clock = &ktime_get_raw_fast_ns;
9610 nmi_safe = true;
9611 break;
9612
9613 case CLOCK_REALTIME:
9614 event->clock = &ktime_get_real_ns;
9615 break;
9616
9617 case CLOCK_BOOTTIME:
9618 event->clock = &ktime_get_boot_ns;
9619 break;
9620
9621 case CLOCK_TAI:
9622 event->clock = &ktime_get_tai_ns;
9623 break;
9624
9625 default:
9626 return -EINVAL;
9627 }
9628
9629 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
9630 return -EINVAL;
9631
9632 return 0;
9633}
9634
Peter Zijlstra922813f2017-01-11 21:09:50 +01009635/*
9636 * Variation on perf_event_ctx_lock_nested(), except we take two context
9637 * mutexes.
9638 */
9639static struct perf_event_context *
9640__perf_event_ctx_lock_double(struct perf_event *group_leader,
9641 struct perf_event_context *ctx)
9642{
9643 struct perf_event_context *gctx;
9644
9645again:
9646 rcu_read_lock();
9647 gctx = READ_ONCE(group_leader->ctx);
9648 if (!atomic_inc_not_zero(&gctx->refcount)) {
9649 rcu_read_unlock();
9650 goto again;
9651 }
9652 rcu_read_unlock();
9653
9654 mutex_lock_double(&gctx->mutex, &ctx->mutex);
9655
9656 if (group_leader->ctx != gctx) {
9657 mutex_unlock(&ctx->mutex);
9658 mutex_unlock(&gctx->mutex);
9659 put_ctx(gctx);
9660 goto again;
9661 }
9662
9663 return gctx;
9664}
9665
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009666/**
9667 * sys_perf_event_open - open a performance event, associate it to a task/cpu
9668 *
9669 * @attr_uptr: event_id type attributes for monitoring/sampling
9670 * @pid: target pid
9671 * @cpu: target cpu
9672 * @group_fd: group leader event fd
9673 */
9674SYSCALL_DEFINE5(perf_event_open,
9675 struct perf_event_attr __user *, attr_uptr,
9676 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
9677{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009678 struct perf_event *group_leader = NULL, *output_event = NULL;
9679 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009680 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009681 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009682 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04009683 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07009684 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009685 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04009686 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009687 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009688 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01009689 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00009690 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009691
9692 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02009693 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009694 return -EINVAL;
9695
Jeff Vander Stoepd28f8562016-05-29 14:22:32 -07009696 if (perf_paranoid_any() && !capable(CAP_SYS_ADMIN))
9697 return -EACCES;
9698
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009699 err = perf_copy_attr(attr_uptr, &attr);
9700 if (err)
9701 return err;
9702
9703 if (!attr.exclude_kernel) {
9704 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9705 return -EACCES;
9706 }
9707
9708 if (attr.freq) {
9709 if (attr.sample_freq > sysctl_perf_event_sample_rate)
9710 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02009711 } else {
9712 if (attr.sample_period & (1ULL << 63))
9713 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009714 }
9715
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009716 if (!attr.sample_max_stack)
9717 attr.sample_max_stack = sysctl_perf_event_max_stack;
9718
Stephane Eraniane5d13672011-02-14 11:20:01 +02009719 /*
9720 * In cgroup mode, the pid argument is used to pass the fd
9721 * opened to the cgroup directory in cgroupfs. The cpu argument
9722 * designates the cpu on which to monitor threads from that
9723 * cgroup.
9724 */
9725 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
9726 return -EINVAL;
9727
Yann Droneauda21b0b32014-01-05 21:36:33 +01009728 if (flags & PERF_FLAG_FD_CLOEXEC)
9729 f_flags |= O_CLOEXEC;
9730
9731 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009732 if (event_fd < 0)
9733 return event_fd;
9734
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009735 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04009736 err = perf_fget_light(group_fd, &group);
9737 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009738 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04009739 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009740 if (flags & PERF_FLAG_FD_OUTPUT)
9741 output_event = group_leader;
9742 if (flags & PERF_FLAG_FD_NO_GROUP)
9743 group_leader = NULL;
9744 }
9745
Stephane Eraniane5d13672011-02-14 11:20:01 +02009746 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009747 task = find_lively_task_by_vpid(pid);
9748 if (IS_ERR(task)) {
9749 err = PTR_ERR(task);
9750 goto err_group_fd;
9751 }
9752 }
9753
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009754 if (task && group_leader &&
9755 group_leader->attr.inherit != attr.inherit) {
9756 err = -EINVAL;
9757 goto err_task;
9758 }
9759
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009760 get_online_cpus();
9761
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009762 if (task) {
9763 err = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
9764 if (err)
9765 goto err_cpus;
9766
9767 /*
9768 * Reuse ptrace permission checks for now.
9769 *
9770 * We must hold cred_guard_mutex across this and any potential
9771 * perf_install_in_context() call for this new event to
9772 * serialize against exec() altering our credentials (and the
9773 * perf_event_exit_task() that could imply).
9774 */
9775 err = -EACCES;
9776 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
9777 goto err_cred;
9778 }
9779
Matt Fleming79dff512015-01-23 18:45:42 +00009780 if (flags & PERF_FLAG_PID_CGROUP)
9781 cgroup_fd = pid;
9782
Avi Kivity4dc0da82011-06-29 18:42:35 +03009783 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009784 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009785 if (IS_ERR(event)) {
9786 err = PTR_ERR(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009787 goto err_cred;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009788 }
9789
Vince Weaver53b25332014-05-16 17:12:12 -04009790 if (is_sampling_event(event)) {
9791 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
Vineet Guptaa1396552016-05-09 15:07:40 +05309792 err = -EOPNOTSUPP;
Vince Weaver53b25332014-05-16 17:12:12 -04009793 goto err_alloc;
9794 }
9795 }
9796
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009797 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009798 * Special case software events and allow them to be part of
9799 * any hardware group.
9800 */
9801 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009802
Peter Zijlstra34f43922015-02-20 14:05:38 +01009803 if (attr.use_clockid) {
9804 err = perf_event_set_clock(event, attr.clockid);
9805 if (err)
9806 goto err_alloc;
9807 }
9808
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07009809 if (pmu->task_ctx_nr == perf_sw_context)
9810 event->event_caps |= PERF_EV_CAP_SOFTWARE;
9811
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009812 if (group_leader &&
9813 (is_software_event(event) != is_software_event(group_leader))) {
9814 if (is_software_event(event)) {
9815 /*
9816 * If event and group_leader are not both a software
9817 * event, and event is, then group leader is not.
9818 *
9819 * Allow the addition of software events to !software
9820 * groups, this is safe because software events never
9821 * fail to schedule.
9822 */
9823 pmu = group_leader->pmu;
9824 } else if (is_software_event(group_leader) &&
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07009825 (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009826 /*
9827 * In case the group is a pure software group, and we
9828 * try to add a hardware event, move the whole group to
9829 * the hardware context.
9830 */
9831 move_group = 1;
9832 }
9833 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009834
9835 /*
9836 * Get the target context (task or percpu):
9837 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009838 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009839 if (IS_ERR(ctx)) {
9840 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009841 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009842 }
9843
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009844 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
9845 err = -EBUSY;
9846 goto err_context;
9847 }
9848
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009849 /*
9850 * Look up the group leader (we will attach this event to it):
9851 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009852 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009853 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009854
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009855 /*
9856 * Do not allow a recursive hierarchy (this new sibling
9857 * becoming part of another group-sibling):
9858 */
9859 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009860 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01009861
9862 /* All events in a group should have the same clock */
9863 if (group_leader->clock != event->clock)
9864 goto err_context;
9865
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009866 /*
Mark Rutlandbde66082017-06-22 15:41:38 +01009867 * Make sure we're both events for the same CPU;
9868 * grouping events for different CPUs is broken; since
9869 * you can never concurrently schedule them anyhow.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009870 */
Mark Rutlandbde66082017-06-22 15:41:38 +01009871 if (group_leader->cpu != event->cpu)
9872 goto err_context;
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01009873
Mark Rutlandbde66082017-06-22 15:41:38 +01009874 /*
9875 * Make sure we're both on the same task, or both
9876 * per-CPU events.
9877 */
9878 if (group_leader->ctx->task != ctx->task)
9879 goto err_context;
9880
9881 /*
9882 * Do not allow to attach to a group in a different task
9883 * or CPU context. If we're moving SW events, we'll fix
9884 * this up later, so allow that.
9885 */
9886 if (!move_group && group_leader->ctx != ctx)
9887 goto err_context;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009888
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009889 /*
9890 * Only a group leader can be exclusive or pinned
9891 */
9892 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009893 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009894 }
9895
9896 if (output_event) {
9897 err = perf_event_set_output(event, output_event);
9898 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009899 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009900 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009901
Yann Droneauda21b0b32014-01-05 21:36:33 +01009902 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
9903 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009904 if (IS_ERR(event_file)) {
9905 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +02009906 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009907 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04009908 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009909
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009910 if (move_group) {
Peter Zijlstra922813f2017-01-11 21:09:50 +01009911 gctx = __perf_event_ctx_lock_double(group_leader, ctx);
9912
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009913 if (gctx->task == TASK_TOMBSTONE) {
9914 err = -ESRCH;
9915 goto err_locked;
9916 }
Peter Zijlstra922813f2017-01-11 21:09:50 +01009917
9918 /*
9919 * Check if we raced against another sys_perf_event_open() call
9920 * moving the software group underneath us.
9921 */
9922 if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
9923 /*
9924 * If someone moved the group out from under us, check
9925 * if this new event wound up on the same ctx, if so
9926 * its the regular !move_group case, otherwise fail.
9927 */
9928 if (gctx != ctx) {
9929 err = -EINVAL;
9930 goto err_locked;
9931 } else {
9932 perf_event_ctx_unlock(group_leader, gctx);
9933 move_group = 0;
9934 }
9935 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009936 } else {
9937 mutex_lock(&ctx->mutex);
9938 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009939
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009940 if (ctx->task == TASK_TOMBSTONE) {
9941 err = -ESRCH;
9942 goto err_locked;
9943 }
9944
Peter Zijlstraa7239682015-09-09 19:06:33 +02009945 if (!perf_event_validate_size(event)) {
9946 err = -E2BIG;
9947 goto err_locked;
9948 }
9949
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009950 /*
9951 * Must be under the same ctx::mutex as perf_install_in_context(),
9952 * because we need to serialize with concurrent event creation.
9953 */
9954 if (!exclusive_event_installable(event, ctx)) {
9955 /* exclusive and group stuff are assumed mutually exclusive */
9956 WARN_ON_ONCE(move_group);
9957
9958 err = -EBUSY;
9959 goto err_locked;
9960 }
9961
9962 WARN_ON_ONCE(ctx->parent_ctx);
9963
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009964 /*
9965 * This is the point on no return; we cannot fail hereafter. This is
9966 * where we start modifying current state.
9967 */
9968
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009969 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009970 /*
9971 * See perf_event_ctx_lock() for comments on the details
9972 * of swizzling perf_event::ctx.
9973 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009974 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01009975
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009976 list_for_each_entry(sibling, &group_leader->sibling_list,
9977 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009978 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009979 put_ctx(gctx);
9980 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009981
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009982 /*
9983 * Wait for everybody to stop referencing the events through
9984 * the old lists, before installing it on new lists.
9985 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009986 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009987
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009988 /*
9989 * Install the group siblings before the group leader.
9990 *
9991 * Because a group leader will try and install the entire group
9992 * (through the sibling list, which is still in-tact), we can
9993 * end up with siblings installed in the wrong context.
9994 *
9995 * By installing siblings first we NO-OP because they're not
9996 * reachable through the group lists.
9997 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009998 list_for_each_entry(sibling, &group_leader->sibling_list,
9999 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010010000 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +010010001 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +020010002 get_ctx(ctx);
10003 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010010004
10005 /*
10006 * Removing from the context ends up with disabled
10007 * event. What we want here is event in the initial
10008 * startup state, ready to be add into new context.
10009 */
10010 perf_event__state_init(group_leader);
10011 perf_install_in_context(ctx, group_leader, group_leader->cpu);
10012 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +020010013
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010014 /*
10015 * Now that all events are installed in @ctx, nothing
10016 * references @gctx anymore, so drop the last reference we have
10017 * on it.
10018 */
10019 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010020 }
10021
Peter Zijlstraf73e22a2015-09-09 20:48:22 +020010022 /*
10023 * Precalculate sample_data sizes; do while holding ctx::mutex such
10024 * that we're serialized against further additions and before
10025 * perf_install_in_context() which is the point the event is active and
10026 * can use these values.
10027 */
10028 perf_event__header_size(event);
10029 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010030
Peter Zijlstra78cd2c72016-01-25 14:08:45 +010010031 event->owner = current;
10032
Yan, Zhenge2d37cd2012-06-15 14:31:32 +080010033 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010034 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010035
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010036 if (move_group)
Peter Zijlstra922813f2017-01-11 21:09:50 +010010037 perf_event_ctx_unlock(group_leader, gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010038 mutex_unlock(&ctx->mutex);
10039
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010040 if (task) {
10041 mutex_unlock(&task->signal->cred_guard_mutex);
10042 put_task_struct(task);
10043 }
10044
Yan, Zhengfbfc6232012-06-15 14:31:31 +080010045 put_online_cpus();
10046
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010047 mutex_lock(&current->perf_event_mutex);
10048 list_add_tail(&event->owner_entry, &current->perf_event_list);
10049 mutex_unlock(&current->perf_event_mutex);
10050
Peter Zijlstra8a495422010-05-27 15:47:49 +020010051 /*
10052 * Drop the reference on the group_event after placing the
10053 * new event on the sibling_list. This ensures destruction
10054 * of the group leader will find the pointer to itself in
10055 * perf_group_detach().
10056 */
Al Viro2903ff02012-08-28 12:52:22 -040010057 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040010058 fd_install(event_fd, event_file);
10059 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010060
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010061err_locked:
10062 if (move_group)
Peter Zijlstra922813f2017-01-11 21:09:50 +010010063 perf_event_ctx_unlock(group_leader, gctx);
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010064 mutex_unlock(&ctx->mutex);
10065/* err_file: */
10066 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010067err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010068 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -040010069 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020010070err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +010010071 /*
10072 * If event_file is set, the fput() above will have called ->release()
10073 * and that will take care of freeing the event.
10074 */
10075 if (!event_file)
10076 free_event(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010077err_cred:
10078 if (task)
10079 mutex_unlock(&task->signal->cred_guard_mutex);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020010080err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +080010081 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020010082err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +020010083 if (task)
10084 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +020010085err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -040010086 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040010087err_fd:
10088 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010089 return err;
10090}
10091
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010092/**
10093 * perf_event_create_kernel_counter
10094 *
10095 * @attr: attributes of the counter to create
10096 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -070010097 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010098 */
10099struct perf_event *
10100perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -070010101 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +030010102 perf_overflow_handler_t overflow_handler,
10103 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010104{
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010105 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010106 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010107 int err;
10108
10109 /*
10110 * Get the target context (task or percpu):
10111 */
10112
Avi Kivity4dc0da82011-06-29 18:42:35 +030010113 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +000010114 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010115 if (IS_ERR(event)) {
10116 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010117 goto err;
10118 }
10119
Jiri Olsaf8697762014-08-01 14:33:01 +020010120 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010121 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +020010122
Yan, Zheng4af57ef282014-11-04 21:56:01 -050010123 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010124 if (IS_ERR(ctx)) {
10125 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010126 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010127 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010128
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010129 WARN_ON_ONCE(ctx->parent_ctx);
10130 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010131 if (ctx->task == TASK_TOMBSTONE) {
10132 err = -ESRCH;
10133 goto err_unlock;
10134 }
10135
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010136 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010137 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010138 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010139 }
10140
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010141 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010142 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010143 mutex_unlock(&ctx->mutex);
10144
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010145 return event;
10146
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010147err_unlock:
10148 mutex_unlock(&ctx->mutex);
10149 perf_unpin_context(ctx);
10150 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010151err_free:
10152 free_event(event);
10153err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010154 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010155}
10156EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
10157
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010158void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
10159{
10160 struct perf_event_context *src_ctx;
10161 struct perf_event_context *dst_ctx;
10162 struct perf_event *event, *tmp;
10163 LIST_HEAD(events);
10164
10165 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
10166 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
10167
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010168 /*
10169 * See perf_event_ctx_lock() for comments on the details
10170 * of swizzling perf_event::ctx.
10171 */
10172 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010173 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
10174 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010175 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020010176 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010177 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +020010178 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010179 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010180
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010010181 /*
10182 * Wait for the events to quiesce before re-instating them.
10183 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010184 synchronize_rcu();
10185
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010010186 /*
10187 * Re-instate events in 2 passes.
10188 *
10189 * Skip over group leaders and only install siblings on this first
10190 * pass, siblings will not get enabled without a leader, however a
10191 * leader will enable its siblings, even if those are still on the old
10192 * context.
10193 */
10194 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
10195 if (event->group_leader == event)
10196 continue;
10197
10198 list_del(&event->migrate_entry);
10199 if (event->state >= PERF_EVENT_STATE_OFF)
10200 event->state = PERF_EVENT_STATE_INACTIVE;
10201 account_event_cpu(event, dst_cpu);
10202 perf_install_in_context(dst_ctx, event, dst_cpu);
10203 get_ctx(dst_ctx);
10204 }
10205
10206 /*
10207 * Once all the siblings are setup properly, install the group leaders
10208 * to make it go.
10209 */
Peter Zijlstra98861672013-10-03 16:02:23 +020010210 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
10211 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010212 if (event->state >= PERF_EVENT_STATE_OFF)
10213 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020010214 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010215 perf_install_in_context(dst_ctx, event, dst_cpu);
10216 get_ctx(dst_ctx);
10217 }
10218 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010219 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010220}
10221EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
10222
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010223static void sync_child_event(struct perf_event *child_event,
10224 struct task_struct *child)
10225{
10226 struct perf_event *parent_event = child_event->parent;
10227 u64 child_val;
10228
10229 if (child_event->attr.inherit_stat)
10230 perf_event_read_event(child_event, child);
10231
Peter Zijlstrab5e58792010-05-21 14:43:12 +020010232 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010233
10234 /*
10235 * Add back the child's count to the parent's count:
10236 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +020010237 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010238 atomic64_add(child_event->total_time_enabled,
10239 &parent_event->child_total_time_enabled);
10240 atomic64_add(child_event->total_time_running,
10241 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010242}
10243
10244static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010245perf_event_exit_event(struct perf_event *child_event,
10246 struct perf_event_context *child_ctx,
10247 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010248{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010249 struct perf_event *parent_event = child_event->parent;
10250
Peter Zijlstra1903d502014-07-15 17:27:27 +020010251 /*
10252 * Do not destroy the 'original' grouping; because of the context
10253 * switch optimization the original events could've ended up in a
10254 * random child task.
10255 *
10256 * If we were to destroy the original group, all group related
10257 * operations would cease to function properly after this random
10258 * child dies.
10259 *
10260 * Do destroy all inherited groups, we don't care about those
10261 * and being thorough is better.
10262 */
Peter Zijlstra32132a32016-01-11 15:40:59 +010010263 raw_spin_lock_irq(&child_ctx->lock);
10264 WARN_ON_ONCE(child_ctx->is_active);
10265
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010266 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +010010267 perf_group_detach(child_event);
10268 list_del_event(child_event, child_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +010010269 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +010010270 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010271
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010272 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010273 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010274 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010275 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -040010276 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010277 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010278 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010279 /*
10280 * Child events can be cleaned up.
10281 */
10282
10283 sync_child_event(child_event, child);
10284
10285 /*
10286 * Remove this event from the parent's list
10287 */
10288 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
10289 mutex_lock(&parent_event->child_mutex);
10290 list_del_init(&child_event->child_list);
10291 mutex_unlock(&parent_event->child_mutex);
10292
10293 /*
10294 * Kick perf_poll() for is_event_hup().
10295 */
10296 perf_event_wakeup(parent_event);
10297 free_event(child_event);
10298 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010299}
10300
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010301static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010302{
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010303 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010304 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010305
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010306 WARN_ON_ONCE(child != current);
10307
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010308 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010309 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010310 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010311
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010312 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010313 * In order to reduce the amount of tricky in ctx tear-down, we hold
10314 * ctx::mutex over the entire thing. This serializes against almost
10315 * everything that wants to access the ctx.
10316 *
10317 * The exception is sys_perf_event_open() /
10318 * perf_event_create_kernel_count() which does find_get_context()
10319 * without ctx::mutex (it cannot because of the move_group double mutex
10320 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010321 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010322 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010323
10324 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010325 * In a single ctx::lock section, de-schedule the events and detach the
10326 * context from the task such that we cannot ever get it scheduled back
10327 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010328 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010329 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010330 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020010331
10332 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010333 * Now that the context is inactive, destroy the task <-> ctx relation
10334 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010335 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010336 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
10337 put_ctx(child_ctx); /* cannot be last */
10338 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
10339 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010340
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010341 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010342 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010343
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010344 if (clone_ctx)
10345 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020010346
10347 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010348 * Report the task dead after unscheduling the events so that we
10349 * won't get any samples after PERF_RECORD_EXIT. We can however still
10350 * get a few PERF_RECORD_READ events.
10351 */
10352 perf_event_task(child, child_ctx, 0);
10353
Peter Zijlstraebf905f2014-05-29 19:00:24 +020010354 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010355 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010356
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010357 mutex_unlock(&child_ctx->mutex);
10358
10359 put_ctx(child_ctx);
10360}
10361
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010362/*
10363 * When a child task exits, feed back event values to parent events.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010364 *
10365 * Can be called with cred_guard_mutex held when called from
10366 * install_exec_creds().
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010367 */
10368void perf_event_exit_task(struct task_struct *child)
10369{
Peter Zijlstra88821352010-11-09 19:01:43 +010010370 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010371 int ctxn;
10372
Peter Zijlstra88821352010-11-09 19:01:43 +010010373 mutex_lock(&child->perf_event_mutex);
10374 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
10375 owner_entry) {
10376 list_del_init(&event->owner_entry);
10377
10378 /*
10379 * Ensure the list deletion is visible before we clear
10380 * the owner, closes a race against perf_release() where
10381 * we need to serialize on the owner->perf_event_mutex.
10382 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +010010383 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +010010384 }
10385 mutex_unlock(&child->perf_event_mutex);
10386
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010387 for_each_task_context_nr(ctxn)
10388 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +010010389
10390 /*
10391 * The perf_event_exit_task_context calls perf_event_task
10392 * with child's task_ctx, which generates EXIT events for
10393 * child contexts and sets child->perf_event_ctxp[] to NULL.
10394 * At this point we need to send EXIT events to cpu contexts.
10395 */
10396 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010397}
10398
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010399static void perf_free_event(struct perf_event *event,
10400 struct perf_event_context *ctx)
10401{
10402 struct perf_event *parent = event->parent;
10403
10404 if (WARN_ON_ONCE(!parent))
10405 return;
10406
10407 mutex_lock(&parent->child_mutex);
10408 list_del_init(&event->child_list);
10409 mutex_unlock(&parent->child_mutex);
10410
Al Viroa6fa9412012-08-20 14:59:25 +010010411 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010412
Peter Zijlstra652884f2015-01-23 11:20:10 +010010413 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +020010414 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010415 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +010010416 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010417 free_event(event);
10418}
10419
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010420/*
Peter Zijlstra652884f2015-01-23 11:20:10 +010010421 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010422 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +010010423 *
10424 * Not all locks are strictly required, but take them anyway to be nice and
10425 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010426 */
10427void perf_event_free_task(struct task_struct *task)
10428{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010429 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010430 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010431 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010432
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010433 for_each_task_context_nr(ctxn) {
10434 ctx = task->perf_event_ctxp[ctxn];
10435 if (!ctx)
10436 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010437
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010438 mutex_lock(&ctx->mutex);
Peter Zijlstrac04a9382017-03-16 13:47:48 +010010439 raw_spin_lock_irq(&ctx->lock);
10440 /*
10441 * Destroy the task <-> ctx relation and mark the context dead.
10442 *
10443 * This is important because even though the task hasn't been
10444 * exposed yet the context has been (through child_list).
10445 */
10446 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL);
10447 WRITE_ONCE(ctx->task, TASK_TOMBSTONE);
10448 put_task_struct(task); /* cannot be last */
10449 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010450again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010451 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
10452 group_entry)
10453 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010454
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010455 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
10456 group_entry)
10457 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010458
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010459 if (!list_empty(&ctx->pinned_groups) ||
10460 !list_empty(&ctx->flexible_groups))
10461 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010462
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010463 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010464
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010465 put_ctx(ctx);
10466 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010467}
10468
Peter Zijlstra4e231c72010-09-09 21:01:59 +020010469void perf_event_delayed_put(struct task_struct *task)
10470{
10471 int ctxn;
10472
10473 for_each_task_context_nr(ctxn)
10474 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
10475}
10476
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010477struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +000010478{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010479 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010480
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010481 file = fget_raw(fd);
10482 if (!file)
10483 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +000010484
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010485 if (file->f_op != &perf_fops) {
10486 fput(file);
10487 return ERR_PTR(-EBADF);
10488 }
Kaixu Xiaffe86902015-08-06 07:02:32 +000010489
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010490 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010491}
10492
10493const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
10494{
10495 if (!event)
10496 return ERR_PTR(-EINVAL);
10497
10498 return &event->attr;
10499}
10500
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010501/*
10502 * inherit a event from parent task to child task:
10503 */
10504static struct perf_event *
10505inherit_event(struct perf_event *parent_event,
10506 struct task_struct *parent,
10507 struct perf_event_context *parent_ctx,
10508 struct task_struct *child,
10509 struct perf_event *group_leader,
10510 struct perf_event_context *child_ctx)
10511{
Jiri Olsa1929def2014-09-12 13:18:27 +020010512 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010513 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +020010514 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010515
10516 /*
10517 * Instead of creating recursive hierarchies of events,
10518 * we link inherited events back to the original parent,
10519 * which has a filp for sure, which we use as the reference
10520 * count:
10521 */
10522 if (parent_event->parent)
10523 parent_event = parent_event->parent;
10524
10525 child_event = perf_event_alloc(&parent_event->attr,
10526 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020010527 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010528 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +000010529 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010530 if (IS_ERR(child_event))
10531 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +010010532
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010533 /*
10534 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
10535 * must be under the same lock in order to serialize against
10536 * perf_event_release_kernel(), such that either we must observe
10537 * is_orphaned_event() or they will observe us on the child_list.
10538 */
10539 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +020010540 if (is_orphaned_event(parent_event) ||
10541 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010542 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +010010543 free_event(child_event);
10544 return NULL;
10545 }
10546
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010547 get_ctx(child_ctx);
10548
10549 /*
10550 * Make the child state follow the state of the parent event,
10551 * not its attr.disabled bit. We hold the parent's mutex,
10552 * so we won't race with perf_event_{en, dis}able_family.
10553 */
Jiri Olsa1929def2014-09-12 13:18:27 +020010554 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010555 child_event->state = PERF_EVENT_STATE_INACTIVE;
10556 else
10557 child_event->state = PERF_EVENT_STATE_OFF;
10558
10559 if (parent_event->attr.freq) {
10560 u64 sample_period = parent_event->hw.sample_period;
10561 struct hw_perf_event *hwc = &child_event->hw;
10562
10563 hwc->sample_period = sample_period;
10564 hwc->last_period = sample_period;
10565
10566 local64_set(&hwc->period_left, sample_period);
10567 }
10568
10569 child_event->ctx = child_ctx;
10570 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030010571 child_event->overflow_handler_context
10572 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010573
10574 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -020010575 * Precalculate sample_data sizes
10576 */
10577 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -020010578 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -020010579
10580 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010581 * Link it up in the child's context:
10582 */
Peter Zijlstracee010e2010-09-10 12:51:54 +020010583 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010584 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +020010585 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010586
10587 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010588 * Link this into the parent event's child list
10589 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010590 list_add_tail(&child_event->child_list, &parent_event->child_list);
10591 mutex_unlock(&parent_event->child_mutex);
10592
10593 return child_event;
10594}
10595
10596static int inherit_group(struct perf_event *parent_event,
10597 struct task_struct *parent,
10598 struct perf_event_context *parent_ctx,
10599 struct task_struct *child,
10600 struct perf_event_context *child_ctx)
10601{
10602 struct perf_event *leader;
10603 struct perf_event *sub;
10604 struct perf_event *child_ctr;
10605
10606 leader = inherit_event(parent_event, parent, parent_ctx,
10607 child, NULL, child_ctx);
10608 if (IS_ERR(leader))
10609 return PTR_ERR(leader);
10610 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
10611 child_ctr = inherit_event(sub, parent, parent_ctx,
10612 child, leader, child_ctx);
10613 if (IS_ERR(child_ctr))
10614 return PTR_ERR(child_ctr);
10615 }
10616 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010617}
10618
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010619static int
10620inherit_task_group(struct perf_event *event, struct task_struct *parent,
10621 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010622 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010623 int *inherited_all)
10624{
10625 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010626 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010627
10628 if (!event->attr.inherit) {
10629 *inherited_all = 0;
10630 return 0;
10631 }
10632
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010633 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010634 if (!child_ctx) {
10635 /*
10636 * This is executed from the parent task context, so
10637 * inherit events that have been marked for cloning.
10638 * First allocate and initialize a context for the
10639 * child.
10640 */
10641
Jiri Olsa734df5a2013-07-09 17:44:10 +020010642 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010643 if (!child_ctx)
10644 return -ENOMEM;
10645
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010646 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010647 }
10648
10649 ret = inherit_group(event, parent, parent_ctx,
10650 child, child_ctx);
10651
10652 if (ret)
10653 *inherited_all = 0;
10654
10655 return ret;
10656}
10657
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010658/*
10659 * Initialize the perf_event context in task_struct
10660 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +020010661static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010662{
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010663 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010664 struct perf_event_context *cloned_ctx;
10665 struct perf_event *event;
10666 struct task_struct *parent = current;
10667 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010668 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010669 int ret = 0;
10670
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010671 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010672 return 0;
10673
10674 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010675 * If the parent's context is a clone, pin it so it won't get
10676 * swapped under us.
10677 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010678 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +020010679 if (!parent_ctx)
10680 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010681
10682 /*
10683 * No need to check if parent_ctx != NULL here; since we saw
10684 * it non-NULL earlier, the only reason for it to become NULL
10685 * is if we exit, and since we're currently in the middle of
10686 * a fork we can't be exiting at the same time.
10687 */
10688
10689 /*
10690 * Lock the parent list. No need to lock the child - not PID
10691 * hashed yet and not running, so nobody can access it.
10692 */
10693 mutex_lock(&parent_ctx->mutex);
10694
10695 /*
10696 * We dont have to disable NMIs - we are only looking at
10697 * the list, not manipulating it:
10698 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010699 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010700 ret = inherit_task_group(event, parent, parent_ctx,
10701 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010702 if (ret)
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010703 goto out_unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010704 }
10705
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010706 /*
10707 * We can't hold ctx->lock when iterating the ->flexible_group list due
10708 * to allocations, but we need to prevent rotation because
10709 * rotate_ctx() will change the list from interrupt context.
10710 */
10711 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10712 parent_ctx->rotate_disable = 1;
10713 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
10714
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010715 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010716 ret = inherit_task_group(event, parent, parent_ctx,
10717 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010718 if (ret)
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010719 goto out_unlock;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010720 }
10721
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010722 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10723 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010724
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010725 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010726
Peter Zijlstra05cbaa22009-12-30 16:00:35 +010010727 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010728 /*
10729 * Mark the child context as a clone of the parent
10730 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010731 *
10732 * Note that if the parent is a clone, the holding of
10733 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010734 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010735 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010736 if (cloned_ctx) {
10737 child_ctx->parent_ctx = cloned_ctx;
10738 child_ctx->parent_gen = parent_ctx->parent_gen;
10739 } else {
10740 child_ctx->parent_ctx = parent_ctx;
10741 child_ctx->parent_gen = parent_ctx->generation;
10742 }
10743 get_ctx(child_ctx->parent_ctx);
10744 }
10745
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010746 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010747out_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010748 mutex_unlock(&parent_ctx->mutex);
10749
10750 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010751 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010752
10753 return ret;
10754}
10755
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010756/*
10757 * Initialize the perf_event context in task_struct
10758 */
10759int perf_event_init_task(struct task_struct *child)
10760{
10761 int ctxn, ret;
10762
Oleg Nesterov8550d7c2011-01-19 19:22:28 +010010763 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
10764 mutex_init(&child->perf_event_mutex);
10765 INIT_LIST_HEAD(&child->perf_event_list);
10766
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010767 for_each_task_context_nr(ctxn) {
10768 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010769 if (ret) {
10770 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010771 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010772 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010773 }
10774
10775 return 0;
10776}
10777
Paul Mackerras220b1402010-03-10 20:45:52 +110010778static void __init perf_event_init_all_cpus(void)
10779{
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010780 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +110010781 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +110010782
10783 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010784 swhash = &per_cpu(swevent_htable, cpu);
10785 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +000010786 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Kan Liangf2fb6be2016-03-23 11:24:37 -070010787
10788 INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu));
10789 raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu));
Peter Zijlstrae48c1782016-07-06 09:18:30 +020010790
10791 INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +110010792 }
10793}
10794
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010795int perf_event_init_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010796{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010797 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010798
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010799 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +000010800 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010801 struct swevent_hlist *hlist;
10802
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010803 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
10804 WARN_ON(!hlist);
10805 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010806 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010807 mutex_unlock(&swhash->hlist_mutex);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010808 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010809}
10810
Dave Young2965faa2015-09-09 15:38:55 -070010811#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010812static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010813{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010814 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010815 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
10816 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010817
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010818 raw_spin_lock(&ctx->lock);
10819 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010820 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010821 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010822}
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010823
10824static void perf_event_exit_cpu_context(int cpu)
10825{
10826 struct perf_event_context *ctx;
10827 struct pmu *pmu;
10828 int idx;
10829
10830 idx = srcu_read_lock(&pmus_srcu);
10831 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +020010832 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010833
10834 mutex_lock(&ctx->mutex);
10835 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
10836 mutex_unlock(&ctx->mutex);
10837 }
10838 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010839}
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010840#else
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010841
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010842static void perf_event_exit_cpu_context(int cpu) { }
10843
10844#endif
10845
10846int perf_event_exit_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010847{
Peter Zijlstrae3703f82014-02-24 12:06:12 +010010848 perf_event_exit_cpu_context(cpu);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010849 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010850}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010851
Peter Zijlstrac2774432010-12-08 15:29:02 +010010852static int
10853perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
10854{
10855 int cpu;
10856
10857 for_each_online_cpu(cpu)
10858 perf_event_exit_cpu(cpu);
10859
10860 return NOTIFY_OK;
10861}
10862
10863/*
10864 * Run the perf reboot notifier at the very last possible moment so that
10865 * the generic watchdog code runs as long as possible.
10866 */
10867static struct notifier_block perf_reboot_notifier = {
10868 .notifier_call = perf_reboot,
10869 .priority = INT_MIN,
10870};
10871
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010872void __init perf_event_init(void)
10873{
Jason Wessel3c502e72010-11-04 17:33:01 -050010874 int ret;
10875
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010876 idr_init(&pmu_idr);
10877
Paul Mackerras220b1402010-03-10 20:45:52 +110010878 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010879 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010880 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
10881 perf_pmu_register(&perf_cpu_clock, NULL, -1);
10882 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010883 perf_tp_register();
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010884 perf_event_init_cpu(smp_processor_id());
Peter Zijlstrac2774432010-12-08 15:29:02 +010010885 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -050010886
10887 ret = init_hw_breakpoint();
10888 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +020010889
Jiri Olsab01c3a02012-03-23 15:41:20 +010010890 /*
10891 * Build time assertion that we keep the data_head at the intended
10892 * location. IOW, validation we got the __reserved[] size right.
10893 */
10894 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
10895 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010896}
Peter Zijlstraabe43402010-11-17 23:17:37 +010010897
Cody P Schaferfd979c02015-01-30 13:45:57 -080010898ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
10899 char *page)
10900{
10901 struct perf_pmu_events_attr *pmu_attr =
10902 container_of(attr, struct perf_pmu_events_attr, attr);
10903
10904 if (pmu_attr->event_str)
10905 return sprintf(page, "%s\n", pmu_attr->event_str);
10906
10907 return 0;
10908}
Thomas Gleixner675965b2016-02-22 22:19:27 +000010909EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -080010910
Peter Zijlstraabe43402010-11-17 23:17:37 +010010911static int __init perf_event_sysfs_init(void)
10912{
10913 struct pmu *pmu;
10914 int ret;
10915
10916 mutex_lock(&pmus_lock);
10917
10918 ret = bus_register(&pmu_bus);
10919 if (ret)
10920 goto unlock;
10921
10922 list_for_each_entry(pmu, &pmus, entry) {
10923 if (!pmu->name || pmu->type < 0)
10924 continue;
10925
10926 ret = pmu_dev_alloc(pmu);
10927 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
10928 }
10929 pmu_bus_running = 1;
10930 ret = 0;
10931
10932unlock:
10933 mutex_unlock(&pmus_lock);
10934
10935 return ret;
10936}
10937device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010938
10939#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -040010940static struct cgroup_subsys_state *
10941perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010942{
10943 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +020010944
Li Zefan1b15d052011-03-03 14:26:06 +080010945 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010946 if (!jc)
10947 return ERR_PTR(-ENOMEM);
10948
Stephane Eraniane5d13672011-02-14 11:20:01 +020010949 jc->info = alloc_percpu(struct perf_cgroup_info);
10950 if (!jc->info) {
10951 kfree(jc);
10952 return ERR_PTR(-ENOMEM);
10953 }
10954
Stephane Eraniane5d13672011-02-14 11:20:01 +020010955 return &jc->css;
10956}
10957
Tejun Heoeb954192013-08-08 20:11:23 -040010958static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010959{
Tejun Heoeb954192013-08-08 20:11:23 -040010960 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
10961
Stephane Eraniane5d13672011-02-14 11:20:01 +020010962 free_percpu(jc->info);
10963 kfree(jc);
10964}
10965
10966static int __perf_cgroup_move(void *info)
10967{
10968 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010969 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010970 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010971 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010972 return 0;
10973}
10974
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010975static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010976{
Tejun Heobb9d97b2011-12-12 18:12:21 -080010977 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010978 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -080010979
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010980 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -080010981 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010982}
10983
Tejun Heo073219e2014-02-08 10:36:58 -050010984struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -080010985 .css_alloc = perf_cgroup_css_alloc,
10986 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -080010987 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +020010988};
10989#endif /* CONFIG_CGROUP_PERF */